Home | History | Annotate | Line # | Download | only in riscv
      1  1.6  skrll /*	$NetBSD: rtld_start.S,v 1.6 2023/05/07 12:41:48 skrll Exp $	*/
      2  1.1   matt 
      3  1.1   matt /*-
      4  1.1   matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1   matt  * All rights reserved.
      6  1.1   matt  *
      7  1.1   matt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   matt  * by Matt Thomas of 3am Software Foundry.
      9  1.1   matt  *
     10  1.1   matt  * Redistribution and use in source and binary forms, with or without
     11  1.1   matt  * modification, are permitted provided that the following conditions
     12  1.1   matt  * are met:
     13  1.1   matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1   matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1   matt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   matt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   matt  *    documentation and/or other materials provided with the distribution.
     18  1.1   matt  *
     19  1.1   matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   matt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   matt  */
     31  1.1   matt 
     32  1.1   matt #include <machine/asm.h>
     33  1.1   matt 
     34  1.1   matt 	.globl _C_LABEL(_rtld_relocate_nonplt_self)
     35  1.1   matt 	.globl _C_LABEL(_rtld)
     36  1.1   matt 
     37  1.6  skrll #define PTRSZ	__SIZEOF_POINTER__
     38  1.5  skrll 
     39  1.1   matt /*
     40  1.1   matt  * void
     41  1.6  skrll  * ___start(
     42  1.6  skrll  *     void (*cleanup)(void),
     43  1.6  skrll  *     struct ps_strings *ps_strings
     44  1.6  skrll  * )
     45  1.1   matt  */
     46  1.6  skrll 
     47  1.1   matt ENTRY(_rtld_start)
     48  1.4  skrll 	mv	s0, sp			# save stack pointer
     49  1.6  skrll 	addi	sp, sp, -4 * PTRSZ	# adjust stack pointer
     50  1.5  skrll 					# -> 2 * PTR_SIZE(sp) for atexit
     51  1.5  skrll 					# -> 3 * PTR_SIZE(sp) for obj_main
     52  1.6  skrll 	mv	s1, a1			# save ps_strings pointer
     53  1.1   matt 
     54  1.2   matt .L0:	auipc	gp, %pcrel_hi(_GLOBAL_OFFSET_TABLE_)
     55  1.2   matt 	PTR_L	t0, %pcrel_lo(.L0)(gp) # &_DYNAMIC
     56  1.2   matt .L1:	auipc	a0, %pcrel_hi(_DYNAMIC)
     57  1.2   matt 	addi	a0, a0, %pcrel_lo(.L1)
     58  1.1   matt 	sub	s2, a0, t0		# save for _rtld
     59  1.4  skrll 	mv	a1, s2
     60  1.1   matt 	call	_C_LABEL(_rtld_relocate_nonplt_self)
     61  1.1   matt 
     62  1.4  skrll 	mv	a1, s2			# relocbase
     63  1.6  skrll 	addi	a0, sp, 2 * PTRSZ	# sp
     64  1.2   matt 	call	_C_LABEL(_rtld)		# a0 = _rtld(sp, relocbase)
     65  1.2   matt 	mv	t0, a0
     66  1.1   matt 
     67  1.6  skrll 	PTR_L	a0, 2 * PTRSZ(sp)	# cleanup function
     68  1.6  skrll //	PTR_L	a1, 3 * PTRSZ(sp)	# obj_main pointer (not used)
     69  1.6  skrll 	mv	a1, s1			# restore ps_strings
     70  1.6  skrll 	mv	sp, s0			# restore stack pointer
     71  1.4  skrll 	mv	s0, zero		# break stack chain
     72  1.6  skrll 	jr	t0			# _start(cleanup, ps_strings);
     73  1.1   matt END(_rtld_start)
     74  1.1   matt 
     75  1.6  skrll /*
     76  1.6  skrll  * Needs to be 12 so that stack alignment is preserved on both RV32 and RV64.
     77  1.6  skrll  */
     78  1.6  skrll 
     79  1.5  skrll #define	XCALLFRAME_SIZ		(12 * SZREG)
     80  1.5  skrll #define	XCALLFRAME_RA		( 8 * SZREG)
     81  1.5  skrll #define	XCALLFRAME_A7		( 7 * SZREG)
     82  1.5  skrll #define	XCALLFRAME_A6		( 6 * SZREG)
     83  1.5  skrll #define	XCALLFRAME_A5		( 5 * SZREG)
     84  1.5  skrll #define	XCALLFRAME_A4		( 4 * SZREG)
     85  1.5  skrll #define	XCALLFRAME_A3		( 3 * SZREG)
     86  1.5  skrll #define	XCALLFRAME_A2		( 2 * SZREG)
     87  1.5  skrll #define	XCALLFRAME_A1		( 1 * SZREG)
     88  1.5  skrll #define	XCALLFRAME_A0		( 0 * SZREG)
     89  1.1   matt 
     90  1.1   matt /*
     91  1.1   matt  * t0 = obj pointer
     92  1.1   matt  * t1 = reloc offset
     93  1.1   matt  */
     94  1.1   matt ENTRY_NP(_rtld_bind_start)
     95  1.1   matt 	addi	sp, sp, -XCALLFRAME_SIZ	// save arguments on stack
     96  1.1   matt 	REG_S	a0,  XCALLFRAME_A0(sp)
     97  1.1   matt 	REG_S	a1,  XCALLFRAME_A1(sp)
     98  1.1   matt 	REG_S	a2,  XCALLFRAME_A2(sp)
     99  1.1   matt 	REG_S	a3,  XCALLFRAME_A3(sp)
    100  1.1   matt 	REG_S	a4,  XCALLFRAME_A4(sp)
    101  1.1   matt 	REG_S	a5,  XCALLFRAME_A5(sp)
    102  1.1   matt 	REG_S	a6,  XCALLFRAME_A6(sp)
    103  1.1   matt 	REG_S	a7,  XCALLFRAME_A7(sp)
    104  1.1   matt 	REG_S	ra,  XCALLFRAME_RA(sp)
    105  1.1   matt 
    106  1.2   matt 	mv	a0, t0			/* object from got.plt[1] */
    107  1.2   matt 	mv	a1, t1			/* reloc offset */
    108  1.1   matt 
    109  1.1   matt 	call	_C_LABEL(_rtld_bind)
    110  1.2   matt 	mv	t0, a0			/* save function pointer */
    111  1.1   matt 
    112  1.1   matt 	REG_L	a0, XCALLFRAME_A0(sp)
    113  1.1   matt 	REG_L	a1, XCALLFRAME_A1(sp)
    114  1.1   matt 	REG_L	a2, XCALLFRAME_A2(sp)
    115  1.1   matt 	REG_L	a3, XCALLFRAME_A3(sp)
    116  1.1   matt 	REG_L	a4, XCALLFRAME_A4(sp)
    117  1.1   matt 	REG_L	a5, XCALLFRAME_A5(sp)
    118  1.1   matt 	REG_L	a6, XCALLFRAME_A6(sp)
    119  1.1   matt 	REG_L	a7, XCALLFRAME_A7(sp)
    120  1.3  skrll 	REG_L	ra, XCALLFRAME_RA(sp)
    121  1.1   matt 	addi	sp, sp, XCALLFRAME_SIZ
    122  1.2   matt 	jr	t0
    123  1.1   matt END(_rtld_bind_start)
    124