1 /* Intel x86-64 Mach-O support for BFD.
2 Copyright (C) 2010-2024 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. */
24 #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
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
);
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, 8, 64, false, 0,
71 complain_overflow_bitfield
,
73 false, MINUS_ONE
, MINUS_ONE
, false),
74 HOWTO(BFD_RELOC_32
, 0, 4, 32, false, 0,
75 complain_overflow_bitfield
,
77 false, 0xffffffff, 0xffffffff, false),
78 HOWTO(BFD_RELOC_32_PCREL
, 0, 4, 32, true, 0,
79 complain_overflow_bitfield
,
81 false, 0xffffffff, 0xffffffff, true),
82 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_1
, 0, 4, 32, true, 0,
83 complain_overflow_bitfield
,
85 false, 0xffffffff, 0xffffffff, true),
87 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_2
, 0, 4, 32, true, 0,
88 complain_overflow_bitfield
,
90 false, 0xffffffff, 0xffffffff, true),
91 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_4
, 0, 4, 32, true, 0,
92 complain_overflow_bitfield
,
94 false, 0xffffffff, 0xffffffff, true),
95 HOWTO(BFD_RELOC_MACH_O_X86_64_BRANCH32
, 0, 4, 32, true, 0,
96 complain_overflow_bitfield
,
98 false, 0xffffffff, 0xffffffff, true),
99 HOWTO(BFD_RELOC_MACH_O_X86_64_GOT_LOAD
, 0, 4, 32, true, 0,
100 complain_overflow_bitfield
,
102 false, 0xffffffff, 0xffffffff, true),
104 HOWTO(BFD_RELOC_MACH_O_SUBTRACTOR32
, 0, 4, 32, false, 0,
105 complain_overflow_bitfield
,
106 NULL
, "SUBTRACTOR32",
107 false, 0xffffffff, 0xffffffff, false),
108 HOWTO(BFD_RELOC_MACH_O_SUBTRACTOR64
, 0, 8, 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, 4, 32, true, 0,
113 complain_overflow_bitfield
,
115 false, 0xffffffff, 0xffffffff, true),
116 HOWTO(BFD_RELOC_MACH_O_X86_64_BRANCH8
, 0, 1, 8, true, 0,
117 complain_overflow_bitfield
,
119 false, 0xff, 0xff, true),
121 HOWTO(BFD_RELOC_MACH_O_X86_64_TLV
, 0, 4, 32, true, 0,
122 complain_overflow_bitfield
,
124 false, 0xffffffff, 0xffffffff, true),
128 bfd_mach_o_x86_64_canonicalize_one_reloc (bfd
* abfd
,
129 struct mach_o_reloc_info_external
* raw
,
132 arelent
* res_base ATTRIBUTE_UNUSED
)
134 bfd_mach_o_reloc_info reloc
;
136 if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd
, raw
, &reloc
, res
, syms
))
139 /* On x86-64, scattered relocs are not used. */
140 if (reloc
.r_scattered
)
143 switch (reloc
.r_type
)
145 case BFD_MACH_O_X86_64_RELOC_UNSIGNED
:
148 switch (reloc
.r_length
)
151 res
->howto
= &x86_64_howto_table
[1];
154 res
->howto
= &x86_64_howto_table
[0];
159 case BFD_MACH_O_X86_64_RELOC_SIGNED
:
160 if (reloc
.r_length
== 2 && reloc
.r_pcrel
)
162 res
->howto
= &x86_64_howto_table
[2];
166 case BFD_MACH_O_X86_64_RELOC_BRANCH
:
169 switch (reloc
.r_length
)
172 res
->howto
= &x86_64_howto_table
[6];
178 case BFD_MACH_O_X86_64_RELOC_GOT_LOAD
:
179 if (reloc
.r_length
== 2 && reloc
.r_pcrel
&& reloc
.r_extern
)
181 res
->howto
= &x86_64_howto_table
[7];
185 case BFD_MACH_O_X86_64_RELOC_GOT
:
186 if (reloc
.r_length
== 2 && reloc
.r_pcrel
&& reloc
.r_extern
)
188 res
->howto
= &x86_64_howto_table
[10];
192 case BFD_MACH_O_X86_64_RELOC_SUBTRACTOR
:
195 switch (reloc
.r_length
)
198 res
->howto
= &x86_64_howto_table
[8];
201 res
->howto
= &x86_64_howto_table
[9];
207 case BFD_MACH_O_X86_64_RELOC_SIGNED_1
:
208 if (reloc
.r_length
== 2 && reloc
.r_pcrel
)
210 res
->howto
= &x86_64_howto_table
[3];
214 case BFD_MACH_O_X86_64_RELOC_SIGNED_2
:
215 if (reloc
.r_length
== 2 && reloc
.r_pcrel
)
217 res
->howto
= &x86_64_howto_table
[4];
221 case BFD_MACH_O_X86_64_RELOC_SIGNED_4
:
222 if (reloc
.r_length
== 2 && reloc
.r_pcrel
)
224 res
->howto
= &x86_64_howto_table
[5];
228 case BFD_MACH_O_X86_64_RELOC_TLV
:
229 if (reloc
.r_length
== 2 && reloc
.r_pcrel
&& reloc
.r_extern
)
231 res
->howto
= &x86_64_howto_table
[12];
242 bfd_mach_o_x86_64_swap_reloc_out (arelent
*rel
, bfd_mach_o_reloc_info
*rinfo
)
244 rinfo
->r_address
= rel
->address
;
245 rinfo
->r_scattered
= 0;
246 switch (rel
->howto
->type
)
249 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_UNSIGNED
;
254 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_UNSIGNED
;
258 case BFD_RELOC_32_PCREL
:
259 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_SIGNED
;
263 case BFD_RELOC_MACH_O_X86_64_PCREL32_1
:
264 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_SIGNED_1
;
268 case BFD_RELOC_MACH_O_X86_64_PCREL32_2
:
269 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_SIGNED_2
;
273 case BFD_RELOC_MACH_O_X86_64_PCREL32_4
:
274 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_SIGNED_4
;
278 case BFD_RELOC_MACH_O_X86_64_BRANCH32
:
279 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_BRANCH
;
283 case BFD_RELOC_MACH_O_SUBTRACTOR32
:
284 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_SUBTRACTOR
;
288 case BFD_RELOC_MACH_O_SUBTRACTOR64
:
289 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_SUBTRACTOR
;
293 case BFD_RELOC_MACH_O_X86_64_GOT
:
294 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_GOT
;
298 case BFD_RELOC_MACH_O_X86_64_GOT_LOAD
:
299 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_GOT_LOAD
;
303 case BFD_RELOC_MACH_O_X86_64_TLV
:
304 rinfo
->r_type
= BFD_MACH_O_X86_64_RELOC_TLV
;
311 if ((*rel
->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
)
315 (*rel
->sym_ptr_ptr
)->section
->output_section
->target_index
;
320 rinfo
->r_value
= (*rel
->sym_ptr_ptr
)->udata
.i
;
325 static reloc_howto_type
*
326 bfd_mach_o_x86_64_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
327 bfd_reloc_code_real_type code
)
332 i
< sizeof (x86_64_howto_table
) / sizeof (*x86_64_howto_table
);
334 if (code
== x86_64_howto_table
[i
].type
)
335 return &x86_64_howto_table
[i
];
339 static reloc_howto_type
*
340 bfd_mach_o_x86_64_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
341 const char *name ATTRIBUTE_UNUSED
)
347 bfd_mach_o_section_type_valid_for_x86_64 (unsigned long val
)
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
)
356 /* We want to bump the alignment of some sections. */
357 static const mach_o_section_name_xlat text_section_names_xlat
[] =
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}
367 const mach_o_segment_name_xlat mach_o_x86_64_segsec_names_xlat
[] =
369 { "__TEXT", text_section_names_xlat
},
373 #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_x86_64_canonicalize_one_reloc
374 #define bfd_mach_o_swap_reloc_out bfd_mach_o_x86_64_swap_reloc_out
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
379 #define bfd_mach_o_tgt_seg_table mach_o_x86_64_segsec_names_xlat
380 #define bfd_mach_o_section_type_valid_for_tgt bfd_mach_o_section_type_valid_for_x86_64
382 #define TARGET_NAME x86_64_mach_o_vec
383 #define TARGET_STRING "mach-o-x86-64"
384 #define TARGET_ARCHITECTURE bfd_arch_i386
385 #define TARGET_PAGESIZE 4096
386 #define TARGET_BIG_ENDIAN 0
387 #define TARGET_ARCHIVE 0
388 #define TARGET_PRIORITY 0
389 #include "mach-o-target.c"