mach-o-arm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* ARM 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/arm.h"
  22. #define bfd_mach_o_object_p bfd_mach_o_arm_object_p
  23. #define bfd_mach_o_core_p bfd_mach_o_arm_core_p
  24. #define bfd_mach_o_mkobject bfd_mach_o_arm_mkobject
  25. #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_arm_canonicalize_one_reloc
  26. #define bfd_mach_o_swap_reloc_out NULL
  27. #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_arm_bfd_reloc_type_lookup
  28. #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_arm_bfd_reloc_name_lookup
  29. #define bfd_mach_o_print_thread NULL
  30. #define bfd_mach_o_tgt_seg_table NULL
  31. #define bfd_mach_o_section_type_valid_for_tgt NULL
  32. static bfd_cleanup
  33. bfd_mach_o_arm_object_p (bfd *abfd)
  34. {
  35. return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_ARM);
  36. }
  37. static bfd_cleanup
  38. bfd_mach_o_arm_core_p (bfd *abfd)
  39. {
  40. return bfd_mach_o_header_p (abfd, 0,
  41. BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_ARM);
  42. }
  43. static bool
  44. bfd_mach_o_arm_mkobject (bfd *abfd)
  45. {
  46. bfd_mach_o_data_struct *mdata;
  47. if (!bfd_mach_o_mkobject_init (abfd))
  48. return false;
  49. mdata = bfd_mach_o_get_data (abfd);
  50. mdata->header.magic = BFD_MACH_O_MH_MAGIC;
  51. mdata->header.cputype = BFD_MACH_O_CPU_TYPE_ARM;
  52. mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_ARM_ALL;
  53. mdata->header.byteorder = BFD_ENDIAN_LITTLE;
  54. mdata->header.version = 1;
  55. return true;
  56. }
  57. static reloc_howto_type arm_howto_table[]=
  58. {
  59. /* 0 */
  60. HOWTO (BFD_RELOC_32, 0, 2, 32, false, 0,
  61. complain_overflow_bitfield,
  62. NULL, "32",
  63. false, 0xffffffff, 0xffffffff, false),
  64. HOWTO (BFD_RELOC_16, 0, 1, 16, false, 0,
  65. complain_overflow_bitfield,
  66. NULL, "16",
  67. false, 0xffff, 0xffff, false),
  68. HOWTO (BFD_RELOC_8, 0, 0, 8, false, 0,
  69. complain_overflow_bitfield,
  70. NULL, "8",
  71. false, 0xff, 0xff, false),
  72. HOWTO (BFD_RELOC_32_PCREL, 0, 2, 32, true, 0,
  73. complain_overflow_bitfield,
  74. NULL, "DISP32",
  75. false, 0xffffffff, 0xffffffff, true),
  76. /* 4 */
  77. HOWTO (BFD_RELOC_16_PCREL, 0, 1, 16, true, 0,
  78. complain_overflow_bitfield,
  79. NULL, "DISP16",
  80. false, 0xffff, 0xffff, true),
  81. HOWTO (BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 32, false, 0,
  82. complain_overflow_bitfield,
  83. NULL, "SECTDIFF_32",
  84. false, 0xffffffff, 0xffffffff, false),
  85. HOWTO (BFD_RELOC_MACH_O_LOCAL_SECTDIFF, 0, 2, 32, false, 0,
  86. complain_overflow_bitfield,
  87. NULL, "LSECTDIFF_32",
  88. false, 0xffffffff, 0xffffffff, false),
  89. HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 2, 32, false, 0,
  90. complain_overflow_bitfield,
  91. NULL, "PAIR_32",
  92. false, 0xffffffff, 0xffffffff, false),
  93. /* 8 */
  94. HOWTO (BFD_RELOC_MACH_O_SECTDIFF, 0, 1, 16, false, 0,
  95. complain_overflow_bitfield,
  96. NULL, "SECTDIFF_16",
  97. false, 0xffff, 0xffff, false),
  98. HOWTO (BFD_RELOC_MACH_O_LOCAL_SECTDIFF, 0, 1, 16, false, 0,
  99. complain_overflow_bitfield,
  100. NULL, "LSECTDIFF_16",
  101. false, 0xffff, 0xffff, false),
  102. HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 1, 16, false, 0,
  103. complain_overflow_bitfield,
  104. NULL, "PAIR_16",
  105. false, 0xffff, 0xffff, false),
  106. HOWTO (BFD_RELOC_ARM_PCREL_CALL, 2, 2, 24, true, 0,
  107. complain_overflow_signed,
  108. NULL, "BR24",
  109. false, 0x00ffffff, 0x00ffffff, true),
  110. /* 12 */
  111. HOWTO (BFD_RELOC_ARM_MOVW, 0, 2, 16, false, 0,
  112. complain_overflow_dont,
  113. NULL, "MOVW",
  114. false, 0x000f0fff, 0x000f0fff, false),
  115. HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 2, 16, false, 0,
  116. complain_overflow_bitfield,
  117. NULL, "PAIR_W",
  118. false, 0x000f0fff, 0x000f0fff, false),
  119. HOWTO (BFD_RELOC_ARM_MOVT, 0, 2, 16, false, 0,
  120. complain_overflow_bitfield,
  121. NULL, "MOVT",
  122. false, 0x000f0fff, 0x000f0fff, false),
  123. HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 2, 16, false, 0,
  124. complain_overflow_bitfield,
  125. NULL, "PAIR_T",
  126. false, 0x000f0fff, 0x000f0fff, false),
  127. /* 16 */
  128. HOWTO (BFD_RELOC_THUMB_PCREL_BLX, 2, 2, 24, true, 0,
  129. complain_overflow_signed,
  130. NULL, "TBR22",
  131. false, 0x07ff2fff, 0x07ff2fff, true)
  132. };
  133. static bool
  134. bfd_mach_o_arm_canonicalize_one_reloc (bfd * abfd,
  135. struct mach_o_reloc_info_external * raw,
  136. arelent * res,
  137. asymbol ** syms,
  138. arelent * res_base)
  139. {
  140. bfd_mach_o_reloc_info reloc;
  141. if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd, raw, &reloc, res, syms))
  142. return false;
  143. if (reloc.r_scattered)
  144. {
  145. switch (reloc.r_type)
  146. {
  147. case BFD_MACH_O_ARM_RELOC_PAIR:
  148. /* PR 21813: Check for a corrupt PAIR reloc at the start. */
  149. if (res == res_base)
  150. {
  151. _bfd_error_handler (_("malformed mach-o ARM reloc pair: "
  152. "reloc is first reloc"));
  153. return false;
  154. }
  155. if (reloc.r_length == 2)
  156. {
  157. res->howto = &arm_howto_table[7];
  158. res->address = res[-1].address;
  159. return true;
  160. }
  161. else if (reloc.r_length == 1)
  162. {
  163. res->howto = &arm_howto_table[10];
  164. res->address = res[-1].address;
  165. return true;
  166. }
  167. _bfd_error_handler (_("malformed mach-o ARM reloc pair: "
  168. "invalid length: %d"), reloc.r_length);
  169. return false;
  170. case BFD_MACH_O_ARM_RELOC_SECTDIFF:
  171. if (reloc.r_length == 2)
  172. {
  173. res->howto = &arm_howto_table[5];
  174. return true;
  175. }
  176. else if (reloc.r_length == 1)
  177. {
  178. res->howto = &arm_howto_table[8];
  179. return true;
  180. }
  181. _bfd_error_handler (_("malformed mach-o ARM sectdiff reloc: "
  182. "invalid length: %d"), reloc.r_length);
  183. return false;
  184. case BFD_MACH_O_ARM_RELOC_LOCAL_SECTDIFF:
  185. if (reloc.r_length == 2)
  186. {
  187. res->howto = &arm_howto_table[6];
  188. return true;
  189. }
  190. else if (reloc.r_length == 1)
  191. {
  192. res->howto = &arm_howto_table[9];
  193. return true;
  194. }
  195. _bfd_error_handler (_("malformed mach-o ARM local sectdiff reloc: "
  196. "invalid length: %d"),
  197. reloc.r_length);
  198. return false;
  199. case BFD_MACH_O_ARM_RELOC_HALF_SECTDIFF:
  200. switch (reloc.r_length)
  201. {
  202. case 2: /* :lower16: for movw arm. */
  203. res->howto = &arm_howto_table[12];
  204. return true;
  205. case 3: /* :upper16: for movt arm. */
  206. res->howto = &arm_howto_table[14];
  207. return true;
  208. }
  209. _bfd_error_handler (_("malformed mach-o ARM half sectdiff reloc: "
  210. "invalid length: %d"),
  211. reloc.r_length);
  212. return false;
  213. default:
  214. break;
  215. }
  216. }
  217. else
  218. {
  219. switch (reloc.r_type)
  220. {
  221. case BFD_MACH_O_ARM_RELOC_VANILLA:
  222. switch ((reloc.r_length << 1) | reloc.r_pcrel)
  223. {
  224. case 0: /* len = 0, pcrel = 0 */
  225. res->howto = &arm_howto_table[2];
  226. return true;
  227. case 2: /* len = 1, pcrel = 0 */
  228. res->howto = &arm_howto_table[1];
  229. return true;
  230. case 3: /* len = 1, pcrel = 1 */
  231. res->howto = &arm_howto_table[4];
  232. return true;
  233. case 4: /* len = 2, pcrel = 0 */
  234. res->howto = &arm_howto_table[0];
  235. return true;
  236. case 5: /* len = 2, pcrel = 1 */
  237. res->howto = &arm_howto_table[3];
  238. return true;
  239. default:
  240. _bfd_error_handler (_("malformed mach-o ARM vanilla reloc: "
  241. "invalid length: %d (pcrel: %d)"),
  242. reloc.r_length, reloc.r_pcrel);
  243. return false;
  244. }
  245. break;
  246. case BFD_MACH_O_ARM_RELOC_BR24:
  247. if (reloc.r_length == 2 && reloc.r_pcrel == 1)
  248. {
  249. res->howto = &arm_howto_table[11];
  250. return true;
  251. }
  252. break;
  253. case BFD_MACH_O_THUMB_RELOC_BR22:
  254. if (reloc.r_length == 2 && reloc.r_pcrel == 1)
  255. {
  256. res->howto = &arm_howto_table[16];
  257. return true;
  258. }
  259. break;
  260. case BFD_MACH_O_ARM_RELOC_HALF:
  261. if (reloc.r_pcrel == 0)
  262. switch (reloc.r_length)
  263. {
  264. case 0: /* :lower16: for movw arm. */
  265. res->howto = &arm_howto_table[12];
  266. return true;
  267. case 1: /* :upper16: for movt arm. */
  268. res->howto = &arm_howto_table[14];
  269. return true;
  270. }
  271. break;
  272. case BFD_MACH_O_ARM_RELOC_PAIR:
  273. if (res == res_base)
  274. {
  275. _bfd_error_handler (_("malformed mach-o ARM reloc pair: "
  276. "reloc is first reloc"));
  277. return false;
  278. }
  279. if (res[-1].howto == &arm_howto_table[12]
  280. && reloc.r_length == 0)
  281. {
  282. /* Pair for :lower16: of movw arm. */
  283. res->howto = &arm_howto_table[13];
  284. /* This reloc contains the other half in its r_address field. */
  285. res[-1].addend += (res->address & 0xffff) << 16;
  286. res->address = res[-1].address;
  287. return true;
  288. }
  289. else if (res[-1].howto == &arm_howto_table[14]
  290. && reloc.r_length == 1)
  291. {
  292. /* Pair for :upper16: of movt arm. */
  293. res->howto = &arm_howto_table[15];
  294. /* This reloc contains the other half in its r_address field. */
  295. res[-1].addend += res->address & 0xffff;
  296. res->address = res[-1].address;
  297. return true;
  298. }
  299. break;
  300. default:
  301. break;
  302. }
  303. }
  304. _bfd_error_handler (_("malformed mach-o ARM reloc: "
  305. "unknown reloc type: %d"), reloc.r_length);
  306. return false;
  307. }
  308. static reloc_howto_type *
  309. bfd_mach_o_arm_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  310. bfd_reloc_code_real_type code)
  311. {
  312. unsigned int i;
  313. for (i = 0; i < sizeof (arm_howto_table) / sizeof (*arm_howto_table); i++)
  314. if (code == arm_howto_table[i].type)
  315. return &arm_howto_table[i];
  316. return NULL;
  317. }
  318. static reloc_howto_type *
  319. bfd_mach_o_arm_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  320. const char *name ATTRIBUTE_UNUSED)
  321. {
  322. return NULL;
  323. }
  324. #define TARGET_NAME arm_mach_o_vec
  325. #define TARGET_STRING "mach-o-arm"
  326. #define TARGET_ARCHITECTURE bfd_arch_arm
  327. #define TARGET_PAGESIZE 4096
  328. #define TARGET_BIG_ENDIAN 0
  329. #define TARGET_ARCHIVE 0
  330. #define TARGET_PRIORITY 0
  331. #include "mach-o-target.c"