1*0a6a1f1dSLionel Sambuc/* $NetBSD: rtld_start.S,v 1.1 2014/09/03 19:34:26 matt Exp $ */ 2*0a6a1f1dSLionel Sambuc 3*0a6a1f1dSLionel Sambuc/*- 4*0a6a1f1dSLionel Sambuc * Copyright (c) 2014 The NetBSD Foundation, Inc. 5*0a6a1f1dSLionel Sambuc * All rights reserved. 6*0a6a1f1dSLionel Sambuc * 7*0a6a1f1dSLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation 8*0a6a1f1dSLionel Sambuc * by Matt Thomas of 3am Software Foundry. 9*0a6a1f1dSLionel Sambuc * 10*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without 11*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions 12*0a6a1f1dSLionel Sambuc * are met: 13*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright 14*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer. 15*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 16*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the 17*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution. 18*0a6a1f1dSLionel Sambuc * 19*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*0a6a1f1dSLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE. 30*0a6a1f1dSLionel Sambuc */ 31*0a6a1f1dSLionel Sambuc 32*0a6a1f1dSLionel Sambuc#include <machine/asm.h> 33*0a6a1f1dSLionel Sambuc 34*0a6a1f1dSLionel Sambuc .globl _rtld_start 35*0a6a1f1dSLionel Sambuc .globl _rtld 36*0a6a1f1dSLionel Sambuc 37*0a6a1f1dSLionel Sambuc#define CF_LEN (3*__SIZEOF_POINTER__) 38*0a6a1f1dSLionel Sambuc#define CF_OBJ (2*__SIZEOF_POINTER__) 39*0a6a1f1dSLionel Sambuc#define CF_CLEANUP (1*__SIZEOF_POINTER__) 40*0a6a1f1dSLionel Sambuc 41*0a6a1f1dSLionel Sambuc /* 42*0a6a1f1dSLionel Sambuc * void ___start(void (*cleanup)(void), const Obj_Entry *obj, 43*0a6a1f1dSLionel Sambuc * struct ps_strings *ps_strings); 44*0a6a1f1dSLionel Sambuc */ 45*0a6a1f1dSLionel SambucENTRY_NP(_rtld_start) 46*0a6a1f1dSLionel Sambuc l.sw CF_LEN(r1), r1 # reserve some stack space 47*0a6a1f1dSLionel Sambuc#if 0 48*0a6a1f1dSLionel Sambuc l.ori r26, r3, 0 # obj (should be 0) 49*0a6a1f1dSLionel Sambuc l.ori r28, r4, 0 # cleanup (should be 0) 50*0a6a1f1dSLionel Sambuc#endif 51*0a6a1f1dSLionel Sambuc l.ori r30, r5, 0 # ps_strings 52*0a6a1f1dSLionel Sambuc l.addi r1, r1, -CF_LEN 53*0a6a1f1dSLionel Sambuc 54*0a6a1f1dSLionel Sambuc PIC_GOTSETUP(r16) 55*0a6a1f1dSLionel Sambuc 56*0a6a1f1dSLionel Sambuc l.movhi r3, gotoffhi(_DYNAMIC) # get _DYNAMIC gotoff address 57*0a6a1f1dSLionel Sambuc l.ori r3, r3, gotofflo(_DYNAMIC) 58*0a6a1f1dSLionel Sambuc 59*0a6a1f1dSLionel Sambuc l.lwz r7, 0(r16) # get base-relative &_DYNAMIC 60*0a6a1f1dSLionel Sambuc l.add r3, r3, r16 # r3 = _DYNAMIC actual address 61*0a6a1f1dSLionel Sambuc l.sub r24, r3, r7 # r24 = relocbase 62*0a6a1f1dSLionel Sambuc l.ori r4, r24, 0 # r4 = relocbase 63*0a6a1f1dSLionel Sambuc l.jal _C_LABEL(_rtld_relocate_nonplt_self) 64*0a6a1f1dSLionel Sambuc l.nop 65*0a6a1f1dSLionel Sambuc 66*0a6a1f1dSLionel Sambuc l.addi r3, r1, CF_CLEANUP 67*0a6a1f1dSLionel Sambuc l.ori r4, r24, 0 # r4 = relocbase 68*0a6a1f1dSLionel Sambuc l.jal _C_LABEL(_rtld) # _start = _rtld(sp, relocbase) 69*0a6a1f1dSLionel Sambuc l.nop 70*0a6a1f1dSLionel Sambuc 71*0a6a1f1dSLionel Sambuc l.lwz r3, CF_OBJ(r1) # obj 72*0a6a1f1dSLionel Sambuc l.lwz r4, CF_CLEANUP(r1) # cleanup 73*0a6a1f1dSLionel Sambuc l.ori r5, r30, 0 # ps_strings 74*0a6a1f1dSLionel Sambuc 75*0a6a1f1dSLionel Sambuc l.addi r1, r1, CF_LEN # pop stack 76*0a6a1f1dSLionel Sambuc l.jalr r11 # _start(obj, cleanup, ps_strings) 77*0a6a1f1dSLionel Sambuc 78*0a6a1f1dSLionel Sambuc l.ori r13, r0, 1 # _exit() 79*0a6a1f1dSLionel Sambuc l.sys 0 80*0a6a1f1dSLionel Sambuc 81*0a6a1f1dSLionel SambucEND(_rtld_start) 82*0a6a1f1dSLionel Sambuc 83*0a6a1f1dSLionel Sambuc .globl _rtld_bind 84*0a6a1f1dSLionel Sambuc 85*0a6a1f1dSLionel Sambuc/* 86*0a6a1f1dSLionel Sambuc * r11 = rela offset 87*0a6a1f1dSLionel Sambuc * r12 = GOT[1] (&obj) 88*0a6a1f1dSLionel Sambuc * r15 = GOT[2] (&_rtld_bind_start) 89*0a6a1f1dSLionel Sambuc */ 90*0a6a1f1dSLionel SambucENTRY_NP(_rtld_bind_start) 91*0a6a1f1dSLionel Sambuc 92*0a6a1f1dSLionel Sambuc l.sw -4(r1), r9 # save lr 93*0a6a1f1dSLionel Sambuc l.sw -8(r1), r8 # save arg5 94*0a6a1f1dSLionel Sambuc l.sw -12(r1), r7 # save arg4 95*0a6a1f1dSLionel Sambuc l.sw -16(r1), r6 # save arg3 96*0a6a1f1dSLionel Sambuc l.sw -20(r1), r5 # save arg2 97*0a6a1f1dSLionel Sambuc l.sw -24(r1), r4 # save arg1 98*0a6a1f1dSLionel Sambuc l.sw -28(r1), r3 # save arg0 99*0a6a1f1dSLionel Sambuc l.sw -32(r1), r1 # establish stack frame 100*0a6a1f1dSLionel Sambuc l.addi r1, r1, -32 101*0a6a1f1dSLionel Sambuc 102*0a6a1f1dSLionel Sambuc l.ori r3, r12, 0 # obj 103*0a6a1f1dSLionel Sambuc l.ori r4, r11, 0 # reloff 104*0a6a1f1dSLionel Sambuc l.jal _C_LABEL(_rtld_bind) # _rtld_bind(obj, reloff) 105*0a6a1f1dSLionel Sambuc l.nop 106*0a6a1f1dSLionel Sambuc 107*0a6a1f1dSLionel Sambuc l.addi r1, r1, 32 # pop stack frame 108*0a6a1f1dSLionel Sambuc l.lwz r3, -28(r1) # restore arg0 109*0a6a1f1dSLionel Sambuc l.lwz r4, -24(r1) # restore arg1 110*0a6a1f1dSLionel Sambuc l.lwz r5, -20(r1) # restore arg2 111*0a6a1f1dSLionel Sambuc l.lwz r6, -16(r1) # restore arg3 112*0a6a1f1dSLionel Sambuc l.lwz r7, -12(r1) # restore arg4 113*0a6a1f1dSLionel Sambuc l.lwz r8, -8(r1) # restore arg5 114*0a6a1f1dSLionel Sambuc l.lwz r9, -4(r1) # restore lr 115*0a6a1f1dSLionel Sambuc 116*0a6a1f1dSLionel Sambuc l.jr r11 # jump to routine 117*0a6a1f1dSLionel Sambuc l.nop 118*0a6a1f1dSLionel SambucEND(_rtld_bind_start) 119