* dwarf2dbg.c: Always include dwarf2dbg.h.
[binutils.git] / bfd / coff-w65.c
blob774d270d22b5d2530c4d7366cc20104dedb927d7
1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright 1995, 1996, 1997, 1999, 2000, 2001 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "bfdlink.h"
25 #include "coff/w65.h"
26 #include "coff/internal.h"
27 #include "libcoff.h"
29 static int select_reloc PARAMS ((reloc_howto_type *));
30 static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
31 static void reloc_processing PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
32 static int w65_reloc16_estimate PARAMS ((bfd *, asection *, arelent *, unsigned int, struct bfd_link_info *));
33 static void w65_reloc16_extra_cases PARAMS ((bfd *,struct bfd_link_info *, struct bfd_link_order *, arelent *, bfd_byte *, unsigned int *, unsigned int *));
35 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
36 static reloc_howto_type howto_table[] =
38 HOWTO (R_W65_ABS8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "abs8", true, 0x000000ff, 0x000000ff, false),
39 HOWTO (R_W65_ABS16, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
40 HOWTO (R_W65_ABS24, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "abs24", true, 0x00ffffff, 0x00ffffff, false),
41 HOWTO (R_W65_ABS8S8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, ">abs8", true, 0x000000ff, 0x000000ff, false),
42 HOWTO (R_W65_ABS8S16, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "^abs8", true, 0x000000ff, 0x000000ff, false),
43 HOWTO (R_W65_ABS16S8, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, ">abs16", true, 0x0000ffff, 0x0000ffff, false),
44 HOWTO (R_W65_ABS16S16,1, 0, 16, false, 0, complain_overflow_bitfield, 0, "^abs16", true, 0x0000ffff, 0x0000ffff, false),
45 HOWTO (R_W65_PCR8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "pcrel8", true, 0x000000ff, 0x000000ff, true),
46 HOWTO (R_W65_PCR16, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, true),
47 HOWTO (R_W65_DP, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "dp", true, 0x000000ff, 0x000000ff, false),
50 /* Turn a howto into a reloc number. */
52 #define SELECT_RELOC(x,howto) \
53 { x.r_type = select_reloc(howto); }
55 #define BADMAG(x) (W65BADMAG(x))
56 #define W65 1 /* Customize coffcode.h */
57 #define __A_MAGIC_SET__
59 /* Code to swap in the reloc */
60 #define SWAP_IN_RELOC_OFFSET H_GET_32
61 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
62 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
63 dst->r_stuff[0] = 'S'; \
64 dst->r_stuff[1] = 'C';
66 static int
67 select_reloc (howto)
68 reloc_howto_type *howto;
70 return howto->type ;
73 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
75 static void
76 rtype2howto (internal, dst)
77 arelent *internal;
78 struct internal_reloc *dst;
80 internal->howto = howto_table + dst->r_type - 1;
83 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
85 /* Perform any necessary magic to the addend in a reloc entry. */
87 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
88 cache_ptr->addend = ext_reloc.r_offset;
90 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
91 reloc_processing(relent, reloc, symbols, abfd, section)
93 static void
94 reloc_processing (relent, reloc, symbols, abfd, section)
95 arelent * relent;
96 struct internal_reloc *reloc;
97 asymbol ** symbols;
98 bfd * abfd;
99 asection * section;
101 relent->address = reloc->r_vaddr;
102 rtype2howto (relent, reloc);
104 if (((int) reloc->r_symndx) > 0)
105 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
106 else
107 relent->sym_ptr_ptr = (asymbol **)&(bfd_abs_symbol);
109 relent->addend = reloc->r_offset;
111 relent->address -= section->vma;
112 /* relent->section = 0;*/
115 static int
116 w65_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
117 bfd *abfd;
118 asection *input_section;
119 arelent *reloc;
120 unsigned int shrink;
121 struct bfd_link_info *link_info;
123 bfd_vma value;
124 bfd_vma dot;
125 bfd_vma gap;
127 /* The address of the thing to be relocated will have moved back by
128 the size of the shrink - but we don't change reloc->address here,
129 since we need it to know where the relocation lives in the source
130 uncooked section. */
132 /* reloc->address -= shrink; conceptual */
134 bfd_vma address = reloc->address - shrink;
136 switch (reloc->howto->type)
138 case R_MOV16B2:
139 case R_JMP2:
140 shrink+=2;
141 break;
143 /* Thing is a move one byte. */
144 case R_MOV16B1:
145 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
147 if (value >= 0xff00)
149 /* Change the reloc type from 16bit, possible 8 to 8bit
150 possible 16. */
151 reloc->howto = reloc->howto + 1;
152 /* The place to relc moves back by one. */
153 /* This will be two bytes smaller in the long run. */
154 shrink += 2;
155 bfd_perform_slip (abfd, 2, input_section, address);
158 break;
159 /* This is the 24 bit branch which could become an 8 bitter,
160 the relocation points to the first byte of the insn, not the
161 actual data. */
163 case R_JMPL1:
164 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
166 dot = input_section->output_section->vma +
167 input_section->output_offset + address;
169 /* See if the address we're looking at within 127 bytes of where
170 we are, if so then we can use a small branch rather than the
171 jump we were going to. */
172 gap = value - dot;
174 if (-120 < (long) gap && (long) gap < 120)
176 /* Change the reloc type from 24bit, possible 8 to 8bit
177 possible 32. */
178 reloc->howto = reloc->howto + 1;
179 /* This will be two bytes smaller in the long run. */
180 shrink += 2;
181 bfd_perform_slip (abfd, 2, input_section, address);
183 break;
185 case R_JMP1:
186 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
188 dot = input_section->output_section->vma +
189 input_section->output_offset + address;
191 /* See if the address we're looking at within 127 bytes of where
192 we are, if so then we can use a small branch rather than the
193 jump we were going to. */
194 gap = value - (dot - shrink);
196 if (-120 < (long) gap && (long) gap < 120)
198 /* Change the reloc type from 16bit, possible 8 to 8bit
199 possible 16. */
200 reloc->howto = reloc->howto + 1;
201 /* The place to relc moves back by one. */
203 /* This will be two bytes smaller in the long run. */
204 shrink += 2;
205 bfd_perform_slip (abfd, 2, input_section, address);
207 break;
210 return shrink;
213 /* First phase of a relaxing link. */
215 /* Reloc types
216 large small
217 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
218 R_JMP1 R_JMP2 jmp or pcrel branch
219 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
220 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
222 static void
223 w65_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
224 dst_ptr)
225 bfd *abfd;
226 struct bfd_link_info *link_info;
227 struct bfd_link_order *link_order;
228 arelent *reloc;
229 bfd_byte *data;
230 unsigned int *src_ptr;
231 unsigned int *dst_ptr;
233 unsigned int src_address = *src_ptr;
234 unsigned int dst_address = *dst_ptr;
235 asection *input_section = link_order->u.indirect.section;
237 switch (reloc->howto->type)
239 case R_W65_ABS8:
240 case R_W65_DP:
242 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
243 input_section);
244 bfd_put_8 (abfd, gap, data + dst_address);
245 dst_address += 1;
246 src_address += 1;
248 break;
250 case R_W65_ABS8S8:
252 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
253 input_section);
254 gap >>= 8;
255 bfd_put_8 (abfd, gap, data + dst_address);
256 dst_address += 1;
257 src_address += 1;
259 break;
261 case R_W65_ABS8S16:
263 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
264 input_section);
265 gap >>= 16;
266 bfd_put_8 (abfd, gap, data + dst_address);
267 dst_address += 1;
268 src_address += 1;
270 break;
272 case R_W65_ABS16:
274 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
275 input_section);
277 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
278 dst_address += 2;
279 src_address += 2;
281 break;
282 case R_W65_ABS16S8:
284 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
285 input_section);
286 gap >>= 8;
287 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
288 dst_address += 2;
289 src_address += 2;
291 break;
292 case R_W65_ABS16S16:
294 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
295 input_section);
296 gap >>= 16;
297 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
298 dst_address += 2;
299 src_address += 2;
301 break;
303 case R_W65_ABS24:
305 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
306 input_section);
307 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
308 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
309 dst_address += 3;
310 src_address += 3;
312 break;
314 case R_W65_PCR8:
316 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
317 input_section);
318 bfd_vma dot = link_order->offset
319 + dst_address
320 + link_order->u.indirect.section->output_section->vma;
322 gap -= dot + 1;
323 if (gap < -128 || gap > 127)
325 if (! ((*link_info->callbacks->reloc_overflow)
326 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
327 reloc->howto->name, reloc->addend, input_section->owner,
328 input_section, reloc->address)))
329 abort ();
331 bfd_put_8 (abfd, gap, data + dst_address);
332 dst_address += 1;
333 src_address += 1;
335 break;
337 case R_W65_PCR16:
339 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
340 input_section);
341 bfd_vma dot = link_order->offset
342 + dst_address
343 + link_order->u.indirect.section->output_section->vma;
345 /* This wraps within the page, so ignore the relativeness, look at the
346 high part. */
347 if ((gap & 0xf0000) != (dot & 0xf0000))
349 if (! ((*link_info->callbacks->reloc_overflow)
350 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
351 reloc->howto->name, reloc->addend, input_section->owner,
352 input_section, reloc->address)))
353 abort ();
356 gap -= dot + 2;
357 bfd_put_16 (abfd, gap, data + dst_address);
358 dst_address += 2;
359 src_address += 2;
361 break;
362 default:
363 printf (_("ignoring reloc %s\n"), reloc->howto->name);
364 break;
367 *src_ptr = src_address;
368 *dst_ptr = dst_address;
371 #define coff_reloc16_extra_cases w65_reloc16_extra_cases
372 #define coff_reloc16_estimate w65_reloc16_estimate
374 #include "coffcode.h"
376 #undef coff_bfd_get_relocated_section_contents
377 #undef coff_bfd_relax_section
378 #define coff_bfd_get_relocated_section_contents \
379 bfd_coff_reloc16_get_relocated_section_contents
380 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
382 CREATE_LITTLE_COFF_TARGET_VEC (w65_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL)