Path: blob/main/libexec/rtld-elf/powerpc64/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>3031.extern _GLOBAL_OFFSET_TABLE_32.extern _DYNAMIC3334_ENTRY(_rtld_start)35stdu %r1,-144(%r1) /* 16-byte aligned stack for reg saves +36exit_proc & obj _rtld args +37backchain & lrsave stack frame */3839/* Save and restore only initial argv, because _rtld will modify40* argv and envp if invoked explicitly, making it necessary to41* load the (possibly) adjusted values from the stack.42*/43std %r4,104(%r1) /* argv */44/* std %r6,120(%r1) *//* obj (always 0) */45/* std %r7,128(%r1) *//* cleanup (always 0) */46std %r8,136(%r1) /* ps_strings */4748/*49* Perform initial relocation of ld-elf.so. Not as easy as it50* sounds.51* - perform small forward branch to put PC into link reg52* - use link-time constants to determine offset to the53* _DYNAMIC section and the GOT. Add these to the PC to54* convert to absolute addresses.55* - call reloc_non_plt_self() to fix up ld-elf.so's relocations56*/5758bl 1f59.llong _DYNAMIC-.601:61mflr %r3 /* PC value at .llong */62ld %r4,0(%r3) /* offset to _DYNAMIC */63add %r3,%r4,%r3 /* r3 = &_DYNAMIC, absolute value */6465ld %r4,-0x8000(%r2) /* First TOC entry is TOC base */66subf %r4,%r4,%r2 /* Subtract from real TOC base to get base */6768bl reloc_non_plt_self /* reloc_non_plt_self(&_DYNAMIC,base) */69nop7071/*72* The _rtld() function likes to see a stack layout containing73* { argc, argv[0], argv[1] ... argv[N], 0, env[0], ... , env[N] }74* Since the PowerPC stack was 16-byte aligned at exec time, the75* original stack layout has to be found by moving back a word76* from the argv pointer.77*/78ld %r4,104(%r1)79addi %r3,%r4,-8 /* locate argc ptr, &argv[-1] */80addi %r4,%r1,128 /* &exit_proc on stack */81addi %r5,%r1,120 /* &obj_main on stack */8283bl _rtld /* &_start = _rtld(sp, &exit_proc, &obj_main)*/84nop85#if !defined(_CALL_ELF) || _CALL_ELF == 186ld %r2,8(%r3)87ld %r11,16(%r3)88ld %r3,0(%r3)89#else90mr %r12,%r391#endif92mtlr %r39394/*95* Restore args, with new obj/exit proc96*/97ld %r4,104(%r1) /* argv */98ld %r3,-8(%r4) /* argc */99100/* envp = argv + argc + 1 */101addi %r5,%r3,1102sldi %r5,%r5,3 /* x8 */103add %r5,%r4,%r5104105ld %r6,120(%r1) /* obj */106ld %r7,128(%r1) /* exit proc */107ld %r8,136(%r1) /* ps_strings */108109blrl /* _start(argc, argv, envp, obj, cleanup, ps_strings) */110111li %r0,1 /* _exit() */112sc113_END(_rtld_start)114115/*116* _rtld_bind_start()117*118* Call into the MI binder. This routine is reached via the PLT call cell119*120* On entry, %r11 contains an object pointer and %r0 contains the PLT index.121*122* Save all registers, call into the binder to resolve and fixup the external123* routine, and then transfer to the external routine on return.124*/125.globl _rtld_bind126127_ENTRY(_rtld_bind_start)128mr %r12,%r0 # save r0 (index) immediately to r12129mflr %r0130std %r0,16(%r1) # save lr131mfcr %r0132std %r0,8(%r1) # save cr133134stdu %r1,-48-12*8(%r1) # stack space for 8 regs + header135# + 2 save regs136std %r3,64+0*8(%r1) # save r3-r10 (arguments)137std %r4,64+1*8(%r1)138std %r5,64+2*8(%r1)139std %r6,64+3*8(%r1)140std %r7,64+4*8(%r1)141std %r8,64+5*8(%r1)142std %r9,64+6*8(%r1)143std %r10,64+7*8(%r1)144145mr %r3,%r11146mulli %r4,%r12,24 # Multiply index by sizeof(Elf_Rela)147148bl _rtld_bind # target addr = _rtld_bind(obj, reloff)149nop150151#if !defined(_CALL_ELF) || _CALL_ELF == 1152ld %r2,8(%r3)153ld %r11,16(%r3)154ld %r3,0(%r3)155#else156mr %r12,%r3157#endif158mtctr %r3 # move absolute target addr into ctr159160ld %r3,64+0*8(%r1) # restore r3-r10161ld %r4,64+1*8(%r1)162ld %r5,64+2*8(%r1)163ld %r6,64+3*8(%r1)164ld %r7,64+4*8(%r1)165ld %r8,64+5*8(%r1)166ld %r9,64+6*8(%r1)167ld %r10,64+7*8(%r1)168169ld %r1,0(%r1) # restore stack170ld %r0,8(%r1) # restore cr171mtcr %r0172ld %r0,16(%r1) # restore lr173mtlr %r0174175bctr # jump to target176_END(_rtld_bind_start)177178.section .note.GNU-stack,"",%progbits179180181