]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mips/dl-machine.h
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysdeps / mips / dl-machine.h
CommitLineData
f1fc1823 1/* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
04277e02 2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
f1fc1823
UD
3 This file is part of the GNU C Library.
4 Contributed by Kazumoto Kojima <kkojima@info.kanagawa-u.ac.jp>.
8db38e6c 5
f1fc1823 6 The GNU C Library is free software; you can redistribute it and/or
3214b89b
AJ
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.
8db38e6c 10
f1fc1823
UD
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
3214b89b 14 Lesser General Public License for more details.
8db38e6c 15
3214b89b 16 You should have received a copy of the GNU Lesser General Public
ab84e3ff 17 License along with the GNU C Library. If not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
8db38e6c 19
4f203842 20/* FIXME: Profiling of shared libraries is not implemented yet. */
0134d025
UD
21#ifndef dl_machine_h
22#define dl_machine_h
23
8db38e6c
RM
24#define ELF_MACHINE_NAME "MIPS"
25
bd4c4968
UD
26#include <entry.h>
27
28#ifndef ENTRY_POINT
29#error ENTRY_POINT needs to be defined for MIPS.
30#endif
31
b8ddf7a1 32#include <sgidefs.h>
03403e30 33#include <sysdep.h>
0d5b7257 34#include <sys/asm.h>
305fae3b 35#include <dl-tls.h>
0d5b7257 36
4f203842
AJ
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
40
bd4c4968 41#ifndef _RTLD_PROLOGUE
921bb2c3
AJ
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"
bd4c4968
UD
47#endif
48
49#ifndef _RTLD_EPILOGUE
921bb2c3
AJ
50# define _RTLD_EPILOGUE(entry) \
51 ".end\t" __STRING(entry) "\n\t" \
52 ".size\t" __STRING(entry) ", . - " __STRING(entry) "\n\t"
bd4c4968
UD
53#endif
54
58f68573 55/* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.
63fb881a
DJ
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))
62
63#define ELF_MACHINE_PLT_REL 1
4cf5b6d0
SP
64#define ELF_MACHINE_NO_REL 0
65#define ELF_MACHINE_NO_RELA 0
8db38e6c 66
910e2e14 67/* Translate a processor specific dynamic tag to the index
8db38e6c
RM
68 in l_info array. */
69#define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
70
a2057c98
MF
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. */
0134d025 74#define ELF_MACHINE_DEBUG_SETUP(l,r) \
a2057c98
MF
75do { 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)]) \
8db38e6c
RM
82 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
83 (ElfW(Addr)) (r); \
84 } while (0)
85
9c21573c
MR
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
95
f1dba308 96/* Return nonzero iff ELF header is compatible with the running host. */
4bf39226 97static inline int __attribute_used__
f1dba308 98elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
8db38e6c 99{
b8ddf7a1 100#if _MIPS_SIM == _ABIO32 || _MIPS_SIM == _ABIN32
0d5b7257 101 /* Don't link o32 and n32 together. */
b8ddf7a1 102 if (((ehdr->e_flags & EF_MIPS_ABI2) != 0) != (_MIPS_SIM == _ABIN32))
0d5b7257
AO
103 return 0;
104#endif
105
9c21573c
MR
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;
109
0bd95672
MF
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;
114
f1dba308 115 switch (ehdr->e_machine)
8db38e6c
RM
116 {
117 case EM_MIPS:
be57c15a 118 case EM_MIPS_RS3_LE:
8db38e6c
RM
119 return 1;
120 default:
121 return 0;
122 }
123}
124
125static inline ElfW(Addr) *
126elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
127{
128 /* FIXME: the offset of gp from GOT may be system-dependent. */
4f203842 129 return (ElfW(Addr) *) (gpreg - OFFSET_GP_GOT);
8db38e6c 130}
0134d025 131
58f68573
AJ
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
0d5b7257 134 uses global data. We assume its $gp points to the primary GOT. */
58f68573
AJ
135static inline ElfW(Addr)
136elf_machine_dynamic (void)
8db38e6c 137{
58f68573 138 register ElfW(Addr) gp __asm__ ("$28");
119fab98 139 return *elf_mips_got_from_gpreg (gp);
8db38e6c
RM
140}
141
0d5b7257
AO
142#define STRINGXP(X) __STRING(X)
143#define STRINGXV(X) STRINGV_(X)
144#define STRINGV_(...) # __VA_ARGS__
8db38e6c
RM
145
146/* Return the run-time load address of the shared object. */
147static inline ElfW(Addr)
148elf_machine_load_address (void)
149{
150 ElfW(Addr) addr;
43301bd3 151#ifndef __mips16
8db38e6c 152 asm (" .set noreorder\n"
0d5b7257 153 " " STRINGXP (PTR_LA) " %0, 0f\n"
87336cae 154# if !defined __mips_isa_rev || __mips_isa_rev < 6
0d5b7257 155 " bltzal $0, 0f\n"
8db38e6c 156 " nop\n"
0d5b7257 157 "0: " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
03403e30
SE
158# else
159 "0: addiupc $31, 0\n"
160 " " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
161# endif
8db38e6c 162 " .set reorder\n"
bd4c4968
UD
163 : "=r" (addr)
164 : /* No inputs */
165 : "$31");
43301bd3
MR
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
8db38e6c
RM
179 return addr;
180}
181
127035bb 182/* The MSB of got[1] of a gnu object is set to identify gnu objects. */
b8ddf7a1 183#if _MIPS_SIM == _ABI64
3d06657c
AO
184# define ELF_MIPS_GNU_GOT1_MASK 0x8000000000000000L
185#else
186# define ELF_MIPS_GNU_GOT1_MASK 0x80000000L
187#endif
d9c1416a 188
921bb2c3
AJ
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) \
192do { \
b8818ab5 193 struct link_map *map = BOOTSTRAP_MAP; \
921bb2c3
AJ
194 ElfW(Sym) *sym; \
195 ElfW(Addr) *got; \
196 int i, n; \
197 \
198 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]); \
199 \
921bb2c3 200 if (__builtin_expect (map->l_addr == 0, 1)) \
872cf89f 201 break; \
921bb2c3
AJ
202 \
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; \
208 \
9285ecc8 209 /* Add the run-time displacement to all local got entries. */ \
921bb2c3
AJ
210 while (i < n) \
211 got[i++] += map->l_addr; \
212 \
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); \
219 \
220 while (i--) \
221 { \
222 if (sym->st_shndx == SHN_UNDEF || sym->st_shndx == SHN_COMMON) \
10a446dd 223 *got = SYMBOL_ADDRESS (map, sym, true); \
921bb2c3
AJ
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) \
228 { \
229 if (sym->st_other == 0) \
230 *got += map->l_addr; \
231 } \
232 else \
10a446dd 233 *got = SYMBOL_ADDRESS (map, sym, true); \
921bb2c3
AJ
234 \
235 got++; \
236 sym++; \
237 } \
921bb2c3
AJ
238} while(0)
239
240
8db38e6c
RM
241/* Mask identifying addresses reserved for the user program,
242 where the dynamic linker should not map anything. */
bd4c4968 243#define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
8db38e6c 244
0134d025 245
8db38e6c
RM
246/* Initial entry point code for the dynamic linker.
247 The C function `_dl_start' is the real entry point;
bd4c4968
UD
248 its return value is the user program's entry point.
249 Note how we have to be careful about two things:
250
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.
257
d05f2edb 258 2) That under Unix the entry is named __start
bd4c4968 259 and not just plain _start. */
8db38e6c 260
43301bd3 261#ifndef __mips16
87336cae 262# if !defined __mips_isa_rev || __mips_isa_rev < 6
03403e30
SE
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
43301bd3 269# define RTLD_START asm (\
bd9fad09
AJ
270 ".text\n\
271 " _RTLD_PROLOGUE(ENTRY_POINT) "\
0d5b7257
AO
272 " STRINGXV(SETUP_GPX($25)) "\n\
273 " STRINGXV(SETUP_GPX64($18,$25)) "\n\
8db38e6c
RM
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\
0d5b7257 277 " STRINGXP(PTR_LA) " $4, _DYNAMIC\n\
4f203842 278 # Subtract OFFSET_GP_GOT\n\
0d5b7257 279 " STRINGXP(PTR_S) " $4, -0x7ff0($28)\n\
8db38e6c 280 move $4, $29\n\
0d5b7257 281 " STRINGXP(PTR_SUBIU) " $29, 16\n\
921bb2c3 282 \n\
03403e30
SE
283 " STRINGXP(PTR_LA) " $8, " LCOFF "\n\
284.Lcof1: " LOAD_31 "\n\
285.Lcof2: " STRINGXP(PTR_SUBU) " $8, $31, $8\n\
921bb2c3 286 \n\
0d5b7257
AO
287 " STRINGXP(PTR_LA) " $25, _dl_start\n\
288 " STRINGXP(PTR_ADDU) " $25, $8\n\
921bb2c3
AJ
289 jalr $25\n\
290 \n\
0d5b7257 291 " STRINGXP(PTR_ADDIU) " $29, 16\n\
8db38e6c 292 # Get the value of label '_dl_start_user' in t9 ($25).\n\
0d5b7257 293 " STRINGXP(PTR_LA) " $25, _dl_start_user\n\
bd9fad09
AJ
294 " _RTLD_EPILOGUE(ENTRY_POINT) "\
295 \n\
296 \n\
297 " _RTLD_PROLOGUE(_dl_start_user) "\
0d5b7257
AO
298 " STRINGXP(SETUP_GP) "\n\
299 " STRINGXV(SETUP_GP64($18,_dl_start_user)) "\n\
8db38e6c 300 move $16, $28\n\
3a344e73 301 # Save the user entry point address in a saved register.\n\
8db38e6c
RM
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\
0d5b7257 308 " STRINGXP(PTR_L) " $4, 0($29)\n\
8db38e6c
RM
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\
0d5b7257
AO
312 sll $2, " STRINGXP (PTRLOG) "\n\
313 " STRINGXP(PTR_ADDU) " $29, $2\n\
8db38e6c 314 # Save back the modified argument count.\n\
0d5b7257 315 " STRINGXP(PTR_S) " $4, 0($29)\n\
3a344e73 3161: # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
0d5b7257
AO
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\
c5684fdb 323 # Make sure the stack pointer is aligned for _dl_init.\n\
bd9fad09 324 and $2, $29, -2 * " STRINGXP(SZREG) "\n\
76b1f93b 325 move $8, $29\n\
bd9fad09 326 " STRINGXP(PTR_SUBIU) " $29, $2, 32\n\
76b1f93b 327 " STRINGXP(PTR_S) " $8, (32 - " STRINGXP(SZREG) ")($29)\n\
0d5b7257 328 " STRINGXP(SAVE_GP(16)) "\n\
3a344e73 329 # Call the function to run the initializers.\n\
c5684fdb 330 jal _dl_init\n\
bd9fad09 331 # Restore the stack pointer for _start.\n\
76b1f93b 332 " STRINGXP(PTR_L) " $29, (32 - " STRINGXP(SZREG) ")($29)\n\
49e1806c 333 # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
0d5b7257 334 " STRINGXP(PTR_LA) " $2, _dl_fini\n\
8db38e6c 335 # Jump to the user entry point.\n\
feaff189 336 move $25, $17\n\
57f4291c 337 jr $25\n\t"\
bd9fad09 338 _RTLD_EPILOGUE(_dl_start_user)\
921bb2c3 339 ".previous"\
09d24ad4 340);
0134d025 341
43301bd3
MR
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. */
346
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) "\
368 \n\
369 \n\
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\
3911: # 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\
c5684fdb 399 # Make sure the stack pointer is aligned for _dl_init.\n\
43301bd3
MR
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\
c5684fdb 409 lw $2, %call16(_dl_init)($16)\n\
43301bd3
MR
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"\
423);
424
425#endif /* __mips16 */
426
bd9fad09
AJ
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
438
87ae60cc
RM
439/* We define an initialization function. This is called very early in
440 _dl_sysdep_start. */
441#define DL_PLATFORM_INIT dl_platform_init ()
442
443static inline void __attribute__ ((unused))
444dl_platform_init (void)
445{
446 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
447 /* Avoid an empty string which would disturb us. */
448 GLRO(dl_platform) = NULL;
449}
450
63fb881a
DJ
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. */
454static inline ElfW(Addr)
455elf_machine_fixup_plt (struct link_map *map, lookup_t t,
0572433b 456 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
63fb881a
DJ
457 const ElfW(Rel) *reloc,
458 ElfW(Addr) *reloc_addr, ElfW(Addr) value)
459{
460 return *reloc_addr = value;
461}
462
463static inline ElfW(Addr)
464elf_machine_plt_value (struct link_map *map, const ElfW(Rel) *reloc,
465 ElfW(Addr) value)
466{
467 return value;
468}
469
cb4a2928
JM
470/* The semantics of zero/non-zero values of undefined symbols differs
471 depending on whether the non-PIC ABI is in use. Under the non-PIC
472 ABI, a non-zero value indicates that there is an address reference
473 to the symbol and thus it must always be resolved (except when
474 resolving a jump slot relocation) to the PLT entry whose address is
475 provided as the symbol's value; a zero value indicates that this
476 canonical-address behaviour is not required. Yet under the classic
477 MIPS psABI, a zero value indicates that there is an address
478 reference to the function and the dynamic linker must resolve the
479 symbol immediately upon loading. To avoid conflict, symbols for
480 which the dynamic linker must assume the non-PIC ABI semantics are
481 marked with the STO_MIPS_PLT flag. */
482#define ELF_MACHINE_SYM_NO_MATCH(sym) \
483 ((sym)->st_shndx == SHN_UNDEF && !((sym)->st_other & STO_MIPS_PLT))
484
0134d025 485#endif /* !dl_machine_h */
8db38e6c 486
bd9fad09 487#ifdef RESOLVE_MAP
3887a8dc 488
cb5bcf74
DJ
489/* Perform a relocation described by R_INFO at the location pointed to
490 by RELOC_ADDR. SYM is the relocation symbol specified by R_INFO and
3887a8dc
RM
491 MAP is the object containing the reloc. */
492
bd9fad09
AJ
493auto inline void
494__attribute__ ((always_inline))
9077d4dc 495elf_machine_reloc (struct link_map *map, ElfW(Addr) r_info,
cb5bcf74
DJ
496 const ElfW(Sym) *sym, const struct r_found_version *version,
497 void *reloc_addr, ElfW(Addr) r_addend, int inplace_p)
3887a8dc 498{
cb5bcf74
DJ
499 const unsigned long int r_type = ELFW(R_TYPE) (r_info);
500 ElfW(Addr) *addr_field = (ElfW(Addr) *) reloc_addr;
85bdccdb 501
f71d7f57 502#if !defined RTLD_BOOTSTRAP && !defined SHARED
5166e402
AJ
503 /* This is defined in rtld.c, but nowhere in the static libc.a;
504 make the reference weak so static programs can still link. This
505 declaration cannot be done when compiling rtld.c (i.e. #ifdef
506 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
507 _dl_rtld_map, which is incompatible with a weak decl in the same
508 file. */
f71d7f57 509 weak_extern (GL(dl_rtld_map));
5166e402 510#endif
3887a8dc 511
85bdccdb 512 switch (r_type)
3887a8dc 513 {
b8ead09a 514#if !defined (RTLD_BOOTSTRAP)
305fae3b
AJ
515# if _MIPS_SIM == _ABI64
516 case R_MIPS_TLS_DTPMOD64:
517 case R_MIPS_TLS_DTPREL64:
518 case R_MIPS_TLS_TPREL64:
519# else
520 case R_MIPS_TLS_DTPMOD32:
521 case R_MIPS_TLS_DTPREL32:
522 case R_MIPS_TLS_TPREL32:
523# endif
524 {
525 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
305fae3b
AJ
526
527 switch (r_type)
528 {
529 case R_MIPS_TLS_DTPMOD64:
530 case R_MIPS_TLS_DTPMOD32:
531 if (sym_map)
cb5bcf74 532 *addr_field = sym_map->l_tls_modid;
305fae3b
AJ
533 break;
534
535 case R_MIPS_TLS_DTPREL64:
536 case R_MIPS_TLS_DTPREL32:
cb5bcf74
DJ
537 if (sym)
538 {
539 if (inplace_p)
540 r_addend = *addr_field;
541 *addr_field = r_addend + TLS_DTPREL_VALUE (sym);
542 }
305fae3b
AJ
543 break;
544
545 case R_MIPS_TLS_TPREL32:
546 case R_MIPS_TLS_TPREL64:
cb5bcf74
DJ
547 if (sym)
548 {
549 CHECK_STATIC_TLS (map, sym_map);
550 if (inplace_p)
551 r_addend = *addr_field;
552 *addr_field = r_addend + TLS_TPREL_VALUE (sym_map, sym);
553 }
305fae3b
AJ
554 break;
555 }
556
557 break;
558 }
559#endif
560
b8ddf7a1 561#if _MIPS_SIM == _ABI64
0d5b7257
AO
562 case (R_MIPS_64 << 8) | R_MIPS_REL32:
563#else
3887a8dc 564 case R_MIPS_REL32:
0d5b7257 565#endif
9285ecc8 566 {
cb5bcf74 567 int symidx = ELFW(R_SYM) (r_info);
0d5b7257
AO
568 ElfW(Addr) reloc_value;
569
cb5bcf74
DJ
570 if (inplace_p)
571 /* Support relocations on mis-aligned offsets. */
572 __builtin_memcpy (&reloc_value, reloc_addr, sizeof (reloc_value));
573 else
574 reloc_value = r_addend;
9285ecc8
AJ
575
576 if (symidx)
577 {
578 const ElfW(Word) gotsym
579 = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
580
3d06657c 581 if ((ElfW(Word))symidx < gotsym)
9285ecc8 582 {
0d5b7257
AO
583 /* This wouldn't work for a symbol imported from other
584 libraries for which there's no GOT entry, but MIPS
585 requires every symbol referenced in a dynamic
586 relocation to have a GOT entry in the primary GOT,
587 so we only get here for locally-defined symbols.
588 For section symbols, we should *NOT* be adding
589 sym->st_value (per the definition of the meaning of
590 S in reloc expressions in the ELF64 MIPS ABI),
591 since it should have already been added to
592 reloc_value by the linker, but older versions of
593 GNU ld didn't add it, and newer versions don't emit
594 useless relocations to section symbols any more, so
595 it is safe to keep on adding sym->st_value, even
596 though it's not ABI compliant. Some day we should
597 bite the bullet and stop doing this. */
9285ecc8 598#ifndef RTLD_BOOTSTRAP
f71d7f57 599 if (map != &GL(dl_rtld_map))
9285ecc8 600#endif
10a446dd 601 reloc_value += SYMBOL_ADDRESS (map, sym, true);
9285ecc8
AJ
602 }
603 else
604 {
605#ifndef RTLD_BOOTSTRAP
606 const ElfW(Addr) *got
607 = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
608 const ElfW(Word) local_gotno
609 = (const ElfW(Word))
610 map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
611
0d5b7257 612 reloc_value += got[symidx + local_gotno - gotsym];
9285ecc8
AJ
613#endif
614 }
615 }
616 else
3887a8dc 617#ifndef RTLD_BOOTSTRAP
f71d7f57 618 if (map != &GL(dl_rtld_map))
3887a8dc 619#endif
0d5b7257
AO
620 reloc_value += map->l_addr;
621
622 __builtin_memcpy (reloc_addr, &reloc_value, sizeof (reloc_value));
9285ecc8 623 }
3887a8dc 624 break;
cb5bcf74
DJ
625#ifndef RTLD_BOOTSTRAP
626#if _MIPS_SIM == _ABI64
627 case (R_MIPS_64 << 8) | R_MIPS_GLOB_DAT:
628#else
629 case R_MIPS_GLOB_DAT:
630#endif
631 {
632 int symidx = ELFW(R_SYM) (r_info);
633 const ElfW(Word) gotsym
634 = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
635
636 if (__builtin_expect ((ElfW(Word)) symidx >= gotsym, 1))
637 {
638 const ElfW(Addr) *got
639 = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
640 const ElfW(Word) local_gotno
641 = ((const ElfW(Word))
642 map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val);
643
644 ElfW(Addr) reloc_value = got[symidx + local_gotno - gotsym];
645 __builtin_memcpy (reloc_addr, &reloc_value, sizeof (reloc_value));
646 }
647 }
648 break;
649#endif
3887a8dc
RM
650 case R_MIPS_NONE: /* Alright, Wilbur. */
651 break;
63fb881a
DJ
652
653 case R_MIPS_JUMP_SLOT:
654 {
655 struct link_map *sym_map;
656 ElfW(Addr) value;
657
658 /* The addend for a jump slot relocation must always be zero:
659 calls via the PLT always branch to the symbol's address and
660 not to the address plus a non-zero offset. */
661 if (r_addend != 0)
662 _dl_signal_error (0, map->l_name, NULL,
663 "found jump slot relocation with non-zero addend");
664
665 sym_map = RESOLVE_MAP (&sym, version, r_type);
10a446dd 666 value = SYMBOL_ADDRESS (sym_map, sym, true);
63fb881a
DJ
667 *addr_field = value;
668
669 break;
670 }
671
672 case R_MIPS_COPY:
673 {
674 const ElfW(Sym) *const refsym = sym;
675 struct link_map *sym_map;
676 ElfW(Addr) value;
677
678 /* Calculate the address of the symbol. */
679 sym_map = RESOLVE_MAP (&sym, version, r_type);
10a446dd 680 value = SYMBOL_ADDRESS (sym_map, sym, true);
63fb881a
DJ
681
682 if (__builtin_expect (sym == NULL, 0))
683 /* This can happen in trace mode if an object could not be
684 found. */
685 break;
686 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
687 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
688 && GLRO(dl_verbose)))
689 {
690 const char *strtab;
691
692 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
693 _dl_error_printf ("\
694 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
b9375348 695 RTLD_PROGNAME, strtab + refsym->st_name);
63fb881a
DJ
696 }
697 memcpy (reloc_addr, (void *) value,
ef5d8b43
L
698 sym->st_size < refsym->st_size
699 ? sym->st_size : refsym->st_size);
63fb881a
DJ
700 break;
701 }
702
b8ddf7a1 703#if _MIPS_SIM == _ABI64
0d5b7257
AO
704 case R_MIPS_64:
705 /* For full compliance with the ELF64 ABI, one must precede the
706 _REL32/_64 pair of relocations with a _64 relocation, such
707 that the in-place addend is read as a 64-bit value. IRIX
708 didn't pick up on this requirement, so we treat the
709 _REL32/_64 relocation as a 64-bit relocation even if it's by
710 itself. For ABI compliance, we ignore such _64 dummy
711 relocations. For RELA, this may be simply removed, since
712 it's totally unnecessary. */
cb5bcf74 713 if (ELFW(R_SYM) (r_info) == 0)
0d5b7257 714 break;
0d5b7257 715#endif
e0cb7b61 716 /* Fall through. */
3887a8dc 717 default:
85bdccdb 718 _dl_reloc_bad_type (map, r_type, 0);
3887a8dc
RM
719 break;
720 }
721}
722
cb5bcf74
DJ
723/* Perform the relocation specified by RELOC and SYM (which is fully resolved).
724 MAP is the object containing the reloc. */
725
726auto inline void
727__attribute__ ((always_inline))
728elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
729 const ElfW(Sym) *sym, const struct r_found_version *version,
9a7e1091 730 void *const reloc_addr, int skip_ifunc)
cb5bcf74
DJ
731{
732 elf_machine_reloc (map, reloc->r_info, sym, version, reloc_addr, 0, 1);
733}
734
bd9fad09
AJ
735auto inline void
736__attribute__((always_inline))
335e9a00 737elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
5027ae10 738 void *const reloc_addr)
85bdccdb
UD
739{
740 /* XXX Nothing to do. There is no relative relocation, right? */
741}
742
bd9fad09
AJ
743auto inline void
744__attribute__((always_inline))
893f3d10 745elf_machine_lazy_rel (struct link_map *map,
9a7e1091
AS
746 ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
747 int skip_ifunc)
3887a8dc 748{
63fb881a
DJ
749 ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
750 const unsigned int r_type = ELFW(R_TYPE) (reloc->r_info);
751 /* Check for unexpected PLT reloc type. */
752 if (__builtin_expect (r_type == R_MIPS_JUMP_SLOT, 1))
753 {
754 if (__builtin_expect (map->l_mach.plt, 0) == 0)
755 {
756 /* Nothing is required here since we only support lazy
757 relocation in executables. */
758 }
759 else
760 *reloc_addr = map->l_mach.plt;
761 }
762 else
763 _dl_reloc_bad_type (map, r_type, 1);
3887a8dc
RM
764}
765
bd9fad09
AJ
766auto inline void
767__attribute__ ((always_inline))
768elf_machine_rela (struct link_map *map, const ElfW(Rela) *reloc,
769 const ElfW(Sym) *sym, const struct r_found_version *version,
9a7e1091 770 void *const reloc_addr, int skip_ifunc)
bd9fad09 771{
cb5bcf74
DJ
772 elf_machine_reloc (map, reloc->r_info, sym, version, reloc_addr,
773 reloc->r_addend, 0);
bd9fad09
AJ
774}
775
776auto inline void
777__attribute__((always_inline))
778elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
779 void *const reloc_addr)
780{
781}
782
760ab284 783#ifndef RTLD_BOOTSTRAP
bab23124 784/* Relocate GOT. */
bd9fad09
AJ
785auto inline void
786__attribute__((always_inline))
bab23124
AJ
787elf_machine_got_rel (struct link_map *map, int lazy)
788{
789 ElfW(Addr) *got;
790 ElfW(Sym) *sym;
760ab284 791 const ElfW(Half) *vernum;
12c4efc1 792 int i, n, symidx;
760ab284 793
63fb881a 794#define RESOLVE_GOTSYM(sym,vernum,sym_index,reloc) \
127035bb
AJ
795 ({ \
796 const ElfW(Sym) *ref = sym; \
c9c253d3 797 const struct r_found_version *version __attribute__ ((unused)) \
9a7e1091 798 = vernum ? &map->l_versions[vernum[sym_index] & 0x7fff] : NULL; \
bd9fad09 799 struct link_map *sym_map; \
63fb881a 800 sym_map = RESOLVE_MAP (&ref, version, reloc); \
10a446dd 801 SYMBOL_ADDRESS (sym_map, ref, true); \
bab23124 802 })
760ab284
UD
803
804 if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
805 vernum = (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
806 else
807 vernum = NULL;
bab23124 808
f5a37cdb 809 got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
bab23124 810
bab23124 811 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
d9c1416a 812 /* The dynamic linker's local got entries have already been relocated. */
f71d7f57 813 if (map != &GL(dl_rtld_map))
043351e0 814 {
d9c1416a
AJ
815 /* got[0] is reserved. got[1] is also reserved for the dynamic object
816 generated by gnu ld. Skip these reserved entries from relocation. */
817 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1;
818
9285ecc8 819 /* Add the run-time displacement to all local got entries if
9a7e1091 820 needed. */
d9c1416a
AJ
821 if (__builtin_expect (map->l_addr != 0, 0))
822 {
823 while (i < n)
824 got[i++] += map->l_addr;
825 }
043351e0 826 }
4f203842 827
bab23124
AJ
828 /* Handle global got entries. */
829 got += n;
127035bb
AJ
830 /* Keep track of the symbol index. */
831 symidx = map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
245bff94 832 sym = (ElfW(Sym) *) D_PTR (map, l_info[DT_SYMTAB]) + symidx;
bab23124
AJ
833 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
834 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
4f203842
AJ
835
836 /* This loop doesn't handle Quickstart. */
bab23124
AJ
837 while (i--)
838 {
839 if (sym->st_shndx == SHN_UNDEF)
840 {
63fb881a
DJ
841 if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC && sym->st_value
842 && !(sym->st_other & STO_MIPS_PLT))
843 {
844 if (lazy)
10a446dd 845 *got = SYMBOL_ADDRESS (map, sym, true);
63fb881a
DJ
846 else
847 /* This is a lazy-binding stub, so we don't need the
848 canonical address. */
849 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_JUMP_SLOT);
850 }
4f203842 851 else
63fb881a 852 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
bab23124
AJ
853 }
854 else if (sym->st_shndx == SHN_COMMON)
63fb881a 855 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
bab23124 856 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
63fb881a
DJ
857 && *got != sym->st_value)
858 {
859 if (lazy)
860 *got += map->l_addr;
861 else
862 /* This is a lazy-binding stub, so we don't need the
863 canonical address. */
864 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_JUMP_SLOT);
865 }
bab23124
AJ
866 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)
867 {
868 if (sym->st_other == 0)
869 *got += map->l_addr;
870 }
871 else
63fb881a 872 *got = RESOLVE_GOTSYM (sym, vernum, symidx, R_MIPS_32);
bab23124 873
12c4efc1
AJ
874 ++got;
875 ++sym;
876 ++symidx;
bab23124
AJ
877 }
878
879#undef RESOLVE_GOTSYM
bab23124 880}
760ab284 881#endif
bab23124
AJ
882
883/* Set up the loaded object described by L so its stub function
5719e666 884 will jump to the on-demand fixup code __dl_runtime_resolve. */
bab23124 885
bd9fad09
AJ
886auto inline int
887__attribute__((always_inline))
bab23124
AJ
888elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
889{
890# ifndef RTLD_BOOTSTRAP
891 ElfW(Addr) *got;
892 extern void _dl_runtime_resolve (ElfW(Word));
63fb881a 893 extern void _dl_runtime_pltresolve (void);
bab23124
AJ
894 extern int _dl_mips_gnu_objects;
895
896 if (lazy)
897 {
898 /* The GOT entries for functions have not yet been filled in.
899 Their initial contents will arrange when called to put an
900 offset into the .dynsym section in t8, the return address
901 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
f5a37cdb 902 got = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
bab23124
AJ
903
904 /* This function will get called to fix up the GOT entry indicated by
905 the register t8, and then jump to the resolved address. */
906 got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
907
908 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
909 of got[1] of a gnu object is set to identify gnu objects.
910 Where we can store l for non gnu objects? XXX */
911 if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
3d06657c 912 got[1] = ((ElfW(Addr)) l | ELF_MIPS_GNU_GOT1_MASK);
bab23124
AJ
913 else
914 _dl_mips_gnu_objects = 0;
915 }
916
917 /* Relocate global offset table. */
918 elf_machine_got_rel (l, lazy);
919
63fb881a
DJ
920 /* If using PLTs, fill in the first two entries of .got.plt. */
921 if (l->l_info[DT_JMPREL] && lazy)
922 {
923 ElfW(Addr) *gotplt;
924 gotplt = (ElfW(Addr) *) D_PTR (l, l_info[DT_MIPS (PLTGOT)]);
925 /* If a library is prelinked but we have to relocate anyway,
926 we have to be able to undo the prelinking of .got.plt.
927 The prelinker saved the address of .plt for us here. */
928 if (gotplt[1])
929 l->l_mach.plt = gotplt[1] + l->l_addr;
930 gotplt[0] = (ElfW(Addr)) &_dl_runtime_pltresolve;
931 gotplt[1] = (ElfW(Addr)) l;
932 }
933
bab23124
AJ
934# endif
935 return lazy;
936}
937
bd9fad09 938#endif /* RESOLVE_MAP */