1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
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, 59 Temple Place - Suite 330, Boston, MA
23 /* This thing should be set up to do byteordering correctly. But... */
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
31 /* This looks like a good idea. Let's try turning it on always, for now. */
32 #undef BFD_FAST_SECTION_FILL
33 #define BFD_FAST_SECTION_FILL
35 #ifndef TC_ADJUST_RELOC_COUNT
36 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
39 #ifndef TC_FORCE_RELOCATION
40 #define TC_FORCE_RELOCATION(FIXP) 0
43 #ifndef TC_FORCE_RELOCATION_SECTION
44 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
47 #ifndef TC_LINKRELAX_FIXUP
48 #define TC_LINKRELAX_FIXUP(SEG) 1
51 #ifndef TC_FIX_ADJUSTABLE
52 #define TC_FIX_ADJUSTABLE(fix) 1
55 #ifndef MD_PCREL_FROM_SECTION
56 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
59 #ifndef WORKING_DOT_WORD
60 extern CONST
int md_short_jump_size
;
61 extern CONST
int md_long_jump_size
;
64 /* Used to control final evaluation of expressions that are more
65 complex than symbol + constant. 1 means set final value for simple
66 expressions, 2 means set final value for more complex expressions. */
67 int finalize_syms
= 1;
69 int symbol_table_frozen
;
70 void print_fixup
PARAMS ((fixS
*));
73 static void renumber_sections
PARAMS ((bfd
*, asection
*, PTR
));
75 /* We generally attach relocs to frag chains. However, after we have
76 chained these all together into a segment, any relocs we add after
77 that must be attached to a segment. This will include relocs added
78 in md_estimate_size_for_relax, for example. */
79 static int frags_chained
= 0;
85 struct frag
*text_frag_root
;
86 struct frag
*data_frag_root
;
87 struct frag
*bss_frag_root
;
89 struct frag
*text_last_frag
; /* Last frag in segment. */
90 struct frag
*data_last_frag
; /* Last frag in segment. */
91 static struct frag
*bss_last_frag
; /* Last frag in segment. */
95 static object_headers headers
;
98 long string_byte_count
;
99 char *next_object_file_charP
; /* Tracks object file bytes. */
102 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
105 #endif /* BFD_ASSEMBLER */
110 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
111 symbolS
*add
, symbolS
*sub
,
112 offsetT offset
, int pcrel
,
113 bfd_reloc_code_real_type r_type
));
115 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
116 symbolS
*add
, symbolS
*sub
,
117 offsetT offset
, int pcrel
,
120 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
121 static long fixup_segment
PARAMS ((fixS
* fixP
, segT this_segment_type
));
123 static relax_addressT relax_align
PARAMS ((relax_addressT addr
, int align
));
124 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
125 static fragS
*chain_frchains_together_1
PARAMS ((segT
, struct frchain
*));
128 static void chain_frchains_together
PARAMS ((bfd
*, segT
, PTR
));
129 static void cvt_frag_to_fill
PARAMS ((segT
, fragS
*));
130 static void adjust_reloc_syms
PARAMS ((bfd
*, asection
*, PTR
));
131 static void write_relocs
PARAMS ((bfd
*, asection
*, PTR
));
132 static void write_contents
PARAMS ((bfd
*, asection
*, PTR
));
133 static void set_symtab
PARAMS ((void));
135 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
136 static void merge_data_into_text
PARAMS ((void));
138 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
139 static void cvt_frag_to_fill
PARAMS ((object_headers
*, segT
, fragS
*));
140 static void remove_subsegs
PARAMS ((frchainS
*, int, fragS
**, fragS
**));
141 static void relax_and_size_all_segments
PARAMS ((void));
143 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
144 static void set_segment_vma
PARAMS ((bfd
*, asection
*, PTR
));
147 /* Create a fixS in obstack 'notes'. */
150 fix_new_internal (frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
,
152 fragS
*frag
; /* Which frag? */
153 int where
; /* Where in that frag? */
154 int size
; /* 1, 2, or 4 usually. */
155 symbolS
*add_symbol
; /* X_add_symbol. */
156 symbolS
*sub_symbol
; /* X_op_symbol. */
157 offsetT offset
; /* X_add_number. */
158 int pcrel
; /* TRUE if PC-relative relocation. */
160 bfd_reloc_code_real_type r_type
; /* Relocation type. */
162 int r_type
; /* Relocation type. */
169 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
171 fixP
->fx_frag
= frag
;
172 fixP
->fx_where
= where
;
173 fixP
->fx_size
= size
;
174 /* We've made fx_size a narrow field; check that it's wide enough. */
175 if (fixP
->fx_size
!= size
)
177 as_bad (_("field fx_size too small to hold %d"), size
);
180 fixP
->fx_addsy
= add_symbol
;
181 fixP
->fx_subsy
= sub_symbol
;
182 fixP
->fx_offset
= offset
;
183 fixP
->fx_pcrel
= pcrel
;
185 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
186 fixP
->fx_r_type
= r_type
;
188 fixP
->fx_im_disp
= 0;
189 fixP
->fx_pcrel_adjust
= 0;
190 fixP
->fx_bit_fixP
= 0;
191 fixP
->fx_addnumber
= 0;
194 fixP
->fx_no_overflow
= 0;
198 fixP
->fx_cgen
.insn
= NULL
;
199 fixP
->fx_cgen
.opinfo
= 0;
203 TC_INIT_FIX_DATA (fixP
);
206 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
208 /* Usually, we want relocs sorted numerically, but while
209 comparing to older versions of gas that have relocs
210 reverse sorted, it is convenient to have this compile
211 time option. xoxorich. */
215 fixS
**seg_fix_rootP
= (frags_chained
216 ? &seg_info (now_seg
)->fix_root
217 : &frchain_now
->fix_root
);
218 fixS
**seg_fix_tailP
= (frags_chained
219 ? &seg_info (now_seg
)->fix_tail
220 : &frchain_now
->fix_tail
);
223 #ifdef REVERSE_SORT_RELOCS
225 fixP
->fx_next
= *seg_fix_rootP
;
226 *seg_fix_rootP
= fixP
;
228 #else /* REVERSE_SORT_RELOCS */
230 fixP
->fx_next
= NULL
;
233 (*seg_fix_tailP
)->fx_next
= fixP
;
235 *seg_fix_rootP
= fixP
;
236 *seg_fix_tailP
= fixP
;
238 #endif /* REVERSE_SORT_RELOCS */
244 /* Create a fixup relative to a symbol (plus a constant). */
247 fix_new (frag
, where
, size
, add_symbol
, offset
, pcrel
, r_type
)
248 fragS
*frag
; /* Which frag? */
249 int where
; /* Where in that frag? */
250 int size
; /* 1, 2, or 4 usually. */
251 symbolS
*add_symbol
; /* X_add_symbol. */
252 offsetT offset
; /* X_add_number. */
253 int pcrel
; /* TRUE if PC-relative relocation. */
255 bfd_reloc_code_real_type r_type
; /* Relocation type. */
257 int r_type
; /* Relocation type. */
260 return fix_new_internal (frag
, where
, size
, add_symbol
,
261 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
264 /* Create a fixup for an expression. Currently we only support fixups
265 for difference expressions. That is itself more than most object
266 file formats support anyhow. */
269 fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
)
270 fragS
*frag
; /* Which frag? */
271 int where
; /* Where in that frag? */
272 int size
; /* 1, 2, or 4 usually. */
273 expressionS
*exp
; /* Expression. */
274 int pcrel
; /* TRUE if PC-relative relocation. */
276 bfd_reloc_code_real_type r_type
; /* Relocation type. */
278 int r_type
; /* Relocation type. */
291 as_bad (_("register value used as expression"));
295 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
296 the difference expression cannot immediately be reduced. */
298 symbolS
*stmp
= make_expr_symbol (exp
);
300 exp
->X_op
= O_symbol
;
301 exp
->X_op_symbol
= 0;
302 exp
->X_add_symbol
= stmp
;
303 exp
->X_add_number
= 0;
305 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
309 add
= exp
->X_add_symbol
;
310 off
= exp
->X_add_number
;
312 #if defined(BFD_ASSEMBLER)
313 r_type
= BFD_RELOC_RVA
;
315 #if defined(TC_RVA_RELOC)
316 r_type
= TC_RVA_RELOC
;
318 as_fatal (_("rva not supported"));
324 sub
= exp
->X_add_symbol
;
325 off
= exp
->X_add_number
;
329 sub
= exp
->X_op_symbol
;
332 add
= exp
->X_add_symbol
;
335 off
= exp
->X_add_number
;
339 add
= make_expr_symbol (exp
);
343 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
, r_type
);
346 /* Append a string onto another string, bumping the pointer along. */
348 append (charPP
, fromP
, length
)
351 unsigned long length
;
353 /* Don't trust memcpy() of 0 chars. */
357 memcpy (*charPP
, fromP
, length
);
361 #ifndef BFD_ASSEMBLER
362 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
365 /* This routine records the largest alignment seen for each segment.
366 If the beginning of the segment is aligned on the worst-case
367 boundary, all of the other alignments within it will work. At
368 least one object format really uses this info. */
371 record_alignment (seg
, align
)
372 /* Segment to which alignment pertains. */
374 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
378 if (seg
== absolute_section
)
381 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
382 bfd_set_section_alignment (stdoutput
, seg
, align
);
384 if (align
> section_alignment
[(int) seg
])
385 section_alignment
[(int) seg
] = align
;
390 get_recorded_alignment (seg
)
393 if (seg
== absolute_section
)
396 return bfd_get_section_alignment (stdoutput
, seg
);
398 return section_alignment
[(int) seg
];
404 /* Reset the section indices after removing the gas created sections. */
407 renumber_sections (abfd
, sec
, countparg
)
408 bfd
*abfd ATTRIBUTE_UNUSED
;
412 int *countp
= (int *) countparg
;
414 sec
->index
= *countp
;
418 #endif /* defined (BFD_ASSEMBLER) */
420 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
423 chain_frchains_together_1 (section
, frchp
)
425 struct frchain
*frchp
;
427 fragS dummy
, *prev_frag
= &dummy
;
429 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
432 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
434 prev_frag
->fr_next
= frchp
->frch_root
;
435 prev_frag
= frchp
->frch_last
;
436 assert (prev_frag
->fr_type
!= 0);
438 if (frchp
->fix_root
!= (fixS
*) NULL
)
440 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
441 seg_info (section
)->fix_root
= frchp
->fix_root
;
442 prev_fix
->fx_next
= frchp
->fix_root
;
443 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
444 prev_fix
= frchp
->fix_tail
;
448 assert (prev_frag
->fr_type
!= 0);
449 prev_frag
->fr_next
= 0;
458 chain_frchains_together (abfd
, section
, xxx
)
459 bfd
*abfd ATTRIBUTE_UNUSED
;
461 PTR xxx ATTRIBUTE_UNUSED
;
463 segment_info_type
*info
;
465 /* BFD may have introduced its own sections without using
466 subseg_new, so it is possible that seg_info is NULL. */
467 info
= seg_info (section
);
468 if (info
!= (segment_info_type
*) NULL
)
469 info
->frchainP
->frch_last
470 = chain_frchains_together_1 (section
, info
->frchainP
);
472 /* Now that we've chained the frags together, we must add new fixups
473 to the segment, not to the frag chain. */
479 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
482 remove_subsegs (head
, seg
, root
, last
)
488 *root
= head
->frch_root
;
489 *last
= chain_frchains_together_1 (seg
, head
);
494 #if defined (BFD_ASSEMBLER) || !defined (BFD)
498 cvt_frag_to_fill (sec
, fragP
)
499 segT sec ATTRIBUTE_UNUSED
;
503 cvt_frag_to_fill (headersP
, sec
, fragP
)
504 object_headers
*headersP
;
509 switch (fragP
->fr_type
)
517 HANDLE_ALIGN (fragP
);
519 know (fragP
->fr_next
!= NULL
);
520 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
522 - fragP
->fr_fix
) / fragP
->fr_var
;
523 if (fragP
->fr_offset
< 0)
525 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
526 _("attempt to .org/.space backwards? (%ld)"),
527 (long) fragP
->fr_offset
);
529 fragP
->fr_type
= rs_fill
;
537 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
540 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
543 fragP
->fr_fix
+= size
;
544 fragP
->fr_type
= rs_fill
;
546 fragP
->fr_offset
= 0;
547 fragP
->fr_symbol
= NULL
;
552 eh_frame_convert_frag (fragP
);
556 dwarf2dbg_convert_frag (fragP
);
559 case rs_machine_dependent
:
561 md_convert_frag (stdoutput
, sec
, fragP
);
563 md_convert_frag (headersP
, sec
, fragP
);
566 assert (fragP
->fr_next
== NULL
567 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
570 /* After md_convert_frag, we make the frag into a ".space 0".
571 md_convert_frag() should set up any fixSs and constants
576 #ifndef WORKING_DOT_WORD
579 struct broken_word
*lie
;
581 if (fragP
->fr_subtype
)
583 fragP
->fr_fix
+= md_short_jump_size
;
584 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
585 lie
&& lie
->dispfrag
== fragP
;
586 lie
= lie
->next_broken_word
)
588 fragP
->fr_fix
+= md_long_jump_size
;
596 BAD_CASE (fragP
->fr_type
);
601 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
604 static void relax_seg
PARAMS ((bfd
*, asection
*, PTR
));
606 relax_seg (abfd
, sec
, xxx
)
607 bfd
*abfd ATTRIBUTE_UNUSED
;
611 segment_info_type
*seginfo
= seg_info (sec
);
613 if (seginfo
&& seginfo
->frchainP
614 && relax_segment (seginfo
->frchainP
->frch_root
, sec
))
616 int *result
= (int *) xxx
;
621 static void size_seg
PARAMS ((bfd
*, asection
*, PTR
));
623 size_seg (abfd
, sec
, xxx
)
626 PTR xxx ATTRIBUTE_UNUSED
;
630 segment_info_type
*seginfo
;
632 valueT size
, newsize
;
634 subseg_change (sec
, 0);
636 seginfo
= seg_info (sec
);
637 if (seginfo
&& seginfo
->frchainP
)
639 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
640 cvt_frag_to_fill (sec
, fragp
);
641 for (fragp
= seginfo
->frchainP
->frch_root
;
643 fragp
= fragp
->fr_next
)
644 /* Walk to last elt. */
646 size
= fragp
->fr_address
+ fragp
->fr_fix
;
651 flags
= bfd_get_section_flags (abfd
, sec
);
653 if (size
> 0 && ! seginfo
->bss
)
654 flags
|= SEC_HAS_CONTENTS
;
656 /* @@ This is just an approximation. */
657 if (seginfo
&& seginfo
->fix_root
)
661 x
= bfd_set_section_flags (abfd
, sec
, flags
);
664 newsize
= md_section_align (sec
, size
);
665 x
= bfd_set_section_size (abfd
, sec
, newsize
);
668 /* If the size had to be rounded up, add some padding in the last
670 assert (newsize
>= size
);
673 fragS
*last
= seginfo
->frchainP
->frch_last
;
674 fragp
= seginfo
->frchainP
->frch_root
;
675 while (fragp
->fr_next
!= last
)
676 fragp
= fragp
->fr_next
;
677 last
->fr_address
= size
;
678 fragp
->fr_offset
+= newsize
- size
;
681 #ifdef tc_frob_section
682 tc_frob_section (sec
);
684 #ifdef obj_frob_section
685 obj_frob_section (sec
);
691 dump_section_relocs (abfd
, sec
, stream_
)
692 bfd
*abfd ATTRIBUTE_UNUSED
;
696 FILE *stream
= (FILE *) stream_
;
697 segment_info_type
*seginfo
= seg_info (sec
);
698 fixS
*fixp
= seginfo
->fix_root
;
703 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
706 symbolS
*s
= fixp
->fx_addsy
;
708 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
709 (int) fixp
->fx_r_type
);
711 fprintf (stream
, "no sym\n");
714 print_symbol_value_1 (stream
, s
);
715 fprintf (stream
, "\n");
717 fixp
= fixp
->fx_next
;
721 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
724 #ifndef EMIT_SECTION_SYMBOLS
725 #define EMIT_SECTION_SYMBOLS 1
729 adjust_reloc_syms (abfd
, sec
, xxx
)
730 bfd
*abfd ATTRIBUTE_UNUSED
;
732 PTR xxx ATTRIBUTE_UNUSED
;
734 segment_info_type
*seginfo
= seg_info (sec
);
740 dump_section_relocs (abfd
, sec
, stderr
);
742 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
746 else if (fixp
->fx_addsy
)
752 fprintf (stderr
, "\n\nadjusting fixup:\n");
756 sym
= fixp
->fx_addsy
;
758 /* All symbols should have already been resolved at this
759 point. It is possible to see unresolved expression
760 symbols, though, since they are not in the regular symbol
763 resolve_symbol_value (sym
, finalize_syms
);
765 if (fixp
->fx_subsy
!= NULL
)
766 resolve_symbol_value (fixp
->fx_subsy
, finalize_syms
);
768 /* If this symbol is equated to an undefined symbol, convert
769 the fixup to being against that symbol. */
770 if (sym
!= NULL
&& symbol_equated_p (sym
)
771 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
773 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
774 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
775 fixp
->fx_addsy
= sym
;
778 if (sym
!= NULL
&& symbol_mri_common_p (sym
))
780 /* These symbols are handled specially in fixup_segment. */
784 symsec
= S_GET_SEGMENT (sym
);
789 if (bfd_is_abs_section (symsec
))
791 /* The fixup_segment routine will not use this symbol in a
792 relocation unless TC_FORCE_RELOCATION returns 1. */
793 if (TC_FORCE_RELOCATION (fixp
))
795 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
796 #ifdef UNDEFINED_DIFFERENCE_OK
797 if (fixp
->fx_subsy
!= NULL
)
798 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
804 /* If it's one of these sections, assume the symbol is
805 definitely going to be output. The code in
806 md_estimate_size_before_relax in tc-mips.c uses this test
807 as well, so if you change this code you should look at that
809 if (bfd_is_und_section (symsec
)
810 || bfd_is_com_section (symsec
))
812 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
813 #ifdef UNDEFINED_DIFFERENCE_OK
814 /* We have the difference of an undefined symbol and some
815 other symbol. Make sure to mark the other symbol as used
816 in a relocation so that it will always be output. */
818 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
823 /* Don't try to reduce relocs which refer to non-local symbols
824 in .linkonce sections. It can lead to confusion when a
825 debugging section refers to a .linkonce section. I hope
826 this will always be correct. */
827 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
833 if ((bfd_get_section_flags (stdoutput
, symsec
) & SEC_LINK_ONCE
)
838 /* The GNU toolchain uses an extension for ELF: a section
839 beginning with the magic string .gnu.linkonce is a
841 if (strncmp (segment_name (symsec
), ".gnu.linkonce",
842 sizeof ".gnu.linkonce" - 1) == 0)
848 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
849 #ifdef UNDEFINED_DIFFERENCE_OK
850 if (fixp
->fx_subsy
!= NULL
)
851 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
857 /* Since we're reducing to section symbols, don't attempt to reduce
858 anything that's already using one. */
859 if (symbol_section_p (sym
))
861 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
866 /* We can never adjust a reloc against a weak symbol. If we
867 did, and the weak symbol was overridden by a real symbol
868 somewhere else, then our relocation would be pointing at
869 the wrong area of memory. */
872 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
877 /* Is there some other reason we can't adjust this one? (E.g.,
878 call/bal links in i960-bout symbols.) */
879 #ifdef obj_fix_adjustable
880 if (! obj_fix_adjustable (fixp
))
882 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
887 /* Is there some other (target cpu dependent) reason we can't adjust
888 this one? (E.g. relocations involving function addresses on
890 #ifdef tc_fix_adjustable
891 if (! tc_fix_adjustable (fixp
))
893 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
898 /* If the section symbol isn't going to be output, the relocs
899 at least should still work. If not, figure out what to do
900 when we run into that case.
902 We refetch the segment when calling section_symbol, rather
903 than using symsec, because S_GET_VALUE may wind up changing
904 the section when it calls resolve_symbol_value. */
905 fixp
->fx_offset
+= S_GET_VALUE (sym
);
906 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
907 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
909 fprintf (stderr
, "\nadjusted fixup:\n");
916 #if 1 /* def RELOC_REQUIRES_SYMBOL */
919 /* There was no symbol required by this relocation. However,
920 BFD doesn't really handle relocations without symbols well.
921 (At least, the COFF support doesn't.) So for now we fake up
922 a local symbol in the absolute section. */
924 fixp
->fx_addsy
= section_symbol (absolute_section
);
926 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
931 dump_section_relocs (abfd
, sec
, stderr
);
935 write_relocs (abfd
, sec
, xxx
)
938 PTR xxx ATTRIBUTE_UNUSED
;
940 segment_info_type
*seginfo
= seg_info (sec
);
947 /* If seginfo is NULL, we did not create this section; don't do
952 fixup_segment (seginfo
->fix_root
, sec
);
955 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
958 #ifndef RELOC_EXPANSION_POSSIBLE
959 /* Set up reloc information as well. */
960 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
961 memset ((char *) relocs
, 0, n
* sizeof (arelent
*));
964 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
967 bfd_reloc_status_type s
;
976 /* If this is an undefined symbol which was equated to another
977 symbol, then use generate the reloc against the latter symbol
978 rather than the former. */
979 sym
= fixp
->fx_addsy
;
980 while (symbol_equated_p (sym
)
981 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
985 /* We must avoid looping, as that can occur with a badly
987 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
990 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
993 fixp
->fx_addsy
= sym
;
995 reloc
= tc_gen_reloc (sec
, fixp
);
1003 /* This test is triggered inappropriately for the SH. */
1004 if (fixp
->fx_where
+ fixp
->fx_size
1005 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1009 s
= bfd_install_relocation (stdoutput
, reloc
,
1010 fixp
->fx_frag
->fr_literal
,
1011 fixp
->fx_frag
->fr_address
,
1017 case bfd_reloc_overflow
:
1018 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation overflow"));
1020 case bfd_reloc_outofrange
:
1021 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation out of range"));
1024 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1025 fixp
->fx_file
, fixp
->fx_line
, s
);
1027 relocs
[i
++] = reloc
;
1030 n
= n
* MAX_RELOC_EXPANSION
;
1031 /* Set up reloc information as well. */
1032 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
1035 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1039 bfd_reloc_status_type s
;
1049 /* If this is an undefined symbol which was equated to another
1050 symbol, then generate the reloc against the latter symbol
1051 rather than the former. */
1052 sym
= fixp
->fx_addsy
;
1053 while (symbol_equated_p (sym
)
1054 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
1055 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
1056 fixp
->fx_addsy
= sym
;
1058 reloc
= tc_gen_reloc (sec
, fixp
);
1060 for (j
= 0; reloc
[j
]; j
++)
1062 relocs
[i
++] = reloc
[j
];
1065 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1066 if (fixp
->fx_where
+ fixp
->fx_size
1067 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1068 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1069 _("internal error: fixup not contained within frag"));
1070 for (j
= 0; reloc
[j
]; j
++)
1072 s
= bfd_install_relocation (stdoutput
, reloc
[j
],
1073 fixp
->fx_frag
->fr_literal
,
1074 fixp
->fx_frag
->fr_address
,
1080 case bfd_reloc_overflow
:
1081 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1082 _("relocation overflow"));
1085 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1086 fixp
->fx_file
, fixp
->fx_line
);
1097 sympp
= bfd_get_outsymbols (stdoutput
);
1098 nsyms
= bfd_get_symcount (stdoutput
);
1099 for (i
= 0; i
< n
; i
++)
1100 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1102 for (j
= 0; j
< nsyms
; j
++)
1103 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
1112 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1114 bfd_set_section_flags (abfd
, sec
,
1115 (bfd_get_section_flags (abfd
, sec
)
1116 & (flagword
) ~SEC_RELOC
));
1118 #ifdef SET_SECTION_RELOCS
1119 SET_SECTION_RELOCS (sec
, relocs
, n
);
1127 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1128 for (i
= 0; i
< n
; i
++)
1131 s
= *r
->sym_ptr_ptr
;
1132 fprintf (stderr
, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1133 i
, r
, r
->address
, s
->name
, r
->addend
);
1140 write_contents (abfd
, sec
, xxx
)
1141 bfd
*abfd ATTRIBUTE_UNUSED
;
1143 PTR xxx ATTRIBUTE_UNUSED
;
1145 segment_info_type
*seginfo
= seg_info (sec
);
1146 unsigned long offset
= 0;
1149 /* Write out the frags. */
1151 || !(bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1154 for (f
= seginfo
->frchainP
->frch_root
;
1159 unsigned long fill_size
;
1163 assert (f
->fr_type
== rs_fill
);
1166 x
= bfd_set_section_contents (stdoutput
, sec
,
1167 f
->fr_literal
, (file_ptr
) offset
,
1168 (bfd_size_type
) f
->fr_fix
);
1171 bfd_perror (stdoutput
->filename
);
1172 as_perror (_("FATAL: Can't write %s"), stdoutput
->filename
);
1173 exit (EXIT_FAILURE
);
1175 offset
+= f
->fr_fix
;
1177 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1178 fill_size
= f
->fr_var
;
1179 count
= f
->fr_offset
;
1180 assert (count
>= 0);
1181 if (fill_size
&& count
)
1184 if (fill_size
> sizeof (buf
))
1186 /* Do it the old way. Can this ever happen? */
1189 x
= bfd_set_section_contents (stdoutput
, sec
,
1192 (bfd_size_type
) fill_size
);
1195 bfd_perror (stdoutput
->filename
);
1196 as_perror (_("FATAL: Can't write %s"),
1197 stdoutput
->filename
);
1198 exit (EXIT_FAILURE
);
1200 offset
+= fill_size
;
1205 /* Build a buffer full of fill objects and output it as
1206 often as necessary. This saves on the overhead of
1207 potentially lots of bfd_set_section_contents calls. */
1211 n_per_buf
= sizeof (buf
);
1212 memset (buf
, *fill_literal
, n_per_buf
);
1217 n_per_buf
= sizeof (buf
) / fill_size
;
1218 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1219 memcpy (bufp
, fill_literal
, fill_size
);
1221 for (; count
> 0; count
-= n_per_buf
)
1223 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1224 x
= bfd_set_section_contents
1225 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1226 (bfd_size_type
) n_per_buf
* fill_size
);
1228 as_fatal (_("Cannot write to output file."));
1229 offset
+= n_per_buf
* fill_size
;
1237 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1239 merge_data_into_text ()
1241 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1242 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1243 seg_info (data_section
)->frchainP
->frch_root
;
1244 seg_info (text_section
)->frchainP
->frch_last
=
1245 seg_info (data_section
)->frchainP
->frch_last
;
1246 seg_info (data_section
)->frchainP
= 0;
1250 text_last_frag
->fr_next
= data_frag_root
;
1251 text_last_frag
= data_last_frag
;
1252 data_last_frag
= NULL
;
1253 data_frag_root
= NULL
;
1256 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
1257 tmp
->fx_next
= data_fix_root
;
1258 text_fix_tail
= data_fix_tail
;
1261 text_fix_root
= data_fix_root
;
1262 data_fix_root
= NULL
;
1265 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1267 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1269 relax_and_size_all_segments ()
1273 relax_segment (text_frag_root
, SEG_TEXT
);
1274 relax_segment (data_frag_root
, SEG_DATA
);
1275 relax_segment (bss_frag_root
, SEG_BSS
);
1277 /* Now the addresses of frags are correct within the segment. */
1278 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
1279 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
1280 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
1282 /* Join the 2 segments into 1 huge segment.
1283 To do this, re-compute every rn_address in the SEG_DATA frags.
1284 Then join the data frags after the text frags.
1286 Determine a_data [length of data segment]. */
1289 register relax_addressT slide
;
1291 know ((text_last_frag
->fr_type
== rs_fill
)
1292 && (text_last_frag
->fr_offset
== 0));
1294 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
1295 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
1296 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
1298 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1299 /* For b.out: If the data section has a strict alignment
1300 requirement, its load address in the .o file will be
1301 rounded up from the size of the text section. These
1302 two values are *not* the same! Similarly for the bss
1304 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
1307 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1308 fragP
->fr_address
+= slide
;
1310 know (text_last_frag
!= 0);
1311 text_last_frag
->fr_next
= data_frag_root
;
1315 H_SET_DATA_SIZE (&headers
, 0);
1319 /* See above comments on b.out data section address. */
1322 if (data_last_frag
== 0)
1323 bss_vma
= H_GET_TEXT_SIZE (&headers
);
1325 bss_vma
= data_last_frag
->fr_address
;
1326 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
1327 bss_address_frag
.fr_address
= bss_vma
;
1329 #else /* ! OBJ_BOUT */
1330 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
1331 H_GET_DATA_SIZE (&headers
));
1333 #endif /* ! OBJ_BOUT */
1335 /* Slide all the frags. */
1338 relax_addressT slide
= bss_address_frag
.fr_address
;
1340 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1341 fragP
->fr_address
+= slide
;
1345 H_SET_BSS_SIZE (&headers
,
1346 bss_last_frag
->fr_address
- bss_frag_root
->fr_address
);
1348 H_SET_BSS_SIZE (&headers
, 0);
1350 #endif /* ! BFD_ASSEMBLER && ! BFD */
1352 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1354 #ifdef BFD_ASSEMBLER
1362 extern PTR bfd_alloc
PARAMS ((bfd
*, size_t));
1364 /* Count symbols. We can't rely on a count made by the loop in
1365 write_object_file, because *_frob_file may add a new symbol or
1368 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1375 asympp
= (asymbol
**) bfd_alloc (stdoutput
,
1376 nsyms
* sizeof (asymbol
*));
1377 symp
= symbol_rootP
;
1378 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1380 asympp
[i
] = symbol_get_bfdsym (symp
);
1381 symbol_mark_written (symp
);
1386 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1387 assert (result
== true);
1388 symbol_table_frozen
= 1;
1392 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1394 set_segment_vma (abfd
, sec
, xxx
)
1397 PTR xxx ATTRIBUTE_UNUSED
;
1399 static bfd_vma addr
= 0;
1401 bfd_set_section_vma (abfd
, sec
, addr
);
1402 addr
+= bfd_section_size (abfd
, sec
);
1404 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1406 /* Finish the subsegments. After every sub-segment, we fake an
1407 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1408 ".fill 0" because that is the kind of frag that requires least
1409 thought. ".align" frags like to have a following frag since that
1410 makes calculating their intended length trivial. */
1412 #ifndef SUB_SEGMENT_ALIGN
1413 #ifdef BFD_ASSEMBLER
1414 #define SUB_SEGMENT_ALIGN(SEG) (0)
1416 #define SUB_SEGMENT_ALIGN(SEG) (2)
1423 struct frchain
*frchainP
;
1425 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
1429 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
1431 /* This now gets called even if we had errors. In that case,
1432 any alignment is meaningless, and, moreover, will look weird
1433 if we are generating a listing. */
1434 alignment
= had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg
);
1436 /* The last subsegment gets an aligment corresponding to the
1437 alignment of the section. This allows proper nop-filling
1438 at the end of code-bearing sections. */
1439 if (!frchainP
->frch_next
|| frchainP
->frch_next
->frch_seg
!= now_seg
)
1440 alignment
= get_recorded_alignment (now_seg
);
1442 if (subseg_text_p (now_seg
))
1443 frag_align_code (alignment
, 0);
1445 frag_align (alignment
, 0, 0);
1447 /* frag_align will have left a new frag.
1448 Use this last frag for an empty ".fill".
1450 For this segment ...
1451 Create a last frag. Do not leave a "being filled in frag". */
1452 frag_wane (frag_now
);
1453 frag_now
->fr_fix
= 0;
1454 know (frag_now
->fr_next
== NULL
);
1458 /* Write the object file. */
1461 write_object_file ()
1463 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1464 fragS
*fragP
; /* Track along all frags. */
1467 /* Do we really want to write it? */
1469 int n_warns
, n_errs
;
1470 n_warns
= had_warnings ();
1471 n_errs
= had_errors ();
1472 /* The -Z flag indicates that an object file should be generated,
1473 regardless of warnings and errors. */
1474 if (flag_always_generate_output
)
1476 if (n_warns
|| n_errs
)
1477 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1478 n_errs
, n_errs
== 1 ? "" : "s",
1479 n_warns
, n_warns
== 1 ? "" : "s");
1484 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1485 n_errs
, n_errs
== 1 ? "" : "s",
1486 n_warns
, n_warns
== 1 ? "" : "s");
1491 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1492 a routine to check for the definition of the procedure "_main",
1493 and if so -- fix it up so that it can be program entry point. */
1494 vms_check_for_main ();
1495 #endif /* OBJ_VMS */
1497 /* From now on, we don't care about sub-segments. Build one frag chain
1498 for each segment. Linked thru fr_next. */
1500 #ifdef BFD_ASSEMBLER
1501 /* Remove the sections created by gas for its own purposes. */
1503 asection
**seclist
, *sec
;
1506 seclist
= &stdoutput
->sections
;
1507 while (seclist
&& *seclist
)
1510 while (sec
== reg_section
|| sec
== expr_section
)
1514 stdoutput
->section_count
--;
1519 seclist
= &(*seclist
)->next
;
1522 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1525 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1527 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
1528 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
1529 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
1532 /* We have two segments. If user gave -R flag, then we must put the
1533 data frags into the text segment. Do this before relaxing so
1534 we know to take advantage of -R and make shorter addresses. */
1535 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1536 if (flag_readonly_data_in_text
)
1538 merge_data_into_text ();
1542 #ifdef BFD_ASSEMBLER
1548 bfd_map_over_sections (stdoutput
, relax_seg
, &changed
);
1552 bfd_map_over_sections (stdoutput
, size_seg
, (char *) 0);
1554 relax_and_size_all_segments ();
1555 #endif /* BFD_ASSEMBLER */
1557 /* Relaxation has completed. Freeze all syms. */
1560 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1561 /* Now that the segments have their final sizes, run through the
1562 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1563 should too. Currently, only DJGPP uses this code, but other
1564 COFF targets may need to execute this too. */
1565 bfd_map_over_sections (stdoutput
, set_segment_vma
, (char *) 0);
1568 #ifndef BFD_ASSEMBLER
1569 /* Crawl the symbol chain.
1571 For each symbol whose value depends on a frag, take the address of
1572 that frag and subsume it into the value of the symbol.
1573 After this, there is just one way to lookup a symbol value.
1574 Values are left in their final state for object file emission.
1575 We adjust the values of 'L' local symbols, even if we do
1576 not intend to emit them to the object file, because their values
1577 are needed for fix-ups.
1579 Unless we saw a -L flag, remove all symbols that begin with 'L'
1580 from the symbol chain. (They are still pointed to by the fixes.)
1582 Count the remaining symbols.
1583 Assign a symbol number to each symbol.
1584 Count the number of string-table chars we will emit.
1585 Put this info into the headers as appropriate. */
1586 know (zero_address_frag
.fr_address
== 0);
1587 string_byte_count
= sizeof (string_byte_count
);
1589 obj_crawl_symbol_chain (&headers
);
1591 if (string_byte_count
== sizeof (string_byte_count
))
1592 string_byte_count
= 0;
1594 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1596 /* Addresses of frags now reflect addresses we use in the object file.
1597 Symbol values are correct.
1598 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1599 Also converting any machine-dependent frags using md_convert_frag(); */
1600 subseg_change (SEG_TEXT
, 0);
1602 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1604 /* At this point we have linked all the frags into a single
1605 chain. However, cvt_frag_to_fill may call md_convert_frag
1606 which may call fix_new. We need to ensure that fix_new adds
1607 the fixup to the right section. */
1608 if (fragP
== data_frag_root
)
1609 subseg_change (SEG_DATA
, 0);
1611 cvt_frag_to_fill (&headers
, SEG_TEXT
, fragP
);
1613 /* Some assert macros don't work with # directives mixed in. */
1615 if (!(fragP
->fr_next
== NULL
1617 || fragP
->fr_next
== data_frag_root
1619 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
1620 == (fragP
->fr_fix
+ fragP
->fr_offset
* fragP
->fr_var
))))
1624 #endif /* ! BFD_ASSEMBLER */
1626 #ifndef WORKING_DOT_WORD
1628 struct broken_word
*lie
;
1629 struct broken_word
**prevP
;
1631 prevP
= &broken_words
;
1632 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1637 subseg_change (lie
->seg
, lie
->subseg
);
1638 exp
.X_op
= O_subtract
;
1639 exp
.X_add_symbol
= lie
->add
;
1640 exp
.X_op_symbol
= lie
->sub
;
1641 exp
.X_add_number
= lie
->addnum
;
1642 #ifdef BFD_ASSEMBLER
1643 #ifdef TC_CONS_FIX_NEW
1644 TC_CONS_FIX_NEW (lie
->frag
,
1645 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1648 fix_new_exp (lie
->frag
,
1649 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1650 2, &exp
, 0, BFD_RELOC_16
);
1653 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1654 fix_new_exp (lie
->frag
,
1655 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1656 2, &exp
, 0, NO_RELOC
);
1659 fix_new_ns32k_exp (lie
->frag
,
1660 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1661 2, &exp
, 0, 0, 2, 0, 0);
1663 fix_new_exp (lie
->frag
,
1664 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1666 #endif /* TC_NS32K */
1667 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1668 #endif /* BFD_ASSEMBLER */
1669 *prevP
= lie
->next_broken_word
;
1672 prevP
= &(lie
->next_broken_word
);
1674 for (lie
= broken_words
; lie
;)
1676 struct broken_word
*untruth
;
1678 addressT table_addr
;
1679 addressT from_addr
, to_addr
;
1682 subseg_change (lie
->seg
, lie
->subseg
);
1683 fragP
= lie
->dispfrag
;
1685 /* Find out how many broken_words go here. */
1688 untruth
&& untruth
->dispfrag
== fragP
;
1689 untruth
= untruth
->next_broken_word
)
1690 if (untruth
->added
== 1)
1693 table_ptr
= lie
->dispfrag
->fr_opcode
;
1694 table_addr
= (lie
->dispfrag
->fr_address
1695 + (table_ptr
- lie
->dispfrag
->fr_literal
));
1696 /* Create the jump around the long jumps. This is a short
1697 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1698 from_addr
= table_addr
;
1699 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1700 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1702 table_ptr
+= md_short_jump_size
;
1703 table_addr
+= md_short_jump_size
;
1706 lie
&& lie
->dispfrag
== fragP
;
1707 m
++, lie
= lie
->next_broken_word
)
1709 if (lie
->added
== 2)
1711 /* Patch the jump table. */
1712 /* This is the offset from ??? to table_ptr+0. */
1713 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1714 #ifdef BFD_ASSEMBLER
1715 to_addr
-= symbol_get_frag (lie
->sub
)->fr_address
;
1717 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1718 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr
, lie
);
1720 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1721 for (untruth
= lie
->next_broken_word
;
1722 untruth
&& untruth
->dispfrag
== fragP
;
1723 untruth
= untruth
->next_broken_word
)
1725 if (untruth
->use_jump
== lie
)
1726 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1729 /* Install the long jump. */
1730 /* This is a long jump from table_ptr+0 to the final target. */
1731 from_addr
= table_addr
;
1732 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1733 #ifdef BFD_ASSEMBLER
1734 to_addr
+= symbol_get_frag (lie
->add
)->fr_address
;
1736 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1738 table_ptr
+= md_long_jump_size
;
1739 table_addr
+= md_long_jump_size
;
1743 #endif /* not WORKING_DOT_WORD */
1745 #ifndef BFD_ASSEMBLER
1748 char *the_object_file
;
1749 long object_file_size
;
1750 /* Scan every FixS performing fixups. We had to wait until now to
1751 do this because md_convert_frag() may have made some fixSs. */
1754 subseg_change (SEG_TEXT
, 0);
1755 trsize
= md_reloc_size
* fixup_segment (text_fix_root
, SEG_TEXT
);
1756 subseg_change (SEG_DATA
, 0);
1757 drsize
= md_reloc_size
* fixup_segment (data_fix_root
, SEG_DATA
);
1758 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
1760 /* FIXME: Move this stuff into the pre-write-hook. */
1761 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
1762 H_SET_ENTRY_POINT (&headers
, 0);
1764 obj_pre_write_hook (&headers
); /* Extra coff stuff. */
1766 object_file_size
= H_GET_FILE_SIZE (&headers
);
1767 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
1769 output_file_create (out_file_name
);
1771 obj_header_append (&next_object_file_charP
, &headers
);
1773 know ((next_object_file_charP
- the_object_file
)
1774 == H_GET_HEADER_SIZE (&headers
));
1777 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1779 register long count
;
1780 register char *fill_literal
;
1781 register long fill_size
;
1784 know (fragP
->fr_type
== rs_fill
);
1785 append (&next_object_file_charP
, fragP
->fr_literal
,
1786 (unsigned long) fragP
->fr_fix
);
1787 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
1788 fill_size
= fragP
->fr_var
;
1789 know (fragP
->fr_offset
>= 0);
1791 for (count
= fragP
->fr_offset
; count
; count
--)
1792 append (&next_object_file_charP
, fill_literal
,
1793 (unsigned long) fill_size
);
1796 know ((next_object_file_charP
- the_object_file
)
1797 == (H_GET_HEADER_SIZE (&headers
)
1798 + H_GET_TEXT_SIZE (&headers
)
1799 + H_GET_DATA_SIZE (&headers
)));
1801 /* Emit relocations. */
1802 obj_emit_relocations (&next_object_file_charP
, text_fix_root
,
1803 (relax_addressT
) 0);
1804 know ((next_object_file_charP
- the_object_file
)
1805 == (H_GET_HEADER_SIZE (&headers
)
1806 + H_GET_TEXT_SIZE (&headers
)
1807 + H_GET_DATA_SIZE (&headers
)
1808 + H_GET_TEXT_RELOCATION_SIZE (&headers
)));
1810 /* Make addresses in data relocation directives relative to beginning of
1811 first data fragment, not end of last text fragment: alignment of the
1812 start of the data segment may place a gap between the segments. */
1813 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1814 data0_frchainP
->frch_root
->fr_address
);
1816 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1817 text_last_frag
->fr_address
);
1818 #endif /* TC_I960 */
1820 know ((next_object_file_charP
- the_object_file
)
1821 == (H_GET_HEADER_SIZE (&headers
)
1822 + H_GET_TEXT_SIZE (&headers
)
1823 + H_GET_DATA_SIZE (&headers
)
1824 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1825 + H_GET_DATA_RELOCATION_SIZE (&headers
)));
1827 /* Emit line number entries. */
1828 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
1829 know ((next_object_file_charP
- the_object_file
)
1830 == (H_GET_HEADER_SIZE (&headers
)
1831 + H_GET_TEXT_SIZE (&headers
)
1832 + H_GET_DATA_SIZE (&headers
)
1833 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1834 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1835 + H_GET_LINENO_SIZE (&headers
)));
1838 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
1839 know ((next_object_file_charP
- the_object_file
)
1840 == (H_GET_HEADER_SIZE (&headers
)
1841 + H_GET_TEXT_SIZE (&headers
)
1842 + H_GET_DATA_SIZE (&headers
)
1843 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1844 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1845 + H_GET_LINENO_SIZE (&headers
)
1846 + H_GET_SYMBOL_TABLE_SIZE (&headers
)));
1849 if (string_byte_count
> 0)
1850 obj_emit_strings (&next_object_file_charP
);
1853 bfd_seek (stdoutput
, 0, 0);
1854 bfd_write (the_object_file
, 1, object_file_size
, stdoutput
);
1857 /* Write the data to the file. */
1858 output_file_append (the_object_file
, object_file_size
, out_file_name
);
1859 free (the_object_file
);
1863 /* Now do the VMS-dependent part of writing the object file. */
1864 vms_write_object_file (H_GET_TEXT_SIZE (&headers
),
1865 H_GET_DATA_SIZE (&headers
),
1866 H_GET_BSS_SIZE (&headers
),
1867 text_frag_root
, data_frag_root
);
1868 #endif /* OBJ_VMS */
1869 #else /* BFD_ASSEMBLER */
1871 /* Resolve symbol values. This needs to be done before processing
1877 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1878 resolve_symbol_value (symp
, finalize_syms
);
1880 resolve_local_symbol_values ();
1884 #ifdef tc_frob_file_before_adjust
1885 tc_frob_file_before_adjust ();
1887 #ifdef obj_frob_file_before_adjust
1888 obj_frob_file_before_adjust ();
1891 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
1893 /* Set up symbol table, and write it out. */
1898 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1903 if (symbol_mri_common_p (symp
))
1905 if (S_IS_EXTERNAL (symp
))
1906 as_bad (_("%s: global symbols not supported in common sections"),
1908 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1912 name
= S_GET_NAME (symp
);
1916 decode_local_label_name ((char *) S_GET_NAME (symp
));
1917 /* They only differ if `name' is a fb or dollar local
1919 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
1920 as_bad (_("local label %s is not defined"), name2
);
1923 /* Do it again, because adjust_reloc_syms might introduce
1924 more symbols. They'll probably only be section symbols,
1925 but they'll still need to have the values computed. */
1926 resolve_symbol_value (symp
, finalize_syms
);
1928 /* Skip symbols which were equated to undefined or common
1930 if (symbol_equated_p (symp
)
1931 && (! S_IS_DEFINED (symp
) || S_IS_COMMON (symp
)))
1933 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1937 /* So far, common symbols have been treated like undefined symbols.
1938 Put them in the common section now. */
1939 if (S_IS_DEFINED (symp
) == 0
1940 && S_GET_VALUE (symp
) != 0)
1941 S_SET_SEGMENT (symp
, bfd_com_section_ptr
);
1943 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1944 S_GET_NAME (symp
), symp
,
1946 symbol_get_bfdsym (symp
)->flags
,
1947 segment_name (S_GET_SEGMENT (symp
)));
1950 #ifdef obj_frob_symbol
1951 obj_frob_symbol (symp
, punt
);
1953 #ifdef tc_frob_symbol
1954 if (! punt
|| symbol_used_in_reloc_p (symp
))
1955 tc_frob_symbol (symp
, punt
);
1958 /* If we don't want to keep this symbol, splice it out of
1959 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1960 want section symbols. Otherwise, we skip local symbols
1961 and symbols that the frob_symbol macros told us to punt,
1962 but we keep such symbols if they are used in relocs. */
1963 if ((! EMIT_SECTION_SYMBOLS
1964 && symbol_section_p (symp
))
1965 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1966 opposites. Sometimes the former checks flags and the
1967 latter examines the name... */
1968 || (!S_IS_EXTERN (symp
)
1969 && (S_IS_LOCAL (symp
) || punt
)
1970 && ! symbol_used_in_reloc_p (symp
)))
1972 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1974 /* After symbol_remove, symbol_next(symp) still returns
1975 the one that came after it in the chain. So we don't
1976 need to do any extra cleanup work here. */
1980 /* Make sure we really got a value for the symbol. */
1981 if (! symbol_resolved_p (symp
))
1983 as_bad (_("can't resolve value for symbol \"%s\""),
1985 symbol_mark_resolved (symp
);
1988 /* Set the value into the BFD symbol. Up til now the value
1989 has only been kept in the gas symbolS struct. */
1990 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
1996 /* Now do any format-specific adjustments to the symbol table, such
1997 as adding file symbols. */
1998 #ifdef tc_adjust_symtab
1999 tc_adjust_symtab ();
2001 #ifdef obj_adjust_symtab
2002 obj_adjust_symtab ();
2005 /* Now that all the sizes are known, and contents correct, we can
2006 start writing to the file. */
2009 /* If *_frob_file changes the symbol value at this point, it is
2010 responsible for moving the changed value into symp->bsym->value
2011 as well. Hopefully all symbol value changing can be done in
2016 #ifdef obj_frob_file
2020 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
2022 #ifdef tc_frob_file_after_relocs
2023 tc_frob_file_after_relocs ();
2025 #ifdef obj_frob_file_after_relocs
2026 obj_frob_file_after_relocs ();
2029 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
2030 #endif /* BFD_ASSEMBLER */
2034 #ifdef TC_GENERIC_RELAX_TABLE
2036 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2039 relax_frag (segment
, fragP
, stretch
)
2044 const relax_typeS
*this_type
;
2045 const relax_typeS
*start_type
;
2046 relax_substateT next_state
;
2047 relax_substateT this_state
;
2053 const relax_typeS
*table
;
2055 target
= fragP
->fr_offset
;
2056 address
= fragP
->fr_address
;
2057 table
= TC_GENERIC_RELAX_TABLE
;
2058 this_state
= fragP
->fr_subtype
;
2059 start_type
= this_type
= table
+ this_state
;
2060 symbolP
= fragP
->fr_symbol
;
2066 sym_frag
= symbol_get_frag (symbolP
);
2068 #ifndef DIFF_EXPR_OK
2069 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2070 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2071 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2072 || (S_GET_SEGMENT (symbolP
) == SEG_BSS
)
2073 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
2075 know (sym_frag
!= NULL
);
2077 know (!(S_GET_SEGMENT (symbolP
) == absolute_section
)
2078 || sym_frag
== &zero_address_frag
);
2079 target
+= S_GET_VALUE (symbolP
) + sym_frag
->fr_address
;
2081 /* If frag has yet to be reached on this pass,
2082 assume it will move by STRETCH just as we did.
2083 If this is not so, it will be because some frag
2084 between grows, and that will force another pass. */
2087 && sym_frag
->relax_marker
!= fragP
->relax_marker
2088 && S_GET_SEGMENT (symbolP
) == segment
)
2094 aim
= target
- address
- fragP
->fr_fix
;
2095 #ifdef TC_PCREL_ADJUST
2096 /* Currently only the ns32k family needs this. */
2097 aim
+= TC_PCREL_ADJUST (fragP
);
2099 /* This machine doesn't want to use pcrel_adjust.
2100 In that case, pcrel_adjust should be zero. */
2102 assert (fragP
->fr_targ
.ns32k
.pcrel_adjust
== 0);
2105 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2106 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2111 /* Look backwards. */
2112 for (next_state
= this_type
->rlx_more
; next_state
;)
2113 if (aim
>= this_type
->rlx_backward
)
2117 /* Grow to next state. */
2118 this_state
= next_state
;
2119 this_type
= table
+ this_state
;
2120 next_state
= this_type
->rlx_more
;
2125 /* Look forwards. */
2126 for (next_state
= this_type
->rlx_more
; next_state
;)
2127 if (aim
<= this_type
->rlx_forward
)
2131 /* Grow to next state. */
2132 this_state
= next_state
;
2133 this_type
= table
+ this_state
;
2134 next_state
= this_type
->rlx_more
;
2138 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2140 fragP
->fr_subtype
= this_state
;
2144 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2146 /* Relax_align. Advance location counter to next address that has 'alignment'
2147 lowest order bits all 0s, return size of adjustment made. */
2148 static relax_addressT
2149 relax_align (address
, alignment
)
2150 register relax_addressT address
; /* Address now. */
2151 register int alignment
; /* Alignment (binary). */
2153 relax_addressT mask
;
2154 relax_addressT new_address
;
2156 mask
= ~((~0) << alignment
);
2157 new_address
= (address
+ mask
) & (~mask
);
2158 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2160 /* We must provide lots of padding, so the linker can discard it
2161 when needed. The linker will not add extra space, ever. */
2162 new_address
+= (1 << alignment
);
2164 return (new_address
- address
);
2167 /* Now we have a segment, not a crowd of sub-segments, we can make
2172 After this, all frags in this segment have addresses that are correct
2173 within the segment. Since segments live in different file addresses,
2174 these frag addresses may not be the same as final object-file
2178 relax_segment (segment_frag_root
, segment
)
2179 struct frag
*segment_frag_root
;
2182 register struct frag
*fragP
;
2183 register relax_addressT address
;
2186 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2187 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
2189 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2190 subseg_change (segment
, 0);
2192 /* For each frag in segment: count and store (a 1st guess of)
2195 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2197 fragP
->relax_marker
= 0;
2198 fragP
->fr_address
= address
;
2199 address
+= fragP
->fr_fix
;
2201 switch (fragP
->fr_type
)
2204 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2211 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2213 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2216 if (offset
% fragP
->fr_var
!= 0)
2218 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2219 (unsigned long) offset
, (long) fragP
->fr_var
);
2220 offset
-= (offset
% fragP
->fr_var
);
2229 /* Assume .org is nugatory. It will grow with 1st relax. */
2232 case rs_machine_dependent
:
2233 address
+= md_estimate_size_before_relax (fragP
, segment
);
2236 #ifndef WORKING_DOT_WORD
2237 /* Broken words don't concern us yet. */
2238 case rs_broken_word
:
2243 /* Initial guess is always 1; doing otherwise can result in
2244 stable solutions that are larger than the minimum. */
2245 address
+= fragP
->fr_offset
= 1;
2249 address
+= eh_frame_estimate_size_before_relax (fragP
);
2253 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
2257 BAD_CASE (fragP
->fr_type
);
2264 long stretch
; /* May be any size, 0 or negative. */
2265 /* Cumulative number of addresses we have relaxed this pass.
2266 We may have relaxed more than one address. */
2267 int stretched
; /* Have we stretched on this pass? */
2268 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2269 grew, and another shrank. If a branch instruction doesn't fit anymore,
2270 we could be scrod. */
2277 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2280 addressT was_address
;
2284 fragP
->relax_marker
^= 1;
2285 was_address
= fragP
->fr_address
;
2286 address
= fragP
->fr_address
+= stretch
;
2287 symbolP
= fragP
->fr_symbol
;
2288 offset
= fragP
->fr_offset
;
2290 switch (fragP
->fr_type
)
2292 case rs_fill
: /* .fill never relaxes. */
2296 #ifndef WORKING_DOT_WORD
2297 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2298 for it I do not want to write it. I do not want to have
2299 anything to do with it. This is not the proper way to
2300 implement this misfeature. */
2301 case rs_broken_word
:
2303 struct broken_word
*lie
;
2304 struct broken_word
*untruth
;
2306 /* Yes this is ugly (storing the broken_word pointer
2307 in the symbol slot). Still, this whole chunk of
2308 code is ugly, and I don't feel like doing anything
2309 about it. Think of it as stubbornness in action. */
2311 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2312 lie
&& lie
->dispfrag
== fragP
;
2313 lie
= lie
->next_broken_word
)
2319 offset
= (symbol_get_frag (lie
->add
)->fr_address
2320 + S_GET_VALUE (lie
->add
)
2322 - (symbol_get_frag (lie
->sub
)->fr_address
2323 + S_GET_VALUE (lie
->sub
)));
2324 if (offset
<= -32768 || offset
>= 32767)
2326 if (flag_warn_displacement
)
2329 sprint_value (buf
, (addressT
) lie
->addnum
);
2330 as_warn (_(".word %s-%s+%s didn't fit"),
2331 S_GET_NAME (lie
->add
),
2332 S_GET_NAME (lie
->sub
),
2336 if (fragP
->fr_subtype
== 0)
2338 fragP
->fr_subtype
++;
2339 growth
+= md_short_jump_size
;
2341 for (untruth
= lie
->next_broken_word
;
2342 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2343 untruth
= untruth
->next_broken_word
)
2344 if ((symbol_get_frag (untruth
->add
)
2345 == symbol_get_frag (lie
->add
))
2346 && (S_GET_VALUE (untruth
->add
)
2347 == S_GET_VALUE (lie
->add
)))
2350 untruth
->use_jump
= lie
;
2352 growth
+= md_long_jump_size
;
2357 } /* case rs_broken_word */
2363 addressT oldoff
, newoff
;
2365 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2367 newoff
= relax_align (address
+ fragP
->fr_fix
,
2370 if (fragP
->fr_subtype
!= 0)
2372 if (oldoff
> fragP
->fr_subtype
)
2374 if (newoff
> fragP
->fr_subtype
)
2378 growth
= newoff
- oldoff
;
2384 addressT target
= offset
;
2389 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2390 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2391 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2392 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
)
2393 || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
2394 know (symbolP
->sy_frag
);
2395 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2396 || (symbolP
->sy_frag
== &zero_address_frag
));
2398 target
+= (S_GET_VALUE (symbolP
)
2399 + symbol_get_frag (symbolP
)->fr_address
);
2400 } /* if we have a symbol */
2402 know (fragP
->fr_next
);
2403 after
= fragP
->fr_next
->fr_address
;
2404 growth
= target
- after
;
2407 /* Growth may be negative, but variable part of frag
2408 cannot have fewer than 0 chars. That is, we can't
2410 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2411 _("attempt to .org backwards ignored"));
2413 /* We've issued an error message. Change the
2414 frag to avoid cascading errors. */
2415 fragP
->fr_type
= rs_align
;
2416 fragP
->fr_subtype
= 0;
2417 fragP
->fr_offset
= 0;
2418 fragP
->fr_fix
= after
- address
;
2422 /* This is an absolute growth factor */
2430 growth
= S_GET_VALUE (symbolP
);
2431 if (symbol_get_frag (symbolP
) != &zero_address_frag
2432 || S_IS_COMMON (symbolP
)
2433 || ! S_IS_DEFINED (symbolP
))
2434 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2435 _(".space specifies non-absolute value"));
2436 fragP
->fr_symbol
= 0;
2439 as_warn (_(".space or .fill with negative value, ignored"));
2447 case rs_machine_dependent
:
2448 #ifdef md_relax_frag
2449 growth
= md_relax_frag (segment
, fragP
, stretch
);
2451 #ifdef TC_GENERIC_RELAX_TABLE
2452 /* The default way to relax a frag is to look through
2453 TC_GENERIC_RELAX_TABLE. */
2454 growth
= relax_frag (segment
, fragP
, stretch
);
2455 #endif /* TC_GENERIC_RELAX_TABLE */
2464 value
= resolve_symbol_value (fragP
->fr_symbol
, 0);
2465 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2466 growth
= size
- fragP
->fr_offset
;
2467 fragP
->fr_offset
= size
;
2472 growth
= eh_frame_relax_frag (fragP
);
2476 growth
= dwarf2dbg_relax_frag (fragP
);
2480 BAD_CASE (fragP
->fr_type
);
2488 } /* For each frag in the segment. */
2490 while (stretched
); /* Until nothing further to relax. */
2494 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2495 if (fragP
->last_fr_address
!= fragP
->fr_address
)
2497 fragP
->last_fr_address
= fragP
->fr_address
;
2503 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2505 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2506 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2511 Go through all the fixS's in a segment and see which ones can be
2512 handled now. (These consist of fixS where we have since discovered
2513 the value of a symbol, or the address of the frag involved.)
2514 For each one, call md_apply_fix to put the fix into the frag data.
2516 Result is a count of how many relocation structs will be needed to
2517 handle the remaining fixS's that we couldn't completely handle here.
2518 These will be output later by emit_relocations(). */
2521 fixup_segment (fixP
, this_segment_type
)
2522 register fixS
*fixP
;
2523 segT this_segment_type
; /* N_TYPE bits for segment. */
2525 long seg_reloc_count
= 0;
2526 symbolS
*add_symbolP
;
2527 symbolS
*sub_symbolP
;
2534 segT add_symbol_segment
= absolute_section
;
2536 /* If the linker is doing the relaxing, we must not do any fixups.
2538 Well, strictly speaking that's not true -- we could do any that are
2539 PC-relative and don't cross regions that could change size. And for the
2540 i960 (the only machine for which we've got a relaxing linker right now),
2541 we might be able to turn callx/callj into bal anyways in cases where we
2542 know the maximum displacement. */
2543 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment_type
))
2545 for (; fixP
; fixP
= fixP
->fx_next
)
2547 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2548 return seg_reloc_count
;
2551 for (; fixP
; fixP
= fixP
->fx_next
)
2554 fprintf (stderr
, "\nprocessing fixup:\n");
2558 fragP
= fixP
->fx_frag
;
2560 where
= fixP
->fx_where
;
2561 place
= fragP
->fr_literal
+ where
;
2562 size
= fixP
->fx_size
;
2563 add_symbolP
= fixP
->fx_addsy
;
2564 #ifdef TC_VALIDATE_FIX
2565 TC_VALIDATE_FIX (fixP
, this_segment_type
, skip
);
2567 sub_symbolP
= fixP
->fx_subsy
;
2568 add_number
= fixP
->fx_offset
;
2569 pcrel
= fixP
->fx_pcrel
;
2572 if (add_symbolP
!= NULL
2573 && symbol_mri_common_p (add_symbolP
))
2575 know (add_symbolP
->sy_value
.X_op
== O_symbol
);
2576 add_number
+= S_GET_VALUE (add_symbolP
);
2577 fixP
->fx_offset
= add_number
;
2578 add_symbolP
= fixP
->fx_addsy
=
2579 symbol_get_value_expression (add_symbolP
)->X_add_symbol
;
2583 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2587 resolve_symbol_value (sub_symbolP
, finalize_syms
);
2588 if (add_symbolP
== NULL
|| add_symbol_segment
== absolute_section
)
2590 if (add_symbolP
!= NULL
)
2592 add_number
+= S_GET_VALUE (add_symbolP
);
2594 fixP
->fx_addsy
= NULL
;
2597 /* It's just -sym. */
2598 if (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)
2600 add_number
-= S_GET_VALUE (sub_symbolP
);
2601 fixP
->fx_subsy
= NULL
;
2604 && S_GET_SEGMENT (sub_symbolP
) == this_segment_type
)
2606 /* Should try converting to a constant. */
2611 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2612 _("Negative of non-absolute symbol %s"),
2613 S_GET_NAME (sub_symbolP
));
2615 else if (S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
2616 && SEG_NORMAL (add_symbol_segment
))
2618 /* Difference of 2 symbols from same segment.
2619 Can't make difference of 2 undefineds: 'value' means
2620 something different for N_UNDF. */
2622 /* Makes no sense to use the difference of 2 arbitrary symbols
2623 as the target of a call instruction. */
2625 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2626 _("callj to difference of 2 symbols"));
2627 #endif /* TC_I960 */
2628 add_number
+= S_GET_VALUE (add_symbolP
) -
2629 S_GET_VALUE (sub_symbolP
);
2632 pcrel
= 0; /* No further pcrel processing. */
2634 /* Let the target machine make the final determination
2635 as to whether or not a relocation will be needed to
2636 handle this fixup. */
2637 if (!TC_FORCE_RELOCATION_SECTION (fixP
, this_segment_type
))
2640 fixP
->fx_addsy
= NULL
;
2641 fixP
->fx_subsy
= NULL
;
2646 /* Different segments in subtraction. */
2647 know (!(S_IS_EXTERNAL (sub_symbolP
)
2648 && (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)));
2650 if ((S_GET_SEGMENT (sub_symbolP
) == absolute_section
))
2651 add_number
-= S_GET_VALUE (sub_symbolP
);
2654 else if (S_GET_SEGMENT (sub_symbolP
) == this_segment_type
2656 /* Do this even if it's already described as
2657 pc-relative. For example, on the m68k, an
2658 operand of "pc@(foo-.-2)" should address
2659 "foo" in a pc-relative mode. */
2664 /* Make it pc-relative. */
2665 add_number
+= (MD_PCREL_FROM_SECTION (fixP
, this_segment_type
)
2666 - S_GET_VALUE (sub_symbolP
));
2673 #ifdef UNDEFINED_DIFFERENCE_OK
2674 /* The PA needs this for PIC code generation. We basically
2675 don't want to do anything if we have the difference of two
2676 symbols at this point. */
2679 /* Leave it alone. */
2682 #ifdef BFD_ASSEMBLER
2683 else if (fixP
->fx_r_type
== BFD_RELOC_GPREL32
2684 || fixP
->fx_r_type
== BFD_RELOC_GPREL16
)
2686 /* Leave it alone. */
2692 sprint_value (buf
, fragP
->fr_address
+ where
);
2693 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2694 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2695 S_GET_NAME (add_symbolP
),
2696 segment_name (S_GET_SEGMENT (add_symbolP
)),
2697 S_GET_NAME (sub_symbolP
),
2698 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2706 if (add_symbol_segment
== this_segment_type
&& pcrel
&& !plt
2707 && TC_RELOC_RTSYM_LOC_FIXUP (fixP
))
2709 /* This fixup was made when the symbol's segment was
2710 SEG_UNKNOWN, but it is now in the local segment.
2711 So we know how to do the address without relocation. */
2713 /* reloc_callj() may replace a 'call' with a 'calls' or a
2714 'bal', in which cases it modifies *fixP as appropriate.
2715 In the case of a 'calls', no further work is required,
2716 and *fixP has been set up to make the rest of the code
2719 #endif /* TC_I960 */
2721 add_number
+= S_GET_VALUE (add_symbolP
);
2722 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2723 /* Lie. Don't want further pcrel processing. */
2726 /* Let the target machine make the final determination
2727 as to whether or not a relocation will be needed to
2728 handle this fixup. */
2729 if (!TC_FORCE_RELOCATION (fixP
))
2732 fixP
->fx_addsy
= NULL
;
2737 if (add_symbol_segment
== absolute_section
2741 /* See comment about reloc_callj() above. */
2743 #endif /* TC_I960 */
2744 add_number
+= S_GET_VALUE (add_symbolP
);
2746 /* Let the target machine make the final determination
2747 as to whether or not a relocation will be needed to
2748 handle this fixup. */
2750 if (!TC_FORCE_RELOCATION (fixP
))
2752 fixP
->fx_addsy
= NULL
;
2756 else if (add_symbol_segment
== undefined_section
2757 #ifdef BFD_ASSEMBLER
2758 || bfd_is_com_section (add_symbol_segment
)
2763 if ((int) fixP
->fx_bit_fixP
== 13)
2765 /* This is a COBR instruction. They have only a
2766 13-bit displacement and are only to be used
2767 for local branches: flag as error, don't generate
2769 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2770 _("can't use COBR format with external label"));
2771 fixP
->fx_addsy
= NULL
;
2775 #endif /* TC_I960 */
2779 if (S_IS_COMMON (add_symbolP
))
2780 add_number
+= S_GET_VALUE (add_symbolP
);
2781 #endif /* TE_I386AIX */
2782 #endif /* OBJ_COFF */
2788 if (TC_FIX_ADJUSTABLE (fixP
))
2789 add_number
+= S_GET_VALUE (add_symbolP
);
2796 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2797 if (add_symbolP
== 0)
2799 #ifndef BFD_ASSEMBLER
2800 fixP
->fx_addsy
= &abs_symbol
;
2802 fixP
->fx_addsy
= section_symbol (absolute_section
);
2804 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2811 #ifdef MD_APPLY_FIX3
2812 md_apply_fix3 (fixP
, &add_number
, this_segment_type
);
2814 #ifdef BFD_ASSEMBLER
2815 md_apply_fix (fixP
, &add_number
);
2817 md_apply_fix (fixP
, add_number
);
2821 #ifndef TC_HANDLES_FX_DONE
2822 /* If the tc-* files haven't been converted, assume it's handling
2823 it the old way, where a null fx_addsy means that the fix has
2824 been applied completely, and no further work is needed. */
2825 if (fixP
->fx_addsy
== 0 && fixP
->fx_pcrel
== 0)
2830 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& size
> 0)
2832 if ((size_t) size
< sizeof (valueT
))
2837 mask
--; /* Set all bits to one. */
2838 mask
<<= size
* 8 - (fixP
->fx_signed
? 1 : 0);
2839 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
2841 char buf
[50], buf2
[50];
2842 sprint_value (buf
, fragP
->fr_address
+ where
);
2843 if (add_number
> 1000)
2844 sprint_value (buf2
, add_number
);
2846 sprintf (buf2
, "%ld", (long) add_number
);
2847 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2848 _("Value of %s too large for field of %d bytes at %s"),
2850 } /* Generic error checking. */
2852 #ifdef WARN_SIGNED_OVERFLOW_WORD
2853 /* Warn if a .word value is too large when treated as a signed
2854 number. We already know it is not too negative. This is to
2855 catch over-large switches generated by gcc on the 68k. */
2856 if (!flag_signed_overflow_ok
2858 && add_number
> 0x7fff)
2859 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2860 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2862 (unsigned long) (fragP
->fr_address
+ where
));
2864 } /* Not a bit fix. */
2866 #ifdef TC_VALIDATE_FIX
2867 skip
: ATTRIBUTE_UNUSED_LABEL
2871 fprintf (stderr
, "result:\n");
2874 } /* For each fixS in this segment. */
2876 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2877 return seg_reloc_count
;
2880 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2883 number_to_chars_bigendian (buf
, val
, n
)
2888 if ((size_t) n
> sizeof (val
) || n
<= 0)
2892 buf
[n
] = val
& 0xff;
2898 number_to_chars_littleendian (buf
, val
, n
)
2903 if ((size_t) n
> sizeof (val
) || n
<= 0)
2907 *buf
++ = val
& 0xff;
2913 write_print_statistics (file
)
2916 fprintf (file
, "fixups: %d\n", n_fixups
);
2919 /* For debugging. */
2920 extern int indent_level
;
2927 fprintf (stderr
, "fix %lx %s:%d", (long) fixp
, fixp
->fx_file
, fixp
->fx_line
);
2929 fprintf (stderr
, " pcrel");
2930 if (fixp
->fx_pcrel_adjust
)
2931 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2932 if (fixp
->fx_im_disp
)
2935 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2937 fprintf (stderr
, " im_disp");
2941 fprintf (stderr
, " tcbit");
2943 fprintf (stderr
, " done");
2944 fprintf (stderr
, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2945 fixp
->fx_size
, (long) fixp
->fx_frag
, (long) fixp
->fx_where
,
2946 (long) fixp
->fx_offset
, (long) fixp
->fx_addnumber
);
2947 #ifdef BFD_ASSEMBLER
2948 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2951 #ifdef NEED_FX_R_TYPE
2952 fprintf (stderr
, " r_type=%d", fixp
->fx_r_type
);
2957 fprintf (stderr
, "\n +<");
2958 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2959 fprintf (stderr
, ">");
2963 fprintf (stderr
, "\n -<");
2964 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2965 fprintf (stderr
, ">");
2967 fprintf (stderr
, "\n");
2968 #ifdef TC_FIX_DATA_PRINT
2969 TC_FIX_DATA_PRINT (stderr
, fixp
);