Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / mips / dl-machine.h
blobc2dd915e8b31393d70de19680559c63254f908e9
1 /* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
2 Copyright (C) 1996-2014 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 <sys/asm.h>
34 #include <dl-tls.h>
36 /* The offset of gp from GOT might be system-dependent. It's set by
37 ld. The same value is also */
38 #define OFFSET_GP_GOT 0x7ff0
40 #ifndef _RTLD_PROLOGUE
41 # define _RTLD_PROLOGUE(entry) \
42 ".globl\t" __STRING(entry) "\n\t" \
43 ".ent\t" __STRING(entry) "\n\t" \
44 ".type\t" __STRING(entry) ", @function\n" \
45 __STRING(entry) ":\n\t"
46 #endif
48 #ifndef _RTLD_EPILOGUE
49 # define _RTLD_EPILOGUE(entry) \
50 ".end\t" __STRING(entry) "\n\t" \
51 ".size\t" __STRING(entry) ", . - " __STRING(entry) "\n\t"
52 #endif
54 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.
55 This only makes sense on MIPS when using PLTs, so choose the
56 PLT relocation (not encountered when not using PLTs). */
57 #define ELF_MACHINE_JMP_SLOT R_MIPS_JUMP_SLOT
58 #define elf_machine_type_class(type) \
59 ((((type) == ELF_MACHINE_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
60 | (((type) == R_MIPS_COPY) * ELF_RTYPE_CLASS_COPY))
62 #define ELF_MACHINE_PLT_REL 1
64 /* Translate a processor specific dynamic tag to the index
65 in l_info array. */
66 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
68 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
69 with the run-time address of the r_debug structure */
70 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
71 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
72 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
73 (ElfW(Addr)) (r); \
74 } while (0)
76 #if ((defined __mips_nan2008 && !defined HAVE_MIPS_NAN2008) \
77 || (!defined __mips_nan2008 && defined HAVE_MIPS_NAN2008))
78 # error "Configuration inconsistency: __mips_nan2008 != HAVE_MIPS_NAN2008, overridden CFLAGS?"
79 #endif
80 #ifdef __mips_nan2008
81 # define ELF_MACHINE_NAN2008 EF_MIPS_NAN2008
82 #else
83 # define ELF_MACHINE_NAN2008 0
84 #endif
86 /* Return nonzero iff ELF header is compatible with the running host. */
87 static inline int __attribute_used__
88 elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
90 #if _MIPS_SIM == _ABIO32 || _MIPS_SIM == _ABIN32
91 /* Don't link o32 and n32 together. */
92 if (((ehdr->e_flags & EF_MIPS_ABI2) != 0) != (_MIPS_SIM == _ABIN32))
93 return 0;
94 #endif
96 /* Don't link 2008-NaN and legacy-NaN objects together. */
97 if ((ehdr->e_flags & EF_MIPS_NAN2008) != ELF_MACHINE_NAN2008)
98 return 0;
100 switch (ehdr->e_machine)
102 case EM_MIPS:
103 case EM_MIPS_RS3_LE:
104 return 1;
105 default:
106 return 0;
110 static inline ElfW(Addr) *
111 elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
113 /* FIXME: the offset of gp from GOT may be system-dependent. */
114 return (ElfW(Addr) *) (gpreg - OFFSET_GP_GOT);
117 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
118 first element of the GOT. This must be inlined in a function which
119 uses global data. We assume its $gp points to the primary GOT. */
120 static inline ElfW(Addr)
121 elf_machine_dynamic (void)
123 register ElfW(Addr) gp __asm__ ("$28");
124 return *elf_mips_got_from_gpreg (gp);
127 #define STRINGXP(X) __STRING(X)
128 #define STRINGXV(X) STRINGV_(X)
129 #define STRINGV_(...) # __VA_ARGS__
131 /* Return the run-time load address of the shared object. */
132 static inline ElfW(Addr)
133 elf_machine_load_address (void)
135 ElfW(Addr) addr;
136 #ifndef __mips16
137 asm (" .set noreorder\n"
138 " " STRINGXP (PTR_LA) " %0, 0f\n"
139 " bltzal $0, 0f\n"
140 " nop\n"
141 "0: " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
142 " .set reorder\n"
143 : "=r" (addr)
144 : /* No inputs */
145 : "$31");
146 #else
147 ElfW(Addr) tmp;
148 asm (" .set noreorder\n"
149 " move %1,$gp\n"
150 " lw %1,%%got(0f)(%1)\n"
151 "0: .fill 0\n" /* Clear the ISA bit on 0:. */
152 " la %0,0b\n"
153 " addiu %1,%%lo(0b)\n"
154 " subu %0,%1\n"
155 " .set reorder\n"
156 : "=d" (addr), "=d" (tmp)
157 : /* No inputs */);
158 #endif
159 return addr;
162 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
163 #if _MIPS_SIM == _ABI64
164 # define ELF_MIPS_GNU_GOT1_MASK 0x8000000000000000L
165 #else
166 # define ELF_MIPS_GNU_GOT1_MASK 0x80000000L
167 #endif
169 /* We can't rely on elf_machine_got_rel because _dl_object_relocation_scope
170 fiddles with global data. */
171 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
172 do { \
173 struct link_map *map = &bootstrap_map; \
174 ElfW(Sym) *sym; \
175 ElfW(Addr) *got; \
176 int i, n; \
178 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]); \
180 if (__builtin_expect (map->l_addr == 0, 1)) \
181 break; \
183 /* got[0] is reserved. got[1] is also reserved for the dynamic object \
184 generated by gnu ld. Skip these reserved entries from \
185 relocation. */ \
186 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1; \
187 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; \
189 /* Add the run-time displacement to all local got entries. */ \
190 while (i < n) \
191 got[i++] += map->l_addr; \
193 /* Handle global got entries. */ \
194 got += n; \
195 sym = (ElfW(Sym) *) D_PTR(map, l_info[DT_SYMTAB]) \
196 + map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val; \
197 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val \
198 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val); \
200 while (i--) \
202 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx == SHN_COMMON) \
203 *got = map->l_addr + sym->st_value; \
204 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC \
205 && *got != sym->st_value) \
206 *got += map->l_addr; \
207 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION) \
209 if (sym->st_other == 0) \
210 *got += map->l_addr; \
212 else \
213 *got = map->l_addr + sym->st_value; \
215 got++; \
216 sym++; \
218 } while(0)
221 /* Mask identifying addresses reserved for the user program,
222 where the dynamic linker should not map anything. */
223 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
226 /* Initial entry point code for the dynamic linker.
227 The C function `_dl_start' is the real entry point;
228 its return value is the user program's entry point.
229 Note how we have to be careful about two things:
231 1) That we allocate a minimal stack of 24 bytes for
232 every function call, the MIPS ABI states that even
233 if all arguments are passed in registers the procedure
234 called can use the 16 byte area pointed to by $sp
235 when it is called to store away the arguments passed
236 to it.
238 2) That under Unix the entry is named __start
239 and not just plain _start. */
241 #ifndef __mips16
242 # define RTLD_START asm (\
243 ".text\n\
244 " _RTLD_PROLOGUE(ENTRY_POINT) "\
245 " STRINGXV(SETUP_GPX($25)) "\n\
246 " STRINGXV(SETUP_GPX64($18,$25)) "\n\
247 # i386 ABI book says that the first entry of GOT holds\n\
248 # the address of the dynamic structure. Though MIPS ABI\n\
249 # doesn't say nothing about this, I emulate this here.\n\
250 " STRINGXP(PTR_LA) " $4, _DYNAMIC\n\
251 # Subtract OFFSET_GP_GOT\n\
252 " STRINGXP(PTR_S) " $4, -0x7ff0($28)\n\
253 move $4, $29\n\
254 " STRINGXP(PTR_SUBIU) " $29, 16\n\
256 " STRINGXP(PTR_LA) " $8, .Lcoff\n\
257 bltzal $8, .Lcoff\n\
258 .Lcoff: " STRINGXP(PTR_SUBU) " $8, $31, $8\n\
260 " STRINGXP(PTR_LA) " $25, _dl_start\n\
261 " STRINGXP(PTR_ADDU) " $25, $8\n\
262 jalr $25\n\
264 " STRINGXP(PTR_ADDIU) " $29, 16\n\
265 # Get the value of label '_dl_start_user' in t9 ($25).\n\
266 " STRINGXP(PTR_LA) " $25, _dl_start_user\n\
267 " _RTLD_EPILOGUE(ENTRY_POINT) "\
270 " _RTLD_PROLOGUE(_dl_start_user) "\
271 " STRINGXP(SETUP_GP) "\n\
272 " STRINGXV(SETUP_GP64($18,_dl_start_user)) "\n\
273 move $16, $28\n\
274 # Save the user entry point address in a saved register.\n\
275 move $17, $2\n\
276 # See if we were run as a command with the executable file\n\
277 # name as an extra leading argument.\n\
278 lw $2, _dl_skip_args\n\
279 beq $2, $0, 1f\n\
280 # Load the original argument count.\n\
281 " STRINGXP(PTR_L) " $4, 0($29)\n\
282 # Subtract _dl_skip_args from it.\n\
283 subu $4, $2\n\
284 # Adjust the stack pointer to skip _dl_skip_args words.\n\
285 sll $2, " STRINGXP (PTRLOG) "\n\
286 " STRINGXP(PTR_ADDU) " $29, $2\n\
287 # Save back the modified argument count.\n\
288 " STRINGXP(PTR_S) " $4, 0($29)\n\
289 1: # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
290 " STRINGXP(PTR_L) " $4, _rtld_local\n\
291 " STRINGXP(PTR_L) /* or lw??? fixme */ " $5, 0($29)\n\
292 " STRINGXP(PTR_LA) " $6, " STRINGXP (PTRSIZE) "($29)\n\
293 sll $7, $5, " STRINGXP (PTRLOG) "\n\
294 " STRINGXP(PTR_ADDU) " $7, $7, $6\n\
295 " STRINGXP(PTR_ADDU) " $7, $7, " STRINGXP (PTRSIZE) " \n\
296 # Make sure the stack pointer is aligned for _dl_init_internal.\n\
297 and $2, $29, -2 * " STRINGXP(SZREG) "\n\
298 move $8, $29\n\
299 " STRINGXP(PTR_SUBIU) " $29, $2, 32\n\
300 " STRINGXP(PTR_S) " $8, (32 - " STRINGXP(SZREG) ")($29)\n\
301 " STRINGXP(SAVE_GP(16)) "\n\
302 # Call the function to run the initializers.\n\
303 jal _dl_init_internal\n\
304 # Restore the stack pointer for _start.\n\
305 " STRINGXP(PTR_L) " $29, (32 - " STRINGXP(SZREG) ")($29)\n\
306 # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
307 " STRINGXP(PTR_LA) " $2, _dl_fini\n\
308 # Jump to the user entry point.\n\
309 move $25, $17\n\
310 jr $25\n\t"\
311 _RTLD_EPILOGUE(_dl_start_user)\
312 ".previous"\
315 #else /* __mips16 */
316 /* MIPS16 version. We currently only support O32 under MIPS16; the proper
317 assembly preprocessor abstractions will need to be added if other ABIs
318 are to be supported. */
320 # define RTLD_START asm (\
321 ".text\n\
322 .set mips16\n\
323 " _RTLD_PROLOGUE (ENTRY_POINT) "\
324 # Construct GP value in $3.\n\
325 li $3, %hi(_gp_disp)\n\
326 addiu $4, $pc, %lo(_gp_disp)\n\
327 sll $3, 16\n\
328 addu $3, $4\n\
329 move $28, $3\n\
330 lw $4, %got(_DYNAMIC)($3)\n\
331 sw $4, -0x7ff0($3)\n\
332 move $4, $sp\n\
333 addiu $sp, -16\n\
334 # _dl_start() is sufficiently near to use pc-relative\n\
335 # load address.\n\
336 la $3, _dl_start\n\
337 move $25, $3\n\
338 jalr $3\n\
339 addiu $sp, 16\n\
340 " _RTLD_EPILOGUE (ENTRY_POINT) "\
343 " _RTLD_PROLOGUE (_dl_start_user) "\
344 li $16, %hi(_gp_disp)\n\
345 addiu $4, $pc, %lo(_gp_disp)\n\
346 sll $16, 16\n\
347 addu $16, $4\n\
348 move $17, $2\n\
349 move $28, $16\n\
350 lw $4, %got(_dl_skip_args)($16)\n\
351 lw $4, 0($4)\n\
352 beqz $4, 1f\n\
353 # Load the original argument count.\n\
354 lw $5, 0($sp)\n\
355 # Subtract _dl_skip_args from it.\n\
356 subu $5, $4\n\
357 # Adjust the stack pointer to skip _dl_skip_args words.\n\
358 sll $4, " STRINGXP (PTRLOG) "\n\
359 move $6, $sp\n\
360 addu $6, $4\n\
361 move $sp, $6\n\
362 # Save back the modified argument count.\n\
363 sw $5, 0($sp)\n\
364 1: # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
365 lw $4, %got(_rtld_local)($16)\n\
366 lw $4, 0($4)\n\
367 lw $5, 0($sp)\n\
368 addiu $6, $sp, " STRINGXP (PTRSIZE) "\n\
369 sll $7, $5, " STRINGXP (PTRLOG) "\n\
370 addu $7, $6\n\
371 addu $7, " STRINGXP (PTRSIZE) "\n\
372 # Make sure the stack pointer is aligned for _dl_init_internal.\n\
373 li $2, 2 * " STRINGXP (SZREG) "\n\
374 neg $2, $2\n\
375 move $3, $sp\n\
376 and $2, $3\n\
377 sw $3, -" STRINGXP (SZREG) "($2)\n\
378 addiu $2, -32\n\
379 move $sp, $2\n\
380 sw $16, 16($sp)\n\
381 # Call the function to run the initializers.\n\
382 lw $2, %call16(_dl_init_internal)($16)\n\
383 move $25, $2\n\
384 jalr $2\n\
385 # Restore the stack pointer for _start.\n\
386 lw $2, 32-" STRINGXP (SZREG) "($sp)\n\
387 move $sp, $2\n\
388 move $28, $16\n\
389 # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
390 lw $2, %call16(_dl_fini)($16)\n\
391 # Jump to the user entry point.\n\
392 move $25, $17\n\
393 jr $17\n\t"\
394 _RTLD_EPILOGUE (_dl_start_user)\
395 ".previous"\
398 #endif /* __mips16 */
400 /* Names of the architecture-specific auditing callback functions. */
401 # if _MIPS_SIM == _ABIO32
402 # define ARCH_LA_PLTENTER mips_o32_gnu_pltenter
403 # define ARCH_LA_PLTEXIT mips_o32_gnu_pltexit
404 # elif _MIPS_SIM == _ABIN32
405 # define ARCH_LA_PLTENTER mips_n32_gnu_pltenter
406 # define ARCH_LA_PLTEXIT mips_n32_gnu_pltexit
407 # else
408 # define ARCH_LA_PLTENTER mips_n64_gnu_pltenter
409 # define ARCH_LA_PLTEXIT mips_n64_gnu_pltexit
410 # endif
412 /* We define an initialization function. This is called very early in
413 _dl_sysdep_start. */
414 #define DL_PLATFORM_INIT dl_platform_init ()
416 static inline void __attribute__ ((unused))
417 dl_platform_init (void)
419 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
420 /* Avoid an empty string which would disturb us. */
421 GLRO(dl_platform) = NULL;
424 /* For a non-writable PLT, rewrite the .got.plt entry at RELOC_ADDR to
425 point at the symbol with address VALUE. For a writable PLT, rewrite
426 the corresponding PLT entry instead. */
427 static inline ElfW(Addr)
428 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
429 const ElfW(Rel) *reloc,
430 ElfW(Addr) *reloc_addr, ElfW(Addr) value)
432 return *reloc_addr = value;
435 static inline ElfW(Addr)
436 elf_machine_plt_value (struct link_map *map, const ElfW(Rel) *reloc,
437 ElfW(Addr) value)
439 return value;
442 #endif /* !dl_machine_h */
444 #ifdef RESOLVE_MAP
446 /* Perform a relocation described by R_INFO at the location pointed to
447 by RELOC_ADDR. SYM is the relocation symbol specified by R_INFO and
448 MAP is the object containing the reloc. */
450 auto inline void
451 __attribute__ ((always_inline))
452 elf_machine_reloc (struct link_map *map, ElfW(Addr) r_info,
453 const ElfW(Sym) *sym, const struct r_found_version *version,
454 void *reloc_addr, ElfW(Addr) r_addend, int inplace_p)
456 const unsigned long int r_type = ELFW(R_TYPE) (r_info);
457 ElfW(Addr) *addr_field = (ElfW(Addr) *) reloc_addr;
459 #if !defined RTLD_BOOTSTRAP && !defined SHARED
460 /* This is defined in rtld.c, but nowhere in the static libc.a;
461 make the reference weak so static programs can still link. This
462 declaration cannot be done when compiling rtld.c (i.e. #ifdef
463 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
464 _dl_rtld_map, which is incompatible with a weak decl in the same
465 file. */
466 weak_extern (GL(dl_rtld_map));
467 #endif
469 switch (r_type)
471 #if !defined (RTLD_BOOTSTRAP)
472 # if _MIPS_SIM == _ABI64
473 case R_MIPS_TLS_DTPMOD64:
474 case R_MIPS_TLS_DTPREL64:
475 case R_MIPS_TLS_TPREL64:
476 # else
477 case R_MIPS_TLS_DTPMOD32:
478 case R_MIPS_TLS_DTPREL32:
479 case R_MIPS_TLS_TPREL32:
480 # endif
482 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
484 switch (r_type)
486 case R_MIPS_TLS_DTPMOD64:
487 case R_MIPS_TLS_DTPMOD32:
488 if (sym_map)
489 *addr_field = sym_map->l_tls_modid;
490 break;
492 case R_MIPS_TLS_DTPREL64:
493 case R_MIPS_TLS_DTPREL32:
494 if (sym)
496 if (inplace_p)
497 r_addend = *addr_field;
498 *addr_field = r_addend + TLS_DTPREL_VALUE (sym);
500 break;
502 case R_MIPS_TLS_TPREL32:
503 case R_MIPS_TLS_TPREL64:
504 if (sym)
506 CHECK_STATIC_TLS (map, sym_map);
507 if (inplace_p)
508 r_addend = *addr_field;
509 *addr_field = r_addend + TLS_TPREL_VALUE (sym_map, sym);
511 break;
514 break;
516 #endif
518 #if _MIPS_SIM == _ABI64
519 case (R_MIPS_64 << 8) | R_MIPS_REL32:
520 #else
521 case R_MIPS_REL32:
522 #endif
524 int symidx = ELFW(R_SYM) (r_info);
525 ElfW(Addr) reloc_value;
527 if (inplace_p)
528 /* Support relocations on mis-aligned offsets. */
529 __builtin_memcpy (&reloc_value, reloc_addr, sizeof (reloc_value));
530 else
531 reloc_value = r_addend;
533 if (symidx)
535 const ElfW(Word) gotsym
536 = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
538 if ((ElfW(Word))symidx < gotsym)
540 /* This wouldn't work for a symbol imported from other
541 libraries for which there's no GOT entry, but MIPS
542 requires every symbol referenced in a dynamic
543 relocation to have a GOT entry in the primary GOT,
544 so we only get here for locally-defined symbols.
545 For section symbols, we should *NOT* be adding
546 sym->st_value (per the definition of the meaning of
547 S in reloc expressions in the ELF64 MIPS ABI),
548 since it should have already been added to
549 reloc_value by the linker, but older versions of
550 GNU ld didn't add it, and newer versions don't emit
551 useless relocations to section symbols any more, so
552 it is safe to keep on adding sym->st_value, even
553 though it's not ABI compliant. Some day we should
554 bite the bullet and stop doing this. */
555 #ifndef RTLD_BOOTSTRAP
556 if (map != &GL(dl_rtld_map))
557 #endif
558 reloc_value += sym->st_value + map->l_addr;
560 else
562 #ifndef RTLD_BOOTSTRAP
563 const ElfW(Addr) *got
564 = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
565 const ElfW(Word) local_gotno
566 = (const ElfW(Word))
567 map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
569 reloc_value += got[symidx + local_gotno - gotsym];
570 #endif
573 else
574 #ifndef RTLD_BOOTSTRAP
575 if (map != &GL(dl_rtld_map))
576 #endif
577 reloc_value += map->l_addr;
579 __builtin_memcpy (reloc_addr, &reloc_value, sizeof (reloc_value));
581 break;
582 #ifndef RTLD_BOOTSTRAP
583 #if _MIPS_SIM == _ABI64
584 case (R_MIPS_64 << 8) | R_MIPS_GLOB_DAT:
585 #else
586 case R_MIPS_GLOB_DAT:
587 #endif
589 int symidx = ELFW(R_SYM) (r_info);
590 const ElfW(Word) gotsym
591 = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
593 if (__builtin_expect ((ElfW(Word)) symidx >= gotsym, 1))
595 const ElfW(Addr) *got
596 = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
597 const ElfW(Word) local_gotno
598 = ((const ElfW(Word))
599 map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val);
601 ElfW(Addr) reloc_value = got[symidx + local_gotno - gotsym];
602 __builtin_memcpy (reloc_addr, &reloc_value, sizeof (reloc_value));
605 break;
606 #endif
607 case R_MIPS_NONE: /* Alright, Wilbur. */
608 break;
610 case R_MIPS_JUMP_SLOT:
612 struct link_map *sym_map;
613 ElfW(Addr) value;
615 /* The addend for a jump slot relocation must always be zero:
616 calls via the PLT always branch to the symbol's address and
617 not to the address plus a non-zero offset. */
618 if (r_addend != 0)
619 _dl_signal_error (0, map->l_name, NULL,
620 "found jump slot relocation with non-zero addend");
622 sym_map = RESOLVE_MAP (&sym, version, r_type);
623 value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
624 *addr_field = value;
626 break;
629 case R_MIPS_COPY:
631 const ElfW(Sym) *const refsym = sym;
632 struct link_map *sym_map;
633 ElfW(Addr) value;
635 /* Calculate the address of the symbol. */
636 sym_map = RESOLVE_MAP (&sym, version, r_type);
637 value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
639 if (__builtin_expect (sym == NULL, 0))
640 /* This can happen in trace mode if an object could not be
641 found. */
642 break;
643 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
644 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
645 && GLRO(dl_verbose)))
647 const char *strtab;
649 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
650 _dl_error_printf ("\
651 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
652 RTLD_PROGNAME, strtab + refsym->st_name);
654 memcpy (reloc_addr, (void *) value,
655 MIN (sym->st_size, refsym->st_size));
656 break;
659 #if _MIPS_SIM == _ABI64
660 case R_MIPS_64:
661 /* For full compliance with the ELF64 ABI, one must precede the
662 _REL32/_64 pair of relocations with a _64 relocation, such
663 that the in-place addend is read as a 64-bit value. IRIX
664 didn't pick up on this requirement, so we treat the
665 _REL32/_64 relocation as a 64-bit relocation even if it's by
666 itself. For ABI compliance, we ignore such _64 dummy
667 relocations. For RELA, this may be simply removed, since
668 it's totally unnecessary. */
669 if (ELFW(R_SYM) (r_info) == 0)
670 break;
671 /* Fall through. */
672 #endif
673 default:
674 _dl_reloc_bad_type (map, r_type, 0);
675 break;
679 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
680 MAP is the object containing the reloc. */
682 auto inline void
683 __attribute__ ((always_inline))
684 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
685 const ElfW(Sym) *sym, const struct r_found_version *version,
686 void *const reloc_addr, int skip_ifunc)
688 elf_machine_reloc (map, reloc->r_info, sym, version, reloc_addr, 0, 1);
691 auto inline void
692 __attribute__((always_inline))
693 elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
694 void *const reloc_addr)
696 /* XXX Nothing to do. There is no relative relocation, right? */
699 auto inline void
700 __attribute__((always_inline))
701 elf_machine_lazy_rel (struct link_map *map,
702 ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
703 int skip_ifunc)
705 ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
706 const unsigned int r_type = ELFW(R_TYPE) (reloc->r_info);
707 /* Check for unexpected PLT reloc type. */
708 if (__builtin_expect (r_type == R_MIPS_JUMP_SLOT, 1))
710 if (__builtin_expect (map->l_mach.plt, 0) == 0)
712 /* Nothing is required here since we only support lazy
713 relocation in executables. */
715 else
716 *reloc_addr = map->l_mach.plt;
718 else
719 _dl_reloc_bad_type (map, r_type, 1);
722 auto inline void
723 __attribute__ ((always_inline))
724 elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
725 const ElfW(Sym) *sym, const struct r_found_version *version,
726 void *const reloc_addr, int skip_ifunc)
728 elf_machine_reloc (map, reloc->r_info, sym, version, reloc_addr,
729 reloc->r_addend, 0);
732 auto inline void
733 __attribute__((always_inline))
734 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
735 void *const reloc_addr)
739 #ifndef RTLD_BOOTSTRAP
740 /* Relocate GOT. */
741 auto inline void
742 __attribute__((always_inline))
743 elf_machine_got_rel (struct link_map *map, int lazy)
745 ElfW(Addr) *got;
746 ElfW(Sym) *sym;
747 const ElfW(Half) *vernum;
748 int i, n, symidx;
750 #define RESOLVE_GOTSYM(sym,vernum,sym_index,reloc) \
751 ({ \
752 const ElfW(Sym) *ref = sym; \
753 const struct r_found_version *version __attribute__ ((unused)) \
754 = vernum ? &map->l_versions[vernum[sym_index] & 0x7fff] : NULL; \
755 struct link_map *sym_map; \
756 sym_map = RESOLVE_MAP (&ref, version, reloc); \
757 ref ? sym_map->l_addr + ref->st_value : 0; \
760 if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
761 vernum = (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
762 else
763 vernum = NULL;
765 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
767 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
768 /* The dynamic linker's local got entries have already been relocated. */
769 if (map != &GL(dl_rtld_map))
771 /* got[0] is reserved. got[1] is also reserved for the dynamic object
772 generated by gnu ld. Skip these reserved entries from relocation. */
773 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1;
775 /* Add the run-time displacement to all local got entries if
776 needed. */
777 if (__builtin_expect (map->l_addr != 0, 0))
779 while (i < n)
780 got[i++] += map->l_addr;
784 /* Handle global got entries. */
785 got += n;
786 /* Keep track of the symbol index. */
787 symidx = map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
788 sym = (ElfW(Sym) *) D_PTR (map, l_info[DT_SYMTAB]) + symidx;
789 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
790 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
792 /* This loop doesn't handle Quickstart. */
793 while (i--)
795 if (sym->st_shndx == SHN_UNDEF)
797 if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC && sym->st_value
798 && !(sym->st_other & STO_MIPS_PLT))
800 if (lazy)
801 *got = sym->st_value + map->l_addr;
802 else
803 /* This is a lazy-binding stub, so we don't need the
804 canonical address. */
805 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_JUMP_SLOT);
807 else
808 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
810 else if (sym->st_shndx == SHN_COMMON)
811 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
812 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
813 && *got != sym->st_value)
815 if (lazy)
816 *got += map->l_addr;
817 else
818 /* This is a lazy-binding stub, so we don't need the
819 canonical address. */
820 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_JUMP_SLOT);
822 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)
824 if (sym->st_other == 0)
825 *got += map->l_addr;
827 else
828 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
830 ++got;
831 ++sym;
832 ++symidx;
835 #undef RESOLVE_GOTSYM
837 #endif
839 /* Set up the loaded object described by L so its stub function
840 will jump to the on-demand fixup code __dl_runtime_resolve. */
842 auto inline int
843 __attribute__((always_inline))
844 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
846 # ifndef RTLD_BOOTSTRAP
847 ElfW(Addr) *got;
848 extern void _dl_runtime_resolve (ElfW(Word));
849 extern void _dl_runtime_pltresolve (void);
850 extern int _dl_mips_gnu_objects;
852 if (lazy)
854 /* The GOT entries for functions have not yet been filled in.
855 Their initial contents will arrange when called to put an
856 offset into the .dynsym section in t8, the return address
857 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
858 got = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
860 /* This function will get called to fix up the GOT entry indicated by
861 the register t8, and then jump to the resolved address. */
862 got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
864 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
865 of got[1] of a gnu object is set to identify gnu objects.
866 Where we can store l for non gnu objects? XXX */
867 if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
868 got[1] = ((ElfW(Addr)) l | ELF_MIPS_GNU_GOT1_MASK);
869 else
870 _dl_mips_gnu_objects = 0;
873 /* Relocate global offset table. */
874 elf_machine_got_rel (l, lazy);
876 /* If using PLTs, fill in the first two entries of .got.plt. */
877 if (l->l_info[DT_JMPREL] && lazy)
879 ElfW(Addr) *gotplt;
880 gotplt = (ElfW(Addr) *) D_PTR (l, l_info[DT_MIPS (PLTGOT)]);
881 /* If a library is prelinked but we have to relocate anyway,
882 we have to be able to undo the prelinking of .got.plt.
883 The prelinker saved the address of .plt for us here. */
884 if (gotplt[1])
885 l->l_mach.plt = gotplt[1] + l->l_addr;
886 gotplt[0] = (ElfW(Addr)) &_dl_runtime_pltresolve;
887 gotplt[1] = (ElfW(Addr)) l;
890 # endif
891 return lazy;
894 #endif /* RESOLVE_MAP */