1 /* BFD back-end for Renesas H8/300 COFF binaries.
2 Copyright (C) 1990-2016 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 #include "coff/h8300.h"
28 #include "coff/internal.h"
30 #include "libiberty.h"
32 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
34 /* We derive a hash table from the basic BFD hash table to
35 hold entries in the function vector. Aside from the
36 info stored by the basic hash table, we need the offset
37 of a particular entry within the hash table as well as
38 the offset where we'll add the next entry. */
40 struct funcvec_hash_entry
42 /* The basic hash table entry. */
43 struct bfd_hash_entry root;
45 /* The offset within the vectors section where
50 struct funcvec_hash_table
52 /* The basic hash table. */
53 struct bfd_hash_table root;
57 /* Offset at which we'll add the next entry. */
62 /* To lookup a value in the function vector hash table. */
63 #define funcvec_hash_lookup(table, string, create, copy) \
64 ((struct funcvec_hash_entry *) \
65 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
67 /* The derived h8300 COFF linker table. Note it's derived from
68 the generic linker hash table, not the COFF backend linker hash
69 table! We use this to attach additional data structures we
70 need while linking on the h8300. */
71 struct h8300_coff_link_hash_table {
72 /* The main hash table. */
73 struct generic_link_hash_table root;
75 /* Section for the vectors table. This gets attached to a
76 random input bfd, we keep it here for easy access. */
77 asection *vectors_sec;
79 /* Hash table of the functions we need to enter into the function
81 struct funcvec_hash_table *funcvec_hash_table;
84 static struct bfd_link_hash_table *h8300_coff_link_hash_table_create (bfd *);
86 /* Get the H8/300 COFF linker hash table from a link_info structure. */
88 #define h8300_coff_hash_table(p) \
89 ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p))))
91 /* Initialize fields within a funcvec hash table entry. Called whenever
92 a new entry is added to the funcvec hash table. */
94 static struct bfd_hash_entry *
95 funcvec_hash_newfunc (struct bfd_hash_entry *entry,
96 struct bfd_hash_table *gen_table,
99 struct funcvec_hash_entry *ret;
100 struct funcvec_hash_table *table;
102 ret = (struct funcvec_hash_entry *) entry;
103 table = (struct funcvec_hash_table *) gen_table;
105 /* Allocate the structure if it has not already been allocated by a
108 ret = ((struct funcvec_hash_entry *)
109 bfd_hash_allocate (gen_table,
110 sizeof (struct funcvec_hash_entry)));
114 /* Call the allocation method of the superclass. */
115 ret = ((struct funcvec_hash_entry *)
116 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, gen_table, string));
121 /* Note where this entry will reside in the function vector table. */
122 ret->offset = table->offset;
124 /* Bump the offset at which we store entries in the function
125 vector. We'd like to bump up the size of the vectors section,
126 but it's not easily available here. */
127 switch (bfd_get_mach (table->abfd))
130 case bfd_mach_h8300hn:
131 case bfd_mach_h8300sn:
134 case bfd_mach_h8300h:
135 case bfd_mach_h8300s:
142 /* Everything went OK. */
143 return (struct bfd_hash_entry *) ret;
146 /* Initialize the function vector hash table. */
149 funcvec_hash_table_init (struct funcvec_hash_table *table,
151 struct bfd_hash_entry *(*newfunc)
152 (struct bfd_hash_entry *,
153 struct bfd_hash_table *,
155 unsigned int entsize)
157 /* Initialize our local fields, then call the generic initialization
161 return (bfd_hash_table_init (&table->root, newfunc, entsize));
164 /* Create the derived linker hash table. We use a derived hash table
165 basically to hold "static" information during an H8/300 coff link
166 without using static variables. */
168 static struct bfd_link_hash_table *
169 h8300_coff_link_hash_table_create (bfd *abfd)
171 struct h8300_coff_link_hash_table *ret;
172 bfd_size_type amt = sizeof (struct h8300_coff_link_hash_table);
174 ret = (struct h8300_coff_link_hash_table *) bfd_zmalloc (amt);
177 if (!_bfd_link_hash_table_init (&ret->root.root, abfd,
178 _bfd_generic_link_hash_newfunc,
179 sizeof (struct generic_link_hash_entry)))
185 return &ret->root.root;
188 /* Special handling for H8/300 relocs.
189 We only come here for pcrel stuff and return normally if not an -r link.
190 When doing -r, we can't do any arithmetic for the pcrel stuff, because
191 the code in reloc.c assumes that we can manipulate the targets of
192 the pcrel branches. This isn't so, since the H8/300 can do relaxing,
193 which means that the gap after the instruction may not be enough to
194 contain the offset required for the branch, so we have to use only
195 the addend until the final link. */
197 static bfd_reloc_status_type
198 special (bfd * abfd ATTRIBUTE_UNUSED,
199 arelent * reloc_entry ATTRIBUTE_UNUSED,
200 asymbol * symbol ATTRIBUTE_UNUSED,
201 void * data ATTRIBUTE_UNUSED,
202 asection * input_section ATTRIBUTE_UNUSED,
204 char ** error_message ATTRIBUTE_UNUSED)
206 if (output_bfd == (bfd *) NULL)
207 return bfd_reloc_continue;
209 /* Adjust the reloc address to that in the output section. */
210 reloc_entry->address += input_section->output_offset;
214 static reloc_howto_type howto_table[] =
216 HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8", FALSE, 0x000000ff, 0x000000ff, FALSE),
217 HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
218 HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "32", FALSE, 0xffffffff, 0xffffffff, FALSE),
219 HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8", FALSE, 0x000000ff, 0x000000ff, TRUE),
220 HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, special, "DISP16", FALSE, 0x0000ffff, 0x0000ffff, TRUE),
221 HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, special, "DISP32", FALSE, 0xffffffff, 0xffffffff, TRUE),
222 HOWTO (R_MOV16B1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:16", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
223 HOWTO (R_MOV16B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:16", FALSE, 0x000000ff, 0x000000ff, FALSE),
224 HOWTO (R_JMP1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16/pcrel", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
225 HOWTO (R_JMP2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pcrecl/16", FALSE, 0x000000ff, 0x000000ff, FALSE),
226 HOWTO (R_JMPL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "24/pcrell", FALSE, 0x00ffffff, 0x00ffffff, FALSE),
227 HOWTO (R_JMPL2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pc8/24", FALSE, 0x000000ff, 0x000000ff, FALSE),
228 HOWTO (R_MOV24B1, 0, 1, 32, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:24", FALSE, 0xffffffff, 0xffffffff, FALSE),
229 HOWTO (R_MOV24B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:24", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
231 /* An indirect reference to a function. This causes the function's address
232 to be added to the function vector in lo-mem and puts the address of
233 the function vector's entry in the jsr instruction. */
234 HOWTO (R_MEM_INDIRECT, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8/indirect", FALSE, 0x000000ff, 0x000000ff, FALSE),
236 /* Internal reloc for relaxing. This is created when a 16-bit pc-relative
237 branch is turned into an 8-bit pc-relative branch. */
238 HOWTO (R_PCRWORD_B, 0, 0, 8, TRUE, 0, complain_overflow_bitfield, special, "relaxed bCC:16", FALSE, 0x000000ff, 0x000000ff, FALSE),
240 HOWTO (R_MOVL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,special, "32/24 relaxable move", FALSE, 0xffffffff, 0xffffffff, FALSE),
242 HOWTO (R_MOVL2, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "32/24 relaxed move", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
244 HOWTO (R_BCC_INV, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8 inverted", FALSE, 0x000000ff, 0x000000ff, TRUE),
246 HOWTO (R_JMP_DEL, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "Deleted jump", FALSE, 0x000000ff, 0x000000ff, TRUE),
249 /* Turn a howto into a reloc number. */
251 #define SELECT_RELOC(x,howto) \
252 { x.r_type = select_reloc (howto); }
254 #define BADMAG(x) (H8300BADMAG (x) && H8300HBADMAG (x) && H8300SBADMAG (x) \
255 && H8300HNBADMAG(x) && H8300SNBADMAG(x))
256 #define H8300 1 /* Customize coffcode.h */
257 #define __A_MAGIC_SET__
259 /* Code to swap in the reloc. */
260 #define SWAP_IN_RELOC_OFFSET H_GET_32
261 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
262 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
263 dst->r_stuff[0] = 'S'; \
264 dst->r_stuff[1] = 'C';
267 select_reloc (reloc_howto_type *howto)
272 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
275 rtype2howto (arelent *internal, struct internal_reloc *dst)
280 internal->howto = howto_table + 0;
283 internal->howto = howto_table + 1;
286 internal->howto = howto_table + 2;
289 internal->howto = howto_table + 3;
292 internal->howto = howto_table + 4;
295 internal->howto = howto_table + 5;
298 internal->howto = howto_table + 6;
301 internal->howto = howto_table + 7;
304 internal->howto = howto_table + 8;
307 internal->howto = howto_table + 9;
310 internal->howto = howto_table + 10;
313 internal->howto = howto_table + 11;
316 internal->howto = howto_table + 12;
319 internal->howto = howto_table + 13;
322 internal->howto = howto_table + 14;
325 internal->howto = howto_table + 15;
328 internal->howto = howto_table + 16;
331 internal->howto = howto_table + 17;
334 internal->howto = howto_table + 18;
337 internal->howto = howto_table + 19;
340 internal->howto = NULL;
345 #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
347 /* Perform any necessary magic to the addend in a reloc entry. */
349 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
350 cache_ptr->addend = ext_reloc.r_offset;
352 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
353 reloc_processing (relent, reloc, symbols, abfd, section)
356 reloc_processing (arelent *relent, struct internal_reloc *reloc,
357 asymbol **symbols, bfd *abfd, asection *section)
359 relent->address = reloc->r_vaddr;
360 rtype2howto (relent, reloc);
362 if (((int) reloc->r_symndx) > 0)
363 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
365 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
367 relent->addend = reloc->r_offset;
368 relent->address -= section->vma;
372 h8300_symbol_address_p (bfd *abfd, asection *input_section, bfd_vma address)
376 s = _bfd_generic_link_get_symbols (abfd);
377 BFD_ASSERT (s != (asymbol **) NULL);
379 /* Search all the symbols for one in INPUT_SECTION with
385 if (p->section == input_section
386 && (input_section->output_section->vma
387 + input_section->output_offset
388 + p->value) == address)
395 /* If RELOC represents a relaxable instruction/reloc, change it into
396 the relaxed reloc, notify the linker that symbol addresses
397 have changed (bfd_perform_slip) and return how much the current
398 section has shrunk by.
400 FIXME: Much of this code has knowledge of the ordering of entries
401 in the howto table. This needs to be fixed. */
404 h8300_reloc16_estimate (bfd *abfd, asection *input_section, arelent *reloc,
405 unsigned int shrink, struct bfd_link_info *link_info)
410 static asection *last_input_section = NULL;
411 static arelent *last_reloc = NULL;
413 /* The address of the thing to be relocated will have moved back by
414 the size of the shrink - but we don't change reloc->address here,
415 since we need it to know where the relocation lives in the source
417 bfd_vma address = reloc->address - shrink;
419 if (input_section != last_input_section)
422 /* Only examine the relocs which might be relaxable. */
423 switch (reloc->howto->type)
425 /* This is the 16-/24-bit absolute branch which could become an
426 8-bit pc-relative branch. */
429 /* Get the address of the target of this branch. */
430 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
432 /* Get the address of the next instruction (not the reloc). */
433 dot = (input_section->output_section->vma
434 + input_section->output_offset + address);
436 /* Adjust for R_JMP1 vs R_JMPL1. */
437 dot += (reloc->howto->type == R_JMP1 ? 1 : 2);
439 /* Compute the distance from this insn to the branch target. */
442 /* If the distance is within -128..+128 inclusive, then we can relax
443 this jump. +128 is valid since the target will move two bytes
444 closer if we do relax this branch. */
445 if ((int) gap >= -128 && (int) gap <= 128)
449 if (!bfd_get_section_contents (abfd, input_section, & code,
452 code = bfd_get_8 (abfd, & code);
454 /* It's possible we may be able to eliminate this branch entirely;
455 if the previous instruction is a branch around this instruction,
456 and there's no label at this instruction, then we can reverse
457 the condition on the previous branch and eliminate this jump.
464 This saves 4 bytes instead of two, and should be relatively
467 Only perform this optimisation for jumps (code 0x5a) not
468 subroutine calls, as otherwise it could transform:
481 which changes the call (jsr) into a branch (bne). */
485 && last_reloc->howto->type == R_PCRBYTE)
488 last_value = bfd_coff_reloc16_get_value (last_reloc, link_info,
491 if (last_value == dot + 2
492 && last_reloc->address + 1 == reloc->address
493 && !h8300_symbol_address_p (abfd, input_section, dot - 2))
495 reloc->howto = howto_table + 19;
496 last_reloc->howto = howto_table + 18;
497 last_reloc->sym_ptr_ptr = reloc->sym_ptr_ptr;
498 last_reloc->addend = reloc->addend;
500 bfd_perform_slip (abfd, 4, input_section, address);
505 /* Change the reloc type. */
506 reloc->howto = reloc->howto + 1;
508 /* This shrinks this section by two bytes. */
510 bfd_perform_slip (abfd, 2, input_section, address);
514 /* This is the 16-bit pc-relative branch which could become an 8-bit
515 pc-relative branch. */
517 /* Get the address of the target of this branch, add one to the value
518 because the addend field in PCrel jumps is off by -1. */
519 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section) + 1;
521 /* Get the address of the next instruction if we were to relax. */
522 dot = input_section->output_section->vma +
523 input_section->output_offset + address;
525 /* Compute the distance from this insn to the branch target. */
528 /* If the distance is within -128..+128 inclusive, then we can relax
529 this jump. +128 is valid since the target will move two bytes
530 closer if we do relax this branch. */
531 if ((int) gap >= -128 && (int) gap <= 128)
533 /* Change the reloc type. */
534 reloc->howto = howto_table + 15;
536 /* This shrinks this section by two bytes. */
538 bfd_perform_slip (abfd, 2, input_section, address);
542 /* This is a 16-bit absolute address in a mov.b insn, which can
543 become an 8-bit absolute address if it's in the right range. */
545 /* Get the address of the data referenced by this mov.b insn. */
546 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
547 value = bfd_h8300_pad_address (abfd, value);
549 /* If the address is in the top 256 bytes of the address space
550 then we can relax this instruction. */
551 if (value >= 0xffffff00u)
553 /* Change the reloc type. */
554 reloc->howto = reloc->howto + 1;
556 /* This shrinks this section by two bytes. */
558 bfd_perform_slip (abfd, 2, input_section, address);
562 /* Similarly for a 24-bit absolute address in a mov.b. Note that
563 if we can't relax this into an 8-bit absolute, we'll fall through
564 and try to relax it into a 16-bit absolute. */
566 /* Get the address of the data referenced by this mov.b insn. */
567 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
568 value = bfd_h8300_pad_address (abfd, value);
570 if (value >= 0xffffff00u)
572 /* Change the reloc type. */
573 reloc->howto = reloc->howto + 1;
575 /* This shrinks this section by four bytes. */
577 bfd_perform_slip (abfd, 4, input_section, address);
579 /* Done with this reloc. */
583 /* FALLTHROUGH and try to turn the 24-/32-bit reloc into a 16-bit
586 /* This is a 24-/32-bit absolute address in a mov insn, which can
587 become an 16-bit absolute address if it's in the right range. */
589 /* Get the address of the data referenced by this mov insn. */
590 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
591 value = bfd_h8300_pad_address (abfd, value);
593 /* If the address is a sign-extended 16-bit value then we can
594 relax this instruction. */
595 if (value <= 0x7fff || value >= 0xffff8000u)
597 /* Change the reloc type. */
598 reloc->howto = howto_table + 17;
600 /* This shrinks this section by two bytes. */
602 bfd_perform_slip (abfd, 2, input_section, address);
606 /* No other reloc types represent relaxing opportunities. */
612 last_input_section = input_section;
616 /* Handle relocations for the H8/300, including relocs for relaxed
619 FIXME: Not all relocations check for overflow! */
622 h8300_reloc16_extra_cases (bfd *abfd, struct bfd_link_info *link_info,
623 struct bfd_link_order *link_order, arelent *reloc,
624 bfd_byte *data, unsigned int *src_ptr,
625 unsigned int *dst_ptr)
627 unsigned int src_address = *src_ptr;
628 unsigned int dst_address = *dst_ptr;
629 asection *input_section = link_order->u.indirect.section;
633 unsigned char temp_code;
635 switch (reloc->howto->type)
637 /* Generic 8-bit pc-relative relocation. */
639 /* Get the address of the target of this branch. */
640 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
642 dot = (input_section->output_offset
644 + link_order->u.indirect.section->output_section->vma);
649 if (gap < -128 || gap > 126)
650 (*link_info->callbacks->reloc_overflow)
651 (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
652 reloc->howto->name, reloc->addend, input_section->owner,
653 input_section, reloc->address);
655 /* Everything looks OK. Apply the relocation and update the
656 src/dst address appropriately. */
657 bfd_put_8 (abfd, gap, data + dst_address);
664 /* Generic 16-bit pc-relative relocation. */
666 /* Get the address of the target of this branch. */
667 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
669 /* Get the address of the instruction (not the reloc). */
670 dot = (input_section->output_offset
672 + link_order->u.indirect.section->output_section->vma + 1);
677 if (gap > 32766 || gap < -32768)
678 (*link_info->callbacks->reloc_overflow)
679 (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
680 reloc->howto->name, reloc->addend, input_section->owner,
681 input_section, reloc->address);
683 /* Everything looks OK. Apply the relocation and update the
684 src/dst address appropriately. */
685 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
692 /* Generic 8-bit absolute relocation. */
694 /* Get the address of the object referenced by this insn. */
695 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
697 bfd_put_8 (abfd, value & 0xff, data + dst_address);
704 /* Various simple 16-bit absolute relocations. */
708 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
709 bfd_put_16 (abfd, value, data + dst_address);
714 /* Various simple 24-/32-bit absolute relocations. */
718 /* Get the address of the target of this branch. */
719 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
720 bfd_put_32 (abfd, value, data + dst_address);
725 /* Another 24-/32-bit absolute relocation. */
727 /* Get the address of the target of this branch. */
728 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
730 value = ((value & 0x00ffffff)
731 | (bfd_get_32 (abfd, data + src_address) & 0xff000000));
732 bfd_put_32 (abfd, value, data + dst_address);
737 /* This is a 24-/32-bit absolute address in one of the following
740 "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
741 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", "ldc.w",
742 "stc.w" and "mov.[bwl]"
744 We may relax this into an 16-bit absolute address if it's in
747 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
748 value = bfd_h8300_pad_address (abfd, value);
751 if (value <= 0x7fff || value >= 0xffff8000u)
753 /* Insert the 16-bit value into the proper location. */
754 bfd_put_16 (abfd, value, data + dst_address);
756 /* Fix the opcode. For all the instructions that belong to
757 this relaxation, we simply need to turn off bit 0x20 in
758 the previous byte. */
759 data[dst_address - 1] &= ~0x20;
764 (*link_info->callbacks->reloc_overflow)
765 (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
766 reloc->howto->name, reloc->addend, input_section->owner,
767 input_section, reloc->address);
770 /* A 16-bit absolute branch that is now an 8-bit pc-relative branch. */
772 /* Get the address of the target of this branch. */
773 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
775 /* Get the address of the next instruction. */
776 dot = (input_section->output_offset
778 + link_order->u.indirect.section->output_section->vma + 1);
783 if (gap < -128 || gap > 126)
784 (*link_info->callbacks->reloc_overflow)
785 (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
786 reloc->howto->name, reloc->addend, input_section->owner,
787 input_section, reloc->address);
789 /* Now fix the instruction itself. */
790 switch (data[dst_address - 1])
794 bfd_put_8 (abfd, 0x55, data + dst_address - 1);
798 bfd_put_8 (abfd, 0x40, data + dst_address - 1);
805 /* Write out the 8-bit value. */
806 bfd_put_8 (abfd, gap, data + dst_address);
813 /* A 16-bit pc-relative branch that is now an 8-bit pc-relative branch. */
815 /* Get the address of the target of this branch. */
816 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
818 /* Get the address of the instruction (not the reloc). */
819 dot = (input_section->output_offset
821 + link_order->u.indirect.section->output_section->vma - 1);
826 if (gap < -128 || gap > 126)
827 (*link_info->callbacks->reloc_overflow)
828 (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
829 reloc->howto->name, reloc->addend, input_section->owner,
830 input_section, reloc->address);
832 /* Now fix the instruction. */
833 switch (data[dst_address - 2])
836 /* bCC:16 -> bCC:8 */
837 /* Get the second byte of the original insn, which contains
838 the condition code. */
839 tmp = data[dst_address - 1];
841 /* Compute the fisrt byte of the relaxed instruction. The
842 original sequence 0x58 0xX0 is relaxed to 0x4X, where X
843 represents the condition code. */
849 bfd_put_8 (abfd, tmp, data + dst_address - 2);
853 /* bsr:16 -> bsr:8 */
854 bfd_put_8 (abfd, 0x55, data + dst_address - 2);
861 /* Output the target. */
862 bfd_put_8 (abfd, gap, data + dst_address - 1);
864 /* We don't advance dst_address -- the 8-bit reloc is applied at
865 dst_address - 1, so the next insn should begin at dst_address. */
870 /* Similarly for a 24-bit absolute that is now 8 bits. */
872 /* Get the address of the target of this branch. */
873 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
875 /* Get the address of the instruction (not the reloc). */
876 dot = (input_section->output_offset
878 + link_order->u.indirect.section->output_section->vma + 2);
882 /* Fix the instruction. */
883 switch (data[src_address])
887 bfd_put_8 (abfd, 0x55, data + dst_address);
891 bfd_put_8 (abfd, 0x40, data + dst_address);
897 bfd_put_8 (abfd, gap, data + dst_address + 1);
903 /* This is a 16-bit absolute address in one of the following
906 "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
907 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
910 We may relax this into an 8-bit absolute address if it's in
913 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
915 /* All instructions with R_H8_DIR16B2 start with 0x6a. */
916 if (data[dst_address - 2] != 0x6a)
919 temp_code = data[src_address - 1];
921 /* If this is a mov.b instruction, clear the lower nibble, which
922 contains the source/destination register number. */
923 if ((temp_code & 0x10) != 0x10)
926 /* Fix up the opcode. */
930 /* This is mov.b @aa:16,Rd. */
931 data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x20;
934 /* This is mov.b Rs,@aa:16. */
935 data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x30;
938 /* This is a bit-maniputation instruction that stores one
939 bit into memory, one of "bclr", "bist", "bnot", "bset",
941 data[dst_address - 2] = 0x7f;
944 /* This is a bit-maniputation instruction that loads one bit
945 from memory, one of "band", "biand", "bild", "bior",
946 "bixor", "bld", "bor", "btst", and "bxor". */
947 data[dst_address - 2] = 0x7e;
953 bfd_put_8 (abfd, value & 0xff, data + dst_address - 1);
957 /* This is a 24-bit absolute address in one of the following
960 "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
961 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
964 We may relax this into an 8-bit absolute address if it's in
967 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
969 /* All instructions with R_MOV24B2 start with 0x6a. */
970 if (data[dst_address - 2] != 0x6a)
973 temp_code = data[src_address - 1];
975 /* If this is a mov.b instruction, clear the lower nibble, which
976 contains the source/destination register number. */
977 if ((temp_code & 0x30) != 0x30)
980 /* Fix up the opcode. */
984 /* This is mov.b @aa:24/32,Rd. */
985 data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x20;
988 /* This is mov.b Rs,@aa:24/32. */
989 data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x30;
992 /* This is a bit-maniputation instruction that stores one
993 bit into memory, one of "bclr", "bist", "bnot", "bset",
995 data[dst_address - 2] = 0x7f;
998 /* This is a bit-maniputation instruction that loads one bit
999 from memory, one of "band", "biand", "bild", "bior",
1000 "bixor", "bld", "bor", "btst", and "bxor". */
1001 data[dst_address - 2] = 0x7e;
1007 bfd_put_8 (abfd, value & 0xff, data + dst_address - 1);
1012 /* Get the address of the target of this branch. */
1013 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
1015 dot = (input_section->output_offset
1017 + link_order->u.indirect.section->output_section->vma) + 1;
1022 if (gap < -128 || gap > 126)
1023 (*link_info->callbacks->reloc_overflow)
1024 (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
1025 reloc->howto->name, reloc->addend, input_section->owner,
1026 input_section, reloc->address);
1028 /* Everything looks OK. Fix the condition in the instruction, apply
1029 the relocation, and update the src/dst address appropriately. */
1031 bfd_put_8 (abfd, bfd_get_8 (abfd, data + dst_address - 1) ^ 1,
1032 data + dst_address - 1);
1033 bfd_put_8 (abfd, gap, data + dst_address);
1044 /* An 8-bit memory indirect instruction (jmp/jsr).
1046 There's several things that need to be done to handle
1049 If this is a reloc against the absolute symbol, then
1050 we should handle it just R_RELBYTE. Likewise if it's
1051 for a symbol with a value ge 0 and le 0xff.
1053 Otherwise it's a jump/call through the function vector,
1054 and the linker is expected to set up the function vector
1055 and put the right value into the jump/call instruction. */
1056 case R_MEM_INDIRECT:
1058 /* We need to find the symbol so we can determine it's
1059 address in the function vector table. */
1062 struct funcvec_hash_table *ftab;
1063 struct funcvec_hash_entry *h;
1064 struct h8300_coff_link_hash_table *htab;
1065 asection *vectors_sec;
1067 if (link_info->output_bfd->xvec != abfd->xvec)
1069 (*_bfd_error_handler)
1070 (_("cannot handle R_MEM_INDIRECT reloc when using %s output"),
1071 link_info->output_bfd->xvec->name);
1073 /* What else can we do? This function doesn't allow return
1074 of an error, and we don't want to call abort as that
1075 indicates an internal error. */
1076 #ifndef EXIT_FAILURE
1077 #define EXIT_FAILURE 1
1079 xexit (EXIT_FAILURE);
1081 htab = h8300_coff_hash_table (link_info);
1082 vectors_sec = htab->vectors_sec;
1084 /* First see if this is a reloc against the absolute symbol
1085 or against a symbol with a nonnegative value <= 0xff. */
1086 symbol = *(reloc->sym_ptr_ptr);
1087 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
1088 if (symbol == bfd_abs_section_ptr->symbol
1091 /* This should be handled in a manner very similar to
1092 R_RELBYTES. If the value is in range, then just slam
1093 the value into the right location. Else trigger a
1094 reloc overflow callback. */
1097 bfd_put_8 (abfd, value, data + dst_address);
1102 (*link_info->callbacks->reloc_overflow)
1103 (link_info, NULL, bfd_asymbol_name (*reloc->sym_ptr_ptr),
1104 reloc->howto->name, reloc->addend, input_section->owner,
1105 input_section, reloc->address);
1109 /* This is a jump/call through a function vector, and we're
1110 expected to create the function vector ourselves.
1112 First look up this symbol in the linker hash table -- we need
1113 the derived linker symbol which holds this symbol's index
1114 in the function vector. */
1115 name = symbol->name;
1116 if (symbol->flags & BSF_LOCAL)
1118 char *new_name = bfd_malloc ((bfd_size_type) strlen (name) + 10);
1120 if (new_name == NULL)
1123 sprintf (new_name, "%s_%08x", name, symbol->section->id);
1127 ftab = htab->funcvec_hash_table;
1128 h = funcvec_hash_lookup (ftab, name, FALSE, FALSE);
1130 /* This shouldn't ever happen. If it does that means we've got
1131 data corruption of some kind. Aborting seems like a reasonable
1132 thing to do here. */
1133 if (h == NULL || vectors_sec == NULL)
1136 /* Place the address of the function vector entry into the
1139 vectors_sec->output_offset + h->offset,
1140 data + dst_address);
1145 /* Now create an entry in the function vector itself. */
1146 switch (bfd_get_mach (input_section->owner))
1148 case bfd_mach_h8300:
1149 case bfd_mach_h8300hn:
1150 case bfd_mach_h8300sn:
1152 bfd_coff_reloc16_get_value (reloc,
1155 vectors_sec->contents + h->offset);
1157 case bfd_mach_h8300h:
1158 case bfd_mach_h8300s:
1160 bfd_coff_reloc16_get_value (reloc,
1163 vectors_sec->contents + h->offset);
1169 /* Gross. We've already written the contents of the vector section
1170 before we get here... So we write it again with the new data. */
1171 bfd_set_section_contents (vectors_sec->output_section->owner,
1172 vectors_sec->output_section,
1173 vectors_sec->contents,
1174 (file_ptr) vectors_sec->output_offset,
1185 *src_ptr = src_address;
1186 *dst_ptr = dst_address;
1189 /* Routine for the h8300 linker.
1191 This routine is necessary to handle the special R_MEM_INDIRECT
1192 relocs on the h8300. It's responsible for generating a vectors
1193 section and attaching it to an input bfd as well as sizing
1194 the vectors section. It also creates our vectors hash table.
1196 It uses the generic linker routines to actually add the symbols.
1197 from this BFD to the bfd linker hash table. It may add a few
1198 selected static symbols to the bfd linker hash table. */
1201 h8300_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1204 struct funcvec_hash_table *funcvec_hash_table;
1206 struct h8300_coff_link_hash_table *htab;
1208 /* Add the symbols using the generic code. */
1209 _bfd_generic_link_add_symbols (abfd, info);
1211 if (info->output_bfd->xvec != abfd->xvec)
1214 htab = h8300_coff_hash_table (info);
1216 /* If we haven't created a vectors section, do so now. */
1217 if (!htab->vectors_sec)
1221 /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */
1222 flags = (SEC_ALLOC | SEC_LOAD
1223 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY);
1224 htab->vectors_sec = bfd_make_section_with_flags (abfd, ".vectors",
1227 /* If the section wasn't created, or we couldn't set the flags,
1228 quit quickly now, rather than dying a painful death later. */
1229 if (!htab->vectors_sec)
1232 /* Also create the vector hash table. */
1233 amt = sizeof (struct funcvec_hash_table);
1234 funcvec_hash_table = (struct funcvec_hash_table *) bfd_alloc (abfd, amt);
1236 if (!funcvec_hash_table)
1239 /* And initialize the funcvec hash table. */
1240 if (!funcvec_hash_table_init (funcvec_hash_table, abfd,
1241 funcvec_hash_newfunc,
1242 sizeof (struct funcvec_hash_entry)))
1244 bfd_release (abfd, funcvec_hash_table);
1248 /* Store away a pointer to the funcvec hash table. */
1249 htab->funcvec_hash_table = funcvec_hash_table;
1252 /* Load up the function vector hash table. */
1253 funcvec_hash_table = htab->funcvec_hash_table;
1255 /* Now scan the relocs for all the sections in this bfd; create
1256 additional space in the .vectors section as needed. */
1257 for (sec = abfd->sections; sec; sec = sec->next)
1259 long reloc_size, reloc_count, i;
1263 /* Suck in the relocs, symbols & canonicalize them. */
1264 reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
1265 if (reloc_size <= 0)
1268 relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
1272 /* The symbols should have been read in by _bfd_generic link_add_symbols
1273 call abovec, so we can cheat and use the pointer to them that was
1274 saved in the above call. */
1275 symbols = _bfd_generic_link_get_symbols(abfd);
1276 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, symbols);
1277 if (reloc_count <= 0)
1283 /* Now walk through all the relocations in this section. */
1284 for (i = 0; i < reloc_count; i++)
1286 arelent *reloc = relocs[i];
1287 asymbol *symbol = *(reloc->sym_ptr_ptr);
1290 /* We've got an indirect reloc. See if we need to add it
1291 to the function vector table. At this point, we have
1292 to add a new entry for each unique symbol referenced
1293 by an R_MEM_INDIRECT relocation except for a reloc
1294 against the absolute section symbol. */
1295 if (reloc->howto->type == R_MEM_INDIRECT
1296 && symbol != bfd_abs_section_ptr->symbol)
1299 struct funcvec_hash_table *ftab;
1300 struct funcvec_hash_entry *h;
1302 name = symbol->name;
1303 if (symbol->flags & BSF_LOCAL)
1307 new_name = bfd_malloc ((bfd_size_type) strlen (name) + 10);
1308 if (new_name == NULL)
1311 sprintf (new_name, "%s_%08x", name, symbol->section->id);
1315 /* Look this symbol up in the function vector hash table. */
1316 ftab = htab->funcvec_hash_table;
1317 h = funcvec_hash_lookup (ftab, name, FALSE, FALSE);
1319 /* If this symbol isn't already in the hash table, add
1320 it and bump up the size of the hash table. */
1323 h = funcvec_hash_lookup (ftab, name, TRUE, TRUE);
1330 /* Bump the size of the vectors section. Each vector
1331 takes 2 bytes on the h8300 and 4 bytes on the h8300h. */
1332 switch (bfd_get_mach (abfd))
1334 case bfd_mach_h8300:
1335 case bfd_mach_h8300hn:
1336 case bfd_mach_h8300sn:
1337 htab->vectors_sec->size += 2;
1339 case bfd_mach_h8300h:
1340 case bfd_mach_h8300s:
1341 htab->vectors_sec->size += 4;
1350 /* We're done with the relocations, release them. */
1354 /* Now actually allocate some space for the function vector. It's
1355 wasteful to do this more than once, but this is easier. */
1356 sec = htab->vectors_sec;
1359 /* Free the old contents. */
1361 free (sec->contents);
1363 /* Allocate new contents. */
1364 sec->contents = bfd_malloc (sec->size);
1370 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1371 #define coff_reloc16_estimate h8300_reloc16_estimate
1372 #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1373 #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
1375 #define COFF_LONG_FILENAMES
1377 #ifndef bfd_pe_print_pdata
1378 #define bfd_pe_print_pdata NULL
1381 #include "coffcode.h"
1383 #undef coff_bfd_get_relocated_section_contents
1384 #undef coff_bfd_relax_section
1385 #define coff_bfd_get_relocated_section_contents \
1386 bfd_coff_reloc16_get_relocated_section_contents
1387 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1389 CREATE_BIG_COFF_TARGET_VEC (h8300_coff_vec, "coff-h8300", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)