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