config:
[binutils.git] / bfd / coff-w65.c
blobe4e304b0f8f871ada81cf827a328047450fc418b
1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "coff/w65.h"
27 #include "coff/internal.h"
28 #include "libcoff.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';
67 static int
68 select_reloc (howto)
69 reloc_howto_type *howto;
71 return howto->type ;
74 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
76 static void
77 rtype2howto (internal, dst)
78 arelent *internal;
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)
94 static void
95 reloc_processing (relent, reloc, symbols, abfd, section)
96 arelent * relent;
97 struct internal_reloc *reloc;
98 asymbol ** symbols;
99 bfd * abfd;
100 asection * section;
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];
107 else
108 relent->sym_ptr_ptr = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
110 relent->addend = reloc->r_offset;
112 relent->address -= section->vma;
113 /* relent->section = 0;*/
116 static int
117 w65_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
118 bfd *abfd;
119 asection *input_section;
120 arelent *reloc;
121 unsigned int shrink;
122 struct bfd_link_info *link_info;
124 bfd_vma value;
125 bfd_vma dot;
126 bfd_vma gap;
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
131 uncooked section. */
133 /* reloc->address -= shrink; conceptual */
135 bfd_vma address = reloc->address - shrink;
137 switch (reloc->howto->type)
139 case R_MOV16B2:
140 case R_JMP2:
141 shrink+=2;
142 break;
144 /* Thing is a move one byte. */
145 case R_MOV16B1:
146 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
148 if (value >= 0xff00)
150 /* Change the reloc type from 16bit, possible 8 to 8bit
151 possible 16. */
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. */
155 shrink += 2;
156 bfd_perform_slip (abfd, 2, input_section, address);
159 break;
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
162 actual data. */
164 case R_JMPL1:
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. */
173 gap = value - dot;
175 if (-120 < (long) gap && (long) gap < 120)
177 /* Change the reloc type from 24bit, possible 8 to 8bit
178 possible 32. */
179 reloc->howto = reloc->howto + 1;
180 /* This will be two bytes smaller in the long run. */
181 shrink += 2;
182 bfd_perform_slip (abfd, 2, input_section, address);
184 break;
186 case R_JMP1:
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
200 possible 16. */
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. */
205 shrink += 2;
206 bfd_perform_slip (abfd, 2, input_section, address);
208 break;
211 return shrink;
214 /* First phase of a relaxing link. */
216 /* Reloc types
217 large small
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 */
223 static void
224 w65_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
225 dst_ptr)
226 bfd *abfd;
227 struct bfd_link_info *link_info;
228 struct bfd_link_order *link_order;
229 arelent *reloc;
230 bfd_byte *data;
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)
240 case R_W65_ABS8:
241 case R_W65_DP:
243 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
244 input_section);
245 bfd_put_8 (abfd, gap, data + dst_address);
246 dst_address += 1;
247 src_address += 1;
249 break;
251 case R_W65_ABS8S8:
253 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
254 input_section);
255 gap >>= 8;
256 bfd_put_8 (abfd, gap, data + dst_address);
257 dst_address += 1;
258 src_address += 1;
260 break;
262 case R_W65_ABS8S16:
264 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
265 input_section);
266 gap >>= 16;
267 bfd_put_8 (abfd, gap, data + dst_address);
268 dst_address += 1;
269 src_address += 1;
271 break;
273 case R_W65_ABS16:
275 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
276 input_section);
278 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
279 dst_address += 2;
280 src_address += 2;
282 break;
283 case R_W65_ABS16S8:
285 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
286 input_section);
287 gap >>= 8;
288 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
289 dst_address += 2;
290 src_address += 2;
292 break;
293 case R_W65_ABS16S16:
295 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
296 input_section);
297 gap >>= 16;
298 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
299 dst_address += 2;
300 src_address += 2;
302 break;
304 case R_W65_ABS24:
306 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
307 input_section);
308 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
309 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
310 dst_address += 3;
311 src_address += 3;
313 break;
315 case R_W65_PCR8:
317 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
318 input_section);
319 bfd_vma dot = (dst_address
320 + input_section->output_offset
321 + input_section->output_section->vma);
323 gap -= dot + 1;
324 if (gap < -128 || gap > 127)
326 if (! ((*link_info->callbacks->reloc_overflow)
327 (link_info, NULL,
328 bfd_asymbol_name (*reloc->sym_ptr_ptr),
329 reloc->howto->name, reloc->addend, input_section->owner,
330 input_section, reloc->address)))
331 abort ();
333 bfd_put_8 (abfd, gap, data + dst_address);
334 dst_address += 1;
335 src_address += 1;
337 break;
339 case R_W65_PCR16:
341 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
342 input_section);
343 bfd_vma dot = (dst_address
344 + input_section->output_offset
345 + input_section->output_section->vma);
347 /* This wraps within the page, so ignore the relativeness, look at the
348 high part. */
349 if ((gap & 0xf0000) != (dot & 0xf0000))
351 if (! ((*link_info->callbacks->reloc_overflow)
352 (link_info, NULL,
353 bfd_asymbol_name (*reloc->sym_ptr_ptr),
354 reloc->howto->name, reloc->addend, input_section->owner,
355 input_section, reloc->address)))
356 abort ();
359 gap -= dot + 2;
360 bfd_put_16 (abfd, gap, data + dst_address);
361 dst_address += 2;
362 src_address += 2;
364 break;
365 default:
366 printf (_("ignoring reloc %s\n"), reloc->howto->name);
367 break;
370 *src_ptr = src_address;
371 *dst_ptr = dst_address;
374 #define coff_reloc16_extra_cases w65_reloc16_extra_cases
375 #define coff_reloc16_estimate w65_reloc16_estimate
377 #include "coffcode.h"
379 #undef coff_bfd_get_relocated_section_contents
380 #undef coff_bfd_relax_section
381 #define coff_bfd_get_relocated_section_contents \
382 bfd_coff_reloc16_get_relocated_section_contents
383 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
385 CREATE_LITTLE_COFF_TARGET_VEC (w65_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)