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 static bfd_reloc_status_type special
PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
73 static int select_reloc
PARAMS ((reloc_howto_type
*));
74 static void rtype2howto
PARAMS ((arelent
*, struct internal_reloc
*));
75 static void reloc_processing
PARAMS ((arelent
*, struct internal_reloc
*, asymbol
**, bfd
*, asection
*));
76 static boolean h8300_symbol_address_p
PARAMS ((bfd
*, asection
*, bfd_vma
));
77 static int h8300_reloc16_estimate
PARAMS ((bfd
*, asection
*, arelent
*, unsigned int, struct bfd_link_info
*));
78 static void h8300_reloc16_extra_cases
PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*, arelent
*, bfd_byte
*, unsigned int *, unsigned int *));
79 static boolean h8300_bfd_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
81 /* To lookup a value in the function vector hash table. */
82 #define funcvec_hash_lookup(table, string, create, copy) \
83 ((struct funcvec_hash_entry *) \
84 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
86 /* The derived h8300 COFF linker table. Note it's derived from
87 the generic linker hash table, not the COFF backend linker hash
88 table! We use this to attach additional data structures we
89 need while linking on the h8300. */
90 struct h8300_coff_link_hash_table
92 /* The main hash table. */
93 struct generic_link_hash_table root
;
95 /* Section for the vectors table. This gets attached to a
96 random input bfd, we keep it here for easy access. */
97 asection
*vectors_sec
;
99 /* Hash table of the functions we need to enter into the function
101 struct funcvec_hash_table
*funcvec_hash_table
;
104 static struct bfd_link_hash_table
*h8300_coff_link_hash_table_create
107 /* Get the H8/300 COFF linker hash table from a link_info structure. */
109 #define h8300_coff_hash_table(p) \
110 ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p))))
112 /* Initialize fields within a funcvec hash table entry. Called whenever
113 a new entry is added to the funcvec hash table. */
115 static struct bfd_hash_entry
*
116 funcvec_hash_newfunc (entry
, gen_table
, string
)
117 struct bfd_hash_entry
*entry
;
118 struct bfd_hash_table
*gen_table
;
121 struct funcvec_hash_entry
*ret
;
122 struct funcvec_hash_table
*table
;
124 ret
= (struct funcvec_hash_entry
*) entry
;
125 table
= (struct funcvec_hash_table
*) gen_table
;
127 /* Allocate the structure if it has not already been allocated by a
130 ret
= ((struct funcvec_hash_entry
*)
131 bfd_hash_allocate (gen_table
,
132 sizeof (struct funcvec_hash_entry
)));
136 /* Call the allocation method of the superclass. */
137 ret
= ((struct funcvec_hash_entry
*)
138 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, gen_table
, string
));
143 /* Note where this entry will reside in the function vector table. */
144 ret
->offset
= table
->offset
;
146 /* Bump the offset at which we store entries in the function
147 vector. We'd like to bump up the size of the vectors section,
148 but it's not easily available here. */
149 if (bfd_get_mach (table
->abfd
) == bfd_mach_h8300
)
151 else if (bfd_get_mach (table
->abfd
) == bfd_mach_h8300h
152 || bfd_get_mach (table
->abfd
) == bfd_mach_h8300s
)
157 /* Everything went OK. */
158 return (struct bfd_hash_entry
*) ret
;
161 /* Initialize the function vector hash table. */
164 funcvec_hash_table_init (table
, abfd
, newfunc
)
165 struct funcvec_hash_table
*table
;
167 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
168 struct bfd_hash_table
*,
171 /* Initialize our local fields, then call the generic initialization
175 return (bfd_hash_table_init (&table
->root
, newfunc
));
178 /* Create the derived linker hash table. We use a derived hash table
179 basically to hold "static" information during an h8/300 coff link
180 without using static variables. */
182 static struct bfd_link_hash_table
*
183 h8300_coff_link_hash_table_create (abfd
)
186 struct h8300_coff_link_hash_table
*ret
;
187 ret
= ((struct h8300_coff_link_hash_table
*)
188 bfd_alloc (abfd
, sizeof (struct h8300_coff_link_hash_table
)));
191 if (!_bfd_link_hash_table_init (&ret
->root
.root
, abfd
, _bfd_generic_link_hash_newfunc
))
193 bfd_release (abfd
, ret
);
197 /* Initialize our data. */
198 ret
->vectors_sec
= NULL
;
199 ret
->funcvec_hash_table
= NULL
;
201 /* OK. Everything's intialized, return the base pointer. */
202 return &ret
->root
.root
;
205 /* Special handling for H8/300 relocs.
206 We only come here for pcrel stuff and return normally if not an -r link.
207 When doing -r, we can't do any arithmetic for the pcrel stuff, because
208 the code in reloc.c assumes that we can manipulate the targets of
209 the pcrel branches. This isn't so, since the H8/300 can do relaxing,
210 which means that the gap after the instruction may not be enough to
211 contain the offset required for the branch, so we have to use only
212 the addend until the final link. */
214 static bfd_reloc_status_type
215 special (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
217 bfd
*abfd ATTRIBUTE_UNUSED
;
218 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
219 asymbol
*symbol ATTRIBUTE_UNUSED
;
220 PTR data ATTRIBUTE_UNUSED
;
221 asection
*input_section ATTRIBUTE_UNUSED
;
223 char **error_message ATTRIBUTE_UNUSED
;
225 if (output_bfd
== (bfd
*) NULL
)
226 return bfd_reloc_continue
;
228 /* Adjust the reloc address to that in the output section. */
229 reloc_entry
->address
+= input_section
->output_offset
;
233 static reloc_howto_type howto_table
[] =
235 HOWTO (R_RELBYTE
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "8", false, 0x000000ff, 0x000000ff, false),
236 HOWTO (R_RELWORD
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "16", false, 0x0000ffff, 0x0000ffff, false),
237 HOWTO (R_RELLONG
, 0, 2, 32, false, 0, complain_overflow_bitfield
, special
, "32", false, 0xffffffff, 0xffffffff, false),
238 HOWTO (R_PCRBYTE
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "DISP8", false, 0x000000ff, 0x000000ff, true),
239 HOWTO (R_PCRWORD
, 0, 1, 16, true, 0, complain_overflow_signed
, special
, "DISP16", false, 0x0000ffff, 0x0000ffff, true),
240 HOWTO (R_PCRLONG
, 0, 2, 32, true, 0, complain_overflow_signed
, special
, "DISP32", false, 0xffffffff, 0xffffffff, true),
241 HOWTO (R_MOV16B1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "relaxable mov.b:16", false, 0x0000ffff, 0x0000ffff, false),
242 HOWTO (R_MOV16B2
, 0, 1, 8, false, 0, complain_overflow_bitfield
, special
, "relaxed mov.b:16", false, 0x000000ff, 0x000000ff, false),
243 HOWTO (R_JMP1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "16/pcrel", false, 0x0000ffff, 0x0000ffff, false),
244 HOWTO (R_JMP2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "pcrecl/16", false, 0x000000ff, 0x000000ff, false),
245 HOWTO (R_JMPL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
, special
, "24/pcrell", false, 0x00ffffff, 0x00ffffff, false),
246 HOWTO (R_JMPL2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "pc8/24", false, 0x000000ff, 0x000000ff, false),
247 HOWTO (R_MOV24B1
, 0, 1, 32, false, 0, complain_overflow_bitfield
, special
, "relaxable mov.b:24", false, 0xffffffff, 0xffffffff, false),
248 HOWTO (R_MOV24B2
, 0, 1, 8, false, 0, complain_overflow_bitfield
, special
, "relaxed mov.b:24", false, 0x0000ffff, 0x0000ffff, false),
250 /* An indirect reference to a function. This causes the function's address
251 to be added to the function vector in lo-mem and puts the address of
252 the function vector's entry in the jsr instruction. */
253 HOWTO (R_MEM_INDIRECT
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "8/indirect", false, 0x000000ff, 0x000000ff, false),
255 /* Internal reloc for relaxing. This is created when a 16bit pc-relative
256 branch is turned into an 8bit pc-relative branch. */
257 HOWTO (R_PCRWORD_B
, 0, 0, 8, true, 0, complain_overflow_bitfield
, special
, "relaxed bCC:16", false, 0x000000ff, 0x000000ff, false),
259 HOWTO (R_MOVL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
,special
, "32/24 relaxable move", false, 0xffffffff, 0xffffffff, false),
261 HOWTO (R_MOVL2
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "32/24 relaxed move", false, 0x0000ffff, 0x0000ffff, false),
263 HOWTO (R_BCC_INV
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "DISP8 inverted", false, 0x000000ff, 0x000000ff, true),
265 HOWTO (R_JMP_DEL
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "Deleted jump", false, 0x000000ff, 0x000000ff, true),
268 /* Turn a howto into a reloc number. */
270 #define SELECT_RELOC(x,howto) \
271 { x.r_type = select_reloc(howto); }
273 #define BADMAG(x) (H8300BADMAG(x) && H8300HBADMAG(x) && H8300SBADMAG(x))
274 #define H8300 1 /* Customize coffcode.h */
275 #define __A_MAGIC_SET__
277 /* Code to swap in the reloc. */
278 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
279 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
280 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
281 dst->r_stuff[0] = 'S'; \
282 dst->r_stuff[1] = 'C';
286 reloc_howto_type
*howto
;
291 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
294 rtype2howto (internal
, dst
)
296 struct internal_reloc
*dst
;
301 internal
->howto
= howto_table
+ 0;
304 internal
->howto
= howto_table
+ 1;
307 internal
->howto
= howto_table
+ 2;
310 internal
->howto
= howto_table
+ 3;
313 internal
->howto
= howto_table
+ 4;
316 internal
->howto
= howto_table
+ 5;
319 internal
->howto
= howto_table
+ 6;
322 internal
->howto
= howto_table
+ 7;
325 internal
->howto
= howto_table
+ 8;
328 internal
->howto
= howto_table
+ 9;
331 internal
->howto
= howto_table
+ 10;
334 internal
->howto
= howto_table
+ 11;
337 internal
->howto
= howto_table
+ 12;
340 internal
->howto
= howto_table
+ 13;
343 internal
->howto
= howto_table
+ 14;
346 internal
->howto
= howto_table
+ 15;
349 internal
->howto
= howto_table
+ 16;
352 internal
->howto
= howto_table
+ 17;
355 internal
->howto
= howto_table
+ 18;
358 internal
->howto
= howto_table
+ 19;
366 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
368 /* Perform any necessary magic to the addend in a reloc entry. */
370 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
371 cache_ptr->addend = ext_reloc.r_offset;
373 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
374 reloc_processing(relent, reloc, symbols, abfd, section)
377 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
379 struct internal_reloc
*reloc
;
384 relent
->address
= reloc
->r_vaddr
;
385 rtype2howto (relent
, reloc
);
387 if (((int) reloc
->r_symndx
) > 0)
389 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
393 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
396 relent
->addend
= reloc
->r_offset
;
398 relent
->address
-= section
->vma
;
405 h8300_symbol_address_p (abfd
, input_section
, address
)
407 asection
*input_section
;
412 s
= _bfd_generic_link_get_symbols (abfd
);
413 BFD_ASSERT (s
!= (asymbol
**) NULL
);
415 /* Search all the symbols for one in INPUT_SECTION with
420 if (p
->section
== input_section
421 && (input_section
->output_section
->vma
422 + input_section
->output_offset
423 + p
->value
) == address
)
430 /* If RELOC represents a relaxable instruction/reloc, change it into
431 the relaxed reloc, notify the linker that symbol addresses
432 have changed (bfd_perform_slip) and return how much the current
433 section has shrunk by.
435 FIXME: Much of this code has knowledge of the ordering of entries
436 in the howto table. This needs to be fixed. */
439 h8300_reloc16_estimate (abfd
, input_section
, reloc
, shrink
, link_info
)
441 asection
*input_section
;
444 struct bfd_link_info
*link_info
;
449 static asection
*last_input_section
= NULL
;
450 static arelent
*last_reloc
= NULL
;
452 /* The address of the thing to be relocated will have moved back by
453 the size of the shrink - but we don't change reloc->address here,
454 since we need it to know where the relocation lives in the source
456 bfd_vma address
= reloc
->address
- shrink
;
458 if (input_section
!= last_input_section
)
461 /* Only examine the relocs which might be relaxable. */
462 switch (reloc
->howto
->type
)
464 /* This is the 16/24 bit absolute branch which could become an 8 bit
465 pc-relative branch. */
468 /* Get the address of the target of this branch. */
469 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
471 /* Get the address of the next instruction (not the reloc). */
472 dot
= (input_section
->output_section
->vma
473 + input_section
->output_offset
+ address
);
475 /* Adjust for R_JMP1 vs R_JMPL1. */
476 dot
+= (reloc
->howto
->type
== R_JMP1
? 1 : 2);
478 /* Compute the distance from this insn to the branch target. */
481 /* If the distance is within -128..+128 inclusive, then we can relax
482 this jump. +128 is valid since the target will move two bytes
483 closer if we do relax this branch. */
484 if ((int)gap
>= -128 && (int)gap
<= 128 )
486 /* It's possible we may be able to eliminate this branch entirely;
487 if the previous instruction is a branch around this instruction,
488 and there's no label at this instruction, then we can reverse
489 the condition on the previous branch and eliminate this jump.
496 This saves 4 bytes instead of two, and should be relatively
501 && last_reloc
->howto
->type
== R_PCRBYTE
)
504 last_value
= bfd_coff_reloc16_get_value (last_reloc
, link_info
,
507 if (last_value
== dot
+ 2
508 && last_reloc
->address
+ 1 == reloc
->address
509 && !h8300_symbol_address_p (abfd
, input_section
, dot
- 2))
511 reloc
->howto
= howto_table
+ 19;
512 last_reloc
->howto
= howto_table
+ 18;
513 last_reloc
->sym_ptr_ptr
= reloc
->sym_ptr_ptr
;
514 last_reloc
->addend
= reloc
->addend
;
516 bfd_perform_slip (abfd
, 4, input_section
, address
);
521 /* Change the reloc type. */
522 reloc
->howto
= reloc
->howto
+ 1;
524 /* This shrinks this section by two bytes. */
526 bfd_perform_slip (abfd
, 2, input_section
, address
);
530 /* This is the 16 bit pc-relative branch which could become an 8 bit
531 pc-relative branch. */
533 /* Get the address of the target of this branch, add one to the value
534 because the addend field in PCrel jumps is off by -1. */
535 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
) + 1;
537 /* Get the address of the next instruction if we were to relax. */
538 dot
= input_section
->output_section
->vma
+
539 input_section
->output_offset
+ address
;
541 /* Compute the distance from this insn to the branch target. */
544 /* If the distance is within -128..+128 inclusive, then we can relax
545 this jump. +128 is valid since the target will move two bytes
546 closer if we do relax this branch. */
547 if ((int)gap
>= -128 && (int)gap
<= 128 )
549 /* Change the reloc type. */
550 reloc
->howto
= howto_table
+ 15;
552 /* This shrinks this section by two bytes. */
554 bfd_perform_slip (abfd
, 2, input_section
, address
);
558 /* This is a 16 bit absolute address in a mov.b insn, which can
559 become an 8 bit absolute address if it's in the right range. */
561 /* Get the address of the data referenced by this mov.b insn. */
562 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
564 /* The address is in 0xff00..0xffff inclusive on the h8300 or
565 0xffff00..0xffffff inclusive on the h8300h, then we can
567 if ((bfd_get_mach (abfd
) == bfd_mach_h8300
570 || ((bfd_get_mach (abfd
) == bfd_mach_h8300h
571 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
573 && value
<= 0xffffff))
575 /* Change the reloc type. */
576 reloc
->howto
= reloc
->howto
+ 1;
578 /* This shrinks this section by two bytes. */
580 bfd_perform_slip (abfd
, 2, input_section
, address
);
584 /* Similarly for a 24 bit absolute address in a mov.b. Note that
585 if we can't relax this into an 8 bit absolute, we'll fall through
586 and try to relax it into a 16bit absolute. */
588 /* Get the address of the data referenced by this mov.b insn. */
589 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
591 /* The address is in 0xffff00..0xffffff inclusive on the h8300h,
592 then we can relax this mov.b */
593 if ((bfd_get_mach (abfd
) == bfd_mach_h8300h
594 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
596 && value
<= 0xffffff)
598 /* Change the reloc type. */
599 reloc
->howto
= reloc
->howto
+ 1;
601 /* This shrinks this section by four bytes. */
603 bfd_perform_slip (abfd
, 4, input_section
, address
);
605 /* Done with this reloc. */
609 /* FALLTHROUGH and try to turn the 32/24 bit reloc into a 16 bit
612 /* This is a 24/32 bit absolute address in a mov insn, which can
613 become an 16 bit absolute address if it's in the right range. */
615 /* Get the address of the data referenced by this mov insn. */
616 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
618 /* If this address is in 0x0000..0x7fff inclusive or
619 0xff8000..0xffffff inclusive, then it can be relaxed. */
620 if (value
<= 0x7fff || value
>= 0xff8000)
622 /* Change the reloc type. */
623 reloc
->howto
= howto_table
+ 17;
625 /* This shrinks this section by two bytes. */
627 bfd_perform_slip (abfd
, 2, input_section
, address
);
631 /* No other reloc types represent relaxing opportunities. */
637 last_input_section
= input_section
;
641 /* Handle relocations for the H8/300, including relocs for relaxed
644 FIXME: Not all relocations check for overflow! */
647 h8300_reloc16_extra_cases (abfd
, link_info
, link_order
, reloc
, data
, src_ptr
,
650 struct bfd_link_info
*link_info
;
651 struct bfd_link_order
*link_order
;
654 unsigned int *src_ptr
;
655 unsigned int *dst_ptr
;
657 unsigned int src_address
= *src_ptr
;
658 unsigned int dst_address
= *dst_ptr
;
659 asection
*input_section
= link_order
->u
.indirect
.section
;
664 switch (reloc
->howto
->type
)
666 /* Generic 8bit pc-relative relocation. */
668 /* Get the address of the target of this branch. */
669 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
671 dot
= (link_order
->offset
673 + link_order
->u
.indirect
.section
->output_section
->vma
);
678 if (gap
< -128 || gap
> 126)
680 if (! ((*link_info
->callbacks
->reloc_overflow
)
681 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
682 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
683 input_section
, reloc
->address
)))
687 /* Everything looks OK. Apply the relocation and update the
688 src/dst address appropriately. */
690 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
697 /* Generic 16bit pc-relative relocation. */
699 /* Get the address of the target of this branch. */
700 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
702 /* Get the address of the instruction (not the reloc). */
703 dot
= (link_order
->offset
705 + link_order
->u
.indirect
.section
->output_section
->vma
+ 1);
710 if (gap
> 32766 || gap
< -32768)
712 if (! ((*link_info
->callbacks
->reloc_overflow
)
713 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
714 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
715 input_section
, reloc
->address
)))
719 /* Everything looks OK. Apply the relocation and update the
720 src/dst address appropriately. */
722 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
729 /* Generic 8bit absolute relocation. */
731 /* Get the address of the object referenced by this insn. */
732 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
736 || (value
>= 0x0000ff00 && value
<= 0x0000ffff)
737 || (value
>= 0x00ffff00 && value
<= 0x00ffffff)
738 || (value
>= 0xffffff00 && value
<= 0xffffffff))
740 /* Everything looks OK. Apply the relocation and update the
741 src/dst address appropriately. */
743 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
);
749 if (! ((*link_info
->callbacks
->reloc_overflow
)
750 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
751 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
752 input_section
, reloc
->address
)))
759 /* Various simple 16bit absolute relocations. */
763 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
764 bfd_put_16 (abfd
, value
, data
+ dst_address
);
769 /* Various simple 24/32bit absolute relocations. */
773 /* Get the address of the target of this branch. */
774 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
775 bfd_put_32 (abfd
, value
, data
+ dst_address
);
780 /* Another 24/32bit absolute relocation. */
782 /* Get the address of the target of this branch. */
783 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
785 value
= ((value
& 0x00ffffff)
786 | (bfd_get_32 (abfd
, data
+ src_address
) & 0xff000000));
787 bfd_put_32 (abfd
, value
, data
+ dst_address
);
792 /* A 16bit abolute relocation that was formerlly a 24/32bit
793 absolute relocation. */
795 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
798 if (value
<= 0x7fff || value
>= 0xff8000)
800 /* Insert the 16bit value into the proper location. */
801 bfd_put_16 (abfd
, value
, data
+ dst_address
);
803 /* Fix the opcode. For all the move insns, we simply
804 need to turn off bit 0x20 in the previous byte. */
805 data
[dst_address
- 1] &= ~0x20;
811 if (! ((*link_info
->callbacks
->reloc_overflow
)
812 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
813 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
814 input_section
, reloc
->address
)))
819 /* A 16bit absolute branch that is now an 8-bit pc-relative branch. */
821 /* Get the address of the target of this branch. */
822 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
824 /* Get the address of the next instruction. */
825 dot
= (link_order
->offset
827 + link_order
->u
.indirect
.section
->output_section
->vma
+ 1);
832 if (gap
< -128 || gap
> 126)
834 if (! ((*link_info
->callbacks
->reloc_overflow
)
835 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
836 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
837 input_section
, reloc
->address
)))
841 /* Now fix the instruction itself. */
842 switch (data
[dst_address
- 1])
846 bfd_put_8 (abfd
, 0x55, data
+ dst_address
- 1);
850 bfd_put_8 (abfd
, 0x40, data
+ dst_address
- 1);
857 /* Write out the 8bit value. */
858 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
865 /* A 16bit pc-relative branch that is now an 8-bit pc-relative branch. */
867 /* Get the address of the target of this branch. */
868 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
870 /* Get the address of the instruction (not the reloc). */
871 dot
= (link_order
->offset
873 + link_order
->u
.indirect
.section
->output_section
->vma
- 1);
878 if (gap
< -128 || gap
> 126)
880 if (! ((*link_info
->callbacks
->reloc_overflow
)
881 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
882 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
883 input_section
, reloc
->address
)))
887 /* Now fix the instruction. */
888 switch (data
[dst_address
- 2])
891 /* bCC:16 -> bCC:8 */
892 /* Get the condition code from the original insn. */
893 tmp
= data
[dst_address
- 1];
897 /* Now or in the high nibble of the opcode. */
901 bfd_put_8 (abfd
, tmp
, data
+ dst_address
- 2);
905 /* bsr:16 -> bsr:8 */
906 bfd_put_8 (abfd
, 0x55, data
+ dst_address
- 2);
913 /* Output the target. */
914 bfd_put_8 (abfd
, gap
, data
+ dst_address
- 1);
916 /* We don't advance dst_address -- the 8bit reloc is applied at
917 dst_address - 1, so the next insn should begin at dst_address. */
922 /* Similarly for a 24bit absolute that is now 8 bits. */
924 /* Get the address of the target of this branch. */
925 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
927 /* Get the address of the instruction (not the reloc). */
928 dot
= (link_order
->offset
930 + link_order
->u
.indirect
.section
->output_section
->vma
+ 2);
934 /* Fix the instruction. */
935 switch (data
[src_address
])
939 bfd_put_8 (abfd
, 0x55, data
+ dst_address
);
943 bfd_put_8 (abfd
, 0x40, data
+ dst_address
);
949 bfd_put_8 (abfd
, gap
, data
+ dst_address
+ 1);
955 /* A 16bit absolute mov.b that is now an 8bit absolute mov.b. */
957 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
960 if (data
[dst_address
- 2] != 0x6a)
963 /* Fix up the opcode. */
964 switch (data
[src_address
- 1] & 0xf0)
967 data
[dst_address
- 2] = (data
[src_address
- 1] & 0xf) | 0x20;
970 data
[dst_address
- 2] = (data
[src_address
- 1] & 0xf) | 0x30;
976 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
- 1);
980 /* Similarly for a 24bit mov.b */
982 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
985 if (data
[dst_address
- 2] != 0x6a)
988 /* Fix up the opcode. */
989 switch (data
[src_address
- 1] & 0xf0)
992 data
[dst_address
- 2] = (data
[src_address
- 1] & 0xf) | 0x20;
995 data
[dst_address
- 2] = (data
[src_address
- 1] & 0xf) | 0x30;
1001 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
- 1);
1006 /* Get the address of the target of this branch. */
1007 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
1009 dot
= (link_order
->offset
1011 + link_order
->u
.indirect
.section
->output_section
->vma
) + 1;
1016 if (gap
< -128 || gap
> 126)
1018 if (! ((*link_info
->callbacks
->reloc_overflow
)
1019 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
1020 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
1021 input_section
, reloc
->address
)))
1025 /* Everything looks OK. Fix the condition in the instruction, apply
1026 the relocation, and update the src/dst address appropriately. */
1028 bfd_put_8 (abfd
, bfd_get_8 (abfd
, data
+ dst_address
- 1) ^ 1,
1029 data
+ dst_address
- 1);
1030 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
1041 /* An 8bit memory indirect instruction (jmp/jsr).
1043 There's several things that need to be done to handle
1046 If this is a reloc against the absolute symbol, then
1047 we should handle it just R_RELBYTE. Likewise if it's
1048 for a symbol with a value ge 0 and le 0xff.
1050 Otherwise it's a jump/call through the function vector,
1051 and the linker is expected to set up the function vector
1052 and put the right value into the jump/call instruction. */
1053 case R_MEM_INDIRECT
:
1055 /* We need to find the symbol so we can determine it's
1056 address in the function vector table. */
1060 struct funcvec_hash_entry
*h
;
1061 asection
*vectors_sec
= h8300_coff_hash_table (link_info
)->vectors_sec
;
1063 /* First see if this is a reloc against the absolute symbol
1064 or against a symbol with a nonnegative value <= 0xff. */
1065 symbol
= *(reloc
->sym_ptr_ptr
);
1066 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
1067 if (symbol
== bfd_abs_section_ptr
->symbol
1070 /* This should be handled in a manner very similar to
1071 R_RELBYTES. If the value is in range, then just slam
1072 the value into the right location. Else trigger a
1073 reloc overflow callback. */
1076 bfd_put_8 (abfd
, value
, data
+ dst_address
);
1082 if (! ((*link_info
->callbacks
->reloc_overflow
)
1083 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
1084 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
1085 input_section
, reloc
->address
)))
1091 /* This is a jump/call through a function vector, and we're
1092 expected to create the function vector ourselves.
1094 First look up this symbol in the linker hash table -- we need
1095 the derived linker symbol which holds this symbol's index
1096 in the function vector. */
1097 name
= symbol
->name
;
1098 if (symbol
->flags
& BSF_LOCAL
)
1100 char *new_name
= bfd_malloc (strlen (name
) + 9);
1101 if (new_name
== NULL
)
1104 strcpy (new_name
, name
);
1105 sprintf (new_name
+ strlen (name
), "_%08x",
1106 (int) symbol
->section
);
1110 h
= funcvec_hash_lookup (h8300_coff_hash_table (link_info
)->funcvec_hash_table
,
1111 name
, false, false);
1113 /* This shouldn't ever happen. If it does that means we've got
1114 data corruption of some kind. Aborting seems like a reasonable
1115 think to do here. */
1116 if (h
== NULL
|| vectors_sec
== NULL
)
1119 /* Place the address of the function vector entry into the
1122 vectors_sec
->output_offset
+ h
->offset
,
1123 data
+ dst_address
);
1128 /* Now create an entry in the function vector itself. */
1129 if (bfd_get_mach (input_section
->owner
) == bfd_mach_h8300
)
1131 bfd_coff_reloc16_get_value (reloc
,
1134 vectors_sec
->contents
+ h
->offset
);
1135 else if (bfd_get_mach (input_section
->owner
) == bfd_mach_h8300h
1136 || bfd_get_mach (input_section
->owner
) == bfd_mach_h8300s
)
1138 bfd_coff_reloc16_get_value (reloc
,
1141 vectors_sec
->contents
+ h
->offset
);
1145 /* Gross. We've already written the contents of the vector section
1146 before we get here... So we write it again with the new data. */
1147 bfd_set_section_contents (vectors_sec
->output_section
->owner
,
1148 vectors_sec
->output_section
,
1149 vectors_sec
->contents
,
1150 vectors_sec
->output_offset
,
1151 vectors_sec
->_raw_size
);
1161 *src_ptr
= src_address
;
1162 *dst_ptr
= dst_address
;
1165 /* Routine for the h8300 linker.
1167 This routine is necessary to handle the special R_MEM_INDIRECT
1168 relocs on the h8300. It's responsible for generating a vectors
1169 section and attaching it to an input bfd as well as sizing
1170 the vectors section. It also creates our vectors hash table.
1172 It uses the generic linker routines to actually add the symbols.
1173 from this BFD to the bfd linker hash table. It may add a few
1174 selected static symbols to the bfd linker hash table. */
1177 h8300_bfd_link_add_symbols (abfd
, info
)
1179 struct bfd_link_info
*info
;
1182 struct funcvec_hash_table
*funcvec_hash_table
;
1184 /* If we haven't created a vectors section, do so now. */
1185 if (!h8300_coff_hash_table (info
)->vectors_sec
)
1189 /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */
1190 flags
= (SEC_ALLOC
| SEC_LOAD
1191 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_READONLY
);
1192 h8300_coff_hash_table (info
)->vectors_sec
= bfd_make_section (abfd
,
1195 /* If the section wasn't created, or we couldn't set the flags,
1196 quit quickly now, rather than dieing a painful death later. */
1197 if (! h8300_coff_hash_table (info
)->vectors_sec
1198 || ! bfd_set_section_flags (abfd
,
1199 h8300_coff_hash_table(info
)->vectors_sec
,
1203 /* Also create the vector hash table. */
1204 funcvec_hash_table
= ((struct funcvec_hash_table
*)
1205 bfd_alloc (abfd
, sizeof (struct funcvec_hash_table
)));
1207 if (!funcvec_hash_table
)
1210 /* And initialize the funcvec hash table. */
1211 if (!funcvec_hash_table_init (funcvec_hash_table
, abfd
,
1212 funcvec_hash_newfunc
))
1214 bfd_release (abfd
, funcvec_hash_table
);
1218 /* Store away a pointer to the funcvec hash table. */
1219 h8300_coff_hash_table (info
)->funcvec_hash_table
= funcvec_hash_table
;
1222 /* Load up the function vector hash table. */
1223 funcvec_hash_table
= h8300_coff_hash_table (info
)->funcvec_hash_table
;
1225 /* Add the symbols using the generic code. */
1226 _bfd_generic_link_add_symbols (abfd
, info
);
1228 /* Now scan the relocs for all the sections in this bfd; create
1229 additional space in the .vectors section as needed. */
1230 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1232 long reloc_size
, reloc_count
, i
;
1236 /* Suck in the relocs, symbols & canonicalize them. */
1237 reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
1238 if (reloc_size
<= 0)
1241 relocs
= (arelent
**) bfd_malloc ((size_t) reloc_size
);
1245 /* The symbols should have been read in by _bfd_generic link_add_symbols
1246 call abovec, so we can cheat and use the pointer to them that was
1247 saved in the above call. */
1248 symbols
= _bfd_generic_link_get_symbols(abfd
);
1249 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, symbols
);
1250 if (reloc_count
<= 0)
1256 /* Now walk through all the relocations in this section. */
1257 for (i
= 0; i
< reloc_count
; i
++)
1259 arelent
*reloc
= relocs
[i
];
1260 asymbol
*symbol
= *(reloc
->sym_ptr_ptr
);
1263 /* We've got an indirect reloc. See if we need to add it
1264 to the function vector table. At this point, we have
1265 to add a new entry for each unique symbol referenced
1266 by an R_MEM_INDIRECT relocation except for a reloc
1267 against the absolute section symbol. */
1268 if (reloc
->howto
->type
== R_MEM_INDIRECT
1269 && symbol
!= bfd_abs_section_ptr
->symbol
)
1272 struct funcvec_hash_entry
*h
;
1274 name
= symbol
->name
;
1275 if (symbol
->flags
& BSF_LOCAL
)
1277 char *new_name
= bfd_malloc (strlen (name
) + 9);
1279 if (new_name
== NULL
)
1282 strcpy (new_name
, name
);
1283 sprintf (new_name
+ strlen (name
), "_%08x",
1284 (int) symbol
->section
);
1288 /* Look this symbol up in the function vector hash table. */
1289 h
= funcvec_hash_lookup (h8300_coff_hash_table (info
)->funcvec_hash_table
,
1290 name
, false, false);
1292 /* If this symbol isn't already in the hash table, add
1293 it and bump up the size of the hash table. */
1296 h
= funcvec_hash_lookup (h8300_coff_hash_table (info
)->funcvec_hash_table
,
1304 /* Bump the size of the vectors section. Each vector
1305 takes 2 bytes on the h8300 and 4 bytes on the h8300h. */
1306 if (bfd_get_mach (abfd
) == bfd_mach_h8300
)
1307 h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
+= 2;
1308 else if (bfd_get_mach (abfd
) == bfd_mach_h8300h
1309 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
1310 h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
+= 4;
1315 /* We're done with the relocations, release them. */
1319 /* Now actually allocate some space for the function vector. It's
1320 wasteful to do this more than once, but this is easier. */
1321 if (h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
!= 0)
1323 /* Free the old contents. */
1324 if (h8300_coff_hash_table (info
)->vectors_sec
->contents
)
1325 free (h8300_coff_hash_table (info
)->vectors_sec
->contents
);
1327 /* Allocate new contents. */
1328 h8300_coff_hash_table (info
)->vectors_sec
->contents
1329 = bfd_malloc (h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
);
1335 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1336 #define coff_reloc16_estimate h8300_reloc16_estimate
1337 #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1338 #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
1340 #define COFF_LONG_FILENAMES
1341 #include "coffcode.h"
1343 #undef coff_bfd_get_relocated_section_contents
1344 #undef coff_bfd_relax_section
1345 #define coff_bfd_get_relocated_section_contents \
1346 bfd_coff_reloc16_get_relocated_section_contents
1347 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1349 CREATE_BIG_COFF_TARGET_VEC (h8300coff_vec
, "coff-h8300", BFD_IS_RELAXABLE
, 0, '_', NULL
)