bfd/
[binutils.git] / bfd / cpu-arm.c
blob52c359b8cc3005110edbb6d7529e348d49b33165
1 /* BFD support for the ARM processor
2 Copyright 1994, 1997, 1999, 2000, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
6 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "libiberty.h"
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
29 to its info structure. */
31 static const bfd_arch_info_type *
32 compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
34 /* If a & b are for different architecture we can do nothing. */
35 if (a->arch != b->arch)
36 return NULL;
38 /* If a & b are for the same machine then all is well. */
39 if (a->mach == b->mach)
40 return a;
42 /* Otherwise if either a or b is the 'default' machine
43 then it can be polymorphed into the other. */
44 if (a->the_default)
45 return b;
47 if (b->the_default)
48 return a;
50 /* So far all newer ARM architecture cores are
51 supersets of previous cores. */
52 if (a->mach < b->mach)
53 return b;
54 else if (a->mach > b->mach)
55 return a;
57 /* Never reached! */
58 return NULL;
61 static struct
63 unsigned int mach;
64 char * name;
66 processors[] =
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" },
82 { bfd_mach_arm_4T, "arm7tdmi" },
83 { bfd_mach_arm_4, "arm8" },
84 { bfd_mach_arm_4, "arm810" },
85 { bfd_mach_arm_4, "arm9" },
86 { bfd_mach_arm_4, "arm920" },
87 { bfd_mach_arm_4T, "arm920t" },
88 { bfd_mach_arm_4T, "arm9tdmi" },
89 { bfd_mach_arm_4, "sa1" },
90 { bfd_mach_arm_4, "strongarm"},
91 { bfd_mach_arm_4, "strongarm110" },
92 { bfd_mach_arm_4, "strongarm1100" },
93 { bfd_mach_arm_XScale, "xscale" },
94 { bfd_mach_arm_ep9312, "ep9312" },
95 { bfd_mach_arm_iWMMXt, "iwmmxt" },
96 { bfd_mach_arm_iWMMXt2, "iwmmxt2" }
99 static bfd_boolean
100 scan (const struct bfd_arch_info *info, const char *string)
102 int i;
104 /* First test for an exact match. */
105 if (strcasecmp (string, info->printable_name) == 0)
106 return TRUE;
108 /* Next check for a processor name instead of an Architecture name. */
109 for (i = sizeof (processors) / sizeof (processors[0]); i--;)
111 if (strcasecmp (string, processors [i].name) == 0)
112 break;
115 if (i != -1 && info->mach == processors [i].mach)
116 return TRUE;
118 /* Finally check for the default architecture. */
119 if (strcasecmp (string, "arm") == 0)
120 return info->the_default;
122 return FALSE;
125 #define N(number, print, default, next) \
126 { 32, 32, 8, bfd_arch_arm, number, "arm", print, 4, default, compatible, scan, next }
128 static const bfd_arch_info_type arch_info_struct[] =
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]),
139 N (bfd_mach_arm_XScale, "xscale", FALSE, & arch_info_struct[10]),
140 N (bfd_mach_arm_ep9312, "ep9312", FALSE, & arch_info_struct[11]),
141 N (bfd_mach_arm_iWMMXt, "iwmmxt", FALSE, & arch_info_struct[12]),
142 N (bfd_mach_arm_iWMMXt2, "iwmmxt2", FALSE, NULL)
145 const bfd_arch_info_type bfd_arm_arch =
146 N (0, "arm", TRUE, & arch_info_struct[0]);
148 /* Support functions used by both the COFF and ELF versions of the ARM port. */
150 /* Handle the merging of the 'machine' settings of input file IBFD
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. */
155 bfd_boolean
156 bfd_arm_merge_machines (bfd *ibfd, bfd *obfd)
158 unsigned int in = bfd_get_mach (ibfd);
159 unsigned int out = bfd_get_mach (obfd);
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);
165 /* If the input architecture is unknown,
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);
172 /* If they are the same then nothing needs to be done. */
173 else if (out == in)
176 /* Otherwise the general principle that a earlier architecture can be
177 linked with a later architecture to produce a binary that will execute
178 on the later architecture.
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
184 && (out == bfd_mach_arm_XScale
185 || out == bfd_mach_arm_iWMMXt
186 || out == bfd_mach_arm_iWMMXt2))
188 _bfd_error_handler (_("\
189 ERROR: %B is compiled for the EP9312, whereas %B is compiled for XScale"),
190 ibfd, obfd);
191 bfd_set_error (bfd_error_wrong_format);
192 return FALSE;
194 else if (out == bfd_mach_arm_ep9312
195 && (in == bfd_mach_arm_XScale
196 || in == bfd_mach_arm_iWMMXt
197 || in == bfd_mach_arm_iWMMXt2))
199 _bfd_error_handler (_("\
200 ERROR: %B is compiled for the EP9312, whereas %B is compiled for XScale"),
201 obfd, ibfd);
202 bfd_set_error (bfd_error_wrong_format);
203 return FALSE;
205 else if (in > out)
206 bfd_set_arch_mach (obfd, bfd_arch_arm, in);
207 /* else
208 Nothing to do. */
210 return TRUE;
213 typedef struct
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;
221 static bfd_boolean
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)
228 unsigned long namesz;
229 unsigned long descsz;
230 unsigned long type;
231 char * descr;
233 if (buffer_size < offsetof (arm_Note, name))
234 return FALSE;
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));
241 descr = (char *) buffer + offsetof (arm_Note, name);
243 /* Check for buffer overflow. */
244 if (namesz + descsz + offsetof (arm_Note, name) > buffer_size)
245 return FALSE;
247 if (expected_name == NULL)
249 if (namesz != 0)
250 return FALSE;
252 else
254 if (namesz != ((strlen (expected_name) + 1 + 3) & ~3))
255 return FALSE;
257 if (strcmp (descr, expected_name) != 0)
258 return FALSE;
260 descr += (namesz + 3) & ~3;
263 /* FIXME: We should probably check the type as well. */
265 if (description_return != NULL)
266 * description_return = descr;
268 return TRUE;
271 #define NOTE_ARCH_STRING "arch: "
273 bfd_boolean
274 bfd_arm_update_notes (bfd *abfd, const char *note_section)
276 asection * arm_arch_section;
277 bfd_size_type buffer_size;
278 bfd_byte * buffer;
279 char * arch_string;
280 char * expected;
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);
287 if (arm_arch_section == NULL)
288 return TRUE;
290 buffer_size = arm_arch_section->size;
291 if (buffer_size == 0)
292 return FALSE;
294 if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer))
295 goto FAIL;
297 /* Parse the note. */
298 if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string))
299 goto FAIL;
301 /* Check the architecture in the note against the architecture of the bfd. */
302 switch (bfd_get_mach (abfd))
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;
318 case bfd_mach_arm_iWMMXt2: expected = "iWMMXt2"; break;
321 if (strcmp (arch_string, expected) != 0)
323 strcpy ((char *) buffer + (offsetof (arm_Note, name)
324 + ((strlen (NOTE_ARCH_STRING) + 3) & ~3)),
325 expected);
327 if (! bfd_set_section_contents (abfd, arm_arch_section, buffer,
328 (file_ptr) 0, buffer_size))
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;
337 free (buffer);
338 return TRUE;
340 FAIL:
341 if (buffer != NULL)
342 free (buffer);
343 return FALSE;
347 static struct
349 const char * string;
350 unsigned int mach;
352 architectures[] =
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 },
365 { "iWMMXt", bfd_mach_arm_iWMMXt },
366 { "iWMMXt2", bfd_mach_arm_iWMMXt2 }
369 /* Extract the machine number stored in a note section. */
370 unsigned int
371 bfd_arm_get_mach_from_notes (bfd *abfd, const char *note_section)
373 asection * arm_arch_section;
374 bfd_size_type buffer_size;
375 bfd_byte * buffer;
376 char * arch_string;
377 int i;
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);
384 if (arm_arch_section == NULL)
385 return bfd_mach_arm_unknown;
387 buffer_size = arm_arch_section->size;
388 if (buffer_size == 0)
389 return bfd_mach_arm_unknown;
391 if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer))
392 goto FAIL;
394 /* Parse the note. */
395 if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string))
396 goto FAIL;
398 /* Interpret the architecture string. */
399 for (i = ARRAY_SIZE (architectures); i--;)
400 if (strcmp (arch_string, architectures[i].string) == 0)
402 free (buffer);
403 return architectures[i].mach;
406 FAIL:
407 if (buffer != NULL)
408 free (buffer);
409 return bfd_mach_arm_unknown;
412 bfd_boolean
413 bfd_is_arm_special_symbol_name (const char * name, int type)
415 /* The ARM compiler outputs several obsolete forms. Recognize them
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. */
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;
429 return (type != 0 && (name[2] == 0 || name[2] == '.'));