1b9dea67fSPeter Grehan/* $NetBSD: rtld_start.S,v 1.4 2001/09/26 04:06:43 mycroft Exp $ */ 2b9dea67fSPeter Grehan 3b9dea67fSPeter Grehan/*- 4b9dea67fSPeter Grehan * Copyright (C) 1998 Tsubai Masanari 5b9dea67fSPeter Grehan * All rights reserved. 6b9dea67fSPeter Grehan * 7b9dea67fSPeter Grehan * Redistribution and use in source and binary forms, with or without 8b9dea67fSPeter Grehan * modification, are permitted provided that the following conditions 9b9dea67fSPeter Grehan * are met: 10b9dea67fSPeter Grehan * 1. Redistributions of source code must retain the above copyright 11b9dea67fSPeter Grehan * notice, this list of conditions and the following disclaimer. 12b9dea67fSPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright 13b9dea67fSPeter Grehan * notice, this list of conditions and the following disclaimer in the 14b9dea67fSPeter Grehan * documentation and/or other materials provided with the distribution. 15b9dea67fSPeter Grehan * 3. The name of the author may not be used to endorse or promote products 16b9dea67fSPeter Grehan * derived from this software without specific prior written permission. 17b9dea67fSPeter Grehan * 18b9dea67fSPeter Grehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19b9dea67fSPeter Grehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20b9dea67fSPeter Grehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21b9dea67fSPeter Grehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22b9dea67fSPeter Grehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23b9dea67fSPeter Grehan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24b9dea67fSPeter Grehan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25b9dea67fSPeter Grehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26b9dea67fSPeter Grehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27b9dea67fSPeter Grehan * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28b9dea67fSPeter Grehan */ 29b9dea67fSPeter Grehan 30b9dea67fSPeter Grehan#include <machine/asm.h> 317511645eSJustin Hibbits#include <machine/spr.h> /* For SPR_SPEFSCR if needed. */ 32b9dea67fSPeter Grehan 33b9dea67fSPeter Grehan.extern _GLOBAL_OFFSET_TABLE_ 34b9dea67fSPeter Grehan.extern _DYNAMIC 35b9dea67fSPeter Grehan 36b9dea67fSPeter Grehan_ENTRY(.rtld_start) 37b9dea67fSPeter Grehan stwu %r1,-48(%r1) /* 16-byte aligned stack for reg saves + 38b9dea67fSPeter Grehan exit_proc & obj _rtld args + 39b9dea67fSPeter Grehan backchain & lrsave stack frame */ 40b9dea67fSPeter Grehan stw %r3,16(%r1) /* argc */ 41b9dea67fSPeter Grehan stw %r4,20(%r1) /* argv */ 42b9dea67fSPeter Grehan stw %r5,24(%r1) /* envp */ 43b9dea67fSPeter Grehan/* stw %r6,28(%r1) *//* obj (always 0) */ 44b9dea67fSPeter Grehan/* stw %r7,32(%r1) *//* cleanup (always 0) */ 45b9dea67fSPeter Grehan stw %r8,36(%r1) /* ps_strings */ 46b9dea67fSPeter Grehan 47b9dea67fSPeter Grehan /* 48b9dea67fSPeter Grehan * Perform initial relocation of ld-elf.so. Not as easy as it 49b9dea67fSPeter Grehan * sounds. 50b9dea67fSPeter Grehan * - perform small forward branch to put PC into link reg 51b9dea67fSPeter Grehan * - use link-time constants to determine offset to the 52b9dea67fSPeter Grehan * _DYNAMIC section and the GOT. Add these to the PC to 53b9dea67fSPeter Grehan * convert to absolute addresses. 54b9dea67fSPeter Grehan * - read GOT[0], which is the SVR4 ABI-specified link-time 55b9dea67fSPeter Grehan * value of _DYNAMIC. Subtract this value from the absolute 56b9dea67fSPeter Grehan * value to determine the load address 57b9dea67fSPeter Grehan * - call reloc_non_plt_self() to fix up ld-elf.so's relocations 58b9dea67fSPeter Grehan */ 59f62da49bSJustin Hibbits bcl 20,31,1f 60f62da49bSJustin Hibbits1: mflr %r30 61f62da49bSJustin Hibbits mr %r3,%r30 # save for _DYNAMIC 62f62da49bSJustin Hibbits addis %r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@ha 63f62da49bSJustin Hibbits addi %r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@l 64f62da49bSJustin Hibbits addis %r3,%r3,_DYNAMIC-1b@ha # get _DYNAMIC actual address 65f62da49bSJustin Hibbits addi %r3,%r3,_DYNAMIC-1b@l 66f62da49bSJustin Hibbits lwz %r28,0(%r30) # get base-relative &_DYNAMIC 67f62da49bSJustin Hibbits sub %r28,%r3,%r28 # r28 = relocbase 68f62da49bSJustin Hibbits mr %r4,%r28 # r4 = relocbase 69f62da49bSJustin Hibbits bl reloc_non_plt_self /* reloc_non_plt_self(&_DYNAMIC,base) */ 70b9dea67fSPeter Grehan 71b9dea67fSPeter Grehan /* 72b9dea67fSPeter Grehan * The _rtld() function likes to see a stack layout containing 73b9dea67fSPeter Grehan * { argc, argv[0], argv[1] ... argv[N], 0, env[0], ... , env[N] } 74b9dea67fSPeter Grehan * Since the PowerPC stack was 16-byte aligned at exec time, the 75b9dea67fSPeter Grehan * original stack layout has to be found by moving back a word 76b9dea67fSPeter Grehan * from the argv pointer. 77b9dea67fSPeter Grehan */ 78b9dea67fSPeter Grehan lwz %r4,20(%r1) /* restore argv */ 79b9dea67fSPeter Grehan addi %r3,%r4,-4 /* locate argc ptr, &argv[-1] */ 80b9dea67fSPeter Grehan 81b9dea67fSPeter Grehan addi %r4,%r1,8 /* &exit_proc on stack */ 82b9dea67fSPeter Grehan addi %r5,%r1,12 /* &obj_main on stack */ 83b9dea67fSPeter Grehan 84f62da49bSJustin Hibbits bl _rtld /* &_start = _rtld(sp, &exit_proc, &obj_main)*/ 85b9dea67fSPeter Grehan mtlr %r3 86b9dea67fSPeter Grehan 87b9dea67fSPeter Grehan /* 88b9dea67fSPeter Grehan * Restore args, with new obj/exit proc 89b9dea67fSPeter Grehan */ 90b9dea67fSPeter Grehan lwz %r3,16(%r1) /* argc */ 91b9dea67fSPeter Grehan lwz %r4,20(%r1) /* argv */ 92b9dea67fSPeter Grehan lwz %r5,24(%r1) /* envp */ 93b9dea67fSPeter Grehan lwz %r6,12(%r1) /* obj */ 94b9dea67fSPeter Grehan lwz %r7,8(%r1) /* exit proc */ 95b9dea67fSPeter Grehan lwz %r8,36(%r1) /* ps_strings */ 96b9dea67fSPeter Grehan addi %r1,%r1,48 /* restore original stackptr */ 97b9dea67fSPeter Grehan 98b9dea67fSPeter Grehan blrl /* _start(argc, argv, envp, obj, cleanup, ps_strings) */ 99b9dea67fSPeter Grehan 100b9dea67fSPeter Grehan li %r0,1 /* _exit() */ 101b9dea67fSPeter Grehan sc 102*78599c32SConrad Meyer_END(.rtld_start) 103b9dea67fSPeter Grehan 1047511645eSJustin Hibbits#ifdef __SPE__ 1057511645eSJustin Hibbits/* stack space for 30 GPRs + SPEFSCR/ACC/lr/cr */ 1067511645eSJustin Hibbits#define NREGS 31 1077511645eSJustin Hibbits#define GPRWIDTH 8 1087511645eSJustin Hibbits#define FUDGE 4 /* Fudge factor for alignment */ 1097511645eSJustin Hibbits#else 1107511645eSJustin Hibbits/* stack space for 30 GPRs + lr/cr */ 1117511645eSJustin Hibbits#define NREGS 30 1127511645eSJustin Hibbits#define GPRWIDTH 4 113e58d3795SBrandon Bergren#define FUDGE 4 1147511645eSJustin Hibbits#endif 1157511645eSJustin Hibbits/* Stack frame needs the 12-byte ABI frame plus fudge factor. */ 1167511645eSJustin Hibbits#define STACK_SIZE (NREGS * GPRWIDTH + 4 * 2 + 12 + FUDGE) 1177511645eSJustin Hibbits 118b9dea67fSPeter Grehan/* 119f62da49bSJustin Hibbits * _rtld_bind_secureplt_start() 120f62da49bSJustin Hibbits * 121f62da49bSJustin Hibbits * Call into the MI binder (Secure-PLT stub). 122f62da49bSJustin Hibbits * secure-plt expects %r11 to be the offset to the rela entry. 123f62da49bSJustin Hibbits * bss-plt expects %r11 to be index of the rela entry. 124f62da49bSJustin Hibbits * So for bss-plt, we multiply the index by 12 to get the offset. 125f62da49bSJustin Hibbits */ 126f62da49bSJustin Hibbits_ENTRY(_rtld_bind_secureplt_start) 1277511645eSJustin Hibbits stwu %r1,-STACK_SIZE(%r1) 1287511645eSJustin Hibbits#ifdef __SPE__ 1297511645eSJustin Hibbits evstdd %r0,24(%r1) 1307511645eSJustin Hibbits#else 131f62da49bSJustin Hibbits stw %r0,20(%r1) # save r0 1327511645eSJustin Hibbits#endif 133f62da49bSJustin Hibbits 134f62da49bSJustin Hibbits /* 135f62da49bSJustin Hibbits * Instead of division which is costly we will use multiplicative 136f62da49bSJustin Hibbits * inverse. a / n = ((a * inv(n)) >> 32) 137f62da49bSJustin Hibbits * where inv(n) = (0x100000000 + n - 1) / n 138f62da49bSJustin Hibbits */ 139f62da49bSJustin Hibbits mr %r0,%r11 140f62da49bSJustin Hibbits lis %r11,0x15555556@h # load multiplicative inverse of 12 141f62da49bSJustin Hibbits ori %r11,%r11,0x15555556@l 142f62da49bSJustin Hibbits mulhwu %r11,%r11,%r0 # get high half of multiplication 143f62da49bSJustin Hibbits b 1f 144*78599c32SConrad Meyer_END(_rtld_bind_secureplt_start) 145f62da49bSJustin Hibbits 146f62da49bSJustin Hibbits/* 147b9dea67fSPeter Grehan * _rtld_bind_start() 148b9dea67fSPeter Grehan * 149b9dea67fSPeter Grehan * Call into the MI binder. This routine is reached via the PLT call cell, 150b9dea67fSPeter Grehan * and then _rtld_powerpc_pltresolve(). 151b9dea67fSPeter Grehan * On entry, %r11 contains the index of the PLT cell, and %r12 contains 152b9dea67fSPeter Grehan * a pointer to the ELF object for the file. 153b9dea67fSPeter Grehan * Save all registers, call into the binder to resolve and fixup the external 154b9dea67fSPeter Grehan * routine, and then transfer to the external routine on return. 155b9dea67fSPeter Grehan */ 156b9dea67fSPeter Grehan .globl _rtld_bind 157b9dea67fSPeter Grehan 158b9dea67fSPeter Grehan_ENTRY(_rtld_bind_start) 1597511645eSJustin Hibbits stwu %r1,-STACK_SIZE(%r1) 1607511645eSJustin Hibbits#ifdef __SPE__ 1617511645eSJustin Hibbits evstdd %r0,24(%r1) 1627511645eSJustin Hibbits#else 163b9dea67fSPeter Grehan stw %r0,20(%r1) # save r0 1647511645eSJustin Hibbits#endif 165f62da49bSJustin Hibbits1: 166b9dea67fSPeter Grehan mflr %r0 167b9dea67fSPeter Grehan stw %r0,16(%r1) # save lr 168b9dea67fSPeter Grehan mfcr %r0 169b9dea67fSPeter Grehan stw %r0,12(%r1) # save cr 1707511645eSJustin Hibbits#ifdef __SPE__ 1717511645eSJustin Hibbits evstdd %r3, 32(%r1) 1727511645eSJustin Hibbits evstdd %r4, 40(%r1) 1737511645eSJustin Hibbits evstdd %r5, 48(%r1) 1747511645eSJustin Hibbits evstdd %r6, 56(%r1) 1757511645eSJustin Hibbits evstdd %r7, 64(%r1) 1767511645eSJustin Hibbits evstdd %r8, 72(%r1) 1777511645eSJustin Hibbits evstdd %r9, 80(%r1) 1787511645eSJustin Hibbits evstdd %r10, 88(%r1) 1797511645eSJustin Hibbits evstdd %r11, 96(%r1) 1807511645eSJustin Hibbits evstdd %r12, 104(%r1) 1817511645eSJustin Hibbits evstdd %r13, 112(%r1) 1827511645eSJustin Hibbits evstdd %r14, 120(%r1) 1837511645eSJustin Hibbits evstdd %r15, 128(%r1) 1847511645eSJustin Hibbits evstdd %r16, 136(%r1) 1857511645eSJustin Hibbits evstdd %r17, 144(%r1) 1867511645eSJustin Hibbits evstdd %r18, 152(%r1) 1877511645eSJustin Hibbits evstdd %r19, 160(%r1) 1887511645eSJustin Hibbits evstdd %r20, 168(%r1) 1897511645eSJustin Hibbits evstdd %r21, 176(%r1) 1907511645eSJustin Hibbits evstdd %r22, 184(%r1) 1917511645eSJustin Hibbits evstdd %r23, 192(%r1) 1927511645eSJustin Hibbits evstdd %r24, 200(%r1) 1937511645eSJustin Hibbits evstdd %r25, 208(%r1) 1947511645eSJustin Hibbits evstdd %r26, 216(%r1) 1957511645eSJustin Hibbits evstdd %r27, 224(%r1) 1967511645eSJustin Hibbits evstdd %r28, 232(%r1) 1977511645eSJustin Hibbits evstdd %r29, 240(%r1) 1987511645eSJustin Hibbits evstdd %r30, 248(%r1) 1997511645eSJustin Hibbits li %r3, 256 2007511645eSJustin Hibbits evstddx %r31, %r1, %r3 2017511645eSJustin Hibbits evxor %r0, %r0, %r0 2027511645eSJustin Hibbits li %r3, 264 2037511645eSJustin Hibbits evmwumiaa %r0, %r0, %r0 2047511645eSJustin Hibbits evstddx %r0, %r1, %r3 2057511645eSJustin Hibbits mfspr %r3, SPR_SPEFSCR 2067511645eSJustin Hibbits stw %r3, 20(%r1) 2077511645eSJustin Hibbits#else 208b9dea67fSPeter Grehan stmw %r3,24(%r1) # save r3-r31 2097511645eSJustin Hibbits#endif 210b9dea67fSPeter Grehan 211b9dea67fSPeter Grehan mr %r3,%r12 # obj 212b9dea67fSPeter Grehan mulli %r4,%r11,12 # rela index * sizeof(Elf_Rela) 213f62da49bSJustin Hibbits bl _rtld_bind # target addr = _rtld_bind(obj, reloff) 214b9dea67fSPeter Grehan mtctr %r3 # move absolute target addr into ctr 215b9dea67fSPeter Grehan 2167511645eSJustin Hibbits#ifdef __SPE__ 2177511645eSJustin Hibbits lwz %r3, 20(%r1) 2187511645eSJustin Hibbits mtspr SPR_SPEFSCR, %r3 2197511645eSJustin Hibbits li %r3, 264 2207511645eSJustin Hibbits evlddx %r0, %r3, %r1 2217511645eSJustin Hibbits evmra %r0, %r0 2227511645eSJustin Hibbits evldd %r3, 32(%r1) 2237511645eSJustin Hibbits evldd %r4, 40(%r1) 2247511645eSJustin Hibbits evldd %r5, 48(%r1) 2257511645eSJustin Hibbits evldd %r6, 56(%r1) 2267511645eSJustin Hibbits evldd %r7, 64(%r1) 2277511645eSJustin Hibbits evldd %r8, 72(%r1) 2287511645eSJustin Hibbits evldd %r9, 80(%r1) 2297511645eSJustin Hibbits evldd %r10, 88(%r1) 2307511645eSJustin Hibbits evldd %r11, 96(%r1) 2317511645eSJustin Hibbits evldd %r12, 104(%r1) 2327511645eSJustin Hibbits evldd %r13, 112(%r1) 2337511645eSJustin Hibbits evldd %r14, 120(%r1) 2347511645eSJustin Hibbits evldd %r15, 128(%r1) 2357511645eSJustin Hibbits evldd %r16, 136(%r1) 2367511645eSJustin Hibbits evldd %r17, 144(%r1) 2377511645eSJustin Hibbits evldd %r18, 152(%r1) 2387511645eSJustin Hibbits evldd %r19, 160(%r1) 2397511645eSJustin Hibbits evldd %r20, 168(%r1) 2407511645eSJustin Hibbits evldd %r21, 176(%r1) 2417511645eSJustin Hibbits evldd %r22, 184(%r1) 2427511645eSJustin Hibbits evldd %r23, 192(%r1) 2437511645eSJustin Hibbits evldd %r24, 200(%r1) 2447511645eSJustin Hibbits evldd %r25, 208(%r1) 2457511645eSJustin Hibbits evldd %r26, 216(%r1) 2467511645eSJustin Hibbits evldd %r27, 224(%r1) 2477511645eSJustin Hibbits evldd %r28, 232(%r1) 2487511645eSJustin Hibbits evldd %r29, 240(%r1) 2497511645eSJustin Hibbits evldd %r30, 248(%r1) 2507511645eSJustin Hibbits li %r0, 256 2517511645eSJustin Hibbits evlddx %r31, %r1, %r0 2527511645eSJustin Hibbits#else 253b9dea67fSPeter Grehan lmw %r3,24(%r1) # restore r3-r31 2547511645eSJustin Hibbits#endif 255b9dea67fSPeter Grehan lwz %r0,12(%r1) # restore cr 256b9dea67fSPeter Grehan mtcr %r0 257b9dea67fSPeter Grehan lwz %r0,16(%r1) # restore lr 258b9dea67fSPeter Grehan mtlr %r0 2597511645eSJustin Hibbits#ifdef __SPE__ 2607511645eSJustin Hibbits evldd %r0,24(%r1) 2617511645eSJustin Hibbits#else 262b9dea67fSPeter Grehan lwz %r0,20(%r1) # restore r0 2637511645eSJustin Hibbits#endif 264b9dea67fSPeter Grehan 2657511645eSJustin Hibbits addi %r1,%r1,STACK_SIZE # restore stack 266b9dea67fSPeter Grehan bctr # jump to target 267*78599c32SConrad Meyer_END(_rtld_bind_start) 268b9dea67fSPeter Grehan 269b9dea67fSPeter Grehan 270b9dea67fSPeter Grehan/* 271b9dea67fSPeter Grehan * _rtld_powerpc_pltresolve() 272b9dea67fSPeter Grehan * 273b9dea67fSPeter Grehan * This routine is copied into the latter part of the 72-byte reserved 274b9dea67fSPeter Grehan * area at the start of the PLT. The absolute address of the _rtld_bind_start 275b9dea67fSPeter Grehan * routine, and the ELF object for the loaded file, are inserted into 276b9dea67fSPeter Grehan * the code by the reloc.c:init_pltgot() routine. 277b9dea67fSPeter Grehan * The first time an external routine is called, the PLT slot will 278b9dea67fSPeter Grehan * set up %r11 to the offset of the slot, and will jump to this routine. 279b9dea67fSPeter Grehan * The ELF object is shifted into %r11, and _rtld_bind_start is called 280b9dea67fSPeter Grehan * to complete the binding. 281b9dea67fSPeter Grehan */ 2824f2730f7SNathan Whitehorn_ENTRY(_rtld_powerpc_pltlongresolve) 2834f2730f7SNathan Whitehorn lis %r12,0 # lis 12,jmptab@ha 2844f2730f7SNathan Whitehorn addi %r12,%r12,0 # addi 12,12,jmptab@l 2854f2730f7SNathan Whitehorn subf %r11,%r12,%r11 # reloff 2864f2730f7SNathan Whitehorn li %r12,2 2874f2730f7SNathan Whitehorn srw %r11,%r11,%r12 # index = reloff/sizeof(Elf_Addr) 288*78599c32SConrad Meyer_END(_rtld_powerpc_pltlongresolve) 289b9dea67fSPeter Grehan_ENTRY(_rtld_powerpc_pltresolve) 290b9dea67fSPeter Grehan lis %r12,0 # lis 12,_rtld_bind_start@ha 291b9dea67fSPeter Grehan addi %r12,%r12,0 # addi 12,12,_rtld_bind_start@l 292b9dea67fSPeter Grehan mtctr %r12 293b9dea67fSPeter Grehan lis %r12,0 # lis 12,obj@ha 294b9dea67fSPeter Grehan addi %r12,%r12,0 # addi 12,12,obj@l 295b9dea67fSPeter Grehan bctr 296*78599c32SConrad Meyer_END(_rtld_powerpc_pltresolve) 297b9dea67fSPeter Grehan 298b9dea67fSPeter Grehan/* 299b9dea67fSPeter Grehan * _rtld_powerpc_pltcall() 300b9dea67fSPeter Grehan * 301b9dea67fSPeter Grehan * This routine is copied into the 72-byte reserved area at the 302b9dea67fSPeter Grehan * start of the PLT. The reloc.c:init_pltgot() routine inserts 303b9dea67fSPeter Grehan * the absolute address of the jumptable. 304b9dea67fSPeter Grehan * Control is transferred to this routine when the binder has 305b9dea67fSPeter Grehan * located the external routine, but determined that it is > 32Mb 306b9dea67fSPeter Grehan * from the PLT slot. Code is inserted into the PLT slot to set up 307b9dea67fSPeter Grehan * %r11 with the jumptable index, and jump to here, where the 308b9dea67fSPeter Grehan * absolute address of the external routine is loaded from the 309b9dea67fSPeter Grehan * jumptable and transferred to 310b9dea67fSPeter Grehan */ 311b9dea67fSPeter Grehan_ENTRY(_rtld_powerpc_pltcall) 312b9dea67fSPeter Grehan slwi %r11,%r11,2 # jmptab offset = index * 4 313b9dea67fSPeter Grehan addis %r11,%r11,0 # addis 11,11,jmptab@ha 314b9dea67fSPeter Grehan lwz %r11,0(%r11) # lwz 11,jmptab@l(11) 315b9dea67fSPeter Grehan mtctr %r11 316b9dea67fSPeter Grehan bctr # (*jmptab[index])() 317*78599c32SConrad Meyer_END(_rtld_powerpc_pltcall) 318b9dea67fSPeter Grehan 3198ae32158SKonstantin Belousov .section .note.GNU-stack,"",%progbits 320