1 /* BFD back-end for Hitachi H8/300 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993 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. */
25 #include "coff/h8300.h"
26 #include "coff/internal.h"
29 extern bfd_error_vector_type bfd_error_vector
;
31 static reloc_howto_type howto_table
[] =
33 HOWTO (R_RELBYTE
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "8", true, 0x000000ff, 0x000000ff, false),
34 HOWTO (R_RELWORD
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "16", true, 0x0000ffff, 0x0000ffff, false),
35 HOWTO (R_RELLONG
, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0, "32", true, 0xffffffff, 0xffffffff, false),
36 HOWTO (R_PCRBYTE
, 0, 0, 8, true, 0, complain_overflow_signed
, 0, "DISP8", true, 0x000000ff, 0x000000ff, true),
37 HOWTO (R_PCRWORD
, 0, 1, 16, true, 0, complain_overflow_signed
, 0, "DISP16", true, 0x0000ffff, 0x0000ffff, true),
38 HOWTO (R_PCRLONG
, 0, 2, 32, true, 0, complain_overflow_signed
, 0, "DISP32", true, 0xffffffff, 0xffffffff, true),
39 HOWTO (R_MOVB1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "16/8", true, 0x0000ffff, 0x0000ffff, false),
40 HOWTO (R_MOVB2
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "8/16", true, 0x0000ffff, 0x0000ffff, false),
41 HOWTO (R_JMP1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "16/pcrel", true, 0x0000ffff, 0x0000ffff, false),
42 HOWTO (R_JMP2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "pcrecl/16", true, 0x000000ff, 0x000000ff, false),
45 HOWTO (R_JMPL1
, 0, 1, 32, false, 0, complain_overflow_bitfield
, 0, "24/pcrell", true, 0x0000ffff, 0x0000ffff, false),
46 HOWTO (R_JMPL_B8
, 0, 0, 8, false, 0, complain_overflow_bitfield
, 0, "pc8/24", true, 0x000000ff, 0x000000ff, false),
48 HOWTO (R_MOVLB1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "24/8", true, 0x0000ffff, 0x0000ffff, false),
49 HOWTO (R_MOVLB2
, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "8/24", true, 0x0000ffff, 0x0000ffff, false),
54 /* Turn a howto into a reloc number */
56 #define SELECT_RELOC(x,howto) \
57 { x = select_reloc(howto); }
59 #define BADMAG(x) (H8300BADMAG(x)&& H8300HBADMAG(x))
60 #define H8300 1 /* Customize coffcode.h */
61 #define __A_MAGIC_SET__
65 /* Code to swap in the reloc */
66 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
67 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
68 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
69 dst->r_stuff[0] = 'S'; \
70 dst->r_stuff[1] = 'C';
75 reloc_howto_type
*howto
;
80 /* Code to turn a r_type into a howto ptr, uses the above howto table
84 rtype2howto (internal
, dst
)
86 struct internal_reloc
*dst
;
91 internal
->howto
= howto_table
+ 0;
94 internal
->howto
= howto_table
+ 1;
97 internal
->howto
= howto_table
+ 2;
100 internal
->howto
= howto_table
+ 3;
103 internal
->howto
= howto_table
+ 4;
106 internal
->howto
= howto_table
+ 5;
109 internal
->howto
= howto_table
+ 6;
112 internal
->howto
= howto_table
+ 7;
115 internal
->howto
= howto_table
+ 8;
118 internal
->howto
= howto_table
+ 9;
121 internal
->howto
= howto_table
+ 10;
124 internal
->howto
= howto_table
+ 11;
127 internal
->howto
= howto_table
+ 12;
130 internal
->howto
= howto_table
+ 13;
133 fprintf (stderr
, "Bad reloc\n");
138 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
141 /* Perform any necessaru magic to the addend in a reloc entry */
144 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
145 cache_ptr->addend = ext_reloc.r_offset;
148 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
149 reloc_processing(relent, reloc, symbols, abfd, section)
152 DEFUN (reloc_processing
, (relent
, reloc
, symbols
, abfd
, section
),
154 struct internal_reloc
*reloc AND
155 asymbol
** symbols AND
159 relent
->address
= reloc
->r_vaddr
;
160 rtype2howto (relent
, reloc
);
162 if (((int) reloc
->r_symndx
) > 0)
164 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
168 relent
->sym_ptr_ptr
= &(bfd_abs_symbol
);
173 relent
->addend
= reloc
->r_offset
;
175 relent
->address
-= section
->vma
;
176 /* relent->section = 0;*/
181 h8300_reloc16_estimate(input_section
, symbols
, reloc
, shrink
)
182 asection
*input_section
;
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
,0);
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(symbols
, 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
, 0);
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(symbols
, 2, input_section
, address
);
255 value
= bfd_coff_reloc16_get_value(reloc
, 0);
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(symbols
, 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
, seclet
, reloc
, data
, src_ptr
, dst_ptr
)
301 struct bfd_seclet
*seclet
;
304 unsigned int *src_ptr
;
305 unsigned int *dst_ptr
;
307 unsigned int src_address
= *src_ptr
;
308 unsigned int dst_address
= *dst_ptr
;
310 switch (reloc
->howto
->type
)
312 /* A 24 bit branch which could be a 8 bit pcrel, really pointing to
313 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
316 bfd_vma dot
= seclet
->offset
318 + seclet
->u
.indirect
.section
->output_section
->vma
;
319 int gap
= bfd_coff_reloc16_get_value (reloc
, seclet
) - dot
;
320 if (gap
> 127 || gap
< -128)
322 bfd_error_vector
.reloc_value_truncated (reloc
, seclet
);
325 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
334 unsigned int gap
= bfd_coff_reloc16_get_value (reloc
, seclet
);
335 if (gap
> 0xff && gap
< ~0xff)
337 bfd_error_vector
.reloc_value_truncated (reloc
, seclet
);
340 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
348 /* A relword which would have like to have been a pcrel */
350 /* A relword which would like to have been modified but
353 bfd_put_16 (abfd
, bfd_coff_reloc16_get_value (reloc
, seclet
),
359 bfd_put_32 (abfd
, bfd_coff_reloc16_get_value (reloc
, seclet
),
366 /* Special relaxed type, there will be a gap between where we
367 get stuff from and where we put stuff to now
369 for a mov.b @aa:16 -> mov.b @aa:8
370 opcode 0x6a 0x0y offset
373 if (data
[dst_address
- 1] != 0x6a)
375 switch (data
[src_address
] & 0xf0)
379 data
[dst_address
- 1] = (data
[src_address
] & 0xf) | 0x20;
383 data
[dst_address
- 1] = (data
[src_address
] & 0xf) | 0x30;
389 /* the offset must fit ! after all, what was all the relaxing
392 bfd_put_8 (abfd
, bfd_coff_reloc16_get_value (reloc
, seclet
),
395 /* Note the magic - src goes up by two bytes, but dst by only
404 /* Speciial relaxed type */
406 bfd_vma dot
= seclet
->offset
408 + seclet
->u
.indirect
.section
->output_section
->vma
;
410 int gap
= bfd_coff_reloc16_get_value (reloc
, seclet
) - dot
- 1;
412 if ((gap
& ~0xff) != 0 && ((gap
& 0xff00) != 0xff00))
415 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
417 switch (data
[dst_address
- 1])
421 bfd_put_8 (abfd
, 0x55, data
+ dst_address
- 1);
425 bfd_put_8 (abfd
, 0x40, data
+ dst_address
- 1);
438 case R_JMPL_B8
: /* 24 bit branch which is now 8 bits */
440 /* Speciial relaxed type */
442 bfd_vma dot
= seclet
->offset
444 + seclet
->u
.indirect
.section
->output_section
->vma
;
446 int gap
= bfd_coff_reloc16_get_value (reloc
, seclet
) - dot
- 2;
448 if ((gap
& ~0xff) != 0 && ((gap
& 0xff00) != 0xff00))
451 switch (data
[src_address
])
455 bfd_put_8 (abfd
, 0x55, data
+ dst_address
);
459 bfd_put_8 (abfd
, 0x40, data
+ dst_address
);
463 bfd_put_8 (abfd
, 0xde, data
+ dst_address
);
467 bfd_put_8 (abfd
, gap
, data
+ dst_address
+ 1);
476 int v
= bfd_coff_reloc16_get_value (reloc
, seclet
);
477 int o
= bfd_get_32 (abfd
, data
+ src_address
);
478 v
= (v
& 0x00ffffff) | (o
& 0xff000000);
479 bfd_put_32 (abfd
, v
, data
+ dst_address
);
487 /* A 24 bit mov which could be an 8 bit move, really pointing to
488 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
491 int v
= bfd_coff_reloc16_get_value (reloc
, seclet
);
492 int o
= bfd_get_32 (abfd
, data
+ dst_address
);
493 v
= (v
& 0x00ffffff) | (o
& 0xff000000);
494 bfd_put_32 (abfd
, v
, data
+ dst_address
);
506 *src_ptr
= src_address
;
507 *dst_ptr
= dst_address
;
511 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
512 #define coff_reloc16_estimate h8300_reloc16_estimate
514 #include "coffcode.h"
517 #undef coff_bfd_get_relocated_section_contents
518 #undef coff_bfd_relax_section
519 #define coff_bfd_get_relocated_section_contents bfd_coff_reloc16_get_relocated_section_contents
520 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
524 bfd_target h8300coff_vec
=
526 "coff-h8300", /* name */
527 bfd_target_coff_flavour
,
528 true, /* data byte order is big */
529 true, /* header byte order is big */
531 (HAS_RELOC
| EXEC_P
| /* object flags */
532 HAS_LINENO
| HAS_DEBUG
|
533 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
),
534 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
535 '_', /* leading char */
536 '/', /* ar_pad_char */
537 15, /* ar_max_namelen */
538 1, /* minimum section alignment */
539 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
540 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
541 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
542 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
543 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
544 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
546 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
547 bfd_generic_archive_p
, _bfd_dummy_target
},
548 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
550 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
551 _bfd_write_archive_contents
, bfd_false
},