reloc.c revision 1.2 1 /* $NetBSD: reloc.c,v 1.2 1998/03/25 04:13:01 mhitch 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 #ifdef __alpha__
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__ */
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 #ifdef __alpha__ /* 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 default: {
261 const Elf_Sym *def;
262 const Obj_Entry *defobj;
263
264 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, true);
265 dbg("sym = %d, type = %d, offset = %p, addend = %p, contents = %p, symbol = %s",
266 ELF_R_SYM(rela->r_info), ELF_R_TYPE(rela->r_info),
267 rela->r_offset, rela->r_addend, *where,
268 def ? defobj->strtab + def->st_name : "??");
269 _rtld_error("%s: Unsupported relocation type %d in non-PLT relocations\n",
270 obj->path, ELF_R_TYPE(rela->r_info));
271 return -1;
272 }
273 }
274 return 0;
275 }
276
277 static int
279 _rtld_relocate_plt_object(
280 const Obj_Entry *obj,
281 const Elf_RelA *rela,
282 bool bind_now)
283 {
284 Elf_Addr *where = (Elf_Addr *) (obj->relocbase + rela->r_offset);
285 Elf_Addr new_value;
286
287 /* Fully resolve procedure addresses now */
288
289 #if defined(__alpha__) /* (jrs) */
290 if (bind_now || obj->pltgot == NULL) {
291 const Elf_Sym *def;
292 const Obj_Entry *defobj;
293
294 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
295
296 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj, &defobj, true);
297 if (def == NULL)
298 return -1;
299
300 new_value = (Elf_Addr) (defobj->relocbase + def->st_value);
301 #if 0
302 dbg("fixup %s in %s --> %p in %s",
303 defobj->strtab + def->st_name, obj->path,
304 new_value, defobj->path);
305 #endif
306 } else
307 #endif /* __alpha__ (jrs) */
308 if (!obj->mainprog) {
309 /* Just relocate the GOT slots pointing into the PLT */
310 new_value = *where + (Elf_Addr) (obj->relocbase);
311 } else {
312 return 0;
313 }
314 /*
315 * Since this page is probably copy-on-write, let's not write
316 * it unless we really really have to.
317 */
318 if (*where != new_value)
319 *where = new_value;
320 return 0;
321 }
322
323 caddr_t
325 _rtld_bind(
326 const Obj_Entry *obj,
327 Elf_Word reloff)
328 {
329 const Elf_RelA *rela;
330 Elf_RelA ourrela;
331
332 if (obj->pltrel != NULL) {
333 ourrela.r_info = ((const Elf_Rel *) ((caddr_t) obj->pltrel + reloff))->r_info;
334 ourrela.r_offset = ((const Elf_Rel *) ((caddr_t) obj->pltrel + reloff))->r_offset;
335 rela = &ourrela;
336 } else {
337 rela = (const Elf_RelA *) ((caddr_t) obj->pltrela + reloff);
338 }
339
340
341 if (_rtld_relocate_plt_object(obj, rela, true) < 0)
342 _rtld_die();
343
344 return *(caddr_t *)(obj->relocbase + rela->r_offset);
345 }
346
347 /*
349 * Relocate newly-loaded shared objects. The argument is a pointer to
350 * the Obj_Entry for the first such object. All objects from the first
351 * to the end of the list of objects are relocated. Returns 0 on success,
352 * or -1 on failure.
353 */
354 int
355 _rtld_relocate_objects(
356 Obj_Entry *first,
357 bool bind_now)
358 {
359 Obj_Entry *obj;
360 int ok = 1;
361
362 for (obj = first; obj != NULL; obj = obj->next) {
363
364 if (obj->nbuckets == 0 || obj->nchains == 0
365 || obj->buckets == NULL || obj->symtab == NULL
366 || obj->strtab == NULL) {
367 _rtld_error("%s: Shared object has no run-time symbol table",
368 obj->path);
369 return -1;
370 }
371
372 dbg(" relocating %s (%d/%d rel/rela, %d/%d plt rel/rela)",
373 obj->path,
374 obj->rellim - obj->rel, obj->relalim - obj->rela,
375 obj->pltrellim - obj->pltrel, obj->pltrelalim - obj->pltrela);
376
377 if (obj->textrel) {
378 /* There are relocations to the write-protected text segment. */
379 if (mprotect(obj->mapbase, obj->textsize,
380 PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
381 _rtld_error("%s: Cannot write-enable text segment: %s",
382 obj->path, xstrerror(errno));
383 return -1;
384 }
385 }
386
387 if (obj->rel != NULL) {
388 /* Process the non-PLT relocations. */
389 const Elf_Rel *rel;
390 for (rel = obj->rel; rel < obj->rellim; ++rel) {
391 Elf_RelA ourrela;
392 ourrela.r_info = rel->r_info;
393 ourrela.r_offset = rel->r_offset;
394 #if defined(__mips__)
395 /* rel->r_offset is not valid on mips? */
396 if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
397 ourrela.r_addend = 0;
398 else
399 #endif
400 ourrela.r_addend = *(Elf_Word *) (obj->relocbase + rel->r_offset);
401
402 if (_rtld_relocate_nonplt_object(obj, &ourrela) < 0)
403 ok = 0;
404 }
405 }
406
407 if (obj->rela != NULL) {
408 /* Process the non-PLT relocations. */
409 const Elf_RelA *rela;
410 for (rela = obj->rela; rela < obj->relalim; ++rela) {
411 if (_rtld_relocate_nonplt_object(obj, rela) < 0)
412 ok = 0;
413 }
414 }
415
416 if (obj->textrel) { /* Re-protected the text segment. */
417 if (mprotect(obj->mapbase, obj->textsize,
418 PROT_READ|PROT_EXEC) == -1) {
419 _rtld_error("%s: Cannot write-protect text segment: %s",
420 obj->path, xstrerror(errno));
421 return -1;
422 }
423 }
424
425 /* Process the PLT relocations. */
426 if (obj->pltrel != NULL) {
427 const Elf_Rel *rel;
428 for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
429 Elf_RelA ourrela;
430 ourrela.r_info = rel->r_info;
431 ourrela.r_offset = rel->r_offset;
432 ourrela.r_addend = *(Elf_Word *) (obj->relocbase + rel->r_offset);
433 if (_rtld_relocate_plt_object(obj, &ourrela, bind_now) < 0)
434 ok = 0;
435 }
436 }
437
438 if (obj->pltrela != NULL) {
439 const Elf_RelA *rela;
440 for (rela = obj->pltrela; rela < obj->pltrelalim; ++rela) {
441 if (_rtld_relocate_plt_object(obj, rela, bind_now) < 0)
442 ok = 0;
443 }
444 }
445
446 if (!ok)
447 return -1;
448
449
450 /* Set some sanity-checking numbers in the Obj_Entry. */
451 obj->magic = RTLD_MAGIC;
452 obj->version = RTLD_VERSION;
453
454 /* Fill in the dynamic linker entry points. */
455 obj->dlopen = _rtld_dlopen;
456 obj->dlsym = _rtld_dlsym;
457 obj->dlerror = _rtld_dlerror;
458 obj->dlclose = _rtld_dlclose;
459
460 /* Set the special PLTGOT entries. */
461 if (obj->pltgot != NULL) {
462 #if defined(__i386__)
463 obj->pltgot[1] = (Elf_Addr) obj;
464 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
465 #endif
466 #if defined(__alpha__)
467 /* This function will be called to perform the relocation. */
468 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
469 /* Identify this shared object */
470 obj->pltgot[3] = (Elf_Addr) obj;
471 #endif
472 #if defined(__mips__)
473 _rtld_relocate_mips_got(obj);
474
475 obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
476 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
477 obj->pltgot[1] |= (Elf_Addr) obj;
478 #endif
479 }
480 }
481
482 return 0;
483 }
484