cpu-sh.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* BFD library support routines for the Renesas / SuperH SH architecture.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Hacked by Steve Chamberlain of Cygnus Support.
  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 "../opcodes/sh-opc.h"
  21. #define SH_NEXT arch_info_struct + 0
  22. #define SH2_NEXT arch_info_struct + 1
  23. #define SH2E_NEXT arch_info_struct + 2
  24. #define SH_DSP_NEXT arch_info_struct + 3
  25. #define SH3_NEXT arch_info_struct + 4
  26. #define SH3_NOMMU_NEXT arch_info_struct + 5
  27. #define SH3_DSP_NEXT arch_info_struct + 6
  28. #define SH3E_NEXT arch_info_struct + 7
  29. #define SH4_NEXT arch_info_struct + 8
  30. #define SH4A_NEXT arch_info_struct + 9
  31. #define SH4AL_DSP_NEXT arch_info_struct + 10
  32. #define SH4_NOFPU_NEXT arch_info_struct + 11
  33. #define SH4_NOMMU_NOFPU_NEXT arch_info_struct + 12
  34. #define SH4A_NOFPU_NEXT arch_info_struct + 13
  35. #define SH2A_NEXT arch_info_struct + 14
  36. #define SH2A_NOFPU_NEXT arch_info_struct + 15
  37. #define SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT arch_info_struct + 16
  38. #define SH2A_NOFPU_OR_SH3_NOMMU_NEXT arch_info_struct + 17
  39. #define SH2A_OR_SH4_NEXT arch_info_struct + 18
  40. #define SH2A_OR_SH3E_NEXT arch_info_struct + 19
  41. #define SH64_NEXT NULL
  42. static const bfd_arch_info_type arch_info_struct[] =
  43. {
  44. {
  45. 32, /* 32 bits in a word. */
  46. 32, /* 32 bits in an address. */
  47. 8, /* 8 bits in a byte. */
  48. bfd_arch_sh,
  49. bfd_mach_sh2,
  50. "sh", /* Architecture name. */
  51. "sh2", /* Machine name. */
  52. 1,
  53. FALSE, /* Not the default. */
  54. bfd_default_compatible,
  55. bfd_default_scan,
  56. bfd_arch_default_fill,
  57. SH2_NEXT
  58. },
  59. {
  60. 32, /* 32 bits in a word. */
  61. 32, /* 32 bits in an address. */
  62. 8, /* 8 bits in a byte. */
  63. bfd_arch_sh,
  64. bfd_mach_sh2e,
  65. "sh", /* Architecture name. */
  66. "sh2e", /* Machine name. */
  67. 1,
  68. FALSE, /* Not the default. */
  69. bfd_default_compatible,
  70. bfd_default_scan,
  71. bfd_arch_default_fill,
  72. SH2E_NEXT
  73. },
  74. {
  75. 32, /* 32 bits in a word. */
  76. 32, /* 32 bits in an address. */
  77. 8, /* 8 bits in a byte. */
  78. bfd_arch_sh,
  79. bfd_mach_sh_dsp,
  80. "sh", /* Architecture name. */
  81. "sh-dsp", /* Machine name. */
  82. 1,
  83. FALSE, /* Not the default. */
  84. bfd_default_compatible,
  85. bfd_default_scan,
  86. bfd_arch_default_fill,
  87. SH_DSP_NEXT
  88. },
  89. {
  90. 32, /* 32 bits in a word. */
  91. 32, /* 32 bits in an address. */
  92. 8, /* 8 bits in a byte. */
  93. bfd_arch_sh,
  94. bfd_mach_sh3,
  95. "sh", /* Architecture name. */
  96. "sh3", /* Machine name. */
  97. 1,
  98. FALSE, /* Not the default. */
  99. bfd_default_compatible,
  100. bfd_default_scan,
  101. bfd_arch_default_fill,
  102. SH3_NEXT
  103. },
  104. {
  105. 32, /* 32 bits in a word. */
  106. 32, /* 32 bits in an address. */
  107. 8, /* 8 bits in a byte. */
  108. bfd_arch_sh,
  109. bfd_mach_sh3_nommu,
  110. "sh", /* Architecture name. */
  111. "sh3-nommu", /* Machine name. */
  112. 1,
  113. FALSE, /* Not the default. */
  114. bfd_default_compatible,
  115. bfd_default_scan,
  116. bfd_arch_default_fill,
  117. SH3_NOMMU_NEXT
  118. },
  119. {
  120. 32, /* 32 bits in a word. */
  121. 32, /* 32 bits in an address. */
  122. 8, /* 8 bits in a byte. */
  123. bfd_arch_sh,
  124. bfd_mach_sh3_dsp,
  125. "sh", /* Architecture name. */
  126. "sh3-dsp", /* Machine name. */
  127. 1,
  128. FALSE, /* Not the default. */
  129. bfd_default_compatible,
  130. bfd_default_scan,
  131. bfd_arch_default_fill,
  132. SH3_DSP_NEXT
  133. },
  134. {
  135. 32, /* 32 bits in a word. */
  136. 32, /* 32 bits in an address. */
  137. 8, /* 8 bits in a byte. */
  138. bfd_arch_sh,
  139. bfd_mach_sh3e,
  140. "sh", /* Architecture name. */
  141. "sh3e", /* Machine name. */
  142. 1,
  143. FALSE, /* Not the default. */
  144. bfd_default_compatible,
  145. bfd_default_scan,
  146. bfd_arch_default_fill,
  147. SH3E_NEXT
  148. },
  149. {
  150. 32, /* 32 bits in a word. */
  151. 32, /* 32 bits in an address. */
  152. 8, /* 8 bits in a byte. */
  153. bfd_arch_sh,
  154. bfd_mach_sh4,
  155. "sh", /* Architecture name. */
  156. "sh4", /* Machine name. */
  157. 1,
  158. FALSE, /* Not the default. */
  159. bfd_default_compatible,
  160. bfd_default_scan,
  161. bfd_arch_default_fill,
  162. SH4_NEXT
  163. },
  164. {
  165. 32, /* 32 bits in a word. */
  166. 32, /* 32 bits in an address. */
  167. 8, /* 8 bits in a byte. */
  168. bfd_arch_sh,
  169. bfd_mach_sh4a,
  170. "sh", /* Architecture name. */
  171. "sh4a", /* Machine name. */
  172. 1,
  173. FALSE, /* Not the default. */
  174. bfd_default_compatible,
  175. bfd_default_scan,
  176. bfd_arch_default_fill,
  177. SH4A_NEXT
  178. },
  179. {
  180. 32, /* 32 bits in a word. */
  181. 32, /* 32 bits in an address. */
  182. 8, /* 8 bits in a byte. */
  183. bfd_arch_sh,
  184. bfd_mach_sh4al_dsp,
  185. "sh", /* Architecture name. */
  186. "sh4al-dsp", /* Machine name. */
  187. 1,
  188. FALSE, /* Not the default. */
  189. bfd_default_compatible,
  190. bfd_default_scan,
  191. bfd_arch_default_fill,
  192. SH4AL_DSP_NEXT
  193. },
  194. {
  195. 32, /* 32 bits in a word. */
  196. 32, /* 32 bits in an address. */
  197. 8, /* 8 bits in a byte. */
  198. bfd_arch_sh,
  199. bfd_mach_sh4_nofpu,
  200. "sh", /* Architecture name. */
  201. "sh4-nofpu", /* Machine name. */
  202. 1,
  203. FALSE, /* Not the default. */
  204. bfd_default_compatible,
  205. bfd_default_scan,
  206. bfd_arch_default_fill,
  207. SH4_NOFPU_NEXT
  208. },
  209. {
  210. 32, /* 32 bits in a word. */
  211. 32, /* 32 bits in an address. */
  212. 8, /* 8 bits in a byte. */
  213. bfd_arch_sh,
  214. bfd_mach_sh4_nommu_nofpu,
  215. "sh", /* Architecture name. */
  216. "sh4-nommu-nofpu", /* Machine name. */
  217. 1,
  218. FALSE, /* Not the default. */
  219. bfd_default_compatible,
  220. bfd_default_scan,
  221. bfd_arch_default_fill,
  222. SH4_NOMMU_NOFPU_NEXT
  223. },
  224. {
  225. 32, /* 32 bits in a word. */
  226. 32, /* 32 bits in an address. */
  227. 8, /* 8 bits in a byte. */
  228. bfd_arch_sh,
  229. bfd_mach_sh4a_nofpu,
  230. "sh", /* Architecture name. */
  231. "sh4a-nofpu", /* Machine name. */
  232. 1,
  233. FALSE, /* Not the default. */
  234. bfd_default_compatible,
  235. bfd_default_scan,
  236. bfd_arch_default_fill,
  237. SH4A_NOFPU_NEXT
  238. },
  239. {
  240. 32, /* 32 bits in a word. */
  241. 32, /* 32 bits in an address. */
  242. 8, /* 8 bits in a byte. */
  243. bfd_arch_sh,
  244. bfd_mach_sh2a,
  245. "sh", /* Architecture name. */
  246. "sh2a", /* Machine name. */
  247. 1,
  248. FALSE, /* Not the default. */
  249. bfd_default_compatible,
  250. bfd_default_scan,
  251. bfd_arch_default_fill,
  252. SH2A_NEXT
  253. },
  254. {
  255. 32, /* 32 bits in a word. */
  256. 32, /* 32 bits in an address. */
  257. 8, /* 8 bits in a byte. */
  258. bfd_arch_sh,
  259. bfd_mach_sh2a_nofpu,
  260. "sh", /* Architecture name. */
  261. "sh2a-nofpu", /* Machine name. */
  262. 1,
  263. FALSE, /* Not the default. */
  264. bfd_default_compatible,
  265. bfd_default_scan,
  266. bfd_arch_default_fill,
  267. SH2A_NOFPU_NEXT
  268. },
  269. {
  270. 32, /* 32 bits in a word. */
  271. 32, /* 32 bits in an address. */
  272. 8, /* 8 bits in a byte. */
  273. bfd_arch_sh,
  274. bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu,
  275. "sh", /* Architecture name. */
  276. "sh2a-nofpu-or-sh4-nommu-nofpu", /* Machine name. */
  277. 1,
  278. FALSE, /* Not the default. */
  279. bfd_default_compatible,
  280. bfd_default_scan,
  281. bfd_arch_default_fill,
  282. SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT
  283. },
  284. {
  285. 32, /* 32 bits in a word. */
  286. 32, /* 32 bits in an address. */
  287. 8, /* 8 bits in a byte. */
  288. bfd_arch_sh,
  289. bfd_mach_sh2a_nofpu_or_sh3_nommu,
  290. "sh", /* Architecture name. . */
  291. "sh2a-nofpu-or-sh3-nommu", /* Machine name. */
  292. 1,
  293. FALSE, /* Not the default. */
  294. bfd_default_compatible,
  295. bfd_default_scan,
  296. bfd_arch_default_fill,
  297. SH2A_NOFPU_OR_SH3_NOMMU_NEXT
  298. },
  299. {
  300. 32, /* 32 bits in a word. */
  301. 32, /* 32 bits in an address. */
  302. 8, /* 8 bits in a byte. */
  303. bfd_arch_sh,
  304. bfd_mach_sh2a_or_sh4,
  305. "sh", /* Architecture name. */
  306. "sh2a-or-sh4", /* Machine name. */
  307. 1,
  308. FALSE, /* Not the default. */
  309. bfd_default_compatible,
  310. bfd_default_scan,
  311. bfd_arch_default_fill,
  312. SH2A_OR_SH4_NEXT
  313. },
  314. {
  315. 32, /* 32 bits in a word. */
  316. 32, /* 32 bits in an address. */
  317. 8, /* 8 bits in a byte. */
  318. bfd_arch_sh,
  319. bfd_mach_sh2a_or_sh3e,
  320. "sh", /* Architecture name. */
  321. "sh2a-or-sh3e", /* Machine name. */
  322. 1,
  323. FALSE, /* Not the default. */
  324. bfd_default_compatible,
  325. bfd_default_scan,
  326. bfd_arch_default_fill,
  327. SH2A_OR_SH3E_NEXT
  328. },
  329. {
  330. 64, /* 64 bits in a word. */
  331. 64, /* 64 bits in an address. */
  332. 8, /* 8 bits in a byte. */
  333. bfd_arch_sh,
  334. bfd_mach_sh5,
  335. "sh", /* Architecture name. */
  336. "sh5", /* Machine name. */
  337. 1,
  338. FALSE, /* Not the default. */
  339. bfd_default_compatible,
  340. bfd_default_scan,
  341. bfd_arch_default_fill,
  342. SH64_NEXT
  343. },
  344. };
  345. const bfd_arch_info_type bfd_sh_arch =
  346. {
  347. 32, /* 32 bits in a word. */
  348. 32, /* 32 bits in an address. */
  349. 8, /* 8 bits in a byte. */
  350. bfd_arch_sh,
  351. bfd_mach_sh,
  352. "sh", /* Architecture name. */
  353. "sh", /* Machine name. */
  354. 1,
  355. TRUE, /* The default machine. */
  356. bfd_default_compatible,
  357. bfd_default_scan,
  358. bfd_arch_default_fill,
  359. SH_NEXT
  360. };
  361. /* This table defines the mappings from the BFD internal numbering
  362. system to the opcodes internal flags system.
  363. It is used by the functions defined below.
  364. The prototypes for these SH specific functions are found in
  365. sh-opc.h . */
  366. static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
  367. {
  368. { bfd_mach_sh, arch_sh1, arch_sh_up },
  369. { bfd_mach_sh2, arch_sh2, arch_sh2_up },
  370. { bfd_mach_sh2e, arch_sh2e, arch_sh2e_up },
  371. { bfd_mach_sh_dsp, arch_sh_dsp, arch_sh_dsp_up },
  372. { bfd_mach_sh2a, arch_sh2a, arch_sh2a_up },
  373. { bfd_mach_sh2a_nofpu, arch_sh2a_nofpu, arch_sh2a_nofpu_up },
  374. { bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu_up },
  375. { bfd_mach_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu_up },
  376. { bfd_mach_sh2a_or_sh4, arch_sh2a_or_sh4, arch_sh2a_or_sh4_up },
  377. { bfd_mach_sh2a_or_sh3e, arch_sh2a_or_sh3e, arch_sh2a_or_sh3e_up },
  378. { bfd_mach_sh3, arch_sh3, arch_sh3_up },
  379. { bfd_mach_sh3_nommu, arch_sh3_nommu, arch_sh3_nommu_up },
  380. { bfd_mach_sh3_dsp, arch_sh3_dsp, arch_sh3_dsp_up },
  381. { bfd_mach_sh3e, arch_sh3e, arch_sh3e_up },
  382. { bfd_mach_sh4, arch_sh4, arch_sh4_up },
  383. { bfd_mach_sh4a, arch_sh4a, arch_sh4a_up },
  384. { bfd_mach_sh4al_dsp, arch_sh4al_dsp, arch_sh4al_dsp_up },
  385. { bfd_mach_sh4_nofpu, arch_sh4_nofpu, arch_sh4_nofpu_up },
  386. { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
  387. { bfd_mach_sh4a_nofpu, arch_sh4a_nofpu, arch_sh4a_nofpu_up },
  388. { 0, 0, 0 } /* Terminator. */
  389. };
  390. /* Convert a BFD mach number into the right opcodes arch flags
  391. using the table above. */
  392. unsigned int
  393. sh_get_arch_from_bfd_mach (unsigned long mach)
  394. {
  395. int i = 0;
  396. while (bfd_to_arch_table[i].bfd_mach != 0)
  397. if (bfd_to_arch_table[i].bfd_mach == mach)
  398. return bfd_to_arch_table[i].arch;
  399. else
  400. i++;
  401. /* Machine not found. */
  402. BFD_FAIL();
  403. return SH_ARCH_UNKNOWN_ARCH;
  404. }
  405. /* Convert a BFD mach number into a set of opcodes arch flags
  406. describing all the compatible architectures (i.e. arch_up)
  407. using the table above. */
  408. unsigned int
  409. sh_get_arch_up_from_bfd_mach (unsigned long mach)
  410. {
  411. int i = 0;
  412. while (bfd_to_arch_table[i].bfd_mach != 0)
  413. if (bfd_to_arch_table[i].bfd_mach == mach)
  414. return bfd_to_arch_table[i].arch_up;
  415. else
  416. i++;
  417. /* Machine not found. */
  418. BFD_FAIL();
  419. return SH_ARCH_UNKNOWN_ARCH;
  420. }
  421. /* Convert an arbitary arch_set - not necessarily corresponding
  422. directly to anything in the table above - to the most generic
  423. architecture which supports all the required features, and
  424. return the corresponding BFD mach. */
  425. unsigned long
  426. sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
  427. {
  428. unsigned long result = 0;
  429. unsigned int best = ~arch_set;
  430. unsigned int co_mask = ~0;
  431. int i = 0;
  432. /* If arch_set permits variants with no coprocessor then do not allow
  433. the other irrelevant co-processor bits to influence the choice:
  434. e.g. if dsp is disallowed by arch_set, then the algorithm would
  435. prefer fpu variants over nofpu variants because they also disallow
  436. dsp - even though the nofpu would be the most correct choice.
  437. This assumes that EVERY fpu/dsp variant has a no-coprocessor
  438. counter-part, or their non-fpu/dsp instructions do not have the
  439. no co-processor bit set. */
  440. if (arch_set & arch_sh_no_co)
  441. co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
  442. while (bfd_to_arch_table[i].bfd_mach != 0)
  443. {
  444. unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
  445. /* Conceptually: Find the architecture with the least number
  446. of extra features or, if they have the same number, then
  447. the greatest number of required features. Disregard
  448. architectures where the required features alone do
  449. not describe a valid architecture. */
  450. if (((try & ~arch_set) < (best & ~arch_set)
  451. || ((try & ~arch_set) == (best & ~arch_set)
  452. && (~try & arch_set) < (~best & arch_set)))
  453. && SH_MERGE_ARCH_SET_VALID (try, arch_set))
  454. {
  455. result = bfd_to_arch_table[i].bfd_mach;
  456. best = try;
  457. }
  458. i++;
  459. }
  460. /* This might happen if a new variant is added to sh-opc.h
  461. but no corresponding entry is added to the table above. */
  462. BFD_ASSERT (result != 0);
  463. return result;
  464. }
  465. /* Merge the architecture type of two BFD files, such that the
  466. resultant architecture supports all the features required
  467. by the two input BFDs.
  468. If the input BFDs are multually incompatible - i.e. one uses
  469. DSP while the other uses FPU - or there is no known architecture
  470. that fits the requirements then an error is emitted. */
  471. bfd_boolean
  472. sh_merge_bfd_arch (bfd *ibfd, bfd *obfd)
  473. {
  474. unsigned int old_arch, new_arch, merged_arch;
  475. if (! _bfd_generic_verify_endian_match (ibfd, obfd))
  476. return FALSE;
  477. old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
  478. new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
  479. merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
  480. if (!SH_VALID_CO_ARCH_SET (merged_arch))
  481. {
  482. (*_bfd_error_handler)
  483. ("%B: uses %s instructions while previous modules use %s instructions",
  484. ibfd,
  485. SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
  486. SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
  487. bfd_set_error (bfd_error_bad_value);
  488. return FALSE;
  489. }
  490. else if (!SH_VALID_ARCH_SET (merged_arch))
  491. {
  492. (*_bfd_error_handler)
  493. ("internal error: merge of architecture '%s' with architecture '%s' produced unknown architecture\n",
  494. bfd_printable_name (obfd),
  495. bfd_printable_name (ibfd));
  496. bfd_set_error (bfd_error_bad_value);
  497. return FALSE;
  498. }
  499. bfd_default_set_arch_mach (obfd, bfd_arch_sh,
  500. sh_get_bfd_mach_from_arch_set (merged_arch));
  501. return TRUE;
  502. }