| 1 | /* BFD back-end for WDC 65816 COFF binaries.
|
|---|
| 2 | Copyright 1995, 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
|
|---|
| 3 | Written by Steve Chamberlain, <[email protected]>.
|
|---|
| 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 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
|
|---|
| 30 | static reloc_howto_type howto_table[] =
|
|---|
| 31 | {
|
|---|
| 32 | HOWTO (R_W65_ABS8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "abs8", true, 0x000000ff, 0x000000ff, false),
|
|---|
| 33 | HOWTO (R_W65_ABS16, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
|
|---|
| 34 | HOWTO (R_W65_ABS24, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "abs24", true, 0x00ffffff, 0x00ffffff, false),
|
|---|
| 35 | HOWTO (R_W65_ABS8S8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, ">abs8", true, 0x000000ff, 0x000000ff, false),
|
|---|
| 36 | HOWTO (R_W65_ABS8S16, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "^abs8", true, 0x000000ff, 0x000000ff, false),
|
|---|
| 37 | HOWTO (R_W65_ABS16S8, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, ">abs16", true, 0x0000ffff, 0x0000ffff, false),
|
|---|
| 38 | HOWTO (R_W65_ABS16S16,1, 0, 16, false, 0, complain_overflow_bitfield, 0, "^abs16", true, 0x0000ffff, 0x0000ffff, false),
|
|---|
| 39 | HOWTO (R_W65_PCR8, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "pcrel8", true, 0x000000ff, 0x000000ff, true),
|
|---|
| 40 | HOWTO (R_W65_PCR16, 1, 0, 16, false, 0, complain_overflow_bitfield, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, true),
|
|---|
| 41 | HOWTO (R_W65_DP, 0, 0, 8, false, 0, complain_overflow_bitfield, 0, "dp", true, 0x000000ff, 0x000000ff, false),
|
|---|
| 42 |
|
|---|
| 43 | };
|
|---|
| 44 |
|
|---|
| 45 | /* Turn a howto into a reloc number */
|
|---|
| 46 |
|
|---|
| 47 | #define SELECT_RELOC(x,howto) \
|
|---|
| 48 | { x.r_type = select_reloc(howto); }
|
|---|
| 49 |
|
|---|
| 50 | #define BADMAG(x) (W65BADMAG(x))
|
|---|
| 51 | #define W65 1 /* Customize coffcode.h */
|
|---|
| 52 | #define __A_MAGIC_SET__
|
|---|
| 53 |
|
|---|
| 54 | /* Code to swap in the reloc */
|
|---|
| 55 | #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
|
|---|
| 56 | #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
|
|---|
| 57 | #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
|
|---|
| 58 | dst->r_stuff[0] = 'S'; \
|
|---|
| 59 | dst->r_stuff[1] = 'C';
|
|---|
| 60 |
|
|---|
| 61 | static int
|
|---|
| 62 | select_reloc (howto)
|
|---|
| 63 | reloc_howto_type *howto;
|
|---|
| 64 | {
|
|---|
| 65 | return howto->type ;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /* Code to turn a r_type into a howto ptr, uses the above howto table
|
|---|
| 69 | */
|
|---|
| 70 |
|
|---|
| 71 | static void
|
|---|
| 72 | rtype2howto (internal, dst)
|
|---|
| 73 | arelent *internal;
|
|---|
| 74 | struct internal_reloc *dst;
|
|---|
| 75 | {
|
|---|
| 76 | internal->howto = howto_table + dst->r_type - 1;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
|
|---|
| 80 |
|
|---|
| 81 | /* Perform any necessary magic to the addend in a reloc entry */
|
|---|
| 82 |
|
|---|
| 83 | #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
|
|---|
| 84 | cache_ptr->addend = ext_reloc.r_offset;
|
|---|
| 85 |
|
|---|
| 86 | #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
|
|---|
| 87 | reloc_processing(relent, reloc, symbols, abfd, section)
|
|---|
| 88 |
|
|---|
| 89 | static void
|
|---|
| 90 | reloc_processing (relent, reloc, symbols, abfd, section)
|
|---|
| 91 | arelent * relent;
|
|---|
| 92 | struct internal_reloc *reloc;
|
|---|
| 93 | asymbol ** symbols;
|
|---|
| 94 | bfd * abfd;
|
|---|
| 95 | asection * section;
|
|---|
| 96 | {
|
|---|
| 97 | relent->address = reloc->r_vaddr;
|
|---|
| 98 | rtype2howto (relent, reloc);
|
|---|
| 99 |
|
|---|
| 100 | if (((int) reloc->r_symndx) > 0)
|
|---|
| 101 | {
|
|---|
| 102 | relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
|
|---|
| 103 | }
|
|---|
| 104 | else
|
|---|
| 105 | {
|
|---|
| 106 | relent->sym_ptr_ptr = (asymbol **)&(bfd_abs_symbol);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | relent->addend = reloc->r_offset;
|
|---|
| 110 |
|
|---|
| 111 | relent->address -= section->vma;
|
|---|
| 112 | /* relent->section = 0;*/
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | static int
|
|---|
| 116 | h8300_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 |
|
|---|
| 150 | /* Change the reloc type from 16bit, possible 8 to 8bit
|
|---|
| 151 | possible 16 */
|
|---|
| 152 | reloc->howto = reloc->howto + 1;
|
|---|
| 153 | /* The place to relc moves back by one */
|
|---|
| 154 | /* This will be two bytes smaller in the long run */
|
|---|
| 155 | shrink +=2 ;
|
|---|
| 156 | bfd_perform_slip(abfd, 2, input_section, address);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | break;
|
|---|
| 160 | /* This is the 24 bit branch which could become an 8 bitter,
|
|---|
| 161 | the relocation points to the first byte of the insn, not the
|
|---|
| 162 | actual data */
|
|---|
| 163 |
|
|---|
| 164 | case R_JMPL1:
|
|---|
| 165 | value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
|
|---|
| 166 |
|
|---|
| 167 | dot = input_section->output_section->vma +
|
|---|
| 168 | input_section->output_offset + address;
|
|---|
| 169 |
|
|---|
| 170 | /* See if the address we're looking at within 127 bytes of where
|
|---|
| 171 | we are, if so then we can use a small branch rather than the
|
|---|
| 172 | jump we were going to */
|
|---|
| 173 |
|
|---|
| 174 | gap = value - dot ;
|
|---|
| 175 |
|
|---|
| 176 | if (-120 < (long)gap && (long)gap < 120 )
|
|---|
| 177 | {
|
|---|
| 178 |
|
|---|
| 179 | /* Change the reloc type from 24bit, possible 8 to 8bit
|
|---|
| 180 | possible 32 */
|
|---|
| 181 | reloc->howto = reloc->howto + 1;
|
|---|
| 182 | /* This will be two bytes smaller in the long run */
|
|---|
| 183 | shrink +=2 ;
|
|---|
| 184 | bfd_perform_slip(abfd, 2, input_section, address);
|
|---|
| 185 | }
|
|---|
| 186 | break;
|
|---|
| 187 |
|
|---|
| 188 | case R_JMP1:
|
|---|
| 189 |
|
|---|
| 190 | value = bfd_coff_reloc16_get_value(reloc, link_info, input_section);
|
|---|
| 191 |
|
|---|
| 192 | dot = input_section->output_section->vma +
|
|---|
| 193 | input_section->output_offset + address;
|
|---|
| 194 |
|
|---|
| 195 | /* See if the address we're looking at within 127 bytes of where
|
|---|
| 196 | we are, if so then we can use a small branch rather than the
|
|---|
| 197 | jump we were going to */
|
|---|
|
|---|