1/* $OpenBSD: ldasm.S,v 1.3 2021/06/26 14:50:25 kettenis Exp $ */ 2 3/* 4 * Copyright (c) 2016,2021 Dale Rahn <drahn@openbsd.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29#define DL_DATA_SIZE (16 * 8) /* needs to be 8(16?) byte aligned */ 30#include <machine/asm.h> 31#include <sys/syscall.h> 32 33 .option norelax 34 .section .boot.text,"ax",@progbits 35 _ALIGN_TEXT 36 .globl _dl_start 37 .type _dl_start,@function 38_dl_start: 39 mv a0, sp 40 mv fp, sp 41 42 addi sp, sp, -(8+8+DL_DATA_SIZE) // dl_data size 43 addi s10, sp, 8 // dl_data 44 45 mv a1, s10 // dl_data 46 471: auipc a2, %pcrel_hi(_DYNAMIC) /* &_DYNAMIC */ 48 addi a2, a2, %pcrel_lo(1b) 49 50 call _dl_boot_bind 51 52 ld a0, (fp) // load argc 53 addi a1, fp, 0x0008 // argv 54 slli a2, a0, 0x3 55 add a2, a1, a2 56 addi a2, a2, 0x0008 // compute envp into a2 57 58 // _dl_boot(argv, envp, loff, dl_data) 59 mv a0, a1 // argv 60 mv a1, a2 // envp 61 ld a2, (7*8)(s10) // loff from dl_data 62 mv a3, s10 // dl_data 63 64 call _dl_boot 65 66 mv sp, fp // move stack back 67 mv fp, zero // clear frame back pointer 68 692: auipc a3, %pcrel_hi(_dl_dtors) /* cleanup */ 70 addi a3, a3, %pcrel_lo(2b) 71 72 jr a0 73END(_dl_start) 74 75ENTRY(_dl_bind_start) 76 /* 77 * t0 is the "link map" 78 * t1 is the .got.plt offset 79 */ 80 81 /* save parameter/result registers */ 82 addi sp, sp, -(10*8) /* should be aligned well enough */ 83 sd ra, (9*8)(sp) 84 sd a0, (0*8)(sp) 85 sd a1, (1*8)(sp) 86 sd a2, (2*8)(sp) 87 sd a3, (3*8)(sp) 88 sd a4, (4*8)(sp) 89 sd a5, (5*8)(sp) 90 sd a6, (6*8)(sp) 91 sd a7, (7*8)(sp) 92 93 /* 94 * no need to save the FP registers as ld.so is compiled such that 95 * it doesn't touch them 96 */ 97 98 mv a0, t0 99 srli a1, t1, 3 100 jal _dl_bind 101 mv t0, a0 102 103 /* restore parameter/result registers */ 104 ld a0, (0*8)(sp) 105 ld a1, (1*8)(sp) 106 ld a2, (2*8)(sp) 107 ld a3, (3*8)(sp) 108 ld a4, (4*8)(sp) 109 ld a5, (5*8)(sp) 110 ld a6, (6*8)(sp) 111 ld a7, (7*8)(sp) 112 ld ra, (9*8)(sp) 113 addi sp, sp, (10*8) 114 115 jr t0 116END(_dl_bind_start) 117 118ENTRY(_rtld_tlsdesc) 119 RETGUARD_SETUP(_rtld_tlsdesc, x15) 120 ld a0, 8(a0) 121 RETGUARD_CHECK(_rtld_tlsdesc, x15) 122 ret 123END(_rtld_tlsdesc) 124