1 /* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
2 Copyright (C) 1996, 1997, 1999 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"
26 #define ELF_MACHINE_NO_PLT
32 #error ENTRY_POINT needs to be defined for MIPS.
35 #ifndef _RTLD_PROLOGUE
37 #define _RTLD_PROLOGUE(entry) "\n\t.globl " #entry \
41 #define _RTLD_PROLOGUE(entry) "\n\t.globl entry\n\t.ent entry\n\t entry:\n\t"
45 #ifndef _RTLD_EPILOGUE
47 #define _RTLD_EPILOGUE(entry) "\t.end " #entry "\n"
49 #define _RTLD_EPILOGUE(entry) "\t.end entry\n"
53 /* I have no idea what I am doing. */
54 #define ELF_MACHINE_RELOC_NOPLT -1
55 #define elf_machine_lookup_noplt_p(type) (1)
56 #define elf_machine_lookup_noexec_p(type) (0)
58 /* Translate a processor specific dynamic tag to the index
60 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
63 /* We may need 64k alignment. */
64 #define ELF_MACHINE_ALIGN_MASK 0xffff
68 * MIPS libraries are usually linked to a non-zero base address. We
69 * subtrace the base address from the address where we map the object
70 * to. This results in more efficient address space usage.
73 #define MAP_BASE_ADDR(l) ((l)->l_info[DT_MIPS(BASE_ADDRESS)] ? \
74 (l)->l_info[DT_MIPS(BASE_ADDRESS)]->d_un.d_ptr : 0)
76 #define MAP_BASE_ADDR(l) 0x5ffe0000
79 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
80 with the run-time address of the r_debug structure */
81 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
82 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
83 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
87 /* Return nonzero iff E_MACHINE is compatible with the running host. */
88 static inline int __attribute__ ((unused))
89 elf_machine_matches_host (ElfW(Half) e_machine)
101 static inline ElfW(Addr) *
102 elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
104 /* FIXME: the offset of gp from GOT may be system-dependent. */
105 return (ElfW(Addr) *) (gpreg - 0x7ff0);
108 /* Return the run-time address of the _GLOBAL_OFFSET_TABLE_.
109 Must be inlined in a function which uses global data. */
110 static inline ElfW(Addr) *
111 elf_machine_got (void)
115 __asm__ __volatile__("move %0, $28\n\t" : "=r" (gp));
116 return elf_mips_got_from_gpreg (gp);
120 /* Return the run-time load address of the shared object. */
121 static inline ElfW(Addr)
122 elf_machine_load_address (void)
125 asm (" .set noreorder\n"
129 "here: dsubu %0, $31, %0\n"
137 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
138 #define ELF_MIPS_GNU_GOT1_MASK 0x80000000
142 elf_machine_got_rel (struct link_map *map, int lazy)
147 const char *strtab = (const void *) map->l_info[DT_STRTAB]->d_un.d_ptr;
149 #define RESOLVE_GOTSYM(sym) \
151 const ElfW(Sym) *ref = sym; \
152 ElfW(Addr) sym_loadaddr; \
153 sym_loadaddr = _dl_lookup_symbol (strtab + sym->st_name, &ref, \
155 map->l_name, ELF_MACHINE_RELOC_NOPLT);\
156 (ref)? sym_loadaddr + ref->st_value: 0; \
159 got = (ElfW(Addr) *) map->l_info[DT_PLTGOT]->d_un.d_ptr;
161 /* got[0] is reserved. got[1] is also reserved for the dynamic object
162 generated by gnu ld. Skip these reserved entries from relocation. */
163 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2: 1;
164 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
165 /* Add the run-time display to all local got entries. */
167 got[i++] += map->l_addr;
169 /* Handle global got entries. */
171 sym = (ElfW(Sym) *) map->l_info[DT_SYMTAB]->d_un.d_ptr;
172 sym += map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
173 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
174 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
178 if (sym->st_shndx == SHN_UNDEF)
180 if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC)
182 if (sym->st_value && lazy)
183 *got = sym->st_value + map->l_addr;
185 *got = RESOLVE_GOTSYM (sym);
187 else /* if (*got == 0 || *got == QS) */
188 *got = RESOLVE_GOTSYM (sym);
190 else if (sym->st_shndx == SHN_COMMON)
191 *got = RESOLVE_GOTSYM (sym);
192 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
193 && *got != sym->st_value
196 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)
198 if (sym->st_other == 0)
202 *got = RESOLVE_GOTSYM (sym);
208 #undef RESOLVE_GOTSYM
213 /* Set up the loaded object described by L so its stub function
214 will jump to the on-demand fixup code in dl-runtime.c. */
217 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
220 extern void _dl_runtime_resolve (ElfW(Word));
221 extern int _dl_mips_gnu_objects;
223 #ifdef RTLD_BOOTSTRAP
230 /* The GOT entries for functions have not yet been filled in.
231 Their initial contents will arrange when called to put an
232 offset into the .dynsym section in t8, the return address
233 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
234 got = (ElfW(Addr) *) l->l_info[DT_PLTGOT]->d_un.d_ptr;
236 /* This function will get called to fix up the GOT entry indicated by
237 the register t8, and then jump to the resolved address. */
238 got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
240 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
241 of got[1] of a gnu object is set to identify gnu objects.
242 Where we can store l for non gnu objects? XXX */
243 if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
244 got[1] = (ElfW(Addr)) ((unsigned) l | ELF_MIPS_GNU_GOT1_MASK);
246 _dl_mips_gnu_objects = 0;
249 /* Relocate global offset table. */
250 elf_machine_got_rel (l, lazy);
255 /* Get link_map for this object. */
256 static inline struct link_map *
257 elf_machine_runtime_link_map (ElfW(Addr) gpreg, ElfW(Addr) stub_pc)
259 extern int _dl_mips_gnu_objects;
261 /* got[1] is reserved to keep its link map address for the shared
262 object generated by gnu linker. If all are such object, we can
263 find link map from current GPREG simply. If not so, get link map
264 for callers object containing STUB_PC. */
266 if (_dl_mips_gnu_objects)
268 ElfW(Addr) *got = elf_mips_got_from_gpreg (gpreg);
271 g1 = ((ElfW(Word) *) got)[1];
273 if ((g1 & ELF_MIPS_GNU_GOT1_MASK) != 0)
274 return (struct link_map *) (g1 & ~ELF_MIPS_GNU_GOT1_MASK);
278 struct link_map *l = _dl_loaded;
279 struct link_map *ret = 0;
280 ElfW(Addr) candidate = 0;
285 const ElfW(Phdr) *p = l->l_phdr;
286 ElfW(Half) this, nent = l->l_phnum;
289 for (this = 0; this < nent; this++)
290 if (p[this].p_type == PT_LOAD)
292 base = p[this].p_vaddr + l->l_addr;
301 /* Find closest link base addr. */
302 if ((base < stub_pc) && (candidate < base))
309 if (candidate && ret && (candidate < stub_pc))
315 _dl_signal_error (0, NULL, "cannot find runtime link map");
319 /* Mips has no PLT but define elf_machine_relplt to be elf_machine_rel. */
320 #define elf_machine_relplt elf_machine_rel
322 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
323 is called from assembler function _dl_runtime_resolve which converts
324 special argument registers t7 ($15) and t8 ($24):
325 t7 address to return to the caller of the function
326 t8 index for this function symbol in .dynsym
327 to usual c arguments. */
329 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
330 /* The flag _dl_mips_gnu_objects is set if all dynamic objects are \
331 generated by the gnu linker. */ \
332 int _dl_mips_gnu_objects = 1; \
334 /* This is called from assembly stubs below which the compiler can't see. */ \
336 __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr)) \
337 __attribute__ ((unused)); \
340 __dl_runtime_resolve (ElfW(Word) sym_index, \
341 ElfW(Word) return_address, \
342 ElfW(Addr) old_gpreg, \
343 ElfW(Addr) stub_pc) \
345 struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc); \
346 const ElfW(Sym) *const symtab \
347 = (const ElfW(Sym) *) l->l_info[DT_SYMTAB]->d_un.d_ptr; \
348 const char *strtab = (const void *) l->l_info[DT_STRTAB]->d_un.d_ptr; \
349 const ElfW(Addr) *got \
350 = (const ElfW(Addr) *) l->l_info[DT_PLTGOT]->d_un.d_ptr; \
351 const ElfW(Word) local_gotno \
352 = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; \
353 const ElfW(Word) gotsym \
354 = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val; \
355 const ElfW(Sym) *definer; \
356 ElfW(Addr) loadbase; \
357 ElfW(Addr) funcaddr; \
359 /* Look up the symbol's run-time value. */ \
360 definer = &symtab[sym_index]; \
362 loadbase = _dl_lookup_symbol (strtab + definer->st_name, &definer, \
363 l->l_scope, l->l_name, \
364 ELF_MACHINE_RELOC_NOPLT); \
366 /* Apply the relocation with that value. */ \
367 funcaddr = loadbase + definer->st_value; \
368 *(got + local_gotno + sym_index - gotsym) = funcaddr; \
376 .globl _dl_runtime_resolve\n \
377 .type _dl_runtime_resolve,@function\n \
378 .ent _dl_runtime_resolve\n \
379 _dl_runtime_resolve:\n \
381 # Save old GP to $3.\n \
383 # Modify t9 ($25) so as to point .cpload instruction.\n \
388 # Save slot call pc.\n \
390 # Save arguments and sp value in stack.\n \
404 jal __dl_runtime_resolve\n \
415 .end _dl_runtime_resolve\n \
419 /* Mask identifying addresses reserved for the user program,
420 where the dynamic linker should not map anything. */
421 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
425 /* Initial entry point code for the dynamic linker.
426 The C function `_dl_start' is the real entry point;
427 its return value is the user program's entry point.
428 Note how we have to be careful about two things:
430 1) That we allocate a minimal stack of 24 bytes for
431 every function call, the MIPS ABI states that even
432 if all arguments are passed in registers the procedure
433 called can use the 16 byte area pointed to by $sp
434 when it is called to store away the arguments passed
437 2) That under Linux the entry is named __start
438 and not just plain _start. */
440 #define RTLD_START asm ("\
443 _RTLD_PROLOGUE (ENTRY_POINT)\
444 " .globl _dl_start_user\n\
450 # i386 ABI book says that the first entry of GOT holds\n\
451 # the address of the dynamic structure. Though MIPS ABI\n\
452 # doesn't say nothing about this, I emulate this here.\n\
454 sd $4, -0x7ff0($28)\n\
457 # Get the value of label '_dl_start_user' in t9 ($25).\n\
458 dla $25, _dl_start_user\n\
464 # Save the user entry point address in saved register.\n\
466 # See if we were run as a command with the executable file\n\
467 # name as an extra leading argument.\n\
468 ld $2, _dl_skip_args\n\
470 # Load the original argument count.\n\
472 # Subtract _dl_skip_args from it.\n\
474 # Adjust the stack pointer to skip _dl_skip_args words.\n\
477 # Save back the modified argument count.\n\
479 # Get _dl_default_scope[2] as argument in _dl_init_next call below.\n\
480 1: dla $2, _dl_default_scope\n\
482 # Call _dl_init_next to return the address of an initializer\n\
483 # function to run.\n\
486 # Check for zero return, when out of initializers.\n\
488 # Call the shared object initializer function.\n\
496 # Loop to call _dl_init_next for the next initializer.\n\
498 # Pass our finalizer function to the user in ra.\n\
499 2: dla $31, _dl_fini\n\
500 # Jump to the user entry point.\n\
507 _RTLD_EPILOGUE(ENTRY_POINT) \
512 /* The MIPS never uses Elfxx_Rela relocations. */
513 #define ELF_MACHINE_NO_RELA 1
515 #endif /* !dl_machine_h */
519 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
520 MAP is the object containing the reloc. */
523 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
524 const ElfW(Sym) *sym, const struct r_found_version *version,
525 ElfW(Addr) *const reloc_addr)
528 ElfW(Addr) undo __attribute__ ((unused));
530 switch (ELFW(R_TYPE) (reloc->r_info))
536 if (ELFW(ST_BIND) (sym->st_info) == STB_LOCAL
537 && (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION
538 || ELFW(ST_TYPE) (sym->st_info) == STT_NOTYPE))
540 *reloc_addr += map->l_addr;
543 #ifndef RTLD_BOOTSTRAP
544 /* This is defined in rtld.c, but nowhere in the static libc.a;
545 make the reference weak so static programs can still link. This
546 declaration cannot be done when compiling rtld.c (i.e. #ifdef
547 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
548 _dl_rtld_map, which is incompatible with a weak decl in the same
550 weak_extern (_dl_rtld_map);
551 if (map == &_dl_rtld_map)
552 /* Undo the relocation done here during bootstrapping. Now we will
553 relocate it anew, possibly using a binding found in the user
554 program or a loaded library rather than the dynamic linker's
555 built-in definitions used while loading those libraries. */
556 undo = map->l_addr + sym->st_value;
558 loadbase = RESOLVE (&sym, version, 0);
559 *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
562 case R_MIPS_NONE: /* Alright, Wilbur. */
565 assert (! "unexpected dynamic reloc type");
571 elf_machine_lazy_rel (struct link_map *map, const ElfW(Rel) *reloc)