removed rcs cruft
[deliverable/binutils-gdb.git] / bfd / coff-h8300.c
1 /* BFD back-end for Hitachi H8/300 COFF binaries.
2 Copyright 1990, 1991, 1992, 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "obstack.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "coff/h8300.h"
27 #include "coff/internal.h"
28 #include "libcoff.h"
29
30 static reloc_howto_type howto_table[] =
31 {
32 HOWTO (R_RELBYTE, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "8", true, 0x000000ff, 0x000000ff, false),
33 HOWTO (R_RELWORD, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "16", true, 0x0000ffff, 0x0000ffff, false),
34 HOWTO (R_RELLONG, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "32", true, 0xffffffff, 0xffffffff, false),
35 HOWTO (R_PCRBYTE, 0, 0, 8, true, 0, complain_overflow_signed, 0, "DISP8", true, 0x000000ff, 0x000000ff, true),
36 HOWTO (R_PCRWORD, 0, 1, 16, true, 0, complain_overflow_signed, 0, "DISP16", true, 0x0000ffff, 0x0000ffff, true),
37 HOWTO (R_PCRLONG, 0, 2, 32, true, 0, complain_overflow_signed, 0, "DISP32", true, 0xffffffff, 0xffffffff, true),
38 HOWTO (R_MOVB1, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "16/8", true, 0x0000ffff, 0x0000ffff, false),
39 HOWTO (R_MOVB2, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "8/16", true, 0x0000ffff, 0x0000ffff, false),
40 HOWTO (R_JMP1, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "16/pcrel", true, 0x0000ffff, 0x0000ffff, false),
41 HOWTO (R_JMP2, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "pcrecl/16", true, 0x000000ff, 0x000000ff, false),
42
43
44 HOWTO (R_JMPL1, 0, 1, 32, false, 0, complain_overflow_bitfield, 0, "24/pcrell", true, 0x0000ffff, 0x0000ffff, false),
45 HOWTO (R_JMPL_B8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "pc8/24", true, 0x000000ff, 0x000000ff, false),
46
47 HOWTO (R_MOVLB1, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "24/8", true, 0x0000ffff, 0x0000ffff, false),
48 HOWTO (R_MOVLB2, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "8/24", true, 0x0000ffff, 0x0000ffff, false),
49
50 };
51
52
53 /* Turn a howto into a reloc number */
54
55 #define SELECT_RELOC(x,howto) \
56 { x = select_reloc(howto); }
57
58 #define BADMAG(x) (H8300BADMAG(x)&& H8300HBADMAG(x))
59 #define H8300 1 /* Customize coffcode.h */
60 #define __A_MAGIC_SET__
61
62
63
64 /* Code to swap in the reloc */
65 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
66 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
67 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
68 dst->r_stuff[0] = 'S'; \
69 dst->r_stuff[1] = 'C';
70
71
72 static int
73 select_reloc (howto)
74 reloc_howto_type *howto;
75 {
76 return howto->type;
77 }
78
79 /* Code to turn a r_type into a howto ptr, uses the above howto table
80 */
81
82 static void
83 rtype2howto (internal, dst)
84 arelent *internal;
85 struct internal_reloc *dst;
86 {
87 switch (dst->r_type)
88 {
89 case R_RELBYTE:
90 internal->howto = howto_table + 0;
91 break;
92 case R_RELWORD:
93 internal->howto = howto_table + 1;
94 break;
95 case R_RELLONG:
96 internal->howto = howto_table + 2;
97 break;
98 case R_PCRBYTE:
99 internal->howto = howto_table + 3;
100 break;
101 case R_PCRWORD:
102 internal->howto = howto_table + 4;
103 break;
104 case R_PCRLONG:
105 internal->howto = howto_table + 5;
106 break;
107 case R_MOVB1:
108 internal->howto = howto_table + 6;
109 break;
110 case R_MOVB2:
111 internal->howto = howto_table + 7;
112 break;
113 case R_JMP1:
114 internal->howto = howto_table + 8;
115 break;
116 case R_JMP2:
117 internal->howto = howto_table + 9;
118 break;
119 case R_JMPL1:
120 internal->howto = howto_table + 10;
121 break;
122 case R_JMPL_B8:
123 internal->howto = howto_table + 11;
124 break;
125 case R_MOVLB1:
126 internal->howto = howto_table + 12;
127 break;
128 case R_MOVLB2:
129 internal->howto = howto_table + 13;
130 break;
131 default:
132 fprintf (stderr, "Bad reloc\n");
133 break;
134 }
135 }
136
137 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
138
139
140 /* Perform any necessaru magic to the addend in a reloc entry */
141
142
143 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
144 cache_ptr->addend = ext_reloc.r_offset;
145
146
147 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
148 reloc_processing(relent, reloc, symbols, abfd, section)
149
150 static void
151 DEFUN (reloc_processing, (relent, reloc, symbols, abfd, section),
152 arelent * relent AND
153 struct internal_reloc *reloc AND
154 asymbol ** symbols AND
155 bfd * abfd AND
156 asection * section)
157 {
158 relent->address = reloc->r_vaddr;
159 rtype2howto (relent, reloc);
160
161 if (((int) reloc->r_symndx) > 0)
162 {
163 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
164 }
165 else
166 {
167 relent->sym_ptr_ptr = &(bfd_abs_symbol);
168 }
169
170
171
172 relent->addend = reloc->r_offset;
173
174 relent->address -= section->vma;
175 /* relent->section = 0;*/
176 }
177
178
179 static int
180 h8300_reloc16_estimate(input_section, symbols, reloc, shrink, link_info)
181 asection *input_section;
182 asymbol **symbols;
183 arelent *reloc;
184 unsigned int shrink;
185 struct bfd_link_info *link_info;
186 {
187 bfd_vma value;
188 bfd_vma dot;
189 bfd_vma gap;
190
191 /* The address of the thing to be relocated will have moved back by
192 the size of the shrink - but we don't change reloc->address here,
193 since we need it to know where the relocation lives in the source
194 uncooked section */
195
196 /* reloc->address -= shrink; conceptual */
197
198 bfd_vma address = reloc->address - shrink;
199
200
201 switch (reloc->howto->type)
202 {
203 case R_MOVB2:
204 case R_JMP2:
205 shrink+=2;
206 break;
207
208 /* Thing is a move one byte */
209 case R_MOVB1:
210 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
211
212 if (value >= 0xff00)
213 {
214
215 /* Change the reloc type from 16bit, possible 8 to 8bit
216 possible 16 */
217 reloc->howto = reloc->howto + 1;
218 /* The place to relc moves back by one */
219 /* This will be two bytes smaller in the long run */
220 shrink +=2 ;
221 bfd_perform_slip(symbols, 2, input_section, address);
222 }
223
224 break;
225 /* This is the 24 bit branch which could become an 8 bitter,
226 the relocation points to the first byte of the insn, not the
227 actual data */
228
229 case R_JMPL1:
230 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
231
232 dot = input_section->output_section->vma +
233 input_section->output_offset + address;
234
235 /* See if the address we're looking at within 127 bytes of where
236 we are, if so then we can use a small branch rather than the
237 jump we were going to */
238
239 gap = value - dot ;
240
241 if (-120 < (long)gap && (long)gap < 120 )
242 {
243
244 /* Change the reloc type from 24bit, possible 8 to 8bit
245 possible 32 */
246 reloc->howto = reloc->howto + 1;
247 /* This will be two bytes smaller in the long run */
248 shrink +=2 ;
249 bfd_perform_slip(symbols, 2, input_section, address);
250 }
251 break;
252
253 case R_JMP1:
254
255 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
256
257 dot = input_section->output_section->vma +
258 input_section->output_offset + address;
259
260 /* See if the address we're looking at within 127 bytes of where
261 we are, if so then we can use a small branch rather than the
262 jump we were going to */
263
264 gap = value - (dot - shrink);
265
266
267 if (-120 < (long)gap && (long)gap < 120 )
268 {
269
270 /* Change the reloc type from 16bit, possible 8 to 8bit
271 possible 16 */
272 reloc->howto = reloc->howto + 1;
273 /* The place to relc moves back by one */
274
275 /* This will be two bytes smaller in the long run */
276 shrink +=2 ;
277 bfd_perform_slip(symbols, 2, input_section, address);
278 }
279 break;
280 }
281
282
283 return shrink;
284 }
285
286
287 /* First phase of a relaxing link */
288
289 /* Reloc types
290 large small
291 R_MOVB1 R_MOVB2 mov.b with 16bit or 8 bit address
292 R_JMP1 R_JMP2 jmp or pcrel branch
293 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
294 R_MOVLB1 R_MOVLB2 24 or 8 bit reloc for mov.b
295
296 */
297
298 static void
299 h8300_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
300 dst_ptr)
301 bfd *abfd;
302 struct bfd_link_info *link_info;
303 struct bfd_link_order *link_order;
304 arelent *reloc;
305 bfd_byte *data;
306 unsigned int *src_ptr;
307 unsigned int *dst_ptr;
308 {
309 unsigned int src_address = *src_ptr;
310 unsigned int dst_address = *dst_ptr;
311 asection *input_section = link_order->u.indirect.section;
312
313 switch (reloc->howto->type)
314 {
315 /* A 24 bit branch which could be a 8 bit pcrel, really pointing to
316 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
317 case R_PCRBYTE:
318 {
319 bfd_vma dot = link_order->offset
320 + dst_address
321 + link_order->u.indirect.section->output_section->vma;
322 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
323 - dot);
324 if (gap > 127 || gap < -128)
325 {
326 if (! ((*link_info->callbacks->reloc_overflow)
327 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
328 reloc->howto->name, reloc->addend, input_section->owner,
329 input_section, reloc->address)))
330 abort ();
331 }
332
333 bfd_put_8 (abfd, gap, data + dst_address);
334 dst_address++;
335 src_address++;
336
337 break;
338 }
339
340 case R_RELBYTE:
341 {
342 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
343 input_section);
344 if (gap > 0xff && gap < ~0xff)
345 {
346 if (! ((*link_info->callbacks->reloc_overflow)
347 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
348 reloc->howto->name, reloc->addend, input_section->owner,
349 input_section, reloc->address)))
350 abort ();
351 }
352
353 bfd_put_8 (abfd, gap, data + dst_address);
354 dst_address += 1;
355 src_address += 1;
356
357
358 }
359 break;
360 case R_JMP1:
361 /* A relword which would have like to have been a pcrel */
362 case R_MOVB1:
363 /* A relword which would like to have been modified but
364 didn't make it */
365 case R_RELWORD:
366 bfd_put_16 (abfd,
367 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
368 data + dst_address);
369 dst_address += 2;
370 src_address += 2;
371 break;
372 case R_RELLONG:
373 bfd_put_32 (abfd,
374 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
375 data + dst_address);
376 dst_address += 4;
377 src_address += 4;
378 break;
379
380 case R_MOVB2:
381 /* Special relaxed type, there will be a gap between where we
382 get stuff from and where we put stuff to now
383
384 for a mov.b @aa:16 -> mov.b @aa:8
385 opcode 0x6a 0x0y offset
386 -> 0x2y off
387 */
388 if (data[dst_address - 1] != 0x6a)
389 abort ();
390 switch (data[src_address] & 0xf0)
391 {
392 case 0x00:
393 /* Src is memory */
394 data[dst_address - 1] = (data[src_address] & 0xf) | 0x20;
395 break;
396 case 0x80:
397 /* Src is reg */
398 data[dst_address - 1] = (data[src_address] & 0xf) | 0x30;
399 break;
400 default:
401 abort ();
402 }
403
404 /* the offset must fit ! after all, what was all the relaxing
405 about ? */
406
407 bfd_put_8 (abfd,
408 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
409 data + dst_address);
410
411 /* Note the magic - src goes up by two bytes, but dst by only
412 one */
413 dst_address += 1;
414 src_address += 3;
415
416 break;
417
418 case R_JMP2:
419
420 /* Speciial relaxed type */
421 {
422 bfd_vma dot = link_order->offset
423 + dst_address
424 + link_order->u.indirect.section->output_section->vma;
425
426 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
427 - dot - 1);
428
429 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
430 abort ();
431
432 bfd_put_8 (abfd, gap, data + dst_address);
433
434 switch (data[dst_address - 1])
435 {
436 case 0x5e:
437 /* jsr -> bsr */
438 bfd_put_8 (abfd, 0x55, data + dst_address - 1);
439 break;
440 case 0x5a:
441 /* jmp ->bra */
442 bfd_put_8 (abfd, 0x40, data + dst_address - 1);
443 break;
444
445 default:
446 abort ();
447 }
448 dst_address++;
449 src_address += 3;
450
451 break;
452 }
453 break;
454
455 case R_JMPL_B8: /* 24 bit branch which is now 8 bits */
456
457 /* Speciial relaxed type */
458 {
459 bfd_vma dot = link_order->offset
460 + dst_address
461 + link_order->u.indirect.section->output_section->vma;
462
463 int gap = (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
464 - dot - 2);
465
466 if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
467 abort ();
468
469 switch (data[src_address])
470 {
471 case 0x5e:
472 /* jsr -> bsr */
473 bfd_put_8 (abfd, 0x55, data + dst_address);
474 break;
475 case 0x5a:
476 /* jmp ->bra */
477 bfd_put_8 (abfd, 0x40, data + dst_address);
478 break;
479
480 default:
481 bfd_put_8 (abfd, 0xde, data + dst_address);
482 break;
483 }
484
485 bfd_put_8 (abfd, gap, data + dst_address + 1);
486 dst_address += 2;
487 src_address += 4;
488
489 break;
490 }
491
492 case R_JMPL1:
493 {
494 int v = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
495 int o = bfd_get_32 (abfd, data + src_address);
496 v = (v & 0x00ffffff) | (o & 0xff000000);
497 bfd_put_32 (abfd, v, data + dst_address);
498 dst_address += 4;
499 src_address += 4;
500 }
501
502 break;
503
504
505 /* A 24 bit mov which could be an 8 bit move, really pointing to
506 the byte before the 24bit hole, so we can treat it as a 32bit pointer */
507 case R_MOVLB1:
508 {
509 int v = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
510 int o = bfd_get_32 (abfd, data + dst_address);
511 v = (v & 0x00ffffff) | (o & 0xff000000);
512 bfd_put_32 (abfd, v, data + dst_address);
513 dst_address += 4;
514 src_address += 4;
515 }
516
517 break;
518 default:
519
520 abort ();
521 break;
522
523 }
524 *src_ptr = src_address;
525 *dst_ptr = dst_address;
526
527 }
528
529 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
530 #define coff_reloc16_estimate h8300_reloc16_estimate
531
532 #include "coffcode.h"
533
534
535 #undef coff_bfd_get_relocated_section_contents
536 #undef coff_bfd_relax_section
537 #define coff_bfd_get_relocated_section_contents \
538 bfd_coff_reloc16_get_relocated_section_contents
539 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
540
541
542
543 bfd_target h8300coff_vec =
544 {
545 "coff-h8300", /* name */
546 bfd_target_coff_flavour,
547 true, /* data byte order is big */
548 true, /* header byte order is big */
549
550 (HAS_RELOC | EXEC_P | /* object flags */
551 HAS_LINENO | HAS_DEBUG |
552 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
553 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
554 '_', /* leading char */
555 '/', /* ar_pad_char */
556 15, /* ar_max_namelen */
557 1, /* minimum section alignment */
558 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
559 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
560 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
561 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
562 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
563 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
564
565 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
566 bfd_generic_archive_p, _bfd_dummy_target},
567 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
568 bfd_false},
569 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
570 _bfd_write_archive_contents, bfd_false},
571
572 JUMP_TABLE (coff),
573 COFF_SWAP_TABLE,
574 };
This page took 0.042999 seconds and 4 git commands to generate.