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
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"
31 #ifndef TC_ADJUST_RELOC_COUNT
32 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
35 #ifndef TC_FORCE_RELOCATION
36 #define TC_FORCE_RELOCATION(FIX) \
37 (generic_force_reloc (FIX))
40 #ifndef TC_FORCE_RELOCATION_ABS
41 #define TC_FORCE_RELOCATION_ABS(FIX) \
42 (TC_FORCE_RELOCATION (FIX))
45 #ifndef TC_FORCE_RELOCATION_LOCAL
46 #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 static void renumber_sections (bfd
*, asection
*, PTR
);
114 /* We generally attach relocs to frag chains. However, after we have
115 chained these all together into a segment, any relocs we add after
116 that must be attached to a segment. This will include relocs added
117 in md_estimate_size_for_relax, for example. */
118 static int frags_chained
= 0;
122 #define RELOC_ENUM enum bfd_reloc_code_real
124 static fixS
*fix_new_internal (fragS
*, int where
, int size
,
125 symbolS
*add
, symbolS
*sub
,
126 offsetT offset
, int pcrel
,
128 static long fixup_segment (fixS
*, segT
);
129 static relax_addressT
relax_align (relax_addressT addr
, int align
);
130 static fragS
*chain_frchains_together_1 (segT
, struct frchain
*);
131 static void chain_frchains_together (bfd
*, segT
, PTR
);
132 static void cvt_frag_to_fill (segT
, fragS
*);
133 static void adjust_reloc_syms (bfd
*, asection
*, PTR
);
134 static void fix_segment (bfd
*, asection
*, PTR
);
135 static void write_relocs (bfd
*, asection
*, PTR
);
136 static void write_contents (bfd
*, asection
*, PTR
);
137 static void set_symtab (void);
138 static void merge_data_into_text (void);
140 /* Create a fixS in obstack 'notes'. */
143 fix_new_internal (fragS
*frag
, /* Which frag? */
144 int where
, /* Where in that frag? */
145 int size
, /* 1, 2, or 4 usually. */
146 symbolS
*add_symbol
, /* X_add_symbol. */
147 symbolS
*sub_symbol
, /* X_op_symbol. */
148 offsetT offset
, /* X_add_number. */
149 int pcrel
, /* TRUE if PC-relative relocation. */
150 RELOC_ENUM r_type ATTRIBUTE_UNUSED
/* Relocation type. */)
156 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
158 fixP
->fx_frag
= frag
;
159 fixP
->fx_where
= where
;
160 fixP
->fx_size
= size
;
161 /* We've made fx_size a narrow field; check that it's wide enough. */
162 if (fixP
->fx_size
!= size
)
164 as_bad (_("field fx_size too small to hold %d"), size
);
167 fixP
->fx_addsy
= add_symbol
;
168 fixP
->fx_subsy
= sub_symbol
;
169 fixP
->fx_offset
= offset
;
170 fixP
->fx_dot_value
= dot_value
;
171 fixP
->fx_pcrel
= pcrel
;
173 fixP
->fx_r_type
= r_type
;
174 fixP
->fx_im_disp
= 0;
175 fixP
->fx_pcrel_adjust
= 0;
176 fixP
->fx_bit_fixP
= 0;
177 fixP
->fx_addnumber
= 0;
180 fixP
->fx_no_overflow
= 0;
184 fixP
->fx_cgen
.insn
= NULL
;
185 fixP
->fx_cgen
.opinfo
= 0;
189 TC_INIT_FIX_DATA (fixP
);
192 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
194 /* Usually, we want relocs sorted numerically, but while
195 comparing to older versions of gas that have relocs
196 reverse sorted, it is convenient to have this compile
197 time option. xoxorich. */
200 fixS
**seg_fix_rootP
= (frags_chained
201 ? &seg_info (now_seg
)->fix_root
202 : &frchain_now
->fix_root
);
203 fixS
**seg_fix_tailP
= (frags_chained
204 ? &seg_info (now_seg
)->fix_tail
205 : &frchain_now
->fix_tail
);
207 #ifdef REVERSE_SORT_RELOCS
209 fixP
->fx_next
= *seg_fix_rootP
;
210 *seg_fix_rootP
= fixP
;
212 #else /* REVERSE_SORT_RELOCS */
214 fixP
->fx_next
= NULL
;
217 (*seg_fix_tailP
)->fx_next
= fixP
;
219 *seg_fix_rootP
= fixP
;
220 *seg_fix_tailP
= fixP
;
222 #endif /* REVERSE_SORT_RELOCS */
228 /* Create a fixup relative to a symbol (plus a constant). */
231 fix_new (fragS
*frag
, /* Which frag? */
232 int where
, /* Where in that frag? */
233 int size
, /* 1, 2, or 4 usually. */
234 symbolS
*add_symbol
, /* X_add_symbol. */
235 offsetT offset
, /* X_add_number. */
236 int pcrel
, /* TRUE if PC-relative relocation. */
237 RELOC_ENUM r_type
/* Relocation type. */)
239 return fix_new_internal (frag
, where
, size
, add_symbol
,
240 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
243 /* Create a fixup for an expression. Currently we only support fixups
244 for difference expressions. That is itself more than most object
245 file formats support anyhow. */
248 fix_new_exp (fragS
*frag
, /* Which frag? */
249 int where
, /* Where in that frag? */
250 int size
, /* 1, 2, or 4 usually. */
251 expressionS
*exp
, /* Expression. */
252 int pcrel
, /* TRUE if PC-relative relocation. */
253 RELOC_ENUM r_type
/* Relocation type. */)
265 as_bad (_("register value used as expression"));
269 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
270 the difference expression cannot immediately be reduced. */
272 symbolS
*stmp
= make_expr_symbol (exp
);
274 exp
->X_op
= O_symbol
;
275 exp
->X_op_symbol
= 0;
276 exp
->X_add_symbol
= stmp
;
277 exp
->X_add_number
= 0;
279 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
283 add
= exp
->X_add_symbol
;
284 off
= exp
->X_add_number
;
285 r_type
= BFD_RELOC_RVA
;
289 sub
= exp
->X_add_symbol
;
290 off
= exp
->X_add_number
;
294 sub
= exp
->X_op_symbol
;
297 add
= exp
->X_add_symbol
;
300 off
= exp
->X_add_number
;
304 add
= make_expr_symbol (exp
);
308 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
, r_type
);
311 /* Generic function to determine whether a fixup requires a relocation. */
313 generic_force_reloc (fixS
*fix
)
315 if (fix
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
316 || fix
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
319 if (fix
->fx_addsy
== NULL
)
322 return S_FORCE_RELOC (fix
->fx_addsy
, fix
->fx_subsy
== NULL
);
325 /* Append a string onto another string, bumping the pointer along. */
327 append (char **charPP
, char *fromP
, unsigned long length
)
329 /* Don't trust memcpy() of 0 chars. */
333 memcpy (*charPP
, fromP
, length
);
337 /* This routine records the largest alignment seen for each segment.
338 If the beginning of the segment is aligned on the worst-case
339 boundary, all of the other alignments within it will work. At
340 least one object format really uses this info. */
343 record_alignment (/* Segment to which alignment pertains. */
345 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
346 boundary, 2 => 4-byte boundary, etc.) */
349 if (seg
== absolute_section
)
352 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
353 bfd_set_section_alignment (stdoutput
, seg
, align
);
357 get_recorded_alignment (segT seg
)
359 if (seg
== absolute_section
)
362 return bfd_get_section_alignment (stdoutput
, seg
);
365 /* Reset the section indices after removing the gas created sections. */
368 renumber_sections (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, PTR countparg
)
370 int *countp
= (int *) countparg
;
372 sec
->index
= *countp
;
377 chain_frchains_together_1 (segT section
, struct frchain
*frchp
)
379 fragS dummy
, *prev_frag
= &dummy
;
380 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
382 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
384 prev_frag
->fr_next
= frchp
->frch_root
;
385 prev_frag
= frchp
->frch_last
;
386 assert (prev_frag
->fr_type
!= 0);
387 if (frchp
->fix_root
!= (fixS
*) NULL
)
389 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
390 seg_info (section
)->fix_root
= frchp
->fix_root
;
391 prev_fix
->fx_next
= frchp
->fix_root
;
392 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
393 prev_fix
= frchp
->fix_tail
;
396 assert (prev_frag
->fr_type
!= 0);
397 prev_frag
->fr_next
= 0;
402 chain_frchains_together (bfd
*abfd ATTRIBUTE_UNUSED
,
404 PTR xxx ATTRIBUTE_UNUSED
)
406 segment_info_type
*info
;
408 /* BFD may have introduced its own sections without using
409 subseg_new, so it is possible that seg_info is NULL. */
410 info
= seg_info (section
);
411 if (info
!= (segment_info_type
*) NULL
)
412 info
->frchainP
->frch_last
413 = chain_frchains_together_1 (section
, info
->frchainP
);
415 /* Now that we've chained the frags together, we must add new fixups
416 to the segment, not to the frag chain. */
421 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED
, fragS
*fragP
)
423 switch (fragP
->fr_type
)
431 HANDLE_ALIGN (fragP
);
433 know (fragP
->fr_next
!= NULL
);
434 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
436 - fragP
->fr_fix
) / fragP
->fr_var
;
437 if (fragP
->fr_offset
< 0)
439 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
440 _("attempt to .org/.space backwards? (%ld)"),
441 (long) fragP
->fr_offset
);
442 fragP
->fr_offset
= 0;
444 fragP
->fr_type
= rs_fill
;
452 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
455 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
458 fragP
->fr_fix
+= size
;
459 fragP
->fr_type
= rs_fill
;
461 fragP
->fr_offset
= 0;
462 fragP
->fr_symbol
= NULL
;
467 eh_frame_convert_frag (fragP
);
471 dwarf2dbg_convert_frag (fragP
);
474 case rs_machine_dependent
:
475 md_convert_frag (stdoutput
, sec
, fragP
);
477 assert (fragP
->fr_next
== NULL
478 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
481 /* After md_convert_frag, we make the frag into a ".space 0".
482 md_convert_frag() should set up any fixSs and constants
487 #ifndef WORKING_DOT_WORD
490 struct broken_word
*lie
;
492 if (fragP
->fr_subtype
)
494 fragP
->fr_fix
+= md_short_jump_size
;
495 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
496 lie
&& lie
->dispfrag
== fragP
;
497 lie
= lie
->next_broken_word
)
499 fragP
->fr_fix
+= md_long_jump_size
;
507 BAD_CASE (fragP
->fr_type
);
511 md_frag_check (fragP
);
515 struct relax_seg_info
522 relax_seg (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *xxx
)
524 segment_info_type
*seginfo
= seg_info (sec
);
525 struct relax_seg_info
*info
= (struct relax_seg_info
*) xxx
;
527 if (seginfo
&& seginfo
->frchainP
528 && relax_segment (seginfo
->frchainP
->frch_root
, sec
, info
->pass
))
532 static void size_seg (bfd
*, asection
*, PTR
);
535 size_seg (bfd
*abfd
, asection
*sec
, PTR xxx ATTRIBUTE_UNUSED
)
539 segment_info_type
*seginfo
;
541 valueT size
, newsize
;
543 subseg_change (sec
, 0);
545 seginfo
= seg_info (sec
);
546 if (seginfo
&& seginfo
->frchainP
)
548 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
549 cvt_frag_to_fill (sec
, fragp
);
550 for (fragp
= seginfo
->frchainP
->frch_root
;
552 fragp
= fragp
->fr_next
)
553 /* Walk to last elt. */
555 size
= fragp
->fr_address
+ fragp
->fr_fix
;
560 flags
= bfd_get_section_flags (abfd
, sec
);
562 if (size
> 0 && ! seginfo
->bss
)
563 flags
|= SEC_HAS_CONTENTS
;
565 /* @@ This is just an approximation. */
566 if (seginfo
&& seginfo
->fix_root
)
570 x
= bfd_set_section_flags (abfd
, sec
, flags
);
573 newsize
= md_section_align (sec
, size
);
574 x
= bfd_set_section_size (abfd
, sec
, newsize
);
577 /* If the size had to be rounded up, add some padding in the last
579 assert (newsize
>= size
);
582 fragS
*last
= seginfo
->frchainP
->frch_last
;
583 fragp
= seginfo
->frchainP
->frch_root
;
584 while (fragp
->fr_next
!= last
)
585 fragp
= fragp
->fr_next
;
586 last
->fr_address
= size
;
587 if ((newsize
- size
) % fragp
->fr_var
== 0)
588 fragp
->fr_offset
+= (newsize
- size
) / fragp
->fr_var
;
590 /* If we hit this abort, it's likely due to subsegs_finish not
591 providing sufficient alignment on the last frag, and the
592 machine dependent code using alignment frags with fr_var
597 #ifdef tc_frob_section
598 tc_frob_section (sec
);
600 #ifdef obj_frob_section
601 obj_frob_section (sec
);
607 dump_section_relocs (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, FILE *stream
)
609 segment_info_type
*seginfo
= seg_info (sec
);
610 fixS
*fixp
= seginfo
->fix_root
;
615 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
618 symbolS
*s
= fixp
->fx_addsy
;
620 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
621 (int) fixp
->fx_r_type
);
623 fprintf (stream
, "no sym\n");
626 print_symbol_value_1 (stream
, s
);
627 fprintf (stream
, "\n");
629 fixp
= fixp
->fx_next
;
633 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
636 #ifndef EMIT_SECTION_SYMBOLS
637 #define EMIT_SECTION_SYMBOLS 1
640 /* This pass over fixups decides whether symbols can be replaced with
644 adjust_reloc_syms (bfd
*abfd ATTRIBUTE_UNUSED
,
646 PTR xxx ATTRIBUTE_UNUSED
)
648 segment_info_type
*seginfo
= seg_info (sec
);
654 dump_section_relocs (abfd
, sec
, stderr
);
656 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
660 else if (fixp
->fx_addsy
)
666 fprintf (stderr
, "\n\nadjusting fixup:\n");
670 sym
= fixp
->fx_addsy
;
672 /* All symbols should have already been resolved at this
673 point. It is possible to see unresolved expression
674 symbols, though, since they are not in the regular symbol
676 resolve_symbol_value (sym
);
678 if (fixp
->fx_subsy
!= NULL
)
679 resolve_symbol_value (fixp
->fx_subsy
);
681 /* If this symbol is equated to an undefined or common symbol,
682 convert the fixup to being against that symbol. */
683 if (symbol_equated_reloc_p (sym
)
684 || S_IS_WEAKREFR (sym
))
686 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
687 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
688 fixp
->fx_addsy
= sym
;
691 if (symbol_mri_common_p (sym
))
693 /* These symbols are handled specially in fixup_segment. */
697 /* If the symbol is undefined, common, weak, or global (ELF
698 shared libs), we can't replace it with the section symbol. */
699 if (S_FORCE_RELOC (fixp
->fx_addsy
, 1))
702 /* Is there some other (target cpu dependent) reason we can't adjust
703 this one? (E.g. relocations involving function addresses on
705 #ifdef tc_fix_adjustable
706 if (! tc_fix_adjustable (fixp
))
710 /* Since we're reducing to section symbols, don't attempt to reduce
711 anything that's already using one. */
712 if (symbol_section_p (sym
))
715 symsec
= S_GET_SEGMENT (sym
);
719 if (bfd_is_abs_section (symsec
))
721 /* The fixup_segment routine normally will not use this
722 symbol in a relocation. */
726 /* Don't try to reduce relocs which refer to non-local symbols
727 in .linkonce sections. It can lead to confusion when a
728 debugging section refers to a .linkonce section. I hope
729 this will always be correct. */
730 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
732 if ((symsec
->flags
& SEC_LINK_ONCE
) != 0
734 /* The GNU toolchain uses an extension for ELF: a
735 section beginning with the magic string
736 .gnu.linkonce is a linkonce section. */
737 && strncmp (segment_name (symsec
), ".gnu.linkonce",
738 sizeof ".gnu.linkonce" - 1) == 0))
742 /* Never adjust a reloc against local symbol in a merge section
743 with non-zero addend. */
744 if ((symsec
->flags
& SEC_MERGE
) != 0
745 && (fixp
->fx_offset
!= 0 || fixp
->fx_subsy
!= NULL
))
748 /* Never adjust a reloc against TLS local symbol. */
749 if ((symsec
->flags
& SEC_THREAD_LOCAL
) != 0)
752 /* We refetch the segment when calling section_symbol, rather
753 than using symsec, because S_GET_VALUE may wind up changing
754 the section when it calls resolve_symbol_value. */
755 fixp
->fx_offset
+= S_GET_VALUE (sym
);
756 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
758 fprintf (stderr
, "\nadjusted fixup:\n");
763 dump_section_relocs (abfd
, sec
, stderr
);
767 fix_segment (bfd
*abfd ATTRIBUTE_UNUSED
,
769 PTR xxx ATTRIBUTE_UNUSED
)
771 segment_info_type
*seginfo
= seg_info (sec
);
773 fixup_segment (seginfo
->fix_root
, sec
);
777 write_relocs (bfd
*abfd
, asection
*sec
, PTR xxx ATTRIBUTE_UNUSED
)
779 segment_info_type
*seginfo
= seg_info (sec
);
786 /* If seginfo is NULL, we did not create this section; don't do
792 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
795 #ifndef RELOC_EXPANSION_POSSIBLE
796 /* Set up reloc information as well. */
797 relocs
= (arelent
**) xcalloc (n
, sizeof (arelent
*));
800 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
803 bfd_reloc_status_type s
;
812 /* If this is an undefined symbol which was equated to another
813 symbol, then generate the reloc against the latter symbol
814 rather than the former. */
815 sym
= fixp
->fx_addsy
;
816 while (symbol_equated_reloc_p (sym
))
820 /* We must avoid looping, as that can occur with a badly
822 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
825 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
828 fixp
->fx_addsy
= sym
;
830 reloc
= tc_gen_reloc (sec
, fixp
);
838 This test is triggered inappropriately for the SH:
839 if (fixp->fx_where + fixp->fx_size
840 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
844 s
= bfd_install_relocation (stdoutput
, reloc
,
845 fixp
->fx_frag
->fr_literal
,
846 fixp
->fx_frag
->fr_address
,
852 case bfd_reloc_overflow
:
853 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
854 _("relocation overflow"));
856 case bfd_reloc_outofrange
:
857 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
858 _("relocation out of range"));
861 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
862 fixp
->fx_file
, fixp
->fx_line
, s
);
867 n
= n
* MAX_RELOC_EXPANSION
;
868 /* Set up reloc information as well. */
869 relocs
= (arelent
**) xcalloc (n
, sizeof (arelent
*));
872 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
875 bfd_reloc_status_type s
;
885 /* If this is an undefined symbol which was equated to another
886 symbol, then generate the reloc against the latter symbol
887 rather than the former. */
888 sym
= fixp
->fx_addsy
;
889 while (symbol_equated_reloc_p (sym
))
893 /* We must avoid looping, as that can occur with a badly
895 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
898 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
901 fixp
->fx_addsy
= sym
;
903 reloc
= tc_gen_reloc (sec
, fixp
);
905 for (j
= 0; reloc
[j
]; j
++)
907 relocs
[i
++] = reloc
[j
];
910 if (fixp
->fx_where
+ fixp
->fx_size
911 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
912 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
913 _("internal error: fixup not contained within frag"));
914 for (j
= 0; reloc
[j
]; j
++)
916 s
= bfd_install_relocation (stdoutput
, reloc
[j
],
917 fixp
->fx_frag
->fr_literal
,
918 fixp
->fx_frag
->fr_address
,
924 case bfd_reloc_overflow
:
925 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
926 _("relocation overflow"));
928 case bfd_reloc_outofrange
:
929 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
930 _("relocation out of range"));
933 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
934 fixp
->fx_file
, fixp
->fx_line
, s
);
943 unsigned int i
, j
, nsyms
;
945 sympp
= bfd_get_outsymbols (stdoutput
);
946 nsyms
= bfd_get_symcount (stdoutput
);
947 for (i
= 0; i
< n
; i
++)
948 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
950 for (j
= 0; j
< nsyms
; j
++)
951 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
960 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
962 bfd_set_section_flags (abfd
, sec
,
963 (bfd_get_section_flags (abfd
, sec
)
964 & (flagword
) ~SEC_RELOC
));
966 #ifdef SET_SECTION_RELOCS
967 SET_SECTION_RELOCS (sec
, relocs
, n
);
975 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
976 for (i
= 0; i
< n
; i
++)
980 fprintf (stderr
, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
981 i
, r
, (unsigned long)r
->address
, s
->name
, (unsigned long)r
->addend
);
988 write_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
990 PTR xxx ATTRIBUTE_UNUSED
)
992 segment_info_type
*seginfo
= seg_info (sec
);
996 /* Write out the frags. */
998 || !(bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1001 for (f
= seginfo
->frchainP
->frch_root
;
1010 assert (f
->fr_type
== rs_fill
);
1013 x
= bfd_set_section_contents (stdoutput
, sec
,
1014 f
->fr_literal
, (file_ptr
) offset
,
1015 (bfd_size_type
) f
->fr_fix
);
1018 bfd_perror (stdoutput
->filename
);
1019 as_perror (_("FATAL: Can't write %s"), stdoutput
->filename
);
1020 exit (EXIT_FAILURE
);
1022 offset
+= f
->fr_fix
;
1024 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1025 fill_size
= f
->fr_var
;
1026 count
= f
->fr_offset
;
1027 assert (count
>= 0);
1028 if (fill_size
&& count
)
1031 if (fill_size
> sizeof (buf
))
1033 /* Do it the old way. Can this ever happen? */
1036 x
= bfd_set_section_contents (stdoutput
, sec
,
1039 (bfd_size_type
) fill_size
);
1042 bfd_perror (stdoutput
->filename
);
1043 as_perror (_("FATAL: Can't write %s"),
1044 stdoutput
->filename
);
1045 exit (EXIT_FAILURE
);
1047 offset
+= fill_size
;
1052 /* Build a buffer full of fill objects and output it as
1053 often as necessary. This saves on the overhead of
1054 potentially lots of bfd_set_section_contents calls. */
1058 n_per_buf
= sizeof (buf
);
1059 memset (buf
, *fill_literal
, n_per_buf
);
1064 n_per_buf
= sizeof (buf
) / fill_size
;
1065 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1066 memcpy (bufp
, fill_literal
, fill_size
);
1068 for (; count
> 0; count
-= n_per_buf
)
1070 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1071 x
= bfd_set_section_contents
1072 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1073 (bfd_size_type
) n_per_buf
* fill_size
);
1075 as_fatal (_("cannot write to output file"));
1076 offset
+= n_per_buf
* fill_size
;
1084 merge_data_into_text (void)
1086 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1087 seg_info (data_section
)->frchainP
->frch_root
;
1088 seg_info (text_section
)->frchainP
->frch_last
=
1089 seg_info (data_section
)->frchainP
->frch_last
;
1090 seg_info (data_section
)->frchainP
= 0;
1100 extern PTR
bfd_alloc (bfd
*, bfd_size_type
);
1102 /* Count symbols. We can't rely on a count made by the loop in
1103 write_object_file, because *_frob_file may add a new symbol or
1106 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1112 bfd_size_type amt
= (bfd_size_type
) nsyms
* sizeof (asymbol
*);
1114 asympp
= (asymbol
**) bfd_alloc (stdoutput
, amt
);
1115 symp
= symbol_rootP
;
1116 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1118 asympp
[i
] = symbol_get_bfdsym (symp
);
1119 symbol_mark_written (symp
);
1124 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1126 symbol_table_frozen
= 1;
1129 /* Finish the subsegments. After every sub-segment, we fake an
1130 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1131 ".fill 0" because that is the kind of frag that requires least
1132 thought. ".align" frags like to have a following frag since that
1133 makes calculating their intended length trivial. */
1135 #ifndef SUB_SEGMENT_ALIGN
1137 /* The last subsegment gets an alignment corresponding to the alignment
1138 of the section. This allows proper nop-filling at the end of
1139 code-bearing sections. */
1140 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1141 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1142 ? get_recorded_alignment (SEG) : 0)
1144 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1149 subsegs_finish (void)
1151 struct frchain
*frchainP
;
1153 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
1157 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
1159 /* This now gets called even if we had errors. In that case,
1160 any alignment is meaningless, and, moreover, will look weird
1161 if we are generating a listing. */
1164 alignment
= SUB_SEGMENT_ALIGN (now_seg
, frchainP
);
1165 if ((bfd_get_section_flags (now_seg
->owner
, now_seg
) & SEC_MERGE
)
1166 && now_seg
->entsize
)
1168 unsigned int entsize
= now_seg
->entsize
;
1171 while ((entsize
& 1) == 0)
1176 if (entalign
> alignment
)
1177 alignment
= entalign
;
1181 if (subseg_text_p (now_seg
))
1182 frag_align_code (alignment
, 0);
1184 frag_align (alignment
, 0, 0);
1186 /* frag_align will have left a new frag.
1187 Use this last frag for an empty ".fill".
1189 For this segment ...
1190 Create a last frag. Do not leave a "being filled in frag". */
1191 frag_wane (frag_now
);
1192 frag_now
->fr_fix
= 0;
1193 know (frag_now
->fr_next
== NULL
);
1197 /* Write the object file. */
1200 write_object_file (void)
1202 struct relax_seg_info rsi
;
1203 #ifndef WORKING_DOT_WORD
1204 fragS
*fragP
; /* Track along all frags. */
1207 /* Do we really want to write it? */
1209 int n_warns
, n_errs
;
1210 n_warns
= had_warnings ();
1211 n_errs
= had_errors ();
1212 /* The -Z flag indicates that an object file should be generated,
1213 regardless of warnings and errors. */
1214 if (flag_always_generate_output
)
1216 if (n_warns
|| n_errs
)
1217 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1218 n_errs
, n_errs
== 1 ? "" : "s",
1219 n_warns
, n_warns
== 1 ? "" : "s");
1224 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1225 n_errs
, n_errs
== 1 ? "" : "s",
1226 n_warns
, n_warns
== 1 ? "" : "s");
1231 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1232 a routine to check for the definition of the procedure "_main",
1233 and if so -- fix it up so that it can be program entry point. */
1234 vms_check_for_main ();
1235 #endif /* OBJ_VMS */
1237 /* From now on, we don't care about sub-segments. Build one frag chain
1238 for each segment. Linked thru fr_next. */
1240 /* Remove the sections created by gas for its own purposes. */
1244 bfd_section_list_remove (stdoutput
, reg_section
);
1245 bfd_section_list_remove (stdoutput
, expr_section
);
1246 stdoutput
->section_count
-= 2;
1248 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1251 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1253 /* We have two segments. If user gave -R flag, then we must put the
1254 data frags into the text segment. Do this before relaxing so
1255 we know to take advantage of -R and make shorter addresses. */
1256 if (flag_readonly_data_in_text
)
1258 merge_data_into_text ();
1264 #ifndef WORKING_DOT_WORD
1265 /* We need to reset the markers in the broken word list and
1266 associated frags between calls to relax_segment (via
1267 relax_seg). Since the broken word list is global, we do it
1268 once per round, rather than locally in relax_segment for each
1270 struct broken_word
*brokp
;
1272 for (brokp
= broken_words
;
1273 brokp
!= (struct broken_word
*) NULL
;
1274 brokp
= brokp
->next_broken_word
)
1278 if (brokp
->dispfrag
!= (fragS
*) NULL
1279 && brokp
->dispfrag
->fr_type
== rs_broken_word
)
1280 brokp
->dispfrag
->fr_subtype
= 0;
1285 bfd_map_over_sections (stdoutput
, relax_seg
, &rsi
);
1291 /* Note - Most ports will use the default value of
1292 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1293 local symbols to be resolved, removing their frag information.
1294 Some ports however, will not have finished relaxing all of
1295 their frags and will still need the local symbol frag
1296 information. These ports can set
1297 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1298 finalize_syms
= TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
;
1300 bfd_map_over_sections (stdoutput
, size_seg
, (char *) 0);
1302 /* Relaxation has completed. Freeze all syms. */
1305 #ifdef md_post_relax_hook
1309 #ifndef WORKING_DOT_WORD
1311 struct broken_word
*lie
;
1312 struct broken_word
**prevP
;
1314 prevP
= &broken_words
;
1315 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1320 subseg_change (lie
->seg
, lie
->subseg
);
1321 exp
.X_op
= O_subtract
;
1322 exp
.X_add_symbol
= lie
->add
;
1323 exp
.X_op_symbol
= lie
->sub
;
1324 exp
.X_add_number
= lie
->addnum
;
1325 #ifdef TC_CONS_FIX_NEW
1326 TC_CONS_FIX_NEW (lie
->frag
,
1327 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1330 fix_new_exp (lie
->frag
,
1331 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1332 2, &exp
, 0, BFD_RELOC_16
);
1334 *prevP
= lie
->next_broken_word
;
1337 prevP
= &(lie
->next_broken_word
);
1339 for (lie
= broken_words
; lie
;)
1341 struct broken_word
*untruth
;
1343 addressT table_addr
;
1344 addressT from_addr
, to_addr
;
1347 subseg_change (lie
->seg
, lie
->subseg
);
1348 fragP
= lie
->dispfrag
;
1350 /* Find out how many broken_words go here. */
1353 untruth
&& untruth
->dispfrag
== fragP
;
1354 untruth
= untruth
->next_broken_word
)
1355 if (untruth
->added
== 1)
1358 table_ptr
= lie
->dispfrag
->fr_opcode
;
1359 table_addr
= (lie
->dispfrag
->fr_address
1360 + (table_ptr
- lie
->dispfrag
->fr_literal
));
1361 /* Create the jump around the long jumps. This is a short
1362 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1363 from_addr
= table_addr
;
1364 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1365 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1367 table_ptr
+= md_short_jump_size
;
1368 table_addr
+= md_short_jump_size
;
1371 lie
&& lie
->dispfrag
== fragP
;
1372 m
++, lie
= lie
->next_broken_word
)
1374 if (lie
->added
== 2)
1376 /* Patch the jump table. */
1377 /* This is the offset from ??? to table_ptr+0. */
1378 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1379 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1380 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr
, lie
);
1382 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1383 for (untruth
= lie
->next_broken_word
;
1384 untruth
&& untruth
->dispfrag
== fragP
;
1385 untruth
= untruth
->next_broken_word
)
1387 if (untruth
->use_jump
== lie
)
1388 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1391 /* Install the long jump. */
1392 /* This is a long jump from table_ptr+0 to the final target. */
1393 from_addr
= table_addr
;
1394 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1395 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1397 table_ptr
+= md_long_jump_size
;
1398 table_addr
+= md_long_jump_size
;
1402 #endif /* not WORKING_DOT_WORD */
1404 /* Resolve symbol values. This needs to be done before processing
1410 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1411 resolve_symbol_value (symp
);
1413 resolve_local_symbol_values ();
1417 #ifdef tc_frob_file_before_adjust
1418 tc_frob_file_before_adjust ();
1420 #ifdef obj_frob_file_before_adjust
1421 obj_frob_file_before_adjust ();
1424 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
1426 #ifdef tc_frob_file_before_fix
1427 tc_frob_file_before_fix ();
1429 #ifdef obj_frob_file_before_fix
1430 obj_frob_file_before_fix ();
1433 bfd_map_over_sections (stdoutput
, fix_segment
, (char *) 0);
1435 /* Set up symbol table, and write it out. */
1439 bfd_boolean skip_next_symbol
= FALSE
;
1441 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1446 if (skip_next_symbol
)
1448 /* Don't do anything besides moving the value of the
1449 symbol from the GAS value-field to the BFD value-field. */
1450 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
1451 skip_next_symbol
= FALSE
;
1455 if (symbol_mri_common_p (symp
))
1457 if (S_IS_EXTERNAL (symp
))
1458 as_bad (_("%s: global symbols not supported in common sections"),
1460 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1464 name
= S_GET_NAME (symp
);
1468 decode_local_label_name ((char *) S_GET_NAME (symp
));
1469 /* They only differ if `name' is a fb or dollar local
1471 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
1472 as_bad (_("local label `%s' is not defined"), name2
);
1475 /* Do it again, because adjust_reloc_syms might introduce
1476 more symbols. They'll probably only be section symbols,
1477 but they'll still need to have the values computed. */
1478 resolve_symbol_value (symp
);
1480 /* Skip symbols which were equated to undefined or common
1482 if (symbol_equated_reloc_p (symp
)
1483 || S_IS_WEAKREFR (symp
))
1485 const char *name
= S_GET_NAME (symp
);
1486 if (S_IS_COMMON (symp
)
1487 && !TC_FAKE_LABEL (name
)
1488 && !S_IS_WEAKREFR (symp
)
1489 && (!S_IS_EXTERNAL (symp
) || S_IS_LOCAL (symp
)))
1491 expressionS
*e
= symbol_get_value_expression (symp
);
1492 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1493 name
, S_GET_NAME (e
->X_add_symbol
));
1495 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1499 #ifdef obj_frob_symbol
1500 obj_frob_symbol (symp
, punt
);
1502 #ifdef tc_frob_symbol
1503 if (! punt
|| symbol_used_in_reloc_p (symp
))
1504 tc_frob_symbol (symp
, punt
);
1507 /* If we don't want to keep this symbol, splice it out of
1508 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1509 want section symbols. Otherwise, we skip local symbols
1510 and symbols that the frob_symbol macros told us to punt,
1511 but we keep such symbols if they are used in relocs. */
1512 if (symp
== abs_section_sym
1513 || (! EMIT_SECTION_SYMBOLS
1514 && symbol_section_p (symp
))
1515 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1516 opposites. Sometimes the former checks flags and the
1517 latter examines the name... */
1518 || (!S_IS_EXTERNAL (symp
)
1519 && (punt
|| S_IS_LOCAL (symp
) ||
1520 (S_IS_WEAKREFD (symp
) && ! symbol_used_p (symp
)))
1521 && ! symbol_used_in_reloc_p (symp
)))
1523 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1525 /* After symbol_remove, symbol_next(symp) still returns
1526 the one that came after it in the chain. So we don't
1527 need to do any extra cleanup work here. */
1531 /* Make sure we really got a value for the symbol. */
1532 if (! symbol_resolved_p (symp
))
1534 as_bad (_("can't resolve value for symbol `%s'"),
1536 symbol_mark_resolved (symp
);
1539 /* Set the value into the BFD symbol. Up til now the value
1540 has only been kept in the gas symbolS struct. */
1541 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
1543 /* A warning construct is a warning symbol followed by the
1544 symbol warned about. Don't let anything object-format or
1545 target-specific muck with it; it's ready for output. */
1546 if (symbol_get_bfdsym (symp
)->flags
& BSF_WARNING
)
1547 skip_next_symbol
= TRUE
;
1553 /* Now do any format-specific adjustments to the symbol table, such
1554 as adding file symbols. */
1555 #ifdef tc_adjust_symtab
1556 tc_adjust_symtab ();
1558 #ifdef obj_adjust_symtab
1559 obj_adjust_symtab ();
1562 /* Now that all the sizes are known, and contents correct, we can
1563 start writing to the file. */
1566 /* If *_frob_file changes the symbol value at this point, it is
1567 responsible for moving the changed value into symp->bsym->value
1568 as well. Hopefully all symbol value changing can be done in
1573 #ifdef obj_frob_file
1577 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
1579 #ifdef tc_frob_file_after_relocs
1580 tc_frob_file_after_relocs ();
1582 #ifdef obj_frob_file_after_relocs
1583 obj_frob_file_after_relocs ();
1586 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
1589 #ifdef TC_GENERIC_RELAX_TABLE
1590 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1593 relax_frag (segT segment
, fragS
*fragP
, long stretch
)
1595 const relax_typeS
*this_type
;
1596 const relax_typeS
*start_type
;
1597 relax_substateT next_state
;
1598 relax_substateT this_state
;
1604 const relax_typeS
*table
;
1606 target
= fragP
->fr_offset
;
1607 address
= fragP
->fr_address
;
1608 table
= TC_GENERIC_RELAX_TABLE
;
1609 this_state
= fragP
->fr_subtype
;
1610 start_type
= this_type
= table
+ this_state
;
1611 symbolP
= fragP
->fr_symbol
;
1617 sym_frag
= symbol_get_frag (symbolP
);
1619 #ifndef DIFF_EXPR_OK
1620 know (sym_frag
!= NULL
);
1622 know (S_GET_SEGMENT (symbolP
) != absolute_section
1623 || sym_frag
== &zero_address_frag
);
1624 target
+= S_GET_VALUE (symbolP
);
1626 /* If frag has yet to be reached on this pass,
1627 assume it will move by STRETCH just as we did.
1628 If this is not so, it will be because some frag
1629 between grows, and that will force another pass. */
1632 && sym_frag
->relax_marker
!= fragP
->relax_marker
1633 && S_GET_SEGMENT (symbolP
) == segment
)
1639 aim
= target
- address
- fragP
->fr_fix
;
1640 #ifdef TC_PCREL_ADJUST
1641 /* Currently only the ns32k family needs this. */
1642 aim
+= TC_PCREL_ADJUST (fragP
);
1645 #ifdef md_prepare_relax_scan
1646 /* Formerly called M68K_AIM_KLUDGE. */
1647 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
1652 /* Look backwards. */
1653 for (next_state
= this_type
->rlx_more
; next_state
;)
1654 if (aim
>= this_type
->rlx_backward
)
1658 /* Grow to next state. */
1659 this_state
= next_state
;
1660 this_type
= table
+ this_state
;
1661 next_state
= this_type
->rlx_more
;
1666 /* Look forwards. */
1667 for (next_state
= this_type
->rlx_more
; next_state
;)
1668 if (aim
<= this_type
->rlx_forward
)
1672 /* Grow to next state. */
1673 this_state
= next_state
;
1674 this_type
= table
+ this_state
;
1675 next_state
= this_type
->rlx_more
;
1679 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
1681 fragP
->fr_subtype
= this_state
;
1685 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
1687 /* Relax_align. Advance location counter to next address that has 'alignment'
1688 lowest order bits all 0s, return size of adjustment made. */
1689 static relax_addressT
1690 relax_align (register relax_addressT address
, /* Address now. */
1691 register int alignment
/* Alignment (binary). */)
1693 relax_addressT mask
;
1694 relax_addressT new_address
;
1696 mask
= ~((~0) << alignment
);
1697 new_address
= (address
+ mask
) & (~mask
);
1698 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1700 /* We must provide lots of padding, so the linker can discard it
1701 when needed. The linker will not add extra space, ever. */
1702 new_address
+= (1 << alignment
);
1704 return (new_address
- address
);
1707 /* Now we have a segment, not a crowd of sub-segments, we can make
1712 After this, all frags in this segment have addresses that are correct
1713 within the segment. Since segments live in different file addresses,
1714 these frag addresses may not be the same as final object-file
1718 relax_segment (struct frag
*segment_frag_root
, segT segment
, int pass
)
1720 unsigned long frag_count
;
1722 relax_addressT address
;
1725 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1726 subseg_change (segment
, 0);
1728 /* For each frag in segment: count and store (a 1st guess of)
1731 for (frag_count
= 0, fragP
= segment_frag_root
;
1733 fragP
= fragP
->fr_next
, frag_count
++)
1735 fragP
->relax_marker
= 0;
1736 fragP
->fr_address
= address
;
1737 address
+= fragP
->fr_fix
;
1739 switch (fragP
->fr_type
)
1742 address
+= fragP
->fr_offset
* fragP
->fr_var
;
1749 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
1751 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
1754 if (offset
% fragP
->fr_var
!= 0)
1756 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
1757 _("alignment padding (%lu bytes) not a multiple of %ld"),
1758 (unsigned long) offset
, (long) fragP
->fr_var
);
1759 offset
-= (offset
% fragP
->fr_var
);
1768 /* Assume .org is nugatory. It will grow with 1st relax. */
1771 case rs_machine_dependent
:
1772 /* If fr_symbol is an expression, this call to
1773 resolve_symbol_value sets up the correct segment, which will
1774 likely be needed in md_estimate_size_before_relax. */
1775 if (fragP
->fr_symbol
)
1776 resolve_symbol_value (fragP
->fr_symbol
);
1778 address
+= md_estimate_size_before_relax (fragP
, segment
);
1781 #ifndef WORKING_DOT_WORD
1782 /* Broken words don't concern us yet. */
1783 case rs_broken_word
:
1788 /* Initial guess is always 1; doing otherwise can result in
1789 stable solutions that are larger than the minimum. */
1790 address
+= fragP
->fr_offset
= 1;
1794 address
+= eh_frame_estimate_size_before_relax (fragP
);
1798 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
1802 BAD_CASE (fragP
->fr_type
);
1809 unsigned long max_iterations
;
1810 offsetT stretch
; /* May be any size, 0 or negative. */
1811 /* Cumulative number of addresses we have relaxed this pass.
1812 We may have relaxed more than one address. */
1813 int stretched
; /* Have we stretched on this pass? */
1814 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1815 grew, and another shrank. If a branch instruction doesn't fit anymore,
1816 we could be scrod. */
1818 /* We want to prevent going into an infinite loop where one frag grows
1819 depending upon the location of a symbol which is in turn moved by
1820 the growing frag. eg:
1826 So we dictate that this algorithm can be at most O2. */
1827 max_iterations
= frag_count
* frag_count
;
1828 /* Check for overflow. */
1829 if (max_iterations
< frag_count
)
1830 max_iterations
= frag_count
;
1838 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1841 addressT was_address
;
1845 fragP
->relax_marker
^= 1;
1846 was_address
= fragP
->fr_address
;
1847 address
= fragP
->fr_address
+= stretch
;
1848 symbolP
= fragP
->fr_symbol
;
1849 offset
= fragP
->fr_offset
;
1851 switch (fragP
->fr_type
)
1853 case rs_fill
: /* .fill never relaxes. */
1857 #ifndef WORKING_DOT_WORD
1858 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1859 for it I do not want to write it. I do not want to have
1860 anything to do with it. This is not the proper way to
1861 implement this misfeature. */
1862 case rs_broken_word
:
1864 struct broken_word
*lie
;
1865 struct broken_word
*untruth
;
1867 /* Yes this is ugly (storing the broken_word pointer
1868 in the symbol slot). Still, this whole chunk of
1869 code is ugly, and I don't feel like doing anything
1870 about it. Think of it as stubbornness in action. */
1872 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
1873 lie
&& lie
->dispfrag
== fragP
;
1874 lie
= lie
->next_broken_word
)
1880 offset
= (S_GET_VALUE (lie
->add
)
1882 - S_GET_VALUE (lie
->sub
));
1883 if (offset
<= -32768 || offset
>= 32767)
1885 if (flag_warn_displacement
)
1888 sprint_value (buf
, (addressT
) lie
->addnum
);
1889 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
1890 _(".word %s-%s+%s didn't fit"),
1891 S_GET_NAME (lie
->add
),
1892 S_GET_NAME (lie
->sub
),
1896 if (fragP
->fr_subtype
== 0)
1898 fragP
->fr_subtype
++;
1899 growth
+= md_short_jump_size
;
1901 for (untruth
= lie
->next_broken_word
;
1902 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
1903 untruth
= untruth
->next_broken_word
)
1904 if ((symbol_get_frag (untruth
->add
)
1905 == symbol_get_frag (lie
->add
))
1906 && (S_GET_VALUE (untruth
->add
)
1907 == S_GET_VALUE (lie
->add
)))
1910 untruth
->use_jump
= lie
;
1912 growth
+= md_long_jump_size
;
1917 } /* case rs_broken_word */
1923 addressT oldoff
, newoff
;
1925 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
1927 newoff
= relax_align (address
+ fragP
->fr_fix
,
1930 if (fragP
->fr_subtype
!= 0)
1932 if (oldoff
> fragP
->fr_subtype
)
1934 if (newoff
> fragP
->fr_subtype
)
1938 growth
= newoff
- oldoff
;
1944 addressT target
= offset
;
1949 /* Convert from an actual address to an octet offset
1950 into the section. Here it is assumed that the
1951 section's VMA is zero, and can omit subtracting it
1952 from the symbol's value to get the address offset. */
1953 know (S_GET_SEGMENT (symbolP
)->vma
== 0);
1954 target
+= S_GET_VALUE (symbolP
) * OCTETS_PER_BYTE
;
1957 know (fragP
->fr_next
);
1958 after
= fragP
->fr_next
->fr_address
;
1959 growth
= target
- after
;
1964 /* Don't error on first few frag relax passes.
1965 The symbol might be an expression involving
1966 symbol values from other sections. If those
1967 sections have not yet been processed their
1968 frags will all have zero addresses, so we
1969 will calculate incorrect values for them. The
1970 number of passes we allow before giving an
1971 error is somewhat arbitrary. It should be at
1972 least one, with larger values requiring
1973 increasingly contrived dependencies between
1974 frags to trigger a false error. */
1977 /* Force another pass. */
1982 /* Growth may be negative, but variable part of frag
1983 cannot have fewer than 0 chars. That is, we can't
1985 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
1986 _("attempt to move .org backwards"));
1988 /* We've issued an error message. Change the
1989 frag to avoid cascading errors. */
1990 fragP
->fr_type
= rs_align
;
1991 fragP
->fr_subtype
= 0;
1992 fragP
->fr_offset
= 0;
1993 fragP
->fr_fix
= after
- was_address
;
1997 /* This is an absolute growth factor */
2008 amount
= S_GET_VALUE (symbolP
);
2009 if (S_GET_SEGMENT (symbolP
) != absolute_section
2010 || S_IS_COMMON (symbolP
)
2011 || ! S_IS_DEFINED (symbolP
))
2013 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2014 _(".space specifies non-absolute value"));
2015 /* Prevent repeat of this error message. */
2016 fragP
->fr_symbol
= 0;
2018 else if (amount
< 0)
2020 /* Don't error on first few frag relax passes.
2021 See rs_org comment for a longer explanation. */
2028 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2029 _(".space or .fill with negative value, ignored"));
2030 fragP
->fr_symbol
= 0;
2033 growth
= (was_address
+ fragP
->fr_fix
+ amount
2034 - fragP
->fr_next
->fr_address
);
2038 case rs_machine_dependent
:
2039 #ifdef md_relax_frag
2040 growth
= md_relax_frag (segment
, fragP
, stretch
);
2042 #ifdef TC_GENERIC_RELAX_TABLE
2043 /* The default way to relax a frag is to look through
2044 TC_GENERIC_RELAX_TABLE. */
2045 growth
= relax_frag (segment
, fragP
, stretch
);
2046 #endif /* TC_GENERIC_RELAX_TABLE */
2055 value
= resolve_symbol_value (fragP
->fr_symbol
);
2056 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2057 growth
= size
- fragP
->fr_offset
;
2058 fragP
->fr_offset
= size
;
2063 growth
= eh_frame_relax_frag (fragP
);
2067 growth
= dwarf2dbg_relax_frag (fragP
);
2071 BAD_CASE (fragP
->fr_type
);
2081 /* Until nothing further to relax. */
2082 while (stretched
&& -- max_iterations
);
2085 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2086 segment_name (segment
));
2089 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2090 if (fragP
->last_fr_address
!= fragP
->fr_address
)
2092 fragP
->last_fr_address
= fragP
->fr_address
;
2100 Go through all the fixS's in a segment and see which ones can be
2101 handled now. (These consist of fixS where we have since discovered
2102 the value of a symbol, or the address of the frag involved.)
2103 For each one, call md_apply_fix to put the fix into the frag data.
2105 Result is a count of how many relocation structs will be needed to
2106 handle the remaining fixS's that we couldn't completely handle here.
2107 These will be output later by emit_relocations(). */
2110 fixup_segment (fixS
*fixP
, segT this_segment
)
2112 long seg_reloc_count
= 0;
2115 segT add_symbol_segment
= absolute_section
;
2117 if (fixP
!= NULL
&& abs_section_sym
== NULL
)
2118 abs_section_sym
= section_symbol (absolute_section
);
2120 /* If the linker is doing the relaxing, we must not do any fixups.
2122 Well, strictly speaking that's not true -- we could do any that
2123 are PC-relative and don't cross regions that could change size.
2124 And for the i960 we might be able to turn callx/callj into bal
2125 anyways in cases where we know the maximum displacement. */
2126 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment
))
2128 for (; fixP
; fixP
= fixP
->fx_next
)
2131 if (fixP
->fx_addsy
== NULL
)
2133 /* There was no symbol required by this relocation.
2134 However, BFD doesn't really handle relocations
2135 without symbols well. So fake up a local symbol in
2136 the absolute section. */
2137 fixP
->fx_addsy
= abs_section_sym
;
2139 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2140 if (fixP
->fx_subsy
!= NULL
)
2141 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
2144 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2145 return seg_reloc_count
;
2148 for (; fixP
; fixP
= fixP
->fx_next
)
2151 fprintf (stderr
, "\nprocessing fixup:\n");
2155 fragP
= fixP
->fx_frag
;
2157 #ifdef TC_VALIDATE_FIX
2158 TC_VALIDATE_FIX (fixP
, this_segment
, skip
);
2160 add_number
= fixP
->fx_offset
;
2162 if (fixP
->fx_addsy
!= NULL
2163 && symbol_mri_common_p (fixP
->fx_addsy
))
2165 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2166 fixP
->fx_offset
= add_number
;
2168 = symbol_get_value_expression (fixP
->fx_addsy
)->X_add_symbol
;
2171 if (fixP
->fx_addsy
!= NULL
)
2172 add_symbol_segment
= S_GET_SEGMENT (fixP
->fx_addsy
);
2174 if (fixP
->fx_subsy
!= NULL
)
2176 segT sub_symbol_segment
;
2177 resolve_symbol_value (fixP
->fx_subsy
);
2178 sub_symbol_segment
= S_GET_SEGMENT (fixP
->fx_subsy
);
2179 if (fixP
->fx_addsy
!= NULL
2180 && sub_symbol_segment
== add_symbol_segment
2181 && !TC_FORCE_RELOCATION_SUB_SAME (fixP
, add_symbol_segment
))
2183 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2184 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
2185 fixP
->fx_offset
= add_number
;
2186 fixP
->fx_addsy
= NULL
;
2187 fixP
->fx_subsy
= NULL
;
2189 /* See the comment below about 68k weirdness. */
2193 else if (sub_symbol_segment
== absolute_section
2194 && !TC_FORCE_RELOCATION_SUB_ABS (fixP
))
2196 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
2197 fixP
->fx_offset
= add_number
;
2198 fixP
->fx_subsy
= NULL
;
2200 else if (sub_symbol_segment
== this_segment
2201 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP
))
2203 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
2204 fixP
->fx_offset
= (add_number
+ fixP
->fx_dot_value
2205 + fixP
->fx_frag
->fr_address
);
2207 /* Make it pc-relative. If the back-end code has not
2208 selected a pc-relative reloc, cancel the adjustment
2209 we do later on all pc-relative relocs. */
2212 /* Do this for m68k even if it's already described
2213 as pc-relative. On the m68k, an operand of
2214 "pc@(foo-.-2)" should address "foo" in a
2215 pc-relative mode. */
2219 add_number
+= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
2220 fixP
->fx_subsy
= NULL
;
2223 else if (!TC_VALIDATE_FIX_SUB (fixP
))
2225 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2226 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2227 fixP
->fx_addsy
? S_GET_NAME (fixP
->fx_addsy
) : "0",
2228 segment_name (add_symbol_segment
),
2229 S_GET_NAME (fixP
->fx_subsy
),
2230 segment_name (sub_symbol_segment
));
2236 if (add_symbol_segment
== this_segment
2237 && !TC_FORCE_RELOCATION_LOCAL (fixP
))
2239 /* This fixup was made when the symbol's segment was
2240 SEG_UNKNOWN, but it is now in the local segment.
2241 So we know how to do the address without relocation. */
2242 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2243 fixP
->fx_offset
= add_number
;
2245 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
2246 fixP
->fx_addsy
= NULL
;
2249 else if (add_symbol_segment
== absolute_section
2250 && !TC_FORCE_RELOCATION_ABS (fixP
))
2252 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2253 fixP
->fx_offset
= add_number
;
2254 fixP
->fx_addsy
= NULL
;
2256 else if (add_symbol_segment
!= undefined_section
2257 && ! bfd_is_com_section (add_symbol_segment
)
2258 && MD_APPLY_SYM_VALUE (fixP
))
2259 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2264 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
2265 if (!fixP
->fx_done
&& fixP
->fx_addsy
== NULL
)
2267 /* There was no symbol required by this relocation.
2268 However, BFD doesn't really handle relocations
2269 without symbols well. So fake up a local symbol in
2270 the absolute section. */
2271 fixP
->fx_addsy
= abs_section_sym
;
2276 md_apply_fix (fixP
, &add_number
, this_segment
);
2281 if (fixP
->fx_addsy
== NULL
)
2282 fixP
->fx_addsy
= abs_section_sym
;
2283 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2284 if (fixP
->fx_subsy
!= NULL
)
2285 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
2288 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& fixP
->fx_size
!= 0)
2290 if (fixP
->fx_size
< sizeof (valueT
))
2295 mask
--; /* Set all bits to one. */
2296 mask
<<= fixP
->fx_size
* 8 - (fixP
->fx_signed
? 1 : 0);
2297 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
2299 char buf
[50], buf2
[50];
2300 sprint_value (buf
, fragP
->fr_address
+ fixP
->fx_where
);
2301 if (add_number
> 1000)
2302 sprint_value (buf2
, add_number
);
2304 sprintf (buf2
, "%ld", (long) add_number
);
2305 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2306 _("value of %s too large for field of %d bytes at %s"),
2307 buf2
, fixP
->fx_size
, buf
);
2308 } /* Generic error checking. */
2310 #ifdef WARN_SIGNED_OVERFLOW_WORD
2311 /* Warn if a .word value is too large when treated as a signed
2312 number. We already know it is not too negative. This is to
2313 catch over-large switches generated by gcc on the 68k. */
2314 if (!flag_signed_overflow_ok
2315 && fixP
->fx_size
== 2
2316 && add_number
> 0x7fff)
2317 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2318 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2320 (long) (fragP
->fr_address
+ fixP
->fx_where
));
2322 } /* Not a bit fix. */
2324 #ifdef TC_VALIDATE_FIX
2325 skip
: ATTRIBUTE_UNUSED_LABEL
2329 fprintf (stderr
, "result:\n");
2332 } /* For each fixS in this segment. */
2334 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2335 return seg_reloc_count
;
2339 number_to_chars_bigendian (char *buf
, valueT val
, int n
)
2345 buf
[n
] = val
& 0xff;
2351 number_to_chars_littleendian (char *buf
, valueT val
, int n
)
2357 *buf
++ = val
& 0xff;
2363 write_print_statistics (FILE *file
)
2365 fprintf (file
, "fixups: %d\n", n_fixups
);
2368 /* For debugging. */
2369 extern int indent_level
;
2372 print_fixup (fixS
*fixp
)
2375 fprintf (stderr
, "fix %lx %s:%d", (long) fixp
, fixp
->fx_file
, fixp
->fx_line
);
2377 fprintf (stderr
, " pcrel");
2378 if (fixp
->fx_pcrel_adjust
)
2379 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2380 if (fixp
->fx_im_disp
)
2383 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2385 fprintf (stderr
, " im_disp");
2389 fprintf (stderr
, " tcbit");
2391 fprintf (stderr
, " done");
2392 fprintf (stderr
, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2393 fixp
->fx_size
, (long) fixp
->fx_frag
, (long) fixp
->fx_where
,
2394 (long) fixp
->fx_offset
, (long) fixp
->fx_addnumber
);
2395 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2399 fprintf (stderr
, "\n +<");
2400 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2401 fprintf (stderr
, ">");
2405 fprintf (stderr
, "\n -<");
2406 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2407 fprintf (stderr
, ">");
2409 fprintf (stderr
, "\n");
2410 #ifdef TC_FIX_DATA_PRINT
2411 TC_FIX_DATA_PRINT (stderr
, fixp
);