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 Free Software Foundation, Inc.
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 static int select_reloc PARAMS ((reloc_howto_type *));
32 static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
33 static void reloc_processing PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
34 static int w65_reloc16_estimate PARAMS ((bfd *, asection *, arelent *, unsigned int, struct bfd_link_info *));
35 static void w65_reloc16_extra_cases PARAMS ((bfd *,struct bfd_link_info *, struct bfd_link_order *, arelent *, bfd_byte *, unsigned int *, unsigned int *));
37 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
38 static reloc_howto_type howto_table[] =
40 HOWTO (R_W65_ABS8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
41 HOWTO (R_W65_ABS16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
42 HOWTO (R_W65_ABS24, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "abs24", TRUE, 0x00ffffff, 0x00ffffff, FALSE),
43 HOWTO (R_W65_ABS8S8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, ">abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
44 HOWTO (R_W65_ABS8S16, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "^abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
45 HOWTO (R_W65_ABS16S8, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, ">abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
46 HOWTO (R_W65_ABS16S16,1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "^abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
47 HOWTO (R_W65_PCR8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "pcrel8", TRUE, 0x000000ff, 0x000000ff, TRUE),
48 HOWTO (R_W65_PCR16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "pcrel16", TRUE, 0x0000ffff, 0x0000ffff, TRUE),
49 HOWTO (R_W65_DP, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "dp", TRUE, 0x000000ff, 0x000000ff, FALSE),
52 /* Turn a howto into a reloc number. */
54 #define SELECT_RELOC(x,howto) \
55 { x.r_type = select_reloc(howto); }
57 #define BADMAG(x) (W65BADMAG(x))
58 #define W65 1 /* Customize coffcode.h */
59 #define __A_MAGIC_SET__
61 /* Code to swap in the reloc */
62 #define SWAP_IN_RELOC_OFFSET H_GET_32
63 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
64 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
65 dst->r_stuff[0] = 'S'; \
66 dst->r_stuff[1] = 'C';
70 reloc_howto_type *howto;
75 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
78 rtype2howto (internal, dst)
80 struct internal_reloc *dst;
82 internal->howto = howto_table + dst->r_type - 1;
85 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
87 /* Perform any necessary magic to the addend in a reloc entry. */
89 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
90 cache_ptr->addend = ext_reloc.r_offset;
92 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
93 reloc_processing(relent, reloc, symbols, abfd, section)
96 reloc_processing (relent, reloc, symbols, abfd, section)
98 struct internal_reloc *reloc;
103 relent->address = reloc->r_vaddr;
104 rtype2howto (relent, reloc);
106 if (((int) reloc->r_symndx) > 0)
107 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
109 relent->sym_ptr_ptr = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
111 relent->addend = reloc->r_offset;
113 relent->address -= section->vma;
114 /* relent->section = 0;*/
118 w65_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
120 asection *input_section;
123 struct bfd_link_info *link_info;
129 /* The address of the thing to be relocated will have moved back by
130 the size of the shrink - but we don't change reloc->address here,
131 since we need it to know where the relocation lives in the source
134 /* reloc->address -= shrink; conceptual */
136 bfd_vma address = reloc->address - shrink;
138 switch (reloc->howto->type)
145 /* Thing is a move one byte. */
147 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
151 /* Change the reloc type from 16bit, possible 8 to 8bit
153 reloc->howto = reloc->howto + 1;
154 /* The place to relc moves back by one. */
155 /* This will be two bytes smaller in the long run. */
157 bfd_perform_slip (abfd, 2, input_section, address);
161 /* This is the 24 bit branch which could become an 8 bitter,
162 the relocation points to the first byte of the insn, not the
166 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
168 dot = input_section->output_section->vma +
169 input_section->output_offset + address;
171 /* See if the address we're looking at within 127 bytes of where
172 we are, if so then we can use a small branch rather than the
173 jump we were going to. */
176 if (-120 < (long) gap && (long) gap < 120)
178 /* Change the reloc type from 24bit, possible 8 to 8bit
180 reloc->howto = reloc->howto + 1;
181 /* This will be two bytes smaller in the long run. */
183 bfd_perform_slip (abfd, 2, input_section, address);
188 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
190 dot = input_section->output_section->vma +
191 input_section->output_offset + address;
193 /* See if the address we're looking at within 127 bytes of where
194 we are, if so then we can use a small branch rather than the
195 jump we were going to. */
196 gap = value - (dot - shrink);
198 if (-120 < (long) gap && (long) gap < 120)
200 /* Change the reloc type from 16bit, possible 8 to 8bit
202 reloc->howto = reloc->howto + 1;
203 /* The place to relc moves back by one. */
205 /* This will be two bytes smaller in the long run. */
207 bfd_perform_slip (abfd, 2, input_section, address);
215 /* First phase of a relaxing link. */
219 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
220 R_JMP1 R_JMP2 jmp or pcrel branch
221 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
222 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
225 w65_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
228 struct bfd_link_info *link_info;
229 struct bfd_link_order *link_order;
232 unsigned int *src_ptr;
233 unsigned int *dst_ptr;
235 unsigned int src_address = *src_ptr;
236 unsigned int dst_address = *dst_ptr;
237 asection *input_section = link_order->u.indirect.section;
239 switch (reloc->howto->type)
244 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
246 bfd_put_8 (abfd, gap, data + dst_address);
254 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
257 bfd_put_8 (abfd, gap, data + dst_address);
265 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
268 bfd_put_8 (abfd, gap, data + dst_address);
276 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
279 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
286 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
289 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
296 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
299 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
307 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
309 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
310 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
318 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
320 bfd_vma dot = (dst_address
321 + input_section->output_offset
322 + input_section->output_section->vma);
325 if (gap < -128 || gap > 127)
327 if (! ((*link_info->callbacks->reloc_overflow)
329 bfd_asymbol_name (*reloc->sym_ptr_ptr),
330 reloc->howto->name, reloc->addend, input_section->owner,
331 input_section, reloc->address)))
334 bfd_put_8 (abfd, gap, data + dst_address);
342 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
344 bfd_vma dot = (dst_address
345 + input_section->output_offset
346 + input_section->output_section->vma);
348 /* This wraps within the page, so ignore the relativeness, look at the
350 if ((gap & 0xf0000) != (dot & 0xf0000))
352 if (! ((*link_info->callbacks->reloc_overflow)
354 bfd_asymbol_name (*reloc->sym_ptr_ptr),
355 reloc->howto->name, reloc->addend, input_section->owner,
356 input_section, reloc->address)))
361 bfd_put_16 (abfd, gap, data + dst_address);
367 printf (_("ignoring reloc %s\n"), reloc->howto->name);
371 *src_ptr = src_address;
372 *dst_ptr = dst_address;
375 #define coff_reloc16_extra_cases w65_reloc16_extra_cases
376 #define coff_reloc16_estimate w65_reloc16_estimate
378 #ifndef bfd_pe_print_pdata
379 #define bfd_pe_print_pdata NULL
382 #include "coffcode.h"
384 #undef coff_bfd_get_relocated_section_contents
385 #undef coff_bfd_relax_section
386 #define coff_bfd_get_relocated_section_contents \
387 bfd_coff_reloc16_get_relocated_section_contents
388 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
390 CREATE_LITTLE_COFF_TARGET_VEC (w65_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)