daily update
[deliverable/binutils-gdb.git] / bfd / coff-w65.c
CommitLineData
252b5132 1/* BFD back-end for WDC 65816 COFF binaries.
f4ffd778 2 Copyright 1995, 1996, 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
252b5132
RH
3 Written by Steve Chamberlain, <sac@cygnus.com>.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24#include "bfdlink.h"
25#include "coff/w65.h"
26#include "coff/internal.h"
27#include "libcoff.h"
28
f4ffd778
NC
29static int select_reloc PARAMS ((reloc_howto_type *));
30static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
31static void reloc_processing PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
32static int w65_reloc16_estimate PARAMS ((bfd *, asection *, arelent *, unsigned int, struct bfd_link_info *));
33static void w65_reloc16_extra_cases PARAMS ((bfd *,struct bfd_link_info *, struct bfd_link_order *, arelent *, bfd_byte *, unsigned int *, unsigned int *));
34
252b5132
RH
35#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
36static reloc_howto_type howto_table[] =
f4ffd778
NC
37 {
38 HOWTO (R_W65_ABS8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "abs8", true, 0x000000ff, 0x000000ff, false),
39 HOWTO (R_W65_ABS16, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
40 HOWTO (R_W65_ABS24, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "abs24", true, 0x00ffffff, 0x00ffffff, false),
41 HOWTO (R_W65_ABS8S8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, ">abs8", true, 0x000000ff, 0x000000ff, false),
42 HOWTO (R_W65_ABS8S16, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "^abs8", true, 0x000000ff, 0x000000ff, false),
43 HOWTO (R_W65_ABS16S8, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, ">abs16", true, 0x0000ffff, 0x0000ffff, false),
44 HOWTO (R_W65_ABS16S16,1, 0, 16, false, 0, complain_overflow_bitfield, 0, "^abs16", true, 0x0000ffff, 0x0000ffff, false),
45 HOWTO (R_W65_PCR8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "pcrel8", true, 0x000000ff, 0x000000ff, true),
46 HOWTO (R_W65_PCR16, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, true),
47 HOWTO (R_W65_DP, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "dp", true, 0x000000ff, 0x000000ff, false),
48 };
49
50/* Turn a howto into a reloc number. */
252b5132
RH
51
52#define SELECT_RELOC(x,howto) \
53 { x.r_type = select_reloc(howto); }
54
55#define BADMAG(x) (W65BADMAG(x))
56#define W65 1 /* Customize coffcode.h */
57#define __A_MAGIC_SET__
58
252b5132 59/* Code to swap in the reloc */
dc810e39
AM
60#define SWAP_IN_RELOC_OFFSET H_GET_32
61#define SWAP_OUT_RELOC_OFFSET H_PUT_32
252b5132
RH
62#define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
63 dst->r_stuff[0] = 'S'; \
64 dst->r_stuff[1] = 'C';
65
252b5132
RH
66static int
67select_reloc (howto)
68 reloc_howto_type *howto;
69{
70 return howto->type ;
71}
72
f4ffd778 73/* Code to turn a r_type into a howto ptr, uses the above howto table. */
252b5132
RH
74
75static void
76rtype2howto (internal, dst)
77 arelent *internal;
78 struct internal_reloc *dst;
79{
f4ffd778 80 internal->howto = howto_table + dst->r_type - 1;
252b5132
RH
81}
82
83#define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
84
f4ffd778 85/* Perform any necessary magic to the addend in a reloc entry. */
252b5132 86
252b5132
RH
87#define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
88 cache_ptr->addend = ext_reloc.r_offset;
89
252b5132
RH
90#define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
91 reloc_processing(relent, reloc, symbols, abfd, section)
92
93static void
94reloc_processing (relent, reloc, symbols, abfd, section)
95 arelent * relent;
96 struct internal_reloc *reloc;
97 asymbol ** symbols;
98 bfd * abfd;
99 asection * section;
100{
101 relent->address = reloc->r_vaddr;
102 rtype2howto (relent, reloc);
103
104 if (((int) reloc->r_symndx) > 0)
f4ffd778 105 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
252b5132 106 else
f4ffd778 107 relent->sym_ptr_ptr = (asymbol **)&(bfd_abs_symbol);
252b5132 108
252b5132
RH
109 relent->addend = reloc->r_offset;
110
111 relent->address -= section->vma;
112 /* relent->section = 0;*/
113}
114
252b5132 115static int
f4ffd778 116w65_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
252b5132
RH
117 bfd *abfd;
118 asection *input_section;
119 arelent *reloc;
120 unsigned int shrink;
121 struct bfd_link_info *link_info;
122{
cbfe05c4 123 bfd_vma value;
252b5132
RH
124 bfd_vma dot;
125 bfd_vma gap;
126
cbfe05c4 127 /* The address of the thing to be relocated will have moved back by
252b5132
RH
128 the size of the shrink - but we don't change reloc->address here,
129 since we need it to know where the relocation lives in the source
f4ffd778 130 uncooked section. */
252b5132
RH
131
132 /* reloc->address -= shrink; conceptual */
133
134 bfd_vma address = reloc->address - shrink;
252b5132
RH
135
136 switch (reloc->howto->type)
cbfe05c4 137 {
252b5132
RH
138 case R_MOV16B2:
139 case R_JMP2:
140 shrink+=2;
141 break;
142
f4ffd778 143 /* Thing is a move one byte. */
252b5132 144 case R_MOV16B1:
f4ffd778 145 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
252b5132
RH
146
147 if (value >= 0xff00)
cbfe05c4 148 {
252b5132 149 /* Change the reloc type from 16bit, possible 8 to 8bit
f4ffd778 150 possible 16. */
cbfe05c4 151 reloc->howto = reloc->howto + 1;
f4ffd778
NC
152 /* The place to relc moves back by one. */
153 /* This will be two bytes smaller in the long run. */
154 shrink += 2;
155 bfd_perform_slip (abfd, 2, input_section, address);
cbfe05c4 156 }
252b5132
RH
157
158 break;
cbfe05c4 159 /* This is the 24 bit branch which could become an 8 bitter,
f4ffd778
NC
160 the relocation points to the first byte of the insn, not the
161 actual data. */
252b5132
RH
162
163 case R_JMPL1:
f4ffd778 164 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
cbfe05c4 165
252b5132
RH
166 dot = input_section->output_section->vma +
167 input_section->output_offset + address;
cbfe05c4 168
252b5132
RH
169 /* See if the address we're looking at within 127 bytes of where
170 we are, if so then we can use a small branch rather than the
f4ffd778
NC
171 jump we were going to. */
172 gap = value - dot;
cbfe05c4 173
f4ffd778 174 if (-120 < (long) gap && (long) gap < 120)
cbfe05c4 175 {
252b5132 176 /* Change the reloc type from 24bit, possible 8 to 8bit
f4ffd778 177 possible 32. */
cbfe05c4 178 reloc->howto = reloc->howto + 1;
f4ffd778
NC
179 /* This will be two bytes smaller in the long run. */
180 shrink += 2;
181 bfd_perform_slip (abfd, 2, input_section, address);
252b5132
RH
182 }
183 break;
184
185 case R_JMP1:
f4ffd778 186 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
cbfe05c4 187
252b5132
RH
188 dot = input_section->output_section->vma +
189 input_section->output_offset + address;
cbfe05c4 190
252b5132
RH
191 /* See if the address we're looking at within 127 bytes of where
192 we are, if so then we can use a small branch rather than the
f4ffd778 193 jump we were going to. */
252b5132 194 gap = value - (dot - shrink);
252b5132 195
f4ffd778 196 if (-120 < (long) gap && (long) gap < 120)
cbfe05c4 197 {
252b5132 198 /* Change the reloc type from 16bit, possible 8 to 8bit
f4ffd778 199 possible 16. */
cbfe05c4 200 reloc->howto = reloc->howto + 1;
f4ffd778 201 /* The place to relc moves back by one. */
252b5132 202
f4ffd778
NC
203 /* This will be two bytes smaller in the long run. */
204 shrink += 2;
205 bfd_perform_slip (abfd, 2, input_section, address);
252b5132
RH
206 }
207 break;
208 }
209
252b5132
RH
210 return shrink;
211}
212
f4ffd778 213/* First phase of a relaxing link. */
252b5132
RH
214
215/* Reloc types
216 large small
217 R_MOV16B1 R_MOV16B2 mov.b with 16bit or 8 bit address
218 R_JMP1 R_JMP2 jmp or pcrel branch
219 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
f4ffd778 220 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
252b5132
RH
221
222static void
f4ffd778 223w65_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
252b5132
RH
224 dst_ptr)
225 bfd *abfd;
226 struct bfd_link_info *link_info;
227 struct bfd_link_order *link_order;
228 arelent *reloc;
229 bfd_byte *data;
230 unsigned int *src_ptr;
231 unsigned int *dst_ptr;
232{
233 unsigned int src_address = *src_ptr;
234 unsigned int dst_address = *dst_ptr;
235 asection *input_section = link_order->u.indirect.section;
236
237 switch (reloc->howto->type)
238 {
239 case R_W65_ABS8:
240 case R_W65_DP:
241 {
242 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
243 input_section);
244 bfd_put_8 (abfd, gap, data + dst_address);
245 dst_address += 1;
246 src_address += 1;
247 }
248 break;
249
250 case R_W65_ABS8S8:
251 {
252 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
253 input_section);
254 gap >>= 8;
255 bfd_put_8 (abfd, gap, data + dst_address);
256 dst_address += 1;
257 src_address += 1;
258 }
259 break;
260
261 case R_W65_ABS8S16:
262 {
263 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
264 input_section);
f4ffd778 265 gap >>= 16;
252b5132
RH
266 bfd_put_8 (abfd, gap, data + dst_address);
267 dst_address += 1;
268 src_address += 1;
269 }
270 break;
271
272 case R_W65_ABS16:
273 {
274 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
275 input_section);
276
dc810e39 277 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
252b5132
RH
278 dst_address += 2;
279 src_address += 2;
280 }
281 break;
282 case R_W65_ABS16S8:
283 {
284 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
285 input_section);
286 gap >>= 8;
dc810e39 287 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
252b5132
RH
288 dst_address += 2;
289 src_address += 2;
290 }
291 break;
292 case R_W65_ABS16S16:
293 {
294 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
295 input_section);
296 gap >>= 16;
dc810e39 297 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
252b5132
RH
298 dst_address += 2;
299 src_address += 2;
300 }
301 break;
302
303 case R_W65_ABS24:
304 {
305 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
306 input_section);
dc810e39 307 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
f4ffd778 308 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
252b5132
RH
309 dst_address += 3;
310 src_address += 3;
311 }
312 break;
313
314 case R_W65_PCR8:
315 {
316 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
317 input_section);
cbfe05c4
KH
318 bfd_vma dot = link_order->offset
319 + dst_address
252b5132
RH
320 + link_order->u.indirect.section->output_section->vma;
321
322 gap -= dot + 1;
f4ffd778
NC
323 if (gap < -128 || gap > 127)
324 {
325 if (! ((*link_info->callbacks->reloc_overflow)
326 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
327 reloc->howto->name, reloc->addend, input_section->owner,
328 input_section, reloc->address)))
329 abort ();
330 }
252b5132
RH
331 bfd_put_8 (abfd, gap, data + dst_address);
332 dst_address += 1;
333 src_address += 1;
334 }
335 break;
336
337 case R_W65_PCR16:
338 {
339 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
340 input_section);
cbfe05c4
KH
341 bfd_vma dot = link_order->offset
342 + dst_address
252b5132
RH
343 + link_order->u.indirect.section->output_section->vma;
344
252b5132 345 /* This wraps within the page, so ignore the relativeness, look at the
f4ffd778
NC
346 high part. */
347 if ((gap & 0xf0000) != (dot & 0xf0000))
348 {
349 if (! ((*link_info->callbacks->reloc_overflow)
350 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
351 reloc->howto->name, reloc->addend, input_section->owner,
352 input_section, reloc->address)))
353 abort ();
354 }
252b5132
RH
355
356 gap -= dot + 2;
357 bfd_put_16 (abfd, gap, data + dst_address);
358 dst_address += 2;
359 src_address += 2;
360 }
361 break;
362 default:
6e301b2b 363 printf (_("ignoring reloc %s\n"), reloc->howto->name);
252b5132
RH
364 break;
365
366 }
367 *src_ptr = src_address;
368 *dst_ptr = dst_address;
252b5132
RH
369}
370
f4ffd778
NC
371#define coff_reloc16_extra_cases w65_reloc16_extra_cases
372#define coff_reloc16_estimate w65_reloc16_estimate
252b5132
RH
373
374#include "coffcode.h"
375
252b5132
RH
376#undef coff_bfd_get_relocated_section_contents
377#undef coff_bfd_relax_section
378#define coff_bfd_get_relocated_section_contents \
379 bfd_coff_reloc16_get_relocated_section_contents
380#define coff_bfd_relax_section bfd_coff_reloc16_relax_section
381
371e71b8 382CREATE_LITTLE_COFF_TARGET_VEC (w65_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL)
This page took 0.170151 seconds and 4 git commands to generate.