1 /* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Kazumoto Kojima <kkojima@info.kanagawa-u.ac.jp>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #define ELF_MACHINE_NAME "MIPS"
30 #error ENTRY_POINT needs to be defined for MIPS.
33 #ifndef _RTLD_PROLOGUE
35 #define _RTLD_PROLOGUE(entry) "\n\t.globl " #entry \
39 #define _RTLD_PROLOGUE(entry) "\n\t.globl entry\n\t.ent entry\n\t entry:\n\t"
43 #ifndef _RTLD_EPILOGUE
45 #define _RTLD_EPILOGUE(entry) "\t.end " #entry "\n"
47 #define _RTLD_EPILOGUE(entry) "\t.end entry\n"
51 /* I have no idea what I am doing. */
52 #define ELF_MACHINE_RELOC_NOPLT -1
53 #define elf_machine_lookup_noplt_p(type) (1)
54 #define elf_machine_lookup_noexec_p(type) (0)
56 /* Translate a processor specific dynamic tag to the index
58 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
61 /* We may need 64k alignment. */
62 #define ELF_MACHINE_ALIGN_MASK 0xffff
66 * MIPS libraries are usually linked to a non-zero base address. We
67 * subtrace the base address from the address where we map the object
68 * to. This results in more efficient address space usage.
71 #define MAP_BASE_ADDR(l) ((l)->l_info[DT_MIPS(BASE_ADDRESS)] ? \
72 (l)->l_info[DT_MIPS(BASE_ADDRESS)]->d_un.d_ptr : 0)
74 #define MAP_BASE_ADDR(l) 0x5ffe0000
77 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
78 with the run-time address of the r_debug structure */
79 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
80 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
81 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
85 /* Return nonzero iff E_MACHINE is compatible with the running host. */
86 static inline int __attribute__ ((unused
))
87 elf_machine_matches_host (ElfW(Half
) e_machine
)
99 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
100 + first element of the GOT. This must be inlined in a function which
101 + uses global data. */
102 +static inline ElfW(Addr
)
103 +elf_machine_dynamic (void)
105 + register ElfW(Addr
) gp
asm ("$28");
106 + return * (ElfW(Addr
) *) (gp
- 0x7ff0);
109 static inline ElfW(Addr
) *
110 elf_mips_got_from_gpreg (ElfW(Addr
) gpreg
)
112 /* FIXME: the offset of gp from GOT may be system-dependent. */
113 return (ElfW(Addr
) *) (gpreg
- 0x7ff0);
116 /* Return the run-time address of the _GLOBAL_OFFSET_TABLE_.
117 Must be inlined in a function which uses global data. */
118 static inline ElfW(Addr
) *
119 elf_machine_got (void)
123 __asm__
__volatile__("move %0, $28\n\t" : "=r" (gp
));
124 return elf_mips_got_from_gpreg (gp
);
128 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
129 first element of the GOT. This must be inlined in a function which
131 static inline ElfW(Addr
)
132 elf_machine_dynamic (void)
134 register ElfW(Addr
) gp
asm ("$28");
135 return * (ElfW(Addr
) *) (gp
- 0x7ff0);
138 /* Return the run-time load address of the shared object. */
139 static inline ElfW(Addr
)
140 elf_machine_load_address (void)
144 asm (" .set noreorder\n"
148 "here: dsubu %0, $31, %0\n"
154 asm (" .set noreorder\n"
158 "here: subu %0, $31, %0\n"
167 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
168 #define ELF_MIPS_GNU_GOT1_MASK 0x80000000
172 elf_machine_got_rel (struct link_map
*map
, int lazy
)
177 struct link_map
**scope
;
179 = ((void *) map
->l_addr
+ map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
181 #define RESOLVE_GOTSYM(sym) \
183 const ElfW(Sym) *ref = sym; \
184 ElfW(Addr) sym_loadaddr; \
185 sym_loadaddr = _dl_lookup_symbol (strtab + sym->st_name, &ref, scope, \
186 map->l_name, ELF_MACHINE_RELOC_NOPLT);\
187 (ref)? sym_loadaddr + ref->st_value: 0; \
190 got
= (ElfW(Addr
) *) ((void *) map
->l_addr
191 + map
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
193 /* got[0] is reserved. got[1] is also reserved for the dynamic object
194 generated by gnu ld. Skip these reserved entries from relocation. */
195 i
= (got
[1] & ELF_MIPS_GNU_GOT1_MASK
)? 2: 1;
196 n
= map
->l_info
[DT_MIPS (LOCAL_GOTNO
)]->d_un
.d_val
;
197 /* Add the run-time display to all local got entries. */
199 got
[i
++] += map
->l_addr
;
202 scope
= _dl_object_relocation_scope (map
);
204 /* Handle global got entries. */
206 sym
= (ElfW(Sym
) *) ((void *) map
->l_addr
207 + map
->l_info
[DT_SYMTAB
]->d_un
.d_ptr
);
208 sym
+= map
->l_info
[DT_MIPS (GOTSYM
)]->d_un
.d_val
;
209 i
= (map
->l_info
[DT_MIPS (SYMTABNO
)]->d_un
.d_val
210 - map
->l_info
[DT_MIPS (GOTSYM
)]->d_un
.d_val
);
214 if (sym
->st_shndx
== SHN_UNDEF
)
216 if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_FUNC
)
218 if (sym
->st_value
&& lazy
)
219 *got
= sym
->st_value
+ map
->l_addr
;
221 *got
= RESOLVE_GOTSYM (sym
);
223 else /* if (*got == 0 || *got == QS) */
224 *got
= RESOLVE_GOTSYM (sym
);
226 else if (sym
->st_shndx
== SHN_COMMON
)
227 *got
= RESOLVE_GOTSYM (sym
);
228 else if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_FUNC
229 && *got
!= sym
->st_value
232 else if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_SECTION
)
234 if (sym
->st_other
== 0)
238 *got
= RESOLVE_GOTSYM (sym
);
244 #undef RESOLVE_GOTSYM
245 *_dl_global_scope_end
= NULL
;
250 /* Set up the loaded object described by L so its stub function
251 will jump to the on-demand fixup code in dl-runtime.c. */
254 elf_machine_runtime_setup (struct link_map
*l
, int lazy
)
257 extern void _dl_runtime_resolve (ElfW(Word
));
258 extern int _dl_mips_gnu_objects
;
260 #ifdef RTLD_BOOTSTRAP
267 /* The GOT entries for functions have not yet been filled in.
268 Their initial contents will arrange when called to put an
269 offset into the .dynsym section in t8, the return address
270 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
271 got
= (ElfW(Addr
) *) ((void *) l
->l_addr
272 + l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
274 /* This function will get called to fix up the GOT entry indicated by
275 the register t8, and then jump to the resolved address. */
276 got
[0] = (ElfW(Addr
)) &_dl_runtime_resolve
;
278 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
279 of got[1] of a gnu object is set to identify gnu objects.
280 Where we can store l for non gnu objects? XXX */
281 if ((got
[1] & ELF_MIPS_GNU_GOT1_MASK
) != 0)
282 got
[1] = (ElfW(Addr
)) ((unsigned) l
| ELF_MIPS_GNU_GOT1_MASK
);
284 _dl_mips_gnu_objects
= 0;
287 /* Relocate global offset table. */
288 elf_machine_got_rel (l
, lazy
);
293 /* Get link_map for this object. */
294 static inline struct link_map
*
295 elf_machine_runtime_link_map (ElfW(Addr
) gpreg
, ElfW(Addr
) stub_pc
)
297 extern int _dl_mips_gnu_objects
;
299 /* got[1] is reserved to keep its link map address for the shared
300 object generated by gnu linker. If all are such object, we can
301 find link map from current GPREG simply. If not so, get link map
302 for callers object containing STUB_PC. */
304 if (_dl_mips_gnu_objects
)
306 ElfW(Addr
) *got
= elf_mips_got_from_gpreg (gpreg
);
309 g1
= ((ElfW(Word
) *) got
)[1];
311 if ((g1
& ELF_MIPS_GNU_GOT1_MASK
) != 0)
312 return (struct link_map
*) (g1
& ~ELF_MIPS_GNU_GOT1_MASK
);
316 struct link_map
*l
= _dl_loaded
;
317 struct link_map
*ret
= 0;
318 ElfW(Addr
) candidate
= 0;
323 const ElfW(Phdr
) *p
= l
->l_phdr
;
324 ElfW(Half
) this, nent
= l
->l_phnum
;
327 for (this = 0; this < nent
; this++)
328 if (p
[this].p_type
== PT_LOAD
)
330 base
= p
[this].p_vaddr
+ l
->l_addr
;
339 /* Find closest link base addr. */
340 if ((base
< stub_pc
) && (candidate
< base
))
347 if (candidate
&& ret
&& (candidate
< stub_pc
))
353 _dl_signal_error (0, NULL
, "cannot find runtime link map");
357 /* Mips has no PLT but define elf_machine_relplt to be elf_machine_rel. */
358 #define elf_machine_relplt elf_machine_rel
360 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
361 is called from assembler function _dl_runtime_resolve which converts
362 special argument registers t7 ($15) and t8 ($24):
363 t7 address to return to the caller of the function
364 t8 index for this function symbol in .dynsym
365 to usual c arguments. */
368 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
369 /* The flag _dl_mips_gnu_objects is set if all dynamic objects are \
370 generated by the gnu linker. */ \
371 int _dl_mips_gnu_objects = 1; \
373 /* This is called from assembly stubs below which the compiler can't see. */ \
375 __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr)) \
376 __attribute__ ((unused)); \
379 __dl_runtime_resolve (ElfW(Word) sym_index, \
380 ElfW(Word) return_address, \
381 ElfW(Addr) old_gpreg, \
382 ElfW(Addr) stub_pc) \
384 struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc); \
385 const ElfW(Sym) *const symtab \
386 = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr); \
388 = (void *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr); \
389 const ElfW(Addr) *got \
390 = (const ElfW(Addr) *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr); \
391 const ElfW(Word) local_gotno \
392 = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; \
393 const ElfW(Word) gotsym \
394 = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val; \
395 const ElfW(Sym) *definer; \
396 ElfW(Addr) loadbase; \
397 ElfW(Addr) funcaddr; \
398 struct link_map **scope; \
400 /* Look up the symbol's run-time value. */ \
401 scope = _dl_object_relocation_scope (l); \
402 definer = &symtab[sym_index]; \
404 loadbase = _dl_lookup_symbol (strtab + definer->st_name, &definer, \
405 scope, l->l_name, ELF_MACHINE_RELOC_NOPLT); \
407 *_dl_global_scope_end = NULL; \
409 /* Apply the relocation with that value. */ \
410 funcaddr = loadbase + definer->st_value; \
411 *(got + local_gotno + sym_index - gotsym) = funcaddr; \
419 .globl _dl_runtime_resolve\n \
420 .type _dl_runtime_resolve,@function\n \
421 .ent _dl_runtime_resolve\n \
422 _dl_runtime_resolve:\n \
424 # Save old GP to $3.\n \
426 # Modify t9 ($25) so as to point .cpload instruction.\n \
431 # Save slot call pc.\n \
433 # Save arguments and sp value in stack.\n \
447 jal __dl_runtime_resolve\n \
458 .end _dl_runtime_resolve\n \
461 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
462 /* The flag _dl_mips_gnu_objects is set if all dynamic objects are \
463 generated by the gnu linker. */ \
464 int _dl_mips_gnu_objects = 1; \
466 /* This is called from assembly stubs below which the compiler can't see. */ \
468 __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr)) \
469 __attribute__ ((unused)); \
472 __dl_runtime_resolve (ElfW(Word) sym_index, \
473 ElfW(Word) return_address, \
474 ElfW(Addr) old_gpreg, \
475 ElfW(Addr) stub_pc) \
477 struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc); \
478 const ElfW(Sym) *const symtab \
479 = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr); \
481 = (void *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr); \
482 const ElfW(Addr) *got \
483 = (const ElfW(Addr) *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr); \
484 const ElfW(Word) local_gotno \
485 = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; \
486 const ElfW(Word) gotsym \
487 = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val; \
488 const ElfW(Sym) *definer; \
489 ElfW(Addr) loadbase; \
490 ElfW(Addr) funcaddr; \
491 struct link_map **scope; \
493 /* Look up the symbol's run-time value. */ \
494 scope = _dl_object_relocation_scope (l); \
495 definer = &symtab[sym_index]; \
497 loadbase = _dl_lookup_symbol (strtab + definer->st_name, &definer, \
498 scope, l->l_name, ELF_MACHINE_RELOC_NOPLT); \
500 *_dl_global_scope_end = NULL; \
502 /* Apply the relocation with that value. */ \
503 funcaddr = loadbase + definer->st_value; \
504 *(got + local_gotno + sym_index - gotsym) = funcaddr; \
512 .globl _dl_runtime_resolve\n \
513 .type _dl_runtime_resolve,@function\n \
514 .ent _dl_runtime_resolve\n \
515 _dl_runtime_resolve:\n \
517 # Save slot call pc.\n \
519 # Modify t9 ($25) so as to point .cpload instruction.\n \
524 # Save slot call pc.\n \
526 # Save arguments and sp value in stack.\n \
540 jal __dl_runtime_resolve\n \
551 .end _dl_runtime_resolve\n \
555 /* Mask identifying addresses reserved for the user program,
556 where the dynamic linker should not map anything. */
557 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
561 /* Initial entry point code for the dynamic linker.
562 The C function `_dl_start' is the real entry point;
563 its return value is the user program's entry point.
564 Note how we have to be careful about two things:
566 1) That we allocate a minimal stack of 24 bytes for
567 every function call, the MIPS ABI states that even
568 if all arguments are passed in registers the procedure
569 called can use the 16 byte area pointed to by $sp
570 when it is called to store away the arguments passed
573 2) That under Linux the entry is named __start
574 and not just plain _start. */
577 #define RTLD_START asm ("\
580 _RTLD_PROLOGUE (ENTRY_POINT)\
581 " .globl _dl_start_user\n\
587 # i386 ABI book says that the first entry of GOT holds\n\
588 # the address of the dynamic structure. Though MIPS ABI\n\
589 # doesn't say nothing about this, I emulate this here.\n\
591 sd $4, -0x7ff0($28)\n\
594 # Get the value of label '_dl_start_user' in t9 ($25).\n\
595 dla $25, _dl_start_user\n\
601 # Save the user entry point address in saved register.\n\
603 # See if we were run as a command with the executable file\n\
604 # name as an extra leading argument.\n\
605 ld $2, _dl_skip_args\n\
607 # Load the original argument count.\n\
609 # Subtract _dl_skip_args from it.\n\
611 # Adjust the stack pointer to skip _dl_skip_args words.\n\
614 # Save back the modified argument count.\n\
616 # Get _dl_default_scope[2] as argument in _dl_init_next call below.\n\
617 1: dla $2, _dl_default_scope\n\
619 # Call _dl_init_next to return the address of an initializer\n\
620 # function to run.\n\
623 # Check for zero return, when out of initializers.\n\
625 # Call the shared object initializer function.\n\
633 # Loop to call _dl_init_next for the next initializer.\n\
635 # Pass our finalizer function to the user in ra.\n\
636 2: dla $31, _dl_fini\n\
637 # Jump to the user entry point.\n\
644 _RTLD_EPILOGUE(ENTRY_POINT) \
648 #define RTLD_START asm ("\
650 _RTLD_PROLOGUE(ENTRY_POINT)\
651 " .globl _dl_start_user\n\
657 # i386 ABI book says that the first entry of GOT holds\n\
658 # the address of the dynamic structure. Though MIPS ABI\n\
659 # doesn't say nothing about this, I emulate this here.\n\
661 sw $4, -0x7ff0($28)\n\
666 # Get the value of label '_dl_start_user' in t9 ($25).\n\
667 la $25, _dl_start_user\n\
673 # Save the user entry point address in saved register.\n\
675 # See if we were run as a command with the executable file\n\
676 # name as an extra leading argument.\n\
677 lw $2, _dl_skip_args\n\
679 # Load the original argument count.\n\
681 # Subtract _dl_skip_args from it.\n\
683 # Adjust the stack pointer to skip _dl_skip_args words.\n\
686 # Save back the modified argument count.\n\
688 # Get _dl_default_scope[2] as argument in _dl_init_next call below.\n\
689 1: la $2, _dl_default_scope\n\
691 # Call _dl_init_next to return the address of an initializer\n\
692 # function to run.\n\
697 # Check for zero return, when out of initializers.\n\
699 # Call the shared object initializer function.\n\
707 # Loop to call _dl_init_next for the next initializer.\n\
709 2: # Clear the startup flag. Assumes 32 bit ints.\n\
710 sw $0, _dl_starting_up\n\
711 # Pass our finalizer function to the user in ra.\n\
713 # Jump to the user entry point.\n\
720 _RTLD_EPILOGUE(ENTRY_POINT)\
724 /* The MIPS never uses Elfxx_Rela relocations. */
725 #define ELF_MACHINE_NO_RELA 1
727 #endif /* !dl_machine_h */
731 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
732 MAP is the object containing the reloc. */
735 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
736 const ElfW(Sym) *sym, const struct r_found_version *version)
738 ElfW(Addr) *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
740 ElfW(Addr) undo __attribute__ ((unused));
742 switch (ELFW(R_TYPE) (reloc->r_info))
748 if (ELFW(ST_BIND) (sym->st_info) == STB_LOCAL
749 && (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION
750 || ELFW(ST_TYPE) (sym->st_info) == STT_NOTYPE))
752 *reloc_addr += map->l_addr;
755 #ifndef RTLD_BOOTSTRAP
756 /* This is defined in rtld.c, but nowhere in the static libc.a;
757 make the reference weak so static programs can still link. This
758 declaration cannot be done when compiling rtld.c (i.e. #ifdef
759 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
760 _dl_rtld_map, which is incompatible with a weak decl in the same
762 weak_extern (_dl_rtld_map);
763 if (map == &_dl_rtld_map)
764 /* Undo the relocation done here during bootstrapping. Now we will
765 relocate it anew, possibly using a binding found in the user
766 program or a loaded library rather than the dynamic linker's
767 built-in definitions used while loading those libraries. */
768 undo = map->l_addr + sym->st_value;
770 loadbase = RESOLVE (&sym, version, 0);
771 *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
774 case R_MIPS_NONE: /* Alright, Wilbur. */
777 assert (! "unexpected dynamic reloc type");
783 elf_machine_lazy_rel (struct link_map *map, const ElfW(Rel) *reloc)