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