1 /* BFD back-end for WDC 65816 COFF binaries.
2 Copyright 1995 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 #include "coff/internal.h"
30 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
31 static reloc_howto_type howto_table[] =
33 HOWTO (R_W65_ABS8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "abs8", true, 0x000000ff, 0x000000ff, false),
34 HOWTO (R_W65_ABS16, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
35 HOWTO (R_W65_ABS24, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "abs24", true, 0x00ffffff, 0x00ffffff, false),
36 HOWTO (R_W65_ABS8S8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, ">abs8", true, 0x000000ff, 0x000000ff, false),
37 HOWTO (R_W65_ABS8S16, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "^abs8", true, 0x000000ff, 0x000000ff, false),
38 HOWTO (R_W65_ABS16S8, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, ">abs16", true, 0x0000ffff, 0x0000ffff, false),
39 HOWTO (R_W65_ABS16S16,1, 0, 16, false, 0, complain_overflow_bitfield, 0, "^abs16", true, 0x0000ffff, 0x0000ffff, false),
40 HOWTO (R_W65_PCR8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "pcrel8", true, 0x000000ff, 0x000000ff, true),
41 HOWTO (R_W65_PCR16, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, true),
42 HOWTO (R_W65_DP, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "dp", true, 0x000000ff, 0x000000ff, false),
47 /* Turn a howto into a reloc number */
49 #define SELECT_RELOC(x,howto) \
50 { x.r_type = select_reloc(howto); }
52 #define BADMAG(x) (W65BADMAG(x))
53 #define W65 1 /* Customize coffcode.h */
54 #define __A_MAGIC_SET__
57 /* Code to swap in the reloc */
58 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
59 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
60 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
61 dst->r_stuff[0] = 'S'; \
62 dst->r_stuff[1] = 'C';
67 reloc_howto_type *howto;
72 /* Code to turn a r_type into a howto ptr, uses the above howto table
76 rtype2howto (internal, dst)
78 struct internal_reloc *dst;
80 internal->howto = howto_table + dst->r_type - 1;
83 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
86 /* Perform any necessaru magic to the addend in a reloc entry */
89 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
90 cache_ptr->addend = ext_reloc.r_offset;
93 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
94 reloc_processing(relent, reloc, symbols, abfd, section)
97 reloc_processing (relent, reloc, symbols, abfd, section)
99 struct internal_reloc *reloc;
104 relent->address = reloc->r_vaddr;
105 rtype2howto (relent, reloc);
107 if (((int) reloc->r_symndx) > 0)
109 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
113 relent->sym_ptr_ptr = (asymbol **)&(bfd_abs_symbol);
118 relent->addend = reloc->r_offset;
120 relent->address -= section->vma;
121 /* relent->section = 0;*/
126 h8300_reloc16_estimate(abfd, input_section, reloc, shrink, link_info)
128 asection *input_section;
131 struct bfd_link_info *link_info;
137 /* The address of the thing to be relocated will have moved back by
138 the size of the shrink - but we don't change reloc->address here,
139 since we need it to know where the relocation lives in the source
142 /* reloc->address -= shrink; conceptual */
144 bfd_vma address = reloc->address - shrink;
147 switch (reloc->howto->type)
154 /* Thing is a move one byte */
156 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
161 /* Change the reloc type from 16bit, possible 8 to 8bit
163 reloc->howto = reloc->howto + 1;
164 /* The place to relc moves back by one */
165 /* This will be two bytes smaller in the long run */
167 bfd_perform_slip(abfd, 2, input_section, address);
171 /* This is the 24 bit branch which could become an 8 bitter,
172 the relocation points to the first byte of the insn, not the
176 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
178 dot = input_section->output_section->vma +
179 input_section->output_offset + address;
181 /* See if the address we're looking at within 127 bytes of where
182 we are, if so then we can use a small branch rather than the
183 jump we were going to */
187 if (-120 < (long)gap && (long)gap < 120 )
190 /* Change the reloc type from 24bit, possible 8 to 8bit
192 reloc->howto = reloc->howto + 1;
193 /* This will be two bytes smaller in the long run */
195 bfd_perform_slip(abfd, 2, input_section, address);
201 value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
203 dot = input_section->output_section->vma +
204 input_section->output_offset + address;
206 /* See if the address we're looking at within 127 bytes of where
207 we are, if so then we can use a small branch rather than the
208 jump we were going to */
210 gap = value - (dot - shrink);
213 if (-120 < (long)gap && (long)gap < 120 )
216 /* Change the reloc type from 16bit, possible 8 to 8bit
218 reloc->howto = reloc->howto + 1;
219 /* The place to relc moves back by one */
221 /* This will be two bytes smaller in the long run */
223 bfd_perform_slip(abfd, 2, input_section, address);
233 /* First phase of a relaxing link */
237 R_MOVB1 R_MOVB2 mov.b with 16bit or 8 bit address
238 R_JMP1 R_JMP2 jmp or pcrel branch
239 R_JMPL1 R_JMPL_B8 24jmp or pcrel branch
240 R_MOVLB1 R_MOVLB2 24 or 8 bit reloc for mov.b
245 h8300_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
248 struct bfd_link_info *link_info;
249 struct bfd_link_order *link_order;
252 unsigned int *src_ptr;
253 unsigned int *dst_ptr;
255 unsigned int src_address = *src_ptr;
256 unsigned int dst_address = *dst_ptr;
257 asection *input_section = link_order->u.indirect.section;
259 switch (reloc->howto->type)
264 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
266 bfd_put_8 (abfd, gap, data + dst_address);
274 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
277 bfd_put_8 (abfd, gap, data + dst_address);
285 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
288 bfd_put_8 (abfd, gap, data + dst_address);
296 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
299 bfd_put_16 (abfd, gap, data + dst_address);
306 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
309 bfd_put_16 (abfd, gap, data + dst_address);
316 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
319 bfd_put_16 (abfd, gap, data + dst_address);
327 unsigned int gap = bfd_coff_reloc16_get_value (reloc, link_info,
329 bfd_put_16 (abfd, gap, data + dst_address);
330 bfd_put_8 (abfd, gap>>16, data+dst_address+2);
338 int gap = bfd_coff_reloc16_get_value (reloc, link_info,
340 bfd_vma dot = link_order->offset
342 + link_order->u.indirect.section->output_section->vma;
345 if (gap < -128 || gap > 127) {
346 if (! ((*link_info->callbacks->reloc_overflow)
347 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
348 reloc->howto->name, reloc->addend, input_section->owner,
349 input_section, reloc->address)))
352 bfd_put_8 (abfd, gap, data + dst_address);
360 bfd_vma gap = bfd_coff_reloc16_get_value (reloc, link_info,
362 bfd_vma dot = link_order->offset
364 + link_order->u.indirect.section->output_section->vma;
367 /* This wraps within the page, so ignore the relativeness, look at the
369 if ((gap & 0xf0000) != (dot & 0xf0000)) {
370 if (! ((*link_info->callbacks->reloc_overflow)
371 (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr),
372 reloc->howto->name, reloc->addend, input_section->owner,
373 input_section, reloc->address)))
378 bfd_put_16 (abfd, gap, data + dst_address);
384 printf("ignoring reloc %s\n", reloc->howto->name);
388 *src_ptr = src_address;
389 *dst_ptr = dst_address;
393 #define coff_reloc16_extra_cases h8300_reloc16_extra_cases
394 #define coff_reloc16_estimate h8300_reloc16_estimate
396 #include "coffcode.h"
399 #undef coff_bfd_get_relocated_section_contents
400 #undef coff_bfd_relax_section
401 #define coff_bfd_get_relocated_section_contents \
402 bfd_coff_reloc16_get_relocated_section_contents
403 #define coff_bfd_relax_section bfd_coff_reloc16_relax_section
409 "coff-w65", /* name */
410 bfd_target_coff_flavour,
411 false, /* data byte order is big */
412 false, /* header byte order is big */
414 (HAS_RELOC | EXEC_P | /* object flags */
415 HAS_LINENO | HAS_DEBUG |
416 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
417 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
418 '_', /* leading char */
419 '/', /* ar_pad_char */
420 15, /* ar_max_namelen */
421 1, /* minimum section alignment */
422 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
423 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
424 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
425 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
426 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
427 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
429 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
430 bfd_generic_archive_p, _bfd_dummy_target},
431 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
433 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
434 _bfd_write_archive_contents, bfd_false},
436 BFD_JUMP_TABLE_GENERIC (coff),
437 BFD_JUMP_TABLE_COPY (coff),
438 BFD_JUMP_TABLE_CORE (_bfd_nocore),
439 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
440 BFD_JUMP_TABLE_SYMBOLS (coff),
441 BFD_JUMP_TABLE_RELOCS (coff),
442 BFD_JUMP_TABLE_WRITE (coff),
443 BFD_JUMP_TABLE_LINK (coff),
444 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),