coff-w65.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* BFD back-end for WDC 65816 COFF binaries.
  2. Copyright (C) 1995-2017 Free Software Foundation, Inc.
  3. Written by Steve Chamberlain, <sac@cygnus.com>.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "bfdlink.h"
  21. #include "coff/w65.h"
  22. #include "coff/internal.h"
  23. #include "libcoff.h"
  24. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
  25. static reloc_howto_type howto_table[] =
  26. {
  27. HOWTO (R_W65_ABS8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
  28. HOWTO (R_W65_ABS16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
  29. HOWTO (R_W65_ABS24, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "abs24", TRUE, 0x00ffffff, 0x00ffffff, FALSE),
  30. HOWTO (R_W65_ABS8S8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, ">abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
  31. HOWTO (R_W65_ABS8S16, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "^abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
  32. HOWTO (R_W65_ABS16S8, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, ">abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
  33. HOWTO (R_W65_ABS16S16,1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "^abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
  34. HOWTO (R_W65_PCR8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "pcrel8", TRUE, 0x000000ff, 0x000000ff, TRUE),
  35. HOWTO (R_W65_PCR16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "pcrel16", TRUE, 0x0000ffff, 0x0000ffff, TRUE),
  36. HOWTO (R_W65_DP, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "dp", TRUE, 0x000000ff, 0x000000ff, FALSE),
  37. };
  38. #define NUM_HOWTOS (sizeof (howto_table) / sizeof (howto_table[0]))
  39. /* Turn a howto into a reloc number. */
  40. #define SELECT_RELOC(x,howto) \
  41. { x.r_type = select_reloc(howto); }
  42. #define BADMAG(x) (W65BADMAG(x))
  43. #define W65 1 /* Customize coffcode.h */
  44. #define __A_MAGIC_SET__
  45. /* Code to swap in the reloc */
  46. #define SWAP_IN_RELOC_OFFSET H_GET_32
  47. #define SWAP_OUT_RELOC_OFFSET H_PUT_32
  48. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
  49. dst->r_stuff[0] = 'S'; \
  50. dst->r_stuff[1] = 'C';
  51. static int
  52. select_reloc (reloc_howto_type *howto)
  53. {
  54. return howto->type;
  55. }
  56. /* Code to turn a r_type into a howto ptr, uses the above howto table. */
  57. static void
  58. rtype2howto (arelent *internal,
  59. struct internal_reloc *dst)
  60. {
  61. if (dst->r_type > 0 && dst->r_type <= NUM_HOWTOS)
  62. internal->howto = howto_table + dst->r_type - 1;
  63. else
  64. internal->howto = NULL;
  65. }
  66. #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
  67. /* Perform any necessary magic to the addend in a reloc entry. */
  68. #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
  69. cache_ptr->addend = ext_reloc.r_offset;
  70. #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
  71. reloc_processing(relent, reloc, symbols, abfd, section)
  72. static void
  73. reloc_processing (arelent * relent,
  74. struct internal_reloc *reloc,
  75. asymbol ** symbols,
  76. bfd * abfd,
  77. asection * section)
  78. {
  79. relent->address = reloc->r_vaddr;
  80. rtype2howto (relent, reloc);
  81. if (((int) reloc->r_symndx) > 0)
  82. relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
  83. else
  84. relent->sym_ptr_ptr = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
  85. relent->addend = reloc->r_offset;
  86. relent->address -= section->vma;
  87. /* relent->section = 0;*/
  88. }
  89. static int
  90. w65_reloc16_estimate (bfd *abfd,
  91. asection *input_section,
  92. arelent *reloc,
  93. unsigned int shrink,
  94. struct bfd_link_info *link_info)
  95. {
  96. bfd_vma value;
  97. bfd_vma dot;
  98. bfd_vma gap;
  99. /* The address of the thing to be relocated will have moved back by
  100. the size of the shrink - but we don't change reloc->address here,
  101. since we need it to know where the relocation lives in the source
  102. uncooked section. */
  103. /* reloc->address -= shrink; conceptual */
  104. bfd_vma address = reloc->address - shrink;
  105. switch (reloc->howto->type)
  106. {
  107. case R_MOV16B2:
  108. case R_JMP2:
  109. shrink+=2;
  110. break;
  111. /* Thing is a move one byte. */
  112. case R_MOV16B1:
  113. value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
  114. if (value >= 0xff00)
  115. {
  116. /* Change the reloc type from 16bit, possible 8 to 8bit
  117. possible 16. */
  118. reloc->howto = reloc->howto + 1;
  119. /* The place to relc moves back by one. */
  120. /* This will be two bytes smaller in the long run. */
  121. shrink += 2;
  122. bfd_perform_slip (abfd, 2, input_section, address);
  123. }
  124. break;
  125. /* This is the 24 bit branch which could become an 8 bitter,
  126. the relocation points to the first byte of the insn, not the
  127. actual data. */
  128. case R_JMPL1:
  129. value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
  130. dot = input_section->output_section->vma +
  131. input_section->output_offset + address;
  132. /* See if the address we're looking at within 127 bytes of where
  133. we are, if so then we can use a small branch rather than the
  134. jump we were going to. */
  135. gap = value - dot;
  136. if (-120 < (long) gap && (long) gap < 120)
  137. {
  138. /* Change the reloc type from 24bit, possible 8 to 8bit
  139. possible 32. */
  140. reloc->howto = reloc->howto + 1;
  141. /* This will be two bytes smaller in the long run. */
  142. shrink += 2;
  143. bfd_perform_slip (abfd, 2, input_section, address);
  144. }
  145. break;
  146. case R_JMP1:
  147. value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
  148. dot = input_section->output_section->vma +
  149. input_section->output_offset + address;
  150. /* See if the address we're looking at within 127 bytes of where
  151. we are, if so then we can use a small branch rather than the
  152. jump we were going to. */
  153. gap = value - (dot - shrink);
  154. if (-120 < (long) gap && (long) gap < 120)
  155. {
  156. /* Change the reloc type from 16bit, possible 8 to 8bit
  157. possible 16. */
  158. reloc->howto = reloc->howto + 1;
  159. /* The place to relc moves back by one. */
  160. /* This will be two bytes smaller in the long run. */
  161. shrink += 2;
  162. bfd_perform_slip (abfd, 2, input_section, address);
  163. }
  164. break;
  165. }
  166. return shrink;
  167. }
  168. /* First phase of a relaxing link. */
  169. /* Reloc types
  170. large small
  171. R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
  172. R_JMP1 R_JMP2 jmp or pcrel branch
  173. R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
  174. R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
  175. static void
  176. w65_reloc16_extra_cases (bfd *abfd,
  177. struct bfd_link_info *link_info,
  178. struct bfd_link_order *link_order,
  179. arelent *reloc,
  180. bfd_byte *data,
  181. unsigned int *src_ptr,
  182. unsigned int *dst_ptr)
  183. {
  184. unsigned int src_address = *src_ptr;
  185. unsigned int dst_address = *dst_ptr;
  186. asection *input_section = link_order->u.indirect.section;
  187. switch (reloc->howto->type)
  188. {
  189. case R_W65_ABS8:
  190. case R_W65_DP:
  191. {
  192. unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
  193. input_section);
  194. bfd_put_8 (abfd, gap, data + dst_address);
  195. dst_address += 1;
  196. src_address += 1;
  197. }
  198. break;
  199. case R_W65_ABS8S8:
  200. {
  201. unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
  202. input_section);
  203. gap >>= 8;
  204. bfd_put_8 (abfd, gap, data + dst_address);
  205. dst_address += 1;
  206. src_address += 1;
  207. }
  208. break;
  209. case R_W65_ABS8S16:
  210. {
  211. unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
  212. input_section);
  213. gap >>= 16;
  214. bfd_put_8 (abfd, gap, data + dst_address);
  215. dst_address += 1;
  216. src_address += 1;
  217. }
  218. break;
  219. case R_W65_ABS16:
  220. {
  221. unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
  222. input_section);
  223. bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
  224. dst_address += 2;
  225. src_address += 2;
  226. }
  227. break;
  228. case R_W65_ABS16S8:
  229. {
  230. unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
  231. input_section);
  232. gap >>= 8;
  233. bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
  234. dst_address += 2;
  235. src_address += 2;
  236. }
  237. break;
  238. case R_W65_ABS16S16:
  239. {
  240. unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
  241. input_section);
  242. gap >>= 16;
  243. bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
  244. dst_address += 2;
  245. src_address += 2;
  246. }
  247. break;
  248. case R_W65_ABS24:
  249. {
  250. unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
  251. input_section);
  252. bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
  253. bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
  254. dst_address += 3;
  255. src_address += 3;
  256. }
  257. break;
  258. case R_W65_PCR8:
  259. {
  260. int gap = bfd_coff_reloc16_get_value (reloc, link_info,
  261. input_section);
  262. bfd_vma dot = (dst_address
  263. + input_section->output_offset
  264. + input_section->output_section->vma);
  265. gap -= dot + 1;
  266. if (gap < -128 || gap > 127)
  267. (*link_info->callbacks->reloc_overflow)
  268. (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
  269. reloc->howto->name, reloc->addend, input_section->owner,
  270. input_section, reloc->address);
  271. bfd_put_8 (abfd, gap, data + dst_address);
  272. dst_address += 1;
  273. src_address += 1;
  274. }
  275. break;
  276. case R_W65_PCR16:
  277. {
  278. bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
  279. input_section);
  280. bfd_vma dot = (dst_address
  281. + input_section->output_offset
  282. + input_section->output_section->vma);
  283. /* This wraps within the page, so ignore the relativeness, look at the
  284. high part. */
  285. if ((gap & 0xf0000) != (dot & 0xf0000))
  286. (*link_info->callbacks->reloc_overflow)
  287. (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
  288. reloc->howto->name, reloc->addend, input_section->owner,
  289. input_section, reloc->address);
  290. gap -= dot + 2;
  291. bfd_put_16 (abfd, gap, data + dst_address);
  292. dst_address += 2;
  293. src_address += 2;
  294. }
  295. break;
  296. default:
  297. printf (_("ignoring reloc %s\n"), reloc->howto->name);
  298. break;
  299. }
  300. *src_ptr = src_address;
  301. *dst_ptr = dst_address;
  302. }
  303. #define coff_reloc16_extra_cases w65_reloc16_extra_cases
  304. #define coff_reloc16_estimate w65_reloc16_estimate
  305. #ifndef bfd_pe_print_pdata
  306. #define bfd_pe_print_pdata NULL
  307. #endif
  308. #include "coffcode.h"
  309. #undef coff_bfd_get_relocated_section_contents
  310. #undef coff_bfd_relax_section
  311. #define coff_bfd_get_relocated_section_contents \
  312. bfd_coff_reloc16_get_relocated_section_contents
  313. #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
  314. CREATE_LITTLE_COFF_TARGET_VEC (w65_coff_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)