1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003
3 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "coff/internal.h"
30 static int select_reloc PARAMS ((reloc_howto_type *));
31 static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
32 static void reloc_processing PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
33 static int w65_reloc16_estimate PARAMS ((bfd *, asection *, arelent *, unsigned int, struct bfd_link_info *));
34 static void w65_reloc16_extra_cases PARAMS ((bfd *,struct bfd_link_info *, struct bfd_link_order *, arelent *, bfd_byte *, unsigned int *, unsigned int *));
36 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
37 static reloc_howto_type howto_table[] =
39 HOWTO (R_W65_ABS8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
40 HOWTO (R_W65_ABS16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
41 HOWTO (R_W65_ABS24, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "abs24", TRUE, 0x00ffffff, 0x00ffffff, FALSE),
42 HOWTO (R_W65_ABS8S8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, ">abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
43 HOWTO (R_W65_ABS8S16, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "^abs8", TRUE, 0x000000ff, 0x000000ff, FALSE),
44 HOWTO (R_W65_ABS16S8, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, ">abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
45 HOWTO (R_W65_ABS16S16,1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "^abs16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
46 HOWTO (R_W65_PCR8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "pcrel8", TRUE, 0x000000ff, 0x000000ff, TRUE),
47 HOWTO (R_W65_PCR16, 1, 0, 16, FALSE, 0, complain_overflow_bitfield, 0, "pcrel16", TRUE, 0x0000ffff, 0x0000ffff, TRUE),
48 HOWTO (R_W65_DP, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "dp", TRUE, 0x000000ff, 0x000000ff, FALSE),
51 /* Turn a howto into a reloc number. */
53 #define SELECT_RELOC(x,howto) \
54 { x.r_type = select_reloc(howto); }
56 #define BADMAG(x) (W65BADMAG(x))
57 #define W65 1 /* Customize coffcode.h */
58 #define __A_MAGIC_SET__
60 /* Code to swap in the reloc */
61 #define SWAP_IN_RELOC_OFFSET H_GET_32
62 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
63 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
64 dst->r_stuff[0] = 'S'; \
65 dst->r_stuff[1] = 'C';
69 reloc_howto_type *howto;
74 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
77 rtype2howto (internal, dst)
79 struct internal_reloc *dst;
81 internal->howto = howto_table + dst->r_type - 1;
84 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
86 /* Perform any necessary magic to the addend in a reloc entry. */
88 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
89 cache_ptr->addend = ext_reloc.r_offset;
91 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
92 reloc_processing(relent, reloc, symbols, abfd, section)
95 reloc_processing (relent, reloc, symbols, abfd, section)
97 struct internal_reloc *reloc;
102 relent->address = reloc->r_vaddr;
103 rtype2howto (relent, reloc);
105 if (((int) reloc->r_symndx) > 0)
106 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
108 relent->sym_ptr_ptr = (asymbol **)&(bfd_abs_symbol);
110 relent->addend = reloc->r_offset;
112 relent->address -= section->vma;
113 /* relent->section = 0;*/
117 w65_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
119 asection *input_section;
122 struct bfd_link_info *link_info;
128 /* The address of the thing to be relocated will have moved back by
129 the size of the shrink - but we don't change reloc->address here,
130 since we need it to know where the relocation lives in the source
133 /* reloc->address -= shrink; conceptual */
135 bfd_vma address = reloc->address - shrink;
137 switch (reloc->howto->type)
144 /* Thing is a move one byte. */
146 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
150 /* Change the reloc type from 16bit, possible 8 to 8bit
152 reloc->howto = reloc->howto + 1;
153 /* The place to relc moves back by one. */
154 /* This will be two bytes smaller in the long run. */
156 bfd_perform_slip (abfd, 2, input_section, address);
160 /* This is the 24 bit branch which could become an 8 bitter,
161 the relocation points to the first byte of the insn, not the
165 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
167 dot = input_section->output_section->vma +
168 input_section->output_offset + address;
170 /* See if the address we're looking at within 127 bytes of where
171 we are, if so then we can use a small branch rather than the
172 jump we were going to. */
175 if (-120 < (long) gap && (long) gap < 120)
177 /* Change the reloc type from 24bit, possible 8 to 8bit
179 reloc->howto = reloc->howto + 1;
180 /* This will be two bytes smaller in the long run. */
182 bfd_perform_slip (abfd, 2, input_section, address);
187 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
189 dot = input_section->output_section->vma +
190 input_section->output_offset + address;
192 /* See if the address we're looking at within 127 bytes of where
193 we are, if so then we can use a small branch rather than the
194 jump we were going to. */
195 gap = value - (dot - shrink);
197 if (-120 < (long) gap && (long) gap < 120)
199 /* Change the reloc type from 16bit, possible 8 to 8bit
201 reloc->howto = reloc->howto + 1;
202 /* The place to relc moves back by one. */
204 /* This will be two bytes smaller in the long run. */
206 bfd_perform_slip (abfd, 2, input_section, address);
214 /* First phase of a relaxing link. */
218 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
219 R_JMP1 R_JMP2 jmp or pcrel branch
220 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
221 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
224 w65_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
227 struct bfd_link_info *link_info;
228 struct bfd_link_order *link_order;
231 unsigned int *src_ptr;
232 unsigned int *dst_ptr;
234 unsigned int src_address = *src_ptr;
235 unsigned int dst_address = *dst_ptr;
236 asection *input_section = link_order->u.indirect.section;
238 switch (reloc->howto->type)
243 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_8 (abfd, gap, data + dst_address);
275 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
278 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
285 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
288 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
295 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
298 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
306 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
308 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
309 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
317 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
319 bfd_vma dot = link_order->offset
321 + link_order->u.indirect.section->output_section->vma;
324 if (gap < -128 || gap > 127)
326 if (! ((*link_info->callbacks->reloc_overflow)
327 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
328 reloc->howto->name, reloc->addend, input_section->owner,
329 input_section, reloc->address)))
332 bfd_put_8 (abfd, gap, data + dst_address);
340 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
342 bfd_vma dot = link_order->offset
344 + link_order->u.indirect.section->output_section->vma;
346 /* This wraps within the page, so ignore the relativeness, look at the
348 if ((gap & 0xf0000) != (dot & 0xf0000))
350 if (! ((*link_info->callbacks->reloc_overflow)
351 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
352 reloc->howto->name, reloc->addend, input_section->owner,
353 input_section, reloc->address)))
358 bfd_put_16 (abfd, gap, data + dst_address);
364 printf (_("ignoring reloc %s\n"), reloc->howto->name);
368 *src_ptr = src_address;
369 *dst_ptr = dst_address;
372 #define coff_reloc16_extra_cases w65_reloc16_extra_cases
373 #define coff_reloc16_estimate w65_reloc16_estimate
375 #include "coffcode.h"
377 #undef coff_bfd_get_relocated_section_contents
378 #undef coff_bfd_relax_section
379 #define coff_bfd_get_relocated_section_contents \
380 bfd_coff_reloc16_get_relocated_section_contents
381 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
383 CREATE_LITTLE_COFF_TARGET_VEC (w65_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)