dl-trampoline.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /* PLT trampoline. MIPS version.
  2. Copyright (C) 1996-2019 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>.
  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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library. If not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* FIXME: Profiling of shared libraries is not implemented yet. */
  17. #include <sysdep.h>
  18. #include <link.h>
  19. #include <elf.h>
  20. #include <ldsodefs.h>
  21. #include <dl-machine.h>
  22. #include <sysdep-cancel.h>
  23. /* Get link map for callers object containing STUB_PC. */
  24. static inline struct link_map *
  25. elf_machine_runtime_link_map (ElfW(Addr) gpreg, ElfW(Addr) stub_pc)
  26. {
  27. extern int _dl_mips_gnu_objects;
  28. /* got[1] is reserved to keep its link map address for the shared
  29. object generated by the gnu linker. If all are such objects, we
  30. can find the link map from current GPREG simply. If not so, get
  31. the link map for caller's object containing STUB_PC. */
  32. if (_dl_mips_gnu_objects)
  33. {
  34. ElfW(Addr) *got = elf_mips_got_from_gpreg (gpreg);
  35. ElfW(Word) g1;
  36. g1 = ((ElfW(Word) *) got)[1];
  37. if ((g1 & ELF_MIPS_GNU_GOT1_MASK) != 0)
  38. {
  39. struct link_map *l =
  40. (struct link_map *) (g1 & ~ELF_MIPS_GNU_GOT1_MASK);
  41. ElfW(Addr) base, limit;
  42. const ElfW(Phdr) *p = l->l_phdr;
  43. ElfW(Half) this, nent = l->l_phnum;
  44. /* For the common case of a stub being called from the containing
  45. object, STUB_PC will point to somewhere within the object that
  46. is described by the link map fetched via got[1]. Otherwise we
  47. have to scan all maps. */
  48. for (this = 0; this < nent; this++)
  49. {
  50. if (p[this].p_type == PT_LOAD)
  51. {
  52. base = p[this].p_vaddr + l->l_addr;
  53. limit = base + p[this].p_memsz;
  54. if (stub_pc >= base && stub_pc < limit)
  55. return l;
  56. }
  57. }
  58. }
  59. }
  60. struct link_map *l;
  61. Lmid_t nsid;
  62. for (nsid = 0; nsid < DL_NNS; ++nsid)
  63. for (l = GL(dl_ns)[nsid]._ns_loaded; l != NULL; l = l->l_next)
  64. {
  65. ElfW(Addr) base, limit;
  66. const ElfW(Phdr) *p = l->l_phdr;
  67. ElfW(Half) this, nent = l->l_phnum;
  68. for (this = 0; this < nent; ++this)
  69. {
  70. if (p[this].p_type == PT_LOAD)
  71. {
  72. base = p[this].p_vaddr + l->l_addr;
  73. limit = base + p[this].p_memsz;
  74. if (stub_pc >= base && stub_pc < limit)
  75. return l;
  76. }
  77. }
  78. }
  79. _dl_signal_error (0, NULL, NULL, "cannot find runtime link map");
  80. return NULL;
  81. }
  82. /* Define mips specific runtime resolver. The function __dl_runtime_resolve
  83. is called from assembler function _dl_runtime_resolve which converts
  84. special argument registers t7 ($15) and t8 ($24):
  85. t7 address to return to the caller of the function
  86. t8 index for this function symbol in .dynsym
  87. to usual c arguments.
  88. Other architectures call fixup from dl-runtime.c in
  89. _dl_runtime_resolve. MIPS instead calls __dl_runtime_resolve. We
  90. have to use our own version because of the way the got section is
  91. treated on MIPS (we've also got ELF_MACHINE_PLT defined). */
  92. /* The flag _dl_mips_gnu_objects is set if all dynamic objects are
  93. generated by the gnu linker. */
  94. int _dl_mips_gnu_objects = 1;
  95. /* This is called from assembly stubs below which the compiler can't see. */
  96. static ElfW(Addr)
  97. __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr))
  98. __attribute_used__;
  99. static ElfW(Addr)
  100. __dl_runtime_resolve (ElfW(Word) sym_index,
  101. ElfW(Word) return_address,
  102. ElfW(Addr) old_gpreg,
  103. ElfW(Addr) stub_pc)
  104. {
  105. struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc);
  106. const ElfW(Sym) *const symtab
  107. = (const ElfW(Sym) *) D_PTR (l, l_info[DT_SYMTAB]);
  108. const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
  109. ElfW(Addr) *got
  110. = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
  111. const ElfW(Word) local_gotno
  112. = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
  113. const ElfW(Word) gotsym
  114. = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
  115. const ElfW(Sym) *sym = &symtab[sym_index];
  116. struct link_map *sym_map;
  117. ElfW(Addr) value;
  118. /* FIXME: The symbol versioning stuff is not tested yet. */
  119. if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
  120. {
  121. switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL ? 1 : 0)
  122. {
  123. default:
  124. {
  125. const ElfW(Half) *vernum =
  126. (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
  127. ElfW(Half) ndx = vernum[sym_index] & 0x7fff;
  128. const struct r_found_version *version = &l->l_versions[ndx];
  129. if (version->hash != 0)
  130. {
  131. /* We need to keep the scope around so do some locking. This is
  132. not necessary for objects which cannot be unloaded or when
  133. we are not using any threads (yet). */
  134. if (!RTLD_SINGLE_THREAD_P)
  135. THREAD_GSCOPE_SET_FLAG ();
  136. sym_map = _dl_lookup_symbol_x (strtab + sym->st_name, l,
  137. &sym, l->l_scope, version,
  138. ELF_RTYPE_CLASS_PLT, 0, 0);
  139. /* We are done with the global scope. */
  140. if (!RTLD_SINGLE_THREAD_P)
  141. THREAD_GSCOPE_RESET_FLAG ();
  142. break;
  143. }
  144. /* Fall through. */
  145. }
  146. case 0:
  147. {
  148. /* We need to keep the scope around so do some locking. This is
  149. not necessary for objects which cannot be unloaded or when
  150. we are not using any threads (yet). */
  151. int flags = DL_LOOKUP_ADD_DEPENDENCY;
  152. if (!RTLD_SINGLE_THREAD_P)
  153. {
  154. THREAD_GSCOPE_SET_FLAG ();
  155. flags |= DL_LOOKUP_GSCOPE_LOCK;
  156. }
  157. sym_map = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
  158. l->l_scope, 0, ELF_RTYPE_CLASS_PLT,
  159. flags, 0);
  160. /* We are done with the global scope. */
  161. if (!RTLD_SINGLE_THREAD_P)
  162. THREAD_GSCOPE_RESET_FLAG ();
  163. }
  164. }
  165. /* Currently value contains the base load address of the object
  166. that defines sym. Now add in the symbol offset. */
  167. value = SYMBOL_ADDRESS (sym_map, sym, true);
  168. }
  169. else
  170. /* We already found the symbol. The module (and therefore its load
  171. address) is also known. */
  172. value = SYMBOL_ADDRESS (l, sym, true);
  173. /* Apply the relocation with that value. */
  174. *(got + local_gotno + sym_index - gotsym) = value;
  175. return value;
  176. }
  177. #if _MIPS_SIM == _ABIO32
  178. #define ELF_DL_FRAME_SIZE 40
  179. #define ELF_DL_SAVE_ARG_REGS "\
  180. sw $15, 36($29)\n \
  181. sw $4, 16($29)\n \
  182. sw $5, 20($29)\n \
  183. sw $6, 24($29)\n \
  184. sw $7, 28($29)\n \
  185. "
  186. #define ELF_DL_RESTORE_ARG_REGS "\
  187. lw $31, 36($29)\n \
  188. lw $4, 16($29)\n \
  189. lw $5, 20($29)\n \
  190. lw $6, 24($29)\n \
  191. lw $7, 28($29)\n \
  192. "
  193. /* The PLT resolver should also save and restore $2 and $3, which are used
  194. as arguments to MIPS16 stub functions. */
  195. #define ELF_DL_PLT_FRAME_SIZE 48
  196. #define ELF_DL_PLT_SAVE_ARG_REGS \
  197. ELF_DL_SAVE_ARG_REGS "\
  198. sw $2, 40($29)\n \
  199. sw $3, 44($29)\n \
  200. "
  201. #define ELF_DL_PLT_RESTORE_ARG_REGS \
  202. ELF_DL_RESTORE_ARG_REGS "\
  203. lw $2, 40($29)\n \
  204. lw $3, 44($29)\n \
  205. "
  206. #define IFABIO32(X) X
  207. #define IFNEWABI(X)
  208. #else /* _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64 */
  209. #define ELF_DL_FRAME_SIZE 80
  210. #define ELF_DL_SAVE_ARG_REGS "\
  211. sd $15, 72($29)\n \
  212. sd $4, 8($29)\n \
  213. sd $5, 16($29)\n \
  214. sd $6, 24($29)\n \
  215. sd $7, 32($29)\n \
  216. sd $8, 40($29)\n \
  217. sd $9, 48($29)\n \
  218. sd $10, 56($29)\n \
  219. sd $11, 64($29)\n \
  220. "
  221. #define ELF_DL_RESTORE_ARG_REGS "\
  222. ld $31, 72($29)\n \
  223. ld $4, 8($29)\n \
  224. ld $5, 16($29)\n \
  225. ld $6, 24($29)\n \
  226. ld $7, 32($29)\n \
  227. ld $8, 40($29)\n \
  228. ld $9, 48($29)\n \
  229. ld $10, 56($29)\n \
  230. ld $11, 64($29)\n \
  231. "
  232. /* The PLT resolver should also save and restore $2 and $3, which are used
  233. as arguments to MIPS16 stub functions. */
  234. #define ELF_DL_PLT_FRAME_SIZE 96
  235. #define ELF_DL_PLT_SAVE_ARG_REGS \
  236. ELF_DL_SAVE_ARG_REGS "\
  237. sd $2, 80($29)\n \
  238. sd $3, 88($29)\n \
  239. "
  240. #define ELF_DL_PLT_RESTORE_ARG_REGS \
  241. ELF_DL_RESTORE_ARG_REGS "\
  242. ld $2, 80($29)\n \
  243. ld $3, 88($29)\n \
  244. "
  245. #define IFABIO32(X)
  246. #define IFNEWABI(X) X
  247. #endif
  248. #ifndef __mips16
  249. asm ("\n\
  250. .text\n\
  251. .align 2\n\
  252. .set nomips16\n\
  253. .globl _dl_runtime_resolve\n\
  254. .type _dl_runtime_resolve,@function\n\
  255. .ent _dl_runtime_resolve\n\
  256. _dl_runtime_resolve:\n\
  257. .frame $29, " STRINGXP(ELF_DL_FRAME_SIZE) ", $31\n\
  258. .set noreorder\n\
  259. # Save GP.\n\
  260. 1: move $3, $28\n\
  261. # Save arguments and sp value in stack.\n\
  262. " STRINGXP(PTR_SUBIU) " $29, " STRINGXP(ELF_DL_FRAME_SIZE) "\n\
  263. # Modify t9 ($25) so as to point .cpload instruction.\n\
  264. " IFABIO32(STRINGXP(PTR_ADDIU) " $25, (2f-1b)\n") "\
  265. # Compute GP.\n\
  266. 2: " STRINGXP(SETUP_GP) "\n\
  267. " STRINGXV(SETUP_GP64 (0, _dl_runtime_resolve)) "\n\
  268. .set reorder\n\
  269. # Save slot call pc.\n\
  270. move $2, $31\n\
  271. " IFABIO32(STRINGXP(CPRESTORE(32))) "\n\
  272. " ELF_DL_SAVE_ARG_REGS "\
  273. move $4, $24\n\
  274. move $5, $15\n\
  275. move $6, $3\n\
  276. move $7, $2\n\
  277. jal __dl_runtime_resolve\n\
  278. " ELF_DL_RESTORE_ARG_REGS "\
  279. " STRINGXP(RESTORE_GP64) "\n\
  280. " STRINGXP(PTR_ADDIU) " $29, " STRINGXP(ELF_DL_FRAME_SIZE) "\n\
  281. move $25, $2\n\
  282. jr $25\n\
  283. .end _dl_runtime_resolve\n\
  284. .previous\n\
  285. ");
  286. /* Assembler veneer called from the PLT header code when using PLTs.
  287. Code in each PLT entry and the PLT header fills in the arguments to
  288. this function:
  289. - $15 (o32 t7, n32/n64 t3) - caller's return address
  290. - $24 (t8) - PLT entry index
  291. - $25 (t9) - address of _dl_runtime_pltresolve
  292. - o32 $28 (gp), n32/n64 $14 (t2) - address of .got.plt
  293. Different registers are used for .got.plt because the ABI was
  294. originally designed for o32, where gp was available (call
  295. clobbered). On n32/n64 gp is call saved.
  296. _dl_fixup needs:
  297. - $4 (a0) - link map address
  298. - $5 (a1) - .rel.plt offset (== PLT entry index * 8) */
  299. asm ("\n\
  300. .text\n\
  301. .align 2\n\
  302. .set nomips16\n\
  303. .globl _dl_runtime_pltresolve\n\
  304. .type _dl_runtime_pltresolve,@function\n\
  305. .ent _dl_runtime_pltresolve\n\
  306. _dl_runtime_pltresolve:\n\
  307. .frame $29, " STRINGXP(ELF_DL_PLT_FRAME_SIZE) ", $31\n\
  308. .set noreorder\n\
  309. # Save arguments and sp value in stack.\n\
  310. 1: " STRINGXP(PTR_SUBIU) " $29, " STRINGXP(ELF_DL_PLT_FRAME_SIZE) "\n\
  311. " IFABIO32(STRINGXP(PTR_L) " $13, " STRINGXP(PTRSIZE) "($28)") "\n\
  312. " IFNEWABI(STRINGXP(PTR_L) " $13, " STRINGXP(PTRSIZE) "($14)") "\n\
  313. # Modify t9 ($25) so as to point .cpload instruction.\n\
  314. " IFABIO32(STRINGXP(PTR_ADDIU) " $25, (2f-1b)\n") "\
  315. # Compute GP.\n\
  316. 2: " STRINGXP(SETUP_GP) "\n\
  317. " STRINGXV(SETUP_GP64 (0, _dl_runtime_pltresolve)) "\n\
  318. .set reorder\n\
  319. " IFABIO32(STRINGXP(CPRESTORE(32))) "\n\
  320. " ELF_DL_PLT_SAVE_ARG_REGS "\
  321. move $4, $13\n\
  322. sll $5, $24, " STRINGXP(PTRLOG) " + 1\n\
  323. jal _dl_fixup\n\
  324. move $25, $2\n\
  325. " ELF_DL_PLT_RESTORE_ARG_REGS "\
  326. " STRINGXP(RESTORE_GP64) "\n\
  327. " STRINGXP(PTR_ADDIU) " $29, " STRINGXP(ELF_DL_PLT_FRAME_SIZE) "\n\
  328. jr $25\n\
  329. .end _dl_runtime_pltresolve\n\
  330. .previous\n\
  331. ");
  332. #elif _MIPS_SIM == _ABIO32 /* __mips16 */
  333. /* MIPS16 version, O32 only. */
  334. asm ("\n\
  335. .text\n\
  336. .align 2\n\
  337. .set mips16\n\
  338. .globl _dl_runtime_resolve\n\
  339. .type _dl_runtime_resolve,@function\n\
  340. .ent _dl_runtime_resolve\n\
  341. _dl_runtime_resolve:\n\
  342. .frame $29, " STRINGXP (ELF_DL_FRAME_SIZE) ", $31\n\
  343. # Save arguments and sp value in stack.\n\t"
  344. # if _MIPS_ISA >= _MIPS_ISA_MIPS32
  345. "save " STRINGXP (ELF_DL_FRAME_SIZE) ", $4-$7, $ra\n\t"
  346. # else
  347. "addiu $sp, -" STRINGXP (ELF_DL_FRAME_SIZE) "\n\
  348. sw $7, 32($sp)\n\
  349. sw $6, 28($sp)\n\
  350. sw $5, 24($sp)\n\
  351. sw $4, 20($sp)\n\t"
  352. # endif
  353. "# Preserve caller's $ra, for RESTORE instruction below.\n\
  354. move $5, $15\n\
  355. sw $5, 36($sp)\n\
  356. # Compute GP into $2.\n\
  357. li $2, %hi(_gp_disp)\n\
  358. addiu $3, $pc, %lo(_gp_disp)\n\
  359. sll $2, 16\n\
  360. addu $2, $3\n\
  361. lw $3, %got(__dl_runtime_resolve)($2)\n\
  362. move $4, $24\n\
  363. addiu $3, %lo(__dl_runtime_resolve)\n\
  364. move $7, $ra\n\
  365. move $6, $28\n\
  366. move $25, $3\n\
  367. jalr $3\n\t"
  368. # if _MIPS_ISA >= _MIPS_ISA_MIPS32
  369. "restore " STRINGXP(ELF_DL_FRAME_SIZE) ", $4-$7, $ra\n\t"
  370. # else
  371. "# Restore $ra, move placed further down to hide latency.\n\
  372. lw $4, 36($sp)\n\
  373. lw $5, 24($sp)\n\
  374. lw $6, 28($sp)\n\
  375. lw $7, 32($sp)\n\
  376. move $ra, $4\n\
  377. lw $4, 20($sp)\n\
  378. addiu $sp, " STRINGXP(ELF_DL_FRAME_SIZE) "\n\t"
  379. # endif
  380. "move $25, $2\n\
  381. jr $2\n\
  382. .end _dl_runtime_resolve\n\
  383. .previous\n\
  384. ");
  385. asm ("\n\
  386. .text\n\
  387. .align 2\n\
  388. .set mips16\n\
  389. .globl _dl_runtime_pltresolve\n\
  390. .type _dl_runtime_pltresolve,@function\n\
  391. .ent _dl_runtime_pltresolve\n\
  392. _dl_runtime_pltresolve:\n\
  393. .frame $29, " STRINGXP(ELF_DL_PLT_FRAME_SIZE) ", $31\n\
  394. # Save arguments and sp value in stack.\n\t"
  395. # if _MIPS_ISA >= _MIPS_ISA_MIPS32
  396. "save " STRINGXP(ELF_DL_PLT_FRAME_SIZE) ", $4-$7, $ra\n\t"
  397. # else
  398. "addiu $sp, -" STRINGXP(ELF_DL_PLT_FRAME_SIZE) "\n\
  399. sw $7, 40($sp)\n\
  400. sw $6, 36($sp)\n\
  401. sw $5, 32($sp)\n\
  402. sw $4, 28($sp)\n\t"
  403. # endif
  404. "# Preserve MIPS16 stub function arguments.\n\
  405. sw $3, 20($sp)\n\
  406. sw $2, 16($sp)\n\
  407. # Preserve caller's $ra, for RESTORE instruction below.\n\
  408. move $3, $15\n\
  409. sw $3, 44($sp)\n\
  410. # Compute GP into $2.\n\
  411. li $2, %hi(_gp_disp)\n\
  412. addiu $3, $pc, %lo(_gp_disp)\n\
  413. sll $2, 16\n\
  414. addu $2, $3\n\
  415. # Save GP value in slot.\n\
  416. sw $2, 24($sp)\n\
  417. # Load _dl_fixup address.\n\
  418. lw $6, %call16(_dl_fixup)($2)\n\
  419. # Load link map address.\n\
  420. move $3, $28\n\
  421. lw $4, " STRINGXP (PTRSIZE) "($3)\n\
  422. move $5, $24\n\
  423. sll $5, " STRINGXP (PTRLOG) " + 1\n\
  424. # Call _dl_fixup.\n\
  425. move $25, $6\n\
  426. jalr $6\n\
  427. move $25, $2\n\
  428. # Reload GP value into $28.\n\
  429. lw $3, 24($sp)\n\
  430. move $28, $3\n\
  431. lw $3, 16($sp)\n\
  432. move $15, $3\n\
  433. lw $3, 20($sp)\n\t"
  434. # if _MIPS_ISA >= _MIPS_ISA_MIPS32
  435. "restore " STRINGXP (ELF_DL_PLT_FRAME_SIZE) ", $4-$7, $ra\n\t"
  436. # else
  437. "# Restore $ra, move placed further down to hide latency.\n\
  438. lw $4, 44($sp)\n\
  439. lw $5, 32($sp)\n\
  440. lw $6, 36($sp)\n\
  441. lw $7, 40($sp)\n\
  442. move $ra, $4\n\
  443. lw $4, 28($sp)\n\
  444. addiu $sp, " STRINGXP (ELF_DL_PLT_FRAME_SIZE) "\n\t"
  445. # endif
  446. ".set noreorder\n\
  447. jr $2\n\
  448. move $2, $15\n\
  449. .set reorder\n\
  450. .end _dl_runtime_pltresolve\n\
  451. .previous\n\
  452. ");
  453. #else /* __mips16 && _MIPS_SIM != _ABIO32 */
  454. # error "MIPS16 support for N32/N64 not implemented"
  455. #endif /* __mips16 */