Commit | Line | Data |
---|---|---|
618b7301 | 1 | /* Intel x86-64 Mach-O support for BFD. |
b3adc24a | 2 | Copyright (C) 2010-2020 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" | |
618b7301 TG |
22 | #include "bfd.h" |
23 | #include "libbfd.h" | |
24 | #include "libiberty.h" | |
2d5d5a8f | 25 | #include "mach-o.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 | ||
cb001c0d | 32 | static bfd_cleanup |
618b7301 TG |
33 | bfd_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 | ||
cb001c0d | 38 | static bfd_cleanup |
618b7301 TG |
39 | bfd_mach_o_x86_64_core_p (bfd *abfd) |
40 | { | |
47daa70f | 41 | return bfd_mach_o_header_p (abfd, 0, |
07d6d2b8 | 42 | BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_X86_64); |
618b7301 TG |
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); | |
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 | ||
67 | static 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), | |
ed1299fe M |
120 | /* 12 */ |
121 | HOWTO(BFD_RELOC_MACH_O_X86_64_TLV, 0, 2, 32, TRUE, 0, | |
122 | complain_overflow_bitfield, | |
123 | NULL, "TLV", | |
124 | FALSE, 0xffffffff, 0xffffffff, TRUE), | |
618b7301 TG |
125 | }; |
126 | ||
127 | static bfd_boolean | |
ca4cf9b9 NC |
128 | bfd_mach_o_x86_64_canonicalize_one_reloc (bfd * abfd, |
129 | struct mach_o_reloc_info_external * raw, | |
130 | arelent * res, | |
131 | asymbol ** syms, | |
132 | arelent * res_base ATTRIBUTE_UNUSED) | |
618b7301 | 133 | { |
bcb51645 TG |
134 | bfd_mach_o_reloc_info reloc; |
135 | ||
136 | if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd, raw, &reloc, res, syms)) | |
137 | return FALSE; | |
138 | ||
618b7301 | 139 | /* On x86-64, scattered relocs are not used. */ |
bcb51645 | 140 | if (reloc.r_scattered) |
618b7301 TG |
141 | return FALSE; |
142 | ||
bcb51645 | 143 | switch (reloc.r_type) |
618b7301 TG |
144 | { |
145 | case BFD_MACH_O_X86_64_RELOC_UNSIGNED: | |
bcb51645 | 146 | if (reloc.r_pcrel) |
07d6d2b8 | 147 | return FALSE; |
bcb51645 | 148 | switch (reloc.r_length) |
07d6d2b8 AM |
149 | { |
150 | case 2: | |
151 | res->howto = &x86_64_howto_table[1]; | |
152 | return TRUE; | |
153 | case 3: | |
154 | res->howto = &x86_64_howto_table[0]; | |
155 | return TRUE; | |
156 | default: | |
157 | return FALSE; | |
158 | } | |
618b7301 | 159 | case BFD_MACH_O_X86_64_RELOC_SIGNED: |
bcb51645 | 160 | if (reloc.r_length == 2 && reloc.r_pcrel) |
07d6d2b8 AM |
161 | { |
162 | res->howto = &x86_64_howto_table[2]; | |
163 | return TRUE; | |
164 | } | |
618b7301 TG |
165 | break; |
166 | case BFD_MACH_O_X86_64_RELOC_BRANCH: | |
bcb51645 | 167 | if (!reloc.r_pcrel) |
07d6d2b8 | 168 | return FALSE; |
bcb51645 | 169 | switch (reloc.r_length) |
07d6d2b8 AM |
170 | { |
171 | case 2: | |
172 | res->howto = &x86_64_howto_table[6]; | |
173 | return TRUE; | |
174 | default: | |
175 | return FALSE; | |
176 | } | |
618b7301 TG |
177 | break; |
178 | case BFD_MACH_O_X86_64_RELOC_GOT_LOAD: | |
bcb51645 | 179 | if (reloc.r_length == 2 && reloc.r_pcrel && reloc.r_extern) |
07d6d2b8 AM |
180 | { |
181 | res->howto = &x86_64_howto_table[7]; | |
182 | return TRUE; | |
183 | } | |
618b7301 TG |
184 | break; |
185 | case BFD_MACH_O_X86_64_RELOC_GOT: | |
bcb51645 | 186 | if (reloc.r_length == 2 && reloc.r_pcrel && reloc.r_extern) |
07d6d2b8 AM |
187 | { |
188 | res->howto = &x86_64_howto_table[10]; | |
189 | return TRUE; | |
190 | } | |
618b7301 TG |
191 | break; |
192 | case BFD_MACH_O_X86_64_RELOC_SUBTRACTOR: | |
bcb51645 | 193 | if (reloc.r_pcrel) |
07d6d2b8 | 194 | return FALSE; |
bcb51645 | 195 | switch (reloc.r_length) |
07d6d2b8 AM |
196 | { |
197 | case 2: | |
198 | res->howto = &x86_64_howto_table[8]; | |
199 | return TRUE; | |
200 | case 3: | |
201 | res->howto = &x86_64_howto_table[9]; | |
202 | return TRUE; | |
203 | default: | |
204 | return FALSE; | |
205 | } | |
618b7301 TG |
206 | break; |
207 | case BFD_MACH_O_X86_64_RELOC_SIGNED_1: | |
bcb51645 | 208 | if (reloc.r_length == 2 && reloc.r_pcrel) |
07d6d2b8 AM |
209 | { |
210 | res->howto = &x86_64_howto_table[3]; | |
211 | return TRUE; | |
212 | } | |
618b7301 TG |
213 | break; |
214 | case BFD_MACH_O_X86_64_RELOC_SIGNED_2: | |
bcb51645 | 215 | if (reloc.r_length == 2 && reloc.r_pcrel) |
07d6d2b8 AM |
216 | { |
217 | res->howto = &x86_64_howto_table[4]; | |
218 | return TRUE; | |
219 | } | |
618b7301 TG |
220 | break; |
221 | case BFD_MACH_O_X86_64_RELOC_SIGNED_4: | |
bcb51645 | 222 | if (reloc.r_length == 2 && reloc.r_pcrel) |
07d6d2b8 AM |
223 | { |
224 | res->howto = &x86_64_howto_table[5]; | |
225 | return TRUE; | |
226 | } | |
618b7301 | 227 | break; |
ed1299fe M |
228 | case BFD_MACH_O_X86_64_RELOC_TLV: |
229 | if (reloc.r_length == 2 && reloc.r_pcrel && reloc.r_extern) | |
230 | { | |
231 | res->howto = &x86_64_howto_table[12]; | |
232 | return TRUE; | |
233 | } | |
234 | break; | |
618b7301 TG |
235 | default: |
236 | return FALSE; | |
237 | } | |
238 | return FALSE; | |
239 | } | |
240 | ||
241 | static bfd_boolean | |
242 | bfd_mach_o_x86_64_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo) | |
243 | { | |
244 | rinfo->r_address = rel->address; | |
fde33808 | 245 | rinfo->r_scattered = 0; |
618b7301 TG |
246 | switch (rel->howto->type) |
247 | { | |
71141310 TG |
248 | case BFD_RELOC_32: |
249 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_UNSIGNED; | |
250 | rinfo->r_pcrel = 0; | |
251 | rinfo->r_length = 2; | |
252 | break; | |
618b7301 | 253 | case BFD_RELOC_64: |
618b7301 TG |
254 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_UNSIGNED; |
255 | rinfo->r_pcrel = 0; | |
fde33808 TG |
256 | rinfo->r_length = 3; |
257 | break; | |
258 | case BFD_RELOC_32_PCREL: | |
71141310 TG |
259 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED; |
260 | rinfo->r_pcrel = 1; | |
261 | rinfo->r_length = 2; | |
262 | break; | |
263 | case BFD_RELOC_MACH_O_X86_64_PCREL32_1: | |
264 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_1; | |
265 | rinfo->r_pcrel = 1; | |
266 | rinfo->r_length = 2; | |
267 | break; | |
268 | case BFD_RELOC_MACH_O_X86_64_PCREL32_2: | |
269 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_2; | |
270 | rinfo->r_pcrel = 1; | |
271 | rinfo->r_length = 2; | |
272 | break; | |
273 | case BFD_RELOC_MACH_O_X86_64_PCREL32_4: | |
274 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_4; | |
275 | rinfo->r_pcrel = 1; | |
276 | rinfo->r_length = 2; | |
277 | break; | |
278 | case BFD_RELOC_MACH_O_X86_64_BRANCH32: | |
fde33808 TG |
279 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_BRANCH; |
280 | rinfo->r_pcrel = 1; | |
281 | rinfo->r_length = 2; | |
282 | break; | |
f075eb5e | 283 | case BFD_RELOC_MACH_O_SUBTRACTOR32: |
71141310 TG |
284 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SUBTRACTOR; |
285 | rinfo->r_pcrel = 0; | |
286 | rinfo->r_length = 2; | |
287 | break; | |
f075eb5e | 288 | case BFD_RELOC_MACH_O_SUBTRACTOR64: |
fde33808 TG |
289 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SUBTRACTOR; |
290 | rinfo->r_pcrel = 0; | |
291 | rinfo->r_length = 3; | |
292 | break; | |
71141310 TG |
293 | case BFD_RELOC_MACH_O_X86_64_GOT: |
294 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_GOT; | |
295 | rinfo->r_pcrel = 1; | |
296 | rinfo->r_length = 2; | |
297 | break; | |
fde33808 TG |
298 | case BFD_RELOC_MACH_O_X86_64_GOT_LOAD: |
299 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_GOT_LOAD; | |
300 | rinfo->r_pcrel = 1; | |
301 | rinfo->r_length = 2; | |
618b7301 | 302 | break; |
ed1299fe M |
303 | case BFD_RELOC_MACH_O_X86_64_TLV: |
304 | rinfo->r_type = BFD_MACH_O_X86_64_RELOC_TLV; | |
305 | rinfo->r_pcrel = 1; | |
306 | rinfo->r_length = 2; | |
307 | break; | |
618b7301 TG |
308 | default: |
309 | return FALSE; | |
310 | } | |
fde33808 TG |
311 | if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) |
312 | { | |
313 | rinfo->r_extern = 0; | |
47c50932 TG |
314 | rinfo->r_value = |
315 | (*rel->sym_ptr_ptr)->section->output_section->target_index; | |
fde33808 TG |
316 | } |
317 | else | |
318 | { | |
319 | rinfo->r_extern = 1; | |
320 | rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i; | |
321 | } | |
618b7301 TG |
322 | return TRUE; |
323 | } | |
324 | ||
325 | static reloc_howto_type * | |
326 | bfd_mach_o_x86_64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
07d6d2b8 | 327 | bfd_reloc_code_real_type code) |
618b7301 TG |
328 | { |
329 | unsigned int i; | |
330 | ||
331 | for (i = 0; | |
332 | i < sizeof (x86_64_howto_table) / sizeof (*x86_64_howto_table); | |
333 | i++) | |
334 | if (code == x86_64_howto_table[i].type) | |
335 | return &x86_64_howto_table[i]; | |
336 | return NULL; | |
337 | } | |
338 | ||
339 | static reloc_howto_type * | |
340 | bfd_mach_o_x86_64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
07d6d2b8 | 341 | const char *name ATTRIBUTE_UNUSED) |
618b7301 TG |
342 | { |
343 | return NULL; | |
344 | } | |
345 | ||
ab76eeaf IS |
346 | static bfd_boolean |
347 | bfd_mach_o_section_type_valid_for_x86_64 (unsigned long val) | |
348 | { | |
349 | if (val == BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS | |
350 | || val == BFD_MACH_O_S_LAZY_SYMBOL_POINTERS | |
351 | || val == BFD_MACH_O_S_SYMBOL_STUBS) | |
352 | return FALSE; | |
353 | return TRUE; | |
354 | } | |
355 | ||
632039e0 IS |
356 | /* We want to bump the alignment of some sections. */ |
357 | static const mach_o_section_name_xlat text_section_names_xlat[] = | |
358 | { | |
359 | { ".eh_frame", "__eh_frame", | |
360 | SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_COALESCED, | |
361 | BFD_MACH_O_S_ATTR_LIVE_SUPPORT | |
362 | | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS | |
363 | | BFD_MACH_O_S_ATTR_NO_TOC, 3}, | |
364 | { NULL, NULL, 0, 0, 0, 0} | |
365 | }; | |
366 | ||
367 | const mach_o_segment_name_xlat mach_o_x86_64_segsec_names_xlat[] = | |
368 | { | |
369 | { "__TEXT", text_section_names_xlat }, | |
370 | { NULL, NULL } | |
371 | }; | |
372 | ||
bcb51645 | 373 | #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_x86_64_canonicalize_one_reloc |
618b7301 TG |
374 | #define bfd_mach_o_swap_reloc_out bfd_mach_o_x86_64_swap_reloc_out |
375 | ||
376 | #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_x86_64_bfd_reloc_type_lookup | |
377 | #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_x86_64_bfd_reloc_name_lookup | |
378 | #define bfd_mach_o_print_thread NULL | |
632039e0 | 379 | #define bfd_mach_o_tgt_seg_table mach_o_x86_64_segsec_names_xlat |
ab76eeaf | 380 | #define bfd_mach_o_section_type_valid_for_tgt bfd_mach_o_section_type_valid_for_x86_64 |
618b7301 | 381 | |
07d6d2b8 AM |
382 | #define TARGET_NAME x86_64_mach_o_vec |
383 | #define TARGET_STRING "mach-o-x86-64" | |
618b7301 | 384 | #define TARGET_ARCHITECTURE bfd_arch_i386 |
4384b284 | 385 | #define TARGET_PAGESIZE 4096 |
07d6d2b8 AM |
386 | #define TARGET_BIG_ENDIAN 0 |
387 | #define TARGET_ARCHIVE 0 | |
b93a1992 | 388 | #define TARGET_PRIORITY 0 |
618b7301 | 389 | #include "mach-o-target.c" |