2000-11-15 Kazu Hirata <kazu@hxi.com>
[external/binutils.git] / bfd / coff-z8k.c
1 /* BFD back-end for Zilog Z800n COFF binaries.
2    Copyright 1992, 93, 94, 95, 1997 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "coff/z8k.h"
27 #include "coff/internal.h"
28 #include "libcoff.h"
29
30 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
31
32 static reloc_howto_type r_imm32 =
33 HOWTO (R_IMM32, 0, 1, 32, false, 0,
34        complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff,
35        0xffffffff, false);
36
37 static reloc_howto_type r_imm4l =
38 HOWTO (R_IMM4L, 0, 1, 4, false, 0,
39        complain_overflow_bitfield, 0, "r_imm4l", true, 0xf, 0xf, false);
40
41 static reloc_howto_type r_da =
42 HOWTO (R_IMM16, 0, 1, 16, false, 0,
43        complain_overflow_bitfield, 0, "r_da", true, 0x0000ffff, 0x0000ffff,
44        false);
45
46 static reloc_howto_type r_imm8 =
47 HOWTO (R_IMM8, 0, 1, 8, false, 0,
48        complain_overflow_bitfield, 0, "r_imm8", true, 0x000000ff, 0x000000ff,
49        false);
50
51 static reloc_howto_type r_jr =
52 HOWTO (R_JR, 0, 1, 8, true, 0, complain_overflow_signed, 0,
53        "r_jr", true, 0, 0, true);
54
55 /* Turn a howto into a reloc number */
56
57 static int
58 coff_z8k_select_reloc (howto)
59      reloc_howto_type *howto;
60 {
61   return howto->type;
62 }
63
64 #define SELECT_RELOC(x,howto) x.r_type = coff_z8k_select_reloc(howto)
65
66 #define BADMAG(x) Z8KBADMAG(x)
67 #define Z8K 1                   /* Customize coffcode.h */
68 #define __A_MAGIC_SET__
69
70 /* Code to swap in the reloc */
71 #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
72 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
73 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
74   dst->r_stuff[0] = 'S'; \
75   dst->r_stuff[1] = 'C';
76
77 /* Code to turn a r_type into a howto ptr, uses the above howto table
78    */
79
80 static void
81 rtype2howto (internal, dst)
82      arelent * internal;
83      struct internal_reloc *dst;
84 {
85   switch (dst->r_type)
86     {
87     default:
88       abort ();
89       break;
90     case R_IMM8:
91       internal->howto = &r_imm8;
92       break;
93      case R_IMM16:
94       internal->howto = &r_da;
95       break;
96     case R_JR:
97       internal->howto = &r_jr;
98       break;
99     case R_IMM32:
100       internal->howto = &r_imm32;
101       break;
102     case R_IMM4L:
103       internal->howto = &r_imm4l;
104       break;
105     }
106 }
107
108 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
109
110 /* Perform any necessary magic to the addend in a reloc entry */
111
112 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
113  cache_ptr->addend =  ext_reloc.r_offset;
114
115 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
116  reloc_processing(relent, reloc, symbols, abfd, section)
117
118 static void
119 reloc_processing (relent, reloc, symbols, abfd, section)
120      arelent * relent;
121      struct internal_reloc *reloc;
122      asymbol ** symbols;
123      bfd * abfd;
124      asection * section;
125 {
126   relent->address = reloc->r_vaddr;
127   rtype2howto (relent, reloc);
128
129   if (reloc->r_symndx > 0)
130     {
131       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
132     }
133   else
134     {
135       relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
136     }
137
138   relent->addend = reloc->r_offset;
139   relent->address -= section->vma;
140 }
141
142 static void
143 extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr)
144      bfd *in_abfd;
145      struct bfd_link_info *link_info;
146      struct bfd_link_order *link_order;
147      arelent *reloc;
148      bfd_byte *data;
149      unsigned int *src_ptr;
150      unsigned int *dst_ptr;
151 {
152   asection *input_section = link_order->u.indirect.section;
153
154   switch (reloc->howto->type)
155     {
156     case R_IMM8:
157       bfd_put_8 (in_abfd,
158                  bfd_coff_reloc16_get_value (reloc, link_info, input_section),
159                  data + *dst_ptr);
160       (*dst_ptr) += 1;
161       (*src_ptr) += 1;
162       break;
163
164     case R_IMM32:
165       bfd_put_32 (in_abfd,
166                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
167                   data + *dst_ptr);
168       (*dst_ptr) += 4;
169       (*src_ptr) += 4;
170       break;
171
172     case R_IMM4L:
173       bfd_put_8 (in_abfd,
174                  ((bfd_get_8 (in_abfd, data + *dst_ptr) & 0xf0)
175                   | (0x0f
176                      & bfd_coff_reloc16_get_value (reloc, link_info,
177                                                    input_section))),
178                  data + *dst_ptr);
179       (*dst_ptr) += 1;
180       (*src_ptr) += 1;
181       break;
182
183     case R_IMM16:
184       bfd_put_16 (in_abfd,
185                   bfd_coff_reloc16_get_value (reloc, link_info, input_section),
186                   data + *dst_ptr);
187       (*dst_ptr) += 2;
188       (*src_ptr) += 2;
189       break;
190
191     case R_JR:
192       {
193         bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
194                                                   input_section);
195         bfd_vma dot = (link_order->offset
196                        + *dst_ptr
197                        + input_section->output_section->vma);
198         int gap = dst - dot - 1;/* -1 since were in the odd byte of the
199                                     word and the pc's been incremented */
200
201         if (gap & 1)
202           abort ();
203         gap /= 2;
204         if (gap > 128 || gap < -128)
205           {
206             if (! ((*link_info->callbacks->reloc_overflow)
207                    (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
208                     reloc->howto->name, reloc->addend, input_section->owner,
209                     input_section, reloc->address)))
210               abort ();
211           }
212         bfd_put_8 (in_abfd, gap, data + *dst_ptr);
213         (*dst_ptr)++;
214         (*src_ptr)++;
215         break;
216       }
217     default:
218       abort ();
219     }
220 }
221
222 #define coff_reloc16_extra_cases extra_case
223
224 #include "coffcode.h"
225
226 #undef  coff_bfd_get_relocated_section_contents
227 #undef coff_bfd_relax_section
228 #define coff_bfd_get_relocated_section_contents \
229   bfd_coff_reloc16_get_relocated_section_contents
230 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
231
232 CREATE_BIG_COFF_TARGET_VEC (z8kcoff_vec, "coff-z8k", 0, 0, '_', NULL)