1 /* BFD support for the ARM processor
2 Copyright 1994, 1997, 1999, 2000, 2002, 2003, 2004
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "libiberty.h"
27 static const bfd_arch_info_type
* compatible
28 PARAMS ((const bfd_arch_info_type
*, const bfd_arch_info_type
*));
29 static bfd_boolean scan
30 PARAMS ((const struct bfd_arch_info
*, const char *));
31 static bfd_boolean arm_check_note
32 PARAMS ((bfd
*, char *, bfd_size_type
, const char *, char **));
34 /* This routine is provided two arch_infos and works out which ARM
35 machine which would be compatible with both and returns a pointer
36 to its info structure. */
38 static const bfd_arch_info_type
*
40 const bfd_arch_info_type
* a
;
41 const bfd_arch_info_type
* b
;
43 /* If a & b are for different architecture we can do nothing. */
44 if (a
->arch
!= b
->arch
)
47 /* If a & b are for the same machine then all is well. */
48 if (a
->mach
== b
->mach
)
51 /* Otherwise if either a or b is the 'default' machine
52 then it can be polymorphed into the other. */
59 /* So far all newer ARM architecture cores are
60 supersets of previous cores. */
61 if (a
->mach
< b
->mach
)
63 else if (a
->mach
> b
->mach
)
77 { bfd_mach_arm_2
, "arm2" },
78 { bfd_mach_arm_2a
, "arm250" },
79 { bfd_mach_arm_2a
, "arm3" },
80 { bfd_mach_arm_3
, "arm6" },
81 { bfd_mach_arm_3
, "arm60" },
82 { bfd_mach_arm_3
, "arm600" },
83 { bfd_mach_arm_3
, "arm610" },
84 { bfd_mach_arm_3
, "arm7" },
85 { bfd_mach_arm_3
, "arm710" },
86 { bfd_mach_arm_3
, "arm7500" },
87 { bfd_mach_arm_3
, "arm7d" },
88 { bfd_mach_arm_3
, "arm7di" },
89 { bfd_mach_arm_3M
, "arm7dm" },
90 { bfd_mach_arm_3M
, "arm7dmi" },
91 { bfd_mach_arm_4T
, "arm7tdmi" },
92 { bfd_mach_arm_4
, "arm8" },
93 { bfd_mach_arm_4
, "arm810" },
94 { bfd_mach_arm_4
, "arm9" },
95 { bfd_mach_arm_4
, "arm920" },
96 { bfd_mach_arm_4T
, "arm920t" },
97 { bfd_mach_arm_4T
, "arm9tdmi" },
98 { bfd_mach_arm_4
, "sa1" },
99 { bfd_mach_arm_4
, "strongarm"},
100 { bfd_mach_arm_4
, "strongarm110" },
101 { bfd_mach_arm_4
, "strongarm1100" },
102 { bfd_mach_arm_XScale
, "xscale" },
103 { bfd_mach_arm_ep9312
, "ep9312" },
104 { bfd_mach_arm_iWMMXt
, "iwmmxt" }
109 const struct bfd_arch_info
* info
;
114 /* First test for an exact match. */
115 if (strcasecmp (string
, info
->printable_name
) == 0)
118 /* Next check for a processor name instead of an Architecture name. */
119 for (i
= sizeof (processors
) / sizeof (processors
[0]); i
--;)
121 if (strcasecmp (string
, processors
[i
].name
) == 0)
125 if (i
!= -1 && info
->mach
== processors
[i
].mach
)
128 /* Finally check for the default architecture. */
129 if (strcasecmp (string
, "arm") == 0)
130 return info
->the_default
;
135 #define N(number, print, default, next) \
136 { 32, 32, 8, bfd_arch_arm, number, "arm", print, 4, default, compatible, scan, next }
138 static const bfd_arch_info_type arch_info_struct
[] =
140 N (bfd_mach_arm_2
, "armv2", FALSE
, & arch_info_struct
[1]),
141 N (bfd_mach_arm_2a
, "armv2a", FALSE
, & arch_info_struct
[2]),
142 N (bfd_mach_arm_3
, "armv3", FALSE
, & arch_info_struct
[3]),
143 N (bfd_mach_arm_3M
, "armv3m", FALSE
, & arch_info_struct
[4]),
144 N (bfd_mach_arm_4
, "armv4", FALSE
, & arch_info_struct
[5]),
145 N (bfd_mach_arm_4T
, "armv4t", FALSE
, & arch_info_struct
[6]),
146 N (bfd_mach_arm_5
, "armv5", FALSE
, & arch_info_struct
[7]),
147 N (bfd_mach_arm_5T
, "armv5t", FALSE
, & arch_info_struct
[8]),
148 N (bfd_mach_arm_5TE
, "armv5te", FALSE
, & arch_info_struct
[9]),
149 N (bfd_mach_arm_XScale
, "xscale", FALSE
, & arch_info_struct
[10]),
150 N (bfd_mach_arm_ep9312
, "ep9312", FALSE
, & arch_info_struct
[11]),
151 N (bfd_mach_arm_iWMMXt
,"iwmmxt", FALSE
, NULL
)
154 const bfd_arch_info_type bfd_arm_arch
=
155 N (0, "arm", TRUE
, & arch_info_struct
[0]);
157 /* Support functions used by both the COFF and ELF versions of the ARM port. */
159 /* Handle the merging of the 'machine' settings of input file IBFD
160 and an output file OBFD. These values actually represent the
161 different possible ARM architecture variants.
162 Returns TRUE if they were merged successfully or FALSE otherwise. */
165 bfd_arm_merge_machines (ibfd
, obfd
)
169 unsigned int in
= bfd_get_mach (ibfd
);
170 unsigned int out
= bfd_get_mach (obfd
);
172 /* If the output architecture is unknown, we now have a value to set. */
173 if (out
== bfd_mach_arm_unknown
)
174 bfd_set_arch_mach (obfd
, bfd_arch_arm
, in
);
176 /* If the input architecture is unknown,
177 then so must be the output architecture. */
178 else if (in
== bfd_mach_arm_unknown
)
179 /* FIXME: We ought to have some way to
180 override this on the command line. */
181 bfd_set_arch_mach (obfd
, bfd_arch_arm
, bfd_mach_arm_unknown
);
183 /* If they are the same then nothing needs to be done. */
187 /* Otherwise the general principle that a earlier architecture can be
188 linked with a later architecture to produce a binary that will execute
189 on the later architecture.
191 We fail however if we attempt to link a Cirrus EP9312 binary with an
192 Intel XScale binary, since these architecture have co-processors which
193 will not both be present on the same physical hardware. */
194 else if (in
== bfd_mach_arm_ep9312
195 && (out
== bfd_mach_arm_XScale
|| out
== bfd_mach_arm_iWMMXt
))
197 _bfd_error_handler (_("\
198 ERROR: %B is compiled for the EP9312, whereas %B is compiled for XScale"),
200 bfd_set_error (bfd_error_wrong_format
);
203 else if (out
== bfd_mach_arm_ep9312
204 && (in
== bfd_mach_arm_XScale
|| in
== bfd_mach_arm_iWMMXt
))
206 _bfd_error_handler (_("\
207 ERROR: %B is compiled for the EP9312, whereas %B is compiled for XScale"),
209 bfd_set_error (bfd_error_wrong_format
);
213 bfd_set_arch_mach (obfd
, bfd_arch_arm
, in
);
222 unsigned char namesz
[4]; /* Size of entry's owner string. */
223 unsigned char descsz
[4]; /* Size of the note descriptor. */
224 unsigned char type
[4]; /* Interpretation of the descriptor. */
225 char name
[1]; /* Start of the name+desc data. */
229 arm_check_note (abfd
, buffer
, buffer_size
, expected_name
, description_return
)
232 bfd_size_type buffer_size
;
233 const char * expected_name
;
234 char ** description_return
;
236 unsigned long namesz
;
237 unsigned long descsz
;
241 if (buffer_size
< offsetof (arm_Note
, name
))
244 /* We have to extract the values this way to allow for a
245 host whose endian-ness is different from the target. */
246 namesz
= bfd_get_32 (abfd
, buffer
);
247 descsz
= bfd_get_32 (abfd
, buffer
+ offsetof (arm_Note
, descsz
));
248 type
= bfd_get_32 (abfd
, buffer
+ offsetof (arm_Note
, type
));
249 descr
= buffer
+ offsetof (arm_Note
, name
);
251 /* Check for buffer overflow. */
252 if (namesz
+ descsz
+ offsetof (arm_Note
, name
) > buffer_size
)
255 if (expected_name
== NULL
)
262 if (namesz
!= ((strlen (expected_name
) + 1 + 3) & ~3))
265 if (strcmp (descr
, expected_name
) != 0)
268 descr
+= (namesz
+ 3) & ~3;
271 /* FIXME: We should probably check the type as well. */
273 if (description_return
!= NULL
)
274 * description_return
= descr
;
279 #define NOTE_ARCH_STRING "arch: "
282 bfd_arm_update_notes (abfd
, note_section
)
284 const char * note_section
;
286 asection
* arm_arch_section
;
287 bfd_size_type buffer_size
;
292 /* Look for a note section. If one is present check the architecture
293 string encoded in it, and set it to the current architecture if it is
295 arm_arch_section
= bfd_get_section_by_name (abfd
, note_section
);
297 if (arm_arch_section
== NULL
)
300 buffer_size
= arm_arch_section
->size
;
301 if (buffer_size
== 0)
304 if (!bfd_malloc_and_get_section (abfd
, arm_arch_section
, &buffer
))
307 /* Parse the note. */
308 if (! arm_check_note (abfd
, buffer
, buffer_size
, NOTE_ARCH_STRING
, & arch_string
))
311 /* Check the architecture in the note against the architecture of the bfd. */
312 switch (bfd_get_mach (abfd
))
315 case bfd_mach_arm_unknown
: expected
= "unknown"; break;
316 case bfd_mach_arm_2
: expected
= "armv2"; break;
317 case bfd_mach_arm_2a
: expected
= "armv2a"; break;
318 case bfd_mach_arm_3
: expected
= "armv3"; break;
319 case bfd_mach_arm_3M
: expected
= "armv3M"; break;
320 case bfd_mach_arm_4
: expected
= "armv4"; break;
321 case bfd_mach_arm_4T
: expected
= "armv4t"; break;
322 case bfd_mach_arm_5
: expected
= "armv5"; break;
323 case bfd_mach_arm_5T
: expected
= "armv5t"; break;
324 case bfd_mach_arm_5TE
: expected
= "armv5te"; break;
325 case bfd_mach_arm_XScale
: expected
= "XScale"; break;
326 case bfd_mach_arm_ep9312
: expected
= "ep9312"; break;
327 case bfd_mach_arm_iWMMXt
: expected
= "iWMMXt"; break;
330 if (strcmp (arch_string
, expected
) != 0)
332 strcpy (buffer
+ offsetof (arm_Note
, name
) + ((strlen (NOTE_ARCH_STRING
) + 3) & ~3), expected
);
334 if (! bfd_set_section_contents (abfd
, arm_arch_section
, buffer
,
335 (file_ptr
) 0, buffer_size
))
337 (*_bfd_error_handler
)
338 (_("warning: unable to update contents of %s section in %s"),
339 note_section
, bfd_get_filename (abfd
));
361 { "armv2", bfd_mach_arm_2
},
362 { "armv2a", bfd_mach_arm_2a
},
363 { "armv3", bfd_mach_arm_3
},
364 { "armv3M", bfd_mach_arm_3M
},
365 { "armv4", bfd_mach_arm_4
},
366 { "armv4t", bfd_mach_arm_4T
},
367 { "armv5", bfd_mach_arm_5
},
368 { "armv5t", bfd_mach_arm_5T
},
369 { "armv5te", bfd_mach_arm_5TE
},
370 { "XScale", bfd_mach_arm_XScale
},
371 { "ep9312", bfd_mach_arm_ep9312
},
372 { "iWMMXt", bfd_mach_arm_iWMMXt
}
375 /* Extract the machine number stored in a note section. */
377 bfd_arm_get_mach_from_notes (abfd
, note_section
)
379 const char * note_section
;
381 asection
* arm_arch_section
;
382 bfd_size_type buffer_size
;
387 /* Look for a note section. If one is present check the architecture
388 string encoded in it, and set it to the current architecture if it is
390 arm_arch_section
= bfd_get_section_by_name (abfd
, note_section
);
392 if (arm_arch_section
== NULL
)
393 return bfd_mach_arm_unknown
;
395 buffer_size
= arm_arch_section
->size
;
396 if (buffer_size
== 0)
397 return bfd_mach_arm_unknown
;
399 if (!bfd_malloc_and_get_section (abfd
, arm_arch_section
, &buffer
))
402 /* Parse the note. */
403 if (! arm_check_note (abfd
, buffer
, buffer_size
, NOTE_ARCH_STRING
, & arch_string
))
406 /* Interpret the architecture string. */
407 for (i
= ARRAY_SIZE (architectures
); i
--;)
408 if (strcmp (arch_string
, architectures
[i
].string
) == 0)
411 return architectures
[i
].mach
;
417 return bfd_mach_arm_unknown
;