1/*-
2 * Copyright (c) 2014 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Andrew Turner under
6 * sponsorship from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <machine/asm.h>
31__FBSDID("$FreeBSD$");
32
33ENTRY(.rtld_start)
34	mov	x19, x0		/* Put ps_strings in a callee-saved register */
35	mov	x20, sp		/* And the stack pointer */
36
37	sub	sp, sp, #16	/* Make room for obj_main & exit proc */
38
39	mov	x1, sp		/* exit_proc */
40	add	x2, x1, #8	/* obj_main */
41	bl	_rtld		/* Call the loader */
42	mov	x8, x0		/* Backup the entry point */
43
44	ldr	x2, [sp]	/* Load cleanup */
45	ldr	x1, [sp, #8]	/* Load obj_main */
46	mov	x0, x19		/* Restore ps_strings */
47	mov	sp, x20		/* Restore the stack pointer */
48	br	x8		/* Jump to the entry point */
49END(.rtld_start)
50
51/*
52 * sp + 0 = &GOT[x + 3]
53 * sp + 8 = RA
54 * x16 = &GOT[2]
55 * x17 = &_rtld_bind_start
56 */
57ENTRY(_rtld_bind_start)
58	mov	x17, sp
59
60	/* Save the arguments */
61	stp	x0, x1, [sp, #-16]!
62	stp	x2, x3, [sp, #-16]!
63	stp	x4, x5, [sp, #-16]!
64	stp	x6, x7, [sp, #-16]!
65	stp	x8, xzr, [sp, #-16]!
66
67	/* Save any floating-point arguments */
68	stp	q0, q1, [sp, #-32]!
69	stp	q2, q3, [sp, #-32]!
70	stp	q4, q5, [sp, #-32]!
71	stp	q6, q7, [sp, #-32]!
72
73	/* Calculate reloff */
74	ldr	x2, [x17, #0]	/* Get the address of the entry */
75	sub	x1, x2, x16	/* Find its offset */
76	sub	x1, x1, #8	/* Adjust for x16 not being at offset 0 */
77	/* Each rela item has 3 entriesso we need reloff = 3 * index */
78	lsl	x3, x1, #1	/* x3 = 2 * offset */
79	add	x1, x1, x3	/* x1 = x3 + offset = 3 * offset */
80
81	/* Load obj */
82	ldr	x0, [x16, #-8]
83
84	/* Call into rtld */
85	bl	_rtld_bind
86
87	/* Restore the registers saved by the plt code */
88	ldp	xzr, x30, [sp, #(5 * 16 + 4 * 32)]
89
90	/* Backup the address to branch to */
91	mov	x16, x0
92
93	/* restore the arguments */
94	ldp	q6, q7, [sp], #32
95	ldp	q4, q5, [sp], #32
96	ldp	q2, q3, [sp], #32
97	ldp	q0, q1, [sp], #32
98	ldp	x8, xzr, [sp], #16
99	ldp	x6, x7, [sp], #16
100	ldp	x4, x5, [sp], #16
101	ldp	x2, x3, [sp], #16
102	ldp	x0, x1, [sp], #16
103	/* And the part of the stack the plt entry handled */
104	add	sp, sp, #16
105
106	/* Call into the correct function */
107	br	x16
108END(_rtld_bind_start)
109
110/*
111 * uint64_t _rtld_tlsdesc(struct tlsdesc *);
112 *
113 * struct tlsdesc {
114 *  uint64_t ptr;
115 *  uint64_t data;
116 * };
117 *
118 * Returns the data.
119 */
120ENTRY(_rtld_tlsdesc)
121	ldr	x0, [x0, #8]
122	ret
123END(_rtld_tlsdesc)
124
125/*
126 * uint64_t _rtld_tlsdesc_dynamic(struct tlsdesc *);
127 *
128 * TODO: We could lookup the saved index here to skip saving the entire stack.
129 */
130ENTRY(_rtld_tlsdesc_dynamic)
131	/* Store any registers we may use in rtld_tlsdesc_handle */
132	stp	x29, x30, [sp, #-(10 * 16)]!
133	mov	x29, sp
134	stp	x1, x2,   [sp, #(1 * 16)]
135	stp	x3, x4,   [sp, #(2 * 16)]
136	stp	x5, x6,   [sp, #(3 * 16)]
137	stp	x7, x8,   [sp, #(4 * 16)]
138	stp	x9, x10,  [sp, #(5 * 16)]
139	stp	x11, x12, [sp, #(6 * 16)]
140	stp	x13, x14, [sp, #(7 * 16)]
141	stp	x15, x16, [sp, #(8 * 16)]
142	stp	x17, x18, [sp, #(9 * 16)]
143
144	/* Find the tls offset */
145	ldr	x0, [x0, #8]
146	mov	x1, #1
147	bl	rtld_tlsdesc_handle
148
149	/* Restore the registers */
150	ldp	x17, x18, [sp, #(9 * 16)]
151	ldp	x15, x16, [sp, #(8 * 16)]
152	ldp	x13, x14, [sp, #(7 * 16)]
153	ldp	x11, x12, [sp, #(6 * 16)]
154	ldp	x9, x10,  [sp, #(5 * 16)]
155	ldp	x7, x8,   [sp, #(4 * 16)]
156	ldp	x5, x6,   [sp, #(3 * 16)]
157	ldp	x3, x4,   [sp, #(2 * 16)]
158	ldp	x1, x2,   [sp, #(1 * 16)]
159	ldp	x29, x30, [sp], #(10 * 16)
160
161	ret
162END(_rtld_tlsdesc_dynamic)
163