| 1 | /* write.c - emit .o file |
| 2 | |
| 3 | Copyright (C) 1986, 1987, 1990, 1991, 1992 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GAS, the GNU Assembler. |
| 6 | |
| 7 | GAS 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, or (at your option) |
| 10 | any later version. |
| 11 | |
| 12 | GAS 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 GAS; see the file COPYING. If not, write to |
| 19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 20 | |
| 21 | /* This thing should be set up to do byteordering correctly. But... */ |
| 22 | |
| 23 | #include "as.h" |
| 24 | #include "subsegs.h" |
| 25 | #include "obstack.h" |
| 26 | #include "output-file.h" |
| 27 | |
| 28 | /* The NOP_OPCODE is for the alignment fill value. |
| 29 | * fill it a nop instruction so that the disassembler does not choke |
| 30 | * on it |
| 31 | */ |
| 32 | #ifndef NOP_OPCODE |
| 33 | #define NOP_OPCODE 0x00 |
| 34 | #endif |
| 35 | |
| 36 | #ifndef MANY_SEGMENTS |
| 37 | struct frag *text_frag_root; |
| 38 | struct frag *data_frag_root; |
| 39 | struct frag *bss_frag_root; |
| 40 | |
| 41 | struct frag *text_last_frag; /* Last frag in segment. */ |
| 42 | struct frag *data_last_frag; /* Last frag in segment. */ |
| 43 | static struct frag *bss_last_frag; /* Last frag in segment. */ |
| 44 | #endif |
| 45 | |
| 46 | static object_headers headers; |
| 47 | |
| 48 | long string_byte_count; |
| 49 | |
| 50 | static char *the_object_file; |
| 51 | |
| 52 | char *next_object_file_charP; /* Tracks object file bytes. */ |
| 53 | |
| 54 | int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE; |
| 55 | |
| 56 | static int is_dnrange PARAMS ((struct frag * f1, struct frag * f2)); |
| 57 | static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type)); |
| 58 | static relax_addressT relax_align PARAMS ((relax_addressT addr, long align)); |
| 59 | void relax_segment PARAMS ((struct frag * seg_frag_root, segT seg_type)); |
| 60 | |
| 61 | /* |
| 62 | * fix_new() |
| 63 | * |
| 64 | * Create a fixS in obstack 'notes'. |
| 65 | */ |
| 66 | fixS * |
| 67 | fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type) |
| 68 | fragS *frag; /* Which frag? */ |
| 69 | int where; /* Where in that frag? */ |
| 70 | short int size; /* 1, 2, or 4 usually. */ |
| 71 | symbolS *add_symbol; /* X_add_symbol. */ |
| 72 | symbolS *sub_symbol; /* X_subtract_symbol. */ |
| 73 | long offset; /* X_add_number. */ |
| 74 | int pcrel; /* TRUE if PC-relative relocation. */ |
| 75 | int r_type; /* Relocation type */ |
| 76 | { |
| 77 | fixS *fixP; |
| 78 | |
| 79 | fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS)); |
| 80 | |
| 81 | fixP->fx_frag = frag; |
| 82 | fixP->fx_where = where; |
| 83 | fixP->fx_size = size; |
| 84 | fixP->fx_addsy = add_symbol; |
| 85 | fixP->fx_subsy = sub_symbol; |
| 86 | fixP->fx_offset = offset; |
| 87 | fixP->fx_pcrel = pcrel; |
| 88 | #if defined(TC_SPARC) || defined(TC_A29K) || defined( NEED_FX_R_TYPE) |
| 89 | fixP->fx_r_type = r_type; |
| 90 | #endif |
| 91 | /* JF these 'cuz of the NS32K stuff */ |
| 92 | fixP->fx_im_disp = 0; |
| 93 | fixP->fx_pcrel_adjust = 0; |
| 94 | fixP->fx_bsr = 0; |
| 95 | fixP->fx_bit_fixP = 0; |
| 96 | |
| 97 | /* usually, we want relocs sorted numerically, but while |
| 98 | comparing to older versions of gas that have relocs |
| 99 | reverse sorted, it is convenient to have this compile |
| 100 | time option. xoxorich. */ |
| 101 | |
| 102 | #ifdef REVERSE_SORT_RELOCS |
| 103 | |
| 104 | fixP->fx_next = *seg_fix_rootP; |
| 105 | *seg_fix_rootP = fixP; |
| 106 | |
| 107 | #else /* REVERSE_SORT_RELOCS */ |
| 108 | |
| 109 | fixP->fx_next = NULL; |
| 110 | |
| 111 | if (*seg_fix_tailP) |
| 112 | (*seg_fix_tailP)->fx_next = fixP; |
| 113 | else |
| 114 | *seg_fix_rootP = fixP; |
| 115 | *seg_fix_tailP = fixP; |
| 116 | |
| 117 | #endif /* REVERSE_SORT_RELOCS */ |
| 118 | |
| 119 | fixP->fx_callj = 0; |
| 120 | return (fixP); |
| 121 | } /* fix_new() */ |
| 122 | |
| 123 | #ifndef BFD |
| 124 | |
| 125 | void |
| 126 | remove_subsegs (head, seg, root, last) |
| 127 | frchainS *head; |
| 128 | int seg; |
| 129 | fragS **root; |
| 130 | fragS **last; |
| 131 | { |
| 132 | fragS dummy; |
| 133 | fragS *prev_frag = &dummy; |
| 134 | *root = head->frch_root; |
| 135 | while (head && head->frch_seg == seg) |
| 136 | { |
| 137 | prev_frag->fr_next = head->frch_root; |
| 138 | |
| 139 | prev_frag = head->frch_last; |
| 140 | head = head->frch_next; |
| 141 | } |
| 142 | *last = prev_frag; |
| 143 | prev_frag->fr_next = 0; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | write_object_file () |
| 148 | { |
| 149 | register struct frchain *frchainP; /* Track along all frchains. */ |
| 150 | register fragS *fragP; /* Track along all frags. */ |
| 151 | register struct frchain *next_frchainP; |
| 152 | register fragS **prev_fragPP; |
| 153 | unsigned int data_siz; |
| 154 | |
| 155 | long object_file_size; |
| 156 | |
| 157 | #ifdef VMS |
| 158 | /* |
| 159 | * Under VMS we try to be compatible with VAX-11 "C". Thus, we |
| 160 | * call a routine to check for the definition of the procedure |
| 161 | * "_main", and if so -- fix it up so that it can be program |
| 162 | * entry point. |
| 163 | */ |
| 164 | VMS_Check_For_Main (); |
| 165 | #endif /* VMS */ |
| 166 | /* |
| 167 | * After every sub-segment, we fake an ".align ...". This conforms to |
| 168 | * BSD4.2 brane-damage. We then fake ".fill 0" because that is the |
| 169 | * kind of frag that requires least thought. ".align" frags like to |
| 170 | * have a following frag since that makes calculating their intended |
| 171 | * length trivial. |
| 172 | */ |
| 173 | #define SUB_SEGMENT_ALIGN (2) |
| 174 | for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next) |
| 175 | { |
| 176 | #ifdef VMS |
| 177 | /* |
| 178 | * Under VAX/VMS, the linker (and PSECT specifications) |
| 179 | * take care of correctly aligning the segments. |
| 180 | * Doing the alignment here (on initialized data) can |
| 181 | * mess up the calculation of global data PSECT sizes. |
| 182 | */ |
| 183 | #undef SUB_SEGMENT_ALIGN |
| 184 | #define SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0) |
| 185 | #endif /* VMS */ |
| 186 | subseg_new (frchainP->frch_seg, frchainP->frch_subseg); |
| 187 | frag_align (SUB_SEGMENT_ALIGN, NOP_OPCODE); |
| 188 | /* frag_align will have left a new frag. */ |
| 189 | /* Use this last frag for an empty ".fill". */ |
| 190 | /* |
| 191 | * For this segment ... |
| 192 | * Create a last frag. Do not leave a "being filled in frag". |
| 193 | */ |
| 194 | frag_wane (frag_now); |
| 195 | frag_now->fr_fix = 0; |
| 196 | know (frag_now->fr_next == NULL); |
| 197 | /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */ |
| 198 | /* Above shows we haven't left a half-completed object on obstack. */ |
| 199 | } /* walk the frag chain */ |
| 200 | |
| 201 | /* |
| 202 | * From now on, we don't care about sub-segments. |
| 203 | * Build one frag chain for each segment. Linked thru fr_next. |
| 204 | * We know that there is at least 1 text frchain & at least 1 data |
| 205 | * frchain. |
| 206 | */ |
| 207 | |
| 208 | remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag); |
| 209 | remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag); |
| 210 | remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag); |
| 211 | |
| 212 | /* |
| 213 | * We have two segments. If user gave -R flag, then we must put the |
| 214 | * data frags into the text segment. Do this before relaxing so |
| 215 | * we know to take advantage of -R and make shorter addresses. |
| 216 | */ |
| 217 | #ifndef OBJ_AOUT |
| 218 | if (flagseen['R']) |
| 219 | { |
| 220 | fixS *tmp; |
| 221 | |
| 222 | text_last_frag->fr_next = data_frag_root; |
| 223 | text_last_frag = data_last_frag; |
| 224 | data_last_frag = NULL; |
| 225 | data_frag_root = NULL; |
| 226 | if (text_fix_root) |
| 227 | { |
| 228 | for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);; |
| 229 | tmp->fx_next = data_fix_root; |
| 230 | text_fix_tail = data_fix_tail; |
| 231 | } |
| 232 | else |
| 233 | text_fix_root = data_fix_root; |
| 234 | data_fix_root = NULL; |
| 235 | } |
| 236 | #endif |
| 237 | relax_segment (text_frag_root, SEG_TEXT); |
| 238 | relax_segment (data_frag_root, SEG_DATA); |
| 239 | relax_segment (bss_frag_root, SEG_BSS); |
| 240 | /* |
| 241 | * Now the addresses of frags are correct within the segment. |
| 242 | */ |
| 243 | |
| 244 | know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0); |
| 245 | H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address); |
| 246 | text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers); |
| 247 | |
| 248 | /* |
| 249 | * Join the 2 segments into 1 huge segment. |
| 250 | * To do this, re-compute every rn_address in the SEG_DATA frags. |
| 251 | * Then join the data frags after the text frags. |
| 252 | * |
| 253 | * Determine a_data [length of data segment]. |
| 254 | */ |
| 255 | if (data_frag_root) |
| 256 | { |
| 257 | register relax_addressT slide; |
| 258 | |
| 259 | know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0)); |
| 260 | |
| 261 | H_SET_DATA_SIZE (&headers, data_last_frag->fr_address); |
| 262 | data_last_frag->fr_address = H_GET_DATA_SIZE (&headers); |
| 263 | slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */ |
| 264 | #ifdef OBJ_BOUT |
| 265 | #define RoundUp(N,S) (((N)+(S)-1)&-(S)) |
| 266 | /* For b.out: If the data section has a strict alignment |
| 267 | requirement, its load address in the .o file will be |
| 268 | rounded up from the size of the text section. These |
| 269 | two values are *not* the same! Similarly for the bss |
| 270 | section.... */ |
| 271 | slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]); |
| 272 | #endif |
| 273 | |
| 274 | for (fragP = data_frag_root; fragP; fragP = fragP->fr_next) |
| 275 | { |
| 276 | fragP->fr_address += slide; |
| 277 | } /* for each data frag */ |
| 278 | |
| 279 | know (text_last_frag != 0); |
| 280 | text_last_frag->fr_next = data_frag_root; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | H_SET_DATA_SIZE (&headers, 0); |
| 285 | data_siz = 0; |
| 286 | } |
| 287 | |
| 288 | #ifdef OBJ_BOUT |
| 289 | /* See above comments on b.out data section address. */ |
| 290 | { |
| 291 | long bss_vma; |
| 292 | if (data_last_frag == 0) |
| 293 | bss_vma = H_GET_TEXT_SIZE (&headers); |
| 294 | else |
| 295 | bss_vma = data_last_frag->fr_address; |
| 296 | bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]); |
| 297 | bss_address_frag.fr_address = bss_vma; |
| 298 | } |
| 299 | #else |
| 300 | bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) + |
| 301 | H_GET_DATA_SIZE (&headers)); |
| 302 | |
| 303 | |
| 304 | /* Slide all the frags */ |
| 305 | if (bss_frag_root) |
| 306 | { |
| 307 | relax_addressT slide = bss_address_frag.fr_address + local_bss_counter; |
| 308 | |
| 309 | for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next) |
| 310 | { |
| 311 | fragP->fr_address += slide; |
| 312 | } /* for each bss frag */ |
| 313 | } |
| 314 | |
| 315 | #endif |
| 316 | if (bss_last_frag) |
| 317 | { |
| 318 | local_bss_counter += bss_last_frag->fr_address - bss_frag_root->fr_address; |
| 319 | } |
| 320 | H_SET_BSS_SIZE (&headers, local_bss_counter); |
| 321 | |
| 322 | /* |
| 323 | * |
| 324 | * Crawl the symbol chain. |
| 325 | * |
| 326 | * For each symbol whose value depends on a frag, take the address of |
| 327 | * that frag and subsume it into the value of the symbol. |
| 328 | * After this, there is just one way to lookup a symbol value. |
| 329 | * Values are left in their final state for object file emission. |
| 330 | * We adjust the values of 'L' local symbols, even if we do |
| 331 | * not intend to emit them to the object file, because their values |
| 332 | * are needed for fix-ups. |
| 333 | * |
| 334 | * Unless we saw a -L flag, remove all symbols that begin with 'L' |
| 335 | * from the symbol chain. (They are still pointed to by the fixes.) |
| 336 | * |
| 337 | * Count the remaining symbols. |
| 338 | * Assign a symbol number to each symbol. |
| 339 | * Count the number of string-table chars we will emit. |
| 340 | * Put this info into the headers as appropriate. |
| 341 | * |
| 342 | */ |
| 343 | know (zero_address_frag.fr_address == 0); |
| 344 | string_byte_count = sizeof (string_byte_count); |
| 345 | |
| 346 | obj_crawl_symbol_chain (&headers); |
| 347 | |
| 348 | if (string_byte_count == sizeof (string_byte_count)) |
| 349 | { |
| 350 | string_byte_count = 0; |
| 351 | } /* if no strings, then no count. */ |
| 352 | |
| 353 | H_SET_STRING_SIZE (&headers, string_byte_count); |
| 354 | |
| 355 | /* |
| 356 | * Addresses of frags now reflect addresses we use in the object file. |
| 357 | * Symbol values are correct. |
| 358 | * Scan the frags, converting any ".org"s and ".align"s to ".fill"s. |
| 359 | * Also converting any machine-dependent frags using md_convert_frag(); |
| 360 | */ |
| 361 | subseg_change (SEG_TEXT, 0); |
| 362 | |
| 363 | for (fragP = text_frag_root; fragP; fragP = fragP->fr_next) |
| 364 | { |
| 365 | switch (fragP->fr_type) |
| 366 | { |
| 367 | case rs_align: |
| 368 | case rs_org: |
| 369 | #ifdef HANDLE_ALIGN |
| 370 | HANDLE_ALIGN (fragP); |
| 371 | #endif |
| 372 | fragP->fr_type = rs_fill; |
| 373 | know (fragP->fr_var == 1); |
| 374 | know (fragP->fr_next != NULL); |
| 375 | |
| 376 | fragP->fr_offset = (fragP->fr_next->fr_address |
| 377 | - fragP->fr_address |
| 378 | - fragP->fr_fix); |
| 379 | break; |
| 380 | |
| 381 | case rs_fill: |
| 382 | break; |
| 383 | |
| 384 | case rs_machine_dependent: |
| 385 | md_convert_frag (&headers, fragP); |
| 386 | |
| 387 | know ((fragP->fr_next == NULL) || ((fragP->fr_next->fr_address - fragP->fr_address) == fragP->fr_fix)); |
| 388 | |
| 389 | /* |
| 390 | * After md_convert_frag, we make the frag into a ".space 0". |
| 391 | * Md_convert_frag() should set up any fixSs and constants |
| 392 | * required. |
| 393 | */ |
| 394 | frag_wane (fragP); |
| 395 | break; |
| 396 | |
| 397 | #ifndef WORKING_DOT_WORD |
| 398 | case rs_broken_word: |
| 399 | { |
| 400 | struct broken_word *lie; |
| 401 | extern md_short_jump_size; |
| 402 | extern md_long_jump_size; |
| 403 | |
| 404 | if (fragP->fr_subtype) |
| 405 | { |
| 406 | fragP->fr_fix += md_short_jump_size; |
| 407 | for (lie = (struct broken_word *) (fragP->fr_symbol); lie && lie->dispfrag == fragP; lie = lie->next_broken_word) |
| 408 | if (lie->added == 1) |
| 409 | fragP->fr_fix += md_long_jump_size; |
| 410 | } |
| 411 | frag_wane (fragP); |
| 412 | } |
| 413 | break; |
| 414 | #endif |
| 415 | |
| 416 | default: |
| 417 | BAD_CASE (fragP->fr_type); |
| 418 | break; |
| 419 | } /* switch (fr_type) */ |
| 420 | |
| 421 | if (!((fragP->fr_next == NULL) |
| 422 | #ifdef OBJ_BOUT |
| 423 | || (fragP->fr_next == data_frag_root) |
| 424 | #endif |
| 425 | || ((fragP->fr_next->fr_address - fragP->fr_address) |
| 426 | == (fragP->fr_fix + (fragP->fr_offset * fragP->fr_var))))) |
| 427 | { |
| 428 | fprintf (stderr, "assertion failed: file `%s', line %d\n", |
| 429 | __FILE__, __LINE__ - 4); |
| 430 | exit (1); |
| 431 | } |
| 432 | } /* for each frag. */ |
| 433 | |
| 434 | #ifndef WORKING_DOT_WORD |
| 435 | { |
| 436 | struct broken_word *lie; |
| 437 | struct broken_word **prevP; |
| 438 | |
| 439 | prevP = &broken_words; |
| 440 | for (lie = broken_words; lie; lie = lie->next_broken_word) |
| 441 | if (!lie->added) |
| 442 | { |
| 443 | #ifdef TC_NS32K |
| 444 | fix_new_ns32k (lie->frag, |
| 445 | lie->word_goes_here - lie->frag->fr_literal, |
| 446 | 2, |
| 447 | lie->add, |
| 448 | lie->sub, |
| 449 | lie->addnum, |
| 450 | 0, 0, 2, 0, 0); |
| 451 | #else |
| 452 | # if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE) |
| 453 | fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal, |
| 454 | 2, lie->add, |
| 455 | lie->sub, lie->addnum, |
| 456 | 0, NO_RELOC); |
| 457 | # else |
| 458 | fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal, |
| 459 | 2, lie->add, |
| 460 | lie->sub, lie->addnum, |
| 461 | 0, 0); |
| 462 | |
| 463 | # endif /* tc_sparc|tc_a29k|need_fx_r_type */ |
| 464 | #endif /* TC_NS32K */ |
| 465 | /* md_number_to_chars(lie->word_goes_here, |
| 466 | S_GET_VALUE(lie->add) |
| 467 | + lie->addnum |
| 468 | - S_GET_VALUE(lie->sub), |
| 469 | 2); */ |
| 470 | *prevP = lie->next_broken_word; |
| 471 | } |
| 472 | else |
| 473 | prevP = &(lie->next_broken_word); |
| 474 | |
| 475 | for (lie = broken_words; lie;) |
| 476 | { |
| 477 | struct broken_word *untruth; |
| 478 | char *table_ptr; |
| 479 | long table_addr; |
| 480 | long from_addr, to_addr; |
| 481 | int n, m; |
| 482 | |
| 483 | extern md_short_jump_size; |
| 484 | extern md_long_jump_size; |
| 485 | |
| 486 | fragP = lie->dispfrag; |
| 487 | |
| 488 | /* Find out how many broken_words go here */ |
| 489 | n = 0; |
| 490 | for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word) |
| 491 | if (untruth->added == 1) |
| 492 | n++; |
| 493 | |
| 494 | table_ptr = lie->dispfrag->fr_opcode; |
| 495 | table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal); |
| 496 | /* Create the jump around the long jumps */ |
| 497 | /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */ |
| 498 | from_addr = table_addr; |
| 499 | to_addr = table_addr + md_short_jump_size + n * md_long_jump_size; |
| 500 | md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add); |
| 501 | table_ptr += md_short_jump_size; |
| 502 | table_addr += md_short_jump_size; |
| 503 | |
| 504 | for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word) |
| 505 | { |
| 506 | if (lie->added == 2) |
| 507 | continue; |
| 508 | /* Patch the jump table */ |
| 509 | /* This is the offset from ??? to table_ptr+0 */ |
| 510 | to_addr = table_addr |
| 511 | - S_GET_VALUE (lie->sub); |
| 512 | md_number_to_chars (lie->word_goes_here, to_addr, 2); |
| 513 | for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word) |
| 514 | { |
| 515 | if (untruth->use_jump == lie) |
| 516 | md_number_to_chars (untruth->word_goes_here, to_addr, 2); |
| 517 | } |
| 518 | |
| 519 | /* Install the long jump */ |
| 520 | /* this is a long jump from table_ptr+0 to the final target */ |
| 521 | from_addr = table_addr; |
| 522 | to_addr = S_GET_VALUE (lie->add) + lie->addnum; |
| 523 | md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add); |
| 524 | table_ptr += md_long_jump_size; |
| 525 | table_addr += md_long_jump_size; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | #endif /* not WORKING_DOT_WORD */ |
| 530 | |
| 531 | #ifndef VMS |
| 532 | { /* not vms */ |
| 533 | /* |
| 534 | * Scan every FixS performing fixups. We had to wait until now to do |
| 535 | * this because md_convert_frag() may have made some fixSs. |
| 536 | */ |
| 537 | int trsize, drsize; |
| 538 | |
| 539 | subseg_change (SEG_TEXT, 0); |
| 540 | trsize = md_reloc_size * fixup_segment (text_fix_root, |
| 541 | SEG_TEXT); |
| 542 | subseg_change (SEG_DATA, 0); |
| 543 | drsize = md_reloc_size * fixup_segment (data_fix_root, |
| 544 | SEG_DATA); |
| 545 | H_SET_RELOCATION_SIZE (&headers, trsize, drsize); |
| 546 | |
| 547 | /* FIXME move this stuff into the pre-write-hook */ |
| 548 | H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file); |
| 549 | H_SET_ENTRY_POINT (&headers, 0); |
| 550 | |
| 551 | obj_pre_write_hook (&headers); /* extra coff stuff */ |
| 552 | if ((had_warnings () && flagseen['Z']) |
| 553 | || had_errors () > 0) |
| 554 | { |
| 555 | if (flagseen['Z']) |
| 556 | { |
| 557 | as_warn ("%d error%s, %d warning%s, generating bad object file.\n", |
| 558 | had_errors (), had_errors () == 1 ? "" : "s", |
| 559 | had_warnings (), had_warnings () == 1 ? "" : "s"); |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | as_fatal ("%d error%s, %d warning%s, no object file generated.\n", |
| 564 | had_errors (), had_errors () == 1 ? "" : "s", |
| 565 | had_warnings (), had_warnings () == 1 ? "" : "s"); |
| 566 | } /* on want output */ |
| 567 | } /* on error condition */ |
| 568 | |
| 569 | object_file_size = H_GET_FILE_SIZE (&headers); |
| 570 | next_object_file_charP = the_object_file = xmalloc (object_file_size); |
| 571 | |
| 572 | output_file_create (out_file_name); |
| 573 | |
| 574 | obj_header_append (&next_object_file_charP, &headers); |
| 575 | |
| 576 | know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers)); |
| 577 | |
| 578 | /* |
| 579 | * Emit code. |
| 580 | */ |
| 581 | for (fragP = text_frag_root; fragP; fragP = fragP->fr_next) |
| 582 | { |
| 583 | register long count; |
| 584 | register char *fill_literal; |
| 585 | register long fill_size; |
| 586 | |
| 587 | know (fragP->fr_type == rs_fill); |
| 588 | append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix); |
| 589 | fill_literal = fragP->fr_literal + fragP->fr_fix; |
| 590 | fill_size = fragP->fr_var; |
| 591 | know (fragP->fr_offset >= 0); |
| 592 | |
| 593 | for (count = fragP->fr_offset; count; count--) |
| 594 | { |
| 595 | append (&next_object_file_charP, fill_literal, (unsigned long) fill_size); |
| 596 | } /* for each */ |
| 597 | |
| 598 | } /* for each code frag. */ |
| 599 | |
| 600 | know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers))); |
| 601 | |
| 602 | /* |
| 603 | * Emit relocations. |
| 604 | */ |
| 605 | obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0); |
| 606 | know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers))); |
| 607 | #ifdef TC_I960 |
| 608 | /* Make addresses in data relocation directives relative to beginning of |
| 609 | * first data fragment, not end of last text fragment: alignment of the |
| 610 | * start of the data segment may place a gap between the segments. |
| 611 | */ |
| 612 | obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address); |
| 613 | #else /* TC_I960 */ |
| 614 | obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address); |
| 615 | #endif /* TC_I960 */ |
| 616 | |
| 617 | know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers))); |
| 618 | |
| 619 | /* |
| 620 | * Emit line number entries. |
| 621 | */ |
| 622 | OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file); |
| 623 | know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers))); |
| 624 | |
| 625 | /* |
| 626 | * Emit symbols. |
| 627 | */ |
| 628 | obj_emit_symbols (&next_object_file_charP, symbol_rootP); |
| 629 | know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers) + H_GET_SYMBOL_TABLE_SIZE (&headers))); |
| 630 | |
| 631 | /* |
| 632 | * Emit strings. |
| 633 | */ |
| 634 | |
| 635 | if (string_byte_count > 0) |
| 636 | { |
| 637 | obj_emit_strings (&next_object_file_charP); |
| 638 | } /* only if we have a string table */ |
| 639 | |
| 640 | /* know((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE(&headers) + H_GET_TEXT_SIZE(&headers) + H_GET_DATA_SIZE(&headers) + H_GET_TEXT_RELOCATION_SIZE(&headers) + H_GET_DATA_RELOCATION_SIZE(&headers) + H_GET_LINENO_SIZE(&headers) + H_GET_SYMBOL_TABLE_SIZE(&headers) + H_GET_STRING_SIZE(&headers))); |
| 641 | */ |
| 642 | /* know(next_object_file_charP == the_object_file + object_file_size);*/ |
| 643 | |
| 644 | #ifdef BFD_HEADERS |
| 645 | bfd_seek (stdoutput, 0, 0); |
| 646 | bfd_write (the_object_file, 1, object_file_size, stdoutput); |
| 647 | #else |
| 648 | |
| 649 | /* Write the data to the file */ |
| 650 | output_file_append (the_object_file, object_file_size, out_file_name); |
| 651 | #endif |
| 652 | |
| 653 | output_file_close (out_file_name); |
| 654 | } /* non vms output */ |
| 655 | #else /* VMS */ |
| 656 | /* |
| 657 | * Now do the VMS-dependent part of writing the object file |
| 658 | */ |
| 659 | VMS_write_object_file (text_siz, data_siz, text_frag_root, data_frag_root); |
| 660 | #endif /* VMS */ |
| 661 | } /* write_object_file() */ |
| 662 | |
| 663 | #else |
| 664 | #endif |
| 665 | |
| 666 | /* |
| 667 | * relax_segment() |
| 668 | * |
| 669 | * Now we have a segment, not a crowd of sub-segments, we can make fr_address |
| 670 | * values. |
| 671 | * |
| 672 | * Relax the frags. |
| 673 | * |
| 674 | * After this, all frags in this segment have addresses that are correct |
| 675 | * within the segment. Since segments live in different file addresses, |
| 676 | * these frag addresses may not be the same as final object-file addresses. |
| 677 | */ |
| 678 | |
| 679 | |
| 680 | |
| 681 | void |
| 682 | relax_segment (segment_frag_root, segment) |
| 683 | struct frag *segment_frag_root; |
| 684 | segT segment; /* SEG_DATA or SEG_TEXT */ |
| 685 | { |
| 686 | register struct frag *fragP; |
| 687 | register relax_addressT address; |
| 688 | #ifndef MANY_SEGMENTS |
| 689 | know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS); |
| 690 | #endif |
| 691 | /* In case md_estimate_size_before_relax() wants to make fixSs. */ |
| 692 | subseg_change (segment, 0); |
| 693 | |
| 694 | /* For each frag in segment: count and store (a 1st guess of) |
| 695 | fr_address. */ |
| 696 | address = 0; |
| 697 | for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) |
| 698 | { |
| 699 | fragP->fr_address = address; |
| 700 | address += fragP->fr_fix; |
| 701 | |
| 702 | switch (fragP->fr_type) |
| 703 | { |
| 704 | case rs_fill: |
| 705 | address += fragP->fr_offset * fragP->fr_var; |
| 706 | break; |
| 707 | |
| 708 | case rs_align: |
| 709 | address += relax_align (address, fragP->fr_offset); |
| 710 | break; |
| 711 | |
| 712 | case rs_org: |
| 713 | /* Assume .org is nugatory. It will grow with 1st relax. */ |
| 714 | break; |
| 715 | |
| 716 | case rs_machine_dependent: |
| 717 | address += md_estimate_size_before_relax (fragP, segment); |
| 718 | break; |
| 719 | |
| 720 | #ifndef WORKING_DOT_WORD |
| 721 | /* Broken words don't concern us yet */ |
| 722 | case rs_broken_word: |
| 723 | break; |
| 724 | #endif |
| 725 | |
| 726 | default: |
| 727 | BAD_CASE (fragP->fr_type); |
| 728 | break; |
| 729 | } /* switch(fr_type) */ |
| 730 | } /* for each frag in the segment */ |
| 731 | |
| 732 | /* Do relax(). */ |
| 733 | { |
| 734 | long stretch; /* May be any size, 0 or negative. */ |
| 735 | /* Cumulative number of addresses we have */ |
| 736 | /* relaxed this pass. */ |
| 737 | /* We may have relaxed more than one address. */ |
| 738 | long stretched; /* Have we stretched on this pass? */ |
| 739 | /* This is 'cuz stretch may be zero, when, in fact some piece of code |
| 740 | grew, and another shrank. If a branch instruction doesn't fit anymore, |
| 741 | we could be scrod. */ |
| 742 | |
| 743 | do |
| 744 | { |
| 745 | stretch = stretched = 0; |
| 746 | for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) |
| 747 | { |
| 748 | long growth = 0; |
| 749 | unsigned long was_address; |
| 750 | long offset; |
| 751 | symbolS *symbolP; |
| 752 | long target; |
| 753 | long after; |
| 754 | long aim; |
| 755 | |
| 756 | was_address = fragP->fr_address; |
| 757 | address = fragP->fr_address += stretch; |
| 758 | symbolP = fragP->fr_symbol; |
| 759 | offset = fragP->fr_offset; |
| 760 | |
| 761 | switch (fragP->fr_type) |
| 762 | { |
| 763 | case rs_fill: /* .fill never relaxes. */ |
| 764 | growth = 0; |
| 765 | break; |
| 766 | |
| 767 | #ifndef WORKING_DOT_WORD |
| 768 | /* JF: This is RMS's idea. I do *NOT* want to be blamed |
| 769 | for it I do not want to write it. I do not want to have |
| 770 | anything to do with it. This is not the proper way to |
| 771 | implement this misfeature. */ |
| 772 | case rs_broken_word: |
| 773 | { |
| 774 | struct broken_word *lie; |
| 775 | struct broken_word *untruth; |
| 776 | extern int md_short_jump_size; |
| 777 | extern int md_long_jump_size; |
| 778 | |
| 779 | /* Yes this is ugly (storing the broken_word pointer |
| 780 | in the symbol slot). Still, this whole chunk of |
| 781 | code is ugly, and I don't feel like doing anything |
| 782 | about it. Think of it as stubbornness in action. */ |
| 783 | growth = 0; |
| 784 | for (lie = (struct broken_word *) (fragP->fr_symbol); |
| 785 | lie && lie->dispfrag == fragP; |
| 786 | lie = lie->next_broken_word) |
| 787 | { |
| 788 | |
| 789 | if (lie->added) |
| 790 | continue; |
| 791 | |
| 792 | offset = (lie->add->sy_frag->fr_address |
| 793 | + S_GET_VALUE (lie->add) |
| 794 | + lie->addnum |
| 795 | - (lie->sub->sy_frag->fr_address |
| 796 | + S_GET_VALUE (lie->sub))); |
| 797 | if (offset <= -32768 || offset >= 32767) |
| 798 | { |
| 799 | if (flagseen['K']) |
| 800 | as_warn (".word %s-%s+%ld didn't fit", |
| 801 | S_GET_NAME (lie->add), |
| 802 | S_GET_NAME (lie->sub), |
| 803 | lie->addnum); |
| 804 | lie->added = 1; |
| 805 | if (fragP->fr_subtype == 0) |
| 806 | { |
| 807 | fragP->fr_subtype++; |
| 808 | growth += md_short_jump_size; |
| 809 | } |
| 810 | for (untruth = lie->next_broken_word; |
| 811 | untruth && untruth->dispfrag == lie->dispfrag; |
| 812 | untruth = untruth->next_broken_word) |
| 813 | if ((untruth->add->sy_frag == lie->add->sy_frag) |
| 814 | && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add)) |
| 815 | { |
| 816 | untruth->added = 2; |
| 817 | untruth->use_jump = lie; |
| 818 | } |
| 819 | growth += md_long_jump_size; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | break; |
| 824 | } /* case rs_broken_word */ |
| 825 | #endif |
| 826 | case rs_align: |
| 827 | growth = (relax_align ((relax_addressT) (address + fragP->fr_fix), offset) |
| 828 | - relax_align ((relax_addressT) (was_address + fragP->fr_fix), offset)); |
| 829 | break; |
| 830 | |
| 831 | case rs_org: |
| 832 | target = offset; |
| 833 | |
| 834 | if (symbolP) |
| 835 | { |
| 836 | #ifdef MANY_SEGMENTS |
| 837 | #else |
| 838 | know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) || (S_GET_SEGMENT (symbolP) == SEG_DATA) || (S_GET_SEGMENT (symbolP) == SEG_TEXT) || S_GET_SEGMENT (symbolP) == SEG_BSS); |
| 839 | know (symbolP->sy_frag); |
| 840 | know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) || (symbolP->sy_frag == &zero_address_frag)); |
| 841 | #endif |
| 842 | target += S_GET_VALUE (symbolP) |
| 843 | + symbolP->sy_frag->fr_address; |
| 844 | } /* if we have a symbol */ |
| 845 | |
| 846 | know (fragP->fr_next); |
| 847 | after = fragP->fr_next->fr_address; |
| 848 | growth = ((target - after) > 0) ? (target - after) : 0; |
| 849 | /* Growth may be -ve, but variable part of frag cannot have |
| 850 | fewer than 0 chars. That is, we can't .org backwards. */ |
| 851 | |
| 852 | growth -= stretch; /* This is an absolute growth factor */ |
| 853 | break; |
| 854 | |
| 855 | case rs_machine_dependent: |
| 856 | { |
| 857 | const relax_typeS *this_type; |
| 858 | const relax_typeS *start_type; |
| 859 | relax_substateT next_state; |
| 860 | relax_substateT this_state; |
| 861 | |
| 862 | this_state = fragP->fr_subtype; |
| 863 | start_type = this_type = md_relax_table + this_state; |
| 864 | target = offset; |
| 865 | |
| 866 | if (symbolP) |
| 867 | { |
| 868 | #ifndef MANY_SEGMENTS |
| 869 | know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) || (S_GET_SEGMENT (symbolP) == SEG_DATA) || (S_GET_SEGMENT (symbolP) == SEG_BSS) || (S_GET_SEGMENT (symbolP) == SEG_TEXT)); |
| 870 | #endif |
| 871 | know (symbolP->sy_frag); |
| 872 | know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) || symbolP->sy_frag == &zero_address_frag); |
| 873 | target += |
| 874 | S_GET_VALUE (symbolP) |
| 875 | + symbolP->sy_frag->fr_address; |
| 876 | |
| 877 | /* If frag has yet to be reached on this pass, |
| 878 | assume it will move by STRETCH just as we did. |
| 879 | If this is not so, it will be because some frag |
| 880 | between grows, and that will force another pass. */ |
| 881 | |
| 882 | /* JF was just address */ |
| 883 | /* JF also added is_dnrange hack */ |
| 884 | /* There's gotta be a better/faster/etc way |
| 885 | to do this. . . */ |
| 886 | /* gnu@cygnus.com: I changed this from > to >= |
| 887 | because I ran into a zero-length frag (fr_fix=0) |
| 888 | which was created when the obstack needed a new |
| 889 | chunk JUST AFTER the opcode of a branch. Since |
| 890 | fr_fix is zero, fr_address of this frag is the same |
| 891 | as fr_address of the next frag. This |
| 892 | zero-length frag was variable and jumped to .+2 |
| 893 | (in the next frag), but since the > comparison |
| 894 | below failed (the two were =, not >), "stretch" |
| 895 | was not added to the target. Stretch was 178, so |
| 896 | the offset appeared to be .-176 instead, which did |
| 897 | not fit into a byte branch, so the assembler |
| 898 | relaxed the branch to a word. This didn't compare |
| 899 | with what happened when the same source file was |
| 900 | assembled on other machines, which is how I found it. |
| 901 | You might want to think about what other places have |
| 902 | trouble with zero length frags... */ |
| 903 | |
| 904 | if (symbolP->sy_frag->fr_address >= was_address |
| 905 | && is_dnrange (fragP, symbolP->sy_frag)) |
| 906 | { |
| 907 | target += stretch; |
| 908 | } |
| 909 | } /* if there's a symbol attached */ |
| 910 | |
| 911 | aim = target - address - fragP->fr_fix; |
| 912 | /* The displacement is affected by the instruction size |
| 913 | for the 32k architecture. I think we ought to be able |
| 914 | to add fragP->fr_pcrel_adjust in all cases (it should be |
| 915 | zero if not used), but just in case it breaks something |
| 916 | else we'll put this inside #ifdef NS32K ... #endif */ |
| 917 | #ifdef TC_NS32K |
| 918 | aim += fragP->fr_pcrel_adjust; |
| 919 | #endif /* TC_NS32K */ |
| 920 | |
| 921 | if (aim < 0) |
| 922 | { |
| 923 | /* Look backwards. */ |
| 924 | for (next_state = this_type->rlx_more; next_state;) |
| 925 | if (aim >= this_type->rlx_backward) |
| 926 | next_state = 0; |
| 927 | else |
| 928 | { |
| 929 | /* Grow to next state. */ |
| 930 | this_state = next_state; |
| 931 | this_type = md_relax_table + this_state; |
| 932 | next_state = this_type->rlx_more; |
| 933 | } |
| 934 | } |
| 935 | else |
| 936 | { |
| 937 | #ifdef M68K_AIM_KLUDGE |
| 938 | M68K_AIM_KLUDGE (aim, this_state, this_type); |
| 939 | #endif |
| 940 | /* Look forwards. */ |
| 941 | for (next_state = this_type->rlx_more; next_state;) |
| 942 | if (aim <= this_type->rlx_forward) |
| 943 | next_state = 0; |
| 944 | else |
| 945 | { |
| 946 | /* Grow to next state. */ |
| 947 | this_state = next_state; |
| 948 | this_type = md_relax_table + this_state; |
| 949 | next_state = this_type->rlx_more; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | growth = this_type->rlx_length - start_type->rlx_length; |
| 954 | if (growth != 0) |
| 955 | fragP->fr_subtype = this_state; |
| 956 | } |
| 957 | break; |
| 958 | |
| 959 | default: |
| 960 | BAD_CASE (fragP->fr_type); |
| 961 | break; |
| 962 | } |
| 963 | if (growth) |
| 964 | { |
| 965 | stretch += growth; |
| 966 | stretched++; |
| 967 | } |
| 968 | } /* For each frag in the segment. */ |
| 969 | } |
| 970 | while (stretched); /* Until nothing further to relax. */ |
| 971 | } /* do_relax */ |
| 972 | |
| 973 | /* |
| 974 | * We now have valid fr_address'es for each frag. |
| 975 | */ |
| 976 | |
| 977 | /* |
| 978 | * All fr_address's are correct, relative to their own segment. |
| 979 | * We have made all the fixS we will ever make. |
| 980 | */ |
| 981 | } /* relax_segment() */ |
| 982 | |
| 983 | /* |
| 984 | * Relax_align. Advance location counter to next address that has 'alignment' |
| 985 | * lowest order bits all 0s. |
| 986 | */ |
| 987 | |
| 988 | /* How many addresses does the .align take? */ |
| 989 | static relax_addressT |
| 990 | relax_align (address, alignment) |
| 991 | register relax_addressT address; /* Address now. */ |
| 992 | register long alignment; /* Alignment (binary). */ |
| 993 | { |
| 994 | relax_addressT mask; |
| 995 | relax_addressT new_address; |
| 996 | |
| 997 | mask = ~((~0) << alignment); |
| 998 | new_address = (address + mask) & (~mask); |
| 999 | if (linkrelax) |
| 1000 | /* We must provide lots of padding, so the linker can discard it |
| 1001 | when needed. The linker will not add extra space, ever. */ |
| 1002 | new_address += (1 << alignment); |
| 1003 | return (new_address - address); |
| 1004 | } /* relax_align() */ |
| 1005 | |
| 1006 | /* fixup_segment() |
| 1007 | |
| 1008 | Go through all the fixS's in a segment and see which ones can be |
| 1009 | handled now. (These consist of fixS where we have since discovered |
| 1010 | the value of a symbol, or the address of the frag involved.) |
| 1011 | For each one, call md_apply_fix to put the fix into the frag data. |
| 1012 | |
| 1013 | Result is a count of how many relocation structs will be needed to |
| 1014 | handle the remaining fixS's that we couldn't completely handle here. |
| 1015 | These will be output later by emit_relocations(). */ |
| 1016 | |
| 1017 | static long |
| 1018 | fixup_segment (fixP, this_segment_type) |
| 1019 | register fixS *fixP; |
| 1020 | segT this_segment_type; /* N_TYPE bits for segment. */ |
| 1021 | { |
| 1022 | register long seg_reloc_count; |
| 1023 | register symbolS *add_symbolP; |
| 1024 | register symbolS *sub_symbolP; |
| 1025 | register long add_number; |
| 1026 | register int size; |
| 1027 | register char *place; |
| 1028 | register long where; |
| 1029 | register char pcrel; |
| 1030 | register fragS *fragP; |
| 1031 | register segT add_symbol_segment = SEG_ABSOLUTE; |
| 1032 | |
| 1033 | /* FIXME: remove this line *//* fixS *orig = fixP; */ |
| 1034 | seg_reloc_count = 0; |
| 1035 | #ifdef TC_I960 |
| 1036 | /* If the linker is doing the relaxing, we must not do any fixups */ |
| 1037 | if (linkrelax) |
| 1038 | { |
| 1039 | for (; fixP; fixP = fixP->fx_next) |
| 1040 | { |
| 1041 | seg_reloc_count++; |
| 1042 | } |
| 1043 | } |
| 1044 | else |
| 1045 | #endif |
| 1046 | for (; fixP; fixP = fixP->fx_next) |
| 1047 | { |
| 1048 | fragP = fixP->fx_frag; |
| 1049 | know (fragP); |
| 1050 | where = fixP->fx_where; |
| 1051 | place = fragP->fr_literal + where; |
| 1052 | size = fixP->fx_size; |
| 1053 | add_symbolP = fixP->fx_addsy; |
| 1054 | #ifdef TC_I960 |
| 1055 | if (fixP->fx_callj && TC_S_IS_CALLNAME (add_symbolP)) |
| 1056 | { |
| 1057 | /* Relocation should be done via the |
| 1058 | associated 'bal' entry point |
| 1059 | symbol. */ |
| 1060 | |
| 1061 | if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP))) |
| 1062 | { |
| 1063 | as_bad ("No 'bal' entry point for leafproc %s", |
| 1064 | S_GET_NAME (add_symbolP)); |
| 1065 | continue; |
| 1066 | } |
| 1067 | fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP); |
| 1068 | } /* callj relocation */ |
| 1069 | #endif |
| 1070 | sub_symbolP = fixP->fx_subsy; |
| 1071 | add_number = fixP->fx_offset; |
| 1072 | pcrel = fixP->fx_pcrel; |
| 1073 | |
| 1074 | if (add_symbolP) |
| 1075 | { |
| 1076 | add_symbol_segment = S_GET_SEGMENT (add_symbolP); |
| 1077 | } /* if there is an addend */ |
| 1078 | |
| 1079 | if (sub_symbolP) |
| 1080 | { |
| 1081 | if (!add_symbolP) |
| 1082 | { |
| 1083 | /* Its just -sym */ |
| 1084 | if (S_GET_SEGMENT (sub_symbolP) != SEG_ABSOLUTE) |
| 1085 | { |
| 1086 | as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP)); |
| 1087 | } /* not absolute */ |
| 1088 | |
| 1089 | add_number -= S_GET_VALUE (sub_symbolP); |
| 1090 | |
| 1091 | /* if sub_symbol is in the same segment that add_symbol |
| 1092 | and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */ |
| 1093 | } |
| 1094 | else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment) |
| 1095 | && (SEG_NORMAL (add_symbol_segment) |
| 1096 | || (add_symbol_segment == SEG_ABSOLUTE))) |
| 1097 | { |
| 1098 | /* Difference of 2 symbols from same segment. */ |
| 1099 | /* Can't make difference of 2 undefineds: 'value' means */ |
| 1100 | /* something different for N_UNDF. */ |
| 1101 | #ifdef TC_I960 |
| 1102 | /* Makes no sense to use the difference of 2 arbitrary symbols |
| 1103 | * as the target of a call instruction. |
| 1104 | */ |
| 1105 | if (fixP->fx_callj) |
| 1106 | { |
| 1107 | as_bad ("callj to difference of 2 symbols"); |
| 1108 | } |
| 1109 | #endif /* TC_I960 */ |
| 1110 | add_number += S_GET_VALUE (add_symbolP) - |
| 1111 | S_GET_VALUE (sub_symbolP); |
| 1112 | |
| 1113 | add_symbolP = NULL; |
| 1114 | fixP->fx_addsy = NULL; |
| 1115 | } |
| 1116 | else |
| 1117 | { |
| 1118 | /* Different segments in subtraction. */ |
| 1119 | know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE))); |
| 1120 | |
| 1121 | if ((S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE)) |
| 1122 | { |
| 1123 | add_number -= S_GET_VALUE (sub_symbolP); |
| 1124 | } |
| 1125 | else |
| 1126 | { |
| 1127 | as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.", |
| 1128 | segment_name (S_GET_SEGMENT (sub_symbolP)), |
| 1129 | S_GET_NAME (sub_symbolP), fragP->fr_address + where); |
| 1130 | } /* if absolute */ |
| 1131 | } |
| 1132 | } /* if sub_symbolP */ |
| 1133 | |
| 1134 | if (add_symbolP) |
| 1135 | { |
| 1136 | if (add_symbol_segment == this_segment_type && pcrel) |
| 1137 | { |
| 1138 | /* |
| 1139 | * This fixup was made when the symbol's segment was |
| 1140 | * SEG_UNKNOWN, but it is now in the local segment. |
| 1141 | * So we know how to do the address without relocation. |
| 1142 | */ |
| 1143 | #ifdef TC_I960 |
| 1144 | /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal', |
| 1145 | * in which cases it modifies *fixP as appropriate. In the case |
| 1146 | * of a 'calls', no further work is required, and *fixP has been |
| 1147 | * set up to make the rest of the code below a no-op. |
| 1148 | */ |
| 1149 | reloc_callj (fixP); |
| 1150 | #endif /* TC_I960 */ |
| 1151 | |
| 1152 | add_number += S_GET_VALUE (add_symbolP); |
| 1153 | add_number -= md_pcrel_from (fixP); |
| 1154 | pcrel = 0; /* Lie. Don't want further pcrel processing. */ |
| 1155 | fixP->fx_addsy = NULL; /* No relocations please. */ |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | switch (add_symbol_segment) |
| 1160 | { |
| 1161 | case SEG_ABSOLUTE: |
| 1162 | #ifdef TC_I960 |
| 1163 | reloc_callj (fixP); /* See comment about reloc_callj() above*/ |
| 1164 | #endif /* TC_I960 */ |
| 1165 | add_number += S_GET_VALUE (add_symbolP); |
| 1166 | fixP->fx_addsy = NULL; |
| 1167 | add_symbolP = NULL; |
| 1168 | break; |
| 1169 | default: |
| 1170 | seg_reloc_count++; |
| 1171 | add_number += S_GET_VALUE (add_symbolP); |
| 1172 | break; |
| 1173 | |
| 1174 | case SEG_UNKNOWN: |
| 1175 | #ifdef TC_I960 |
| 1176 | if ((int) fixP->fx_bit_fixP == 13) |
| 1177 | { |
| 1178 | /* This is a COBR instruction. They have only a |
| 1179 | * 13-bit displacement and are only to be used |
| 1180 | * for local branches: flag as error, don't generate |
| 1181 | * relocation. |
| 1182 | */ |
| 1183 | as_bad ("can't use COBR format with external label"); |
| 1184 | fixP->fx_addsy = NULL; /* No relocations please. */ |
| 1185 | continue; |
| 1186 | } /* COBR */ |
| 1187 | #endif /* TC_I960 */ |
| 1188 | |
| 1189 | #ifdef OBJ_COFF |
| 1190 | #ifdef TE_I386AIX |
| 1191 | if (S_IS_COMMON (add_symbolP)) |
| 1192 | add_number += S_GET_VALUE (add_symbolP); |
| 1193 | #endif /* TE_I386AIX */ |
| 1194 | #endif /* OBJ_COFF */ |
| 1195 | ++seg_reloc_count; |
| 1196 | |
| 1197 | break; |
| 1198 | |
| 1199 | |
| 1200 | } /* switch on symbol seg */ |
| 1201 | } /* if not in local seg */ |
| 1202 | } /* if there was a + symbol */ |
| 1203 | |
| 1204 | if (pcrel) |
| 1205 | { |
| 1206 | add_number -= md_pcrel_from (fixP); |
| 1207 | if (add_symbolP == 0) |
| 1208 | { |
| 1209 | fixP->fx_addsy = &abs_symbol; |
| 1210 | ++seg_reloc_count; |
| 1211 | } /* if there's an add_symbol */ |
| 1212 | } /* if pcrel */ |
| 1213 | |
| 1214 | if (!fixP->fx_bit_fixP) |
| 1215 | { |
| 1216 | if ((size == 1 && |
| 1217 | (add_number & ~0xFF) && ((add_number & ~0xFF) != (-1 & ~0xFF))) || |
| 1218 | (size == 2 && |
| 1219 | (add_number & ~0xFFFF) && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF)))) |
| 1220 | { |
| 1221 | as_bad ("Value of %d too large for field of %d bytes at 0x%x", |
| 1222 | add_number, size, fragP->fr_address + where); |
| 1223 | } /* generic error checking */ |
| 1224 | #ifdef WARN_SIGNED_OVERFLOW_WORD |
| 1225 | /* Warn if a .word value is too large when |
| 1226 | treated as a signed number. We already |
| 1227 | know it is not too negative. This is to |
| 1228 | catch over-large switches generated by gcc |
| 1229 | on the 68k. */ |
| 1230 | if (!flagseen['J'] |
| 1231 | && size == 2 |
| 1232 | && add_number > 0x7fff) |
| 1233 | as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x", |
| 1234 | add_number, fragP->fr_address + where); |
| 1235 | #endif |
| 1236 | } /* not a bit fix */ |
| 1237 | |
| 1238 | md_apply_fix (fixP, add_number); |
| 1239 | } /* For each fixS in this segment. */ |
| 1240 | |
| 1241 | #ifdef OBJ_COFF |
| 1242 | #ifdef TC_I960 |
| 1243 | { |
| 1244 | fixS *topP = fixP; |
| 1245 | |
| 1246 | /* two relocs per callj under coff. */ |
| 1247 | for (fixP = topP; fixP; fixP = fixP->fx_next) |
| 1248 | { |
| 1249 | if (fixP->fx_callj && fixP->fx_addsy != 0) |
| 1250 | { |
| 1251 | ++seg_reloc_count; |
| 1252 | } /* if callj and not already fixed. */ |
| 1253 | } /* for each fix */ |
| 1254 | } |
| 1255 | #endif /* TC_I960 */ |
| 1256 | |
| 1257 | #endif /* OBJ_COFF */ |
| 1258 | return (seg_reloc_count); |
| 1259 | } /* fixup_segment() */ |
| 1260 | |
| 1261 | |
| 1262 | static int |
| 1263 | is_dnrange (f1, f2) |
| 1264 | struct frag *f1; |
| 1265 | struct frag *f2; |
| 1266 | { |
| 1267 | while (f1) |
| 1268 | { |
| 1269 | if (f1->fr_next == f2) |
| 1270 | return 1; |
| 1271 | f1 = f1->fr_next; |
| 1272 | } |
| 1273 | return 0; |
| 1274 | } /* is_dnrange() */ |
| 1275 | |
| 1276 | /* Append a string onto another string, bumping the pointer along. */ |
| 1277 | void |
| 1278 | append (charPP, fromP, length) |
| 1279 | char **charPP; |
| 1280 | char *fromP; |
| 1281 | unsigned long length; |
| 1282 | { |
| 1283 | if (length) |
| 1284 | { /* Don't trust memcpy() of 0 chars. */ |
| 1285 | memcpy (*charPP, fromP, (int) length); |
| 1286 | *charPP += length; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | int section_alignment[SEG_MAXIMUM_ORDINAL]; |
| 1291 | |
| 1292 | /* |
| 1293 | * This routine records the largest alignment seen for each segment. |
| 1294 | * If the beginning of the segment is aligned on the worst-case |
| 1295 | * boundary, all of the other alignments within it will work. At |
| 1296 | * least one object format really uses this info. |
| 1297 | */ |
| 1298 | void |
| 1299 | record_alignment (seg, align) |
| 1300 | segT seg; /* Segment to which alignment pertains */ |
| 1301 | int align; /* Alignment, as a power of 2 |
| 1302 | * (e.g., 1 => 2-byte boundary, 2 => 4-byte boundary, etc.) |
| 1303 | */ |
| 1304 | { |
| 1305 | |
| 1306 | if (align > section_alignment[(int) seg]) |
| 1307 | { |
| 1308 | section_alignment[(int) seg] = align; |
| 1309 | } /* if highest yet */ |
| 1310 | |
| 1311 | return; |
| 1312 | } /* record_alignment() */ |
| 1313 | |
| 1314 | /* |
| 1315 | * Local Variables: |
| 1316 | * comment-column: 0 |
| 1317 | * fill-column: 131 |
| 1318 | * End: |
| 1319 | */ |
| 1320 | |
| 1321 | /* end of write.c */ |