mdreloc.c revision 1.1 1 1.1 eeh /* $NetBSD: mdreloc.c,v 1.1 2000/07/13 23:14:18 eeh Exp $ */
2 1.1 eeh
3 1.1 eeh /*-
4 1.1 eeh * Copyright (c) 2000 Eduardo Horvath.
5 1.1 eeh * Copyright (c) 1999 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.1 eeh #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
71 1.1 eeh #define _RF_RS(s) ( (s) & 0xff) /* right shift */
72 1.1 eeh static int reloc_target_flags[] = {
73 1.1 eeh 0, /* NONE */
74 1.1 eeh _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
75 1.1 eeh _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
76 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
77 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
78 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
79 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
80 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
81 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
82 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
83 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
84 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
85 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
86 1.1 eeh _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
87 1.1 eeh _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
88 1.1 eeh _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
89 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
90 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
91 1.1 eeh _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
92 1.1 eeh _RF_SZ(32) | _RF_RS(0), /* COPY */
93 1.1 eeh _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */
94 1.1 eeh _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
95 1.1 eeh _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */
96 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
97 1.1 eeh
98 1.1 eeh _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */
99 1.1 eeh _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */
100 1.1 eeh _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
101 1.1 eeh _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */
102 1.1 eeh _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */
103 1.1 eeh _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */
104 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */
105 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */
106 1.1 eeh _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */
107 1.1 eeh _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */
108 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */
109 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */
110 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */
111 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */
112 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */
113 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */
114 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */
115 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */
116 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */
117 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */
118 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */
119 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */
120 1.1 eeh _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */
121 1.1 eeh _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */
122 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */
123 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */
124 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */
125 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */
126 1.1 eeh _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */
127 1.1 eeh _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */
128 1.1 eeh _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* UA64 */
129 1.1 eeh _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* UA16 */
130 1.1 eeh };
131 1.1 eeh
132 1.1 eeh #ifdef RTLD_DEBUG_RELOC
133 1.1 eeh static const char *reloc_names[] = {
134 1.1 eeh "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
135 1.1 eeh "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
136 1.1 eeh "22", "13", "LO10", "GOT10", "GOT13",
137 1.1 eeh "GOT22", "PC10", "PC22", "WPLT30", "COPY",
138 1.1 eeh "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32",
139 1.1 eeh "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32",
140 1.1 eeh "10", "11", "64", "OLO10", "HH22",
141 1.1 eeh "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22",
142 1.1 eeh "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6",
143 1.1 eeh "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
144 1.1 eeh "L44", "REGISTER", "UA64", "UA16"
145 1.1 eeh };
146 1.1 eeh #endif
147 1.1 eeh
148 1.1 eeh #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
149 1.1 eeh #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
150 1.1 eeh #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
151 1.1 eeh #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
152 1.1 eeh #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
153 1.1 eeh
154 1.1 eeh static long reloc_target_bitmask[] = {
155 1.1 eeh #define _BM(x) (~(-(1ULL << (x))))
156 1.1 eeh 0, /* NONE */
157 1.1 eeh _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
158 1.1 eeh _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
159 1.1 eeh _BM(30), _BM(22), /* WDISP30, WDISP22 */
160 1.1 eeh _BM(22), _BM(22), /* HI22, _22 */
161 1.1 eeh _BM(13), _BM(10), /* RELOC_13, _LO10 */
162 1.1 eeh _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
163 1.1 eeh _BM(10), _BM(22), /* _PC10, _PC22 */
164 1.1 eeh _BM(30), 0, /* _WPLT30, _COPY */
165 1.1 eeh _BM(32), _BM(32), _BM(32), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
166 1.1 eeh _BM(32), _BM(32), /* _UA32, PLT32 */
167 1.1 eeh _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */
168 1.1 eeh _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */
169 1.1 eeh _BM(10), _BM(11), -1, /* _10, _11, _64 */
170 1.1 eeh _BM(10), _BM(22), /* _OLO10, _HH22 */
171 1.1 eeh _BM(10), _BM(22), /* _HM10, _LM22 */
172 1.1 eeh _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */
173 1.1 eeh _BM(16), _BM(19), /* _WDISP16, _WDISP19 */
174 1.1 eeh -1, /* GLOB_JMP */
175 1.1 eeh _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */
176 1.1 eeh -1, -1, /* DISP64, PLT64 */
177 1.1 eeh _BM(22), _BM(13), /* HIX22, LOX10 */
178 1.1 eeh _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */
179 1.1 eeh -1, -1, _BM(16), /* REGISTER, UA64, UA16 */
180 1.1 eeh #undef _BM
181 1.1 eeh };
182 1.1 eeh #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
183 1.1 eeh
184 1.1 eeh
185 1.1 eeh int
186 1.1 eeh _rtld_relocate_nonplt_object(obj, rela, dodebug)
187 1.1 eeh Obj_Entry *obj;
188 1.1 eeh const Elf_RelA *rela;
189 1.1 eeh bool dodebug;
190 1.1 eeh {
191 1.1 eeh Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
192 1.1 eeh Elf_Word type, value, mask;
193 1.1 eeh const Elf_Sym *def = NULL;
194 1.1 eeh const Obj_Entry *defobj = NULL;
195 1.1 eeh
196 1.1 eeh type = ELF_R_TYPE(rela->r_info);
197 1.1 eeh if (type == R_TYPE(NONE))
198 1.1 eeh return (0);
199 1.1 eeh
200 1.1 eeh /* We do JMP_SLOTs in relocate_plt_object() below */
201 1.1 eeh if (type == R_TYPE(JMP_SLOT))
202 1.1 eeh return (0);
203 1.1 eeh
204 1.1 eeh /* COPY relocs are also handled elsewhere */
205 1.1 eeh if (type == R_TYPE(COPY))
206 1.1 eeh return (0);
207 1.1 eeh
208 1.1 eeh /*
209 1.1 eeh * We use the fact that relocation types are an `enum'
210 1.1 eeh * Note: R_SPARC_UA16 is currently numerically largest.
211 1.1 eeh */
212 1.1 eeh if (type > R_TYPE(UA16))
213 1.1 eeh return (-1);
214 1.1 eeh
215 1.1 eeh value = rela->r_addend;
216 1.1 eeh
217 1.1 eeh /*
218 1.1 eeh * Handle relative relocs here, because we might not
219 1.1 eeh * be able to access globals yet.
220 1.1 eeh */
221 1.1 eeh if (!dodebug && type == R_TYPE(RELATIVE)) {
222 1.1 eeh *where += (Elf_Addr)(obj->relocbase + value);
223 1.1 eeh return (0);
224 1.1 eeh }
225 1.1 eeh
226 1.1 eeh if (RELOC_RESOLVE_SYMBOL(type)) {
227 1.1 eeh
228 1.1 eeh /* Find the symbol */
229 1.1 eeh def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
230 1.1 eeh NULL, obj, &defobj, false);
231 1.1 eeh if (def == NULL)
232 1.1 eeh return (-1);
233 1.1 eeh
234 1.1 eeh /* Add in the symbol's absolute address */
235 1.1 eeh value += (Elf_Word)(defobj->relocbase + def->st_value);
236 1.1 eeh }
237 1.1 eeh
238 1.1 eeh if (RELOC_PC_RELATIVE(type)) {
239 1.1 eeh value -= (Elf_Word)where;
240 1.1 eeh }
241 1.1 eeh
242 1.1 eeh if (RELOC_BASE_RELATIVE(type)) {
243 1.1 eeh /*
244 1.1 eeh * Note that even though sparcs use `Elf_rela' exclusively
245 1.1 eeh * we still need the implicit memory addend in relocations
246 1.1 eeh * referring to GOT entries. Undoubtedly, someone f*cked
247 1.1 eeh * this up in the distant past, and now we're stuck with
248 1.1 eeh * it in the name of compatibility for all eternity..
249 1.1 eeh *
250 1.1 eeh * In any case, the implicit and explicit should be mutually
251 1.1 eeh * exclusive. We provide a check for that here.
252 1.1 eeh */
253 1.1 eeh #define DIAGNOSTIC
254 1.1 eeh #ifdef DIAGNOSTIC
255 1.1 eeh if (value != 0 && *where != 0) {
256 1.1 eeh xprintf("BASE_REL(%s): where=%p, *where 0x%lx, "
257 1.1 eeh "addend=0x%lx, base %p\n",
258 1.1 eeh obj->path, where, *where,
259 1.1 eeh rela->r_addend, obj->relocbase);
260 1.1 eeh }
261 1.1 eeh #endif
262 1.1 eeh value += (Elf_Word)(obj->relocbase + *where);
263 1.1 eeh }
264 1.1 eeh
265 1.1 eeh mask = RELOC_VALUE_BITMASK(type);
266 1.1 eeh value >>= RELOC_VALUE_RIGHTSHIFT(type);
267 1.1 eeh value &= mask;
268 1.1 eeh
269 1.1 eeh /* We ignore alignment restrictions here */
270 1.1 eeh if (RELOC_TARGET_SIZE(type) > 32) {
271 1.1 eeh *where &= ~mask;
272 1.1 eeh *where |= value;
273 1.1 eeh #ifdef RTLD_DEBUG_RELOC
274 1.1 eeh value = (Elf_Word)*where;
275 1.1 eeh #endif
276 1.1 eeh } else {
277 1.1 eeh Elf32_Addr *where32 = (Elf32_Addr *)where;
278 1.1 eeh
279 1.1 eeh *where32 &= ~mask;
280 1.1 eeh *where32 |= value;
281 1.1 eeh #ifdef RTLD_DEBUG_RELOC
282 1.1 eeh value = (Elf_Word)*where32;
283 1.1 eeh #endif
284 1.1 eeh }
285 1.1 eeh
286 1.1 eeh #ifdef RTLD_DEBUG_RELOC
287 1.1 eeh if (RELOC_RESOLVE_SYMBOL(type)) {
288 1.1 eeh rdbg(dodebug, ("%s %s in %s --> %p %s",
289 1.1 eeh reloc_names[type],
290 1.1 eeh defobj->strtab + def->st_name, obj->path,
291 1.1 eeh (void *)value, defobj->path));
292 1.1 eeh }
293 1.1 eeh else {
294 1.1 eeh rdbg(dodebug, ("%s --> %p", reloc_names[type],
295 1.1 eeh (void *)value));
296 1.1 eeh }
297 1.1 eeh #endif
298 1.1 eeh return (0);
299 1.1 eeh }
300 1.1 eeh
301 1.1 eeh /*
302 1.1 eeh * Instruction templates:
303 1.1 eeh */
304 1.1 eeh #define BAA 0x10400000 /* ba,a %xcc, 0 */
305 1.1 eeh #define SETHI 0x03000000 /* sethi %hi(0), %g1 */
306 1.1 eeh #define JMP 0x81c06000 /* jmpl %g1+%lo(0), %g0 */
307 1.1 eeh #define NOP 0x01000000 /* sethi %hi(0), %g0 */
308 1.1 eeh #define OR 0x82806000 /* or %g1, 0, %g1 */
309 1.1 eeh #define XOR 0x82c06000 /* xor %g1, 0, %g1 */
310 1.1 eeh #define MOV71 0x8283a000 /* or %o7, 0, %g1 */
311 1.1 eeh #define MOV17 0x9c806000 /* or %g1, 0, %o7 */
312 1.1 eeh #define CALL 0x40000000 /* call 0 */
313 1.1 eeh #define SLLX 0x8b407000 /* sllx %g1, 0, %g1 */
314 1.1 eeh #define SETHIG5 0x0b000000 /* sethi %hi(0), %g5 */
315 1.1 eeh #define ORG5 0x82804005 /* or %g1, %g5, %g1 */
316 1.1 eeh
317 1.1 eeh
318 1.1 eeh /* %hi(v) with variable shift */
319 1.1 eeh #define HIVAL(v, s) (((v) >> (s)) & 0x003fffff)
320 1.1 eeh #define LOVAL(v) ((v) & 0x000003ff)
321 1.1 eeh
322 1.1 eeh int
323 1.1 eeh _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
324 1.1 eeh Obj_Entry *obj;
325 1.1 eeh const Elf_RelA *rela;
326 1.1 eeh caddr_t *addrp;
327 1.1 eeh bool bind_now;
328 1.1 eeh bool dodebug;
329 1.1 eeh {
330 1.1 eeh const Elf_Sym *def;
331 1.1 eeh const Obj_Entry *defobj;
332 1.1 eeh Elf32_Word *where = (Elf32_Word *) (obj->relocbase + rela->r_offset);
333 1.1 eeh Elf_Addr value, offset;
334 1.1 eeh
335 1.1 eeh if (bind_now == 0 && obj->pltgot != NULL)
336 1.1 eeh return (0);
337 1.1 eeh
338 1.1 eeh /* Fully resolve procedure addresses now */
339 1.1 eeh
340 1.1 eeh assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
341 1.1 eeh
342 1.1 eeh def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
343 1.1 eeh NULL, obj, &defobj, true);
344 1.1 eeh if (def == NULL)
345 1.1 eeh return (-1);
346 1.1 eeh
347 1.1 eeh value = (Elf_Addr) (defobj->relocbase + def->st_value);
348 1.1 eeh rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
349 1.1 eeh (int)bind_now, defobj->strtab + def->st_name,
350 1.1 eeh (void *)*where, (void *)value));
351 1.1 eeh
352 1.1 eeh /*
353 1.1 eeh * At the PLT entry pointed at by `where', we now construct
354 1.1 eeh * a direct transfer to the now fully resolved function
355 1.1 eeh * address.
356 1.1 eeh *
357 1.1 eeh * A PLT entry is supposed to start by looking like this:
358 1.1 eeh *
359 1.1 eeh * sethi %hi(. - .PLT0), %g1
360 1.1 eeh * ba,a %xcc, .PLT1
361 1.1 eeh * nop
362 1.1 eeh * nop
363 1.1 eeh * nop
364 1.1 eeh * nop
365 1.1 eeh * nop
366 1.1 eeh * nop
367 1.1 eeh *
368 1.1 eeh * When we replace these entries we start from the second
369 1.1 eeh * entry and do it in reverse order so the last thing we
370 1.1 eeh * do is replace the branch. That allows us to change this
371 1.1 eeh * atomically.
372 1.1 eeh *
373 1.1 eeh * We now need to find out how far we need to jump. We
374 1.1 eeh * have a choice of several different relocation techniques
375 1.1 eeh * which are increasingly expensive.
376 1.1 eeh */
377 1.1 eeh
378 1.1 eeh offset = ((Elf_Addr)where) - value;
379 1.1 eeh if (rela->r_addend) {
380 1.1 eeh Elf_Addr *ptr = (Elf_Addr *)where;
381 1.1 eeh /*
382 1.1 eeh * This entry is >32768. Just replace the pointer.
383 1.1 eeh */
384 1.1 eeh ptr[0] = value;
385 1.1 eeh
386 1.1 eeh } else if (offset <= (1L<<20) && offset >= -(1L<<20)) {
387 1.1 eeh /*
388 1.1 eeh * We're within 1MB -- we can use a direct branch insn.
389 1.1 eeh *
390 1.1 eeh * We can generate this pattern:
391 1.1 eeh *
392 1.1 eeh * sethi %hi(. - .PLT0), %g1
393 1.1 eeh * ba,a %xcc, addr
394 1.1 eeh * nop
395 1.1 eeh * nop
396 1.1 eeh * nop
397 1.1 eeh * nop
398 1.1 eeh * nop
399 1.1 eeh * nop
400 1.1 eeh *
401 1.1 eeh */
402 1.1 eeh where[1] = BAA | ((offset >> 2) &0x3fffff);
403 1.1 eeh __asm __volatile("iflush %0+4" : : "r" (where));
404 1.1 eeh } else if (value >= 0 && value < (1L<<32)) {
405 1.1 eeh /*
406 1.1 eeh * We're withing 32-bits of address zero.
407 1.1 eeh *
408 1.1 eeh * The resulting code in the jump slot is:
409 1.1 eeh *
410 1.1 eeh * sethi %hi(. - .PLT0), %g1
411 1.1 eeh * sethi %hi(addr), %g1
412 1.1 eeh * jmp %g1+%lo(addr)
413 1.1 eeh * nop
414 1.1 eeh * nop
415 1.1 eeh * nop
416 1.1 eeh * nop
417 1.1 eeh * nop
418 1.1 eeh *
419 1.1 eeh */
420 1.1 eeh where[2] = JMP | LOVAL(value);
421 1.1 eeh where[1] = SETHI | HIVAL(value, 10);
422 1.1 eeh __asm __volatile("iflush %0+8" : : "r" (where));
423 1.1 eeh __asm __volatile("iflush %0+4" : : "r" (where));
424 1.1 eeh
425 1.1 eeh } else if (value <= 0 && value > -(1L<<32)) {
426 1.1 eeh /*
427 1.1 eeh * We're withing 32-bits of address -1.
428 1.1 eeh *
429 1.1 eeh * The resulting code in the jump slot is:
430 1.1 eeh *
431 1.1 eeh * sethi %hi(. - .PLT0), %g1
432 1.1 eeh * sethi %hix(addr), %g1
433 1.1 eeh * xor %g1, %lox(addr), %g1
434 1.1 eeh * jmp %g1
435 1.1 eeh * nop
436 1.1 eeh * nop
437 1.1 eeh * nop
438 1.1 eeh * nop
439 1.1 eeh *
440 1.1 eeh */
441 1.1 eeh where[3] = JMP;
442 1.1 eeh where[2] = XOR | ((~value) & 0x00001fff);
443 1.1 eeh where[1] = SETHI | HIVAL(~value, 10);
444 1.1 eeh __asm __volatile("iflush %0+12" : : "r" (where));
445 1.1 eeh __asm __volatile("iflush %0+8" : : "r" (where));
446 1.1 eeh __asm __volatile("iflush %0+4" : : "r" (where));
447 1.1 eeh
448 1.1 eeh } else if (offset <= (1L<<32) && offset >= -((1L<<32) - 4)) {
449 1.1 eeh /*
450 1.1 eeh * We're withing 32-bits -- we can use a direct call insn
451 1.1 eeh *
452 1.1 eeh * The resulting code in the jump slot is:
453 1.1 eeh *
454 1.1 eeh * sethi %hi(. - .PLT0), %g1
455 1.1 eeh * mov %o7, %g1
456 1.1 eeh * call (.+offset)
457 1.1 eeh * mov %g1, %o7
458 1.1 eeh * nop
459 1.1 eeh * nop
460 1.1 eeh * nop
461 1.1 eeh * nop
462 1.1 eeh *
463 1.1 eeh */
464 1.1 eeh where[3] = MOV17;
465 1.1 eeh where[2] = CALL | ((offset >> 4) & 0x3fffffff);
466 1.1 eeh where[1] = MOV71;
467 1.1 eeh __asm __volatile("iflush %0+12" : : "r" (where));
468 1.1 eeh __asm __volatile("iflush %0+8" : : "r" (where));
469 1.1 eeh __asm __volatile("iflush %0+4" : : "r" (where));
470 1.1 eeh
471 1.1 eeh } else if (offset >= 0 && offset < (1L<<44)) {
472 1.1 eeh /*
473 1.1 eeh * We're withing 44 bits. We can generate this pattern:
474 1.1 eeh *
475 1.1 eeh * The resulting code in the jump slot is:
476 1.1 eeh *
477 1.1 eeh * sethi %hi(. - .PLT0), %g1
478 1.1 eeh * sethi %h44(addr), %g1
479 1.1 eeh * or %g1, %m44(addr), %g1
480 1.1 eeh * sllx %g1, 12, %g1
481 1.1 eeh * jmp %g1+%l44(addr)
482 1.1 eeh * nop
483 1.1 eeh * nop
484 1.1 eeh * nop
485 1.1 eeh *
486 1.1 eeh */
487 1.1 eeh where[4] = JMP | LOVAL(offset);
488 1.1 eeh where[3] = SLLX | 12;
489 1.1 eeh where[2] = OR | (((offset) >> 12) & 0x00001fff);
490 1.1 eeh where[1] = SETHI | HIVAL(offset, 22);
491 1.1 eeh __asm __volatile("iflush %0+16" : : "r" (where));
492 1.1 eeh __asm __volatile("iflush %0+12" : : "r" (where));
493 1.1 eeh __asm __volatile("iflush %0+8" : : "r" (where));
494 1.1 eeh __asm __volatile("iflush %0+4" : : "r" (where));
495 1.1 eeh
496 1.1 eeh } else if (offset < 0 && offset > -(1L<<44)) {
497 1.1 eeh /*
498 1.1 eeh * We're withing 44 bits. We can generate this pattern:
499 1.1 eeh *
500 1.1 eeh * The resulting code in the jump slot is:
501 1.1 eeh *
502 1.1 eeh * sethi %hi(. - .PLT0), %g1
503 1.1 eeh * sethi %h44(-addr), %g1
504 1.1 eeh * xor %g1, %m44(-addr), %g1
505 1.1 eeh * sllx %g1, 12, %g1
506 1.1 eeh * jmp %g1+%l44(addr)
507 1.1 eeh * nop
508 1.1 eeh * nop
509 1.1 eeh * nop
510 1.1 eeh *
511 1.1 eeh */
512 1.1 eeh where[4] = JMP | LOVAL(offset);
513 1.1 eeh where[3] = SLLX | 12;
514 1.1 eeh where[2] = XOR | (((~offset) >> 12) & 0x00001fff);
515 1.1 eeh where[1] = SETHI | HIVAL(~offset, 22);
516 1.1 eeh __asm __volatile("iflush %0+16" : : "r" (where));
517 1.1 eeh __asm __volatile("iflush %0+12" : : "r" (where));
518 1.1 eeh __asm __volatile("iflush %0+8" : : "r" (where));
519 1.1 eeh __asm __volatile("iflush %0+4" : : "r" (where));
520 1.1 eeh
521 1.1 eeh } else {
522 1.1 eeh /*
523 1.1 eeh * We need to load all 64-bits
524 1.1 eeh *
525 1.1 eeh * The resulting code in the jump slot is:
526 1.1 eeh *
527 1.1 eeh * sethi %hi(. - .PLT0), %g1
528 1.1 eeh * sethi %hh(addr), %g1
529 1.1 eeh * sethi %lm(addr), %g5
530 1.1 eeh * or %g1, %hm(addr), %g1
531 1.1 eeh * sllx %g1, 32, %g1
532 1.1 eeh * or %g1, %g5, %g1
533 1.1 eeh * jmp %g1+%lo(addr)
534 1.1 eeh * nop
535 1.1 eeh *
536 1.1 eeh */
537 1.1 eeh where[6] = JMP | LOVAL(value);
538 1.1 eeh where[5] = ORG5;
539 1.1 eeh where[4] = SLLX | 12;
540 1.1 eeh where[3] = OR | LOVAL((value) >> 32);
541 1.1 eeh where[2] = SETHIG5 | HIVAL(value, 10);
542 1.1 eeh where[1] = SETHI | HIVAL(value, 42);
543 1.1 eeh __asm __volatile("iflush %0+20" : : "r" (where));
544 1.1 eeh __asm __volatile("iflush %0+16" : : "r" (where));
545 1.1 eeh __asm __volatile("iflush %0+16" : : "r" (where));
546 1.1 eeh __asm __volatile("iflush %0+12" : : "r" (where));
547 1.1 eeh __asm __volatile("iflush %0+8" : : "r" (where));
548 1.1 eeh __asm __volatile("iflush %0+4" : : "r" (where));
549 1.1 eeh
550 1.1 eeh }
551 1.1 eeh
552 1.1 eeh if (addrp != NULL)
553 1.1 eeh *addrp = (caddr_t)value;
554 1.1 eeh
555 1.1 eeh return (0);
556 1.1 eeh }
557 1.1 eeh
558 1.1 eeh /*
559 1.1 eeh * Install rtld function call into this PLT slot.
560 1.1 eeh */
561 1.1 eeh #define SAVE 0x9de3bf50
562 1.1 eeh #define SETHI_l0 0x21000000
563 1.1 eeh #define SETHI_l1 0x23000000
564 1.1 eeh #define OR_l0_l0 0xa0142000
565 1.1 eeh #define SLLX_l0_32_l0 0xa12c3020
566 1.1 eeh #define OR_l0_l1_l0 0xa0140011
567 1.1 eeh #define JMPL_l0_o0 0x93c42000
568 1.1 eeh #define MOV_g1_o0 0x90100001
569 1.1 eeh
570 1.1 eeh void _rtld_install_plt __P((Elf32_Word *pltgot, Elf_Addr proc));
571 1.1 eeh
572 1.1 eeh void
573 1.1 eeh _rtld_install_plt(pltgot, proc)
574 1.1 eeh Elf32_Word *pltgot;
575 1.1 eeh Elf_Addr proc;
576 1.1 eeh {
577 1.1 eeh pltgot[0] = SAVE;
578 1.1 eeh pltgot[1] = SETHI_l0 | HIVAL(proc, 42);
579 1.1 eeh pltgot[2] = SETHI_l1 | HIVAL(proc, 10);
580 1.1 eeh pltgot[3] = OR_l0_l0 | LOVAL((proc) >> 32);
581 1.1 eeh pltgot[4] = SLLX_l0_32_l0;
582 1.1 eeh pltgot[5] = OR_l0_l1_l0;
583 1.1 eeh pltgot[6] = JMPL_l0_o0 | LOVAL(proc);
584 1.1 eeh pltgot[7] = MOV_g1_o0;
585 1.1 eeh }
586