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