xref: /freebsd/libexec/rtld-elf/riscv/rtld_start.S (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
1/*-
2 * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
3 * All rights reserved.
4 *
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * This software was developed by the University of Cambridge Computer
10 * Laboratory as part of the CTSRD Project, with support from the UK Higher
11 * Education Innovation Fund (HEIF).
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <machine/asm.h>
36/*
37 * func_ptr_type
38 * _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
39 */
40
41ENTRY(.rtld_start)
42	.cfi_undefined ra	/* Do not attempt to unwind any further. */
43	mv	s0, a0		/* Put ps_strings in a callee-saved register */
44	mv	s1, sp		/* And the stack pointer */
45
46	addi	sp, sp, -16	/* Make room for obj_main & exit proc */
47
48	mv	a1, sp		/* exit_proc */
49	addi	a2, a1, 8	/* obj_main */
50	jal	_rtld		/* Call the loader */
51	mv	t0, a0		/* Backup the entry point */
52
53	ld	a2, 0(sp)	/* Load cleanup */
54	ld	a1, 8(sp)	/* Load obj_main */
55	mv	a0, s0		/* Restore ps_strings */
56	mv	sp, s1		/* Restore the stack pointer */
57	jr	t0		/* Jump to the entry point */
58END(.rtld_start)
59
60/*
61 * t0 = obj pointer
62 * t1 = reloc offset
63 */
64ENTRY(_rtld_bind_start)
65	/* Save the arguments and ra */
66	/* We require 17 dwords, but the stack must be aligned to 16-bytes */
67	addi	sp, sp, -(8 * 18)
68	sd	a0, (8 * 0)(sp)
69	sd	a1, (8 * 1)(sp)
70	sd	a2, (8 * 2)(sp)
71	sd	a3, (8 * 3)(sp)
72	sd	a4, (8 * 4)(sp)
73	sd	a5, (8 * 5)(sp)
74	sd	a6, (8 * 6)(sp)
75	sd	a7, (8 * 7)(sp)
76	sd	ra, (8 * 8)(sp)
77
78#ifdef __riscv_float_abi_double
79	/* Save any floating-point arguments */
80	fsd	fa0, (8 * 9)(sp)
81	fsd	fa1, (8 * 10)(sp)
82	fsd	fa2, (8 * 11)(sp)
83	fsd	fa3, (8 * 12)(sp)
84	fsd	fa4, (8 * 13)(sp)
85	fsd	fa5, (8 * 14)(sp)
86	fsd	fa6, (8 * 15)(sp)
87	fsd	fa7, (8 * 16)(sp)
88#endif
89
90	/* Reloc offset is 3x of the .got.plt offset */
91	slli	a1, t1, 1	/* Mult items by 2 */
92	add	a1, a1, t1	/* Plus item */
93
94	/* Load obj */
95	mv	a0, t0
96
97	/* Call into rtld */
98	jal	_rtld_bind
99
100	/* Backup the address to branch to */
101	mv	t0, a0
102
103	/* Restore the arguments and ra */
104	ld	a0, (8 * 0)(sp)
105	ld	a1, (8 * 1)(sp)
106	ld	a2, (8 * 2)(sp)
107	ld	a3, (8 * 3)(sp)
108	ld	a4, (8 * 4)(sp)
109	ld	a5, (8 * 5)(sp)
110	ld	a6, (8 * 6)(sp)
111	ld	a7, (8 * 7)(sp)
112	ld	ra, (8 * 8)(sp)
113
114#ifdef __riscv_float_abi_double
115	/* Restore floating-point arguments */
116	fld	fa0, (8 * 9)(sp)
117	fld	fa1, (8 * 10)(sp)
118	fld	fa2, (8 * 11)(sp)
119	fld	fa3, (8 * 12)(sp)
120	fld	fa4, (8 * 13)(sp)
121	fld	fa5, (8 * 14)(sp)
122	fld	fa6, (8 * 15)(sp)
123	fld	fa7, (8 * 16)(sp)
124#endif
125	addi	sp, sp, (8 * 18)
126
127	/* Call into the correct function */
128	jr	t0
129END(_rtld_bind_start)
130