bfd/
[deliverable/binutils-gdb.git] / bfd / coff-w65.c
1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright 1995, 1996, 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Written by Steve Chamberlain, <sac@cygnus.com>.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 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
29 static int select_reloc PARAMS ((reloc_howto_type *));
30 static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
31 static void reloc_processing PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
32 static int w65_reloc16_estimate PARAMS ((bfd *, asection *, arelent *, unsigned int, struct bfd_link_info *));
33 static void w65_reloc16_extra_cases PARAMS ((bfd *,struct bfd_link_info *, struct bfd_link_order *, arelent *, bfd_byte *, unsigned int *, unsigned int *));
34
35 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
36 static reloc_howto_type howto_table[] =
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. */
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
59 /* Code to swap in the reloc */
60 #define SWAP_IN_RELOC_OFFSET H_GET_32
61 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
62 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
63 dst->r_stuff[0] = 'S'; \
64 dst->r_stuff[1] = 'C';
65
66 static int
67 select_reloc (howto)
68 reloc_howto_type *howto;
69 {
70 return howto->type ;
71 }
72
73 /* Code to turn a r_type into a howto ptr, uses the above howto table. */
74
75 static void
76 rtype2howto (internal, dst)
77 arelent *internal;
78 struct internal_reloc *dst;
79 {
80 internal->howto = howto_table + dst->r_type - 1;
81 }
82
83 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
84
85 /* Perform any necessary magic to the addend in a reloc entry. */
86
87 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
88 cache_ptr->addend = ext_reloc.r_offset;
89
90 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
91 reloc_processing(relent, reloc, symbols, abfd, section)
92
93 static void
94 reloc_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)
105 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
106 else
107 relent->sym_ptr_ptr = (asymbol **)&(bfd_abs_symbol);
108
109 relent->addend = reloc->r_offset;
110
111 relent->address -= section->vma;
112 /* relent->section = 0;*/
113 }
114
115 static int
116 w65_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
117 bfd *abfd;
118 asection *input_section;
119 arelent *reloc;
120 unsigned int shrink;
121 struct bfd_link_info *link_info;
122 {
123 bfd_vma value;
124 bfd_vma dot;
125 bfd_vma gap;
126
127 /* The address of the thing to be relocated will have moved back by
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
130 uncooked section. */
131
132 /* reloc->address -= shrink; conceptual */
133
134 bfd_vma address = reloc->address - shrink;
135
136 switch (reloc->howto->type)
137 {
138 case R_MOV16B2:
139 case R_JMP2:
140 shrink+=2;
141 break;
142
143 /* Thing is a move one byte. */
144 case R_MOV16B1:
145 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
146
147 if (value >= 0xff00)
148 {
149 /* Change the reloc type from 16bit, possible 8 to 8bit
150 possible 16. */
151 reloc->howto = reloc->howto + 1;
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);
156 }
157
158 break;
159 /* This is the 24 bit branch which could become an 8 bitter,
160 the relocation points to the first byte of the insn, not the
161 actual data. */
162
163 case R_JMPL1:
164 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
165
166 dot = input_section->output_section->vma +
167 input_section->output_offset + address;
168
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
171 jump we were going to. */
172 gap = value - dot;
173
174 if (-120 < (long) gap && (long) gap < 120)
175 {
176 /* Change the reloc type from 24bit, possible 8 to 8bit
177 possible 32. */
178 reloc->howto = reloc->howto + 1;
179 /* This will be two bytes smaller in the long run. */
180 shrink += 2;
181 bfd_perform_slip (abfd, 2, input_section, address);
182 }
183 break;
184
185 case R_JMP1:
186 value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
187
188 dot = input_section->output_section->vma +
189 input_section->output_offset + address;
190
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
193 jump we were going to. */
194 gap = value - (dot - shrink);
195
196 if (-120 < (long) gap && (long) gap < 120)
197 {
198 /* Change the reloc type from 16bit, possible 8 to 8bit
199 possible 16. */
200 reloc->howto = reloc->howto + 1;
201 /* The place to relc moves back by one. */
202
203 /* This will be two bytes smaller in the long run. */
204 shrink += 2;
205 bfd_perform_slip (abfd, 2, input_section, address);
206 }
207 break;
208 }
209
210 return shrink;
211 }
212
213 /* First phase of a relaxing link. */
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
220 R_MOV24B1 R_MOV24B2 24 or 8 bit reloc for mov.b */
221
222 static void
223 w65_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
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);
265 gap >>= 16;
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
277 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
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;
287 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
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;
297 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
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);
307 bfd_put_16 (abfd, (bfd_vma) gap, data + dst_address);
308 bfd_put_8 (abfd, gap >> 16, data+dst_address + 2);
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);
318 bfd_vma dot = link_order->offset
319 + dst_address
320 + link_order->u.indirect.section->output_section->vma;
321
322 gap -= dot + 1;
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 }
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);
341 bfd_vma dot = link_order->offset
342 + dst_address
343 + link_order->u.indirect.section->output_section->vma;
344
345 /* This wraps within the page, so ignore the relativeness, look at the
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 }
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:
363 printf (_("ignoring reloc %s\n"), reloc->howto->name);
364 break;
365
366 }
367 *src_ptr = src_address;
368 *dst_ptr = dst_address;
369 }
370
371 #define coff_reloc16_extra_cases w65_reloc16_extra_cases
372 #define coff_reloc16_estimate w65_reloc16_estimate
373
374 #include "coffcode.h"
375
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
382 CREATE_LITTLE_COFF_TARGET_VEC (w65_vec, "coff-w65", BFD_IS_RELAXABLE, 0, '_', NULL)
This page took 0.047224 seconds and 4 git commands to generate.