bfd/
[external/binutils.git] / bfd / cpu-arm.c
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)
5
6    This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "libiberty.h"
26
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 **));
33
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.  */
37
38 static const bfd_arch_info_type *
39 compatible (a,b)
40      const bfd_arch_info_type * a;
41      const bfd_arch_info_type * b;
42 {
43   /* If a & b are for different architecture we can do nothing.  */
44   if (a->arch != b->arch)
45       return NULL;
46
47   /* If a & b are for the same machine then all is well.  */
48   if (a->mach == b->mach)
49     return a;
50
51   /* Otherwise if either a or b is the 'default' machine
52      then it can be polymorphed into the other.  */
53   if (a->the_default)
54     return b;
55
56   if (b->the_default)
57     return a;
58
59   /* So far all newer ARM architecture cores are
60      supersets of previous cores.  */
61   if (a->mach < b->mach)
62     return b;
63   else if (a->mach > b->mach)
64     return a;
65
66   /* Never reached!  */
67   return NULL;
68 }
69
70 static struct
71 {
72   unsigned int mach;
73   char *       name;
74 }
75 processors[] =
76 {
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" }
105 };
106
107 static bfd_boolean
108 scan (info, string)
109      const struct bfd_arch_info * info;
110      const char * string;
111 {
112   int  i;
113
114   /* First test for an exact match.  */
115   if (strcasecmp (string, info->printable_name) == 0)
116     return TRUE;
117
118   /* Next check for a processor name instead of an Architecture name.  */
119   for (i = sizeof (processors) / sizeof (processors[0]); i--;)
120     {
121       if (strcasecmp (string, processors [i].name) == 0)
122         break;
123     }
124
125   if (i != -1 && info->mach == processors [i].mach)
126     return TRUE;
127
128   /* Finally check for the default architecture.  */
129   if (strcasecmp (string, "arm") == 0)
130     return info->the_default;
131
132   return FALSE;
133 }
134
135 #define N(number, print, default, next)  \
136 {  32, 32, 8, bfd_arch_arm, number, "arm", print, 4, default, compatible, scan, next }
137
138 static const bfd_arch_info_type arch_info_struct[] =
139 {
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)
152 };
153
154 const bfd_arch_info_type bfd_arm_arch =
155   N (0, "arm", TRUE, & arch_info_struct[0]);
156
157 /* Support functions used by both the COFF and ELF versions of the ARM port.  */
158
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.  */
163
164 bfd_boolean
165 bfd_arm_merge_machines (ibfd, obfd)
166      bfd * ibfd;
167      bfd * obfd;
168 {
169   unsigned int in  = bfd_get_mach (ibfd);
170   unsigned int out = bfd_get_mach (obfd);
171
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);
175
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);
182
183   /* If they are the same then nothing needs to be done.  */
184   else if (out == in)
185     ;
186
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.
190
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))
196     {
197       _bfd_error_handler (_("\
198 ERROR: %s is compiled for the EP9312, whereas %s is compiled for XScale"),
199                           bfd_archive_filename (ibfd),
200                           bfd_get_filename (obfd));
201       bfd_set_error (bfd_error_wrong_format);
202       return FALSE;
203     }
204   else if (out == bfd_mach_arm_ep9312
205            && (in == bfd_mach_arm_XScale || in == bfd_mach_arm_iWMMXt))
206     {
207       _bfd_error_handler (_("\
208 ERROR: %s is compiled for the EP9312, whereas %s is compiled for XScale"),
209                           bfd_archive_filename (obfd),
210                           bfd_get_filename (ibfd));
211       bfd_set_error (bfd_error_wrong_format);
212       return FALSE;
213     }
214   else if (in > out)
215     bfd_set_arch_mach (obfd, bfd_arch_arm, in);
216   /* else
217      Nothing to do.  */
218
219   return TRUE;
220 }
221
222 typedef struct
223 {
224   unsigned char namesz[4];      /* Size of entry's owner string.  */
225   unsigned char descsz[4];      /* Size of the note descriptor.  */
226   unsigned char type[4];        /* Interpretation of the descriptor.  */
227   char          name[1];        /* Start of the name+desc data.  */
228 } arm_Note;
229
230 static bfd_boolean
231 arm_check_note (abfd, buffer, buffer_size, expected_name, description_return)
232      bfd *           abfd;
233      char *          buffer;
234      bfd_size_type   buffer_size;
235      const char *    expected_name;
236      char **         description_return;
237 {
238   unsigned long namesz;
239   unsigned long descsz;
240   unsigned long type;
241   char *        descr;
242
243   if (buffer_size < offsetof (arm_Note, name))
244     return FALSE;
245
246   /* We have to extract the values this way to allow for a
247      host whose endian-ness is different from the target.  */
248   namesz = bfd_get_32 (abfd, buffer);
249   descsz = bfd_get_32 (abfd, buffer + offsetof (arm_Note, descsz));
250   type   = bfd_get_32 (abfd, buffer + offsetof (arm_Note, type));
251   descr  = buffer + offsetof (arm_Note, name);
252
253   /* Check for buffer overflow.  */
254   if (namesz + descsz + offsetof (arm_Note, name) > buffer_size)
255     return FALSE;
256
257   if (expected_name == NULL)
258     {
259       if (namesz != 0)
260         return FALSE;
261     }
262   else
263     { 
264       if (namesz != ((strlen (expected_name) + 1 + 3) & ~3))
265         return FALSE;
266       
267       if (strcmp (descr, expected_name) != 0)
268         return FALSE;
269
270       descr += (namesz + 3) & ~3;
271     }
272
273   /* FIXME: We should probably check the type as well.  */
274
275   if (description_return != NULL)
276     * description_return = descr;
277
278   return TRUE;
279 }
280
281 #define NOTE_ARCH_STRING        "arch: "
282
283 bfd_boolean
284 bfd_arm_update_notes (abfd, note_section)
285      bfd * abfd;
286      const char * note_section;
287 {
288   asection *     arm_arch_section;
289   bfd_size_type  buffer_size;
290   bfd_byte *     buffer;
291   char *         arch_string;
292   char *         expected;
293
294   /* Look for a note section.  If one is present check the architecture
295      string encoded in it, and set it to the current architecture if it is
296      different.  */
297   arm_arch_section = bfd_get_section_by_name (abfd, note_section);
298
299   if (arm_arch_section == NULL)
300     return TRUE;
301
302   buffer_size = arm_arch_section->size;
303   if (buffer_size == 0)
304     return FALSE;
305
306   if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer))
307     goto FAIL;
308
309   /* Parse the note.  */
310   if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string))
311     goto FAIL;
312
313   /* Check the architecture in the note against the architecture of the bfd.  */
314   switch (bfd_get_mach (abfd))
315     {
316     default:
317     case bfd_mach_arm_unknown: expected = "unknown"; break;
318     case bfd_mach_arm_2:       expected = "armv2"; break;
319     case bfd_mach_arm_2a:      expected = "armv2a"; break;
320     case bfd_mach_arm_3:       expected = "armv3"; break;
321     case bfd_mach_arm_3M:      expected = "armv3M"; break;
322     case bfd_mach_arm_4:       expected = "armv4"; break;
323     case bfd_mach_arm_4T:      expected = "armv4t"; break;
324     case bfd_mach_arm_5:       expected = "armv5"; break;
325     case bfd_mach_arm_5T:      expected = "armv5t"; break;
326     case bfd_mach_arm_5TE:     expected = "armv5te"; break;
327     case bfd_mach_arm_XScale:  expected = "XScale"; break;
328     case bfd_mach_arm_ep9312:  expected = "ep9312"; break;
329     case bfd_mach_arm_iWMMXt:  expected = "iWMMXt"; break;
330     }
331
332   if (strcmp (arch_string, expected) != 0)
333     {
334       strcpy (buffer + offsetof (arm_Note, name) + ((strlen (NOTE_ARCH_STRING) + 3) & ~3), expected);
335
336       if (! bfd_set_section_contents (abfd, arm_arch_section, buffer,
337                                       (file_ptr) 0, buffer_size))
338         {
339           (*_bfd_error_handler)
340             (_("warning: unable to update contents of %s section in %s"),
341              note_section, bfd_get_filename (abfd));
342           goto FAIL;
343         }
344     }
345
346   free (buffer);
347   return TRUE;
348
349  FAIL:
350   if (buffer != NULL)
351     free (buffer);
352   return FALSE;
353 }
354
355
356 static struct
357 {
358   const char * string;
359   unsigned int mach;
360 }
361 architectures[] =
362 {
363   { "armv2",   bfd_mach_arm_2 },
364   { "armv2a",  bfd_mach_arm_2a },
365   { "armv3",   bfd_mach_arm_3 },
366   { "armv3M",  bfd_mach_arm_3M },
367   { "armv4",   bfd_mach_arm_4 },
368   { "armv4t",  bfd_mach_arm_4T },
369   { "armv5",   bfd_mach_arm_5 },
370   { "armv5t",  bfd_mach_arm_5T },
371   { "armv5te", bfd_mach_arm_5TE },
372   { "XScale",  bfd_mach_arm_XScale },
373   { "ep9312",  bfd_mach_arm_ep9312 },
374   { "iWMMXt",  bfd_mach_arm_iWMMXt }
375 };
376
377 /* Extract the machine number stored in a note section.  */
378 unsigned int
379 bfd_arm_get_mach_from_notes (abfd, note_section)
380      bfd * abfd;
381      const char * note_section;
382 {
383   asection *     arm_arch_section;
384   bfd_size_type  buffer_size;
385   bfd_byte *     buffer;
386   char *         arch_string;
387   int            i;
388
389   /* Look for a note section.  If one is present check the architecture
390      string encoded in it, and set it to the current architecture if it is
391      different.  */
392   arm_arch_section = bfd_get_section_by_name (abfd, note_section);
393
394   if (arm_arch_section == NULL)
395     return bfd_mach_arm_unknown;
396
397   buffer_size = arm_arch_section->size;
398   if (buffer_size == 0)
399     return bfd_mach_arm_unknown;
400
401   if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer))
402     goto FAIL;
403
404   /* Parse the note.  */
405   if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string))
406     goto FAIL;
407
408   /* Interpret the architecture string.  */
409   for (i = ARRAY_SIZE (architectures); i--;)
410     if (strcmp (arch_string, architectures[i].string) == 0)
411       {
412         free (buffer);
413         return architectures[i].mach;
414       }
415
416  FAIL:
417   if (buffer != NULL)
418     free (buffer);
419   return bfd_mach_arm_unknown;
420 }