cpu-arm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* BFD support for the ARM processor
  2. Copyright (C) 1994-2015 Free Software Foundation, Inc.
  3. Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "libiberty.h"
  21. /* This routine is provided two arch_infos and works out which ARM
  22. machine which would be compatible with both and returns a pointer
  23. to its info structure. */
  24. static const bfd_arch_info_type *
  25. compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
  26. {
  27. /* If a & b are for different architecture we can do nothing. */
  28. if (a->arch != b->arch)
  29. return NULL;
  30. /* If a & b are for the same machine then all is well. */
  31. if (a->mach == b->mach)
  32. return a;
  33. /* Otherwise if either a or b is the 'default' machine
  34. then it can be polymorphed into the other. */
  35. if (a->the_default)
  36. return b;
  37. if (b->the_default)
  38. return a;
  39. /* So far all newer ARM architecture cores are
  40. supersets of previous cores. */
  41. if (a->mach < b->mach)
  42. return b;
  43. else if (a->mach > b->mach)
  44. return a;
  45. /* Never reached! */
  46. return NULL;
  47. }
  48. static struct
  49. {
  50. unsigned int mach;
  51. char * name;
  52. }
  53. processors[] =
  54. {
  55. { bfd_mach_arm_2, "arm2" },
  56. { bfd_mach_arm_2a, "arm250" },
  57. { bfd_mach_arm_2a, "arm3" },
  58. { bfd_mach_arm_3, "arm6" },
  59. { bfd_mach_arm_3, "arm60" },
  60. { bfd_mach_arm_3, "arm600" },
  61. { bfd_mach_arm_3, "arm610" },
  62. { bfd_mach_arm_3, "arm7" },
  63. { bfd_mach_arm_3, "arm710" },
  64. { bfd_mach_arm_3, "arm7500" },
  65. { bfd_mach_arm_3, "arm7d" },
  66. { bfd_mach_arm_3, "arm7di" },
  67. { bfd_mach_arm_3M, "arm7dm" },
  68. { bfd_mach_arm_3M, "arm7dmi" },
  69. { bfd_mach_arm_4T, "arm7tdmi" },
  70. { bfd_mach_arm_4, "arm8" },
  71. { bfd_mach_arm_4, "arm810" },
  72. { bfd_mach_arm_4, "arm9" },
  73. { bfd_mach_arm_4, "arm920" },
  74. { bfd_mach_arm_4T, "arm920t" },
  75. { bfd_mach_arm_4T, "arm9tdmi" },
  76. { bfd_mach_arm_4, "sa1" },
  77. { bfd_mach_arm_4, "strongarm"},
  78. { bfd_mach_arm_4, "strongarm110" },
  79. { bfd_mach_arm_4, "strongarm1100" },
  80. { bfd_mach_arm_XScale, "xscale" },
  81. { bfd_mach_arm_ep9312, "ep9312" },
  82. { bfd_mach_arm_iWMMXt, "iwmmxt" },
  83. { bfd_mach_arm_iWMMXt2, "iwmmxt2" }
  84. };
  85. static bfd_boolean
  86. scan (const struct bfd_arch_info *info, const char *string)
  87. {
  88. int i;
  89. /* First test for an exact match. */
  90. if (strcasecmp (string, info->printable_name) == 0)
  91. return TRUE;
  92. /* Next check for a processor name instead of an Architecture name. */
  93. for (i = sizeof (processors) / sizeof (processors[0]); i--;)
  94. {
  95. if (strcasecmp (string, processors [i].name) == 0)
  96. break;
  97. }
  98. if (i != -1 && info->mach == processors [i].mach)
  99. return TRUE;
  100. /* Finally check for the default architecture. */
  101. if (strcasecmp (string, "arm") == 0)
  102. return info->the_default;
  103. return FALSE;
  104. }
  105. #define N(number, print, default, next) \
  106. { 32, 32, 8, bfd_arch_arm, number, "arm", print, 4, default, compatible, \
  107. scan, bfd_arch_default_fill, next }
  108. static const bfd_arch_info_type arch_info_struct[] =
  109. {
  110. N (bfd_mach_arm_2, "armv2", FALSE, & arch_info_struct[1]),
  111. N (bfd_mach_arm_2a, "armv2a", FALSE, & arch_info_struct[2]),
  112. N (bfd_mach_arm_3, "armv3", FALSE, & arch_info_struct[3]),
  113. N (bfd_mach_arm_3M, "armv3m", FALSE, & arch_info_struct[4]),
  114. N (bfd_mach_arm_4, "armv4", FALSE, & arch_info_struct[5]),
  115. N (bfd_mach_arm_4T, "armv4t", FALSE, & arch_info_struct[6]),
  116. N (bfd_mach_arm_5, "armv5", FALSE, & arch_info_struct[7]),
  117. N (bfd_mach_arm_5T, "armv5t", FALSE, & arch_info_struct[8]),
  118. N (bfd_mach_arm_5TE, "armv5te", FALSE, & arch_info_struct[9]),
  119. N (bfd_mach_arm_XScale, "xscale", FALSE, & arch_info_struct[10]),
  120. N (bfd_mach_arm_ep9312, "ep9312", FALSE, & arch_info_struct[11]),
  121. N (bfd_mach_arm_iWMMXt, "iwmmxt", FALSE, & arch_info_struct[12]),
  122. N (bfd_mach_arm_iWMMXt2, "iwmmxt2", FALSE, NULL)
  123. };
  124. const bfd_arch_info_type bfd_arm_arch =
  125. N (0, "arm", TRUE, & arch_info_struct[0]);
  126. /* Support functions used by both the COFF and ELF versions of the ARM port. */
  127. /* Handle the merging of the 'machine' settings of input file IBFD
  128. and an output file OBFD. These values actually represent the
  129. different possible ARM architecture variants.
  130. Returns TRUE if they were merged successfully or FALSE otherwise. */
  131. bfd_boolean
  132. bfd_arm_merge_machines (bfd *ibfd, bfd *obfd)
  133. {
  134. unsigned int in = bfd_get_mach (ibfd);
  135. unsigned int out = bfd_get_mach (obfd);
  136. /* If the output architecture is unknown, we now have a value to set. */
  137. if (out == bfd_mach_arm_unknown)
  138. bfd_set_arch_mach (obfd, bfd_arch_arm, in);
  139. /* If the input architecture is unknown,
  140. then so must be the output architecture. */
  141. else if (in == bfd_mach_arm_unknown)
  142. /* FIXME: We ought to have some way to
  143. override this on the command line. */
  144. bfd_set_arch_mach (obfd, bfd_arch_arm, bfd_mach_arm_unknown);
  145. /* If they are the same then nothing needs to be done. */
  146. else if (out == in)
  147. ;
  148. /* Otherwise the general principle that a earlier architecture can be
  149. linked with a later architecture to produce a binary that will execute
  150. on the later architecture.
  151. We fail however if we attempt to link a Cirrus EP9312 binary with an
  152. Intel XScale binary, since these architecture have co-processors which
  153. will not both be present on the same physical hardware. */
  154. else if (in == bfd_mach_arm_ep9312
  155. && (out == bfd_mach_arm_XScale
  156. || out == bfd_mach_arm_iWMMXt
  157. || out == bfd_mach_arm_iWMMXt2))
  158. {
  159. _bfd_error_handler (_("\
  160. error: %B is compiled for the EP9312, whereas %B is compiled for XScale"),
  161. ibfd, obfd);
  162. bfd_set_error (bfd_error_wrong_format);
  163. return FALSE;
  164. }
  165. else if (out == bfd_mach_arm_ep9312
  166. && (in == bfd_mach_arm_XScale
  167. || in == bfd_mach_arm_iWMMXt
  168. || in == bfd_mach_arm_iWMMXt2))
  169. {
  170. _bfd_error_handler (_("\
  171. error: %B is compiled for the EP9312, whereas %B is compiled for XScale"),
  172. obfd, ibfd);
  173. bfd_set_error (bfd_error_wrong_format);
  174. return FALSE;
  175. }
  176. else if (in > out)
  177. bfd_set_arch_mach (obfd, bfd_arch_arm, in);
  178. /* else
  179. Nothing to do. */
  180. return TRUE;
  181. }
  182. typedef struct
  183. {
  184. unsigned char namesz[4]; /* Size of entry's owner string. */
  185. unsigned char descsz[4]; /* Size of the note descriptor. */
  186. unsigned char type[4]; /* Interpretation of the descriptor. */
  187. char name[1]; /* Start of the name+desc data. */
  188. } arm_Note;
  189. static bfd_boolean
  190. arm_check_note (bfd *abfd,
  191. bfd_byte *buffer,
  192. bfd_size_type buffer_size,
  193. const char *expected_name,
  194. char **description_return)
  195. {
  196. unsigned long namesz;
  197. unsigned long descsz;
  198. unsigned long type;
  199. char * descr;
  200. if (buffer_size < offsetof (arm_Note, name))
  201. return FALSE;
  202. /* We have to extract the values this way to allow for a
  203. host whose endian-ness is different from the target. */
  204. namesz = bfd_get_32 (abfd, buffer);
  205. descsz = bfd_get_32 (abfd, buffer + offsetof (arm_Note, descsz));
  206. type = bfd_get_32 (abfd, buffer + offsetof (arm_Note, type));
  207. descr = (char *) buffer + offsetof (arm_Note, name);
  208. /* Check for buffer overflow. */
  209. if (namesz + descsz + offsetof (arm_Note, name) > buffer_size)
  210. return FALSE;
  211. if (expected_name == NULL)
  212. {
  213. if (namesz != 0)
  214. return FALSE;
  215. }
  216. else
  217. {
  218. if (namesz != ((strlen (expected_name) + 1 + 3) & ~3))
  219. return FALSE;
  220. if (strcmp (descr, expected_name) != 0)
  221. return FALSE;
  222. descr += (namesz + 3) & ~3;
  223. }
  224. /* FIXME: We should probably check the type as well. */
  225. (void) type;
  226. if (description_return != NULL)
  227. * description_return = descr;
  228. return TRUE;
  229. }
  230. #define NOTE_ARCH_STRING "arch: "
  231. bfd_boolean
  232. bfd_arm_update_notes (bfd *abfd, const char *note_section)
  233. {
  234. asection * arm_arch_section;
  235. bfd_size_type buffer_size;
  236. bfd_byte * buffer;
  237. char * arch_string;
  238. char * expected;
  239. /* Look for a note section. If one is present check the architecture
  240. string encoded in it, and set it to the current architecture if it is
  241. different. */
  242. arm_arch_section = bfd_get_section_by_name (abfd, note_section);
  243. if (arm_arch_section == NULL)
  244. return TRUE;
  245. buffer_size = arm_arch_section->size;
  246. if (buffer_size == 0)
  247. return FALSE;
  248. if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer))
  249. goto FAIL;
  250. /* Parse the note. */
  251. if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string))
  252. goto FAIL;
  253. /* Check the architecture in the note against the architecture of the bfd. */
  254. switch (bfd_get_mach (abfd))
  255. {
  256. default:
  257. case bfd_mach_arm_unknown: expected = "unknown"; break;
  258. case bfd_mach_arm_2: expected = "armv2"; break;
  259. case bfd_mach_arm_2a: expected = "armv2a"; break;
  260. case bfd_mach_arm_3: expected = "armv3"; break;
  261. case bfd_mach_arm_3M: expected = "armv3M"; break;
  262. case bfd_mach_arm_4: expected = "armv4"; break;
  263. case bfd_mach_arm_4T: expected = "armv4t"; break;
  264. case bfd_mach_arm_5: expected = "armv5"; break;
  265. case bfd_mach_arm_5T: expected = "armv5t"; break;
  266. case bfd_mach_arm_5TE: expected = "armv5te"; break;
  267. case bfd_mach_arm_XScale: expected = "XScale"; break;
  268. case bfd_mach_arm_ep9312: expected = "ep9312"; break;
  269. case bfd_mach_arm_iWMMXt: expected = "iWMMXt"; break;
  270. case bfd_mach_arm_iWMMXt2: expected = "iWMMXt2"; break;
  271. }
  272. if (strcmp (arch_string, expected) != 0)
  273. {
  274. strcpy ((char *) buffer + (offsetof (arm_Note, name)
  275. + ((strlen (NOTE_ARCH_STRING) + 3) & ~3)),
  276. expected);
  277. if (! bfd_set_section_contents (abfd, arm_arch_section, buffer,
  278. (file_ptr) 0, buffer_size))
  279. {
  280. (*_bfd_error_handler)
  281. (_("warning: unable to update contents of %s section in %s"),
  282. note_section, bfd_get_filename (abfd));
  283. goto FAIL;
  284. }
  285. }
  286. free (buffer);
  287. return TRUE;
  288. FAIL:
  289. if (buffer != NULL)
  290. free (buffer);
  291. return FALSE;
  292. }
  293. static struct
  294. {
  295. const char * string;
  296. unsigned int mach;
  297. }
  298. architectures[] =
  299. {
  300. { "armv2", bfd_mach_arm_2 },
  301. { "armv2a", bfd_mach_arm_2a },
  302. { "armv3", bfd_mach_arm_3 },
  303. { "armv3M", bfd_mach_arm_3M },
  304. { "armv4", bfd_mach_arm_4 },
  305. { "armv4t", bfd_mach_arm_4T },
  306. { "armv5", bfd_mach_arm_5 },
  307. { "armv5t", bfd_mach_arm_5T },
  308. { "armv5te", bfd_mach_arm_5TE },
  309. { "XScale", bfd_mach_arm_XScale },
  310. { "ep9312", bfd_mach_arm_ep9312 },
  311. { "iWMMXt", bfd_mach_arm_iWMMXt },
  312. { "iWMMXt2", bfd_mach_arm_iWMMXt2 }
  313. };
  314. /* Extract the machine number stored in a note section. */
  315. unsigned int
  316. bfd_arm_get_mach_from_notes (bfd *abfd, const char *note_section)
  317. {
  318. asection * arm_arch_section;
  319. bfd_size_type buffer_size;
  320. bfd_byte * buffer;
  321. char * arch_string;
  322. int i;
  323. /* Look for a note section. If one is present check the architecture
  324. string encoded in it, and set it to the current architecture if it is
  325. different. */
  326. arm_arch_section = bfd_get_section_by_name (abfd, note_section);
  327. if (arm_arch_section == NULL)
  328. return bfd_mach_arm_unknown;
  329. buffer_size = arm_arch_section->size;
  330. if (buffer_size == 0)
  331. return bfd_mach_arm_unknown;
  332. if (!bfd_malloc_and_get_section (abfd, arm_arch_section, &buffer))
  333. goto FAIL;
  334. /* Parse the note. */
  335. if (! arm_check_note (abfd, buffer, buffer_size, NOTE_ARCH_STRING, & arch_string))
  336. goto FAIL;
  337. /* Interpret the architecture string. */
  338. for (i = ARRAY_SIZE (architectures); i--;)
  339. if (strcmp (arch_string, architectures[i].string) == 0)
  340. {
  341. free (buffer);
  342. return architectures[i].mach;
  343. }
  344. FAIL:
  345. if (buffer != NULL)
  346. free (buffer);
  347. return bfd_mach_arm_unknown;
  348. }
  349. bfd_boolean
  350. bfd_is_arm_special_symbol_name (const char * name, int type)
  351. {
  352. /* The ARM compiler outputs several obsolete forms. Recognize them
  353. in addition to the standard $a, $t and $d. We are somewhat loose
  354. in what we accept here, since the full set is not documented. */
  355. if (!name || name[0] != '$')
  356. return FALSE;
  357. if (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
  358. type &= BFD_ARM_SPECIAL_SYM_TYPE_MAP;
  359. else if (name[1] == 'm' || name[1] == 'f' || name[1] == 'p')
  360. type &= BFD_ARM_SPECIAL_SYM_TYPE_TAG;
  361. else if (name[1] >= 'a' && name[1] <= 'z')
  362. type &= BFD_ARM_SPECIAL_SYM_TYPE_OTHER;
  363. else
  364. return FALSE;
  365. return (type != 0 && (name[2] == 0 || name[2] == '.'));
  366. }