1 /* ARM Mach-O support for BFD.
2 Copyright (C) 2015-2017 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. */
25 #include "libiberty.h"
26 #include "mach-o/arm.h"
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
32 #define bfd_mach_o_canonicalize_one_reloc bfd_mach_o_arm_canonicalize_one_reloc
33 #define bfd_mach_o_swap_reloc_out NULL
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
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
41 static const bfd_target *
42 bfd_mach_o_arm_object_p (bfd *abfd)
44 return bfd_mach_o_header_p (abfd, 0, 0, BFD_MACH_O_CPU_TYPE_ARM);
47 static const bfd_target *
48 bfd_mach_o_arm_core_p (bfd *abfd)
50 return bfd_mach_o_header_p (abfd, 0,
51 BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_ARM);
55 bfd_mach_o_arm_mkobject (bfd *abfd)
57 bfd_mach_o_data_struct *mdata;
59 if (!bfd_mach_o_mkobject_init (abfd))
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;
72 static reloc_howto_type arm_howto_table[]=
75 HOWTO (BFD_RELOC_32, 0, 2, 32, FALSE, 0,
76 complain_overflow_bitfield,
78 FALSE, 0xffffffff, 0xffffffff, FALSE),
79 HOWTO (BFD_RELOC_16, 0, 1, 16, FALSE, 0,
80 complain_overflow_bitfield,
82 FALSE, 0xffff, 0xffff, FALSE),
83 HOWTO (BFD_RELOC_8, 0, 0, 8, FALSE, 0,
84 complain_overflow_bitfield,
86 FALSE, 0xff, 0xff, FALSE),
87 HOWTO (BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0,
88 complain_overflow_bitfield,
90 FALSE, 0xffffffff, 0xffffffff, TRUE),
92 HOWTO (BFD_RELOC_16_PCREL, 0, 1, 16, TRUE, 0,
93 complain_overflow_bitfield,
95 FALSE, 0xffff, 0xffff, TRUE),
96 HOWTO (BFD_RELOC_MACH_O_SECTDIFF, 0, 2, 32, FALSE, 0,
97 complain_overflow_bitfield,
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,
107 FALSE, 0xffffffff, 0xffffffff, FALSE),
109 HOWTO (BFD_RELOC_MACH_O_SECTDIFF, 0, 1, 16, FALSE, 0,
110 complain_overflow_bitfield,
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,
120 FALSE, 0xffff, 0xffff, FALSE),
121 HOWTO (BFD_RELOC_ARM_PCREL_CALL, 2, 2, 24, TRUE, 0,
122 complain_overflow_signed,
124 FALSE, 0x00ffffff, 0x00ffffff, TRUE),
126 HOWTO (BFD_RELOC_ARM_MOVW, 0, 2, 16, FALSE, 0,
127 complain_overflow_dont,
129 FALSE, 0x000f0fff, 0x000f0fff, FALSE),
130 HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 2, 16, FALSE, 0,
131 complain_overflow_bitfield,
133 FALSE, 0x000f0fff, 0x000f0fff, FALSE),
134 HOWTO (BFD_RELOC_ARM_MOVT, 0, 2, 16, FALSE, 0,
135 complain_overflow_bitfield,
137 FALSE, 0x000f0fff, 0x000f0fff, FALSE),
138 HOWTO (BFD_RELOC_MACH_O_PAIR, 0, 2, 16, FALSE, 0,
139 complain_overflow_bitfield,
141 FALSE, 0x000f0fff, 0x000f0fff, FALSE),
143 HOWTO (BFD_RELOC_THUMB_PCREL_BLX, 2, 2, 24, TRUE, 0,
144 complain_overflow_signed,
146 FALSE, 0x07ff2fff, 0x07ff2fff, TRUE)
150 bfd_mach_o_arm_canonicalize_one_reloc (bfd * abfd,
151 struct mach_o_reloc_info_external * raw,
156 bfd_mach_o_reloc_info reloc;
158 if (!bfd_mach_o_pre_canonicalize_one_reloc (abfd, raw, &reloc, res, syms))
161 if (reloc.r_scattered)
163 switch (reloc.r_type)
165 case BFD_MACH_O_ARM_RELOC_PAIR:
166 /* PR 21813: Check for a corrupt PAIR reloc at the start. */
169 if (reloc.r_length == 2)
171 res->howto = &arm_howto_table[7];
172 res->address = res[-1].address;
175 else if (reloc.r_length == 1)
177 res->howto = &arm_howto_table[10];
178 res->address = res[-1].address;
183 case BFD_MACH_O_ARM_RELOC_SECTDIFF:
184 if (reloc.r_length == 2)
186 res->howto = &arm_howto_table[5];
189 else if (reloc.r_length == 1)
191 res->howto = &arm_howto_table[8];
196 case BFD_MACH_O_ARM_RELOC_LOCAL_SECTDIFF:
197 if (reloc.r_length == 2)
199 res->howto = &arm_howto_table[6];
202 else if (reloc.r_length == 1)
204 res->howto = &arm_howto_table[9];
209 case BFD_MACH_O_ARM_RELOC_HALF_SECTDIFF:
210 switch (reloc.r_length)
212 case 2: /* :lower16: for movw arm. */
213 res->howto = &arm_howto_table[12];
215 case 3: /* :upper16: for movt arm. */
216 res->howto = &arm_howto_table[14];
227 switch (reloc.r_type)
229 case BFD_MACH_O_ARM_RELOC_VANILLA:
230 switch ((reloc.r_length << 1) | reloc.r_pcrel)
232 case 0: /* len = 0, pcrel = 0 */
233 res->howto = &arm_howto_table[2];
235 case 2: /* len = 1, pcrel = 0 */
236 res->howto = &arm_howto_table[1];
238 case 3: /* len = 1, pcrel = 1 */
239 res->howto = &arm_howto_table[4];
241 case 4: /* len = 2, pcrel = 0 */
242 res->howto = &arm_howto_table[0];
244 case 5: /* len = 2, pcrel = 1 */
245 res->howto = &arm_howto_table[3];
252 case BFD_MACH_O_ARM_RELOC_BR24:
253 if (reloc.r_length == 2 && reloc.r_pcrel == 1)
255 res->howto = &arm_howto_table[11];
260 case BFD_MACH_O_THUMB_RELOC_BR22:
261 if (reloc.r_length == 2 && reloc.r_pcrel == 1)
263 res->howto = &arm_howto_table[16];
268 case BFD_MACH_O_ARM_RELOC_HALF:
269 if (reloc.r_pcrel == 0)
270 switch (reloc.r_length)
272 case 0: /* :lower16: for movw arm. */
273 res->howto = &arm_howto_table[12];
275 case 1: /* :upper16: for movt arm. */
276 res->howto = &arm_howto_table[14];
281 case BFD_MACH_O_ARM_RELOC_PAIR:
282 if (res[-1].howto == &arm_howto_table[12]
283 && reloc.r_length == 0)
285 /* Pair for :lower16: of movw arm. */
286 res->howto = &arm_howto_table[13];
287 /* This reloc contains the other half in its r_address field. */
288 res[-1].addend += (res->address & 0xffff) << 16;
289 res->address = res[-1].address;
292 else if (res[-1].howto == &arm_howto_table[14]
293 && reloc.r_length == 1)
295 /* Pair for :upper16: of movt arm. */
296 res->howto = &arm_howto_table[15];
297 /* This reloc contains the other half in its r_address field. */
298 res[-1].addend += res->address & 0xffff;
299 res->address = res[-1].address;
312 static reloc_howto_type *
313 bfd_mach_o_arm_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
314 bfd_reloc_code_real_type code)
318 for (i = 0; i < sizeof (arm_howto_table) / sizeof (*arm_howto_table); i++)
319 if (code == arm_howto_table[i].type)
320 return &arm_howto_table[i];
324 static reloc_howto_type *
325 bfd_mach_o_arm_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
326 const char *name ATTRIBUTE_UNUSED)
331 #define TARGET_NAME arm_mach_o_vec
332 #define TARGET_STRING "mach-o-arm"
333 #define TARGET_ARCHITECTURE bfd_arch_arm
334 #define TARGET_PAGESIZE 4096
335 #define TARGET_BIG_ENDIAN 0
336 #define TARGET_ARCHIVE 0
337 #define TARGET_PRIORITY 0
338 #include "mach-o-target.c"