1 /* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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. */
21 /* FIXME: Profiling of shared libraries is not implemented yet. */
25 #define ELF_MACHINE_NAME "MIPS"
27 #define ELF_MACHINE_NO_PLT
32 #error ENTRY_POINT needs to be defined for MIPS.
35 /* The offset of gp from GOT might be system-dependent. It's set by
36 ld. The same value is also */
37 #define OFFSET_GP_GOT 0x7ff0
39 #ifndef _RTLD_PROLOGUE
40 # define _RTLD_PROLOGUE(entry) \
41 ".globl\t" __STRING(entry) "\n\t" \
42 ".ent\t" __STRING(entry) "\n\t" \
43 ".type\t" __STRING(entry) ", @function\n" \
44 __STRING(entry) ":\n\t"
47 #ifndef _RTLD_EPILOGUE
48 # define _RTLD_EPILOGUE(entry) \
49 ".end\t" __STRING(entry) "\n\t" \
50 ".size\t" __STRING(entry) ", . - " __STRING(entry) "\n\t"
53 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.
54 This makes no sense on MIPS but we have to define this to R_MIPS_REL32
55 to avoid the asserts in dl-lookup.c from blowing. */
56 #define ELF_MACHINE_JMP_SLOT R_MIPS_REL32
57 #define elf_machine_lookup_noplt_p(type) (1)
58 #define elf_machine_lookup_noexec_p(type) (0)
60 /* Translate a processor specific dynamic tag to the index
62 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
65 * MIPS libraries are usually linked to a non-zero base address. We
66 * subtract the base address from the address where we map the object
67 * to. This results in more efficient address space usage.
69 * FIXME: By the time when MAP_BASE_ADDR is called we don't have the
70 * DYNAMIC section read. Until this is fixed make the assumption that
71 * libraries have their base address at 0x5ffe0000. This needs to be
72 * fixed before we can safely get rid of this MIPSism.
75 #define MAP_BASE_ADDR(l) ((l)->l_info[DT_MIPS(BASE_ADDRESS)] ? \
76 (l)->l_info[DT_MIPS(BASE_ADDRESS)]->d_un.d_ptr : 0)
78 #define MAP_BASE_ADDR(l) 0x5ffe0000
81 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
82 with the run-time address of the r_debug structure */
83 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
84 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
85 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
89 /* Return nonzero iff E_MACHINE is compatible with the running host. */
90 static inline int __attribute__ ((unused))
91 elf_machine_matches_host (ElfW(Half) e_machine)
103 static inline ElfW(Addr) *
104 elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
106 /* FIXME: the offset of gp from GOT may be system-dependent. */
107 return (ElfW(Addr) *) (gpreg - OFFSET_GP_GOT);
110 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
111 first element of the GOT. This must be inlined in a function which
113 static inline ElfW(Addr)
114 elf_machine_dynamic (void)
116 register ElfW(Addr) gp __asm__ ("$28");
117 return *elf_mips_got_from_gpreg (gp);
121 /* Return the run-time load address of the shared object. */
122 static inline ElfW(Addr)
123 elf_machine_load_address (void)
126 asm (" .set noreorder\n"
130 "here: subu %0, $31, %0\n"
138 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
139 #define ELF_MIPS_GNU_GOT1_MASK 0x80000000
141 /* We can't rely on elf_machine_got_rel because _dl_object_relocation_scope
142 fiddles with global data. */
143 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
145 struct link_map *map = &bootstrap_map; \
150 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]); \
153 if (__builtin_expect (map->l_addr == 0, 1)) \
156 /* got[0] is reserved. got[1] is also reserved for the dynamic object \
157 generated by gnu ld. Skip these reserved entries from \
159 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1; \
160 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; \
162 /* Add the run-time display to all local got entries. */ \
164 got[i++] += map->l_addr; \
166 /* Handle global got entries. */ \
168 sym = (ElfW(Sym) *) D_PTR(map, l_info[DT_SYMTAB]) \
169 + map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val; \
170 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val \
171 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val); \
175 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx == SHN_COMMON) \
176 *got = map->l_addr + sym->st_value; \
177 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC \
178 && *got != sym->st_value) \
179 *got += map->l_addr; \
180 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION) \
182 if (sym->st_other == 0) \
183 *got += map->l_addr; \
186 *got = map->l_addr + sym->st_value; \
195 /* Get link map for callers object containing STUB_PC. */
196 static inline struct link_map *
197 elf_machine_runtime_link_map (ElfW(Addr) gpreg, ElfW(Addr) stub_pc)
199 extern int _dl_mips_gnu_objects;
201 /* got[1] is reserved to keep its link map address for the shared
202 object generated by the gnu linker. If all are such objects, we
203 can find the link map from current GPREG simply. If not so, get
204 the link map for caller's object containing STUB_PC. */
206 if (_dl_mips_gnu_objects)
208 ElfW(Addr) *got = elf_mips_got_from_gpreg (gpreg);
211 g1 = ((ElfW(Word) *) got)[1];
213 if ((g1 & ELF_MIPS_GNU_GOT1_MASK) != 0)
216 (struct link_map *) (g1 & ~ELF_MIPS_GNU_GOT1_MASK);
217 ElfW(Addr) base, limit;
218 const ElfW(Phdr) *p = l->l_phdr;
219 ElfW(Half) this, nent = l->l_phnum;
221 /* For the common case of a stub being called from the containing
222 object, STUB_PC will point to somewhere within the object that
223 is described by the link map fetched via got[1]. Otherwise we
224 have to scan all maps. */
225 for (this = 0; this < nent; this++)
227 if (p[this].p_type == PT_LOAD)
229 base = p[this].p_vaddr + l->l_addr;
230 limit = base + p[this].p_memsz;
231 if (stub_pc >= base && stub_pc < limit)
239 struct link_map *l = _dl_loaded;
243 ElfW(Addr) base, limit;
244 const ElfW(Phdr) *p = l->l_phdr;
245 ElfW(Half) this, nent = l->l_phnum;
247 for (this = 0; this < nent; ++this)
249 if (p[this].p_type == PT_LOAD)
251 base = p[this].p_vaddr + l->l_addr;
252 limit = base + p[this].p_memsz;
253 if (stub_pc >= base && stub_pc < limit)
261 _dl_signal_error (0, NULL, "cannot find runtime link map");
265 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
266 is called from assembler function _dl_runtime_resolve which converts
267 special argument registers t7 ($15) and t8 ($24):
268 t7 address to return to the caller of the function
269 t8 index for this function symbol in .dynsym
270 to usual c arguments.
272 Other architectures call fixup from dl-runtime.c in
273 _dl_runtime_resolve. MIPS instead calls __dl_runtime_resolve. We
274 have to use our own version because of the way the got section is
275 treated on MIPS (we've also got ELF_MACHINE_PLT defined). */
277 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
278 /* The flag _dl_mips_gnu_objects is set if all dynamic objects are \
279 generated by the gnu linker. */ \
280 int _dl_mips_gnu_objects = 1; \
282 /* This is called from assembly stubs below which the compiler can't see. */ \
284 __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr)) \
285 __attribute__ ((unused)); \
288 __dl_runtime_resolve (ElfW(Word) sym_index, \
289 ElfW(Word) return_address, \
290 ElfW(Addr) old_gpreg, \
291 ElfW(Addr) stub_pc) \
293 struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc); \
294 const ElfW(Sym) *const symtab \
295 = (const void *) D_PTR (l, l_info[DT_SYMTAB]); \
297 = (const void *) D_PTR (l, l_info[DT_STRTAB]); \
298 const ElfW(Addr) *got \
299 = (const ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]); \
300 const ElfW(Word) local_gotno \
301 = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; \
302 const ElfW(Word) gotsym \
303 = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val; \
304 const ElfW(Sym) *sym = &symtab[sym_index]; \
307 /* FIXME: The symbol versioning stuff is not tested yet. */ \
308 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0) \
310 switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL) \
314 const ElfW(Half) *vernum = \
315 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]); \
316 ElfW(Half) ndx = vernum[sym_index]; \
317 const struct r_found_version *version = &l->l_versions[ndx]; \
319 if (version->hash != 0) \
321 value = _dl_lookup_versioned_symbol(strtab + sym->st_name, l, \
322 &sym, l->l_scope, version,\
326 /* Fall through. */ \
329 value = _dl_lookup_symbol (strtab + sym->st_name, l, &sym, \
330 l->l_scope, R_MIPS_REL32, 0); \
333 /* Currently value contains the base load address of the object \
334 that defines sym. Now add in the symbol offset. */ \
335 value = (sym ? value + sym->st_value : 0); \
338 /* We already found the symbol. The module (and therefore its load \
339 address) is also known. */ \
340 value = l->l_addr + sym->st_value; \
342 /* Apply the relocation with that value. */ \
343 *(got + local_gotno + sym_index - gotsym) = value; \
351 .globl _dl_runtime_resolve\n \
352 .type _dl_runtime_resolve,@function\n \
353 .ent _dl_runtime_resolve\n \
354 _dl_runtime_resolve:\n \
358 # Modify t9 ($25) so as to point .cpload instruction.\n \
363 # Save slot call pc.\n \
365 # Save arguments and sp value in stack.\n \
379 jal __dl_runtime_resolve\n \
390 .end _dl_runtime_resolve\n \
394 /* Mask identifying addresses reserved for the user program,
395 where the dynamic linker should not map anything. */
396 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
400 /* Initial entry point code for the dynamic linker.
401 The C function `_dl_start' is the real entry point;
402 its return value is the user program's entry point.
403 Note how we have to be careful about two things:
405 1) That we allocate a minimal stack of 24 bytes for
406 every function call, the MIPS ABI states that even
407 if all arguments are passed in registers the procedure
408 called can use the 16 byte area pointed to by $sp
409 when it is called to store away the arguments passed
412 2) That under Linux the entry is named __start
413 and not just plain _start. */
415 #define RTLD_START asm (\
417 _RTLD_PROLOGUE(ENTRY_POINT)\
424 # i386 ABI book says that the first entry of GOT holds\n\
425 # the address of the dynamic structure. Though MIPS ABI\n\
426 # doesn't say nothing about this, I emulate this here.\n\
428 # Subtract OFFSET_GP_GOT\n\
429 sw $4, -0x7ff0($28)\n\
435 coff: subu $8, $31, $8\n\
442 # Get the value of label '_dl_start_user' in t9 ($25).\n\
443 la $25, _dl_start_user\n\
444 .globl _dl_start_user\n\
450 # Save the user entry point address in a saved register.\n\
452 # Store the highest stack address\n\
453 sw $29, __libc_stack_end\n\
454 # See if we were run as a command with the executable file\n\
455 # name as an extra leading argument.\n\
456 lw $2, _dl_skip_args\n\
458 # Load the original argument count.\n\
460 # Subtract _dl_skip_args from it.\n\
462 # Adjust the stack pointer to skip _dl_skip_args words.\n\
465 # Save back the modified argument count.\n\
467 1: # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
475 # Call the function to run the initializers.\n\
478 # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
480 # Jump to the user entry point.\n\
483 _RTLD_EPILOGUE(ENTRY_POINT)\
487 /* The MIPS never uses Elfxx_Rela relocations. */
488 #define ELF_MACHINE_NO_RELA 1
490 #endif /* !dl_machine_h */
494 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
495 MAP is the object containing the reloc. */
498 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
499 const ElfW(Sym) *sym, const struct r_found_version *version,
500 ElfW(Addr) *const reloc_addr)
502 #ifndef RTLD_BOOTSTRAP
503 /* This is defined in rtld.c, but nowhere in the static libc.a;
504 make the reference weak so static programs can still link. This
505 declaration cannot be done when compiling rtld.c (i.e. #ifdef
506 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
507 _dl_rtld_map, which is incompatible with a weak decl in the same
509 weak_extern (_dl_rtld_map);
512 switch (ELFW(R_TYPE) (reloc->r_info))
515 #ifndef RTLD_BOOTSTRAP
516 if (map != &_dl_rtld_map)
518 *reloc_addr += map->l_addr;
520 case R_MIPS_NONE: /* Alright, Wilbur. */
523 _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 0);
529 elf_machine_lazy_rel (struct link_map *map,
530 ElfW(Addr) l_addr, const ElfW(Rel) *reloc)
537 elf_machine_got_rel (struct link_map *map, int lazy)
542 /* This function is loaded in dl-reloc as a nested function and can
543 therefore access the variables scope and strtab from
544 _dl_relocate_object. */
545 #ifdef RTLD_BOOTSTRAP
546 # define RESOLVE_GOTSYM(sym,sym_index) 0
548 # define RESOLVE_GOTSYM(sym,sym_index) \
550 const ElfW(Sym) *ref = sym; \
553 switch (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL) \
557 const ElfW(Half) *vernum = \
558 (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]); \
559 ElfW(Half) ndx = vernum[sym_index]; \
560 const struct r_found_version *version = &l->l_versions[ndx]; \
562 if (version->hash != 0) \
564 value = _dl_lookup_versioned_symbol(strtab + sym->st_name,\
566 &ref, scope, version, \
570 /* Fall through. */ \
573 value = _dl_lookup_symbol (strtab + sym->st_name, map, &ref, \
574 scope, R_MIPS_REL32, 0); \
577 (ref)? value + ref->st_value: 0; \
579 #endif /* RTLD_BOOTSTRAP */
581 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
583 /* got[0] is reserved. got[1] is also reserved for the dynamic object
584 generated by gnu ld. Skip these reserved entries from relocation. */
585 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1;
586 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
587 /* Add the run-time display to all local got entries if needed. */
588 if (__builtin_expect (map->l_addr != 0, 0))
591 got[i++] += map->l_addr;
594 /* Handle global got entries. */
596 /* Keep track of the symbol index. */
597 symidx = map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
598 sym = (ElfW(Sym) *) D_PTR (map, l_info[DT_SYMTAB]) + symidx;
599 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
600 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
602 /* This loop doesn't handle Quickstart. */
605 if (sym->st_shndx == SHN_UNDEF)
607 if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
608 && sym->st_value && lazy)
609 *got = sym->st_value + map->l_addr;
611 *got = RESOLVE_GOTSYM (sym, symidx);
613 else if (sym->st_shndx == SHN_COMMON)
614 *got = RESOLVE_GOTSYM (sym, symidx);
615 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
616 && *got != sym->st_value
619 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)
621 if (sym->st_other == 0)
625 *got = RESOLVE_GOTSYM (sym, symidx);
632 #undef RESOLVE_GOTSYM
637 /* Set up the loaded object described by L so its stub function
638 will jump to the on-demand fixup code __dl_runtime_resolve. */
641 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
643 # ifndef RTLD_BOOTSTRAP
645 extern void _dl_runtime_resolve (ElfW(Word));
646 extern int _dl_mips_gnu_objects;
650 /* The GOT entries for functions have not yet been filled in.
651 Their initial contents will arrange when called to put an
652 offset into the .dynsym section in t8, the return address
653 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
654 got = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
656 /* This function will get called to fix up the GOT entry indicated by
657 the register t8, and then jump to the resolved address. */
658 got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
660 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
661 of got[1] of a gnu object is set to identify gnu objects.
662 Where we can store l for non gnu objects? XXX */
663 if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
664 got[1] = (ElfW(Addr)) ((unsigned) l | ELF_MIPS_GNU_GOT1_MASK);
666 _dl_mips_gnu_objects = 0;
669 /* Relocate global offset table. */
670 elf_machine_got_rel (l, lazy);