1 /* write.c - emit .o file
2 Copyright (C) 1986-2021 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
);
490 case rs_machine_dependent
:
491 md_convert_frag (stdoutput
, sec
, fragP
);
493 gas_assert (fragP
->fr_next
== NULL
494 || (fragP
->fr_next
->fr_address
- fragP
->fr_address
497 /* After md_convert_frag, we make the frag into a ".space 0".
498 md_convert_frag() should set up any fixSs and constants
503 #ifndef WORKING_DOT_WORD
506 struct broken_word
*lie
;
508 if (fragP
->fr_subtype
)
510 fragP
->fr_fix
+= md_short_jump_size
;
511 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
512 lie
&& lie
->dispfrag
== fragP
;
513 lie
= lie
->next_broken_word
)
515 fragP
->fr_fix
+= md_long_jump_size
;
523 BAD_CASE (fragP
->fr_type
);
527 md_frag_check (fragP
);
531 struct relax_seg_info
538 relax_seg (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *xxx
)
540 segment_info_type
*seginfo
= seg_info (sec
);
541 struct relax_seg_info
*info
= (struct relax_seg_info
*) xxx
;
543 if (seginfo
&& seginfo
->frchainP
544 && relax_segment (seginfo
->frchainP
->frch_root
, sec
, info
->pass
))
549 size_seg (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
553 segment_info_type
*seginfo
;
555 valueT size
, newsize
;
557 subseg_change (sec
, 0);
559 seginfo
= seg_info (sec
);
560 if (seginfo
&& seginfo
->frchainP
)
562 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
563 cvt_frag_to_fill (sec
, fragp
);
564 for (fragp
= seginfo
->frchainP
->frch_root
;
566 fragp
= fragp
->fr_next
)
567 /* Walk to last elt. */
569 size
= fragp
->fr_address
+ fragp
->fr_fix
;
574 flags
= bfd_section_flags (sec
);
575 if (size
== 0 && bfd_section_size (sec
) != 0 &&
576 (flags
& SEC_HAS_CONTENTS
) != 0)
579 if (size
> 0 && ! seginfo
->bss
)
580 flags
|= SEC_HAS_CONTENTS
;
583 x
= bfd_set_section_flags (sec
, flags
);
586 /* If permitted, allow the backend to pad out the section
587 to some alignment boundary. */
588 if (do_not_pad_sections_to_alignment
)
591 newsize
= md_section_align (sec
, size
);
592 x
= bfd_set_section_size (sec
, newsize
);
595 /* If the size had to be rounded up, add some padding in the last
597 gas_assert (newsize
>= size
);
600 fragS
*last
= seginfo
->frchainP
->frch_last
;
601 fragp
= seginfo
->frchainP
->frch_root
;
602 while (fragp
->fr_next
!= last
)
603 fragp
= fragp
->fr_next
;
604 last
->fr_address
= size
;
605 if ((newsize
- size
) % fragp
->fr_var
== 0)
606 fragp
->fr_offset
+= (newsize
- size
) / fragp
->fr_var
;
608 /* If we hit this abort, it's likely due to subsegs_finish not
609 providing sufficient alignment on the last frag, and the
610 machine dependent code using alignment frags with fr_var
615 #ifdef tc_frob_section
616 tc_frob_section (sec
);
618 #ifdef obj_frob_section
619 obj_frob_section (sec
);
625 dump_section_relocs (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, FILE *stream
)
627 segment_info_type
*seginfo
= seg_info (sec
);
628 fixS
*fixp
= seginfo
->fix_root
;
633 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
636 symbolS
*s
= fixp
->fx_addsy
;
638 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
639 (int) fixp
->fx_r_type
);
641 fprintf (stream
, "no sym\n");
644 print_symbol_value_1 (stream
, s
);
645 fprintf (stream
, "\n");
647 fixp
= fixp
->fx_next
;
651 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
654 #ifndef EMIT_SECTION_SYMBOLS
655 #define EMIT_SECTION_SYMBOLS 1
658 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
659 and check for validity. Convert RELOC_LIST from using U.A fields
662 resolve_reloc_expr_symbols (void)
664 bfd_vma addr_mask
= 1;
665 struct reloc_list
*r
;
667 /* Avoid a shift by the width of type. */
668 addr_mask
<<= bfd_arch_bits_per_address (stdoutput
) - 1;
672 for (r
= reloc_list
; r
; r
= r
->next
)
674 reloc_howto_type
*howto
= r
->u
.a
.howto
;
677 bfd_vma offset
, addend
;
680 resolve_symbol_value (r
->u
.a
.offset_sym
);
681 symval
= symbol_get_value_expression (r
->u
.a
.offset_sym
);
685 if (symval
->X_op
== O_constant
)
686 sym
= r
->u
.a
.offset_sym
;
687 else if (symval
->X_op
== O_symbol
)
689 sym
= symval
->X_add_symbol
;
690 offset
= symval
->X_add_number
;
691 symval
= symbol_get_value_expression (symval
->X_add_symbol
);
694 || symval
->X_op
!= O_constant
695 || (sec
= S_GET_SEGMENT (sym
)) == NULL
696 || !SEG_NORMAL (sec
))
698 as_bad_where (r
->file
, r
->line
, _("invalid offset expression"));
702 offset
+= S_GET_VALUE (sym
);
705 addend
= r
->u
.a
.addend
;
706 if (r
->u
.a
.sym
!= NULL
)
708 resolve_symbol_value (r
->u
.a
.sym
);
709 symval
= symbol_get_value_expression (r
->u
.a
.sym
);
710 if (symval
->X_op
== O_constant
)
712 else if (symval
->X_op
== O_symbol
)
714 sym
= symval
->X_add_symbol
;
715 addend
+= symval
->X_add_number
;
716 symval
= symbol_get_value_expression (symval
->X_add_symbol
);
718 if (symval
->X_op
!= O_constant
)
720 as_bad_where (r
->file
, r
->line
, _("invalid reloc expression"));
723 else if (sym
!= NULL
&& sec
!= NULL
)
725 /* Convert relocs against local symbols to refer to the
726 corresponding section symbol plus offset instead. Keep
727 PC-relative relocs of the REL variety intact though to
728 prevent the offset from overflowing the relocated field,
729 unless it has enough bits to cover the whole address
732 && S_IS_DEFINED (sym
)
733 && !symbol_section_p (sym
)
735 || (howto
->partial_inplace
736 && (!howto
->pc_relative
737 || howto
->src_mask
== addr_mask
))))
739 asection
*symsec
= S_GET_SEGMENT (sym
);
740 if (!(((symsec
->flags
& SEC_MERGE
) != 0
742 || (symsec
->flags
& SEC_THREAD_LOCAL
) != 0))
744 addend
+= S_GET_VALUE (sym
);
745 sym
= section_symbol (symsec
);
748 symbol_mark_used_in_reloc (sym
);
753 if (abs_section_sym
== NULL
)
754 abs_section_sym
= section_symbol (absolute_section
);
755 sym
= abs_section_sym
;
759 r
->u
.b
.s
= symbol_get_bfdsym (sym
);
760 r
->u
.b
.r
.sym_ptr_ptr
= &r
->u
.b
.s
;
761 r
->u
.b
.r
.address
= offset
;
762 r
->u
.b
.r
.addend
= addend
;
763 r
->u
.b
.r
.howto
= howto
;
767 /* This pass over fixups decides whether symbols can be replaced with
771 adjust_reloc_syms (bfd
*abfd ATTRIBUTE_UNUSED
,
773 void *xxx ATTRIBUTE_UNUSED
)
775 segment_info_type
*seginfo
= seg_info (sec
);
781 dump_section_relocs (abfd
, sec
, stderr
);
783 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
787 else if (fixp
->fx_addsy
)
793 fprintf (stderr
, "\n\nadjusting fixup:\n");
797 sym
= fixp
->fx_addsy
;
799 /* All symbols should have already been resolved at this
800 point. It is possible to see unresolved expression
801 symbols, though, since they are not in the regular symbol
803 resolve_symbol_value (sym
);
805 if (fixp
->fx_subsy
!= NULL
)
806 resolve_symbol_value (fixp
->fx_subsy
);
808 /* If this symbol is equated to an undefined or common symbol,
809 convert the fixup to being against that symbol. */
810 while (symbol_equated_reloc_p (sym
)
811 || S_IS_WEAKREFR (sym
))
813 symbolS
*newsym
= symbol_get_value_expression (sym
)->X_add_symbol
;
816 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
817 fixp
->fx_addsy
= newsym
;
821 if (symbol_mri_common_p (sym
))
823 fixp
->fx_offset
+= S_GET_VALUE (sym
);
824 fixp
->fx_addsy
= symbol_get_value_expression (sym
)->X_add_symbol
;
828 /* If the symbol is undefined, common, weak, or global (ELF
829 shared libs), we can't replace it with the section symbol. */
830 if (S_FORCE_RELOC (fixp
->fx_addsy
, 1))
833 /* Is there some other (target cpu dependent) reason we can't adjust
834 this one? (E.g. relocations involving function addresses on
836 #ifdef tc_fix_adjustable
837 if (! tc_fix_adjustable (fixp
))
841 /* Since we're reducing to section symbols, don't attempt to reduce
842 anything that's already using one. */
843 if (symbol_section_p (sym
))
845 /* Mark the section symbol used in relocation so that it will
846 be included in the symbol table. */
847 symbol_mark_used_in_reloc (sym
);
851 symsec
= S_GET_SEGMENT (sym
);
855 if (bfd_is_abs_section (symsec
)
856 || symsec
== reg_section
)
858 /* The fixup_segment routine normally will not use this
859 symbol in a relocation. */
863 /* Don't try to reduce relocs which refer to non-local symbols
864 in .linkonce sections. It can lead to confusion when a
865 debugging section refers to a .linkonce section. I hope
866 this will always be correct. */
867 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
869 if ((symsec
->flags
& SEC_LINK_ONCE
) != 0
871 /* The GNU toolchain uses an extension for ELF: a
872 section beginning with the magic string
873 .gnu.linkonce is a linkonce section. */
874 && startswith (segment_name (symsec
), ".gnu.linkonce")))
878 /* Never adjust a reloc against local symbol in a merge section
879 with non-zero addend. */
880 if ((symsec
->flags
& SEC_MERGE
) != 0
881 && (fixp
->fx_offset
!= 0 || fixp
->fx_subsy
!= NULL
))
884 /* Never adjust a reloc against TLS local symbol. */
885 if ((symsec
->flags
& SEC_THREAD_LOCAL
) != 0)
888 /* We refetch the segment when calling section_symbol, rather
889 than using symsec, because S_GET_VALUE may wind up changing
890 the section when it calls resolve_symbol_value. */
891 fixp
->fx_offset
+= S_GET_VALUE (sym
);
892 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
894 fprintf (stderr
, "\nadjusted fixup:\n");
899 dump_section_relocs (abfd
, sec
, stderr
);
903 as_bad_subtract (fixS
*fixp
)
905 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
906 _("can't resolve %s - %s"),
907 fixp
->fx_addsy
? S_GET_NAME (fixp
->fx_addsy
) : "0",
908 S_GET_NAME (fixp
->fx_subsy
));
913 Go through all the fixS's in a segment and see which ones can be
914 handled now. (These consist of fixS where we have since discovered
915 the value of a symbol, or the address of the frag involved.)
916 For each one, call md_apply_fix to put the fix into the frag data.
917 Ones that we couldn't completely handle here will be output later
918 by emit_relocations. */
921 fixup_segment (fixS
*fixP
, segT this_segment
)
926 if (fixP
!= NULL
&& abs_section_sym
== NULL
)
927 abs_section_sym
= section_symbol (absolute_section
);
929 /* If the linker is doing the relaxing, we must not do any fixups.
931 Well, strictly speaking that's not true -- we could do any that
932 are PC-relative and don't cross regions that could change size. */
933 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment
))
935 for (; fixP
; fixP
= fixP
->fx_next
)
938 if (fixP
->fx_addsy
== NULL
)
940 /* There was no symbol required by this relocation.
941 However, BFD doesn't really handle relocations
942 without symbols well. So fake up a local symbol in
943 the absolute section. */
944 fixP
->fx_addsy
= abs_section_sym
;
946 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
947 if (fixP
->fx_subsy
!= NULL
)
948 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
953 for (; fixP
; fixP
= fixP
->fx_next
)
955 segT add_symbol_segment
= absolute_section
;
958 fprintf (stderr
, "\nprocessing fixup:\n");
962 fragP
= fixP
->fx_frag
;
964 #ifdef TC_VALIDATE_FIX
965 TC_VALIDATE_FIX (fixP
, this_segment
, skip
);
967 add_number
= fixP
->fx_offset
;
969 if (fixP
->fx_addsy
!= NULL
)
970 add_symbol_segment
= S_GET_SEGMENT (fixP
->fx_addsy
);
972 if (fixP
->fx_subsy
!= NULL
)
974 segT sub_symbol_segment
;
975 resolve_symbol_value (fixP
->fx_subsy
);
976 sub_symbol_segment
= S_GET_SEGMENT (fixP
->fx_subsy
);
977 if (fixP
->fx_addsy
!= NULL
978 && sub_symbol_segment
== add_symbol_segment
979 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
980 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
981 && !TC_FORCE_RELOCATION_SUB_SAME (fixP
, add_symbol_segment
))
983 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
984 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
985 fixP
->fx_offset
= add_number
;
986 fixP
->fx_addsy
= NULL
;
987 fixP
->fx_subsy
= NULL
;
989 /* See the comment below about 68k weirdness. */
993 else if (sub_symbol_segment
== absolute_section
994 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
995 && !TC_FORCE_RELOCATION_SUB_ABS (fixP
, add_symbol_segment
))
997 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
998 fixP
->fx_offset
= add_number
;
999 fixP
->fx_subsy
= NULL
;
1001 else if (sub_symbol_segment
== this_segment
1002 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
1003 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP
, add_symbol_segment
))
1005 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
1006 fixP
->fx_offset
= (add_number
+ fixP
->fx_dot_value
1007 + fixP
->fx_dot_frag
->fr_address
);
1009 /* Make it pc-relative. If the back-end code has not
1010 selected a pc-relative reloc, cancel the adjustment
1011 we do later on all pc-relative relocs. */
1014 /* Do this for m68k even if it's already described
1015 as pc-relative. On the m68k, an operand of
1016 "pc@(foo-.-2)" should address "foo" in a
1017 pc-relative mode. */
1021 add_number
+= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
1022 fixP
->fx_subsy
= NULL
;
1025 else if (!TC_VALIDATE_FIX_SUB (fixP
, add_symbol_segment
))
1027 if (!md_register_arithmetic
1028 && (add_symbol_segment
== reg_section
1029 || sub_symbol_segment
== reg_section
))
1030 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1031 _("register value used as expression"));
1033 as_bad_subtract (fixP
);
1035 else if (sub_symbol_segment
!= undefined_section
1036 && ! bfd_is_com_section (sub_symbol_segment
)
1037 && MD_APPLY_SYM_VALUE (fixP
))
1038 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
1043 if (add_symbol_segment
== this_segment
1044 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
1045 && !TC_FORCE_RELOCATION_LOCAL (fixP
))
1047 /* This fixup was made when the symbol's segment was
1048 SEG_UNKNOWN, but it is now in the local segment.
1049 So we know how to do the address without relocation. */
1050 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
1051 fixP
->fx_offset
= add_number
;
1053 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
1054 fixP
->fx_addsy
= NULL
;
1057 else if (add_symbol_segment
== absolute_section
1058 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
1059 && !TC_FORCE_RELOCATION_ABS (fixP
))
1061 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
1062 fixP
->fx_offset
= add_number
;
1063 fixP
->fx_addsy
= NULL
;
1065 else if (add_symbol_segment
!= undefined_section
1066 && ! bfd_is_com_section (add_symbol_segment
)
1067 && MD_APPLY_SYM_VALUE (fixP
))
1068 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
1073 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
1074 if (!fixP
->fx_done
&& fixP
->fx_addsy
== NULL
)
1076 /* There was no symbol required by this relocation.
1077 However, BFD doesn't really handle relocations
1078 without symbols well. So fake up a local symbol in
1079 the absolute section. */
1080 fixP
->fx_addsy
= abs_section_sym
;
1085 md_apply_fix (fixP
, &add_number
, this_segment
);
1089 if (fixP
->fx_addsy
== NULL
)
1090 fixP
->fx_addsy
= abs_section_sym
;
1091 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
1092 if (fixP
->fx_subsy
!= NULL
)
1093 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
1096 if (!fixP
->fx_no_overflow
&& fixP
->fx_size
!= 0)
1098 if (fixP
->fx_size
< sizeof (valueT
))
1103 mask
--; /* Set all bits to one. */
1104 mask
<<= fixP
->fx_size
* 8 - (fixP
->fx_signed
? 1 : 0);
1105 if ((add_number
& mask
) != 0
1107 ? (add_number
& mask
) != mask
1108 : (-add_number
& mask
) != 0))
1110 char buf
[50], buf2
[50];
1111 bfd_sprintf_vma (stdoutput
, buf
, fragP
->fr_address
+ fixP
->fx_where
);
1112 if (add_number
> 1000)
1113 bfd_sprintf_vma (stdoutput
, buf2
, add_number
);
1115 sprintf (buf2
, "%ld", (long) add_number
);
1116 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1117 ngettext ("value of %s too large for field "
1119 "value of %s too large for field "
1120 "of %d bytes at %s",
1122 buf2
, fixP
->fx_size
, buf
);
1123 } /* Generic error checking. */
1125 #ifdef WARN_SIGNED_OVERFLOW_WORD
1126 /* Warn if a .word value is too large when treated as a signed
1127 number. We already know it is not too negative. This is to
1128 catch over-large switches generated by gcc on the 68k. */
1129 if (!flag_signed_overflow_ok
1130 && fixP
->fx_size
== 2
1131 && add_number
> 0x7fff)
1132 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1133 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1135 (long) (fragP
->fr_address
+ fixP
->fx_where
));
1139 #ifdef TC_VALIDATE_FIX
1140 skip
: ATTRIBUTE_UNUSED_LABEL
1144 fprintf (stderr
, "result:\n");
1147 } /* For each fixS in this segment. */
1151 fix_segment (bfd
*abfd ATTRIBUTE_UNUSED
,
1153 void *xxx ATTRIBUTE_UNUSED
)
1155 segment_info_type
*seginfo
= seg_info (sec
);
1157 fixup_segment (seginfo
->fix_root
, sec
);
1161 install_reloc (asection
*sec
, arelent
*reloc
, fragS
*fragp
,
1162 const char *file
, unsigned int line
)
1165 bfd_reloc_status_type s
;
1168 if (reloc
->sym_ptr_ptr
!= NULL
1169 && (sym
= *reloc
->sym_ptr_ptr
) != NULL
1170 && (sym
->flags
& BSF_KEEP
) == 0
1171 && ((sym
->flags
& BSF_SECTION_SYM
) == 0
1172 || (EMIT_SECTION_SYMBOLS
1173 && !bfd_is_abs_section (sym
->section
))))
1174 as_bad_where (file
, line
, _("redefined symbol cannot be used on reloc"));
1176 s
= bfd_install_relocation (stdoutput
, reloc
,
1177 fragp
->fr_literal
, fragp
->fr_address
,
1183 case bfd_reloc_overflow
:
1184 as_bad_where (file
, line
, _("relocation overflow"));
1186 case bfd_reloc_outofrange
:
1187 as_bad_where (file
, line
, _("relocation out of range"));
1190 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1196 get_frag_for_reloc (fragS
*last_frag
,
1197 const segment_info_type
*seginfo
,
1198 const struct reloc_list
*r
)
1202 for (f
= last_frag
; f
!= NULL
; f
= f
->fr_next
)
1203 if (f
->fr_address
<= r
->u
.b
.r
.address
1204 && r
->u
.b
.r
.address
< f
->fr_address
+ f
->fr_fix
)
1207 for (f
= seginfo
->frchainP
->frch_root
; 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 as_bad_where (r
->file
, r
->line
,
1218 _("reloc not within (fixed part of) section"));
1223 write_relocs (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1224 void *xxx ATTRIBUTE_UNUSED
)
1226 segment_info_type
*seginfo
= seg_info (sec
);
1228 struct reloc_list
*my_reloc_list
, **rp
, *r
;
1233 /* If seginfo is NULL, we did not create this section; don't do
1234 anything with it. */
1235 if (seginfo
== NULL
)
1239 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
1243 #ifdef RELOC_EXPANSION_POSSIBLE
1244 n
*= MAX_RELOC_EXPANSION
;
1247 /* Extract relocs for this section from reloc_list. */
1250 my_reloc_list
= NULL
;
1251 while ((r
= *rp
) != NULL
)
1253 if (r
->u
.b
.sec
== sec
)
1256 r
->next
= my_reloc_list
;
1264 relocs
= XCNEWVEC (arelent
*, n
);
1269 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1274 #ifndef RELOC_EXPANSION_POSSIBLE
1283 fx_size
= fixp
->fx_size
;
1284 slack
= TC_FX_SIZE_SLACK (fixp
);
1286 fx_size
= fx_size
> slack
? fx_size
- slack
: 0;
1287 loc
= fixp
->fx_where
+ fx_size
;
1288 if (slack
>= 0 && loc
> fixp
->fx_frag
->fr_fix
)
1289 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1290 _("internal error: fixup not contained within frag"));
1292 #ifdef obj_fixup_removed_symbol
1293 if (fixp
->fx_addsy
&& symbol_removed_p (fixp
->fx_addsy
))
1294 obj_fixup_removed_symbol (&fixp
->fx_addsy
);
1295 if (fixp
->fx_subsy
&& symbol_removed_p (fixp
->fx_subsy
))
1296 obj_fixup_removed_symbol (&fixp
->fx_subsy
);
1299 #ifndef RELOC_EXPANSION_POSSIBLE
1300 *reloc
= tc_gen_reloc (sec
, fixp
);
1302 reloc
= tc_gen_reloc (sec
, fixp
);
1307 while (r
!= NULL
&& r
->u
.b
.r
.address
< (*reloc
)->address
)
1309 fragS
*f
= get_frag_for_reloc (last_frag
, seginfo
, r
);
1313 relocs
[n
++] = &r
->u
.b
.r
;
1314 install_reloc (sec
, &r
->u
.b
.r
, f
, r
->file
, r
->line
);
1318 relocs
[n
++] = *reloc
;
1319 install_reloc (sec
, *reloc
, fixp
->fx_frag
,
1320 fixp
->fx_file
, fixp
->fx_line
);
1321 #ifndef RELOC_EXPANSION_POSSIBLE
1331 fragS
*f
= get_frag_for_reloc (last_frag
, seginfo
, r
);
1335 relocs
[n
++] = &r
->u
.b
.r
;
1336 install_reloc (sec
, &r
->u
.b
.r
, f
, r
->file
, r
->line
);
1343 unsigned int k
, j
, nsyms
;
1345 sympp
= bfd_get_outsymbols (stdoutput
);
1346 nsyms
= bfd_get_symcount (stdoutput
);
1347 for (k
= 0; k
< n
; k
++)
1348 if (((*relocs
[k
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1350 for (j
= 0; j
< nsyms
; j
++)
1351 if (sympp
[j
] == *relocs
[k
]->sym_ptr_ptr
)
1361 flagword flags
= bfd_section_flags (sec
);
1363 bfd_set_section_flags (sec
, flags
);
1364 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1367 #ifdef SET_SECTION_RELOCS
1368 SET_SECTION_RELOCS (sec
, relocs
, n
);
1375 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1376 for (k
= 0; k
< n
; k
++)
1378 arelent
*rel
= relocs
[k
];
1379 asymbol
*s
= *rel
->sym_ptr_ptr
;
1380 fprintf (stderr
, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1381 k
, rel
, (unsigned long)rel
->address
, s
->name
,
1382 (unsigned long)rel
->addend
);
1389 compress_frag (struct z_stream_s
*strm
, const char *contents
, int in_size
,
1390 fragS
**last_newf
, struct obstack
*ob
)
1393 int total_out_size
= 0;
1394 fragS
*f
= *last_newf
;
1398 /* Call the compression routine repeatedly until it has finished
1399 processing the frag. */
1402 /* Reserve all the space available in the current chunk.
1403 If none is available, start a new frag. */
1404 avail_out
= obstack_room (ob
);
1407 obstack_finish (ob
);
1408 f
= frag_alloc (ob
);
1409 f
->fr_type
= rs_fill
;
1410 (*last_newf
)->fr_next
= f
;
1412 avail_out
= obstack_room (ob
);
1415 as_fatal (_("can't extend frag"));
1416 next_out
= obstack_next_free (ob
);
1417 obstack_blank_fast (ob
, avail_out
);
1418 out_size
= compress_data (strm
, &contents
, &in_size
,
1419 &next_out
, &avail_out
);
1423 f
->fr_fix
+= out_size
;
1424 total_out_size
+= out_size
;
1426 /* Return unused space. */
1428 obstack_blank_fast (ob
, -avail_out
);
1431 return total_out_size
;
1435 compress_debug (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
1437 segment_info_type
*seginfo
= seg_info (sec
);
1441 struct obstack
*ob
= &seginfo
->frchainP
->frch_obstack
;
1442 bfd_size_type uncompressed_size
= (bfd_size_type
) sec
->size
;
1443 bfd_size_type compressed_size
;
1444 const char *section_name
;
1445 char *compressed_name
;
1447 struct z_stream_s
*strm
;
1449 flagword flags
= bfd_section_flags (sec
);
1450 unsigned int header_size
, compression_header_size
;
1454 || (flags
& (SEC_ALLOC
| SEC_HAS_CONTENTS
)) == SEC_ALLOC
)
1457 section_name
= bfd_section_name (sec
);
1458 if (!startswith (section_name
, ".debug_"))
1461 strm
= compress_init ();
1465 if (flag_compress_debug
== COMPRESS_DEBUG_GABI_ZLIB
)
1467 compression_header_size
1468 = bfd_get_compression_header_size (stdoutput
, NULL
);
1469 header_size
= compression_header_size
;
1473 compression_header_size
= 0;
1477 /* Create a new frag to contain the compression header. */
1478 first_newf
= frag_alloc (ob
);
1479 if (obstack_room (ob
) < header_size
)
1480 first_newf
= frag_alloc (ob
);
1481 if (obstack_room (ob
) < header_size
)
1482 as_fatal (ngettext ("can't extend frag %lu char",
1483 "can't extend frag %lu chars",
1484 (unsigned long) header_size
),
1485 (unsigned long) header_size
);
1486 last_newf
= first_newf
;
1487 obstack_blank_fast (ob
, header_size
);
1488 last_newf
->fr_type
= rs_fill
;
1489 last_newf
->fr_fix
= header_size
;
1490 header
= last_newf
->fr_literal
;
1491 compressed_size
= header_size
;
1493 /* Stream the frags through the compression engine, adding new frags
1494 as necessary to accommodate the compressed output. */
1495 for (f
= seginfo
->frchainP
->frch_root
;
1504 gas_assert (f
->fr_type
== rs_fill
);
1507 out_size
= compress_frag (strm
, f
->fr_literal
, f
->fr_fix
,
1511 compressed_size
+= out_size
;
1513 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1514 fill_size
= f
->fr_var
;
1515 count
= f
->fr_offset
;
1516 gas_assert (count
>= 0);
1517 if (fill_size
&& count
)
1521 out_size
= compress_frag (strm
, fill_literal
, (int) fill_size
,
1525 compressed_size
+= out_size
;
1530 /* Flush the compression state. */
1537 /* Reserve all the space available in the current chunk.
1538 If none is available, start a new frag. */
1539 avail_out
= obstack_room (ob
);
1544 obstack_finish (ob
);
1545 newf
= frag_alloc (ob
);
1546 newf
->fr_type
= rs_fill
;
1547 last_newf
->fr_next
= newf
;
1549 avail_out
= obstack_room (ob
);
1552 as_fatal (_("can't extend frag"));
1553 next_out
= obstack_next_free (ob
);
1554 obstack_blank_fast (ob
, avail_out
);
1555 x
= compress_finish (strm
, &next_out
, &avail_out
, &out_size
);
1559 last_newf
->fr_fix
+= out_size
;
1560 compressed_size
+= out_size
;
1562 /* Return unused space. */
1564 obstack_blank_fast (ob
, -avail_out
);
1570 /* PR binutils/18087: If compression didn't make the section smaller,
1571 just keep it uncompressed. */
1572 if (compressed_size
>= uncompressed_size
)
1575 /* Replace the uncompressed frag list with the compressed frag list. */
1576 seginfo
->frchainP
->frch_root
= first_newf
;
1577 seginfo
->frchainP
->frch_last
= last_newf
;
1579 /* Update the section size and its name. */
1580 bfd_update_compression_header (abfd
, (bfd_byte
*) header
, sec
);
1581 x
= bfd_set_section_size (sec
, compressed_size
);
1583 if (!compression_header_size
)
1585 compressed_name
= concat (".z", section_name
+ 1, (char *) NULL
);
1586 bfd_rename_section (sec
, compressed_name
);
1590 #ifndef md_generate_nops
1591 /* Genenerate COUNT bytes of no-op instructions to WHERE. A target
1592 backend must override this with proper no-op instructions. */
1595 md_generate_nops (fragS
*f ATTRIBUTE_UNUSED
,
1596 char *where ATTRIBUTE_UNUSED
,
1597 offsetT count ATTRIBUTE_UNUSED
,
1598 int control ATTRIBUTE_UNUSED
)
1600 as_bad (_("unimplemented .nops directive"));
1605 write_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
1607 void *xxx ATTRIBUTE_UNUSED
)
1609 segment_info_type
*seginfo
= seg_info (sec
);
1610 addressT offset
= 0;
1613 /* Write out the frags. */
1615 || !(bfd_section_flags (sec
) & SEC_HAS_CONTENTS
))
1618 for (f
= seginfo
->frchainP
->frch_root
;
1627 gas_assert (f
->fr_type
== rs_fill
|| f
->fr_type
== rs_fill_nop
);
1630 x
= bfd_set_section_contents (stdoutput
, sec
,
1631 f
->fr_literal
, (file_ptr
) offset
,
1632 (bfd_size_type
) f
->fr_fix
);
1634 as_fatal (ngettext ("can't write %ld byte "
1635 "to section %s of %s: '%s'",
1636 "can't write %ld bytes "
1637 "to section %s of %s: '%s'",
1640 bfd_section_name (sec
), bfd_get_filename (stdoutput
),
1641 bfd_errmsg (bfd_get_error ()));
1642 offset
+= f
->fr_fix
;
1645 fill_size
= f
->fr_var
;
1646 count
= f
->fr_offset
;
1647 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1649 if (f
->fr_type
== rs_fill_nop
)
1651 gas_assert (count
>= 0 && fill_size
== 1);
1654 char *buf
= xmalloc (count
);
1655 md_generate_nops (f
, buf
, count
, *fill_literal
);
1656 x
= bfd_set_section_contents
1657 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1658 (bfd_size_type
) count
);
1660 as_fatal (ngettext ("can't fill %ld byte "
1661 "in section %s of %s: '%s'",
1662 "can't fill %ld bytes "
1663 "in section %s of %s: '%s'",
1666 bfd_section_name (sec
),
1667 bfd_get_filename (stdoutput
),
1668 bfd_errmsg (bfd_get_error ()));
1675 gas_assert (count
>= 0);
1676 if (fill_size
&& count
)
1679 if (fill_size
> sizeof (buf
))
1681 /* Do it the old way. Can this ever happen? */
1684 x
= bfd_set_section_contents (stdoutput
, sec
,
1687 (bfd_size_type
) fill_size
);
1689 as_fatal (ngettext ("can't fill %ld byte "
1690 "in section %s of %s: '%s'",
1691 "can't fill %ld bytes "
1692 "in section %s of %s: '%s'",
1695 bfd_section_name (sec
),
1696 bfd_get_filename (stdoutput
),
1697 bfd_errmsg (bfd_get_error ()));
1698 offset
+= fill_size
;
1703 /* Build a buffer full of fill objects and output it as
1704 often as necessary. This saves on the overhead of
1705 potentially lots of bfd_set_section_contents calls. */
1709 n_per_buf
= sizeof (buf
);
1710 memset (buf
, *fill_literal
, n_per_buf
);
1715 n_per_buf
= sizeof (buf
) / fill_size
;
1716 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1717 memcpy (bufp
, fill_literal
, fill_size
);
1719 for (; count
> 0; count
-= n_per_buf
)
1721 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1722 x
= bfd_set_section_contents
1723 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1724 (bfd_size_type
) n_per_buf
* fill_size
);
1726 as_fatal (ngettext ("can't fill %ld byte "
1727 "in section %s of %s: '%s'",
1728 "can't fill %ld bytes "
1729 "in section %s of %s: '%s'",
1730 (long) (n_per_buf
* fill_size
)),
1731 (long) (n_per_buf
* fill_size
),
1732 bfd_section_name (sec
),
1733 bfd_get_filename (stdoutput
),
1734 bfd_errmsg (bfd_get_error ()));
1735 offset
+= n_per_buf
* fill_size
;
1743 merge_data_into_text (void)
1745 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1746 seg_info (data_section
)->frchainP
->frch_root
;
1747 seg_info (text_section
)->frchainP
->frch_last
=
1748 seg_info (data_section
)->frchainP
->frch_last
;
1749 seg_info (data_section
)->frchainP
= 0;
1760 /* Count symbols. We can't rely on a count made by the loop in
1761 write_object_file, because *_frob_file may add a new symbol or
1762 two. Generate unused section symbols only if needed. */
1764 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1765 if (!symbol_removed_p (symp
)
1766 && (bfd_keep_unused_section_symbols (stdoutput
)
1767 || !symbol_section_p (symp
)
1768 || symbol_used_in_reloc_p (symp
)))
1774 bfd_size_type amt
= (bfd_size_type
) nsyms
* sizeof (asymbol
*);
1776 asympp
= (asymbol
**) bfd_alloc (stdoutput
, amt
);
1777 symp
= symbol_rootP
;
1778 for (i
= 0; i
< nsyms
; 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
)))
1784 asympp
[i
] = symbol_get_bfdsym (symp
);
1785 if (asympp
[i
]->flags
!= BSF_SECTION_SYM
1786 || !(bfd_is_const_section (asympp
[i
]->section
)
1787 && asympp
[i
]->section
->symbol
== asympp
[i
]))
1788 asympp
[i
]->flags
|= BSF_KEEP
;
1789 symbol_mark_written (symp
);
1790 /* Include this section symbol in the symbol table. */
1791 if (symbol_section_p (symp
))
1792 asympp
[i
]->flags
|= BSF_SECTION_SYM_USED
;
1798 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1799 gas_assert (result
);
1800 symbol_table_frozen
= 1;
1803 /* Finish the subsegments. After every sub-segment, we fake an
1804 ".align ...". This conforms to BSD4.2 brain-damage. We then fake
1805 ".fill 0" because that is the kind of frag that requires least
1806 thought. ".align" frags like to have a following frag since that
1807 makes calculating their intended length trivial. */
1809 #ifndef SUB_SEGMENT_ALIGN
1811 /* The last subsegment gets an alignment corresponding to the alignment
1812 of the section. This allows proper nop-filling at the end of
1813 code-bearing sections. */
1814 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1815 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
1816 && !do_not_pad_sections_to_alignment \
1817 ? get_recorded_alignment (SEG) \
1820 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1825 subsegs_finish_section (asection
*s
)
1827 struct frchain
*frchainP
;
1828 segment_info_type
*seginfo
= seg_info (s
);
1832 for (frchainP
= seginfo
->frchainP
;
1834 frchainP
= frchainP
->frch_next
)
1838 subseg_set (s
, frchainP
->frch_subseg
);
1840 /* This now gets called even if we had errors. In that case,
1841 any alignment is meaningless, and, moreover, will look weird
1842 if we are generating a listing. */
1844 do_not_pad_sections_to_alignment
= 1;
1846 alignment
= SUB_SEGMENT_ALIGN (now_seg
, frchainP
);
1847 if ((bfd_section_flags (now_seg
) & SEC_MERGE
)
1848 && now_seg
->entsize
)
1850 unsigned int entsize
= now_seg
->entsize
;
1853 while ((entsize
& 1) == 0)
1859 if (entalign
> alignment
)
1860 alignment
= entalign
;
1863 if (subseg_text_p (now_seg
))
1864 frag_align_code (alignment
, 0);
1866 frag_align (alignment
, 0, 0);
1868 /* frag_align will have left a new frag.
1869 Use this last frag for an empty ".fill".
1871 For this segment ...
1872 Create a last frag. Do not leave a "being filled in frag". */
1873 frag_wane (frag_now
);
1874 frag_now
->fr_fix
= 0;
1875 know (frag_now
->fr_next
== NULL
);
1880 subsegs_finish (void)
1884 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
1885 subsegs_finish_section (s
);
1890 create_obj_attrs_section (void)
1897 size
= bfd_elf_obj_attr_size (stdoutput
);
1901 name
= get_elf_backend_data (stdoutput
)->obj_attrs_section
;
1903 name
= ".gnu.attributes";
1904 s
= subseg_new (name
, 0);
1905 elf_section_type (s
)
1906 = get_elf_backend_data (stdoutput
)->obj_attrs_section_type
;
1907 bfd_set_section_flags (s
, SEC_READONLY
| SEC_DATA
);
1909 p
= frag_more (size
);
1910 bfd_elf_set_obj_attr_contents (stdoutput
, (bfd_byte
*)p
, size
);
1912 subsegs_finish_section (s
);
1913 relax_segment (seg_info (s
)->frchainP
->frch_root
, s
, 0);
1914 size_seg (stdoutput
, s
, NULL
);
1917 /* Create a relocation against an entry in a GNU Build attribute section. */
1920 create_note_reloc (segT sec
,
1922 bfd_size_type note_offset
,
1923 bfd_size_type desc2_offset
,
1929 struct reloc_list
* reloc
;
1931 reloc
= XNEW (struct reloc_list
);
1933 /* We create a .b type reloc as resolve_reloc_expr_symbols() has already been called. */
1934 reloc
->u
.b
.sec
= sec
;
1935 reloc
->u
.b
.s
= symbol_get_bfdsym (sym
);
1936 reloc
->u
.b
.r
.sym_ptr_ptr
= & reloc
->u
.b
.s
;
1937 reloc
->u
.b
.r
.address
= note_offset
+ desc2_offset
;
1938 reloc
->u
.b
.r
.addend
= addend
;
1939 reloc
->u
.b
.r
.howto
= bfd_reloc_type_lookup (stdoutput
, reloc_type
);
1941 if (reloc
->u
.b
.r
.howto
== NULL
)
1943 as_bad (_("unable to create reloc for build note"));
1947 reloc
->file
= N_("<gnu build note>");
1950 reloc
->next
= reloc_list
;
1953 /* For REL relocs, store the addend in the section. */
1954 if (! sec
->use_rela_p
1955 /* The SH target is a special case that uses RELA relocs
1956 but still stores the addend in the word being relocated. */
1957 || strstr (bfd_get_target (stdoutput
), "-sh") != NULL
)
1961 /* Zero out the addend, since it is now stored in the note. */
1962 reloc
->u
.b
.r
.addend
= 0;
1964 if (target_big_endian
)
1966 for (i
= desc2_size
; addend
!= 0 && i
> 0; addend
>>= 8, i
--)
1967 note
[desc2_offset
+ i
- 1] = (addend
& 0xff);
1971 for (i
= 0; addend
!= 0 && i
< desc2_size
; addend
>>= 8, i
++)
1972 note
[desc2_offset
+ i
] = (addend
& 0xff);
1978 maybe_generate_build_notes (void)
1985 offsetT desc2_offset
;
1990 if (! flag_generate_build_notes
1991 || bfd_get_section_by_name (stdoutput
,
1992 GNU_BUILD_ATTRS_SECTION_NAME
) != NULL
)
1995 /* Create a GNU Build Attribute section. */
1996 sec
= subseg_new (GNU_BUILD_ATTRS_SECTION_NAME
, false);
1997 elf_section_type (sec
) = SHT_NOTE
;
1998 bfd_set_section_flags (sec
, (SEC_READONLY
| SEC_HAS_CONTENTS
| SEC_DATA
2000 bfd_set_section_alignment (sec
, 2);
2002 /* Work out the size of the notes that we will create,
2003 and the relocation we should use. */
2004 if (bfd_arch_bits_per_address (stdoutput
) <= 32)
2007 desc_size
= 8; /* Two 4-byte offsets. */
2010 /* FIXME: The BFD backend for the CRX target does not support the
2011 BFD_RELOC_32, even though it really should. Likewise for the
2012 CR16 target. So we have special case code here... */
2013 if (strstr (bfd_get_target (stdoutput
), "-crx") != NULL
)
2014 desc_reloc
= BFD_RELOC_CRX_NUM32
;
2015 else if (strstr (bfd_get_target (stdoutput
), "-cr16") != NULL
)
2016 desc_reloc
= BFD_RELOC_CR16_NUM32
;
2018 desc_reloc
= BFD_RELOC_32
;
2023 desc_size
= 16; /* Two 8-byte offsets. */
2025 /* FIXME: The BFD backend for the IA64 target does not support the
2026 BFD_RELOC_64, even though it really should. The HPPA backend
2027 has a similar issue, although it does not support BFD_RELOCs at
2028 all! So we have special case code to handle these targets. */
2029 if (strstr (bfd_get_target (stdoutput
), "-ia64") != NULL
)
2030 desc_reloc
= target_big_endian
? BFD_RELOC_IA64_DIR32MSB
: BFD_RELOC_IA64_DIR32LSB
;
2031 else if (strstr (bfd_get_target (stdoutput
), "-hppa") != NULL
)
2032 desc_reloc
= 80; /* R_PARISC_DIR64. */
2034 desc_reloc
= BFD_RELOC_64
;
2037 /* We have to create a note for *each* code section.
2038 Linker garbage collection might discard some. */
2042 for (sym
= symbol_rootP
; sym
!= NULL
; sym
= symbol_next (sym
))
2043 if ((bsym
= symbol_get_bfdsym (sym
)) != NULL
2044 && bsym
->flags
& BSF_SECTION_SYM
2045 && bsym
->section
!= NULL
2046 /* Skip linkonce sections - we cannot use these section symbols as they may disappear. */
2047 && (bsym
->section
->flags
& (SEC_CODE
| SEC_LINK_ONCE
)) == SEC_CODE
2048 /* Not all linkonce sections are flagged... */
2049 && !startswith (S_GET_NAME (sym
), ".gnu.linkonce"))
2051 /* Create a version note. */
2053 note
= frag_more (note_size
);
2054 memset (note
, 0, note_size
);
2056 if (target_big_endian
)
2058 note
[3] = 8; /* strlen (name) + 1. */
2059 note
[7] = desc_size
; /* Two N-byte offsets. */
2060 note
[10] = NT_GNU_BUILD_ATTRIBUTE_OPEN
>> 8;
2061 note
[11] = NT_GNU_BUILD_ATTRIBUTE_OPEN
& 0xff;
2065 note
[0] = 8; /* strlen (name) + 1. */
2066 note
[4] = desc_size
; /* Two N-byte offsets. */
2067 note
[8] = NT_GNU_BUILD_ATTRIBUTE_OPEN
& 0xff;
2068 note
[9] = NT_GNU_BUILD_ATTRIBUTE_OPEN
>> 8;
2071 /* The a1 version number indicates that this note was
2072 generated by the assembler and not the gcc annobin plugin. */
2073 memcpy (note
+ 12, "GA$\ 13a1", 8);
2075 /* Create a relocation to install the start address of the note... */
2076 create_note_reloc (sec
, sym
, total_size
, 20, desc_size
/ 2, desc_reloc
, 0, note
);
2078 /* ...and another one to install the end address. */
2079 create_note_reloc (sec
, sym
, total_size
, desc2_offset
,
2082 bfd_section_size (bsym
->section
),
2085 /* Mark the section symbol used in relocation so that it will be
2086 included in the symbol table. */
2087 symbol_mark_used_in_reloc (sym
);
2089 total_size
+= note_size
;
2090 /* FIXME: Maybe add a note recording the assembler command line and version ? */
2093 /* Install the note(s) into the section. */
2095 bfd_set_section_contents (stdoutput
, sec
, (bfd_byte
*) note
, 0, total_size
);
2096 subsegs_finish_section (sec
);
2097 relax_segment (seg_info (sec
)->frchainP
->frch_root
, sec
, 0);
2098 size_seg (stdoutput
, sec
, NULL
);
2100 #endif /* OBJ_ELF */
2102 /* Write the object file. */
2105 write_object_file (void)
2107 struct relax_seg_info rsi
;
2108 #ifndef WORKING_DOT_WORD
2109 fragS
*fragP
; /* Track along all frags. */
2114 #ifdef md_pre_output_hook
2118 #ifdef md_pre_relax_hook
2122 /* From now on, we don't care about sub-segments. Build one frag chain
2123 for each segment. Linked through fr_next. */
2125 /* Remove the sections created by gas for its own purposes. */
2129 bfd_section_list_remove (stdoutput
, reg_section
);
2130 bfd_section_list_remove (stdoutput
, expr_section
);
2131 stdoutput
->section_count
-= 2;
2133 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
2136 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
2138 /* We have two segments. If user gave -R flag, then we must put the
2139 data frags into the text segment. Do this before relaxing so
2140 we know to take advantage of -R and make shorter addresses. */
2141 if (flag_readonly_data_in_text
)
2143 merge_data_into_text ();
2149 #ifndef WORKING_DOT_WORD
2150 /* We need to reset the markers in the broken word list and
2151 associated frags between calls to relax_segment (via
2152 relax_seg). Since the broken word list is global, we do it
2153 once per round, rather than locally in relax_segment for each
2155 struct broken_word
*brokp
;
2157 for (brokp
= broken_words
;
2158 brokp
!= (struct broken_word
*) NULL
;
2159 brokp
= brokp
->next_broken_word
)
2163 if (brokp
->dispfrag
!= (fragS
*) NULL
2164 && brokp
->dispfrag
->fr_type
== rs_broken_word
)
2165 brokp
->dispfrag
->fr_subtype
= 0;
2170 bfd_map_over_sections (stdoutput
, relax_seg
, &rsi
);
2176 /* Note - Most ports will use the default value of
2177 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
2178 local symbols to be resolved, removing their frag information.
2179 Some ports however, will not have finished relaxing all of
2180 their frags and will still need the local symbol frag
2181 information. These ports can set
2182 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
2183 finalize_syms
= TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
;
2185 bfd_map_over_sections (stdoutput
, size_seg
, (char *) 0);
2187 /* Relaxation has completed. Freeze all syms. */
2190 dwarf2dbg_final_check ();
2192 #ifdef md_post_relax_hook
2198 create_obj_attrs_section ();
2201 #ifndef WORKING_DOT_WORD
2203 struct broken_word
*lie
;
2204 struct broken_word
**prevP
;
2206 prevP
= &broken_words
;
2207 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
2212 subseg_change (lie
->seg
, lie
->subseg
);
2213 exp
.X_op
= O_subtract
;
2214 exp
.X_add_symbol
= lie
->add
;
2215 exp
.X_op_symbol
= lie
->sub
;
2216 exp
.X_add_number
= lie
->addnum
;
2217 #ifdef TC_CONS_FIX_NEW
2218 TC_CONS_FIX_NEW (lie
->frag
,
2219 lie
->word_goes_here
- lie
->frag
->fr_literal
,
2220 2, &exp
, TC_PARSE_CONS_RETURN_NONE
);
2222 fix_new_exp (lie
->frag
,
2223 lie
->word_goes_here
- lie
->frag
->fr_literal
,
2224 2, &exp
, 0, BFD_RELOC_16
);
2226 *prevP
= lie
->next_broken_word
;
2229 prevP
= &(lie
->next_broken_word
);
2231 for (lie
= broken_words
; lie
;)
2233 struct broken_word
*untruth
;
2235 addressT table_addr
;
2236 addressT from_addr
, to_addr
;
2239 subseg_change (lie
->seg
, lie
->subseg
);
2240 fragP
= lie
->dispfrag
;
2242 /* Find out how many broken_words go here. */
2245 untruth
&& untruth
->dispfrag
== fragP
;
2246 untruth
= untruth
->next_broken_word
)
2247 if (untruth
->added
== 1)
2250 table_ptr
= lie
->dispfrag
->fr_opcode
;
2251 table_addr
= (lie
->dispfrag
->fr_address
2252 + (table_ptr
- lie
->dispfrag
->fr_literal
));
2253 /* Create the jump around the long jumps. This is a short
2254 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
2255 from_addr
= table_addr
;
2256 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
2257 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
2259 table_ptr
+= md_short_jump_size
;
2260 table_addr
+= md_short_jump_size
;
2263 lie
&& lie
->dispfrag
== fragP
;
2264 m
++, lie
= lie
->next_broken_word
)
2266 if (lie
->added
== 2)
2268 /* Patch the jump table. */
2269 for (untruth
= (struct broken_word
*) (fragP
->fr_symbol
);
2270 untruth
&& untruth
->dispfrag
== fragP
;
2271 untruth
= untruth
->next_broken_word
)
2273 if (untruth
->use_jump
== lie
)
2275 /* This is the offset from ??? to table_ptr+0.
2276 The target is the same for all users of this
2277 md_long_jump, but the "sub" bases (and hence the
2278 offsets) may be different. */
2279 addressT to_word
= table_addr
- S_GET_VALUE (untruth
->sub
);
2280 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
2281 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word
, untruth
);
2283 md_number_to_chars (untruth
->word_goes_here
, to_word
, 2);
2287 /* Install the long jump. */
2288 /* This is a long jump from table_ptr+0 to the final target. */
2289 from_addr
= table_addr
;
2290 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
2291 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
2293 table_ptr
+= md_long_jump_size
;
2294 table_addr
+= md_long_jump_size
;
2298 #endif /* not WORKING_DOT_WORD */
2300 /* Resolve symbol values. This needs to be done before processing
2306 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2307 resolve_symbol_value (symp
);
2309 resolve_local_symbol_values ();
2310 resolve_reloc_expr_symbols ();
2314 maybe_generate_build_notes ();
2319 #ifdef tc_frob_file_before_adjust
2320 tc_frob_file_before_adjust ();
2322 #ifdef obj_frob_file_before_adjust
2323 obj_frob_file_before_adjust ();
2326 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
2328 #ifdef tc_frob_file_before_fix
2329 tc_frob_file_before_fix ();
2331 #ifdef obj_frob_file_before_fix
2332 obj_frob_file_before_fix ();
2335 bfd_map_over_sections (stdoutput
, fix_segment
, (char *) 0);
2337 /* Set up symbol table, and write it out. */
2341 bool skip_next_symbol
= false;
2343 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2348 if (skip_next_symbol
)
2350 /* Don't do anything besides moving the value of the
2351 symbol from the GAS value-field to the BFD value-field. */
2352 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
2353 skip_next_symbol
= false;
2357 if (symbol_mri_common_p (symp
))
2359 if (S_IS_EXTERNAL (symp
))
2360 as_bad (_("%s: global symbols not supported in common sections"),
2362 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2366 name
= S_GET_NAME (symp
);
2370 decode_local_label_name ((char *) S_GET_NAME (symp
));
2371 /* They only differ if `name' is a fb or dollar local
2373 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
2374 as_bad (_("local label `%s' is not defined"), name2
);
2377 /* Do it again, because adjust_reloc_syms might introduce
2378 more symbols. They'll probably only be section symbols,
2379 but they'll still need to have the values computed. */
2380 resolve_symbol_value (symp
);
2382 /* Skip symbols which were equated to undefined or common
2384 if (symbol_equated_reloc_p (symp
)
2385 || S_IS_WEAKREFR (symp
))
2387 const char *sname
= S_GET_NAME (symp
);
2389 if (S_IS_COMMON (symp
)
2390 && !TC_FAKE_LABEL (sname
)
2391 && !S_IS_WEAKREFR (symp
))
2393 expressionS
*e
= symbol_get_value_expression (symp
);
2395 as_bad (_("`%s' can't be equated to common symbol `%s'"),
2396 sname
, S_GET_NAME (e
->X_add_symbol
));
2398 if (S_GET_SEGMENT (symp
) == reg_section
)
2400 /* Report error only if we know the symbol name. */
2401 if (S_GET_NAME (symp
) != reg_section
->name
)
2402 as_bad (_("can't make global register symbol `%s'"),
2405 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2409 #ifdef obj_frob_symbol
2410 obj_frob_symbol (symp
, punt
);
2412 #ifdef tc_frob_symbol
2413 if (! punt
|| symbol_used_in_reloc_p (symp
))
2414 tc_frob_symbol (symp
, punt
);
2417 /* If we don't want to keep this symbol, splice it out of
2418 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2419 want section symbols. Otherwise, we skip local symbols
2420 and symbols that the frob_symbol macros told us to punt,
2421 but we keep such symbols if they are used in relocs. */
2422 if (symp
== abs_section_sym
2423 || (! EMIT_SECTION_SYMBOLS
2424 && symbol_section_p (symp
))
2425 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2426 opposites. Sometimes the former checks flags and the
2427 latter examines the name... */
2428 || (!S_IS_EXTERNAL (symp
)
2429 && (punt
|| S_IS_LOCAL (symp
) ||
2430 (S_IS_WEAKREFD (symp
) && ! symbol_used_p (symp
)))
2431 && ! symbol_used_in_reloc_p (symp
)))
2433 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2435 /* After symbol_remove, symbol_next(symp) still returns
2436 the one that came after it in the chain. So we don't
2437 need to do any extra cleanup work here. */
2441 /* Make sure we really got a value for the symbol. */
2442 if (! symbol_resolved_p (symp
))
2444 as_bad (_("can't resolve value for symbol `%s'"),
2446 symbol_mark_resolved (symp
);
2449 /* Set the value into the BFD symbol. Up til now the value
2450 has only been kept in the gas symbolS struct. */
2451 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
2453 /* A warning construct is a warning symbol followed by the
2454 symbol warned about. Don't let anything object-format or
2455 target-specific muck with it; it's ready for output. */
2456 if (symbol_get_bfdsym (symp
)->flags
& BSF_WARNING
)
2457 skip_next_symbol
= true;
2463 /* Now do any format-specific adjustments to the symbol table, such
2464 as adding file symbols. */
2465 #ifdef tc_adjust_symtab
2466 tc_adjust_symtab ();
2468 #ifdef obj_adjust_symtab
2469 obj_adjust_symtab ();
2472 /* Stop if there is an error. */
2473 if (!flag_always_generate_output
&& had_errors ())
2476 /* Now that all the sizes are known, and contents correct, we can
2477 start writing to the file. */
2480 /* If *_frob_file changes the symbol value at this point, it is
2481 responsible for moving the changed value into symp->bsym->value
2482 as well. Hopefully all symbol value changing can be done in
2487 #ifdef obj_frob_file
2490 #ifdef obj_coff_generate_pdata
2491 obj_coff_generate_pdata ();
2494 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
2496 #ifdef tc_frob_file_after_relocs
2497 tc_frob_file_after_relocs ();
2499 #ifdef obj_frob_file_after_relocs
2500 obj_frob_file_after_relocs ();
2503 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
2504 if (IS_ELF
&& flag_use_elf_stt_common
)
2505 stdoutput
->flags
|= BFD_CONVERT_ELF_COMMON
| BFD_USE_ELF_STT_COMMON
;
2508 /* Once all relocations have been written, we can compress the
2509 contents of the debug sections. This needs to be done before
2510 we start writing any sections, because it will affect the file
2511 layout, which is fixed once we start writing contents. */
2512 if (flag_compress_debug
)
2514 if (flag_compress_debug
== COMPRESS_DEBUG_GABI_ZLIB
)
2515 stdoutput
->flags
|= BFD_COMPRESS
| BFD_COMPRESS_GABI
;
2517 stdoutput
->flags
|= BFD_COMPRESS
;
2518 bfd_map_over_sections (stdoutput
, compress_debug
, (char *) 0);
2521 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
2524 #ifdef TC_GENERIC_RELAX_TABLE
2525 #ifndef md_generic_table_relax_frag
2526 #define md_generic_table_relax_frag relax_frag
2529 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2532 relax_frag (segT segment
, fragS
*fragP
, long stretch
)
2534 const relax_typeS
*this_type
;
2535 const relax_typeS
*start_type
;
2536 relax_substateT next_state
;
2537 relax_substateT this_state
;
2543 const relax_typeS
*table
;
2545 target
= fragP
->fr_offset
;
2546 address
= fragP
->fr_address
+ fragP
->fr_fix
;
2547 table
= TC_GENERIC_RELAX_TABLE
;
2548 this_state
= fragP
->fr_subtype
;
2549 start_type
= this_type
= table
+ this_state
;
2550 symbolP
= fragP
->fr_symbol
;
2556 sym_frag
= symbol_get_frag (symbolP
);
2558 #ifndef DIFF_EXPR_OK
2559 know (sym_frag
!= NULL
);
2561 know (S_GET_SEGMENT (symbolP
) != absolute_section
2562 || sym_frag
== &zero_address_frag
);
2563 target
+= S_GET_VALUE (symbolP
);
2565 /* If SYM_FRAG has yet to be reached on this pass, assume it
2566 will move by STRETCH just as we did, unless there is an
2567 alignment frag between here and SYM_FRAG. An alignment may
2568 well absorb any STRETCH, and we don't want to choose a larger
2569 branch insn by overestimating the needed reach of this
2570 branch. It isn't critical to calculate TARGET exactly; We
2571 know we'll be doing another pass if STRETCH is non-zero. */
2574 && sym_frag
->relax_marker
!= fragP
->relax_marker
2575 && S_GET_SEGMENT (symbolP
) == segment
)
2578 || sym_frag
->region
== fragP
->region
)
2580 /* If we get here we know we have a forward branch. This
2581 relax pass may have stretched previous instructions so
2582 far that omitting STRETCH would make the branch
2583 negative. Don't allow this in case the negative reach is
2584 large enough to require a larger branch instruction. */
2585 else if (target
< address
)
2590 aim
= target
- address
;
2591 #ifdef TC_PCREL_ADJUST
2592 /* Currently only the ns32k and arc needs this. */
2593 aim
+= TC_PCREL_ADJUST (fragP
);
2596 #ifdef md_prepare_relax_scan
2597 /* Formerly called M68K_AIM_KLUDGE. */
2598 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2603 /* Look backwards. */
2604 for (next_state
= this_type
->rlx_more
; next_state
;)
2605 if (aim
>= this_type
->rlx_backward
)
2609 /* Grow to next state. */
2610 this_state
= next_state
;
2611 this_type
= table
+ this_state
;
2612 next_state
= this_type
->rlx_more
;
2617 /* Look forwards. */
2618 for (next_state
= this_type
->rlx_more
; next_state
;)
2619 if (aim
<= this_type
->rlx_forward
)
2623 /* Grow to next state. */
2624 this_state
= next_state
;
2625 this_type
= table
+ this_state
;
2626 next_state
= this_type
->rlx_more
;
2630 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2632 fragP
->fr_subtype
= this_state
;
2636 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2638 /* Relax_align. Advance location counter to next address that has 'alignment'
2639 lowest order bits all 0s, return size of adjustment made. */
2640 static relax_addressT
2641 relax_align (relax_addressT address
, /* Address now. */
2642 int alignment
/* Alignment (binary). */)
2644 relax_addressT mask
;
2645 relax_addressT new_address
;
2647 mask
= ~((relax_addressT
) ~0 << alignment
);
2648 new_address
= (address
+ mask
) & (~mask
);
2649 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2651 /* We must provide lots of padding, so the linker can discard it
2652 when needed. The linker will not add extra space, ever. */
2653 new_address
+= (1 << alignment
);
2655 return (new_address
- address
);
2658 /* Now we have a segment, not a crowd of sub-segments, we can make
2663 After this, all frags in this segment have addresses that are correct
2664 within the segment. Since segments live in different file addresses,
2665 these frag addresses may not be the same as final object-file
2669 relax_segment (struct frag
*segment_frag_root
, segT segment
, int pass
)
2671 unsigned long frag_count
;
2673 relax_addressT address
;
2677 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2678 subseg_change (segment
, 0);
2680 /* For each frag in segment: count and store (a 1st guess of)
2684 for (frag_count
= 0, fragP
= segment_frag_root
;
2686 fragP
= fragP
->fr_next
, frag_count
++)
2688 fragP
->region
= region
;
2689 fragP
->relax_marker
= 0;
2690 fragP
->fr_address
= address
;
2691 address
+= fragP
->fr_fix
;
2693 switch (fragP
->fr_type
)
2696 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2703 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2705 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2708 if (offset
% fragP
->fr_var
!= 0)
2710 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2711 ngettext ("alignment padding (%lu byte) "
2712 "not a multiple of %ld",
2713 "alignment padding (%lu bytes) "
2714 "not a multiple of %ld",
2715 (unsigned long) offset
),
2716 (unsigned long) offset
, (long) fragP
->fr_var
);
2717 offset
-= (offset
% fragP
->fr_var
);
2726 /* Assume .org is nugatory. It will grow with 1st relax. */
2734 case rs_machine_dependent
:
2735 /* If fr_symbol is an expression, this call to
2736 resolve_symbol_value sets up the correct segment, which will
2737 likely be needed in md_estimate_size_before_relax. */
2738 if (fragP
->fr_symbol
)
2739 resolve_symbol_value (fragP
->fr_symbol
);
2741 address
+= md_estimate_size_before_relax (fragP
, segment
);
2744 #ifndef WORKING_DOT_WORD
2745 /* Broken words don't concern us yet. */
2746 case rs_broken_word
:
2751 /* Initial guess is always 1; doing otherwise can result in
2752 stable solutions that are larger than the minimum. */
2753 address
+= fragP
->fr_offset
= 1;
2757 address
+= eh_frame_estimate_size_before_relax (fragP
);
2761 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
2765 BAD_CASE (fragP
->fr_type
);
2772 unsigned long max_iterations
;
2774 /* Cumulative address adjustment. */
2777 /* Have we made any adjustment this pass? We can't just test
2778 stretch because one piece of code may have grown and another
2782 /* Most horrible, but gcc may give us some exception data that
2783 is impossible to assemble, of the form
2787 .uleb128 end - start
2793 If the leb128 is two bytes in size, then end-start is 128*128,
2794 which requires a three byte leb128. If the leb128 is three
2795 bytes in size, then end-start is 128*128-1, which requires a
2796 two byte leb128. We work around this dilemma by inserting
2797 an extra 4 bytes of alignment just after the .align. This
2798 works because the data after the align is accessed relative to
2801 This counter is used in a tiny state machine to detect
2802 whether a leb128 followed by an align is impossible to
2804 int rs_leb128_fudge
= 0;
2806 /* We want to prevent going into an infinite loop where one frag grows
2807 depending upon the location of a symbol which is in turn moved by
2808 the growing frag. eg:
2814 So we dictate that this algorithm can be at most O2. */
2815 max_iterations
= frag_count
* frag_count
;
2816 /* Check for overflow. */
2817 if (max_iterations
< frag_count
)
2818 max_iterations
= frag_count
;
2826 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2829 addressT was_address
;
2833 fragP
->relax_marker
^= 1;
2834 was_address
= fragP
->fr_address
;
2835 address
= fragP
->fr_address
+= stretch
;
2836 symbolP
= fragP
->fr_symbol
;
2837 offset
= fragP
->fr_offset
;
2839 switch (fragP
->fr_type
)
2841 case rs_fill
: /* .fill never relaxes. */
2845 #ifndef WORKING_DOT_WORD
2846 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2847 for it I do not want to write it. I do not want to have
2848 anything to do with it. This is not the proper way to
2849 implement this misfeature. */
2850 case rs_broken_word
:
2852 struct broken_word
*lie
;
2853 struct broken_word
*untruth
;
2855 /* Yes this is ugly (storing the broken_word pointer
2856 in the symbol slot). Still, this whole chunk of
2857 code is ugly, and I don't feel like doing anything
2858 about it. Think of it as stubbornness in action. */
2860 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2861 lie
&& lie
->dispfrag
== fragP
;
2862 lie
= lie
->next_broken_word
)
2868 offset
= (S_GET_VALUE (lie
->add
)
2870 - S_GET_VALUE (lie
->sub
));
2871 if (offset
<= -32768 || offset
>= 32767)
2873 if (flag_warn_displacement
)
2877 bfd_sprintf_vma (stdoutput
, buf
,
2878 (addressT
) lie
->addnum
);
2879 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2880 _(".word %s-%s+%s didn't fit"),
2881 S_GET_NAME (lie
->add
),
2882 S_GET_NAME (lie
->sub
),
2885 if (fragP
->fr_subtype
== 0)
2887 fragP
->fr_subtype
++;
2888 growth
+= md_short_jump_size
;
2891 /* Redirect *all* words of this table with the same
2892 target, lest we have to handle the case where the
2893 same target but with a offset that fits on this
2894 round overflows at the next relaxation round. */
2895 for (untruth
= (struct broken_word
*) (fragP
->fr_symbol
);
2896 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2897 untruth
= untruth
->next_broken_word
)
2898 if ((symbol_get_frag (untruth
->add
)
2899 == symbol_get_frag (lie
->add
))
2900 && (S_GET_VALUE (untruth
->add
)
2901 == S_GET_VALUE (lie
->add
)))
2904 untruth
->use_jump
= lie
;
2908 growth
+= md_long_jump_size
;
2913 } /* case rs_broken_word */
2919 addressT oldoff
, newoff
;
2921 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2923 newoff
= relax_align (address
+ fragP
->fr_fix
,
2926 if (fragP
->fr_subtype
!= 0)
2928 if (oldoff
> fragP
->fr_subtype
)
2930 if (newoff
> fragP
->fr_subtype
)
2934 growth
= newoff
- oldoff
;
2936 /* If this align happens to follow a leb128 and
2937 we have determined that the leb128 is bouncing
2938 in size, then break the cycle by inserting an
2941 && (rs_leb128_fudge
& 16) != 0
2942 && (rs_leb128_fudge
& 15) >= 2)
2944 segment_info_type
*seginfo
= seg_info (segment
);
2945 struct obstack
*ob
= &seginfo
->frchainP
->frch_obstack
;
2948 newf
= frag_alloc (ob
);
2949 obstack_blank_fast (ob
, fragP
->fr_var
);
2950 obstack_finish (ob
);
2951 memcpy (newf
, fragP
, SIZEOF_STRUCT_FRAG
);
2952 memcpy (newf
->fr_literal
,
2953 fragP
->fr_literal
+ fragP
->fr_fix
,
2955 newf
->fr_type
= rs_fill
;
2956 newf
->fr_address
= address
+ fragP
->fr_fix
+ newoff
;
2958 newf
->fr_offset
= (((offsetT
) 1 << fragP
->fr_offset
)
2960 if (newf
->fr_offset
* newf
->fr_var
2961 != (offsetT
) 1 << fragP
->fr_offset
)
2963 newf
->fr_offset
= (offsetT
) 1 << fragP
->fr_offset
;
2966 /* Include size of new frag in GROWTH. */
2967 growth
+= newf
->fr_offset
* newf
->fr_var
;
2968 /* Adjust the new frag address for the amount
2969 we'll add when we process the new frag. */
2970 newf
->fr_address
-= stretch
+ growth
;
2971 newf
->relax_marker
^= 1;
2972 fragP
->fr_next
= newf
;
2974 as_warn (_("padding added"));
2982 offsetT target
= offset
;
2987 /* Convert from an actual address to an octet offset
2988 into the section. Here it is assumed that the
2989 section's VMA is zero, and can omit subtracting it
2990 from the symbol's value to get the address offset. */
2991 know (S_GET_SEGMENT (symbolP
)->vma
== 0);
2992 target
+= S_GET_VALUE (symbolP
) * OCTETS_PER_BYTE
;
2995 know (fragP
->fr_next
);
2996 after
= fragP
->fr_next
->fr_address
+ stretch
;
2997 growth
= target
- after
;
2999 /* Growth may be negative, but variable part of frag
3000 cannot have fewer than 0 chars. That is, we can't
3002 if ((offsetT
) (address
+ fragP
->fr_fix
) > target
)
3006 /* Don't error on first few frag relax passes.
3007 The symbol might be an expression involving
3008 symbol values from other sections. If those
3009 sections have not yet been processed their
3010 frags will all have zero addresses, so we
3011 will calculate incorrect values for them. The
3012 number of passes we allow before giving an
3013 error is somewhat arbitrary. It should be at
3014 least one, with larger values requiring
3015 increasingly contrived dependencies between
3016 frags to trigger a false error. */
3019 /* Force another pass. */
3024 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
3025 _("attempt to move .org backwards"));
3027 /* We've issued an error message. Change the
3028 frag to avoid cascading errors. */
3029 fragP
->fr_type
= rs_align
;
3030 fragP
->fr_subtype
= 0;
3031 fragP
->fr_offset
= 0;
3032 fragP
->fr_fix
= after
- address
;
3044 amount
= S_GET_VALUE (symbolP
);
3045 if (S_GET_SEGMENT (symbolP
) != absolute_section
3046 || S_IS_COMMON (symbolP
)
3047 || ! S_IS_DEFINED (symbolP
))
3049 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
3050 _(".space, .nops or .fill specifies non-absolute value"));
3051 /* Prevent repeat of this error message. */
3052 fragP
->fr_symbol
= 0;
3054 else if (amount
< 0)
3056 /* Don't error on first few frag relax passes.
3057 See rs_org comment for a longer explanation. */
3064 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
3065 _(".space, .nops or .fill with negative value, ignored"));
3066 fragP
->fr_symbol
= 0;
3069 growth
= (was_address
+ fragP
->fr_fix
+ amount
3070 - fragP
->fr_next
->fr_address
);
3074 case rs_machine_dependent
:
3075 #ifdef md_relax_frag
3076 growth
= md_relax_frag (segment
, fragP
, stretch
);
3078 #ifdef TC_GENERIC_RELAX_TABLE
3079 /* The default way to relax a frag is to look through
3080 TC_GENERIC_RELAX_TABLE. */
3081 growth
= md_generic_table_relax_frag (segment
, fragP
,
3083 #endif /* TC_GENERIC_RELAX_TABLE */
3092 value
= resolve_symbol_value (fragP
->fr_symbol
);
3093 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
3094 growth
= size
- fragP
->fr_offset
;
3095 fragP
->fr_offset
= size
;
3100 growth
= eh_frame_relax_frag (fragP
);
3104 growth
= dwarf2dbg_relax_frag (fragP
);
3108 BAD_CASE (fragP
->fr_type
);
3115 if (fragP
->fr_type
== rs_leb128
)
3116 rs_leb128_fudge
+= 16;
3117 else if (fragP
->fr_type
== rs_align
3118 && (rs_leb128_fudge
& 16) != 0
3120 rs_leb128_fudge
+= 16;
3122 rs_leb128_fudge
= 0;
3127 && (rs_leb128_fudge
& 16) == 0
3128 && (rs_leb128_fudge
& -16) != 0)
3129 rs_leb128_fudge
+= 1;
3131 rs_leb128_fudge
= 0;
3133 /* Until nothing further to relax. */
3134 while (stretched
&& -- max_iterations
);
3137 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
3138 segment_name (segment
));
3141 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
3142 if (fragP
->last_fr_address
!= fragP
->fr_address
)
3144 fragP
->last_fr_address
= fragP
->fr_address
;
3151 number_to_chars_bigendian (char *buf
, valueT val
, int n
)
3157 buf
[n
] = val
& 0xff;
3163 number_to_chars_littleendian (char *buf
, valueT val
, int n
)
3169 *buf
++ = val
& 0xff;
3175 write_print_statistics (FILE *file
)
3177 fprintf (file
, "fixups: %d\n", n_fixups
);
3180 /* For debugging. */
3181 extern int indent_level
;
3184 print_fixup (fixS
*fixp
)
3187 fprintf (stderr
, "fix ");
3188 fprintf_vma (stderr
, (bfd_vma
)((bfd_hostptr_t
) fixp
));
3189 fprintf (stderr
, " %s:%d",fixp
->fx_file
, fixp
->fx_line
);
3191 fprintf (stderr
, " pcrel");
3192 if (fixp
->fx_pcrel_adjust
)
3193 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
3195 fprintf (stderr
, " tcbit");
3197 fprintf (stderr
, " done");
3198 fprintf (stderr
, "\n size=%d frag=", fixp
->fx_size
);
3199 fprintf_vma (stderr
, (bfd_vma
) ((bfd_hostptr_t
) fixp
->fx_frag
));
3200 fprintf (stderr
, " where=%ld offset=%lx addnumber=%lx",
3201 (long) fixp
->fx_where
,
3202 (unsigned long) fixp
->fx_offset
,
3203 (unsigned long) fixp
->fx_addnumber
);
3204 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
3208 fprintf (stderr
, "\n +<");
3209 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
3210 fprintf (stderr
, ">");
3214 fprintf (stderr
, "\n -<");
3215 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
3216 fprintf (stderr
, ">");
3218 fprintf (stderr
, "\n");
3219 #ifdef TC_FIX_DATA_PRINT
3220 TC_FIX_DATA_PRINT (stderr
, fixp
);