xref: /freebsd/libexec/rtld-elf/powerpc/rtld_start.S (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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#include <machine/spr.h>	/* For SPR_SPEFSCR if needed. */
34
35.extern _GLOBAL_OFFSET_TABLE_
36.extern _DYNAMIC
37
38_ENTRY(.rtld_start)
39	stwu    %r1,-48(%r1)	/* 16-byte aligned stack for reg saves +
40				exit_proc & obj _rtld args +
41				backchain & lrsave stack frame */
42	stw     %r3,16(%r1)	/*  argc */
43	stw     %r4,20(%r1)	/*  argv */
44	stw     %r5,24(%r1)	/*  envp */
45/*	stw     %r6,28(%r1)   *//*  obj (always 0) */
46/*	stw     %r7,32(%r1)   *//*  cleanup (always 0) */
47	stw     %r8,36(%r1)	/*  ps_strings */
48
49	/*
50	 * Perform initial relocation of ld-elf.so. Not as easy as it
51	 * sounds.
52	 *  - perform small forward branch to put PC into link reg
53	 *  - use link-time constants to determine offset to the
54	 *    _DYNAMIC section and the GOT. Add these to the PC to
55	 *    convert to absolute addresses.
56	 *  - read GOT[0], which is the SVR4 ABI-specified link-time
57	 *    value of _DYNAMIC. Subtract this value from the absolute
58	 *    value to determine the load address
59	 *  - call reloc_non_plt_self() to fix up ld-elf.so's relocations
60	 */
61	bcl	20,31,1f
621:	mflr	%r30
63	mr	%r3,%r30		# save for _DYNAMIC
64	addis	%r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@ha
65	addi	%r30,%r30,_GLOBAL_OFFSET_TABLE_-1b@l
66	addis	%r3,%r3,_DYNAMIC-1b@ha	# get _DYNAMIC actual address
67	addi	%r3,%r3,_DYNAMIC-1b@l
68	lwz	%r28,0(%r30)		# get base-relative &_DYNAMIC
69	sub	%r28,%r3,%r28		# r28 = relocbase
70	mr	%r4,%r28		# r4 = relocbase
71	bl	reloc_non_plt_self /* reloc_non_plt_self(&_DYNAMIC,base) */
72
73	/*
74	 * The _rtld() function likes to see a stack layout containing
75	 * { argc, argv[0], argv[1] ... argv[N], 0, env[0], ... , env[N] }
76	 * Since the PowerPC stack was 16-byte aligned at exec time, the
77	 * original stack layout has to be found by moving back a word
78	 * from the argv pointer.
79	 */
80        lwz     %r4,20(%r1)	/* restore argv */
81        addi    %r3,%r4,-4	/* locate argc ptr, &argv[-1] */
82
83	addi	%r4,%r1,8	/* &exit_proc on stack */
84	addi	%r5,%r1,12	/* &obj_main on stack */
85
86	bl      _rtld		/* &_start = _rtld(sp, &exit_proc, &obj_main)*/
87	mtlr    %r3
88
89	/*
90	 * Restore args, with new obj/exit proc
91	 */
92	lwz     %r3,16(%r1)     /* argc */
93	lwz     %r4,20(%r1)	/* argv */
94	lwz     %r5,24(%r1)	/* envp */
95	lwz     %r6,12(%r1)	/* obj */
96	lwz     %r7,8(%r1)	/* exit proc */
97	lwz     %r8,36(%r1)	/* ps_strings */
98        addi    %r1,%r1,48	/* restore original stackptr */
99
100	blrl	/* _start(argc, argv, envp, obj, cleanup, ps_strings) */
101
102	li      %r0,1		/* _exit() */
103	sc
104_END(.rtld_start)
105
106#ifdef __SPE__
107/* stack space for 30 GPRs + SPEFSCR/ACC/lr/cr */
108#define	NREGS		31
109#define	GPRWIDTH	8
110#define	FUDGE		4	/* Fudge factor for alignment */
111#else
112/* stack space for 30 GPRs + lr/cr */
113#define	NREGS		30
114#define	GPRWIDTH	4
115#define	FUDGE		4
116#endif
117/* Stack frame needs the 12-byte ABI frame plus fudge factor. */
118#define	STACK_SIZE	(NREGS * GPRWIDTH + 4 * 2 + 12 + FUDGE)
119
120/*
121 * _rtld_bind_secureplt_start()
122 *
123 * Call into the MI binder (Secure-PLT stub).
124 * secure-plt expects %r11 to be the offset to the rela entry.
125 * bss-plt expects %r11 to be index of the rela entry.
126 * So for bss-plt, we multiply the index by 12 to get the offset.
127 */
128_ENTRY(_rtld_bind_secureplt_start)
129	stwu    %r1,-STACK_SIZE(%r1)
130#ifdef __SPE__
131	evstdd	%r0,24(%r1)
132#else
133	stw     %r0,20(%r1)		# save r0
134#endif
135
136	/*
137	 * Instead of division which is costly we will use multiplicative
138	 * inverse.  a / n = ((a * inv(n)) >> 32)
139	 * where inv(n) = (0x100000000 + n - 1) / n
140	 */
141	mr	%r0,%r11
142	lis	%r11,0x15555556@h	# load multiplicative inverse of 12
143	ori	%r11,%r11,0x15555556@l
144	mulhwu	%r11,%r11,%r0		# get high half of multiplication
145	b	1f
146_END(_rtld_bind_secureplt_start)
147
148/*
149 * _rtld_bind_start()
150 *
151 * Call into the MI binder. This routine is reached via the PLT call cell,
152 * and then _rtld_powerpc_pltresolve().
153 * On entry, %r11 contains the index of the PLT cell, and %r12 contains
154 * a pointer to the ELF object for the file.
155 *  Save all registers, call into the binder to resolve and fixup the external
156 * routine, and then transfer to the external routine on return.
157 */
158	.globl  _rtld_bind
159
160_ENTRY(_rtld_bind_start)
161	stwu    %r1,-STACK_SIZE(%r1)
162#ifdef __SPE__
163	evstdd	%r0,24(%r1)
164#else
165	stw     %r0,20(%r1)		# save r0
166#endif
1671:
168	mflr    %r0
169	stw     %r0,16(%r1)		# save lr
170	mfcr    %r0
171	stw     %r0,12(%r1)		# save cr
172#ifdef __SPE__
173	evstdd	%r3, 32(%r1)
174	evstdd	%r4, 40(%r1)
175	evstdd	%r5, 48(%r1)
176	evstdd	%r6, 56(%r1)
177	evstdd	%r7, 64(%r1)
178	evstdd	%r8, 72(%r1)
179	evstdd	%r9, 80(%r1)
180	evstdd	%r10, 88(%r1)
181	evstdd	%r11, 96(%r1)
182	evstdd	%r12, 104(%r1)
183	evstdd	%r13, 112(%r1)
184	evstdd	%r14, 120(%r1)
185	evstdd	%r15, 128(%r1)
186	evstdd	%r16, 136(%r1)
187	evstdd	%r17, 144(%r1)
188	evstdd	%r18, 152(%r1)
189	evstdd	%r19, 160(%r1)
190	evstdd	%r20, 168(%r1)
191	evstdd	%r21, 176(%r1)
192	evstdd	%r22, 184(%r1)
193	evstdd	%r23, 192(%r1)
194	evstdd	%r24, 200(%r1)
195	evstdd	%r25, 208(%r1)
196	evstdd	%r26, 216(%r1)
197	evstdd	%r27, 224(%r1)
198	evstdd	%r28, 232(%r1)
199	evstdd	%r29, 240(%r1)
200	evstdd	%r30, 248(%r1)
201	li	%r3, 256
202	evstddx	%r31, %r1, %r3
203	evxor	%r0, %r0, %r0
204	li	%r3, 264
205	evmwumiaa	%r0, %r0, %r0
206	evstddx	%r0, %r1, %r3
207	mfspr	%r3, SPR_SPEFSCR
208	stw	%r3, 20(%r1)
209#else
210	stmw    %r3,24(%r1)		# save r3-r31
211#endif
212
213	mr      %r3,%r12		# obj
214	mulli   %r4,%r11,12		# rela index * sizeof(Elf_Rela)
215	bl      _rtld_bind		# target addr = _rtld_bind(obj, reloff)
216	mtctr   %r3			# move absolute target addr into ctr
217
218#ifdef __SPE__
219	lwz	%r3, 20(%r1)
220	mtspr	SPR_SPEFSCR, %r3
221	li	%r3, 264
222	evlddx	%r0, %r3, %r1
223	evmra	%r0, %r0
224	evldd	%r3, 32(%r1)
225	evldd	%r4, 40(%r1)
226	evldd	%r5, 48(%r1)
227	evldd	%r6, 56(%r1)
228	evldd	%r7, 64(%r1)
229	evldd	%r8, 72(%r1)
230	evldd	%r9, 80(%r1)
231	evldd	%r10, 88(%r1)
232	evldd	%r11, 96(%r1)
233	evldd	%r12, 104(%r1)
234	evldd	%r13, 112(%r1)
235	evldd	%r14, 120(%r1)
236	evldd	%r15, 128(%r1)
237	evldd	%r16, 136(%r1)
238	evldd	%r17, 144(%r1)
239	evldd	%r18, 152(%r1)
240	evldd	%r19, 160(%r1)
241	evldd	%r20, 168(%r1)
242	evldd	%r21, 176(%r1)
243	evldd	%r22, 184(%r1)
244	evldd	%r23, 192(%r1)
245	evldd	%r24, 200(%r1)
246	evldd	%r25, 208(%r1)
247	evldd	%r26, 216(%r1)
248	evldd	%r27, 224(%r1)
249	evldd	%r28, 232(%r1)
250	evldd	%r29, 240(%r1)
251	evldd	%r30, 248(%r1)
252	li	%r0, 256
253	evlddx	%r31, %r1, %r0
254#else
255        lmw     %r3,24(%r1)		# restore r3-r31
256#endif
257        lwz     %r0,12(%r1)		# restore cr
258        mtcr    %r0
259        lwz     %r0,16(%r1)		# restore lr
260        mtlr    %r0
261#ifdef __SPE__
262	evldd	%r0,24(%r1)
263#else
264        lwz     %r0,20(%r1)		# restore r0
265#endif
266
267        addi    %r1,%r1,STACK_SIZE	# restore stack
268        bctr				# jump to target
269_END(_rtld_bind_start)
270
271
272/*
273 * _rtld_powerpc_pltresolve()
274 *
275 *  This routine is copied into the latter part of the 72-byte reserved
276 * area at the start of the PLT. The absolute address of the _rtld_bind_start
277 * routine, and the ELF object for the loaded file, are inserted into
278 * the code by the reloc.c:init_pltgot() routine.
279 *  The first time an external routine is called, the PLT slot will
280 * set up %r11 to the offset of the slot, and will jump to this routine.
281 * The ELF object is shifted into %r11, and _rtld_bind_start is called
282 * to complete the binding.
283 */
284_ENTRY(_rtld_powerpc_pltlongresolve)
285	lis	%r12,0			# lis	12,jmptab@ha
286	addi    %r12,%r12,0		# addi  12,12,jmptab@l
287	subf	%r11,%r12,%r11		# reloff
288	li	%r12,2
289	srw	%r11,%r11,%r12		# index = reloff/sizeof(Elf_Addr)
290_END(_rtld_powerpc_pltlongresolve)
291_ENTRY(_rtld_powerpc_pltresolve)
292        lis     %r12,0			# lis   12,_rtld_bind_start@ha
293        addi    %r12,%r12,0		# addi  12,12,_rtld_bind_start@l
294        mtctr   %r12
295        lis     %r12,0			# lis   12,obj@ha
296        addi    %r12,%r12,0		# addi  12,12,obj@l
297        bctr
298_END(_rtld_powerpc_pltresolve)
299
300/*
301 * _rtld_powerpc_pltcall()
302 *
303 *  This routine is copied into the 72-byte reserved area at the
304 * start of the PLT. The reloc.c:init_pltgot() routine inserts
305 * the absolute address of the jumptable.
306 *  Control is transferred to this routine when the binder has
307 * located the external routine, but determined that it is > 32Mb
308 * from the PLT slot. Code is inserted into the PLT slot to set up
309 * %r11 with the jumptable index, and jump to here, where the
310 * absolute address of the external routine is loaded from the
311 * jumptable and transferred to
312 */
313_ENTRY(_rtld_powerpc_pltcall)
314        slwi    %r11,%r11,2		# jmptab offset = index * 4
315        addis   %r11,%r11,0		# addis 11,11,jmptab@ha
316        lwz     %r11,0(%r11)		# lwz   11,jmptab@l(11)
317        mtctr   %r11
318        bctr				# (*jmptab[index])()
319_END(_rtld_powerpc_pltcall)
320
321	.section .note.GNU-stack,"",%progbits
322