| 1 | /* BFD back-end for ns32k a.out-ish binaries. |
| 2 | Copyright (C) 1990, 91, 92, 94, 95, 96, 1998 Free Software Foundation, Inc. |
| 3 | Contributed by Ian Dall (idall@eleceng.adelaide.edu.au). |
| 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 | #define BYTES_IN_WORD 4 |
| 22 | |
| 23 | #include "bfd.h" |
| 24 | #include "aout/aout64.h" |
| 25 | #include "ns32k.h" |
| 26 | |
| 27 | #define MYNS(OP) CAT(ns32kaout_,OP) |
| 28 | reloc_howto_type * |
| 29 | MYNS(bfd_reloc_type_lookup) |
| 30 | PARAMS((bfd *abfd AND |
| 31 | bfd_reloc_code_real_type code)); |
| 32 | |
| 33 | boolean |
| 34 | MYNS(write_object_contents) |
| 35 | PARAMS((bfd *abfd)); |
| 36 | |
| 37 | /* Avoid multiple definitions from aoutx if supporting standard a.out format(s) |
| 38 | * as well as this one |
| 39 | */ |
| 40 | #define NAME(x,y) CAT3(ns32kaout,_32_,y) |
| 41 | |
| 42 | void bfd_ns32k_arch PARAMS ((void)); |
| 43 | |
| 44 | #include "libaout.h" |
| 45 | |
| 46 | #define MY(OP) MYNS(OP) |
| 47 | |
| 48 | #define MY_swap_std_reloc_in MY(swap_std_reloc_in) |
| 49 | #define MY_swap_std_reloc_out MY(swap_std_reloc_out) |
| 50 | |
| 51 | static void |
| 52 | MY_swap_std_reloc_in PARAMS ((bfd *abfd, struct reloc_std_external *bytes, |
| 53 | arelent *cache_ptr, asymbol **symbols, |
| 54 | bfd_size_type symcount)); |
| 55 | |
| 56 | static void |
| 57 | MY_swap_std_reloc_out PARAMS ((bfd *abfd, arelent *g, |
| 58 | struct reloc_std_external *natptr)); |
| 59 | |
| 60 | /* The ns32k series is ah, unusual, when it comes to relocation. |
| 61 | * There are three storage methods for relocateable objects. There |
| 62 | * are displacements, immediate operands and ordinary twos complement |
| 63 | * data. Of these, only the last fits into the standard relocation |
| 64 | * scheme. Immediate operands are stored huffman encoded and |
| 65 | * immediate operands are stored big endian (where as the natural byte |
| 66 | * order is little endian for this achitecture). |
| 67 | |
| 68 | * Note that the ns32k displacement storage method is orthogonal to |
| 69 | * whether the relocation is pc relative or not. The "displacement" |
| 70 | * storage scheme is used for essentially all address constants. The |
| 71 | * displacement can be relative to zero (absolute displacement), |
| 72 | * relative to the pc (pc relative), the stack pointer, the frame |
| 73 | * pointer, the static base register and general purpose register etc. |
| 74 | |
| 75 | * For example: |
| 76 | * |
| 77 | * sym1: .long . # pc relative 2's complement |
| 78 | * sym1: .long foo # 2's complement not pc relative |
| 79 | * |
| 80 | * self: movd @self, r0 # pc relative displacement |
| 81 | * movd foo, r0 # non pc relative displacement |
| 82 | * |
| 83 | * self: movd self, r0 # pc relative immediate |
| 84 | * movd foo, r0 # non pc relative immediate |
| 85 | * |
| 86 | * In addition, for historical reasons the encoding of the relocation types |
| 87 | * in the a.out format relocation entries is such that even the relocation |
| 88 | * methods which are standard are not encoded the standard way. |
| 89 | * |
| 90 | */ |
| 91 | |
| 92 | reloc_howto_type MY(howto_table)[] = |
| 93 | { |
| 94 | /* ns32k immediate operands */ |
| 95 | HOWTO (BFD_RELOC_NS32K_IMM_8, 0, 0, 8, false, 0, true, |
| 96 | _bfd_ns32k_reloc_imm, "NS32K_IMM_8", |
| 97 | true, 0x000000ff,0x000000ff, false), |
| 98 | HOWTO (BFD_RELOC_NS32K_IMM_16, 0, 1, 16, false, 0, true, |
| 99 | _bfd_ns32k_reloc_imm, "NS32K_IMM_16", |
| 100 | true, 0x0000ffff,0x0000ffff, false), |
| 101 | HOWTO (BFD_RELOC_NS32K_IMM_32, 0, 2, 32, false, 0, true, |
| 102 | _bfd_ns32k_reloc_imm, "NS32K_IMM_32", |
| 103 | true, 0xffffffff,0xffffffff, false), |
| 104 | HOWTO (BFD_RELOC_NS32K_IMM_8_PCREL, 0, 0, 8, true, 0, false, |
| 105 | _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_8", |
| 106 | true, 0x000000ff, 0x000000ff, false), |
| 107 | HOWTO (BFD_RELOC_NS32K_IMM_16_PCREL, 0, 1, 16, true, 0, false, |
| 108 | _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_16", |
| 109 | true, 0x0000ffff,0x0000ffff, false), |
| 110 | HOWTO (BFD_RELOC_NS32K_IMM_32_PCREL, 0, 2, 32, true, 0, false, |
| 111 | _bfd_ns32k_reloc_imm, "PCREL_NS32K_IMM_32", |
| 112 | true, 0xffffffff,0xffffffff, false), |
| 113 | |
| 114 | /* ns32k displacements */ |
| 115 | HOWTO (BFD_RELOC_NS32K_DISP_8, 0, 0, 8, false, 0, true, |
| 116 | _bfd_ns32k_reloc_disp, "NS32K_DISP_8", |
| 117 | true, 0x000000ff,0x000000ff, false), |
| 118 | HOWTO (BFD_RELOC_NS32K_DISP_16, 0, 1, 16, false, 0, true, |
| 119 | _bfd_ns32k_reloc_disp, "NS32K_DISP_16", |
| 120 | true, 0x0000ffff, 0x0000ffff, false), |
| 121 | HOWTO (BFD_RELOC_NS32K_DISP_32, 0, 2, 32, false, 0, true, |
| 122 | _bfd_ns32k_reloc_disp, "NS32K_DISP_32", |
| 123 | true, 0xffffffff, 0xffffffff, false), |
| 124 | HOWTO (BFD_RELOC_NS32K_DISP_8_PCREL, 0, 0, 8, true, 0, false, |
| 125 | _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_8", |
| 126 | true, 0x000000ff,0x000000ff, false), |
| 127 | HOWTO (BFD_RELOC_NS32K_DISP_16_PCREL, 0, 1, 16, true, 0, false, |
| 128 | _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_16", |
| 129 | true, 0x0000ffff,0x0000ffff, false), |
| 130 | HOWTO (BFD_RELOC_NS32K_DISP_32_PCREL, 0, 2, 32, true, 0, false, |
| 131 | _bfd_ns32k_reloc_disp, "PCREL_NS32K_DISP_32", |
| 132 | true, 0xffffffff,0xffffffff, false), |
| 133 | |
| 134 | /* Normal 2's complement */ |
| 135 | HOWTO (BFD_RELOC_8, 0, 0, 8, false, 0, complain_overflow_bitfield,0, |
| 136 | "8", true, 0x000000ff,0x000000ff, false), |
| 137 | HOWTO (BFD_RELOC_16, 0, 1, 16, false, 0, complain_overflow_bitfield,0, |
| 138 | "16", true, 0x0000ffff,0x0000ffff, false), |
| 139 | HOWTO (BFD_RELOC_32, 0, 2, 32, false, 0, complain_overflow_bitfield,0, |
| 140 | "32", true, 0xffffffff,0xffffffff, false), |
| 141 | HOWTO (BFD_RELOC_8_PCREL, 0, 0, 8, true, 0, complain_overflow_signed, 0, |
| 142 | "PCREL_8", true, 0x000000ff,0x000000ff, false), |
| 143 | HOWTO (BFD_RELOC_16_PCREL, 0, 1, 16, true, 0, complain_overflow_signed, 0, |
| 144 | "PCREL_16", true, 0x0000ffff,0x0000ffff, false), |
| 145 | HOWTO (BFD_RELOC_32_PCREL, 0, 2, 32, true, 0, complain_overflow_signed, 0, |
| 146 | "PCREL_32", true, 0xffffffff,0xffffffff, false), |
| 147 | }; |
| 148 | |
| 149 | #define CTOR_TABLE_RELOC_HOWTO(BFD) (MY(howto_table) + 14) |
| 150 | |
| 151 | #define RELOC_STD_BITS_NS32K_TYPE_BIG 0x06 |
| 152 | #define RELOC_STD_BITS_NS32K_TYPE_LITTLE 0x60 |
| 153 | #define RELOC_STD_BITS_NS32K_TYPE_SH_BIG 1 |
| 154 | #define RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE 5 |
| 155 | |
| 156 | reloc_howto_type * |
| 157 | MY(reloc_howto) (abfd, rel, r_index, r_extern, r_pcrel) |
| 158 | bfd *abfd ATTRIBUTE_UNUSED; |
| 159 | struct reloc_std_external *rel; |
| 160 | int *r_index; |
| 161 | int *r_extern; |
| 162 | int *r_pcrel; |
| 163 | { |
| 164 | unsigned int r_length; |
| 165 | int r_ns32k_type; |
| 166 | /* BFD_ASSERT(bfd_header_little_endian (abfd)); */ |
| 167 | *r_index = ((rel->r_index[2] << 16) |
| 168 | | (rel->r_index[1] << 8) |
| 169 | | rel->r_index[0] ); |
| 170 | *r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE)); |
| 171 | *r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE)); |
| 172 | r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE) |
| 173 | >> RELOC_STD_BITS_LENGTH_SH_LITTLE); |
| 174 | r_ns32k_type = ((rel->r_type[0] & RELOC_STD_BITS_NS32K_TYPE_LITTLE) |
| 175 | >> RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE); |
| 176 | return (MY(howto_table) + r_length + 3 * (*r_pcrel) + 6 * r_ns32k_type); |
| 177 | } |
| 178 | |
| 179 | #define MY_reloc_howto(BFD,REL,IN,EX,PC) MY(reloc_howto) (BFD, REL, &IN, &EX, &PC) |
| 180 | |
| 181 | void |
| 182 | MY(put_reloc) (abfd, r_extern, r_index, value, howto, reloc) |
| 183 | bfd *abfd; |
| 184 | int r_extern; |
| 185 | int r_index; |
| 186 | long value; |
| 187 | reloc_howto_type *howto; |
| 188 | struct reloc_std_external *reloc; |
| 189 | { |
| 190 | unsigned int r_length; |
| 191 | int r_pcrel; |
| 192 | int r_ns32k_type; |
| 193 | PUT_WORD (abfd, value, reloc->r_address); |
| 194 | r_length = howto->size ; /* Size as a power of two */ |
| 195 | r_pcrel = (int) howto->pc_relative; /* Relative to PC? */ |
| 196 | r_ns32k_type = (howto - MY(howto_table) )/6; |
| 197 | /* BFD_ASSERT (bfd_header_little_endian (abfd)); */ |
| 198 | reloc->r_index[2] = r_index >> 16; |
| 199 | reloc->r_index[1] = r_index >> 8; |
| 200 | reloc->r_index[0] = r_index; |
| 201 | reloc->r_type[0] = |
| 202 | (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0) |
| 203 | | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0) |
| 204 | | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE) |
| 205 | | (r_ns32k_type << RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE); |
| 206 | } |
| 207 | |
| 208 | #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \ |
| 209 | MY(put_reloc) (BFD, EXT, IDX, VAL, HOWTO, RELOC) |
| 210 | |
| 211 | #define STAT_FOR_EXEC |
| 212 | |
| 213 | #define MY_final_link_relocate _bfd_ns32k_final_link_relocate |
| 214 | #define MY_relocate_contents _bfd_ns32k_relocate_contents |
| 215 | |
| 216 | #include <aoutx.h> |
| 217 | |
| 218 | reloc_howto_type * |
| 219 | MY(bfd_reloc_type_lookup) (abfd,code) |
| 220 | bfd *abfd; |
| 221 | bfd_reloc_code_real_type code; |
| 222 | { |
| 223 | |
| 224 | #define ENTRY(i,j) case i: return &MY(howto_table)[j] |
| 225 | |
| 226 | int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE; |
| 227 | |
| 228 | BFD_ASSERT(ext == 0); |
| 229 | if (code == BFD_RELOC_CTOR) |
| 230 | switch (bfd_get_arch_info (abfd)->bits_per_address) |
| 231 | { |
| 232 | case 32: |
| 233 | code = BFD_RELOC_32; |
| 234 | break; |
| 235 | } |
| 236 | switch (code) |
| 237 | { |
| 238 | ENTRY(BFD_RELOC_NS32K_IMM_8, 0); |
| 239 | ENTRY(BFD_RELOC_NS32K_IMM_16, 1); |
| 240 | ENTRY(BFD_RELOC_NS32K_IMM_32, 2); |
| 241 | ENTRY(BFD_RELOC_NS32K_IMM_8_PCREL, 3); |
| 242 | ENTRY(BFD_RELOC_NS32K_IMM_16_PCREL, 4); |
| 243 | ENTRY(BFD_RELOC_NS32K_IMM_32_PCREL, 5); |
| 244 | ENTRY(BFD_RELOC_NS32K_DISP_8, 6); |
| 245 | ENTRY(BFD_RELOC_NS32K_DISP_16, 7); |
| 246 | ENTRY(BFD_RELOC_NS32K_DISP_32, 8); |
| 247 | ENTRY(BFD_RELOC_NS32K_DISP_8_PCREL, 9); |
| 248 | ENTRY(BFD_RELOC_NS32K_DISP_16_PCREL, 10); |
| 249 | ENTRY(BFD_RELOC_NS32K_DISP_32_PCREL, 11); |
| 250 | ENTRY(BFD_RELOC_8, 12); |
| 251 | ENTRY(BFD_RELOC_16, 13); |
| 252 | ENTRY(BFD_RELOC_32, 14); |
| 253 | ENTRY(BFD_RELOC_8_PCREL, 15); |
| 254 | ENTRY(BFD_RELOC_16_PCREL, 16); |
| 255 | ENTRY(BFD_RELOC_32_PCREL, 17); |
| 256 | default: return (reloc_howto_type *) NULL; |
| 257 | } |
| 258 | #undef ENTRY |
| 259 | } |
| 260 | |
| 261 | static void |
| 262 | MY_swap_std_reloc_in (abfd, bytes, cache_ptr, symbols, symcount) |
| 263 | bfd *abfd; |
| 264 | struct reloc_std_external *bytes; |
| 265 | arelent *cache_ptr; |
| 266 | asymbol **symbols; |
| 267 | bfd_size_type symcount ATTRIBUTE_UNUSED; |
| 268 | { |
| 269 | int r_index; |
| 270 | int r_extern; |
| 271 | int r_pcrel; |
| 272 | struct aoutdata *su = &(abfd->tdata.aout_data->a); |
| 273 | |
| 274 | cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address); |
| 275 | |
| 276 | /* now the fun stuff */ |
| 277 | |
| 278 | cache_ptr->howto = MY_reloc_howto(abfd, bytes, r_index, r_extern, r_pcrel); |
| 279 | |
| 280 | MOVE_ADDRESS(0); |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | MY_swap_std_reloc_out (abfd, g, natptr) |
| 285 | bfd *abfd; |
| 286 | arelent *g; |
| 287 | struct reloc_std_external *natptr; |
| 288 | { |
| 289 | int r_index; |
| 290 | asymbol *sym = *(g->sym_ptr_ptr); |
| 291 | int r_extern; |
| 292 | unsigned int r_addend; |
| 293 | asection *output_section = sym->section->output_section; |
| 294 | |
| 295 | r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma; |
| 296 | |
| 297 | /* name was clobbered by aout_write_syms to be symbol index */ |
| 298 | |
| 299 | /* If this relocation is relative to a symbol then set the |
| 300 | r_index to the symbols index, and the r_extern bit. |
| 301 | |
| 302 | Absolute symbols can come in in two ways, either as an offset |
| 303 | from the abs section, or as a symbol which has an abs value. |
| 304 | Check for that here. */ |
| 305 | |
| 306 | if (bfd_is_com_section (output_section) |
| 307 | || output_section == &bfd_abs_section |
| 308 | || output_section == &bfd_und_section) |
| 309 | { |
| 310 | if (bfd_abs_section.symbol == sym) |
| 311 | { |
| 312 | /* Whoops, looked like an abs symbol, but is really an offset |
| 313 | from the abs section */ |
| 314 | r_index = 0; |
| 315 | r_extern = 0; |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | /* Fill in symbol */ |
| 320 | r_extern = 1; |
| 321 | #undef KEEPIT |
| 322 | #define KEEPIT udata.i |
| 323 | r_index = (*(g->sym_ptr_ptr))->KEEPIT; |
| 324 | #undef KEEPIT |
| 325 | } |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | /* Just an ordinary section */ |
| 330 | r_extern = 0; |
| 331 | r_index = output_section->target_index; |
| 332 | } |
| 333 | |
| 334 | MY_put_reloc (abfd, r_extern, r_index, g->address, g->howto, natptr); |
| 335 | } |
| 336 | |
| 337 | bfd_reloc_status_type |
| 338 | _bfd_ns32k_relocate_contents (howto, input_bfd, relocation, location) |
| 339 | reloc_howto_type *howto; |
| 340 | bfd *input_bfd; |
| 341 | bfd_vma relocation; |
| 342 | bfd_byte *location; |
| 343 | { |
| 344 | int r_ns32k_type = (howto - MY(howto_table)) / 6; |
| 345 | long (*get_data) PARAMS ((bfd_byte *, long, long)); |
| 346 | int (*put_data) PARAMS ((long, bfd_byte *, long, long)); |
| 347 | |
| 348 | switch (r_ns32k_type) |
| 349 | { |
| 350 | case 0: |
| 351 | get_data = _bfd_ns32k_get_immediate; |
| 352 | put_data = _bfd_ns32k_put_immediate; |
| 353 | break; |
| 354 | case 1: |
| 355 | get_data = _bfd_ns32k_get_displacement; |
| 356 | put_data = _bfd_ns32k_put_displacement; |
| 357 | break; |
| 358 | case 2: |
| 359 | return _bfd_relocate_contents (howto, input_bfd, relocation, |
| 360 | location); |
| 361 | /* NOT REACHED */ |
| 362 | break; |
| 363 | default: |
| 364 | return bfd_reloc_notsupported; |
| 365 | } |
| 366 | return _bfd_do_ns32k_reloc_contents (howto, input_bfd, relocation, |
| 367 | location, get_data, put_data); |
| 368 | } |