| 1 | /* BFD back-end for Hitachi H8/500 COFF binaries. |
| 2 | Copyright 1993, 94, 95, 1997 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Support. |
| 4 | Written by Steve Chamberlain, <sac@cygnus.com>. |
| 5 | |
| 6 | This file is part of BFD, the Binary File Descriptor library. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 21 | |
| 22 | #include "bfd.h" |
| 23 | #include "sysdep.h" |
| 24 | #include "libbfd.h" |
| 25 | #include "bfdlink.h" |
| 26 | #include "coff/h8500.h" |
| 27 | #include "coff/internal.h" |
| 28 | #include "libcoff.h" |
| 29 | |
| 30 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1) |
| 31 | |
| 32 | static reloc_howto_type r_imm8 = |
| 33 | HOWTO (R_H8500_IMM8, 0, 1, 8, false, 0, |
| 34 | complain_overflow_bitfield, 0, "r_imm8", true, 0x000000ff, 0x000000ff, false); |
| 35 | |
| 36 | static reloc_howto_type r_imm16 = |
| 37 | HOWTO (R_H8500_IMM16, 0, 1, 16, false, 0, |
| 38 | complain_overflow_bitfield, 0, "r_imm16", true, 0x0000ffff, 0x0000ffff, false); |
| 39 | |
| 40 | static reloc_howto_type r_imm24 = |
| 41 | HOWTO (R_H8500_IMM24, 0, 1, 24, false, 0, |
| 42 | complain_overflow_bitfield, 0, "r_imm24", true, 0x00ffffff, 0x00ffffff, false); |
| 43 | |
| 44 | static reloc_howto_type r_imm32 = |
| 45 | HOWTO (R_H8500_IMM32, 0, 1, 32, false, 0, |
| 46 | complain_overflow_bitfield, 0, "r_imm32", true, 0xffffffff, 0xffffffff, false); |
| 47 | |
| 48 | |
| 49 | static reloc_howto_type r_high8 = |
| 50 | HOWTO (R_H8500_HIGH8, 0, 1, 8, false, 0, |
| 51 | complain_overflow_dont, 0, "r_high8", true, 0x000000ff, 0x000000ff, false); |
| 52 | |
| 53 | static reloc_howto_type r_low16 = |
| 54 | HOWTO (R_H8500_LOW16, 0, 1, 16, false, 0, |
| 55 | complain_overflow_dont, 0, "r_low16", true, 0x0000ffff, 0x0000ffff, false); |
| 56 | |
| 57 | static reloc_howto_type r_pcrel8 = |
| 58 | HOWTO (R_H8500_PCREL8, 0, 1, 8, true, 0, complain_overflow_signed, 0, "r_pcrel8", true, 0, 0, true); |
| 59 | |
| 60 | |
| 61 | static reloc_howto_type r_pcrel16 = |
| 62 | HOWTO (R_H8500_PCREL16, 0, 1, 16, true, 0, complain_overflow_signed, 0, "r_pcrel16", true, 0, 0, true); |
| 63 | |
| 64 | static reloc_howto_type r_high16 = |
| 65 | HOWTO (R_H8500_HIGH16, 0, 1, 8, false, 0, |
| 66 | complain_overflow_dont, 0, "r_high16", true, 0x000ffff, 0x0000ffff, false); |
| 67 | |
| 68 | |
| 69 | /* Turn a howto into a reloc number */ |
| 70 | |
| 71 | static int |
| 72 | coff_h8500_select_reloc (howto) |
| 73 | reloc_howto_type *howto; |
| 74 | { |
| 75 | return howto->type; |
| 76 | } |
| 77 | |
| 78 | #define SELECT_RELOC(x,howto) x.r_type = coff_h8500_select_reloc(howto) |
| 79 | |
| 80 | |
| 81 | #define BADMAG(x) H8500BADMAG(x) |
| 82 | #define H8500 1 /* Customize coffcode.h */ |
| 83 | |
| 84 | #define __A_MAGIC_SET__ |
| 85 | |
| 86 | /* Code to swap in the reloc */ |
| 87 | #define SWAP_IN_RELOC_OFFSET bfd_h_get_32 |
| 88 | #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32 |
| 89 | #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \ |
| 90 | dst->r_stuff[0] = 'S'; \ |
| 91 | dst->r_stuff[1] = 'C'; |
| 92 | |
| 93 | /* Code to turn a r_type into a howto ptr, uses the above howto table |
| 94 | */ |
| 95 | |
| 96 | static void |
| 97 | rtype2howto(internal, dst) |
| 98 | arelent * internal; |
| 99 | struct internal_reloc *dst; |
| 100 | { |
| 101 | switch (dst->r_type) |
| 102 | { |
| 103 | default: |
| 104 | abort (); |
| 105 | break; |
| 106 | case R_H8500_IMM8: |
| 107 | internal->howto = &r_imm8; |
| 108 | break; |
| 109 | case R_H8500_IMM16: |
| 110 | internal->howto = &r_imm16; |
| 111 | break; |
| 112 | case R_H8500_IMM24: |
| 113 | internal->howto = &r_imm24; |
| 114 | break; |
| 115 | case R_H8500_IMM32: |
| 116 | internal->howto = &r_imm32; |
| 117 | break; |
| 118 | case R_H8500_PCREL8: |
| 119 | internal->howto = &r_pcrel8; |
| 120 | break; |
| 121 | case R_H8500_PCREL16: |
| 122 | internal->howto = &r_pcrel16; |
| 123 | break; |
| 124 | case R_H8500_HIGH8: |
| 125 | internal->howto = &r_high8; |
| 126 | break; |
| 127 | case R_H8500_HIGH16: |
| 128 | internal->howto = &r_high16; |
| 129 | break; |
| 130 | case R_H8500_LOW16: |
| 131 | internal->howto = &r_low16; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry) |
| 137 | |
| 138 | |
| 139 | /* Perform any necessary magic to the addend in a reloc entry */ |
| 140 | |
| 141 | |
| 142 | #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \ |
| 143 | cache_ptr->addend = ext_reloc.r_offset; |
| 144 | |
| 145 | |
| 146 | #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \ |
| 147 | reloc_processing(relent, reloc, symbols, abfd, section) |
| 148 | |
| 149 | static void reloc_processing (relent, reloc, symbols, abfd, section) |
| 150 | arelent * relent; |
| 151 | struct internal_reloc *reloc; |
| 152 | asymbol ** symbols; |
| 153 | bfd * abfd; |
| 154 | asection * section; |
| 155 | { |
| 156 | relent->address = reloc->r_vaddr; |
| 157 | rtype2howto (relent, reloc); |
| 158 | |
| 159 | if (reloc->r_symndx > 0) |
| 160 | { |
| 161 | relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx]; |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | relent->addend = reloc->r_offset; |
| 170 | relent->address -= section->vma; |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | extra_case (in_abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr) |
| 175 | bfd *in_abfd; |
| 176 | struct bfd_link_info *link_info; |
| 177 | struct bfd_link_order *link_order; |
| 178 | arelent *reloc; |
| 179 | bfd_byte *data; |
| 180 | unsigned int *src_ptr; |
| 181 | unsigned int *dst_ptr; |
| 182 | { |
| 183 | bfd_byte *d = data+*dst_ptr; |
| 184 | asection *input_section = link_order->u.indirect.section; |
| 185 | switch (reloc->howto->type) |
| 186 | { |
| 187 | case R_H8500_IMM8: |
| 188 | bfd_put_8 (in_abfd, |
| 189 | bfd_coff_reloc16_get_value (reloc, link_info, input_section), |
| 190 | d); |
| 191 | (*dst_ptr) += 1; |
| 192 | (*src_ptr) += 1; |
| 193 | break; |
| 194 | |
| 195 | case R_H8500_HIGH8: |
| 196 | bfd_put_8 (in_abfd, |
| 197 | (bfd_coff_reloc16_get_value (reloc, link_info, input_section) |
| 198 | >> 16), |
| 199 | d ); |
| 200 | (*dst_ptr) += 1; |
| 201 | (*src_ptr) += 1; |
| 202 | break; |
| 203 | |
| 204 | case R_H8500_IMM16: |
| 205 | bfd_put_16 (in_abfd, |
| 206 | bfd_coff_reloc16_get_value (reloc, link_info, input_section), |
| 207 | d ); |
| 208 | (*dst_ptr) += 2; |
| 209 | (*src_ptr) += 2; |
| 210 | break; |
| 211 | |
| 212 | case R_H8500_LOW16: |
| 213 | bfd_put_16 (in_abfd, |
| 214 | bfd_coff_reloc16_get_value (reloc, link_info, input_section), |
| 215 | d); |
| 216 | |
| 217 | (*dst_ptr) += 2; |
| 218 | (*src_ptr) += 2; |
| 219 | break; |
| 220 | |
| 221 | case R_H8500_HIGH16: |
| 222 | bfd_put_16 (in_abfd, |
| 223 | (bfd_coff_reloc16_get_value (reloc, link_info, input_section) |
| 224 | >>16), |
| 225 | d); |
| 226 | |
| 227 | (*dst_ptr) += 2; |
| 228 | (*src_ptr) += 2; |
| 229 | break; |
| 230 | |
| 231 | case R_H8500_IMM24: |
| 232 | { |
| 233 | int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section); |
| 234 | int o = bfd_get_32(in_abfd, data+ *dst_ptr -1); |
| 235 | v = (v & 0x00ffffff) | (o & 0xff00000); |
| 236 | bfd_put_32 (in_abfd, v, data + *dst_ptr -1); |
| 237 | (*dst_ptr) +=3; |
| 238 | (*src_ptr)+=3;; |
| 239 | } |
| 240 | break; |
| 241 | case R_H8500_IMM32: |
| 242 | { |
| 243 | int v = bfd_coff_reloc16_get_value(reloc, link_info, input_section); |
| 244 | bfd_put_32 (in_abfd, v, data + *dst_ptr); |
| 245 | (*dst_ptr) +=4; |
| 246 | (*src_ptr)+=4;; |
| 247 | } |
| 248 | break; |
| 249 | |
| 250 | |
| 251 | case R_H8500_PCREL8: |
| 252 | { |
| 253 | bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info, |
| 254 | input_section); |
| 255 | bfd_vma dot = link_order->offset |
| 256 | + *dst_ptr |
| 257 | + link_order->u.indirect.section->output_section->vma; |
| 258 | int gap = dst - dot - 1; /* -1 since were in the odd byte of the |
| 259 | word and the pc's been incremented */ |
| 260 | |
| 261 | if (gap > 128 || gap < -128) |
| 262 | { |
| 263 | if (! ((*link_info->callbacks->reloc_overflow) |
| 264 | (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr), |
| 265 | reloc->howto->name, reloc->addend, input_section->owner, |
| 266 | input_section, reloc->address))) |
| 267 | abort (); |
| 268 | } |
| 269 | bfd_put_8 (in_abfd, gap, data + *dst_ptr); |
| 270 | (*dst_ptr)++; |
| 271 | (*src_ptr)++; |
| 272 | break; |
| 273 | } |
| 274 | case R_H8500_PCREL16: |
| 275 | { |
| 276 | bfd_vma dst = bfd_coff_reloc16_get_value (reloc, link_info, |
| 277 | input_section); |
| 278 | bfd_vma dot = link_order->offset |
| 279 | + *dst_ptr |
| 280 | + link_order->u.indirect.section->output_section->vma; |
| 281 | int gap = dst - dot - 1; /* -1 since were in the odd byte of the |
| 282 | word and the pc's been incremented */ |
| 283 | |
| 284 | if (gap > 32767 || gap < -32768) |
| 285 | { |
| 286 | if (! ((*link_info->callbacks->reloc_overflow) |
| 287 | (link_info, bfd_asymbol_name (*reloc->sym_ptr_ptr), |
| 288 | reloc->howto->name, reloc->addend, input_section->owner, |
| 289 | input_section, reloc->address))) |
| 290 | abort (); |
| 291 | } |
| 292 | bfd_put_16 (in_abfd, gap, data + *dst_ptr); |
| 293 | (*dst_ptr)+=2; |
| 294 | (*src_ptr)+=2; |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | default: |
| 299 | abort (); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | #define coff_reloc16_extra_cases extra_case |
| 304 | |
| 305 | #include "coffcode.h" |
| 306 | |
| 307 | |
| 308 | #undef coff_bfd_get_relocated_section_contents |
| 309 | #undef coff_bfd_relax_section |
| 310 | #define coff_bfd_get_relocated_section_contents \ |
| 311 | bfd_coff_reloc16_get_relocated_section_contents |
| 312 | #define coff_bfd_relax_section bfd_coff_reloc16_relax_section |
| 313 | |
| 314 | CREATE_BIG_COFF_TARGET_VEC (h8500coff_vec, "coff-h8500", 0, 0, '_', NULL) |