e8e3a5ee5884a8dd80266db1ba4c14b2cd3d8604
[external/binutils.git] / bfd / coff-h8500.c
1 /* BFD back-end for Hitachi H8/500 COFF binaries.
2    Copyright 1993 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4    Written by Steve Chamberlain, <sac@cygnus.com>.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "obstack.h"
26 #include "coff/h8500.h"
27 #include "coff/internal.h"
28 #include "libcoff.h"
29 #include "seclet.h"
30
31 extern bfd_error_vector_type bfd_error_vector;
32
33 static reloc_howto_type r_imm8 =
34 HOWTO (R_H8500_IMM8, 0, 1, 8, false, 0, true,
35        true, 0, "r_imm8", true, 0x000000ff, 0x000000ff, false);
36
37 static reloc_howto_type r_imm16 =
38 HOWTO (R_H8500_IMM16, 0, 1, 16, false, 0, true,
39        true, 0, "r_imm16", true, 0x0000ffff, 0x0000ffff, false);
40
41 static reloc_howto_type r_imm24 =
42 HOWTO (R_H8500_IMM24, 0, 1, 24, false, 0, true,
43        true, 0, "r_imm24", true, 0x00ffffff, 0x00ffffff, false);
44
45 static reloc_howto_type r_imm32 =
46 HOWTO (R_H8500_IMM32, 0, 1, 32, false, 0, true,
47        true, 0, "r_imm32", true, 0xffffffff, 0xffffffff, false);
48
49
50 static reloc_howto_type r_high8 =
51 HOWTO (R_H8500_HIGH8, 0, 1, 8, false, 0, true,
52        true, 0, "r_high8", true, 0x000000ff, 0x000000ff, false);
53
54 static reloc_howto_type r_low16 =
55 HOWTO (R_H8500_LOW16, 0, 1, 16, false, 0, true,
56        true, 0, "r_low16", true, 0x0000ffff, 0x0000ffff, false);
57
58 static reloc_howto_type r_pcrel8 =
59 HOWTO (R_H8500_PCREL8, 0, 1, 8, true, 0, true, true, 0, "r_pcrel8", true, 0, 0, true);
60
61
62 static reloc_howto_type r_pcrel16 =
63 HOWTO (R_H8500_PCREL16, 0, 1, 16, true, 0, true, true, 0, "r_pcrel16", true, 0, 0, true);
64
65 static reloc_howto_type r_high16 =
66 HOWTO (R_H8500_HIGH16, 0, 1, 8, false, 0, true,
67        true, 0, "r_high16", true, 0x000ffff, 0x0000ffff, false);
68
69
70 /* Turn a howto into a reloc number */
71
72 static int 
73 coff_h8500_select_reloc (howto)
74      reloc_howto_type *howto;
75 {
76   return howto->type;
77 }
78
79 #define SELECT_RELOC(x,howto) x= coff_h8500_select_reloc(howto)
80
81
82 #define BADMAG(x) H8500BADMAG(x)
83 #define H8500 1                 /* Customize coffcode.h */
84
85 #define __A_MAGIC_SET__
86
87 /* Code to swap in the reloc */
88 #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
89 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
90 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
91   dst->r_stuff[0] = 'S'; \
92   dst->r_stuff[1] = 'C';
93
94 /* Code to turn a r_type into a howto ptr, uses the above howto table
95    */
96
97 static void
98 rtype2howto(internal, dst)
99      arelent * internal;
100      struct internal_reloc *dst;
101 {
102   switch (dst->r_type)
103     {
104     default:
105       printf ("BAD 0x%x\n", dst->r_type);
106     case R_H8500_IMM8:
107       internal->howto = &r_imm8;
108       break;
109     case R_H8500_IMM16:
110       internal->howto = &r_imm16;
111       break;
112     case R_H8500_IMM24:
113       internal->howto = &r_imm24;
114       break;
115     case R_H8500_IMM32:
116       internal->howto = &r_imm32;
117       break;
118     case R_H8500_PCREL8:
119       internal->howto = &r_pcrel8;
120       break;
121     case R_H8500_PCREL16:
122       internal->howto = &r_pcrel16;
123       break;
124     case R_H8500_HIGH8:
125       internal->howto = &r_high8;
126       break;
127     case R_H8500_HIGH16:
128       internal->howto = &r_high16;
129       break;
130     case R_H8500_LOW16:
131       internal->howto = &r_low16;
132       break;
133     }
134 }
135
136 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
137
138
139 /* Perform any necessaru magic to the addend in a reloc entry */
140
141
142 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
143  cache_ptr->addend =  ext_reloc.r_offset;
144
145
146 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
147  reloc_processing(relent, reloc, symbols, abfd, section)
148
149 static void reloc_processing (relent, reloc, symbols, abfd, section)
150      arelent * relent;
151      struct internal_reloc *reloc;
152      asymbol ** symbols;
153      bfd * abfd;
154      asection * section;
155 {
156   relent->address = reloc->r_vaddr;
157   rtype2howto (relent, reloc);
158
159   if (reloc->r_symndx > 0)
160     {
161       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
162     }
163   else
164     {
165       relent->sym_ptr_ptr = &(bfd_abs_symbol);
166     }
167
168
169   relent->addend = reloc->r_offset;
170   relent->address -= section->vma;
171 }
172
173 static void
174 extra_case (in_abfd, seclet, reloc, data, src_ptr, dst_ptr)
175      bfd *in_abfd;
176      bfd_seclet_type *seclet;
177      arelent *reloc;
178      bfd_byte *data;
179      unsigned int *src_ptr;
180      unsigned int *dst_ptr;
181 {
182   bfd_byte *d = data+*dst_ptr;
183   switch (reloc->howto->type)
184     {
185     case R_H8500_IMM8:
186       bfd_put_8 (in_abfd,
187                  bfd_coff_reloc16_get_value (reloc, seclet),
188                  d);
189       (*dst_ptr) += 1;
190       (*src_ptr) += 1;
191       break;
192
193     case R_H8500_HIGH8:
194       bfd_put_8 (in_abfd,
195                  (bfd_coff_reloc16_get_value (reloc, seclet)>>16),
196                  d );
197       (*dst_ptr) += 1;
198       (*src_ptr) += 1;
199       break;
200
201     case R_H8500_IMM16:
202       bfd_put_16 (in_abfd,
203                   bfd_coff_reloc16_get_value (reloc, seclet) ,
204                   d  );
205       (*dst_ptr) += 2;
206       (*src_ptr) += 2;
207       break;
208
209     case R_H8500_LOW16:
210       bfd_put_16 (in_abfd,
211                   bfd_coff_reloc16_get_value (reloc, seclet) ,
212                   d);
213
214       (*dst_ptr) += 2;
215       (*src_ptr) += 2;
216       break;
217       
218     case R_H8500_HIGH16:
219       bfd_put_16 (in_abfd,
220                   bfd_coff_reloc16_get_value (reloc, seclet)>>16 ,
221                   d);
222
223       (*dst_ptr) += 2;
224       (*src_ptr) += 2;
225       break;
226
227     case R_H8500_IMM24:
228       {
229         int v = bfd_coff_reloc16_get_value(reloc, seclet);
230         int o = bfd_get_32(in_abfd, data+ *dst_ptr -1);
231         v = (v & 0x00ffffff) | (o & 0xff00000);
232         bfd_put_32 (in_abfd, v, data  + *dst_ptr -1);
233         (*dst_ptr) +=3;
234         (*src_ptr)+=3;;
235       }
236       break;
237     case R_H8500_IMM32:
238       {
239         int v = bfd_coff_reloc16_get_value(reloc, seclet);
240         bfd_put_32 (in_abfd, v, data  + *dst_ptr);
241         (*dst_ptr) +=4;
242         (*src_ptr)+=4;;
243       }
244       break;
245
246
247     case R_H8500_PCREL8:
248       {
249         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, seclet);
250         bfd_vma dot = seclet->offset
251           + *dst_ptr
252             + seclet->u.indirect.section->output_section->vma;
253         int gap = dst - dot - 1; /* -1 since were in the odd byte of the
254                                     word and the pc's been incremented */
255
256         if (gap > 128 || gap < -128)
257           {
258             bfd_error_vector.reloc_value_truncated (reloc, seclet);
259           }
260         bfd_put_8 (in_abfd, gap, data + *dst_ptr);
261         (*dst_ptr)++;
262         (*src_ptr)++;
263         break;
264       }
265     case R_H8500_PCREL16:
266       {
267         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, seclet);
268         bfd_vma dot = seclet->offset
269           + *dst_ptr
270             + seclet->u.indirect.section->output_section->vma;
271         int gap = dst - dot - 1; /* -1 since were in the odd byte of the
272                                     word and the pc's been incremented */
273
274         if (gap > 32767 || gap < -32768)
275           {
276             bfd_error_vector.reloc_value_truncated (reloc, seclet);
277           }
278         bfd_put_16 (in_abfd, gap, data + *dst_ptr);
279         (*dst_ptr)+=2;
280         (*src_ptr)+=2;
281         break;
282       }
283
284     default:
285       abort ();
286     }
287 }
288
289 #define coff_reloc16_extra_cases extra_case
290
291 #include "coffcode.h"
292
293
294 #undef  coff_bfd_get_relocated_section_contents
295 #undef coff_bfd_relax_section
296 #define  coff_bfd_get_relocated_section_contents bfd_coff_reloc16_get_relocated_section_contents
297 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
298
299 bfd_target h8500coff_vec =
300 {
301   "coff-h8500",                 /* name */
302   bfd_target_coff_flavour,
303   true,                         /* data byte order is big */
304   true,                         /* header byte order is big */
305
306   (HAS_RELOC | EXEC_P |         /* object flags */
307    HAS_LINENO | HAS_DEBUG |
308    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
309
310   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),        /* section flags */
311   '_',                          /* leading symbol underscore */
312   '/',                          /* ar_pad_char */
313   15,                           /* ar_max_namelen */
314   1,                            /* minimum section alignment */
315   _do_getb64, _do_getb_signed_64, _do_putb64,
316      _do_getb32, _do_getb_signed_32, _do_putb32,
317      _do_getb16, _do_getb_signed_16, _do_putb16,        /* data */
318   _do_getb64, _do_getb_signed_64, _do_putb64,
319      _do_getb32, _do_getb_signed_32, _do_putb32,
320      _do_getb16, _do_getb_signed_16, _do_putb16,        /* hdrs */
321
322   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
323    bfd_generic_archive_p, _bfd_dummy_target},
324   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
325    bfd_false},
326   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
327    _bfd_write_archive_contents, bfd_false},
328
329   JUMP_TABLE (coff),
330   COFF_SWAP_TABLE,
331 };