| 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_unknown, "arm_any", FALSE, NULL) |
| 146 | }; |
| 147 | |
| 148 | const bfd_arch_info_type bfd_arm_arch = |
| 149 | N (0, "arm", TRUE, & arch_info_struct[0]); |
| 150 | |
| 151 | /* Support functions used by both the COFF and ELF versions of the ARM port. */ |
| 152 | |
| 153 | /* Handle the merging of the 'machine' settings of input file IBFD |
| 154 | and an output file OBFD. These values actually represent the |
| 155 | different possible ARM architecture variants. |
| 156 | Returns TRUE if they were merged successfully or FALSE otherwise. */ |
| 157 | |
| 158 | bfd_boolean |
| 159 | bfd_arm_merge_machines (bfd *ibfd, bfd *obfd) |
| 160 | { |
| 161 | unsigned int in = bfd_get_mach (ibfd); |
| 162 | unsigned int out = bfd_get_mach (obfd); |
| 163 | |
| 164 | /* If the output architecture is unknown, we now have a value to set. */ |
| 165 | if (out == bfd_mach_arm_unknown) |
| 166 | bfd_set_arch_mach (obfd, bfd_arch_arm, in); |
| 167 | |
| 168 | /* If the input architecture is unknown, |
| 169 | then so must be the output architecture. */ |
| 170 | else if (in == bfd_mach_arm_unknown) |
| 171 | /* FIXME: We ought to have some way to |
| 172 | override this on the command line. */ |
| 173 | bfd_set_arch_mach (obfd, bfd_arch_arm, bfd_mach_arm_unknown); |
| 174 | |
| 175 | /* If they are the same then nothing needs to be done. */ |
| 176 | else if (out == in) |
| 177 | ; |
| 178 | |
| 179 | /* Otherwise the general principle that a earlier architecture can be |
| 180 | linked with a later architecture to produce a binary that will execute |
| 181 | on the later architecture. |
| 182 | |
| 183 | We fail however if we attempt to link a Cirrus EP9312 binary with an |
| 184 | Intel XScale binary, since these architecture have co-processors which |
| 185 | will not both be present on the same physical hardware. */ |
| 186 | else if (in == bfd_mach_arm_ep9312 |
| 187 | && (out == bfd_mach_arm_XScale |
| 188 | || out == bfd_mach_arm_iWMMXt |
| 189 | || out == bfd_mach_arm_iWMMXt2)) |
| 190 | { |
| 191 | /* xgettext: c-format */ |
| 192 | _bfd_error_handler (_("\ |
| 193 | error: %pB is compiled for the EP9312, whereas %pB is compiled for XScale"), |
| 194 | ibfd, obfd); |
| 195 | bfd_set_error (bfd_error_wrong_format); |
| 196 | return FALSE; |
| 197 | } |
| 198 | else if (out == bfd_mach_arm_ep9312 |
| 199 | && (in == bfd_mach_arm_XScale |
| 200 | || in == bfd_mach_arm_iWMMXt |
| 201 | || in == bfd_mach_arm_iWMMXt2)) |
| 202 | { |
| 203 | /* xgettext: c-format */ |
| 204 | _bfd_error_handler (_("\ |
| 205 | error: %pB is compiled for the EP9312, whereas %pB is compiled for XScale"), |
| 206 | obfd, ibfd); |
| 207 | bfd_set_error (bfd_error_wrong_format); |
| 208 | return FALSE; |
| 209 | } |
| 210 | else if (in > out) |
| 211 | bfd_set_arch_mach (obfd, bfd_arch_arm, in); |
| 212 | /* else |
| 213 | Nothing to do. */ |
| 214 | |
| 215 | return TRUE; |
| 216 | } |
| 217 | |
| 218 | typedef struct |
| 219 | { |
| 220 | unsigned char namesz[4]; /* Size of entry's owner string. */ |
| 221 | unsigned char descsz[4]; /* Size of the note descriptor. */ |
| 222 | unsigned char type[4]; /* Interpretation of the descriptor. */ |
| 223 | char name[1]; /* Start of the name+desc data. */ |
| 224 | } arm_Note; |
| 225 | |
| 226 | static bfd_boolean |
| 227 | arm_check_note (bfd *abfd, |
| 228 | bfd_byte *buffer, |
| 229 | bfd_size_type buffer_size, |
| 230 | const char *expected_name, |
| 231 | char **description_return) |
| 232 | { |
| 233 | unsigned long namesz; |
| 234 | unsigned long descsz; |
| 235 | unsigned long type; |
| 236 | char * descr; |
| 237 | |
| 238 | if (buffer_size < offsetof (arm_Note, name)) |
| 239 | return FALSE; |
| 240 | |
| 241 | /* We have to extract the values this way to allow for a |
| 242 | host whose endian-ness is different from the target. */ |
| 243 | namesz = bfd_get_32 (abfd, buffer); |
| 244 | descsz = bfd_get_32 (abfd, buffer + offsetof (arm_Note, descsz)); |
| 245 | type = bfd_get_32 (abfd, buffer + offsetof (arm_Note, type)); |
| 246 | descr = (char *) buffer + offsetof (arm_Note, name); |
| 247 | |
| 248 | /* Check for buffer overflow. */ |
| 249 | if (namesz + descsz + offsetof (arm_Note, name) > buffer_size) |
| 250 | return FALSE; |
| 251 | |
| 252 | if (expected_name == NULL) |
| 253 | { |
| 254 | if (namesz != 0) |
| 255 | return FALSE; |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | if (namesz != ((strlen (expected_name) + 1 + 3) & ~3)) |
| 260 | return FALSE; |
| 261 | |
| 262 | if (strcmp (descr, expected_name) != 0) |
| 263 | return FALSE; |
| 264 | |
| 265 | descr += (namesz + 3) & ~3; |
| 266 | } |
| 267 | |
| 268 | /* FIXME: We should probably check the type as well. */ |
| 269 | (void) type; |
| 270 | |
| 271 | if (description_return != NULL) |
| 272 | * description_return = descr; |
| 273 | |
| 274 | return TRUE; |
| 275 | } |
| 276 | |
| 277 | #define NOTE_ARCH_STRING "arch: " |
| 278 | |
| 279 | bfd_boolean |
| 280 | bfd_arm_update_notes (bfd *abfd, const char *note_section) |
| 281 | { |
| 282 | asection * arm_arch_section; |
| 283 | bfd_size_type buffer_size; |
| 284 | bfd_byte * buffer; |
| 285 | char * arch_string; |
| 286 | char * expected; |
| 287 | |
| 288 | /* Look for a note section. If one is present check the architecture |
| 289 | string encoded in it, and set it to the current architecture if it is |
| 290 | different. */ |
| 291 | arm_arch_section = bfd_get_section_by_name (abfd, note_section); |
| 292 | |
| 293 | if (arm_arch_section == NULL) |
| 294 | return TRUE; |
| 295 | |
| 296 | buffer_size = arm_arch_section->size; |
| 297 | if (buffer_size == 0) |
| 298 | return FALSE; |
| 299 | |
| 300 | if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer)) |
| 301 | goto FAIL; |
| 302 | |
| 303 | /* Parse the note. */ |
| 304 | if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string)) |
| 305 | goto FAIL; |
| 306 | |
| 307 | /* Check the architecture in the note against the architecture of the bfd. */ |
| 308 | switch (bfd_get_mach (abfd)) |
| 309 | { |
| 310 | default: |
| 311 | case bfd_mach_arm_unknown: expected = "unknown"; break; |
| 312 | case bfd_mach_arm_2: expected = "armv2"; break; |
| 313 | case bfd_mach_arm_2a: expected = "armv2a"; break; |
| 314 | case bfd_mach_arm_3: expected = "armv3"; break; |
| 315 | case bfd_mach_arm_3M: expected = "armv3M"; break; |
| 316 | case bfd_mach_arm_4: expected = "armv4"; break; |
| 317 | case bfd_mach_arm_4T: expected = "armv4t"; break; |
| 318 | case bfd_mach_arm_5: expected = "armv5"; break; |
| 319 | case bfd_mach_arm_5T: expected = "armv5t"; break; |
| 320 | case bfd_mach_arm_5TE: expected = "armv5te"; break; |
| 321 | case bfd_mach_arm_XScale: expected = "XScale"; break; |
| 322 | case bfd_mach_arm_ep9312: expected = "ep9312"; break; |
| 323 | case bfd_mach_arm_iWMMXt: expected = "iWMMXt"; break; |
| 324 | case bfd_mach_arm_iWMMXt2: expected = "iWMMXt2"; break; |
| 325 | } |
| 326 | |
| 327 | if (strcmp (arch_string, expected) != 0) |
| 328 | { |
| 329 | strcpy ((char *) buffer + (offsetof (arm_Note, name) |
| 330 | + ((strlen (NOTE_ARCH_STRING) + 3) & ~3)), |
| 331 | expected); |
| 332 | |
| 333 | if (! bfd_set_section_contents (abfd, arm_arch_section, buffer, |
| 334 | (file_ptr) 0, buffer_size)) |
| 335 | { |
| 336 | _bfd_error_handler |
| 337 | /* xgettext: c-format */ |
| 338 | (_("warning: unable to update contents of %s section in %pB"), |
| 339 | note_section, abfd); |
| 340 | goto FAIL; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | free (buffer); |
| 345 | return TRUE; |
| 346 | |
| 347 | FAIL: |
| 348 | if (buffer != NULL) |
| 349 | free (buffer); |
| 350 | return FALSE; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | static struct |
| 355 | { |
| 356 | const char * string; |
| 357 | unsigned int mach; |
| 358 | } |
| 359 | architectures[] = |
| 360 | { |
| 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 }, |
| 373 | { "iWMMXt2", bfd_mach_arm_iWMMXt2 }, |
| 374 | { "arm_any", bfd_mach_arm_unknown } |
| 375 | }; |
| 376 | |
| 377 | /* Extract the machine number stored in a note section. */ |
| 378 | unsigned int |
| 379 | bfd_arm_get_mach_from_notes (bfd *abfd, const char *note_section) |
| 380 | { |
| 381 | asection * arm_arch_section; |
| 382 | bfd_size_type buffer_size; |
| 383 | bfd_byte * buffer; |
| 384 | char * arch_string; |
| 385 | int i; |
| 386 | |
| 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 |
| 389 | different. */ |
| 390 | arm_arch_section = bfd_get_section_by_name (abfd, note_section); |
| 391 | |
| 392 | if (arm_arch_section == NULL) |
| 393 | return bfd_mach_arm_unknown; |
| 394 | |
| 395 | buffer_size = arm_arch_section->size; |
| 396 | if (buffer_size == 0) |
| 397 | return bfd_mach_arm_unknown; |
| 398 | |
| 399 | if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer)) |
| 400 | goto FAIL; |
| 401 | |
| 402 | /* Parse the note. */ |
| 403 | if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string)) |
| 404 | goto FAIL; |
| 405 | |
| 406 | /* Interpret the architecture string. */ |
| 407 | for (i = ARRAY_SIZE (architectures); i--;) |
| 408 | if (strcmp (arch_string, architectures[i].string) == 0) |
| 409 | { |
| 410 | free (buffer); |
| 411 | return architectures[i].mach; |
| 412 | } |
| 413 | |
| 414 | FAIL: |
| 415 | if (buffer != NULL) |
| 416 | free (buffer); |
| 417 | return bfd_mach_arm_unknown; |
| 418 | } |
| 419 | |
| 420 | bfd_boolean |
| 421 | bfd_is_arm_special_symbol_name (const char * name, int type) |
| 422 | { |
| 423 | /* The ARM compiler outputs several obsolete forms. Recognize them |
| 424 | in addition to the standard $a, $t and $d. We are somewhat loose |
| 425 | in what we accept here, since the full set is not documented. */ |
| 426 | if (!name || name[0] != '$') |
| 427 | return FALSE; |
| 428 | if (name[1] == 'a' || name[1] == 't' || name[1] == 'd') |
| 429 | type &= BFD_ARM_SPECIAL_SYM_TYPE_MAP; |
| 430 | else if (name[1] == 'm' || name[1] == 'f' || name[1] == 'p') |
| 431 | type &= BFD_ARM_SPECIAL_SYM_TYPE_TAG; |
| 432 | else if (name[1] >= 'a' && name[1] <= 'z') |
| 433 | type &= BFD_ARM_SPECIAL_SYM_TYPE_OTHER; |
| 434 | else |
| 435 | return FALSE; |
| 436 | |
| 437 | return (type != 0 && (name[2] == 0 || name[2] == '.')); |
| 438 | } |
| 439 | |