* sparc-opc.c: Set F_FBR on floating point branch instructions.
[deliverable/binutils-gdb.git] / bfd / coff-h8300.c
1 /* BFD back-end for Hitachi H8/300 COFF binaries.
2 Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Written by Steve Chamberlain, <sac@cygnus.com>.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "obstack.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "genlink.h"
27 #include "coff/h8300.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30
31 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
32
33 /* We derive a hash table from the basic BFD hash table to
34 hold entries in the function vector. Aside from the
35 info stored by the basic hash table, we need the offset
36 of a particular entry within the hash table as well as
37 the offset where we'll add the next entry. */
38
39 struct funcvec_hash_entry
40 {
41 /* The basic hash table entry. */
42 struct bfd_hash_entry root;
43
44 /* The offset within the vectors section where
45 this entry lives. */
46 bfd_vma offset;
47 };
48
49 struct funcvec_hash_table
50 {
51 /* The basic hash table. */
52 struct bfd_hash_table root;
53
54 bfd *abfd;
55
56 /* Offset at which we'll add the next entry. */
57 unsigned int offset;
58 };
59
60 static struct bfd_hash_entry *
61 funcvec_hash_newfunc
62 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
63
64 static boolean
65 funcvec_hash_table_init
66 PARAMS ((struct funcvec_hash_table *, bfd *,
67 struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *,
68 struct bfd_hash_table *,
69 const char *))));
70
71 /* To lookup a value in the function vector hash table. */
72 #define funcvec_hash_lookup(table, string, create, copy) \
73 ((struct funcvec_hash_entry *) \
74 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
75
76 /* The derived h8300 COFF linker table. Note it's derived from
77 the generic linker hash table, not the COFF backend linker hash
78 table! We use this to attach additional data structures we
79 need while linking on the h8300. */
80 struct h8300_coff_link_hash_table
81 {
82 /* The main hash table. */
83 struct generic_link_hash_table root;
84
85 /* Section for the vectors table. This gets attached to a
86 random input bfd, we keep it here for easy access. */
87 asection *vectors_sec;
88
89 /* Hash table of the functions we need to enter into the function
90 vector. */
91 struct funcvec_hash_table *funcvec_hash_table;
92 };
93
94 static struct bfd_link_hash_table *h8300_coff_link_hash_table_create
95 PARAMS ((bfd *));
96
97 /* Get the H8/300 COFF linker hash table from a link_info structure. */
98
99 #define h8300_coff_hash_table(p) \
100 ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p))))
101
102 /* Initialize fields within a funcvec hash table entry. Called whenever
103 a new entry is added to the funcvec hash table. */
104
105 static struct bfd_hash_entry *
106 funcvec_hash_newfunc (entry, gen_table, string)
107 struct bfd_hash_entry *entry;
108 struct bfd_hash_table *gen_table;
109 const char *string;
110 {
111 struct funcvec_hash_entry *ret;
112 struct funcvec_hash_table *table;
113
114 ret = (struct funcvec_hash_entry *) entry;
115 table = (struct funcvec_hash_table *) gen_table;
116
117 /* Allocate the structure if it has not already been allocated by a
118 subclass. */
119 if (ret == NULL)
120 ret = ((struct funcvec_hash_entry *)
121 bfd_hash_allocate (gen_table,
122 sizeof (struct funcvec_hash_entry)));
123 if (ret == NULL)
124 return NULL;
125
126 /* Call the allocation method of the superclass. */
127 ret = ((struct funcvec_hash_entry *)
128 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, gen_table, string));
129
130 if (ret == NULL)
131 return NULL;
132
133 /* Note where this entry will reside in the function vector table. */
134 ret->offset = table->offset;
135
136 /* Bump the offset at which we store entries in the function
137 vector. We'd like to bump up the size of the vectors section,
138 but it's not easily available here. */
139 if (bfd_get_mach (table->abfd) == bfd_mach_h8300)
140 table->offset += 2;
141 else if (bfd_get_mach (table->abfd) == bfd_mach_h8300h)
142 table->offset += 4;
143 else
144 return NULL;
145
146 /* Everything went OK. */
147 return (struct bfd_hash_entry *) ret;
148 }
149
150 /* Initialize the function vector hash table. */
151
152 static boolean
153 funcvec_hash_table_init (table, abfd, newfunc)
154 struct funcvec_hash_table *table;
155 bfd *abfd;
156 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
157 struct bfd_hash_table *,
158 const char *));
159 {
160 /* Initialize our local fields, then call the generic initialization
161 routine. */
162 table->offset = 0;
163 table->abfd = abfd;
164 return (bfd_hash_table_init (&table->root, newfunc));
165 }
166
167 /* Create the derived linker hash table. We use a derived hash table
168 basically to hold "static" information during an h8/300 coff link
169 without using static variables. */
170
171 static struct bfd_link_hash_table *
172 h8300_coff_link_hash_table_create (abfd)
173 bfd *abfd;
174 {
175 struct h8300_coff_link_hash_table *ret;
176 ret = ((struct h8300_coff_link_hash_table *)
177 bfd_alloc (abfd, sizeof (struct h8300_coff_link_hash_table)));
178 if (ret == NULL)
179 return NULL;
180 if (!_bfd_link_hash_table_init (&ret->root.root, abfd, generic_link_hash_newfunc))
181 {
182 bfd_release (abfd, ret);
183 return NULL;
184 }
185
186 /* Initialize our data. */
187 ret->vectors_sec = NULL;
188 ret->funcvec_hash_table = NULL;
189
190 /* OK. Everything's intialized, return the base pointer. */
191 return &ret->root.root;
192 }
193
194 /* special handling for H8/300 relocs.
195 We only come here for pcrel stuff and return normally if not an -r link.
196 When doing -r, we can't do any arithmetic for the pcrel stuff, because
197 the code in reloc.c assumes that we can manipulate the targets of
198 the pcrel branches. This isn't so, since the H8/300 can do relaxing,
199 which means that the gap after the instruction may not be enough to
200 contain the offset required for the branch, so we have to use the only
201 the addend until the final link */
202
203 static bfd_reloc_status_type
204 special (abfd, reloc_entry, symbol, data, input_section, output_bfd,
205 error_message)
206 bfd *abfd;
207 arelent *reloc_entry;
208 asymbol *symbol;
209 PTR data;
210 asection *input_section;
211 bfd *output_bfd;
212 char **error_message;
213 {
214 if (output_bfd == (bfd *) NULL)
215 return bfd_reloc_continue;
216
217 return bfd_reloc_ok;
218 }
219
220 static reloc_howto_type howto_table[] =
221 {
222 HOWTO (R_RELBYTE, 0, 0, 8, false, 0, complain_overflow_bitfield, special, "8", false, 0x000000ff, 0x000000ff, false),
223 HOWTO (R_RELWORD, 0, 1, 16, false, 0, complain_overflow_bitfield, special, "16", false, 0x0000ffff, 0x0000ffff, false),
224 HOWTO (R_RELLONG, 0, 2, 32, false, 0, complain_overflow_bitfield, special, "32", false, 0xffffffff, 0xffffffff, false),
225 HOWTO (R_PCRBYTE, 0, 0, 8, true, 0, complain_overflow_signed, special, "DISP8", false, 0x000000ff, 0x000000ff, true),
226 HOWTO (R_PCRWORD, 0, 1, 16, true, 0, complain_overflow_signed, special, "DISP16", false, 0x0000ffff, 0x0000ffff, true),
227 HOWTO (R_PCRLONG, 0, 2, 32, true, 0, complain_overflow_signed, special, "DISP32", false, 0xffffffff, 0xffffffff, true),
228 HOWTO (R_MOVB1, 0, 1, 16, false, 0, complain_overflow_bitfield, special, "16/8", false, 0x0000ffff, 0x0000ffff, false),
229 HOWTO (R_MOVB2, 0, 1, 16, false, 0, complain_overflow_bitfield, special, "8/16", false, 0x0000ffff, 0x0000ffff, false),
230 HOWTO (R_JMP1, 0, 1, 16, false, 0, complain_overflow_bitfield, special, "16/pcrel", false, 0x0000ffff, 0x0000ffff, false),
231 HOWTO (R_JMP2, 0, 0, 8, false, 0, complain_overflow_bitfield, special, "pcrecl/16", false, 0x000000ff, 0x000000ff, false),
232
233
234 HOWTO (R_JMPL1, 0, 2, 32, false, 0, complain_overflow_bitfield, special, "24/pcrell", false, 0x00ffffff, 0x00ffffff, false),
235 HOWTO (R_JMPL_B8, 0, 0, 8, false, 0, complain_overflow_bitfield, special, "pc8/24", false, 0x000000ff, 0x000000ff, false),
236
237 HOWTO (R_MOVLB1, 0, 1, 16, false, 0, complain_overflow_bitfield,special, "24/8", false, 0x0000ffff, 0x0000ffff, false),
238 HOWTO (R_MOVLB2, 0, 1, 16, false, 0, complain_overflow_bitfield, special, "8/24", false, 0x0000ffff, 0x0000ffff, false),
239
240 /* An indirect reference to a function. This causes the function's address
241 to be added to the function vector in lo-mem and puts the address of
242 the function vector's entry in the jsr instruction. */
243 HOWTO (R_MEM_INDIRECT, 0, 0, 8, false, 0, complain_overflow_bitfield, special, "8/indirect", false, 0x000000ff, 0x000000ff, false),
244
245 };
246
247
248 /* Turn a howto into a reloc number */
249
250 #define SELECT_RELOC(x,howto) \
251 { x.r_type = select_reloc(howto); }
252
253 #define BADMAG(x) (H8300BADMAG(x)&& H8300HBADMAG(x))
254 #define H8300 1 /* Customize coffcode.h */
255 #define __A_MAGIC_SET__
256
257
258
259 /* Code to swap in the reloc */
260 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
261 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
262 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
263 dst->r_stuff[0] = 'S'; \
264 dst->r_stuff[1] = 'C';
265
266
267 static int
268 select_reloc (howto)
269 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
277 static void
278 rtype2howto (internal, dst)
279 arelent *internal;
280 struct internal_reloc *dst;
281 {
282 switch (dst->r_type)
283 {
284 case R_RELBYTE:
285 internal->howto = howto_table + 0;
286 break;
287 case R_RELWORD:
288 internal->howto = howto_table + 1;
289 break;
290 case R_RELLONG:
291 internal->howto = howto_table + 2;
292 break;
293 case R_PCRBYTE:
294 internal->howto = howto_table + 3;
295 break;
296 case R_PCRWORD:
297 internal->howto = howto_table + 4;
298 break;
299 case R_PCRLONG:
300 internal->howto = howto_table + 5;
301 break;
302 case R_MOVB1:
303 internal->howto = howto_table + 6;
304 break;
305 case R_MOVB2:
306 internal->howto = howto_table + 7;
307 break;
308 case R_JMP1:
309 internal->howto = howto_table + 8;
310 break;
311 case R_JMP2:
312 internal->howto = howto_table + 9;
313 break;
314 case R_JMPL1:
315 internal->howto = howto_table + 10;
316 break;
317 case R_JMPL_B8:
318 internal->howto = howto_table + 11;
319 break;
320 case R_MOVLB1:
321 internal->howto = howto_table + 12;
322 break;
323 case R_MOVLB2:
324 internal->howto = howto_table + 13;
325 break;
326 case R_MEM_INDIRECT:
327 internal->howto = howto_table + 14;
328 break;
329 default:
330 abort ();
331 break;
332 }
333 }
334
335 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
336
337
338 /* Perform any necessaru magic to the addend in a reloc entry */
339
340
341 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
342 cache_ptr->addend = ext_reloc.r_offset;
343
344
345 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
346 reloc_processing(relent, reloc, symbols, abfd, section)
347
348 static void
349 reloc_processing (relent, reloc, symbols, abfd, section)
350 arelent * relent;
351 struct internal_reloc *reloc;
352 asymbol ** symbols;
353 bfd * abfd;
354 asection * section;
355 {
356 relent->address = reloc->r_vaddr;
357 rtype2howto (relent, reloc);
358
359 if (((int) reloc->r_symndx) > 0)
360 {
361 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
362 }
363 else
364 {
365 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
366 }
367
368
369
370 relent->addend = reloc->r_offset;
371
372 relent->address -= section->vma;
373 /* relent->section = 0;*/
374 }
375
376
377 static int
378 h8300_reloc16_estimate(abfd, input_section, reloc, shrink, link_info)
379 bfd *abfd;
380 asection *input_section;
381 arelent *reloc;
382 unsigned int shrink;
383 struct bfd_link_info *link_info;
384 {
385 bfd_vma value;
386 bfd_vma dot;
387 bfd_vma gap;
388
389 /* The address of the thing to be relocated will have moved back by
390 the size of the shrink - but we don't change reloc->address here,
391 since we need it to know where the relocation lives in the source
392 uncooked section */
393
394 /* reloc->address -= shrink; conceptual */
395
396 bfd_vma address = reloc->address - shrink;
397
398
399 switch (reloc->howto->type)
400 {
401 case R_MOVB2:
402 case R_JMP2:
403 shrink+=2;
404 break;
405
406 /* Thing is a move one byte */
407 case R_MOVB1:
408 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
409
410 if (value >= 0xff00)
411 {
412
413 /* Change the reloc type from 16bit, possible 8 to 8bit
414 possible 16 */
415 reloc->howto = reloc->howto + 1;
416 /* The place to relc moves back by one */
417 /* This will be two bytes smaller in the long run */
418 shrink +=2 ;
419 bfd_perform_slip(abfd, 2, input_section, address);
420 }
421
422 break;
423 /* This is the 24 bit branch which could become an 8 bitter,
424 the relocation points to the first byte of the insn, not the
425 actual data */
426
427 case R_JMPL1:
428 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
429
430 dot = input_section->output_section->vma +
431 input_section->output_offset + address;
432
433 /* See if the address we're looking at within 127 bytes of where
434 we are, if so then we can use a small branch rather than the
435 jump we were going to */
436
437 gap = value - dot ;
438
439 if (-120 < (long)gap && (long)gap < 120 )
440 {
441
442 /* Change the reloc type from 24bit, possible 8 to 8bit
443 possible 32 */
444 reloc->howto = reloc->howto + 1;
445 /* This will be two bytes smaller in the long run */
446 shrink +=2 ;
447 bfd_perform_slip(abfd, 2, input_section, address);
448 }
449 break;
450
451 case R_JMP1:
452
453 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
454
455 dot = input_section->output_section->vma +
456 input_section->output_offset + address;
457
458 /* See if the address we're looking at within 127 bytes of where
459 we are, if so then we can use a small branch rather than the
460 jump we were going to */
461
462 gap = value - (dot - shrink);
463
464
465 if (-120 < (long)gap && (long)gap < 120 )
466 {
467
468 /* Change the reloc type from 16bit, possible 8 to 8bit
469 possible 16 */
470 reloc->howto = reloc->howto + 1;
471 /* The place to relc moves back by one */
472
473 /* This will be two bytes smaller in the long run */
474 shrink +=2 ;
475 bfd_perform_slip(abfd, 2, input_section, address);
476 }
477 break;
478 }
479
480
481 return shrink;
482 }
483
484
485 /* First phase of a relaxing link */
486
487 /* Reloc types
488 large small
489 R_MOVB1 R_MOVB2 mov.b with 16bit or 8 bit address
490 R_JMP1 R_JMP2 jmp or pcrel branch
491 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
492 R_MOVLB1 R_MOVLB2 24 or 8 bit reloc for mov.b
493
494 */
495
496
497 static void
498 h8300_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
499 dst_ptr)
500 bfd *abfd;
501 struct bfd_link_info *link_info;
502 struct bfd_link_order *link_order;
503 arelent *reloc;
504 bfd_byte *data;
505 unsigned int *src_ptr;
506 unsigned int *dst_ptr;
507 {
508 unsigned int src_address = *src_ptr;
509 unsigned int dst_address = *dst_ptr;
510 asection *input_section = link_order->u.indirect.section;
511
512 switch (reloc->howto->type)
513 {
514 /* A 24 bit branch which could be a 8 bit pcrel, really pointing to
515 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
516 case R_PCRBYTE:
517 {
518 bfd_vma dot = link_order->offset
519 + dst_address
520 + link_order->u.indirect.section->output_section->vma;
521 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
522 - dot);
523 if (gap > 127 || gap < -128)
524 {
525 if (! ((*link_info->callbacks->reloc_overflow)
526 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
527 reloc->howto->name, reloc->addend, input_section->owner,
528 input_section, reloc->address)))
529 abort ();
530 }
531 gap &= ~1;
532 bfd_put_8 (abfd, gap, data + dst_address);
533 dst_address++;
534 src_address++;
535
536 break;
537 }
538 case R_PCRWORD:
539 {
540 bfd_vma dot = link_order->offset
541 + dst_address
542 + link_order->u.indirect.section->output_section->vma;
543 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
544 - dot) - 1;
545 if (gap > 32767 || gap < -32768)
546 {
547 if (! ((*link_info->callbacks->reloc_overflow)
548 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
549 reloc->howto->name, reloc->addend, input_section->owner,
550 input_section, reloc->address)))
551 abort ();
552 }
553
554 bfd_put_16 (abfd, gap, data + dst_address);
555 dst_address+=2;
556 src_address+=2;
557
558 break;
559 }
560
561 case R_RELBYTE:
562 {
563 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
564 input_section);
565 if (gap < 0xff
566 || (gap >= 0x0000ff00
567 && gap <= 0x0000ffff)
568 || ( gap >= 0x00ffff00
569 && gap <= 0x00ffffff)
570 || ( gap >= 0xffffff00
571 && gap <= 0xffffffff))
572 {
573 bfd_put_8 (abfd, gap, data + dst_address);
574 dst_address += 1;
575 src_address += 1;
576 }
577 else
578 {
579 if (! ((*link_info->callbacks->reloc_overflow)
580 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
581 reloc->howto->name, reloc->addend, input_section->owner,
582 input_section, reloc->address)))
583 abort ();
584 }
585 }
586 break;
587 case R_JMP1:
588 /* A relword which would have like to have been a pcrel */
589 case R_MOVB1:
590 /* A relword which would like to have been modified but
591 didn't make it */
592 case R_RELWORD:
593 bfd_put_16 (abfd,
594 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
595 data + dst_address);
596 dst_address += 2;
597 src_address += 2;
598 break;
599 case R_RELLONG:
600 bfd_put_32 (abfd,
601 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
602 data + dst_address);
603 dst_address += 4;
604 src_address += 4;
605 break;
606
607 case R_MOVB2:
608 /* Special relaxed type, there will be a gap between where we
609 get stuff from and where we put stuff to now
610
611 for a mov.b @aa:16 -> mov.b @aa:8
612 opcode 0x6a 0x0y offset
613 -> 0x2y off
614 */
615 if (data[dst_address - 1] != 0x6a)
616 abort ();
617 switch (data[src_address] & 0xf0)
618 {
619 case 0x00:
620 /* Src is memory */
621 data[dst_address - 1] = (data[src_address] & 0xf) | 0x20;
622 break;
623 case 0x80:
624 /* Src is reg */
625 data[dst_address - 1] = (data[src_address] & 0xf) | 0x30;
626 break;
627 default:
628 abort ();
629 }
630
631 /* the offset must fit ! after all, what was all the relaxing
632 about ? */
633
634 bfd_put_8 (abfd,
635 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
636 data + dst_address);
637
638 /* Note the magic - src goes up by two bytes, but dst by only
639 one */
640 dst_address += 1;
641 src_address += 3;
642
643 break;
644
645 case R_JMP2:
646
647 /* Speciial relaxed type */
648 {
649 bfd_vma dot = link_order->offset
650 + dst_address
651 + link_order->u.indirect.section->output_section->vma;
652
653 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
654 - dot - 1);
655
656 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
657 abort ();
658
659 bfd_put_8 (abfd, gap, data + dst_address);
660
661 switch (data[dst_address - 1])
662 {
663 case 0x5e:
664 /* jsr -> bsr */
665 bfd_put_8 (abfd, 0x55, data + dst_address - 1);
666 break;
667 case 0x5a:
668 /* jmp ->bra */
669 bfd_put_8 (abfd, 0x40, data + dst_address - 1);
670 break;
671
672 default:
673 abort ();
674 }
675 dst_address++;
676 src_address += 3;
677
678 break;
679 }
680 break;
681
682 case R_JMPL_B8: /* 24 bit branch which is now 8 bits */
683
684 /* Speciial relaxed type */
685 {
686 bfd_vma dot = link_order->offset
687 + dst_address
688 + link_order->u.indirect.section->output_section->vma;
689
690 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
691 - dot - 2);
692
693 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
694 abort ();
695
696 switch (data[src_address])
697 {
698 case 0x5e:
699 /* jsr -> bsr */
700 bfd_put_8 (abfd, 0x55, data + dst_address);
701 break;
702 case 0x5a:
703 /* jmp ->bra */
704 bfd_put_8 (abfd, 0x40, data + dst_address);
705 break;
706
707 default:
708 bfd_put_8 (abfd, 0xde, data + dst_address);
709 break;
710 }
711
712 bfd_put_8 (abfd, gap, data + dst_address + 1);
713 dst_address += 2;
714 src_address += 4;
715
716 break;
717 }
718
719 case R_JMPL1:
720 {
721 int v = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
722 int o = bfd_get_32 (abfd, data + src_address);
723 v = (v & 0x00ffffff) | (o & 0xff000000);
724 bfd_put_32 (abfd, v, data + dst_address);
725 dst_address += 4;
726 src_address += 4;
727 }
728
729 break;
730
731
732 /* A 24 bit mov which could be an 8 bit move, really pointing to
733 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
734 case R_MOVLB1:
735 {
736 int v = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
737 int o = bfd_get_32 (abfd, data + dst_address);
738 v = (v & 0x00ffffff) | (o & 0xff000000);
739 bfd_put_32 (abfd, v, data + dst_address);
740 dst_address += 4;
741 src_address += 4;
742 }
743
744 break;
745
746 /* An 8bit memory indirect instruction (jmp/jsr).
747
748 There's several things that need to be done to handle
749 this relocation.
750
751 If this is a reloc against the absolute symbol, then
752 we should handle it just R_RELBYTE. Likewise if it's
753 for a symbol with a value ge 0 and le 0xff.
754
755 Otherwise it's a jump/call through the function vector,
756 and the linker is expected to set up the function vector
757 and put the right value into the jump/call instruction. */
758 case R_MEM_INDIRECT:
759 {
760 /* We need to find the symbol so we can determine it's
761 address in the function vector table. */
762 asymbol *symbol;
763 bfd_vma value;
764 char *name;
765 struct funcvec_hash_entry *h;
766 asection *vectors_sec = h8300_coff_hash_table (link_info)->vectors_sec;
767
768 /* First see if this is a reloc against the absolute symbol
769 or against a symbol with a nonnegative value <= 0xff. */
770 symbol = *(reloc->sym_ptr_ptr);
771 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
772 if (symbol == bfd_abs_section_ptr->symbol
773 || (value >= 0 && value <= 0xff))
774 {
775 /* This should be handled in a manner very similar to
776 R_RELBYTES. If the value is in range, then just slam
777 the value into the right location. Else trigger a
778 reloc overflow callback. */
779 if (value >= 0 && value <= 0xff)
780 {
781 bfd_put_8 (abfd, value, data + dst_address);
782 dst_address += 1;
783 src_address += 1;
784 }
785 else
786 {
787 if (! ((*link_info->callbacks->reloc_overflow)
788 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
789 reloc->howto->name, reloc->addend, input_section->owner,
790 input_section, reloc->address)))
791 abort ();
792 }
793 break;
794 }
795
796 /* This is a jump/call through a function vector, and we're
797 expected to create the function vector ourselves.
798
799 First look up this symbol in the linker hash table -- we need
800 the derived linker symbol which holds this symbol's index
801 in the function vector. */
802 name = symbol->name;
803 if (symbol->flags & BSF_LOCAL)
804 {
805 char *new_name = bfd_malloc (strlen (name) + 9);
806 if (new_name == NULL)
807 abort ();
808
809 strcpy (new_name, name);
810 sprintf (new_name + strlen (name), "_%08x",
811 (int)symbol->section);
812 name = new_name;
813 }
814
815 h = funcvec_hash_lookup (h8300_coff_hash_table (link_info)->funcvec_hash_table,
816 name, false, false);
817
818 /* This shouldn't ever happen. If it does that means we've got
819 data corruption of some kind. Aborting seems like a reasonable
820 think to do here. */
821 if (h == NULL || vectors_sec == NULL)
822 abort ();
823
824 /* Place the address of the function vector entry into the
825 reloc's address. */
826 bfd_put_8 (abfd,
827 vectors_sec->output_offset + h->offset,
828 data + dst_address);
829
830 dst_address++;
831 src_address++;
832
833 /* Now create an entry in the function vector itself. */
834 if (bfd_get_mach (input_section->owner) == bfd_mach_h8300)
835 bfd_put_16 (abfd,
836 bfd_coff_reloc16_get_value (reloc,
837 link_info,
838 input_section),
839 vectors_sec->contents + h->offset);
840 else if (bfd_get_mach (input_section->owner) == bfd_mach_h8300h)
841 bfd_put_32 (abfd,
842 bfd_coff_reloc16_get_value (reloc,
843 link_info,
844 input_section),
845 vectors_sec->contents + h->offset);
846 else
847 abort ();
848
849 /* Gross. We've already written the contents of the vector section
850 before we get here... So we write it again with the new data. */
851 bfd_set_section_contents (vectors_sec->output_section->owner,
852 vectors_sec->output_section,
853 vectors_sec->contents,
854 vectors_sec->output_offset,
855 vectors_sec->_raw_size);
856 break;
857 }
858
859 default:
860 abort ();
861 break;
862
863 }
864
865 *src_ptr = src_address;
866 *dst_ptr = dst_address;
867 }
868
869
870 /* Routine for the h8300 linker.
871
872 This routine is necessary to handle the special R_MEM_INDIRECT
873 relocs on the h8300. It's responsible for generating a vectors
874 section and attaching it to an input bfd as well as sizing
875 the vectors section. It also creates our vectors hash table.
876
877 It uses the generic linker routines to actually add the symbols.
878 from this BFD to the bfd linker hash table. It may add a few
879 selected static symbols to the bfd linker hash table. */
880
881 static boolean
882 h8300_bfd_link_add_symbols(abfd, info)
883 bfd *abfd;
884 struct bfd_link_info *info;
885 {
886 asection *sec;
887 struct funcvec_hash_table *funcvec_hash_table;
888
889 /* If we haven't created a vectors section, do so now. */
890 if (!h8300_coff_hash_table (info)->vectors_sec)
891 {
892 flagword flags;
893
894 /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */
895 flags = (SEC_ALLOC | SEC_LOAD
896 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY);
897 h8300_coff_hash_table (info)->vectors_sec = bfd_make_section (abfd,
898 ".vectors");
899
900 /* If the section wasn't created, or we couldn't set the flags,
901 quit quickly now, rather than dieing a painful death later. */
902 if (! h8300_coff_hash_table (info)->vectors_sec
903 || ! bfd_set_section_flags (abfd,
904 h8300_coff_hash_table(info)->vectors_sec,
905 flags))
906 return false;
907
908 /* Also create the vector hash table. */
909 funcvec_hash_table = ((struct funcvec_hash_table *)
910 bfd_alloc (abfd, sizeof (struct funcvec_hash_table)));
911
912 if (!funcvec_hash_table)
913 return false;
914
915 /* And initialize the funcvec hash table. */
916 if (!funcvec_hash_table_init (funcvec_hash_table, abfd,
917 funcvec_hash_newfunc))
918 {
919 bfd_release (abfd, funcvec_hash_table);
920 return false;
921 }
922
923 /* Store away a pointer to the funcvec hash table. */
924 h8300_coff_hash_table (info)->funcvec_hash_table = funcvec_hash_table;
925 }
926
927 /* Load up the function vector hash table. */
928 funcvec_hash_table = h8300_coff_hash_table (info)->funcvec_hash_table;
929
930 /* Add the symbols using the generic code. */
931 _bfd_generic_link_add_symbols (abfd, info);
932
933 /* Now scan the relocs for all the sections in this bfd; create
934 additional space in the .vectors section as needed. */
935 for (sec = abfd->sections; sec; sec = sec->next)
936 {
937 unsigned long reloc_size, reloc_count, i;
938 asymbol **symbols;
939 arelent **relocs;
940
941 /* Suck in the relocs, symbols & canonicalize them. */
942 reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
943 if (reloc_size <= 0)
944 continue;
945
946 relocs = (arelent **)bfd_malloc ((size_t)reloc_size);
947 if (!relocs)
948 return false;
949
950 /* The symbols should have been read in by _bfd_generic link_add_symbols
951 call abovec, so we can cheat and use the pointer to them that was
952 saved in the above call. */
953 symbols = _bfd_generic_link_get_symbols(abfd);
954 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, symbols);
955
956 /* Now walk through all the relocations in this section. */
957 for (i = 0; i < reloc_count; i++)
958 {
959 arelent *reloc = relocs[i];
960 asymbol *symbol = *(reloc->sym_ptr_ptr);
961 char *name;
962
963 /* We've got an indirect reloc. See if we need to add it
964 to the function vector table. At this point, we have
965 to add a new entry for each unique symbol referenced
966 by an R_MEM_INDIRECT relocation except for a reloc
967 against the absolute section symbol. */
968 if (reloc->howto->type == R_MEM_INDIRECT
969 && symbol != bfd_abs_section_ptr->symbol)
970
971 {
972 struct funcvec_hash_entry *h;
973
974 name = symbol->name;
975 if (symbol->flags & BSF_LOCAL)
976 {
977 char *new_name = bfd_malloc (strlen (name) + 9);
978
979 if (new_name == NULL)
980 abort ();
981
982 strcpy (new_name, name);
983 sprintf (new_name + strlen (name), "_%08x",
984 (int)symbol->section);
985 name = new_name;
986 }
987
988 /* Look this symbol up in the function vector hash table. */
989 h = funcvec_hash_lookup (h8300_coff_hash_table (info)->funcvec_hash_table,
990 name, false, false);
991
992
993 /* If this symbol isn't already in the hash table, add
994 it and bump up the size of the hash table. */
995 if (h == NULL)
996 {
997 h = funcvec_hash_lookup (h8300_coff_hash_table (info)->funcvec_hash_table,
998 name, true, true);
999 if (h == NULL)
1000 {
1001 free (relocs);
1002 return false;
1003 }
1004
1005 /* Bump the size of the vectors section. Each vector
1006 takes 2 bytes on the h8300 and 4 bytes on the h8300h. */
1007 if (bfd_get_mach (abfd) == bfd_mach_h8300)
1008 h8300_coff_hash_table (info)->vectors_sec->_raw_size += 2;
1009 else if (bfd_get_mach (abfd) == bfd_mach_h8300h)
1010 h8300_coff_hash_table (info)->vectors_sec->_raw_size += 4;
1011 }
1012 }
1013 }
1014
1015 /* We're done with the relocations, release them. */
1016 free (relocs);
1017 }
1018
1019 /* Now actually allocate some space for the function vector. It's
1020 wasteful to do this more than once, but this is easier. */
1021 if (h8300_coff_hash_table (info)->vectors_sec->_raw_size != 0)
1022 {
1023 /* Free the old contents. */
1024 if (h8300_coff_hash_table (info)->vectors_sec->contents)
1025 free (h8300_coff_hash_table (info)->vectors_sec->contents);
1026
1027 /* Allocate new contents. */
1028 h8300_coff_hash_table (info)->vectors_sec->contents
1029 = bfd_malloc (h8300_coff_hash_table (info)->vectors_sec->_raw_size);
1030 }
1031
1032 return true;
1033 }
1034
1035 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1036 #define coff_reloc16_estimate h8300_reloc16_estimate
1037 #define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1038 #define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
1039
1040 #define COFF_LONG_FILENAMES
1041 #include "coffcode.h"
1042
1043
1044 #undef coff_bfd_get_relocated_section_contents
1045 #undef coff_bfd_relax_section
1046 #define coff_bfd_get_relocated_section_contents \
1047 bfd_coff_reloc16_get_relocated_section_contents
1048 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1049
1050
1051
1052 const bfd_target h8300coff_vec =
1053 {
1054 "coff-h8300", /* name */
1055 bfd_target_coff_flavour,
1056 BFD_ENDIAN_BIG, /* data byte order is big */
1057 BFD_ENDIAN_BIG, /* header byte order is big */
1058
1059 (HAS_RELOC | EXEC_P | /* object flags */
1060 HAS_LINENO | HAS_DEBUG |
1061 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
1062 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1063 '_', /* leading char */
1064 '/', /* ar_pad_char */
1065 15, /* ar_max_namelen */
1066 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1067 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1068 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1069 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1070 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1071 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1072
1073 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1074 bfd_generic_archive_p, _bfd_dummy_target},
1075 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1076 bfd_false},
1077 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1078 _bfd_write_archive_contents, bfd_false},
1079
1080 BFD_JUMP_TABLE_GENERIC (coff),
1081 BFD_JUMP_TABLE_COPY (coff),
1082 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1083 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1084 BFD_JUMP_TABLE_SYMBOLS (coff),
1085 BFD_JUMP_TABLE_RELOCS (coff),
1086 BFD_JUMP_TABLE_WRITE (coff),
1087 BFD_JUMP_TABLE_LINK (coff),
1088 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1089
1090 COFF_SWAP_TABLE,
1091 };
This page took 0.051017 seconds and 4 git commands to generate.