Path: blob/main/libexec/rtld-elf/powerpc/rtld_start.S
34923 views
/* $NetBSD: rtld_start.S,v 1.4 2001/09/26 04:06:43 mycroft Exp $ */12/*-3* Copyright (C) 1998 Tsubai Masanari4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. The name of the author may not be used to endorse or promote products15* derived from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR18* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES19* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.20* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,21* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT22* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/2829#include <machine/asm.h>30#include <machine/spr.h> /* For SPR_SPEFSCR if needed. */3132.extern _GLOBAL_OFFSET_TABLE_33.extern _DYNAMIC3435_ENTRY(.rtld_start)36stwu %r1,-48(%r1) /* 16-byte aligned stack for reg saves +37exit_proc & obj _rtld args +38backchain & lrsave stack frame */39stw %r3,16(%r1) /* argc */40stw %r4,20(%r1) /* argv */41stw %r5,24(%r1) /* envp */42/* stw %r6,28(%r1) *//* obj (always 0) */43/* stw %r7,32(%r1) *//* cleanup (always 0) */44stw %r8,36(%r1) /* ps_strings */4546/*47* Perform initial relocation of ld-elf.so. Not as easy as it48* sounds.49* - perform small forward branch to put PC into link reg50* - use link-time constants to determine offset to the51* _DYNAMIC section and the GOT. Add these to the PC to52* convert to absolute addresses.53* - read GOT[0], which is the SVR4 ABI-specified link-time54* value of _DYNAMIC. Subtract this value from the absolute55* value to determine the load address56* - call reloc_non_plt_self() to fix up ld-elf.so's relocations57*/58bcl 20,31,1f591: mflr %r3060mr %r3,%r30 # save for _DYNAMIC61addis %r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@ha62addi %r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@l63addis %r3,%r3,_DYNAMIC-1b@ha # get _DYNAMIC actual address64addi %r3,%r3,_DYNAMIC-1b@l65lwz %r28,0(%r30) # get base-relative &_DYNAMIC66sub %r28,%r3,%r28 # r28 = relocbase67mr %r4,%r28 # r4 = relocbase68bl reloc_non_plt_self /* reloc_non_plt_self(&_DYNAMIC,base) */6970/*71* The _rtld() function likes to see a stack layout containing72* { argc, argv[0], argv[1] ... argv[N], 0, env[0], ... , env[N] }73* Since the PowerPC stack was 16-byte aligned at exec time, the74* original stack layout has to be found by moving back a word75* from the argv pointer.76*/77lwz %r4,20(%r1) /* restore argv */78addi %r3,%r4,-4 /* locate argc ptr, &argv[-1] */7980addi %r4,%r1,8 /* &exit_proc on stack */81addi %r5,%r1,12 /* &obj_main on stack */8283bl _rtld /* &_start = _rtld(sp, &exit_proc, &obj_main)*/84mtlr %r38586/*87* Restore args, with new obj/exit proc88*/89lwz %r3,16(%r1) /* argc */90lwz %r4,20(%r1) /* argv */91lwz %r5,24(%r1) /* envp */92lwz %r6,12(%r1) /* obj */93lwz %r7,8(%r1) /* exit proc */94lwz %r8,36(%r1) /* ps_strings */95addi %r1,%r1,48 /* restore original stackptr */9697blrl /* _start(argc, argv, envp, obj, cleanup, ps_strings) */9899li %r0,1 /* _exit() */100sc101_END(.rtld_start)102103#ifdef __SPE__104/* stack space for 30 GPRs + SPEFSCR/ACC/lr/cr */105#define NREGS 31106#define GPRWIDTH 8107#define FUDGE 4 /* Fudge factor for alignment */108#else109/* stack space for 30 GPRs + lr/cr */110#define NREGS 30111#define GPRWIDTH 4112#define FUDGE 4113#endif114/* Stack frame needs the 12-byte ABI frame plus fudge factor. */115#define STACK_SIZE (NREGS * GPRWIDTH + 4 * 2 + 12 + FUDGE)116117/*118* _rtld_bind_secureplt_start()119*120* Call into the MI binder (Secure-PLT stub).121* secure-plt expects %r11 to be the offset to the rela entry.122* bss-plt expects %r11 to be index of the rela entry.123* So for bss-plt, we multiply the index by 12 to get the offset.124*/125_ENTRY(_rtld_bind_secureplt_start)126stwu %r1,-STACK_SIZE(%r1)127#ifdef __SPE__128evstdd %r0,24(%r1)129#else130stw %r0,20(%r1) # save r0131#endif132133/*134* Instead of division which is costly we will use multiplicative135* inverse. a / n = ((a * inv(n)) >> 32)136* where inv(n) = (0x100000000 + n - 1) / n137*/138mr %r0,%r11139lis %r11,0x15555556@h # load multiplicative inverse of 12140ori %r11,%r11,0x15555556@l141mulhwu %r11,%r11,%r0 # get high half of multiplication142b 1f143_END(_rtld_bind_secureplt_start)144145/*146* _rtld_bind_start()147*148* Call into the MI binder. This routine is reached via the PLT call cell,149* and then _rtld_powerpc_pltresolve().150* On entry, %r11 contains the index of the PLT cell, and %r12 contains151* a pointer to the ELF object for the file.152* Save all registers, call into the binder to resolve and fixup the external153* routine, and then transfer to the external routine on return.154*/155.globl _rtld_bind156157_ENTRY(_rtld_bind_start)158stwu %r1,-STACK_SIZE(%r1)159#ifdef __SPE__160evstdd %r0,24(%r1)161#else162stw %r0,20(%r1) # save r0163#endif1641:165mflr %r0166stw %r0,16(%r1) # save lr167mfcr %r0168stw %r0,12(%r1) # save cr169#ifdef __SPE__170evstdd %r3, 32(%r1)171evstdd %r4, 40(%r1)172evstdd %r5, 48(%r1)173evstdd %r6, 56(%r1)174evstdd %r7, 64(%r1)175evstdd %r8, 72(%r1)176evstdd %r9, 80(%r1)177evstdd %r10, 88(%r1)178evstdd %r11, 96(%r1)179evstdd %r12, 104(%r1)180evstdd %r13, 112(%r1)181evstdd %r14, 120(%r1)182evstdd %r15, 128(%r1)183evstdd %r16, 136(%r1)184evstdd %r17, 144(%r1)185evstdd %r18, 152(%r1)186evstdd %r19, 160(%r1)187evstdd %r20, 168(%r1)188evstdd %r21, 176(%r1)189evstdd %r22, 184(%r1)190evstdd %r23, 192(%r1)191evstdd %r24, 200(%r1)192evstdd %r25, 208(%r1)193evstdd %r26, 216(%r1)194evstdd %r27, 224(%r1)195evstdd %r28, 232(%r1)196evstdd %r29, 240(%r1)197evstdd %r30, 248(%r1)198li %r3, 256199evstddx %r31, %r1, %r3200evxor %r0, %r0, %r0201li %r3, 264202evmwumiaa %r0, %r0, %r0203evstddx %r0, %r1, %r3204mfspr %r3, SPR_SPEFSCR205stw %r3, 20(%r1)206#else207stmw %r3,24(%r1) # save r3-r31208#endif209210mr %r3,%r12 # obj211mulli %r4,%r11,12 # rela index * sizeof(Elf_Rela)212bl _rtld_bind # target addr = _rtld_bind(obj, reloff)213mtctr %r3 # move absolute target addr into ctr214215#ifdef __SPE__216lwz %r3, 20(%r1)217mtspr SPR_SPEFSCR, %r3218li %r3, 264219evlddx %r0, %r3, %r1220evmra %r0, %r0221evldd %r3, 32(%r1)222evldd %r4, 40(%r1)223evldd %r5, 48(%r1)224evldd %r6, 56(%r1)225evldd %r7, 64(%r1)226evldd %r8, 72(%r1)227evldd %r9, 80(%r1)228evldd %r10, 88(%r1)229evldd %r11, 96(%r1)230evldd %r12, 104(%r1)231evldd %r13, 112(%r1)232evldd %r14, 120(%r1)233evldd %r15, 128(%r1)234evldd %r16, 136(%r1)235evldd %r17, 144(%r1)236evldd %r18, 152(%r1)237evldd %r19, 160(%r1)238evldd %r20, 168(%r1)239evldd %r21, 176(%r1)240evldd %r22, 184(%r1)241evldd %r23, 192(%r1)242evldd %r24, 200(%r1)243evldd %r25, 208(%r1)244evldd %r26, 216(%r1)245evldd %r27, 224(%r1)246evldd %r28, 232(%r1)247evldd %r29, 240(%r1)248evldd %r30, 248(%r1)249li %r0, 256250evlddx %r31, %r1, %r0251#else252lmw %r3,24(%r1) # restore r3-r31253#endif254lwz %r0,12(%r1) # restore cr255mtcr %r0256lwz %r0,16(%r1) # restore lr257mtlr %r0258#ifdef __SPE__259evldd %r0,24(%r1)260#else261lwz %r0,20(%r1) # restore r0262#endif263264addi %r1,%r1,STACK_SIZE # restore stack265bctr # jump to target266_END(_rtld_bind_start)267268269/*270* _rtld_powerpc_pltresolve()271*272* This routine is copied into the latter part of the 72-byte reserved273* area at the start of the PLT. The absolute address of the _rtld_bind_start274* routine, and the ELF object for the loaded file, are inserted into275* the code by the reloc.c:init_pltgot() routine.276* The first time an external routine is called, the PLT slot will277* set up %r11 to the offset of the slot, and will jump to this routine.278* The ELF object is shifted into %r11, and _rtld_bind_start is called279* to complete the binding.280*/281_ENTRY(_rtld_powerpc_pltlongresolve)282lis %r12,0 # lis 12,jmptab@ha283addi %r12,%r12,0 # addi 12,12,jmptab@l284subf %r11,%r12,%r11 # reloff285li %r12,2286srw %r11,%r11,%r12 # index = reloff/sizeof(Elf_Addr)287_END(_rtld_powerpc_pltlongresolve)288_ENTRY(_rtld_powerpc_pltresolve)289lis %r12,0 # lis 12,_rtld_bind_start@ha290addi %r12,%r12,0 # addi 12,12,_rtld_bind_start@l291mtctr %r12292lis %r12,0 # lis 12,obj@ha293addi %r12,%r12,0 # addi 12,12,obj@l294bctr295_END(_rtld_powerpc_pltresolve)296297/*298* _rtld_powerpc_pltcall()299*300* This routine is copied into the 72-byte reserved area at the301* start of the PLT. The reloc.c:init_pltgot() routine inserts302* the absolute address of the jumptable.303* Control is transferred to this routine when the binder has304* located the external routine, but determined that it is > 32Mb305* from the PLT slot. Code is inserted into the PLT slot to set up306* %r11 with the jumptable index, and jump to here, where the307* absolute address of the external routine is loaded from the308* jumptable and transferred to309*/310_ENTRY(_rtld_powerpc_pltcall)311slwi %r11,%r11,2 # jmptab offset = index * 4312addis %r11,%r11,0 # addis 11,11,jmptab@ha313lwz %r11,0(%r11) # lwz 11,jmptab@l(11)314mtctr %r11315bctr # (*jmptab[index])()316_END(_rtld_powerpc_pltcall)317318.section .note.GNU-stack,"",%progbits319320321