1e058c834Schristos /* AArch-64 Mach-O support for BFD.
2*c42dbd0eSchristos Copyright (C) 2015-2022 Free Software Foundation, Inc.
3e058c834Schristos
4e058c834Schristos This file is part of BFD, the Binary File Descriptor library.
5e058c834Schristos
6e058c834Schristos This program is free software; you can redistribute it and/or modify
7e058c834Schristos it under the terms of the GNU General Public License as published by
8e058c834Schristos the Free Software Foundation; either version 3 of the License, or
9e058c834Schristos (at your option) any later version.
10e058c834Schristos
11e058c834Schristos This program is distributed in the hope that it will be useful,
12e058c834Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13e058c834Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14e058c834Schristos GNU General Public License for more details.
15e058c834Schristos
16e058c834Schristos You should have received a copy of the GNU General Public License
17e058c834Schristos along with this program; if not, write to the Free Software
18e058c834Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19e058c834Schristos MA 02110-1301, USA. */
20e058c834Schristos
21e058c834Schristos #include "sysdep.h"
22e058c834Schristos #include "bfd.h"
23e058c834Schristos #include "libbfd.h"
24e058c834Schristos #include "libiberty.h"
25867d70fcSchristos #include "mach-o.h"
26e058c834Schristos #include "mach-o/arm64.h"
27e058c834Schristos
28e058c834Schristos #define bfd_mach_o_object_p bfd_mach_o_arm64_object_p
29e058c834Schristos #define bfd_mach_o_core_p bfd_mach_o_arm64_core_p
30e058c834Schristos #define bfd_mach_o_mkobject bfd_mach_o_arm64_mkobject
31e058c834Schristos
32e058c834Schristos #define bfd_mach_o_canonicalize_one_reloc \
33e058c834Schristos bfd_mach_o_arm64_canonicalize_one_reloc
34e058c834Schristos #define bfd_mach_o_swap_reloc_out NULL
35e058c834Schristos
36e058c834Schristos #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_arm64_bfd_reloc_type_lookup
37e058c834Schristos #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_arm64_bfd_reloc_name_lookup
38e058c834Schristos
39e058c834Schristos #define bfd_mach_o_print_thread NULL
40e058c834Schristos #define bfd_mach_o_tgt_seg_table NULL
41e058c834Schristos #define bfd_mach_o_section_type_valid_for_tgt NULL
42e058c834Schristos
43*c42dbd0eSchristos static bfd_cleanup
bfd_mach_o_arm64_object_p(bfd * abfd)44e058c834Schristos bfd_mach_o_arm64_object_p (bfd *abfd)
45e058c834Schristos {
46e058c834Schristos return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_ARM64);
47e058c834Schristos }
48e058c834Schristos
49*c42dbd0eSchristos static bfd_cleanup
bfd_mach_o_arm64_core_p(bfd * abfd)50e058c834Schristos bfd_mach_o_arm64_core_p (bfd *abfd)
51e058c834Schristos {
52e058c834Schristos return bfd_mach_o_header_p (abfd, 0,
53e058c834Schristos BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_ARM64);
54e058c834Schristos }
55e058c834Schristos
56*c42dbd0eSchristos static bool
bfd_mach_o_arm64_mkobject(bfd * abfd)57e058c834Schristos bfd_mach_o_arm64_mkobject (bfd *abfd)
58e058c834Schristos {
59e058c834Schristos bfd_mach_o_data_struct *mdata;
60e058c834Schristos
61e058c834Schristos if (!bfd_mach_o_mkobject_init (abfd))
62*c42dbd0eSchristos return false;
63e058c834Schristos
64e058c834Schristos mdata = bfd_mach_o_get_data (abfd);
65e058c834Schristos mdata->header.magic = BFD_MACH_O_MH_MAGIC;
66e058c834Schristos mdata->header.cputype = BFD_MACH_O_CPU_TYPE_ARM64;
67e058c834Schristos mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL;
68e058c834Schristos mdata->header.byteorder = BFD_ENDIAN_LITTLE;
69e058c834Schristos mdata->header.version = 1;
70e058c834Schristos
71*c42dbd0eSchristos return true;
72e058c834Schristos }
73e058c834Schristos
74e058c834Schristos /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
75e058c834Schristos #define MINUS_ONE (~ (bfd_vma) 0)
76e058c834Schristos
77e058c834Schristos static reloc_howto_type arm64_howto_table[]=
78e058c834Schristos {
79e058c834Schristos /* 0 */
80*c42dbd0eSchristos HOWTO (BFD_RELOC_64, 0, 8, 64, false, 0,
81e058c834Schristos complain_overflow_bitfield,
82e058c834Schristos NULL, "64",
83*c42dbd0eSchristos false, MINUS_ONE, MINUS_ONE, false),
84*c42dbd0eSchristos HOWTO (BFD_RELOC_32, 0, 4, 32, false, 0,
85e058c834Schristos complain_overflow_bitfield,
86e058c834Schristos NULL, "32",
87*c42dbd0eSchristos false, 0xffffffff, 0xffffffff, false),
88*c42dbd0eSchristos HOWTO (BFD_RELOC_16, 0, 2, 16, false, 0,
89e058c834Schristos complain_overflow_bitfield,
90e058c834Schristos NULL, "16",
91*c42dbd0eSchristos false, 0xffff, 0xffff, false),
92*c42dbd0eSchristos HOWTO (BFD_RELOC_8, 0, 1, 8, false, 0,
93e058c834Schristos complain_overflow_bitfield,
94e058c834Schristos NULL, "8",
95*c42dbd0eSchristos false, 0xff, 0xff, false),
96e058c834Schristos /* 4 */
97*c42dbd0eSchristos HOWTO (BFD_RELOC_64_PCREL, 0, 8, 64, true, 0,
98e058c834Schristos complain_overflow_bitfield,
99e058c834Schristos NULL, "DISP64",
100*c42dbd0eSchristos false, MINUS_ONE, MINUS_ONE, true),
101*c42dbd0eSchristos HOWTO (BFD_RELOC_32_PCREL, 0, 4, 32, true, 0,
102e058c834Schristos complain_overflow_bitfield,
103e058c834Schristos NULL, "DISP32",
104*c42dbd0eSchristos false, 0xffffffff, 0xffffffff, true),
105*c42dbd0eSchristos HOWTO (BFD_RELOC_16_PCREL, 0, 2, 16, true, 0,
106e058c834Schristos complain_overflow_bitfield,
107e058c834Schristos NULL, "DISP16",
108*c42dbd0eSchristos false, 0xffff, 0xffff, true),
109*c42dbd0eSchristos HOWTO (BFD_RELOC_AARCH64_CALL26, 0, 4, 26, true, 0,
110e058c834Schristos complain_overflow_bitfield,
111e058c834Schristos NULL, "BRANCH26",
112*c42dbd0eSchristos false, 0x03ffffff, 0x03ffffff, true),
113e058c834Schristos /* 8 */
114*c42dbd0eSchristos HOWTO (BFD_RELOC_AARCH64_ADR_HI21_PCREL, 12, 4, 21, true, 0,
115e058c834Schristos complain_overflow_signed,
116e058c834Schristos NULL, "PAGE21",
117*c42dbd0eSchristos false, 0x1fffff, 0x1fffff, true),
118*c42dbd0eSchristos HOWTO (BFD_RELOC_AARCH64_LDST16_LO12, 1, 4, 12, true, 0,
119e058c834Schristos complain_overflow_signed,
120e058c834Schristos NULL, "PGOFF12",
121*c42dbd0eSchristos false, 0xffe, 0xffe, true),
122*c42dbd0eSchristos HOWTO (BFD_RELOC_MACH_O_ARM64_ADDEND, 0, 4, 32, false, 0,
123e058c834Schristos complain_overflow_signed,
124e058c834Schristos NULL, "ADDEND",
125*c42dbd0eSchristos false, 0xffffffff, 0xffffffff, false),
126*c42dbd0eSchristos HOWTO (BFD_RELOC_MACH_O_SUBTRACTOR32, 0, 4, 32, false, 0,
127e058c834Schristos complain_overflow_bitfield,
128e058c834Schristos NULL, "SUBTRACTOR32",
129*c42dbd0eSchristos false, 0xffffffff, 0xffffffff, false),
130e058c834Schristos /* 12 */
131*c42dbd0eSchristos HOWTO (BFD_RELOC_MACH_O_SUBTRACTOR64, 0, 8, 64, false, 0,
132e058c834Schristos complain_overflow_bitfield,
133e058c834Schristos NULL, "SUBTRACTOR64",
134*c42dbd0eSchristos false, MINUS_ONE, MINUS_ONE, false),
135*c42dbd0eSchristos HOWTO (BFD_RELOC_MACH_O_ARM64_GOT_LOAD_PAGE21, 12, 4, 21, true, 0,
136e058c834Schristos complain_overflow_signed,
137e058c834Schristos NULL, "GOT_LD_PG21",
138*c42dbd0eSchristos false, 0x1fffff, 0x1fffff, true),
139*c42dbd0eSchristos HOWTO (BFD_RELOC_MACH_O_ARM64_GOT_LOAD_PAGEOFF12, 1, 4, 12, true, 0,
140e058c834Schristos complain_overflow_signed,
141e058c834Schristos NULL, "GOT_LD_PGOFF12",
142*c42dbd0eSchristos false, 0xffe, 0xffe, true),
143*c42dbd0eSchristos HOWTO (BFD_RELOC_MACH_O_ARM64_POINTER_TO_GOT, 0, 4, 32, true, 0,
144e058c834Schristos complain_overflow_bitfield,
145e058c834Schristos NULL, "PTR_TO_GOT",
146*c42dbd0eSchristos false, 0xffffffff, 0xffffffff, true),
147e058c834Schristos };
148e058c834Schristos
149*c42dbd0eSchristos static bool
bfd_mach_o_arm64_canonicalize_one_reloc(bfd * abfd,struct mach_o_reloc_info_external * raw,arelent * res,asymbol ** syms,arelent * res_base ATTRIBUTE_UNUSED)150e058c834Schristos bfd_mach_o_arm64_canonicalize_one_reloc (bfd * abfd,
151e058c834Schristos struct mach_o_reloc_info_external * raw,
152e6c7e151Schristos arelent * res,
153e6c7e151Schristos asymbol ** syms,
154e6c7e151Schristos arelent * res_base ATTRIBUTE_UNUSED)
155e058c834Schristos {
156e058c834Schristos bfd_mach_o_reloc_info reloc;
157e058c834Schristos
158e058c834Schristos res->address = bfd_get_32 (abfd, raw->r_address);
159e058c834Schristos if (res->address & BFD_MACH_O_SR_SCATTERED)
160e058c834Schristos {
161e058c834Schristos /* Only non-scattered relocations. */
162*c42dbd0eSchristos return false;
163e058c834Schristos }
164e058c834Schristos
165e058c834Schristos /* The value and info fields have to be extracted dependent on target
166e058c834Schristos endian-ness. */
167e058c834Schristos bfd_mach_o_swap_in_non_scattered_reloc (abfd, &reloc, raw->r_symbolnum);
168e058c834Schristos
169e058c834Schristos if (reloc.r_type == BFD_MACH_O_ARM64_RELOC_ADDEND)
170e058c834Schristos {
171e058c834Schristos if (reloc.r_length == 2 && reloc.r_pcrel == 0)
172e058c834Schristos {
173e058c834Schristos res->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
174e058c834Schristos res->addend = reloc.r_value;
175e058c834Schristos res->howto = &arm64_howto_table[10];
176*c42dbd0eSchristos return true;
177e058c834Schristos }
178*c42dbd0eSchristos return false;
179e058c834Schristos }
180e058c834Schristos
181e058c834Schristos if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, &reloc, res, syms))
182*c42dbd0eSchristos return false;
183e058c834Schristos
184e058c834Schristos switch (reloc.r_type)
185e058c834Schristos {
186e058c834Schristos case BFD_MACH_O_ARM64_RELOC_UNSIGNED:
187e058c834Schristos switch ((reloc.r_length << 1) | reloc.r_pcrel)
188e058c834Schristos {
189e058c834Schristos case 0: /* len = 0, pcrel = 0 */
190e058c834Schristos res->howto = &arm64_howto_table[3];
191*c42dbd0eSchristos return true;
192e058c834Schristos case 2: /* len = 1, pcrel = 0 */
193e058c834Schristos res->howto = &arm64_howto_table[2];
194*c42dbd0eSchristos return true;
195e058c834Schristos case 3: /* len = 1, pcrel = 1 */
196e058c834Schristos res->howto = &arm64_howto_table[6];
197*c42dbd0eSchristos return true;
198e058c834Schristos case 4: /* len = 2, pcrel = 0 */
199e058c834Schristos res->howto = &arm64_howto_table[1];
200*c42dbd0eSchristos return true;
201e058c834Schristos case 5: /* len = 2, pcrel = 1 */
202e058c834Schristos res->howto = &arm64_howto_table[5];
203*c42dbd0eSchristos return true;
204e058c834Schristos case 6: /* len = 3, pcrel = 0 */
205e058c834Schristos res->howto = &arm64_howto_table[0];
206*c42dbd0eSchristos return true;
207e058c834Schristos case 7: /* len = 3, pcrel = 1 */
208e058c834Schristos res->howto = &arm64_howto_table[4];
209*c42dbd0eSchristos return true;
210e058c834Schristos default:
211*c42dbd0eSchristos return false;
212e058c834Schristos }
213e058c834Schristos break;
214e058c834Schristos case BFD_MACH_O_ARM64_RELOC_SUBTRACTOR:
215e058c834Schristos if (reloc.r_pcrel)
216*c42dbd0eSchristos return false;
217e058c834Schristos switch (reloc.r_length)
218e058c834Schristos {
219e058c834Schristos case 2:
220e058c834Schristos res->howto = &arm64_howto_table[11];
221*c42dbd0eSchristos return true;
222e058c834Schristos case 3:
223e058c834Schristos res->howto = &arm64_howto_table[12];
224*c42dbd0eSchristos return true;
225e058c834Schristos default:
226*c42dbd0eSchristos return false;
227e058c834Schristos }
228e058c834Schristos break;
229e058c834Schristos case BFD_MACH_O_ARM64_RELOC_BRANCH26:
230e058c834Schristos if (reloc.r_length == 2 && reloc.r_pcrel == 1)
231e058c834Schristos {
232e058c834Schristos res->howto = &arm64_howto_table[7];
233*c42dbd0eSchristos return true;
234e058c834Schristos }
235e058c834Schristos break;
236e058c834Schristos case BFD_MACH_O_ARM64_RELOC_PAGE21:
237e058c834Schristos if (reloc.r_length == 2 && reloc.r_pcrel == 1)
238e058c834Schristos {
239e058c834Schristos res->howto = &arm64_howto_table[8];
240*c42dbd0eSchristos return true;
241e058c834Schristos }
242e058c834Schristos break;
243e058c834Schristos case BFD_MACH_O_ARM64_RELOC_PAGEOFF12:
244e058c834Schristos if (reloc.r_length == 2 && reloc.r_pcrel == 0)
245e058c834Schristos {
246e058c834Schristos res->howto = &arm64_howto_table[9];
247*c42dbd0eSchristos return true;
248e058c834Schristos }
249e058c834Schristos break;
250e058c834Schristos case BFD_MACH_O_ARM64_RELOC_GOT_LOAD_PAGE21:
251e058c834Schristos if (reloc.r_length == 2 && reloc.r_pcrel == 1)
252e058c834Schristos {
253e058c834Schristos res->howto = &arm64_howto_table[13];
254*c42dbd0eSchristos return true;
255e058c834Schristos }
256e058c834Schristos break;
257e058c834Schristos case BFD_MACH_O_ARM64_RELOC_GOT_LOAD_PAGEOFF12:
258e058c834Schristos if (reloc.r_length == 2 && reloc.r_pcrel == 0)
259e058c834Schristos {
260e058c834Schristos res->howto = &arm64_howto_table[14];
261*c42dbd0eSchristos return true;
262e058c834Schristos }
263e058c834Schristos break;
264e058c834Schristos case BFD_MACH_O_ARM64_RELOC_POINTER_TO_GOT:
265e058c834Schristos if (reloc.r_length == 2 && reloc.r_pcrel == 1)
266e058c834Schristos {
267e058c834Schristos res->howto = &arm64_howto_table[15];
268*c42dbd0eSchristos return true;
269e058c834Schristos }
270e058c834Schristos break;
271e058c834Schristos default:
272e058c834Schristos break;
273e058c834Schristos }
274*c42dbd0eSchristos return false;
275e058c834Schristos }
276e058c834Schristos
277e058c834Schristos static reloc_howto_type *
bfd_mach_o_arm64_bfd_reloc_type_lookup(bfd * abfd ATTRIBUTE_UNUSED,bfd_reloc_code_real_type code)278e058c834Schristos bfd_mach_o_arm64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
279e058c834Schristos bfd_reloc_code_real_type code)
280e058c834Schristos {
281e058c834Schristos unsigned int i;
282e058c834Schristos
283e058c834Schristos for (i = 0;
284e058c834Schristos i < sizeof (arm64_howto_table) / sizeof (*arm64_howto_table);
285e058c834Schristos i++)
286e058c834Schristos if (code == arm64_howto_table[i].type)
287e058c834Schristos return &arm64_howto_table[i];
288e058c834Schristos return NULL;
289e058c834Schristos }
290e058c834Schristos
291e058c834Schristos static reloc_howto_type *
bfd_mach_o_arm64_bfd_reloc_name_lookup(bfd * abfd ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED)292e058c834Schristos bfd_mach_o_arm64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
293e058c834Schristos const char *name ATTRIBUTE_UNUSED)
294e058c834Schristos {
295e058c834Schristos return NULL;
296e058c834Schristos }
297e058c834Schristos
298e058c834Schristos #define TARGET_NAME aarch64_mach_o_vec
299e058c834Schristos #define TARGET_STRING "mach-o-arm64"
300e058c834Schristos #define TARGET_ARCHITECTURE bfd_arch_aarch64
301e058c834Schristos #define TARGET_PAGESIZE 4096
302e058c834Schristos #define TARGET_BIG_ENDIAN 0
303e058c834Schristos #define TARGET_ARCHIVE 0
304e058c834Schristos #define TARGET_PRIORITY 0
305e058c834Schristos #include "mach-o-target.c"
306e058c834Schristos
307e058c834Schristos #undef TARGET_NAME
308e058c834Schristos #undef TARGET_STRING
309e058c834Schristos #undef TARGET_ARCHIVE
310e058c834Schristos #undef TARGET_PRIORITY
311