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