1/* $OpenBSD: ldasm.S,v 1.6 2010/03/27 20:45:09 kettenis 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 32/* Stack at this stage is: 33 * struct stack { 34 * int kargc; 35 * char *kargv[1]; size depends on kargc 36 * char kargstr[1]; size varies 37 * char kenvstr[1]; size varies 38 * }; 39 */ 40 41FRAMESZ= MKFSIZ(4,16) 42GPOFF= FRAMESZ-2*REGSZ 43RAOFF= FRAMESZ-1*REGSZ 44 45LEAF(_dl_start, FRAMESZ) /* Not really LEAF, but we simplify */ 46 PTR_SUBU sp, FRAMESZ # Some space. 47 SETUP_GP64(GPOFF, _dl_start) 48 49 LA s1, 1f 50 bgezal zero, 1f 511: 52 PTR_SUBU s0, ra, s1 # This is the load offset 53 LA t0, _fdata 54 PTR_SRL t0, 20 # check if distance is > 2**16. 55 beqz t0, 2f 56 li t0, 0x10000 57 58 li t0, 0x100000 592: 60 61 # This is a hack to change protection of .rodata so it 62 # can be relocated. A better way to find the location 63 # of .rodata should probably be used. 64 # We know that .rodata is aligned on 0x100000 or 0x10000 65 # and is at most 64 k in size. 66 li v0, SYS_mprotect 67 or a0, ra, 0xfff 68 xor a0, 0xfff 69 PTR_ADDU a0, t0 70 li a1, 0x10000 71 li a2, 7 /* (PROT_READ|PROT_WRITE|PROT_EXEC) */ 72 syscall 73 74 PTR_ADDU a0, sp, FRAMESZ # Where stack info is. 75 PTR_ADDU a1, sp, 0 # Where fast AUX info will be. 76 LA t9, _dl_boot_bind 77 PTR_ADDU t9, s0 78 jalr t9 # Relocate ourself. 79 80 REG_L a3, FRAMESZ(sp) # argc 81 PTR_ADDU a0, sp, FRAMESZ+REGSZ # argv 82 PTR_ADDU a1, a0, REGSZ 83 PTR_SLL a3, a3, LOGREGSZ 84 PTR_ADDU a1, a3 85 PTR_ADDU a3, sp, 0 # Where fast AUX info will be. 86 move a2, s0 # Load offset 87 jal _dl_boot # Go do the linking. 88 89 RESTORE_GP64 90 PTR_ADDU sp, FRAMESZ # Restore stack pointer. 91 move t9, v0 # Entry address from _dl_boot. 92 j t9 # Go execute the 'real' program. 93END(_dl_start) 94 95LEAF(_dl__syscall, 0) 96 li v0, SYS___syscall # Indirect syscall. 97 syscall 98 bne a3, zero, 1f 99 j ra 1001: 101 li v0, -1 102 j ra 103END(_dl__syscall) 104 105FRAMESZ= MKFSIZ(4,16) 106GPOFF= FRAMESZ-2*REGSZ 107RAOFF= FRAMESZ-1*REGSZ 108A0OFF= FRAMESZ-3*REGSZ 109A1OFF= FRAMESZ-4*REGSZ 110A2OFF= FRAMESZ-5*REGSZ 111A3OFF= FRAMESZ-6*REGSZ 112A4OFF= FRAMESZ-7*REGSZ 113A5OFF= FRAMESZ-8*REGSZ 114A6OFF= FRAMESZ-9*REGSZ 115A7OFF= FRAMESZ-10*REGSZ 116S0OFF= FRAMESZ-11*REGSZ 117 118 .globl _dl_bind_start 119 .ent _dl_bind_start, 0 120_dl_bind_start: 121 ld v1, -32744(gp) 122 PTR_SUBU sp, FRAMESZ 123 SETUP_GP64(GPOFF, _dl_bind_start) 124 REG_S a0, A0OFF(sp) 125 REG_S a1, A1OFF(sp) 126 REG_S a2, A2OFF(sp) 127 REG_S a3, A3OFF(sp) 128 REG_S a4, A4OFF(sp) 129 REG_S a5, A5OFF(sp) 130 REG_S a6, A6OFF(sp) 131 REG_S a7, A7OFF(sp) 132 REG_S $15, RAOFF(sp) 133 REG_S s0, S0OFF(sp) 134 move s0, sp 135 move a0, v1 136 move a1, t8 137 jal _dl_bind 138 139 move sp, s0 140 REG_L ra, RAOFF(sp) 141 REG_L s0, S0OFF(sp) 142 REG_L a0, A0OFF(sp) 143 REG_L a1, A1OFF(sp) 144 REG_L a2, A2OFF(sp) 145 REG_L a3, A3OFF(sp) 146 REG_L a4, A4OFF(sp) 147 REG_L a5, A5OFF(sp) 148 REG_L a6, A6OFF(sp) 149 REG_L a7, A7OFF(sp) 150 RESTORE_GP64 151 PTR_ADDU sp, FRAMESZ 152 move t9, v0 153 jr t9 154 .end _dl_bind_start 155