1 /* BFD back-end for Hitachi H8/300 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "coff/h8300.h"
29 #include "coff/internal.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. */
61 static struct bfd_hash_entry
*
63 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
66 funcvec_hash_table_init
67 PARAMS ((struct funcvec_hash_table
*, bfd
*,
68 struct bfd_hash_entry
*(*) PARAMS ((struct bfd_hash_entry
*,
69 struct bfd_hash_table
*,
72 /* To lookup a value in the function vector hash table. */
73 #define funcvec_hash_lookup(table, string, create, copy) \
74 ((struct funcvec_hash_entry *) \
75 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
77 /* The derived h8300 COFF linker table. Note it's derived from
78 the generic linker hash table, not the COFF backend linker hash
79 table! We use this to attach additional data structures we
80 need while linking on the h8300. */
81 struct h8300_coff_link_hash_table
83 /* The main hash table. */
84 struct generic_link_hash_table root
;
86 /* Section for the vectors table. This gets attached to a
87 random input bfd, we keep it here for easy access. */
88 asection
*vectors_sec
;
90 /* Hash table of the functions we need to enter into the function
92 struct funcvec_hash_table
*funcvec_hash_table
;
95 static struct bfd_link_hash_table
*h8300_coff_link_hash_table_create
98 /* Get the H8/300 COFF linker hash table from a link_info structure. */
100 #define h8300_coff_hash_table(p) \
101 ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p))))
103 /* Initialize fields within a funcvec hash table entry. Called whenever
104 a new entry is added to the funcvec hash table. */
106 static struct bfd_hash_entry
*
107 funcvec_hash_newfunc (entry
, gen_table
, string
)
108 struct bfd_hash_entry
*entry
;
109 struct bfd_hash_table
*gen_table
;
112 struct funcvec_hash_entry
*ret
;
113 struct funcvec_hash_table
*table
;
115 ret
= (struct funcvec_hash_entry
*) entry
;
116 table
= (struct funcvec_hash_table
*) gen_table
;
118 /* Allocate the structure if it has not already been allocated by a
121 ret
= ((struct funcvec_hash_entry
*)
122 bfd_hash_allocate (gen_table
,
123 sizeof (struct funcvec_hash_entry
)));
127 /* Call the allocation method of the superclass. */
128 ret
= ((struct funcvec_hash_entry
*)
129 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, gen_table
, string
));
134 /* Note where this entry will reside in the function vector table. */
135 ret
->offset
= table
->offset
;
137 /* Bump the offset at which we store entries in the function
138 vector. We'd like to bump up the size of the vectors section,
139 but it's not easily available here. */
140 if (bfd_get_mach (table
->abfd
) == bfd_mach_h8300
)
142 else if (bfd_get_mach (table
->abfd
) == bfd_mach_h8300h
143 || bfd_get_mach (table
->abfd
) == bfd_mach_h8300s
)
148 /* Everything went OK. */
149 return (struct bfd_hash_entry
*) ret
;
152 /* Initialize the function vector hash table. */
155 funcvec_hash_table_init (table
, abfd
, newfunc
)
156 struct funcvec_hash_table
*table
;
158 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
159 struct bfd_hash_table
*,
162 /* Initialize our local fields, then call the generic initialization
166 return (bfd_hash_table_init (&table
->root
, newfunc
));
169 /* Create the derived linker hash table. We use a derived hash table
170 basically to hold "static" information during an h8/300 coff link
171 without using static variables. */
173 static struct bfd_link_hash_table
*
174 h8300_coff_link_hash_table_create (abfd
)
177 struct h8300_coff_link_hash_table
*ret
;
178 ret
= ((struct h8300_coff_link_hash_table
*)
179 bfd_alloc (abfd
, sizeof (struct h8300_coff_link_hash_table
)));
182 if (!_bfd_link_hash_table_init (&ret
->root
.root
, abfd
, _bfd_generic_link_hash_newfunc
))
184 bfd_release (abfd
, ret
);
188 /* Initialize our data. */
189 ret
->vectors_sec
= NULL
;
190 ret
->funcvec_hash_table
= NULL
;
192 /* OK. Everything's intialized, return the base pointer. */
193 return &ret
->root
.root
;
196 /* Special handling for H8/300 relocs.
197 We only come here for pcrel stuff and return normally if not an -r link.
198 When doing -r, we can't do any arithmetic for the pcrel stuff, because
199 the code in reloc.c assumes that we can manipulate the targets of
200 the pcrel branches. This isn't so, since the H8/300 can do relaxing,
201 which means that the gap after the instruction may not be enough to
202 contain the offset required for the branch, so we have to use only
203 the addend until the final link. */
205 static bfd_reloc_status_type
206 special (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
208 bfd
*abfd ATTRIBUTE_UNUSED
;
209 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
210 asymbol
*symbol ATTRIBUTE_UNUSED
;
211 PTR data ATTRIBUTE_UNUSED
;
212 asection
*input_section ATTRIBUTE_UNUSED
;
214 char **error_message ATTRIBUTE_UNUSED
;
216 if (output_bfd
== (bfd
*) NULL
)
217 return bfd_reloc_continue
;
219 /* Adjust the reloc address to that in the output section. */
220 reloc_entry
->address
+= input_section
->output_offset
;
224 static reloc_howto_type howto_table
[] =
226 HOWTO (R_RELBYTE
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "8", false, 0x000000ff, 0x000000ff, false),
227 HOWTO (R_RELWORD
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "16", false, 0x0000ffff, 0x0000ffff, false),
228 HOWTO (R_RELLONG
, 0, 2, 32, false, 0, complain_overflow_bitfield
, special
, "32", false, 0xffffffff, 0xffffffff, false),
229 HOWTO (R_PCRBYTE
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "DISP8", false, 0x000000ff, 0x000000ff, true),
230 HOWTO (R_PCRWORD
, 0, 1, 16, true, 0, complain_overflow_signed
, special
, "DISP16", false, 0x0000ffff, 0x0000ffff, true),
231 HOWTO (R_PCRLONG
, 0, 2, 32, true, 0, complain_overflow_signed
, special
, "DISP32", false, 0xffffffff, 0xffffffff, true),
232 HOWTO (R_MOV16B1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "relaxable mov.b:16", false, 0x0000ffff, 0x0000ffff, false),
233 HOWTO (R_MOV16B2
, 0, 1, 8, false, 0, complain_overflow_bitfield
, special
, "relaxed mov.b:16", false, 0x000000ff, 0x000000ff, false),
234 HOWTO (R_JMP1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "16/pcrel", false, 0x0000ffff, 0x0000ffff, false),
235 HOWTO (R_JMP2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "pcrecl/16", false, 0x000000ff, 0x000000ff, false),
236 HOWTO (R_JMPL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
, special
, "24/pcrell", false, 0x00ffffff, 0x00ffffff, false),
237 HOWTO (R_JMPL2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "pc8/24", false, 0x000000ff, 0x000000ff, false),
238 HOWTO (R_MOV24B1
, 0, 1, 32, false, 0, complain_overflow_bitfield
, special
, "relaxable mov.b:24", false, 0xffffffff, 0xffffffff, false),
239 HOWTO (R_MOV24B2
, 0, 1, 8, false, 0, complain_overflow_bitfield
, special
, "relaxed mov.b:24", false, 0x0000ffff, 0x0000ffff, false),
241 /* An indirect reference to a function. This causes the function's address
242 to be added to the function vector in lo-mem and puts the address of
243 the function vector's entry in the jsr instruction. */
244 HOWTO (R_MEM_INDIRECT
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "8/indirect", false, 0x000000ff, 0x000000ff, false),
246 /* Internal reloc for relaxing. This is created when a 16bit pc-relative
247 branch is turned into an 8bit pc-relative branch. */
248 HOWTO (R_PCRWORD_B
, 0, 0, 8, true, 0, complain_overflow_bitfield
, special
, "relaxed bCC:16", false, 0x000000ff, 0x000000ff, false),
250 HOWTO (R_MOVL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
,special
, "32/24 relaxable move", false, 0xffffffff, 0xffffffff, false),
252 HOWTO (R_MOVL2
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "32/24 relaxed move", false, 0x0000ffff, 0x0000ffff, false),
254 HOWTO (R_BCC_INV
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "DISP8 inverted", false, 0x000000ff, 0x000000ff, true),
256 HOWTO (R_JMP_DEL
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "Deleted jump", false, 0x000000ff, 0x000000ff, true),
259 /* Turn a howto into a reloc number. */
261 #define SELECT_RELOC(x,howto) \
262 { x.r_type = select_reloc(howto); }
264 #define BADMAG(x) (H8300BADMAG(x) && H8300HBADMAG(x) && H8300SBADMAG(x))
265 #define H8300 1 /* Customize coffcode.h */
266 #define __A_MAGIC_SET__
268 /* Code to swap in the reloc. */
269 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
270 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
271 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
272 dst->r_stuff[0] = 'S'; \
273 dst->r_stuff[1] = 'C';
277 reloc_howto_type
*howto
;
282 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
285 rtype2howto (internal
, dst
)
287 struct internal_reloc
*dst
;
292 internal
->howto
= howto_table
+ 0;
295 internal
->howto
= howto_table
+ 1;
298 internal
->howto
= howto_table
+ 2;
301 internal
->howto
= howto_table
+ 3;
304 internal
->howto
= howto_table
+ 4;
307 internal
->howto
= howto_table
+ 5;
310 internal
->howto
= howto_table
+ 6;
313 internal
->howto
= howto_table
+ 7;
316 internal
->howto
= howto_table
+ 8;
319 internal
->howto
= howto_table
+ 9;
322 internal
->howto
= howto_table
+ 10;
325 internal
->howto
= howto_table
+ 11;
328 internal
->howto
= howto_table
+ 12;
331 internal
->howto
= howto_table
+ 13;
334 internal
->howto
= howto_table
+ 14;
337 internal
->howto
= howto_table
+ 15;
340 internal
->howto
= howto_table
+ 16;
343 internal
->howto
= howto_table
+ 17;
346 internal
->howto
= howto_table
+ 18;
349 internal
->howto
= howto_table
+ 19;
357 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
359 /* Perform any necessary magic to the addend in a reloc entry. */
361 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
362 cache_ptr->addend = ext_reloc.r_offset;
364 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
365 reloc_processing(relent, reloc, symbols, abfd, section)
368 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
370 struct internal_reloc
*reloc
;
375 relent
->address
= reloc
->r_vaddr
;
376 rtype2howto (relent
, reloc
);
378 if (((int) reloc
->r_symndx
) > 0)
380 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
384 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
387 relent
->addend
= reloc
->r_offset
;
389 relent
->address
-= section
->vma
;
396 h8300_symbol_address_p (abfd
, input_section
, address
)
398 asection
*input_section
;
403 s
= _bfd_generic_link_get_symbols (abfd
);
404 BFD_ASSERT (s
!= (asymbol
**) NULL
);
406 /* Search all the symbols for one in INPUT_SECTION with
411 if (p
->section
== input_section
412 && (input_section
->output_section
->vma
413 + input_section
->output_offset
414 + p
->value
) == address
)
421 /* If RELOC represents a relaxable instruction/reloc, change it into
422 the relaxed reloc, notify the linker that symbol addresses
423 have changed (bfd_perform_slip) and return how much the current
424 section has shrunk by.
426 FIXME: Much of this code has knowledge of the ordering of entries
427 in the howto table. This needs to be fixed. */
430 h8300_reloc16_estimate (abfd
, input_section
, reloc
, shrink
, link_info
)
432 asection
*input_section
;
435 struct bfd_link_info
*link_info
;
440 static asection
*last_input_section
= NULL
;
441 static arelent
*last_reloc
= NULL
;
443 /* The address of the thing to be relocated will have moved back by
444 the size of the shrink - but we don't change reloc->address here,
445 since we need it to know where the relocation lives in the source
447 bfd_vma address
= reloc
->address
- shrink
;
449 if (input_section
!= last_input_section
)
452 /* Only examine the relocs which might be relaxable. */
453 switch (reloc
->howto
->type
)
455 /* This is the 16/24 bit absolute branch which could become an 8 bit
456 pc-relative branch. */
459 /* Get the address of the target of this branch. */
460 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
462 /* Get the address of the next instruction (not the reloc). */
463 dot
= (input_section
->output_section
->vma
464 + input_section
->output_offset
+ address
);
466 /* Adjust for R_JMP1 vs R_JMPL1. */
467 dot
+= (reloc
->howto
->type
== R_JMP1
? 1 : 2);
469 /* Compute the distance from this insn to the branch target. */
472 /* If the distance is within -128..+128 inclusive, then we can relax
473 this jump. +128 is valid since the target will move two bytes
474 closer if we do relax this branch. */
475 if ((int)gap
>= -128 && (int)gap
<= 128 )
477 /* It's possible we may be able to eliminate this branch entirely;
478 if the previous instruction is a branch around this instruction,
479 and there's no label at this instruction, then we can reverse
480 the condition on the previous branch and eliminate this jump.
487 This saves 4 bytes instead of two, and should be relatively
492 && last_reloc
->howto
->type
== R_PCRBYTE
)
495 last_value
= bfd_coff_reloc16_get_value (last_reloc
, link_info
,
498 if (last_value
== dot
+ 2
499 && last_reloc
->address
+ 1 == reloc
->address
500 && !h8300_symbol_address_p (abfd
, input_section
, dot
- 2))
502 reloc
->howto
= howto_table
+ 19;
503 last_reloc
->howto
= howto_table
+ 18;
504 last_reloc
->sym_ptr_ptr
= reloc
->sym_ptr_ptr
;
505 last_reloc
->addend
= reloc
->addend
;
507 bfd_perform_slip (abfd
, 4, input_section
, address
);
512 /* Change the reloc type. */
513 reloc
->howto
= reloc
->howto
+ 1;
515 /* This shrinks this section by two bytes. */
517 bfd_perform_slip (abfd
, 2, input_section
, address
);
521 /* This is the 16 bit pc-relative branch which could become an 8 bit
522 pc-relative branch. */
524 /* Get the address of the target of this branch, add one to the value
525 because the addend field in PCrel jumps is off by -1. */
526 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
) + 1;
528 /* Get the address of the next instruction if we were to relax. */
529 dot
= input_section
->output_section
->vma
+
530 input_section
->output_offset
+ address
;
532 /* Compute the distance from this insn to the branch target. */
535 /* If the distance is within -128..+128 inclusive, then we can relax
536 this jump. +128 is valid since the target will move two bytes
537 closer if we do relax this branch. */
538 if ((int)gap
>= -128 && (int)gap
<= 128 )
540 /* Change the reloc type. */
541 reloc
->howto
= howto_table
+ 15;
543 /* This shrinks this section by two bytes. */
545 bfd_perform_slip (abfd
, 2, input_section
, address
);
549 /* This is a 16 bit absolute address in a mov.b insn, which can
550 become an 8 bit absolute address if it's in the right range. */
552 /* Get the address of the data referenced by this mov.b insn. */
553 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
555 /* The address is in 0xff00..0xffff inclusive on the h8300 or
556 0xffff00..0xffffff inclusive on the h8300h, then we can
558 if ((bfd_get_mach (abfd
) == bfd_mach_h8300
561 || ((bfd_get_mach (abfd
) == bfd_mach_h8300h
562 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
564 && value
<= 0xffffff))
566 /* Change the reloc type. */
567 reloc
->howto
= reloc
->howto
+ 1;
569 /* This shrinks this section by two bytes. */
571 bfd_perform_slip (abfd
, 2, input_section
, address
);
575 /* Similarly for a 24 bit absolute address in a mov.b. Note that
576 if we can't relax this into an 8 bit absolute, we'll fall through
577 and try to relax it into a 16bit absolute. */
579 /* Get the address of the data referenced by this mov.b insn. */
580 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
582 /* The address is in 0xffff00..0xffffff inclusive on the h8300h,
583 then we can relax this mov.b */
584 if ((bfd_get_mach (abfd
) == bfd_mach_h8300h
585 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
587 && value
<= 0xffffff)
589 /* Change the reloc type. */
590 reloc
->howto
= reloc
->howto
+ 1;
592 /* This shrinks this section by four bytes. */
594 bfd_perform_slip (abfd
, 4, input_section
, address
);
596 /* Done with this reloc. */
600 /* FALLTHROUGH and try to turn the 32/24 bit reloc into a 16 bit
603 /* This is a 24/32 bit absolute address in a mov insn, which can
604 become an 16 bit absolute address if it's in the right range. */
606 /* Get the address of the data referenced by this mov insn. */
607 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
609 /* If this address is in 0x0000..0x7fff inclusive or
610 0xff8000..0xffffff inclusive, then it can be relaxed. */
611 if (value
<= 0x7fff || value
>= 0xff8000)
613 /* Change the reloc type. */
614 reloc
->howto
= howto_table
+ 17;
616 /* This shrinks this section by two bytes. */
618 bfd_perform_slip (abfd
, 2, input_section
, address
);
622 /* No other reloc types represent relaxing opportunities. */
628 last_input_section
= input_section
;
632 /* Handle relocations for the H8/300, including relocs for relaxed
635 FIXME: Not all relocations check for overflow! */
638 h8300_reloc16_extra_cases (abfd
, link_info
, link_order
, reloc
, data
, src_ptr
,
641 struct bfd_link_info
*link_info
;
642 struct bfd_link_order
*link_order
;
645 unsigned int *src_ptr
;
646 unsigned int *dst_ptr
;
648 unsigned int src_address
= *src_ptr
;
649 unsigned int dst_address
= *dst_ptr
;
650 asection
*input_section
= link_order
->u
.indirect
.section
;
655 switch (reloc
->howto
->type
)
657 /* Generic 8bit pc-relative relocation. */
659 /* Get the address of the target of this branch. */
660 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
662 dot
= (link_order
->offset
664 + link_order
->u
.indirect
.section
->output_section
->vma
);
669 if (gap
< -128 || gap
> 126)
671 if (! ((*link_info
->callbacks
->reloc_overflow
)
672 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
673 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
674 input_section
, reloc
->address
)))
678 /* Everything looks OK. Apply the relocation and update the
679 src/dst address appropriately. */
681 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
688 /* Generic 16bit pc-relative relocation. */
690 /* Get the address of the target of this branch. */
691 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
693 /* Get the address of the instruction (not the reloc). */
694 dot
= (link_order
->offset
696 + link_order
->u
.indirect
.section
->output_section
->vma
+ 1);
701 if (gap
> 32766 || gap
< -32768)
703 if (! ((*link_info
->callbacks
->reloc_overflow
)
704 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
705 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
706 input_section
, reloc
->address
)))
710 /* Everything looks OK. Apply the relocation and update the
711 src/dst address appropriately. */
713 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
720 /* Generic 8bit absolute relocation. */
722 /* Get the address of the object referenced by this insn. */
723 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
727 || (value
>= 0x0000ff00 && value
<= 0x0000ffff)
728 || (value
>= 0x00ffff00 && value
<= 0x00ffffff)
729 || (value
>= 0xffffff00 && value
<= 0xffffffff))
731 /* Everything looks OK. Apply the relocation and update the
732 src/dst address appropriately. */
734 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
);
740 if (! ((*link_info
->callbacks
->reloc_overflow
)
741 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
742 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
743 input_section
, reloc
->address
)))
750 /* Various simple 16bit absolute relocations. */
754 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
755 bfd_put_16 (abfd
, value
, data
+ dst_address
);
760 /* Various simple 24/32bit absolute relocations. */
764 /* Get the address of the target of this branch. */
765 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
766 bfd_put_32 (abfd
, value
, data
+ dst_address
);
771 /* Another 24/32bit absolute relocation. */
773 /* Get the address of the target of this branch. */
774 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
776 value
= ((value
& 0x00ffffff)
777 | (bfd_get_32 (abfd
, data
+ src_address
) & 0xff000000));
778 bfd_put_32 (abfd
, value
, data
+ dst_address
);
783 /* A 16bit abolute relocation that was formerlly a 24/32bit
784 absolute relocation. */
786 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
789 if (value
<= 0x7fff || value
>= 0xff8000)
791 /* Insert the 16bit value into the proper location. */
792 bfd_put_16 (abfd
, value
, data
+ dst_address
);
794 /* Fix the opcode. For all the move insns, we simply
795 need to turn off bit 0x20 in the previous byte. */
796 data
[dst_address
- 1] &= ~0x20;
802 if (! ((*link_info
->callbacks
->reloc_overflow
)
803 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
804 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
805 input_section
, reloc
->address
)))
810 /* A 16bit absolute branch that is now an 8-bit pc-relative branch. */
812 /* Get the address of the target of this branch. */
813 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
815 /* Get the address of the next instruction. */
816 dot
= (link_order
->offset
818 + link_order
->u
.indirect
.section
->output_section
->vma
+ 1);
823 if (gap
< -128 || gap
> 126)
825 if (! ((*link_info
->callbacks
->reloc_overflow
)
826 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
827 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
828 input_section
, reloc
->address
)))
832 /* Now fix the instruction itself. */
833 switch (data
[dst_address
- 1])
837 bfd_put_8 (abfd
, 0x55, data
+ dst_address
- 1);
841 bfd_put_8 (abfd
, 0x40, data
+ dst_address
- 1);
848 /* Write out the 8bit value. */
849 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
856 /* A 16bit pc-relative branch that is now an 8-bit pc-relative branch. */
858 /* Get the address of the target of this branch. */
859 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
861 /* Get the address of the instruction (not the reloc). */
862 dot
= (link_order
->offset
864 + link_order
->u
.indirect
.section
->output_section
->vma
- 1);
869 if (gap
< -128 || gap
> 126)
871 if (! ((*link_info
->callbacks
->reloc_overflow
)
872 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
873 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
874 input_section
, reloc
->address
)))
878 /* Now fix the instruction. */
879 switch (data
[dst_address
- 2])
882 /* bCC:16 -> bCC:8 */
883 /* Get the condition code from the original insn. */
884 tmp
= data
[dst_address
- 1];
888 /* Now or in the high nibble of the opcode. */
892 bfd_put_8 (abfd
, tmp
, data
+ dst_address
- 2);
896 /* bsr:16 -> bsr:8 */
897 bfd_put_8 (abfd
, 0x55, data
+ dst_address
- 2);
904 /* Output the target. */
905 bfd_put_8 (abfd
, gap
, data
+ dst_address
- 1);
907 /* We don't advance dst_address -- the 8bit reloc is applied at
908 dst_address - 1, so the next insn should begin at dst_address. */
913 /* Similarly for a 24bit absolute that is now 8 bits. */
915 /* Get the address of the target of this branch. */
916 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
918 /* Get the address of the instruction (not the reloc). */
919 dot
= (link_order
->offset
921 + link_order
->u
.indirect
.section
->output_section
->vma
+ 2);
925 /* Fix the instruction. */
926 switch (data
[src_address
])
930 bfd_put_8 (abfd
, 0x55, data
+ dst_address
);
934 bfd_put_8 (abfd
, 0x40, data
+ dst_address
);
940 bfd_put_8 (abfd
, gap
, data
+ dst_address
+ 1);
946 /* A 16bit absolute mov.b that is now an 8bit absolute mov.b. */
948 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
951 if (data
[dst_address
- 2] != 0x6a)
954 /* Fix up the opcode. */
955 switch (data
[src_address
- 1] & 0xf0)
958 data
[dst_address
- 2] = (data
[src_address
- 1] & 0xf) | 0x20;
961 data
[dst_address
- 2] = (data
[src_address
- 1] & 0xf) | 0x30;
967 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
- 1);
971 /* Similarly for a 24bit mov.b */
973 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
976 if (data
[dst_address
- 2] != 0x6a)
979 /* Fix up the opcode. */
980 switch (data
[src_address
- 1] & 0xf0)
983 data
[dst_address
- 2] = (data
[src_address
- 1] & 0xf) | 0x20;
986 data
[dst_address
- 2] = (data
[src_address
- 1] & 0xf) | 0x30;
992 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
- 1);
997 /* Get the address of the target of this branch. */
998 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
1000 dot
= (link_order
->offset
1002 + link_order
->u
.indirect
.section
->output_section
->vma
) + 1;
1007 if (gap
< -128 || gap
> 126)
1009 if (! ((*link_info
->callbacks
->reloc_overflow
)
1010 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
1011 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
1012 input_section
, reloc
->address
)))
1016 /* Everything looks OK. Fix the condition in the instruction, apply
1017 the relocation, and update the src/dst address appropriately. */
1019 bfd_put_8 (abfd
, bfd_get_8 (abfd
, data
+ dst_address
- 1) ^ 1,
1020 data
+ dst_address
- 1);
1021 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
1032 /* An 8bit memory indirect instruction (jmp/jsr).
1034 There's several things that need to be done to handle
1037 If this is a reloc against the absolute symbol, then
1038 we should handle it just R_RELBYTE. Likewise if it's
1039 for a symbol with a value ge 0 and le 0xff.
1041 Otherwise it's a jump/call through the function vector,
1042 and the linker is expected to set up the function vector
1043 and put the right value into the jump/call instruction. */
1044 case R_MEM_INDIRECT
:
1046 /* We need to find the symbol so we can determine it's
1047 address in the function vector table. */
1051 struct funcvec_hash_entry
*h
;
1052 asection
*vectors_sec
= h8300_coff_hash_table (link_info
)->vectors_sec
;
1054 /* First see if this is a reloc against the absolute symbol
1055 or against a symbol with a nonnegative value <= 0xff. */
1056 symbol
= *(reloc
->sym_ptr_ptr
);
1057 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
1058 if (symbol
== bfd_abs_section_ptr
->symbol
1061 /* This should be handled in a manner very similar to
1062 R_RELBYTES. If the value is in range, then just slam
1063 the value into the right location. Else trigger a
1064 reloc overflow callback. */
1067 bfd_put_8 (abfd
, value
, data
+ dst_address
);
1073 if (! ((*link_info
->callbacks
->reloc_overflow
)
1074 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
1075 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
1076 input_section
, reloc
->address
)))
1082 /* This is a jump/call through a function vector, and we're
1083 expected to create the function vector ourselves.
1085 First look up this symbol in the linker hash table -- we need
1086 the derived linker symbol which holds this symbol's index
1087 in the function vector. */
1088 name
= symbol
->name
;
1089 if (symbol
->flags
& BSF_LOCAL
)
1091 char *new_name
= bfd_malloc (strlen (name
) + 9);
1092 if (new_name
== NULL
)
1095 strcpy (new_name
, name
);
1096 sprintf (new_name
+ strlen (name
), "_%08x",
1097 (int) symbol
->section
);
1101 h
= funcvec_hash_lookup (h8300_coff_hash_table (link_info
)->funcvec_hash_table
,
1102 name
, false, false);
1104 /* This shouldn't ever happen. If it does that means we've got
1105 data corruption of some kind. Aborting seems like a reasonable
1106 think to do here. */
1107 if (h
== NULL
|| vectors_sec
== NULL
)
1110 /* Place the address of the function vector entry into the
1113 vectors_sec
->output_offset
+ h
->offset
,
1114 data
+ dst_address
);
1119 /* Now create an entry in the function vector itself. */
1120 if (bfd_get_mach (input_section
->owner
) == bfd_mach_h8300
)
1122 bfd_coff_reloc16_get_value (reloc
,
1125 vectors_sec
->contents
+ h
->offset
);
1126 else if (bfd_get_mach (input_section
->owner
) == bfd_mach_h8300h
1127 || bfd_get_mach (input_section
->owner
) == bfd_mach_h8300s
)
1129 bfd_coff_reloc16_get_value (reloc
,
1132 vectors_sec
->contents
+ h
->offset
);
1136 /* Gross. We've already written the contents of the vector section
1137 before we get here... So we write it again with the new data. */
1138 bfd_set_section_contents (vectors_sec
->output_section
->owner
,
1139 vectors_sec
->output_section
,
1140 vectors_sec
->contents
,
1141 vectors_sec
->output_offset
,
1142 vectors_sec
->_raw_size
);
1152 *src_ptr
= src_address
;
1153 *dst_ptr
= dst_address
;
1156 /* Routine for the h8300 linker.
1158 This routine is necessary to handle the special R_MEM_INDIRECT
1159 relocs on the h8300. It's responsible for generating a vectors
1160 section and attaching it to an input bfd as well as sizing
1161 the vectors section. It also creates our vectors hash table.
1163 It uses the generic linker routines to actually add the symbols.
1164 from this BFD to the bfd linker hash table. It may add a few
1165 selected static symbols to the bfd linker hash table. */
1168 h8300_bfd_link_add_symbols (abfd
, info
)
1170 struct bfd_link_info
*info
;
1173 struct funcvec_hash_table
*funcvec_hash_table
;
1175 /* If we haven't created a vectors section, do so now. */
1176 if (!h8300_coff_hash_table (info
)->vectors_sec
)
1180 /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */
1181 flags
= (SEC_ALLOC
| SEC_LOAD
1182 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_READONLY
);
1183 h8300_coff_hash_table (info
)->vectors_sec
= bfd_make_section (abfd
,
1186 /* If the section wasn't created, or we couldn't set the flags,
1187 quit quickly now, rather than dieing a painful death later. */
1188 if (! h8300_coff_hash_table (info
)->vectors_sec
1189 || ! bfd_set_section_flags (abfd
,
1190 h8300_coff_hash_table(info
)->vectors_sec
,
1194 /* Also create the vector hash table. */
1195 funcvec_hash_table
= ((struct funcvec_hash_table
*)
1196 bfd_alloc (abfd
, sizeof (struct funcvec_hash_table
)));
1198 if (!funcvec_hash_table
)
1201 /* And initialize the funcvec hash table. */
1202 if (!funcvec_hash_table_init (funcvec_hash_table
, abfd
,
1203 funcvec_hash_newfunc
))
1205 bfd_release (abfd
, funcvec_hash_table
);
1209 /* Store away a pointer to the funcvec hash table. */
1210 h8300_coff_hash_table (info
)->funcvec_hash_table
= funcvec_hash_table
;
1213 /* Load up the function vector hash table. */
1214 funcvec_hash_table
= h8300_coff_hash_table (info
)->funcvec_hash_table
;
1216 /* Add the symbols using the generic code. */
1217 _bfd_generic_link_add_symbols (abfd
, info
);
1219 /* Now scan the relocs for all the sections in this bfd; create
1220 additional space in the .vectors section as needed. */
1221 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1223 long reloc_size
, reloc_count
, i
;
1227 /* Suck in the relocs, symbols & canonicalize them. */
1228 reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
1229 if (reloc_size
<= 0)
1232 relocs
= (arelent
**) bfd_malloc ((size_t) reloc_size
);
1236 /* The symbols should have been read in by _bfd_generic link_add_symbols
1237 call abovec, so we can cheat and use the pointer to them that was
1238 saved in the above call. */
1239 symbols
= _bfd_generic_link_get_symbols(abfd
);
1240 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, symbols
);
1241 if (reloc_count
<= 0)
1247 /* Now walk through all the relocations in this section. */
1248 for (i
= 0; i
< reloc_count
; i
++)
1250 arelent
*reloc
= relocs
[i
];
1251 asymbol
*symbol
= *(reloc
->sym_ptr_ptr
);
1254 /* We've got an indirect reloc. See if we need to add it
1255 to the function vector table. At this point, we have
1256 to add a new entry for each unique symbol referenced
1257 by an R_MEM_INDIRECT relocation except for a reloc
1258 against the absolute section symbol. */
1259 if (reloc
->howto
->type
== R_MEM_INDIRECT
1260 && symbol
!= bfd_abs_section_ptr
->symbol
)
1263 struct funcvec_hash_entry
*h
;
1265 name
= symbol
->name
;
1266 if (symbol
->flags
& BSF_LOCAL
)
1268 char *new_name
= bfd_malloc (strlen (name
) + 9);
1270 if (new_name
== NULL
)
1273 strcpy (new_name
, name
);
1274 sprintf (new_name
+ strlen (name
), "_%08x",
1275 (int) symbol
->section
);
1279 /* Look this symbol up in the function vector hash table. */
1280 h
= funcvec_hash_lookup (h8300_coff_hash_table (info
)->funcvec_hash_table
,
1281 name
, false, false);
1283 /* If this symbol isn't already in the hash table, add
1284 it and bump up the size of the hash table. */
1287 h
= funcvec_hash_lookup (h8300_coff_hash_table (info
)->funcvec_hash_table
,
1295 /* Bump the size of the vectors section. Each vector
1296 takes 2 bytes on the h8300 and 4 bytes on the h8300h. */
1297 if (bfd_get_mach (abfd
) == bfd_mach_h8300
)
1298 h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
+= 2;
1299 else if (bfd_get_mach (abfd
) == bfd_mach_h8300h
1300 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
1301 h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
+= 4;
1306 /* We're done with the relocations, release them. */
1310 /* Now actually allocate some space for the function vector. It's
1311 wasteful to do this more than once, but this is easier. */
1312 if (h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
!= 0)
1314 /* Free the old contents. */
1315 if (h8300_coff_hash_table (info
)->vectors_sec
->contents
)
1316 free (h8300_coff_hash_table (info
)->vectors_sec
->contents
);
1318 /* Allocate new contents. */
1319 h8300_coff_hash_table (info
)->vectors_sec
->contents
1320 = bfd_malloc (h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
);
1326 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1327 #define coff_reloc16_estimate h8300_reloc16_estimate
1328 #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1329 #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
1331 #define COFF_LONG_FILENAMES
1332 #include "coffcode.h"
1334 #undef coff_bfd_get_relocated_section_contents
1335 #undef coff_bfd_relax_section
1336 #define coff_bfd_get_relocated_section_contents \
1337 bfd_coff_reloc16_get_relocated_section_contents
1338 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1340 CREATE_BIG_COFF_TARGET_VEC (h8300coff_vec
, "coff-h8300", BFD_IS_RELAXABLE
, 0, '_', NULL
)