1 /* BFD back-end for Hitachi H8/300 COFF binaries.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain, <sac@cygnus.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 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
142 || bfd_get_mach (table
->abfd
) == bfd_mach_h8300s
)
147 /* Everything went OK. */
148 return (struct bfd_hash_entry
*) ret
;
151 /* Initialize the function vector hash table. */
154 funcvec_hash_table_init (table
, abfd
, newfunc
)
155 struct funcvec_hash_table
*table
;
157 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
158 struct bfd_hash_table
*,
161 /* Initialize our local fields, then call the generic initialization
165 return (bfd_hash_table_init (&table
->root
, newfunc
));
168 /* Create the derived linker hash table. We use a derived hash table
169 basically to hold "static" information during an h8/300 coff link
170 without using static variables. */
172 static struct bfd_link_hash_table
*
173 h8300_coff_link_hash_table_create (abfd
)
176 struct h8300_coff_link_hash_table
*ret
;
177 ret
= ((struct h8300_coff_link_hash_table
*)
178 bfd_alloc (abfd
, sizeof (struct h8300_coff_link_hash_table
)));
181 if (!_bfd_link_hash_table_init (&ret
->root
.root
, abfd
, _bfd_generic_link_hash_newfunc
))
183 bfd_release (abfd
, ret
);
187 /* Initialize our data. */
188 ret
->vectors_sec
= NULL
;
189 ret
->funcvec_hash_table
= NULL
;
191 /* OK. Everything's intialized, return the base pointer. */
192 return &ret
->root
.root
;
195 /* special handling for H8/300 relocs.
196 We only come here for pcrel stuff and return normally if not an -r link.
197 When doing -r, we can't do any arithmetic for the pcrel stuff, because
198 the code in reloc.c assumes that we can manipulate the targets of
199 the pcrel branches. This isn't so, since the H8/300 can do relaxing,
200 which means that the gap after the instruction may not be enough to
201 contain the offset required for the branch, so we have to use the only
202 the addend until the final link */
204 static bfd_reloc_status_type
205 special (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
207 bfd
*abfd ATTRIBUTE_UNUSED
;
208 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
209 asymbol
*symbol ATTRIBUTE_UNUSED
;
210 PTR data ATTRIBUTE_UNUSED
;
211 asection
*input_section ATTRIBUTE_UNUSED
;
213 char **error_message ATTRIBUTE_UNUSED
;
215 if (output_bfd
== (bfd
*) NULL
)
216 return bfd_reloc_continue
;
221 static reloc_howto_type howto_table
[] =
223 HOWTO (R_RELBYTE
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "8", false, 0x000000ff, 0x000000ff, false),
224 HOWTO (R_RELWORD
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "16", false, 0x0000ffff, 0x0000ffff, false),
225 HOWTO (R_RELLONG
, 0, 2, 32, false, 0, complain_overflow_bitfield
, special
, "32", false, 0xffffffff, 0xffffffff, false),
226 HOWTO (R_PCRBYTE
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "DISP8", false, 0x000000ff, 0x000000ff, true),
227 HOWTO (R_PCRWORD
, 0, 1, 16, true, 0, complain_overflow_signed
, special
, "DISP16", false, 0x0000ffff, 0x0000ffff, true),
228 HOWTO (R_PCRLONG
, 0, 2, 32, true, 0, complain_overflow_signed
, special
, "DISP32", false, 0xffffffff, 0xffffffff, true),
229 HOWTO (R_MOV16B1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "relaxable mov.b:16", false, 0x0000ffff, 0x0000ffff, false),
230 HOWTO (R_MOV16B2
, 0, 1, 8, false, 0, complain_overflow_bitfield
, special
, "relaxed mov.b:16", false, 0x000000ff, 0x000000ff, false),
231 HOWTO (R_JMP1
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "16/pcrel", false, 0x0000ffff, 0x0000ffff, false),
232 HOWTO (R_JMP2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "pcrecl/16", false, 0x000000ff, 0x000000ff, false),
233 HOWTO (R_JMPL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
, special
, "24/pcrell", false, 0x00ffffff, 0x00ffffff, false),
234 HOWTO (R_JMPL2
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "pc8/24", false, 0x000000ff, 0x000000ff, false),
235 HOWTO (R_MOV24B1
, 0, 1, 32, false, 0, complain_overflow_bitfield
, special
, "relaxable mov.b:24", false, 0xffffffff, 0xffffffff, false),
236 HOWTO (R_MOV24B2
, 0, 1, 8, false, 0, complain_overflow_bitfield
, special
, "relaxed mov.b:24", false, 0x0000ffff, 0x0000ffff, false),
238 /* An indirect reference to a function. This causes the function's address
239 to be added to the function vector in lo-mem and puts the address of
240 the function vector's entry in the jsr instruction. */
241 HOWTO (R_MEM_INDIRECT
, 0, 0, 8, false, 0, complain_overflow_bitfield
, special
, "8/indirect", false, 0x000000ff, 0x000000ff, false),
243 /* Internal reloc for relaxing. This is created when a 16bit pc-relative
244 branch is turned into an 8bit pc-relative branch. */
245 HOWTO (R_PCRWORD_B
, 0, 0, 8, true, 0, complain_overflow_bitfield
, special
, "relaxed bCC:16", false, 0x000000ff, 0x000000ff, false),
247 HOWTO (R_MOVL1
, 0, 2, 32, false, 0, complain_overflow_bitfield
,special
, "32/24 relaxable move", false, 0xffffffff, 0xffffffff, false),
249 HOWTO (R_MOVL2
, 0, 1, 16, false, 0, complain_overflow_bitfield
, special
, "32/24 relaxed move", false, 0x0000ffff, 0x0000ffff, false),
251 HOWTO (R_BCC_INV
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "DISP8 inverted", false, 0x000000ff, 0x000000ff, true),
253 HOWTO (R_JMP_DEL
, 0, 0, 8, true, 0, complain_overflow_signed
, special
, "Deleted jump", false, 0x000000ff, 0x000000ff, true),
257 /* Turn a howto into a reloc number */
259 #define SELECT_RELOC(x,howto) \
260 { x.r_type = select_reloc(howto); }
262 #define BADMAG(x) (H8300BADMAG(x) && H8300HBADMAG(x) && H8300SBADMAG(x))
263 #define H8300 1 /* Customize coffcode.h */
264 #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';
278 reloc_howto_type
*howto
;
283 /* Code to turn a r_type into a howto ptr, uses the above howto table
287 rtype2howto (internal
, dst
)
289 struct internal_reloc
*dst
;
294 internal
->howto
= howto_table
+ 0;
297 internal
->howto
= howto_table
+ 1;
300 internal
->howto
= howto_table
+ 2;
303 internal
->howto
= howto_table
+ 3;
306 internal
->howto
= howto_table
+ 4;
309 internal
->howto
= howto_table
+ 5;
312 internal
->howto
= howto_table
+ 6;
315 internal
->howto
= howto_table
+ 7;
318 internal
->howto
= howto_table
+ 8;
321 internal
->howto
= howto_table
+ 9;
324 internal
->howto
= howto_table
+ 10;
327 internal
->howto
= howto_table
+ 11;
330 internal
->howto
= howto_table
+ 12;
333 internal
->howto
= howto_table
+ 13;
336 internal
->howto
= howto_table
+ 14;
339 internal
->howto
= howto_table
+ 15;
342 internal
->howto
= howto_table
+ 16;
345 internal
->howto
= howto_table
+ 17;
348 internal
->howto
= howto_table
+ 18;
351 internal
->howto
= howto_table
+ 19;
359 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
362 /* Perform any necessary magic to the addend in a reloc entry */
365 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
366 cache_ptr->addend = ext_reloc.r_offset;
369 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
370 reloc_processing(relent, reloc, symbols, abfd, section)
373 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
375 struct internal_reloc
*reloc
;
380 relent
->address
= reloc
->r_vaddr
;
381 rtype2howto (relent
, reloc
);
383 if (((int) reloc
->r_symndx
) > 0)
385 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
389 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
394 relent
->addend
= reloc
->r_offset
;
396 relent
->address
-= section
->vma
;
397 /* relent->section = 0;*/
401 h8300_symbol_address_p (abfd
, input_section
, address
)
403 asection
*input_section
;
408 s
= _bfd_generic_link_get_symbols (abfd
);
409 BFD_ASSERT (s
!= (asymbol
**) NULL
);
411 /* Search all the symbols for one in INPUT_SECTION with
416 if (p
->section
== input_section
417 && (input_section
->output_section
->vma
418 + input_section
->output_offset
419 + p
->value
) == address
)
427 /* If RELOC represents a relaxable instruction/reloc, change it into
428 the relaxed reloc, notify the linker that symbol addresses
429 have changed (bfd_perform_slip) and return how much the current
430 section has shrunk by.
432 FIXME: Much of this code has knowledge of the ordering of entries
433 in the howto table. This needs to be fixed. */
436 h8300_reloc16_estimate(abfd
, input_section
, reloc
, shrink
, link_info
)
438 asection
*input_section
;
441 struct bfd_link_info
*link_info
;
446 static asection
*last_input_section
= NULL
;
447 static arelent
*last_reloc
= NULL
;
449 /* The address of the thing to be relocated will have moved back by
450 the size of the shrink - but we don't change reloc->address here,
451 since we need it to know where the relocation lives in the source
453 bfd_vma address
= reloc
->address
- shrink
;
455 if (input_section
!= last_input_section
)
458 /* Only examine the relocs which might be relaxable. */
459 switch (reloc
->howto
->type
)
462 /* This is the 16/24 bit absolute branch which could become an 8 bit
463 pc-relative branch. */
466 /* Get the address of the target of this branch. */
467 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
469 /* Get the address of the next instruction (not the reloc). */
470 dot
= (input_section
->output_section
->vma
471 + input_section
->output_offset
+ address
);
473 /* Adjust for R_JMP1 vs R_JMPL1. */
474 dot
+= (reloc
->howto
->type
== R_JMP1
? 1 : 2);
476 /* Compute the distance from this insn to the branch target. */
479 /* If the distance is within -128..+128 inclusive, then we can relax
480 this jump. +128 is valid since the target will move two bytes
481 closer if we do relax this branch. */
482 if ((int)gap
>= -128 && (int)gap
<= 128 )
485 /* It's possible we may be able to eliminate this branch entirely;
486 if the previous instruction is a branch around this instruction,
487 and there's no label at this instruction, then we can reverse
488 the condition on the previous branch and eliminate this jump.
495 This saves 4 bytes instead of two, and should be relatively
500 && last_reloc
->howto
->type
== R_PCRBYTE
)
503 last_value
= bfd_coff_reloc16_get_value (last_reloc
, link_info
,
506 if (last_value
== dot
+ 2
507 && last_reloc
->address
+ 1 == reloc
->address
508 && ! h8300_symbol_address_p (abfd
, input_section
, dot
- 2))
510 reloc
->howto
= howto_table
+ 19;
511 last_reloc
->howto
= howto_table
+ 18;
512 last_reloc
->sym_ptr_ptr
= reloc
->sym_ptr_ptr
;
513 last_reloc
->addend
= reloc
->addend
;
515 bfd_perform_slip (abfd
, 4, input_section
, address
);
520 /* Change the reloc type. */
521 reloc
->howto
= reloc
->howto
+ 1;
523 /* This shrinks this section by two bytes. */
525 bfd_perform_slip(abfd
, 2, input_section
, address
);
529 /* This is the 16 bit pc-relative branch which could become an 8 bit
530 pc-relative branch. */
532 /* Get the address of the target of this branch, add one to the value
533 because the addend field in PCrel jumps is off by -1. */
534 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
) + 1;
536 /* Get the address of the next instruction if we were to relax. */
537 dot
= input_section
->output_section
->vma
+
538 input_section
->output_offset
+ address
;
540 /* Compute the distance from this insn to the branch target. */
543 /* If the distance is within -128..+128 inclusive, then we can relax
544 this jump. +128 is valid since the target will move two bytes
545 closer if we do relax this branch. */
546 if ((int)gap
>= -128 && (int)gap
<= 128 )
548 /* Change the reloc type. */
549 reloc
->howto
= howto_table
+ 15;
551 /* This shrinks this section by two bytes. */
553 bfd_perform_slip(abfd
, 2, input_section
, address
);
557 /* This is a 16 bit absolute address in a mov.b insn, which can
558 become an 8 bit absolute address if it's in the right range. */
560 /* Get the address of the data referenced by this mov.b insn. */
561 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
563 /* The address is in 0xff00..0xffff inclusive on the h8300 or
564 0xffff00..0xffffff inclusive on the h8300h, then we can
566 if ((bfd_get_mach (abfd
) == bfd_mach_h8300
569 || ((bfd_get_mach (abfd
) == bfd_mach_h8300h
570 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
572 && value
<= 0xffffff))
574 /* Change the reloc type. */
575 reloc
->howto
= reloc
->howto
+ 1;
577 /* This shrinks this section by two bytes. */
579 bfd_perform_slip(abfd
, 2, input_section
, address
);
583 /* Similarly for a 24 bit absolute address in a mov.b. Note that
584 if we can't relax this into an 8 bit absolute, we'll fall through
585 and try to relax it into a 16bit absolute. */
587 /* Get the address of the data referenced by this mov.b insn. */
588 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
590 /* The address is in 0xffff00..0xffffff inclusive on the h8300h,
591 then we can relax this mov.b */
592 if ((bfd_get_mach (abfd
) == bfd_mach_h8300h
593 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
595 && value
<= 0xffffff)
597 /* Change the reloc type. */
598 reloc
->howto
= reloc
->howto
+ 1;
600 /* This shrinks this section by four bytes. */
602 bfd_perform_slip(abfd
, 4, input_section
, address
);
604 /* Done with this reloc. */
608 /* FALLTHROUGH and try to turn the 32/24 bit reloc into a 16 bit
611 /* This is a 24/32 bit absolute address in a mov insn, which can
612 become an 16 bit absolute address if it's in the right range. */
614 /* Get the address of the data referenced by this mov insn. */
615 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
617 /* If this address is in 0x0000..0x7fff inclusive or
618 0xff8000..0xffffff inclusive, then it can be relaxed. */
619 if (value
<= 0x7fff || value
>= 0xff8000)
621 /* Change the reloc type. */
622 reloc
->howto
= howto_table
+ 17;
624 /* This shrinks this section by two bytes. */
626 bfd_perform_slip(abfd
, 2, input_section
, address
);
630 /* No other reloc types represent relaxing opportunities. */
636 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
)
667 /* Generic 8bit pc-relative relocation. */
669 /* Get the address of the target of this branch. */
670 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
672 dot
= (link_order
->offset
674 + link_order
->u
.indirect
.section
->output_section
->vma
);
679 if (gap
< -128 || gap
> 126)
681 if (! ((*link_info
->callbacks
->reloc_overflow
)
682 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
683 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
684 input_section
, reloc
->address
)))
688 /* Everything looks OK. Apply the relocation and update the
689 src/dst address appropriately. */
691 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
698 /* Generic 16bit pc-relative relocation. */
700 /* Get the address of the target of this branch. */
701 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
703 /* Get the address of the instruction (not the reloc). */
704 dot
= (link_order
->offset
706 + link_order
->u
.indirect
.section
->output_section
->vma
+ 1);
711 if (gap
> 32766 || gap
< -32768)
713 if (! ((*link_info
->callbacks
->reloc_overflow
)
714 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
715 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
716 input_section
, reloc
->address
)))
720 /* Everything looks OK. Apply the relocation and update the
721 src/dst address appropriately. */
723 bfd_put_16 (abfd
, gap
, data
+ dst_address
);
730 /* Generic 8bit absolute relocation. */
732 /* Get the address of the object referenced by this insn. */
733 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
737 || (value
>= 0x0000ff00 && value
<= 0x0000ffff)
738 || (value
>= 0x00ffff00 && value
<= 0x00ffffff)
739 || (value
>= 0xffffff00 && value
<= 0xffffffff))
741 /* Everything looks OK. Apply the relocation and update the
742 src/dst address appropriately. */
744 bfd_put_8 (abfd
, value
& 0xff, data
+ dst_address
);
750 if (! ((*link_info
->callbacks
->reloc_overflow
)
751 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
752 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
753 input_section
, reloc
->address
)))
760 /* Various simple 16bit absolute relocations. */
764 value
= bfd_coff_reloc16_get_value(reloc
, link_info
, input_section
);
765 bfd_put_16 (abfd
, value
, data
+ dst_address
);
770 /* Various simple 24/32bit absolute relocations. */
774 /* Get the address of the target of this branch. */
775 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
),
776 bfd_put_32 (abfd
, value
, data
+ dst_address
);
781 /* Another 24/32bit absolute relocation. */
783 /* Get the address of the target of this branch. */
784 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
786 value
= ((value
& 0x00ffffff)
787 | (bfd_get_32 (abfd
, data
+ src_address
) & 0xff000000));
788 bfd_put_32 (abfd
, value
, data
+ dst_address
);
793 /* A 16bit abolute relocation that was formerlly a 24/32bit
794 absolute relocation. */
796 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
799 if (value
<= 0x7fff || value
>= 0xff8000)
801 /* Insert the 16bit value into the proper location. */
802 bfd_put_16 (abfd
, value
, data
+ dst_address
);
804 /* Fix the opcode. For all the move insns, we simply
805 need to turn off bit 0x20 in the previous byte. */
806 data
[dst_address
- 1] &= ~0x20;
812 if (! ((*link_info
->callbacks
->reloc_overflow
)
813 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
814 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
815 input_section
, reloc
->address
)))
820 /* A 16bit absolute branch that is now an 8-bit pc-relative branch. */
822 /* Get the address of the target of this branch. */
823 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
825 /* Get the address of the next instruction. */
826 dot
= (link_order
->offset
828 + link_order
->u
.indirect
.section
->output_section
->vma
+ 1);
833 if (gap
< -128 || gap
> 126)
835 if (! ((*link_info
->callbacks
->reloc_overflow
)
836 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
837 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
838 input_section
, reloc
->address
)))
842 /* Now fix the instruction itself. */
843 switch (data
[dst_address
- 1])
847 bfd_put_8 (abfd
, 0x55, data
+ dst_address
- 1);
851 bfd_put_8 (abfd
, 0x40, data
+ dst_address
- 1);
858 /* Write out the 8bit value. */
859 bfd_put_8 (abfd
, gap
, data
+ dst_address
);
866 /* A 16bit pc-relative branch that is now an 8-bit pc-relative branch. */
868 /* Get the address of the target of this branch. */
869 value
= bfd_coff_reloc16_get_value (reloc
, link_info
, input_section
);
871 /* Get the address of the instruction (not the reloc). */
872 dot
= (link_order
->offset
874 + link_order
->u
.indirect
.section
->output_section
->vma
- 1);
879 if (gap
< -128 || gap
> 126)
881 if (! ((*link_info
->callbacks
->reloc_overflow
)
882 (link_info
, bfd_asymbol_name (*reloc
->sym_ptr_ptr
),
883 reloc
->howto
->name
, reloc
->addend
, input_section
->owner
,
884 input_section
, reloc
->address
)))
888 /* Now fix the instruction. */
889 switch (data
[dst_address
- 2])
892 /* bCC:16 -> bCC:8 */
893 /* Get the condition code from the original insn. */
894 tmp
= data
[dst_address
- 1];
898 /* Now or in the high nibble of the opcode. */
902 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
;
1166 /* Routine for the h8300 linker.
1168 This routine is necessary to handle the special R_MEM_INDIRECT
1169 relocs on the h8300. It's responsible for generating a vectors
1170 section and attaching it to an input bfd as well as sizing
1171 the vectors section. It also creates our vectors hash table.
1173 It uses the generic linker routines to actually add the symbols.
1174 from this BFD to the bfd linker hash table. It may add a few
1175 selected static symbols to the bfd linker hash table. */
1178 h8300_bfd_link_add_symbols(abfd
, info
)
1180 struct bfd_link_info
*info
;
1183 struct funcvec_hash_table
*funcvec_hash_table
;
1185 /* If we haven't created a vectors section, do so now. */
1186 if (!h8300_coff_hash_table (info
)->vectors_sec
)
1190 /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */
1191 flags
= (SEC_ALLOC
| SEC_LOAD
1192 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_READONLY
);
1193 h8300_coff_hash_table (info
)->vectors_sec
= bfd_make_section (abfd
,
1196 /* If the section wasn't created, or we couldn't set the flags,
1197 quit quickly now, rather than dieing a painful death later. */
1198 if (! h8300_coff_hash_table (info
)->vectors_sec
1199 || ! bfd_set_section_flags (abfd
,
1200 h8300_coff_hash_table(info
)->vectors_sec
,
1204 /* Also create the vector hash table. */
1205 funcvec_hash_table
= ((struct funcvec_hash_table
*)
1206 bfd_alloc (abfd
, sizeof (struct funcvec_hash_table
)));
1208 if (!funcvec_hash_table
)
1211 /* And initialize the funcvec hash table. */
1212 if (!funcvec_hash_table_init (funcvec_hash_table
, abfd
,
1213 funcvec_hash_newfunc
))
1215 bfd_release (abfd
, funcvec_hash_table
);
1219 /* Store away a pointer to the funcvec hash table. */
1220 h8300_coff_hash_table (info
)->funcvec_hash_table
= funcvec_hash_table
;
1223 /* Load up the function vector hash table. */
1224 funcvec_hash_table
= h8300_coff_hash_table (info
)->funcvec_hash_table
;
1226 /* Add the symbols using the generic code. */
1227 _bfd_generic_link_add_symbols (abfd
, info
);
1229 /* Now scan the relocs for all the sections in this bfd; create
1230 additional space in the .vectors section as needed. */
1231 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1233 long reloc_size
, reloc_count
, i
;
1237 /* Suck in the relocs, symbols & canonicalize them. */
1238 reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
1239 if (reloc_size
<= 0)
1242 relocs
= (arelent
**)bfd_malloc ((size_t)reloc_size
);
1246 /* The symbols should have been read in by _bfd_generic link_add_symbols
1247 call abovec, so we can cheat and use the pointer to them that was
1248 saved in the above call. */
1249 symbols
= _bfd_generic_link_get_symbols(abfd
);
1250 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, symbols
);
1251 if (reloc_count
<= 0)
1257 /* Now walk through all the relocations in this section. */
1258 for (i
= 0; i
< reloc_count
; i
++)
1260 arelent
*reloc
= relocs
[i
];
1261 asymbol
*symbol
= *(reloc
->sym_ptr_ptr
);
1264 /* We've got an indirect reloc. See if we need to add it
1265 to the function vector table. At this point, we have
1266 to add a new entry for each unique symbol referenced
1267 by an R_MEM_INDIRECT relocation except for a reloc
1268 against the absolute section symbol. */
1269 if (reloc
->howto
->type
== R_MEM_INDIRECT
1270 && symbol
!= bfd_abs_section_ptr
->symbol
)
1273 struct funcvec_hash_entry
*h
;
1275 name
= symbol
->name
;
1276 if (symbol
->flags
& BSF_LOCAL
)
1278 char *new_name
= bfd_malloc (strlen (name
) + 9);
1280 if (new_name
== NULL
)
1283 strcpy (new_name
, name
);
1284 sprintf (new_name
+ strlen (name
), "_%08x",
1285 (int)symbol
->section
);
1289 /* Look this symbol up in the function vector hash table. */
1290 h
= funcvec_hash_lookup (h8300_coff_hash_table (info
)->funcvec_hash_table
,
1291 name
, false, false);
1294 /* If this symbol isn't already in the hash table, add
1295 it and bump up the size of the hash table. */
1298 h
= funcvec_hash_lookup (h8300_coff_hash_table (info
)->funcvec_hash_table
,
1306 /* Bump the size of the vectors section. Each vector
1307 takes 2 bytes on the h8300 and 4 bytes on the h8300h. */
1308 if (bfd_get_mach (abfd
) == bfd_mach_h8300
)
1309 h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
+= 2;
1310 else if (bfd_get_mach (abfd
) == bfd_mach_h8300h
1311 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
1312 h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
+= 4;
1317 /* We're done with the relocations, release them. */
1321 /* Now actually allocate some space for the function vector. It's
1322 wasteful to do this more than once, but this is easier. */
1323 if (h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
!= 0)
1325 /* Free the old contents. */
1326 if (h8300_coff_hash_table (info
)->vectors_sec
->contents
)
1327 free (h8300_coff_hash_table (info
)->vectors_sec
->contents
);
1329 /* Allocate new contents. */
1330 h8300_coff_hash_table (info
)->vectors_sec
->contents
1331 = bfd_malloc (h8300_coff_hash_table (info
)->vectors_sec
->_raw_size
);
1337 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1338 #define coff_reloc16_estimate h8300_reloc16_estimate
1339 #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1340 #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
1342 #define COFF_LONG_FILENAMES
1343 #include "coffcode.h"
1346 #undef coff_bfd_get_relocated_section_contents
1347 #undef coff_bfd_relax_section
1348 #define coff_bfd_get_relocated_section_contents \
1349 bfd_coff_reloc16_get_relocated_section_contents
1350 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1353 CREATE_BIG_COFF_TARGET_VEC (h8300coff_vec
, "coff-h8300", BFD_IS_RELAXABLE
, 0, '_', NULL
)