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),
46 /* Turn a howto into a reloc number */
48 #define SELECT_RELOC(x,howto) \
49 { x.r_type = select_reloc(howto); }
51 #define BADMAG(x) (W65BADMAG(x))
52 #define W65 1 /* Customize coffcode.h */
53 #define __A_MAGIC_SET__
56 /* Code to swap in the reloc */
57 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
58 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
59 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
60 dst->r_stuff[0] = 'S'; \
61 dst->r_stuff[1] = 'C';
66 reloc_howto_type
*howto
;
71 /* Code to turn a r_type into a howto ptr, uses the above howto table
75 rtype2howto (internal
, dst
)
77 struct internal_reloc
*dst
;
79 internal
->howto
= howto_table
+ dst
->r_type
- 1;
82 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
85 /* 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;
92 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
93 reloc_processing(relent, reloc, symbols, abfd, section)
96 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
98 struct internal_reloc
*reloc
;
103 relent
->address
= reloc
->r_vaddr
;
104 rtype2howto (relent
, reloc
);
106 if (((int) reloc
->r_symndx
) > 0)
108 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
112 relent
->sym_ptr_ptr
= (asymbol
**)&(bfd_abs_symbol
);
117 relent
->addend
= reloc
->r_offset
;
119 relent
->address
-= section
->vma
;
120 /* relent->section = 0;*/
125 h8300_reloc16_estimate(abfd
, input_section
, reloc
, shrink
, link_info
)
127 asection
*input_section
;
130 struct bfd_link_info
*link_info
;
136 /* The address of the thing to be relocated will have moved back by
137 the size of the shrink - but we don't change reloc->address here,
138 since we need it to know where the relocation lives in the source
141 /* reloc->address -= shrink; conceptual */
143 bfd_vma address
= reloc
->address
- shrink
;
146 switch (reloc
->howto
->type
)
153 /* Thing is a move one byte */
155 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
160 /* Change the reloc type from 16bit, possible 8 to 8bit
162 reloc
->howto
= reloc
->howto
+ 1;
163 /* The place to relc moves back by one */
164 /* This will be two bytes smaller in the long run */
166 bfd_perform_slip(abfd
, 2, input_section
, address
);
170 /* This is the 24 bit branch which could become an 8 bitter,
171 the relocation points to the first byte of the insn, not the
175 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
177 dot
= input_section
->output_section
->vma
+
178 input_section
->output_offset
+ address
;
180 /* See if the address we're looking at within 127 bytes of where
181 we are, if so then we can use a small branch rather than the
182 jump we were going to */
186 if (-120 < (long)gap
&& (long)gap
< 120 )
189 /* Change the reloc type from 24bit, possible 8 to 8bit
191 reloc
->howto
= reloc
->howto
+ 1;
192 /* This will be two bytes smaller in the long run */
194 bfd_perform_slip(abfd
, 2, input_section
, address
);
200 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
202 dot
= input_section
->output_section
->vma
+
203 input_section
->output_offset
+ address
;
205 /* See if the address we're looking at within 127 bytes of where
206 we are, if so then we can use a small branch rather than the
207 jump we were going to */
209 gap
= value
- (dot
- shrink
);
212 if (-120 < (long)gap
&& (long)gap
< 120 )
215 /* Change the reloc type from 16bit, possible 8 to 8bit
217 reloc
->howto
= reloc
->howto
+ 1;
218 /* The place to relc moves back by one */
220 /* This will be two bytes smaller in the long run */
222 bfd_perform_slip(abfd
, 2, input_section
, address
);
232 /* First phase of a relaxing link */
236 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
237 R_JMP1 R_JMP2 jmp or pcrel branch
238 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
239 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b
244 h8300_reloc16_extra_cases (abfd
, link_info
, link_order
, reloc
, data
, src_ptr
,
247 struct bfd_link_info
*link_info
;
248 struct bfd_link_order
*link_order
;
251 unsigned int *src_ptr
;
252 unsigned int *dst_ptr
;
254 unsigned int src_address
= *src_ptr
;
255 unsigned int dst_address
= *dst_ptr
;
256 asection
*input_section
= link_order
->u
.indirect
.section
;
258 switch (reloc
->howto
->type
)
263 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
265 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
273 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
276 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
284 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
287 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
295 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
298 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
305 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
308 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
315 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
318 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
326 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
328 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
329 bfd_put_8 (abfd
, gap
>>16, data
+dst_address
+2);
337 int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
339 bfd_vma dot
= link_order
->offset
341 + link_order
->u
.indirect
.section
->output_section
->vma
;
344 if (gap
< -128 || gap
> 127) {
345 if (! ((*link_info
->callbacks
->reloc_overflow
)
346 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
347 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
348 input_section
, reloc
->address
)))
351 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
359 bfd_vma gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
361 bfd_vma dot
= link_order
->offset
363 + link_order
->u
.indirect
.section
->output_section
->vma
;
366 /* This wraps within the page, so ignore the relativeness, look at the
368 if ((gap
& 0xf0000) != (dot
& 0xf0000)) {
369 if (! ((*link_info
->callbacks
->reloc_overflow
)
370 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
371 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
372 input_section
, reloc
->address
)))
377 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
383 printf(_("ignoring reloc %s\n"), reloc
->howto
->name
);
387 *src_ptr
= src_address
;
388 *dst_ptr
= dst_address
;
392 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
393 #define coff_reloc16_estimate h8300_reloc16_estimate
395 #include "coffcode.h"
398 #undef coff_bfd_get_relocated_section_contents
399 #undef coff_bfd_relax_section
400 #define coff_bfd_get_relocated_section_contents \
401 bfd_coff_reloc16_get_relocated_section_contents
402 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
408 "coff-w65", /* name */
409 bfd_target_coff_flavour
,
410 BFD_ENDIAN_LITTLE
, /* data byte order is little */
411 BFD_ENDIAN_LITTLE
, /* header byte order is little */
413 (HAS_RELOC
| EXEC_P
| /* object flags */
414 HAS_LINENO
| HAS_DEBUG
|
415 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| BFD_IS_RELAXABLE
),
416 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
417 '_', /* leading char */
418 '/', /* ar_pad_char */
419 15, /* ar_max_namelen */
420 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
421 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
422 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
423 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
424 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
425 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
427 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
428 bfd_generic_archive_p
, _bfd_dummy_target
},
429 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
431 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
432 _bfd_write_archive_contents
, bfd_false
},
434 BFD_JUMP_TABLE_GENERIC (coff
),
435 BFD_JUMP_TABLE_COPY (coff
),
436 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
437 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
438 BFD_JUMP_TABLE_SYMBOLS (coff
),
439 BFD_JUMP_TABLE_RELOCS (coff
),
440 BFD_JUMP_TABLE_WRITE (coff
),
441 BFD_JUMP_TABLE_LINK (coff
),
442 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),