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