* Many files: change all bfd_target vectors to be const. Change
[platform/upstream/binutils.git] / bfd / coff-h8500.c
1 /* BFD back-end for Hitachi H8/500 COFF binaries.
2    Copyright 1993, 1994 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 "obstack.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27 #include "coff/h8500.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30
31 static reloc_howto_type r_imm8 =
32 HOWTO (R_H8500_IMM8, 0, 1, 8, false, 0,
33        complain_overflow_bitfield, 0, "r_imm8", true, 0x000000ff, 0x000000ff, false);
34
35 static reloc_howto_type r_imm16 =
36 HOWTO (R_H8500_IMM16, 0, 1, 16, false, 0,
37        complain_overflow_bitfield, 0, "r_imm16", true, 0x0000ffff, 0x0000ffff, false);
38
39 static reloc_howto_type r_imm24 =
40 HOWTO (R_H8500_IMM24, 0, 1, 24, false, 0,
41        complain_overflow_bitfield, 0, "r_imm24", true, 0x00ffffff, 0x00ffffff, false);
42
43 static reloc_howto_type r_imm32 =
44 HOWTO (R_H8500_IMM32, 0, 1, 32, false, 0,
45        complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff, 0xffffffff, false);
46
47
48 static reloc_howto_type r_high8 =
49 HOWTO (R_H8500_HIGH8, 0, 1, 8, false, 0,
50        complain_overflow_dont, 0, "r_high8", true, 0x000000ff, 0x000000ff, false);
51
52 static reloc_howto_type r_low16 =
53 HOWTO (R_H8500_LOW16, 0, 1, 16, false, 0,
54        complain_overflow_dont, 0, "r_low16", true, 0x0000ffff, 0x0000ffff, false);
55
56 static reloc_howto_type r_pcrel8 =
57 HOWTO (R_H8500_PCREL8, 0, 1, 8, true, 0, complain_overflow_signed, 0, "r_pcrel8", true, 0, 0, true);
58
59
60 static reloc_howto_type r_pcrel16 =
61 HOWTO (R_H8500_PCREL16, 0, 1, 16, true, 0, complain_overflow_signed, 0, "r_pcrel16", true, 0, 0, true);
62
63 static reloc_howto_type r_high16 =
64 HOWTO (R_H8500_HIGH16, 0, 1, 8, false, 0,
65        complain_overflow_dont, 0, "r_high16", true, 0x000ffff, 0x0000ffff, false);
66
67
68 /* Turn a howto into a reloc number */
69
70 static int 
71 coff_h8500_select_reloc (howto)
72      reloc_howto_type *howto;
73 {
74   return howto->type;
75 }
76
77 #define SELECT_RELOC(x,howto) x.r_type = coff_h8500_select_reloc(howto)
78
79
80 #define BADMAG(x) H8500BADMAG(x)
81 #define H8500 1                 /* Customize coffcode.h */
82
83 #define __A_MAGIC_SET__
84
85 /* Code to swap in the reloc */
86 #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
87 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
88 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
89   dst->r_stuff[0] = 'S'; \
90   dst->r_stuff[1] = 'C';
91
92 /* Code to turn a r_type into a howto ptr, uses the above howto table
93    */
94
95 static void
96 rtype2howto(internal, dst)
97      arelent * internal;
98      struct internal_reloc *dst;
99 {
100   switch (dst->r_type)
101     {
102     default:
103       fprintf (stderr, "BAD 0x%x\n", dst->r_type);
104     case R_H8500_IMM8:
105       internal->howto = &r_imm8;
106       break;
107     case R_H8500_IMM16:
108       internal->howto = &r_imm16;
109       break;
110     case R_H8500_IMM24:
111       internal->howto = &r_imm24;
112       break;
113     case R_H8500_IMM32:
114       internal->howto = &r_imm32;
115       break;
116     case R_H8500_PCREL8:
117       internal->howto = &r_pcrel8;
118       break;
119     case R_H8500_PCREL16:
120       internal->howto = &r_pcrel16;
121       break;
122     case R_H8500_HIGH8:
123       internal->howto = &r_high8;
124       break;
125     case R_H8500_HIGH16:
126       internal->howto = &r_high16;
127       break;
128     case R_H8500_LOW16:
129       internal->howto = &r_low16;
130       break;
131     }
132 }
133
134 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
135
136
137 /* Perform any necessaru magic to the addend in a reloc entry */
138
139
140 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
141  cache_ptr->addend =  ext_reloc.r_offset;
142
143
144 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
145  reloc_processing(relent, reloc, symbols, abfd, section)
146
147 static void reloc_processing (relent, reloc, symbols, abfd, section)
148      arelent * relent;
149      struct internal_reloc *reloc;
150      asymbol ** symbols;
151      bfd * abfd;
152      asection * section;
153 {
154   relent->address = reloc->r_vaddr;
155   rtype2howto (relent, reloc);
156
157   if (reloc->r_symndx > 0)
158     {
159       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
160     }
161   else
162     {
163       relent->sym_ptr_ptr = &(bfd_abs_symbol);
164     }
165
166
167   relent->addend = reloc->r_offset;
168   relent->address -= section->vma;
169 }
170
171 static void
172 extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)
173      bfd *in_abfd;
174      struct bfd_link_info *link_info;
175      struct bfd_link_order *link_order;
176      arelent *reloc;
177      bfd_byte *data;
178      unsigned int *src_ptr;
179      unsigned int *dst_ptr;
180 {
181   bfd_byte *d = data+*dst_ptr;
182   asection *input_section = link_order->u.indirect.section;
183   switch (reloc->howto->type)
184     {
185     case R_H8500_IMM8:
186       bfd_put_8 (in_abfd,
187                  bfd_coff_reloc16_get_value (reloc, link_info, input_section),
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, link_info, input_section)
196                   >> 16),
197                  d );
198       (*dst_ptr) += 1;
199       (*src_ptr) += 1;
200       break;
201
202     case R_H8500_IMM16:
203       bfd_put_16 (in_abfd,
204                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
205                   d  );
206       (*dst_ptr) += 2;
207       (*src_ptr) += 2;
208       break;
209
210     case R_H8500_LOW16:
211       bfd_put_16 (in_abfd,
212                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
213                   d);
214
215       (*dst_ptr) += 2;
216       (*src_ptr) += 2;
217       break;
218       
219     case R_H8500_HIGH16:
220       bfd_put_16 (in_abfd,
221                   (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
222                    >>16),
223                   d);
224
225       (*dst_ptr) += 2;
226       (*src_ptr) += 2;
227       break;
228
229     case R_H8500_IMM24:
230       {
231         int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
232         int o = bfd_get_32(in_abfd, data+ *dst_ptr -1);
233         v = (v & 0x00ffffff) | (o & 0xff00000);
234         bfd_put_32 (in_abfd, v, data  + *dst_ptr -1);
235         (*dst_ptr) +=3;
236         (*src_ptr)+=3;;
237       }
238       break;
239     case R_H8500_IMM32:
240       {
241         int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
242         bfd_put_32 (in_abfd, v, data  + *dst_ptr);
243         (*dst_ptr) +=4;
244         (*src_ptr)+=4;;
245       }
246       break;
247
248
249     case R_H8500_PCREL8:
250       {
251         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
252                                                   input_section);
253         bfd_vma dot = link_order->offset
254           + *dst_ptr
255             + link_order->u.indirect.section->output_section->vma;
256         int gap = dst - dot - 1; /* -1 since were in the odd byte of the
257                                     word and the pc's been incremented */
258
259         if (gap > 128 || gap < -128)
260           {
261             if (! ((*link_info->callbacks->reloc_overflow)
262                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
263                     reloc->howto->name, reloc->addend, input_section->owner,
264                     input_section, reloc->address)))
265               abort ();
266           }
267         bfd_put_8 (in_abfd, gap, data + *dst_ptr);
268         (*dst_ptr)++;
269         (*src_ptr)++;
270         break;
271       }
272     case R_H8500_PCREL16:
273       {
274         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
275                                                   input_section);
276         bfd_vma dot = link_order->offset
277           + *dst_ptr
278             + link_order->u.indirect.section->output_section->vma;
279         int gap = dst - dot - 1; /* -1 since were in the odd byte of the
280                                     word and the pc's been incremented */
281
282         if (gap > 32767 || gap < -32768)
283           {
284             if (! ((*link_info->callbacks->reloc_overflow)
285                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
286                     reloc->howto->name, reloc->addend, input_section->owner,
287                     input_section, reloc->address)))
288               abort ();
289           }
290         bfd_put_16 (in_abfd, gap, data + *dst_ptr);
291         (*dst_ptr)+=2;
292         (*src_ptr)+=2;
293         break;
294       }
295
296     default:
297       abort ();
298     }
299 }
300
301 #define coff_reloc16_extra_cases extra_case
302
303 #include "coffcode.h"
304
305
306 #undef  coff_bfd_get_relocated_section_contents
307 #undef coff_bfd_relax_section
308 #define coff_bfd_get_relocated_section_contents \
309   bfd_coff_reloc16_get_relocated_section_contents
310 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
311
312 const bfd_target h8500coff_vec =
313 {
314   "coff-h8500",                 /* name */
315   bfd_target_coff_flavour,
316   true,                         /* data byte order is big */
317   true,                         /* header byte order is big */
318
319   (HAS_RELOC | EXEC_P |         /* object flags */
320    HAS_LINENO | HAS_DEBUG |
321    HAS_SYMS | HAS_LOCALS | WP_TEXT),
322
323   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),        /* section flags */
324   '_',                          /* leading symbol underscore */
325   '/',                          /* ar_pad_char */
326   15,                           /* ar_max_namelen */
327   1,                            /* minimum section alignment */
328   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
329      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
330      bfd_getb16, bfd_getb_signed_16, bfd_putb16,        /* data */
331   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
332      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
333      bfd_getb16, bfd_getb_signed_16, bfd_putb16,        /* hdrs */
334
335   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
336    bfd_generic_archive_p, _bfd_dummy_target},
337   {bfd_false, coff_mkobject, _bfd_generic_mkarchive,    /* bfd_set_format */
338    bfd_false},
339   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
340    _bfd_write_archive_contents, bfd_false},
341
342      BFD_JUMP_TABLE_GENERIC (coff),
343      BFD_JUMP_TABLE_COPY (coff),
344      BFD_JUMP_TABLE_CORE (_bfd_nocore),
345      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
346      BFD_JUMP_TABLE_SYMBOLS (coff),
347      BFD_JUMP_TABLE_RELOCS (coff),
348      BFD_JUMP_TABLE_WRITE (coff),
349      BFD_JUMP_TABLE_LINK (coff),
350      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
351
352   COFF_SWAP_TABLE,
353 };