1 /* Intel x86-64 Mach-O support for BFD.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
25 #include "libiberty.h"
26 #include "mach-o/x86-64.h"
28 #define bfd_mach_o_object_p bfd_mach_o_x86_64_object_p
29 #define bfd_mach_o_core_p bfd_mach_o_x86_64_core_p
30 #define bfd_mach_o_mkobject bfd_mach_o_x86_64_mkobject
32 static const bfd_target *
33 bfd_mach_o_x86_64_object_p (bfd *abfd)
35 return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_X86_64);
38 static const bfd_target *
39 bfd_mach_o_x86_64_core_p (bfd *abfd)
41 return bfd_mach_o_header_p (abfd, 0,
42 BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_X86_64);
46 bfd_mach_o_x86_64_mkobject (bfd *abfd)
48 bfd_mach_o_data_struct *mdata;
50 if (!bfd_mach_o_mkobject_init (abfd))
53 mdata = bfd_mach_o_get_data (abfd);
54 mdata->header.magic = BFD_MACH_O_MH_MAGIC_64;
55 mdata->header.cputype = BFD_MACH_O_CPU_TYPE_X86_64;
56 mdata->header.cpusubtype =
57 BFD_MACH_O_CPU_SUBTYPE_X86_ALL | BFD_MACH_O_CPU_SUBTYPE_LIB64;
58 mdata->header.byteorder = BFD_ENDIAN_LITTLE;
59 mdata->header.version = 2;
64 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
65 #define MINUS_ONE (~ (bfd_vma) 0)
67 static reloc_howto_type x86_64_howto_table[]=
70 HOWTO(BFD_RELOC_64, 0, 4, 64, FALSE, 0,
71 complain_overflow_bitfield,
73 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
74 HOWTO(BFD_RELOC_32, 0, 2, 32, FALSE, 0,
75 complain_overflow_bitfield,
77 FALSE, 0xffffffff, 0xffffffff, FALSE),
78 HOWTO(BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0,
79 complain_overflow_bitfield,
81 FALSE, 0xffffffff, 0xffffffff, TRUE),
82 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_1, 0, 2, 32, TRUE, 0,
83 complain_overflow_bitfield,
85 FALSE, 0xffffffff, 0xffffffff, TRUE),
87 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_2, 0, 2, 32, TRUE, 0,
88 complain_overflow_bitfield,
90 FALSE, 0xffffffff, 0xffffffff, TRUE),
91 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_4, 0, 2, 32, TRUE, 0,
92 complain_overflow_bitfield,
94 FALSE, 0xffffffff, 0xffffffff, TRUE),
95 HOWTO(BFD_RELOC_MACH_O_X86_64_BRANCH32, 0, 2, 32, TRUE, 0,
96 complain_overflow_bitfield,
98 FALSE, 0xffffffff, 0xffffffff, TRUE),
99 HOWTO(BFD_RELOC_MACH_O_X86_64_GOT_LOAD, 0, 2, 32, TRUE, 0,
100 complain_overflow_bitfield,
102 FALSE, 0xffffffff, 0xffffffff, TRUE),
104 HOWTO(BFD_RELOC_MACH_O_SUBTRACTOR32, 0, 2, 32, FALSE, 0,
105 complain_overflow_bitfield,
106 NULL, "SUBTRACTOR32",
107 FALSE, 0xffffffff, 0xffffffff, FALSE),
108 HOWTO(BFD_RELOC_MACH_O_SUBTRACTOR64, 0, 4, 64, FALSE, 0,
109 complain_overflow_bitfield,
110 NULL, "SUBTRACTOR64",
111 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
112 HOWTO(BFD_RELOC_MACH_O_X86_64_GOT, 0, 2, 32, TRUE, 0,
113 complain_overflow_bitfield,
115 FALSE, 0xffffffff, 0xffffffff, TRUE),
116 HOWTO(BFD_RELOC_MACH_O_X86_64_BRANCH8, 0, 0, 8, TRUE, 0,
117 complain_overflow_bitfield,
119 FALSE, 0xff, 0xff, TRUE),
123 bfd_mach_o_x86_64_canonicalize_one_reloc (bfd *abfd,
124 struct mach_o_reloc_info_external *raw,
125 arelent *res, asymbol **syms)
127 bfd_mach_o_reloc_info reloc;
129 if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd, raw, &reloc, res, syms))
132 /* On x86-64, scattered relocs are not used. */
133 if (reloc.r_scattered)
136 switch (reloc.r_type)
138 case BFD_MACH_O_X86_64_RELOC_UNSIGNED:
141 switch (reloc.r_length)
144 res->howto = &x86_64_howto_table[1];
147 res->howto = &x86_64_howto_table[0];
152 case BFD_MACH_O_X86_64_RELOC_SIGNED:
153 if (reloc.r_length == 2 && reloc.r_pcrel)
155 res->howto = &x86_64_howto_table[2];
159 case BFD_MACH_O_X86_64_RELOC_BRANCH:
162 switch (reloc.r_length)
165 res->howto = &x86_64_howto_table[6];
171 case BFD_MACH_O_X86_64_RELOC_GOT_LOAD:
172 if (reloc.r_length == 2 && reloc.r_pcrel && reloc.r_extern)
174 res->howto = &x86_64_howto_table[7];
178 case BFD_MACH_O_X86_64_RELOC_GOT:
179 if (reloc.r_length == 2 && reloc.r_pcrel && reloc.r_extern)
181 res->howto = &x86_64_howto_table[10];
185 case BFD_MACH_O_X86_64_RELOC_SUBTRACTOR:
188 switch (reloc.r_length)
191 res->howto = &x86_64_howto_table[8];
194 res->howto = &x86_64_howto_table[9];
200 case BFD_MACH_O_X86_64_RELOC_SIGNED_1:
201 if (reloc.r_length == 2 && reloc.r_pcrel)
203 res->howto = &x86_64_howto_table[3];
207 case BFD_MACH_O_X86_64_RELOC_SIGNED_2:
208 if (reloc.r_length == 2 && reloc.r_pcrel)
210 res->howto = &x86_64_howto_table[4];
214 case BFD_MACH_O_X86_64_RELOC_SIGNED_4:
215 if (reloc.r_length == 2 && reloc.r_pcrel)
217 res->howto = &x86_64_howto_table[5];
228 bfd_mach_o_x86_64_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo)
230 rinfo->r_address = rel->address;
231 rinfo->r_scattered = 0;
232 switch (rel->howto->type)
235 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_UNSIGNED;
240 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_UNSIGNED;
244 case BFD_RELOC_32_PCREL:
245 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED;
249 case BFD_RELOC_MACH_O_X86_64_PCREL32_1:
250 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_1;
254 case BFD_RELOC_MACH_O_X86_64_PCREL32_2:
255 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_2;
259 case BFD_RELOC_MACH_O_X86_64_PCREL32_4:
260 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_4;
264 case BFD_RELOC_MACH_O_X86_64_BRANCH32:
265 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_BRANCH;
269 case BFD_RELOC_MACH_O_SUBTRACTOR32:
270 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SUBTRACTOR;
274 case BFD_RELOC_MACH_O_SUBTRACTOR64:
275 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SUBTRACTOR;
279 case BFD_RELOC_MACH_O_X86_64_GOT:
280 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_GOT;
284 case BFD_RELOC_MACH_O_X86_64_GOT_LOAD:
285 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_GOT_LOAD;
292 if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM)
296 (*rel->sym_ptr_ptr)->section->output_section->target_index;
301 rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i;
306 static reloc_howto_type *
307 bfd_mach_o_x86_64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
308 bfd_reloc_code_real_type code)
313 i < sizeof (x86_64_howto_table) / sizeof (*x86_64_howto_table);
315 if (code == x86_64_howto_table[i].type)
316 return &x86_64_howto_table[i];
320 static reloc_howto_type *
321 bfd_mach_o_x86_64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
322 const char *name ATTRIBUTE_UNUSED)
328 bfd_mach_o_section_type_valid_for_x86_64 (unsigned long val)
330 if (val == BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
331 || val == BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
332 || val == BFD_MACH_O_S_SYMBOL_STUBS)
337 /* We want to bump the alignment of some sections. */
338 static const mach_o_section_name_xlat text_section_names_xlat[] =
340 { ".eh_frame", "__eh_frame",
341 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_COALESCED,
342 BFD_MACH_O_S_ATTR_LIVE_SUPPORT
343 | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
344 | BFD_MACH_O_S_ATTR_NO_TOC, 3},
345 { NULL, NULL, 0, 0, 0, 0}
348 const mach_o_segment_name_xlat mach_o_x86_64_segsec_names_xlat[] =
350 { "__TEXT", text_section_names_xlat },
354 #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_x86_64_canonicalize_one_reloc
355 #define bfd_mach_o_swap_reloc_out bfd_mach_o_x86_64_swap_reloc_out
357 #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_x86_64_bfd_reloc_type_lookup
358 #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_x86_64_bfd_reloc_name_lookup
359 #define bfd_mach_o_print_thread NULL
360 #define bfd_mach_o_tgt_seg_table mach_o_x86_64_segsec_names_xlat
361 #define bfd_mach_o_section_type_valid_for_tgt bfd_mach_o_section_type_valid_for_x86_64
363 #define TARGET_NAME x86_64_mach_o_vec
364 #define TARGET_STRING "mach-o-x86-64"
365 #define TARGET_ARCHITECTURE bfd_arch_i386
366 #define TARGET_PAGESIZE 4096
367 #define TARGET_BIG_ENDIAN 0
368 #define TARGET_ARCHIVE 0
369 #define TARGET_PRIORITY 0
370 #include "mach-o-target.c"