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