1/* $OpenBSD: ldasm.S,v 1.16 2015/11/15 03:41:24 deraadt Exp $ */ 2 3/* 4 * Copyright (c) 1998-2002 Opsycon AB, Sweden. 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#include <machine/asm.h> 30#include <sys/syscall.h> 31#include <SYS.h> 32 33/* Stack at this stage is: 34 * struct stack { 35 * int kargc; 36 * char *kargv[1]; size depends on kargc 37 * char kargstr[1]; size varies 38 * char kenvstr[1]; size varies 39 * }; 40 */ 41 42FRAMESZ= MKFSIZ(4,16) 43GPOFF= FRAMESZ-2*REGSZ 44RAOFF= FRAMESZ-1*REGSZ 45 46LEAF(_dl_start, FRAMESZ) /* Not really LEAF, but we simplify */ 47 PTR_SUBU sp, FRAMESZ # Some space. 48 SETUP_GP64(GPOFF, _dl_start) 49 50 LA s1, 1f 51 bgezal zero, 1f 521: 53 PTR_SUBU s0, ra, s1 # This is the load offset 54 55 PTR_ADDU a0, sp, FRAMESZ # Where stack info is. 56 PTR_ADDU a1, sp, 0 # Where fast AUX info will be. 57 LA t9, _dl_boot_bind 58 PTR_ADDU t9, s0 59 jalr t9 # Relocate ourself. 60 61 REG_L a3, FRAMESZ(sp) # argc 62 PTR_ADDU a0, sp, FRAMESZ+REGSZ # argv 63 PTR_ADDU a1, a0, REGSZ 64 PTR_SLL a3, a3, LOGREGSZ 65 PTR_ADDU a1, a3 66 PTR_ADDU a3, sp, 0 # Where fast AUX info will be. 67 move a2, s0 # Load offset 68 jal _dl_boot # Go do the linking. 69 70 move t9, v0 # Entry address from _dl_boot. 71 LA v0, _dl_dtors # cleanup 72 73 RESTORE_GP64 74 PTR_ADDU sp, FRAMESZ # Restore stack pointer. 75 move ra, zero # Mark last stack frame. 76 j t9 # Go execute the 'real' program. 77END(_dl_start) 78 79FRAMESZ= MKFSIZ(4,16) 80GPOFF= FRAMESZ-2*REGSZ 81RAOFF= FRAMESZ-1*REGSZ 82A0OFF= FRAMESZ-3*REGSZ 83A1OFF= FRAMESZ-4*REGSZ 84A2OFF= FRAMESZ-5*REGSZ 85A3OFF= FRAMESZ-6*REGSZ 86A4OFF= FRAMESZ-7*REGSZ 87A5OFF= FRAMESZ-8*REGSZ 88A6OFF= FRAMESZ-9*REGSZ 89A7OFF= FRAMESZ-10*REGSZ 90S0OFF= FRAMESZ-11*REGSZ 91 92 .globl _dl_bind_start 93 .ent _dl_bind_start, 0 94_dl_bind_start: 95 ld v1, -32744(gp) 96 PTR_SUBU sp, FRAMESZ 97 SETUP_GP64(GPOFF, _dl_bind_start) 98 REG_S a0, A0OFF(sp) 99 REG_S a1, A1OFF(sp) 100 REG_S a2, A2OFF(sp) 101 REG_S a3, A3OFF(sp) 102 REG_S a4, A4OFF(sp) 103 REG_S a5, A5OFF(sp) 104 REG_S a6, A6OFF(sp) 105 REG_S a7, A7OFF(sp) 106 REG_S $15, RAOFF(sp) 107 REG_S s0, S0OFF(sp) 108 move s0, sp 109 move a0, v1 110 move a1, t8 111 jal _dl_bind 112 113 move sp, s0 114 REG_L ra, RAOFF(sp) 115 REG_L s0, S0OFF(sp) 116 REG_L a0, A0OFF(sp) 117 REG_L a1, A1OFF(sp) 118 REG_L a2, A2OFF(sp) 119 REG_L a3, A3OFF(sp) 120 REG_L a4, A4OFF(sp) 121 REG_L a5, A5OFF(sp) 122 REG_L a6, A6OFF(sp) 123 REG_L a7, A7OFF(sp) 124 RESTORE_GP64 125 PTR_ADDU sp, FRAMESZ 126 move t9, v0 127 jr t9 128 .end _dl_bind_start 129 130#define DL_SYSCALL(n) DL_SYSCALL2(n,n) 131#define DL_SYSCALL_NOERR(n) DL_SYSCALL2_NOERR(n,n) 132#define DL_SYSCALL2(n,c) \ 133NLEAF(_dl_##n,0) \ 134 __DO_SYSCALL(c); \ 135 bnez a3, _dl_cerror; \ 136 j ra; \ 137END(_dl_##n) 138#define DL_SYSCALL2_NOERR(n,c) \ 139NLEAF(_dl_##n,0) \ 140 __DO_SYSCALL(c); \ 141 j ra; \ 142END(_dl_##n) 143 144_dl_cerror: 145 subu v0, zero, v0 146 j ra 147 148DL_SYSCALL(close) 149DL_SYSCALL_NOERR(exit) 150DL_SYSCALL(fstat) 151DL_SYSCALL2(getcwd,__getcwd) 152DL_SYSCALL(getdents) 153DL_SYSCALL(getentropy) 154DL_SYSCALL(sendsyslog) 155DL_SYSCALL(pledge) 156DL_SYSCALL(gettimeofday) 157DL_SYSCALL_NOERR(issetugid) 158DL_SYSCALL(lstat) 159DL_SYSCALL(mprotect) 160DL_SYSCALL(munmap) 161DL_SYSCALL(open) 162DL_SYSCALL(read) 163DL_SYSCALL(readlink) 164DL_SYSCALL2(_syscall,__syscall) 165DL_SYSCALL(sysctl) 166DL_SYSCALL(utrace) 167DL_SYSCALL(write) 168