1 /* $OpenBSD: md_init.h,v 1.11 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 #define MD_SECT_CALL_FUNC(section, func) \ 37 __asm (".section "#section", \"ax\"\n" \ 38 " call " #func "\n" \ 39 " .previous") 40 41 #define MD_SECTION_PROLOGUE(sect, entry_pt) \ 42 __asm ( \ 43 ".section "#sect",\"ax\",@progbits \n" \ 44 " .globl " #entry_pt " \n" \ 45 " .type " #entry_pt ",@function \n" \ 46 " .align 16 \n" \ 47 #entry_pt": \n" \ 48 " endbr64 \n" \ 49 " subq $8,%rsp \n" \ 50 " .previous") 51 52 53 #define MD_SECTION_EPILOGUE(sect) \ 54 __asm ( \ 55 ".section "#sect",\"ax\",@progbits \n" \ 56 " addq $8,%rsp \n" \ 57 " ret \n" \ 58 " .previous") 59 60 61 #define MD_CRT0_START \ 62 __asm( \ 63 ".text \n" \ 64 " .align 8 \n" \ 65 " .globl __start \n" \ 66 " .globl _start \n" \ 67 "_start: \n" \ 68 "__start: \n" \ 69 " endbr64 \n" \ 70 " movq %rdx,%rcx \n" \ 71 " movq (%rsp),%rdi \n" \ 72 " leaq 16(%rsp,%rdi,8),%rdx \n" \ 73 " leaq 8(%rsp),%rsi \n" \ 74 " subq $8,%rsp \n" \ 75 " andq $~15,%rsp \n" \ 76 " addq $8,%rsp \n" \ 77 " jmp ___start \n" \ 78 " .previous") 79 80 #define MD_RCRT0_START \ 81 __asm( \ 82 ".text \n" \ 83 " .align 8 \n" \ 84 " .globl __start \n" \ 85 " .type __start,@function \n" \ 86 "_start: \n" \ 87 "__start: \n" \ 88 " endbr64 \n" \ 89 " movq %rsp, %r12 \n" \ 90 " subq $8, %rsp \n" \ 91 " andq $~15, %rsp \n" \ 92 " addq $8, %rsp \n" \ 93 " pushq %rbx \n" \ 94 " subq $(16*8), %rsp \n" \ 95 " leaq _DYNAMIC(%rip),%rdx \n" \ 96 " movq %rsp, %rsi \n" \ 97 " movq %r12, %rdi \n" \ 98 " call _dl_boot_bind@PLT \n" \ 99 " \n" \ 100 " movq $0, %rcx \n" \ 101 " movq %r12, %rsp \n" \ 102 " movq (%rsp),%rdi \n" \ 103 " leaq 16(%rsp,%rdi,8),%rdx \n" \ 104 " leaq 8(%rsp),%rsi \n" \ 105 " subq $8,%rsp \n" \ 106 " andq $~15,%rsp \n" \ 107 " addq $8,%rsp \n" \ 108 " jmp ___start \n" \ 109 " \n" \ 110 " .global _csu_abort \n" \ 111 " .type _csu_abort,@function \n" \ 112 " .align 8 \n" \ 113 "_csu_abort: \n" \ 114 " endbr64 \n" \ 115 " int3 \n" \ 116 " .previous") 117