1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright (C) 1995-2014 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 /* Turn a howto into a reloc number. */
47 #define SELECT_RELOC(x,howto) \
48 { x.r_type = select_reloc(howto); }
50 #define BADMAG(x) (W65BADMAG(x))
51 #define W65 1 /* Customize coffcode.h */
52 #define __A_MAGIC_SET__
54 /* Code to swap in the reloc */
55 #define SWAP_IN_RELOC_OFFSET H_GET_32
56 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
57 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
58 dst->r_stuff[0] = 'S'; \
59 dst->r_stuff[1] = 'C';
62 select_reloc (reloc_howto_type *howto)
67 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
70 rtype2howto (arelent *internal,
71 struct internal_reloc *dst)
73 internal->howto = howto_table + dst->r_type - 1;
76 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
78 /* Perform any necessary magic to the addend in a reloc entry. */
80 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
81 cache_ptr->addend = ext_reloc.r_offset;
83 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
84 reloc_processing(relent, reloc, symbols, abfd, section)
87 reloc_processing (arelent * relent,
88 struct internal_reloc *reloc,
93 relent->address = reloc->r_vaddr;
94 rtype2howto (relent, reloc);
96 if (((int) reloc->r_symndx) > 0)
97 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
99 relent->sym_ptr_ptr = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
101 relent->addend = reloc->r_offset;
103 relent->address -= section->vma;
104 /* relent->section = 0;*/
108 w65_reloc16_estimate (bfd *abfd,
109 asection *input_section,
112 struct bfd_link_info *link_info)
118 /* The address of the thing to be relocated will have moved back by
119 the size of the shrink - but we don't change reloc->address here,
120 since we need it to know where the relocation lives in the source
123 /* reloc->address -= shrink; conceptual */
125 bfd_vma address = reloc->address - shrink;
127 switch (reloc->howto->type)
134 /* Thing is a move one byte. */
136 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
140 /* Change the reloc type from 16bit, possible 8 to 8bit
142 reloc->howto = reloc->howto + 1;
143 /* The place to relc moves back by one. */
144 /* This will be two bytes smaller in the long run. */
146 bfd_perform_slip (abfd, 2, input_section, address);
150 /* This is the 24 bit branch which could become an 8 bitter,
151 the relocation points to the first byte of the insn, not the
155 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
157 dot = input_section->output_section->vma +
158 input_section->output_offset + address;
160 /* See if the address we're looking at within 127 bytes of where
161 we are, if so then we can use a small branch rather than the
162 jump we were going to. */
165 if (-120 < (long) gap && (long) gap < 120)
167 /* Change the reloc type from 24bit, possible 8 to 8bit
169 reloc->howto = reloc->howto + 1;
170 /* This will be two bytes smaller in the long run. */
172 bfd_perform_slip (abfd, 2, input_section, address);
177 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
179 dot = input_section->output_section->vma +
180 input_section->output_offset + address;
182 /* See if the address we're looking at within 127 bytes of where
183 we are, if so then we can use a small branch rather than the
184 jump we were going to. */
185 gap = value - (dot - shrink);
187 if (-120 < (long) gap && (long) gap < 120)
189 /* Change the reloc type from 16bit, possible 8 to 8bit
191 reloc->howto = reloc->howto + 1;
192 /* The place to relc moves back by one. */
194 /* This will be two bytes smaller in the long run. */
196 bfd_perform_slip (abfd, 2, input_section, address);
204 /* First phase of a relaxing link. */
208 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
209 R_JMP1 R_JMP2 jmp or pcrel branch
210 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
211 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
214 w65_reloc16_extra_cases (bfd *abfd,
215 struct bfd_link_info *link_info,
216 struct bfd_link_order *link_order,
219 unsigned int *src_ptr,
220 unsigned int *dst_ptr)
222 unsigned int src_address = *src_ptr;
223 unsigned int dst_address = *dst_ptr;
224 asection *input_section = link_order->u.indirect.section;
226 switch (reloc->howto->type)
231 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
233 bfd_put_8 (abfd, gap, data + dst_address);
241 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
244 bfd_put_8 (abfd, gap, data + dst_address);
252 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
255 bfd_put_8 (abfd, gap, data + dst_address);
263 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
266 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
273 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
276 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
283 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
286 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
294 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
296 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
297 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
305 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
307 bfd_vma dot = (dst_address
308 + input_section->output_offset
309 + input_section->output_section->vma);
312 if (gap < -128 || gap > 127)
314 if (! ((*link_info->callbacks->reloc_overflow)
316 bfd_asymbol_name (*reloc->sym_ptr_ptr),
317 reloc->howto->name, reloc->addend, input_section->owner,
318 input_section, reloc->address)))
321 bfd_put_8 (abfd, gap, data + dst_address);
329 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
331 bfd_vma dot = (dst_address
332 + input_section->output_offset
333 + input_section->output_section->vma);
335 /* This wraps within the page, so ignore the relativeness, look at the
337 if ((gap & 0xf0000) != (dot & 0xf0000))
339 if (! ((*link_info->callbacks->reloc_overflow)
341 bfd_asymbol_name (*reloc->sym_ptr_ptr),
342 reloc->howto->name, reloc->addend, input_section->owner,
343 input_section, reloc->address)))
348 bfd_put_16 (abfd, gap, data + dst_address);
354 printf (_("ignoring reloc %s\n"), reloc->howto->name);
358 *src_ptr = src_address;
359 *dst_ptr = dst_address;
362 #define coff_reloc16_extra_cases w65_reloc16_extra_cases
363 #define coff_reloc16_estimate w65_reloc16_estimate
365 #ifndef bfd_pe_print_pdata
366 #define bfd_pe_print_pdata NULL
369 #include "coffcode.h"
371 #undef coff_bfd_get_relocated_section_contents
372 #undef coff_bfd_relax_section
373 #define coff_bfd_get_relocated_section_contents \
374 bfd_coff_reloc16_get_relocated_section_contents
375 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
377 CREATE_LITTLE_COFF_TARGET_VEC (w65_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)