1 /* write.c - emit .o file 2 Copyright (C) 1986-2022 Free Software Foundation, Inc. 3 4 This file is part of GAS, the GNU Assembler. 5 6 GAS is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3, or (at your option) 9 any later version. 10 11 GAS is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GAS; see the file COPYING. If not, write to the Free 18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 19 02110-1301, USA. */ 20 21 /* This thing should be set up to do byte ordering correctly. But... */ 22 23 #include "as.h" 24 #include "subsegs.h" 25 #include "obstack.h" 26 #include "output-file.h" 27 #include "dwarf2dbg.h" 28 #include "compress-debug.h" 29 30 #ifndef TC_FORCE_RELOCATION 31 #define TC_FORCE_RELOCATION(FIX) \ 32 (generic_force_reloc (FIX)) 33 #endif 34 35 #ifndef TC_FORCE_RELOCATION_ABS 36 #define TC_FORCE_RELOCATION_ABS(FIX) \ 37 (TC_FORCE_RELOCATION (FIX)) 38 #endif 39 40 #define GENERIC_FORCE_RELOCATION_LOCAL(FIX) \ 41 (!(FIX)->fx_pcrel \ 42 || TC_FORCE_RELOCATION (FIX)) 43 #ifndef TC_FORCE_RELOCATION_LOCAL 44 #define TC_FORCE_RELOCATION_LOCAL GENERIC_FORCE_RELOCATION_LOCAL 45 #endif 46 47 #define GENERIC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \ 48 (!SEG_NORMAL (SEG)) 49 #ifndef TC_FORCE_RELOCATION_SUB_SAME 50 #define TC_FORCE_RELOCATION_SUB_SAME GENERIC_FORCE_RELOCATION_SUB_SAME 51 #endif 52 53 #ifndef md_register_arithmetic 54 # define md_register_arithmetic 1 55 #endif 56 57 #ifndef TC_FORCE_RELOCATION_SUB_ABS 58 #define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG) \ 59 (!md_register_arithmetic && (SEG) == reg_section) 60 #endif 61 62 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL 63 #ifdef DIFF_EXPR_OK 64 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) \ 65 (!md_register_arithmetic && (SEG) == reg_section) 66 #else 67 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) 1 68 #endif 69 #endif 70 71 #ifndef TC_VALIDATE_FIX_SUB 72 #define TC_VALIDATE_FIX_SUB(FIX, SEG) 0 73 #endif 74 75 #ifndef TC_LINKRELAX_FIXUP 76 #define TC_LINKRELAX_FIXUP(SEG) 1 77 #endif 78 79 #ifndef MD_APPLY_SYM_VALUE 80 #define MD_APPLY_SYM_VALUE(FIX) 1 81 #endif 82 83 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 84 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1 85 #endif 86 87 #ifndef MD_PCREL_FROM_SECTION 88 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX) 89 #endif 90 91 #ifndef TC_FAKE_LABEL 92 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0) 93 #endif 94 95 /* Positive values of TC_FX_SIZE_SLACK allow a target to define 96 fixups that far past the end of a frag. Having such fixups 97 is of course most most likely a bug in setting fx_size correctly. 98 A negative value disables the fixup check entirely, which is 99 appropriate for something like the Renesas / SuperH SH_COUNT 100 reloc. */ 101 #ifndef TC_FX_SIZE_SLACK 102 #define TC_FX_SIZE_SLACK(FIX) 0 103 #endif 104 105 /* Used to control final evaluation of expressions. */ 106 int finalize_syms = 0; 107 108 int symbol_table_frozen; 109 110 symbolS *abs_section_sym; 111 112 /* Remember the value of dot when parsing expressions. */ 113 addressT dot_value; 114 115 /* The frag that dot_value is based from. */ 116 fragS *dot_frag; 117 118 /* Relocs generated by ".reloc" pseudo. */ 119 struct reloc_list* reloc_list; 120 121 void print_fixup (fixS *); 122 123 /* We generally attach relocs to frag chains. However, after we have 124 chained these all together into a segment, any relocs we add after 125 that must be attached to a segment. This will include relocs added 126 in md_estimate_size_for_relax, for example. */ 127 static int frags_chained = 0; 128 129 static int n_fixups; 130 131 #define RELOC_ENUM enum bfd_reloc_code_real 132 133 /* Create a fixS in obstack 'notes'. */ 134 135 static fixS * 136 fix_new_internal (fragS *frag, /* Which frag? */ 137 unsigned long where, /* Where in that frag? */ 138 unsigned long size, /* 1, 2, or 4 usually. */ 139 symbolS *add_symbol, /* X_add_symbol. */ 140 symbolS *sub_symbol, /* X_op_symbol. */ 141 offsetT offset, /* X_add_number. */ 142 int pcrel, /* TRUE if PC-relative relocation. */ 143 RELOC_ENUM r_type /* Relocation type. */, 144 int at_beginning) /* Add to the start of the list? */ 145 { 146 fixS *fixP; 147 148 n_fixups++; 149 150 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS)); 151 152 fixP->fx_frag = frag; 153 fixP->fx_where = where; 154 fixP->fx_size = size; 155 /* We've made fx_size a narrow field; check that it's wide enough. */ 156 if (fixP->fx_size != size) 157 { 158 as_bad (_("field fx_size too small to hold %lu"), size); 159 abort (); 160 } 161 fixP->fx_addsy = add_symbol; 162 fixP->fx_subsy = sub_symbol; 163 fixP->fx_offset = offset; 164 fixP->fx_dot_value = dot_value; 165 fixP->fx_dot_frag = dot_frag; 166 fixP->fx_pcrel = pcrel; 167 fixP->fx_r_type = r_type; 168 fixP->fx_pcrel_adjust = 0; 169 fixP->fx_addnumber = 0; 170 fixP->fx_tcbit = 0; 171 fixP->fx_tcbit2 = 0; 172 fixP->fx_done = 0; 173 fixP->fx_no_overflow = 0; 174 fixP->fx_signed = 0; 175 176 #ifdef USING_CGEN 177 fixP->fx_cgen.insn = NULL; 178 fixP->fx_cgen.opinfo = 0; 179 #endif 180 181 #ifdef TC_FIX_TYPE 182 TC_INIT_FIX_DATA (fixP); 183 #endif 184 185 fixP->fx_file = as_where (&fixP->fx_line); 186 187 { 188 189 fixS **seg_fix_rootP = (frags_chained 190 ? &seg_info (now_seg)->fix_root 191 : &frchain_now->fix_root); 192 fixS **seg_fix_tailP = (frags_chained 193 ? &seg_info (now_seg)->fix_tail 194 : &frchain_now->fix_tail); 195 196 if (at_beginning) 197 { 198 fixP->fx_next = *seg_fix_rootP; 199 *seg_fix_rootP = fixP; 200 if (fixP->fx_next == NULL) 201 *seg_fix_tailP = fixP; 202 } 203 else 204 { 205 fixP->fx_next = NULL; 206 if (*seg_fix_tailP) 207 (*seg_fix_tailP)->fx_next = fixP; 208 else 209 *seg_fix_rootP = fixP; 210 *seg_fix_tailP = fixP; 211 } 212 } 213 214 return fixP; 215 } 216 217 /* Create a fixup relative to a symbol (plus a constant). */ 218 219 fixS * 220 fix_new (fragS *frag, /* Which frag? */ 221 unsigned long where, /* Where in that frag? */ 222 unsigned long size, /* 1, 2, or 4 usually. */ 223 symbolS *add_symbol, /* X_add_symbol. */ 224 offsetT offset, /* X_add_number. */ 225 int pcrel, /* TRUE if PC-relative relocation. */ 226 RELOC_ENUM r_type /* Relocation type. */) 227 { 228 return fix_new_internal (frag, where, size, add_symbol, 229 (symbolS *) NULL, offset, pcrel, r_type, false); 230 } 231 232 /* Create a fixup for an expression. Currently we only support fixups 233 for difference expressions. That is itself more than most object 234 file formats support anyhow. */ 235 236 fixS * 237 fix_new_exp (fragS *frag, /* Which frag? */ 238 unsigned long where, /* Where in that frag? */ 239 unsigned long size, /* 1, 2, or 4 usually. */ 240 expressionS *exp, /* Expression. */ 241 int pcrel, /* TRUE if PC-relative relocation. */ 242 RELOC_ENUM r_type /* Relocation type. */) 243 { 244 symbolS *add = NULL; 245 symbolS *sub = NULL; 246 offsetT off = 0; 247 248 switch (exp->X_op) 249 { 250 case O_absent: 251 break; 252 253 case O_register: 254 as_bad (_("register value used as expression")); 255 break; 256 257 case O_add: 258 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if 259 the difference expression cannot immediately be reduced. */ 260 { 261 symbolS *stmp = make_expr_symbol (exp); 262 263 exp->X_op = O_symbol; 264 exp->X_op_symbol = 0; 265 exp->X_add_symbol = stmp; 266 exp->X_add_number = 0; 267 268 return fix_new_exp (frag, where, size, exp, pcrel, r_type); 269 } 270 271 case O_symbol_rva: 272 add = exp->X_add_symbol; 273 off = exp->X_add_number; 274 r_type = BFD_RELOC_RVA; 275 break; 276 277 case O_uminus: 278 sub = exp->X_add_symbol; 279 off = exp->X_add_number; 280 break; 281 282 case O_subtract: 283 sub = exp->X_op_symbol; 284 /* Fall through. */ 285 case O_symbol: 286 add = exp->X_add_symbol; 287 /* Fall through. */ 288 case O_constant: 289 off = exp->X_add_number; 290 break; 291 292 default: 293 add = make_expr_symbol (exp); 294 break; 295 } 296 297 return fix_new_internal (frag, where, size, add, sub, off, pcrel, 298 r_type, false); 299 } 300 301 /* Create a fixup at the beginning of FRAG. The arguments are the same 302 as for fix_new, except that WHERE is implicitly 0. */ 303 304 fixS * 305 fix_at_start (fragS *frag, unsigned long size, symbolS *add_symbol, 306 offsetT offset, int pcrel, RELOC_ENUM r_type) 307 { 308 return fix_new_internal (frag, 0, size, add_symbol, 309 (symbolS *) NULL, offset, pcrel, r_type, true); 310 } 311 312 /* Generic function to determine whether a fixup requires a relocation. */ 313 int 314 generic_force_reloc (fixS *fix) 315 { 316 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT 317 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY) 318 return 1; 319 320 if (fix->fx_addsy == NULL) 321 return 0; 322 323 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL); 324 } 325 326 /* Append a string onto another string, bumping the pointer along. */ 327 void 328 append (char **charPP, char *fromP, unsigned long length) 329 { 330 /* Don't trust memcpy() of 0 chars. */ 331 if (length == 0) 332 return; 333 334 memcpy (*charPP, fromP, length); 335 *charPP += length; 336 } 337 338 /* This routine records the largest alignment seen for each segment. 339 If the beginning of the segment is aligned on the worst-case 340 boundary, all of the other alignments within it will work. At 341 least one object format really uses this info. */ 342 343 void 344 record_alignment (/* Segment to which alignment pertains. */ 345 segT seg, 346 /* Alignment, as a power of 2 (e.g., 1 => 2-byte 347 boundary, 2 => 4-byte boundary, etc.) */ 348 unsigned int align) 349 { 350 if (seg == absolute_section) 351 return; 352 353 if (align > bfd_section_alignment (seg)) 354 bfd_set_section_alignment (seg, align); 355 } 356 357 int 358 get_recorded_alignment (segT seg) 359 { 360 if (seg == absolute_section) 361 return 0; 362 363 return bfd_section_alignment (seg); 364 } 365 366 /* Reset the section indices after removing the gas created sections. */ 367 368 static void 369 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg) 370 { 371 int *countp = (int *) countparg; 372 373 sec->index = *countp; 374 ++*countp; 375 } 376 377 static fragS * 378 chain_frchains_together_1 (segT section, struct frchain *frchp) 379 { 380 fragS dummy, *prev_frag = &dummy; 381 fixS fix_dummy, *prev_fix = &fix_dummy; 382 383 for (; frchp; frchp = frchp->frch_next) 384 { 385 prev_frag->fr_next = frchp->frch_root; 386 prev_frag = frchp->frch_last; 387 gas_assert (prev_frag->fr_type != 0); 388 if (frchp->fix_root != (fixS *) NULL) 389 { 390 if (seg_info (section)->fix_root == (fixS *) NULL) 391 seg_info (section)->fix_root = frchp->fix_root; 392 prev_fix->fx_next = frchp->fix_root; 393 seg_info (section)->fix_tail = frchp->fix_tail; 394 prev_fix = frchp->fix_tail; 395 } 396 } 397 gas_assert (prev_frag != &dummy 398 && prev_frag->fr_type != 0); 399 prev_frag->fr_next = 0; 400 return prev_frag; 401 } 402 403 static void 404 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED, 405 segT section, 406 void *xxx ATTRIBUTE_UNUSED) 407 { 408 segment_info_type *info; 409 410 /* BFD may have introduced its own sections without using 411 subseg_new, so it is possible that seg_info is NULL. */ 412 info = seg_info (section); 413 if (info != (segment_info_type *) NULL) 414 info->frchainP->frch_last 415 = chain_frchains_together_1 (section, info->frchainP); 416 417 /* Now that we've chained the frags together, we must add new fixups 418 to the segment, not to the frag chain. */ 419 frags_chained = 1; 420 } 421 422 static void 423 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP) 424 { 425 switch (fragP->fr_type) 426 { 427 case rs_space_nop: 428 goto skip_align; 429 case rs_align: 430 case rs_align_code: 431 case rs_align_test: 432 case rs_org: 433 case rs_space: 434 #ifdef HANDLE_ALIGN 435 HANDLE_ALIGN (fragP); 436 #endif 437 skip_align: 438 know (fragP->fr_next != NULL); 439 fragP->fr_offset = (fragP->fr_next->fr_address 440 - fragP->fr_address 441 - fragP->fr_fix) / fragP->fr_var; 442 if (fragP->fr_offset < 0) 443 { 444 as_bad_where (fragP->fr_file, fragP->fr_line, 445 _("attempt to .org/.space/.nops backwards? (%ld)"), 446 (long) fragP->fr_offset); 447 fragP->fr_offset = 0; 448 } 449 if (fragP->fr_type == rs_space_nop) 450 fragP->fr_type = rs_fill_nop; 451 else 452 fragP->fr_type = rs_fill; 453 break; 454 455 case rs_fill: 456 case rs_fill_nop: 457 break; 458 459 case rs_leb128: 460 { 461 valueT value = S_GET_VALUE (fragP->fr_symbol); 462 int size; 463 464 if (!S_IS_DEFINED (fragP->fr_symbol)) 465 { 466 as_bad_where (fragP->fr_file, fragP->fr_line, 467 _("leb128 operand is an undefined symbol: %s"), 468 S_GET_NAME (fragP->fr_symbol)); 469 } 470 471 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value, 472 fragP->fr_subtype); 473 474 fragP->fr_fix += size; 475 fragP->fr_type = rs_fill; 476 fragP->fr_var = 0; 477 fragP->fr_offset = 0; 478 fragP->fr_symbol = NULL; 479 } 480 break; 481 482 case rs_cfa: 483 eh_frame_convert_frag (fragP); 484 break; 485 486 case rs_dwarf2dbg: 487 dwarf2dbg_convert_frag (fragP); 488 break; 489 490 case rs_machine_dependent: 491 md_convert_frag (stdoutput, sec, fragP); 492 493 gas_assert (fragP->fr_next == NULL 494 || (fragP->fr_next->fr_address - fragP->fr_address 495 == fragP->fr_fix)); 496 497 /* After md_convert_frag, we make the frag into a ".space 0". 498 md_convert_frag() should set up any fixSs and constants 499 required. */ 500 frag_wane (fragP); 501 break; 502 503 #ifndef WORKING_DOT_WORD 504 case rs_broken_word: 505 { 506 struct broken_word *lie; 507 508 if (fragP->fr_subtype) 509 { 510 fragP->fr_fix += md_short_jump_size; 511 for (lie = (struct broken_word *) (fragP->fr_symbol); 512 lie && lie->dispfrag == fragP; 513 lie = lie->next_broken_word) 514 if (lie->added == 1) 515 fragP->fr_fix += md_long_jump_size; 516 } 517 frag_wane (fragP); 518 } 519 break; 520 #endif 521 522 default: 523 BAD_CASE (fragP->fr_type); 524 break; 525 } 526 #ifdef md_frag_check 527 md_frag_check (fragP); 528 #endif 529 } 530 531 struct relax_seg_info 532 { 533 int pass; 534 int changed; 535 }; 536 537 static void 538 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx) 539 { 540 segment_info_type *seginfo = seg_info (sec); 541 struct relax_seg_info *info = (struct relax_seg_info *) xxx; 542 543 if (seginfo && seginfo->frchainP 544 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass)) 545 info->changed = 1; 546 } 547 548 static void 549 size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED) 550 { 551 flagword flags; 552 fragS *fragp; 553 segment_info_type *seginfo; 554 int x; 555 valueT size, newsize; 556 557 subseg_change (sec, 0); 558 559 seginfo = seg_info (sec); 560 if (seginfo && seginfo->frchainP) 561 { 562 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next) 563 cvt_frag_to_fill (sec, fragp); 564 for (fragp = seginfo->frchainP->frch_root; 565 fragp->fr_next; 566 fragp = fragp->fr_next) 567 /* Walk to last elt. */ 568 ; 569 size = fragp->fr_address + fragp->fr_fix; 570 } 571 else 572 size = 0; 573 574 flags = bfd_section_flags (sec); 575 if (size == 0 && bfd_section_size (sec) != 0 && 576 (flags & SEC_HAS_CONTENTS) != 0) 577 return; 578 579 if (size > 0 && ! seginfo->bss) 580 flags |= SEC_HAS_CONTENTS; 581 582 flags &= ~SEC_RELOC; 583 x = bfd_set_section_flags (sec, flags); 584 gas_assert (x); 585 586 /* If permitted, allow the backend to pad out the section 587 to some alignment boundary. */ 588 if (do_not_pad_sections_to_alignment) 589 newsize = size; 590 else 591 newsize = md_section_align (sec, size); 592 x = bfd_set_section_size (sec, newsize); 593 gas_assert (x); 594 595 /* If the size had to be rounded up, add some padding in the last 596 non-empty frag. */ 597 gas_assert (newsize >= size); 598 if (size != newsize) 599 { 600 fragS *last = seginfo->frchainP->frch_last; 601 fragp = seginfo->frchainP->frch_root; 602 while (fragp->fr_next != last) 603 fragp = fragp->fr_next; 604 last->fr_address = size; 605 if ((newsize - size) % fragp->fr_var == 0) 606 fragp->fr_offset += (newsize - size) / fragp->fr_var; 607 else 608 /* If we hit this abort, it's likely due to subsegs_finish not 609 providing sufficient alignment on the last frag, and the 610 machine dependent code using alignment frags with fr_var 611 greater than 1. */ 612 abort (); 613 } 614 615 #ifdef tc_frob_section 616 tc_frob_section (sec); 617 #endif 618 #ifdef obj_frob_section 619 obj_frob_section (sec); 620 #endif 621 } 622 623 #ifdef DEBUG2 624 static void 625 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream) 626 { 627 segment_info_type *seginfo = seg_info (sec); 628 fixS *fixp = seginfo->fix_root; 629 630 if (!fixp) 631 return; 632 633 fprintf (stream, "sec %s relocs:\n", sec->name); 634 while (fixp) 635 { 636 symbolS *s = fixp->fx_addsy; 637 638 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp, 639 (int) fixp->fx_r_type); 640 if (s == NULL) 641 fprintf (stream, "no sym\n"); 642 else 643 { 644 print_symbol_value_1 (stream, s); 645 fprintf (stream, "\n"); 646 } 647 fixp = fixp->fx_next; 648 } 649 } 650 #else 651 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0) 652 #endif 653 654 #ifndef EMIT_SECTION_SYMBOLS 655 #define EMIT_SECTION_SYMBOLS 1 656 #endif 657 658 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries, 659 and check for validity. Convert RELOC_LIST from using U.A fields 660 to U.B fields. */ 661 static void 662 resolve_reloc_expr_symbols (void) 663 { 664 bfd_vma addr_mask = 1; 665 struct reloc_list *r; 666 667 /* Avoid a shift by the width of type. */ 668 addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1; 669 addr_mask <<= 1; 670 addr_mask -= 1; 671 672 for (r = reloc_list; r; r = r->next) 673 { 674 reloc_howto_type *howto = r->u.a.howto; 675 expressionS *symval; 676 symbolS *sym; 677 bfd_vma offset, addend; 678 asection *sec; 679 680 resolve_symbol_value (r->u.a.offset_sym); 681 symval = symbol_get_value_expression (r->u.a.offset_sym); 682 683 offset = 0; 684 sym = NULL; 685 if (symval->X_op == O_constant) 686 sym = r->u.a.offset_sym; 687 else if (symval->X_op == O_symbol) 688 { 689 sym = symval->X_add_symbol; 690 offset = symval->X_add_number; 691 symval = symbol_get_value_expression (symval->X_add_symbol); 692 } 693 if (sym == NULL 694 || symval->X_op != O_constant 695 || (sec = S_GET_SEGMENT (sym)) == NULL 696 || !SEG_NORMAL (sec)) 697 { 698 as_bad_where (r->file, r->line, _("invalid offset expression")); 699 sec = NULL; 700 } 701 else 702 offset += S_GET_VALUE (sym); 703 704 sym = NULL; 705 addend = r->u.a.addend; 706 if (r->u.a.sym != NULL) 707 { 708 resolve_symbol_value (r->u.a.sym); 709 symval = symbol_get_value_expression (r->u.a.sym); 710 if (symval->X_op == O_constant) 711 sym = r->u.a.sym; 712 else if (symval->X_op == O_symbol) 713 { 714 sym = symval->X_add_symbol; 715 addend += symval->X_add_number; 716 symval = symbol_get_value_expression (symval->X_add_symbol); 717 } 718 if (symval->X_op != O_constant) 719 { 720 as_bad_where (r->file, r->line, _("invalid reloc expression")); 721 sec = NULL; 722 } 723 else if (sym != NULL && sec != NULL) 724 { 725 /* Convert relocs against local symbols to refer to the 726 corresponding section symbol plus offset instead. Keep 727 PC-relative relocs of the REL variety intact though to 728 prevent the offset from overflowing the relocated field, 729 unless it has enough bits to cover the whole address 730 space. */ 731 if (S_IS_LOCAL (sym) 732 && S_IS_DEFINED (sym) 733 && !symbol_section_p (sym) 734 && (sec->use_rela_p 735 || (howto->partial_inplace 736 && (!howto->pc_relative 737 || howto->src_mask == addr_mask)))) 738 { 739 asection *symsec = S_GET_SEGMENT (sym); 740 if (!(((symsec->flags & SEC_MERGE) != 0 741 && addend != 0) 742 || (symsec->flags & SEC_THREAD_LOCAL) != 0)) 743 { 744 addend += S_GET_VALUE (sym); 745 sym = section_symbol (symsec); 746 } 747 } 748 symbol_mark_used_in_reloc (sym); 749 } 750 } 751 if (sym == NULL) 752 { 753 if (abs_section_sym == NULL) 754 abs_section_sym = section_symbol (absolute_section); 755 sym = abs_section_sym; 756 } 757 758 r->u.b.sec = sec; 759 r->u.b.s = symbol_get_bfdsym (sym); 760 r->u.b.r.sym_ptr_ptr = &r->u.b.s; 761 r->u.b.r.address = offset; 762 r->u.b.r.addend = addend; 763 r->u.b.r.howto = howto; 764 } 765 } 766 767 /* This pass over fixups decides whether symbols can be replaced with 768 section symbols. */ 769 770 static void 771 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED, 772 asection *sec, 773 void *xxx ATTRIBUTE_UNUSED) 774 { 775 segment_info_type *seginfo = seg_info (sec); 776 fixS *fixp; 777 778 if (seginfo == NULL) 779 return; 780 781 dump_section_relocs (abfd, sec, stderr); 782 783 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next) 784 if (fixp->fx_done) 785 /* Ignore it. */ 786 ; 787 else if (fixp->fx_addsy) 788 { 789 symbolS *sym; 790 asection *symsec; 791 792 #ifdef DEBUG5 793 fprintf (stderr, "\n\nadjusting fixup:\n"); 794 print_fixup (fixp); 795 #endif 796 797 sym = fixp->fx_addsy; 798 799 /* All symbols should have already been resolved at this 800 point. It is possible to see unresolved expression 801 symbols, though, since they are not in the regular symbol 802 table. */ 803 resolve_symbol_value (sym); 804 805 if (fixp->fx_subsy != NULL) 806 resolve_symbol_value (fixp->fx_subsy); 807 808 /* If this symbol is equated to an undefined or common symbol, 809 convert the fixup to being against that symbol. */ 810 while (symbol_equated_reloc_p (sym) 811 || S_IS_WEAKREFR (sym)) 812 { 813 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol; 814 if (sym == newsym) 815 break; 816 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number; 817 fixp->fx_addsy = newsym; 818 sym = newsym; 819 } 820 821 if (symbol_mri_common_p (sym)) 822 { 823 fixp->fx_offset += S_GET_VALUE (sym); 824 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol; 825 continue; 826 } 827 828 /* If the symbol is undefined, common, weak, or global (ELF 829 shared libs), we can't replace it with the section symbol. */ 830 if (S_FORCE_RELOC (fixp->fx_addsy, 1)) 831 continue; 832 833 /* Is there some other (target cpu dependent) reason we can't adjust 834 this one? (E.g. relocations involving function addresses on 835 the PA. */ 836 #ifdef tc_fix_adjustable 837 if (! tc_fix_adjustable (fixp)) 838 continue; 839 #endif 840 841 /* Since we're reducing to section symbols, don't attempt to reduce 842 anything that's already using one. */ 843 if (symbol_section_p (sym)) 844 { 845 /* Mark the section symbol used in relocation so that it will 846 be included in the symbol table. */ 847 symbol_mark_used_in_reloc (sym); 848 continue; 849 } 850 851 symsec = S_GET_SEGMENT (sym); 852 if (symsec == NULL) 853 abort (); 854 855 if (bfd_is_abs_section (symsec) 856 || symsec == reg_section) 857 { 858 /* The fixup_segment routine normally will not use this 859 symbol in a relocation. */ 860 continue; 861 } 862 863 /* Don't try to reduce relocs which refer to non-local symbols 864 in .linkonce sections. It can lead to confusion when a 865 debugging section refers to a .linkonce section. I hope 866 this will always be correct. */ 867 if (symsec != sec && ! S_IS_LOCAL (sym)) 868 { 869 if ((symsec->flags & SEC_LINK_ONCE) != 0 870 || (IS_ELF 871 /* The GNU toolchain uses an extension for ELF: a 872 section beginning with the magic string 873 .gnu.linkonce is a linkonce section. */ 874 && startswith (segment_name (symsec), ".gnu.linkonce"))) 875 continue; 876 } 877 878 /* Never adjust a reloc against local symbol in a merge section 879 with non-zero addend. */ 880 if ((symsec->flags & SEC_MERGE) != 0 881 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL)) 882 continue; 883 884 /* Never adjust a reloc against TLS local symbol. */ 885 if ((symsec->flags & SEC_THREAD_LOCAL) != 0) 886 continue; 887 888 /* We refetch the segment when calling section_symbol, rather 889 than using symsec, because S_GET_VALUE may wind up changing 890 the section when it calls resolve_symbol_value. */ 891 fixp->fx_offset += S_GET_VALUE (sym); 892 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym)); 893 #ifdef DEBUG5 894 fprintf (stderr, "\nadjusted fixup:\n"); 895 print_fixup (fixp); 896 #endif 897 } 898 899 dump_section_relocs (abfd, sec, stderr); 900 } 901 902 void 903 as_bad_subtract (fixS *fixp) 904 { 905 as_bad_where (fixp->fx_file, fixp->fx_line, 906 _("can't resolve %s - %s"), 907 fixp->fx_addsy ? S_GET_NAME (fixp->fx_addsy) : "0", 908 S_GET_NAME (fixp->fx_subsy)); 909 } 910 911 /* fixup_segment() 912 913 Go through all the fixS's in a segment and see which ones can be 914 handled now. (These consist of fixS where we have since discovered 915 the value of a symbol, or the address of the frag involved.) 916 For each one, call md_apply_fix to put the fix into the frag data. 917 Ones that we couldn't completely handle here will be output later 918 by emit_relocations. */ 919 920 static void 921 fixup_segment (fixS *fixP, segT this_segment) 922 { 923 valueT add_number; 924 fragS *fragP; 925 926 if (fixP != NULL && abs_section_sym == NULL) 927 abs_section_sym = section_symbol (absolute_section); 928 929 /* If the linker is doing the relaxing, we must not do any fixups. 930 931 Well, strictly speaking that's not true -- we could do any that 932 are PC-relative and don't cross regions that could change size. */ 933 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment)) 934 { 935 for (; fixP; fixP = fixP->fx_next) 936 if (!fixP->fx_done) 937 { 938 if (fixP->fx_addsy == NULL) 939 { 940 /* There was no symbol required by this relocation. 941 However, BFD doesn't really handle relocations 942 without symbols well. So fake up a local symbol in 943 the absolute section. */ 944 fixP->fx_addsy = abs_section_sym; 945 } 946 symbol_mark_used_in_reloc (fixP->fx_addsy); 947 if (fixP->fx_subsy != NULL) 948 symbol_mark_used_in_reloc (fixP->fx_subsy); 949 } 950 return; 951 } 952 953 for (; fixP; fixP = fixP->fx_next) 954 { 955 segT add_symbol_segment = absolute_section; 956 957 #ifdef DEBUG5 958 fprintf (stderr, "\nprocessing fixup:\n"); 959 print_fixup (fixP); 960 #endif 961 962 fragP = fixP->fx_frag; 963 know (fragP); 964 #ifdef TC_VALIDATE_FIX 965 TC_VALIDATE_FIX (fixP, this_segment, skip); 966 #endif 967 add_number = fixP->fx_offset; 968 969 if (fixP->fx_addsy != NULL) 970 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy); 971 972 if (fixP->fx_subsy != NULL) 973 { 974 segT sub_symbol_segment; 975 resolve_symbol_value (fixP->fx_subsy); 976 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy); 977 if (fixP->fx_addsy != NULL 978 && sub_symbol_segment == add_symbol_segment 979 && !S_FORCE_RELOC (fixP->fx_addsy, 0) 980 && !S_FORCE_RELOC (fixP->fx_subsy, 0) 981 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment)) 982 { 983 add_number += S_GET_VALUE (fixP->fx_addsy); 984 add_number -= S_GET_VALUE (fixP->fx_subsy); 985 fixP->fx_offset = add_number; 986 fixP->fx_addsy = NULL; 987 fixP->fx_subsy = NULL; 988 #ifdef TC_M68K 989 /* See the comment below about 68k weirdness. */ 990 fixP->fx_pcrel = 0; 991 #endif 992 } 993 else if (sub_symbol_segment == absolute_section 994 && !S_FORCE_RELOC (fixP->fx_subsy, 0) 995 && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment)) 996 { 997 add_number -= S_GET_VALUE (fixP->fx_subsy); 998 fixP->fx_offset = add_number; 999 fixP->fx_subsy = NULL; 1000 } 1001 else if (sub_symbol_segment == this_segment 1002 && !S_FORCE_RELOC (fixP->fx_subsy, 0) 1003 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment)) 1004 { 1005 add_number -= S_GET_VALUE (fixP->fx_subsy); 1006 fixP->fx_offset = (add_number + fixP->fx_dot_value 1007 + fixP->fx_dot_frag->fr_address); 1008 1009 /* Make it pc-relative. If the back-end code has not 1010 selected a pc-relative reloc, cancel the adjustment 1011 we do later on all pc-relative relocs. */ 1012 if (0 1013 #ifdef TC_M68K 1014 /* Do this for m68k even if it's already described 1015 as pc-relative. On the m68k, an operand of 1016 "pc@(foo-.-2)" should address "foo" in a 1017 pc-relative mode. */ 1018 || 1 1019 #endif 1020 || !fixP->fx_pcrel) 1021 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment); 1022 fixP->fx_subsy = NULL; 1023 fixP->fx_pcrel = 1; 1024 } 1025 else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment)) 1026 { 1027 if (!md_register_arithmetic 1028 && (add_symbol_segment == reg_section 1029 || sub_symbol_segment == reg_section)) 1030 as_bad_where (fixP->fx_file, fixP->fx_line, 1031 _("register value used as expression")); 1032 else 1033 as_bad_subtract (fixP); 1034 } 1035 else if (sub_symbol_segment != undefined_section 1036 && ! bfd_is_com_section (sub_symbol_segment) 1037 && MD_APPLY_SYM_VALUE (fixP)) 1038 add_number -= S_GET_VALUE (fixP->fx_subsy); 1039 } 1040 1041 if (fixP->fx_addsy) 1042 { 1043 if (add_symbol_segment == this_segment 1044 && !S_FORCE_RELOC (fixP->fx_addsy, 0) 1045 && !TC_FORCE_RELOCATION_LOCAL (fixP)) 1046 { 1047 /* This fixup was made when the symbol's segment was 1048 SEG_UNKNOWN, but it is now in the local segment. 1049 So we know how to do the address without relocation. */ 1050 add_number += S_GET_VALUE (fixP->fx_addsy); 1051 fixP->fx_offset = add_number; 1052 if (fixP->fx_pcrel) 1053 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment); 1054 fixP->fx_addsy = NULL; 1055 fixP->fx_pcrel = 0; 1056 } 1057 else if (add_symbol_segment == absolute_section 1058 && !S_FORCE_RELOC (fixP->fx_addsy, 0) 1059 && !TC_FORCE_RELOCATION_ABS (fixP)) 1060 { 1061 add_number += S_GET_VALUE (fixP->fx_addsy); 1062 fixP->fx_offset = add_number; 1063 fixP->fx_addsy = NULL; 1064 } 1065 else if (add_symbol_segment != undefined_section 1066 && ! bfd_is_com_section (add_symbol_segment) 1067 && MD_APPLY_SYM_VALUE (fixP)) 1068 add_number += S_GET_VALUE (fixP->fx_addsy); 1069 } 1070 1071 if (fixP->fx_pcrel) 1072 { 1073 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment); 1074 if (!fixP->fx_done && fixP->fx_addsy == NULL) 1075 { 1076 /* There was no symbol required by this relocation. 1077 However, BFD doesn't really handle relocations 1078 without symbols well. So fake up a local symbol in 1079 the absolute section. */ 1080 fixP->fx_addsy = abs_section_sym; 1081 } 1082 } 1083 1084 if (!fixP->fx_done) 1085 md_apply_fix (fixP, &add_number, this_segment); 1086 1087 if (!fixP->fx_done) 1088 { 1089 if (fixP->fx_addsy == NULL) 1090 fixP->fx_addsy = abs_section_sym; 1091 symbol_mark_used_in_reloc (fixP->fx_addsy); 1092 if (fixP->fx_subsy != NULL) 1093 symbol_mark_used_in_reloc (fixP->fx_subsy); 1094 } 1095 1096 if (!fixP->fx_no_overflow && fixP->fx_size != 0) 1097 { 1098 if (fixP->fx_size < sizeof (valueT)) 1099 { 1100 valueT mask; 1101 1102 mask = 0; 1103 mask--; /* Set all bits to one. */ 1104 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0); 1105 if ((add_number & mask) != 0 1106 && (fixP->fx_signed 1107 ? (add_number & mask) != mask 1108 : (-add_number & mask) != 0)) 1109 { 1110 char buf[50], buf2[50]; 1111 bfd_sprintf_vma (stdoutput, buf, fragP->fr_address + fixP->fx_where); 1112 if (add_number > 1000) 1113 bfd_sprintf_vma (stdoutput, buf2, add_number); 1114 else 1115 sprintf (buf2, "%ld", (long) add_number); 1116 as_bad_where (fixP->fx_file, fixP->fx_line, 1117 ngettext ("value of %s too large for field " 1118 "of %d byte at %s", 1119 "value of %s too large for field " 1120 "of %d bytes at %s", 1121 fixP->fx_size), 1122 buf2, fixP->fx_size, buf); 1123 } /* Generic error checking. */ 1124 } 1125 #ifdef WARN_SIGNED_OVERFLOW_WORD 1126 /* Warn if a .word value is too large when treated as a signed 1127 number. We already know it is not too negative. This is to 1128 catch over-large switches generated by gcc on the 68k. */ 1129 if (!flag_signed_overflow_ok 1130 && fixP->fx_size == 2 1131 && add_number > 0x7fff) 1132 as_bad_where (fixP->fx_file, fixP->fx_line, 1133 _("signed .word overflow; switch may be too large; %ld at 0x%lx"), 1134 (long) add_number, 1135 (long) (fragP->fr_address + fixP->fx_where)); 1136 #endif 1137 } 1138 1139 #ifdef TC_VALIDATE_FIX 1140 skip: ATTRIBUTE_UNUSED_LABEL 1141 ; 1142 #endif 1143 #ifdef DEBUG5 1144 fprintf (stderr, "result:\n"); 1145 print_fixup (fixP); 1146 #endif 1147 } /* For each fixS in this segment. */ 1148 } 1149 1150 static void 1151 fix_segment (bfd *abfd ATTRIBUTE_UNUSED, 1152 asection *sec, 1153 void *xxx ATTRIBUTE_UNUSED) 1154 { 1155 segment_info_type *seginfo = seg_info (sec); 1156 1157 fixup_segment (seginfo->fix_root, sec); 1158 } 1159 1160 static void 1161 install_reloc (asection *sec, arelent *reloc, fragS *fragp, 1162 const char *file, unsigned int line) 1163 { 1164 char *err; 1165 bfd_reloc_status_type s; 1166 asymbol *sym; 1167 1168 if (reloc->sym_ptr_ptr != NULL 1169 && (sym = *reloc->sym_ptr_ptr) != NULL 1170 && (sym->flags & BSF_KEEP) == 0 1171 && ((sym->flags & BSF_SECTION_SYM) == 0 1172 || (EMIT_SECTION_SYMBOLS 1173 && !bfd_is_abs_section (sym->section)))) 1174 as_bad_where (file, line, _("redefined symbol cannot be used on reloc")); 1175 1176 s = bfd_install_relocation (stdoutput, reloc, 1177 fragp->fr_literal, fragp->fr_address, 1178 sec, &err); 1179 switch (s) 1180 { 1181 case bfd_reloc_ok: 1182 break; 1183 case bfd_reloc_overflow: 1184 as_bad_where (file, line, _("relocation overflow")); 1185 break; 1186 case bfd_reloc_outofrange: 1187 as_bad_where (file, line, _("relocation out of range")); 1188 break; 1189 default: 1190 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"), 1191 file, line, s); 1192 } 1193 } 1194 1195 static fragS * 1196 get_frag_for_reloc (fragS *last_frag, 1197 const segment_info_type *seginfo, 1198 const struct reloc_list *r) 1199 { 1200 fragS *f; 1201 1202 for (f = last_frag; f != NULL; f = f->fr_next) 1203 if (f->fr_address <= r->u.b.r.address 1204 && r->u.b.r.address < f->fr_address + f->fr_fix) 1205 return f; 1206 1207 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next) 1208 if (f->fr_address <= r->u.b.r.address 1209 && r->u.b.r.address < f->fr_address + f->fr_fix) 1210 return f; 1211 1212 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next) 1213 if (f->fr_address <= r->u.b.r.address 1214 && r->u.b.r.address <= f->fr_address + f->fr_fix) 1215 return f; 1216 1217 as_bad_where (r->file, r->line, 1218 _("reloc not within (fixed part of) section")); 1219 return NULL; 1220 } 1221 1222 static void 1223 write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, 1224 void *xxx ATTRIBUTE_UNUSED) 1225 { 1226 segment_info_type *seginfo = seg_info (sec); 1227 unsigned int n; 1228 struct reloc_list *my_reloc_list, **rp, *r; 1229 arelent **relocs; 1230 fixS *fixp; 1231 fragS *last_frag; 1232 1233 /* If seginfo is NULL, we did not create this section; don't do 1234 anything with it. */ 1235 if (seginfo == NULL) 1236 return; 1237 1238 n = 0; 1239 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next) 1240 if (!fixp->fx_done) 1241 n++; 1242 1243 #ifdef RELOC_EXPANSION_POSSIBLE 1244 n *= MAX_RELOC_EXPANSION; 1245 #endif 1246 1247 /* Extract relocs for this section from reloc_list. */ 1248 rp = &reloc_list; 1249 1250 my_reloc_list = NULL; 1251 while ((r = *rp) != NULL) 1252 { 1253 if (r->u.b.sec == sec) 1254 { 1255 *rp = r->next; 1256 r->next = my_reloc_list; 1257 my_reloc_list = r; 1258 n++; 1259 } 1260 else 1261 rp = &r->next; 1262 } 1263 1264 relocs = XCNEWVEC (arelent *, n); 1265 1266 n = 0; 1267 r = my_reloc_list; 1268 last_frag = NULL; 1269 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next) 1270 { 1271 int fx_size, slack; 1272 valueT loc; 1273 arelent **reloc; 1274 #ifndef RELOC_EXPANSION_POSSIBLE 1275 arelent *rel; 1276 1277 reloc = &rel; 1278 #endif 1279 1280 if (fixp->fx_done) 1281 continue; 1282 1283 fx_size = fixp->fx_size; 1284 slack = TC_FX_SIZE_SLACK (fixp); 1285 if (slack > 0) 1286 fx_size = fx_size > slack ? fx_size - slack : 0; 1287 loc = fixp->fx_where + fx_size; 1288 if (slack >= 0 && loc > fixp->fx_frag->fr_fix) 1289 as_bad_where (fixp->fx_file, fixp->fx_line, 1290 _("internal error: fixup not contained within frag")); 1291 1292 #ifdef obj_fixup_removed_symbol 1293 if (fixp->fx_addsy && symbol_removed_p (fixp->fx_addsy)) 1294 obj_fixup_removed_symbol (&fixp->fx_addsy); 1295 if (fixp->fx_subsy && symbol_removed_p (fixp->fx_subsy)) 1296 obj_fixup_removed_symbol (&fixp->fx_subsy); 1297 #endif 1298 1299 #ifndef RELOC_EXPANSION_POSSIBLE 1300 *reloc = tc_gen_reloc (sec, fixp); 1301 #else 1302 reloc = tc_gen_reloc (sec, fixp); 1303 #endif 1304 1305 while (*reloc) 1306 { 1307 while (r != NULL && r->u.b.r.address < (*reloc)->address) 1308 { 1309 fragS *f = get_frag_for_reloc (last_frag, seginfo, r); 1310 if (f != NULL) 1311 { 1312 last_frag = f; 1313 relocs[n++] = &r->u.b.r; 1314 install_reloc (sec, &r->u.b.r, f, r->file, r->line); 1315 } 1316 r = r->next; 1317 } 1318 #ifdef GAS_SORT_RELOCS 1319 if (n != 0 && (*reloc)->address < relocs[n - 1]->address) 1320 { 1321 size_t lo = 0; 1322 size_t hi = n - 1; 1323 bfd_vma look = (*reloc)->address; 1324 while (lo < hi) 1325 { 1326 size_t mid = (lo + hi) / 2; 1327 if (relocs[mid]->address > look) 1328 hi = mid; 1329 else 1330 { 1331 lo = mid + 1; 1332 if (relocs[mid]->address == look) 1333 break; 1334 } 1335 } 1336 while (lo < hi && relocs[lo]->address == look) 1337 lo++; 1338 memmove (relocs + lo + 1, relocs + lo, 1339 (n - lo) * sizeof (*relocs)); 1340 n++; 1341 relocs[lo] = *reloc; 1342 } 1343 else 1344 #endif 1345 relocs[n++] = *reloc; 1346 install_reloc (sec, *reloc, fixp->fx_frag, 1347 fixp->fx_file, fixp->fx_line); 1348 #ifndef RELOC_EXPANSION_POSSIBLE 1349 break; 1350 #else 1351 reloc++; 1352 #endif 1353 } 1354 } 1355 1356 while (r != NULL) 1357 { 1358 fragS *f = get_frag_for_reloc (last_frag, seginfo, r); 1359 if (f != NULL) 1360 { 1361 last_frag = f; 1362 relocs[n++] = &r->u.b.r; 1363 install_reloc (sec, &r->u.b.r, f, r->file, r->line); 1364 } 1365 r = r->next; 1366 } 1367 1368 #ifdef DEBUG4 1369 { 1370 unsigned int k, j, nsyms; 1371 asymbol **sympp; 1372 sympp = bfd_get_outsymbols (stdoutput); 1373 nsyms = bfd_get_symcount (stdoutput); 1374 for (k = 0; k < n; k++) 1375 if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0) 1376 { 1377 for (j = 0; j < nsyms; j++) 1378 if (sympp[j] == *relocs[k]->sym_ptr_ptr) 1379 break; 1380 if (j == nsyms) 1381 abort (); 1382 } 1383 } 1384 #endif 1385 1386 if (n) 1387 { 1388 flagword flags = bfd_section_flags (sec); 1389 flags |= SEC_RELOC; 1390 bfd_set_section_flags (sec, flags); 1391 bfd_set_reloc (stdoutput, sec, relocs, n); 1392 } 1393 1394 #ifdef SET_SECTION_RELOCS 1395 SET_SECTION_RELOCS (sec, relocs, n); 1396 #endif 1397 1398 #ifdef DEBUG3 1399 { 1400 unsigned int k; 1401 1402 fprintf (stderr, "relocs for sec %s\n", sec->name); 1403 for (k = 0; k < n; k++) 1404 { 1405 arelent *rel = relocs[k]; 1406 asymbol *s = *rel->sym_ptr_ptr; 1407 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n", 1408 k, rel, (unsigned long)rel->address, s->name, 1409 (unsigned long)rel->addend); 1410 } 1411 } 1412 #endif 1413 } 1414 1415 static int 1416 compress_frag (struct z_stream_s *strm, const char *contents, int in_size, 1417 fragS **last_newf, struct obstack *ob) 1418 { 1419 int out_size; 1420 int total_out_size = 0; 1421 fragS *f = *last_newf; 1422 char *next_out; 1423 int avail_out; 1424 1425 /* Call the compression routine repeatedly until it has finished 1426 processing the frag. */ 1427 while (in_size > 0) 1428 { 1429 /* Reserve all the space available in the current chunk. 1430 If none is available, start a new frag. */ 1431 avail_out = obstack_room (ob); 1432 if (avail_out <= 0) 1433 { 1434 obstack_finish (ob); 1435 f = frag_alloc (ob); 1436 f->fr_type = rs_fill; 1437 (*last_newf)->fr_next = f; 1438 *last_newf = f; 1439 avail_out = obstack_room (ob); 1440 } 1441 if (avail_out <= 0) 1442 as_fatal (_("can't extend frag")); 1443 next_out = obstack_next_free (ob); 1444 obstack_blank_fast (ob, avail_out); 1445 out_size = compress_data (strm, &contents, &in_size, 1446 &next_out, &avail_out); 1447 if (out_size < 0) 1448 return -1; 1449 1450 f->fr_fix += out_size; 1451 total_out_size += out_size; 1452 1453 /* Return unused space. */ 1454 if (avail_out > 0) 1455 obstack_blank_fast (ob, -avail_out); 1456 } 1457 1458 return total_out_size; 1459 } 1460 1461 static void 1462 compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED) 1463 { 1464 segment_info_type *seginfo = seg_info (sec); 1465 fragS *f; 1466 fragS *first_newf; 1467 fragS *last_newf; 1468 struct obstack *ob = &seginfo->frchainP->frch_obstack; 1469 bfd_size_type uncompressed_size = (bfd_size_type) sec->size; 1470 bfd_size_type compressed_size; 1471 const char *section_name; 1472 char *compressed_name; 1473 char *header; 1474 struct z_stream_s *strm; 1475 int x; 1476 flagword flags = bfd_section_flags (sec); 1477 unsigned int header_size, compression_header_size; 1478 1479 if (seginfo == NULL 1480 || sec->size < 32 1481 || (flags & (SEC_ALLOC | SEC_HAS_CONTENTS)) == SEC_ALLOC) 1482 return; 1483 1484 section_name = bfd_section_name (sec); 1485 if (!startswith (section_name, ".debug_")) 1486 return; 1487 1488 strm = compress_init (); 1489 if (strm == NULL) 1490 return; 1491 1492 if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB) 1493 { 1494 compression_header_size 1495 = bfd_get_compression_header_size (stdoutput, NULL); 1496 header_size = compression_header_size; 1497 } 1498 else 1499 { 1500 compression_header_size = 0; 1501 header_size = 12; 1502 } 1503 1504 /* Create a new frag to contain the compression header. */ 1505 first_newf = frag_alloc (ob); 1506 if (obstack_room (ob) < header_size) 1507 first_newf = frag_alloc (ob); 1508 if (obstack_room (ob) < header_size) 1509 as_fatal (ngettext ("can't extend frag %lu char", 1510 "can't extend frag %lu chars", 1511 (unsigned long) header_size), 1512 (unsigned long) header_size); 1513 last_newf = first_newf; 1514 obstack_blank_fast (ob, header_size); 1515 last_newf->fr_type = rs_fill; 1516 last_newf->fr_fix = header_size; 1517 header = last_newf->fr_literal; 1518 compressed_size = header_size; 1519 1520 /* Stream the frags through the compression engine, adding new frags 1521 as necessary to accommodate the compressed output. */ 1522 for (f = seginfo->frchainP->frch_root; 1523 f; 1524 f = f->fr_next) 1525 { 1526 offsetT fill_size; 1527 char *fill_literal; 1528 offsetT count; 1529 int out_size; 1530 1531 gas_assert (f->fr_type == rs_fill); 1532 if (f->fr_fix) 1533 { 1534 out_size = compress_frag (strm, f->fr_literal, f->fr_fix, 1535 &last_newf, ob); 1536 if (out_size < 0) 1537 return; 1538 compressed_size += out_size; 1539 } 1540 fill_literal = f->fr_literal + f->fr_fix; 1541 fill_size = f->fr_var; 1542 count = f->fr_offset; 1543 gas_assert (count >= 0); 1544 if (fill_size && count) 1545 { 1546 while (count--) 1547 { 1548 out_size = compress_frag (strm, fill_literal, (int) fill_size, 1549 &last_newf, ob); 1550 if (out_size < 0) 1551 return; 1552 compressed_size += out_size; 1553 } 1554 } 1555 } 1556 1557 /* Flush the compression state. */ 1558 for (;;) 1559 { 1560 int avail_out; 1561 char *next_out; 1562 int out_size; 1563 1564 /* Reserve all the space available in the current chunk. 1565 If none is available, start a new frag. */ 1566 avail_out = obstack_room (ob); 1567 if (avail_out <= 0) 1568 { 1569 fragS *newf; 1570 1571 obstack_finish (ob); 1572 newf = frag_alloc (ob); 1573 newf->fr_type = rs_fill; 1574 last_newf->fr_next = newf; 1575 last_newf = newf; 1576 avail_out = obstack_room (ob); 1577 } 1578 if (avail_out <= 0) 1579 as_fatal (_("can't extend frag")); 1580 next_out = obstack_next_free (ob); 1581 obstack_blank_fast (ob, avail_out); 1582 x = compress_finish (strm, &next_out, &avail_out, &out_size); 1583 if (x < 0) 1584 return; 1585 1586 last_newf->fr_fix += out_size; 1587 compressed_size += out_size; 1588 1589 /* Return unused space. */ 1590 if (avail_out > 0) 1591 obstack_blank_fast (ob, -avail_out); 1592 1593 if (x == 0) 1594 break; 1595 } 1596 1597 /* PR binutils/18087: If compression didn't make the section smaller, 1598 just keep it uncompressed. */ 1599 if (compressed_size >= uncompressed_size) 1600 return; 1601 1602 /* Replace the uncompressed frag list with the compressed frag list. */ 1603 seginfo->frchainP->frch_root = first_newf; 1604 seginfo->frchainP->frch_last = last_newf; 1605 1606 /* Update the section size and its name. */ 1607 bfd_update_compression_header (abfd, (bfd_byte *) header, sec); 1608 x = bfd_set_section_size (sec, compressed_size); 1609 gas_assert (x); 1610 if (!compression_header_size) 1611 { 1612 compressed_name = concat (".z", section_name + 1, (char *) NULL); 1613 bfd_rename_section (sec, compressed_name); 1614 } 1615 } 1616 1617 #ifndef md_generate_nops 1618 /* Genenerate COUNT bytes of no-op instructions to WHERE. A target 1619 backend must override this with proper no-op instructions. */ 1620 1621 static void 1622 md_generate_nops (fragS *f ATTRIBUTE_UNUSED, 1623 char *where ATTRIBUTE_UNUSED, 1624 offsetT count ATTRIBUTE_UNUSED, 1625 int control ATTRIBUTE_UNUSED) 1626 { 1627 as_bad (_("unimplemented .nops directive")); 1628 } 1629 #endif 1630 1631 static void 1632 write_contents (bfd *abfd ATTRIBUTE_UNUSED, 1633 asection *sec, 1634 void *xxx ATTRIBUTE_UNUSED) 1635 { 1636 segment_info_type *seginfo = seg_info (sec); 1637 addressT offset = 0; 1638 fragS *f; 1639 1640 /* Write out the frags. */ 1641 if (seginfo == NULL 1642 || !(bfd_section_flags (sec) & SEC_HAS_CONTENTS)) 1643 return; 1644 1645 for (f = seginfo->frchainP->frch_root; 1646 f; 1647 f = f->fr_next) 1648 { 1649 int x; 1650 addressT fill_size; 1651 char *fill_literal; 1652 offsetT count; 1653 1654 gas_assert (f->fr_type == rs_fill || f->fr_type == rs_fill_nop); 1655 if (f->fr_fix) 1656 { 1657 x = bfd_set_section_contents (stdoutput, sec, 1658 f->fr_literal, (file_ptr) offset, 1659 (bfd_size_type) f->fr_fix); 1660 if (!x) 1661 as_fatal (ngettext ("can't write %ld byte " 1662 "to section %s of %s: '%s'", 1663 "can't write %ld bytes " 1664 "to section %s of %s: '%s'", 1665 (long) f->fr_fix), 1666 (long) f->fr_fix, 1667 bfd_section_name (sec), bfd_get_filename (stdoutput), 1668 bfd_errmsg (bfd_get_error ())); 1669 offset += f->fr_fix; 1670 } 1671 1672 fill_size = f->fr_var; 1673 count = f->fr_offset; 1674 fill_literal = f->fr_literal + f->fr_fix; 1675 1676 if (f->fr_type == rs_fill_nop) 1677 { 1678 gas_assert (count >= 0 && fill_size == 1); 1679 if (count > 0) 1680 { 1681 char *buf = xmalloc (count); 1682 md_generate_nops (f, buf, count, *fill_literal); 1683 x = bfd_set_section_contents 1684 (stdoutput, sec, buf, (file_ptr) offset, 1685 (bfd_size_type) count); 1686 if (!x) 1687 as_fatal (ngettext ("can't fill %ld byte " 1688 "in section %s of %s: '%s'", 1689 "can't fill %ld bytes " 1690 "in section %s of %s: '%s'", 1691 (long) count), 1692 (long) count, 1693 bfd_section_name (sec), 1694 bfd_get_filename (stdoutput), 1695 bfd_errmsg (bfd_get_error ())); 1696 offset += count; 1697 free (buf); 1698 } 1699 continue; 1700 } 1701 1702 gas_assert (count >= 0); 1703 if (fill_size && count) 1704 { 1705 char buf[256]; 1706 if (fill_size > sizeof (buf)) 1707 { 1708 /* Do it the old way. Can this ever happen? */ 1709 while (count--) 1710 { 1711 x = bfd_set_section_contents (stdoutput, sec, 1712 fill_literal, 1713 (file_ptr) offset, 1714 (bfd_size_type) fill_size); 1715 if (!x) 1716 as_fatal (ngettext ("can't fill %ld byte " 1717 "in section %s of %s: '%s'", 1718 "can't fill %ld bytes " 1719 "in section %s of %s: '%s'", 1720 (long) fill_size), 1721 (long) fill_size, 1722 bfd_section_name (sec), 1723 bfd_get_filename (stdoutput), 1724 bfd_errmsg (bfd_get_error ())); 1725 offset += fill_size; 1726 } 1727 } 1728 else 1729 { 1730 /* Build a buffer full of fill objects and output it as 1731 often as necessary. This saves on the overhead of 1732 potentially lots of bfd_set_section_contents calls. */ 1733 int n_per_buf, i; 1734 if (fill_size == 1) 1735 { 1736 n_per_buf = sizeof (buf); 1737 memset (buf, *fill_literal, n_per_buf); 1738 } 1739 else 1740 { 1741 char *bufp; 1742 n_per_buf = sizeof (buf) / fill_size; 1743 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size) 1744 memcpy (bufp, fill_literal, fill_size); 1745 } 1746 for (; count > 0; count -= n_per_buf) 1747 { 1748 n_per_buf = n_per_buf > count ? count : n_per_buf; 1749 x = bfd_set_section_contents 1750 (stdoutput, sec, buf, (file_ptr) offset, 1751 (bfd_size_type) n_per_buf * fill_size); 1752 if (!x) 1753 as_fatal (ngettext ("can't fill %ld byte " 1754 "in section %s of %s: '%s'", 1755 "can't fill %ld bytes " 1756 "in section %s of %s: '%s'", 1757 (long) (n_per_buf * fill_size)), 1758 (long) (n_per_buf * fill_size), 1759 bfd_section_name (sec), 1760 bfd_get_filename (stdoutput), 1761 bfd_errmsg (bfd_get_error ())); 1762 offset += n_per_buf * fill_size; 1763 } 1764 } 1765 } 1766 } 1767 } 1768 1769 static void 1770 merge_data_into_text (void) 1771 { 1772 seg_info (text_section)->frchainP->frch_last->fr_next = 1773 seg_info (data_section)->frchainP->frch_root; 1774 seg_info (text_section)->frchainP->frch_last = 1775 seg_info (data_section)->frchainP->frch_last; 1776 seg_info (data_section)->frchainP = 0; 1777 } 1778 1779 static void 1780 set_symtab (void) 1781 { 1782 int nsyms; 1783 asymbol **asympp; 1784 symbolS *symp; 1785 bool result; 1786 1787 /* Count symbols. We can't rely on a count made by the loop in 1788 write_object_file, because *_frob_file may add a new symbol or 1789 two. Generate unused section symbols only if needed. */ 1790 nsyms = 0; 1791 for (symp = symbol_rootP; symp; symp = symbol_next (symp)) 1792 if (!symbol_removed_p (symp) 1793 && (bfd_keep_unused_section_symbols (stdoutput) 1794 || !symbol_section_p (symp) 1795 || symbol_used_in_reloc_p (symp))) 1796 nsyms++; 1797 1798 if (nsyms) 1799 { 1800 int i; 1801 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *); 1802 1803 asympp = (asymbol **) bfd_alloc (stdoutput, amt); 1804 symp = symbol_rootP; 1805 for (i = 0; i < nsyms; symp = symbol_next (symp)) 1806 if (!symbol_removed_p (symp) 1807 && (bfd_keep_unused_section_symbols (stdoutput) 1808 || !symbol_section_p (symp) 1809 || symbol_used_in_reloc_p (symp))) 1810 { 1811 asympp[i] = symbol_get_bfdsym (symp); 1812 if (asympp[i]->flags != BSF_SECTION_SYM 1813 || !(bfd_is_const_section (asympp[i]->section) 1814 && asympp[i]->section->symbol == asympp[i])) 1815 asympp[i]->flags |= BSF_KEEP; 1816 symbol_mark_written (symp); 1817 /* Include this section symbol in the symbol table. */ 1818 if (symbol_section_p (symp)) 1819 asympp[i]->flags |= BSF_SECTION_SYM_USED; 1820 i++; 1821 } 1822 } 1823 else 1824 asympp = 0; 1825 result = bfd_set_symtab (stdoutput, asympp, nsyms); 1826 gas_assert (result); 1827 symbol_table_frozen = 1; 1828 } 1829 1830 /* Finish the subsegments. After every sub-segment, we fake an 1831 ".align ...". This conforms to BSD4.2 brain-damage. We then fake 1832 ".fill 0" because that is the kind of frag that requires least 1833 thought. ".align" frags like to have a following frag since that 1834 makes calculating their intended length trivial. */ 1835 1836 #ifndef SUB_SEGMENT_ALIGN 1837 #ifdef HANDLE_ALIGN 1838 /* The last subsegment gets an alignment corresponding to the alignment 1839 of the section. This allows proper nop-filling at the end of 1840 code-bearing sections. */ 1841 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \ 1842 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \ 1843 && !do_not_pad_sections_to_alignment \ 1844 ? get_recorded_alignment (SEG) \ 1845 : 0) 1846 #else 1847 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0 1848 #endif 1849 #endif 1850 1851 static void 1852 subsegs_finish_section (asection *s) 1853 { 1854 struct frchain *frchainP; 1855 segment_info_type *seginfo = seg_info (s); 1856 if (!seginfo) 1857 return; 1858 1859 for (frchainP = seginfo->frchainP; 1860 frchainP != NULL; 1861 frchainP = frchainP->frch_next) 1862 { 1863 int alignment; 1864 1865 subseg_set (s, frchainP->frch_subseg); 1866 1867 /* This now gets called even if we had errors. In that case, 1868 any alignment is meaningless, and, moreover, will look weird 1869 if we are generating a listing. */ 1870 if (had_errors ()) 1871 do_not_pad_sections_to_alignment = 1; 1872 1873 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP); 1874 if ((bfd_section_flags (now_seg) & SEC_MERGE) 1875 && now_seg->entsize) 1876 { 1877 unsigned int entsize = now_seg->entsize; 1878 int entalign = 0; 1879 1880 while ((entsize & 1) == 0) 1881 { 1882 ++entalign; 1883 entsize >>= 1; 1884 } 1885 1886 if (entalign > alignment) 1887 alignment = entalign; 1888 } 1889 1890 if (subseg_text_p (now_seg)) 1891 frag_align_code (alignment, 0); 1892 else 1893 frag_align (alignment, 0, 0); 1894 1895 /* frag_align will have left a new frag. 1896 Use this last frag for an empty ".fill". 1897 1898 For this segment ... 1899 Create a last frag. Do not leave a "being filled in frag". */ 1900 frag_wane (frag_now); 1901 frag_now->fr_fix = 0; 1902 know (frag_now->fr_next == NULL); 1903 } 1904 } 1905 1906 static void 1907 subsegs_finish (void) 1908 { 1909 asection *s; 1910 1911 for (s = stdoutput->sections; s; s = s->next) 1912 subsegs_finish_section (s); 1913 } 1914 1915 #ifdef OBJ_ELF 1916 static void 1917 create_obj_attrs_section (void) 1918 { 1919 segT s; 1920 char *p; 1921 offsetT size; 1922 const char *name; 1923 1924 size = bfd_elf_obj_attr_size (stdoutput); 1925 if (size == 0) 1926 return; 1927 1928 name = get_elf_backend_data (stdoutput)->obj_attrs_section; 1929 if (!name) 1930 name = ".gnu.attributes"; 1931 s = subseg_new (name, 0); 1932 elf_section_type (s) 1933 = get_elf_backend_data (stdoutput)->obj_attrs_section_type; 1934 bfd_set_section_flags (s, SEC_READONLY | SEC_DATA); 1935 frag_now_fix (); 1936 p = frag_more (size); 1937 bfd_elf_set_obj_attr_contents (stdoutput, (bfd_byte *)p, size); 1938 1939 subsegs_finish_section (s); 1940 relax_segment (seg_info (s)->frchainP->frch_root, s, 0); 1941 size_seg (stdoutput, s, NULL); 1942 } 1943 1944 /* Create a relocation against an entry in a GNU Build attribute section. */ 1945 1946 static void 1947 create_note_reloc (segT sec, 1948 symbolS * sym, 1949 bfd_size_type note_offset, 1950 bfd_size_type desc2_offset, 1951 offsetT desc2_size, 1952 int reloc_type, 1953 bfd_vma addend, 1954 char * note) 1955 { 1956 struct reloc_list * reloc; 1957 1958 reloc = XNEW (struct reloc_list); 1959 1960 /* We create a .b type reloc as resolve_reloc_expr_symbols() has already been called. */ 1961 reloc->u.b.sec = sec; 1962 reloc->u.b.s = symbol_get_bfdsym (sym); 1963 reloc->u.b.r.sym_ptr_ptr = & reloc->u.b.s; 1964 reloc->u.b.r.address = note_offset + desc2_offset; 1965 reloc->u.b.r.addend = addend; 1966 reloc->u.b.r.howto = bfd_reloc_type_lookup (stdoutput, reloc_type); 1967 1968 if (reloc->u.b.r.howto == NULL) 1969 { 1970 as_bad (_("unable to create reloc for build note")); 1971 return; 1972 } 1973 1974 reloc->file = N_("<gnu build note>"); 1975 reloc->line = 0; 1976 1977 reloc->next = reloc_list; 1978 reloc_list = reloc; 1979 1980 /* For REL relocs, store the addend in the section. */ 1981 if (! sec->use_rela_p 1982 /* The SH target is a special case that uses RELA relocs 1983 but still stores the addend in the word being relocated. */ 1984 || strstr (bfd_get_target (stdoutput), "-sh") != NULL) 1985 { 1986 offsetT i; 1987 1988 /* Zero out the addend, since it is now stored in the note. */ 1989 reloc->u.b.r.addend = 0; 1990 1991 if (target_big_endian) 1992 { 1993 for (i = desc2_size; addend != 0 && i > 0; addend >>= 8, i--) 1994 note[desc2_offset + i - 1] = (addend & 0xff); 1995 } 1996 else 1997 { 1998 for (i = 0; addend != 0 && i < desc2_size; addend >>= 8, i++) 1999 note[desc2_offset + i] = (addend & 0xff); 2000 } 2001 } 2002 } 2003 2004 static void 2005 maybe_generate_build_notes (void) 2006 { 2007 segT sec; 2008 char * note; 2009 offsetT note_size; 2010 offsetT total_size; 2011 offsetT desc_size; 2012 offsetT desc2_offset; 2013 int desc_reloc; 2014 symbolS * sym; 2015 asymbol * bsym; 2016 2017 if (! flag_generate_build_notes 2018 || bfd_get_section_by_name (stdoutput, 2019 GNU_BUILD_ATTRS_SECTION_NAME) != NULL) 2020 return; 2021 2022 /* Create a GNU Build Attribute section. */ 2023 sec = subseg_new (GNU_BUILD_ATTRS_SECTION_NAME, false); 2024 elf_section_type (sec) = SHT_NOTE; 2025 bfd_set_section_flags (sec, (SEC_READONLY | SEC_HAS_CONTENTS | SEC_DATA 2026 | SEC_OCTETS)); 2027 bfd_set_section_alignment (sec, 2); 2028 2029 /* Work out the size of the notes that we will create, 2030 and the relocation we should use. */ 2031 if (bfd_arch_bits_per_address (stdoutput) <= 32) 2032 { 2033 note_size = 28; 2034 desc_size = 8; /* Two 4-byte offsets. */ 2035 desc2_offset = 24; 2036 2037 /* FIXME: The BFD backend for the CRX target does not support the 2038 BFD_RELOC_32, even though it really should. Likewise for the 2039 CR16 target. So we have special case code here... */ 2040 if (strstr (bfd_get_target (stdoutput), "-crx") != NULL) 2041 desc_reloc = BFD_RELOC_CRX_NUM32; 2042 else if (strstr (bfd_get_target (stdoutput), "-cr16") != NULL) 2043 desc_reloc = BFD_RELOC_CR16_NUM32; 2044 else 2045 desc_reloc = BFD_RELOC_32; 2046 } 2047 else 2048 { 2049 note_size = 36; 2050 desc_size = 16; /* Two 8-byte offsets. */ 2051 desc2_offset = 28; 2052 /* FIXME: The BFD backend for the IA64 target does not support the 2053 BFD_RELOC_64, even though it really should. The HPPA backend 2054 has a similar issue, although it does not support BFD_RELOCs at 2055 all! So we have special case code to handle these targets. */ 2056 if (strstr (bfd_get_target (stdoutput), "-ia64") != NULL) 2057 desc_reloc = target_big_endian ? BFD_RELOC_IA64_DIR32MSB : BFD_RELOC_IA64_DIR32LSB; 2058 else if (strstr (bfd_get_target (stdoutput), "-hppa") != NULL) 2059 desc_reloc = 80; /* R_PARISC_DIR64. */ 2060 else 2061 desc_reloc = BFD_RELOC_64; 2062 } 2063 2064 /* We have to create a note for *each* code section. 2065 Linker garbage collection might discard some. */ 2066 total_size = 0; 2067 note = NULL; 2068 2069 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym)) 2070 if ((bsym = symbol_get_bfdsym (sym)) != NULL 2071 && bsym->flags & BSF_SECTION_SYM 2072 && bsym->section != NULL 2073 /* Skip linkonce sections - we cannot use these section symbols as they may disappear. */ 2074 && (bsym->section->flags & (SEC_CODE | SEC_LINK_ONCE)) == SEC_CODE 2075 /* Not all linkonce sections are flagged... */ 2076 && !startswith (S_GET_NAME (sym), ".gnu.linkonce")) 2077 { 2078 /* Create a version note. */ 2079 frag_now_fix (); 2080 note = frag_more (note_size); 2081 memset (note, 0, note_size); 2082 2083 if (target_big_endian) 2084 { 2085 note[3] = 8; /* strlen (name) + 1. */ 2086 note[7] = desc_size; /* Two N-byte offsets. */ 2087 note[10] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8; 2088 note[11] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff; 2089 } 2090 else 2091 { 2092 note[0] = 8; /* strlen (name) + 1. */ 2093 note[4] = desc_size; /* Two N-byte offsets. */ 2094 note[8] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff; 2095 note[9] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8; 2096 } 2097 2098 /* The a1 version number indicates that this note was 2099 generated by the assembler and not the gcc annobin plugin. */ 2100 memcpy (note + 12, "GA$3a1", 8); 2101 2102 /* Create a relocation to install the start address of the note... */ 2103 create_note_reloc (sec, sym, total_size, 20, desc_size / 2, desc_reloc, 0, note); 2104 2105 /* ...and another one to install the end address. */ 2106 create_note_reloc (sec, sym, total_size, desc2_offset, 2107 desc_size / 2, 2108 desc_reloc, 2109 bfd_section_size (bsym->section), 2110 note); 2111 2112 /* Mark the section symbol used in relocation so that it will be 2113 included in the symbol table. */ 2114 symbol_mark_used_in_reloc (sym); 2115 2116 total_size += note_size; 2117 /* FIXME: Maybe add a note recording the assembler command line and version ? */ 2118 } 2119 2120 /* Install the note(s) into the section. */ 2121 if (total_size) 2122 bfd_set_section_contents (stdoutput, sec, (bfd_byte *) note, 0, total_size); 2123 subsegs_finish_section (sec); 2124 relax_segment (seg_info (sec)->frchainP->frch_root, sec, 0); 2125 size_seg (stdoutput, sec, NULL); 2126 } 2127 #endif /* OBJ_ELF */ 2128 2129 /* Write the object file. */ 2130 2131 void 2132 write_object_file (void) 2133 { 2134 struct relax_seg_info rsi; 2135 #ifndef WORKING_DOT_WORD 2136 fragS *fragP; /* Track along all frags. */ 2137 #endif 2138 2139 subsegs_finish (); 2140 2141 #ifdef md_pre_output_hook 2142 md_pre_output_hook; 2143 #endif 2144 2145 #ifdef md_pre_relax_hook 2146 md_pre_relax_hook; 2147 #endif 2148 2149 /* From now on, we don't care about sub-segments. Build one frag chain 2150 for each segment. Linked through fr_next. */ 2151 2152 /* Remove the sections created by gas for its own purposes. */ 2153 { 2154 int i; 2155 2156 bfd_section_list_remove (stdoutput, reg_section); 2157 bfd_section_list_remove (stdoutput, expr_section); 2158 stdoutput->section_count -= 2; 2159 i = 0; 2160 bfd_map_over_sections (stdoutput, renumber_sections, &i); 2161 } 2162 2163 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0); 2164 2165 /* We have two segments. If user gave -R flag, then we must put the 2166 data frags into the text segment. Do this before relaxing so 2167 we know to take advantage of -R and make shorter addresses. */ 2168 if (flag_readonly_data_in_text) 2169 { 2170 merge_data_into_text (); 2171 } 2172 2173 rsi.pass = 0; 2174 while (1) 2175 { 2176 #ifndef WORKING_DOT_WORD 2177 /* We need to reset the markers in the broken word list and 2178 associated frags between calls to relax_segment (via 2179 relax_seg). Since the broken word list is global, we do it 2180 once per round, rather than locally in relax_segment for each 2181 segment. */ 2182 struct broken_word *brokp; 2183 2184 for (brokp = broken_words; 2185 brokp != (struct broken_word *) NULL; 2186 brokp = brokp->next_broken_word) 2187 { 2188 brokp->added = 0; 2189 2190 if (brokp->dispfrag != (fragS *) NULL 2191 && brokp->dispfrag->fr_type == rs_broken_word) 2192 brokp->dispfrag->fr_subtype = 0; 2193 } 2194 #endif 2195 2196 rsi.changed = 0; 2197 bfd_map_over_sections (stdoutput, relax_seg, &rsi); 2198 rsi.pass++; 2199 if (!rsi.changed) 2200 break; 2201 } 2202 2203 /* Note - Most ports will use the default value of 2204 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force 2205 local symbols to be resolved, removing their frag information. 2206 Some ports however, will not have finished relaxing all of 2207 their frags and will still need the local symbol frag 2208 information. These ports can set 2209 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */ 2210 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG; 2211 2212 bfd_map_over_sections (stdoutput, size_seg, (char *) 0); 2213 2214 /* Relaxation has completed. Freeze all syms. */ 2215 finalize_syms = 1; 2216 2217 dwarf2dbg_final_check (); 2218 2219 #ifdef md_post_relax_hook 2220 md_post_relax_hook; 2221 #endif 2222 2223 #ifdef OBJ_ELF 2224 if (IS_ELF) 2225 create_obj_attrs_section (); 2226 #endif 2227 2228 #ifndef WORKING_DOT_WORD 2229 { 2230 struct broken_word *lie; 2231 struct broken_word **prevP; 2232 2233 prevP = &broken_words; 2234 for (lie = broken_words; lie; lie = lie->next_broken_word) 2235 if (!lie->added) 2236 { 2237 expressionS exp; 2238 2239 subseg_change (lie->seg, lie->subseg); 2240 exp.X_op = O_subtract; 2241 exp.X_add_symbol = lie->add; 2242 exp.X_op_symbol = lie->sub; 2243 exp.X_add_number = lie->addnum; 2244 #ifdef TC_CONS_FIX_NEW 2245 TC_CONS_FIX_NEW (lie->frag, 2246 lie->word_goes_here - lie->frag->fr_literal, 2247 2, &exp, TC_PARSE_CONS_RETURN_NONE); 2248 #else 2249 fix_new_exp (lie->frag, 2250 lie->word_goes_here - lie->frag->fr_literal, 2251 2, &exp, 0, BFD_RELOC_16); 2252 #endif 2253 *prevP = lie->next_broken_word; 2254 } 2255 else 2256 prevP = &(lie->next_broken_word); 2257 2258 for (lie = broken_words; lie;) 2259 { 2260 struct broken_word *untruth; 2261 char *table_ptr; 2262 addressT table_addr; 2263 addressT from_addr, to_addr; 2264 int n, m; 2265 2266 subseg_change (lie->seg, lie->subseg); 2267 fragP = lie->dispfrag; 2268 2269 /* Find out how many broken_words go here. */ 2270 n = 0; 2271 for (untruth = lie; 2272 untruth && untruth->dispfrag == fragP; 2273 untruth = untruth->next_broken_word) 2274 if (untruth->added == 1) 2275 n++; 2276 2277 table_ptr = lie->dispfrag->fr_opcode; 2278 table_addr = (lie->dispfrag->fr_address 2279 + (table_ptr - lie->dispfrag->fr_literal)); 2280 /* Create the jump around the long jumps. This is a short 2281 jump from table_ptr+0 to table_ptr+n*long_jump_size. */ 2282 from_addr = table_addr; 2283 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size; 2284 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, 2285 lie->add); 2286 table_ptr += md_short_jump_size; 2287 table_addr += md_short_jump_size; 2288 2289 for (m = 0; 2290 lie && lie->dispfrag == fragP; 2291 m++, lie = lie->next_broken_word) 2292 { 2293 if (lie->added == 2) 2294 continue; 2295 /* Patch the jump table. */ 2296 for (untruth = (struct broken_word *) (fragP->fr_symbol); 2297 untruth && untruth->dispfrag == fragP; 2298 untruth = untruth->next_broken_word) 2299 { 2300 if (untruth->use_jump == lie) 2301 { 2302 /* This is the offset from ??? to table_ptr+0. 2303 The target is the same for all users of this 2304 md_long_jump, but the "sub" bases (and hence the 2305 offsets) may be different. */ 2306 addressT to_word = table_addr - S_GET_VALUE (untruth->sub); 2307 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD 2308 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth); 2309 #endif 2310 md_number_to_chars (untruth->word_goes_here, to_word, 2); 2311 } 2312 } 2313 2314 /* Install the long jump. */ 2315 /* This is a long jump from table_ptr+0 to the final target. */ 2316 from_addr = table_addr; 2317 to_addr = S_GET_VALUE (lie->add) + lie->addnum; 2318 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, 2319 lie->add); 2320 table_ptr += md_long_jump_size; 2321 table_addr += md_long_jump_size; 2322 } 2323 } 2324 } 2325 #endif /* not WORKING_DOT_WORD */ 2326 2327 /* Resolve symbol values. This needs to be done before processing 2328 the relocations. */ 2329 if (symbol_rootP) 2330 { 2331 symbolS *symp; 2332 2333 for (symp = symbol_rootP; symp; symp = symbol_next (symp)) 2334 resolve_symbol_value (symp); 2335 } 2336 resolve_local_symbol_values (); 2337 resolve_reloc_expr_symbols (); 2338 2339 #ifdef OBJ_ELF 2340 if (IS_ELF) 2341 maybe_generate_build_notes (); 2342 #endif 2343 2344 PROGRESS (1); 2345 2346 #ifdef tc_frob_file_before_adjust 2347 tc_frob_file_before_adjust (); 2348 #endif 2349 #ifdef obj_frob_file_before_adjust 2350 obj_frob_file_before_adjust (); 2351 #endif 2352 2353 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0); 2354 2355 #ifdef tc_frob_file_before_fix 2356 tc_frob_file_before_fix (); 2357 #endif 2358 #ifdef obj_frob_file_before_fix 2359 obj_frob_file_before_fix (); 2360 #endif 2361 2362 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0); 2363 2364 /* Set up symbol table, and write it out. */ 2365 if (symbol_rootP) 2366 { 2367 symbolS *symp; 2368 bool skip_next_symbol = false; 2369 2370 for (symp = symbol_rootP; symp; symp = symbol_next (symp)) 2371 { 2372 int punt = 0; 2373 const char *name; 2374 2375 if (skip_next_symbol) 2376 { 2377 /* Don't do anything besides moving the value of the 2378 symbol from the GAS value-field to the BFD value-field. */ 2379 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp); 2380 skip_next_symbol = false; 2381 continue; 2382 } 2383 2384 if (symbol_mri_common_p (symp)) 2385 { 2386 if (S_IS_EXTERNAL (symp)) 2387 as_bad (_("%s: global symbols not supported in common sections"), 2388 S_GET_NAME (symp)); 2389 symbol_remove (symp, &symbol_rootP, &symbol_lastP); 2390 continue; 2391 } 2392 2393 name = S_GET_NAME (symp); 2394 if (name) 2395 { 2396 const char *name2 = 2397 decode_local_label_name ((char *) S_GET_NAME (symp)); 2398 /* They only differ if `name' is a fb or dollar local 2399 label name. */ 2400 if (name2 != name && ! S_IS_DEFINED (symp)) 2401 as_bad (_("local label `%s' is not defined"), name2); 2402 } 2403 2404 /* Do it again, because adjust_reloc_syms might introduce 2405 more symbols. They'll probably only be section symbols, 2406 but they'll still need to have the values computed. */ 2407 resolve_symbol_value (symp); 2408 2409 /* Skip symbols which were equated to undefined or common 2410 symbols. */ 2411 if (symbol_equated_reloc_p (symp) 2412 || S_IS_WEAKREFR (symp)) 2413 { 2414 const char *sname = S_GET_NAME (symp); 2415 2416 if (S_IS_COMMON (symp) 2417 && !TC_FAKE_LABEL (sname) 2418 && !S_IS_WEAKREFR (symp)) 2419 { 2420 expressionS *e = symbol_get_value_expression (symp); 2421 2422 as_bad (_("`%s' can't be equated to common symbol `%s'"), 2423 sname, S_GET_NAME (e->X_add_symbol)); 2424 } 2425 if (S_GET_SEGMENT (symp) == reg_section) 2426 { 2427 /* Report error only if we know the symbol name. */ 2428 if (S_GET_NAME (symp) != reg_section->name) 2429 as_bad (_("can't make global register symbol `%s'"), 2430 sname); 2431 } 2432 symbol_remove (symp, &symbol_rootP, &symbol_lastP); 2433 continue; 2434 } 2435 2436 #ifdef obj_frob_symbol 2437 obj_frob_symbol (symp, punt); 2438 #endif 2439 #ifdef tc_frob_symbol 2440 if (! punt || symbol_used_in_reloc_p (symp)) 2441 tc_frob_symbol (symp, punt); 2442 #endif 2443 2444 /* If we don't want to keep this symbol, splice it out of 2445 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never 2446 want section symbols. Otherwise, we skip local symbols 2447 and symbols that the frob_symbol macros told us to punt, 2448 but we keep such symbols if they are used in relocs. */ 2449 if (symp == abs_section_sym 2450 || (! EMIT_SECTION_SYMBOLS 2451 && symbol_section_p (symp)) 2452 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always 2453 opposites. Sometimes the former checks flags and the 2454 latter examines the name... */ 2455 || (!S_IS_EXTERNAL (symp) 2456 && (punt || S_IS_LOCAL (symp) || 2457 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp))) 2458 && ! symbol_used_in_reloc_p (symp))) 2459 { 2460 symbol_remove (symp, &symbol_rootP, &symbol_lastP); 2461 2462 /* After symbol_remove, symbol_next(symp) still returns 2463 the one that came after it in the chain. So we don't 2464 need to do any extra cleanup work here. */ 2465 continue; 2466 } 2467 2468 /* Make sure we really got a value for the symbol. */ 2469 if (! symbol_resolved_p (symp)) 2470 { 2471 as_bad (_("can't resolve value for symbol `%s'"), 2472 S_GET_NAME (symp)); 2473 symbol_mark_resolved (symp); 2474 } 2475 2476 /* Set the value into the BFD symbol. Up til now the value 2477 has only been kept in the gas symbolS struct. */ 2478 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp); 2479 2480 /* A warning construct is a warning symbol followed by the 2481 symbol warned about. Don't let anything object-format or 2482 target-specific muck with it; it's ready for output. */ 2483 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING) 2484 skip_next_symbol = true; 2485 } 2486 } 2487 2488 PROGRESS (1); 2489 2490 /* Now do any format-specific adjustments to the symbol table, such 2491 as adding file symbols. */ 2492 #ifdef tc_adjust_symtab 2493 tc_adjust_symtab (); 2494 #endif 2495 #ifdef obj_adjust_symtab 2496 obj_adjust_symtab (); 2497 #endif 2498 2499 /* Stop if there is an error. */ 2500 if (!flag_always_generate_output && had_errors ()) 2501 return; 2502 2503 /* Now that all the sizes are known, and contents correct, we can 2504 start writing to the file. */ 2505 set_symtab (); 2506 2507 /* If *_frob_file changes the symbol value at this point, it is 2508 responsible for moving the changed value into symp->bsym->value 2509 as well. Hopefully all symbol value changing can be done in 2510 *_frob_symbol. */ 2511 #ifdef tc_frob_file 2512 tc_frob_file (); 2513 #endif 2514 #ifdef obj_frob_file 2515 obj_frob_file (); 2516 #endif 2517 #ifdef obj_coff_generate_pdata 2518 obj_coff_generate_pdata (); 2519 #endif 2520 2521 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0); 2522 2523 #ifdef tc_frob_file_after_relocs 2524 tc_frob_file_after_relocs (); 2525 #endif 2526 #ifdef obj_frob_file_after_relocs 2527 obj_frob_file_after_relocs (); 2528 #endif 2529 2530 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF 2531 if (IS_ELF && flag_use_elf_stt_common) 2532 stdoutput->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON; 2533 #endif 2534 2535 /* Once all relocations have been written, we can compress the 2536 contents of the debug sections. This needs to be done before 2537 we start writing any sections, because it will affect the file 2538 layout, which is fixed once we start writing contents. */ 2539 if (flag_compress_debug) 2540 { 2541 if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB) 2542 stdoutput->flags |= BFD_COMPRESS | BFD_COMPRESS_GABI; 2543 else 2544 stdoutput->flags |= BFD_COMPRESS; 2545 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0); 2546 } 2547 2548 bfd_map_over_sections (stdoutput, write_contents, (char *) 0); 2549 } 2550 2551 #ifdef TC_GENERIC_RELAX_TABLE 2552 #ifndef md_generic_table_relax_frag 2553 #define md_generic_table_relax_frag relax_frag 2554 #endif 2555 2556 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */ 2557 2558 long 2559 relax_frag (segT segment, fragS *fragP, long stretch) 2560 { 2561 const relax_typeS *this_type; 2562 const relax_typeS *start_type; 2563 relax_substateT next_state; 2564 relax_substateT this_state; 2565 offsetT growth; 2566 offsetT aim; 2567 addressT target; 2568 addressT address; 2569 symbolS *symbolP; 2570 const relax_typeS *table; 2571 2572 target = fragP->fr_offset; 2573 address = fragP->fr_address + fragP->fr_fix; 2574 table = TC_GENERIC_RELAX_TABLE; 2575 this_state = fragP->fr_subtype; 2576 start_type = this_type = table + this_state; 2577 symbolP = fragP->fr_symbol; 2578 2579 if (symbolP) 2580 { 2581 fragS *sym_frag; 2582 2583 sym_frag = symbol_get_frag (symbolP); 2584 2585 #ifndef DIFF_EXPR_OK 2586 know (sym_frag != NULL); 2587 #endif 2588 know (S_GET_SEGMENT (symbolP) != absolute_section 2589 || sym_frag == &zero_address_frag); 2590 target += S_GET_VALUE (symbolP); 2591 2592 /* If SYM_FRAG has yet to be reached on this pass, assume it 2593 will move by STRETCH just as we did, unless there is an 2594 alignment frag between here and SYM_FRAG. An alignment may 2595 well absorb any STRETCH, and we don't want to choose a larger 2596 branch insn by overestimating the needed reach of this 2597 branch. It isn't critical to calculate TARGET exactly; We 2598 know we'll be doing another pass if STRETCH is non-zero. */ 2599 2600 if (stretch != 0 2601 && sym_frag->relax_marker != fragP->relax_marker 2602 && S_GET_SEGMENT (symbolP) == segment) 2603 { 2604 if (stretch < 0 2605 || sym_frag->region == fragP->region) 2606 target += stretch; 2607 /* If we get here we know we have a forward branch. This 2608 relax pass may have stretched previous instructions so 2609 far that omitting STRETCH would make the branch 2610 negative. Don't allow this in case the negative reach is 2611 large enough to require a larger branch instruction. */ 2612 else if (target < address) 2613 return 0; 2614 } 2615 } 2616 2617 aim = target - address; 2618 #ifdef TC_PCREL_ADJUST 2619 /* Currently only the ns32k and arc needs this. */ 2620 aim += TC_PCREL_ADJUST (fragP); 2621 #endif 2622 2623 #ifdef md_prepare_relax_scan 2624 /* Formerly called M68K_AIM_KLUDGE. */ 2625 md_prepare_relax_scan (fragP, address, aim, this_state, this_type); 2626 #endif 2627 2628 if (aim < 0) 2629 { 2630 /* Look backwards. */ 2631 for (next_state = this_type->rlx_more; next_state;) 2632 if (aim >= this_type->rlx_backward) 2633 next_state = 0; 2634 else 2635 { 2636 /* Grow to next state. */ 2637 this_state = next_state; 2638 this_type = table + this_state; 2639 next_state = this_type->rlx_more; 2640 } 2641 } 2642 else 2643 { 2644 /* Look forwards. */ 2645 for (next_state = this_type->rlx_more; next_state;) 2646 if (aim <= this_type->rlx_forward) 2647 next_state = 0; 2648 else 2649 { 2650 /* Grow to next state. */ 2651 this_state = next_state; 2652 this_type = table + this_state; 2653 next_state = this_type->rlx_more; 2654 } 2655 } 2656 2657 growth = this_type->rlx_length - start_type->rlx_length; 2658 if (growth != 0) 2659 fragP->fr_subtype = this_state; 2660 return growth; 2661 } 2662 2663 #endif /* defined (TC_GENERIC_RELAX_TABLE) */ 2664 2665 /* Relax_align. Advance location counter to next address that has 'alignment' 2666 lowest order bits all 0s, return size of adjustment made. */ 2667 static relax_addressT 2668 relax_align (relax_addressT address, /* Address now. */ 2669 int alignment /* Alignment (binary). */) 2670 { 2671 relax_addressT mask; 2672 relax_addressT new_address; 2673 2674 mask = ~((relax_addressT) ~0 << alignment); 2675 new_address = (address + mask) & (~mask); 2676 #ifdef LINKER_RELAXING_SHRINKS_ONLY 2677 if (linkrelax) 2678 /* We must provide lots of padding, so the linker can discard it 2679 when needed. The linker will not add extra space, ever. */ 2680 new_address += (1 << alignment); 2681 #endif 2682 return (new_address - address); 2683 } 2684 2685 /* Now we have a segment, not a crowd of sub-segments, we can make 2686 fr_address values. 2687 2688 Relax the frags. 2689 2690 After this, all frags in this segment have addresses that are correct 2691 within the segment. Since segments live in different file addresses, 2692 these frag addresses may not be the same as final object-file 2693 addresses. */ 2694 2695 int 2696 relax_segment (struct frag *segment_frag_root, segT segment, int pass) 2697 { 2698 unsigned long frag_count; 2699 struct frag *fragP; 2700 relax_addressT address; 2701 int region; 2702 int ret; 2703 2704 /* In case md_estimate_size_before_relax() wants to make fixSs. */ 2705 subseg_change (segment, 0); 2706 2707 /* For each frag in segment: count and store (a 1st guess of) 2708 fr_address. */ 2709 address = 0; 2710 region = 0; 2711 for (frag_count = 0, fragP = segment_frag_root; 2712 fragP; 2713 fragP = fragP->fr_next, frag_count ++) 2714 { 2715 fragP->region = region; 2716 fragP->relax_marker = 0; 2717 fragP->fr_address = address; 2718 address += fragP->fr_fix; 2719 2720 switch (fragP->fr_type) 2721 { 2722 case rs_fill: 2723 address += fragP->fr_offset * fragP->fr_var; 2724 break; 2725 2726 case rs_align: 2727 case rs_align_code: 2728 case rs_align_test: 2729 { 2730 addressT offset = relax_align (address, (int) fragP->fr_offset); 2731 2732 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype) 2733 offset = 0; 2734 2735 if (offset % fragP->fr_var != 0) 2736 { 2737 as_bad_where (fragP->fr_file, fragP->fr_line, 2738 ngettext ("alignment padding (%lu byte) " 2739 "not a multiple of %ld", 2740 "alignment padding (%lu bytes) " 2741 "not a multiple of %ld", 2742 (unsigned long) offset), 2743 (unsigned long) offset, (long) fragP->fr_var); 2744 offset -= (offset % fragP->fr_var); 2745 } 2746 2747 address += offset; 2748 region += 1; 2749 } 2750 break; 2751 2752 case rs_org: 2753 /* Assume .org is nugatory. It will grow with 1st relax. */ 2754 region += 1; 2755 break; 2756 2757 case rs_space: 2758 case rs_space_nop: 2759 break; 2760 2761 case rs_machine_dependent: 2762 /* If fr_symbol is an expression, this call to 2763 resolve_symbol_value sets up the correct segment, which will 2764 likely be needed in md_estimate_size_before_relax. */ 2765 if (fragP->fr_symbol) 2766 resolve_symbol_value (fragP->fr_symbol); 2767 2768 address += md_estimate_size_before_relax (fragP, segment); 2769 break; 2770 2771 #ifndef WORKING_DOT_WORD 2772 /* Broken words don't concern us yet. */ 2773 case rs_broken_word: 2774 break; 2775 #endif 2776 2777 case rs_leb128: 2778 /* Initial guess is always 1; doing otherwise can result in 2779 stable solutions that are larger than the minimum. */ 2780 address += fragP->fr_offset = 1; 2781 break; 2782 2783 case rs_cfa: 2784 address += eh_frame_estimate_size_before_relax (fragP); 2785 break; 2786 2787 case rs_dwarf2dbg: 2788 address += dwarf2dbg_estimate_size_before_relax (fragP); 2789 break; 2790 2791 default: 2792 BAD_CASE (fragP->fr_type); 2793 break; 2794 } 2795 } 2796 2797 /* Do relax(). */ 2798 { 2799 unsigned long max_iterations; 2800 2801 /* Cumulative address adjustment. */ 2802 offsetT stretch; 2803 2804 /* Have we made any adjustment this pass? We can't just test 2805 stretch because one piece of code may have grown and another 2806 shrank. */ 2807 int stretched; 2808 2809 /* Most horrible, but gcc may give us some exception data that 2810 is impossible to assemble, of the form 2811 2812 .align 4 2813 .byte 0, 0 2814 .uleb128 end - start 2815 start: 2816 .space 128*128 - 1 2817 .align 4 2818 end: 2819 2820 If the leb128 is two bytes in size, then end-start is 128*128, 2821 which requires a three byte leb128. If the leb128 is three 2822 bytes in size, then end-start is 128*128-1, which requires a 2823 two byte leb128. We work around this dilemma by inserting 2824 an extra 4 bytes of alignment just after the .align. This 2825 works because the data after the align is accessed relative to 2826 the end label. 2827 2828 This counter is used in a tiny state machine to detect 2829 whether a leb128 followed by an align is impossible to 2830 relax. */ 2831 int rs_leb128_fudge = 0; 2832 2833 /* We want to prevent going into an infinite loop where one frag grows 2834 depending upon the location of a symbol which is in turn moved by 2835 the growing frag. eg: 2836 2837 foo = . 2838 .org foo+16 2839 foo = . 2840 2841 So we dictate that this algorithm can be at most O2. */ 2842 max_iterations = frag_count * frag_count; 2843 /* Check for overflow. */ 2844 if (max_iterations < frag_count) 2845 max_iterations = frag_count; 2846 2847 ret = 0; 2848 do 2849 { 2850 stretch = 0; 2851 stretched = 0; 2852 2853 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) 2854 { 2855 offsetT growth = 0; 2856 addressT was_address; 2857 offsetT offset; 2858 symbolS *symbolP; 2859 2860 fragP->relax_marker ^= 1; 2861 was_address = fragP->fr_address; 2862 address = fragP->fr_address += stretch; 2863 symbolP = fragP->fr_symbol; 2864 offset = fragP->fr_offset; 2865 2866 switch (fragP->fr_type) 2867 { 2868 case rs_fill: /* .fill never relaxes. */ 2869 growth = 0; 2870 break; 2871 2872 #ifndef WORKING_DOT_WORD 2873 /* JF: This is RMS's idea. I do *NOT* want to be blamed 2874 for it I do not want to write it. I do not want to have 2875 anything to do with it. This is not the proper way to 2876 implement this misfeature. */ 2877 case rs_broken_word: 2878 { 2879 struct broken_word *lie; 2880 struct broken_word *untruth; 2881 2882 /* Yes this is ugly (storing the broken_word pointer 2883 in the symbol slot). Still, this whole chunk of 2884 code is ugly, and I don't feel like doing anything 2885 about it. Think of it as stubbornness in action. */ 2886 growth = 0; 2887 for (lie = (struct broken_word *) (fragP->fr_symbol); 2888 lie && lie->dispfrag == fragP; 2889 lie = lie->next_broken_word) 2890 { 2891 2892 if (lie->added) 2893 continue; 2894 2895 offset = (S_GET_VALUE (lie->add) 2896 + lie->addnum 2897 - S_GET_VALUE (lie->sub)); 2898 if (offset <= -32768 || offset >= 32767) 2899 { 2900 if (flag_warn_displacement) 2901 { 2902 char buf[50]; 2903 2904 bfd_sprintf_vma (stdoutput, buf, 2905 (addressT) lie->addnum); 2906 as_warn_where (fragP->fr_file, fragP->fr_line, 2907 _(".word %s-%s+%s didn't fit"), 2908 S_GET_NAME (lie->add), 2909 S_GET_NAME (lie->sub), 2910 buf); 2911 } 2912 if (fragP->fr_subtype == 0) 2913 { 2914 fragP->fr_subtype++; 2915 growth += md_short_jump_size; 2916 } 2917 2918 /* Redirect *all* words of this table with the same 2919 target, lest we have to handle the case where the 2920 same target but with a offset that fits on this 2921 round overflows at the next relaxation round. */ 2922 for (untruth = (struct broken_word *) (fragP->fr_symbol); 2923 untruth && untruth->dispfrag == lie->dispfrag; 2924 untruth = untruth->next_broken_word) 2925 if ((symbol_get_frag (untruth->add) 2926 == symbol_get_frag (lie->add)) 2927 && (S_GET_VALUE (untruth->add) 2928 == S_GET_VALUE (lie->add))) 2929 { 2930 untruth->added = 2; 2931 untruth->use_jump = lie; 2932 } 2933 2934 lie->added = 1; 2935 growth += md_long_jump_size; 2936 } 2937 } 2938 2939 break; 2940 } /* case rs_broken_word */ 2941 #endif 2942 case rs_align: 2943 case rs_align_code: 2944 case rs_align_test: 2945 { 2946 addressT oldoff, newoff; 2947 2948 oldoff = relax_align (was_address + fragP->fr_fix, 2949 (int) offset); 2950 newoff = relax_align (address + fragP->fr_fix, 2951 (int) offset); 2952 2953 if (fragP->fr_subtype != 0) 2954 { 2955 if (oldoff > fragP->fr_subtype) 2956 oldoff = 0; 2957 if (newoff > fragP->fr_subtype) 2958 newoff = 0; 2959 } 2960 2961 growth = newoff - oldoff; 2962 2963 /* If this align happens to follow a leb128 and 2964 we have determined that the leb128 is bouncing 2965 in size, then break the cycle by inserting an 2966 extra alignment. */ 2967 if (growth < 0 2968 && (rs_leb128_fudge & 16) != 0 2969 && (rs_leb128_fudge & 15) >= 2) 2970 { 2971 segment_info_type *seginfo = seg_info (segment); 2972 struct obstack *ob = &seginfo->frchainP->frch_obstack; 2973 struct frag *newf; 2974 2975 newf = frag_alloc (ob); 2976 obstack_blank_fast (ob, fragP->fr_var); 2977 obstack_finish (ob); 2978 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG); 2979 memcpy (newf->fr_literal, 2980 fragP->fr_literal + fragP->fr_fix, 2981 fragP->fr_var); 2982 newf->fr_type = rs_fill; 2983 newf->fr_address = address + fragP->fr_fix + newoff; 2984 newf->fr_fix = 0; 2985 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset) 2986 / fragP->fr_var); 2987 if (newf->fr_offset * newf->fr_var 2988 != (offsetT) 1 << fragP->fr_offset) 2989 { 2990 newf->fr_offset = (offsetT) 1 << fragP->fr_offset; 2991 newf->fr_var = 1; 2992 } 2993 /* Include size of new frag in GROWTH. */ 2994 growth += newf->fr_offset * newf->fr_var; 2995 /* Adjust the new frag address for the amount 2996 we'll add when we process the new frag. */ 2997 newf->fr_address -= stretch + growth; 2998 newf->relax_marker ^= 1; 2999 fragP->fr_next = newf; 3000 #ifdef DEBUG 3001 as_warn (_("padding added")); 3002 #endif 3003 } 3004 } 3005 break; 3006 3007 case rs_org: 3008 { 3009 offsetT target = offset; 3010 addressT after; 3011 3012 if (symbolP) 3013 { 3014 /* Convert from an actual address to an octet offset 3015 into the section. Here it is assumed that the 3016 section's VMA is zero, and can omit subtracting it 3017 from the symbol's value to get the address offset. */ 3018 know (S_GET_SEGMENT (symbolP)->vma == 0); 3019 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE; 3020 } 3021 3022 know (fragP->fr_next); 3023 after = fragP->fr_next->fr_address + stretch; 3024 growth = target - after; 3025 3026 /* Growth may be negative, but variable part of frag 3027 cannot have fewer than 0 chars. That is, we can't 3028 .org backwards. */ 3029 if ((offsetT) (address + fragP->fr_fix) > target) 3030 { 3031 growth = 0; 3032 3033 /* Don't error on first few frag relax passes. 3034 The symbol might be an expression involving 3035 symbol values from other sections. If those 3036 sections have not yet been processed their 3037 frags will all have zero addresses, so we 3038 will calculate incorrect values for them. The 3039 number of passes we allow before giving an 3040 error is somewhat arbitrary. It should be at 3041 least one, with larger values requiring 3042 increasingly contrived dependencies between 3043 frags to trigger a false error. */ 3044 if (pass < 2) 3045 { 3046 /* Force another pass. */ 3047 ret = 1; 3048 break; 3049 } 3050 3051 as_bad_where (fragP->fr_file, fragP->fr_line, 3052 _("attempt to move .org backwards")); 3053 3054 /* We've issued an error message. Change the 3055 frag to avoid cascading errors. */ 3056 fragP->fr_type = rs_align; 3057 fragP->fr_subtype = 0; 3058 fragP->fr_offset = 0; 3059 fragP->fr_fix = after - address; 3060 } 3061 } 3062 break; 3063 3064 case rs_space: 3065 case rs_space_nop: 3066 growth = 0; 3067 if (symbolP) 3068 { 3069 offsetT amount; 3070 3071 amount = S_GET_VALUE (symbolP); 3072 if (S_GET_SEGMENT (symbolP) != absolute_section 3073 || S_IS_COMMON (symbolP) 3074 || ! S_IS_DEFINED (symbolP)) 3075 { 3076 as_bad_where (fragP->fr_file, fragP->fr_line, 3077 _(".space, .nops or .fill specifies non-absolute value")); 3078 /* Prevent repeat of this error message. */ 3079 fragP->fr_symbol = 0; 3080 } 3081 else if (amount < 0) 3082 { 3083 /* Don't error on first few frag relax passes. 3084 See rs_org comment for a longer explanation. */ 3085 if (pass < 2) 3086 { 3087 ret = 1; 3088 break; 3089 } 3090 3091 as_warn_where (fragP->fr_file, fragP->fr_line, 3092 _(".space, .nops or .fill with negative value, ignored")); 3093 fragP->fr_symbol = 0; 3094 } 3095 else 3096 growth = (was_address + fragP->fr_fix + amount 3097 - fragP->fr_next->fr_address); 3098 } 3099 break; 3100 3101 case rs_machine_dependent: 3102 #ifdef md_relax_frag 3103 growth = md_relax_frag (segment, fragP, stretch); 3104 #else 3105 #ifdef TC_GENERIC_RELAX_TABLE 3106 /* The default way to relax a frag is to look through 3107 TC_GENERIC_RELAX_TABLE. */ 3108 growth = md_generic_table_relax_frag (segment, fragP, 3109 stretch); 3110 #endif /* TC_GENERIC_RELAX_TABLE */ 3111 #endif 3112 break; 3113 3114 case rs_leb128: 3115 { 3116 valueT value; 3117 offsetT size; 3118 3119 value = resolve_symbol_value (fragP->fr_symbol); 3120 size = sizeof_leb128 (value, fragP->fr_subtype); 3121 growth = size - fragP->fr_offset; 3122 fragP->fr_offset = size; 3123 } 3124 break; 3125 3126 case rs_cfa: 3127 growth = eh_frame_relax_frag (fragP); 3128 break; 3129 3130 case rs_dwarf2dbg: 3131 growth = dwarf2dbg_relax_frag (fragP); 3132 break; 3133 3134 default: 3135 BAD_CASE (fragP->fr_type); 3136 break; 3137 } 3138 if (growth) 3139 { 3140 stretch += growth; 3141 stretched = 1; 3142 if (fragP->fr_type == rs_leb128) 3143 rs_leb128_fudge += 16; 3144 else if (fragP->fr_type == rs_align 3145 && (rs_leb128_fudge & 16) != 0 3146 && stretch == 0) 3147 rs_leb128_fudge += 16; 3148 else 3149 rs_leb128_fudge = 0; 3150 } 3151 } 3152 3153 if (stretch == 0 3154 && (rs_leb128_fudge & 16) == 0 3155 && (rs_leb128_fudge & -16) != 0) 3156 rs_leb128_fudge += 1; 3157 else 3158 rs_leb128_fudge = 0; 3159 } 3160 /* Until nothing further to relax. */ 3161 while (stretched && -- max_iterations); 3162 3163 if (stretched) 3164 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"), 3165 segment_name (segment)); 3166 } 3167 3168 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) 3169 if (fragP->last_fr_address != fragP->fr_address) 3170 { 3171 fragP->last_fr_address = fragP->fr_address; 3172 ret = 1; 3173 } 3174 return ret; 3175 } 3176 3177 void 3178 number_to_chars_bigendian (char *buf, valueT val, int n) 3179 { 3180 if (n <= 0) 3181 abort (); 3182 while (n--) 3183 { 3184 buf[n] = val & 0xff; 3185 val >>= 8; 3186 } 3187 } 3188 3189 void 3190 number_to_chars_littleendian (char *buf, valueT val, int n) 3191 { 3192 if (n <= 0) 3193 abort (); 3194 while (n--) 3195 { 3196 *buf++ = val & 0xff; 3197 val >>= 8; 3198 } 3199 } 3200 3201 void 3202 write_print_statistics (FILE *file) 3203 { 3204 fprintf (file, "fixups: %d\n", n_fixups); 3205 } 3206 3207 /* For debugging. */ 3208 extern int indent_level; 3209 3210 void 3211 print_fixup (fixS *fixp) 3212 { 3213 indent_level = 1; 3214 fprintf (stderr, "fix "); 3215 fprintf_vma (stderr, (bfd_vma) (uintptr_t) fixp); 3216 fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line); 3217 if (fixp->fx_pcrel) 3218 fprintf (stderr, " pcrel"); 3219 if (fixp->fx_pcrel_adjust) 3220 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust); 3221 if (fixp->fx_tcbit) 3222 fprintf (stderr, " tcbit"); 3223 if (fixp->fx_done) 3224 fprintf (stderr, " done"); 3225 fprintf (stderr, "\n size=%d frag=", fixp->fx_size); 3226 fprintf_vma (stderr, (bfd_vma) (uintptr_t) fixp->fx_frag); 3227 fprintf (stderr, " where=%ld offset=%lx addnumber=%lx", 3228 (long) fixp->fx_where, 3229 (unsigned long) fixp->fx_offset, 3230 (unsigned long) fixp->fx_addnumber); 3231 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type), 3232 fixp->fx_r_type); 3233 if (fixp->fx_addsy) 3234 { 3235 fprintf (stderr, "\n +<"); 3236 print_symbol_value_1 (stderr, fixp->fx_addsy); 3237 fprintf (stderr, ">"); 3238 } 3239 if (fixp->fx_subsy) 3240 { 3241 fprintf (stderr, "\n -<"); 3242 print_symbol_value_1 (stderr, fixp->fx_subsy); 3243 fprintf (stderr, ">"); 3244 } 3245 fprintf (stderr, "\n"); 3246 #ifdef TC_FIX_DATA_PRINT 3247 TC_FIX_DATA_PRINT (stderr, fixp); 3248 #endif 3249 } 3250