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