1*0cdd3313Sskrll/*	$NetBSD: rtld_start.S,v 1.6 2023/05/07 12:41:48 skrll Exp $	*/
225195b1eSmatt
325195b1eSmatt/*-
425195b1eSmatt * Copyright (c) 2014 The NetBSD Foundation, Inc.
525195b1eSmatt * All rights reserved.
625195b1eSmatt *
725195b1eSmatt * This code is derived from software contributed to The NetBSD Foundation
825195b1eSmatt * by Matt Thomas of 3am Software Foundry.
925195b1eSmatt *
1025195b1eSmatt * Redistribution and use in source and binary forms, with or without
1125195b1eSmatt * modification, are permitted provided that the following conditions
1225195b1eSmatt * are met:
1325195b1eSmatt * 1. Redistributions of source code must retain the above copyright
1425195b1eSmatt *    notice, this list of conditions and the following disclaimer.
1525195b1eSmatt * 2. Redistributions in binary form must reproduce the above copyright
1625195b1eSmatt *    notice, this list of conditions and the following disclaimer in the
1725195b1eSmatt *    documentation and/or other materials provided with the distribution.
1825195b1eSmatt *
1925195b1eSmatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2025195b1eSmatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2125195b1eSmatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2225195b1eSmatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2325195b1eSmatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2425195b1eSmatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2525195b1eSmatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2625195b1eSmatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2725195b1eSmatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2825195b1eSmatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2925195b1eSmatt * POSSIBILITY OF SUCH DAMAGE.
3025195b1eSmatt */
3125195b1eSmatt
3225195b1eSmatt#include <machine/asm.h>
3325195b1eSmatt
3425195b1eSmatt	.globl _C_LABEL(_rtld_relocate_nonplt_self)
3525195b1eSmatt	.globl _C_LABEL(_rtld)
3625195b1eSmatt
37*0cdd3313Sskrll#define PTRSZ	__SIZEOF_POINTER__
38a66eafddSskrll
3925195b1eSmatt/*
4025195b1eSmatt * void
41*0cdd3313Sskrll * ___start(
42*0cdd3313Sskrll *     void (*cleanup)(void),
43*0cdd3313Sskrll *     struct ps_strings *ps_strings
44*0cdd3313Sskrll * )
4525195b1eSmatt */
46*0cdd3313Sskrll
4725195b1eSmattENTRY(_rtld_start)
48abb1a6d3Sskrll	mv	s0, sp			# save stack pointer
49*0cdd3313Sskrll	addi	sp, sp, -4 * PTRSZ	# adjust stack pointer
5025195b1eSmatt					# -> 2 * PTR_SIZE(sp) for atexit
5125195b1eSmatt					# -> 3 * PTR_SIZE(sp) for obj_main
52*0cdd3313Sskrll	mv	s1, a1			# save ps_strings pointer
5325195b1eSmatt
543ae76ab5Smatt.L0:	auipc	gp, %pcrel_hi(_GLOBAL_OFFSET_TABLE_)
553ae76ab5Smatt	PTR_L	t0, %pcrel_lo(.L0)(gp) # &_DYNAMIC
563ae76ab5Smatt.L1:	auipc	a0, %pcrel_hi(_DYNAMIC)
573ae76ab5Smatt	addi	a0, a0, %pcrel_lo(.L1)
5825195b1eSmatt	sub	s2, a0, t0		# save for _rtld
59abb1a6d3Sskrll	mv	a1, s2
6025195b1eSmatt	call	_C_LABEL(_rtld_relocate_nonplt_self)
6125195b1eSmatt
62abb1a6d3Sskrll	mv	a1, s2			# relocbase
63*0cdd3313Sskrll	addi	a0, sp, 2 * PTRSZ	# sp
643ae76ab5Smatt	call	_C_LABEL(_rtld)		# a0 = _rtld(sp, relocbase)
653ae76ab5Smatt	mv	t0, a0
6625195b1eSmatt
67*0cdd3313Sskrll	PTR_L	a0, 2 * PTRSZ(sp)	# cleanup function
68*0cdd3313Sskrll//	PTR_L	a1, 3 * PTRSZ(sp)	# obj_main pointer (not used)
69*0cdd3313Sskrll	mv	a1, s1			# restore ps_strings
70*0cdd3313Sskrll	mv	sp, s0			# restore stack pointer
71abb1a6d3Sskrll	mv	s0, zero		# break stack chain
72*0cdd3313Sskrll	jr	t0			# _start(cleanup, ps_strings);
7325195b1eSmattEND(_rtld_start)
7425195b1eSmatt
75*0cdd3313Sskrll/*
76*0cdd3313Sskrll * Needs to be 12 so that stack alignment is preserved on both RV32 and RV64.
77*0cdd3313Sskrll */
78*0cdd3313Sskrll
7925195b1eSmatt#define	XCALLFRAME_SIZ		(12 * SZREG)
8025195b1eSmatt#define	XCALLFRAME_RA		( 8 * SZREG)
8125195b1eSmatt#define	XCALLFRAME_A7		( 7 * SZREG)
8225195b1eSmatt#define	XCALLFRAME_A6		( 6 * SZREG)
8325195b1eSmatt#define	XCALLFRAME_A5		( 5 * SZREG)
8425195b1eSmatt#define	XCALLFRAME_A4		( 4 * SZREG)
8525195b1eSmatt#define	XCALLFRAME_A3		( 3 * SZREG)
8625195b1eSmatt#define	XCALLFRAME_A2		( 2 * SZREG)
8725195b1eSmatt#define	XCALLFRAME_A1		( 1 * SZREG)
8825195b1eSmatt#define	XCALLFRAME_A0		( 0 * SZREG)
8925195b1eSmatt
9025195b1eSmatt/*
9125195b1eSmatt * t0 = obj pointer
9225195b1eSmatt * t1 = reloc offset
9325195b1eSmatt */
9425195b1eSmattENTRY_NP(_rtld_bind_start)
9525195b1eSmatt	addi	sp, sp, -XCALLFRAME_SIZ	// save arguments on stack
9625195b1eSmatt	REG_S	a0,  XCALLFRAME_A0(sp)
9725195b1eSmatt	REG_S	a1,  XCALLFRAME_A1(sp)
9825195b1eSmatt	REG_S	a2,  XCALLFRAME_A2(sp)
9925195b1eSmatt	REG_S	a3,  XCALLFRAME_A3(sp)
10025195b1eSmatt	REG_S	a4,  XCALLFRAME_A4(sp)
10125195b1eSmatt	REG_S	a5,  XCALLFRAME_A5(sp)
10225195b1eSmatt	REG_S	a6,  XCALLFRAME_A6(sp)
10325195b1eSmatt	REG_S	a7,  XCALLFRAME_A7(sp)
10425195b1eSmatt	REG_S	ra,  XCALLFRAME_RA(sp)
10525195b1eSmatt
1063ae76ab5Smatt	mv	a0, t0			/* object from got.plt[1] */
1073ae76ab5Smatt	mv	a1, t1			/* reloc offset */
10825195b1eSmatt
10925195b1eSmatt	call	_C_LABEL(_rtld_bind)
1103ae76ab5Smatt	mv	t0, a0			/* save function pointer */
11125195b1eSmatt
11225195b1eSmatt	REG_L	a0, XCALLFRAME_A0(sp)
11325195b1eSmatt	REG_L	a1, XCALLFRAME_A1(sp)
11425195b1eSmatt	REG_L	a2, XCALLFRAME_A2(sp)
11525195b1eSmatt	REG_L	a3, XCALLFRAME_A3(sp)
11625195b1eSmatt	REG_L	a4, XCALLFRAME_A4(sp)
11725195b1eSmatt	REG_L	a5, XCALLFRAME_A5(sp)
11825195b1eSmatt	REG_L	a6, XCALLFRAME_A6(sp)
11925195b1eSmatt	REG_L	a7, XCALLFRAME_A7(sp)
12025195b1eSmatt	REG_L	ra, XCALLFRAME_RA(sp)
12125195b1eSmatt	addi	sp, sp, XCALLFRAME_SIZ
1223ae76ab5Smatt	jr	t0
12325195b1eSmattEND(_rtld_bind_start)
124