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