1/*	$OpenBSD: ldasm.S,v 1.42 2018/11/22 21:37:30 guenther Exp $ */
2
3/*
4 * Copyright (c) 2001 Niklas Hallqvist
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 * Copyright 1996 Matt Thomas <matt@3am-software.com>
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 *    notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 *    notice, this list of conditions and the following disclaimer in the
39 *    documentation and/or other materials provided with the distribution.
40 * 3. The name of the author may not be used to endorse or promote products
41 *    derived from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
47 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 */
54
55#include <machine/asm.h>
56
57#define AUX_entry 9
58
59/* Not really a leaf... but we are special.  */
60LEAF_NOPROFILE(_dl_start, 0)
61	.set	noreorder
62	br	pv, L1
63L1:
64	LDGP(pv)
65
66	mov	a0, s0		/* save arg */
67
68	/* relocate ourself. */
69	br	s2, L2		/* get our PC */
70L2:	ldiq	s3, L2		/* get where the linker thought we were */
71
72	subq	s2, s3, s2
73	lda	t5, _DYNAMIC
74	addq	s2, t5, s6
75
76	/* allocate stack */
77	lda	sp, -((AUX_entry + 1) * 8)(sp)
78
79	mov	s0, a0
80	mov	s2, s1		/* relocation displacement */
81	ldq	a2, 0(a0)	/* argc */
82	lda	a3, 8(a0)	/* argv */
83	mov	a3, s3
84	lda	t3, 1(a2)
85	sll	t3, 3, t3
86	addq	a3, t3, a4	/* envp */
87	mov	a4, s4
88	mov	a5, s5
89	lda	s2, 0(sp)
90	mov	s2, a1
91	mov	s6, a2		/* &_DYNAMIC */
92	/* we can't use CALL here as gp would not be set correctly by ldgp,
93	   due to the GOT not being relocated yet. */
94	lda	pv, _dl_boot_bind
95	addq	s1, pv, pv
96	bsr	ra, _dl_boot_bind
97	mov	s3, a0		/* **argv  */
98	mov	s4, a1		/* **envp  */
99	mov	s1, a2		/* loff    */
100	mov	s2, a3		/* dl_data */
101	CALL(_dl_boot)
102	mov	s0, a0		/* stack */
103	lda	a1, _dl_dtors	/* cleanup */
104	mov	v0, pv
105	jsr	ra, (pv)
106END(_dl_start)
107
108/*
109 * Lazy binding entry point, called via secure (read-only) PLT.
110 */
111NESTED_NOPROFILE(_dl_bind_start, 0, 160, ra, 0, 0)
112	.set	noat
113	/* at_reg and t11 already used by PLT code. */
114
115	/*
116	 * Allocate stack frame and preserve all registers that the caller
117	 * would have normally saved themselves.
118	 */
119	lda	sp, -160(sp)
120	stq	ra, 0(sp)
121	stq	v0, 8(sp)
122	stq	t0, 16(sp)
123	stq	t1, 24(sp)
124	stq	t2, 32(sp)
125	stq	t3, 40(sp)
126	stq	t4, 48(sp)
127	stq	t5, 56(sp)
128	stq	t6, 64(sp)
129	stq	t7, 72(sp)
130	stq	a0, 80(sp)
131	stq	a1, 88(sp)
132	stq	a2, 96(sp)
133	stq	a3, 104(sp)
134	stq	a4, 112(sp)
135	stq	a5, 120(sp)
136	stq	t8, 128(sp)
137	stq	t9, 136(sp)
138	stq	t10, 144(sp)
139	stq	gp, 152(sp)
140
141	/*
142	 * Load our global pointer.  Note, can't use pv, since it is
143	 * already used by the PLT code.
144	 */
145	br	t0, 1f
1461:	LDGP(t0)
147
148	/* Set up the arguments for _dl_bind. */
149	mov	at_reg, a0	/* object */
150	mov	t11, a1		/* reloff as computed by the plt resolver */
151	CALL(_dl_bind)
152
153	/* Move the destination address into position. */
154	mov	v0, pv
155
156	/* Restore program registers. */
157	ldq	ra, 0(sp)
158	ldq	v0, 8(sp)
159	ldq	t0, 16(sp)
160	ldq	t1, 24(sp)
161	ldq	t2, 32(sp)
162	ldq	t3, 40(sp)
163	ldq	t4, 48(sp)
164	ldq	t5, 56(sp)
165	ldq	t6, 64(sp)
166	ldq	t7, 72(sp)
167	ldq	a0, 80(sp)
168	ldq	a1, 88(sp)
169	ldq	a2, 96(sp)
170	ldq	a3, 104(sp)
171	ldq	a4, 112(sp)
172	ldq	a5, 120(sp)
173	ldq	t8, 128(sp)
174	ldq	t9, 136(sp)
175	ldq	t10, 144(sp)
176	ldq	gp, 152(sp)
177	/* XXX LDGP? */
178
179	/* Pop the stack frame and turn control to the destination. */
180	lda	sp, 160(sp)
181	jmp	zero, (pv)
182END(_dl_bind_start)
183