Avoid attempt for runtime checks if all environments are defined
[glibc.git] / sysdeps / mips / dl-machine.h
blob8c0b40e8de514b866564e5acf60bc0cfc731ea79
1 /* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
2 Copyright (C) 1996-2016 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library. If not, see
18 <http://www.gnu.org/licenses/>. */
20 /* FIXME: Profiling of shared libraries is not implemented yet. */
21 #ifndef dl_machine_h
22 #define dl_machine_h
24 #define ELF_MACHINE_NAME "MIPS"
26 #include <entry.h>
28 #ifndef ENTRY_POINT
29 #error ENTRY_POINT needs to be defined for MIPS.
30 #endif
32 #include <sgidefs.h>
33 #include <sysdep.h>
34 #include <sys/asm.h>
35 #include <dl-tls.h>
37 /* The offset of gp from GOT might be system-dependent. It's set by
38 ld. The same value is also */
39 #define OFFSET_GP_GOT 0x7ff0
41 #ifndef _RTLD_PROLOGUE
42 # define _RTLD_PROLOGUE(entry) \
43 ".globl\t" __STRING(entry) "\n\t" \
44 ".ent\t" __STRING(entry) "\n\t" \
45 ".type\t" __STRING(entry) ", @function\n" \
46 __STRING(entry) ":\n\t"
47 #endif
49 #ifndef _RTLD_EPILOGUE
50 # define _RTLD_EPILOGUE(entry) \
51 ".end\t" __STRING(entry) "\n\t" \
52 ".size\t" __STRING(entry) ", . - " __STRING(entry) "\n\t"
53 #endif
55 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.
56 This only makes sense on MIPS when using PLTs, so choose the
57 PLT relocation (not encountered when not using PLTs). */
58 #define ELF_MACHINE_JMP_SLOT R_MIPS_JUMP_SLOT
59 #define elf_machine_type_class(type) \
60 ((((type) == ELF_MACHINE_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
61 | (((type) == R_MIPS_COPY) * ELF_RTYPE_CLASS_COPY))
63 #define ELF_MACHINE_PLT_REL 1
64 #define ELF_MACHINE_NO_REL 0
65 #define ELF_MACHINE_NO_RELA 0
67 /* Translate a processor specific dynamic tag to the index
68 in l_info array. */
69 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
71 /* If there is a DT_MIPS_RLD_MAP_REL or DT_MIPS_RLD_MAP entry in the dynamic
72 section, fill in the debug map pointer with the run-time address of the
73 r_debug structure. */
74 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
75 do { if ((l)->l_info[DT_MIPS (RLD_MAP_REL)]) \
76 { \
77 char *ptr = (char *)(l)->l_info[DT_MIPS (RLD_MAP_REL)]; \
78 ptr += (l)->l_info[DT_MIPS (RLD_MAP_REL)]->d_un.d_val; \
79 *(ElfW(Addr) *)ptr = (ElfW(Addr)) (r); \
80 } \
81 else if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
82 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
83 (ElfW(Addr)) (r); \
84 } while (0)
86 #if ((defined __mips_nan2008 && !defined HAVE_MIPS_NAN2008) \
87 || (!defined __mips_nan2008 && defined HAVE_MIPS_NAN2008))
88 # error "Configuration inconsistency: __mips_nan2008 != HAVE_MIPS_NAN2008, overridden CFLAGS?"
89 #endif
90 #ifdef __mips_nan2008
91 # define ELF_MACHINE_NAN2008 EF_MIPS_NAN2008
92 #else
93 # define ELF_MACHINE_NAN2008 0
94 #endif
96 /* Return nonzero iff ELF header is compatible with the running host. */
97 static inline int __attribute_used__
98 elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
100 #if _MIPS_SIM == _ABIO32 || _MIPS_SIM == _ABIN32
101 /* Don't link o32 and n32 together. */
102 if (((ehdr->e_flags & EF_MIPS_ABI2) != 0) != (_MIPS_SIM == _ABIN32))
103 return 0;
104 #endif
106 /* Don't link 2008-NaN and legacy-NaN objects together. */
107 if ((ehdr->e_flags & EF_MIPS_NAN2008) != ELF_MACHINE_NAN2008)
108 return 0;
110 /* Ensure that the old O32 FP64 ABI is never loaded, it is not supported
111 on linux. */
112 if (ehdr->e_flags & EF_MIPS_FP64)
113 return 0;
115 switch (ehdr->e_machine)
117 case EM_MIPS:
118 case EM_MIPS_RS3_LE:
119 return 1;
120 default:
121 return 0;
125 static inline ElfW(Addr) *
126 elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
128 /* FIXME: the offset of gp from GOT may be system-dependent. */
129 return (ElfW(Addr) *) (gpreg - OFFSET_GP_GOT);
132 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
133 first element of the GOT. This must be inlined in a function which
134 uses global data. We assume its $gp points to the primary GOT. */
135 static inline ElfW(Addr)
136 elf_machine_dynamic (void)
138 register ElfW(Addr) gp __asm__ ("$28");
139 return *elf_mips_got_from_gpreg (gp);
142 #define STRINGXP(X) __STRING(X)
143 #define STRINGXV(X) STRINGV_(X)
144 #define STRINGV_(...) # __VA_ARGS__
146 /* Return the run-time load address of the shared object. */
147 static inline ElfW(Addr)
148 elf_machine_load_address (void)
150 ElfW(Addr) addr;
151 #ifndef __mips16
152 asm (" .set noreorder\n"
153 " " STRINGXP (PTR_LA) " %0, 0f\n"
154 # if !defined __mips_isa_rev || __mips_isa_rev < 6
155 " bltzal $0, 0f\n"
156 " nop\n"
157 "0: " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
158 # else
159 "0: addiupc $31, 0\n"
160 " " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
161 # endif
162 " .set reorder\n"
163 : "=r" (addr)
164 : /* No inputs */
165 : "$31");
166 #else
167 ElfW(Addr) tmp;
168 asm (" .set noreorder\n"
169 " move %1,$gp\n"
170 " lw %1,%%got(0f)(%1)\n"
171 "0: .fill 0\n" /* Clear the ISA bit on 0:. */
172 " la %0,0b\n"
173 " addiu %1,%%lo(0b)\n"
174 " subu %0,%1\n"
175 " .set reorder\n"
176 : "=d" (addr), "=d" (tmp)
177 : /* No inputs */);
178 #endif
179 return addr;
182 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
183 #if _MIPS_SIM == _ABI64
184 # define ELF_MIPS_GNU_GOT1_MASK 0x8000000000000000L
185 #else
186 # define ELF_MIPS_GNU_GOT1_MASK 0x80000000L
187 #endif
189 /* We can't rely on elf_machine_got_rel because _dl_object_relocation_scope
190 fiddles with global data. */
191 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
192 do { \
193 struct link_map *map = &bootstrap_map; \
194 ElfW(Sym) *sym; \
195 ElfW(Addr) *got; \
196 int i, n; \
198 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]); \
200 if (__builtin_expect (map->l_addr == 0, 1)) \
201 break; \
203 /* got[0] is reserved. got[1] is also reserved for the dynamic object \
204 generated by gnu ld. Skip these reserved entries from \
205 relocation. */ \
206 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1; \
207 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; \
209 /* Add the run-time displacement to all local got entries. */ \
210 while (i < n) \
211 got[i++] += map->l_addr; \
213 /* Handle global got entries. */ \
214 got += n; \
215 sym = (ElfW(Sym) *) D_PTR(map, l_info[DT_SYMTAB]) \
216 + map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val; \
217 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val \
218 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val); \
220 while (i--) \
222 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx == SHN_COMMON) \
223 *got = map->l_addr + sym->st_value; \
224 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC \
225 && *got != sym->st_value) \
226 *got += map->l_addr; \
227 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION) \
229 if (sym->st_other == 0) \
230 *got += map->l_addr; \
232 else \
233 *got = map->l_addr + sym->st_value; \
235 got++; \
236 sym++; \
238 } while(0)
241 /* Mask identifying addresses reserved for the user program,
242 where the dynamic linker should not map anything. */
243 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
246 /* Initial entry point code for the dynamic linker.
247 The C function `_dl_start' is the real entry point;
248 its return value is the user program's entry point.
249 Note how we have to be careful about two things:
251 1) That we allocate a minimal stack of 24 bytes for
252 every function call, the MIPS ABI states that even
253 if all arguments are passed in registers the procedure
254 called can use the 16 byte area pointed to by $sp
255 when it is called to store away the arguments passed
256 to it.
258 2) That under Unix the entry is named __start
259 and not just plain _start. */
261 #ifndef __mips16
262 # if !defined __mips_isa_rev || __mips_isa_rev < 6
263 # define LCOFF STRINGXP(.Lcof2)
264 # define LOAD_31 STRINGXP(bltzal $8) "," STRINGXP(.Lcof2)
265 # else
266 # define LCOFF STRINGXP(.Lcof1)
267 # define LOAD_31 "addiupc $31, 0"
268 # endif
269 # define RTLD_START asm (\
270 ".text\n\
271 " _RTLD_PROLOGUE(ENTRY_POINT) "\
272 " STRINGXV(SETUP_GPX($25)) "\n\
273 " STRINGXV(SETUP_GPX64($18,$25)) "\n\
274 # i386 ABI book says that the first entry of GOT holds\n\
275 # the address of the dynamic structure. Though MIPS ABI\n\
276 # doesn't say nothing about this, I emulate this here.\n\
277 " STRINGXP(PTR_LA) " $4, _DYNAMIC\n\
278 # Subtract OFFSET_GP_GOT\n\
279 " STRINGXP(PTR_S) " $4, -0x7ff0($28)\n\
280 move $4, $29\n\
281 " STRINGXP(PTR_SUBIU) " $29, 16\n\
283 " STRINGXP(PTR_LA) " $8, " LCOFF "\n\
284 .Lcof1: " LOAD_31 "\n\
285 .Lcof2: " STRINGXP(PTR_SUBU) " $8, $31, $8\n\
287 " STRINGXP(PTR_LA) " $25, _dl_start\n\
288 " STRINGXP(PTR_ADDU) " $25, $8\n\
289 jalr $25\n\
291 " STRINGXP(PTR_ADDIU) " $29, 16\n\
292 # Get the value of label '_dl_start_user' in t9 ($25).\n\
293 " STRINGXP(PTR_LA) " $25, _dl_start_user\n\
294 " _RTLD_EPILOGUE(ENTRY_POINT) "\
297 " _RTLD_PROLOGUE(_dl_start_user) "\
298 " STRINGXP(SETUP_GP) "\n\
299 " STRINGXV(SETUP_GP64($18,_dl_start_user)) "\n\
300 move $16, $28\n\
301 # Save the user entry point address in a saved register.\n\
302 move $17, $2\n\
303 # See if we were run as a command with the executable file\n\
304 # name as an extra leading argument.\n\
305 lw $2, _dl_skip_args\n\
306 beq $2, $0, 1f\n\
307 # Load the original argument count.\n\
308 " STRINGXP(PTR_L) " $4, 0($29)\n\
309 # Subtract _dl_skip_args from it.\n\
310 subu $4, $2\n\
311 # Adjust the stack pointer to skip _dl_skip_args words.\n\
312 sll $2, " STRINGXP (PTRLOG) "\n\
313 " STRINGXP(PTR_ADDU) " $29, $2\n\
314 # Save back the modified argument count.\n\
315 " STRINGXP(PTR_S) " $4, 0($29)\n\
316 1: # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
317 " STRINGXP(PTR_L) " $4, _rtld_local\n\
318 " STRINGXP(PTR_L) /* or lw??? fixme */ " $5, 0($29)\n\
319 " STRINGXP(PTR_LA) " $6, " STRINGXP (PTRSIZE) "($29)\n\
320 sll $7, $5, " STRINGXP (PTRLOG) "\n\
321 " STRINGXP(PTR_ADDU) " $7, $7, $6\n\
322 " STRINGXP(PTR_ADDU) " $7, $7, " STRINGXP (PTRSIZE) " \n\
323 # Make sure the stack pointer is aligned for _dl_init.\n\
324 and $2, $29, -2 * " STRINGXP(SZREG) "\n\
325 move $8, $29\n\
326 " STRINGXP(PTR_SUBIU) " $29, $2, 32\n\
327 " STRINGXP(PTR_S) " $8, (32 - " STRINGXP(SZREG) ")($29)\n\
328 " STRINGXP(SAVE_GP(16)) "\n\
329 # Call the function to run the initializers.\n\
330 jal _dl_init\n\
331 # Restore the stack pointer for _start.\n\
332 " STRINGXP(PTR_L) " $29, (32 - " STRINGXP(SZREG) ")($29)\n\
333 # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
334 " STRINGXP(PTR_LA) " $2, _dl_fini\n\
335 # Jump to the user entry point.\n\
336 move $25, $17\n\
337 jr $25\n\t"\
338 _RTLD_EPILOGUE(_dl_start_user)\
339 ".previous"\
342 #else /* __mips16 */
343 /* MIPS16 version. We currently only support O32 under MIPS16; the proper
344 assembly preprocessor abstractions will need to be added if other ABIs
345 are to be supported. */
347 # define RTLD_START asm (\
348 ".text\n\
349 .set mips16\n\
350 " _RTLD_PROLOGUE (ENTRY_POINT) "\
351 # Construct GP value in $3.\n\
352 li $3, %hi(_gp_disp)\n\
353 addiu $4, $pc, %lo(_gp_disp)\n\
354 sll $3, 16\n\
355 addu $3, $4\n\
356 move $28, $3\n\
357 lw $4, %got(_DYNAMIC)($3)\n\
358 sw $4, -0x7ff0($3)\n\
359 move $4, $sp\n\
360 addiu $sp, -16\n\
361 # _dl_start() is sufficiently near to use pc-relative\n\
362 # load address.\n\
363 la $3, _dl_start\n\
364 move $25, $3\n\
365 jalr $3\n\
366 addiu $sp, 16\n\
367 " _RTLD_EPILOGUE (ENTRY_POINT) "\
370 " _RTLD_PROLOGUE (_dl_start_user) "\
371 li $16, %hi(_gp_disp)\n\
372 addiu $4, $pc, %lo(_gp_disp)\n\
373 sll $16, 16\n\
374 addu $16, $4\n\
375 move $17, $2\n\
376 move $28, $16\n\
377 lw $4, %got(_dl_skip_args)($16)\n\
378 lw $4, 0($4)\n\
379 beqz $4, 1f\n\
380 # Load the original argument count.\n\
381 lw $5, 0($sp)\n\
382 # Subtract _dl_skip_args from it.\n\
383 subu $5, $4\n\
384 # Adjust the stack pointer to skip _dl_skip_args words.\n\
385 sll $4, " STRINGXP (PTRLOG) "\n\
386 move $6, $sp\n\
387 addu $6, $4\n\
388 move $sp, $6\n\
389 # Save back the modified argument count.\n\
390 sw $5, 0($sp)\n\
391 1: # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
392 lw $4, %got(_rtld_local)($16)\n\
393 lw $4, 0($4)\n\
394 lw $5, 0($sp)\n\
395 addiu $6, $sp, " STRINGXP (PTRSIZE) "\n\
396 sll $7, $5, " STRINGXP (PTRLOG) "\n\
397 addu $7, $6\n\
398 addu $7, " STRINGXP (PTRSIZE) "\n\
399 # Make sure the stack pointer is aligned for _dl_init.\n\
400 li $2, 2 * " STRINGXP (SZREG) "\n\
401 neg $2, $2\n\
402 move $3, $sp\n\
403 and $2, $3\n\
404 sw $3, -" STRINGXP (SZREG) "($2)\n\
405 addiu $2, -32\n\
406 move $sp, $2\n\
407 sw $16, 16($sp)\n\
408 # Call the function to run the initializers.\n\
409 lw $2, %call16(_dl_init)($16)\n\
410 move $25, $2\n\
411 jalr $2\n\
412 # Restore the stack pointer for _start.\n\
413 lw $2, 32-" STRINGXP (SZREG) "($sp)\n\
414 move $sp, $2\n\
415 move $28, $16\n\
416 # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
417 lw $2, %call16(_dl_fini)($16)\n\
418 # Jump to the user entry point.\n\
419 move $25, $17\n\
420 jr $17\n\t"\
421 _RTLD_EPILOGUE (_dl_start_user)\
422 ".previous"\
425 #endif /* __mips16 */
427 /* Names of the architecture-specific auditing callback functions. */
428 # if _MIPS_SIM == _ABIO32
429 # define ARCH_LA_PLTENTER mips_o32_gnu_pltenter
430 # define ARCH_LA_PLTEXIT mips_o32_gnu_pltexit
431 # elif _MIPS_SIM == _ABIN32
432 # define ARCH_LA_PLTENTER mips_n32_gnu_pltenter
433 # define ARCH_LA_PLTEXIT mips_n32_gnu_pltexit
434 # else
435 # define ARCH_LA_PLTENTER mips_n64_gnu_pltenter
436 # define ARCH_LA_PLTEXIT mips_n64_gnu_pltexit
437 # endif
439 /* We define an initialization function. This is called very early in
440 _dl_sysdep_start. */
441 #define DL_PLATFORM_INIT dl_platform_init ()
443 static inline void __attribute__ ((unused))
444 dl_platform_init (void)
446 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
447 /* Avoid an empty string which would disturb us. */
448 GLRO(dl_platform) = NULL;
451 /* For a non-writable PLT, rewrite the .got.plt entry at RELOC_ADDR to
452 point at the symbol with address VALUE. For a writable PLT, rewrite
453 the corresponding PLT entry instead. */
454 static inline ElfW(Addr)
455 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
456 const ElfW(Rel) *reloc,
457 ElfW(Addr) *reloc_addr, ElfW(Addr) value)
459 return *reloc_addr = value;
462 static inline ElfW(Addr)
463 elf_machine_plt_value (struct link_map *map, const ElfW(Rel) *reloc,
464 ElfW(Addr) value)
466 return value;
469 /* The semantics of zero/non-zero values of undefined symbols differs
470 depending on whether the non-PIC ABI is in use. Under the non-PIC
471 ABI, a non-zero value indicates that there is an address reference
472 to the symbol and thus it must always be resolved (except when
473 resolving a jump slot relocation) to the PLT entry whose address is
474 provided as the symbol's value; a zero value indicates that this
475 canonical-address behaviour is not required. Yet under the classic
476 MIPS psABI, a zero value indicates that there is an address
477 reference to the function and the dynamic linker must resolve the
478 symbol immediately upon loading. To avoid conflict, symbols for
479 which the dynamic linker must assume the non-PIC ABI semantics are
480 marked with the STO_MIPS_PLT flag. */
481 #define ELF_MACHINE_SYM_NO_MATCH(sym) \
482 ((sym)->st_shndx == SHN_UNDEF && !((sym)->st_other & STO_MIPS_PLT))
484 #endif /* !dl_machine_h */
486 #ifdef RESOLVE_MAP
488 /* Perform a relocation described by R_INFO at the location pointed to
489 by RELOC_ADDR. SYM is the relocation symbol specified by R_INFO and
490 MAP is the object containing the reloc. */
492 auto inline void
493 __attribute__ ((always_inline))
494 elf_machine_reloc (struct link_map *map, ElfW(Addr) r_info,
495 const ElfW(Sym) *sym, const struct r_found_version *version,
496 void *reloc_addr, ElfW(Addr) r_addend, int inplace_p)
498 const unsigned long int r_type = ELFW(R_TYPE) (r_info);
499 ElfW(Addr) *addr_field = (ElfW(Addr) *) reloc_addr;
501 #if !defined RTLD_BOOTSTRAP && !defined SHARED
502 /* This is defined in rtld.c, but nowhere in the static libc.a;
503 make the reference weak so static programs can still link. This
504 declaration cannot be done when compiling rtld.c (i.e. #ifdef
505 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
506 _dl_rtld_map, which is incompatible with a weak decl in the same
507 file. */
508 weak_extern (GL(dl_rtld_map));
509 #endif
511 switch (r_type)
513 #if !defined (RTLD_BOOTSTRAP)
514 # if _MIPS_SIM == _ABI64
515 case R_MIPS_TLS_DTPMOD64:
516 case R_MIPS_TLS_DTPREL64:
517 case R_MIPS_TLS_TPREL64:
518 # else
519 case R_MIPS_TLS_DTPMOD32:
520 case R_MIPS_TLS_DTPREL32:
521 case R_MIPS_TLS_TPREL32:
522 # endif
524 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
526 switch (r_type)
528 case R_MIPS_TLS_DTPMOD64:
529 case R_MIPS_TLS_DTPMOD32:
530 if (sym_map)
531 *addr_field = sym_map->l_tls_modid;
532 break;
534 case R_MIPS_TLS_DTPREL64:
535 case R_MIPS_TLS_DTPREL32:
536 if (sym)
538 if (inplace_p)
539 r_addend = *addr_field;
540 *addr_field = r_addend + TLS_DTPREL_VALUE (sym);
542 break;
544 case R_MIPS_TLS_TPREL32:
545 case R_MIPS_TLS_TPREL64:
546 if (sym)
548 CHECK_STATIC_TLS (map, sym_map);
549 if (inplace_p)
550 r_addend = *addr_field;
551 *addr_field = r_addend + TLS_TPREL_VALUE (sym_map, sym);
553 break;
556 break;
558 #endif
560 #if _MIPS_SIM == _ABI64
561 case (R_MIPS_64 << 8) | R_MIPS_REL32:
562 #else
563 case R_MIPS_REL32:
564 #endif
566 int symidx = ELFW(R_SYM) (r_info);
567 ElfW(Addr) reloc_value;
569 if (inplace_p)
570 /* Support relocations on mis-aligned offsets. */
571 __builtin_memcpy (&reloc_value, reloc_addr, sizeof (reloc_value));
572 else
573 reloc_value = r_addend;
575 if (symidx)
577 const ElfW(Word) gotsym
578 = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
580 if ((ElfW(Word))symidx < gotsym)
582 /* This wouldn't work for a symbol imported from other
583 libraries for which there's no GOT entry, but MIPS
584 requires every symbol referenced in a dynamic
585 relocation to have a GOT entry in the primary GOT,
586 so we only get here for locally-defined symbols.
587 For section symbols, we should *NOT* be adding
588 sym->st_value (per the definition of the meaning of
589 S in reloc expressions in the ELF64 MIPS ABI),
590 since it should have already been added to
591 reloc_value by the linker, but older versions of
592 GNU ld didn't add it, and newer versions don't emit
593 useless relocations to section symbols any more, so
594 it is safe to keep on adding sym->st_value, even
595 though it's not ABI compliant. Some day we should
596 bite the bullet and stop doing this. */
597 #ifndef RTLD_BOOTSTRAP
598 if (map != &GL(dl_rtld_map))
599 #endif
600 reloc_value += sym->st_value + map->l_addr;
602 else
604 #ifndef RTLD_BOOTSTRAP
605 const ElfW(Addr) *got
606 = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
607 const ElfW(Word) local_gotno
608 = (const ElfW(Word))
609 map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
611 reloc_value += got[symidx + local_gotno - gotsym];
612 #endif
615 else
616 #ifndef RTLD_BOOTSTRAP
617 if (map != &GL(dl_rtld_map))
618 #endif
619 reloc_value += map->l_addr;
621 __builtin_memcpy (reloc_addr, &reloc_value, sizeof (reloc_value));
623 break;
624 #ifndef RTLD_BOOTSTRAP
625 #if _MIPS_SIM == _ABI64
626 case (R_MIPS_64 << 8) | R_MIPS_GLOB_DAT:
627 #else
628 case R_MIPS_GLOB_DAT:
629 #endif
631 int symidx = ELFW(R_SYM) (r_info);
632 const ElfW(Word) gotsym
633 = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
635 if (__builtin_expect ((ElfW(Word)) symidx >= gotsym, 1))
637 const ElfW(Addr) *got
638 = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
639 const ElfW(Word) local_gotno
640 = ((const ElfW(Word))
641 map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val);
643 ElfW(Addr) reloc_value = got[symidx + local_gotno - gotsym];
644 __builtin_memcpy (reloc_addr, &reloc_value, sizeof (reloc_value));
647 break;
648 #endif
649 case R_MIPS_NONE: /* Alright, Wilbur. */
650 break;
652 case R_MIPS_JUMP_SLOT:
654 struct link_map *sym_map;
655 ElfW(Addr) value;
657 /* The addend for a jump slot relocation must always be zero:
658 calls via the PLT always branch to the symbol's address and
659 not to the address plus a non-zero offset. */
660 if (r_addend != 0)
661 _dl_signal_error (0, map->l_name, NULL,
662 "found jump slot relocation with non-zero addend");
664 sym_map = RESOLVE_MAP (&sym, version, r_type);
665 value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
666 *addr_field = value;
668 break;
671 case R_MIPS_COPY:
673 const ElfW(Sym) *const refsym = sym;
674 struct link_map *sym_map;
675 ElfW(Addr) value;
677 /* Calculate the address of the symbol. */
678 sym_map = RESOLVE_MAP (&sym, version, r_type);
679 value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
681 if (__builtin_expect (sym == NULL, 0))
682 /* This can happen in trace mode if an object could not be
683 found. */
684 break;
685 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
686 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
687 && GLRO(dl_verbose)))
689 const char *strtab;
691 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
692 _dl_error_printf ("\
693 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
694 RTLD_PROGNAME, strtab + refsym->st_name);
696 memcpy (reloc_addr, (void *) value,
697 MIN (sym->st_size, refsym->st_size));
698 break;
701 #if _MIPS_SIM == _ABI64
702 case R_MIPS_64:
703 /* For full compliance with the ELF64 ABI, one must precede the
704 _REL32/_64 pair of relocations with a _64 relocation, such
705 that the in-place addend is read as a 64-bit value. IRIX
706 didn't pick up on this requirement, so we treat the
707 _REL32/_64 relocation as a 64-bit relocation even if it's by
708 itself. For ABI compliance, we ignore such _64 dummy
709 relocations. For RELA, this may be simply removed, since
710 it's totally unnecessary. */
711 if (ELFW(R_SYM) (r_info) == 0)
712 break;
713 /* Fall through. */
714 #endif
715 default:
716 _dl_reloc_bad_type (map, r_type, 0);
717 break;
721 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
722 MAP is the object containing the reloc. */
724 auto inline void
725 __attribute__ ((always_inline))
726 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
727 const ElfW(Sym) *sym, const struct r_found_version *version,
728 void *const reloc_addr, int skip_ifunc)
730 elf_machine_reloc (map, reloc->r_info, sym, version, reloc_addr, 0, 1);
733 auto inline void
734 __attribute__((always_inline))
735 elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
736 void *const reloc_addr)
738 /* XXX Nothing to do. There is no relative relocation, right? */
741 auto inline void
742 __attribute__((always_inline))
743 elf_machine_lazy_rel (struct link_map *map,
744 ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
745 int skip_ifunc)
747 ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
748 const unsigned int r_type = ELFW(R_TYPE) (reloc->r_info);
749 /* Check for unexpected PLT reloc type. */
750 if (__builtin_expect (r_type == R_MIPS_JUMP_SLOT, 1))
752 if (__builtin_expect (map->l_mach.plt, 0) == 0)
754 /* Nothing is required here since we only support lazy
755 relocation in executables. */
757 else
758 *reloc_addr = map->l_mach.plt;
760 else
761 _dl_reloc_bad_type (map, r_type, 1);
764 auto inline void
765 __attribute__ ((always_inline))
766 elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
767 const ElfW(Sym) *sym, const struct r_found_version *version,
768 void *const reloc_addr, int skip_ifunc)
770 elf_machine_reloc (map, reloc->r_info, sym, version, reloc_addr,
771 reloc->r_addend, 0);
774 auto inline void
775 __attribute__((always_inline))
776 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
777 void *const reloc_addr)
781 #ifndef RTLD_BOOTSTRAP
782 /* Relocate GOT. */
783 auto inline void
784 __attribute__((always_inline))
785 elf_machine_got_rel (struct link_map *map, int lazy)
787 ElfW(Addr) *got;
788 ElfW(Sym) *sym;
789 const ElfW(Half) *vernum;
790 int i, n, symidx;
792 #define RESOLVE_GOTSYM(sym,vernum,sym_index,reloc) \
793 ({ \
794 const ElfW(Sym) *ref = sym; \
795 const struct r_found_version *version __attribute__ ((unused)) \
796 = vernum ? &map->l_versions[vernum[sym_index] & 0x7fff] : NULL; \
797 struct link_map *sym_map; \
798 sym_map = RESOLVE_MAP (&ref, version, reloc); \
799 ref ? sym_map->l_addr + ref->st_value : 0; \
802 if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
803 vernum = (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
804 else
805 vernum = NULL;
807 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
809 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
810 /* The dynamic linker's local got entries have already been relocated. */
811 if (map != &GL(dl_rtld_map))
813 /* got[0] is reserved. got[1] is also reserved for the dynamic object
814 generated by gnu ld. Skip these reserved entries from relocation. */
815 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1;
817 /* Add the run-time displacement to all local got entries if
818 needed. */
819 if (__builtin_expect (map->l_addr != 0, 0))
821 while (i < n)
822 got[i++] += map->l_addr;
826 /* Handle global got entries. */
827 got += n;
828 /* Keep track of the symbol index. */
829 symidx = map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
830 sym = (ElfW(Sym) *) D_PTR (map, l_info[DT_SYMTAB]) + symidx;
831 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
832 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
834 /* This loop doesn't handle Quickstart. */
835 while (i--)
837 if (sym->st_shndx == SHN_UNDEF)
839 if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC && sym->st_value
840 && !(sym->st_other & STO_MIPS_PLT))
842 if (lazy)
843 *got = sym->st_value + map->l_addr;
844 else
845 /* This is a lazy-binding stub, so we don't need the
846 canonical address. */
847 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_JUMP_SLOT);
849 else
850 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
852 else if (sym->st_shndx == SHN_COMMON)
853 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
854 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
855 && *got != sym->st_value)
857 if (lazy)
858 *got += map->l_addr;
859 else
860 /* This is a lazy-binding stub, so we don't need the
861 canonical address. */
862 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_JUMP_SLOT);
864 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)
866 if (sym->st_other == 0)
867 *got += map->l_addr;
869 else
870 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
872 ++got;
873 ++sym;
874 ++symidx;
877 #undef RESOLVE_GOTSYM
879 #endif
881 /* Set up the loaded object described by L so its stub function
882 will jump to the on-demand fixup code __dl_runtime_resolve. */
884 auto inline int
885 __attribute__((always_inline))
886 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
888 # ifndef RTLD_BOOTSTRAP
889 ElfW(Addr) *got;
890 extern void _dl_runtime_resolve (ElfW(Word));
891 extern void _dl_runtime_pltresolve (void);
892 extern int _dl_mips_gnu_objects;
894 if (lazy)
896 /* The GOT entries for functions have not yet been filled in.
897 Their initial contents will arrange when called to put an
898 offset into the .dynsym section in t8, the return address
899 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
900 got = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
902 /* This function will get called to fix up the GOT entry indicated by
903 the register t8, and then jump to the resolved address. */
904 got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
906 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
907 of got[1] of a gnu object is set to identify gnu objects.
908 Where we can store l for non gnu objects? XXX */
909 if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
910 got[1] = ((ElfW(Addr)) l | ELF_MIPS_GNU_GOT1_MASK);
911 else
912 _dl_mips_gnu_objects = 0;
915 /* Relocate global offset table. */
916 elf_machine_got_rel (l, lazy);
918 /* If using PLTs, fill in the first two entries of .got.plt. */
919 if (l->l_info[DT_JMPREL] && lazy)
921 ElfW(Addr) *gotplt;
922 gotplt = (ElfW(Addr) *) D_PTR (l, l_info[DT_MIPS (PLTGOT)]);
923 /* If a library is prelinked but we have to relocate anyway,
924 we have to be able to undo the prelinking of .got.plt.
925 The prelinker saved the address of .plt for us here. */
926 if (gotplt[1])
927 l->l_mach.plt = gotplt[1] + l->l_addr;
928 gotplt[0] = (ElfW(Addr)) &_dl_runtime_pltresolve;
929 gotplt[1] = (ElfW(Addr)) l;
932 # endif
933 return lazy;
936 #endif /* RESOLVE_MAP */