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. */
65 int finalize_syms
= 0;
67 int symbol_table_frozen
;
68 void print_fixup
PARAMS ((fixS
*));
71 static void renumber_sections
PARAMS ((bfd
*, asection
*, PTR
));
73 /* We generally attach relocs to frag chains. However, after we have
74 chained these all together into a segment, any relocs we add after
75 that must be attached to a segment. This will include relocs added
76 in md_estimate_size_for_relax, for example. */
77 static int frags_chained
= 0;
83 struct frag
*text_frag_root
;
84 struct frag
*data_frag_root
;
85 struct frag
*bss_frag_root
;
87 struct frag
*text_last_frag
; /* Last frag in segment. */
88 struct frag
*data_last_frag
; /* Last frag in segment. */
89 static struct frag
*bss_last_frag
; /* Last frag in segment. */
93 static object_headers headers
;
96 long string_byte_count
;
97 char *next_object_file_charP
; /* Tracks object file bytes. */
100 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
103 #endif /* BFD_ASSEMBLER */
108 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
109 symbolS
*add
, symbolS
*sub
,
110 offsetT offset
, int pcrel
,
111 bfd_reloc_code_real_type r_type
));
113 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
114 symbolS
*add
, symbolS
*sub
,
115 offsetT offset
, int pcrel
,
118 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
119 static long fixup_segment
PARAMS ((fixS
* fixP
, segT this_segment_type
));
121 static relax_addressT relax_align
PARAMS ((relax_addressT addr
, int align
));
122 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
123 static fragS
*chain_frchains_together_1
PARAMS ((segT
, struct frchain
*));
126 static void chain_frchains_together
PARAMS ((bfd
*, segT
, PTR
));
127 static void cvt_frag_to_fill
PARAMS ((segT
, fragS
*));
128 static void adjust_reloc_syms
PARAMS ((bfd
*, asection
*, PTR
));
129 static void write_relocs
PARAMS ((bfd
*, asection
*, PTR
));
130 static void write_contents
PARAMS ((bfd
*, asection
*, PTR
));
131 static void set_symtab
PARAMS ((void));
133 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
134 static void merge_data_into_text
PARAMS ((void));
136 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
137 static void cvt_frag_to_fill
PARAMS ((object_headers
*, segT
, fragS
*));
138 static void remove_subsegs
PARAMS ((frchainS
*, int, fragS
**, fragS
**));
139 static void relax_and_size_all_segments
PARAMS ((void));
141 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
142 static void set_segment_vma
PARAMS ((bfd
*, asection
*, PTR
));
145 /* Create a fixS in obstack 'notes'. */
148 fix_new_internal (frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
,
150 fragS
*frag
; /* Which frag? */
151 int where
; /* Where in that frag? */
152 int size
; /* 1, 2, or 4 usually. */
153 symbolS
*add_symbol
; /* X_add_symbol. */
154 symbolS
*sub_symbol
; /* X_op_symbol. */
155 offsetT offset
; /* X_add_number. */
156 int pcrel
; /* TRUE if PC-relative relocation. */
158 bfd_reloc_code_real_type r_type
; /* Relocation type. */
160 int r_type
; /* Relocation type. */
167 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
169 fixP
->fx_frag
= frag
;
170 fixP
->fx_where
= where
;
171 fixP
->fx_size
= size
;
172 /* We've made fx_size a narrow field; check that it's wide enough. */
173 if (fixP
->fx_size
!= size
)
175 as_bad (_("field fx_size too small to hold %d"), size
);
178 fixP
->fx_addsy
= add_symbol
;
179 fixP
->fx_subsy
= sub_symbol
;
180 fixP
->fx_offset
= offset
;
181 fixP
->fx_pcrel
= pcrel
;
183 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
184 fixP
->fx_r_type
= r_type
;
186 fixP
->fx_im_disp
= 0;
187 fixP
->fx_pcrel_adjust
= 0;
188 fixP
->fx_bit_fixP
= 0;
189 fixP
->fx_addnumber
= 0;
192 fixP
->fx_no_overflow
= 0;
196 fixP
->fx_cgen
.insn
= NULL
;
197 fixP
->fx_cgen
.opinfo
= 0;
201 TC_INIT_FIX_DATA (fixP
);
204 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
206 /* Usually, we want relocs sorted numerically, but while
207 comparing to older versions of gas that have relocs
208 reverse sorted, it is convenient to have this compile
209 time option. xoxorich. */
213 fixS
**seg_fix_rootP
= (frags_chained
214 ? &seg_info (now_seg
)->fix_root
215 : &frchain_now
->fix_root
);
216 fixS
**seg_fix_tailP
= (frags_chained
217 ? &seg_info (now_seg
)->fix_tail
218 : &frchain_now
->fix_tail
);
221 #ifdef REVERSE_SORT_RELOCS
223 fixP
->fx_next
= *seg_fix_rootP
;
224 *seg_fix_rootP
= fixP
;
226 #else /* REVERSE_SORT_RELOCS */
228 fixP
->fx_next
= NULL
;
231 (*seg_fix_tailP
)->fx_next
= fixP
;
233 *seg_fix_rootP
= fixP
;
234 *seg_fix_tailP
= fixP
;
236 #endif /* REVERSE_SORT_RELOCS */
242 /* Create a fixup relative to a symbol (plus a constant). */
245 fix_new (frag
, where
, size
, add_symbol
, offset
, pcrel
, r_type
)
246 fragS
*frag
; /* Which frag? */
247 int where
; /* Where in that frag? */
248 int size
; /* 1, 2, or 4 usually. */
249 symbolS
*add_symbol
; /* X_add_symbol. */
250 offsetT offset
; /* X_add_number. */
251 int pcrel
; /* TRUE if PC-relative relocation. */
253 bfd_reloc_code_real_type r_type
; /* Relocation type. */
255 int r_type
; /* Relocation type. */
258 return fix_new_internal (frag
, where
, size
, add_symbol
,
259 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
262 /* Create a fixup for an expression. Currently we only support fixups
263 for difference expressions. That is itself more than most object
264 file formats support anyhow. */
267 fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
)
268 fragS
*frag
; /* Which frag? */
269 int where
; /* Where in that frag? */
270 int size
; /* 1, 2, or 4 usually. */
271 expressionS
*exp
; /* Expression. */
272 int pcrel
; /* TRUE if PC-relative relocation. */
274 bfd_reloc_code_real_type r_type
; /* Relocation type. */
276 int r_type
; /* Relocation type. */
289 as_bad (_("register value used as expression"));
293 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
294 the difference expression cannot immediately be reduced. */
296 symbolS
*stmp
= make_expr_symbol (exp
);
298 exp
->X_op
= O_symbol
;
299 exp
->X_op_symbol
= 0;
300 exp
->X_add_symbol
= stmp
;
301 exp
->X_add_number
= 0;
303 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
307 add
= exp
->X_add_symbol
;
308 off
= exp
->X_add_number
;
310 #if defined(BFD_ASSEMBLER)
311 r_type
= BFD_RELOC_RVA
;
313 #if defined(TC_RVA_RELOC)
314 r_type
= TC_RVA_RELOC
;
316 as_fatal (_("rva not supported"));
322 sub
= exp
->X_add_symbol
;
323 off
= exp
->X_add_number
;
327 sub
= exp
->X_op_symbol
;
330 add
= exp
->X_add_symbol
;
333 off
= exp
->X_add_number
;
337 add
= make_expr_symbol (exp
);
341 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
, r_type
);
344 /* Append a string onto another string, bumping the pointer along. */
346 append (charPP
, fromP
, length
)
349 unsigned long length
;
351 /* Don't trust memcpy() of 0 chars. */
355 memcpy (*charPP
, fromP
, length
);
359 #ifndef BFD_ASSEMBLER
360 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
363 /* This routine records the largest alignment seen for each segment.
364 If the beginning of the segment is aligned on the worst-case
365 boundary, all of the other alignments within it will work. At
366 least one object format really uses this info. */
369 record_alignment (seg
, align
)
370 /* Segment to which alignment pertains. */
372 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
376 if (seg
== absolute_section
)
379 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
380 bfd_set_section_alignment (stdoutput
, seg
, align
);
382 if (align
> section_alignment
[(int) seg
])
383 section_alignment
[(int) seg
] = align
;
388 get_recorded_alignment (seg
)
391 if (seg
== absolute_section
)
394 return bfd_get_section_alignment (stdoutput
, seg
);
396 return section_alignment
[(int) seg
];
402 /* Reset the section indices after removing the gas created sections. */
405 renumber_sections (abfd
, sec
, countparg
)
406 bfd
*abfd ATTRIBUTE_UNUSED
;
410 int *countp
= (int *) countparg
;
412 sec
->index
= *countp
;
416 #endif /* defined (BFD_ASSEMBLER) */
418 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
421 chain_frchains_together_1 (section
, frchp
)
423 struct frchain
*frchp
;
425 fragS dummy
, *prev_frag
= &dummy
;
427 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
430 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
432 prev_frag
->fr_next
= frchp
->frch_root
;
433 prev_frag
= frchp
->frch_last
;
434 assert (prev_frag
->fr_type
!= 0);
436 if (frchp
->fix_root
!= (fixS
*) NULL
)
438 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
439 seg_info (section
)->fix_root
= frchp
->fix_root
;
440 prev_fix
->fx_next
= frchp
->fix_root
;
441 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
442 prev_fix
= frchp
->fix_tail
;
446 assert (prev_frag
->fr_type
!= 0);
447 prev_frag
->fr_next
= 0;
456 chain_frchains_together (abfd
, section
, xxx
)
457 bfd
*abfd ATTRIBUTE_UNUSED
;
459 PTR xxx ATTRIBUTE_UNUSED
;
461 segment_info_type
*info
;
463 /* BFD may have introduced its own sections without using
464 subseg_new, so it is possible that seg_info is NULL. */
465 info
= seg_info (section
);
466 if (info
!= (segment_info_type
*) NULL
)
467 info
->frchainP
->frch_last
468 = chain_frchains_together_1 (section
, info
->frchainP
);
470 /* Now that we've chained the frags together, we must add new fixups
471 to the segment, not to the frag chain. */
477 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
480 remove_subsegs (head
, seg
, root
, last
)
486 *root
= head
->frch_root
;
487 *last
= chain_frchains_together_1 (seg
, head
);
492 #if defined (BFD_ASSEMBLER) || !defined (BFD)
496 cvt_frag_to_fill (sec
, fragP
)
497 segT sec ATTRIBUTE_UNUSED
;
501 cvt_frag_to_fill (headersP
, sec
, fragP
)
502 object_headers
*headersP
;
507 switch (fragP
->fr_type
)
515 HANDLE_ALIGN (fragP
);
517 know (fragP
->fr_next
!= NULL
);
518 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
520 - fragP
->fr_fix
) / fragP
->fr_var
;
521 if (fragP
->fr_offset
< 0)
523 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
524 _("attempt to .org/.space backwards? (%ld)"),
525 (long) fragP
->fr_offset
);
527 fragP
->fr_type
= rs_fill
;
535 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
538 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
541 fragP
->fr_fix
+= size
;
542 fragP
->fr_type
= rs_fill
;
544 fragP
->fr_offset
= 0;
545 fragP
->fr_symbol
= NULL
;
550 eh_frame_convert_frag (fragP
);
554 dwarf2dbg_convert_frag (fragP
);
557 case rs_machine_dependent
:
559 md_convert_frag (stdoutput
, sec
, fragP
);
561 md_convert_frag (headersP
, sec
, fragP
);
564 assert (fragP
->fr_next
== NULL
565 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
568 /* After md_convert_frag, we make the frag into a ".space 0".
569 md_convert_frag() should set up any fixSs and constants
574 #ifndef WORKING_DOT_WORD
577 struct broken_word
*lie
;
579 if (fragP
->fr_subtype
)
581 fragP
->fr_fix
+= md_short_jump_size
;
582 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
583 lie
&& lie
->dispfrag
== fragP
;
584 lie
= lie
->next_broken_word
)
586 fragP
->fr_fix
+= md_long_jump_size
;
594 BAD_CASE (fragP
->fr_type
);
599 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
602 static void relax_seg
PARAMS ((bfd
*, asection
*, PTR
));
604 relax_seg (abfd
, sec
, xxx
)
605 bfd
*abfd ATTRIBUTE_UNUSED
;
609 segment_info_type
*seginfo
= seg_info (sec
);
611 if (seginfo
&& seginfo
->frchainP
612 && relax_segment (seginfo
->frchainP
->frch_root
, sec
))
614 int *result
= (int *) xxx
;
619 static void size_seg
PARAMS ((bfd
*, asection
*, PTR
));
621 size_seg (abfd
, sec
, xxx
)
624 PTR xxx ATTRIBUTE_UNUSED
;
628 segment_info_type
*seginfo
;
630 valueT size
, newsize
;
632 subseg_change (sec
, 0);
634 seginfo
= seg_info (sec
);
635 if (seginfo
&& seginfo
->frchainP
)
637 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
638 cvt_frag_to_fill (sec
, fragp
);
639 for (fragp
= seginfo
->frchainP
->frch_root
;
641 fragp
= fragp
->fr_next
)
642 /* Walk to last elt. */
644 size
= fragp
->fr_address
+ fragp
->fr_fix
;
649 flags
= bfd_get_section_flags (abfd
, sec
);
651 if (size
> 0 && ! seginfo
->bss
)
652 flags
|= SEC_HAS_CONTENTS
;
654 /* @@ This is just an approximation. */
655 if (seginfo
&& seginfo
->fix_root
)
659 x
= bfd_set_section_flags (abfd
, sec
, flags
);
662 newsize
= md_section_align (sec
, size
);
663 x
= bfd_set_section_size (abfd
, sec
, newsize
);
666 /* If the size had to be rounded up, add some padding in the last
668 assert (newsize
>= size
);
671 fragS
*last
= seginfo
->frchainP
->frch_last
;
672 fragp
= seginfo
->frchainP
->frch_root
;
673 while (fragp
->fr_next
!= last
)
674 fragp
= fragp
->fr_next
;
675 last
->fr_address
= size
;
676 fragp
->fr_offset
+= newsize
- size
;
679 #ifdef tc_frob_section
680 tc_frob_section (sec
);
682 #ifdef obj_frob_section
683 obj_frob_section (sec
);
689 dump_section_relocs (abfd
, sec
, stream_
)
690 bfd
*abfd ATTRIBUTE_UNUSED
;
694 FILE *stream
= (FILE *) stream_
;
695 segment_info_type
*seginfo
= seg_info (sec
);
696 fixS
*fixp
= seginfo
->fix_root
;
701 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
704 symbolS
*s
= fixp
->fx_addsy
;
706 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
707 (int) fixp
->fx_r_type
);
709 fprintf (stream
, "no sym\n");
712 print_symbol_value_1 (stream
, s
);
713 fprintf (stream
, "\n");
715 fixp
= fixp
->fx_next
;
719 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
722 #ifndef EMIT_SECTION_SYMBOLS
723 #define EMIT_SECTION_SYMBOLS 1
727 adjust_reloc_syms (abfd
, sec
, xxx
)
728 bfd
*abfd ATTRIBUTE_UNUSED
;
730 PTR xxx ATTRIBUTE_UNUSED
;
732 segment_info_type
*seginfo
= seg_info (sec
);
738 dump_section_relocs (abfd
, sec
, stderr
);
740 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
744 else if (fixp
->fx_addsy
)
750 fprintf (stderr
, "\n\nadjusting fixup:\n");
754 sym
= fixp
->fx_addsy
;
756 /* All symbols should have already been resolved at this
757 point. It is possible to see unresolved expression
758 symbols, though, since they are not in the regular symbol
761 resolve_symbol_value (sym
);
763 if (fixp
->fx_subsy
!= NULL
)
764 resolve_symbol_value (fixp
->fx_subsy
);
766 /* If this symbol is equated to an undefined symbol, convert
767 the fixup to being against that symbol. */
768 if (sym
!= NULL
&& symbol_equated_p (sym
)
769 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
771 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
772 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
773 fixp
->fx_addsy
= sym
;
776 if (sym
!= NULL
&& symbol_mri_common_p (sym
))
778 /* These symbols are handled specially in fixup_segment. */
782 symsec
= S_GET_SEGMENT (sym
);
787 if (bfd_is_abs_section (symsec
))
789 /* The fixup_segment routine will not use this symbol in a
790 relocation unless TC_FORCE_RELOCATION returns 1. */
791 if (TC_FORCE_RELOCATION (fixp
))
793 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
794 #ifdef UNDEFINED_DIFFERENCE_OK
795 if (fixp
->fx_subsy
!= NULL
)
796 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
802 /* If it's one of these sections, assume the symbol is
803 definitely going to be output. The code in
804 md_estimate_size_before_relax in tc-mips.c uses this test
805 as well, so if you change this code you should look at that
807 if (bfd_is_und_section (symsec
)
808 || bfd_is_com_section (symsec
))
810 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
811 #ifdef UNDEFINED_DIFFERENCE_OK
812 /* We have the difference of an undefined symbol and some
813 other symbol. Make sure to mark the other symbol as used
814 in a relocation so that it will always be output. */
816 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
821 /* Don't try to reduce relocs which refer to non-local symbols
822 in .linkonce sections. It can lead to confusion when a
823 debugging section refers to a .linkonce section. I hope
824 this will always be correct. */
825 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
831 if ((bfd_get_section_flags (stdoutput
, symsec
) & SEC_LINK_ONCE
)
836 /* The GNU toolchain uses an extension for ELF: a section
837 beginning with the magic string .gnu.linkonce is a
839 if (strncmp (segment_name (symsec
), ".gnu.linkonce",
840 sizeof ".gnu.linkonce" - 1) == 0)
846 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
847 #ifdef UNDEFINED_DIFFERENCE_OK
848 if (fixp
->fx_subsy
!= NULL
)
849 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
855 /* Since we're reducing to section symbols, don't attempt to reduce
856 anything that's already using one. */
857 if (symbol_section_p (sym
))
859 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
864 /* We can never adjust a reloc against a weak symbol. If we
865 did, and the weak symbol was overridden by a real symbol
866 somewhere else, then our relocation would be pointing at
867 the wrong area of memory. */
870 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
874 /* Never adjust a reloc against local symbol in a merge section. */
875 if (symsec
->flags
& SEC_MERGE
)
877 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
882 /* Is there some other reason we can't adjust this one? (E.g.,
883 call/bal links in i960-bout symbols.) */
884 #ifdef obj_fix_adjustable
885 if (! obj_fix_adjustable (fixp
))
887 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
892 /* Is there some other (target cpu dependent) reason we can't adjust
893 this one? (E.g. relocations involving function addresses on
895 #ifdef tc_fix_adjustable
896 if (! tc_fix_adjustable (fixp
))
898 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
903 /* If the section symbol isn't going to be output, the relocs
904 at least should still work. If not, figure out what to do
905 when we run into that case.
907 We refetch the segment when calling section_symbol, rather
908 than using symsec, because S_GET_VALUE may wind up changing
909 the section when it calls resolve_symbol_value. */
910 fixp
->fx_offset
+= S_GET_VALUE (sym
);
911 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
912 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
914 fprintf (stderr
, "\nadjusted fixup:\n");
921 #if 1 /* def RELOC_REQUIRES_SYMBOL */
924 /* There was no symbol required by this relocation. However,
925 BFD doesn't really handle relocations without symbols well.
926 (At least, the COFF support doesn't.) So for now we fake up
927 a local symbol in the absolute section. */
929 fixp
->fx_addsy
= section_symbol (absolute_section
);
931 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
936 dump_section_relocs (abfd
, sec
, stderr
);
940 write_relocs (abfd
, sec
, xxx
)
943 PTR xxx ATTRIBUTE_UNUSED
;
945 segment_info_type
*seginfo
= seg_info (sec
);
952 /* If seginfo is NULL, we did not create this section; don't do
957 fixup_segment (seginfo
->fix_root
, sec
);
960 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
963 #ifndef RELOC_EXPANSION_POSSIBLE
964 /* Set up reloc information as well. */
965 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
966 memset ((char *) relocs
, 0, n
* sizeof (arelent
*));
969 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
972 bfd_reloc_status_type s
;
981 /* If this is an undefined symbol which was equated to another
982 symbol, then use generate the reloc against the latter symbol
983 rather than the former. */
984 sym
= fixp
->fx_addsy
;
985 while (symbol_equated_p (sym
)
986 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
990 /* We must avoid looping, as that can occur with a badly
992 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
995 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
998 fixp
->fx_addsy
= sym
;
1000 reloc
= tc_gen_reloc (sec
, fixp
);
1008 /* This test is triggered inappropriately for the SH. */
1009 if (fixp
->fx_where
+ fixp
->fx_size
1010 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1014 s
= bfd_install_relocation (stdoutput
, reloc
,
1015 fixp
->fx_frag
->fr_literal
,
1016 fixp
->fx_frag
->fr_address
,
1022 case bfd_reloc_overflow
:
1023 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation overflow"));
1025 case bfd_reloc_outofrange
:
1026 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation out of range"));
1029 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1030 fixp
->fx_file
, fixp
->fx_line
, s
);
1032 relocs
[i
++] = reloc
;
1035 n
= n
* MAX_RELOC_EXPANSION
;
1036 /* Set up reloc information as well. */
1037 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
1040 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1044 bfd_reloc_status_type s
;
1054 /* If this is an undefined symbol which was equated to another
1055 symbol, then generate the reloc against the latter symbol
1056 rather than the former. */
1057 sym
= fixp
->fx_addsy
;
1058 while (symbol_equated_p (sym
)
1059 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
1060 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
1061 fixp
->fx_addsy
= sym
;
1063 reloc
= tc_gen_reloc (sec
, fixp
);
1065 for (j
= 0; reloc
[j
]; j
++)
1067 relocs
[i
++] = reloc
[j
];
1070 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1071 if (fixp
->fx_where
+ fixp
->fx_size
1072 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1073 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1074 _("internal error: fixup not contained within frag"));
1075 for (j
= 0; reloc
[j
]; j
++)
1077 s
= bfd_install_relocation (stdoutput
, reloc
[j
],
1078 fixp
->fx_frag
->fr_literal
,
1079 fixp
->fx_frag
->fr_address
,
1085 case bfd_reloc_overflow
:
1086 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1087 _("relocation overflow"));
1090 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1091 fixp
->fx_file
, fixp
->fx_line
);
1102 sympp
= bfd_get_outsymbols (stdoutput
);
1103 nsyms
= bfd_get_symcount (stdoutput
);
1104 for (i
= 0; i
< n
; i
++)
1105 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1107 for (j
= 0; j
< nsyms
; j
++)
1108 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
1117 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1119 bfd_set_section_flags (abfd
, sec
,
1120 (bfd_get_section_flags (abfd
, sec
)
1121 & (flagword
) ~SEC_RELOC
));
1123 #ifdef SET_SECTION_RELOCS
1124 SET_SECTION_RELOCS (sec
, relocs
, n
);
1132 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1133 for (i
= 0; i
< n
; i
++)
1136 s
= *r
->sym_ptr_ptr
;
1137 fprintf (stderr
, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1138 i
, r
, r
->address
, s
->name
, r
->addend
);
1145 write_contents (abfd
, sec
, xxx
)
1146 bfd
*abfd ATTRIBUTE_UNUSED
;
1148 PTR xxx ATTRIBUTE_UNUSED
;
1150 segment_info_type
*seginfo
= seg_info (sec
);
1151 unsigned long offset
= 0;
1154 /* Write out the frags. */
1156 || !(bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1159 for (f
= seginfo
->frchainP
->frch_root
;
1164 unsigned long fill_size
;
1168 assert (f
->fr_type
== rs_fill
);
1171 x
= bfd_set_section_contents (stdoutput
, sec
,
1172 f
->fr_literal
, (file_ptr
) offset
,
1173 (bfd_size_type
) f
->fr_fix
);
1176 bfd_perror (stdoutput
->filename
);
1177 as_perror (_("FATAL: Can't write %s"), stdoutput
->filename
);
1178 exit (EXIT_FAILURE
);
1180 offset
+= f
->fr_fix
;
1182 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1183 fill_size
= f
->fr_var
;
1184 count
= f
->fr_offset
;
1185 assert (count
>= 0);
1186 if (fill_size
&& count
)
1189 if (fill_size
> sizeof (buf
))
1191 /* Do it the old way. Can this ever happen? */
1194 x
= bfd_set_section_contents (stdoutput
, sec
,
1197 (bfd_size_type
) fill_size
);
1200 bfd_perror (stdoutput
->filename
);
1201 as_perror (_("FATAL: Can't write %s"),
1202 stdoutput
->filename
);
1203 exit (EXIT_FAILURE
);
1205 offset
+= fill_size
;
1210 /* Build a buffer full of fill objects and output it as
1211 often as necessary. This saves on the overhead of
1212 potentially lots of bfd_set_section_contents calls. */
1216 n_per_buf
= sizeof (buf
);
1217 memset (buf
, *fill_literal
, n_per_buf
);
1222 n_per_buf
= sizeof (buf
) / fill_size
;
1223 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1224 memcpy (bufp
, fill_literal
, fill_size
);
1226 for (; count
> 0; count
-= n_per_buf
)
1228 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1229 x
= bfd_set_section_contents
1230 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1231 (bfd_size_type
) n_per_buf
* fill_size
);
1233 as_fatal (_("Cannot write to output file."));
1234 offset
+= n_per_buf
* fill_size
;
1242 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1244 merge_data_into_text ()
1246 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1247 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1248 seg_info (data_section
)->frchainP
->frch_root
;
1249 seg_info (text_section
)->frchainP
->frch_last
=
1250 seg_info (data_section
)->frchainP
->frch_last
;
1251 seg_info (data_section
)->frchainP
= 0;
1255 text_last_frag
->fr_next
= data_frag_root
;
1256 text_last_frag
= data_last_frag
;
1257 data_last_frag
= NULL
;
1258 data_frag_root
= NULL
;
1261 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
1262 tmp
->fx_next
= data_fix_root
;
1263 text_fix_tail
= data_fix_tail
;
1266 text_fix_root
= data_fix_root
;
1267 data_fix_root
= NULL
;
1270 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1272 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1274 relax_and_size_all_segments ()
1278 relax_segment (text_frag_root
, SEG_TEXT
);
1279 relax_segment (data_frag_root
, SEG_DATA
);
1280 relax_segment (bss_frag_root
, SEG_BSS
);
1282 /* Now the addresses of frags are correct within the segment. */
1283 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
1284 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
1285 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
1287 /* Join the 2 segments into 1 huge segment.
1288 To do this, re-compute every rn_address in the SEG_DATA frags.
1289 Then join the data frags after the text frags.
1291 Determine a_data [length of data segment]. */
1294 register relax_addressT slide
;
1296 know ((text_last_frag
->fr_type
== rs_fill
)
1297 && (text_last_frag
->fr_offset
== 0));
1299 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
1300 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
1301 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
1303 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1304 /* For b.out: If the data section has a strict alignment
1305 requirement, its load address in the .o file will be
1306 rounded up from the size of the text section. These
1307 two values are *not* the same! Similarly for the bss
1309 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
1312 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1313 fragP
->fr_address
+= slide
;
1315 know (text_last_frag
!= 0);
1316 text_last_frag
->fr_next
= data_frag_root
;
1320 H_SET_DATA_SIZE (&headers
, 0);
1324 /* See above comments on b.out data section address. */
1327 if (data_last_frag
== 0)
1328 bss_vma
= H_GET_TEXT_SIZE (&headers
);
1330 bss_vma
= data_last_frag
->fr_address
;
1331 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
1332 bss_address_frag
.fr_address
= bss_vma
;
1334 #else /* ! OBJ_BOUT */
1335 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
1336 H_GET_DATA_SIZE (&headers
));
1338 #endif /* ! OBJ_BOUT */
1340 /* Slide all the frags. */
1343 relax_addressT slide
= bss_address_frag
.fr_address
;
1345 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1346 fragP
->fr_address
+= slide
;
1350 H_SET_BSS_SIZE (&headers
,
1351 bss_last_frag
->fr_address
- bss_frag_root
->fr_address
);
1353 H_SET_BSS_SIZE (&headers
, 0);
1355 #endif /* ! BFD_ASSEMBLER && ! BFD */
1357 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1359 #ifdef BFD_ASSEMBLER
1367 extern PTR bfd_alloc
PARAMS ((bfd
*, size_t));
1369 /* Count symbols. We can't rely on a count made by the loop in
1370 write_object_file, because *_frob_file may add a new symbol or
1373 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1380 asympp
= (asymbol
**) bfd_alloc (stdoutput
,
1381 nsyms
* sizeof (asymbol
*));
1382 symp
= symbol_rootP
;
1383 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1385 asympp
[i
] = symbol_get_bfdsym (symp
);
1386 symbol_mark_written (symp
);
1391 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1392 assert (result
== true);
1393 symbol_table_frozen
= 1;
1397 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1399 set_segment_vma (abfd
, sec
, xxx
)
1402 PTR xxx ATTRIBUTE_UNUSED
;
1404 static bfd_vma addr
= 0;
1406 bfd_set_section_vma (abfd
, sec
, addr
);
1407 addr
+= bfd_section_size (abfd
, sec
);
1409 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1411 /* Finish the subsegments. After every sub-segment, we fake an
1412 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1413 ".fill 0" because that is the kind of frag that requires least
1414 thought. ".align" frags like to have a following frag since that
1415 makes calculating their intended length trivial. */
1417 #ifndef SUB_SEGMENT_ALIGN
1418 #ifdef BFD_ASSEMBLER
1419 #define SUB_SEGMENT_ALIGN(SEG) (0)
1421 #define SUB_SEGMENT_ALIGN(SEG) (2)
1428 struct frchain
*frchainP
;
1430 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
1434 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
1436 /* This now gets called even if we had errors. In that case,
1437 any alignment is meaningless, and, moreover, will look weird
1438 if we are generating a listing. */
1439 alignment
= had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg
);
1441 /* The last subsegment gets an aligment corresponding to the
1442 alignment of the section. This allows proper nop-filling
1443 at the end of code-bearing sections. */
1444 if (!frchainP
->frch_next
|| frchainP
->frch_next
->frch_seg
!= now_seg
)
1445 alignment
= get_recorded_alignment (now_seg
);
1447 if (subseg_text_p (now_seg
))
1448 frag_align_code (alignment
, 0);
1450 frag_align (alignment
, 0, 0);
1452 /* frag_align will have left a new frag.
1453 Use this last frag for an empty ".fill".
1455 For this segment ...
1456 Create a last frag. Do not leave a "being filled in frag". */
1457 frag_wane (frag_now
);
1458 frag_now
->fr_fix
= 0;
1459 know (frag_now
->fr_next
== NULL
);
1463 /* Write the object file. */
1466 write_object_file ()
1468 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1469 fragS
*fragP
; /* Track along all frags. */
1472 /* Do we really want to write it? */
1474 int n_warns
, n_errs
;
1475 n_warns
= had_warnings ();
1476 n_errs
= had_errors ();
1477 /* The -Z flag indicates that an object file should be generated,
1478 regardless of warnings and errors. */
1479 if (flag_always_generate_output
)
1481 if (n_warns
|| n_errs
)
1482 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1483 n_errs
, n_errs
== 1 ? "" : "s",
1484 n_warns
, n_warns
== 1 ? "" : "s");
1489 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1490 n_errs
, n_errs
== 1 ? "" : "s",
1491 n_warns
, n_warns
== 1 ? "" : "s");
1496 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1497 a routine to check for the definition of the procedure "_main",
1498 and if so -- fix it up so that it can be program entry point. */
1499 vms_check_for_main ();
1500 #endif /* OBJ_VMS */
1502 /* From now on, we don't care about sub-segments. Build one frag chain
1503 for each segment. Linked thru fr_next. */
1505 #ifdef BFD_ASSEMBLER
1506 /* Remove the sections created by gas for its own purposes. */
1508 asection
**seclist
, *sec
;
1511 seclist
= &stdoutput
->sections
;
1512 while (seclist
&& *seclist
)
1515 while (sec
== reg_section
|| sec
== expr_section
)
1519 stdoutput
->section_count
--;
1524 seclist
= &(*seclist
)->next
;
1527 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1530 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1532 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
1533 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
1534 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
1537 /* We have two segments. If user gave -R flag, then we must put the
1538 data frags into the text segment. Do this before relaxing so
1539 we know to take advantage of -R and make shorter addresses. */
1540 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1541 if (flag_readonly_data_in_text
)
1543 merge_data_into_text ();
1547 #ifdef BFD_ASSEMBLER
1552 #ifndef WORKING_DOT_WORD
1553 /* We need to reset the markers in the broken word list and
1554 associated frags between calls to relax_segment (via
1555 relax_seg). Since the broken word list is global, we do it
1556 once per round, rather than locally in relax_segment for each
1558 struct broken_word
*brokp
;
1560 for (brokp
= broken_words
;
1561 brokp
!= (struct broken_word
*) NULL
;
1562 brokp
= brokp
->next_broken_word
)
1566 if (brokp
->dispfrag
!= (fragS
*) NULL
1567 && brokp
->dispfrag
->fr_type
== rs_broken_word
)
1568 brokp
->dispfrag
->fr_subtype
= 0;
1573 bfd_map_over_sections (stdoutput
, relax_seg
, &changed
);
1577 bfd_map_over_sections (stdoutput
, size_seg
, (char *) 0);
1579 relax_and_size_all_segments ();
1580 #endif /* BFD_ASSEMBLER */
1582 /* Relaxation has completed. Freeze all syms. */
1585 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1586 /* Now that the segments have their final sizes, run through the
1587 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1588 should too. Currently, only DJGPP uses this code, but other
1589 COFF targets may need to execute this too. */
1590 bfd_map_over_sections (stdoutput
, set_segment_vma
, (char *) 0);
1593 #ifndef BFD_ASSEMBLER
1594 /* Crawl the symbol chain.
1596 For each symbol whose value depends on a frag, take the address of
1597 that frag and subsume it into the value of the symbol.
1598 After this, there is just one way to lookup a symbol value.
1599 Values are left in their final state for object file emission.
1600 We adjust the values of 'L' local symbols, even if we do
1601 not intend to emit them to the object file, because their values
1602 are needed for fix-ups.
1604 Unless we saw a -L flag, remove all symbols that begin with 'L'
1605 from the symbol chain. (They are still pointed to by the fixes.)
1607 Count the remaining symbols.
1608 Assign a symbol number to each symbol.
1609 Count the number of string-table chars we will emit.
1610 Put this info into the headers as appropriate. */
1611 know (zero_address_frag
.fr_address
== 0);
1612 string_byte_count
= sizeof (string_byte_count
);
1614 obj_crawl_symbol_chain (&headers
);
1616 if (string_byte_count
== sizeof (string_byte_count
))
1617 string_byte_count
= 0;
1619 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1621 /* Addresses of frags now reflect addresses we use in the object file.
1622 Symbol values are correct.
1623 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1624 Also converting any machine-dependent frags using md_convert_frag(); */
1625 subseg_change (SEG_TEXT
, 0);
1627 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1629 /* At this point we have linked all the frags into a single
1630 chain. However, cvt_frag_to_fill may call md_convert_frag
1631 which may call fix_new. We need to ensure that fix_new adds
1632 the fixup to the right section. */
1633 if (fragP
== data_frag_root
)
1634 subseg_change (SEG_DATA
, 0);
1636 cvt_frag_to_fill (&headers
, SEG_TEXT
, fragP
);
1638 /* Some assert macros don't work with # directives mixed in. */
1640 if (!(fragP
->fr_next
== NULL
1642 || fragP
->fr_next
== data_frag_root
1644 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
1645 == (fragP
->fr_fix
+ fragP
->fr_offset
* fragP
->fr_var
))))
1649 #endif /* ! BFD_ASSEMBLER */
1651 #ifndef WORKING_DOT_WORD
1653 struct broken_word
*lie
;
1654 struct broken_word
**prevP
;
1656 prevP
= &broken_words
;
1657 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1662 subseg_change (lie
->seg
, lie
->subseg
);
1663 exp
.X_op
= O_subtract
;
1664 exp
.X_add_symbol
= lie
->add
;
1665 exp
.X_op_symbol
= lie
->sub
;
1666 exp
.X_add_number
= lie
->addnum
;
1667 #ifdef BFD_ASSEMBLER
1668 #ifdef TC_CONS_FIX_NEW
1669 TC_CONS_FIX_NEW (lie
->frag
,
1670 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1673 fix_new_exp (lie
->frag
,
1674 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1675 2, &exp
, 0, BFD_RELOC_16
);
1678 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1679 fix_new_exp (lie
->frag
,
1680 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1681 2, &exp
, 0, NO_RELOC
);
1684 fix_new_ns32k_exp (lie
->frag
,
1685 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1686 2, &exp
, 0, 0, 2, 0, 0);
1688 fix_new_exp (lie
->frag
,
1689 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1691 #endif /* TC_NS32K */
1692 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1693 #endif /* BFD_ASSEMBLER */
1694 *prevP
= lie
->next_broken_word
;
1697 prevP
= &(lie
->next_broken_word
);
1699 for (lie
= broken_words
; lie
;)
1701 struct broken_word
*untruth
;
1703 addressT table_addr
;
1704 addressT from_addr
, to_addr
;
1707 subseg_change (lie
->seg
, lie
->subseg
);
1708 fragP
= lie
->dispfrag
;
1710 /* Find out how many broken_words go here. */
1713 untruth
&& untruth
->dispfrag
== fragP
;
1714 untruth
= untruth
->next_broken_word
)
1715 if (untruth
->added
== 1)
1718 table_ptr
= lie
->dispfrag
->fr_opcode
;
1719 table_addr
= (lie
->dispfrag
->fr_address
1720 + (table_ptr
- lie
->dispfrag
->fr_literal
));
1721 /* Create the jump around the long jumps. This is a short
1722 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1723 from_addr
= table_addr
;
1724 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1725 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1727 table_ptr
+= md_short_jump_size
;
1728 table_addr
+= md_short_jump_size
;
1731 lie
&& lie
->dispfrag
== fragP
;
1732 m
++, lie
= lie
->next_broken_word
)
1734 if (lie
->added
== 2)
1736 /* Patch the jump table. */
1737 /* This is the offset from ??? to table_ptr+0. */
1738 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1739 #ifdef BFD_ASSEMBLER
1740 to_addr
-= symbol_get_frag (lie
->sub
)->fr_address
;
1742 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1743 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr
, lie
);
1745 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1746 for (untruth
= lie
->next_broken_word
;
1747 untruth
&& untruth
->dispfrag
== fragP
;
1748 untruth
= untruth
->next_broken_word
)
1750 if (untruth
->use_jump
== lie
)
1751 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1754 /* Install the long jump. */
1755 /* This is a long jump from table_ptr+0 to the final target. */
1756 from_addr
= table_addr
;
1757 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1758 #ifdef BFD_ASSEMBLER
1759 to_addr
+= symbol_get_frag (lie
->add
)->fr_address
;
1761 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1763 table_ptr
+= md_long_jump_size
;
1764 table_addr
+= md_long_jump_size
;
1768 #endif /* not WORKING_DOT_WORD */
1770 #ifndef BFD_ASSEMBLER
1773 char *the_object_file
;
1774 long object_file_size
;
1775 /* Scan every FixS performing fixups. We had to wait until now to
1776 do this because md_convert_frag() may have made some fixSs. */
1779 subseg_change (SEG_TEXT
, 0);
1780 trsize
= md_reloc_size
* fixup_segment (text_fix_root
, SEG_TEXT
);
1781 subseg_change (SEG_DATA
, 0);
1782 drsize
= md_reloc_size
* fixup_segment (data_fix_root
, SEG_DATA
);
1783 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
1785 /* FIXME: Move this stuff into the pre-write-hook. */
1786 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
1787 H_SET_ENTRY_POINT (&headers
, 0);
1789 obj_pre_write_hook (&headers
); /* Extra coff stuff. */
1791 object_file_size
= H_GET_FILE_SIZE (&headers
);
1792 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
1794 output_file_create (out_file_name
);
1796 obj_header_append (&next_object_file_charP
, &headers
);
1798 know ((next_object_file_charP
- the_object_file
)
1799 == H_GET_HEADER_SIZE (&headers
));
1802 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1804 register long count
;
1805 register char *fill_literal
;
1806 register long fill_size
;
1809 know (fragP
->fr_type
== rs_fill
);
1810 append (&next_object_file_charP
, fragP
->fr_literal
,
1811 (unsigned long) fragP
->fr_fix
);
1812 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
1813 fill_size
= fragP
->fr_var
;
1814 know (fragP
->fr_offset
>= 0);
1816 for (count
= fragP
->fr_offset
; count
; count
--)
1817 append (&next_object_file_charP
, fill_literal
,
1818 (unsigned long) fill_size
);
1821 know ((next_object_file_charP
- the_object_file
)
1822 == (H_GET_HEADER_SIZE (&headers
)
1823 + H_GET_TEXT_SIZE (&headers
)
1824 + H_GET_DATA_SIZE (&headers
)));
1826 /* Emit relocations. */
1827 obj_emit_relocations (&next_object_file_charP
, text_fix_root
,
1828 (relax_addressT
) 0);
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
)));
1835 /* Make addresses in data relocation directives relative to beginning of
1836 first data fragment, not end of last text fragment: alignment of the
1837 start of the data segment may place a gap between the segments. */
1838 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1839 data0_frchainP
->frch_root
->fr_address
);
1841 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1842 text_last_frag
->fr_address
);
1843 #endif /* TC_I960 */
1845 know ((next_object_file_charP
- the_object_file
)
1846 == (H_GET_HEADER_SIZE (&headers
)
1847 + H_GET_TEXT_SIZE (&headers
)
1848 + H_GET_DATA_SIZE (&headers
)
1849 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1850 + H_GET_DATA_RELOCATION_SIZE (&headers
)));
1852 /* Emit line number entries. */
1853 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
1854 know ((next_object_file_charP
- the_object_file
)
1855 == (H_GET_HEADER_SIZE (&headers
)
1856 + H_GET_TEXT_SIZE (&headers
)
1857 + H_GET_DATA_SIZE (&headers
)
1858 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1859 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1860 + H_GET_LINENO_SIZE (&headers
)));
1863 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
1864 know ((next_object_file_charP
- the_object_file
)
1865 == (H_GET_HEADER_SIZE (&headers
)
1866 + H_GET_TEXT_SIZE (&headers
)
1867 + H_GET_DATA_SIZE (&headers
)
1868 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1869 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1870 + H_GET_LINENO_SIZE (&headers
)
1871 + H_GET_SYMBOL_TABLE_SIZE (&headers
)));
1874 if (string_byte_count
> 0)
1875 obj_emit_strings (&next_object_file_charP
);
1878 bfd_seek (stdoutput
, 0, 0);
1879 bfd_write (the_object_file
, 1, object_file_size
, stdoutput
);
1882 /* Write the data to the file. */
1883 output_file_append (the_object_file
, object_file_size
, out_file_name
);
1884 free (the_object_file
);
1888 /* Now do the VMS-dependent part of writing the object file. */
1889 vms_write_object_file (H_GET_TEXT_SIZE (&headers
),
1890 H_GET_DATA_SIZE (&headers
),
1891 H_GET_BSS_SIZE (&headers
),
1892 text_frag_root
, data_frag_root
);
1893 #endif /* OBJ_VMS */
1894 #else /* BFD_ASSEMBLER */
1896 /* Resolve symbol values. This needs to be done before processing
1902 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1903 resolve_symbol_value (symp
);
1905 resolve_local_symbol_values ();
1909 #ifdef tc_frob_file_before_adjust
1910 tc_frob_file_before_adjust ();
1912 #ifdef obj_frob_file_before_adjust
1913 obj_frob_file_before_adjust ();
1916 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
1918 /* Set up symbol table, and write it out. */
1923 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1928 if (symbol_mri_common_p (symp
))
1930 if (S_IS_EXTERNAL (symp
))
1931 as_bad (_("%s: global symbols not supported in common sections"),
1933 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1937 name
= S_GET_NAME (symp
);
1941 decode_local_label_name ((char *) S_GET_NAME (symp
));
1942 /* They only differ if `name' is a fb or dollar local
1944 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
1945 as_bad (_("local label %s is not defined"), name2
);
1948 /* Do it again, because adjust_reloc_syms might introduce
1949 more symbols. They'll probably only be section symbols,
1950 but they'll still need to have the values computed. */
1951 resolve_symbol_value (symp
);
1953 /* Skip symbols which were equated to undefined or common
1955 if (symbol_equated_p (symp
)
1956 && (! S_IS_DEFINED (symp
) || S_IS_COMMON (symp
)))
1958 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1962 /* So far, common symbols have been treated like undefined symbols.
1963 Put them in the common section now. */
1964 if (S_IS_DEFINED (symp
) == 0
1965 && S_GET_VALUE (symp
) != 0)
1966 S_SET_SEGMENT (symp
, bfd_com_section_ptr
);
1968 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1969 S_GET_NAME (symp
), symp
,
1971 symbol_get_bfdsym (symp
)->flags
,
1972 segment_name (S_GET_SEGMENT (symp
)));
1975 #ifdef obj_frob_symbol
1976 obj_frob_symbol (symp
, punt
);
1978 #ifdef tc_frob_symbol
1979 if (! punt
|| symbol_used_in_reloc_p (symp
))
1980 tc_frob_symbol (symp
, punt
);
1983 /* If we don't want to keep this symbol, splice it out of
1984 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1985 want section symbols. Otherwise, we skip local symbols
1986 and symbols that the frob_symbol macros told us to punt,
1987 but we keep such symbols if they are used in relocs. */
1988 if ((! EMIT_SECTION_SYMBOLS
1989 && symbol_section_p (symp
))
1990 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1991 opposites. Sometimes the former checks flags and the
1992 latter examines the name... */
1993 || (!S_IS_EXTERN (symp
)
1994 && (S_IS_LOCAL (symp
) || punt
)
1995 && ! symbol_used_in_reloc_p (symp
)))
1997 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1999 /* After symbol_remove, symbol_next(symp) still returns
2000 the one that came after it in the chain. So we don't
2001 need to do any extra cleanup work here. */
2005 /* Make sure we really got a value for the symbol. */
2006 if (! symbol_resolved_p (symp
))
2008 as_bad (_("can't resolve value for symbol \"%s\""),
2010 symbol_mark_resolved (symp
);
2013 /* Set the value into the BFD symbol. Up til now the value
2014 has only been kept in the gas symbolS struct. */
2015 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
2021 /* Now do any format-specific adjustments to the symbol table, such
2022 as adding file symbols. */
2023 #ifdef tc_adjust_symtab
2024 tc_adjust_symtab ();
2026 #ifdef obj_adjust_symtab
2027 obj_adjust_symtab ();
2030 /* Now that all the sizes are known, and contents correct, we can
2031 start writing to the file. */
2034 /* If *_frob_file changes the symbol value at this point, it is
2035 responsible for moving the changed value into symp->bsym->value
2036 as well. Hopefully all symbol value changing can be done in
2041 #ifdef obj_frob_file
2045 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
2047 #ifdef tc_frob_file_after_relocs
2048 tc_frob_file_after_relocs ();
2050 #ifdef obj_frob_file_after_relocs
2051 obj_frob_file_after_relocs ();
2054 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
2055 #endif /* BFD_ASSEMBLER */
2059 #ifdef TC_GENERIC_RELAX_TABLE
2061 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2064 relax_frag (segment
, fragP
, stretch
)
2069 const relax_typeS
*this_type
;
2070 const relax_typeS
*start_type
;
2071 relax_substateT next_state
;
2072 relax_substateT this_state
;
2078 const relax_typeS
*table
;
2080 target
= fragP
->fr_offset
;
2081 address
= fragP
->fr_address
;
2082 table
= TC_GENERIC_RELAX_TABLE
;
2083 this_state
= fragP
->fr_subtype
;
2084 start_type
= this_type
= table
+ this_state
;
2085 symbolP
= fragP
->fr_symbol
;
2091 sym_frag
= symbol_get_frag (symbolP
);
2093 #ifndef DIFF_EXPR_OK
2094 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2095 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2096 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2097 || (S_GET_SEGMENT (symbolP
) == SEG_BSS
)
2098 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
2100 know (sym_frag
!= NULL
);
2102 know (!(S_GET_SEGMENT (symbolP
) == absolute_section
)
2103 || sym_frag
== &zero_address_frag
);
2104 target
+= S_GET_VALUE (symbolP
) + sym_frag
->fr_address
;
2106 /* If frag has yet to be reached on this pass,
2107 assume it will move by STRETCH just as we did.
2108 If this is not so, it will be because some frag
2109 between grows, and that will force another pass. */
2112 && sym_frag
->relax_marker
!= fragP
->relax_marker
2113 && S_GET_SEGMENT (symbolP
) == segment
)
2119 aim
= target
- address
- fragP
->fr_fix
;
2120 #ifdef TC_PCREL_ADJUST
2121 /* Currently only the ns32k family needs this. */
2122 aim
+= TC_PCREL_ADJUST (fragP
);
2124 /* This machine doesn't want to use pcrel_adjust.
2125 In that case, pcrel_adjust should be zero. */
2127 assert (fragP
->fr_targ
.ns32k
.pcrel_adjust
== 0);
2130 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2131 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2136 /* Look backwards. */
2137 for (next_state
= this_type
->rlx_more
; next_state
;)
2138 if (aim
>= this_type
->rlx_backward
)
2142 /* Grow to next state. */
2143 this_state
= next_state
;
2144 this_type
= table
+ this_state
;
2145 next_state
= this_type
->rlx_more
;
2150 /* Look forwards. */
2151 for (next_state
= this_type
->rlx_more
; next_state
;)
2152 if (aim
<= this_type
->rlx_forward
)
2156 /* Grow to next state. */
2157 this_state
= next_state
;
2158 this_type
= table
+ this_state
;
2159 next_state
= this_type
->rlx_more
;
2163 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2165 fragP
->fr_subtype
= this_state
;
2169 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2171 /* Relax_align. Advance location counter to next address that has 'alignment'
2172 lowest order bits all 0s, return size of adjustment made. */
2173 static relax_addressT
2174 relax_align (address
, alignment
)
2175 register relax_addressT address
; /* Address now. */
2176 register int alignment
; /* Alignment (binary). */
2178 relax_addressT mask
;
2179 relax_addressT new_address
;
2181 mask
= ~((~0) << alignment
);
2182 new_address
= (address
+ mask
) & (~mask
);
2183 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2185 /* We must provide lots of padding, so the linker can discard it
2186 when needed. The linker will not add extra space, ever. */
2187 new_address
+= (1 << alignment
);
2189 return (new_address
- address
);
2192 /* Now we have a segment, not a crowd of sub-segments, we can make
2197 After this, all frags in this segment have addresses that are correct
2198 within the segment. Since segments live in different file addresses,
2199 these frag addresses may not be the same as final object-file
2203 relax_segment (segment_frag_root
, segment
)
2204 struct frag
*segment_frag_root
;
2207 register struct frag
*fragP
;
2208 register relax_addressT address
;
2211 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2212 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
2214 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2215 subseg_change (segment
, 0);
2217 /* For each frag in segment: count and store (a 1st guess of)
2220 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2222 fragP
->relax_marker
= 0;
2223 fragP
->fr_address
= address
;
2224 address
+= fragP
->fr_fix
;
2226 switch (fragP
->fr_type
)
2229 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2236 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2238 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2241 if (offset
% fragP
->fr_var
!= 0)
2243 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2244 (unsigned long) offset
, (long) fragP
->fr_var
);
2245 offset
-= (offset
% fragP
->fr_var
);
2254 /* Assume .org is nugatory. It will grow with 1st relax. */
2257 case rs_machine_dependent
:
2258 address
+= md_estimate_size_before_relax (fragP
, segment
);
2261 #ifndef WORKING_DOT_WORD
2262 /* Broken words don't concern us yet. */
2263 case rs_broken_word
:
2268 /* Initial guess is always 1; doing otherwise can result in
2269 stable solutions that are larger than the minimum. */
2270 address
+= fragP
->fr_offset
= 1;
2274 address
+= eh_frame_estimate_size_before_relax (fragP
);
2278 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
2282 BAD_CASE (fragP
->fr_type
);
2289 long stretch
; /* May be any size, 0 or negative. */
2290 /* Cumulative number of addresses we have relaxed this pass.
2291 We may have relaxed more than one address. */
2292 int stretched
; /* Have we stretched on this pass? */
2293 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2294 grew, and another shrank. If a branch instruction doesn't fit anymore,
2295 we could be scrod. */
2302 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2305 addressT was_address
;
2309 fragP
->relax_marker
^= 1;
2310 was_address
= fragP
->fr_address
;
2311 address
= fragP
->fr_address
+= stretch
;
2312 symbolP
= fragP
->fr_symbol
;
2313 offset
= fragP
->fr_offset
;
2315 switch (fragP
->fr_type
)
2317 case rs_fill
: /* .fill never relaxes. */
2321 #ifndef WORKING_DOT_WORD
2322 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2323 for it I do not want to write it. I do not want to have
2324 anything to do with it. This is not the proper way to
2325 implement this misfeature. */
2326 case rs_broken_word
:
2328 struct broken_word
*lie
;
2329 struct broken_word
*untruth
;
2331 /* Yes this is ugly (storing the broken_word pointer
2332 in the symbol slot). Still, this whole chunk of
2333 code is ugly, and I don't feel like doing anything
2334 about it. Think of it as stubbornness in action. */
2336 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2337 lie
&& lie
->dispfrag
== fragP
;
2338 lie
= lie
->next_broken_word
)
2344 offset
= (symbol_get_frag (lie
->add
)->fr_address
2345 + S_GET_VALUE (lie
->add
)
2347 - (symbol_get_frag (lie
->sub
)->fr_address
2348 + S_GET_VALUE (lie
->sub
)));
2349 if (offset
<= -32768 || offset
>= 32767)
2351 if (flag_warn_displacement
)
2354 sprint_value (buf
, (addressT
) lie
->addnum
);
2355 as_warn (_(".word %s-%s+%s didn't fit"),
2356 S_GET_NAME (lie
->add
),
2357 S_GET_NAME (lie
->sub
),
2361 if (fragP
->fr_subtype
== 0)
2363 fragP
->fr_subtype
++;
2364 growth
+= md_short_jump_size
;
2366 for (untruth
= lie
->next_broken_word
;
2367 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2368 untruth
= untruth
->next_broken_word
)
2369 if ((symbol_get_frag (untruth
->add
)
2370 == symbol_get_frag (lie
->add
))
2371 && (S_GET_VALUE (untruth
->add
)
2372 == S_GET_VALUE (lie
->add
)))
2375 untruth
->use_jump
= lie
;
2377 growth
+= md_long_jump_size
;
2382 } /* case rs_broken_word */
2388 addressT oldoff
, newoff
;
2390 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2392 newoff
= relax_align (address
+ fragP
->fr_fix
,
2395 if (fragP
->fr_subtype
!= 0)
2397 if (oldoff
> fragP
->fr_subtype
)
2399 if (newoff
> fragP
->fr_subtype
)
2403 growth
= newoff
- oldoff
;
2409 addressT target
= offset
;
2414 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2415 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2416 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2417 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
)
2418 || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
2419 know (symbolP
->sy_frag
);
2420 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2421 || (symbolP
->sy_frag
== &zero_address_frag
));
2423 target
+= (S_GET_VALUE (symbolP
)
2424 + symbol_get_frag (symbolP
)->fr_address
);
2425 } /* if we have a symbol */
2427 know (fragP
->fr_next
);
2428 after
= fragP
->fr_next
->fr_address
;
2429 growth
= target
- after
;
2432 /* Growth may be negative, but variable part of frag
2433 cannot have fewer than 0 chars. That is, we can't
2435 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2436 _("attempt to .org backwards ignored"));
2438 /* We've issued an error message. Change the
2439 frag to avoid cascading errors. */
2440 fragP
->fr_type
= rs_align
;
2441 fragP
->fr_subtype
= 0;
2442 fragP
->fr_offset
= 0;
2443 fragP
->fr_fix
= after
- address
;
2447 /* This is an absolute growth factor */
2455 growth
= S_GET_VALUE (symbolP
);
2456 if (symbol_get_frag (symbolP
) != &zero_address_frag
2457 || S_IS_COMMON (symbolP
)
2458 || ! S_IS_DEFINED (symbolP
))
2459 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2460 _(".space specifies non-absolute value"));
2461 fragP
->fr_symbol
= 0;
2464 as_warn (_(".space or .fill with negative value, ignored"));
2472 case rs_machine_dependent
:
2473 #ifdef md_relax_frag
2474 growth
= md_relax_frag (segment
, fragP
, stretch
);
2476 #ifdef TC_GENERIC_RELAX_TABLE
2477 /* The default way to relax a frag is to look through
2478 TC_GENERIC_RELAX_TABLE. */
2479 growth
= relax_frag (segment
, fragP
, stretch
);
2480 #endif /* TC_GENERIC_RELAX_TABLE */
2489 value
= resolve_symbol_value (fragP
->fr_symbol
);
2490 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2491 growth
= size
- fragP
->fr_offset
;
2492 fragP
->fr_offset
= size
;
2497 growth
= eh_frame_relax_frag (fragP
);
2501 growth
= dwarf2dbg_relax_frag (fragP
);
2505 BAD_CASE (fragP
->fr_type
);
2513 } /* For each frag in the segment. */
2515 while (stretched
); /* Until nothing further to relax. */
2519 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2520 if (fragP
->last_fr_address
!= fragP
->fr_address
)
2522 fragP
->last_fr_address
= fragP
->fr_address
;
2528 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2530 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2531 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2536 Go through all the fixS's in a segment and see which ones can be
2537 handled now. (These consist of fixS where we have since discovered
2538 the value of a symbol, or the address of the frag involved.)
2539 For each one, call md_apply_fix to put the fix into the frag data.
2541 Result is a count of how many relocation structs will be needed to
2542 handle the remaining fixS's that we couldn't completely handle here.
2543 These will be output later by emit_relocations(). */
2546 fixup_segment (fixP
, this_segment_type
)
2547 register fixS
*fixP
;
2548 segT this_segment_type
; /* N_TYPE bits for segment. */
2550 long seg_reloc_count
= 0;
2551 symbolS
*add_symbolP
;
2552 symbolS
*sub_symbolP
;
2559 segT add_symbol_segment
= absolute_section
;
2561 /* If the linker is doing the relaxing, we must not do any fixups.
2563 Well, strictly speaking that's not true -- we could do any that are
2564 PC-relative and don't cross regions that could change size. And for the
2565 i960 (the only machine for which we've got a relaxing linker right now),
2566 we might be able to turn callx/callj into bal anyways in cases where we
2567 know the maximum displacement. */
2568 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment_type
))
2570 for (; fixP
; fixP
= fixP
->fx_next
)
2572 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2573 return seg_reloc_count
;
2576 for (; fixP
; fixP
= fixP
->fx_next
)
2579 fprintf (stderr
, "\nprocessing fixup:\n");
2583 fragP
= fixP
->fx_frag
;
2585 where
= fixP
->fx_where
;
2586 place
= fragP
->fr_literal
+ where
;
2587 size
= fixP
->fx_size
;
2588 add_symbolP
= fixP
->fx_addsy
;
2589 #ifdef TC_VALIDATE_FIX
2590 TC_VALIDATE_FIX (fixP
, this_segment_type
, skip
);
2592 sub_symbolP
= fixP
->fx_subsy
;
2593 add_number
= fixP
->fx_offset
;
2594 pcrel
= fixP
->fx_pcrel
;
2597 if (add_symbolP
!= NULL
2598 && symbol_mri_common_p (add_symbolP
))
2600 know (add_symbolP
->sy_value
.X_op
== O_symbol
);
2601 add_number
+= S_GET_VALUE (add_symbolP
);
2602 fixP
->fx_offset
= add_number
;
2603 add_symbolP
= fixP
->fx_addsy
=
2604 symbol_get_value_expression (add_symbolP
)->X_add_symbol
;
2608 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2612 resolve_symbol_value (sub_symbolP
);
2613 if (add_symbolP
== NULL
|| add_symbol_segment
== absolute_section
)
2615 if (add_symbolP
!= NULL
)
2617 add_number
+= S_GET_VALUE (add_symbolP
);
2619 fixP
->fx_addsy
= NULL
;
2622 /* It's just -sym. */
2623 if (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)
2625 add_number
-= S_GET_VALUE (sub_symbolP
);
2626 fixP
->fx_subsy
= NULL
;
2629 && S_GET_SEGMENT (sub_symbolP
) == this_segment_type
)
2631 /* Should try converting to a constant. */
2636 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2637 _("Negative of non-absolute symbol %s"),
2638 S_GET_NAME (sub_symbolP
));
2640 else if (S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
2641 && SEG_NORMAL (add_symbol_segment
))
2643 /* Difference of 2 symbols from same segment.
2644 Can't make difference of 2 undefineds: 'value' means
2645 something different for N_UNDF. */
2647 /* Makes no sense to use the difference of 2 arbitrary symbols
2648 as the target of a call instruction. */
2650 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2651 _("callj to difference of 2 symbols"));
2652 #endif /* TC_I960 */
2653 add_number
+= S_GET_VALUE (add_symbolP
) -
2654 S_GET_VALUE (sub_symbolP
);
2657 pcrel
= 0; /* No further pcrel processing. */
2659 /* Let the target machine make the final determination
2660 as to whether or not a relocation will be needed to
2661 handle this fixup. */
2662 if (!TC_FORCE_RELOCATION_SECTION (fixP
, this_segment_type
))
2665 fixP
->fx_addsy
= NULL
;
2666 fixP
->fx_subsy
= NULL
;
2671 /* Different segments in subtraction. */
2672 know (!(S_IS_EXTERNAL (sub_symbolP
)
2673 && (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)));
2675 if ((S_GET_SEGMENT (sub_symbolP
) == absolute_section
))
2676 add_number
-= S_GET_VALUE (sub_symbolP
);
2679 else if (S_GET_SEGMENT (sub_symbolP
) == this_segment_type
2681 /* Do this even if it's already described as
2682 pc-relative. For example, on the m68k, an
2683 operand of "pc@(foo-.-2)" should address
2684 "foo" in a pc-relative mode. */
2689 /* Make it pc-relative. */
2690 add_number
+= (MD_PCREL_FROM_SECTION (fixP
, this_segment_type
)
2691 - S_GET_VALUE (sub_symbolP
));
2698 #ifdef UNDEFINED_DIFFERENCE_OK
2699 /* The PA needs this for PIC code generation. We basically
2700 don't want to do anything if we have the difference of two
2701 symbols at this point. */
2704 /* Leave it alone. */
2707 #ifdef BFD_ASSEMBLER
2708 else if (fixP
->fx_r_type
== BFD_RELOC_GPREL32
2709 || fixP
->fx_r_type
== BFD_RELOC_GPREL16
)
2711 /* Leave it alone. */
2717 sprint_value (buf
, fragP
->fr_address
+ where
);
2718 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2719 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2720 S_GET_NAME (add_symbolP
),
2721 segment_name (S_GET_SEGMENT (add_symbolP
)),
2722 S_GET_NAME (sub_symbolP
),
2723 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2731 if (add_symbol_segment
== this_segment_type
&& pcrel
&& !plt
2732 && TC_RELOC_RTSYM_LOC_FIXUP (fixP
))
2734 /* This fixup was made when the symbol's segment was
2735 SEG_UNKNOWN, but it is now in the local segment.
2736 So we know how to do the address without relocation. */
2738 /* reloc_callj() may replace a 'call' with a 'calls' or a
2739 'bal', in which cases it modifies *fixP as appropriate.
2740 In the case of a 'calls', no further work is required,
2741 and *fixP has been set up to make the rest of the code
2744 #endif /* TC_I960 */
2746 add_number
+= S_GET_VALUE (add_symbolP
);
2747 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2748 /* Lie. Don't want further pcrel processing. */
2751 /* Let the target machine make the final determination
2752 as to whether or not a relocation will be needed to
2753 handle this fixup. */
2754 if (!TC_FORCE_RELOCATION (fixP
))
2757 fixP
->fx_addsy
= NULL
;
2762 if (add_symbol_segment
== absolute_section
2766 /* See comment about reloc_callj() above. */
2768 #endif /* TC_I960 */
2769 add_number
+= S_GET_VALUE (add_symbolP
);
2771 /* Let the target machine make the final determination
2772 as to whether or not a relocation will be needed to
2773 handle this fixup. */
2775 if (!TC_FORCE_RELOCATION (fixP
))
2777 fixP
->fx_addsy
= NULL
;
2781 else if (add_symbol_segment
== undefined_section
2782 #ifdef BFD_ASSEMBLER
2783 || bfd_is_com_section (add_symbol_segment
)
2788 if ((int) fixP
->fx_bit_fixP
== 13)
2790 /* This is a COBR instruction. They have only a
2791 13-bit displacement and are only to be used
2792 for local branches: flag as error, don't generate
2794 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2795 _("can't use COBR format with external label"));
2796 fixP
->fx_addsy
= NULL
;
2800 #endif /* TC_I960 */
2804 if (S_IS_COMMON (add_symbolP
))
2805 add_number
+= S_GET_VALUE (add_symbolP
);
2806 #endif /* TE_I386AIX */
2807 #endif /* OBJ_COFF */
2813 if (TC_FIX_ADJUSTABLE (fixP
))
2814 add_number
+= S_GET_VALUE (add_symbolP
);
2821 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2822 if (add_symbolP
== 0)
2824 #ifndef BFD_ASSEMBLER
2825 fixP
->fx_addsy
= &abs_symbol
;
2827 fixP
->fx_addsy
= section_symbol (absolute_section
);
2829 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2836 #ifdef MD_APPLY_FIX3
2837 md_apply_fix3 (fixP
, &add_number
, this_segment_type
);
2839 #ifdef BFD_ASSEMBLER
2840 md_apply_fix (fixP
, &add_number
);
2842 md_apply_fix (fixP
, add_number
);
2846 #ifndef TC_HANDLES_FX_DONE
2847 /* If the tc-* files haven't been converted, assume it's handling
2848 it the old way, where a null fx_addsy means that the fix has
2849 been applied completely, and no further work is needed. */
2850 if (fixP
->fx_addsy
== 0 && fixP
->fx_pcrel
== 0)
2855 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& size
> 0)
2857 if ((size_t) size
< sizeof (valueT
))
2862 mask
--; /* Set all bits to one. */
2863 mask
<<= size
* 8 - (fixP
->fx_signed
? 1 : 0);
2864 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
2866 char buf
[50], buf2
[50];
2867 sprint_value (buf
, fragP
->fr_address
+ where
);
2868 if (add_number
> 1000)
2869 sprint_value (buf2
, add_number
);
2871 sprintf (buf2
, "%ld", (long) add_number
);
2872 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2873 _("Value of %s too large for field of %d bytes at %s"),
2875 } /* Generic error checking. */
2877 #ifdef WARN_SIGNED_OVERFLOW_WORD
2878 /* Warn if a .word value is too large when treated as a signed
2879 number. We already know it is not too negative. This is to
2880 catch over-large switches generated by gcc on the 68k. */
2881 if (!flag_signed_overflow_ok
2883 && add_number
> 0x7fff)
2884 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2885 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2887 (unsigned long) (fragP
->fr_address
+ where
));
2889 } /* Not a bit fix. */
2891 #ifdef TC_VALIDATE_FIX
2892 skip
: ATTRIBUTE_UNUSED_LABEL
2896 fprintf (stderr
, "result:\n");
2899 } /* For each fixS in this segment. */
2901 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2902 return seg_reloc_count
;
2905 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2908 number_to_chars_bigendian (buf
, val
, n
)
2913 if ((size_t) n
> sizeof (val
) || n
<= 0)
2917 buf
[n
] = val
& 0xff;
2923 number_to_chars_littleendian (buf
, val
, n
)
2928 if ((size_t) n
> sizeof (val
) || n
<= 0)
2932 *buf
++ = val
& 0xff;
2938 write_print_statistics (file
)
2941 fprintf (file
, "fixups: %d\n", n_fixups
);
2944 /* For debugging. */
2945 extern int indent_level
;
2952 fprintf (stderr
, "fix %lx %s:%d", (long) fixp
, fixp
->fx_file
, fixp
->fx_line
);
2954 fprintf (stderr
, " pcrel");
2955 if (fixp
->fx_pcrel_adjust
)
2956 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2957 if (fixp
->fx_im_disp
)
2960 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2962 fprintf (stderr
, " im_disp");
2966 fprintf (stderr
, " tcbit");
2968 fprintf (stderr
, " done");
2969 fprintf (stderr
, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2970 fixp
->fx_size
, (long) fixp
->fx_frag
, (long) fixp
->fx_where
,
2971 (long) fixp
->fx_offset
, (long) fixp
->fx_addnumber
);
2972 #ifdef BFD_ASSEMBLER
2973 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2976 #ifdef NEED_FX_R_TYPE
2977 fprintf (stderr
, " r_type=%d", fixp
->fx_r_type
);
2982 fprintf (stderr
, "\n +<");
2983 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2984 fprintf (stderr
, ">");
2988 fprintf (stderr
, "\n -<");
2989 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2990 fprintf (stderr
, ">");
2992 fprintf (stderr
, "\n");
2993 #ifdef TC_FIX_DATA_PRINT
2994 TC_FIX_DATA_PRINT (stderr
, fixp
);