reloc.c revision 1.52 1 /* $NetBSD: reloc.c,v 1.52 2002/07/10 15:12:35 fredette 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(__hppa__) || \
162 defined(__i386__) || defined(__m68k__) || defined(__powerpc__) || \
163 defined(__sh__) || defined(__vax__)
164 Elf_Addr tmp;
165 #endif
166
167 switch (ELF_R_TYPE(rela->r_info)) {
168
169 case R_TYPE(NONE):
170 break;
171
172 #if defined(__i386__)
173 case R_TYPE(GOT32):
174
175 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
176 &defobj, false);
177 if (def == NULL)
178 return -1;
179
180 tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
181 if (*where != tmp)
182 *where = tmp;
183 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
184 defobj->strtab + def->st_name, obj->path,
185 (void *)*where, defobj->path));
186 break;
187
188 case R_TYPE(PC32):
189 /*
190 * I don't think the dynamic linker should ever see this
191 * type of relocation. But the binutils-2.6 tools sometimes
192 * generate it.
193 */
194
195 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
196 &defobj, false);
197 if (def == NULL)
198 return -1;
199
200 *where += (Elf_Addr)(defobj->relocbase + def->st_value) -
201 (Elf_Addr)where;
202 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
203 defobj->strtab + def->st_name, obj->path,
204 (void *)*where, defobj->path));
205 break;
206
207 case R_TYPE(32):
208 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
209 &defobj, false);
210 if (def == NULL)
211 return -1;
212
213 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
214 rdbg(dodebug, ("32 %s in %s --> %p in %s",
215 defobj->strtab + def->st_name, obj->path,
216 (void *)*where, defobj->path));
217 break;
218 #endif /* __i386__ */
219
220 #if defined(__m68k__)
221 case R_TYPE(GOT32):
222 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
223 &defobj, false);
224 if (def == NULL)
225 return -1;
226
227 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
228 rela->r_addend);
229 if (*where != tmp)
230 *where = tmp;
231 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
232 defobj->strtab + def->st_name, obj->path,
233 (void *)*where, defobj->path));
234 break;
235
236 case R_TYPE(PC32):
237 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
238 &defobj, false);
239 if (def == NULL)
240 return -1;
241
242 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
243 rela->r_addend) - (Elf_Addr)where;
244 if (*where != tmp)
245 *where = tmp;
246 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
247 defobj->strtab + def->st_name, obj->path,
248 (void *)*where, defobj->path));
249 break;
250
251 case R_TYPE(32):
252 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
253 &defobj, false);
254 if (def == NULL)
255 return -1;
256
257 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
258 rela->r_addend);
259 if (*where != tmp)
260 *where = tmp;
261 rdbg(dodebug, ("32 %s in %s --> %p in %s",
262 defobj->strtab + def->st_name, obj->path,
263 (void *)*where, defobj->path));
264 break;
265 #endif /* __m68k__ */
266
267 #if defined(__sh__)
268 case R_TYPE(GOT32):
269 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
270 &defobj, false);
271 if (def == NULL)
272 return -1;
273
274 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
275 rela->r_addend);
276 if (*where != tmp)
277 *where = tmp;
278 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
279 defobj->strtab + def->st_name, obj->path,
280 (void *)*where, defobj->path));
281 break;
282
283 case R_TYPE(REL32):
284 /*
285 * I don't think the dynamic linker should ever see this
286 * type of relocation, but some versions of Binutils
287 * generate it.
288 */
289 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
290 &defobj, false);
291 if (def == NULL)
292 return -1;
293
294 *where += (Elf_Addr)(defobj->relocbase + def->st_value +
295 rela->r_addend) - (Elf_Addr)where;
296 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
297 defobj->strtab + def->st_name, obj->path,
298 (void *)*where, defobj->path));
299 break;
300
301 case R_TYPE(DIR32):
302 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
303 &defobj, false);
304 if (def == NULL)
305 return -1;
306
307 *where += (Elf_Addr)(defobj->relocbase + def->st_value +
308 rela->r_addend);
309 rdbg(dodebug, ("32 %s in %s --> %p in %s",
310 defobj->strtab + def->st_name, obj->path,
311 (void *)*where, defobj->path));
312 break;
313 #endif /* __sh__ */
314
315 #if defined(__alpha__)
316 case R_TYPE(REFQUAD):
317 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
318 &defobj, false);
319 if (def == NULL)
320 return -1;
321
322 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
323 *where + rela->r_addend;
324 if (*where != tmp)
325 *where = tmp;
326 rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
327 defobj->strtab + def->st_name, obj->path,
328 (void *)*where, defobj->path));
329 break;
330
331 case R_TYPE(RELATIVE):
332 {
333 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
334 extern Elf_Addr _GOT_END_[];
335
336 /* This is the ...iffy hueristic. */
337 if (!dodebug ||
338 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
339 (caddr_t)where >= (caddr_t)_GOT_END_) {
340 *where += (Elf_Addr)obj->relocbase;
341 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
342 (void *)*where));
343 } else
344 rdbg(dodebug, ("RELATIVE in %s stays at %p",
345 obj->path, (void *)*where));
346 break;
347 }
348 #endif /* __alpha__ */
349
350 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
351 defined(__sh__)
352 case R_TYPE(GLOB_DAT):
353 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
354 &defobj, false);
355 if (def == NULL)
356 return -1;
357
358 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
359 rela->r_addend;
360 if (*where != tmp)
361 *where = tmp;
362 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
363 defobj->strtab + def->st_name, obj->path,
364 (void *)*where, defobj->path));
365 break;
366 #if defined(__sh__)
367 case R_TYPE(RELATIVE):
368 if (rela->r_addend)
369 *where = (Elf_Addr)obj->relocbase + rela->r_addend;
370 else
371 *where += (Elf_Addr)obj->relocbase;
372 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
373 (void *)*where));
374 break;
375 #elif !defined(__alpha__)
376 case R_TYPE(RELATIVE):
377 *where += (Elf_Addr)obj->relocbase;
378 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
379 (void *)*where));
380 break;
381 #endif /* ! __alpha__ */
382 #endif /* __alpha__ || __i386__ || __m68k__ || __sh__ */
383
384 #if defined(__alpha__) || defined(__hppa__) || defined(__i386__) || \
385 defined(__m68k__) || defined(__sh__)
386 case R_TYPE(COPY):
387 /*
388 * These are deferred until all other relocations have
389 * been done. All we do here is make sure that the COPY
390 * relocation is not in a shared library. They are allowed
391 * only in executable files.
392 */
393 if (!obj->mainprog) {
394 _rtld_error(
395 "%s: Unexpected R_COPY relocation in shared library",
396 obj->path);
397 return -1;
398 }
399 rdbg(dodebug, ("COPY (avoid in main)"));
400 break;
401 #endif /* __alpha__ || __hppa__ || __i386__ || __m68k__ || __sh__ */
402
403 #if defined(__mips__)
404 case R_TYPE(REL32):
405 /* 32-bit PC-relative reference */
406 def = obj->symtab + ELF_R_SYM(rela->r_info);
407
408 if (ELFDEFNNAME(ST_BIND)(def->st_info) == STB_LOCAL &&
409 (ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_SECTION ||
410 ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_NOTYPE)) {
411 /*
412 * XXX: ABI DIFFERENCE!
413 *
414 * Old NetBSD binutils would generate shared libs
415 * with section-relative relocations being already
416 * adjusted for the start address of the section.
417 *
418 * New binutils, OTOH, generate shared libs with
419 * the same relocations being based at zero, so we
420 * need to add in the start address of the section.
421 *
422 * We assume that all section-relative relocs with
423 * contents less than the start of the section need
424 * to be adjusted; this should work with both old
425 * and new shlibs.
426 *
427 * --rkb, Oct 6, 2001
428 */
429 if (def->st_info == STT_SECTION &&
430 (*where < def->st_value))
431 *where += (Elf_Addr) def->st_value;
432
433 *where += (Elf_Addr)obj->relocbase;
434
435 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
436 obj->strtab + def->st_name, obj->path,
437 (void *)*where, obj->path));
438 } else {
439 /* XXX maybe do something re: bootstrapping? */
440 def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
441 NULL, obj, &defobj, false);
442 if (def == NULL)
443 return -1;
444 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
445 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
446 defobj->strtab + def->st_name, obj->path,
447 (void *)*where, defobj->path));
448 }
449 break;
450
451 #endif /* __mips__ */
452
453 #if defined(__powerpc__) || defined(__vax__)
454 case R_TYPE(32): /* word32 S + A */
455 case R_TYPE(GLOB_DAT): /* word32 S + A */
456 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
457 &defobj, false);
458 if (def == NULL)
459 return -1;
460
461 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
462 rela->r_addend);
463
464 if (*where != tmp)
465 *where = tmp;
466 rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
467 defobj->strtab + def->st_name, obj->path,
468 (void *)*where, defobj->path));
469 break;
470
471 case R_TYPE(COPY):
472 rdbg(dodebug, ("COPY"));
473 break;
474
475 case R_TYPE(JMP_SLOT):
476 rdbg(dodebug, ("JMP_SLOT"));
477 break;
478
479 case R_TYPE(RELATIVE): /* word32 B + A */
480 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
481 if (*where != tmp)
482 *where = tmp;
483 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
484 (void *)*where));
485 break;
486 #endif /* __powerpc__ || __vax__ */
487
488 #if defined(__powerpc__)
489 case R_TYPE(16_LO): /* #lo(S + A) */
490 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
491 if (*(Elf32_Half *)where != tmp)
492 *(Elf32_Half *)where = tmp;
493 rdbg(dodebug, ("16_LO in %s --> %p", obj->path,
494 (void *)*where));
495 break;
496
497 case R_TYPE(16_HI): /* #hi(S + A) */
498 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend) >> 16;
499 if (*(Elf32_Half *)where != tmp)
500 *(Elf32_Half *)where = tmp;
501 rdbg(dodebug, ("16_HI in %s --> %p", obj->path,
502 (void *)*where));
503 break;
504
505 case R_TYPE(16_HA): /* #ha(S + A) */
506 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend + 0x8000)
507 >> 16;
508 if (*(Elf32_Half *)where != tmp)
509 *(Elf32_Half *)where = tmp;
510 rdbg(dodebug, ("16_HA in %s --> %p", obj->path,
511 (void *)*where));
512 break;
513 #endif /* __powerpc__ */
514
515 #if defined(__arm__)
516 case R_TYPE(GLOB_DAT): /* word32 B + S */
517 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
518 &defobj, false);
519 if (def == NULL)
520 return -1;
521 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
522 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
523 defobj->strtab + def->st_name, obj->path,
524 (void *)*where, where, defobj->path));
525 break;
526
527 case R_TYPE(COPY):
528 rdbg(dodebug, ("COPY"));
529 break;
530
531 case R_TYPE(JUMP_SLOT):
532 rdbg(dodebug, ("JUMP_SLOT"));
533 break;
534
535 case R_TYPE(ABS32): /* word32 B + S + A */
536 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
537 &defobj, false);
538 if (def == NULL)
539 return -1;
540 *where += (Elf_Addr)defobj->relocbase + def->st_value;
541 rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
542 defobj->strtab + def->st_name, obj->path,
543 (void *)*where, where, defobj->path));
544 break;
545
546 case R_TYPE(RELATIVE): /* word32 B + A */
547 *where += (Elf_Addr)obj->relocbase;
548 rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
549 (void *)*where, where));
550 break;
551
552 case R_TYPE(PC24): { /* word32 S - P + A */
553 Elf32_Sword addend;
554
555 /*
556 * Extract addend and sign-extend if needed.
557 */
558 addend = *where;
559 if (addend & 0x00800000)
560 addend |= 0xff000000;
561
562 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
563 &defobj, false);
564 if (def == NULL)
565 return -1;
566 tmp = (Elf_Addr)obj->relocbase + def->st_value
567 - (Elf_Addr)where + (addend << 2);
568 if ((tmp & 0xfe000000) != 0xfe000000 &&
569 (tmp & 0xfe000000) != 0) {
570 _rtld_error(
571 "%s: R_ARM_PC24 relocation @ %p to %s failed "
572 "(displacement %ld (%#lx) out of range)",
573 obj->path, where, defobj->strtab + def->st_name,
574 (long) tmp, (long) tmp);
575 return -1;
576 }
577 tmp >>= 2;
578 *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
579 rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
580 defobj->strtab + def->st_name, obj->path,
581 (void *)*where, where, defobj->path));
582 break;
583 }
584 #endif /* __arm__ */
585
586 #ifdef __hppa__
587 case R_TYPE(DIR32):
588 if (ELF_R_SYM(rela->r_info)) {
589 /*
590 * This is either a DIR32 against a symbol
591 * (def->st_name != 0), or against a local
592 * section (def->st_name == 0).
593 */
594 def = obj->symtab + ELF_R_SYM(rela->r_info);
595 defobj = obj;
596 if (def->st_name != 0)
597 /*
598 * While we're relocating self, _rtld_objlist
599 * is NULL, so we just pass in self.
600 */
601 def = _rtld_find_symdef((_rtld_objlist == NULL ?
602 obj : _rtld_objlist), rela->r_info,
603 NULL, obj, &defobj, false);
604 if (def == NULL)
605 return -1;
606
607 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
608 rela->r_addend);
609
610 if (*where != tmp)
611 *where = tmp;
612 rdbg(dodebug, ("DIR32 %s in %s --> %p in %s",
613 defobj->strtab + def->st_name, obj->path,
614 (void *)*where, defobj->path));
615 } else {
616 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
617 extern Elf_Addr _GOT_END_[];
618
619 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
620
621 /* This is the ...iffy hueristic. */
622 if (!dodebug ||
623 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
624 (caddr_t)where >= (caddr_t)_GOT_END_) {
625 if (*where != tmp)
626 *where = tmp;
627 rdbg(dodebug, ("DIR32 in %s --> %p", obj->path,
628 (void *)*where));
629 } else
630 rdbg(dodebug, ("DIR32 in %s stays at %p",
631 obj->path, (void *)*where));
632 }
633 break;
634
635 case R_TYPE(PLABEL32):
636 if (ELF_R_SYM(rela->r_info)) {
637 /*
638 * While we're relocating self, _rtld_objlist
639 * is NULL, so we just pass in self.
640 */
641 def = _rtld_find_symdef((_rtld_objlist == NULL ?
642 obj : _rtld_objlist), rela->r_info,
643 NULL, obj, &defobj, false);
644 if (def == NULL)
645 return -1;
646
647 tmp = _rtld_function_descriptor_alloc(defobj, def,
648 rela->r_addend);
649 if (tmp == (Elf_Addr)-1)
650 return -1;
651
652 if (*where != tmp)
653 *where = tmp;
654 rdbg(dodebug, ("PLABEL32 %s in %s --> %p in %s",
655 defobj->strtab + def->st_name, obj->path,
656 (void *)*where, defobj->path));
657 } else {
658 /*
659 * This is a PLABEL for a static function, and the
660 * dynamic linker has both allocated a PLT entry
661 * for this function and told us where it is. We
662 * can safely use the PLT entry as the PLABEL
663 * because there should be no other PLABEL reloc
664 * referencing this function. This object should
665 * also have an IPLT relocation to initialize the
666 * PLT entry.
667 *
668 * The dynamic linker should also have ensured
669 * that the addend has the next-least-significant
670 * bit set; the $$dyncall millicode uses this to
671 * distinguish a PLABEL pointer from a plain
672 * function pointer.
673 */
674 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
675
676 if (*where != tmp)
677 *where = tmp;
678 rdbg(dodebug, ("PLABEL32 in %s --> %p",
679 obj->path, (void *)*where));
680 }
681 break;
682 #endif /* __hppa__ */
683
684 default:
685 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
686 &defobj, true);
687 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
688 "addend = %p, contents = %p, symbol = %s",
689 (u_long)ELF_R_SYM(rela->r_info),
690 (u_long)ELF_R_TYPE(rela->r_info),
691 (void *)rela->r_offset, (void *)rela->r_addend,
692 (void *)*where,
693 def ? defobj->strtab + def->st_name : "??"));
694 _rtld_error("%s: Unsupported relocation type %ld "
695 "in non-PLT relocations\n",
696 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
697 return -1;
698 }
699 return 0;
700 }
701
702
703 #if !defined(__powerpc__) && !defined(__hppa__)
704
705 int
706 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
707 Obj_Entry *obj;
708 const Elf_Rela *rela;
709 caddr_t *addrp;
710 bool bind_now;
711 bool dodebug;
712 {
713 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
714 Elf_Addr new_value;
715
716 /* Fully resolve procedure addresses now */
717
718 #if defined(__alpha__) || defined(__arm__) || defined(__i386__) || \
719 defined(__m68k__) || defined(__sh__) || defined(__vax__)
720 if (bind_now || obj->pltgot == NULL) {
721 const Elf_Sym *def;
722 const Obj_Entry *defobj;
723
724 #if !defined(__arm__)
725 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
726 #else
727 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
728 #endif
729
730 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
731 &defobj, true);
732 if (def == NULL)
733 return -1;
734
735 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
736 #if defined(__sh__) || defined(__vax__)
737 new_value += rela->r_addend;
738 #endif
739 rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
740 (int)bind_now,
741 defobj->strtab + def->st_name,
742 (void *)*where, (void *)new_value));
743 } else
744 #endif /* __alpha__ || __i386__ || __m68k__ || __sh__ || __vax__ */
745 if (!obj->mainprog) {
746 /* Just relocate the GOT slots pointing into the PLT */
747 new_value = *where + (Elf_Addr)(obj->relocbase);
748 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
749 (void *)*where));
750 } else {
751 return 0;
752 }
753 /*
754 * Since this page is probably copy-on-write, let's not write
755 * it unless we really really have to.
756 */
757 if (*where != new_value)
758 *where = new_value;
759 if (addrp != NULL) {
760 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
761 #if defined(__vax__)
762 *addrp -= rela->r_addend;
763 #endif
764 }
765 return 0;
766 }
767
768 #endif /* __powerpc__ || __hppa__ */
769
770 #endif /* __sparc__ || __x86_64__ */
771
772 caddr_t
773 _rtld_bind(obj, reloff)
774 Obj_Entry *obj;
775 Elf_Word reloff;
776 {
777 const Elf_Rela *rela;
778 Elf_Rela ourrela;
779 caddr_t addr;
780
781 if (obj->pltrel != NULL) {
782 const Elf_Rel *rel;
783
784 rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
785 ourrela.r_info = rel->r_info;
786 ourrela.r_offset = rel->r_offset;
787 ourrela.r_addend = 0;
788 rela = &ourrela;
789 } else {
790 rela = (const Elf_Rela *)((caddr_t) obj->pltrela + reloff);
791 #ifdef __sparc64__
792 if (ELF_R_TYPE(obj->pltrela->r_info) == R_TYPE(JMP_SLOT)) {
793 /*
794 * XXXX
795 *
796 * The first four PLT entries are reserved. There
797 * is some disagreement whether they should have
798 * associated relocation entries. Both the SPARC
799 * 32-bit and 64-bit ELF specifications say that
800 * they should have relocation entries, but the
801 * 32-bit SPARC binutils do not generate them,
802 * and now the 64-bit SPARC binutils have stopped
803 * generating them too.
804 *
805 * So, to provide binary compatibility, we will
806 * check the first entry, if it is reserved it
807 * should not be of the type JMP_SLOT. If it
808 * is JMP_SLOT, then the 4 reserved entries were
809 * not generated and our index is 4 entries too far.
810 */
811 rela -= 4;
812 }
813 #endif
814 }
815
816 if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
817 _rtld_die();
818
819 return addr;
820 }
821
822 /*
823 * Relocate newly-loaded shared objects. The argument is a pointer to
824 * the Obj_Entry for the first such object. All objects from the first
825 * to the end of the list of objects are relocated. Returns 0 on success,
826 * or -1 on failure.
827 */
828 int
829 _rtld_relocate_objects(first, bind_now, dodebug)
830 Obj_Entry *first;
831 bool bind_now;
832 bool dodebug;
833 {
834 Obj_Entry *obj;
835 int ok = 1;
836
837 for (obj = first; obj != NULL; obj = obj->next) {
838 if (obj->nbuckets == 0 || obj->nchains == 0 ||
839 obj->buckets == NULL || obj->symtab == NULL ||
840 obj->strtab == NULL) {
841 _rtld_error("%s: Shared object has no run-time"
842 " symbol table", obj->path);
843 return -1;
844 }
845 rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
846 "%ld/%ld plt rel/rela)",
847 obj->path,
848 (long)(obj->rellim - obj->rel),
849 (long)(obj->relalim - obj->rela),
850 (long)(obj->pltrellim - obj->pltrel),
851 (long)(obj->pltrelalim - obj->pltrela)));
852
853 if (obj->textrel) {
854 /*
855 * There are relocations to the write-protected text
856 * segment.
857 */
858 if (mprotect(obj->mapbase, obj->textsize,
859 PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
860 _rtld_error("%s: Cannot write-enable text "
861 "segment: %s", obj->path, xstrerror(errno));
862 return -1;
863 }
864 }
865 if (obj->rel != NULL) {
866 /* Process the non-PLT relocations. */
867 const Elf_Rel *rel;
868 for (rel = obj->rel; rel < obj->rellim; ++rel) {
869 Elf_Rela ourrela;
870 ourrela.r_info = rel->r_info;
871 ourrela.r_offset = rel->r_offset;
872 #if defined(__mips__)
873 /* rel->r_offset is not valid on mips? */
874 if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
875 ourrela.r_addend = 0;
876 else
877 #endif
878 ourrela.r_addend =
879 *(Elf_Word *)(obj->relocbase +
880 rel->r_offset);
881
882 if (_rtld_relocate_nonplt_object(obj, &ourrela,
883 dodebug) < 0)
884 ok = 0;
885 }
886 }
887 if (obj->rela != NULL) {
888 /* Process the non-PLT relocations. */
889 const Elf_Rela *rela;
890 for (rela = obj->rela; rela < obj->relalim; ++rela) {
891 if (_rtld_relocate_nonplt_object(obj, rela,
892 dodebug) < 0)
893 ok = 0;
894 }
895 }
896 if (obj->textrel) { /* Re-protected the text segment. */
897 if (mprotect(obj->mapbase, obj->textsize,
898 PROT_READ | PROT_EXEC) == -1) {
899 _rtld_error("%s: Cannot write-protect text "
900 "segment: %s", obj->path, xstrerror(errno));
901 return -1;
902 }
903 }
904 /* Process the PLT relocations. */
905 if (obj->pltrel != NULL) {
906 const Elf_Rel *rel;
907 for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
908 Elf_Rela ourrela;
909 ourrela.r_info = rel->r_info;
910 ourrela.r_offset = rel->r_offset;
911 ourrela.r_addend =
912 *(Elf_Word *)(obj->relocbase +
913 rel->r_offset);
914 if (_rtld_relocate_plt_object(obj, &ourrela,
915 NULL, bind_now, dodebug) < 0)
916 ok = 0;
917 }
918 }
919 if (obj->pltrela != NULL) {
920 const Elf_Rela *rela;
921 for (rela = obj->pltrela; rela < obj->pltrelalim;
922 ++rela) {
923 #ifdef __sparc64__
924 if (ELF_R_TYPE(rela->r_info) !=
925 R_TYPE(JMP_SLOT)) {
926 /*
927 * XXXX
928 *
929 * The first four PLT entries are
930 * reserved. There is some
931 * disagreement whether they should
932 * have associated relocation
933 * entries. Both the SPARC 32-bit
934 * and 64-bit ELF specifications say
935 * that they should have relocation
936 * entries, but the 32-bit SPARC
937 * binutils do not generate them,
938 * and now the 64-bit SPARC binutils
939 * have stopped generating them too.
940 *
941 * To provide binary compatibility, we
942 * will skip any entries that are not
943 * of type JMP_SLOT.
944 */
945 continue;
946 }
947 #endif
948 if (_rtld_relocate_plt_object(obj, rela,
949 NULL, bind_now, dodebug) < 0)
950 ok = 0;
951 }
952 }
953 if (!ok)
954 return -1;
955
956
957 /* Set some sanity-checking numbers in the Obj_Entry. */
958 obj->magic = RTLD_MAGIC;
959 obj->version = RTLD_VERSION;
960
961 /* Fill in the dynamic linker entry points. */
962 obj->dlopen = _rtld_dlopen;
963 obj->dlsym = _rtld_dlsym;
964 obj->dlerror = _rtld_dlerror;
965 obj->dlclose = _rtld_dlclose;
966 obj->dladdr = _rtld_dladdr;
967
968 /* Set the special PLTGOT entries. */
969 if (obj->pltgot != NULL) {
970 #if defined(__arm__) || defined(__i386__) || defined(__m68k__) || \
971 defined(__sh__) || defined(__x86_64__)
972 obj->pltgot[1] = (Elf_Addr) obj;
973 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
974 #endif
975 #if defined(__alpha__)
976 _rtld_setup_alpha_pltgot(obj, dodebug);
977 #endif
978 #if defined(__hppa__)
979 _rtld_setup_hppa_pltgot(obj);
980 #endif
981 #if defined(__mips__)
982 _rtld_relocate_mips_got(obj);
983
984 obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
985 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
986 obj->pltgot[1] |= (Elf_Addr) obj;
987 #endif
988 #if defined(__powerpc__)
989 _rtld_setup_powerpc_plt(obj);
990 #endif
991 #if defined(__sparc__)
992 #if defined(__arch64__) || defined(__sparc_v9__)
993 /*
994 * On sparc64 we got troubles.
995 *
996 * Instructions are 4 bytes long.
997 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
998 * array entries.
999 * Each PLT entry jumps to PLT0 to enter the dynamic
1000 * linker.
1001 * Loading an arbitrary 64-bit pointer takes 6
1002 * instructions and 2 registers.
1003 *
1004 * Somehow we need to issue a save to get a new stack
1005 * frame, load the address of the dynamic linker, and
1006 * jump there, in 8 instructions or less.
1007 *
1008 * Oh, we need to fill out both PLT0 and PLT1.
1009 */
1010 {
1011 Elf_Word *entry = (Elf_Word *)obj->pltgot;
1012 extern void _rtld_install_plt __P((Elf_Word *,
1013 Elf_Addr));
1014 extern void _rtld_bind_start_0 __P((long,
1015 long));
1016 extern void _rtld_bind_start_1 __P((long,
1017 long));
1018
1019 /* Install in entries 0 and 1 */
1020 _rtld_install_plt(&entry[0],
1021 (Elf_Addr)&_rtld_bind_start_0);
1022 _rtld_install_plt(&entry[8],
1023 (Elf_Addr)&_rtld_bind_start_1);
1024
1025 /*
1026 * Install the object reference in first slot
1027 * of entry 2.
1028 */
1029 obj->pltgot[8] = (Elf_Addr) obj;
1030 }
1031 #else
1032 /*
1033 * PLTGOT is the PLT on the sparc.
1034 * The first entry holds the call the dynamic linker.
1035 * We construct a `call' sequence that transfers
1036 * to `_rtld_bind_start()'.
1037 * The second entry holds the object identification.
1038 * Note: each PLT entry is three words long.
1039 */
1040 #define SAVE 0x9de3bfc0 /* i.e. `save %sp,-64,%sp' */
1041 #define CALL 0x40000000
1042 #define NOP 0x01000000
1043 obj->pltgot[0] = SAVE;
1044 obj->pltgot[1] = CALL |
1045 ((Elf_Addr)&_rtld_bind_start -
1046 (Elf_Addr)&obj->pltgot[1]) >> 2;
1047 obj->pltgot[2] = NOP;
1048
1049 obj->pltgot[3] = (Elf_Addr) obj;
1050 #endif /* __arch64__ */
1051 #endif /* __sparc__ */
1052 #if defined(__vax__)
1053 obj->pltgot[1] = (Elf_Addr) obj;
1054 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
1055 #endif
1056 }
1057 }
1058
1059 return 0;
1060 }
1061