xref: /minix3/libexec/ld.elf_so/arch/mips/rtld_start.S (revision e83f7ba2c93894ce4409c1a9136af3d6d8ad48cf)
1*e83f7ba2SBen Gras/*	$NetBSD: rtld_start.S,v 1.10 2009/12/14 00:41:19 matt Exp $	*/
2*e83f7ba2SBen Gras
3*e83f7ba2SBen Gras/*
4*e83f7ba2SBen Gras * Copyright 1997 Michael L. Hitch <mhitch@montana.edu>
5*e83f7ba2SBen Gras * Portions copyright 2002 Charles M. Hannum <root@ihack.net>
6*e83f7ba2SBen Gras * All rights reserved.
7*e83f7ba2SBen Gras *
8*e83f7ba2SBen Gras * Redistribution and use in source and binary forms, with or without
9*e83f7ba2SBen Gras * modification, are permitted provided that the following conditions
10*e83f7ba2SBen Gras * are met:
11*e83f7ba2SBen Gras * 1. Redistributions of source code must retain the above copyright
12*e83f7ba2SBen Gras *    notice, this list of conditions and the following disclaimer.
13*e83f7ba2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
14*e83f7ba2SBen Gras *    notice, this list of conditions and the following disclaimer in the
15*e83f7ba2SBen Gras *    documentation and/or other materials provided with the distribution.
16*e83f7ba2SBen Gras * 3. The name of the author may not be used to endorse or promote products
17*e83f7ba2SBen Gras *    derived from this software without specific prior written permission.
18*e83f7ba2SBen Gras *
19*e83f7ba2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*e83f7ba2SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*e83f7ba2SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*e83f7ba2SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*e83f7ba2SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*e83f7ba2SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*e83f7ba2SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*e83f7ba2SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*e83f7ba2SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*e83f7ba2SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*e83f7ba2SBen Gras */
30*e83f7ba2SBen Gras
31*e83f7ba2SBen Gras#include <mips/asm.h>
32*e83f7ba2SBen Gras
33*e83f7ba2SBen Gras.globl _C_LABEL(_rtld_relocate_nonplt_self)
34*e83f7ba2SBen Gras.globl _C_LABEL(_rtld)
35*e83f7ba2SBen Gras
36*e83f7ba2SBen Gras#define	PTR_SIZE	(1<<PTR_SCALESHIFT)
37*e83f7ba2SBen Gras
38*e83f7ba2SBen GrasLEAF(rtld_start)
39*e83f7ba2SBen Gras	.frame	sp, 4*PTR_SIZE, ra
40*e83f7ba2SBen Gras	.mask	0x10090000,-PTR_SIZE
41*e83f7ba2SBen Gras	.set	noreorder
42*e83f7ba2SBen Gras	SETUP_GP
43*e83f7ba2SBen Gras	PTR_SUBU sp, 4*PTR_SIZE		# adjust stack pointer
44*e83f7ba2SBen Gras	SETUP_GP64(s4, rtld_start)
45*e83f7ba2SBen Gras	SAVE_GP(0)
46*e83f7ba2SBen Gras					# -> 1*PTR_SIZE(sp) for atexit
47*e83f7ba2SBen Gras					# -> 2*PTR_SIZE(sp) for obj_main
48*e83f7ba2SBen Gras	move	s0, a0			# save stack pointer from a0
49*e83f7ba2SBen Gras	move	s3, a3			# save ps_strings pointer
50*e83f7ba2SBen Gras
51*e83f7ba2SBen Gras	PTR_LA	a1, 1f
52*e83f7ba2SBen Gras	bal	1f
53*e83f7ba2SBen Gras	 PTR_LA	t0, _C_LABEL(_rtld_relocate_nonplt_self)
54*e83f7ba2SBen Gras1:	PTR_SUBU a1, ra, a1		# relocbase
55*e83f7ba2SBen Gras	move	s2, a1			# save for _rtld
56*e83f7ba2SBen Gras	PTR_LA	a0, _DYNAMIC
57*e83f7ba2SBen Gras	PTR_ADDU t9, a1, t0
58*e83f7ba2SBen Gras	jalr	t9			# _rtld_relocate_nonplt_self(dynp, relocabase)
59*e83f7ba2SBen Gras	 PTR_ADDU a0, a1, a0		# &_DYNAMIC
60*e83f7ba2SBen Gras
61*e83f7ba2SBen Gras	move	a1, s2			# relocbase
62*e83f7ba2SBen Gras	PTR_ADDU a0, sp, 2*PTR_SIZE	# sp
63*e83f7ba2SBen Gras	jal	_C_LABEL(_rtld)		# v0 = _rtld(sp, relocbase)
64*e83f7ba2SBen Gras	 nop
65*e83f7ba2SBen Gras
66*e83f7ba2SBen Gras	PTR_L	a1, 2*PTR_SIZE(sp)	# our atexit function
67*e83f7ba2SBen Gras	PTR_L	a2, 3*PTR_SIZE(sp)	# obj_main entry
68*e83f7ba2SBen Gras	PTR_ADDU sp, 4*PTR_SIZE		# readjust stack
69*e83f7ba2SBen Gras	move	a0, s0			# stack pointer
70*e83f7ba2SBen Gras	move	t9, v0
71*e83f7ba2SBen Gras	jr	t9			# _start(sp, cleanup, obj);
72*e83f7ba2SBen Gras	 move	a3, s3			# restore ps_strings
73*e83f7ba2SBen Gras
74*e83f7ba2SBen GrasEND(rtld_start)
75*e83f7ba2SBen Gras
76*e83f7ba2SBen Gras#define	XCALLFRAME_SIZ		(12*SZREG)
77*e83f7ba2SBen Gras#define	XCALLFRAME_RA		(10*SZREG)
78*e83f7ba2SBen Gras#define	XCALLFRAME_GP		(9*SZREG)
79*e83f7ba2SBen Gras#define	XCALLFRAME_S0		(8*SZREG)
80*e83f7ba2SBen Gras#define	XCALLFRAME_A3		(7*SZREG)
81*e83f7ba2SBen Gras#define	XCALLFRAME_A2		(6*SZREG)
82*e83f7ba2SBen Gras#define	XCALLFRAME_A1		(5*SZREG)
83*e83f7ba2SBen Gras#define	XCALLFRAME_A0		(4*SZREG)
84*e83f7ba2SBen Gras#if defined(__mips_n32) || defined(__mips_n64)
85*e83f7ba2SBen Gras#define	XCALLFRAME_A7		(3*SZREG)
86*e83f7ba2SBen Gras#define	XCALLFRAME_A6		(2*SZREG)
87*e83f7ba2SBen Gras#define	XCALLFRAME_A5		(1*SZREG)
88*e83f7ba2SBen Gras#define	XCALLFRAME_A4		(0*SZREG)
89*e83f7ba2SBen Gras#endif
90*e83f7ba2SBen Gras
91*e83f7ba2SBen Gras	.globl	_rtld_bind_start
92*e83f7ba2SBen Gras	.ent	_rtld_bind_start
93*e83f7ba2SBen Gras_rtld_bind_start:
94*e83f7ba2SBen Gras	.frame	sp, XCALLFRAME_SIZ, $15
95*e83f7ba2SBen Gras	move	v1, gp			# save old GP
96*e83f7ba2SBen Gras#if defined(__mips_o32) || defined(__mips_o64)
97*e83f7ba2SBen Gras	PTR_ADDU t9, 8			# modify T9 to point at .cpload
98*e83f7ba2SBen Gras#endif
99*e83f7ba2SBen Gras	SETUP_GP
100*e83f7ba2SBen Gras	PTR_SUBU sp, XCALLFRAME_SIZ	# save arguments and sp value in stack
101*e83f7ba2SBen Gras	SETUP_GP64(XCALLFRAME_GP, _rtld_bind_start)
102*e83f7ba2SBen Gras	SAVE_GP(XCALLFRAME_GP)
103*e83f7ba2SBen Gras#if defined(__mips_n32) || defined(__mips_n64)
104*e83f7ba2SBen Gras	REG_S	a4,  XCALLFRAME_A4(sp)
105*e83f7ba2SBen Gras	REG_S	a5,  XCALLFRAME_A5(sp)
106*e83f7ba2SBen Gras	REG_S	a6,  XCALLFRAME_A6(sp)
107*e83f7ba2SBen Gras	REG_S	a7,  XCALLFRAME_A7(sp)
108*e83f7ba2SBen Gras#endif
109*e83f7ba2SBen Gras	REG_S	a0,  XCALLFRAME_A0(sp)
110*e83f7ba2SBen Gras	REG_S	a1,  XCALLFRAME_A1(sp)
111*e83f7ba2SBen Gras	REG_S	a2,  XCALLFRAME_A2(sp)
112*e83f7ba2SBen Gras	REG_S	a3,  XCALLFRAME_A3(sp)
113*e83f7ba2SBen Gras	REG_S	$15,  XCALLFRAME_RA(sp)	# ra is in t7/t3
114*e83f7ba2SBen Gras	REG_S	s0,  XCALLFRAME_S0(sp)
115*e83f7ba2SBen Gras	move	s0, sp
116*e83f7ba2SBen Gras	move	a0, t8			# symbol index
117*e83f7ba2SBen Gras	move	a1, $15			# old RA
118*e83f7ba2SBen Gras	move	a2, v1			# old GP
119*e83f7ba2SBen Gras	move	a3, ra			# current RA
120*e83f7ba2SBen Gras	jal	_C_LABEL(_rtld_bind)
121*e83f7ba2SBen Gras	 nop
122*e83f7ba2SBen Gras	move	sp, s0
123*e83f7ba2SBen Gras	REG_L	ra, XCALLFRAME_RA(sp)
124*e83f7ba2SBen Gras	REG_L	s0, XCALLFRAME_S0(sp)
125*e83f7ba2SBen Gras	REG_L	a0, XCALLFRAME_A0(sp)
126*e83f7ba2SBen Gras	REG_L	a1, XCALLFRAME_A1(sp)
127*e83f7ba2SBen Gras	REG_L	a2, XCALLFRAME_A2(sp)
128*e83f7ba2SBen Gras	REG_L	a3, XCALLFRAME_A3(sp)
129*e83f7ba2SBen Gras#if defined(__mips_n32) || defined(__mips_n64)
130*e83f7ba2SBen Gras	REG_L	a4, XCALLFRAME_A4(sp)
131*e83f7ba2SBen Gras	REG_L	a5, XCALLFRAME_A5(sp)
132*e83f7ba2SBen Gras	REG_L	a6, XCALLFRAME_A6(sp)
133*e83f7ba2SBen Gras	REG_L	a7, XCALLFRAME_A7(sp)
134*e83f7ba2SBen Gras#endif
135*e83f7ba2SBen Gras	RESTORE_GP64
136*e83f7ba2SBen Gras	PTR_ADDU sp, XCALLFRAME_SIZ
137*e83f7ba2SBen Gras	move	t9, v0
138*e83f7ba2SBen Gras	jr	t9
139*e83f7ba2SBen Gras	 nop
140*e83f7ba2SBen GrasEND(_rtld_bind_start)
141