reloc.c revision 1.47 1 /* $NetBSD: reloc.c,v 1.47 2001/12/20 02:23:24 thorpej Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by John Polstra.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Dynamic linker for ELF.
36 *
37 * John Polstra <jdp (at) polstra.com>.
38 */
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/types.h>
49 #include <sys/mman.h>
50 #include <dirent.h>
51
52 #include "debug.h"
53 #include "rtld.h"
54
55 #ifndef RTLD_INHIBIT_COPY_RELOCS
56 static int _rtld_do_copy_relocation __P((const Obj_Entry *, const Elf_Rela *,
57 bool));
58
59 /*
60 * XXX: These don't work for the alpha and i386; don't know about powerpc
61 * The alpha and the i386 avoid the problem by compiling everything PIC.
62 * These relocation are supposed to be writing the address of the
63 * function to be called on the bss.rel or bss.rela segment, but:
64 * - st_size == 0
65 * - on the i386 at least the call instruction is a direct call
66 * not an indirect call.
67 */
68 static int
69 _rtld_do_copy_relocation(dstobj, rela, dodebug)
70 const Obj_Entry *dstobj;
71 const Elf_Rela *rela;
72 bool dodebug;
73 {
74 void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
75 const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
76 const char *name = dstobj->strtab + dstsym->st_name;
77 unsigned long hash = _rtld_elf_hash(name);
78 size_t size = dstsym->st_size;
79 const void *srcaddr;
80 const Elf_Sym *srcsym = NULL;
81 Obj_Entry *srcobj;
82
83 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
84 if ((srcsym = _rtld_symlook_obj(name, hash, srcobj,
85 false)) != NULL)
86 break;
87
88 if (srcobj == NULL) {
89 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
90 " relocation in %s", name, dstobj->path);
91 return (-1);
92 }
93 srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
94 (void)memcpy(dstaddr, srcaddr, size);
95 rdbg(dodebug, ("COPY %s %s %s --> src=%p dst=%p *dst= %p size %ld",
96 dstobj->path, srcobj->path, name, (void *)srcaddr,
97 (void *)dstaddr, (void *)*(long *)dstaddr, (long)size));
98 return (0);
99 }
100 #endif /* RTLD_INHIBIT_COPY_RELOCS */
101
102
103 /*
104 * Process the special R_xxx_COPY relocations in the main program. These
105 * copy data from a shared object into a region in the main program's BSS
106 * segment.
107 *
108 * Returns 0 on success, -1 on failure.
109 */
110 int
111 _rtld_do_copy_relocations(dstobj, dodebug)
112 const Obj_Entry *dstobj;
113 bool dodebug;
114 {
115 #ifndef RTLD_INHIBIT_COPY_RELOCS
116
117 /* COPY relocations are invalid elsewhere */
118 assert(dstobj->mainprog);
119
120 if (dstobj->rel != NULL) {
121 const Elf_Rel *rel;
122 for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
123 if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
124 Elf_Rela ourrela;
125 ourrela.r_info = rel->r_info;
126 ourrela.r_offset = rel->r_offset;
127 ourrela.r_addend = 0;
128 if (_rtld_do_copy_relocation(dstobj,
129 &ourrela, dodebug) < 0)
130 return (-1);
131 }
132 }
133 }
134 if (dstobj->rela != NULL) {
135 const Elf_Rela *rela;
136 for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
137 if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
138 if (_rtld_do_copy_relocation(dstobj, rela,
139 dodebug) < 0)
140 return (-1);
141 }
142 }
143 }
144 #endif /* RTLD_INHIBIT_COPY_RELOCS */
145
146 return (0);
147 }
148
149
150 #if !defined(__sparc__) && !defined(__x86_64__)
151
152 int
153 _rtld_relocate_nonplt_object(obj, rela, dodebug)
154 Obj_Entry *obj;
155 const Elf_Rela *rela;
156 bool dodebug;
157 {
158 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
159 const Elf_Sym *def;
160 const Obj_Entry *defobj;
161 #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
162 defined(__m68k__) || defined(__powerpc__) || defined(__vax__)
163 Elf_Addr tmp;
164 #endif
165
166 switch (ELF_R_TYPE(rela->r_info)) {
167
168 case R_TYPE(NONE):
169 break;
170
171 #if defined(__i386__)
172 case R_TYPE(GOT32):
173
174 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
175 &defobj, false);
176 if (def == NULL)
177 return -1;
178
179 tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
180 if (*where != tmp)
181 *where = tmp;
182 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
183 defobj->strtab + def->st_name, obj->path,
184 (void *)*where, defobj->path));
185 break;
186
187 case R_TYPE(PC32):
188 /*
189 * I don't think the dynamic linker should ever see this
190 * type of relocation. But the binutils-2.6 tools sometimes
191 * generate it.
192 */
193
194 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
195 &defobj, false);
196 if (def == NULL)
197 return -1;
198
199 *where += (Elf_Addr)(defobj->relocbase + def->st_value) -
200 (Elf_Addr)where;
201 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
202 defobj->strtab + def->st_name, obj->path,
203 (void *)*where, defobj->path));
204 break;
205
206 case R_TYPE(32):
207 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
208 &defobj, false);
209 if (def == NULL)
210 return -1;
211
212 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
213 rdbg(dodebug, ("32 %s in %s --> %p in %s",
214 defobj->strtab + def->st_name, obj->path,
215 (void *)*where, defobj->path));
216 break;
217 #endif /* __i386__ */
218
219 #if defined(__m68k__)
220 case R_TYPE(GOT32):
221 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
222 &defobj, false);
223 if (def == NULL)
224 return -1;
225
226 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
227 rela->r_addend);
228 if (*where != tmp)
229 *where = tmp;
230 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
231 defobj->strtab + def->st_name, obj->path,
232 (void *)*where, defobj->path));
233 break;
234
235 case R_TYPE(PC32):
236 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
237 &defobj, false);
238 if (def == NULL)
239 return -1;
240
241 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
242 rela->r_addend) - (Elf_Addr)where;
243 if (*where != tmp)
244 *where = tmp;
245 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
246 defobj->strtab + def->st_name, obj->path,
247 (void *)*where, defobj->path));
248 break;
249
250 case R_TYPE(32):
251 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
252 &defobj, false);
253 if (def == NULL)
254 return -1;
255
256 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
257 rela->r_addend);
258 if (*where != tmp)
259 *where = tmp;
260 rdbg(dodebug, ("32 %s in %s --> %p in %s",
261 defobj->strtab + def->st_name, obj->path,
262 (void *)*where, defobj->path));
263 break;
264 #endif /* __m68k__ */
265
266 #if defined(__alpha__)
267 case R_TYPE(REFQUAD):
268 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
269 &defobj, false);
270 if (def == NULL)
271 return -1;
272
273 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
274 *where + rela->r_addend;
275 if (*where != tmp)
276 *where = tmp;
277 rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
278 defobj->strtab + def->st_name, obj->path,
279 (void *)*where, defobj->path));
280 break;
281
282 case R_TYPE(RELATIVE):
283 {
284 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
285 extern Elf_Addr _GOT_END_[];
286
287 /* This is the ...iffy hueristic. */
288 if (!dodebug ||
289 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
290 (caddr_t)where >= (caddr_t)_GOT_END_) {
291 *where += (Elf_Addr)(obj->relocbase + rela->r_addend);
292 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
293 (void *)*where));
294 } else
295 rdbg(dodebug, ("RELATIVE in %s stays at %p",
296 obj->path, (void *)*where));
297 break;
298 }
299 #endif /* __alpha__ */
300
301 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
302 case R_TYPE(GLOB_DAT):
303 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
304 &defobj, false);
305 if (def == NULL)
306 return -1;
307
308 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
309 rela->r_addend;
310 if (*where != tmp)
311 *where = tmp;
312 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
313 defobj->strtab + def->st_name, obj->path,
314 (void *)*where, defobj->path));
315 break;
316
317 #if !defined(__alpha__)
318 case R_TYPE(RELATIVE):
319 *where += (Elf_Addr)obj->relocbase;
320 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
321 (void *)*where));
322 break;
323 #endif /* ! __alpha__ */
324
325 case R_TYPE(COPY):
326 /*
327 * These are deferred until all other relocations have
328 * been done. All we do here is make sure that the COPY
329 * relocation is not in a shared library. They are allowed
330 * only in executable files.
331 */
332 if (!obj->mainprog) {
333 _rtld_error(
334 "%s: Unexpected R_COPY relocation in shared library",
335 obj->path);
336 return -1;
337 }
338 rdbg(dodebug, ("COPY (avoid in main)"));
339 break;
340 #endif /* __i386__ || __alpha__ || __m68k__ */
341
342 #if defined(__mips__)
343 case R_TYPE(REL32):
344 /* 32-bit PC-relative reference */
345 def = obj->symtab + ELF_R_SYM(rela->r_info);
346
347 if (ELFDEFNNAME(ST_BIND)(def->st_info) == STB_LOCAL &&
348 (ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_SECTION ||
349 ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_NOTYPE)) {
350 /*
351 * XXX: ABI DIFFERENCE!
352 *
353 * Old NetBSD binutils would generate shared libs
354 * with section-relative relocations being already
355 * adjusted for the start address of the section.
356 *
357 * New binutils, OTOH, generate shared libs with
358 * the same relocations being based at zero, so we
359 * need to add in the start address of the section.
360 *
361 * We assume that all section-relative relocs with
362 * contents less than the start of the section need
363 * to be adjusted; this should work with both old
364 * and new shlibs.
365 *
366 * --rkb, Oct 6, 2001
367 */
368 if (def->st_info == STT_SECTION &&
369 (*where < def->st_value))
370 *where += (Elf_Addr) def->st_value;
371
372 *where += (Elf_Addr)obj->relocbase;
373
374 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
375 obj->strtab + def->st_name, obj->path,
376 (void *)*where, obj->path));
377 } else {
378 /* XXX maybe do something re: bootstrapping? */
379 def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
380 NULL, obj, &defobj, false);
381 if (def == NULL)
382 return -1;
383 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
384 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
385 defobj->strtab + def->st_name, obj->path,
386 (void *)*where, defobj->path));
387 }
388 break;
389
390 #endif /* __mips__ */
391
392 #if defined(__powerpc__) || defined(__vax__)
393 case R_TYPE(32): /* word32 S + A */
394 case R_TYPE(GLOB_DAT): /* word32 S + A */
395 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
396 &defobj, false);
397 if (def == NULL)
398 return -1;
399
400 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
401 rela->r_addend);
402
403 if (*where != tmp)
404 *where = tmp;
405 rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
406 defobj->strtab + def->st_name, obj->path,
407 (void *)*where, defobj->path));
408 break;
409
410 case R_TYPE(COPY):
411 rdbg(dodebug, ("COPY"));
412 break;
413
414 case R_TYPE(JMP_SLOT):
415 rdbg(dodebug, ("JMP_SLOT"));
416 break;
417
418 case R_TYPE(RELATIVE): /* word32 B + A */
419 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
420 if (*where != tmp)
421 *where = tmp;
422 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
423 (void *)*where));
424 break;
425 #endif /* __powerpc__ || __vax__ */
426
427 #if defined(__powerpc__)
428 case R_TYPE(16_LO): /* #lo(S + A) */
429 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
430 if (*(Elf32_Half *)where != tmp)
431 *(Elf32_Half *)where = tmp;
432 rdbg(dodebug, ("16_LO in %s --> %p", obj->path,
433 (void *)*where));
434 break;
435
436 case R_TYPE(16_HI): /* #hi(S + A) */
437 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend) >> 16;
438 if (*(Elf32_Half *)where != tmp)
439 *(Elf32_Half *)where = tmp;
440 rdbg(dodebug, ("16_HI in %s --> %p", obj->path,
441 (void *)*where));
442 break;
443
444 case R_TYPE(16_HA): /* #ha(S + A) */
445 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend + 0x8000)
446 >> 16;
447 if (*(Elf32_Half *)where != tmp)
448 *(Elf32_Half *)where = tmp;
449 rdbg(dodebug, ("16_HA in %s --> %p", obj->path,
450 (void *)*where));
451 break;
452 #endif /* __powerpc__ */
453
454 #if defined(__arm__)
455 case R_TYPE(GLOB_DAT): /* word32 B + S */
456 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
457 &defobj, false);
458 if (def == NULL)
459 return -1;
460 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
461 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
462 defobj->strtab + def->st_name, obj->path,
463 (void *)*where, where, defobj->path));
464 break;
465
466 case R_TYPE(COPY):
467 rdbg(dodebug, ("COPY"));
468 break;
469
470 case R_TYPE(JUMP_SLOT):
471 rdbg(dodebug, ("JUMP_SLOT"));
472 break;
473
474 case R_TYPE(ABS32): /* word32 B + S + A */
475 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
476 &defobj, false);
477 if (def == NULL)
478 return -1;
479 *where += (Elf_Addr)defobj->relocbase + def->st_value;
480 rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
481 defobj->strtab + def->st_name, obj->path,
482 (void *)*where, where, defobj->path));
483 break;
484
485 case R_TYPE(RELATIVE): /* word32 B + A */
486 *where += (Elf_Addr)obj->relocbase;
487 rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
488 (void *)*where, where));
489 break;
490
491 case R_TYPE(PC24): { /* word32 S - P + A */
492 Elf32_Sword addend;
493
494 /*
495 * Extract addend and sign-extend if needed.
496 */
497 addend = *where;
498 if (addend & 0x00800000)
499 addend |= 0xff000000;
500
501 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
502 &defobj, false);
503 if (def == NULL)
504 return -1;
505 tmp = (Elf_Addr)obj->relocbase + def->st_value
506 - (Elf_Addr)where + (addend << 2);
507 if ((tmp & 0xfe000000) != 0xfe000000 && (tmp & 0xfe000000) != 0) {
508 _rtld_error(
509 "%s: R_ARM_PC24 relocation @ %p to %s failed "
510 "(displacement %ld (%#lx) out of range)",
511 obj->path, where, defobj->strtab + def->st_name,
512 (long) tmp, (long) tmp);
513 return -1;
514 }
515 tmp >>= 2;
516 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
517 rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
518 defobj->strtab + def->st_name, obj->path,
519 (void *)*where, where, defobj->path));
520 break;
521 }
522 #endif
523
524 default:
525 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
526 &defobj, true);
527 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
528 "addend = %p, contents = %p, symbol = %s",
529 (u_long)ELF_R_SYM(rela->r_info),
530 (u_long)ELF_R_TYPE(rela->r_info),
531 (void *)rela->r_offset, (void *)rela->r_addend,
532 (void *)*where,
533 def ? defobj->strtab + def->st_name : "??"));
534 _rtld_error("%s: Unsupported relocation type %ld "
535 "in non-PLT relocations\n",
536 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
537 return -1;
538 }
539 return 0;
540 }
541
542
543 #if !defined(__powerpc__)
544
545 int
546 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
547 Obj_Entry *obj;
548 const Elf_Rela *rela;
549 caddr_t *addrp;
550 bool bind_now;
551 bool dodebug;
552 {
553 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
554 Elf_Addr new_value;
555
556 /* Fully resolve procedure addresses now */
557
558 #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
559 defined(__m68k__) || defined(__vax__)
560 if (bind_now || obj->pltgot == NULL) {
561 const Elf_Sym *def;
562 const Obj_Entry *defobj;
563
564 #if !defined(__arm__)
565 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
566 #else
567 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
568 #endif
569
570 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
571 &defobj, true);
572 if (def == NULL)
573 return -1;
574
575 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
576 #if defined(__vax__)
577 new_value += rela->r_addend;
578 #endif
579 rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
580 (int)bind_now,
581 defobj->strtab + def->st_name,
582 (void *)*where, (void *)new_value));
583 } else
584 #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
585 if (!obj->mainprog) {
586 /* Just relocate the GOT slots pointing into the PLT */
587 new_value = *where + (Elf_Addr)(obj->relocbase);
588 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
589 (void *)*where));
590 } else {
591 return 0;
592 }
593 /*
594 * Since this page is probably copy-on-write, let's not write
595 * it unless we really really have to.
596 */
597 if (*where != new_value)
598 *where = new_value;
599 if (addrp != NULL) {
600 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
601 #if defined(__vax__)
602 *addrp -= rela->r_addend;
603 #endif
604 }
605 return 0;
606 }
607
608 #endif /* __powerpc__ */
609
610 #endif /* __sparc__ || __x86_64__ */
611
612 caddr_t
613 _rtld_bind(obj, reloff)
614 Obj_Entry *obj;
615 Elf_Word reloff;
616 {
617 const Elf_Rela *rela;
618 Elf_Rela ourrela;
619 caddr_t addr;
620
621 if (obj->pltrel != NULL) {
622 const Elf_Rel *rel;
623
624 rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
625 ourrela.r_info = rel->r_info;
626 ourrela.r_offset = rel->r_offset;
627 ourrela.r_addend = 0;
628 rela = &ourrela;
629 } else {
630 rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
631 #ifdef __sparc64__
632 if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
633 /*
634 * XXXX
635 *
636 * The first for PLT entries are reserved. There
637 * is some disagreement whether they should have
638 * associated relocation entries. Both the SPARC
639 * 32-bit and 64-bit ELF specifications say that
640 * they should have relocation entries, but the
641 * 32-bit SPARC binutils do not generate them,
642 * and now the 64-bit SPARC binutils have stopped
643 * generating them too.
644 *
645 * So, to provide binary compatibility, we will
646 * check the first entry, if it is reserved it
647 * should not be of the type JMP_SLOT. If it
648 * is JMP_SLOT, then the 4 reserved entries were
649 * not generated and our index is 4 entries too far.
650 */
651 rela -= 4;
652 }
653 #endif
654 }
655
656 if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
657 _rtld_die();
658
659 return addr;
660 }
661
662 /*
663 * Relocate newly-loaded shared objects. The argument is a pointer to
664 * the Obj_Entry for the first such object. All objects from the first
665 * to the end of the list of objects are relocated. Returns 0 on success,
666 * or -1 on failure.
667 */
668 int
669 _rtld_relocate_objects(first, bind_now, dodebug)
670 Obj_Entry *first;
671 bool bind_now;
672 bool dodebug;
673 {
674 Obj_Entry *obj;
675 int ok = 1;
676
677 for (obj = first; obj != NULL; obj = obj->next) {
678 if (obj->nbuckets == 0 || obj->nchains == 0 ||
679 obj->buckets == NULL || obj->symtab == NULL ||
680 obj->strtab == NULL) {
681 _rtld_error("%s: Shared object has no run-time"
682 " symbol table", obj->path);
683 return -1;
684 }
685 rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
686 "%ld/%ld plt rel/rela)",
687 obj->path,
688 (long)(obj->rellim - obj->rel),
689 (long)(obj->relalim - obj->rela),
690 (long)(obj->pltrellim - obj->pltrel),
691 (long)(obj->pltrelalim - obj->pltrela)));
692
693 if (obj->textrel) {
694 /*
695 * There are relocations to the write-protected text
696 * segment.
697 */
698 if (mprotect(obj->mapbase, obj->textsize,
699 PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
700 _rtld_error("%s: Cannot write-enable text "
701 "segment: %s", obj->path, xstrerror(errno));
702 return -1;
703 }
704 }
705 if (obj->rel != NULL) {
706 /* Process the non-PLT relocations. */
707 const Elf_Rel *rel;
708 for (rel = obj->rel; rel < obj->rellim; ++rel) {
709 Elf_Rela ourrela;
710 ourrela.r_info = rel->r_info;
711 ourrela.r_offset = rel->r_offset;
712 #if defined(__mips__)
713 /* rel->r_offset is not valid on mips? */
714 if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
715 ourrela.r_addend = 0;
716 else
717 #endif
718 ourrela.r_addend =
719 *(Elf_Word *)(obj->relocbase +
720 rel->r_offset);
721
722 if (_rtld_relocate_nonplt_object(obj, &ourrela,
723 dodebug) < 0)
724 ok = 0;
725 }
726 }
727 if (obj->rela != NULL) {
728 /* Process the non-PLT relocations. */
729 const Elf_Rela *rela;
730 for (rela = obj->rela; rela < obj->relalim; ++rela) {
731 if (_rtld_relocate_nonplt_object(obj, rela,
732 dodebug) < 0)
733 ok = 0;
734 }
735 }
736 if (obj->textrel) { /* Re-protected the text segment. */
737 if (mprotect(obj->mapbase, obj->textsize,
738 PROT_READ | PROT_EXEC) == -1) {
739 _rtld_error("%s: Cannot write-protect text "
740 "segment: %s", obj->path, xstrerror(errno));
741 return -1;
742 }
743 }
744 /* Process the PLT relocations. */
745 if (obj->pltrel != NULL) {
746 const Elf_Rel *rel;
747 for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
748 Elf_Rela ourrela;
749 ourrela.r_info = rel->r_info;
750 ourrela.r_offset = rel->r_offset;
751 ourrela.r_addend =
752 *(Elf_Word *)(obj->relocbase +
753 rel->r_offset);
754 if (_rtld_relocate_plt_object(obj, &ourrela,
755 NULL, bind_now, dodebug) < 0)
756 ok = 0;
757 }
758 }
759 if (obj->pltrela != NULL) {
760 const Elf_Rela *rela;
761 for (rela = obj->pltrela; rela < obj->pltrelalim;
762 ++rela) {
763 if (_rtld_relocate_plt_object(obj, rela,
764 NULL, bind_now, dodebug) < 0)
765 ok = 0;
766 }
767 }
768 if (!ok)
769 return -1;
770
771
772 /* Set some sanity-checking numbers in the Obj_Entry. */
773 obj->magic = RTLD_MAGIC;
774 obj->version = RTLD_VERSION;
775
776 /* Fill in the dynamic linker entry points. */
777 obj->dlopen = _rtld_dlopen;
778 obj->dlsym = _rtld_dlsym;
779 obj->dlerror = _rtld_dlerror;
780 obj->dlclose = _rtld_dlclose;
781 obj->dladdr = _rtld_dladdr;
782
783 /* Set the special PLTGOT entries. */
784 if (obj->pltgot != NULL) {
785 #if defined(__arm__) || defined(__i386__) || defined(__m68k__) || \
786 defined(__x86_64__)
787 obj->pltgot[1] = (Elf_Addr) obj;
788 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
789 #endif
790 #if defined(__alpha__)
791 _rtld_setup_alpha_pltgot(obj, dodebug);
792 #endif
793 #if defined(__mips__)
794 _rtld_relocate_mips_got(obj);
795
796 obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
797 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
798 obj->pltgot[1] |= (Elf_Addr) obj;
799 #endif
800 #if defined(__powerpc__)
801 _rtld_setup_powerpc_plt(obj);
802 #endif
803 #if defined(__sparc__)
804 #if defined(__arch64__) || defined(__sparc_v9__)
805 /*
806 * On sparc64 we got troubles.
807 *
808 * Instructions are 4 bytes long.
809 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
810 * array entries.
811 * Each PLT entry jumps to PLT0 to enter the dynamic
812 * linker.
813 * Loading an arbitrary 64-bit pointer takes 6
814 * instructions and 2 registers.
815 *
816 * Somehow we need to issue a save to get a new stack
817 * frame, load the address of the dynamic linker, and
818 * jump there, in 8 instructions or less.
819 *
820 * Oh, we need to fill out both PLT0 and PLT1.
821 */
822 {
823 Elf_Word *entry = (Elf_Word *)obj->pltgot;
824 extern void _rtld_install_plt __P((Elf_Word *,
825 Elf_Addr));
826 extern void _rtld_bind_start_0 __P((long,
827 long));
828 extern void _rtld_bind_start_1 __P((long,
829 long));
830
831 /* Install in entries 0 and 1 */
832 _rtld_install_plt(&entry[0],
833 (Elf_Addr)&_rtld_bind_start_0);
834 _rtld_install_plt(&entry[8],
835 (Elf_Addr)&_rtld_bind_start_1);
836
837 /*
838 * Install the object reference in first slot
839 * of entry 2.
840 */
841 obj->pltgot[8] = (Elf_Addr) obj;
842 }
843 #else
844 /*
845 * PLTGOT is the PLT on the sparc.
846 * The first entry holds the call the dynamic linker.
847 * We construct a `call' sequence that transfers
848 * to `_rtld_bind_start()'.
849 * The second entry holds the object identification.
850 * Note: each PLT entry is three words long.
851 */
852 #define SAVE 0x9de3bfc0 /* i.e. `save %sp,-64,%sp' */
853 #define CALL 0x40000000
854 #define NOP 0x01000000
855 obj->pltgot[0] = SAVE;
856 obj->pltgot[1] = CALL |
857 ((Elf_Addr)&_rtld_bind_start -
858 (Elf_Addr)&obj->pltgot[1]) >> 2;
859 obj->pltgot[2] = NOP;
860
861 obj->pltgot[3] = (Elf_Addr) obj;
862 #endif /* __arch64__ */
863 #endif /* __sparc__ */
864 #if defined(__vax__)
865 obj->pltgot[1] = (Elf_Addr) obj;
866 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
867 #endif
868 }
869 }
870
871 return 0;
872 }
873