reloc.c revision 1.27 1 /* $NetBSD: reloc.c,v 1.27 2000/07/13 23:14:17 eeh 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 static int _rtld_do_copy_relocation __P((const Obj_Entry *, const Elf_RelA *,
57 bool));
58
59 /*
60 * XXX: These don't work for the alpha and i386; don't know about powerpc
61 * The alpha and the i386 avoid the problem by compiling everything PIC.
62 * These relocation are supposed to be writing the address of the
63 * function to be called on the bss.rel or bss.rela segment, but:
64 * - st_size == 0
65 * - on the i386 at least the call instruction is a direct call
66 * not an indirect call.
67 */
68 static int
69 _rtld_do_copy_relocation(dstobj, rela, dodebug)
70 const Obj_Entry *dstobj;
71 const Elf_RelA *rela;
72 bool dodebug;
73 {
74 void *dstaddr = (void *)(dstobj->relocbase + rela->r_offset);
75 const Elf_Sym *dstsym = dstobj->symtab + ELF_R_SYM(rela->r_info);
76 const char *name = dstobj->strtab + dstsym->st_name;
77 unsigned long hash = _rtld_elf_hash(name);
78 size_t size = dstsym->st_size;
79 const void *srcaddr;
80 const Elf_Sym *srcsym;
81 Obj_Entry *srcobj;
82
83 for (srcobj = dstobj->next; srcobj != NULL; srcobj = srcobj->next)
84 if ((srcsym = _rtld_symlook_obj(name, hash, srcobj,
85 false)) != NULL)
86 break;
87
88 if (srcobj == NULL) {
89 _rtld_error("Undefined symbol \"%s\" referenced from COPY"
90 " relocation in %s", name, dstobj->path);
91 return (-1);
92 }
93 srcaddr = (const void *)(srcobj->relocbase + srcsym->st_value);
94 (void)memcpy(dstaddr, srcaddr, size);
95 rdbg(dodebug, ("COPY %s %s %s --> src=%p dst=%p *dst= %p size %ld",
96 dstobj->path, srcobj->path, name, (void *)srcaddr,
97 (void *)dstaddr, (void *)*(long *)dstaddr, (long)size));
98 return (0);
99 }
100 #endif /* RTLD_INHIBIT_COPY_RELOCS */
101
102
103 /*
104 * Process the special R_xxx_COPY relocations in the main program. These
105 * copy data from a shared object into a region in the main program's BSS
106 * segment.
107 *
108 * Returns 0 on success, -1 on failure.
109 */
110 int
111 _rtld_do_copy_relocations(dstobj, dodebug)
112 const Obj_Entry *dstobj;
113 bool dodebug;
114 {
115 #ifndef RTLD_INHIBIT_COPY_RELOCS
116
117 /* COPY relocations are invalid elsewhere */
118 assert(dstobj->mainprog);
119
120 if (dstobj->rel != NULL) {
121 const Elf_Rel *rel;
122 for (rel = dstobj->rel; rel < dstobj->rellim; ++rel) {
123 if (ELF_R_TYPE(rel->r_info) == R_TYPE(COPY)) {
124 Elf_RelA ourrela;
125 ourrela.r_info = rel->r_info;
126 ourrela.r_offset = rel->r_offset;
127 ourrela.r_addend = 0;
128 if (_rtld_do_copy_relocation(dstobj,
129 &ourrela, dodebug) < 0)
130 return (-1);
131 }
132 }
133 }
134 if (dstobj->rela != NULL) {
135 const Elf_RelA *rela;
136 for (rela = dstobj->rela; rela < dstobj->relalim; ++rela) {
137 if (ELF_R_TYPE(rela->r_info) == R_TYPE(COPY)) {
138 if (_rtld_do_copy_relocation(dstobj, rela,
139 dodebug) < 0)
140 return (-1);
141 }
142 }
143 }
144 #endif /* RTLD_INHIBIT_COPY_RELOCS */
145
146 return (0);
147 }
148
149
150 #ifndef __sparc__
151 int
152 _rtld_relocate_nonplt_object(obj, rela, dodebug)
153 Obj_Entry *obj;
154 const Elf_RelA *rela;
155 bool dodebug;
156 {
157 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
158 const Elf_Sym *def;
159 const Obj_Entry *defobj;
160 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
161 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
162 extern Elf_Dyn _DYNAMIC;
163 #endif
164 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
165 defined(__powerpc__) || defined(__vax__)
166 Elf_Addr tmp;
167 #endif
168
169 switch (ELF_R_TYPE(rela->r_info)) {
170
171 case R_TYPE(NONE):
172 break;
173
174 #if defined(__i386__)
175 case R_TYPE(GOT32):
176
177 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
178 &defobj, false);
179 if (def == NULL)
180 return -1;
181
182 tmp = (Elf_Addr)(defobj->relocbase + def->st_value);
183 if (*where != tmp)
184 *where = tmp;
185 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
186 defobj->strtab + def->st_name, obj->path,
187 (void *)*where, defobj->path));
188 break;
189
190 case R_TYPE(PC32):
191 /*
192 * I don't think the dynamic linker should ever see this
193 * type of relocation. But the binutils-2.6 tools sometimes
194 * generate it.
195 */
196
197 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
198 &defobj, false);
199 if (def == NULL)
200 return -1;
201
202 *where += (Elf_Addr)(defobj->relocbase + def->st_value) -
203 (Elf_Addr)where;
204 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
205 defobj->strtab + def->st_name, obj->path,
206 (void *)*where, defobj->path));
207 break;
208
209 case R_TYPE(32):
210 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
211 &defobj, false);
212 if (def == NULL)
213 return -1;
214
215 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
216 rdbg(dodebug, ("32 %s in %s --> %p in %s",
217 defobj->strtab + def->st_name, obj->path,
218 (void *)*where, defobj->path));
219 break;
220 #endif /* __i386__ */
221
222 #if defined(__m68k__)
223 case R_TYPE(GOT32):
224 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
225 &defobj, false);
226 if (def == NULL)
227 return -1;
228
229 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
230 rela->r_addend);
231 if (*where != tmp)
232 *where = tmp;
233 rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
234 defobj->strtab + def->st_name, obj->path,
235 (void *)*where, defobj->path));
236 break;
237
238 case R_TYPE(PC32):
239 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
240 &defobj, false);
241 if (def == NULL)
242 return -1;
243
244 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
245 rela->r_addend) - (Elf_Addr)where;
246 if (*where != tmp)
247 *where = tmp;
248 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
249 defobj->strtab + def->st_name, obj->path,
250 (void *)*where, defobj->path));
251 break;
252
253 case R_TYPE(32):
254 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
255 &defobj, false);
256 if (def == NULL)
257 return -1;
258
259 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
260 rela->r_addend);
261 if (*where != tmp)
262 *where = tmp;
263 rdbg(dodebug, ("32 %s in %s --> %p in %s",
264 defobj->strtab + def->st_name, obj->path,
265 (void *)*where, defobj->path));
266 break;
267 #endif /* __m68k__ */
268
269 #if defined(__alpha__)
270 case R_TYPE(REFQUAD):
271 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
272 &defobj, false);
273 if (def == NULL)
274 return -1;
275
276 tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
277 *where + rela->r_addend;
278 if (*where != tmp)
279 *where = tmp;
280 rdbg(dodebug, ("REFQUAD %s in %s --> %p in %s",
281 defobj->strtab + def->st_name, obj->path,
282 (void *)*where, defobj->path));
283 break;
284 #endif /* __alpha__ */
285
286 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__)
287 case R_TYPE(GLOB_DAT):
288 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
289 &defobj, false);
290 if (def == NULL)
291 return -1;
292
293 if (*where != (Elf_Addr)(defobj->relocbase + def->st_value))
294 *where = (Elf_Addr)(defobj->relocbase + def->st_value);
295 rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
296 defobj->strtab + def->st_name, obj->path,
297 (void *)*where, defobj->path));
298 break;
299
300 case R_TYPE(RELATIVE):
301 if (!dodebug ||
302 (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
303 (caddr_t)where >= (caddr_t)&_DYNAMIC) {
304 *where += (Elf_Addr)obj->relocbase;
305 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
306 (void *)*where));
307 }
308 else
309 rdbg(dodebug, ("RELATIVE in %s stays at %p",
310 obj->path, (void *)*where));
311 break;
312
313 case R_TYPE(COPY):
314 /*
315 * These are deferred until all other relocations have
316 * been done. All we do here is make sure that the COPY
317 * relocation is not in a shared library. They are allowed
318 * only in executable files.
319 */
320 if (!obj->mainprog) {
321 _rtld_error(
322 "%s: Unexpected R_COPY relocation in shared library",
323 obj->path);
324 return -1;
325 }
326 rdbg(dodebug, ("COPY (avoid in main)"));
327 break;
328 #endif /* __i386__ || __alpha__ */
329
330 #if defined(__mips__)
331 case R_TYPE(REL32):
332 /* 32-bit PC-relative reference */
333 def = obj->symtab + ELF_R_SYM(rela->r_info);
334
335 if (ELFDEFNNAME(ST_BIND)(def->st_info) == STB_LOCAL &&
336 (ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_SECTION ||
337 ELFDEFNNAME(ST_TYPE)(def->st_info) == STT_NOTYPE)) {
338 *where += (Elf_Addr)obj->relocbase;
339 rdbg(dodebug, ("REL32 in %s --> %p", obj->path,
340 (void *)*where));
341 } else {
342 /* XXX maybe do something re: bootstrapping? */
343 def = _rtld_find_symdef(_rtld_objlist, rela->r_info,
344 NULL, obj, &defobj, false);
345 if (def == NULL)
346 return -1;
347 *where += (Elf_Addr)(defobj->relocbase + def->st_value);
348 rdbg(dodebug, ("REL32 %s in %s --> %p in %s",
349 defobj->strtab + def->st_name, obj->path,
350 (void *)*where, defobj->path));
351 }
352 break;
353
354 #endif /* __mips__ */
355
356 #if defined(__powerpc__) || defined(__vax__)
357 case R_TYPE(32): /* word32 S + A */
358 case R_TYPE(GLOB_DAT): /* word32 S + A */
359 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
360 &defobj, false);
361 if (def == NULL)
362 return -1;
363
364 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
365 rela->r_addend);
366
367 if (*where != tmp)
368 *where = tmp;
369 rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
370 defobj->strtab + def->st_name, obj->path,
371 (void *)*where, defobj->path));
372 break;
373
374 case R_TYPE(COPY):
375 rdbg(dodebug, ("COPY"));
376 break;
377
378 case R_TYPE(JMP_SLOT):
379 rdbg(dodebug, ("JMP_SLOT"));
380 break;
381
382 case R_TYPE(RELATIVE): /* word32 B + A */
383 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
384 if (*where != tmp)
385 *where = tmp;
386 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
387 (void *)*where));
388 break;
389 #endif /* __powerpc__ || __vax__ */
390
391 #if defined(__powerpc__)
392 case R_TYPE(16_LO): /* #lo(S + A) */
393 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
394 if (*(Elf32_Half *)where != tmp)
395 *(Elf32_Half *)where = tmp;
396 rdbg(dodebug, ("16_LO in %s --> %p", obj->path,
397 (void *)*where));
398 break;
399
400 case R_TYPE(16_HI): /* #hi(S + A) */
401 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend) >> 16;
402 if (*(Elf32_Half *)where != tmp)
403 *(Elf32_Half *)where = tmp;
404 rdbg(dodebug, ("16_HI in %s --> %p", obj->path,
405 (void *)*where));
406 break;
407
408 case R_TYPE(16_HA): /* #ha(S + A) */
409 tmp = (Elf_Addr)(obj->relocbase + rela->r_addend + 0x8000)
410 >> 16;
411 if (*(Elf32_Half *)where != tmp)
412 *(Elf32_Half *)where = tmp;
413 rdbg(dodebug, ("16_HA in %s --> %p", obj->path,
414 (void *)*where));
415 break;
416 #endif /* __powerpc__ */
417
418 #if defined(__vax__)
419 case R_TYPE(REL32):
420 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
421 &defobj, false);
422 if (def == NULL)
423 return -1;
424
425 *where += (Elf_Addr)(defobj->relocbase + def->st_value
426 + rela->r_addend);
427 rdbg(dodebug, ("32 %s in %s --> %p in %s",
428 defobj->strtab + def->st_name, obj->path,
429 (void *)*where, defobj->path));
430 break;
431 #endif /* __vax__ */
432
433 default:
434 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
435 &defobj, true);
436 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
437 "addend = %p, contents = %p, symbol = %s",
438 (u_long)ELF_R_SYM(rela->r_info),
439 (u_long)ELF_R_TYPE(rela->r_info),
440 (void *)rela->r_offset, (void *)rela->r_addend,
441 (void *)*where,
442 def ? defobj->strtab + def->st_name : "??"));
443 _rtld_error("%s: Unsupported relocation type %d"
444 "in non-PLT relocations\n",
445 obj->path, ELF_R_TYPE(rela->r_info));
446 return -1;
447 }
448 return 0;
449 }
450
451
452
453 int
454 _rtld_relocate_plt_object(obj, rela, addrp, bind_now, dodebug)
455 Obj_Entry *obj;
456 const Elf_RelA *rela;
457 caddr_t *addrp;
458 bool bind_now;
459 bool dodebug;
460 {
461 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
462 Elf_Addr new_value;
463
464 /* Fully resolve procedure addresses now */
465
466 #if defined(__powerpc__)
467 return _rtld_reloc_powerpc_plt(obj, rela, bind_now);
468 #endif
469
470 #if defined(__alpha__) || defined(__i386__) || defined(__m68k__) || \
471 defined(__vax__)
472 if (bind_now || obj->pltgot == NULL) {
473 const Elf_Sym *def;
474 const Obj_Entry *defobj;
475
476 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
477
478 def = _rtld_find_symdef(_rtld_objlist, rela->r_info, NULL, obj,
479 &defobj, true);
480 if (def == NULL)
481 return -1;
482
483 new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
484 rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
485 (int)bind_now,
486 defobj->strtab + def->st_name,
487 (void *)*where, (void *)new_value));
488 } else
489 #endif /* __alpha__ || __i386__ || __m68k__ || __vax__ */
490 if (!obj->mainprog) {
491 /* Just relocate the GOT slots pointing into the PLT */
492 new_value = *where + (Elf_Addr)(obj->relocbase);
493 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
494 (void *)*where));
495 } else {
496 return 0;
497 }
498 /*
499 * Since this page is probably copy-on-write, let's not write
500 * it unless we really really have to.
501 */
502 if (*where != new_value)
503 *where = new_value;
504 if (addrp != NULL)
505 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset);
506 return 0;
507 }
508 #endif /* __sparc__ */
509
510 caddr_t
511 _rtld_bind(obj, reloff)
512 Obj_Entry *obj;
513 Elf_Word reloff;
514 {
515 const Elf_RelA *rela;
516 Elf_RelA ourrela;
517 caddr_t addr;
518
519 if (obj->pltrel != NULL) {
520 const Elf_Rel *rel;
521
522 rel = (const Elf_Rel *)((caddr_t) obj->pltrel + reloff);
523 ourrela.r_info = rel->r_info;
524 ourrela.r_offset = rel->r_offset;
525 rela = &ourrela;
526 } else {
527 rela = (const Elf_RelA *)((caddr_t) obj->pltrela + reloff);
528 }
529
530 if (_rtld_relocate_plt_object(obj, rela, &addr, true, true) < 0)
531 _rtld_die();
532
533 return addr;
534 }
535
536 /*
537 * Relocate newly-loaded shared objects. The argument is a pointer to
538 * the Obj_Entry for the first such object. All objects from the first
539 * to the end of the list of objects are relocated. Returns 0 on success,
540 * or -1 on failure.
541 */
542 int
543 _rtld_relocate_objects(first, bind_now, dodebug)
544 Obj_Entry *first;
545 bool bind_now;
546 bool dodebug;
547 {
548 Obj_Entry *obj;
549 int ok = 1;
550
551 for (obj = first; obj != NULL; obj = obj->next) {
552 if (obj->nbuckets == 0 || obj->nchains == 0 ||
553 obj->buckets == NULL || obj->symtab == NULL ||
554 obj->strtab == NULL) {
555 _rtld_error("%s: Shared object has no run-time"
556 " symbol table", obj->path);
557 return -1;
558 }
559 rdbg(dodebug, (" relocating %s (%ld/%ld rel/rela, "
560 "%ld/%ld plt rel/rela)",
561 obj->path,
562 (long)(obj->rellim - obj->rel),
563 (long)(obj->relalim - obj->rela),
564 (long)(obj->pltrellim - obj->pltrel),
565 (long)(obj->pltrelalim - obj->pltrela)));
566
567 if (obj->textrel) {
568 /*
569 * There are relocations to the write-protected text
570 * segment.
571 */
572 if (mprotect(obj->mapbase, obj->textsize,
573 PROT_READ | PROT_WRITE | PROT_EXEC) == -1) {
574 _rtld_error("%s: Cannot write-enable text "
575 "segment: %s", obj->path, xstrerror(errno));
576 return -1;
577 }
578 }
579 if (obj->rel != NULL) {
580 /* Process the non-PLT relocations. */
581 const Elf_Rel *rel;
582 for (rel = obj->rel; rel < obj->rellim; ++rel) {
583 Elf_RelA ourrela;
584 ourrela.r_info = rel->r_info;
585 ourrela.r_offset = rel->r_offset;
586 #if defined(__mips__)
587 /* rel->r_offset is not valid on mips? */
588 if (ELF_R_TYPE(ourrela.r_info) == R_TYPE(NONE))
589 ourrela.r_addend = 0;
590 else
591 #endif
592 ourrela.r_addend =
593 *(Elf_Word *)(obj->relocbase +
594 rel->r_offset);
595
596 if (_rtld_relocate_nonplt_object(obj, &ourrela,
597 dodebug) < 0)
598 ok = 0;
599 }
600 }
601 if (obj->rela != NULL) {
602 /* Process the non-PLT relocations. */
603 const Elf_RelA *rela;
604 for (rela = obj->rela; rela < obj->relalim; ++rela) {
605 if (_rtld_relocate_nonplt_object(obj, rela,
606 dodebug) < 0)
607 ok = 0;
608 }
609 }
610 if (obj->textrel) { /* Re-protected the text segment. */
611 if (mprotect(obj->mapbase, obj->textsize,
612 PROT_READ | PROT_EXEC) == -1) {
613 _rtld_error("%s: Cannot write-protect text "
614 "segment: %s", obj->path, xstrerror(errno));
615 return -1;
616 }
617 }
618 /* Process the PLT relocations. */
619 if (obj->pltrel != NULL) {
620 const Elf_Rel *rel;
621 for (rel = obj->pltrel; rel < obj->pltrellim; ++rel) {
622 Elf_RelA ourrela;
623 ourrela.r_info = rel->r_info;
624 ourrela.r_offset = rel->r_offset;
625 ourrela.r_addend =
626 *(Elf_Word *)(obj->relocbase +
627 rel->r_offset);
628 if (_rtld_relocate_plt_object(obj, &ourrela,
629 NULL, bind_now, dodebug) < 0)
630 ok = 0;
631 }
632 }
633 if (obj->pltrela != NULL) {
634 const Elf_RelA *rela;
635 for (rela = obj->pltrela; rela < obj->pltrelalim;
636 ++rela) {
637 if (_rtld_relocate_plt_object(obj, rela,
638 NULL, bind_now, dodebug) < 0)
639 ok = 0;
640 }
641 }
642 if (!ok)
643 return -1;
644
645
646 /* Set some sanity-checking numbers in the Obj_Entry. */
647 obj->magic = RTLD_MAGIC;
648 obj->version = RTLD_VERSION;
649
650 /* Fill in the dynamic linker entry points. */
651 obj->dlopen = _rtld_dlopen;
652 obj->dlsym = _rtld_dlsym;
653 obj->dlerror = _rtld_dlerror;
654 obj->dlclose = _rtld_dlclose;
655 obj->dladdr = _rtld_dladdr;
656
657 /* Set the special PLTGOT entries. */
658 if (obj->pltgot != NULL) {
659 #if defined(__i386__) || defined(__m68k__)
660 obj->pltgot[1] = (Elf_Addr) obj;
661 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
662 #endif
663 #if defined(__alpha__)
664 /*
665 * This function will be called to perform the
666 * relocation.
667 */
668 obj->pltgot[2] = (Elf_Addr) & _rtld_bind_start;
669 /* Identify this shared object */
670 obj->pltgot[3] = (Elf_Addr) obj;
671 #endif
672 #if defined(__mips__)
673 _rtld_relocate_mips_got(obj);
674
675 obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
676 /* XXX only if obj->pltgot[1] & 0x80000000 ?? */
677 obj->pltgot[1] |= (Elf_Addr) obj;
678 #endif
679 #if defined(__powerpc__)
680 _rtld_setup_powerpc_plt(obj);
681 #endif
682 #if defined(__sparc__)
683 #if defined(__arch64__)
684 /*
685 * On sparc64 we got troubles.
686 *
687 * Instructions are 4 bytes long.
688 * Elf[64]_Addr is 8 bytes long, so are our pltglot[]
689 * array entries.
690 * Each PLT entry jumps to PLT0 to enter the dynamic
691 * linker.
692 * Loading an arbitrary 64-bit pointer takes 6
693 * instructions and 2 registers.
694 *
695 * Somehow we need to issue a save to get a new stack
696 * frame, load the address of the dynamic linker, and
697 * jump there, in 8 instructions or less.
698 *
699 * Oh, we need to fill out both PLT0 and PLT1.
700 */
701 {
702 Elf32_Word *entry = (Elf32_Word *)obj->pltgot;
703 extern void _rtld_install_plt __P((Elf32_Word *,
704 Elf_Addr));
705 extern void _rtld_bind_start_0 __P((long,
706 long));
707 extern void _rtld_bind_start_1 __P((long,
708 long));
709
710 /* Install in entries 0 and 1 */
711 _rtld_install_plt(&entry[0],
712 (Elf_Addr)&_rtld_bind_start_0);
713 _rtld_install_plt(&entry[8],
714 (Elf_Addr)&_rtld_bind_start_1);
715
716 /*
717 * Install the object reference in first slot
718 * of entry 2.
719 */
720 obj->pltgot[12] = (Elf_Addr) obj;
721 }
722 #else
723 /*
724 * PLTGOT is the PLT on the sparc.
725 * The first entry holds the call the dynamic linker.
726 * We construct a `call' sequence that transfers
727 * to `_rtld_bind_start()'.
728 * The second entry holds the object identification.
729 * Note: each PLT entry is three words long.
730 */
731 #define SAVE 0x9de3bfc0 /* i.e. `save %sp,-64,%sp' */
732 #define CALL 0x40000000
733 #define NOP 0x01000000
734 obj->pltgot[0] = SAVE;
735 obj->pltgot[1] = CALL |
736 ((Elf_Addr)&_rtld_bind_start -
737 (Elf_Addr)&obj->pltgot[1]) >> 2;
738 obj->pltgot[2] = NOP;
739
740 obj->pltgot[3] = (Elf_Addr) obj;
741 #endif
742 #endif
743 #if defined(__vax__)
744 obj->pltgot[0] = (Elf_Addr) & _rtld_bind_start;
745 obj->pltgot[1] = (Elf_Addr) obj;
746 #endif
747 }
748 }
749
750 return 0;
751 }
752