xref: /freebsd/libexec/rtld-elf/riscv/rtld_start.S (revision 42249ef2)
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__FBSDID("$FreeBSD$");
37
38/*
39 * func_ptr_type
40 * _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
41 */
42
43ENTRY(.rtld_start)
44	mv	s0, a0		/* Put ps_strings in a callee-saved register */
45	mv	s1, sp		/* And the stack pointer */
46
47	addi	sp, sp, -16	/* Make room for obj_main & exit proc */
48
49	mv	a1, sp		/* exit_proc */
50	addi	a2, a1, 8	/* obj_main */
51	jal	_rtld		/* Call the loader */
52	mv	t0, a0		/* Backup the entry point */
53
54	ld	a2, 0(sp)	/* Load cleanup */
55	ld	a1, 8(sp)	/* Load obj_main */
56	mv	a0, s0		/* Restore ps_strings */
57	mv	sp, s1		/* Restore the stack pointer */
58	jr	t0		/* Jump to the entry point */
59END(.rtld_start)
60
61/*
62 * t0 = obj pointer
63 * t1 = reloc offset
64 */
65ENTRY(_rtld_bind_start)
66	/* Save the arguments and ra */
67	addi	sp, sp, -(8 * 17)
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 * 17)
126
127	/* Call into the correct function */
128	jr	t0
129END(_rtld_bind_start)
130