1 /* write.c - emit .o file
2 Copyright (C) 1986, 87, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
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"
29 /* This looks like a good idea. Let's try turning it on always, for now. */
30 #undef BFD_FAST_SECTION_FILL
31 #define BFD_FAST_SECTION_FILL
33 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
34 instruction so that the disassembler does not choke on it. */
36 #define NOP_OPCODE 0x00
39 #ifndef TC_ADJUST_RELOC_COUNT
40 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
43 #ifndef TC_FORCE_RELOCATION
44 #define TC_FORCE_RELOCATION(FIXP) 0
47 #ifndef TC_FORCE_RELOCATION_SECTION
48 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
51 #ifndef MD_PCREL_FROM_SECTION
52 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
55 #ifndef WORKING_DOT_WORD
56 extern CONST
int md_short_jump_size
;
57 extern CONST
int md_long_jump_size
;
60 int symbol_table_frozen
;
61 void print_fixup
PARAMS ((fixS
*));
64 static void renumber_sections
PARAMS ((bfd
*, asection
*, PTR
));
66 /* We generally attach relocs to frag chains. However, after we have
67 chained these all together into a segment, any relocs we add after
68 that must be attached to a segment. This will include relocs added
69 in md_estimate_size_for_relax, for example. */
70 static int frags_chained
= 0;
76 struct frag
*text_frag_root
;
77 struct frag
*data_frag_root
;
78 struct frag
*bss_frag_root
;
80 struct frag
*text_last_frag
; /* Last frag in segment. */
81 struct frag
*data_last_frag
; /* Last frag in segment. */
82 static struct frag
*bss_last_frag
; /* Last frag in segment. */
86 static object_headers headers
;
89 long string_byte_count
;
90 char *next_object_file_charP
; /* Tracks object file bytes. */
93 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
96 #endif /* BFD_ASSEMBLER */
101 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
102 symbolS
*add
, symbolS
*sub
,
103 offsetT offset
, int pcrel
,
104 bfd_reloc_code_real_type r_type
));
106 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
107 symbolS
*add
, symbolS
*sub
,
108 offsetT offset
, int pcrel
,
111 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
112 static long fixup_segment
PARAMS ((fixS
* fixP
, segT this_segment_type
));
114 static relax_addressT relax_align
PARAMS ((relax_addressT addr
, int align
));
115 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
116 static fragS
*chain_frchains_together_1
PARAMS ((segT
, struct frchain
*));
119 static void chain_frchains_together
PARAMS ((bfd
*, segT
, PTR
));
120 static void cvt_frag_to_fill
PARAMS ((segT
, fragS
*));
121 static void relax_and_size_seg
PARAMS ((bfd
*, asection
*, PTR
));
122 static void adjust_reloc_syms
PARAMS ((bfd
*, asection
*, PTR
));
123 static void write_relocs
PARAMS ((bfd
*, asection
*, PTR
));
124 static void write_contents
PARAMS ((bfd
*, asection
*, PTR
));
125 static void set_symtab
PARAMS ((void));
127 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
128 static void merge_data_into_text
PARAMS ((void));
130 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
131 static void cvt_frag_to_fill
PARAMS ((object_headers
*, segT
, fragS
*));
132 static void remove_subsegs
PARAMS ((frchainS
*, int, fragS
**, fragS
**));
133 static void relax_and_size_all_segments
PARAMS ((void));
135 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
136 static void set_segment_vma
PARAMS ((bfd
*, asection
*, PTR
));
142 * Create a fixS in obstack 'notes'.
145 fix_new_internal (frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
,
147 fragS
*frag
; /* Which frag? */
148 int where
; /* Where in that frag? */
149 int size
; /* 1, 2, or 4 usually. */
150 symbolS
*add_symbol
; /* X_add_symbol. */
151 symbolS
*sub_symbol
; /* X_op_symbol. */
152 offsetT offset
; /* X_add_number. */
153 int pcrel
; /* TRUE if PC-relative relocation. */
155 bfd_reloc_code_real_type r_type
; /* Relocation type */
157 int r_type
; /* Relocation type */
164 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
166 fixP
->fx_frag
= frag
;
167 fixP
->fx_where
= where
;
168 fixP
->fx_size
= size
;
169 /* We've made fx_size a narrow field; check that it's wide enough. */
170 if (fixP
->fx_size
!= size
)
172 as_bad (_("field fx_size too small to hold %d"), size
);
175 fixP
->fx_addsy
= add_symbol
;
176 fixP
->fx_subsy
= sub_symbol
;
177 fixP
->fx_offset
= offset
;
178 fixP
->fx_pcrel
= pcrel
;
180 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
181 fixP
->fx_r_type
= r_type
;
183 fixP
->fx_im_disp
= 0;
184 fixP
->fx_pcrel_adjust
= 0;
185 fixP
->fx_bit_fixP
= 0;
186 fixP
->fx_addnumber
= 0;
189 fixP
->fx_no_overflow
= 0;
193 fixP
->fx_cgen
.insn
= NULL
;
194 fixP
->fx_cgen
.opinfo
= 0;
198 TC_INIT_FIX_DATA(fixP
);
201 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
203 /* Usually, we want relocs sorted numerically, but while
204 comparing to older versions of gas that have relocs
205 reverse sorted, it is convenient to have this compile
206 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 */
241 /* Create a fixup relative to a symbol (plus a constant). */
244 fix_new (frag
, where
, size
, add_symbol
, offset
, pcrel
, r_type
)
245 fragS
*frag
; /* Which frag? */
246 int where
; /* Where in that frag? */
247 int size
; /* 1, 2, or 4 usually. */
248 symbolS
*add_symbol
; /* X_add_symbol. */
249 offsetT offset
; /* X_add_number. */
250 int pcrel
; /* TRUE if PC-relative relocation. */
252 bfd_reloc_code_real_type r_type
; /* Relocation type */
254 int r_type
; /* Relocation type */
257 return fix_new_internal (frag
, where
, size
, add_symbol
,
258 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
261 /* Create a fixup for an expression. Currently we only support fixups
262 for difference expressions. That is itself more than most object
263 file formats support anyhow. */
266 fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
)
267 fragS
*frag
; /* Which frag? */
268 int where
; /* Where in that frag? */
269 int size
; /* 1, 2, or 4 usually. */
270 expressionS
*exp
; /* Expression. */
271 int pcrel
; /* TRUE if PC-relative relocation. */
273 bfd_reloc_code_real_type r_type
; /* Relocation type */
275 int r_type
; /* Relocation type */
288 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
289 the difference expression cannot immediately be reduced. */
291 symbolS
*stmp
= make_expr_symbol (exp
);
292 exp
->X_op
= O_symbol
;
293 exp
->X_op_symbol
= 0;
294 exp
->X_add_symbol
= stmp
;
295 exp
->X_add_number
= 0;
296 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
300 add
= exp
->X_add_symbol
;
301 off
= exp
->X_add_number
;
303 #if defined(BFD_ASSEMBLER)
304 r_type
= BFD_RELOC_RVA
;
306 #if defined(TC_RVA_RELOC)
307 r_type
= TC_RVA_RELOC
;
309 as_fatal(_("rva not supported"));
315 sub
= exp
->X_add_symbol
;
316 off
= exp
->X_add_number
;
320 sub
= exp
->X_op_symbol
;
323 add
= exp
->X_add_symbol
;
326 off
= exp
->X_add_number
;
330 add
= make_expr_symbol (exp
);
334 return fix_new_internal (frag
, where
, size
, add
, sub
, off
,
338 /* Append a string onto another string, bumping the pointer along. */
340 append (charPP
, fromP
, length
)
343 unsigned long length
;
345 /* Don't trust memcpy() of 0 chars. */
349 memcpy (*charPP
, fromP
, length
);
353 #ifndef BFD_ASSEMBLER
354 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
358 * This routine records the largest alignment seen for each segment.
359 * If the beginning of the segment is aligned on the worst-case
360 * boundary, all of the other alignments within it will work. At
361 * least one object format really uses this info.
364 record_alignment (seg
, align
)
365 /* Segment to which alignment pertains */
367 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
371 if (seg
== absolute_section
)
374 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
375 bfd_set_section_alignment (stdoutput
, seg
, align
);
377 if (align
> section_alignment
[(int) seg
])
378 section_alignment
[(int) seg
] = align
;
384 /* Reset the section indices after removing the gas created sections. */
387 renumber_sections (abfd
, sec
, countparg
)
388 bfd
*abfd ATTRIBUTE_UNUSED
;
392 int *countp
= (int *) countparg
;
394 sec
->index
= *countp
;
398 #endif /* defined (BFD_ASSEMBLER) */
400 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
403 chain_frchains_together_1 (section
, frchp
)
405 struct frchain
*frchp
;
407 fragS dummy
, *prev_frag
= &dummy
;
409 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
412 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
414 prev_frag
->fr_next
= frchp
->frch_root
;
415 prev_frag
= frchp
->frch_last
;
416 assert (prev_frag
->fr_type
!= 0);
418 if (frchp
->fix_root
!= (fixS
*) NULL
)
420 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
421 seg_info (section
)->fix_root
= frchp
->fix_root
;
422 prev_fix
->fx_next
= frchp
->fix_root
;
423 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
424 prev_fix
= frchp
->fix_tail
;
428 assert (prev_frag
->fr_type
!= 0);
429 prev_frag
->fr_next
= 0;
438 chain_frchains_together (abfd
, section
, xxx
)
439 bfd
*abfd ATTRIBUTE_UNUSED
;
441 PTR xxx ATTRIBUTE_UNUSED
;
443 segment_info_type
*info
;
445 /* BFD may have introduced its own sections without using
446 subseg_new, so it is possible that seg_info is NULL. */
447 info
= seg_info (section
);
448 if (info
!= (segment_info_type
*) NULL
)
449 info
->frchainP
->frch_last
450 = chain_frchains_together_1 (section
, info
->frchainP
);
452 /* Now that we've chained the frags together, we must add new fixups
453 to the segment, not to the frag chain. */
459 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
462 remove_subsegs (head
, seg
, root
, last
)
468 *root
= head
->frch_root
;
469 *last
= chain_frchains_together_1 (seg
, head
);
474 #if defined (BFD_ASSEMBLER) || !defined (BFD)
478 cvt_frag_to_fill (sec
, fragP
)
483 cvt_frag_to_fill (headersP
, sec
, fragP
)
484 object_headers
*headersP
;
489 switch (fragP
->fr_type
)
496 HANDLE_ALIGN (fragP
);
498 know (fragP
->fr_next
!= NULL
);
499 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
501 - fragP
->fr_fix
) / fragP
->fr_var
;
502 if (fragP
->fr_offset
< 0)
504 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
505 _("attempt to .org/.space backwards? (%ld)"),
506 (long) fragP
->fr_offset
);
508 fragP
->fr_type
= rs_fill
;
516 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
519 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
522 fragP
->fr_fix
+= size
;
523 fragP
->fr_type
= rs_fill
;
525 fragP
->fr_offset
= 0;
526 fragP
->fr_symbol
= NULL
;
531 eh_frame_convert_frag (fragP
);
534 case rs_machine_dependent
:
536 md_convert_frag (stdoutput
, sec
, fragP
);
538 md_convert_frag (headersP
, sec
, fragP
);
541 assert (fragP
->fr_next
== NULL
542 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
546 * After md_convert_frag, we make the frag into a ".space 0".
547 * Md_convert_frag() should set up any fixSs and constants
553 #ifndef WORKING_DOT_WORD
556 struct broken_word
*lie
;
558 if (fragP
->fr_subtype
)
560 fragP
->fr_fix
+= md_short_jump_size
;
561 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
562 lie
&& lie
->dispfrag
== fragP
;
563 lie
= lie
->next_broken_word
)
565 fragP
->fr_fix
+= md_long_jump_size
;
573 BAD_CASE (fragP
->fr_type
);
578 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
582 relax_and_size_seg (abfd
, sec
, xxx
)
585 PTR xxx ATTRIBUTE_UNUSED
;
589 segment_info_type
*seginfo
;
591 valueT size
, newsize
;
593 subseg_change (sec
, 0);
595 flags
= bfd_get_section_flags (abfd
, sec
);
597 seginfo
= seg_info (sec
);
598 if (seginfo
&& seginfo
->frchainP
)
600 relax_segment (seginfo
->frchainP
->frch_root
, sec
);
601 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
602 cvt_frag_to_fill (sec
, fragp
);
603 for (fragp
= seginfo
->frchainP
->frch_root
;
605 fragp
= fragp
->fr_next
)
606 /* walk to last elt */;
607 size
= fragp
->fr_address
+ fragp
->fr_fix
;
612 if (size
> 0 && ! seginfo
->bss
)
613 flags
|= SEC_HAS_CONTENTS
;
615 /* @@ This is just an approximation. */
616 if (seginfo
&& seginfo
->fix_root
)
620 x
= bfd_set_section_flags (abfd
, sec
, flags
);
623 newsize
= md_section_align (sec
, size
);
624 x
= bfd_set_section_size (abfd
, sec
, newsize
);
627 /* If the size had to be rounded up, add some padding in the last
629 assert (newsize
>= size
);
632 fragS
*last
= seginfo
->frchainP
->frch_last
;
633 fragp
= seginfo
->frchainP
->frch_root
;
634 while (fragp
->fr_next
!= last
)
635 fragp
= fragp
->fr_next
;
636 last
->fr_address
= size
;
637 fragp
->fr_offset
+= newsize
- size
;
640 #ifdef tc_frob_section
641 tc_frob_section (sec
);
643 #ifdef obj_frob_section
644 obj_frob_section (sec
);
650 dump_section_relocs (abfd
, sec
, stream_
)
651 bfd
*abfd ATTRIBUTE_UNUSED
;
655 FILE *stream
= (FILE *) stream_
;
656 segment_info_type
*seginfo
= seg_info (sec
);
657 fixS
*fixp
= seginfo
->fix_root
;
662 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
665 symbolS
*s
= fixp
->fx_addsy
;
667 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
668 (int) fixp
->fx_r_type
);
670 fprintf (stream
, "no sym\n");
673 print_symbol_value_1 (stream
, s
);
674 fprintf (stream
, "\n");
676 fixp
= fixp
->fx_next
;
680 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
683 #ifndef EMIT_SECTION_SYMBOLS
684 #define EMIT_SECTION_SYMBOLS 1
688 adjust_reloc_syms (abfd
, sec
, xxx
)
689 bfd
*abfd ATTRIBUTE_UNUSED
;
691 PTR xxx ATTRIBUTE_UNUSED
;
693 segment_info_type
*seginfo
= seg_info (sec
);
699 dump_section_relocs (abfd
, sec
, stderr
);
701 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
704 else if (fixp
->fx_addsy
)
710 fprintf (stderr
, "\n\nadjusting fixup:\n");
714 sym
= fixp
->fx_addsy
;
716 /* All symbols should have already been resolved at this
717 point. It is possible to see unresolved expression
718 symbols, though, since they are not in the regular symbol
721 resolve_symbol_value (sym
, 1);
723 if (fixp
->fx_subsy
!= NULL
)
724 resolve_symbol_value (fixp
->fx_subsy
, 1);
726 /* If this symbol is equated to an undefined symbol, convert
727 the fixup to being against that symbol. */
728 if (sym
!= NULL
&& symbol_equated_p (sym
)
729 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
731 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
732 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
733 fixp
->fx_addsy
= sym
;
736 if (sym
!= NULL
&& symbol_mri_common_p (sym
))
738 /* These symbols are handled specially in fixup_segment. */
742 symsec
= S_GET_SEGMENT (sym
);
747 if (bfd_is_abs_section (symsec
))
749 /* The fixup_segment routine will not use this symbol in a
750 relocation unless TC_FORCE_RELOCATION returns 1. */
751 if (TC_FORCE_RELOCATION (fixp
))
753 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
754 #ifdef UNDEFINED_DIFFERENCE_OK
755 if (fixp
->fx_subsy
!= NULL
)
756 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
762 /* If it's one of these sections, assume the symbol is
763 definitely going to be output. The code in
764 md_estimate_size_before_relax in tc-mips.c uses this test
765 as well, so if you change this code you should look at that
767 if (bfd_is_und_section (symsec
)
768 || bfd_is_com_section (symsec
))
770 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
771 #ifdef UNDEFINED_DIFFERENCE_OK
772 /* We have the difference of an undefined symbol and some
773 other symbol. Make sure to mark the other symbol as used
774 in a relocation so that it will always be output. */
776 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
781 /* Don't try to reduce relocs which refer to non-local symbols
782 in .linkonce sections. It can lead to confusion when a
783 debugging section refers to a .linkonce section. I hope
784 this will always be correct. */
785 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
791 if ((bfd_get_section_flags (stdoutput
, symsec
) & SEC_LINK_ONCE
)
796 /* The GNU toolchain uses an extension for ELF: a section
797 beginning with the magic string .gnu.linkonce is a
799 if (strncmp (segment_name (symsec
), ".gnu.linkonce",
800 sizeof ".gnu.linkonce" - 1) == 0)
806 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
807 #ifdef UNDEFINED_DIFFERENCE_OK
808 if (fixp
->fx_subsy
!= NULL
)
809 symbol_mark_used_in_reloc (fixp
->fx_subsy
);
815 /* Since we're reducing to section symbols, don't attempt to reduce
816 anything that's already using one. */
817 if (symbol_section_p (sym
))
819 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
824 /* We can never adjust a reloc against a weak symbol. If we
825 did, and the weak symbol was overridden by a real symbol
826 somewhere else, then our relocation would be pointing at
827 the wrong area of memory. */
830 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
835 /* Is there some other reason we can't adjust this one? (E.g.,
836 call/bal links in i960-bout symbols.) */
837 #ifdef obj_fix_adjustable
838 if (! obj_fix_adjustable (fixp
))
840 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
845 /* Is there some other (target cpu dependent) reason we can't adjust
846 this one? (E.g. relocations involving function addresses on
848 #ifdef tc_fix_adjustable
849 if (! tc_fix_adjustable (fixp
))
851 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
856 /* If the section symbol isn't going to be output, the relocs
857 at least should still work. If not, figure out what to do
858 when we run into that case.
860 We refetch the segment when calling section_symbol, rather
861 than using symsec, because S_GET_VALUE may wind up changing
862 the section when it calls resolve_symbol_value. */
863 fixp
->fx_offset
+= S_GET_VALUE (sym
);
864 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
865 symbol_mark_used_in_reloc (fixp
->fx_addsy
);
867 fprintf (stderr
, "\nadjusted fixup:\n");
874 #if 1/*def RELOC_REQUIRES_SYMBOL*/
877 /* There was no symbol required by this relocation. However,
878 BFD doesn't really handle relocations without symbols well.
879 (At least, the COFF support doesn't.) So for now we fake up
880 a local symbol in the absolute section. */
882 fixp
->fx_addsy
= section_symbol (absolute_section
);
883 /* fixp->fx_addsy->sy_used_in_reloc = 1; */
887 dump_section_relocs (abfd
, sec
, stderr
);
891 write_relocs (abfd
, sec
, xxx
)
894 PTR xxx ATTRIBUTE_UNUSED
;
896 segment_info_type
*seginfo
= seg_info (sec
);
903 /* If seginfo is NULL, we did not create this section; don't do
908 fixup_segment (seginfo
->fix_root
, sec
);
911 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
914 #ifndef RELOC_EXPANSION_POSSIBLE
915 /* Set up reloc information as well. */
916 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
917 memset ((char*)relocs
, 0, n
* sizeof (arelent
*));
920 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
923 bfd_reloc_status_type s
;
932 /* If this is an undefined symbol which was equated to another
933 symbol, then use generate the reloc against the latter symbol
934 rather than the former. */
935 sym
= fixp
->fx_addsy
;
936 while (symbol_equated_p (sym
)
937 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
941 /* We must avoid looping, as that can occur with a badly
943 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
946 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
949 fixp
->fx_addsy
= sym
;
951 reloc
= tc_gen_reloc (sec
, fixp
);
959 /* This test is triggered inappropriately for the SH. */
960 if (fixp
->fx_where
+ fixp
->fx_size
961 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
965 s
= bfd_install_relocation (stdoutput
, reloc
,
966 fixp
->fx_frag
->fr_literal
,
967 fixp
->fx_frag
->fr_address
,
973 case bfd_reloc_overflow
:
974 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation overflow"));
976 case bfd_reloc_outofrange
:
977 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, _("relocation out of range"));
980 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
981 fixp
->fx_file
, fixp
->fx_line
, s
);
986 n
= n
* MAX_RELOC_EXPANSION
;
987 /* Set up reloc information as well. */
988 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
991 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
995 bfd_reloc_status_type s
;
1005 /* If this is an undefined symbol which was equated to another
1006 symbol, then use generate the reloc against the latter symbol
1007 rather than the former. */
1008 sym
= fixp
->fx_addsy
;
1009 while (symbol_equated_p (sym
)
1010 && (! S_IS_DEFINED (sym
) || S_IS_COMMON (sym
)))
1011 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
1012 fixp
->fx_addsy
= sym
;
1014 reloc
= tc_gen_reloc (sec
, fixp
);
1016 for (j
= 0; reloc
[j
]; j
++)
1018 relocs
[i
++] = reloc
[j
];
1021 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1022 if (fixp
->fx_where
+ fixp
->fx_size
1023 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1024 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1025 _("internal error: fixup not contained within frag"));
1026 for (j
= 0; reloc
[j
]; j
++)
1028 s
= bfd_install_relocation (stdoutput
, reloc
[j
],
1029 fixp
->fx_frag
->fr_literal
,
1030 fixp
->fx_frag
->fr_address
,
1036 case bfd_reloc_overflow
:
1037 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1038 _("relocation overflow"));
1041 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1042 fixp
->fx_file
, fixp
->fx_line
);
1053 sympp
= bfd_get_outsymbols (stdoutput
);
1054 nsyms
= bfd_get_symcount (stdoutput
);
1055 for (i
= 0; i
< n
; i
++)
1056 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1058 for (j
= 0; j
< nsyms
; j
++)
1059 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
1068 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1070 bfd_set_section_flags (abfd
, sec
,
1071 (bfd_get_section_flags (abfd
, sec
)
1072 & (flagword
) ~SEC_RELOC
));
1074 #ifdef SET_SECTION_RELOCS
1075 SET_SECTION_RELOCS (sec
, relocs
, n
);
1083 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1084 for (i
= 0; i
< n
; i
++)
1087 s
= *r
->sym_ptr_ptr
;
1088 fprintf (stderr
, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1089 i
, r
, r
->address
, s
->name
, r
->addend
);
1096 write_contents (abfd
, sec
, xxx
)
1097 bfd
*abfd ATTRIBUTE_UNUSED
;
1099 PTR xxx ATTRIBUTE_UNUSED
;
1101 segment_info_type
*seginfo
= seg_info (sec
);
1102 unsigned long offset
= 0;
1105 /* Write out the frags. */
1107 || ! (bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1110 for (f
= seginfo
->frchainP
->frch_root
;
1115 unsigned long fill_size
;
1119 assert (f
->fr_type
== rs_fill
);
1122 x
= bfd_set_section_contents (stdoutput
, sec
,
1123 f
->fr_literal
, (file_ptr
) offset
,
1124 (bfd_size_type
) f
->fr_fix
);
1127 bfd_perror (stdoutput
->filename
);
1128 as_perror (_("FATAL: Can't write %s"), stdoutput
->filename
);
1129 exit (EXIT_FAILURE
);
1131 offset
+= f
->fr_fix
;
1133 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1134 fill_size
= f
->fr_var
;
1135 count
= f
->fr_offset
;
1136 assert (count
>= 0);
1137 if (fill_size
&& count
)
1140 if (fill_size
> sizeof(buf
))
1142 /* Do it the old way. Can this ever happen? */
1145 x
= bfd_set_section_contents (stdoutput
, sec
,
1148 (bfd_size_type
) fill_size
);
1151 bfd_perror (stdoutput
->filename
);
1152 as_perror (_("FATAL: Can't write %s"), stdoutput
->filename
);
1153 exit (EXIT_FAILURE
);
1155 offset
+= fill_size
;
1160 /* Build a buffer full of fill objects and output it as
1161 often as necessary. This saves on the overhead of
1162 potentially lots of bfd_set_section_contents calls. */
1166 n_per_buf
= sizeof (buf
);
1167 memset (buf
, *fill_literal
, n_per_buf
);
1172 n_per_buf
= sizeof(buf
)/fill_size
;
1173 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1174 memcpy(bufp
, fill_literal
, fill_size
);
1176 for (; count
> 0; count
-= n_per_buf
)
1178 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1179 x
= bfd_set_section_contents (stdoutput
, sec
,
1180 buf
, (file_ptr
) offset
,
1181 (bfd_size_type
) n_per_buf
* fill_size
);
1183 as_fatal (_("Cannot write to output file."));
1184 offset
+= n_per_buf
* fill_size
;
1192 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1194 merge_data_into_text ()
1196 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1197 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1198 seg_info (data_section
)->frchainP
->frch_root
;
1199 seg_info (text_section
)->frchainP
->frch_last
=
1200 seg_info (data_section
)->frchainP
->frch_last
;
1201 seg_info (data_section
)->frchainP
= 0;
1205 text_last_frag
->fr_next
= data_frag_root
;
1206 text_last_frag
= data_last_frag
;
1207 data_last_frag
= NULL
;
1208 data_frag_root
= NULL
;
1211 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
1212 tmp
->fx_next
= data_fix_root
;
1213 text_fix_tail
= data_fix_tail
;
1216 text_fix_root
= data_fix_root
;
1217 data_fix_root
= NULL
;
1220 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1222 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1224 relax_and_size_all_segments ()
1228 relax_segment (text_frag_root
, SEG_TEXT
);
1229 relax_segment (data_frag_root
, SEG_DATA
);
1230 relax_segment (bss_frag_root
, SEG_BSS
);
1232 * Now the addresses of frags are correct within the segment.
1235 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
1236 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
1237 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
1240 * Join the 2 segments into 1 huge segment.
1241 * To do this, re-compute every rn_address in the SEG_DATA frags.
1242 * Then join the data frags after the text frags.
1244 * Determine a_data [length of data segment].
1248 register relax_addressT slide
;
1250 know ((text_last_frag
->fr_type
== rs_fill
) && (text_last_frag
->fr_offset
== 0));
1252 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
1253 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
1254 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
1256 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1257 /* For b.out: If the data section has a strict alignment
1258 requirement, its load address in the .o file will be
1259 rounded up from the size of the text section. These
1260 two values are *not* the same! Similarly for the bss
1262 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
1265 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1267 fragP
->fr_address
+= slide
;
1268 } /* for each data frag */
1270 know (text_last_frag
!= 0);
1271 text_last_frag
->fr_next
= data_frag_root
;
1275 H_SET_DATA_SIZE (&headers
, 0);
1279 /* See above comments on b.out data section address. */
1282 if (data_last_frag
== 0)
1283 bss_vma
= H_GET_TEXT_SIZE (&headers
);
1285 bss_vma
= data_last_frag
->fr_address
;
1286 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
1287 bss_address_frag
.fr_address
= bss_vma
;
1289 #else /* ! OBJ_BOUT */
1290 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
1291 H_GET_DATA_SIZE (&headers
));
1293 #endif /* ! OBJ_BOUT */
1295 /* Slide all the frags */
1298 relax_addressT slide
= bss_address_frag
.fr_address
;
1300 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1302 fragP
->fr_address
+= slide
;
1303 } /* for each bss frag */
1307 H_SET_BSS_SIZE (&headers
,
1308 bss_last_frag
->fr_address
- bss_frag_root
->fr_address
);
1310 H_SET_BSS_SIZE (&headers
, 0);
1312 #endif /* ! BFD_ASSEMBLER && ! BFD */
1314 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1316 #ifdef BFD_ASSEMBLER
1324 extern PTR bfd_alloc
PARAMS ((bfd
*, size_t));
1326 /* Count symbols. We can't rely on a count made by the loop in
1327 write_object_file, because *_frob_file may add a new symbol or
1330 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1337 asympp
= (asymbol
**) bfd_alloc (stdoutput
,
1338 nsyms
* sizeof (asymbol
*));
1339 symp
= symbol_rootP
;
1340 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1342 asympp
[i
] = symbol_get_bfdsym (symp
);
1343 symbol_mark_written (symp
);
1348 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1349 assert (result
== true);
1350 symbol_table_frozen
= 1;
1354 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1356 set_segment_vma (abfd
, sec
, xxx
)
1359 PTR xxx ATTRIBUTE_UNUSED
;
1361 static bfd_vma addr
= 0;
1363 bfd_set_section_vma (abfd
, sec
, addr
);
1364 addr
+= bfd_section_size (abfd
, sec
);
1366 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1368 /* Finish the subsegments. After every sub-segment, we fake an
1369 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1370 ".fill 0" because that is the kind of frag that requires least
1371 thought. ".align" frags like to have a following frag since that
1372 makes calculating their intended length trivial. */
1374 #ifndef SUB_SEGMENT_ALIGN
1375 #ifdef BFD_ASSEMBLER
1376 #define SUB_SEGMENT_ALIGN(SEG) (0)
1378 #define SUB_SEGMENT_ALIGN(SEG) (2)
1385 struct frchain
*frchainP
;
1387 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
1389 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
1391 /* This now gets called even if we had errors. In that case,
1392 any alignment is meaningless, and, moreover, will look weird
1393 if we are generating a listing. */
1394 frag_align (had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg
),
1395 subseg_text_p (now_seg
) ? NOP_OPCODE
: 0,
1398 /* frag_align will have left a new frag.
1399 Use this last frag for an empty ".fill".
1401 For this segment ...
1402 Create a last frag. Do not leave a "being filled in frag". */
1404 frag_wane (frag_now
);
1405 frag_now
->fr_fix
= 0;
1406 know (frag_now
->fr_next
== NULL
);
1410 /* Write the object file. */
1413 write_object_file ()
1415 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1416 fragS
*fragP
; /* Track along all frags. */
1419 /* Do we really want to write it? */
1421 int n_warns
, n_errs
;
1422 n_warns
= had_warnings ();
1423 n_errs
= had_errors ();
1424 /* The -Z flag indicates that an object file should be generated,
1425 regardless of warnings and errors. */
1426 if (flag_always_generate_output
)
1428 if (n_warns
|| n_errs
)
1429 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1430 n_errs
, n_errs
== 1 ? "" : "s",
1431 n_warns
, n_warns
== 1 ? "" : "s");
1436 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1437 n_errs
, n_errs
== 1 ? "" : "s",
1438 n_warns
, n_warns
== 1 ? "" : "s");
1443 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1444 a routine to check for the definition of the procedure "_main",
1445 and if so -- fix it up so that it can be program entry point. */
1446 vms_check_for_main ();
1447 #endif /* OBJ_VMS */
1449 /* From now on, we don't care about sub-segments. Build one frag chain
1450 for each segment. Linked thru fr_next. */
1452 #ifdef BFD_ASSEMBLER
1453 /* Remove the sections created by gas for its own purposes. */
1455 asection
**seclist
, *sec
;
1458 seclist
= &stdoutput
->sections
;
1459 while (seclist
&& *seclist
)
1462 while (sec
== reg_section
|| sec
== expr_section
)
1466 stdoutput
->section_count
--;
1471 seclist
= &(*seclist
)->next
;
1474 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1477 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1479 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
1480 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
1481 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
1484 /* We have two segments. If user gave -R flag, then we must put the
1485 data frags into the text segment. Do this before relaxing so
1486 we know to take advantage of -R and make shorter addresses. */
1487 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1488 if (flag_readonly_data_in_text
)
1490 merge_data_into_text ();
1494 #ifdef BFD_ASSEMBLER
1495 bfd_map_over_sections (stdoutput
, relax_and_size_seg
, (char *) 0);
1497 relax_and_size_all_segments ();
1498 #endif /* BFD_ASSEMBLER */
1500 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1501 /* Now that the segments have their final sizes, run through the
1502 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1503 should too. Currently, only DJGPP uses this code, but other
1504 COFF targets may need to execute this too. */
1505 bfd_map_over_sections (stdoutput
, set_segment_vma
, (char *) 0);
1508 #ifndef BFD_ASSEMBLER
1511 * Crawl the symbol chain.
1513 * For each symbol whose value depends on a frag, take the address of
1514 * that frag and subsume it into the value of the symbol.
1515 * After this, there is just one way to lookup a symbol value.
1516 * Values are left in their final state for object file emission.
1517 * We adjust the values of 'L' local symbols, even if we do
1518 * not intend to emit them to the object file, because their values
1519 * are needed for fix-ups.
1521 * Unless we saw a -L flag, remove all symbols that begin with 'L'
1522 * from the symbol chain. (They are still pointed to by the fixes.)
1524 * Count the remaining symbols.
1525 * Assign a symbol number to each symbol.
1526 * Count the number of string-table chars we will emit.
1527 * Put this info into the headers as appropriate.
1530 know (zero_address_frag
.fr_address
== 0);
1531 string_byte_count
= sizeof (string_byte_count
);
1533 obj_crawl_symbol_chain (&headers
);
1535 if (string_byte_count
== sizeof (string_byte_count
))
1536 string_byte_count
= 0;
1538 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1541 * Addresses of frags now reflect addresses we use in the object file.
1542 * Symbol values are correct.
1543 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1544 * Also converting any machine-dependent frags using md_convert_frag();
1546 subseg_change (SEG_TEXT
, 0);
1548 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1550 /* At this point we have linked all the frags into a single
1551 chain. However, cvt_frag_to_fill may call md_convert_frag
1552 which may call fix_new. We need to ensure that fix_new adds
1553 the fixup to the right section. */
1554 if (fragP
== data_frag_root
)
1555 subseg_change (SEG_DATA
, 0);
1557 cvt_frag_to_fill (&headers
, SEG_TEXT
, fragP
);
1559 /* Some assert macros don't work with # directives mixed in. */
1561 if (!(fragP
->fr_next
== NULL
1563 || fragP
->fr_next
== data_frag_root
1565 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
1566 == (fragP
->fr_fix
+ fragP
->fr_offset
* fragP
->fr_var
))))
1570 #endif /* ! BFD_ASSEMBLER */
1572 #ifndef WORKING_DOT_WORD
1574 struct broken_word
*lie
;
1575 struct broken_word
**prevP
;
1577 prevP
= &broken_words
;
1578 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1583 subseg_change (lie
->seg
, lie
->subseg
);
1584 exp
.X_op
= O_subtract
;
1585 exp
.X_add_symbol
= lie
->add
;
1586 exp
.X_op_symbol
= lie
->sub
;
1587 exp
.X_add_number
= lie
->addnum
;
1588 #ifdef BFD_ASSEMBLER
1589 #ifdef TC_CONS_FIX_NEW
1590 TC_CONS_FIX_NEW (lie
->frag
,
1591 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1594 fix_new_exp (lie
->frag
,
1595 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1596 2, &exp
, 0, BFD_RELOC_16
);
1599 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1600 fix_new_exp (lie
->frag
,
1601 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1602 2, &exp
, 0, NO_RELOC
);
1605 fix_new_ns32k_exp (lie
->frag
,
1606 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1607 2, &exp
, 0, 0, 2, 0, 0);
1609 fix_new_exp (lie
->frag
,
1610 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1612 #endif /* TC_NS32K */
1613 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1614 #endif /* BFD_ASSEMBLER */
1615 *prevP
= lie
->next_broken_word
;
1618 prevP
= &(lie
->next_broken_word
);
1620 for (lie
= broken_words
; lie
;)
1622 struct broken_word
*untruth
;
1624 addressT table_addr
;
1625 addressT from_addr
, to_addr
;
1628 subseg_change (lie
->seg
, lie
->subseg
);
1629 fragP
= lie
->dispfrag
;
1631 /* Find out how many broken_words go here. */
1633 for (untruth
= lie
; untruth
&& untruth
->dispfrag
== fragP
; untruth
= untruth
->next_broken_word
)
1634 if (untruth
->added
== 1)
1637 table_ptr
= lie
->dispfrag
->fr_opcode
;
1638 table_addr
= lie
->dispfrag
->fr_address
+ (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
, lie
->add
);
1644 table_ptr
+= md_short_jump_size
;
1645 table_addr
+= md_short_jump_size
;
1647 for (m
= 0; lie
&& lie
->dispfrag
== fragP
; m
++, lie
= lie
->next_broken_word
)
1649 if (lie
->added
== 2)
1651 /* Patch the jump table */
1652 /* This is the offset from ??? to table_ptr+0 */
1653 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1654 #ifdef BFD_ASSEMBLER
1655 to_addr
-= symbol_get_frag (lie
->sub
)->fr_address
;
1657 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1658 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr
, lie
);
1660 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1661 for (untruth
= lie
->next_broken_word
; untruth
&& untruth
->dispfrag
== fragP
; untruth
= untruth
->next_broken_word
)
1663 if (untruth
->use_jump
== lie
)
1664 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1667 /* Install the long jump */
1668 /* this is a long jump from table_ptr+0 to the final target */
1669 from_addr
= table_addr
;
1670 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1671 #ifdef BFD_ASSEMBLER
1672 to_addr
+= symbol_get_frag (lie
->add
)->fr_address
;
1674 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
, lie
->add
);
1675 table_ptr
+= md_long_jump_size
;
1676 table_addr
+= md_long_jump_size
;
1680 #endif /* not WORKING_DOT_WORD */
1682 #ifndef BFD_ASSEMBLER
1685 char *the_object_file
;
1686 long object_file_size
;
1688 * Scan every FixS performing fixups. We had to wait until now to do
1689 * this because md_convert_frag() may have made some fixSs.
1693 subseg_change (SEG_TEXT
, 0);
1694 trsize
= md_reloc_size
* fixup_segment (text_fix_root
, SEG_TEXT
);
1695 subseg_change (SEG_DATA
, 0);
1696 drsize
= md_reloc_size
* fixup_segment (data_fix_root
, SEG_DATA
);
1697 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
1699 /* FIXME move this stuff into the pre-write-hook */
1700 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
1701 H_SET_ENTRY_POINT (&headers
, 0);
1703 obj_pre_write_hook (&headers
); /* extra coff stuff */
1705 object_file_size
= H_GET_FILE_SIZE (&headers
);
1706 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
1708 output_file_create (out_file_name
);
1710 obj_header_append (&next_object_file_charP
, &headers
);
1712 know ((next_object_file_charP
- the_object_file
) == H_GET_HEADER_SIZE (&headers
));
1717 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1719 register long count
;
1720 register char *fill_literal
;
1721 register long fill_size
;
1724 know (fragP
->fr_type
== rs_fill
);
1725 append (&next_object_file_charP
, fragP
->fr_literal
, (unsigned long) fragP
->fr_fix
);
1726 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
1727 fill_size
= fragP
->fr_var
;
1728 know (fragP
->fr_offset
>= 0);
1730 for (count
= fragP
->fr_offset
; count
; count
--)
1732 append (&next_object_file_charP
, fill_literal
, (unsigned long) fill_size
);
1735 } /* for each code frag. */
1737 know ((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE (&headers
) + H_GET_TEXT_SIZE (&headers
) + H_GET_DATA_SIZE (&headers
)));
1742 obj_emit_relocations (&next_object_file_charP
, text_fix_root
, (relax_addressT
) 0);
1743 know ((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE (&headers
) + H_GET_TEXT_SIZE (&headers
) + H_GET_DATA_SIZE (&headers
) + H_GET_TEXT_RELOCATION_SIZE (&headers
)));
1745 /* Make addresses in data relocation directives relative to beginning of
1746 * first data fragment, not end of last text fragment: alignment of the
1747 * start of the data segment may place a gap between the segments.
1749 obj_emit_relocations (&next_object_file_charP
, data_fix_root
, data0_frchainP
->frch_root
->fr_address
);
1751 obj_emit_relocations (&next_object_file_charP
, data_fix_root
, text_last_frag
->fr_address
);
1752 #endif /* TC_I960 */
1754 know ((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE (&headers
) + H_GET_TEXT_SIZE (&headers
) + H_GET_DATA_SIZE (&headers
) + H_GET_TEXT_RELOCATION_SIZE (&headers
) + H_GET_DATA_RELOCATION_SIZE (&headers
)));
1757 * Emit line number entries.
1759 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
1760 know ((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE (&headers
) + H_GET_TEXT_SIZE (&headers
) + H_GET_DATA_SIZE (&headers
) + H_GET_TEXT_RELOCATION_SIZE (&headers
) + H_GET_DATA_RELOCATION_SIZE (&headers
) + H_GET_LINENO_SIZE (&headers
)));
1765 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
1766 know ((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE (&headers
) + H_GET_TEXT_SIZE (&headers
) + H_GET_DATA_SIZE (&headers
) + H_GET_TEXT_RELOCATION_SIZE (&headers
) + H_GET_DATA_RELOCATION_SIZE (&headers
) + H_GET_LINENO_SIZE (&headers
) + H_GET_SYMBOL_TABLE_SIZE (&headers
)));
1772 if (string_byte_count
> 0)
1774 obj_emit_strings (&next_object_file_charP
);
1775 } /* only if we have a string table */
1778 bfd_seek (stdoutput
, 0, 0);
1779 bfd_write (the_object_file
, 1, object_file_size
, stdoutput
);
1782 /* Write the data to the file */
1783 output_file_append (the_object_file
, object_file_size
, out_file_name
);
1784 free (the_object_file
);
1786 } /* non vms output */
1789 * Now do the VMS-dependent part of writing the object file
1791 vms_write_object_file (H_GET_TEXT_SIZE (&headers
),
1792 H_GET_DATA_SIZE (&headers
),
1793 H_GET_BSS_SIZE (&headers
),
1794 text_frag_root
, data_frag_root
);
1795 #endif /* OBJ_VMS */
1796 #else /* BFD_ASSEMBLER */
1798 /* Resolve symbol values. This needs to be done before processing
1804 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1805 resolve_symbol_value (symp
, 1);
1807 resolve_local_symbol_values ();
1811 #ifdef tc_frob_file_before_adjust
1812 tc_frob_file_before_adjust ();
1814 #ifdef obj_frob_file_before_adjust
1815 obj_frob_file_before_adjust ();
1818 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *)0);
1820 /* Set up symbol table, and write it out. */
1825 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1830 if (symbol_mri_common_p (symp
))
1832 if (S_IS_EXTERNAL (symp
))
1833 as_bad (_("%s: global symbols not supported in common sections"),
1835 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1839 name
= S_GET_NAME (symp
);
1842 const char *name2
= decode_local_label_name ((char *)S_GET_NAME (symp
));
1843 /* They only differ if `name' is a fb or dollar local
1845 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
1846 as_bad (_("local label %s is not defined"), name2
);
1849 /* Do it again, because adjust_reloc_syms might introduce
1850 more symbols. They'll probably only be section symbols,
1851 but they'll still need to have the values computed. */
1852 resolve_symbol_value (symp
, 1);
1854 /* Skip symbols which were equated to undefined or common
1856 if (symbol_equated_p (symp
)
1857 && (! S_IS_DEFINED (symp
) || S_IS_COMMON (symp
)))
1859 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1863 /* So far, common symbols have been treated like undefined symbols.
1864 Put them in the common section now. */
1865 if (S_IS_DEFINED (symp
) == 0
1866 && S_GET_VALUE (symp
) != 0)
1867 S_SET_SEGMENT (symp
, bfd_com_section_ptr
);
1869 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1870 S_GET_NAME (symp
), symp
,
1872 symbol_get_bfdsym (symp
)->flags
,
1873 segment_name (S_GET_SEGMENT (symp
)));
1876 #ifdef obj_frob_symbol
1877 obj_frob_symbol (symp
, punt
);
1879 #ifdef tc_frob_symbol
1880 if (! punt
|| symbol_used_in_reloc_p (symp
))
1881 tc_frob_symbol (symp
, punt
);
1884 /* If we don't want to keep this symbol, splice it out of
1885 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1886 want section symbols. Otherwise, we skip local symbols
1887 and symbols that the frob_symbol macros told us to punt,
1888 but we keep such symbols if they are used in relocs. */
1889 if ((! EMIT_SECTION_SYMBOLS
1890 && symbol_section_p (symp
))
1891 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1892 opposites. Sometimes the former checks flags and the
1893 latter examines the name... */
1894 || (!S_IS_EXTERN (symp
)
1895 && (S_IS_LOCAL (symp
) || punt
)
1896 && ! symbol_used_in_reloc_p (symp
)))
1898 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1899 /* After symbol_remove, symbol_next(symp) still returns
1900 the one that came after it in the chain. So we don't
1901 need to do any extra cleanup work here. */
1906 /* Make sure we really got a value for the symbol. */
1907 if (! symbol_resolved_p (symp
))
1909 as_bad (_("can't resolve value for symbol \"%s\""),
1911 symbol_mark_resolved (symp
);
1914 /* Set the value into the BFD symbol. Up til now the value
1915 has only been kept in the gas symbolS struct. */
1916 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
1922 /* Now do any format-specific adjustments to the symbol table, such
1923 as adding file symbols. */
1924 #ifdef tc_adjust_symtab
1925 tc_adjust_symtab ();
1927 #ifdef obj_adjust_symtab
1928 obj_adjust_symtab ();
1931 /* Now that all the sizes are known, and contents correct, we can
1932 start writing to the file. */
1935 /* If *_frob_file changes the symbol value at this point, it is
1936 responsible for moving the changed value into symp->bsym->value
1937 as well. Hopefully all symbol value changing can be done in
1942 #ifdef obj_frob_file
1946 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
1948 #ifdef tc_frob_file_after_relocs
1949 tc_frob_file_after_relocs ();
1951 #ifdef obj_frob_file_after_relocs
1952 obj_frob_file_after_relocs ();
1955 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
1956 #endif /* BFD_ASSEMBLER */
1963 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1968 * After this, all frags in this segment have addresses that are correct
1969 * within the segment. Since segments live in different file addresses,
1970 * these frag addresses may not be the same as final object-file addresses.
1973 #ifdef TC_GENERIC_RELAX_TABLE
1975 static int is_dnrange
PARAMS ((fragS
*, fragS
*));
1977 /* Subroutines of relax_segment. */
1983 for (; f1
; f1
= f1
->fr_next
)
1984 if (f1
->fr_next
== f2
)
1989 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1992 relax_frag (fragP
, stretch
)
1996 const relax_typeS
*this_type
;
1997 const relax_typeS
*start_type
;
1998 relax_substateT next_state
;
1999 relax_substateT this_state
;
2000 long aim
, target
, growth
;
2001 symbolS
*symbolP
= fragP
->fr_symbol
;
2002 long offset
= fragP
->fr_offset
;
2003 /* Recompute was_address by undoing "+= stretch" done by relax_segment. */
2004 unsigned long was_address
= fragP
->fr_address
- stretch
;
2005 unsigned long address
= fragP
->fr_address
;
2006 const relax_typeS
*table
= TC_GENERIC_RELAX_TABLE
;
2008 this_state
= fragP
->fr_subtype
;
2009 start_type
= this_type
= table
+ this_state
;
2014 #ifndef DIFF_EXPR_OK
2015 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2016 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2017 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2018 || (S_GET_SEGMENT (symbolP
) == SEG_BSS
)
2019 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
2021 know (symbolP
->sy_frag
);
2023 know (!(S_GET_SEGMENT (symbolP
) == absolute_section
)
2024 || symbolP
->sy_frag
== &zero_address_frag
);
2025 target
+= S_GET_VALUE (symbolP
) + symbol_get_frag (symbolP
)->fr_address
;
2027 /* If frag has yet to be reached on this pass,
2028 assume it will move by STRETCH just as we did.
2029 If this is not so, it will be because some frag
2030 between grows, and that will force another pass.
2032 Beware zero-length frags.
2034 There should be a faster way to do this. */
2036 if (symbol_get_frag (symbolP
)->fr_address
>= was_address
2037 && is_dnrange (fragP
, symbol_get_frag (symbolP
)))
2043 aim
= target
- address
- fragP
->fr_fix
;
2044 #ifdef TC_PCREL_ADJUST
2045 /* Currently only the ns32k family needs this */
2046 aim
+= TC_PCREL_ADJUST(fragP
);
2048 /* This machine doesn't want to use pcrel_adjust.
2049 In that case, pcrel_adjust should be zero. */
2050 /* assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);*/
2052 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2053 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2058 /* Look backwards. */
2059 for (next_state
= this_type
->rlx_more
; next_state
;)
2060 if (aim
>= this_type
->rlx_backward
)
2064 /* Grow to next state. */
2065 this_state
= next_state
;
2066 this_type
= table
+ this_state
;
2067 next_state
= this_type
->rlx_more
;
2072 /* Look forwards. */
2073 for (next_state
= this_type
->rlx_more
; next_state
;)
2074 if (aim
<= this_type
->rlx_forward
)
2078 /* Grow to next state. */
2079 this_state
= next_state
;
2080 this_type
= table
+ this_state
;
2081 next_state
= this_type
->rlx_more
;
2085 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2087 fragP
->fr_subtype
= this_state
;
2091 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2093 /* Relax_align. Advance location counter to next address that has 'alignment'
2094 lowest order bits all 0s, return size of adjustment made. */
2095 static relax_addressT
2096 relax_align (address
, alignment
)
2097 register relax_addressT address
; /* Address now. */
2098 register int alignment
; /* Alignment (binary). */
2100 relax_addressT mask
;
2101 relax_addressT new_address
;
2103 mask
= ~((~0) << alignment
);
2104 new_address
= (address
+ mask
) & (~mask
);
2105 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2107 /* We must provide lots of padding, so the linker can discard it
2108 when needed. The linker will not add extra space, ever. */
2109 new_address
+= (1 << alignment
);
2111 return (new_address
- address
);
2115 relax_segment (segment_frag_root
, segment
)
2116 struct frag
*segment_frag_root
;
2119 register struct frag
*fragP
;
2120 register relax_addressT address
;
2121 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2122 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
2124 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2125 subseg_change (segment
, 0);
2127 /* For each frag in segment: count and store (a 1st guess of)
2130 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2132 fragP
->fr_address
= address
;
2133 address
+= fragP
->fr_fix
;
2135 switch (fragP
->fr_type
)
2138 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2144 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2146 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2149 if (offset
% fragP
->fr_var
!= 0)
2151 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2152 (unsigned long) offset
, (long) fragP
->fr_var
);
2153 offset
-= (offset
% fragP
->fr_var
);
2162 /* Assume .org is nugatory. It will grow with 1st relax. */
2165 case rs_machine_dependent
:
2166 address
+= md_estimate_size_before_relax (fragP
, segment
);
2169 #ifndef WORKING_DOT_WORD
2170 /* Broken words don't concern us yet */
2171 case rs_broken_word
:
2176 /* Initial guess is always 1; doing otherwise can result in
2177 stable solutions that are larger than the minimum. */
2178 address
+= fragP
->fr_offset
= 1;
2182 address
+= eh_frame_estimate_size_before_relax (fragP
);
2186 BAD_CASE (fragP
->fr_type
);
2188 } /* switch(fr_type) */
2189 } /* for each frag in the segment */
2193 long stretch
; /* May be any size, 0 or negative. */
2194 /* Cumulative number of addresses we have */
2195 /* relaxed this pass. */
2196 /* We may have relaxed more than one address. */
2197 long stretched
; /* Have we stretched on this pass? */
2198 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2199 grew, and another shrank. If a branch instruction doesn't fit anymore,
2200 we could be scrod. */
2204 stretch
= stretched
= 0;
2205 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2208 addressT was_address
;
2212 was_address
= fragP
->fr_address
;
2213 address
= fragP
->fr_address
+= stretch
;
2214 symbolP
= fragP
->fr_symbol
;
2215 offset
= fragP
->fr_offset
;
2217 switch (fragP
->fr_type
)
2219 case rs_fill
: /* .fill never relaxes. */
2223 #ifndef WORKING_DOT_WORD
2224 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2225 for it I do not want to write it. I do not want to have
2226 anything to do with it. This is not the proper way to
2227 implement this misfeature. */
2228 case rs_broken_word
:
2230 struct broken_word
*lie
;
2231 struct broken_word
*untruth
;
2233 /* Yes this is ugly (storing the broken_word pointer
2234 in the symbol slot). Still, this whole chunk of
2235 code is ugly, and I don't feel like doing anything
2236 about it. Think of it as stubbornness in action. */
2238 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2239 lie
&& lie
->dispfrag
== fragP
;
2240 lie
= lie
->next_broken_word
)
2246 offset
= (symbol_get_frag (lie
->add
)->fr_address
2247 + S_GET_VALUE (lie
->add
)
2249 - (symbol_get_frag (lie
->sub
)->fr_address
2250 + S_GET_VALUE (lie
->sub
)));
2251 if (offset
<= -32768 || offset
>= 32767)
2253 if (flag_warn_displacement
)
2256 sprint_value (buf
, (addressT
) lie
->addnum
);
2257 as_warn (_(".word %s-%s+%s didn't fit"),
2258 S_GET_NAME (lie
->add
),
2259 S_GET_NAME (lie
->sub
),
2263 if (fragP
->fr_subtype
== 0)
2265 fragP
->fr_subtype
++;
2266 growth
+= md_short_jump_size
;
2268 for (untruth
= lie
->next_broken_word
;
2269 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2270 untruth
= untruth
->next_broken_word
)
2271 if ((symbol_get_frag (untruth
->add
)
2272 == symbol_get_frag (lie
->add
))
2273 && (S_GET_VALUE (untruth
->add
)
2274 == S_GET_VALUE (lie
->add
)))
2277 untruth
->use_jump
= lie
;
2279 growth
+= md_long_jump_size
;
2284 } /* case rs_broken_word */
2289 addressT oldoff
, newoff
;
2291 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2293 newoff
= relax_align (address
+ fragP
->fr_fix
,
2296 if (fragP
->fr_subtype
!= 0)
2298 if (oldoff
> fragP
->fr_subtype
)
2300 if (newoff
> fragP
->fr_subtype
)
2304 growth
= newoff
- oldoff
;
2310 long target
= offset
;
2315 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2316 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2317 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2318 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
)
2319 || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
2320 know (symbolP
->sy_frag
);
2321 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2322 || (symbolP
->sy_frag
== &zero_address_frag
));
2324 target
+= (S_GET_VALUE (symbolP
)
2325 + symbol_get_frag (symbolP
)->fr_address
);
2326 } /* if we have a symbol */
2328 know (fragP
->fr_next
);
2329 after
= fragP
->fr_next
->fr_address
;
2330 growth
= target
- after
;
2333 /* Growth may be negative, but variable part of frag
2334 cannot have fewer than 0 chars. That is, we can't
2336 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2337 _("attempt to .org backwards ignored"));
2339 /* We've issued an error message. Change the
2340 frag to avoid cascading errors. */
2341 fragP
->fr_type
= rs_align
;
2342 fragP
->fr_subtype
= 0;
2343 fragP
->fr_offset
= 0;
2344 fragP
->fr_fix
= after
- address
;
2348 growth
-= stretch
; /* This is an absolute growth factor */
2355 growth
= S_GET_VALUE (symbolP
);
2356 if (symbol_get_frag (symbolP
) != &zero_address_frag
2357 || S_IS_COMMON (symbolP
)
2358 || ! S_IS_DEFINED (symbolP
))
2359 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2360 _(".space specifies non-absolute value"));
2361 fragP
->fr_symbol
= 0;
2364 as_warn (_(".space or .fill with negative value, ignored"));
2372 case rs_machine_dependent
:
2373 #ifdef md_relax_frag
2374 growth
= md_relax_frag (fragP
, stretch
);
2376 #ifdef TC_GENERIC_RELAX_TABLE
2377 /* The default way to relax a frag is to look through
2378 TC_GENERIC_RELAX_TABLE. */
2379 growth
= relax_frag (fragP
, stretch
);
2380 #endif /* TC_GENERIC_RELAX_TABLE */
2389 value
= resolve_symbol_value (fragP
->fr_symbol
, 0);
2390 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2391 growth
= size
- fragP
->fr_offset
;
2392 fragP
->fr_offset
= size
;
2397 growth
= eh_frame_relax_frag (fragP
);
2401 BAD_CASE (fragP
->fr_type
);
2409 } /* For each frag in the segment. */
2411 while (stretched
); /* Until nothing further to relax. */
2415 * We now have valid fr_address'es for each frag.
2419 * All fr_address's are correct, relative to their own segment.
2420 * We have made all the fixS we will ever make.
2422 } /* relax_segment() */
2424 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2426 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2427 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2432 Go through all the fixS's in a segment and see which ones can be
2433 handled now. (These consist of fixS where we have since discovered
2434 the value of a symbol, or the address of the frag involved.)
2435 For each one, call md_apply_fix to put the fix into the frag data.
2437 Result is a count of how many relocation structs will be needed to
2438 handle the remaining fixS's that we couldn't completely handle here.
2439 These will be output later by emit_relocations(). */
2442 fixup_segment (fixP
, this_segment_type
)
2443 register fixS
*fixP
;
2444 segT this_segment_type
; /* N_TYPE bits for segment. */
2446 long seg_reloc_count
= 0;
2447 symbolS
*add_symbolP
;
2448 symbolS
*sub_symbolP
;
2455 segT add_symbol_segment
= absolute_section
;
2457 /* If the linker is doing the relaxing, we must not do any fixups.
2459 Well, strictly speaking that's not true -- we could do any that are
2460 PC-relative and don't cross regions that could change size. And for the
2461 i960 (the only machine for which we've got a relaxing linker right now),
2462 we might be able to turn callx/callj into bal anyways in cases where we
2463 know the maximum displacement. */
2466 for (; fixP
; fixP
= fixP
->fx_next
)
2468 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2469 return seg_reloc_count
;
2472 for (; fixP
; fixP
= fixP
->fx_next
)
2476 fprintf (stderr
, "\nprocessing fixup:\n");
2481 fragP
= fixP
->fx_frag
;
2483 where
= fixP
->fx_where
;
2484 place
= fragP
->fr_literal
+ where
;
2485 size
= fixP
->fx_size
;
2486 add_symbolP
= fixP
->fx_addsy
;
2487 #ifdef TC_VALIDATE_FIX
2488 TC_VALIDATE_FIX (fixP
, this_segment_type
, skip
);
2490 sub_symbolP
= fixP
->fx_subsy
;
2491 add_number
= fixP
->fx_offset
;
2492 pcrel
= fixP
->fx_pcrel
;
2495 if (add_symbolP
!= NULL
2496 && symbol_mri_common_p (add_symbolP
))
2498 know (add_symbolP
->sy_value
.X_op
== O_symbol
);
2499 add_number
+= S_GET_VALUE (add_symbolP
);
2500 fixP
->fx_offset
= add_number
;
2501 add_symbolP
= fixP
->fx_addsy
=
2502 symbol_get_value_expression (add_symbolP
)->X_add_symbol
;
2506 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2510 resolve_symbol_value (sub_symbolP
, 1);
2511 if (add_symbolP
== NULL
|| add_symbol_segment
== absolute_section
)
2513 if (add_symbolP
!= NULL
)
2515 add_number
+= S_GET_VALUE (add_symbolP
);
2517 fixP
->fx_addsy
= NULL
;
2520 /* It's just -sym */
2521 if (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)
2523 add_number
-= S_GET_VALUE (sub_symbolP
);
2524 fixP
->fx_subsy
= NULL
;
2527 && S_GET_SEGMENT (sub_symbolP
) == this_segment_type
)
2529 /* Should try converting to a constant. */
2534 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2535 _("Negative of non-absolute symbol %s"),
2536 S_GET_NAME (sub_symbolP
));
2538 else if (S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
2539 && SEG_NORMAL (add_symbol_segment
))
2541 /* Difference of 2 symbols from same segment.
2542 Can't make difference of 2 undefineds: 'value' means
2543 something different for N_UNDF. */
2545 /* Makes no sense to use the difference of 2 arbitrary symbols
2546 as the target of a call instruction. */
2548 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2549 _("callj to difference of 2 symbols"));
2550 #endif /* TC_I960 */
2551 add_number
+= S_GET_VALUE (add_symbolP
) -
2552 S_GET_VALUE (sub_symbolP
);
2555 pcrel
= 0; /* No further pcrel processing. */
2557 /* Let the target machine make the final determination
2558 as to whether or not a relocation will be needed to
2559 handle this fixup. */
2560 if (!TC_FORCE_RELOCATION_SECTION (fixP
, this_segment_type
))
2563 fixP
->fx_addsy
= NULL
;
2564 fixP
->fx_subsy
= NULL
;
2569 /* Different segments in subtraction. */
2570 know (!(S_IS_EXTERNAL (sub_symbolP
)
2571 && (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)));
2573 if ((S_GET_SEGMENT (sub_symbolP
) == absolute_section
))
2574 add_number
-= S_GET_VALUE (sub_symbolP
);
2577 else if (S_GET_SEGMENT (sub_symbolP
) == this_segment_type
2578 #if 0 /* Do this even if it's already described as pc-relative. For example,
2579 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
2580 pc-relative mode. */
2585 /* Make it pc-relative. */
2586 add_number
+= (MD_PCREL_FROM_SECTION (fixP
, this_segment_type
)
2587 - S_GET_VALUE (sub_symbolP
));
2594 #ifdef UNDEFINED_DIFFERENCE_OK
2595 /* The PA needs this for PIC code generation. We basically
2596 don't want to do anything if we have the difference of two
2597 symbols at this point. */
2600 /* Leave it alone. */
2603 #ifdef BFD_ASSEMBLER
2604 else if (fixP
->fx_r_type
== BFD_RELOC_GPREL32
2605 || fixP
->fx_r_type
== BFD_RELOC_GPREL16
)
2607 /* Leave it alone. */
2613 sprint_value (buf
, fragP
->fr_address
+ where
);
2614 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2615 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2616 S_GET_NAME (add_symbolP
),
2617 segment_name (S_GET_SEGMENT (add_symbolP
)),
2618 S_GET_NAME (sub_symbolP
),
2619 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2627 if (add_symbol_segment
== this_segment_type
&& pcrel
&& !plt
2628 && TC_RELOC_RTSYM_LOC_FIXUP (fixP
))
2631 * This fixup was made when the symbol's segment was
2632 * SEG_UNKNOWN, but it is now in the local segment.
2633 * So we know how to do the address without relocation.
2636 /* reloc_callj() may replace a 'call' with a 'calls' or a
2637 'bal', in which cases it modifies *fixP as appropriate.
2638 In the case of a 'calls', no further work is required,
2639 and *fixP has been set up to make the rest of the code
2642 #endif /* TC_I960 */
2644 add_number
+= S_GET_VALUE (add_symbolP
);
2645 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2646 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
2648 /* Let the target machine make the final determination
2649 as to whether or not a relocation will be needed to
2650 handle this fixup. */
2651 if (!TC_FORCE_RELOCATION (fixP
))
2654 fixP
->fx_addsy
= NULL
;
2659 if (add_symbol_segment
== absolute_section
2663 /* See comment about reloc_callj() above. */
2665 #endif /* TC_I960 */
2666 add_number
+= S_GET_VALUE (add_symbolP
);
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. */
2672 if (!TC_FORCE_RELOCATION (fixP
))
2674 fixP
->fx_addsy
= NULL
;
2678 else if (add_symbol_segment
== undefined_section
2679 #ifdef BFD_ASSEMBLER
2680 || bfd_is_com_section (add_symbol_segment
)
2685 if ((int) fixP
->fx_bit_fixP
== 13)
2687 /* This is a COBR instruction. They have only a
2688 * 13-bit displacement and are only to be used
2689 * for local branches: flag as error, don't generate
2692 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2693 _("can't use COBR format with external label"));
2694 fixP
->fx_addsy
= NULL
;
2698 #endif /* TC_I960 */
2702 if (S_IS_COMMON (add_symbolP
))
2703 add_number
+= S_GET_VALUE (add_symbolP
);
2704 #endif /* TE_I386AIX */
2705 #endif /* OBJ_COFF */
2711 #if !(defined (TC_V850) && defined (OBJ_ELF))
2712 #if !(defined (TC_M68K) && defined (OBJ_ELF))
2713 #if !(defined (TC_ARM) && defined (OBJ_ELF))
2714 #if !(defined (TC_I960) && defined (OBJ_ELF))
2715 #if !defined (TC_I386) || !(defined (OBJ_ELF) || defined (OBJ_COFF)) || defined (TE_PE)
2716 add_number
+= S_GET_VALUE (add_symbolP
);
2728 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment_type
);
2729 if (add_symbolP
== 0)
2731 #ifndef BFD_ASSEMBLER
2732 fixP
->fx_addsy
= &abs_symbol
;
2734 fixP
->fx_addsy
= section_symbol (absolute_section
);
2736 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2743 #ifdef MD_APPLY_FIX3
2744 md_apply_fix3 (fixP
, &add_number
, this_segment_type
);
2746 #ifdef BFD_ASSEMBLER
2747 md_apply_fix (fixP
, &add_number
);
2749 md_apply_fix (fixP
, add_number
);
2753 #ifndef TC_HANDLES_FX_DONE
2754 /* If the tc-* files haven't been converted, assume it's handling
2755 it the old way, where a null fx_addsy means that the fix has
2756 been applied completely, and no further work is needed. */
2757 if (fixP
->fx_addsy
== 0 && fixP
->fx_pcrel
== 0)
2762 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& size
> 0)
2764 if ((size_t) size
< sizeof (valueT
))
2769 mask
--; /* set all bits to one */
2770 mask
<<= size
* 8 - (fixP
->fx_signed
? 1 : 0);
2771 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
2773 char buf
[50], buf2
[50];
2774 sprint_value (buf
, fragP
->fr_address
+ where
);
2775 if (add_number
> 1000)
2776 sprint_value (buf2
, add_number
);
2778 sprintf (buf2
, "%ld", (long) add_number
);
2779 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2780 _("Value of %s too large for field of %d bytes at %s"),
2782 } /* generic error checking */
2784 #ifdef WARN_SIGNED_OVERFLOW_WORD
2785 /* Warn if a .word value is too large when treated as a signed
2786 number. We already know it is not too negative. This is to
2787 catch over-large switches generated by gcc on the 68k. */
2788 if (!flag_signed_overflow_ok
2790 && add_number
> 0x7fff)
2791 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2792 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2794 (unsigned long) (fragP
->fr_address
+ where
));
2796 } /* not a bit fix */
2798 #ifdef TC_VALIDATE_FIX
2799 skip
: ATTRIBUTE_UNUSED_LABEL
2803 fprintf (stderr
, "result:\n");
2806 } /* For each fixS in this segment. */
2808 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2809 return seg_reloc_count
;
2812 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2815 number_to_chars_bigendian (buf
, val
, n
)
2820 if ((size_t) n
> sizeof (val
) || n
<= 0)
2824 buf
[n
] = val
& 0xff;
2830 number_to_chars_littleendian (buf
, val
, n
)
2835 if ((size_t) n
> sizeof (val
) || n
<= 0)
2839 *buf
++ = val
& 0xff;
2845 write_print_statistics (file
)
2848 fprintf (file
, "fixups: %d\n", n_fixups
);
2852 extern int indent_level
;
2859 fprintf (stderr
, "fix %lx %s:%d", (long) fixp
, fixp
->fx_file
, fixp
->fx_line
);
2861 fprintf (stderr
, " pcrel");
2862 if (fixp
->fx_pcrel_adjust
)
2863 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2864 if (fixp
->fx_im_disp
)
2867 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2869 fprintf (stderr
, " im_disp");
2873 fprintf (stderr
, " tcbit");
2875 fprintf (stderr
, " done");
2876 fprintf (stderr
, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2877 fixp
->fx_size
, (long) fixp
->fx_frag
, (long) fixp
->fx_where
,
2878 (long) fixp
->fx_offset
, (long) fixp
->fx_addnumber
);
2879 #ifdef BFD_ASSEMBLER
2880 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2883 #ifdef NEED_FX_R_TYPE
2884 fprintf (stderr
, " r_type=%d", fixp
->fx_r_type
);
2889 fprintf (stderr
, "\n +<");
2890 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2891 fprintf (stderr
, ">");
2895 fprintf (stderr
, "\n -<");
2896 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2897 fprintf (stderr
, ">");
2899 fprintf (stderr
, "\n");
2900 #ifdef TC_FIX_DATA_PRINT
2901 TC_FIX_DATA_PRINT (stderr
, fixp
);
2905 /* end of write.c */