1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This thing should be set up to do byteordering correctly. But... */
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
31 #ifndef TC_ADJUST_RELOC_COUNT
32 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
35 #ifndef TC_FORCE_RELOCATION
36 #define TC_FORCE_RELOCATION(FIX) \
37 (S_FORCE_RELOC ((FIX)->fx_addsy))
40 #ifndef TC_FORCE_RELOCATION_ABS
41 #define TC_FORCE_RELOCATION_ABS(FIX) \
42 (TC_FORCE_RELOCATION (FIX))
45 #ifndef TC_FORCE_RELOCATION_LOCAL
46 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
49 || TC_FORCE_RELOCATION (FIX))
52 #ifndef TC_FORCE_RELOCATION_SUB_SAME
53 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
57 #ifndef TC_FORCE_RELOCATION_SUB_ABS
58 #define TC_FORCE_RELOCATION_SUB_ABS(FIX) \
59 (S_FORCE_RELOC ((FIX)->fx_subsy))
62 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
64 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) \
65 (S_FORCE_RELOC ((FIX)->fx_subsy))
67 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 1
71 #ifndef TC_VALIDATE_FIX_SUB
72 #ifdef UNDEFINED_DIFFERENCE_OK
73 /* The PA needs this for PIC code generation. */
74 #define TC_VALIDATE_FIX_SUB(FIX) 1
77 #define TC_VALIDATE_FIX_SUB(FIX) \
78 ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
79 || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
81 #define TC_VALIDATE_FIX_SUB(FIX) 0
86 #ifndef TC_LINKRELAX_FIXUP
87 #define TC_LINKRELAX_FIXUP(SEG) 1
90 #ifndef MD_APPLY_SYM_VALUE
91 #define MD_APPLY_SYM_VALUE(FIX) 1
94 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
95 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
98 #ifndef MD_PCREL_FROM_SECTION
99 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
102 #ifndef WORKING_DOT_WORD
103 extern const int md_short_jump_size
;
104 extern const int md_long_jump_size
;
107 /* Used to control final evaluation of expressions. */
108 int finalize_syms
= 0;
110 int symbol_table_frozen
;
112 symbolS
*abs_section_sym
;
114 /* Remember the value of dot when parsing expressions. */
117 void print_fixup
PARAMS ((fixS
*));
120 static void renumber_sections
PARAMS ((bfd
*, asection
*, PTR
));
122 /* We generally attach relocs to frag chains. However, after we have
123 chained these all together into a segment, any relocs we add after
124 that must be attached to a segment. This will include relocs added
125 in md_estimate_size_for_relax, for example. */
126 static int frags_chained
= 0;
129 #ifndef BFD_ASSEMBLER
131 #ifndef MANY_SEGMENTS
132 struct frag
*text_frag_root
;
133 struct frag
*data_frag_root
;
134 struct frag
*bss_frag_root
;
136 struct frag
*text_last_frag
; /* Last frag in segment. */
137 struct frag
*data_last_frag
; /* Last frag in segment. */
138 static struct frag
*bss_last_frag
; /* Last frag in segment. */
142 static object_headers headers
;
145 long string_byte_count
;
146 char *next_object_file_charP
; /* Tracks object file bytes. */
149 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
152 #endif /* BFD_ASSEMBLER */
157 #define RELOC_ENUM enum bfd_reloc_code_real
159 #define RELOC_ENUM int
162 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, int size
,
163 symbolS
*add
, symbolS
*sub
,
164 offsetT offset
, int pcrel
,
166 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
167 static long fixup_segment
PARAMS ((fixS
*, segT
));
169 static relax_addressT relax_align
PARAMS ((relax_addressT addr
, int align
));
170 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
171 static fragS
*chain_frchains_together_1
PARAMS ((segT
, struct frchain
*));
174 static void chain_frchains_together
PARAMS ((bfd
*, segT
, PTR
));
175 static void cvt_frag_to_fill
PARAMS ((segT
, fragS
*));
176 static void adjust_reloc_syms
PARAMS ((bfd
*, asection
*, PTR
));
177 static void fix_segment
PARAMS ((bfd
*, asection
*, PTR
));
178 static void write_relocs
PARAMS ((bfd
*, asection
*, PTR
));
179 static void write_contents
PARAMS ((bfd
*, asection
*, PTR
));
180 static void set_symtab
PARAMS ((void));
182 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
183 static void merge_data_into_text
PARAMS ((void));
185 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
186 static void cvt_frag_to_fill
PARAMS ((object_headers
*, segT
, fragS
*));
187 static void remove_subsegs
PARAMS ((frchainS
*, int, fragS
**, fragS
**));
188 static void relax_and_size_all_segments
PARAMS ((void));
190 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
191 static void set_segment_vma
PARAMS ((bfd
*, asection
*, PTR
));
194 /* Create a fixS in obstack 'notes'. */
197 fix_new_internal (frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
,
199 fragS
*frag
; /* Which frag? */
200 int where
; /* Where in that frag? */
201 int size
; /* 1, 2, or 4 usually. */
202 symbolS
*add_symbol
; /* X_add_symbol. */
203 symbolS
*sub_symbol
; /* X_op_symbol. */
204 offsetT offset
; /* X_add_number. */
205 int pcrel
; /* TRUE if PC-relative relocation. */
206 RELOC_ENUM r_type ATTRIBUTE_UNUSED
; /* Relocation type. */
212 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
214 fixP
->fx_frag
= frag
;
215 fixP
->fx_where
= where
;
216 fixP
->fx_size
= size
;
217 /* We've made fx_size a narrow field; check that it's wide enough. */
218 if (fixP
->fx_size
!= size
)
220 as_bad (_("field fx_size too small to hold %d"), size
);
223 fixP
->fx_addsy
= add_symbol
;
224 fixP
->fx_subsy
= sub_symbol
;
225 fixP
->fx_offset
= offset
;
226 fixP
->fx_dot_value
= dot_value
;
227 fixP
->fx_pcrel
= pcrel
;
229 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
230 fixP
->fx_r_type
= r_type
;
232 fixP
->fx_im_disp
= 0;
233 fixP
->fx_pcrel_adjust
= 0;
234 fixP
->fx_bit_fixP
= 0;
235 fixP
->fx_addnumber
= 0;
238 fixP
->fx_no_overflow
= 0;
242 fixP
->fx_cgen
.insn
= NULL
;
243 fixP
->fx_cgen
.opinfo
= 0;
247 TC_INIT_FIX_DATA (fixP
);
250 as_where (&fixP
->fx_file
, &fixP
->fx_line
);
252 /* Usually, we want relocs sorted numerically, but while
253 comparing to older versions of gas that have relocs
254 reverse sorted, it is convenient to have this compile
255 time option. xoxorich. */
259 fixS
**seg_fix_rootP
= (frags_chained
260 ? &seg_info (now_seg
)->fix_root
261 : &frchain_now
->fix_root
);
262 fixS
**seg_fix_tailP
= (frags_chained
263 ? &seg_info (now_seg
)->fix_tail
264 : &frchain_now
->fix_tail
);
267 #ifdef REVERSE_SORT_RELOCS
269 fixP
->fx_next
= *seg_fix_rootP
;
270 *seg_fix_rootP
= fixP
;
272 #else /* REVERSE_SORT_RELOCS */
274 fixP
->fx_next
= NULL
;
277 (*seg_fix_tailP
)->fx_next
= fixP
;
279 *seg_fix_rootP
= fixP
;
280 *seg_fix_tailP
= fixP
;
282 #endif /* REVERSE_SORT_RELOCS */
288 /* Create a fixup relative to a symbol (plus a constant). */
291 fix_new (frag
, where
, size
, add_symbol
, offset
, pcrel
, r_type
)
292 fragS
*frag
; /* Which frag? */
293 int where
; /* Where in that frag? */
294 int size
; /* 1, 2, or 4 usually. */
295 symbolS
*add_symbol
; /* X_add_symbol. */
296 offsetT offset
; /* X_add_number. */
297 int pcrel
; /* TRUE if PC-relative relocation. */
298 RELOC_ENUM r_type
; /* Relocation type. */
300 return fix_new_internal (frag
, where
, size
, add_symbol
,
301 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
304 /* Create a fixup for an expression. Currently we only support fixups
305 for difference expressions. That is itself more than most object
306 file formats support anyhow. */
309 fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
)
310 fragS
*frag
; /* Which frag? */
311 int where
; /* Where in that frag? */
312 int size
; /* 1, 2, or 4 usually. */
313 expressionS
*exp
; /* Expression. */
314 int pcrel
; /* TRUE if PC-relative relocation. */
315 RELOC_ENUM r_type
; /* Relocation type. */
327 as_bad (_("register value used as expression"));
331 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
332 the difference expression cannot immediately be reduced. */
334 symbolS
*stmp
= make_expr_symbol (exp
);
336 exp
->X_op
= O_symbol
;
337 exp
->X_op_symbol
= 0;
338 exp
->X_add_symbol
= stmp
;
339 exp
->X_add_number
= 0;
341 return fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
);
345 add
= exp
->X_add_symbol
;
346 off
= exp
->X_add_number
;
348 #if defined(BFD_ASSEMBLER)
349 r_type
= BFD_RELOC_RVA
;
351 #if defined(TC_RVA_RELOC)
352 r_type
= TC_RVA_RELOC
;
354 as_fatal (_("rva not supported"));
360 sub
= exp
->X_add_symbol
;
361 off
= exp
->X_add_number
;
365 sub
= exp
->X_op_symbol
;
368 add
= exp
->X_add_symbol
;
371 off
= exp
->X_add_number
;
375 add
= make_expr_symbol (exp
);
379 return fix_new_internal (frag
, where
, size
, add
, sub
, off
, pcrel
, r_type
);
382 /* Append a string onto another string, bumping the pointer along. */
384 append (charPP
, fromP
, length
)
387 unsigned long length
;
389 /* Don't trust memcpy() of 0 chars. */
393 memcpy (*charPP
, fromP
, length
);
397 #ifndef BFD_ASSEMBLER
398 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
401 /* This routine records the largest alignment seen for each segment.
402 If the beginning of the segment is aligned on the worst-case
403 boundary, all of the other alignments within it will work. At
404 least one object format really uses this info. */
407 record_alignment (seg
, align
)
408 /* Segment to which alignment pertains. */
410 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
414 if (seg
== absolute_section
)
417 if ((unsigned int) align
> bfd_get_section_alignment (stdoutput
, seg
))
418 bfd_set_section_alignment (stdoutput
, seg
, align
);
420 if (align
> section_alignment
[(int) seg
])
421 section_alignment
[(int) seg
] = align
;
426 get_recorded_alignment (seg
)
429 if (seg
== absolute_section
)
432 return bfd_get_section_alignment (stdoutput
, seg
);
434 return section_alignment
[(int) seg
];
440 /* Reset the section indices after removing the gas created sections. */
443 renumber_sections (abfd
, sec
, countparg
)
444 bfd
*abfd ATTRIBUTE_UNUSED
;
448 int *countp
= (int *) countparg
;
450 sec
->index
= *countp
;
454 #endif /* defined (BFD_ASSEMBLER) */
456 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
459 chain_frchains_together_1 (section
, frchp
)
461 struct frchain
*frchp
;
463 fragS dummy
, *prev_frag
= &dummy
;
465 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
468 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
470 prev_frag
->fr_next
= frchp
->frch_root
;
471 prev_frag
= frchp
->frch_last
;
472 assert (prev_frag
->fr_type
!= 0);
474 if (frchp
->fix_root
!= (fixS
*) NULL
)
476 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
477 seg_info (section
)->fix_root
= frchp
->fix_root
;
478 prev_fix
->fx_next
= frchp
->fix_root
;
479 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
480 prev_fix
= frchp
->fix_tail
;
484 assert (prev_frag
->fr_type
!= 0);
485 prev_frag
->fr_next
= 0;
494 chain_frchains_together (abfd
, section
, xxx
)
495 bfd
*abfd ATTRIBUTE_UNUSED
;
497 PTR xxx ATTRIBUTE_UNUSED
;
499 segment_info_type
*info
;
501 /* BFD may have introduced its own sections without using
502 subseg_new, so it is possible that seg_info is NULL. */
503 info
= seg_info (section
);
504 if (info
!= (segment_info_type
*) NULL
)
505 info
->frchainP
->frch_last
506 = chain_frchains_together_1 (section
, info
->frchainP
);
508 /* Now that we've chained the frags together, we must add new fixups
509 to the segment, not to the frag chain. */
515 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
518 remove_subsegs (head
, seg
, root
, last
)
524 *root
= head
->frch_root
;
525 *last
= chain_frchains_together_1 (seg
, head
);
530 #if defined (BFD_ASSEMBLER) || !defined (BFD)
534 cvt_frag_to_fill (sec
, fragP
)
535 segT sec ATTRIBUTE_UNUSED
;
539 cvt_frag_to_fill (headersP
, sec
, fragP
)
540 object_headers
*headersP
;
545 switch (fragP
->fr_type
)
553 HANDLE_ALIGN (fragP
);
555 know (fragP
->fr_next
!= NULL
);
556 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
558 - fragP
->fr_fix
) / fragP
->fr_var
;
559 if (fragP
->fr_offset
< 0)
561 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
562 _("attempt to .org/.space backwards? (%ld)"),
563 (long) fragP
->fr_offset
);
564 fragP
->fr_offset
= 0;
566 fragP
->fr_type
= rs_fill
;
574 valueT value
= S_GET_VALUE (fragP
->fr_symbol
);
577 size
= output_leb128 (fragP
->fr_literal
+ fragP
->fr_fix
, value
,
580 fragP
->fr_fix
+= size
;
581 fragP
->fr_type
= rs_fill
;
583 fragP
->fr_offset
= 0;
584 fragP
->fr_symbol
= NULL
;
589 eh_frame_convert_frag (fragP
);
593 dwarf2dbg_convert_frag (fragP
);
596 case rs_machine_dependent
:
598 md_convert_frag (stdoutput
, sec
, fragP
);
600 md_convert_frag (headersP
, sec
, fragP
);
603 assert (fragP
->fr_next
== NULL
604 || ((offsetT
) (fragP
->fr_next
->fr_address
- fragP
->fr_address
)
607 /* After md_convert_frag, we make the frag into a ".space 0".
608 md_convert_frag() should set up any fixSs and constants
613 #ifndef WORKING_DOT_WORD
616 struct broken_word
*lie
;
618 if (fragP
->fr_subtype
)
620 fragP
->fr_fix
+= md_short_jump_size
;
621 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
622 lie
&& lie
->dispfrag
== fragP
;
623 lie
= lie
->next_broken_word
)
625 fragP
->fr_fix
+= md_long_jump_size
;
633 BAD_CASE (fragP
->fr_type
);
638 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
641 static void relax_seg
PARAMS ((bfd
*, asection
*, PTR
));
643 relax_seg (abfd
, sec
, xxx
)
644 bfd
*abfd ATTRIBUTE_UNUSED
;
648 segment_info_type
*seginfo
= seg_info (sec
);
650 if (seginfo
&& seginfo
->frchainP
651 && relax_segment (seginfo
->frchainP
->frch_root
, sec
))
653 int *result
= (int *) xxx
;
658 static void size_seg
PARAMS ((bfd
*, asection
*, PTR
));
660 size_seg (abfd
, sec
, xxx
)
663 PTR xxx ATTRIBUTE_UNUSED
;
667 segment_info_type
*seginfo
;
669 valueT size
, newsize
;
671 subseg_change (sec
, 0);
673 seginfo
= seg_info (sec
);
674 if (seginfo
&& seginfo
->frchainP
)
676 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
677 cvt_frag_to_fill (sec
, fragp
);
678 for (fragp
= seginfo
->frchainP
->frch_root
;
680 fragp
= fragp
->fr_next
)
681 /* Walk to last elt. */
683 size
= fragp
->fr_address
+ fragp
->fr_fix
;
688 flags
= bfd_get_section_flags (abfd
, sec
);
690 if (size
> 0 && ! seginfo
->bss
)
691 flags
|= SEC_HAS_CONTENTS
;
693 /* @@ This is just an approximation. */
694 if (seginfo
&& seginfo
->fix_root
)
698 x
= bfd_set_section_flags (abfd
, sec
, flags
);
701 newsize
= md_section_align (sec
, size
);
702 x
= bfd_set_section_size (abfd
, sec
, newsize
);
705 /* If the size had to be rounded up, add some padding in the last
707 assert (newsize
>= size
);
710 fragS
*last
= seginfo
->frchainP
->frch_last
;
711 fragp
= seginfo
->frchainP
->frch_root
;
712 while (fragp
->fr_next
!= last
)
713 fragp
= fragp
->fr_next
;
714 last
->fr_address
= size
;
715 if ((newsize
- size
) % fragp
->fr_var
== 0)
716 fragp
->fr_offset
+= (newsize
- size
) / fragp
->fr_var
;
718 /* If we hit this abort, it's likely due to subsegs_finish not
719 providing sufficient alignment on the last frag, and the
720 machine dependent code using alignment frags with fr_var
725 #ifdef tc_frob_section
726 tc_frob_section (sec
);
728 #ifdef obj_frob_section
729 obj_frob_section (sec
);
735 dump_section_relocs (abfd
, sec
, stream_
)
736 bfd
*abfd ATTRIBUTE_UNUSED
;
740 FILE *stream
= (FILE *) stream_
;
741 segment_info_type
*seginfo
= seg_info (sec
);
742 fixS
*fixp
= seginfo
->fix_root
;
747 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
750 symbolS
*s
= fixp
->fx_addsy
;
752 fprintf (stream
, " %08lx: type %d ", (unsigned long) fixp
,
753 (int) fixp
->fx_r_type
);
755 fprintf (stream
, "no sym\n");
758 print_symbol_value_1 (stream
, s
);
759 fprintf (stream
, "\n");
761 fixp
= fixp
->fx_next
;
765 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
768 #ifndef EMIT_SECTION_SYMBOLS
769 #define EMIT_SECTION_SYMBOLS 1
772 /* This pass over fixups decides whether symbols can be replaced with
776 adjust_reloc_syms (abfd
, sec
, xxx
)
777 bfd
*abfd ATTRIBUTE_UNUSED
;
779 PTR xxx ATTRIBUTE_UNUSED
;
781 segment_info_type
*seginfo
= seg_info (sec
);
787 dump_section_relocs (abfd
, sec
, stderr
);
789 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
793 else if (fixp
->fx_addsy
)
799 fprintf (stderr
, "\n\nadjusting fixup:\n");
803 sym
= fixp
->fx_addsy
;
805 /* All symbols should have already been resolved at this
806 point. It is possible to see unresolved expression
807 symbols, though, since they are not in the regular symbol
809 resolve_symbol_value (sym
);
811 if (fixp
->fx_subsy
!= NULL
)
812 resolve_symbol_value (fixp
->fx_subsy
);
814 /* If this symbol is equated to an undefined symbol, convert
815 the fixup to being against that symbol. */
816 if (symbol_equated_reloc_p (sym
))
818 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
819 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
820 fixp
->fx_addsy
= sym
;
823 if (symbol_mri_common_p (sym
))
825 /* These symbols are handled specially in fixup_segment. */
829 /* If the symbol is undefined, common, weak, or global (ELF
830 shared libs), we can't replace it with the section symbol. */
831 if (S_FORCE_RELOC (fixp
->fx_addsy
))
834 /* Is there some other (target cpu dependent) reason we can't adjust
835 this one? (E.g. relocations involving function addresses on
837 #ifdef tc_fix_adjustable
838 if (! tc_fix_adjustable (fixp
))
842 /* Since we're reducing to section symbols, don't attempt to reduce
843 anything that's already using one. */
844 if (symbol_section_p (sym
))
847 symsec
= S_GET_SEGMENT (sym
);
851 if (bfd_is_abs_section (symsec
))
853 /* The fixup_segment routine normally will not use this
854 symbol in a relocation. */
858 /* Don't try to reduce relocs which refer to non-local symbols
859 in .linkonce sections. It can lead to confusion when a
860 debugging section refers to a .linkonce section. I hope
861 this will always be correct. */
862 if (symsec
!= sec
&& ! S_IS_LOCAL (sym
))
864 if ((symsec
->flags
& SEC_LINK_ONCE
) != 0
866 /* The GNU toolchain uses an extension for ELF: a
867 section beginning with the magic string
868 .gnu.linkonce is a linkonce section. */
869 && strncmp (segment_name (symsec
), ".gnu.linkonce",
870 sizeof ".gnu.linkonce" - 1) == 0))
874 /* Never adjust a reloc against local symbol in a merge section
875 with non-zero addend. */
876 if ((symsec
->flags
& SEC_MERGE
) != 0 && fixp
->fx_offset
!= 0)
879 /* Never adjust a reloc against TLS local symbol. */
880 if ((symsec
->flags
& SEC_THREAD_LOCAL
) != 0)
883 /* We refetch the segment when calling section_symbol, rather
884 than using symsec, because S_GET_VALUE may wind up changing
885 the section when it calls resolve_symbol_value. */
886 fixp
->fx_offset
+= S_GET_VALUE (sym
);
887 fixp
->fx_addsy
= section_symbol (S_GET_SEGMENT (sym
));
889 fprintf (stderr
, "\nadjusted fixup:\n");
894 dump_section_relocs (abfd
, sec
, stderr
);
898 fix_segment (abfd
, sec
, xxx
)
899 bfd
*abfd ATTRIBUTE_UNUSED
;
901 PTR xxx ATTRIBUTE_UNUSED
;
903 segment_info_type
*seginfo
= seg_info (sec
);
905 fixup_segment (seginfo
->fix_root
, sec
);
909 write_relocs (abfd
, sec
, xxx
)
912 PTR xxx ATTRIBUTE_UNUSED
;
914 segment_info_type
*seginfo
= seg_info (sec
);
921 /* If seginfo is NULL, we did not create this section; don't do
927 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
930 #ifndef RELOC_EXPANSION_POSSIBLE
931 /* Set up reloc information as well. */
932 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
933 memset ((char *) relocs
, 0, n
* sizeof (arelent
*));
936 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
939 bfd_reloc_status_type s
;
948 /* If this is an undefined symbol which was equated to another
949 symbol, then generate the reloc against the latter symbol
950 rather than the former. */
951 sym
= fixp
->fx_addsy
;
952 while (symbol_equated_reloc_p (sym
))
956 /* We must avoid looping, as that can occur with a badly
958 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
961 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
964 fixp
->fx_addsy
= sym
;
966 reloc
= tc_gen_reloc (sec
, fixp
);
974 /* This test is triggered inappropriately for the SH. */
975 if (fixp
->fx_where
+ fixp
->fx_size
976 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
980 s
= bfd_install_relocation (stdoutput
, reloc
,
981 fixp
->fx_frag
->fr_literal
,
982 fixp
->fx_frag
->fr_address
,
988 case bfd_reloc_overflow
:
989 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
990 _("relocation overflow"));
992 case bfd_reloc_outofrange
:
993 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
994 _("relocation out of range"));
997 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
998 fixp
->fx_file
, fixp
->fx_line
, s
);
1000 relocs
[i
++] = reloc
;
1003 n
= n
* MAX_RELOC_EXPANSION
;
1004 /* Set up reloc information as well. */
1005 relocs
= (arelent
**) xmalloc (n
* sizeof (arelent
*));
1008 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
1012 bfd_reloc_status_type s
;
1022 /* If this is an undefined symbol which was equated to another
1023 symbol, then generate the reloc against the latter symbol
1024 rather than the former. */
1025 sym
= fixp
->fx_addsy
;
1026 while (symbol_equated_reloc_p (sym
))
1030 /* We must avoid looping, as that can occur with a badly
1032 n
= symbol_get_value_expression (sym
)->X_add_symbol
;
1035 fixp
->fx_offset
+= symbol_get_value_expression (sym
)->X_add_number
;
1038 fixp
->fx_addsy
= sym
;
1040 reloc
= tc_gen_reloc (sec
, fixp
);
1042 for (j
= 0; reloc
[j
]; j
++)
1044 relocs
[i
++] = reloc
[j
];
1047 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1048 if (fixp
->fx_where
+ fixp
->fx_size
1049 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
1050 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1051 _("internal error: fixup not contained within frag"));
1052 for (j
= 0; reloc
[j
]; j
++)
1054 s
= bfd_install_relocation (stdoutput
, reloc
[j
],
1055 fixp
->fx_frag
->fr_literal
,
1056 fixp
->fx_frag
->fr_address
,
1062 case bfd_reloc_overflow
:
1063 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1064 _("relocation overflow"));
1066 case bfd_reloc_outofrange
:
1067 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1068 _("relocation out of range"));
1071 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1072 fixp
->fx_file
, fixp
->fx_line
, s
);
1083 sympp
= bfd_get_outsymbols (stdoutput
);
1084 nsyms
= bfd_get_symcount (stdoutput
);
1085 for (i
= 0; i
< n
; i
++)
1086 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
1088 for (j
= 0; j
< nsyms
; j
++)
1089 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
1098 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
1100 bfd_set_section_flags (abfd
, sec
,
1101 (bfd_get_section_flags (abfd
, sec
)
1102 & (flagword
) ~SEC_RELOC
));
1104 #ifdef SET_SECTION_RELOCS
1105 SET_SECTION_RELOCS (sec
, relocs
, n
);
1113 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
1114 for (i
= 0; i
< n
; i
++)
1117 s
= *r
->sym_ptr_ptr
;
1118 fprintf (stderr
, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1119 i
, r
, r
->address
, s
->name
, r
->addend
);
1126 write_contents (abfd
, sec
, xxx
)
1127 bfd
*abfd ATTRIBUTE_UNUSED
;
1129 PTR xxx ATTRIBUTE_UNUSED
;
1131 segment_info_type
*seginfo
= seg_info (sec
);
1132 unsigned long offset
= 0;
1135 /* Write out the frags. */
1137 || !(bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
1140 for (f
= seginfo
->frchainP
->frch_root
;
1145 unsigned long fill_size
;
1149 assert (f
->fr_type
== rs_fill
);
1152 x
= bfd_set_section_contents (stdoutput
, sec
,
1153 f
->fr_literal
, (file_ptr
) offset
,
1154 (bfd_size_type
) f
->fr_fix
);
1157 bfd_perror (stdoutput
->filename
);
1158 as_perror (_("FATAL: Can't write %s"), stdoutput
->filename
);
1159 exit (EXIT_FAILURE
);
1161 offset
+= f
->fr_fix
;
1163 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
1164 fill_size
= f
->fr_var
;
1165 count
= f
->fr_offset
;
1166 assert (count
>= 0);
1167 if (fill_size
&& count
)
1170 if (fill_size
> sizeof (buf
))
1172 /* Do it the old way. Can this ever happen? */
1175 x
= bfd_set_section_contents (stdoutput
, sec
,
1178 (bfd_size_type
) fill_size
);
1181 bfd_perror (stdoutput
->filename
);
1182 as_perror (_("FATAL: Can't write %s"),
1183 stdoutput
->filename
);
1184 exit (EXIT_FAILURE
);
1186 offset
+= fill_size
;
1191 /* Build a buffer full of fill objects and output it as
1192 often as necessary. This saves on the overhead of
1193 potentially lots of bfd_set_section_contents calls. */
1197 n_per_buf
= sizeof (buf
);
1198 memset (buf
, *fill_literal
, n_per_buf
);
1203 n_per_buf
= sizeof (buf
) / fill_size
;
1204 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
1205 memcpy (bufp
, fill_literal
, fill_size
);
1207 for (; count
> 0; count
-= n_per_buf
)
1209 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
1210 x
= bfd_set_section_contents
1211 (stdoutput
, sec
, buf
, (file_ptr
) offset
,
1212 (bfd_size_type
) n_per_buf
* fill_size
);
1214 as_fatal (_("cannot write to output file"));
1215 offset
+= n_per_buf
* fill_size
;
1223 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1225 merge_data_into_text ()
1227 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1228 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
1229 seg_info (data_section
)->frchainP
->frch_root
;
1230 seg_info (text_section
)->frchainP
->frch_last
=
1231 seg_info (data_section
)->frchainP
->frch_last
;
1232 seg_info (data_section
)->frchainP
= 0;
1236 text_last_frag
->fr_next
= data_frag_root
;
1237 text_last_frag
= data_last_frag
;
1238 data_last_frag
= NULL
;
1239 data_frag_root
= NULL
;
1242 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
1243 tmp
->fx_next
= data_fix_root
;
1244 text_fix_tail
= data_fix_tail
;
1247 text_fix_root
= data_fix_root
;
1248 data_fix_root
= NULL
;
1251 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1253 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1255 relax_and_size_all_segments ()
1259 relax_segment (text_frag_root
, SEG_TEXT
);
1260 relax_segment (data_frag_root
, SEG_DATA
);
1261 relax_segment (bss_frag_root
, SEG_BSS
);
1263 /* Now the addresses of frags are correct within the segment. */
1264 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
1265 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
1266 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
1268 /* Join the 2 segments into 1 huge segment.
1269 To do this, re-compute every rn_address in the SEG_DATA frags.
1270 Then join the data frags after the text frags.
1272 Determine a_data [length of data segment]. */
1275 register relax_addressT slide
;
1277 know ((text_last_frag
->fr_type
== rs_fill
)
1278 && (text_last_frag
->fr_offset
== 0));
1280 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
1281 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
1282 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
1284 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1285 /* For b.out: If the data section has a strict alignment
1286 requirement, its load address in the .o file will be
1287 rounded up from the size of the text section. These
1288 two values are *not* the same! Similarly for the bss
1290 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
1293 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1294 fragP
->fr_address
+= slide
;
1296 know (text_last_frag
!= 0);
1297 text_last_frag
->fr_next
= data_frag_root
;
1301 H_SET_DATA_SIZE (&headers
, 0);
1305 /* See above comments on b.out data section address. */
1308 if (data_last_frag
== 0)
1309 bss_vma
= H_GET_TEXT_SIZE (&headers
);
1311 bss_vma
= data_last_frag
->fr_address
;
1312 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
1313 bss_address_frag
.fr_address
= bss_vma
;
1315 #else /* ! OBJ_BOUT */
1316 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
1317 H_GET_DATA_SIZE (&headers
));
1319 #endif /* ! OBJ_BOUT */
1321 /* Slide all the frags. */
1324 relax_addressT slide
= bss_address_frag
.fr_address
;
1326 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1327 fragP
->fr_address
+= slide
;
1331 H_SET_BSS_SIZE (&headers
,
1332 bss_last_frag
->fr_address
- bss_frag_root
->fr_address
);
1334 H_SET_BSS_SIZE (&headers
, 0);
1336 #endif /* ! BFD_ASSEMBLER && ! BFD */
1338 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1340 #ifdef BFD_ASSEMBLER
1348 extern PTR bfd_alloc
PARAMS ((bfd
*, bfd_size_type
));
1350 /* Count symbols. We can't rely on a count made by the loop in
1351 write_object_file, because *_frob_file may add a new symbol or
1354 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1360 bfd_size_type amt
= (bfd_size_type
) nsyms
* sizeof (asymbol
*);
1362 asympp
= (asymbol
**) bfd_alloc (stdoutput
, amt
);
1363 symp
= symbol_rootP
;
1364 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1366 asympp
[i
] = symbol_get_bfdsym (symp
);
1367 symbol_mark_written (symp
);
1372 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1373 assert (result
== true);
1374 symbol_table_frozen
= 1;
1378 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1380 set_segment_vma (abfd
, sec
, xxx
)
1383 PTR xxx ATTRIBUTE_UNUSED
;
1385 static bfd_vma addr
= 0;
1387 bfd_set_section_vma (abfd
, sec
, addr
);
1388 addr
+= bfd_section_size (abfd
, sec
);
1390 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1392 /* Finish the subsegments. After every sub-segment, we fake an
1393 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1394 ".fill 0" because that is the kind of frag that requires least
1395 thought. ".align" frags like to have a following frag since that
1396 makes calculating their intended length trivial. */
1398 #ifndef SUB_SEGMENT_ALIGN
1400 /* The last subsegment gets an aligment corresponding to the alignment
1401 of the section. This allows proper nop-filling at the end of
1402 code-bearing sections. */
1403 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1404 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1405 ? get_recorded_alignment (SEG) : 0)
1407 #ifdef BFD_ASSEMBLER
1408 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1410 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 2
1418 struct frchain
*frchainP
;
1420 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
1424 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
1426 /* This now gets called even if we had errors. In that case,
1427 any alignment is meaningless, and, moreover, will look weird
1428 if we are generating a listing. */
1430 alignment
= SUB_SEGMENT_ALIGN (now_seg
, frchainP
);
1432 if (subseg_text_p (now_seg
))
1433 frag_align_code (alignment
, 0);
1435 frag_align (alignment
, 0, 0);
1437 /* frag_align will have left a new frag.
1438 Use this last frag for an empty ".fill".
1440 For this segment ...
1441 Create a last frag. Do not leave a "being filled in frag". */
1442 frag_wane (frag_now
);
1443 frag_now
->fr_fix
= 0;
1444 know (frag_now
->fr_next
== NULL
);
1448 /* Write the object file. */
1451 write_object_file ()
1453 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1454 fragS
*fragP
; /* Track along all frags. */
1457 /* Do we really want to write it? */
1459 int n_warns
, n_errs
;
1460 n_warns
= had_warnings ();
1461 n_errs
= had_errors ();
1462 /* The -Z flag indicates that an object file should be generated,
1463 regardless of warnings and errors. */
1464 if (flag_always_generate_output
)
1466 if (n_warns
|| n_errs
)
1467 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1468 n_errs
, n_errs
== 1 ? "" : "s",
1469 n_warns
, n_warns
== 1 ? "" : "s");
1474 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1475 n_errs
, n_errs
== 1 ? "" : "s",
1476 n_warns
, n_warns
== 1 ? "" : "s");
1481 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1482 a routine to check for the definition of the procedure "_main",
1483 and if so -- fix it up so that it can be program entry point. */
1484 vms_check_for_main ();
1485 #endif /* OBJ_VMS */
1487 /* From now on, we don't care about sub-segments. Build one frag chain
1488 for each segment. Linked thru fr_next. */
1490 #ifdef BFD_ASSEMBLER
1491 /* Remove the sections created by gas for its own purposes. */
1496 seclist
= &stdoutput
->sections
;
1499 if (*seclist
== reg_section
|| *seclist
== expr_section
)
1501 bfd_section_list_remove (stdoutput
, seclist
);
1502 stdoutput
->section_count
--;
1505 seclist
= &(*seclist
)->next
;
1508 bfd_map_over_sections (stdoutput
, renumber_sections
, &i
);
1511 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1513 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
1514 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
1515 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
1518 /* We have two segments. If user gave -R flag, then we must put the
1519 data frags into the text segment. Do this before relaxing so
1520 we know to take advantage of -R and make shorter addresses. */
1521 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1522 if (flag_readonly_data_in_text
)
1524 merge_data_into_text ();
1528 #ifdef BFD_ASSEMBLER
1533 #ifndef WORKING_DOT_WORD
1534 /* We need to reset the markers in the broken word list and
1535 associated frags between calls to relax_segment (via
1536 relax_seg). Since the broken word list is global, we do it
1537 once per round, rather than locally in relax_segment for each
1539 struct broken_word
*brokp
;
1541 for (brokp
= broken_words
;
1542 brokp
!= (struct broken_word
*) NULL
;
1543 brokp
= brokp
->next_broken_word
)
1547 if (brokp
->dispfrag
!= (fragS
*) NULL
1548 && brokp
->dispfrag
->fr_type
== rs_broken_word
)
1549 brokp
->dispfrag
->fr_subtype
= 0;
1554 bfd_map_over_sections (stdoutput
, relax_seg
, &changed
);
1559 /* Note - Most ports will use the default value of
1560 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1561 local symbols to be resolved, removing their frag information.
1562 Some ports however, will not have finished relaxing all of
1563 their frags and will still need the local symbol frag
1564 information. These ports can set
1565 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1566 finalize_syms
= TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
;
1568 bfd_map_over_sections (stdoutput
, size_seg
, (char *) 0);
1570 relax_and_size_all_segments ();
1571 #endif /* BFD_ASSEMBLER */
1573 /* Relaxation has completed. Freeze all syms. */
1576 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1577 /* Now that the segments have their final sizes, run through the
1578 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1579 should too. Currently, only DJGPP uses this code, but other
1580 COFF targets may need to execute this too. */
1581 bfd_map_over_sections (stdoutput
, set_segment_vma
, (char *) 0);
1584 #ifndef BFD_ASSEMBLER
1585 /* Crawl the symbol chain.
1587 For each symbol whose value depends on a frag, take the address of
1588 that frag and subsume it into the value of the symbol.
1589 After this, there is just one way to lookup a symbol value.
1590 Values are left in their final state for object file emission.
1591 We adjust the values of 'L' local symbols, even if we do
1592 not intend to emit them to the object file, because their values
1593 are needed for fix-ups.
1595 Unless we saw a -L flag, remove all symbols that begin with 'L'
1596 from the symbol chain. (They are still pointed to by the fixes.)
1598 Count the remaining symbols.
1599 Assign a symbol number to each symbol.
1600 Count the number of string-table chars we will emit.
1601 Put this info into the headers as appropriate. */
1602 know (zero_address_frag
.fr_address
== 0);
1603 string_byte_count
= sizeof (string_byte_count
);
1605 obj_crawl_symbol_chain (&headers
);
1607 if (string_byte_count
== sizeof (string_byte_count
))
1608 string_byte_count
= 0;
1610 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1612 /* Addresses of frags now reflect addresses we use in the object file.
1613 Symbol values are correct.
1614 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1615 Also converting any machine-dependent frags using md_convert_frag(); */
1616 subseg_change (SEG_TEXT
, 0);
1618 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1620 /* At this point we have linked all the frags into a single
1621 chain. However, cvt_frag_to_fill may call md_convert_frag
1622 which may call fix_new. We need to ensure that fix_new adds
1623 the fixup to the right section. */
1624 if (fragP
== data_frag_root
)
1625 subseg_change (SEG_DATA
, 0);
1627 cvt_frag_to_fill (&headers
, SEG_TEXT
, fragP
);
1629 /* Some assert macros don't work with # directives mixed in. */
1631 if (!(fragP
->fr_next
== NULL
1633 || fragP
->fr_next
== data_frag_root
1635 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
1636 == (fragP
->fr_fix
+ fragP
->fr_offset
* fragP
->fr_var
))))
1640 #endif /* ! BFD_ASSEMBLER */
1642 #ifndef WORKING_DOT_WORD
1644 struct broken_word
*lie
;
1645 struct broken_word
**prevP
;
1647 prevP
= &broken_words
;
1648 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1653 subseg_change (lie
->seg
, lie
->subseg
);
1654 exp
.X_op
= O_subtract
;
1655 exp
.X_add_symbol
= lie
->add
;
1656 exp
.X_op_symbol
= lie
->sub
;
1657 exp
.X_add_number
= lie
->addnum
;
1658 #ifdef BFD_ASSEMBLER
1659 #ifdef TC_CONS_FIX_NEW
1660 TC_CONS_FIX_NEW (lie
->frag
,
1661 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1664 fix_new_exp (lie
->frag
,
1665 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1666 2, &exp
, 0, BFD_RELOC_16
);
1669 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1670 fix_new_exp (lie
->frag
,
1671 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1672 2, &exp
, 0, NO_RELOC
);
1675 fix_new_ns32k_exp (lie
->frag
,
1676 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1677 2, &exp
, 0, 0, 2, 0, 0);
1679 fix_new_exp (lie
->frag
,
1680 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1682 #endif /* TC_NS32K */
1683 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1684 #endif /* BFD_ASSEMBLER */
1685 *prevP
= lie
->next_broken_word
;
1688 prevP
= &(lie
->next_broken_word
);
1690 for (lie
= broken_words
; lie
;)
1692 struct broken_word
*untruth
;
1694 addressT table_addr
;
1695 addressT from_addr
, to_addr
;
1698 subseg_change (lie
->seg
, lie
->subseg
);
1699 fragP
= lie
->dispfrag
;
1701 /* Find out how many broken_words go here. */
1704 untruth
&& untruth
->dispfrag
== fragP
;
1705 untruth
= untruth
->next_broken_word
)
1706 if (untruth
->added
== 1)
1709 table_ptr
= lie
->dispfrag
->fr_opcode
;
1710 table_addr
= (lie
->dispfrag
->fr_address
1711 + (table_ptr
- lie
->dispfrag
->fr_literal
));
1712 /* Create the jump around the long jumps. This is a short
1713 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1714 from_addr
= table_addr
;
1715 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1716 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1718 table_ptr
+= md_short_jump_size
;
1719 table_addr
+= md_short_jump_size
;
1722 lie
&& lie
->dispfrag
== fragP
;
1723 m
++, lie
= lie
->next_broken_word
)
1725 if (lie
->added
== 2)
1727 /* Patch the jump table. */
1728 /* This is the offset from ??? to table_ptr+0. */
1729 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1730 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1731 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr
, lie
);
1733 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1734 for (untruth
= lie
->next_broken_word
;
1735 untruth
&& untruth
->dispfrag
== fragP
;
1736 untruth
= untruth
->next_broken_word
)
1738 if (untruth
->use_jump
== lie
)
1739 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1742 /* Install the long jump. */
1743 /* This is a long jump from table_ptr+0 to the final target. */
1744 from_addr
= table_addr
;
1745 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1746 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
,
1748 table_ptr
+= md_long_jump_size
;
1749 table_addr
+= md_long_jump_size
;
1753 #endif /* not WORKING_DOT_WORD */
1755 #ifndef BFD_ASSEMBLER
1758 char *the_object_file
;
1759 long object_file_size
;
1760 /* Scan every FixS performing fixups. We had to wait until now to
1761 do this because md_convert_frag() may have made some fixSs. */
1764 subseg_change (SEG_TEXT
, 0);
1765 trsize
= md_reloc_size
* fixup_segment (text_fix_root
, SEG_TEXT
);
1766 subseg_change (SEG_DATA
, 0);
1767 drsize
= md_reloc_size
* fixup_segment (data_fix_root
, SEG_DATA
);
1768 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
1770 /* FIXME: Move this stuff into the pre-write-hook. */
1771 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
1772 H_SET_ENTRY_POINT (&headers
, 0);
1774 obj_pre_write_hook (&headers
); /* Extra coff stuff. */
1776 object_file_size
= H_GET_FILE_SIZE (&headers
);
1777 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
1779 output_file_create (out_file_name
);
1781 obj_header_append (&next_object_file_charP
, &headers
);
1783 know ((next_object_file_charP
- the_object_file
)
1784 == H_GET_HEADER_SIZE (&headers
));
1787 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1789 register long count
;
1790 register char *fill_literal
;
1791 register long fill_size
;
1794 know (fragP
->fr_type
== rs_fill
);
1795 append (&next_object_file_charP
, fragP
->fr_literal
,
1796 (unsigned long) fragP
->fr_fix
);
1797 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
1798 fill_size
= fragP
->fr_var
;
1799 know (fragP
->fr_offset
>= 0);
1801 for (count
= fragP
->fr_offset
; count
; count
--)
1802 append (&next_object_file_charP
, fill_literal
,
1803 (unsigned long) fill_size
);
1806 know ((next_object_file_charP
- the_object_file
)
1807 == (H_GET_HEADER_SIZE (&headers
)
1808 + H_GET_TEXT_SIZE (&headers
)
1809 + H_GET_DATA_SIZE (&headers
)));
1811 /* Emit relocations. */
1812 obj_emit_relocations (&next_object_file_charP
, text_fix_root
,
1813 (relax_addressT
) 0);
1814 know ((next_object_file_charP
- the_object_file
)
1815 == (H_GET_HEADER_SIZE (&headers
)
1816 + H_GET_TEXT_SIZE (&headers
)
1817 + H_GET_DATA_SIZE (&headers
)
1818 + H_GET_TEXT_RELOCATION_SIZE (&headers
)));
1820 /* Make addresses in data relocation directives relative to beginning of
1821 first data fragment, not end of last text fragment: alignment of the
1822 start of the data segment may place a gap between the segments. */
1823 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1824 data0_frchainP
->frch_root
->fr_address
);
1826 obj_emit_relocations (&next_object_file_charP
, data_fix_root
,
1827 text_last_frag
->fr_address
);
1828 #endif /* TC_I960 */
1830 know ((next_object_file_charP
- the_object_file
)
1831 == (H_GET_HEADER_SIZE (&headers
)
1832 + H_GET_TEXT_SIZE (&headers
)
1833 + H_GET_DATA_SIZE (&headers
)
1834 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1835 + H_GET_DATA_RELOCATION_SIZE (&headers
)));
1837 /* Emit line number entries. */
1838 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
1839 know ((next_object_file_charP
- the_object_file
)
1840 == (H_GET_HEADER_SIZE (&headers
)
1841 + H_GET_TEXT_SIZE (&headers
)
1842 + H_GET_DATA_SIZE (&headers
)
1843 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1844 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1845 + H_GET_LINENO_SIZE (&headers
)));
1848 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
1849 know ((next_object_file_charP
- the_object_file
)
1850 == (H_GET_HEADER_SIZE (&headers
)
1851 + H_GET_TEXT_SIZE (&headers
)
1852 + H_GET_DATA_SIZE (&headers
)
1853 + H_GET_TEXT_RELOCATION_SIZE (&headers
)
1854 + H_GET_DATA_RELOCATION_SIZE (&headers
)
1855 + H_GET_LINENO_SIZE (&headers
)
1856 + H_GET_SYMBOL_TABLE_SIZE (&headers
)));
1859 if (string_byte_count
> 0)
1860 obj_emit_strings (&next_object_file_charP
);
1863 bfd_seek (stdoutput
, (file_ptr
) 0, 0);
1864 bfd_bwrite (the_object_file
, (bfd_size_type
) object_file_size
, stdoutput
);
1867 /* Write the data to the file. */
1868 output_file_append (the_object_file
, object_file_size
, out_file_name
);
1869 free (the_object_file
);
1873 /* Now do the VMS-dependent part of writing the object file. */
1874 vms_write_object_file (H_GET_TEXT_SIZE (&headers
),
1875 H_GET_DATA_SIZE (&headers
),
1876 H_GET_BSS_SIZE (&headers
),
1877 text_frag_root
, data_frag_root
);
1878 #endif /* OBJ_VMS */
1879 #else /* BFD_ASSEMBLER */
1881 /* Resolve symbol values. This needs to be done before processing
1887 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1888 resolve_symbol_value (symp
);
1890 resolve_local_symbol_values ();
1894 #ifdef tc_frob_file_before_adjust
1895 tc_frob_file_before_adjust ();
1897 #ifdef obj_frob_file_before_adjust
1898 obj_frob_file_before_adjust ();
1901 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *) 0);
1903 #ifdef tc_frob_file_before_fix
1904 tc_frob_file_before_fix ();
1906 #ifdef obj_frob_file_before_fix
1907 obj_frob_file_before_fix ();
1910 bfd_map_over_sections (stdoutput
, fix_segment
, (char *) 0);
1912 /* Set up symbol table, and write it out. */
1917 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1922 if (symbol_mri_common_p (symp
))
1924 if (S_IS_EXTERNAL (symp
))
1925 as_bad (_("%s: global symbols not supported in common sections"),
1927 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1931 name
= S_GET_NAME (symp
);
1935 decode_local_label_name ((char *) S_GET_NAME (symp
));
1936 /* They only differ if `name' is a fb or dollar local
1938 if (name2
!= name
&& ! S_IS_DEFINED (symp
))
1939 as_bad (_("local label `%s' is not defined"), name2
);
1942 /* Do it again, because adjust_reloc_syms might introduce
1943 more symbols. They'll probably only be section symbols,
1944 but they'll still need to have the values computed. */
1945 resolve_symbol_value (symp
);
1947 /* Skip symbols which were equated to undefined or common
1949 if (symbol_equated_reloc_p (symp
))
1951 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1955 /* So far, common symbols have been treated like undefined symbols.
1956 Put them in the common section now. */
1957 if (S_IS_DEFINED (symp
) == 0
1958 && S_GET_VALUE (symp
) != 0)
1959 S_SET_SEGMENT (symp
, bfd_com_section_ptr
);
1961 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1962 S_GET_NAME (symp
), symp
,
1964 symbol_get_bfdsym (symp
)->flags
,
1965 segment_name (S_GET_SEGMENT (symp
)));
1968 #ifdef obj_frob_symbol
1969 obj_frob_symbol (symp
, punt
);
1971 #ifdef tc_frob_symbol
1972 if (! punt
|| symbol_used_in_reloc_p (symp
))
1973 tc_frob_symbol (symp
, punt
);
1976 /* If we don't want to keep this symbol, splice it out of
1977 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1978 want section symbols. Otherwise, we skip local symbols
1979 and symbols that the frob_symbol macros told us to punt,
1980 but we keep such symbols if they are used in relocs. */
1981 if (symp
== abs_section_sym
1982 || (! EMIT_SECTION_SYMBOLS
1983 && symbol_section_p (symp
))
1984 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1985 opposites. Sometimes the former checks flags and the
1986 latter examines the name... */
1987 || (!S_IS_EXTERN (symp
)
1988 && (punt
|| S_IS_LOCAL (symp
))
1989 && ! symbol_used_in_reloc_p (symp
)))
1991 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1993 /* After symbol_remove, symbol_next(symp) still returns
1994 the one that came after it in the chain. So we don't
1995 need to do any extra cleanup work here. */
1999 /* Make sure we really got a value for the symbol. */
2000 if (! symbol_resolved_p (symp
))
2002 as_bad (_("can't resolve value for symbol `%s'"),
2004 symbol_mark_resolved (symp
);
2007 /* Set the value into the BFD symbol. Up til now the value
2008 has only been kept in the gas symbolS struct. */
2009 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
2015 /* Now do any format-specific adjustments to the symbol table, such
2016 as adding file symbols. */
2017 #ifdef tc_adjust_symtab
2018 tc_adjust_symtab ();
2020 #ifdef obj_adjust_symtab
2021 obj_adjust_symtab ();
2024 /* Now that all the sizes are known, and contents correct, we can
2025 start writing to the file. */
2028 /* If *_frob_file changes the symbol value at this point, it is
2029 responsible for moving the changed value into symp->bsym->value
2030 as well. Hopefully all symbol value changing can be done in
2035 #ifdef obj_frob_file
2039 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
2041 #ifdef tc_frob_file_after_relocs
2042 tc_frob_file_after_relocs ();
2044 #ifdef obj_frob_file_after_relocs
2045 obj_frob_file_after_relocs ();
2048 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
2049 #endif /* BFD_ASSEMBLER */
2053 #ifdef TC_GENERIC_RELAX_TABLE
2055 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2058 relax_frag (segment
, fragP
, stretch
)
2063 const relax_typeS
*this_type
;
2064 const relax_typeS
*start_type
;
2065 relax_substateT next_state
;
2066 relax_substateT this_state
;
2072 const relax_typeS
*table
;
2074 target
= fragP
->fr_offset
;
2075 address
= fragP
->fr_address
;
2076 table
= TC_GENERIC_RELAX_TABLE
;
2077 this_state
= fragP
->fr_subtype
;
2078 start_type
= this_type
= table
+ this_state
;
2079 symbolP
= fragP
->fr_symbol
;
2085 sym_frag
= symbol_get_frag (symbolP
);
2087 #ifndef DIFF_EXPR_OK
2088 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2089 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2090 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2091 || (S_GET_SEGMENT (symbolP
) == SEG_BSS
)
2092 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
2094 know (sym_frag
!= NULL
);
2096 know (S_GET_SEGMENT (symbolP
) != absolute_section
2097 || sym_frag
== &zero_address_frag
);
2098 target
+= S_GET_VALUE (symbolP
);
2100 /* If frag has yet to be reached on this pass,
2101 assume it will move by STRETCH just as we did.
2102 If this is not so, it will be because some frag
2103 between grows, and that will force another pass. */
2106 && sym_frag
->relax_marker
!= fragP
->relax_marker
2107 && S_GET_SEGMENT (symbolP
) == segment
)
2113 aim
= target
- address
- fragP
->fr_fix
;
2114 #ifdef TC_PCREL_ADJUST
2115 /* Currently only the ns32k family needs this. */
2116 aim
+= TC_PCREL_ADJUST (fragP
);
2118 /* This machine doesn't want to use pcrel_adjust.
2119 In that case, pcrel_adjust should be zero. */
2121 assert (fragP
->fr_targ
.ns32k
.pcrel_adjust
== 0);
2124 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2125 md_prepare_relax_scan (fragP
, address
, aim
, this_state
, this_type
);
2130 /* Look backwards. */
2131 for (next_state
= this_type
->rlx_more
; next_state
;)
2132 if (aim
>= this_type
->rlx_backward
)
2136 /* Grow to next state. */
2137 this_state
= next_state
;
2138 this_type
= table
+ this_state
;
2139 next_state
= this_type
->rlx_more
;
2144 /* Look forwards. */
2145 for (next_state
= this_type
->rlx_more
; next_state
;)
2146 if (aim
<= this_type
->rlx_forward
)
2150 /* Grow to next state. */
2151 this_state
= next_state
;
2152 this_type
= table
+ this_state
;
2153 next_state
= this_type
->rlx_more
;
2157 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2159 fragP
->fr_subtype
= this_state
;
2163 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2165 /* Relax_align. Advance location counter to next address that has 'alignment'
2166 lowest order bits all 0s, return size of adjustment made. */
2167 static relax_addressT
2168 relax_align (address
, alignment
)
2169 register relax_addressT address
; /* Address now. */
2170 register int alignment
; /* Alignment (binary). */
2172 relax_addressT mask
;
2173 relax_addressT new_address
;
2175 mask
= ~((~0) << alignment
);
2176 new_address
= (address
+ mask
) & (~mask
);
2177 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2179 /* We must provide lots of padding, so the linker can discard it
2180 when needed. The linker will not add extra space, ever. */
2181 new_address
+= (1 << alignment
);
2183 return (new_address
- address
);
2186 /* Now we have a segment, not a crowd of sub-segments, we can make
2191 After this, all frags in this segment have addresses that are correct
2192 within the segment. Since segments live in different file addresses,
2193 these frag addresses may not be the same as final object-file
2197 relax_segment (segment_frag_root
, segment
)
2198 struct frag
*segment_frag_root
;
2201 register struct frag
*fragP
;
2202 register relax_addressT address
;
2205 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2206 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
2208 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2209 subseg_change (segment
, 0);
2211 /* For each frag in segment: count and store (a 1st guess of)
2214 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2216 fragP
->relax_marker
= 0;
2217 fragP
->fr_address
= address
;
2218 address
+= fragP
->fr_fix
;
2220 switch (fragP
->fr_type
)
2223 address
+= fragP
->fr_offset
* fragP
->fr_var
;
2230 addressT offset
= relax_align (address
, (int) fragP
->fr_offset
);
2232 if (fragP
->fr_subtype
!= 0 && offset
> fragP
->fr_subtype
)
2235 if (offset
% fragP
->fr_var
!= 0)
2237 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2238 _("alignment padding (%lu bytes) not a multiple of %ld"),
2239 (unsigned long) offset
, (long) fragP
->fr_var
);
2240 offset
-= (offset
% fragP
->fr_var
);
2249 /* Assume .org is nugatory. It will grow with 1st relax. */
2252 case rs_machine_dependent
:
2253 /* If fr_symbol is an expression, this call to
2254 resolve_symbol_value sets up the correct segment, which will
2255 likely be needed in md_estimate_size_before_relax. */
2256 if (fragP
->fr_symbol
)
2257 resolve_symbol_value (fragP
->fr_symbol
);
2259 address
+= md_estimate_size_before_relax (fragP
, segment
);
2262 #ifndef WORKING_DOT_WORD
2263 /* Broken words don't concern us yet. */
2264 case rs_broken_word
:
2269 /* Initial guess is always 1; doing otherwise can result in
2270 stable solutions that are larger than the minimum. */
2271 address
+= fragP
->fr_offset
= 1;
2275 address
+= eh_frame_estimate_size_before_relax (fragP
);
2279 address
+= dwarf2dbg_estimate_size_before_relax (fragP
);
2283 BAD_CASE (fragP
->fr_type
);
2290 long stretch
; /* May be any size, 0 or negative. */
2291 /* Cumulative number of addresses we have relaxed this pass.
2292 We may have relaxed more than one address. */
2293 int stretched
; /* Have we stretched on this pass? */
2294 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2295 grew, and another shrank. If a branch instruction doesn't fit anymore,
2296 we could be scrod. */
2303 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2306 addressT was_address
;
2310 fragP
->relax_marker
^= 1;
2311 was_address
= fragP
->fr_address
;
2312 address
= fragP
->fr_address
+= stretch
;
2313 symbolP
= fragP
->fr_symbol
;
2314 offset
= fragP
->fr_offset
;
2316 switch (fragP
->fr_type
)
2318 case rs_fill
: /* .fill never relaxes. */
2322 #ifndef WORKING_DOT_WORD
2323 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2324 for it I do not want to write it. I do not want to have
2325 anything to do with it. This is not the proper way to
2326 implement this misfeature. */
2327 case rs_broken_word
:
2329 struct broken_word
*lie
;
2330 struct broken_word
*untruth
;
2332 /* Yes this is ugly (storing the broken_word pointer
2333 in the symbol slot). Still, this whole chunk of
2334 code is ugly, and I don't feel like doing anything
2335 about it. Think of it as stubbornness in action. */
2337 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
2338 lie
&& lie
->dispfrag
== fragP
;
2339 lie
= lie
->next_broken_word
)
2345 offset
= (S_GET_VALUE (lie
->add
)
2347 - S_GET_VALUE (lie
->sub
));
2348 if (offset
<= -32768 || offset
>= 32767)
2350 if (flag_warn_displacement
)
2353 sprint_value (buf
, (addressT
) lie
->addnum
);
2354 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2355 _(".word %s-%s+%s didn't fit"),
2356 S_GET_NAME (lie
->add
),
2357 S_GET_NAME (lie
->sub
),
2361 if (fragP
->fr_subtype
== 0)
2363 fragP
->fr_subtype
++;
2364 growth
+= md_short_jump_size
;
2366 for (untruth
= lie
->next_broken_word
;
2367 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
2368 untruth
= untruth
->next_broken_word
)
2369 if ((symbol_get_frag (untruth
->add
)
2370 == symbol_get_frag (lie
->add
))
2371 && (S_GET_VALUE (untruth
->add
)
2372 == S_GET_VALUE (lie
->add
)))
2375 untruth
->use_jump
= lie
;
2377 growth
+= md_long_jump_size
;
2382 } /* case rs_broken_word */
2388 addressT oldoff
, newoff
;
2390 oldoff
= relax_align (was_address
+ fragP
->fr_fix
,
2392 newoff
= relax_align (address
+ fragP
->fr_fix
,
2395 if (fragP
->fr_subtype
!= 0)
2397 if (oldoff
> fragP
->fr_subtype
)
2399 if (newoff
> fragP
->fr_subtype
)
2403 growth
= newoff
- oldoff
;
2409 addressT target
= offset
;
2414 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2415 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2416 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
2417 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
)
2418 || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
2419 know (symbolP
->sy_frag
);
2420 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
2421 || (symbolP
->sy_frag
== &zero_address_frag
));
2423 /* Convert from an actual address to an octet offset
2424 into the section. Here it is assumed that the
2425 section's VMA is zero, and can omit subtracting it
2426 from the symbol's value to get the address offset. */
2427 know (S_GET_SECTION (symbolP
)->vma
== 0);
2428 target
+= S_GET_VALUE (symbolP
) * OCTETS_PER_BYTE
;
2431 know (fragP
->fr_next
);
2432 after
= fragP
->fr_next
->fr_address
;
2433 growth
= target
- after
;
2436 /* Growth may be negative, but variable part of frag
2437 cannot have fewer than 0 chars. That is, we can't
2439 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2440 _("attempt to .org backwards"));
2442 /* We've issued an error message. Change the
2443 frag to avoid cascading errors. */
2444 fragP
->fr_type
= rs_align
;
2445 fragP
->fr_subtype
= 0;
2446 fragP
->fr_offset
= 0;
2447 fragP
->fr_fix
= after
- address
;
2451 /* This is an absolute growth factor */
2462 amount
= S_GET_VALUE (symbolP
);
2463 if (S_GET_SEGMENT (symbolP
) != absolute_section
2464 || S_IS_COMMON (symbolP
)
2465 || ! S_IS_DEFINED (symbolP
))
2467 as_bad_where (fragP
->fr_file
, fragP
->fr_line
,
2468 _(".space specifies non-absolute value"));
2469 /* Prevent repeat of this error message. */
2470 fragP
->fr_symbol
= 0;
2472 else if (amount
< 0)
2474 as_warn_where (fragP
->fr_file
, fragP
->fr_line
,
2475 _(".space or .fill with negative value, ignored"));
2476 fragP
->fr_symbol
= 0;
2479 growth
= (was_address
+ fragP
->fr_fix
+ amount
2480 - fragP
->fr_next
->fr_address
);
2484 case rs_machine_dependent
:
2485 #ifdef md_relax_frag
2486 growth
= md_relax_frag (segment
, fragP
, stretch
);
2488 #ifdef TC_GENERIC_RELAX_TABLE
2489 /* The default way to relax a frag is to look through
2490 TC_GENERIC_RELAX_TABLE. */
2491 growth
= relax_frag (segment
, fragP
, stretch
);
2492 #endif /* TC_GENERIC_RELAX_TABLE */
2501 value
= resolve_symbol_value (fragP
->fr_symbol
);
2502 size
= sizeof_leb128 (value
, fragP
->fr_subtype
);
2503 growth
= size
- fragP
->fr_offset
;
2504 fragP
->fr_offset
= size
;
2509 growth
= eh_frame_relax_frag (fragP
);
2513 growth
= dwarf2dbg_relax_frag (fragP
);
2517 BAD_CASE (fragP
->fr_type
);
2525 } /* For each frag in the segment. */
2527 while (stretched
); /* Until nothing further to relax. */
2531 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
2532 if (fragP
->last_fr_address
!= fragP
->fr_address
)
2534 fragP
->last_fr_address
= fragP
->fr_address
;
2540 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2544 Go through all the fixS's in a segment and see which ones can be
2545 handled now. (These consist of fixS where we have since discovered
2546 the value of a symbol, or the address of the frag involved.)
2547 For each one, call md_apply_fix3 to put the fix into the frag data.
2549 Result is a count of how many relocation structs will be needed to
2550 handle the remaining fixS's that we couldn't completely handle here.
2551 These will be output later by emit_relocations(). */
2554 fixup_segment (fixP
, this_segment
)
2558 long seg_reloc_count
= 0;
2561 segT add_symbol_segment
= absolute_section
;
2563 if (fixP
!= NULL
&& abs_section_sym
== NULL
)
2565 #ifndef BFD_ASSEMBLER
2566 abs_section_sym
= &abs_symbol
;
2568 abs_section_sym
= section_symbol (absolute_section
);
2572 /* If the linker is doing the relaxing, we must not do any fixups.
2574 Well, strictly speaking that's not true -- we could do any that
2575 are PC-relative and don't cross regions that could change size.
2576 And for the i960 we might be able to turn callx/callj into bal
2577 anyways in cases where we know the maximum displacement. */
2578 if (linkrelax
&& TC_LINKRELAX_FIXUP (this_segment
))
2580 for (; fixP
; fixP
= fixP
->fx_next
)
2583 if (fixP
->fx_addsy
== NULL
)
2585 /* There was no symbol required by this relocation.
2586 However, BFD doesn't really handle relocations
2587 without symbols well. So fake up a local symbol in
2588 the absolute section. */
2589 fixP
->fx_addsy
= abs_section_sym
;
2591 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2592 if (fixP
->fx_subsy
!= NULL
)
2593 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
2596 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2597 return seg_reloc_count
;
2600 for (; fixP
; fixP
= fixP
->fx_next
)
2603 fprintf (stderr
, "\nprocessing fixup:\n");
2607 fragP
= fixP
->fx_frag
;
2609 #ifdef TC_VALIDATE_FIX
2610 TC_VALIDATE_FIX (fixP
, this_segment
, skip
);
2612 add_number
= fixP
->fx_offset
;
2614 if (fixP
->fx_addsy
!= NULL
2615 && symbol_mri_common_p (fixP
->fx_addsy
))
2617 know (fixP
->fx_addsy
->sy_value
.X_op
== O_symbol
);
2618 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2619 fixP
->fx_offset
= add_number
;
2621 = symbol_get_value_expression (fixP
->fx_addsy
)->X_add_symbol
;
2624 if (fixP
->fx_addsy
!= NULL
)
2625 add_symbol_segment
= S_GET_SEGMENT (fixP
->fx_addsy
);
2627 if (fixP
->fx_subsy
!= NULL
)
2629 segT sub_symbol_segment
;
2630 resolve_symbol_value (fixP
->fx_subsy
);
2631 sub_symbol_segment
= S_GET_SEGMENT (fixP
->fx_subsy
);
2632 if (fixP
->fx_addsy
!= NULL
2633 && sub_symbol_segment
== add_symbol_segment
2634 && !TC_FORCE_RELOCATION_SUB_SAME (fixP
, add_symbol_segment
))
2636 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2637 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
2638 fixP
->fx_offset
= add_number
;
2639 /* If the back-end code has selected a pc-relative
2640 reloc, adjust the value to be pc-relative. */
2643 /* See the comment below about 68k weirdness. */
2647 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
2648 fixP
->fx_addsy
= NULL
;
2649 fixP
->fx_subsy
= NULL
;
2652 else if (sub_symbol_segment
== absolute_section
2653 && !TC_FORCE_RELOCATION_SUB_ABS (fixP
))
2655 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
2656 fixP
->fx_offset
= add_number
;
2657 fixP
->fx_subsy
= NULL
;
2659 else if (sub_symbol_segment
== this_segment
2660 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP
))
2662 add_number
-= S_GET_VALUE (fixP
->fx_subsy
);
2663 fixP
->fx_offset
= (add_number
+ fixP
->fx_dot_value
2664 + fixP
->fx_frag
->fr_address
);
2666 /* Make it pc-relative. If the back-end code has not
2667 selected a pc-relative reloc, cancel the adjustment
2668 we do later on all pc-relative relocs. */
2671 /* Do this for m68k even if it's already described
2672 as pc-relative. On the m68k, an operand of
2673 "pc@(foo-.-2)" should address "foo" in a
2674 pc-relative mode. */
2678 add_number
+= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
2679 fixP
->fx_subsy
= NULL
;
2682 else if (!TC_VALIDATE_FIX_SUB (fixP
))
2684 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2685 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2686 fixP
->fx_addsy
? S_GET_NAME (fixP
->fx_addsy
) : "0",
2687 segment_name (add_symbol_segment
),
2688 S_GET_NAME (fixP
->fx_subsy
),
2689 segment_name (sub_symbol_segment
));
2695 if (add_symbol_segment
== this_segment
2696 && !TC_FORCE_RELOCATION_LOCAL (fixP
))
2698 /* This fixup was made when the symbol's segment was
2699 SEG_UNKNOWN, but it is now in the local segment.
2700 So we know how to do the address without relocation. */
2701 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2702 fixP
->fx_offset
= add_number
;
2704 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
2705 fixP
->fx_addsy
= NULL
;
2708 else if (add_symbol_segment
== absolute_section
2709 && !TC_FORCE_RELOCATION_ABS (fixP
))
2711 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2712 fixP
->fx_offset
= add_number
;
2713 fixP
->fx_addsy
= NULL
;
2715 else if (add_symbol_segment
!= undefined_section
2716 #ifdef BFD_ASSEMBLER
2717 && ! bfd_is_com_section (add_symbol_segment
)
2719 && MD_APPLY_SYM_VALUE (fixP
))
2720 add_number
+= S_GET_VALUE (fixP
->fx_addsy
);
2725 add_number
-= MD_PCREL_FROM_SECTION (fixP
, this_segment
);
2726 if (!fixP
->fx_done
&& fixP
->fx_addsy
== NULL
)
2728 /* There was no symbol required by this relocation.
2729 However, BFD doesn't really handle relocations
2730 without symbols well. So fake up a local symbol in
2731 the absolute section. */
2732 fixP
->fx_addsy
= abs_section_sym
;
2737 md_apply_fix3 (fixP
, &add_number
, this_segment
);
2742 if (fixP
->fx_addsy
== NULL
)
2743 fixP
->fx_addsy
= abs_section_sym
;
2744 symbol_mark_used_in_reloc (fixP
->fx_addsy
);
2745 if (fixP
->fx_subsy
!= NULL
)
2746 symbol_mark_used_in_reloc (fixP
->fx_subsy
);
2749 if (!fixP
->fx_bit_fixP
&& !fixP
->fx_no_overflow
&& fixP
->fx_size
!= 0)
2751 if (fixP
->fx_size
< sizeof (valueT
))
2756 mask
--; /* Set all bits to one. */
2757 mask
<<= fixP
->fx_size
* 8 - (fixP
->fx_signed
? 1 : 0);
2758 if ((add_number
& mask
) != 0 && (add_number
& mask
) != mask
)
2760 char buf
[50], buf2
[50];
2761 sprint_value (buf
, fragP
->fr_address
+ fixP
->fx_where
);
2762 if (add_number
> 1000)
2763 sprint_value (buf2
, add_number
);
2765 sprintf (buf2
, "%ld", (long) add_number
);
2766 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2767 _("value of %s too large for field of %d bytes at %s"),
2768 buf2
, fixP
->fx_size
, buf
);
2769 } /* Generic error checking. */
2771 #ifdef WARN_SIGNED_OVERFLOW_WORD
2772 /* Warn if a .word value is too large when treated as a signed
2773 number. We already know it is not too negative. This is to
2774 catch over-large switches generated by gcc on the 68k. */
2775 if (!flag_signed_overflow_ok
2776 && fixP
->fx_size
== 2
2777 && add_number
> 0x7fff)
2778 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2779 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2781 (long) (fragP
->fr_address
+ fixP
->fx_where
));
2783 } /* Not a bit fix. */
2785 #ifdef TC_VALIDATE_FIX
2786 skip
: ATTRIBUTE_UNUSED_LABEL
2790 fprintf (stderr
, "result:\n");
2793 } /* For each fixS in this segment. */
2795 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2796 return seg_reloc_count
;
2799 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2802 number_to_chars_bigendian (buf
, val
, n
)
2811 buf
[n
] = val
& 0xff;
2817 number_to_chars_littleendian (buf
, val
, n
)
2826 *buf
++ = val
& 0xff;
2832 write_print_statistics (file
)
2835 fprintf (file
, "fixups: %d\n", n_fixups
);
2838 /* For debugging. */
2839 extern int indent_level
;
2846 fprintf (stderr
, "fix %lx %s:%d", (long) fixp
, fixp
->fx_file
, fixp
->fx_line
);
2848 fprintf (stderr
, " pcrel");
2849 if (fixp
->fx_pcrel_adjust
)
2850 fprintf (stderr
, " pcrel_adjust=%d", fixp
->fx_pcrel_adjust
);
2851 if (fixp
->fx_im_disp
)
2854 fprintf (stderr
, " im_disp=%d", fixp
->fx_im_disp
);
2856 fprintf (stderr
, " im_disp");
2860 fprintf (stderr
, " tcbit");
2862 fprintf (stderr
, " done");
2863 fprintf (stderr
, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2864 fixp
->fx_size
, (long) fixp
->fx_frag
, (long) fixp
->fx_where
,
2865 (long) fixp
->fx_offset
, (long) fixp
->fx_addnumber
);
2866 #ifdef BFD_ASSEMBLER
2867 fprintf (stderr
, "\n %s (%d)", bfd_get_reloc_code_name (fixp
->fx_r_type
),
2870 #ifdef NEED_FX_R_TYPE
2871 fprintf (stderr
, " r_type=%d", fixp
->fx_r_type
);
2876 fprintf (stderr
, "\n +<");
2877 print_symbol_value_1 (stderr
, fixp
->fx_addsy
);
2878 fprintf (stderr
, ">");
2882 fprintf (stderr
, "\n -<");
2883 print_symbol_value_1 (stderr
, fixp
->fx_subsy
);
2884 fprintf (stderr
, ">");
2886 fprintf (stderr
, "\n");
2887 #ifdef TC_FIX_DATA_PRINT
2888 TC_FIX_DATA_PRINT (stderr
, fixp
);