1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2012 Free Software Foundation, Inc.
4 Written by Steve Chamberlain, <sac@cygnus.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 #include "coff/internal.h"
31 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
32 static reloc_howto_type howto_table[] =
34 HOWTO (R_W65_ABS8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
35 HOWTO (R_W65_ABS16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
36 HOWTO (R_W65_ABS24, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "abs24", TRUE, 0x00ffffff, 0x00ffffff, FALSE),
37 HOWTO (R_W65_ABS8S8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, ">abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
38 HOWTO (R_W65_ABS8S16, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "^abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
39 HOWTO (R_W65_ABS16S8, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, ">abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
40 HOWTO (R_W65_ABS16S16,1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "^abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
41 HOWTO (R_W65_PCR8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "pcrel8", TRUE, 0x000000ff, 0x000000ff, TRUE),
42 HOWTO (R_W65_PCR16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "pcrel16", TRUE, 0x0000ffff, 0x0000ffff, TRUE),
43 HOWTO (R_W65_DP, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "dp", TRUE, 0x000000ff, 0x000000ff, FALSE),
46 /* Turn a howto into a reloc number. */
48 #define SELECT_RELOC(x,howto) \
49 { x.r_type = select_reloc(howto); }
51 #define BADMAG(x) (W65BADMAG(x))
52 #define W65 1 /* Customize coffcode.h */
53 #define __A_MAGIC_SET__
55 /* Code to swap in the reloc */
56 #define SWAP_IN_RELOC_OFFSET H_GET_32
57 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
58 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
59 dst->r_stuff[0] = 'S'; \
60 dst->r_stuff[1] = 'C';
63 select_reloc (reloc_howto_type *howto)
68 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
71 rtype2howto (arelent *internal,
72 struct internal_reloc *dst)
74 internal->howto = howto_table + dst->r_type - 1;
77 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
79 /* Perform any necessary magic to the addend in a reloc entry. */
81 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
82 cache_ptr->addend = ext_reloc.r_offset;
84 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
85 reloc_processing(relent, reloc, symbols, abfd, section)
88 reloc_processing (arelent * relent,
89 struct internal_reloc *reloc,
94 relent->address = reloc->r_vaddr;
95 rtype2howto (relent, reloc);
97 if (((int) reloc->r_symndx) > 0)
98 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
100 relent->sym_ptr_ptr = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
102 relent->addend = reloc->r_offset;
104 relent->address -= section->vma;
105 /* relent->section = 0;*/
109 w65_reloc16_estimate (bfd *abfd,
110 asection *input_section,
113 struct bfd_link_info *link_info)
119 /* The address of the thing to be relocated will have moved back by
120 the size of the shrink - but we don't change reloc->address here,
121 since we need it to know where the relocation lives in the source
124 /* reloc->address -= shrink; conceptual */
126 bfd_vma address = reloc->address - shrink;
128 switch (reloc->howto->type)
135 /* Thing is a move one byte. */
137 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
141 /* Change the reloc type from 16bit, possible 8 to 8bit
143 reloc->howto = reloc->howto + 1;
144 /* The place to relc moves back by one. */
145 /* This will be two bytes smaller in the long run. */
147 bfd_perform_slip (abfd, 2, input_section, address);
151 /* This is the 24 bit branch which could become an 8 bitter,
152 the relocation points to the first byte of the insn, not the
156 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
158 dot = input_section->output_section->vma +
159 input_section->output_offset + address;
161 /* See if the address we're looking at within 127 bytes of where
162 we are, if so then we can use a small branch rather than the
163 jump we were going to. */
166 if (-120 < (long) gap && (long) gap < 120)
168 /* Change the reloc type from 24bit, possible 8 to 8bit
170 reloc->howto = reloc->howto + 1;
171 /* This will be two bytes smaller in the long run. */
173 bfd_perform_slip (abfd, 2, input_section, address);
178 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
180 dot = input_section->output_section->vma +
181 input_section->output_offset + address;
183 /* See if the address we're looking at within 127 bytes of where
184 we are, if so then we can use a small branch rather than the
185 jump we were going to. */
186 gap = value - (dot - shrink);
188 if (-120 < (long) gap && (long) gap < 120)
190 /* Change the reloc type from 16bit, possible 8 to 8bit
192 reloc->howto = reloc->howto + 1;
193 /* The place to relc moves back by one. */
195 /* This will be two bytes smaller in the long run. */
197 bfd_perform_slip (abfd, 2, input_section, address);
205 /* First phase of a relaxing link. */
209 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
210 R_JMP1 R_JMP2 jmp or pcrel branch
211 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
212 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
215 w65_reloc16_extra_cases (bfd *abfd,
216 struct bfd_link_info *link_info,
217 struct bfd_link_order *link_order,
220 unsigned int *src_ptr,
221 unsigned int *dst_ptr)
223 unsigned int src_address = *src_ptr;
224 unsigned int dst_address = *dst_ptr;
225 asection *input_section = link_order->u.indirect.section;
227 switch (reloc->howto->type)
232 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
234 bfd_put_8 (abfd, gap, data + dst_address);
242 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
245 bfd_put_8 (abfd, gap, data + dst_address);
253 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
256 bfd_put_8 (abfd, gap, data + dst_address);
264 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
267 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
274 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
277 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
284 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
287 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
295 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
297 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
298 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
306 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
308 bfd_vma dot = (dst_address
309 + input_section->output_offset
310 + input_section->output_section->vma);
313 if (gap < -128 || gap > 127)
315 if (! ((*link_info->callbacks->reloc_overflow)
317 bfd_asymbol_name (*reloc->sym_ptr_ptr),
318 reloc->howto->name, reloc->addend, input_section->owner,
319 input_section, reloc->address)))
322 bfd_put_8 (abfd, gap, data + dst_address);
330 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
332 bfd_vma dot = (dst_address
333 + input_section->output_offset
334 + input_section->output_section->vma);
336 /* This wraps within the page, so ignore the relativeness, look at the
338 if ((gap & 0xf0000) != (dot & 0xf0000))
340 if (! ((*link_info->callbacks->reloc_overflow)
342 bfd_asymbol_name (*reloc->sym_ptr_ptr),
343 reloc->howto->name, reloc->addend, input_section->owner,
344 input_section, reloc->address)))
349 bfd_put_16 (abfd, gap, data + dst_address);
355 printf (_("ignoring reloc %s\n"), reloc->howto->name);
359 *src_ptr = src_address;
360 *dst_ptr = dst_address;
363 #define coff_reloc16_extra_cases w65_reloc16_extra_cases
364 #define coff_reloc16_estimate w65_reloc16_estimate
366 #ifndef bfd_pe_print_pdata
367 #define bfd_pe_print_pdata NULL
370 #include "coffcode.h"
372 #undef coff_bfd_get_relocated_section_contents
373 #undef coff_bfd_relax_section
374 #define coff_bfd_get_relocated_section_contents \
375 bfd_coff_reloc16_get_relocated_section_contents
376 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
378 CREATE_LITTLE_COFF_TARGET_VEC (w65_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)