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