2004-11-03 Randolph Chung <tausq@debian.org>
[deliverable/binutils-gdb.git] / bfd / coff-z8k.c
CommitLineData
252b5132 1/* BFD back-end for Zilog Z800n COFF binaries.
eac93a98 2 Copyright 1992, 1993, 1994, 1995, 1997, 1999, 2000, 2001, 2002, 2003
7898deda 3 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by Cygnus Support.
5 Written by Steve Chamberlain, <sac@cygnus.com>.
6
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, 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#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
32
33static reloc_howto_type r_imm32 =
b34976b6
AM
34HOWTO (R_IMM32, 0, 2, 32, FALSE, 0,
35 complain_overflow_bitfield, 0, "r_imm32", TRUE, 0xffffffff,
36 0xffffffff, FALSE);
252b5132
RH
37
38static reloc_howto_type r_imm4l =
b34976b6
AM
39HOWTO (R_IMM4L, 0, 0, 4, FALSE, 0,
40 complain_overflow_bitfield, 0, "r_imm4l", TRUE, 0xf, 0xf, FALSE);
252b5132
RH
41
42static reloc_howto_type r_da =
b34976b6
AM
43HOWTO (R_IMM16, 0, 1, 16, FALSE, 0,
44 complain_overflow_bitfield, 0, "r_da", TRUE, 0x0000ffff, 0x0000ffff,
45 FALSE);
252b5132
RH
46
47static reloc_howto_type r_imm8 =
b34976b6
AM
48HOWTO (R_IMM8, 0, 0, 8, FALSE, 0,
49 complain_overflow_bitfield, 0, "r_imm8", TRUE, 0x000000ff, 0x000000ff,
50 FALSE);
252b5132 51
6840198f 52static reloc_howto_type r_rel16 =
b34976b6
AM
53HOWTO (R_REL16, 0, 1, 16, FALSE, 0,
54 complain_overflow_bitfield, 0, "r_rel16", TRUE, 0x0000ffff, 0x0000ffff,
55 TRUE);
6840198f 56
252b5132 57static reloc_howto_type r_jr =
b34976b6
AM
58HOWTO (R_JR, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0,
59 "r_jr", TRUE, 0, 0, TRUE);
252b5132 60
6840198f 61static reloc_howto_type r_disp7 =
b34976b6
AM
62HOWTO (R_DISP7, 0, 0, 7, TRUE, 0, complain_overflow_bitfield, 0,
63 "r_disp7", TRUE, 0, 0, TRUE);
6840198f
NC
64
65static reloc_howto_type r_callr =
b34976b6
AM
66HOWTO (R_CALLR, 0, 1, 12, TRUE, 0, complain_overflow_signed, 0,
67 "r_callr", TRUE, 0xfff, 0xfff, TRUE);
6840198f 68
252b5132
RH
69/* Turn a howto into a reloc number */
70
cbfe05c4 71static int
552585ed 72coff_z8k_select_reloc (reloc_howto_type *howto)
252b5132
RH
73{
74 return howto->type;
75}
76
77#define SELECT_RELOC(x,howto) x.r_type = coff_z8k_select_reloc(howto)
78
252b5132
RH
79#define BADMAG(x) Z8KBADMAG(x)
80#define Z8K 1 /* Customize coffcode.h */
81#define __A_MAGIC_SET__
82
f4ffd778 83/* Code to swap in the reloc. */
dc810e39
AM
84#define SWAP_IN_RELOC_OFFSET H_GET_32
85#define SWAP_OUT_RELOC_OFFSET H_PUT_32
252b5132
RH
86#define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
87 dst->r_stuff[0] = 'S'; \
88 dst->r_stuff[1] = 'C';
89
f4ffd778 90/* Code to turn a r_type into a howto ptr, uses the above howto table. */
252b5132
RH
91
92static void
552585ed 93rtype2howto (arelent *internal, struct internal_reloc *dst)
252b5132
RH
94{
95 switch (dst->r_type)
96 {
97 default:
98 abort ();
99 break;
100 case R_IMM8:
101 internal->howto = &r_imm8;
102 break;
103 case R_IMM16:
104 internal->howto = &r_da;
105 break;
106 case R_JR:
107 internal->howto = &r_jr;
108 break;
6840198f
NC
109 case R_DISP7:
110 internal->howto = &r_disp7;
111 break;
112 case R_CALLR:
113 internal->howto = &r_callr;
114 break;
115 case R_REL16:
116 internal->howto = &r_rel16;
117 break;
252b5132
RH
118 case R_IMM32:
119 internal->howto = &r_imm32;
120 break;
121 case R_IMM4L:
122 internal->howto = &r_imm4l;
123 break;
124 }
125}
126
f4ffd778 127#define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
252b5132 128
f4ffd778 129/* Perform any necessary magic to the addend in a reloc entry. */
252b5132 130
252b5132
RH
131#define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
132 cache_ptr->addend = ext_reloc.r_offset;
133
252b5132
RH
134#define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
135 reloc_processing(relent, reloc, symbols, abfd, section)
136
cbfe05c4 137static void
552585ed
CG
138reloc_processing (arelent *relent,
139 struct internal_reloc *reloc,
140 asymbol **symbols,
141 bfd *abfd,
142 asection *section)
252b5132
RH
143{
144 relent->address = reloc->r_vaddr;
145 rtype2howto (relent, reloc);
146
147 if (reloc->r_symndx > 0)
879db8be 148 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
252b5132 149 else
879db8be 150 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
252b5132 151
252b5132
RH
152 relent->addend = reloc->r_offset;
153 relent->address -= section->vma;
154}
155
156static void
552585ed
CG
157extra_case (bfd *in_abfd,
158 struct bfd_link_info *link_info,
159 struct bfd_link_order *link_order,
160 arelent *reloc,
161 bfd_byte *data,
162 unsigned int *src_ptr,
163 unsigned int *dst_ptr)
252b5132 164{
879db8be 165 asection * input_section = link_order->u.indirect.section;
252b5132
RH
166
167 switch (reloc->howto->type)
168 {
169 case R_IMM8:
170 bfd_put_8 (in_abfd,
171 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
172 data + *dst_ptr);
173 (*dst_ptr) += 1;
174 (*src_ptr) += 1;
175 break;
176
177 case R_IMM32:
3c25c5f6
NC
178 /* If no flags are set, assume immediate value. */
179 if (! (*reloc->sym_ptr_ptr)->section->flags)
180 {
181 bfd_put_32 (in_abfd,
182 bfd_coff_reloc16_get_value (reloc, link_info,
183 input_section),
184 data + *dst_ptr);
185 }
186 else
187 {
188 bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
189 input_section);
5c4491d3 190 /* Addresses are 23 bit, and the layout of those in a 32-bit
3c25c5f6
NC
191 value is as follows:
192 1AAAAAAA xxxxxxxx AAAAAAAA AAAAAAAA
193 (A - address bits, x - ignore). */
194 dst = (dst & 0xffff) | ((dst & 0xff0000) << 8) | 0x80000000;
195 bfd_put_32 (in_abfd, dst, data + *dst_ptr);
196 }
252b5132
RH
197 (*dst_ptr) += 4;
198 (*src_ptr) += 4;
199 break;
200
201 case R_IMM4L:
202 bfd_put_8 (in_abfd,
cbfe05c4 203 ((bfd_get_8 (in_abfd, data + *dst_ptr) & 0xf0)
252b5132
RH
204 | (0x0f
205 & bfd_coff_reloc16_get_value (reloc, link_info,
206 input_section))),
207 data + *dst_ptr);
208 (*dst_ptr) += 1;
209 (*src_ptr) += 1;
210 break;
211
212 case R_IMM16:
213 bfd_put_16 (in_abfd,
214 bfd_coff_reloc16_get_value (reloc, link_info, input_section),
215 data + *dst_ptr);
216 (*dst_ptr) += 2;
217 (*src_ptr) += 2;
218 break;
219
220 case R_JR:
221 {
222 bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
223 input_section);
224 bfd_vma dot = (link_order->offset
225 + *dst_ptr
226 + input_section->output_section->vma);
879db8be
NC
227 int gap = dst - dot - 1; /* -1, since we're in the odd byte of the
228 word and the pc's been incremented. */
252b5132
RH
229
230 if (gap & 1)
231 abort ();
232 gap /= 2;
233 if (gap > 128 || gap < -128)
234 {
235 if (! ((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
236 (link_info, NULL,
237 bfd_asymbol_name (*reloc->sym_ptr_ptr),
252b5132
RH
238 reloc->howto->name, reloc->addend, input_section->owner,
239 input_section, reloc->address)))
240 abort ();
241 }
242 bfd_put_8 (in_abfd, gap, data + *dst_ptr);
243 (*dst_ptr)++;
244 (*src_ptr)++;
245 break;
246 }
6840198f
NC
247
248 case R_DISP7:
249 {
250 bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
251 input_section);
252 bfd_vma dot = (link_order->offset
253 + *dst_ptr
254 + input_section->output_section->vma);
879db8be
NC
255 int gap = dst - dot - 1; /* -1, since we're in the odd byte of the
256 word and the pc's been incremented. */
6840198f
NC
257
258 if (gap & 1)
259 abort ();
260 gap /= 2;
261
8b7cf393 262 if (gap > 0 || gap < -127)
6840198f
NC
263 {
264 if (! ((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
265 (link_info, NULL,
266 bfd_asymbol_name (*reloc->sym_ptr_ptr),
6840198f
NC
267 reloc->howto->name, reloc->addend, input_section->owner,
268 input_section, reloc->address)))
269 abort ();
270 }
271 bfd_put_8 (in_abfd,
272 (bfd_get_8 ( in_abfd, data + *dst_ptr) & 0x80) + (-gap & 0x7f),
273 data + *dst_ptr);
274 (*dst_ptr)++;
275 (*src_ptr)++;
276 break;
277 }
278
279 case R_CALLR:
280 {
281 bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
282 input_section);
283 bfd_vma dot = (link_order->offset
284 + *dst_ptr
285 + input_section->output_section->vma);
286 int gap = dst - dot - 2;
287
288 if (gap & 1)
289 abort ();
8b7cf393 290 if (gap > 4096 || gap < -4095)
6840198f
NC
291 {
292 if (! ((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
293 (link_info, NULL,
294 bfd_asymbol_name (*reloc->sym_ptr_ptr),
6840198f
NC
295 reloc->howto->name, reloc->addend, input_section->owner,
296 input_section, reloc->address)))
297 abort ();
298 }
8b7cf393 299 gap /= 2;
6840198f
NC
300 bfd_put_16 (in_abfd,
301 (bfd_get_16 ( in_abfd, data + *dst_ptr) & 0xf000) | (-gap & 0x0fff),
302 data + *dst_ptr);
303 (*dst_ptr) += 2;
304 (*src_ptr) += 2;
305 break;
306 }
307
308 case R_REL16:
309 {
310 bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info,
311 input_section);
312 bfd_vma dot = (link_order->offset
313 + *dst_ptr
314 + input_section->output_section->vma);
315 int gap = dst - dot - 2;
316
317 if (gap > 32767 || gap < -32768)
318 {
319 if (! ((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
320 (link_info, NULL,
321 bfd_asymbol_name (*reloc->sym_ptr_ptr),
6840198f
NC
322 reloc->howto->name, reloc->addend, input_section->owner,
323 input_section, reloc->address)))
324 abort ();
325 }
dc810e39 326 bfd_put_16 (in_abfd, (bfd_vma) gap, data + *dst_ptr);
6840198f
NC
327 (*dst_ptr) += 2;
328 (*src_ptr) += 2;
329 break;
330 }
879db8be 331
252b5132
RH
332 default:
333 abort ();
334 }
335}
336
337#define coff_reloc16_extra_cases extra_case
338
339#include "coffcode.h"
340
252b5132
RH
341#undef coff_bfd_get_relocated_section_contents
342#undef coff_bfd_relax_section
343#define coff_bfd_get_relocated_section_contents \
344 bfd_coff_reloc16_get_relocated_section_contents
345#define coff_bfd_relax_section bfd_coff_reloc16_relax_section
346
3fa78519 347CREATE_BIG_COFF_TARGET_VEC (z8kcoff_vec, "coff-z8k", 0, 0, '_', NULL, COFF_SWAP_TABLE)
This page took 0.257887 seconds and 4 git commands to generate.