xref: /freebsd/libexec/rtld-elf/powerpc64/rtld_start.S (revision 8ae32158df8a25eabae596125d1c911ed5d4eb65)
127bd4146SNathan Whitehorn/*      $NetBSD: rtld_start.S,v 1.4 2001/09/26 04:06:43 mycroft Exp $   */
227bd4146SNathan Whitehorn
327bd4146SNathan Whitehorn/*-
427bd4146SNathan Whitehorn * Copyright (C) 1998   Tsubai Masanari
527bd4146SNathan Whitehorn * All rights reserved.
627bd4146SNathan Whitehorn *
727bd4146SNathan Whitehorn * Redistribution and use in source and binary forms, with or without
827bd4146SNathan Whitehorn * modification, are permitted provided that the following conditions
927bd4146SNathan Whitehorn * are met:
1027bd4146SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright
1127bd4146SNathan Whitehorn *    notice, this list of conditions and the following disclaimer.
1227bd4146SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright
1327bd4146SNathan Whitehorn *    notice, this list of conditions and the following disclaimer in the
1427bd4146SNathan Whitehorn *    documentation and/or other materials provided with the distribution.
1527bd4146SNathan Whitehorn * 3. The name of the author may not be used to endorse or promote products
1627bd4146SNathan Whitehorn *    derived from this software without specific prior written permission.
1727bd4146SNathan Whitehorn *
1827bd4146SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1927bd4146SNathan Whitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2027bd4146SNathan Whitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2127bd4146SNathan Whitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2227bd4146SNathan Whitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2327bd4146SNathan Whitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2427bd4146SNathan Whitehorn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2527bd4146SNathan Whitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2627bd4146SNathan Whitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2727bd4146SNathan Whitehorn * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2827bd4146SNathan Whitehorn *
2927bd4146SNathan Whitehorn * $FreeBSD$
3027bd4146SNathan Whitehorn */
3127bd4146SNathan Whitehorn
3227bd4146SNathan Whitehorn#include <machine/asm.h>
3327bd4146SNathan Whitehorn
3427bd4146SNathan Whitehorn.extern _GLOBAL_OFFSET_TABLE_
3527bd4146SNathan Whitehorn.extern _DYNAMIC
3627bd4146SNathan Whitehorn
3727bd4146SNathan Whitehorn_ENTRY(_rtld_start)
38071a51cfSNathan Whitehorn	stdu    %r1,-144(%r1)	/* 16-byte aligned stack for reg saves +
3927bd4146SNathan Whitehorn				exit_proc & obj _rtld args +
4027bd4146SNathan Whitehorn				backchain & lrsave stack frame */
41071a51cfSNathan Whitehorn	std     %r3,96(%r1)	/*  argc */
42071a51cfSNathan Whitehorn	std     %r4,104(%r1)	/*  argv */
43071a51cfSNathan Whitehorn	std     %r5,112(%r1)	/*  envp */
44071a51cfSNathan Whitehorn/*	std     %r6,120(%r1)   *//*  obj (always 0) */
45071a51cfSNathan Whitehorn/*	std     %r7,128(%r1)   *//*  cleanup (always 0) */
46071a51cfSNathan Whitehorn	std     %r8,136(%r1)	/*  ps_strings */
4727bd4146SNathan Whitehorn
4827bd4146SNathan Whitehorn	/*
4927bd4146SNathan Whitehorn	 * Perform initial relocation of ld-elf.so. Not as easy as it
5027bd4146SNathan Whitehorn	 * sounds.
5127bd4146SNathan Whitehorn	 *  - perform small forward branch to put PC into link reg
5227bd4146SNathan Whitehorn	 *  - use link-time constants to determine offset to the
5327bd4146SNathan Whitehorn	 *    _DYNAMIC section and the GOT. Add these to the PC to
5427bd4146SNathan Whitehorn	 *    convert to absolute addresses.
5527bd4146SNathan Whitehorn	 *  - call reloc_non_plt_self() to fix up ld-elf.so's relocations
5627bd4146SNathan Whitehorn	 */
5727bd4146SNathan Whitehorn
5827bd4146SNathan Whitehorn	bl	1f
5927bd4146SNathan Whitehorn	.llong	_DYNAMIC-.
6027bd4146SNathan Whitehorn1:
6127bd4146SNathan Whitehorn	mflr	%r3		/* PC value at .llong */
6227bd4146SNathan Whitehorn	ld	%r4,0(%r3)	/* offset to _DYNAMIC */
6327bd4146SNathan Whitehorn	add	%r3,%r4,%r3	/* r3 = &_DYNAMIC, absolute value */
6427bd4146SNathan Whitehorn
6527bd4146SNathan Whitehorn	ld	%r4,-0x8000(%r2) /* First TOC entry is TOC base */
6627bd4146SNathan Whitehorn	subf	%r4,%r4,%r2	/* Subtract from real TOC base to get base */
6727bd4146SNathan Whitehorn
6827bd4146SNathan Whitehorn	bl	.reloc_non_plt_self /* reloc_non_plt_self(&_DYNAMIC,base) */
6927bd4146SNathan Whitehorn	nop
7027bd4146SNathan Whitehorn
7127bd4146SNathan Whitehorn	/*
7227bd4146SNathan Whitehorn	 * The _rtld() function likes to see a stack layout containing
7327bd4146SNathan Whitehorn	 * { argc, argv[0], argv[1] ... argv[N], 0, env[0], ... , env[N] }
7427bd4146SNathan Whitehorn	 * Since the PowerPC stack was 16-byte aligned at exec time, the
7527bd4146SNathan Whitehorn	 * original stack layout has to be found by moving back a word
7627bd4146SNathan Whitehorn	 * from the argv pointer.
7727bd4146SNathan Whitehorn	 */
78071a51cfSNathan Whitehorn	ld	%r4,104(%r1)
7927bd4146SNathan Whitehorn        addi    %r3,%r4,-8	/* locate argc ptr, &argv[-1] */
80071a51cfSNathan Whitehorn	addi	%r4,%r1,128	/* &exit_proc on stack */
81071a51cfSNathan Whitehorn	addi	%r5,%r1,120	/* &obj_main on stack */
8227bd4146SNathan Whitehorn
8327bd4146SNathan Whitehorn	bl      ._rtld		/* &_start = _rtld(sp, &exit_proc, &obj_main)*/
8427bd4146SNathan Whitehorn	nop
8527bd4146SNathan Whitehorn	ld	%r2,8(%r3)
8627bd4146SNathan Whitehorn	ld	%r11,16(%r3)
8727bd4146SNathan Whitehorn	ld	%r3,0(%r3)
8827bd4146SNathan Whitehorn	mtlr    %r3
8927bd4146SNathan Whitehorn
9027bd4146SNathan Whitehorn	/*
9127bd4146SNathan Whitehorn	 * Restore args, with new obj/exit proc
9227bd4146SNathan Whitehorn	 */
93071a51cfSNathan Whitehorn	ld      %r3,96(%r1)     /* argc */
94071a51cfSNathan Whitehorn	ld      %r4,104(%r1)	/* argv */
95071a51cfSNathan Whitehorn	ld      %r5,112(%r1)	/* envp */
96071a51cfSNathan Whitehorn	ld      %r6,120(%r1)	/* obj */
97071a51cfSNathan Whitehorn	ld      %r7,128(%r1)	/* exit proc */
98071a51cfSNathan Whitehorn	ld      %r8,136(%r1)	/* ps_strings */
9927bd4146SNathan Whitehorn
10027bd4146SNathan Whitehorn	blrl	/* _start(argc, argv, envp, obj, cleanup, ps_strings) */
10127bd4146SNathan Whitehorn
10227bd4146SNathan Whitehorn	li      %r0,1		/* _exit() */
10327bd4146SNathan Whitehorn	sc
10427bd4146SNathan Whitehorn
10527bd4146SNathan Whitehorn/*
10627bd4146SNathan Whitehorn * _rtld_bind_start()
10727bd4146SNathan Whitehorn *
10827bd4146SNathan Whitehorn * Call into the MI binder. This routine is reached via the PLT call cell
10927bd4146SNathan Whitehorn * On entry, %r11 contains a pointer to the (object, relocation) tuple.
11027bd4146SNathan Whitehorn *
11127bd4146SNathan Whitehorn * Save all registers, call into the binder to resolve and fixup the external
11227bd4146SNathan Whitehorn * routine, and then transfer to the external routine on return.
11327bd4146SNathan Whitehorn */
11427bd4146SNathan Whitehorn	.globl  _rtld_bind
11527bd4146SNathan Whitehorn
11627bd4146SNathan Whitehorn_ENTRY(_rtld_bind_start)
11727bd4146SNathan Whitehorn	mflr    %r0
11827bd4146SNathan Whitehorn	std     %r0,16(%r1)		# save lr
11927bd4146SNathan Whitehorn	mfcr    %r0
12027bd4146SNathan Whitehorn	std     %r0,8(%r1)		# save cr
12127bd4146SNathan Whitehorn
122071a51cfSNathan Whitehorn	stdu    %r1,-48-12*8(%r1)	# stack space for 8 regs + header
123071a51cfSNathan Whitehorn					#   + 2 save regs
124071a51cfSNathan Whitehorn	std	%r3,64+0*8(%r1)		# save r3-r31
125071a51cfSNathan Whitehorn	std	%r4,64+1*8(%r1)
126071a51cfSNathan Whitehorn	std	%r5,64+2*8(%r1)
127071a51cfSNathan Whitehorn	std	%r6,64+3*8(%r1)
128071a51cfSNathan Whitehorn	std	%r7,64+4*8(%r1)
129071a51cfSNathan Whitehorn	std	%r8,64+5*8(%r1)
130071a51cfSNathan Whitehorn	std	%r9,64+6*8(%r1)
131071a51cfSNathan Whitehorn	std	%r10,64+7*8(%r1)
132071a51cfSNathan Whitehorn	std	%r12,64+8*8(%r1)
13327bd4146SNathan Whitehorn
13427bd4146SNathan Whitehorn	ld	%r3,0(%r11)
13527bd4146SNathan Whitehorn	ld	%r4,8(%r11)
13627bd4146SNathan Whitehorn	bl      ._rtld_bind		# target addr = _rtld_bind(obj, reloff)
13727bd4146SNathan Whitehorn	nop
13827bd4146SNathan Whitehorn
13927bd4146SNathan Whitehorn	ld	%r2,8(%r3)
14027bd4146SNathan Whitehorn	ld	%r11,16(%r3)
14127bd4146SNathan Whitehorn	ld	%r3,0(%r3)
14227bd4146SNathan Whitehorn	mtctr   %r3			# move absolute target addr into ctr
14327bd4146SNathan Whitehorn
144071a51cfSNathan Whitehorn	ld	%r3,64+0*8(%r1)		# restore r3-r31
145071a51cfSNathan Whitehorn	ld	%r4,64+1*8(%r1)
146071a51cfSNathan Whitehorn	ld	%r5,64+2*8(%r1)
147071a51cfSNathan Whitehorn	ld	%r6,64+3*8(%r1)
148071a51cfSNathan Whitehorn	ld	%r7,64+4*8(%r1)
149071a51cfSNathan Whitehorn	ld	%r8,64+5*8(%r1)
150071a51cfSNathan Whitehorn	ld	%r9,64+6*8(%r1)
151071a51cfSNathan Whitehorn	ld	%r10,64+7*8(%r1)
152071a51cfSNathan Whitehorn	ld	%r12,64+8*8(%r1)
15327bd4146SNathan Whitehorn
154071a51cfSNathan Whitehorn	ld	%r1,0(%r1)		# restore stack
15527bd4146SNathan Whitehorn	ld	%r0,8(%r1)		# restore cr
15627bd4146SNathan Whitehorn	mtcr    %r0
15727bd4146SNathan Whitehorn	ld	%r0,16(%r1)		# restore lr
15827bd4146SNathan Whitehorn	mtlr    %r0
15927bd4146SNathan Whitehorn
16027bd4146SNathan Whitehorn	bctr				# jump to target
16127bd4146SNathan Whitehorn
162*8ae32158SKonstantin Belousov	.section .note.GNU-stack,"",%progbits
163