xref: /freebsd/libexec/rtld-elf/riscv/rtld_start.S (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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	mv	s0, a0		/* Put ps_strings in a callee-saved register */
43	mv	s1, sp		/* And the stack pointer */
44
45	addi	sp, sp, -16	/* Make room for obj_main & exit proc */
46
47	mv	a1, sp		/* exit_proc */
48	addi	a2, a1, 8	/* obj_main */
49	jal	_rtld		/* Call the loader */
50	mv	t0, a0		/* Backup the entry point */
51
52	ld	a2, 0(sp)	/* Load cleanup */
53	ld	a1, 8(sp)	/* Load obj_main */
54	mv	a0, s0		/* Restore ps_strings */
55	mv	sp, s1		/* Restore the stack pointer */
56	jr	t0		/* Jump to the entry point */
57END(.rtld_start)
58
59/*
60 * t0 = obj pointer
61 * t1 = reloc offset
62 */
63ENTRY(_rtld_bind_start)
64	/* Save the arguments and ra */
65	/* We require 17 dwords, but the stack must be aligned to 16-bytes */
66	addi	sp, sp, -(8 * 18)
67	sd	a0, (8 * 0)(sp)
68	sd	a1, (8 * 1)(sp)
69	sd	a2, (8 * 2)(sp)
70	sd	a3, (8 * 3)(sp)
71	sd	a4, (8 * 4)(sp)
72	sd	a5, (8 * 5)(sp)
73	sd	a6, (8 * 6)(sp)
74	sd	a7, (8 * 7)(sp)
75	sd	ra, (8 * 8)(sp)
76
77#ifdef __riscv_float_abi_double
78	/* Save any floating-point arguments */
79	fsd	fa0, (8 * 9)(sp)
80	fsd	fa1, (8 * 10)(sp)
81	fsd	fa2, (8 * 11)(sp)
82	fsd	fa3, (8 * 12)(sp)
83	fsd	fa4, (8 * 13)(sp)
84	fsd	fa5, (8 * 14)(sp)
85	fsd	fa6, (8 * 15)(sp)
86	fsd	fa7, (8 * 16)(sp)
87#endif
88
89	/* Reloc offset is 3x of the .got.plt offset */
90	slli	a1, t1, 1	/* Mult items by 2 */
91	add	a1, a1, t1	/* Plus item */
92
93	/* Load obj */
94	mv	a0, t0
95
96	/* Call into rtld */
97	jal	_rtld_bind
98
99	/* Backup the address to branch to */
100	mv	t0, a0
101
102	/* Restore the arguments and ra */
103	ld	a0, (8 * 0)(sp)
104	ld	a1, (8 * 1)(sp)
105	ld	a2, (8 * 2)(sp)
106	ld	a3, (8 * 3)(sp)
107	ld	a4, (8 * 4)(sp)
108	ld	a5, (8 * 5)(sp)
109	ld	a6, (8 * 6)(sp)
110	ld	a7, (8 * 7)(sp)
111	ld	ra, (8 * 8)(sp)
112
113#ifdef __riscv_float_abi_double
114	/* Restore floating-point arguments */
115	fld	fa0, (8 * 9)(sp)
116	fld	fa1, (8 * 10)(sp)
117	fld	fa2, (8 * 11)(sp)
118	fld	fa3, (8 * 12)(sp)
119	fld	fa4, (8 * 13)(sp)
120	fld	fa5, (8 * 14)(sp)
121	fld	fa6, (8 * 15)(sp)
122	fld	fa7, (8 * 16)(sp)
123#endif
124	addi	sp, sp, (8 * 18)
125
126	/* Call into the correct function */
127	jr	t0
128END(_rtld_bind_start)
129