ldasm.S revision 1.4
1/*	$OpenBSD: ldasm.S,v 1.4 2017/08/27 21:59:51 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2016 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 DL_DATA_SIZE	(16 * 8)	/* XXX */
30#include <machine/asm.h>
31#include <sys/syscall.h>
32
33ENTRY(_dl_start)
34	mov	x29, sp
35	// need to worry about alignment, I think not?
36	mov	x19, sp
37	sub	sp, sp, #8+8+DL_DATA_SIZE	// dl_data size
38	add	x20, sp, #4			// dl_data
39	mov	x21, lr				// save old lr
40
41	mov	x0, x29				// original stack
42	mov	x1, x20				// dl_data
43
44	adrp	x2, :got:_DYNAMIC		/* &_DYNAMIC */
45	add	x2, x2, #:lo12:_DYNAMIC
46
47	bl	_dl_boot_bind
48
49	add	x0, x29, #8			// argv
50	ldr	x1, [x29]			// load argc
51	add	x1, x0, x1, lsl #3		// envp = argv + argc * 8
52	add	x1, x1, #8			//                        + 8
53	ldr	x2, [x20, #7*8]			// loff from dl_data
54	mov	x3, x20				// dl_data
55	bl	_dl_boot
56
57	mov	sp, x29				// move stack back
58	mov	x29, xzr			// clear frame back pointer
59	mov	lr, xzr
60
61	adrp	x8, :got:_dl_dtors
62	ldr	x2, [x8, :got_lo12:_dl_dtors]
63
64	br	x0
65
66ENTRY(_dl_bind_start)
67	/*
68	 * ip is pointer to got entry for this relocation
69	 * lr is pointer to pltgot[2], which is entry -1 of got plt reloc.
70	 * return address is on stack
71	 */
72	stp	x29, x30, [sp, #-160]!
73	stp	 x0,  x1, [sp,#16]
74	stp	 x2,  x3, [sp,#32]
75	stp	 x4,  x5, [sp,#48]
76	stp	 x6,  x7, [sp,#64]
77	stp	 x8,  x9, [sp,#80]
78	stp	x10, x11, [sp,#96]
79	stp	x12, x13, [sp,#112]
80	stp	x14, x15, [sp,#128]
81	str	x18,      [sp,#144]
82
83	mov	x1, x16		// reladdr
84
85
86	ldp	 x0,  x1, [sp,#16]
87	ldp	 x2,  x3, [sp,#32]
88	ldp	 x4,  x5, [sp,#48]
89	ldp	 x6,  x7, [sp,#64]
90	ldp	 x8,  x9, [sp,#80]
91	ldp	x10, x11, [sp,#96]
92	ldp	x12, x13, [sp,#112]
93	ldp	x14, x15, [sp,#128]
94	ldr	x18,      [sp,#144]
95	ldp	x29, x30, [sp], #-160
96	br x16
97
98
99#if 0
100	stmdb	sp!, {r0-r4,sl,fp}
101
102	sub	r1, ip, lr		/* r1 = 4 * (n + 1) */
103	sub	r1, r1, #4		/* r1 = 4 * n */
104	mov	r1, r1, lsr #2		/* r1 = n */
105
106	ldr	r0, [lr, #-4]
107	bl	_dl_bind
108	mov	ip, r0
109	ldmia	sp!, {r0-r4,sl,fp,lr}
110	mov	pc, ip
111#endif
112
113ENTRY(_rtld_tlsdesc)
114	ldr	x0, [x0, #8]
115	ret
116