Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / binutils / bfd / coff-z8k.c
1 /* BFD back-end for Zilog Z800n COFF binaries.
2    Copyright 1992, 1993, 1994, 1995, 1997, 1999, 2000, 2001
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/z8k.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30
31 static void extra_case PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *, bfd_byte *, unsigned int *, unsigned int *));
32 static void reloc_processing PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
33 static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
34 static int coff_z8k_select_reloc PARAMS ((reloc_howto_type *));
35
36 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
37
38 static reloc_howto_type r_imm32 =
39 HOWTO (R_IMM32, 0, 2, 32, false, 0,
40        complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff,
41        0xffffffff, false);
42
43 static reloc_howto_type r_imm4l =
44 HOWTO (R_IMM4L, 0, 0, 4, false, 0,
45        complain_overflow_bitfield, 0, "r_imm4l", true, 0xf, 0xf, false);
46
47 static reloc_howto_type r_da =
48 HOWTO (R_IMM16, 0, 1, 16, false, 0,
49        complain_overflow_bitfield, 0, "r_da", true, 0x0000ffff, 0x0000ffff,
50        false);
51
52 static reloc_howto_type r_imm8 =
53 HOWTO (R_IMM8, 0, 0, 8, false, 0,
54        complain_overflow_bitfield, 0, "r_imm8", true, 0x000000ff, 0x000000ff,
55        false);
56
57 static reloc_howto_type r_rel16 =
58 HOWTO (R_REL16, 0, 1, 16, false, 0,
59        complain_overflow_bitfield, 0, "r_rel16", true, 0x0000ffff, 0x0000ffff,
60        true);
61
62 static reloc_howto_type r_jr =
63 HOWTO (R_JR, 0, 0, 8, true, 0, complain_overflow_signed, 0,
64        "r_jr", true, 0, 0, true);
65
66 static reloc_howto_type r_disp7 =
67 HOWTO (R_DISP7, 0, 0, 7, true, 0, complain_overflow_bitfield, 0,
68        "r_disp7", true, 0, 0, true);
69
70 static reloc_howto_type r_callr =
71 HOWTO (R_CALLR, 0, 1, 12, true, 0, complain_overflow_signed, 0,
72        "r_callr", true, 0xfff, 0xfff, true);
73
74 /* Turn a howto into a reloc number */
75
76 static int
77 coff_z8k_select_reloc (howto)
78      reloc_howto_type *howto;
79 {
80   return howto->type;
81 }
82
83 #define SELECT_RELOC(x,howto) x.r_type = coff_z8k_select_reloc(howto)
84
85 #define BADMAG(x) Z8KBADMAG(x)
86 #define Z8K 1                   /* Customize coffcode.h */
87 #define __A_MAGIC_SET__
88
89 /* Code to swap in the reloc.  */
90 #define SWAP_IN_RELOC_OFFSET    H_GET_32
91 #define SWAP_OUT_RELOC_OFFSET   H_PUT_32
92 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
93   dst->r_stuff[0] = 'S'; \
94   dst->r_stuff[1] = 'C';
95
96 /* Code to turn a r_type into a howto ptr, uses the above howto table.  */
97
98 static void
99 rtype2howto (internal, dst)
100      arelent * internal;
101      struct internal_reloc *dst;
102 {
103   switch (dst->r_type)
104     {
105     default:
106       abort ();
107       break;
108     case R_IMM8:
109       internal->howto = &r_imm8;
110       break;
111      case R_IMM16:
112       internal->howto = &r_da;
113       break;
114     case R_JR:
115       internal->howto = &r_jr;
116       break;
117     case R_DISP7:
118       internal->howto = &r_disp7;
119       break;
120     case R_CALLR:
121       internal->howto = &r_callr;
122       break;
123     case R_REL16:
124       internal->howto = &r_rel16;
125       break;
126     case R_IMM32:
127       internal->howto = &r_imm32;
128       break;
129     case R_IMM4L:
130       internal->howto = &r_imm4l;
131       break;
132     }
133 }
134
135 #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
136
137 /* Perform any necessary magic to the addend in a reloc entry.  */
138
139 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
140  cache_ptr->addend =  ext_reloc.r_offset;
141
142 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
143  reloc_processing(relent, reloc, symbols, abfd, section)
144
145 static void
146 reloc_processing (relent, reloc, symbols, abfd, section)
147      arelent * relent;
148      struct internal_reloc * reloc;
149      asymbol ** symbols;
150      bfd * abfd;
151      asection * section;
152 {
153   relent->address = reloc->r_vaddr;
154   rtype2howto (relent, reloc);
155
156   if (reloc->r_symndx > 0)
157     relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
158   else
159     relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
160
161   relent->addend = reloc->r_offset;
162   relent->address -= section->vma;
163 }
164
165 static void
166 extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)
167      bfd * in_abfd;
168      struct bfd_link_info * link_info;
169      struct bfd_link_order * link_order;
170      arelent * reloc;
171      bfd_byte * data;
172      unsigned int * src_ptr;
173      unsigned int * dst_ptr;
174 {
175   asection * input_section = link_order->u.indirect.section;
176
177   switch (reloc->howto->type)
178     {
179     case R_IMM8:
180       bfd_put_8 (in_abfd,
181                  bfd_coff_reloc16_get_value (reloc, link_info, input_section),
182                  data + *dst_ptr);
183       (*dst_ptr) += 1;
184       (*src_ptr) += 1;
185       break;
186
187     case R_IMM32:
188       bfd_put_32 (in_abfd,
189                   /* 0x80000000 indicates a long segmented address.  */
190                   bfd_coff_reloc16_get_value (reloc, link_info, input_section) | 0x80000000,
191                   data + *dst_ptr);
192       (*dst_ptr) += 4;
193       (*src_ptr) += 4;
194       break;
195
196     case R_IMM4L:
197       bfd_put_8 (in_abfd,
198                  ((bfd_get_8 (in_abfd, data + *dst_ptr) & 0xf0)
199                   | (0x0f
200                      & bfd_coff_reloc16_get_value (reloc, link_info,
201                                                    input_section))),
202                  data + *dst_ptr);
203       (*dst_ptr) += 1;
204       (*src_ptr) += 1;
205       break;
206
207     case R_IMM16:
208       bfd_put_16 (in_abfd,
209                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
210                   data + *dst_ptr);
211       (*dst_ptr) += 2;
212       (*src_ptr) += 2;
213       break;
214
215     case R_JR:
216       {
217         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
218                                                   input_section);
219         bfd_vma dot = (link_order->offset
220                        + *dst_ptr
221                        + input_section->output_section->vma);
222         int gap = dst - dot - 1;  /* -1, since we're in the odd byte of the
223                                      word and the pc's been incremented.  */
224
225         if (gap & 1)
226           abort ();
227         gap /= 2;
228         if (gap > 128 || gap < -128)
229           {
230             if (! ((*link_info->callbacks->reloc_overflow)
231                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
232                     reloc->howto->name, reloc->addend, input_section->owner,
233                     input_section, reloc->address)))
234               abort ();
235           }
236         bfd_put_8 (in_abfd, gap, data + *dst_ptr);
237         (*dst_ptr)++;
238         (*src_ptr)++;
239         break;
240       }
241
242     case R_DISP7:
243       {
244         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
245                                                   input_section);
246         bfd_vma dot = (link_order->offset
247                        + *dst_ptr
248                        + input_section->output_section->vma);
249         int gap = dst - dot - 1;  /* -1, since we're in the odd byte of the
250                                      word and the pc's been incremented.  */
251
252         if (gap & 1)
253           abort ();
254         gap /= 2;
255
256         if (gap > 0 || gap < -128)
257           {
258             if (! ((*link_info->callbacks->reloc_overflow)
259                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
260                     reloc->howto->name, reloc->addend, input_section->owner,
261                     input_section, reloc->address)))
262               abort ();
263           }
264         bfd_put_8 (in_abfd,
265                    (bfd_get_8 ( in_abfd, data + *dst_ptr) & 0x80) + (-gap & 0x7f),
266                    data + *dst_ptr);
267         (*dst_ptr)++;
268         (*src_ptr)++;
269         break;
270       }
271
272     case R_CALLR:
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                        + input_section->output_section->vma);
279         int gap = dst - dot - 2;
280
281         if (gap & 1)
282           abort ();
283         gap /= 2;
284         if (gap > 8191 || gap < -8192)
285           {
286             if (! ((*link_info->callbacks->reloc_overflow)
287                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
288                     reloc->howto->name, reloc->addend, input_section->owner,
289                     input_section, reloc->address)))
290               abort ();
291           }
292         bfd_put_16 (in_abfd,
293                     (bfd_get_16 ( in_abfd, data + *dst_ptr) & 0xf000) | (-gap & 0x0fff),
294                     data + *dst_ptr);
295         (*dst_ptr) += 2;
296         (*src_ptr) += 2;
297         break;
298       }
299
300     case R_REL16:
301       {
302         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
303                                                   input_section);
304         bfd_vma dot = (link_order->offset
305                        + *dst_ptr
306                        + input_section->output_section->vma);
307         int gap = dst - dot - 2;
308
309         if (gap > 32767 || gap < -32768)
310           {
311             if (! ((*link_info->callbacks->reloc_overflow)
312                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
313                     reloc->howto->name, reloc->addend, input_section->owner,
314                     input_section, reloc->address)))
315               abort ();
316           }
317         bfd_put_16 (in_abfd, (bfd_vma) gap, data + *dst_ptr);
318         (*dst_ptr) += 2;
319         (*src_ptr) += 2;
320         break;
321       }
322
323     default:
324       abort ();
325     }
326 }
327
328 #define coff_reloc16_extra_cases extra_case
329
330 #include "coffcode.h"
331
332 #undef  coff_bfd_get_relocated_section_contents
333 #undef coff_bfd_relax_section
334 #define coff_bfd_get_relocated_section_contents \
335   bfd_coff_reloc16_get_relocated_section_contents
336 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
337
338 CREATE_BIG_COFF_TARGET_VEC (z8kcoff_vec, "coff-z8k", 0, 0, '_', NULL)