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