1/* $OpenBSD: ldasm.S,v 1.30 2019/05/10 13:29:21 guenther Exp $ */ 2 3/* 4 * Copyright (c) 2002,2004 Dale Rahn 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 AUTHOR ``AS IS'' AND ANY EXPRESS 17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 31#define DL_DATA_SIZE (16*8) /* 16 * sizeof(ELF_Addr) */ 32#define DL_LOFF_OFFSET (7*8) /* index 7 */ 33#include <machine/asm.h> 34 35 .section .boot.text,"ax",@progbits 36 .align 16,0xcc 37 .globl _dl_start 38 .type _dl_start,@function 39_dl_start: 40 movq %rsp, %r12 # save stack pointer for _rtld 41 42 subq $8, %rsp # align stack 43 andq $~15, %rsp 44 addq $8, %rsp 45 46 pushq %rbx # save ps_strings 47 subq $DL_DATA_SIZE, %rsp # allocate dl_data 48 49 leaq _DYNAMIC(%rip),%rdx # &_DYNAMIC 50 movq %rsp, %rsi # dl_data for dl_boot_bind 51 movq %r12, %rdi # load saved SP for dl_boot_bind 52 call _dl_boot_bind@PLT # _dl_boot_bind(sp,dl_data,dynamicp) 53 54 movq %rsp, %rcx # dl_data 55 movq DL_LOFF_OFFSET(%rsp), %rdx # loff from dl_data 56 57 movq (%r12), %rdi 58 leaq 16(%r12,%rdi,8), %rsi # envp 59 movq %r12, %rdi 60 addq $8,%rdi # argv 61 call _dl_boot@PLT # _dl_boot(argv,envp,loff,dl_data) 62 63 addq $DL_DATA_SIZE,%rsp # return dl_data 64 65 leaq _dl_dtors(%rip), %rdx # %rdx = cleanup 66 movq %r12, %rsp 67 jmp *%rax 68END(_dl_start) 69 70_ENTRY(_dl_bind_start) 71 .cfi_startproc 72 .cfi_adjust_cfa_offset 16 73 pushfq # save registers 74 .cfi_adjust_cfa_offset 8 75 /*.cfi_offset %rflags, -16 */ 76 pushq %rax 77 .cfi_adjust_cfa_offset 8 78 .cfi_offset %rax, -24 79 pushq %rcx 80 .cfi_adjust_cfa_offset 8 81 .cfi_offset %rcx, -32 82 pushq %rdx 83 .cfi_adjust_cfa_offset 8 84 .cfi_offset %rdx, -40 85 pushq %rsi 86 .cfi_adjust_cfa_offset 8 87 .cfi_offset %rsi, -48 88 pushq %rdi 89 .cfi_adjust_cfa_offset 8 90 .cfi_offset %rdi, -56 91 pushq %r8 92 .cfi_adjust_cfa_offset 8 93 .cfi_offset %r8, -64 94 pushq %r9 95 .cfi_adjust_cfa_offset 8 96 .cfi_offset %r9, -72 97 pushq %r10 98 .cfi_adjust_cfa_offset 8 99 .cfi_offset %r10, -80 100 pushq %r11 101 .cfi_adjust_cfa_offset 8 102 .cfi_offset %r11, -88 103 104 movq 80(%rsp), %rdi # Copy of reloff 105 movq 88(%rsp), %rsi # Copy of obj 106 call _dl_bind@PLT # Call the binder 107 movq %rax,88(%rsp) # Store function to be called in obj 108 109 popq %r11 # restore registers 110 .cfi_adjust_cfa_offset -8 111 .cfi_restore %r11 112 popq %r10 113 .cfi_adjust_cfa_offset -8 114 .cfi_restore %r10 115 popq %r9 116 .cfi_adjust_cfa_offset -8 117 .cfi_restore %r9 118 popq %r8 119 .cfi_adjust_cfa_offset -8 120 .cfi_restore %r8 121 popq %rdi 122 .cfi_adjust_cfa_offset -8 123 .cfi_restore %rdi 124 popq %rsi 125 .cfi_adjust_cfa_offset -8 126 .cfi_restore %rsi 127 popq %rdx 128 .cfi_adjust_cfa_offset -8 129 .cfi_restore %rdx 130 popq %rcx 131 .cfi_adjust_cfa_offset -8 132 .cfi_restore %rcx 133 popq %rax 134 .cfi_adjust_cfa_offset -8 135 .cfi_restore %rax 136 popfq 137 .cfi_adjust_cfa_offset -8 138 /*.cfi_restore %rflags */ 139 140 leaq 8(%rsp),%rsp # Discard reloff, do not change eflags 141 .cfi_adjust_cfa_offset -8 142 ret 143 .cfi_endproc 144END(_dl_bind_start) 145