MIPS/LD: Accept high-part relocations in PIC code with absolute symbols
[external/binutils.git] / bfd / cpu-arm.c
1 /* BFD support for the ARM processor
2    Copyright (C) 1994-2018 Free Software Foundation, Inc.
3    Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
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.
16
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
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "libiberty.h"
26
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.  */
30
31 static const bfd_arch_info_type *
32 compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
33 {
34   /* If a & b are for different architecture we can do nothing.  */
35   if (a->arch != b->arch)
36       return NULL;
37
38   /* If a & b are for the same machine then all is well.  */
39   if (a->mach == b->mach)
40     return a;
41
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;
46
47   if (b->the_default)
48     return a;
49
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;
56
57   /* Never reached!  */
58   return NULL;
59 }
60
61 static struct
62 {
63   unsigned int mach;
64   char *       name;
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"  },
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" },
97   { bfd_mach_arm_unknown, "arm_any" }
98 };
99
100 static bfd_boolean
101 scan (const struct bfd_arch_info *info, const char *string)
102 {
103   int  i;
104
105   /* First test for an exact match.  */
106   if (strcasecmp (string, info->printable_name) == 0)
107     return TRUE;
108
109   /* Next check for a processor name instead of an Architecture name.  */
110   for (i = sizeof (processors) / sizeof (processors[0]); i--;)
111     {
112       if (strcasecmp (string, processors [i].name) == 0)
113         break;
114     }
115
116   if (i != -1 && info->mach == processors [i].mach)
117     return TRUE;
118
119   /* Finally check for the default architecture.  */
120   if (strcasecmp (string, "arm") == 0)
121     return info->the_default;
122
123   return FALSE;
124 }
125
126 #define N(number, print, default, next)  \
127 {  32, 32, 8, bfd_arch_arm, number, "arm", print, 4, default, compatible, \
128    scan, bfd_arch_default_fill, next }
129
130 static const bfd_arch_info_type arch_info_struct[] =
131 {
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_5TEJ,      "armv5tej",       FALSE, & arch_info_struct[14]),
146   N (bfd_mach_arm_6,         "armv6",          FALSE, & arch_info_struct[15]),
147   N (bfd_mach_arm_6KZ,       "armv6kz",        FALSE, & arch_info_struct[16]),
148   N (bfd_mach_arm_6T2,       "armv6t2",        FALSE, & arch_info_struct[17]),
149   N (bfd_mach_arm_6K,        "armv6k",         FALSE, & arch_info_struct[18]),
150   N (bfd_mach_arm_7,         "armv7",          FALSE, & arch_info_struct[19]),
151   N (bfd_mach_arm_6M,        "armv6-m",        FALSE, & arch_info_struct[20]),
152   N (bfd_mach_arm_6SM,       "armv6s-m",       FALSE, & arch_info_struct[21]),
153   N (bfd_mach_arm_7EM,       "armv7e-m",       FALSE, & arch_info_struct[22]),
154   N (bfd_mach_arm_8,         "armv8-a",        FALSE, & arch_info_struct[23]),
155   N (bfd_mach_arm_8R,        "armv8-r",        FALSE, & arch_info_struct[24]),
156   N (bfd_mach_arm_8M_BASE,   "armv8-m.base",   FALSE, & arch_info_struct[25]),
157   N (bfd_mach_arm_8M_MAIN,   "armv8-m.main",   FALSE, & arch_info_struct[26]),
158   N (bfd_mach_arm_unknown,   "arm_any",        FALSE, NULL)
159 };
160
161 const bfd_arch_info_type bfd_arm_arch =
162   N (0, "arm", TRUE, & arch_info_struct[0]);
163
164 /* Support functions used by both the COFF and ELF versions of the ARM port.  */
165
166 /* Handle the merging of the 'machine' settings of input file IBFD
167    and an output file OBFD.  These values actually represent the
168    different possible ARM architecture variants.
169    Returns TRUE if they were merged successfully or FALSE otherwise.  */
170
171 bfd_boolean
172 bfd_arm_merge_machines (bfd *ibfd, bfd *obfd)
173 {
174   unsigned int in  = bfd_get_mach (ibfd);
175   unsigned int out = bfd_get_mach (obfd);
176
177   /* If the output architecture is unknown, we now have a value to set.  */
178   if (out == bfd_mach_arm_unknown)
179     bfd_set_arch_mach (obfd, bfd_arch_arm, in);
180
181   /* If the input architecture is unknown,
182      then so must be the output architecture.  */
183   else if (in == bfd_mach_arm_unknown)
184     /* FIXME: We ought to have some way to
185        override this on the command line.  */
186     bfd_set_arch_mach (obfd, bfd_arch_arm, bfd_mach_arm_unknown);
187
188   /* If they are the same then nothing needs to be done.  */
189   else if (out == in)
190     ;
191
192   /* Otherwise the general principle that a earlier architecture can be
193      linked with a later architecture to produce a binary that will execute
194      on the later architecture.
195
196      We fail however if we attempt to link a Cirrus EP9312 binary with an
197      Intel XScale binary, since these architecture have co-processors which
198      will not both be present on the same physical hardware.  */
199   else if (in == bfd_mach_arm_ep9312
200            && (out == bfd_mach_arm_XScale
201                || out == bfd_mach_arm_iWMMXt
202                || out == bfd_mach_arm_iWMMXt2))
203     {
204       /* xgettext: c-format */
205       _bfd_error_handler (_("\
206 error: %pB is compiled for the EP9312, whereas %pB is compiled for XScale"),
207                           ibfd, obfd);
208       bfd_set_error (bfd_error_wrong_format);
209       return FALSE;
210     }
211   else if (out == bfd_mach_arm_ep9312
212            && (in == bfd_mach_arm_XScale
213                || in == bfd_mach_arm_iWMMXt
214                || in == bfd_mach_arm_iWMMXt2))
215     {
216       /* xgettext: c-format */
217       _bfd_error_handler (_("\
218 error: %pB is compiled for the EP9312, whereas %pB is compiled for XScale"),
219                           obfd, ibfd);
220       bfd_set_error (bfd_error_wrong_format);
221       return FALSE;
222     }
223   else if (in > out)
224     bfd_set_arch_mach (obfd, bfd_arch_arm, in);
225   /* else
226      Nothing to do.  */
227
228   return TRUE;
229 }
230
231 typedef struct
232 {
233   unsigned char namesz[4];      /* Size of entry's owner string.  */
234   unsigned char descsz[4];      /* Size of the note descriptor.  */
235   unsigned char type[4];        /* Interpretation of the descriptor.  */
236   char          name[1];        /* Start of the name+desc data.  */
237 } arm_Note;
238
239 static bfd_boolean
240 arm_check_note (bfd *abfd,
241                 bfd_byte *buffer,
242                 bfd_size_type buffer_size,
243                 const char *expected_name,
244                 char **description_return)
245 {
246   unsigned long namesz;
247   unsigned long descsz;
248   unsigned long type;
249   char *        descr;
250
251   if (buffer_size < offsetof (arm_Note, name))
252     return FALSE;
253
254   /* We have to extract the values this way to allow for a
255      host whose endian-ness is different from the target.  */
256   namesz = bfd_get_32 (abfd, buffer);
257   descsz = bfd_get_32 (abfd, buffer + offsetof (arm_Note, descsz));
258   type   = bfd_get_32 (abfd, buffer + offsetof (arm_Note, type));
259   descr  = (char *) buffer + offsetof (arm_Note, name);
260
261   /* Check for buffer overflow.  */
262   if (namesz + descsz + offsetof (arm_Note, name) > buffer_size)
263     return FALSE;
264
265   if (expected_name == NULL)
266     {
267       if (namesz != 0)
268         return FALSE;
269     }
270   else
271     {
272       if (namesz != ((strlen (expected_name) + 1 + 3) & ~3))
273         return FALSE;
274
275       if (strcmp (descr, expected_name) != 0)
276         return FALSE;
277
278       descr += (namesz + 3) & ~3;
279     }
280
281   /* FIXME: We should probably check the type as well.  */
282   (void) type;
283
284   if (description_return != NULL)
285     * description_return = descr;
286
287   return TRUE;
288 }
289
290 #define NOTE_ARCH_STRING        "arch: "
291
292 bfd_boolean
293 bfd_arm_update_notes (bfd *abfd, const char *note_section)
294 {
295   asection *     arm_arch_section;
296   bfd_size_type  buffer_size;
297   bfd_byte *     buffer;
298   char *         arch_string;
299   char *         expected;
300
301   /* Look for a note section.  If one is present check the architecture
302      string encoded in it, and set it to the current architecture if it is
303      different.  */
304   arm_arch_section = bfd_get_section_by_name (abfd, note_section);
305
306   if (arm_arch_section == NULL)
307     return TRUE;
308
309   buffer_size = arm_arch_section->size;
310   if (buffer_size == 0)
311     return FALSE;
312
313   if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer))
314     goto FAIL;
315
316   /* Parse the note.  */
317   if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string))
318     goto FAIL;
319
320   /* Check the architecture in the note against the architecture of the bfd.  */
321   switch (bfd_get_mach (abfd))
322     {
323     default:
324     case bfd_mach_arm_unknown: expected = "unknown"; break;
325     case bfd_mach_arm_2:       expected = "armv2"; break;
326     case bfd_mach_arm_2a:      expected = "armv2a"; break;
327     case bfd_mach_arm_3:       expected = "armv3"; break;
328     case bfd_mach_arm_3M:      expected = "armv3M"; break;
329     case bfd_mach_arm_4:       expected = "armv4"; break;
330     case bfd_mach_arm_4T:      expected = "armv4t"; break;
331     case bfd_mach_arm_5:       expected = "armv5"; break;
332     case bfd_mach_arm_5T:      expected = "armv5t"; break;
333     case bfd_mach_arm_5TE:     expected = "armv5te"; break;
334     case bfd_mach_arm_XScale:  expected = "XScale"; break;
335     case bfd_mach_arm_ep9312:  expected = "ep9312"; break;
336     case bfd_mach_arm_iWMMXt:  expected = "iWMMXt"; break;
337     case bfd_mach_arm_iWMMXt2: expected = "iWMMXt2"; break;
338     }
339
340   if (strcmp (arch_string, expected) != 0)
341     {
342       strcpy ((char *) buffer + (offsetof (arm_Note, name)
343                                  + ((strlen (NOTE_ARCH_STRING) + 3) & ~3)),
344               expected);
345
346       if (! bfd_set_section_contents (abfd, arm_arch_section, buffer,
347                                       (file_ptr) 0, buffer_size))
348         {
349           _bfd_error_handler
350             /* xgettext: c-format */
351             (_("warning: unable to update contents of %s section in %pB"),
352              note_section, abfd);
353           goto FAIL;
354         }
355     }
356
357   free (buffer);
358   return TRUE;
359
360  FAIL:
361   if (buffer != NULL)
362     free (buffer);
363   return FALSE;
364 }
365
366
367 static struct
368 {
369   const char * string;
370   unsigned int mach;
371 }
372 architectures[] =
373 {
374   { "armv2",   bfd_mach_arm_2 },
375   { "armv2a",  bfd_mach_arm_2a },
376   { "armv3",   bfd_mach_arm_3 },
377   { "armv3M",  bfd_mach_arm_3M },
378   { "armv4",   bfd_mach_arm_4 },
379   { "armv4t",  bfd_mach_arm_4T },
380   { "armv5",   bfd_mach_arm_5 },
381   { "armv5t",  bfd_mach_arm_5T },
382   { "armv5te", bfd_mach_arm_5TE },
383   { "XScale",  bfd_mach_arm_XScale },
384   { "ep9312",  bfd_mach_arm_ep9312 },
385   { "iWMMXt",  bfd_mach_arm_iWMMXt },
386   { "iWMMXt2", bfd_mach_arm_iWMMXt2 },
387   { "arm_any", bfd_mach_arm_unknown }
388 };
389
390 /* Extract the machine number stored in a note section.  */
391 unsigned int
392 bfd_arm_get_mach_from_notes (bfd *abfd, const char *note_section)
393 {
394   asection *     arm_arch_section;
395   bfd_size_type  buffer_size;
396   bfd_byte *     buffer;
397   char *         arch_string;
398   int            i;
399
400   /* Look for a note section.  If one is present check the architecture
401      string encoded in it, and set it to the current architecture if it is
402      different.  */
403   arm_arch_section = bfd_get_section_by_name (abfd, note_section);
404
405   if (arm_arch_section == NULL)
406     return bfd_mach_arm_unknown;
407
408   buffer_size = arm_arch_section->size;
409   if (buffer_size == 0)
410     return bfd_mach_arm_unknown;
411
412   if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer))
413     goto FAIL;
414
415   /* Parse the note.  */
416   if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string))
417     goto FAIL;
418
419   /* Interpret the architecture string.  */
420   for (i = ARRAY_SIZE (architectures); i--;)
421     if (strcmp (arch_string, architectures[i].string) == 0)
422       {
423         free (buffer);
424         return architectures[i].mach;
425       }
426
427  FAIL:
428   if (buffer != NULL)
429     free (buffer);
430   return bfd_mach_arm_unknown;
431 }
432
433 bfd_boolean
434 bfd_is_arm_special_symbol_name (const char * name, int type)
435 {
436   /* The ARM compiler outputs several obsolete forms.  Recognize them
437      in addition to the standard $a, $t and $d.  We are somewhat loose
438      in what we accept here, since the full set is not documented.  */
439   if (!name || name[0] != '$')
440     return FALSE;
441   if (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
442     type &= BFD_ARM_SPECIAL_SYM_TYPE_MAP;
443   else if (name[1] == 'm' || name[1] == 'f' || name[1] == 'p')
444     type &= BFD_ARM_SPECIAL_SYM_TYPE_TAG;
445   else if (name[1] >= 'a' && name[1] <= 'z')
446     type &= BFD_ARM_SPECIAL_SYM_TYPE_OTHER;
447   else
448     return FALSE;
449
450   return (type != 0 && (name[2] == 0 || name[2] == '.'));
451 }
452