reloc.c revision 1.58 1 /* $NetBSD: reloc.c,v 1.58 2002/09/05 17:58:02 mycroft 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__) && !defined(__mips__)
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 Elf_Addr tmp;
162
163 switch (ELF_R_TYPE(rela->r_info)) {
164
165 case R_TYPE(NONE):
166 break;
167
168 #if defined(__i386__)
169 case R_TYPE(GOT32):
170
171 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
172 if (def == NULL)
173 return -1;
174
175 tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
176 if (*where != tmp)
177 *where = tmp;
178 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
179 defobj->strtab + def->st_name, obj->path,
180 (void *)*where, defobj->path));
181 break;
182
183 case R_TYPE(PC32):
184 /*
185 * I don't think the dynamic linker should ever see this
186 * type of relocation. But the binutils-2.6 tools sometimes
187 * generate it.
188 */
189
190 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
191 if (def == NULL)
192 return -1;
193
194 *where += (Elf_Addr)(defobj->relocbase + def->st_value) -
195 (Elf_Addr)where;
196 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
197 defobj->strtab + def->st_name, obj->path,
198 (void *)*where, defobj->path));
199 break;
200
201 case R_TYPE(32):
202 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
203 if (def == NULL)
204 return -1;
205
206 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
207 rdbg(dodebug, ("32 %s in %s --> %p in %s",
208 defobj->strtab + def->st_name, obj->path,
209 (void *)*where, defobj->path));
210 break;
211 #endif /* __i386__ */
212
213 #if defined(__m68k__)
214 case R_TYPE(GOT32):
215 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
216 if (def == NULL)
217 return -1;
218
219 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
220 rela->r_addend);
221 if (*where != tmp)
222 *where = tmp;
223 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
224 defobj->strtab + def->st_name, obj->path,
225 (void *)*where, defobj->path));
226 break;
227
228 case R_TYPE(PC32):
229 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
230 if (def == NULL)
231 return -1;
232
233 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
234 rela->r_addend) - (Elf_Addr)where;
235 if (*where != tmp)
236 *where = tmp;
237 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
238 defobj->strtab + def->st_name, obj->path,
239 (void *)*where, defobj->path));
240 break;
241
242 case R_TYPE(32):
243 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
244 if (def == NULL)
245 return -1;
246
247 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
248 rela->r_addend);
249 if (*where != tmp)
250 *where = tmp;
251 rdbg(dodebug, ("32 %s in %s --> %p in %s",
252 defobj->strtab + def->st_name, obj->path,
253 (void *)*where, defobj->path));
254 break;
255 #endif /* __m68k__ */
256
257 #if defined(__sh__)
258 case R_TYPE(GOT32):
259 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
260 if (def == NULL)
261 return -1;
262
263 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
264 rela->r_addend);
265 if (*where != tmp)
266 *where = tmp;
267 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
268 defobj->strtab + def->st_name, obj->path,
269 (void *)*where, defobj->path));
270 break;
271
272 case R_TYPE(REL32):
273 /*
274 * I don't think the dynamic linker should ever see this
275 * type of relocation, but some versions of Binutils
276 * generate it.
277 */
278 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
279 if (def == NULL)
280 return -1;
281
282 *where += (Elf_Addr)(defobj->relocbase + def->st_value +
283 rela->r_addend) - (Elf_Addr)where;
284 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
285 defobj->strtab + def->st_name, obj->path,
286 (void *)*where, defobj->path));
287 break;
288
289 case R_TYPE(DIR32):
290 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
291 if (def == NULL)
292 return -1;
293
294 *where += (Elf_Addr)(defobj->relocbase + def->st_value +
295 rela->r_addend);
296 rdbg(dodebug, ("32 %s in %s --> %p in %s",
297 defobj->strtab + def->st_name, obj->path,
298 (void *)*where, defobj->path));
299 break;
300 #endif /* __sh__ */
301
302 #if defined(__alpha__)
303 case R_TYPE(REFQUAD):
304 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
305 if (def == NULL)
306 return -1;
307
308 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
309 *where + rela->r_addend;
310 if (*where != tmp)
311 *where = tmp;
312 rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
313 defobj->strtab + def->st_name, obj->path,
314 (void *)*where, defobj->path));
315 break;
316
317 case R_TYPE(RELATIVE):
318 {
319 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
320 extern Elf_Addr _GOT_END_[];
321
322 /* This is the ...iffy hueristic. */
323 if (!dodebug ||
324 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
325 (caddr_t)where >= (caddr_t)_GOT_END_) {
326 *where += (Elf_Addr)obj->relocbase;
327 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
328 (void *)*where));
329 } else
330 rdbg(dodebug, ("RELATIVE in %s stays at %p",
331 obj->path, (void *)*where));
332 break;
333 }
334 #endif /* __alpha__ */
335
336 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
337 defined(__sh__)
338 case R_TYPE(GLOB_DAT):
339 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
340 if (def == NULL)
341 return -1;
342
343 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
344 rela->r_addend;
345 if (*where != tmp)
346 *where = tmp;
347 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
348 defobj->strtab + def->st_name, obj->path,
349 (void *)*where, defobj->path));
350 break;
351 #if defined(__sh__)
352 case R_TYPE(RELATIVE):
353 if (rela->r_addend)
354 *where = (Elf_Addr)obj->relocbase + rela->r_addend;
355 else
356 *where += (Elf_Addr)obj->relocbase;
357 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
358 (void *)*where));
359 break;
360 #elif !defined(__alpha__)
361 case R_TYPE(RELATIVE):
362 *where += (Elf_Addr)obj->relocbase;
363 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
364 (void *)*where));
365 break;
366 #endif /* ! __alpha__ */
367 #endif /* __alpha__ || __i386__ || __m68k__ || __sh__ */
368
369 #if defined(__powerpc__) || defined(__vax__)
370 case R_TYPE(32): /* word32 S + A */
371 case R_TYPE(GLOB_DAT): /* word32 S + A */
372 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
373 if (def == NULL)
374 return -1;
375
376 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
377 rela->r_addend);
378
379 if (*where != tmp)
380 *where = tmp;
381 rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
382 defobj->strtab + def->st_name, obj->path,
383 (void *)*where, defobj->path));
384 break;
385
386 case R_TYPE(RELATIVE): /* word32 B + A */
387 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
388 if (*where != tmp)
389 *where = tmp;
390 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
391 (void *)*where));
392 break;
393 #endif /* __powerpc__ || __vax__ */
394
395 #if defined(__arm__)
396 case R_TYPE(GLOB_DAT): /* word32 B + S */
397 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
398 if (def == NULL)
399 return -1;
400 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
401 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
402 defobj->strtab + def->st_name, obj->path,
403 (void *)*where, where, defobj->path));
404 break;
405
406 case R_TYPE(ABS32): /* word32 B + S + A */
407 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
408 if (def == NULL)
409 return -1;
410 *where += (Elf_Addr)defobj->relocbase + def->st_value;
411 rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
412 defobj->strtab + def->st_name, obj->path,
413 (void *)*where, where, defobj->path));
414 break;
415
416 case R_TYPE(RELATIVE): /* word32 B + A */
417 *where += (Elf_Addr)obj->relocbase;
418 rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
419 (void *)*where, where));
420 break;
421
422 case R_TYPE(PC24): { /* word32 S - P + A */
423 Elf32_Sword addend;
424
425 /*
426 * Extract addend and sign-extend if needed.
427 */
428 addend = *where;
429 if (addend & 0x00800000)
430 addend |= 0xff000000;
431
432 def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
433 if (def == NULL)
434 return -1;
435 tmp = (Elf_Addr)obj->relocbase + def->st_value
436 - (Elf_Addr)where + (addend << 2);
437 if ((tmp & 0xfe000000) != 0xfe000000 &&
438 (tmp & 0xfe000000) != 0) {
439 _rtld_error(
440 "%s: R_ARM_PC24 relocation @ %p to %s failed "
441 "(displacement %ld (%#lx) out of range)",
442 obj->path, where, defobj->strtab + def->st_name,
443 (long) tmp, (long) tmp);
444 return -1;
445 }
446 tmp >>= 2;
447 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
448 rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
449 defobj->strtab + def->st_name, obj->path,
450 (void *)*where, where, defobj->path));
451 break;
452 }
453 #endif /* __arm__ */
454
455 #ifdef __hppa__
456 case R_TYPE(DIR32):
457 if (ELF_R_SYM(rela->r_info)) {
458 /*
459 * This is either a DIR32 against a symbol
460 * (def->st_name != 0), or against a local
461 * section (def->st_name == 0).
462 */
463 def = obj->symtab + ELF_R_SYM(rela->r_info);
464 defobj = obj;
465 if (def->st_name != 0)
466 /*
467 * While we're relocating self, _rtld_objlist
468 * is NULL, so we just pass in self.
469 */
470 def = _rtld_find_symdef(rela->r_info, obj,
471 &defobj, false);
472 if (def == NULL)
473 return -1;
474
475 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
476 rela->r_addend);
477
478 if (*where != tmp)
479 *where = tmp;
480 rdbg(dodebug, ("DIR32 %s in %s --> %p in %s",
481 defobj->strtab + def->st_name, obj->path,
482 (void *)*where, defobj->path));
483 } else {
484 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
485 extern Elf_Addr _GOT_END_[];
486
487 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
488
489 /* This is the ...iffy hueristic. */
490 if (!dodebug ||
491 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
492 (caddr_t)where >= (caddr_t)_GOT_END_) {
493 if (*where != tmp)
494 *where = tmp;
495 rdbg(dodebug, ("DIR32 in %s --> %p", obj->path,
496 (void *)*where));
497 } else
498 rdbg(dodebug, ("DIR32 in %s stays at %p",
499 obj->path, (void *)*where));
500 }
501 break;
502
503 case R_TYPE(PLABEL32):
504 if (ELF_R_SYM(rela->r_info)) {
505 /*
506 * While we're relocating self, _rtld_objlist
507 * is NULL, so we just pass in self.
508 */
509 def = _rtld_find_symdef(rela->r_info, obj, &defobj,
510 false);
511 if (def == NULL)
512 return -1;
513
514 tmp = _rtld_function_descriptor_alloc(defobj, def,
515 rela->r_addend);
516 if (tmp == (Elf_Addr)-1)
517 return -1;
518
519 if (*where != tmp)
520 *where = tmp;
521 rdbg(dodebug, ("PLABEL32 %s in %s --> %p in %s",
522 defobj->strtab + def->st_name, obj->path,
523 (void *)*where, defobj->path));
524 } else {
525 /*
526 * This is a PLABEL for a static function, and the
527 * dynamic linker has both allocated a PLT entry
528 * for this function and told us where it is. We
529 * can safely use the PLT entry as the PLABEL
530 * because there should be no other PLABEL reloc
531 * referencing this function. This object should
532 * also have an IPLT relocation to initialize the
533 * PLT entry.
534 *
535 * The dynamic linker should also have ensured
536 * that the addend has the next-least-significant
537 * bit set; the $$dyncall millicode uses this to
538 * distinguish a PLABEL pointer from a plain
539 * function pointer.
540 */
541 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
542
543 if (*where != tmp)
544 *where = tmp;
545 rdbg(dodebug, ("PLABEL32 in %s --> %p",
546 obj->path, (void *)*where));
547 }
548 break;
549 #endif /* __hppa__ */
550
551 case R_TYPE(COPY):
552 /*
553 * These are deferred until all other relocations have
554 * been done. All we do here is make sure that the COPY
555 * relocation is not in a shared library. They are allowed
556 * only in executable files.
557 */
558 if (!obj->mainprog) {
559 _rtld_error(
560 "%s: Unexpected R_COPY relocation in shared library",
561 obj->path);
562 return -1;
563 }
564 rdbg(dodebug, ("COPY (avoid in main)"));
565 break;
566
567 default:
568 def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
569 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
570 "addend = %p, contents = %p, symbol = %s",
571 (u_long)ELF_R_SYM(rela->r_info),
572 (u_long)ELF_R_TYPE(rela->r_info),
573 (void *)rela->r_offset, (void *)rela->r_addend,
574 (void *)*where,
575 def ? defobj->strtab + def->st_name : "??"));
576 _rtld_error("%s: Unsupported relocation type %ld "
577 "in non-PLT relocations\n",
578 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
579 return -1;
580 }
581 return 0;
582 }
583
584
585 #if !defined(__powerpc__) && !defined(__hppa__)
586
587 int
588 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
589 Obj_Entry *obj;
590 const Elf_Rela *rela;
591 caddr_t *addrp;
592 bool bind_now;
593 bool dodebug;
594 {
595 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
596 Elf_Addr new_value;
597
598 /* Fully resolve procedure addresses now */
599
600 if (bind_now || obj->pltgot == NULL) {
601 const Elf_Sym *def;
602 const Obj_Entry *defobj;
603
604 #if !defined(__arm__)
605 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
606 #else
607 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
608 #endif
609
610 def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
611 if (def == NULL)
612 return -1;
613
614 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
615 #if defined(__sh__) || defined(__vax__)
616 new_value += rela->r_addend;
617 #endif
618 rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
619 (int)bind_now,
620 defobj->strtab + def->st_name,
621 (void *)*where, (void *)new_value));
622 } else if (!obj->mainprog) {
623 /* Just relocate the GOT slots pointing into the PLT */
624 new_value = *where + (Elf_Addr)(obj->relocbase);
625 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
626 (void *)*where));
627 } else {
628 return 0;
629 }
630 /*
631 * Since this page is probably copy-on-write, let's not write
632 * it unless we really really have to.
633 */
634 if (*where != new_value)
635 *where = new_value;
636 if (addrp != NULL) {
637 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
638 #if defined(__vax__)
639 *addrp -= rela->r_addend;
640 #endif
641 }
642 return 0;
643 }
644
645 #endif /* __powerpc__ || __hppa__ */
646
647 #endif /* __sparc__ || __x86_64__ || __mips__ */
648
649 caddr_t
650 _rtld_bind(obj, reloff)
651 Obj_Entry *obj;
652 Elf_Word reloff;
653 {
654 const Elf_Rela *rela;
655 Elf_Rela ourrela;
656 caddr_t addr;
657
658 if (obj->pltrel != NULL) {
659 const Elf_Rel *rel;
660
661 rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
662 ourrela.r_info = rel->r_info;
663 ourrela.r_offset = rel->r_offset;
664 ourrela.r_addend = 0;
665 rela = &ourrela;
666 } else {
667 rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
668 #ifdef __sparc64__
669 if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
670 /*
671 * XXXX
672 *
673 * The first four PLT entries are reserved. There
674 * is some disagreement whether they should have
675 * associated relocation entries. Both the SPARC
676 * 32-bit and 64-bit ELF specifications say that
677 * they should have relocation entries, but the
678 * 32-bit SPARC binutils do not generate them,
679 * and now the 64-bit SPARC binutils have stopped
680 * generating them too.
681 *
682 * So, to provide binary compatibility, we will
683 * check the first entry, if it is reserved it
684 * should not be of the type JMP_SLOT. If it
685 * is JMP_SLOT, then the 4 reserved entries were
686 * not generated and our index is 4 entries too far.
687 */
688 rela -= 4;
689 }
690 #endif
691 }
692
693 if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
694 _rtld_die();
695
696 return addr;
697 }
698
699 /*
700 * Relocate newly-loaded shared objects. The argument is a pointer to
701 * the Obj_Entry for the first such object. All objects from the first
702 * to the end of the list of objects are relocated. Returns 0 on success,
703 * or -1 on failure.
704 */
705 int
706 _rtld_relocate_objects(first, bind_now, dodebug)
707 Obj_Entry *first;
708 bool bind_now;
709 bool dodebug;
710 {
711 Obj_Entry *obj;
712 int ok = 1;
713
714 for (obj = first; obj != NULL; obj = obj->next) {
715 if (obj->nbuckets == 0 || obj->nchains == 0 ||
716 obj->buckets == NULL || obj->symtab == NULL ||
717 obj->strtab == NULL) {
718 _rtld_error("%s: Shared object has no run-time"
719 " symbol table", obj->path);
720 return -1;
721 }
722 rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
723 "%ld/%ld plt rel/rela)",
724 obj->path,
725 (long)(obj->rellim - obj->rel),
726 (long)(obj->relalim - obj->rela),
727 (long)(obj->pltrellim - obj->pltrel),
728 (long)(obj->pltrelalim - obj->pltrela)));
729
730 if (obj->textrel) {
731 /*
732 * There are relocations to the write-protected text
733 * segment.
734 */
735 if (mprotect(obj->mapbase, obj->textsize,
736 PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
737 _rtld_error("%s: Cannot write-enable text "
738 "segment: %s", obj->path, xstrerror(errno));
739 return -1;
740 }
741 }
742 if (obj->rel != NULL) {
743 /* Process the non-PLT relocations. */
744 const Elf_Rel *rel;
745 for (rel = obj->rel; rel < obj->rellim; ++rel) {
746 Elf_Rela ourrela;
747 ourrela.r_info = rel->r_info;
748 ourrela.r_offset = rel->r_offset;
749 #if defined(__mips__)
750 /* rel->r_offset is not valid on mips? */
751 if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
752 ourrela.r_addend = 0;
753 else
754 #endif
755 ourrela.r_addend =
756 *(Elf_Word *)(obj->relocbase +
757 rel->r_offset);
758
759 if (_rtld_relocate_nonplt_object(obj, &ourrela,
760 dodebug) < 0)
761 ok = 0;
762 }
763 }
764 if (obj->rela != NULL) {
765 /* Process the non-PLT relocations. */
766 const Elf_Rela *rela;
767 for (rela = obj->rela; rela < obj->relalim; ++rela) {
768 if (_rtld_relocate_nonplt_object(obj, rela,
769 dodebug) < 0)
770 ok = 0;
771 }
772 }
773 if (obj->textrel) { /* Re-protected the text segment. */
774 if (mprotect(obj->mapbase, obj->textsize,
775 PROT_READ | PROT_EXEC) == -1) {
776 _rtld_error("%s: Cannot write-protect text "
777 "segment: %s", obj->path, xstrerror(errno));
778 return -1;
779 }
780 }
781 /* Process the PLT relocations. */
782 if (obj->pltrel != NULL) {
783 const Elf_Rel *rel;
784 for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
785 Elf_Rela ourrela;
786 ourrela.r_info = rel->r_info;
787 ourrela.r_offset = rel->r_offset;
788 ourrela.r_addend =
789 *(Elf_Word *)(obj->relocbase +
790 rel->r_offset);
791 if (_rtld_relocate_plt_object(obj, &ourrela,
792 NULL, bind_now, dodebug) < 0)
793 ok = 0;
794 }
795 }
796 if (obj->pltrela != NULL) {
797 const Elf_Rela *rela;
798 for (rela = obj->pltrela; rela < obj->pltrelalim;
799 ++rela) {
800 #ifdef __sparc64__
801 if (ELF_R_TYPE(rela->r_info) !=
802 R_TYPE(JMP_SLOT)) {
803 /*
804 * XXXX
805 *
806 * The first four PLT entries are
807 * reserved. There is some
808 * disagreement whether they should
809 * have associated relocation
810 * entries. Both the SPARC 32-bit
811 * and 64-bit ELF specifications say
812 * that they should have relocation
813 * entries, but the 32-bit SPARC
814 * binutils do not generate them,
815 * and now the 64-bit SPARC binutils
816 * have stopped generating them too.
817 *
818 * To provide binary compatibility, we
819 * will skip any entries that are not
820 * of type JMP_SLOT.
821 */
822 continue;
823 }
824 #endif
825 if (_rtld_relocate_plt_object(obj, rela,
826 NULL, bind_now, dodebug) < 0)
827 ok = 0;
828 }
829 }
830 if (!ok)
831 return -1;
832
833
834 /* Set some sanity-checking numbers in the Obj_Entry. */
835 obj->magic = RTLD_MAGIC;
836 obj->version = RTLD_VERSION;
837
838 /* Fill in the dynamic linker entry points. */
839 obj->dlopen = _rtld_dlopen;
840 obj->dlsym = _rtld_dlsym;
841 obj->dlerror = _rtld_dlerror;
842 obj->dlclose = _rtld_dlclose;
843 obj->dladdr = _rtld_dladdr;
844
845 /* Set the special PLTGOT entries. */
846 if (obj->pltgot != NULL)
847 _rtld_setup_pltgot(obj);
848 }
849
850 return 0;
851 }
852