xref: /openbsd/libexec/ld.so/powerpc64/ldasm.S (revision af856e3a)
1/*	$OpenBSD: ldasm.S,v 1.3 2020/07/16 21:18:09 kettenis Exp $ */
2
3/*
4 * Copyright (c) 1999 Dale Rahn
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29#define AUX_entry 9
30
31#include <machine/asm.h>
32#include <sys/syscall.h>
33
34	.section .boot.text,"ax",@progbits
35	.align	2
36	.globl	_dl_start
37	.type	_dl_start,@function
38_dl_start:
39	bl	1f
401:
41	mflr	%r30
42	addis   %r2, %r30, .TOC.-1b@ha
43	addi    %r2, %r2, .TOC.-1b@l
44
45	stdu	%r1, (-64 -((AUX_entry+3)*8))(%r1)	# Some space.
46
47	# squirrel away the arguments for main
48	mr	%r20, %r3	#argc
49	mr	%r21, %r4	#argv
50	mr	%r22, %r5	#envp
51	mr	%r23, %r6	# ???
52	nop
53
54	addis	%r18, %r2,  _DYNAMIC@toc@ha
55	addi	%r18, %r18, _DYNAMIC@toc@l
56
57	ld	%r4,  -0x8000(%r2) # First entry of TOC is TOC base
58	sub	%r4,  %r2, %r4	# determine load offset
59
60	mr	%r17, %r4		# save for _dl_boot
61
62	subi	%r3, %r21, 8	# Get stack pointer (arg0 for _dl_boot).
63	addi	%r4, %r1, 32		# dl_data
64	mr	%r5, %r18		# dynamicp
65
66	bl	_dl_boot_bind
67
68	mr	%r3, %r21		# argv
69	mr	%r4, %r22		# envp
70	mr	%r5, %r17		# loff
71	addi	%r6, %r1, 32		# dl_data
72
73	bl	_dl_boot
74
75	mtctr	%r3		# put return value into ctr to execute
76
77	# get back the squirreled away the arguments for main
78	mr	%r3, %r20
79	mr	%r4, %r21
80	mr	%r5, %r22
81	mr	%r6, %r23
82
83	addis	%r7, %r2, _dl_dtors@toc@ha
84	addi	%r7, %r7, _dl_dtors@toc@l
85
86	mtlr	%r27
87	ld	%r1, 0(%r1)		# Restore stack pointer.
88	bctr			# Go execute the 'real' program.
89END(_dl_start)
90
91ENTRY(_dl_bind_start)
92	# r0 contains offset, do not overwrite
93	# r2 ld.so toc is loaded on entry to this function.
94	mflr	%r12
95	std	%r12,16(%r1)		# save	lr
96
97	stdu	1,-104(%r1)
98
99	std	%r3,32(%r1)		# save	r3-r10, C calling convention
100	std	%r4,40(%r1)		# r13 - r31 are preserved by called code
101	std	%r5,48(%r1)
102	std	%r6,56(%r1)
103	std	%r7,64(%r1)
104	std	%r8,72(%r1)
105	std	%r9,80(%r1)
106	std	%r10,88(%r1)
107
108	mr	%r3,%r11		# obj
109	mr	%r4,%r0			# relidx
110	bl	_dl_bind		# _rtld_bind(obj, relidx)
111	mtctr	%r3
112	mr	%r12, %r3
113
114	ld	%r3,32(%r1)
115	ld	%r4,40(%r1)
116	ld	%r5,48(%r1)
117	ld	%r6,56(%r1)
118	ld	%r7,64(%r1)
119	ld	%r8,72(%r1)
120	ld	%r9,80(%r1)
121	ld	%r10,88(%r1)
122
123	addi	%r1,%r1,104
124
125	ld	%r0,16(%r1)		# restore lr
126	mtlr	%r0
127
128	bctr
129END(_dl_bind_start)
130