1 /* write.c - emit .o file
2 Copyright (C) 1986-2015 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 byteordering correctly. But... */
26 #include "output-file.h"
27 #include "dwarf2dbg.h"
29 #include "compress-debug.h"
31 #ifndef TC_FORCE_RELOCATION
32 #define TC_FORCE_RELOCATION(FIX) \
33 (generic_force_reloc (FIX))
36 #ifndef TC_FORCE_RELOCATION_ABS
37 #define TC_FORCE_RELOCATION_ABS(FIX) \
38 (TC_FORCE_RELOCATION (FIX))
41 #ifndef TC_FORCE_RELOCATION_LOCAL
42 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
44 || TC_FORCE_RELOCATION (FIX))
47 #ifndef TC_FORCE_RELOCATION_SUB_SAME
48 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
52 #ifndef md_register_arithmetic
53 # define md_register_arithmetic 1
56 #ifndef TC_FORCE_RELOCATION_SUB_ABS
57 #define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG) \
58 (!md_register_arithmetic && (SEG) == reg_section)
61 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
63 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) \
64 (!md_register_arithmetic && (SEG) == reg_section)
66 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) 1
70 #ifndef TC_VALIDATE_FIX_SUB
71 #ifdef UNDEFINED_DIFFERENCE_OK
72 /* The PA needs this for PIC code generation. */
73 #define TC_VALIDATE_FIX_SUB(FIX, SEG) \
74 (md_register_arithmetic || (SEG) != reg_section)
76 #define TC_VALIDATE_FIX_SUB(FIX, SEG) \
77 ((md_register_arithmetic || (SEG) != reg_section) \
78 && ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
79 || (FIX)->fx_r_type == BFD_RELOC_GPREL16))
83 #ifndef TC_LINKRELAX_FIXUP
84 #define TC_LINKRELAX_FIXUP(SEG) 1
87 #ifndef MD_APPLY_SYM_VALUE
88 #define MD_APPLY_SYM_VALUE(FIX) 1
91 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
92 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
95 #ifndef MD_PCREL_FROM_SECTION
96 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
100 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
103 /* Positive values of TC_FX_SIZE_SLACK allow a target to define
104 fixups that far past the end of a frag. Having such fixups
105 is of course most most likely a bug in setting fx_size correctly.
106 A negative value disables the fixup check entirely, which is
107 appropriate for something like the Renesas / SuperH SH_COUNT
109 #ifndef TC_FX_SIZE_SLACK
110 #define TC_FX_SIZE_SLACK(FIX) 0
113 /* Used to control final evaluation of expressions. */
114 int finalize_syms
= 0;
116 int symbol_table_frozen
;
118 symbolS
*abs_section_sym
;
120 /* Remember the value of dot when parsing expressions. */
123 /* The frag that dot_value is based from. */
126 /* Relocs generated by ".reloc" pseudo. */
127 struct reloc_list
* reloc_list
;
129 void print_fixup (fixS
*);
131 /* We generally attach relocs to frag chains. However, after we have
132 chained these all together into a segment, any relocs we add after
133 that must be attached to a segment. This will include relocs added
134 in md_estimate_size_for_relax, for example. */
135 static int frags_chained
= 0;
139 #define RELOC_ENUM enum bfd_reloc_code_real
141 /* Create a fixS in obstack 'notes'. */
144 fix_new_internal (fragS
*frag
, /* Which frag? */
145 int where
, /* Where in that frag? */
146 int size
, /* 1, 2, or 4 usually. */
147 symbolS
*add_symbol
, /* X_add_symbol. */
148 symbolS
*sub_symbol
, /* X_op_symbol. */
149 offsetT offset
, /* X_add_number. */
150 int pcrel
, /* TRUE if PC-relative relocation. */
151 RELOC_ENUM r_type
/* Relocation type. */,
152 int at_beginning
) /* Add to the start of the list? */
158 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
160 fixP
->fx_frag
= frag
;
161 fixP
->fx_where
= where
;
162 fixP
->fx_size
= size
;
163 /* We've made fx_size a narrow field; check that it's wide enough. */
164 if (fixP
->fx_size
!= size
)
166 as_bad (_("field fx_size too small to hold %d"), size
);
169 fixP
->fx_addsy
= add_symbol
;
170 fixP
->fx_subsy
= sub_symbol
;
171 fixP
->fx_offset
= offset
;
172 fixP
->fx_dot_value
= dot_value
;
173 fixP
->fx_dot_frag
= dot_frag
;
174 fixP
->fx_pcrel
= pcrel
;
175 fixP
->fx_r_type
= r_type
;
176 fixP
->fx_im_disp
= 0;
177 fixP
->fx_pcrel_adjust
= 0;
178 fixP
->fx_bit_fixP
= 0;
179 fixP
->fx_addnumber
= 0;
183 fixP
->fx_no_overflow
= 0;
187 fixP
->fx_cgen
.insn
= NULL
;
188 fixP
->fx_cgen
.opinfo
= 0;
192 TC_INIT_FIX_DATA (fixP
);
195 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
199 fixS
**seg_fix_rootP
= (frags_chained
200 ? &seg_info (now_seg
)->fix_root
201 : &frchain_now
->fix_root
);
202 fixS
**seg_fix_tailP
= (frags_chained
203 ? &seg_info (now_seg
)->fix_tail
204 : &frchain_now
->fix_tail
);
208 fixP
->fx_next
= *seg_fix_rootP
;
209 *seg_fix_rootP
= fixP
;
210 if (fixP
->fx_next
== NULL
)
211 *seg_fix_tailP
= fixP
;
215 fixP
->fx_next
= NULL
;
217 (*seg_fix_tailP
)->fx_next
= fixP
;
219 *seg_fix_rootP
= fixP
;
220 *seg_fix_tailP
= fixP
;
227 /* Create a fixup relative to a symbol (plus a constant). */
230 fix_new (fragS
*frag
, /* Which frag? */
231 int where
, /* Where in that frag? */
232 int size
, /* 1, 2, or 4 usually. */
233 symbolS
*add_symbol
, /* X_add_symbol. */
234 offsetT offset
, /* X_add_number. */
235 int pcrel
, /* TRUE if PC-relative relocation. */
236 RELOC_ENUM r_type
/* Relocation type. */)
238 return fix_new_internal (frag
, where
, size
, add_symbol
,
239 (symbolS
*) NULL
, offset
, pcrel
, r_type
, FALSE
);
242 /* Create a fixup for an expression. Currently we only support fixups
243 for difference expressions. That is itself more than most object
244 file formats support anyhow. */
247 fix_new_exp (fragS
*frag
, /* Which frag? */
248 int where
, /* Where in that frag? */
249 int size
, /* 1, 2, or 4 usually. */
250 expressionS
*exp
, /* Expression. */
251 int pcrel
, /* TRUE if PC-relative relocation. */
252 RELOC_ENUM r_type
/* Relocation type. */)
264 as_bad (_("register value used as expression"));
268 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
269 the difference expression cannot immediately be reduced. */
271 symbolS
*stmp
= make_expr_symbol (exp
);
273 exp
->X_op
= O_symbol
;
274 exp
->X_op_symbol
= 0;
275 exp
->X_add_symbol
= stmp
;
276 exp
->X_add_number
= 0;
278 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
282 add
= exp
->X_add_symbol
;
283 off
= exp
->X_add_number
;
284 r_type
= BFD_RELOC_RVA
;
288 sub
= exp
->X_add_symbol
;
289 off
= exp
->X_add_number
;
293 sub
= exp
->X_op_symbol
;
296 add
= exp
->X_add_symbol
;
299 off
= exp
->X_add_number
;
303 add
= make_expr_symbol (exp
);
307 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
,
311 /* Create a fixup at the beginning of FRAG. The arguments are the same
312 as for fix_new, except that WHERE is implicitly 0. */
315 fix_at_start (fragS
*frag
, int size
, symbolS
*add_symbol
,
316 offsetT offset
, int pcrel
, RELOC_ENUM r_type
)
318 return fix_new_internal (frag
, 0, size
, add_symbol
,
319 (symbolS
*) NULL
, offset
, pcrel
, r_type
, TRUE
);
322 /* Generic function to determine whether a fixup requires a relocation. */
324 generic_force_reloc (fixS
*fix
)
326 if (fix
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
327 || fix
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
330 if (fix
->fx_addsy
== NULL
)
333 return S_FORCE_RELOC (fix
->fx_addsy
, fix
->fx_subsy
== NULL
);
336 /* Append a string onto another string, bumping the pointer along. */
338 append (char **charPP
, char *fromP
, unsigned long length
)
340 /* Don't trust memcpy() of 0 chars. */
344 memcpy (*charPP
, fromP
, length
);
348 /* This routine records the largest alignment seen for each segment.
349 If the beginning of the segment is aligned on the worst-case
350 boundary, all of the other alignments within it will work. At
351 least one object format really uses this info. */
354 record_alignment (/* Segment to which alignment pertains. */
356 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
357 boundary, 2 => 4-byte boundary, etc.) */
360 if (seg
== absolute_section
)
363 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
364 bfd_set_section_alignment (stdoutput
, seg
, align
);
368 get_recorded_alignment (segT seg
)
370 if (seg
== absolute_section
)
373 return bfd_get_section_alignment (stdoutput
, seg
);
376 /* Reset the section indices after removing the gas created sections. */
379 renumber_sections (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *countparg
)
381 int *countp
= (int *) countparg
;
383 sec
->index
= *countp
;
388 chain_frchains_together_1 (segT section
, struct frchain
*frchp
)
390 fragS dummy
, *prev_frag
= &dummy
;
391 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
393 for (; frchp
; frchp
= frchp
->frch_next
)
395 prev_frag
->fr_next
= frchp
->frch_root
;
396 prev_frag
= frchp
->frch_last
;
397 gas_assert (prev_frag
->fr_type
!= 0);
398 if (frchp
->fix_root
!= (fixS
*) NULL
)
400 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
401 seg_info (section
)->fix_root
= frchp
->fix_root
;
402 prev_fix
->fx_next
= frchp
->fix_root
;
403 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
404 prev_fix
= frchp
->fix_tail
;
407 gas_assert (prev_frag
!= &dummy
408 && prev_frag
->fr_type
!= 0);
409 prev_frag
->fr_next
= 0;
414 chain_frchains_together (bfd
*abfd ATTRIBUTE_UNUSED
,
416 void *xxx ATTRIBUTE_UNUSED
)
418 segment_info_type
*info
;
420 /* BFD may have introduced its own sections without using
421 subseg_new, so it is possible that seg_info is NULL. */
422 info
= seg_info (section
);
423 if (info
!= (segment_info_type
*) NULL
)
424 info
->frchainP
->frch_last
425 = chain_frchains_together_1 (section
, info
->frchainP
);
427 /* Now that we've chained the frags together, we must add new fixups
428 to the segment, not to the frag chain. */
433 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED
, fragS
*fragP
)
435 switch (fragP
->fr_type
)
443 HANDLE_ALIGN (fragP
);
445 know (fragP
->fr_next
!= NULL
);
446 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
448 - fragP
->fr_fix
) / fragP
->fr_var
;
449 if (fragP
->fr_offset
< 0)
451 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
452 _("attempt to .org/.space backwards? (%ld)"),
453 (long) fragP
->fr_offset
);
454 fragP
->fr_offset
= 0;
456 fragP
->fr_type
= rs_fill
;
464 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
467 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
470 fragP
->fr_fix
+= size
;
471 fragP
->fr_type
= rs_fill
;
473 fragP
->fr_offset
= 0;
474 fragP
->fr_symbol
= NULL
;
479 eh_frame_convert_frag (fragP
);
483 dwarf2dbg_convert_frag (fragP
);
486 case rs_machine_dependent
:
487 md_convert_frag (stdoutput
, sec
, fragP
);
489 gas_assert (fragP
->fr_next
== NULL
490 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
493 /* After md_convert_frag, we make the frag into a ".space 0".
494 md_convert_frag() should set up any fixSs and constants
499 #ifndef WORKING_DOT_WORD
502 struct broken_word
*lie
;
504 if (fragP
->fr_subtype
)
506 fragP
->fr_fix
+= md_short_jump_size
;
507 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
508 lie
&& lie
->dispfrag
== fragP
;
509 lie
= lie
->next_broken_word
)
511 fragP
->fr_fix
+= md_long_jump_size
;
519 BAD_CASE (fragP
->fr_type
);
523 md_frag_check (fragP
);
527 struct relax_seg_info
534 relax_seg (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *xxx
)
536 segment_info_type
*seginfo
= seg_info (sec
);
537 struct relax_seg_info
*info
= (struct relax_seg_info
*) xxx
;
539 if (seginfo
&& seginfo
->frchainP
540 && relax_segment (seginfo
->frchainP
->frch_root
, sec
, info
->pass
))
545 size_seg (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
549 segment_info_type
*seginfo
;
551 valueT size
, newsize
;
553 subseg_change (sec
, 0);
555 seginfo
= seg_info (sec
);
556 if (seginfo
&& seginfo
->frchainP
)
558 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
559 cvt_frag_to_fill (sec
, fragp
);
560 for (fragp
= seginfo
->frchainP
->frch_root
;
562 fragp
= fragp
->fr_next
)
563 /* Walk to last elt. */
565 size
= fragp
->fr_address
+ fragp
->fr_fix
;
570 flags
= bfd_get_section_flags (abfd
, sec
);
571 if (size
== 0 && bfd_get_section_size (sec
) != 0 &&
572 (flags
& SEC_HAS_CONTENTS
) != 0)
575 if (size
> 0 && ! seginfo
->bss
)
576 flags
|= SEC_HAS_CONTENTS
;
579 x
= bfd_set_section_flags (abfd
, sec
, flags
);
582 newsize
= md_section_align (sec
, size
);
583 x
= bfd_set_section_size (abfd
, sec
, newsize
);
586 /* If the size had to be rounded up, add some padding in the last
588 gas_assert (newsize
>= size
);
591 fragS
*last
= seginfo
->frchainP
->frch_last
;
592 fragp
= seginfo
->frchainP
->frch_root
;
593 while (fragp
->fr_next
!= last
)
594 fragp
= fragp
->fr_next
;
595 last
->fr_address
= size
;
596 if ((newsize
- size
) % fragp
->fr_var
== 0)
597 fragp
->fr_offset
+= (newsize
- size
) / fragp
->fr_var
;
599 /* If we hit this abort, it's likely due to subsegs_finish not
600 providing sufficient alignment on the last frag, and the
601 machine dependent code using alignment frags with fr_var
606 #ifdef tc_frob_section
607 tc_frob_section (sec
);
609 #ifdef obj_frob_section
610 obj_frob_section (sec
);
616 dump_section_relocs (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, FILE *stream
)
618 segment_info_type
*seginfo
= seg_info (sec
);
619 fixS
*fixp
= seginfo
->fix_root
;
624 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
627 symbolS
*s
= fixp
->fx_addsy
;
629 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
630 (int) fixp
->fx_r_type
);
632 fprintf (stream
, "no sym\n");
635 print_symbol_value_1 (stream
, s
);
636 fprintf (stream
, "\n");
638 fixp
= fixp
->fx_next
;
642 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
645 #ifndef EMIT_SECTION_SYMBOLS
646 #define EMIT_SECTION_SYMBOLS 1
649 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
650 and check for validity. Convert RELOC_LIST from using U.A fields
653 resolve_reloc_expr_symbols (void)
655 bfd_vma addr_mask
= 1;
656 struct reloc_list
*r
;
658 /* Avoid a shift by the width of type. */
659 addr_mask
<<= bfd_arch_bits_per_address (stdoutput
) - 1;
663 for (r
= reloc_list
; r
; r
= r
->next
)
665 reloc_howto_type
*howto
= r
->u
.a
.howto
;
668 bfd_vma offset
, addend
;
671 resolve_symbol_value (r
->u
.a
.offset_sym
);
672 symval
= symbol_get_value_expression (r
->u
.a
.offset_sym
);
676 if (symval
->X_op
== O_constant
)
677 sym
= r
->u
.a
.offset_sym
;
678 else if (symval
->X_op
== O_symbol
)
680 sym
= symval
->X_add_symbol
;
681 offset
= symval
->X_add_number
;
682 symval
= symbol_get_value_expression (symval
->X_add_symbol
);
685 || symval
->X_op
!= O_constant
686 || (sec
= S_GET_SEGMENT (sym
)) == NULL
687 || !SEG_NORMAL (sec
))
689 as_bad_where (r
->file
, r
->line
, _("invalid offset expression"));
693 offset
+= S_GET_VALUE (sym
);
696 addend
= r
->u
.a
.addend
;
697 if (r
->u
.a
.sym
!= NULL
)
699 resolve_symbol_value (r
->u
.a
.sym
);
700 symval
= symbol_get_value_expression (r
->u
.a
.sym
);
701 if (symval
->X_op
== O_constant
)
703 else if (symval
->X_op
== O_symbol
)
705 sym
= symval
->X_add_symbol
;
706 addend
+= symval
->X_add_number
;
707 symval
= symbol_get_value_expression (symval
->X_add_symbol
);
709 if (symval
->X_op
!= O_constant
)
711 as_bad_where (r
->file
, r
->line
, _("invalid reloc expression"));
714 else if (sym
!= NULL
)
716 /* Convert relocs against local symbols to refer to the
717 corresponding section symbol plus offset instead. Keep
718 PC-relative relocs of the REL variety intact though to
719 prevent the offset from overflowing the relocated field,
720 unless it has enough bits to cover the whole address
722 if (S_IS_LOCAL (sym
) && !symbol_section_p (sym
)
724 || (howto
->partial_inplace
725 && (!howto
->pc_relative
726 || howto
->src_mask
== addr_mask
))))
728 asection
*symsec
= S_GET_SEGMENT (sym
);
729 if (!(((symsec
->flags
& SEC_MERGE
) != 0
731 || (symsec
->flags
& SEC_THREAD_LOCAL
) != 0))
733 addend
+= S_GET_VALUE (sym
);
734 sym
= section_symbol (symsec
);
737 symbol_mark_used_in_reloc (sym
);
742 if (abs_section_sym
== NULL
)
743 abs_section_sym
= section_symbol (absolute_section
);
744 sym
= abs_section_sym
;
748 r
->u
.b
.s
= symbol_get_bfdsym (sym
);
749 r
->u
.b
.r
.sym_ptr_ptr
= &r
->u
.b
.s
;
750 r
->u
.b
.r
.address
= offset
;
751 r
->u
.b
.r
.addend
= addend
;
752 r
->u
.b
.r
.howto
= howto
;
756 /* This pass over fixups decides whether symbols can be replaced with
760 adjust_reloc_syms (bfd
*abfd ATTRIBUTE_UNUSED
,
762 void *xxx ATTRIBUTE_UNUSED
)
764 segment_info_type
*seginfo
= seg_info (sec
);
770 dump_section_relocs (abfd
, sec
, stderr
);
772 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
776 else if (fixp
->fx_addsy
)
782 fprintf (stderr
, "\n\nadjusting fixup:\n");
786 sym
= fixp
->fx_addsy
;
788 /* All symbols should have already been resolved at this
789 point. It is possible to see unresolved expression
790 symbols, though, since they are not in the regular symbol
792 resolve_symbol_value (sym
);
794 if (fixp
->fx_subsy
!= NULL
)
795 resolve_symbol_value (fixp
->fx_subsy
);
797 /* If this symbol is equated to an undefined or common symbol,
798 convert the fixup to being against that symbol. */
799 while (symbol_equated_reloc_p (sym
)
800 || S_IS_WEAKREFR (sym
))
802 symbolS
*newsym
= symbol_get_value_expression (sym
)->X_add_symbol
;
805 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
806 fixp
->fx_addsy
= newsym
;
810 if (symbol_mri_common_p (sym
))
812 fixp
->fx_offset
+= S_GET_VALUE (sym
);
813 fixp
->fx_addsy
= symbol_get_value_expression (sym
)->X_add_symbol
;
817 /* If the symbol is undefined, common, weak, or global (ELF
818 shared libs), we can't replace it with the section symbol. */
819 if (S_FORCE_RELOC (fixp
->fx_addsy
, 1))
822 /* Is there some other (target cpu dependent) reason we can't adjust
823 this one? (E.g. relocations involving function addresses on
825 #ifdef tc_fix_adjustable
826 if (! tc_fix_adjustable (fixp
))
830 /* Since we're reducing to section symbols, don't attempt to reduce
831 anything that's already using one. */
832 if (symbol_section_p (sym
))
835 symsec
= S_GET_SEGMENT (sym
);
839 if (bfd_is_abs_section (symsec
)
840 || symsec
== reg_section
)
842 /* The fixup_segment routine normally will not use this
843 symbol in a relocation. */
847 /* Don't try to reduce relocs which refer to non-local symbols
848 in .linkonce sections. It can lead to confusion when a
849 debugging section refers to a .linkonce section. I hope
850 this will always be correct. */
851 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
853 if ((symsec
->flags
& SEC_LINK_ONCE
) != 0
855 /* The GNU toolchain uses an extension for ELF: a
856 section beginning with the magic string
857 .gnu.linkonce is a linkonce section. */
858 && strncmp (segment_name (symsec
), ".gnu.linkonce",
859 sizeof ".gnu.linkonce" - 1) == 0))
863 /* Never adjust a reloc against local symbol in a merge section
864 with non-zero addend. */
865 if ((symsec
->flags
& SEC_MERGE
) != 0
866 && (fixp
->fx_offset
!= 0 || fixp
->fx_subsy
!= NULL
))
869 /* Never adjust a reloc against TLS local symbol. */
870 if ((symsec
->flags
& SEC_THREAD_LOCAL
) != 0)
873 /* We refetch the segment when calling section_symbol, rather
874 than using symsec, because S_GET_VALUE may wind up changing
875 the section when it calls resolve_symbol_value. */
876 fixp
->fx_offset
+= S_GET_VALUE (sym
);
877 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
879 fprintf (stderr
, "\nadjusted fixup:\n");
884 dump_section_relocs (abfd
, sec
, stderr
);
889 Go through all the fixS's in a segment and see which ones can be
890 handled now. (These consist of fixS where we have since discovered
891 the value of a symbol, or the address of the frag involved.)
892 For each one, call md_apply_fix to put the fix into the frag data.
893 Ones that we couldn't completely handle here will be output later
894 by emit_relocations. */
897 fixup_segment (fixS
*fixP
, segT this_segment
)
901 segT add_symbol_segment
= absolute_section
;
903 if (fixP
!= NULL
&& abs_section_sym
== NULL
)
904 abs_section_sym
= section_symbol (absolute_section
);
906 /* If the linker is doing the relaxing, we must not do any fixups.
908 Well, strictly speaking that's not true -- we could do any that
909 are PC-relative and don't cross regions that could change size.
910 And for the i960 we might be able to turn callx/callj into bal
911 anyways in cases where we know the maximum displacement. */
912 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment
))
914 for (; fixP
; fixP
= fixP
->fx_next
)
917 if (fixP
->fx_addsy
== NULL
)
919 /* There was no symbol required by this relocation.
920 However, BFD doesn't really handle relocations
921 without symbols well. So fake up a local symbol in
922 the absolute section. */
923 fixP
->fx_addsy
= abs_section_sym
;
925 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
926 if (fixP
->fx_subsy
!= NULL
)
927 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
932 for (; fixP
; fixP
= fixP
->fx_next
)
935 fprintf (stderr
, "\nprocessing fixup:\n");
939 fragP
= fixP
->fx_frag
;
941 #ifdef TC_VALIDATE_FIX
942 TC_VALIDATE_FIX (fixP
, this_segment
, skip
);
944 add_number
= fixP
->fx_offset
;
946 if (fixP
->fx_addsy
!= NULL
)
947 add_symbol_segment
= S_GET_SEGMENT (fixP
->fx_addsy
);
949 if (fixP
->fx_subsy
!= NULL
)
951 segT sub_symbol_segment
;
952 resolve_symbol_value (fixP
->fx_subsy
);
953 sub_symbol_segment
= S_GET_SEGMENT (fixP
->fx_subsy
);
954 if (fixP
->fx_addsy
!= NULL
955 && sub_symbol_segment
== add_symbol_segment
956 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
957 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
958 && !TC_FORCE_RELOCATION_SUB_SAME (fixP
, add_symbol_segment
))
960 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
961 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
962 fixP
->fx_offset
= add_number
;
963 fixP
->fx_addsy
= NULL
;
964 fixP
->fx_subsy
= NULL
;
966 /* See the comment below about 68k weirdness. */
970 else if (sub_symbol_segment
== absolute_section
971 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
972 && !TC_FORCE_RELOCATION_SUB_ABS (fixP
, add_symbol_segment
))
974 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
975 fixP
->fx_offset
= add_number
;
976 fixP
->fx_subsy
= NULL
;
978 else if (sub_symbol_segment
== this_segment
979 && !S_FORCE_RELOC (fixP
->fx_subsy
, 0)
980 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP
, add_symbol_segment
))
982 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
983 fixP
->fx_offset
= (add_number
+ fixP
->fx_dot_value
984 + fixP
->fx_dot_frag
->fr_address
);
986 /* Make it pc-relative. If the back-end code has not
987 selected a pc-relative reloc, cancel the adjustment
988 we do later on all pc-relative relocs. */
991 /* Do this for m68k even if it's already described
992 as pc-relative. On the m68k, an operand of
993 "pc@(foo-.-2)" should address "foo" in a
998 add_number
+= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
999 fixP
->fx_subsy
= NULL
;
1002 else if (!TC_VALIDATE_FIX_SUB (fixP
, add_symbol_segment
))
1004 if (!md_register_arithmetic
1005 && (add_symbol_segment
== reg_section
1006 || sub_symbol_segment
== reg_section
))
1007 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1008 _("register value used as expression"));
1010 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1011 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
1012 fixP
->fx_addsy
? S_GET_NAME (fixP
->fx_addsy
) : "0",
1013 segment_name (add_symbol_segment
),
1014 S_GET_NAME (fixP
->fx_subsy
),
1015 segment_name (sub_symbol_segment
));
1017 else if (sub_symbol_segment
!= undefined_section
1018 && ! bfd_is_com_section (sub_symbol_segment
)
1019 && MD_APPLY_SYM_VALUE (fixP
))
1020 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
1025 if (add_symbol_segment
== this_segment
1026 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
1027 && !TC_FORCE_RELOCATION_LOCAL (fixP
))
1029 /* This fixup was made when the symbol's segment was
1030 SEG_UNKNOWN, but it is now in the local segment.
1031 So we know how to do the address without relocation. */
1032 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
1033 fixP
->fx_offset
= add_number
;
1035 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
1036 fixP
->fx_addsy
= NULL
;
1039 else if (add_symbol_segment
== absolute_section
1040 && !S_FORCE_RELOC (fixP
->fx_addsy
, 0)
1041 && !TC_FORCE_RELOCATION_ABS (fixP
))
1043 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
1044 fixP
->fx_offset
= add_number
;
1045 fixP
->fx_addsy
= NULL
;
1047 else if (add_symbol_segment
!= undefined_section
1048 && ! bfd_is_com_section (add_symbol_segment
)
1049 && MD_APPLY_SYM_VALUE (fixP
))
1050 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
1055 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
1056 if (!fixP
->fx_done
&& fixP
->fx_addsy
== NULL
)
1058 /* There was no symbol required by this relocation.
1059 However, BFD doesn't really handle relocations
1060 without symbols well. So fake up a local symbol in
1061 the absolute section. */
1062 fixP
->fx_addsy
= abs_section_sym
;
1067 md_apply_fix (fixP
, &add_number
, this_segment
);
1071 if (fixP
->fx_addsy
== NULL
)
1072 fixP
->fx_addsy
= abs_section_sym
;
1073 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
1074 if (fixP
->fx_subsy
!= NULL
)
1075 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
1078 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& fixP
->fx_size
!= 0)
1080 if (fixP
->fx_size
< sizeof (valueT
))
1085 mask
--; /* Set all bits to one. */
1086 mask
<<= fixP
->fx_size
* 8 - (fixP
->fx_signed
? 1 : 0);
1087 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
1089 char buf
[50], buf2
[50];
1090 sprint_value (buf
, fragP
->fr_address
+ fixP
->fx_where
);
1091 if (add_number
> 1000)
1092 sprint_value (buf2
, add_number
);
1094 sprintf (buf2
, "%ld", (long) add_number
);
1095 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1096 _("value of %s too large for field of %d bytes at %s"),
1097 buf2
, fixP
->fx_size
, buf
);
1098 } /* Generic error checking. */
1100 #ifdef WARN_SIGNED_OVERFLOW_WORD
1101 /* Warn if a .word value is too large when treated as a signed
1102 number. We already know it is not too negative. This is to
1103 catch over-large switches generated by gcc on the 68k. */
1104 if (!flag_signed_overflow_ok
1105 && fixP
->fx_size
== 2
1106 && add_number
> 0x7fff)
1107 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1108 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1110 (long) (fragP
->fr_address
+ fixP
->fx_where
));
1112 } /* Not a bit fix. */
1114 #ifdef TC_VALIDATE_FIX
1115 skip
: ATTRIBUTE_UNUSED_LABEL
1119 fprintf (stderr
, "result:\n");
1122 } /* For each fixS in this segment. */
1126 fix_segment (bfd
*abfd ATTRIBUTE_UNUSED
,
1128 void *xxx ATTRIBUTE_UNUSED
)
1130 segment_info_type
*seginfo
= seg_info (sec
);
1132 fixup_segment (seginfo
->fix_root
, sec
);
1136 install_reloc (asection
*sec
, arelent
*reloc
, fragS
*fragp
,
1137 char *file
, unsigned int line
)
1140 bfd_reloc_status_type s
;
1143 if (reloc
->sym_ptr_ptr
!= NULL
1144 && (sym
= *reloc
->sym_ptr_ptr
) != NULL
1145 && (sym
->flags
& BSF_KEEP
) == 0
1146 && ((sym
->flags
& BSF_SECTION_SYM
) == 0
1147 || (EMIT_SECTION_SYMBOLS
1148 && !bfd_is_abs_section (sym
->section
))))
1149 as_bad_where (file
, line
, _("redefined symbol cannot be used on reloc"));
1151 s
= bfd_install_relocation (stdoutput
, reloc
,
1152 fragp
->fr_literal
, fragp
->fr_address
,
1158 case bfd_reloc_overflow
:
1159 as_bad_where (file
, line
, _("relocation overflow"));
1161 case bfd_reloc_outofrange
:
1162 as_bad_where (file
, line
, _("relocation out of range"));
1165 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1171 get_frag_for_reloc (fragS
*last_frag
,
1172 const segment_info_type
*seginfo
,
1173 const struct reloc_list
*r
)
1177 for (f
= last_frag
; f
!= NULL
; f
= f
->fr_next
)
1178 if (f
->fr_address
<= r
->u
.b
.r
.address
1179 && r
->u
.b
.r
.address
< f
->fr_address
+ f
->fr_fix
)
1182 for (f
= seginfo
->frchainP
->frch_root
; f
!= NULL
; f
= f
->fr_next
)
1183 if (f
->fr_address
<= r
->u
.b
.r
.address
1184 && r
->u
.b
.r
.address
< f
->fr_address
+ f
->fr_fix
)
1187 for (f
= seginfo
->frchainP
->frch_root
; f
!= NULL
; f
= f
->fr_next
)
1188 if (f
->fr_address
<= r
->u
.b
.r
.address
1189 && r
->u
.b
.r
.address
<= f
->fr_address
+ f
->fr_fix
)
1192 as_bad_where (r
->file
, r
->line
,
1193 _("reloc not within (fixed part of) section"));
1198 write_relocs (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
1200 segment_info_type
*seginfo
= seg_info (sec
);
1202 struct reloc_list
*my_reloc_list
, **rp
, *r
;
1207 /* If seginfo is NULL, we did not create this section; don't do
1208 anything with it. */
1209 if (seginfo
== NULL
)
1213 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
1217 #ifdef RELOC_EXPANSION_POSSIBLE
1218 n
*= MAX_RELOC_EXPANSION
;
1221 /* Extract relocs for this section from reloc_list. */
1223 my_reloc_list
= NULL
;
1224 while ((r
= *rp
) != NULL
)
1226 if (r
->u
.b
.sec
== sec
)
1229 r
->next
= my_reloc_list
;
1237 relocs
= (arelent
**) xcalloc (n
, sizeof (arelent
*));
1242 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1247 #ifndef RELOC_EXPANSION_POSSIBLE
1256 fx_size
= fixp
->fx_size
;
1257 slack
= TC_FX_SIZE_SLACK (fixp
);
1259 fx_size
= fx_size
> slack
? fx_size
- slack
: 0;
1260 loc
= fixp
->fx_where
+ fx_size
;
1261 if (slack
>= 0 && loc
> fixp
->fx_frag
->fr_fix
)
1262 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1263 _("internal error: fixup not contained within frag"));
1265 #ifndef RELOC_EXPANSION_POSSIBLE
1266 *reloc
= tc_gen_reloc (sec
, fixp
);
1268 reloc
= tc_gen_reloc (sec
, fixp
);
1273 while (r
!= NULL
&& r
->u
.b
.r
.address
< (*reloc
)->address
)
1275 fragS
*f
= get_frag_for_reloc (last_frag
, seginfo
, r
);
1279 relocs
[n
++] = &r
->u
.b
.r
;
1280 install_reloc (sec
, &r
->u
.b
.r
, f
, r
->file
, r
->line
);
1284 relocs
[n
++] = *reloc
;
1285 install_reloc (sec
, *reloc
, fixp
->fx_frag
,
1286 fixp
->fx_file
, fixp
->fx_line
);
1287 #ifndef RELOC_EXPANSION_POSSIBLE
1297 fragS
*f
= get_frag_for_reloc (last_frag
, seginfo
, r
);
1301 relocs
[n
++] = &r
->u
.b
.r
;
1302 install_reloc (sec
, &r
->u
.b
.r
, f
, r
->file
, r
->line
);
1309 unsigned int k
, j
, nsyms
;
1311 sympp
= bfd_get_outsymbols (stdoutput
);
1312 nsyms
= bfd_get_symcount (stdoutput
);
1313 for (k
= 0; k
< n
; k
++)
1314 if (((*relocs
[k
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1316 for (j
= 0; j
< nsyms
; j
++)
1317 if (sympp
[j
] == *relocs
[k
]->sym_ptr_ptr
)
1327 flagword flags
= bfd_get_section_flags (abfd
, sec
);
1329 bfd_set_section_flags (abfd
, sec
, flags
);
1330 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1333 #ifdef SET_SECTION_RELOCS
1334 SET_SECTION_RELOCS (sec
, relocs
, n
);
1341 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1342 for (k
= 0; k
< n
; k
++)
1344 arelent
*rel
= relocs
[k
];
1345 asymbol
*s
= *rel
->sym_ptr_ptr
;
1346 fprintf (stderr
, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1347 k
, rel
, (unsigned long)rel
->address
, s
->name
,
1348 (unsigned long)rel
->addend
);
1355 compress_frag (struct z_stream_s
*strm
, const char *contents
, int in_size
,
1356 fragS
**last_newf
, struct obstack
*ob
)
1359 int total_out_size
= 0;
1360 fragS
*f
= *last_newf
;
1364 /* Call the compression routine repeatedly until it has finished
1365 processing the frag. */
1368 /* Reserve all the space available in the current chunk.
1369 If none is available, start a new frag. */
1370 avail_out
= obstack_room (ob
);
1373 obstack_finish (ob
);
1374 f
= frag_alloc (ob
);
1375 f
->fr_type
= rs_fill
;
1376 (*last_newf
)->fr_next
= f
;
1378 avail_out
= obstack_room (ob
);
1381 as_fatal (_("can't extend frag"));
1382 next_out
= obstack_next_free (ob
);
1383 obstack_blank_fast (ob
, avail_out
);
1384 out_size
= compress_data (strm
, &contents
, &in_size
,
1385 &next_out
, &avail_out
);
1389 f
->fr_fix
+= out_size
;
1390 total_out_size
+= out_size
;
1392 /* Return unused space. */
1394 obstack_blank_fast (ob
, -avail_out
);
1397 return total_out_size
;
1401 compress_debug (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
1403 segment_info_type
*seginfo
= seg_info (sec
);
1407 struct obstack
*ob
= &seginfo
->frchainP
->frch_obstack
;
1408 bfd_size_type uncompressed_size
= (bfd_size_type
) sec
->size
;
1409 bfd_size_type compressed_size
;
1410 const char *section_name
;
1411 char *compressed_name
;
1413 struct z_stream_s
*strm
;
1415 flagword flags
= bfd_get_section_flags (abfd
, sec
);
1419 || (flags
& (SEC_ALLOC
| SEC_HAS_CONTENTS
)) == SEC_ALLOC
)
1422 section_name
= bfd_get_section_name (stdoutput
, sec
);
1423 if (strncmp (section_name
, ".debug_", 7) != 0)
1426 strm
= compress_init ();
1430 /* Create a new frag to contain the "ZLIB" header. */
1431 first_newf
= frag_alloc (ob
);
1432 if (obstack_room (ob
) < 12)
1433 first_newf
= frag_alloc (ob
);
1434 if (obstack_room (ob
) < 12)
1435 as_fatal (_("can't extend frag %u chars"), 12);
1436 last_newf
= first_newf
;
1437 obstack_blank_fast (ob
, 12);
1438 last_newf
->fr_type
= rs_fill
;
1439 last_newf
->fr_fix
= 12;
1440 header
= last_newf
->fr_literal
;
1441 compressed_size
= 12;
1443 /* Stream the frags through the compression engine, adding new frags
1444 as necessary to accomodate the compressed output. */
1445 for (f
= seginfo
->frchainP
->frch_root
;
1454 gas_assert (f
->fr_type
== rs_fill
);
1457 out_size
= compress_frag (strm
, f
->fr_literal
, f
->fr_fix
,
1461 compressed_size
+= out_size
;
1463 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1464 fill_size
= f
->fr_var
;
1465 count
= f
->fr_offset
;
1466 gas_assert (count
>= 0);
1467 if (fill_size
&& count
)
1471 out_size
= compress_frag (strm
, fill_literal
, (int) fill_size
,
1475 compressed_size
+= out_size
;
1480 /* Flush the compression state. */
1487 /* Reserve all the space available in the current chunk.
1488 If none is available, start a new frag. */
1489 avail_out
= obstack_room (ob
);
1494 obstack_finish (ob
);
1495 newf
= frag_alloc (ob
);
1496 newf
->fr_type
= rs_fill
;
1497 last_newf
->fr_next
= newf
;
1499 avail_out
= obstack_room (ob
);
1502 as_fatal (_("can't extend frag"));
1503 next_out
= obstack_next_free (ob
);
1504 obstack_blank_fast (ob
, avail_out
);
1505 x
= compress_finish (strm
, &next_out
, &avail_out
, &out_size
);
1509 last_newf
->fr_fix
+= out_size
;
1510 compressed_size
+= out_size
;
1512 /* Return unused space. */
1514 obstack_blank_fast (ob
, -avail_out
);
1520 /* PR binutils/18087: If compression didn't make the section smaller,
1521 just keep it uncompressed. */
1522 if (compressed_size
> uncompressed_size
)
1525 memcpy (header
, "ZLIB", 4);
1526 header
[11] = uncompressed_size
; uncompressed_size
>>= 8;
1527 header
[10] = uncompressed_size
; uncompressed_size
>>= 8;
1528 header
[9] = uncompressed_size
; uncompressed_size
>>= 8;
1529 header
[8] = uncompressed_size
; uncompressed_size
>>= 8;
1530 header
[7] = uncompressed_size
; uncompressed_size
>>= 8;
1531 header
[6] = uncompressed_size
; uncompressed_size
>>= 8;
1532 header
[5] = uncompressed_size
; uncompressed_size
>>= 8;
1533 header
[4] = uncompressed_size
;
1535 /* Replace the uncompressed frag list with the compressed frag list. */
1536 seginfo
->frchainP
->frch_root
= first_newf
;
1537 seginfo
->frchainP
->frch_last
= last_newf
;
1539 /* Update the section size and its name. */
1540 x
= bfd_set_section_size (abfd
, sec
, compressed_size
);
1542 compressed_name
= (char *) xmalloc (strlen (section_name
) + 2);
1543 compressed_name
[0] = '.';
1544 compressed_name
[1] = 'z';
1545 strcpy (compressed_name
+ 2, section_name
+ 1);
1546 bfd_section_name (stdoutput
, sec
) = compressed_name
;
1550 write_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
1552 void *xxx ATTRIBUTE_UNUSED
)
1554 segment_info_type
*seginfo
= seg_info (sec
);
1555 addressT offset
= 0;
1558 /* Write out the frags. */
1560 || !(bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1563 for (f
= seginfo
->frchainP
->frch_root
;
1572 gas_assert (f
->fr_type
== rs_fill
);
1575 x
= bfd_set_section_contents (stdoutput
, sec
,
1576 f
->fr_literal
, (file_ptr
) offset
,
1577 (bfd_size_type
) f
->fr_fix
);
1579 as_fatal (_("can't write %s: %s"), stdoutput
->filename
,
1580 bfd_errmsg (bfd_get_error ()));
1581 offset
+= f
->fr_fix
;
1583 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1584 fill_size
= f
->fr_var
;
1585 count
= f
->fr_offset
;
1586 gas_assert (count
>= 0);
1587 if (fill_size
&& count
)
1590 if (fill_size
> sizeof (buf
))
1592 /* Do it the old way. Can this ever happen? */
1595 x
= bfd_set_section_contents (stdoutput
, sec
,
1598 (bfd_size_type
) fill_size
);
1600 as_fatal (_("can't write %s: %s"), stdoutput
->filename
,
1601 bfd_errmsg (bfd_get_error ()));
1602 offset
+= fill_size
;
1607 /* Build a buffer full of fill objects and output it as
1608 often as necessary. This saves on the overhead of
1609 potentially lots of bfd_set_section_contents calls. */
1613 n_per_buf
= sizeof (buf
);
1614 memset (buf
, *fill_literal
, n_per_buf
);
1619 n_per_buf
= sizeof (buf
) / fill_size
;
1620 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1621 memcpy (bufp
, fill_literal
, fill_size
);
1623 for (; count
> 0; count
-= n_per_buf
)
1625 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1626 x
= bfd_set_section_contents
1627 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1628 (bfd_size_type
) n_per_buf
* fill_size
);
1630 as_fatal (_("cannot write to output file '%s': %s"),
1631 stdoutput
->filename
,
1632 bfd_errmsg (bfd_get_error ()));
1633 offset
+= n_per_buf
* fill_size
;
1641 merge_data_into_text (void)
1643 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1644 seg_info (data_section
)->frchainP
->frch_root
;
1645 seg_info (text_section
)->frchainP
->frch_last
=
1646 seg_info (data_section
)->frchainP
->frch_last
;
1647 seg_info (data_section
)->frchainP
= 0;
1658 /* Count symbols. We can't rely on a count made by the loop in
1659 write_object_file, because *_frob_file may add a new symbol or
1662 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1668 bfd_size_type amt
= (bfd_size_type
) nsyms
* sizeof (asymbol
*);
1670 asympp
= (asymbol
**) bfd_alloc (stdoutput
, amt
);
1671 symp
= symbol_rootP
;
1672 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1674 asympp
[i
] = symbol_get_bfdsym (symp
);
1675 if (asympp
[i
]->flags
!= BSF_SECTION_SYM
1676 || !(bfd_is_const_section (asympp
[i
]->section
)
1677 && asympp
[i
]->section
->symbol
== asympp
[i
]))
1678 asympp
[i
]->flags
|= BSF_KEEP
;
1679 symbol_mark_written (symp
);
1684 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1685 gas_assert (result
);
1686 symbol_table_frozen
= 1;
1689 /* Finish the subsegments. After every sub-segment, we fake an
1690 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1691 ".fill 0" because that is the kind of frag that requires least
1692 thought. ".align" frags like to have a following frag since that
1693 makes calculating their intended length trivial. */
1695 #ifndef SUB_SEGMENT_ALIGN
1697 /* The last subsegment gets an alignment corresponding to the alignment
1698 of the section. This allows proper nop-filling at the end of
1699 code-bearing sections. */
1700 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1701 (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
1702 ? get_recorded_alignment (SEG) \
1705 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1710 subsegs_finish_section (asection
*s
)
1712 struct frchain
*frchainP
;
1713 segment_info_type
*seginfo
= seg_info (s
);
1717 for (frchainP
= seginfo
->frchainP
;
1719 frchainP
= frchainP
->frch_next
)
1723 subseg_set (s
, frchainP
->frch_subseg
);
1725 /* This now gets called even if we had errors. In that case,
1726 any alignment is meaningless, and, moreover, will look weird
1727 if we are generating a listing. */
1730 alignment
= SUB_SEGMENT_ALIGN (now_seg
, frchainP
);
1731 if ((bfd_get_section_flags (now_seg
->owner
, now_seg
) & SEC_MERGE
)
1732 && now_seg
->entsize
)
1734 unsigned int entsize
= now_seg
->entsize
;
1737 while ((entsize
& 1) == 0)
1743 if (entalign
> alignment
)
1744 alignment
= entalign
;
1748 if (subseg_text_p (now_seg
))
1749 frag_align_code (alignment
, 0);
1751 frag_align (alignment
, 0, 0);
1753 /* frag_align will have left a new frag.
1754 Use this last frag for an empty ".fill".
1756 For this segment ...
1757 Create a last frag. Do not leave a "being filled in frag". */
1758 frag_wane (frag_now
);
1759 frag_now
->fr_fix
= 0;
1760 know (frag_now
->fr_next
== NULL
);
1765 subsegs_finish (void)
1769 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
1770 subsegs_finish_section (s
);
1775 create_obj_attrs_section (void)
1782 size
= bfd_elf_obj_attr_size (stdoutput
);
1785 name
= get_elf_backend_data (stdoutput
)->obj_attrs_section
;
1787 name
= ".gnu.attributes";
1788 s
= subseg_new (name
, 0);
1789 elf_section_type (s
)
1790 = get_elf_backend_data (stdoutput
)->obj_attrs_section_type
;
1791 bfd_set_section_flags (stdoutput
, s
, SEC_READONLY
| SEC_DATA
);
1793 p
= frag_more (size
);
1794 bfd_elf_set_obj_attr_contents (stdoutput
, (bfd_byte
*)p
, size
);
1796 subsegs_finish_section (s
);
1797 relax_segment (seg_info (s
)->frchainP
->frch_root
, s
, 0);
1798 size_seg (stdoutput
, s
, NULL
);
1803 /* Write the object file. */
1806 write_object_file (void)
1808 struct relax_seg_info rsi
;
1809 #ifndef WORKING_DOT_WORD
1810 fragS
*fragP
; /* Track along all frags. */
1815 #ifdef md_pre_output_hook
1819 #ifdef md_pre_relax_hook
1823 /* From now on, we don't care about sub-segments. Build one frag chain
1824 for each segment. Linked thru fr_next. */
1826 /* Remove the sections created by gas for its own purposes. */
1830 bfd_section_list_remove (stdoutput
, reg_section
);
1831 bfd_section_list_remove (stdoutput
, expr_section
);
1832 stdoutput
->section_count
-= 2;
1834 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1837 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1839 /* We have two segments. If user gave -R flag, then we must put the
1840 data frags into the text segment. Do this before relaxing so
1841 we know to take advantage of -R and make shorter addresses. */
1842 if (flag_readonly_data_in_text
)
1844 merge_data_into_text ();
1850 #ifndef WORKING_DOT_WORD
1851 /* We need to reset the markers in the broken word list and
1852 associated frags between calls to relax_segment (via
1853 relax_seg). Since the broken word list is global, we do it
1854 once per round, rather than locally in relax_segment for each
1856 struct broken_word
*brokp
;
1858 for (brokp
= broken_words
;
1859 brokp
!= (struct broken_word
*) NULL
;
1860 brokp
= brokp
->next_broken_word
)
1864 if (brokp
->dispfrag
!= (fragS
*) NULL
1865 && brokp
->dispfrag
->fr_type
== rs_broken_word
)
1866 brokp
->dispfrag
->fr_subtype
= 0;
1871 bfd_map_over_sections (stdoutput
, relax_seg
, &rsi
);
1877 /* Note - Most ports will use the default value of
1878 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1879 local symbols to be resolved, removing their frag information.
1880 Some ports however, will not have finished relaxing all of
1881 their frags and will still need the local symbol frag
1882 information. These ports can set
1883 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1884 finalize_syms
= TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
;
1886 bfd_map_over_sections (stdoutput
, size_seg
, (char *) 0);
1888 /* Relaxation has completed. Freeze all syms. */
1891 #ifdef md_post_relax_hook
1897 create_obj_attrs_section ();
1900 #ifndef WORKING_DOT_WORD
1902 struct broken_word
*lie
;
1903 struct broken_word
**prevP
;
1905 prevP
= &broken_words
;
1906 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1911 subseg_change (lie
->seg
, lie
->subseg
);
1912 exp
.X_op
= O_subtract
;
1913 exp
.X_add_symbol
= lie
->add
;
1914 exp
.X_op_symbol
= lie
->sub
;
1915 exp
.X_add_number
= lie
->addnum
;
1916 #ifdef TC_CONS_FIX_NEW
1917 TC_CONS_FIX_NEW (lie
->frag
,
1918 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1919 2, &exp
, TC_PARSE_CONS_RETURN_NONE
);
1921 fix_new_exp (lie
->frag
,
1922 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1923 2, &exp
, 0, BFD_RELOC_16
);
1925 *prevP
= lie
->next_broken_word
;
1928 prevP
= &(lie
->next_broken_word
);
1930 for (lie
= broken_words
; lie
;)
1932 struct broken_word
*untruth
;
1934 addressT table_addr
;
1935 addressT from_addr
, to_addr
;
1938 subseg_change (lie
->seg
, lie
->subseg
);
1939 fragP
= lie
->dispfrag
;
1941 /* Find out how many broken_words go here. */
1944 untruth
&& untruth
->dispfrag
== fragP
;
1945 untruth
= untruth
->next_broken_word
)
1946 if (untruth
->added
== 1)
1949 table_ptr
= lie
->dispfrag
->fr_opcode
;
1950 table_addr
= (lie
->dispfrag
->fr_address
1951 + (table_ptr
- lie
->dispfrag
->fr_literal
));
1952 /* Create the jump around the long jumps. This is a short
1953 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1954 from_addr
= table_addr
;
1955 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1956 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1958 table_ptr
+= md_short_jump_size
;
1959 table_addr
+= md_short_jump_size
;
1962 lie
&& lie
->dispfrag
== fragP
;
1963 m
++, lie
= lie
->next_broken_word
)
1965 if (lie
->added
== 2)
1967 /* Patch the jump table. */
1968 for (untruth
= (struct broken_word
*) (fragP
->fr_symbol
);
1969 untruth
&& untruth
->dispfrag
== fragP
;
1970 untruth
= untruth
->next_broken_word
)
1972 if (untruth
->use_jump
== lie
)
1974 /* This is the offset from ??? to table_ptr+0.
1975 The target is the same for all users of this
1976 md_long_jump, but the "sub" bases (and hence the
1977 offsets) may be different. */
1978 addressT to_word
= table_addr
- S_GET_VALUE (untruth
->sub
);
1979 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1980 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word
, untruth
);
1982 md_number_to_chars (untruth
->word_goes_here
, to_word
, 2);
1986 /* Install the long jump. */
1987 /* This is a long jump from table_ptr+0 to the final target. */
1988 from_addr
= table_addr
;
1989 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1990 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1992 table_ptr
+= md_long_jump_size
;
1993 table_addr
+= md_long_jump_size
;
1997 #endif /* not WORKING_DOT_WORD */
1999 /* Resolve symbol values. This needs to be done before processing
2005 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2006 resolve_symbol_value (symp
);
2008 resolve_local_symbol_values ();
2009 resolve_reloc_expr_symbols ();
2013 #ifdef tc_frob_file_before_adjust
2014 tc_frob_file_before_adjust ();
2016 #ifdef obj_frob_file_before_adjust
2017 obj_frob_file_before_adjust ();
2020 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
2022 #ifdef tc_frob_file_before_fix
2023 tc_frob_file_before_fix ();
2025 #ifdef obj_frob_file_before_fix
2026 obj_frob_file_before_fix ();
2029 bfd_map_over_sections (stdoutput
, fix_segment
, (char *) 0);
2031 /* Set up symbol table, and write it out. */
2035 bfd_boolean skip_next_symbol
= FALSE
;
2037 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2042 if (skip_next_symbol
)
2044 /* Don't do anything besides moving the value of the
2045 symbol from the GAS value-field to the BFD value-field. */
2046 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
2047 skip_next_symbol
= FALSE
;
2051 if (symbol_mri_common_p (symp
))
2053 if (S_IS_EXTERNAL (symp
))
2054 as_bad (_("%s: global symbols not supported in common sections"),
2056 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2060 name
= S_GET_NAME (symp
);
2064 decode_local_label_name ((char *) S_GET_NAME (symp
));
2065 /* They only differ if `name' is a fb or dollar local
2067 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
2068 as_bad (_("local label `%s' is not defined"), name2
);
2071 /* Do it again, because adjust_reloc_syms might introduce
2072 more symbols. They'll probably only be section symbols,
2073 but they'll still need to have the values computed. */
2074 resolve_symbol_value (symp
);
2076 /* Skip symbols which were equated to undefined or common
2078 if (symbol_equated_reloc_p (symp
)
2079 || S_IS_WEAKREFR (symp
))
2081 const char *sname
= S_GET_NAME (symp
);
2083 if (S_IS_COMMON (symp
)
2084 && !TC_FAKE_LABEL (sname
)
2085 && !S_IS_WEAKREFR (symp
)
2086 && (!S_IS_EXTERNAL (symp
) || S_IS_LOCAL (symp
)))
2088 expressionS
*e
= symbol_get_value_expression (symp
);
2090 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
2091 sname
, S_GET_NAME (e
->X_add_symbol
));
2093 if (S_GET_SEGMENT (symp
) == reg_section
)
2095 /* Report error only if we know the symbol name. */
2096 if (S_GET_NAME (symp
) != reg_section
->name
)
2097 as_bad (_("can't make global register symbol `%s'"),
2100 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2104 #ifdef obj_frob_symbol
2105 obj_frob_symbol (symp
, punt
);
2107 #ifdef tc_frob_symbol
2108 if (! punt
|| symbol_used_in_reloc_p (symp
))
2109 tc_frob_symbol (symp
, punt
);
2112 /* If we don't want to keep this symbol, splice it out of
2113 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2114 want section symbols. Otherwise, we skip local symbols
2115 and symbols that the frob_symbol macros told us to punt,
2116 but we keep such symbols if they are used in relocs. */
2117 if (symp
== abs_section_sym
2118 || (! EMIT_SECTION_SYMBOLS
2119 && symbol_section_p (symp
))
2120 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2121 opposites. Sometimes the former checks flags and the
2122 latter examines the name... */
2123 || (!S_IS_EXTERNAL (symp
)
2124 && (punt
|| S_IS_LOCAL (symp
) ||
2125 (S_IS_WEAKREFD (symp
) && ! symbol_used_p (symp
)))
2126 && ! symbol_used_in_reloc_p (symp
)))
2128 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2130 /* After symbol_remove, symbol_next(symp) still returns
2131 the one that came after it in the chain. So we don't
2132 need to do any extra cleanup work here. */
2136 /* Make sure we really got a value for the symbol. */
2137 if (! symbol_resolved_p (symp
))
2139 as_bad (_("can't resolve value for symbol `%s'"),
2141 symbol_mark_resolved (symp
);
2144 /* Set the value into the BFD symbol. Up til now the value
2145 has only been kept in the gas symbolS struct. */
2146 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
2148 /* A warning construct is a warning symbol followed by the
2149 symbol warned about. Don't let anything object-format or
2150 target-specific muck with it; it's ready for output. */
2151 if (symbol_get_bfdsym (symp
)->flags
& BSF_WARNING
)
2152 skip_next_symbol
= TRUE
;
2158 /* Now do any format-specific adjustments to the symbol table, such
2159 as adding file symbols. */
2160 #ifdef tc_adjust_symtab
2161 tc_adjust_symtab ();
2163 #ifdef obj_adjust_symtab
2164 obj_adjust_symtab ();
2167 /* Stop if there is an error. */
2171 /* Now that all the sizes are known, and contents correct, we can
2172 start writing to the file. */
2175 /* If *_frob_file changes the symbol value at this point, it is
2176 responsible for moving the changed value into symp->bsym->value
2177 as well. Hopefully all symbol value changing can be done in
2182 #ifdef obj_frob_file
2185 #ifdef obj_coff_generate_pdata
2186 obj_coff_generate_pdata ();
2188 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
2190 #ifdef tc_frob_file_after_relocs
2191 tc_frob_file_after_relocs ();
2193 #ifdef obj_frob_file_after_relocs
2194 obj_frob_file_after_relocs ();
2197 /* Once all relocations have been written, we can compress the
2198 contents of the debug sections. This needs to be done before
2199 we start writing any sections, because it will affect the file
2200 layout, which is fixed once we start writing contents. */
2201 if (flag_compress_debug
)
2202 bfd_map_over_sections (stdoutput
, compress_debug
, (char *) 0);
2204 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
2207 #ifdef TC_GENERIC_RELAX_TABLE
2208 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2211 relax_frag (segT segment
, fragS
*fragP
, long stretch
)
2213 const relax_typeS
*this_type
;
2214 const relax_typeS
*start_type
;
2215 relax_substateT next_state
;
2216 relax_substateT this_state
;
2222 const relax_typeS
*table
;
2224 target
= fragP
->fr_offset
;
2225 address
= fragP
->fr_address
;
2226 table
= TC_GENERIC_RELAX_TABLE
;
2227 this_state
= fragP
->fr_subtype
;
2228 start_type
= this_type
= table
+ this_state
;
2229 symbolP
= fragP
->fr_symbol
;
2235 sym_frag
= symbol_get_frag (symbolP
);
2237 #ifndef DIFF_EXPR_OK
2238 know (sym_frag
!= NULL
);
2240 know (S_GET_SEGMENT (symbolP
) != absolute_section
2241 || sym_frag
== &zero_address_frag
);
2242 target
+= S_GET_VALUE (symbolP
);
2244 /* If SYM_FRAG has yet to be reached on this pass, assume it
2245 will move by STRETCH just as we did, unless there is an
2246 alignment frag between here and SYM_FRAG. An alignment may
2247 well absorb any STRETCH, and we don't want to choose a larger
2248 branch insn by overestimating the needed reach of this
2249 branch. It isn't critical to calculate TARGET exactly; We
2250 know we'll be doing another pass if STRETCH is non-zero. */
2253 && sym_frag
->relax_marker
!= fragP
->relax_marker
2254 && S_GET_SEGMENT (symbolP
) == segment
)
2257 || sym_frag
->region
== fragP
->region
)
2259 /* If we get here we know we have a forward branch. This
2260 relax pass may have stretched previous instructions so
2261 far that omitting STRETCH would make the branch
2262 negative. Don't allow this in case the negative reach is
2263 large enough to require a larger branch instruction. */
2264 else if (target
< address
)
2265 target
= fragP
->fr_next
->fr_address
+ stretch
;
2269 aim
= target
- address
- fragP
->fr_fix
;
2270 #ifdef TC_PCREL_ADJUST
2271 /* Currently only the ns32k family needs this. */
2272 aim
+= TC_PCREL_ADJUST (fragP
);
2275 #ifdef md_prepare_relax_scan
2276 /* Formerly called M68K_AIM_KLUDGE. */
2277 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2282 /* Look backwards. */
2283 for (next_state
= this_type
->rlx_more
; next_state
;)
2284 if (aim
>= this_type
->rlx_backward
)
2288 /* Grow to next state. */
2289 this_state
= next_state
;
2290 this_type
= table
+ this_state
;
2291 next_state
= this_type
->rlx_more
;
2296 /* Look forwards. */
2297 for (next_state
= this_type
->rlx_more
; next_state
;)
2298 if (aim
<= this_type
->rlx_forward
)
2302 /* Grow to next state. */
2303 this_state
= next_state
;
2304 this_type
= table
+ this_state
;
2305 next_state
= this_type
->rlx_more
;
2309 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2311 fragP
->fr_subtype
= this_state
;
2315 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2317 /* Relax_align. Advance location counter to next address that has 'alignment'
2318 lowest order bits all 0s, return size of adjustment made. */
2319 static relax_addressT
2320 relax_align (relax_addressT address
, /* Address now. */
2321 int alignment
/* Alignment (binary). */)
2323 relax_addressT mask
;
2324 relax_addressT new_address
;
2326 mask
= ~((relax_addressT
) ~0 << alignment
);
2327 new_address
= (address
+ mask
) & (~mask
);
2328 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2330 /* We must provide lots of padding, so the linker can discard it
2331 when needed. The linker will not add extra space, ever. */
2332 new_address
+= (1 << alignment
);
2334 return (new_address
- address
);
2337 /* Now we have a segment, not a crowd of sub-segments, we can make
2342 After this, all frags in this segment have addresses that are correct
2343 within the segment. Since segments live in different file addresses,
2344 these frag addresses may not be the same as final object-file
2348 relax_segment (struct frag
*segment_frag_root
, segT segment
, int pass
)
2350 unsigned long frag_count
;
2352 relax_addressT address
;
2356 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2357 subseg_change (segment
, 0);
2359 /* For each frag in segment: count and store (a 1st guess of)
2363 for (frag_count
= 0, fragP
= segment_frag_root
;
2365 fragP
= fragP
->fr_next
, frag_count
++)
2367 fragP
->region
= region
;
2368 fragP
->relax_marker
= 0;
2369 fragP
->fr_address
= address
;
2370 address
+= fragP
->fr_fix
;
2372 switch (fragP
->fr_type
)
2375 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2382 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2384 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2387 if (offset
% fragP
->fr_var
!= 0)
2389 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2390 _("alignment padding (%lu bytes) not a multiple of %ld"),
2391 (unsigned long) offset
, (long) fragP
->fr_var
);
2392 offset
-= (offset
% fragP
->fr_var
);
2401 /* Assume .org is nugatory. It will grow with 1st relax. */
2408 case rs_machine_dependent
:
2409 /* If fr_symbol is an expression, this call to
2410 resolve_symbol_value sets up the correct segment, which will
2411 likely be needed in md_estimate_size_before_relax. */
2412 if (fragP
->fr_symbol
)
2413 resolve_symbol_value (fragP
->fr_symbol
);
2415 address
+= md_estimate_size_before_relax (fragP
, segment
);
2418 #ifndef WORKING_DOT_WORD
2419 /* Broken words don't concern us yet. */
2420 case rs_broken_word
:
2425 /* Initial guess is always 1; doing otherwise can result in
2426 stable solutions that are larger than the minimum. */
2427 address
+= fragP
->fr_offset
= 1;
2431 address
+= eh_frame_estimate_size_before_relax (fragP
);
2435 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
2439 BAD_CASE (fragP
->fr_type
);
2446 unsigned long max_iterations
;
2448 /* Cumulative address adjustment. */
2451 /* Have we made any adjustment this pass? We can't just test
2452 stretch because one piece of code may have grown and another
2456 /* Most horrible, but gcc may give us some exception data that
2457 is impossible to assemble, of the form
2461 .uleb128 end - start
2467 If the leb128 is two bytes in size, then end-start is 128*128,
2468 which requires a three byte leb128. If the leb128 is three
2469 bytes in size, then end-start is 128*128-1, which requires a
2470 two byte leb128. We work around this dilemma by inserting
2471 an extra 4 bytes of alignment just after the .align. This
2472 works because the data after the align is accessed relative to
2475 This counter is used in a tiny state machine to detect
2476 whether a leb128 followed by an align is impossible to
2478 int rs_leb128_fudge
= 0;
2480 /* We want to prevent going into an infinite loop where one frag grows
2481 depending upon the location of a symbol which is in turn moved by
2482 the growing frag. eg:
2488 So we dictate that this algorithm can be at most O2. */
2489 max_iterations
= frag_count
* frag_count
;
2490 /* Check for overflow. */
2491 if (max_iterations
< frag_count
)
2492 max_iterations
= frag_count
;
2500 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2503 addressT was_address
;
2507 fragP
->relax_marker
^= 1;
2508 was_address
= fragP
->fr_address
;
2509 address
= fragP
->fr_address
+= stretch
;
2510 symbolP
= fragP
->fr_symbol
;
2511 offset
= fragP
->fr_offset
;
2513 switch (fragP
->fr_type
)
2515 case rs_fill
: /* .fill never relaxes. */
2519 #ifndef WORKING_DOT_WORD
2520 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2521 for it I do not want to write it. I do not want to have
2522 anything to do with it. This is not the proper way to
2523 implement this misfeature. */
2524 case rs_broken_word
:
2526 struct broken_word
*lie
;
2527 struct broken_word
*untruth
;
2529 /* Yes this is ugly (storing the broken_word pointer
2530 in the symbol slot). Still, this whole chunk of
2531 code is ugly, and I don't feel like doing anything
2532 about it. Think of it as stubbornness in action. */
2534 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2535 lie
&& lie
->dispfrag
== fragP
;
2536 lie
= lie
->next_broken_word
)
2542 offset
= (S_GET_VALUE (lie
->add
)
2544 - S_GET_VALUE (lie
->sub
));
2545 if (offset
<= -32768 || offset
>= 32767)
2547 if (flag_warn_displacement
)
2550 sprint_value (buf
, (addressT
) lie
->addnum
);
2551 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2552 _(".word %s-%s+%s didn't fit"),
2553 S_GET_NAME (lie
->add
),
2554 S_GET_NAME (lie
->sub
),
2557 if (fragP
->fr_subtype
== 0)
2559 fragP
->fr_subtype
++;
2560 growth
+= md_short_jump_size
;
2563 /* Redirect *all* words of this table with the same
2564 target, lest we have to handle the case where the
2565 same target but with a offset that fits on this
2566 round overflows at the next relaxation round. */
2567 for (untruth
= (struct broken_word
*) (fragP
->fr_symbol
);
2568 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2569 untruth
= untruth
->next_broken_word
)
2570 if ((symbol_get_frag (untruth
->add
)
2571 == symbol_get_frag (lie
->add
))
2572 && (S_GET_VALUE (untruth
->add
)
2573 == S_GET_VALUE (lie
->add
)))
2576 untruth
->use_jump
= lie
;
2580 growth
+= md_long_jump_size
;
2585 } /* case rs_broken_word */
2591 addressT oldoff
, newoff
;
2593 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2595 newoff
= relax_align (address
+ fragP
->fr_fix
,
2598 if (fragP
->fr_subtype
!= 0)
2600 if (oldoff
> fragP
->fr_subtype
)
2602 if (newoff
> fragP
->fr_subtype
)
2606 growth
= newoff
- oldoff
;
2608 /* If this align happens to follow a leb128 and
2609 we have determined that the leb128 is bouncing
2610 in size, then break the cycle by inserting an
2613 && (rs_leb128_fudge
& 16) != 0
2614 && (rs_leb128_fudge
& 15) >= 2)
2616 segment_info_type
*seginfo
= seg_info (segment
);
2617 struct obstack
*ob
= &seginfo
->frchainP
->frch_obstack
;
2620 newf
= frag_alloc (ob
);
2621 obstack_blank_fast (ob
, fragP
->fr_var
);
2622 obstack_finish (ob
);
2623 memcpy (newf
, fragP
, SIZEOF_STRUCT_FRAG
);
2624 memcpy (newf
->fr_literal
,
2625 fragP
->fr_literal
+ fragP
->fr_fix
,
2627 newf
->fr_type
= rs_fill
;
2628 newf
->fr_address
= address
+ fragP
->fr_fix
+ newoff
;
2630 newf
->fr_offset
= (((offsetT
) 1 << fragP
->fr_offset
)
2632 if (newf
->fr_offset
* newf
->fr_var
2633 != (offsetT
) 1 << fragP
->fr_offset
)
2635 newf
->fr_offset
= (offsetT
) 1 << fragP
->fr_offset
;
2638 /* Include size of new frag in GROWTH. */
2639 growth
+= newf
->fr_offset
* newf
->fr_var
;
2640 /* Adjust the new frag address for the amount
2641 we'll add when we process the new frag. */
2642 newf
->fr_address
-= stretch
+ growth
;
2643 newf
->relax_marker
^= 1;
2644 fragP
->fr_next
= newf
;
2646 as_warn (_("padding added"));
2654 addressT target
= offset
;
2659 /* Convert from an actual address to an octet offset
2660 into the section. Here it is assumed that the
2661 section's VMA is zero, and can omit subtracting it
2662 from the symbol's value to get the address offset. */
2663 know (S_GET_SEGMENT (symbolP
)->vma
== 0);
2664 target
+= S_GET_VALUE (symbolP
) * OCTETS_PER_BYTE
;
2667 know (fragP
->fr_next
);
2668 after
= fragP
->fr_next
->fr_address
+ stretch
;
2669 growth
= target
- after
;
2674 /* Don't error on first few frag relax passes.
2675 The symbol might be an expression involving
2676 symbol values from other sections. If those
2677 sections have not yet been processed their
2678 frags will all have zero addresses, so we
2679 will calculate incorrect values for them. The
2680 number of passes we allow before giving an
2681 error is somewhat arbitrary. It should be at
2682 least one, with larger values requiring
2683 increasingly contrived dependencies between
2684 frags to trigger a false error. */
2687 /* Force another pass. */
2692 /* Growth may be negative, but variable part of frag
2693 cannot have fewer than 0 chars. That is, we can't
2695 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2696 _("attempt to move .org backwards"));
2698 /* We've issued an error message. Change the
2699 frag to avoid cascading errors. */
2700 fragP
->fr_type
= rs_align
;
2701 fragP
->fr_subtype
= 0;
2702 fragP
->fr_offset
= 0;
2703 fragP
->fr_fix
= after
- address
;
2714 amount
= S_GET_VALUE (symbolP
);
2715 if (S_GET_SEGMENT (symbolP
) != absolute_section
2716 || S_IS_COMMON (symbolP
)
2717 || ! S_IS_DEFINED (symbolP
))
2719 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2720 _(".space specifies non-absolute value"));
2721 /* Prevent repeat of this error message. */
2722 fragP
->fr_symbol
= 0;
2724 else if (amount
< 0)
2726 /* Don't error on first few frag relax passes.
2727 See rs_org comment for a longer explanation. */
2734 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2735 _(".space or .fill with negative value, ignored"));
2736 fragP
->fr_symbol
= 0;
2739 growth
= (was_address
+ fragP
->fr_fix
+ amount
2740 - fragP
->fr_next
->fr_address
);
2744 case rs_machine_dependent
:
2745 #ifdef md_relax_frag
2746 growth
= md_relax_frag (segment
, fragP
, stretch
);
2748 #ifdef TC_GENERIC_RELAX_TABLE
2749 /* The default way to relax a frag is to look through
2750 TC_GENERIC_RELAX_TABLE. */
2751 growth
= relax_frag (segment
, fragP
, stretch
);
2752 #endif /* TC_GENERIC_RELAX_TABLE */
2761 value
= resolve_symbol_value (fragP
->fr_symbol
);
2762 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2763 growth
= size
- fragP
->fr_offset
;
2764 fragP
->fr_offset
= size
;
2769 growth
= eh_frame_relax_frag (fragP
);
2773 growth
= dwarf2dbg_relax_frag (fragP
);
2777 BAD_CASE (fragP
->fr_type
);
2784 if (fragP
->fr_type
== rs_leb128
)
2785 rs_leb128_fudge
+= 16;
2786 else if (fragP
->fr_type
== rs_align
2787 && (rs_leb128_fudge
& 16) != 0
2789 rs_leb128_fudge
+= 16;
2791 rs_leb128_fudge
= 0;
2796 && (rs_leb128_fudge
& 16) == 0
2797 && (rs_leb128_fudge
& -16) != 0)
2798 rs_leb128_fudge
+= 1;
2800 rs_leb128_fudge
= 0;
2802 /* Until nothing further to relax. */
2803 while (stretched
&& -- max_iterations
);
2806 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2807 segment_name (segment
));
2810 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2811 if (fragP
->last_fr_address
!= fragP
->fr_address
)
2813 fragP
->last_fr_address
= fragP
->fr_address
;
2820 number_to_chars_bigendian (char *buf
, valueT val
, int n
)
2826 buf
[n
] = val
& 0xff;
2832 number_to_chars_littleendian (char *buf
, valueT val
, int n
)
2838 *buf
++ = val
& 0xff;
2844 write_print_statistics (FILE *file
)
2846 fprintf (file
, "fixups: %d\n", n_fixups
);
2849 /* For debugging. */
2850 extern int indent_level
;
2853 print_fixup (fixS
*fixp
)
2856 fprintf (stderr
, "fix ");
2857 fprintf_vma (stderr
, (bfd_vma
)((bfd_hostptr_t
) fixp
));
2858 fprintf (stderr
, " %s:%d",fixp
->fx_file
, fixp
->fx_line
);
2860 fprintf (stderr
, " pcrel");
2861 if (fixp
->fx_pcrel_adjust
)
2862 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2863 if (fixp
->fx_im_disp
)
2866 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2868 fprintf (stderr
, " im_disp");
2872 fprintf (stderr
, " tcbit");
2874 fprintf (stderr
, " done");
2875 fprintf (stderr
, "\n size=%d frag=", fixp
->fx_size
);
2876 fprintf_vma (stderr
, (bfd_vma
) ((bfd_hostptr_t
) fixp
->fx_frag
));
2877 fprintf (stderr
, " where=%ld offset=%lx addnumber=%lx",
2878 (long) fixp
->fx_where
,
2879 (unsigned long) fixp
->fx_offset
,
2880 (unsigned long) fixp
->fx_addnumber
);
2881 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2885 fprintf (stderr
, "\n +<");
2886 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2887 fprintf (stderr
, ">");
2891 fprintf (stderr
, "\n -<");
2892 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2893 fprintf (stderr
, ">");
2895 fprintf (stderr
, "\n");
2896 #ifdef TC_FIX_DATA_PRINT
2897 TC_FIX_DATA_PRINT (stderr
, fixp
);