Automatic date update in version.in
[binutils-gdb.git] / gas / write.c
blob18cf18fc8301968794bf1a12ec14d16e22270896
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;
782 valueT val;
784 if (seginfo == NULL)
785 return;
787 dump_section_relocs (abfd, sec, stderr);
789 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
790 if (fixp->fx_done)
791 /* Ignore it. */
793 else if (fixp->fx_addsy)
795 symbolS *sym;
796 asection *symsec;
798 #ifdef DEBUG5
799 fprintf (stderr, "\n\nadjusting fixup:\n");
800 print_fixup (fixp);
801 #endif
803 sym = fixp->fx_addsy;
805 /* All symbols should have already been resolved at this
806 point. It is possible to see unresolved expression
807 symbols, though, since they are not in the regular symbol
808 table. */
809 resolve_symbol_value (sym);
811 if (fixp->fx_subsy != NULL)
812 resolve_symbol_value (fixp->fx_subsy);
814 /* If this symbol is equated to an undefined or common symbol,
815 convert the fixup to being against that symbol. */
816 while (symbol_equated_reloc_p (sym)
817 || S_IS_WEAKREFR (sym))
819 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
820 if (sym == newsym)
821 break;
822 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
823 fixp->fx_addsy = newsym;
824 sym = newsym;
827 if (symbol_mri_common_p (sym))
829 fixp->fx_offset += S_GET_VALUE (sym);
830 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
831 continue;
834 /* If the symbol is undefined, common, weak, or global (ELF
835 shared libs), we can't replace it with the section symbol. */
836 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
837 continue;
839 /* Is there some other (target cpu dependent) reason we can't adjust
840 this one? (E.g. relocations involving function addresses on
841 the PA. */
842 #ifdef tc_fix_adjustable
843 if (! tc_fix_adjustable (fixp))
844 continue;
845 #endif
847 /* Since we're reducing to section symbols, don't attempt to reduce
848 anything that's already using one. */
849 if (symbol_section_p (sym))
851 /* Mark the section symbol used in relocation so that it will
852 be included in the symbol table. */
853 symbol_mark_used_in_reloc (sym);
854 continue;
857 symsec = S_GET_SEGMENT (sym);
858 if (symsec == NULL)
859 abort ();
861 if (bfd_is_abs_section (symsec)
862 || symsec == reg_section)
864 /* The fixup_segment routine normally will not use this
865 symbol in a relocation. */
866 continue;
869 /* Don't try to reduce relocs which refer to non-local symbols
870 in .linkonce sections. It can lead to confusion when a
871 debugging section refers to a .linkonce section. I hope
872 this will always be correct. */
873 if (symsec != sec && ! S_IS_LOCAL (sym))
875 if ((symsec->flags & SEC_LINK_ONCE) != 0
876 || (IS_ELF
877 /* The GNU toolchain uses an extension for ELF: a
878 section beginning with the magic string
879 .gnu.linkonce is a linkonce section. */
880 && startswith (segment_name (symsec), ".gnu.linkonce")))
881 continue;
884 /* Never adjust a reloc against local symbol in a merge section
885 with non-zero addend. */
886 if ((symsec->flags & SEC_MERGE) != 0
887 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
888 continue;
890 /* Never adjust a reloc against TLS local symbol. */
891 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
892 continue;
894 val = S_GET_VALUE (sym);
896 #if defined(TC_AARCH64) && defined(OBJ_COFF)
897 /* coff aarch64 relocation offsets need to be limited to 21bits.
898 This is because addend may need to be stored in an ADRP instruction.
899 In this case the addend cannot be stored down shifted otherwise rounding errors occur. */
900 if ((val + 0x100000) > 0x1fffff)
901 continue;
902 #endif
904 /* We refetch the segment when calling section_symbol, rather
905 than using symsec, because S_GET_VALUE may wind up changing
906 the section when it calls resolve_symbol_value. */
907 fixp->fx_offset += val;
908 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
909 #ifdef DEBUG5
910 fprintf (stderr, "\nadjusted fixup:\n");
911 print_fixup (fixp);
912 #endif
915 dump_section_relocs (abfd, sec, stderr);
918 void
919 as_bad_subtract (fixS *fixp)
921 as_bad_where (fixp->fx_file, fixp->fx_line,
922 _("can't resolve %s - %s"),
923 fixp->fx_addsy ? S_GET_NAME (fixp->fx_addsy) : "0",
924 S_GET_NAME (fixp->fx_subsy));
927 /* fixup_segment()
929 Go through all the fixS's in a segment and see which ones can be
930 handled now. (These consist of fixS where we have since discovered
931 the value of a symbol, or the address of the frag involved.)
932 For each one, call md_apply_fix to put the fix into the frag data.
933 Ones that we couldn't completely handle here will be output later
934 by emit_relocations. */
936 static void
937 fixup_segment (fixS *fixP, segT this_segment)
939 valueT add_number;
940 fragS *fragP;
942 if (fixP != NULL && abs_section_sym == NULL)
943 abs_section_sym = section_symbol (absolute_section);
945 /* If the linker is doing the relaxing, we must not do any fixups.
947 Well, strictly speaking that's not true -- we could do any that
948 are PC-relative and don't cross regions that could change size. */
949 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
951 for (; fixP; fixP = fixP->fx_next)
952 if (!fixP->fx_done)
954 if (fixP->fx_addsy == NULL)
956 /* There was no symbol required by this relocation.
957 However, BFD doesn't really handle relocations
958 without symbols well. So fake up a local symbol in
959 the absolute section. */
960 fixP->fx_addsy = abs_section_sym;
962 symbol_mark_used_in_reloc (fixP->fx_addsy);
963 if (fixP->fx_subsy != NULL)
964 symbol_mark_used_in_reloc (fixP->fx_subsy);
966 return;
969 for (; fixP; fixP = fixP->fx_next)
971 segT add_symbol_segment = absolute_section;
973 #ifdef DEBUG5
974 fprintf (stderr, "\nprocessing fixup:\n");
975 print_fixup (fixP);
976 #endif
978 fragP = fixP->fx_frag;
979 know (fragP);
980 #ifdef TC_VALIDATE_FIX
981 TC_VALIDATE_FIX (fixP, this_segment, skip);
982 #endif
983 add_number = fixP->fx_offset;
985 if (fixP->fx_addsy != NULL)
986 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
988 if (fixP->fx_subsy != NULL)
990 segT sub_symbol_segment;
992 resolve_symbol_value (fixP->fx_subsy);
993 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
995 if (fixP->fx_addsy != NULL
996 && sub_symbol_segment == add_symbol_segment
997 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
998 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
999 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
1001 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1002 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1003 fixP->fx_offset = add_number;
1004 fixP->fx_addsy = NULL;
1005 fixP->fx_subsy = NULL;
1006 #ifdef TC_M68K
1007 /* See the comment below about 68k weirdness. */
1008 fixP->fx_pcrel = 0;
1009 #endif
1011 else if (sub_symbol_segment == absolute_section
1012 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
1013 && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
1015 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1016 fixP->fx_offset = add_number;
1017 fixP->fx_subsy = NULL;
1019 else if (sub_symbol_segment == this_segment
1020 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
1021 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
1023 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1024 fixP->fx_offset = (add_number + fixP->fx_dot_value
1025 + fixP->fx_dot_frag->fr_address);
1027 /* Make it pc-relative. If the back-end code has not
1028 selected a pc-relative reloc, cancel the adjustment
1029 we do later on all pc-relative relocs. */
1030 if (0
1031 #ifdef TC_M68K
1032 /* Do this for m68k even if it's already described
1033 as pc-relative. On the m68k, an operand of
1034 "pc@(foo-.-2)" should address "foo" in a
1035 pc-relative mode. */
1036 || 1
1037 #endif
1038 || !fixP->fx_pcrel)
1039 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
1040 fixP->fx_subsy = NULL;
1041 fixP->fx_pcrel = 1;
1043 else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
1045 if (!md_register_arithmetic
1046 && (add_symbol_segment == reg_section
1047 || sub_symbol_segment == reg_section))
1048 as_bad_where (fixP->fx_file, fixP->fx_line,
1049 _("register value used as expression"));
1050 else
1051 as_bad_subtract (fixP);
1053 else if (sub_symbol_segment != undefined_section
1054 && ! bfd_is_com_section (sub_symbol_segment)
1055 && MD_APPLY_SYM_VALUE (fixP))
1056 add_number -= S_GET_VALUE_WHERE (fixP->fx_subsy, fixP->fx_file, fixP->fx_line);
1059 if (fixP->fx_addsy)
1061 if (add_symbol_segment == this_segment
1062 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1063 && !TC_FORCE_RELOCATION_LOCAL (fixP))
1065 /* This fixup was made when the symbol's segment was
1066 SEG_UNKNOWN, but it is now in the local segment.
1067 So we know how to do the address without relocation. */
1068 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1069 fixP->fx_offset = add_number;
1070 if (fixP->fx_pcrel)
1071 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1072 fixP->fx_addsy = NULL;
1073 fixP->fx_pcrel = 0;
1075 else if (add_symbol_segment == absolute_section
1076 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1077 && !TC_FORCE_RELOCATION_ABS (fixP))
1079 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1080 fixP->fx_offset = add_number;
1081 fixP->fx_addsy = NULL;
1083 else if (add_symbol_segment != undefined_section
1084 && ! bfd_is_com_section (add_symbol_segment)
1085 && MD_APPLY_SYM_VALUE (fixP))
1086 add_number += S_GET_VALUE_WHERE (fixP->fx_addsy, fixP->fx_file, fixP->fx_line);
1089 if (fixP->fx_pcrel)
1091 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1092 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1094 /* There was no symbol required by this relocation.
1095 However, BFD doesn't really handle relocations
1096 without symbols well. So fake up a local symbol in
1097 the absolute section. */
1098 fixP->fx_addsy = abs_section_sym;
1102 if (!fixP->fx_done)
1103 md_apply_fix (fixP, &add_number, this_segment);
1105 if (!fixP->fx_done)
1107 if (fixP->fx_addsy == NULL)
1108 fixP->fx_addsy = abs_section_sym;
1109 symbol_mark_used_in_reloc (fixP->fx_addsy);
1110 if (fixP->fx_subsy != NULL)
1111 symbol_mark_used_in_reloc (fixP->fx_subsy);
1114 if (!fixP->fx_no_overflow && fixP->fx_size != 0)
1116 if (fixP->fx_size < sizeof (valueT))
1118 valueT mask;
1120 mask = 0;
1121 mask--; /* Set all bits to one. */
1122 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1123 if ((add_number & mask) != 0
1124 && (fixP->fx_signed
1125 ? (add_number & mask) != mask
1126 : (-add_number & mask) != 0))
1128 char buf[50], buf2[50];
1129 bfd_sprintf_vma (stdoutput, buf, fragP->fr_address + fixP->fx_where);
1130 if (add_number > 1000)
1131 bfd_sprintf_vma (stdoutput, buf2, add_number);
1132 else
1133 sprintf (buf2, "%ld", (long) add_number);
1134 as_bad_where (fixP->fx_file, fixP->fx_line,
1135 ngettext ("value of %s too large for field "
1136 "of %d byte at %s",
1137 "value of %s too large for field "
1138 "of %d bytes at %s",
1139 fixP->fx_size),
1140 buf2, fixP->fx_size, buf);
1141 } /* Generic error checking. */
1143 #ifdef WARN_SIGNED_OVERFLOW_WORD
1144 /* Warn if a .word value is too large when treated as a signed
1145 number. We already know it is not too negative. This is to
1146 catch over-large switches generated by gcc on the 68k. */
1147 if (!flag_signed_overflow_ok
1148 && fixP->fx_size == 2
1149 && add_number > 0x7fff)
1150 as_bad_where (fixP->fx_file, fixP->fx_line,
1151 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1152 (long) add_number,
1153 (long) (fragP->fr_address + fixP->fx_where));
1154 #endif
1157 #ifdef TC_VALIDATE_FIX
1158 skip: ATTRIBUTE_UNUSED_LABEL
1160 #endif
1161 #ifdef DEBUG5
1162 fprintf (stderr, "result:\n");
1163 print_fixup (fixP);
1164 #endif
1165 } /* For each fixS in this segment. */
1168 static void
1169 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1170 asection *sec,
1171 void *xxx ATTRIBUTE_UNUSED)
1173 segment_info_type *seginfo = seg_info (sec);
1175 fixup_segment (seginfo->fix_root, sec);
1178 static void
1179 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1180 const char *file, unsigned int line)
1182 char *err;
1183 bfd_reloc_status_type s;
1184 asymbol *sym;
1186 if (reloc->sym_ptr_ptr != NULL
1187 && (sym = *reloc->sym_ptr_ptr) != NULL
1188 && (sym->flags & BSF_KEEP) == 0
1189 && ((sym->flags & BSF_SECTION_SYM) == 0
1190 || (EMIT_SECTION_SYMBOLS
1191 && !bfd_is_abs_section (sym->section))))
1192 as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
1194 s = bfd_install_relocation (stdoutput, reloc,
1195 fragp->fr_literal, fragp->fr_address,
1196 sec, &err);
1197 switch (s)
1199 case bfd_reloc_ok:
1200 break;
1201 case bfd_reloc_overflow:
1202 as_bad_where (file, line, _("relocation overflow"));
1203 break;
1204 case bfd_reloc_outofrange:
1205 as_bad_where (file, line, _("relocation out of range"));
1206 break;
1207 default:
1208 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1209 file, line, s);
1213 static fragS *
1214 get_frag_for_reloc (fragS *last_frag,
1215 const segment_info_type *seginfo,
1216 const struct reloc_list *r)
1218 fragS *f;
1220 for (f = last_frag; f != NULL; f = f->fr_next)
1221 if (f->fr_address <= r->u.b.r.address
1222 && r->u.b.r.address < f->fr_address + f->fr_fix)
1223 return f;
1225 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1226 if (f->fr_address <= r->u.b.r.address
1227 && r->u.b.r.address < f->fr_address + f->fr_fix)
1228 return f;
1230 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1231 if (f->fr_address <= r->u.b.r.address
1232 && r->u.b.r.address <= f->fr_address + f->fr_fix)
1233 return f;
1235 as_bad_where (r->file, r->line,
1236 _("reloc not within (fixed part of) section"));
1237 return NULL;
1240 static void
1241 write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1242 void *xxx ATTRIBUTE_UNUSED)
1244 segment_info_type *seginfo = seg_info (sec);
1245 unsigned int n;
1246 struct reloc_list *my_reloc_list, **rp, *r;
1247 arelent **relocs;
1248 fixS *fixp;
1249 fragS *last_frag;
1251 /* If seginfo is NULL, we did not create this section; don't do
1252 anything with it. */
1253 if (seginfo == NULL)
1254 return;
1256 n = 0;
1257 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1258 if (!fixp->fx_done)
1259 n++;
1261 #ifdef RELOC_EXPANSION_POSSIBLE
1262 n *= MAX_RELOC_EXPANSION;
1263 #endif
1265 /* Extract relocs for this section from reloc_list. */
1266 rp = &reloc_list;
1268 my_reloc_list = NULL;
1269 while ((r = *rp) != NULL)
1271 if (r->u.b.sec == sec)
1273 *rp = r->next;
1274 r->next = my_reloc_list;
1275 my_reloc_list = r;
1276 n++;
1278 else
1279 rp = &r->next;
1282 relocs = XCNEWVEC (arelent *, n);
1284 n = 0;
1285 r = my_reloc_list;
1286 last_frag = NULL;
1287 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1289 int fx_size, slack;
1290 valueT loc;
1291 arelent **reloc;
1292 #ifndef RELOC_EXPANSION_POSSIBLE
1293 arelent *rel;
1295 reloc = &rel;
1296 #endif
1298 if (fixp->fx_done)
1299 continue;
1301 fx_size = fixp->fx_size;
1302 slack = TC_FX_SIZE_SLACK (fixp);
1303 if (slack > 0)
1304 fx_size = fx_size > slack ? fx_size - slack : 0;
1305 loc = fixp->fx_where + fx_size;
1306 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1307 as_bad_where (fixp->fx_file, fixp->fx_line,
1308 _("internal error: fixup not contained within frag"));
1310 #ifdef obj_fixup_removed_symbol
1311 if (fixp->fx_addsy && symbol_removed_p (fixp->fx_addsy))
1312 obj_fixup_removed_symbol (&fixp->fx_addsy);
1313 if (fixp->fx_subsy && symbol_removed_p (fixp->fx_subsy))
1314 obj_fixup_removed_symbol (&fixp->fx_subsy);
1315 #endif
1317 #ifndef RELOC_EXPANSION_POSSIBLE
1318 *reloc = tc_gen_reloc (sec, fixp);
1319 #else
1320 reloc = tc_gen_reloc (sec, fixp);
1321 #endif
1323 while (*reloc)
1325 while (r != NULL && r->u.b.r.address < (*reloc)->address)
1327 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1328 if (f != NULL)
1330 last_frag = f;
1331 relocs[n++] = &r->u.b.r;
1332 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1334 r = r->next;
1336 #ifdef GAS_SORT_RELOCS
1337 if (n != 0 && (*reloc)->address < relocs[n - 1]->address)
1339 size_t lo = 0;
1340 size_t hi = n - 1;
1341 bfd_vma look = (*reloc)->address;
1342 while (lo < hi)
1344 size_t mid = (lo + hi) / 2;
1345 if (relocs[mid]->address > look)
1346 hi = mid;
1347 else
1349 lo = mid + 1;
1350 if (relocs[mid]->address == look)
1351 break;
1354 while (lo < hi && relocs[lo]->address == look)
1355 lo++;
1356 memmove (relocs + lo + 1, relocs + lo,
1357 (n - lo) * sizeof (*relocs));
1358 n++;
1359 relocs[lo] = *reloc;
1361 else
1362 #endif
1363 relocs[n++] = *reloc;
1364 install_reloc (sec, *reloc, fixp->fx_frag,
1365 fixp->fx_file, fixp->fx_line);
1366 #ifndef RELOC_EXPANSION_POSSIBLE
1367 break;
1368 #else
1369 reloc++;
1370 #endif
1374 while (r != NULL)
1376 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1377 if (f != NULL)
1379 last_frag = f;
1380 relocs[n++] = &r->u.b.r;
1381 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1383 r = r->next;
1386 #ifdef DEBUG4
1388 unsigned int k, j, nsyms;
1389 asymbol **sympp;
1390 sympp = bfd_get_outsymbols (stdoutput);
1391 nsyms = bfd_get_symcount (stdoutput);
1392 for (k = 0; k < n; k++)
1393 if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1395 for (j = 0; j < nsyms; j++)
1396 if (sympp[j] == *relocs[k]->sym_ptr_ptr)
1397 break;
1398 if (j == nsyms)
1399 abort ();
1402 #endif
1404 bfd_set_reloc (stdoutput, sec, n ? relocs : NULL, n);
1406 #ifdef SET_SECTION_RELOCS
1407 SET_SECTION_RELOCS (sec, relocs, n);
1408 #endif
1410 #ifdef DEBUG3
1412 unsigned int k;
1414 fprintf (stderr, "relocs for sec %s\n", sec->name);
1415 for (k = 0; k < n; k++)
1417 arelent *rel = relocs[k];
1418 asymbol *s = *rel->sym_ptr_ptr;
1419 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1420 k, rel, (unsigned long)rel->address, s->name,
1421 (unsigned long)rel->addend);
1424 #endif
1427 static int
1428 compress_frag (bool use_zstd, void *ctx, const char *contents, int in_size,
1429 fragS **last_newf, struct obstack *ob)
1431 int out_size;
1432 int total_out_size = 0;
1433 fragS *f = *last_newf;
1434 char *next_out;
1435 int avail_out;
1437 /* Call the compression routine repeatedly until it has finished
1438 processing the frag. */
1439 while (in_size > 0)
1441 /* Reserve all the space available in the current chunk.
1442 If none is available, start a new frag. */
1443 avail_out = obstack_room (ob);
1444 if (avail_out <= 0)
1446 obstack_finish (ob);
1447 f = frag_alloc (ob);
1448 f->fr_type = rs_fill;
1449 (*last_newf)->fr_next = f;
1450 *last_newf = f;
1451 avail_out = obstack_room (ob);
1453 if (avail_out <= 0)
1454 as_fatal (_("can't extend frag"));
1455 next_out = obstack_next_free (ob);
1456 obstack_blank_fast (ob, avail_out);
1457 out_size = compress_data (use_zstd, ctx, &contents, &in_size, &next_out,
1458 &avail_out);
1459 if (out_size < 0)
1460 return -1;
1462 f->fr_fix += out_size;
1463 total_out_size += out_size;
1465 /* Return unused space. */
1466 if (avail_out > 0)
1467 obstack_blank_fast (ob, -avail_out);
1470 return total_out_size;
1473 static void
1474 compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1476 segment_info_type *seginfo = seg_info (sec);
1477 bfd_size_type uncompressed_size = sec->size;
1478 flagword flags = bfd_section_flags (sec);
1480 if (seginfo == NULL
1481 || uncompressed_size < 32
1482 || (flags & SEC_HAS_CONTENTS) == 0)
1483 return;
1485 const char *section_name = bfd_section_name (sec);
1486 if (!startswith (section_name, ".debug_")
1487 && !startswith (section_name, ".gnu.debuglto_.debug_")
1488 && !startswith (section_name, ".gnu.linkonce.wi."))
1489 return;
1491 bool use_zstd = abfd->flags & BFD_COMPRESS_ZSTD;
1492 void *ctx = compress_init (use_zstd);
1493 if (ctx == NULL)
1494 return;
1496 unsigned int header_size;
1497 if ((abfd->flags & BFD_COMPRESS_GABI) == 0)
1498 header_size = 12;
1499 else
1500 header_size = bfd_get_compression_header_size (stdoutput, NULL);
1502 /* Create a new frag to contain the compression header. */
1503 struct obstack *ob = &seginfo->frchainP->frch_obstack;
1504 fragS *first_newf = frag_alloc (ob);
1505 if (obstack_room (ob) < header_size)
1506 first_newf = frag_alloc (ob);
1507 if (obstack_room (ob) < header_size)
1508 as_fatal (ngettext ("can't extend frag %lu char",
1509 "can't extend frag %lu chars",
1510 (unsigned long) header_size),
1511 (unsigned long) header_size);
1512 fragS *last_newf = first_newf;
1513 obstack_blank_fast (ob, header_size);
1514 last_newf->fr_type = rs_fill;
1515 last_newf->fr_fix = header_size;
1516 char *header = last_newf->fr_literal;
1517 bfd_size_type compressed_size = header_size;
1519 /* Stream the frags through the compression engine, adding new frags
1520 as necessary to accommodate the compressed output. */
1521 for (fragS *f = seginfo->frchainP->frch_root;
1523 f = f->fr_next)
1525 offsetT fill_size;
1526 char *fill_literal;
1527 offsetT count;
1528 int out_size;
1530 gas_assert (f->fr_type == rs_fill);
1531 if (f->fr_fix)
1533 out_size = compress_frag (use_zstd, ctx, f->fr_literal, f->fr_fix,
1534 &last_newf, ob);
1535 if (out_size < 0)
1536 return;
1537 compressed_size += out_size;
1539 fill_literal = f->fr_literal + f->fr_fix;
1540 fill_size = f->fr_var;
1541 count = f->fr_offset;
1542 gas_assert (count >= 0);
1543 if (fill_size && count)
1545 while (count--)
1547 out_size = compress_frag (use_zstd, ctx, fill_literal,
1548 (int)fill_size, &last_newf, ob);
1549 if (out_size < 0)
1550 return;
1551 compressed_size += out_size;
1556 /* Flush the compression state. */
1557 for (;;)
1559 int avail_out;
1560 char *next_out;
1561 int out_size;
1563 /* Reserve all the space available in the current chunk.
1564 If none is available, start a new frag. */
1565 avail_out = obstack_room (ob);
1566 if (avail_out <= 0)
1568 fragS *newf;
1570 obstack_finish (ob);
1571 newf = frag_alloc (ob);
1572 newf->fr_type = rs_fill;
1573 last_newf->fr_next = newf;
1574 last_newf = newf;
1575 avail_out = obstack_room (ob);
1577 if (avail_out <= 0)
1578 as_fatal (_("can't extend frag"));
1579 next_out = obstack_next_free (ob);
1580 obstack_blank_fast (ob, avail_out);
1581 int x = compress_finish (use_zstd, ctx, &next_out, &avail_out, &out_size);
1582 if (x < 0)
1583 return;
1585 last_newf->fr_fix += out_size;
1586 compressed_size += out_size;
1588 /* Return unused space. */
1589 if (avail_out > 0)
1590 obstack_blank_fast (ob, -avail_out);
1592 if (x == 0)
1593 break;
1596 /* PR binutils/18087: If compression didn't make the section smaller,
1597 just keep it uncompressed. */
1598 if (compressed_size >= uncompressed_size)
1599 return;
1601 /* Replace the uncompressed frag list with the compressed frag list. */
1602 seginfo->frchainP->frch_root = first_newf;
1603 seginfo->frchainP->frch_last = last_newf;
1605 /* Update the section size and its name. */
1606 bfd_update_compression_header (abfd, (bfd_byte *) header, sec);
1607 bool x = bfd_set_section_size (sec, compressed_size);
1608 gas_assert (x);
1609 if ((abfd->flags & BFD_COMPRESS_GABI) == 0
1610 && section_name[1] == 'd')
1612 char *compressed_name = bfd_debug_name_to_zdebug (abfd, section_name);
1613 bfd_rename_section (sec, compressed_name);
1617 #ifndef md_generate_nops
1618 /* Genenerate COUNT bytes of no-op instructions to WHERE. A target
1619 backend must override this with proper no-op instructions. */
1621 static void
1622 md_generate_nops (fragS *f ATTRIBUTE_UNUSED,
1623 char *where ATTRIBUTE_UNUSED,
1624 offsetT count ATTRIBUTE_UNUSED,
1625 int control ATTRIBUTE_UNUSED)
1627 as_bad (_("unimplemented .nops directive"));
1629 #endif
1631 static void
1632 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1633 asection *sec,
1634 void *xxx ATTRIBUTE_UNUSED)
1636 segment_info_type *seginfo = seg_info (sec);
1637 addressT offset = 0;
1638 fragS *f;
1640 /* Write out the frags. */
1641 if (seginfo == NULL
1642 || !(bfd_section_flags (sec) & SEC_HAS_CONTENTS))
1643 return;
1645 for (f = seginfo->frchainP->frch_root;
1647 f = f->fr_next)
1649 int x;
1650 addressT fill_size;
1651 char *fill_literal;
1652 offsetT count;
1654 gas_assert (f->fr_type == rs_fill || f->fr_type == rs_fill_nop);
1655 if (f->fr_fix)
1657 x = bfd_set_section_contents (stdoutput, sec,
1658 f->fr_literal, (file_ptr) offset,
1659 (bfd_size_type) f->fr_fix);
1660 if (!x)
1661 as_fatal (ngettext ("can't write %ld byte "
1662 "to section %s of %s: '%s'",
1663 "can't write %ld bytes "
1664 "to section %s of %s: '%s'",
1665 (long) f->fr_fix),
1666 (long) f->fr_fix,
1667 bfd_section_name (sec), bfd_get_filename (stdoutput),
1668 bfd_errmsg (bfd_get_error ()));
1669 offset += f->fr_fix;
1672 fill_size = f->fr_var;
1673 count = f->fr_offset;
1674 fill_literal = f->fr_literal + f->fr_fix;
1676 if (f->fr_type == rs_fill_nop)
1678 gas_assert (count >= 0 && fill_size == 1);
1679 if (count > 0)
1681 char *buf = xmalloc (count);
1682 md_generate_nops (f, buf, count, *fill_literal);
1683 x = bfd_set_section_contents
1684 (stdoutput, sec, buf, (file_ptr) offset,
1685 (bfd_size_type) count);
1686 if (!x)
1687 as_fatal (ngettext ("can't fill %ld byte "
1688 "in section %s of %s: '%s'",
1689 "can't fill %ld bytes "
1690 "in section %s of %s: '%s'",
1691 (long) count),
1692 (long) count,
1693 bfd_section_name (sec),
1694 bfd_get_filename (stdoutput),
1695 bfd_errmsg (bfd_get_error ()));
1696 offset += count;
1697 #ifndef NO_LISTING
1698 if (listing & LISTING_LISTING)
1699 f->fr_opcode = buf;
1700 else
1701 #endif
1702 free (buf);
1704 continue;
1707 gas_assert (count >= 0);
1708 if (fill_size && count)
1710 char buf[256];
1711 if (fill_size > sizeof (buf))
1713 /* Do it the old way. Can this ever happen? */
1714 while (count--)
1716 x = bfd_set_section_contents (stdoutput, sec,
1717 fill_literal,
1718 (file_ptr) offset,
1719 (bfd_size_type) fill_size);
1720 if (!x)
1721 as_fatal (ngettext ("can't fill %ld byte "
1722 "in section %s of %s: '%s'",
1723 "can't fill %ld bytes "
1724 "in section %s of %s: '%s'",
1725 (long) fill_size),
1726 (long) fill_size,
1727 bfd_section_name (sec),
1728 bfd_get_filename (stdoutput),
1729 bfd_errmsg (bfd_get_error ()));
1730 offset += fill_size;
1733 else
1735 /* Build a buffer full of fill objects and output it as
1736 often as necessary. This saves on the overhead of
1737 potentially lots of bfd_set_section_contents calls. */
1738 int n_per_buf, i;
1739 if (fill_size == 1)
1741 n_per_buf = sizeof (buf);
1742 memset (buf, *fill_literal, n_per_buf);
1744 else
1746 char *bufp;
1747 n_per_buf = sizeof (buf) / fill_size;
1748 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1749 memcpy (bufp, fill_literal, fill_size);
1751 for (; count > 0; count -= n_per_buf)
1753 n_per_buf = n_per_buf > count ? count : n_per_buf;
1754 x = bfd_set_section_contents
1755 (stdoutput, sec, buf, (file_ptr) offset,
1756 (bfd_size_type) n_per_buf * fill_size);
1757 if (!x)
1758 as_fatal (ngettext ("can't fill %ld byte "
1759 "in section %s of %s: '%s'",
1760 "can't fill %ld bytes "
1761 "in section %s of %s: '%s'",
1762 (long) (n_per_buf * fill_size)),
1763 (long) (n_per_buf * fill_size),
1764 bfd_section_name (sec),
1765 bfd_get_filename (stdoutput),
1766 bfd_errmsg (bfd_get_error ()));
1767 offset += n_per_buf * fill_size;
1774 static void
1775 merge_data_into_text (void)
1777 seg_info (text_section)->frchainP->frch_last->fr_next =
1778 seg_info (data_section)->frchainP->frch_root;
1779 seg_info (text_section)->frchainP->frch_last =
1780 seg_info (data_section)->frchainP->frch_last;
1781 seg_info (data_section)->frchainP = 0;
1784 static void
1785 set_symtab (void)
1787 int nsyms;
1788 asymbol **asympp;
1789 symbolS *symp;
1790 bool result;
1792 /* Count symbols. We can't rely on a count made by the loop in
1793 write_object_file, because *_frob_file may add a new symbol or
1794 two. Generate unused section symbols only if needed. */
1795 nsyms = 0;
1796 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1797 if (!symbol_removed_p (symp)
1798 && (bfd_keep_unused_section_symbols (stdoutput)
1799 || !symbol_section_p (symp)
1800 || symbol_used_in_reloc_p (symp)))
1801 nsyms++;
1803 if (nsyms)
1805 int i;
1806 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1808 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1809 symp = symbol_rootP;
1810 for (i = 0; i < nsyms; symp = symbol_next (symp))
1811 if (!symbol_removed_p (symp)
1812 && (bfd_keep_unused_section_symbols (stdoutput)
1813 || !symbol_section_p (symp)
1814 || symbol_used_in_reloc_p (symp)))
1816 asympp[i] = symbol_get_bfdsym (symp);
1817 if (asympp[i]->flags != BSF_SECTION_SYM
1818 || !(bfd_is_const_section (asympp[i]->section)
1819 && asympp[i]->section->symbol == asympp[i]))
1820 asympp[i]->flags |= BSF_KEEP;
1821 symbol_mark_written (symp);
1822 /* Include this section symbol in the symbol table. */
1823 if (symbol_section_p (symp))
1824 asympp[i]->flags |= BSF_SECTION_SYM_USED;
1825 i++;
1828 else
1829 asympp = 0;
1830 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1831 gas_assert (result);
1832 symbol_table_frozen = 1;
1835 /* Finish the subsegments. After every sub-segment, we fake an
1836 ".align ...". This conforms to BSD4.2 brain-damage. We then fake
1837 ".fill 0" because that is the kind of frag that requires least
1838 thought. ".align" frags like to have a following frag since that
1839 makes calculating their intended length trivial. */
1841 #ifndef SUB_SEGMENT_ALIGN
1842 #ifdef HANDLE_ALIGN
1843 /* The last subsegment gets an alignment corresponding to the alignment
1844 of the section. This allows proper nop-filling at the end of
1845 code-bearing sections. */
1846 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1847 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
1848 && !do_not_pad_sections_to_alignment \
1849 ? get_recorded_alignment (SEG) \
1850 : 0)
1851 #else
1852 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1853 #endif
1854 #endif
1856 static void
1857 subsegs_finish_section (asection *s)
1859 struct frchain *frchainP;
1860 segment_info_type *seginfo = seg_info (s);
1861 if (!seginfo)
1862 return;
1864 for (frchainP = seginfo->frchainP;
1865 frchainP != NULL;
1866 frchainP = frchainP->frch_next)
1868 int alignment;
1870 subseg_set (s, frchainP->frch_subseg);
1872 /* This now gets called even if we had errors. In that case,
1873 any alignment is meaningless, and, moreover, will look weird
1874 if we are generating a listing. */
1875 if (had_errors ())
1876 do_not_pad_sections_to_alignment = 1;
1878 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1879 if ((bfd_section_flags (now_seg) & SEC_MERGE)
1880 && now_seg->entsize)
1882 unsigned int entsize = now_seg->entsize;
1883 int entalign = 0;
1885 while ((entsize & 1) == 0)
1887 ++entalign;
1888 entsize >>= 1;
1891 if (entalign > alignment)
1892 alignment = entalign;
1895 if (subseg_text_p (now_seg))
1896 frag_align_code (alignment, 0);
1897 else
1898 frag_align (alignment, 0, 0);
1900 /* frag_align will have left a new frag.
1901 Use this last frag for an empty ".fill".
1903 For this segment ...
1904 Create a last frag. Do not leave a "being filled in frag". */
1905 frag_wane (frag_now);
1906 frag_now->fr_fix = 0;
1907 know (frag_now->fr_next == NULL);
1911 static void
1912 subsegs_finish (void)
1914 asection *s;
1916 for (s = stdoutput->sections; s; s = s->next)
1917 subsegs_finish_section (s);
1920 #ifdef OBJ_ELF
1921 static void
1922 create_obj_attrs_section (void)
1924 segT s;
1925 char *p;
1926 offsetT size;
1927 const char *name;
1929 size = bfd_elf_obj_attr_size (stdoutput);
1930 if (size == 0)
1931 return;
1933 name = get_elf_backend_data (stdoutput)->obj_attrs_section;
1934 if (!name)
1935 name = ".gnu.attributes";
1936 s = subseg_new (name, 0);
1937 elf_section_type (s)
1938 = get_elf_backend_data (stdoutput)->obj_attrs_section_type;
1939 bfd_set_section_flags (s, SEC_READONLY | SEC_DATA);
1940 frag_now_fix ();
1941 p = frag_more (size);
1942 bfd_elf_set_obj_attr_contents (stdoutput, (bfd_byte *)p, size);
1944 subsegs_finish_section (s);
1945 relax_segment (seg_info (s)->frchainP->frch_root, s, 0);
1946 size_seg (stdoutput, s, NULL);
1949 /* Create a relocation against an entry in a GNU Build attribute section. */
1951 static void
1952 create_note_reloc (segT sec,
1953 symbolS * sym,
1954 bfd_size_type note_offset,
1955 bfd_size_type desc2_offset,
1956 offsetT desc2_size,
1957 int reloc_type,
1958 bfd_vma addend,
1959 char * note)
1961 struct reloc_list * reloc;
1963 reloc = XNEW (struct reloc_list);
1965 /* We create a .b type reloc as resolve_reloc_expr_symbols() has already been called. */
1966 reloc->u.b.sec = sec;
1967 reloc->u.b.s = symbol_get_bfdsym (sym);
1968 reloc->u.b.r.sym_ptr_ptr = & reloc->u.b.s;
1969 reloc->u.b.r.address = note_offset + desc2_offset;
1970 reloc->u.b.r.addend = addend;
1971 reloc->u.b.r.howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1973 if (reloc->u.b.r.howto == NULL)
1975 as_bad (_("unable to create reloc for build note"));
1976 return;
1979 reloc->file = N_("<gnu build note>");
1980 reloc->line = 0;
1982 reloc->next = reloc_list;
1983 reloc_list = reloc;
1985 /* For REL relocs, store the addend in the section. */
1986 if (! sec->use_rela_p
1987 /* The SH target is a special case that uses RELA relocs
1988 but still stores the addend in the word being relocated. */
1989 || strstr (bfd_get_target (stdoutput), "-sh") != NULL)
1991 offsetT i;
1993 /* Zero out the addend, since it is now stored in the note. */
1994 reloc->u.b.r.addend = 0;
1996 if (target_big_endian)
1998 for (i = desc2_size; addend != 0 && i > 0; addend >>= 8, i--)
1999 note[desc2_offset + i - 1] = (addend & 0xff);
2001 else
2003 for (i = 0; addend != 0 && i < desc2_size; addend >>= 8, i++)
2004 note[desc2_offset + i] = (addend & 0xff);
2009 static void
2010 maybe_generate_build_notes (void)
2012 segT sec;
2013 char * note;
2014 offsetT note_size;
2015 offsetT total_size;
2016 offsetT desc_size;
2017 offsetT desc2_offset;
2018 int desc_reloc;
2019 symbolS * sym;
2020 asymbol * bsym;
2022 if (! flag_generate_build_notes
2023 || bfd_get_section_by_name (stdoutput,
2024 GNU_BUILD_ATTRS_SECTION_NAME) != NULL)
2025 return;
2027 /* Create a GNU Build Attribute section. */
2028 sec = subseg_new (GNU_BUILD_ATTRS_SECTION_NAME, false);
2029 elf_section_type (sec) = SHT_NOTE;
2030 bfd_set_section_flags (sec, (SEC_READONLY | SEC_HAS_CONTENTS | SEC_DATA
2031 | SEC_OCTETS));
2032 bfd_set_section_alignment (sec, 2);
2034 /* Work out the size of the notes that we will create,
2035 and the relocation we should use. */
2036 if (bfd_arch_bits_per_address (stdoutput) <= 32)
2038 note_size = 28;
2039 desc_size = 8; /* Two 4-byte offsets. */
2040 desc2_offset = 24;
2042 /* FIXME: The BFD backend for the CRX target does not support the
2043 BFD_RELOC_32, even though it really should. Likewise for the
2044 CR16 target. So we have special case code here... */
2045 if (strstr (bfd_get_target (stdoutput), "-crx") != NULL)
2046 desc_reloc = BFD_RELOC_CRX_NUM32;
2047 else if (strstr (bfd_get_target (stdoutput), "-cr16") != NULL)
2048 desc_reloc = BFD_RELOC_CR16_NUM32;
2049 else
2050 desc_reloc = BFD_RELOC_32;
2052 else
2054 note_size = 36;
2055 desc_size = 16; /* Two 8-byte offsets. */
2056 desc2_offset = 28;
2057 /* FIXME: The BFD backend for the IA64 target does not support the
2058 BFD_RELOC_64, even though it really should. The HPPA backend
2059 has a similar issue, although it does not support BFD_RELOCs at
2060 all! So we have special case code to handle these targets. */
2061 if (strstr (bfd_get_target (stdoutput), "-ia64") != NULL)
2062 desc_reloc = target_big_endian ? BFD_RELOC_IA64_DIR32MSB : BFD_RELOC_IA64_DIR32LSB;
2063 else if (strstr (bfd_get_target (stdoutput), "-hppa") != NULL)
2064 desc_reloc = 80; /* R_PARISC_DIR64. */
2065 else
2066 desc_reloc = BFD_RELOC_64;
2069 /* We have to create a note for *each* code section.
2070 Linker garbage collection might discard some. */
2071 total_size = 0;
2072 note = NULL;
2074 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
2075 if ((bsym = symbol_get_bfdsym (sym)) != NULL
2076 && bsym->flags & BSF_SECTION_SYM
2077 && bsym->section != NULL
2078 /* Skip linkonce sections - we cannot use these section symbols as they may disappear. */
2079 && (bsym->section->flags & (SEC_CODE | SEC_LINK_ONCE)) == SEC_CODE
2080 /* Not all linkonce sections are flagged... */
2081 && !startswith (S_GET_NAME (sym), ".gnu.linkonce"))
2083 /* Create a version note. */
2084 frag_now_fix ();
2085 note = frag_more (note_size);
2086 memset (note, 0, note_size);
2088 if (target_big_endian)
2090 note[3] = 8; /* strlen (name) + 1. */
2091 note[7] = desc_size; /* Two N-byte offsets. */
2092 note[10] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
2093 note[11] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
2095 else
2097 note[0] = 8; /* strlen (name) + 1. */
2098 note[4] = desc_size; /* Two N-byte offsets. */
2099 note[8] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
2100 note[9] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
2103 /* The a1 version number indicates that this note was
2104 generated by the assembler and not the gcc annobin plugin. */
2105 memcpy (note + 12, "GA$\x013a1", 8);
2107 /* Create a relocation to install the start address of the note... */
2108 create_note_reloc (sec, sym, total_size, 20, desc_size / 2, desc_reloc, 0, note);
2110 /* ...and another one to install the end address. */
2111 create_note_reloc (sec, sym, total_size, desc2_offset,
2112 desc_size / 2,
2113 desc_reloc,
2114 bfd_section_size (bsym->section),
2115 note);
2117 /* Mark the section symbol used in relocation so that it will be
2118 included in the symbol table. */
2119 symbol_mark_used_in_reloc (sym);
2121 total_size += note_size;
2122 /* FIXME: Maybe add a note recording the assembler command line and version ? */
2125 /* Install the note(s) into the section. */
2126 if (total_size)
2127 bfd_set_section_contents (stdoutput, sec, (bfd_byte *) note, 0, total_size);
2128 subsegs_finish_section (sec);
2129 relax_segment (seg_info (sec)->frchainP->frch_root, sec, 0);
2130 size_seg (stdoutput, sec, NULL);
2132 #endif /* OBJ_ELF */
2134 /* Write the object file. */
2136 void
2137 write_object_file (void)
2139 struct relax_seg_info rsi;
2140 #ifndef WORKING_DOT_WORD
2141 fragS *fragP; /* Track along all frags. */
2142 #endif
2144 subsegs_finish ();
2146 #ifdef md_pre_output_hook
2147 md_pre_output_hook;
2148 #endif
2150 #ifdef md_pre_relax_hook
2151 md_pre_relax_hook;
2152 #endif
2154 /* From now on, we don't care about sub-segments. Build one frag chain
2155 for each segment. Linked through fr_next. */
2157 /* Remove the sections created by gas for its own purposes. */
2159 int i;
2161 bfd_section_list_remove (stdoutput, reg_section);
2162 bfd_section_list_remove (stdoutput, expr_section);
2163 stdoutput->section_count -= 2;
2164 i = 0;
2165 bfd_map_over_sections (stdoutput, renumber_sections, &i);
2168 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
2170 /* We have two segments. If user gave -R flag, then we must put the
2171 data frags into the text segment. Do this before relaxing so
2172 we know to take advantage of -R and make shorter addresses. */
2173 if (flag_readonly_data_in_text)
2175 merge_data_into_text ();
2178 rsi.pass = 0;
2179 while (1)
2181 #ifndef WORKING_DOT_WORD
2182 /* We need to reset the markers in the broken word list and
2183 associated frags between calls to relax_segment (via
2184 relax_seg). Since the broken word list is global, we do it
2185 once per round, rather than locally in relax_segment for each
2186 segment. */
2187 struct broken_word *brokp;
2189 for (brokp = broken_words;
2190 brokp != (struct broken_word *) NULL;
2191 brokp = brokp->next_broken_word)
2193 brokp->added = 0;
2195 if (brokp->dispfrag != (fragS *) NULL
2196 && brokp->dispfrag->fr_type == rs_broken_word)
2197 brokp->dispfrag->fr_subtype = 0;
2199 #endif
2201 rsi.changed = 0;
2202 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
2203 rsi.pass++;
2204 if (!rsi.changed)
2205 break;
2208 /* Note - Most ports will use the default value of
2209 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
2210 local symbols to be resolved, removing their frag information.
2211 Some ports however, will not have finished relaxing all of
2212 their frags and will still need the local symbol frag
2213 information. These ports can set
2214 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
2215 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
2217 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
2219 /* Relaxation has completed. Freeze all syms. */
2220 finalize_syms = 1;
2222 dwarf2dbg_final_check ();
2224 #ifdef md_post_relax_hook
2225 md_post_relax_hook;
2226 #endif
2228 #ifdef OBJ_ELF
2229 if (IS_ELF)
2230 create_obj_attrs_section ();
2231 #endif
2233 #ifndef WORKING_DOT_WORD
2235 struct broken_word *lie;
2236 struct broken_word **prevP;
2238 prevP = &broken_words;
2239 for (lie = broken_words; lie; lie = lie->next_broken_word)
2240 if (!lie->added)
2242 expressionS exp;
2244 subseg_change (lie->seg, lie->subseg);
2245 exp.X_op = O_subtract;
2246 exp.X_add_symbol = lie->add;
2247 exp.X_op_symbol = lie->sub;
2248 exp.X_add_number = lie->addnum;
2249 #ifdef TC_CONS_FIX_NEW
2250 TC_CONS_FIX_NEW (lie->frag,
2251 lie->word_goes_here - lie->frag->fr_literal,
2252 2, &exp, TC_PARSE_CONS_RETURN_NONE);
2253 #else
2254 fix_new_exp (lie->frag,
2255 lie->word_goes_here - lie->frag->fr_literal,
2256 2, &exp, 0, BFD_RELOC_16);
2257 #endif
2258 *prevP = lie->next_broken_word;
2260 else
2261 prevP = &(lie->next_broken_word);
2263 for (lie = broken_words; lie;)
2265 struct broken_word *untruth;
2266 char *table_ptr;
2267 addressT table_addr;
2268 addressT from_addr, to_addr;
2269 int n, m;
2271 subseg_change (lie->seg, lie->subseg);
2272 fragP = lie->dispfrag;
2274 /* Find out how many broken_words go here. */
2275 n = 0;
2276 for (untruth = lie;
2277 untruth && untruth->dispfrag == fragP;
2278 untruth = untruth->next_broken_word)
2279 if (untruth->added == 1)
2280 n++;
2282 table_ptr = lie->dispfrag->fr_opcode;
2283 table_addr = (lie->dispfrag->fr_address
2284 + (table_ptr - lie->dispfrag->fr_literal));
2285 /* Create the jump around the long jumps. This is a short
2286 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
2287 from_addr = table_addr;
2288 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
2289 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2290 lie->add);
2291 table_ptr += md_short_jump_size;
2292 table_addr += md_short_jump_size;
2294 for (m = 0;
2295 lie && lie->dispfrag == fragP;
2296 m++, lie = lie->next_broken_word)
2298 if (lie->added == 2)
2299 continue;
2300 /* Patch the jump table. */
2301 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2302 untruth && untruth->dispfrag == fragP;
2303 untruth = untruth->next_broken_word)
2305 if (untruth->use_jump == lie)
2307 /* This is the offset from ??? to table_ptr+0.
2308 The target is the same for all users of this
2309 md_long_jump, but the "sub" bases (and hence the
2310 offsets) may be different. */
2311 addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
2312 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
2313 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
2314 #endif
2315 md_number_to_chars (untruth->word_goes_here, to_word, 2);
2319 /* Install the long jump. */
2320 /* This is a long jump from table_ptr+0 to the final target. */
2321 from_addr = table_addr;
2322 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
2323 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
2324 lie->add);
2325 table_ptr += md_long_jump_size;
2326 table_addr += md_long_jump_size;
2330 #endif /* not WORKING_DOT_WORD */
2332 /* Resolve symbol values. This needs to be done before processing
2333 the relocations. */
2334 if (symbol_rootP)
2336 symbolS *symp;
2338 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2339 resolve_symbol_value (symp);
2341 resolve_local_symbol_values ();
2342 resolve_reloc_expr_symbols ();
2344 #ifdef OBJ_ELF
2345 if (IS_ELF)
2346 maybe_generate_build_notes ();
2347 #endif
2349 #ifdef tc_frob_file_before_adjust
2350 tc_frob_file_before_adjust ();
2351 #endif
2352 #ifdef obj_frob_file_before_adjust
2353 obj_frob_file_before_adjust ();
2354 #endif
2356 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
2358 #ifdef tc_frob_file_before_fix
2359 tc_frob_file_before_fix ();
2360 #endif
2361 #ifdef obj_frob_file_before_fix
2362 obj_frob_file_before_fix ();
2363 #endif
2365 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
2367 /* Set up symbol table, and write it out. */
2368 if (symbol_rootP)
2370 symbolS *symp;
2371 bool skip_next_symbol = false;
2373 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2375 int punt = 0;
2376 const char *name;
2378 if (skip_next_symbol)
2380 /* Don't do anything besides moving the value of the
2381 symbol from the GAS value-field to the BFD value-field. */
2382 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2383 skip_next_symbol = false;
2384 continue;
2387 if (symbol_mri_common_p (symp))
2389 if (S_IS_EXTERNAL (symp))
2390 as_bad (_("%s: global symbols not supported in common sections"),
2391 S_GET_NAME (symp));
2392 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2393 continue;
2396 name = S_GET_NAME (symp);
2397 if (name)
2399 const char *name2 =
2400 decode_local_label_name ((char *) S_GET_NAME (symp));
2401 /* They only differ if `name' is a fb or dollar local
2402 label name. */
2403 if (name2 != name && ! S_IS_DEFINED (symp))
2404 as_bad (_("local label `%s' is not defined"), name2);
2407 /* Do it again, because adjust_reloc_syms might introduce
2408 more symbols. They'll probably only be section symbols,
2409 but they'll still need to have the values computed. */
2410 resolve_symbol_value (symp);
2412 /* Skip symbols which were equated to undefined or common
2413 symbols. */
2414 if (symbol_equated_reloc_p (symp)
2415 || S_IS_WEAKREFR (symp))
2417 const char *sname = S_GET_NAME (symp);
2419 if (S_IS_COMMON (symp)
2420 && !TC_FAKE_LABEL (sname)
2421 && !S_IS_WEAKREFR (symp))
2423 expressionS *e = symbol_get_value_expression (symp);
2425 as_bad (_("`%s' can't be equated to common symbol `%s'"),
2426 sname, S_GET_NAME (e->X_add_symbol));
2428 if (S_GET_SEGMENT (symp) == reg_section)
2430 /* Report error only if we know the symbol name. */
2431 if (S_GET_NAME (symp) != reg_section->name)
2432 as_bad (_("can't make global register symbol `%s'"),
2433 sname);
2435 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2436 continue;
2439 #ifdef obj_frob_symbol
2440 obj_frob_symbol (symp, punt);
2441 #endif
2442 #ifdef tc_frob_symbol
2443 if (! punt || symbol_used_in_reloc_p (symp))
2444 tc_frob_symbol (symp, punt);
2445 #endif
2447 /* If we don't want to keep this symbol, splice it out of
2448 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2449 want section symbols. Otherwise, we skip local symbols
2450 and symbols that the frob_symbol macros told us to punt,
2451 but we keep such symbols if they are used in relocs. */
2452 if (symp == abs_section_sym
2453 || (! EMIT_SECTION_SYMBOLS
2454 && symbol_section_p (symp))
2455 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2456 opposites. Sometimes the former checks flags and the
2457 latter examines the name... */
2458 || (!S_IS_EXTERNAL (symp)
2459 && (punt || S_IS_LOCAL (symp) ||
2460 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
2461 && ! symbol_used_in_reloc_p (symp)))
2463 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2465 /* After symbol_remove, symbol_next(symp) still returns
2466 the one that came after it in the chain. So we don't
2467 need to do any extra cleanup work here. */
2468 continue;
2471 /* Make sure we really got a value for the symbol. */
2472 if (! symbol_resolved_p (symp))
2474 as_bad (_("can't resolve value for symbol `%s'"),
2475 S_GET_NAME (symp));
2476 symbol_mark_resolved (symp);
2479 /* Set the value into the BFD symbol. Up til now the value
2480 has only been kept in the gas symbolS struct. */
2481 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2483 /* A warning construct is a warning symbol followed by the
2484 symbol warned about. Don't let anything object-format or
2485 target-specific muck with it; it's ready for output. */
2486 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2487 skip_next_symbol = true;
2491 /* Now do any format-specific adjustments to the symbol table, such
2492 as adding file symbols. */
2493 #ifdef tc_adjust_symtab
2494 tc_adjust_symtab ();
2495 #endif
2496 #ifdef obj_adjust_symtab
2497 obj_adjust_symtab ();
2498 #endif
2500 /* Stop if there is an error. */
2501 if (!flag_always_generate_output && had_errors ())
2502 return;
2504 /* Now that all the sizes are known, and contents correct, we can
2505 start writing to the file. */
2506 set_symtab ();
2508 /* If *_frob_file changes the symbol value at this point, it is
2509 responsible for moving the changed value into symp->bsym->value
2510 as well. Hopefully all symbol value changing can be done in
2511 *_frob_symbol. */
2512 #ifdef tc_frob_file
2513 tc_frob_file ();
2514 #endif
2515 #ifdef obj_frob_file
2516 obj_frob_file ();
2517 #endif
2518 #ifdef obj_coff_generate_pdata
2519 obj_coff_generate_pdata ();
2520 #endif
2522 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2524 #ifdef tc_frob_file_after_relocs
2525 tc_frob_file_after_relocs ();
2526 #endif
2527 #ifdef obj_frob_file_after_relocs
2528 obj_frob_file_after_relocs ();
2529 #endif
2531 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
2532 if (IS_ELF && flag_use_elf_stt_common)
2533 stdoutput->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
2534 #endif
2536 /* Once all relocations have been written, we can compress the
2537 contents of the debug sections. This needs to be done before
2538 we start writing any sections, because it will affect the file
2539 layout, which is fixed once we start writing contents. */
2540 if (flag_compress_debug != COMPRESS_DEBUG_NONE)
2542 flagword flags = BFD_COMPRESS;
2543 if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
2544 flags = BFD_COMPRESS | BFD_COMPRESS_GABI;
2545 else if (flag_compress_debug == COMPRESS_DEBUG_ZSTD)
2546 flags = BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
2547 stdoutput->flags |= flags & bfd_applicable_file_flags (stdoutput);
2548 if ((stdoutput->flags & BFD_COMPRESS) != 0)
2549 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
2552 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2555 #ifdef TC_GENERIC_RELAX_TABLE
2556 #ifndef md_generic_table_relax_frag
2557 #define md_generic_table_relax_frag relax_frag
2558 #endif
2560 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2562 long
2563 relax_frag (segT segment, fragS *fragP, long stretch)
2565 const relax_typeS *this_type;
2566 const relax_typeS *start_type;
2567 relax_substateT next_state;
2568 relax_substateT this_state;
2569 offsetT growth;
2570 offsetT aim;
2571 addressT target;
2572 addressT address;
2573 symbolS *symbolP;
2574 const relax_typeS *table;
2576 target = fragP->fr_offset;
2577 address = fragP->fr_address + fragP->fr_fix;
2578 table = TC_GENERIC_RELAX_TABLE;
2579 this_state = fragP->fr_subtype;
2580 start_type = this_type = table + this_state;
2581 symbolP = fragP->fr_symbol;
2583 if (symbolP)
2585 fragS *sym_frag;
2587 sym_frag = symbol_get_frag (symbolP);
2589 #ifndef DIFF_EXPR_OK
2590 know (sym_frag != NULL);
2591 #endif
2592 know (S_GET_SEGMENT (symbolP) != absolute_section
2593 || sym_frag == &zero_address_frag);
2594 target += S_GET_VALUE (symbolP);
2596 /* If SYM_FRAG has yet to be reached on this pass, assume it
2597 will move by STRETCH just as we did, unless there is an
2598 alignment frag between here and SYM_FRAG. An alignment may
2599 well absorb any STRETCH, and we don't want to choose a larger
2600 branch insn by overestimating the needed reach of this
2601 branch. It isn't critical to calculate TARGET exactly; We
2602 know we'll be doing another pass if STRETCH is non-zero. */
2604 if (stretch != 0
2605 && sym_frag->relax_marker != fragP->relax_marker
2606 && S_GET_SEGMENT (symbolP) == segment)
2608 if (stretch < 0
2609 || sym_frag->region == fragP->region)
2610 target += stretch;
2611 /* If we get here we know we have a forward branch. This
2612 relax pass may have stretched previous instructions so
2613 far that omitting STRETCH would make the branch
2614 negative. Don't allow this in case the negative reach is
2615 large enough to require a larger branch instruction. */
2616 else if (target < address)
2617 return 0;
2621 aim = target - address;
2622 #ifdef TC_PCREL_ADJUST
2623 /* Currently only the ns32k and arc needs this. */
2624 aim += TC_PCREL_ADJUST (fragP);
2625 #endif
2627 #ifdef md_prepare_relax_scan
2628 /* Formerly called M68K_AIM_KLUDGE. */
2629 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2630 #endif
2632 if (aim < 0)
2634 /* Look backwards. */
2635 for (next_state = this_type->rlx_more; next_state;)
2636 if (aim >= this_type->rlx_backward)
2637 next_state = 0;
2638 else
2640 /* Grow to next state. */
2641 this_state = next_state;
2642 this_type = table + this_state;
2643 next_state = this_type->rlx_more;
2646 else
2648 /* Look forwards. */
2649 for (next_state = this_type->rlx_more; next_state;)
2650 if (aim <= this_type->rlx_forward)
2651 next_state = 0;
2652 else
2654 /* Grow to next state. */
2655 this_state = next_state;
2656 this_type = table + this_state;
2657 next_state = this_type->rlx_more;
2661 growth = this_type->rlx_length - start_type->rlx_length;
2662 if (growth != 0)
2663 fragP->fr_subtype = this_state;
2664 return growth;
2667 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2669 /* Relax_align. Advance location counter to next address that has 'alignment'
2670 lowest order bits all 0s, return size of adjustment made. */
2671 static relax_addressT
2672 relax_align (relax_addressT address, /* Address now. */
2673 int alignment /* Alignment (binary). */)
2675 relax_addressT mask;
2676 relax_addressT new_address;
2678 mask = ~((relax_addressT) ~0 << alignment);
2679 new_address = (address + mask) & (~mask);
2680 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2681 if (linkrelax)
2682 /* We must provide lots of padding, so the linker can discard it
2683 when needed. The linker will not add extra space, ever. */
2684 new_address += (1 << alignment);
2685 #endif
2686 return (new_address - address);
2689 /* Now we have a segment, not a crowd of sub-segments, we can make
2690 fr_address values.
2692 Relax the frags.
2694 After this, all frags in this segment have addresses that are correct
2695 within the segment. Since segments live in different file addresses,
2696 these frag addresses may not be the same as final object-file
2697 addresses. */
2700 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2702 unsigned long frag_count;
2703 struct frag *fragP;
2704 relax_addressT address;
2705 int region;
2706 int ret;
2708 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2709 subseg_change (segment, 0);
2711 /* For each frag in segment: count and store (a 1st guess of)
2712 fr_address. */
2713 address = 0;
2714 region = 0;
2715 for (frag_count = 0, fragP = segment_frag_root;
2716 fragP;
2717 fragP = fragP->fr_next, frag_count ++)
2719 fragP->region = region;
2720 fragP->relax_marker = 0;
2721 fragP->fr_address = address;
2722 address += fragP->fr_fix;
2724 switch (fragP->fr_type)
2726 case rs_fill:
2727 address += fragP->fr_offset * fragP->fr_var;
2728 break;
2730 case rs_align:
2731 case rs_align_code:
2732 case rs_align_test:
2734 addressT offset = relax_align (address, (int) fragP->fr_offset);
2736 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2737 offset = 0;
2739 if (offset % fragP->fr_var != 0)
2741 as_bad_where (fragP->fr_file, fragP->fr_line,
2742 ngettext ("alignment padding (%lu byte) "
2743 "not a multiple of %ld",
2744 "alignment padding (%lu bytes) "
2745 "not a multiple of %ld",
2746 (unsigned long) offset),
2747 (unsigned long) offset, (long) fragP->fr_var);
2748 offset -= (offset % fragP->fr_var);
2751 address += offset;
2752 region += 1;
2754 break;
2756 case rs_org:
2757 /* Assume .org is nugatory. It will grow with 1st relax. */
2758 region += 1;
2759 break;
2761 case rs_space:
2762 case rs_space_nop:
2763 break;
2765 case rs_machine_dependent:
2766 /* If fr_symbol is an expression, this call to
2767 resolve_symbol_value sets up the correct segment, which will
2768 likely be needed in md_estimate_size_before_relax. */
2769 if (fragP->fr_symbol)
2770 resolve_symbol_value (fragP->fr_symbol);
2772 address += md_estimate_size_before_relax (fragP, segment);
2773 break;
2775 #ifndef WORKING_DOT_WORD
2776 /* Broken words don't concern us yet. */
2777 case rs_broken_word:
2778 break;
2779 #endif
2781 case rs_leb128:
2782 /* Initial guess is always 1; doing otherwise can result in
2783 stable solutions that are larger than the minimum. */
2784 address += fragP->fr_offset = 1;
2785 break;
2787 case rs_cfa:
2788 address += eh_frame_estimate_size_before_relax (fragP);
2789 break;
2791 case rs_dwarf2dbg:
2792 address += dwarf2dbg_estimate_size_before_relax (fragP);
2793 break;
2795 case rs_sframe:
2796 /* Initial estimate can be set to atleast 1 byte. */
2797 address += sframe_estimate_size_before_relax (fragP);
2798 break;
2800 default:
2801 BAD_CASE (fragP->fr_type);
2802 break;
2806 /* Do relax(). */
2808 unsigned long max_iterations;
2810 /* Cumulative address adjustment. */
2811 offsetT stretch;
2813 /* Have we made any adjustment this pass? We can't just test
2814 stretch because one piece of code may have grown and another
2815 shrank. */
2816 int stretched;
2818 /* Most horrible, but gcc may give us some exception data that
2819 is impossible to assemble, of the form
2821 .align 4
2822 .byte 0, 0
2823 .uleb128 end - start
2824 start:
2825 .space 128*128 - 1
2826 .align 4
2827 end:
2829 If the leb128 is two bytes in size, then end-start is 128*128,
2830 which requires a three byte leb128. If the leb128 is three
2831 bytes in size, then end-start is 128*128-1, which requires a
2832 two byte leb128. We work around this dilemma by inserting
2833 an extra 4 bytes of alignment just after the .align. This
2834 works because the data after the align is accessed relative to
2835 the end label.
2837 This counter is used in a tiny state machine to detect
2838 whether a leb128 followed by an align is impossible to
2839 relax. */
2840 int rs_leb128_fudge = 0;
2842 /* We want to prevent going into an infinite loop where one frag grows
2843 depending upon the location of a symbol which is in turn moved by
2844 the growing frag. eg:
2846 foo = .
2847 .org foo+16
2848 foo = .
2850 So we dictate that this algorithm can be at most O2. */
2851 max_iterations = frag_count * frag_count;
2852 /* Check for overflow. */
2853 if (max_iterations < frag_count)
2854 max_iterations = frag_count;
2856 ret = 0;
2859 stretch = 0;
2860 stretched = 0;
2862 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2864 offsetT growth = 0;
2865 addressT was_address;
2866 offsetT offset;
2867 symbolS *symbolP;
2869 fragP->relax_marker ^= 1;
2870 was_address = fragP->fr_address;
2871 address = fragP->fr_address += stretch;
2872 symbolP = fragP->fr_symbol;
2873 offset = fragP->fr_offset;
2875 switch (fragP->fr_type)
2877 case rs_fill: /* .fill never relaxes. */
2878 growth = 0;
2879 break;
2881 #ifndef WORKING_DOT_WORD
2882 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2883 for it I do not want to write it. I do not want to have
2884 anything to do with it. This is not the proper way to
2885 implement this misfeature. */
2886 case rs_broken_word:
2888 struct broken_word *lie;
2889 struct broken_word *untruth;
2891 /* Yes this is ugly (storing the broken_word pointer
2892 in the symbol slot). Still, this whole chunk of
2893 code is ugly, and I don't feel like doing anything
2894 about it. Think of it as stubbornness in action. */
2895 growth = 0;
2896 for (lie = (struct broken_word *) (fragP->fr_symbol);
2897 lie && lie->dispfrag == fragP;
2898 lie = lie->next_broken_word)
2901 if (lie->added)
2902 continue;
2904 offset = (S_GET_VALUE (lie->add)
2905 + lie->addnum
2906 - S_GET_VALUE (lie->sub));
2907 if (offset <= -32768 || offset >= 32767)
2909 if (flag_warn_displacement)
2911 char buf[50];
2913 bfd_sprintf_vma (stdoutput, buf,
2914 (addressT) lie->addnum);
2915 as_warn_where (fragP->fr_file, fragP->fr_line,
2916 _(".word %s-%s+%s didn't fit"),
2917 S_GET_NAME (lie->add),
2918 S_GET_NAME (lie->sub),
2919 buf);
2921 if (fragP->fr_subtype == 0)
2923 fragP->fr_subtype++;
2924 growth += md_short_jump_size;
2927 /* Redirect *all* words of this table with the same
2928 target, lest we have to handle the case where the
2929 same target but with a offset that fits on this
2930 round overflows at the next relaxation round. */
2931 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2932 untruth && untruth->dispfrag == lie->dispfrag;
2933 untruth = untruth->next_broken_word)
2934 if ((symbol_get_frag (untruth->add)
2935 == symbol_get_frag (lie->add))
2936 && (S_GET_VALUE (untruth->add)
2937 == S_GET_VALUE (lie->add)))
2939 untruth->added = 2;
2940 untruth->use_jump = lie;
2943 lie->added = 1;
2944 growth += md_long_jump_size;
2948 break;
2949 } /* case rs_broken_word */
2950 #endif
2951 case rs_align:
2952 case rs_align_code:
2953 case rs_align_test:
2955 addressT oldoff, newoff;
2957 oldoff = relax_align (was_address + fragP->fr_fix,
2958 (int) offset);
2959 newoff = relax_align (address + fragP->fr_fix,
2960 (int) offset);
2962 if (fragP->fr_subtype != 0)
2964 if (oldoff > fragP->fr_subtype)
2965 oldoff = 0;
2966 if (newoff > fragP->fr_subtype)
2967 newoff = 0;
2970 growth = newoff - oldoff;
2972 /* If this align happens to follow a leb128 and
2973 we have determined that the leb128 is bouncing
2974 in size, then break the cycle by inserting an
2975 extra alignment. */
2976 if (growth < 0
2977 && (rs_leb128_fudge & 16) != 0
2978 && (rs_leb128_fudge & 15) >= 2)
2980 segment_info_type *seginfo = seg_info (segment);
2981 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2982 struct frag *newf;
2984 newf = frag_alloc (ob);
2985 obstack_blank_fast (ob, fragP->fr_var);
2986 obstack_finish (ob);
2987 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2988 memcpy (newf->fr_literal,
2989 fragP->fr_literal + fragP->fr_fix,
2990 fragP->fr_var);
2991 newf->fr_type = rs_fill;
2992 newf->fr_address = address + fragP->fr_fix + newoff;
2993 newf->fr_fix = 0;
2994 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2995 / fragP->fr_var);
2996 if (newf->fr_offset * newf->fr_var
2997 != (offsetT) 1 << fragP->fr_offset)
2999 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
3000 newf->fr_var = 1;
3002 /* Include size of new frag in GROWTH. */
3003 growth += newf->fr_offset * newf->fr_var;
3004 /* Adjust the new frag address for the amount
3005 we'll add when we process the new frag. */
3006 newf->fr_address -= stretch + growth;
3007 newf->relax_marker ^= 1;
3008 fragP->fr_next = newf;
3009 #ifdef DEBUG
3010 as_warn (_("padding added"));
3011 #endif
3014 break;
3016 case rs_org:
3018 offsetT target = offset;
3019 addressT after;
3021 if (symbolP)
3023 /* Convert from an actual address to an octet offset
3024 into the section. Here it is assumed that the
3025 section's VMA is zero, and can omit subtracting it
3026 from the symbol's value to get the address offset. */
3027 know (S_GET_SEGMENT (symbolP)->vma == 0);
3028 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
3031 know (fragP->fr_next);
3032 after = fragP->fr_next->fr_address + stretch;
3033 growth = target - after;
3035 /* Growth may be negative, but variable part of frag
3036 cannot have fewer than 0 chars. That is, we can't
3037 .org backwards. */
3038 if ((offsetT) (address + fragP->fr_fix) > target)
3040 growth = 0;
3042 /* Don't error on first few frag relax passes.
3043 The symbol might be an expression involving
3044 symbol values from other sections. If those
3045 sections have not yet been processed their
3046 frags will all have zero addresses, so we
3047 will calculate incorrect values for them. The
3048 number of passes we allow before giving an
3049 error is somewhat arbitrary. It should be at
3050 least one, with larger values requiring
3051 increasingly contrived dependencies between
3052 frags to trigger a false error. */
3053 if (pass < 2)
3055 /* Force another pass. */
3056 ret = 1;
3057 break;
3060 as_bad_where (fragP->fr_file, fragP->fr_line,
3061 _("attempt to move .org backwards"));
3063 /* We've issued an error message. Change the
3064 frag to avoid cascading errors. */
3065 fragP->fr_type = rs_align;
3066 fragP->fr_subtype = 0;
3067 fragP->fr_offset = 0;
3068 fragP->fr_fix = after - address;
3071 break;
3073 case rs_space:
3074 case rs_space_nop:
3075 growth = 0;
3076 if (symbolP)
3078 offsetT amount;
3080 amount = S_GET_VALUE (symbolP);
3081 if (S_GET_SEGMENT (symbolP) != absolute_section
3082 || S_IS_COMMON (symbolP)
3083 || ! S_IS_DEFINED (symbolP))
3085 as_bad_where (fragP->fr_file, fragP->fr_line,
3086 _(".space, .nops or .fill specifies non-absolute value"));
3087 /* Prevent repeat of this error message. */
3088 fragP->fr_symbol = 0;
3090 else if (amount < 0)
3092 /* Don't error on first few frag relax passes.
3093 See rs_org comment for a longer explanation. */
3094 if (pass < 2)
3096 ret = 1;
3097 break;
3100 as_warn_where (fragP->fr_file, fragP->fr_line,
3101 _(".space, .nops or .fill with negative value, ignored"));
3102 fragP->fr_symbol = 0;
3104 else
3105 growth = (was_address + fragP->fr_fix + amount
3106 - fragP->fr_next->fr_address);
3108 break;
3110 case rs_machine_dependent:
3111 #ifdef md_relax_frag
3112 growth = md_relax_frag (segment, fragP, stretch);
3113 #else
3114 #ifdef TC_GENERIC_RELAX_TABLE
3115 /* The default way to relax a frag is to look through
3116 TC_GENERIC_RELAX_TABLE. */
3117 growth = md_generic_table_relax_frag (segment, fragP,
3118 stretch);
3119 #endif /* TC_GENERIC_RELAX_TABLE */
3120 #endif
3121 break;
3123 case rs_leb128:
3125 valueT value;
3126 offsetT size;
3128 value = resolve_symbol_value (fragP->fr_symbol);
3129 size = sizeof_leb128 (value, fragP->fr_subtype);
3130 growth = size - fragP->fr_offset;
3131 fragP->fr_offset = size;
3133 break;
3135 case rs_cfa:
3136 growth = eh_frame_relax_frag (fragP);
3137 break;
3139 case rs_dwarf2dbg:
3140 growth = dwarf2dbg_relax_frag (fragP);
3141 break;
3143 case rs_sframe:
3144 growth = sframe_relax_frag (fragP);
3145 break;
3147 default:
3148 BAD_CASE (fragP->fr_type);
3149 break;
3151 if (growth)
3153 stretch += growth;
3154 stretched = 1;
3155 if (fragP->fr_type == rs_leb128)
3156 rs_leb128_fudge += 16;
3157 else if (fragP->fr_type == rs_align
3158 && (rs_leb128_fudge & 16) != 0
3159 && stretch == 0)
3160 rs_leb128_fudge += 16;
3161 else
3162 rs_leb128_fudge = 0;
3166 if (stretch == 0
3167 && (rs_leb128_fudge & 16) == 0
3168 && (rs_leb128_fudge & -16) != 0)
3169 rs_leb128_fudge += 1;
3170 else
3171 rs_leb128_fudge = 0;
3173 /* Until nothing further to relax. */
3174 while (stretched && -- max_iterations);
3176 if (stretched)
3177 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
3178 segment_name (segment));
3181 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
3182 if (fragP->last_fr_address != fragP->fr_address)
3184 fragP->last_fr_address = fragP->fr_address;
3185 ret = 1;
3187 return ret;
3190 void
3191 number_to_chars_bigendian (char *buf, valueT val, int n)
3193 if (n <= 0)
3194 abort ();
3195 while (n--)
3197 buf[n] = val & 0xff;
3198 val >>= 8;
3202 void
3203 number_to_chars_littleendian (char *buf, valueT val, int n)
3205 if (n <= 0)
3206 abort ();
3207 while (n--)
3209 *buf++ = val & 0xff;
3210 val >>= 8;
3214 void
3215 write_print_statistics (FILE *file)
3217 fprintf (file, "fixups: %d\n", n_fixups);
3220 /* For debugging. */
3221 extern int indent_level;
3223 void
3224 print_fixup (fixS *fixp)
3226 indent_level = 1;
3227 fprintf (stderr, "fix %p %s:%d", fixp, fixp->fx_file, fixp->fx_line);
3228 if (fixp->fx_pcrel)
3229 fprintf (stderr, " pcrel");
3230 if (fixp->fx_pcrel_adjust)
3231 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
3232 if (fixp->fx_tcbit)
3233 fprintf (stderr, " tcbit");
3234 if (fixp->fx_done)
3235 fprintf (stderr, " done");
3236 fprintf (stderr, "\n size=%d frag=%p", fixp->fx_size, fixp->fx_frag);
3237 fprintf (stderr, " where=%ld offset=%" PRIx64 " addnumber=%" PRIx64,
3238 fixp->fx_where, (uint64_t) fixp->fx_offset,
3239 (uint64_t) fixp->fx_addnumber);
3240 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
3241 fixp->fx_r_type);
3242 if (fixp->fx_addsy)
3244 fprintf (stderr, "\n +<");
3245 print_symbol_value_1 (stderr, fixp->fx_addsy);
3246 fprintf (stderr, ">");
3248 if (fixp->fx_subsy)
3250 fprintf (stderr, "\n -<");
3251 print_symbol_value_1 (stderr, fixp->fx_subsy);
3252 fprintf (stderr, ">");
3254 fprintf (stderr, "\n");
3255 #ifdef TC_FIX_DATA_PRINT
3256 TC_FIX_DATA_PRINT (stderr, fixp);
3257 #endif