1 1.17 mrg /* $NetBSD: rtld_start.S,v 1.17 2011/09/26 01:52:22 mrg Exp $ */ 2 1.1 tsubai 3 1.1 tsubai /*- 4 1.1 tsubai * Copyright (C) 1998 Tsubai Masanari 5 1.11 mycroft * Portions copyright 2002 Charles M. Hannum <root (at) ihack.net> 6 1.1 tsubai * All rights reserved. 7 1.1 tsubai * 8 1.1 tsubai * Redistribution and use in source and binary forms, with or without 9 1.1 tsubai * modification, are permitted provided that the following conditions 10 1.1 tsubai * are met: 11 1.1 tsubai * 1. Redistributions of source code must retain the above copyright 12 1.1 tsubai * notice, this list of conditions and the following disclaimer. 13 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 tsubai * notice, this list of conditions and the following disclaimer in the 15 1.1 tsubai * documentation and/or other materials provided with the distribution. 16 1.1 tsubai * 3. The name of the author may not be used to endorse or promote products 17 1.1 tsubai * derived from this software without specific prior written permission. 18 1.1 tsubai * 19 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 1.1 tsubai */ 30 1.1 tsubai 31 1.1 tsubai #include <machine/asm.h> 32 1.1 tsubai 33 1.1 tsubai .globl _rtld_start 34 1.1 tsubai .globl _rtld 35 1.1 tsubai 36 1.1 tsubai .text 37 1.1 tsubai 38 1.1 tsubai _rtld_start: 39 1.12 matt stwu %r1,-48(%r1) 40 1.12 matt stw %r3,12(%r1) # argc 41 1.12 matt stw %r4,16(%r1) # argv 42 1.12 matt stw %r5,20(%r1) # envp 43 1.12 matt /* stw %r6,24(%r1) # obj (always 0) */ 44 1.12 matt /* stw %r7,28(%r1) # cleanup (always 0) */ 45 1.12 matt stw %r8,32(%r1) # ps_strings 46 1.1 tsubai 47 1.14 matt bcl 20,31,1f 48 1.14 matt 1: mflr %r30 49 1.14 matt mr %r3,%r30 # save for _DYNAMIC 50 1.14 matt addis %r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@ha 51 1.14 matt addi %r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@l 52 1.14 matt addis %r3,%r3,_DYNAMIC-1b@ha # get _DYNAMIC actual address 53 1.14 matt addi %r3,%r3,_DYNAMIC-1b@l 54 1.14 matt lwz %r28,0(%r30) # get base-relative &_DYNAMIC 55 1.14 matt sub %r28,%r3,%r28 # r28 = relocbase 56 1.13 thorpej mr %r4,%r28 # r4 = relocbase 57 1.14 matt bl _rtld_relocate_nonplt_self 58 1.1 tsubai 59 1.12 matt lwz %r3,16(%r1) 60 1.12 matt addi %r3,%r3,-12 # sp = &argv[-3] /* XXX */ 61 1.13 thorpej mr %r4,%r28 # r4 = relocbase 62 1.14 matt bl _rtld # _start = _rtld(sp, relocbase) 63 1.12 matt mtlr %r3 64 1.1 tsubai 65 1.12 matt lwz %r3,12(%r1) # argc 66 1.12 matt lwz %r4,16(%r1) # argv 67 1.12 matt lwz %r5,20(%r1) # envp 68 1.12 matt lwz %r6,-8(%r4) # obj = sp[1] (== argv[-2]) 69 1.12 matt lwz %r7,-12(%r4) # cleanup = sp[0] (== argv[-3]) 70 1.12 matt lwz %r8,32(%r1) # ps_strings 71 1.1 tsubai 72 1.12 matt addi %r1,%r1,48 73 1.1 tsubai blrl # _start(argc, argv, envp, obj, cleanup, ps_strings) 74 1.1 tsubai 75 1.12 matt li %r0,1 # _exit() 76 1.1 tsubai sc 77 1.1 tsubai 78 1.14 matt END(_rtld_start) 79 1.1 tsubai 80 1.10 mycroft .globl _rtld_bind 81 1.1 tsubai 82 1.14 matt /* 83 1.14 matt * secure-plt expects %r11 to be the offset to the rela entry. 84 1.14 matt * bss-plt expects %r11 to be index of the rela entry. 85 1.14 matt * So for bss-plt, we multiply the index by 12 to get the offset. 86 1.14 matt */ 87 1.15 matt ENTRY_NOPROFILE(_rtld_bind_secureplt_start) 88 1.15 matt stwu %r1,-160(%r1) 89 1.15 matt stw %r0,20(%r1) 90 1.15 matt 91 1.15 matt /* 92 1.15 matt * Instead of division which is costly we will use multiplicative 93 1.15 matt * inverse. a / n = ((a * inv(n)) >> 32) 94 1.15 matt * where inv(n) = (0x100000000 + n - 1) / n 95 1.15 matt */ 96 1.15 matt mr %r0,%r11 97 1.16 matt lis %r11,0x15555556@h # load multiplicative inverse of 12 98 1.16 matt ori %r11,%r11,0x15555556@l 99 1.15 matt mulhwu %r11,%r11,%r0 # get high half of multiplication 100 1.15 matt 101 1.15 matt b 1f 102 1.14 matt ENTRY_NOPROFILE(_rtld_bind_bssplt_start) 103 1.12 matt stwu %r1,-160(%r1) 104 1.4 mycroft 105 1.12 matt stw %r0,20(%r1) 106 1.15 matt 1: 107 1.12 matt mflr %r0 108 1.12 matt stw %r0,16(%r1) # save lr 109 1.12 matt mfcr %r0 110 1.12 matt stw %r0,12(%r1) # save cr 111 1.12 matt stmw %r3,24(%r1) # save r3-r31 112 1.1 tsubai 113 1.12 matt mr %r3,%r12 # obj 114 1.12 matt mr %r4,%r11 # reloff 115 1.14 matt bl _rtld_bind # _rtld_bind(obj, reloff) 116 1.12 matt mtctr %r3 117 1.1 tsubai 118 1.12 matt lmw %r3,24(%r1) # load r3-r31 119 1.12 matt lwz %r0,12(%r1) # restore cr 120 1.12 matt mtcr %r0 121 1.12 matt lwz %r0,16(%r1) # restore lr 122 1.12 matt mtlr %r0 123 1.12 matt lwz %r0,20(%r1) 124 1.4 mycroft 125 1.12 matt addi %r1,%r1,160 126 1.1 tsubai bctr 127 1.17 mrg END(_rtld_bind_secureplt_start) 128 1.1 tsubai 129 1.1 tsubai .globl _rtld_powerpc_pltcall 130 1.1 tsubai .globl _rtld_powerpc_pltresolve 131 1.1 tsubai 132 1.1 tsubai _rtld_powerpc_pltcall: 133 1.12 matt slwi %r11,%r11,2 134 1.12 matt addis %r11,%r11,0 # addis 11,11,jmptab@ha 135 1.12 matt lwz %r11,0(%r11) # lwz 11,jmptab@l(11) 136 1.12 matt mtctr %r11 137 1.1 tsubai bctr 138 1.1 tsubai 139 1.1 tsubai _rtld_powerpc_pltresolve: 140 1.14 matt lis %r12,0 # lis 12,_rtld_bind_bssplt_start@ha 141 1.14 matt addi %r12,%r12,0 # addi 12,12,_rtld_bind_bssplt_start@l 142 1.12 matt mtctr %r12 143 1.12 matt lis %r12,0 # lis 12,obj@ha 144 1.12 matt addi %r12,%r12,0 # addi 12,12,obj@l 145 1.1 tsubai bctr 146