xref: /netbsd-src/libexec/ld.elf_so/arch/mips/rtld_start.S (revision ce716eeb9a02c7ecc82ab81d906a970d97432925)
1*ce716eebSriastradh/*	$NetBSD: rtld_start.S,v 1.11 2024/08/03 21:59:58 riastradh Exp $	*/
2082edeccSmhitch
3082edeccSmhitch/*
4082edeccSmhitch * Copyright 1997 Michael L. Hitch <mhitch@montana.edu>
5ad8ccd62Smycroft * Portions copyright 2002 Charles M. Hannum <root@ihack.net>
6082edeccSmhitch * All rights reserved.
7082edeccSmhitch *
8082edeccSmhitch * Redistribution and use in source and binary forms, with or without
9082edeccSmhitch * modification, are permitted provided that the following conditions
10082edeccSmhitch * are met:
11082edeccSmhitch * 1. Redistributions of source code must retain the above copyright
12082edeccSmhitch *    notice, this list of conditions and the following disclaimer.
13082edeccSmhitch * 2. Redistributions in binary form must reproduce the above copyright
14082edeccSmhitch *    notice, this list of conditions and the following disclaimer in the
15082edeccSmhitch *    documentation and/or other materials provided with the distribution.
16082edeccSmhitch * 3. The name of the author may not be used to endorse or promote products
17082edeccSmhitch *    derived from this software without specific prior written permission.
18082edeccSmhitch *
19082edeccSmhitch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20082edeccSmhitch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21082edeccSmhitch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22082edeccSmhitch * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23082edeccSmhitch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24082edeccSmhitch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25082edeccSmhitch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26082edeccSmhitch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27082edeccSmhitch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28082edeccSmhitch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29082edeccSmhitch */
30082edeccSmhitch
31082edeccSmhitch#include <mips/asm.h>
32082edeccSmhitch
33ac81e326Smycroft.globl _C_LABEL(_rtld_relocate_nonplt_self)
34ac81e326Smycroft.globl _C_LABEL(_rtld)
35ac81e326Smycroft
36d8d1440dSmatt#define	PTR_SIZE	(1<<PTR_SCALESHIFT)
37d8d1440dSmatt
38082edeccSmhitchLEAF(rtld_start)
39d8d1440dSmatt	.frame	sp, 4*PTR_SIZE, ra
40d8d1440dSmatt	.mask	0x10090000,-PTR_SIZE
41082edeccSmhitch	.set	noreorder
42d8d1440dSmatt	SETUP_GP
43d8d1440dSmatt	PTR_SUBU sp, 4*PTR_SIZE		# adjust stack pointer
44d8d1440dSmatt	SETUP_GP64(s4, rtld_start)
45d8d1440dSmatt	SAVE_GP(0)
46d8d1440dSmatt					# -> 1*PTR_SIZE(sp) for atexit
47d8d1440dSmatt					# -> 2*PTR_SIZE(sp) for obj_main
48082edeccSmhitch	move	s0, a0			# save stack pointer from a0
49d8d1440dSmatt	move	s3, a3			# save ps_strings pointer
50ac81e326Smycroft
51d8d1440dSmatt	PTR_LA	a1, 1f
52ac81e326Smycroft	bal	1f
53d8d1440dSmatt	 PTR_LA	t0, _C_LABEL(_rtld_relocate_nonplt_self)
54d8d1440dSmatt1:	PTR_SUBU a1, ra, a1		# relocbase
55d8d1440dSmatt	move	s2, a1			# save for _rtld
56d8d1440dSmatt	PTR_LA	a0, _DYNAMIC
57d8d1440dSmatt	PTR_ADDU t9, a1, t0
58d8d1440dSmatt	jalr	t9			# _rtld_relocate_nonplt_self(dynp, relocabase)
59d8d1440dSmatt	 PTR_ADDU a0, a1, a0		# &_DYNAMIC
60ac81e326Smycroft
61ac81e326Smycroft	move	a1, s2			# relocbase
62d8d1440dSmatt	PTR_ADDU a0, sp, 2*PTR_SIZE	# sp
63ac81e326Smycroft	jal	_C_LABEL(_rtld)		# v0 = _rtld(sp, relocbase)
64ac81e326Smycroft	 nop
65ac81e326Smycroft
66d8d1440dSmatt	PTR_L	a1, 2*PTR_SIZE(sp)	# our atexit function
67d8d1440dSmatt	PTR_L	a2, 3*PTR_SIZE(sp)	# obj_main entry
68d8d1440dSmatt	PTR_ADDU sp, 4*PTR_SIZE		# readjust stack
69082edeccSmhitch	move	a0, s0			# stack pointer
70082edeccSmhitch	move	t9, v0
71082edeccSmhitch	jr	t9			# _start(sp, cleanup, obj);
72d8d1440dSmatt	 move	a3, s3			# restore ps_strings
73082edeccSmhitch
74082edeccSmhitchEND(rtld_start)
75082edeccSmhitch
76d8d1440dSmatt#define	XCALLFRAME_SIZ		(12*SZREG)
77d8d1440dSmatt#define	XCALLFRAME_RA		(10*SZREG)
78d8d1440dSmatt#define	XCALLFRAME_GP		(9*SZREG)
79d8d1440dSmatt#define	XCALLFRAME_S0		(8*SZREG)
80d8d1440dSmatt#define	XCALLFRAME_A3		(7*SZREG)
81d8d1440dSmatt#define	XCALLFRAME_A2		(6*SZREG)
82d8d1440dSmatt#define	XCALLFRAME_A1		(5*SZREG)
83d8d1440dSmatt#define	XCALLFRAME_A0		(4*SZREG)
84d8d1440dSmatt#if defined(__mips_n32) || defined(__mips_n64)
85d8d1440dSmatt#define	XCALLFRAME_A7		(3*SZREG)
86d8d1440dSmatt#define	XCALLFRAME_A6		(2*SZREG)
87d8d1440dSmatt#define	XCALLFRAME_A5		(1*SZREG)
88d8d1440dSmatt#define	XCALLFRAME_A4		(0*SZREG)
89d8d1440dSmatt#endif
90d8d1440dSmatt
91082edeccSmhitch	.globl	_rtld_bind_start
92082edeccSmhitch	.ent	_rtld_bind_start
93082edeccSmhitch_rtld_bind_start:
94d8d1440dSmatt	.frame	sp, XCALLFRAME_SIZ, $15
95082edeccSmhitch	move	v1, gp			# save old GP
96d8d1440dSmatt#if defined(__mips_o32) || defined(__mips_o64)
97d8d1440dSmatt	PTR_ADDU t9, 8			# modify T9 to point at .cpload
98d8d1440dSmatt#endif
99d8d1440dSmatt	SETUP_GP
100d8d1440dSmatt	PTR_SUBU sp, XCALLFRAME_SIZ	# save arguments and sp value in stack
101d8d1440dSmatt	SETUP_GP64(XCALLFRAME_GP, _rtld_bind_start)
102d8d1440dSmatt	SAVE_GP(XCALLFRAME_GP)
103d8d1440dSmatt#if defined(__mips_n32) || defined(__mips_n64)
104d8d1440dSmatt	REG_S	a4,  XCALLFRAME_A4(sp)
105d8d1440dSmatt	REG_S	a5,  XCALLFRAME_A5(sp)
106d8d1440dSmatt	REG_S	a6,  XCALLFRAME_A6(sp)
107d8d1440dSmatt	REG_S	a7,  XCALLFRAME_A7(sp)
108d8d1440dSmatt#endif
109d8d1440dSmatt	REG_S	a0,  XCALLFRAME_A0(sp)
110d8d1440dSmatt	REG_S	a1,  XCALLFRAME_A1(sp)
111d8d1440dSmatt	REG_S	a2,  XCALLFRAME_A2(sp)
112d8d1440dSmatt	REG_S	a3,  XCALLFRAME_A3(sp)
113d8d1440dSmatt	REG_S	$15,  XCALLFRAME_RA(sp)	# ra is in t7/t3
114d8d1440dSmatt	REG_S	s0,  XCALLFRAME_S0(sp)
115082edeccSmhitch	move	s0, sp
116082edeccSmhitch	move	a0, t8			# symbol index
117d8d1440dSmatt	move	a1, $15			# old RA
118082edeccSmhitch	move	a2, v1			# old GP
119082edeccSmhitch	move	a3, ra			# current RA
1207d4bbc7fSmycroft	jal	_C_LABEL(_rtld_bind)
121082edeccSmhitch	 nop
122082edeccSmhitch	move	sp, s0
123d8d1440dSmatt	REG_L	ra, XCALLFRAME_RA(sp)
124d8d1440dSmatt	REG_L	s0, XCALLFRAME_S0(sp)
125d8d1440dSmatt	REG_L	a0, XCALLFRAME_A0(sp)
126d8d1440dSmatt	REG_L	a1, XCALLFRAME_A1(sp)
127d8d1440dSmatt	REG_L	a2, XCALLFRAME_A2(sp)
128d8d1440dSmatt	REG_L	a3, XCALLFRAME_A3(sp)
129d8d1440dSmatt#if defined(__mips_n32) || defined(__mips_n64)
130d8d1440dSmatt	REG_L	a4, XCALLFRAME_A4(sp)
131d8d1440dSmatt	REG_L	a5, XCALLFRAME_A5(sp)
132d8d1440dSmatt	REG_L	a6, XCALLFRAME_A6(sp)
133d8d1440dSmatt	REG_L	a7, XCALLFRAME_A7(sp)
134d8d1440dSmatt#endif
135d8d1440dSmatt	RESTORE_GP64
136d8d1440dSmatt	PTR_ADDU sp, XCALLFRAME_SIZ
137082edeccSmhitch	move	t9, v0
138082edeccSmhitch	jr	t9
139082edeccSmhitch	 nop
140d8d1440dSmattEND(_rtld_bind_start)
141