Update copyright notices
[platform/upstream/binutils.git] / bfd / coff-h8500.c
1 /* BFD back-end for Hitachi H8/500 COFF binaries.
2    Copyright 1993, 1994, 1995, 1997, 1999, 2000
3    Free Software Foundation, Inc.
4    Contributed by Cygnus Support.
5    Written by Steve Chamberlain, <sac@cygnus.com>.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.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 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
32
33 static reloc_howto_type r_imm8 =
34 HOWTO (R_H8500_IMM8, 0, 1, 8, false, 0,
35        complain_overflow_bitfield, 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,
39        complain_overflow_bitfield, 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,
43        complain_overflow_bitfield, 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,
47        complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff, 0xffffffff, false);
48
49 static reloc_howto_type r_high8 =
50 HOWTO (R_H8500_HIGH8, 0, 1, 8, false, 0,
51        complain_overflow_dont, 0, "r_high8", true, 0x000000ff, 0x000000ff, false);
52
53 static reloc_howto_type r_low16 =
54 HOWTO (R_H8500_LOW16, 0, 1, 16, false, 0,
55        complain_overflow_dont, 0, "r_low16", true, 0x0000ffff, 0x0000ffff, false);
56
57 static reloc_howto_type r_pcrel8 =
58 HOWTO (R_H8500_PCREL8, 0, 1, 8, true, 0, complain_overflow_signed, 0, "r_pcrel8", true, 0, 0, true);
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 /* Turn a howto into a reloc number */
68
69 static int
70 coff_h8500_select_reloc (howto)
71      reloc_howto_type *howto;
72 {
73   return howto->type;
74 }
75
76 #define SELECT_RELOC(x,howto) x.r_type = coff_h8500_select_reloc(howto)
77
78 #define BADMAG(x) H8500BADMAG(x)
79 #define H8500 1                 /* Customize coffcode.h */
80
81 #define __A_MAGIC_SET__
82
83 /* Code to swap in the reloc */
84 #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
85 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
86 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
87   dst->r_stuff[0] = 'S'; \
88   dst->r_stuff[1] = 'C';
89
90 /* Code to turn a r_type into a howto ptr, uses the above howto table
91    */
92
93 static void
94 rtype2howto(internal, dst)
95      arelent * internal;
96      struct internal_reloc *dst;
97 {
98   switch (dst->r_type)
99     {
100     default:
101       abort ();
102       break;
103     case R_H8500_IMM8:
104       internal->howto = &r_imm8;
105       break;
106     case R_H8500_IMM16:
107       internal->howto = &r_imm16;
108       break;
109     case R_H8500_IMM24:
110       internal->howto = &r_imm24;
111       break;
112     case R_H8500_IMM32:
113       internal->howto = &r_imm32;
114       break;
115     case R_H8500_PCREL8:
116       internal->howto = &r_pcrel8;
117       break;
118     case R_H8500_PCREL16:
119       internal->howto = &r_pcrel16;
120       break;
121     case R_H8500_HIGH8:
122       internal->howto = &r_high8;
123       break;
124     case R_H8500_HIGH16:
125       internal->howto = &r_high16;
126       break;
127     case R_H8500_LOW16:
128       internal->howto = &r_low16;
129       break;
130     }
131 }
132
133 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
134
135 /* Perform any necessary magic to the addend in a reloc entry */
136
137 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
138  cache_ptr->addend =  ext_reloc.r_offset;
139
140 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
141  reloc_processing(relent, reloc, symbols, abfd, section)
142
143 static void reloc_processing (relent, reloc, symbols, abfd, section)
144      arelent * relent;
145      struct internal_reloc *reloc;
146      asymbol ** symbols;
147      bfd * abfd;
148      asection * section;
149 {
150   relent->address = reloc->r_vaddr;
151   rtype2howto (relent, reloc);
152
153   if (reloc->r_symndx > 0)
154     {
155       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
156     }
157   else
158     {
159       relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
160     }
161
162   relent->addend = reloc->r_offset;
163   relent->address -= section->vma;
164 }
165
166 static void
167 extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)
168      bfd *in_abfd;
169      struct bfd_link_info *link_info;
170      struct bfd_link_order *link_order;
171      arelent *reloc;
172      bfd_byte *data;
173      unsigned int *src_ptr;
174      unsigned int *dst_ptr;
175 {
176   bfd_byte *d = data+*dst_ptr;
177   asection *input_section = link_order->u.indirect.section;
178   switch (reloc->howto->type)
179     {
180     case R_H8500_IMM8:
181       bfd_put_8 (in_abfd,
182                  bfd_coff_reloc16_get_value (reloc, link_info, input_section),
183                  d);
184       (*dst_ptr) += 1;
185       (*src_ptr) += 1;
186       break;
187
188     case R_H8500_HIGH8:
189       bfd_put_8 (in_abfd,
190                  (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
191                   >> 16),
192                  d );
193       (*dst_ptr) += 1;
194       (*src_ptr) += 1;
195       break;
196
197     case R_H8500_IMM16:
198       bfd_put_16 (in_abfd,
199                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
200                   d  );
201       (*dst_ptr) += 2;
202       (*src_ptr) += 2;
203       break;
204
205     case R_H8500_LOW16:
206       bfd_put_16 (in_abfd,
207                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
208                   d);
209
210       (*dst_ptr) += 2;
211       (*src_ptr) += 2;
212       break;
213
214     case R_H8500_HIGH16:
215       bfd_put_16 (in_abfd,
216                   (bfd_coff_reloc16_get_value (reloc, link_info, input_section)
217                    >>16),
218                   d);
219
220       (*dst_ptr) += 2;
221       (*src_ptr) += 2;
222       break;
223
224     case R_H8500_IMM24:
225       {
226         int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
227         int o = bfd_get_32 (in_abfd, data+ *dst_ptr -1);
228         v = (v & 0x00ffffff) | (o & 0xff00000);
229         bfd_put_32 (in_abfd, v, data  + *dst_ptr -1);
230         (*dst_ptr) +=3;
231         (*src_ptr)+=3;;
232       }
233       break;
234     case R_H8500_IMM32:
235       {
236         int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
237         bfd_put_32 (in_abfd, v, data  + *dst_ptr);
238         (*dst_ptr) +=4;
239         (*src_ptr)+=4;;
240       }
241       break;
242
243     case R_H8500_PCREL8:
244       {
245         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
246                                                   input_section);
247         bfd_vma dot = link_order->offset
248           + *dst_ptr
249             + link_order->u.indirect.section->output_section->vma;
250         int gap = dst - dot - 1; /* -1 since were in the odd byte of the
251                                     word and the pc's been incremented */
252
253         if (gap > 128 || gap < -128)
254           {
255             if (! ((*link_info->callbacks->reloc_overflow)
256                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
257                     reloc->howto->name, reloc->addend, input_section->owner,
258                     input_section, reloc->address)))
259               abort ();
260           }
261         bfd_put_8 (in_abfd, gap, data + *dst_ptr);
262         (*dst_ptr)++;
263         (*src_ptr)++;
264         break;
265       }
266     case R_H8500_PCREL16:
267       {
268         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
269                                                   input_section);
270         bfd_vma dot = link_order->offset
271           + *dst_ptr
272             + link_order->u.indirect.section->output_section->vma;
273         int gap = dst - dot - 1; /* -1 since were in the odd byte of the
274                                     word and the pc's been incremented */
275
276         if (gap > 32767 || gap < -32768)
277           {
278             if (! ((*link_info->callbacks->reloc_overflow)
279                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
280                     reloc->howto->name, reloc->addend, input_section->owner,
281                     input_section, reloc->address)))
282               abort ();
283           }
284         bfd_put_16 (in_abfd, gap, data + *dst_ptr);
285         (*dst_ptr)+=2;
286         (*src_ptr)+=2;
287         break;
288       }
289
290     default:
291       abort ();
292     }
293 }
294
295 #define coff_reloc16_extra_cases extra_case
296
297 #include "coffcode.h"
298
299 #undef  coff_bfd_get_relocated_section_contents
300 #undef coff_bfd_relax_section
301 #define coff_bfd_get_relocated_section_contents \
302   bfd_coff_reloc16_get_relocated_section_contents
303 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
304
305 CREATE_BIG_COFF_TARGET_VEC (h8500coff_vec, "coff-h8500", 0, 0, '_', NULL)