1107572Sgrehan/* $NetBSD: rtld_start.S,v 1.4 2001/09/26 04:06:43 mycroft Exp $ */ 2107572Sgrehan 3107572Sgrehan/*- 4107572Sgrehan * Copyright (C) 1998 Tsubai Masanari 5107572Sgrehan * All rights reserved. 6107572Sgrehan * 7107572Sgrehan * Redistribution and use in source and binary forms, with or without 8107572Sgrehan * modification, are permitted provided that the following conditions 9107572Sgrehan * are met: 10107572Sgrehan * 1. Redistributions of source code must retain the above copyright 11107572Sgrehan * notice, this list of conditions and the following disclaimer. 12107572Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 13107572Sgrehan * notice, this list of conditions and the following disclaimer in the 14107572Sgrehan * documentation and/or other materials provided with the distribution. 15107572Sgrehan * 3. The name of the author may not be used to endorse or promote products 16107572Sgrehan * derived from this software without specific prior written permission. 17107572Sgrehan * 18107572Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19107572Sgrehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20107572Sgrehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21107572Sgrehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22107572Sgrehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23107572Sgrehan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24107572Sgrehan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25107572Sgrehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26107572Sgrehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27107572Sgrehan * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28107572Sgrehan * 29107572Sgrehan * $FreeBSD$ 30107572Sgrehan */ 31107572Sgrehan 32107572Sgrehan#include <machine/asm.h> 33107572Sgrehan 34107572Sgrehan.extern _GLOBAL_OFFSET_TABLE_ 35107572Sgrehan.extern _DYNAMIC 36115396Skan 37107572Sgrehan_ENTRY(.rtld_start) 38115396Skan stwu %r1,-48(%r1) /* 16-byte aligned stack for reg saves + 39115396Skan exit_proc & obj _rtld args + 40107572Sgrehan backchain & lrsave stack frame */ 41107572Sgrehan stw %r3,16(%r1) /* argc */ 42107572Sgrehan stw %r4,20(%r1) /* argv */ 43107572Sgrehan stw %r5,24(%r1) /* envp */ 44107572Sgrehan/* stw %r6,28(%r1) *//* obj (always 0) */ 45107572Sgrehan/* stw %r7,32(%r1) *//* cleanup (always 0) */ 46107572Sgrehan stw %r8,36(%r1) /* ps_strings */ 47107572Sgrehan 48107572Sgrehan /* 49107572Sgrehan * Perform initial relocation of ld-elf.so. Not as easy as it 50107572Sgrehan * sounds. 51107572Sgrehan * - perform small forward branch to put PC into link reg 52107572Sgrehan * - use link-time constants to determine offset to the 53107572Sgrehan * _DYNAMIC section and the GOT. Add these to the PC to 54107572Sgrehan * convert to absolute addresses. 55107572Sgrehan * - sync icache to allow execution of the SVR4 ABI-specified 56107572Sgrehan * blrl instruction preceding the GOT 57107572Sgrehan * - Use this instruction to determine the GOT absolute address 58107572Sgrehan * - read GOT[0], which is the SVR4 ABI-specified link-time 59107572Sgrehan * value of _DYNAMIC. Subtract this value from the absolute 60107572Sgrehan * value to determine the load address 61107572Sgrehan * - call reloc_non_plt_self() to fix up ld-elf.so's relocations 62107572Sgrehan */ 63107572Sgrehan bl 1f 64107572Sgrehan .long _DYNAMIC-. 65107572Sgrehan .long _GLOBAL_OFFSET_TABLE_-. /* branch lr + 4 */ 66107572Sgrehan1: 67107572Sgrehan mflr %r3 /* PC value at .long */ 68107572Sgrehan lwz %r4,4(%r3) 69107572Sgrehan add %r4,%r4,%r3 /* &_GLOBAL_OFFSET_TABLE-4, blrl insn. */ 70107572Sgrehan dcbst %r0,%r4 /* sync i-cache with d-cache */ 71107572Sgrehan sync 72107572Sgrehan icbi %r0,%r4 73107572Sgrehan isync 74115396Skan 75107572Sgrehan lwz %r4,0(%r3) /* offset to _DYNAMIC */ 76107572Sgrehan add %r3,%r4,%r3 /* r3 = &_DYNAMIC, absolute value */ 77107572Sgrehan 78107572Sgrehan bl _GLOBAL_OFFSET_TABLE_@local-4 79107572Sgrehan mflr %r4 /* &_GLOBAL_OFFSET_TABLE_, absolute value */ 80107572Sgrehan lwz %r4,0(%r4) /* linker &_DYNAMIC, from got[0] */ 81107572Sgrehan subf %r4,%r4,%r3 /* subtract to calculate relocbase */ 82115396Skan 83107572Sgrehan bl reloc_non_plt_self@plt /* reloc_non_plt_self(&_DYNAMIC,base) */ 84107572Sgrehan 85107572Sgrehan /* 86107572Sgrehan * The _rtld() function likes to see a stack layout containing 87107572Sgrehan * { argc, argv[0], argv[1] ... argv[N], 0, env[0], ... , env[N] } 88107572Sgrehan * Since the PowerPC stack was 16-byte aligned at exec time, the 89107572Sgrehan * original stack layout has to be found by moving back a word 90107572Sgrehan * from the argv pointer. 91107572Sgrehan */ 92107572Sgrehan lwz %r4,20(%r1) /* restore argv */ 93107572Sgrehan addi %r3,%r4,-4 /* locate argc ptr, &argv[-1] */ 94107572Sgrehan 95107572Sgrehan addi %r4,%r1,8 /* &exit_proc on stack */ 96107572Sgrehan addi %r5,%r1,12 /* &obj_main on stack */ 97107572Sgrehan 98107572Sgrehan bl _rtld@plt /* &_start = _rtld(sp, &exit_proc, &obj_main)*/ 99107572Sgrehan mtlr %r3 100107572Sgrehan 101107572Sgrehan /* 102107572Sgrehan * Restore args, with new obj/exit proc 103107572Sgrehan */ 104107572Sgrehan lwz %r3,16(%r1) /* argc */ 105107572Sgrehan lwz %r4,20(%r1) /* argv */ 106107572Sgrehan lwz %r5,24(%r1) /* envp */ 107107572Sgrehan lwz %r6,12(%r1) /* obj */ 108107572Sgrehan lwz %r7,8(%r1) /* exit proc */ 109107572Sgrehan lwz %r8,36(%r1) /* ps_strings */ 110107572Sgrehan addi %r1,%r1,48 /* restore original stackptr */ 111107572Sgrehan 112107572Sgrehan blrl /* _start(argc, argv, envp, obj, cleanup, ps_strings) */ 113107572Sgrehan 114107572Sgrehan li %r0,1 /* _exit() */ 115107572Sgrehan sc 116107572Sgrehan 117107572Sgrehan/* 118107572Sgrehan * _rtld_bind_start() 119107572Sgrehan * 120107572Sgrehan * Call into the MI binder. This routine is reached via the PLT call cell, 121107572Sgrehan * and then _rtld_powerpc_pltresolve(). 122107572Sgrehan * On entry, %r11 contains the index of the PLT cell, and %r12 contains 123107572Sgrehan * a pointer to the ELF object for the file. 124107572Sgrehan * Save all registers, call into the binder to resolve and fixup the external 125107572Sgrehan * routine, and then transfer to the external routine on return. 126107572Sgrehan */ 127107572Sgrehan .globl _rtld_bind 128107572Sgrehan 129107572Sgrehan_ENTRY(_rtld_bind_start) 130107572Sgrehan stwu %r1,-160(%r1) # stack space for 29 regs + r0/lr/cr 131107572Sgrehan stw %r0,20(%r1) # save r0 132107572Sgrehan mflr %r0 133107572Sgrehan stw %r0,16(%r1) # save lr 134107572Sgrehan mfcr %r0 135107572Sgrehan stw %r0,12(%r1) # save cr 136107572Sgrehan stmw %r3,24(%r1) # save r3-r31 137107572Sgrehan 138107572Sgrehan mr %r3,%r12 # obj 139107572Sgrehan mulli %r4,%r11,12 # rela index * sizeof(Elf_Rela) 140107572Sgrehan bl _rtld_bind@PLT # target addr = _rtld_bind(obj, reloff) 141107572Sgrehan mtctr %r3 # move absolute target addr into ctr 142107572Sgrehan 143107572Sgrehan lmw %r3,24(%r1) # restore r3-r31 144107572Sgrehan lwz %r0,12(%r1) # restore cr 145107572Sgrehan mtcr %r0 146107572Sgrehan lwz %r0,16(%r1) # restore lr 147107572Sgrehan mtlr %r0 148107572Sgrehan lwz %r0,20(%r1) # restore r0 149107572Sgrehan 150107572Sgrehan addi %r1,%r1,160 # restore stack 151107572Sgrehan bctr # jump to target 152107572Sgrehan 153107572Sgrehan 154107572Sgrehan/* 155107572Sgrehan * _rtld_powerpc_pltresolve() 156107572Sgrehan * 157107572Sgrehan * This routine is copied into the latter part of the 72-byte reserved 158107572Sgrehan * area at the start of the PLT. The absolute address of the _rtld_bind_start 159107572Sgrehan * routine, and the ELF object for the loaded file, are inserted into 160107572Sgrehan * the code by the reloc.c:init_pltgot() routine. 161107572Sgrehan * The first time an external routine is called, the PLT slot will 162107572Sgrehan * set up %r11 to the offset of the slot, and will jump to this routine. 163107572Sgrehan * The ELF object is shifted into %r11, and _rtld_bind_start is called 164107572Sgrehan * to complete the binding. 165107572Sgrehan */ 166204211Snwhitehorn_ENTRY(_rtld_powerpc_pltlongresolve) 167204211Snwhitehorn lis %r12,0 # lis 12,jmptab@ha 168204211Snwhitehorn addi %r12,%r12,0 # addi 12,12,jmptab@l 169204211Snwhitehorn subf %r11,%r12,%r11 # reloff 170204211Snwhitehorn li %r12,2 171204211Snwhitehorn srw %r11,%r11,%r12 # index = reloff/sizeof(Elf_Addr) 172107572Sgrehan_ENTRY(_rtld_powerpc_pltresolve) 173107572Sgrehan lis %r12,0 # lis 12,_rtld_bind_start@ha 174107572Sgrehan addi %r12,%r12,0 # addi 12,12,_rtld_bind_start@l 175107572Sgrehan mtctr %r12 176107572Sgrehan lis %r12,0 # lis 12,obj@ha 177107572Sgrehan addi %r12,%r12,0 # addi 12,12,obj@l 178107572Sgrehan bctr 179107572Sgrehan 180107572Sgrehan/* 181107572Sgrehan * _rtld_powerpc_pltcall() 182107572Sgrehan * 183107572Sgrehan * This routine is copied into the 72-byte reserved area at the 184107572Sgrehan * start of the PLT. The reloc.c:init_pltgot() routine inserts 185107572Sgrehan * the absolute address of the jumptable. 186107572Sgrehan * Control is transferred to this routine when the binder has 187107572Sgrehan * located the external routine, but determined that it is > 32Mb 188107572Sgrehan * from the PLT slot. Code is inserted into the PLT slot to set up 189107572Sgrehan * %r11 with the jumptable index, and jump to here, where the 190107572Sgrehan * absolute address of the external routine is loaded from the 191107572Sgrehan * jumptable and transferred to 192107572Sgrehan */ 193107572Sgrehan_ENTRY(_rtld_powerpc_pltcall) 194107572Sgrehan slwi %r11,%r11,2 # jmptab offset = index * 4 195107572Sgrehan addis %r11,%r11,0 # addis 11,11,jmptab@ha 196107572Sgrehan lwz %r11,0(%r11) # lwz 11,jmptab@l(11) 197107572Sgrehan mtctr %r11 198107572Sgrehan bctr # (*jmptab[index])() 199107572Sgrehan 200217397Skib .section .note.GNU-stack,"",%progbits 201