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