Update year range in copyright notice of binutils files
[binutils-gdb.git] / gas / write.c
blob98ead0b991c99875e266c2aef149ba466697335c
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 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_add:
259 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
260 the difference expression cannot immediately be reduced. */
262 symbolS *stmp = make_expr_symbol (exp);
264 exp->X_op = O_symbol;
265 exp->X_op_symbol = 0;
266 exp->X_add_symbol = stmp;
267 exp->X_add_number = 0;
269 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
272 case O_symbol_rva:
273 add = exp->X_add_symbol;
274 off = exp->X_add_number;
275 r_type = BFD_RELOC_RVA;
276 break;
278 case O_uminus:
279 sub = exp->X_add_symbol;
280 off = exp->X_add_number;
281 break;
283 case O_subtract:
284 sub = exp->X_op_symbol;
285 /* Fall through. */
286 case O_symbol:
287 add = exp->X_add_symbol;
288 /* Fall through. */
289 case O_constant:
290 off = exp->X_add_number;
291 break;
293 default:
294 add = make_expr_symbol (exp);
295 break;
298 return fix_new_internal (frag, where, size, add, sub, off, pcrel,
299 r_type, false);
302 /* Create a fixup at the beginning of FRAG. The arguments are the same
303 as for fix_new, except that WHERE is implicitly 0. */
305 fixS *
306 fix_at_start (fragS *frag, unsigned long size, symbolS *add_symbol,
307 offsetT offset, int pcrel, RELOC_ENUM r_type)
309 return fix_new_internal (frag, 0, size, add_symbol,
310 (symbolS *) NULL, offset, pcrel, r_type, true);
313 /* Generic function to determine whether a fixup requires a relocation. */
315 generic_force_reloc (fixS *fix)
317 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
318 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
319 return 1;
321 if (fix->fx_addsy == NULL)
322 return 0;
324 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
327 /* Append a string onto another string, bumping the pointer along. */
328 void
329 append (char **charPP, char *fromP, unsigned long length)
331 /* Don't trust memcpy() of 0 chars. */
332 if (length == 0)
333 return;
335 memcpy (*charPP, fromP, length);
336 *charPP += length;
339 /* This routine records the largest alignment seen for each segment.
340 If the beginning of the segment is aligned on the worst-case
341 boundary, all of the other alignments within it will work. At
342 least one object format really uses this info. */
344 void
345 record_alignment (/* Segment to which alignment pertains. */
346 segT seg,
347 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
348 boundary, 2 => 4-byte boundary, etc.) */
349 unsigned int align)
351 if (seg == absolute_section)
352 return;
354 if (align > bfd_section_alignment (seg))
355 bfd_set_section_alignment (seg, align);
359 get_recorded_alignment (segT seg)
361 if (seg == absolute_section)
362 return 0;
364 return bfd_section_alignment (seg);
367 /* Reset the section indices after removing the gas created sections. */
369 static void
370 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
372 int *countp = (int *) countparg;
374 sec->index = *countp;
375 ++*countp;
378 static fragS *
379 chain_frchains_together_1 (segT section, struct frchain *frchp)
381 fragS dummy, *prev_frag = &dummy;
382 fixS fix_dummy, *prev_fix = &fix_dummy;
386 prev_frag->fr_next = frchp->frch_root;
387 prev_frag = frchp->frch_last;
388 gas_assert (prev_frag->fr_type != 0);
389 if (frchp->fix_root != (fixS *) NULL)
391 if (seg_info (section)->fix_root == (fixS *) NULL)
392 seg_info (section)->fix_root = frchp->fix_root;
393 prev_fix->fx_next = frchp->fix_root;
394 seg_info (section)->fix_tail = frchp->fix_tail;
395 prev_fix = frchp->fix_tail;
397 frchp = frchp->frch_next;
398 } while (frchp);
399 gas_assert (prev_frag != &dummy
400 && prev_frag->fr_type != 0);
401 prev_frag->fr_next = 0;
402 return prev_frag;
405 static void
406 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
407 segT section,
408 void *xxx ATTRIBUTE_UNUSED)
410 segment_info_type *info;
412 /* BFD may have introduced its own sections without using
413 subseg_new, so it is possible that seg_info is NULL. */
414 info = seg_info (section);
415 if (info != (segment_info_type *) NULL)
416 info->frchainP->frch_last
417 = chain_frchains_together_1 (section, info->frchainP);
419 /* Now that we've chained the frags together, we must add new fixups
420 to the segment, not to the frag chain. */
421 frags_chained = true;
424 static void
425 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
427 switch (fragP->fr_type)
429 case rs_space_nop:
430 goto skip_align;
431 case rs_align:
432 case rs_align_code:
433 case rs_align_test:
434 case rs_org:
435 case rs_space:
436 #ifdef HANDLE_ALIGN
437 HANDLE_ALIGN (fragP);
438 #endif
439 skip_align:
440 know (fragP->fr_next != NULL);
441 fragP->fr_offset = (fragP->fr_next->fr_address
442 - fragP->fr_address
443 - fragP->fr_fix) / fragP->fr_var;
444 if (fragP->fr_offset < 0)
446 as_bad_where (fragP->fr_file, fragP->fr_line,
447 _("attempt to .org/.space/.nops backwards? (%ld)"),
448 (long) fragP->fr_offset);
449 fragP->fr_offset = 0;
451 if (fragP->fr_type == rs_space_nop)
452 fragP->fr_type = rs_fill_nop;
453 else
454 fragP->fr_type = rs_fill;
455 break;
457 case rs_fill:
458 case rs_fill_nop:
459 break;
461 case rs_leb128:
463 valueT value = S_GET_VALUE (fragP->fr_symbol);
464 int size;
466 if (!S_IS_DEFINED (fragP->fr_symbol))
468 as_bad_where (fragP->fr_file, fragP->fr_line,
469 _("leb128 operand is an undefined symbol: %s"),
470 S_GET_NAME (fragP->fr_symbol));
473 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
474 fragP->fr_subtype);
476 fragP->fr_fix += size;
477 fragP->fr_type = rs_fill;
478 fragP->fr_var = 0;
479 fragP->fr_offset = 0;
480 fragP->fr_symbol = NULL;
482 break;
484 case rs_cfa:
485 eh_frame_convert_frag (fragP);
486 break;
488 case rs_dwarf2dbg:
489 dwarf2dbg_convert_frag (fragP);
490 break;
492 case rs_sframe:
493 sframe_convert_frag (fragP);
494 break;
496 case rs_machine_dependent:
497 md_convert_frag (stdoutput, sec, fragP);
499 gas_assert (fragP->fr_next == NULL
500 || (fragP->fr_next->fr_address - fragP->fr_address
501 == fragP->fr_fix));
503 /* After md_convert_frag, we make the frag into a ".space 0".
504 md_convert_frag() should set up any fixSs and constants
505 required. */
506 frag_wane (fragP);
507 break;
509 #ifndef WORKING_DOT_WORD
510 case rs_broken_word:
512 struct broken_word *lie;
514 if (fragP->fr_subtype)
516 fragP->fr_fix += md_short_jump_size;
517 for (lie = (struct broken_word *) (fragP->fr_symbol);
518 lie && lie->dispfrag == fragP;
519 lie = lie->next_broken_word)
520 if (lie->added == 1)
521 fragP->fr_fix += md_long_jump_size;
523 frag_wane (fragP);
525 break;
526 #endif
528 default:
529 BAD_CASE (fragP->fr_type);
530 break;
532 #ifdef md_frag_check
533 md_frag_check (fragP);
534 #endif
537 struct relax_seg_info
539 int pass;
540 int changed;
543 static void
544 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
546 segment_info_type *seginfo = seg_info (sec);
547 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
549 if (seginfo && seginfo->frchainP
550 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
551 info->changed = 1;
554 static void
555 size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
557 flagword flags;
558 fragS *fragp;
559 segment_info_type *seginfo;
560 int x;
561 valueT size, newsize;
563 subseg_change (sec, 0);
565 seginfo = seg_info (sec);
566 if (seginfo && seginfo->frchainP)
568 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
569 cvt_frag_to_fill (sec, fragp);
570 for (fragp = seginfo->frchainP->frch_root;
571 fragp->fr_next;
572 fragp = fragp->fr_next)
573 /* Walk to last elt. */
575 size = fragp->fr_address + fragp->fr_fix;
577 else
578 size = 0;
580 flags = bfd_section_flags (sec);
581 if (size == 0 && bfd_section_size (sec) != 0 &&
582 (flags & SEC_HAS_CONTENTS) != 0)
583 return;
585 if (size > 0 && ! seginfo->bss)
586 flags |= SEC_HAS_CONTENTS;
588 x = bfd_set_section_flags (sec, flags);
589 gas_assert (x);
591 /* If permitted, allow the backend to pad out the section
592 to some alignment boundary. */
593 if (do_not_pad_sections_to_alignment)
594 newsize = size;
595 else
596 newsize = md_section_align (sec, size);
597 x = bfd_set_section_size (sec, newsize);
598 gas_assert (x);
600 /* If the size had to be rounded up, add some padding in the last
601 non-empty frag. */
602 gas_assert (newsize >= size);
603 if (size != newsize)
605 fragS *last = seginfo->frchainP->frch_last;
606 fragp = seginfo->frchainP->frch_root;
607 while (fragp->fr_next != last)
608 fragp = fragp->fr_next;
609 last->fr_address = size;
610 if ((newsize - size) % fragp->fr_var == 0)
611 fragp->fr_offset += (newsize - size) / fragp->fr_var;
612 else
613 /* If we hit this abort, it's likely due to subsegs_finish not
614 providing sufficient alignment on the last frag, and the
615 machine dependent code using alignment frags with fr_var
616 greater than 1. */
617 abort ();
620 #ifdef tc_frob_section
621 tc_frob_section (sec);
622 #endif
623 #ifdef obj_frob_section
624 obj_frob_section (sec);
625 #endif
628 #ifdef DEBUG2
629 static void
630 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
632 segment_info_type *seginfo = seg_info (sec);
633 fixS *fixp = seginfo->fix_root;
635 if (!fixp)
636 return;
638 fprintf (stream, "sec %s relocs:\n", sec->name);
639 while (fixp)
641 symbolS *s = fixp->fx_addsy;
643 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
644 (int) fixp->fx_r_type);
645 if (s == NULL)
646 fprintf (stream, "no sym\n");
647 else
649 print_symbol_value_1 (stream, s);
650 fprintf (stream, "\n");
652 fixp = fixp->fx_next;
655 #else
656 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
657 #endif
659 #ifndef EMIT_SECTION_SYMBOLS
660 #define EMIT_SECTION_SYMBOLS 1
661 #endif
663 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
664 and check for validity. Convert RELOC_LIST from using U.A fields
665 to U.B fields. */
666 static void
667 resolve_reloc_expr_symbols (void)
669 bfd_vma addr_mask = 1;
670 struct reloc_list *r;
672 /* Avoid a shift by the width of type. */
673 addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1;
674 addr_mask <<= 1;
675 addr_mask -= 1;
677 for (r = reloc_list; r; r = r->next)
679 reloc_howto_type *howto = r->u.a.howto;
680 expressionS *symval;
681 symbolS *sym;
682 bfd_vma offset, addend;
683 asection *sec;
685 resolve_symbol_value (r->u.a.offset_sym);
686 symval = symbol_get_value_expression (r->u.a.offset_sym);
688 offset = 0;
689 sym = NULL;
690 if (symval->X_op == O_constant)
691 sym = r->u.a.offset_sym;
692 else if (symval->X_op == O_symbol)
694 sym = symval->X_add_symbol;
695 offset = symval->X_add_number;
696 symval = symbol_get_value_expression (symval->X_add_symbol);
698 if (sym == NULL
699 || symval->X_op != O_constant
700 || (sec = S_GET_SEGMENT (sym)) == NULL
701 || !SEG_NORMAL (sec))
703 as_bad_where (r->file, r->line, _("invalid offset expression"));
704 sec = NULL;
706 else
707 offset += S_GET_VALUE (sym);
709 sym = NULL;
710 addend = r->u.a.addend;
711 if (r->u.a.sym != NULL)
713 resolve_symbol_value (r->u.a.sym);
714 symval = symbol_get_value_expression (r->u.a.sym);
715 if (symval->X_op == O_constant)
716 sym = r->u.a.sym;
717 else if (symval->X_op == O_symbol)
719 sym = symval->X_add_symbol;
720 addend += symval->X_add_number;
721 symval = symbol_get_value_expression (symval->X_add_symbol);
723 if (symval->X_op != O_constant)
725 as_bad_where (r->file, r->line, _("invalid reloc expression"));
726 sec = NULL;
728 else if (sym != NULL && sec != NULL)
730 /* Convert relocs against local symbols to refer to the
731 corresponding section symbol plus offset instead. Keep
732 PC-relative relocs of the REL variety intact though to
733 prevent the offset from overflowing the relocated field,
734 unless it has enough bits to cover the whole address
735 space. */
736 if (S_IS_LOCAL (sym)
737 && S_IS_DEFINED (sym)
738 && !symbol_section_p (sym)
739 && (sec->use_rela_p
740 || (howto->partial_inplace
741 && (!howto->pc_relative
742 || howto->src_mask == addr_mask))))
744 asection *symsec = S_GET_SEGMENT (sym);
745 if (!(((symsec->flags & SEC_MERGE) != 0
746 && addend != 0)
747 || (symsec->flags & SEC_THREAD_LOCAL) != 0))
749 addend += S_GET_VALUE (sym);
750 sym = section_symbol (symsec);
753 symbol_mark_used_in_reloc (sym);
756 if (sym == NULL)
758 if (abs_section_sym == NULL)
759 abs_section_sym = section_symbol (absolute_section);
760 sym = abs_section_sym;
763 r->u.b.sec = sec;
764 r->u.b.s = symbol_get_bfdsym (sym);
765 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
766 r->u.b.r.address = offset;
767 r->u.b.r.addend = addend;
768 r->u.b.r.howto = howto;
772 /* This pass over fixups decides whether symbols can be replaced with
773 section symbols. */
775 static void
776 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
777 asection *sec,
778 void *xxx ATTRIBUTE_UNUSED)
780 segment_info_type *seginfo = seg_info (sec);
781 fixS *fixp;
783 if (seginfo == NULL)
784 return;
786 dump_section_relocs (abfd, sec, stderr);
788 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
789 if (fixp->fx_done)
790 /* Ignore it. */
792 else if (fixp->fx_addsy)
794 symbolS *sym;
795 asection *symsec;
797 #ifdef DEBUG5
798 fprintf (stderr, "\n\nadjusting fixup:\n");
799 print_fixup (fixp);
800 #endif
802 sym = fixp->fx_addsy;
804 /* All symbols should have already been resolved at this
805 point. It is possible to see unresolved expression
806 symbols, though, since they are not in the regular symbol
807 table. */
808 resolve_symbol_value (sym);
810 if (fixp->fx_subsy != NULL)
811 resolve_symbol_value (fixp->fx_subsy);
813 /* If this symbol is equated to an undefined or common symbol,
814 convert the fixup to being against that symbol. */
815 while (symbol_equated_reloc_p (sym)
816 || S_IS_WEAKREFR (sym))
818 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
819 if (sym == newsym)
820 break;
821 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
822 fixp->fx_addsy = newsym;
823 sym = newsym;
826 if (symbol_mri_common_p (sym))
828 fixp->fx_offset += S_GET_VALUE (sym);
829 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
830 continue;
833 /* If the symbol is undefined, common, weak, or global (ELF
834 shared libs), we can't replace it with the section symbol. */
835 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
836 continue;
838 /* Is there some other (target cpu dependent) reason we can't adjust
839 this one? (E.g. relocations involving function addresses on
840 the PA. */
841 #ifdef tc_fix_adjustable
842 if (! tc_fix_adjustable (fixp))
843 continue;
844 #endif
846 /* Since we're reducing to section symbols, don't attempt to reduce
847 anything that's already using one. */
848 if (symbol_section_p (sym))
850 /* Mark the section symbol used in relocation so that it will
851 be included in the symbol table. */
852 symbol_mark_used_in_reloc (sym);
853 continue;
856 symsec = S_GET_SEGMENT (sym);
857 if (symsec == NULL)
858 abort ();
860 if (bfd_is_abs_section (symsec)
861 || symsec == reg_section)
863 /* The fixup_segment routine normally will not use this
864 symbol in a relocation. */
865 continue;
868 /* Don't try to reduce relocs which refer to non-local symbols
869 in .linkonce sections. It can lead to confusion when a
870 debugging section refers to a .linkonce section. I hope
871 this will always be correct. */
872 if (symsec != sec && ! S_IS_LOCAL (sym))
874 if ((symsec->flags & SEC_LINK_ONCE) != 0
875 || (IS_ELF
876 /* The GNU toolchain uses an extension for ELF: a
877 section beginning with the magic string
878 .gnu.linkonce is a linkonce section. */
879 && startswith (segment_name (symsec), ".gnu.linkonce")))
880 continue;
883 /* Never adjust a reloc against local symbol in a merge section
884 with non-zero addend. */
885 if ((symsec->flags & SEC_MERGE) != 0
886 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
887 continue;
889 /* Never adjust a reloc against TLS local symbol. */
890 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
891 continue;
893 /* We refetch the segment when calling section_symbol, rather
894 than using symsec, because S_GET_VALUE may wind up changing
895 the section when it calls resolve_symbol_value. */
896 fixp->fx_offset += S_GET_VALUE (sym);
897 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
898 #ifdef DEBUG5
899 fprintf (stderr, "\nadjusted fixup:\n");
900 print_fixup (fixp);
901 #endif
904 dump_section_relocs (abfd, sec, stderr);
907 void
908 as_bad_subtract (fixS *fixp)
910 as_bad_where (fixp->fx_file, fixp->fx_line,
911 _("can't resolve %s - %s"),
912 fixp->fx_addsy ? S_GET_NAME (fixp->fx_addsy) : "0",
913 S_GET_NAME (fixp->fx_subsy));
916 /* fixup_segment()
918 Go through all the fixS's in a segment and see which ones can be
919 handled now. (These consist of fixS where we have since discovered
920 the value of a symbol, or the address of the frag involved.)
921 For each one, call md_apply_fix to put the fix into the frag data.
922 Ones that we couldn't completely handle here will be output later
923 by emit_relocations. */
925 static void
926 fixup_segment (fixS *fixP, segT this_segment)
928 valueT add_number;
929 fragS *fragP;
931 if (fixP != NULL && abs_section_sym == NULL)
932 abs_section_sym = section_symbol (absolute_section);
934 /* If the linker is doing the relaxing, we must not do any fixups.
936 Well, strictly speaking that's not true -- we could do any that
937 are PC-relative and don't cross regions that could change size. */
938 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
940 for (; fixP; fixP = fixP->fx_next)
941 if (!fixP->fx_done)
943 if (fixP->fx_addsy == NULL)
945 /* There was no symbol required by this relocation.
946 However, BFD doesn't really handle relocations
947 without symbols well. So fake up a local symbol in
948 the absolute section. */
949 fixP->fx_addsy = abs_section_sym;
951 symbol_mark_used_in_reloc (fixP->fx_addsy);
952 if (fixP->fx_subsy != NULL)
953 symbol_mark_used_in_reloc (fixP->fx_subsy);
955 return;
958 for (; fixP; fixP = fixP->fx_next)
960 segT add_symbol_segment = absolute_section;
962 #ifdef DEBUG5
963 fprintf (stderr, "\nprocessing fixup:\n");
964 print_fixup (fixP);
965 #endif
967 fragP = fixP->fx_frag;
968 know (fragP);
969 #ifdef TC_VALIDATE_FIX
970 TC_VALIDATE_FIX (fixP, this_segment, skip);
971 #endif
972 add_number = fixP->fx_offset;
974 if (fixP->fx_addsy != NULL)
975 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
977 if (fixP->fx_subsy != NULL)
979 segT sub_symbol_segment;
981 resolve_symbol_value (fixP->fx_subsy);
982 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
984 if (fixP->fx_addsy != NULL
985 && sub_symbol_segment == add_symbol_segment
986 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
987 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
988 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
990 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
991 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
992 fixP->fx_offset = add_number;
993 fixP->fx_addsy = NULL;
994 fixP->fx_subsy = NULL;
995 #ifdef TC_M68K
996 /* See the comment below about 68k weirdness. */
997 fixP->fx_pcrel = 0;
998 #endif
1000 else if (sub_symbol_segment == absolute_section
1001 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
1002 && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
1004 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1005 fixP->fx_offset = add_number;
1006 fixP->fx_subsy = NULL;
1008 else if (sub_symbol_segment == this_segment
1009 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
1010 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
1012 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1013 fixP->fx_offset = (add_number + fixP->fx_dot_value
1014 + fixP->fx_dot_frag->fr_address);
1016 /* Make it pc-relative. If the back-end code has not
1017 selected a pc-relative reloc, cancel the adjustment
1018 we do later on all pc-relative relocs. */
1019 if (0
1020 #ifdef TC_M68K
1021 /* Do this for m68k even if it's already described
1022 as pc-relative. On the m68k, an operand of
1023 "pc@(foo-.-2)" should address "foo" in a
1024 pc-relative mode. */
1025 || 1
1026 #endif
1027 || !fixP->fx_pcrel)
1028 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
1029 fixP->fx_subsy = NULL;
1030 fixP->fx_pcrel = 1;
1032 else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
1034 if (!md_register_arithmetic
1035 && (add_symbol_segment == reg_section
1036 || sub_symbol_segment == reg_section))
1037 as_bad_where (fixP->fx_file, fixP->fx_line,
1038 _("register value used as expression"));
1039 else
1040 as_bad_subtract (fixP);
1042 else if (sub_symbol_segment != undefined_section
1043 && ! bfd_is_com_section (sub_symbol_segment)
1044 && MD_APPLY_SYM_VALUE (fixP))
1045 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1048 if (fixP->fx_addsy)
1050 if (add_symbol_segment == this_segment
1051 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1052 && !TC_FORCE_RELOCATION_LOCAL (fixP))
1054 /* This fixup was made when the symbol's segment was
1055 SEG_UNKNOWN, but it is now in the local segment.
1056 So we know how to do the address without relocation. */
1057 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1058 fixP->fx_offset = add_number;
1059 if (fixP->fx_pcrel)
1060 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1061 fixP->fx_addsy = NULL;
1062 fixP->fx_pcrel = 0;
1064 else if (add_symbol_segment == absolute_section
1065 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1066 && !TC_FORCE_RELOCATION_ABS (fixP))
1068 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1069 fixP->fx_offset = add_number;
1070 fixP->fx_addsy = NULL;
1072 else if (add_symbol_segment != undefined_section
1073 && ! bfd_is_com_section (add_symbol_segment)
1074 && MD_APPLY_SYM_VALUE (fixP))
1075 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1078 if (fixP->fx_pcrel)
1080 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1081 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1083 /* There was no symbol required by this relocation.
1084 However, BFD doesn't really handle relocations
1085 without symbols well. So fake up a local symbol in
1086 the absolute section. */
1087 fixP->fx_addsy = abs_section_sym;
1091 if (!fixP->fx_done)
1092 md_apply_fix (fixP, &add_number, this_segment);
1094 if (!fixP->fx_done)
1096 if (fixP->fx_addsy == NULL)
1097 fixP->fx_addsy = abs_section_sym;
1098 symbol_mark_used_in_reloc (fixP->fx_addsy);
1099 if (fixP->fx_subsy != NULL)
1100 symbol_mark_used_in_reloc (fixP->fx_subsy);
1103 if (!fixP->fx_no_overflow && fixP->fx_size != 0)
1105 if (fixP->fx_size < sizeof (valueT))
1107 valueT mask;
1109 mask = 0;
1110 mask--; /* Set all bits to one. */
1111 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1112 if ((add_number & mask) != 0
1113 && (fixP->fx_signed
1114 ? (add_number & mask) != mask
1115 : (-add_number & mask) != 0))
1117 char buf[50], buf2[50];
1118 bfd_sprintf_vma (stdoutput, buf, fragP->fr_address + fixP->fx_where);
1119 if (add_number > 1000)
1120 bfd_sprintf_vma (stdoutput, buf2, add_number);
1121 else
1122 sprintf (buf2, "%ld", (long) add_number);
1123 as_bad_where (fixP->fx_file, fixP->fx_line,
1124 ngettext ("value of %s too large for field "
1125 "of %d byte at %s",
1126 "value of %s too large for field "
1127 "of %d bytes at %s",
1128 fixP->fx_size),
1129 buf2, fixP->fx_size, buf);
1130 } /* Generic error checking. */
1132 #ifdef WARN_SIGNED_OVERFLOW_WORD
1133 /* Warn if a .word value is too large when treated as a signed
1134 number. We already know it is not too negative. This is to
1135 catch over-large switches generated by gcc on the 68k. */
1136 if (!flag_signed_overflow_ok
1137 && fixP->fx_size == 2
1138 && add_number > 0x7fff)
1139 as_bad_where (fixP->fx_file, fixP->fx_line,
1140 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1141 (long) add_number,
1142 (long) (fragP->fr_address + fixP->fx_where));
1143 #endif
1146 #ifdef TC_VALIDATE_FIX
1147 skip: ATTRIBUTE_UNUSED_LABEL
1149 #endif
1150 #ifdef DEBUG5
1151 fprintf (stderr, "result:\n");
1152 print_fixup (fixP);
1153 #endif
1154 } /* For each fixS in this segment. */
1157 static void
1158 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1159 asection *sec,
1160 void *xxx ATTRIBUTE_UNUSED)
1162 segment_info_type *seginfo = seg_info (sec);
1164 fixup_segment (seginfo->fix_root, sec);
1167 static void
1168 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1169 const char *file, unsigned int line)
1171 char *err;
1172 bfd_reloc_status_type s;
1173 asymbol *sym;
1175 if (reloc->sym_ptr_ptr != NULL
1176 && (sym = *reloc->sym_ptr_ptr) != NULL
1177 && (sym->flags & BSF_KEEP) == 0
1178 && ((sym->flags & BSF_SECTION_SYM) == 0
1179 || (EMIT_SECTION_SYMBOLS
1180 && !bfd_is_abs_section (sym->section))))
1181 as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
1183 s = bfd_install_relocation (stdoutput, reloc,
1184 fragp->fr_literal, fragp->fr_address,
1185 sec, &err);
1186 switch (s)
1188 case bfd_reloc_ok:
1189 break;
1190 case bfd_reloc_overflow:
1191 as_bad_where (file, line, _("relocation overflow"));
1192 break;
1193 case bfd_reloc_outofrange:
1194 as_bad_where (file, line, _("relocation out of range"));
1195 break;
1196 default:
1197 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1198 file, line, s);
1202 static fragS *
1203 get_frag_for_reloc (fragS *last_frag,
1204 const segment_info_type *seginfo,
1205 const struct reloc_list *r)
1207 fragS *f;
1209 for (f = last_frag; f != NULL; f = f->fr_next)
1210 if (f->fr_address <= r->u.b.r.address
1211 && r->u.b.r.address < f->fr_address + f->fr_fix)
1212 return f;
1214 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1215 if (f->fr_address <= r->u.b.r.address
1216 && r->u.b.r.address < f->fr_address + f->fr_fix)
1217 return f;
1219 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1220 if (f->fr_address <= r->u.b.r.address
1221 && r->u.b.r.address <= f->fr_address + f->fr_fix)
1222 return f;
1224 as_bad_where (r->file, r->line,
1225 _("reloc not within (fixed part of) section"));
1226 return NULL;
1229 static void
1230 write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1231 void *xxx ATTRIBUTE_UNUSED)
1233 segment_info_type *seginfo = seg_info (sec);
1234 unsigned int n;
1235 struct reloc_list *my_reloc_list, **rp, *r;
1236 arelent **relocs;
1237 fixS *fixp;
1238 fragS *last_frag;
1240 /* If seginfo is NULL, we did not create this section; don't do
1241 anything with it. */
1242 if (seginfo == NULL)
1243 return;
1245 n = 0;
1246 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1247 if (!fixp->fx_done)
1248 n++;
1250 #ifdef RELOC_EXPANSION_POSSIBLE
1251 n *= MAX_RELOC_EXPANSION;
1252 #endif
1254 /* Extract relocs for this section from reloc_list. */
1255 rp = &reloc_list;
1257 my_reloc_list = NULL;
1258 while ((r = *rp) != NULL)
1260 if (r->u.b.sec == sec)
1262 *rp = r->next;
1263 r->next = my_reloc_list;
1264 my_reloc_list = r;
1265 n++;
1267 else
1268 rp = &r->next;
1271 relocs = XCNEWVEC (arelent *, n);
1273 n = 0;
1274 r = my_reloc_list;
1275 last_frag = NULL;
1276 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1278 int fx_size, slack;
1279 valueT loc;
1280 arelent **reloc;
1281 #ifndef RELOC_EXPANSION_POSSIBLE
1282 arelent *rel;
1284 reloc = &rel;
1285 #endif
1287 if (fixp->fx_done)
1288 continue;
1290 fx_size = fixp->fx_size;
1291 slack = TC_FX_SIZE_SLACK (fixp);
1292 if (slack > 0)
1293 fx_size = fx_size > slack ? fx_size - slack : 0;
1294 loc = fixp->fx_where + fx_size;
1295 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1296 as_bad_where (fixp->fx_file, fixp->fx_line,
1297 _("internal error: fixup not contained within frag"));
1299 #ifdef obj_fixup_removed_symbol
1300 if (fixp->fx_addsy && symbol_removed_p (fixp->fx_addsy))
1301 obj_fixup_removed_symbol (&fixp->fx_addsy);
1302 if (fixp->fx_subsy && symbol_removed_p (fixp->fx_subsy))
1303 obj_fixup_removed_symbol (&fixp->fx_subsy);
1304 #endif
1306 #ifndef RELOC_EXPANSION_POSSIBLE
1307 *reloc = tc_gen_reloc (sec, fixp);
1308 #else
1309 reloc = tc_gen_reloc (sec, fixp);
1310 #endif
1312 while (*reloc)
1314 while (r != NULL && r->u.b.r.address < (*reloc)->address)
1316 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1317 if (f != NULL)
1319 last_frag = f;
1320 relocs[n++] = &r->u.b.r;
1321 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1323 r = r->next;
1325 #ifdef GAS_SORT_RELOCS
1326 if (n != 0 && (*reloc)->address < relocs[n - 1]->address)
1328 size_t lo = 0;
1329 size_t hi = n - 1;
1330 bfd_vma look = (*reloc)->address;
1331 while (lo < hi)
1333 size_t mid = (lo + hi) / 2;
1334 if (relocs[mid]->address > look)
1335 hi = mid;
1336 else
1338 lo = mid + 1;
1339 if (relocs[mid]->address == look)
1340 break;
1343 while (lo < hi && relocs[lo]->address == look)
1344 lo++;
1345 memmove (relocs + lo + 1, relocs + lo,
1346 (n - lo) * sizeof (*relocs));
1347 n++;
1348 relocs[lo] = *reloc;
1350 else
1351 #endif
1352 relocs[n++] = *reloc;
1353 install_reloc (sec, *reloc, fixp->fx_frag,
1354 fixp->fx_file, fixp->fx_line);
1355 #ifndef RELOC_EXPANSION_POSSIBLE
1356 break;
1357 #else
1358 reloc++;
1359 #endif
1363 while (r != NULL)
1365 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1366 if (f != NULL)
1368 last_frag = f;
1369 relocs[n++] = &r->u.b.r;
1370 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1372 r = r->next;
1375 #ifdef DEBUG4
1377 unsigned int k, j, nsyms;
1378 asymbol **sympp;
1379 sympp = bfd_get_outsymbols (stdoutput);
1380 nsyms = bfd_get_symcount (stdoutput);
1381 for (k = 0; k < n; k++)
1382 if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1384 for (j = 0; j < nsyms; j++)
1385 if (sympp[j] == *relocs[k]->sym_ptr_ptr)
1386 break;
1387 if (j == nsyms)
1388 abort ();
1391 #endif
1393 bfd_set_reloc (stdoutput, sec, n ? relocs : NULL, n);
1395 #ifdef SET_SECTION_RELOCS
1396 SET_SECTION_RELOCS (sec, relocs, n);
1397 #endif
1399 #ifdef DEBUG3
1401 unsigned int k;
1403 fprintf (stderr, "relocs for sec %s\n", sec->name);
1404 for (k = 0; k < n; k++)
1406 arelent *rel = relocs[k];
1407 asymbol *s = *rel->sym_ptr_ptr;
1408 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1409 k, rel, (unsigned long)rel->address, s->name,
1410 (unsigned long)rel->addend);
1413 #endif
1416 static int
1417 compress_frag (bool use_zstd, void *ctx, const char *contents, int in_size,
1418 fragS **last_newf, struct obstack *ob)
1420 int out_size;
1421 int total_out_size = 0;
1422 fragS *f = *last_newf;
1423 char *next_out;
1424 int avail_out;
1426 /* Call the compression routine repeatedly until it has finished
1427 processing the frag. */
1428 while (in_size > 0)
1430 /* Reserve all the space available in the current chunk.
1431 If none is available, start a new frag. */
1432 avail_out = obstack_room (ob);
1433 if (avail_out <= 0)
1435 obstack_finish (ob);
1436 f = frag_alloc (ob);
1437 f->fr_type = rs_fill;
1438 (*last_newf)->fr_next = f;
1439 *last_newf = f;
1440 avail_out = obstack_room (ob);
1442 if (avail_out <= 0)
1443 as_fatal (_("can't extend frag"));
1444 next_out = obstack_next_free (ob);
1445 obstack_blank_fast (ob, avail_out);
1446 out_size = compress_data (use_zstd, ctx, &contents, &in_size, &next_out,
1447 &avail_out);
1448 if (out_size < 0)
1449 return -1;
1451 f->fr_fix += out_size;
1452 total_out_size += out_size;
1454 /* Return unused space. */
1455 if (avail_out > 0)
1456 obstack_blank_fast (ob, -avail_out);
1459 return total_out_size;
1462 static void
1463 compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1465 segment_info_type *seginfo = seg_info (sec);
1466 bfd_size_type uncompressed_size = sec->size;
1467 flagword flags = bfd_section_flags (sec);
1469 if (seginfo == NULL
1470 || uncompressed_size < 32
1471 || (flags & SEC_HAS_CONTENTS) == 0)
1472 return;
1474 const char *section_name = bfd_section_name (sec);
1475 if (!startswith (section_name, ".debug_")
1476 && !startswith (section_name, ".gnu.debuglto_.debug_")
1477 && !startswith (section_name, ".gnu.linkonce.wi."))
1478 return;
1480 bool use_zstd = abfd->flags & BFD_COMPRESS_ZSTD;
1481 void *ctx = compress_init (use_zstd);
1482 if (ctx == NULL)
1483 return;
1485 unsigned int header_size;
1486 if ((abfd->flags & BFD_COMPRESS_GABI) == 0)
1487 header_size = 12;
1488 else
1489 header_size = bfd_get_compression_header_size (stdoutput, NULL);
1491 /* Create a new frag to contain the compression header. */
1492 struct obstack *ob = &seginfo->frchainP->frch_obstack;
1493 fragS *first_newf = frag_alloc (ob);
1494 if (obstack_room (ob) < header_size)
1495 first_newf = frag_alloc (ob);
1496 if (obstack_room (ob) < header_size)
1497 as_fatal (ngettext ("can't extend frag %lu char",
1498 "can't extend frag %lu chars",
1499 (unsigned long) header_size),
1500 (unsigned long) header_size);
1501 fragS *last_newf = first_newf;
1502 obstack_blank_fast (ob, header_size);
1503 last_newf->fr_type = rs_fill;
1504 last_newf->fr_fix = header_size;
1505 char *header = last_newf->fr_literal;
1506 bfd_size_type compressed_size = header_size;
1508 /* Stream the frags through the compression engine, adding new frags
1509 as necessary to accommodate the compressed output. */
1510 for (fragS *f = seginfo->frchainP->frch_root;
1512 f = f->fr_next)
1514 offsetT fill_size;
1515 char *fill_literal;
1516 offsetT count;
1517 int out_size;
1519 gas_assert (f->fr_type == rs_fill);
1520 if (f->fr_fix)
1522 out_size = compress_frag (use_zstd, ctx, f->fr_literal, f->fr_fix,
1523 &last_newf, ob);
1524 if (out_size < 0)
1525 return;
1526 compressed_size += out_size;
1528 fill_literal = f->fr_literal + f->fr_fix;
1529 fill_size = f->fr_var;
1530 count = f->fr_offset;
1531 gas_assert (count >= 0);
1532 if (fill_size && count)
1534 while (count--)
1536 out_size = compress_frag (use_zstd, ctx, fill_literal,
1537 (int)fill_size, &last_newf, ob);
1538 if (out_size < 0)
1539 return;
1540 compressed_size += out_size;
1545 /* Flush the compression state. */
1546 for (;;)
1548 int avail_out;
1549 char *next_out;
1550 int out_size;
1552 /* Reserve all the space available in the current chunk.
1553 If none is available, start a new frag. */
1554 avail_out = obstack_room (ob);
1555 if (avail_out <= 0)
1557 fragS *newf;
1559 obstack_finish (ob);
1560 newf = frag_alloc (ob);
1561 newf->fr_type = rs_fill;
1562 last_newf->fr_next = newf;
1563 last_newf = newf;
1564 avail_out = obstack_room (ob);
1566 if (avail_out <= 0)
1567 as_fatal (_("can't extend frag"));
1568 next_out = obstack_next_free (ob);
1569 obstack_blank_fast (ob, avail_out);
1570 int x = compress_finish (use_zstd, ctx, &next_out, &avail_out, &out_size);
1571 if (x < 0)
1572 return;
1574 last_newf->fr_fix += out_size;
1575 compressed_size += out_size;
1577 /* Return unused space. */
1578 if (avail_out > 0)
1579 obstack_blank_fast (ob, -avail_out);
1581 if (x == 0)
1582 break;
1585 /* PR binutils/18087: If compression didn't make the section smaller,
1586 just keep it uncompressed. */
1587 if (compressed_size >= uncompressed_size)
1588 return;
1590 /* Replace the uncompressed frag list with the compressed frag list. */
1591 seginfo->frchainP->frch_root = first_newf;
1592 seginfo->frchainP->frch_last = last_newf;
1594 /* Update the section size and its name. */
1595 bfd_update_compression_header (abfd, (bfd_byte *) header, sec);
1596 bool x = bfd_set_section_size (sec, compressed_size);
1597 gas_assert (x);
1598 if ((abfd->flags & BFD_COMPRESS_GABI) == 0
1599 && section_name[1] == 'd')
1601 char *compressed_name = bfd_debug_name_to_zdebug (abfd, section_name);
1602 bfd_rename_section (sec, compressed_name);
1606 #ifndef md_generate_nops
1607 /* Genenerate COUNT bytes of no-op instructions to WHERE. A target
1608 backend must override this with proper no-op instructions. */
1610 static void
1611 md_generate_nops (fragS *f ATTRIBUTE_UNUSED,
1612 char *where ATTRIBUTE_UNUSED,
1613 offsetT count ATTRIBUTE_UNUSED,
1614 int control ATTRIBUTE_UNUSED)
1616 as_bad (_("unimplemented .nops directive"));
1618 #endif
1620 static void
1621 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1622 asection *sec,
1623 void *xxx ATTRIBUTE_UNUSED)
1625 segment_info_type *seginfo = seg_info (sec);
1626 addressT offset = 0;
1627 fragS *f;
1629 /* Write out the frags. */
1630 if (seginfo == NULL
1631 || !(bfd_section_flags (sec) & SEC_HAS_CONTENTS))
1632 return;
1634 for (f = seginfo->frchainP->frch_root;
1636 f = f->fr_next)
1638 int x;
1639 addressT fill_size;
1640 char *fill_literal;
1641 offsetT count;
1643 gas_assert (f->fr_type == rs_fill || f->fr_type == rs_fill_nop);
1644 if (f->fr_fix)
1646 x = bfd_set_section_contents (stdoutput, sec,
1647 f->fr_literal, (file_ptr) offset,
1648 (bfd_size_type) f->fr_fix);
1649 if (!x)
1650 as_fatal (ngettext ("can't write %ld byte "
1651 "to section %s of %s: '%s'",
1652 "can't write %ld bytes "
1653 "to section %s of %s: '%s'",
1654 (long) f->fr_fix),
1655 (long) f->fr_fix,
1656 bfd_section_name (sec), bfd_get_filename (stdoutput),
1657 bfd_errmsg (bfd_get_error ()));
1658 offset += f->fr_fix;
1661 fill_size = f->fr_var;
1662 count = f->fr_offset;
1663 fill_literal = f->fr_literal + f->fr_fix;
1665 if (f->fr_type == rs_fill_nop)
1667 gas_assert (count >= 0 && fill_size == 1);
1668 if (count > 0)
1670 char *buf = xmalloc (count);
1671 md_generate_nops (f, buf, count, *fill_literal);
1672 x = bfd_set_section_contents
1673 (stdoutput, sec, buf, (file_ptr) offset,
1674 (bfd_size_type) count);
1675 if (!x)
1676 as_fatal (ngettext ("can't fill %ld byte "
1677 "in section %s of %s: '%s'",
1678 "can't fill %ld bytes "
1679 "in section %s of %s: '%s'",
1680 (long) count),
1681 (long) count,
1682 bfd_section_name (sec),
1683 bfd_get_filename (stdoutput),
1684 bfd_errmsg (bfd_get_error ()));
1685 offset += count;
1686 #ifndef NO_LISTING
1687 if (listing & LISTING_LISTING)
1688 f->fr_opcode = buf;
1689 else
1690 #endif
1691 free (buf);
1693 continue;
1696 gas_assert (count >= 0);
1697 if (fill_size && count)
1699 char buf[256];
1700 if (fill_size > sizeof (buf))
1702 /* Do it the old way. Can this ever happen? */
1703 while (count--)
1705 x = bfd_set_section_contents (stdoutput, sec,
1706 fill_literal,
1707 (file_ptr) offset,
1708 (bfd_size_type) fill_size);
1709 if (!x)
1710 as_fatal (ngettext ("can't fill %ld byte "
1711 "in section %s of %s: '%s'",
1712 "can't fill %ld bytes "
1713 "in section %s of %s: '%s'",
1714 (long) fill_size),
1715 (long) fill_size,
1716 bfd_section_name (sec),
1717 bfd_get_filename (stdoutput),
1718 bfd_errmsg (bfd_get_error ()));
1719 offset += fill_size;
1722 else
1724 /* Build a buffer full of fill objects and output it as
1725 often as necessary. This saves on the overhead of
1726 potentially lots of bfd_set_section_contents calls. */
1727 int n_per_buf, i;
1728 if (fill_size == 1)
1730 n_per_buf = sizeof (buf);
1731 memset (buf, *fill_literal, n_per_buf);
1733 else
1735 char *bufp;
1736 n_per_buf = sizeof (buf) / fill_size;
1737 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1738 memcpy (bufp, fill_literal, fill_size);
1740 for (; count > 0; count -= n_per_buf)
1742 n_per_buf = n_per_buf > count ? count : n_per_buf;
1743 x = bfd_set_section_contents
1744 (stdoutput, sec, buf, (file_ptr) offset,
1745 (bfd_size_type) n_per_buf * fill_size);
1746 if (!x)
1747 as_fatal (ngettext ("can't fill %ld byte "
1748 "in section %s of %s: '%s'",
1749 "can't fill %ld bytes "
1750 "in section %s of %s: '%s'",
1751 (long) (n_per_buf * fill_size)),
1752 (long) (n_per_buf * fill_size),
1753 bfd_section_name (sec),
1754 bfd_get_filename (stdoutput),
1755 bfd_errmsg (bfd_get_error ()));
1756 offset += n_per_buf * fill_size;
1763 static void
1764 merge_data_into_text (void)
1766 seg_info (text_section)->frchainP->frch_last->fr_next =
1767 seg_info (data_section)->frchainP->frch_root;
1768 seg_info (text_section)->frchainP->frch_last =
1769 seg_info (data_section)->frchainP->frch_last;
1770 seg_info (data_section)->frchainP = 0;
1773 static void
1774 set_symtab (void)
1776 int nsyms;
1777 asymbol **asympp;
1778 symbolS *symp;
1779 bool result;
1781 /* Count symbols. We can't rely on a count made by the loop in
1782 write_object_file, because *_frob_file may add a new symbol or
1783 two. Generate unused section symbols only if needed. */
1784 nsyms = 0;
1785 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1786 if (!symbol_removed_p (symp)
1787 && (bfd_keep_unused_section_symbols (stdoutput)
1788 || !symbol_section_p (symp)
1789 || symbol_used_in_reloc_p (symp)))
1790 nsyms++;
1792 if (nsyms)
1794 int i;
1795 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1797 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1798 symp = symbol_rootP;
1799 for (i = 0; i < nsyms; symp = symbol_next (symp))
1800 if (!symbol_removed_p (symp)
1801 && (bfd_keep_unused_section_symbols (stdoutput)
1802 || !symbol_section_p (symp)
1803 || symbol_used_in_reloc_p (symp)))
1805 asympp[i] = symbol_get_bfdsym (symp);
1806 if (asympp[i]->flags != BSF_SECTION_SYM
1807 || !(bfd_is_const_section (asympp[i]->section)
1808 && asympp[i]->section->symbol == asympp[i]))
1809 asympp[i]->flags |= BSF_KEEP;
1810 symbol_mark_written (symp);
1811 /* Include this section symbol in the symbol table. */
1812 if (symbol_section_p (symp))
1813 asympp[i]->flags |= BSF_SECTION_SYM_USED;
1814 i++;
1817 else
1818 asympp = 0;
1819 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1820 gas_assert (result);
1821 symbol_table_frozen = 1;
1824 /* Finish the subsegments. After every sub-segment, we fake an
1825 ".align ...". This conforms to BSD4.2 brain-damage. We then fake
1826 ".fill 0" because that is the kind of frag that requires least
1827 thought. ".align" frags like to have a following frag since that
1828 makes calculating their intended length trivial. */
1830 #ifndef SUB_SEGMENT_ALIGN
1831 #ifdef HANDLE_ALIGN
1832 /* The last subsegment gets an alignment corresponding to the alignment
1833 of the section. This allows proper nop-filling at the end of
1834 code-bearing sections. */
1835 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1836 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
1837 && !do_not_pad_sections_to_alignment \
1838 ? get_recorded_alignment (SEG) \
1839 : 0)
1840 #else
1841 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1842 #endif
1843 #endif
1845 static void
1846 subsegs_finish_section (asection *s)
1848 struct frchain *frchainP;
1849 segment_info_type *seginfo = seg_info (s);
1850 if (!seginfo)
1851 return;
1853 for (frchainP = seginfo->frchainP;
1854 frchainP != NULL;
1855 frchainP = frchainP->frch_next)
1857 int alignment;
1859 subseg_set (s, frchainP->frch_subseg);
1861 /* This now gets called even if we had errors. In that case,
1862 any alignment is meaningless, and, moreover, will look weird
1863 if we are generating a listing. */
1864 if (had_errors ())
1865 do_not_pad_sections_to_alignment = 1;
1867 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1868 if ((bfd_section_flags (now_seg) & SEC_MERGE)
1869 && now_seg->entsize)
1871 unsigned int entsize = now_seg->entsize;
1872 int entalign = 0;
1874 while ((entsize & 1) == 0)
1876 ++entalign;
1877 entsize >>= 1;
1880 if (entalign > alignment)
1881 alignment = entalign;
1884 if (subseg_text_p (now_seg))
1885 frag_align_code (alignment, 0);
1886 else
1887 frag_align (alignment, 0, 0);
1889 /* frag_align will have left a new frag.
1890 Use this last frag for an empty ".fill".
1892 For this segment ...
1893 Create a last frag. Do not leave a "being filled in frag". */
1894 frag_wane (frag_now);
1895 frag_now->fr_fix = 0;
1896 know (frag_now->fr_next == NULL);
1900 static void
1901 subsegs_finish (void)
1903 asection *s;
1905 for (s = stdoutput->sections; s; s = s->next)
1906 subsegs_finish_section (s);
1909 #ifdef OBJ_ELF
1910 static void
1911 create_obj_attrs_section (void)
1913 segT s;
1914 char *p;
1915 offsetT size;
1916 const char *name;
1918 size = bfd_elf_obj_attr_size (stdoutput);
1919 if (size == 0)
1920 return;
1922 name = get_elf_backend_data (stdoutput)->obj_attrs_section;
1923 if (!name)
1924 name = ".gnu.attributes";
1925 s = subseg_new (name, 0);
1926 elf_section_type (s)
1927 = get_elf_backend_data (stdoutput)->obj_attrs_section_type;
1928 bfd_set_section_flags (s, SEC_READONLY | SEC_DATA);
1929 frag_now_fix ();
1930 p = frag_more (size);
1931 bfd_elf_set_obj_attr_contents (stdoutput, (bfd_byte *)p, size);
1933 subsegs_finish_section (s);
1934 relax_segment (seg_info (s)->frchainP->frch_root, s, 0);
1935 size_seg (stdoutput, s, NULL);
1938 /* Create a relocation against an entry in a GNU Build attribute section. */
1940 static void
1941 create_note_reloc (segT sec,
1942 symbolS * sym,
1943 bfd_size_type note_offset,
1944 bfd_size_type desc2_offset,
1945 offsetT desc2_size,
1946 int reloc_type,
1947 bfd_vma addend,
1948 char * note)
1950 struct reloc_list * reloc;
1952 reloc = XNEW (struct reloc_list);
1954 /* We create a .b type reloc as resolve_reloc_expr_symbols() has already been called. */
1955 reloc->u.b.sec = sec;
1956 reloc->u.b.s = symbol_get_bfdsym (sym);
1957 reloc->u.b.r.sym_ptr_ptr = & reloc->u.b.s;
1958 reloc->u.b.r.address = note_offset + desc2_offset;
1959 reloc->u.b.r.addend = addend;
1960 reloc->u.b.r.howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1962 if (reloc->u.b.r.howto == NULL)
1964 as_bad (_("unable to create reloc for build note"));
1965 return;
1968 reloc->file = N_("<gnu build note>");
1969 reloc->line = 0;
1971 reloc->next = reloc_list;
1972 reloc_list = reloc;
1974 /* For REL relocs, store the addend in the section. */
1975 if (! sec->use_rela_p
1976 /* The SH target is a special case that uses RELA relocs
1977 but still stores the addend in the word being relocated. */
1978 || strstr (bfd_get_target (stdoutput), "-sh") != NULL)
1980 offsetT i;
1982 /* Zero out the addend, since it is now stored in the note. */
1983 reloc->u.b.r.addend = 0;
1985 if (target_big_endian)
1987 for (i = desc2_size; addend != 0 && i > 0; addend >>= 8, i--)
1988 note[desc2_offset + i - 1] = (addend & 0xff);
1990 else
1992 for (i = 0; addend != 0 && i < desc2_size; addend >>= 8, i++)
1993 note[desc2_offset + i] = (addend & 0xff);
1998 static void
1999 maybe_generate_build_notes (void)
2001 segT sec;
2002 char * note;
2003 offsetT note_size;
2004 offsetT total_size;
2005 offsetT desc_size;
2006 offsetT desc2_offset;
2007 int desc_reloc;
2008 symbolS * sym;
2009 asymbol * bsym;
2011 if (! flag_generate_build_notes
2012 || bfd_get_section_by_name (stdoutput,
2013 GNU_BUILD_ATTRS_SECTION_NAME) != NULL)
2014 return;
2016 /* Create a GNU Build Attribute section. */
2017 sec = subseg_new (GNU_BUILD_ATTRS_SECTION_NAME, false);
2018 elf_section_type (sec) = SHT_NOTE;
2019 bfd_set_section_flags (sec, (SEC_READONLY | SEC_HAS_CONTENTS | SEC_DATA
2020 | SEC_OCTETS));
2021 bfd_set_section_alignment (sec, 2);
2023 /* Work out the size of the notes that we will create,
2024 and the relocation we should use. */
2025 if (bfd_arch_bits_per_address (stdoutput) <= 32)
2027 note_size = 28;
2028 desc_size = 8; /* Two 4-byte offsets. */
2029 desc2_offset = 24;
2031 /* FIXME: The BFD backend for the CRX target does not support the
2032 BFD_RELOC_32, even though it really should. Likewise for the
2033 CR16 target. So we have special case code here... */
2034 if (strstr (bfd_get_target (stdoutput), "-crx") != NULL)
2035 desc_reloc = BFD_RELOC_CRX_NUM32;
2036 else if (strstr (bfd_get_target (stdoutput), "-cr16") != NULL)
2037 desc_reloc = BFD_RELOC_CR16_NUM32;
2038 else
2039 desc_reloc = BFD_RELOC_32;
2041 else
2043 note_size = 36;
2044 desc_size = 16; /* Two 8-byte offsets. */
2045 desc2_offset = 28;
2046 /* FIXME: The BFD backend for the IA64 target does not support the
2047 BFD_RELOC_64, even though it really should. The HPPA backend
2048 has a similar issue, although it does not support BFD_RELOCs at
2049 all! So we have special case code to handle these targets. */
2050 if (strstr (bfd_get_target (stdoutput), "-ia64") != NULL)
2051 desc_reloc = target_big_endian ? BFD_RELOC_IA64_DIR32MSB : BFD_RELOC_IA64_DIR32LSB;
2052 else if (strstr (bfd_get_target (stdoutput), "-hppa") != NULL)
2053 desc_reloc = 80; /* R_PARISC_DIR64. */
2054 else
2055 desc_reloc = BFD_RELOC_64;
2058 /* We have to create a note for *each* code section.
2059 Linker garbage collection might discard some. */
2060 total_size = 0;
2061 note = NULL;
2063 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
2064 if ((bsym = symbol_get_bfdsym (sym)) != NULL
2065 && bsym->flags & BSF_SECTION_SYM
2066 && bsym->section != NULL
2067 /* Skip linkonce sections - we cannot use these section symbols as they may disappear. */
2068 && (bsym->section->flags & (SEC_CODE | SEC_LINK_ONCE)) == SEC_CODE
2069 /* Not all linkonce sections are flagged... */
2070 && !startswith (S_GET_NAME (sym), ".gnu.linkonce"))
2072 /* Create a version note. */
2073 frag_now_fix ();
2074 note = frag_more (note_size);
2075 memset (note, 0, note_size);
2077 if (target_big_endian)
2079 note[3] = 8; /* strlen (name) + 1. */
2080 note[7] = desc_size; /* Two N-byte offsets. */
2081 note[10] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
2082 note[11] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
2084 else
2086 note[0] = 8; /* strlen (name) + 1. */
2087 note[4] = desc_size; /* Two N-byte offsets. */
2088 note[8] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
2089 note[9] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
2092 /* The a1 version number indicates that this note was
2093 generated by the assembler and not the gcc annobin plugin. */
2094 memcpy (note + 12, "GA$\x013a1", 8);
2096 /* Create a relocation to install the start address of the note... */
2097 create_note_reloc (sec, sym, total_size, 20, desc_size / 2, desc_reloc, 0, note);
2099 /* ...and another one to install the end address. */
2100 create_note_reloc (sec, sym, total_size, desc2_offset,
2101 desc_size / 2,
2102 desc_reloc,
2103 bfd_section_size (bsym->section),
2104 note);
2106 /* Mark the section symbol used in relocation so that it will be
2107 included in the symbol table. */
2108 symbol_mark_used_in_reloc (sym);
2110 total_size += note_size;
2111 /* FIXME: Maybe add a note recording the assembler command line and version ? */
2114 /* Install the note(s) into the section. */
2115 if (total_size)
2116 bfd_set_section_contents (stdoutput, sec, (bfd_byte *) note, 0, total_size);
2117 subsegs_finish_section (sec);
2118 relax_segment (seg_info (sec)->frchainP->frch_root, sec, 0);
2119 size_seg (stdoutput, sec, NULL);
2121 #endif /* OBJ_ELF */
2123 /* Write the object file. */
2125 void
2126 write_object_file (void)
2128 struct relax_seg_info rsi;
2129 #ifndef WORKING_DOT_WORD
2130 fragS *fragP; /* Track along all frags. */
2131 #endif
2133 subsegs_finish ();
2135 #ifdef md_pre_output_hook
2136 md_pre_output_hook;
2137 #endif
2139 #ifdef md_pre_relax_hook
2140 md_pre_relax_hook;
2141 #endif
2143 /* From now on, we don't care about sub-segments. Build one frag chain
2144 for each segment. Linked through fr_next. */
2146 /* Remove the sections created by gas for its own purposes. */
2148 int i;
2150 bfd_section_list_remove (stdoutput, reg_section);
2151 bfd_section_list_remove (stdoutput, expr_section);
2152 stdoutput->section_count -= 2;
2153 i = 0;
2154 bfd_map_over_sections (stdoutput, renumber_sections, &i);
2157 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
2159 /* We have two segments. If user gave -R flag, then we must put the
2160 data frags into the text segment. Do this before relaxing so
2161 we know to take advantage of -R and make shorter addresses. */
2162 if (flag_readonly_data_in_text)
2164 merge_data_into_text ();
2167 rsi.pass = 0;
2168 while (1)
2170 #ifndef WORKING_DOT_WORD
2171 /* We need to reset the markers in the broken word list and
2172 associated frags between calls to relax_segment (via
2173 relax_seg). Since the broken word list is global, we do it
2174 once per round, rather than locally in relax_segment for each
2175 segment. */
2176 struct broken_word *brokp;
2178 for (brokp = broken_words;
2179 brokp != (struct broken_word *) NULL;
2180 brokp = brokp->next_broken_word)
2182 brokp->added = 0;
2184 if (brokp->dispfrag != (fragS *) NULL
2185 && brokp->dispfrag->fr_type == rs_broken_word)
2186 brokp->dispfrag->fr_subtype = 0;
2188 #endif
2190 rsi.changed = 0;
2191 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
2192 rsi.pass++;
2193 if (!rsi.changed)
2194 break;
2197 /* Note - Most ports will use the default value of
2198 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
2199 local symbols to be resolved, removing their frag information.
2200 Some ports however, will not have finished relaxing all of
2201 their frags and will still need the local symbol frag
2202 information. These ports can set
2203 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
2204 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
2206 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
2208 /* Relaxation has completed. Freeze all syms. */
2209 finalize_syms = 1;
2211 dwarf2dbg_final_check ();
2213 #ifdef md_post_relax_hook
2214 md_post_relax_hook;
2215 #endif
2217 #ifdef OBJ_ELF
2218 if (IS_ELF)
2219 create_obj_attrs_section ();
2220 #endif
2222 #ifndef WORKING_DOT_WORD
2224 struct broken_word *lie;
2225 struct broken_word **prevP;
2227 prevP = &broken_words;
2228 for (lie = broken_words; lie; lie = lie->next_broken_word)
2229 if (!lie->added)
2231 expressionS exp;
2233 subseg_change (lie->seg, lie->subseg);
2234 exp.X_op = O_subtract;
2235 exp.X_add_symbol = lie->add;
2236 exp.X_op_symbol = lie->sub;
2237 exp.X_add_number = lie->addnum;
2238 #ifdef TC_CONS_FIX_NEW
2239 TC_CONS_FIX_NEW (lie->frag,
2240 lie->word_goes_here - lie->frag->fr_literal,
2241 2, &exp, TC_PARSE_CONS_RETURN_NONE);
2242 #else
2243 fix_new_exp (lie->frag,
2244 lie->word_goes_here - lie->frag->fr_literal,
2245 2, &exp, 0, BFD_RELOC_16);
2246 #endif
2247 *prevP = lie->next_broken_word;
2249 else
2250 prevP = &(lie->next_broken_word);
2252 for (lie = broken_words; lie;)
2254 struct broken_word *untruth;
2255 char *table_ptr;
2256 addressT table_addr;
2257 addressT from_addr, to_addr;
2258 int n, m;
2260 subseg_change (lie->seg, lie->subseg);
2261 fragP = lie->dispfrag;
2263 /* Find out how many broken_words go here. */
2264 n = 0;
2265 for (untruth = lie;
2266 untruth && untruth->dispfrag == fragP;
2267 untruth = untruth->next_broken_word)
2268 if (untruth->added == 1)
2269 n++;
2271 table_ptr = lie->dispfrag->fr_opcode;
2272 table_addr = (lie->dispfrag->fr_address
2273 + (table_ptr - lie->dispfrag->fr_literal));
2274 /* Create the jump around the long jumps. This is a short
2275 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
2276 from_addr = table_addr;
2277 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
2278 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2279 lie->add);
2280 table_ptr += md_short_jump_size;
2281 table_addr += md_short_jump_size;
2283 for (m = 0;
2284 lie && lie->dispfrag == fragP;
2285 m++, lie = lie->next_broken_word)
2287 if (lie->added == 2)
2288 continue;
2289 /* Patch the jump table. */
2290 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2291 untruth && untruth->dispfrag == fragP;
2292 untruth = untruth->next_broken_word)
2294 if (untruth->use_jump == lie)
2296 /* This is the offset from ??? to table_ptr+0.
2297 The target is the same for all users of this
2298 md_long_jump, but the "sub" bases (and hence the
2299 offsets) may be different. */
2300 addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
2301 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
2302 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
2303 #endif
2304 md_number_to_chars (untruth->word_goes_here, to_word, 2);
2308 /* Install the long jump. */
2309 /* This is a long jump from table_ptr+0 to the final target. */
2310 from_addr = table_addr;
2311 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
2312 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2313 lie->add);
2314 table_ptr += md_long_jump_size;
2315 table_addr += md_long_jump_size;
2319 #endif /* not WORKING_DOT_WORD */
2321 /* Resolve symbol values. This needs to be done before processing
2322 the relocations. */
2323 if (symbol_rootP)
2325 symbolS *symp;
2327 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2328 resolve_symbol_value (symp);
2330 resolve_local_symbol_values ();
2331 resolve_reloc_expr_symbols ();
2333 #ifdef OBJ_ELF
2334 if (IS_ELF)
2335 maybe_generate_build_notes ();
2336 #endif
2338 #ifdef tc_frob_file_before_adjust
2339 tc_frob_file_before_adjust ();
2340 #endif
2341 #ifdef obj_frob_file_before_adjust
2342 obj_frob_file_before_adjust ();
2343 #endif
2345 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
2347 #ifdef tc_frob_file_before_fix
2348 tc_frob_file_before_fix ();
2349 #endif
2350 #ifdef obj_frob_file_before_fix
2351 obj_frob_file_before_fix ();
2352 #endif
2354 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
2356 /* Set up symbol table, and write it out. */
2357 if (symbol_rootP)
2359 symbolS *symp;
2360 bool skip_next_symbol = false;
2362 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2364 int punt = 0;
2365 const char *name;
2367 if (skip_next_symbol)
2369 /* Don't do anything besides moving the value of the
2370 symbol from the GAS value-field to the BFD value-field. */
2371 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2372 skip_next_symbol = false;
2373 continue;
2376 if (symbol_mri_common_p (symp))
2378 if (S_IS_EXTERNAL (symp))
2379 as_bad (_("%s: global symbols not supported in common sections"),
2380 S_GET_NAME (symp));
2381 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2382 continue;
2385 name = S_GET_NAME (symp);
2386 if (name)
2388 const char *name2 =
2389 decode_local_label_name ((char *) S_GET_NAME (symp));
2390 /* They only differ if `name' is a fb or dollar local
2391 label name. */
2392 if (name2 != name && ! S_IS_DEFINED (symp))
2393 as_bad (_("local label `%s' is not defined"), name2);
2396 /* Do it again, because adjust_reloc_syms might introduce
2397 more symbols. They'll probably only be section symbols,
2398 but they'll still need to have the values computed. */
2399 resolve_symbol_value (symp);
2401 /* Skip symbols which were equated to undefined or common
2402 symbols. */
2403 if (symbol_equated_reloc_p (symp)
2404 || S_IS_WEAKREFR (symp))
2406 const char *sname = S_GET_NAME (symp);
2408 if (S_IS_COMMON (symp)
2409 && !TC_FAKE_LABEL (sname)
2410 && !S_IS_WEAKREFR (symp))
2412 expressionS *e = symbol_get_value_expression (symp);
2414 as_bad (_("`%s' can't be equated to common symbol `%s'"),
2415 sname, S_GET_NAME (e->X_add_symbol));
2417 if (S_GET_SEGMENT (symp) == reg_section)
2419 /* Report error only if we know the symbol name. */
2420 if (S_GET_NAME (symp) != reg_section->name)
2421 as_bad (_("can't make global register symbol `%s'"),
2422 sname);
2424 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2425 continue;
2428 #ifdef obj_frob_symbol
2429 obj_frob_symbol (symp, punt);
2430 #endif
2431 #ifdef tc_frob_symbol
2432 if (! punt || symbol_used_in_reloc_p (symp))
2433 tc_frob_symbol (symp, punt);
2434 #endif
2436 /* If we don't want to keep this symbol, splice it out of
2437 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2438 want section symbols. Otherwise, we skip local symbols
2439 and symbols that the frob_symbol macros told us to punt,
2440 but we keep such symbols if they are used in relocs. */
2441 if (symp == abs_section_sym
2442 || (! EMIT_SECTION_SYMBOLS
2443 && symbol_section_p (symp))
2444 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2445 opposites. Sometimes the former checks flags and the
2446 latter examines the name... */
2447 || (!S_IS_EXTERNAL (symp)
2448 && (punt || S_IS_LOCAL (symp) ||
2449 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
2450 && ! symbol_used_in_reloc_p (symp)))
2452 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2454 /* After symbol_remove, symbol_next(symp) still returns
2455 the one that came after it in the chain. So we don't
2456 need to do any extra cleanup work here. */
2457 continue;
2460 /* Make sure we really got a value for the symbol. */
2461 if (! symbol_resolved_p (symp))
2463 as_bad (_("can't resolve value for symbol `%s'"),
2464 S_GET_NAME (symp));
2465 symbol_mark_resolved (symp);
2468 /* Set the value into the BFD symbol. Up til now the value
2469 has only been kept in the gas symbolS struct. */
2470 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2472 /* A warning construct is a warning symbol followed by the
2473 symbol warned about. Don't let anything object-format or
2474 target-specific muck with it; it's ready for output. */
2475 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2476 skip_next_symbol = true;
2480 /* Now do any format-specific adjustments to the symbol table, such
2481 as adding file symbols. */
2482 #ifdef tc_adjust_symtab
2483 tc_adjust_symtab ();
2484 #endif
2485 #ifdef obj_adjust_symtab
2486 obj_adjust_symtab ();
2487 #endif
2489 /* Stop if there is an error. */
2490 if (!flag_always_generate_output && had_errors ())
2491 return;
2493 /* Now that all the sizes are known, and contents correct, we can
2494 start writing to the file. */
2495 set_symtab ();
2497 /* If *_frob_file changes the symbol value at this point, it is
2498 responsible for moving the changed value into symp->bsym->value
2499 as well. Hopefully all symbol value changing can be done in
2500 *_frob_symbol. */
2501 #ifdef tc_frob_file
2502 tc_frob_file ();
2503 #endif
2504 #ifdef obj_frob_file
2505 obj_frob_file ();
2506 #endif
2507 #ifdef obj_coff_generate_pdata
2508 obj_coff_generate_pdata ();
2509 #endif
2511 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2513 #ifdef tc_frob_file_after_relocs
2514 tc_frob_file_after_relocs ();
2515 #endif
2516 #ifdef obj_frob_file_after_relocs
2517 obj_frob_file_after_relocs ();
2518 #endif
2520 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
2521 if (IS_ELF && flag_use_elf_stt_common)
2522 stdoutput->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
2523 #endif
2525 /* Once all relocations have been written, we can compress the
2526 contents of the debug sections. This needs to be done before
2527 we start writing any sections, because it will affect the file
2528 layout, which is fixed once we start writing contents. */
2529 if (flag_compress_debug != COMPRESS_DEBUG_NONE)
2531 flagword flags = BFD_COMPRESS;
2532 if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
2533 flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
2534 else if (flag_compress_debug == COMPRESS_DEBUG_ZSTD)
2535 flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
2536 stdoutput->flags |= flags & bfd_applicable_file_flags (stdoutput);
2537 if ((stdoutput->flags & BFD_COMPRESS) != 0)
2538 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
2541 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2544 #ifdef TC_GENERIC_RELAX_TABLE
2545 #ifndef md_generic_table_relax_frag
2546 #define md_generic_table_relax_frag relax_frag
2547 #endif
2549 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2551 long
2552 relax_frag (segT segment, fragS *fragP, long stretch)
2554 const relax_typeS *this_type;
2555 const relax_typeS *start_type;
2556 relax_substateT next_state;
2557 relax_substateT this_state;
2558 offsetT growth;
2559 offsetT aim;
2560 addressT target;
2561 addressT address;
2562 symbolS *symbolP;
2563 const relax_typeS *table;
2565 target = fragP->fr_offset;
2566 address = fragP->fr_address + fragP->fr_fix;
2567 table = TC_GENERIC_RELAX_TABLE;
2568 this_state = fragP->fr_subtype;
2569 start_type = this_type = table + this_state;
2570 symbolP = fragP->fr_symbol;
2572 if (symbolP)
2574 fragS *sym_frag;
2576 sym_frag = symbol_get_frag (symbolP);
2578 #ifndef DIFF_EXPR_OK
2579 know (sym_frag != NULL);
2580 #endif
2581 know (S_GET_SEGMENT (symbolP) != absolute_section
2582 || sym_frag == &zero_address_frag);
2583 target += S_GET_VALUE (symbolP);
2585 /* If SYM_FRAG has yet to be reached on this pass, assume it
2586 will move by STRETCH just as we did, unless there is an
2587 alignment frag between here and SYM_FRAG. An alignment may
2588 well absorb any STRETCH, and we don't want to choose a larger
2589 branch insn by overestimating the needed reach of this
2590 branch. It isn't critical to calculate TARGET exactly; We
2591 know we'll be doing another pass if STRETCH is non-zero. */
2593 if (stretch != 0
2594 && sym_frag->relax_marker != fragP->relax_marker
2595 && S_GET_SEGMENT (symbolP) == segment)
2597 if (stretch < 0
2598 || sym_frag->region == fragP->region)
2599 target += stretch;
2600 /* If we get here we know we have a forward branch. This
2601 relax pass may have stretched previous instructions so
2602 far that omitting STRETCH would make the branch
2603 negative. Don't allow this in case the negative reach is
2604 large enough to require a larger branch instruction. */
2605 else if (target < address)
2606 return 0;
2610 aim = target - address;
2611 #ifdef TC_PCREL_ADJUST
2612 /* Currently only the ns32k and arc needs this. */
2613 aim += TC_PCREL_ADJUST (fragP);
2614 #endif
2616 #ifdef md_prepare_relax_scan
2617 /* Formerly called M68K_AIM_KLUDGE. */
2618 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2619 #endif
2621 if (aim < 0)
2623 /* Look backwards. */
2624 for (next_state = this_type->rlx_more; next_state;)
2625 if (aim >= this_type->rlx_backward)
2626 next_state = 0;
2627 else
2629 /* Grow to next state. */
2630 this_state = next_state;
2631 this_type = table + this_state;
2632 next_state = this_type->rlx_more;
2635 else
2637 /* Look forwards. */
2638 for (next_state = this_type->rlx_more; next_state;)
2639 if (aim <= this_type->rlx_forward)
2640 next_state = 0;
2641 else
2643 /* Grow to next state. */
2644 this_state = next_state;
2645 this_type = table + this_state;
2646 next_state = this_type->rlx_more;
2650 growth = this_type->rlx_length - start_type->rlx_length;
2651 if (growth != 0)
2652 fragP->fr_subtype = this_state;
2653 return growth;
2656 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2658 /* Relax_align. Advance location counter to next address that has 'alignment'
2659 lowest order bits all 0s, return size of adjustment made. */
2660 static relax_addressT
2661 relax_align (relax_addressT address, /* Address now. */
2662 int alignment /* Alignment (binary). */)
2664 relax_addressT mask;
2665 relax_addressT new_address;
2667 mask = ~((relax_addressT) ~0 << alignment);
2668 new_address = (address + mask) & (~mask);
2669 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2670 if (linkrelax)
2671 /* We must provide lots of padding, so the linker can discard it
2672 when needed. The linker will not add extra space, ever. */
2673 new_address += (1 << alignment);
2674 #endif
2675 return (new_address - address);
2678 /* Now we have a segment, not a crowd of sub-segments, we can make
2679 fr_address values.
2681 Relax the frags.
2683 After this, all frags in this segment have addresses that are correct
2684 within the segment. Since segments live in different file addresses,
2685 these frag addresses may not be the same as final object-file
2686 addresses. */
2689 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2691 unsigned long frag_count;
2692 struct frag *fragP;
2693 relax_addressT address;
2694 int region;
2695 int ret;
2697 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2698 subseg_change (segment, 0);
2700 /* For each frag in segment: count and store (a 1st guess of)
2701 fr_address. */
2702 address = 0;
2703 region = 0;
2704 for (frag_count = 0, fragP = segment_frag_root;
2705 fragP;
2706 fragP = fragP->fr_next, frag_count ++)
2708 fragP->region = region;
2709 fragP->relax_marker = 0;
2710 fragP->fr_address = address;
2711 address += fragP->fr_fix;
2713 switch (fragP->fr_type)
2715 case rs_fill:
2716 address += fragP->fr_offset * fragP->fr_var;
2717 break;
2719 case rs_align:
2720 case rs_align_code:
2721 case rs_align_test:
2723 addressT offset = relax_align (address, (int) fragP->fr_offset);
2725 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2726 offset = 0;
2728 if (offset % fragP->fr_var != 0)
2730 as_bad_where (fragP->fr_file, fragP->fr_line,
2731 ngettext ("alignment padding (%lu byte) "
2732 "not a multiple of %ld",
2733 "alignment padding (%lu bytes) "
2734 "not a multiple of %ld",
2735 (unsigned long) offset),
2736 (unsigned long) offset, (long) fragP->fr_var);
2737 offset -= (offset % fragP->fr_var);
2740 address += offset;
2741 region += 1;
2743 break;
2745 case rs_org:
2746 /* Assume .org is nugatory. It will grow with 1st relax. */
2747 region += 1;
2748 break;
2750 case rs_space:
2751 case rs_space_nop:
2752 break;
2754 case rs_machine_dependent:
2755 /* If fr_symbol is an expression, this call to
2756 resolve_symbol_value sets up the correct segment, which will
2757 likely be needed in md_estimate_size_before_relax. */
2758 if (fragP->fr_symbol)
2759 resolve_symbol_value (fragP->fr_symbol);
2761 address += md_estimate_size_before_relax (fragP, segment);
2762 break;
2764 #ifndef WORKING_DOT_WORD
2765 /* Broken words don't concern us yet. */
2766 case rs_broken_word:
2767 break;
2768 #endif
2770 case rs_leb128:
2771 /* Initial guess is always 1; doing otherwise can result in
2772 stable solutions that are larger than the minimum. */
2773 address += fragP->fr_offset = 1;
2774 break;
2776 case rs_cfa:
2777 address += eh_frame_estimate_size_before_relax (fragP);
2778 break;
2780 case rs_dwarf2dbg:
2781 address += dwarf2dbg_estimate_size_before_relax (fragP);
2782 break;
2784 case rs_sframe:
2785 /* Initial estimate can be set to atleast 1 byte. */
2786 address += sframe_estimate_size_before_relax (fragP);
2787 break;
2789 default:
2790 BAD_CASE (fragP->fr_type);
2791 break;
2795 /* Do relax(). */
2797 unsigned long max_iterations;
2799 /* Cumulative address adjustment. */
2800 offsetT stretch;
2802 /* Have we made any adjustment this pass? We can't just test
2803 stretch because one piece of code may have grown and another
2804 shrank. */
2805 int stretched;
2807 /* Most horrible, but gcc may give us some exception data that
2808 is impossible to assemble, of the form
2810 .align 4
2811 .byte 0, 0
2812 .uleb128 end - start
2813 start:
2814 .space 128*128 - 1
2815 .align 4
2816 end:
2818 If the leb128 is two bytes in size, then end-start is 128*128,
2819 which requires a three byte leb128. If the leb128 is three
2820 bytes in size, then end-start is 128*128-1, which requires a
2821 two byte leb128. We work around this dilemma by inserting
2822 an extra 4 bytes of alignment just after the .align. This
2823 works because the data after the align is accessed relative to
2824 the end label.
2826 This counter is used in a tiny state machine to detect
2827 whether a leb128 followed by an align is impossible to
2828 relax. */
2829 int rs_leb128_fudge = 0;
2831 /* We want to prevent going into an infinite loop where one frag grows
2832 depending upon the location of a symbol which is in turn moved by
2833 the growing frag. eg:
2835 foo = .
2836 .org foo+16
2837 foo = .
2839 So we dictate that this algorithm can be at most O2. */
2840 max_iterations = frag_count * frag_count;
2841 /* Check for overflow. */
2842 if (max_iterations < frag_count)
2843 max_iterations = frag_count;
2845 ret = 0;
2848 stretch = 0;
2849 stretched = 0;
2851 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2853 offsetT growth = 0;
2854 addressT was_address;
2855 offsetT offset;
2856 symbolS *symbolP;
2858 fragP->relax_marker ^= 1;
2859 was_address = fragP->fr_address;
2860 address = fragP->fr_address += stretch;
2861 symbolP = fragP->fr_symbol;
2862 offset = fragP->fr_offset;
2864 switch (fragP->fr_type)
2866 case rs_fill: /* .fill never relaxes. */
2867 growth = 0;
2868 break;
2870 #ifndef WORKING_DOT_WORD
2871 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2872 for it I do not want to write it. I do not want to have
2873 anything to do with it. This is not the proper way to
2874 implement this misfeature. */
2875 case rs_broken_word:
2877 struct broken_word *lie;
2878 struct broken_word *untruth;
2880 /* Yes this is ugly (storing the broken_word pointer
2881 in the symbol slot). Still, this whole chunk of
2882 code is ugly, and I don't feel like doing anything
2883 about it. Think of it as stubbornness in action. */
2884 growth = 0;
2885 for (lie = (struct broken_word *) (fragP->fr_symbol);
2886 lie && lie->dispfrag == fragP;
2887 lie = lie->next_broken_word)
2890 if (lie->added)
2891 continue;
2893 offset = (S_GET_VALUE (lie->add)
2894 + lie->addnum
2895 - S_GET_VALUE (lie->sub));
2896 if (offset <= -32768 || offset >= 32767)
2898 if (flag_warn_displacement)
2900 char buf[50];
2902 bfd_sprintf_vma (stdoutput, buf,
2903 (addressT) lie->addnum);
2904 as_warn_where (fragP->fr_file, fragP->fr_line,
2905 _(".word %s-%s+%s didn't fit"),
2906 S_GET_NAME (lie->add),
2907 S_GET_NAME (lie->sub),
2908 buf);
2910 if (fragP->fr_subtype == 0)
2912 fragP->fr_subtype++;
2913 growth += md_short_jump_size;
2916 /* Redirect *all* words of this table with the same
2917 target, lest we have to handle the case where the
2918 same target but with a offset that fits on this
2919 round overflows at the next relaxation round. */
2920 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2921 untruth && untruth->dispfrag == lie->dispfrag;
2922 untruth = untruth->next_broken_word)
2923 if ((symbol_get_frag (untruth->add)
2924 == symbol_get_frag (lie->add))
2925 && (S_GET_VALUE (untruth->add)
2926 == S_GET_VALUE (lie->add)))
2928 untruth->added = 2;
2929 untruth->use_jump = lie;
2932 lie->added = 1;
2933 growth += md_long_jump_size;
2937 break;
2938 } /* case rs_broken_word */
2939 #endif
2940 case rs_align:
2941 case rs_align_code:
2942 case rs_align_test:
2944 addressT oldoff, newoff;
2946 oldoff = relax_align (was_address + fragP->fr_fix,
2947 (int) offset);
2948 newoff = relax_align (address + fragP->fr_fix,
2949 (int) offset);
2951 if (fragP->fr_subtype != 0)
2953 if (oldoff > fragP->fr_subtype)
2954 oldoff = 0;
2955 if (newoff > fragP->fr_subtype)
2956 newoff = 0;
2959 growth = newoff - oldoff;
2961 /* If this align happens to follow a leb128 and
2962 we have determined that the leb128 is bouncing
2963 in size, then break the cycle by inserting an
2964 extra alignment. */
2965 if (growth < 0
2966 && (rs_leb128_fudge & 16) != 0
2967 && (rs_leb128_fudge & 15) >= 2)
2969 segment_info_type *seginfo = seg_info (segment);
2970 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2971 struct frag *newf;
2973 newf = frag_alloc (ob);
2974 obstack_blank_fast (ob, fragP->fr_var);
2975 obstack_finish (ob);
2976 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2977 memcpy (newf->fr_literal,
2978 fragP->fr_literal + fragP->fr_fix,
2979 fragP->fr_var);
2980 newf->fr_type = rs_fill;
2981 newf->fr_address = address + fragP->fr_fix + newoff;
2982 newf->fr_fix = 0;
2983 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2984 / fragP->fr_var);
2985 if (newf->fr_offset * newf->fr_var
2986 != (offsetT) 1 << fragP->fr_offset)
2988 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2989 newf->fr_var = 1;
2991 /* Include size of new frag in GROWTH. */
2992 growth += newf->fr_offset * newf->fr_var;
2993 /* Adjust the new frag address for the amount
2994 we'll add when we process the new frag. */
2995 newf->fr_address -= stretch + growth;
2996 newf->relax_marker ^= 1;
2997 fragP->fr_next = newf;
2998 #ifdef DEBUG
2999 as_warn (_("padding added"));
3000 #endif
3003 break;
3005 case rs_org:
3007 offsetT target = offset;
3008 addressT after;
3010 if (symbolP)
3012 /* Convert from an actual address to an octet offset
3013 into the section. Here it is assumed that the
3014 section's VMA is zero, and can omit subtracting it
3015 from the symbol's value to get the address offset. */
3016 know (S_GET_SEGMENT (symbolP)->vma == 0);
3017 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
3020 know (fragP->fr_next);
3021 after = fragP->fr_next->fr_address + stretch;
3022 growth = target - after;
3024 /* Growth may be negative, but variable part of frag
3025 cannot have fewer than 0 chars. That is, we can't
3026 .org backwards. */
3027 if ((offsetT) (address + fragP->fr_fix) > target)
3029 growth = 0;
3031 /* Don't error on first few frag relax passes.
3032 The symbol might be an expression involving
3033 symbol values from other sections. If those
3034 sections have not yet been processed their
3035 frags will all have zero addresses, so we
3036 will calculate incorrect values for them. The
3037 number of passes we allow before giving an
3038 error is somewhat arbitrary. It should be at
3039 least one, with larger values requiring
3040 increasingly contrived dependencies between
3041 frags to trigger a false error. */
3042 if (pass < 2)
3044 /* Force another pass. */
3045 ret = 1;
3046 break;
3049 as_bad_where (fragP->fr_file, fragP->fr_line,
3050 _("attempt to move .org backwards"));
3052 /* We've issued an error message. Change the
3053 frag to avoid cascading errors. */
3054 fragP->fr_type = rs_align;
3055 fragP->fr_subtype = 0;
3056 fragP->fr_offset = 0;
3057 fragP->fr_fix = after - address;
3060 break;
3062 case rs_space:
3063 case rs_space_nop:
3064 growth = 0;
3065 if (symbolP)
3067 offsetT amount;
3069 amount = S_GET_VALUE (symbolP);
3070 if (S_GET_SEGMENT (symbolP) != absolute_section
3071 || S_IS_COMMON (symbolP)
3072 || ! S_IS_DEFINED (symbolP))
3074 as_bad_where (fragP->fr_file, fragP->fr_line,
3075 _(".space, .nops or .fill specifies non-absolute value"));
3076 /* Prevent repeat of this error message. */
3077 fragP->fr_symbol = 0;
3079 else if (amount < 0)
3081 /* Don't error on first few frag relax passes.
3082 See rs_org comment for a longer explanation. */
3083 if (pass < 2)
3085 ret = 1;
3086 break;
3089 as_warn_where (fragP->fr_file, fragP->fr_line,
3090 _(".space, .nops or .fill with negative value, ignored"));
3091 fragP->fr_symbol = 0;
3093 else
3094 growth = (was_address + fragP->fr_fix + amount
3095 - fragP->fr_next->fr_address);
3097 break;
3099 case rs_machine_dependent:
3100 #ifdef md_relax_frag
3101 growth = md_relax_frag (segment, fragP, stretch);
3102 #else
3103 #ifdef TC_GENERIC_RELAX_TABLE
3104 /* The default way to relax a frag is to look through
3105 TC_GENERIC_RELAX_TABLE. */
3106 growth = md_generic_table_relax_frag (segment, fragP,
3107 stretch);
3108 #endif /* TC_GENERIC_RELAX_TABLE */
3109 #endif
3110 break;
3112 case rs_leb128:
3114 valueT value;
3115 offsetT size;
3117 value = resolve_symbol_value (fragP->fr_symbol);
3118 size = sizeof_leb128 (value, fragP->fr_subtype);
3119 growth = size - fragP->fr_offset;
3120 fragP->fr_offset = size;
3122 break;
3124 case rs_cfa:
3125 growth = eh_frame_relax_frag (fragP);
3126 break;
3128 case rs_dwarf2dbg:
3129 growth = dwarf2dbg_relax_frag (fragP);
3130 break;
3132 case rs_sframe:
3133 growth = sframe_relax_frag (fragP);
3134 break;
3136 default:
3137 BAD_CASE (fragP->fr_type);
3138 break;
3140 if (growth)
3142 stretch += growth;
3143 stretched = 1;
3144 if (fragP->fr_type == rs_leb128)
3145 rs_leb128_fudge += 16;
3146 else if (fragP->fr_type == rs_align
3147 && (rs_leb128_fudge & 16) != 0
3148 && stretch == 0)
3149 rs_leb128_fudge += 16;
3150 else
3151 rs_leb128_fudge = 0;
3155 if (stretch == 0
3156 && (rs_leb128_fudge & 16) == 0
3157 && (rs_leb128_fudge & -16) != 0)
3158 rs_leb128_fudge += 1;
3159 else
3160 rs_leb128_fudge = 0;
3162 /* Until nothing further to relax. */
3163 while (stretched && -- max_iterations);
3165 if (stretched)
3166 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
3167 segment_name (segment));
3170 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
3171 if (fragP->last_fr_address != fragP->fr_address)
3173 fragP->last_fr_address = fragP->fr_address;
3174 ret = 1;
3176 return ret;
3179 void
3180 number_to_chars_bigendian (char *buf, valueT val, int n)
3182 if (n <= 0)
3183 abort ();
3184 while (n--)
3186 buf[n] = val & 0xff;
3187 val >>= 8;
3191 void
3192 number_to_chars_littleendian (char *buf, valueT val, int n)
3194 if (n <= 0)
3195 abort ();
3196 while (n--)
3198 *buf++ = val & 0xff;
3199 val >>= 8;
3203 void
3204 write_print_statistics (FILE *file)
3206 fprintf (file, "fixups: %d\n", n_fixups);
3209 /* For debugging. */
3210 extern int indent_level;
3212 void
3213 print_fixup (fixS *fixp)
3215 indent_level = 1;
3216 fprintf (stderr, "fix %p %s:%d", fixp, 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=%p", fixp->fx_size, fixp->fx_frag);
3226 fprintf (stderr, " where=%ld offset=%" PRIx64 " addnumber=%" PRIx64,
3227 fixp->fx_where, (uint64_t) fixp->fx_offset,
3228 (uint64_t) fixp->fx_addnumber);
3229 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
3230 fixp->fx_r_type);
3231 if (fixp->fx_addsy)
3233 fprintf (stderr, "\n +<");
3234 print_symbol_value_1 (stderr, fixp->fx_addsy);
3235 fprintf (stderr, ">");
3237 if (fixp->fx_subsy)
3239 fprintf (stderr, "\n -<");
3240 print_symbol_value_1 (stderr, fixp->fx_subsy);
3241 fprintf (stderr, ">");
3243 fprintf (stderr, "\n");
3244 #ifdef TC_FIX_DATA_PRINT
3245 TC_FIX_DATA_PRINT (stderr, fixp);
3246 #endif