| 1 | /* BFD back-end for Intel 960 b.out binaries. |
| 2 | Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc. |
| 3 | Written by Cygnus Support. |
| 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 | |
| 22 | #include "bfd.h" |
| 23 | #include "sysdep.h" |
| 24 | #include "libbfd.h" |
| 25 | #include "bfdlink.h" |
| 26 | #include "genlink.h" |
| 27 | #include "bout.h" |
| 28 | |
| 29 | #include "aout/stab_gnu.h" |
| 30 | #include "libaout.h" /* BFD a.out internal data structures */ |
| 31 | |
| 32 | |
| 33 | static int aligncode PARAMS ((bfd *abfd, asection *input_section, |
| 34 | arelent *r, unsigned int shrink)); |
| 35 | static void perform_slip PARAMS ((bfd *abfd, unsigned int slip, |
| 36 | asection *input_section, bfd_vma value)); |
| 37 | static boolean b_out_squirt_out_relocs PARAMS ((bfd *abfd, asection *section)); |
| 38 | static const bfd_target *b_out_callback PARAMS ((bfd *)); |
| 39 | static bfd_reloc_status_type calljx_callback |
| 40 | PARAMS ((bfd *, struct bfd_link_info *, arelent *, PTR src, PTR dst, |
| 41 | asection *)); |
| 42 | static bfd_reloc_status_type callj_callback |
| 43 | PARAMS ((bfd *, struct bfd_link_info *, arelent *, PTR data, |
| 44 | unsigned int srcidx, unsigned int dstidx, asection *, boolean)); |
| 45 | static bfd_vma get_value PARAMS ((arelent *, struct bfd_link_info *, |
| 46 | asection *)); |
| 47 | static int abs32code PARAMS ((bfd *, asection *, arelent *, |
| 48 | unsigned int, struct bfd_link_info *)); |
| 49 | static boolean b_out_bfd_relax_section PARAMS ((bfd *, asection *, |
| 50 | struct bfd_link_info *, |
| 51 | boolean *)); |
| 52 | static bfd_byte *b_out_bfd_get_relocated_section_contents |
| 53 | PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, |
| 54 | bfd_byte *, boolean, asymbol **)); |
| 55 | |
| 56 | /* Swaps the information in an executable header taken from a raw byte |
| 57 | stream memory image, into the internal exec_header structure. */ |
| 58 | |
| 59 | void |
| 60 | bout_swap_exec_header_in (abfd, raw_bytes, execp) |
| 61 | bfd *abfd; |
| 62 | struct external_exec *raw_bytes; |
| 63 | struct internal_exec *execp; |
| 64 | { |
| 65 | struct external_exec *bytes = (struct external_exec *)raw_bytes; |
| 66 | |
| 67 | /* Now fill in fields in the execp, from the bytes in the raw data. */ |
| 68 | execp->a_info = bfd_h_get_32 (abfd, bytes->e_info); |
| 69 | execp->a_text = GET_WORD (abfd, bytes->e_text); |
| 70 | execp->a_data = GET_WORD (abfd, bytes->e_data); |
| 71 | execp->a_bss = GET_WORD (abfd, bytes->e_bss); |
| 72 | execp->a_syms = GET_WORD (abfd, bytes->e_syms); |
| 73 | execp->a_entry = GET_WORD (abfd, bytes->e_entry); |
| 74 | execp->a_trsize = GET_WORD (abfd, bytes->e_trsize); |
| 75 | execp->a_drsize = GET_WORD (abfd, bytes->e_drsize); |
| 76 | execp->a_tload = GET_WORD (abfd, bytes->e_tload); |
| 77 | execp->a_dload = GET_WORD (abfd, bytes->e_dload); |
| 78 | execp->a_talign = bytes->e_talign[0]; |
| 79 | execp->a_dalign = bytes->e_dalign[0]; |
| 80 | execp->a_balign = bytes->e_balign[0]; |
| 81 | execp->a_relaxable = bytes->e_relaxable[0]; |
| 82 | } |
| 83 | |
| 84 | /* Swaps the information in an internal exec header structure into the |
| 85 | supplied buffer ready for writing to disk. */ |
| 86 | |
| 87 | PROTO(void, bout_swap_exec_header_out, |
| 88 | (bfd *abfd, |
| 89 | struct internal_exec *execp, |
| 90 | struct external_exec *raw_bytes)); |
| 91 | void |
| 92 | bout_swap_exec_header_out (abfd, execp, raw_bytes) |
| 93 | bfd *abfd; |
| 94 | struct internal_exec *execp; |
| 95 | struct external_exec *raw_bytes; |
| 96 | { |
| 97 | struct external_exec *bytes = (struct external_exec *)raw_bytes; |
| 98 | |
| 99 | /* Now fill in fields in the raw data, from the fields in the exec struct. */ |
| 100 | bfd_h_put_32 (abfd, execp->a_info , bytes->e_info); |
| 101 | PUT_WORD (abfd, execp->a_text , bytes->e_text); |
| 102 | PUT_WORD (abfd, execp->a_data , bytes->e_data); |
| 103 | PUT_WORD (abfd, execp->a_bss , bytes->e_bss); |
| 104 | PUT_WORD (abfd, execp->a_syms , bytes->e_syms); |
| 105 | PUT_WORD (abfd, execp->a_entry , bytes->e_entry); |
| 106 | PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize); |
| 107 | PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize); |
| 108 | PUT_WORD (abfd, execp->a_tload , bytes->e_tload); |
| 109 | PUT_WORD (abfd, execp->a_dload , bytes->e_dload); |
| 110 | bytes->e_talign[0] = execp->a_talign; |
| 111 | bytes->e_dalign[0] = execp->a_dalign; |
| 112 | bytes->e_balign[0] = execp->a_balign; |
| 113 | bytes->e_relaxable[0] = execp->a_relaxable; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static const bfd_target * |
| 118 | b_out_object_p (abfd) |
| 119 | bfd *abfd; |
| 120 | { |
| 121 | struct internal_exec anexec; |
| 122 | struct external_exec exec_bytes; |
| 123 | |
| 124 | if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd) |
| 125 | != EXEC_BYTES_SIZE) { |
| 126 | if (bfd_get_error () != bfd_error_system_call) |
| 127 | bfd_set_error (bfd_error_wrong_format); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info); |
| 132 | |
| 133 | if (N_BADMAG (anexec)) { |
| 134 | bfd_set_error (bfd_error_wrong_format); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | bout_swap_exec_header_in (abfd, &exec_bytes, &anexec); |
| 139 | return aout_32_some_aout_object_p (abfd, &anexec, b_out_callback); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /* Finish up the opening of a b.out file for reading. Fill in all the |
| 144 | fields that are not handled by common code. */ |
| 145 | |
| 146 | static const bfd_target * |
| 147 | b_out_callback (abfd) |
| 148 | bfd *abfd; |
| 149 | { |
| 150 | struct internal_exec *execp = exec_hdr (abfd); |
| 151 | unsigned long bss_start; |
| 152 | |
| 153 | /* Architecture and machine type */ |
| 154 | bfd_set_arch_mach(abfd, |
| 155 | bfd_arch_i960, /* B.out only used on i960 */ |
| 156 | bfd_mach_i960_core /* Default */ |
| 157 | ); |
| 158 | |
| 159 | /* The positions of the string table and symbol table. */ |
| 160 | obj_str_filepos (abfd) = N_STROFF (*execp); |
| 161 | obj_sym_filepos (abfd) = N_SYMOFF (*execp); |
| 162 | |
| 163 | /* The alignments of the sections */ |
| 164 | obj_textsec (abfd)->alignment_power = execp->a_talign; |
| 165 | obj_datasec (abfd)->alignment_power = execp->a_dalign; |
| 166 | obj_bsssec (abfd)->alignment_power = execp->a_balign; |
| 167 | |
| 168 | /* The starting addresses of the sections. */ |
| 169 | obj_textsec (abfd)->vma = execp->a_tload; |
| 170 | obj_datasec (abfd)->vma = execp->a_dload; |
| 171 | |
| 172 | /* And reload the sizes, since the aout module zaps them */ |
| 173 | obj_textsec (abfd)->_raw_size = execp->a_text; |
| 174 | |
| 175 | bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section */ |
| 176 | obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign); |
| 177 | |
| 178 | /* The file positions of the sections */ |
| 179 | obj_textsec (abfd)->filepos = N_TXTOFF(*execp); |
| 180 | obj_datasec (abfd)->filepos = N_DATOFF(*execp); |
| 181 | |
| 182 | /* The file positions of the relocation info */ |
| 183 | obj_textsec (abfd)->rel_filepos = N_TROFF(*execp); |
| 184 | obj_datasec (abfd)->rel_filepos = N_DROFF(*execp); |
| 185 | |
| 186 | adata(abfd).page_size = 1; /* Not applicable. */ |
| 187 | adata(abfd).segment_size = 1; /* Not applicable. */ |
| 188 | adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE; |
| 189 | |
| 190 | if (execp->a_relaxable) |
| 191 | abfd->flags |= BFD_IS_RELAXABLE; |
| 192 | return abfd->xvec; |
| 193 | } |
| 194 | |
| 195 | struct bout_data_struct { |
| 196 | struct aoutdata a; |
| 197 | struct internal_exec e; |
| 198 | }; |
| 199 | |
| 200 | static boolean |
| 201 | b_out_mkobject (abfd) |
| 202 | bfd *abfd; |
| 203 | { |
| 204 | struct bout_data_struct *rawptr; |
| 205 | |
| 206 | rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct)); |
| 207 | if (rawptr == NULL) |
| 208 | return false; |
| 209 | |
| 210 | abfd->tdata.bout_data = rawptr; |
| 211 | exec_hdr (abfd) = &rawptr->e; |
| 212 | |
| 213 | obj_textsec (abfd) = (asection *)NULL; |
| 214 | obj_datasec (abfd) = (asection *)NULL; |
| 215 | obj_bsssec (abfd) = (asection *)NULL; |
| 216 | |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | static boolean |
| 221 | b_out_write_object_contents (abfd) |
| 222 | bfd *abfd; |
| 223 | { |
| 224 | struct external_exec swapped_hdr; |
| 225 | |
| 226 | if (! aout_32_make_sections (abfd)) |
| 227 | return false; |
| 228 | |
| 229 | exec_hdr (abfd)->a_info = BMAGIC; |
| 230 | |
| 231 | exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size; |
| 232 | exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size; |
| 233 | exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size; |
| 234 | exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist); |
| 235 | exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd); |
| 236 | exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) * |
| 237 | sizeof (struct relocation_info)); |
| 238 | exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) * |
| 239 | sizeof (struct relocation_info)); |
| 240 | |
| 241 | exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power; |
| 242 | exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power; |
| 243 | exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power; |
| 244 | |
| 245 | exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma; |
| 246 | exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma; |
| 247 | |
| 248 | bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr); |
| 249 | |
| 250 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 |
| 251 | || (bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd) |
| 252 | != EXEC_BYTES_SIZE)) |
| 253 | return false; |
| 254 | |
| 255 | /* Now write out reloc info, followed by syms and strings */ |
| 256 | if (bfd_get_symcount (abfd) != 0) |
| 257 | { |
| 258 | if (bfd_seek (abfd, (file_ptr)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET) |
| 259 | != 0) |
| 260 | return false; |
| 261 | |
| 262 | if (! aout_32_write_syms (abfd)) |
| 263 | return false; |
| 264 | |
| 265 | if (bfd_seek (abfd, (file_ptr)(N_TROFF(*exec_hdr(abfd))), SEEK_SET) != 0) |
| 266 | return false; |
| 267 | |
| 268 | if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) return false; |
| 269 | if (bfd_seek (abfd, (file_ptr)(N_DROFF(*exec_hdr(abfd))), SEEK_SET) |
| 270 | != 0) |
| 271 | return false; |
| 272 | |
| 273 | if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd))) return false; |
| 274 | } |
| 275 | return true; |
| 276 | } |
| 277 | \f |
| 278 | /** Some reloc hackery */ |
| 279 | |
| 280 | #define CALLS 0x66003800 /* Template for 'calls' instruction */ |
| 281 | #define BAL 0x0b000000 /* Template for 'bal' instruction */ |
| 282 | #define BALX 0x85000000 /* Template for 'balx' instruction */ |
| 283 | #define BAL_MASK 0x00ffffff |
| 284 | #define CALL 0x09000000 |
| 285 | #define PCREL13_MASK 0x1fff |
| 286 | |
| 287 | |
| 288 | #define output_addr(sec) ((sec)->output_offset+(sec)->output_section->vma) |
| 289 | |
| 290 | /* Magic to turn callx into calljx */ |
| 291 | static bfd_reloc_status_type |
| 292 | calljx_callback (abfd, link_info, reloc_entry, src, dst, input_section) |
| 293 | bfd *abfd; |
| 294 | struct bfd_link_info *link_info; |
| 295 | arelent *reloc_entry; |
| 296 | PTR src; |
| 297 | PTR dst; |
| 298 | asection *input_section; |
| 299 | { |
| 300 | int word = bfd_get_32 (abfd, src); |
| 301 | asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr); |
| 302 | aout_symbol_type *symbol = aout_symbol (symbol_in); |
| 303 | bfd_vma value; |
| 304 | |
| 305 | value = get_value (reloc_entry, link_info, input_section); |
| 306 | |
| 307 | if (IS_CALLNAME (symbol->other)) |
| 308 | { |
| 309 | aout_symbol_type *balsym = symbol+1; |
| 310 | int inst = bfd_get_32 (abfd, (bfd_byte *) src-4); |
| 311 | /* The next symbol should be an N_BALNAME */ |
| 312 | BFD_ASSERT (IS_BALNAME (balsym->other)); |
| 313 | inst &= BAL_MASK; |
| 314 | inst |= BALX; |
| 315 | bfd_put_32 (abfd, inst, (bfd_byte *) dst-4); |
| 316 | symbol = balsym; |
| 317 | value = (symbol->symbol.value |
| 318 | + output_addr (symbol->symbol.section)); |
| 319 | } |
| 320 | |
| 321 | word += value + reloc_entry->addend; |
| 322 | |
| 323 | bfd_put_32(abfd, word, dst); |
| 324 | return bfd_reloc_ok; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* Magic to turn call into callj */ |
| 329 | static bfd_reloc_status_type |
| 330 | callj_callback (abfd, link_info, reloc_entry, data, srcidx, dstidx, |
| 331 | input_section, shrinking) |
| 332 | bfd *abfd; |
| 333 | struct bfd_link_info *link_info; |
| 334 | arelent *reloc_entry; |
| 335 | PTR data; |
| 336 | unsigned int srcidx; |
| 337 | unsigned int dstidx; |
| 338 | asection *input_section; |
| 339 | boolean shrinking; |
| 340 | { |
| 341 | int word = bfd_get_32 (abfd, (bfd_byte *) data + srcidx); |
| 342 | asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr); |
| 343 | aout_symbol_type *symbol = aout_symbol (symbol_in); |
| 344 | bfd_vma value; |
| 345 | |
| 346 | value = get_value (reloc_entry, link_info, input_section); |
| 347 | |
| 348 | if (IS_OTHER(symbol->other)) |
| 349 | { |
| 350 | /* Call to a system procedure - replace code with system |
| 351 | procedure number. */ |
| 352 | word = CALLS | (symbol->other - 1); |
| 353 | } |
| 354 | else if (IS_CALLNAME(symbol->other)) |
| 355 | { |
| 356 | aout_symbol_type *balsym = symbol+1; |
| 357 | |
| 358 | /* The next symbol should be an N_BALNAME. */ |
| 359 | BFD_ASSERT(IS_BALNAME(balsym->other)); |
| 360 | |
| 361 | /* We are calling a leaf, so replace the call instruction with a |
| 362 | bal. */ |
| 363 | word = BAL | ((word |
| 364 | + output_addr (balsym->symbol.section) |
| 365 | + balsym->symbol.value + reloc_entry->addend |
| 366 | - dstidx |
| 367 | - output_addr (input_section)) |
| 368 | & BAL_MASK); |
| 369 | } |
| 370 | else if ((symbol->symbol.flags & BSF_SECTION_SYM) != 0) |
| 371 | { |
| 372 | /* A callj against a symbol in the same section is a fully |
| 373 | resolved relative call. We don't need to do anything here. |
| 374 | If the symbol is not in the same section, I'm not sure what |
| 375 | to do; fortunately, this case will probably never arise. */ |
| 376 | BFD_ASSERT (! shrinking); |
| 377 | BFD_ASSERT (symbol->symbol.section == input_section); |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | word = CALL | (((word & BAL_MASK) |
| 382 | + value |
| 383 | + reloc_entry->addend |
| 384 | - (shrinking ? dstidx : 0) |
| 385 | - output_addr (input_section)) |
| 386 | & BAL_MASK); |
| 387 | } |
| 388 | bfd_put_32(abfd, word, (bfd_byte *) data + dstidx); |
| 389 | return bfd_reloc_ok; |
| 390 | } |
| 391 | |
| 392 | /* type rshift size bitsize pcrel bitpos absolute overflow check*/ |
| 393 | |
| 394 | #define ABS32CODE 0 |
| 395 | #define ABS32CODE_SHRUNK 1 |
| 396 | #define PCREL24 2 |
| 397 | #define CALLJ 3 |
| 398 | #define ABS32 4 |
| 399 | #define PCREL13 5 |
| 400 | #define ABS32_MAYBE_RELAXABLE 1 |
| 401 | #define ABS32_WAS_RELAXABLE 2 |
| 402 | |
| 403 | #define ALIGNER 10 |
| 404 | #define ALIGNDONE 11 |
| 405 | static reloc_howto_type howto_reloc_callj = |
| 406 | HOWTO(CALLJ, 0, 2, 24, true, 0, complain_overflow_signed, 0,"callj", true, 0x00ffffff, 0x00ffffff,false); |
| 407 | static reloc_howto_type howto_reloc_abs32 = |
| 408 | HOWTO(ABS32, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"abs32", true, 0xffffffff,0xffffffff,false); |
| 409 | static reloc_howto_type howto_reloc_pcrel24 = |
| 410 | HOWTO(PCREL24, 0, 2, 24, true, 0, complain_overflow_signed,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false); |
| 411 | |
| 412 | static reloc_howto_type howto_reloc_pcrel13 = |
| 413 | HOWTO(PCREL13, 0, 2, 13, true, 0, complain_overflow_signed,0,"pcrel13", true, 0x00001fff,0x00001fff,false); |
| 414 | |
| 415 | |
| 416 | static reloc_howto_type howto_reloc_abs32codeshrunk = |
| 417 | HOWTO(ABS32CODE_SHRUNK, 0, 2, 24, true, 0, complain_overflow_signed, 0,"callx->callj", true, 0x00ffffff, 0x00ffffff,false); |
| 418 | |
| 419 | static reloc_howto_type howto_reloc_abs32code = |
| 420 | HOWTO(ABS32CODE, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"callx", true, 0xffffffff,0xffffffff,false); |
| 421 | |
| 422 | static reloc_howto_type howto_align_table[] = { |
| 423 | HOWTO (ALIGNER, 0, 0x1, 0, false, 0, complain_overflow_dont, 0, "align16", false, 0, 0, false), |
| 424 | HOWTO (ALIGNER, 0, 0x3, 0, false, 0, complain_overflow_dont, 0, "align32", false, 0, 0, false), |
| 425 | HOWTO (ALIGNER, 0, 0x7, 0, false, 0, complain_overflow_dont, 0, "align64", false, 0, 0, false), |
| 426 | HOWTO (ALIGNER, 0, 0xf, 0, false, 0, complain_overflow_dont, 0, "align128", false, 0, 0, false), |
| 427 | }; |
| 428 | |
| 429 | static reloc_howto_type howto_done_align_table[] = { |
| 430 | HOWTO (ALIGNDONE, 0x1, 0x1, 0, false, 0, complain_overflow_dont, 0, "donealign16", false, 0, 0, false), |
| 431 | HOWTO (ALIGNDONE, 0x3, 0x3, 0, false, 0, complain_overflow_dont, 0, "donealign32", false, 0, 0, false), |
| 432 | HOWTO (ALIGNDONE, 0x7, 0x7, 0, false, 0, complain_overflow_dont, 0, "donealign64", false, 0, 0, false), |
| 433 | HOWTO (ALIGNDONE, 0xf, 0xf, 0, false, 0, complain_overflow_dont, 0, "donealign128", false, 0, 0, false), |
| 434 | }; |
| 435 | |
| 436 | static reloc_howto_type * |
| 437 | b_out_bfd_reloc_type_lookup (abfd, code) |
| 438 | bfd *abfd; |
| 439 | bfd_reloc_code_real_type code; |
| 440 | { |
| 441 | switch (code) |
| 442 | { |
| 443 | default: |
| 444 | return 0; |
| 445 | case BFD_RELOC_I960_CALLJ: |
| 446 | return &howto_reloc_callj; |
| 447 | case BFD_RELOC_32: |
| 448 | case BFD_RELOC_CTOR: |
| 449 | return &howto_reloc_abs32; |
| 450 | case BFD_RELOC_24_PCREL: |
| 451 | return &howto_reloc_pcrel24; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /* Allocate enough room for all the reloc entries, plus pointers to them all */ |
| 456 | |
| 457 | static boolean |
| 458 | b_out_slurp_reloc_table (abfd, asect, symbols) |
| 459 | bfd *abfd; |
| 460 | sec_ptr asect; |
| 461 | asymbol **symbols; |
| 462 | { |
| 463 | register struct relocation_info *rptr; |
| 464 | unsigned int counter ; |
| 465 | arelent *cache_ptr ; |
| 466 | int extern_mask, pcrel_mask, callj_mask, length_shift; |
| 467 | int incode_mask; |
| 468 | int size_mask; |
| 469 | bfd_vma prev_addr = 0; |
| 470 | unsigned int count; |
| 471 | size_t reloc_size; |
| 472 | struct relocation_info *relocs; |
| 473 | arelent *reloc_cache; |
| 474 | |
| 475 | if (asect->relocation) |
| 476 | return true; |
| 477 | if (!aout_32_slurp_symbol_table (abfd)) |
| 478 | return false; |
| 479 | |
| 480 | if (asect == obj_datasec (abfd)) { |
| 481 | reloc_size = exec_hdr(abfd)->a_drsize; |
| 482 | goto doit; |
| 483 | } |
| 484 | |
| 485 | if (asect == obj_textsec (abfd)) { |
| 486 | reloc_size = exec_hdr(abfd)->a_trsize; |
| 487 | goto doit; |
| 488 | } |
| 489 | |
| 490 | if (asect == obj_bsssec (abfd)) { |
| 491 | reloc_size = 0; |
| 492 | goto doit; |
| 493 | } |
| 494 | |
| 495 | bfd_set_error (bfd_error_invalid_operation); |
| 496 | return false; |
| 497 | |
| 498 | doit: |
| 499 | if (bfd_seek (abfd, (file_ptr)(asect->rel_filepos), SEEK_SET) != 0) |
| 500 | return false; |
| 501 | count = reloc_size / sizeof (struct relocation_info); |
| 502 | |
| 503 | relocs = (struct relocation_info *) bfd_malloc (reloc_size); |
| 504 | if (!relocs && reloc_size != 0) |
| 505 | return false; |
| 506 | reloc_cache = (arelent *) bfd_malloc ((count+1) * sizeof (arelent)); |
| 507 | if (!reloc_cache) { |
| 508 | if (relocs != NULL) |
| 509 | free ((char*)relocs); |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | if (bfd_read ((PTR) relocs, 1, reloc_size, abfd) != reloc_size) { |
| 514 | free (reloc_cache); |
| 515 | if (relocs != NULL) |
| 516 | free (relocs); |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | |
| 522 | if (bfd_header_big_endian (abfd)) { |
| 523 | /* big-endian bit field allocation order */ |
| 524 | pcrel_mask = 0x80; |
| 525 | extern_mask = 0x10; |
| 526 | incode_mask = 0x08; |
| 527 | callj_mask = 0x02; |
| 528 | size_mask = 0x20; |
| 529 | length_shift = 5; |
| 530 | } else { |
| 531 | /* little-endian bit field allocation order */ |
| 532 | pcrel_mask = 0x01; |
| 533 | extern_mask = 0x08; |
| 534 | incode_mask = 0x10; |
| 535 | callj_mask = 0x40; |
| 536 | size_mask = 0x02; |
| 537 | length_shift = 1; |
| 538 | } |
| 539 | |
| 540 | for (rptr = relocs, cache_ptr = reloc_cache, counter = 0; |
| 541 | counter < count; |
| 542 | counter++, rptr++, cache_ptr++) |
| 543 | { |
| 544 | unsigned char *raw = (unsigned char *)rptr; |
| 545 | unsigned int symnum; |
| 546 | cache_ptr->address = bfd_h_get_32 (abfd, raw + 0); |
| 547 | cache_ptr->howto = 0; |
| 548 | if (bfd_header_big_endian (abfd)) |
| 549 | { |
| 550 | symnum = (raw[4] << 16) | (raw[5] << 8) | raw[6]; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | symnum = (raw[6] << 16) | (raw[5] << 8) | raw[4]; |
| 555 | } |
| 556 | |
| 557 | if (raw[7] & extern_mask) |
| 558 | { |
| 559 | /* if this is set then the r_index is a index into the symbol table; |
| 560 | * if the bit is not set then r_index contains a section map. |
| 561 | * we either fill in the sym entry with a pointer to the symbol, |
| 562 | * or point to the correct section |
| 563 | */ |
| 564 | cache_ptr->sym_ptr_ptr = symbols + symnum; |
| 565 | cache_ptr->addend = 0; |
| 566 | } else |
| 567 | { |
| 568 | /* in a.out symbols are relative to the beginning of the |
| 569 | * file rather than sections ? |
| 570 | * (look in translate_from_native_sym_flags) |
| 571 | * the reloc entry addend has added to it the offset into the |
| 572 | * file of the data, so subtract the base to make the reloc |
| 573 | * section relative */ |
| 574 | int s; |
| 575 | { |
| 576 | /* sign-extend symnum from 24 bits to whatever host uses */ |
| 577 | s = symnum; |
| 578 | if (s & (1 << 23)) |
| 579 | s |= (~0) << 24; |
| 580 | } |
| 581 | cache_ptr->sym_ptr_ptr = (asymbol **)NULL; |
| 582 | switch (s) |
| 583 | { |
| 584 | case N_TEXT: |
| 585 | case N_TEXT | N_EXT: |
| 586 | cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; |
| 587 | cache_ptr->addend = - obj_textsec(abfd)->vma; |
| 588 | break; |
| 589 | case N_DATA: |
| 590 | case N_DATA | N_EXT: |
| 591 | cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; |
| 592 | cache_ptr->addend = - obj_datasec(abfd)->vma; |
| 593 | break; |
| 594 | case N_BSS: |
| 595 | case N_BSS | N_EXT: |
| 596 | cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; |
| 597 | cache_ptr->addend = - obj_bsssec(abfd)->vma; |
| 598 | break; |
| 599 | case N_ABS: |
| 600 | case N_ABS | N_EXT: |
| 601 | cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; |
| 602 | cache_ptr->addend = 0; |
| 603 | break; |
| 604 | case -2: /* .align */ |
| 605 | if (raw[7] & pcrel_mask) |
| 606 | { |
| 607 | cache_ptr->howto = &howto_align_table[(raw[7] >> length_shift) & 3]; |
| 608 | cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | /* .org? */ |
| 613 | abort (); |
| 614 | } |
| 615 | cache_ptr->addend = 0; |
| 616 | break; |
| 617 | default: |
| 618 | BFD_ASSERT(0); |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | } |
| 623 | |
| 624 | /* the i960 only has a few relocation types: |
| 625 | abs 32-bit and pcrel 24bit. except for callj's! */ |
| 626 | if (cache_ptr->howto != 0) |
| 627 | ; |
| 628 | else if (raw[7] & callj_mask) |
| 629 | { |
| 630 | cache_ptr->howto = &howto_reloc_callj; |
| 631 | } |
| 632 | else if ( raw[7] & pcrel_mask) |
| 633 | { |
| 634 | if (raw[7] & size_mask) |
| 635 | cache_ptr->howto = &howto_reloc_pcrel13; |
| 636 | else |
| 637 | cache_ptr->howto = &howto_reloc_pcrel24; |
| 638 | } |
| 639 | else |
| 640 | { |
| 641 | if (raw[7] & incode_mask) |
| 642 | { |
| 643 | cache_ptr->howto = &howto_reloc_abs32code; |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | cache_ptr->howto = &howto_reloc_abs32; |
| 648 | } |
| 649 | } |
| 650 | if (cache_ptr->address < prev_addr) |
| 651 | { |
| 652 | /* Ouch! this reloc is out of order, insert into the right place |
| 653 | */ |
| 654 | arelent tmp; |
| 655 | arelent *cursor = cache_ptr-1; |
| 656 | bfd_vma stop = cache_ptr->address; |
| 657 | tmp = *cache_ptr; |
| 658 | while (cursor->address > stop && cursor >= reloc_cache) |
| 659 | { |
| 660 | cursor[1] = cursor[0]; |
| 661 | cursor--; |
| 662 | } |
| 663 | cursor[1] = tmp; |
| 664 | } |
| 665 | else |
| 666 | { |
| 667 | prev_addr = cache_ptr->address; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | |
| 672 | if (relocs != NULL) |
| 673 | free (relocs); |
| 674 | asect->relocation = reloc_cache; |
| 675 | asect->reloc_count = count; |
| 676 | |
| 677 | |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | |
| 682 | static boolean |
| 683 | b_out_squirt_out_relocs (abfd, section) |
| 684 | bfd *abfd; |
| 685 | asection *section; |
| 686 | { |
| 687 | arelent **generic; |
| 688 | int r_extern = 0; |
| 689 | int r_idx; |
| 690 | int incode_mask; |
| 691 | int len_1; |
| 692 | unsigned int count = section->reloc_count; |
| 693 | struct relocation_info *native, *natptr; |
| 694 | size_t natsize = count * sizeof (struct relocation_info); |
| 695 | int extern_mask, pcrel_mask, len_2, callj_mask; |
| 696 | if (count == 0) return true; |
| 697 | generic = section->orelocation; |
| 698 | native = ((struct relocation_info *) bfd_malloc (natsize)); |
| 699 | if (!native && natsize != 0) |
| 700 | return false; |
| 701 | |
| 702 | if (bfd_header_big_endian (abfd)) |
| 703 | { |
| 704 | /* Big-endian bit field allocation order */ |
| 705 | pcrel_mask = 0x80; |
| 706 | extern_mask = 0x10; |
| 707 | len_2 = 0x40; |
| 708 | len_1 = 0x20; |
| 709 | callj_mask = 0x02; |
| 710 | incode_mask = 0x08; |
| 711 | } |
| 712 | else |
| 713 | { |
| 714 | /* Little-endian bit field allocation order */ |
| 715 | pcrel_mask = 0x01; |
| 716 | extern_mask = 0x08; |
| 717 | len_2 = 0x04; |
| 718 | len_1 = 0x02; |
| 719 | callj_mask = 0x40; |
| 720 | incode_mask = 0x10; |
| 721 | } |
| 722 | |
| 723 | for (natptr = native; count > 0; --count, ++natptr, ++generic) |
| 724 | { |
| 725 | arelent *g = *generic; |
| 726 | unsigned char *raw = (unsigned char *)natptr; |
| 727 | asymbol *sym = *(g->sym_ptr_ptr); |
| 728 | |
| 729 | asection *output_section = sym->section->output_section; |
| 730 | |
| 731 | bfd_h_put_32(abfd, g->address, raw); |
| 732 | /* Find a type in the output format which matches the input howto - |
| 733 | * at the moment we assume input format == output format FIXME!! |
| 734 | */ |
| 735 | r_idx = 0; |
| 736 | /* FIXME: Need callj stuff here, and to check the howto entries to |
| 737 | be sure they are real for this architecture. */ |
| 738 | if (g->howto== &howto_reloc_callj) |
| 739 | { |
| 740 | raw[7] = callj_mask + pcrel_mask + len_2; |
| 741 | } |
| 742 | else if (g->howto == &howto_reloc_pcrel24) |
| 743 | { |
| 744 | raw[7] = pcrel_mask + len_2; |
| 745 | } |
| 746 | else if (g->howto == &howto_reloc_pcrel13) |
| 747 | { |
| 748 | raw[7] = pcrel_mask + len_1; |
| 749 | } |
| 750 | else if (g->howto == &howto_reloc_abs32code) |
| 751 | { |
| 752 | raw[7] = len_2 + incode_mask; |
| 753 | } |
| 754 | else if (g->howto >= howto_align_table |
| 755 | && g->howto <= (howto_align_table |
| 756 | + sizeof (howto_align_table) / sizeof (howto_align_table[0]) |
| 757 | - 1)) |
| 758 | { |
| 759 | /* symnum == -2; extern_mask not set, pcrel_mask set */ |
| 760 | r_idx = -2; |
| 761 | r_extern = 0; |
| 762 | raw[7] = (pcrel_mask |
| 763 | | ((g->howto - howto_align_table) << 1)); |
| 764 | } |
| 765 | else { |
| 766 | raw[7] = len_2; |
| 767 | } |
| 768 | |
| 769 | if (r_idx != 0) |
| 770 | /* already mucked with r_extern, r_idx */; |
| 771 | else if (bfd_is_com_section (output_section) |
| 772 | || bfd_is_abs_section (output_section) |
| 773 | || bfd_is_und_section (output_section)) |
| 774 | { |
| 775 | |
| 776 | if (bfd_abs_section_ptr->symbol == sym) |
| 777 | { |
| 778 | /* Whoops, looked like an abs symbol, but is really an offset |
| 779 | from the abs section */ |
| 780 | r_idx = 0; |
| 781 | r_extern = 0; |
| 782 | } |
| 783 | else |
| 784 | { |
| 785 | /* Fill in symbol */ |
| 786 | |
| 787 | r_extern = 1; |
| 788 | r_idx = stoi((*(g->sym_ptr_ptr))->flags); |
| 789 | } |
| 790 | } |
| 791 | else |
| 792 | { |
| 793 | /* Just an ordinary section */ |
| 794 | r_extern = 0; |
| 795 | r_idx = output_section->target_index; |
| 796 | } |
| 797 | |
| 798 | if (bfd_header_big_endian (abfd)) { |
| 799 | raw[4] = (unsigned char) (r_idx >> 16); |
| 800 | raw[5] = (unsigned char) (r_idx >> 8); |
| 801 | raw[6] = (unsigned char) (r_idx ); |
| 802 | } else { |
| 803 | raw[6] = (unsigned char) (r_idx >> 16); |
| 804 | raw[5] = (unsigned char) (r_idx>> 8); |
| 805 | raw[4] = (unsigned char) (r_idx ); |
| 806 | } |
| 807 | if (r_extern) |
| 808 | raw[7] |= extern_mask; |
| 809 | } |
| 810 | |
| 811 | if (bfd_write ((PTR) native, 1, natsize, abfd) != natsize) { |
| 812 | free((PTR)native); |
| 813 | return false; |
| 814 | } |
| 815 | free ((PTR)native); |
| 816 | |
| 817 | return true; |
| 818 | } |
| 819 | |
| 820 | /* This is stupid. This function should be a boolean predicate */ |
| 821 | static long |
| 822 | b_out_canonicalize_reloc (abfd, section, relptr, symbols) |
| 823 | bfd *abfd; |
| 824 | sec_ptr section; |
| 825 | arelent **relptr; |
| 826 | asymbol **symbols; |
| 827 | { |
| 828 | arelent *tblptr; |
| 829 | unsigned int count; |
| 830 | |
| 831 | if ((section->flags & SEC_CONSTRUCTOR) != 0) |
| 832 | { |
| 833 | arelent_chain *chain = section->constructor_chain; |
| 834 | for (count = 0; count < section->reloc_count; count++) |
| 835 | { |
| 836 | *relptr++ = &chain->relent; |
| 837 | chain = chain->next; |
| 838 | } |
| 839 | } |
| 840 | else |
| 841 | { |
| 842 | if (section->relocation == NULL |
| 843 | && ! b_out_slurp_reloc_table (abfd, section, symbols)) |
| 844 | return -1; |
| 845 | |
| 846 | tblptr = section->relocation; |
| 847 | for (count = 0; count++ < section->reloc_count;) |
| 848 | *relptr++ = tblptr++; |
| 849 | } |
| 850 | |
| 851 | *relptr = NULL; |
| 852 | |
| 853 | return section->reloc_count; |
| 854 | } |
| 855 | |
| 856 | static long |
| 857 | b_out_get_reloc_upper_bound (abfd, asect) |
| 858 | bfd *abfd; |
| 859 | sec_ptr asect; |
| 860 | { |
| 861 | if (bfd_get_format (abfd) != bfd_object) { |
| 862 | bfd_set_error (bfd_error_invalid_operation); |
| 863 | return -1; |
| 864 | } |
| 865 | |
| 866 | if (asect->flags & SEC_CONSTRUCTOR) |
| 867 | return sizeof (arelent *) * (asect->reloc_count + 1); |
| 868 | |
| 869 | if (asect == obj_datasec (abfd)) |
| 870 | return (sizeof (arelent *) * |
| 871 | ((exec_hdr(abfd)->a_drsize / sizeof (struct relocation_info)) |
| 872 | +1)); |
| 873 | |
| 874 | if (asect == obj_textsec (abfd)) |
| 875 | return (sizeof (arelent *) * |
| 876 | ((exec_hdr(abfd)->a_trsize / sizeof (struct relocation_info)) |
| 877 | +1)); |
| 878 | |
| 879 | if (asect == obj_bsssec (abfd)) |
| 880 | return 0; |
| 881 | |
| 882 | bfd_set_error (bfd_error_invalid_operation); |
| 883 | return -1; |
| 884 | } |
| 885 | \f |
| 886 | static boolean |
| 887 | b_out_set_section_contents (abfd, section, location, offset, count) |
| 888 | bfd *abfd; |
| 889 | asection *section; |
| 890 | PTR location; |
| 891 | file_ptr offset; |
| 892 | bfd_size_type count; |
| 893 | { |
| 894 | |
| 895 | if (abfd->output_has_begun == false) { /* set by bfd.c handler */ |
| 896 | if (! aout_32_make_sections (abfd)) |
| 897 | return false; |
| 898 | |
| 899 | obj_textsec (abfd)->filepos = sizeof(struct internal_exec); |
| 900 | obj_datasec(abfd)->filepos = obj_textsec(abfd)->filepos |
| 901 | + obj_textsec (abfd)->_raw_size; |
| 902 | |
| 903 | } |
| 904 | /* regardless, once we know what we're doing, we might as well get going */ |
| 905 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0) |
| 906 | return false; |
| 907 | |
| 908 | if (count != 0) { |
| 909 | return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false; |
| 910 | } |
| 911 | return true; |
| 912 | } |
| 913 | |
| 914 | static boolean |
| 915 | b_out_set_arch_mach (abfd, arch, machine) |
| 916 | bfd *abfd; |
| 917 | enum bfd_architecture arch; |
| 918 | unsigned long machine; |
| 919 | { |
| 920 | bfd_default_set_arch_mach(abfd, arch, machine); |
| 921 | |
| 922 | if (arch == bfd_arch_unknown) /* Unknown machine arch is OK */ |
| 923 | return true; |
| 924 | if (arch == bfd_arch_i960) /* i960 default is OK */ |
| 925 | switch (machine) { |
| 926 | case bfd_mach_i960_core: |
| 927 | case bfd_mach_i960_kb_sb: |
| 928 | case bfd_mach_i960_mc: |
| 929 | case bfd_mach_i960_xa: |
| 930 | case bfd_mach_i960_ca: |
| 931 | case bfd_mach_i960_ka_sa: |
| 932 | case bfd_mach_i960_jx: |
| 933 | case bfd_mach_i960_hx: |
| 934 | case 0: |
| 935 | return true; |
| 936 | default: |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | return false; |
| 941 | } |
| 942 | |
| 943 | static int |
| 944 | b_out_sizeof_headers (ignore_abfd, ignore) |
| 945 | bfd *ignore_abfd; |
| 946 | boolean ignore; |
| 947 | { |
| 948 | return sizeof(struct internal_exec); |
| 949 | } |
| 950 | |
| 951 | |
| 952 | |
| 953 | /************************************************************************/ |
| 954 | static bfd_vma |
| 955 | get_value (reloc, link_info, input_section) |
| 956 | arelent *reloc; |
| 957 | struct bfd_link_info *link_info; |
| 958 | asection *input_section; |
| 959 | { |
| 960 | bfd_vma value; |
| 961 | asymbol *symbol = *(reloc->sym_ptr_ptr); |
| 962 | |
| 963 | /* A symbol holds a pointer to a section, and an offset from the |
| 964 | base of the section. To relocate, we find where the section will |
| 965 | live in the output and add that in */ |
| 966 | |
| 967 | if (bfd_is_und_section (symbol->section)) |
| 968 | { |
| 969 | struct bfd_link_hash_entry *h; |
| 970 | |
| 971 | /* The symbol is undefined in this BFD. Look it up in the |
| 972 | global linker hash table. FIXME: This should be changed when |
| 973 | we convert b.out to use a specific final_link function and |
| 974 | change the interface to bfd_relax_section to not require the |
| 975 | generic symbols. */ |
| 976 | h = bfd_link_hash_lookup (link_info->hash, bfd_asymbol_name (symbol), |
| 977 | false, false, true); |
| 978 | if (h != (struct bfd_link_hash_entry *) NULL |
| 979 | && (h->type == bfd_link_hash_defined |
| 980 | || h->type == bfd_link_hash_defweak)) |
| 981 | value = h->u.def.value + output_addr (h->u.def.section); |
| 982 | else if (h != (struct bfd_link_hash_entry *) NULL |
| 983 | && h->type == bfd_link_hash_common) |
| 984 | value = h->u.c.size; |
| 985 | else |
| 986 | { |
| 987 | if (! ((*link_info->callbacks->undefined_symbol) |
| 988 | (link_info, bfd_asymbol_name (symbol), |
| 989 | input_section->owner, input_section, reloc->address))) |
| 990 | abort (); |
| 991 | value = 0; |
| 992 | } |
| 993 | } |
| 994 | else |
| 995 | { |
| 996 | value = symbol->value + output_addr (symbol->section); |
| 997 | } |
| 998 | |
| 999 | /* Add the value contained in the relocation */ |
| 1000 | value += reloc->addend; |
| 1001 | |
| 1002 | return value; |
| 1003 | } |
| 1004 | |
| 1005 | static void |
| 1006 | perform_slip (abfd, slip, input_section, value) |
| 1007 | bfd *abfd; |
| 1008 | unsigned int slip; |
| 1009 | asection *input_section; |
| 1010 | bfd_vma value; |
| 1011 | { |
| 1012 | asymbol **s; |
| 1013 | |
| 1014 | s = _bfd_generic_link_get_symbols (abfd); |
| 1015 | BFD_ASSERT (s != (asymbol **) NULL); |
| 1016 | |
| 1017 | /* Find all symbols past this point, and make them know |
| 1018 | what's happened */ |
| 1019 | while (*s) |
| 1020 | { |
| 1021 | asymbol *p = *s; |
| 1022 | if (p->section == input_section) |
| 1023 | { |
| 1024 | /* This was pointing into this section, so mangle it */ |
| 1025 | if (p->value > value) |
| 1026 | { |
| 1027 | p->value -=slip; |
| 1028 | if (p->udata.p != NULL) |
| 1029 | { |
| 1030 | struct generic_link_hash_entry *h; |
| 1031 | |
| 1032 | h = (struct generic_link_hash_entry *) p->udata.p; |
| 1033 | BFD_ASSERT (h->root.type == bfd_link_hash_defined); |
| 1034 | h->root.u.def.value -= slip; |
| 1035 | BFD_ASSERT (h->root.u.def.value == p->value); |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | s++; |
| 1040 | |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | /* This routine works out if the thing we want to get to can be |
| 1045 | reached with a 24bit offset instead of a 32 bit one. |
| 1046 | If it can, then it changes the amode */ |
| 1047 | |
| 1048 | static int |
| 1049 | abs32code (abfd, input_section, r, shrink, link_info) |
| 1050 | bfd *abfd; |
| 1051 | asection *input_section; |
| 1052 | arelent *r; |
| 1053 | unsigned int shrink; |
| 1054 | struct bfd_link_info *link_info; |
| 1055 | { |
| 1056 | bfd_vma value = get_value (r, link_info, input_section); |
| 1057 | bfd_vma dot = output_addr (input_section) + r->address; |
| 1058 | bfd_vma gap; |
| 1059 | |
| 1060 | /* See if the address we're looking at within 2^23 bytes of where |
| 1061 | we are, if so then we can use a small branch rather than the |
| 1062 | jump we were going to */ |
| 1063 | |
| 1064 | gap = value - (dot - shrink); |
| 1065 | |
| 1066 | |
| 1067 | if (-1<<23 < (long)gap && (long)gap < 1<<23 ) |
| 1068 | { |
| 1069 | /* Change the reloc type from 32bitcode possible 24, to 24bit |
| 1070 | possible 32 */ |
| 1071 | |
| 1072 | r->howto = &howto_reloc_abs32codeshrunk; |
| 1073 | /* The place to relc moves back by four bytes */ |
| 1074 | r->address -=4; |
| 1075 | |
| 1076 | /* This will be four bytes smaller in the long run */ |
| 1077 | shrink += 4 ; |
| 1078 | perform_slip (abfd, 4, input_section, r->address-shrink + 4); |
| 1079 | } |
| 1080 | return shrink; |
| 1081 | } |
| 1082 | |
| 1083 | static int |
| 1084 | aligncode (abfd, input_section, r, shrink) |
| 1085 | bfd *abfd; |
| 1086 | asection *input_section; |
| 1087 | arelent *r; |
| 1088 | unsigned int shrink; |
| 1089 | { |
| 1090 | bfd_vma dot = output_addr (input_section) + r->address; |
| 1091 | bfd_vma gap; |
| 1092 | bfd_vma old_end; |
| 1093 | bfd_vma new_end; |
| 1094 | int shrink_delta; |
| 1095 | int size = r->howto->size; |
| 1096 | |
| 1097 | /* Reduce the size of the alignment so that it's still aligned but |
| 1098 | smaller - the current size is already the same size as or bigger |
| 1099 | than the alignment required. */ |
| 1100 | |
| 1101 | /* calculate the first byte following the padding before we optimize */ |
| 1102 | old_end = ((dot + size ) & ~size) + size+1; |
| 1103 | /* work out where the new end will be - remember that we're smaller |
| 1104 | than we used to be */ |
| 1105 | new_end = ((dot - shrink + size) & ~size); |
| 1106 | |
| 1107 | /* This is the new end */ |
| 1108 | gap = old_end - ((dot + size) & ~size); |
| 1109 | |
| 1110 | shrink_delta = (old_end - new_end) - shrink; |
| 1111 | |
| 1112 | if (shrink_delta) |
| 1113 | { |
| 1114 | /* Change the reloc so that it knows how far to align to */ |
| 1115 | r->howto = howto_done_align_table + (r->howto - howto_align_table); |
| 1116 | |
| 1117 | /* Encode the stuff into the addend - for future use we need to |
| 1118 | know how big the reloc used to be */ |
| 1119 | r->addend = old_end - dot + r->address; |
| 1120 | |
| 1121 | /* This will be N bytes smaller in the long run, adjust all the symbols */ |
| 1122 | perform_slip (abfd, shrink_delta, input_section, r->address - shrink); |
| 1123 | shrink += shrink_delta; |
| 1124 | } |
| 1125 | return shrink; |
| 1126 | } |
| 1127 | |
| 1128 | static boolean |
| 1129 | b_out_bfd_relax_section (abfd, i, link_info, again) |
| 1130 | bfd *abfd; |
| 1131 | asection *i; |
| 1132 | struct bfd_link_info *link_info; |
| 1133 | boolean *again; |
| 1134 | { |
| 1135 | /* Get enough memory to hold the stuff */ |
| 1136 | bfd *input_bfd = i->owner; |
| 1137 | asection *input_section = i; |
| 1138 | int shrink = 0 ; |
| 1139 | arelent **reloc_vector = NULL; |
| 1140 | long reloc_size = bfd_get_reloc_upper_bound(input_bfd, |
| 1141 | input_section); |
| 1142 | |
| 1143 | if (reloc_size < 0) |
| 1144 | return false; |
| 1145 | |
| 1146 | /* We only run this relaxation once. It might work to run it |
| 1147 | multiple times, but it hasn't been tested. */ |
| 1148 | *again = false; |
| 1149 | |
| 1150 | if (reloc_size) |
| 1151 | { |
| 1152 | long reloc_count; |
| 1153 | |
| 1154 | reloc_vector = (arelent **) bfd_malloc (reloc_size); |
| 1155 | if (reloc_vector == NULL && reloc_size != 0) |
| 1156 | goto error_return; |
| 1157 | |
| 1158 | /* Get the relocs and think about them */ |
| 1159 | reloc_count = |
| 1160 | bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector, |
| 1161 | _bfd_generic_link_get_symbols (input_bfd)); |
| 1162 | if (reloc_count < 0) |
| 1163 | goto error_return; |
| 1164 | if (reloc_count > 0) |
| 1165 | { |
| 1166 | arelent **parent; |
| 1167 | for (parent = reloc_vector; *parent; parent++) |
| 1168 | { |
| 1169 | arelent *r = *parent; |
| 1170 | switch (r->howto->type) |
| 1171 | { |
| 1172 | case ALIGNER: |
| 1173 | /* An alignment reloc */ |
| 1174 | shrink = aligncode (abfd, input_section, r, shrink); |
| 1175 | break; |
| 1176 | case ABS32CODE: |
| 1177 | /* A 32bit reloc in an addressing mode */ |
| 1178 | shrink = abs32code (input_bfd, input_section, r, shrink, |
| 1179 | link_info); |
| 1180 | break; |
| 1181 | case ABS32CODE_SHRUNK: |
| 1182 | shrink+=4; |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | input_section->_cooked_size = input_section->_raw_size - shrink; |
| 1189 | |
| 1190 | if (reloc_vector != NULL) |
| 1191 | free (reloc_vector); |
| 1192 | return true; |
| 1193 | error_return: |
| 1194 | if (reloc_vector != NULL) |
| 1195 | free (reloc_vector); |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | static bfd_byte * |
| 1200 | b_out_bfd_get_relocated_section_contents (in_abfd, link_info, link_order, |
| 1201 | data, relocateable, symbols) |
| 1202 | bfd *in_abfd; |
| 1203 | struct bfd_link_info *link_info; |
| 1204 | struct bfd_link_order *link_order; |
| 1205 | bfd_byte *data; |
| 1206 | boolean relocateable; |
| 1207 | asymbol **symbols; |
| 1208 | { |
| 1209 | /* Get enough memory to hold the stuff */ |
| 1210 | bfd *input_bfd = link_order->u.indirect.section->owner; |
| 1211 | asection *input_section = link_order->u.indirect.section; |
| 1212 | long reloc_size = bfd_get_reloc_upper_bound(input_bfd, |
| 1213 | input_section); |
| 1214 | arelent **reloc_vector = NULL; |
| 1215 | long reloc_count; |
| 1216 | |
| 1217 | if (reloc_size < 0) |
| 1218 | goto error_return; |
| 1219 | |
| 1220 | /* If producing relocateable output, don't bother to relax. */ |
| 1221 | if (relocateable) |
| 1222 | return bfd_generic_get_relocated_section_contents (in_abfd, link_info, |
| 1223 | link_order, |
| 1224 | data, relocateable, |
| 1225 | symbols); |
| 1226 | |
| 1227 | reloc_vector = (arelent **) bfd_malloc (reloc_size); |
| 1228 | if (reloc_vector == NULL && reloc_size != 0) |
| 1229 | goto error_return; |
| 1230 | |
| 1231 | input_section->reloc_done = 1; |
| 1232 | |
| 1233 | /* read in the section */ |
| 1234 | BFD_ASSERT (true == bfd_get_section_contents(input_bfd, |
| 1235 | input_section, |
| 1236 | data, |
| 1237 | 0, |
| 1238 | input_section->_raw_size)); |
| 1239 | |
| 1240 | reloc_count = bfd_canonicalize_reloc (input_bfd, |
| 1241 | input_section, |
| 1242 | reloc_vector, |
| 1243 | symbols); |
| 1244 | if (reloc_count < 0) |
| 1245 | goto error_return; |
| 1246 | if (reloc_count > 0) |
| 1247 | { |
| 1248 | arelent **parent = reloc_vector; |
| 1249 | arelent *reloc ; |
| 1250 | |
| 1251 | unsigned int dst_address = 0; |
| 1252 | unsigned int src_address = 0; |
| 1253 | unsigned int run; |
| 1254 | unsigned int idx; |
| 1255 | |
| 1256 | /* Find how long a run we can do */ |
| 1257 | while (dst_address < link_order->size) |
| 1258 | { |
| 1259 | reloc = *parent; |
| 1260 | if (reloc) |
| 1261 | { |
| 1262 | /* Note that the relaxing didn't tie up the addresses in the |
| 1263 | relocation, so we use the original address to work out the |
| 1264 | run of non-relocated data */ |
| 1265 | BFD_ASSERT (reloc->address >= src_address); |
| 1266 | run = reloc->address - src_address; |
| 1267 | parent++; |
| 1268 | } |
| 1269 | else |
| 1270 | { |
| 1271 | run = link_order->size - dst_address; |
| 1272 | } |
| 1273 | /* Copy the bytes */ |
| 1274 | for (idx = 0; idx < run; idx++) |
| 1275 | { |
| 1276 | data[dst_address++] = data[src_address++]; |
| 1277 | } |
| 1278 | |
| 1279 | /* Now do the relocation */ |
| 1280 | |
| 1281 | if (reloc) |
| 1282 | { |
| 1283 | switch (reloc->howto->type) |
| 1284 | { |
| 1285 | case ABS32CODE: |
| 1286 | calljx_callback (in_abfd, link_info, reloc, |
| 1287 | src_address + data, dst_address + data, |
| 1288 | input_section); |
| 1289 | src_address+=4; |
| 1290 | dst_address+=4; |
| 1291 | break; |
| 1292 | case ABS32: |
| 1293 | bfd_put_32(in_abfd, |
| 1294 | (bfd_get_32 (in_abfd, data+src_address) |
| 1295 | + get_value (reloc, link_info, input_section)), |
| 1296 | data+dst_address); |
| 1297 | src_address+=4; |
| 1298 | dst_address+=4; |
| 1299 | break; |
| 1300 | case CALLJ: |
| 1301 | callj_callback (in_abfd, link_info, reloc, data, src_address, |
| 1302 | dst_address, input_section, false); |
| 1303 | src_address+=4; |
| 1304 | dst_address+=4; |
| 1305 | break; |
| 1306 | case ALIGNDONE: |
| 1307 | BFD_ASSERT (reloc->addend >= src_address); |
| 1308 | BFD_ASSERT (reloc->addend <= input_section->_raw_size); |
| 1309 | src_address = reloc->addend; |
| 1310 | dst_address = ((dst_address + reloc->howto->size) |
| 1311 | & ~reloc->howto->size); |
| 1312 | break; |
| 1313 | case ABS32CODE_SHRUNK: |
| 1314 | /* This used to be a callx, but we've found out that a |
| 1315 | callj will reach, so do the right thing. */ |
| 1316 | callj_callback (in_abfd, link_info, reloc, data, |
| 1317 | src_address + 4, dst_address, input_section, |
| 1318 | true); |
| 1319 | dst_address+=4; |
| 1320 | src_address+=8; |
| 1321 | break; |
| 1322 | case PCREL24: |
| 1323 | { |
| 1324 | long int word = bfd_get_32(in_abfd, data+src_address); |
| 1325 | bfd_vma value; |
| 1326 | |
| 1327 | value = get_value (reloc, link_info, input_section); |
| 1328 | word = ((word & ~BAL_MASK) |
| 1329 | | (((word & BAL_MASK) |
| 1330 | + value |
| 1331 | - output_addr (input_section) |
| 1332 | + reloc->addend) |
| 1333 | & BAL_MASK)); |
| 1334 | |
| 1335 | bfd_put_32(in_abfd,word, data+dst_address); |
| 1336 | dst_address+=4; |
| 1337 | src_address+=4; |
| 1338 | |
| 1339 | } |
| 1340 | break; |
| 1341 | |
| 1342 | case PCREL13: |
| 1343 | { |
| 1344 | long int word = bfd_get_32(in_abfd, data+src_address); |
| 1345 | bfd_vma value; |
| 1346 | |
| 1347 | value = get_value (reloc, link_info, input_section); |
| 1348 | word = ((word & ~PCREL13_MASK) |
| 1349 | | (((word & PCREL13_MASK) |
| 1350 | + value |
| 1351 | + reloc->addend |
| 1352 | - output_addr (input_section)) |
| 1353 | & PCREL13_MASK)); |
| 1354 | |
| 1355 | bfd_put_32(in_abfd,word, data+dst_address); |
| 1356 | dst_address+=4; |
| 1357 | src_address+=4; |
| 1358 | |
| 1359 | } |
| 1360 | break; |
| 1361 | |
| 1362 | default: |
| 1363 | abort(); |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | if (reloc_vector != NULL) |
| 1369 | free (reloc_vector); |
| 1370 | return data; |
| 1371 | error_return: |
| 1372 | if (reloc_vector != NULL) |
| 1373 | free (reloc_vector); |
| 1374 | return NULL; |
| 1375 | } |
| 1376 | /***********************************************************************/ |
| 1377 | |
| 1378 | /* Build the transfer vectors for Big and Little-Endian B.OUT files. */ |
| 1379 | |
| 1380 | #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol |
| 1381 | #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info |
| 1382 | |
| 1383 | #define b_out_bfd_link_hash_table_create _bfd_generic_link_hash_table_create |
| 1384 | #define b_out_bfd_link_add_symbols _bfd_generic_link_add_symbols |
| 1385 | #define b_out_bfd_final_link _bfd_generic_final_link |
| 1386 | #define b_out_bfd_link_split_section _bfd_generic_link_split_section |
| 1387 | |
| 1388 | #define aout_32_get_section_contents_in_window \ |
| 1389 | _bfd_generic_get_section_contents_in_window |
| 1390 | |
| 1391 | const bfd_target b_out_vec_big_host = |
| 1392 | { |
| 1393 | "b.out.big", /* name */ |
| 1394 | bfd_target_aout_flavour, |
| 1395 | BFD_ENDIAN_LITTLE, /* data byte order is little */ |
| 1396 | BFD_ENDIAN_BIG, /* hdr byte order is big */ |
| 1397 | (HAS_RELOC | EXEC_P | /* object flags */ |
| 1398 | HAS_LINENO | HAS_DEBUG | |
| 1399 | HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ), |
| 1400 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
| 1401 | '_', /* symbol leading char */ |
| 1402 | ' ', /* ar_pad_char */ |
| 1403 | 16, /* ar_max_namelen */ |
| 1404 | |
| 1405 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
| 1406 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
| 1407 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ |
| 1408 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
| 1409 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, |
| 1410 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ |
| 1411 | {_bfd_dummy_target, b_out_object_p, /* bfd_check_format */ |
| 1412 | bfd_generic_archive_p, _bfd_dummy_target}, |
| 1413 | {bfd_false, b_out_mkobject, /* bfd_set_format */ |
| 1414 | _bfd_generic_mkarchive, bfd_false}, |
| 1415 | {bfd_false, b_out_write_object_contents, /* bfd_write_contents */ |
| 1416 | _bfd_write_archive_contents, bfd_false}, |
| 1417 | |
| 1418 | BFD_JUMP_TABLE_GENERIC (aout_32), |
| 1419 | BFD_JUMP_TABLE_COPY (_bfd_generic), |
| 1420 | BFD_JUMP_TABLE_CORE (_bfd_nocore), |
| 1421 | BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd), |
| 1422 | BFD_JUMP_TABLE_SYMBOLS (aout_32), |
| 1423 | BFD_JUMP_TABLE_RELOCS (b_out), |
| 1424 | BFD_JUMP_TABLE_WRITE (b_out), |
| 1425 | BFD_JUMP_TABLE_LINK (b_out), |
| 1426 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
| 1427 | |
| 1428 | (PTR) 0, |
| 1429 | }; |
| 1430 | |
| 1431 | |
| 1432 | const bfd_target b_out_vec_little_host = |
| 1433 | { |
| 1434 | "b.out.little", /* name */ |
| 1435 | bfd_target_aout_flavour, |
| 1436 | BFD_ENDIAN_LITTLE, /* data byte order is little */ |
| 1437 | BFD_ENDIAN_LITTLE, /* header byte order is little */ |
| 1438 | (HAS_RELOC | EXEC_P | /* object flags */ |
| 1439 | HAS_LINENO | HAS_DEBUG | |
| 1440 | HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ), |
| 1441 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
| 1442 | '_', /* symbol leading char */ |
| 1443 | ' ', /* ar_pad_char */ |
| 1444 | 16, /* ar_max_namelen */ |
| 1445 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
| 1446 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
| 1447 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ |
| 1448 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
| 1449 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
| 1450 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ |
| 1451 | |
| 1452 | {_bfd_dummy_target, b_out_object_p, /* bfd_check_format */ |
| 1453 | bfd_generic_archive_p, _bfd_dummy_target}, |
| 1454 | {bfd_false, b_out_mkobject, /* bfd_set_format */ |
| 1455 | _bfd_generic_mkarchive, bfd_false}, |
| 1456 | {bfd_false, b_out_write_object_contents, /* bfd_write_contents */ |
| 1457 | _bfd_write_archive_contents, bfd_false}, |
| 1458 | |
| 1459 | BFD_JUMP_TABLE_GENERIC (aout_32), |
| 1460 | BFD_JUMP_TABLE_COPY (_bfd_generic), |
| 1461 | BFD_JUMP_TABLE_CORE (_bfd_nocore), |
| 1462 | BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd), |
| 1463 | BFD_JUMP_TABLE_SYMBOLS (aout_32), |
| 1464 | BFD_JUMP_TABLE_RELOCS (b_out), |
| 1465 | BFD_JUMP_TABLE_WRITE (b_out), |
| 1466 | BFD_JUMP_TABLE_LINK (b_out), |
| 1467 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
| 1468 | |
| 1469 | (PTR) 0 |
| 1470 | }; |