rtld_start.S revision 210629
1204687Simp/* $NetBSD: rtld_start.S,v 1.10 2009/12/14 00:41:19 matt Exp $ */ 2177924Simp 3177924Simp/* 4177924Simp * Copyright 1997 Michael L. Hitch <mhitch@montana.edu> 5177924Simp * Portions copyright 2002 Charles M. Hannum <root@ihack.net> 6177924Simp * All rights reserved. 7177924Simp * 8177924Simp * Redistribution and use in source and binary forms, with or without 9177924Simp * modification, are permitted provided that the following conditions 10177924Simp * are met: 11177924Simp * 1. Redistributions of source code must retain the above copyright 12177924Simp * notice, this list of conditions and the following disclaimer. 13177924Simp * 2. Redistributions in binary form must reproduce the above copyright 14177924Simp * notice, this list of conditions and the following disclaimer in the 15177924Simp * documentation and/or other materials provided with the distribution. 16177924Simp * 3. The name of the author may not be used to endorse or promote products 17177924Simp * derived from this software without specific prior written permission. 18177924Simp * 19177924Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20177924Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21177924Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22177924Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23177924Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24177924Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25177924Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26177924Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27177924Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28177924Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29204687Simp * 30204687Simp * $FreeBSD: head/libexec/rtld-elf/mips/rtld_start.S 210629 2010-07-29 20:18:52Z jchandra $ 31177924Simp */ 32177924Simp 33177924Simp#include <machine/asm.h> 34177924Simp 35177924Simp.globl _C_LABEL(_rtld_relocate_nonplt_self) 36177924Simp.globl _C_LABEL(_rtld) 37177924Simp 38204687Simp#define PTR_SIZE (1<<PTR_SCALESHIFT) 39204687Simp 40204687Simp/* 41204687Simp * a0 stack pointer 42204687Simp * a1 rtld cleanup (filled in by dynamic loader) 43204687Simp * a2 rtld object (filled in by dynamic loader) 44204687Simp * a3 ps_strings 45204687Simp */ 46177924SimpLEAF(rtld_start) 47204687Simp .frame sp, 4*PTR_SIZE, ra 48204687Simp .mask 0x10090000,-PTR_SIZE 49177924Simp .set noreorder 50204687Simp SETUP_GP 51204687Simp PTR_SUBU sp, 4*PTR_SIZE /* adjust stack pointer */ 52204687Simp SETUP_GP64(s4, rtld_start) 53204687Simp SAVE_GP(0) 54204687Simp /* -> 1*PTR_SIZE(sp) for atexit */ 55204687Simp /* -> 2*PTR_SIZE(sp) for obj_main */ 56204687Simp move s0, a0 /* save stack pointer from a0 */ 57204687Simp move s3, a3 /* save ps_strings pointer */ 58177924Simp 59204687Simp PTR_LA a1, 1f 60177924Simp bal 1f 61204687Simp PTR_LA t0, _C_LABEL(_rtld_relocate_nonplt_self) 62204687Simp1: PTR_SUBU a1, ra, a1 /* relocbase */ 63204687Simp PTR_LA a0, _DYNAMIC 64204687Simp PTR_ADDU t9, a1, t0 65204687Simp jalr t9 /* _rtld_relocate_nonplt_self(dynp, relocabase) */ 66204687Simp PTR_ADDU a0, a1, a0 /* &_DYNAMIC */ 67177924Simp 68204687Simp move a0, s0 /* sp */ 69204687Simp PTR_ADDU a1, sp, 2*PTR_SIZE /* &our atexit function */ 70204687Simp PTR_ADDU a2, sp, 3*PTR_SIZE /* obj_main entry */ 71209239Sjchandra PTR_LA t9, _C_LABEL(_rtld) 72209239Sjchandra jalr t9 /* v0 = _rtld(sp, cleanup, objp) */ 73204687Simp nop 74177924Simp 75204687Simp PTR_L a1, 2*PTR_SIZE(sp) /* our atexit function */ 76204687Simp PTR_L a2, 3*PTR_SIZE(sp) /* obj_main entry */ 77204687Simp PTR_ADDU sp, 4*PTR_SIZE /* readjust stack */ 78204687Simp move a0, s0 /* stack pointer */ 79204687Simp move t9, v0 80204687Simp jr t9 /* _start(sp, cleanup, obj); */ 81204687Simp move a3, s3 /* restore ps_strings */ 82177924Simp 83177924SimpEND(rtld_start) 84177924Simp 85204687Simp#define XCALLFRAME_SIZ (12*SZREG) 86204687Simp#define XCALLFRAME_RA (10*SZREG) 87204687Simp#define XCALLFRAME_GP (9*SZREG) 88204687Simp#define XCALLFRAME_S0 (8*SZREG) 89204687Simp#define XCALLFRAME_A3 (7*SZREG) 90204687Simp#define XCALLFRAME_A2 (6*SZREG) 91204687Simp#define XCALLFRAME_A1 (5*SZREG) 92204687Simp#define XCALLFRAME_A0 (4*SZREG) 93204687Simp#if defined(__mips_n32) || defined(__mips_n64) 94204687Simp#define XCALLFRAME_A7 (3*SZREG) 95204687Simp#define XCALLFRAME_A6 (2*SZREG) 96204687Simp#define XCALLFRAME_A5 (1*SZREG) 97204687Simp#define XCALLFRAME_A4 (0*SZREG) 98204687Simp#endif 99204687Simp 100177924Simp .globl _rtld_bind_start 101177924Simp .ent _rtld_bind_start 102177924Simp_rtld_bind_start: 103204687Simp .frame sp, XCALLFRAME_SIZ, $15 104204687Simp move v1, gp /* save old GP */ 105204687Simp#if defined(__mips_o32) || defined(__mips_o64) 106204687Simp PTR_ADDU t9, 8 /* modify T9 to point at .cpload */ 107204687Simp#endif 108204687Simp SETUP_GP 109204687Simp PTR_SUBU sp, XCALLFRAME_SIZ /* save arguments and sp value in stack */ 110204687Simp SETUP_GP64(XCALLFRAME_GP, _rtld_bind_start) 111204687Simp SAVE_GP(XCALLFRAME_GP) 112204687Simp#if defined(__mips_n32) || defined(__mips_n64) 113204687Simp REG_S a4, XCALLFRAME_A4(sp) 114204687Simp REG_S a5, XCALLFRAME_A5(sp) 115204687Simp REG_S a6, XCALLFRAME_A6(sp) 116204687Simp REG_S a7, XCALLFRAME_A7(sp) 117204687Simp#endif 118204687Simp REG_S a0, XCALLFRAME_A0(sp) 119204687Simp REG_S a1, XCALLFRAME_A1(sp) 120204687Simp REG_S a2, XCALLFRAME_A2(sp) 121204687Simp REG_S a3, XCALLFRAME_A3(sp) 122204687Simp REG_S $15, XCALLFRAME_RA(sp) /* ra is in t7/t3 */ 123204687Simp REG_S s0, XCALLFRAME_S0(sp) 124204687Simp move s0, sp 125177924Simp 126204687Simp move a0, v1 /* old GP */ 127209239Sjchandra PTR_SUBU a0, a0, 0x7ff0 /* The offset of $gp from the */ 128204687Simp /* beginning of the .got section: */ 129177924Simp /* $gp = .got + 0x7ff0, so */ 130177924Simp /* .got = $gp - 0x7ff0 */ 131177924Simp /* Simple math as you can see. */ 132204687Simp#if defined(__mips_n64) 133210629Sjchandra ld a0, 8(a0) /* object = pltgot[1] */ 134210629Sjchandra and a0, a0, 0x7fffffffffffffff 135204687Simp#else 136204687Simp lw a0, 4(a0) /* object = pltgot[1] & 0x7fffffff */ 137210629Sjchandra and a0, a0, 0x7fffffff 138204687Simp#endif 139204687Simp move a1, t8 /* symbol index */ 140177924Simp 141209239Sjchandra PTR_LA t9, _C_LABEL(_mips_rtld_bind) 142209239Sjchandra jalr t9 143204687Simp nop 144204687Simp 145204687Simp move sp, s0 146204687Simp REG_L ra, XCALLFRAME_RA(sp) 147204687Simp REG_L s0, XCALLFRAME_S0(sp) 148204687Simp REG_L a0, XCALLFRAME_A0(sp) 149204687Simp REG_L a1, XCALLFRAME_A1(sp) 150204687Simp REG_L a2, XCALLFRAME_A2(sp) 151204687Simp REG_L a3, XCALLFRAME_A3(sp) 152204687Simp#if defined(__mips_n32) || defined(__mips_n64) 153204687Simp REG_L a4, XCALLFRAME_A4(sp) 154204687Simp REG_L a5, XCALLFRAME_A5(sp) 155204687Simp REG_L a6, XCALLFRAME_A6(sp) 156204687Simp REG_L a7, XCALLFRAME_A7(sp) 157204687Simp#endif 158204687Simp RESTORE_GP64 159204687Simp PTR_ADDU sp, XCALLFRAME_SIZ 160204687Simp move t9, v0 161177924Simp jr t9 162204687Simp nop 163204687SimpEND(_rtld_bind_start) 164