Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD support for the ARM processor |
6f2750fe | 2 | Copyright (C) 1994-2016 Free Software Foundation, Inc. |
252b5132 RH |
3 | Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org) |
4 | ||
e2fd756b | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
e2fd756b NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
e2fd756b | 10 | (at your option) any later version. |
252b5132 | 11 | |
e2fd756b NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
e2fd756b NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
252b5132 | 21 | |
252b5132 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
252b5132 | 24 | #include "libbfd.h" |
5a6c6817 | 25 | #include "libiberty.h" |
252b5132 | 26 | |
252b5132 RH |
27 | /* This routine is provided two arch_infos and works out which ARM |
28 | machine which would be compatible with both and returns a pointer | |
e2fd756b | 29 | to its info structure. */ |
252b5132 RH |
30 | |
31 | static const bfd_arch_info_type * | |
f075ee0c | 32 | compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b) |
252b5132 | 33 | { |
e2fd756b | 34 | /* If a & b are for different architecture we can do nothing. */ |
252b5132 RH |
35 | if (a->arch != b->arch) |
36 | return NULL; | |
37 | ||
e2fd756b | 38 | /* If a & b are for the same machine then all is well. */ |
252b5132 RH |
39 | if (a->mach == b->mach) |
40 | return a; | |
41 | ||
e2fd756b NC |
42 | /* Otherwise if either a or b is the 'default' machine |
43 | then it can be polymorphed into the other. */ | |
252b5132 RH |
44 | if (a->the_default) |
45 | return b; | |
71f6b586 | 46 | |
252b5132 RH |
47 | if (b->the_default) |
48 | return a; | |
49 | ||
e2fd756b NC |
50 | /* So far all newer ARM architecture cores are |
51 | supersets of previous cores. */ | |
252b5132 RH |
52 | if (a->mach < b->mach) |
53 | return b; | |
54 | else if (a->mach > b->mach) | |
55 | return a; | |
56 | ||
e2fd756b | 57 | /* Never reached! */ |
252b5132 RH |
58 | return NULL; |
59 | } | |
60 | ||
61 | static struct | |
62 | { | |
e2fd756b NC |
63 | unsigned int mach; |
64 | char * name; | |
252b5132 RH |
65 | } |
66 | processors[] = | |
67 | { | |
68 | { bfd_mach_arm_2, "arm2" }, | |
69 | { bfd_mach_arm_2a, "arm250" }, | |
70 | { bfd_mach_arm_2a, "arm3" }, | |
71 | { bfd_mach_arm_3, "arm6" }, | |
72 | { bfd_mach_arm_3, "arm60" }, | |
73 | { bfd_mach_arm_3, "arm600" }, | |
74 | { bfd_mach_arm_3, "arm610" }, | |
75 | { bfd_mach_arm_3, "arm7" }, | |
76 | { bfd_mach_arm_3, "arm710" }, | |
77 | { bfd_mach_arm_3, "arm7500" }, | |
78 | { bfd_mach_arm_3, "arm7d" }, | |
79 | { bfd_mach_arm_3, "arm7di" }, | |
80 | { bfd_mach_arm_3M, "arm7dm" }, | |
81 | { bfd_mach_arm_3M, "arm7dmi" }, | |
478d07d6 | 82 | { bfd_mach_arm_4T, "arm7tdmi" }, |
252b5132 RH |
83 | { bfd_mach_arm_4, "arm8" }, |
84 | { bfd_mach_arm_4, "arm810" }, | |
478d07d6 NC |
85 | { bfd_mach_arm_4, "arm9" }, |
86 | { bfd_mach_arm_4, "arm920" }, | |
87 | { bfd_mach_arm_4T, "arm920t" }, | |
88 | { bfd_mach_arm_4T, "arm9tdmi" }, | |
252b5132 | 89 | { bfd_mach_arm_4, "sa1" }, |
478d07d6 NC |
90 | { bfd_mach_arm_4, "strongarm"}, |
91 | { bfd_mach_arm_4, "strongarm110" }, | |
92 | { bfd_mach_arm_4, "strongarm1100" }, | |
fde78edd | 93 | { bfd_mach_arm_XScale, "xscale" }, |
e16bb312 | 94 | { bfd_mach_arm_ep9312, "ep9312" }, |
2d447fca | 95 | { bfd_mach_arm_iWMMXt, "iwmmxt" }, |
99914dfd NC |
96 | { bfd_mach_arm_iWMMXt2, "iwmmxt2" }, |
97 | { bfd_mach_arm_unknown, "arm_any" } | |
252b5132 RH |
98 | }; |
99 | ||
b34976b6 | 100 | static bfd_boolean |
f075ee0c | 101 | scan (const struct bfd_arch_info *info, const char *string) |
252b5132 RH |
102 | { |
103 | int i; | |
104 | ||
e2fd756b | 105 | /* First test for an exact match. */ |
252b5132 | 106 | if (strcasecmp (string, info->printable_name) == 0) |
b34976b6 | 107 | return TRUE; |
252b5132 | 108 | |
e2fd756b | 109 | /* Next check for a processor name instead of an Architecture name. */ |
252b5132 RH |
110 | for (i = sizeof (processors) / sizeof (processors[0]); i--;) |
111 | { | |
e2fd756b | 112 | if (strcasecmp (string, processors [i].name) == 0) |
252b5132 RH |
113 | break; |
114 | } | |
115 | ||
e2fd756b | 116 | if (i != -1 && info->mach == processors [i].mach) |
b34976b6 | 117 | return TRUE; |
252b5132 | 118 | |
e2fd756b | 119 | /* Finally check for the default architecture. */ |
252b5132 RH |
120 | if (strcasecmp (string, "arm") == 0) |
121 | return info->the_default; | |
71f6b586 | 122 | |
b34976b6 | 123 | return FALSE; |
252b5132 RH |
124 | } |
125 | ||
252b5132 | 126 | #define N(number, print, default, next) \ |
b7761f11 L |
127 | { 32, 32, 8, bfd_arch_arm, number, "arm", print, 4, default, compatible, \ |
128 | scan, bfd_arch_default_fill, next } | |
252b5132 RH |
129 | |
130 | static const bfd_arch_info_type arch_info_struct[] = | |
71f6b586 | 131 | { |
99914dfd NC |
132 | N (bfd_mach_arm_2, "armv2", FALSE, & arch_info_struct[1]), |
133 | N (bfd_mach_arm_2a, "armv2a", FALSE, & arch_info_struct[2]), | |
134 | N (bfd_mach_arm_3, "armv3", FALSE, & arch_info_struct[3]), | |
135 | N (bfd_mach_arm_3M, "armv3m", FALSE, & arch_info_struct[4]), | |
136 | N (bfd_mach_arm_4, "armv4", FALSE, & arch_info_struct[5]), | |
137 | N (bfd_mach_arm_4T, "armv4t", FALSE, & arch_info_struct[6]), | |
138 | N (bfd_mach_arm_5, "armv5", FALSE, & arch_info_struct[7]), | |
139 | N (bfd_mach_arm_5T, "armv5t", FALSE, & arch_info_struct[8]), | |
140 | N (bfd_mach_arm_5TE, "armv5te", FALSE, & arch_info_struct[9]), | |
141 | N (bfd_mach_arm_XScale, "xscale", FALSE, & arch_info_struct[10]), | |
142 | N (bfd_mach_arm_ep9312, "ep9312", FALSE, & arch_info_struct[11]), | |
143 | N (bfd_mach_arm_iWMMXt, "iwmmxt", FALSE, & arch_info_struct[12]), | |
144 | N (bfd_mach_arm_iWMMXt2, "iwmmxt2", FALSE, & arch_info_struct[13]), | |
145 | N (bfd_mach_arm_unknown, "arm_any", FALSE, NULL) | |
252b5132 RH |
146 | }; |
147 | ||
148 | const bfd_arch_info_type bfd_arm_arch = | |
b34976b6 | 149 | N (0, "arm", TRUE, & arch_info_struct[0]); |
5a6c6817 NC |
150 | |
151 | /* Support functions used by both the COFF and ELF versions of the ARM port. */ | |
152 | ||
5c4491d3 | 153 | /* Handle the merging of the 'machine' settings of input file IBFD |
5a6c6817 NC |
154 | and an output file OBFD. These values actually represent the |
155 | different possible ARM architecture variants. | |
156 | Returns TRUE if they were merged successfully or FALSE otherwise. */ | |
157 | ||
158 | bfd_boolean | |
f075ee0c | 159 | bfd_arm_merge_machines (bfd *ibfd, bfd *obfd) |
5a6c6817 NC |
160 | { |
161 | unsigned int in = bfd_get_mach (ibfd); | |
162 | unsigned int out = bfd_get_mach (obfd); | |
163 | ||
164 | /* If the output architecture is unknown, we now have a value to set. */ | |
165 | if (out == bfd_mach_arm_unknown) | |
166 | bfd_set_arch_mach (obfd, bfd_arch_arm, in); | |
167 | ||
5c4491d3 | 168 | /* If the input architecture is unknown, |
5a6c6817 NC |
169 | then so must be the output architecture. */ |
170 | else if (in == bfd_mach_arm_unknown) | |
171 | /* FIXME: We ought to have some way to | |
172 | override this on the command line. */ | |
173 | bfd_set_arch_mach (obfd, bfd_arch_arm, bfd_mach_arm_unknown); | |
174 | ||
175 | /* If they are the same then nothing needs to be done. */ | |
176 | else if (out == in) | |
177 | ; | |
178 | ||
179 | /* Otherwise the general principle that a earlier architecture can be | |
5c4491d3 | 180 | linked with a later architecture to produce a binary that will execute |
5a6c6817 NC |
181 | on the later architecture. |
182 | ||
183 | We fail however if we attempt to link a Cirrus EP9312 binary with an | |
184 | Intel XScale binary, since these architecture have co-processors which | |
185 | will not both be present on the same physical hardware. */ | |
186 | else if (in == bfd_mach_arm_ep9312 | |
2d447fca JM |
187 | && (out == bfd_mach_arm_XScale |
188 | || out == bfd_mach_arm_iWMMXt | |
189 | || out == bfd_mach_arm_iWMMXt2)) | |
5a6c6817 NC |
190 | { |
191 | _bfd_error_handler (_("\ | |
3895f852 | 192 | error: %B is compiled for the EP9312, whereas %B is compiled for XScale"), |
d003868e | 193 | ibfd, obfd); |
5a6c6817 NC |
194 | bfd_set_error (bfd_error_wrong_format); |
195 | return FALSE; | |
196 | } | |
197 | else if (out == bfd_mach_arm_ep9312 | |
2d447fca JM |
198 | && (in == bfd_mach_arm_XScale |
199 | || in == bfd_mach_arm_iWMMXt | |
200 | || in == bfd_mach_arm_iWMMXt2)) | |
5a6c6817 NC |
201 | { |
202 | _bfd_error_handler (_("\ | |
3895f852 | 203 | error: %B is compiled for the EP9312, whereas %B is compiled for XScale"), |
d003868e | 204 | obfd, ibfd); |
5a6c6817 NC |
205 | bfd_set_error (bfd_error_wrong_format); |
206 | return FALSE; | |
207 | } | |
208 | else if (in > out) | |
209 | bfd_set_arch_mach (obfd, bfd_arch_arm, in); | |
210 | /* else | |
211 | Nothing to do. */ | |
212 | ||
213 | return TRUE; | |
214 | } | |
215 | ||
216 | typedef struct | |
217 | { | |
218 | unsigned char namesz[4]; /* Size of entry's owner string. */ | |
219 | unsigned char descsz[4]; /* Size of the note descriptor. */ | |
220 | unsigned char type[4]; /* Interpretation of the descriptor. */ | |
221 | char name[1]; /* Start of the name+desc data. */ | |
222 | } arm_Note; | |
223 | ||
224 | static bfd_boolean | |
f075ee0c AM |
225 | arm_check_note (bfd *abfd, |
226 | bfd_byte *buffer, | |
227 | bfd_size_type buffer_size, | |
228 | const char *expected_name, | |
229 | char **description_return) | |
5a6c6817 NC |
230 | { |
231 | unsigned long namesz; | |
232 | unsigned long descsz; | |
233 | unsigned long type; | |
234 | char * descr; | |
235 | ||
236 | if (buffer_size < offsetof (arm_Note, name)) | |
237 | return FALSE; | |
238 | ||
239 | /* We have to extract the values this way to allow for a | |
240 | host whose endian-ness is different from the target. */ | |
241 | namesz = bfd_get_32 (abfd, buffer); | |
242 | descsz = bfd_get_32 (abfd, buffer + offsetof (arm_Note, descsz)); | |
243 | type = bfd_get_32 (abfd, buffer + offsetof (arm_Note, type)); | |
f075ee0c | 244 | descr = (char *) buffer + offsetof (arm_Note, name); |
5a6c6817 NC |
245 | |
246 | /* Check for buffer overflow. */ | |
247 | if (namesz + descsz + offsetof (arm_Note, name) > buffer_size) | |
248 | return FALSE; | |
249 | ||
250 | if (expected_name == NULL) | |
251 | { | |
252 | if (namesz != 0) | |
253 | return FALSE; | |
254 | } | |
255 | else | |
68ffbac6 | 256 | { |
60d8b524 | 257 | if (namesz != ((strlen (expected_name) + 1 + 3) & ~3)) |
5a6c6817 | 258 | return FALSE; |
68ffbac6 | 259 | |
5a6c6817 NC |
260 | if (strcmp (descr, expected_name) != 0) |
261 | return FALSE; | |
262 | ||
263 | descr += (namesz + 3) & ~3; | |
264 | } | |
265 | ||
266 | /* FIXME: We should probably check the type as well. */ | |
c7e2358a | 267 | (void) type; |
5a6c6817 NC |
268 | |
269 | if (description_return != NULL) | |
270 | * description_return = descr; | |
271 | ||
272 | return TRUE; | |
273 | } | |
274 | ||
275 | #define NOTE_ARCH_STRING "arch: " | |
276 | ||
277 | bfd_boolean | |
f075ee0c | 278 | bfd_arm_update_notes (bfd *abfd, const char *note_section) |
5a6c6817 NC |
279 | { |
280 | asection * arm_arch_section; | |
281 | bfd_size_type buffer_size; | |
eea6121a | 282 | bfd_byte * buffer; |
5a6c6817 NC |
283 | char * arch_string; |
284 | char * expected; | |
285 | ||
286 | /* Look for a note section. If one is present check the architecture | |
287 | string encoded in it, and set it to the current architecture if it is | |
288 | different. */ | |
289 | arm_arch_section = bfd_get_section_by_name (abfd, note_section); | |
290 | ||
291 | if (arm_arch_section == NULL) | |
292 | return TRUE; | |
293 | ||
eea6121a | 294 | buffer_size = arm_arch_section->size; |
5a6c6817 NC |
295 | if (buffer_size == 0) |
296 | return FALSE; | |
297 | ||
eea6121a | 298 | if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer)) |
5a6c6817 NC |
299 | goto FAIL; |
300 | ||
301 | /* Parse the note. */ | |
302 | if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string)) | |
303 | goto FAIL; | |
304 | ||
305 | /* Check the architecture in the note against the architecture of the bfd. */ | |
306 | switch (bfd_get_mach (abfd)) | |
307 | { | |
308 | default: | |
309 | case bfd_mach_arm_unknown: expected = "unknown"; break; | |
310 | case bfd_mach_arm_2: expected = "armv2"; break; | |
311 | case bfd_mach_arm_2a: expected = "armv2a"; break; | |
312 | case bfd_mach_arm_3: expected = "armv3"; break; | |
313 | case bfd_mach_arm_3M: expected = "armv3M"; break; | |
314 | case bfd_mach_arm_4: expected = "armv4"; break; | |
315 | case bfd_mach_arm_4T: expected = "armv4t"; break; | |
316 | case bfd_mach_arm_5: expected = "armv5"; break; | |
317 | case bfd_mach_arm_5T: expected = "armv5t"; break; | |
318 | case bfd_mach_arm_5TE: expected = "armv5te"; break; | |
319 | case bfd_mach_arm_XScale: expected = "XScale"; break; | |
320 | case bfd_mach_arm_ep9312: expected = "ep9312"; break; | |
321 | case bfd_mach_arm_iWMMXt: expected = "iWMMXt"; break; | |
2d447fca | 322 | case bfd_mach_arm_iWMMXt2: expected = "iWMMXt2"; break; |
5a6c6817 NC |
323 | } |
324 | ||
325 | if (strcmp (arch_string, expected) != 0) | |
326 | { | |
f075ee0c AM |
327 | strcpy ((char *) buffer + (offsetof (arm_Note, name) |
328 | + ((strlen (NOTE_ARCH_STRING) + 3) & ~3)), | |
329 | expected); | |
5a6c6817 NC |
330 | |
331 | if (! bfd_set_section_contents (abfd, arm_arch_section, buffer, | |
332 | (file_ptr) 0, buffer_size)) | |
333 | { | |
334 | (*_bfd_error_handler) | |
335 | (_("warning: unable to update contents of %s section in %s"), | |
336 | note_section, bfd_get_filename (abfd)); | |
337 | goto FAIL; | |
338 | } | |
339 | } | |
340 | ||
341 | free (buffer); | |
342 | return TRUE; | |
343 | ||
344 | FAIL: | |
eea6121a AM |
345 | if (buffer != NULL) |
346 | free (buffer); | |
5a6c6817 NC |
347 | return FALSE; |
348 | } | |
349 | ||
350 | ||
351 | static struct | |
352 | { | |
353 | const char * string; | |
354 | unsigned int mach; | |
355 | } | |
356 | architectures[] = | |
357 | { | |
358 | { "armv2", bfd_mach_arm_2 }, | |
359 | { "armv2a", bfd_mach_arm_2a }, | |
360 | { "armv3", bfd_mach_arm_3 }, | |
361 | { "armv3M", bfd_mach_arm_3M }, | |
362 | { "armv4", bfd_mach_arm_4 }, | |
363 | { "armv4t", bfd_mach_arm_4T }, | |
364 | { "armv5", bfd_mach_arm_5 }, | |
365 | { "armv5t", bfd_mach_arm_5T }, | |
366 | { "armv5te", bfd_mach_arm_5TE }, | |
367 | { "XScale", bfd_mach_arm_XScale }, | |
368 | { "ep9312", bfd_mach_arm_ep9312 }, | |
2d447fca | 369 | { "iWMMXt", bfd_mach_arm_iWMMXt }, |
99914dfd NC |
370 | { "iWMMXt2", bfd_mach_arm_iWMMXt2 }, |
371 | { "arm_any", bfd_mach_arm_unknown } | |
5a6c6817 NC |
372 | }; |
373 | ||
374 | /* Extract the machine number stored in a note section. */ | |
375 | unsigned int | |
f075ee0c | 376 | bfd_arm_get_mach_from_notes (bfd *abfd, const char *note_section) |
5a6c6817 NC |
377 | { |
378 | asection * arm_arch_section; | |
379 | bfd_size_type buffer_size; | |
eea6121a | 380 | bfd_byte * buffer; |
5a6c6817 NC |
381 | char * arch_string; |
382 | int i; | |
383 | ||
384 | /* Look for a note section. If one is present check the architecture | |
385 | string encoded in it, and set it to the current architecture if it is | |
386 | different. */ | |
387 | arm_arch_section = bfd_get_section_by_name (abfd, note_section); | |
388 | ||
389 | if (arm_arch_section == NULL) | |
390 | return bfd_mach_arm_unknown; | |
391 | ||
eea6121a | 392 | buffer_size = arm_arch_section->size; |
5a6c6817 NC |
393 | if (buffer_size == 0) |
394 | return bfd_mach_arm_unknown; | |
395 | ||
eea6121a | 396 | if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer)) |
5a6c6817 NC |
397 | goto FAIL; |
398 | ||
399 | /* Parse the note. */ | |
400 | if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string)) | |
401 | goto FAIL; | |
402 | ||
403 | /* Interpret the architecture string. */ | |
404 | for (i = ARRAY_SIZE (architectures); i--;) | |
405 | if (strcmp (arch_string, architectures[i].string) == 0) | |
406 | { | |
407 | free (buffer); | |
408 | return architectures[i].mach; | |
409 | } | |
410 | ||
411 | FAIL: | |
eea6121a AM |
412 | if (buffer != NULL) |
413 | free (buffer); | |
5a6c6817 NC |
414 | return bfd_mach_arm_unknown; |
415 | } | |
9d2da7ca JB |
416 | |
417 | bfd_boolean | |
b0796911 | 418 | bfd_is_arm_special_symbol_name (const char * name, int type) |
9d2da7ca | 419 | { |
38406048 | 420 | /* The ARM compiler outputs several obsolete forms. Recognize them |
efacb0fb DJ |
421 | in addition to the standard $a, $t and $d. We are somewhat loose |
422 | in what we accept here, since the full set is not documented. */ | |
b0796911 PB |
423 | if (!name || name[0] != '$') |
424 | return FALSE; | |
425 | if (name[1] == 'a' || name[1] == 't' || name[1] == 'd') | |
426 | type &= BFD_ARM_SPECIAL_SYM_TYPE_MAP; | |
427 | else if (name[1] == 'm' || name[1] == 'f' || name[1] == 'p') | |
428 | type &= BFD_ARM_SPECIAL_SYM_TYPE_TAG; | |
429 | else if (name[1] >= 'a' && name[1] <= 'z') | |
430 | type &= BFD_ARM_SPECIAL_SYM_TYPE_OTHER; | |
431 | else | |
432 | return FALSE; | |
433 | ||
434 | return (type != 0 && (name[2] == 0 || name[2] == '.')); | |
9d2da7ca JB |
435 | } |
436 |