arm, objdump: Make objdump use bfd's machine detection to drive disassembly
[binutils-gdb.git] / gas / write.c
blob853a9a012b765661bfbea470dd33fa2cda206c10
1 /* write.c - emit .o file
2 Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
21 /* This thing should be set up to do byte ordering correctly. But... */
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"
30 #ifndef TC_FORCE_RELOCATION
31 #define TC_FORCE_RELOCATION(FIX) \
32 (generic_force_reloc (FIX))
33 #endif
35 #ifndef TC_FORCE_RELOCATION_ABS
36 #define TC_FORCE_RELOCATION_ABS(FIX) \
37 (TC_FORCE_RELOCATION (FIX))
38 #endif
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
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
53 #ifndef md_register_arithmetic
54 # define md_register_arithmetic 1
55 #endif
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
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
71 #ifndef TC_VALIDATE_FIX_SUB
72 #define TC_VALIDATE_FIX_SUB(FIX, SEG) 0
73 #endif
75 #ifndef TC_LINKRELAX_FIXUP
76 #define TC_LINKRELAX_FIXUP(SEG) 1
77 #endif
79 #ifndef MD_APPLY_SYM_VALUE
80 #define MD_APPLY_SYM_VALUE(FIX) 1
81 #endif
83 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
84 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
85 #endif
87 #ifndef MD_PCREL_FROM_SECTION
88 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
89 #endif
91 #ifndef TC_FAKE_LABEL
92 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
93 #endif
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
105 /* Used to control final evaluation of expressions. */
106 int finalize_syms = 0;
108 int symbol_table_frozen;
110 symbolS *abs_section_sym;
112 /* Remember the value of dot when parsing expressions. */
113 addressT dot_value;
115 /* The frag that dot_value is based from. */
116 fragS *dot_frag;
118 /* Relocs generated by ".reloc" pseudo. */
119 struct reloc_list* reloc_list;
121 void print_fixup (fixS *);
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_before_relax, for example. */
127 static bool frags_chained = false;
129 static unsigned int n_fixups;
131 #define RELOC_ENUM enum bfd_reloc_code_real
133 /* Create a fixS in obstack 'notes'. */
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? */
146 fixS *fixP;
148 n_fixups++;
150 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
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)
158 as_bad (_("field fx_size too small to hold %lu"), size);
159 abort ();
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_tcbit3 = 0;
173 fixP->fx_done = 0;
174 fixP->fx_no_overflow = 0;
175 fixP->fx_signed = 0;
177 #ifdef USING_CGEN
178 fixP->fx_cgen.insn = NULL;
179 fixP->fx_cgen.opinfo = 0;
180 #endif
182 #ifdef TC_FIX_TYPE
183 TC_INIT_FIX_DATA (fixP);
184 #endif
186 fixP->fx_file = as_where (&fixP->fx_line);
190 fixS **seg_fix_rootP = (frags_chained
191 ? &seg_info (now_seg)->fix_root
192 : &frchain_now->fix_root);
193 fixS **seg_fix_tailP = (frags_chained
194 ? &seg_info (now_seg)->fix_tail
195 : &frchain_now->fix_tail);
197 if (at_beginning)
199 fixP->fx_next = *seg_fix_rootP;
200 *seg_fix_rootP = fixP;
201 if (fixP->fx_next == NULL)
202 *seg_fix_tailP = fixP;
204 else
206 fixP->fx_next = NULL;
207 if (*seg_fix_tailP)
208 (*seg_fix_tailP)->fx_next = fixP;
209 else
210 *seg_fix_rootP = fixP;
211 *seg_fix_tailP = fixP;
215 return fixP;
218 /* Create a fixup relative to a symbol (plus a constant). */
220 fixS *
221 fix_new (fragS *frag, /* Which frag? */
222 unsigned long where, /* Where in that frag? */
223 unsigned long size, /* 1, 2, or 4 usually. */
224 symbolS *add_symbol, /* X_add_symbol. */
225 offsetT offset, /* X_add_number. */
226 int pcrel, /* TRUE if PC-relative relocation. */
227 RELOC_ENUM r_type /* Relocation type. */)
229 return fix_new_internal (frag, where, size, add_symbol,
230 (symbolS *) NULL, offset, pcrel, r_type, false);
233 /* Create a fixup for an expression. Currently we only support fixups
234 for difference expressions. That is itself more than most object
235 file formats support anyhow. */
237 fixS *
238 fix_new_exp (fragS *frag, /* Which frag? */
239 unsigned long where, /* Where in that frag? */
240 unsigned long size, /* 1, 2, or 4 usually. */
241 const expressionS *exp, /* Expression. */
242 int pcrel, /* TRUE if PC-relative relocation. */
243 RELOC_ENUM r_type /* Relocation type. */)
245 symbolS *add = NULL;
246 symbolS *sub = NULL;
247 offsetT off = 0;
249 switch (exp->X_op)
251 case O_absent:
252 break;
254 case O_register:
255 as_bad (_("register value used as expression"));
256 break;
258 case O_symbol_rva:
259 add = exp->X_add_symbol;
260 off = exp->X_add_number;
261 r_type = BFD_RELOC_RVA;
262 break;
264 case O_uminus:
265 sub = exp->X_add_symbol;
266 off = exp->X_add_number;
267 break;
269 case O_subtract:
270 sub = exp->X_op_symbol;
271 /* Fall through. */
272 case O_symbol:
273 add = exp->X_add_symbol;
274 /* Fall through. */
275 case O_constant:
276 off = exp->X_add_number;
277 break;
279 case O_add: /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
280 the difference expression cannot immediately be reduced. */
281 default:
282 add = make_expr_symbol (exp);
283 break;
286 return fix_new_internal (frag, where, size, add, sub, off, pcrel,
287 r_type, false);
290 /* Create a fixup at the beginning of FRAG. The arguments are the same
291 as for fix_new, except that WHERE is implicitly 0. */
293 fixS *
294 fix_at_start (fragS *frag, unsigned long size, symbolS *add_symbol,
295 offsetT offset, int pcrel, RELOC_ENUM r_type)
297 return fix_new_internal (frag, 0, size, add_symbol,
298 (symbolS *) NULL, offset, pcrel, r_type, true);
301 /* Generic function to determine whether a fixup requires a relocation. */
303 generic_force_reloc (fixS *fix)
305 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
306 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
307 return 1;
309 if (fix->fx_addsy == NULL)
310 return 0;
312 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
315 /* Append a string onto another string, bumping the pointer along. */
316 void
317 append (char **charPP, char *fromP, unsigned long length)
319 /* Don't trust memcpy() of 0 chars. */
320 if (length == 0)
321 return;
323 memcpy (*charPP, fromP, length);
324 *charPP += length;
327 /* This routine records the largest alignment seen for each segment.
328 If the beginning of the segment is aligned on the worst-case
329 boundary, all of the other alignments within it will work. At
330 least one object format really uses this info. */
332 void
333 record_alignment (/* Segment to which alignment pertains. */
334 segT seg,
335 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
336 boundary, 2 => 4-byte boundary, etc.) */
337 unsigned int align)
339 if (seg == absolute_section)
340 return;
342 if (align > bfd_section_alignment (seg))
343 bfd_set_section_alignment (seg, align);
347 get_recorded_alignment (segT seg)
349 if (seg == absolute_section)
350 return 0;
352 return bfd_section_alignment (seg);
355 /* Reset the section indices after removing the gas created sections. */
357 static void
358 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
360 int *countp = (int *) countparg;
362 sec->index = *countp;
363 ++*countp;
366 static fragS *
367 chain_frchains_together_1 (segT section, struct frchain *frchp)
369 fragS dummy, *prev_frag = &dummy;
370 fixS fix_dummy, *prev_fix = &fix_dummy;
374 prev_frag->fr_next = frchp->frch_root;
375 prev_frag = frchp->frch_last;
376 gas_assert (prev_frag->fr_type != 0);
377 if (frchp->fix_root != (fixS *) NULL)
379 if (seg_info (section)->fix_root == (fixS *) NULL)
380 seg_info (section)->fix_root = frchp->fix_root;
381 prev_fix->fx_next = frchp->fix_root;
382 seg_info (section)->fix_tail = frchp->fix_tail;
383 prev_fix = frchp->fix_tail;
385 frchp = frchp->frch_next;
386 } while (frchp);
387 gas_assert (prev_frag != &dummy
388 && prev_frag->fr_type != 0);
389 prev_frag->fr_next = 0;
390 return prev_frag;
393 static void
394 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
395 segT section,
396 void *xxx ATTRIBUTE_UNUSED)
398 segment_info_type *info;
400 /* BFD may have introduced its own sections without using
401 subseg_new, so it is possible that seg_info is NULL. */
402 info = seg_info (section);
403 if (info != (segment_info_type *) NULL)
404 info->frchainP->frch_last
405 = chain_frchains_together_1 (section, info->frchainP);
407 /* Now that we've chained the frags together, we must add new fixups
408 to the segment, not to the frag chain. */
409 frags_chained = true;
412 static void
413 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
415 switch (fragP->fr_type)
417 case rs_space_nop:
418 goto skip_align;
419 case rs_align:
420 case rs_align_code:
421 case rs_align_test:
422 case rs_org:
423 case rs_space:
424 #ifdef HANDLE_ALIGN
425 HANDLE_ALIGN (fragP);
426 #endif
427 skip_align:
428 know (fragP->fr_next != NULL);
429 fragP->fr_offset = (fragP->fr_next->fr_address
430 - fragP->fr_address
431 - fragP->fr_fix) / fragP->fr_var;
432 if (fragP->fr_offset < 0)
434 as_bad_where (fragP->fr_file, fragP->fr_line,
435 _("attempt to .org/.space/.nops backwards? (%ld)"),
436 (long) fragP->fr_offset);
437 fragP->fr_offset = 0;
439 if (fragP->fr_type == rs_space_nop)
440 fragP->fr_type = rs_fill_nop;
441 else
442 fragP->fr_type = rs_fill;
443 break;
445 case rs_fill:
446 case rs_fill_nop:
447 break;
449 case rs_leb128:
451 valueT value = S_GET_VALUE (fragP->fr_symbol);
452 int size;
454 if (!S_IS_DEFINED (fragP->fr_symbol))
456 as_bad_where (fragP->fr_file, fragP->fr_line,
457 _("leb128 operand is an undefined symbol: %s"),
458 S_GET_NAME (fragP->fr_symbol));
461 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
462 fragP->fr_subtype);
464 fragP->fr_fix += size;
465 fragP->fr_type = rs_fill;
466 fragP->fr_var = 0;
467 fragP->fr_offset = 0;
468 fragP->fr_symbol = NULL;
470 break;
472 case rs_cfa:
473 eh_frame_convert_frag (fragP);
474 break;
476 case rs_dwarf2dbg:
477 dwarf2dbg_convert_frag (fragP);
478 break;
480 case rs_sframe:
481 sframe_convert_frag (fragP);
482 break;
484 case rs_machine_dependent:
485 md_convert_frag (stdoutput, sec, fragP);
487 gas_assert (fragP->fr_next == NULL
488 || (fragP->fr_next->fr_address - fragP->fr_address
489 == fragP->fr_fix));
491 /* After md_convert_frag, we make the frag into a ".space 0".
492 md_convert_frag() should set up any fixSs and constants
493 required. */
494 frag_wane (fragP);
495 break;
497 #ifndef WORKING_DOT_WORD
498 case rs_broken_word:
500 struct broken_word *lie;
502 if (fragP->fr_subtype)
504 fragP->fr_fix += md_short_jump_size;
505 for (lie = (struct broken_word *) (fragP->fr_symbol);
506 lie && lie->dispfrag == fragP;
507 lie = lie->next_broken_word)
508 if (lie->added == 1)
509 fragP->fr_fix += md_long_jump_size;
511 frag_wane (fragP);
513 break;
514 #endif
516 default:
517 BAD_CASE (fragP->fr_type);
518 break;
520 #ifdef md_frag_check
521 md_frag_check (fragP);
522 #endif
525 struct relax_seg_info
527 int pass;
528 int changed;
531 static void
532 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
534 segment_info_type *seginfo = seg_info (sec);
535 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
537 if (seginfo && seginfo->frchainP
538 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
539 info->changed = 1;
542 static void
543 size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
545 flagword flags;
546 fragS *fragp;
547 segment_info_type *seginfo;
548 int x;
549 valueT size, newsize;
551 subseg_change (sec, 0);
553 seginfo = seg_info (sec);
554 if (seginfo && seginfo->frchainP)
556 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
557 cvt_frag_to_fill (sec, fragp);
558 for (fragp = seginfo->frchainP->frch_root;
559 fragp->fr_next;
560 fragp = fragp->fr_next)
561 /* Walk to last elt. */
563 size = fragp->fr_address + fragp->fr_fix;
565 else
566 size = 0;
568 flags = bfd_section_flags (sec);
569 if (size == 0 && bfd_section_size (sec) != 0 &&
570 (flags & SEC_HAS_CONTENTS) != 0)
571 return;
573 if (size > 0 && ! seginfo->bss)
574 flags |= SEC_HAS_CONTENTS;
576 x = bfd_set_section_flags (sec, flags);
577 gas_assert (x);
579 /* If permitted, allow the backend to pad out the section
580 to some alignment boundary. */
581 if (do_not_pad_sections_to_alignment)
582 newsize = size;
583 else
584 newsize = md_section_align (sec, size);
585 x = bfd_set_section_size (sec, newsize);
586 gas_assert (x);
588 /* If the size had to be rounded up, add some padding in the last
589 non-empty frag. */
590 gas_assert (newsize >= size);
591 if (size != newsize)
593 fragS *last = seginfo->frchainP->frch_last;
594 fragp = seginfo->frchainP->frch_root;
595 while (fragp->fr_next != last)
596 fragp = fragp->fr_next;
597 last->fr_address = size;
598 if ((newsize - size) % fragp->fr_var == 0)
599 fragp->fr_offset += (newsize - size) / fragp->fr_var;
600 else
601 /* If we hit this abort, it's likely due to subsegs_finish not
602 providing sufficient alignment on the last frag, and the
603 machine dependent code using alignment frags with fr_var
604 greater than 1. */
605 abort ();
608 #ifdef tc_frob_section
609 tc_frob_section (sec);
610 #endif
611 #ifdef obj_frob_section
612 obj_frob_section (sec);
613 #endif
616 #ifdef DEBUG2
617 static void
618 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
620 segment_info_type *seginfo = seg_info (sec);
621 fixS *fixp = seginfo->fix_root;
623 if (!fixp)
624 return;
626 fprintf (stream, "sec %s relocs:\n", sec->name);
627 while (fixp)
629 symbolS *s = fixp->fx_addsy;
631 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
632 (int) fixp->fx_r_type);
633 if (s == NULL)
634 fprintf (stream, "no sym\n");
635 else
637 print_symbol_value_1 (stream, s);
638 fprintf (stream, "\n");
640 fixp = fixp->fx_next;
643 #else
644 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
645 #endif
647 #ifndef EMIT_SECTION_SYMBOLS
648 #define EMIT_SECTION_SYMBOLS 1
649 #endif
651 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
652 and check for validity. Convert RELOC_LIST from using U.A fields
653 to U.B fields. */
654 static void
655 resolve_reloc_expr_symbols (void)
657 bfd_vma addr_mask = 1;
658 struct reloc_list *r;
660 /* Avoid a shift by the width of type. */
661 addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1;
662 addr_mask <<= 1;
663 addr_mask -= 1;
665 for (r = reloc_list; r; r = r->next)
667 reloc_howto_type *howto = r->u.a.howto;
668 expressionS *symval;
669 symbolS *sym;
670 bfd_vma offset, addend;
671 asection *sec;
673 resolve_symbol_value (r->u.a.offset_sym);
674 symval = symbol_get_value_expression (r->u.a.offset_sym);
676 offset = 0;
677 sym = NULL;
678 if (symval->X_op == O_constant)
679 sym = r->u.a.offset_sym;
680 else if (symval->X_op == O_symbol)
682 sym = symval->X_add_symbol;
683 offset = symval->X_add_number;
684 symval = symbol_get_value_expression (symval->X_add_symbol);
686 if (sym == NULL
687 || symval->X_op != O_constant
688 || (sec = S_GET_SEGMENT (sym)) == NULL
689 || !SEG_NORMAL (sec))
691 as_bad_where (r->file, r->line, _("invalid offset expression"));
692 sec = NULL;
694 else
695 offset += S_GET_VALUE (sym);
697 sym = NULL;
698 addend = r->u.a.addend;
699 if (r->u.a.sym != NULL)
701 resolve_symbol_value (r->u.a.sym);
702 symval = symbol_get_value_expression (r->u.a.sym);
703 if (symval->X_op == O_constant)
704 sym = r->u.a.sym;
705 else if (symval->X_op == O_symbol)
707 sym = symval->X_add_symbol;
708 addend += symval->X_add_number;
709 symval = symbol_get_value_expression (symval->X_add_symbol);
711 if (symval->X_op != O_constant)
713 as_bad_where (r->file, r->line, _("invalid reloc expression"));
714 sec = NULL;
716 else if (sym != NULL && sec != NULL)
718 /* Convert relocs against local symbols to refer to the
719 corresponding section symbol plus offset instead. Keep
720 PC-relative relocs of the REL variety intact though to
721 prevent the offset from overflowing the relocated field,
722 unless it has enough bits to cover the whole address
723 space. */
724 if (S_IS_LOCAL (sym)
725 && S_IS_DEFINED (sym)
726 && !symbol_section_p (sym)
727 && (sec->use_rela_p
728 || (howto->partial_inplace
729 && (!howto->pc_relative
730 || howto->src_mask == addr_mask))))
732 asection *symsec = S_GET_SEGMENT (sym);
733 if (!(((symsec->flags & SEC_MERGE) != 0
734 && addend != 0)
735 || (symsec->flags & SEC_THREAD_LOCAL) != 0))
737 addend += S_GET_VALUE (sym);
738 sym = section_symbol (symsec);
741 symbol_mark_used_in_reloc (sym);
744 if (sym == NULL)
746 if (abs_section_sym == NULL)
747 abs_section_sym = section_symbol (absolute_section);
748 sym = abs_section_sym;
751 r->u.b.sec = sec;
752 r->u.b.s = symbol_get_bfdsym (sym);
753 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
754 r->u.b.r.address = offset;
755 r->u.b.r.addend = addend;
756 r->u.b.r.howto = howto;
760 /* This pass over fixups decides whether symbols can be replaced with
761 section symbols. */
763 static void
764 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
765 asection *sec,
766 void *xxx ATTRIBUTE_UNUSED)
768 segment_info_type *seginfo = seg_info (sec);
769 fixS *fixp;
770 valueT val;
772 if (seginfo == NULL)
773 return;
775 dump_section_relocs (abfd, sec, stderr);
777 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
778 if (fixp->fx_done)
779 /* Ignore it. */
781 else if (fixp->fx_addsy)
783 symbolS *sym;
784 asection *symsec;
786 #ifdef DEBUG5
787 fprintf (stderr, "\n\nadjusting fixup:\n");
788 print_fixup (fixp);
789 #endif
791 sym = fixp->fx_addsy;
793 /* All symbols should have already been resolved at this
794 point. It is possible to see unresolved expression
795 symbols, though, since they are not in the regular symbol
796 table. */
797 resolve_symbol_value (sym);
799 if (fixp->fx_subsy != NULL)
800 resolve_symbol_value (fixp->fx_subsy);
802 /* If this symbol is equated to an undefined or common symbol,
803 convert the fixup to being against that symbol. */
804 while (symbol_equated_reloc_p (sym)
805 || S_IS_WEAKREFR (sym))
807 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
808 if (sym == newsym)
809 break;
810 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
811 fixp->fx_addsy = newsym;
812 sym = newsym;
815 if (symbol_mri_common_p (sym))
817 fixp->fx_offset += S_GET_VALUE (sym);
818 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
819 continue;
822 /* If the symbol is undefined, common, weak, or global (ELF
823 shared libs), we can't replace it with the section symbol. */
824 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
825 continue;
827 /* Is there some other (target cpu dependent) reason we can't adjust
828 this one? (E.g. relocations involving function addresses on
829 the PA. */
830 #ifdef tc_fix_adjustable
831 if (! tc_fix_adjustable (fixp))
832 continue;
833 #endif
835 /* Since we're reducing to section symbols, don't attempt to reduce
836 anything that's already using one. */
837 if (symbol_section_p (sym))
839 /* Mark the section symbol used in relocation so that it will
840 be included in the symbol table. */
841 symbol_mark_used_in_reloc (sym);
842 continue;
845 symsec = S_GET_SEGMENT (sym);
846 if (symsec == NULL)
847 abort ();
849 if (bfd_is_abs_section (symsec)
850 || symsec == reg_section)
852 /* The fixup_segment routine normally will not use this
853 symbol in a relocation. */
854 continue;
857 /* Don't try to reduce relocs which refer to non-local symbols
858 in .linkonce sections. It can lead to confusion when a
859 debugging section refers to a .linkonce section. I hope
860 this will always be correct. */
861 if (symsec != sec && ! S_IS_LOCAL (sym))
863 if ((symsec->flags & SEC_LINK_ONCE) != 0
864 || (IS_ELF
865 /* The GNU toolchain uses an extension for ELF: a
866 section beginning with the magic string
867 .gnu.linkonce is a linkonce section. */
868 && startswith (segment_name (symsec), ".gnu.linkonce")))
869 continue;
872 /* Never adjust a reloc against local symbol in a merge section
873 with non-zero addend. */
874 if ((symsec->flags & SEC_MERGE) != 0
875 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
876 continue;
878 /* Never adjust a reloc against TLS local symbol. */
879 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
880 continue;
882 val = S_GET_VALUE (sym);
884 #if defined(TC_AARCH64) && defined(OBJ_COFF)
885 /* coff aarch64 relocation offsets need to be limited to 21bits.
886 This is because addend may need to be stored in an ADRP instruction.
887 In this case the addend cannot be stored down shifted otherwise rounding errors occur. */
888 if ((val + 0x100000) > 0x1fffff)
889 continue;
890 #endif
892 /* We refetch the segment when calling section_symbol, rather
893 than using symsec, because S_GET_VALUE may wind up changing
894 the section when it calls resolve_symbol_value. */
895 fixp->fx_offset += val;
896 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
897 #ifdef DEBUG5
898 fprintf (stderr, "\nadjusted fixup:\n");
899 print_fixup (fixp);
900 #endif
903 dump_section_relocs (abfd, sec, stderr);
906 void
907 as_bad_subtract (fixS *fixp)
909 as_bad_where (fixp->fx_file, fixp->fx_line,
910 _("can't resolve %s - %s"),
911 fixp->fx_addsy ? S_GET_NAME (fixp->fx_addsy) : "0",
912 S_GET_NAME (fixp->fx_subsy));
915 /* fixup_segment()
917 Go through all the fixS's in a segment and see which ones can be
918 handled now. (These consist of fixS where we have since discovered
919 the value of a symbol, or the address of the frag involved.)
920 For each one, call md_apply_fix to put the fix into the frag data.
921 Ones that we couldn't completely handle here will be output later
922 by emit_relocations. */
924 static void
925 fixup_segment (fixS *fixP, segT this_segment)
927 valueT add_number;
928 fragS *fragP;
930 if (fixP != NULL && abs_section_sym == NULL)
931 abs_section_sym = section_symbol (absolute_section);
933 /* If the linker is doing the relaxing, we must not do any fixups.
935 Well, strictly speaking that's not true -- we could do any that
936 are PC-relative and don't cross regions that could change size. */
937 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
939 for (; fixP; fixP = fixP->fx_next)
940 if (!fixP->fx_done)
942 if (fixP->fx_addsy == NULL)
944 /* There was no symbol required by this relocation.
945 However, BFD doesn't really handle relocations
946 without symbols well. So fake up a local symbol in
947 the absolute section. */
948 fixP->fx_addsy = abs_section_sym;
950 symbol_mark_used_in_reloc (fixP->fx_addsy);
951 if (fixP->fx_subsy != NULL)
952 symbol_mark_used_in_reloc (fixP->fx_subsy);
954 return;
957 for (; fixP; fixP = fixP->fx_next)
959 segT add_symbol_segment = absolute_section;
961 #ifdef DEBUG5
962 fprintf (stderr, "\nprocessing fixup:\n");
963 print_fixup (fixP);
964 #endif
966 fragP = fixP->fx_frag;
967 know (fragP);
968 #ifdef TC_VALIDATE_FIX
969 TC_VALIDATE_FIX (fixP, this_segment, skip);
970 #endif
971 add_number = fixP->fx_offset;
973 if (fixP->fx_addsy != NULL)
974 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
976 if (fixP->fx_subsy != NULL)
978 segT sub_symbol_segment;
980 resolve_symbol_value (fixP->fx_subsy);
981 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
983 if (fixP->fx_addsy != NULL
984 && sub_symbol_segment == add_symbol_segment
985 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
986 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
987 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
989 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
990 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
991 fixP->fx_offset = add_number;
992 fixP->fx_addsy = NULL;
993 fixP->fx_subsy = NULL;
994 #ifdef TC_M68K
995 /* See the comment below about 68k weirdness. */
996 fixP->fx_pcrel = 0;
997 #endif
999 else if (sub_symbol_segment == absolute_section
1000 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
1001 && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
1003 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1004 fixP->fx_offset = add_number;
1005 fixP->fx_subsy = NULL;
1007 else if (sub_symbol_segment == this_segment
1008 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
1009 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
1011 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1012 fixP->fx_offset = (add_number + fixP->fx_dot_value
1013 + fixP->fx_dot_frag->fr_address);
1015 /* Make it pc-relative. If the back-end code has not
1016 selected a pc-relative reloc, cancel the adjustment
1017 we do later on all pc-relative relocs. */
1018 if (0
1019 #ifdef TC_M68K
1020 /* Do this for m68k even if it's already described
1021 as pc-relative. On the m68k, an operand of
1022 "pc@(foo-.-2)" should address "foo" in a
1023 pc-relative mode. */
1024 || 1
1025 #endif
1026 || !fixP->fx_pcrel)
1027 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
1028 fixP->fx_subsy = NULL;
1029 fixP->fx_pcrel = 1;
1031 else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
1033 if (!md_register_arithmetic
1034 && (add_symbol_segment == reg_section
1035 || sub_symbol_segment == reg_section))
1036 as_bad_where (fixP->fx_file, fixP->fx_line,
1037 _("register value used as expression"));
1038 else
1039 as_bad_subtract (fixP);
1041 else if (sub_symbol_segment != undefined_section
1042 && ! bfd_is_com_section (sub_symbol_segment)
1043 && MD_APPLY_SYM_VALUE (fixP))
1044 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1047 if (fixP->fx_addsy)
1049 if (add_symbol_segment == this_segment
1050 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1051 && !TC_FORCE_RELOCATION_LOCAL (fixP))
1053 /* This fixup was made when the symbol's segment was
1054 SEG_UNKNOWN, but it is now in the local segment.
1055 So we know how to do the address without relocation. */
1056 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1057 fixP->fx_offset = add_number;
1058 if (fixP->fx_pcrel)
1059 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1060 fixP->fx_addsy = NULL;
1061 fixP->fx_pcrel = 0;
1063 else if (add_symbol_segment == absolute_section
1064 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1065 && !TC_FORCE_RELOCATION_ABS (fixP))
1067 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1068 fixP->fx_offset = add_number;
1069 fixP->fx_addsy = NULL;
1071 else if (add_symbol_segment != undefined_section
1072 && ! bfd_is_com_section (add_symbol_segment)
1073 && MD_APPLY_SYM_VALUE (fixP))
1074 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1077 if (fixP->fx_pcrel)
1079 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1080 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1082 /* There was no symbol required by this relocation.
1083 However, BFD doesn't really handle relocations
1084 without symbols well. So fake up a local symbol in
1085 the absolute section. */
1086 fixP->fx_addsy = abs_section_sym;
1090 if (!fixP->fx_done)
1091 md_apply_fix (fixP, &add_number, this_segment);
1093 if (!fixP->fx_done)
1095 if (fixP->fx_addsy == NULL)
1096 fixP->fx_addsy = abs_section_sym;
1097 symbol_mark_used_in_reloc (fixP->fx_addsy);
1098 if (fixP->fx_subsy != NULL)
1099 symbol_mark_used_in_reloc (fixP->fx_subsy);
1102 if (!fixP->fx_no_overflow && fixP->fx_size != 0)
1104 if (fixP->fx_size < sizeof (valueT))
1106 valueT mask;
1108 mask = 0;
1109 mask--; /* Set all bits to one. */
1110 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1111 if ((add_number & mask) != 0
1112 && (fixP->fx_signed
1113 ? (add_number & mask) != mask
1114 : (-add_number & mask) != 0))
1116 char buf[50], buf2[50];
1117 bfd_sprintf_vma (stdoutput, buf, fragP->fr_address + fixP->fx_where);
1118 if (add_number > 1000)
1119 bfd_sprintf_vma (stdoutput, buf2, add_number);
1120 else
1121 sprintf (buf2, "%ld", (long) add_number);
1122 as_bad_where (fixP->fx_file, fixP->fx_line,
1123 ngettext ("value of %s too large for field "
1124 "of %d byte at %s",
1125 "value of %s too large for field "
1126 "of %d bytes at %s",
1127 fixP->fx_size),
1128 buf2, fixP->fx_size, buf);
1129 } /* Generic error checking. */
1131 #ifdef WARN_SIGNED_OVERFLOW_WORD
1132 /* Warn if a .word value is too large when treated as a signed
1133 number. We already know it is not too negative. This is to
1134 catch over-large switches generated by gcc on the 68k. */
1135 if (!flag_signed_overflow_ok
1136 && fixP->fx_size == 2
1137 && add_number > 0x7fff)
1138 as_bad_where (fixP->fx_file, fixP->fx_line,
1139 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1140 (long) add_number,
1141 (long) (fragP->fr_address + fixP->fx_where));
1142 #endif
1145 #ifdef TC_VALIDATE_FIX
1146 skip: ATTRIBUTE_UNUSED_LABEL
1148 #endif
1149 #ifdef DEBUG5
1150 fprintf (stderr, "result:\n");
1151 print_fixup (fixP);
1152 #endif
1153 } /* For each fixS in this segment. */
1156 static void
1157 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1158 asection *sec,
1159 void *xxx ATTRIBUTE_UNUSED)
1161 segment_info_type *seginfo = seg_info (sec);
1163 fixup_segment (seginfo->fix_root, sec);
1166 static void
1167 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1168 const char *file, unsigned int line)
1170 char *err;
1171 bfd_reloc_status_type s;
1172 asymbol *sym;
1174 if (reloc->sym_ptr_ptr != NULL
1175 && (sym = *reloc->sym_ptr_ptr) != NULL
1176 && (sym->flags & BSF_KEEP) == 0
1177 && ((sym->flags & BSF_SECTION_SYM) == 0
1178 || (EMIT_SECTION_SYMBOLS
1179 && !bfd_is_abs_section (sym->section))))
1180 as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
1182 s = bfd_install_relocation (stdoutput, reloc,
1183 fragp->fr_literal, fragp->fr_address,
1184 sec, &err);
1185 switch (s)
1187 case bfd_reloc_ok:
1188 break;
1189 case bfd_reloc_overflow:
1190 as_bad_where (file, line, _("relocation overflow"));
1191 break;
1192 case bfd_reloc_outofrange:
1193 as_bad_where (file, line, _("relocation out of range"));
1194 break;
1195 default:
1196 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1197 file, line, s);
1201 static fragS *
1202 get_frag_for_reloc (fragS *last_frag,
1203 const segment_info_type *seginfo,
1204 const struct reloc_list *r)
1206 fragS *f;
1208 for (f = last_frag; f != NULL; f = f->fr_next)
1209 if (f->fr_address <= r->u.b.r.address
1210 && r->u.b.r.address < f->fr_address + f->fr_fix)
1211 return f;
1213 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1214 if (f->fr_address <= r->u.b.r.address
1215 && r->u.b.r.address < f->fr_address + f->fr_fix)
1216 return f;
1218 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1219 if (f->fr_address <= r->u.b.r.address
1220 && r->u.b.r.address <= f->fr_address + f->fr_fix)
1221 return f;
1223 as_bad_where (r->file, r->line,
1224 _("reloc not within (fixed part of) section"));
1225 return NULL;
1228 static void
1229 write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1230 void *xxx ATTRIBUTE_UNUSED)
1232 segment_info_type *seginfo = seg_info (sec);
1233 unsigned int n;
1234 struct reloc_list *my_reloc_list, **rp, *r;
1235 arelent **relocs;
1236 fixS *fixp;
1237 fragS *last_frag;
1239 /* If seginfo is NULL, we did not create this section; don't do
1240 anything with it. */
1241 if (seginfo == NULL)
1242 return;
1244 n = 0;
1245 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1246 if (!fixp->fx_done)
1247 n++;
1249 #ifdef RELOC_EXPANSION_POSSIBLE
1250 n *= MAX_RELOC_EXPANSION;
1251 #endif
1253 /* Extract relocs for this section from reloc_list. */
1254 rp = &reloc_list;
1256 my_reloc_list = NULL;
1257 while ((r = *rp) != NULL)
1259 if (r->u.b.sec == sec)
1261 *rp = r->next;
1262 r->next = my_reloc_list;
1263 my_reloc_list = r;
1264 n++;
1266 else
1267 rp = &r->next;
1270 relocs = XCNEWVEC (arelent *, n);
1272 n = 0;
1273 r = my_reloc_list;
1274 last_frag = NULL;
1275 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1277 int fx_size, slack;
1278 valueT loc;
1279 arelent **reloc;
1280 #ifndef RELOC_EXPANSION_POSSIBLE
1281 arelent *rel;
1283 reloc = &rel;
1284 #endif
1286 if (fixp->fx_done)
1287 continue;
1289 fx_size = fixp->fx_size;
1290 slack = TC_FX_SIZE_SLACK (fixp);
1291 if (slack > 0)
1292 fx_size = fx_size > slack ? fx_size - slack : 0;
1293 loc = fixp->fx_where + fx_size;
1294 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1295 as_bad_where (fixp->fx_file, fixp->fx_line,
1296 _("internal error: fixup not contained within frag"));
1298 #ifdef obj_fixup_removed_symbol
1299 if (fixp->fx_addsy && symbol_removed_p (fixp->fx_addsy))
1300 obj_fixup_removed_symbol (&fixp->fx_addsy);
1301 if (fixp->fx_subsy && symbol_removed_p (fixp->fx_subsy))
1302 obj_fixup_removed_symbol (&fixp->fx_subsy);
1303 #endif
1305 #ifndef RELOC_EXPANSION_POSSIBLE
1306 *reloc = tc_gen_reloc (sec, fixp);
1307 #else
1308 reloc = tc_gen_reloc (sec, fixp);
1309 #endif
1311 while (*reloc)
1313 while (r != NULL && r->u.b.r.address < (*reloc)->address)
1315 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1316 if (f != NULL)
1318 last_frag = f;
1319 relocs[n++] = &r->u.b.r;
1320 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1322 r = r->next;
1324 #ifdef GAS_SORT_RELOCS
1325 if (n != 0 && (*reloc)->address < relocs[n - 1]->address)
1327 size_t lo = 0;
1328 size_t hi = n - 1;
1329 bfd_vma look = (*reloc)->address;
1330 while (lo < hi)
1332 size_t mid = (lo + hi) / 2;
1333 if (relocs[mid]->address > look)
1334 hi = mid;
1335 else
1337 lo = mid + 1;
1338 if (relocs[mid]->address == look)
1339 break;
1342 while (lo < hi && relocs[lo]->address == look)
1343 lo++;
1344 memmove (relocs + lo + 1, relocs + lo,
1345 (n - lo) * sizeof (*relocs));
1346 n++;
1347 relocs[lo] = *reloc;
1349 else
1350 #endif
1351 relocs[n++] = *reloc;
1352 install_reloc (sec, *reloc, fixp->fx_frag,
1353 fixp->fx_file, fixp->fx_line);
1354 #ifndef RELOC_EXPANSION_POSSIBLE
1355 break;
1356 #else
1357 reloc++;
1358 #endif
1362 while (r != NULL)
1364 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1365 if (f != NULL)
1367 last_frag = f;
1368 relocs[n++] = &r->u.b.r;
1369 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1371 r = r->next;
1374 #ifdef DEBUG4
1376 unsigned int k, j, nsyms;
1377 asymbol **sympp;
1378 sympp = bfd_get_outsymbols (stdoutput);
1379 nsyms = bfd_get_symcount (stdoutput);
1380 for (k = 0; k < n; k++)
1381 if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1383 for (j = 0; j < nsyms; j++)
1384 if (sympp[j] == *relocs[k]->sym_ptr_ptr)
1385 break;
1386 if (j == nsyms)
1387 abort ();
1390 #endif
1392 bfd_set_reloc (stdoutput, sec, n ? relocs : NULL, n);
1394 #ifdef SET_SECTION_RELOCS
1395 SET_SECTION_RELOCS (sec, relocs, n);
1396 #endif
1398 #ifdef DEBUG3
1400 unsigned int k;
1402 fprintf (stderr, "relocs for sec %s\n", sec->name);
1403 for (k = 0; k < n; k++)
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);
1412 #endif
1415 static int
1416 compress_frag (bool use_zstd, void *ctx, const char *contents, int in_size,
1417 fragS **last_newf, struct obstack *ob)
1419 int out_size;
1420 int total_out_size = 0;
1421 fragS *f = *last_newf;
1422 char *next_out;
1423 int avail_out;
1425 /* Call the compression routine repeatedly until it has finished
1426 processing the frag. */
1427 while (in_size > 0)
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)
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);
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 (use_zstd, ctx, &contents, &in_size, &next_out,
1446 &avail_out);
1447 if (out_size < 0)
1448 return -1;
1450 f->fr_fix += out_size;
1451 total_out_size += out_size;
1453 /* Return unused space. */
1454 if (avail_out > 0)
1455 obstack_blank_fast (ob, -avail_out);
1458 return total_out_size;
1461 static void
1462 compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1464 segment_info_type *seginfo = seg_info (sec);
1465 bfd_size_type uncompressed_size = sec->size;
1466 flagword flags = bfd_section_flags (sec);
1468 if (seginfo == NULL
1469 || uncompressed_size < 32
1470 || (flags & SEC_HAS_CONTENTS) == 0)
1471 return;
1473 const char *section_name = bfd_section_name (sec);
1474 if (!startswith (section_name, ".debug_")
1475 && !startswith (section_name, ".gnu.debuglto_.debug_")
1476 && !startswith (section_name, ".gnu.linkonce.wi."))
1477 return;
1479 bool use_zstd = abfd->flags & BFD_COMPRESS_ZSTD;
1480 void *ctx = compress_init (use_zstd);
1481 if (ctx == NULL)
1482 return;
1484 unsigned int header_size;
1485 if ((abfd->flags & BFD_COMPRESS_GABI) == 0)
1486 header_size = 12;
1487 else
1488 header_size = bfd_get_compression_header_size (stdoutput, NULL);
1490 /* Create a new frag to contain the compression header. */
1491 struct obstack *ob = &seginfo->frchainP->frch_obstack;
1492 fragS *first_newf = frag_alloc (ob);
1493 if (obstack_room (ob) < header_size)
1494 first_newf = frag_alloc (ob);
1495 if (obstack_room (ob) < header_size)
1496 as_fatal (ngettext ("can't extend frag %lu char",
1497 "can't extend frag %lu chars",
1498 (unsigned long) header_size),
1499 (unsigned long) header_size);
1500 fragS *last_newf = first_newf;
1501 obstack_blank_fast (ob, header_size);
1502 last_newf->fr_type = rs_fill;
1503 last_newf->fr_fix = header_size;
1504 char *header = last_newf->fr_literal;
1505 bfd_size_type compressed_size = header_size;
1507 /* Stream the frags through the compression engine, adding new frags
1508 as necessary to accommodate the compressed output. */
1509 for (fragS *f = seginfo->frchainP->frch_root;
1511 f = f->fr_next)
1513 offsetT fill_size;
1514 char *fill_literal;
1515 offsetT count;
1516 int out_size;
1518 gas_assert (f->fr_type == rs_fill);
1519 if (f->fr_fix)
1521 out_size = compress_frag (use_zstd, ctx, f->fr_literal, f->fr_fix,
1522 &last_newf, ob);
1523 if (out_size < 0)
1524 return;
1525 compressed_size += out_size;
1527 fill_literal = f->fr_literal + f->fr_fix;
1528 fill_size = f->fr_var;
1529 count = f->fr_offset;
1530 gas_assert (count >= 0);
1531 if (fill_size && count)
1533 while (count--)
1535 out_size = compress_frag (use_zstd, ctx, fill_literal,
1536 (int)fill_size, &last_newf, ob);
1537 if (out_size < 0)
1538 return;
1539 compressed_size += out_size;
1544 /* Flush the compression state. */
1545 for (;;)
1547 int avail_out;
1548 char *next_out;
1549 int out_size;
1551 /* Reserve all the space available in the current chunk.
1552 If none is available, start a new frag. */
1553 avail_out = obstack_room (ob);
1554 if (avail_out <= 0)
1556 fragS *newf;
1558 obstack_finish (ob);
1559 newf = frag_alloc (ob);
1560 newf->fr_type = rs_fill;
1561 last_newf->fr_next = newf;
1562 last_newf = newf;
1563 avail_out = obstack_room (ob);
1565 if (avail_out <= 0)
1566 as_fatal (_("can't extend frag"));
1567 next_out = obstack_next_free (ob);
1568 obstack_blank_fast (ob, avail_out);
1569 int x = compress_finish (use_zstd, ctx, &next_out, &avail_out, &out_size);
1570 if (x < 0)
1571 return;
1573 last_newf->fr_fix += out_size;
1574 compressed_size += out_size;
1576 /* Return unused space. */
1577 if (avail_out > 0)
1578 obstack_blank_fast (ob, -avail_out);
1580 if (x == 0)
1581 break;
1584 /* PR binutils/18087: If compression didn't make the section smaller,
1585 just keep it uncompressed. */
1586 if (compressed_size >= uncompressed_size)
1587 return;
1589 /* Replace the uncompressed frag list with the compressed frag list. */
1590 seginfo->frchainP->frch_root = first_newf;
1591 seginfo->frchainP->frch_last = last_newf;
1593 /* Update the section size and its name. */
1594 bfd_update_compression_header (abfd, (bfd_byte *) header, sec);
1595 bool x = bfd_set_section_size (sec, compressed_size);
1596 gas_assert (x);
1597 if ((abfd->flags & BFD_COMPRESS_GABI) == 0
1598 && section_name[1] == 'd')
1600 char *compressed_name = bfd_debug_name_to_zdebug (abfd, section_name);
1601 bfd_rename_section (sec, compressed_name);
1605 #ifndef md_generate_nops
1606 /* Genenerate COUNT bytes of no-op instructions to WHERE. A target
1607 backend must override this with proper no-op instructions. */
1609 static void
1610 md_generate_nops (fragS *f ATTRIBUTE_UNUSED,
1611 char *where ATTRIBUTE_UNUSED,
1612 offsetT count ATTRIBUTE_UNUSED,
1613 int control ATTRIBUTE_UNUSED)
1615 as_bad (_("unimplemented .nops directive"));
1617 #endif
1619 static void
1620 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1621 asection *sec,
1622 void *xxx ATTRIBUTE_UNUSED)
1624 segment_info_type *seginfo = seg_info (sec);
1625 addressT offset = 0;
1626 fragS *f;
1628 /* Write out the frags. */
1629 if (seginfo == NULL
1630 || !(bfd_section_flags (sec) & SEC_HAS_CONTENTS))
1631 return;
1633 for (f = seginfo->frchainP->frch_root;
1635 f = f->fr_next)
1637 int x;
1638 addressT fill_size;
1639 char *fill_literal;
1640 offsetT count;
1642 gas_assert (f->fr_type == rs_fill || f->fr_type == rs_fill_nop);
1643 if (f->fr_fix)
1645 x = bfd_set_section_contents (stdoutput, sec,
1646 f->fr_literal, (file_ptr) offset,
1647 (bfd_size_type) f->fr_fix);
1648 if (!x)
1649 as_fatal (ngettext ("can't write %ld byte "
1650 "to section %s of %s: '%s'",
1651 "can't write %ld bytes "
1652 "to section %s of %s: '%s'",
1653 (long) f->fr_fix),
1654 (long) f->fr_fix,
1655 bfd_section_name (sec), bfd_get_filename (stdoutput),
1656 bfd_errmsg (bfd_get_error ()));
1657 offset += f->fr_fix;
1660 fill_size = f->fr_var;
1661 count = f->fr_offset;
1662 fill_literal = f->fr_literal + f->fr_fix;
1664 if (f->fr_type == rs_fill_nop)
1666 gas_assert (count >= 0 && fill_size == 1);
1667 if (count > 0)
1669 char *buf = xmalloc (count);
1670 md_generate_nops (f, buf, count, *fill_literal);
1671 x = bfd_set_section_contents
1672 (stdoutput, sec, buf, (file_ptr) offset,
1673 (bfd_size_type) count);
1674 if (!x)
1675 as_fatal (ngettext ("can't fill %ld byte "
1676 "in section %s of %s: '%s'",
1677 "can't fill %ld bytes "
1678 "in section %s of %s: '%s'",
1679 (long) count),
1680 (long) count,
1681 bfd_section_name (sec),
1682 bfd_get_filename (stdoutput),
1683 bfd_errmsg (bfd_get_error ()));
1684 offset += count;
1685 #ifndef NO_LISTING
1686 if (listing & LISTING_LISTING)
1687 f->fr_opcode = buf;
1688 else
1689 #endif
1690 free (buf);
1692 continue;
1695 gas_assert (count >= 0);
1696 if (fill_size && count)
1698 char buf[256];
1699 if (fill_size > sizeof (buf))
1701 /* Do it the old way. Can this ever happen? */
1702 while (count--)
1704 x = bfd_set_section_contents (stdoutput, sec,
1705 fill_literal,
1706 (file_ptr) offset,
1707 (bfd_size_type) fill_size);
1708 if (!x)
1709 as_fatal (ngettext ("can't fill %ld byte "
1710 "in section %s of %s: '%s'",
1711 "can't fill %ld bytes "
1712 "in section %s of %s: '%s'",
1713 (long) fill_size),
1714 (long) fill_size,
1715 bfd_section_name (sec),
1716 bfd_get_filename (stdoutput),
1717 bfd_errmsg (bfd_get_error ()));
1718 offset += fill_size;
1721 else
1723 /* Build a buffer full of fill objects and output it as
1724 often as necessary. This saves on the overhead of
1725 potentially lots of bfd_set_section_contents calls. */
1726 int n_per_buf, i;
1727 if (fill_size == 1)
1729 n_per_buf = sizeof (buf);
1730 memset (buf, *fill_literal, n_per_buf);
1732 else
1734 char *bufp;
1735 n_per_buf = sizeof (buf) / fill_size;
1736 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1737 memcpy (bufp, fill_literal, fill_size);
1739 for (; count > 0; count -= n_per_buf)
1741 n_per_buf = n_per_buf > count ? count : n_per_buf;
1742 x = bfd_set_section_contents
1743 (stdoutput, sec, buf, (file_ptr) offset,
1744 (bfd_size_type) n_per_buf * fill_size);
1745 if (!x)
1746 as_fatal (ngettext ("can't fill %ld byte "
1747 "in section %s of %s: '%s'",
1748 "can't fill %ld bytes "
1749 "in section %s of %s: '%s'",
1750 (long) (n_per_buf * fill_size)),
1751 (long) (n_per_buf * fill_size),
1752 bfd_section_name (sec),
1753 bfd_get_filename (stdoutput),
1754 bfd_errmsg (bfd_get_error ()));
1755 offset += n_per_buf * fill_size;
1762 static void
1763 merge_data_into_text (void)
1765 seg_info (text_section)->frchainP->frch_last->fr_next =
1766 seg_info (data_section)->frchainP->frch_root;
1767 seg_info (text_section)->frchainP->frch_last =
1768 seg_info (data_section)->frchainP->frch_last;
1769 seg_info (data_section)->frchainP = 0;
1772 static void
1773 set_symtab (void)
1775 int nsyms;
1776 asymbol **asympp;
1777 symbolS *symp;
1778 bool result;
1780 /* Count symbols. We can't rely on a count made by the loop in
1781 write_object_file, because *_frob_file may add a new symbol or
1782 two. Generate unused section symbols only if needed. */
1783 nsyms = 0;
1784 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1785 if (!symbol_removed_p (symp)
1786 && (bfd_keep_unused_section_symbols (stdoutput)
1787 || !symbol_section_p (symp)
1788 || symbol_used_in_reloc_p (symp)))
1789 nsyms++;
1791 if (nsyms)
1793 int i;
1794 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1796 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1797 symp = symbol_rootP;
1798 for (i = 0; i < nsyms; symp = symbol_next (symp))
1799 if (!symbol_removed_p (symp)
1800 && (bfd_keep_unused_section_symbols (stdoutput)
1801 || !symbol_section_p (symp)
1802 || symbol_used_in_reloc_p (symp)))
1804 asympp[i] = symbol_get_bfdsym (symp);
1805 if (asympp[i]->flags != BSF_SECTION_SYM
1806 || !(bfd_is_const_section (asympp[i]->section)
1807 && asympp[i]->section->symbol == asympp[i]))
1808 asympp[i]->flags |= BSF_KEEP;
1809 symbol_mark_written (symp);
1810 /* Include this section symbol in the symbol table. */
1811 if (symbol_section_p (symp))
1812 asympp[i]->flags |= BSF_SECTION_SYM_USED;
1813 i++;
1816 else
1817 asympp = 0;
1818 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1819 gas_assert (result);
1820 symbol_table_frozen = 1;
1823 /* Finish the subsegments. After every sub-segment, we fake an
1824 ".align ...". This conforms to BSD4.2 brain-damage. We then fake
1825 ".fill 0" because that is the kind of frag that requires least
1826 thought. ".align" frags like to have a following frag since that
1827 makes calculating their intended length trivial. */
1829 #ifndef SUB_SEGMENT_ALIGN
1830 #ifdef HANDLE_ALIGN
1831 /* The last subsegment gets an alignment corresponding to the alignment
1832 of the section. This allows proper nop-filling at the end of
1833 code-bearing sections. */
1834 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1835 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
1836 && !do_not_pad_sections_to_alignment \
1837 ? get_recorded_alignment (SEG) \
1838 : 0)
1839 #else
1840 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1841 #endif
1842 #endif
1844 static void
1845 subsegs_finish_section (asection *s)
1847 struct frchain *frchainP;
1848 segment_info_type *seginfo = seg_info (s);
1849 if (!seginfo)
1850 return;
1852 for (frchainP = seginfo->frchainP;
1853 frchainP != NULL;
1854 frchainP = frchainP->frch_next)
1856 int alignment;
1858 subseg_set (s, frchainP->frch_subseg);
1860 /* This now gets called even if we had errors. In that case,
1861 any alignment is meaningless, and, moreover, will look weird
1862 if we are generating a listing. */
1863 if (had_errors ())
1864 do_not_pad_sections_to_alignment = 1;
1866 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1867 if ((bfd_section_flags (now_seg) & SEC_MERGE)
1868 && now_seg->entsize)
1870 unsigned int entsize = now_seg->entsize;
1871 int entalign = 0;
1873 while ((entsize & 1) == 0)
1875 ++entalign;
1876 entsize >>= 1;
1879 if (entalign > alignment)
1880 alignment = entalign;
1883 if (subseg_text_p (now_seg))
1884 frag_align_code (alignment, 0);
1885 else
1886 frag_align (alignment, 0, 0);
1888 /* frag_align will have left a new frag.
1889 Use this last frag for an empty ".fill".
1891 For this segment ...
1892 Create a last frag. Do not leave a "being filled in frag". */
1893 frag_wane (frag_now);
1894 frag_now->fr_fix = 0;
1895 know (frag_now->fr_next == NULL);
1899 static void
1900 subsegs_finish (void)
1902 asection *s;
1904 for (s = stdoutput->sections; s; s = s->next)
1905 subsegs_finish_section (s);
1908 #ifdef OBJ_ELF
1909 static void
1910 create_obj_attrs_section (void)
1912 segT s;
1913 char *p;
1914 offsetT size;
1915 const char *name;
1917 size = bfd_elf_obj_attr_size (stdoutput);
1918 if (size == 0)
1919 return;
1921 name = get_elf_backend_data (stdoutput)->obj_attrs_section;
1922 if (!name)
1923 name = ".gnu.attributes";
1924 s = subseg_new (name, 0);
1925 elf_section_type (s)
1926 = get_elf_backend_data (stdoutput)->obj_attrs_section_type;
1927 bfd_set_section_flags (s, SEC_READONLY | SEC_DATA);
1928 frag_now_fix ();
1929 p = frag_more (size);
1930 bfd_elf_set_obj_attr_contents (stdoutput, (bfd_byte *)p, size);
1932 subsegs_finish_section (s);
1933 relax_segment (seg_info (s)->frchainP->frch_root, s, 0);
1934 size_seg (stdoutput, s, NULL);
1937 /* Create a relocation against an entry in a GNU Build attribute section. */
1939 static void
1940 create_note_reloc (segT sec,
1941 symbolS * sym,
1942 bfd_size_type note_offset,
1943 bfd_size_type desc2_offset,
1944 offsetT desc2_size,
1945 int reloc_type,
1946 bfd_vma addend,
1947 char * note)
1949 struct reloc_list * reloc;
1951 reloc = XNEW (struct reloc_list);
1953 /* We create a .b type reloc as resolve_reloc_expr_symbols() has already been called. */
1954 reloc->u.b.sec = sec;
1955 reloc->u.b.s = symbol_get_bfdsym (sym);
1956 reloc->u.b.r.sym_ptr_ptr = & reloc->u.b.s;
1957 reloc->u.b.r.address = note_offset + desc2_offset;
1958 reloc->u.b.r.addend = addend;
1959 reloc->u.b.r.howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1961 if (reloc->u.b.r.howto == NULL)
1963 as_bad (_("unable to create reloc for build note"));
1964 return;
1967 reloc->file = N_("<gnu build note>");
1968 reloc->line = 0;
1970 reloc->next = reloc_list;
1971 reloc_list = reloc;
1973 /* For REL relocs, store the addend in the section. */
1974 if (! sec->use_rela_p
1975 /* The SH target is a special case that uses RELA relocs
1976 but still stores the addend in the word being relocated. */
1977 || strstr (bfd_get_target (stdoutput), "-sh") != NULL)
1979 offsetT i;
1981 /* Zero out the addend, since it is now stored in the note. */
1982 reloc->u.b.r.addend = 0;
1984 if (target_big_endian)
1986 for (i = desc2_size; addend != 0 && i > 0; addend >>= 8, i--)
1987 note[desc2_offset + i - 1] = (addend & 0xff);
1989 else
1991 for (i = 0; addend != 0 && i < desc2_size; addend >>= 8, i++)
1992 note[desc2_offset + i] = (addend & 0xff);
1997 static void
1998 maybe_generate_build_notes (void)
2000 segT sec;
2001 char * note;
2002 offsetT note_size;
2003 offsetT total_size;
2004 offsetT desc_size;
2005 offsetT desc2_offset;
2006 int desc_reloc;
2007 symbolS * sym;
2008 asymbol * bsym;
2010 if (! flag_generate_build_notes
2011 || bfd_get_section_by_name (stdoutput,
2012 GNU_BUILD_ATTRS_SECTION_NAME) != NULL)
2013 return;
2015 /* Create a GNU Build Attribute section. */
2016 sec = subseg_new (GNU_BUILD_ATTRS_SECTION_NAME, false);
2017 elf_section_type (sec) = SHT_NOTE;
2018 bfd_set_section_flags (sec, (SEC_READONLY | SEC_HAS_CONTENTS | SEC_DATA
2019 | SEC_OCTETS));
2020 bfd_set_section_alignment (sec, 2);
2022 /* Work out the size of the notes that we will create,
2023 and the relocation we should use. */
2024 if (bfd_arch_bits_per_address (stdoutput) <= 32)
2026 note_size = 28;
2027 desc_size = 8; /* Two 4-byte offsets. */
2028 desc2_offset = 24;
2030 /* FIXME: The BFD backend for the CRX target does not support the
2031 BFD_RELOC_32, even though it really should. Likewise for the
2032 CR16 target. So we have special case code here... */
2033 if (strstr (bfd_get_target (stdoutput), "-crx") != NULL)
2034 desc_reloc = BFD_RELOC_CRX_NUM32;
2035 else if (strstr (bfd_get_target (stdoutput), "-cr16") != NULL)
2036 desc_reloc = BFD_RELOC_CR16_NUM32;
2037 else
2038 desc_reloc = BFD_RELOC_32;
2040 else
2042 note_size = 36;
2043 desc_size = 16; /* Two 8-byte offsets. */
2044 desc2_offset = 28;
2045 /* FIXME: The BFD backend for the IA64 target does not support the
2046 BFD_RELOC_64, even though it really should. The HPPA backend
2047 has a similar issue, although it does not support BFD_RELOCs at
2048 all! So we have special case code to handle these targets. */
2049 if (strstr (bfd_get_target (stdoutput), "-ia64") != NULL)
2050 desc_reloc = target_big_endian ? BFD_RELOC_IA64_DIR32MSB : BFD_RELOC_IA64_DIR32LSB;
2051 else if (strstr (bfd_get_target (stdoutput), "-hppa") != NULL)
2052 desc_reloc = 80; /* R_PARISC_DIR64. */
2053 else
2054 desc_reloc = BFD_RELOC_64;
2057 /* We have to create a note for *each* code section.
2058 Linker garbage collection might discard some. */
2059 total_size = 0;
2060 note = NULL;
2062 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
2063 if ((bsym = symbol_get_bfdsym (sym)) != NULL
2064 && bsym->flags & BSF_SECTION_SYM
2065 && bsym->section != NULL
2066 /* Skip linkonce sections - we cannot use these section symbols as they may disappear. */
2067 && (bsym->section->flags & (SEC_CODE | SEC_LINK_ONCE)) == SEC_CODE
2068 /* Not all linkonce sections are flagged... */
2069 && !startswith (S_GET_NAME (sym), ".gnu.linkonce"))
2071 /* Create a version note. */
2072 frag_now_fix ();
2073 note = frag_more (note_size);
2074 memset (note, 0, note_size);
2076 if (target_big_endian)
2078 note[3] = 8; /* strlen (name) + 1. */
2079 note[7] = desc_size; /* Two N-byte offsets. */
2080 note[10] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
2081 note[11] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
2083 else
2085 note[0] = 8; /* strlen (name) + 1. */
2086 note[4] = desc_size; /* Two N-byte offsets. */
2087 note[8] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
2088 note[9] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
2091 /* The a1 version number indicates that this note was
2092 generated by the assembler and not the gcc annobin plugin. */
2093 memcpy (note + 12, "GA$\x013a1", 8);
2095 /* Create a relocation to install the start address of the note... */
2096 create_note_reloc (sec, sym, total_size, 20, desc_size / 2, desc_reloc, 0, note);
2098 /* ...and another one to install the end address. */
2099 create_note_reloc (sec, sym, total_size, desc2_offset,
2100 desc_size / 2,
2101 desc_reloc,
2102 bfd_section_size (bsym->section),
2103 note);
2105 /* Mark the section symbol used in relocation so that it will be
2106 included in the symbol table. */
2107 symbol_mark_used_in_reloc (sym);
2109 total_size += note_size;
2110 /* FIXME: Maybe add a note recording the assembler command line and version ? */
2113 /* Install the note(s) into the section. */
2114 if (total_size)
2115 bfd_set_section_contents (stdoutput, sec, (bfd_byte *) note, 0, total_size);
2116 subsegs_finish_section (sec);
2117 relax_segment (seg_info (sec)->frchainP->frch_root, sec, 0);
2118 size_seg (stdoutput, sec, NULL);
2120 #endif /* OBJ_ELF */
2122 /* Write the object file. */
2124 void
2125 write_object_file (void)
2127 struct relax_seg_info rsi;
2128 #ifndef WORKING_DOT_WORD
2129 fragS *fragP; /* Track along all frags. */
2130 #endif
2132 subsegs_finish ();
2134 #ifdef md_pre_output_hook
2135 md_pre_output_hook;
2136 #endif
2138 #ifdef md_pre_relax_hook
2139 md_pre_relax_hook;
2140 #endif
2142 /* From now on, we don't care about sub-segments. Build one frag chain
2143 for each segment. Linked through fr_next. */
2145 /* Remove the sections created by gas for its own purposes. */
2147 int i;
2149 bfd_section_list_remove (stdoutput, reg_section);
2150 bfd_section_list_remove (stdoutput, expr_section);
2151 stdoutput->section_count -= 2;
2152 i = 0;
2153 bfd_map_over_sections (stdoutput, renumber_sections, &i);
2156 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
2158 /* We have two segments. If user gave -R flag, then we must put the
2159 data frags into the text segment. Do this before relaxing so
2160 we know to take advantage of -R and make shorter addresses. */
2161 if (flag_readonly_data_in_text)
2163 merge_data_into_text ();
2166 rsi.pass = 0;
2167 while (1)
2169 #ifndef WORKING_DOT_WORD
2170 /* We need to reset the markers in the broken word list and
2171 associated frags between calls to relax_segment (via
2172 relax_seg). Since the broken word list is global, we do it
2173 once per round, rather than locally in relax_segment for each
2174 segment. */
2175 struct broken_word *brokp;
2177 for (brokp = broken_words;
2178 brokp != (struct broken_word *) NULL;
2179 brokp = brokp->next_broken_word)
2181 brokp->added = 0;
2183 if (brokp->dispfrag != (fragS *) NULL
2184 && brokp->dispfrag->fr_type == rs_broken_word)
2185 brokp->dispfrag->fr_subtype = 0;
2187 #endif
2189 rsi.changed = 0;
2190 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
2191 rsi.pass++;
2192 if (!rsi.changed)
2193 break;
2196 /* Note - Most ports will use the default value of
2197 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
2198 local symbols to be resolved, removing their frag information.
2199 Some ports however, will not have finished relaxing all of
2200 their frags and will still need the local symbol frag
2201 information. These ports can set
2202 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
2203 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
2205 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
2207 /* Relaxation has completed. Freeze all syms. */
2208 finalize_syms = 1;
2210 dwarf2dbg_final_check ();
2212 #ifdef md_post_relax_hook
2213 md_post_relax_hook;
2214 #endif
2216 #ifdef OBJ_ELF
2217 if (IS_ELF)
2218 create_obj_attrs_section ();
2219 #endif
2221 #ifndef WORKING_DOT_WORD
2223 struct broken_word *lie;
2224 struct broken_word **prevP;
2226 prevP = &broken_words;
2227 for (lie = broken_words; lie; lie = lie->next_broken_word)
2228 if (!lie->added)
2230 expressionS exp;
2232 subseg_change (lie->seg, lie->subseg);
2233 exp.X_op = O_subtract;
2234 exp.X_add_symbol = lie->add;
2235 exp.X_op_symbol = lie->sub;
2236 exp.X_add_number = lie->addnum;
2237 #ifdef TC_CONS_FIX_NEW
2238 TC_CONS_FIX_NEW (lie->frag,
2239 lie->word_goes_here - lie->frag->fr_literal,
2240 2, &exp, TC_PARSE_CONS_RETURN_NONE);
2241 #else
2242 fix_new_exp (lie->frag,
2243 lie->word_goes_here - lie->frag->fr_literal,
2244 2, &exp, 0, BFD_RELOC_16);
2245 #endif
2246 *prevP = lie->next_broken_word;
2248 else
2249 prevP = &(lie->next_broken_word);
2251 for (lie = broken_words; lie;)
2253 struct broken_word *untruth;
2254 char *table_ptr;
2255 addressT table_addr;
2256 addressT from_addr, to_addr;
2257 int n, m;
2259 subseg_change (lie->seg, lie->subseg);
2260 fragP = lie->dispfrag;
2262 /* Find out how many broken_words go here. */
2263 n = 0;
2264 for (untruth = lie;
2265 untruth && untruth->dispfrag == fragP;
2266 untruth = untruth->next_broken_word)
2267 if (untruth->added == 1)
2268 n++;
2270 table_ptr = lie->dispfrag->fr_opcode;
2271 table_addr = (lie->dispfrag->fr_address
2272 + (table_ptr - lie->dispfrag->fr_literal));
2273 /* Create the jump around the long jumps. This is a short
2274 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
2275 from_addr = table_addr;
2276 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
2277 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2278 lie->add);
2279 table_ptr += md_short_jump_size;
2280 table_addr += md_short_jump_size;
2282 for (m = 0;
2283 lie && lie->dispfrag == fragP;
2284 m++, lie = lie->next_broken_word)
2286 if (lie->added == 2)
2287 continue;
2288 /* Patch the jump table. */
2289 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2290 untruth && untruth->dispfrag == fragP;
2291 untruth = untruth->next_broken_word)
2293 if (untruth->use_jump == lie)
2295 /* This is the offset from ??? to table_ptr+0.
2296 The target is the same for all users of this
2297 md_long_jump, but the "sub" bases (and hence the
2298 offsets) may be different. */
2299 addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
2300 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
2301 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
2302 #endif
2303 md_number_to_chars (untruth->word_goes_here, to_word, 2);
2307 /* Install the long jump. */
2308 /* This is a long jump from table_ptr+0 to the final target. */
2309 from_addr = table_addr;
2310 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
2311 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2312 lie->add);
2313 table_ptr += md_long_jump_size;
2314 table_addr += md_long_jump_size;
2318 #endif /* not WORKING_DOT_WORD */
2320 /* Resolve symbol values. This needs to be done before processing
2321 the relocations. */
2322 if (symbol_rootP)
2324 symbolS *symp;
2326 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2327 resolve_symbol_value (symp);
2329 resolve_local_symbol_values ();
2330 resolve_reloc_expr_symbols ();
2332 #ifdef OBJ_ELF
2333 if (IS_ELF)
2334 maybe_generate_build_notes ();
2335 #endif
2337 #ifdef tc_frob_file_before_adjust
2338 tc_frob_file_before_adjust ();
2339 #endif
2340 #ifdef obj_frob_file_before_adjust
2341 obj_frob_file_before_adjust ();
2342 #endif
2344 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
2346 #ifdef tc_frob_file_before_fix
2347 tc_frob_file_before_fix ();
2348 #endif
2349 #ifdef obj_frob_file_before_fix
2350 obj_frob_file_before_fix ();
2351 #endif
2353 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
2355 /* Set up symbol table, and write it out. */
2356 if (symbol_rootP)
2358 symbolS *symp;
2359 bool skip_next_symbol = false;
2361 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2363 int punt = 0;
2364 const char *name;
2366 if (skip_next_symbol)
2368 /* Don't do anything besides moving the value of the
2369 symbol from the GAS value-field to the BFD value-field. */
2370 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2371 skip_next_symbol = false;
2372 continue;
2375 if (symbol_mri_common_p (symp))
2377 if (S_IS_EXTERNAL (symp))
2378 as_bad (_("%s: global symbols not supported in common sections"),
2379 S_GET_NAME (symp));
2380 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2381 continue;
2384 name = S_GET_NAME (symp);
2385 if (name)
2387 const char *name2 =
2388 decode_local_label_name ((char *) S_GET_NAME (symp));
2389 /* They only differ if `name' is a fb or dollar local
2390 label name. */
2391 if (name2 != name && ! S_IS_DEFINED (symp))
2392 as_bad (_("local label `%s' is not defined"), name2);
2395 /* Do it again, because adjust_reloc_syms might introduce
2396 more symbols. They'll probably only be section symbols,
2397 but they'll still need to have the values computed. */
2398 resolve_symbol_value (symp);
2400 /* Skip symbols which were equated to undefined or common
2401 symbols. */
2402 if (symbol_equated_reloc_p (symp)
2403 || S_IS_WEAKREFR (symp))
2405 const char *sname = S_GET_NAME (symp);
2407 if (S_IS_COMMON (symp)
2408 && !TC_FAKE_LABEL (sname)
2409 && !S_IS_WEAKREFR (symp))
2411 expressionS *e = symbol_get_value_expression (symp);
2413 as_bad (_("`%s' can't be equated to common symbol `%s'"),
2414 sname, S_GET_NAME (e->X_add_symbol));
2416 if (S_GET_SEGMENT (symp) == reg_section)
2418 /* Report error only if we know the symbol name. */
2419 if (S_GET_NAME (symp) != reg_section->name)
2420 as_bad (_("can't make global register symbol `%s'"),
2421 sname);
2423 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2424 continue;
2427 #ifdef obj_frob_symbol
2428 obj_frob_symbol (symp, punt);
2429 #endif
2430 #ifdef tc_frob_symbol
2431 if (! punt || symbol_used_in_reloc_p (symp))
2432 tc_frob_symbol (symp, punt);
2433 #endif
2435 /* If we don't want to keep this symbol, splice it out of
2436 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2437 want section symbols. Otherwise, we skip local symbols
2438 and symbols that the frob_symbol macros told us to punt,
2439 but we keep such symbols if they are used in relocs. */
2440 if (symp == abs_section_sym
2441 || (! EMIT_SECTION_SYMBOLS
2442 && symbol_section_p (symp))
2443 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2444 opposites. Sometimes the former checks flags and the
2445 latter examines the name... */
2446 || (!S_IS_EXTERNAL (symp)
2447 && (punt || S_IS_LOCAL (symp) ||
2448 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
2449 && ! symbol_used_in_reloc_p (symp)))
2451 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2453 /* After symbol_remove, symbol_next(symp) still returns
2454 the one that came after it in the chain. So we don't
2455 need to do any extra cleanup work here. */
2456 continue;
2459 /* Make sure we really got a value for the symbol. */
2460 if (! symbol_resolved_p (symp))
2462 as_bad (_("can't resolve value for symbol `%s'"),
2463 S_GET_NAME (symp));
2464 symbol_mark_resolved (symp);
2467 /* Set the value into the BFD symbol. Up til now the value
2468 has only been kept in the gas symbolS struct. */
2469 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2471 /* A warning construct is a warning symbol followed by the
2472 symbol warned about. Don't let anything object-format or
2473 target-specific muck with it; it's ready for output. */
2474 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2475 skip_next_symbol = true;
2479 /* Now do any format-specific adjustments to the symbol table, such
2480 as adding file symbols. */
2481 #ifdef tc_adjust_symtab
2482 tc_adjust_symtab ();
2483 #endif
2484 #ifdef obj_adjust_symtab
2485 obj_adjust_symtab ();
2486 #endif
2488 /* Stop if there is an error. */
2489 if (!flag_always_generate_output && had_errors ())
2490 return;
2492 /* Now that all the sizes are known, and contents correct, we can
2493 start writing to the file. */
2494 set_symtab ();
2496 /* If *_frob_file changes the symbol value at this point, it is
2497 responsible for moving the changed value into symp->bsym->value
2498 as well. Hopefully all symbol value changing can be done in
2499 *_frob_symbol. */
2500 #ifdef tc_frob_file
2501 tc_frob_file ();
2502 #endif
2503 #ifdef obj_frob_file
2504 obj_frob_file ();
2505 #endif
2506 #ifdef obj_coff_generate_pdata
2507 obj_coff_generate_pdata ();
2508 #endif
2510 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2512 #ifdef tc_frob_file_after_relocs
2513 tc_frob_file_after_relocs ();
2514 #endif
2515 #ifdef obj_frob_file_after_relocs
2516 obj_frob_file_after_relocs ();
2517 #endif
2519 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
2520 if (IS_ELF && flag_use_elf_stt_common)
2521 stdoutput->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
2522 #endif
2524 /* Once all relocations have been written, we can compress the
2525 contents of the debug sections. This needs to be done before
2526 we start writing any sections, because it will affect the file
2527 layout, which is fixed once we start writing contents. */
2528 if (flag_compress_debug != COMPRESS_DEBUG_NONE)
2530 flagword flags = BFD_COMPRESS;
2531 if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
2532 flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
2533 else if (flag_compress_debug == COMPRESS_DEBUG_ZSTD)
2534 flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
2535 stdoutput->flags |= flags & bfd_applicable_file_flags (stdoutput);
2536 if ((stdoutput->flags & BFD_COMPRESS) != 0)
2537 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
2540 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2543 #ifdef TC_GENERIC_RELAX_TABLE
2544 #ifndef md_generic_table_relax_frag
2545 #define md_generic_table_relax_frag relax_frag
2546 #endif
2548 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2550 long
2551 relax_frag (segT segment, fragS *fragP, long stretch)
2553 const relax_typeS *this_type;
2554 const relax_typeS *start_type;
2555 relax_substateT next_state;
2556 relax_substateT this_state;
2557 offsetT growth;
2558 offsetT aim;
2559 addressT target;
2560 addressT address;
2561 symbolS *symbolP;
2562 const relax_typeS *table;
2564 target = fragP->fr_offset;
2565 address = fragP->fr_address + fragP->fr_fix;
2566 table = TC_GENERIC_RELAX_TABLE;
2567 this_state = fragP->fr_subtype;
2568 start_type = this_type = table + this_state;
2569 symbolP = fragP->fr_symbol;
2571 if (symbolP)
2573 fragS *sym_frag;
2575 sym_frag = symbol_get_frag (symbolP);
2577 #ifndef DIFF_EXPR_OK
2578 know (sym_frag != NULL);
2579 #endif
2580 know (S_GET_SEGMENT (symbolP) != absolute_section
2581 || sym_frag == &zero_address_frag);
2582 target += S_GET_VALUE (symbolP);
2584 /* If SYM_FRAG has yet to be reached on this pass, assume it
2585 will move by STRETCH just as we did, unless there is an
2586 alignment frag between here and SYM_FRAG. An alignment may
2587 well absorb any STRETCH, and we don't want to choose a larger
2588 branch insn by overestimating the needed reach of this
2589 branch. It isn't critical to calculate TARGET exactly; We
2590 know we'll be doing another pass if STRETCH is non-zero. */
2592 if (stretch != 0
2593 && sym_frag->relax_marker != fragP->relax_marker
2594 && S_GET_SEGMENT (symbolP) == segment)
2596 if (stretch < 0
2597 || sym_frag->region == fragP->region)
2598 target += stretch;
2599 /* If we get here we know we have a forward branch. This
2600 relax pass may have stretched previous instructions so
2601 far that omitting STRETCH would make the branch
2602 negative. Don't allow this in case the negative reach is
2603 large enough to require a larger branch instruction. */
2604 else if (target < address)
2605 return 0;
2609 aim = target - address;
2610 #ifdef TC_PCREL_ADJUST
2611 /* Currently only the ns32k and arc needs this. */
2612 aim += TC_PCREL_ADJUST (fragP);
2613 #endif
2615 #ifdef md_prepare_relax_scan
2616 /* Formerly called M68K_AIM_KLUDGE. */
2617 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2618 #endif
2620 if (aim < 0)
2622 /* Look backwards. */
2623 for (next_state = this_type->rlx_more; next_state;)
2624 if (aim >= this_type->rlx_backward)
2625 next_state = 0;
2626 else
2628 /* Grow to next state. */
2629 this_state = next_state;
2630 this_type = table + this_state;
2631 next_state = this_type->rlx_more;
2634 else
2636 /* Look forwards. */
2637 for (next_state = this_type->rlx_more; next_state;)
2638 if (aim <= this_type->rlx_forward)
2639 next_state = 0;
2640 else
2642 /* Grow to next state. */
2643 this_state = next_state;
2644 this_type = table + this_state;
2645 next_state = this_type->rlx_more;
2649 growth = this_type->rlx_length - start_type->rlx_length;
2650 if (growth != 0)
2651 fragP->fr_subtype = this_state;
2652 return growth;
2655 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2657 /* Relax_align. Advance location counter to next address that has 'alignment'
2658 lowest order bits all 0s, return size of adjustment made. */
2659 static relax_addressT
2660 relax_align (relax_addressT address, /* Address now. */
2661 int alignment /* Alignment (binary). */)
2663 relax_addressT mask;
2664 relax_addressT new_address;
2666 mask = ~((relax_addressT) ~0 << alignment);
2667 new_address = (address + mask) & (~mask);
2668 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2669 if (linkrelax)
2670 /* We must provide lots of padding, so the linker can discard it
2671 when needed. The linker will not add extra space, ever. */
2672 new_address += (1 << alignment);
2673 #endif
2674 return (new_address - address);
2677 /* Now we have a segment, not a crowd of sub-segments, we can make
2678 fr_address values.
2680 Relax the frags.
2682 After this, all frags in this segment have addresses that are correct
2683 within the segment. Since segments live in different file addresses,
2684 these frag addresses may not be the same as final object-file
2685 addresses. */
2688 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2690 unsigned long frag_count;
2691 struct frag *fragP;
2692 relax_addressT address;
2693 int region;
2694 int ret;
2696 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2697 subseg_change (segment, 0);
2699 /* For each frag in segment: count and store (a 1st guess of)
2700 fr_address. */
2701 address = 0;
2702 region = 0;
2703 for (frag_count = 0, fragP = segment_frag_root;
2704 fragP;
2705 fragP = fragP->fr_next, frag_count ++)
2707 fragP->region = region;
2708 fragP->relax_marker = 0;
2709 fragP->fr_address = address;
2710 address += fragP->fr_fix;
2712 switch (fragP->fr_type)
2714 case rs_fill:
2715 address += fragP->fr_offset * fragP->fr_var;
2716 break;
2718 case rs_align:
2719 case rs_align_code:
2720 case rs_align_test:
2722 addressT offset = relax_align (address, (int) fragP->fr_offset);
2724 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2725 offset = 0;
2727 if (offset % fragP->fr_var != 0)
2729 as_bad_where (fragP->fr_file, fragP->fr_line,
2730 ngettext ("alignment padding (%lu byte) "
2731 "not a multiple of %ld",
2732 "alignment padding (%lu bytes) "
2733 "not a multiple of %ld",
2734 (unsigned long) offset),
2735 (unsigned long) offset, (long) fragP->fr_var);
2736 offset -= (offset % fragP->fr_var);
2739 address += offset;
2740 region += 1;
2742 break;
2744 case rs_org:
2745 /* Assume .org is nugatory. It will grow with 1st relax. */
2746 region += 1;
2747 break;
2749 case rs_space:
2750 case rs_space_nop:
2751 break;
2753 case rs_machine_dependent:
2754 /* If fr_symbol is an expression, this call to
2755 resolve_symbol_value sets up the correct segment, which will
2756 likely be needed in md_estimate_size_before_relax. */
2757 if (fragP->fr_symbol)
2758 resolve_symbol_value (fragP->fr_symbol);
2760 address += md_estimate_size_before_relax (fragP, segment);
2761 break;
2763 #ifndef WORKING_DOT_WORD
2764 /* Broken words don't concern us yet. */
2765 case rs_broken_word:
2766 break;
2767 #endif
2769 case rs_leb128:
2770 /* Initial guess is always 1; doing otherwise can result in
2771 stable solutions that are larger than the minimum. */
2772 address += fragP->fr_offset = 1;
2773 break;
2775 case rs_cfa:
2776 address += eh_frame_estimate_size_before_relax (fragP);
2777 break;
2779 case rs_dwarf2dbg:
2780 address += dwarf2dbg_estimate_size_before_relax (fragP);
2781 break;
2783 case rs_sframe:
2784 /* Initial estimate can be set to atleast 1 byte. */
2785 address += sframe_estimate_size_before_relax (fragP);
2786 break;
2788 default:
2789 BAD_CASE (fragP->fr_type);
2790 break;
2794 /* Do relax(). */
2796 unsigned long max_iterations;
2798 /* Cumulative address adjustment. */
2799 offsetT stretch;
2801 /* Have we made any adjustment this pass? We can't just test
2802 stretch because one piece of code may have grown and another
2803 shrank. */
2804 int stretched;
2806 /* Most horrible, but gcc may give us some exception data that
2807 is impossible to assemble, of the form
2809 .align 4
2810 .byte 0, 0
2811 .uleb128 end - start
2812 start:
2813 .space 128*128 - 1
2814 .align 4
2815 end:
2817 If the leb128 is two bytes in size, then end-start is 128*128,
2818 which requires a three byte leb128. If the leb128 is three
2819 bytes in size, then end-start is 128*128-1, which requires a
2820 two byte leb128. We work around this dilemma by inserting
2821 an extra 4 bytes of alignment just after the .align. This
2822 works because the data after the align is accessed relative to
2823 the end label.
2825 This counter is used in a tiny state machine to detect
2826 whether a leb128 followed by an align is impossible to
2827 relax. */
2828 int rs_leb128_fudge = 0;
2830 /* We want to prevent going into an infinite loop where one frag grows
2831 depending upon the location of a symbol which is in turn moved by
2832 the growing frag. eg:
2834 foo = .
2835 .org foo+16
2836 foo = .
2838 So we dictate that this algorithm can be at most O2. */
2839 max_iterations = frag_count * frag_count;
2840 /* Check for overflow. */
2841 if (max_iterations < frag_count)
2842 max_iterations = frag_count;
2844 ret = 0;
2847 stretch = 0;
2848 stretched = 0;
2850 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2852 offsetT growth = 0;
2853 addressT was_address;
2854 offsetT offset;
2855 symbolS *symbolP;
2857 fragP->relax_marker ^= 1;
2858 was_address = fragP->fr_address;
2859 address = fragP->fr_address += stretch;
2860 symbolP = fragP->fr_symbol;
2861 offset = fragP->fr_offset;
2863 switch (fragP->fr_type)
2865 case rs_fill: /* .fill never relaxes. */
2866 growth = 0;
2867 break;
2869 #ifndef WORKING_DOT_WORD
2870 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2871 for it I do not want to write it. I do not want to have
2872 anything to do with it. This is not the proper way to
2873 implement this misfeature. */
2874 case rs_broken_word:
2876 struct broken_word *lie;
2877 struct broken_word *untruth;
2879 /* Yes this is ugly (storing the broken_word pointer
2880 in the symbol slot). Still, this whole chunk of
2881 code is ugly, and I don't feel like doing anything
2882 about it. Think of it as stubbornness in action. */
2883 growth = 0;
2884 for (lie = (struct broken_word *) (fragP->fr_symbol);
2885 lie && lie->dispfrag == fragP;
2886 lie = lie->next_broken_word)
2889 if (lie->added)
2890 continue;
2892 offset = (S_GET_VALUE (lie->add)
2893 + lie->addnum
2894 - S_GET_VALUE (lie->sub));
2895 if (offset <= -32768 || offset >= 32767)
2897 if (flag_warn_displacement)
2899 char buf[50];
2901 bfd_sprintf_vma (stdoutput, buf,
2902 (addressT) lie->addnum);
2903 as_warn_where (fragP->fr_file, fragP->fr_line,
2904 _(".word %s-%s+%s didn't fit"),
2905 S_GET_NAME (lie->add),
2906 S_GET_NAME (lie->sub),
2907 buf);
2909 if (fragP->fr_subtype == 0)
2911 fragP->fr_subtype++;
2912 growth += md_short_jump_size;
2915 /* Redirect *all* words of this table with the same
2916 target, lest we have to handle the case where the
2917 same target but with a offset that fits on this
2918 round overflows at the next relaxation round. */
2919 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2920 untruth && untruth->dispfrag == lie->dispfrag;
2921 untruth = untruth->next_broken_word)
2922 if ((symbol_get_frag (untruth->add)
2923 == symbol_get_frag (lie->add))
2924 && (S_GET_VALUE (untruth->add)
2925 == S_GET_VALUE (lie->add)))
2927 untruth->added = 2;
2928 untruth->use_jump = lie;
2931 lie->added = 1;
2932 growth += md_long_jump_size;
2936 break;
2937 } /* case rs_broken_word */
2938 #endif
2939 case rs_align:
2940 case rs_align_code:
2941 case rs_align_test:
2943 addressT oldoff, newoff;
2945 oldoff = relax_align (was_address + fragP->fr_fix,
2946 (int) offset);
2947 newoff = relax_align (address + fragP->fr_fix,
2948 (int) offset);
2950 if (fragP->fr_subtype != 0)
2952 if (oldoff > fragP->fr_subtype)
2953 oldoff = 0;
2954 if (newoff > fragP->fr_subtype)
2955 newoff = 0;
2958 growth = newoff - oldoff;
2960 /* If this align happens to follow a leb128 and
2961 we have determined that the leb128 is bouncing
2962 in size, then break the cycle by inserting an
2963 extra alignment. */
2964 if (growth < 0
2965 && (rs_leb128_fudge & 16) != 0
2966 && (rs_leb128_fudge & 15) >= 2)
2968 segment_info_type *seginfo = seg_info (segment);
2969 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2970 struct frag *newf;
2972 newf = frag_alloc (ob);
2973 obstack_blank_fast (ob, fragP->fr_var);
2974 obstack_finish (ob);
2975 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2976 memcpy (newf->fr_literal,
2977 fragP->fr_literal + fragP->fr_fix,
2978 fragP->fr_var);
2979 newf->fr_type = rs_fill;
2980 newf->fr_address = address + fragP->fr_fix + newoff;
2981 newf->fr_fix = 0;
2982 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2983 / fragP->fr_var);
2984 if (newf->fr_offset * newf->fr_var
2985 != (offsetT) 1 << fragP->fr_offset)
2987 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2988 newf->fr_var = 1;
2990 /* Include size of new frag in GROWTH. */
2991 growth += newf->fr_offset * newf->fr_var;
2992 /* Adjust the new frag address for the amount
2993 we'll add when we process the new frag. */
2994 newf->fr_address -= stretch + growth;
2995 newf->relax_marker ^= 1;
2996 fragP->fr_next = newf;
2997 #ifdef DEBUG
2998 as_warn (_("padding added"));
2999 #endif
3002 break;
3004 case rs_org:
3006 offsetT target = offset;
3007 addressT after;
3009 if (symbolP)
3011 /* Convert from an actual address to an octet offset
3012 into the section. Here it is assumed that the
3013 section's VMA is zero, and can omit subtracting it
3014 from the symbol's value to get the address offset. */
3015 know (S_GET_SEGMENT (symbolP)->vma == 0);
3016 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
3019 know (fragP->fr_next);
3020 after = fragP->fr_next->fr_address + stretch;
3021 growth = target - after;
3023 /* Growth may be negative, but variable part of frag
3024 cannot have fewer than 0 chars. That is, we can't
3025 .org backwards. */
3026 if ((offsetT) (address + fragP->fr_fix) > target)
3028 growth = 0;
3030 /* Don't error on first few frag relax passes.
3031 The symbol might be an expression involving
3032 symbol values from other sections. If those
3033 sections have not yet been processed their
3034 frags will all have zero addresses, so we
3035 will calculate incorrect values for them. The
3036 number of passes we allow before giving an
3037 error is somewhat arbitrary. It should be at
3038 least one, with larger values requiring
3039 increasingly contrived dependencies between
3040 frags to trigger a false error. */
3041 if (pass < 2)
3043 /* Force another pass. */
3044 ret = 1;
3045 break;
3048 as_bad_where (fragP->fr_file, fragP->fr_line,
3049 _("attempt to move .org backwards"));
3051 /* We've issued an error message. Change the
3052 frag to avoid cascading errors. */
3053 fragP->fr_type = rs_align;
3054 fragP->fr_subtype = 0;
3055 fragP->fr_offset = 0;
3056 fragP->fr_fix = after - address;
3059 break;
3061 case rs_space:
3062 case rs_space_nop:
3063 growth = 0;
3064 if (symbolP)
3066 offsetT amount;
3068 amount = S_GET_VALUE (symbolP);
3069 if (S_GET_SEGMENT (symbolP) != absolute_section
3070 || S_IS_COMMON (symbolP)
3071 || ! S_IS_DEFINED (symbolP))
3073 as_bad_where (fragP->fr_file, fragP->fr_line,
3074 _(".space, .nops or .fill specifies non-absolute value"));
3075 /* Prevent repeat of this error message. */
3076 fragP->fr_symbol = 0;
3078 else if (amount < 0)
3080 /* Don't error on first few frag relax passes.
3081 See rs_org comment for a longer explanation. */
3082 if (pass < 2)
3084 ret = 1;
3085 break;
3088 as_warn_where (fragP->fr_file, fragP->fr_line,
3089 _(".space, .nops or .fill with negative value, ignored"));
3090 fragP->fr_symbol = 0;
3092 else
3093 growth = (was_address + fragP->fr_fix + amount
3094 - fragP->fr_next->fr_address);
3096 break;
3098 case rs_machine_dependent:
3099 #ifdef md_relax_frag
3100 growth = md_relax_frag (segment, fragP, stretch);
3101 #else
3102 #ifdef TC_GENERIC_RELAX_TABLE
3103 /* The default way to relax a frag is to look through
3104 TC_GENERIC_RELAX_TABLE. */
3105 growth = md_generic_table_relax_frag (segment, fragP,
3106 stretch);
3107 #endif /* TC_GENERIC_RELAX_TABLE */
3108 #endif
3109 break;
3111 case rs_leb128:
3113 valueT value;
3114 offsetT size;
3116 value = resolve_symbol_value (fragP->fr_symbol);
3117 size = sizeof_leb128 (value, fragP->fr_subtype);
3118 growth = size - fragP->fr_offset;
3119 fragP->fr_offset = size;
3121 break;
3123 case rs_cfa:
3124 growth = eh_frame_relax_frag (fragP);
3125 break;
3127 case rs_dwarf2dbg:
3128 growth = dwarf2dbg_relax_frag (fragP);
3129 break;
3131 case rs_sframe:
3132 growth = sframe_relax_frag (fragP);
3133 break;
3135 default:
3136 BAD_CASE (fragP->fr_type);
3137 break;
3139 if (growth)
3141 stretch += growth;
3142 stretched = 1;
3143 if (fragP->fr_type == rs_leb128)
3144 rs_leb128_fudge += 16;
3145 else if (fragP->fr_type == rs_align
3146 && (rs_leb128_fudge & 16) != 0
3147 && stretch == 0)
3148 rs_leb128_fudge += 16;
3149 else
3150 rs_leb128_fudge = 0;
3154 if (stretch == 0
3155 && (rs_leb128_fudge & 16) == 0
3156 && (rs_leb128_fudge & -16) != 0)
3157 rs_leb128_fudge += 1;
3158 else
3159 rs_leb128_fudge = 0;
3161 /* Until nothing further to relax. */
3162 while (stretched && -- max_iterations);
3164 if (stretched)
3165 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
3166 segment_name (segment));
3169 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
3170 if (fragP->last_fr_address != fragP->fr_address)
3172 fragP->last_fr_address = fragP->fr_address;
3173 ret = 1;
3175 return ret;
3178 void
3179 number_to_chars_bigendian (char *buf, valueT val, int n)
3181 if (n <= 0)
3182 abort ();
3183 while (n--)
3185 buf[n] = val & 0xff;
3186 val >>= 8;
3190 void
3191 number_to_chars_littleendian (char *buf, valueT val, int n)
3193 if (n <= 0)
3194 abort ();
3195 while (n--)
3197 *buf++ = val & 0xff;
3198 val >>= 8;
3202 void
3203 write_print_statistics (FILE *file)
3205 fprintf (file, "fixups: %d\n", n_fixups);
3208 /* For debugging. */
3209 extern int indent_level;
3211 void
3212 print_fixup (fixS *fixp)
3214 indent_level = 1;
3215 fprintf (stderr, "fix %p %s:%d", fixp, fixp->fx_file, fixp->fx_line);
3216 if (fixp->fx_pcrel)
3217 fprintf (stderr, " pcrel");
3218 if (fixp->fx_pcrel_adjust)
3219 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
3220 if (fixp->fx_tcbit)
3221 fprintf (stderr, " tcbit");
3222 if (fixp->fx_done)
3223 fprintf (stderr, " done");
3224 fprintf (stderr, "\n size=%d frag=%p", fixp->fx_size, fixp->fx_frag);
3225 fprintf (stderr, " where=%ld offset=%" PRIx64 " addnumber=%" PRIx64,
3226 fixp->fx_where, (uint64_t) fixp->fx_offset,
3227 (uint64_t) fixp->fx_addnumber);
3228 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
3229 fixp->fx_r_type);
3230 if (fixp->fx_addsy)
3232 fprintf (stderr, "\n +<");
3233 print_symbol_value_1 (stderr, fixp->fx_addsy);
3234 fprintf (stderr, ">");
3236 if (fixp->fx_subsy)
3238 fprintf (stderr, "\n -<");
3239 print_symbol_value_1 (stderr, fixp->fx_subsy);
3240 fprintf (stderr, ">");
3242 fprintf (stderr, "\n");
3243 #ifdef TC_FIX_DATA_PRINT
3244 TC_FIX_DATA_PRINT (stderr, fixp);
3245 #endif