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