1 /* PLT trampoline. MIPS version.
2 Copyright (C) 1996-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 /* FIXME: Profiling of shared libraries is not implemented yet. */
25 #include <dl-machine.h>
26 #include <sysdep-cancel.h>
28 /* Get link map for callers object containing STUB_PC. */
29 static inline struct link_map *
30 elf_machine_runtime_link_map (ElfW(Addr) gpreg, ElfW(Addr) stub_pc)
32 extern int _dl_mips_gnu_objects;
34 /* got[1] is reserved to keep its link map address for the shared
35 object generated by the gnu linker. If all are such objects, we
36 can find the link map from current GPREG simply. If not so, get
37 the link map for caller's object containing STUB_PC. */
39 if (_dl_mips_gnu_objects)
41 ElfW(Addr) *got = elf_mips_got_from_gpreg (gpreg);
44 g1 = ((ElfW(Word) *) got)[1];
46 if ((g1 & ELF_MIPS_GNU_GOT1_MASK) != 0)
49 (struct link_map *) (g1 & ~ELF_MIPS_GNU_GOT1_MASK);
50 ElfW(Addr) base, limit;
51 const ElfW(Phdr) *p = l->l_phdr;
52 ElfW(Half) this, nent = l->l_phnum;
54 /* For the common case of a stub being called from the containing
55 object, STUB_PC will point to somewhere within the object that
56 is described by the link map fetched via got[1]. Otherwise we
57 have to scan all maps. */
58 for (this = 0; this < nent; this++)
60 if (p[this].p_type == PT_LOAD)
62 base = p[this].p_vaddr + l->l_addr;
63 limit = base + p[this].p_memsz;
64 if (stub_pc >= base && stub_pc < limit)
74 for (nsid = 0; nsid < DL_NNS; ++nsid)
75 for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
77 ElfW(Addr) base, limit;
78 const ElfW(Phdr) *p = l->l_phdr;
79 ElfW(Half) this, nent = l->l_phnum;
81 for (this = 0; this < nent; ++this)
83 if (p[this].p_type == PT_LOAD)
85 base = p[this].p_vaddr + l->l_addr;
86 limit = base + p[this].p_memsz;
87 if (stub_pc >= base && stub_pc < limit)
93 _dl_signal_error (0, NULL, NULL, "cannot find runtime link map");
97 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
98 is called from assembler function _dl_runtime_resolve which converts
99 special argument registers t7 ($15) and t8 ($24):
100 t7 address to return to the caller of the function
101 t8 index for this function symbol in .dynsym
102 to usual c arguments.
104 Other architectures call fixup from dl-runtime.c in
105 _dl_runtime_resolve. MIPS instead calls __dl_runtime_resolve. We
106 have to use our own version because of the way the got section is
107 treated on MIPS (we've also got ELF_MACHINE_PLT defined). */
109 /* The flag _dl_mips_gnu_objects is set if all dynamic objects are
110 generated by the gnu linker. */
111 int _dl_mips_gnu_objects = 1;
113 /* This is called from assembly stubs below which the compiler can't see. */
115 __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr))
119 __dl_runtime_resolve (ElfW(Word) sym_index,
120 ElfW(Word) return_address,
121 ElfW(Addr) old_gpreg,
124 struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc);
125 const ElfW(Sym) *const symtab
126 = (const ElfW(Sym) *) D_PTR (l, l_info[DT_SYMTAB]);
127 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
129 = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
130 const ElfW(Word) local_gotno
131 = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
132 const ElfW(Word) gotsym
133 = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
134 const ElfW(Sym) *sym = &symtab[sym_index];
135 struct link_map *sym_map;
138 /* FIXME: The symbol versioning stuff is not tested yet. */
139 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
141 switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL ? 1 : 0)
145 const ElfW(Half) *vernum =
146 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
147 ElfW(Half) ndx = vernum[sym_index] & 0x7fff;
148 const struct r_found_version *version = &l->l_versions[ndx];
150 if (version->hash != 0)
152 /* We need to keep the scope around so do some locking. This is
153 not necessary for objects which cannot be unloaded or when
154 we are not using any threads (yet). */
155 if (!RTLD_SINGLE_THREAD_P)
156 THREAD_GSCOPE_SET_FLAG ();
158 sym_map = _dl_lookup_symbol_x (strtab + sym->st_name, l,
159 &sym, l->l_scope, version,
160 ELF_RTYPE_CLASS_PLT, 0, 0);
162 /* We are done with the global scope. */
163 if (!RTLD_SINGLE_THREAD_P)
164 THREAD_GSCOPE_RESET_FLAG ();
172 /* We need to keep the scope around so do some locking. This is
173 not necessary for objects which cannot be unloaded or when
174 we are not using any threads (yet). */
175 int flags = DL_LOOKUP_ADD_DEPENDENCY;
176 if (!RTLD_SINGLE_THREAD_P)
178 THREAD_GSCOPE_SET_FLAG ();
179 flags |= DL_LOOKUP_GSCOPE_LOCK;
182 sym_map = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
183 l->l_scope, 0, ELF_RTYPE_CLASS_PLT,
186 /* We are done with the global scope. */
187 if (!RTLD_SINGLE_THREAD_P)
188 THREAD_GSCOPE_RESET_FLAG ();
192 /* Currently value contains the base load address of the object
193 that defines sym. Now add in the symbol offset. */
194 value = SYMBOL_ADDRESS (sym_map, sym, true);
197 /* We already found the symbol. The module (and therefore its load
198 address) is also known. */
199 value = SYMBOL_ADDRESS (l, sym, true);
201 /* Apply the relocation with that value. */
202 *(got + local_gotno + sym_index - gotsym) = value;
207 #if _MIPS_SIM == _ABIO32
208 #define ELF_DL_FRAME_SIZE 40
210 #define ELF_DL_SAVE_ARG_REGS "\
218 #define ELF_DL_RESTORE_ARG_REGS "\
226 /* The PLT resolver should also save and restore $2 and $3, which are used
227 as arguments to MIPS16 stub functions. */
228 #define ELF_DL_PLT_FRAME_SIZE 48
230 #define ELF_DL_PLT_SAVE_ARG_REGS \
231 ELF_DL_SAVE_ARG_REGS "\
236 #define ELF_DL_PLT_RESTORE_ARG_REGS \
237 ELF_DL_RESTORE_ARG_REGS "\
242 #define IFABIO32(X) X
245 #else /* _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64 */
247 #define ELF_DL_FRAME_SIZE 80
249 #define ELF_DL_SAVE_ARG_REGS "\
261 #define ELF_DL_RESTORE_ARG_REGS "\
273 /* The PLT resolver should also save and restore $2 and $3, which are used
274 as arguments to MIPS16 stub functions. */
275 #define ELF_DL_PLT_FRAME_SIZE 96
277 #define ELF_DL_PLT_SAVE_ARG_REGS \
278 ELF_DL_SAVE_ARG_REGS "\
283 #define ELF_DL_PLT_RESTORE_ARG_REGS \
284 ELF_DL_RESTORE_ARG_REGS "\
290 #define IFNEWABI(X) X
299 .globl _dl_runtime_resolve\n\
300 .type _dl_runtime_resolve,@function\n\
301 .ent _dl_runtime_resolve\n\
302 _dl_runtime_resolve:\n\
303 .frame $29, " STRINGXP(ELF_DL_FRAME_SIZE) ", $31\n\
307 # Save arguments and sp value in stack.\n\
308 " STRINGXP(PTR_SUBIU) " $29, " STRINGXP(ELF_DL_FRAME_SIZE) "\n\
309 # Modify t9 ($25) so as to point .cpload instruction.\n\
310 " IFABIO32(STRINGXP(PTR_ADDIU) " $25, (2f-1b)\n") "\
312 2: " STRINGXP(SETUP_GP) "\n\
313 " STRINGXV(SETUP_GP64 (0, _dl_runtime_resolve)) "\n\
315 # Save slot call pc.\n\
317 " IFABIO32(STRINGXP(CPRESTORE(32))) "\n\
318 " ELF_DL_SAVE_ARG_REGS "\
323 jal __dl_runtime_resolve\n\
324 " ELF_DL_RESTORE_ARG_REGS "\
325 " STRINGXP(RESTORE_GP64) "\n\
326 " STRINGXP(PTR_ADDIU) " $29, " STRINGXP(ELF_DL_FRAME_SIZE) "\n\
329 .end _dl_runtime_resolve\n\
333 /* Assembler veneer called from the PLT header code when using PLTs.
335 Code in each PLT entry and the PLT header fills in the arguments to
338 - $15 (o32 t7, n32/n64 t3) - caller's return address
339 - $24 (t8) - PLT entry index
340 - $25 (t9) - address of _dl_runtime_pltresolve
341 - o32 $28 (gp), n32/n64 $14 (t2) - address of .got.plt
343 Different registers are used for .got.plt because the ABI was
344 originally designed for o32, where gp was available (call
345 clobbered). On n32/n64 gp is call saved.
349 - $4 (a0) - link map address
350 - $5 (a1) - .rel.plt offset (== PLT entry index * 8) */
356 .globl _dl_runtime_pltresolve\n\
357 .type _dl_runtime_pltresolve,@function\n\
358 .ent _dl_runtime_pltresolve\n\
359 _dl_runtime_pltresolve:\n\
360 .frame $29, " STRINGXP(ELF_DL_PLT_FRAME_SIZE) ", $31\n\
362 # Save arguments and sp value in stack.\n\
363 1: " STRINGXP(PTR_SUBIU) " $29, " STRINGXP(ELF_DL_PLT_FRAME_SIZE) "\n\
364 " IFABIO32(STRINGXP(PTR_L) " $13, " STRINGXP(PTRSIZE) "($28)") "\n\
365 " IFNEWABI(STRINGXP(PTR_L) " $13, " STRINGXP(PTRSIZE) "($14)") "\n\
366 # Modify t9 ($25) so as to point .cpload instruction.\n\
367 " IFABIO32(STRINGXP(PTR_ADDIU) " $25, (2f-1b)\n") "\
369 2: " STRINGXP(SETUP_GP) "\n\
370 " STRINGXV(SETUP_GP64 (0, _dl_runtime_pltresolve)) "\n\
372 " IFABIO32(STRINGXP(CPRESTORE(32))) "\n\
373 " ELF_DL_PLT_SAVE_ARG_REGS "\
375 sll $5, $24, " STRINGXP(PTRLOG) " + 1\n\
378 " ELF_DL_PLT_RESTORE_ARG_REGS "\
379 " STRINGXP(RESTORE_GP64) "\n\
380 " STRINGXP(PTR_ADDIU) " $29, " STRINGXP(ELF_DL_PLT_FRAME_SIZE) "\n\
382 .end _dl_runtime_pltresolve\n\
386 #elif _MIPS_SIM == _ABIO32 /* __mips16 */
387 /* MIPS16 version, O32 only. */
392 .globl _dl_runtime_resolve\n\
393 .type _dl_runtime_resolve,@function\n\
394 .ent _dl_runtime_resolve\n\
395 _dl_runtime_resolve:\n\
396 .frame $29, " STRINGXP (ELF_DL_FRAME_SIZE) ", $31\n\
397 # Save arguments and sp value in stack.\n\t"
398 # if _MIPS_ISA >= _MIPS_ISA_MIPS32
399 "save " STRINGXP (ELF_DL_FRAME_SIZE) ", $4-$7, $ra\n\t"
401 "addiu $sp, -" STRINGXP (ELF_DL_FRAME_SIZE) "\n\
407 "# Preserve caller's $ra, for RESTORE instruction below.\n\
410 # Compute GP into $2.\n\
411 li $2, %hi(_gp_disp)\n\
412 addiu $3, $pc, %lo(_gp_disp)\n\
415 lw $3, %got(__dl_runtime_resolve)($2)\n\
417 addiu $3, %lo(__dl_runtime_resolve)\n\
422 # if _MIPS_ISA >= _MIPS_ISA_MIPS32
423 "restore " STRINGXP(ELF_DL_FRAME_SIZE) ", $4-$7, $ra\n\t"
425 "# Restore $ra, move placed further down to hide latency.\n\
432 addiu $sp, " STRINGXP(ELF_DL_FRAME_SIZE) "\n\t"
436 .end _dl_runtime_resolve\n\
444 .globl _dl_runtime_pltresolve\n\
445 .type _dl_runtime_pltresolve,@function\n\
446 .ent _dl_runtime_pltresolve\n\
447 _dl_runtime_pltresolve:\n\
448 .frame $29, " STRINGXP(ELF_DL_PLT_FRAME_SIZE) ", $31\n\
449 # Save arguments and sp value in stack.\n\t"
450 # if _MIPS_ISA >= _MIPS_ISA_MIPS32
451 "save " STRINGXP(ELF_DL_PLT_FRAME_SIZE) ", $4-$7, $ra\n\t"
453 "addiu $sp, -" STRINGXP(ELF_DL_PLT_FRAME_SIZE) "\n\
459 "# Preserve MIPS16 stub function arguments.\n\
462 # Preserve caller's $ra, for RESTORE instruction below.\n\
465 # Compute GP into $2.\n\
466 li $2, %hi(_gp_disp)\n\
467 addiu $3, $pc, %lo(_gp_disp)\n\
470 # Save GP value in slot.\n\
472 # Load _dl_fixup address.\n\
473 lw $6, %call16(_dl_fixup)($2)\n\
474 # Load link map address.\n\
476 lw $4, " STRINGXP (PTRSIZE) "($3)\n\
478 sll $5, " STRINGXP (PTRLOG) " + 1\n\
483 # Reload GP value into $28.\n\
489 # if _MIPS_ISA >= _MIPS_ISA_MIPS32
490 "restore " STRINGXP (ELF_DL_PLT_FRAME_SIZE) ", $4-$7, $ra\n\t"
492 "# Restore $ra, move placed further down to hide latency.\n\
499 addiu $sp, " STRINGXP (ELF_DL_PLT_FRAME_SIZE) "\n\t"
505 .end _dl_runtime_pltresolve\n\
509 #else /* __mips16 && _MIPS_SIM != _ABIO32 */
510 # error "MIPS16 support for N32/N64 not implemented"
512 #endif /* __mips16 */