1 /* BFD back-end for Hitachi H8/300 COFF binaries.
2 Copyright 1990, 91, 92, 93, 94, 95, 1996 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "coff/h8300.h"
28 #include "coff/internal.h"
31 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
33 /* We derive a hash table from the basic BFD hash table to
34 hold entries in the function vector. Aside from the
35 info stored by the basic hash table, we need the offset
36 of a particular entry within the hash table as well as
37 the offset where we'll add the next entry. */
39 struct funcvec_hash_entry
41 /* The basic hash table entry. */
42 struct bfd_hash_entry root
;
44 /* The offset within the vectors section where
49 struct funcvec_hash_table
51 /* The basic hash table. */
52 struct bfd_hash_table root
;
56 /* Offset at which we'll add the next entry. */
60 static struct bfd_hash_entry
*
62 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
65 funcvec_hash_table_init
66 PARAMS ((struct funcvec_hash_table
*, bfd
*,
67 struct bfd_hash_entry
*(*) PARAMS ((struct bfd_hash_entry
*,
68 struct bfd_hash_table
*,
71 /* To lookup a value in the function vector hash table. */
72 #define funcvec_hash_lookup(table, string, create, copy) \
73 ((struct funcvec_hash_entry *) \
74 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
76 /* The derived h8300 COFF linker table. Note it's derived from
77 the generic linker hash table, not the COFF backend linker hash
78 table! We use this to attach additional data structures we
79 need while linking on the h8300. */
80 struct h8300_coff_link_hash_table
82 /* The main hash table. */
83 struct generic_link_hash_table root
;
85 /* Section for the vectors table. This gets attached to a
86 random input bfd, we keep it here for easy access. */
87 asection
*vectors_sec
;
89 /* Hash table of the functions we need to enter into the function
91 struct funcvec_hash_table
*funcvec_hash_table
;
94 static struct bfd_link_hash_table
*h8300_coff_link_hash_table_create
97 /* Get the H8/300 COFF linker hash table from a link_info structure. */
99 #define h8300_coff_hash_table(p) \
100 ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p))))
102 /* Initialize fields within a funcvec hash table entry. Called whenever
103 a new entry is added to the funcvec hash table. */
105 static struct bfd_hash_entry
*
106 funcvec_hash_newfunc (entry
, gen_table
, string
)
107 struct bfd_hash_entry
*entry
;
108 struct bfd_hash_table
*gen_table
;
111 struct funcvec_hash_entry
*ret
;
112 struct funcvec_hash_table
*table
;
114 ret
= (struct funcvec_hash_entry
*) entry
;
115 table
= (struct funcvec_hash_table
*) gen_table
;
117 /* Allocate the structure if it has not already been allocated by a
120 ret
= ((struct funcvec_hash_entry
*)
121 bfd_hash_allocate (gen_table
,
122 sizeof (struct funcvec_hash_entry
)));
126 /* Call the allocation method of the superclass. */
127 ret
= ((struct funcvec_hash_entry
*)
128 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, gen_table
, string
));
133 /* Note where this entry will reside in the function vector table. */
134 ret
->offset
= table
->offset
;
136 /* Bump the offset at which we store entries in the function
137 vector. We'd like to bump up the size of the vectors section,
138 but it's not easily available here. */
139 if (bfd_get_mach (table
->abfd
) == bfd_mach_h8300
)
141 else if (bfd_get_mach (table
->abfd
) == bfd_mach_h8300h
)
146 /* Everything went OK. */
147 return (struct bfd_hash_entry
*) ret
;
150 /* Initialize the function vector hash table. */
153 funcvec_hash_table_init (table
, abfd
, newfunc
)
154 struct funcvec_hash_table
*table
;
156 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
157 struct bfd_hash_table
*,
160 /* Initialize our local fields, then call the generic initialization
164 return (bfd_hash_table_init (&table
->root
, newfunc
));
167 /* Create the derived linker hash table. We use a derived hash table
168 basically to hold "static" information during an h8/300 coff link
169 without using static variables. */
171 static struct bfd_link_hash_table
*
172 h8300_coff_link_hash_table_create (abfd
)
175 struct h8300_coff_link_hash_table
*ret
;
176 ret
= ((struct h8300_coff_link_hash_table
*)
177 bfd_alloc (abfd
, sizeof (struct h8300_coff_link_hash_table
)));
180 if (!_bfd_link_hash_table_init (&ret
->root
.root
, abfd
, _bfd_generic_link_hash_newfunc
))
182 bfd_release (abfd
, ret
);
186 /* Initialize our data. */
187 ret
->vectors_sec
= NULL
;
188 ret
->funcvec_hash_table
= NULL
;
190 /* OK. Everything's intialized, return the base pointer. */
191 return &ret
->root
.root
;
194 /* special handling for H8/300 relocs.
195 We only come here for pcrel stuff and return normally if not an -r link.
196 When doing -r, we can't do any arithmetic for the pcrel stuff, because
197 the code in reloc.c assumes that we can manipulate the targets of
198 the pcrel branches. This isn't so, since the H8/300 can do relaxing,
199 which means that the gap after the instruction may not be enough to
200 contain the offset required for the branch, so we have to use the only
201 the addend until the final link */
203 static bfd_reloc_status_type
204 special (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
207 arelent
*reloc_entry
;
210 asection
*input_section
;
212 char **error_message
;
214 if (output_bfd
== (bfd
*) NULL
)
215 return bfd_reloc_continue
;
220 static reloc_howto_type howto_table
[] =
222 HOWTO (R_RELBYTE
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "8", false, 0x000000ff, 0x000000ff, false),
223 HOWTO (R_RELWORD
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "16", false, 0x0000ffff, 0x0000ffff, false),
224 HOWTO (R_RELLONG
, 0, 2, 32, false, 0, complain_overflow_bitfield
, special
, "32", false, 0xffffffff, 0xffffffff, false),
225 HOWTO (R_PCRBYTE
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "DISP8", false, 0x000000ff, 0x000000ff, true),
226 HOWTO (R_PCRWORD
, 0, 1, 16, true, 0, complain_overflow_signed
, special
, "DISP16", false, 0x0000ffff, 0x0000ffff, true),
227 HOWTO (R_PCRLONG
, 0, 2, 32, true, 0, complain_overflow_signed
, special
, "DISP32", false, 0xffffffff, 0xffffffff, true),
228 HOWTO (R_MOV16B1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "relaxable mov.b:16", false, 0x0000ffff, 0x0000ffff, false),
229 HOWTO (R_MOV16B2
, 0, 1, 8, false, 0, complain_overflow_bitfield
, special
, "relaxed mov.b:16", false, 0x000000ff, 0x000000ff, false),
230 HOWTO (R_JMP1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "16/pcrel", false, 0x0000ffff, 0x0000ffff, false),
231 HOWTO (R_JMP2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "pcrecl/16", false, 0x000000ff, 0x000000ff, false),
232 HOWTO (R_JMPL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
, special
, "24/pcrell", false, 0x00ffffff, 0x00ffffff, false),
233 HOWTO (R_JMPL2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "pc8/24", false, 0x000000ff, 0x000000ff, false),
234 HOWTO (R_MOV24B1
, 0, 1, 32, false, 0, complain_overflow_bitfield
, special
, "relaxable mov.b:24", false, 0xffffffff, 0xffffffff, false),
235 HOWTO (R_MOV24B2
, 0, 1, 8, false, 0, complain_overflow_bitfield
, special
, "relaxed mov.b:24", false, 0x0000ffff, 0x0000ffff, false),
237 /* An indirect reference to a function. This causes the function's address
238 to be added to the function vector in lo-mem and puts the address of
239 the function vector's entry in the jsr instruction. */
240 HOWTO (R_MEM_INDIRECT
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "8/indirect", false, 0x000000ff, 0x000000ff, false),
242 /* Internal reloc for relaxing. This is created when a 16bit pc-relative
243 branch is turned into an 8bit pc-relative branch. */
244 HOWTO (R_PCRWORD_B
, 0, 0, 8, true, 0, complain_overflow_bitfield
, special
, "relaxed bCC:16", false, 0x000000ff, 0x000000ff, false),
246 HOWTO (R_MOVL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
,special
, "32/24 relaxable move", false, 0xffffffff, 0xffffffff, false),
248 HOWTO (R_MOVL2
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "32/24 relaxed move", false, 0x0000ffff, 0x0000ffff, false),
253 /* Turn a howto into a reloc number */
255 #define SELECT_RELOC(x,howto) \
256 { x.r_type = select_reloc(howto); }
258 #define BADMAG(x) (H8300BADMAG(x)&& H8300HBADMAG(x))
259 #define H8300 1 /* Customize coffcode.h */
260 #define __A_MAGIC_SET__
264 /* Code to swap in the reloc */
265 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
266 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
267 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
268 dst->r_stuff[0] = 'S'; \
269 dst->r_stuff[1] = 'C';
274 reloc_howto_type
*howto
;
279 /* Code to turn a r_type into a howto ptr, uses the above howto table
283 rtype2howto (internal
, dst
)
285 struct internal_reloc
*dst
;
290 internal
->howto
= howto_table
+ 0;
293 internal
->howto
= howto_table
+ 1;
296 internal
->howto
= howto_table
+ 2;
299 internal
->howto
= howto_table
+ 3;
302 internal
->howto
= howto_table
+ 4;
305 internal
->howto
= howto_table
+ 5;
308 internal
->howto
= howto_table
+ 6;
311 internal
->howto
= howto_table
+ 7;
314 internal
->howto
= howto_table
+ 8;
317 internal
->howto
= howto_table
+ 9;
320 internal
->howto
= howto_table
+ 10;
323 internal
->howto
= howto_table
+ 11;
326 internal
->howto
= howto_table
+ 12;
329 internal
->howto
= howto_table
+ 13;
332 internal
->howto
= howto_table
+ 14;
335 internal
->howto
= howto_table
+ 15;
338 internal
->howto
= howto_table
+ 16;
341 internal
->howto
= howto_table
+ 17;
349 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
352 /* Perform any necessaru magic to the addend in a reloc entry */
355 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
356 cache_ptr->addend = ext_reloc.r_offset;
359 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
360 reloc_processing(relent, reloc, symbols, abfd, section)
363 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
365 struct internal_reloc
*reloc
;
370 relent
->address
= reloc
->r_vaddr
;
371 rtype2howto (relent
, reloc
);
373 if (((int) reloc
->r_symndx
) > 0)
375 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
379 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
384 relent
->addend
= reloc
->r_offset
;
386 relent
->address
-= section
->vma
;
387 /* relent->section = 0;*/
390 /* If RELOC represents a relaxable instruction/reloc, change it into
391 the relaxed reloc, notify the linker that symbol addresses
392 have changed (bfd_perform_slip) and return how much the current
393 section has shrunk by.
395 FIXME: Much of this code has knowledge of the ordering of entries
396 in the howto table. This needs to be fixed. */
399 h8300_reloc16_estimate(abfd
, input_section
, reloc
, shrink
, link_info
)
401 asection
*input_section
;
404 struct bfd_link_info
*link_info
;
410 /* The address of the thing to be relocated will have moved back by
411 the size of the shrink - but we don't change reloc->address here,
412 since we need it to know where the relocation lives in the source
414 bfd_vma address
= reloc
->address
- shrink
;
416 /* Only examine the relocs which might be relaxable. */
417 switch (reloc
->howto
->type
)
420 /* This is the 16/24 bit absolute branch which could become an 8 bit
421 pc-relative branch. */
424 /* Get the address of the target of this branch. */
425 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
427 /* Get the address of the next instruction (not the reloc). */
428 dot
= (input_section
->output_section
->vma
429 + input_section
->output_offset
+ address
);
431 /* Adjust for R_JMP1 vs R_JMPL1. */
432 dot
+= (reloc
->howto
->type
== R_JMP1
? 1 : 2);
434 /* Compute the distance from this insn to the branch target. */
437 /* If the distance is within -128..+128 inclusive, then we can relax
438 this jump. +128 is valid since the target will move two bytes
440 if ((int)gap
>= -128 && (int)gap
<= 128 )
442 /* Change the reloc type. */
443 reloc
->howto
= reloc
->howto
+ 1;
445 /* This shrinks this section by two bytes. */
447 bfd_perform_slip(abfd
, 2, input_section
, address
);
451 /* This is the 16 bit pc-relative branch which could become an 8 bit
452 pc-relative branch. */
454 /* Get the address of the target of this branch, add one to the value
455 because the addend field in PCrel jumps is off by -1. */
456 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
) + 1;
458 /* Get the address of the next instruction if we were to relax. */
459 dot
= input_section
->output_section
->vma
+
460 input_section
->output_offset
+ address
;
462 /* Compute the distance from this insn to the branch target. */
465 /* If the distance is within -128..+128 inclusive, then we can relax
466 this jump. +128 is valid since the target will move two bytes
467 closer if we do relax this branch. */
468 if ((int)gap
>= -128 && (int)gap
<= 128 )
470 /* Change the reloc type. */
471 reloc
->howto
= howto_table
+ 15;
473 /* This shrinks this section by two bytes. */
475 bfd_perform_slip(abfd
, 2, input_section
, address
);
479 /* This is a 16 bit absolute address in a mov.b insn, which can
480 become an 8 bit absolute address if it's in the right range. */
482 /* Get the address of the data referenced by this mov.b insn. */
483 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
485 /* The address is in 0xff00..0xffff inclusive on the h8300 or
486 0xffff00..0xffffff inclusive on the h8300h, then we can
488 if ((bfd_get_mach (abfd
) == bfd_mach_h8300
491 || (bfd_get_mach (abfd
) == bfd_mach_h8300h
493 && value
<= 0xffffff))
495 /* Change the reloc type. */
496 reloc
->howto
= reloc
->howto
+ 1;
498 /* This shrinks this section by two bytes. */
500 bfd_perform_slip(abfd
, 2, input_section
, address
);
504 /* Similarly for a 24 bit absolute address in a mov.b. Note that
505 if we can't relax this into an 8 bit absolute, we'll fall through
506 and try to relax it into a 16bit absolute. */
508 /* Get the address of the data referenced by this mov.b insn. */
509 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
511 /* The address is in 0xffff00..0xffffff inclusive on the h8300h,
512 then we can relax this mov.b */
513 if (bfd_get_mach (abfd
) == bfd_mach_h8300h
515 && value
<= 0xffffff)
517 /* Change the reloc type. */
518 reloc
->howto
= reloc
->howto
+ 1;
520 /* This shrinks this section by four bytes. */
522 bfd_perform_slip(abfd
, 4, input_section
, address
);
524 /* Done with this reloc. */
528 /* FALLTHROUGH and try to turn the 32/24 bit reloc into a 16 bit
531 /* This is a 24/32 bit absolute address in a mov insn, which can
532 become an 16 bit absolute address if it's in the right range. */
534 /* Get the address of the data referenced by this mov insn. */
535 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
537 /* If this address is in 0x0000..0x7fff inclusive or
538 0xff8000..0xffffff inclusive, then it can be relaxed. */
539 if (value
<= 0x7fff || value
>= 0xff8000)
541 /* Change the reloc type. */
542 reloc
->howto
= howto_table
+ 17;
544 /* This shrinks this section by two bytes. */
546 bfd_perform_slip(abfd
, 2, input_section
, address
);
550 /* No other reloc types represent relaxing opportunities. */
559 /* Handle relocations for the H8/300, including relocs for relaxed
562 FIXME: Not all relocations check for overflow! */
565 h8300_reloc16_extra_cases (abfd
, link_info
, link_order
, reloc
, data
, src_ptr
,
568 struct bfd_link_info
*link_info
;
569 struct bfd_link_order
*link_order
;
572 unsigned int *src_ptr
;
573 unsigned int *dst_ptr
;
575 unsigned int src_address
= *src_ptr
;
576 unsigned int dst_address
= *dst_ptr
;
577 asection
*input_section
= link_order
->u
.indirect
.section
;
582 switch (reloc
->howto
->type
)
585 /* Generic 8bit pc-relative relocation. */
587 /* Get the address of the target of this branch. */
588 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
590 dot
= (link_order
->offset
592 + link_order
->u
.indirect
.section
->output_section
->vma
);
597 if (gap
< -128 || gap
> 126)
599 if (! ((*link_info
->callbacks
->reloc_overflow
)
600 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
601 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
602 input_section
, reloc
->address
)))
606 /* Everything looks OK. Apply the relocation and update the
607 src/dst address appropriately. */
609 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
616 /* Generic 16bit pc-relative relocation. */
618 /* Get the address of the target of this branch. */
619 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
621 /* Get the address of the instruction (not the reloc). */
622 dot
= (link_order
->offset
624 + link_order
->u
.indirect
.section
->output_section
->vma
+ 1);
629 if (gap
> 32766 || gap
< -32768)
631 if (! ((*link_info
->callbacks
->reloc_overflow
)
632 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
633 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
634 input_section
, reloc
->address
)))
638 /* Everything looks OK. Apply the relocation and update the
639 src/dst address appropriately. */
641 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
648 /* Generic 8bit absolute relocation. */
650 /* Get the address of the object referenced by this insn. */
651 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
655 || (value
>= 0x0000ff00 && value
<= 0x0000ffff)
656 || (value
>= 0x00ffff00 && value
<= 0x00ffffff)
657 || (value
>= 0xffffff00 && value
<= 0xffffffff))
659 /* Everything looks OK. Apply the relocation and update the
660 src/dst address appropriately. */
662 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
668 if (! ((*link_info
->callbacks
->reloc_overflow
)
669 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
670 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
671 input_section
, reloc
->address
)))
678 /* Various simple 16bit absolute relocations. */
682 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
683 bfd_put_16 (abfd
, value
, data
+ dst_address
);
688 /* Various simple 24/32bit absolute relocations. */
692 /* Get the address of the target of this branch. */
693 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
),
694 bfd_put_32 (abfd
, value
, data
+ dst_address
);
699 /* Another 24/32bit absolute relocation. */
701 /* Get the address of the target of this branch. */
702 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
704 value
= ((value
& 0x00ffffff)
705 | (bfd_get_32 (abfd
, data
+ src_address
) & 0xff000000));
706 bfd_put_32 (abfd
, value
, data
+ dst_address
);
711 /* A 16bit abolute relocation that was formerlly a 24/32bit
712 absolute relocation. */
714 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
717 if (value
< 0x8000 || value
> 0xff8000)
719 /* Insert the 16bit value into the proper location. */
720 bfd_put_16 (abfd
, value
, data
+ dst_address
);
722 /* Fix the opcode. For all the move insns, we simply
723 need to turn off bit 0x20 in the previous byte. */
724 data
[dst_address
- 1] &= ~0x20;
730 if (! ((*link_info
->callbacks
->reloc_overflow
)
731 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
732 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
733 input_section
, reloc
->address
)))
738 /* A 16bit absolute branch that is now an 8-bit pc-relative branch. */
740 /* Get the address of the target of this branch. */
741 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
743 /* Get the address of the next instruction. */
744 dot
= (link_order
->offset
746 + link_order
->u
.indirect
.section
->output_section
->vma
+ 1);
751 if (gap
< -128 || gap
> 126)
753 if (! ((*link_info
->callbacks
->reloc_overflow
)
754 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
755 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
756 input_section
, reloc
->address
)))
760 /* Now fix the instruction itself. */
761 switch (data
[dst_address
- 1])
765 bfd_put_8 (abfd
, 0x55, data
+ dst_address
- 1);
769 bfd_put_8 (abfd
, 0x40, data
+ dst_address
- 1);
776 /* Write out the 8bit value. */
777 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
784 /* A 16bit pc-relative 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 instruction (not the reloc). */
790 dot
= (link_order
->offset
792 + link_order
->u
.indirect
.section
->output_section
->vma
- 1);
797 if (gap
< -128 || gap
> 126)
799 if (! ((*link_info
->callbacks
->reloc_overflow
)
800 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
801 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
802 input_section
, reloc
->address
)))
806 /* Now fix the instruction. */
807 switch (data
[dst_address
- 2])
810 /* bCC:16 -> bCC:8 */
811 /* Get the condition code from the original insn. */
812 tmp
= data
[dst_address
- 1];
816 /* Now or in the high nibble of the opcode. */
820 bfd_put_8 (abfd
, tmp
, data
+ dst_address
- 2);
827 /* Output the target. */
828 bfd_put_8 (abfd
, gap
, data
+ dst_address
- 1);
830 /* We don't advance dst_address -- the 8bit reloc is applied at
831 dst_address - 1, so the next insn should begin at dst_address. */
836 /* Similarly for a 24bit absolute that is now 8 bits. */
838 /* Get the address of the target of this branch. */
839 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
841 /* Get the address of the instruction (not the reloc). */
842 dot
= (link_order
->offset
844 + link_order
->u
.indirect
.section
->output_section
->vma
+ 2);
848 /* Fix the instruction. */
849 switch (data
[src_address
])
853 bfd_put_8 (abfd
, 0x55, data
+ dst_address
);
857 bfd_put_8 (abfd
, 0x40, data
+ dst_address
);
863 bfd_put_8 (abfd
, gap
, data
+ dst_address
+ 1);
869 /* A 16bit absolute mov.b that is now an 8bit absolute mov.b. */
871 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
874 if (data
[dst_address
- 2] != 0x6a)
877 /* Fix up the opcode. */
878 switch (data
[src_address
-1] & 0xf0)
881 data
[dst_address
- 2] = (data
[src_address
-1] & 0xf) | 0x20;
884 data
[dst_address
- 2] = (data
[src_address
-1] & 0xf) | 0x30;
890 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
- 1);
894 /* Similarly for a 24bit mov.b */
896 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
899 if (data
[dst_address
- 2] != 0x6a)
902 /* Fix up the opcode. */
903 switch (data
[src_address
-1] & 0xf0)
906 data
[dst_address
- 2] = (data
[src_address
-1] & 0xf) | 0x20;
909 data
[dst_address
- 2] = (data
[src_address
-1] & 0xf) | 0x30;
915 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
- 1);
919 /* An 8bit memory indirect instruction (jmp/jsr).
921 There's several things that need to be done to handle
924 If this is a reloc against the absolute symbol, then
925 we should handle it just R_RELBYTE. Likewise if it's
926 for a symbol with a value ge 0 and le 0xff.
928 Otherwise it's a jump/call through the function vector,
929 and the linker is expected to set up the function vector
930 and put the right value into the jump/call instruction. */
933 /* We need to find the symbol so we can determine it's
934 address in the function vector table. */
938 struct funcvec_hash_entry
*h
;
939 asection
*vectors_sec
= h8300_coff_hash_table (link_info
)->vectors_sec
;
941 /* First see if this is a reloc against the absolute symbol
942 or against a symbol with a nonnegative value <= 0xff. */
943 symbol
= *(reloc
->sym_ptr_ptr
);
944 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
945 if (symbol
== bfd_abs_section_ptr
->symbol
946 || (value
>= 0 && value
<= 0xff))
948 /* This should be handled in a manner very similar to
949 R_RELBYTES. If the value is in range, then just slam
950 the value into the right location. Else trigger a
951 reloc overflow callback. */
952 if (value
>= 0 && value
<= 0xff)
954 bfd_put_8 (abfd
, value
, data
+ dst_address
);
960 if (! ((*link_info
->callbacks
->reloc_overflow
)
961 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
962 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
963 input_section
, reloc
->address
)))
969 /* This is a jump/call through a function vector, and we're
970 expected to create the function vector ourselves.
972 First look up this symbol in the linker hash table -- we need
973 the derived linker symbol which holds this symbol's index
974 in the function vector. */
976 if (symbol
->flags
& BSF_LOCAL
)
978 char *new_name
= bfd_malloc (strlen (name
) + 9);
979 if (new_name
== NULL
)
982 strcpy (new_name
, name
);
983 sprintf (new_name
+ strlen (name
), "_%08x",
984 (int)symbol
->section
);
988 h
= funcvec_hash_lookup (h8300_coff_hash_table (link_info
)->funcvec_hash_table
,
991 /* This shouldn't ever happen. If it does that means we've got
992 data corruption of some kind. Aborting seems like a reasonable
994 if (h
== NULL
|| vectors_sec
== NULL
)
997 /* Place the address of the function vector entry into the
1000 vectors_sec
->output_offset
+ h
->offset
,
1001 data
+ dst_address
);
1006 /* Now create an entry in the function vector itself. */
1007 if (bfd_get_mach (input_section
->owner
) == bfd_mach_h8300
)
1009 bfd_coff_reloc16_get_value (reloc
,
1012 vectors_sec
->contents
+ h
->offset
);
1013 else if (bfd_get_mach (input_section
->owner
) == bfd_mach_h8300h
)
1015 bfd_coff_reloc16_get_value (reloc
,
1018 vectors_sec
->contents
+ h
->offset
);
1022 /* Gross. We've already written the contents of the vector section
1023 before we get here... So we write it again with the new data. */
1024 bfd_set_section_contents (vectors_sec
->output_section
->owner
,
1025 vectors_sec
->output_section
,
1026 vectors_sec
->contents
,
1027 vectors_sec
->output_offset
,
1028 vectors_sec
->_raw_size
);
1038 *src_ptr
= src_address
;
1039 *dst_ptr
= dst_address
;
1043 /* Routine for the h8300 linker.
1045 This routine is necessary to handle the special R_MEM_INDIRECT
1046 relocs on the h8300. It's responsible for generating a vectors
1047 section and attaching it to an input bfd as well as sizing
1048 the vectors section. It also creates our vectors hash table.
1050 It uses the generic linker routines to actually add the symbols.
1051 from this BFD to the bfd linker hash table. It may add a few
1052 selected static symbols to the bfd linker hash table. */
1055 h8300_bfd_link_add_symbols(abfd
, info
)
1057 struct bfd_link_info
*info
;
1060 struct funcvec_hash_table
*funcvec_hash_table
;
1062 /* If we haven't created a vectors section, do so now. */
1063 if (!h8300_coff_hash_table (info
)->vectors_sec
)
1067 /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */
1068 flags
= (SEC_ALLOC
| SEC_LOAD
1069 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_READONLY
);
1070 h8300_coff_hash_table (info
)->vectors_sec
= bfd_make_section (abfd
,
1073 /* If the section wasn't created, or we couldn't set the flags,
1074 quit quickly now, rather than dieing a painful death later. */
1075 if (! h8300_coff_hash_table (info
)->vectors_sec
1076 || ! bfd_set_section_flags (abfd
,
1077 h8300_coff_hash_table(info
)->vectors_sec
,
1081 /* Also create the vector hash table. */
1082 funcvec_hash_table
= ((struct funcvec_hash_table
*)
1083 bfd_alloc (abfd
, sizeof (struct funcvec_hash_table
)));
1085 if (!funcvec_hash_table
)
1088 /* And initialize the funcvec hash table. */
1089 if (!funcvec_hash_table_init (funcvec_hash_table
, abfd
,
1090 funcvec_hash_newfunc
))
1092 bfd_release (abfd
, funcvec_hash_table
);
1096 /* Store away a pointer to the funcvec hash table. */
1097 h8300_coff_hash_table (info
)->funcvec_hash_table
= funcvec_hash_table
;
1100 /* Load up the function vector hash table. */
1101 funcvec_hash_table
= h8300_coff_hash_table (info
)->funcvec_hash_table
;
1103 /* Add the symbols using the generic code. */
1104 _bfd_generic_link_add_symbols (abfd
, info
);
1106 /* Now scan the relocs for all the sections in this bfd; create
1107 additional space in the .vectors section as needed. */
1108 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1110 unsigned long reloc_size
, reloc_count
, i
;
1114 /* Suck in the relocs, symbols & canonicalize them. */
1115 reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
1116 if (reloc_size
<= 0)
1119 relocs
= (arelent
**)bfd_malloc ((size_t)reloc_size
);
1123 /* The symbols should have been read in by _bfd_generic link_add_symbols
1124 call abovec, so we can cheat and use the pointer to them that was
1125 saved in the above call. */
1126 symbols
= _bfd_generic_link_get_symbols(abfd
);
1127 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, symbols
);
1129 /* Now walk through all the relocations in this section. */
1130 for (i
= 0; i
< reloc_count
; i
++)
1132 arelent
*reloc
= relocs
[i
];
1133 asymbol
*symbol
= *(reloc
->sym_ptr_ptr
);
1136 /* We've got an indirect reloc. See if we need to add it
1137 to the function vector table. At this point, we have
1138 to add a new entry for each unique symbol referenced
1139 by an R_MEM_INDIRECT relocation except for a reloc
1140 against the absolute section symbol. */
1141 if (reloc
->howto
->type
== R_MEM_INDIRECT
1142 && symbol
!= bfd_abs_section_ptr
->symbol
)
1145 struct funcvec_hash_entry
*h
;
1147 name
= symbol
->name
;
1148 if (symbol
->flags
& BSF_LOCAL
)
1150 char *new_name
= bfd_malloc (strlen (name
) + 9);
1152 if (new_name
== NULL
)
1155 strcpy (new_name
, name
);
1156 sprintf (new_name
+ strlen (name
), "_%08x",
1157 (int)symbol
->section
);
1161 /* Look this symbol up in the function vector hash table. */
1162 h
= funcvec_hash_lookup (h8300_coff_hash_table (info
)->funcvec_hash_table
,
1163 name
, false, false);
1166 /* If this symbol isn't already in the hash table, add
1167 it and bump up the size of the hash table. */
1170 h
= funcvec_hash_lookup (h8300_coff_hash_table (info
)->funcvec_hash_table
,
1178 /* Bump the size of the vectors section. Each vector
1179 takes 2 bytes on the h8300 and 4 bytes on the h8300h. */
1180 if (bfd_get_mach (abfd
) == bfd_mach_h8300
)
1181 h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
+= 2;
1182 else if (bfd_get_mach (abfd
) == bfd_mach_h8300h
)
1183 h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
+= 4;
1188 /* We're done with the relocations, release them. */
1192 /* Now actually allocate some space for the function vector. It's
1193 wasteful to do this more than once, but this is easier. */
1194 if (h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
!= 0)
1196 /* Free the old contents. */
1197 if (h8300_coff_hash_table (info
)->vectors_sec
->contents
)
1198 free (h8300_coff_hash_table (info
)->vectors_sec
->contents
);
1200 /* Allocate new contents. */
1201 h8300_coff_hash_table (info
)->vectors_sec
->contents
1202 = bfd_malloc (h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
);
1208 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1209 #define coff_reloc16_estimate h8300_reloc16_estimate
1210 #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1211 #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
1213 #define COFF_LONG_FILENAMES
1214 #include "coffcode.h"
1217 #undef coff_bfd_get_relocated_section_contents
1218 #undef coff_bfd_relax_section
1219 #define coff_bfd_get_relocated_section_contents \
1220 bfd_coff_reloc16_get_relocated_section_contents
1221 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1225 const bfd_target h8300coff_vec
=
1227 "coff-h8300", /* name */
1228 bfd_target_coff_flavour
,
1229 BFD_ENDIAN_BIG
, /* data byte order is big */
1230 BFD_ENDIAN_BIG
, /* header byte order is big */
1232 (HAS_RELOC
| EXEC_P
| /* object flags */
1233 HAS_LINENO
| HAS_DEBUG
|
1234 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| BFD_IS_RELAXABLE
),
1235 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1236 '_', /* leading char */
1237 '/', /* ar_pad_char */
1238 15, /* ar_max_namelen */
1239 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1240 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1241 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1242 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1243 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1244 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1246 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
1247 bfd_generic_archive_p
, _bfd_dummy_target
},
1248 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
1250 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
1251 _bfd_write_archive_contents
, bfd_false
},
1253 BFD_JUMP_TABLE_GENERIC (coff
),
1254 BFD_JUMP_TABLE_COPY (coff
),
1255 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
1256 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
1257 BFD_JUMP_TABLE_SYMBOLS (coff
),
1258 BFD_JUMP_TABLE_RELOCS (coff
),
1259 BFD_JUMP_TABLE_WRITE (coff
),
1260 BFD_JUMP_TABLE_LINK (coff
),
1261 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),