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