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
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA. */
21 #define ELF_MACHINE_NAME "MIPS"
26 #include <sys/types.h>
30 /* DT_MIPS macro ranslate 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 alignement. */
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 run-time address of the _GLOBAL_OFFSET_TABLE_.
71 Must be inlined in a function which uses global data. */
72 static inline ElfW(Addr
) *
73 elf_machine_got (void)
75 register ElfW(Addr
) gp
asm ("$28");
76 return (ElfW(Addr
) *) (gp
- 0x7ff0);
80 /* Return the run-time load address of the shared object. */
81 static inline ElfW(Addr
)
82 elf_machine_load_address (void)
85 asm (" .set noreorder\n"
89 "here: subu %0, $31, %0\n"
95 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
96 #define ELF_MIPS_GNU_GOT1_MASK 0x80000000
100 elf_machine_got_rel (struct link_map
*map
)
105 struct link_map
**scope
;
107 = ((void *) map
->l_addr
+ map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
109 ElfW(Addr
) resolve (const ElfW(Sym
) *sym
)
111 const ElfW(Sym
) *ref
= sym
;
112 ElfW(Addr
) sym_loadaddr
;
113 sym_loadaddr
= _dl_lookup_symbol (strtab
+ sym
->st_name
, &ref
, scope
,
115 return (ref
)? sym_loadaddr
+ ref
->st_value
: 0;
118 got
= (ElfW(Addr
) *) ((void *) map
->l_addr
119 + map
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
121 /* got[0] is reserved. got[1] is also reserved for the dynamic object
122 generated by gnu ld. Skip these reserved entries from relocation. */
123 i
= (got
[1] & ELF_MIPS_GNU_GOT1_MASK
)? 2: 1;
124 n
= map
->l_info
[DT_MIPS (LOCAL_GOTNO
)]->d_un
.d_val
;
125 /* Add the run-time display to all local got entries. */
127 got
[i
++] += map
->l_addr
;
130 scope
= _dl_object_relocation_scope (map
);
132 /* Handle global got entries. */
134 sym
= (ElfW(Sym
) *) ((void *) map
->l_addr
135 + map
->l_info
[DT_SYMTAB
]->d_un
.d_ptr
);
136 sym
+= map
->l_info
[DT_MIPS (GOTSYM
)]->d_un
.d_val
;
137 i
= (map
->l_info
[DT_MIPS (SYMTABNO
)]->d_un
.d_val
138 - map
->l_info
[DT_MIPS (GOTSYM
)]->d_un
.d_val
);
142 if (sym
->st_shndx
== SHN_UNDEF
)
144 if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_FUNC
)
146 if (sym
->st_value
/* && maybe_stub (sym->st_value) */)
147 *got
= sym
->st_value
+ map
->l_addr
;
149 *got
= resolve (sym
);
151 else /* if (*got == 0 || *got == QS) */
152 *got
= resolve (sym
);
154 else if (sym
->st_shndx
== SHN_COMMON
)
155 *got
= resolve (sym
);
156 else if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_FUNC
157 && *got
!= sym
->st_value
158 /* && maybe_stub (*got) */)
160 else if (ELFW(ST_TYPE
) (sym
->st_info
) == STT_SECTION
161 && ELFW(ST_BIND
) (sym
->st_info
) == STB_GLOBAL
)
163 if (sym
->st_other
== 0 && sym
->st_shndx
== SHN_ABS
)
164 *got
= sym
->st_value
+ map
->l_addr
; /* only for _gp_disp */
165 /* else SGI stuff ignored */
168 *got
= resolve (sym
);
174 *_dl_global_scope_end
= NULL
;
179 /* The MIPS never uses Elfxx_Rela relocations. */
180 #define ELF_MACHINE_NO_RELA 1
182 /* Set up the loaded object described by L so its stub function
183 will jump to the on-demand fixup code in dl-runtime.c. */
186 elf_machine_runtime_setup (struct link_map
*l
, int lazy
)
189 extern void _dl_runtime_resolve (ElfW(Word
));
193 /* The GOT entries for functions have not yet been filled in.
194 Their initial contents will arrange when called to put an
195 offset into the .dynsym section in t8, the return address
196 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
197 got
= (ElfW(Addr
) *) ((void *) l
->l_addr
198 + l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
);
200 /* This function will get called to fix up the GOT entry indicated by
201 the register t8, and then jump to the resolved address. */
202 got
[0] = (ElfW(Addr
)) &_dl_runtime_resolve
;
204 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
205 of got[1] of a gnu object is set to identify gnu objects.
206 Where we can store l for non gnu objects? XXX */
207 if ((got
[1] & ELF_MIPS_GNU_GOT1_MASK
) != 0)
208 got
[1] = (ElfW(Addr
)) ((unsigned) l
| ELF_MIPS_GNU_GOT1_MASK
);
213 /* Relocate global offset table. */
214 elf_machine_got_rel (l
);
217 /* Get link_map for this object. */
218 static inline struct link_map
*
219 elf_machine_runtime_link_map (ElfW(Addr
) gpreg
)
221 ElfW(Addr
) *got
= elf_mips_got_from_gpreg (gpreg
);
224 g1
= ((ElfW(Word
) *) got
)[1];
226 /* got[1] is reserved to keep its link map address for the shared
227 object generated by gnu linker. If not so, we must search GOT
228 in object list slowly. XXX */
229 if ((g1
& ELF_MIPS_GNU_GOT1_MASK
) != 0)
230 return (struct link_map
*) (g1
& ~ELF_MIPS_GNU_GOT1_MASK
);
233 struct link_map
*l
= _dl_loaded
;
236 if (got
== (ElfW(Addr
) *) ((void *) l
->l_addr
237 + l
->l_info
[DT_PLTGOT
]->d_un
.d_ptr
))
242 _dl_signal_error (0, NULL
, "cannot find runtime link map");
245 /* Mips has no PLT but define elf_machine_relplt to be elf_machine_rel. */
246 #define elf_machine_relplt elf_machine_rel
248 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
249 is called from assembler function _dl_runtime_resolve which converts
250 special argument registers t7 ($15) and t8 ($24):
251 t7 address to return to the caller of the function
252 t8 index for this function symbol in .dynsym
253 to usual c arguments. */
255 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
256 /* This is called from assembly stubs below which the compiler can't see. */ \
257 static ElfW(Addr) __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr)) \
258 __attribute__ ((unused)); \
261 __dl_runtime_resolve (ElfW(Word) sym_index,\
262 ElfW(Word) return_address,\
263 ElfW(Addr) old_gpreg)\
265 struct link_map *l = elf_machine_runtime_link_map (old_gpreg);\
266 const ElfW(Sym) *const symtab\
267 = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr);\
269 = (void *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);\
270 const ElfW(Addr) *got\
271 = (const ElfW(Addr) *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);\
272 const ElfW(Word) local_gotno\
273 = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;\
274 const ElfW(Word) gotsym\
275 = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;\
276 const ElfW(Sym) *definer;\
277 ElfW(Addr) loadbase;\
278 ElfW(Addr) funcaddr;\
279 struct link_map **scope;\
281 /* Look up the symbol's run-time value. */\
282 scope = _dl_object_relocation_scope (l);\
283 definer = &symtab[sym_index];\
285 loadbase = _dl_lookup_symbol (strtab + definer->st_name, &definer,\
286 scope, l->l_name, 0, 1);\
288 *_dl_global_scope_end = NULL;\
290 /* Apply the relocation with that value. */\
291 funcaddr = loadbase + definer->st_value;\
292 *(got + local_gotno + sym_index - gotsym) = funcaddr;\
300 .globl _dl_runtime_resolve\n\
301 .type _dl_runtime_resolve,@function\n\
302 .ent _dl_runtime_resolve\n\
303 _dl_runtime_resolve:\n\
305 # Save old GP to $3.\n\
307 # Modify t9 ($25) so as to point .cpload instruction.\n\
312 # Save arguments and sp value in stack.\n\
325 jal __dl_runtime_resolve\n\
336 .end _dl_runtime_resolve\n\
339 /* Mask identifying addresses reserved for the user program,
340 where the dynamic linker should not map anything. */
341 #define ELF_MACHINE_USER_ADDRESS_MASK 0x00000000UL
343 /* Initial entry point code for the dynamic linker.
344 The C function `_dl_start' is the real entry point;
345 its return value is the user program's entry point. */
347 #define RTLD_START asm ("\
350 .globl _dl_start_user\n\
358 # i386 ABI book says that the first entry of GOT holds\n\
359 # the address of the dynamic structure. Though MIPS ABI\n\
360 # doesn't say nothing about this, I emulate this here.\n\
362 sw $4, -0x7ff0($28)\n\
365 # Get the value of label '_dl_start_user' in t9 ($25).\n\
366 la $25, _dl_start_user\n\
372 # Save the user entry point address in saved register.\n\
374 # See if we were run as a command with the executable file\n\
375 # name as an extra leading argument.\n\
376 lw $2, _dl_skip_args\n\
378 # Load the original argument count.\n\
380 # Subtract _dl_skip_args from it.\n\
382 # Adjust the stack pointer to skip _dl_skip_args words.\n\
385 # Save back the modified argument count.\n\
387 # Get _dl_default_scope[2] as argument in _dl_init_next call below.\n\
388 1: la $2, _dl_default_scope\n\
390 # Call _dl_init_next to return the address of an initializer\n\
391 # function to run.\n\
394 # Check for zero return, when out of initializers.\n\
396 # Call the shared object initializer function.\n\
404 # Loop to call _dl_init_next for the next initializer.\n\
406 # Pass our finalizer function to the user in ra.\n\
407 2: la $31, _dl_fini\n\
408 # Jump to the user entry point.\n\
420 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
421 MAP is the object containing the reloc. */
424 elf_machine_rel (struct link_map
*map
,
425 const ElfW(Rel
) *reloc
, const ElfW(Sym
) *sym
)
427 ElfW(Addr
) *const reloc_addr
= (void *) (map
->l_addr
+ reloc
->r_offset
);
428 ElfW(Addr
) loadbase
, undo
;
430 switch (ELFW(R_TYPE
) (reloc
->r_info
))
433 if (ELFW(ST_BIND
) (sym
->st_info
) == STB_LOCAL
434 && (ELFW(ST_TYPE
) (sym
->st_info
) == STT_SECTION
435 || ELFW(ST_TYPE
) (sym
->st_info
) == STT_NOTYPE
))
436 *reloc_addr
+= map
->l_addr
;
439 #ifndef RTLD_BOOTSTRAP
440 /* This is defined in rtld.c, but nowhere in the static libc.a;
441 make the reference weak so static programs can still link. This
442 declaration cannot be done when compiling rtld.c (i.e. #ifdef
443 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
444 _dl_rtld_map, which is incompatible with a weak decl in the same
446 weak_extern (_dl_rtld_map
);
447 if (map
== &_dl_rtld_map
)
448 /* Undo the relocation done here during bootstrapping. Now we will
449 relocate it anew, possibly using a binding found in the user
450 program or a loaded library rather than the dynamic linker's
451 built-in definitions used while loading those libraries. */
452 undo
= map
->l_addr
+ sym
->st_value
;
456 loadbase
= RESOLVE (&sym
, (ElfW(Addr
)) reloc_addr
, 0);
457 *reloc_addr
+= (sym
? (loadbase
+ sym
->st_value
) : 0) - undo
;
460 case R_MIPS_NONE
: /* Alright, Wilbur. */
463 assert (! "unexpected dynamic reloc type");
469 elf_machine_lazy_rel (struct link_map
*map
, const ElfW(Rel
) *reloc
)