1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 /* This thing should be set up to do byteordering correctly. But... */
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
32 #ifndef TC_ADJUST_RELOC_COUNT
33 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
36 #ifndef TC_FORCE_RELOCATION
37 #define TC_FORCE_RELOCATION(FIX) \
38 (generic_force_reloc (FIX))
41 #ifndef TC_FORCE_RELOCATION_ABS
42 #define TC_FORCE_RELOCATION_ABS(FIX) \
43 (TC_FORCE_RELOCATION (FIX))
46 #ifndef TC_FORCE_RELOCATION_LOCAL
47 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
49 || TC_FORCE_RELOCATION (FIX))
52 #ifndef TC_FORCE_RELOCATION_SUB_SAME
53 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
57 #ifndef TC_FORCE_RELOCATION_SUB_ABS
58 #define TC_FORCE_RELOCATION_SUB_ABS(FIX) 0
61 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
63 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 0
65 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 1
69 #ifndef TC_VALIDATE_FIX_SUB
70 #ifdef UNDEFINED_DIFFERENCE_OK
71 /* The PA needs this for PIC code generation. */
72 #define TC_VALIDATE_FIX_SUB(FIX) 1
74 #define TC_VALIDATE_FIX_SUB(FIX) \
75 ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
76 || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
80 #ifndef TC_LINKRELAX_FIXUP
81 #define TC_LINKRELAX_FIXUP(SEG) 1
84 #ifndef MD_APPLY_SYM_VALUE
85 #define MD_APPLY_SYM_VALUE(FIX) 1
88 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
89 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
92 #ifndef MD_PCREL_FROM_SECTION
93 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
97 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
100 /* Used to control final evaluation of expressions. */
101 int finalize_syms
= 0;
103 int symbol_table_frozen
;
105 symbolS
*abs_section_sym
;
107 /* Remember the value of dot when parsing expressions. */
110 void print_fixup (fixS
*);
112 /* We generally attach relocs to frag chains. However, after we have
113 chained these all together into a segment, any relocs we add after
114 that must be attached to a segment. This will include relocs added
115 in md_estimate_size_for_relax, for example. */
116 static int frags_chained
= 0;
120 #define RELOC_ENUM enum bfd_reloc_code_real
122 /* Create a fixS in obstack 'notes'. */
125 fix_new_internal (fragS
*frag
, /* Which frag? */
126 int where
, /* Where in that frag? */
127 int size
, /* 1, 2, or 4 usually. */
128 symbolS
*add_symbol
, /* X_add_symbol. */
129 symbolS
*sub_symbol
, /* X_op_symbol. */
130 offsetT offset
, /* X_add_number. */
131 int pcrel
, /* TRUE if PC-relative relocation. */
132 RELOC_ENUM r_type ATTRIBUTE_UNUSED
/* Relocation type. */)
138 fixP
= obstack_alloc (¬es
, sizeof (fixS
));
140 fixP
->fx_frag
= frag
;
141 fixP
->fx_where
= where
;
142 fixP
->fx_size
= size
;
143 /* We've made fx_size a narrow field; check that it's wide enough. */
144 if (fixP
->fx_size
!= size
)
146 as_bad (_("field fx_size too small to hold %d"), size
);
149 fixP
->fx_addsy
= add_symbol
;
150 fixP
->fx_subsy
= sub_symbol
;
151 fixP
->fx_offset
= offset
;
152 fixP
->fx_dot_value
= dot_value
;
153 fixP
->fx_pcrel
= pcrel
;
154 fixP
->fx_r_type
= r_type
;
155 fixP
->fx_im_disp
= 0;
156 fixP
->fx_pcrel_adjust
= 0;
157 fixP
->fx_bit_fixP
= 0;
158 fixP
->fx_addnumber
= 0;
162 fixP
->fx_no_overflow
= 0;
166 fixP
->fx_cgen
.insn
= NULL
;
167 fixP
->fx_cgen
.opinfo
= 0;
171 TC_INIT_FIX_DATA (fixP
);
174 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
176 /* Usually, we want relocs sorted numerically, but while
177 comparing to older versions of gas that have relocs
178 reverse sorted, it is convenient to have this compile
179 time option. xoxorich. */
182 fixS
**seg_fix_rootP
= (frags_chained
183 ? &seg_info (now_seg
)->fix_root
184 : &frchain_now
->fix_root
);
185 fixS
**seg_fix_tailP
= (frags_chained
186 ? &seg_info (now_seg
)->fix_tail
187 : &frchain_now
->fix_tail
);
189 #ifdef REVERSE_SORT_RELOCS
191 fixP
->fx_next
= *seg_fix_rootP
;
192 *seg_fix_rootP
= fixP
;
194 #else /* REVERSE_SORT_RELOCS */
196 fixP
->fx_next
= NULL
;
199 (*seg_fix_tailP
)->fx_next
= fixP
;
201 *seg_fix_rootP
= fixP
;
202 *seg_fix_tailP
= fixP
;
204 #endif /* REVERSE_SORT_RELOCS */
210 /* Create a fixup relative to a symbol (plus a constant). */
213 fix_new (fragS
*frag
, /* Which frag? */
214 int where
, /* Where in that frag? */
215 int size
, /* 1, 2, or 4 usually. */
216 symbolS
*add_symbol
, /* X_add_symbol. */
217 offsetT offset
, /* X_add_number. */
218 int pcrel
, /* TRUE if PC-relative relocation. */
219 RELOC_ENUM r_type
/* Relocation type. */)
221 return fix_new_internal (frag
, where
, size
, add_symbol
,
222 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
225 /* Create a fixup for an expression. Currently we only support fixups
226 for difference expressions. That is itself more than most object
227 file formats support anyhow. */
230 fix_new_exp (fragS
*frag
, /* Which frag? */
231 int where
, /* Where in that frag? */
232 int size
, /* 1, 2, or 4 usually. */
233 expressionS
*exp
, /* Expression. */
234 int pcrel
, /* TRUE if PC-relative relocation. */
235 RELOC_ENUM r_type
/* Relocation type. */)
247 as_bad (_("register value used as expression"));
251 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
252 the difference expression cannot immediately be reduced. */
254 symbolS
*stmp
= make_expr_symbol (exp
);
256 exp
->X_op
= O_symbol
;
257 exp
->X_op_symbol
= 0;
258 exp
->X_add_symbol
= stmp
;
259 exp
->X_add_number
= 0;
261 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
265 add
= exp
->X_add_symbol
;
266 off
= exp
->X_add_number
;
267 r_type
= BFD_RELOC_RVA
;
271 sub
= exp
->X_add_symbol
;
272 off
= exp
->X_add_number
;
276 sub
= exp
->X_op_symbol
;
279 add
= exp
->X_add_symbol
;
282 off
= exp
->X_add_number
;
286 add
= make_expr_symbol (exp
);
290 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
, r_type
);
293 /* Generic function to determine whether a fixup requires a relocation. */
295 generic_force_reloc (fixS
*fix
)
297 if (fix
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
298 || fix
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
301 if (fix
->fx_addsy
== NULL
)
304 return S_FORCE_RELOC (fix
->fx_addsy
, fix
->fx_subsy
== NULL
);
307 /* Append a string onto another string, bumping the pointer along. */
309 append (char **charPP
, char *fromP
, unsigned long length
)
311 /* Don't trust memcpy() of 0 chars. */
315 memcpy (*charPP
, fromP
, length
);
319 /* This routine records the largest alignment seen for each segment.
320 If the beginning of the segment is aligned on the worst-case
321 boundary, all of the other alignments within it will work. At
322 least one object format really uses this info. */
325 record_alignment (/* Segment to which alignment pertains. */
327 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
328 boundary, 2 => 4-byte boundary, etc.) */
331 if (seg
== absolute_section
)
334 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
335 bfd_set_section_alignment (stdoutput
, seg
, align
);
339 get_recorded_alignment (segT seg
)
341 if (seg
== absolute_section
)
344 return bfd_get_section_alignment (stdoutput
, seg
);
347 /* Reset the section indices after removing the gas created sections. */
350 renumber_sections (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *countparg
)
352 int *countp
= (int *) countparg
;
354 sec
->index
= *countp
;
359 chain_frchains_together_1 (segT section
, struct frchain
*frchp
)
361 fragS dummy
, *prev_frag
= &dummy
;
362 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
364 for (; frchp
; frchp
= frchp
->frch_next
)
366 prev_frag
->fr_next
= frchp
->frch_root
;
367 prev_frag
= frchp
->frch_last
;
368 assert (prev_frag
->fr_type
!= 0);
369 if (frchp
->fix_root
!= (fixS
*) NULL
)
371 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
372 seg_info (section
)->fix_root
= frchp
->fix_root
;
373 prev_fix
->fx_next
= frchp
->fix_root
;
374 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
375 prev_fix
= frchp
->fix_tail
;
378 assert (prev_frag
->fr_type
!= 0);
379 assert (prev_frag
!= &dummy
);
380 prev_frag
->fr_next
= 0;
385 chain_frchains_together (bfd
*abfd ATTRIBUTE_UNUSED
,
387 void *xxx ATTRIBUTE_UNUSED
)
389 segment_info_type
*info
;
391 /* BFD may have introduced its own sections without using
392 subseg_new, so it is possible that seg_info is NULL. */
393 info
= seg_info (section
);
394 if (info
!= (segment_info_type
*) NULL
)
395 info
->frchainP
->frch_last
396 = chain_frchains_together_1 (section
, info
->frchainP
);
398 /* Now that we've chained the frags together, we must add new fixups
399 to the segment, not to the frag chain. */
404 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED
, fragS
*fragP
)
406 switch (fragP
->fr_type
)
414 HANDLE_ALIGN (fragP
);
416 know (fragP
->fr_next
!= NULL
);
417 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
419 - fragP
->fr_fix
) / fragP
->fr_var
;
420 if (fragP
->fr_offset
< 0)
422 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
423 _("attempt to .org/.space backwards? (%ld)"),
424 (long) fragP
->fr_offset
);
425 fragP
->fr_offset
= 0;
427 fragP
->fr_type
= rs_fill
;
435 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
438 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
441 fragP
->fr_fix
+= size
;
442 fragP
->fr_type
= rs_fill
;
444 fragP
->fr_offset
= 0;
445 fragP
->fr_symbol
= NULL
;
450 eh_frame_convert_frag (fragP
);
454 dwarf2dbg_convert_frag (fragP
);
457 case rs_machine_dependent
:
458 md_convert_frag (stdoutput
, sec
, fragP
);
460 assert (fragP
->fr_next
== NULL
461 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
464 /* After md_convert_frag, we make the frag into a ".space 0".
465 md_convert_frag() should set up any fixSs and constants
470 #ifndef WORKING_DOT_WORD
473 struct broken_word
*lie
;
475 if (fragP
->fr_subtype
)
477 fragP
->fr_fix
+= md_short_jump_size
;
478 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
479 lie
&& lie
->dispfrag
== fragP
;
480 lie
= lie
->next_broken_word
)
482 fragP
->fr_fix
+= md_long_jump_size
;
490 BAD_CASE (fragP
->fr_type
);
494 md_frag_check (fragP
);
498 struct relax_seg_info
505 relax_seg (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *xxx
)
507 segment_info_type
*seginfo
= seg_info (sec
);
508 struct relax_seg_info
*info
= (struct relax_seg_info
*) xxx
;
510 if (seginfo
&& seginfo
->frchainP
511 && relax_segment (seginfo
->frchainP
->frch_root
, sec
, info
->pass
))
516 size_seg (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
520 segment_info_type
*seginfo
;
522 valueT size
, newsize
;
524 subseg_change (sec
, 0);
526 seginfo
= seg_info (sec
);
527 if (seginfo
&& seginfo
->frchainP
)
529 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
530 cvt_frag_to_fill (sec
, fragp
);
531 for (fragp
= seginfo
->frchainP
->frch_root
;
533 fragp
= fragp
->fr_next
)
534 /* Walk to last elt. */
536 size
= fragp
->fr_address
+ fragp
->fr_fix
;
541 flags
= bfd_get_section_flags (abfd
, sec
);
543 if (size
> 0 && ! seginfo
->bss
)
544 flags
|= SEC_HAS_CONTENTS
;
546 /* @@ This is just an approximation. */
547 if (seginfo
&& seginfo
->fix_root
)
551 x
= bfd_set_section_flags (abfd
, sec
, flags
);
554 newsize
= md_section_align (sec
, size
);
555 x
= bfd_set_section_size (abfd
, sec
, newsize
);
558 /* If the size had to be rounded up, add some padding in the last
560 assert (newsize
>= size
);
563 fragS
*last
= seginfo
->frchainP
->frch_last
;
564 fragp
= seginfo
->frchainP
->frch_root
;
565 while (fragp
->fr_next
!= last
)
566 fragp
= fragp
->fr_next
;
567 last
->fr_address
= size
;
568 if ((newsize
- size
) % fragp
->fr_var
== 0)
569 fragp
->fr_offset
+= (newsize
- size
) / fragp
->fr_var
;
571 /* If we hit this abort, it's likely due to subsegs_finish not
572 providing sufficient alignment on the last frag, and the
573 machine dependent code using alignment frags with fr_var
578 #ifdef tc_frob_section
579 tc_frob_section (sec
);
581 #ifdef obj_frob_section
582 obj_frob_section (sec
);
588 dump_section_relocs (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, FILE *stream
)
590 segment_info_type
*seginfo
= seg_info (sec
);
591 fixS
*fixp
= seginfo
->fix_root
;
596 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
599 symbolS
*s
= fixp
->fx_addsy
;
601 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
602 (int) fixp
->fx_r_type
);
604 fprintf (stream
, "no sym\n");
607 print_symbol_value_1 (stream
, s
);
608 fprintf (stream
, "\n");
610 fixp
= fixp
->fx_next
;
614 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
617 #ifndef EMIT_SECTION_SYMBOLS
618 #define EMIT_SECTION_SYMBOLS 1
621 /* This pass over fixups decides whether symbols can be replaced with
625 adjust_reloc_syms (bfd
*abfd ATTRIBUTE_UNUSED
,
627 void *xxx ATTRIBUTE_UNUSED
)
629 segment_info_type
*seginfo
= seg_info (sec
);
635 dump_section_relocs (abfd
, sec
, stderr
);
637 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
641 else if (fixp
->fx_addsy
)
647 fprintf (stderr
, "\n\nadjusting fixup:\n");
651 sym
= fixp
->fx_addsy
;
653 /* All symbols should have already been resolved at this
654 point. It is possible to see unresolved expression
655 symbols, though, since they are not in the regular symbol
657 resolve_symbol_value (sym
);
659 if (fixp
->fx_subsy
!= NULL
)
660 resolve_symbol_value (fixp
->fx_subsy
);
662 /* If this symbol is equated to an undefined or common symbol,
663 convert the fixup to being against that symbol. */
664 while (symbol_equated_reloc_p (sym
)
665 || S_IS_WEAKREFR (sym
))
667 symbolS
*newsym
= symbol_get_value_expression (sym
)->X_add_symbol
;
670 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
671 fixp
->fx_addsy
= newsym
;
675 if (symbol_mri_common_p (sym
))
677 fixp
->fx_offset
+= S_GET_VALUE (sym
);
678 fixp
->fx_addsy
= symbol_get_value_expression (sym
)->X_add_symbol
;
682 /* If the symbol is undefined, common, weak, or global (ELF
683 shared libs), we can't replace it with the section symbol. */
684 if (S_FORCE_RELOC (fixp
->fx_addsy
, 1))
687 /* Is there some other (target cpu dependent) reason we can't adjust
688 this one? (E.g. relocations involving function addresses on
690 #ifdef tc_fix_adjustable
691 if (! tc_fix_adjustable (fixp
))
695 /* Since we're reducing to section symbols, don't attempt to reduce
696 anything that's already using one. */
697 if (symbol_section_p (sym
))
700 symsec
= S_GET_SEGMENT (sym
);
704 if (bfd_is_abs_section (symsec
))
706 /* The fixup_segment routine normally will not use this
707 symbol in a relocation. */
711 /* Don't try to reduce relocs which refer to non-local symbols
712 in .linkonce sections. It can lead to confusion when a
713 debugging section refers to a .linkonce section. I hope
714 this will always be correct. */
715 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
717 if ((symsec
->flags
& SEC_LINK_ONCE
) != 0
719 /* The GNU toolchain uses an extension for ELF: a
720 section beginning with the magic string
721 .gnu.linkonce is a linkonce section. */
722 && strncmp (segment_name (symsec
), ".gnu.linkonce",
723 sizeof ".gnu.linkonce" - 1) == 0))
727 /* Never adjust a reloc against local symbol in a merge section
728 with non-zero addend. */
729 if ((symsec
->flags
& SEC_MERGE
) != 0
730 && (fixp
->fx_offset
!= 0 || fixp
->fx_subsy
!= NULL
))
733 /* Never adjust a reloc against TLS local symbol. */
734 if ((symsec
->flags
& SEC_THREAD_LOCAL
) != 0)
737 /* We refetch the segment when calling section_symbol, rather
738 than using symsec, because S_GET_VALUE may wind up changing
739 the section when it calls resolve_symbol_value. */
740 fixp
->fx_offset
+= S_GET_VALUE (sym
);
741 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
743 fprintf (stderr
, "\nadjusted fixup:\n");
748 dump_section_relocs (abfd
, sec
, stderr
);
753 Go through all the fixS's in a segment and see which ones can be
754 handled now. (These consist of fixS where we have since discovered
755 the value of a symbol, or the address of the frag involved.)
756 For each one, call md_apply_fix to put the fix into the frag data.
758 Result is a count of how many relocation structs will be needed to
759 handle the remaining fixS's that we couldn't completely handle here.
760 These will be output later by emit_relocations(). */
763 fixup_segment (fixS
*fixP
, segT this_segment
)
765 long seg_reloc_count
= 0;
768 segT add_symbol_segment
= absolute_section
;
770 if (fixP
!= NULL
&& abs_section_sym
== NULL
)
771 abs_section_sym
= section_symbol (absolute_section
);
773 /* If the linker is doing the relaxing, we must not do any fixups.
775 Well, strictly speaking that's not true -- we could do any that
776 are PC-relative and don't cross regions that could change size.
777 And for the i960 we might be able to turn callx/callj into bal
778 anyways in cases where we know the maximum displacement. */
779 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment
))
781 for (; fixP
; fixP
= fixP
->fx_next
)
784 if (fixP
->fx_addsy
== NULL
)
786 /* There was no symbol required by this relocation.
787 However, BFD doesn't really handle relocations
788 without symbols well. So fake up a local symbol in
789 the absolute section. */
790 fixP
->fx_addsy
= abs_section_sym
;
792 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
793 if (fixP
->fx_subsy
!= NULL
)
794 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
797 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
798 return seg_reloc_count
;
801 for (; fixP
; fixP
= fixP
->fx_next
)
804 fprintf (stderr
, "\nprocessing fixup:\n");
808 fragP
= fixP
->fx_frag
;
810 #ifdef TC_VALIDATE_FIX
811 TC_VALIDATE_FIX (fixP
, this_segment
, skip
);
813 add_number
= fixP
->fx_offset
;
815 if (fixP
->fx_addsy
!= NULL
)
816 add_symbol_segment
= S_GET_SEGMENT (fixP
->fx_addsy
);
818 if (fixP
->fx_subsy
!= NULL
)
820 segT sub_symbol_segment
;
821 resolve_symbol_value (fixP
->fx_subsy
);
822 sub_symbol_segment
= S_GET_SEGMENT (fixP
->fx_subsy
);
823 if (fixP
->fx_addsy
!= NULL
824 && sub_symbol_segment
== add_symbol_segment
825 && !TC_FORCE_RELOCATION_SUB_SAME (fixP
, add_symbol_segment
))
827 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
828 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
829 fixP
->fx_offset
= add_number
;
830 fixP
->fx_addsy
= NULL
;
831 fixP
->fx_subsy
= NULL
;
833 /* See the comment below about 68k weirdness. */
837 else if (sub_symbol_segment
== absolute_section
838 && !TC_FORCE_RELOCATION_SUB_ABS (fixP
))
840 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
841 fixP
->fx_offset
= add_number
;
842 fixP
->fx_subsy
= NULL
;
844 else if (sub_symbol_segment
== this_segment
845 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP
))
847 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
848 fixP
->fx_offset
= (add_number
+ fixP
->fx_dot_value
849 + fixP
->fx_frag
->fr_address
);
851 /* Make it pc-relative. If the back-end code has not
852 selected a pc-relative reloc, cancel the adjustment
853 we do later on all pc-relative relocs. */
856 /* Do this for m68k even if it's already described
857 as pc-relative. On the m68k, an operand of
858 "pc@(foo-.-2)" should address "foo" in a
863 add_number
+= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
864 fixP
->fx_subsy
= NULL
;
867 else if (!TC_VALIDATE_FIX_SUB (fixP
))
869 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
870 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
871 fixP
->fx_addsy
? S_GET_NAME (fixP
->fx_addsy
) : "0",
872 segment_name (add_symbol_segment
),
873 S_GET_NAME (fixP
->fx_subsy
),
874 segment_name (sub_symbol_segment
));
880 if (add_symbol_segment
== this_segment
881 && !TC_FORCE_RELOCATION_LOCAL (fixP
))
883 /* This fixup was made when the symbol's segment was
884 SEG_UNKNOWN, but it is now in the local segment.
885 So we know how to do the address without relocation. */
886 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
887 fixP
->fx_offset
= add_number
;
889 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
890 fixP
->fx_addsy
= NULL
;
893 else if (add_symbol_segment
== absolute_section
894 && !TC_FORCE_RELOCATION_ABS (fixP
))
896 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
897 fixP
->fx_offset
= add_number
;
898 fixP
->fx_addsy
= NULL
;
900 else if (add_symbol_segment
!= undefined_section
901 && ! bfd_is_com_section (add_symbol_segment
)
902 && MD_APPLY_SYM_VALUE (fixP
))
903 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
908 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
909 if (!fixP
->fx_done
&& fixP
->fx_addsy
== NULL
)
911 /* There was no symbol required by this relocation.
912 However, BFD doesn't really handle relocations
913 without symbols well. So fake up a local symbol in
914 the absolute section. */
915 fixP
->fx_addsy
= abs_section_sym
;
920 md_apply_fix (fixP
, &add_number
, this_segment
);
925 if (fixP
->fx_addsy
== NULL
)
926 fixP
->fx_addsy
= abs_section_sym
;
927 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
928 if (fixP
->fx_subsy
!= NULL
)
929 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
932 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& fixP
->fx_size
!= 0)
934 if (fixP
->fx_size
< sizeof (valueT
))
939 mask
--; /* Set all bits to one. */
940 mask
<<= fixP
->fx_size
* 8 - (fixP
->fx_signed
? 1 : 0);
941 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
943 char buf
[50], buf2
[50];
944 sprint_value (buf
, fragP
->fr_address
+ fixP
->fx_where
);
945 if (add_number
> 1000)
946 sprint_value (buf2
, add_number
);
948 sprintf (buf2
, "%ld", (long) add_number
);
949 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
950 _("value of %s too large for field of %d bytes at %s"),
951 buf2
, fixP
->fx_size
, buf
);
952 } /* Generic error checking. */
954 #ifdef WARN_SIGNED_OVERFLOW_WORD
955 /* Warn if a .word value is too large when treated as a signed
956 number. We already know it is not too negative. This is to
957 catch over-large switches generated by gcc on the 68k. */
958 if (!flag_signed_overflow_ok
959 && fixP
->fx_size
== 2
960 && add_number
> 0x7fff)
961 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
962 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
964 (long) (fragP
->fr_address
+ fixP
->fx_where
));
966 } /* Not a bit fix. */
968 #ifdef TC_VALIDATE_FIX
969 skip
: ATTRIBUTE_UNUSED_LABEL
973 fprintf (stderr
, "result:\n");
976 } /* For each fixS in this segment. */
978 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
979 return seg_reloc_count
;
983 fix_segment (bfd
*abfd ATTRIBUTE_UNUSED
,
985 void *xxx ATTRIBUTE_UNUSED
)
987 segment_info_type
*seginfo
= seg_info (sec
);
989 fixup_segment (seginfo
->fix_root
, sec
);
993 write_relocs (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
995 segment_info_type
*seginfo
= seg_info (sec
);
1002 /* If seginfo is NULL, we did not create this section; don't do
1003 anything with it. */
1004 if (seginfo
== NULL
)
1008 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
1011 #ifndef RELOC_EXPANSION_POSSIBLE
1012 /* Set up reloc information as well. */
1013 relocs
= xcalloc (n
, sizeof (arelent
*));
1016 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1019 bfd_reloc_status_type s
;
1027 reloc
= tc_gen_reloc (sec
, fixp
);
1035 This test is triggered inappropriately for the SH:
1036 if (fixp->fx_where + fixp->fx_size
1037 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1041 s
= bfd_install_relocation (stdoutput
, reloc
,
1042 fixp
->fx_frag
->fr_literal
,
1043 fixp
->fx_frag
->fr_address
,
1049 case bfd_reloc_overflow
:
1050 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1051 _("relocation overflow"));
1053 case bfd_reloc_outofrange
:
1054 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1055 _("relocation out of range"));
1058 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1059 fixp
->fx_file
, fixp
->fx_line
, s
);
1061 relocs
[i
++] = reloc
;
1064 n
= n
* MAX_RELOC_EXPANSION
;
1065 /* Set up reloc information as well. */
1066 relocs
= xcalloc (n
, sizeof (arelent
*));
1069 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1072 bfd_reloc_status_type s
;
1081 reloc
= tc_gen_reloc (sec
, fixp
);
1083 for (j
= 0; reloc
[j
]; j
++)
1085 relocs
[i
++] = reloc
[j
];
1088 if (fixp
->fx_where
+ fixp
->fx_size
1089 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1090 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1091 _("internal error: fixup not contained within frag"));
1092 for (j
= 0; reloc
[j
]; j
++)
1094 s
= bfd_install_relocation (stdoutput
, reloc
[j
],
1095 fixp
->fx_frag
->fr_literal
,
1096 fixp
->fx_frag
->fr_address
,
1102 case bfd_reloc_overflow
:
1103 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1104 _("relocation overflow"));
1106 case bfd_reloc_outofrange
:
1107 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1108 _("relocation out of range"));
1111 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1112 fixp
->fx_file
, fixp
->fx_line
, s
);
1121 unsigned int i
, j
, nsyms
;
1123 sympp
= bfd_get_outsymbols (stdoutput
);
1124 nsyms
= bfd_get_symcount (stdoutput
);
1125 for (i
= 0; i
< n
; i
++)
1126 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1128 for (j
= 0; j
< nsyms
; j
++)
1129 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
1138 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1140 bfd_set_section_flags (abfd
, sec
,
1141 (bfd_get_section_flags (abfd
, sec
)
1142 & (flagword
) ~SEC_RELOC
));
1144 #ifdef SET_SECTION_RELOCS
1145 SET_SECTION_RELOCS (sec
, relocs
, n
);
1153 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1154 for (i
= 0; i
< n
; i
++)
1157 s
= *r
->sym_ptr_ptr
;
1158 fprintf (stderr
, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1159 i
, r
, (unsigned long)r
->address
, s
->name
, (unsigned long)r
->addend
);
1166 write_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
1168 void *xxx ATTRIBUTE_UNUSED
)
1170 segment_info_type
*seginfo
= seg_info (sec
);
1171 addressT offset
= 0;
1174 /* Write out the frags. */
1176 || !(bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1179 for (f
= seginfo
->frchainP
->frch_root
;
1188 assert (f
->fr_type
== rs_fill
);
1191 x
= bfd_set_section_contents (stdoutput
, sec
,
1192 f
->fr_literal
, (file_ptr
) offset
,
1193 (bfd_size_type
) f
->fr_fix
);
1195 as_fatal (_("can't write %s: %s"), stdoutput
->filename
,
1196 bfd_errmsg (bfd_get_error ()));
1197 offset
+= f
->fr_fix
;
1199 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1200 fill_size
= f
->fr_var
;
1201 count
= f
->fr_offset
;
1202 assert (count
>= 0);
1203 if (fill_size
&& count
)
1206 if (fill_size
> sizeof (buf
))
1208 /* Do it the old way. Can this ever happen? */
1211 x
= bfd_set_section_contents (stdoutput
, sec
,
1214 (bfd_size_type
) fill_size
);
1216 as_fatal (_("can't write %s: %s"), stdoutput
->filename
,
1217 bfd_errmsg (bfd_get_error ()));
1218 offset
+= fill_size
;
1223 /* Build a buffer full of fill objects and output it as
1224 often as necessary. This saves on the overhead of
1225 potentially lots of bfd_set_section_contents calls. */
1229 n_per_buf
= sizeof (buf
);
1230 memset (buf
, *fill_literal
, n_per_buf
);
1235 n_per_buf
= sizeof (buf
) / fill_size
;
1236 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1237 memcpy (bufp
, fill_literal
, fill_size
);
1239 for (; count
> 0; count
-= n_per_buf
)
1241 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1242 x
= bfd_set_section_contents
1243 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1244 (bfd_size_type
) n_per_buf
* fill_size
);
1246 as_fatal (_("cannot write to output file"));
1247 offset
+= n_per_buf
* fill_size
;
1255 merge_data_into_text (void)
1257 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1258 seg_info (data_section
)->frchainP
->frch_root
;
1259 seg_info (text_section
)->frchainP
->frch_last
=
1260 seg_info (data_section
)->frchainP
->frch_last
;
1261 seg_info (data_section
)->frchainP
= 0;
1272 /* Count symbols. We can't rely on a count made by the loop in
1273 write_object_file, because *_frob_file may add a new symbol or
1276 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1282 bfd_size_type amt
= (bfd_size_type
) nsyms
* sizeof (asymbol
*);
1284 asympp
= bfd_alloc (stdoutput
, amt
);
1285 symp
= symbol_rootP
;
1286 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1288 asympp
[i
] = symbol_get_bfdsym (symp
);
1289 symbol_mark_written (symp
);
1294 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1296 symbol_table_frozen
= 1;
1299 /* Finish the subsegments. After every sub-segment, we fake an
1300 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1301 ".fill 0" because that is the kind of frag that requires least
1302 thought. ".align" frags like to have a following frag since that
1303 makes calculating their intended length trivial. */
1305 #ifndef SUB_SEGMENT_ALIGN
1307 /* The last subsegment gets an alignment corresponding to the alignment
1308 of the section. This allows proper nop-filling at the end of
1309 code-bearing sections. */
1310 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1311 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1313 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1318 subsegs_finish (void)
1320 struct frchain
*frchainP
;
1323 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
1325 segment_info_type
*seginfo
= seg_info (s
);
1329 for (frchainP
= seginfo
->frchainP
;
1331 frchainP
= frchainP
->frch_next
)
1335 subseg_set (s
, frchainP
->frch_subseg
);
1337 /* This now gets called even if we had errors. In that case,
1338 any alignment is meaningless, and, moreover, will look weird
1339 if we are generating a listing. */
1342 alignment
= SUB_SEGMENT_ALIGN (now_seg
, frchainP
);
1343 if ((bfd_get_section_flags (now_seg
->owner
, now_seg
) & SEC_MERGE
)
1344 && now_seg
->entsize
)
1346 unsigned int entsize
= now_seg
->entsize
;
1349 while ((entsize
& 1) == 0)
1354 if (entalign
> alignment
)
1355 alignment
= entalign
;
1359 if (subseg_text_p (now_seg
))
1360 frag_align_code (alignment
, 0);
1362 frag_align (alignment
, 0, 0);
1364 /* frag_align will have left a new frag.
1365 Use this last frag for an empty ".fill".
1367 For this segment ...
1368 Create a last frag. Do not leave a "being filled in frag". */
1369 frag_wane (frag_now
);
1370 frag_now
->fr_fix
= 0;
1371 know (frag_now
->fr_next
== NULL
);
1376 /* Write the object file. */
1379 write_object_file (void)
1381 struct relax_seg_info rsi
;
1382 #ifndef WORKING_DOT_WORD
1383 fragS
*fragP
; /* Track along all frags. */
1386 /* Do we really want to write it? */
1388 int n_warns
, n_errs
;
1389 n_warns
= had_warnings ();
1390 n_errs
= had_errors ();
1391 /* The -Z flag indicates that an object file should be generated,
1392 regardless of warnings and errors. */
1393 if (flag_always_generate_output
)
1395 if (n_warns
|| n_errs
)
1396 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1397 n_errs
, n_errs
== 1 ? "" : "s",
1398 n_warns
, n_warns
== 1 ? "" : "s");
1403 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1404 n_errs
, n_errs
== 1 ? "" : "s",
1405 n_warns
, n_warns
== 1 ? "" : "s");
1410 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1411 a routine to check for the definition of the procedure "_main",
1412 and if so -- fix it up so that it can be program entry point. */
1413 vms_check_for_main ();
1414 #endif /* OBJ_VMS */
1416 /* From now on, we don't care about sub-segments. Build one frag chain
1417 for each segment. Linked thru fr_next. */
1419 /* Remove the sections created by gas for its own purposes. */
1423 bfd_section_list_remove (stdoutput
, reg_section
);
1424 bfd_section_list_remove (stdoutput
, expr_section
);
1425 stdoutput
->section_count
-= 2;
1427 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1430 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1432 /* We have two segments. If user gave -R flag, then we must put the
1433 data frags into the text segment. Do this before relaxing so
1434 we know to take advantage of -R and make shorter addresses. */
1435 if (flag_readonly_data_in_text
)
1437 merge_data_into_text ();
1443 #ifndef WORKING_DOT_WORD
1444 /* We need to reset the markers in the broken word list and
1445 associated frags between calls to relax_segment (via
1446 relax_seg). Since the broken word list is global, we do it
1447 once per round, rather than locally in relax_segment for each
1449 struct broken_word
*brokp
;
1451 for (brokp
= broken_words
;
1452 brokp
!= (struct broken_word
*) NULL
;
1453 brokp
= brokp
->next_broken_word
)
1457 if (brokp
->dispfrag
!= (fragS
*) NULL
1458 && brokp
->dispfrag
->fr_type
== rs_broken_word
)
1459 brokp
->dispfrag
->fr_subtype
= 0;
1464 bfd_map_over_sections (stdoutput
, relax_seg
, &rsi
);
1470 /* Note - Most ports will use the default value of
1471 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1472 local symbols to be resolved, removing their frag information.
1473 Some ports however, will not have finished relaxing all of
1474 their frags and will still need the local symbol frag
1475 information. These ports can set
1476 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1477 finalize_syms
= TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
;
1479 bfd_map_over_sections (stdoutput
, size_seg
, (char *) 0);
1481 /* Relaxation has completed. Freeze all syms. */
1484 #ifdef md_post_relax_hook
1488 #ifndef WORKING_DOT_WORD
1490 struct broken_word
*lie
;
1491 struct broken_word
**prevP
;
1493 prevP
= &broken_words
;
1494 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1499 subseg_change (lie
->seg
, lie
->subseg
);
1500 exp
.X_op
= O_subtract
;
1501 exp
.X_add_symbol
= lie
->add
;
1502 exp
.X_op_symbol
= lie
->sub
;
1503 exp
.X_add_number
= lie
->addnum
;
1504 #ifdef TC_CONS_FIX_NEW
1505 TC_CONS_FIX_NEW (lie
->frag
,
1506 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1509 fix_new_exp (lie
->frag
,
1510 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1511 2, &exp
, 0, BFD_RELOC_16
);
1513 *prevP
= lie
->next_broken_word
;
1516 prevP
= &(lie
->next_broken_word
);
1518 for (lie
= broken_words
; lie
;)
1520 struct broken_word
*untruth
;
1522 addressT table_addr
;
1523 addressT from_addr
, to_addr
;
1526 subseg_change (lie
->seg
, lie
->subseg
);
1527 fragP
= lie
->dispfrag
;
1529 /* Find out how many broken_words go here. */
1532 untruth
&& untruth
->dispfrag
== fragP
;
1533 untruth
= untruth
->next_broken_word
)
1534 if (untruth
->added
== 1)
1537 table_ptr
= lie
->dispfrag
->fr_opcode
;
1538 table_addr
= (lie
->dispfrag
->fr_address
1539 + (table_ptr
- lie
->dispfrag
->fr_literal
));
1540 /* Create the jump around the long jumps. This is a short
1541 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1542 from_addr
= table_addr
;
1543 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1544 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1546 table_ptr
+= md_short_jump_size
;
1547 table_addr
+= md_short_jump_size
;
1550 lie
&& lie
->dispfrag
== fragP
;
1551 m
++, lie
= lie
->next_broken_word
)
1553 if (lie
->added
== 2)
1555 /* Patch the jump table. */
1556 /* This is the offset from ??? to table_ptr+0. */
1557 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1558 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1559 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr
, lie
);
1561 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1562 for (untruth
= lie
->next_broken_word
;
1563 untruth
&& untruth
->dispfrag
== fragP
;
1564 untruth
= untruth
->next_broken_word
)
1566 if (untruth
->use_jump
== lie
)
1567 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1570 /* Install the long jump. */
1571 /* This is a long jump from table_ptr+0 to the final target. */
1572 from_addr
= table_addr
;
1573 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1574 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1576 table_ptr
+= md_long_jump_size
;
1577 table_addr
+= md_long_jump_size
;
1581 #endif /* not WORKING_DOT_WORD */
1583 /* Resolve symbol values. This needs to be done before processing
1589 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1590 resolve_symbol_value (symp
);
1592 resolve_local_symbol_values ();
1596 #ifdef tc_frob_file_before_adjust
1597 tc_frob_file_before_adjust ();
1599 #ifdef obj_frob_file_before_adjust
1600 obj_frob_file_before_adjust ();
1603 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
1605 #ifdef tc_frob_file_before_fix
1606 tc_frob_file_before_fix ();
1608 #ifdef obj_frob_file_before_fix
1609 obj_frob_file_before_fix ();
1612 bfd_map_over_sections (stdoutput
, fix_segment
, (char *) 0);
1614 /* Set up symbol table, and write it out. */
1618 bfd_boolean skip_next_symbol
= FALSE
;
1620 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1625 if (skip_next_symbol
)
1627 /* Don't do anything besides moving the value of the
1628 symbol from the GAS value-field to the BFD value-field. */
1629 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
1630 skip_next_symbol
= FALSE
;
1634 if (symbol_mri_common_p (symp
))
1636 if (S_IS_EXTERNAL (symp
))
1637 as_bad (_("%s: global symbols not supported in common sections"),
1639 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1643 name
= S_GET_NAME (symp
);
1647 decode_local_label_name ((char *) S_GET_NAME (symp
));
1648 /* They only differ if `name' is a fb or dollar local
1650 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
1651 as_bad (_("local label `%s' is not defined"), name2
);
1654 /* Do it again, because adjust_reloc_syms might introduce
1655 more symbols. They'll probably only be section symbols,
1656 but they'll still need to have the values computed. */
1657 resolve_symbol_value (symp
);
1659 /* Skip symbols which were equated to undefined or common
1661 if (symbol_equated_reloc_p (symp
)
1662 || S_IS_WEAKREFR (symp
))
1664 const char *name
= S_GET_NAME (symp
);
1665 if (S_IS_COMMON (symp
)
1666 && !TC_FAKE_LABEL (name
)
1667 && !S_IS_WEAKREFR (symp
)
1668 && (!S_IS_EXTERNAL (symp
) || S_IS_LOCAL (symp
)))
1670 expressionS
*e
= symbol_get_value_expression (symp
);
1671 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1672 name
, S_GET_NAME (e
->X_add_symbol
));
1674 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1678 #ifdef obj_frob_symbol
1679 obj_frob_symbol (symp
, punt
);
1681 #ifdef tc_frob_symbol
1682 if (! punt
|| symbol_used_in_reloc_p (symp
))
1683 tc_frob_symbol (symp
, punt
);
1686 /* If we don't want to keep this symbol, splice it out of
1687 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1688 want section symbols. Otherwise, we skip local symbols
1689 and symbols that the frob_symbol macros told us to punt,
1690 but we keep such symbols if they are used in relocs. */
1691 if (symp
== abs_section_sym
1692 || (! EMIT_SECTION_SYMBOLS
1693 && symbol_section_p (symp
))
1694 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1695 opposites. Sometimes the former checks flags and the
1696 latter examines the name... */
1697 || (!S_IS_EXTERNAL (symp
)
1698 && (punt
|| S_IS_LOCAL (symp
) ||
1699 (S_IS_WEAKREFD (symp
) && ! symbol_used_p (symp
)))
1700 && ! symbol_used_in_reloc_p (symp
)))
1702 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1704 /* After symbol_remove, symbol_next(symp) still returns
1705 the one that came after it in the chain. So we don't
1706 need to do any extra cleanup work here. */
1710 /* Make sure we really got a value for the symbol. */
1711 if (! symbol_resolved_p (symp
))
1713 as_bad (_("can't resolve value for symbol `%s'"),
1715 symbol_mark_resolved (symp
);
1718 /* Set the value into the BFD symbol. Up til now the value
1719 has only been kept in the gas symbolS struct. */
1720 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
1722 /* A warning construct is a warning symbol followed by the
1723 symbol warned about. Don't let anything object-format or
1724 target-specific muck with it; it's ready for output. */
1725 if (symbol_get_bfdsym (symp
)->flags
& BSF_WARNING
)
1726 skip_next_symbol
= TRUE
;
1732 /* Now do any format-specific adjustments to the symbol table, such
1733 as adding file symbols. */
1734 #ifdef tc_adjust_symtab
1735 tc_adjust_symtab ();
1737 #ifdef obj_adjust_symtab
1738 obj_adjust_symtab ();
1741 /* Now that all the sizes are known, and contents correct, we can
1742 start writing to the file. */
1745 /* If *_frob_file changes the symbol value at this point, it is
1746 responsible for moving the changed value into symp->bsym->value
1747 as well. Hopefully all symbol value changing can be done in
1752 #ifdef obj_frob_file
1756 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
1758 #ifdef tc_frob_file_after_relocs
1759 tc_frob_file_after_relocs ();
1761 #ifdef obj_frob_file_after_relocs
1762 obj_frob_file_after_relocs ();
1765 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
1768 #ifdef TC_GENERIC_RELAX_TABLE
1769 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1772 relax_frag (segT segment
, fragS
*fragP
, long stretch
)
1774 const relax_typeS
*this_type
;
1775 const relax_typeS
*start_type
;
1776 relax_substateT next_state
;
1777 relax_substateT this_state
;
1783 const relax_typeS
*table
;
1785 target
= fragP
->fr_offset
;
1786 address
= fragP
->fr_address
;
1787 table
= TC_GENERIC_RELAX_TABLE
;
1788 this_state
= fragP
->fr_subtype
;
1789 start_type
= this_type
= table
+ this_state
;
1790 symbolP
= fragP
->fr_symbol
;
1796 sym_frag
= symbol_get_frag (symbolP
);
1798 #ifndef DIFF_EXPR_OK
1799 know (sym_frag
!= NULL
);
1801 know (S_GET_SEGMENT (symbolP
) != absolute_section
1802 || sym_frag
== &zero_address_frag
);
1803 target
+= S_GET_VALUE (symbolP
);
1805 /* If frag has yet to be reached on this pass,
1806 assume it will move by STRETCH just as we did.
1807 If this is not so, it will be because some frag
1808 between grows, and that will force another pass. */
1811 && sym_frag
->relax_marker
!= fragP
->relax_marker
1812 && S_GET_SEGMENT (symbolP
) == segment
)
1818 aim
= target
- address
- fragP
->fr_fix
;
1819 #ifdef TC_PCREL_ADJUST
1820 /* Currently only the ns32k family needs this. */
1821 aim
+= TC_PCREL_ADJUST (fragP
);
1824 #ifdef md_prepare_relax_scan
1825 /* Formerly called M68K_AIM_KLUDGE. */
1826 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
1831 /* Look backwards. */
1832 for (next_state
= this_type
->rlx_more
; next_state
;)
1833 if (aim
>= this_type
->rlx_backward
)
1837 /* Grow to next state. */
1838 this_state
= next_state
;
1839 this_type
= table
+ this_state
;
1840 next_state
= this_type
->rlx_more
;
1845 /* Look forwards. */
1846 for (next_state
= this_type
->rlx_more
; next_state
;)
1847 if (aim
<= this_type
->rlx_forward
)
1851 /* Grow to next state. */
1852 this_state
= next_state
;
1853 this_type
= table
+ this_state
;
1854 next_state
= this_type
->rlx_more
;
1858 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
1860 fragP
->fr_subtype
= this_state
;
1864 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
1866 /* Relax_align. Advance location counter to next address that has 'alignment'
1867 lowest order bits all 0s, return size of adjustment made. */
1868 static relax_addressT
1869 relax_align (register relax_addressT address
, /* Address now. */
1870 register int alignment
/* Alignment (binary). */)
1872 relax_addressT mask
;
1873 relax_addressT new_address
;
1875 mask
= ~((~0) << alignment
);
1876 new_address
= (address
+ mask
) & (~mask
);
1877 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1879 /* We must provide lots of padding, so the linker can discard it
1880 when needed. The linker will not add extra space, ever. */
1881 new_address
+= (1 << alignment
);
1883 return (new_address
- address
);
1886 /* Now we have a segment, not a crowd of sub-segments, we can make
1891 After this, all frags in this segment have addresses that are correct
1892 within the segment. Since segments live in different file addresses,
1893 these frag addresses may not be the same as final object-file
1897 relax_segment (struct frag
*segment_frag_root
, segT segment
, int pass
)
1899 unsigned long frag_count
;
1901 relax_addressT address
;
1904 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1905 subseg_change (segment
, 0);
1907 /* For each frag in segment: count and store (a 1st guess of)
1910 for (frag_count
= 0, fragP
= segment_frag_root
;
1912 fragP
= fragP
->fr_next
, frag_count
++)
1914 fragP
->relax_marker
= 0;
1915 fragP
->fr_address
= address
;
1916 address
+= fragP
->fr_fix
;
1918 switch (fragP
->fr_type
)
1921 address
+= fragP
->fr_offset
* fragP
->fr_var
;
1928 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
1930 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
1933 if (offset
% fragP
->fr_var
!= 0)
1935 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
1936 _("alignment padding (%lu bytes) not a multiple of %ld"),
1937 (unsigned long) offset
, (long) fragP
->fr_var
);
1938 offset
-= (offset
% fragP
->fr_var
);
1947 /* Assume .org is nugatory. It will grow with 1st relax. */
1950 case rs_machine_dependent
:
1951 /* If fr_symbol is an expression, this call to
1952 resolve_symbol_value sets up the correct segment, which will
1953 likely be needed in md_estimate_size_before_relax. */
1954 if (fragP
->fr_symbol
)
1955 resolve_symbol_value (fragP
->fr_symbol
);
1957 address
+= md_estimate_size_before_relax (fragP
, segment
);
1960 #ifndef WORKING_DOT_WORD
1961 /* Broken words don't concern us yet. */
1962 case rs_broken_word
:
1967 /* Initial guess is always 1; doing otherwise can result in
1968 stable solutions that are larger than the minimum. */
1969 address
+= fragP
->fr_offset
= 1;
1973 address
+= eh_frame_estimate_size_before_relax (fragP
);
1977 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
1981 BAD_CASE (fragP
->fr_type
);
1988 unsigned long max_iterations
;
1989 offsetT stretch
; /* May be any size, 0 or negative. */
1990 /* Cumulative number of addresses we have relaxed this pass.
1991 We may have relaxed more than one address. */
1992 int stretched
; /* Have we stretched on this pass? */
1993 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1994 grew, and another shrank. If a branch instruction doesn't fit anymore,
1995 we could be scrod. */
1997 /* We want to prevent going into an infinite loop where one frag grows
1998 depending upon the location of a symbol which is in turn moved by
1999 the growing frag. eg:
2005 So we dictate that this algorithm can be at most O2. */
2006 max_iterations
= frag_count
* frag_count
;
2007 /* Check for overflow. */
2008 if (max_iterations
< frag_count
)
2009 max_iterations
= frag_count
;
2017 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2020 addressT was_address
;
2024 fragP
->relax_marker
^= 1;
2025 was_address
= fragP
->fr_address
;
2026 address
= fragP
->fr_address
+= stretch
;
2027 symbolP
= fragP
->fr_symbol
;
2028 offset
= fragP
->fr_offset
;
2030 switch (fragP
->fr_type
)
2032 case rs_fill
: /* .fill never relaxes. */
2036 #ifndef WORKING_DOT_WORD
2037 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2038 for it I do not want to write it. I do not want to have
2039 anything to do with it. This is not the proper way to
2040 implement this misfeature. */
2041 case rs_broken_word
:
2043 struct broken_word
*lie
;
2044 struct broken_word
*untruth
;
2046 /* Yes this is ugly (storing the broken_word pointer
2047 in the symbol slot). Still, this whole chunk of
2048 code is ugly, and I don't feel like doing anything
2049 about it. Think of it as stubbornness in action. */
2051 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2052 lie
&& lie
->dispfrag
== fragP
;
2053 lie
= lie
->next_broken_word
)
2059 offset
= (S_GET_VALUE (lie
->add
)
2061 - S_GET_VALUE (lie
->sub
));
2062 if (offset
<= -32768 || offset
>= 32767)
2064 if (flag_warn_displacement
)
2067 sprint_value (buf
, (addressT
) lie
->addnum
);
2068 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2069 _(".word %s-%s+%s didn't fit"),
2070 S_GET_NAME (lie
->add
),
2071 S_GET_NAME (lie
->sub
),
2075 if (fragP
->fr_subtype
== 0)
2077 fragP
->fr_subtype
++;
2078 growth
+= md_short_jump_size
;
2080 for (untruth
= lie
->next_broken_word
;
2081 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2082 untruth
= untruth
->next_broken_word
)
2083 if ((symbol_get_frag (untruth
->add
)
2084 == symbol_get_frag (lie
->add
))
2085 && (S_GET_VALUE (untruth
->add
)
2086 == S_GET_VALUE (lie
->add
)))
2089 untruth
->use_jump
= lie
;
2091 growth
+= md_long_jump_size
;
2096 } /* case rs_broken_word */
2102 addressT oldoff
, newoff
;
2104 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2106 newoff
= relax_align (address
+ fragP
->fr_fix
,
2109 if (fragP
->fr_subtype
!= 0)
2111 if (oldoff
> fragP
->fr_subtype
)
2113 if (newoff
> fragP
->fr_subtype
)
2117 growth
= newoff
- oldoff
;
2123 addressT target
= offset
;
2128 /* Convert from an actual address to an octet offset
2129 into the section. Here it is assumed that the
2130 section's VMA is zero, and can omit subtracting it
2131 from the symbol's value to get the address offset. */
2132 know (S_GET_SEGMENT (symbolP
)->vma
== 0);
2133 target
+= S_GET_VALUE (symbolP
) * OCTETS_PER_BYTE
;
2136 know (fragP
->fr_next
);
2137 after
= fragP
->fr_next
->fr_address
;
2138 growth
= target
- after
;
2143 /* Don't error on first few frag relax passes.
2144 The symbol might be an expression involving
2145 symbol values from other sections. If those
2146 sections have not yet been processed their
2147 frags will all have zero addresses, so we
2148 will calculate incorrect values for them. The
2149 number of passes we allow before giving an
2150 error is somewhat arbitrary. It should be at
2151 least one, with larger values requiring
2152 increasingly contrived dependencies between
2153 frags to trigger a false error. */
2156 /* Force another pass. */
2161 /* Growth may be negative, but variable part of frag
2162 cannot have fewer than 0 chars. That is, we can't
2164 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2165 _("attempt to move .org backwards"));
2167 /* We've issued an error message. Change the
2168 frag to avoid cascading errors. */
2169 fragP
->fr_type
= rs_align
;
2170 fragP
->fr_subtype
= 0;
2171 fragP
->fr_offset
= 0;
2172 fragP
->fr_fix
= after
- was_address
;
2176 /* This is an absolute growth factor */
2187 amount
= S_GET_VALUE (symbolP
);
2188 if (S_GET_SEGMENT (symbolP
) != absolute_section
2189 || S_IS_COMMON (symbolP
)
2190 || ! S_IS_DEFINED (symbolP
))
2192 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2193 _(".space specifies non-absolute value"));
2194 /* Prevent repeat of this error message. */
2195 fragP
->fr_symbol
= 0;
2197 else if (amount
< 0)
2199 /* Don't error on first few frag relax passes.
2200 See rs_org comment for a longer explanation. */
2207 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2208 _(".space or .fill with negative value, ignored"));
2209 fragP
->fr_symbol
= 0;
2212 growth
= (was_address
+ fragP
->fr_fix
+ amount
2213 - fragP
->fr_next
->fr_address
);
2217 case rs_machine_dependent
:
2218 #ifdef md_relax_frag
2219 growth
= md_relax_frag (segment
, fragP
, stretch
);
2221 #ifdef TC_GENERIC_RELAX_TABLE
2222 /* The default way to relax a frag is to look through
2223 TC_GENERIC_RELAX_TABLE. */
2224 growth
= relax_frag (segment
, fragP
, stretch
);
2225 #endif /* TC_GENERIC_RELAX_TABLE */
2234 value
= resolve_symbol_value (fragP
->fr_symbol
);
2235 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2236 growth
= size
- fragP
->fr_offset
;
2237 fragP
->fr_offset
= size
;
2242 growth
= eh_frame_relax_frag (fragP
);
2246 growth
= dwarf2dbg_relax_frag (fragP
);
2250 BAD_CASE (fragP
->fr_type
);
2260 /* Until nothing further to relax. */
2261 while (stretched
&& -- max_iterations
);
2264 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2265 segment_name (segment
));
2268 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2269 if (fragP
->last_fr_address
!= fragP
->fr_address
)
2271 fragP
->last_fr_address
= fragP
->fr_address
;
2278 number_to_chars_bigendian (char *buf
, valueT val
, int n
)
2284 buf
[n
] = val
& 0xff;
2290 number_to_chars_littleendian (char *buf
, valueT val
, int n
)
2296 *buf
++ = val
& 0xff;
2302 write_print_statistics (FILE *file
)
2304 fprintf (file
, "fixups: %d\n", n_fixups
);
2307 /* For debugging. */
2308 extern int indent_level
;
2311 print_fixup (fixS
*fixp
)
2314 fprintf (stderr
, "fix %lx %s:%d", (long) fixp
, fixp
->fx_file
, fixp
->fx_line
);
2316 fprintf (stderr
, " pcrel");
2317 if (fixp
->fx_pcrel_adjust
)
2318 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2319 if (fixp
->fx_im_disp
)
2322 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2324 fprintf (stderr
, " im_disp");
2328 fprintf (stderr
, " tcbit");
2330 fprintf (stderr
, " done");
2331 fprintf (stderr
, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2332 fixp
->fx_size
, (long) fixp
->fx_frag
, (long) fixp
->fx_where
,
2333 (long) fixp
->fx_offset
, (long) fixp
->fx_addnumber
);
2334 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2338 fprintf (stderr
, "\n +<");
2339 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2340 fprintf (stderr
, ">");
2344 fprintf (stderr
, "\n -<");
2345 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2346 fprintf (stderr
, ">");
2348 fprintf (stderr
, "\n");
2349 #ifdef TC_FIX_DATA_PRINT
2350 TC_FIX_DATA_PRINT (stderr
, fixp
);