]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - libexec/rtld-elf/powerpc/rtld_start.S
Update to Zstandard 1.4.2
[FreeBSD/FreeBSD.git] / libexec / rtld-elf / powerpc / rtld_start.S
1 /*      $NetBSD: rtld_start.S,v 1.4 2001/09/26 04:06:43 mycroft Exp $   */
2
3 /*-
4  * Copyright (C) 1998   Tsubai Masanari
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include <machine/asm.h>
33
34 .extern _GLOBAL_OFFSET_TABLE_
35 .extern _DYNAMIC
36
37 _ENTRY(.rtld_start)
38         stwu    %r1,-48(%r1)    /* 16-byte aligned stack for reg saves +
39                                 exit_proc & obj _rtld args +
40                                 backchain & lrsave stack frame */
41         stw     %r3,16(%r1)     /*  argc */
42         stw     %r4,20(%r1)     /*  argv */
43         stw     %r5,24(%r1)     /*  envp */
44 /*      stw     %r6,28(%r1)   *//*  obj (always 0) */
45 /*      stw     %r7,32(%r1)   *//*  cleanup (always 0) */
46         stw     %r8,36(%r1)     /*  ps_strings */
47
48         /*
49          * Perform initial relocation of ld-elf.so. Not as easy as it
50          * sounds.
51          *  - perform small forward branch to put PC into link reg
52          *  - use link-time constants to determine offset to the
53          *    _DYNAMIC section and the GOT. Add these to the PC to
54          *    convert to absolute addresses.
55          *  - read GOT[0], which is the SVR4 ABI-specified link-time
56          *    value of _DYNAMIC. Subtract this value from the absolute
57          *    value to determine the load address
58          *  - call reloc_non_plt_self() to fix up ld-elf.so's relocations
59          */
60         bcl     20,31,1f
61 1:      mflr    %r30
62         mr      %r3,%r30                # save for _DYNAMIC
63         addis   %r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@ha
64         addi    %r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@l
65         addis   %r3,%r3,_DYNAMIC-1b@ha  # get _DYNAMIC actual address
66         addi    %r3,%r3,_DYNAMIC-1b@l
67         lwz     %r28,0(%r30)            # get base-relative &_DYNAMIC
68         sub     %r28,%r3,%r28           # r28 = relocbase
69         mr      %r4,%r28                # r4 = relocbase
70         bl      reloc_non_plt_self /* reloc_non_plt_self(&_DYNAMIC,base) */
71
72         /*
73          * The _rtld() function likes to see a stack layout containing
74          * { argc, argv[0], argv[1] ... argv[N], 0, env[0], ... , env[N] }
75          * Since the PowerPC stack was 16-byte aligned at exec time, the
76          * original stack layout has to be found by moving back a word
77          * from the argv pointer.
78          */
79         lwz     %r4,20(%r1)     /* restore argv */
80         addi    %r3,%r4,-4      /* locate argc ptr, &argv[-1] */
81
82         addi    %r4,%r1,8       /* &exit_proc on stack */
83         addi    %r5,%r1,12      /* &obj_main on stack */
84
85         bl      _rtld           /* &_start = _rtld(sp, &exit_proc, &obj_main)*/
86         mtlr    %r3
87
88         /*
89          * Restore args, with new obj/exit proc
90          */
91         lwz     %r3,16(%r1)     /* argc */
92         lwz     %r4,20(%r1)     /* argv */
93         lwz     %r5,24(%r1)     /* envp */
94         lwz     %r6,12(%r1)     /* obj */
95         lwz     %r7,8(%r1)      /* exit proc */
96         lwz     %r8,36(%r1)     /* ps_strings */
97         addi    %r1,%r1,48      /* restore original stackptr */
98
99         blrl    /* _start(argc, argv, envp, obj, cleanup, ps_strings) */
100
101         li      %r0,1           /* _exit() */
102         sc
103
104 /*
105  * _rtld_bind_secureplt_start()
106  *
107  * Call into the MI binder (Secure-PLT stub).
108  * secure-plt expects %r11 to be the offset to the rela entry.
109  * bss-plt expects %r11 to be index of the rela entry.
110  * So for bss-plt, we multiply the index by 12 to get the offset.
111  */
112 _ENTRY(_rtld_bind_secureplt_start)
113         stwu    %r1,-160(%r1)           # stack space for 29 regs + r0/lr/cr
114         stw     %r0,20(%r1)             # save r0
115
116         /*
117          * Instead of division which is costly we will use multiplicative
118          * inverse.  a / n = ((a * inv(n)) >> 32)
119          * where inv(n) = (0x100000000 + n - 1) / n
120          */
121         mr      %r0,%r11
122         lis     %r11,0x15555556@h       # load multiplicative inverse of 12
123         ori     %r11,%r11,0x15555556@l
124         mulhwu  %r11,%r11,%r0           # get high half of multiplication
125         b       1f
126
127 /*
128  * _rtld_bind_start()
129  *
130  * Call into the MI binder. This routine is reached via the PLT call cell,
131  * and then _rtld_powerpc_pltresolve().
132  * On entry, %r11 contains the index of the PLT cell, and %r12 contains
133  * a pointer to the ELF object for the file.
134  *  Save all registers, call into the binder to resolve and fixup the external
135  * routine, and then transfer to the external routine on return.
136  */
137         .globl  _rtld_bind
138
139 _ENTRY(_rtld_bind_start)
140         stwu    %r1,-160(%r1)           # stack space for 29 regs + r0/lr/cr
141         stw     %r0,20(%r1)             # save r0
142 1:
143         mflr    %r0
144         stw     %r0,16(%r1)             # save lr
145         mfcr    %r0
146         stw     %r0,12(%r1)             # save cr
147         stmw    %r3,24(%r1)             # save r3-r31
148
149         mr      %r3,%r12                # obj
150         mulli   %r4,%r11,12             # rela index * sizeof(Elf_Rela)
151         bl      _rtld_bind              # target addr = _rtld_bind(obj, reloff)
152         mtctr   %r3                     # move absolute target addr into ctr
153
154         lmw     %r3,24(%r1)             # restore r3-r31
155         lwz     %r0,12(%r1)             # restore cr
156         mtcr    %r0
157         lwz     %r0,16(%r1)             # restore lr
158         mtlr    %r0
159         lwz     %r0,20(%r1)             # restore r0
160
161         addi    %r1,%r1,160             # restore stack
162         bctr                            # jump to target
163
164
165 /*
166  * _rtld_powerpc_pltresolve()
167  *
168  *  This routine is copied into the latter part of the 72-byte reserved
169  * area at the start of the PLT. The absolute address of the _rtld_bind_start
170  * routine, and the ELF object for the loaded file, are inserted into
171  * the code by the reloc.c:init_pltgot() routine.
172  *  The first time an external routine is called, the PLT slot will
173  * set up %r11 to the offset of the slot, and will jump to this routine.
174  * The ELF object is shifted into %r11, and _rtld_bind_start is called
175  * to complete the binding.
176  */
177 _ENTRY(_rtld_powerpc_pltlongresolve)
178         lis     %r12,0                  # lis   12,jmptab@ha
179         addi    %r12,%r12,0             # addi  12,12,jmptab@l
180         subf    %r11,%r12,%r11          # reloff
181         li      %r12,2
182         srw     %r11,%r11,%r12          # index = reloff/sizeof(Elf_Addr)
183 _ENTRY(_rtld_powerpc_pltresolve)
184         lis     %r12,0                  # lis   12,_rtld_bind_start@ha
185         addi    %r12,%r12,0             # addi  12,12,_rtld_bind_start@l
186         mtctr   %r12
187         lis     %r12,0                  # lis   12,obj@ha
188         addi    %r12,%r12,0             # addi  12,12,obj@l
189         bctr
190
191 /*
192  * _rtld_powerpc_pltcall()
193  *
194  *  This routine is copied into the 72-byte reserved area at the
195  * start of the PLT. The reloc.c:init_pltgot() routine inserts
196  * the absolute address of the jumptable.
197  *  Control is transferred to this routine when the binder has
198  * located the external routine, but determined that it is > 32Mb
199  * from the PLT slot. Code is inserted into the PLT slot to set up
200  * %r11 with the jumptable index, and jump to here, where the
201  * absolute address of the external routine is loaded from the
202  * jumptable and transferred to
203  */
204 _ENTRY(_rtld_powerpc_pltcall)
205         slwi    %r11,%r11,2             # jmptab offset = index * 4
206         addis   %r11,%r11,0             # addis 11,11,jmptab@ha
207         lwz     %r11,0(%r11)            # lwz   11,jmptab@l(11)
208         mtctr   %r11
209         bctr                            # (*jmptab[index])()
210
211         .section .note.GNU-stack,"",%progbits