rtld_start.S revision 1.18 1 1.18 riastrad /* $NetBSD: rtld_start.S,v 1.18 2024/08/03 21:59:58 riastradh Exp $ */
2 1.1 eeh
3 1.1 eeh /*-
4 1.1 eeh * Copyright (c) 2000 Eduardo Horvath.
5 1.16 mycroft * Copyright (c) 1999, 2002, 2003 The NetBSD Foundation, Inc.
6 1.1 eeh * All rights reserved.
7 1.1 eeh *
8 1.1 eeh * This code is derived from software contributed to The NetBSD Foundation
9 1.9 mycroft * by Christos Zoulas, Paul Kranenburg and by Charles M. Hannum.
10 1.1 eeh *
11 1.1 eeh * Redistribution and use in source and binary forms, with or without
12 1.1 eeh * modification, are permitted provided that the following conditions
13 1.1 eeh * are met:
14 1.1 eeh * 1. Redistributions of source code must retain the above copyright
15 1.1 eeh * notice, this list of conditions and the following disclaimer.
16 1.1 eeh * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 eeh * notice, this list of conditions and the following disclaimer in the
18 1.1 eeh * documentation and/or other materials provided with the distribution.
19 1.1 eeh *
20 1.1 eeh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 eeh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 eeh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 eeh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 eeh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 eeh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 eeh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 eeh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 eeh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 eeh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 eeh * POSSIBILITY OF SUCH DAMAGE.
31 1.1 eeh */
32 1.1 eeh
33 1.1 eeh #include <machine/asm.h>
34 1.2 eeh #define _LOCORE
35 1.2 eeh #include <machine/frame.h>
36 1.1 eeh
37 1.1 eeh /*
38 1.1 eeh * ELF:
39 1.1 eeh * On startup the stack should contain 16 extended word register save area,
40 1.1 eeh * followed by the arg count, etc.
41 1.1 eeh *
42 1.1 eeh * _rtld() expects the stack pointer to point to two longwords for argument
43 1.1 eeh * return followed by argc, etc. We need to create a pointer to
44 1.1 eeh * &argc + 16 and pass that in. The return args will be in those locations.
45 1.18 riastrad *
46 1.1 eeh * NB: We are violating the ELF spec by passing a pointer to the ps strings in
47 1.1 eeh * %g1 instead of a termination routine.
48 1.1 eeh */
49 1.6 thorpej
50 1.6 thorpej .register %g2,#scratch
51 1.6 thorpej .register %g3,#scratch
52 1.6 thorpej
53 1.1 eeh /* Offset of ARGC from bottom of stack */
54 1.13 mycroft #define ARGC 176
55 1.18 riastrad
56 1.1 eeh .section ".text"
57 1.7 mycroft .align 4
58 1.1 eeh .global _rtld_start
59 1.1 eeh .type _rtld_start,@function
60 1.1 eeh _rtld_start:
61 1.1 eeh mov 0, %fp /* Erect a fence post for ourselves */
62 1.1 eeh mov %g1, %l1 /* save ps_strings */
63 1.13 mycroft sub %sp, 48+16, %sp /* Make room for return args */
64 1.7 mycroft
65 1.7 mycroft sethi %hi(_GLOBAL_OFFSET_TABLE_-4), %l7
66 1.15 mycroft call 0f
67 1.7 mycroft add %l7, %lo(_GLOBAL_OFFSET_TABLE_+4), %l7
68 1.15 mycroft call _DYNAMIC+8
69 1.15 mycroft 0: add %l7, %o7, %l7 /* real &_GLOBAL_OFFSET_TABLE_ */
70 1.15 mycroft ld [%o7+8], %o0 /* load stub call instruction */
71 1.15 mycroft ldx [%l7], %l0 /* base-relative &_DYNAMIC */
72 1.15 mycroft sll %o0, 2, %o0 /* extract PC offset */
73 1.15 mycroft sra %o0, 0, %o0 /* sign-extend */
74 1.8 mycroft
75 1.15 mycroft add %o0, %o7, %o0 /* real &_DYNAMIC */
76 1.15 mycroft sub %o0, %l0, %l0 /* relocbase */
77 1.7 mycroft call _rtld_relocate_nonplt_self
78 1.15 mycroft mov %l0, %o1 /* relocbase */
79 1.1 eeh
80 1.15 mycroft mov %l0, %o1 /* relocbase */
81 1.1 eeh call _rtld
82 1.7 mycroft add %sp, BIAS + ARGC, %o0 /* &argc - 16 */
83 1.1 eeh
84 1.7 mycroft ldx [%sp + BIAS + ARGC], %g3 /* arg: cleanup */
85 1.7 mycroft ldx [%sp + BIAS + ARGC + 8], %g2 /* arg: obj */
86 1.13 mycroft add %sp, 48+16, %sp /* restore stack pointer */
87 1.1 eeh
88 1.1 eeh jmp %o0
89 1.1 eeh mov %l1, %g1 /* restore ps_strings */
90 1.1 eeh
91 1.1 eeh
92 1.1 eeh /*
93 1.1 eeh * We have two separate entry points to the runtime linker.
94 1.5 eeh * I'm implementing this following the SPARC v9 ABI spec.
95 1.1 eeh *
96 1.14 mycroft * _rtld_bind_start_0(y, x) is called from .PLT0, and is used for
97 1.1 eeh * PLT entries above 32768.
98 1.1 eeh *
99 1.14 mycroft * _rtld_bind_start_1(y, x) is called from .PLT1, and is used for
100 1.1 eeh * PLT entries below 32768.
101 1.1 eeh *
102 1.1 eeh * The first two entries of PLT2 contain the xword object pointer.
103 1.1 eeh *
104 1.18 riastrad * These routines are called with two longword arguments,
105 1.1 eeh * x and y. To calculate the address of the entry,
106 1.14 mycroft * _rtld_bind_start_1(y, x) does:
107 1.1 eeh *
108 1.1 eeh * n = x >> 15;
109 1.1 eeh *
110 1.14 mycroft * and _rtld_bind_start_0(y, x) does:
111 1.1 eeh *
112 1.14 mycroft * i = x - y + 8 - 32768*32;
113 1.1 eeh * n = 32768 + (i/5120)*160 + (i%5120)/24;
114 1.1 eeh *
115 1.1 eeh * Neither routine needs to issue a save since it's already been
116 1.1 eeh * done in the PLT entry.
117 1.1 eeh */
118 1.2 eeh
119 1.1 eeh .section ".text"
120 1.1 eeh .align 4
121 1.2 eeh .global _rtld_bind_start_0
122 1.2 eeh .type _rtld_bind_start_0,@function
123 1.11 mycroft _rtld_bind_start_0: # (y, x)
124 1.11 mycroft /* %o0 = obj->pltgot[6] */
125 1.11 mycroft /* %o1 = plt[4] */
126 1.11 mycroft /* %o1 - %o0 + 8 == offset of plt[] from obj->pltgot[] */
127 1.11 mycroft /* -32768*32 to get offset from beginning of upper PLT section */
128 1.11 mycroft
129 1.11 mycroft sethi %hi(32768*32-8), %l1
130 1.11 mycroft sub %o1, %o0, %l0 /* i = x - y */
131 1.11 mycroft or %l1, %lo(32768*32-8), %l1
132 1.11 mycroft sub %l0, %l1, %l0 /* i = x - y + 8 - 32768*32 */
133 1.11 mycroft
134 1.11 mycroft ldx [%o0 + (10*4)], %o0 /* Load object pointer from PLT2 */
135 1.1 eeh
136 1.12 mycroft sethi %hi(5120), %l1
137 1.12 mycroft sdivx %l0, %l1, %l1 /* Calculate i/5120 */
138 1.12 mycroft sllx %l1, 2, %l2
139 1.12 mycroft add %l2, %l1, %l2
140 1.12 mycroft sllx %l2, 10, %l2
141 1.12 mycroft sub %l0, %l2, %l2 /* And i%5120 */
142 1.18 riastrad
143 1.1 eeh /* Let the division churn for a bit. */
144 1.11 mycroft sdivx %l2, 24, %l4 /* (i%5120)/24 */
145 1.1 eeh
146 1.1 eeh /* 160 is (32 * 5) or (32 * (4 + 1)) */
147 1.1 eeh sllx %l1, 2, %l3 /* 4 * (i/5120) */
148 1.1 eeh add %l1, %l3, %l3 /* 5 * (i/5120) */
149 1.1 eeh sllx %l3, 5, %l3 /* 32 * 5 * (i/5120) */
150 1.1 eeh
151 1.1 eeh sethi %hi(32768), %l6
152 1.1 eeh add %l3, %l4, %l5 /* %l5 = (i/5120)*160 + (i%5120)/24; */
153 1.18 riastrad
154 1.1 eeh call _rtld_bind /* Call _rtld_bind(obj, offset) */
155 1.11 mycroft add %l5, %l6, %o1 /* %o1 = 32768 + ... */
156 1.2 eeh
157 1.1 eeh jmp %o0 /* return value == function address */
158 1.1 eeh restore /* Dump our stack frame */
159 1.18 riastrad
160 1.1 eeh .section ".text"
161 1.1 eeh .align 4
162 1.2 eeh .global _rtld_bind_start_1
163 1.2 eeh .type _rtld_bind_start_1,@function
164 1.11 mycroft _rtld_bind_start_1: # (y, x)
165 1.11 mycroft ldx [%o0 + (2*4)], %o0 /* Load object pointer from PLT2 */
166 1.18 riastrad
167 1.1 eeh call _rtld_bind /* Call _rtld_bind(obj, offset) */
168 1.11 mycroft srax %o1, 15, %o1 /* %o1 is the index to our PLT slot */
169 1.2 eeh
170 1.1 eeh jmp %o0 /* return value == function address */
171 1.1 eeh restore /* Dump our stack frame */
172