1 /* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
2 Copyright (C) 1996 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 #define ELF_MACHINE_NAME "MIPS"
26 #include <sys/types.h>
30 /* Translate a processor specific dynamic tag to the index
32 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
35 /* XXX If FLAGS has the MAP_ALIGN bit, we need 64k alignment. */
37 #define MAP_ALIGN 0x1000
39 #define ELF_MACHINE_ALIGN_MASK(flags) ((flags & MAP_ALIGN) ? 0xffff : 0)
42 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
43 with the run-time address of the r_debug structure */
44 #define ELF_MACHINE_SET_DEBUG(l,r) \
45 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
46 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
50 /* Return nonzero iff E_MACHINE is compatible with the running host. */
52 elf_machine_matches_host (ElfW(Half
) e_machine
)
64 static inline ElfW(Addr
) *
65 elf_mips_got_from_gpreg (ElfW(Addr
) gpreg
)
67 /* FIXME: the offset of gp from GOT may be system-dependent. */
68 return (ElfW(Addr
) *) (gpreg
- 0x7ff0);
70 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
71 first element of the GOT. This must be inlined in a function which
73 static inline ElfW(Addr
)
74 elf_machine_dynamic (void)
76 register ElfW(Addr
) gp
asm ("$28");
77 return * (ElfW(Addr
) *) (gp
- 0x7ff0);
81 /* Return the run-time load address of the shared object. */
82 static inline ElfW(Addr
)
83 elf_machine_load_address (void)
86 asm (" .set noreorder\n"
90 "here: subu %0, $31, %0\n"
96 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
97 #define ELF_MIPS_GNU_GOT1_MASK 0x80000000
101 elf_machine_got_rel (struct link_map
*map
)
106 struct link_map
**scope
;
108 = ((void *) map
->l_addr
+ map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
110 ElfW(Addr
) resolve (const ElfW(Sym
) *sym
)
112 const ElfW(Sym
) *ref
= sym
;
113 ElfW(Addr
) sym_loadaddr
;
114 sym_loadaddr
= _dl_lookup_symbol (strtab
+ sym
->st_name
, &ref
, scope
,
116 return (ref
)? sym_loadaddr
+ ref
->st_value
: 0;
119 got
= (ElfW(Addr
) *) ((void *) map
->l_addr
120 + map
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
122 /* got[0] is reserved. got[1] is also reserved for the dynamic object
123 generated by gnu ld. Skip these reserved entries from relocation. */
124 i
= (got
[1] & ELF_MIPS_GNU_GOT1_MASK
)? 2: 1;
125 n
= map
->l_info
[DT_MIPS (LOCAL_GOTNO
)]->d_un
.d_val
;
126 /* Add the run-time display to all local got entries. */
128 got
[i
++] += map
->l_addr
;
131 scope
= _dl_object_relocation_scope (map
);
133 /* Handle global got entries. */
135 sym
= (ElfW(Sym
) *) ((void *) map
->l_addr
136 + map
->l_info
[DT_SYMTAB
]->d_un
.d_ptr
);
137 sym
+= map
->l_info
[DT_MIPS (GOTSYM
)]->d_un
.d_val
;
138 i
= (map
->l_info
[DT_MIPS (SYMTABNO
)]->d_un
.d_val
139 - map
->l_info
[DT_MIPS (GOTSYM
)]->d_un
.d_val
);
143 if (sym
->st_shndx
== SHN_UNDEF
)
145 if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_FUNC
)
147 if (sym
->st_value
/* && maybe_stub (sym->st_value) */)
148 *got
= sym
->st_value
+ map
->l_addr
;
150 *got
= resolve (sym
);
152 else /* if (*got == 0 || *got == QS) */
153 *got
= resolve (sym
);
155 else if (sym
->st_shndx
== SHN_COMMON
)
156 *got
= resolve (sym
);
157 else if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_FUNC
158 && *got
!= sym
->st_value
159 /* && maybe_stub (*got) */)
161 else if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_SECTION
162 && ELFW(ST_BIND
) (sym
->st_info
) == STB_GLOBAL
)
164 if (sym
->st_other
== 0 && sym
->st_shndx
== SHN_ABS
)
165 *got
= sym
->st_value
+ map
->l_addr
; /* only for _gp_disp */
166 /* else SGI stuff ignored */
169 *got
= resolve (sym
);
175 *_dl_global_scope_end
= NULL
;
180 /* The MIPS never uses Elfxx_Rela relocations. */
181 #define ELF_MACHINE_NO_RELA 1
183 /* Set up the loaded object described by L so its stub function
184 will jump to the on-demand fixup code in dl-runtime.c. */
187 elf_machine_runtime_setup (struct link_map
*l
, int lazy
)
190 extern void _dl_runtime_resolve (ElfW(Word
));
194 /* The GOT entries for functions have not yet been filled in.
195 Their initial contents will arrange when called to put an
196 offset into the .dynsym section in t8, the return address
197 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
198 got
= (ElfW(Addr
) *) ((void *) l
->l_addr
199 + l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
201 /* This function will get called to fix up the GOT entry indicated by
202 the register t8, and then jump to the resolved address. */
203 got
[0] = (ElfW(Addr
)) &_dl_runtime_resolve
;
205 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
206 of got[1] of a gnu object is set to identify gnu objects.
207 Where we can store l for non gnu objects? XXX */
208 if ((got
[1] & ELF_MIPS_GNU_GOT1_MASK
) != 0)
209 got
[1] = (ElfW(Addr
)) ((unsigned) l
| ELF_MIPS_GNU_GOT1_MASK
);
214 /* Relocate global offset table. */
215 elf_machine_got_rel (l
);
218 /* Get link_map for this object. */
219 static inline struct link_map
*
220 elf_machine_runtime_link_map (ElfW(Addr
) gpreg
)
222 ElfW(Addr
) *got
= elf_mips_got_from_gpreg (gpreg
);
225 g1
= ((ElfW(Word
) *) got
)[1];
227 /* got[1] is reserved to keep its link map address for the shared
228 object generated by gnu linker. If not so, we must search GOT
229 in object list slowly. XXX */
230 if ((g1
& ELF_MIPS_GNU_GOT1_MASK
) != 0)
231 return (struct link_map
*) (g1
& ~ELF_MIPS_GNU_GOT1_MASK
);
234 struct link_map
*l
= _dl_loaded
;
237 if (got
== (ElfW(Addr
) *) ((void *) l
->l_addr
238 + l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
))
243 _dl_signal_error (0, NULL
, "cannot find runtime link map");
246 /* Mips has no PLT but define elf_machine_relplt to be elf_machine_rel. */
247 #define elf_machine_relplt elf_machine_rel
249 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
250 is called from assembler function _dl_runtime_resolve which converts
251 special argument registers t7 ($15) and t8 ($24):
252 t7 address to return to the caller of the function
253 t8 index for this function symbol in .dynsym
254 to usual c arguments. */
256 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
257 /* This is called from assembly stubs below which the compiler can't see. */ \
258 static ElfW(Addr) __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr)) \
259 __attribute__ ((unused)); \
262 __dl_runtime_resolve (ElfW(Word) sym_index,\
263 ElfW(Word) return_address,\
264 ElfW(Addr) old_gpreg)\
266 struct link_map *l = elf_machine_runtime_link_map (old_gpreg);\
267 const ElfW(Sym) *const symtab\
268 = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr);\
270 = (void *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);\
271 const ElfW(Addr) *got\
272 = (const ElfW(Addr) *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);\
273 const ElfW(Word) local_gotno\
274 = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;\
275 const ElfW(Word) gotsym\
276 = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;\
277 const ElfW(Sym) *definer;\
278 ElfW(Addr) loadbase;\
279 ElfW(Addr) funcaddr;\
280 struct link_map **scope;\
282 /* Look up the symbol's run-time value. */\
283 scope = _dl_object_relocation_scope (l);\
284 definer = &symtab[sym_index];\
286 loadbase = _dl_lookup_symbol (strtab + definer->st_name, &definer,\
287 scope, l->l_name, 0, 1);\
289 *_dl_global_scope_end = NULL;\
291 /* Apply the relocation with that value. */\
292 funcaddr = loadbase + definer->st_value;\
293 *(got + local_gotno + sym_index - gotsym) = funcaddr;\
301 .globl _dl_runtime_resolve\n\
302 .type _dl_runtime_resolve,@function\n\
303 .ent _dl_runtime_resolve\n\
304 _dl_runtime_resolve:\n\
306 # Save old GP to $3.\n\
308 # Modify t9 ($25) so as to point .cpload instruction.\n\
313 # Save arguments and sp value in stack.\n\
326 jal __dl_runtime_resolve\n\
337 .end _dl_runtime_resolve\n\
340 /* Mask identifying addresses reserved for the user program,
341 where the dynamic linker should not map anything. */
342 #define ELF_MACHINE_USER_ADDRESS_MASK 0x00000000UL
344 /* Initial entry point code for the dynamic linker.
345 The C function `_dl_start' is the real entry point;
346 its return value is the user program's entry point. */
348 #define RTLD_START asm ("\
351 .globl _dl_start_user\n\
359 # i386 ABI book says that the first entry of GOT holds\n\
360 # the address of the dynamic structure. Though MIPS ABI\n\
361 # doesn't say nothing about this, I emulate this here.\n\
363 sw $4, -0x7ff0($28)\n\
366 # Get the value of label '_dl_start_user' in t9 ($25).\n\
367 la $25, _dl_start_user\n\
373 # Save the user entry point address in saved register.\n\
375 # See if we were run as a command with the executable file\n\
376 # name as an extra leading argument.\n\
377 lw $2, _dl_skip_args\n\
379 # Load the original argument count.\n\
381 # Subtract _dl_skip_args from it.\n\
383 # Adjust the stack pointer to skip _dl_skip_args words.\n\
386 # Save back the modified argument count.\n\
388 # Get _dl_default_scope[2] as argument in _dl_init_next call below.\n\
389 1: la $2, _dl_default_scope\n\
391 # Call _dl_init_next to return the address of an initializer\n\
392 # function to run.\n\
395 # Check for zero return, when out of initializers.\n\
397 # Call the shared object initializer function.\n\
405 # Loop to call _dl_init_next for the next initializer.\n\
407 # Pass our finalizer function to the user in ra.\n\
408 2: la $31, _dl_fini\n\
409 # Jump to the user entry point.\n\
421 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
422 MAP is the object containing the reloc. */
425 elf_machine_rel (struct link_map
*map
,
426 const ElfW(Rel
) *reloc
, const ElfW(Sym
) *sym
)
428 ElfW(Addr
) *const reloc_addr
= (void *) (map
->l_addr
+ reloc
->r_offset
);
429 ElfW(Addr
) loadbase
, undo
;
431 switch (ELFW(R_TYPE
) (reloc
->r_info
))
434 if (ELFW(ST_BIND
) (sym
->st_info
) == STB_LOCAL
435 && (ELFW(ST_TYPE
) (sym
->st_info
) == STT_SECTION
436 || ELFW(ST_TYPE
) (sym
->st_info
) == STT_NOTYPE
))
437 *reloc_addr
+= map
->l_addr
;
440 #ifndef RTLD_BOOTSTRAP
441 /* This is defined in rtld.c, but nowhere in the static libc.a;
442 make the reference weak so static programs can still link. This
443 declaration cannot be done when compiling rtld.c (i.e. #ifdef
444 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
445 _dl_rtld_map, which is incompatible with a weak decl in the same
447 weak_extern (_dl_rtld_map
);
448 if (map
== &_dl_rtld_map
)
449 /* Undo the relocation done here during bootstrapping. Now we will
450 relocate it anew, possibly using a binding found in the user
451 program or a loaded library rather than the dynamic linker's
452 built-in definitions used while loading those libraries. */
453 undo
= map
->l_addr
+ sym
->st_value
;
457 loadbase
= RESOLVE (&sym
, 0);
458 *reloc_addr
+= (sym
? (loadbase
+ sym
->st_value
) : 0) - undo
;
461 case R_MIPS_NONE
: /* Alright, Wilbur. */
464 assert (! "unexpected dynamic reloc type");
470 elf_machine_lazy_rel (struct link_map
*map
, const ElfW(Rel
) *reloc
)