xref: /freebsd/libexec/rtld-elf/riscv/rtld_start.S (revision 9768746b)
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	/* We require 17 dwords, but the stack must be aligned to 16-bytes */
68	addi	sp, sp, -(8 * 18)
69	sd	a0, (8 * 0)(sp)
70	sd	a1, (8 * 1)(sp)
71	sd	a2, (8 * 2)(sp)
72	sd	a3, (8 * 3)(sp)
73	sd	a4, (8 * 4)(sp)
74	sd	a5, (8 * 5)(sp)
75	sd	a6, (8 * 6)(sp)
76	sd	a7, (8 * 7)(sp)
77	sd	ra, (8 * 8)(sp)
78
79#ifdef __riscv_float_abi_double
80	/* Save any floating-point arguments */
81	fsd	fa0, (8 * 9)(sp)
82	fsd	fa1, (8 * 10)(sp)
83	fsd	fa2, (8 * 11)(sp)
84	fsd	fa3, (8 * 12)(sp)
85	fsd	fa4, (8 * 13)(sp)
86	fsd	fa5, (8 * 14)(sp)
87	fsd	fa6, (8 * 15)(sp)
88	fsd	fa7, (8 * 16)(sp)
89#endif
90
91	/* Reloc offset is 3x of the .got.plt offset */
92	slli	a1, t1, 1	/* Mult items by 2 */
93	add	a1, a1, t1	/* Plus item */
94
95	/* Load obj */
96	mv	a0, t0
97
98	/* Call into rtld */
99	jal	_rtld_bind
100
101	/* Backup the address to branch to */
102	mv	t0, a0
103
104	/* Restore the arguments and ra */
105	ld	a0, (8 * 0)(sp)
106	ld	a1, (8 * 1)(sp)
107	ld	a2, (8 * 2)(sp)
108	ld	a3, (8 * 3)(sp)
109	ld	a4, (8 * 4)(sp)
110	ld	a5, (8 * 5)(sp)
111	ld	a6, (8 * 6)(sp)
112	ld	a7, (8 * 7)(sp)
113	ld	ra, (8 * 8)(sp)
114
115#ifdef __riscv_float_abi_double
116	/* Restore floating-point arguments */
117	fld	fa0, (8 * 9)(sp)
118	fld	fa1, (8 * 10)(sp)
119	fld	fa2, (8 * 11)(sp)
120	fld	fa3, (8 * 12)(sp)
121	fld	fa4, (8 * 13)(sp)
122	fld	fa5, (8 * 14)(sp)
123	fld	fa6, (8 * 15)(sp)
124	fld	fa7, (8 * 16)(sp)
125#endif
126	addi	sp, sp, (8 * 18)
127
128	/* Call into the correct function */
129	jr	t0
130END(_rtld_bind_start)
131