elfxx-aarch64.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /* AArch64-specific support for ELF.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. Contributed by ARM Ltd.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program 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
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING3. If not,
  15. see <http://www.gnu.org/licenses/>. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "elf-bfd.h"
  19. #include "elfxx-aarch64.h"
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #define MASK(n) ((1u << (n)) - 1)
  23. /* Sign-extend VALUE, which has the indicated number of BITS. */
  24. bfd_signed_vma
  25. _bfd_aarch64_sign_extend (bfd_vma value, int bits)
  26. {
  27. if (value & ((bfd_vma) 1 << (bits - 1)))
  28. /* VALUE is negative. */
  29. value |= ((bfd_vma) - 1) << bits;
  30. return value;
  31. }
  32. /* Decode the IMM field of ADRP. */
  33. uint32_t
  34. _bfd_aarch64_decode_adrp_imm (uint32_t insn)
  35. {
  36. return (((insn >> 5) & MASK (19)) << 2) | ((insn >> 29) & MASK (2));
  37. }
  38. /* Reencode the imm field of add immediate. */
  39. static inline uint32_t
  40. reencode_add_imm (uint32_t insn, uint32_t imm)
  41. {
  42. return (insn & ~(MASK (12) << 10)) | ((imm & MASK (12)) << 10);
  43. }
  44. /* Reencode the IMM field of ADR. */
  45. uint32_t
  46. _bfd_aarch64_reencode_adr_imm (uint32_t insn, uint32_t imm)
  47. {
  48. return (insn & ~((MASK (2) << 29) | (MASK (19) << 5)))
  49. | ((imm & MASK (2)) << 29) | ((imm & (MASK (19) << 2)) << 3);
  50. }
  51. /* Reencode the imm field of ld/st pos immediate. */
  52. static inline uint32_t
  53. reencode_ldst_pos_imm (uint32_t insn, uint32_t imm)
  54. {
  55. return (insn & ~(MASK (12) << 10)) | ((imm & MASK (12)) << 10);
  56. }
  57. /* Encode the 26-bit offset of unconditional branch. */
  58. static inline uint32_t
  59. reencode_branch_ofs_26 (uint32_t insn, uint32_t ofs)
  60. {
  61. return (insn & ~MASK (26)) | (ofs & MASK (26));
  62. }
  63. /* Encode the 19-bit offset of conditional branch and compare & branch. */
  64. static inline uint32_t
  65. reencode_cond_branch_ofs_19 (uint32_t insn, uint32_t ofs)
  66. {
  67. return (insn & ~(MASK (19) << 5)) | ((ofs & MASK (19)) << 5);
  68. }
  69. /* Decode the 19-bit offset of load literal. */
  70. static inline uint32_t
  71. reencode_ld_lit_ofs_19 (uint32_t insn, uint32_t ofs)
  72. {
  73. return (insn & ~(MASK (19) << 5)) | ((ofs & MASK (19)) << 5);
  74. }
  75. /* Encode the 14-bit offset of test & branch. */
  76. static inline uint32_t
  77. reencode_tst_branch_ofs_14 (uint32_t insn, uint32_t ofs)
  78. {
  79. return (insn & ~(MASK (14) << 5)) | ((ofs & MASK (14)) << 5);
  80. }
  81. /* Reencode the imm field of move wide. */
  82. static inline uint32_t
  83. reencode_movw_imm (uint32_t insn, uint32_t imm)
  84. {
  85. return (insn & ~(MASK (16) << 5)) | ((imm & MASK (16)) << 5);
  86. }
  87. /* Reencode mov[zn] to movz. */
  88. static inline uint32_t
  89. reencode_movzn_to_movz (uint32_t opcode)
  90. {
  91. return opcode | (1 << 30);
  92. }
  93. /* Reencode mov[zn] to movn. */
  94. static inline uint32_t
  95. reencode_movzn_to_movn (uint32_t opcode)
  96. {
  97. return opcode & ~(1 << 30);
  98. }
  99. /* Return non-zero if the indicated VALUE has overflowed the maximum
  100. range expressible by a unsigned number with the indicated number of
  101. BITS. */
  102. static bfd_reloc_status_type
  103. aarch64_unsigned_overflow (bfd_vma value, unsigned int bits)
  104. {
  105. bfd_vma lim;
  106. if (bits >= sizeof (bfd_vma) * 8)
  107. return bfd_reloc_ok;
  108. lim = (bfd_vma) 1 << bits;
  109. if (value >= lim)
  110. return bfd_reloc_overflow;
  111. return bfd_reloc_ok;
  112. }
  113. /* Return non-zero if the indicated VALUE has overflowed the maximum
  114. range expressible by an signed number with the indicated number of
  115. BITS. */
  116. static bfd_reloc_status_type
  117. aarch64_signed_overflow (bfd_vma value, unsigned int bits)
  118. {
  119. bfd_signed_vma svalue = (bfd_signed_vma) value;
  120. bfd_signed_vma lim;
  121. if (bits >= sizeof (bfd_vma) * 8)
  122. return bfd_reloc_ok;
  123. lim = (bfd_signed_vma) 1 << (bits - 1);
  124. if (svalue < -lim || svalue >= lim)
  125. return bfd_reloc_overflow;
  126. return bfd_reloc_ok;
  127. }
  128. /* Insert the addend/value into the instruction or data object being
  129. relocated. */
  130. bfd_reloc_status_type
  131. _bfd_aarch64_elf_put_addend (bfd *abfd,
  132. bfd_byte *address, bfd_reloc_code_real_type r_type,
  133. reloc_howto_type *howto, bfd_signed_vma addend)
  134. {
  135. bfd_reloc_status_type status = bfd_reloc_ok;
  136. bfd_signed_vma old_addend = addend;
  137. bfd_vma contents;
  138. int size;
  139. size = bfd_get_reloc_size (howto);
  140. switch (size)
  141. {
  142. case 0:
  143. return status;
  144. case 2:
  145. contents = bfd_get_16 (abfd, address);
  146. break;
  147. case 4:
  148. if (howto->src_mask != 0xffffffff)
  149. /* Must be 32-bit instruction, always little-endian. */
  150. contents = bfd_getl32 (address);
  151. else
  152. /* Must be 32-bit data (endianness dependent). */
  153. contents = bfd_get_32 (abfd, address);
  154. break;
  155. case 8:
  156. contents = bfd_get_64 (abfd, address);
  157. break;
  158. default:
  159. abort ();
  160. }
  161. switch (howto->complain_on_overflow)
  162. {
  163. case complain_overflow_dont:
  164. break;
  165. case complain_overflow_signed:
  166. status = aarch64_signed_overflow (addend,
  167. howto->bitsize + howto->rightshift);
  168. break;
  169. case complain_overflow_unsigned:
  170. status = aarch64_unsigned_overflow (addend,
  171. howto->bitsize + howto->rightshift);
  172. break;
  173. case complain_overflow_bitfield:
  174. default:
  175. abort ();
  176. }
  177. addend >>= howto->rightshift;
  178. switch (r_type)
  179. {
  180. case BFD_RELOC_AARCH64_CALL26:
  181. case BFD_RELOC_AARCH64_JUMP26:
  182. contents = reencode_branch_ofs_26 (contents, addend);
  183. break;
  184. case BFD_RELOC_AARCH64_BRANCH19:
  185. contents = reencode_cond_branch_ofs_19 (contents, addend);
  186. break;
  187. case BFD_RELOC_AARCH64_TSTBR14:
  188. contents = reencode_tst_branch_ofs_14 (contents, addend);
  189. break;
  190. case BFD_RELOC_AARCH64_GOT_LD_PREL19:
  191. case BFD_RELOC_AARCH64_LD_LO19_PCREL:
  192. case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
  193. case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
  194. if (old_addend & ((1 << howto->rightshift) - 1))
  195. return bfd_reloc_overflow;
  196. contents = reencode_ld_lit_ofs_19 (contents, addend);
  197. break;
  198. case BFD_RELOC_AARCH64_TLSDESC_CALL:
  199. break;
  200. case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
  201. case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
  202. case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
  203. case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
  204. case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
  205. case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
  206. case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
  207. case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
  208. case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
  209. case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
  210. case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
  211. contents = _bfd_aarch64_reencode_adr_imm (contents, addend);
  212. break;
  213. case BFD_RELOC_AARCH64_ADD_LO12:
  214. case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
  215. case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
  216. case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
  217. case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
  218. case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
  219. case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
  220. case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
  221. case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
  222. case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
  223. /* Corresponds to: add rd, rn, #uimm12 to provide the low order
  224. 12 bits of the page offset following
  225. BFD_RELOC_AARCH64_ADR_HI21_PCREL which computes the
  226. (pc-relative) page base. */
  227. contents = reencode_add_imm (contents, addend);
  228. break;
  229. case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
  230. case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
  231. case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
  232. case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
  233. case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
  234. case BFD_RELOC_AARCH64_LDST128_LO12:
  235. case BFD_RELOC_AARCH64_LDST16_LO12:
  236. case BFD_RELOC_AARCH64_LDST32_LO12:
  237. case BFD_RELOC_AARCH64_LDST64_LO12:
  238. case BFD_RELOC_AARCH64_LDST8_LO12:
  239. case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
  240. case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
  241. case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
  242. case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
  243. case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
  244. case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
  245. case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
  246. case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
  247. case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
  248. case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
  249. case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
  250. case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
  251. case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12:
  252. case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
  253. case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12:
  254. case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
  255. case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12:
  256. case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
  257. case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12:
  258. case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
  259. if (old_addend & ((1 << howto->rightshift) - 1))
  260. return bfd_reloc_overflow;
  261. /* Used for ldr*|str* rt, [rn, #uimm12] to provide the low order
  262. 12 bits address offset. */
  263. contents = reencode_ldst_pos_imm (contents, addend);
  264. break;
  265. /* Group relocations to create high bits of a 16, 32, 48 or 64
  266. bit signed data or abs address inline. Will change
  267. instruction to MOVN or MOVZ depending on sign of calculated
  268. value. */
  269. case BFD_RELOC_AARCH64_MOVW_G0_S:
  270. case BFD_RELOC_AARCH64_MOVW_G1_S:
  271. case BFD_RELOC_AARCH64_MOVW_G2_S:
  272. case BFD_RELOC_AARCH64_MOVW_PREL_G0:
  273. case BFD_RELOC_AARCH64_MOVW_PREL_G1:
  274. case BFD_RELOC_AARCH64_MOVW_PREL_G2:
  275. case BFD_RELOC_AARCH64_MOVW_PREL_G3:
  276. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
  277. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
  278. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
  279. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
  280. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
  281. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
  282. /* NOTE: We can only come here with movz or movn. */
  283. if (addend < 0)
  284. {
  285. /* Force use of MOVN. */
  286. addend = ~addend;
  287. contents = reencode_movzn_to_movn (contents);
  288. }
  289. else
  290. {
  291. /* Force use of MOVZ. */
  292. contents = reencode_movzn_to_movz (contents);
  293. }
  294. /* Fall through. */
  295. /* Group relocations to create a 16, 32, 48 or 64 bit unsigned
  296. data or abs address inline. */
  297. case BFD_RELOC_AARCH64_MOVW_G0:
  298. case BFD_RELOC_AARCH64_MOVW_G0_NC:
  299. case BFD_RELOC_AARCH64_MOVW_G1:
  300. case BFD_RELOC_AARCH64_MOVW_G1_NC:
  301. case BFD_RELOC_AARCH64_MOVW_G2:
  302. case BFD_RELOC_AARCH64_MOVW_G2_NC:
  303. case BFD_RELOC_AARCH64_MOVW_G3:
  304. case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
  305. case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
  306. case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC:
  307. case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC:
  308. case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC:
  309. case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
  310. case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
  311. case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
  312. case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
  313. case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
  314. case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
  315. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
  316. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
  317. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
  318. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
  319. contents = reencode_movw_imm (contents, addend);
  320. break;
  321. default:
  322. /* Repack simple data */
  323. if (howto->dst_mask & (howto->dst_mask + 1))
  324. return bfd_reloc_notsupported;
  325. contents = ((contents & ~howto->dst_mask) | (addend & howto->dst_mask));
  326. break;
  327. }
  328. switch (size)
  329. {
  330. case 2:
  331. bfd_put_16 (abfd, contents, address);
  332. break;
  333. case 4:
  334. if (howto->dst_mask != 0xffffffff)
  335. /* must be 32-bit instruction, always little-endian */
  336. bfd_putl32 (contents, address);
  337. else
  338. /* must be 32-bit data (endianness dependent) */
  339. bfd_put_32 (abfd, contents, address);
  340. break;
  341. case 8:
  342. bfd_put_64 (abfd, contents, address);
  343. break;
  344. default:
  345. abort ();
  346. }
  347. return status;
  348. }
  349. bfd_vma
  350. _bfd_aarch64_elf_resolve_relocation (bfd *input_bfd,
  351. bfd_reloc_code_real_type r_type,
  352. bfd_vma place, bfd_vma value,
  353. bfd_vma addend, bool weak_undef_p)
  354. {
  355. bool tls_reloc = true;
  356. switch (r_type)
  357. {
  358. case BFD_RELOC_AARCH64_NONE:
  359. case BFD_RELOC_AARCH64_TLSDESC_CALL:
  360. break;
  361. case BFD_RELOC_AARCH64_16_PCREL:
  362. case BFD_RELOC_AARCH64_32_PCREL:
  363. case BFD_RELOC_AARCH64_64_PCREL:
  364. case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
  365. case BFD_RELOC_AARCH64_BRANCH19:
  366. case BFD_RELOC_AARCH64_LD_LO19_PCREL:
  367. case BFD_RELOC_AARCH64_MOVW_PREL_G0:
  368. case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC:
  369. case BFD_RELOC_AARCH64_MOVW_PREL_G1:
  370. case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC:
  371. case BFD_RELOC_AARCH64_MOVW_PREL_G2:
  372. case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC:
  373. case BFD_RELOC_AARCH64_MOVW_PREL_G3:
  374. case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
  375. case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
  376. case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
  377. case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
  378. case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
  379. case BFD_RELOC_AARCH64_TSTBR14:
  380. if (weak_undef_p)
  381. value = place;
  382. value = value + addend - place;
  383. break;
  384. case BFD_RELOC_AARCH64_CALL26:
  385. case BFD_RELOC_AARCH64_JUMP26:
  386. value = value + addend - place;
  387. break;
  388. case BFD_RELOC_AARCH64_16:
  389. case BFD_RELOC_AARCH64_32:
  390. case BFD_RELOC_AARCH64_MOVW_G0:
  391. case BFD_RELOC_AARCH64_MOVW_G0_NC:
  392. case BFD_RELOC_AARCH64_MOVW_G0_S:
  393. case BFD_RELOC_AARCH64_MOVW_G1:
  394. case BFD_RELOC_AARCH64_MOVW_G1_NC:
  395. case BFD_RELOC_AARCH64_MOVW_G1_S:
  396. case BFD_RELOC_AARCH64_MOVW_G2:
  397. case BFD_RELOC_AARCH64_MOVW_G2_NC:
  398. case BFD_RELOC_AARCH64_MOVW_G2_S:
  399. case BFD_RELOC_AARCH64_MOVW_G3:
  400. tls_reloc = false;
  401. /* fall-through. */
  402. case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
  403. case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
  404. case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
  405. case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
  406. case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
  407. case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
  408. case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
  409. case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
  410. case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
  411. case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
  412. case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
  413. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
  414. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
  415. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
  416. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
  417. case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
  418. case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12:
  419. case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12:
  420. case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12:
  421. case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12:
  422. /* Weak Symbols and TLS relocations are implementation defined. For this
  423. case we choose to emit 0. */
  424. if (weak_undef_p && tls_reloc)
  425. {
  426. _bfd_error_handler (_("%pB: warning: Weak TLS is implementation "
  427. "defined and may not work as expected"),
  428. input_bfd);
  429. value = place;
  430. }
  431. value = value + addend;
  432. break;
  433. case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
  434. case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
  435. if (weak_undef_p)
  436. value = PG (place);
  437. value = PG (value + addend) - PG (place);
  438. break;
  439. case BFD_RELOC_AARCH64_GOT_LD_PREL19:
  440. value = value + addend - place;
  441. break;
  442. case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
  443. case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
  444. case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
  445. case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
  446. case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
  447. value = PG (value + addend) - PG (place);
  448. break;
  449. /* Caller must make sure addend is the base address of .got section. */
  450. case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
  451. case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
  452. addend = PG (addend);
  453. /* Fall through. */
  454. case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
  455. case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
  456. case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
  457. value = value - addend;
  458. break;
  459. case BFD_RELOC_AARCH64_ADD_LO12:
  460. case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
  461. case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
  462. case BFD_RELOC_AARCH64_LDST128_LO12:
  463. case BFD_RELOC_AARCH64_LDST16_LO12:
  464. case BFD_RELOC_AARCH64_LDST32_LO12:
  465. case BFD_RELOC_AARCH64_LDST64_LO12:
  466. case BFD_RELOC_AARCH64_LDST8_LO12:
  467. case BFD_RELOC_AARCH64_TLSDESC_ADD:
  468. case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
  469. case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
  470. case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
  471. case BFD_RELOC_AARCH64_TLSDESC_LDR:
  472. case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
  473. case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
  474. case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
  475. case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
  476. case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
  477. case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
  478. case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
  479. case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
  480. case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
  481. case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
  482. case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
  483. case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
  484. value = PG_OFFSET (value + addend);
  485. break;
  486. case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
  487. value = value + addend;
  488. break;
  489. case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
  490. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
  491. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
  492. value = (value + addend) & (bfd_vma) 0xffff0000;
  493. break;
  494. case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
  495. /* Mask off low 12bits, keep all other high bits, so that the later
  496. generic code could check whehter there is overflow. */
  497. value = (value + addend) & ~(bfd_vma) 0xfff;
  498. break;
  499. case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
  500. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
  501. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
  502. value = (value + addend) & (bfd_vma) 0xffff;
  503. break;
  504. case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
  505. value = (value + addend) & ~(bfd_vma) 0xffffffff;
  506. value -= place & ~(bfd_vma) 0xffffffff;
  507. break;
  508. default:
  509. break;
  510. }
  511. return value;
  512. }
  513. /* Support for core dump NOTE sections. */
  514. bool
  515. _bfd_aarch64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  516. {
  517. int offset;
  518. size_t size;
  519. switch (note->descsz)
  520. {
  521. default:
  522. return false;
  523. case 392: /* sizeof(struct elf_prstatus) on Linux/arm64. */
  524. /* pr_cursig */
  525. elf_tdata (abfd)->core->signal
  526. = bfd_get_16 (abfd, note->descdata + 12);
  527. /* pr_pid */
  528. elf_tdata (abfd)->core->lwpid
  529. = bfd_get_32 (abfd, note->descdata + 32);
  530. /* pr_reg */
  531. offset = 112;
  532. size = 272;
  533. break;
  534. }
  535. /* Make a ".reg/999" section. */
  536. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  537. size, note->descpos + offset);
  538. }
  539. bool
  540. _bfd_aarch64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  541. {
  542. switch (note->descsz)
  543. {
  544. default:
  545. return false;
  546. case 136: /* This is sizeof(struct elf_prpsinfo) on Linux/aarch64. */
  547. elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
  548. elf_tdata (abfd)->core->program
  549. = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
  550. elf_tdata (abfd)->core->command
  551. = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
  552. }
  553. /* Note that for some reason, a spurious space is tacked
  554. onto the end of the args in some (at least one anyway)
  555. implementations, so strip it off if it exists. */
  556. {
  557. char *command = elf_tdata (abfd)->core->command;
  558. int n = strlen (command);
  559. if (0 < n && command[n - 1] == ' ')
  560. command[n - 1] = '\0';
  561. }
  562. return true;
  563. }
  564. char *
  565. _bfd_aarch64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
  566. ...)
  567. {
  568. switch (note_type)
  569. {
  570. default:
  571. return NULL;
  572. case NT_PRPSINFO:
  573. {
  574. char data[136] ATTRIBUTE_NONSTRING;
  575. va_list ap;
  576. va_start (ap, note_type);
  577. memset (data, 0, sizeof (data));
  578. strncpy (data + 40, va_arg (ap, const char *), 16);
  579. #if GCC_VERSION == 8000 || GCC_VERSION == 8001
  580. DIAGNOSTIC_PUSH;
  581. /* GCC 8.0 and 8.1 warn about 80 equals destination size with
  582. -Wstringop-truncation:
  583. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
  584. */
  585. DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
  586. #endif
  587. strncpy (data + 56, va_arg (ap, const char *), 80);
  588. #if GCC_VERSION == 8000 || GCC_VERSION == 8001
  589. DIAGNOSTIC_POP;
  590. #endif
  591. va_end (ap);
  592. return elfcore_write_note (abfd, buf, bufsiz, "CORE",
  593. note_type, data, sizeof (data));
  594. }
  595. case NT_PRSTATUS:
  596. {
  597. char data[392];
  598. va_list ap;
  599. long pid;
  600. int cursig;
  601. const void *greg;
  602. va_start (ap, note_type);
  603. memset (data, 0, sizeof (data));
  604. pid = va_arg (ap, long);
  605. bfd_put_32 (abfd, pid, data + 32);
  606. cursig = va_arg (ap, int);
  607. bfd_put_16 (abfd, cursig, data + 12);
  608. greg = va_arg (ap, const void *);
  609. memcpy (data + 112, greg, 272);
  610. va_end (ap);
  611. return elfcore_write_note (abfd, buf, bufsiz, "CORE",
  612. note_type, data, sizeof (data));
  613. }
  614. }
  615. }
  616. /* Find the first input bfd with GNU property and merge it with GPROP. If no
  617. such input is found, add it to a new section at the last input. Update
  618. GPROP accordingly. */
  619. bfd *
  620. _bfd_aarch64_elf_link_setup_gnu_properties (struct bfd_link_info *info,
  621. uint32_t *gprop)
  622. {
  623. asection *sec;
  624. bfd *pbfd;
  625. bfd *ebfd = NULL;
  626. elf_property *prop;
  627. unsigned align;
  628. uint32_t gnu_prop = *gprop;
  629. /* Find a normal input file with GNU property note. */
  630. for (pbfd = info->input_bfds;
  631. pbfd != NULL;
  632. pbfd = pbfd->link.next)
  633. if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
  634. && bfd_count_sections (pbfd) != 0)
  635. {
  636. ebfd = pbfd;
  637. if (elf_properties (pbfd) != NULL)
  638. break;
  639. }
  640. /* If ebfd != NULL it is either an input with property note or the last
  641. input. Either way if we have gnu_prop, we should add it (by creating
  642. a section if needed). */
  643. if (ebfd != NULL && gnu_prop)
  644. {
  645. prop = _bfd_elf_get_property (ebfd,
  646. GNU_PROPERTY_AARCH64_FEATURE_1_AND,
  647. 4);
  648. if (gnu_prop & GNU_PROPERTY_AARCH64_FEATURE_1_BTI
  649. && !(prop->u.number & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
  650. _bfd_error_handler (_("%pB: warning: BTI turned on by -z force-bti "
  651. "when all inputs do not have BTI in NOTE "
  652. "section."), ebfd);
  653. prop->u.number |= gnu_prop;
  654. prop->pr_kind = property_number;
  655. /* pbfd being NULL implies ebfd is the last input. Create the GNU
  656. property note section. */
  657. if (pbfd == NULL)
  658. {
  659. sec = bfd_make_section_with_flags (ebfd,
  660. NOTE_GNU_PROPERTY_SECTION_NAME,
  661. (SEC_ALLOC
  662. | SEC_LOAD
  663. | SEC_IN_MEMORY
  664. | SEC_READONLY
  665. | SEC_HAS_CONTENTS
  666. | SEC_DATA));
  667. if (sec == NULL)
  668. info->callbacks->einfo (
  669. _("%F%P: failed to create GNU property section\n"));
  670. align = (bfd_get_mach (ebfd) & bfd_mach_aarch64_ilp32) ? 2 : 3;
  671. if (!bfd_set_section_alignment (sec, align))
  672. info->callbacks->einfo (_("%F%pA: failed to align section\n"),
  673. sec);
  674. elf_section_type (sec) = SHT_NOTE;
  675. }
  676. }
  677. pbfd = _bfd_elf_link_setup_gnu_properties (info);
  678. if (bfd_link_relocatable (info))
  679. return pbfd;
  680. /* If pbfd has any GNU_PROPERTY_AARCH64_FEATURE_1_AND properties, update
  681. gnu_prop accordingly. */
  682. if (pbfd != NULL)
  683. {
  684. elf_property_list *p;
  685. /* The property list is sorted in order of type. */
  686. for (p = elf_properties (pbfd); p; p = p->next)
  687. {
  688. /* Check for all GNU_PROPERTY_AARCH64_FEATURE_1_AND. */
  689. if (GNU_PROPERTY_AARCH64_FEATURE_1_AND == p->property.pr_type)
  690. {
  691. gnu_prop = (p->property.u.number
  692. & (GNU_PROPERTY_AARCH64_FEATURE_1_PAC
  693. | GNU_PROPERTY_AARCH64_FEATURE_1_BTI));
  694. break;
  695. }
  696. else if (GNU_PROPERTY_AARCH64_FEATURE_1_AND < p->property.pr_type)
  697. break;
  698. }
  699. }
  700. *gprop = gnu_prop;
  701. return pbfd;
  702. }
  703. /* Define elf_backend_parse_gnu_properties for AArch64. */
  704. enum elf_property_kind
  705. _bfd_aarch64_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
  706. bfd_byte *ptr, unsigned int datasz)
  707. {
  708. elf_property *prop;
  709. switch (type)
  710. {
  711. case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
  712. if (datasz != 4)
  713. {
  714. _bfd_error_handler
  715. ( _("error: %pB: <corrupt AArch64 used size: 0x%x>"),
  716. abfd, datasz);
  717. return property_corrupt;
  718. }
  719. prop = _bfd_elf_get_property (abfd, type, datasz);
  720. /* Combine properties of the same type. */
  721. prop->u.number |= bfd_h_get_32 (abfd, ptr);
  722. prop->pr_kind = property_number;
  723. break;
  724. default:
  725. return property_ignored;
  726. }
  727. return property_number;
  728. }
  729. /* Merge AArch64 GNU property BPROP with APROP also accounting for PROP.
  730. If APROP isn't NULL, merge it with BPROP and/or PROP. Vice-versa if BROP
  731. isn't NULL. Return TRUE if there is any update to APROP or if BPROP should
  732. be merge with ABFD. */
  733. bool
  734. _bfd_aarch64_elf_merge_gnu_properties (struct bfd_link_info *info
  735. ATTRIBUTE_UNUSED,
  736. bfd *abfd ATTRIBUTE_UNUSED,
  737. elf_property *aprop,
  738. elf_property *bprop,
  739. uint32_t prop)
  740. {
  741. unsigned int orig_number;
  742. bool updated = false;
  743. unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
  744. switch (pr_type)
  745. {
  746. case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
  747. {
  748. if (aprop != NULL && bprop != NULL)
  749. {
  750. orig_number = aprop->u.number;
  751. aprop->u.number = (orig_number & bprop->u.number) | prop;
  752. updated = orig_number != aprop->u.number;
  753. /* Remove the property if all feature bits are cleared. */
  754. if (aprop->u.number == 0)
  755. aprop->pr_kind = property_remove;
  756. break;
  757. }
  758. /* If either is NULL, the AND would be 0 so, if there is
  759. any PROP, asign it to the input that is not NULL. */
  760. if (prop)
  761. {
  762. if (aprop != NULL)
  763. {
  764. orig_number = aprop->u.number;
  765. aprop->u.number = prop;
  766. updated = orig_number != aprop->u.number;
  767. }
  768. else
  769. {
  770. bprop->u.number = prop;
  771. updated = true;
  772. }
  773. }
  774. /* No PROP and BPROP is NULL, so remove APROP. */
  775. else if (aprop != NULL)
  776. {
  777. aprop->pr_kind = property_remove;
  778. updated = true;
  779. }
  780. }
  781. break;
  782. default:
  783. abort ();
  784. }
  785. return updated;
  786. }
  787. /* Fix up AArch64 GNU properties. */
  788. void
  789. _bfd_aarch64_elf_link_fixup_gnu_properties
  790. (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  791. elf_property_list **listp)
  792. {
  793. elf_property_list *p, *prev;
  794. for (p = *listp, prev = *listp; p; p = p->next)
  795. {
  796. unsigned int type = p->property.pr_type;
  797. if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
  798. {
  799. if (p->property.pr_kind == property_remove)
  800. {
  801. /* Remove empty property. */
  802. if (prev == p)
  803. {
  804. *listp = p->next;
  805. prev = *listp;
  806. }
  807. else
  808. prev->next = p->next;
  809. continue;
  810. }
  811. prev = p;
  812. }
  813. else if (type > GNU_PROPERTY_HIPROC)
  814. {
  815. /* The property list is sorted in order of type. */
  816. break;
  817. }
  818. }
  819. }