xref: /freebsd/libexec/rtld-elf/riscv/rtld_start.S (revision 04262ed78d2368a9e1f64755fb92a22872a20cac)
14b1859c0SRuslan Bukin/*-
2f00ebc42SRuslan Bukin * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
34b1859c0SRuslan Bukin * All rights reserved.
44b1859c0SRuslan Bukin *
54b1859c0SRuslan Bukin * This software was developed by SRI International and the University of
64b1859c0SRuslan Bukin * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
74b1859c0SRuslan Bukin * ("CTSRD"), as part of the DARPA CRASH research programme.
84b1859c0SRuslan Bukin *
94b1859c0SRuslan Bukin * This software was developed by the University of Cambridge Computer
104b1859c0SRuslan Bukin * Laboratory as part of the CTSRD Project, with support from the UK Higher
114b1859c0SRuslan Bukin * Education Innovation Fund (HEIF).
124b1859c0SRuslan Bukin *
134b1859c0SRuslan Bukin * Redistribution and use in source and binary forms, with or without
144b1859c0SRuslan Bukin * modification, are permitted provided that the following conditions
154b1859c0SRuslan Bukin * are met:
164b1859c0SRuslan Bukin * 1. Redistributions of source code must retain the above copyright
174b1859c0SRuslan Bukin *    notice, this list of conditions and the following disclaimer.
184b1859c0SRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright
194b1859c0SRuslan Bukin *    notice, this list of conditions and the following disclaimer in the
204b1859c0SRuslan Bukin *    documentation and/or other materials provided with the distribution.
214b1859c0SRuslan Bukin *
224b1859c0SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
234b1859c0SRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b1859c0SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b1859c0SRuslan Bukin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
264b1859c0SRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b1859c0SRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b1859c0SRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b1859c0SRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b1859c0SRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b1859c0SRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b1859c0SRuslan Bukin * SUCH DAMAGE.
334b1859c0SRuslan Bukin */
344b1859c0SRuslan Bukin
354b1859c0SRuslan Bukin#include <machine/asm.h>
364b1859c0SRuslan Bukin/*
374b1859c0SRuslan Bukin * func_ptr_type
384b1859c0SRuslan Bukin * _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp)
394b1859c0SRuslan Bukin */
404b1859c0SRuslan Bukin
414b1859c0SRuslan BukinENTRY(.rtld_start)
42*04262ed7SAlex Richardson	.cfi_undefined ra	/* Do not attempt to unwind any further. */
434b1859c0SRuslan Bukin	mv	s0, a0		/* Put ps_strings in a callee-saved register */
444b1859c0SRuslan Bukin	mv	s1, sp		/* And the stack pointer */
454b1859c0SRuslan Bukin
464b1859c0SRuslan Bukin	addi	sp, sp, -16	/* Make room for obj_main & exit proc */
474b1859c0SRuslan Bukin
484b1859c0SRuslan Bukin	mv	a1, sp		/* exit_proc */
494b1859c0SRuslan Bukin	addi	a2, a1, 8	/* obj_main */
504b1859c0SRuslan Bukin	jal	_rtld		/* Call the loader */
514b1859c0SRuslan Bukin	mv	t0, a0		/* Backup the entry point */
524b1859c0SRuslan Bukin
534b1859c0SRuslan Bukin	ld	a2, 0(sp)	/* Load cleanup */
544b1859c0SRuslan Bukin	ld	a1, 8(sp)	/* Load obj_main */
554b1859c0SRuslan Bukin	mv	a0, s0		/* Restore ps_strings */
564b1859c0SRuslan Bukin	mv	sp, s1		/* Restore the stack pointer */
574b1859c0SRuslan Bukin	jr	t0		/* Jump to the entry point */
584b1859c0SRuslan BukinEND(.rtld_start)
594b1859c0SRuslan Bukin
604b1859c0SRuslan Bukin/*
614b1859c0SRuslan Bukin * t0 = obj pointer
624b1859c0SRuslan Bukin * t1 = reloc offset
634b1859c0SRuslan Bukin */
644b1859c0SRuslan BukinENTRY(_rtld_bind_start)
654b1859c0SRuslan Bukin	/* Save the arguments and ra */
6629e1fcebSBrooks Davis	/* We require 17 dwords, but the stack must be aligned to 16-bytes */
6729e1fcebSBrooks Davis	addi	sp, sp, -(8 * 18)
684b1859c0SRuslan Bukin	sd	a0, (8 * 0)(sp)
694b1859c0SRuslan Bukin	sd	a1, (8 * 1)(sp)
704b1859c0SRuslan Bukin	sd	a2, (8 * 2)(sp)
714b1859c0SRuslan Bukin	sd	a3, (8 * 3)(sp)
724b1859c0SRuslan Bukin	sd	a4, (8 * 4)(sp)
734b1859c0SRuslan Bukin	sd	a5, (8 * 5)(sp)
744b1859c0SRuslan Bukin	sd	a6, (8 * 6)(sp)
754b1859c0SRuslan Bukin	sd	a7, (8 * 7)(sp)
764b1859c0SRuslan Bukin	sd	ra, (8 * 8)(sp)
77f00ebc42SRuslan Bukin
78f00ebc42SRuslan Bukin#ifdef __riscv_float_abi_double
794b1859c0SRuslan Bukin	/* Save any floating-point arguments */
80f00ebc42SRuslan Bukin	fsd	fa0, (8 * 9)(sp)
81f00ebc42SRuslan Bukin	fsd	fa1, (8 * 10)(sp)
82f00ebc42SRuslan Bukin	fsd	fa2, (8 * 11)(sp)
83f00ebc42SRuslan Bukin	fsd	fa3, (8 * 12)(sp)
84f00ebc42SRuslan Bukin	fsd	fa4, (8 * 13)(sp)
85f00ebc42SRuslan Bukin	fsd	fa5, (8 * 14)(sp)
86f00ebc42SRuslan Bukin	fsd	fa6, (8 * 15)(sp)
87f00ebc42SRuslan Bukin	fsd	fa7, (8 * 16)(sp)
884b1859c0SRuslan Bukin#endif
894b1859c0SRuslan Bukin
904b1859c0SRuslan Bukin	/* Reloc offset is 3x of the .got.plt offset */
914b1859c0SRuslan Bukin	slli	a1, t1, 1	/* Mult items by 2 */
924b1859c0SRuslan Bukin	add	a1, a1, t1	/* Plus item */
934b1859c0SRuslan Bukin
944b1859c0SRuslan Bukin	/* Load obj */
954b1859c0SRuslan Bukin	mv	a0, t0
964b1859c0SRuslan Bukin
974b1859c0SRuslan Bukin	/* Call into rtld */
984b1859c0SRuslan Bukin	jal	_rtld_bind
994b1859c0SRuslan Bukin
1004b1859c0SRuslan Bukin	/* Backup the address to branch to */
1014b1859c0SRuslan Bukin	mv	t0, a0
1024b1859c0SRuslan Bukin
1034b1859c0SRuslan Bukin	/* Restore the arguments and ra */
1044b1859c0SRuslan Bukin	ld	a0, (8 * 0)(sp)
1054b1859c0SRuslan Bukin	ld	a1, (8 * 1)(sp)
1064b1859c0SRuslan Bukin	ld	a2, (8 * 2)(sp)
1074b1859c0SRuslan Bukin	ld	a3, (8 * 3)(sp)
1084b1859c0SRuslan Bukin	ld	a4, (8 * 4)(sp)
1094b1859c0SRuslan Bukin	ld	a5, (8 * 5)(sp)
1104b1859c0SRuslan Bukin	ld	a6, (8 * 6)(sp)
1114b1859c0SRuslan Bukin	ld	a7, (8 * 7)(sp)
1124b1859c0SRuslan Bukin	ld	ra, (8 * 8)(sp)
113f00ebc42SRuslan Bukin
114f00ebc42SRuslan Bukin#ifdef __riscv_float_abi_double
1154b1859c0SRuslan Bukin	/* Restore floating-point arguments */
116f00ebc42SRuslan Bukin	fld	fa0, (8 * 9)(sp)
117f00ebc42SRuslan Bukin	fld	fa1, (8 * 10)(sp)
118f00ebc42SRuslan Bukin	fld	fa2, (8 * 11)(sp)
119f00ebc42SRuslan Bukin	fld	fa3, (8 * 12)(sp)
120f00ebc42SRuslan Bukin	fld	fa4, (8 * 13)(sp)
121f00ebc42SRuslan Bukin	fld	fa5, (8 * 14)(sp)
122f00ebc42SRuslan Bukin	fld	fa6, (8 * 15)(sp)
123f00ebc42SRuslan Bukin	fld	fa7, (8 * 16)(sp)
1244b1859c0SRuslan Bukin#endif
12529e1fcebSBrooks Davis	addi	sp, sp, (8 * 18)
1264b1859c0SRuslan Bukin
1274b1859c0SRuslan Bukin	/* Call into the correct function */
1284b1859c0SRuslan Bukin	jr	t0
1294b1859c0SRuslan BukinEND(_rtld_bind_start)
130