Fix grp.h endgrent, getgrent namespace (bug 18528).
[glibc.git] / sysdeps / mips / dl-machine.h
blobd63238a4fed3abee828c13a5e8a2b405d9e24de8
1 /* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
2 Copyright (C) 1996-2015 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 entry in the dynamic section, fill it in
72 with the run-time address of the r_debug structure */
73 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
74 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
75 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
76 (ElfW(Addr)) (r); \
77 } while (0)
79 #if ((defined __mips_nan2008 && !defined HAVE_MIPS_NAN2008) \
80 || (!defined __mips_nan2008 && defined HAVE_MIPS_NAN2008))
81 # error "Configuration inconsistency: __mips_nan2008 != HAVE_MIPS_NAN2008, overridden CFLAGS?"
82 #endif
83 #ifdef __mips_nan2008
84 # define ELF_MACHINE_NAN2008 EF_MIPS_NAN2008
85 #else
86 # define ELF_MACHINE_NAN2008 0
87 #endif
89 /* Return nonzero iff ELF header is compatible with the running host. */
90 static inline int __attribute_used__
91 elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
93 #if _MIPS_SIM == _ABIO32 || _MIPS_SIM == _ABIN32
94 /* Don't link o32 and n32 together. */
95 if (((ehdr->e_flags & EF_MIPS_ABI2) != 0) != (_MIPS_SIM == _ABIN32))
96 return 0;
97 #endif
99 /* Don't link 2008-NaN and legacy-NaN objects together. */
100 if ((ehdr->e_flags & EF_MIPS_NAN2008) != ELF_MACHINE_NAN2008)
101 return 0;
103 /* Ensure that the old O32 FP64 ABI is never loaded, it is not supported
104 on linux. */
105 if (ehdr->e_flags & EF_MIPS_FP64)
106 return 0;
108 switch (ehdr->e_machine)
110 case EM_MIPS:
111 case EM_MIPS_RS3_LE:
112 return 1;
113 default:
114 return 0;
118 static inline ElfW(Addr) *
119 elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
121 /* FIXME: the offset of gp from GOT may be system-dependent. */
122 return (ElfW(Addr) *) (gpreg - OFFSET_GP_GOT);
125 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
126 first element of the GOT. This must be inlined in a function which
127 uses global data. We assume its $gp points to the primary GOT. */
128 static inline ElfW(Addr)
129 elf_machine_dynamic (void)
131 register ElfW(Addr) gp __asm__ ("$28");
132 return *elf_mips_got_from_gpreg (gp);
135 #define STRINGXP(X) __STRING(X)
136 #define STRINGXV(X) STRINGV_(X)
137 #define STRINGV_(...) # __VA_ARGS__
139 /* Return the run-time load address of the shared object. */
140 static inline ElfW(Addr)
141 elf_machine_load_address (void)
143 ElfW(Addr) addr;
144 #ifndef __mips16
145 asm (" .set noreorder\n"
146 " " STRINGXP (PTR_LA) " %0, 0f\n"
147 # if __mips_isa_rev < 6
148 " bltzal $0, 0f\n"
149 " nop\n"
150 "0: " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
151 # else
152 "0: addiupc $31, 0\n"
153 " " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
154 # endif
155 " .set reorder\n"
156 : "=r" (addr)
157 : /* No inputs */
158 : "$31");
159 #else
160 ElfW(Addr) tmp;
161 asm (" .set noreorder\n"
162 " move %1,$gp\n"
163 " lw %1,%%got(0f)(%1)\n"
164 "0: .fill 0\n" /* Clear the ISA bit on 0:. */
165 " la %0,0b\n"
166 " addiu %1,%%lo(0b)\n"
167 " subu %0,%1\n"
168 " .set reorder\n"
169 : "=d" (addr), "=d" (tmp)
170 : /* No inputs */);
171 #endif
172 return addr;
175 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
176 #if _MIPS_SIM == _ABI64
177 # define ELF_MIPS_GNU_GOT1_MASK 0x8000000000000000L
178 #else
179 # define ELF_MIPS_GNU_GOT1_MASK 0x80000000L
180 #endif
182 /* We can't rely on elf_machine_got_rel because _dl_object_relocation_scope
183 fiddles with global data. */
184 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
185 do { \
186 struct link_map *map = &bootstrap_map; \
187 ElfW(Sym) *sym; \
188 ElfW(Addr) *got; \
189 int i, n; \
191 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]); \
193 if (__builtin_expect (map->l_addr == 0, 1)) \
194 break; \
196 /* got[0] is reserved. got[1] is also reserved for the dynamic object \
197 generated by gnu ld. Skip these reserved entries from \
198 relocation. */ \
199 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1; \
200 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val; \
202 /* Add the run-time displacement to all local got entries. */ \
203 while (i < n) \
204 got[i++] += map->l_addr; \
206 /* Handle global got entries. */ \
207 got += n; \
208 sym = (ElfW(Sym) *) D_PTR(map, l_info[DT_SYMTAB]) \
209 + map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val; \
210 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val \
211 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val); \
213 while (i--) \
215 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx == SHN_COMMON) \
216 *got = map->l_addr + sym->st_value; \
217 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC \
218 && *got != sym->st_value) \
219 *got += map->l_addr; \
220 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION) \
222 if (sym->st_other == 0) \
223 *got += map->l_addr; \
225 else \
226 *got = map->l_addr + sym->st_value; \
228 got++; \
229 sym++; \
231 } while(0)
234 /* Mask identifying addresses reserved for the user program,
235 where the dynamic linker should not map anything. */
236 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
239 /* Initial entry point code for the dynamic linker.
240 The C function `_dl_start' is the real entry point;
241 its return value is the user program's entry point.
242 Note how we have to be careful about two things:
244 1) That we allocate a minimal stack of 24 bytes for
245 every function call, the MIPS ABI states that even
246 if all arguments are passed in registers the procedure
247 called can use the 16 byte area pointed to by $sp
248 when it is called to store away the arguments passed
249 to it.
251 2) That under Unix the entry is named __start
252 and not just plain _start. */
254 #ifndef __mips16
255 # if __mips_isa_rev < 6
256 # define LCOFF STRINGXP(.Lcof2)
257 # define LOAD_31 STRINGXP(bltzal $8) "," STRINGXP(.Lcof2)
258 # else
259 # define LCOFF STRINGXP(.Lcof1)
260 # define LOAD_31 "addiupc $31, 0"
261 # endif
262 # define RTLD_START asm (\
263 ".text\n\
264 " _RTLD_PROLOGUE(ENTRY_POINT) "\
265 " STRINGXV(SETUP_GPX($25)) "\n\
266 " STRINGXV(SETUP_GPX64($18,$25)) "\n\
267 # i386 ABI book says that the first entry of GOT holds\n\
268 # the address of the dynamic structure. Though MIPS ABI\n\
269 # doesn't say nothing about this, I emulate this here.\n\
270 " STRINGXP(PTR_LA) " $4, _DYNAMIC\n\
271 # Subtract OFFSET_GP_GOT\n\
272 " STRINGXP(PTR_S) " $4, -0x7ff0($28)\n\
273 move $4, $29\n\
274 " STRINGXP(PTR_SUBIU) " $29, 16\n\
276 " STRINGXP(PTR_LA) " $8, " LCOFF "\n\
277 .Lcof1: " LOAD_31 "\n\
278 .Lcof2: " STRINGXP(PTR_SUBU) " $8, $31, $8\n\
280 " STRINGXP(PTR_LA) " $25, _dl_start\n\
281 " STRINGXP(PTR_ADDU) " $25, $8\n\
282 jalr $25\n\
284 " STRINGXP(PTR_ADDIU) " $29, 16\n\
285 # Get the value of label '_dl_start_user' in t9 ($25).\n\
286 " STRINGXP(PTR_LA) " $25, _dl_start_user\n\
287 " _RTLD_EPILOGUE(ENTRY_POINT) "\
290 " _RTLD_PROLOGUE(_dl_start_user) "\
291 " STRINGXP(SETUP_GP) "\n\
292 " STRINGXV(SETUP_GP64($18,_dl_start_user)) "\n\
293 move $16, $28\n\
294 # Save the user entry point address in a saved register.\n\
295 move $17, $2\n\
296 # See if we were run as a command with the executable file\n\
297 # name as an extra leading argument.\n\
298 lw $2, _dl_skip_args\n\
299 beq $2, $0, 1f\n\
300 # Load the original argument count.\n\
301 " STRINGXP(PTR_L) " $4, 0($29)\n\
302 # Subtract _dl_skip_args from it.\n\
303 subu $4, $2\n\
304 # Adjust the stack pointer to skip _dl_skip_args words.\n\
305 sll $2, " STRINGXP (PTRLOG) "\n\
306 " STRINGXP(PTR_ADDU) " $29, $2\n\
307 # Save back the modified argument count.\n\
308 " STRINGXP(PTR_S) " $4, 0($29)\n\
309 1: # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
310 " STRINGXP(PTR_L) " $4, _rtld_local\n\
311 " STRINGXP(PTR_L) /* or lw??? fixme */ " $5, 0($29)\n\
312 " STRINGXP(PTR_LA) " $6, " STRINGXP (PTRSIZE) "($29)\n\
313 sll $7, $5, " STRINGXP (PTRLOG) "\n\
314 " STRINGXP(PTR_ADDU) " $7, $7, $6\n\
315 " STRINGXP(PTR_ADDU) " $7, $7, " STRINGXP (PTRSIZE) " \n\
316 # Make sure the stack pointer is aligned for _dl_init.\n\
317 and $2, $29, -2 * " STRINGXP(SZREG) "\n\
318 move $8, $29\n\
319 " STRINGXP(PTR_SUBIU) " $29, $2, 32\n\
320 " STRINGXP(PTR_S) " $8, (32 - " STRINGXP(SZREG) ")($29)\n\
321 " STRINGXP(SAVE_GP(16)) "\n\
322 # Call the function to run the initializers.\n\
323 jal _dl_init\n\
324 # Restore the stack pointer for _start.\n\
325 " STRINGXP(PTR_L) " $29, (32 - " STRINGXP(SZREG) ")($29)\n\
326 # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
327 " STRINGXP(PTR_LA) " $2, _dl_fini\n\
328 # Jump to the user entry point.\n\
329 move $25, $17\n\
330 jr $25\n\t"\
331 _RTLD_EPILOGUE(_dl_start_user)\
332 ".previous"\
335 #else /* __mips16 */
336 /* MIPS16 version. We currently only support O32 under MIPS16; the proper
337 assembly preprocessor abstractions will need to be added if other ABIs
338 are to be supported. */
340 # define RTLD_START asm (\
341 ".text\n\
342 .set mips16\n\
343 " _RTLD_PROLOGUE (ENTRY_POINT) "\
344 # Construct GP value in $3.\n\
345 li $3, %hi(_gp_disp)\n\
346 addiu $4, $pc, %lo(_gp_disp)\n\
347 sll $3, 16\n\
348 addu $3, $4\n\
349 move $28, $3\n\
350 lw $4, %got(_DYNAMIC)($3)\n\
351 sw $4, -0x7ff0($3)\n\
352 move $4, $sp\n\
353 addiu $sp, -16\n\
354 # _dl_start() is sufficiently near to use pc-relative\n\
355 # load address.\n\
356 la $3, _dl_start\n\
357 move $25, $3\n\
358 jalr $3\n\
359 addiu $sp, 16\n\
360 " _RTLD_EPILOGUE (ENTRY_POINT) "\
363 " _RTLD_PROLOGUE (_dl_start_user) "\
364 li $16, %hi(_gp_disp)\n\
365 addiu $4, $pc, %lo(_gp_disp)\n\
366 sll $16, 16\n\
367 addu $16, $4\n\
368 move $17, $2\n\
369 move $28, $16\n\
370 lw $4, %got(_dl_skip_args)($16)\n\
371 lw $4, 0($4)\n\
372 beqz $4, 1f\n\
373 # Load the original argument count.\n\
374 lw $5, 0($sp)\n\
375 # Subtract _dl_skip_args from it.\n\
376 subu $5, $4\n\
377 # Adjust the stack pointer to skip _dl_skip_args words.\n\
378 sll $4, " STRINGXP (PTRLOG) "\n\
379 move $6, $sp\n\
380 addu $6, $4\n\
381 move $sp, $6\n\
382 # Save back the modified argument count.\n\
383 sw $5, 0($sp)\n\
384 1: # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
385 lw $4, %got(_rtld_local)($16)\n\
386 lw $4, 0($4)\n\
387 lw $5, 0($sp)\n\
388 addiu $6, $sp, " STRINGXP (PTRSIZE) "\n\
389 sll $7, $5, " STRINGXP (PTRLOG) "\n\
390 addu $7, $6\n\
391 addu $7, " STRINGXP (PTRSIZE) "\n\
392 # Make sure the stack pointer is aligned for _dl_init.\n\
393 li $2, 2 * " STRINGXP (SZREG) "\n\
394 neg $2, $2\n\
395 move $3, $sp\n\
396 and $2, $3\n\
397 sw $3, -" STRINGXP (SZREG) "($2)\n\
398 addiu $2, -32\n\
399 move $sp, $2\n\
400 sw $16, 16($sp)\n\
401 # Call the function to run the initializers.\n\
402 lw $2, %call16(_dl_init)($16)\n\
403 move $25, $2\n\
404 jalr $2\n\
405 # Restore the stack pointer for _start.\n\
406 lw $2, 32-" STRINGXP (SZREG) "($sp)\n\
407 move $sp, $2\n\
408 move $28, $16\n\
409 # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
410 lw $2, %call16(_dl_fini)($16)\n\
411 # Jump to the user entry point.\n\
412 move $25, $17\n\
413 jr $17\n\t"\
414 _RTLD_EPILOGUE (_dl_start_user)\
415 ".previous"\
418 #endif /* __mips16 */
420 /* Names of the architecture-specific auditing callback functions. */
421 # if _MIPS_SIM == _ABIO32
422 # define ARCH_LA_PLTENTER mips_o32_gnu_pltenter
423 # define ARCH_LA_PLTEXIT mips_o32_gnu_pltexit
424 # elif _MIPS_SIM == _ABIN32
425 # define ARCH_LA_PLTENTER mips_n32_gnu_pltenter
426 # define ARCH_LA_PLTEXIT mips_n32_gnu_pltexit
427 # else
428 # define ARCH_LA_PLTENTER mips_n64_gnu_pltenter
429 # define ARCH_LA_PLTEXIT mips_n64_gnu_pltexit
430 # endif
432 /* We define an initialization function. This is called very early in
433 _dl_sysdep_start. */
434 #define DL_PLATFORM_INIT dl_platform_init ()
436 static inline void __attribute__ ((unused))
437 dl_platform_init (void)
439 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
440 /* Avoid an empty string which would disturb us. */
441 GLRO(dl_platform) = NULL;
444 /* For a non-writable PLT, rewrite the .got.plt entry at RELOC_ADDR to
445 point at the symbol with address VALUE. For a writable PLT, rewrite
446 the corresponding PLT entry instead. */
447 static inline ElfW(Addr)
448 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
449 const ElfW(Rel) *reloc,
450 ElfW(Addr) *reloc_addr, ElfW(Addr) value)
452 return *reloc_addr = value;
455 static inline ElfW(Addr)
456 elf_machine_plt_value (struct link_map *map, const ElfW(Rel) *reloc,
457 ElfW(Addr) value)
459 return value;
462 /* The semantics of zero/non-zero values of undefined symbols differs
463 depending on whether the non-PIC ABI is in use. Under the non-PIC
464 ABI, a non-zero value indicates that there is an address reference
465 to the symbol and thus it must always be resolved (except when
466 resolving a jump slot relocation) to the PLT entry whose address is
467 provided as the symbol's value; a zero value indicates that this
468 canonical-address behaviour is not required. Yet under the classic
469 MIPS psABI, a zero value indicates that there is an address
470 reference to the function and the dynamic linker must resolve the
471 symbol immediately upon loading. To avoid conflict, symbols for
472 which the dynamic linker must assume the non-PIC ABI semantics are
473 marked with the STO_MIPS_PLT flag. */
474 #define ELF_MACHINE_SYM_NO_MATCH(sym) \
475 ((sym)->st_shndx == SHN_UNDEF && !((sym)->st_other & STO_MIPS_PLT))
477 #endif /* !dl_machine_h */
479 #ifdef RESOLVE_MAP
481 /* Perform a relocation described by R_INFO at the location pointed to
482 by RELOC_ADDR. SYM is the relocation symbol specified by R_INFO and
483 MAP is the object containing the reloc. */
485 auto inline void
486 __attribute__ ((always_inline))
487 elf_machine_reloc (struct link_map *map, ElfW(Addr) r_info,
488 const ElfW(Sym) *sym, const struct r_found_version *version,
489 void *reloc_addr, ElfW(Addr) r_addend, int inplace_p)
491 const unsigned long int r_type = ELFW(R_TYPE) (r_info);
492 ElfW(Addr) *addr_field = (ElfW(Addr) *) reloc_addr;
494 #if !defined RTLD_BOOTSTRAP && !defined SHARED
495 /* This is defined in rtld.c, but nowhere in the static libc.a;
496 make the reference weak so static programs can still link. This
497 declaration cannot be done when compiling rtld.c (i.e. #ifdef
498 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
499 _dl_rtld_map, which is incompatible with a weak decl in the same
500 file. */
501 weak_extern (GL(dl_rtld_map));
502 #endif
504 switch (r_type)
506 #if !defined (RTLD_BOOTSTRAP)
507 # if _MIPS_SIM == _ABI64
508 case R_MIPS_TLS_DTPMOD64:
509 case R_MIPS_TLS_DTPREL64:
510 case R_MIPS_TLS_TPREL64:
511 # else
512 case R_MIPS_TLS_DTPMOD32:
513 case R_MIPS_TLS_DTPREL32:
514 case R_MIPS_TLS_TPREL32:
515 # endif
517 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
519 switch (r_type)
521 case R_MIPS_TLS_DTPMOD64:
522 case R_MIPS_TLS_DTPMOD32:
523 if (sym_map)
524 *addr_field = sym_map->l_tls_modid;
525 break;
527 case R_MIPS_TLS_DTPREL64:
528 case R_MIPS_TLS_DTPREL32:
529 if (sym)
531 if (inplace_p)
532 r_addend = *addr_field;
533 *addr_field = r_addend + TLS_DTPREL_VALUE (sym);
535 break;
537 case R_MIPS_TLS_TPREL32:
538 case R_MIPS_TLS_TPREL64:
539 if (sym)
541 CHECK_STATIC_TLS (map, sym_map);
542 if (inplace_p)
543 r_addend = *addr_field;
544 *addr_field = r_addend + TLS_TPREL_VALUE (sym_map, sym);
546 break;
549 break;
551 #endif
553 #if _MIPS_SIM == _ABI64
554 case (R_MIPS_64 << 8) | R_MIPS_REL32:
555 #else
556 case R_MIPS_REL32:
557 #endif
559 int symidx = ELFW(R_SYM) (r_info);
560 ElfW(Addr) reloc_value;
562 if (inplace_p)
563 /* Support relocations on mis-aligned offsets. */
564 __builtin_memcpy (&reloc_value, reloc_addr, sizeof (reloc_value));
565 else
566 reloc_value = r_addend;
568 if (symidx)
570 const ElfW(Word) gotsym
571 = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
573 if ((ElfW(Word))symidx < gotsym)
575 /* This wouldn't work for a symbol imported from other
576 libraries for which there's no GOT entry, but MIPS
577 requires every symbol referenced in a dynamic
578 relocation to have a GOT entry in the primary GOT,
579 so we only get here for locally-defined symbols.
580 For section symbols, we should *NOT* be adding
581 sym->st_value (per the definition of the meaning of
582 S in reloc expressions in the ELF64 MIPS ABI),
583 since it should have already been added to
584 reloc_value by the linker, but older versions of
585 GNU ld didn't add it, and newer versions don't emit
586 useless relocations to section symbols any more, so
587 it is safe to keep on adding sym->st_value, even
588 though it's not ABI compliant. Some day we should
589 bite the bullet and stop doing this. */
590 #ifndef RTLD_BOOTSTRAP
591 if (map != &GL(dl_rtld_map))
592 #endif
593 reloc_value += sym->st_value + map->l_addr;
595 else
597 #ifndef RTLD_BOOTSTRAP
598 const ElfW(Addr) *got
599 = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
600 const ElfW(Word) local_gotno
601 = (const ElfW(Word))
602 map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
604 reloc_value += got[symidx + local_gotno - gotsym];
605 #endif
608 else
609 #ifndef RTLD_BOOTSTRAP
610 if (map != &GL(dl_rtld_map))
611 #endif
612 reloc_value += map->l_addr;
614 __builtin_memcpy (reloc_addr, &reloc_value, sizeof (reloc_value));
616 break;
617 #ifndef RTLD_BOOTSTRAP
618 #if _MIPS_SIM == _ABI64
619 case (R_MIPS_64 << 8) | R_MIPS_GLOB_DAT:
620 #else
621 case R_MIPS_GLOB_DAT:
622 #endif
624 int symidx = ELFW(R_SYM) (r_info);
625 const ElfW(Word) gotsym
626 = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
628 if (__builtin_expect ((ElfW(Word)) symidx >= gotsym, 1))
630 const ElfW(Addr) *got
631 = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
632 const ElfW(Word) local_gotno
633 = ((const ElfW(Word))
634 map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val);
636 ElfW(Addr) reloc_value = got[symidx + local_gotno - gotsym];
637 __builtin_memcpy (reloc_addr, &reloc_value, sizeof (reloc_value));
640 break;
641 #endif
642 case R_MIPS_NONE: /* Alright, Wilbur. */
643 break;
645 case R_MIPS_JUMP_SLOT:
647 struct link_map *sym_map;
648 ElfW(Addr) value;
650 /* The addend for a jump slot relocation must always be zero:
651 calls via the PLT always branch to the symbol's address and
652 not to the address plus a non-zero offset. */
653 if (r_addend != 0)
654 _dl_signal_error (0, map->l_name, NULL,
655 "found jump slot relocation with non-zero addend");
657 sym_map = RESOLVE_MAP (&sym, version, r_type);
658 value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
659 *addr_field = value;
661 break;
664 case R_MIPS_COPY:
666 const ElfW(Sym) *const refsym = sym;
667 struct link_map *sym_map;
668 ElfW(Addr) value;
670 /* Calculate the address of the symbol. */
671 sym_map = RESOLVE_MAP (&sym, version, r_type);
672 value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
674 if (__builtin_expect (sym == NULL, 0))
675 /* This can happen in trace mode if an object could not be
676 found. */
677 break;
678 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
679 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
680 && GLRO(dl_verbose)))
682 const char *strtab;
684 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
685 _dl_error_printf ("\
686 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
687 RTLD_PROGNAME, strtab + refsym->st_name);
689 memcpy (reloc_addr, (void *) value,
690 MIN (sym->st_size, refsym->st_size));
691 break;
694 #if _MIPS_SIM == _ABI64
695 case R_MIPS_64:
696 /* For full compliance with the ELF64 ABI, one must precede the
697 _REL32/_64 pair of relocations with a _64 relocation, such
698 that the in-place addend is read as a 64-bit value. IRIX
699 didn't pick up on this requirement, so we treat the
700 _REL32/_64 relocation as a 64-bit relocation even if it's by
701 itself. For ABI compliance, we ignore such _64 dummy
702 relocations. For RELA, this may be simply removed, since
703 it's totally unnecessary. */
704 if (ELFW(R_SYM) (r_info) == 0)
705 break;
706 /* Fall through. */
707 #endif
708 default:
709 _dl_reloc_bad_type (map, r_type, 0);
710 break;
714 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
715 MAP is the object containing the reloc. */
717 auto inline void
718 __attribute__ ((always_inline))
719 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
720 const ElfW(Sym) *sym, const struct r_found_version *version,
721 void *const reloc_addr, int skip_ifunc)
723 elf_machine_reloc (map, reloc->r_info, sym, version, reloc_addr, 0, 1);
726 auto inline void
727 __attribute__((always_inline))
728 elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
729 void *const reloc_addr)
731 /* XXX Nothing to do. There is no relative relocation, right? */
734 auto inline void
735 __attribute__((always_inline))
736 elf_machine_lazy_rel (struct link_map *map,
737 ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
738 int skip_ifunc)
740 ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
741 const unsigned int r_type = ELFW(R_TYPE) (reloc->r_info);
742 /* Check for unexpected PLT reloc type. */
743 if (__builtin_expect (r_type == R_MIPS_JUMP_SLOT, 1))
745 if (__builtin_expect (map->l_mach.plt, 0) == 0)
747 /* Nothing is required here since we only support lazy
748 relocation in executables. */
750 else
751 *reloc_addr = map->l_mach.plt;
753 else
754 _dl_reloc_bad_type (map, r_type, 1);
757 auto inline void
758 __attribute__ ((always_inline))
759 elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
760 const ElfW(Sym) *sym, const struct r_found_version *version,
761 void *const reloc_addr, int skip_ifunc)
763 elf_machine_reloc (map, reloc->r_info, sym, version, reloc_addr,
764 reloc->r_addend, 0);
767 auto inline void
768 __attribute__((always_inline))
769 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
770 void *const reloc_addr)
774 #ifndef RTLD_BOOTSTRAP
775 /* Relocate GOT. */
776 auto inline void
777 __attribute__((always_inline))
778 elf_machine_got_rel (struct link_map *map, int lazy)
780 ElfW(Addr) *got;
781 ElfW(Sym) *sym;
782 const ElfW(Half) *vernum;
783 int i, n, symidx;
785 #define RESOLVE_GOTSYM(sym,vernum,sym_index,reloc) \
786 ({ \
787 const ElfW(Sym) *ref = sym; \
788 const struct r_found_version *version __attribute__ ((unused)) \
789 = vernum ? &map->l_versions[vernum[sym_index] & 0x7fff] : NULL; \
790 struct link_map *sym_map; \
791 sym_map = RESOLVE_MAP (&ref, version, reloc); \
792 ref ? sym_map->l_addr + ref->st_value : 0; \
795 if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
796 vernum = (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
797 else
798 vernum = NULL;
800 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
802 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
803 /* The dynamic linker's local got entries have already been relocated. */
804 if (map != &GL(dl_rtld_map))
806 /* got[0] is reserved. got[1] is also reserved for the dynamic object
807 generated by gnu ld. Skip these reserved entries from relocation. */
808 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1;
810 /* Add the run-time displacement to all local got entries if
811 needed. */
812 if (__builtin_expect (map->l_addr != 0, 0))
814 while (i < n)
815 got[i++] += map->l_addr;
819 /* Handle global got entries. */
820 got += n;
821 /* Keep track of the symbol index. */
822 symidx = map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
823 sym = (ElfW(Sym) *) D_PTR (map, l_info[DT_SYMTAB]) + symidx;
824 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
825 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
827 /* This loop doesn't handle Quickstart. */
828 while (i--)
830 if (sym->st_shndx == SHN_UNDEF)
832 if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC && sym->st_value
833 && !(sym->st_other & STO_MIPS_PLT))
835 if (lazy)
836 *got = sym->st_value + map->l_addr;
837 else
838 /* This is a lazy-binding stub, so we don't need the
839 canonical address. */
840 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_JUMP_SLOT);
842 else
843 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
845 else if (sym->st_shndx == SHN_COMMON)
846 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
847 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
848 && *got != sym->st_value)
850 if (lazy)
851 *got += map->l_addr;
852 else
853 /* This is a lazy-binding stub, so we don't need the
854 canonical address. */
855 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_JUMP_SLOT);
857 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)
859 if (sym->st_other == 0)
860 *got += map->l_addr;
862 else
863 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
865 ++got;
866 ++sym;
867 ++symidx;
870 #undef RESOLVE_GOTSYM
872 #endif
874 /* Set up the loaded object described by L so its stub function
875 will jump to the on-demand fixup code __dl_runtime_resolve. */
877 auto inline int
878 __attribute__((always_inline))
879 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
881 # ifndef RTLD_BOOTSTRAP
882 ElfW(Addr) *got;
883 extern void _dl_runtime_resolve (ElfW(Word));
884 extern void _dl_runtime_pltresolve (void);
885 extern int _dl_mips_gnu_objects;
887 if (lazy)
889 /* The GOT entries for functions have not yet been filled in.
890 Their initial contents will arrange when called to put an
891 offset into the .dynsym section in t8, the return address
892 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
893 got = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
895 /* This function will get called to fix up the GOT entry indicated by
896 the register t8, and then jump to the resolved address. */
897 got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
899 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
900 of got[1] of a gnu object is set to identify gnu objects.
901 Where we can store l for non gnu objects? XXX */
902 if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
903 got[1] = ((ElfW(Addr)) l | ELF_MIPS_GNU_GOT1_MASK);
904 else
905 _dl_mips_gnu_objects = 0;
908 /* Relocate global offset table. */
909 elf_machine_got_rel (l, lazy);
911 /* If using PLTs, fill in the first two entries of .got.plt. */
912 if (l->l_info[DT_JMPREL] && lazy)
914 ElfW(Addr) *gotplt;
915 gotplt = (ElfW(Addr) *) D_PTR (l, l_info[DT_MIPS (PLTGOT)]);
916 /* If a library is prelinked but we have to relocate anyway,
917 we have to be able to undo the prelinking of .got.plt.
918 The prelinker saved the address of .plt for us here. */
919 if (gotplt[1])
920 l->l_mach.plt = gotplt[1] + l->l_addr;
921 gotplt[0] = (ElfW(Addr)) &_dl_runtime_pltresolve;
922 gotplt[1] = (ElfW(Addr)) l;
925 # endif
926 return lazy;
929 #endif /* RESOLVE_MAP */