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