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