Convert to C90 and a few tweaks.
[external/binutils.git] / bfd / coff-h8300.c
1 /* BFD back-end for Renesas H8/300 COFF binaries.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003
4    Free Software Foundation, Inc.
5    Written by Steve Chamberlain, <sac@cygnus.com>.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
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.
13
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.
18
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.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27 #include "genlink.h"
28 #include "coff/h8300.h"
29 #include "coff/internal.h"
30 #include "libcoff.h"
31 #include "libiberty.h"
32
33 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
34
35 /* We derive a hash table from the basic BFD hash table to
36    hold entries in the function vector.  Aside from the
37    info stored by the basic hash table, we need the offset
38    of a particular entry within the hash table as well as
39    the offset where we'll add the next entry.  */
40
41 struct funcvec_hash_entry
42   {
43     /* The basic hash table entry.  */
44     struct bfd_hash_entry root;
45
46     /* The offset within the vectors section where
47        this entry lives.  */
48     bfd_vma offset;
49   };
50
51 struct funcvec_hash_table
52   {
53     /* The basic hash table.  */
54     struct bfd_hash_table root;
55
56     bfd *abfd;
57
58     /* Offset at which we'll add the next entry.  */
59     unsigned int offset;
60   };
61
62 static struct bfd_hash_entry *
63 funcvec_hash_newfunc
64   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
65
66 static bfd_boolean
67 funcvec_hash_table_init
68   PARAMS ((struct funcvec_hash_table *, bfd *,
69            struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
70                                        struct bfd_hash_table *,
71                                        const char *)));
72
73 static bfd_reloc_status_type special
74   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
75 static int select_reloc
76   PARAMS ((reloc_howto_type *));
77 static void rtype2howto
78   PARAMS ((arelent *, struct internal_reloc *));
79 static void reloc_processing
80   PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
81 static bfd_boolean h8300_symbol_address_p
82   PARAMS ((bfd *, asection *, bfd_vma));
83 static int h8300_reloc16_estimate
84   PARAMS ((bfd *, asection *, arelent *, unsigned int,
85     struct bfd_link_info *));
86 static void h8300_reloc16_extra_cases
87   PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
88     bfd_byte *, unsigned int *, unsigned int *));
89 static bfd_boolean h8300_bfd_link_add_symbols
90   PARAMS ((bfd *, struct bfd_link_info *));
91
92 /* To lookup a value in the function vector hash table.  */
93 #define funcvec_hash_lookup(table, string, create, copy) \
94   ((struct funcvec_hash_entry *) \
95    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
96
97 /* The derived h8300 COFF linker table.  Note it's derived from
98    the generic linker hash table, not the COFF backend linker hash
99    table!  We use this to attach additional data structures we
100    need while linking on the h8300.  */
101 struct h8300_coff_link_hash_table {
102   /* The main hash table.  */
103   struct generic_link_hash_table root;
104
105   /* Section for the vectors table.  This gets attached to a
106      random input bfd, we keep it here for easy access.  */
107   asection *vectors_sec;
108
109   /* Hash table of the functions we need to enter into the function
110      vector.  */
111   struct funcvec_hash_table *funcvec_hash_table;
112 };
113
114 static struct bfd_link_hash_table *h8300_coff_link_hash_table_create
115   PARAMS ((bfd *));
116
117 /* Get the H8/300 COFF linker hash table from a link_info structure.  */
118
119 #define h8300_coff_hash_table(p) \
120   ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p))))
121
122 /* Initialize fields within a funcvec hash table entry.  Called whenever
123    a new entry is added to the funcvec hash table.  */
124
125 static struct bfd_hash_entry *
126 funcvec_hash_newfunc (entry, gen_table, string)
127      struct bfd_hash_entry *entry;
128      struct bfd_hash_table *gen_table;
129      const char *string;
130 {
131   struct funcvec_hash_entry *ret;
132   struct funcvec_hash_table *table;
133
134   ret = (struct funcvec_hash_entry *) entry;
135   table = (struct funcvec_hash_table *) gen_table;
136
137   /* Allocate the structure if it has not already been allocated by a
138      subclass.  */
139   if (ret == NULL)
140     ret = ((struct funcvec_hash_entry *)
141            bfd_hash_allocate (gen_table,
142                               sizeof (struct funcvec_hash_entry)));
143   if (ret == NULL)
144     return NULL;
145
146   /* Call the allocation method of the superclass.  */
147   ret = ((struct funcvec_hash_entry *)
148          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, gen_table, string));
149
150   if (ret == NULL)
151     return NULL;
152
153   /* Note where this entry will reside in the function vector table.  */
154   ret->offset = table->offset;
155
156   /* Bump the offset at which we store entries in the function
157      vector.  We'd like to bump up the size of the vectors section,
158      but it's not easily available here.  */
159   if (bfd_get_mach (table->abfd) == bfd_mach_h8300)
160     table->offset += 2;
161   else if (bfd_get_mach (table->abfd) == bfd_mach_h8300h
162            || bfd_get_mach (table->abfd) == bfd_mach_h8300s)
163     table->offset += 4;
164   else
165     return NULL;
166
167   /* Everything went OK.  */
168   return (struct bfd_hash_entry *) ret;
169 }
170
171 /* Initialize the function vector hash table.  */
172
173 static bfd_boolean
174 funcvec_hash_table_init (table, abfd, newfunc)
175      struct funcvec_hash_table *table;
176      bfd *abfd;
177      struct bfd_hash_entry *(*newfunc)
178        PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
179                 const char *));
180 {
181   /* Initialize our local fields, then call the generic initialization
182      routine.  */
183   table->offset = 0;
184   table->abfd = abfd;
185   return (bfd_hash_table_init (&table->root, newfunc));
186 }
187
188 /* Create the derived linker hash table.  We use a derived hash table
189    basically to hold "static" information during an H8/300 coff link
190    without using static variables.  */
191
192 static struct bfd_link_hash_table *
193 h8300_coff_link_hash_table_create (abfd)
194      bfd *abfd;
195 {
196   struct h8300_coff_link_hash_table *ret;
197   bfd_size_type amt = sizeof (struct h8300_coff_link_hash_table);
198
199   ret = (struct h8300_coff_link_hash_table *) bfd_malloc (amt);
200   if (ret == NULL)
201     return NULL;
202   if (!_bfd_link_hash_table_init (&ret->root.root, abfd,
203                                   _bfd_generic_link_hash_newfunc))
204     {
205       free (ret);
206       return NULL;
207     }
208
209   /* Initialize our data.  */
210   ret->vectors_sec = NULL;
211   ret->funcvec_hash_table = NULL;
212
213   /* OK.  Everything's initialized, return the base pointer.  */
214   return &ret->root.root;
215 }
216
217 /* Special handling for H8/300 relocs.
218    We only come here for pcrel stuff and return normally if not an -r link.
219    When doing -r, we can't do any arithmetic for the pcrel stuff, because
220    the code in reloc.c assumes that we can manipulate the targets of
221    the pcrel branches.  This isn't so, since the H8/300 can do relaxing,
222    which means that the gap after the instruction may not be enough to
223    contain the offset required for the branch, so we have to use only
224    the addend until the final link.  */
225
226 static bfd_reloc_status_type
227 special (abfd, reloc_entry, symbol, data, input_section, output_bfd,
228          error_message)
229      bfd *abfd ATTRIBUTE_UNUSED;
230      arelent *reloc_entry ATTRIBUTE_UNUSED;
231      asymbol *symbol ATTRIBUTE_UNUSED;
232      PTR data ATTRIBUTE_UNUSED;
233      asection *input_section ATTRIBUTE_UNUSED;
234      bfd *output_bfd;
235      char **error_message ATTRIBUTE_UNUSED;
236 {
237   if (output_bfd == (bfd *) NULL)
238     return bfd_reloc_continue;
239
240   /* Adjust the reloc address to that in the output section.  */
241   reloc_entry->address += input_section->output_offset;
242   return bfd_reloc_ok;
243 }
244
245 static reloc_howto_type howto_table[] = {
246   HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8", FALSE, 0x000000ff, 0x000000ff, FALSE),
247   HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
248   HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "32", FALSE, 0xffffffff, 0xffffffff, FALSE),
249   HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8", FALSE, 0x000000ff, 0x000000ff, TRUE),
250   HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, special, "DISP16", FALSE, 0x0000ffff, 0x0000ffff, TRUE),
251   HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, special, "DISP32", FALSE, 0xffffffff, 0xffffffff, TRUE),
252   HOWTO (R_MOV16B1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:16", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
253   HOWTO (R_MOV16B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:16", FALSE, 0x000000ff, 0x000000ff, FALSE),
254   HOWTO (R_JMP1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16/pcrel", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
255   HOWTO (R_JMP2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pcrecl/16", FALSE, 0x000000ff, 0x000000ff, FALSE),
256   HOWTO (R_JMPL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "24/pcrell", FALSE, 0x00ffffff, 0x00ffffff, FALSE),
257   HOWTO (R_JMPL2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pc8/24", FALSE, 0x000000ff, 0x000000ff, FALSE),
258   HOWTO (R_MOV24B1, 0, 1, 32, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:24", FALSE, 0xffffffff, 0xffffffff, FALSE),
259   HOWTO (R_MOV24B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:24", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
260
261   /* An indirect reference to a function.  This causes the function's address
262      to be added to the function vector in lo-mem and puts the address of
263      the function vector's entry in the jsr instruction.  */
264   HOWTO (R_MEM_INDIRECT, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8/indirect", FALSE, 0x000000ff, 0x000000ff, FALSE),
265
266   /* Internal reloc for relaxing.  This is created when a 16bit pc-relative
267      branch is turned into an 8bit pc-relative branch.  */
268   HOWTO (R_PCRWORD_B, 0, 0, 8, TRUE, 0, complain_overflow_bitfield, special, "relaxed bCC:16", FALSE, 0x000000ff, 0x000000ff, FALSE),
269
270   HOWTO (R_MOVL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,special, "32/24 relaxable move", FALSE, 0xffffffff, 0xffffffff, FALSE),
271
272   HOWTO (R_MOVL2, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "32/24 relaxed move", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
273
274   HOWTO (R_BCC_INV, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8 inverted", FALSE, 0x000000ff, 0x000000ff, TRUE),
275
276   HOWTO (R_JMP_DEL, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "Deleted jump", FALSE, 0x000000ff, 0x000000ff, TRUE),
277 };
278
279 /* Turn a howto into a reloc number.  */
280
281 #define SELECT_RELOC(x,howto) \
282   { x.r_type = select_reloc (howto); }
283
284 #define BADMAG(x) (H8300BADMAG (x) && H8300HBADMAG (x) && H8300SBADMAG (x) \
285                                    && H8300HNBADMAG(x) && H8300SNBADMAG(x))
286 #define H8300 1                 /* Customize coffcode.h  */
287 #define __A_MAGIC_SET__
288
289 /* Code to swap in the reloc.  */
290 #define SWAP_IN_RELOC_OFFSET    H_GET_32
291 #define SWAP_OUT_RELOC_OFFSET   H_PUT_32
292 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
293   dst->r_stuff[0] = 'S'; \
294   dst->r_stuff[1] = 'C';
295
296 static int
297 select_reloc (howto)
298      reloc_howto_type *howto;
299 {
300   return howto->type;
301 }
302
303 /* Code to turn a r_type into a howto ptr, uses the above howto table.  */
304
305 static void
306 rtype2howto (internal, dst)
307      arelent *internal;
308      struct internal_reloc *dst;
309 {
310   switch (dst->r_type)
311     {
312     case R_RELBYTE:
313       internal->howto = howto_table + 0;
314       break;
315     case R_RELWORD:
316       internal->howto = howto_table + 1;
317       break;
318     case R_RELLONG:
319       internal->howto = howto_table + 2;
320       break;
321     case R_PCRBYTE:
322       internal->howto = howto_table + 3;
323       break;
324     case R_PCRWORD:
325       internal->howto = howto_table + 4;
326       break;
327     case R_PCRLONG:
328       internal->howto = howto_table + 5;
329       break;
330     case R_MOV16B1:
331       internal->howto = howto_table + 6;
332       break;
333     case R_MOV16B2:
334       internal->howto = howto_table + 7;
335       break;
336     case R_JMP1:
337       internal->howto = howto_table + 8;
338       break;
339     case R_JMP2:
340       internal->howto = howto_table + 9;
341       break;
342     case R_JMPL1:
343       internal->howto = howto_table + 10;
344       break;
345     case R_JMPL2:
346       internal->howto = howto_table + 11;
347       break;
348     case R_MOV24B1:
349       internal->howto = howto_table + 12;
350       break;
351     case R_MOV24B2:
352       internal->howto = howto_table + 13;
353       break;
354     case R_MEM_INDIRECT:
355       internal->howto = howto_table + 14;
356       break;
357     case R_PCRWORD_B:
358       internal->howto = howto_table + 15;
359       break;
360     case R_MOVL1:
361       internal->howto = howto_table + 16;
362       break;
363     case R_MOVL2:
364       internal->howto = howto_table + 17;
365       break;
366     case R_BCC_INV:
367       internal->howto = howto_table + 18;
368       break;
369     case R_JMP_DEL:
370       internal->howto = howto_table + 19;
371       break;
372     default:
373       abort ();
374       break;
375     }
376 }
377
378 #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
379
380 /* Perform any necessary magic to the addend in a reloc entry.  */
381
382 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
383  cache_ptr->addend = ext_reloc.r_offset;
384
385 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
386  reloc_processing (relent, reloc, symbols, abfd, section)
387
388 static void
389 reloc_processing (relent, reloc, symbols, abfd, section)
390      arelent *relent;
391      struct internal_reloc *reloc;
392      asymbol **symbols;
393      bfd *abfd;
394      asection *section;
395 {
396   relent->address = reloc->r_vaddr;
397   rtype2howto (relent, reloc);
398
399   if (((int) reloc->r_symndx) > 0)
400     relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
401   else
402     relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
403
404   relent->addend = reloc->r_offset;
405
406   relent->address -= section->vma;
407 #if 0
408   relent->section = 0;
409 #endif
410 }
411
412 static bfd_boolean
413 h8300_symbol_address_p (abfd, input_section, address)
414      bfd *abfd;
415      asection *input_section;
416      bfd_vma address;
417 {
418   asymbol **s;
419
420   s = _bfd_generic_link_get_symbols (abfd);
421   BFD_ASSERT (s != (asymbol **) NULL);
422
423   /* Search all the symbols for one in INPUT_SECTION with
424      address ADDRESS.  */
425   while (*s)
426     {
427       asymbol *p = *s;
428
429       if (p->section == input_section
430           && (input_section->output_section->vma
431               + input_section->output_offset
432               + p->value) == address)
433         return TRUE;
434       s++;
435     }
436   return FALSE;
437 }
438
439 /* If RELOC represents a relaxable instruction/reloc, change it into
440    the relaxed reloc, notify the linker that symbol addresses
441    have changed (bfd_perform_slip) and return how much the current
442    section has shrunk by.
443
444    FIXME: Much of this code has knowledge of the ordering of entries
445    in the howto table.  This needs to be fixed.  */
446
447 static int
448 h8300_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
449      bfd *abfd;
450      asection *input_section;
451      arelent *reloc;
452      unsigned int shrink;
453      struct bfd_link_info *link_info;
454 {
455   bfd_vma value;
456   bfd_vma dot;
457   bfd_vma gap;
458   static asection *last_input_section = NULL;
459   static arelent *last_reloc = NULL;
460
461   /* The address of the thing to be relocated will have moved back by
462      the size of the shrink - but we don't change reloc->address here,
463      since we need it to know where the relocation lives in the source
464      uncooked section.  */
465   bfd_vma address = reloc->address - shrink;
466
467   if (input_section != last_input_section)
468     last_reloc = NULL;
469
470   /* Only examine the relocs which might be relaxable.  */
471   switch (reloc->howto->type)
472     {
473       /* This is the 16/24 bit absolute branch which could become an 8 bit
474          pc-relative branch.  */
475     case R_JMP1:
476     case R_JMPL1:
477       /* Get the address of the target of this branch.  */
478       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
479
480       /* Get the address of the next instruction (not the reloc).  */
481       dot = (input_section->output_section->vma
482              + input_section->output_offset + address);
483
484       /* Adjust for R_JMP1 vs R_JMPL1.  */
485       dot += (reloc->howto->type == R_JMP1 ? 1 : 2);
486
487       /* Compute the distance from this insn to the branch target.  */
488       gap = value - dot;
489
490       /* If the distance is within -128..+128 inclusive, then we can relax
491          this jump.  +128 is valid since the target will move two bytes
492          closer if we do relax this branch.  */
493       if ((int) gap >= -128 && (int) gap <= 128)
494         {
495           bfd_byte code;
496
497           if (!bfd_get_section_contents (abfd, input_section, & code,
498                                          reloc->address, 1))
499             break;
500           code = bfd_get_8 (abfd, & code);
501
502           /* It's possible we may be able to eliminate this branch entirely;
503              if the previous instruction is a branch around this instruction,
504              and there's no label at this instruction, then we can reverse
505              the condition on the previous branch and eliminate this jump.
506
507                original:                        new:
508                  bCC lab1                       bCC' lab2
509                  jmp lab2
510                 lab1:                           lab1:
511
512              This saves 4 bytes instead of two, and should be relatively
513              common.
514
515              Only perform this optimisation for jumps (code 0x5a) not
516              subroutine calls, as otherwise it could transform:
517
518                              mov.w   r0,r0
519                              beq     .L1
520                              jsr     @_bar
521                       .L1:   rts
522                       _bar:  rts
523              into:
524                              mov.w   r0,r0
525                              bne     _bar
526                              rts
527                       _bar:  rts
528
529              which changes the call (jsr) into a branch (bne).  */
530           if (code == 0x5a
531               && gap <= 126
532               && last_reloc
533               && last_reloc->howto->type == R_PCRBYTE)
534             {
535               bfd_vma last_value;
536               last_value = bfd_coff_reloc16_get_value (last_reloc, link_info,
537                                                        input_section) + 1;
538
539               if (last_value == dot + 2
540                   && last_reloc->address + 1 == reloc->address
541                   && !h8300_symbol_address_p (abfd, input_section, dot - 2))
542                 {
543                   reloc->howto = howto_table + 19;
544                   last_reloc->howto = howto_table + 18;
545                   last_reloc->sym_ptr_ptr = reloc->sym_ptr_ptr;
546                   last_reloc->addend = reloc->addend;
547                   shrink += 4;
548                   bfd_perform_slip (abfd, 4, input_section, address);
549                   break;
550                 }
551             }
552
553           /* Change the reloc type.  */
554           reloc->howto = reloc->howto + 1;
555
556           /* This shrinks this section by two bytes.  */
557           shrink += 2;
558           bfd_perform_slip (abfd, 2, input_section, address);
559         }
560       break;
561
562     /* This is the 16 bit pc-relative branch which could become an 8 bit
563        pc-relative branch.  */
564     case R_PCRWORD:
565       /* Get the address of the target of this branch, add one to the value
566          because the addend field in PCrel jumps is off by -1.  */
567       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section) + 1;
568
569       /* Get the address of the next instruction if we were to relax.  */
570       dot = input_section->output_section->vma +
571         input_section->output_offset + address;
572
573       /* Compute the distance from this insn to the branch target.  */
574       gap = value - dot;
575
576       /* If the distance is within -128..+128 inclusive, then we can relax
577          this jump.  +128 is valid since the target will move two bytes
578          closer if we do relax this branch.  */
579       if ((int) gap >= -128 && (int) gap <= 128)
580         {
581           /* Change the reloc type.  */
582           reloc->howto = howto_table + 15;
583
584           /* This shrinks this section by two bytes.  */
585           shrink += 2;
586           bfd_perform_slip (abfd, 2, input_section, address);
587         }
588       break;
589
590     /* This is a 16 bit absolute address in a mov.b insn, which can
591        become an 8 bit absolute address if it's in the right range.  */
592     case R_MOV16B1:
593       /* Get the address of the data referenced by this mov.b insn.  */
594       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
595
596       /* The address is in 0xff00..0xffff inclusive on the h8300 or
597          0xffff00..0xffffff inclusive on the h8300h, then we can
598          relax this mov.b  */
599       if ((bfd_get_mach (abfd) == bfd_mach_h8300
600            && value >= 0xff00
601            && value <= 0xffff)
602           || ((bfd_get_mach (abfd) == bfd_mach_h8300h
603                || bfd_get_mach (abfd) == bfd_mach_h8300s)
604               && value >= 0xffff00
605               && value <= 0xffffff))
606         {
607           /* Change the reloc type.  */
608           reloc->howto = reloc->howto + 1;
609
610           /* This shrinks this section by two bytes.  */
611           shrink += 2;
612           bfd_perform_slip (abfd, 2, input_section, address);
613         }
614       break;
615
616     /* Similarly for a 24 bit absolute address in a mov.b.  Note that
617        if we can't relax this into an 8 bit absolute, we'll fall through
618        and try to relax it into a 16bit absolute.  */
619     case R_MOV24B1:
620       /* Get the address of the data referenced by this mov.b insn.  */
621       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
622
623       /* The address is in 0xffff00..0xffffff inclusive on the h8300h,
624          then we can relax this mov.b  */
625       if ((bfd_get_mach (abfd) == bfd_mach_h8300h
626            || bfd_get_mach (abfd) == bfd_mach_h8300s)
627           && value >= 0xffff00
628           && value <= 0xffffff)
629         {
630           /* Change the reloc type.  */
631           reloc->howto = reloc->howto + 1;
632
633           /* This shrinks this section by four bytes.  */
634           shrink += 4;
635           bfd_perform_slip (abfd, 4, input_section, address);
636
637           /* Done with this reloc.  */
638           break;
639         }
640
641       /* FALLTHROUGH and try to turn the 32/24 bit reloc into a 16 bit
642          reloc.  */
643
644     /* This is a 24/32 bit absolute address in a mov insn, which can
645        become an 16 bit absolute address if it's in the right range.  */
646     case R_MOVL1:
647       /* Get the address of the data referenced by this mov insn.  */
648       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
649
650       /* If this address is in 0x0000..0x7fff inclusive or
651          0xff8000..0xffffff inclusive, then it can be relaxed.  */
652       if (value <= 0x7fff || value >= 0xff8000)
653         {
654           /* Change the reloc type.  */
655           reloc->howto = howto_table + 17;
656
657           /* This shrinks this section by two bytes.  */
658           shrink += 2;
659           bfd_perform_slip (abfd, 2, input_section, address);
660         }
661       break;
662
663       /* No other reloc types represent relaxing opportunities.  */
664     default:
665       break;
666     }
667
668   last_reloc = reloc;
669   last_input_section = input_section;
670   return shrink;
671 }
672
673 /* Handle relocations for the H8/300, including relocs for relaxed
674    instructions.
675
676    FIXME: Not all relocations check for overflow!  */
677
678 static void
679 h8300_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
680                            dst_ptr)
681      bfd *abfd;
682      struct bfd_link_info *link_info;
683      struct bfd_link_order *link_order;
684      arelent *reloc;
685      bfd_byte *data;
686      unsigned int *src_ptr;
687      unsigned int *dst_ptr;
688 {
689   unsigned int src_address = *src_ptr;
690   unsigned int dst_address = *dst_ptr;
691   asection *input_section = link_order->u.indirect.section;
692   bfd_vma value;
693   bfd_vma dot;
694   int gap, tmp;
695
696   switch (reloc->howto->type)
697     {
698     /* Generic 8bit pc-relative relocation.  */
699     case R_PCRBYTE:
700       /* Get the address of the target of this branch.  */
701       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
702
703       dot = (link_order->offset
704              + dst_address
705              + link_order->u.indirect.section->output_section->vma);
706
707       gap = value - dot;
708
709       /* Sanity check.  */
710       if (gap < -128 || gap > 126)
711         {
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)))
716             abort ();
717         }
718
719       /* Everything looks OK.  Apply the relocation and update the
720          src/dst address appropriately.  */
721       bfd_put_8 (abfd, gap, data + dst_address);
722       dst_address++;
723       src_address++;
724
725       /* All done.  */
726       break;
727
728     /* Generic 16bit pc-relative relocation.  */
729     case R_PCRWORD:
730       /* Get the address of the target of this branch.  */
731       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
732
733       /* Get the address of the instruction (not the reloc).  */
734       dot = (link_order->offset
735              + dst_address
736              + link_order->u.indirect.section->output_section->vma + 1);
737
738       gap = value - dot;
739
740       /* Sanity check.  */
741       if (gap > 32766 || gap < -32768)
742         {
743           if (! ((*link_info->callbacks->reloc_overflow)
744                  (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
745                   reloc->howto->name, reloc->addend, input_section->owner,
746                   input_section, reloc->address)))
747             abort ();
748         }
749
750       /* Everything looks OK.  Apply the relocation and update the
751          src/dst address appropriately.  */
752       bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
753       dst_address += 2;
754       src_address += 2;
755
756       /* All done.  */
757       break;
758
759     /* Generic 8bit absolute relocation.  */
760     case R_RELBYTE:
761       /* Get the address of the object referenced by this insn.  */
762       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
763
764       /* Sanity check.  */
765       if (value <= 0xff
766           || (value >= 0x0000ff00 && value <= 0x0000ffff)
767           || (value >= 0x00ffff00 && value <= 0x00ffffff)
768           || (value >= 0xffffff00 && value <= 0xffffffff))
769         {
770           /* Everything looks OK.  Apply the relocation and update the
771              src/dst address appropriately.  */
772           bfd_put_8 (abfd, value & 0xff, data + dst_address);
773           dst_address += 1;
774           src_address += 1;
775         }
776       else
777         {
778           if (! ((*link_info->callbacks->reloc_overflow)
779                  (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
780                   reloc->howto->name, reloc->addend, input_section->owner,
781                   input_section, reloc->address)))
782             abort ();
783         }
784
785       /* All done.  */
786       break;
787
788     /* Various simple 16bit absolute relocations.  */
789     case R_MOV16B1:
790     case R_JMP1:
791     case R_RELWORD:
792       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
793       bfd_put_16 (abfd, value, data + dst_address);
794       dst_address += 2;
795       src_address += 2;
796       break;
797
798     /* Various simple 24/32bit absolute relocations.  */
799     case R_MOV24B1:
800     case R_MOVL1:
801     case R_RELLONG:
802       /* Get the address of the target of this branch.  */
803       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
804       bfd_put_32 (abfd, value, data + dst_address);
805       dst_address += 4;
806       src_address += 4;
807       break;
808
809     /* Another 24/32bit absolute relocation.  */
810     case R_JMPL1:
811       /* Get the address of the target of this branch.  */
812       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
813
814       value = ((value & 0x00ffffff)
815                | (bfd_get_32 (abfd, data + src_address) & 0xff000000));
816       bfd_put_32 (abfd, value, data + dst_address);
817       dst_address += 4;
818       src_address += 4;
819       break;
820
821     /* A 16bit abolute relocation that was formerlly a 24/32bit
822        absolute relocation.  */
823     case R_MOVL2:
824       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
825
826       /* Sanity check.  */
827       if (value <= 0x7fff || value >= 0xff8000)
828         {
829           /* Insert the 16bit value into the proper location.  */
830           bfd_put_16 (abfd, value, data + dst_address);
831
832           /* Fix the opcode.  For all the move insns, we simply
833              need to turn off bit 0x20 in the previous byte.  */
834           data[dst_address - 1] &= ~0x20;
835           dst_address += 2;
836           src_address += 4;
837         }
838       else
839         {
840           if (! ((*link_info->callbacks->reloc_overflow)
841                  (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
842                   reloc->howto->name, reloc->addend, input_section->owner,
843                   input_section, reloc->address)))
844             abort ();
845         }
846       break;
847
848     /* A 16bit absolute branch that is now an 8-bit pc-relative branch.  */
849     case R_JMP2:
850       /* Get the address of the target of this branch.  */
851       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
852
853       /* Get the address of the next instruction.  */
854       dot = (link_order->offset
855              + dst_address
856              + link_order->u.indirect.section->output_section->vma + 1);
857
858       gap = value - dot;
859
860       /* Sanity check.  */
861       if (gap < -128 || gap > 126)
862         {
863           if (! ((*link_info->callbacks->reloc_overflow)
864                  (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
865                   reloc->howto->name, reloc->addend, input_section->owner,
866                   input_section, reloc->address)))
867             abort ();
868         }
869
870       /* Now fix the instruction itself.  */
871       switch (data[dst_address - 1])
872         {
873         case 0x5e:
874           /* jsr -> bsr */
875           bfd_put_8 (abfd, 0x55, data + dst_address - 1);
876           break;
877         case 0x5a:
878           /* jmp ->bra */
879           bfd_put_8 (abfd, 0x40, data + dst_address - 1);
880           break;
881
882         default:
883           abort ();
884         }
885
886       /* Write out the 8bit value.  */
887       bfd_put_8 (abfd, gap, data + dst_address);
888
889       dst_address += 1;
890       src_address += 3;
891
892       break;
893
894     /* A 16bit pc-relative branch that is now an 8-bit pc-relative branch.  */
895     case R_PCRWORD_B:
896       /* Get the address of the target of this branch.  */
897       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
898
899       /* Get the address of the instruction (not the reloc).  */
900       dot = (link_order->offset
901              + dst_address
902              + link_order->u.indirect.section->output_section->vma - 1);
903
904       gap = value - dot;
905
906       /* Sanity check.  */
907       if (gap < -128 || gap > 126)
908         {
909           if (! ((*link_info->callbacks->reloc_overflow)
910                  (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
911                   reloc->howto->name, reloc->addend, input_section->owner,
912                   input_section, reloc->address)))
913             abort ();
914         }
915
916       /* Now fix the instruction.  */
917       switch (data[dst_address - 2])
918         {
919         case 0x58:
920           /* bCC:16 -> bCC:8 */
921           /* Get the condition code from the original insn.  */
922           tmp = data[dst_address - 1];
923           tmp &= 0xf0;
924           tmp >>= 4;
925
926           /* Now or in the high nibble of the opcode.  */
927           tmp |= 0x40;
928
929           /* Write it.  */
930           bfd_put_8 (abfd, tmp, data + dst_address - 2);
931           break;
932
933         case 0x5c:
934           /* bsr:16 -> bsr:8 */
935           bfd_put_8 (abfd, 0x55, data + dst_address - 2);
936           break;
937
938         default:
939           abort ();
940         }
941
942       /* Output the target.  */
943       bfd_put_8 (abfd, gap, data + dst_address - 1);
944
945       /* We don't advance dst_address -- the 8bit reloc is applied at
946          dst_address - 1, so the next insn should begin at dst_address.  */
947       src_address += 2;
948
949       break;
950
951     /* Similarly for a 24bit absolute that is now 8 bits.  */
952     case R_JMPL2:
953       /* Get the address of the target of this branch.  */
954       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
955
956       /* Get the address of the instruction (not the reloc).  */
957       dot = (link_order->offset
958              + dst_address
959              + link_order->u.indirect.section->output_section->vma + 2);
960
961       gap = value - dot;
962
963       /* Fix the instruction.  */
964       switch (data[src_address])
965         {
966         case 0x5e:
967           /* jsr -> bsr */
968           bfd_put_8 (abfd, 0x55, data + dst_address);
969           break;
970         case 0x5a:
971           /* jmp ->bra */
972           bfd_put_8 (abfd, 0x40, data + dst_address);
973           break;
974         default:
975           abort ();
976         }
977
978       bfd_put_8 (abfd, gap, data + dst_address + 1);
979       dst_address += 2;
980       src_address += 4;
981
982       break;
983
984     /* A 16bit absolute mov.b that is now an 8bit absolute mov.b.  */
985     case R_MOV16B2:
986       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
987
988       /* Sanity check.  */
989       if (data[dst_address - 2] != 0x6a)
990         abort ();
991
992       /* Fix up the opcode.  */
993       switch (data[src_address - 1] & 0xf0)
994         {
995         case 0x00:
996           data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x20;
997           break;
998         case 0x80:
999           data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x30;
1000           break;
1001         default:
1002           abort ();
1003         }
1004
1005       bfd_put_8 (abfd, value & 0xff, data + dst_address - 1);
1006       src_address += 2;
1007       break;
1008
1009     /* Similarly for a 24bit mov.b  */
1010     case R_MOV24B2:
1011       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
1012
1013       /* Sanity check.  */
1014       if (data[dst_address - 2] != 0x6a)
1015         abort ();
1016
1017       /* Fix up the opcode.  */
1018       switch (data[src_address - 1] & 0xf0)
1019         {
1020         case 0x20:
1021           data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x20;
1022           break;
1023         case 0xa0:
1024           data[dst_address - 2] = (data[src_address - 1] & 0xf) | 0x30;
1025           break;
1026         default:
1027           abort ();
1028         }
1029
1030       bfd_put_8 (abfd, value & 0xff, data + dst_address - 1);
1031       src_address += 4;
1032       break;
1033
1034     case R_BCC_INV:
1035       /* Get the address of the target of this branch.  */
1036       value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
1037
1038       dot = (link_order->offset
1039              + dst_address
1040              + link_order->u.indirect.section->output_section->vma) + 1;
1041
1042       gap = value - dot;
1043
1044       /* Sanity check.  */
1045       if (gap < -128 || gap > 126)
1046         {
1047           if (! ((*link_info->callbacks->reloc_overflow)
1048                  (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
1049                   reloc->howto->name, reloc->addend, input_section->owner,
1050                   input_section, reloc->address)))
1051             abort ();
1052         }
1053
1054       /* Everything looks OK.  Fix the condition in the instruction, apply
1055          the relocation, and update the src/dst address appropriately.  */
1056
1057       bfd_put_8 (abfd, bfd_get_8 (abfd, data + dst_address - 1) ^ 1,
1058                  data + dst_address - 1);
1059       bfd_put_8 (abfd, gap, data + dst_address);
1060       dst_address++;
1061       src_address++;
1062
1063       /* All done.  */
1064       break;
1065
1066     case R_JMP_DEL:
1067       src_address += 4;
1068       break;
1069
1070     /* An 8bit memory indirect instruction (jmp/jsr).
1071
1072        There's several things that need to be done to handle
1073        this relocation.
1074
1075        If this is a reloc against the absolute symbol, then
1076        we should handle it just R_RELBYTE.  Likewise if it's
1077        for a symbol with a value ge 0 and le 0xff.
1078
1079        Otherwise it's a jump/call through the function vector,
1080        and the linker is expected to set up the function vector
1081        and put the right value into the jump/call instruction.  */
1082     case R_MEM_INDIRECT:
1083       {
1084         /* We need to find the symbol so we can determine it's
1085            address in the function vector table.  */
1086         asymbol *symbol;
1087         const char *name;
1088         struct funcvec_hash_table *ftab;
1089         struct funcvec_hash_entry *h;
1090         struct h8300_coff_link_hash_table *htab;
1091         asection *vectors_sec;
1092
1093         if (link_info->hash->creator != abfd->xvec)
1094           {
1095             (*_bfd_error_handler)
1096               (_("cannot handle R_MEM_INDIRECT reloc when using %s output"),
1097                link_info->hash->creator->name);
1098
1099             /* What else can we do?  This function doesn't allow return
1100                of an error, and we don't want to call abort as that
1101                indicates an internal error.  */
1102 #ifndef EXIT_FAILURE
1103 #define EXIT_FAILURE 1
1104 #endif
1105             xexit (EXIT_FAILURE);
1106           }
1107         htab = h8300_coff_hash_table (link_info);
1108         vectors_sec = htab->vectors_sec;
1109
1110         /* First see if this is a reloc against the absolute symbol
1111            or against a symbol with a nonnegative value <= 0xff.  */
1112         symbol = *(reloc->sym_ptr_ptr);
1113         value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
1114         if (symbol == bfd_abs_section_ptr->symbol
1115             || value <= 0xff)
1116           {
1117             /* This should be handled in a manner very similar to
1118                R_RELBYTES.   If the value is in range, then just slam
1119                the value into the right location.  Else trigger a
1120                reloc overflow callback.  */
1121             if (value <= 0xff)
1122               {
1123                 bfd_put_8 (abfd, value, data + dst_address);
1124                 dst_address += 1;
1125                 src_address += 1;
1126               }
1127             else
1128               {
1129                 if (! ((*link_info->callbacks->reloc_overflow)
1130                        (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
1131                         reloc->howto->name, reloc->addend, input_section->owner,
1132                         input_section, reloc->address)))
1133                   abort ();
1134               }
1135             break;
1136           }
1137
1138         /* This is a jump/call through a function vector, and we're
1139            expected to create the function vector ourselves.
1140
1141            First look up this symbol in the linker hash table -- we need
1142            the derived linker symbol which holds this symbol's index
1143            in the function vector.  */
1144         name = symbol->name;
1145         if (symbol->flags & BSF_LOCAL)
1146           {
1147             char *new_name = bfd_malloc ((bfd_size_type) strlen (name) + 9);
1148             if (new_name == NULL)
1149               abort ();
1150
1151             strcpy (new_name, name);
1152             sprintf (new_name + strlen (name), "_%08x",
1153                      (int) symbol->section);
1154             name = new_name;
1155           }
1156
1157         ftab = htab->funcvec_hash_table;
1158         h = funcvec_hash_lookup (ftab, name, FALSE, FALSE);
1159
1160         /* This shouldn't ever happen.  If it does that means we've got
1161            data corruption of some kind.  Aborting seems like a reasonable
1162            thing to do here.  */
1163         if (h == NULL || vectors_sec == NULL)
1164           abort ();
1165
1166         /* Place the address of the function vector entry into the
1167            reloc's address.  */
1168         bfd_put_8 (abfd,
1169                    vectors_sec->output_offset + h->offset,
1170                    data + dst_address);
1171
1172         dst_address++;
1173         src_address++;
1174
1175         /* Now create an entry in the function vector itself.  */
1176         if (bfd_get_mach (input_section->owner) == bfd_mach_h8300)
1177           bfd_put_16 (abfd,
1178                       bfd_coff_reloc16_get_value (reloc,
1179                                                   link_info,
1180                                                   input_section),
1181                       vectors_sec->contents + h->offset);
1182         else if (bfd_get_mach (input_section->owner) == bfd_mach_h8300h
1183                  || bfd_get_mach (input_section->owner) == bfd_mach_h8300s)
1184           bfd_put_32 (abfd,
1185                       bfd_coff_reloc16_get_value (reloc,
1186                                                   link_info,
1187                                                   input_section),
1188                       vectors_sec->contents + h->offset);
1189         else
1190           abort ();
1191
1192         /* Gross.  We've already written the contents of the vector section
1193            before we get here...  So we write it again with the new data.  */
1194         bfd_set_section_contents (vectors_sec->output_section->owner,
1195                                   vectors_sec->output_section,
1196                                   vectors_sec->contents,
1197                                   (file_ptr) vectors_sec->output_offset,
1198                                   vectors_sec->_raw_size);
1199         break;
1200       }
1201
1202     default:
1203       abort ();
1204       break;
1205
1206     }
1207
1208   *src_ptr = src_address;
1209   *dst_ptr = dst_address;
1210 }
1211
1212 /* Routine for the h8300 linker.
1213
1214    This routine is necessary to handle the special R_MEM_INDIRECT
1215    relocs on the h8300.  It's responsible for generating a vectors
1216    section and attaching it to an input bfd as well as sizing
1217    the vectors section.  It also creates our vectors hash table.
1218
1219    It uses the generic linker routines to actually add the symbols.
1220    from this BFD to the bfd linker hash table.  It may add a few
1221    selected static symbols to the bfd linker hash table.  */
1222
1223 static bfd_boolean
1224 h8300_bfd_link_add_symbols (abfd, info)
1225      bfd *abfd;
1226      struct bfd_link_info *info;
1227 {
1228   asection *sec;
1229   struct funcvec_hash_table *funcvec_hash_table;
1230   bfd_size_type amt;
1231   struct h8300_coff_link_hash_table *htab;
1232
1233   /* Add the symbols using the generic code.  */
1234   _bfd_generic_link_add_symbols (abfd, info);
1235
1236   if (info->hash->creator != abfd->xvec)
1237     return TRUE;
1238
1239   htab = h8300_coff_hash_table (info);
1240
1241   /* If we haven't created a vectors section, do so now.  */
1242   if (!htab->vectors_sec)
1243     {
1244       flagword flags;
1245
1246       /* Make sure the appropriate flags are set, including SEC_IN_MEMORY.  */
1247       flags = (SEC_ALLOC | SEC_LOAD
1248                | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY);
1249       htab->vectors_sec = bfd_make_section (abfd, ".vectors");
1250
1251       /* If the section wasn't created, or we couldn't set the flags,
1252          quit quickly now, rather than dying a painful death later.  */
1253       if (!htab->vectors_sec
1254           || !bfd_set_section_flags (abfd, htab->vectors_sec, flags))
1255         return FALSE;
1256
1257       /* Also create the vector hash table.  */
1258       amt = sizeof (struct funcvec_hash_table);
1259       funcvec_hash_table = (struct funcvec_hash_table *) bfd_alloc (abfd, amt);
1260
1261       if (!funcvec_hash_table)
1262         return FALSE;
1263
1264       /* And initialize the funcvec hash table.  */
1265       if (!funcvec_hash_table_init (funcvec_hash_table, abfd,
1266                                     funcvec_hash_newfunc))
1267         {
1268           bfd_release (abfd, funcvec_hash_table);
1269           return FALSE;
1270         }
1271
1272       /* Store away a pointer to the funcvec hash table.  */
1273       htab->funcvec_hash_table = funcvec_hash_table;
1274     }
1275
1276   /* Load up the function vector hash table.  */
1277   funcvec_hash_table = htab->funcvec_hash_table;
1278
1279   /* Now scan the relocs for all the sections in this bfd; create
1280      additional space in the .vectors section as needed.  */
1281   for (sec = abfd->sections; sec; sec = sec->next)
1282     {
1283       long reloc_size, reloc_count, i;
1284       asymbol **symbols;
1285       arelent **relocs;
1286
1287       /* Suck in the relocs, symbols & canonicalize them.  */
1288       reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
1289       if (reloc_size <= 0)
1290         continue;
1291
1292       relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
1293       if (!relocs)
1294         return FALSE;
1295
1296       /* The symbols should have been read in by _bfd_generic link_add_symbols
1297          call abovec, so we can cheat and use the pointer to them that was
1298          saved in the above call.  */
1299       symbols = _bfd_generic_link_get_symbols(abfd);
1300       reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, symbols);
1301       if (reloc_count <= 0)
1302         {
1303           free (relocs);
1304           continue;
1305         }
1306
1307       /* Now walk through all the relocations in this section.  */
1308       for (i = 0; i < reloc_count; i++)
1309         {
1310           arelent *reloc = relocs[i];
1311           asymbol *symbol = *(reloc->sym_ptr_ptr);
1312           const char *name;
1313
1314           /* We've got an indirect reloc.  See if we need to add it
1315              to the function vector table.   At this point, we have
1316              to add a new entry for each unique symbol referenced
1317              by an R_MEM_INDIRECT relocation except for a reloc
1318              against the absolute section symbol.  */
1319           if (reloc->howto->type == R_MEM_INDIRECT
1320               && symbol != bfd_abs_section_ptr->symbol)
1321
1322             {
1323               struct funcvec_hash_table *ftab;
1324               struct funcvec_hash_entry *h;
1325
1326               name = symbol->name;
1327               if (symbol->flags & BSF_LOCAL)
1328                 {
1329                   char *new_name;
1330
1331                   new_name = bfd_malloc ((bfd_size_type) strlen (name) + 9);
1332                   if (new_name == NULL)
1333                     abort ();
1334
1335                   strcpy (new_name, name);
1336                   sprintf (new_name + strlen (name), "_%08x",
1337                            (int) symbol->section);
1338                   name = new_name;
1339                 }
1340
1341               /* Look this symbol up in the function vector hash table.  */
1342               ftab = htab->funcvec_hash_table;
1343               h = funcvec_hash_lookup (ftab, name, FALSE, FALSE);
1344
1345               /* If this symbol isn't already in the hash table, add
1346                  it and bump up the size of the hash table.  */
1347               if (h == NULL)
1348                 {
1349                   h = funcvec_hash_lookup (ftab, name, TRUE, TRUE);
1350                   if (h == NULL)
1351                     {
1352                       free (relocs);
1353                       return FALSE;
1354                     }
1355
1356                   /* Bump the size of the vectors section.  Each vector
1357                      takes 2 bytes on the h8300 and 4 bytes on the h8300h.  */
1358                   if (bfd_get_mach (abfd) == bfd_mach_h8300)
1359                     htab->vectors_sec->_raw_size += 2;
1360                   else if (bfd_get_mach (abfd) == bfd_mach_h8300h
1361                            || bfd_get_mach (abfd) == bfd_mach_h8300s)
1362                     htab->vectors_sec->_raw_size += 4;
1363                 }
1364             }
1365         }
1366
1367       /* We're done with the relocations, release them.  */
1368       free (relocs);
1369     }
1370
1371   /* Now actually allocate some space for the function vector.  It's
1372      wasteful to do this more than once, but this is easier.  */
1373   sec = htab->vectors_sec;
1374   if (sec->_raw_size != 0)
1375     {
1376       /* Free the old contents.  */
1377       if (sec->contents)
1378         free (sec->contents);
1379
1380       /* Allocate new contents.  */
1381       sec->contents = bfd_malloc (sec->_raw_size);
1382     }
1383
1384   return TRUE;
1385 }
1386
1387 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1388 #define coff_reloc16_estimate h8300_reloc16_estimate
1389 #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1390 #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
1391
1392 #define COFF_LONG_FILENAMES
1393 #include "coffcode.h"
1394
1395 #undef coff_bfd_get_relocated_section_contents
1396 #undef coff_bfd_relax_section
1397 #define coff_bfd_get_relocated_section_contents \
1398   bfd_coff_reloc16_get_relocated_section_contents
1399 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1400
1401 CREATE_BIG_COFF_TARGET_VEC (h8300coff_vec, "coff-h8300", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)