1 /* BFD back-end for Renesas H8/300 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2012
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain, <sac@cygnus.com>.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
29 #include "coff/h8300.h"
30 #include "coff/internal.h"
32 #include "libiberty.h"
34 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
36 /* We derive a hash table from the basic BFD hash table to
37 hold entries in the function vector. Aside from the
38 info stored by the basic hash table, we need the offset
39 of a particular entry within the hash table as well as
40 the offset where we'll add the next entry. */
42 struct funcvec_hash_entry
44 /* The basic hash table entry. */
45 struct bfd_hash_entry root;
47 /* The offset within the vectors section where
52 struct funcvec_hash_table
54 /* The basic hash table. */
55 struct bfd_hash_table root;
59 /* Offset at which we'll add the next entry. */
64 /* To lookup a value in the function vector hash table. */
65 #define funcvec_hash_lookup(table, string, create, copy) \
66 ((struct funcvec_hash_entry *) \
67 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
69 /* The derived h8300 COFF linker table. Note it's derived from
70 the generic linker hash table, not the COFF backend linker hash
71 table! We use this to attach additional data structures we
72 need while linking on the h8300. */
73 struct h8300_coff_link_hash_table {
74 /* The main hash table. */
75 struct generic_link_hash_table root;
77 /* Section for the vectors table. This gets attached to a
78 random input bfd, we keep it here for easy access. */
79 asection *vectors_sec;
81 /* Hash table of the functions we need to enter into the function
83 struct funcvec_hash_table *funcvec_hash_table;
86 static struct bfd_link_hash_table *h8300_coff_link_hash_table_create (bfd *);
88 /* Get the H8/300 COFF linker hash table from a link_info structure. */
90 #define h8300_coff_hash_table(p) \
91 ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p))))
93 /* Initialize fields within a funcvec hash table entry. Called whenever
94 a new entry is added to the funcvec hash table. */
96 static struct bfd_hash_entry *
97 funcvec_hash_newfunc (struct bfd_hash_entry *entry,
98 struct bfd_hash_table *gen_table,
101 struct funcvec_hash_entry *ret;
102 struct funcvec_hash_table *table;
104 ret = (struct funcvec_hash_entry *) entry;
105 table = (struct funcvec_hash_table *) gen_table;
107 /* Allocate the structure if it has not already been allocated by a
110 ret = ((struct funcvec_hash_entry *)
111 bfd_hash_allocate (gen_table,
112 sizeof (struct funcvec_hash_entry)));
116 /* Call the allocation method of the superclass. */
117 ret = ((struct funcvec_hash_entry *)
118 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, gen_table, string));
123 /* Note where this entry will reside in the function vector table. */
124 ret->offset = table->offset;
126 /* Bump the offset at which we store entries in the function
127 vector. We'd like to bump up the size of the vectors section,
128 but it's not easily available here. */
129 switch (bfd_get_mach (table->abfd))
132 case bfd_mach_h8300hn:
133 case bfd_mach_h8300sn:
136 case bfd_mach_h8300h:
137 case bfd_mach_h8300s:
144 /* Everything went OK. */
145 return (struct bfd_hash_entry *) ret;
148 /* Initialize the function vector hash table. */
151 funcvec_hash_table_init (struct funcvec_hash_table *table,
153 struct bfd_hash_entry *(*newfunc)
154 (struct bfd_hash_entry *,
155 struct bfd_hash_table *,
157 unsigned int entsize)
159 /* Initialize our local fields, then call the generic initialization
163 return (bfd_hash_table_init (&table->root, newfunc, entsize));
166 /* Create the derived linker hash table. We use a derived hash table
167 basically to hold "static" information during an H8/300 coff link
168 without using static variables. */
170 static struct bfd_link_hash_table *
171 h8300_coff_link_hash_table_create (bfd *abfd)
173 struct h8300_coff_link_hash_table *ret;
174 bfd_size_type amt = sizeof (struct h8300_coff_link_hash_table);
176 ret = (struct h8300_coff_link_hash_table *) bfd_zmalloc (amt);
179 if (!_bfd_link_hash_table_init (&ret->root.root, abfd,
180 _bfd_generic_link_hash_newfunc,
181 sizeof (struct generic_link_hash_entry)))
187 return &ret->root.root;
190 /* Special handling for H8/300 relocs.
191 We only come here for pcrel stuff and return normally if not an -r link.
192 When doing -r, we can't do any arithmetic for the pcrel stuff, because
193 the code in reloc.c assumes that we can manipulate the targets of
194 the pcrel branches. This isn't so, since the H8/300 can do relaxing,
195 which means that the gap after the instruction may not be enough to
196 contain the offset required for the branch, so we have to use only
197 the addend until the final link. */
199 static bfd_reloc_status_type
200 special (bfd * abfd ATTRIBUTE_UNUSED,
201 arelent * reloc_entry ATTRIBUTE_UNUSED,
202 asymbol * symbol ATTRIBUTE_UNUSED,
203 void * data ATTRIBUTE_UNUSED,
204 asection * input_section ATTRIBUTE_UNUSED,
206 char ** error_message ATTRIBUTE_UNUSED)
208 if (output_bfd == (bfd *) NULL)
209 return bfd_reloc_continue;
211 /* Adjust the reloc address to that in the output section. */
212 reloc_entry->address += input_section->output_offset;
216 static reloc_howto_type howto_table[] =
218 HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8", FALSE, 0x000000ff, 0x000000ff, FALSE),
219 HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
220 HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "32", FALSE, 0xffffffff, 0xffffffff, FALSE),
221 HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8", FALSE, 0x000000ff, 0x000000ff, TRUE),
222 HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, special, "DISP16", FALSE, 0x0000ffff, 0x0000ffff, TRUE),
223 HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, special, "DISP32", FALSE, 0xffffffff, 0xffffffff, TRUE),
224 HOWTO (R_MOV16B1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:16", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
225 HOWTO (R_MOV16B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:16", FALSE, 0x000000ff, 0x000000ff, FALSE),
226 HOWTO (R_JMP1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16/pcrel", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
227 HOWTO (R_JMP2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pcrecl/16", FALSE, 0x000000ff, 0x000000ff, FALSE),
228 HOWTO (R_JMPL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "24/pcrell", FALSE, 0x00ffffff, 0x00ffffff, FALSE),
229 HOWTO (R_JMPL2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pc8/24", FALSE, 0x000000ff, 0x000000ff, FALSE),
230 HOWTO (R_MOV24B1, 0, 1, 32, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:24", FALSE, 0xffffffff, 0xffffffff, FALSE),
231 HOWTO (R_MOV24B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:24", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
233 /* An indirect reference to a function. This causes the function's address
234 to be added to the function vector in lo-mem and puts the address of
235 the function vector's entry in the jsr instruction. */
236 HOWTO (R_MEM_INDIRECT, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8/indirect", FALSE, 0x000000ff, 0x000000ff, FALSE),
238 /* Internal reloc for relaxing. This is created when a 16-bit pc-relative
239 branch is turned into an 8-bit pc-relative branch. */
240 HOWTO (R_PCRWORD_B, 0, 0, 8, TRUE, 0, complain_overflow_bitfield, special, "relaxed bCC:16", FALSE, 0x000000ff, 0x000000ff, FALSE),
242 HOWTO (R_MOVL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,special, "32/24 relaxable move", FALSE, 0xffffffff, 0xffffffff, FALSE),
244 HOWTO (R_MOVL2, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "32/24 relaxed move", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
246 HOWTO (R_BCC_INV, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8 inverted", FALSE, 0x000000ff, 0x000000ff, TRUE),
248 HOWTO (R_JMP_DEL, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "Deleted jump", FALSE, 0x000000ff, 0x000000ff, TRUE),
251 /* Turn a howto into a reloc number. */
253 #define SELECT_RELOC(x,howto) \
254 { x.r_type = select_reloc (howto); }
256 #define BADMAG(x) (H8300BADMAG (x) && H8300HBADMAG (x) && H8300SBADMAG (x) \
257 && H8300HNBADMAG(x) && H8300SNBADMAG(x))
258 #define H8300 1 /* Customize coffcode.h */
259 #define __A_MAGIC_SET__
261 /* Code to swap in the reloc. */
262 #define SWAP_IN_RELOC_OFFSET H_GET_32
263 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
264 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
265 dst->r_stuff[0] = 'S'; \
266 dst->r_stuff[1] = 'C';
269 select_reloc (reloc_howto_type *howto)
274 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
277 rtype2howto (arelent *internal, struct internal_reloc *dst)
282 internal->howto = howto_table + 0;
285 internal->howto = howto_table + 1;
288 internal->howto = howto_table + 2;
291 internal->howto = howto_table + 3;
294 internal->howto = howto_table + 4;
297 internal->howto = howto_table + 5;
300 internal->howto = howto_table + 6;
303 internal->howto = howto_table + 7;
306 internal->howto = howto_table + 8;
309 internal->howto = howto_table + 9;
312 internal->howto = howto_table + 10;
315 internal->howto = howto_table + 11;
318 internal->howto = howto_table + 12;
321 internal->howto = howto_table + 13;
324 internal->howto = howto_table + 14;
327 internal->howto = howto_table + 15;
330 internal->howto = howto_table + 16;
333 internal->howto = howto_table + 17;
336 internal->howto = howto_table + 18;
339 internal->howto = howto_table + 19;
347 #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
349 /* Perform any necessary magic to the addend in a reloc entry. */
351 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
352 cache_ptr->addend = ext_reloc.r_offset;
354 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
355 reloc_processing (relent, reloc, symbols, abfd, section)
358 reloc_processing (arelent *relent, struct internal_reloc *reloc,
359 asymbol **symbols, bfd *abfd, asection *section)
361 relent->address = reloc->r_vaddr;
362 rtype2howto (relent, reloc);
364 if (((int) reloc->r_symndx) > 0)
365 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
367 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
369 relent->addend = reloc->r_offset;
370 relent->address -= section->vma;
374 h8300_symbol_address_p (bfd *abfd, asection *input_section, bfd_vma address)
378 s = _bfd_generic_link_get_symbols (abfd);
379 BFD_ASSERT (s != (asymbol **) NULL);
381 /* Search all the symbols for one in INPUT_SECTION with
387 if (p->section == input_section
388 && (input_section->output_section->vma
389 + input_section->output_offset
390 + p->value) == address)
397 /* If RELOC represents a relaxable instruction/reloc, change it into
398 the relaxed reloc, notify the linker that symbol addresses
399 have changed (bfd_perform_slip) and return how much the current
400 section has shrunk by.
402 FIXME: Much of this code has knowledge of the ordering of entries
403 in the howto table. This needs to be fixed. */
406 h8300_reloc16_estimate (bfd *abfd, asection *input_section, arelent *reloc,
407 unsigned int shrink, struct bfd_link_info *link_info)
412 static asection *last_input_section = NULL;
413 static arelent *last_reloc = NULL;
415 /* The address of the thing to be relocated will have moved back by
416 the size of the shrink - but we don't change reloc->address here,
417 since we need it to know where the relocation lives in the source
419 bfd_vma address = reloc->address - shrink;
421 if (input_section != last_input_section)
424 /* Only examine the relocs which might be relaxable. */
425 switch (reloc->howto->type)
427 /* This is the 16-/24-bit absolute branch which could become an
428 8-bit pc-relative branch. */
431 /* Get the address of the target of this branch. */
432 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
434 /* Get the address of the next instruction (not the reloc). */
435 dot = (input_section->output_section->vma
436 + input_section->output_offset + address);
438 /* Adjust for R_JMP1 vs R_JMPL1. */
439 dot += (reloc->howto->type == R_JMP1 ? 1 : 2);
441 /* Compute the distance from this insn to the branch target. */
444 /* If the distance is within -128..+128 inclusive, then we can relax
445 this jump. +128 is valid since the target will move two bytes
446 closer if we do relax this branch. */
447 if ((int) gap >= -128 && (int) gap <= 128)
451 if (!bfd_get_section_contents (abfd, input_section, & code,
454 code = bfd_get_8 (abfd, & code);
456 /* It's possible we may be able to eliminate this branch entirely;
457 if the previous instruction is a branch around this instruction,
458 and there's no label at this instruction, then we can reverse
459 the condition on the previous branch and eliminate this jump.
466 This saves 4 bytes instead of two, and should be relatively
469 Only perform this optimisation for jumps (code 0x5a) not
470 subroutine calls, as otherwise it could transform:
483 which changes the call (jsr) into a branch (bne). */
487 && last_reloc->howto->type == R_PCRBYTE)
490 last_value = bfd_coff_reloc16_get_value (last_reloc, link_info,
493 if (last_value == dot + 2
494 && last_reloc->address + 1 == reloc->address
495 && !h8300_symbol_address_p (abfd, input_section, dot - 2))
497 reloc->howto = howto_table + 19;
498 last_reloc->howto = howto_table + 18;
499 last_reloc->sym_ptr_ptr = reloc->sym_ptr_ptr;
500 last_reloc->addend = reloc->addend;
502 bfd_perform_slip (abfd, 4, input_section, address);
507 /* Change the reloc type. */
508 reloc->howto = reloc->howto + 1;
510 /* This shrinks this section by two bytes. */
512 bfd_perform_slip (abfd, 2, input_section, address);
516 /* This is the 16-bit pc-relative branch which could become an 8-bit
517 pc-relative branch. */
519 /* Get the address of the target of this branch, add one to the value
520 because the addend field in PCrel jumps is off by -1. */
521 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section) + 1;
523 /* Get the address of the next instruction if we were to relax. */
524 dot = input_section->output_section->vma +
525 input_section->output_offset + address;
527 /* Compute the distance from this insn to the branch target. */
530 /* If the distance is within -128..+128 inclusive, then we can relax
531 this jump. +128 is valid since the target will move two bytes
532 closer if we do relax this branch. */
533 if ((int) gap >= -128 && (int) gap <= 128)
535 /* Change the reloc type. */
536 reloc->howto = howto_table + 15;
538 /* This shrinks this section by two bytes. */
540 bfd_perform_slip (abfd, 2, input_section, address);
544 /* This is a 16-bit absolute address in a mov.b insn, which can
545 become an 8-bit absolute address if it's in the right range. */
547 /* Get the address of the data referenced by this mov.b insn. */
548 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
549 value = bfd_h8300_pad_address (abfd, value);
551 /* If the address is in the top 256 bytes of the address space
552 then we can relax this instruction. */
553 if (value >= 0xffffff00u)
555 /* Change the reloc type. */
556 reloc->howto = reloc->howto + 1;
558 /* This shrinks this section by two bytes. */
560 bfd_perform_slip (abfd, 2, input_section, address);
564 /* Similarly for a 24-bit absolute address in a mov.b. Note that
565 if we can't relax this into an 8-bit absolute, we'll fall through
566 and try to relax it into a 16-bit absolute. */
568 /* Get the address of the data referenced by this mov.b insn. */
569 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
570 value = bfd_h8300_pad_address (abfd, value);
572 if (value >= 0xffffff00u)
574 /* Change the reloc type. */
575 reloc->howto = reloc->howto + 1;
577 /* This shrinks this section by four bytes. */
579 bfd_perform_slip (abfd, 4, input_section, address);
581 /* Done with this reloc. */
585 /* FALLTHROUGH and try to turn the 24-/32-bit reloc into a 16-bit
588 /* This is a 24-/32-bit absolute address in a mov insn, which can
589 become an 16-bit absolute address if it's in the right range. */
591 /* Get the address of the data referenced by this mov insn. */
592 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
593 value = bfd_h8300_pad_address (abfd, value);
595 /* If the address is a sign-extended 16-bit value then we can
596 relax this instruction. */
597 if (value <= 0x7fff || value >= 0xffff8000u)
599 /* Change the reloc type. */
600 reloc->howto = howto_table + 17;
602 /* This shrinks this section by two bytes. */
604 bfd_perform_slip (abfd, 2, input_section, address);
608 /* No other reloc types represent relaxing opportunities. */
614 last_input_section = input_section;
618 /* Handle relocations for the H8/300, including relocs for relaxed
621 FIXME: Not all relocations check for overflow! */
624 h8300_reloc16_extra_cases (bfd *abfd, struct bfd_link_info *link_info,
625 struct bfd_link_order *link_order, arelent *reloc,
626 bfd_byte *data, unsigned int *src_ptr,
627 unsigned int *dst_ptr)
629 unsigned int src_address = *src_ptr;
630 unsigned int dst_address = *dst_ptr;
631 asection *input_section = link_order->u.indirect.section;
635 unsigned char temp_code;
637 switch (reloc->howto->type)
639 /* Generic 8-bit pc-relative relocation. */
641 /* Get the address of the target of this branch. */
642 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
644 dot = (input_section->output_offset
646 + link_order->u.indirect.section->output_section->vma);
651 if (gap < -128 || gap > 126)
653 if (! ((*link_info->callbacks->reloc_overflow)
655 bfd_asymbol_name (*reloc->sym_ptr_ptr),
656 reloc->howto->name, reloc->addend, input_section->owner,
657 input_section, reloc->address)))
661 /* Everything looks OK. Apply the relocation and update the
662 src/dst address appropriately. */
663 bfd_put_8 (abfd, gap, data + dst_address);
670 /* Generic 16-bit pc-relative relocation. */
672 /* Get the address of the target of this branch. */
673 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
675 /* Get the address of the instruction (not the reloc). */
676 dot = (input_section->output_offset
678 + link_order->u.indirect.section->output_section->vma + 1);
683 if (gap > 32766 || gap < -32768)
685 if (! ((*link_info->callbacks->reloc_overflow)
687 bfd_asymbol_name (*reloc->sym_ptr_ptr),
688 reloc->howto->name, reloc->addend, input_section->owner,
689 input_section, reloc->address)))
693 /* Everything looks OK. Apply the relocation and update the
694 src/dst address appropriately. */
695 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
702 /* Generic 8-bit absolute relocation. */
704 /* Get the address of the object referenced by this insn. */
705 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
707 bfd_put_8 (abfd, value & 0xff, data + dst_address);
714 /* Various simple 16-bit absolute relocations. */
718 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
719 bfd_put_16 (abfd, value, data + dst_address);
724 /* Various simple 24-/32-bit absolute relocations. */
728 /* Get the address of the target of this branch. */
729 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
730 bfd_put_32 (abfd, value, data + dst_address);
735 /* Another 24-/32-bit absolute relocation. */
737 /* Get the address of the target of this branch. */
738 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
740 value = ((value & 0x00ffffff)
741 | (bfd_get_32 (abfd, data + src_address) & 0xff000000));
742 bfd_put_32 (abfd, value, data + dst_address);
747 /* This is a 24-/32-bit absolute address in one of the following
750 "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
751 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", "ldc.w",
752 "stc.w" and "mov.[bwl]"
754 We may relax this into an 16-bit absolute address if it's in
757 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
758 value = bfd_h8300_pad_address (abfd, value);
761 if (value <= 0x7fff || value >= 0xffff8000u)
763 /* Insert the 16-bit value into the proper location. */
764 bfd_put_16 (abfd, value, data + dst_address);
766 /* Fix the opcode. For all the instructions that belong to
767 this relaxation, we simply need to turn off bit 0x20 in
768 the previous byte. */
769 data[dst_address - 1] &= ~0x20;
775 if (! ((*link_info->callbacks->reloc_overflow)
777 bfd_asymbol_name (*reloc->sym_ptr_ptr),
778 reloc->howto->name, reloc->addend, input_section->owner,
779 input_section, reloc->address)))
784 /* A 16-bit absolute branch that is now an 8-bit pc-relative branch. */
786 /* Get the address of the target of this branch. */
787 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
789 /* Get the address of the next instruction. */
790 dot = (input_section->output_offset
792 + link_order->u.indirect.section->output_section->vma + 1);
797 if (gap < -128 || gap > 126)
799 if (! ((*link_info->callbacks->reloc_overflow)
801 bfd_asymbol_name (*reloc->sym_ptr_ptr),
802 reloc->howto->name, reloc->addend, input_section->owner,
803 input_section, reloc->address)))
807 /* Now fix the instruction itself. */
808 switch (data[dst_address - 1])
812 bfd_put_8 (abfd, 0x55, data + dst_address - 1);
816 bfd_put_8 (abfd, 0x40, data + dst_address - 1);
823 /* Write out the 8-bit value. */
824 bfd_put_8 (abfd, gap, data + dst_address);
831 /* A 16-bit pc-relative branch that is now an 8-bit pc-relative branch. */
833 /* Get the address of the target of this branch. */
834 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
836 /* Get the address of the instruction (not the reloc). */
837 dot = (input_section->output_offset
839 + link_order->u.indirect.section->output_section->vma - 1);
844 if (gap < -128 || gap > 126)
846 if (! ((*link_info->callbacks->reloc_overflow)
848 bfd_asymbol_name (*reloc->sym_ptr_ptr),
849 reloc->howto->name, reloc->addend, input_section->owner,
850 input_section, reloc->address)))
854 /* Now fix the instruction. */
855 switch (data[dst_address - 2])
858 /* bCC:16 -> bCC:8 */
859 /* Get the second byte of the original insn, which contains
860 the condition code. */
861 tmp = data[dst_address - 1];
863 /* Compute the fisrt byte of the relaxed instruction. The
864 original sequence 0x58 0xX0 is relaxed to 0x4X, where X
865 represents the condition code. */
871 bfd_put_8 (abfd, tmp, data + dst_address - 2);
875 /* bsr:16 -> bsr:8 */
876 bfd_put_8 (abfd, 0x55, data + dst_address - 2);
883 /* Output the target. */
884 bfd_put_8 (abfd, gap, data + dst_address - 1);
886 /* We don't advance dst_address -- the 8-bit reloc is applied at
887 dst_address - 1, so the next insn should begin at dst_address. */
892 /* Similarly for a 24-bit absolute that is now 8 bits. */
894 /* Get the address of the target of this branch. */
895 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
897 /* Get the address of the instruction (not the reloc). */
898 dot = (input_section->output_offset
900 + link_order->u.indirect.section->output_section->vma + 2);
904 /* Fix the instruction. */
905 switch (data[src_address])
909 bfd_put_8 (abfd, 0x55, data + dst_address);
913 bfd_put_8 (abfd, 0x40, data + dst_address);
919 bfd_put_8 (abfd, gap, data + dst_address + 1);
925 /* This is a 16-bit absolute address in one of the following
928 "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
929 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
932 We may relax this into an 8-bit absolute address if it's in
935 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
937 /* All instructions with R_H8_DIR16B2 start with 0x6a. */
938 if (data[dst_address - 2] != 0x6a)
941 temp_code = data[src_address - 1];
943 /* If this is a mov.b instruction, clear the lower nibble, which
944 contains the source/destination register number. */
945 if ((temp_code & 0x10) != 0x10)
948 /* Fix up the opcode. */
952 /* This is mov.b @aa:16,Rd. */
953 data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x20;
956 /* This is mov.b Rs,@aa:16. */
957 data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x30;
960 /* This is a bit-maniputation instruction that stores one
961 bit into memory, one of "bclr", "bist", "bnot", "bset",
963 data[dst_address - 2] = 0x7f;
966 /* This is a bit-maniputation instruction that loads one bit
967 from memory, one of "band", "biand", "bild", "bior",
968 "bixor", "bld", "bor", "btst", and "bxor". */
969 data[dst_address - 2] = 0x7e;
975 bfd_put_8 (abfd, value & 0xff, data + dst_address - 1);
979 /* This is a 24-bit absolute address in one of the following
982 "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
983 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
986 We may relax this into an 8-bit absolute address if it's in
989 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
991 /* All instructions with R_MOV24B2 start with 0x6a. */
992 if (data[dst_address - 2] != 0x6a)
995 temp_code = data[src_address - 1];
997 /* If this is a mov.b instruction, clear the lower nibble, which
998 contains the source/destination register number. */
999 if ((temp_code & 0x30) != 0x30)
1002 /* Fix up the opcode. */
1006 /* This is mov.b @aa:24/32,Rd. */
1007 data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x20;
1010 /* This is mov.b Rs,@aa:24/32. */
1011 data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x30;
1014 /* This is a bit-maniputation instruction that stores one
1015 bit into memory, one of "bclr", "bist", "bnot", "bset",
1017 data[dst_address - 2] = 0x7f;
1020 /* This is a bit-maniputation instruction that loads one bit
1021 from memory, one of "band", "biand", "bild", "bior",
1022 "bixor", "bld", "bor", "btst", and "bxor". */
1023 data[dst_address - 2] = 0x7e;
1029 bfd_put_8 (abfd, value & 0xff, data + dst_address - 1);
1034 /* Get the address of the target of this branch. */
1035 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
1037 dot = (input_section->output_offset
1039 + link_order->u.indirect.section->output_section->vma) + 1;
1044 if (gap < -128 || gap > 126)
1046 if (! ((*link_info->callbacks->reloc_overflow)
1048 bfd_asymbol_name (*reloc->sym_ptr_ptr),
1049 reloc->howto->name, reloc->addend, input_section->owner,
1050 input_section, reloc->address)))
1054 /* Everything looks OK. Fix the condition in the instruction, apply
1055 the relocation, and update the src/dst address appropriately. */
1057 bfd_put_8 (abfd, bfd_get_8 (abfd, data + dst_address - 1) ^ 1,
1058 data + dst_address - 1);
1059 bfd_put_8 (abfd, gap, data + dst_address);
1070 /* An 8-bit memory indirect instruction (jmp/jsr).
1072 There's several things that need to be done to handle
1075 If this is a reloc against the absolute symbol, then
1076 we should handle it just R_RELBYTE. Likewise if it's
1077 for a symbol with a value ge 0 and le 0xff.
1079 Otherwise it's a jump/call through the function vector,
1080 and the linker is expected to set up the function vector
1081 and put the right value into the jump/call instruction. */
1082 case R_MEM_INDIRECT:
1084 /* We need to find the symbol so we can determine it's
1085 address in the function vector table. */
1088 struct funcvec_hash_table *ftab;
1089 struct funcvec_hash_entry *h;
1090 struct h8300_coff_link_hash_table *htab;
1091 asection *vectors_sec;
1093 if (link_info->output_bfd->xvec != abfd->xvec)
1095 (*_bfd_error_handler)
1096 (_("cannot handle R_MEM_INDIRECT reloc when using %s output"),
1097 link_info->output_bfd->xvec->name);
1099 /* What else can we do? This function doesn't allow return
1100 of an error, and we don't want to call abort as that
1101 indicates an internal error. */
1102 #ifndef EXIT_FAILURE
1103 #define EXIT_FAILURE 1
1105 xexit (EXIT_FAILURE);
1107 htab = h8300_coff_hash_table (link_info);
1108 vectors_sec = htab->vectors_sec;
1110 /* First see if this is a reloc against the absolute symbol
1111 or against a symbol with a nonnegative value <= 0xff. */
1112 symbol = *(reloc->sym_ptr_ptr);
1113 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
1114 if (symbol == bfd_abs_section_ptr->symbol
1117 /* This should be handled in a manner very similar to
1118 R_RELBYTES. If the value is in range, then just slam
1119 the value into the right location. Else trigger a
1120 reloc overflow callback. */
1123 bfd_put_8 (abfd, value, data + dst_address);
1129 if (! ((*link_info->callbacks->reloc_overflow)
1131 bfd_asymbol_name (*reloc->sym_ptr_ptr),
1132 reloc->howto->name, reloc->addend, input_section->owner,
1133 input_section, reloc->address)))
1139 /* This is a jump/call through a function vector, and we're
1140 expected to create the function vector ourselves.
1142 First look up this symbol in the linker hash table -- we need
1143 the derived linker symbol which holds this symbol's index
1144 in the function vector. */
1145 name = symbol->name;
1146 if (symbol->flags & BSF_LOCAL)
1148 char *new_name = bfd_malloc ((bfd_size_type) strlen (name) + 10);
1150 if (new_name == NULL)
1153 sprintf (new_name, "%s_%08x", name, symbol->section->id);
1157 ftab = htab->funcvec_hash_table;
1158 h = funcvec_hash_lookup (ftab, name, FALSE, FALSE);
1160 /* This shouldn't ever happen. If it does that means we've got
1161 data corruption of some kind. Aborting seems like a reasonable
1162 thing to do here. */
1163 if (h == NULL || vectors_sec == NULL)
1166 /* Place the address of the function vector entry into the
1169 vectors_sec->output_offset + h->offset,
1170 data + dst_address);
1175 /* Now create an entry in the function vector itself. */
1176 switch (bfd_get_mach (input_section->owner))
1178 case bfd_mach_h8300:
1179 case bfd_mach_h8300hn:
1180 case bfd_mach_h8300sn:
1182 bfd_coff_reloc16_get_value (reloc,
1185 vectors_sec->contents + h->offset);
1187 case bfd_mach_h8300h:
1188 case bfd_mach_h8300s:
1190 bfd_coff_reloc16_get_value (reloc,
1193 vectors_sec->contents + h->offset);
1199 /* Gross. We've already written the contents of the vector section
1200 before we get here... So we write it again with the new data. */
1201 bfd_set_section_contents (vectors_sec->output_section->owner,
1202 vectors_sec->output_section,
1203 vectors_sec->contents,
1204 (file_ptr) vectors_sec->output_offset,
1215 *src_ptr = src_address;
1216 *dst_ptr = dst_address;
1219 /* Routine for the h8300 linker.
1221 This routine is necessary to handle the special R_MEM_INDIRECT
1222 relocs on the h8300. It's responsible for generating a vectors
1223 section and attaching it to an input bfd as well as sizing
1224 the vectors section. It also creates our vectors hash table.
1226 It uses the generic linker routines to actually add the symbols.
1227 from this BFD to the bfd linker hash table. It may add a few
1228 selected static symbols to the bfd linker hash table. */
1231 h8300_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1234 struct funcvec_hash_table *funcvec_hash_table;
1236 struct h8300_coff_link_hash_table *htab;
1238 /* Add the symbols using the generic code. */
1239 _bfd_generic_link_add_symbols (abfd, info);
1241 if (info->output_bfd->xvec != abfd->xvec)
1244 htab = h8300_coff_hash_table (info);
1246 /* If we haven't created a vectors section, do so now. */
1247 if (!htab->vectors_sec)
1251 /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */
1252 flags = (SEC_ALLOC | SEC_LOAD
1253 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY);
1254 htab->vectors_sec = bfd_make_section_with_flags (abfd, ".vectors",
1257 /* If the section wasn't created, or we couldn't set the flags,
1258 quit quickly now, rather than dying a painful death later. */
1259 if (!htab->vectors_sec)
1262 /* Also create the vector hash table. */
1263 amt = sizeof (struct funcvec_hash_table);
1264 funcvec_hash_table = (struct funcvec_hash_table *) bfd_alloc (abfd, amt);
1266 if (!funcvec_hash_table)
1269 /* And initialize the funcvec hash table. */
1270 if (!funcvec_hash_table_init (funcvec_hash_table, abfd,
1271 funcvec_hash_newfunc,
1272 sizeof (struct funcvec_hash_entry)))
1274 bfd_release (abfd, funcvec_hash_table);
1278 /* Store away a pointer to the funcvec hash table. */
1279 htab->funcvec_hash_table = funcvec_hash_table;
1282 /* Load up the function vector hash table. */
1283 funcvec_hash_table = htab->funcvec_hash_table;
1285 /* Now scan the relocs for all the sections in this bfd; create
1286 additional space in the .vectors section as needed. */
1287 for (sec = abfd->sections; sec; sec = sec->next)
1289 long reloc_size, reloc_count, i;
1293 /* Suck in the relocs, symbols & canonicalize them. */
1294 reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
1295 if (reloc_size <= 0)
1298 relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
1302 /* The symbols should have been read in by _bfd_generic link_add_symbols
1303 call abovec, so we can cheat and use the pointer to them that was
1304 saved in the above call. */
1305 symbols = _bfd_generic_link_get_symbols(abfd);
1306 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, symbols);
1307 if (reloc_count <= 0)
1313 /* Now walk through all the relocations in this section. */
1314 for (i = 0; i < reloc_count; i++)
1316 arelent *reloc = relocs[i];
1317 asymbol *symbol = *(reloc->sym_ptr_ptr);
1320 /* We've got an indirect reloc. See if we need to add it
1321 to the function vector table. At this point, we have
1322 to add a new entry for each unique symbol referenced
1323 by an R_MEM_INDIRECT relocation except for a reloc
1324 against the absolute section symbol. */
1325 if (reloc->howto->type == R_MEM_INDIRECT
1326 && symbol != bfd_abs_section_ptr->symbol)
1329 struct funcvec_hash_table *ftab;
1330 struct funcvec_hash_entry *h;
1332 name = symbol->name;
1333 if (symbol->flags & BSF_LOCAL)
1337 new_name = bfd_malloc ((bfd_size_type) strlen (name) + 10);
1338 if (new_name == NULL)
1341 sprintf (new_name, "%s_%08x", name, symbol->section->id);
1345 /* Look this symbol up in the function vector hash table. */
1346 ftab = htab->funcvec_hash_table;
1347 h = funcvec_hash_lookup (ftab, name, FALSE, FALSE);
1349 /* If this symbol isn't already in the hash table, add
1350 it and bump up the size of the hash table. */
1353 h = funcvec_hash_lookup (ftab, name, TRUE, TRUE);
1360 /* Bump the size of the vectors section. Each vector
1361 takes 2 bytes on the h8300 and 4 bytes on the h8300h. */
1362 switch (bfd_get_mach (abfd))
1364 case bfd_mach_h8300:
1365 case bfd_mach_h8300hn:
1366 case bfd_mach_h8300sn:
1367 htab->vectors_sec->size += 2;
1369 case bfd_mach_h8300h:
1370 case bfd_mach_h8300s:
1371 htab->vectors_sec->size += 4;
1380 /* We're done with the relocations, release them. */
1384 /* Now actually allocate some space for the function vector. It's
1385 wasteful to do this more than once, but this is easier. */
1386 sec = htab->vectors_sec;
1389 /* Free the old contents. */
1391 free (sec->contents);
1393 /* Allocate new contents. */
1394 sec->contents = bfd_malloc (sec->size);
1400 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1401 #define coff_reloc16_estimate h8300_reloc16_estimate
1402 #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1403 #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
1405 #define COFF_LONG_FILENAMES
1407 #ifndef bfd_pe_print_pdata
1408 #define bfd_pe_print_pdata NULL
1411 #include "coffcode.h"
1413 #undef coff_bfd_get_relocated_section_contents
1414 #undef coff_bfd_relax_section
1415 #define coff_bfd_get_relocated_section_contents \
1416 bfd_coff_reloc16_get_relocated_section_contents
1417 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1419 CREATE_BIG_COFF_TARGET_VEC (h8300coff_vec, "coff-h8300", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)