elf32-h8300.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. /* BFD back-end for Renesas H8/300 ELF binaries.
  2. Copyright (C) 1993-2017 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/h8.h"
  21. static reloc_howto_type *elf32_h8_reloc_type_lookup
  22. (bfd *abfd, bfd_reloc_code_real_type code);
  23. static void elf32_h8_info_to_howto
  24. (bfd *, arelent *, Elf_Internal_Rela *);
  25. static void elf32_h8_info_to_howto_rel
  26. (bfd *, arelent *, Elf_Internal_Rela *);
  27. static unsigned long elf32_h8_mach (flagword);
  28. static void elf32_h8_final_write_processing (bfd *, bfd_boolean);
  29. static bfd_boolean elf32_h8_object_p (bfd *);
  30. static bfd_boolean elf32_h8_merge_private_bfd_data
  31. (bfd *, struct bfd_link_info *);
  32. static bfd_boolean elf32_h8_relax_section
  33. (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
  34. static bfd_boolean elf32_h8_relax_delete_bytes
  35. (bfd *, asection *, bfd_vma, int);
  36. static bfd_boolean elf32_h8_symbol_address_p (bfd *, asection *, bfd_vma);
  37. static bfd_byte *elf32_h8_get_relocated_section_contents
  38. (bfd *, struct bfd_link_info *, struct bfd_link_order *,
  39. bfd_byte *, bfd_boolean, asymbol **);
  40. static bfd_reloc_status_type elf32_h8_final_link_relocate
  41. (unsigned long, bfd *, bfd *, asection *,
  42. bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
  43. struct bfd_link_info *, asection *, int);
  44. static bfd_boolean elf32_h8_relocate_section
  45. (bfd *, struct bfd_link_info *, bfd *, asection *,
  46. bfd_byte *, Elf_Internal_Rela *,
  47. Elf_Internal_Sym *, asection **);
  48. static bfd_reloc_status_type special
  49. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  50. /* This does not include any relocation information, but should be
  51. good enough for GDB or objdump to read the file. */
  52. static reloc_howto_type h8_elf_howto_table[] =
  53. {
  54. #define R_H8_NONE_X 0
  55. HOWTO (R_H8_NONE, /* type */
  56. 0, /* rightshift */
  57. 3, /* size (0 = byte, 1 = short, 2 = long) */
  58. 0, /* bitsize */
  59. FALSE, /* pc_relative */
  60. 0, /* bitpos */
  61. complain_overflow_dont,/* complain_on_overflow */
  62. special, /* special_function */
  63. "R_H8_NONE", /* name */
  64. FALSE, /* partial_inplace */
  65. 0, /* src_mask */
  66. 0, /* dst_mask */
  67. FALSE), /* pcrel_offset */
  68. #define R_H8_DIR32_X (R_H8_NONE_X + 1)
  69. HOWTO (R_H8_DIR32, /* type */
  70. 0, /* rightshift */
  71. 2, /* size (0 = byte, 1 = short, 2 = long) */
  72. 32, /* bitsize */
  73. FALSE, /* pc_relative */
  74. 0, /* bitpos */
  75. complain_overflow_dont,/* complain_on_overflow */
  76. special, /* special_function */
  77. "R_H8_DIR32", /* name */
  78. FALSE, /* partial_inplace */
  79. 0, /* src_mask */
  80. 0xffffffff, /* dst_mask */
  81. FALSE), /* pcrel_offset */
  82. #define R_H8_DIR16_X (R_H8_DIR32_X + 1)
  83. HOWTO (R_H8_DIR16, /* type */
  84. 0, /* rightshift */
  85. 1, /* size (0 = byte, 1 = short, 2 = long) */
  86. 16, /* bitsize */
  87. FALSE, /* pc_relative */
  88. 0, /* bitpos */
  89. complain_overflow_dont,/* complain_on_overflow */
  90. special, /* special_function */
  91. "R_H8_DIR16", /* name */
  92. FALSE, /* partial_inplace */
  93. 0, /* src_mask */
  94. 0x0000ffff, /* dst_mask */
  95. FALSE), /* pcrel_offset */
  96. #define R_H8_DIR8_X (R_H8_DIR16_X + 1)
  97. HOWTO (R_H8_DIR8, /* type */
  98. 0, /* rightshift */
  99. 0, /* size (0 = byte, 1 = short, 2 = long) */
  100. 8, /* bitsize */
  101. FALSE, /* pc_relative */
  102. 0, /* bitpos */
  103. complain_overflow_dont,/* complain_on_overflow */
  104. special, /* special_function */
  105. "R_H8_DIR8", /* name */
  106. FALSE, /* partial_inplace */
  107. 0, /* src_mask */
  108. 0x000000ff, /* dst_mask */
  109. FALSE), /* pcrel_offset */
  110. #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1)
  111. HOWTO (R_H8_DIR16A8, /* type */
  112. 0, /* rightshift */
  113. 1, /* size (0 = byte, 1 = short, 2 = long) */
  114. 16, /* bitsize */
  115. FALSE, /* pc_relative */
  116. 0, /* bitpos */
  117. complain_overflow_bitfield, /* complain_on_overflow */
  118. special, /* special_function */
  119. "R_H8_DIR16A8", /* name */
  120. FALSE, /* partial_inplace */
  121. 0, /* src_mask */
  122. 0x0000ffff, /* dst_mask */
  123. FALSE), /* pcrel_offset */
  124. #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1)
  125. HOWTO (R_H8_DIR16R8, /* type */
  126. 0, /* rightshift */
  127. 1, /* size (0 = byte, 1 = short, 2 = long) */
  128. 16, /* bitsize */
  129. FALSE, /* pc_relative */
  130. 0, /* bitpos */
  131. complain_overflow_bitfield, /* complain_on_overflow */
  132. special, /* special_function */
  133. "R_H8_DIR16R8", /* name */
  134. FALSE, /* partial_inplace */
  135. 0, /* src_mask */
  136. 0x0000ffff, /* dst_mask */
  137. FALSE), /* pcrel_offset */
  138. #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1)
  139. HOWTO (R_H8_DIR24A8, /* type */
  140. 0, /* rightshift */
  141. 2, /* size (0 = byte, 1 = short, 2 = long) */
  142. 24, /* bitsize */
  143. FALSE, /* pc_relative */
  144. 0, /* bitpos */
  145. complain_overflow_bitfield, /* complain_on_overflow */
  146. special, /* special_function */
  147. "R_H8_DIR24A8", /* name */
  148. TRUE, /* partial_inplace */
  149. 0xff000000, /* src_mask */
  150. 0x00ffffff, /* dst_mask */
  151. FALSE), /* pcrel_offset */
  152. #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1)
  153. HOWTO (R_H8_DIR24R8, /* type */
  154. 0, /* rightshift */
  155. 2, /* size (0 = byte, 1 = short, 2 = long) */
  156. 24, /* bitsize */
  157. FALSE, /* pc_relative */
  158. 0, /* bitpos */
  159. complain_overflow_bitfield, /* complain_on_overflow */
  160. special, /* special_function */
  161. "R_H8_DIR24R8", /* name */
  162. TRUE, /* partial_inplace */
  163. 0xff000000, /* src_mask */
  164. 0x00ffffff, /* dst_mask */
  165. FALSE), /* pcrel_offset */
  166. #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1)
  167. HOWTO (R_H8_DIR32A16, /* type */
  168. 0, /* rightshift */
  169. 2, /* size (0 = byte, 1 = short, 2 = long) */
  170. 32, /* bitsize */
  171. FALSE, /* pc_relative */
  172. 0, /* bitpos */
  173. complain_overflow_dont,/* complain_on_overflow */
  174. special, /* special_function */
  175. "R_H8_DIR32A16", /* name */
  176. FALSE, /* partial_inplace */
  177. 0, /* src_mask */
  178. 0xffffffff, /* dst_mask */
  179. FALSE), /* pcrel_offset */
  180. #define R_H8_DISP32A16_X (R_H8_DIR32A16_X + 1)
  181. HOWTO (R_H8_DISP32A16, /* type */
  182. 0, /* rightshift */
  183. 2, /* size (0 = byte, 1 = short, 2 = long) */
  184. 32, /* bitsize */
  185. FALSE, /* pc_relative */
  186. 0, /* bitpos */
  187. complain_overflow_dont,/* complain_on_overflow */
  188. special, /* special_function */
  189. "R_H8_DISP32A16", /* name */
  190. FALSE, /* partial_inplace */
  191. 0, /* src_mask */
  192. 0xffffffff, /* dst_mask */
  193. FALSE), /* pcrel_offset */
  194. #define R_H8_PCREL16_X (R_H8_DISP32A16_X + 1)
  195. HOWTO (R_H8_PCREL16, /* type */
  196. 0, /* rightshift */
  197. 1, /* size (0 = byte, 1 = short, 2 = long) */
  198. 16, /* bitsize */
  199. TRUE, /* pc_relative */
  200. 0, /* bitpos */
  201. complain_overflow_signed,/* complain_on_overflow */
  202. special, /* special_function */
  203. "R_H8_PCREL16", /* name */
  204. FALSE, /* partial_inplace */
  205. 0xffff, /* src_mask */
  206. 0xffff, /* dst_mask */
  207. TRUE), /* pcrel_offset */
  208. #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1)
  209. HOWTO (R_H8_PCREL8, /* type */
  210. 0, /* rightshift */
  211. 0, /* size (0 = byte, 1 = short, 2 = long) */
  212. 8, /* bitsize */
  213. TRUE, /* pc_relative */
  214. 0, /* bitpos */
  215. complain_overflow_signed,/* complain_on_overflow */
  216. special, /* special_function */
  217. "R_H8_PCREL8", /* name */
  218. FALSE, /* partial_inplace */
  219. 0xff, /* src_mask */
  220. 0xff, /* dst_mask */
  221. TRUE), /* pcrel_offset */
  222. };
  223. /* This structure is used to map BFD reloc codes to H8 ELF relocs. */
  224. struct elf_reloc_map {
  225. bfd_reloc_code_real_type bfd_reloc_val;
  226. unsigned char howto_index;
  227. };
  228. /* An array mapping BFD reloc codes to H8 ELF relocs. */
  229. static const struct elf_reloc_map h8_reloc_map[] = {
  230. { BFD_RELOC_NONE, R_H8_NONE_X },
  231. { BFD_RELOC_32, R_H8_DIR32_X },
  232. { BFD_RELOC_16, R_H8_DIR16_X },
  233. { BFD_RELOC_8, R_H8_DIR8_X },
  234. { BFD_RELOC_H8_DIR16A8, R_H8_DIR16A8_X },
  235. { BFD_RELOC_H8_DIR16R8, R_H8_DIR16R8_X },
  236. { BFD_RELOC_H8_DIR24A8, R_H8_DIR24A8_X },
  237. { BFD_RELOC_H8_DIR24R8, R_H8_DIR24R8_X },
  238. { BFD_RELOC_H8_DIR32A16, R_H8_DIR32A16_X },
  239. { BFD_RELOC_H8_DISP32A16, R_H8_DISP32A16_X },
  240. { BFD_RELOC_16_PCREL, R_H8_PCREL16_X },
  241. { BFD_RELOC_8_PCREL, R_H8_PCREL8_X },
  242. };
  243. static reloc_howto_type *
  244. elf32_h8_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  245. bfd_reloc_code_real_type code)
  246. {
  247. unsigned int i;
  248. for (i = 0; i < sizeof (h8_reloc_map) / sizeof (struct elf_reloc_map); i++)
  249. {
  250. if (h8_reloc_map[i].bfd_reloc_val == code)
  251. return &h8_elf_howto_table[(int) h8_reloc_map[i].howto_index];
  252. }
  253. return NULL;
  254. }
  255. static reloc_howto_type *
  256. elf32_h8_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  257. const char *r_name)
  258. {
  259. unsigned int i;
  260. for (i = 0;
  261. i < sizeof (h8_elf_howto_table) / sizeof (h8_elf_howto_table[0]);
  262. i++)
  263. if (h8_elf_howto_table[i].name != NULL
  264. && strcasecmp (h8_elf_howto_table[i].name, r_name) == 0)
  265. return &h8_elf_howto_table[i];
  266. return NULL;
  267. }
  268. static void
  269. elf32_h8_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
  270. Elf_Internal_Rela *elf_reloc)
  271. {
  272. unsigned int r;
  273. unsigned int i;
  274. r = ELF32_R_TYPE (elf_reloc->r_info);
  275. for (i = 0; i < sizeof (h8_elf_howto_table) / sizeof (reloc_howto_type); i++)
  276. if (h8_elf_howto_table[i].type == r)
  277. {
  278. bfd_reloc->howto = &h8_elf_howto_table[i];
  279. return;
  280. }
  281. abort ();
  282. }
  283. static void
  284. elf32_h8_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
  285. Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED)
  286. {
  287. unsigned int r;
  288. abort ();
  289. r = ELF32_R_TYPE (elf_reloc->r_info);
  290. bfd_reloc->howto = &h8_elf_howto_table[r];
  291. }
  292. /* Special handling for H8/300 relocs.
  293. We only come here for pcrel stuff and return normally if not an -r link.
  294. When doing -r, we can't do any arithmetic for the pcrel stuff, because
  295. we support relaxing on the H8/300 series chips. */
  296. static bfd_reloc_status_type
  297. special (bfd *abfd ATTRIBUTE_UNUSED,
  298. arelent *reloc_entry ATTRIBUTE_UNUSED,
  299. asymbol *symbol ATTRIBUTE_UNUSED,
  300. void * data ATTRIBUTE_UNUSED,
  301. asection *input_section ATTRIBUTE_UNUSED,
  302. bfd *output_bfd,
  303. char **error_message ATTRIBUTE_UNUSED)
  304. {
  305. if (output_bfd == (bfd *) NULL)
  306. return bfd_reloc_continue;
  307. /* Adjust the reloc address to that in the output section. */
  308. reloc_entry->address += input_section->output_offset;
  309. return bfd_reloc_ok;
  310. }
  311. /* Perform a relocation as part of a final link. */
  312. static bfd_reloc_status_type
  313. elf32_h8_final_link_relocate (unsigned long r_type, bfd *input_bfd,
  314. bfd *output_bfd ATTRIBUTE_UNUSED,
  315. asection *input_section ATTRIBUTE_UNUSED,
  316. bfd_byte *contents, bfd_vma offset,
  317. bfd_vma value, bfd_vma addend,
  318. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  319. asection *sym_sec ATTRIBUTE_UNUSED,
  320. int is_local ATTRIBUTE_UNUSED)
  321. {
  322. bfd_byte *hit_data = contents + offset;
  323. switch (r_type)
  324. {
  325. case R_H8_NONE:
  326. return bfd_reloc_ok;
  327. case R_H8_DIR32:
  328. case R_H8_DIR32A16:
  329. case R_H8_DISP32A16:
  330. case R_H8_DIR24A8:
  331. value += addend;
  332. bfd_put_32 (input_bfd, value, hit_data);
  333. return bfd_reloc_ok;
  334. case R_H8_DIR16:
  335. case R_H8_DIR16A8:
  336. case R_H8_DIR16R8:
  337. value += addend;
  338. bfd_put_16 (input_bfd, value, hit_data);
  339. return bfd_reloc_ok;
  340. /* AKA R_RELBYTE */
  341. case R_H8_DIR8:
  342. value += addend;
  343. bfd_put_8 (input_bfd, value, hit_data);
  344. return bfd_reloc_ok;
  345. case R_H8_DIR24R8:
  346. value += addend;
  347. /* HIT_DATA is the address for the first byte for the relocated
  348. value. Subtract 1 so that we can manipulate the data in 32-bit
  349. hunks. */
  350. hit_data--;
  351. /* Clear out the top byte in value. */
  352. value &= 0xffffff;
  353. /* Retrieve the type byte for value from the section contents. */
  354. value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
  355. /* Now scribble it out in one 32-bit hunk. */
  356. bfd_put_32 (input_bfd, value, hit_data);
  357. return bfd_reloc_ok;
  358. case R_H8_PCREL16:
  359. value -= (input_section->output_section->vma
  360. + input_section->output_offset);
  361. value -= offset;
  362. value += addend;
  363. /* The value is relative to the start of the instruction,
  364. not the relocation offset. Subtract 2 to account for
  365. this minor issue. */
  366. value -= 2;
  367. bfd_put_16 (input_bfd, value, hit_data);
  368. return bfd_reloc_ok;
  369. case R_H8_PCREL8:
  370. value -= (input_section->output_section->vma
  371. + input_section->output_offset);
  372. value -= offset;
  373. value += addend;
  374. /* The value is relative to the start of the instruction,
  375. not the relocation offset. Subtract 1 to account for
  376. this minor issue. */
  377. value -= 1;
  378. bfd_put_8 (input_bfd, value, hit_data);
  379. return bfd_reloc_ok;
  380. default:
  381. return bfd_reloc_notsupported;
  382. }
  383. }
  384. /* Relocate an H8 ELF section. */
  385. static bfd_boolean
  386. elf32_h8_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
  387. bfd *input_bfd, asection *input_section,
  388. bfd_byte *contents, Elf_Internal_Rela *relocs,
  389. Elf_Internal_Sym *local_syms,
  390. asection **local_sections)
  391. {
  392. Elf_Internal_Shdr *symtab_hdr;
  393. struct elf_link_hash_entry **sym_hashes;
  394. Elf_Internal_Rela *rel, *relend;
  395. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  396. sym_hashes = elf_sym_hashes (input_bfd);
  397. rel = relocs;
  398. relend = relocs + input_section->reloc_count;
  399. for (; rel < relend; rel++)
  400. {
  401. unsigned int r_type;
  402. unsigned long r_symndx;
  403. Elf_Internal_Sym *sym;
  404. asection *sec;
  405. struct elf_link_hash_entry *h;
  406. bfd_vma relocation;
  407. bfd_reloc_status_type r;
  408. arelent bfd_reloc;
  409. reloc_howto_type *howto;
  410. elf32_h8_info_to_howto (input_bfd, &bfd_reloc, rel);
  411. howto = bfd_reloc.howto;
  412. r_symndx = ELF32_R_SYM (rel->r_info);
  413. r_type = ELF32_R_TYPE (rel->r_info);
  414. h = NULL;
  415. sym = NULL;
  416. sec = NULL;
  417. if (r_symndx < symtab_hdr->sh_info)
  418. {
  419. sym = local_syms + r_symndx;
  420. sec = local_sections[r_symndx];
  421. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  422. }
  423. else
  424. {
  425. bfd_boolean unresolved_reloc, warned, ignored;
  426. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  427. r_symndx, symtab_hdr, sym_hashes,
  428. h, sec, relocation,
  429. unresolved_reloc, warned, ignored);
  430. }
  431. if (sec != NULL && discarded_section (sec))
  432. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  433. rel, 1, relend, howto, 0, contents);
  434. if (bfd_link_relocatable (info))
  435. continue;
  436. r = elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd,
  437. input_section,
  438. contents, rel->r_offset,
  439. relocation, rel->r_addend,
  440. info, sec, h == NULL);
  441. if (r != bfd_reloc_ok)
  442. {
  443. const char *name;
  444. const char *msg = (const char *) 0;
  445. if (h != NULL)
  446. name = h->root.root.string;
  447. else
  448. {
  449. name = (bfd_elf_string_from_elf_section
  450. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  451. if (name == NULL || *name == '\0')
  452. name = bfd_section_name (input_bfd, sec);
  453. }
  454. switch (r)
  455. {
  456. case bfd_reloc_overflow:
  457. (*info->callbacks->reloc_overflow)
  458. (info, (h ? &h->root : NULL), name, howto->name,
  459. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  460. break;
  461. case bfd_reloc_undefined:
  462. (*info->callbacks->undefined_symbol)
  463. (info, name, input_bfd, input_section, rel->r_offset, TRUE);
  464. break;
  465. case bfd_reloc_outofrange:
  466. msg = _("internal error: out of range error");
  467. goto common_error;
  468. case bfd_reloc_notsupported:
  469. msg = _("internal error: unsupported relocation error");
  470. goto common_error;
  471. case bfd_reloc_dangerous:
  472. msg = _("internal error: dangerous error");
  473. goto common_error;
  474. default:
  475. msg = _("internal error: unknown error");
  476. /* fall through */
  477. common_error:
  478. (*info->callbacks->warning) (info, msg, name, input_bfd,
  479. input_section, rel->r_offset);
  480. break;
  481. }
  482. }
  483. }
  484. return TRUE;
  485. }
  486. /* Object files encode the specific H8 model they were compiled
  487. for in the ELF flags field.
  488. Examine that field and return the proper BFD machine type for
  489. the object file. */
  490. static unsigned long
  491. elf32_h8_mach (flagword flags)
  492. {
  493. switch (flags & EF_H8_MACH)
  494. {
  495. case E_H8_MACH_H8300:
  496. default:
  497. return bfd_mach_h8300;
  498. case E_H8_MACH_H8300H:
  499. return bfd_mach_h8300h;
  500. case E_H8_MACH_H8300S:
  501. return bfd_mach_h8300s;
  502. case E_H8_MACH_H8300HN:
  503. return bfd_mach_h8300hn;
  504. case E_H8_MACH_H8300SN:
  505. return bfd_mach_h8300sn;
  506. case E_H8_MACH_H8300SX:
  507. return bfd_mach_h8300sx;
  508. case E_H8_MACH_H8300SXN:
  509. return bfd_mach_h8300sxn;
  510. }
  511. }
  512. /* The final processing done just before writing out a H8 ELF object
  513. file. We use this opportunity to encode the BFD machine type
  514. into the flags field in the object file. */
  515. static void
  516. elf32_h8_final_write_processing (bfd *abfd,
  517. bfd_boolean linker ATTRIBUTE_UNUSED)
  518. {
  519. unsigned long val;
  520. switch (bfd_get_mach (abfd))
  521. {
  522. default:
  523. case bfd_mach_h8300:
  524. val = E_H8_MACH_H8300;
  525. break;
  526. case bfd_mach_h8300h:
  527. val = E_H8_MACH_H8300H;
  528. break;
  529. case bfd_mach_h8300s:
  530. val = E_H8_MACH_H8300S;
  531. break;
  532. case bfd_mach_h8300hn:
  533. val = E_H8_MACH_H8300HN;
  534. break;
  535. case bfd_mach_h8300sn:
  536. val = E_H8_MACH_H8300SN;
  537. break;
  538. case bfd_mach_h8300sx:
  539. val = E_H8_MACH_H8300SX;
  540. break;
  541. case bfd_mach_h8300sxn:
  542. val = E_H8_MACH_H8300SXN;
  543. break;
  544. }
  545. elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH);
  546. elf_elfheader (abfd)->e_flags |= val;
  547. }
  548. /* Return nonzero if ABFD represents a valid H8 ELF object file; also
  549. record the encoded machine type found in the ELF flags. */
  550. static bfd_boolean
  551. elf32_h8_object_p (bfd *abfd)
  552. {
  553. bfd_default_set_arch_mach (abfd, bfd_arch_h8300,
  554. elf32_h8_mach (elf_elfheader (abfd)->e_flags));
  555. return TRUE;
  556. }
  557. /* Merge backend specific data from an object file to the output
  558. object file when linking. The only data we need to copy at this
  559. time is the architecture/machine information. */
  560. static bfd_boolean
  561. elf32_h8_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  562. {
  563. bfd *obfd = info->output_bfd;
  564. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  565. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  566. return TRUE;
  567. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  568. && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
  569. {
  570. if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  571. bfd_get_mach (ibfd)))
  572. return FALSE;
  573. }
  574. return TRUE;
  575. }
  576. /* This function handles relaxing for the H8..
  577. There are a few relaxing opportunities available on the H8:
  578. jmp/jsr:24 -> bra/bsr:8 2 bytes
  579. The jmp may be completely eliminated if the previous insn is a
  580. conditional branch to the insn after the jump. In that case
  581. we invert the branch and delete the jump and save 4 bytes.
  582. bCC:16 -> bCC:8 2 bytes
  583. bsr:16 -> bsr:8 2 bytes
  584. bset:16 -> bset:8 2 bytes
  585. bset:24/32 -> bset:8 4 bytes
  586. (also applicable to other bit manipulation instructions)
  587. mov.b:16 -> mov.b:8 2 bytes
  588. mov.b:24/32 -> mov.b:8 4 bytes
  589. bset:24/32 -> bset:16 2 bytes
  590. (also applicable to other bit manipulation instructions)
  591. mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes
  592. mov.[bwl] @(displ:24/32+ERx) -> mov.[bwl] @(displ:16+ERx) 4 bytes. */
  593. static bfd_boolean
  594. elf32_h8_relax_section (bfd *abfd, asection *sec,
  595. struct bfd_link_info *link_info, bfd_boolean *again)
  596. {
  597. Elf_Internal_Shdr *symtab_hdr;
  598. Elf_Internal_Rela *internal_relocs;
  599. Elf_Internal_Rela *irel, *irelend;
  600. bfd_byte *contents = NULL;
  601. Elf_Internal_Sym *isymbuf = NULL;
  602. static asection *last_input_section = NULL;
  603. static Elf_Internal_Rela *last_reloc = NULL;
  604. /* Assume nothing changes. */
  605. *again = FALSE;
  606. /* We don't have to do anything for a relocatable link, if
  607. this section does not have relocs, or if this is not a
  608. code section. */
  609. if (bfd_link_relocatable (link_info)
  610. || (sec->flags & SEC_RELOC) == 0
  611. || sec->reloc_count == 0
  612. || (sec->flags & SEC_CODE) == 0)
  613. return TRUE;
  614. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  615. /* Get a copy of the native relocations. */
  616. internal_relocs = (_bfd_elf_link_read_relocs
  617. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  618. link_info->keep_memory));
  619. if (internal_relocs == NULL)
  620. goto error_return;
  621. if (sec != last_input_section)
  622. last_reloc = NULL;
  623. last_input_section = sec;
  624. /* Walk through the relocs looking for relaxing opportunities. */
  625. irelend = internal_relocs + sec->reloc_count;
  626. for (irel = internal_relocs; irel < irelend; irel++)
  627. {
  628. bfd_vma symval;
  629. {
  630. arelent bfd_reloc;
  631. elf32_h8_info_to_howto (abfd, &bfd_reloc, irel);
  632. }
  633. /* Keep track of the previous reloc so that we can delete
  634. some long jumps created by the compiler. */
  635. if (irel != internal_relocs)
  636. last_reloc = irel - 1;
  637. switch(ELF32_R_TYPE (irel->r_info))
  638. {
  639. case R_H8_DIR24R8:
  640. case R_H8_PCREL16:
  641. case R_H8_DIR16A8:
  642. case R_H8_DIR24A8:
  643. case R_H8_DIR32A16:
  644. case R_H8_DISP32A16:
  645. break;
  646. default:
  647. continue;
  648. }
  649. /* Get the section contents if we haven't done so already. */
  650. if (contents == NULL)
  651. {
  652. /* Get cached copy if it exists. */
  653. if (elf_section_data (sec)->this_hdr.contents != NULL)
  654. contents = elf_section_data (sec)->this_hdr.contents;
  655. else
  656. {
  657. /* Go get them off disk. */
  658. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  659. goto error_return;
  660. }
  661. }
  662. /* Read this BFD's local symbols if we haven't done so already. */
  663. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  664. {
  665. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  666. if (isymbuf == NULL)
  667. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  668. symtab_hdr->sh_info, 0,
  669. NULL, NULL, NULL);
  670. if (isymbuf == NULL)
  671. goto error_return;
  672. }
  673. /* Get the value of the symbol referred to by the reloc. */
  674. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  675. {
  676. /* A local symbol. */
  677. Elf_Internal_Sym *isym;
  678. asection *sym_sec;
  679. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  680. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  681. symval = isym->st_value;
  682. /* If the reloc is absolute, it will not have
  683. a symbol or section associated with it. */
  684. if (sym_sec)
  685. symval += sym_sec->output_section->vma
  686. + sym_sec->output_offset;
  687. }
  688. else
  689. {
  690. unsigned long indx;
  691. struct elf_link_hash_entry *h;
  692. /* An external symbol. */
  693. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  694. h = elf_sym_hashes (abfd)[indx];
  695. BFD_ASSERT (h != NULL);
  696. if (h->root.type != bfd_link_hash_defined
  697. && h->root.type != bfd_link_hash_defweak)
  698. {
  699. /* This appears to be a reference to an undefined
  700. symbol. Just ignore it--it will be caught by the
  701. regular reloc processing. */
  702. continue;
  703. }
  704. symval = (h->root.u.def.value
  705. + h->root.u.def.section->output_section->vma
  706. + h->root.u.def.section->output_offset);
  707. }
  708. /* For simplicity of coding, we are going to modify the section
  709. contents, the section relocs, and the BFD symbol table. We
  710. must tell the rest of the code not to free up this
  711. information. It would be possible to instead create a table
  712. of changes which have to be made, as is done in coff-mips.c;
  713. that would be more work, but would require less memory when
  714. the linker is run. */
  715. switch (ELF32_R_TYPE (irel->r_info))
  716. {
  717. /* Try to turn a 24-bit absolute branch/call into an 8-bit
  718. pc-relative branch/call. */
  719. case R_H8_DIR24R8:
  720. {
  721. bfd_vma value = symval + irel->r_addend;
  722. bfd_vma dot, gap;
  723. /* Get the address of this instruction. */
  724. dot = (sec->output_section->vma
  725. + sec->output_offset + irel->r_offset - 1);
  726. /* Compute the distance from this insn to the branch target. */
  727. gap = value - dot;
  728. /* If the distance is within -126..+130 inclusive, then we can
  729. relax this jump. +130 is valid since the target will move
  730. two bytes closer if we do relax this branch. */
  731. if ((int) gap >= -126 && (int) gap <= 130)
  732. {
  733. unsigned char code;
  734. /* Note that we've changed the relocs, section contents,
  735. etc. */
  736. elf_section_data (sec)->relocs = internal_relocs;
  737. elf_section_data (sec)->this_hdr.contents = contents;
  738. symtab_hdr->contents = (unsigned char *) isymbuf;
  739. /* Get the instruction code being relaxed. */
  740. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  741. /* If the previous instruction conditionally jumped around
  742. this instruction, we may be able to reverse the condition
  743. and redirect the previous instruction to the target of
  744. this instruction.
  745. Such sequences are used by the compiler to deal with
  746. long conditional branches.
  747. Only perform this optimisation for jumps (code 0x5a) not
  748. subroutine calls, as otherwise it could transform:
  749. mov.w r0,r0
  750. beq .L1
  751. jsr @_bar
  752. .L1: rts
  753. _bar: rts
  754. into:
  755. mov.w r0,r0
  756. bne _bar
  757. rts
  758. _bar: rts
  759. which changes the call (jsr) into a branch (bne). */
  760. if (code == 0x5a /* jmp24. */
  761. && (int) gap <= 130
  762. && (int) gap >= -128
  763. && last_reloc
  764. && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8
  765. && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info)
  766. {
  767. bfd_vma last_value;
  768. asection *last_sym_sec;
  769. Elf_Internal_Sym *last_sym;
  770. /* We will need to examine the symbol used by the
  771. previous relocation. */
  772. last_sym = isymbuf + ELF32_R_SYM (last_reloc->r_info);
  773. last_sym_sec
  774. = bfd_section_from_elf_index (abfd, last_sym->st_shndx);
  775. last_value = (last_sym->st_value
  776. + last_sym_sec->output_section->vma
  777. + last_sym_sec->output_offset);
  778. /* Verify that the previous relocation was for a
  779. branch around this instruction and that no symbol
  780. exists at the current location. */
  781. if (last_value == dot + 4
  782. && last_reloc->r_offset + 2 == irel->r_offset
  783. && ! elf32_h8_symbol_address_p (abfd, sec, dot))
  784. {
  785. /* We can eliminate this jump. Twiddle the
  786. previous relocation as necessary. */
  787. irel->r_info
  788. = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  789. ELF32_R_TYPE (R_H8_NONE));
  790. last_reloc->r_info
  791. = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  792. ELF32_R_TYPE (R_H8_PCREL8));
  793. last_reloc->r_addend = irel->r_addend;
  794. code = bfd_get_8 (abfd,
  795. contents + last_reloc->r_offset - 1);
  796. code ^= 1;
  797. bfd_put_8 (abfd,
  798. code,
  799. contents + last_reloc->r_offset - 1);
  800. /* Delete four bytes of data. */
  801. if (!elf32_h8_relax_delete_bytes (abfd, sec,
  802. irel->r_offset - 1,
  803. 4))
  804. goto error_return;
  805. *again = TRUE;
  806. break;
  807. }
  808. }
  809. if (code == 0x5e)
  810. /* This is jsr24 */
  811. bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1); /* bsr8. */
  812. else if (code == 0x5a)
  813. /* This is jmp24 */
  814. bfd_put_8 (abfd, 0x40, contents + irel->r_offset - 1); /* bra8. */
  815. else
  816. abort ();
  817. /* Fix the relocation's type. */
  818. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  819. R_H8_PCREL8);
  820. /* Delete two bytes of data. */
  821. if (!elf32_h8_relax_delete_bytes (abfd, sec,
  822. irel->r_offset + 1, 2))
  823. goto error_return;
  824. /* That will change things, so, we should relax again.
  825. Note that this is not required, and it may be slow. */
  826. *again = TRUE;
  827. }
  828. break;
  829. }
  830. /* Try to turn a 16-bit pc-relative branch into a 8-bit pc-relative
  831. branch. */
  832. case R_H8_PCREL16:
  833. {
  834. bfd_vma value = symval + irel->r_addend;
  835. bfd_vma dot;
  836. bfd_vma gap;
  837. /* Get the address of this instruction. */
  838. dot = (sec->output_section->vma
  839. + sec->output_offset
  840. + irel->r_offset - 2);
  841. gap = value - dot;
  842. /* If the distance is within -126..+130 inclusive, then we can
  843. relax this jump. +130 is valid since the target will move
  844. two bytes closer if we do relax this branch. */
  845. if ((int) gap >= -126 && (int) gap <= 130)
  846. {
  847. unsigned char code;
  848. /* Note that we've changed the relocs, section contents,
  849. etc. */
  850. elf_section_data (sec)->relocs = internal_relocs;
  851. elf_section_data (sec)->this_hdr.contents = contents;
  852. symtab_hdr->contents = (unsigned char *) isymbuf;
  853. /* Get the opcode. */
  854. code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
  855. if (code == 0x58)
  856. {
  857. /* bCC:16 -> bCC:8 */
  858. /* Get the second byte of the original insn, which
  859. contains the condition code. */
  860. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  861. /* Compute the first byte of the relaxed
  862. instruction. The original sequence 0x58 0xX0
  863. is relaxed to 0x4X, where X represents the
  864. condition code. */
  865. code &= 0xf0;
  866. code >>= 4;
  867. code |= 0x40;
  868. bfd_put_8 (abfd, code, contents + irel->r_offset - 2); /* bCC:8. */
  869. }
  870. else if (code == 0x5c) /* bsr16. */
  871. /* This is bsr. */
  872. bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2); /* bsr8. */
  873. else
  874. /* Might be MOVSD. */
  875. break;
  876. /* Fix the relocation's type. */
  877. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  878. R_H8_PCREL8);
  879. irel->r_offset--;
  880. /* Delete two bytes of data. */
  881. if (!elf32_h8_relax_delete_bytes (abfd, sec,
  882. irel->r_offset + 1, 2))
  883. goto error_return;
  884. /* That will change things, so, we should relax again.
  885. Note that this is not required, and it may be slow. */
  886. *again = TRUE;
  887. }
  888. break;
  889. }
  890. /* This is a 16-bit absolute address in one of the following
  891. instructions:
  892. "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
  893. "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
  894. "mov.b"
  895. We may relax this into an 8-bit absolute address if it's in
  896. the right range. */
  897. case R_H8_DIR16A8:
  898. {
  899. bfd_vma value;
  900. value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
  901. if (value >= 0xffffff00u)
  902. {
  903. unsigned char code;
  904. unsigned char temp_code;
  905. /* Note that we've changed the relocs, section contents,
  906. etc. */
  907. elf_section_data (sec)->relocs = internal_relocs;
  908. elf_section_data (sec)->this_hdr.contents = contents;
  909. symtab_hdr->contents = (unsigned char *) isymbuf;
  910. /* Get the opcode. */
  911. code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
  912. /* All instructions with R_H8_DIR16A8 start with
  913. 0x6a. */
  914. if (code != 0x6a)
  915. abort ();
  916. temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  917. /* If this is a mov.b instruction, clear the lower
  918. nibble, which contains the source/destination
  919. register number. */
  920. if ((temp_code & 0x10) != 0x10)
  921. temp_code &= 0xf0;
  922. switch (temp_code)
  923. {
  924. case 0x00:
  925. /* This is mov.b @aa:16,Rd. */
  926. bfd_put_8 (abfd, (code & 0xf) | 0x20,
  927. contents + irel->r_offset - 2);
  928. break;
  929. case 0x80:
  930. /* This is mov.b Rs,@aa:16. */
  931. bfd_put_8 (abfd, (code & 0xf) | 0x30,
  932. contents + irel->r_offset - 2);
  933. break;
  934. case 0x18:
  935. /* This is a bit-maniputation instruction that
  936. stores one bit into memory, one of "bclr",
  937. "bist", "bnot", "bset", and "bst". */
  938. bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2);
  939. break;
  940. case 0x10:
  941. /* This is a bit-maniputation instruction that
  942. loads one bit from memory, one of "band",
  943. "biand", "bild", "bior", "bixor", "bld", "bor",
  944. "btst", and "bxor". */
  945. bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2);
  946. break;
  947. default:
  948. abort ();
  949. }
  950. /* Fix the relocation's type. */
  951. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  952. R_H8_DIR8);
  953. /* Move the relocation. */
  954. irel->r_offset--;
  955. /* Delete two bytes of data. */
  956. if (!elf32_h8_relax_delete_bytes (abfd, sec,
  957. irel->r_offset + 1, 2))
  958. goto error_return;
  959. /* That will change things, so, we should relax again.
  960. Note that this is not required, and it may be slow. */
  961. *again = TRUE;
  962. }
  963. break;
  964. }
  965. /* This is a 24-bit absolute address in one of the following
  966. instructions:
  967. "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
  968. "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
  969. "mov.b"
  970. We may relax this into an 8-bit absolute address if it's in
  971. the right range. */
  972. case R_H8_DIR24A8:
  973. {
  974. bfd_vma value;
  975. value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
  976. if (value >= 0xffffff00u)
  977. {
  978. unsigned char code;
  979. unsigned char temp_code;
  980. /* Note that we've changed the relocs, section contents,
  981. etc. */
  982. elf_section_data (sec)->relocs = internal_relocs;
  983. elf_section_data (sec)->this_hdr.contents = contents;
  984. symtab_hdr->contents = (unsigned char *) isymbuf;
  985. /* Get the opcode. */
  986. code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
  987. /* All instructions with R_H8_DIR24A8 start with
  988. 0x6a. */
  989. if (code != 0x6a)
  990. abort ();
  991. temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  992. /* If this is a mov.b instruction, clear the lower
  993. nibble, which contains the source/destination
  994. register number. */
  995. if ((temp_code & 0x30) != 0x30)
  996. temp_code &= 0xf0;
  997. switch (temp_code)
  998. {
  999. case 0x20:
  1000. /* This is mov.b @aa:24/32,Rd. */
  1001. bfd_put_8 (abfd, (code & 0xf) | 0x20,
  1002. contents + irel->r_offset - 2);
  1003. break;
  1004. case 0xa0:
  1005. /* This is mov.b Rs,@aa:24/32. */
  1006. bfd_put_8 (abfd, (code & 0xf) | 0x30,
  1007. contents + irel->r_offset - 2);
  1008. break;
  1009. case 0x38:
  1010. /* This is a bit-maniputation instruction that
  1011. stores one bit into memory, one of "bclr",
  1012. "bist", "bnot", "bset", and "bst". */
  1013. bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2);
  1014. break;
  1015. case 0x30:
  1016. /* This is a bit-maniputation instruction that
  1017. loads one bit from memory, one of "band",
  1018. "biand", "bild", "bior", "bixor", "bld", "bor",
  1019. "btst", and "bxor". */
  1020. bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2);
  1021. break;
  1022. default:
  1023. abort();
  1024. }
  1025. /* Fix the relocation's type. */
  1026. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1027. R_H8_DIR8);
  1028. irel->r_offset--;
  1029. /* Delete four bytes of data. */
  1030. if (!elf32_h8_relax_delete_bytes (abfd, sec,
  1031. irel->r_offset + 1, 4))
  1032. goto error_return;
  1033. /* That will change things, so, we should relax again.
  1034. Note that this is not required, and it may be slow. */
  1035. *again = TRUE;
  1036. break;
  1037. }
  1038. }
  1039. /* Fall through. */
  1040. /* This is a 24-/32-bit absolute address in one of the
  1041. following instructions:
  1042. "band", "bclr", "biand", "bild", "bior", "bist",
  1043. "bixor", "bld", "bnot", "bor", "bset", "bst", "btst",
  1044. "bxor", "ldc.w", "stc.w" and "mov.[bwl]"
  1045. We may relax this into an 16-bit absolute address if it's
  1046. in the right range. */
  1047. case R_H8_DIR32A16:
  1048. {
  1049. bfd_vma value;
  1050. value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
  1051. if (value <= 0x7fff || value >= 0xffff8000u)
  1052. {
  1053. unsigned char code;
  1054. unsigned char op0, op1, op2, op3;
  1055. unsigned char *op_ptr;
  1056. /* Note that we've changed the relocs, section contents,
  1057. etc. */
  1058. elf_section_data (sec)->relocs = internal_relocs;
  1059. elf_section_data (sec)->this_hdr.contents = contents;
  1060. symtab_hdr->contents = (unsigned char *) isymbuf;
  1061. if (irel->r_offset >= 4)
  1062. {
  1063. /* Check for 4-byte MOVA relaxation (SH-specific). */
  1064. int second_reloc = 0;
  1065. op_ptr = contents + irel->r_offset - 4;
  1066. if (last_reloc)
  1067. {
  1068. arelent bfd_reloc;
  1069. reloc_howto_type *h;
  1070. bfd_vma last_reloc_size;
  1071. elf32_h8_info_to_howto (abfd, &bfd_reloc, last_reloc);
  1072. h = bfd_reloc.howto;
  1073. last_reloc_size = 1 << h->size;
  1074. if (last_reloc->r_offset + last_reloc_size
  1075. == irel->r_offset)
  1076. {
  1077. op_ptr -= last_reloc_size;
  1078. second_reloc = 1;
  1079. }
  1080. }
  1081. if (irel + 1 < irelend)
  1082. {
  1083. Elf_Internal_Rela *next_reloc = irel + 1;
  1084. arelent bfd_reloc;
  1085. reloc_howto_type *h;
  1086. bfd_vma next_reloc_size;
  1087. elf32_h8_info_to_howto (abfd, &bfd_reloc, next_reloc);
  1088. h = bfd_reloc.howto;
  1089. next_reloc_size = 1 << h->size;
  1090. if (next_reloc->r_offset + next_reloc_size
  1091. == irel->r_offset)
  1092. {
  1093. op_ptr -= next_reloc_size;
  1094. second_reloc = 1;
  1095. }
  1096. }
  1097. op0 = bfd_get_8 (abfd, op_ptr + 0);
  1098. op1 = bfd_get_8 (abfd, op_ptr + 1);
  1099. op2 = bfd_get_8 (abfd, op_ptr + 2);
  1100. op3 = bfd_get_8 (abfd, op_ptr + 3);
  1101. if (op0 == 0x01
  1102. && (op1 & 0xdf) == 0x5f
  1103. && (op2 & 0x40) == 0x40
  1104. && (op3 & 0x80) == 0x80)
  1105. {
  1106. if ((op2 & 0x08) == 0)
  1107. second_reloc = 1;
  1108. if (second_reloc)
  1109. {
  1110. op3 &= ~0x08;
  1111. bfd_put_8 (abfd, op3, op_ptr + 3);
  1112. }
  1113. else
  1114. {
  1115. op2 &= ~0x08;
  1116. bfd_put_8 (abfd, op2, op_ptr + 2);
  1117. }
  1118. goto r_h8_dir32a16_common;
  1119. }
  1120. }
  1121. /* Now check for short version of MOVA. (SH-specific) */
  1122. op_ptr = contents + irel->r_offset - 2;
  1123. op0 = bfd_get_8 (abfd, op_ptr + 0);
  1124. op1 = bfd_get_8 (abfd, op_ptr + 1);
  1125. if (op0 == 0x7a
  1126. && (op1 & 0x88) == 0x80)
  1127. {
  1128. op1 |= 0x08;
  1129. bfd_put_8 (abfd, op1, op_ptr + 1);
  1130. goto r_h8_dir32a16_common;
  1131. }
  1132. /* Get the opcode. */
  1133. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  1134. /* Fix the opcode. For all the instructions that
  1135. belong to this relaxation, we simply need to turn
  1136. off bit 0x20 in the previous byte. */
  1137. code &= ~0x20;
  1138. bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
  1139. r_h8_dir32a16_common:
  1140. /* Fix the relocation's type. */
  1141. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1142. R_H8_DIR16);
  1143. /* Delete two bytes of data. */
  1144. if (!elf32_h8_relax_delete_bytes (abfd, sec,
  1145. irel->r_offset + 1, 2))
  1146. goto error_return;
  1147. /* That will change things, so, we should relax again.
  1148. Note that this is not required, and it may be slow. */
  1149. *again = TRUE;
  1150. }
  1151. break; /* case R_H8_DIR32A16 */
  1152. }
  1153. case R_H8_DISP32A16:
  1154. /* mov.[bwl] @(displ:24/32+ERx) -> mov.[bwl] @(displ:16+ERx) 4 bytes
  1155. It is assured that instruction uses at least 4 bytes opcode before
  1156. reloc entry addressing mode "register indirect with displacement"
  1157. relaxing options (all saving 4 bytes):
  1158. 0x78 0sss0000 0x6A 0010dddd disp:32 mov.b @(d:32,ERs),Rd ->
  1159. 0x6E 0sssdddd disp:16 mov.b @(d:16,ERs),Rd
  1160. 0x78 0sss0000 0x6B 0010dddd disp:32 mov.w @(d:32,ERs),Rd ->
  1161. 0x6F 0sssdddd disp:16 mov.w @(d:16,ERs),Rd
  1162. 0x01 0x00 0x78 0sss0000 0x6B 00100ddd disp:32 mov.l @(d:32,ERs),ERd ->
  1163. 0x01 0x00 0x6F 0sss0ddd disp:16 mov.l @(d:16,ERs),ERd
  1164. 0x78 0ddd0000 0x6A 1010ssss disp:32 mov.b Rs,@(d:32,ERd) ->
  1165. 0x6E 1dddssss disp:16 mov.b Rs,@(d:16,ERd)
  1166. 0x78 0ddd0000 0x6B 1010ssss disp:32 mov.w Rs,@(d:32,ERd) ->
  1167. 0x6F 1dddssss disp:16 mov.w Rs,@(d:16,ERd)
  1168. 0x01 0x00 0x78 xddd0000 0x6B 10100sss disp:32 mov.l ERs,@(d:32,ERd) ->
  1169. 0x01 0x00 0x6F 1ddd0sss disp:16 mov.l ERs,@(d:16,ERd)
  1170. mov.l prefix 0x01 0x00 can be left as is and mov.l handled same
  1171. as mov.w/ */
  1172. {
  1173. bfd_vma value;
  1174. value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
  1175. if (value <= 0x7fff || value >= 0xffff8000u)
  1176. {
  1177. unsigned char op0, op1, op2, op3, op0n, op1n;
  1178. int relax = 0;
  1179. /* Note that we've changed the relocs, section contents,
  1180. etc. */
  1181. elf_section_data (sec)->relocs = internal_relocs;
  1182. elf_section_data (sec)->this_hdr.contents = contents;
  1183. symtab_hdr->contents = (unsigned char *) isymbuf;
  1184. if (irel->r_offset >= 4)
  1185. {
  1186. op0 = bfd_get_8 (abfd, contents + irel->r_offset - 4);
  1187. op1 = bfd_get_8 (abfd, contents + irel->r_offset - 3);
  1188. op2 = bfd_get_8 (abfd, contents + irel->r_offset - 2);
  1189. op3 = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  1190. if (op0 == 0x78)
  1191. {
  1192. switch(op2)
  1193. {
  1194. case 0x6A:
  1195. if ((op1 & 0x8F) == 0x00 && (op3 & 0x70) == 0x20)
  1196. {
  1197. /* mov.b. */
  1198. op0n = 0x6E;
  1199. relax = 1;
  1200. }
  1201. break;
  1202. case 0x6B:
  1203. if ((op1 & 0x0F) == 0x00 && (op3 & 0x70) == 0x20)
  1204. {
  1205. /* mov.w/l. */
  1206. op0n = 0x6F;
  1207. relax = 1;
  1208. }
  1209. break;
  1210. default:
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. if (relax)
  1216. {
  1217. op1n = (op3 & 0x8F) | (op1 & 0x70);
  1218. bfd_put_8 (abfd, op0n, contents + irel->r_offset - 4);
  1219. bfd_put_8 (abfd, op1n, contents + irel->r_offset - 3);
  1220. /* Fix the relocation's type. */
  1221. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_H8_DIR16);
  1222. irel->r_offset -= 2;
  1223. /* Delete four bytes of data. */
  1224. if (!elf32_h8_relax_delete_bytes (abfd, sec, irel->r_offset + 2, 4))
  1225. goto error_return;
  1226. /* That will change things, so, we should relax again.
  1227. Note that this is not required, and it may be slow. */
  1228. *again = TRUE;
  1229. }
  1230. }
  1231. }
  1232. break;
  1233. default:
  1234. break;
  1235. }
  1236. }
  1237. if (isymbuf != NULL
  1238. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1239. {
  1240. if (! link_info->keep_memory)
  1241. free (isymbuf);
  1242. else
  1243. symtab_hdr->contents = (unsigned char *) isymbuf;
  1244. }
  1245. if (contents != NULL
  1246. && elf_section_data (sec)->this_hdr.contents != contents)
  1247. {
  1248. if (! link_info->keep_memory)
  1249. free (contents);
  1250. else
  1251. {
  1252. /* Cache the section contents for elf_link_input_bfd. */
  1253. elf_section_data (sec)->this_hdr.contents = contents;
  1254. }
  1255. }
  1256. if (internal_relocs != NULL
  1257. && elf_section_data (sec)->relocs != internal_relocs)
  1258. free (internal_relocs);
  1259. return TRUE;
  1260. error_return:
  1261. if (isymbuf != NULL
  1262. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1263. free (isymbuf);
  1264. if (contents != NULL
  1265. && elf_section_data (sec)->this_hdr.contents != contents)
  1266. free (contents);
  1267. if (internal_relocs != NULL
  1268. && elf_section_data (sec)->relocs != internal_relocs)
  1269. free (internal_relocs);
  1270. return FALSE;
  1271. }
  1272. /* Delete some bytes from a section while relaxing. */
  1273. static bfd_boolean
  1274. elf32_h8_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count)
  1275. {
  1276. Elf_Internal_Shdr *symtab_hdr;
  1277. unsigned int sec_shndx;
  1278. bfd_byte *contents;
  1279. Elf_Internal_Rela *irel, *irelend;
  1280. Elf_Internal_Sym *isym;
  1281. Elf_Internal_Sym *isymend;
  1282. bfd_vma toaddr;
  1283. struct elf_link_hash_entry **sym_hashes;
  1284. struct elf_link_hash_entry **end_hashes;
  1285. unsigned int symcount;
  1286. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1287. contents = elf_section_data (sec)->this_hdr.contents;
  1288. toaddr = sec->size;
  1289. irel = elf_section_data (sec)->relocs;
  1290. irelend = irel + sec->reloc_count;
  1291. /* Actually delete the bytes. */
  1292. memmove (contents + addr, contents + addr + count,
  1293. (size_t) (toaddr - addr - count));
  1294. sec->size -= count;
  1295. /* Adjust all the relocs. */
  1296. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  1297. {
  1298. /* Get the new reloc address. */
  1299. if ((irel->r_offset > addr
  1300. && irel->r_offset <= toaddr))
  1301. irel->r_offset -= count;
  1302. }
  1303. /* Adjust the local symbols defined in this section. */
  1304. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1305. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1306. isymend = isym + symtab_hdr->sh_info;
  1307. for (; isym < isymend; isym++)
  1308. {
  1309. if (isym->st_shndx == sec_shndx
  1310. && isym->st_value > addr
  1311. && isym->st_value <= toaddr)
  1312. isym->st_value -= count;
  1313. }
  1314. /* Now adjust the global symbols defined in this section. */
  1315. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1316. - symtab_hdr->sh_info);
  1317. sym_hashes = elf_sym_hashes (abfd);
  1318. end_hashes = sym_hashes + symcount;
  1319. for (; sym_hashes < end_hashes; sym_hashes++)
  1320. {
  1321. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1322. if ((sym_hash->root.type == bfd_link_hash_defined
  1323. || sym_hash->root.type == bfd_link_hash_defweak)
  1324. && sym_hash->root.u.def.section == sec
  1325. && sym_hash->root.u.def.value > addr
  1326. && sym_hash->root.u.def.value <= toaddr)
  1327. sym_hash->root.u.def.value -= count;
  1328. }
  1329. return TRUE;
  1330. }
  1331. /* Return TRUE if a symbol exists at the given address, else return
  1332. FALSE. */
  1333. static bfd_boolean
  1334. elf32_h8_symbol_address_p (bfd *abfd, asection *sec, bfd_vma addr)
  1335. {
  1336. Elf_Internal_Shdr *symtab_hdr;
  1337. unsigned int sec_shndx;
  1338. Elf_Internal_Sym *isym;
  1339. Elf_Internal_Sym *isymend;
  1340. struct elf_link_hash_entry **sym_hashes;
  1341. struct elf_link_hash_entry **end_hashes;
  1342. unsigned int symcount;
  1343. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1344. /* Examine all the symbols. */
  1345. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1346. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1347. isymend = isym + symtab_hdr->sh_info;
  1348. for (; isym < isymend; isym++)
  1349. {
  1350. if (isym->st_shndx == sec_shndx
  1351. && isym->st_value == addr)
  1352. return TRUE;
  1353. }
  1354. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1355. - symtab_hdr->sh_info);
  1356. sym_hashes = elf_sym_hashes (abfd);
  1357. end_hashes = sym_hashes + symcount;
  1358. for (; sym_hashes < end_hashes; sym_hashes++)
  1359. {
  1360. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1361. if ((sym_hash->root.type == bfd_link_hash_defined
  1362. || sym_hash->root.type == bfd_link_hash_defweak)
  1363. && sym_hash->root.u.def.section == sec
  1364. && sym_hash->root.u.def.value == addr)
  1365. return TRUE;
  1366. }
  1367. return FALSE;
  1368. }
  1369. /* This is a version of bfd_generic_get_relocated_section_contents
  1370. which uses elf32_h8_relocate_section. */
  1371. static bfd_byte *
  1372. elf32_h8_get_relocated_section_contents (bfd *output_bfd,
  1373. struct bfd_link_info *link_info,
  1374. struct bfd_link_order *link_order,
  1375. bfd_byte *data,
  1376. bfd_boolean relocatable,
  1377. asymbol **symbols)
  1378. {
  1379. Elf_Internal_Shdr *symtab_hdr;
  1380. asection *input_section = link_order->u.indirect.section;
  1381. bfd *input_bfd = input_section->owner;
  1382. asection **sections = NULL;
  1383. Elf_Internal_Rela *internal_relocs = NULL;
  1384. Elf_Internal_Sym *isymbuf = NULL;
  1385. /* We only need to handle the case of relaxing, or of having a
  1386. particular set of section contents, specially. */
  1387. if (relocatable
  1388. || elf_section_data (input_section)->this_hdr.contents == NULL)
  1389. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  1390. link_order, data,
  1391. relocatable,
  1392. symbols);
  1393. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1394. memcpy (data, elf_section_data (input_section)->this_hdr.contents,
  1395. (size_t) input_section->size);
  1396. if ((input_section->flags & SEC_RELOC) != 0
  1397. && input_section->reloc_count > 0)
  1398. {
  1399. asection **secpp;
  1400. Elf_Internal_Sym *isym, *isymend;
  1401. bfd_size_type amt;
  1402. internal_relocs = (_bfd_elf_link_read_relocs
  1403. (input_bfd, input_section, NULL,
  1404. (Elf_Internal_Rela *) NULL, FALSE));
  1405. if (internal_relocs == NULL)
  1406. goto error_return;
  1407. if (symtab_hdr->sh_info != 0)
  1408. {
  1409. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1410. if (isymbuf == NULL)
  1411. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  1412. symtab_hdr->sh_info, 0,
  1413. NULL, NULL, NULL);
  1414. if (isymbuf == NULL)
  1415. goto error_return;
  1416. }
  1417. amt = symtab_hdr->sh_info;
  1418. amt *= sizeof (asection *);
  1419. sections = (asection **) bfd_malloc (amt);
  1420. if (sections == NULL && amt != 0)
  1421. goto error_return;
  1422. isymend = isymbuf + symtab_hdr->sh_info;
  1423. for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
  1424. {
  1425. asection *isec;
  1426. if (isym->st_shndx == SHN_UNDEF)
  1427. isec = bfd_und_section_ptr;
  1428. else if (isym->st_shndx == SHN_ABS)
  1429. isec = bfd_abs_section_ptr;
  1430. else if (isym->st_shndx == SHN_COMMON)
  1431. isec = bfd_com_section_ptr;
  1432. else
  1433. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  1434. *secpp = isec;
  1435. }
  1436. if (! elf32_h8_relocate_section (output_bfd, link_info, input_bfd,
  1437. input_section, data, internal_relocs,
  1438. isymbuf, sections))
  1439. goto error_return;
  1440. if (sections != NULL)
  1441. free (sections);
  1442. if (isymbuf != NULL
  1443. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1444. free (isymbuf);
  1445. if (elf_section_data (input_section)->relocs != internal_relocs)
  1446. free (internal_relocs);
  1447. }
  1448. return data;
  1449. error_return:
  1450. if (sections != NULL)
  1451. free (sections);
  1452. if (isymbuf != NULL
  1453. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1454. free (isymbuf);
  1455. if (internal_relocs != NULL
  1456. && elf_section_data (input_section)->relocs != internal_relocs)
  1457. free (internal_relocs);
  1458. return NULL;
  1459. }
  1460. #define TARGET_BIG_SYM h8300_elf32_vec
  1461. #define TARGET_BIG_NAME "elf32-h8300"
  1462. #define ELF_ARCH bfd_arch_h8300
  1463. #define ELF_MACHINE_CODE EM_H8_300
  1464. #define ELF_MAXPAGESIZE 0x1
  1465. #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
  1466. #define bfd_elf32_bfd_reloc_name_lookup elf32_h8_reloc_name_lookup
  1467. #define elf_info_to_howto elf32_h8_info_to_howto
  1468. #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel
  1469. /* So we can set/examine bits in e_flags to get the specific
  1470. H8 architecture in use. */
  1471. #define elf_backend_final_write_processing \
  1472. elf32_h8_final_write_processing
  1473. #define elf_backend_object_p \
  1474. elf32_h8_object_p
  1475. #define bfd_elf32_bfd_merge_private_bfd_data \
  1476. elf32_h8_merge_private_bfd_data
  1477. /* ??? when elf_backend_relocate_section is not defined, elf32-target.h
  1478. defaults to using _bfd_generic_link_hash_table_create, but
  1479. bfd_elf_size_dynamic_sections uses
  1480. dynobj = elf_hash_table (info)->dynobj;
  1481. and thus requires an elf hash table. */
  1482. #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
  1483. /* Use an H8 specific linker, not the ELF generic linker. */
  1484. #define elf_backend_relocate_section elf32_h8_relocate_section
  1485. #define elf_backend_rela_normal 1
  1486. #define elf_backend_can_gc_sections 1
  1487. /* And relaxing stuff. */
  1488. #define bfd_elf32_bfd_relax_section elf32_h8_relax_section
  1489. #define bfd_elf32_bfd_get_relocated_section_contents \
  1490. elf32_h8_get_relocated_section_contents
  1491. #define elf_symbol_leading_char '_'
  1492. #include "elf32-target.h"
  1493. #undef TARGET_BIG_SYM
  1494. #define TARGET_BIG_SYM h8300_elf32_linux_vec
  1495. #undef TARGET_BIG_NAME
  1496. #define TARGET_BIG_NAME "elf32-h8300-linux"
  1497. #undef elf_symbol_leading_char
  1498. #define elf32_bed elf32_h8300_linux_bed
  1499. #include "elf32-target.h"