mach-o-x86-64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Intel x86-64 Mach-O support for BFD.
  2. Copyright (C) 2010-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/x86-64.h"
  22. #define bfd_mach_o_object_p bfd_mach_o_x86_64_object_p
  23. #define bfd_mach_o_core_p bfd_mach_o_x86_64_core_p
  24. #define bfd_mach_o_mkobject bfd_mach_o_x86_64_mkobject
  25. static bfd_cleanup
  26. bfd_mach_o_x86_64_object_p (bfd *abfd)
  27. {
  28. return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_X86_64);
  29. }
  30. static bfd_cleanup
  31. bfd_mach_o_x86_64_core_p (bfd *abfd)
  32. {
  33. return bfd_mach_o_header_p (abfd, 0,
  34. BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_X86_64);
  35. }
  36. static bool
  37. bfd_mach_o_x86_64_mkobject (bfd *abfd)
  38. {
  39. bfd_mach_o_data_struct *mdata;
  40. if (!bfd_mach_o_mkobject_init (abfd))
  41. return false;
  42. mdata = bfd_mach_o_get_data (abfd);
  43. mdata->header.magic = BFD_MACH_O_MH_MAGIC_64;
  44. mdata->header.cputype = BFD_MACH_O_CPU_TYPE_X86_64;
  45. mdata->header.cpusubtype =
  46. BFD_MACH_O_CPU_SUBTYPE_X86_ALL | BFD_MACH_O_CPU_SUBTYPE_LIB64;
  47. mdata->header.byteorder = BFD_ENDIAN_LITTLE;
  48. mdata->header.version = 2;
  49. return true;
  50. }
  51. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
  52. #define MINUS_ONE (~ (bfd_vma) 0)
  53. static reloc_howto_type x86_64_howto_table[]=
  54. {
  55. /* 0 */
  56. HOWTO(BFD_RELOC_64, 0, 4, 64, false, 0,
  57. complain_overflow_bitfield,
  58. NULL, "64",
  59. false, MINUS_ONE, MINUS_ONE, false),
  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_32_PCREL, 0, 2, 32, true, 0,
  65. complain_overflow_bitfield,
  66. NULL, "DISP32",
  67. false, 0xffffffff, 0xffffffff, true),
  68. HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_1, 0, 2, 32, true, 0,
  69. complain_overflow_bitfield,
  70. NULL, "DISP32_1",
  71. false, 0xffffffff, 0xffffffff, true),
  72. /* 4 */
  73. HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_2, 0, 2, 32, true, 0,
  74. complain_overflow_bitfield,
  75. NULL, "DISP32_2",
  76. false, 0xffffffff, 0xffffffff, true),
  77. HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_4, 0, 2, 32, true, 0,
  78. complain_overflow_bitfield,
  79. NULL, "DISP32_4",
  80. false, 0xffffffff, 0xffffffff, true),
  81. HOWTO(BFD_RELOC_MACH_O_X86_64_BRANCH32, 0, 2, 32, true, 0,
  82. complain_overflow_bitfield,
  83. NULL, "BRANCH32",
  84. false, 0xffffffff, 0xffffffff, true),
  85. HOWTO(BFD_RELOC_MACH_O_X86_64_GOT_LOAD, 0, 2, 32, true, 0,
  86. complain_overflow_bitfield,
  87. NULL, "GOT_LOAD",
  88. false, 0xffffffff, 0xffffffff, true),
  89. /* 8 */
  90. HOWTO(BFD_RELOC_MACH_O_SUBTRACTOR32, 0, 2, 32, false, 0,
  91. complain_overflow_bitfield,
  92. NULL, "SUBTRACTOR32",
  93. false, 0xffffffff, 0xffffffff, false),
  94. HOWTO(BFD_RELOC_MACH_O_SUBTRACTOR64, 0, 4, 64, false, 0,
  95. complain_overflow_bitfield,
  96. NULL, "SUBTRACTOR64",
  97. false, MINUS_ONE, MINUS_ONE, false),
  98. HOWTO(BFD_RELOC_MACH_O_X86_64_GOT, 0, 2, 32, true, 0,
  99. complain_overflow_bitfield,
  100. NULL, "GOT",
  101. false, 0xffffffff, 0xffffffff, true),
  102. HOWTO(BFD_RELOC_MACH_O_X86_64_BRANCH8, 0, 0, 8, true, 0,
  103. complain_overflow_bitfield,
  104. NULL, "BRANCH8",
  105. false, 0xff, 0xff, true),
  106. /* 12 */
  107. HOWTO(BFD_RELOC_MACH_O_X86_64_TLV, 0, 2, 32, true, 0,
  108. complain_overflow_bitfield,
  109. NULL, "TLV",
  110. false, 0xffffffff, 0xffffffff, true),
  111. };
  112. static bool
  113. bfd_mach_o_x86_64_canonicalize_one_reloc (bfd * abfd,
  114. struct mach_o_reloc_info_external * raw,
  115. arelent * res,
  116. asymbol ** syms,
  117. arelent * res_base ATTRIBUTE_UNUSED)
  118. {
  119. bfd_mach_o_reloc_info reloc;
  120. if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd, raw, &reloc, res, syms))
  121. return false;
  122. /* On x86-64, scattered relocs are not used. */
  123. if (reloc.r_scattered)
  124. return false;
  125. switch (reloc.r_type)
  126. {
  127. case BFD_MACH_O_X86_64_RELOC_UNSIGNED:
  128. if (reloc.r_pcrel)
  129. return false;
  130. switch (reloc.r_length)
  131. {
  132. case 2:
  133. res->howto = &x86_64_howto_table[1];
  134. return true;
  135. case 3:
  136. res->howto = &x86_64_howto_table[0];
  137. return true;
  138. default:
  139. return false;
  140. }
  141. case BFD_MACH_O_X86_64_RELOC_SIGNED:
  142. if (reloc.r_length == 2 && reloc.r_pcrel)
  143. {
  144. res->howto = &x86_64_howto_table[2];
  145. return true;
  146. }
  147. break;
  148. case BFD_MACH_O_X86_64_RELOC_BRANCH:
  149. if (!reloc.r_pcrel)
  150. return false;
  151. switch (reloc.r_length)
  152. {
  153. case 2:
  154. res->howto = &x86_64_howto_table[6];
  155. return true;
  156. default:
  157. return false;
  158. }
  159. break;
  160. case BFD_MACH_O_X86_64_RELOC_GOT_LOAD:
  161. if (reloc.r_length == 2 && reloc.r_pcrel && reloc.r_extern)
  162. {
  163. res->howto = &x86_64_howto_table[7];
  164. return true;
  165. }
  166. break;
  167. case BFD_MACH_O_X86_64_RELOC_GOT:
  168. if (reloc.r_length == 2 && reloc.r_pcrel && reloc.r_extern)
  169. {
  170. res->howto = &x86_64_howto_table[10];
  171. return true;
  172. }
  173. break;
  174. case BFD_MACH_O_X86_64_RELOC_SUBTRACTOR:
  175. if (reloc.r_pcrel)
  176. return false;
  177. switch (reloc.r_length)
  178. {
  179. case 2:
  180. res->howto = &x86_64_howto_table[8];
  181. return true;
  182. case 3:
  183. res->howto = &x86_64_howto_table[9];
  184. return true;
  185. default:
  186. return false;
  187. }
  188. break;
  189. case BFD_MACH_O_X86_64_RELOC_SIGNED_1:
  190. if (reloc.r_length == 2 && reloc.r_pcrel)
  191. {
  192. res->howto = &x86_64_howto_table[3];
  193. return true;
  194. }
  195. break;
  196. case BFD_MACH_O_X86_64_RELOC_SIGNED_2:
  197. if (reloc.r_length == 2 && reloc.r_pcrel)
  198. {
  199. res->howto = &x86_64_howto_table[4];
  200. return true;
  201. }
  202. break;
  203. case BFD_MACH_O_X86_64_RELOC_SIGNED_4:
  204. if (reloc.r_length == 2 && reloc.r_pcrel)
  205. {
  206. res->howto = &x86_64_howto_table[5];
  207. return true;
  208. }
  209. break;
  210. case BFD_MACH_O_X86_64_RELOC_TLV:
  211. if (reloc.r_length == 2 && reloc.r_pcrel && reloc.r_extern)
  212. {
  213. res->howto = &x86_64_howto_table[12];
  214. return true;
  215. }
  216. break;
  217. default:
  218. return false;
  219. }
  220. return false;
  221. }
  222. static bool
  223. bfd_mach_o_x86_64_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo)
  224. {
  225. rinfo->r_address = rel->address;
  226. rinfo->r_scattered = 0;
  227. switch (rel->howto->type)
  228. {
  229. case BFD_RELOC_32:
  230. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_UNSIGNED;
  231. rinfo->r_pcrel = 0;
  232. rinfo->r_length = 2;
  233. break;
  234. case BFD_RELOC_64:
  235. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_UNSIGNED;
  236. rinfo->r_pcrel = 0;
  237. rinfo->r_length = 3;
  238. break;
  239. case BFD_RELOC_32_PCREL:
  240. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED;
  241. rinfo->r_pcrel = 1;
  242. rinfo->r_length = 2;
  243. break;
  244. case BFD_RELOC_MACH_O_X86_64_PCREL32_1:
  245. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_1;
  246. rinfo->r_pcrel = 1;
  247. rinfo->r_length = 2;
  248. break;
  249. case BFD_RELOC_MACH_O_X86_64_PCREL32_2:
  250. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_2;
  251. rinfo->r_pcrel = 1;
  252. rinfo->r_length = 2;
  253. break;
  254. case BFD_RELOC_MACH_O_X86_64_PCREL32_4:
  255. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_4;
  256. rinfo->r_pcrel = 1;
  257. rinfo->r_length = 2;
  258. break;
  259. case BFD_RELOC_MACH_O_X86_64_BRANCH32:
  260. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_BRANCH;
  261. rinfo->r_pcrel = 1;
  262. rinfo->r_length = 2;
  263. break;
  264. case BFD_RELOC_MACH_O_SUBTRACTOR32:
  265. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SUBTRACTOR;
  266. rinfo->r_pcrel = 0;
  267. rinfo->r_length = 2;
  268. break;
  269. case BFD_RELOC_MACH_O_SUBTRACTOR64:
  270. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SUBTRACTOR;
  271. rinfo->r_pcrel = 0;
  272. rinfo->r_length = 3;
  273. break;
  274. case BFD_RELOC_MACH_O_X86_64_GOT:
  275. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_GOT;
  276. rinfo->r_pcrel = 1;
  277. rinfo->r_length = 2;
  278. break;
  279. case BFD_RELOC_MACH_O_X86_64_GOT_LOAD:
  280. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_GOT_LOAD;
  281. rinfo->r_pcrel = 1;
  282. rinfo->r_length = 2;
  283. break;
  284. case BFD_RELOC_MACH_O_X86_64_TLV:
  285. rinfo->r_type = BFD_MACH_O_X86_64_RELOC_TLV;
  286. rinfo->r_pcrel = 1;
  287. rinfo->r_length = 2;
  288. break;
  289. default:
  290. return false;
  291. }
  292. if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
  293. {
  294. rinfo->r_extern = 0;
  295. rinfo->r_value =
  296. (*rel->sym_ptr_ptr)->section->output_section->target_index;
  297. }
  298. else
  299. {
  300. rinfo->r_extern = 1;
  301. rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i;
  302. }
  303. return true;
  304. }
  305. static reloc_howto_type *
  306. bfd_mach_o_x86_64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  307. bfd_reloc_code_real_type code)
  308. {
  309. unsigned int i;
  310. for (i = 0;
  311. i < sizeof (x86_64_howto_table) / sizeof (*x86_64_howto_table);
  312. i++)
  313. if (code == x86_64_howto_table[i].type)
  314. return &x86_64_howto_table[i];
  315. return NULL;
  316. }
  317. static reloc_howto_type *
  318. bfd_mach_o_x86_64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  319. const char *name ATTRIBUTE_UNUSED)
  320. {
  321. return NULL;
  322. }
  323. static bool
  324. bfd_mach_o_section_type_valid_for_x86_64 (unsigned long val)
  325. {
  326. if (val == BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
  327. || val == BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
  328. || val == BFD_MACH_O_S_SYMBOL_STUBS)
  329. return false;
  330. return true;
  331. }
  332. /* We want to bump the alignment of some sections. */
  333. static const mach_o_section_name_xlat text_section_names_xlat[] =
  334. {
  335. { ".eh_frame", "__eh_frame",
  336. SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_COALESCED,
  337. BFD_MACH_O_S_ATTR_LIVE_SUPPORT
  338. | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
  339. | BFD_MACH_O_S_ATTR_NO_TOC, 3},
  340. { NULL, NULL, 0, 0, 0, 0}
  341. };
  342. const mach_o_segment_name_xlat mach_o_x86_64_segsec_names_xlat[] =
  343. {
  344. { "__TEXT", text_section_names_xlat },
  345. { NULL, NULL }
  346. };
  347. #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_x86_64_canonicalize_one_reloc
  348. #define bfd_mach_o_swap_reloc_out bfd_mach_o_x86_64_swap_reloc_out
  349. #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_x86_64_bfd_reloc_type_lookup
  350. #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_x86_64_bfd_reloc_name_lookup
  351. #define bfd_mach_o_print_thread NULL
  352. #define bfd_mach_o_tgt_seg_table mach_o_x86_64_segsec_names_xlat
  353. #define bfd_mach_o_section_type_valid_for_tgt bfd_mach_o_section_type_valid_for_x86_64
  354. #define TARGET_NAME x86_64_mach_o_vec
  355. #define TARGET_STRING "mach-o-x86-64"
  356. #define TARGET_ARCHITECTURE bfd_arch_i386
  357. #define TARGET_PAGESIZE 4096
  358. #define TARGET_BIG_ENDIAN 0
  359. #define TARGET_ARCHIVE 0
  360. #define TARGET_PRIORITY 0
  361. #include "mach-o-target.c"