1 /* BFD back-end for Hitachi H8/300 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "coff/h8300.h"
27 #include "coff/internal.h"
30 static reloc_howto_type howto_table
[] =
32 HOWTO (R_RELBYTE
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "8", true, 0x000000ff, 0x000000ff, false),
33 HOWTO (R_RELWORD
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "16", true, 0x0000ffff, 0x0000ffff, false),
34 HOWTO (R_RELLONG
, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0, "32", true, 0xffffffff, 0xffffffff, false),
35 HOWTO (R_PCRBYTE
, 0, 0, 8, true, 0, complain_overflow_signed
, 0, "DISP8", true, 0x000000ff, 0x000000ff, true),
36 HOWTO (R_PCRWORD
, 0, 1, 16, true, 0, complain_overflow_signed
, 0, "DISP16", true, 0x0000ffff, 0x0000ffff, true),
37 HOWTO (R_PCRLONG
, 0, 2, 32, true, 0, complain_overflow_signed
, 0, "DISP32", true, 0xffffffff, 0xffffffff, true),
38 HOWTO (R_MOVB1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "16/8", true, 0x0000ffff, 0x0000ffff, false),
39 HOWTO (R_MOVB2
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "8/16", true, 0x0000ffff, 0x0000ffff, false),
40 HOWTO (R_JMP1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "16/pcrel", true, 0x0000ffff, 0x0000ffff, false),
41 HOWTO (R_JMP2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "pcrecl/16", true, 0x000000ff, 0x000000ff, false),
44 HOWTO (R_JMPL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0, "24/pcrell", true, 0x00ffffff, 0x00ffffff, false),
45 HOWTO (R_JMPL_B8
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "pc8/24", true, 0x000000ff, 0x000000ff, false),
47 HOWTO (R_MOVLB1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "24/8", true, 0x0000ffff, 0x0000ffff, false),
48 HOWTO (R_MOVLB2
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "8/24", true, 0x0000ffff, 0x0000ffff, false),
53 /* Turn a howto into a reloc number */
55 #define SELECT_RELOC(x,howto) \
56 { x.r_type = select_reloc(howto); }
58 #define BADMAG(x) (H8300BADMAG(x)&& H8300HBADMAG(x))
59 #define H8300 1 /* Customize coffcode.h */
60 #define __A_MAGIC_SET__
64 /* Code to swap in the reloc */
65 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
66 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
67 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
68 dst->r_stuff[0] = 'S'; \
69 dst->r_stuff[1] = 'C';
74 reloc_howto_type
*howto
;
79 /* Code to turn a r_type into a howto ptr, uses the above howto table
83 rtype2howto (internal
, dst
)
85 struct internal_reloc
*dst
;
90 internal
->howto
= howto_table
+ 0;
93 internal
->howto
= howto_table
+ 1;
96 internal
->howto
= howto_table
+ 2;
99 internal
->howto
= howto_table
+ 3;
102 internal
->howto
= howto_table
+ 4;
105 internal
->howto
= howto_table
+ 5;
108 internal
->howto
= howto_table
+ 6;
111 internal
->howto
= howto_table
+ 7;
114 internal
->howto
= howto_table
+ 8;
117 internal
->howto
= howto_table
+ 9;
120 internal
->howto
= howto_table
+ 10;
123 internal
->howto
= howto_table
+ 11;
126 internal
->howto
= howto_table
+ 12;
129 internal
->howto
= howto_table
+ 13;
132 fprintf (stderr
, "Bad reloc\n");
137 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
140 /* Perform any necessaru magic to the addend in a reloc entry */
143 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
144 cache_ptr->addend = ext_reloc.r_offset;
147 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
148 reloc_processing(relent, reloc, symbols, abfd, section)
151 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
153 struct internal_reloc
*reloc
;
158 relent
->address
= reloc
->r_vaddr
;
159 rtype2howto (relent
, reloc
);
161 if (((int) reloc
->r_symndx
) > 0)
163 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
167 relent
->sym_ptr_ptr
= &(bfd_abs_symbol
);
172 relent
->addend
= reloc
->r_offset
;
174 relent
->address
-= section
->vma
;
175 /* relent->section = 0;*/
180 h8300_reloc16_estimate(abfd
, input_section
, reloc
, shrink
, link_info
)
182 asection
*input_section
;
185 struct bfd_link_info
*link_info
;
191 /* The address of the thing to be relocated will have moved back by
192 the size of the shrink - but we don't change reloc->address here,
193 since we need it to know where the relocation lives in the source
196 /* reloc->address -= shrink; conceptual */
198 bfd_vma address
= reloc
->address
- shrink
;
201 switch (reloc
->howto
->type
)
208 /* Thing is a move one byte */
210 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
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 */
219 /* This will be two bytes smaller in the long run */
221 bfd_perform_slip(abfd
, 2, input_section
, address
);
225 /* This is the 24 bit branch which could become an 8 bitter,
226 the relocation points to the first byte of the insn, not the
230 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
232 dot
= input_section
->output_section
->vma
+
233 input_section
->output_offset
+ address
;
235 /* See if the address we're looking at within 127 bytes of where
236 we are, if so then we can use a small branch rather than the
237 jump we were going to */
241 if (-120 < (long)gap
&& (long)gap
< 120 )
244 /* Change the reloc type from 24bit, possible 8 to 8bit
246 reloc
->howto
= reloc
->howto
+ 1;
247 /* This will be two bytes smaller in the long run */
249 bfd_perform_slip(abfd
, 2, input_section
, address
);
255 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
257 dot
= input_section
->output_section
->vma
+
258 input_section
->output_offset
+ address
;
260 /* See if the address we're looking at within 127 bytes of where
261 we are, if so then we can use a small branch rather than the
262 jump we were going to */
264 gap
= value
- (dot
- shrink
);
267 if (-120 < (long)gap
&& (long)gap
< 120 )
270 /* Change the reloc type from 16bit, possible 8 to 8bit
272 reloc
->howto
= reloc
->howto
+ 1;
273 /* The place to relc moves back by one */
275 /* This will be two bytes smaller in the long run */
277 bfd_perform_slip(abfd
, 2, input_section
, address
);
287 /* First phase of a relaxing link */
291 R_MOVB1 R_MOVB2 mov.b with 16bit or 8 bit address
292 R_JMP1 R_JMP2 jmp or pcrel branch
293 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
294 R_MOVLB1 R_MOVLB2 24 or 8 bit reloc for mov.b
299 h8300_reloc16_extra_cases (abfd
, link_info
, link_order
, reloc
, data
, src_ptr
,
302 struct bfd_link_info
*link_info
;
303 struct bfd_link_order
*link_order
;
306 unsigned int *src_ptr
;
307 unsigned int *dst_ptr
;
309 unsigned int src_address
= *src_ptr
;
310 unsigned int dst_address
= *dst_ptr
;
311 asection
*input_section
= link_order
->u
.indirect
.section
;
313 switch (reloc
->howto
->type
)
315 /* A 24 bit branch which could be a 8 bit pcrel, really pointing to
316 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
319 bfd_vma dot
= link_order
->offset
321 + link_order
->u
.indirect
.section
->output_section
->vma
;
322 int gap
= (bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
)
324 if (gap
> 127 || gap
< -128)
326 if (! ((*link_info
->callbacks
->reloc_overflow
)
327 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
328 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
329 input_section
, reloc
->address
)))
333 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
341 bfd_vma dot
= link_order
->offset
343 + link_order
->u
.indirect
.section
->output_section
->vma
;
344 int gap
= (bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
)
346 if (gap
> 32767 || gap
< -32768)
348 if (! ((*link_info
->callbacks
->reloc_overflow
)
349 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
350 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
351 input_section
, reloc
->address
)))
355 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
364 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, link_info
,
366 if (gap
> 0xff && gap
< ~0xff)
368 if (! ((*link_info
->callbacks
->reloc_overflow
)
369 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
370 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
371 input_section
, reloc
->address
)))
375 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
383 /* A relword which would have like to have been a pcrel */
385 /* A relword which would like to have been modified but
389 bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
),
396 bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
),
403 /* Special relaxed type, there will be a gap between where we
404 get stuff from and where we put stuff to now
406 for a mov.b @aa:16 -> mov.b @aa:8
407 opcode 0x6a 0x0y offset
410 if (data
[dst_address
- 1] != 0x6a)
412 switch (data
[src_address
] & 0xf0)
416 data
[dst_address
- 1] = (data
[src_address
] & 0xf) | 0x20;
420 data
[dst_address
- 1] = (data
[src_address
] & 0xf) | 0x30;
426 /* the offset must fit ! after all, what was all the relaxing
430 bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
),
433 /* Note the magic - src goes up by two bytes, but dst by only
442 /* Speciial relaxed type */
444 bfd_vma dot
= link_order
->offset
446 + link_order
->u
.indirect
.section
->output_section
->vma
;
448 int gap
= (bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
)
451 if ((gap
& ~0xff) != 0 && ((gap
& 0xff00) != 0xff00))
454 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
456 switch (data
[dst_address
- 1])
460 bfd_put_8 (abfd
, 0x55, data
+ dst_address
- 1);
464 bfd_put_8 (abfd
, 0x40, data
+ dst_address
- 1);
477 case R_JMPL_B8
: /* 24 bit branch which is now 8 bits */
479 /* Speciial relaxed type */
481 bfd_vma dot
= link_order
->offset
483 + link_order
->u
.indirect
.section
->output_section
->vma
;
485 int gap
= (bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
)
488 if ((gap
& ~0xff) != 0 && ((gap
& 0xff00) != 0xff00))
491 switch (data
[src_address
])
495 bfd_put_8 (abfd
, 0x55, data
+ dst_address
);
499 bfd_put_8 (abfd
, 0x40, data
+ dst_address
);
503 bfd_put_8 (abfd
, 0xde, data
+ dst_address
);
507 bfd_put_8 (abfd
, gap
, data
+ dst_address
+ 1);
516 int v
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
517 int o
= bfd_get_32 (abfd
, data
+ src_address
);
518 v
= (v
& 0x00ffffff) | (o
& 0xff000000);
519 bfd_put_32 (abfd
, v
, data
+ dst_address
);
527 /* A 24 bit mov which could be an 8 bit move, really pointing to
528 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
531 int v
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
532 int o
= bfd_get_32 (abfd
, data
+ dst_address
);
533 v
= (v
& 0x00ffffff) | (o
& 0xff000000);
534 bfd_put_32 (abfd
, v
, data
+ dst_address
);
546 *src_ptr
= src_address
;
547 *dst_ptr
= dst_address
;
551 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
552 #define coff_reloc16_estimate h8300_reloc16_estimate
554 #include "coffcode.h"
557 #undef coff_bfd_get_relocated_section_contents
558 #undef coff_bfd_relax_section
559 #define coff_bfd_get_relocated_section_contents \
560 bfd_coff_reloc16_get_relocated_section_contents
561 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
565 const bfd_target h8300coff_vec
=
567 "coff-h8300", /* name */
568 bfd_target_coff_flavour
,
569 true, /* data byte order is big */
570 true, /* header byte order is big */
572 (HAS_RELOC
| EXEC_P
| /* object flags */
573 HAS_LINENO
| HAS_DEBUG
|
574 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| BFD_IS_RELAXABLE
),
575 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
576 '_', /* leading char */
577 '/', /* ar_pad_char */
578 15, /* ar_max_namelen */
579 1, /* minimum section alignment */
580 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
581 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
582 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
583 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
584 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
585 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
587 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
588 bfd_generic_archive_p
, _bfd_dummy_target
},
589 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
591 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
592 _bfd_write_archive_contents
, bfd_false
},
594 BFD_JUMP_TABLE_GENERIC (coff
),
595 BFD_JUMP_TABLE_COPY (coff
),
596 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
597 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
598 BFD_JUMP_TABLE_SYMBOLS (coff
),
599 BFD_JUMP_TABLE_RELOCS (coff
),
600 BFD_JUMP_TABLE_WRITE (coff
),
601 BFD_JUMP_TABLE_LINK (coff
),
602 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),