1 /* BFD back-end for Hitachi H8/300 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
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, 1, 32, false, 0, complain_overflow_bitfield, 0, "24/pcrell", true, 0x0000ffff, 0x0000ffff, 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 = 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 DEFUN (reloc_processing, (relent, reloc, symbols, abfd, section),
153 struct internal_reloc *reloc AND
154 asymbol ** symbols AND
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(input_section, symbols, reloc, shrink, link_info)
181 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(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, 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(symbols, 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(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, 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);
342 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
344 if (gap > 0xff && gap < ~0xff)
346 if (! ((*link_info->callbacks->reloc_overflow)
347 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
348 reloc->howto->name, reloc->addend, input_section->owner,
349 input_section, reloc->address)))
353 bfd_put_8 (abfd, gap, data + dst_address);
361 /* A relword which would have like to have been a pcrel */
363 /* A relword which would like to have been modified but
367 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
374 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
381 /* Special relaxed type, there will be a gap between where we
382 get stuff from and where we put stuff to now
384 for a mov.b @aa:16 -> mov.b @aa:8
385 opcode 0x6a 0x0y offset
388 if (data[dst_address - 1] != 0x6a)
390 switch (data[src_address] & 0xf0)
394 data[dst_address - 1] = (data[src_address] & 0xf) | 0x20;
398 data[dst_address - 1] = (data[src_address] & 0xf) | 0x30;
404 /* the offset must fit ! after all, what was all the relaxing
408 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
411 /* Note the magic - src goes up by two bytes, but dst by only
420 /* Speciial relaxed type */
422 bfd_vma dot = link_order->offset
424 + link_order->u.indirect.section->output_section->vma;
426 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
429 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
432 bfd_put_8 (abfd, gap, data + dst_address);
434 switch (data[dst_address - 1])
438 bfd_put_8 (abfd, 0x55, data + dst_address - 1);
442 bfd_put_8 (abfd, 0x40, data + dst_address - 1);
455 case R_JMPL_B8: /* 24 bit branch which is now 8 bits */
457 /* Speciial relaxed type */
459 bfd_vma dot = link_order->offset
461 + link_order->u.indirect.section->output_section->vma;
463 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
466 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
469 switch (data[src_address])
473 bfd_put_8 (abfd, 0x55, data + dst_address);
477 bfd_put_8 (abfd, 0x40, data + dst_address);
481 bfd_put_8 (abfd, 0xde, data + dst_address);
485 bfd_put_8 (abfd, gap, data + dst_address + 1);
494 int v = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
495 int o = bfd_get_32 (abfd, data + src_address);
496 v = (v & 0x00ffffff) | (o & 0xff000000);
497 bfd_put_32 (abfd, v, data + dst_address);
505 /* A 24 bit mov which could be an 8 bit move, really pointing to
506 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
509 int v = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
510 int o = bfd_get_32 (abfd, data + dst_address);
511 v = (v & 0x00ffffff) | (o & 0xff000000);
512 bfd_put_32 (abfd, v, data + dst_address);
524 *src_ptr = src_address;
525 *dst_ptr = dst_address;
529 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
530 #define coff_reloc16_estimate h8300_reloc16_estimate
532 #include "coffcode.h"
535 #undef coff_bfd_get_relocated_section_contents
536 #undef coff_bfd_relax_section
537 #define coff_bfd_get_relocated_section_contents \
538 bfd_coff_reloc16_get_relocated_section_contents
539 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
543 bfd_target h8300coff_vec =
545 "coff-h8300", /* name */
546 bfd_target_coff_flavour,
547 true, /* data byte order is big */
548 true, /* header byte order is big */
550 (HAS_RELOC | EXEC_P | /* object flags */
551 HAS_LINENO | HAS_DEBUG |
552 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
553 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
554 '_', /* leading char */
555 '/', /* ar_pad_char */
556 15, /* ar_max_namelen */
557 1, /* minimum section alignment */
558 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
559 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
560 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
561 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
562 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
563 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
565 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
566 bfd_generic_archive_p, _bfd_dummy_target},
567 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
569 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
570 _bfd_write_archive_contents, bfd_false},