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