1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright 1995, 96, 1997 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 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
30 static reloc_howto_type howto_table
[] =
32 HOWTO (R_W65_ABS8
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "abs8", true, 0x000000ff, 0x000000ff, false),
33 HOWTO (R_W65_ABS16
, 1, 0, 16, false, 0, complain_overflow_bitfield
, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
34 HOWTO (R_W65_ABS24
, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0, "abs24", true, 0x00ffffff, 0x00ffffff, false),
35 HOWTO (R_W65_ABS8S8
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, ">abs8", true, 0x000000ff, 0x000000ff, false),
36 HOWTO (R_W65_ABS8S16
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "^abs8", true, 0x000000ff, 0x000000ff, false),
37 HOWTO (R_W65_ABS16S8
, 1, 0, 16, false, 0, complain_overflow_bitfield
, 0, ">abs16", true, 0x0000ffff, 0x0000ffff, false),
38 HOWTO (R_W65_ABS16S16
,1, 0, 16, false, 0, complain_overflow_bitfield
, 0, "^abs16", true, 0x0000ffff, 0x0000ffff, false),
39 HOWTO (R_W65_PCR8
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "pcrel8", true, 0x000000ff, 0x000000ff, true),
40 HOWTO (R_W65_PCR16
, 1, 0, 16, false, 0, complain_overflow_bitfield
, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, true),
41 HOWTO (R_W65_DP
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "dp", true, 0x000000ff, 0x000000ff, false),
45 /* Turn a howto into a reloc number */
47 #define SELECT_RELOC(x,howto) \
48 { x.r_type = select_reloc(howto); }
50 #define BADMAG(x) (W65BADMAG(x))
51 #define W65 1 /* Customize coffcode.h */
52 #define __A_MAGIC_SET__
54 /* Code to swap in the reloc */
55 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
56 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
57 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
58 dst->r_stuff[0] = 'S'; \
59 dst->r_stuff[1] = 'C';
63 reloc_howto_type
*howto
;
68 /* Code to turn a r_type into a howto ptr, uses the above howto table
72 rtype2howto (internal
, dst
)
74 struct internal_reloc
*dst
;
76 internal
->howto
= howto_table
+ dst
->r_type
- 1;
79 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
81 /* Perform any necessary magic to the addend in a reloc entry */
83 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
84 cache_ptr->addend = ext_reloc.r_offset;
86 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
87 reloc_processing(relent, reloc, symbols, abfd, section)
90 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
92 struct internal_reloc
*reloc
;
97 relent
->address
= reloc
->r_vaddr
;
98 rtype2howto (relent
, reloc
);
100 if (((int) reloc
->r_symndx
) > 0)
102 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
106 relent
->sym_ptr_ptr
= (asymbol
**)&(bfd_abs_symbol
);
109 relent
->addend
= reloc
->r_offset
;
111 relent
->address
-= section
->vma
;
112 /* relent->section = 0;*/
116 h8300_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
);
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 */
176 if (-120 < (long)gap
&& (long)gap
< 120 )
179 /* Change the reloc type from 24bit, possible 8 to 8bit
181 reloc
->howto
= reloc
->howto
+ 1;
182 /* This will be two bytes smaller in the long run */
184 bfd_perform_slip(abfd
, 2, input_section
, address
);
190 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
192 dot
= input_section
->output_section
->vma
+
193 input_section
->output_offset
+ address
;
195 /* See if the address we're looking at within 127 bytes of where
196 we are, if so then we can use a small branch rather than the
197 jump we were going to */
199 gap
= value
- (dot
- shrink
);
201 if (-120 < (long)gap
&& (long)gap
< 120 )
204 /* Change the reloc type from 16bit, possible 8 to 8bit
206 reloc
->howto
= reloc
->howto
+ 1;
207 /* The place to relc moves back by one */
209 /* This will be two bytes smaller in the long run */
211 bfd_perform_slip(abfd
, 2, input_section
, address
);
219 /* First phase of a relaxing link */
223 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
224 R_JMP1 R_JMP2 jmp or pcrel branch
225 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
226 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b
231 h8300_reloc16_extra_cases (abfd
, link_info
, link_order
, reloc
, data
, src_ptr
,
234 struct bfd_link_info
*link_info
;
235 struct bfd_link_order
*link_order
;
238 unsigned int *src_ptr
;
239 unsigned int *dst_ptr
;
241 unsigned int src_address
= *src_ptr
;
242 unsigned int dst_address
= *dst_ptr
;
243 asection
*input_section
= link_order
->u
.indirect
.section
;
245 switch (reloc
->howto
->type
)
250 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
252 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
260 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
263 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
271 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
274 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
282 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
285 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
292 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
295 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
302 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
305 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
313 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
315 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
316 bfd_put_8 (abfd
, gap
>>16, data
+dst_address
+2);
324 int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
326 bfd_vma dot
= link_order
->offset
328 + link_order
->u
.indirect
.section
->output_section
->vma
;
331 if (gap
< -128 || gap
> 127) {
332 if (! ((*link_info
->callbacks
->reloc_overflow
)
333 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
334 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
335 input_section
, reloc
->address
)))
338 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
346 bfd_vma gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
348 bfd_vma dot
= link_order
->offset
350 + link_order
->u
.indirect
.section
->output_section
->vma
;
352 /* This wraps within the page, so ignore the relativeness, look at the
354 if ((gap
& 0xf0000) != (dot
& 0xf0000)) {
355 if (! ((*link_info
->callbacks
->reloc_overflow
)
356 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
357 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
358 input_section
, reloc
->address
)))
363 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
369 printf (_("ignoring reloc %s\n"), reloc
->howto
->name
);
373 *src_ptr
= src_address
;
374 *dst_ptr
= dst_address
;
378 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
379 #define coff_reloc16_estimate h8300_reloc16_estimate
381 #include "coffcode.h"
383 #undef coff_bfd_get_relocated_section_contents
384 #undef coff_bfd_relax_section
385 #define coff_bfd_get_relocated_section_contents \
386 bfd_coff_reloc16_get_relocated_section_contents
387 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
389 CREATE_LITTLE_COFF_TARGET_VEC (w65_vec
, "coff-w65", BFD_IS_RELAXABLE
, 0, '_', NULL
)