1 /* write.c - emit .o file
2 Copyright (C) 1986-2023 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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
21 /* This thing should be set up to do byte ordering correctly. But... */
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))
35 #ifndef TC_FORCE_RELOCATION_ABS
36 #define TC_FORCE_RELOCATION_ABS(FIX) \
37 (TC_FORCE_RELOCATION (FIX))
40 #define GENERIC_FORCE_RELOCATION_LOCAL(FIX) \
42 || TC_FORCE_RELOCATION (FIX))
43 #ifndef TC_FORCE_RELOCATION_LOCAL
44 #define TC_FORCE_RELOCATION_LOCAL GENERIC_FORCE_RELOCATION_LOCAL
47 #define GENERIC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
49 #ifndef TC_FORCE_RELOCATION_SUB_SAME
50 #define TC_FORCE_RELOCATION_SUB_SAME GENERIC_FORCE_RELOCATION_SUB_SAME
53 #ifndef md_register_arithmetic
54 # define md_register_arithmetic 1
57 #ifndef TC_FORCE_RELOCATION_SUB_ABS
58 #define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG) \
59 (!md_register_arithmetic && (SEG) == reg_section)
62 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
64 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) \
65 (!md_register_arithmetic && (SEG) == reg_section)
67 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) 1
71 #ifndef TC_VALIDATE_FIX_SUB
72 #define TC_VALIDATE_FIX_SUB(FIX, SEG) 0
75 #ifndef TC_LINKRELAX_FIXUP
76 #define TC_LINKRELAX_FIXUP(SEG) 1
79 #ifndef MD_APPLY_SYM_VALUE
80 #define MD_APPLY_SYM_VALUE(FIX) 1
83 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
84 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
87 #ifndef MD_PCREL_FROM_SECTION
88 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
92 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
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
101 #ifndef TC_FX_SIZE_SLACK
102 #define TC_FX_SIZE_SLACK(FIX) 0
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. */
115 /* The frag that dot_value is based from. */
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_for_relax, for example. */
127 static int frags_chained
= 0;
131 #define RELOC_ENUM enum bfd_reloc_code_real
133 /* Create a fixS in obstack 'notes'. */
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? */
150 fixP
= (fixS
*) obstack_alloc (¬es
, 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
);
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;
173 fixP
->fx_no_overflow
= 0;
177 fixP
->fx_cgen
.insn
= NULL
;
178 fixP
->fx_cgen
.opinfo
= 0;
182 TC_INIT_FIX_DATA (fixP
);
185 fixP
->fx_file
= as_where (&fixP
->fx_line
);
189 fixS
**seg_fix_rootP
= (frags_chained
190 ? &seg_info (now_seg
)->fix_root
191 : &frchain_now
->fix_root
);
192 fixS
**seg_fix_tailP
= (frags_chained
193 ? &seg_info (now_seg
)->fix_tail
194 : &frchain_now
->fix_tail
);
198 fixP
->fx_next
= *seg_fix_rootP
;
199 *seg_fix_rootP
= fixP
;
200 if (fixP
->fx_next
== NULL
)
201 *seg_fix_tailP
= fixP
;
205 fixP
->fx_next
= NULL
;
207 (*seg_fix_tailP
)->fx_next
= fixP
;
209 *seg_fix_rootP
= fixP
;
210 *seg_fix_tailP
= fixP
;
217 /* Create a fixup relative to a symbol (plus a constant). */
220 fix_new (fragS
*frag
, /* Which frag? */
221 unsigned long where
, /* Where in that frag? */
222 unsigned long size
, /* 1, 2, or 4 usually. */
223 symbolS
*add_symbol
, /* X_add_symbol. */
224 offsetT offset
, /* X_add_number. */
225 int pcrel
, /* TRUE if PC-relative relocation. */
226 RELOC_ENUM r_type
/* Relocation type. */)
228 return fix_new_internal (frag
, where
, size
, add_symbol
,
229 (symbolS
*) NULL
, offset
, pcrel
, r_type
, false);
232 /* Create a fixup for an expression. Currently we only support fixups
233 for difference expressions. That is itself more than most object
234 file formats support anyhow. */
237 fix_new_exp (fragS
*frag
, /* Which frag? */
238 unsigned long where
, /* Where in that frag? */
239 unsigned long size
, /* 1, 2, or 4 usually. */
240 expressionS
*exp
, /* Expression. */
241 int pcrel
, /* TRUE if PC-relative relocation. */
242 RELOC_ENUM r_type
/* Relocation type. */)
254 as_bad (_("register value used as expression"));
258 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
259 the difference expression cannot immediately be reduced. */
261 symbolS
*stmp
= make_expr_symbol (exp
);
263 exp
->X_op
= O_symbol
;
264 exp
->X_op_symbol
= 0;
265 exp
->X_add_symbol
= stmp
;
266 exp
->X_add_number
= 0;
268 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
272 add
= exp
->X_add_symbol
;
273 off
= exp
->X_add_number
;
274 r_type
= BFD_RELOC_RVA
;
278 sub
= exp
->X_add_symbol
;
279 off
= exp
->X_add_number
;
283 sub
= exp
->X_op_symbol
;
286 add
= exp
->X_add_symbol
;
289 off
= exp
->X_add_number
;
293 add
= make_expr_symbol (exp
);
297 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
,
301 /* Create a fixup at the beginning of FRAG. The arguments are the same
302 as for fix_new, except that WHERE is implicitly 0. */
305 fix_at_start (fragS
*frag
, unsigned long size
, symbolS
*add_symbol
,
306 offsetT offset
, int pcrel
, RELOC_ENUM r_type
)
308 return fix_new_internal (frag
, 0, size
, add_symbol
,
309 (symbolS
*) NULL
, offset
, pcrel
, r_type
, true);
312 /* Generic function to determine whether a fixup requires a relocation. */
314 generic_force_reloc (fixS
*fix
)
316 if (fix
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
317 || fix
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
320 if (fix
->fx_addsy
== NULL
)
323 return S_FORCE_RELOC (fix
->fx_addsy
, fix
->fx_subsy
== NULL
);
326 /* Append a string onto another string, bumping the pointer along. */
328 append (char **charPP
, char *fromP
, unsigned long length
)
330 /* Don't trust memcpy() of 0 chars. */
334 memcpy (*charPP
, fromP
, length
);
338 /* This routine records the largest alignment seen for each segment.
339 If the beginning of the segment is aligned on the worst-case
340 boundary, all of the other alignments within it will work. At
341 least one object format really uses this info. */
344 record_alignment (/* Segment to which alignment pertains. */
346 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
347 boundary, 2 => 4-byte boundary, etc.) */
350 if (seg
== absolute_section
)
353 if (align
> bfd_section_alignment (seg
))
354 bfd_set_section_alignment (seg
, align
);
358 get_recorded_alignment (segT seg
)
360 if (seg
== absolute_section
)
363 return bfd_section_alignment (seg
);
366 /* Reset the section indices after removing the gas created sections. */
369 renumber_sections (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *countparg
)
371 int *countp
= (int *) countparg
;
373 sec
->index
= *countp
;
378 chain_frchains_together_1 (segT section
, struct frchain
*frchp
)
380 fragS dummy
, *prev_frag
= &dummy
;
381 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
383 for (; frchp
; frchp
= frchp
->frch_next
)
385 prev_frag
->fr_next
= frchp
->frch_root
;
386 prev_frag
= frchp
->frch_last
;
387 gas_assert (prev_frag
->fr_type
!= 0);
388 if (frchp
->fix_root
!= (fixS
*) NULL
)
390 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
391 seg_info (section
)->fix_root
= frchp
->fix_root
;
392 prev_fix
->fx_next
= frchp
->fix_root
;
393 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
394 prev_fix
= frchp
->fix_tail
;
397 gas_assert (prev_frag
!= &dummy
398 && prev_frag
->fr_type
!= 0);
399 prev_frag
->fr_next
= 0;
404 chain_frchains_together (bfd
*abfd ATTRIBUTE_UNUSED
,
406 void *xxx ATTRIBUTE_UNUSED
)
408 segment_info_type
*info
;
410 /* BFD may have introduced its own sections without using
411 subseg_new, so it is possible that seg_info is NULL. */
412 info
= seg_info (section
);
413 if (info
!= (segment_info_type
*) NULL
)
414 info
->frchainP
->frch_last
415 = chain_frchains_together_1 (section
, info
->frchainP
);
417 /* Now that we've chained the frags together, we must add new fixups
418 to the segment, not to the frag chain. */
423 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED
, fragS
*fragP
)
425 switch (fragP
->fr_type
)
435 HANDLE_ALIGN (fragP
);
438 know (fragP
->fr_next
!= NULL
);
439 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
441 - fragP
->fr_fix
) / fragP
->fr_var
;
442 if (fragP
->fr_offset
< 0)
444 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
445 _("attempt to .org/.space/.nops backwards? (%ld)"),
446 (long) fragP
->fr_offset
);
447 fragP
->fr_offset
= 0;
449 if (fragP
->fr_type
== rs_space_nop
)
450 fragP
->fr_type
= rs_fill_nop
;
452 fragP
->fr_type
= rs_fill
;
461 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
464 if (!S_IS_DEFINED (fragP
->fr_symbol
))
466 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
467 _("leb128 operand is an undefined symbol: %s"),
468 S_GET_NAME (fragP
->fr_symbol
));
471 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
474 fragP
->fr_fix
+= size
;
475 fragP
->fr_type
= rs_fill
;
477 fragP
->fr_offset
= 0;
478 fragP
->fr_symbol
= NULL
;
483 eh_frame_convert_frag (fragP
);
487 dwarf2dbg_convert_frag (fragP
);
491 sframe_convert_frag (fragP
);
494 case rs_machine_dependent
:
495 md_convert_frag (stdoutput
, sec
, fragP
);
497 gas_assert (fragP
->fr_next
== NULL
498 || (fragP
->fr_next
->fr_address
- fragP
->fr_address
501 /* After md_convert_frag, we make the frag into a ".space 0".
502 md_convert_frag() should set up any fixSs and constants
507 #ifndef WORKING_DOT_WORD
510 struct broken_word
*lie
;
512 if (fragP
->fr_subtype
)
514 fragP
->fr_fix
+= md_short_jump_size
;
515 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
516 lie
&& lie
->dispfrag
== fragP
;
517 lie
= lie
->next_broken_word
)
519 fragP
->fr_fix
+= md_long_jump_size
;
527 BAD_CASE (fragP
->fr_type
);
531 md_frag_check (fragP
);
535 struct relax_seg_info
542 relax_seg (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *xxx
)
544 segment_info_type
*seginfo
= seg_info (sec
);
545 struct relax_seg_info
*info
= (struct relax_seg_info
*) xxx
;
547 if (seginfo
&& seginfo
->frchainP
548 && relax_segment (seginfo
->frchainP
->frch_root
, sec
, info
->pass
))
553 size_seg (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
557 segment_info_type
*seginfo
;
559 valueT size
, newsize
;
561 subseg_change (sec
, 0);
563 seginfo
= seg_info (sec
);
564 if (seginfo
&& seginfo
->frchainP
)
566 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
567 cvt_frag_to_fill (sec
, fragp
);
568 for (fragp
= seginfo
->frchainP
->frch_root
;
570 fragp
= fragp
->fr_next
)
571 /* Walk to last elt. */
573 size
= fragp
->fr_address
+ fragp
->fr_fix
;
578 flags
= bfd_section_flags (sec
);
579 if (size
== 0 && bfd_section_size (sec
) != 0 &&
580 (flags
& SEC_HAS_CONTENTS
) != 0)
583 if (size
> 0 && ! seginfo
->bss
)
584 flags
|= SEC_HAS_CONTENTS
;
586 x
= bfd_set_section_flags (sec
, flags
);
589 /* If permitted, allow the backend to pad out the section
590 to some alignment boundary. */
591 if (do_not_pad_sections_to_alignment
)
594 newsize
= md_section_align (sec
, size
);
595 x
= bfd_set_section_size (sec
, newsize
);
598 /* If the size had to be rounded up, add some padding in the last
600 gas_assert (newsize
>= size
);
603 fragS
*last
= seginfo
->frchainP
->frch_last
;
604 fragp
= seginfo
->frchainP
->frch_root
;
605 while (fragp
->fr_next
!= last
)
606 fragp
= fragp
->fr_next
;
607 last
->fr_address
= size
;
608 if ((newsize
- size
) % fragp
->fr_var
== 0)
609 fragp
->fr_offset
+= (newsize
- size
) / fragp
->fr_var
;
611 /* If we hit this abort, it's likely due to subsegs_finish not
612 providing sufficient alignment on the last frag, and the
613 machine dependent code using alignment frags with fr_var
618 #ifdef tc_frob_section
619 tc_frob_section (sec
);
621 #ifdef obj_frob_section
622 obj_frob_section (sec
);
628 dump_section_relocs (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, FILE *stream
)
630 segment_info_type
*seginfo
= seg_info (sec
);
631 fixS
*fixp
= seginfo
->fix_root
;
636 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
639 symbolS
*s
= fixp
->fx_addsy
;
641 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
642 (int) fixp
->fx_r_type
);
644 fprintf (stream
, "no sym\n");
647 print_symbol_value_1 (stream
, s
);
648 fprintf (stream
, "\n");
650 fixp
= fixp
->fx_next
;
654 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
657 #ifndef EMIT_SECTION_SYMBOLS
658 #define EMIT_SECTION_SYMBOLS 1
661 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
662 and check for validity. Convert RELOC_LIST from using U.A fields
665 resolve_reloc_expr_symbols (void)
667 bfd_vma addr_mask
= 1;
668 struct reloc_list
*r
;
670 /* Avoid a shift by the width of type. */
671 addr_mask
<<= bfd_arch_bits_per_address (stdoutput
) - 1;
675 for (r
= reloc_list
; r
; r
= r
->next
)
677 reloc_howto_type
*howto
= r
->u
.a
.howto
;
680 bfd_vma offset
, addend
;
683 resolve_symbol_value (r
->u
.a
.offset_sym
);
684 symval
= symbol_get_value_expression (r
->u
.a
.offset_sym
);
688 if (symval
->X_op
== O_constant
)
689 sym
= r
->u
.a
.offset_sym
;
690 else if (symval
->X_op
== O_symbol
)
692 sym
= symval
->X_add_symbol
;
693 offset
= symval
->X_add_number
;
694 symval
= symbol_get_value_expression (symval
->X_add_symbol
);
697 || symval
->X_op
!= O_constant
698 || (sec
= S_GET_SEGMENT (sym
)) == NULL
699 || !SEG_NORMAL (sec
))
701 as_bad_where (r
->file
, r
->line
, _("invalid offset expression"));
705 offset
+= S_GET_VALUE (sym
);
708 addend
= r
->u
.a
.addend
;
709 if (r
->u
.a
.sym
!= NULL
)
711 resolve_symbol_value (r
->u
.a
.sym
);
712 symval
= symbol_get_value_expression (r
->u
.a
.sym
);
713 if (symval
->X_op
== O_constant
)
715 else if (symval
->X_op
== O_symbol
)
717 sym
= symval
->X_add_symbol
;
718 addend
+= symval
->X_add_number
;
719 symval
= symbol_get_value_expression (symval
->X_add_symbol
);
721 if (symval
->X_op
!= O_constant
)
723 as_bad_where (r
->file
, r
->line
, _("invalid reloc expression"));
726 else if (sym
!= NULL
&& sec
!= NULL
)
728 /* Convert relocs against local symbols to refer to the
729 corresponding section symbol plus offset instead. Keep
730 PC-relative relocs of the REL variety intact though to
731 prevent the offset from overflowing the relocated field,
732 unless it has enough bits to cover the whole address
735 && S_IS_DEFINED (sym
)
736 && !symbol_section_p (sym
)
738 || (howto
->partial_inplace
739 && (!howto
->pc_relative
740 || howto
->src_mask
== addr_mask
))))
742 asection
*symsec
= S_GET_SEGMENT (sym
);
743 if (!(((symsec
->flags
& SEC_MERGE
) != 0
745 || (symsec
->flags
& SEC_THREAD_LOCAL
) != 0))
747 addend
+= S_GET_VALUE (sym
);
748 sym
= section_symbol (symsec
);
751 symbol_mark_used_in_reloc (sym
);
756 if (abs_section_sym
== NULL
)
757 abs_section_sym
= section_symbol (absolute_section
);
758 sym
= abs_section_sym
;
762 r
->u
.b
.s
= symbol_get_bfdsym (sym
);
763 r
->u
.b
.r
.sym_ptr_ptr
= &r
->u
.b
.s
;
764 r
->u
.b
.r
.address
= offset
;
765 r
->u
.b
.r
.addend
= addend
;
766 r
->u
.b
.r
.howto
= howto
;
770 /* This pass over fixups decides whether symbols can be replaced with
774 adjust_reloc_syms (bfd
*abfd ATTRIBUTE_UNUSED
,
776 void *xxx ATTRIBUTE_UNUSED
)
778 segment_info_type
*seginfo
= seg_info (sec
);
784 dump_section_relocs (abfd
, sec
, stderr
);
786 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
790 else if (fixp
->fx_addsy
)
796 fprintf (stderr
, "\n\nadjusting fixup:\n");
800 sym
= fixp
->fx_addsy
;
802 /* All symbols should have already been resolved at this
803 point. It is possible to see unresolved expression
804 symbols, though, since they are not in the regular symbol
806 resolve_symbol_value (sym
);
808 if (fixp
->fx_subsy
!= NULL
)
809 resolve_symbol_value (fixp
->fx_subsy
);
811 /* If this symbol is equated to an undefined or common symbol,
812 convert the fixup to being against that symbol. */
813 while (symbol_equated_reloc_p (sym
)
814 || S_IS_WEAKREFR (sym
))
816 symbolS
*newsym
= symbol_get_value_expression (sym
)->X_add_symbol
;
819 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
820 fixp
->fx_addsy
= newsym
;
824 if (symbol_mri_common_p (sym
))
826 fixp
->fx_offset
+= S_GET_VALUE (sym
);
827 fixp
->fx_addsy
= symbol_get_value_expression (sym
)->X_add_symbol
;
831 /* If the symbol is undefined, common, weak, or global (ELF
832 shared libs), we can't replace it with the section symbol. */
833 if (S_FORCE_RELOC (fixp
->fx_addsy
, 1))
836 /* Is there some other (target cpu dependent) reason we can't adjust
837 this one? (E.g. relocations involving function addresses on
839 #ifdef tc_fix_adjustable
840 if (! tc_fix_adjustable (fixp
))
844 /* Since we're reducing to section symbols, don't attempt to reduce
845 anything that's already using one. */
846 if (symbol_section_p (sym
))
848 /* Mark the section symbol used in relocation so that it will
849 be included in the symbol table. */
850 symbol_mark_used_in_reloc (sym
);
854 symsec
= S_GET_SEGMENT (sym
);
858 if (bfd_is_abs_section (symsec
)
859 || symsec
== reg_section
)
861 /* The fixup_segment routine normally will not use this
862 symbol in a relocation. */
866 /* Don't try to reduce relocs which refer to non-local symbols
867 in .linkonce sections. It can lead to confusion when a
868 debugging section refers to a .linkonce section. I hope
869 this will always be correct. */
870 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
872 if ((symsec
->flags
& SEC_LINK_ONCE
) != 0
874 /* The GNU toolchain uses an extension for ELF: a
875 section beginning with the magic string
876 .gnu.linkonce is a linkonce section. */
877 && startswith (segment_name (symsec
), ".gnu.linkonce")))
881 /* Never adjust a reloc against local symbol in a merge section
882 with non-zero addend. */
883 if ((symsec
->flags
& SEC_MERGE
) != 0
884 && (fixp
->fx_offset
!= 0 || fixp
->fx_subsy
!= NULL
))
887 /* Never adjust a reloc against TLS local symbol. */
888 if ((symsec
->flags
& SEC_THREAD_LOCAL
) != 0)
891 /* We refetch the segment when calling section_symbol, rather
892 than using symsec, because S_GET_VALUE may wind up changing
893 the section when it calls resolve_symbol_value. */
894 fixp
->fx_offset
+= S_GET_VALUE (sym
);
895 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
897 fprintf (stderr
, "\nadjusted fixup:\n");
902 dump_section_relocs (abfd
, sec
, stderr
);
906 as_bad_subtract (fixS
*fixp
)
908 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
909 _("can't resolve %s - %s"),
910 fixp
->fx_addsy
? S_GET_NAME (fixp
->fx_addsy
) : "0",
911 S_GET_NAME (fixp
->fx_subsy
));
916 Go through all the fixS's in a segment and see which ones can be
917 handled now. (These consist of fixS where we have since discovered
918 the value of a symbol, or the address of the frag involved.)
919 For each one, call md_apply_fix to put the fix into the frag data.
920 Ones that we couldn't completely handle here will be output later
921 by emit_relocations. */
924 fixup_segment (fixS
*fixP
, segT this_segment
)
929 if (fixP
!= NULL
&& abs_section_sym
== NULL
)
930 abs_section_sym
= section_symbol (absolute_section
);
932 /* If the linker is doing the relaxing, we must not do any fixups.
934 Well, strictly speaking that's not true -- we could do any that
935 are PC-relative and don't cross regions that could change size. */
936 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment
))
938 for (; fixP
; fixP
= fixP
->fx_next
)
941 if (fixP
->fx_addsy
== NULL
)
943 /* There was no symbol required by this relocation.
944 However, BFD doesn't really handle relocations
945 without symbols well. So fake up a local symbol in
946 the absolute section. */
947 fixP
->fx_addsy
= abs_section_sym
;
949 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
950 if (fixP
->fx_subsy
!= NULL
)
951 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
956 for (; fixP
; fixP
= fixP
->fx_next
)
958 segT add_symbol_segment
= absolute_section
;
961 fprintf (stderr
, "\nprocessing fixup:\n");
965 fragP
= fixP
->fx_frag
;
967 #ifdef TC_VALIDATE_FIX
968 TC_VALIDATE_FIX (fixP
, this_segment
, skip
);
970 add_number
= fixP
->fx_offset
;
972 if (fixP
->fx_addsy
!= NULL
)
973 add_symbol_segment
= S_GET_SEGMENT (fixP
->fx_addsy
);
975 if (fixP
->fx_subsy
!= NULL
)
977 segT sub_symbol_segment
;
979 resolve_symbol_value (fixP
->fx_subsy
);
980 sub_symbol_segment
= S_GET_SEGMENT (fixP
->fx_subsy
);
982 if (fixP
->fx_addsy
!= NULL
983 && sub_symbol_segment
== add_symbol_segment
984 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
985 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
986 && !TC_FORCE_RELOCATION_SUB_SAME (fixP
, add_symbol_segment
))
988 add_number
+= S_GET_VALUE_WHERE (fixP
->fx_addsy
, fixP
->fx_file
, fixP
->fx_line
);
989 add_number
-= S_GET_VALUE_WHERE (fixP
->fx_subsy
, fixP
->fx_file
, fixP
->fx_line
);
990 fixP
->fx_offset
= add_number
;
991 fixP
->fx_addsy
= NULL
;
992 fixP
->fx_subsy
= NULL
;
994 /* See the comment below about 68k weirdness. */
998 else if (sub_symbol_segment
== absolute_section
999 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
1000 && !TC_FORCE_RELOCATION_SUB_ABS (fixP
, add_symbol_segment
))
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_subsy
= NULL
;
1006 else if (sub_symbol_segment
== this_segment
1007 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
1008 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP
, add_symbol_segment
))
1010 add_number
-= S_GET_VALUE_WHERE (fixP
->fx_subsy
, fixP
->fx_file
, fixP
->fx_line
);
1011 fixP
->fx_offset
= (add_number
+ fixP
->fx_dot_value
1012 + fixP
->fx_dot_frag
->fr_address
);
1014 /* Make it pc-relative. If the back-end code has not
1015 selected a pc-relative reloc, cancel the adjustment
1016 we do later on all pc-relative relocs. */
1019 /* Do this for m68k even if it's already described
1020 as pc-relative. On the m68k, an operand of
1021 "pc@(foo-.-2)" should address "foo" in a
1022 pc-relative mode. */
1026 add_number
+= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
1027 fixP
->fx_subsy
= NULL
;
1030 else if (!TC_VALIDATE_FIX_SUB (fixP
, add_symbol_segment
))
1032 if (!md_register_arithmetic
1033 && (add_symbol_segment
== reg_section
1034 || sub_symbol_segment
== reg_section
))
1035 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1036 _("register value used as expression"));
1038 as_bad_subtract (fixP
);
1040 else if (sub_symbol_segment
!= undefined_section
1041 && ! bfd_is_com_section (sub_symbol_segment
)
1042 && MD_APPLY_SYM_VALUE (fixP
))
1043 add_number
-= S_GET_VALUE_WHERE (fixP
->fx_subsy
, fixP
->fx_file
, fixP
->fx_line
);
1048 if (add_symbol_segment
== this_segment
1049 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
1050 && !TC_FORCE_RELOCATION_LOCAL (fixP
))
1052 /* This fixup was made when the symbol's segment was
1053 SEG_UNKNOWN, but it is now in the local segment.
1054 So we know how to do the address without relocation. */
1055 add_number
+= S_GET_VALUE_WHERE (fixP
->fx_addsy
, fixP
->fx_file
, fixP
->fx_line
);
1056 fixP
->fx_offset
= add_number
;
1058 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
1059 fixP
->fx_addsy
= NULL
;
1062 else if (add_symbol_segment
== absolute_section
1063 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
1064 && !TC_FORCE_RELOCATION_ABS (fixP
))
1066 add_number
+= S_GET_VALUE_WHERE (fixP
->fx_addsy
, fixP
->fx_file
, fixP
->fx_line
);
1067 fixP
->fx_offset
= add_number
;
1068 fixP
->fx_addsy
= NULL
;
1070 else if (add_symbol_segment
!= undefined_section
1071 && ! bfd_is_com_section (add_symbol_segment
)
1072 && MD_APPLY_SYM_VALUE (fixP
))
1073 add_number
+= S_GET_VALUE_WHERE (fixP
->fx_addsy
, fixP
->fx_file
, fixP
->fx_line
);
1078 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
1079 if (!fixP
->fx_done
&& fixP
->fx_addsy
== NULL
)
1081 /* There was no symbol required by this relocation.
1082 However, BFD doesn't really handle relocations
1083 without symbols well. So fake up a local symbol in
1084 the absolute section. */
1085 fixP
->fx_addsy
= abs_section_sym
;
1090 md_apply_fix (fixP
, &add_number
, this_segment
);
1094 if (fixP
->fx_addsy
== NULL
)
1095 fixP
->fx_addsy
= abs_section_sym
;
1096 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
1097 if (fixP
->fx_subsy
!= NULL
)
1098 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
1101 if (!fixP
->fx_no_overflow
&& fixP
->fx_size
!= 0)
1103 if (fixP
->fx_size
< sizeof (valueT
))
1108 mask
--; /* Set all bits to one. */
1109 mask
<<= fixP
->fx_size
* 8 - (fixP
->fx_signed
? 1 : 0);
1110 if ((add_number
& mask
) != 0
1112 ? (add_number
& mask
) != mask
1113 : (-add_number
& mask
) != 0))
1115 char buf
[50], buf2
[50];
1116 bfd_sprintf_vma (stdoutput
, buf
, fragP
->fr_address
+ fixP
->fx_where
);
1117 if (add_number
> 1000)
1118 bfd_sprintf_vma (stdoutput
, buf2
, add_number
);
1120 sprintf (buf2
, "%ld", (long) add_number
);
1121 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1122 ngettext ("value of %s too large for field "
1124 "value of %s too large for field "
1125 "of %d bytes at %s",
1127 buf2
, fixP
->fx_size
, buf
);
1128 } /* Generic error checking. */
1130 #ifdef WARN_SIGNED_OVERFLOW_WORD
1131 /* Warn if a .word value is too large when treated as a signed
1132 number. We already know it is not too negative. This is to
1133 catch over-large switches generated by gcc on the 68k. */
1134 if (!flag_signed_overflow_ok
1135 && fixP
->fx_size
== 2
1136 && add_number
> 0x7fff)
1137 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1138 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1140 (long) (fragP
->fr_address
+ fixP
->fx_where
));
1144 #ifdef TC_VALIDATE_FIX
1145 skip
: ATTRIBUTE_UNUSED_LABEL
1149 fprintf (stderr
, "result:\n");
1152 } /* For each fixS in this segment. */
1156 fix_segment (bfd
*abfd ATTRIBUTE_UNUSED
,
1158 void *xxx ATTRIBUTE_UNUSED
)
1160 segment_info_type
*seginfo
= seg_info (sec
);
1162 fixup_segment (seginfo
->fix_root
, sec
);
1166 install_reloc (asection
*sec
, arelent
*reloc
, fragS
*fragp
,
1167 const char *file
, unsigned int line
)
1170 bfd_reloc_status_type s
;
1173 if (reloc
->sym_ptr_ptr
!= NULL
1174 && (sym
= *reloc
->sym_ptr_ptr
) != NULL
1175 && (sym
->flags
& BSF_KEEP
) == 0
1176 && ((sym
->flags
& BSF_SECTION_SYM
) == 0
1177 || (EMIT_SECTION_SYMBOLS
1178 && !bfd_is_abs_section (sym
->section
))))
1179 as_bad_where (file
, line
, _("redefined symbol cannot be used on reloc"));
1181 s
= bfd_install_relocation (stdoutput
, reloc
,
1182 fragp
->fr_literal
, fragp
->fr_address
,
1188 case bfd_reloc_overflow
:
1189 as_bad_where (file
, line
, _("relocation overflow"));
1191 case bfd_reloc_outofrange
:
1192 as_bad_where (file
, line
, _("relocation out of range"));
1195 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1201 get_frag_for_reloc (fragS
*last_frag
,
1202 const segment_info_type
*seginfo
,
1203 const struct reloc_list
*r
)
1207 for (f
= last_frag
; f
!= NULL
; f
= f
->fr_next
)
1208 if (f
->fr_address
<= r
->u
.b
.r
.address
1209 && r
->u
.b
.r
.address
< f
->fr_address
+ f
->fr_fix
)
1212 for (f
= seginfo
->frchainP
->frch_root
; f
!= NULL
; f
= f
->fr_next
)
1213 if (f
->fr_address
<= r
->u
.b
.r
.address
1214 && r
->u
.b
.r
.address
< f
->fr_address
+ f
->fr_fix
)
1217 for (f
= seginfo
->frchainP
->frch_root
; f
!= NULL
; f
= f
->fr_next
)
1218 if (f
->fr_address
<= r
->u
.b
.r
.address
1219 && r
->u
.b
.r
.address
<= f
->fr_address
+ f
->fr_fix
)
1222 as_bad_where (r
->file
, r
->line
,
1223 _("reloc not within (fixed part of) section"));
1228 write_relocs (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1229 void *xxx ATTRIBUTE_UNUSED
)
1231 segment_info_type
*seginfo
= seg_info (sec
);
1233 struct reloc_list
*my_reloc_list
, **rp
, *r
;
1238 /* If seginfo is NULL, we did not create this section; don't do
1239 anything with it. */
1240 if (seginfo
== NULL
)
1244 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
1248 #ifdef RELOC_EXPANSION_POSSIBLE
1249 n
*= MAX_RELOC_EXPANSION
;
1252 /* Extract relocs for this section from reloc_list. */
1255 my_reloc_list
= NULL
;
1256 while ((r
= *rp
) != NULL
)
1258 if (r
->u
.b
.sec
== sec
)
1261 r
->next
= my_reloc_list
;
1269 relocs
= XCNEWVEC (arelent
*, n
);
1274 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1279 #ifndef RELOC_EXPANSION_POSSIBLE
1288 fx_size
= fixp
->fx_size
;
1289 slack
= TC_FX_SIZE_SLACK (fixp
);
1291 fx_size
= fx_size
> slack
? fx_size
- slack
: 0;
1292 loc
= fixp
->fx_where
+ fx_size
;
1293 if (slack
>= 0 && loc
> fixp
->fx_frag
->fr_fix
)
1294 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1295 _("internal error: fixup not contained within frag"));
1297 #ifdef obj_fixup_removed_symbol
1298 if (fixp
->fx_addsy
&& symbol_removed_p (fixp
->fx_addsy
))
1299 obj_fixup_removed_symbol (&fixp
->fx_addsy
);
1300 if (fixp
->fx_subsy
&& symbol_removed_p (fixp
->fx_subsy
))
1301 obj_fixup_removed_symbol (&fixp
->fx_subsy
);
1304 #ifndef RELOC_EXPANSION_POSSIBLE
1305 *reloc
= tc_gen_reloc (sec
, fixp
);
1307 reloc
= tc_gen_reloc (sec
, fixp
);
1312 while (r
!= NULL
&& r
->u
.b
.r
.address
< (*reloc
)->address
)
1314 fragS
*f
= get_frag_for_reloc (last_frag
, seginfo
, r
);
1318 relocs
[n
++] = &r
->u
.b
.r
;
1319 install_reloc (sec
, &r
->u
.b
.r
, f
, r
->file
, r
->line
);
1323 #ifdef GAS_SORT_RELOCS
1324 if (n
!= 0 && (*reloc
)->address
< relocs
[n
- 1]->address
)
1328 bfd_vma look
= (*reloc
)->address
;
1331 size_t mid
= (lo
+ hi
) / 2;
1332 if (relocs
[mid
]->address
> look
)
1337 if (relocs
[mid
]->address
== look
)
1341 while (lo
< hi
&& relocs
[lo
]->address
== look
)
1343 memmove (relocs
+ lo
+ 1, relocs
+ lo
,
1344 (n
- lo
) * sizeof (*relocs
));
1346 relocs
[lo
] = *reloc
;
1350 relocs
[n
++] = *reloc
;
1351 install_reloc (sec
, *reloc
, fixp
->fx_frag
,
1352 fixp
->fx_file
, fixp
->fx_line
);
1353 #ifndef RELOC_EXPANSION_POSSIBLE
1363 fragS
*f
= get_frag_for_reloc (last_frag
, seginfo
, r
);
1367 relocs
[n
++] = &r
->u
.b
.r
;
1368 install_reloc (sec
, &r
->u
.b
.r
, f
, r
->file
, r
->line
);
1375 unsigned int k
, j
, nsyms
;
1377 sympp
= bfd_get_outsymbols (stdoutput
);
1378 nsyms
= bfd_get_symcount (stdoutput
);
1379 for (k
= 0; k
< n
; k
++)
1380 if (((*relocs
[k
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1382 for (j
= 0; j
< nsyms
; j
++)
1383 if (sympp
[j
] == *relocs
[k
]->sym_ptr_ptr
)
1391 bfd_set_reloc (stdoutput
, sec
, n
? relocs
: NULL
, n
);
1393 #ifdef SET_SECTION_RELOCS
1394 SET_SECTION_RELOCS (sec
, relocs
, n
);
1401 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1402 for (k
= 0; k
< n
; k
++)
1404 arelent
*rel
= relocs
[k
];
1405 asymbol
*s
= *rel
->sym_ptr_ptr
;
1406 fprintf (stderr
, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1407 k
, rel
, (unsigned long)rel
->address
, s
->name
,
1408 (unsigned long)rel
->addend
);
1415 compress_frag (bool use_zstd
, void *ctx
, const char *contents
, int in_size
,
1416 fragS
**last_newf
, struct obstack
*ob
)
1419 int total_out_size
= 0;
1420 fragS
*f
= *last_newf
;
1424 /* Call the compression routine repeatedly until it has finished
1425 processing the frag. */
1428 /* Reserve all the space available in the current chunk.
1429 If none is available, start a new frag. */
1430 avail_out
= obstack_room (ob
);
1433 obstack_finish (ob
);
1434 f
= frag_alloc (ob
);
1435 f
->fr_type
= rs_fill
;
1436 (*last_newf
)->fr_next
= f
;
1438 avail_out
= obstack_room (ob
);
1441 as_fatal (_("can't extend frag"));
1442 next_out
= obstack_next_free (ob
);
1443 obstack_blank_fast (ob
, avail_out
);
1444 out_size
= compress_data (use_zstd
, ctx
, &contents
, &in_size
, &next_out
,
1449 f
->fr_fix
+= out_size
;
1450 total_out_size
+= out_size
;
1452 /* Return unused space. */
1454 obstack_blank_fast (ob
, -avail_out
);
1457 return total_out_size
;
1461 compress_debug (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
1463 segment_info_type
*seginfo
= seg_info (sec
);
1464 bfd_size_type uncompressed_size
= sec
->size
;
1465 flagword flags
= bfd_section_flags (sec
);
1468 || uncompressed_size
< 32
1469 || (flags
& SEC_HAS_CONTENTS
) == 0)
1472 const char *section_name
= bfd_section_name (sec
);
1473 if (!startswith (section_name
, ".debug_")
1474 && !startswith (section_name
, ".gnu.debuglto_.debug_")
1475 && !startswith (section_name
, ".gnu.linkonce.wi."))
1478 bool use_zstd
= abfd
->flags
& BFD_COMPRESS_ZSTD
;
1479 void *ctx
= compress_init (use_zstd
);
1483 unsigned int header_size
;
1484 if ((abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
1487 header_size
= bfd_get_compression_header_size (stdoutput
, NULL
);
1489 /* Create a new frag to contain the compression header. */
1490 struct obstack
*ob
= &seginfo
->frchainP
->frch_obstack
;
1491 fragS
*first_newf
= frag_alloc (ob
);
1492 if (obstack_room (ob
) < header_size
)
1493 first_newf
= frag_alloc (ob
);
1494 if (obstack_room (ob
) < header_size
)
1495 as_fatal (ngettext ("can't extend frag %lu char",
1496 "can't extend frag %lu chars",
1497 (unsigned long) header_size
),
1498 (unsigned long) header_size
);
1499 fragS
*last_newf
= first_newf
;
1500 obstack_blank_fast (ob
, header_size
);
1501 last_newf
->fr_type
= rs_fill
;
1502 last_newf
->fr_fix
= header_size
;
1503 char *header
= last_newf
->fr_literal
;
1504 bfd_size_type compressed_size
= header_size
;
1506 /* Stream the frags through the compression engine, adding new frags
1507 as necessary to accommodate the compressed output. */
1508 for (fragS
*f
= seginfo
->frchainP
->frch_root
;
1517 gas_assert (f
->fr_type
== rs_fill
);
1520 out_size
= compress_frag (use_zstd
, ctx
, f
->fr_literal
, f
->fr_fix
,
1524 compressed_size
+= out_size
;
1526 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1527 fill_size
= f
->fr_var
;
1528 count
= f
->fr_offset
;
1529 gas_assert (count
>= 0);
1530 if (fill_size
&& count
)
1534 out_size
= compress_frag (use_zstd
, ctx
, fill_literal
,
1535 (int)fill_size
, &last_newf
, ob
);
1538 compressed_size
+= out_size
;
1543 /* Flush the compression state. */
1550 /* Reserve all the space available in the current chunk.
1551 If none is available, start a new frag. */
1552 avail_out
= obstack_room (ob
);
1557 obstack_finish (ob
);
1558 newf
= frag_alloc (ob
);
1559 newf
->fr_type
= rs_fill
;
1560 last_newf
->fr_next
= newf
;
1562 avail_out
= obstack_room (ob
);
1565 as_fatal (_("can't extend frag"));
1566 next_out
= obstack_next_free (ob
);
1567 obstack_blank_fast (ob
, avail_out
);
1568 int x
= compress_finish (use_zstd
, ctx
, &next_out
, &avail_out
, &out_size
);
1572 last_newf
->fr_fix
+= out_size
;
1573 compressed_size
+= out_size
;
1575 /* Return unused space. */
1577 obstack_blank_fast (ob
, -avail_out
);
1583 /* PR binutils/18087: If compression didn't make the section smaller,
1584 just keep it uncompressed. */
1585 if (compressed_size
>= uncompressed_size
)
1588 /* Replace the uncompressed frag list with the compressed frag list. */
1589 seginfo
->frchainP
->frch_root
= first_newf
;
1590 seginfo
->frchainP
->frch_last
= last_newf
;
1592 /* Update the section size and its name. */
1593 bfd_update_compression_header (abfd
, (bfd_byte
*) header
, sec
);
1594 bool x
= bfd_set_section_size (sec
, compressed_size
);
1596 if ((abfd
->flags
& BFD_COMPRESS_GABI
) == 0
1597 && section_name
[1] == 'd')
1599 char *compressed_name
= bfd_debug_name_to_zdebug (abfd
, section_name
);
1600 bfd_rename_section (sec
, compressed_name
);
1604 #ifndef md_generate_nops
1605 /* Genenerate COUNT bytes of no-op instructions to WHERE. A target
1606 backend must override this with proper no-op instructions. */
1609 md_generate_nops (fragS
*f ATTRIBUTE_UNUSED
,
1610 char *where ATTRIBUTE_UNUSED
,
1611 offsetT count ATTRIBUTE_UNUSED
,
1612 int control ATTRIBUTE_UNUSED
)
1614 as_bad (_("unimplemented .nops directive"));
1619 write_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
1621 void *xxx ATTRIBUTE_UNUSED
)
1623 segment_info_type
*seginfo
= seg_info (sec
);
1624 addressT offset
= 0;
1627 /* Write out the frags. */
1629 || !(bfd_section_flags (sec
) & SEC_HAS_CONTENTS
))
1632 for (f
= seginfo
->frchainP
->frch_root
;
1641 gas_assert (f
->fr_type
== rs_fill
|| f
->fr_type
== rs_fill_nop
);
1644 x
= bfd_set_section_contents (stdoutput
, sec
,
1645 f
->fr_literal
, (file_ptr
) offset
,
1646 (bfd_size_type
) f
->fr_fix
);
1648 as_fatal (ngettext ("can't write %ld byte "
1649 "to section %s of %s: '%s'",
1650 "can't write %ld bytes "
1651 "to section %s of %s: '%s'",
1654 bfd_section_name (sec
), bfd_get_filename (stdoutput
),
1655 bfd_errmsg (bfd_get_error ()));
1656 offset
+= f
->fr_fix
;
1659 fill_size
= f
->fr_var
;
1660 count
= f
->fr_offset
;
1661 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1663 if (f
->fr_type
== rs_fill_nop
)
1665 gas_assert (count
>= 0 && fill_size
== 1);
1668 char *buf
= xmalloc (count
);
1669 md_generate_nops (f
, buf
, count
, *fill_literal
);
1670 x
= bfd_set_section_contents
1671 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1672 (bfd_size_type
) count
);
1674 as_fatal (ngettext ("can't fill %ld byte "
1675 "in section %s of %s: '%s'",
1676 "can't fill %ld bytes "
1677 "in section %s of %s: '%s'",
1680 bfd_section_name (sec
),
1681 bfd_get_filename (stdoutput
),
1682 bfd_errmsg (bfd_get_error ()));
1689 gas_assert (count
>= 0);
1690 if (fill_size
&& count
)
1693 if (fill_size
> sizeof (buf
))
1695 /* Do it the old way. Can this ever happen? */
1698 x
= bfd_set_section_contents (stdoutput
, sec
,
1701 (bfd_size_type
) fill_size
);
1703 as_fatal (ngettext ("can't fill %ld byte "
1704 "in section %s of %s: '%s'",
1705 "can't fill %ld bytes "
1706 "in section %s of %s: '%s'",
1709 bfd_section_name (sec
),
1710 bfd_get_filename (stdoutput
),
1711 bfd_errmsg (bfd_get_error ()));
1712 offset
+= fill_size
;
1717 /* Build a buffer full of fill objects and output it as
1718 often as necessary. This saves on the overhead of
1719 potentially lots of bfd_set_section_contents calls. */
1723 n_per_buf
= sizeof (buf
);
1724 memset (buf
, *fill_literal
, n_per_buf
);
1729 n_per_buf
= sizeof (buf
) / fill_size
;
1730 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1731 memcpy (bufp
, fill_literal
, fill_size
);
1733 for (; count
> 0; count
-= n_per_buf
)
1735 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1736 x
= bfd_set_section_contents
1737 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1738 (bfd_size_type
) n_per_buf
* fill_size
);
1740 as_fatal (ngettext ("can't fill %ld byte "
1741 "in section %s of %s: '%s'",
1742 "can't fill %ld bytes "
1743 "in section %s of %s: '%s'",
1744 (long) (n_per_buf
* fill_size
)),
1745 (long) (n_per_buf
* fill_size
),
1746 bfd_section_name (sec
),
1747 bfd_get_filename (stdoutput
),
1748 bfd_errmsg (bfd_get_error ()));
1749 offset
+= n_per_buf
* fill_size
;
1757 merge_data_into_text (void)
1759 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1760 seg_info (data_section
)->frchainP
->frch_root
;
1761 seg_info (text_section
)->frchainP
->frch_last
=
1762 seg_info (data_section
)->frchainP
->frch_last
;
1763 seg_info (data_section
)->frchainP
= 0;
1774 /* Count symbols. We can't rely on a count made by the loop in
1775 write_object_file, because *_frob_file may add a new symbol or
1776 two. Generate unused section symbols only if needed. */
1778 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1779 if (!symbol_removed_p (symp
)
1780 && (bfd_keep_unused_section_symbols (stdoutput
)
1781 || !symbol_section_p (symp
)
1782 || symbol_used_in_reloc_p (symp
)))
1788 bfd_size_type amt
= (bfd_size_type
) nsyms
* sizeof (asymbol
*);
1790 asympp
= (asymbol
**) bfd_alloc (stdoutput
, amt
);
1791 symp
= symbol_rootP
;
1792 for (i
= 0; i
< nsyms
; symp
= symbol_next (symp
))
1793 if (!symbol_removed_p (symp
)
1794 && (bfd_keep_unused_section_symbols (stdoutput
)
1795 || !symbol_section_p (symp
)
1796 || symbol_used_in_reloc_p (symp
)))
1798 asympp
[i
] = symbol_get_bfdsym (symp
);
1799 if (asympp
[i
]->flags
!= BSF_SECTION_SYM
1800 || !(bfd_is_const_section (asympp
[i
]->section
)
1801 && asympp
[i
]->section
->symbol
== asympp
[i
]))
1802 asympp
[i
]->flags
|= BSF_KEEP
;
1803 symbol_mark_written (symp
);
1804 /* Include this section symbol in the symbol table. */
1805 if (symbol_section_p (symp
))
1806 asympp
[i
]->flags
|= BSF_SECTION_SYM_USED
;
1812 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1813 gas_assert (result
);
1814 symbol_table_frozen
= 1;
1817 /* Finish the subsegments. After every sub-segment, we fake an
1818 ".align ...". This conforms to BSD4.2 brain-damage. We then fake
1819 ".fill 0" because that is the kind of frag that requires least
1820 thought. ".align" frags like to have a following frag since that
1821 makes calculating their intended length trivial. */
1823 #ifndef SUB_SEGMENT_ALIGN
1825 /* The last subsegment gets an alignment corresponding to the alignment
1826 of the section. This allows proper nop-filling at the end of
1827 code-bearing sections. */
1828 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1829 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
1830 && !do_not_pad_sections_to_alignment \
1831 ? get_recorded_alignment (SEG) \
1834 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1839 subsegs_finish_section (asection
*s
)
1841 struct frchain
*frchainP
;
1842 segment_info_type
*seginfo
= seg_info (s
);
1846 for (frchainP
= seginfo
->frchainP
;
1848 frchainP
= frchainP
->frch_next
)
1852 subseg_set (s
, frchainP
->frch_subseg
);
1854 /* This now gets called even if we had errors. In that case,
1855 any alignment is meaningless, and, moreover, will look weird
1856 if we are generating a listing. */
1858 do_not_pad_sections_to_alignment
= 1;
1860 alignment
= SUB_SEGMENT_ALIGN (now_seg
, frchainP
);
1861 if ((bfd_section_flags (now_seg
) & SEC_MERGE
)
1862 && now_seg
->entsize
)
1864 unsigned int entsize
= now_seg
->entsize
;
1867 while ((entsize
& 1) == 0)
1873 if (entalign
> alignment
)
1874 alignment
= entalign
;
1877 if (subseg_text_p (now_seg
))
1878 frag_align_code (alignment
, 0);
1880 frag_align (alignment
, 0, 0);
1882 /* frag_align will have left a new frag.
1883 Use this last frag for an empty ".fill".
1885 For this segment ...
1886 Create a last frag. Do not leave a "being filled in frag". */
1887 frag_wane (frag_now
);
1888 frag_now
->fr_fix
= 0;
1889 know (frag_now
->fr_next
== NULL
);
1894 subsegs_finish (void)
1898 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
1899 subsegs_finish_section (s
);
1904 create_obj_attrs_section (void)
1911 size
= bfd_elf_obj_attr_size (stdoutput
);
1915 name
= get_elf_backend_data (stdoutput
)->obj_attrs_section
;
1917 name
= ".gnu.attributes";
1918 s
= subseg_new (name
, 0);
1919 elf_section_type (s
)
1920 = get_elf_backend_data (stdoutput
)->obj_attrs_section_type
;
1921 bfd_set_section_flags (s
, SEC_READONLY
| SEC_DATA
);
1923 p
= frag_more (size
);
1924 bfd_elf_set_obj_attr_contents (stdoutput
, (bfd_byte
*)p
, size
);
1926 subsegs_finish_section (s
);
1927 relax_segment (seg_info (s
)->frchainP
->frch_root
, s
, 0);
1928 size_seg (stdoutput
, s
, NULL
);
1931 /* Create a relocation against an entry in a GNU Build attribute section. */
1934 create_note_reloc (segT sec
,
1936 bfd_size_type note_offset
,
1937 bfd_size_type desc2_offset
,
1943 struct reloc_list
* reloc
;
1945 reloc
= XNEW (struct reloc_list
);
1947 /* We create a .b type reloc as resolve_reloc_expr_symbols() has already been called. */
1948 reloc
->u
.b
.sec
= sec
;
1949 reloc
->u
.b
.s
= symbol_get_bfdsym (sym
);
1950 reloc
->u
.b
.r
.sym_ptr_ptr
= & reloc
->u
.b
.s
;
1951 reloc
->u
.b
.r
.address
= note_offset
+ desc2_offset
;
1952 reloc
->u
.b
.r
.addend
= addend
;
1953 reloc
->u
.b
.r
.howto
= bfd_reloc_type_lookup (stdoutput
, reloc_type
);
1955 if (reloc
->u
.b
.r
.howto
== NULL
)
1957 as_bad (_("unable to create reloc for build note"));
1961 reloc
->file
= N_("<gnu build note>");
1964 reloc
->next
= reloc_list
;
1967 /* For REL relocs, store the addend in the section. */
1968 if (! sec
->use_rela_p
1969 /* The SH target is a special case that uses RELA relocs
1970 but still stores the addend in the word being relocated. */
1971 || strstr (bfd_get_target (stdoutput
), "-sh") != NULL
)
1975 /* Zero out the addend, since it is now stored in the note. */
1976 reloc
->u
.b
.r
.addend
= 0;
1978 if (target_big_endian
)
1980 for (i
= desc2_size
; addend
!= 0 && i
> 0; addend
>>= 8, i
--)
1981 note
[desc2_offset
+ i
- 1] = (addend
& 0xff);
1985 for (i
= 0; addend
!= 0 && i
< desc2_size
; addend
>>= 8, i
++)
1986 note
[desc2_offset
+ i
] = (addend
& 0xff);
1992 maybe_generate_build_notes (void)
1999 offsetT desc2_offset
;
2004 if (! flag_generate_build_notes
2005 || bfd_get_section_by_name (stdoutput
,
2006 GNU_BUILD_ATTRS_SECTION_NAME
) != NULL
)
2009 /* Create a GNU Build Attribute section. */
2010 sec
= subseg_new (GNU_BUILD_ATTRS_SECTION_NAME
, false);
2011 elf_section_type (sec
) = SHT_NOTE
;
2012 bfd_set_section_flags (sec
, (SEC_READONLY
| SEC_HAS_CONTENTS
| SEC_DATA
2014 bfd_set_section_alignment (sec
, 2);
2016 /* Work out the size of the notes that we will create,
2017 and the relocation we should use. */
2018 if (bfd_arch_bits_per_address (stdoutput
) <= 32)
2021 desc_size
= 8; /* Two 4-byte offsets. */
2024 /* FIXME: The BFD backend for the CRX target does not support the
2025 BFD_RELOC_32, even though it really should. Likewise for the
2026 CR16 target. So we have special case code here... */
2027 if (strstr (bfd_get_target (stdoutput
), "-crx") != NULL
)
2028 desc_reloc
= BFD_RELOC_CRX_NUM32
;
2029 else if (strstr (bfd_get_target (stdoutput
), "-cr16") != NULL
)
2030 desc_reloc
= BFD_RELOC_CR16_NUM32
;
2032 desc_reloc
= BFD_RELOC_32
;
2037 desc_size
= 16; /* Two 8-byte offsets. */
2039 /* FIXME: The BFD backend for the IA64 target does not support the
2040 BFD_RELOC_64, even though it really should. The HPPA backend
2041 has a similar issue, although it does not support BFD_RELOCs at
2042 all! So we have special case code to handle these targets. */
2043 if (strstr (bfd_get_target (stdoutput
), "-ia64") != NULL
)
2044 desc_reloc
= target_big_endian
? BFD_RELOC_IA64_DIR32MSB
: BFD_RELOC_IA64_DIR32LSB
;
2045 else if (strstr (bfd_get_target (stdoutput
), "-hppa") != NULL
)
2046 desc_reloc
= 80; /* R_PARISC_DIR64. */
2048 desc_reloc
= BFD_RELOC_64
;
2051 /* We have to create a note for *each* code section.
2052 Linker garbage collection might discard some. */
2056 for (sym
= symbol_rootP
; sym
!= NULL
; sym
= symbol_next (sym
))
2057 if ((bsym
= symbol_get_bfdsym (sym
)) != NULL
2058 && bsym
->flags
& BSF_SECTION_SYM
2059 && bsym
->section
!= NULL
2060 /* Skip linkonce sections - we cannot use these section symbols as they may disappear. */
2061 && (bsym
->section
->flags
& (SEC_CODE
| SEC_LINK_ONCE
)) == SEC_CODE
2062 /* Not all linkonce sections are flagged... */
2063 && !startswith (S_GET_NAME (sym
), ".gnu.linkonce"))
2065 /* Create a version note. */
2067 note
= frag_more (note_size
);
2068 memset (note
, 0, note_size
);
2070 if (target_big_endian
)
2072 note
[3] = 8; /* strlen (name) + 1. */
2073 note
[7] = desc_size
; /* Two N-byte offsets. */
2074 note
[10] = NT_GNU_BUILD_ATTRIBUTE_OPEN
>> 8;
2075 note
[11] = NT_GNU_BUILD_ATTRIBUTE_OPEN
& 0xff;
2079 note
[0] = 8; /* strlen (name) + 1. */
2080 note
[4] = desc_size
; /* Two N-byte offsets. */
2081 note
[8] = NT_GNU_BUILD_ATTRIBUTE_OPEN
& 0xff;
2082 note
[9] = NT_GNU_BUILD_ATTRIBUTE_OPEN
>> 8;
2085 /* The a1 version number indicates that this note was
2086 generated by the assembler and not the gcc annobin plugin. */
2087 memcpy (note
+ 12, "GA$\x013a1", 8);
2089 /* Create a relocation to install the start address of the note... */
2090 create_note_reloc (sec
, sym
, total_size
, 20, desc_size
/ 2, desc_reloc
, 0, note
);
2092 /* ...and another one to install the end address. */
2093 create_note_reloc (sec
, sym
, total_size
, desc2_offset
,
2096 bfd_section_size (bsym
->section
),
2099 /* Mark the section symbol used in relocation so that it will be
2100 included in the symbol table. */
2101 symbol_mark_used_in_reloc (sym
);
2103 total_size
+= note_size
;
2104 /* FIXME: Maybe add a note recording the assembler command line and version ? */
2107 /* Install the note(s) into the section. */
2109 bfd_set_section_contents (stdoutput
, sec
, (bfd_byte
*) note
, 0, total_size
);
2110 subsegs_finish_section (sec
);
2111 relax_segment (seg_info (sec
)->frchainP
->frch_root
, sec
, 0);
2112 size_seg (stdoutput
, sec
, NULL
);
2114 #endif /* OBJ_ELF */
2116 /* Write the object file. */
2119 write_object_file (void)
2121 struct relax_seg_info rsi
;
2122 #ifndef WORKING_DOT_WORD
2123 fragS
*fragP
; /* Track along all frags. */
2128 #ifdef md_pre_output_hook
2132 #ifdef md_pre_relax_hook
2136 /* From now on, we don't care about sub-segments. Build one frag chain
2137 for each segment. Linked through fr_next. */
2139 /* Remove the sections created by gas for its own purposes. */
2143 bfd_section_list_remove (stdoutput
, reg_section
);
2144 bfd_section_list_remove (stdoutput
, expr_section
);
2145 stdoutput
->section_count
-= 2;
2147 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
2150 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
2152 /* We have two segments. If user gave -R flag, then we must put the
2153 data frags into the text segment. Do this before relaxing so
2154 we know to take advantage of -R and make shorter addresses. */
2155 if (flag_readonly_data_in_text
)
2157 merge_data_into_text ();
2163 #ifndef WORKING_DOT_WORD
2164 /* We need to reset the markers in the broken word list and
2165 associated frags between calls to relax_segment (via
2166 relax_seg). Since the broken word list is global, we do it
2167 once per round, rather than locally in relax_segment for each
2169 struct broken_word
*brokp
;
2171 for (brokp
= broken_words
;
2172 brokp
!= (struct broken_word
*) NULL
;
2173 brokp
= brokp
->next_broken_word
)
2177 if (brokp
->dispfrag
!= (fragS
*) NULL
2178 && brokp
->dispfrag
->fr_type
== rs_broken_word
)
2179 brokp
->dispfrag
->fr_subtype
= 0;
2184 bfd_map_over_sections (stdoutput
, relax_seg
, &rsi
);
2190 /* Note - Most ports will use the default value of
2191 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
2192 local symbols to be resolved, removing their frag information.
2193 Some ports however, will not have finished relaxing all of
2194 their frags and will still need the local symbol frag
2195 information. These ports can set
2196 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
2197 finalize_syms
= TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
;
2199 bfd_map_over_sections (stdoutput
, size_seg
, (char *) 0);
2201 /* Relaxation has completed. Freeze all syms. */
2204 dwarf2dbg_final_check ();
2206 #ifdef md_post_relax_hook
2212 create_obj_attrs_section ();
2215 #ifndef WORKING_DOT_WORD
2217 struct broken_word
*lie
;
2218 struct broken_word
**prevP
;
2220 prevP
= &broken_words
;
2221 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
2226 subseg_change (lie
->seg
, lie
->subseg
);
2227 exp
.X_op
= O_subtract
;
2228 exp
.X_add_symbol
= lie
->add
;
2229 exp
.X_op_symbol
= lie
->sub
;
2230 exp
.X_add_number
= lie
->addnum
;
2231 #ifdef TC_CONS_FIX_NEW
2232 TC_CONS_FIX_NEW (lie
->frag
,
2233 lie
->word_goes_here
- lie
->frag
->fr_literal
,
2234 2, &exp
, TC_PARSE_CONS_RETURN_NONE
);
2236 fix_new_exp (lie
->frag
,
2237 lie
->word_goes_here
- lie
->frag
->fr_literal
,
2238 2, &exp
, 0, BFD_RELOC_16
);
2240 *prevP
= lie
->next_broken_word
;
2243 prevP
= &(lie
->next_broken_word
);
2245 for (lie
= broken_words
; lie
;)
2247 struct broken_word
*untruth
;
2249 addressT table_addr
;
2250 addressT from_addr
, to_addr
;
2253 subseg_change (lie
->seg
, lie
->subseg
);
2254 fragP
= lie
->dispfrag
;
2256 /* Find out how many broken_words go here. */
2259 untruth
&& untruth
->dispfrag
== fragP
;
2260 untruth
= untruth
->next_broken_word
)
2261 if (untruth
->added
== 1)
2264 table_ptr
= lie
->dispfrag
->fr_opcode
;
2265 table_addr
= (lie
->dispfrag
->fr_address
2266 + (table_ptr
- lie
->dispfrag
->fr_literal
));
2267 /* Create the jump around the long jumps. This is a short
2268 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
2269 from_addr
= table_addr
;
2270 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
2271 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
2273 table_ptr
+= md_short_jump_size
;
2274 table_addr
+= md_short_jump_size
;
2277 lie
&& lie
->dispfrag
== fragP
;
2278 m
++, lie
= lie
->next_broken_word
)
2280 if (lie
->added
== 2)
2282 /* Patch the jump table. */
2283 for (untruth
= (struct broken_word
*) (fragP
->fr_symbol
);
2284 untruth
&& untruth
->dispfrag
== fragP
;
2285 untruth
= untruth
->next_broken_word
)
2287 if (untruth
->use_jump
== lie
)
2289 /* This is the offset from ??? to table_ptr+0.
2290 The target is the same for all users of this
2291 md_long_jump, but the "sub" bases (and hence the
2292 offsets) may be different. */
2293 addressT to_word
= table_addr
- S_GET_VALUE (untruth
->sub
);
2294 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
2295 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word
, untruth
);
2297 md_number_to_chars (untruth
->word_goes_here
, to_word
, 2);
2301 /* Install the long jump. */
2302 /* This is a long jump from table_ptr+0 to the final target. */
2303 from_addr
= table_addr
;
2304 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
2305 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
2307 table_ptr
+= md_long_jump_size
;
2308 table_addr
+= md_long_jump_size
;
2312 #endif /* not WORKING_DOT_WORD */
2314 /* Resolve symbol values. This needs to be done before processing
2320 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2321 resolve_symbol_value (symp
);
2323 resolve_local_symbol_values ();
2324 resolve_reloc_expr_symbols ();
2328 maybe_generate_build_notes ();
2331 #ifdef tc_frob_file_before_adjust
2332 tc_frob_file_before_adjust ();
2334 #ifdef obj_frob_file_before_adjust
2335 obj_frob_file_before_adjust ();
2338 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
2340 #ifdef tc_frob_file_before_fix
2341 tc_frob_file_before_fix ();
2343 #ifdef obj_frob_file_before_fix
2344 obj_frob_file_before_fix ();
2347 bfd_map_over_sections (stdoutput
, fix_segment
, (char *) 0);
2349 /* Set up symbol table, and write it out. */
2353 bool skip_next_symbol
= false;
2355 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2360 if (skip_next_symbol
)
2362 /* Don't do anything besides moving the value of the
2363 symbol from the GAS value-field to the BFD value-field. */
2364 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
2365 skip_next_symbol
= false;
2369 if (symbol_mri_common_p (symp
))
2371 if (S_IS_EXTERNAL (symp
))
2372 as_bad (_("%s: global symbols not supported in common sections"),
2374 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2378 name
= S_GET_NAME (symp
);
2382 decode_local_label_name ((char *) S_GET_NAME (symp
));
2383 /* They only differ if `name' is a fb or dollar local
2385 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
2386 as_bad (_("local label `%s' is not defined"), name2
);
2389 /* Do it again, because adjust_reloc_syms might introduce
2390 more symbols. They'll probably only be section symbols,
2391 but they'll still need to have the values computed. */
2392 resolve_symbol_value (symp
);
2394 /* Skip symbols which were equated to undefined or common
2396 if (symbol_equated_reloc_p (symp
)
2397 || S_IS_WEAKREFR (symp
))
2399 const char *sname
= S_GET_NAME (symp
);
2401 if (S_IS_COMMON (symp
)
2402 && !TC_FAKE_LABEL (sname
)
2403 && !S_IS_WEAKREFR (symp
))
2405 expressionS
*e
= symbol_get_value_expression (symp
);
2407 as_bad (_("`%s' can't be equated to common symbol `%s'"),
2408 sname
, S_GET_NAME (e
->X_add_symbol
));
2410 if (S_GET_SEGMENT (symp
) == reg_section
)
2412 /* Report error only if we know the symbol name. */
2413 if (S_GET_NAME (symp
) != reg_section
->name
)
2414 as_bad (_("can't make global register symbol `%s'"),
2417 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2421 #ifdef obj_frob_symbol
2422 obj_frob_symbol (symp
, punt
);
2424 #ifdef tc_frob_symbol
2425 if (! punt
|| symbol_used_in_reloc_p (symp
))
2426 tc_frob_symbol (symp
, punt
);
2429 /* If we don't want to keep this symbol, splice it out of
2430 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2431 want section symbols. Otherwise, we skip local symbols
2432 and symbols that the frob_symbol macros told us to punt,
2433 but we keep such symbols if they are used in relocs. */
2434 if (symp
== abs_section_sym
2435 || (! EMIT_SECTION_SYMBOLS
2436 && symbol_section_p (symp
))
2437 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2438 opposites. Sometimes the former checks flags and the
2439 latter examines the name... */
2440 || (!S_IS_EXTERNAL (symp
)
2441 && (punt
|| S_IS_LOCAL (symp
) ||
2442 (S_IS_WEAKREFD (symp
) && ! symbol_used_p (symp
)))
2443 && ! symbol_used_in_reloc_p (symp
)))
2445 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2447 /* After symbol_remove, symbol_next(symp) still returns
2448 the one that came after it in the chain. So we don't
2449 need to do any extra cleanup work here. */
2453 /* Make sure we really got a value for the symbol. */
2454 if (! symbol_resolved_p (symp
))
2456 as_bad (_("can't resolve value for symbol `%s'"),
2458 symbol_mark_resolved (symp
);
2461 /* Set the value into the BFD symbol. Up til now the value
2462 has only been kept in the gas symbolS struct. */
2463 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
2465 /* A warning construct is a warning symbol followed by the
2466 symbol warned about. Don't let anything object-format or
2467 target-specific muck with it; it's ready for output. */
2468 if (symbol_get_bfdsym (symp
)->flags
& BSF_WARNING
)
2469 skip_next_symbol
= true;
2473 /* Now do any format-specific adjustments to the symbol table, such
2474 as adding file symbols. */
2475 #ifdef tc_adjust_symtab
2476 tc_adjust_symtab ();
2478 #ifdef obj_adjust_symtab
2479 obj_adjust_symtab ();
2482 /* Stop if there is an error. */
2483 if (!flag_always_generate_output
&& had_errors ())
2486 /* Now that all the sizes are known, and contents correct, we can
2487 start writing to the file. */
2490 /* If *_frob_file changes the symbol value at this point, it is
2491 responsible for moving the changed value into symp->bsym->value
2492 as well. Hopefully all symbol value changing can be done in
2497 #ifdef obj_frob_file
2500 #ifdef obj_coff_generate_pdata
2501 obj_coff_generate_pdata ();
2504 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
2506 #ifdef tc_frob_file_after_relocs
2507 tc_frob_file_after_relocs ();
2509 #ifdef obj_frob_file_after_relocs
2510 obj_frob_file_after_relocs ();
2513 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
2514 if (IS_ELF
&& flag_use_elf_stt_common
)
2515 stdoutput
->flags
|= BFD_CONVERT_ELF_COMMON
| BFD_USE_ELF_STT_COMMON
;
2518 /* Once all relocations have been written, we can compress the
2519 contents of the debug sections. This needs to be done before
2520 we start writing any sections, because it will affect the file
2521 layout, which is fixed once we start writing contents. */
2522 if (flag_compress_debug
!= COMPRESS_DEBUG_NONE
)
2524 flagword flags
= BFD_COMPRESS
;
2525 if (flag_compress_debug
== COMPRESS_DEBUG_GABI_ZLIB
)
2526 flags
= BFD_COMPRESS
| BFD_COMPRESS_GABI
;
2527 else if (flag_compress_debug
== COMPRESS_DEBUG_ZSTD
)
2528 flags
= BFD_COMPRESS
| BFD_COMPRESS_GABI
| BFD_COMPRESS_ZSTD
;
2529 stdoutput
->flags
|= flags
& bfd_applicable_file_flags (stdoutput
);
2530 if ((stdoutput
->flags
& BFD_COMPRESS
) != 0)
2531 bfd_map_over_sections (stdoutput
, compress_debug
, (char *) 0);
2534 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
2537 #ifdef TC_GENERIC_RELAX_TABLE
2538 #ifndef md_generic_table_relax_frag
2539 #define md_generic_table_relax_frag relax_frag
2542 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2545 relax_frag (segT segment
, fragS
*fragP
, long stretch
)
2547 const relax_typeS
*this_type
;
2548 const relax_typeS
*start_type
;
2549 relax_substateT next_state
;
2550 relax_substateT this_state
;
2556 const relax_typeS
*table
;
2558 target
= fragP
->fr_offset
;
2559 address
= fragP
->fr_address
+ fragP
->fr_fix
;
2560 table
= TC_GENERIC_RELAX_TABLE
;
2561 this_state
= fragP
->fr_subtype
;
2562 start_type
= this_type
= table
+ this_state
;
2563 symbolP
= fragP
->fr_symbol
;
2569 sym_frag
= symbol_get_frag (symbolP
);
2571 #ifndef DIFF_EXPR_OK
2572 know (sym_frag
!= NULL
);
2574 know (S_GET_SEGMENT (symbolP
) != absolute_section
2575 || sym_frag
== &zero_address_frag
);
2576 target
+= S_GET_VALUE (symbolP
);
2578 /* If SYM_FRAG has yet to be reached on this pass, assume it
2579 will move by STRETCH just as we did, unless there is an
2580 alignment frag between here and SYM_FRAG. An alignment may
2581 well absorb any STRETCH, and we don't want to choose a larger
2582 branch insn by overestimating the needed reach of this
2583 branch. It isn't critical to calculate TARGET exactly; We
2584 know we'll be doing another pass if STRETCH is non-zero. */
2587 && sym_frag
->relax_marker
!= fragP
->relax_marker
2588 && S_GET_SEGMENT (symbolP
) == segment
)
2591 || sym_frag
->region
== fragP
->region
)
2593 /* If we get here we know we have a forward branch. This
2594 relax pass may have stretched previous instructions so
2595 far that omitting STRETCH would make the branch
2596 negative. Don't allow this in case the negative reach is
2597 large enough to require a larger branch instruction. */
2598 else if (target
< address
)
2603 aim
= target
- address
;
2604 #ifdef TC_PCREL_ADJUST
2605 /* Currently only the ns32k and arc needs this. */
2606 aim
+= TC_PCREL_ADJUST (fragP
);
2609 #ifdef md_prepare_relax_scan
2610 /* Formerly called M68K_AIM_KLUDGE. */
2611 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2616 /* Look backwards. */
2617 for (next_state
= this_type
->rlx_more
; next_state
;)
2618 if (aim
>= this_type
->rlx_backward
)
2622 /* Grow to next state. */
2623 this_state
= next_state
;
2624 this_type
= table
+ this_state
;
2625 next_state
= this_type
->rlx_more
;
2630 /* Look forwards. */
2631 for (next_state
= this_type
->rlx_more
; next_state
;)
2632 if (aim
<= this_type
->rlx_forward
)
2636 /* Grow to next state. */
2637 this_state
= next_state
;
2638 this_type
= table
+ this_state
;
2639 next_state
= this_type
->rlx_more
;
2643 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2645 fragP
->fr_subtype
= this_state
;
2649 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2651 /* Relax_align. Advance location counter to next address that has 'alignment'
2652 lowest order bits all 0s, return size of adjustment made. */
2653 static relax_addressT
2654 relax_align (relax_addressT address
, /* Address now. */
2655 int alignment
/* Alignment (binary). */)
2657 relax_addressT mask
;
2658 relax_addressT new_address
;
2660 mask
= ~((relax_addressT
) ~0 << alignment
);
2661 new_address
= (address
+ mask
) & (~mask
);
2662 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2664 /* We must provide lots of padding, so the linker can discard it
2665 when needed. The linker will not add extra space, ever. */
2666 new_address
+= (1 << alignment
);
2668 return (new_address
- address
);
2671 /* Now we have a segment, not a crowd of sub-segments, we can make
2676 After this, all frags in this segment have addresses that are correct
2677 within the segment. Since segments live in different file addresses,
2678 these frag addresses may not be the same as final object-file
2682 relax_segment (struct frag
*segment_frag_root
, segT segment
, int pass
)
2684 unsigned long frag_count
;
2686 relax_addressT address
;
2690 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2691 subseg_change (segment
, 0);
2693 /* For each frag in segment: count and store (a 1st guess of)
2697 for (frag_count
= 0, fragP
= segment_frag_root
;
2699 fragP
= fragP
->fr_next
, frag_count
++)
2701 fragP
->region
= region
;
2702 fragP
->relax_marker
= 0;
2703 fragP
->fr_address
= address
;
2704 address
+= fragP
->fr_fix
;
2706 switch (fragP
->fr_type
)
2709 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2716 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2718 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2721 if (offset
% fragP
->fr_var
!= 0)
2723 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2724 ngettext ("alignment padding (%lu byte) "
2725 "not a multiple of %ld",
2726 "alignment padding (%lu bytes) "
2727 "not a multiple of %ld",
2728 (unsigned long) offset
),
2729 (unsigned long) offset
, (long) fragP
->fr_var
);
2730 offset
-= (offset
% fragP
->fr_var
);
2739 /* Assume .org is nugatory. It will grow with 1st relax. */
2747 case rs_machine_dependent
:
2748 /* If fr_symbol is an expression, this call to
2749 resolve_symbol_value sets up the correct segment, which will
2750 likely be needed in md_estimate_size_before_relax. */
2751 if (fragP
->fr_symbol
)
2752 resolve_symbol_value (fragP
->fr_symbol
);
2754 address
+= md_estimate_size_before_relax (fragP
, segment
);
2757 #ifndef WORKING_DOT_WORD
2758 /* Broken words don't concern us yet. */
2759 case rs_broken_word
:
2764 /* Initial guess is always 1; doing otherwise can result in
2765 stable solutions that are larger than the minimum. */
2766 address
+= fragP
->fr_offset
= 1;
2770 address
+= eh_frame_estimate_size_before_relax (fragP
);
2774 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
2778 /* Initial estimate can be set to atleast 1 byte. */
2779 address
+= sframe_estimate_size_before_relax (fragP
);
2783 BAD_CASE (fragP
->fr_type
);
2790 unsigned long max_iterations
;
2792 /* Cumulative address adjustment. */
2795 /* Have we made any adjustment this pass? We can't just test
2796 stretch because one piece of code may have grown and another
2800 /* Most horrible, but gcc may give us some exception data that
2801 is impossible to assemble, of the form
2805 .uleb128 end - start
2811 If the leb128 is two bytes in size, then end-start is 128*128,
2812 which requires a three byte leb128. If the leb128 is three
2813 bytes in size, then end-start is 128*128-1, which requires a
2814 two byte leb128. We work around this dilemma by inserting
2815 an extra 4 bytes of alignment just after the .align. This
2816 works because the data after the align is accessed relative to
2819 This counter is used in a tiny state machine to detect
2820 whether a leb128 followed by an align is impossible to
2822 int rs_leb128_fudge
= 0;
2824 /* We want to prevent going into an infinite loop where one frag grows
2825 depending upon the location of a symbol which is in turn moved by
2826 the growing frag. eg:
2832 So we dictate that this algorithm can be at most O2. */
2833 max_iterations
= frag_count
* frag_count
;
2834 /* Check for overflow. */
2835 if (max_iterations
< frag_count
)
2836 max_iterations
= frag_count
;
2844 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2847 addressT was_address
;
2851 fragP
->relax_marker
^= 1;
2852 was_address
= fragP
->fr_address
;
2853 address
= fragP
->fr_address
+= stretch
;
2854 symbolP
= fragP
->fr_symbol
;
2855 offset
= fragP
->fr_offset
;
2857 switch (fragP
->fr_type
)
2859 case rs_fill
: /* .fill never relaxes. */
2863 #ifndef WORKING_DOT_WORD
2864 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2865 for it I do not want to write it. I do not want to have
2866 anything to do with it. This is not the proper way to
2867 implement this misfeature. */
2868 case rs_broken_word
:
2870 struct broken_word
*lie
;
2871 struct broken_word
*untruth
;
2873 /* Yes this is ugly (storing the broken_word pointer
2874 in the symbol slot). Still, this whole chunk of
2875 code is ugly, and I don't feel like doing anything
2876 about it. Think of it as stubbornness in action. */
2878 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2879 lie
&& lie
->dispfrag
== fragP
;
2880 lie
= lie
->next_broken_word
)
2886 offset
= (S_GET_VALUE (lie
->add
)
2888 - S_GET_VALUE (lie
->sub
));
2889 if (offset
<= -32768 || offset
>= 32767)
2891 if (flag_warn_displacement
)
2895 bfd_sprintf_vma (stdoutput
, buf
,
2896 (addressT
) lie
->addnum
);
2897 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2898 _(".word %s-%s+%s didn't fit"),
2899 S_GET_NAME (lie
->add
),
2900 S_GET_NAME (lie
->sub
),
2903 if (fragP
->fr_subtype
== 0)
2905 fragP
->fr_subtype
++;
2906 growth
+= md_short_jump_size
;
2909 /* Redirect *all* words of this table with the same
2910 target, lest we have to handle the case where the
2911 same target but with a offset that fits on this
2912 round overflows at the next relaxation round. */
2913 for (untruth
= (struct broken_word
*) (fragP
->fr_symbol
);
2914 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2915 untruth
= untruth
->next_broken_word
)
2916 if ((symbol_get_frag (untruth
->add
)
2917 == symbol_get_frag (lie
->add
))
2918 && (S_GET_VALUE (untruth
->add
)
2919 == S_GET_VALUE (lie
->add
)))
2922 untruth
->use_jump
= lie
;
2926 growth
+= md_long_jump_size
;
2931 } /* case rs_broken_word */
2937 addressT oldoff
, newoff
;
2939 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2941 newoff
= relax_align (address
+ fragP
->fr_fix
,
2944 if (fragP
->fr_subtype
!= 0)
2946 if (oldoff
> fragP
->fr_subtype
)
2948 if (newoff
> fragP
->fr_subtype
)
2952 growth
= newoff
- oldoff
;
2954 /* If this align happens to follow a leb128 and
2955 we have determined that the leb128 is bouncing
2956 in size, then break the cycle by inserting an
2959 && (rs_leb128_fudge
& 16) != 0
2960 && (rs_leb128_fudge
& 15) >= 2)
2962 segment_info_type
*seginfo
= seg_info (segment
);
2963 struct obstack
*ob
= &seginfo
->frchainP
->frch_obstack
;
2966 newf
= frag_alloc (ob
);
2967 obstack_blank_fast (ob
, fragP
->fr_var
);
2968 obstack_finish (ob
);
2969 memcpy (newf
, fragP
, SIZEOF_STRUCT_FRAG
);
2970 memcpy (newf
->fr_literal
,
2971 fragP
->fr_literal
+ fragP
->fr_fix
,
2973 newf
->fr_type
= rs_fill
;
2974 newf
->fr_address
= address
+ fragP
->fr_fix
+ newoff
;
2976 newf
->fr_offset
= (((offsetT
) 1 << fragP
->fr_offset
)
2978 if (newf
->fr_offset
* newf
->fr_var
2979 != (offsetT
) 1 << fragP
->fr_offset
)
2981 newf
->fr_offset
= (offsetT
) 1 << fragP
->fr_offset
;
2984 /* Include size of new frag in GROWTH. */
2985 growth
+= newf
->fr_offset
* newf
->fr_var
;
2986 /* Adjust the new frag address for the amount
2987 we'll add when we process the new frag. */
2988 newf
->fr_address
-= stretch
+ growth
;
2989 newf
->relax_marker
^= 1;
2990 fragP
->fr_next
= newf
;
2992 as_warn (_("padding added"));
3000 offsetT target
= offset
;
3005 /* Convert from an actual address to an octet offset
3006 into the section. Here it is assumed that the
3007 section's VMA is zero, and can omit subtracting it
3008 from the symbol's value to get the address offset. */
3009 know (S_GET_SEGMENT (symbolP
)->vma
== 0);
3010 target
+= S_GET_VALUE (symbolP
) * OCTETS_PER_BYTE
;
3013 know (fragP
->fr_next
);
3014 after
= fragP
->fr_next
->fr_address
+ stretch
;
3015 growth
= target
- after
;
3017 /* Growth may be negative, but variable part of frag
3018 cannot have fewer than 0 chars. That is, we can't
3020 if ((offsetT
) (address
+ fragP
->fr_fix
) > target
)
3024 /* Don't error on first few frag relax passes.
3025 The symbol might be an expression involving
3026 symbol values from other sections. If those
3027 sections have not yet been processed their
3028 frags will all have zero addresses, so we
3029 will calculate incorrect values for them. The
3030 number of passes we allow before giving an
3031 error is somewhat arbitrary. It should be at
3032 least one, with larger values requiring
3033 increasingly contrived dependencies between
3034 frags to trigger a false error. */
3037 /* Force another pass. */
3042 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
3043 _("attempt to move .org backwards"));
3045 /* We've issued an error message. Change the
3046 frag to avoid cascading errors. */
3047 fragP
->fr_type
= rs_align
;
3048 fragP
->fr_subtype
= 0;
3049 fragP
->fr_offset
= 0;
3050 fragP
->fr_fix
= after
- address
;
3062 amount
= S_GET_VALUE (symbolP
);
3063 if (S_GET_SEGMENT (symbolP
) != absolute_section
3064 || S_IS_COMMON (symbolP
)
3065 || ! S_IS_DEFINED (symbolP
))
3067 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
3068 _(".space, .nops or .fill specifies non-absolute value"));
3069 /* Prevent repeat of this error message. */
3070 fragP
->fr_symbol
= 0;
3072 else if (amount
< 0)
3074 /* Don't error on first few frag relax passes.
3075 See rs_org comment for a longer explanation. */
3082 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
3083 _(".space, .nops or .fill with negative value, ignored"));
3084 fragP
->fr_symbol
= 0;
3087 growth
= (was_address
+ fragP
->fr_fix
+ amount
3088 - fragP
->fr_next
->fr_address
);
3092 case rs_machine_dependent
:
3093 #ifdef md_relax_frag
3094 growth
= md_relax_frag (segment
, fragP
, stretch
);
3096 #ifdef TC_GENERIC_RELAX_TABLE
3097 /* The default way to relax a frag is to look through
3098 TC_GENERIC_RELAX_TABLE. */
3099 growth
= md_generic_table_relax_frag (segment
, fragP
,
3101 #endif /* TC_GENERIC_RELAX_TABLE */
3110 value
= resolve_symbol_value (fragP
->fr_symbol
);
3111 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
3112 growth
= size
- fragP
->fr_offset
;
3113 fragP
->fr_offset
= size
;
3118 growth
= eh_frame_relax_frag (fragP
);
3122 growth
= dwarf2dbg_relax_frag (fragP
);
3126 growth
= sframe_relax_frag (fragP
);
3130 BAD_CASE (fragP
->fr_type
);
3137 if (fragP
->fr_type
== rs_leb128
)
3138 rs_leb128_fudge
+= 16;
3139 else if (fragP
->fr_type
== rs_align
3140 && (rs_leb128_fudge
& 16) != 0
3142 rs_leb128_fudge
+= 16;
3144 rs_leb128_fudge
= 0;
3149 && (rs_leb128_fudge
& 16) == 0
3150 && (rs_leb128_fudge
& -16) != 0)
3151 rs_leb128_fudge
+= 1;
3153 rs_leb128_fudge
= 0;
3155 /* Until nothing further to relax. */
3156 while (stretched
&& -- max_iterations
);
3159 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
3160 segment_name (segment
));
3163 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
3164 if (fragP
->last_fr_address
!= fragP
->fr_address
)
3166 fragP
->last_fr_address
= fragP
->fr_address
;
3173 number_to_chars_bigendian (char *buf
, valueT val
, int n
)
3179 buf
[n
] = val
& 0xff;
3185 number_to_chars_littleendian (char *buf
, valueT val
, int n
)
3191 *buf
++ = val
& 0xff;
3197 write_print_statistics (FILE *file
)
3199 fprintf (file
, "fixups: %d\n", n_fixups
);
3202 /* For debugging. */
3203 extern int indent_level
;
3206 print_fixup (fixS
*fixp
)
3209 fprintf (stderr
, "fix %p %s:%d", fixp
, fixp
->fx_file
, fixp
->fx_line
);
3211 fprintf (stderr
, " pcrel");
3212 if (fixp
->fx_pcrel_adjust
)
3213 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
3215 fprintf (stderr
, " tcbit");
3217 fprintf (stderr
, " done");
3218 fprintf (stderr
, "\n size=%d frag=%p", fixp
->fx_size
, fixp
->fx_frag
);
3219 fprintf (stderr
, " where=%ld offset=%" PRIx64
" addnumber=%" PRIx64
,
3220 fixp
->fx_where
, (uint64_t) fixp
->fx_offset
,
3221 (uint64_t) fixp
->fx_addnumber
);
3222 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
3226 fprintf (stderr
, "\n +<");
3227 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
3228 fprintf (stderr
, ">");
3232 fprintf (stderr
, "\n -<");
3233 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
3234 fprintf (stderr
, ">");
3236 fprintf (stderr
, "\n");
3237 #ifdef TC_FIX_DATA_PRINT
3238 TC_FIX_DATA_PRINT (stderr
, fixp
);