1292691Sbr/*-
2292691Sbr * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3292691Sbr * All rights reserved.
4292691Sbr *
5292691Sbr * This software was developed by SRI International and the University of
6292691Sbr * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7292691Sbr * ("CTSRD"), as part of the DARPA CRASH research programme.
8292691Sbr *
9292691Sbr * This software was developed by the University of Cambridge Computer
10292691Sbr * Laboratory as part of the CTSRD Project, with support from the UK Higher
11292691Sbr * Education Innovation Fund (HEIF).
12292691Sbr *
13292691Sbr * Redistribution and use in source and binary forms, with or without
14292691Sbr * modification, are permitted provided that the following conditions
15292691Sbr * are met:
16292691Sbr * 1. Redistributions of source code must retain the above copyright
17292691Sbr *    notice, this list of conditions and the following disclaimer.
18292691Sbr * 2. Redistributions in binary form must reproduce the above copyright
19292691Sbr *    notice, this list of conditions and the following disclaimer in the
20292691Sbr *    documentation and/or other materials provided with the distribution.
21292691Sbr *
22292691Sbr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23292691Sbr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24292691Sbr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25292691Sbr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26292691Sbr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27292691Sbr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28292691Sbr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29292691Sbr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30292691Sbr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31292691Sbr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32292691Sbr * SUCH DAMAGE.
33292691Sbr */
34292691Sbr
35292691Sbr#include <machine/asm.h>
36292691Sbr__FBSDID("$FreeBSD: releng/11.0/libexec/rtld-elf/riscv/rtld_start.S 292691 2015-12-24 15:47:51Z br $");
37292691Sbr
38292691Sbr/*
39292691Sbr * func_ptr_type
40292691Sbr * _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
41292691Sbr */
42292691Sbr
43292691SbrENTRY(.rtld_start)
44292691Sbr	mv	s0, a0		/* Put ps_strings in a callee-saved register */
45292691Sbr	mv	s1, sp		/* And the stack pointer */
46292691Sbr
47292691Sbr	addi	sp, sp, -16	/* Make room for obj_main & exit proc */
48292691Sbr
49292691Sbr	mv	a1, sp		/* exit_proc */
50292691Sbr	addi	a2, a1, 8	/* obj_main */
51292691Sbr	jal	_rtld		/* Call the loader */
52292691Sbr	mv	t0, a0		/* Backup the entry point */
53292691Sbr
54292691Sbr	ld	a2, 0(sp)	/* Load cleanup */
55292691Sbr	ld	a1, 8(sp)	/* Load obj_main */
56292691Sbr	mv	a0, s0		/* Restore ps_strings */
57292691Sbr	mv	sp, s1		/* Restore the stack pointer */
58292691Sbr	jr	t0		/* Jump to the entry point */
59292691SbrEND(.rtld_start)
60292691Sbr
61292691Sbr/*
62292691Sbr * t0 = obj pointer
63292691Sbr * t1 = reloc offset
64292691Sbr */
65292691SbrENTRY(_rtld_bind_start)
66292691Sbr	/* Save the arguments and ra */
67292691Sbr	addi	sp, sp, -(8 * 25)
68292691Sbr	sd	a0, (8 * 0)(sp)
69292691Sbr	sd	a1, (8 * 1)(sp)
70292691Sbr	sd	a2, (8 * 2)(sp)
71292691Sbr	sd	a3, (8 * 3)(sp)
72292691Sbr	sd	a4, (8 * 4)(sp)
73292691Sbr	sd	a5, (8 * 5)(sp)
74292691Sbr	sd	a6, (8 * 6)(sp)
75292691Sbr	sd	a7, (8 * 7)(sp)
76292691Sbr	sd	ra, (8 * 8)(sp)
77292691Sbr#if 0
78292691Sbr	/* RISCVTODO VFP */
79292691Sbr	/* Save any floating-point arguments */
80292691Sbr	fsq	fa0, (8 * 9)(sp)
81292691Sbr	fsq	fa1, (8 * 11)(sp)
82292691Sbr	fsq	fa2, (8 * 13)(sp)
83292691Sbr	fsq	fa3, (8 * 15)(sp)
84292691Sbr	fsq	fa4, (8 * 17)(sp)
85292691Sbr	fsq	fa5, (8 * 19)(sp)
86292691Sbr	fsq	fa6, (8 * 21)(sp)
87292691Sbr	fsq	fa7, (8 * 23)(sp)
88292691Sbr#endif
89292691Sbr
90292691Sbr	/* Reloc offset is 3x of the .got.plt offset */
91292691Sbr	slli	a1, t1, 1	/* Mult items by 2 */
92292691Sbr	add	a1, a1, t1	/* Plus item */
93292691Sbr
94292691Sbr	/* Load obj */
95292691Sbr	mv	a0, t0
96292691Sbr
97292691Sbr	/* Call into rtld */
98292691Sbr	jal	_rtld_bind
99292691Sbr
100292691Sbr	/* Backup the address to branch to */
101292691Sbr	mv	t0, a0
102292691Sbr
103292691Sbr	/* Restore the arguments and ra */
104292691Sbr	ld	a0, (8 * 0)(sp)
105292691Sbr	ld	a1, (8 * 1)(sp)
106292691Sbr	ld	a2, (8 * 2)(sp)
107292691Sbr	ld	a3, (8 * 3)(sp)
108292691Sbr	ld	a4, (8 * 4)(sp)
109292691Sbr	ld	a5, (8 * 5)(sp)
110292691Sbr	ld	a6, (8 * 6)(sp)
111292691Sbr	ld	a7, (8 * 7)(sp)
112292691Sbr	ld	ra, (8 * 8)(sp)
113292691Sbr#if 0
114292691Sbr	/* RISCVTODO VFP */
115292691Sbr	/* Restore floating-point arguments */
116292691Sbr	flq	fa0, (8 * 9)(sp)
117292691Sbr	flq	fa1, (8 * 11)(sp)
118292691Sbr	flq	fa2, (8 * 13)(sp)
119292691Sbr	flq	fa3, (8 * 15)(sp)
120292691Sbr	flq	fa4, (8 * 17)(sp)
121292691Sbr	flq	fa5, (8 * 19)(sp)
122292691Sbr	flq	fa6, (8 * 21)(sp)
123292691Sbr	flq	fa7, (8 * 23)(sp)
124292691Sbr#endif
125292691Sbr	addi	sp, sp, (8 * 25)
126292691Sbr
127292691Sbr	/* Call into the correct function */
128292691Sbr	jr	t0
129292691SbrEND(_rtld_bind_start)
130