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