reloc.c revision 1.49 1 /* $NetBSD: reloc.c,v 1.49 2001/12/20 06:54:25 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;
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 &&
508 (tmp & 0xfe000000) != 0) {
509 _rtld_error(
510 "%s: R_ARM_PC24 relocation @ %p to %s failed "
511 "(displacement %ld (%#lx) out of range)",
512 obj->path, where, defobj->strtab + def->st_name,
513 (long) tmp, (long) tmp);
514 return -1;
515 }
516 tmp >>= 2;
517 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
518 rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
519 defobj->strtab + def->st_name, obj->path,
520 (void *)*where, where, defobj->path));
521 break;
522 }
523 #endif
524
525 default:
526 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
527 &defobj, true);
528 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
529 "addend = %p, contents = %p, symbol = %s",
530 (u_long)ELF_R_SYM(rela->r_info),
531 (u_long)ELF_R_TYPE(rela->r_info),
532 (void *)rela->r_offset, (void *)rela->r_addend,
533 (void *)*where,
534 def ? defobj->strtab + def->st_name : "??"));
535 _rtld_error("%s: Unsupported relocation type %ld "
536 "in non-PLT relocations\n",
537 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
538 return -1;
539 }
540 return 0;
541 }
542
543
544 #if !defined(__powerpc__)
545
546 int
547 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
548 Obj_Entry *obj;
549 const Elf_Rela *rela;
550 caddr_t *addrp;
551 bool bind_now;
552 bool dodebug;
553 {
554 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
555 Elf_Addr new_value;
556
557 /* Fully resolve procedure addresses now */
558
559 #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
560 defined(__m68k__) || defined(__vax__)
561 if (bind_now || obj->pltgot == NULL) {
562 const Elf_Sym *def;
563 const Obj_Entry *defobj;
564
565 #if !defined(__arm__)
566 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
567 #else
568 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
569 #endif
570
571 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
572 &defobj, true);
573 if (def == NULL)
574 return -1;
575
576 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
577 #if defined(__vax__)
578 new_value += rela->r_addend;
579 #endif
580 rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
581 (int)bind_now,
582 defobj->strtab + def->st_name,
583 (void *)*where, (void *)new_value));
584 } else
585 #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
586 if (!obj->mainprog) {
587 /* Just relocate the GOT slots pointing into the PLT */
588 new_value = *where + (Elf_Addr)(obj->relocbase);
589 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
590 (void *)*where));
591 } else {
592 return 0;
593 }
594 /*
595 * Since this page is probably copy-on-write, let's not write
596 * it unless we really really have to.
597 */
598 if (*where != new_value)
599 *where = new_value;
600 if (addrp != NULL) {
601 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
602 #if defined(__vax__)
603 *addrp -= rela->r_addend;
604 #endif
605 }
606 return 0;
607 }
608
609 #endif /* __powerpc__ */
610
611 #endif /* __sparc__ || __x86_64__ */
612
613 caddr_t
614 _rtld_bind(obj, reloff)
615 Obj_Entry *obj;
616 Elf_Word reloff;
617 {
618 const Elf_Rela *rela;
619 Elf_Rela ourrela;
620 caddr_t addr;
621
622 if (obj->pltrel != NULL) {
623 const Elf_Rel *rel;
624
625 rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
626 ourrela.r_info = rel->r_info;
627 ourrela.r_offset = rel->r_offset;
628 ourrela.r_addend = 0;
629 rela = &ourrela;
630 } else {
631 rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
632 #ifdef __sparc64__
633 if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
634 /*
635 * XXXX
636 *
637 * The first for PLT entries are reserved. There
638 * is some disagreement whether they should have
639 * associated relocation entries. Both the SPARC
640 * 32-bit and 64-bit ELF specifications say that
641 * they should have relocation entries, but the
642 * 32-bit SPARC binutils do not generate them,
643 * and now the 64-bit SPARC binutils have stopped
644 * generating them too.
645 *
646 * So, to provide binary compatibility, we will
647 * check the first entry, if it is reserved it
648 * should not be of the type JMP_SLOT. If it
649 * is JMP_SLOT, then the 4 reserved entries were
650 * not generated and our index is 4 entries too far.
651 */
652 rela -= 4;
653 }
654 #endif
655 }
656
657 if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
658 _rtld_die();
659
660 return addr;
661 }
662
663 /*
664 * Relocate newly-loaded shared objects. The argument is a pointer to
665 * the Obj_Entry for the first such object. All objects from the first
666 * to the end of the list of objects are relocated. Returns 0 on success,
667 * or -1 on failure.
668 */
669 int
670 _rtld_relocate_objects(first, bind_now, dodebug)
671 Obj_Entry *first;
672 bool bind_now;
673 bool dodebug;
674 {
675 Obj_Entry *obj;
676 int ok = 1;
677
678 for (obj = first; obj != NULL; obj = obj->next) {
679 if (obj->nbuckets == 0 || obj->nchains == 0 ||
680 obj->buckets == NULL || obj->symtab == NULL ||
681 obj->strtab == NULL) {
682 _rtld_error("%s: Shared object has no run-time"
683 " symbol table", obj->path);
684 return -1;
685 }
686 rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
687 "%ld/%ld plt rel/rela)",
688 obj->path,
689 (long)(obj->rellim - obj->rel),
690 (long)(obj->relalim - obj->rela),
691 (long)(obj->pltrellim - obj->pltrel),
692 (long)(obj->pltrelalim - obj->pltrela)));
693
694 if (obj->textrel) {
695 /*
696 * There are relocations to the write-protected text
697 * segment.
698 */
699 if (mprotect(obj->mapbase, obj->textsize,
700 PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
701 _rtld_error("%s: Cannot write-enable text "
702 "segment: %s", obj->path, xstrerror(errno));
703 return -1;
704 }
705 }
706 if (obj->rel != NULL) {
707 /* Process the non-PLT relocations. */
708 const Elf_Rel *rel;
709 for (rel = obj->rel; rel < obj->rellim; ++rel) {
710 Elf_Rela ourrela;
711 ourrela.r_info = rel->r_info;
712 ourrela.r_offset = rel->r_offset;
713 #if defined(__mips__)
714 /* rel->r_offset is not valid on mips? */
715 if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
716 ourrela.r_addend = 0;
717 else
718 #endif
719 ourrela.r_addend =
720 *(Elf_Word *)(obj->relocbase +
721 rel->r_offset);
722
723 if (_rtld_relocate_nonplt_object(obj, &ourrela,
724 dodebug) < 0)
725 ok = 0;
726 }
727 }
728 if (obj->rela != NULL) {
729 /* Process the non-PLT relocations. */
730 const Elf_Rela *rela;
731 for (rela = obj->rela; rela < obj->relalim; ++rela) {
732 if (_rtld_relocate_nonplt_object(obj, rela,
733 dodebug) < 0)
734 ok = 0;
735 }
736 }
737 if (obj->textrel) { /* Re-protected the text segment. */
738 if (mprotect(obj->mapbase, obj->textsize,
739 PROT_READ | PROT_EXEC) == -1) {
740 _rtld_error("%s: Cannot write-protect text "
741 "segment: %s", obj->path, xstrerror(errno));
742 return -1;
743 }
744 }
745 /* Process the PLT relocations. */
746 if (obj->pltrel != NULL) {
747 const Elf_Rel *rel;
748 for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
749 Elf_Rela ourrela;
750 ourrela.r_info = rel->r_info;
751 ourrela.r_offset = rel->r_offset;
752 ourrela.r_addend =
753 *(Elf_Word *)(obj->relocbase +
754 rel->r_offset);
755 if (_rtld_relocate_plt_object(obj, &ourrela,
756 NULL, bind_now, dodebug) < 0)
757 ok = 0;
758 }
759 }
760 if (obj->pltrela != NULL) {
761 const Elf_Rela *rela;
762 for (rela = obj->pltrela; rela < obj->pltrelalim;
763 ++rela) {
764 if (_rtld_relocate_plt_object(obj, rela,
765 NULL, bind_now, dodebug) < 0)
766 ok = 0;
767 }
768 }
769 if (!ok)
770 return -1;
771
772
773 /* Set some sanity-checking numbers in the Obj_Entry. */
774 obj->magic = RTLD_MAGIC;
775 obj->version = RTLD_VERSION;
776
777 /* Fill in the dynamic linker entry points. */
778 obj->dlopen = _rtld_dlopen;
779 obj->dlsym = _rtld_dlsym;
780 obj->dlerror = _rtld_dlerror;
781 obj->dlclose = _rtld_dlclose;
782 obj->dladdr = _rtld_dladdr;
783
784 /* Set the special PLTGOT entries. */
785 if (obj->pltgot != NULL) {
786 #if defined(__arm__) || defined(__i386__) || defined(__m68k__) || \
787 defined(__x86_64__)
788 obj->pltgot[1] = (Elf_Addr) obj;
789 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
790 #endif
791 #if defined(__alpha__)
792 _rtld_setup_alpha_pltgot(obj, dodebug);
793 #endif
794 #if defined(__mips__)
795 _rtld_relocate_mips_got(obj);
796
797 obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
798 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
799 obj->pltgot[1] |= (Elf_Addr) obj;
800 #endif
801 #if defined(__powerpc__)
802 _rtld_setup_powerpc_plt(obj);
803 #endif
804 #if defined(__sparc__)
805 #if defined(__arch64__) || defined(__sparc_v9__)
806 /*
807 * On sparc64 we got troubles.
808 *
809 * Instructions are 4 bytes long.
810 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
811 * array entries.
812 * Each PLT entry jumps to PLT0 to enter the dynamic
813 * linker.
814 * Loading an arbitrary 64-bit pointer takes 6
815 * instructions and 2 registers.
816 *
817 * Somehow we need to issue a save to get a new stack
818 * frame, load the address of the dynamic linker, and
819 * jump there, in 8 instructions or less.
820 *
821 * Oh, we need to fill out both PLT0 and PLT1.
822 */
823 {
824 Elf_Word *entry = (Elf_Word *)obj->pltgot;
825 extern void _rtld_install_plt __P((Elf_Word *,
826 Elf_Addr));
827 extern void _rtld_bind_start_0 __P((long,
828 long));
829 extern void _rtld_bind_start_1 __P((long,
830 long));
831
832 /* Install in entries 0 and 1 */
833 _rtld_install_plt(&entry[0],
834 (Elf_Addr)&_rtld_bind_start_0);
835 _rtld_install_plt(&entry[8],
836 (Elf_Addr)&_rtld_bind_start_1);
837
838 /*
839 * Install the object reference in first slot
840 * of entry 2.
841 */
842 obj->pltgot[8] = (Elf_Addr) obj;
843 }
844 #else
845 /*
846 * PLTGOT is the PLT on the sparc.
847 * The first entry holds the call the dynamic linker.
848 * We construct a `call' sequence that transfers
849 * to `_rtld_bind_start()'.
850 * The second entry holds the object identification.
851 * Note: each PLT entry is three words long.
852 */
853 #define SAVE 0x9de3bfc0 /* i.e. `save %sp,-64,%sp' */
854 #define CALL 0x40000000
855 #define NOP 0x01000000
856 obj->pltgot[0] = SAVE;
857 obj->pltgot[1] = CALL |
858 ((Elf_Addr)&_rtld_bind_start -
859 (Elf_Addr)&obj->pltgot[1]) >> 2;
860 obj->pltgot[2] = NOP;
861
862 obj->pltgot[3] = (Elf_Addr) obj;
863 #endif /* __arch64__ */
864 #endif /* __sparc__ */
865 #if defined(__vax__)
866 obj->pltgot[1] = (Elf_Addr) obj;
867 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
868 #endif
869 }
870 }
871
872 return 0;
873 }
874