[gdb/symtab] Fix race on dwarf2_per_cu_data::{queued,is_debug_type}
[binutils-gdb.git] / gas / write.c
blob573a667da2206b07c4a98f46d700775e5dd8526a
1 /* write.c - emit .o file
2 Copyright (C) 1986-2023 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_done = 0;
173 fixP->fx_no_overflow = 0;
174 fixP->fx_signed = 0;
176 #ifdef USING_CGEN
177 fixP->fx_cgen.insn = NULL;
178 fixP->fx_cgen.opinfo = 0;
179 #endif
181 #ifdef TC_FIX_TYPE
182 TC_INIT_FIX_DATA (fixP);
183 #endif
185 fixP->fx_file = as_where (&fixP->fx_line);
189 fixS **seg_fix_rootP = (frags_chained
190 ? &seg_info (now_seg)->fix_root
191 : &frchain_now->fix_root);
192 fixS **seg_fix_tailP = (frags_chained
193 ? &seg_info (now_seg)->fix_tail
194 : &frchain_now->fix_tail);
196 if (at_beginning)
198 fixP->fx_next = *seg_fix_rootP;
199 *seg_fix_rootP = fixP;
200 if (fixP->fx_next == NULL)
201 *seg_fix_tailP = fixP;
203 else
205 fixP->fx_next = NULL;
206 if (*seg_fix_tailP)
207 (*seg_fix_tailP)->fx_next = fixP;
208 else
209 *seg_fix_rootP = fixP;
210 *seg_fix_tailP = fixP;
214 return fixP;
217 /* Create a fixup relative to a symbol (plus a constant). */
219 fixS *
220 fix_new (fragS *frag, /* Which frag? */
221 unsigned long where, /* Where in that frag? */
222 unsigned long size, /* 1, 2, or 4 usually. */
223 symbolS *add_symbol, /* X_add_symbol. */
224 offsetT offset, /* X_add_number. */
225 int pcrel, /* TRUE if PC-relative relocation. */
226 RELOC_ENUM r_type /* Relocation type. */)
228 return fix_new_internal (frag, where, size, add_symbol,
229 (symbolS *) NULL, offset, pcrel, r_type, false);
232 /* Create a fixup for an expression. Currently we only support fixups
233 for difference expressions. That is itself more than most object
234 file formats support anyhow. */
236 fixS *
237 fix_new_exp (fragS *frag, /* Which frag? */
238 unsigned long where, /* Where in that frag? */
239 unsigned long size, /* 1, 2, or 4 usually. */
240 expressionS *exp, /* Expression. */
241 int pcrel, /* TRUE if PC-relative relocation. */
242 RELOC_ENUM r_type /* Relocation type. */)
244 symbolS *add = NULL;
245 symbolS *sub = NULL;
246 offsetT off = 0;
248 switch (exp->X_op)
250 case O_absent:
251 break;
253 case O_register:
254 as_bad (_("register value used as expression"));
255 break;
257 case O_add:
258 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
259 the difference expression cannot immediately be reduced. */
261 symbolS *stmp = make_expr_symbol (exp);
263 exp->X_op = O_symbol;
264 exp->X_op_symbol = 0;
265 exp->X_add_symbol = stmp;
266 exp->X_add_number = 0;
268 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
271 case O_symbol_rva:
272 add = exp->X_add_symbol;
273 off = exp->X_add_number;
274 r_type = BFD_RELOC_RVA;
275 break;
277 case O_uminus:
278 sub = exp->X_add_symbol;
279 off = exp->X_add_number;
280 break;
282 case O_subtract:
283 sub = exp->X_op_symbol;
284 /* Fall through. */
285 case O_symbol:
286 add = exp->X_add_symbol;
287 /* Fall through. */
288 case O_constant:
289 off = exp->X_add_number;
290 break;
292 default:
293 add = make_expr_symbol (exp);
294 break;
297 return fix_new_internal (frag, where, size, add, sub, off, pcrel,
298 r_type, false);
301 /* Create a fixup at the beginning of FRAG. The arguments are the same
302 as for fix_new, except that WHERE is implicitly 0. */
304 fixS *
305 fix_at_start (fragS *frag, unsigned long size, symbolS *add_symbol,
306 offsetT offset, int pcrel, RELOC_ENUM r_type)
308 return fix_new_internal (frag, 0, size, add_symbol,
309 (symbolS *) NULL, offset, pcrel, r_type, true);
312 /* Generic function to determine whether a fixup requires a relocation. */
314 generic_force_reloc (fixS *fix)
316 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
317 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
318 return 1;
320 if (fix->fx_addsy == NULL)
321 return 0;
323 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
326 /* Append a string onto another string, bumping the pointer along. */
327 void
328 append (char **charPP, char *fromP, unsigned long length)
330 /* Don't trust memcpy() of 0 chars. */
331 if (length == 0)
332 return;
334 memcpy (*charPP, fromP, length);
335 *charPP += length;
338 /* This routine records the largest alignment seen for each segment.
339 If the beginning of the segment is aligned on the worst-case
340 boundary, all of the other alignments within it will work. At
341 least one object format really uses this info. */
343 void
344 record_alignment (/* Segment to which alignment pertains. */
345 segT seg,
346 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
347 boundary, 2 => 4-byte boundary, etc.) */
348 unsigned int align)
350 if (seg == absolute_section)
351 return;
353 if (align > bfd_section_alignment (seg))
354 bfd_set_section_alignment (seg, align);
358 get_recorded_alignment (segT seg)
360 if (seg == absolute_section)
361 return 0;
363 return bfd_section_alignment (seg);
366 /* Reset the section indices after removing the gas created sections. */
368 static void
369 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
371 int *countp = (int *) countparg;
373 sec->index = *countp;
374 ++*countp;
377 static fragS *
378 chain_frchains_together_1 (segT section, struct frchain *frchp)
380 fragS dummy, *prev_frag = &dummy;
381 fixS fix_dummy, *prev_fix = &fix_dummy;
385 prev_frag->fr_next = frchp->frch_root;
386 prev_frag = frchp->frch_last;
387 gas_assert (prev_frag->fr_type != 0);
388 if (frchp->fix_root != (fixS *) NULL)
390 if (seg_info (section)->fix_root == (fixS *) NULL)
391 seg_info (section)->fix_root = frchp->fix_root;
392 prev_fix->fx_next = frchp->fix_root;
393 seg_info (section)->fix_tail = frchp->fix_tail;
394 prev_fix = frchp->fix_tail;
396 frchp = frchp->frch_next;
397 } while (frchp);
398 gas_assert (prev_frag != &dummy
399 && prev_frag->fr_type != 0);
400 prev_frag->fr_next = 0;
401 return prev_frag;
404 static void
405 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
406 segT section,
407 void *xxx ATTRIBUTE_UNUSED)
409 segment_info_type *info;
411 /* BFD may have introduced its own sections without using
412 subseg_new, so it is possible that seg_info is NULL. */
413 info = seg_info (section);
414 if (info != (segment_info_type *) NULL)
415 info->frchainP->frch_last
416 = chain_frchains_together_1 (section, info->frchainP);
418 /* Now that we've chained the frags together, we must add new fixups
419 to the segment, not to the frag chain. */
420 frags_chained = true;
423 static void
424 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
426 switch (fragP->fr_type)
428 case rs_space_nop:
429 goto skip_align;
430 case rs_align:
431 case rs_align_code:
432 case rs_align_test:
433 case rs_org:
434 case rs_space:
435 #ifdef HANDLE_ALIGN
436 HANDLE_ALIGN (fragP);
437 #endif
438 skip_align:
439 know (fragP->fr_next != NULL);
440 fragP->fr_offset = (fragP->fr_next->fr_address
441 - fragP->fr_address
442 - fragP->fr_fix) / fragP->fr_var;
443 if (fragP->fr_offset < 0)
445 as_bad_where (fragP->fr_file, fragP->fr_line,
446 _("attempt to .org/.space/.nops backwards? (%ld)"),
447 (long) fragP->fr_offset);
448 fragP->fr_offset = 0;
450 if (fragP->fr_type == rs_space_nop)
451 fragP->fr_type = rs_fill_nop;
452 else
453 fragP->fr_type = rs_fill;
454 break;
456 case rs_fill:
457 case rs_fill_nop:
458 break;
460 case rs_leb128:
462 valueT value = S_GET_VALUE (fragP->fr_symbol);
463 int size;
465 if (!S_IS_DEFINED (fragP->fr_symbol))
467 as_bad_where (fragP->fr_file, fragP->fr_line,
468 _("leb128 operand is an undefined symbol: %s"),
469 S_GET_NAME (fragP->fr_symbol));
472 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
473 fragP->fr_subtype);
475 fragP->fr_fix += size;
476 fragP->fr_type = rs_fill;
477 fragP->fr_var = 0;
478 fragP->fr_offset = 0;
479 fragP->fr_symbol = NULL;
481 break;
483 case rs_cfa:
484 eh_frame_convert_frag (fragP);
485 break;
487 case rs_dwarf2dbg:
488 dwarf2dbg_convert_frag (fragP);
489 break;
491 case rs_sframe:
492 sframe_convert_frag (fragP);
493 break;
495 case rs_machine_dependent:
496 md_convert_frag (stdoutput, sec, fragP);
498 gas_assert (fragP->fr_next == NULL
499 || (fragP->fr_next->fr_address - fragP->fr_address
500 == fragP->fr_fix));
502 /* After md_convert_frag, we make the frag into a ".space 0".
503 md_convert_frag() should set up any fixSs and constants
504 required. */
505 frag_wane (fragP);
506 break;
508 #ifndef WORKING_DOT_WORD
509 case rs_broken_word:
511 struct broken_word *lie;
513 if (fragP->fr_subtype)
515 fragP->fr_fix += md_short_jump_size;
516 for (lie = (struct broken_word *) (fragP->fr_symbol);
517 lie && lie->dispfrag == fragP;
518 lie = lie->next_broken_word)
519 if (lie->added == 1)
520 fragP->fr_fix += md_long_jump_size;
522 frag_wane (fragP);
524 break;
525 #endif
527 default:
528 BAD_CASE (fragP->fr_type);
529 break;
531 #ifdef md_frag_check
532 md_frag_check (fragP);
533 #endif
536 struct relax_seg_info
538 int pass;
539 int changed;
542 static void
543 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
545 segment_info_type *seginfo = seg_info (sec);
546 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
548 if (seginfo && seginfo->frchainP
549 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
550 info->changed = 1;
553 static void
554 size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
556 flagword flags;
557 fragS *fragp;
558 segment_info_type *seginfo;
559 int x;
560 valueT size, newsize;
562 subseg_change (sec, 0);
564 seginfo = seg_info (sec);
565 if (seginfo && seginfo->frchainP)
567 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
568 cvt_frag_to_fill (sec, fragp);
569 for (fragp = seginfo->frchainP->frch_root;
570 fragp->fr_next;
571 fragp = fragp->fr_next)
572 /* Walk to last elt. */
574 size = fragp->fr_address + fragp->fr_fix;
576 else
577 size = 0;
579 flags = bfd_section_flags (sec);
580 if (size == 0 && bfd_section_size (sec) != 0 &&
581 (flags & SEC_HAS_CONTENTS) != 0)
582 return;
584 if (size > 0 && ! seginfo->bss)
585 flags |= SEC_HAS_CONTENTS;
587 x = bfd_set_section_flags (sec, flags);
588 gas_assert (x);
590 /* If permitted, allow the backend to pad out the section
591 to some alignment boundary. */
592 if (do_not_pad_sections_to_alignment)
593 newsize = size;
594 else
595 newsize = md_section_align (sec, size);
596 x = bfd_set_section_size (sec, newsize);
597 gas_assert (x);
599 /* If the size had to be rounded up, add some padding in the last
600 non-empty frag. */
601 gas_assert (newsize >= size);
602 if (size != newsize)
604 fragS *last = seginfo->frchainP->frch_last;
605 fragp = seginfo->frchainP->frch_root;
606 while (fragp->fr_next != last)
607 fragp = fragp->fr_next;
608 last->fr_address = size;
609 if ((newsize - size) % fragp->fr_var == 0)
610 fragp->fr_offset += (newsize - size) / fragp->fr_var;
611 else
612 /* If we hit this abort, it's likely due to subsegs_finish not
613 providing sufficient alignment on the last frag, and the
614 machine dependent code using alignment frags with fr_var
615 greater than 1. */
616 abort ();
619 #ifdef tc_frob_section
620 tc_frob_section (sec);
621 #endif
622 #ifdef obj_frob_section
623 obj_frob_section (sec);
624 #endif
627 #ifdef DEBUG2
628 static void
629 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
631 segment_info_type *seginfo = seg_info (sec);
632 fixS *fixp = seginfo->fix_root;
634 if (!fixp)
635 return;
637 fprintf (stream, "sec %s relocs:\n", sec->name);
638 while (fixp)
640 symbolS *s = fixp->fx_addsy;
642 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
643 (int) fixp->fx_r_type);
644 if (s == NULL)
645 fprintf (stream, "no sym\n");
646 else
648 print_symbol_value_1 (stream, s);
649 fprintf (stream, "\n");
651 fixp = fixp->fx_next;
654 #else
655 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
656 #endif
658 #ifndef EMIT_SECTION_SYMBOLS
659 #define EMIT_SECTION_SYMBOLS 1
660 #endif
662 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
663 and check for validity. Convert RELOC_LIST from using U.A fields
664 to U.B fields. */
665 static void
666 resolve_reloc_expr_symbols (void)
668 bfd_vma addr_mask = 1;
669 struct reloc_list *r;
671 /* Avoid a shift by the width of type. */
672 addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1;
673 addr_mask <<= 1;
674 addr_mask -= 1;
676 for (r = reloc_list; r; r = r->next)
678 reloc_howto_type *howto = r->u.a.howto;
679 expressionS *symval;
680 symbolS *sym;
681 bfd_vma offset, addend;
682 asection *sec;
684 resolve_symbol_value (r->u.a.offset_sym);
685 symval = symbol_get_value_expression (r->u.a.offset_sym);
687 offset = 0;
688 sym = NULL;
689 if (symval->X_op == O_constant)
690 sym = r->u.a.offset_sym;
691 else if (symval->X_op == O_symbol)
693 sym = symval->X_add_symbol;
694 offset = symval->X_add_number;
695 symval = symbol_get_value_expression (symval->X_add_symbol);
697 if (sym == NULL
698 || symval->X_op != O_constant
699 || (sec = S_GET_SEGMENT (sym)) == NULL
700 || !SEG_NORMAL (sec))
702 as_bad_where (r->file, r->line, _("invalid offset expression"));
703 sec = NULL;
705 else
706 offset += S_GET_VALUE (sym);
708 sym = NULL;
709 addend = r->u.a.addend;
710 if (r->u.a.sym != NULL)
712 resolve_symbol_value (r->u.a.sym);
713 symval = symbol_get_value_expression (r->u.a.sym);
714 if (symval->X_op == O_constant)
715 sym = r->u.a.sym;
716 else if (symval->X_op == O_symbol)
718 sym = symval->X_add_symbol;
719 addend += symval->X_add_number;
720 symval = symbol_get_value_expression (symval->X_add_symbol);
722 if (symval->X_op != O_constant)
724 as_bad_where (r->file, r->line, _("invalid reloc expression"));
725 sec = NULL;
727 else if (sym != NULL && sec != NULL)
729 /* Convert relocs against local symbols to refer to the
730 corresponding section symbol plus offset instead. Keep
731 PC-relative relocs of the REL variety intact though to
732 prevent the offset from overflowing the relocated field,
733 unless it has enough bits to cover the whole address
734 space. */
735 if (S_IS_LOCAL (sym)
736 && S_IS_DEFINED (sym)
737 && !symbol_section_p (sym)
738 && (sec->use_rela_p
739 || (howto->partial_inplace
740 && (!howto->pc_relative
741 || howto->src_mask == addr_mask))))
743 asection *symsec = S_GET_SEGMENT (sym);
744 if (!(((symsec->flags & SEC_MERGE) != 0
745 && addend != 0)
746 || (symsec->flags & SEC_THREAD_LOCAL) != 0))
748 addend += S_GET_VALUE (sym);
749 sym = section_symbol (symsec);
752 symbol_mark_used_in_reloc (sym);
755 if (sym == NULL)
757 if (abs_section_sym == NULL)
758 abs_section_sym = section_symbol (absolute_section);
759 sym = abs_section_sym;
762 r->u.b.sec = sec;
763 r->u.b.s = symbol_get_bfdsym (sym);
764 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
765 r->u.b.r.address = offset;
766 r->u.b.r.addend = addend;
767 r->u.b.r.howto = howto;
771 /* This pass over fixups decides whether symbols can be replaced with
772 section symbols. */
774 static void
775 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
776 asection *sec,
777 void *xxx ATTRIBUTE_UNUSED)
779 segment_info_type *seginfo = seg_info (sec);
780 fixS *fixp;
782 if (seginfo == NULL)
783 return;
785 dump_section_relocs (abfd, sec, stderr);
787 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
788 if (fixp->fx_done)
789 /* Ignore it. */
791 else if (fixp->fx_addsy)
793 symbolS *sym;
794 asection *symsec;
796 #ifdef DEBUG5
797 fprintf (stderr, "\n\nadjusting fixup:\n");
798 print_fixup (fixp);
799 #endif
801 sym = fixp->fx_addsy;
803 /* All symbols should have already been resolved at this
804 point. It is possible to see unresolved expression
805 symbols, though, since they are not in the regular symbol
806 table. */
807 resolve_symbol_value (sym);
809 if (fixp->fx_subsy != NULL)
810 resolve_symbol_value (fixp->fx_subsy);
812 /* If this symbol is equated to an undefined or common symbol,
813 convert the fixup to being against that symbol. */
814 while (symbol_equated_reloc_p (sym)
815 || S_IS_WEAKREFR (sym))
817 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
818 if (sym == newsym)
819 break;
820 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
821 fixp->fx_addsy = newsym;
822 sym = newsym;
825 if (symbol_mri_common_p (sym))
827 fixp->fx_offset += S_GET_VALUE (sym);
828 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
829 continue;
832 /* If the symbol is undefined, common, weak, or global (ELF
833 shared libs), we can't replace it with the section symbol. */
834 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
835 continue;
837 /* Is there some other (target cpu dependent) reason we can't adjust
838 this one? (E.g. relocations involving function addresses on
839 the PA. */
840 #ifdef tc_fix_adjustable
841 if (! tc_fix_adjustable (fixp))
842 continue;
843 #endif
845 /* Since we're reducing to section symbols, don't attempt to reduce
846 anything that's already using one. */
847 if (symbol_section_p (sym))
849 /* Mark the section symbol used in relocation so that it will
850 be included in the symbol table. */
851 symbol_mark_used_in_reloc (sym);
852 continue;
855 symsec = S_GET_SEGMENT (sym);
856 if (symsec == NULL)
857 abort ();
859 if (bfd_is_abs_section (symsec)
860 || symsec == reg_section)
862 /* The fixup_segment routine normally will not use this
863 symbol in a relocation. */
864 continue;
867 /* Don't try to reduce relocs which refer to non-local symbols
868 in .linkonce sections. It can lead to confusion when a
869 debugging section refers to a .linkonce section. I hope
870 this will always be correct. */
871 if (symsec != sec && ! S_IS_LOCAL (sym))
873 if ((symsec->flags & SEC_LINK_ONCE) != 0
874 || (IS_ELF
875 /* The GNU toolchain uses an extension for ELF: a
876 section beginning with the magic string
877 .gnu.linkonce is a linkonce section. */
878 && startswith (segment_name (symsec), ".gnu.linkonce")))
879 continue;
882 /* Never adjust a reloc against local symbol in a merge section
883 with non-zero addend. */
884 if ((symsec->flags & SEC_MERGE) != 0
885 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
886 continue;
888 /* Never adjust a reloc against TLS local symbol. */
889 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
890 continue;
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 += S_GET_VALUE (sym);
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 free (buf);
1687 continue;
1690 gas_assert (count >= 0);
1691 if (fill_size && count)
1693 char buf[256];
1694 if (fill_size > sizeof (buf))
1696 /* Do it the old way. Can this ever happen? */
1697 while (count--)
1699 x = bfd_set_section_contents (stdoutput, sec,
1700 fill_literal,
1701 (file_ptr) offset,
1702 (bfd_size_type) fill_size);
1703 if (!x)
1704 as_fatal (ngettext ("can't fill %ld byte "
1705 "in section %s of %s: '%s'",
1706 "can't fill %ld bytes "
1707 "in section %s of %s: '%s'",
1708 (long) fill_size),
1709 (long) fill_size,
1710 bfd_section_name (sec),
1711 bfd_get_filename (stdoutput),
1712 bfd_errmsg (bfd_get_error ()));
1713 offset += fill_size;
1716 else
1718 /* Build a buffer full of fill objects and output it as
1719 often as necessary. This saves on the overhead of
1720 potentially lots of bfd_set_section_contents calls. */
1721 int n_per_buf, i;
1722 if (fill_size == 1)
1724 n_per_buf = sizeof (buf);
1725 memset (buf, *fill_literal, n_per_buf);
1727 else
1729 char *bufp;
1730 n_per_buf = sizeof (buf) / fill_size;
1731 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1732 memcpy (bufp, fill_literal, fill_size);
1734 for (; count > 0; count -= n_per_buf)
1736 n_per_buf = n_per_buf > count ? count : n_per_buf;
1737 x = bfd_set_section_contents
1738 (stdoutput, sec, buf, (file_ptr) offset,
1739 (bfd_size_type) n_per_buf * fill_size);
1740 if (!x)
1741 as_fatal (ngettext ("can't fill %ld byte "
1742 "in section %s of %s: '%s'",
1743 "can't fill %ld bytes "
1744 "in section %s of %s: '%s'",
1745 (long) (n_per_buf * fill_size)),
1746 (long) (n_per_buf * fill_size),
1747 bfd_section_name (sec),
1748 bfd_get_filename (stdoutput),
1749 bfd_errmsg (bfd_get_error ()));
1750 offset += n_per_buf * fill_size;
1757 static void
1758 merge_data_into_text (void)
1760 seg_info (text_section)->frchainP->frch_last->fr_next =
1761 seg_info (data_section)->frchainP->frch_root;
1762 seg_info (text_section)->frchainP->frch_last =
1763 seg_info (data_section)->frchainP->frch_last;
1764 seg_info (data_section)->frchainP = 0;
1767 static void
1768 set_symtab (void)
1770 int nsyms;
1771 asymbol **asympp;
1772 symbolS *symp;
1773 bool result;
1775 /* Count symbols. We can't rely on a count made by the loop in
1776 write_object_file, because *_frob_file may add a new symbol or
1777 two. Generate unused section symbols only if needed. */
1778 nsyms = 0;
1779 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1780 if (!symbol_removed_p (symp)
1781 && (bfd_keep_unused_section_symbols (stdoutput)
1782 || !symbol_section_p (symp)
1783 || symbol_used_in_reloc_p (symp)))
1784 nsyms++;
1786 if (nsyms)
1788 int i;
1789 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1791 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1792 symp = symbol_rootP;
1793 for (i = 0; i < nsyms; symp = symbol_next (symp))
1794 if (!symbol_removed_p (symp)
1795 && (bfd_keep_unused_section_symbols (stdoutput)
1796 || !symbol_section_p (symp)
1797 || symbol_used_in_reloc_p (symp)))
1799 asympp[i] = symbol_get_bfdsym (symp);
1800 if (asympp[i]->flags != BSF_SECTION_SYM
1801 || !(bfd_is_const_section (asympp[i]->section)
1802 && asympp[i]->section->symbol == asympp[i]))
1803 asympp[i]->flags |= BSF_KEEP;
1804 symbol_mark_written (symp);
1805 /* Include this section symbol in the symbol table. */
1806 if (symbol_section_p (symp))
1807 asympp[i]->flags |= BSF_SECTION_SYM_USED;
1808 i++;
1811 else
1812 asympp = 0;
1813 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1814 gas_assert (result);
1815 symbol_table_frozen = 1;
1818 /* Finish the subsegments. After every sub-segment, we fake an
1819 ".align ...". This conforms to BSD4.2 brain-damage. We then fake
1820 ".fill 0" because that is the kind of frag that requires least
1821 thought. ".align" frags like to have a following frag since that
1822 makes calculating their intended length trivial. */
1824 #ifndef SUB_SEGMENT_ALIGN
1825 #ifdef HANDLE_ALIGN
1826 /* The last subsegment gets an alignment corresponding to the alignment
1827 of the section. This allows proper nop-filling at the end of
1828 code-bearing sections. */
1829 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1830 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
1831 && !do_not_pad_sections_to_alignment \
1832 ? get_recorded_alignment (SEG) \
1833 : 0)
1834 #else
1835 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1836 #endif
1837 #endif
1839 static void
1840 subsegs_finish_section (asection *s)
1842 struct frchain *frchainP;
1843 segment_info_type *seginfo = seg_info (s);
1844 if (!seginfo)
1845 return;
1847 for (frchainP = seginfo->frchainP;
1848 frchainP != NULL;
1849 frchainP = frchainP->frch_next)
1851 int alignment;
1853 subseg_set (s, frchainP->frch_subseg);
1855 /* This now gets called even if we had errors. In that case,
1856 any alignment is meaningless, and, moreover, will look weird
1857 if we are generating a listing. */
1858 if (had_errors ())
1859 do_not_pad_sections_to_alignment = 1;
1861 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1862 if ((bfd_section_flags (now_seg) & SEC_MERGE)
1863 && now_seg->entsize)
1865 unsigned int entsize = now_seg->entsize;
1866 int entalign = 0;
1868 while ((entsize & 1) == 0)
1870 ++entalign;
1871 entsize >>= 1;
1874 if (entalign > alignment)
1875 alignment = entalign;
1878 if (subseg_text_p (now_seg))
1879 frag_align_code (alignment, 0);
1880 else
1881 frag_align (alignment, 0, 0);
1883 /* frag_align will have left a new frag.
1884 Use this last frag for an empty ".fill".
1886 For this segment ...
1887 Create a last frag. Do not leave a "being filled in frag". */
1888 frag_wane (frag_now);
1889 frag_now->fr_fix = 0;
1890 know (frag_now->fr_next == NULL);
1894 static void
1895 subsegs_finish (void)
1897 asection *s;
1899 for (s = stdoutput->sections; s; s = s->next)
1900 subsegs_finish_section (s);
1903 #ifdef OBJ_ELF
1904 static void
1905 create_obj_attrs_section (void)
1907 segT s;
1908 char *p;
1909 offsetT size;
1910 const char *name;
1912 size = bfd_elf_obj_attr_size (stdoutput);
1913 if (size == 0)
1914 return;
1916 name = get_elf_backend_data (stdoutput)->obj_attrs_section;
1917 if (!name)
1918 name = ".gnu.attributes";
1919 s = subseg_new (name, 0);
1920 elf_section_type (s)
1921 = get_elf_backend_data (stdoutput)->obj_attrs_section_type;
1922 bfd_set_section_flags (s, SEC_READONLY | SEC_DATA);
1923 frag_now_fix ();
1924 p = frag_more (size);
1925 bfd_elf_set_obj_attr_contents (stdoutput, (bfd_byte *)p, size);
1927 subsegs_finish_section (s);
1928 relax_segment (seg_info (s)->frchainP->frch_root, s, 0);
1929 size_seg (stdoutput, s, NULL);
1932 /* Create a relocation against an entry in a GNU Build attribute section. */
1934 static void
1935 create_note_reloc (segT sec,
1936 symbolS * sym,
1937 bfd_size_type note_offset,
1938 bfd_size_type desc2_offset,
1939 offsetT desc2_size,
1940 int reloc_type,
1941 bfd_vma addend,
1942 char * note)
1944 struct reloc_list * reloc;
1946 reloc = XNEW (struct reloc_list);
1948 /* We create a .b type reloc as resolve_reloc_expr_symbols() has already been called. */
1949 reloc->u.b.sec = sec;
1950 reloc->u.b.s = symbol_get_bfdsym (sym);
1951 reloc->u.b.r.sym_ptr_ptr = & reloc->u.b.s;
1952 reloc->u.b.r.address = note_offset + desc2_offset;
1953 reloc->u.b.r.addend = addend;
1954 reloc->u.b.r.howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1956 if (reloc->u.b.r.howto == NULL)
1958 as_bad (_("unable to create reloc for build note"));
1959 return;
1962 reloc->file = N_("<gnu build note>");
1963 reloc->line = 0;
1965 reloc->next = reloc_list;
1966 reloc_list = reloc;
1968 /* For REL relocs, store the addend in the section. */
1969 if (! sec->use_rela_p
1970 /* The SH target is a special case that uses RELA relocs
1971 but still stores the addend in the word being relocated. */
1972 || strstr (bfd_get_target (stdoutput), "-sh") != NULL)
1974 offsetT i;
1976 /* Zero out the addend, since it is now stored in the note. */
1977 reloc->u.b.r.addend = 0;
1979 if (target_big_endian)
1981 for (i = desc2_size; addend != 0 && i > 0; addend >>= 8, i--)
1982 note[desc2_offset + i - 1] = (addend & 0xff);
1984 else
1986 for (i = 0; addend != 0 && i < desc2_size; addend >>= 8, i++)
1987 note[desc2_offset + i] = (addend & 0xff);
1992 static void
1993 maybe_generate_build_notes (void)
1995 segT sec;
1996 char * note;
1997 offsetT note_size;
1998 offsetT total_size;
1999 offsetT desc_size;
2000 offsetT desc2_offset;
2001 int desc_reloc;
2002 symbolS * sym;
2003 asymbol * bsym;
2005 if (! flag_generate_build_notes
2006 || bfd_get_section_by_name (stdoutput,
2007 GNU_BUILD_ATTRS_SECTION_NAME) != NULL)
2008 return;
2010 /* Create a GNU Build Attribute section. */
2011 sec = subseg_new (GNU_BUILD_ATTRS_SECTION_NAME, false);
2012 elf_section_type (sec) = SHT_NOTE;
2013 bfd_set_section_flags (sec, (SEC_READONLY | SEC_HAS_CONTENTS | SEC_DATA
2014 | SEC_OCTETS));
2015 bfd_set_section_alignment (sec, 2);
2017 /* Work out the size of the notes that we will create,
2018 and the relocation we should use. */
2019 if (bfd_arch_bits_per_address (stdoutput) <= 32)
2021 note_size = 28;
2022 desc_size = 8; /* Two 4-byte offsets. */
2023 desc2_offset = 24;
2025 /* FIXME: The BFD backend for the CRX target does not support the
2026 BFD_RELOC_32, even though it really should. Likewise for the
2027 CR16 target. So we have special case code here... */
2028 if (strstr (bfd_get_target (stdoutput), "-crx") != NULL)
2029 desc_reloc = BFD_RELOC_CRX_NUM32;
2030 else if (strstr (bfd_get_target (stdoutput), "-cr16") != NULL)
2031 desc_reloc = BFD_RELOC_CR16_NUM32;
2032 else
2033 desc_reloc = BFD_RELOC_32;
2035 else
2037 note_size = 36;
2038 desc_size = 16; /* Two 8-byte offsets. */
2039 desc2_offset = 28;
2040 /* FIXME: The BFD backend for the IA64 target does not support the
2041 BFD_RELOC_64, even though it really should. The HPPA backend
2042 has a similar issue, although it does not support BFD_RELOCs at
2043 all! So we have special case code to handle these targets. */
2044 if (strstr (bfd_get_target (stdoutput), "-ia64") != NULL)
2045 desc_reloc = target_big_endian ? BFD_RELOC_IA64_DIR32MSB : BFD_RELOC_IA64_DIR32LSB;
2046 else if (strstr (bfd_get_target (stdoutput), "-hppa") != NULL)
2047 desc_reloc = 80; /* R_PARISC_DIR64. */
2048 else
2049 desc_reloc = BFD_RELOC_64;
2052 /* We have to create a note for *each* code section.
2053 Linker garbage collection might discard some. */
2054 total_size = 0;
2055 note = NULL;
2057 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
2058 if ((bsym = symbol_get_bfdsym (sym)) != NULL
2059 && bsym->flags & BSF_SECTION_SYM
2060 && bsym->section != NULL
2061 /* Skip linkonce sections - we cannot use these section symbols as they may disappear. */
2062 && (bsym->section->flags & (SEC_CODE | SEC_LINK_ONCE)) == SEC_CODE
2063 /* Not all linkonce sections are flagged... */
2064 && !startswith (S_GET_NAME (sym), ".gnu.linkonce"))
2066 /* Create a version note. */
2067 frag_now_fix ();
2068 note = frag_more (note_size);
2069 memset (note, 0, note_size);
2071 if (target_big_endian)
2073 note[3] = 8; /* strlen (name) + 1. */
2074 note[7] = desc_size; /* Two N-byte offsets. */
2075 note[10] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
2076 note[11] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
2078 else
2080 note[0] = 8; /* strlen (name) + 1. */
2081 note[4] = desc_size; /* Two N-byte offsets. */
2082 note[8] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
2083 note[9] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
2086 /* The a1 version number indicates that this note was
2087 generated by the assembler and not the gcc annobin plugin. */
2088 memcpy (note + 12, "GA$\x013a1", 8);
2090 /* Create a relocation to install the start address of the note... */
2091 create_note_reloc (sec, sym, total_size, 20, desc_size / 2, desc_reloc, 0, note);
2093 /* ...and another one to install the end address. */
2094 create_note_reloc (sec, sym, total_size, desc2_offset,
2095 desc_size / 2,
2096 desc_reloc,
2097 bfd_section_size (bsym->section),
2098 note);
2100 /* Mark the section symbol used in relocation so that it will be
2101 included in the symbol table. */
2102 symbol_mark_used_in_reloc (sym);
2104 total_size += note_size;
2105 /* FIXME: Maybe add a note recording the assembler command line and version ? */
2108 /* Install the note(s) into the section. */
2109 if (total_size)
2110 bfd_set_section_contents (stdoutput, sec, (bfd_byte *) note, 0, total_size);
2111 subsegs_finish_section (sec);
2112 relax_segment (seg_info (sec)->frchainP->frch_root, sec, 0);
2113 size_seg (stdoutput, sec, NULL);
2115 #endif /* OBJ_ELF */
2117 /* Write the object file. */
2119 void
2120 write_object_file (void)
2122 struct relax_seg_info rsi;
2123 #ifndef WORKING_DOT_WORD
2124 fragS *fragP; /* Track along all frags. */
2125 #endif
2127 subsegs_finish ();
2129 #ifdef md_pre_output_hook
2130 md_pre_output_hook;
2131 #endif
2133 #ifdef md_pre_relax_hook
2134 md_pre_relax_hook;
2135 #endif
2137 /* From now on, we don't care about sub-segments. Build one frag chain
2138 for each segment. Linked through fr_next. */
2140 /* Remove the sections created by gas for its own purposes. */
2142 int i;
2144 bfd_section_list_remove (stdoutput, reg_section);
2145 bfd_section_list_remove (stdoutput, expr_section);
2146 stdoutput->section_count -= 2;
2147 i = 0;
2148 bfd_map_over_sections (stdoutput, renumber_sections, &i);
2151 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
2153 /* We have two segments. If user gave -R flag, then we must put the
2154 data frags into the text segment. Do this before relaxing so
2155 we know to take advantage of -R and make shorter addresses. */
2156 if (flag_readonly_data_in_text)
2158 merge_data_into_text ();
2161 rsi.pass = 0;
2162 while (1)
2164 #ifndef WORKING_DOT_WORD
2165 /* We need to reset the markers in the broken word list and
2166 associated frags between calls to relax_segment (via
2167 relax_seg). Since the broken word list is global, we do it
2168 once per round, rather than locally in relax_segment for each
2169 segment. */
2170 struct broken_word *brokp;
2172 for (brokp = broken_words;
2173 brokp != (struct broken_word *) NULL;
2174 brokp = brokp->next_broken_word)
2176 brokp->added = 0;
2178 if (brokp->dispfrag != (fragS *) NULL
2179 && brokp->dispfrag->fr_type == rs_broken_word)
2180 brokp->dispfrag->fr_subtype = 0;
2182 #endif
2184 rsi.changed = 0;
2185 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
2186 rsi.pass++;
2187 if (!rsi.changed)
2188 break;
2191 /* Note - Most ports will use the default value of
2192 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
2193 local symbols to be resolved, removing their frag information.
2194 Some ports however, will not have finished relaxing all of
2195 their frags and will still need the local symbol frag
2196 information. These ports can set
2197 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
2198 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
2200 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
2202 /* Relaxation has completed. Freeze all syms. */
2203 finalize_syms = 1;
2205 dwarf2dbg_final_check ();
2207 #ifdef md_post_relax_hook
2208 md_post_relax_hook;
2209 #endif
2211 #ifdef OBJ_ELF
2212 if (IS_ELF)
2213 create_obj_attrs_section ();
2214 #endif
2216 #ifndef WORKING_DOT_WORD
2218 struct broken_word *lie;
2219 struct broken_word **prevP;
2221 prevP = &broken_words;
2222 for (lie = broken_words; lie; lie = lie->next_broken_word)
2223 if (!lie->added)
2225 expressionS exp;
2227 subseg_change (lie->seg, lie->subseg);
2228 exp.X_op = O_subtract;
2229 exp.X_add_symbol = lie->add;
2230 exp.X_op_symbol = lie->sub;
2231 exp.X_add_number = lie->addnum;
2232 #ifdef TC_CONS_FIX_NEW
2233 TC_CONS_FIX_NEW (lie->frag,
2234 lie->word_goes_here - lie->frag->fr_literal,
2235 2, &exp, TC_PARSE_CONS_RETURN_NONE);
2236 #else
2237 fix_new_exp (lie->frag,
2238 lie->word_goes_here - lie->frag->fr_literal,
2239 2, &exp, 0, BFD_RELOC_16);
2240 #endif
2241 *prevP = lie->next_broken_word;
2243 else
2244 prevP = &(lie->next_broken_word);
2246 for (lie = broken_words; lie;)
2248 struct broken_word *untruth;
2249 char *table_ptr;
2250 addressT table_addr;
2251 addressT from_addr, to_addr;
2252 int n, m;
2254 subseg_change (lie->seg, lie->subseg);
2255 fragP = lie->dispfrag;
2257 /* Find out how many broken_words go here. */
2258 n = 0;
2259 for (untruth = lie;
2260 untruth && untruth->dispfrag == fragP;
2261 untruth = untruth->next_broken_word)
2262 if (untruth->added == 1)
2263 n++;
2265 table_ptr = lie->dispfrag->fr_opcode;
2266 table_addr = (lie->dispfrag->fr_address
2267 + (table_ptr - lie->dispfrag->fr_literal));
2268 /* Create the jump around the long jumps. This is a short
2269 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
2270 from_addr = table_addr;
2271 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
2272 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2273 lie->add);
2274 table_ptr += md_short_jump_size;
2275 table_addr += md_short_jump_size;
2277 for (m = 0;
2278 lie && lie->dispfrag == fragP;
2279 m++, lie = lie->next_broken_word)
2281 if (lie->added == 2)
2282 continue;
2283 /* Patch the jump table. */
2284 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2285 untruth && untruth->dispfrag == fragP;
2286 untruth = untruth->next_broken_word)
2288 if (untruth->use_jump == lie)
2290 /* This is the offset from ??? to table_ptr+0.
2291 The target is the same for all users of this
2292 md_long_jump, but the "sub" bases (and hence the
2293 offsets) may be different. */
2294 addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
2295 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
2296 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
2297 #endif
2298 md_number_to_chars (untruth->word_goes_here, to_word, 2);
2302 /* Install the long jump. */
2303 /* This is a long jump from table_ptr+0 to the final target. */
2304 from_addr = table_addr;
2305 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
2306 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2307 lie->add);
2308 table_ptr += md_long_jump_size;
2309 table_addr += md_long_jump_size;
2313 #endif /* not WORKING_DOT_WORD */
2315 /* Resolve symbol values. This needs to be done before processing
2316 the relocations. */
2317 if (symbol_rootP)
2319 symbolS *symp;
2321 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2322 resolve_symbol_value (symp);
2324 resolve_local_symbol_values ();
2325 resolve_reloc_expr_symbols ();
2327 #ifdef OBJ_ELF
2328 if (IS_ELF)
2329 maybe_generate_build_notes ();
2330 #endif
2332 #ifdef tc_frob_file_before_adjust
2333 tc_frob_file_before_adjust ();
2334 #endif
2335 #ifdef obj_frob_file_before_adjust
2336 obj_frob_file_before_adjust ();
2337 #endif
2339 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
2341 #ifdef tc_frob_file_before_fix
2342 tc_frob_file_before_fix ();
2343 #endif
2344 #ifdef obj_frob_file_before_fix
2345 obj_frob_file_before_fix ();
2346 #endif
2348 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
2350 /* Set up symbol table, and write it out. */
2351 if (symbol_rootP)
2353 symbolS *symp;
2354 bool skip_next_symbol = false;
2356 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2358 int punt = 0;
2359 const char *name;
2361 if (skip_next_symbol)
2363 /* Don't do anything besides moving the value of the
2364 symbol from the GAS value-field to the BFD value-field. */
2365 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2366 skip_next_symbol = false;
2367 continue;
2370 if (symbol_mri_common_p (symp))
2372 if (S_IS_EXTERNAL (symp))
2373 as_bad (_("%s: global symbols not supported in common sections"),
2374 S_GET_NAME (symp));
2375 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2376 continue;
2379 name = S_GET_NAME (symp);
2380 if (name)
2382 const char *name2 =
2383 decode_local_label_name ((char *) S_GET_NAME (symp));
2384 /* They only differ if `name' is a fb or dollar local
2385 label name. */
2386 if (name2 != name && ! S_IS_DEFINED (symp))
2387 as_bad (_("local label `%s' is not defined"), name2);
2390 /* Do it again, because adjust_reloc_syms might introduce
2391 more symbols. They'll probably only be section symbols,
2392 but they'll still need to have the values computed. */
2393 resolve_symbol_value (symp);
2395 /* Skip symbols which were equated to undefined or common
2396 symbols. */
2397 if (symbol_equated_reloc_p (symp)
2398 || S_IS_WEAKREFR (symp))
2400 const char *sname = S_GET_NAME (symp);
2402 if (S_IS_COMMON (symp)
2403 && !TC_FAKE_LABEL (sname)
2404 && !S_IS_WEAKREFR (symp))
2406 expressionS *e = symbol_get_value_expression (symp);
2408 as_bad (_("`%s' can't be equated to common symbol `%s'"),
2409 sname, S_GET_NAME (e->X_add_symbol));
2411 if (S_GET_SEGMENT (symp) == reg_section)
2413 /* Report error only if we know the symbol name. */
2414 if (S_GET_NAME (symp) != reg_section->name)
2415 as_bad (_("can't make global register symbol `%s'"),
2416 sname);
2418 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2419 continue;
2422 #ifdef obj_frob_symbol
2423 obj_frob_symbol (symp, punt);
2424 #endif
2425 #ifdef tc_frob_symbol
2426 if (! punt || symbol_used_in_reloc_p (symp))
2427 tc_frob_symbol (symp, punt);
2428 #endif
2430 /* If we don't want to keep this symbol, splice it out of
2431 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2432 want section symbols. Otherwise, we skip local symbols
2433 and symbols that the frob_symbol macros told us to punt,
2434 but we keep such symbols if they are used in relocs. */
2435 if (symp == abs_section_sym
2436 || (! EMIT_SECTION_SYMBOLS
2437 && symbol_section_p (symp))
2438 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2439 opposites. Sometimes the former checks flags and the
2440 latter examines the name... */
2441 || (!S_IS_EXTERNAL (symp)
2442 && (punt || S_IS_LOCAL (symp) ||
2443 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
2444 && ! symbol_used_in_reloc_p (symp)))
2446 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2448 /* After symbol_remove, symbol_next(symp) still returns
2449 the one that came after it in the chain. So we don't
2450 need to do any extra cleanup work here. */
2451 continue;
2454 /* Make sure we really got a value for the symbol. */
2455 if (! symbol_resolved_p (symp))
2457 as_bad (_("can't resolve value for symbol `%s'"),
2458 S_GET_NAME (symp));
2459 symbol_mark_resolved (symp);
2462 /* Set the value into the BFD symbol. Up til now the value
2463 has only been kept in the gas symbolS struct. */
2464 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2466 /* A warning construct is a warning symbol followed by the
2467 symbol warned about. Don't let anything object-format or
2468 target-specific muck with it; it's ready for output. */
2469 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2470 skip_next_symbol = true;
2474 /* Now do any format-specific adjustments to the symbol table, such
2475 as adding file symbols. */
2476 #ifdef tc_adjust_symtab
2477 tc_adjust_symtab ();
2478 #endif
2479 #ifdef obj_adjust_symtab
2480 obj_adjust_symtab ();
2481 #endif
2483 /* Stop if there is an error. */
2484 if (!flag_always_generate_output && had_errors ())
2485 return;
2487 /* Now that all the sizes are known, and contents correct, we can
2488 start writing to the file. */
2489 set_symtab ();
2491 /* If *_frob_file changes the symbol value at this point, it is
2492 responsible for moving the changed value into symp->bsym->value
2493 as well. Hopefully all symbol value changing can be done in
2494 *_frob_symbol. */
2495 #ifdef tc_frob_file
2496 tc_frob_file ();
2497 #endif
2498 #ifdef obj_frob_file
2499 obj_frob_file ();
2500 #endif
2501 #ifdef obj_coff_generate_pdata
2502 obj_coff_generate_pdata ();
2503 #endif
2505 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2507 #ifdef tc_frob_file_after_relocs
2508 tc_frob_file_after_relocs ();
2509 #endif
2510 #ifdef obj_frob_file_after_relocs
2511 obj_frob_file_after_relocs ();
2512 #endif
2514 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
2515 if (IS_ELF && flag_use_elf_stt_common)
2516 stdoutput->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
2517 #endif
2519 /* Once all relocations have been written, we can compress the
2520 contents of the debug sections. This needs to be done before
2521 we start writing any sections, because it will affect the file
2522 layout, which is fixed once we start writing contents. */
2523 if (flag_compress_debug != COMPRESS_DEBUG_NONE)
2525 flagword flags = BFD_COMPRESS;
2526 if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
2527 flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
2528 else if (flag_compress_debug == COMPRESS_DEBUG_ZSTD)
2529 flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
2530 stdoutput->flags |= flags & bfd_applicable_file_flags (stdoutput);
2531 if ((stdoutput->flags & BFD_COMPRESS) != 0)
2532 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
2535 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2538 #ifdef TC_GENERIC_RELAX_TABLE
2539 #ifndef md_generic_table_relax_frag
2540 #define md_generic_table_relax_frag relax_frag
2541 #endif
2543 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2545 long
2546 relax_frag (segT segment, fragS *fragP, long stretch)
2548 const relax_typeS *this_type;
2549 const relax_typeS *start_type;
2550 relax_substateT next_state;
2551 relax_substateT this_state;
2552 offsetT growth;
2553 offsetT aim;
2554 addressT target;
2555 addressT address;
2556 symbolS *symbolP;
2557 const relax_typeS *table;
2559 target = fragP->fr_offset;
2560 address = fragP->fr_address + fragP->fr_fix;
2561 table = TC_GENERIC_RELAX_TABLE;
2562 this_state = fragP->fr_subtype;
2563 start_type = this_type = table + this_state;
2564 symbolP = fragP->fr_symbol;
2566 if (symbolP)
2568 fragS *sym_frag;
2570 sym_frag = symbol_get_frag (symbolP);
2572 #ifndef DIFF_EXPR_OK
2573 know (sym_frag != NULL);
2574 #endif
2575 know (S_GET_SEGMENT (symbolP) != absolute_section
2576 || sym_frag == &zero_address_frag);
2577 target += S_GET_VALUE (symbolP);
2579 /* If SYM_FRAG has yet to be reached on this pass, assume it
2580 will move by STRETCH just as we did, unless there is an
2581 alignment frag between here and SYM_FRAG. An alignment may
2582 well absorb any STRETCH, and we don't want to choose a larger
2583 branch insn by overestimating the needed reach of this
2584 branch. It isn't critical to calculate TARGET exactly; We
2585 know we'll be doing another pass if STRETCH is non-zero. */
2587 if (stretch != 0
2588 && sym_frag->relax_marker != fragP->relax_marker
2589 && S_GET_SEGMENT (symbolP) == segment)
2591 if (stretch < 0
2592 || sym_frag->region == fragP->region)
2593 target += stretch;
2594 /* If we get here we know we have a forward branch. This
2595 relax pass may have stretched previous instructions so
2596 far that omitting STRETCH would make the branch
2597 negative. Don't allow this in case the negative reach is
2598 large enough to require a larger branch instruction. */
2599 else if (target < address)
2600 return 0;
2604 aim = target - address;
2605 #ifdef TC_PCREL_ADJUST
2606 /* Currently only the ns32k and arc needs this. */
2607 aim += TC_PCREL_ADJUST (fragP);
2608 #endif
2610 #ifdef md_prepare_relax_scan
2611 /* Formerly called M68K_AIM_KLUDGE. */
2612 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2613 #endif
2615 if (aim < 0)
2617 /* Look backwards. */
2618 for (next_state = this_type->rlx_more; next_state;)
2619 if (aim >= this_type->rlx_backward)
2620 next_state = 0;
2621 else
2623 /* Grow to next state. */
2624 this_state = next_state;
2625 this_type = table + this_state;
2626 next_state = this_type->rlx_more;
2629 else
2631 /* Look forwards. */
2632 for (next_state = this_type->rlx_more; next_state;)
2633 if (aim <= this_type->rlx_forward)
2634 next_state = 0;
2635 else
2637 /* Grow to next state. */
2638 this_state = next_state;
2639 this_type = table + this_state;
2640 next_state = this_type->rlx_more;
2644 growth = this_type->rlx_length - start_type->rlx_length;
2645 if (growth != 0)
2646 fragP->fr_subtype = this_state;
2647 return growth;
2650 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2652 /* Relax_align. Advance location counter to next address that has 'alignment'
2653 lowest order bits all 0s, return size of adjustment made. */
2654 static relax_addressT
2655 relax_align (relax_addressT address, /* Address now. */
2656 int alignment /* Alignment (binary). */)
2658 relax_addressT mask;
2659 relax_addressT new_address;
2661 mask = ~((relax_addressT) ~0 << alignment);
2662 new_address = (address + mask) & (~mask);
2663 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2664 if (linkrelax)
2665 /* We must provide lots of padding, so the linker can discard it
2666 when needed. The linker will not add extra space, ever. */
2667 new_address += (1 << alignment);
2668 #endif
2669 return (new_address - address);
2672 /* Now we have a segment, not a crowd of sub-segments, we can make
2673 fr_address values.
2675 Relax the frags.
2677 After this, all frags in this segment have addresses that are correct
2678 within the segment. Since segments live in different file addresses,
2679 these frag addresses may not be the same as final object-file
2680 addresses. */
2683 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2685 unsigned long frag_count;
2686 struct frag *fragP;
2687 relax_addressT address;
2688 int region;
2689 int ret;
2691 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2692 subseg_change (segment, 0);
2694 /* For each frag in segment: count and store (a 1st guess of)
2695 fr_address. */
2696 address = 0;
2697 region = 0;
2698 for (frag_count = 0, fragP = segment_frag_root;
2699 fragP;
2700 fragP = fragP->fr_next, frag_count ++)
2702 fragP->region = region;
2703 fragP->relax_marker = 0;
2704 fragP->fr_address = address;
2705 address += fragP->fr_fix;
2707 switch (fragP->fr_type)
2709 case rs_fill:
2710 address += fragP->fr_offset * fragP->fr_var;
2711 break;
2713 case rs_align:
2714 case rs_align_code:
2715 case rs_align_test:
2717 addressT offset = relax_align (address, (int) fragP->fr_offset);
2719 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2720 offset = 0;
2722 if (offset % fragP->fr_var != 0)
2724 as_bad_where (fragP->fr_file, fragP->fr_line,
2725 ngettext ("alignment padding (%lu byte) "
2726 "not a multiple of %ld",
2727 "alignment padding (%lu bytes) "
2728 "not a multiple of %ld",
2729 (unsigned long) offset),
2730 (unsigned long) offset, (long) fragP->fr_var);
2731 offset -= (offset % fragP->fr_var);
2734 address += offset;
2735 region += 1;
2737 break;
2739 case rs_org:
2740 /* Assume .org is nugatory. It will grow with 1st relax. */
2741 region += 1;
2742 break;
2744 case rs_space:
2745 case rs_space_nop:
2746 break;
2748 case rs_machine_dependent:
2749 /* If fr_symbol is an expression, this call to
2750 resolve_symbol_value sets up the correct segment, which will
2751 likely be needed in md_estimate_size_before_relax. */
2752 if (fragP->fr_symbol)
2753 resolve_symbol_value (fragP->fr_symbol);
2755 address += md_estimate_size_before_relax (fragP, segment);
2756 break;
2758 #ifndef WORKING_DOT_WORD
2759 /* Broken words don't concern us yet. */
2760 case rs_broken_word:
2761 break;
2762 #endif
2764 case rs_leb128:
2765 /* Initial guess is always 1; doing otherwise can result in
2766 stable solutions that are larger than the minimum. */
2767 address += fragP->fr_offset = 1;
2768 break;
2770 case rs_cfa:
2771 address += eh_frame_estimate_size_before_relax (fragP);
2772 break;
2774 case rs_dwarf2dbg:
2775 address += dwarf2dbg_estimate_size_before_relax (fragP);
2776 break;
2778 case rs_sframe:
2779 /* Initial estimate can be set to atleast 1 byte. */
2780 address += sframe_estimate_size_before_relax (fragP);
2781 break;
2783 default:
2784 BAD_CASE (fragP->fr_type);
2785 break;
2789 /* Do relax(). */
2791 unsigned long max_iterations;
2793 /* Cumulative address adjustment. */
2794 offsetT stretch;
2796 /* Have we made any adjustment this pass? We can't just test
2797 stretch because one piece of code may have grown and another
2798 shrank. */
2799 int stretched;
2801 /* Most horrible, but gcc may give us some exception data that
2802 is impossible to assemble, of the form
2804 .align 4
2805 .byte 0, 0
2806 .uleb128 end - start
2807 start:
2808 .space 128*128 - 1
2809 .align 4
2810 end:
2812 If the leb128 is two bytes in size, then end-start is 128*128,
2813 which requires a three byte leb128. If the leb128 is three
2814 bytes in size, then end-start is 128*128-1, which requires a
2815 two byte leb128. We work around this dilemma by inserting
2816 an extra 4 bytes of alignment just after the .align. This
2817 works because the data after the align is accessed relative to
2818 the end label.
2820 This counter is used in a tiny state machine to detect
2821 whether a leb128 followed by an align is impossible to
2822 relax. */
2823 int rs_leb128_fudge = 0;
2825 /* We want to prevent going into an infinite loop where one frag grows
2826 depending upon the location of a symbol which is in turn moved by
2827 the growing frag. eg:
2829 foo = .
2830 .org foo+16
2831 foo = .
2833 So we dictate that this algorithm can be at most O2. */
2834 max_iterations = frag_count * frag_count;
2835 /* Check for overflow. */
2836 if (max_iterations < frag_count)
2837 max_iterations = frag_count;
2839 ret = 0;
2842 stretch = 0;
2843 stretched = 0;
2845 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2847 offsetT growth = 0;
2848 addressT was_address;
2849 offsetT offset;
2850 symbolS *symbolP;
2852 fragP->relax_marker ^= 1;
2853 was_address = fragP->fr_address;
2854 address = fragP->fr_address += stretch;
2855 symbolP = fragP->fr_symbol;
2856 offset = fragP->fr_offset;
2858 switch (fragP->fr_type)
2860 case rs_fill: /* .fill never relaxes. */
2861 growth = 0;
2862 break;
2864 #ifndef WORKING_DOT_WORD
2865 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2866 for it I do not want to write it. I do not want to have
2867 anything to do with it. This is not the proper way to
2868 implement this misfeature. */
2869 case rs_broken_word:
2871 struct broken_word *lie;
2872 struct broken_word *untruth;
2874 /* Yes this is ugly (storing the broken_word pointer
2875 in the symbol slot). Still, this whole chunk of
2876 code is ugly, and I don't feel like doing anything
2877 about it. Think of it as stubbornness in action. */
2878 growth = 0;
2879 for (lie = (struct broken_word *) (fragP->fr_symbol);
2880 lie && lie->dispfrag == fragP;
2881 lie = lie->next_broken_word)
2884 if (lie->added)
2885 continue;
2887 offset = (S_GET_VALUE (lie->add)
2888 + lie->addnum
2889 - S_GET_VALUE (lie->sub));
2890 if (offset <= -32768 || offset >= 32767)
2892 if (flag_warn_displacement)
2894 char buf[50];
2896 bfd_sprintf_vma (stdoutput, buf,
2897 (addressT) lie->addnum);
2898 as_warn_where (fragP->fr_file, fragP->fr_line,
2899 _(".word %s-%s+%s didn't fit"),
2900 S_GET_NAME (lie->add),
2901 S_GET_NAME (lie->sub),
2902 buf);
2904 if (fragP->fr_subtype == 0)
2906 fragP->fr_subtype++;
2907 growth += md_short_jump_size;
2910 /* Redirect *all* words of this table with the same
2911 target, lest we have to handle the case where the
2912 same target but with a offset that fits on this
2913 round overflows at the next relaxation round. */
2914 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2915 untruth && untruth->dispfrag == lie->dispfrag;
2916 untruth = untruth->next_broken_word)
2917 if ((symbol_get_frag (untruth->add)
2918 == symbol_get_frag (lie->add))
2919 && (S_GET_VALUE (untruth->add)
2920 == S_GET_VALUE (lie->add)))
2922 untruth->added = 2;
2923 untruth->use_jump = lie;
2926 lie->added = 1;
2927 growth += md_long_jump_size;
2931 break;
2932 } /* case rs_broken_word */
2933 #endif
2934 case rs_align:
2935 case rs_align_code:
2936 case rs_align_test:
2938 addressT oldoff, newoff;
2940 oldoff = relax_align (was_address + fragP->fr_fix,
2941 (int) offset);
2942 newoff = relax_align (address + fragP->fr_fix,
2943 (int) offset);
2945 if (fragP->fr_subtype != 0)
2947 if (oldoff > fragP->fr_subtype)
2948 oldoff = 0;
2949 if (newoff > fragP->fr_subtype)
2950 newoff = 0;
2953 growth = newoff - oldoff;
2955 /* If this align happens to follow a leb128 and
2956 we have determined that the leb128 is bouncing
2957 in size, then break the cycle by inserting an
2958 extra alignment. */
2959 if (growth < 0
2960 && (rs_leb128_fudge & 16) != 0
2961 && (rs_leb128_fudge & 15) >= 2)
2963 segment_info_type *seginfo = seg_info (segment);
2964 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2965 struct frag *newf;
2967 newf = frag_alloc (ob);
2968 obstack_blank_fast (ob, fragP->fr_var);
2969 obstack_finish (ob);
2970 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2971 memcpy (newf->fr_literal,
2972 fragP->fr_literal + fragP->fr_fix,
2973 fragP->fr_var);
2974 newf->fr_type = rs_fill;
2975 newf->fr_address = address + fragP->fr_fix + newoff;
2976 newf->fr_fix = 0;
2977 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2978 / fragP->fr_var);
2979 if (newf->fr_offset * newf->fr_var
2980 != (offsetT) 1 << fragP->fr_offset)
2982 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2983 newf->fr_var = 1;
2985 /* Include size of new frag in GROWTH. */
2986 growth += newf->fr_offset * newf->fr_var;
2987 /* Adjust the new frag address for the amount
2988 we'll add when we process the new frag. */
2989 newf->fr_address -= stretch + growth;
2990 newf->relax_marker ^= 1;
2991 fragP->fr_next = newf;
2992 #ifdef DEBUG
2993 as_warn (_("padding added"));
2994 #endif
2997 break;
2999 case rs_org:
3001 offsetT target = offset;
3002 addressT after;
3004 if (symbolP)
3006 /* Convert from an actual address to an octet offset
3007 into the section. Here it is assumed that the
3008 section's VMA is zero, and can omit subtracting it
3009 from the symbol's value to get the address offset. */
3010 know (S_GET_SEGMENT (symbolP)->vma == 0);
3011 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
3014 know (fragP->fr_next);
3015 after = fragP->fr_next->fr_address + stretch;
3016 growth = target - after;
3018 /* Growth may be negative, but variable part of frag
3019 cannot have fewer than 0 chars. That is, we can't
3020 .org backwards. */
3021 if ((offsetT) (address + fragP->fr_fix) > target)
3023 growth = 0;
3025 /* Don't error on first few frag relax passes.
3026 The symbol might be an expression involving
3027 symbol values from other sections. If those
3028 sections have not yet been processed their
3029 frags will all have zero addresses, so we
3030 will calculate incorrect values for them. The
3031 number of passes we allow before giving an
3032 error is somewhat arbitrary. It should be at
3033 least one, with larger values requiring
3034 increasingly contrived dependencies between
3035 frags to trigger a false error. */
3036 if (pass < 2)
3038 /* Force another pass. */
3039 ret = 1;
3040 break;
3043 as_bad_where (fragP->fr_file, fragP->fr_line,
3044 _("attempt to move .org backwards"));
3046 /* We've issued an error message. Change the
3047 frag to avoid cascading errors. */
3048 fragP->fr_type = rs_align;
3049 fragP->fr_subtype = 0;
3050 fragP->fr_offset = 0;
3051 fragP->fr_fix = after - address;
3054 break;
3056 case rs_space:
3057 case rs_space_nop:
3058 growth = 0;
3059 if (symbolP)
3061 offsetT amount;
3063 amount = S_GET_VALUE (symbolP);
3064 if (S_GET_SEGMENT (symbolP) != absolute_section
3065 || S_IS_COMMON (symbolP)
3066 || ! S_IS_DEFINED (symbolP))
3068 as_bad_where (fragP->fr_file, fragP->fr_line,
3069 _(".space, .nops or .fill specifies non-absolute value"));
3070 /* Prevent repeat of this error message. */
3071 fragP->fr_symbol = 0;
3073 else if (amount < 0)
3075 /* Don't error on first few frag relax passes.
3076 See rs_org comment for a longer explanation. */
3077 if (pass < 2)
3079 ret = 1;
3080 break;
3083 as_warn_where (fragP->fr_file, fragP->fr_line,
3084 _(".space, .nops or .fill with negative value, ignored"));
3085 fragP->fr_symbol = 0;
3087 else
3088 growth = (was_address + fragP->fr_fix + amount
3089 - fragP->fr_next->fr_address);
3091 break;
3093 case rs_machine_dependent:
3094 #ifdef md_relax_frag
3095 growth = md_relax_frag (segment, fragP, stretch);
3096 #else
3097 #ifdef TC_GENERIC_RELAX_TABLE
3098 /* The default way to relax a frag is to look through
3099 TC_GENERIC_RELAX_TABLE. */
3100 growth = md_generic_table_relax_frag (segment, fragP,
3101 stretch);
3102 #endif /* TC_GENERIC_RELAX_TABLE */
3103 #endif
3104 break;
3106 case rs_leb128:
3108 valueT value;
3109 offsetT size;
3111 value = resolve_symbol_value (fragP->fr_symbol);
3112 size = sizeof_leb128 (value, fragP->fr_subtype);
3113 growth = size - fragP->fr_offset;
3114 fragP->fr_offset = size;
3116 break;
3118 case rs_cfa:
3119 growth = eh_frame_relax_frag (fragP);
3120 break;
3122 case rs_dwarf2dbg:
3123 growth = dwarf2dbg_relax_frag (fragP);
3124 break;
3126 case rs_sframe:
3127 growth = sframe_relax_frag (fragP);
3128 break;
3130 default:
3131 BAD_CASE (fragP->fr_type);
3132 break;
3134 if (growth)
3136 stretch += growth;
3137 stretched = 1;
3138 if (fragP->fr_type == rs_leb128)
3139 rs_leb128_fudge += 16;
3140 else if (fragP->fr_type == rs_align
3141 && (rs_leb128_fudge & 16) != 0
3142 && stretch == 0)
3143 rs_leb128_fudge += 16;
3144 else
3145 rs_leb128_fudge = 0;
3149 if (stretch == 0
3150 && (rs_leb128_fudge & 16) == 0
3151 && (rs_leb128_fudge & -16) != 0)
3152 rs_leb128_fudge += 1;
3153 else
3154 rs_leb128_fudge = 0;
3156 /* Until nothing further to relax. */
3157 while (stretched && -- max_iterations);
3159 if (stretched)
3160 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
3161 segment_name (segment));
3164 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
3165 if (fragP->last_fr_address != fragP->fr_address)
3167 fragP->last_fr_address = fragP->fr_address;
3168 ret = 1;
3170 return ret;
3173 void
3174 number_to_chars_bigendian (char *buf, valueT val, int n)
3176 if (n <= 0)
3177 abort ();
3178 while (n--)
3180 buf[n] = val & 0xff;
3181 val >>= 8;
3185 void
3186 number_to_chars_littleendian (char *buf, valueT val, int n)
3188 if (n <= 0)
3189 abort ();
3190 while (n--)
3192 *buf++ = val & 0xff;
3193 val >>= 8;
3197 void
3198 write_print_statistics (FILE *file)
3200 fprintf (file, "fixups: %d\n", n_fixups);
3203 /* For debugging. */
3204 extern int indent_level;
3206 void
3207 print_fixup (fixS *fixp)
3209 indent_level = 1;
3210 fprintf (stderr, "fix %p %s:%d", fixp, fixp->fx_file, fixp->fx_line);
3211 if (fixp->fx_pcrel)
3212 fprintf (stderr, " pcrel");
3213 if (fixp->fx_pcrel_adjust)
3214 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
3215 if (fixp->fx_tcbit)
3216 fprintf (stderr, " tcbit");
3217 if (fixp->fx_done)
3218 fprintf (stderr, " done");
3219 fprintf (stderr, "\n size=%d frag=%p", fixp->fx_size, fixp->fx_frag);
3220 fprintf (stderr, " where=%ld offset=%" PRIx64 " addnumber=%" PRIx64,
3221 fixp->fx_where, (uint64_t) fixp->fx_offset,
3222 (uint64_t) fixp->fx_addnumber);
3223 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
3224 fixp->fx_r_type);
3225 if (fixp->fx_addsy)
3227 fprintf (stderr, "\n +<");
3228 print_symbol_value_1 (stderr, fixp->fx_addsy);
3229 fprintf (stderr, ">");
3231 if (fixp->fx_subsy)
3233 fprintf (stderr, "\n -<");
3234 print_symbol_value_1 (stderr, fixp->fx_subsy);
3235 fprintf (stderr, ">");
3237 fprintf (stderr, "\n");
3238 #ifdef TC_FIX_DATA_PRINT
3239 TC_FIX_DATA_PRINT (stderr, fixp);
3240 #endif