1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3 Written by Steve Chamberlain, <sac@cygnus.com>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 #include "coff/internal.h"
30 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
31 static reloc_howto_type howto_table[] =
33 HOWTO (R_W65_ABS8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
34 HOWTO (R_W65_ABS16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
35 HOWTO (R_W65_ABS24, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "abs24", TRUE, 0x00ffffff, 0x00ffffff, FALSE),
36 HOWTO (R_W65_ABS8S8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, ">abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
37 HOWTO (R_W65_ABS8S16, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "^abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
38 HOWTO (R_W65_ABS16S8, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, ">abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
39 HOWTO (R_W65_ABS16S16,1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "^abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
40 HOWTO (R_W65_PCR8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "pcrel8", TRUE, 0x000000ff, 0x000000ff, TRUE),
41 HOWTO (R_W65_PCR16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "pcrel16", TRUE, 0x0000ffff, 0x0000ffff, TRUE),
42 HOWTO (R_W65_DP, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "dp", TRUE, 0x000000ff, 0x000000ff, FALSE),
45 #define NUM_HOWTOS (sizeof (howto_table) / sizeof (howto_table[0]))
47 /* Turn a howto into a reloc number. */
49 #define SELECT_RELOC(x,howto) \
50 { x.r_type = select_reloc(howto); }
52 #define BADMAG(x) (W65BADMAG(x))
53 #define W65 1 /* Customize coffcode.h */
54 #define __A_MAGIC_SET__
56 /* Code to swap in the reloc */
57 #define SWAP_IN_RELOC_OFFSET H_GET_32
58 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
59 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
60 dst->r_stuff[0] = 'S'; \
61 dst->r_stuff[1] = 'C';
64 select_reloc (reloc_howto_type *howto)
69 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
72 rtype2howto (arelent *internal,
73 struct internal_reloc *dst)
75 if (dst->r_type > 0 && dst->r_type <= NUM_HOWTOS)
76 internal->howto = howto_table + dst->r_type - 1;
78 internal->howto = NULL;
81 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
83 /* Perform any necessary magic to the addend in a reloc entry. */
85 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
86 cache_ptr->addend = ext_reloc.r_offset;
88 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
89 reloc_processing(relent, reloc, symbols, abfd, section)
92 reloc_processing (arelent * relent,
93 struct internal_reloc *reloc,
98 relent->address = reloc->r_vaddr;
99 rtype2howto (relent, reloc);
101 if (((int) reloc->r_symndx) > 0)
102 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
104 relent->sym_ptr_ptr = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
106 relent->addend = reloc->r_offset;
108 relent->address -= section->vma;
109 /* relent->section = 0;*/
113 w65_reloc16_estimate (bfd *abfd,
114 asection *input_section,
117 struct bfd_link_info *link_info)
123 /* The address of the thing to be relocated will have moved back by
124 the size of the shrink - but we don't change reloc->address here,
125 since we need it to know where the relocation lives in the source
128 /* reloc->address -= shrink; conceptual */
130 bfd_vma address = reloc->address - shrink;
132 switch (reloc->howto->type)
139 /* Thing is a move one byte. */
141 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
145 /* Change the reloc type from 16bit, possible 8 to 8bit
147 reloc->howto = reloc->howto + 1;
148 /* The place to relc moves back by one. */
149 /* This will be two bytes smaller in the long run. */
151 bfd_perform_slip (abfd, 2, input_section, address);
155 /* This is the 24 bit branch which could become an 8 bitter,
156 the relocation points to the first byte of the insn, not the
160 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
162 dot = input_section->output_section->vma +
163 input_section->output_offset + address;
165 /* See if the address we're looking at within 127 bytes of where
166 we are, if so then we can use a small branch rather than the
167 jump we were going to. */
170 if (-120 < (long) gap && (long) gap < 120)
172 /* Change the reloc type from 24bit, possible 8 to 8bit
174 reloc->howto = reloc->howto + 1;
175 /* This will be two bytes smaller in the long run. */
177 bfd_perform_slip (abfd, 2, input_section, address);
182 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
184 dot = input_section->output_section->vma +
185 input_section->output_offset + address;
187 /* See if the address we're looking at within 127 bytes of where
188 we are, if so then we can use a small branch rather than the
189 jump we were going to. */
190 gap = value - (dot - shrink);
192 if (-120 < (long) gap && (long) gap < 120)
194 /* Change the reloc type from 16bit, possible 8 to 8bit
196 reloc->howto = reloc->howto + 1;
197 /* The place to relc moves back by one. */
199 /* This will be two bytes smaller in the long run. */
201 bfd_perform_slip (abfd, 2, input_section, address);
209 /* First phase of a relaxing link. */
213 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
214 R_JMP1 R_JMP2 jmp or pcrel branch
215 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
216 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
219 w65_reloc16_extra_cases (bfd *abfd,
220 struct bfd_link_info *link_info,
221 struct bfd_link_order *link_order,
224 unsigned int *src_ptr,
225 unsigned int *dst_ptr)
227 unsigned int src_address = *src_ptr;
228 unsigned int dst_address = *dst_ptr;
229 asection *input_section = link_order->u.indirect.section;
231 switch (reloc->howto->type)
236 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
238 bfd_put_8 (abfd, gap, data + dst_address);
246 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
249 bfd_put_8 (abfd, gap, data + dst_address);
257 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
260 bfd_put_8 (abfd, gap, data + dst_address);
268 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
271 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
278 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
281 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
288 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
291 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
299 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
301 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
302 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
310 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
312 bfd_vma dot = (dst_address
313 + input_section->output_offset
314 + input_section->output_section->vma);
317 if (gap < -128 || gap > 127)
319 if (! ((*link_info->callbacks->reloc_overflow)
321 bfd_asymbol_name (*reloc->sym_ptr_ptr),
322 reloc->howto->name, reloc->addend, input_section->owner,
323 input_section, reloc->address)))
326 bfd_put_8 (abfd, gap, data + dst_address);
334 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
336 bfd_vma dot = (dst_address
337 + input_section->output_offset
338 + input_section->output_section->vma);
340 /* This wraps within the page, so ignore the relativeness, look at the
342 if ((gap & 0xf0000) != (dot & 0xf0000))
344 if (! ((*link_info->callbacks->reloc_overflow)
346 bfd_asymbol_name (*reloc->sym_ptr_ptr),
347 reloc->howto->name, reloc->addend, input_section->owner,
348 input_section, reloc->address)))
353 bfd_put_16 (abfd, gap, data + dst_address);
359 printf (_("ignoring reloc %s\n"), reloc->howto->name);
363 *src_ptr = src_address;
364 *dst_ptr = dst_address;
367 #define coff_reloc16_extra_cases w65_reloc16_extra_cases
368 #define coff_reloc16_estimate w65_reloc16_estimate
370 #ifndef bfd_pe_print_pdata
371 #define bfd_pe_print_pdata NULL
374 #include "coffcode.h"
376 #undef coff_bfd_get_relocated_section_contents
377 #undef coff_bfd_relax_section
378 #define coff_bfd_get_relocated_section_contents \
379 bfd_coff_reloc16_get_relocated_section_contents
380 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
382 CREATE_LITTLE_COFF_TARGET_VEC (w65_coff_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)