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. */
26 #include "coff/internal.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';
68 reloc_howto_type
*howto
;
73 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
76 rtype2howto (internal
, dst
)
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)
94 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
96 struct internal_reloc
*reloc
;
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
];
107 relent
->sym_ptr_ptr
= (asymbol
**)&(bfd_abs_symbol
);
109 relent
->addend
= reloc
->r_offset
;
111 relent
->address
-= section
->vma
;
112 /* relent->section = 0;*/
116 w65_reloc16_estimate (abfd
, input_section
, reloc
, shrink
, link_info
)
118 asection
*input_section
;
121 struct bfd_link_info
*link_info
;
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
132 /* reloc->address -= shrink; conceptual */
134 bfd_vma address
= reloc
->address
- shrink
;
136 switch (reloc
->howto
->type
)
143 /* Thing is a move one byte. */
145 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
149 /* Change the reloc type from 16bit, possible 8 to 8bit
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. */
155 bfd_perform_slip (abfd
, 2, input_section
, address
);
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
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. */
174 if (-120 < (long) gap
&& (long) gap
< 120)
176 /* Change the reloc type from 24bit, possible 8 to 8bit
178 reloc
->howto
= reloc
->howto
+ 1;
179 /* This will be two bytes smaller in the long run. */
181 bfd_perform_slip (abfd
, 2, input_section
, address
);
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
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. */
205 bfd_perform_slip (abfd
, 2, input_section
, address
);
213 /* First phase of a relaxing link. */
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 */
223 w65_reloc16_extra_cases (abfd
, link_info
, link_order
, reloc
, data
, src_ptr
,
226 struct bfd_link_info
*link_info
;
227 struct bfd_link_order
*link_order
;
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
)
242 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_8 (abfd
, gap
, data
+ dst_address
);
274 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
277 bfd_put_16 (abfd
, (bfd_vma
) gap
, data
+ dst_address
);
284 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
287 bfd_put_16 (abfd
, (bfd_vma
) gap
, data
+ dst_address
);
294 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
297 bfd_put_16 (abfd
, (bfd_vma
) gap
, data
+ dst_address
);
305 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
307 bfd_put_16 (abfd
, (bfd_vma
) gap
, data
+ dst_address
);
308 bfd_put_8 (abfd
, gap
>> 16, data
+dst_address
+ 2);
316 int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
318 bfd_vma dot
= link_order
->offset
320 + link_order
->u
.indirect
.section
->output_section
->vma
;
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
)))
331 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
339 bfd_vma gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
341 bfd_vma dot
= link_order
->offset
343 + link_order
->u
.indirect
.section
->output_section
->vma
;
345 /* This wraps within the page, so ignore the relativeness, look at the
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
)))
357 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
363 printf (_("ignoring reloc %s\n"), reloc
->howto
->name
);
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
)