mdreloc.c revision 1.26 1 1.26 mycroft /* $NetBSD: mdreloc.c,v 1.26 2002/09/25 16:35:08 mycroft Exp $ */
2 1.1 eeh
3 1.1 eeh /*-
4 1.1 eeh * Copyright (c) 2000 Eduardo Horvath.
5 1.23 mycroft * Copyright (c) 1999, 2002 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.1 eeh * by Paul Kranenburg.
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 * 3. All advertising materials mentioning features or use of this software
20 1.1 eeh * must display the following acknowledgement:
21 1.1 eeh * This product includes software developed by the NetBSD
22 1.1 eeh * Foundation, Inc. and its contributors.
23 1.1 eeh * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 eeh * contributors may be used to endorse or promote products derived
25 1.1 eeh * from this software without specific prior written permission.
26 1.1 eeh *
27 1.1 eeh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 eeh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 eeh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 eeh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 eeh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 eeh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 eeh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 eeh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 eeh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 eeh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 eeh * POSSIBILITY OF SUCH DAMAGE.
38 1.1 eeh */
39 1.1 eeh
40 1.1 eeh #include <errno.h>
41 1.1 eeh #include <stdio.h>
42 1.1 eeh #include <stdlib.h>
43 1.1 eeh #include <string.h>
44 1.1 eeh #include <unistd.h>
45 1.1 eeh #include <sys/stat.h>
46 1.1 eeh
47 1.1 eeh #include "rtldenv.h"
48 1.1 eeh #include "debug.h"
49 1.1 eeh #include "rtld.h"
50 1.1 eeh
51 1.1 eeh /*
52 1.1 eeh * The following table holds for each relocation type:
53 1.1 eeh * - the width in bits of the memory location the relocation
54 1.1 eeh * applies to (not currently used)
55 1.1 eeh * - the number of bits the relocation value must be shifted to the
56 1.1 eeh * right (i.e. discard least significant bits) to fit into
57 1.1 eeh * the appropriate field in the instruction word.
58 1.1 eeh * - flags indicating whether
59 1.1 eeh * * the relocation involves a symbol
60 1.1 eeh * * the relocation is relative to the current position
61 1.1 eeh * * the relocation is for a GOT entry
62 1.1 eeh * * the relocation is relative to the load address
63 1.1 eeh *
64 1.1 eeh */
65 1.1 eeh #define _RF_S 0x80000000 /* Resolve symbol */
66 1.1 eeh #define _RF_A 0x40000000 /* Use addend */
67 1.1 eeh #define _RF_P 0x20000000 /* Location relative */
68 1.1 eeh #define _RF_G 0x10000000 /* GOT offset */
69 1.1 eeh #define _RF_B 0x08000000 /* Load address relative */
70 1.2 eeh #define _RF_U 0x04000000 /* Unaligned */
71 1.1 eeh #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
72 1.1 eeh #define _RF_RS(s) ( (s) & 0xff) /* right shift */
73 1.16 mycroft static const int reloc_target_flags[] = {
74 1.1 eeh 0, /* NONE */
75 1.1 eeh _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
76 1.1 eeh _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
77 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
78 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
79 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
80 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
81 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
82 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
83 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
84 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
85 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
86 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
87 1.1 eeh _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
88 1.1 eeh _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
89 1.1 eeh _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
90 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
91 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
92 1.1 eeh _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
93 1.1 eeh _RF_SZ(32) | _RF_RS(0), /* COPY */
94 1.1 eeh _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */
95 1.1 eeh _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
96 1.1 eeh _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */
97 1.2 eeh _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
98 1.1 eeh
99 1.1 eeh _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */
100 1.1 eeh _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */
101 1.1 eeh _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
102 1.1 eeh _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */
103 1.1 eeh _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */
104 1.1 eeh _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */
105 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */
106 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */
107 1.1 eeh _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */
108 1.1 eeh _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */
109 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */
110 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */
111 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */
112 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */
113 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */
114 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */
115 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */
116 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */
117 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */
118 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */
119 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */
120 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */
121 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */
122 1.1 eeh _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */
123 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */
124 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */
125 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */
126 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */
127 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */
128 1.1 eeh _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */
129 1.2 eeh _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */
130 1.2 eeh _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */
131 1.1 eeh };
132 1.1 eeh
133 1.1 eeh #ifdef RTLD_DEBUG_RELOC
134 1.1 eeh static const char *reloc_names[] = {
135 1.1 eeh "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
136 1.1 eeh "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
137 1.1 eeh "22", "13", "LO10", "GOT10", "GOT13",
138 1.1 eeh "GOT22", "PC10", "PC22", "WPLT30", "COPY",
139 1.1 eeh "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
140 1.1 eeh "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
141 1.1 eeh "10", "11", "64", "OLO10", "HH22",
142 1.1 eeh "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
143 1.1 eeh "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
144 1.1 eeh "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
145 1.1 eeh "L44", "REGISTER", "UA64", "UA16"
146 1.1 eeh };
147 1.1 eeh #endif
148 1.1 eeh
149 1.1 eeh #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
150 1.1 eeh #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
151 1.1 eeh #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
152 1.2 eeh #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0)
153 1.2 eeh #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
154 1.1 eeh #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
155 1.1 eeh #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
156 1.1 eeh
157 1.16 mycroft static const long reloc_target_bitmask[] = {
158 1.1 eeh #define _BM(x) (~(-(1ULL << (x))))
159 1.1 eeh 0, /* NONE */
160 1.1 eeh _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
161 1.1 eeh _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
162 1.1 eeh _BM(30), _BM(22), /* WDISP30, WDISP22 */
163 1.1 eeh _BM(22), _BM(22), /* HI22, _22 */
164 1.1 eeh _BM(13), _BM(10), /* RELOC_13, _LO10 */
165 1.1 eeh _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
166 1.1 eeh _BM(10), _BM(22), /* _PC10, _PC22 */
167 1.1 eeh _BM(30), 0, /* _WPLT30, _COPY */
168 1.1 eeh _BM(32), _BM(32), _BM(32), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
169 1.1 eeh _BM(32), _BM(32), /* _UA32, PLT32 */
170 1.1 eeh _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */
171 1.1 eeh _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */
172 1.1 eeh _BM(10), _BM(11), -1, /* _10, _11, _64 */
173 1.1 eeh _BM(10), _BM(22), /* _OLO10, _HH22 */
174 1.1 eeh _BM(10), _BM(22), /* _HM10, _LM22 */
175 1.1 eeh _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */
176 1.1 eeh _BM(16), _BM(19), /* _WDISP16, _WDISP19 */
177 1.1 eeh -1, /* GLOB_JMP */
178 1.1 eeh _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */
179 1.1 eeh -1, -1, /* DISP64, PLT64 */
180 1.1 eeh _BM(22), _BM(13), /* HIX22, LOX10 */
181 1.1 eeh _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */
182 1.1 eeh -1, -1, _BM(16), /* REGISTER, UA64, UA16 */
183 1.1 eeh #undef _BM
184 1.1 eeh };
185 1.1 eeh #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
186 1.1 eeh
187 1.1 eeh /*
188 1.1 eeh * Instruction templates:
189 1.1 eeh */
190 1.1 eeh #define BAA 0x10400000 /* ba,a %xcc, 0 */
191 1.1 eeh #define SETHI 0x03000000 /* sethi %hi(0), %g1 */
192 1.1 eeh #define JMP 0x81c06000 /* jmpl %g1+%lo(0), %g0 */
193 1.1 eeh #define NOP 0x01000000 /* sethi %hi(0), %g0 */
194 1.1 eeh #define OR 0x82806000 /* or %g1, 0, %g1 */
195 1.1 eeh #define XOR 0x82c06000 /* xor %g1, 0, %g1 */
196 1.1 eeh #define MOV71 0x8283a000 /* or %o7, 0, %g1 */
197 1.1 eeh #define MOV17 0x9c806000 /* or %g1, 0, %o7 */
198 1.1 eeh #define CALL 0x40000000 /* call 0 */
199 1.1 eeh #define SLLX 0x8b407000 /* sllx %g1, 0, %g1 */
200 1.1 eeh #define SETHIG5 0x0b000000 /* sethi %hi(0), %g5 */
201 1.1 eeh #define ORG5 0x82804005 /* or %g1, %g5, %g1 */
202 1.1 eeh
203 1.1 eeh
204 1.26 mycroft /* %hi(v)/%lo(v) with variable shift */
205 1.26 mycroft #define HIVAL(v, s) (((v) >> (s)) & 0x003fffff)
206 1.26 mycroft #define LOVAL(v, s) (((v) >> (s)) & 0x000003ff)
207 1.1 eeh
208 1.20 mycroft void _rtld_bind_start_0(long, long);
209 1.20 mycroft void _rtld_bind_start_1(long, long);
210 1.18 mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
211 1.23 mycroft caddr_t _rtld_bind __P((const Obj_Entry *, Elf_Word));
212 1.1 eeh
213 1.1 eeh /*
214 1.1 eeh * Install rtld function call into this PLT slot.
215 1.1 eeh */
216 1.1 eeh #define SAVE 0x9de3bf50
217 1.1 eeh #define SETHI_l0 0x21000000
218 1.1 eeh #define SETHI_l1 0x23000000
219 1.1 eeh #define OR_l0_l0 0xa0142000
220 1.1 eeh #define SLLX_l0_32_l0 0xa12c3020
221 1.1 eeh #define OR_l0_l1_l0 0xa0140011
222 1.26 mycroft #define JMPL_l0_o0 0x91c42000
223 1.26 mycroft #define MOV_g1_o1 0x92100001
224 1.1 eeh
225 1.3 mycroft void _rtld_install_plt __P((Elf_Word *pltgot, Elf_Addr proc));
226 1.1 eeh
227 1.1 eeh void
228 1.1 eeh _rtld_install_plt(pltgot, proc)
229 1.3 mycroft Elf_Word *pltgot;
230 1.1 eeh Elf_Addr proc;
231 1.1 eeh {
232 1.1 eeh pltgot[0] = SAVE;
233 1.1 eeh pltgot[1] = SETHI_l0 | HIVAL(proc, 42);
234 1.1 eeh pltgot[2] = SETHI_l1 | HIVAL(proc, 10);
235 1.26 mycroft pltgot[3] = OR_l0_l0 | LOVAL(proc, 32);
236 1.1 eeh pltgot[4] = SLLX_l0_32_l0;
237 1.1 eeh pltgot[5] = OR_l0_l1_l0;
238 1.26 mycroft pltgot[6] = JMPL_l0_o0 | LOVAL(proc, 0);
239 1.26 mycroft pltgot[7] = MOV_g1_o1;
240 1.1 eeh }
241 1.2 eeh
242 1.25 mycroft #if 0
243 1.2 eeh long _rtld_bind_start_0_stub __P((long x, long y));
244 1.2 eeh long
245 1.26 mycroft _rtld_bind_start_0_stub(y, x)
246 1.26 mycroft long y, x;
247 1.2 eeh {
248 1.2 eeh long i;
249 1.2 eeh long n;
250 1.2 eeh
251 1.26 mycroft i = x - y + 8 - 32768*32;
252 1.2 eeh n = 32768 + (i/5120)*160 + (i%5120)/24;
253 1.2 eeh
254 1.2 eeh return (n);
255 1.2 eeh }
256 1.25 mycroft #endif
257 1.2 eeh
258 1.6 mycroft void
259 1.6 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
260 1.6 mycroft {
261 1.6 mycroft /*
262 1.6 mycroft * On sparc64 we got troubles.
263 1.6 mycroft *
264 1.6 mycroft * Instructions are 4 bytes long.
265 1.6 mycroft * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
266 1.6 mycroft * array entries.
267 1.6 mycroft * Each PLT entry jumps to PLT0 to enter the dynamic
268 1.6 mycroft * linker.
269 1.6 mycroft * Loading an arbitrary 64-bit pointer takes 6
270 1.6 mycroft * instructions and 2 registers.
271 1.6 mycroft *
272 1.6 mycroft * Somehow we need to issue a save to get a new stack
273 1.6 mycroft * frame, load the address of the dynamic linker, and
274 1.6 mycroft * jump there, in 8 instructions or less.
275 1.6 mycroft *
276 1.6 mycroft * Oh, we need to fill out both PLT0 and PLT1.
277 1.6 mycroft */
278 1.6 mycroft {
279 1.6 mycroft Elf_Word *entry = (Elf_Word *)obj->pltgot;
280 1.6 mycroft
281 1.6 mycroft /* Install in entries 0 and 1 */
282 1.6 mycroft _rtld_install_plt(&entry[0], (Elf_Addr) &_rtld_bind_start_0);
283 1.6 mycroft _rtld_install_plt(&entry[8], (Elf_Addr) &_rtld_bind_start_1);
284 1.6 mycroft
285 1.6 mycroft /*
286 1.6 mycroft * Install the object reference in first slot
287 1.6 mycroft * of entry 2.
288 1.6 mycroft */
289 1.6 mycroft obj->pltgot[8] = (Elf_Addr) obj;
290 1.6 mycroft }
291 1.8 mycroft }
292 1.8 mycroft
293 1.18 mycroft void
294 1.18 mycroft _rtld_relocate_nonplt_self(dynp, relocbase)
295 1.18 mycroft Elf_Dyn *dynp;
296 1.18 mycroft Elf_Addr relocbase;
297 1.18 mycroft {
298 1.18 mycroft const Elf_Rela *rela = 0, *relalim;
299 1.18 mycroft Elf_Addr relasz = 0;
300 1.18 mycroft Elf_Addr *where;
301 1.18 mycroft
302 1.18 mycroft for (; dynp->d_tag != DT_NULL; dynp++) {
303 1.18 mycroft switch (dynp->d_tag) {
304 1.18 mycroft case DT_RELA:
305 1.18 mycroft rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
306 1.18 mycroft break;
307 1.18 mycroft case DT_RELASZ:
308 1.18 mycroft relasz = dynp->d_un.d_val;
309 1.18 mycroft break;
310 1.18 mycroft }
311 1.18 mycroft }
312 1.18 mycroft relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
313 1.18 mycroft for (; rela < relalim; rela++) {
314 1.18 mycroft where = (Elf_Addr *)(relocbase + rela->r_offset);
315 1.18 mycroft *where = (Elf_Addr)(relocbase + rela->r_addend);
316 1.18 mycroft }
317 1.18 mycroft }
318 1.18 mycroft
319 1.8 mycroft int
320 1.21 mycroft _rtld_relocate_nonplt_objects(obj, self)
321 1.14 mycroft const Obj_Entry *obj;
322 1.15 mycroft bool self;
323 1.8 mycroft {
324 1.9 mycroft const Elf_Rela *rela;
325 1.8 mycroft
326 1.18 mycroft if (self)
327 1.18 mycroft return (0);
328 1.18 mycroft
329 1.9 mycroft for (rela = obj->rela; rela < obj->relalim; rela++) {
330 1.9 mycroft Elf_Addr *where;
331 1.9 mycroft Elf_Word type;
332 1.9 mycroft Elf_Addr value = 0, mask;
333 1.9 mycroft const Elf_Sym *def = NULL;
334 1.9 mycroft const Obj_Entry *defobj = NULL;
335 1.10 mycroft unsigned long symnum;
336 1.9 mycroft
337 1.9 mycroft where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
338 1.10 mycroft symnum = ELF_R_SYM(rela->r_info);
339 1.9 mycroft
340 1.9 mycroft type = ELF_R_TYPE(rela->r_info);
341 1.9 mycroft if (type == R_TYPE(NONE))
342 1.12 mycroft continue;
343 1.9 mycroft
344 1.23 mycroft /* We do JMP_SLOTs in _rtld_bind() below */
345 1.9 mycroft if (type == R_TYPE(JMP_SLOT))
346 1.12 mycroft continue;
347 1.9 mycroft
348 1.9 mycroft /* COPY relocs are also handled elsewhere */
349 1.9 mycroft if (type == R_TYPE(COPY))
350 1.12 mycroft continue;
351 1.8 mycroft
352 1.9 mycroft /*
353 1.9 mycroft * We use the fact that relocation types are an `enum'
354 1.9 mycroft * Note: R_SPARC_UA16 is currently numerically largest.
355 1.9 mycroft */
356 1.9 mycroft if (type > R_TYPE(UA16))
357 1.9 mycroft return (-1);
358 1.8 mycroft
359 1.9 mycroft value = rela->r_addend;
360 1.8 mycroft
361 1.9 mycroft /*
362 1.18 mycroft * Handle relative relocs here, as an optimization.
363 1.9 mycroft */
364 1.17 mycroft if (type == R_TYPE(RELATIVE)) {
365 1.9 mycroft *where = (Elf_Addr)(obj->relocbase + value);
366 1.21 mycroft rdbg(("RELATIVE in %s --> %p", obj->path,
367 1.18 mycroft (void *)*where));
368 1.12 mycroft continue;
369 1.9 mycroft }
370 1.8 mycroft
371 1.9 mycroft if (RELOC_RESOLVE_SYMBOL(type)) {
372 1.8 mycroft
373 1.9 mycroft /* Find the symbol */
374 1.10 mycroft def = _rtld_find_symdef(symnum, obj, &defobj, false);
375 1.9 mycroft if (def == NULL)
376 1.9 mycroft return (-1);
377 1.8 mycroft
378 1.9 mycroft /* Add in the symbol's absolute address */
379 1.9 mycroft value += (Elf_Addr)(defobj->relocbase + def->st_value);
380 1.9 mycroft }
381 1.8 mycroft
382 1.9 mycroft if (RELOC_PC_RELATIVE(type)) {
383 1.9 mycroft value -= (Elf_Addr)where;
384 1.9 mycroft }
385 1.8 mycroft
386 1.9 mycroft if (RELOC_BASE_RELATIVE(type)) {
387 1.9 mycroft /*
388 1.9 mycroft * Note that even though sparcs use `Elf_rela'
389 1.9 mycroft * exclusively we still need the implicit memory addend
390 1.9 mycroft * in relocations referring to GOT entries.
391 1.9 mycroft * Undoubtedly, someone f*cked this up in the distant
392 1.9 mycroft * past, and now we're stuck with it in the name of
393 1.9 mycroft * compatibility for all eternity..
394 1.9 mycroft *
395 1.9 mycroft * In any case, the implicit and explicit should be
396 1.9 mycroft * mutually exclusive. We provide a check for that
397 1.9 mycroft * here.
398 1.9 mycroft */
399 1.8 mycroft #ifdef DIAGNOSTIC
400 1.9 mycroft if (value != 0 && *where != 0) {
401 1.9 mycroft xprintf("BASE_REL(%s): where=%p, *where 0x%lx, "
402 1.9 mycroft "addend=0x%lx, base %p\n",
403 1.9 mycroft obj->path, where, *where,
404 1.9 mycroft rela->r_addend, obj->relocbase);
405 1.9 mycroft }
406 1.9 mycroft #endif
407 1.9 mycroft /* XXXX -- apparently we ignore the preexisting value */
408 1.9 mycroft value += (Elf_Addr)(obj->relocbase);
409 1.8 mycroft }
410 1.8 mycroft
411 1.9 mycroft mask = RELOC_VALUE_BITMASK(type);
412 1.9 mycroft value >>= RELOC_VALUE_RIGHTSHIFT(type);
413 1.9 mycroft value &= mask;
414 1.9 mycroft
415 1.9 mycroft if (RELOC_UNALIGNED(type)) {
416 1.9 mycroft /* Handle unaligned relocations. */
417 1.9 mycroft Elf_Addr tmp = 0;
418 1.9 mycroft char *ptr = (char *)where;
419 1.9 mycroft int i, size = RELOC_TARGET_SIZE(type)/8;
420 1.9 mycroft
421 1.9 mycroft /* Read it in one byte at a time. */
422 1.9 mycroft for (i=0; i<size; i++)
423 1.9 mycroft tmp = (tmp << 8) | ptr[i];
424 1.9 mycroft
425 1.9 mycroft tmp &= ~mask;
426 1.9 mycroft tmp |= value;
427 1.9 mycroft
428 1.9 mycroft /* Write it back out. */
429 1.9 mycroft for (i=0; i<size; i++)
430 1.9 mycroft ptr[i] = ((tmp >> (8*i)) & 0xff);
431 1.8 mycroft #ifdef RTLD_DEBUG_RELOC
432 1.9 mycroft value = (Elf_Addr)tmp;
433 1.8 mycroft #endif
434 1.8 mycroft
435 1.9 mycroft } else if (RELOC_TARGET_SIZE(type) > 32) {
436 1.9 mycroft *where &= ~mask;
437 1.9 mycroft *where |= value;
438 1.8 mycroft #ifdef RTLD_DEBUG_RELOC
439 1.9 mycroft value = (Elf_Addr)*where;
440 1.8 mycroft #endif
441 1.9 mycroft } else {
442 1.9 mycroft Elf32_Addr *where32 = (Elf32_Addr *)where;
443 1.8 mycroft
444 1.9 mycroft *where32 &= ~mask;
445 1.9 mycroft *where32 |= value;
446 1.8 mycroft #ifdef RTLD_DEBUG_RELOC
447 1.9 mycroft value = (Elf_Addr)*where32;
448 1.8 mycroft #endif
449 1.9 mycroft }
450 1.8 mycroft
451 1.8 mycroft #ifdef RTLD_DEBUG_RELOC
452 1.9 mycroft if (RELOC_RESOLVE_SYMBOL(type)) {
453 1.21 mycroft rdbg(("%s %s in %s --> %p in %s", reloc_names[type],
454 1.11 mycroft obj->strtab + obj->symtab[symnum].st_name,
455 1.18 mycroft obj->path, (void *)*where, defobj->path));
456 1.11 mycroft } else {
457 1.21 mycroft rdbg(("%s in %s --> %p", reloc_names[type],
458 1.18 mycroft obj->path, (void *)*where));
459 1.9 mycroft }
460 1.9 mycroft #endif
461 1.8 mycroft }
462 1.13 mycroft return (0);
463 1.13 mycroft }
464 1.13 mycroft
465 1.13 mycroft int
466 1.21 mycroft _rtld_relocate_plt_lazy(obj)
467 1.14 mycroft const Obj_Entry *obj;
468 1.13 mycroft {
469 1.8 mycroft return (0);
470 1.23 mycroft }
471 1.23 mycroft
472 1.23 mycroft caddr_t
473 1.23 mycroft _rtld_bind(obj, reloff)
474 1.23 mycroft const Obj_Entry *obj;
475 1.23 mycroft Elf_Word reloff;
476 1.23 mycroft {
477 1.24 mycroft const Elf_Rela *rela = obj->pltrela + reloff;
478 1.23 mycroft const Elf_Sym *def;
479 1.23 mycroft const Obj_Entry *defobj;
480 1.23 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
481 1.23 mycroft Elf_Addr value, offset;
482 1.23 mycroft
483 1.23 mycroft if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
484 1.23 mycroft /*
485 1.23 mycroft * XXXX
486 1.23 mycroft *
487 1.23 mycroft * The first four PLT entries are reserved. There is some
488 1.23 mycroft * disagreement whether they should have associated relocation
489 1.23 mycroft * entries. Both the SPARC 32-bit and 64-bit ELF
490 1.23 mycroft * specifications say that they should have relocation entries,
491 1.23 mycroft * but the 32-bit SPARC binutils do not generate them, and now
492 1.23 mycroft * the 64-bit SPARC binutils have stopped generating them too.
493 1.23 mycroft *
494 1.23 mycroft * So, to provide binary compatibility, we will check the first
495 1.23 mycroft * entry, if it is reserved it should not be of the type
496 1.23 mycroft * JMP_SLOT. If it is JMP_SLOT, then the 4 reserved entries
497 1.23 mycroft * were not generated and our index is 4 entries too far.
498 1.23 mycroft */
499 1.23 mycroft rela -= 4;
500 1.23 mycroft }
501 1.23 mycroft
502 1.23 mycroft /* Fully resolve procedure addresses now */
503 1.23 mycroft
504 1.23 mycroft assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
505 1.23 mycroft
506 1.23 mycroft def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
507 1.23 mycroft if (def == NULL)
508 1.23 mycroft _rtld_die();
509 1.23 mycroft
510 1.23 mycroft value = (Elf_Addr)(defobj->relocbase + def->st_value);
511 1.23 mycroft rdbg(("bind now/fixup in %s --> old=%p new=%p",
512 1.23 mycroft defobj->strtab + def->st_name, (void *)*where, (void *)value));
513 1.23 mycroft
514 1.23 mycroft /*
515 1.23 mycroft * At the PLT entry pointed at by `where', we now construct
516 1.23 mycroft * a direct transfer to the now fully resolved function
517 1.23 mycroft * address.
518 1.23 mycroft *
519 1.23 mycroft * A PLT entry is supposed to start by looking like this:
520 1.23 mycroft *
521 1.23 mycroft * sethi %hi(. - .PLT0), %g1
522 1.23 mycroft * ba,a %xcc, .PLT1
523 1.23 mycroft * nop
524 1.23 mycroft * nop
525 1.23 mycroft * nop
526 1.23 mycroft * nop
527 1.23 mycroft * nop
528 1.23 mycroft * nop
529 1.23 mycroft *
530 1.23 mycroft * When we replace these entries we start from the second
531 1.23 mycroft * entry and do it in reverse order so the last thing we
532 1.23 mycroft * do is replace the branch. That allows us to change this
533 1.23 mycroft * atomically.
534 1.23 mycroft *
535 1.23 mycroft * We now need to find out how far we need to jump. We
536 1.23 mycroft * have a choice of several different relocation techniques
537 1.23 mycroft * which are increasingly expensive.
538 1.23 mycroft */
539 1.23 mycroft
540 1.23 mycroft offset = ((Elf_Addr)where) - value;
541 1.23 mycroft if (rela->r_addend) {
542 1.23 mycroft Elf_Addr *ptr = (Elf_Addr *)where;
543 1.23 mycroft /*
544 1.23 mycroft * This entry is >32768. Just replace the pointer.
545 1.23 mycroft */
546 1.23 mycroft ptr[0] = value;
547 1.23 mycroft
548 1.23 mycroft } else if (offset <= (1L<<20) && offset >= -(1L<<20)) {
549 1.23 mycroft /*
550 1.23 mycroft * We're within 1MB -- we can use a direct branch insn.
551 1.23 mycroft *
552 1.23 mycroft * We can generate this pattern:
553 1.23 mycroft *
554 1.23 mycroft * sethi %hi(. - .PLT0), %g1
555 1.23 mycroft * ba,a %xcc, addr
556 1.23 mycroft * nop
557 1.23 mycroft * nop
558 1.23 mycroft * nop
559 1.23 mycroft * nop
560 1.23 mycroft * nop
561 1.23 mycroft * nop
562 1.23 mycroft *
563 1.23 mycroft */
564 1.23 mycroft where[1] = BAA | ((offset >> 2) &0x3fffff);
565 1.23 mycroft __asm __volatile("iflush %0+4" : : "r" (where));
566 1.23 mycroft } else if (value >= 0 && value < (1L<<32)) {
567 1.23 mycroft /*
568 1.26 mycroft * We're within 32-bits of address zero.
569 1.23 mycroft *
570 1.23 mycroft * The resulting code in the jump slot is:
571 1.23 mycroft *
572 1.23 mycroft * sethi %hi(. - .PLT0), %g1
573 1.23 mycroft * sethi %hi(addr), %g1
574 1.23 mycroft * jmp %g1+%lo(addr)
575 1.23 mycroft * nop
576 1.23 mycroft * nop
577 1.23 mycroft * nop
578 1.23 mycroft * nop
579 1.23 mycroft * nop
580 1.23 mycroft *
581 1.23 mycroft */
582 1.26 mycroft where[2] = JMP | LOVAL(value, 0);
583 1.23 mycroft where[1] = SETHI | HIVAL(value, 10);
584 1.23 mycroft __asm __volatile("iflush %0+8" : : "r" (where));
585 1.23 mycroft __asm __volatile("iflush %0+4" : : "r" (where));
586 1.23 mycroft
587 1.23 mycroft } else if (value <= 0 && value > -(1L<<32)) {
588 1.23 mycroft /*
589 1.26 mycroft * We're within 32-bits of address -1.
590 1.23 mycroft *
591 1.23 mycroft * The resulting code in the jump slot is:
592 1.23 mycroft *
593 1.23 mycroft * sethi %hi(. - .PLT0), %g1
594 1.23 mycroft * sethi %hix(addr), %g1
595 1.23 mycroft * xor %g1, %lox(addr), %g1
596 1.23 mycroft * jmp %g1
597 1.23 mycroft * nop
598 1.23 mycroft * nop
599 1.23 mycroft * nop
600 1.23 mycroft * nop
601 1.23 mycroft *
602 1.23 mycroft */
603 1.23 mycroft where[3] = JMP;
604 1.23 mycroft where[2] = XOR | ((~value) & 0x00001fff);
605 1.23 mycroft where[1] = SETHI | HIVAL(~value, 10);
606 1.23 mycroft __asm __volatile("iflush %0+12" : : "r" (where));
607 1.23 mycroft __asm __volatile("iflush %0+8" : : "r" (where));
608 1.23 mycroft __asm __volatile("iflush %0+4" : : "r" (where));
609 1.23 mycroft
610 1.23 mycroft } else if (offset <= (1L<<32) && offset >= -((1L<<32) - 4)) {
611 1.23 mycroft /*
612 1.26 mycroft * We're within 32-bits -- we can use a direct call insn
613 1.23 mycroft *
614 1.23 mycroft * The resulting code in the jump slot is:
615 1.23 mycroft *
616 1.23 mycroft * sethi %hi(. - .PLT0), %g1
617 1.23 mycroft * mov %o7, %g1
618 1.23 mycroft * call (.+offset)
619 1.23 mycroft * mov %g1, %o7
620 1.23 mycroft * nop
621 1.23 mycroft * nop
622 1.23 mycroft * nop
623 1.23 mycroft * nop
624 1.23 mycroft *
625 1.23 mycroft */
626 1.23 mycroft where[3] = MOV17;
627 1.23 mycroft where[2] = CALL | ((offset >> 4) & 0x3fffffff);
628 1.23 mycroft where[1] = MOV71;
629 1.23 mycroft __asm __volatile("iflush %0+12" : : "r" (where));
630 1.23 mycroft __asm __volatile("iflush %0+8" : : "r" (where));
631 1.23 mycroft __asm __volatile("iflush %0+4" : : "r" (where));
632 1.23 mycroft
633 1.23 mycroft } else if (offset >= 0 && offset < (1L<<44)) {
634 1.23 mycroft /*
635 1.26 mycroft * We're within 44 bits. We can generate this pattern:
636 1.23 mycroft *
637 1.23 mycroft * The resulting code in the jump slot is:
638 1.23 mycroft *
639 1.23 mycroft * sethi %hi(. - .PLT0), %g1
640 1.23 mycroft * sethi %h44(addr), %g1
641 1.23 mycroft * or %g1, %m44(addr), %g1
642 1.23 mycroft * sllx %g1, 12, %g1
643 1.23 mycroft * jmp %g1+%l44(addr)
644 1.23 mycroft * nop
645 1.23 mycroft * nop
646 1.23 mycroft * nop
647 1.23 mycroft *
648 1.23 mycroft */
649 1.26 mycroft where[4] = JMP | LOVAL(offset, 0);
650 1.23 mycroft where[3] = SLLX | 12;
651 1.23 mycroft where[2] = OR | (((offset) >> 12) & 0x00001fff);
652 1.23 mycroft where[1] = SETHI | HIVAL(offset, 22);
653 1.23 mycroft __asm __volatile("iflush %0+16" : : "r" (where));
654 1.23 mycroft __asm __volatile("iflush %0+12" : : "r" (where));
655 1.23 mycroft __asm __volatile("iflush %0+8" : : "r" (where));
656 1.23 mycroft __asm __volatile("iflush %0+4" : : "r" (where));
657 1.23 mycroft
658 1.23 mycroft } else if (offset < 0 && offset > -(1L<<44)) {
659 1.23 mycroft /*
660 1.26 mycroft * We're within 44 bits. We can generate this pattern:
661 1.23 mycroft *
662 1.23 mycroft * The resulting code in the jump slot is:
663 1.23 mycroft *
664 1.23 mycroft * sethi %hi(. - .PLT0), %g1
665 1.23 mycroft * sethi %h44(-addr), %g1
666 1.23 mycroft * xor %g1, %m44(-addr), %g1
667 1.23 mycroft * sllx %g1, 12, %g1
668 1.23 mycroft * jmp %g1+%l44(addr)
669 1.23 mycroft * nop
670 1.23 mycroft * nop
671 1.23 mycroft * nop
672 1.23 mycroft *
673 1.23 mycroft */
674 1.26 mycroft where[4] = JMP | LOVAL(offset, 0);
675 1.23 mycroft where[3] = SLLX | 12;
676 1.23 mycroft where[2] = XOR | (((~offset) >> 12) & 0x00001fff);
677 1.23 mycroft where[1] = SETHI | HIVAL(~offset, 22);
678 1.23 mycroft __asm __volatile("iflush %0+16" : : "r" (where));
679 1.23 mycroft __asm __volatile("iflush %0+12" : : "r" (where));
680 1.23 mycroft __asm __volatile("iflush %0+8" : : "r" (where));
681 1.23 mycroft __asm __volatile("iflush %0+4" : : "r" (where));
682 1.23 mycroft
683 1.23 mycroft } else {
684 1.23 mycroft /*
685 1.23 mycroft * We need to load all 64-bits
686 1.23 mycroft *
687 1.23 mycroft * The resulting code in the jump slot is:
688 1.23 mycroft *
689 1.23 mycroft * sethi %hi(. - .PLT0), %g1
690 1.23 mycroft * sethi %hh(addr), %g1
691 1.23 mycroft * sethi %lm(addr), %g5
692 1.23 mycroft * or %g1, %hm(addr), %g1
693 1.23 mycroft * sllx %g1, 32, %g1
694 1.23 mycroft * or %g1, %g5, %g1
695 1.23 mycroft * jmp %g1+%lo(addr)
696 1.23 mycroft * nop
697 1.23 mycroft *
698 1.23 mycroft */
699 1.26 mycroft where[6] = JMP | LOVAL(value, 0);
700 1.23 mycroft where[5] = ORG5;
701 1.26 mycroft where[4] = SLLX | 32;
702 1.26 mycroft where[3] = OR | LOVAL(value, 32);
703 1.23 mycroft where[2] = SETHIG5 | HIVAL(value, 10);
704 1.23 mycroft where[1] = SETHI | HIVAL(value, 42);
705 1.26 mycroft __asm __volatile("iflush %0+24" : : "r" (where));
706 1.23 mycroft __asm __volatile("iflush %0+20" : : "r" (where));
707 1.23 mycroft __asm __volatile("iflush %0+16" : : "r" (where));
708 1.23 mycroft __asm __volatile("iflush %0+12" : : "r" (where));
709 1.23 mycroft __asm __volatile("iflush %0+8" : : "r" (where));
710 1.23 mycroft __asm __volatile("iflush %0+4" : : "r" (where));
711 1.23 mycroft
712 1.23 mycroft }
713 1.23 mycroft
714 1.23 mycroft return (caddr_t)value;
715 1.6 mycroft }
716