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