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