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