Commit | Line | Data |
---|---|---|
f075eb5e | 1 | /* ARM Mach-O support for BFD. |
250d07de | 2 | Copyright (C) 2015-2021 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/arm.h" |
27 | ||
28 | #define bfd_mach_o_object_p bfd_mach_o_arm_object_p | |
29 | #define bfd_mach_o_core_p bfd_mach_o_arm_core_p | |
30 | #define bfd_mach_o_mkobject bfd_mach_o_arm_mkobject | |
31 | ||
32 | #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_arm_canonicalize_one_reloc | |
ca4cf9b9 | 33 | #define bfd_mach_o_swap_reloc_out NULL |
f075eb5e TG |
34 | #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_arm_bfd_reloc_type_lookup |
35 | #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_arm_bfd_reloc_name_lookup | |
36 | ||
37 | #define bfd_mach_o_print_thread NULL | |
38 | #define bfd_mach_o_tgt_seg_table NULL | |
39 | #define bfd_mach_o_section_type_valid_for_tgt NULL | |
40 | ||
cb001c0d | 41 | static bfd_cleanup |
f075eb5e TG |
42 | bfd_mach_o_arm_object_p (bfd *abfd) |
43 | { | |
44 | return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_ARM); | |
45 | } | |
46 | ||
cb001c0d | 47 | static bfd_cleanup |
f075eb5e TG |
48 | bfd_mach_o_arm_core_p (bfd *abfd) |
49 | { | |
50 | return bfd_mach_o_header_p (abfd, 0, | |
07d6d2b8 | 51 | BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_ARM); |
f075eb5e TG |
52 | } |
53 | ||
54 | static bfd_boolean | |
55 | bfd_mach_o_arm_mkobject (bfd *abfd) | |
56 | { | |
57 | bfd_mach_o_data_struct *mdata; | |
58 | ||
59 | if (!bfd_mach_o_mkobject_init (abfd)) | |
60 | return FALSE; | |
61 | ||
62 | mdata = bfd_mach_o_get_data (abfd); | |
63 | mdata->header.magic = BFD_MACH_O_MH_MAGIC; | |
64 | mdata->header.cputype = BFD_MACH_O_CPU_TYPE_ARM; | |
65 | mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_ARM_ALL; | |
66 | mdata->header.byteorder = BFD_ENDIAN_LITTLE; | |
67 | mdata->header.version = 1; | |
68 | ||
69 | return TRUE; | |
70 | } | |
71 | ||
72 | static reloc_howto_type arm_howto_table[]= | |
73 | { | |
74 | /* 0 */ | |
75 | HOWTO (BFD_RELOC_32, 0, 2, 32, FALSE, 0, | |
76 | complain_overflow_bitfield, | |
77 | NULL, "32", | |
78 | FALSE, 0xffffffff, 0xffffffff, FALSE), | |
79 | HOWTO (BFD_RELOC_16, 0, 1, 16, FALSE, 0, | |
80 | complain_overflow_bitfield, | |
81 | NULL, "16", | |
82 | FALSE, 0xffff, 0xffff, FALSE), | |
83 | HOWTO (BFD_RELOC_8, 0, 0, 8, FALSE, 0, | |
84 | complain_overflow_bitfield, | |
85 | NULL, "8", | |
86 | FALSE, 0xff, 0xff, FALSE), | |
87 | HOWTO (BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0, | |
88 | complain_overflow_bitfield, | |
89 | NULL, "DISP32", | |
90 | FALSE, 0xffffffff, 0xffffffff, TRUE), | |
91 | /* 4 */ | |
92 | HOWTO (BFD_RELOC_16_PCREL, 0, 1, 16, TRUE, 0, | |
93 | complain_overflow_bitfield, | |
94 | NULL, "DISP16", | |
95 | FALSE, 0xffff, 0xffff, TRUE), | |
96 | HOWTO (BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 32, FALSE, 0, | |
97 | complain_overflow_bitfield, | |
98 | NULL, "SECTDIFF_32", | |
99 | FALSE, 0xffffffff, 0xffffffff, FALSE), | |
100 | HOWTO (BFD_RELOC_MACH_O_LOCAL_SECTDIFF, 0, 2, 32, FALSE, 0, | |
101 | complain_overflow_bitfield, | |
102 | NULL, "LSECTDIFF_32", | |
103 | FALSE, 0xffffffff, 0xffffffff, FALSE), | |
104 | HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 2, 32, FALSE, 0, | |
105 | complain_overflow_bitfield, | |
106 | NULL, "PAIR_32", | |
107 | FALSE, 0xffffffff, 0xffffffff, FALSE), | |
108 | /* 8 */ | |
109 | HOWTO (BFD_RELOC_MACH_O_SECTDIFF, 0, 1, 16, FALSE, 0, | |
110 | complain_overflow_bitfield, | |
111 | NULL, "SECTDIFF_16", | |
112 | FALSE, 0xffff, 0xffff, FALSE), | |
113 | HOWTO (BFD_RELOC_MACH_O_LOCAL_SECTDIFF, 0, 1, 16, FALSE, 0, | |
114 | complain_overflow_bitfield, | |
115 | NULL, "LSECTDIFF_16", | |
116 | FALSE, 0xffff, 0xffff, FALSE), | |
117 | HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 1, 16, FALSE, 0, | |
118 | complain_overflow_bitfield, | |
119 | NULL, "PAIR_16", | |
120 | FALSE, 0xffff, 0xffff, FALSE), | |
121 | HOWTO (BFD_RELOC_ARM_PCREL_CALL, 2, 2, 24, TRUE, 0, | |
122 | complain_overflow_signed, | |
123 | NULL, "BR24", | |
124 | FALSE, 0x00ffffff, 0x00ffffff, TRUE), | |
125 | /* 12 */ | |
126 | HOWTO (BFD_RELOC_ARM_MOVW, 0, 2, 16, FALSE, 0, | |
127 | complain_overflow_dont, | |
128 | NULL, "MOVW", | |
129 | FALSE, 0x000f0fff, 0x000f0fff, FALSE), | |
130 | HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 2, 16, FALSE, 0, | |
131 | complain_overflow_bitfield, | |
132 | NULL, "PAIR_W", | |
133 | FALSE, 0x000f0fff, 0x000f0fff, FALSE), | |
134 | HOWTO (BFD_RELOC_ARM_MOVT, 0, 2, 16, FALSE, 0, | |
135 | complain_overflow_bitfield, | |
136 | NULL, "MOVT", | |
137 | FALSE, 0x000f0fff, 0x000f0fff, FALSE), | |
138 | HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 2, 16, FALSE, 0, | |
139 | complain_overflow_bitfield, | |
140 | NULL, "PAIR_T", | |
141 | FALSE, 0x000f0fff, 0x000f0fff, FALSE), | |
142 | /* 16 */ | |
143 | HOWTO (BFD_RELOC_THUMB_PCREL_BLX, 2, 2, 24, TRUE, 0, | |
144 | complain_overflow_signed, | |
145 | NULL, "TBR22", | |
146 | FALSE, 0x07ff2fff, 0x07ff2fff, TRUE) | |
147 | }; | |
148 | ||
149 | static bfd_boolean | |
ca4cf9b9 NC |
150 | bfd_mach_o_arm_canonicalize_one_reloc (bfd * abfd, |
151 | struct mach_o_reloc_info_external * raw, | |
152 | arelent * res, | |
153 | asymbol ** syms, | |
154 | arelent * res_base) | |
0bae9e9e | 155 | { |
f075eb5e TG |
156 | bfd_mach_o_reloc_info reloc; |
157 | ||
158 | if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd, raw, &reloc, res, syms)) | |
a68aa5d3 NC |
159 | { |
160 | fprintf (stderr, "ARm 1\n"); | |
f075eb5e | 161 | return FALSE; |
a68aa5d3 | 162 | } |
f075eb5e TG |
163 | |
164 | if (reloc.r_scattered) | |
165 | { | |
166 | switch (reloc.r_type) | |
07d6d2b8 AM |
167 | { |
168 | case BFD_MACH_O_ARM_RELOC_PAIR: | |
ca4cf9b9 NC |
169 | /* PR 21813: Check for a corrupt PAIR reloc at the start. */ |
170 | if (res == res_base) | |
a68aa5d3 NC |
171 | { |
172 | _bfd_error_handler (_("\ | |
173 | malformed mach-o ARM reloc pair: reloc is first reloc")); | |
ca4cf9b9 | 174 | return FALSE; |
a68aa5d3 | 175 | } |
07d6d2b8 AM |
176 | if (reloc.r_length == 2) |
177 | { | |
f075eb5e TG |
178 | res->howto = &arm_howto_table[7]; |
179 | res->address = res[-1].address; | |
180 | return TRUE; | |
07d6d2b8 AM |
181 | } |
182 | else if (reloc.r_length == 1) | |
f075eb5e TG |
183 | { |
184 | res->howto = &arm_howto_table[10]; | |
185 | res->address = res[-1].address; | |
186 | return TRUE; | |
187 | } | |
a68aa5d3 NC |
188 | _bfd_error_handler (_("\ |
189 | malformed mach-o ARM reloc pair: invalid length: %d"), reloc.r_length); | |
07d6d2b8 | 190 | return FALSE; |
0bae9e9e | 191 | |
07d6d2b8 AM |
192 | case BFD_MACH_O_ARM_RELOC_SECTDIFF: |
193 | if (reloc.r_length == 2) | |
194 | { | |
f075eb5e TG |
195 | res->howto = &arm_howto_table[5]; |
196 | return TRUE; | |
07d6d2b8 AM |
197 | } |
198 | else if (reloc.r_length == 1) | |
199 | { | |
f075eb5e TG |
200 | res->howto = &arm_howto_table[8]; |
201 | return TRUE; | |
07d6d2b8 | 202 | } |
a68aa5d3 NC |
203 | _bfd_error_handler (_("\ |
204 | malformed mach-o ARM sectdiff reloc: invalid length: %d"), reloc.r_length); | |
07d6d2b8 | 205 | return FALSE; |
0bae9e9e | 206 | |
07d6d2b8 AM |
207 | case BFD_MACH_O_ARM_RELOC_LOCAL_SECTDIFF: |
208 | if (reloc.r_length == 2) | |
209 | { | |
f075eb5e TG |
210 | res->howto = &arm_howto_table[6]; |
211 | return TRUE; | |
07d6d2b8 AM |
212 | } |
213 | else if (reloc.r_length == 1) | |
214 | { | |
f075eb5e TG |
215 | res->howto = &arm_howto_table[9]; |
216 | return TRUE; | |
07d6d2b8 | 217 | } |
a68aa5d3 NC |
218 | _bfd_error_handler (_("\ |
219 | malformed mach-o ARM local sectdiff reloc: invalid length: %d"), | |
220 | reloc.r_length); | |
07d6d2b8 | 221 | return FALSE; |
0bae9e9e | 222 | |
f075eb5e TG |
223 | case BFD_MACH_O_ARM_RELOC_HALF_SECTDIFF: |
224 | switch (reloc.r_length) | |
225 | { | |
226 | case 2: /* :lower16: for movw arm. */ | |
227 | res->howto = &arm_howto_table[12]; | |
228 | return TRUE; | |
229 | case 3: /* :upper16: for movt arm. */ | |
230 | res->howto = &arm_howto_table[14]; | |
231 | return TRUE; | |
232 | } | |
a68aa5d3 NC |
233 | _bfd_error_handler (_("\ |
234 | malformed mach-o ARM half sectdiff reloc: invalid length: %d"), | |
235 | reloc.r_length); | |
f075eb5e | 236 | return FALSE; |
0bae9e9e | 237 | |
07d6d2b8 AM |
238 | default: |
239 | break; | |
240 | } | |
f075eb5e TG |
241 | } |
242 | else | |
243 | { | |
244 | switch (reloc.r_type) | |
07d6d2b8 AM |
245 | { |
246 | case BFD_MACH_O_ARM_RELOC_VANILLA: | |
247 | switch ((reloc.r_length << 1) | reloc.r_pcrel) | |
248 | { | |
249 | case 0: /* len = 0, pcrel = 0 */ | |
250 | res->howto = &arm_howto_table[2]; | |
251 | return TRUE; | |
252 | case 2: /* len = 1, pcrel = 0 */ | |
253 | res->howto = &arm_howto_table[1]; | |
254 | return TRUE; | |
255 | case 3: /* len = 1, pcrel = 1 */ | |
256 | res->howto = &arm_howto_table[4]; | |
257 | return TRUE; | |
258 | case 4: /* len = 2, pcrel = 0 */ | |
259 | res->howto = &arm_howto_table[0]; | |
260 | return TRUE; | |
261 | case 5: /* len = 2, pcrel = 1 */ | |
262 | res->howto = &arm_howto_table[3]; | |
263 | return TRUE; | |
264 | default: | |
a68aa5d3 NC |
265 | _bfd_error_handler (_("\ |
266 | malformed mach-o ARM vanilla reloc: invalid length: %d (pcrel: %d)"), | |
267 | reloc.r_length, reloc.r_pcrel); | |
07d6d2b8 AM |
268 | return FALSE; |
269 | } | |
270 | break; | |
271 | ||
272 | case BFD_MACH_O_ARM_RELOC_BR24: | |
f075eb5e TG |
273 | if (reloc.r_length == 2 && reloc.r_pcrel == 1) |
274 | { | |
07d6d2b8 AM |
275 | res->howto = &arm_howto_table[11]; |
276 | return TRUE; | |
f075eb5e | 277 | } |
f075eb5e | 278 | break; |
0bae9e9e | 279 | |
07d6d2b8 | 280 | case BFD_MACH_O_THUMB_RELOC_BR22: |
f075eb5e TG |
281 | if (reloc.r_length == 2 && reloc.r_pcrel == 1) |
282 | { | |
07d6d2b8 AM |
283 | res->howto = &arm_howto_table[16]; |
284 | return TRUE; | |
f075eb5e | 285 | } |
f075eb5e | 286 | break; |
0bae9e9e | 287 | |
07d6d2b8 | 288 | case BFD_MACH_O_ARM_RELOC_HALF: |
f075eb5e TG |
289 | if (reloc.r_pcrel == 0) |
290 | switch (reloc.r_length) | |
291 | { | |
292 | case 0: /* :lower16: for movw arm. */ | |
293 | res->howto = &arm_howto_table[12]; | |
294 | return TRUE; | |
295 | case 1: /* :upper16: for movt arm. */ | |
296 | res->howto = &arm_howto_table[14]; | |
297 | return TRUE; | |
298 | } | |
0bae9e9e NC |
299 | break; |
300 | ||
07d6d2b8 | 301 | case BFD_MACH_O_ARM_RELOC_PAIR: |
f075eb5e TG |
302 | if (res[-1].howto == &arm_howto_table[12] |
303 | && reloc.r_length == 0) | |
304 | { | |
305 | /* Pair for :lower16: of movw arm. */ | |
306 | res->howto = &arm_howto_table[13]; | |
307 | /* This reloc contains the other half in its r_address field. */ | |
308 | res[-1].addend += (res->address & 0xffff) << 16; | |
309 | res->address = res[-1].address; | |
310 | return TRUE; | |
07d6d2b8 | 311 | } |
f075eb5e TG |
312 | else if (res[-1].howto == &arm_howto_table[14] |
313 | && reloc.r_length == 1) | |
314 | { | |
315 | /* Pair for :upper16: of movt arm. */ | |
316 | res->howto = &arm_howto_table[15]; | |
317 | /* This reloc contains the other half in its r_address field. */ | |
318 | res[-1].addend += res->address & 0xffff; | |
319 | res->address = res[-1].address; | |
320 | return TRUE; | |
07d6d2b8 | 321 | } |
0bae9e9e NC |
322 | break; |
323 | ||
07d6d2b8 | 324 | default: |
0bae9e9e | 325 | break; |
07d6d2b8 | 326 | } |
f075eb5e | 327 | } |
0bae9e9e | 328 | |
a68aa5d3 NC |
329 | _bfd_error_handler (_("\ |
330 | malformed mach-o ARM reloc: unknown reloc type: %d"), reloc.r_length); | |
0bae9e9e | 331 | return FALSE; |
f075eb5e TG |
332 | } |
333 | ||
334 | static reloc_howto_type * | |
335 | bfd_mach_o_arm_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
336 | bfd_reloc_code_real_type code) | |
337 | { | |
338 | unsigned int i; | |
339 | ||
340 | for (i = 0; i < sizeof (arm_howto_table) / sizeof (*arm_howto_table); i++) | |
341 | if (code == arm_howto_table[i].type) | |
342 | return &arm_howto_table[i]; | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | static reloc_howto_type * | |
347 | bfd_mach_o_arm_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
348 | const char *name ATTRIBUTE_UNUSED) | |
349 | { | |
350 | return NULL; | |
351 | } | |
352 | ||
07d6d2b8 AM |
353 | #define TARGET_NAME arm_mach_o_vec |
354 | #define TARGET_STRING "mach-o-arm" | |
f075eb5e TG |
355 | #define TARGET_ARCHITECTURE bfd_arch_arm |
356 | #define TARGET_PAGESIZE 4096 | |
07d6d2b8 AM |
357 | #define TARGET_BIG_ENDIAN 0 |
358 | #define TARGET_ARCHIVE 0 | |
f075eb5e TG |
359 | #define TARGET_PRIORITY 0 |
360 | #include "mach-o-target.c" |