elf32-m68hc11.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /* Motorola 68HC11-specific support for 32-bit ELF
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Contributed by Stephane Carrez (stcarrez@nerim.fr)
  4. (Heavily copied from the D10V port by Martin Hunt (hunt@cygnus.com))
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "bfdlink.h"
  21. #include "libbfd.h"
  22. #include "elf-bfd.h"
  23. #include "elf32-m68hc1x.h"
  24. #include "elf/m68hc11.h"
  25. #include "opcode/m68hc11.h"
  26. /* Relocation functions. */
  27. static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
  28. (bfd *, bfd_reloc_code_real_type);
  29. static bool m68hc11_info_to_howto_rel
  30. (bfd *, arelent *, Elf_Internal_Rela *);
  31. /* Trampoline generation. */
  32. static bool m68hc11_elf_size_one_stub
  33. (struct bfd_hash_entry *gen_entry, void *in_arg);
  34. static bool m68hc11_elf_build_one_stub
  35. (struct bfd_hash_entry *gen_entry, void *in_arg);
  36. static struct bfd_link_hash_table* m68hc11_elf_bfd_link_hash_table_create
  37. (bfd* abfd);
  38. /* Linker relaxation. */
  39. static bool m68hc11_elf_relax_section
  40. (bfd *, asection *, struct bfd_link_info *, bool *);
  41. static void m68hc11_elf_relax_delete_bytes
  42. (bfd *, asection *, bfd_vma, int);
  43. static void m68hc11_relax_group
  44. (bfd *, asection *, bfd_byte *, unsigned, unsigned long, unsigned long);
  45. static int compare_reloc (const void *, const void *);
  46. /* Use REL instead of RELA to save space */
  47. #define USE_REL 1
  48. /* The Motorola 68HC11 microcontroller only addresses 64Kb but we also
  49. support a memory bank switching mechanism similar to 68HC12.
  50. We must handle 8 and 16-bit relocations. The 32-bit relocation
  51. are used for debugging sections (DWARF2) to represent a virtual
  52. address.
  53. The 3-bit and 16-bit PC rel relocation is only used by 68HC12. */
  54. static reloc_howto_type elf_m68hc11_howto_table[] = {
  55. /* This reloc does nothing. */
  56. HOWTO (R_M68HC11_NONE, /* type */
  57. 0, /* rightshift */
  58. 3, /* size (0 = byte, 1 = short, 2 = long) */
  59. 0, /* bitsize */
  60. false, /* pc_relative */
  61. 0, /* bitpos */
  62. complain_overflow_dont,/* complain_on_overflow */
  63. bfd_elf_generic_reloc, /* special_function */
  64. "R_M68HC11_NONE", /* name */
  65. false, /* partial_inplace */
  66. 0, /* src_mask */
  67. 0, /* dst_mask */
  68. false), /* pcrel_offset */
  69. /* A 8 bit absolute relocation */
  70. HOWTO (R_M68HC11_8, /* type */
  71. 0, /* rightshift */
  72. 0, /* size (0 = byte, 1 = short, 2 = long) */
  73. 8, /* bitsize */
  74. false, /* pc_relative */
  75. 0, /* bitpos */
  76. complain_overflow_bitfield, /* complain_on_overflow */
  77. bfd_elf_generic_reloc, /* special_function */
  78. "R_M68HC11_8", /* name */
  79. false, /* partial_inplace */
  80. 0x00ff, /* src_mask */
  81. 0x00ff, /* dst_mask */
  82. false), /* pcrel_offset */
  83. /* A 8 bit absolute relocation (upper address) */
  84. HOWTO (R_M68HC11_HI8, /* type */
  85. 8, /* rightshift */
  86. 0, /* size (0 = byte, 1 = short, 2 = long) */
  87. 8, /* bitsize */
  88. false, /* pc_relative */
  89. 0, /* bitpos */
  90. complain_overflow_bitfield, /* complain_on_overflow */
  91. bfd_elf_generic_reloc, /* special_function */
  92. "R_M68HC11_HI8", /* name */
  93. false, /* partial_inplace */
  94. 0x00ff, /* src_mask */
  95. 0x00ff, /* dst_mask */
  96. false), /* pcrel_offset */
  97. /* A 8 bit absolute relocation (upper address) */
  98. HOWTO (R_M68HC11_LO8, /* type */
  99. 0, /* rightshift */
  100. 0, /* size (0 = byte, 1 = short, 2 = long) */
  101. 8, /* bitsize */
  102. false, /* pc_relative */
  103. 0, /* bitpos */
  104. complain_overflow_dont, /* complain_on_overflow */
  105. bfd_elf_generic_reloc, /* special_function */
  106. "R_M68HC11_LO8", /* name */
  107. false, /* partial_inplace */
  108. 0x00ff, /* src_mask */
  109. 0x00ff, /* dst_mask */
  110. false), /* pcrel_offset */
  111. /* A 8 bit PC-rel relocation */
  112. HOWTO (R_M68HC11_PCREL_8, /* type */
  113. 0, /* rightshift */
  114. 0, /* size (0 = byte, 1 = short, 2 = long) */
  115. 8, /* bitsize */
  116. true, /* pc_relative */
  117. 0, /* bitpos */
  118. complain_overflow_bitfield, /* complain_on_overflow */
  119. bfd_elf_generic_reloc, /* special_function */
  120. "R_M68HC11_PCREL_8", /* name */
  121. false, /* partial_inplace */
  122. 0x00ff, /* src_mask */
  123. 0x00ff, /* dst_mask */
  124. true), /* pcrel_offset */
  125. /* A 16 bit absolute relocation */
  126. HOWTO (R_M68HC11_16, /* type */
  127. 0, /* rightshift */
  128. 1, /* size (0 = byte, 1 = short, 2 = long) */
  129. 16, /* bitsize */
  130. false, /* pc_relative */
  131. 0, /* bitpos */
  132. complain_overflow_dont /*bitfield */ , /* complain_on_overflow */
  133. bfd_elf_generic_reloc, /* special_function */
  134. "R_M68HC11_16", /* name */
  135. false, /* partial_inplace */
  136. 0xffff, /* src_mask */
  137. 0xffff, /* dst_mask */
  138. false), /* pcrel_offset */
  139. /* A 32 bit absolute relocation. This one is never used for the
  140. code relocation. It's used by gas for -gstabs generation. */
  141. HOWTO (R_M68HC11_32, /* type */
  142. 0, /* rightshift */
  143. 2, /* size (0 = byte, 1 = short, 2 = long) */
  144. 32, /* bitsize */
  145. false, /* pc_relative */
  146. 0, /* bitpos */
  147. complain_overflow_bitfield, /* complain_on_overflow */
  148. bfd_elf_generic_reloc, /* special_function */
  149. "R_M68HC11_32", /* name */
  150. false, /* partial_inplace */
  151. 0xffffffff, /* src_mask */
  152. 0xffffffff, /* dst_mask */
  153. false), /* pcrel_offset */
  154. /* A 3 bit absolute relocation */
  155. HOWTO (R_M68HC11_3B, /* type */
  156. 0, /* rightshift */
  157. 0, /* size (0 = byte, 1 = short, 2 = long) */
  158. 3, /* bitsize */
  159. false, /* pc_relative */
  160. 0, /* bitpos */
  161. complain_overflow_bitfield, /* complain_on_overflow */
  162. bfd_elf_generic_reloc, /* special_function */
  163. "R_M68HC11_4B", /* name */
  164. false, /* partial_inplace */
  165. 0x003, /* src_mask */
  166. 0x003, /* dst_mask */
  167. false), /* pcrel_offset */
  168. /* A 16 bit PC-rel relocation */
  169. HOWTO (R_M68HC11_PCREL_16, /* type */
  170. 0, /* rightshift */
  171. 1, /* size (0 = byte, 1 = short, 2 = long) */
  172. 16, /* bitsize */
  173. true, /* pc_relative */
  174. 0, /* bitpos */
  175. complain_overflow_dont, /* complain_on_overflow */
  176. bfd_elf_generic_reloc, /* special_function */
  177. "R_M68HC11_PCREL_16", /* name */
  178. false, /* partial_inplace */
  179. 0xffff, /* src_mask */
  180. 0xffff, /* dst_mask */
  181. true), /* pcrel_offset */
  182. /* GNU extension to record C++ vtable hierarchy */
  183. HOWTO (R_M68HC11_GNU_VTINHERIT, /* type */
  184. 0, /* rightshift */
  185. 1, /* size (0 = byte, 1 = short, 2 = long) */
  186. 0, /* bitsize */
  187. false, /* pc_relative */
  188. 0, /* bitpos */
  189. complain_overflow_dont, /* complain_on_overflow */
  190. NULL, /* special_function */
  191. "R_M68HC11_GNU_VTINHERIT", /* name */
  192. false, /* partial_inplace */
  193. 0, /* src_mask */
  194. 0, /* dst_mask */
  195. false), /* pcrel_offset */
  196. /* GNU extension to record C++ vtable member usage */
  197. HOWTO (R_M68HC11_GNU_VTENTRY, /* type */
  198. 0, /* rightshift */
  199. 1, /* size (0 = byte, 1 = short, 2 = long) */
  200. 0, /* bitsize */
  201. false, /* pc_relative */
  202. 0, /* bitpos */
  203. complain_overflow_dont, /* complain_on_overflow */
  204. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  205. "R_M68HC11_GNU_VTENTRY", /* name */
  206. false, /* partial_inplace */
  207. 0, /* src_mask */
  208. 0, /* dst_mask */
  209. false), /* pcrel_offset */
  210. /* A 24 bit relocation */
  211. HOWTO (R_M68HC11_24, /* type */
  212. 0, /* rightshift */
  213. 1, /* size (0 = byte, 1 = short, 2 = long) */
  214. 24, /* bitsize */
  215. false, /* pc_relative */
  216. 0, /* bitpos */
  217. complain_overflow_bitfield, /* complain_on_overflow */
  218. bfd_elf_generic_reloc, /* special_function */
  219. "R_M68HC11_24", /* name */
  220. false, /* partial_inplace */
  221. 0xffffff, /* src_mask */
  222. 0xffffff, /* dst_mask */
  223. false), /* pcrel_offset */
  224. /* A 16-bit low relocation */
  225. HOWTO (R_M68HC11_LO16, /* type */
  226. 0, /* rightshift */
  227. 1, /* size (0 = byte, 1 = short, 2 = long) */
  228. 16, /* bitsize */
  229. false, /* pc_relative */
  230. 0, /* bitpos */
  231. complain_overflow_bitfield, /* complain_on_overflow */
  232. bfd_elf_generic_reloc, /* special_function */
  233. "R_M68HC11_LO16", /* name */
  234. false, /* partial_inplace */
  235. 0xffff, /* src_mask */
  236. 0xffff, /* dst_mask */
  237. false), /* pcrel_offset */
  238. /* A page relocation */
  239. HOWTO (R_M68HC11_PAGE, /* type */
  240. 0, /* rightshift */
  241. 0, /* size (0 = byte, 1 = short, 2 = long) */
  242. 8, /* bitsize */
  243. false, /* pc_relative */
  244. 0, /* bitpos */
  245. complain_overflow_bitfield, /* complain_on_overflow */
  246. bfd_elf_generic_reloc, /* special_function */
  247. "R_M68HC11_PAGE", /* name */
  248. false, /* partial_inplace */
  249. 0x00ff, /* src_mask */
  250. 0x00ff, /* dst_mask */
  251. false), /* pcrel_offset */
  252. EMPTY_HOWTO (14),
  253. EMPTY_HOWTO (15),
  254. EMPTY_HOWTO (16),
  255. EMPTY_HOWTO (17),
  256. EMPTY_HOWTO (18),
  257. EMPTY_HOWTO (19),
  258. /* Mark beginning of a jump instruction (any form). */
  259. HOWTO (R_M68HC11_RL_JUMP, /* type */
  260. 0, /* rightshift */
  261. 1, /* size (0 = byte, 1 = short, 2 = long) */
  262. 0, /* bitsize */
  263. false, /* pc_relative */
  264. 0, /* bitpos */
  265. complain_overflow_dont, /* complain_on_overflow */
  266. m68hc11_elf_ignore_reloc, /* special_function */
  267. "R_M68HC11_RL_JUMP", /* name */
  268. true, /* partial_inplace */
  269. 0, /* src_mask */
  270. 0, /* dst_mask */
  271. true), /* pcrel_offset */
  272. /* Mark beginning of Gcc relaxation group instruction. */
  273. HOWTO (R_M68HC11_RL_GROUP, /* type */
  274. 0, /* rightshift */
  275. 1, /* size (0 = byte, 1 = short, 2 = long) */
  276. 0, /* bitsize */
  277. false, /* pc_relative */
  278. 0, /* bitpos */
  279. complain_overflow_dont, /* complain_on_overflow */
  280. m68hc11_elf_ignore_reloc, /* special_function */
  281. "R_M68HC11_RL_GROUP", /* name */
  282. true, /* partial_inplace */
  283. 0, /* src_mask */
  284. 0, /* dst_mask */
  285. true), /* pcrel_offset */
  286. };
  287. /* Map BFD reloc types to M68HC11 ELF reloc types. */
  288. struct m68hc11_reloc_map
  289. {
  290. bfd_reloc_code_real_type bfd_reloc_val;
  291. unsigned char elf_reloc_val;
  292. };
  293. static const struct m68hc11_reloc_map m68hc11_reloc_map[] = {
  294. {BFD_RELOC_NONE, R_M68HC11_NONE,},
  295. {BFD_RELOC_8, R_M68HC11_8},
  296. {BFD_RELOC_M68HC11_HI8, R_M68HC11_HI8},
  297. {BFD_RELOC_M68HC11_LO8, R_M68HC11_LO8},
  298. {BFD_RELOC_8_PCREL, R_M68HC11_PCREL_8},
  299. {BFD_RELOC_16_PCREL, R_M68HC11_PCREL_16},
  300. {BFD_RELOC_16, R_M68HC11_16},
  301. {BFD_RELOC_32, R_M68HC11_32},
  302. {BFD_RELOC_M68HC11_3B, R_M68HC11_3B},
  303. {BFD_RELOC_VTABLE_INHERIT, R_M68HC11_GNU_VTINHERIT},
  304. {BFD_RELOC_VTABLE_ENTRY, R_M68HC11_GNU_VTENTRY},
  305. {BFD_RELOC_M68HC11_LO16, R_M68HC11_LO16},
  306. {BFD_RELOC_M68HC11_PAGE, R_M68HC11_PAGE},
  307. {BFD_RELOC_M68HC11_24, R_M68HC11_24},
  308. {BFD_RELOC_M68HC11_RL_JUMP, R_M68HC11_RL_JUMP},
  309. {BFD_RELOC_M68HC11_RL_GROUP, R_M68HC11_RL_GROUP},
  310. };
  311. static reloc_howto_type *
  312. bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  313. bfd_reloc_code_real_type code)
  314. {
  315. unsigned int i;
  316. for (i = 0;
  317. i < sizeof (m68hc11_reloc_map) / sizeof (struct m68hc11_reloc_map);
  318. i++)
  319. {
  320. if (m68hc11_reloc_map[i].bfd_reloc_val == code)
  321. return &elf_m68hc11_howto_table[m68hc11_reloc_map[i].elf_reloc_val];
  322. }
  323. return NULL;
  324. }
  325. static reloc_howto_type *
  326. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  327. const char *r_name)
  328. {
  329. unsigned int i;
  330. for (i = 0;
  331. i < (sizeof (elf_m68hc11_howto_table)
  332. / sizeof (elf_m68hc11_howto_table[0]));
  333. i++)
  334. if (elf_m68hc11_howto_table[i].name != NULL
  335. && strcasecmp (elf_m68hc11_howto_table[i].name, r_name) == 0)
  336. return &elf_m68hc11_howto_table[i];
  337. return NULL;
  338. }
  339. /* Set the howto pointer for an M68HC11 ELF reloc. */
  340. static bool
  341. m68hc11_info_to_howto_rel (bfd *abfd,
  342. arelent *cache_ptr, Elf_Internal_Rela *dst)
  343. {
  344. unsigned int r_type;
  345. r_type = ELF32_R_TYPE (dst->r_info);
  346. if (r_type >= (unsigned int) R_M68HC11_max)
  347. {
  348. /* xgettext:c-format */
  349. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  350. abfd, r_type);
  351. bfd_set_error (bfd_error_bad_value);
  352. return false;
  353. }
  354. cache_ptr->howto = &elf_m68hc11_howto_table[r_type];
  355. return true;
  356. }
  357. /* Far trampoline generation. */
  358. /* Build a 68HC11 trampoline stub. */
  359. static bool
  360. m68hc11_elf_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
  361. {
  362. struct elf32_m68hc11_stub_hash_entry *stub_entry;
  363. struct bfd_link_info *info;
  364. struct m68hc11_elf_link_hash_table *htab;
  365. asection *stub_sec;
  366. bfd *stub_bfd;
  367. bfd_byte *loc;
  368. bfd_vma sym_value, phys_page, phys_addr;
  369. /* Massage our args to the form they really have. */
  370. stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
  371. info = (struct bfd_link_info *) in_arg;
  372. /* Fail if the target section could not be assigned to an output
  373. section. The user should fix his linker script. */
  374. if (stub_entry->target_section->output_section == NULL
  375. && info->non_contiguous_regions)
  376. info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
  377. "Retry without --enable-non-contiguous-regions.\n"),
  378. stub_entry->target_section);
  379. htab = m68hc11_elf_hash_table (info);
  380. if (htab == NULL)
  381. return false;
  382. stub_sec = stub_entry->stub_sec;
  383. /* Make a note of the offset within the stubs for this entry. */
  384. stub_entry->stub_offset = stub_sec->size;
  385. stub_sec->size += 10;
  386. loc = stub_sec->contents + stub_entry->stub_offset;
  387. stub_bfd = stub_sec->owner;
  388. /* Create the trampoline call stub:
  389. pshb
  390. ldab #%page(symbol)
  391. ldy #%addr(symbol)
  392. jmp __trampoline
  393. */
  394. sym_value = (stub_entry->target_value
  395. + stub_entry->target_section->output_offset
  396. + stub_entry->target_section->output_section->vma);
  397. phys_addr = m68hc11_phys_addr (&htab->pinfo, sym_value);
  398. phys_page = m68hc11_phys_page (&htab->pinfo, sym_value);
  399. /* pshb; ldab #%page(sym) */
  400. bfd_put_8 (stub_bfd, 0x37, loc);
  401. bfd_put_8 (stub_bfd, 0xC6, loc + 1);
  402. bfd_put_8 (stub_bfd, phys_page, loc + 2);
  403. loc += 3;
  404. /* ldy #%addr(sym) */
  405. bfd_put_8 (stub_bfd, 0x18, loc);
  406. bfd_put_8 (stub_bfd, 0xCE, loc + 1);
  407. bfd_put_16 (stub_bfd, phys_addr, loc + 2);
  408. loc += 4;
  409. /* jmp __trampoline */
  410. bfd_put_8 (stub_bfd, 0x7E, loc);
  411. bfd_put_16 (stub_bfd, htab->pinfo.trampoline_addr, loc + 1);
  412. return true;
  413. }
  414. /* As above, but don't actually build the stub. Just bump offset so
  415. we know stub section sizes. */
  416. static bool
  417. m68hc11_elf_size_one_stub (struct bfd_hash_entry *gen_entry,
  418. void *in_arg ATTRIBUTE_UNUSED)
  419. {
  420. struct elf32_m68hc11_stub_hash_entry *stub_entry;
  421. /* Massage our args to the form they really have. */
  422. stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
  423. stub_entry->stub_sec->size += 10;
  424. return true;
  425. }
  426. /* Create a 68HC11 ELF linker hash table. */
  427. static struct bfd_link_hash_table *
  428. m68hc11_elf_bfd_link_hash_table_create (bfd *abfd)
  429. {
  430. struct m68hc11_elf_link_hash_table *ret;
  431. ret = m68hc11_elf_hash_table_create (abfd);
  432. if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
  433. return NULL;
  434. ret->size_one_stub = m68hc11_elf_size_one_stub;
  435. ret->build_one_stub = m68hc11_elf_build_one_stub;
  436. return &ret->root.root;
  437. }
  438. /* 68HC11 Linker Relaxation. */
  439. const struct m68hc11_direct_relax
  440. {
  441. const char *name;
  442. unsigned char code;
  443. unsigned char direct_code;
  444. } m68hc11_direct_relax_table[] = {
  445. { "adca", 0xB9, 0x99 },
  446. { "adcb", 0xF9, 0xD9 },
  447. { "adda", 0xBB, 0x9B },
  448. { "addb", 0xFB, 0xDB },
  449. { "addd", 0xF3, 0xD3 },
  450. { "anda", 0xB4, 0x94 },
  451. { "andb", 0xF4, 0xD4 },
  452. { "cmpa", 0xB1, 0x91 },
  453. { "cmpb", 0xF1, 0xD1 },
  454. { "cpd", 0xB3, 0x93 },
  455. { "cpxy", 0xBC, 0x9C },
  456. /* { "cpy", 0xBC, 0x9C }, */
  457. { "eora", 0xB8, 0x98 },
  458. { "eorb", 0xF8, 0xD8 },
  459. { "jsr", 0xBD, 0x9D },
  460. { "ldaa", 0xB6, 0x96 },
  461. { "ldab", 0xF6, 0xD6 },
  462. { "ldd", 0xFC, 0xDC },
  463. { "lds", 0xBE, 0x9E },
  464. { "ldxy", 0xFE, 0xDE },
  465. /* { "ldy", 0xFE, 0xDE },*/
  466. { "oraa", 0xBA, 0x9A },
  467. { "orab", 0xFA, 0xDA },
  468. { "sbca", 0xB2, 0x92 },
  469. { "sbcb", 0xF2, 0xD2 },
  470. { "staa", 0xB7, 0x97 },
  471. { "stab", 0xF7, 0xD7 },
  472. { "std", 0xFD, 0xDD },
  473. { "sts", 0xBF, 0x9F },
  474. { "stxy", 0xFF, 0xDF },
  475. /* { "sty", 0xFF, 0xDF },*/
  476. { "suba", 0xB0, 0x90 },
  477. { "subb", 0xF0, 0xD0 },
  478. { "subd", 0xB3, 0x93 },
  479. { 0, 0, 0 }
  480. };
  481. static const struct m68hc11_direct_relax *
  482. find_relaxable_insn (unsigned char code)
  483. {
  484. int i;
  485. for (i = 0; m68hc11_direct_relax_table[i].name; i++)
  486. if (m68hc11_direct_relax_table[i].code == code)
  487. return &m68hc11_direct_relax_table[i];
  488. return 0;
  489. }
  490. static int
  491. compare_reloc (const void *e1, const void *e2)
  492. {
  493. const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
  494. const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
  495. if (i1->r_offset == i2->r_offset)
  496. return 0;
  497. else
  498. return i1->r_offset < i2->r_offset ? -1 : 1;
  499. }
  500. #define M6811_OP_LDX_IMMEDIATE (0xCE)
  501. static void
  502. m68hc11_relax_group (bfd *abfd, asection *sec, bfd_byte *contents,
  503. unsigned value, unsigned long offset,
  504. unsigned long end_group)
  505. {
  506. unsigned char code;
  507. unsigned long start_offset;
  508. unsigned long ldx_offset = offset;
  509. unsigned long ldx_size;
  510. int can_delete_ldx;
  511. int relax_ldy = 0;
  512. /* First instruction of the relax group must be a
  513. LDX #value or LDY #value. If this is not the case,
  514. ignore the relax group. */
  515. code = bfd_get_8 (abfd, contents + offset);
  516. if (code == 0x18)
  517. {
  518. relax_ldy++;
  519. offset++;
  520. code = bfd_get_8 (abfd, contents + offset);
  521. }
  522. ldx_size = offset - ldx_offset + 3;
  523. offset += 3;
  524. if (code != M6811_OP_LDX_IMMEDIATE || offset >= end_group)
  525. return;
  526. /* We can remove the LDX/LDY only when all bset/brclr instructions
  527. of the relax group have been converted to use direct addressing
  528. mode. */
  529. can_delete_ldx = 1;
  530. while (offset < end_group)
  531. {
  532. unsigned isize;
  533. unsigned new_value;
  534. int bset_use_y;
  535. bset_use_y = 0;
  536. start_offset = offset;
  537. code = bfd_get_8 (abfd, contents + offset);
  538. if (code == 0x18)
  539. {
  540. bset_use_y++;
  541. offset++;
  542. code = bfd_get_8 (abfd, contents + offset);
  543. }
  544. /* Check the instruction and translate to use direct addressing mode. */
  545. switch (code)
  546. {
  547. /* bset */
  548. case 0x1C:
  549. code = 0x14;
  550. isize = 3;
  551. break;
  552. /* brclr */
  553. case 0x1F:
  554. code = 0x13;
  555. isize = 4;
  556. break;
  557. /* brset */
  558. case 0x1E:
  559. code = 0x12;
  560. isize = 4;
  561. break;
  562. /* bclr */
  563. case 0x1D:
  564. code = 0x15;
  565. isize = 3;
  566. break;
  567. /* This instruction is not recognized and we are not
  568. at end of the relax group. Ignore and don't remove
  569. the first LDX (we don't know what it is used for...). */
  570. default:
  571. return;
  572. }
  573. new_value = (unsigned) bfd_get_8 (abfd, contents + offset + 1);
  574. new_value += value;
  575. if ((new_value & 0xff00) == 0 && bset_use_y == relax_ldy)
  576. {
  577. bfd_put_8 (abfd, code, contents + offset);
  578. bfd_put_8 (abfd, new_value, contents + offset + 1);
  579. if (start_offset != offset)
  580. {
  581. m68hc11_elf_relax_delete_bytes (abfd, sec, start_offset,
  582. offset - start_offset);
  583. end_group--;
  584. }
  585. }
  586. else
  587. {
  588. can_delete_ldx = 0;
  589. }
  590. offset = start_offset + isize;
  591. }
  592. if (can_delete_ldx)
  593. {
  594. /* Remove the move instruction (3 or 4 bytes win). */
  595. m68hc11_elf_relax_delete_bytes (abfd, sec, ldx_offset, ldx_size);
  596. }
  597. }
  598. /* This function handles relaxing for the 68HC11.
  599. and somewhat more difficult to support. */
  600. static bool
  601. m68hc11_elf_relax_section (bfd *abfd, asection *sec,
  602. struct bfd_link_info *link_info, bool *again)
  603. {
  604. Elf_Internal_Shdr *symtab_hdr;
  605. Elf_Internal_Rela *internal_relocs;
  606. Elf_Internal_Rela *free_relocs = NULL;
  607. Elf_Internal_Rela *irel, *irelend;
  608. bfd_byte *contents = NULL;
  609. bfd_byte *free_contents = NULL;
  610. Elf32_External_Sym *free_extsyms = NULL;
  611. Elf_Internal_Rela *prev_insn_branch = NULL;
  612. Elf_Internal_Rela *prev_insn_group = NULL;
  613. unsigned insn_group_value = 0;
  614. Elf_Internal_Sym *isymbuf = NULL;
  615. /* Assume nothing changes. */
  616. *again = false;
  617. /* We don't have to do anything for a relocatable link, if
  618. this section does not have relocs, or if this is not a
  619. code section. */
  620. if (bfd_link_relocatable (link_info)
  621. || (sec->flags & SEC_RELOC) == 0
  622. || sec->reloc_count == 0
  623. || (sec->flags & SEC_CODE) == 0)
  624. return true;
  625. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  626. /* Get a copy of the native relocations. */
  627. internal_relocs = (_bfd_elf_link_read_relocs
  628. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  629. link_info->keep_memory));
  630. if (internal_relocs == NULL)
  631. goto error_return;
  632. if (! link_info->keep_memory)
  633. free_relocs = internal_relocs;
  634. /* Checking for branch relaxation relies on the relocations to
  635. be sorted on 'r_offset'. This is not guaranteed so we must sort. */
  636. qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
  637. compare_reloc);
  638. /* Walk through them looking for relaxing opportunities. */
  639. irelend = internal_relocs + sec->reloc_count;
  640. for (irel = internal_relocs; irel < irelend; irel++)
  641. {
  642. bfd_vma symval;
  643. bfd_vma value;
  644. Elf_Internal_Sym *isym;
  645. asection *sym_sec;
  646. int is_far = 0;
  647. /* If this isn't something that can be relaxed, then ignore
  648. this reloc. */
  649. if (ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_16
  650. && ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_RL_JUMP
  651. && ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_RL_GROUP)
  652. {
  653. prev_insn_branch = 0;
  654. prev_insn_group = 0;
  655. continue;
  656. }
  657. /* Get the section contents if we haven't done so already. */
  658. if (contents == NULL)
  659. {
  660. /* Get cached copy if it exists. */
  661. if (elf_section_data (sec)->this_hdr.contents != NULL)
  662. contents = elf_section_data (sec)->this_hdr.contents;
  663. else
  664. {
  665. /* Go get them off disk. */
  666. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  667. goto error_return;
  668. }
  669. }
  670. /* Try to eliminate an unconditional 8 bit pc-relative branch
  671. which immediately follows a conditional 8 bit pc-relative
  672. branch around the unconditional branch.
  673. original: new:
  674. bCC lab1 bCC' lab2
  675. bra lab2
  676. lab1: lab1:
  677. This happens when the bCC can't reach lab2 at assembly time,
  678. but due to other relaxations it can reach at link time. */
  679. if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_RL_JUMP)
  680. {
  681. Elf_Internal_Rela *nrel;
  682. unsigned char code;
  683. unsigned char roffset;
  684. prev_insn_branch = 0;
  685. prev_insn_group = 0;
  686. /* Do nothing if this reloc is the last byte in the section. */
  687. if (irel->r_offset + 2 >= sec->size)
  688. continue;
  689. /* See if the next instruction is an unconditional pc-relative
  690. branch, more often than not this test will fail, so we
  691. test it first to speed things up. */
  692. code = bfd_get_8 (abfd, contents + irel->r_offset + 2);
  693. if (code != 0x7e)
  694. continue;
  695. /* Also make sure the next relocation applies to the next
  696. instruction and that it's a pc-relative 8 bit branch. */
  697. nrel = irel + 1;
  698. if (nrel == irelend
  699. || irel->r_offset + 3 != nrel->r_offset
  700. || ELF32_R_TYPE (nrel->r_info) != (int) R_M68HC11_16)
  701. continue;
  702. /* Make sure our destination immediately follows the
  703. unconditional branch. */
  704. roffset = bfd_get_8 (abfd, contents + irel->r_offset + 1);
  705. if (roffset != 3)
  706. continue;
  707. prev_insn_branch = irel;
  708. prev_insn_group = 0;
  709. continue;
  710. }
  711. /* Read this BFD's symbols if we haven't done so already. */
  712. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  713. {
  714. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  715. if (isymbuf == NULL)
  716. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  717. symtab_hdr->sh_info, 0,
  718. NULL, NULL, NULL);
  719. if (isymbuf == NULL)
  720. goto error_return;
  721. }
  722. /* Get the value of the symbol referred to by the reloc. */
  723. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  724. {
  725. /* A local symbol. */
  726. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  727. is_far = isym->st_other & STO_M68HC12_FAR;
  728. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  729. symval = (isym->st_value
  730. + sym_sec->output_section->vma
  731. + sym_sec->output_offset);
  732. }
  733. else
  734. {
  735. unsigned long indx;
  736. struct elf_link_hash_entry *h;
  737. /* An external symbol. */
  738. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  739. h = elf_sym_hashes (abfd)[indx];
  740. BFD_ASSERT (h != NULL);
  741. if (h->root.type != bfd_link_hash_defined
  742. && h->root.type != bfd_link_hash_defweak)
  743. {
  744. /* This appears to be a reference to an undefined
  745. symbol. Just ignore it--it will be caught by the
  746. regular reloc processing. */
  747. prev_insn_branch = 0;
  748. prev_insn_group = 0;
  749. continue;
  750. }
  751. is_far = h->other & STO_M68HC12_FAR;
  752. isym = 0;
  753. sym_sec = h->root.u.def.section;
  754. symval = (h->root.u.def.value
  755. + sym_sec->output_section->vma
  756. + sym_sec->output_offset);
  757. }
  758. if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_RL_GROUP)
  759. {
  760. prev_insn_branch = 0;
  761. prev_insn_group = 0;
  762. /* Do nothing if this reloc is the last byte in the section. */
  763. if (irel->r_offset == sec->size)
  764. continue;
  765. prev_insn_group = irel;
  766. insn_group_value = isym->st_value;
  767. continue;
  768. }
  769. /* When we relax some bytes, the size of our section changes.
  770. This affects the layout of next input sections that go in our
  771. output section. When the symbol is part of another section that
  772. will go in the same output section as the current one, it's
  773. final address may now be incorrect (too far). We must let the
  774. linker re-compute all section offsets before processing this
  775. reloc. Code example:
  776. Initial Final
  777. .sect .text section size = 6 section size = 4
  778. jmp foo
  779. jmp bar
  780. .sect .text.foo_bar output_offset = 6 output_offset = 4
  781. foo: rts
  782. bar: rts
  783. If we process the reloc now, the jmp bar is replaced by a
  784. relative branch to the initial bar address (output_offset 6). */
  785. if (*again && sym_sec != sec
  786. && sym_sec->output_section == sec->output_section)
  787. {
  788. prev_insn_group = 0;
  789. prev_insn_branch = 0;
  790. continue;
  791. }
  792. value = symval;
  793. /* Try to turn a far branch to a near branch. */
  794. if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_16
  795. && prev_insn_branch)
  796. {
  797. bfd_vma offset;
  798. unsigned char code;
  799. offset = value - (prev_insn_branch->r_offset
  800. + sec->output_section->vma
  801. + sec->output_offset + 2);
  802. /* If the offset is still out of -128..+127 range,
  803. leave that far branch unchanged. */
  804. if ((offset & 0xff80) != 0 && (offset & 0xff80) != 0xff80)
  805. {
  806. prev_insn_branch = 0;
  807. continue;
  808. }
  809. /* Shrink the branch. */
  810. code = bfd_get_8 (abfd, contents + prev_insn_branch->r_offset);
  811. if (code == 0x7e)
  812. {
  813. code = 0x20;
  814. bfd_put_8 (abfd, code, contents + prev_insn_branch->r_offset);
  815. bfd_put_8 (abfd, 0xff,
  816. contents + prev_insn_branch->r_offset + 1);
  817. irel->r_offset = prev_insn_branch->r_offset + 1;
  818. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  819. R_M68HC11_PCREL_8);
  820. m68hc11_elf_relax_delete_bytes (abfd, sec,
  821. irel->r_offset + 1, 1);
  822. }
  823. else
  824. {
  825. code ^= 0x1;
  826. bfd_put_8 (abfd, code, contents + prev_insn_branch->r_offset);
  827. bfd_put_8 (abfd, 0xff,
  828. contents + prev_insn_branch->r_offset + 1);
  829. irel->r_offset = prev_insn_branch->r_offset + 1;
  830. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  831. R_M68HC11_PCREL_8);
  832. m68hc11_elf_relax_delete_bytes (abfd, sec,
  833. irel->r_offset + 1, 3);
  834. }
  835. prev_insn_branch = 0;
  836. *again = true;
  837. }
  838. /* Try to turn a 16 bit address into a 8 bit page0 address. */
  839. else if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_16
  840. && (value & 0xff00) == 0)
  841. {
  842. unsigned char code;
  843. unsigned short offset;
  844. const struct m68hc11_direct_relax *rinfo;
  845. prev_insn_branch = 0;
  846. offset = bfd_get_16 (abfd, contents + irel->r_offset);
  847. offset += value;
  848. if ((offset & 0xff00) != 0)
  849. {
  850. prev_insn_group = 0;
  851. continue;
  852. }
  853. if (prev_insn_group)
  854. {
  855. unsigned long old_sec_size = sec->size;
  856. /* Note that we've changed the relocation contents, etc. */
  857. elf_section_data (sec)->relocs = internal_relocs;
  858. free_relocs = NULL;
  859. elf_section_data (sec)->this_hdr.contents = contents;
  860. free_contents = NULL;
  861. symtab_hdr->contents = (bfd_byte *) isymbuf;
  862. free_extsyms = NULL;
  863. m68hc11_relax_group (abfd, sec, contents, offset,
  864. prev_insn_group->r_offset,
  865. insn_group_value);
  866. irel = prev_insn_group;
  867. prev_insn_group = 0;
  868. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  869. R_M68HC11_NONE);
  870. if (sec->size != old_sec_size)
  871. *again = true;
  872. continue;
  873. }
  874. /* Get the opcode. */
  875. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  876. rinfo = find_relaxable_insn (code);
  877. if (rinfo == 0)
  878. {
  879. prev_insn_group = 0;
  880. continue;
  881. }
  882. /* Note that we've changed the relocation contents, etc. */
  883. elf_section_data (sec)->relocs = internal_relocs;
  884. free_relocs = NULL;
  885. elf_section_data (sec)->this_hdr.contents = contents;
  886. free_contents = NULL;
  887. symtab_hdr->contents = (bfd_byte *) isymbuf;
  888. free_extsyms = NULL;
  889. /* Fix the opcode. */
  890. /* printf ("A relaxable case : 0x%02x (%s)\n",
  891. code, rinfo->name); */
  892. bfd_put_8 (abfd, rinfo->direct_code,
  893. contents + irel->r_offset - 1);
  894. /* Delete one byte of data (upper byte of address). */
  895. m68hc11_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 1);
  896. /* Fix the relocation's type. */
  897. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  898. R_M68HC11_8);
  899. /* That will change things, so, we should relax again. */
  900. *again = true;
  901. }
  902. else if (ELF32_R_TYPE (irel->r_info) == R_M68HC11_16 && !is_far)
  903. {
  904. unsigned char code;
  905. bfd_vma offset;
  906. prev_insn_branch = 0;
  907. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  908. if (code == 0x7e || code == 0xbd)
  909. {
  910. offset = value - (irel->r_offset
  911. + sec->output_section->vma
  912. + sec->output_offset + 1);
  913. offset += bfd_get_16 (abfd, contents + irel->r_offset);
  914. /* If the offset is still out of -128..+127 range,
  915. leave that far branch unchanged. */
  916. if ((offset & 0xff80) == 0 || (offset & 0xff80) == 0xff80)
  917. {
  918. /* Note that we've changed the relocation contents, etc. */
  919. elf_section_data (sec)->relocs = internal_relocs;
  920. free_relocs = NULL;
  921. elf_section_data (sec)->this_hdr.contents = contents;
  922. free_contents = NULL;
  923. symtab_hdr->contents = (bfd_byte *) isymbuf;
  924. free_extsyms = NULL;
  925. /* Shrink the branch. */
  926. code = (code == 0x7e) ? 0x20 : 0x8d;
  927. bfd_put_8 (abfd, code,
  928. contents + irel->r_offset - 1);
  929. bfd_put_8 (abfd, 0xff,
  930. contents + irel->r_offset);
  931. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  932. R_M68HC11_PCREL_8);
  933. m68hc11_elf_relax_delete_bytes (abfd, sec,
  934. irel->r_offset + 1, 1);
  935. /* That will change things, so, we should relax again. */
  936. *again = true;
  937. }
  938. }
  939. }
  940. prev_insn_branch = 0;
  941. prev_insn_group = 0;
  942. }
  943. free (free_relocs);
  944. free_relocs = NULL;
  945. if (free_contents != NULL)
  946. {
  947. if (! link_info->keep_memory)
  948. free (free_contents);
  949. else
  950. {
  951. /* Cache the section contents for elf_link_input_bfd. */
  952. elf_section_data (sec)->this_hdr.contents = contents;
  953. }
  954. free_contents = NULL;
  955. }
  956. if (free_extsyms != NULL)
  957. {
  958. if (! link_info->keep_memory)
  959. free (free_extsyms);
  960. else
  961. {
  962. /* Cache the symbols for elf_link_input_bfd. */
  963. symtab_hdr->contents = (unsigned char *) isymbuf;
  964. }
  965. free_extsyms = NULL;
  966. }
  967. return true;
  968. error_return:
  969. free (free_relocs);
  970. free (free_contents);
  971. free (free_extsyms);
  972. return false;
  973. }
  974. /* Delete some bytes from a section while relaxing. */
  975. static void
  976. m68hc11_elf_relax_delete_bytes (bfd *abfd, asection *sec,
  977. bfd_vma addr, int count)
  978. {
  979. Elf_Internal_Shdr *symtab_hdr;
  980. unsigned int sec_shndx;
  981. bfd_byte *contents;
  982. Elf_Internal_Rela *irel, *irelend;
  983. bfd_vma toaddr;
  984. Elf_Internal_Sym *isymbuf, *isym, *isymend;
  985. struct elf_link_hash_entry **sym_hashes;
  986. struct elf_link_hash_entry **end_hashes;
  987. unsigned int symcount;
  988. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  989. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  990. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  991. contents = elf_section_data (sec)->this_hdr.contents;
  992. toaddr = sec->size;
  993. irel = elf_section_data (sec)->relocs;
  994. irelend = irel + sec->reloc_count;
  995. /* Actually delete the bytes. */
  996. memmove (contents + addr, contents + addr + count,
  997. (size_t) (toaddr - addr - count));
  998. sec->size -= count;
  999. /* Adjust all the relocs. */
  1000. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  1001. {
  1002. unsigned char code;
  1003. unsigned char offset;
  1004. unsigned short raddr;
  1005. unsigned long old_offset;
  1006. int branch_pos;
  1007. old_offset = irel->r_offset;
  1008. /* See if this reloc was for the bytes we have deleted, in which
  1009. case we no longer care about it. Don't delete relocs which
  1010. represent addresses, though. */
  1011. if (ELF32_R_TYPE (irel->r_info) != R_M68HC11_RL_JUMP
  1012. && irel->r_offset >= addr && irel->r_offset < addr + count)
  1013. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1014. R_M68HC11_NONE);
  1015. if (ELF32_R_TYPE (irel->r_info) == R_M68HC11_NONE)
  1016. continue;
  1017. /* Get the new reloc address. */
  1018. if ((irel->r_offset > addr
  1019. && irel->r_offset < toaddr))
  1020. irel->r_offset -= count;
  1021. /* If this is a PC relative reloc, see if the range it covers
  1022. includes the bytes we have deleted. */
  1023. switch (ELF32_R_TYPE (irel->r_info))
  1024. {
  1025. default:
  1026. break;
  1027. case R_M68HC11_RL_JUMP:
  1028. code = bfd_get_8 (abfd, contents + irel->r_offset);
  1029. switch (code)
  1030. {
  1031. /* jsr and jmp instruction are also marked with RL_JUMP
  1032. relocs but no adjustment must be made. */
  1033. case 0x7e:
  1034. case 0x9d:
  1035. case 0xbd:
  1036. continue;
  1037. case 0x12:
  1038. case 0x13:
  1039. branch_pos = 3;
  1040. raddr = 4;
  1041. /* Special case when we translate a brclr N,y into brclr *<addr>
  1042. In this case, the 0x18 page2 prefix is removed.
  1043. The reloc offset is not modified but the instruction
  1044. size is reduced by 1. */
  1045. if (old_offset == addr)
  1046. raddr++;
  1047. break;
  1048. case 0x1e:
  1049. case 0x1f:
  1050. branch_pos = 3;
  1051. raddr = 4;
  1052. break;
  1053. case 0x18:
  1054. branch_pos = 4;
  1055. raddr = 5;
  1056. break;
  1057. default:
  1058. branch_pos = 1;
  1059. raddr = 2;
  1060. break;
  1061. }
  1062. offset = bfd_get_8 (abfd, contents + irel->r_offset + branch_pos);
  1063. raddr += old_offset;
  1064. raddr += ((unsigned short) offset | ((offset & 0x80) ? 0xff00 : 0));
  1065. if (irel->r_offset < addr && raddr > addr)
  1066. {
  1067. offset -= count;
  1068. bfd_put_8 (abfd, offset, contents + irel->r_offset + branch_pos);
  1069. }
  1070. else if (irel->r_offset >= addr && raddr <= addr)
  1071. {
  1072. offset += count;
  1073. bfd_put_8 (abfd, offset, contents + irel->r_offset + branch_pos);
  1074. }
  1075. else
  1076. {
  1077. /*printf ("Not adjusted 0x%04x [0x%4x 0x%4x]\n", raddr,
  1078. irel->r_offset, addr);*/
  1079. }
  1080. break;
  1081. }
  1082. }
  1083. /* Adjust the local symbols defined in this section. */
  1084. isymend = isymbuf + symtab_hdr->sh_info;
  1085. for (isym = isymbuf; isym < isymend; isym++)
  1086. {
  1087. if (isym->st_shndx == sec_shndx
  1088. && isym->st_value > addr
  1089. && isym->st_value <= toaddr)
  1090. isym->st_value -= count;
  1091. }
  1092. /* Now adjust the global symbols defined in this section. */
  1093. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1094. - symtab_hdr->sh_info);
  1095. sym_hashes = elf_sym_hashes (abfd);
  1096. end_hashes = sym_hashes + symcount;
  1097. for (; sym_hashes < end_hashes; sym_hashes++)
  1098. {
  1099. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1100. if ((sym_hash->root.type == bfd_link_hash_defined
  1101. || sym_hash->root.type == bfd_link_hash_defweak)
  1102. && sym_hash->root.u.def.section == sec
  1103. && sym_hash->root.u.def.value > addr
  1104. && sym_hash->root.u.def.value <= toaddr)
  1105. {
  1106. sym_hash->root.u.def.value -= count;
  1107. }
  1108. }
  1109. }
  1110. /* Specific sections:
  1111. - The .page0 is a data section that is mapped in [0x0000..0x00FF].
  1112. Page0 accesses are faster on the M68HC11. Soft registers used by GCC-m6811
  1113. are located in .page0.
  1114. - The .vectors is the section that represents the interrupt
  1115. vectors. */
  1116. static const struct bfd_elf_special_section elf32_m68hc11_special_sections[] =
  1117. {
  1118. { STRING_COMMA_LEN (".eeprom"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  1119. { STRING_COMMA_LEN (".page0"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  1120. { STRING_COMMA_LEN (".softregs"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  1121. { STRING_COMMA_LEN (".vectors"), 0, SHT_PROGBITS, SHF_ALLOC },
  1122. { NULL, 0, 0, 0, 0 }
  1123. };
  1124. #define ELF_ARCH bfd_arch_m68hc11
  1125. #define ELF_TARGET_ID M68HC11_ELF_DATA
  1126. #define ELF_MACHINE_CODE EM_68HC11
  1127. #define ELF_MAXPAGESIZE 0x1000
  1128. #define TARGET_BIG_SYM m68hc11_elf32_vec
  1129. #define TARGET_BIG_NAME "elf32-m68hc11"
  1130. #define elf_info_to_howto NULL
  1131. #define elf_info_to_howto_rel m68hc11_info_to_howto_rel
  1132. #define bfd_elf32_bfd_relax_section m68hc11_elf_relax_section
  1133. #define elf_backend_check_relocs elf32_m68hc11_check_relocs
  1134. #define elf_backend_relocate_section elf32_m68hc11_relocate_section
  1135. #define elf_backend_add_symbol_hook elf32_m68hc11_add_symbol_hook
  1136. #define elf_backend_object_p 0
  1137. #define elf_backend_can_gc_sections 1
  1138. #define elf_backend_special_sections elf32_m68hc11_special_sections
  1139. #define elf_backend_merge_symbol_attribute elf32_m68hc11_merge_symbol_attribute
  1140. #define bfd_elf32_bfd_link_hash_table_create \
  1141. m68hc11_elf_bfd_link_hash_table_create
  1142. #define bfd_elf32_bfd_merge_private_bfd_data \
  1143. _bfd_m68hc11_elf_merge_private_bfd_data
  1144. #define bfd_elf32_bfd_set_private_flags _bfd_m68hc11_elf_set_private_flags
  1145. #define bfd_elf32_bfd_print_private_bfd_data \
  1146. _bfd_m68hc11_elf_print_private_bfd_data
  1147. #include "elf32-target.h"