Path: blob/main/libexec/rtld-elf/powerpc/rtld_start.S
103381 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/* stack space for 30 GPRs + lr/cr */104#define NREGS 30105#define GPRWIDTH 4106#define FUDGE 4107/* Stack frame needs the 12-byte ABI frame plus fudge factor. */108#define STACK_SIZE (NREGS * GPRWIDTH + 4 * 2 + 12 + FUDGE)109110/*111* _rtld_bind_secureplt_start()112*113* Call into the MI binder (Secure-PLT stub).114* secure-plt expects %r11 to be the offset to the rela entry.115* bss-plt expects %r11 to be index of the rela entry.116* So for bss-plt, we multiply the index by 12 to get the offset.117*/118_ENTRY(_rtld_bind_secureplt_start)119stwu %r1,-STACK_SIZE(%r1)120stw %r0,20(%r1) # save r0121122/*123* Instead of division which is costly we will use multiplicative124* inverse. a / n = ((a * inv(n)) >> 32)125* where inv(n) = (0x100000000 + n - 1) / n126*/127mr %r0,%r11128lis %r11,0x15555556@h # load multiplicative inverse of 12129ori %r11,%r11,0x15555556@l130mulhwu %r11,%r11,%r0 # get high half of multiplication131b 1f132_END(_rtld_bind_secureplt_start)133134/*135* _rtld_bind_start()136*137* Call into the MI binder. This routine is reached via the PLT call cell,138* and then _rtld_powerpc_pltresolve().139* On entry, %r11 contains the index of the PLT cell, and %r12 contains140* a pointer to the ELF object for the file.141* Save all registers, call into the binder to resolve and fixup the external142* routine, and then transfer to the external routine on return.143*/144.globl _rtld_bind145146_ENTRY(_rtld_bind_start)147stwu %r1,-STACK_SIZE(%r1)148stw %r0,20(%r1) # save r01491:150mflr %r0151stw %r0,16(%r1) # save lr152mfcr %r0153stw %r0,12(%r1) # save cr154stmw %r3,24(%r1) # save r3-r31155156mr %r3,%r12 # obj157mulli %r4,%r11,12 # rela index * sizeof(Elf_Rela)158bl _rtld_bind # target addr = _rtld_bind(obj, reloff)159mtctr %r3 # move absolute target addr into ctr160161lmw %r3,24(%r1) # restore r3-r31162lwz %r0,12(%r1) # restore cr163mtcr %r0164lwz %r0,16(%r1) # restore lr165mtlr %r0166lwz %r0,20(%r1) # restore r0167168addi %r1,%r1,STACK_SIZE # restore stack169bctr # jump to target170_END(_rtld_bind_start)171172173/*174* _rtld_powerpc_pltresolve()175*176* This routine is copied into the latter part of the 72-byte reserved177* area at the start of the PLT. The absolute address of the _rtld_bind_start178* routine, and the ELF object for the loaded file, are inserted into179* the code by the reloc.c:init_pltgot() routine.180* The first time an external routine is called, the PLT slot will181* set up %r11 to the offset of the slot, and will jump to this routine.182* The ELF object is shifted into %r11, and _rtld_bind_start is called183* to complete the binding.184*/185_ENTRY(_rtld_powerpc_pltlongresolve)186lis %r12,0 # lis 12,jmptab@ha187addi %r12,%r12,0 # addi 12,12,jmptab@l188subf %r11,%r12,%r11 # reloff189li %r12,2190srw %r11,%r11,%r12 # index = reloff/sizeof(Elf_Addr)191_END(_rtld_powerpc_pltlongresolve)192_ENTRY(_rtld_powerpc_pltresolve)193lis %r12,0 # lis 12,_rtld_bind_start@ha194addi %r12,%r12,0 # addi 12,12,_rtld_bind_start@l195mtctr %r12196lis %r12,0 # lis 12,obj@ha197addi %r12,%r12,0 # addi 12,12,obj@l198bctr199_END(_rtld_powerpc_pltresolve)200201/*202* _rtld_powerpc_pltcall()203*204* This routine is copied into the 72-byte reserved area at the205* start of the PLT. The reloc.c:init_pltgot() routine inserts206* the absolute address of the jumptable.207* Control is transferred to this routine when the binder has208* located the external routine, but determined that it is > 32Mb209* from the PLT slot. Code is inserted into the PLT slot to set up210* %r11 with the jumptable index, and jump to here, where the211* absolute address of the external routine is loaded from the212* jumptable and transferred to213*/214_ENTRY(_rtld_powerpc_pltcall)215slwi %r11,%r11,2 # jmptab offset = index * 4216addis %r11,%r11,0 # addis 11,11,jmptab@ha217lwz %r11,0(%r11) # lwz 11,jmptab@l(11)218mtctr %r11219bctr # (*jmptab[index])()220_END(_rtld_powerpc_pltcall)221222.section .note.GNU-stack,"",%progbits223224225