1 /* write.c - emit .o file
2 Copyright (C) 1986, 87, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This thing should be set up to do byteordering correctly. But... */
27 #include "output-file.h"
28 #include "dwarf2dbg.h"
30 /* This looks like a good idea. Let's try turning it on always, for now. */
31 #undef BFD_FAST_SECTION_FILL
32 #define BFD_FAST_SECTION_FILL
34 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
35 instruction so that the disassembler does not choke on it. */
37 #define NOP_OPCODE 0x00
40 #ifndef TC_ADJUST_RELOC_COUNT
41 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
44 #ifndef TC_FORCE_RELOCATION
45 #define TC_FORCE_RELOCATION(FIXP) 0
48 #ifndef TC_FORCE_RELOCATION_SECTION
49 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
52 #ifndef TC_LINKRELAX_FIXUP
53 #define TC_LINKRELAX_FIXUP(SEG) 1
56 #ifndef TC_FIX_ADJUSTABLE
57 #define TC_FIX_ADJUSTABLE(fix) 1
60 #ifndef MD_PCREL_FROM_SECTION
61 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
64 #ifndef WORKING_DOT_WORD
65 extern CONST
int md_short_jump_size
;
66 extern CONST
int md_long_jump_size
;
69 int symbol_table_frozen
;
70 void print_fixup
PARAMS ((fixS
*));
73 static void renumber_sections
PARAMS ((bfd
*, asection
*, PTR
));
75 /* We generally attach relocs to frag chains. However, after we have
76 chained these all together into a segment, any relocs we add after
77 that must be attached to a segment. This will include relocs added
78 in md_estimate_size_for_relax, for example. */
79 static int frags_chained
= 0;
85 struct frag
*text_frag_root
;
86 struct frag
*data_frag_root
;
87 struct frag
*bss_frag_root
;
89 struct frag
*text_last_frag
; /* Last frag in segment. */
90 struct frag
*data_last_frag
; /* Last frag in segment. */
91 static struct frag
*bss_last_frag
; /* Last frag in segment. */
95 static object_headers headers
;
98 long string_byte_count
;
99 char *next_object_file_charP
; /* Tracks object file bytes. */
102 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
105 #endif /* BFD_ASSEMBLER */
110 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
111 symbolS
*add
, symbolS
*sub
,
112 offsetT offset
, int pcrel
,
113 bfd_reloc_code_real_type r_type
));
115 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
116 symbolS
*add
, symbolS
*sub
,
117 offsetT offset
, int pcrel
,
120 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
121 static long fixup_segment
PARAMS ((fixS
* fixP
, segT this_segment_type
));
123 static relax_addressT relax_align
PARAMS ((relax_addressT addr
, int align
));
124 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
125 static fragS
*chain_frchains_together_1
PARAMS ((segT
, struct frchain
*));
128 static void chain_frchains_together
PARAMS ((bfd
*, segT
, PTR
));
129 static void cvt_frag_to_fill
PARAMS ((segT
, fragS
*));
130 static void relax_and_size_seg
PARAMS ((bfd
*, asection
*, PTR
));
131 static void adjust_reloc_syms
PARAMS ((bfd
*, asection
*, PTR
));
132 static void write_relocs
PARAMS ((bfd
*, asection
*, PTR
));
133 static void write_contents
PARAMS ((bfd
*, asection
*, PTR
));
134 static void set_symtab
PARAMS ((void));
136 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
137 static void merge_data_into_text
PARAMS ((void));
139 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
140 static void cvt_frag_to_fill
PARAMS ((object_headers
*, segT
, fragS
*));
141 static void remove_subsegs
PARAMS ((frchainS
*, int, fragS
**, fragS
**));
142 static void relax_and_size_all_segments
PARAMS ((void));
144 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
145 static void set_segment_vma
PARAMS ((bfd
*, asection
*, PTR
));
148 /* Create a fixS in obstack 'notes'. */
151 fix_new_internal (frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
,
153 fragS
*frag
; /* Which frag? */
154 int where
; /* Where in that frag? */
155 int size
; /* 1, 2, or 4 usually. */
156 symbolS
*add_symbol
; /* X_add_symbol. */
157 symbolS
*sub_symbol
; /* X_op_symbol. */
158 offsetT offset
; /* X_add_number. */
159 int pcrel
; /* TRUE if PC-relative relocation. */
161 bfd_reloc_code_real_type r_type
; /* Relocation type. */
163 int r_type
; /* Relocation type. */
170 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
172 fixP
->fx_frag
= frag
;
173 fixP
->fx_where
= where
;
174 fixP
->fx_size
= size
;
175 /* We've made fx_size a narrow field; check that it's wide enough. */
176 if (fixP
->fx_size
!= size
)
178 as_bad (_("field fx_size too small to hold %d"), size
);
181 fixP
->fx_addsy
= add_symbol
;
182 fixP
->fx_subsy
= sub_symbol
;
183 fixP
->fx_offset
= offset
;
184 fixP
->fx_pcrel
= pcrel
;
186 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
187 fixP
->fx_r_type
= r_type
;
189 fixP
->fx_im_disp
= 0;
190 fixP
->fx_pcrel_adjust
= 0;
191 fixP
->fx_bit_fixP
= 0;
192 fixP
->fx_addnumber
= 0;
195 fixP
->fx_no_overflow
= 0;
199 fixP
->fx_cgen
.insn
= NULL
;
200 fixP
->fx_cgen
.opinfo
= 0;
204 TC_INIT_FIX_DATA (fixP
);
207 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
209 /* Usually, we want relocs sorted numerically, but while
210 comparing to older versions of gas that have relocs
211 reverse sorted, it is convenient to have this compile
212 time option. xoxorich. */
216 fixS
**seg_fix_rootP
= (frags_chained
217 ? &seg_info (now_seg
)->fix_root
218 : &frchain_now
->fix_root
);
219 fixS
**seg_fix_tailP
= (frags_chained
220 ? &seg_info (now_seg
)->fix_tail
221 : &frchain_now
->fix_tail
);
224 #ifdef REVERSE_SORT_RELOCS
226 fixP
->fx_next
= *seg_fix_rootP
;
227 *seg_fix_rootP
= fixP
;
229 #else /* REVERSE_SORT_RELOCS */
231 fixP
->fx_next
= NULL
;
234 (*seg_fix_tailP
)->fx_next
= fixP
;
236 *seg_fix_rootP
= fixP
;
237 *seg_fix_tailP
= fixP
;
239 #endif /* REVERSE_SORT_RELOCS */
245 /* Create a fixup relative to a symbol (plus a constant). */
248 fix_new (frag
, where
, size
, add_symbol
, offset
, pcrel
, r_type
)
249 fragS
*frag
; /* Which frag? */
250 int where
; /* Where in that frag? */
251 int size
; /* 1, 2, or 4 usually. */
252 symbolS
*add_symbol
; /* X_add_symbol. */
253 offsetT offset
; /* X_add_number. */
254 int pcrel
; /* TRUE if PC-relative relocation. */
256 bfd_reloc_code_real_type r_type
; /* Relocation type. */
258 int r_type
; /* Relocation type. */
261 return fix_new_internal (frag
, where
, size
, add_symbol
,
262 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
265 /* Create a fixup for an expression. Currently we only support fixups
266 for difference expressions. That is itself more than most object
267 file formats support anyhow. */
270 fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
)
271 fragS
*frag
; /* Which frag? */
272 int where
; /* Where in that frag? */
273 int size
; /* 1, 2, or 4 usually. */
274 expressionS
*exp
; /* Expression. */
275 int pcrel
; /* TRUE if PC-relative relocation. */
277 bfd_reloc_code_real_type r_type
; /* Relocation type. */
279 int r_type
; /* Relocation type. */
292 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
293 the difference expression cannot immediately be reduced. */
295 symbolS
*stmp
= make_expr_symbol (exp
);
297 exp
->X_op
= O_symbol
;
298 exp
->X_op_symbol
= 0;
299 exp
->X_add_symbol
= stmp
;
300 exp
->X_add_number
= 0;
302 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
306 add
= exp
->X_add_symbol
;
307 off
= exp
->X_add_number
;
309 #if defined(BFD_ASSEMBLER)
310 r_type
= BFD_RELOC_RVA
;
312 #if defined(TC_RVA_RELOC)
313 r_type
= TC_RVA_RELOC
;
315 as_fatal (_("rva not supported"));
321 sub
= exp
->X_add_symbol
;
322 off
= exp
->X_add_number
;
326 sub
= exp
->X_op_symbol
;
329 add
= exp
->X_add_symbol
;
332 off
= exp
->X_add_number
;
336 add
= make_expr_symbol (exp
);
340 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
, r_type
);
343 /* Append a string onto another string, bumping the pointer along. */
345 append (charPP
, fromP
, length
)
348 unsigned long length
;
350 /* Don't trust memcpy() of 0 chars. */
354 memcpy (*charPP
, fromP
, length
);
358 #ifndef BFD_ASSEMBLER
359 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
362 /* This routine records the largest alignment seen for each segment.
363 If the beginning of the segment is aligned on the worst-case
364 boundary, all of the other alignments within it will work. At
365 least one object format really uses this info. */
368 record_alignment (seg
, align
)
369 /* Segment to which alignment pertains. */
371 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
375 if (seg
== absolute_section
)
378 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
379 bfd_set_section_alignment (stdoutput
, seg
, align
);
381 if (align
> section_alignment
[(int) seg
])
382 section_alignment
[(int) seg
] = align
;
388 /* Reset the section indices after removing the gas created sections. */
391 renumber_sections (abfd
, sec
, countparg
)
392 bfd
*abfd ATTRIBUTE_UNUSED
;
396 int *countp
= (int *) countparg
;
398 sec
->index
= *countp
;
402 #endif /* defined (BFD_ASSEMBLER) */
404 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
407 chain_frchains_together_1 (section
, frchp
)
409 struct frchain
*frchp
;
411 fragS dummy
, *prev_frag
= &dummy
;
413 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
416 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
418 prev_frag
->fr_next
= frchp
->frch_root
;
419 prev_frag
= frchp
->frch_last
;
420 assert (prev_frag
->fr_type
!= 0);
422 if (frchp
->fix_root
!= (fixS
*) NULL
)
424 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
425 seg_info (section
)->fix_root
= frchp
->fix_root
;
426 prev_fix
->fx_next
= frchp
->fix_root
;
427 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
428 prev_fix
= frchp
->fix_tail
;
432 assert (prev_frag
->fr_type
!= 0);
433 prev_frag
->fr_next
= 0;
442 chain_frchains_together (abfd
, section
, xxx
)
443 bfd
*abfd ATTRIBUTE_UNUSED
;
445 PTR xxx ATTRIBUTE_UNUSED
;
447 segment_info_type
*info
;
449 /* BFD may have introduced its own sections without using
450 subseg_new, so it is possible that seg_info is NULL. */
451 info
= seg_info (section
);
452 if (info
!= (segment_info_type
*) NULL
)
453 info
->frchainP
->frch_last
454 = chain_frchains_together_1 (section
, info
->frchainP
);
456 /* Now that we've chained the frags together, we must add new fixups
457 to the segment, not to the frag chain. */
463 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
466 remove_subsegs (head
, seg
, root
, last
)
472 *root
= head
->frch_root
;
473 *last
= chain_frchains_together_1 (seg
, head
);
478 #if defined (BFD_ASSEMBLER) || !defined (BFD)
482 cvt_frag_to_fill (sec
, fragP
)
483 segT sec ATTRIBUTE_UNUSED
;
487 cvt_frag_to_fill (headersP
, sec
, fragP
)
488 object_headers
*headersP
;
493 switch (fragP
->fr_type
)
500 HANDLE_ALIGN (fragP
);
502 know (fragP
->fr_next
!= NULL
);
503 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
505 - fragP
->fr_fix
) / fragP
->fr_var
;
506 if (fragP
->fr_offset
< 0)
508 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
509 _("attempt to .org/.space backwards? (%ld)"),
510 (long) fragP
->fr_offset
);
512 fragP
->fr_type
= rs_fill
;
520 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
523 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
526 fragP
->fr_fix
+= size
;
527 fragP
->fr_type
= rs_fill
;
529 fragP
->fr_offset
= 0;
530 fragP
->fr_symbol
= NULL
;
535 eh_frame_convert_frag (fragP
);
539 dwarf2dbg_convert_frag (fragP
);
542 case rs_machine_dependent
:
544 md_convert_frag (stdoutput
, sec
, fragP
);
546 md_convert_frag (headersP
, sec
, fragP
);
549 assert (fragP
->fr_next
== NULL
550 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
553 /* After md_convert_frag, we make the frag into a ".space 0".
554 md_convert_frag() should set up any fixSs and constants
559 #ifndef WORKING_DOT_WORD
562 struct broken_word
*lie
;
564 if (fragP
->fr_subtype
)
566 fragP
->fr_fix
+= md_short_jump_size
;
567 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
568 lie
&& lie
->dispfrag
== fragP
;
569 lie
= lie
->next_broken_word
)
571 fragP
->fr_fix
+= md_long_jump_size
;
579 BAD_CASE (fragP
->fr_type
);
584 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
588 relax_and_size_seg (abfd
, sec
, xxx
)
591 PTR xxx ATTRIBUTE_UNUSED
;
595 segment_info_type
*seginfo
;
597 valueT size
, newsize
;
599 subseg_change (sec
, 0);
601 flags
= bfd_get_section_flags (abfd
, sec
);
603 seginfo
= seg_info (sec
);
604 if (seginfo
&& seginfo
->frchainP
)
606 relax_segment (seginfo
->frchainP
->frch_root
, sec
);
607 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
608 cvt_frag_to_fill (sec
, fragp
);
609 for (fragp
= seginfo
->frchainP
->frch_root
;
611 fragp
= fragp
->fr_next
)
612 /* Walk to last elt. */
614 size
= fragp
->fr_address
+ fragp
->fr_fix
;
619 if (size
> 0 && ! seginfo
->bss
)
620 flags
|= SEC_HAS_CONTENTS
;
622 /* @@ This is just an approximation. */
623 if (seginfo
&& seginfo
->fix_root
)
627 x
= bfd_set_section_flags (abfd
, sec
, flags
);
630 newsize
= md_section_align (sec
, size
);
631 x
= bfd_set_section_size (abfd
, sec
, newsize
);
634 /* If the size had to be rounded up, add some padding in the last
636 assert (newsize
>= size
);
639 fragS
*last
= seginfo
->frchainP
->frch_last
;
640 fragp
= seginfo
->frchainP
->frch_root
;
641 while (fragp
->fr_next
!= last
)
642 fragp
= fragp
->fr_next
;
643 last
->fr_address
= size
;
644 fragp
->fr_offset
+= newsize
- size
;
647 #ifdef tc_frob_section
648 tc_frob_section (sec
);
650 #ifdef obj_frob_section
651 obj_frob_section (sec
);
657 dump_section_relocs (abfd
, sec
, stream_
)
658 bfd
*abfd ATTRIBUTE_UNUSED
;
662 FILE *stream
= (FILE *) stream_
;
663 segment_info_type
*seginfo
= seg_info (sec
);
664 fixS
*fixp
= seginfo
->fix_root
;
669 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
672 symbolS
*s
= fixp
->fx_addsy
;
674 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
675 (int) fixp
->fx_r_type
);
677 fprintf (stream
, "no sym\n");
680 print_symbol_value_1 (stream
, s
);
681 fprintf (stream
, "\n");
683 fixp
= fixp
->fx_next
;
687 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
690 #ifndef EMIT_SECTION_SYMBOLS
691 #define EMIT_SECTION_SYMBOLS 1
695 adjust_reloc_syms (abfd
, sec
, xxx
)
696 bfd
*abfd ATTRIBUTE_UNUSED
;
698 PTR xxx ATTRIBUTE_UNUSED
;
700 segment_info_type
*seginfo
= seg_info (sec
);
706 dump_section_relocs (abfd
, sec
, stderr
);
708 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
712 else if (fixp
->fx_addsy
)
718 fprintf (stderr
, "\n\nadjusting fixup:\n");
722 sym
= fixp
->fx_addsy
;
724 /* All symbols should have already been resolved at this
725 point. It is possible to see unresolved expression
726 symbols, though, since they are not in the regular symbol
729 resolve_symbol_value (sym
, 1);
731 if (fixp
->fx_subsy
!= NULL
)
732 resolve_symbol_value (fixp
->fx_subsy
, 1);
734 /* If this symbol is equated to an undefined symbol, convert
735 the fixup to being against that symbol. */
736 if (sym
!= NULL
&& symbol_equated_p (sym
)
737 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
739 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
740 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
741 fixp
->fx_addsy
= sym
;
744 if (sym
!= NULL
&& symbol_mri_common_p (sym
))
746 /* These symbols are handled specially in fixup_segment. */
750 symsec
= S_GET_SEGMENT (sym
);
755 if (bfd_is_abs_section (symsec
))
757 /* The fixup_segment routine will not use this symbol in a
758 relocation unless TC_FORCE_RELOCATION returns 1. */
759 if (TC_FORCE_RELOCATION (fixp
))
761 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
762 #ifdef UNDEFINED_DIFFERENCE_OK
763 if (fixp
->fx_subsy
!= NULL
)
764 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
770 /* If it's one of these sections, assume the symbol is
771 definitely going to be output. The code in
772 md_estimate_size_before_relax in tc-mips.c uses this test
773 as well, so if you change this code you should look at that
775 if (bfd_is_und_section (symsec
)
776 || bfd_is_com_section (symsec
))
778 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
779 #ifdef UNDEFINED_DIFFERENCE_OK
780 /* We have the difference of an undefined symbol and some
781 other symbol. Make sure to mark the other symbol as used
782 in a relocation so that it will always be output. */
784 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
789 /* Don't try to reduce relocs which refer to non-local symbols
790 in .linkonce sections. It can lead to confusion when a
791 debugging section refers to a .linkonce section. I hope
792 this will always be correct. */
793 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
799 if ((bfd_get_section_flags (stdoutput
, symsec
) & SEC_LINK_ONCE
)
804 /* The GNU toolchain uses an extension for ELF: a section
805 beginning with the magic string .gnu.linkonce is a
807 if (strncmp (segment_name (symsec
), ".gnu.linkonce",
808 sizeof ".gnu.linkonce" - 1) == 0)
814 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
815 #ifdef UNDEFINED_DIFFERENCE_OK
816 if (fixp
->fx_subsy
!= NULL
)
817 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
823 /* Since we're reducing to section symbols, don't attempt to reduce
824 anything that's already using one. */
825 if (symbol_section_p (sym
))
827 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
832 /* We can never adjust a reloc against a weak symbol. If we
833 did, and the weak symbol was overridden by a real symbol
834 somewhere else, then our relocation would be pointing at
835 the wrong area of memory. */
838 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
843 /* Is there some other reason we can't adjust this one? (E.g.,
844 call/bal links in i960-bout symbols.) */
845 #ifdef obj_fix_adjustable
846 if (! obj_fix_adjustable (fixp
))
848 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
853 /* Is there some other (target cpu dependent) reason we can't adjust
854 this one? (E.g. relocations involving function addresses on
856 #ifdef tc_fix_adjustable
857 if (! tc_fix_adjustable (fixp
))
859 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
864 /* If the section symbol isn't going to be output, the relocs
865 at least should still work. If not, figure out what to do
866 when we run into that case.
868 We refetch the segment when calling section_symbol, rather
869 than using symsec, because S_GET_VALUE may wind up changing
870 the section when it calls resolve_symbol_value. */
871 fixp
->fx_offset
+= S_GET_VALUE (sym
);
872 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
873 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
875 fprintf (stderr
, "\nadjusted fixup:\n");
882 #if 1 /* def RELOC_REQUIRES_SYMBOL */
885 /* There was no symbol required by this relocation. However,
886 BFD doesn't really handle relocations without symbols well.
887 (At least, the COFF support doesn't.) So for now we fake up
888 a local symbol in the absolute section. */
890 fixp
->fx_addsy
= section_symbol (absolute_section
);
892 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
897 dump_section_relocs (abfd
, sec
, stderr
);
901 write_relocs (abfd
, sec
, xxx
)
904 PTR xxx ATTRIBUTE_UNUSED
;
906 segment_info_type
*seginfo
= seg_info (sec
);
913 /* If seginfo is NULL, we did not create this section; don't do
918 fixup_segment (seginfo
->fix_root
, sec
);
921 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
924 #ifndef RELOC_EXPANSION_POSSIBLE
925 /* Set up reloc information as well. */
926 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
927 memset ((char *) relocs
, 0, n
* sizeof (arelent
*));
930 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
933 bfd_reloc_status_type s
;
942 /* If this is an undefined symbol which was equated to another
943 symbol, then use generate the reloc against the latter symbol
944 rather than the former. */
945 sym
= fixp
->fx_addsy
;
946 while (symbol_equated_p (sym
)
947 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
951 /* We must avoid looping, as that can occur with a badly
953 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
956 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
959 fixp
->fx_addsy
= sym
;
961 reloc
= tc_gen_reloc (sec
, fixp
);
969 /* This test is triggered inappropriately for the SH. */
970 if (fixp
->fx_where
+ fixp
->fx_size
971 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
975 s
= bfd_install_relocation (stdoutput
, reloc
,
976 fixp
->fx_frag
->fr_literal
,
977 fixp
->fx_frag
->fr_address
,
983 case bfd_reloc_overflow
:
984 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation overflow"));
986 case bfd_reloc_outofrange
:
987 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation out of range"));
990 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
991 fixp
->fx_file
, fixp
->fx_line
, s
);
996 n
= n
* MAX_RELOC_EXPANSION
;
997 /* Set up reloc information as well. */
998 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
1001 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1005 bfd_reloc_status_type s
;
1015 /* If this is an undefined symbol which was equated to another
1016 symbol, then generate the reloc against the latter symbol
1017 rather than the former. */
1018 sym
= fixp
->fx_addsy
;
1019 while (symbol_equated_p (sym
)
1020 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
1021 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
1022 fixp
->fx_addsy
= sym
;
1024 reloc
= tc_gen_reloc (sec
, fixp
);
1026 for (j
= 0; reloc
[j
]; j
++)
1028 relocs
[i
++] = reloc
[j
];
1031 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1032 if (fixp
->fx_where
+ fixp
->fx_size
1033 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1034 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1035 _("internal error: fixup not contained within frag"));
1036 for (j
= 0; reloc
[j
]; j
++)
1038 s
= bfd_install_relocation (stdoutput
, reloc
[j
],
1039 fixp
->fx_frag
->fr_literal
,
1040 fixp
->fx_frag
->fr_address
,
1046 case bfd_reloc_overflow
:
1047 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1048 _("relocation overflow"));
1051 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1052 fixp
->fx_file
, fixp
->fx_line
);
1063 sympp
= bfd_get_outsymbols (stdoutput
);
1064 nsyms
= bfd_get_symcount (stdoutput
);
1065 for (i
= 0; i
< n
; i
++)
1066 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1068 for (j
= 0; j
< nsyms
; j
++)
1069 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
1078 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1080 bfd_set_section_flags (abfd
, sec
,
1081 (bfd_get_section_flags (abfd
, sec
)
1082 & (flagword
) ~SEC_RELOC
));
1084 #ifdef SET_SECTION_RELOCS
1085 SET_SECTION_RELOCS (sec
, relocs
, n
);
1093 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1094 for (i
= 0; i
< n
; i
++)
1097 s
= *r
->sym_ptr_ptr
;
1098 fprintf (stderr
, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1099 i
, r
, r
->address
, s
->name
, r
->addend
);
1106 write_contents (abfd
, sec
, xxx
)
1107 bfd
*abfd ATTRIBUTE_UNUSED
;
1109 PTR xxx ATTRIBUTE_UNUSED
;
1111 segment_info_type
*seginfo
= seg_info (sec
);
1112 unsigned long offset
= 0;
1115 /* Write out the frags. */
1117 || !(bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1120 for (f
= seginfo
->frchainP
->frch_root
;
1125 unsigned long fill_size
;
1129 assert (f
->fr_type
== rs_fill
);
1132 x
= bfd_set_section_contents (stdoutput
, sec
,
1133 f
->fr_literal
, (file_ptr
) offset
,
1134 (bfd_size_type
) f
->fr_fix
);
1137 bfd_perror (stdoutput
->filename
);
1138 as_perror (_("FATAL: Can't write %s"), stdoutput
->filename
);
1139 exit (EXIT_FAILURE
);
1141 offset
+= f
->fr_fix
;
1143 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1144 fill_size
= f
->fr_var
;
1145 count
= f
->fr_offset
;
1146 assert (count
>= 0);
1147 if (fill_size
&& count
)
1150 if (fill_size
> sizeof (buf
))
1152 /* Do it the old way. Can this ever happen? */
1155 x
= bfd_set_section_contents (stdoutput
, sec
,
1158 (bfd_size_type
) fill_size
);
1161 bfd_perror (stdoutput
->filename
);
1162 as_perror (_("FATAL: Can't write %s"),
1163 stdoutput
->filename
);
1164 exit (EXIT_FAILURE
);
1166 offset
+= fill_size
;
1171 /* Build a buffer full of fill objects and output it as
1172 often as necessary. This saves on the overhead of
1173 potentially lots of bfd_set_section_contents calls. */
1177 n_per_buf
= sizeof (buf
);
1178 memset (buf
, *fill_literal
, n_per_buf
);
1183 n_per_buf
= sizeof (buf
) / fill_size
;
1184 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1185 memcpy (bufp
, fill_literal
, fill_size
);
1187 for (; count
> 0; count
-= n_per_buf
)
1189 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1190 x
= bfd_set_section_contents
1191 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1192 (bfd_size_type
) n_per_buf
* fill_size
);
1194 as_fatal (_("Cannot write to output file."));
1195 offset
+= n_per_buf
* fill_size
;
1203 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1205 merge_data_into_text ()
1207 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1208 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1209 seg_info (data_section
)->frchainP
->frch_root
;
1210 seg_info (text_section
)->frchainP
->frch_last
=
1211 seg_info (data_section
)->frchainP
->frch_last
;
1212 seg_info (data_section
)->frchainP
= 0;
1216 text_last_frag
->fr_next
= data_frag_root
;
1217 text_last_frag
= data_last_frag
;
1218 data_last_frag
= NULL
;
1219 data_frag_root
= NULL
;
1222 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
1223 tmp
->fx_next
= data_fix_root
;
1224 text_fix_tail
= data_fix_tail
;
1227 text_fix_root
= data_fix_root
;
1228 data_fix_root
= NULL
;
1231 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1233 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1235 relax_and_size_all_segments ()
1239 relax_segment (text_frag_root
, SEG_TEXT
);
1240 relax_segment (data_frag_root
, SEG_DATA
);
1241 relax_segment (bss_frag_root
, SEG_BSS
);
1243 /* Now the addresses of frags are correct within the segment. */
1244 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
1245 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
1246 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
1248 /* Join the 2 segments into 1 huge segment.
1249 To do this, re-compute every rn_address in the SEG_DATA frags.
1250 Then join the data frags after the text frags.
1252 Determine a_data [length of data segment]. */
1255 register relax_addressT slide
;
1257 know ((text_last_frag
->fr_type
== rs_fill
)
1258 && (text_last_frag
->fr_offset
== 0));
1260 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
1261 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
1262 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
1264 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1265 /* For b.out: If the data section has a strict alignment
1266 requirement, its load address in the .o file will be
1267 rounded up from the size of the text section. These
1268 two values are *not* the same! Similarly for the bss
1270 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
1273 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1274 fragP
->fr_address
+= slide
;
1276 know (text_last_frag
!= 0);
1277 text_last_frag
->fr_next
= data_frag_root
;
1281 H_SET_DATA_SIZE (&headers
, 0);
1285 /* See above comments on b.out data section address. */
1288 if (data_last_frag
== 0)
1289 bss_vma
= H_GET_TEXT_SIZE (&headers
);
1291 bss_vma
= data_last_frag
->fr_address
;
1292 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
1293 bss_address_frag
.fr_address
= bss_vma
;
1295 #else /* ! OBJ_BOUT */
1296 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
1297 H_GET_DATA_SIZE (&headers
));
1299 #endif /* ! OBJ_BOUT */
1301 /* Slide all the frags. */
1304 relax_addressT slide
= bss_address_frag
.fr_address
;
1306 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1307 fragP
->fr_address
+= slide
;
1311 H_SET_BSS_SIZE (&headers
,
1312 bss_last_frag
->fr_address
- bss_frag_root
->fr_address
);
1314 H_SET_BSS_SIZE (&headers
, 0);
1316 #endif /* ! BFD_ASSEMBLER && ! BFD */
1318 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1320 #ifdef BFD_ASSEMBLER
1328 extern PTR bfd_alloc
PARAMS ((bfd
*, size_t));
1330 /* Count symbols. We can't rely on a count made by the loop in
1331 write_object_file, because *_frob_file may add a new symbol or
1334 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1341 asympp
= (asymbol
**) bfd_alloc (stdoutput
,
1342 nsyms
* sizeof (asymbol
*));
1343 symp
= symbol_rootP
;
1344 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1346 asympp
[i
] = symbol_get_bfdsym (symp
);
1347 symbol_mark_written (symp
);
1352 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1353 assert (result
== true);
1354 symbol_table_frozen
= 1;
1358 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1360 set_segment_vma (abfd
, sec
, xxx
)
1363 PTR xxx ATTRIBUTE_UNUSED
;
1365 static bfd_vma addr
= 0;
1367 bfd_set_section_vma (abfd
, sec
, addr
);
1368 addr
+= bfd_section_size (abfd
, sec
);
1370 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1372 /* Finish the subsegments. After every sub-segment, we fake an
1373 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1374 ".fill 0" because that is the kind of frag that requires least
1375 thought. ".align" frags like to have a following frag since that
1376 makes calculating their intended length trivial. */
1378 #ifndef SUB_SEGMENT_ALIGN
1379 #ifdef BFD_ASSEMBLER
1380 #define SUB_SEGMENT_ALIGN(SEG) (0)
1382 #define SUB_SEGMENT_ALIGN(SEG) (2)
1389 struct frchain
*frchainP
;
1391 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
1393 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
1395 /* This now gets called even if we had errors. In that case,
1396 any alignment is meaningless, and, moreover, will look weird
1397 if we are generating a listing. */
1398 frag_align (had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg
),
1399 subseg_text_p (now_seg
) ? NOP_OPCODE
: 0,
1402 /* frag_align will have left a new frag.
1403 Use this last frag for an empty ".fill".
1405 For this segment ...
1406 Create a last frag. Do not leave a "being filled in frag". */
1407 frag_wane (frag_now
);
1408 frag_now
->fr_fix
= 0;
1409 know (frag_now
->fr_next
== NULL
);
1413 /* Write the object file. */
1416 write_object_file ()
1418 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1419 fragS
*fragP
; /* Track along all frags. */
1422 /* Do we really want to write it? */
1424 int n_warns
, n_errs
;
1425 n_warns
= had_warnings ();
1426 n_errs
= had_errors ();
1427 /* The -Z flag indicates that an object file should be generated,
1428 regardless of warnings and errors. */
1429 if (flag_always_generate_output
)
1431 if (n_warns
|| n_errs
)
1432 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1433 n_errs
, n_errs
== 1 ? "" : "s",
1434 n_warns
, n_warns
== 1 ? "" : "s");
1439 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1440 n_errs
, n_errs
== 1 ? "" : "s",
1441 n_warns
, n_warns
== 1 ? "" : "s");
1446 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1447 a routine to check for the definition of the procedure "_main",
1448 and if so -- fix it up so that it can be program entry point. */
1449 vms_check_for_main ();
1450 #endif /* OBJ_VMS */
1452 /* From now on, we don't care about sub-segments. Build one frag chain
1453 for each segment. Linked thru fr_next. */
1455 #ifdef BFD_ASSEMBLER
1456 /* Remove the sections created by gas for its own purposes. */
1458 asection
**seclist
, *sec
;
1461 seclist
= &stdoutput
->sections
;
1462 while (seclist
&& *seclist
)
1465 while (sec
== reg_section
|| sec
== expr_section
)
1469 stdoutput
->section_count
--;
1474 seclist
= &(*seclist
)->next
;
1477 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1480 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1482 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
1483 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
1484 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
1487 /* We have two segments. If user gave -R flag, then we must put the
1488 data frags into the text segment. Do this before relaxing so
1489 we know to take advantage of -R and make shorter addresses. */
1490 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1491 if (flag_readonly_data_in_text
)
1493 merge_data_into_text ();
1497 #ifdef BFD_ASSEMBLER
1498 bfd_map_over_sections (stdoutput
, relax_and_size_seg
, (char *) 0);
1500 relax_and_size_all_segments ();
1501 #endif /* BFD_ASSEMBLER */
1503 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1504 /* Now that the segments have their final sizes, run through the
1505 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1506 should too. Currently, only DJGPP uses this code, but other
1507 COFF targets may need to execute this too. */
1508 bfd_map_over_sections (stdoutput
, set_segment_vma
, (char *) 0);
1511 #ifndef BFD_ASSEMBLER
1512 /* Crawl the symbol chain.
1514 For each symbol whose value depends on a frag, take the address of
1515 that frag and subsume it into the value of the symbol.
1516 After this, there is just one way to lookup a symbol value.
1517 Values are left in their final state for object file emission.
1518 We adjust the values of 'L' local symbols, even if we do
1519 not intend to emit them to the object file, because their values
1520 are needed for fix-ups.
1522 Unless we saw a -L flag, remove all symbols that begin with 'L'
1523 from the symbol chain. (They are still pointed to by the fixes.)
1525 Count the remaining symbols.
1526 Assign a symbol number to each symbol.
1527 Count the number of string-table chars we will emit.
1528 Put this info into the headers as appropriate. */
1529 know (zero_address_frag
.fr_address
== 0);
1530 string_byte_count
= sizeof (string_byte_count
);
1532 obj_crawl_symbol_chain (&headers
);
1534 if (string_byte_count
== sizeof (string_byte_count
))
1535 string_byte_count
= 0;
1537 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1539 /* Addresses of frags now reflect addresses we use in the object file.
1540 Symbol values are correct.
1541 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1542 Also converting any machine-dependent frags using md_convert_frag(); */
1543 subseg_change (SEG_TEXT
, 0);
1545 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1547 /* At this point we have linked all the frags into a single
1548 chain. However, cvt_frag_to_fill may call md_convert_frag
1549 which may call fix_new. We need to ensure that fix_new adds
1550 the fixup to the right section. */
1551 if (fragP
== data_frag_root
)
1552 subseg_change (SEG_DATA
, 0);
1554 cvt_frag_to_fill (&headers
, SEG_TEXT
, fragP
);
1556 /* Some assert macros don't work with # directives mixed in. */
1558 if (!(fragP
->fr_next
== NULL
1560 || fragP
->fr_next
== data_frag_root
1562 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
1563 == (fragP
->fr_fix
+ fragP
->fr_offset
* fragP
->fr_var
))))
1567 #endif /* ! BFD_ASSEMBLER */
1569 #ifndef WORKING_DOT_WORD
1571 struct broken_word
*lie
;
1572 struct broken_word
**prevP
;
1574 prevP
= &broken_words
;
1575 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1580 subseg_change (lie
->seg
, lie
->subseg
);
1581 exp
.X_op
= O_subtract
;
1582 exp
.X_add_symbol
= lie
->add
;
1583 exp
.X_op_symbol
= lie
->sub
;
1584 exp
.X_add_number
= lie
->addnum
;
1585 #ifdef BFD_ASSEMBLER
1586 #ifdef TC_CONS_FIX_NEW
1587 TC_CONS_FIX_NEW (lie
->frag
,
1588 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1591 fix_new_exp (lie
->frag
,
1592 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1593 2, &exp
, 0, BFD_RELOC_16
);
1596 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1597 fix_new_exp (lie
->frag
,
1598 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1599 2, &exp
, 0, NO_RELOC
);
1602 fix_new_ns32k_exp (lie
->frag
,
1603 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1604 2, &exp
, 0, 0, 2, 0, 0);
1606 fix_new_exp (lie
->frag
,
1607 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1609 #endif /* TC_NS32K */
1610 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1611 #endif /* BFD_ASSEMBLER */
1612 *prevP
= lie
->next_broken_word
;
1615 prevP
= &(lie
->next_broken_word
);
1617 for (lie
= broken_words
; lie
;)
1619 struct broken_word
*untruth
;
1621 addressT table_addr
;
1622 addressT from_addr
, to_addr
;
1625 subseg_change (lie
->seg
, lie
->subseg
);
1626 fragP
= lie
->dispfrag
;
1628 /* Find out how many broken_words go here. */
1631 untruth
&& untruth
->dispfrag
== fragP
;
1632 untruth
= untruth
->next_broken_word
)
1633 if (untruth
->added
== 1)
1636 table_ptr
= lie
->dispfrag
->fr_opcode
;
1637 table_addr
= (lie
->dispfrag
->fr_address
1638 + (table_ptr
- lie
->dispfrag
->fr_literal
));
1639 /* Create the jump around the long jumps. This is a short
1640 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1641 from_addr
= table_addr
;
1642 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1643 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1645 table_ptr
+= md_short_jump_size
;
1646 table_addr
+= md_short_jump_size
;
1649 lie
&& lie
->dispfrag
== fragP
;
1650 m
++, lie
= lie
->next_broken_word
)
1652 if (lie
->added
== 2)
1654 /* Patch the jump table. */
1655 /* This is the offset from ??? to table_ptr+0. */
1656 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1657 #ifdef BFD_ASSEMBLER
1658 to_addr
-= symbol_get_frag (lie
->sub
)->fr_address
;
1660 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1661 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr
, lie
);
1663 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1664 for (untruth
= lie
->next_broken_word
;
1665 untruth
&& untruth
->dispfrag
== fragP
;
1666 untruth
= untruth
->next_broken_word
)
1668 if (untruth
->use_jump
== lie
)
1669 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1672 /* Install the long jump. */
1673 /* This is a long jump from table_ptr+0 to the final target. */
1674 from_addr
= table_addr
;
1675 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1676 #ifdef BFD_ASSEMBLER
1677 to_addr
+= symbol_get_frag (lie
->add
)->fr_address
;
1679 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1681 table_ptr
+= md_long_jump_size
;
1682 table_addr
+= md_long_jump_size
;
1686 #endif /* not WORKING_DOT_WORD */
1688 #ifndef BFD_ASSEMBLER
1691 char *the_object_file
;
1692 long object_file_size
;
1693 /* Scan every FixS performing fixups. We had to wait until now to
1694 do this because md_convert_frag() may have made some fixSs. */
1697 subseg_change (SEG_TEXT
, 0);
1698 trsize
= md_reloc_size
* fixup_segment (text_fix_root
, SEG_TEXT
);
1699 subseg_change (SEG_DATA
, 0);
1700 drsize
= md_reloc_size
* fixup_segment (data_fix_root
, SEG_DATA
);
1701 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
1703 /* FIXME: Move this stuff into the pre-write-hook. */
1704 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
1705 H_SET_ENTRY_POINT (&headers
, 0);
1707 obj_pre_write_hook (&headers
); /* Extra coff stuff. */
1709 object_file_size
= H_GET_FILE_SIZE (&headers
);
1710 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
1712 output_file_create (out_file_name
);
1714 obj_header_append (&next_object_file_charP
, &headers
);
1716 know ((next_object_file_charP
- the_object_file
)
1717 == H_GET_HEADER_SIZE (&headers
));
1720 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1722 register long count
;
1723 register char *fill_literal
;
1724 register long fill_size
;
1727 know (fragP
->fr_type
== rs_fill
);
1728 append (&next_object_file_charP
, fragP
->fr_literal
,
1729 (unsigned long) fragP
->fr_fix
);
1730 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
1731 fill_size
= fragP
->fr_var
;
1732 know (fragP
->fr_offset
>= 0);
1734 for (count
= fragP
->fr_offset
; count
; count
--)
1735 append (&next_object_file_charP
, fill_literal
,
1736 (unsigned long) fill_size
);
1739 know ((next_object_file_charP
- the_object_file
)
1740 == (H_GET_HEADER_SIZE (&headers
)
1741 + H_GET_TEXT_SIZE (&headers
)
1742 + H_GET_DATA_SIZE (&headers
)));
1744 /* Emit relocations. */
1745 obj_emit_relocations (&next_object_file_charP
, text_fix_root
,
1746 (relax_addressT
) 0);
1747 know ((next_object_file_charP
- the_object_file
)
1748 == (H_GET_HEADER_SIZE (&headers
)
1749 + H_GET_TEXT_SIZE (&headers
)
1750 + H_GET_DATA_SIZE (&headers
)
1751 + H_GET_TEXT_RELOCATION_SIZE (&headers
)));
1753 /* Make addresses in data relocation directives relative to beginning of
1754 first data fragment, not end of last text fragment: alignment of the
1755 start of the data segment may place a gap between the segments. */
1756 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1757 data0_frchainP
->frch_root
->fr_address
);
1759 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1760 text_last_frag
->fr_address
);
1761 #endif /* TC_I960 */
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
)
1767 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1768 + H_GET_DATA_RELOCATION_SIZE (&headers
)));
1770 /* Emit line number entries. */
1771 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
1772 know ((next_object_file_charP
- the_object_file
)
1773 == (H_GET_HEADER_SIZE (&headers
)
1774 + H_GET_TEXT_SIZE (&headers
)
1775 + H_GET_DATA_SIZE (&headers
)
1776 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1777 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1778 + H_GET_LINENO_SIZE (&headers
)));
1781 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
1782 know ((next_object_file_charP
- the_object_file
)
1783 == (H_GET_HEADER_SIZE (&headers
)
1784 + H_GET_TEXT_SIZE (&headers
)
1785 + H_GET_DATA_SIZE (&headers
)
1786 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1787 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1788 + H_GET_LINENO_SIZE (&headers
)
1789 + H_GET_SYMBOL_TABLE_SIZE (&headers
)));
1792 if (string_byte_count
> 0)
1793 obj_emit_strings (&next_object_file_charP
);
1796 bfd_seek (stdoutput
, 0, 0);
1797 bfd_write (the_object_file
, 1, object_file_size
, stdoutput
);
1800 /* Write the data to the file. */
1801 output_file_append (the_object_file
, object_file_size
, out_file_name
);
1802 free (the_object_file
);
1806 /* Now do the VMS-dependent part of writing the object file. */
1807 vms_write_object_file (H_GET_TEXT_SIZE (&headers
),
1808 H_GET_DATA_SIZE (&headers
),
1809 H_GET_BSS_SIZE (&headers
),
1810 text_frag_root
, data_frag_root
);
1811 #endif /* OBJ_VMS */
1812 #else /* BFD_ASSEMBLER */
1814 /* Resolve symbol values. This needs to be done before processing
1820 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1821 resolve_symbol_value (symp
, 1);
1823 resolve_local_symbol_values ();
1827 #ifdef tc_frob_file_before_adjust
1828 tc_frob_file_before_adjust ();
1830 #ifdef obj_frob_file_before_adjust
1831 obj_frob_file_before_adjust ();
1834 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
1836 /* Set up symbol table, and write it out. */
1841 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1846 if (symbol_mri_common_p (symp
))
1848 if (S_IS_EXTERNAL (symp
))
1849 as_bad (_("%s: global symbols not supported in common sections"),
1851 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1855 name
= S_GET_NAME (symp
);
1859 decode_local_label_name ((char *) S_GET_NAME (symp
));
1860 /* They only differ if `name' is a fb or dollar local
1862 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
1863 as_bad (_("local label %s is not defined"), name2
);
1866 /* Do it again, because adjust_reloc_syms might introduce
1867 more symbols. They'll probably only be section symbols,
1868 but they'll still need to have the values computed. */
1869 resolve_symbol_value (symp
, 1);
1871 /* Skip symbols which were equated to undefined or common
1873 if (symbol_equated_p (symp
)
1874 && (! S_IS_DEFINED (symp
) || S_IS_COMMON (symp
)))
1876 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1880 /* So far, common symbols have been treated like undefined symbols.
1881 Put them in the common section now. */
1882 if (S_IS_DEFINED (symp
) == 0
1883 && S_GET_VALUE (symp
) != 0)
1884 S_SET_SEGMENT (symp
, bfd_com_section_ptr
);
1886 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1887 S_GET_NAME (symp
), symp
,
1889 symbol_get_bfdsym (symp
)->flags
,
1890 segment_name (S_GET_SEGMENT (symp
)));
1893 #ifdef obj_frob_symbol
1894 obj_frob_symbol (symp
, punt
);
1896 #ifdef tc_frob_symbol
1897 if (! punt
|| symbol_used_in_reloc_p (symp
))
1898 tc_frob_symbol (symp
, punt
);
1901 /* If we don't want to keep this symbol, splice it out of
1902 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1903 want section symbols. Otherwise, we skip local symbols
1904 and symbols that the frob_symbol macros told us to punt,
1905 but we keep such symbols if they are used in relocs. */
1906 if ((! EMIT_SECTION_SYMBOLS
1907 && symbol_section_p (symp
))
1908 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1909 opposites. Sometimes the former checks flags and the
1910 latter examines the name... */
1911 || (!S_IS_EXTERN (symp
)
1912 && (S_IS_LOCAL (symp
) || punt
)
1913 && ! symbol_used_in_reloc_p (symp
)))
1915 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1917 /* After symbol_remove, symbol_next(symp) still returns
1918 the one that came after it in the chain. So we don't
1919 need to do any extra cleanup work here. */
1923 /* Make sure we really got a value for the symbol. */
1924 if (! symbol_resolved_p (symp
))
1926 as_bad (_("can't resolve value for symbol \"%s\""),
1928 symbol_mark_resolved (symp
);
1931 /* Set the value into the BFD symbol. Up til now the value
1932 has only been kept in the gas symbolS struct. */
1933 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
1939 /* Now do any format-specific adjustments to the symbol table, such
1940 as adding file symbols. */
1941 #ifdef tc_adjust_symtab
1942 tc_adjust_symtab ();
1944 #ifdef obj_adjust_symtab
1945 obj_adjust_symtab ();
1948 /* Now that all the sizes are known, and contents correct, we can
1949 start writing to the file. */
1952 /* If *_frob_file changes the symbol value at this point, it is
1953 responsible for moving the changed value into symp->bsym->value
1954 as well. Hopefully all symbol value changing can be done in
1959 #ifdef obj_frob_file
1963 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
1965 #ifdef tc_frob_file_after_relocs
1966 tc_frob_file_after_relocs ();
1968 #ifdef obj_frob_file_after_relocs
1969 obj_frob_file_after_relocs ();
1972 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
1973 #endif /* BFD_ASSEMBLER */
1977 #ifdef TC_GENERIC_RELAX_TABLE
1979 static int is_dnrange
PARAMS ((fragS
*, fragS
*));
1981 /* Subroutines of relax_segment. */
1987 for (; f1
; f1
= f1
->fr_next
)
1988 if (f1
->fr_next
== f2
)
1993 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1996 relax_frag (fragP
, stretch
)
2000 const relax_typeS
*this_type
;
2001 const relax_typeS
*start_type
;
2002 relax_substateT next_state
;
2003 relax_substateT this_state
;
2004 long aim
, target
, growth
;
2005 symbolS
*symbolP
= fragP
->fr_symbol
;
2006 long offset
= fragP
->fr_offset
;
2007 /* Recompute was_address by undoing "+= stretch" done by relax_segment. */
2008 unsigned long was_address
= fragP
->fr_address
- stretch
;
2009 unsigned long address
= fragP
->fr_address
;
2010 const relax_typeS
*table
= TC_GENERIC_RELAX_TABLE
;
2012 this_state
= fragP
->fr_subtype
;
2013 start_type
= this_type
= table
+ this_state
;
2018 #ifndef DIFF_EXPR_OK
2019 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2020 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2021 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2022 || (S_GET_SEGMENT (symbolP
) == SEG_BSS
)
2023 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
2025 know (symbolP
->sy_frag
);
2027 know (!(S_GET_SEGMENT (symbolP
) == absolute_section
)
2028 || symbolP
->sy_frag
== &zero_address_frag
);
2029 target
+= S_GET_VALUE (symbolP
) + symbol_get_frag (symbolP
)->fr_address
;
2031 /* If frag has yet to be reached on this pass,
2032 assume it will move by STRETCH just as we did.
2033 If this is not so, it will be because some frag
2034 between grows, and that will force another pass.
2036 Beware zero-length frags.
2038 There should be a faster way to do this. */
2040 if (symbol_get_frag (symbolP
)->fr_address
>= was_address
2041 && is_dnrange (fragP
, symbol_get_frag (symbolP
)))
2047 aim
= target
- address
- fragP
->fr_fix
;
2048 #ifdef TC_PCREL_ADJUST
2049 /* Currently only the ns32k family needs this. */
2050 aim
+= TC_PCREL_ADJUST (fragP
);
2052 /* This machine doesn't want to use pcrel_adjust.
2053 In that case, pcrel_adjust should be zero. */
2055 assert (fragP
->fr_targ
.ns32k
.pcrel_adjust
== 0);
2058 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2059 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2064 /* Look backwards. */
2065 for (next_state
= this_type
->rlx_more
; next_state
;)
2066 if (aim
>= this_type
->rlx_backward
)
2070 /* Grow to next state. */
2071 this_state
= next_state
;
2072 this_type
= table
+ this_state
;
2073 next_state
= this_type
->rlx_more
;
2078 /* Look forwards. */
2079 for (next_state
= this_type
->rlx_more
; next_state
;)
2080 if (aim
<= this_type
->rlx_forward
)
2084 /* Grow to next state. */
2085 this_state
= next_state
;
2086 this_type
= table
+ this_state
;
2087 next_state
= this_type
->rlx_more
;
2091 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2093 fragP
->fr_subtype
= this_state
;
2097 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2099 /* Relax_align. Advance location counter to next address that has 'alignment'
2100 lowest order bits all 0s, return size of adjustment made. */
2101 static relax_addressT
2102 relax_align (address
, alignment
)
2103 register relax_addressT address
; /* Address now. */
2104 register int alignment
; /* Alignment (binary). */
2106 relax_addressT mask
;
2107 relax_addressT new_address
;
2109 mask
= ~((~0) << alignment
);
2110 new_address
= (address
+ mask
) & (~mask
);
2111 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2113 /* We must provide lots of padding, so the linker can discard it
2114 when needed. The linker will not add extra space, ever. */
2115 new_address
+= (1 << alignment
);
2117 return (new_address
- address
);
2120 /* Now we have a segment, not a crowd of sub-segments, we can make
2125 After this, all frags in this segment have addresses that are correct
2126 within the segment. Since segments live in different file addresses,
2127 these frag addresses may not be the same as final object-file
2131 relax_segment (segment_frag_root
, segment
)
2132 struct frag
*segment_frag_root
;
2135 register struct frag
*fragP
;
2136 register relax_addressT address
;
2137 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2138 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
2140 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2141 subseg_change (segment
, 0);
2143 /* For each frag in segment: count and store (a 1st guess of)
2146 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2148 fragP
->fr_address
= address
;
2149 address
+= fragP
->fr_fix
;
2151 switch (fragP
->fr_type
)
2154 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2160 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2162 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2165 if (offset
% fragP
->fr_var
!= 0)
2167 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2168 (unsigned long) offset
, (long) fragP
->fr_var
);
2169 offset
-= (offset
% fragP
->fr_var
);
2178 /* Assume .org is nugatory. It will grow with 1st relax. */
2181 case rs_machine_dependent
:
2182 address
+= md_estimate_size_before_relax (fragP
, segment
);
2185 #ifndef WORKING_DOT_WORD
2186 /* Broken words don't concern us yet. */
2187 case rs_broken_word
:
2192 /* Initial guess is always 1; doing otherwise can result in
2193 stable solutions that are larger than the minimum. */
2194 address
+= fragP
->fr_offset
= 1;
2198 address
+= eh_frame_estimate_size_before_relax (fragP
);
2202 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
2206 BAD_CASE (fragP
->fr_type
);
2213 long stretch
; /* May be any size, 0 or negative. */
2214 /* Cumulative number of addresses we have relaxed this pass.
2215 We may have relaxed more than one address. */
2216 long stretched
; /* Have we stretched on this pass? */
2217 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2218 grew, and another shrank. If a branch instruction doesn't fit anymore,
2219 we could be scrod. */
2223 stretch
= stretched
= 0;
2225 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2228 addressT was_address
;
2232 was_address
= fragP
->fr_address
;
2233 address
= fragP
->fr_address
+= stretch
;
2234 symbolP
= fragP
->fr_symbol
;
2235 offset
= fragP
->fr_offset
;
2237 switch (fragP
->fr_type
)
2239 case rs_fill
: /* .fill never relaxes. */
2243 #ifndef WORKING_DOT_WORD
2244 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2245 for it I do not want to write it. I do not want to have
2246 anything to do with it. This is not the proper way to
2247 implement this misfeature. */
2248 case rs_broken_word
:
2250 struct broken_word
*lie
;
2251 struct broken_word
*untruth
;
2253 /* Yes this is ugly (storing the broken_word pointer
2254 in the symbol slot). Still, this whole chunk of
2255 code is ugly, and I don't feel like doing anything
2256 about it. Think of it as stubbornness in action. */
2258 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2259 lie
&& lie
->dispfrag
== fragP
;
2260 lie
= lie
->next_broken_word
)
2266 offset
= (symbol_get_frag (lie
->add
)->fr_address
2267 + S_GET_VALUE (lie
->add
)
2269 - (symbol_get_frag (lie
->sub
)->fr_address
2270 + S_GET_VALUE (lie
->sub
)));
2271 if (offset
<= -32768 || offset
>= 32767)
2273 if (flag_warn_displacement
)
2276 sprint_value (buf
, (addressT
) lie
->addnum
);
2277 as_warn (_(".word %s-%s+%s didn't fit"),
2278 S_GET_NAME (lie
->add
),
2279 S_GET_NAME (lie
->sub
),
2283 if (fragP
->fr_subtype
== 0)
2285 fragP
->fr_subtype
++;
2286 growth
+= md_short_jump_size
;
2288 for (untruth
= lie
->next_broken_word
;
2289 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2290 untruth
= untruth
->next_broken_word
)
2291 if ((symbol_get_frag (untruth
->add
)
2292 == symbol_get_frag (lie
->add
))
2293 && (S_GET_VALUE (untruth
->add
)
2294 == S_GET_VALUE (lie
->add
)))
2297 untruth
->use_jump
= lie
;
2299 growth
+= md_long_jump_size
;
2304 } /* case rs_broken_word */
2309 addressT oldoff
, newoff
;
2311 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2313 newoff
= relax_align (address
+ fragP
->fr_fix
,
2316 if (fragP
->fr_subtype
!= 0)
2318 if (oldoff
> fragP
->fr_subtype
)
2320 if (newoff
> fragP
->fr_subtype
)
2324 growth
= newoff
- oldoff
;
2330 long target
= offset
;
2335 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2336 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2337 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2338 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
)
2339 || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
2340 know (symbolP
->sy_frag
);
2341 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2342 || (symbolP
->sy_frag
== &zero_address_frag
));
2344 target
+= (S_GET_VALUE (symbolP
)
2345 + symbol_get_frag (symbolP
)->fr_address
);
2346 } /* if we have a symbol */
2348 know (fragP
->fr_next
);
2349 after
= fragP
->fr_next
->fr_address
;
2350 growth
= target
- after
;
2353 /* Growth may be negative, but variable part of frag
2354 cannot have fewer than 0 chars. That is, we can't
2356 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2357 _("attempt to .org backwards ignored"));
2359 /* We've issued an error message. Change the
2360 frag to avoid cascading errors. */
2361 fragP
->fr_type
= rs_align
;
2362 fragP
->fr_subtype
= 0;
2363 fragP
->fr_offset
= 0;
2364 fragP
->fr_fix
= after
- address
;
2368 /* This is an absolute growth factor */
2376 growth
= S_GET_VALUE (symbolP
);
2377 if (symbol_get_frag (symbolP
) != &zero_address_frag
2378 || S_IS_COMMON (symbolP
)
2379 || ! S_IS_DEFINED (symbolP
))
2380 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2381 _(".space specifies non-absolute value"));
2382 fragP
->fr_symbol
= 0;
2385 as_warn (_(".space or .fill with negative value, ignored"));
2393 case rs_machine_dependent
:
2394 #ifdef md_relax_frag
2395 growth
= md_relax_frag (fragP
, stretch
);
2397 #ifdef TC_GENERIC_RELAX_TABLE
2398 /* The default way to relax a frag is to look through
2399 TC_GENERIC_RELAX_TABLE. */
2400 growth
= relax_frag (fragP
, stretch
);
2401 #endif /* TC_GENERIC_RELAX_TABLE */
2410 value
= resolve_symbol_value (fragP
->fr_symbol
, 0);
2411 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2412 growth
= size
- fragP
->fr_offset
;
2413 fragP
->fr_offset
= size
;
2418 growth
= eh_frame_relax_frag (fragP
);
2422 growth
= dwarf2dbg_relax_frag (fragP
);
2426 BAD_CASE (fragP
->fr_type
);
2434 } /* For each frag in the segment. */
2436 while (stretched
); /* Until nothing further to relax. */
2439 /* We now have valid fr_address'es for each frag. */
2441 /* All fr_address's are correct, relative to their own segment.
2442 We have made all the fixS we will ever make. */
2445 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2447 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2448 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2453 Go through all the fixS's in a segment and see which ones can be
2454 handled now. (These consist of fixS where we have since discovered
2455 the value of a symbol, or the address of the frag involved.)
2456 For each one, call md_apply_fix to put the fix into the frag data.
2458 Result is a count of how many relocation structs will be needed to
2459 handle the remaining fixS's that we couldn't completely handle here.
2460 These will be output later by emit_relocations(). */
2463 fixup_segment (fixP
, this_segment_type
)
2464 register fixS
*fixP
;
2465 segT this_segment_type
; /* N_TYPE bits for segment. */
2467 long seg_reloc_count
= 0;
2468 symbolS
*add_symbolP
;
2469 symbolS
*sub_symbolP
;
2476 segT add_symbol_segment
= absolute_section
;
2478 /* If the linker is doing the relaxing, we must not do any fixups.
2480 Well, strictly speaking that's not true -- we could do any that are
2481 PC-relative and don't cross regions that could change size. And for the
2482 i960 (the only machine for which we've got a relaxing linker right now),
2483 we might be able to turn callx/callj into bal anyways in cases where we
2484 know the maximum displacement. */
2485 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment_type
))
2487 for (; fixP
; fixP
= fixP
->fx_next
)
2489 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2490 return seg_reloc_count
;
2493 for (; fixP
; fixP
= fixP
->fx_next
)
2496 fprintf (stderr
, "\nprocessing fixup:\n");
2500 fragP
= fixP
->fx_frag
;
2502 where
= fixP
->fx_where
;
2503 place
= fragP
->fr_literal
+ where
;
2504 size
= fixP
->fx_size
;
2505 add_symbolP
= fixP
->fx_addsy
;
2506 #ifdef TC_VALIDATE_FIX
2507 TC_VALIDATE_FIX (fixP
, this_segment_type
, skip
);
2509 sub_symbolP
= fixP
->fx_subsy
;
2510 add_number
= fixP
->fx_offset
;
2511 pcrel
= fixP
->fx_pcrel
;
2514 if (add_symbolP
!= NULL
2515 && symbol_mri_common_p (add_symbolP
))
2517 know (add_symbolP
->sy_value
.X_op
== O_symbol
);
2518 add_number
+= S_GET_VALUE (add_symbolP
);
2519 fixP
->fx_offset
= add_number
;
2520 add_symbolP
= fixP
->fx_addsy
=
2521 symbol_get_value_expression (add_symbolP
)->X_add_symbol
;
2525 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2529 resolve_symbol_value (sub_symbolP
, 1);
2530 if (add_symbolP
== NULL
|| add_symbol_segment
== absolute_section
)
2532 if (add_symbolP
!= NULL
)
2534 add_number
+= S_GET_VALUE (add_symbolP
);
2536 fixP
->fx_addsy
= NULL
;
2539 /* It's just -sym. */
2540 if (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)
2542 add_number
-= S_GET_VALUE (sub_symbolP
);
2543 fixP
->fx_subsy
= NULL
;
2546 && S_GET_SEGMENT (sub_symbolP
) == this_segment_type
)
2548 /* Should try converting to a constant. */
2553 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2554 _("Negative of non-absolute symbol %s"),
2555 S_GET_NAME (sub_symbolP
));
2557 else if (S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
2558 && SEG_NORMAL (add_symbol_segment
))
2560 /* Difference of 2 symbols from same segment.
2561 Can't make difference of 2 undefineds: 'value' means
2562 something different for N_UNDF. */
2564 /* Makes no sense to use the difference of 2 arbitrary symbols
2565 as the target of a call instruction. */
2567 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2568 _("callj to difference of 2 symbols"));
2569 #endif /* TC_I960 */
2570 add_number
+= S_GET_VALUE (add_symbolP
) -
2571 S_GET_VALUE (sub_symbolP
);
2574 pcrel
= 0; /* No further pcrel processing. */
2576 /* Let the target machine make the final determination
2577 as to whether or not a relocation will be needed to
2578 handle this fixup. */
2579 if (!TC_FORCE_RELOCATION_SECTION (fixP
, this_segment_type
))
2582 fixP
->fx_addsy
= NULL
;
2583 fixP
->fx_subsy
= NULL
;
2588 /* Different segments in subtraction. */
2589 know (!(S_IS_EXTERNAL (sub_symbolP
)
2590 && (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)));
2592 if ((S_GET_SEGMENT (sub_symbolP
) == absolute_section
))
2593 add_number
-= S_GET_VALUE (sub_symbolP
);
2596 else if (S_GET_SEGMENT (sub_symbolP
) == this_segment_type
2598 /* Do this even if it's already described as
2599 pc-relative. For example, on the m68k, an
2600 operand of "pc@(foo-.-2)" should address
2601 "foo" in a pc-relative mode. */
2606 /* Make it pc-relative. */
2607 add_number
+= (MD_PCREL_FROM_SECTION (fixP
, this_segment_type
)
2608 - S_GET_VALUE (sub_symbolP
));
2615 #ifdef UNDEFINED_DIFFERENCE_OK
2616 /* The PA needs this for PIC code generation. We basically
2617 don't want to do anything if we have the difference of two
2618 symbols at this point. */
2621 /* Leave it alone. */
2624 #ifdef BFD_ASSEMBLER
2625 else if (fixP
->fx_r_type
== BFD_RELOC_GPREL32
2626 || fixP
->fx_r_type
== BFD_RELOC_GPREL16
)
2628 /* Leave it alone. */
2634 sprint_value (buf
, fragP
->fr_address
+ where
);
2635 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2636 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2637 S_GET_NAME (add_symbolP
),
2638 segment_name (S_GET_SEGMENT (add_symbolP
)),
2639 S_GET_NAME (sub_symbolP
),
2640 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2648 if (add_symbol_segment
== this_segment_type
&& pcrel
&& !plt
2649 && TC_RELOC_RTSYM_LOC_FIXUP (fixP
))
2651 /* This fixup was made when the symbol's segment was
2652 SEG_UNKNOWN, but it is now in the local segment.
2653 So we know how to do the address without relocation. */
2655 /* reloc_callj() may replace a 'call' with a 'calls' or a
2656 'bal', in which cases it modifies *fixP as appropriate.
2657 In the case of a 'calls', no further work is required,
2658 and *fixP has been set up to make the rest of the code
2661 #endif /* TC_I960 */
2663 add_number
+= S_GET_VALUE (add_symbolP
);
2664 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2665 /* Lie. Don't want further pcrel processing. */
2668 /* Let the target machine make the final determination
2669 as to whether or not a relocation will be needed to
2670 handle this fixup. */
2671 if (!TC_FORCE_RELOCATION (fixP
))
2674 fixP
->fx_addsy
= NULL
;
2679 if (add_symbol_segment
== absolute_section
2683 /* See comment about reloc_callj() above. */
2685 #endif /* TC_I960 */
2686 add_number
+= S_GET_VALUE (add_symbolP
);
2688 /* Let the target machine make the final determination
2689 as to whether or not a relocation will be needed to
2690 handle this fixup. */
2692 if (!TC_FORCE_RELOCATION (fixP
))
2694 fixP
->fx_addsy
= NULL
;
2698 else if (add_symbol_segment
== undefined_section
2699 #ifdef BFD_ASSEMBLER
2700 || bfd_is_com_section (add_symbol_segment
)
2705 if ((int) fixP
->fx_bit_fixP
== 13)
2707 /* This is a COBR instruction. They have only a
2708 13-bit displacement and are only to be used
2709 for local branches: flag as error, don't generate
2711 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2712 _("can't use COBR format with external label"));
2713 fixP
->fx_addsy
= NULL
;
2717 #endif /* TC_I960 */
2721 if (S_IS_COMMON (add_symbolP
))
2722 add_number
+= S_GET_VALUE (add_symbolP
);
2723 #endif /* TE_I386AIX */
2724 #endif /* OBJ_COFF */
2730 if (TC_FIX_ADJUSTABLE (fixP
))
2731 add_number
+= S_GET_VALUE (add_symbolP
);
2738 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2739 if (add_symbolP
== 0)
2741 #ifndef BFD_ASSEMBLER
2742 fixP
->fx_addsy
= &abs_symbol
;
2744 fixP
->fx_addsy
= section_symbol (absolute_section
);
2746 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2753 #ifdef MD_APPLY_FIX3
2754 md_apply_fix3 (fixP
, &add_number
, this_segment_type
);
2756 #ifdef BFD_ASSEMBLER
2757 md_apply_fix (fixP
, &add_number
);
2759 md_apply_fix (fixP
, add_number
);
2763 #ifndef TC_HANDLES_FX_DONE
2764 /* If the tc-* files haven't been converted, assume it's handling
2765 it the old way, where a null fx_addsy means that the fix has
2766 been applied completely, and no further work is needed. */
2767 if (fixP
->fx_addsy
== 0 && fixP
->fx_pcrel
== 0)
2772 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& size
> 0)
2774 if ((size_t) size
< sizeof (valueT
))
2779 mask
--; /* Set all bits to one. */
2780 mask
<<= size
* 8 - (fixP
->fx_signed
? 1 : 0);
2781 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
2783 char buf
[50], buf2
[50];
2784 sprint_value (buf
, fragP
->fr_address
+ where
);
2785 if (add_number
> 1000)
2786 sprint_value (buf2
, add_number
);
2788 sprintf (buf2
, "%ld", (long) add_number
);
2789 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2790 _("Value of %s too large for field of %d bytes at %s"),
2792 } /* Generic error checking. */
2794 #ifdef WARN_SIGNED_OVERFLOW_WORD
2795 /* Warn if a .word value is too large when treated as a signed
2796 number. We already know it is not too negative. This is to
2797 catch over-large switches generated by gcc on the 68k. */
2798 if (!flag_signed_overflow_ok
2800 && add_number
> 0x7fff)
2801 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2802 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2804 (unsigned long) (fragP
->fr_address
+ where
));
2806 } /* Not a bit fix. */
2808 #ifdef TC_VALIDATE_FIX
2809 skip
: ATTRIBUTE_UNUSED_LABEL
2813 fprintf (stderr
, "result:\n");
2816 } /* For each fixS in this segment. */
2818 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2819 return seg_reloc_count
;
2822 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2825 number_to_chars_bigendian (buf
, val
, n
)
2830 if ((size_t) n
> sizeof (val
) || n
<= 0)
2834 buf
[n
] = val
& 0xff;
2840 number_to_chars_littleendian (buf
, val
, n
)
2845 if ((size_t) n
> sizeof (val
) || n
<= 0)
2849 *buf
++ = val
& 0xff;
2855 write_print_statistics (file
)
2858 fprintf (file
, "fixups: %d\n", n_fixups
);
2861 /* For debugging. */
2862 extern int indent_level
;
2869 fprintf (stderr
, "fix %lx %s:%d", (long) fixp
, fixp
->fx_file
, fixp
->fx_line
);
2871 fprintf (stderr
, " pcrel");
2872 if (fixp
->fx_pcrel_adjust
)
2873 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2874 if (fixp
->fx_im_disp
)
2877 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2879 fprintf (stderr
, " im_disp");
2883 fprintf (stderr
, " tcbit");
2885 fprintf (stderr
, " done");
2886 fprintf (stderr
, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2887 fixp
->fx_size
, (long) fixp
->fx_frag
, (long) fixp
->fx_where
,
2888 (long) fixp
->fx_offset
, (long) fixp
->fx_addnumber
);
2889 #ifdef BFD_ASSEMBLER
2890 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2893 #ifdef NEED_FX_R_TYPE
2894 fprintf (stderr
, " r_type=%d", fixp
->fx_r_type
);
2899 fprintf (stderr
, "\n +<");
2900 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2901 fprintf (stderr
, ">");
2905 fprintf (stderr
, "\n -<");
2906 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2907 fprintf (stderr
, ">");
2909 fprintf (stderr
, "\n");
2910 #ifdef TC_FIX_DATA_PRINT
2911 TC_FIX_DATA_PRINT (stderr
, fixp
);