dl-machine.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* Machine-dependent ELF dynamic relocation inline functions. IA-64 version.
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef dl_machine_h
  16. #define dl_machine_h 1
  17. #define ELF_MACHINE_NAME "ia64"
  18. #include <assert.h>
  19. #include <string.h>
  20. #include <link.h>
  21. #include <errno.h>
  22. #include <dl-fptr.h>
  23. #include <tls.h>
  24. /* Translate a processor specific dynamic tag to the index
  25. in l_info array. */
  26. #define DT_IA_64(x) (DT_IA_64_##x - DT_LOPROC + DT_NUM)
  27. static inline void __attribute__ ((always_inline))
  28. __ia64_init_bootstrap_fdesc_table (struct link_map *map)
  29. {
  30. Elf64_Addr *boot_table;
  31. /* careful: this will be called before got has been relocated... */
  32. asm (";; addl %0 = @gprel (_dl_boot_fptr_table), gp" : "=r"(boot_table));
  33. map->l_mach.fptr_table_len = ELF_MACHINE_BOOT_FPTR_TABLE_LEN;
  34. map->l_mach.fptr_table = boot_table;
  35. }
  36. #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
  37. __ia64_init_bootstrap_fdesc_table (BOOTSTRAP_MAP);
  38. /* Return nonzero iff ELF header is compatible with the running host. */
  39. static inline int __attribute__ ((unused))
  40. elf_machine_matches_host (const Elf64_Ehdr *ehdr)
  41. {
  42. return ehdr->e_machine == EM_IA_64;
  43. }
  44. /* Return the link-time address of _DYNAMIC. */
  45. static inline Elf64_Addr __attribute__ ((unused, const))
  46. elf_machine_dynamic (void)
  47. {
  48. Elf64_Addr *p;
  49. __asm__ (
  50. ".section .sdata\n"
  51. " .type __dynamic_ltv#, @object\n"
  52. " .size __dynamic_ltv#, 8\n"
  53. "__dynamic_ltv:\n"
  54. " data8 @ltv(_DYNAMIC#)\n"
  55. ".previous\n"
  56. " addl %0 = @gprel(__dynamic_ltv#), gp ;;"
  57. : "=r" (p));
  58. return *p;
  59. }
  60. /* Return the run-time load address of the shared object. */
  61. static inline Elf64_Addr __attribute__ ((unused))
  62. elf_machine_load_address (void)
  63. {
  64. Elf64_Addr ip;
  65. int *p;
  66. __asm__ (
  67. "1: mov %0 = ip\n"
  68. ".section .sdata\n"
  69. "2: data4 @ltv(1b)\n"
  70. " .align 8\n"
  71. ".previous\n"
  72. " addl %1 = @gprel(2b), gp ;;"
  73. : "=r" (ip), "=r" (p));
  74. return ip - (Elf64_Addr) *p;
  75. }
  76. /* Set up the loaded object described by L so its unrelocated PLT
  77. entries will jump to the on-demand fixup code in dl-runtime.c. */
  78. static inline int __attribute__ ((unused, always_inline))
  79. elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
  80. {
  81. extern void _dl_runtime_resolve (void);
  82. extern void _dl_runtime_profile (void);
  83. if (lazy)
  84. {
  85. register Elf64_Addr gp __asm__ ("gp");
  86. Elf64_Addr *reserve, doit;
  87. /*
  88. * Careful with the typecast here or it will try to add l-l_addr
  89. * pointer elements
  90. */
  91. reserve = ((Elf64_Addr *)
  92. (l->l_info[DT_IA_64 (PLT_RESERVE)]->d_un.d_ptr + l->l_addr));
  93. /* Identify this shared object. */
  94. reserve[0] = (Elf64_Addr) l;
  95. /* This function will be called to perform the relocation. */
  96. if (!profile)
  97. doit = (Elf64_Addr) ELF_PTR_TO_FDESC (&_dl_runtime_resolve)->ip;
  98. else
  99. {
  100. if (GLRO(dl_profile) != NULL
  101. && _dl_name_match_p (GLRO(dl_profile), l))
  102. {
  103. /* This is the object we are looking for. Say that we really
  104. want profiling and the timers are started. */
  105. GL(dl_profile_map) = l;
  106. }
  107. doit = (Elf64_Addr) ELF_PTR_TO_FDESC (&_dl_runtime_profile)->ip;
  108. }
  109. reserve[1] = doit;
  110. reserve[2] = gp;
  111. }
  112. return lazy;
  113. }
  114. /* Names of the architecture-specific auditing callback functions. */
  115. #define ARCH_LA_PLTENTER ia64_gnu_pltenter
  116. #define ARCH_LA_PLTEXIT ia64_gnu_pltexit
  117. /* Undo the adds out0 = 16, sp below to get at the value we want in
  118. __libc_stack_end. */
  119. #define DL_STACK_END(cookie) \
  120. ((void *) (((long) (cookie)) - 16))
  121. /* Initial entry point code for the dynamic linker.
  122. The C function `_dl_start' is the real entry point;
  123. its return value is the user program's entry point. */
  124. #define RTLD_START asm ( \
  125. ".text\n" \
  126. " .global _start#\n" \
  127. " .proc _start#\n" \
  128. "_start:\n" \
  129. "0: { .mii\n" \
  130. " .prologue\n" \
  131. " .save rp, r0\n" \
  132. " .body\n" \
  133. " .prologue\n" \
  134. " .save ar.pfs, r32\n" \
  135. " alloc loc0 = ar.pfs, 0, 3, 4, 0\n" \
  136. " .body\n" \
  137. " mov r2 = ip\n" \
  138. " addl r3 = @gprel(0b), r0\n" \
  139. " ;;\n" \
  140. " }\n" \
  141. " { .mlx\n" \
  142. " /* Calculate the GP, and save a copy in loc1. */\n" \
  143. " sub gp = r2, r3\n" \
  144. " movl r8 = 0x9804c0270033f\n" \
  145. " ;;\n" \
  146. " }\n" \
  147. " { .mii\n" \
  148. " mov ar.fpsr = r8\n" \
  149. " sub loc1 = r2, r3\n" \
  150. " /* _dl_start wants a pointer to the pointer to the arg block and\n" \
  151. " the arg block starts with an integer, thus the magic 16. */\n" \
  152. " adds out0 = 16, sp\n" \
  153. " }\n" \
  154. " { .bbb\n" \
  155. " br.call.sptk.many b0 = _dl_start#\n" \
  156. " ;;\n" \
  157. " }\n" \
  158. " .endp _start#\n" \
  159. " /* FALLTHRU */\n" \
  160. " .global _dl_start_user#\n" \
  161. " .proc _dl_start_user#\n" \
  162. "_dl_start_user:\n" \
  163. " .prologue\n" \
  164. " .save rp, r0\n" \
  165. " .body\n" \
  166. " .prologue\n" \
  167. " .save ar.pfs, r32\n" \
  168. " .body\n" \
  169. " { .mii\n" \
  170. " addl r3 = @gprel(_dl_skip_args), gp\n" \
  171. " adds r11 = 24, sp /* Load the address of argv. */\n" \
  172. " /* Save the pointer to the user entry point fptr in loc2. */\n" \
  173. " mov loc2 = ret0\n" \
  174. " ;;\n" \
  175. " }\n" \
  176. " { .mii\n" \
  177. " ld4 r3 = [r3]\n" \
  178. " adds r10 = 16, sp /* Load the address of argc. */\n" \
  179. " mov out2 = r11\n" \
  180. " ;;\n" \
  181. " /* See if we were run as a command with the executable file\n" \
  182. " name as an extra leading argument. If so, adjust the argv\n" \
  183. " pointer to skip _dl_skip_args words.\n" \
  184. " Note that _dl_skip_args is an integer, not a long - Jes\n" \
  185. "\n" \
  186. " The stack pointer has to be 16 byte aligned. We cannot simply\n" \
  187. " addjust the stack pointer. We have to move the whole argv and\n" \
  188. " envp and adjust _dl_argv by _dl_skip_args. H.J. */\n" \
  189. " }\n" \
  190. " { .mib\n" \
  191. " ld8 out1 = [r10] /* is argc actually stored as a long\n" \
  192. " or as an int? */\n" \
  193. " addl r2 = @ltoff(_dl_argv), gp\n" \
  194. " ;;\n" \
  195. " }\n" \
  196. " { .mmi\n" \
  197. " ld8 r2 = [r2] /* Get the address of _dl_argv. */\n" \
  198. " sub out1 = out1, r3 /* Get the new argc. */\n" \
  199. " shladd r3 = r3, 3, r0\n" \
  200. " ;;\n" \
  201. " }\n" \
  202. " {\n" \
  203. " .mib\n" \
  204. " ld8 r17 = [r2] /* Get _dl_argv. */\n" \
  205. " add r15 = r11, r3 /* The address of the argv we move */\n" \
  206. " ;;\n" \
  207. " }\n" \
  208. " /* ??? Could probably merge these two loops into 3 bundles.\n" \
  209. " using predication to control which set of copies we're on. */\n" \
  210. "1: /* Copy argv. */\n" \
  211. " { .mfi\n" \
  212. " ld8 r16 = [r15], 8 /* Load the value in the old argv. */\n" \
  213. " ;;\n" \
  214. " }\n" \
  215. " { .mib\n" \
  216. " st8 [r11] = r16, 8 /* Store it in the new argv. */\n" \
  217. " cmp.ne p6, p7 = 0, r16\n" \
  218. "(p6) br.cond.dptk.few 1b\n" \
  219. " ;;\n" \
  220. " }\n" \
  221. " { .mmi\n" \
  222. " mov out3 = r11\n" \
  223. " sub r17 = r17, r3 /* Substract _dl_skip_args. */\n" \
  224. " addl out0 = @gprel(_rtld_local), gp\n" \
  225. " }\n" \
  226. "1: /* Copy env. */\n" \
  227. " { .mfi\n" \
  228. " ld8 r16 = [r15], 8 /* Load the value in the old env. */\n" \
  229. " ;;\n" \
  230. " }\n" \
  231. " { .mib\n" \
  232. " st8 [r11] = r16, 8 /* Store it in the new env. */\n" \
  233. " cmp.ne p6, p7 = 0, r16\n" \
  234. "(p6) br.cond.dptk.few 1b\n" \
  235. " ;;\n" \
  236. " }\n" \
  237. " { .mmb\n" \
  238. " st8 [r10] = out1 /* Record the new argc. */\n" \
  239. " ld8 out0 = [out0] /* get the linkmap */\n" \
  240. " }\n" \
  241. " { .mmb\n" \
  242. " st8 [r2] = r17 /* Load the new _dl_argv. */\n" \
  243. " br.call.sptk.many b0 = _dl_init#\n" \
  244. " ;;\n" \
  245. " }\n" \
  246. " /* Pass our finalizer function to the user,\n" \
  247. " and jump to the user's entry point. */\n" \
  248. " { .mmi\n" \
  249. " ld8 r3 = [loc2], 8\n" \
  250. " mov b0 = r0\n" \
  251. " }\n" \
  252. " { .mmi\n" \
  253. " addl ret0 = @ltoff(@fptr(_dl_fini#)), gp\n" \
  254. " ;;\n" \
  255. " mov b6 = r3\n" \
  256. " }\n" \
  257. " { .mmi\n" \
  258. " ld8 ret0 = [ret0]\n" \
  259. " ld8 gp = [loc2]\n" \
  260. " mov ar.pfs = loc0\n" \
  261. " ;;\n" \
  262. " }\n" \
  263. " { .mfb\n" \
  264. " br.sptk.many b6\n" \
  265. " ;;\n" \
  266. " }\n" \
  267. " .endp _dl_start_user#\n" \
  268. ".previous\n");
  269. #ifndef RTLD_START_SPECIAL_INIT
  270. #define RTLD_START_SPECIAL_INIT /* nothing */
  271. #endif
  272. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or TLS
  273. variable, so undefined references should not be allowed to define the
  274. value.
  275. ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
  276. of the main executable's symbols, as for a COPY reloc, which we don't
  277. use. */
  278. /* ??? Ignore *MSB for now. */
  279. #define elf_machine_type_class(type) \
  280. (((type) == R_IA64_IPLTLSB || (type) == R_IA64_DTPMOD64LSB \
  281. || (type) == R_IA64_DTPREL64LSB || (type) == R_IA64_TPREL64LSB) \
  282. * ELF_RTYPE_CLASS_PLT)
  283. /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
  284. #define ELF_MACHINE_JMP_SLOT R_IA64_IPLTLSB
  285. /* According to the IA-64 specific documentation, Rela is always used. */
  286. #define ELF_MACHINE_NO_REL 1
  287. #define ELF_MACHINE_NO_RELA 0
  288. /* Return the address of the entry point. */
  289. #define ELF_MACHINE_START_ADDRESS(map, start) \
  290. ({ \
  291. ElfW(Addr) addr; \
  292. DL_DT_FUNCTION_ADDRESS(map, start, static, addr) \
  293. addr; \
  294. })
  295. /* Fixup a PLT entry to bounce directly to the function at VALUE. */
  296. static inline struct fdesc __attribute__ ((always_inline))
  297. elf_machine_fixup_plt (struct link_map *l, lookup_t t,
  298. const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
  299. const Elf64_Rela *reloc,
  300. Elf64_Addr *reloc_addr, struct fdesc value)
  301. {
  302. /* l is the link_map for the caller, t is the link_map for the object
  303. * being called */
  304. /* got has already been relocated in elf_get_dynamic_info() */
  305. reloc_addr[1] = value.gp;
  306. /* we need a "release" here to ensure that the gp is visible before
  307. the code entry point is updated: */
  308. ((volatile Elf64_Addr *) reloc_addr)[0] = value.ip;
  309. return value;
  310. }
  311. /* Return the final value of a plt relocation. */
  312. static inline struct fdesc
  313. elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
  314. struct fdesc value)
  315. {
  316. /* No need to handle rel vs rela since IA64 is rela only */
  317. return (struct fdesc) { value.ip + reloc->r_addend, value.gp };
  318. }
  319. #endif /* !dl_machine_h */
  320. #ifdef RESOLVE_MAP
  321. #define R_IA64_TYPE(R) ((R) & -8)
  322. #define R_IA64_FORMAT(R) ((R) & 7)
  323. #define R_IA64_FORMAT_32MSB 4
  324. #define R_IA64_FORMAT_32LSB 5
  325. #define R_IA64_FORMAT_64MSB 6
  326. #define R_IA64_FORMAT_64LSB 7
  327. /* Perform the relocation specified by RELOC and SYM (which is fully
  328. resolved). MAP is the object containing the reloc. */
  329. auto inline void
  330. __attribute ((always_inline))
  331. elf_machine_rela (struct link_map *map,
  332. const Elf64_Rela *reloc,
  333. const Elf64_Sym *sym,
  334. const struct r_found_version *version,
  335. void *const reloc_addr_arg,
  336. int skip_ifunc)
  337. {
  338. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  339. const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
  340. Elf64_Addr value;
  341. #if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC && !defined SHARED
  342. /* This is defined in rtld.c, but nowhere in the static libc.a; make the
  343. reference weak so static programs can still link. This declaration
  344. cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
  345. because rtld.c contains the common defn for _dl_rtld_map, which is
  346. incompatible with a weak decl in the same file. */
  347. weak_extern (_dl_rtld_map);
  348. #endif
  349. /* We cannot use a switch here because we cannot locate the switch
  350. jump table until we've self-relocated. */
  351. #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
  352. if (__builtin_expect (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_REL64LSB),
  353. 0))
  354. {
  355. assert (ELF64_R_TYPE (reloc->r_info) == R_IA64_REL64LSB);
  356. value = *reloc_addr;
  357. # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
  358. /* Already done in dynamic linker. */
  359. if (map != &GL(dl_rtld_map))
  360. # endif
  361. value += map->l_addr;
  362. }
  363. else
  364. #endif
  365. if (__builtin_expect (r_type == R_IA64_NONE, 0))
  366. return;
  367. else
  368. {
  369. struct link_map *sym_map;
  370. /* RESOLVE_MAP() will return NULL if it fail to locate the symbol. */
  371. if ((sym_map = RESOLVE_MAP (&sym, version, r_type)))
  372. {
  373. value = SYMBOL_ADDRESS (sym_map, sym, true) + reloc->r_addend;
  374. if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DIR64LSB))
  375. ;/* No adjustment. */
  376. else if (r_type == R_IA64_IPLTLSB)
  377. {
  378. elf_machine_fixup_plt (NULL, NULL, NULL, NULL, reloc, reloc_addr,
  379. DL_FIXUP_MAKE_VALUE (sym_map, value));
  380. return;
  381. }
  382. else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_FPTR64LSB))
  383. value = _dl_make_fptr (sym_map, sym, value);
  384. else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_PCREL64LSB))
  385. value -= (Elf64_Addr) reloc_addr & -16;
  386. else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DTPMOD64LSB))
  387. #ifdef RTLD_BOOTSTRAP
  388. /* During startup the dynamic linker is always index 1. */
  389. value = 1;
  390. #else
  391. /* Get the information from the link map returned by the
  392. resolv function. */
  393. value = sym_map->l_tls_modid;
  394. else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DTPREL64LSB))
  395. value -= sym_map->l_addr;
  396. #endif
  397. else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_TPREL64LSB))
  398. {
  399. #ifndef RTLD_BOOTSTRAP
  400. CHECK_STATIC_TLS (map, sym_map);
  401. #endif
  402. value += sym_map->l_tls_offset - sym_map->l_addr;
  403. }
  404. else
  405. _dl_reloc_bad_type (map, r_type, 0);
  406. }
  407. else
  408. value = 0;
  409. }
  410. /* ??? Ignore MSB and Instruction format for now. */
  411. if (R_IA64_FORMAT (r_type) == R_IA64_FORMAT_64LSB)
  412. *reloc_addr = value;
  413. else if (R_IA64_FORMAT (r_type) == R_IA64_FORMAT_32LSB)
  414. *(int *) reloc_addr = value;
  415. else if (r_type == R_IA64_IPLTLSB)
  416. {
  417. reloc_addr[0] = 0;
  418. reloc_addr[1] = 0;
  419. }
  420. else
  421. _dl_reloc_bad_type (map, r_type, 0);
  422. }
  423. /* Let do-rel.h know that on IA-64 if l_addr is 0, all RELATIVE relocs
  424. can be skipped. */
  425. #define ELF_MACHINE_REL_RELATIVE 1
  426. auto inline void
  427. __attribute ((always_inline))
  428. elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
  429. void *const reloc_addr_arg)
  430. {
  431. Elf64_Addr *const reloc_addr = reloc_addr_arg;
  432. /* ??? Ignore MSB and Instruction format for now. */
  433. assert (ELF64_R_TYPE (reloc->r_info) == R_IA64_REL64LSB);
  434. *reloc_addr += l_addr;
  435. }
  436. /* Perform a RELATIVE reloc on the .got entry that transfers to the .plt. */
  437. auto inline void
  438. __attribute ((always_inline))
  439. elf_machine_lazy_rel (struct link_map *map,
  440. Elf64_Addr l_addr, const Elf64_Rela *reloc,
  441. int skip_ifunc)
  442. {
  443. Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
  444. const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
  445. if (r_type == R_IA64_IPLTLSB)
  446. {
  447. reloc_addr[0] += l_addr;
  448. reloc_addr[1] += l_addr;
  449. }
  450. else if (r_type == R_IA64_NONE)
  451. return;
  452. else
  453. _dl_reloc_bad_type (map, r_type, 1);
  454. }
  455. #endif /* RESOLVE_MAP */