]> Git Repo - binutils.git/blob - bfd/coff-h8300.c
changes so far
[binutils.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, <[email protected]>.
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 "libbfd.h"
24 #include "obstack.h"
25 #include "coff/h8300.h"
26 #include "coff/internal.h"
27 #include "seclet.h"
28 #include "libcoff.h"
29 extern bfd_error_vector_type bfd_error_vector;
30
31 static reloc_howto_type howto_table[] =
32 {
33   HOWTO (R_RELBYTE, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "8", true, 0x000000ff, 0x000000ff, false),
34   HOWTO (R_RELWORD, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "16", true, 0x0000ffff, 0x0000ffff, false),
35   HOWTO (R_RELLONG, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "32", true, 0xffffffff, 0xffffffff, false),
36   HOWTO (R_PCRBYTE, 0, 0, 8, true, 0, complain_overflow_signed, 0, "DISP8", true, 0x000000ff, 0x000000ff, true),
37   HOWTO (R_PCRWORD, 0, 1, 16, true, 0, complain_overflow_signed, 0, "DISP16", true, 0x0000ffff, 0x0000ffff, true),
38   HOWTO (R_PCRLONG, 0, 2, 32, true, 0, complain_overflow_signed, 0, "DISP32", true, 0xffffffff, 0xffffffff, true),
39   HOWTO (R_MOVB1, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "16/8", true, 0x0000ffff, 0x0000ffff, false),
40   HOWTO (R_MOVB2, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "8/16", true, 0x0000ffff, 0x0000ffff, false),
41   HOWTO (R_JMP1, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "16/pcrel", true, 0x0000ffff, 0x0000ffff, false),
42   HOWTO (R_JMP2, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "pcrecl/16", true, 0x000000ff, 0x000000ff, false),
43
44
45   HOWTO (R_JMPL1, 0, 1, 32, false, 0, complain_overflow_bitfield, 0, "24/pcrell", true, 0x0000ffff, 0x0000ffff, false),
46   HOWTO (R_JMPL_B8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "pc8/24", true, 0x000000ff, 0x000000ff, false),
47
48   HOWTO (R_MOVLB1, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "24/8", true, 0x0000ffff, 0x0000ffff, false),
49   HOWTO (R_MOVLB2, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "8/24", true, 0x0000ffff, 0x0000ffff, false),
50
51 };
52
53
54 /* Turn a howto into a reloc number */
55
56 #define SELECT_RELOC(x,howto) \
57   { x = select_reloc(howto); }
58
59 #define BADMAG(x) (H8300BADMAG(x)&& H8300HBADMAG(x))
60 #define H8300 1                 /* Customize coffcode.h */
61 #define __A_MAGIC_SET__
62
63
64
65 /* Code to swap in the reloc */
66 #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
67 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
68 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
69   dst->r_stuff[0] = 'S'; \
70   dst->r_stuff[1] = 'C';
71
72
73 static int
74 select_reloc (howto)
75      reloc_howto_type *howto;
76 {
77   return howto->type;
78 }
79
80 /* Code to turn a r_type into a howto ptr, uses the above howto table
81    */
82
83 static void
84 rtype2howto (internal, dst)
85      arelent *internal;
86      struct internal_reloc *dst;
87 {
88   switch (dst->r_type)
89     {
90     case R_RELBYTE:
91       internal->howto = howto_table + 0;
92       break;
93     case R_RELWORD:
94       internal->howto = howto_table + 1;
95       break;
96     case R_RELLONG:
97       internal->howto = howto_table + 2;
98       break;
99     case R_PCRBYTE:
100       internal->howto = howto_table + 3;
101       break;
102     case R_PCRWORD:
103       internal->howto = howto_table + 4;
104       break;
105     case R_PCRLONG:
106       internal->howto = howto_table + 5;
107       break;
108     case R_MOVB1:
109       internal->howto = howto_table + 6;
110       break;
111     case R_MOVB2:
112       internal->howto = howto_table + 7;
113       break;
114     case R_JMP1:
115       internal->howto = howto_table + 8;
116       break;
117     case R_JMP2:
118       internal->howto = howto_table + 9;
119       break;
120     case R_JMPL1:
121       internal->howto = howto_table + 10;
122       break;
123     case R_JMPL_B8:
124       internal->howto = howto_table + 11;
125       break;
126     case R_MOVLB1:
127       internal->howto = howto_table + 12;
128       break;
129     case R_MOVLB2:
130       internal->howto = howto_table + 13;
131       break;
132     default:
133       fprintf (stderr, "Bad reloc\n");
134       break;
135     }
136 }
137
138 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
139
140
141 /* Perform any necessaru magic to the addend in a reloc entry */
142
143
144 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
145  cache_ptr->addend =  ext_reloc.r_offset;
146
147
148 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
149  reloc_processing(relent, reloc, symbols, abfd, section)
150
151 static void
152 DEFUN (reloc_processing, (relent, reloc, symbols, abfd, section),
153        arelent * relent AND
154        struct internal_reloc *reloc AND
155        asymbol ** symbols AND
156        bfd * abfd AND
157        asection * section)
158 {
159   relent->address = reloc->r_vaddr;
160   rtype2howto (relent, reloc);
161
162   if (((int) reloc->r_symndx) > 0)
163     {
164       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
165     }
166   else
167     {
168       relent->sym_ptr_ptr = &(bfd_abs_symbol);
169     }
170
171
172
173   relent->addend = reloc->r_offset;
174
175   relent->address -= section->vma;
176   /*  relent->section = 0;*/
177 }
178
179
180 static int
181 h8300_reloc16_estimate(input_section, symbols, reloc, shrink)
182      asection *input_section;
183      asymbol **symbols;
184      arelent *reloc;
185      unsigned int shrink;
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,0);
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, 0);
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, 0);
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, seclet, reloc, data, src_ptr, dst_ptr)
300      bfd *abfd;
301      struct bfd_seclet *seclet;
302      arelent *reloc;
303      bfd_byte *data;
304      unsigned int *src_ptr;
305      unsigned int *dst_ptr;
306 {
307   unsigned int src_address = *src_ptr;
308   unsigned int dst_address = *dst_ptr;
309
310   switch (reloc->howto->type)
311     {
312       /* A 24 bit branch which could be a 8 bit pcrel, really pointing to
313          the byte before the 24bit hole, so we can treat it as a 32bit pointer */
314     case R_PCRBYTE:
315       {
316         bfd_vma dot = seclet->offset 
317           + dst_address 
318             + seclet->u.indirect.section->output_section->vma;
319         int gap = bfd_coff_reloc16_get_value (reloc, seclet) - dot;
320         if (gap > 127 || gap < -128)
321           {
322             bfd_error_vector.reloc_value_truncated (reloc, seclet);
323           }
324
325         bfd_put_8 (abfd, gap, data + dst_address);
326         dst_address++;
327         src_address++;
328
329         break;
330       }
331
332     case R_RELBYTE:
333       {
334         unsigned int gap = bfd_coff_reloc16_get_value (reloc, seclet);
335         if (gap > 0xff && gap < ~0xff)
336           {
337             bfd_error_vector.reloc_value_truncated (reloc, seclet);
338           }
339
340         bfd_put_8 (abfd, gap, data + dst_address);
341         dst_address += 1;
342         src_address += 1;
343
344
345       }
346       break;
347     case R_JMP1:
348       /* A relword which would have like to have been a pcrel */
349     case R_MOVB1:
350       /* A relword which would like to have been modified but
351              didn't make it */
352     case R_RELWORD:
353       bfd_put_16 (abfd, bfd_coff_reloc16_get_value (reloc, seclet),
354                   data + dst_address);
355       dst_address += 2;
356       src_address += 2;
357       break;
358     case R_RELLONG:
359       bfd_put_32 (abfd, bfd_coff_reloc16_get_value (reloc, seclet),
360                   data + dst_address);
361       dst_address += 4;
362       src_address += 4;
363       break;
364
365     case R_MOVB2:
366       /* Special relaxed type, there will be a gap between where we
367              get stuff from and where we put stuff to now
368         
369              for a mov.b @aa:16 -> mov.b @aa:8
370              opcode 0x6a 0x0y offset
371              ->     0x2y off
372              */
373       if (data[dst_address - 1] != 0x6a)
374         abort ();
375       switch (data[src_address] & 0xf0)
376         {
377         case 0x00:
378           /* Src is memory */
379           data[dst_address - 1] = (data[src_address] & 0xf) | 0x20;
380           break;
381         case 0x80:
382           /* Src is reg */
383           data[dst_address - 1] = (data[src_address] & 0xf) | 0x30;
384           break;
385         default:
386           abort ();
387         }
388
389       /* the offset must fit ! after all, what was all the relaxing
390              about ? */
391
392       bfd_put_8 (abfd, bfd_coff_reloc16_get_value (reloc, seclet),
393                  data + dst_address);
394
395       /* Note the magic - src goes up by two bytes, but dst by only
396              one */
397       dst_address += 1;
398       src_address += 3;
399
400       break;
401
402     case R_JMP2:
403       
404       /* Speciial relaxed type */
405       {
406         bfd_vma dot = seclet->offset
407         + dst_address
408         + seclet->u.indirect.section->output_section->vma;
409
410         int gap = bfd_coff_reloc16_get_value (reloc, seclet) - dot - 1;
411
412         if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
413           abort ();
414
415         bfd_put_8 (abfd, gap, data + dst_address);
416
417         switch (data[dst_address - 1])
418           {
419           case 0x5e:
420             /* jsr -> bsr */
421             bfd_put_8 (abfd, 0x55, data + dst_address - 1);
422             break;
423           case 0x5a:
424             /* jmp ->bra */
425             bfd_put_8 (abfd, 0x40, data + dst_address - 1);
426             break;
427
428           default:
429             abort ();
430           }
431         dst_address++;
432         src_address += 3;
433
434         break;
435       }
436       break;
437       
438     case R_JMPL_B8: /* 24 bit branch which is now 8 bits */
439       
440       /* Speciial relaxed type */
441       {
442         bfd_vma dot = seclet->offset
443         + dst_address
444         + seclet->u.indirect.section->output_section->vma;
445
446         int gap = bfd_coff_reloc16_get_value (reloc, seclet) - dot - 2;
447
448         if ((gap & ~0xff) != 0 && ((gap & 0xff00) != 0xff00))
449           abort ();
450
451         switch (data[src_address])
452           {
453           case 0x5e:
454             /* jsr -> bsr */
455             bfd_put_8 (abfd, 0x55, data + dst_address);
456             break;
457           case 0x5a:
458             /* jmp ->bra */
459             bfd_put_8 (abfd, 0x40, data + dst_address);
460             break;
461
462           default:
463             bfd_put_8 (abfd, 0xde, data + dst_address);
464             break;
465           }
466
467         bfd_put_8 (abfd, gap, data + dst_address + 1);
468         dst_address += 2;
469         src_address += 4;
470
471         break;
472       }
473
474     case R_JMPL1:
475       {
476         int v = bfd_coff_reloc16_get_value (reloc, seclet);
477         int o = bfd_get_32 (abfd, data + src_address);
478         v = (v & 0x00ffffff) | (o & 0xff000000);
479         bfd_put_32 (abfd, v, data + dst_address);
480         dst_address += 4;
481         src_address += 4;
482       }
483
484       break;
485
486
487       /* A 24 bit mov  which could be an 8 bit move, really pointing to
488          the byte before the 24bit hole, so we can treat it as a 32bit pointer */
489     case R_MOVLB1:
490       {
491         int v = bfd_coff_reloc16_get_value (reloc, seclet);
492         int o = bfd_get_32 (abfd, data + dst_address);
493         v = (v & 0x00ffffff) | (o & 0xff000000);
494         bfd_put_32 (abfd, v, data + dst_address);
495         dst_address += 4;
496         src_address += 4;
497       }
498
499       break;
500     default:
501
502       abort ();
503       break;
504
505     }
506   *src_ptr = src_address;
507   *dst_ptr = dst_address;
508
509 }
510
511 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
512 #define coff_reloc16_estimate h8300_reloc16_estimate
513
514 #include "coffcode.h"
515
516
517 #undef coff_bfd_get_relocated_section_contents
518 #undef coff_bfd_relax_section
519 #define  coff_bfd_get_relocated_section_contents bfd_coff_reloc16_get_relocated_section_contents
520 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
521
522
523
524 bfd_target h8300coff_vec =
525 {
526   "coff-h8300",                 /* name */
527   bfd_target_coff_flavour,
528   true,                         /* data byte order is big */
529   true,                         /* header byte order is big */
530
531   (HAS_RELOC | EXEC_P |         /* object flags */
532    HAS_LINENO | HAS_DEBUG |
533    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
534   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),        /* section flags */
535   '_',                          /* leading char */
536   '/',                          /* ar_pad_char */
537   15,                           /* ar_max_namelen */
538   1,                            /* minimum section alignment */
539   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
540   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
541   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
542   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
543   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
544   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
545
546   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
547    bfd_generic_archive_p, _bfd_dummy_target},
548   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
549    bfd_false},
550   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
551    _bfd_write_archive_contents, bfd_false},
552
553   JUMP_TABLE (coff),
554   COFF_SWAP_TABLE,
555 };
This page took 0.059133 seconds and 4 git commands to generate.