mach-o-aarch64.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* AArch-64 Mach-O support for BFD.
  2. Copyright (C) 2015-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "libiberty.h"
  20. #include "mach-o.h"
  21. #include "mach-o/arm64.h"
  22. #define bfd_mach_o_object_p bfd_mach_o_arm64_object_p
  23. #define bfd_mach_o_core_p bfd_mach_o_arm64_core_p
  24. #define bfd_mach_o_mkobject bfd_mach_o_arm64_mkobject
  25. #define bfd_mach_o_canonicalize_one_reloc \
  26. bfd_mach_o_arm64_canonicalize_one_reloc
  27. #define bfd_mach_o_swap_reloc_out NULL
  28. #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_arm64_bfd_reloc_type_lookup
  29. #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_arm64_bfd_reloc_name_lookup
  30. #define bfd_mach_o_print_thread NULL
  31. #define bfd_mach_o_tgt_seg_table NULL
  32. #define bfd_mach_o_section_type_valid_for_tgt NULL
  33. static bfd_cleanup
  34. bfd_mach_o_arm64_object_p (bfd *abfd)
  35. {
  36. return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_ARM64);
  37. }
  38. static bfd_cleanup
  39. bfd_mach_o_arm64_core_p (bfd *abfd)
  40. {
  41. return bfd_mach_o_header_p (abfd, 0,
  42. BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_ARM64);
  43. }
  44. static bool
  45. bfd_mach_o_arm64_mkobject (bfd *abfd)
  46. {
  47. bfd_mach_o_data_struct *mdata;
  48. if (!bfd_mach_o_mkobject_init (abfd))
  49. return false;
  50. mdata = bfd_mach_o_get_data (abfd);
  51. mdata->header.magic = BFD_MACH_O_MH_MAGIC;
  52. mdata->header.cputype = BFD_MACH_O_CPU_TYPE_ARM64;
  53. mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL;
  54. mdata->header.byteorder = BFD_ENDIAN_LITTLE;
  55. mdata->header.version = 1;
  56. return true;
  57. }
  58. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
  59. #define MINUS_ONE (~ (bfd_vma) 0)
  60. static reloc_howto_type arm64_howto_table[]=
  61. {
  62. /* 0 */
  63. HOWTO (BFD_RELOC_64, 0, 4, 64, false, 0,
  64. complain_overflow_bitfield,
  65. NULL, "64",
  66. false, MINUS_ONE, MINUS_ONE, false),
  67. HOWTO (BFD_RELOC_32, 0, 2, 32, false, 0,
  68. complain_overflow_bitfield,
  69. NULL, "32",
  70. false, 0xffffffff, 0xffffffff, false),
  71. HOWTO (BFD_RELOC_16, 0, 1, 16, false, 0,
  72. complain_overflow_bitfield,
  73. NULL, "16",
  74. false, 0xffff, 0xffff, false),
  75. HOWTO (BFD_RELOC_8, 0, 0, 8, false, 0,
  76. complain_overflow_bitfield,
  77. NULL, "8",
  78. false, 0xff, 0xff, false),
  79. /* 4 */
  80. HOWTO (BFD_RELOC_64_PCREL, 0, 4, 64, true, 0,
  81. complain_overflow_bitfield,
  82. NULL, "DISP64",
  83. false, MINUS_ONE, MINUS_ONE, true),
  84. HOWTO (BFD_RELOC_32_PCREL, 0, 2, 32, true, 0,
  85. complain_overflow_bitfield,
  86. NULL, "DISP32",
  87. false, 0xffffffff, 0xffffffff, true),
  88. HOWTO (BFD_RELOC_16_PCREL, 0, 1, 16, true, 0,
  89. complain_overflow_bitfield,
  90. NULL, "DISP16",
  91. false, 0xffff, 0xffff, true),
  92. HOWTO (BFD_RELOC_AARCH64_CALL26, 0, 2, 26, true, 0,
  93. complain_overflow_bitfield,
  94. NULL, "BRANCH26",
  95. false, 0x03ffffff, 0x03ffffff, true),
  96. /* 8 */
  97. HOWTO (BFD_RELOC_AARCH64_ADR_HI21_PCREL, 12, 2, 21, true, 0,
  98. complain_overflow_signed,
  99. NULL, "PAGE21",
  100. false, 0x1fffff, 0x1fffff, true),
  101. HOWTO (BFD_RELOC_AARCH64_LDST16_LO12, 1, 2, 12, true, 0,
  102. complain_overflow_signed,
  103. NULL, "PGOFF12",
  104. false, 0xffe, 0xffe, true),
  105. HOWTO (BFD_RELOC_MACH_O_ARM64_ADDEND, 0, 2, 32, false, 0,
  106. complain_overflow_signed,
  107. NULL, "ADDEND",
  108. false, 0xffffffff, 0xffffffff, false),
  109. HOWTO (BFD_RELOC_MACH_O_SUBTRACTOR32, 0, 2, 32, false, 0,
  110. complain_overflow_bitfield,
  111. NULL, "SUBTRACTOR32",
  112. false, 0xffffffff, 0xffffffff, false),
  113. /* 12 */
  114. HOWTO (BFD_RELOC_MACH_O_SUBTRACTOR64, 0, 4, 64, false, 0,
  115. complain_overflow_bitfield,
  116. NULL, "SUBTRACTOR64",
  117. false, MINUS_ONE, MINUS_ONE, false),
  118. HOWTO (BFD_RELOC_MACH_O_ARM64_GOT_LOAD_PAGE21, 12, 2, 21, true, 0,
  119. complain_overflow_signed,
  120. NULL, "GOT_LD_PG21",
  121. false, 0x1fffff, 0x1fffff, true),
  122. HOWTO (BFD_RELOC_MACH_O_ARM64_GOT_LOAD_PAGEOFF12, 1, 2, 12, true, 0,
  123. complain_overflow_signed,
  124. NULL, "GOT_LD_PGOFF12",
  125. false, 0xffe, 0xffe, true),
  126. HOWTO (BFD_RELOC_MACH_O_ARM64_POINTER_TO_GOT, 0, 2, 32, true, 0,
  127. complain_overflow_bitfield,
  128. NULL, "PTR_TO_GOT",
  129. false, 0xffffffff, 0xffffffff, true),
  130. };
  131. static bool
  132. bfd_mach_o_arm64_canonicalize_one_reloc (bfd * abfd,
  133. struct mach_o_reloc_info_external * raw,
  134. arelent * res,
  135. asymbol ** syms,
  136. arelent * res_base ATTRIBUTE_UNUSED)
  137. {
  138. bfd_mach_o_reloc_info reloc;
  139. res->address = bfd_get_32 (abfd, raw->r_address);
  140. if (res->address & BFD_MACH_O_SR_SCATTERED)
  141. {
  142. /* Only non-scattered relocations. */
  143. return false;
  144. }
  145. /* The value and info fields have to be extracted dependent on target
  146. endian-ness. */
  147. bfd_mach_o_swap_in_non_scattered_reloc (abfd, &reloc, raw->r_symbolnum);
  148. if (reloc.r_type == BFD_MACH_O_ARM64_RELOC_ADDEND)
  149. {
  150. if (reloc.r_length == 2 && reloc.r_pcrel == 0)
  151. {
  152. res->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  153. res->addend = reloc.r_value;
  154. res->howto = &arm64_howto_table[10];
  155. return true;
  156. }
  157. return false;
  158. }
  159. if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, &reloc, res, syms))
  160. return false;
  161. switch (reloc.r_type)
  162. {
  163. case BFD_MACH_O_ARM64_RELOC_UNSIGNED:
  164. switch ((reloc.r_length << 1) | reloc.r_pcrel)
  165. {
  166. case 0: /* len = 0, pcrel = 0 */
  167. res->howto = &arm64_howto_table[3];
  168. return true;
  169. case 2: /* len = 1, pcrel = 0 */
  170. res->howto = &arm64_howto_table[2];
  171. return true;
  172. case 3: /* len = 1, pcrel = 1 */
  173. res->howto = &arm64_howto_table[6];
  174. return true;
  175. case 4: /* len = 2, pcrel = 0 */
  176. res->howto = &arm64_howto_table[1];
  177. return true;
  178. case 5: /* len = 2, pcrel = 1 */
  179. res->howto = &arm64_howto_table[5];
  180. return true;
  181. case 6: /* len = 3, pcrel = 0 */
  182. res->howto = &arm64_howto_table[0];
  183. return true;
  184. case 7: /* len = 3, pcrel = 1 */
  185. res->howto = &arm64_howto_table[4];
  186. return true;
  187. default:
  188. return false;
  189. }
  190. break;
  191. case BFD_MACH_O_ARM64_RELOC_SUBTRACTOR:
  192. if (reloc.r_pcrel)
  193. return false;
  194. switch (reloc.r_length)
  195. {
  196. case 2:
  197. res->howto = &arm64_howto_table[11];
  198. return true;
  199. case 3:
  200. res->howto = &arm64_howto_table[12];
  201. return true;
  202. default:
  203. return false;
  204. }
  205. break;
  206. case BFD_MACH_O_ARM64_RELOC_BRANCH26:
  207. if (reloc.r_length == 2 && reloc.r_pcrel == 1)
  208. {
  209. res->howto = &arm64_howto_table[7];
  210. return true;
  211. }
  212. break;
  213. case BFD_MACH_O_ARM64_RELOC_PAGE21:
  214. if (reloc.r_length == 2 && reloc.r_pcrel == 1)
  215. {
  216. res->howto = &arm64_howto_table[8];
  217. return true;
  218. }
  219. break;
  220. case BFD_MACH_O_ARM64_RELOC_PAGEOFF12:
  221. if (reloc.r_length == 2 && reloc.r_pcrel == 0)
  222. {
  223. res->howto = &arm64_howto_table[9];
  224. return true;
  225. }
  226. break;
  227. case BFD_MACH_O_ARM64_RELOC_GOT_LOAD_PAGE21:
  228. if (reloc.r_length == 2 && reloc.r_pcrel == 1)
  229. {
  230. res->howto = &arm64_howto_table[13];
  231. return true;
  232. }
  233. break;
  234. case BFD_MACH_O_ARM64_RELOC_GOT_LOAD_PAGEOFF12:
  235. if (reloc.r_length == 2 && reloc.r_pcrel == 0)
  236. {
  237. res->howto = &arm64_howto_table[14];
  238. return true;
  239. }
  240. break;
  241. case BFD_MACH_O_ARM64_RELOC_POINTER_TO_GOT:
  242. if (reloc.r_length == 2 && reloc.r_pcrel == 1)
  243. {
  244. res->howto = &arm64_howto_table[15];
  245. return true;
  246. }
  247. break;
  248. default:
  249. break;
  250. }
  251. return false;
  252. }
  253. static reloc_howto_type *
  254. bfd_mach_o_arm64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  255. bfd_reloc_code_real_type code)
  256. {
  257. unsigned int i;
  258. for (i = 0;
  259. i < sizeof (arm64_howto_table) / sizeof (*arm64_howto_table);
  260. i++)
  261. if (code == arm64_howto_table[i].type)
  262. return &arm64_howto_table[i];
  263. return NULL;
  264. }
  265. static reloc_howto_type *
  266. bfd_mach_o_arm64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  267. const char *name ATTRIBUTE_UNUSED)
  268. {
  269. return NULL;
  270. }
  271. #define TARGET_NAME aarch64_mach_o_vec
  272. #define TARGET_STRING "mach-o-arm64"
  273. #define TARGET_ARCHITECTURE bfd_arch_aarch64
  274. #define TARGET_PAGESIZE 4096
  275. #define TARGET_BIG_ENDIAN 0
  276. #define TARGET_ARCHIVE 0
  277. #define TARGET_PRIORITY 0
  278. #include "mach-o-target.c"
  279. #undef TARGET_NAME
  280. #undef TARGET_STRING
  281. #undef TARGET_ARCHIVE
  282. #undef TARGET_PRIORITY