reloc.c revision 1.17 1 /* $NetBSD: reloc.c,v 1.17 1999/03/03 12:20:00 jonathan 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;
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 %d",
96 dstobj->path, srcobj->path, name, (void *)srcaddr,
97 (void *)dstaddr, (void *)*(long *)dstaddr, 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 #ifndef __sparc__
151 int
152 _rtld_relocate_nonplt_object(obj, rela, dodebug)
153 const Obj_Entry *obj;
154 const Elf_RelA *rela;
155 bool dodebug;
156 {
157 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
158 const Elf_Sym *def;
159 const Obj_Entry *defobj;
160 #if defined(__i386__) || defined(__alpha__)
161 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
162 extern Elf_Dyn _DYNAMIC;
163 #endif
164 #if defined(__i386__) || defined(__alpha__) || defined(__powerpc__)
165 Elf_Addr tmp;
166 #endif
167
168 switch (ELF_R_TYPE(rela->r_info)) {
169
170 case R_TYPE(NONE):
171 break;
172
173 #ifdef __i386__
174 case R_TYPE(GOT32):
175
176 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
177 &defobj, false);
178 if (def == NULL)
179 return -1;
180
181 tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
182 if (*where != tmp)
183 *where = tmp;
184 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
185 defobj->strtab + def->st_name, obj->path,
186 (void *)*where, defobj->path));
187 break;
188
189 case R_TYPE(PC32):
190 /*
191 * I don't think the dynamic linker should ever see this
192 * type of relocation. But the binutils-2.6 tools sometimes
193 * generate it.
194 */
195
196 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
197 &defobj, false);
198 if (def == NULL)
199 return -1;
200
201 *where += (Elf_Addr)(defobj->relocbase + def->st_value) -
202 (Elf_Addr)where;
203 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
204 defobj->strtab + def->st_name, obj->path,
205 (void *)*where, defobj->path));
206 break;
207
208 case R_TYPE(32):
209 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
210 &defobj, false);
211 if (def == NULL)
212 return -1;
213
214 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
215 rdbg(dodebug, ("32 %s in %s --> %p in %s",
216 defobj->strtab + def->st_name, obj->path,
217 (void *)*where, defobj->path));
218 break;
219 #endif /* __i386__ */
220
221 #ifdef __alpha__
222 case R_TYPE(REFQUAD):
223 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
224 &defobj, false);
225 if (def == NULL)
226 return -1;
227
228 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
229 *where + rela->r_addend;
230 if (*where != tmp)
231 *where = tmp;
232 rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
233 defobj->strtab + def->st_name, obj->path,
234 (void *)*where, defobj->path));
235 break;
236 #endif /* __alpha__ */
237
238 #if defined(__i386__) || defined(__alpha__)
239 case R_TYPE(GLOB_DAT):
240 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
241 &defobj, false);
242 if (def == NULL)
243 return -1;
244
245 if (*where != (Elf_Addr)(defobj->relocbase + def->st_value))
246 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
247 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
248 defobj->strtab + def->st_name, obj->path,
249 (void *)*where, defobj->path));
250 break;
251
252 case R_TYPE(RELATIVE):
253 if ((caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
254 (caddr_t)where >= (caddr_t)&_DYNAMIC) {
255 *where += (Elf_Addr)obj->relocbase;
256 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
257 (void *)*where));
258 }
259 else
260 rdbg(dodebug, ("RELATIVE in %s stays at %p",
261 obj->path, (void *)*where));
262 break;
263
264 case R_TYPE(COPY):
265 /*
266 * These are deferred until all other relocations have
267 * been done. All we do here is make sure that the COPY
268 * relocation is not in a shared library. They are allowed
269 * only in executable files.
270 */
271 if (!obj->mainprog) {
272 _rtld_error(
273 "%s: Unexpected R_COPY relocation in shared library",
274 obj->path);
275 return -1;
276 }
277 rdbg(dodebug, ("COPY (avoid in main)"));
278 break;
279 #endif /* __i386__ || __alpha__ */
280
281 #ifdef __mips__
282 case R_TYPE(REL32):
283 /* 32-bit PC-relative reference */
284 def = obj->symtab + ELF_R_SYM(rela->r_info);
285
286 if (ELF_SYM_BIND(def->st_info) == Elf_estb_local &&
287 (ELF_SYM_TYPE(def->st_info) == Elf_estt_section ||
288 ELF_SYM_TYPE(def->st_info) == Elf_estt_notype)) {
289 *where += (Elf_Addr)obj->relocbase;
290 rdbg(dodebug, ("REL32 in %s --> %p", obj->path,
291 (void *)*where));
292 } else {
293 /* XXX maybe do something re: bootstrapping? */
294 def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
295 NULL, obj, &defobj, false);
296 if (def == NULL)
297 return -1;
298 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
299 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
300 defobj->strtab + def->st_name, obj->path,
301 (void *)*where, defobj->path));
302 }
303 break;
304
305 #endif /* __mips__ */
306
307 #ifdef __powerpc__
308 case R_TYPE(32): /* word32 S + A */
309 case R_TYPE(GLOB_DAT): /* word32 S + A */
310 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
311 &defobj, false);
312 if (def == NULL)
313 return -1;
314
315 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
316 rela->r_addend);
317
318 if (*where != tmp)
319 *where = tmp;
320 rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
321 defobj->strtab + def->st_name, obj->path,
322 (void *)*where, defobj->path));
323 break;
324
325 case R_TYPE(COPY):
326 rdbg(dodebug, ("COPY"));
327 break;
328
329 case R_TYPE(JMP_SLOT):
330 rdbg(dodebug, ("JMP_SLOT"));
331 break;
332
333 case R_TYPE(RELATIVE): /* word32 B + A */
334 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
335 if (obj == &_rtld_objself && *where == tmp)
336 break; /* GOT - already done */
337
338 *where = tmp;
339 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
340 (void *)*where));
341 break;
342 #endif /* __powerpc__ */
343
344 default:
345 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
346 &defobj, true);
347 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
348 "addend = %p, contents = %p, symbol = %s",
349 (u_long)ELF_R_SYM(rela->r_info),
350 (u_long)ELF_R_TYPE(rela->r_info),
351 (void *)rela->r_offset, (void *)rela->r_addend,
352 (void *)*where,
353 def ? defobj->strtab + def->st_name : "??"));
354 _rtld_error("%s: Unsupported relocation type %d"
355 "in non-PLT relocations\n",
356 obj->path, ELF_R_TYPE(rela->r_info));
357 return -1;
358 }
359 return 0;
360 }
361
362
363
364 int
365 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
366 const Obj_Entry *obj;
367 const Elf_RelA *rela;
368 caddr_t *addrp;
369 bool bind_now;
370 bool dodebug;
371 {
372 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
373 Elf_Addr new_value;
374
375 /* Fully resolve procedure addresses now */
376
377 #if defined(__powerpc__)
378 return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
379 #endif
380
381 #if defined(__alpha__) || defined(__i386__)
382 if (bind_now || obj->pltgot == NULL) {
383 const Elf_Sym *def;
384 const Obj_Entry *defobj;
385
386 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
387
388 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
389 &defobj, true);
390 if (def == NULL)
391 return -1;
392
393 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
394 rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
395 (int)bind_now,
396 defobj->strtab + def->st_name,
397 (void *)*where, (void *)new_value));
398 } else
399 #endif /* __alpha__ || __i386__ */
400 if (!obj->mainprog) {
401 /* Just relocate the GOT slots pointing into the PLT */
402 new_value = *where + (Elf_Addr)(obj->relocbase);
403 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
404 (void *)*where));
405 } else {
406 return 0;
407 }
408 /*
409 * Since this page is probably copy-on-write, let's not write
410 * it unless we really really have to.
411 */
412 if (*where != new_value)
413 *where = new_value;
414 if (addrp != NULL)
415 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
416 return 0;
417 }
418 #endif /* __sparc__ */
419
420 caddr_t
421 _rtld_bind(obj, reloff)
422 const Obj_Entry *obj;
423 Elf_Word reloff;
424 {
425 const Elf_RelA *rela;
426 Elf_RelA ourrela;
427 caddr_t addr;
428
429 if (obj->pltrel != NULL) {
430 const Elf_Rel *rel;
431
432 rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
433 ourrela.r_info = rel->r_info;
434 ourrela.r_offset = rel->r_offset;
435 rela = &ourrela;
436 } else {
437 rela = (const Elf_RelA *)((caddr_t) obj->pltrela + reloff);
438 }
439
440 if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
441 _rtld_die();
442
443 return addr;
444 }
445
446 /*
447 * Relocate newly-loaded shared objects. The argument is a pointer to
448 * the Obj_Entry for the first such object. All objects from the first
449 * to the end of the list of objects are relocated. Returns 0 on success,
450 * or -1 on failure.
451 */
452 int
453 _rtld_relocate_objects(first, bind_now, dodebug)
454 Obj_Entry *first;
455 bool bind_now;
456 bool dodebug;
457 {
458 Obj_Entry *obj;
459 int ok = 1;
460
461 for (obj = first; obj != NULL; obj = obj->next) {
462 if (obj->nbuckets == 0 || obj->nchains == 0 ||
463 obj->buckets == NULL || obj->symtab == NULL ||
464 obj->strtab == NULL) {
465 _rtld_error("%s: Shared object has no run-time"
466 " symbol table", obj->path);
467 return -1;
468 }
469 rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
470 "%ld/%ld plt rel/rela)",
471 obj->path,
472 (long)(obj->rellim - obj->rel),
473 (long)(obj->relalim - obj->rela),
474 (long)(obj->pltrellim - obj->pltrel),
475 (long)(obj->pltrelalim - obj->pltrela)));
476
477 if (obj->textrel) {
478 /*
479 * There are relocations to the write-protected text
480 * segment.
481 */
482 if (mprotect(obj->mapbase, obj->textsize,
483 PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
484 _rtld_error("%s: Cannot write-enable text "
485 "segment: %s", obj->path, xstrerror(errno));
486 return -1;
487 }
488 }
489 if (obj->rel != NULL) {
490 /* Process the non-PLT relocations. */
491 const Elf_Rel *rel;
492 for (rel = obj->rel; rel < obj->rellim; ++rel) {
493 Elf_RelA ourrela;
494 ourrela.r_info = rel->r_info;
495 ourrela.r_offset = rel->r_offset;
496 #if defined(__mips__)
497 /* rel->r_offset is not valid on mips? */
498 if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
499 ourrela.r_addend = 0;
500 else
501 #endif
502 ourrela.r_addend =
503 *(Elf_Word *)(obj->relocbase +
504 rel->r_offset);
505
506 if (_rtld_relocate_nonplt_object(obj, &ourrela,
507 dodebug) < 0)
508 ok = 0;
509 }
510 }
511 if (obj->rela != NULL) {
512 /* Process the non-PLT relocations. */
513 const Elf_RelA *rela;
514 for (rela = obj->rela; rela < obj->relalim; ++rela) {
515 if (_rtld_relocate_nonplt_object(obj, rela,
516 dodebug) < 0)
517 ok = 0;
518 }
519 }
520 if (obj->textrel) { /* Re-protected the text segment. */
521 if (mprotect(obj->mapbase, obj->textsize,
522 PROT_READ | PROT_EXEC) == -1) {
523 _rtld_error("%s: Cannot write-protect text "
524 "segment: %s", obj->path, xstrerror(errno));
525 return -1;
526 }
527 }
528 /* Process the PLT relocations. */
529 if (obj->pltrel != NULL) {
530 const Elf_Rel *rel;
531 for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
532 Elf_RelA ourrela;
533 ourrela.r_info = rel->r_info;
534 ourrela.r_offset = rel->r_offset;
535 ourrela.r_addend =
536 *(Elf_Word *)(obj->relocbase +
537 rel->r_offset);
538 if (_rtld_relocate_plt_object(obj, &ourrela,
539 NULL, bind_now, dodebug) < 0)
540 ok = 0;
541 }
542 }
543 if (obj->pltrela != NULL) {
544 const Elf_RelA *rela;
545 for (rela = obj->pltrela; rela < obj->pltrelalim;
546 ++rela) {
547 if (_rtld_relocate_plt_object(obj, rela,
548 NULL, bind_now, dodebug) < 0)
549 ok = 0;
550 }
551 }
552 if (!ok)
553 return -1;
554
555
556 /* Set some sanity-checking numbers in the Obj_Entry. */
557 obj->magic = RTLD_MAGIC;
558 obj->version = RTLD_VERSION;
559
560 /* Fill in the dynamic linker entry points. */
561 obj->dlopen = _rtld_dlopen;
562 obj->dlsym = _rtld_dlsym;
563 obj->dlerror = _rtld_dlerror;
564 obj->dlclose = _rtld_dlclose;
565
566 /* Set the special PLTGOT entries. */
567 if (obj->pltgot != NULL) {
568 #if defined(__i386__)
569 obj->pltgot[1] = (Elf_Addr) obj;
570 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
571 #endif
572 #if defined(__alpha__)
573 /*
574 * This function will be called to perform the
575 * relocation.
576 */
577 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
578 /* Identify this shared object */
579 obj->pltgot[3] = (Elf_Addr) obj;
580 #endif
581 #if defined(__mips__)
582 _rtld_relocate_mips_got(obj);
583
584 obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
585 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
586 obj->pltgot[1] |= (Elf_Addr) obj;
587 #endif
588 #if defined(__powerpc__)
589 _rtld_setup_powerpc_plt(obj);
590 #endif
591 #if defined(__sparc__)
592 /*
593 * PLTGOT is the PLT on the sparc.
594 * The first entry holds the call the dynamic linker.
595 * We construct a `call' sequence that transfers
596 * to `_rtld_bind_start()'.
597 * The second entry holds the object identification.
598 * Note: each PLT entry is three words long.
599 */
600 #define SAVE 0x9de3bfc0 /* i.e. `save %sp,-64,%sp' */
601 #define CALL 0x40000000
602 #define NOP 0x01000000
603 obj->pltgot[0] = SAVE;
604 obj->pltgot[1] = CALL |
605 ((Elf_Addr)&_rtld_bind_start -
606 (Elf_Addr)&obj->pltgot[1]) >> 2;
607 obj->pltgot[2] = NOP;
608
609 obj->pltgot[3] = (Elf_Addr) obj;
610 #endif
611 }
612 }
613
614 return 0;
615 }
616