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