xref: /netbsd-src/libexec/ld.elf_so/arch/arm/rtld_start.S (revision 467f6149c3861c8d2406befc5bc63d581fbd4567)
1*467f6149Smatt/*	$NetBSD: rtld_start.S,v 1.12 2013/12/03 00:19:56 matt Exp $	*/
2a61a4099Smatt
3a61a4099Smatt/*-
4ebf50cdfSmycroft * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
5a61a4099Smatt * All rights reserved.
6a61a4099Smatt *
7a61a4099Smatt * This code is derived from software contributed to The NetBSD Foundation
8ebf50cdfSmycroft * by Matt Thomas and by Charles M. Hannum.
9a61a4099Smatt *
10a61a4099Smatt * Redistribution and use in source and binary forms, with or without
11a61a4099Smatt * modification, are permitted provided that the following conditions
12a61a4099Smatt * are met:
13a61a4099Smatt * 1. Redistributions of source code must retain the above copyright
14a61a4099Smatt *    notice, this list of conditions and the following disclaimer.
15a61a4099Smatt * 2. Redistributions in binary form must reproduce the above copyright
16a61a4099Smatt *    notice, this list of conditions and the following disclaimer in the
17a61a4099Smatt *    documentation and/or other materials provided with the distribution.
18a61a4099Smatt *
19a61a4099Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a61a4099Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a61a4099Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a61a4099Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a61a4099Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a61a4099Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a61a4099Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a61a4099Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a61a4099Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a61a4099Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a61a4099Smatt * POSSIBILITY OF SUCH DAMAGE.
30a61a4099Smatt */
31a61a4099Smatt
32a61a4099Smatt#include <machine/asm.h>
33a61a4099Smatt
34*467f6149SmattRCSID("$NetBSD: rtld_start.S,v 1.12 2013/12/03 00:19:56 matt Exp $")
35b9fe4e7dSskrll
36*467f6149SmattENTRY_NP(_rtld_start)
37a61a4099Smatt	sub	sp, sp, #8		/* make room for obj_main & exit proc */
38*467f6149Smatt	movs	r4, r0			/* save ps_strings */
39a61a4099Smatt
40*467f6149Smatt	ldr	r6, .Lgot
41*467f6149Smatt	ldr	r5, .Ldynamic
42*467f6149Smatt	add	r6, r6, pc		/* r6 = &GOT[0]; pc = &LPIC1; */
43*467f6149Smatt	adds	r0, r6, r5		/* arg0 = &_DYNAMIC */
44*467f6149Smatt.LPIC1:
45*467f6149Smatt	ldr	r5, [r6]		/* GOT[0] = &_DYNAMIC */
46*467f6149Smatt	subs	r5, r0, r5		/* relocbase = GOT[0] - &_DYNAMIC */
47*467f6149Smatt	movs	r1, r5			/* arg1 = relocabase */
481da1ca49Smycroft	bl	_rtld_relocate_nonplt_self
499f0e1451Smycroft
50*467f6149Smatt	movs	r1, r5			/* relocbase */
515ee0e5e1Smycroft	mov	r0, sp			/* sp */
52a61a4099Smatt	bl	_rtld			/* call the shared loader */
53*467f6149Smatt	movs	r3, r0			/* save entry point */
54a61a4099Smatt
55*467f6149Smatt#ifdef __thumb__
56*467f6149Smatt	ldr	r2, [sp, #0]		/* pop r2 = cleanup */
57*467f6149Smatt	ldr	r1, [sp, #4]		/* pop r1 = obj_main */
58*467f6149Smatt	add	sp, sp, #8
59*467f6149Smatt#else
604389fd11Smatt	ldr	r2, [sp], #4		/* pop r2 = cleanup */
614389fd11Smatt	ldr	r1, [sp], #4		/* pop r1 = obj_main */
62c3e14804Srearnsha#endif
63*467f6149Smatt	movs	r0, r4			/* restore ps_strings */
64*467f6149Smatt	RETr(r3)
65a61a4099Smatt
66a61a4099Smatt	.align	0
67*467f6149Smatt.Lgot:
68*467f6149Smatt	.word	_GLOBAL_OFFSET_TABLE_ - .LPIC1
69*467f6149Smatt.Ldynamic:
70*467f6149Smatt	.word	_DYNAMIC(GOTOFF)
71*467f6149SmattEND(_rtld_start)
72*467f6149Smatt
73a61a4099Smatt/*
74a61a4099Smatt *	stack[0] = RA
75a61a4099Smatt *	ip = &GOT[n+3]
76a61a4099Smatt *	lr = &GOT[2]
77a61a4099Smatt */
78*467f6149SmattARM_ENTRY_NP(_rtld_bind_start)
794389fd11Smatt	stmdb	sp!,{r0-r4,sl,fp}	/* 8 byte aligned (lr already saved) */
80a61a4099Smatt
81a61a4099Smatt	sub	r1, ip, lr		/* r1 = 4 * (n + 1) */
82a61a4099Smatt	sub	r1, r1, #4		/* r1 = 4 * n */
83a61a4099Smatt	add	r1, r1, r1		/* r1 = 8 * n */
84a61a4099Smatt
85a61a4099Smatt	ldr	r0, [lr, #-4]		/* get obj ptr from GOT[1] */
8685d99cf4Smatt	mov	r4, ip			/* save GOT location */
87a61a4099Smatt
88a61a4099Smatt	bl	_rtld_bind		/* Call the binder */
89a61a4099Smatt
9085d99cf4Smatt	str	r0, [r4]		/* save address in GOT */
91a61a4099Smatt	mov	ip, r0			/* save new address */
92a61a4099Smatt
9385d99cf4Smatt	ldmia	sp!,{r0-r4,sl,fp,lr}	/* restore the stack */
94*467f6149Smatt	RETr(ip)			/* jump to the new address */
95*467f6149SmattEND(_rtld_bind_start)
96