64-bit MIPS ELF ABI objdump support:
[deliverable/binutils-gdb.git] / bfd / coff-h8300.c
CommitLineData
14aa9a78 1/* BFD back-end for Hitachi H8/300 COFF binaries.
d3e572fe 2 Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
14aa9a78 3 Written by Steve Chamberlain, <sac@cygnus.com>.
b4e42a64
SC
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
a5655244 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
b4e42a64 20
b4e42a64
SC
21#include "bfd.h"
22#include "sysdep.h"
b4e42a64 23#include "obstack.h"
4991ebb9
ILT
24#include "libbfd.h"
25#include "bfdlink.h"
d3e572fe 26#include "genlink.h"
e98e6ec1
SC
27#include "coff/h8300.h"
28#include "coff/internal.h"
b4e42a64
SC
29#include "libcoff.h"
30
e7d9ee90
SC
31#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
32
39f27966
JL
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
39struct 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
49struct 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
60static struct bfd_hash_entry *
61funcvec_hash_newfunc
62 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
63
64static boolean
65funcvec_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. */
80struct 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
94static 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
105static struct bfd_hash_entry *
106funcvec_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
152static boolean
153funcvec_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
171static struct bfd_link_hash_table *
172h8300_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;
c9f5444e 180 if (!_bfd_link_hash_table_init (&ret->root.root, abfd, _bfd_generic_link_hash_newfunc))
39f27966
JL
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}
e7d9ee90
SC
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
203static bfd_reloc_status_type
204special (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{
e7d9ee90
SC
214 if (output_bfd == (bfd *) NULL)
215 return bfd_reloc_continue;
216
217 return bfd_reloc_ok;
218}
219
14aa9a78 220static reloc_howto_type howto_table[] =
b4e42a64 221{
e7d9ee90
SC
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),
14aa9a78
ILT
232
233
e7d9ee90
SC
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),
14aa9a78 236
e7d9ee90
SC
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),
e98e6ec1 239
d3e572fe
ILT
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
c9f5444e
JL
245 /* Internal reloc for relaxing. This is created when a 16bit pc-relative
246 branch is turned into an 8bit pc-relative branch. */
247 HOWTO (R_PCRWORD_B, 0, 0, 8, true, 0, complain_overflow_bitfield, special, "pcrecl/16", false, 0x000000ff, 0x000000ff, false),
b4e42a64
SC
248};
249
250
251/* Turn a howto into a reloc number */
252
e98e6ec1 253#define SELECT_RELOC(x,howto) \
47e70c54 254 { x.r_type = select_reloc(howto); }
e98e6ec1 255
14aa9a78
ILT
256#define BADMAG(x) (H8300BADMAG(x)&& H8300HBADMAG(x))
257#define H8300 1 /* Customize coffcode.h */
b4e42a64
SC
258#define __A_MAGIC_SET__
259
260
261
262/* Code to swap in the reloc */
e98e6ec1
SC
263#define SWAP_IN_RELOC_OFFSET bfd_h_get_32
264#define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
b4e42a64
SC
265#define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
266 dst->r_stuff[0] = 'S'; \
14aa9a78
ILT
267 dst->r_stuff[1] = 'C';
268
269
270static int
271select_reloc (howto)
272 reloc_howto_type *howto;
273{
274 return howto->type;
275}
b4e42a64
SC
276
277/* Code to turn a r_type into a howto ptr, uses the above howto table
278 */
279
14aa9a78
ILT
280static void
281rtype2howto (internal, dst)
282 arelent *internal;
283 struct internal_reloc *dst;
e98e6ec1 284{
14aa9a78
ILT
285 switch (dst->r_type)
286 {
e98e6ec1 287 case R_RELBYTE:
14aa9a78 288 internal->howto = howto_table + 0;
e98e6ec1
SC
289 break;
290 case R_RELWORD:
14aa9a78
ILT
291 internal->howto = howto_table + 1;
292 break;
293 case R_RELLONG:
294 internal->howto = howto_table + 2;
295 break;
e98e6ec1 296 case R_PCRBYTE:
14aa9a78 297 internal->howto = howto_table + 3;
e98e6ec1
SC
298 break;
299 case R_PCRWORD:
14aa9a78
ILT
300 internal->howto = howto_table + 4;
301 break;
302 case R_PCRLONG:
303 internal->howto = howto_table + 5;
e98e6ec1
SC
304 break;
305 case R_MOVB1:
306 internal->howto = howto_table + 6;
307 break;
308 case R_MOVB2:
309 internal->howto = howto_table + 7;
310 break;
311 case R_JMP1:
14aa9a78 312 internal->howto = howto_table + 8;
e98e6ec1 313 break;
e98e6ec1 314 case R_JMP2:
14aa9a78
ILT
315 internal->howto = howto_table + 9;
316 break;
317 case R_JMPL1:
318 internal->howto = howto_table + 10;
319 break;
320 case R_JMPL_B8:
321 internal->howto = howto_table + 11;
322 break;
323 case R_MOVLB1:
324 internal->howto = howto_table + 12;
325 break;
326 case R_MOVLB2:
327 internal->howto = howto_table + 13;
e98e6ec1 328 break;
d3e572fe
ILT
329 case R_MEM_INDIRECT:
330 internal->howto = howto_table + 14;
331 break;
c9f5444e
JL
332 case R_PCRWORD_B:
333 internal->howto = howto_table + 15;
334 break;
e98e6ec1 335 default:
d3e572fe 336 abort ();
e98e6ec1
SC
337 break;
338 }
339}
340
341#define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
b4e42a64
SC
342
343
344/* Perform any necessaru magic to the addend in a reloc entry */
345
346
347#define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
348 cache_ptr->addend = ext_reloc.r_offset;
349
14aa9a78 350
e98e6ec1
SC
351#define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
352 reloc_processing(relent, reloc, symbols, abfd, section)
353
14aa9a78 354static void
47e70c54
SC
355reloc_processing (relent, reloc, symbols, abfd, section)
356 arelent * relent;
357 struct internal_reloc *reloc;
358 asymbol ** symbols;
359 bfd * abfd;
360 asection * section;
e98e6ec1 361{
14aa9a78
ILT
362 relent->address = reloc->r_vaddr;
363 rtype2howto (relent, reloc);
364
365 if (((int) reloc->r_symndx) > 0)
366 {
367 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
368 }
369 else
370 {
e7d9ee90 371 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
14aa9a78
ILT
372 }
373
374
375
376 relent->addend = reloc->r_offset;
377
378 relent->address -= section->vma;
379 /* relent->section = 0;*/
380}
381
382
383static int
47e70c54
SC
384h8300_reloc16_estimate(abfd, input_section, reloc, shrink, link_info)
385 bfd *abfd;
14aa9a78 386 asection *input_section;
14aa9a78
ILT
387 arelent *reloc;
388 unsigned int shrink;
4991ebb9 389 struct bfd_link_info *link_info;
14aa9a78
ILT
390{
391 bfd_vma value;
392 bfd_vma dot;
393 bfd_vma gap;
394
395 /* The address of the thing to be relocated will have moved back by
396 the size of the shrink - but we don't change reloc->address here,
397 since we need it to know where the relocation lives in the source
398 uncooked section */
399
400 /* reloc->address -= shrink; conceptual */
401
402 bfd_vma address = reloc->address - shrink;
e98e6ec1
SC
403
404
14aa9a78
ILT
405 switch (reloc->howto->type)
406 {
407 case R_MOVB2:
408 case R_JMP2:
c9f5444e 409 case R_PCRWORD_B:
14aa9a78
ILT
410 shrink+=2;
411 break;
412
413 /* Thing is a move one byte */
414 case R_MOVB1:
4991ebb9
ILT
415 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
416
14aa9a78
ILT
417 if (value >= 0xff00)
418 {
419
420 /* Change the reloc type from 16bit, possible 8 to 8bit
421 possible 16 */
422 reloc->howto = reloc->howto + 1;
423 /* The place to relc moves back by one */
424 /* This will be two bytes smaller in the long run */
425 shrink +=2 ;
47e70c54 426 bfd_perform_slip(abfd, 2, input_section, address);
14aa9a78
ILT
427 }
428
429 break;
430 /* This is the 24 bit branch which could become an 8 bitter,
431 the relocation points to the first byte of the insn, not the
432 actual data */
433
434 case R_JMPL1:
4991ebb9 435 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
14aa9a78
ILT
436
437 dot = input_section->output_section->vma +
438 input_section->output_offset + address;
e98e6ec1 439
14aa9a78
ILT
440 /* See if the address we're looking at within 127 bytes of where
441 we are, if so then we can use a small branch rather than the
442 jump we were going to */
443
444 gap = value - dot ;
445
446 if (-120 < (long)gap && (long)gap < 120 )
447 {
448
449 /* Change the reloc type from 24bit, possible 8 to 8bit
450 possible 32 */
451 reloc->howto = reloc->howto + 1;
452 /* This will be two bytes smaller in the long run */
453 shrink +=2 ;
47e70c54 454 bfd_perform_slip(abfd, 2, input_section, address);
14aa9a78
ILT
455 }
456 break;
457
458 case R_JMP1:
459
4991ebb9 460 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
14aa9a78
ILT
461
462 dot = input_section->output_section->vma +
463 input_section->output_offset + address;
464
465 /* See if the address we're looking at within 127 bytes of where
466 we are, if so then we can use a small branch rather than the
467 jump we were going to */
468
469 gap = value - (dot - shrink);
470
471
472 if (-120 < (long)gap && (long)gap < 120 )
473 {
474
475 /* Change the reloc type from 16bit, possible 8 to 8bit
476 possible 16 */
477 reloc->howto = reloc->howto + 1;
478 /* The place to relc moves back by one */
479
480 /* This will be two bytes smaller in the long run */
481 shrink +=2 ;
47e70c54 482 bfd_perform_slip(abfd, 2, input_section, address);
14aa9a78
ILT
483 }
484 break;
14aa9a78 485
c9f5444e
JL
486 case R_PCRWORD:
487
488 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
489
490 dot = input_section->output_section->vma +
491 input_section->output_offset + address - 2;
e98e6ec1 492
c9f5444e
JL
493 /* See if the address we're looking at within 127 bytes of where
494 we are, if so then we can use a small branch rather than the
495 jump we were going to */
496
497 gap = value - (dot - shrink);
498
499
500 if (-120 < (long)gap && (long)gap < 120 )
501 {
502
503 /* Change the reloc type from 16bit, possible 8 to 8bit
504 possible 16 */
505 reloc->howto = howto_table + 15;
506 /* The place to relc moves back by one */
507
508 /* This will be two bytes smaller in the long run */
509 shrink +=2 ;
510 bfd_perform_slip(abfd, 2, input_section, address);
511 }
512 break;
513 }
514
14aa9a78 515 return shrink;
e98e6ec1
SC
516}
517
14aa9a78
ILT
518
519/* First phase of a relaxing link */
520
521/* Reloc types
522 large small
523 R_MOVB1 R_MOVB2 mov.b with 16bit or 8 bit address
524 R_JMP1 R_JMP2 jmp or pcrel branch
525 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
526 R_MOVLB1 R_MOVLB2 24 or 8 bit reloc for mov.b
c9f5444e
JL
527 R_PCRWORD R_PCRWORD_B 8 bit pcrel branch from 16bit pcrel
528 branch.
14aa9a78
ILT
529
530*/
531
39f27966 532
14aa9a78 533static void
4991ebb9
ILT
534h8300_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
535 dst_ptr)
14aa9a78 536 bfd *abfd;
4991ebb9
ILT
537 struct bfd_link_info *link_info;
538 struct bfd_link_order *link_order;
14aa9a78
ILT
539 arelent *reloc;
540 bfd_byte *data;
541 unsigned int *src_ptr;
542 unsigned int *dst_ptr;
543{
544 unsigned int src_address = *src_ptr;
545 unsigned int dst_address = *dst_ptr;
4991ebb9 546 asection *input_section = link_order->u.indirect.section;
14aa9a78
ILT
547
548 switch (reloc->howto->type)
549 {
550 /* A 24 bit branch which could be a 8 bit pcrel, really pointing to
551 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
552 case R_PCRBYTE:
553 {
4991ebb9 554 bfd_vma dot = link_order->offset
14aa9a78 555 + dst_address
4991ebb9
ILT
556 + link_order->u.indirect.section->output_section->vma;
557 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
558 - dot);
14aa9a78
ILT
559 if (gap > 127 || gap < -128)
560 {
4991ebb9
ILT
561 if (! ((*link_info->callbacks->reloc_overflow)
562 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
563 reloc->howto->name, reloc->addend, input_section->owner,
564 input_section, reloc->address)))
565 abort ();
14aa9a78 566 }
e7d9ee90 567 gap &= ~1;
14aa9a78
ILT
568 bfd_put_8 (abfd, gap, data + dst_address);
569 dst_address++;
570 src_address++;
571
47e70c54
SC
572 break;
573 }
574 case R_PCRWORD:
575 {
576 bfd_vma dot = link_order->offset
577 + dst_address
578 + link_order->u.indirect.section->output_section->vma;
579 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
2f3508ad 580 - dot) - 1;
47e70c54
SC
581 if (gap > 32767 || gap < -32768)
582 {
583 if (! ((*link_info->callbacks->reloc_overflow)
584 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
585 reloc->howto->name, reloc->addend, input_section->owner,
586 input_section, reloc->address)))
587 abort ();
588 }
589
590 bfd_put_16 (abfd, gap, data + dst_address);
591 dst_address+=2;
592 src_address+=2;
593
14aa9a78
ILT
594 break;
595 }
596
597 case R_RELBYTE:
598 {
4991ebb9
ILT
599 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
600 input_section);
e7d9ee90
SC
601 if (gap < 0xff
602 || (gap >= 0x0000ff00
603 && gap <= 0x0000ffff)
604 || ( gap >= 0x00ffff00
605 && gap <= 0x00ffffff)
606 || ( gap >= 0xffffff00
607 && gap <= 0xffffffff))
608 {
609 bfd_put_8 (abfd, gap, data + dst_address);
610 dst_address += 1;
611 src_address += 1;
612 }
613 else
14aa9a78 614 {
4991ebb9
ILT
615 if (! ((*link_info->callbacks->reloc_overflow)
616 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
617 reloc->howto->name, reloc->addend, input_section->owner,
618 input_section, reloc->address)))
619 abort ();
14aa9a78 620 }
14aa9a78
ILT
621 }
622 break;
623 case R_JMP1:
624 /* A relword which would have like to have been a pcrel */
625 case R_MOVB1:
626 /* A relword which would like to have been modified but
627 didn't make it */
628 case R_RELWORD:
4991ebb9
ILT
629 bfd_put_16 (abfd,
630 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
14aa9a78
ILT
631 data + dst_address);
632 dst_address += 2;
633 src_address += 2;
634 break;
635 case R_RELLONG:
4991ebb9
ILT
636 bfd_put_32 (abfd,
637 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
14aa9a78
ILT
638 data + dst_address);
639 dst_address += 4;
640 src_address += 4;
641 break;
642
643 case R_MOVB2:
644 /* Special relaxed type, there will be a gap between where we
645 get stuff from and where we put stuff to now
646
647 for a mov.b @aa:16 -> mov.b @aa:8
648 opcode 0x6a 0x0y offset
649 -> 0x2y off
650 */
651 if (data[dst_address - 1] != 0x6a)
652 abort ();
653 switch (data[src_address] & 0xf0)
654 {
655 case 0x00:
656 /* Src is memory */
657 data[dst_address - 1] = (data[src_address] & 0xf) | 0x20;
658 break;
659 case 0x80:
660 /* Src is reg */
661 data[dst_address - 1] = (data[src_address] & 0xf) | 0x30;
662 break;
663 default:
664 abort ();
665 }
666
667 /* the offset must fit ! after all, what was all the relaxing
668 about ? */
669
4991ebb9
ILT
670 bfd_put_8 (abfd,
671 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
14aa9a78
ILT
672 data + dst_address);
673
674 /* Note the magic - src goes up by two bytes, but dst by only
675 one */
676 dst_address += 1;
677 src_address += 3;
678
679 break;
680
681 case R_JMP2:
682
c9f5444e 683 /* Special relaxed type */
14aa9a78 684 {
4991ebb9 685 bfd_vma dot = link_order->offset
14aa9a78 686 + dst_address
4991ebb9 687 + link_order->u.indirect.section->output_section->vma;
14aa9a78 688
4991ebb9
ILT
689 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
690 - dot - 1);
14aa9a78
ILT
691
692 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
693 abort ();
694
695 bfd_put_8 (abfd, gap, data + dst_address);
696
697 switch (data[dst_address - 1])
698 {
699 case 0x5e:
700 /* jsr -> bsr */
701 bfd_put_8 (abfd, 0x55, data + dst_address - 1);
702 break;
703 case 0x5a:
704 /* jmp ->bra */
705 bfd_put_8 (abfd, 0x40, data + dst_address - 1);
706 break;
707
708 default:
709 abort ();
710 }
711 dst_address++;
712 src_address += 3;
713
714 break;
715 }
716 break;
c9f5444e
JL
717
718 case R_PCRWORD_B:
719
720 /* Special relaxed type */
721 {
722 bfd_vma dot = link_order->offset
723 + dst_address
724 + link_order->u.indirect.section->output_section->vma - 2;
725
726 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
727 - dot - 1);
728
729 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
730 abort ();
731
732 switch (data[dst_address - 2])
733 {
734 int tmp;
735
736 case 0x58:
737 /* bCC:16 -> bCC:8 */
738 /* Get the condition code from the original insn. */
739 tmp = data[dst_address - 1];
740 tmp &= 0xf0;
741 tmp >>= 4;
742
743 /* Now or in the high nibble of the opcode. */
744 tmp |= 0x40;
745
746 /* Write it. */
747 bfd_put_8 (abfd, tmp, data + dst_address - 2);
748 break;
749
750 default:
751 abort ();
752 }
753
754 /* Output the target. */
755 bfd_put_8 (abfd, gap, data + dst_address - 1);
756
757 /* We don't advance dst_address -- the 8bit reloc is applied at
758 dst_address - 1, so the next insn should begin at dst_address.
759
760 src_address is advanced by two (original reloc was 16bits). */
761 src_address += 2;
762
763 break;
764 }
765 break;
14aa9a78
ILT
766
767 case R_JMPL_B8: /* 24 bit branch which is now 8 bits */
768
769 /* Speciial relaxed type */
770 {
4991ebb9 771 bfd_vma dot = link_order->offset
14aa9a78 772 + dst_address
4991ebb9 773 + link_order->u.indirect.section->output_section->vma;
14aa9a78 774
4991ebb9
ILT
775 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
776 - dot - 2);
14aa9a78
ILT
777
778 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
779 abort ();
780
781 switch (data[src_address])
782 {
783 case 0x5e:
784 /* jsr -> bsr */
785 bfd_put_8 (abfd, 0x55, data + dst_address);
786 break;
787 case 0x5a:
788 /* jmp ->bra */
789 bfd_put_8 (abfd, 0x40, data + dst_address);
790 break;
791
792 default:
793 bfd_put_8 (abfd, 0xde, data + dst_address);
794 break;
795 }
796
797 bfd_put_8 (abfd, gap, data + dst_address + 1);
798 dst_address += 2;
799 src_address += 4;
800
801 break;
802 }
803
804 case R_JMPL1:
805 {
4991ebb9 806 int v = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
14aa9a78
ILT
807 int o = bfd_get_32 (abfd, data + src_address);
808 v = (v & 0x00ffffff) | (o & 0xff000000);
809 bfd_put_32 (abfd, v, data + dst_address);
810 dst_address += 4;
811 src_address += 4;
812 }
813
814 break;
815
816
817 /* A 24 bit mov which could be an 8 bit move, really pointing to
818 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
819 case R_MOVLB1:
820 {
4991ebb9 821 int v = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
14aa9a78
ILT
822 int o = bfd_get_32 (abfd, data + dst_address);
823 v = (v & 0x00ffffff) | (o & 0xff000000);
824 bfd_put_32 (abfd, v, data + dst_address);
825 dst_address += 4;
826 src_address += 4;
827 }
828
829 break;
14aa9a78 830
39f27966
JL
831 /* An 8bit memory indirect instruction (jmp/jsr).
832
833 There's several things that need to be done to handle
834 this relocation.
835
836 If this is a reloc against the absolute symbol, then
837 we should handle it just R_RELBYTE. Likewise if it's
838 for a symbol with a value ge 0 and le 0xff.
839
840 Otherwise it's a jump/call through the function vector,
841 and the linker is expected to set up the function vector
842 and put the right value into the jump/call instruction. */
843 case R_MEM_INDIRECT:
844 {
845 /* We need to find the symbol so we can determine it's
846 address in the function vector table. */
847 asymbol *symbol;
848 bfd_vma value;
849 char *name;
850 struct funcvec_hash_entry *h;
851 asection *vectors_sec = h8300_coff_hash_table (link_info)->vectors_sec;
852
853 /* First see if this is a reloc against the absolute symbol
854 or against a symbol with a nonnegative value <= 0xff. */
855 symbol = *(reloc->sym_ptr_ptr);
856 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
857 if (symbol == bfd_abs_section_ptr->symbol
858 || (value >= 0 && value <= 0xff))
859 {
860 /* This should be handled in a manner very similar to
861 R_RELBYTES. If the value is in range, then just slam
862 the value into the right location. Else trigger a
863 reloc overflow callback. */
864 if (value >= 0 && value <= 0xff)
865 {
866 bfd_put_8 (abfd, value, data + dst_address);
867 dst_address += 1;
868 src_address += 1;
869 }
870 else
871 {
872 if (! ((*link_info->callbacks->reloc_overflow)
873 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
874 reloc->howto->name, reloc->addend, input_section->owner,
875 input_section, reloc->address)))
876 abort ();
877 }
878 break;
879 }
880
881 /* This is a jump/call through a function vector, and we're
882 expected to create the function vector ourselves.
883
884 First look up this symbol in the linker hash table -- we need
885 the derived linker symbol which holds this symbol's index
886 in the function vector. */
887 name = symbol->name;
888 if (symbol->flags & BSF_LOCAL)
889 {
890 char *new_name = bfd_malloc (strlen (name) + 9);
891 if (new_name == NULL)
892 abort ();
893
894 strcpy (new_name, name);
895 sprintf (new_name + strlen (name), "_%08x",
896 (int)symbol->section);
897 name = new_name;
898 }
899
900 h = funcvec_hash_lookup (h8300_coff_hash_table (link_info)->funcvec_hash_table,
901 name, false, false);
902
903 /* This shouldn't ever happen. If it does that means we've got
904 data corruption of some kind. Aborting seems like a reasonable
905 think to do here. */
906 if (h == NULL || vectors_sec == NULL)
907 abort ();
908
909 /* Place the address of the function vector entry into the
910 reloc's address. */
911 bfd_put_8 (abfd,
912 vectors_sec->output_offset + h->offset,
913 data + dst_address);
914
915 dst_address++;
916 src_address++;
917
918 /* Now create an entry in the function vector itself. */
919 if (bfd_get_mach (input_section->owner) == bfd_mach_h8300)
920 bfd_put_16 (abfd,
921 bfd_coff_reloc16_get_value (reloc,
922 link_info,
923 input_section),
924 vectors_sec->contents + h->offset);
925 else if (bfd_get_mach (input_section->owner) == bfd_mach_h8300h)
926 bfd_put_32 (abfd,
927 bfd_coff_reloc16_get_value (reloc,
928 link_info,
929 input_section),
930 vectors_sec->contents + h->offset);
931 else
932 abort ();
933
934 /* Gross. We've already written the contents of the vector section
935 before we get here... So we write it again with the new data. */
936 bfd_set_section_contents (vectors_sec->output_section->owner,
937 vectors_sec->output_section,
938 vectors_sec->contents,
939 vectors_sec->output_offset,
940 vectors_sec->_raw_size);
941 break;
942 }
943
944 default:
14aa9a78
ILT
945 abort ();
946 break;
947
948 }
39f27966 949
14aa9a78
ILT
950 *src_ptr = src_address;
951 *dst_ptr = dst_address;
39f27966
JL
952}
953
954
955/* Routine for the h8300 linker.
956
957 This routine is necessary to handle the special R_MEM_INDIRECT
958 relocs on the h8300. It's responsible for generating a vectors
959 section and attaching it to an input bfd as well as sizing
960 the vectors section. It also creates our vectors hash table.
961
962 It uses the generic linker routines to actually add the symbols.
963 from this BFD to the bfd linker hash table. It may add a few
964 selected static symbols to the bfd linker hash table. */
965
966static boolean
967h8300_bfd_link_add_symbols(abfd, info)
968 bfd *abfd;
969 struct bfd_link_info *info;
970{
971 asection *sec;
972 struct funcvec_hash_table *funcvec_hash_table;
973
974 /* If we haven't created a vectors section, do so now. */
975 if (!h8300_coff_hash_table (info)->vectors_sec)
976 {
977 flagword flags;
978
979 /* Make sure the appropriate flags are set, including SEC_IN_MEMORY. */
980 flags = (SEC_ALLOC | SEC_LOAD
981 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY);
982 h8300_coff_hash_table (info)->vectors_sec = bfd_make_section (abfd,
983 ".vectors");
984
985 /* If the section wasn't created, or we couldn't set the flags,
986 quit quickly now, rather than dieing a painful death later. */
987 if (! h8300_coff_hash_table (info)->vectors_sec
988 || ! bfd_set_section_flags (abfd,
989 h8300_coff_hash_table(info)->vectors_sec,
990 flags))
991 return false;
992
993 /* Also create the vector hash table. */
994 funcvec_hash_table = ((struct funcvec_hash_table *)
995 bfd_alloc (abfd, sizeof (struct funcvec_hash_table)));
996
997 if (!funcvec_hash_table)
998 return false;
999
1000 /* And initialize the funcvec hash table. */
1001 if (!funcvec_hash_table_init (funcvec_hash_table, abfd,
1002 funcvec_hash_newfunc))
1003 {
1004 bfd_release (abfd, funcvec_hash_table);
1005 return false;
1006 }
1007
1008 /* Store away a pointer to the funcvec hash table. */
1009 h8300_coff_hash_table (info)->funcvec_hash_table = funcvec_hash_table;
1010 }
1011
1012 /* Load up the function vector hash table. */
1013 funcvec_hash_table = h8300_coff_hash_table (info)->funcvec_hash_table;
1014
1015 /* Add the symbols using the generic code. */
1016 _bfd_generic_link_add_symbols (abfd, info);
1017
1018 /* Now scan the relocs for all the sections in this bfd; create
1019 additional space in the .vectors section as needed. */
1020 for (sec = abfd->sections; sec; sec = sec->next)
1021 {
1022 unsigned long reloc_size, reloc_count, i;
1023 asymbol **symbols;
1024 arelent **relocs;
1025
1026 /* Suck in the relocs, symbols & canonicalize them. */
1027 reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
1028 if (reloc_size <= 0)
1029 continue;
1030
1031 relocs = (arelent **)bfd_malloc ((size_t)reloc_size);
1032 if (!relocs)
1033 return false;
1034
1035 /* The symbols should have been read in by _bfd_generic link_add_symbols
1036 call abovec, so we can cheat and use the pointer to them that was
1037 saved in the above call. */
1038 symbols = _bfd_generic_link_get_symbols(abfd);
1039 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, symbols);
1040
1041 /* Now walk through all the relocations in this section. */
1042 for (i = 0; i < reloc_count; i++)
1043 {
1044 arelent *reloc = relocs[i];
1045 asymbol *symbol = *(reloc->sym_ptr_ptr);
1046 char *name;
1047
1048 /* We've got an indirect reloc. See if we need to add it
1049 to the function vector table. At this point, we have
1050 to add a new entry for each unique symbol referenced
1051 by an R_MEM_INDIRECT relocation except for a reloc
1052 against the absolute section symbol. */
1053 if (reloc->howto->type == R_MEM_INDIRECT
1054 && symbol != bfd_abs_section_ptr->symbol)
1055
1056 {
1057 struct funcvec_hash_entry *h;
1058
1059 name = symbol->name;
1060 if (symbol->flags & BSF_LOCAL)
1061 {
1062 char *new_name = bfd_malloc (strlen (name) + 9);
1063
1064 if (new_name == NULL)
1065 abort ();
1066
1067 strcpy (new_name, name);
1068 sprintf (new_name + strlen (name), "_%08x",
1069 (int)symbol->section);
1070 name = new_name;
1071 }
1072
1073 /* Look this symbol up in the function vector hash table. */
1074 h = funcvec_hash_lookup (h8300_coff_hash_table (info)->funcvec_hash_table,
1075 name, false, false);
1076
1077
1078 /* If this symbol isn't already in the hash table, add
1079 it and bump up the size of the hash table. */
1080 if (h == NULL)
1081 {
1082 h = funcvec_hash_lookup (h8300_coff_hash_table (info)->funcvec_hash_table,
1083 name, true, true);
1084 if (h == NULL)
1085 {
1086 free (relocs);
1087 return false;
1088 }
1089
1090 /* Bump the size of the vectors section. Each vector
1091 takes 2 bytes on the h8300 and 4 bytes on the h8300h. */
1092 if (bfd_get_mach (abfd) == bfd_mach_h8300)
1093 h8300_coff_hash_table (info)->vectors_sec->_raw_size += 2;
1094 else if (bfd_get_mach (abfd) == bfd_mach_h8300h)
1095 h8300_coff_hash_table (info)->vectors_sec->_raw_size += 4;
1096 }
1097 }
1098 }
1099
1100 /* We're done with the relocations, release them. */
1101 free (relocs);
1102 }
1103
1104 /* Now actually allocate some space for the function vector. It's
1105 wasteful to do this more than once, but this is easier. */
1106 if (h8300_coff_hash_table (info)->vectors_sec->_raw_size != 0)
1107 {
1108 /* Free the old contents. */
1109 if (h8300_coff_hash_table (info)->vectors_sec->contents)
1110 free (h8300_coff_hash_table (info)->vectors_sec->contents);
1111
1112 /* Allocate new contents. */
1113 h8300_coff_hash_table (info)->vectors_sec->contents
1114 = bfd_malloc (h8300_coff_hash_table (info)->vectors_sec->_raw_size);
1115 }
14aa9a78 1116
39f27966 1117 return true;
14aa9a78
ILT
1118}
1119
1120#define coff_reloc16_extra_cases h8300_reloc16_extra_cases
1121#define coff_reloc16_estimate h8300_reloc16_estimate
39f27966
JL
1122#define coff_bfd_link_add_symbols h8300_bfd_link_add_symbols
1123#define coff_bfd_link_hash_table_create h8300_coff_link_hash_table_create
14aa9a78 1124
69b799df 1125#define COFF_LONG_FILENAMES
b4e42a64
SC
1126#include "coffcode.h"
1127
1128
14aa9a78 1129#undef coff_bfd_get_relocated_section_contents
294eaca4 1130#undef coff_bfd_relax_section
4991ebb9
ILT
1131#define coff_bfd_get_relocated_section_contents \
1132 bfd_coff_reloc16_get_relocated_section_contents
14aa9a78
ILT
1133#define coff_bfd_relax_section bfd_coff_reloc16_relax_section
1134
1135
b4e42a64 1136
2f3508ad 1137const bfd_target h8300coff_vec =
b4e42a64 1138{
14aa9a78 1139 "coff-h8300", /* name */
b4e42a64 1140 bfd_target_coff_flavour,
d3e572fe
ILT
1141 BFD_ENDIAN_BIG, /* data byte order is big */
1142 BFD_ENDIAN_BIG, /* header byte order is big */
b4e42a64
SC
1143
1144 (HAS_RELOC | EXEC_P | /* object flags */
1145 HAS_LINENO | HAS_DEBUG |
4991ebb9 1146 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
14aa9a78
ILT
1147 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1148 '_', /* leading char */
b4e42a64
SC
1149 '/', /* ar_pad_char */
1150 15, /* ar_max_namelen */
14aa9a78
ILT
1151 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1152 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1153 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1154 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1155 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1156 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1157
1158 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1159 bfd_generic_archive_p, _bfd_dummy_target},
1160 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1161 bfd_false},
b4e42a64 1162 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
14aa9a78 1163 _bfd_write_archive_contents, bfd_false},
b4e42a64 1164
6812b607
ILT
1165 BFD_JUMP_TABLE_GENERIC (coff),
1166 BFD_JUMP_TABLE_COPY (coff),
1167 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1168 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1169 BFD_JUMP_TABLE_SYMBOLS (coff),
1170 BFD_JUMP_TABLE_RELOCS (coff),
1171 BFD_JUMP_TABLE_WRITE (coff),
1172 BFD_JUMP_TABLE_LINK (coff),
2f3508ad 1173 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
6812b607 1174
14aa9a78 1175 COFF_SWAP_TABLE,
b4e42a64 1176};
This page took 0.306677 seconds and 4 git commands to generate.