1/* $NetBSD: rtld_start.S,v 1.4 2001/09/26 04:06:43 mycroft Exp $ */ 2 3/*- 4 * Copyright (C) 1998 Tsubai Masanari 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32#include <machine/asm.h> 33 34.extern _GLOBAL_OFFSET_TABLE_ 35.extern _DYNAMIC 36 37_ENTRY(.rtld_start) 38 stwu %r1,-48(%r1) /* 16-byte aligned stack for reg saves + 39 exit_proc & obj _rtld args + 40 backchain & lrsave stack frame */ 41 stw %r3,16(%r1) /* argc */ 42 stw %r4,20(%r1) /* argv */ 43 stw %r5,24(%r1) /* envp */ 44/* stw %r6,28(%r1) *//* obj (always 0) */ 45/* stw %r7,32(%r1) *//* cleanup (always 0) */ 46 stw %r8,36(%r1) /* ps_strings */ 47 48 /* 49 * Perform initial relocation of ld-elf.so. Not as easy as it 50 * sounds. 51 * - perform small forward branch to put PC into link reg 52 * - use link-time constants to determine offset to the 53 * _DYNAMIC section and the GOT. Add these to the PC to 54 * convert to absolute addresses. 55 * - sync icache to allow execution of the SVR4 ABI-specified 56 * blrl instruction preceding the GOT 57 * - Use this instruction to determine the GOT absolute address 58 * - read GOT[0], which is the SVR4 ABI-specified link-time 59 * value of _DYNAMIC. Subtract this value from the absolute 60 * value to determine the load address 61 * - call reloc_non_plt_self() to fix up ld-elf.so's relocations 62 */ 63 bl 1f 64 .long _DYNAMIC-. 65 .long _GLOBAL_OFFSET_TABLE_-. /* branch lr + 4 */ 661: 67 mflr %r3 /* PC value at .long */ 68 lwz %r4,4(%r3) 69 add %r4,%r4,%r3 /* &_GLOBAL_OFFSET_TABLE-4, blrl insn. */ 70 dcbst %r0,%r4 /* sync i-cache with d-cache */ 71 sync 72 icbi %r0,%r4 73 isync 74 75 lwz %r4,0(%r3) /* offset to _DYNAMIC */ 76 add %r3,%r4,%r3 /* r3 = &_DYNAMIC, absolute value */ 77 78 bl _GLOBAL_OFFSET_TABLE_@local-4 79 mflr %r4 /* &_GLOBAL_OFFSET_TABLE_, absolute value */ 80 lwz %r4,0(%r4) /* linker &_DYNAMIC, from got[0] */ 81 subf %r4,%r4,%r3 /* subtract to calculate relocbase */ 82 83 bl reloc_non_plt_self@plt /* reloc_non_plt_self(&_DYNAMIC,base) */ 84 85 /* 86 * The _rtld() function likes to see a stack layout containing 87 * { argc, argv[0], argv[1] ... argv[N], 0, env[0], ... , env[N] } 88 * Since the PowerPC stack was 16-byte aligned at exec time, the 89 * original stack layout has to be found by moving back a word 90 * from the argv pointer. 91 */ 92 lwz %r4,20(%r1) /* restore argv */ 93 addi %r3,%r4,-4 /* locate argc ptr, &argv[-1] */ 94 95 addi %r4,%r1,8 /* &exit_proc on stack */ 96 addi %r5,%r1,12 /* &obj_main on stack */ 97 98 bl _rtld@plt /* &_start = _rtld(sp, &exit_proc, &obj_main)*/ 99 mtlr %r3 100 101 /* 102 * Restore args, with new obj/exit proc 103 */ 104 lwz %r3,16(%r1) /* argc */ 105 lwz %r4,20(%r1) /* argv */ 106 lwz %r5,24(%r1) /* envp */ 107 lwz %r6,12(%r1) /* obj */ 108 lwz %r7,8(%r1) /* exit proc */ 109 lwz %r8,36(%r1) /* ps_strings */ 110 addi %r1,%r1,48 /* restore original stackptr */ 111 112 blrl /* _start(argc, argv, envp, obj, cleanup, ps_strings) */ 113 114 li %r0,1 /* _exit() */ 115 sc 116 117/* 118 * _rtld_bind_start() 119 * 120 * Call into the MI binder. This routine is reached via the PLT call cell, 121 * and then _rtld_powerpc_pltresolve(). 122 * On entry, %r11 contains the index of the PLT cell, and %r12 contains 123 * a pointer to the ELF object for the file. 124 * Save all registers, call into the binder to resolve and fixup the external 125 * routine, and then transfer to the external routine on return. 126 */ 127 .globl _rtld_bind 128 129_ENTRY(_rtld_bind_start) 130 stwu %r1,-160(%r1) # stack space for 29 regs + r0/lr/cr 131 stw %r0,20(%r1) # save r0 132 mflr %r0 133 stw %r0,16(%r1) # save lr 134 mfcr %r0 135 stw %r0,12(%r1) # save cr 136 stmw %r3,24(%r1) # save r3-r31 137 138 mr %r3,%r12 # obj 139 mulli %r4,%r11,12 # rela index * sizeof(Elf_Rela) 140 bl _rtld_bind@PLT # target addr = _rtld_bind(obj, reloff) 141 mtctr %r3 # move absolute target addr into ctr 142 143 lmw %r3,24(%r1) # restore r3-r31 144 lwz %r0,12(%r1) # restore cr 145 mtcr %r0 146 lwz %r0,16(%r1) # restore lr 147 mtlr %r0 148 lwz %r0,20(%r1) # restore r0 149 150 addi %r1,%r1,160 # restore stack 151 bctr # jump to target 152 153 154/* 155 * _rtld_powerpc_pltresolve() 156 * 157 * This routine is copied into the latter part of the 72-byte reserved 158 * area at the start of the PLT. The absolute address of the _rtld_bind_start 159 * routine, and the ELF object for the loaded file, are inserted into 160 * the code by the reloc.c:init_pltgot() routine. 161 * The first time an external routine is called, the PLT slot will 162 * set up %r11 to the offset of the slot, and will jump to this routine. 163 * The ELF object is shifted into %r11, and _rtld_bind_start is called 164 * to complete the binding. 165 */ 166_ENTRY(_rtld_powerpc_pltresolve) 167 lis %r12,0 # lis 12,_rtld_bind_start@ha 168 addi %r12,%r12,0 # addi 12,12,_rtld_bind_start@l 169 mtctr %r12 170 lis %r12,0 # lis 12,obj@ha 171 addi %r12,%r12,0 # addi 12,12,obj@l 172 bctr 173 174/* 175 * _rtld_powerpc_pltcall() 176 * 177 * This routine is copied into the 72-byte reserved area at the 178 * start of the PLT. The reloc.c:init_pltgot() routine inserts 179 * the absolute address of the jumptable. 180 * Control is transferred to this routine when the binder has 181 * located the external routine, but determined that it is > 32Mb 182 * from the PLT slot. Code is inserted into the PLT slot to set up 183 * %r11 with the jumptable index, and jump to here, where the 184 * absolute address of the external routine is loaded from the 185 * jumptable and transferred to 186 */ 187_ENTRY(_rtld_powerpc_pltcall) 188 slwi %r11,%r11,2 # jmptab offset = index * 4 189 addis %r11,%r11,0 # addis 11,11,jmptab@ha 190 lwz %r11,0(%r11) # lwz 11,jmptab@l(11) 191 mtctr %r11 192 bctr # (*jmptab[index])() 193 194 195