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 int symbol_table_frozen
;
65 void print_fixup
PARAMS ((fixS
*));
68 static void renumber_sections
PARAMS ((bfd
*, asection
*, PTR
));
70 /* We generally attach relocs to frag chains. However, after we have
71 chained these all together into a segment, any relocs we add after
72 that must be attached to a segment. This will include relocs added
73 in md_estimate_size_for_relax, for example. */
74 static int frags_chained
= 0;
80 struct frag
*text_frag_root
;
81 struct frag
*data_frag_root
;
82 struct frag
*bss_frag_root
;
84 struct frag
*text_last_frag
; /* Last frag in segment. */
85 struct frag
*data_last_frag
; /* Last frag in segment. */
86 static struct frag
*bss_last_frag
; /* Last frag in segment. */
90 static object_headers headers
;
93 long string_byte_count
;
94 char *next_object_file_charP
; /* Tracks object file bytes. */
97 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
100 #endif /* BFD_ASSEMBLER */
105 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
106 symbolS
*add
, symbolS
*sub
,
107 offsetT offset
, int pcrel
,
108 bfd_reloc_code_real_type r_type
));
110 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
111 symbolS
*add
, symbolS
*sub
,
112 offsetT offset
, int pcrel
,
115 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
116 static long fixup_segment
PARAMS ((fixS
* fixP
, segT this_segment_type
));
118 static relax_addressT relax_align
PARAMS ((relax_addressT addr
, int align
));
119 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
120 static fragS
*chain_frchains_together_1
PARAMS ((segT
, struct frchain
*));
123 static void chain_frchains_together
PARAMS ((bfd
*, segT
, PTR
));
124 static void cvt_frag_to_fill
PARAMS ((segT
, fragS
*));
125 static void relax_and_size_seg
PARAMS ((bfd
*, asection
*, PTR
));
126 static void adjust_reloc_syms
PARAMS ((bfd
*, asection
*, PTR
));
127 static void write_relocs
PARAMS ((bfd
*, asection
*, PTR
));
128 static void write_contents
PARAMS ((bfd
*, asection
*, PTR
));
129 static void set_symtab
PARAMS ((void));
131 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
132 static void merge_data_into_text
PARAMS ((void));
134 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
135 static void cvt_frag_to_fill
PARAMS ((object_headers
*, segT
, fragS
*));
136 static void remove_subsegs
PARAMS ((frchainS
*, int, fragS
**, fragS
**));
137 static void relax_and_size_all_segments
PARAMS ((void));
139 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
140 static void set_segment_vma
PARAMS ((bfd
*, asection
*, PTR
));
143 /* Create a fixS in obstack 'notes'. */
146 fix_new_internal (frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
,
148 fragS
*frag
; /* Which frag? */
149 int where
; /* Where in that frag? */
150 int size
; /* 1, 2, or 4 usually. */
151 symbolS
*add_symbol
; /* X_add_symbol. */
152 symbolS
*sub_symbol
; /* X_op_symbol. */
153 offsetT offset
; /* X_add_number. */
154 int pcrel
; /* TRUE if PC-relative relocation. */
156 bfd_reloc_code_real_type r_type
; /* Relocation type. */
158 int r_type
; /* Relocation type. */
165 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
167 fixP
->fx_frag
= frag
;
168 fixP
->fx_where
= where
;
169 fixP
->fx_size
= size
;
170 /* We've made fx_size a narrow field; check that it's wide enough. */
171 if (fixP
->fx_size
!= size
)
173 as_bad (_("field fx_size too small to hold %d"), size
);
176 fixP
->fx_addsy
= add_symbol
;
177 fixP
->fx_subsy
= sub_symbol
;
178 fixP
->fx_offset
= offset
;
179 fixP
->fx_pcrel
= pcrel
;
181 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
182 fixP
->fx_r_type
= r_type
;
184 fixP
->fx_im_disp
= 0;
185 fixP
->fx_pcrel_adjust
= 0;
186 fixP
->fx_bit_fixP
= 0;
187 fixP
->fx_addnumber
= 0;
190 fixP
->fx_no_overflow
= 0;
194 fixP
->fx_cgen
.insn
= NULL
;
195 fixP
->fx_cgen
.opinfo
= 0;
199 TC_INIT_FIX_DATA (fixP
);
202 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
204 /* Usually, we want relocs sorted numerically, but while
205 comparing to older versions of gas that have relocs
206 reverse sorted, it is convenient to have this compile
207 time option. xoxorich. */
211 fixS
**seg_fix_rootP
= (frags_chained
212 ? &seg_info (now_seg
)->fix_root
213 : &frchain_now
->fix_root
);
214 fixS
**seg_fix_tailP
= (frags_chained
215 ? &seg_info (now_seg
)->fix_tail
216 : &frchain_now
->fix_tail
);
219 #ifdef REVERSE_SORT_RELOCS
221 fixP
->fx_next
= *seg_fix_rootP
;
222 *seg_fix_rootP
= fixP
;
224 #else /* REVERSE_SORT_RELOCS */
226 fixP
->fx_next
= NULL
;
229 (*seg_fix_tailP
)->fx_next
= fixP
;
231 *seg_fix_rootP
= fixP
;
232 *seg_fix_tailP
= fixP
;
234 #endif /* REVERSE_SORT_RELOCS */
240 /* Create a fixup relative to a symbol (plus a constant). */
243 fix_new (frag
, where
, size
, add_symbol
, offset
, pcrel
, r_type
)
244 fragS
*frag
; /* Which frag? */
245 int where
; /* Where in that frag? */
246 int size
; /* 1, 2, or 4 usually. */
247 symbolS
*add_symbol
; /* X_add_symbol. */
248 offsetT offset
; /* X_add_number. */
249 int pcrel
; /* TRUE if PC-relative relocation. */
251 bfd_reloc_code_real_type r_type
; /* Relocation type. */
253 int r_type
; /* Relocation type. */
256 return fix_new_internal (frag
, where
, size
, add_symbol
,
257 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
260 /* Create a fixup for an expression. Currently we only support fixups
261 for difference expressions. That is itself more than most object
262 file formats support anyhow. */
265 fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
)
266 fragS
*frag
; /* Which frag? */
267 int where
; /* Where in that frag? */
268 int size
; /* 1, 2, or 4 usually. */
269 expressionS
*exp
; /* Expression. */
270 int pcrel
; /* TRUE if PC-relative relocation. */
272 bfd_reloc_code_real_type r_type
; /* Relocation type. */
274 int r_type
; /* Relocation type. */
287 as_bad (_("register value used as expression"));
291 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
292 the difference expression cannot immediately be reduced. */
294 symbolS
*stmp
= make_expr_symbol (exp
);
296 exp
->X_op
= O_symbol
;
297 exp
->X_op_symbol
= 0;
298 exp
->X_add_symbol
= stmp
;
299 exp
->X_add_number
= 0;
301 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
305 add
= exp
->X_add_symbol
;
306 off
= exp
->X_add_number
;
308 #if defined(BFD_ASSEMBLER)
309 r_type
= BFD_RELOC_RVA
;
311 #if defined(TC_RVA_RELOC)
312 r_type
= TC_RVA_RELOC
;
314 as_fatal (_("rva not supported"));
320 sub
= exp
->X_add_symbol
;
321 off
= exp
->X_add_number
;
325 sub
= exp
->X_op_symbol
;
328 add
= exp
->X_add_symbol
;
331 off
= exp
->X_add_number
;
335 add
= make_expr_symbol (exp
);
339 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
, r_type
);
342 /* Append a string onto another string, bumping the pointer along. */
344 append (charPP
, fromP
, length
)
347 unsigned long length
;
349 /* Don't trust memcpy() of 0 chars. */
353 memcpy (*charPP
, fromP
, length
);
357 #ifndef BFD_ASSEMBLER
358 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
361 /* This routine records the largest alignment seen for each segment.
362 If the beginning of the segment is aligned on the worst-case
363 boundary, all of the other alignments within it will work. At
364 least one object format really uses this info. */
367 record_alignment (seg
, align
)
368 /* Segment to which alignment pertains. */
370 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
374 if (seg
== absolute_section
)
377 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
378 bfd_set_section_alignment (stdoutput
, seg
, align
);
380 if (align
> section_alignment
[(int) seg
])
381 section_alignment
[(int) seg
] = align
;
386 get_recorded_alignment (seg
)
389 if (seg
== absolute_section
)
392 return bfd_get_section_alignment (stdoutput
, seg
);
394 return section_alignment
[(int) seg
];
400 /* Reset the section indices after removing the gas created sections. */
403 renumber_sections (abfd
, sec
, countparg
)
404 bfd
*abfd ATTRIBUTE_UNUSED
;
408 int *countp
= (int *) countparg
;
410 sec
->index
= *countp
;
414 #endif /* defined (BFD_ASSEMBLER) */
416 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
419 chain_frchains_together_1 (section
, frchp
)
421 struct frchain
*frchp
;
423 fragS dummy
, *prev_frag
= &dummy
;
425 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
428 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
430 prev_frag
->fr_next
= frchp
->frch_root
;
431 prev_frag
= frchp
->frch_last
;
432 assert (prev_frag
->fr_type
!= 0);
434 if (frchp
->fix_root
!= (fixS
*) NULL
)
436 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
437 seg_info (section
)->fix_root
= frchp
->fix_root
;
438 prev_fix
->fx_next
= frchp
->fix_root
;
439 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
440 prev_fix
= frchp
->fix_tail
;
444 assert (prev_frag
->fr_type
!= 0);
445 prev_frag
->fr_next
= 0;
454 chain_frchains_together (abfd
, section
, xxx
)
455 bfd
*abfd ATTRIBUTE_UNUSED
;
457 PTR xxx ATTRIBUTE_UNUSED
;
459 segment_info_type
*info
;
461 /* BFD may have introduced its own sections without using
462 subseg_new, so it is possible that seg_info is NULL. */
463 info
= seg_info (section
);
464 if (info
!= (segment_info_type
*) NULL
)
465 info
->frchainP
->frch_last
466 = chain_frchains_together_1 (section
, info
->frchainP
);
468 /* Now that we've chained the frags together, we must add new fixups
469 to the segment, not to the frag chain. */
475 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
478 remove_subsegs (head
, seg
, root
, last
)
484 *root
= head
->frch_root
;
485 *last
= chain_frchains_together_1 (seg
, head
);
490 #if defined (BFD_ASSEMBLER) || !defined (BFD)
494 cvt_frag_to_fill (sec
, fragP
)
495 segT sec ATTRIBUTE_UNUSED
;
499 cvt_frag_to_fill (headersP
, sec
, fragP
)
500 object_headers
*headersP
;
505 switch (fragP
->fr_type
)
513 HANDLE_ALIGN (fragP
);
515 know (fragP
->fr_next
!= NULL
);
516 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
518 - fragP
->fr_fix
) / fragP
->fr_var
;
519 if (fragP
->fr_offset
< 0)
521 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
522 _("attempt to .org/.space backwards? (%ld)"),
523 (long) fragP
->fr_offset
);
525 fragP
->fr_type
= rs_fill
;
533 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
536 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
539 fragP
->fr_fix
+= size
;
540 fragP
->fr_type
= rs_fill
;
542 fragP
->fr_offset
= 0;
543 fragP
->fr_symbol
= NULL
;
548 eh_frame_convert_frag (fragP
);
552 dwarf2dbg_convert_frag (fragP
);
555 case rs_machine_dependent
:
557 md_convert_frag (stdoutput
, sec
, fragP
);
559 md_convert_frag (headersP
, sec
, fragP
);
562 assert (fragP
->fr_next
== NULL
563 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
566 /* After md_convert_frag, we make the frag into a ".space 0".
567 md_convert_frag() should set up any fixSs and constants
572 #ifndef WORKING_DOT_WORD
575 struct broken_word
*lie
;
577 if (fragP
->fr_subtype
)
579 fragP
->fr_fix
+= md_short_jump_size
;
580 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
581 lie
&& lie
->dispfrag
== fragP
;
582 lie
= lie
->next_broken_word
)
584 fragP
->fr_fix
+= md_long_jump_size
;
592 BAD_CASE (fragP
->fr_type
);
597 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
601 relax_and_size_seg (abfd
, sec
, xxx
)
604 PTR xxx ATTRIBUTE_UNUSED
;
608 segment_info_type
*seginfo
;
610 valueT size
, newsize
;
612 subseg_change (sec
, 0);
614 flags
= bfd_get_section_flags (abfd
, sec
);
616 seginfo
= seg_info (sec
);
617 if (seginfo
&& seginfo
->frchainP
)
619 relax_segment (seginfo
->frchainP
->frch_root
, sec
);
620 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
621 cvt_frag_to_fill (sec
, fragp
);
622 for (fragp
= seginfo
->frchainP
->frch_root
;
624 fragp
= fragp
->fr_next
)
625 /* Walk to last elt. */
627 size
= fragp
->fr_address
+ fragp
->fr_fix
;
632 if (size
> 0 && ! seginfo
->bss
)
633 flags
|= SEC_HAS_CONTENTS
;
635 /* @@ This is just an approximation. */
636 if (seginfo
&& seginfo
->fix_root
)
640 x
= bfd_set_section_flags (abfd
, sec
, flags
);
643 newsize
= md_section_align (sec
, size
);
644 x
= bfd_set_section_size (abfd
, sec
, newsize
);
647 /* If the size had to be rounded up, add some padding in the last
649 assert (newsize
>= size
);
652 fragS
*last
= seginfo
->frchainP
->frch_last
;
653 fragp
= seginfo
->frchainP
->frch_root
;
654 while (fragp
->fr_next
!= last
)
655 fragp
= fragp
->fr_next
;
656 last
->fr_address
= size
;
657 fragp
->fr_offset
+= newsize
- size
;
660 #ifdef tc_frob_section
661 tc_frob_section (sec
);
663 #ifdef obj_frob_section
664 obj_frob_section (sec
);
670 dump_section_relocs (abfd
, sec
, stream_
)
671 bfd
*abfd ATTRIBUTE_UNUSED
;
675 FILE *stream
= (FILE *) stream_
;
676 segment_info_type
*seginfo
= seg_info (sec
);
677 fixS
*fixp
= seginfo
->fix_root
;
682 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
685 symbolS
*s
= fixp
->fx_addsy
;
687 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
688 (int) fixp
->fx_r_type
);
690 fprintf (stream
, "no sym\n");
693 print_symbol_value_1 (stream
, s
);
694 fprintf (stream
, "\n");
696 fixp
= fixp
->fx_next
;
700 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
703 #ifndef EMIT_SECTION_SYMBOLS
704 #define EMIT_SECTION_SYMBOLS 1
708 adjust_reloc_syms (abfd
, sec
, xxx
)
709 bfd
*abfd ATTRIBUTE_UNUSED
;
711 PTR xxx ATTRIBUTE_UNUSED
;
713 segment_info_type
*seginfo
= seg_info (sec
);
719 dump_section_relocs (abfd
, sec
, stderr
);
721 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
725 else if (fixp
->fx_addsy
)
731 fprintf (stderr
, "\n\nadjusting fixup:\n");
735 sym
= fixp
->fx_addsy
;
737 /* All symbols should have already been resolved at this
738 point. It is possible to see unresolved expression
739 symbols, though, since they are not in the regular symbol
742 resolve_symbol_value (sym
, 1);
744 if (fixp
->fx_subsy
!= NULL
)
745 resolve_symbol_value (fixp
->fx_subsy
, 1);
747 /* If this symbol is equated to an undefined symbol, convert
748 the fixup to being against that symbol. */
749 if (sym
!= NULL
&& symbol_equated_p (sym
)
750 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
752 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
753 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
754 fixp
->fx_addsy
= sym
;
757 if (sym
!= NULL
&& symbol_mri_common_p (sym
))
759 /* These symbols are handled specially in fixup_segment. */
763 symsec
= S_GET_SEGMENT (sym
);
768 if (bfd_is_abs_section (symsec
))
770 /* The fixup_segment routine will not use this symbol in a
771 relocation unless TC_FORCE_RELOCATION returns 1. */
772 if (TC_FORCE_RELOCATION (fixp
))
774 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
775 #ifdef UNDEFINED_DIFFERENCE_OK
776 if (fixp
->fx_subsy
!= NULL
)
777 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
783 /* If it's one of these sections, assume the symbol is
784 definitely going to be output. The code in
785 md_estimate_size_before_relax in tc-mips.c uses this test
786 as well, so if you change this code you should look at that
788 if (bfd_is_und_section (symsec
)
789 || bfd_is_com_section (symsec
))
791 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
792 #ifdef UNDEFINED_DIFFERENCE_OK
793 /* We have the difference of an undefined symbol and some
794 other symbol. Make sure to mark the other symbol as used
795 in a relocation so that it will always be output. */
797 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
802 /* Don't try to reduce relocs which refer to non-local symbols
803 in .linkonce sections. It can lead to confusion when a
804 debugging section refers to a .linkonce section. I hope
805 this will always be correct. */
806 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
812 if ((bfd_get_section_flags (stdoutput
, symsec
) & SEC_LINK_ONCE
)
817 /* The GNU toolchain uses an extension for ELF: a section
818 beginning with the magic string .gnu.linkonce is a
820 if (strncmp (segment_name (symsec
), ".gnu.linkonce",
821 sizeof ".gnu.linkonce" - 1) == 0)
827 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
828 #ifdef UNDEFINED_DIFFERENCE_OK
829 if (fixp
->fx_subsy
!= NULL
)
830 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
836 /* Since we're reducing to section symbols, don't attempt to reduce
837 anything that's already using one. */
838 if (symbol_section_p (sym
))
840 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
845 /* We can never adjust a reloc against a weak symbol. If we
846 did, and the weak symbol was overridden by a real symbol
847 somewhere else, then our relocation would be pointing at
848 the wrong area of memory. */
851 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
856 /* Is there some other reason we can't adjust this one? (E.g.,
857 call/bal links in i960-bout symbols.) */
858 #ifdef obj_fix_adjustable
859 if (! obj_fix_adjustable (fixp
))
861 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
866 /* Is there some other (target cpu dependent) reason we can't adjust
867 this one? (E.g. relocations involving function addresses on
869 #ifdef tc_fix_adjustable
870 if (! tc_fix_adjustable (fixp
))
872 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
877 /* If the section symbol isn't going to be output, the relocs
878 at least should still work. If not, figure out what to do
879 when we run into that case.
881 We refetch the segment when calling section_symbol, rather
882 than using symsec, because S_GET_VALUE may wind up changing
883 the section when it calls resolve_symbol_value. */
884 fixp
->fx_offset
+= S_GET_VALUE (sym
);
885 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
886 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
888 fprintf (stderr
, "\nadjusted fixup:\n");
895 #if 1 /* def RELOC_REQUIRES_SYMBOL */
898 /* There was no symbol required by this relocation. However,
899 BFD doesn't really handle relocations without symbols well.
900 (At least, the COFF support doesn't.) So for now we fake up
901 a local symbol in the absolute section. */
903 fixp
->fx_addsy
= section_symbol (absolute_section
);
905 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
910 dump_section_relocs (abfd
, sec
, stderr
);
914 write_relocs (abfd
, sec
, xxx
)
917 PTR xxx ATTRIBUTE_UNUSED
;
919 segment_info_type
*seginfo
= seg_info (sec
);
926 /* If seginfo is NULL, we did not create this section; don't do
931 fixup_segment (seginfo
->fix_root
, sec
);
934 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
937 #ifndef RELOC_EXPANSION_POSSIBLE
938 /* Set up reloc information as well. */
939 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
940 memset ((char *) relocs
, 0, n
* sizeof (arelent
*));
943 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
946 bfd_reloc_status_type s
;
955 /* If this is an undefined symbol which was equated to another
956 symbol, then use generate the reloc against the latter symbol
957 rather than the former. */
958 sym
= fixp
->fx_addsy
;
959 while (symbol_equated_p (sym
)
960 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
964 /* We must avoid looping, as that can occur with a badly
966 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
969 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
972 fixp
->fx_addsy
= sym
;
974 reloc
= tc_gen_reloc (sec
, fixp
);
982 /* This test is triggered inappropriately for the SH. */
983 if (fixp
->fx_where
+ fixp
->fx_size
984 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
988 s
= bfd_install_relocation (stdoutput
, reloc
,
989 fixp
->fx_frag
->fr_literal
,
990 fixp
->fx_frag
->fr_address
,
996 case bfd_reloc_overflow
:
997 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation overflow"));
999 case bfd_reloc_outofrange
:
1000 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation out of range"));
1003 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1004 fixp
->fx_file
, fixp
->fx_line
, s
);
1006 relocs
[i
++] = reloc
;
1009 n
= n
* MAX_RELOC_EXPANSION
;
1010 /* Set up reloc information as well. */
1011 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
1014 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1018 bfd_reloc_status_type s
;
1028 /* If this is an undefined symbol which was equated to another
1029 symbol, then generate the reloc against the latter symbol
1030 rather than the former. */
1031 sym
= fixp
->fx_addsy
;
1032 while (symbol_equated_p (sym
)
1033 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
1034 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
1035 fixp
->fx_addsy
= sym
;
1037 reloc
= tc_gen_reloc (sec
, fixp
);
1039 for (j
= 0; reloc
[j
]; j
++)
1041 relocs
[i
++] = reloc
[j
];
1044 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1045 if (fixp
->fx_where
+ fixp
->fx_size
1046 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1047 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1048 _("internal error: fixup not contained within frag"));
1049 for (j
= 0; reloc
[j
]; j
++)
1051 s
= bfd_install_relocation (stdoutput
, reloc
[j
],
1052 fixp
->fx_frag
->fr_literal
,
1053 fixp
->fx_frag
->fr_address
,
1059 case bfd_reloc_overflow
:
1060 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1061 _("relocation overflow"));
1064 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1065 fixp
->fx_file
, fixp
->fx_line
);
1076 sympp
= bfd_get_outsymbols (stdoutput
);
1077 nsyms
= bfd_get_symcount (stdoutput
);
1078 for (i
= 0; i
< n
; i
++)
1079 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1081 for (j
= 0; j
< nsyms
; j
++)
1082 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
1091 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1093 bfd_set_section_flags (abfd
, sec
,
1094 (bfd_get_section_flags (abfd
, sec
)
1095 & (flagword
) ~SEC_RELOC
));
1097 #ifdef SET_SECTION_RELOCS
1098 SET_SECTION_RELOCS (sec
, relocs
, n
);
1106 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1107 for (i
= 0; i
< n
; i
++)
1110 s
= *r
->sym_ptr_ptr
;
1111 fprintf (stderr
, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1112 i
, r
, r
->address
, s
->name
, r
->addend
);
1119 write_contents (abfd
, sec
, xxx
)
1120 bfd
*abfd ATTRIBUTE_UNUSED
;
1122 PTR xxx ATTRIBUTE_UNUSED
;
1124 segment_info_type
*seginfo
= seg_info (sec
);
1125 unsigned long offset
= 0;
1128 /* Write out the frags. */
1130 || !(bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1133 for (f
= seginfo
->frchainP
->frch_root
;
1138 unsigned long fill_size
;
1142 assert (f
->fr_type
== rs_fill
);
1145 x
= bfd_set_section_contents (stdoutput
, sec
,
1146 f
->fr_literal
, (file_ptr
) offset
,
1147 (bfd_size_type
) f
->fr_fix
);
1150 bfd_perror (stdoutput
->filename
);
1151 as_perror (_("FATAL: Can't write %s"), stdoutput
->filename
);
1152 exit (EXIT_FAILURE
);
1154 offset
+= f
->fr_fix
;
1156 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1157 fill_size
= f
->fr_var
;
1158 count
= f
->fr_offset
;
1159 assert (count
>= 0);
1160 if (fill_size
&& count
)
1163 if (fill_size
> sizeof (buf
))
1165 /* Do it the old way. Can this ever happen? */
1168 x
= bfd_set_section_contents (stdoutput
, sec
,
1171 (bfd_size_type
) fill_size
);
1174 bfd_perror (stdoutput
->filename
);
1175 as_perror (_("FATAL: Can't write %s"),
1176 stdoutput
->filename
);
1177 exit (EXIT_FAILURE
);
1179 offset
+= fill_size
;
1184 /* Build a buffer full of fill objects and output it as
1185 often as necessary. This saves on the overhead of
1186 potentially lots of bfd_set_section_contents calls. */
1190 n_per_buf
= sizeof (buf
);
1191 memset (buf
, *fill_literal
, n_per_buf
);
1196 n_per_buf
= sizeof (buf
) / fill_size
;
1197 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1198 memcpy (bufp
, fill_literal
, fill_size
);
1200 for (; count
> 0; count
-= n_per_buf
)
1202 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1203 x
= bfd_set_section_contents
1204 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1205 (bfd_size_type
) n_per_buf
* fill_size
);
1207 as_fatal (_("Cannot write to output file."));
1208 offset
+= n_per_buf
* fill_size
;
1216 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1218 merge_data_into_text ()
1220 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1221 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1222 seg_info (data_section
)->frchainP
->frch_root
;
1223 seg_info (text_section
)->frchainP
->frch_last
=
1224 seg_info (data_section
)->frchainP
->frch_last
;
1225 seg_info (data_section
)->frchainP
= 0;
1229 text_last_frag
->fr_next
= data_frag_root
;
1230 text_last_frag
= data_last_frag
;
1231 data_last_frag
= NULL
;
1232 data_frag_root
= NULL
;
1235 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
1236 tmp
->fx_next
= data_fix_root
;
1237 text_fix_tail
= data_fix_tail
;
1240 text_fix_root
= data_fix_root
;
1241 data_fix_root
= NULL
;
1244 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1246 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1248 relax_and_size_all_segments ()
1252 relax_segment (text_frag_root
, SEG_TEXT
);
1253 relax_segment (data_frag_root
, SEG_DATA
);
1254 relax_segment (bss_frag_root
, SEG_BSS
);
1256 /* Now the addresses of frags are correct within the segment. */
1257 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
1258 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
1259 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
1261 /* Join the 2 segments into 1 huge segment.
1262 To do this, re-compute every rn_address in the SEG_DATA frags.
1263 Then join the data frags after the text frags.
1265 Determine a_data [length of data segment]. */
1268 register relax_addressT slide
;
1270 know ((text_last_frag
->fr_type
== rs_fill
)
1271 && (text_last_frag
->fr_offset
== 0));
1273 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
1274 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
1275 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
1277 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1278 /* For b.out: If the data section has a strict alignment
1279 requirement, its load address in the .o file will be
1280 rounded up from the size of the text section. These
1281 two values are *not* the same! Similarly for the bss
1283 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
1286 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1287 fragP
->fr_address
+= slide
;
1289 know (text_last_frag
!= 0);
1290 text_last_frag
->fr_next
= data_frag_root
;
1294 H_SET_DATA_SIZE (&headers
, 0);
1298 /* See above comments on b.out data section address. */
1301 if (data_last_frag
== 0)
1302 bss_vma
= H_GET_TEXT_SIZE (&headers
);
1304 bss_vma
= data_last_frag
->fr_address
;
1305 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
1306 bss_address_frag
.fr_address
= bss_vma
;
1308 #else /* ! OBJ_BOUT */
1309 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
1310 H_GET_DATA_SIZE (&headers
));
1312 #endif /* ! OBJ_BOUT */
1314 /* Slide all the frags. */
1317 relax_addressT slide
= bss_address_frag
.fr_address
;
1319 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1320 fragP
->fr_address
+= slide
;
1324 H_SET_BSS_SIZE (&headers
,
1325 bss_last_frag
->fr_address
- bss_frag_root
->fr_address
);
1327 H_SET_BSS_SIZE (&headers
, 0);
1329 #endif /* ! BFD_ASSEMBLER && ! BFD */
1331 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1333 #ifdef BFD_ASSEMBLER
1341 extern PTR bfd_alloc
PARAMS ((bfd
*, size_t));
1343 /* Count symbols. We can't rely on a count made by the loop in
1344 write_object_file, because *_frob_file may add a new symbol or
1347 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1354 asympp
= (asymbol
**) bfd_alloc (stdoutput
,
1355 nsyms
* sizeof (asymbol
*));
1356 symp
= symbol_rootP
;
1357 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1359 asympp
[i
] = symbol_get_bfdsym (symp
);
1360 symbol_mark_written (symp
);
1365 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1366 assert (result
== true);
1367 symbol_table_frozen
= 1;
1371 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1373 set_segment_vma (abfd
, sec
, xxx
)
1376 PTR xxx ATTRIBUTE_UNUSED
;
1378 static bfd_vma addr
= 0;
1380 bfd_set_section_vma (abfd
, sec
, addr
);
1381 addr
+= bfd_section_size (abfd
, sec
);
1383 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1385 /* Finish the subsegments. After every sub-segment, we fake an
1386 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1387 ".fill 0" because that is the kind of frag that requires least
1388 thought. ".align" frags like to have a following frag since that
1389 makes calculating their intended length trivial. */
1391 #ifndef SUB_SEGMENT_ALIGN
1392 #ifdef BFD_ASSEMBLER
1393 #define SUB_SEGMENT_ALIGN(SEG) (0)
1395 #define SUB_SEGMENT_ALIGN(SEG) (2)
1402 struct frchain
*frchainP
;
1404 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
1408 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
1410 /* This now gets called even if we had errors. In that case,
1411 any alignment is meaningless, and, moreover, will look weird
1412 if we are generating a listing. */
1413 alignment
= had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg
);
1415 /* The last subsegment gets an aligment corresponding to the
1416 alignment of the section. This allows proper nop-filling
1417 at the end of code-bearing sections. */
1418 if (!frchainP
->frch_next
|| frchainP
->frch_next
->frch_seg
!= now_seg
)
1419 alignment
= get_recorded_alignment (now_seg
);
1421 if (subseg_text_p (now_seg
))
1422 frag_align_code (alignment
, 0);
1424 frag_align (alignment
, 0, 0);
1426 /* frag_align will have left a new frag.
1427 Use this last frag for an empty ".fill".
1429 For this segment ...
1430 Create a last frag. Do not leave a "being filled in frag". */
1431 frag_wane (frag_now
);
1432 frag_now
->fr_fix
= 0;
1433 know (frag_now
->fr_next
== NULL
);
1437 /* Write the object file. */
1440 write_object_file ()
1442 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1443 fragS
*fragP
; /* Track along all frags. */
1446 /* Do we really want to write it? */
1448 int n_warns
, n_errs
;
1449 n_warns
= had_warnings ();
1450 n_errs
= had_errors ();
1451 /* The -Z flag indicates that an object file should be generated,
1452 regardless of warnings and errors. */
1453 if (flag_always_generate_output
)
1455 if (n_warns
|| n_errs
)
1456 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1457 n_errs
, n_errs
== 1 ? "" : "s",
1458 n_warns
, n_warns
== 1 ? "" : "s");
1463 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1464 n_errs
, n_errs
== 1 ? "" : "s",
1465 n_warns
, n_warns
== 1 ? "" : "s");
1470 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1471 a routine to check for the definition of the procedure "_main",
1472 and if so -- fix it up so that it can be program entry point. */
1473 vms_check_for_main ();
1474 #endif /* OBJ_VMS */
1476 /* From now on, we don't care about sub-segments. Build one frag chain
1477 for each segment. Linked thru fr_next. */
1479 #ifdef BFD_ASSEMBLER
1480 /* Remove the sections created by gas for its own purposes. */
1482 asection
**seclist
, *sec
;
1485 seclist
= &stdoutput
->sections
;
1486 while (seclist
&& *seclist
)
1489 while (sec
== reg_section
|| sec
== expr_section
)
1493 stdoutput
->section_count
--;
1498 seclist
= &(*seclist
)->next
;
1501 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1504 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1506 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
1507 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
1508 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
1511 /* We have two segments. If user gave -R flag, then we must put the
1512 data frags into the text segment. Do this before relaxing so
1513 we know to take advantage of -R and make shorter addresses. */
1514 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1515 if (flag_readonly_data_in_text
)
1517 merge_data_into_text ();
1521 #ifdef BFD_ASSEMBLER
1522 bfd_map_over_sections (stdoutput
, relax_and_size_seg
, (char *) 0);
1524 relax_and_size_all_segments ();
1525 #endif /* BFD_ASSEMBLER */
1527 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1528 /* Now that the segments have their final sizes, run through the
1529 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1530 should too. Currently, only DJGPP uses this code, but other
1531 COFF targets may need to execute this too. */
1532 bfd_map_over_sections (stdoutput
, set_segment_vma
, (char *) 0);
1535 #ifndef BFD_ASSEMBLER
1536 /* Crawl the symbol chain.
1538 For each symbol whose value depends on a frag, take the address of
1539 that frag and subsume it into the value of the symbol.
1540 After this, there is just one way to lookup a symbol value.
1541 Values are left in their final state for object file emission.
1542 We adjust the values of 'L' local symbols, even if we do
1543 not intend to emit them to the object file, because their values
1544 are needed for fix-ups.
1546 Unless we saw a -L flag, remove all symbols that begin with 'L'
1547 from the symbol chain. (They are still pointed to by the fixes.)
1549 Count the remaining symbols.
1550 Assign a symbol number to each symbol.
1551 Count the number of string-table chars we will emit.
1552 Put this info into the headers as appropriate. */
1553 know (zero_address_frag
.fr_address
== 0);
1554 string_byte_count
= sizeof (string_byte_count
);
1556 obj_crawl_symbol_chain (&headers
);
1558 if (string_byte_count
== sizeof (string_byte_count
))
1559 string_byte_count
= 0;
1561 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1563 /* Addresses of frags now reflect addresses we use in the object file.
1564 Symbol values are correct.
1565 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1566 Also converting any machine-dependent frags using md_convert_frag(); */
1567 subseg_change (SEG_TEXT
, 0);
1569 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1571 /* At this point we have linked all the frags into a single
1572 chain. However, cvt_frag_to_fill may call md_convert_frag
1573 which may call fix_new. We need to ensure that fix_new adds
1574 the fixup to the right section. */
1575 if (fragP
== data_frag_root
)
1576 subseg_change (SEG_DATA
, 0);
1578 cvt_frag_to_fill (&headers
, SEG_TEXT
, fragP
);
1580 /* Some assert macros don't work with # directives mixed in. */
1582 if (!(fragP
->fr_next
== NULL
1584 || fragP
->fr_next
== data_frag_root
1586 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
1587 == (fragP
->fr_fix
+ fragP
->fr_offset
* fragP
->fr_var
))))
1591 #endif /* ! BFD_ASSEMBLER */
1593 #ifndef WORKING_DOT_WORD
1595 struct broken_word
*lie
;
1596 struct broken_word
**prevP
;
1598 prevP
= &broken_words
;
1599 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1604 subseg_change (lie
->seg
, lie
->subseg
);
1605 exp
.X_op
= O_subtract
;
1606 exp
.X_add_symbol
= lie
->add
;
1607 exp
.X_op_symbol
= lie
->sub
;
1608 exp
.X_add_number
= lie
->addnum
;
1609 #ifdef BFD_ASSEMBLER
1610 #ifdef TC_CONS_FIX_NEW
1611 TC_CONS_FIX_NEW (lie
->frag
,
1612 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1615 fix_new_exp (lie
->frag
,
1616 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1617 2, &exp
, 0, BFD_RELOC_16
);
1620 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1621 fix_new_exp (lie
->frag
,
1622 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1623 2, &exp
, 0, NO_RELOC
);
1626 fix_new_ns32k_exp (lie
->frag
,
1627 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1628 2, &exp
, 0, 0, 2, 0, 0);
1630 fix_new_exp (lie
->frag
,
1631 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1633 #endif /* TC_NS32K */
1634 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1635 #endif /* BFD_ASSEMBLER */
1636 *prevP
= lie
->next_broken_word
;
1639 prevP
= &(lie
->next_broken_word
);
1641 for (lie
= broken_words
; lie
;)
1643 struct broken_word
*untruth
;
1645 addressT table_addr
;
1646 addressT from_addr
, to_addr
;
1649 subseg_change (lie
->seg
, lie
->subseg
);
1650 fragP
= lie
->dispfrag
;
1652 /* Find out how many broken_words go here. */
1655 untruth
&& untruth
->dispfrag
== fragP
;
1656 untruth
= untruth
->next_broken_word
)
1657 if (untruth
->added
== 1)
1660 table_ptr
= lie
->dispfrag
->fr_opcode
;
1661 table_addr
= (lie
->dispfrag
->fr_address
1662 + (table_ptr
- lie
->dispfrag
->fr_literal
));
1663 /* Create the jump around the long jumps. This is a short
1664 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1665 from_addr
= table_addr
;
1666 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1667 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1669 table_ptr
+= md_short_jump_size
;
1670 table_addr
+= md_short_jump_size
;
1673 lie
&& lie
->dispfrag
== fragP
;
1674 m
++, lie
= lie
->next_broken_word
)
1676 if (lie
->added
== 2)
1678 /* Patch the jump table. */
1679 /* This is the offset from ??? to table_ptr+0. */
1680 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1681 #ifdef BFD_ASSEMBLER
1682 to_addr
-= symbol_get_frag (lie
->sub
)->fr_address
;
1684 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1685 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr
, lie
);
1687 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1688 for (untruth
= lie
->next_broken_word
;
1689 untruth
&& untruth
->dispfrag
== fragP
;
1690 untruth
= untruth
->next_broken_word
)
1692 if (untruth
->use_jump
== lie
)
1693 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1696 /* Install the long jump. */
1697 /* This is a long jump from table_ptr+0 to the final target. */
1698 from_addr
= table_addr
;
1699 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1700 #ifdef BFD_ASSEMBLER
1701 to_addr
+= symbol_get_frag (lie
->add
)->fr_address
;
1703 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1705 table_ptr
+= md_long_jump_size
;
1706 table_addr
+= md_long_jump_size
;
1710 #endif /* not WORKING_DOT_WORD */
1712 #ifndef BFD_ASSEMBLER
1715 char *the_object_file
;
1716 long object_file_size
;
1717 /* Scan every FixS performing fixups. We had to wait until now to
1718 do this because md_convert_frag() may have made some fixSs. */
1721 subseg_change (SEG_TEXT
, 0);
1722 trsize
= md_reloc_size
* fixup_segment (text_fix_root
, SEG_TEXT
);
1723 subseg_change (SEG_DATA
, 0);
1724 drsize
= md_reloc_size
* fixup_segment (data_fix_root
, SEG_DATA
);
1725 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
1727 /* FIXME: Move this stuff into the pre-write-hook. */
1728 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
1729 H_SET_ENTRY_POINT (&headers
, 0);
1731 obj_pre_write_hook (&headers
); /* Extra coff stuff. */
1733 object_file_size
= H_GET_FILE_SIZE (&headers
);
1734 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
1736 output_file_create (out_file_name
);
1738 obj_header_append (&next_object_file_charP
, &headers
);
1740 know ((next_object_file_charP
- the_object_file
)
1741 == H_GET_HEADER_SIZE (&headers
));
1744 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1746 register long count
;
1747 register char *fill_literal
;
1748 register long fill_size
;
1751 know (fragP
->fr_type
== rs_fill
);
1752 append (&next_object_file_charP
, fragP
->fr_literal
,
1753 (unsigned long) fragP
->fr_fix
);
1754 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
1755 fill_size
= fragP
->fr_var
;
1756 know (fragP
->fr_offset
>= 0);
1758 for (count
= fragP
->fr_offset
; count
; count
--)
1759 append (&next_object_file_charP
, fill_literal
,
1760 (unsigned long) fill_size
);
1763 know ((next_object_file_charP
- the_object_file
)
1764 == (H_GET_HEADER_SIZE (&headers
)
1765 + H_GET_TEXT_SIZE (&headers
)
1766 + H_GET_DATA_SIZE (&headers
)));
1768 /* Emit relocations. */
1769 obj_emit_relocations (&next_object_file_charP
, text_fix_root
,
1770 (relax_addressT
) 0);
1771 know ((next_object_file_charP
- the_object_file
)
1772 == (H_GET_HEADER_SIZE (&headers
)
1773 + H_GET_TEXT_SIZE (&headers
)
1774 + H_GET_DATA_SIZE (&headers
)
1775 + H_GET_TEXT_RELOCATION_SIZE (&headers
)));
1777 /* Make addresses in data relocation directives relative to beginning of
1778 first data fragment, not end of last text fragment: alignment of the
1779 start of the data segment may place a gap between the segments. */
1780 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1781 data0_frchainP
->frch_root
->fr_address
);
1783 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1784 text_last_frag
->fr_address
);
1785 #endif /* TC_I960 */
1787 know ((next_object_file_charP
- the_object_file
)
1788 == (H_GET_HEADER_SIZE (&headers
)
1789 + H_GET_TEXT_SIZE (&headers
)
1790 + H_GET_DATA_SIZE (&headers
)
1791 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1792 + H_GET_DATA_RELOCATION_SIZE (&headers
)));
1794 /* Emit line number entries. */
1795 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
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
)
1800 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1801 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1802 + H_GET_LINENO_SIZE (&headers
)));
1805 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
1806 know ((next_object_file_charP
- the_object_file
)
1807 == (H_GET_HEADER_SIZE (&headers
)
1808 + H_GET_TEXT_SIZE (&headers
)
1809 + H_GET_DATA_SIZE (&headers
)
1810 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1811 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1812 + H_GET_LINENO_SIZE (&headers
)
1813 + H_GET_SYMBOL_TABLE_SIZE (&headers
)));
1816 if (string_byte_count
> 0)
1817 obj_emit_strings (&next_object_file_charP
);
1820 bfd_seek (stdoutput
, 0, 0);
1821 bfd_write (the_object_file
, 1, object_file_size
, stdoutput
);
1824 /* Write the data to the file. */
1825 output_file_append (the_object_file
, object_file_size
, out_file_name
);
1826 free (the_object_file
);
1830 /* Now do the VMS-dependent part of writing the object file. */
1831 vms_write_object_file (H_GET_TEXT_SIZE (&headers
),
1832 H_GET_DATA_SIZE (&headers
),
1833 H_GET_BSS_SIZE (&headers
),
1834 text_frag_root
, data_frag_root
);
1835 #endif /* OBJ_VMS */
1836 #else /* BFD_ASSEMBLER */
1838 /* Resolve symbol values. This needs to be done before processing
1844 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1845 resolve_symbol_value (symp
, 1);
1847 resolve_local_symbol_values ();
1851 #ifdef tc_frob_file_before_adjust
1852 tc_frob_file_before_adjust ();
1854 #ifdef obj_frob_file_before_adjust
1855 obj_frob_file_before_adjust ();
1858 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
1860 /* Set up symbol table, and write it out. */
1865 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1870 if (symbol_mri_common_p (symp
))
1872 if (S_IS_EXTERNAL (symp
))
1873 as_bad (_("%s: global symbols not supported in common sections"),
1875 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1879 name
= S_GET_NAME (symp
);
1883 decode_local_label_name ((char *) S_GET_NAME (symp
));
1884 /* They only differ if `name' is a fb or dollar local
1886 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
1887 as_bad (_("local label %s is not defined"), name2
);
1890 /* Do it again, because adjust_reloc_syms might introduce
1891 more symbols. They'll probably only be section symbols,
1892 but they'll still need to have the values computed. */
1893 resolve_symbol_value (symp
, 1);
1895 /* Skip symbols which were equated to undefined or common
1897 if (symbol_equated_p (symp
)
1898 && (! S_IS_DEFINED (symp
) || S_IS_COMMON (symp
)))
1900 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1904 /* So far, common symbols have been treated like undefined symbols.
1905 Put them in the common section now. */
1906 if (S_IS_DEFINED (symp
) == 0
1907 && S_GET_VALUE (symp
) != 0)
1908 S_SET_SEGMENT (symp
, bfd_com_section_ptr
);
1910 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1911 S_GET_NAME (symp
), symp
,
1913 symbol_get_bfdsym (symp
)->flags
,
1914 segment_name (S_GET_SEGMENT (symp
)));
1917 #ifdef obj_frob_symbol
1918 obj_frob_symbol (symp
, punt
);
1920 #ifdef tc_frob_symbol
1921 if (! punt
|| symbol_used_in_reloc_p (symp
))
1922 tc_frob_symbol (symp
, punt
);
1925 /* If we don't want to keep this symbol, splice it out of
1926 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1927 want section symbols. Otherwise, we skip local symbols
1928 and symbols that the frob_symbol macros told us to punt,
1929 but we keep such symbols if they are used in relocs. */
1930 if ((! EMIT_SECTION_SYMBOLS
1931 && symbol_section_p (symp
))
1932 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1933 opposites. Sometimes the former checks flags and the
1934 latter examines the name... */
1935 || (!S_IS_EXTERN (symp
)
1936 && (S_IS_LOCAL (symp
) || punt
)
1937 && ! symbol_used_in_reloc_p (symp
)))
1939 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1941 /* After symbol_remove, symbol_next(symp) still returns
1942 the one that came after it in the chain. So we don't
1943 need to do any extra cleanup work here. */
1947 /* Make sure we really got a value for the symbol. */
1948 if (! symbol_resolved_p (symp
))
1950 as_bad (_("can't resolve value for symbol \"%s\""),
1952 symbol_mark_resolved (symp
);
1955 /* Set the value into the BFD symbol. Up til now the value
1956 has only been kept in the gas symbolS struct. */
1957 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
1963 /* Now do any format-specific adjustments to the symbol table, such
1964 as adding file symbols. */
1965 #ifdef tc_adjust_symtab
1966 tc_adjust_symtab ();
1968 #ifdef obj_adjust_symtab
1969 obj_adjust_symtab ();
1972 /* Now that all the sizes are known, and contents correct, we can
1973 start writing to the file. */
1976 /* If *_frob_file changes the symbol value at this point, it is
1977 responsible for moving the changed value into symp->bsym->value
1978 as well. Hopefully all symbol value changing can be done in
1983 #ifdef obj_frob_file
1987 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
1989 #ifdef tc_frob_file_after_relocs
1990 tc_frob_file_after_relocs ();
1992 #ifdef obj_frob_file_after_relocs
1993 obj_frob_file_after_relocs ();
1996 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
1997 #endif /* BFD_ASSEMBLER */
2001 #ifdef TC_GENERIC_RELAX_TABLE
2003 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2006 relax_frag (segment
, fragP
, stretch
)
2011 const relax_typeS
*this_type
;
2012 const relax_typeS
*start_type
;
2013 relax_substateT next_state
;
2014 relax_substateT this_state
;
2020 const relax_typeS
*table
;
2022 target
= fragP
->fr_offset
;
2023 address
= fragP
->fr_address
;
2024 table
= TC_GENERIC_RELAX_TABLE
;
2025 this_state
= fragP
->fr_subtype
;
2026 start_type
= this_type
= table
+ this_state
;
2027 symbolP
= fragP
->fr_symbol
;
2033 sym_frag
= symbol_get_frag (symbolP
);
2035 #ifndef DIFF_EXPR_OK
2036 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2037 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2038 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2039 || (S_GET_SEGMENT (symbolP
) == SEG_BSS
)
2040 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
2042 know (sym_frag
!= NULL
);
2044 know (!(S_GET_SEGMENT (symbolP
) == absolute_section
)
2045 || sym_frag
== &zero_address_frag
);
2046 target
+= S_GET_VALUE (symbolP
) + sym_frag
->fr_address
;
2048 /* If frag has yet to be reached on this pass,
2049 assume it will move by STRETCH just as we did.
2050 If this is not so, it will be because some frag
2051 between grows, and that will force another pass. */
2054 && sym_frag
->relax_marker
!= fragP
->relax_marker
2055 && S_GET_SEGMENT (symbolP
) == segment
)
2061 aim
= target
- address
- fragP
->fr_fix
;
2062 #ifdef TC_PCREL_ADJUST
2063 /* Currently only the ns32k family needs this. */
2064 aim
+= TC_PCREL_ADJUST (fragP
);
2066 /* This machine doesn't want to use pcrel_adjust.
2067 In that case, pcrel_adjust should be zero. */
2069 assert (fragP
->fr_targ
.ns32k
.pcrel_adjust
== 0);
2072 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2073 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2078 /* Look backwards. */
2079 for (next_state
= this_type
->rlx_more
; next_state
;)
2080 if (aim
>= this_type
->rlx_backward
)
2084 /* Grow to next state. */
2085 this_state
= next_state
;
2086 this_type
= table
+ this_state
;
2087 next_state
= this_type
->rlx_more
;
2092 /* Look forwards. */
2093 for (next_state
= this_type
->rlx_more
; next_state
;)
2094 if (aim
<= this_type
->rlx_forward
)
2098 /* Grow to next state. */
2099 this_state
= next_state
;
2100 this_type
= table
+ this_state
;
2101 next_state
= this_type
->rlx_more
;
2105 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2107 fragP
->fr_subtype
= this_state
;
2111 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2113 /* Relax_align. Advance location counter to next address that has 'alignment'
2114 lowest order bits all 0s, return size of adjustment made. */
2115 static relax_addressT
2116 relax_align (address
, alignment
)
2117 register relax_addressT address
; /* Address now. */
2118 register int alignment
; /* Alignment (binary). */
2120 relax_addressT mask
;
2121 relax_addressT new_address
;
2123 mask
= ~((~0) << alignment
);
2124 new_address
= (address
+ mask
) & (~mask
);
2125 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2127 /* We must provide lots of padding, so the linker can discard it
2128 when needed. The linker will not add extra space, ever. */
2129 new_address
+= (1 << alignment
);
2131 return (new_address
- address
);
2134 /* Now we have a segment, not a crowd of sub-segments, we can make
2139 After this, all frags in this segment have addresses that are correct
2140 within the segment. Since segments live in different file addresses,
2141 these frag addresses may not be the same as final object-file
2145 relax_segment (segment_frag_root
, segment
)
2146 struct frag
*segment_frag_root
;
2149 register struct frag
*fragP
;
2150 register relax_addressT address
;
2151 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2152 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
2154 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2155 subseg_change (segment
, 0);
2157 /* For each frag in segment: count and store (a 1st guess of)
2160 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2162 fragP
->relax_marker
= 0;
2163 fragP
->fr_address
= address
;
2164 address
+= fragP
->fr_fix
;
2166 switch (fragP
->fr_type
)
2169 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2176 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2178 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2181 if (offset
% fragP
->fr_var
!= 0)
2183 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2184 (unsigned long) offset
, (long) fragP
->fr_var
);
2185 offset
-= (offset
% fragP
->fr_var
);
2194 /* Assume .org is nugatory. It will grow with 1st relax. */
2197 case rs_machine_dependent
:
2198 address
+= md_estimate_size_before_relax (fragP
, segment
);
2201 #ifndef WORKING_DOT_WORD
2202 /* Broken words don't concern us yet. */
2203 case rs_broken_word
:
2208 /* Initial guess is always 1; doing otherwise can result in
2209 stable solutions that are larger than the minimum. */
2210 address
+= fragP
->fr_offset
= 1;
2214 address
+= eh_frame_estimate_size_before_relax (fragP
);
2218 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
2222 BAD_CASE (fragP
->fr_type
);
2229 long stretch
; /* May be any size, 0 or negative. */
2230 /* Cumulative number of addresses we have relaxed this pass.
2231 We may have relaxed more than one address. */
2232 long stretched
; /* Have we stretched on this pass? */
2233 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2234 grew, and another shrank. If a branch instruction doesn't fit anymore,
2235 we could be scrod. */
2239 stretch
= stretched
= 0;
2241 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2244 addressT was_address
;
2248 fragP
->relax_marker
^= 1;
2249 was_address
= fragP
->fr_address
;
2250 address
= fragP
->fr_address
+= stretch
;
2251 symbolP
= fragP
->fr_symbol
;
2252 offset
= fragP
->fr_offset
;
2254 switch (fragP
->fr_type
)
2256 case rs_fill
: /* .fill never relaxes. */
2260 #ifndef WORKING_DOT_WORD
2261 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2262 for it I do not want to write it. I do not want to have
2263 anything to do with it. This is not the proper way to
2264 implement this misfeature. */
2265 case rs_broken_word
:
2267 struct broken_word
*lie
;
2268 struct broken_word
*untruth
;
2270 /* Yes this is ugly (storing the broken_word pointer
2271 in the symbol slot). Still, this whole chunk of
2272 code is ugly, and I don't feel like doing anything
2273 about it. Think of it as stubbornness in action. */
2275 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2276 lie
&& lie
->dispfrag
== fragP
;
2277 lie
= lie
->next_broken_word
)
2283 offset
= (symbol_get_frag (lie
->add
)->fr_address
2284 + S_GET_VALUE (lie
->add
)
2286 - (symbol_get_frag (lie
->sub
)->fr_address
2287 + S_GET_VALUE (lie
->sub
)));
2288 if (offset
<= -32768 || offset
>= 32767)
2290 if (flag_warn_displacement
)
2293 sprint_value (buf
, (addressT
) lie
->addnum
);
2294 as_warn (_(".word %s-%s+%s didn't fit"),
2295 S_GET_NAME (lie
->add
),
2296 S_GET_NAME (lie
->sub
),
2300 if (fragP
->fr_subtype
== 0)
2302 fragP
->fr_subtype
++;
2303 growth
+= md_short_jump_size
;
2305 for (untruth
= lie
->next_broken_word
;
2306 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2307 untruth
= untruth
->next_broken_word
)
2308 if ((symbol_get_frag (untruth
->add
)
2309 == symbol_get_frag (lie
->add
))
2310 && (S_GET_VALUE (untruth
->add
)
2311 == S_GET_VALUE (lie
->add
)))
2314 untruth
->use_jump
= lie
;
2316 growth
+= md_long_jump_size
;
2321 } /* case rs_broken_word */
2327 addressT oldoff
, newoff
;
2329 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2331 newoff
= relax_align (address
+ fragP
->fr_fix
,
2334 if (fragP
->fr_subtype
!= 0)
2336 if (oldoff
> fragP
->fr_subtype
)
2338 if (newoff
> fragP
->fr_subtype
)
2342 growth
= newoff
- oldoff
;
2348 long target
= offset
;
2353 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2354 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2355 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2356 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
)
2357 || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
2358 know (symbolP
->sy_frag
);
2359 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2360 || (symbolP
->sy_frag
== &zero_address_frag
));
2362 target
+= (S_GET_VALUE (symbolP
)
2363 + symbol_get_frag (symbolP
)->fr_address
);
2364 } /* if we have a symbol */
2366 know (fragP
->fr_next
);
2367 after
= fragP
->fr_next
->fr_address
;
2368 growth
= target
- after
;
2371 /* Growth may be negative, but variable part of frag
2372 cannot have fewer than 0 chars. That is, we can't
2374 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2375 _("attempt to .org backwards ignored"));
2377 /* We've issued an error message. Change the
2378 frag to avoid cascading errors. */
2379 fragP
->fr_type
= rs_align
;
2380 fragP
->fr_subtype
= 0;
2381 fragP
->fr_offset
= 0;
2382 fragP
->fr_fix
= after
- address
;
2386 /* This is an absolute growth factor */
2394 growth
= S_GET_VALUE (symbolP
);
2395 if (symbol_get_frag (symbolP
) != &zero_address_frag
2396 || S_IS_COMMON (symbolP
)
2397 || ! S_IS_DEFINED (symbolP
))
2398 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2399 _(".space specifies non-absolute value"));
2400 fragP
->fr_symbol
= 0;
2403 as_warn (_(".space or .fill with negative value, ignored"));
2411 case rs_machine_dependent
:
2412 #ifdef md_relax_frag
2413 growth
= md_relax_frag (segment
, fragP
, stretch
);
2415 #ifdef TC_GENERIC_RELAX_TABLE
2416 /* The default way to relax a frag is to look through
2417 TC_GENERIC_RELAX_TABLE. */
2418 growth
= relax_frag (segment
, fragP
, stretch
);
2419 #endif /* TC_GENERIC_RELAX_TABLE */
2428 value
= resolve_symbol_value (fragP
->fr_symbol
, 0);
2429 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2430 growth
= size
- fragP
->fr_offset
;
2431 fragP
->fr_offset
= size
;
2436 growth
= eh_frame_relax_frag (fragP
);
2440 growth
= dwarf2dbg_relax_frag (fragP
);
2444 BAD_CASE (fragP
->fr_type
);
2452 } /* For each frag in the segment. */
2454 while (stretched
); /* Until nothing further to relax. */
2457 /* We now have valid fr_address'es for each frag. */
2459 /* All fr_address's are correct, relative to their own segment.
2460 We have made all the fixS we will ever make. */
2463 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2465 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2466 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2471 Go through all the fixS's in a segment and see which ones can be
2472 handled now. (These consist of fixS where we have since discovered
2473 the value of a symbol, or the address of the frag involved.)
2474 For each one, call md_apply_fix to put the fix into the frag data.
2476 Result is a count of how many relocation structs will be needed to
2477 handle the remaining fixS's that we couldn't completely handle here.
2478 These will be output later by emit_relocations(). */
2481 fixup_segment (fixP
, this_segment_type
)
2482 register fixS
*fixP
;
2483 segT this_segment_type
; /* N_TYPE bits for segment. */
2485 long seg_reloc_count
= 0;
2486 symbolS
*add_symbolP
;
2487 symbolS
*sub_symbolP
;
2494 segT add_symbol_segment
= absolute_section
;
2496 /* If the linker is doing the relaxing, we must not do any fixups.
2498 Well, strictly speaking that's not true -- we could do any that are
2499 PC-relative and don't cross regions that could change size. And for the
2500 i960 (the only machine for which we've got a relaxing linker right now),
2501 we might be able to turn callx/callj into bal anyways in cases where we
2502 know the maximum displacement. */
2503 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment_type
))
2505 for (; fixP
; fixP
= fixP
->fx_next
)
2507 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2508 return seg_reloc_count
;
2511 for (; fixP
; fixP
= fixP
->fx_next
)
2514 fprintf (stderr
, "\nprocessing fixup:\n");
2518 fragP
= fixP
->fx_frag
;
2520 where
= fixP
->fx_where
;
2521 place
= fragP
->fr_literal
+ where
;
2522 size
= fixP
->fx_size
;
2523 add_symbolP
= fixP
->fx_addsy
;
2524 #ifdef TC_VALIDATE_FIX
2525 TC_VALIDATE_FIX (fixP
, this_segment_type
, skip
);
2527 sub_symbolP
= fixP
->fx_subsy
;
2528 add_number
= fixP
->fx_offset
;
2529 pcrel
= fixP
->fx_pcrel
;
2532 if (add_symbolP
!= NULL
2533 && symbol_mri_common_p (add_symbolP
))
2535 know (add_symbolP
->sy_value
.X_op
== O_symbol
);
2536 add_number
+= S_GET_VALUE (add_symbolP
);
2537 fixP
->fx_offset
= add_number
;
2538 add_symbolP
= fixP
->fx_addsy
=
2539 symbol_get_value_expression (add_symbolP
)->X_add_symbol
;
2543 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2547 resolve_symbol_value (sub_symbolP
, 1);
2548 if (add_symbolP
== NULL
|| add_symbol_segment
== absolute_section
)
2550 if (add_symbolP
!= NULL
)
2552 add_number
+= S_GET_VALUE (add_symbolP
);
2554 fixP
->fx_addsy
= NULL
;
2557 /* It's just -sym. */
2558 if (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)
2560 add_number
-= S_GET_VALUE (sub_symbolP
);
2561 fixP
->fx_subsy
= NULL
;
2564 && S_GET_SEGMENT (sub_symbolP
) == this_segment_type
)
2566 /* Should try converting to a constant. */
2571 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2572 _("Negative of non-absolute symbol %s"),
2573 S_GET_NAME (sub_symbolP
));
2575 else if (S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
2576 && SEG_NORMAL (add_symbol_segment
))
2578 /* Difference of 2 symbols from same segment.
2579 Can't make difference of 2 undefineds: 'value' means
2580 something different for N_UNDF. */
2582 /* Makes no sense to use the difference of 2 arbitrary symbols
2583 as the target of a call instruction. */
2585 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2586 _("callj to difference of 2 symbols"));
2587 #endif /* TC_I960 */
2588 add_number
+= S_GET_VALUE (add_symbolP
) -
2589 S_GET_VALUE (sub_symbolP
);
2592 pcrel
= 0; /* No further pcrel processing. */
2594 /* Let the target machine make the final determination
2595 as to whether or not a relocation will be needed to
2596 handle this fixup. */
2597 if (!TC_FORCE_RELOCATION_SECTION (fixP
, this_segment_type
))
2600 fixP
->fx_addsy
= NULL
;
2601 fixP
->fx_subsy
= NULL
;
2606 /* Different segments in subtraction. */
2607 know (!(S_IS_EXTERNAL (sub_symbolP
)
2608 && (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)));
2610 if ((S_GET_SEGMENT (sub_symbolP
) == absolute_section
))
2611 add_number
-= S_GET_VALUE (sub_symbolP
);
2614 else if (S_GET_SEGMENT (sub_symbolP
) == this_segment_type
2616 /* Do this even if it's already described as
2617 pc-relative. For example, on the m68k, an
2618 operand of "pc@(foo-.-2)" should address
2619 "foo" in a pc-relative mode. */
2624 /* Make it pc-relative. */
2625 add_number
+= (MD_PCREL_FROM_SECTION (fixP
, this_segment_type
)
2626 - S_GET_VALUE (sub_symbolP
));
2633 #ifdef UNDEFINED_DIFFERENCE_OK
2634 /* The PA needs this for PIC code generation. We basically
2635 don't want to do anything if we have the difference of two
2636 symbols at this point. */
2639 /* Leave it alone. */
2642 #ifdef BFD_ASSEMBLER
2643 else if (fixP
->fx_r_type
== BFD_RELOC_GPREL32
2644 || fixP
->fx_r_type
== BFD_RELOC_GPREL16
)
2646 /* Leave it alone. */
2652 sprint_value (buf
, fragP
->fr_address
+ where
);
2653 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2654 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2655 S_GET_NAME (add_symbolP
),
2656 segment_name (S_GET_SEGMENT (add_symbolP
)),
2657 S_GET_NAME (sub_symbolP
),
2658 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2666 if (add_symbol_segment
== this_segment_type
&& pcrel
&& !plt
2667 && TC_RELOC_RTSYM_LOC_FIXUP (fixP
))
2669 /* This fixup was made when the symbol's segment was
2670 SEG_UNKNOWN, but it is now in the local segment.
2671 So we know how to do the address without relocation. */
2673 /* reloc_callj() may replace a 'call' with a 'calls' or a
2674 'bal', in which cases it modifies *fixP as appropriate.
2675 In the case of a 'calls', no further work is required,
2676 and *fixP has been set up to make the rest of the code
2679 #endif /* TC_I960 */
2681 add_number
+= S_GET_VALUE (add_symbolP
);
2682 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2683 /* Lie. Don't want further pcrel processing. */
2686 /* Let the target machine make the final determination
2687 as to whether or not a relocation will be needed to
2688 handle this fixup. */
2689 if (!TC_FORCE_RELOCATION (fixP
))
2692 fixP
->fx_addsy
= NULL
;
2697 if (add_symbol_segment
== absolute_section
2701 /* See comment about reloc_callj() above. */
2703 #endif /* TC_I960 */
2704 add_number
+= S_GET_VALUE (add_symbolP
);
2706 /* Let the target machine make the final determination
2707 as to whether or not a relocation will be needed to
2708 handle this fixup. */
2710 if (!TC_FORCE_RELOCATION (fixP
))
2712 fixP
->fx_addsy
= NULL
;
2716 else if (add_symbol_segment
== undefined_section
2717 #ifdef BFD_ASSEMBLER
2718 || bfd_is_com_section (add_symbol_segment
)
2723 if ((int) fixP
->fx_bit_fixP
== 13)
2725 /* This is a COBR instruction. They have only a
2726 13-bit displacement and are only to be used
2727 for local branches: flag as error, don't generate
2729 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2730 _("can't use COBR format with external label"));
2731 fixP
->fx_addsy
= NULL
;
2735 #endif /* TC_I960 */
2739 if (S_IS_COMMON (add_symbolP
))
2740 add_number
+= S_GET_VALUE (add_symbolP
);
2741 #endif /* TE_I386AIX */
2742 #endif /* OBJ_COFF */
2748 if (TC_FIX_ADJUSTABLE (fixP
))
2749 add_number
+= S_GET_VALUE (add_symbolP
);
2756 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2757 if (add_symbolP
== 0)
2759 #ifndef BFD_ASSEMBLER
2760 fixP
->fx_addsy
= &abs_symbol
;
2762 fixP
->fx_addsy
= section_symbol (absolute_section
);
2764 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2771 #ifdef MD_APPLY_FIX3
2772 md_apply_fix3 (fixP
, &add_number
, this_segment_type
);
2774 #ifdef BFD_ASSEMBLER
2775 md_apply_fix (fixP
, &add_number
);
2777 md_apply_fix (fixP
, add_number
);
2781 #ifndef TC_HANDLES_FX_DONE
2782 /* If the tc-* files haven't been converted, assume it's handling
2783 it the old way, where a null fx_addsy means that the fix has
2784 been applied completely, and no further work is needed. */
2785 if (fixP
->fx_addsy
== 0 && fixP
->fx_pcrel
== 0)
2790 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& size
> 0)
2792 if ((size_t) size
< sizeof (valueT
))
2797 mask
--; /* Set all bits to one. */
2798 mask
<<= size
* 8 - (fixP
->fx_signed
? 1 : 0);
2799 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
2801 char buf
[50], buf2
[50];
2802 sprint_value (buf
, fragP
->fr_address
+ where
);
2803 if (add_number
> 1000)
2804 sprint_value (buf2
, add_number
);
2806 sprintf (buf2
, "%ld", (long) add_number
);
2807 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2808 _("Value of %s too large for field of %d bytes at %s"),
2810 } /* Generic error checking. */
2812 #ifdef WARN_SIGNED_OVERFLOW_WORD
2813 /* Warn if a .word value is too large when treated as a signed
2814 number. We already know it is not too negative. This is to
2815 catch over-large switches generated by gcc on the 68k. */
2816 if (!flag_signed_overflow_ok
2818 && add_number
> 0x7fff)
2819 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2820 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2822 (unsigned long) (fragP
->fr_address
+ where
));
2824 } /* Not a bit fix. */
2826 #ifdef TC_VALIDATE_FIX
2827 skip
: ATTRIBUTE_UNUSED_LABEL
2831 fprintf (stderr
, "result:\n");
2834 } /* For each fixS in this segment. */
2836 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2837 return seg_reloc_count
;
2840 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2843 number_to_chars_bigendian (buf
, val
, n
)
2848 if ((size_t) n
> sizeof (val
) || n
<= 0)
2852 buf
[n
] = val
& 0xff;
2858 number_to_chars_littleendian (buf
, val
, n
)
2863 if ((size_t) n
> sizeof (val
) || n
<= 0)
2867 *buf
++ = val
& 0xff;
2873 write_print_statistics (file
)
2876 fprintf (file
, "fixups: %d\n", n_fixups
);
2879 /* For debugging. */
2880 extern int indent_level
;
2887 fprintf (stderr
, "fix %lx %s:%d", (long) fixp
, fixp
->fx_file
, fixp
->fx_line
);
2889 fprintf (stderr
, " pcrel");
2890 if (fixp
->fx_pcrel_adjust
)
2891 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2892 if (fixp
->fx_im_disp
)
2895 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2897 fprintf (stderr
, " im_disp");
2901 fprintf (stderr
, " tcbit");
2903 fprintf (stderr
, " done");
2904 fprintf (stderr
, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2905 fixp
->fx_size
, (long) fixp
->fx_frag
, (long) fixp
->fx_where
,
2906 (long) fixp
->fx_offset
, (long) fixp
->fx_addnumber
);
2907 #ifdef BFD_ASSEMBLER
2908 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2911 #ifdef NEED_FX_R_TYPE
2912 fprintf (stderr
, " r_type=%d", fixp
->fx_r_type
);
2917 fprintf (stderr
, "\n +<");
2918 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2919 fprintf (stderr
, ">");
2923 fprintf (stderr
, "\n -<");
2924 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2925 fprintf (stderr
, ">");
2927 fprintf (stderr
, "\n");
2928 #ifdef TC_FIX_DATA_PRINT
2929 TC_FIX_DATA_PRINT (stderr
, fixp
);