1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This thing should be set up to do byteordering correctly. But... */
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
31 /* This looks like a good idea. Let's try turning it on always, for now. */
32 #undef BFD_FAST_SECTION_FILL
33 #define BFD_FAST_SECTION_FILL
35 #ifndef TC_ADJUST_RELOC_COUNT
36 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
39 #ifndef TC_FORCE_RELOCATION
40 #define TC_FORCE_RELOCATION(FIXP) 0
43 #ifndef TC_FORCE_RELOCATION_SECTION
44 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
47 #ifndef TC_LINKRELAX_FIXUP
48 #define TC_LINKRELAX_FIXUP(SEG) 1
51 #ifndef TC_FIX_ADJUSTABLE
52 #define TC_FIX_ADJUSTABLE(fix) 1
55 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
56 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
59 #ifndef MD_PCREL_FROM_SECTION
60 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
63 #ifndef WORKING_DOT_WORD
64 extern CONST int md_short_jump_size;
65 extern CONST int md_long_jump_size;
68 /* Used to control final evaluation of expressions. */
69 int finalize_syms = 0;
71 int symbol_table_frozen;
72 void print_fixup PARAMS ((fixS *));
75 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
77 /* We generally attach relocs to frag chains. However, after we have
78 chained these all together into a segment, any relocs we add after
79 that must be attached to a segment. This will include relocs added
80 in md_estimate_size_for_relax, for example. */
81 static int frags_chained = 0;
87 struct frag *text_frag_root;
88 struct frag *data_frag_root;
89 struct frag *bss_frag_root;
91 struct frag *text_last_frag; /* Last frag in segment. */
92 struct frag *data_last_frag; /* Last frag in segment. */
93 static struct frag *bss_last_frag; /* Last frag in segment. */
97 static object_headers headers;
100 long string_byte_count;
101 char *next_object_file_charP; /* Tracks object file bytes. */
104 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
107 #endif /* BFD_ASSEMBLER */
112 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
113 symbolS *add, symbolS *sub,
114 offsetT offset, int pcrel,
115 bfd_reloc_code_real_type r_type));
117 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
118 symbolS *add, symbolS *sub,
119 offsetT offset, int pcrel,
122 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
123 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
125 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
126 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
127 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
130 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
131 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
132 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
133 static void write_relocs PARAMS ((bfd *, asection *, PTR));
134 static void write_contents PARAMS ((bfd *, asection *, PTR));
135 static void set_symtab PARAMS ((void));
137 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
138 static void merge_data_into_text PARAMS ((void));
140 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
141 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
142 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
143 static void relax_and_size_all_segments PARAMS ((void));
145 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
146 static void set_segment_vma PARAMS ((bfd *, asection *, PTR));
149 /* Create a fixS in obstack 'notes'. */
152 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
154 fragS *frag; /* Which frag? */
155 int where; /* Where in that frag? */
156 int size; /* 1, 2, or 4 usually. */
157 symbolS *add_symbol; /* X_add_symbol. */
158 symbolS *sub_symbol; /* X_op_symbol. */
159 offsetT offset; /* X_add_number. */
160 int pcrel; /* TRUE if PC-relative relocation. */
162 bfd_reloc_code_real_type r_type; /* Relocation type. */
164 int r_type; /* Relocation type. */
171 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
173 fixP->fx_frag = frag;
174 fixP->fx_where = where;
175 fixP->fx_size = size;
176 /* We've made fx_size a narrow field; check that it's wide enough. */
177 if (fixP->fx_size != size)
179 as_bad (_("field fx_size too small to hold %d"), size);
182 fixP->fx_addsy = add_symbol;
183 fixP->fx_subsy = sub_symbol;
184 fixP->fx_offset = offset;
185 fixP->fx_pcrel = pcrel;
187 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
188 fixP->fx_r_type = r_type;
190 fixP->fx_im_disp = 0;
191 fixP->fx_pcrel_adjust = 0;
192 fixP->fx_bit_fixP = 0;
193 fixP->fx_addnumber = 0;
196 fixP->fx_no_overflow = 0;
200 fixP->fx_cgen.insn = NULL;
201 fixP->fx_cgen.opinfo = 0;
205 TC_INIT_FIX_DATA (fixP);
208 as_where (&fixP->fx_file, &fixP->fx_line);
210 /* Usually, we want relocs sorted numerically, but while
211 comparing to older versions of gas that have relocs
212 reverse sorted, it is convenient to have this compile
213 time option. xoxorich. */
217 fixS **seg_fix_rootP = (frags_chained
218 ? &seg_info (now_seg)->fix_root
219 : &frchain_now->fix_root);
220 fixS **seg_fix_tailP = (frags_chained
221 ? &seg_info (now_seg)->fix_tail
222 : &frchain_now->fix_tail);
225 #ifdef REVERSE_SORT_RELOCS
227 fixP->fx_next = *seg_fix_rootP;
228 *seg_fix_rootP = fixP;
230 #else /* REVERSE_SORT_RELOCS */
232 fixP->fx_next = NULL;
235 (*seg_fix_tailP)->fx_next = fixP;
237 *seg_fix_rootP = fixP;
238 *seg_fix_tailP = fixP;
240 #endif /* REVERSE_SORT_RELOCS */
246 /* Create a fixup relative to a symbol (plus a constant). */
249 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
250 fragS *frag; /* Which frag? */
251 int where; /* Where in that frag? */
252 int size; /* 1, 2, or 4 usually. */
253 symbolS *add_symbol; /* X_add_symbol. */
254 offsetT offset; /* X_add_number. */
255 int pcrel; /* TRUE if PC-relative relocation. */
257 bfd_reloc_code_real_type r_type; /* Relocation type. */
259 int r_type; /* Relocation type. */
262 return fix_new_internal (frag, where, size, add_symbol,
263 (symbolS *) NULL, offset, pcrel, r_type);
266 /* Create a fixup for an expression. Currently we only support fixups
267 for difference expressions. That is itself more than most object
268 file formats support anyhow. */
271 fix_new_exp (frag, where, size, exp, pcrel, r_type)
272 fragS *frag; /* Which frag? */
273 int where; /* Where in that frag? */
274 int size; /* 1, 2, or 4 usually. */
275 expressionS *exp; /* Expression. */
276 int pcrel; /* TRUE if PC-relative relocation. */
278 bfd_reloc_code_real_type r_type; /* Relocation type. */
280 int r_type; /* Relocation type. */
293 as_bad (_("register value used as expression"));
297 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
298 the difference expression cannot immediately be reduced. */
300 symbolS *stmp = make_expr_symbol (exp);
302 exp->X_op = O_symbol;
303 exp->X_op_symbol = 0;
304 exp->X_add_symbol = stmp;
305 exp->X_add_number = 0;
307 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
311 add = exp->X_add_symbol;
312 off = exp->X_add_number;
314 #if defined(BFD_ASSEMBLER)
315 r_type = BFD_RELOC_RVA;
317 #if defined(TC_RVA_RELOC)
318 r_type = TC_RVA_RELOC;
320 as_fatal (_("rva not supported"));
326 sub = exp->X_add_symbol;
327 off = exp->X_add_number;
331 sub = exp->X_op_symbol;
334 add = exp->X_add_symbol;
337 off = exp->X_add_number;
341 add = make_expr_symbol (exp);
345 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
348 /* Append a string onto another string, bumping the pointer along. */
350 append (charPP, fromP, length)
353 unsigned long length;
355 /* Don't trust memcpy() of 0 chars. */
359 memcpy (*charPP, fromP, length);
363 #ifndef BFD_ASSEMBLER
364 int section_alignment[SEG_MAXIMUM_ORDINAL];
367 /* This routine records the largest alignment seen for each segment.
368 If the beginning of the segment is aligned on the worst-case
369 boundary, all of the other alignments within it will work. At
370 least one object format really uses this info. */
373 record_alignment (seg, align)
374 /* Segment to which alignment pertains. */
376 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
380 if (seg == absolute_section)
383 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
384 bfd_set_section_alignment (stdoutput, seg, align);
386 if (align > section_alignment[(int) seg])
387 section_alignment[(int) seg] = align;
392 get_recorded_alignment (seg)
395 if (seg == absolute_section)
398 return bfd_get_section_alignment (stdoutput, seg);
400 return section_alignment[(int) seg];
406 /* Reset the section indices after removing the gas created sections. */
409 renumber_sections (abfd, sec, countparg)
410 bfd *abfd ATTRIBUTE_UNUSED;
414 int *countp = (int *) countparg;
416 sec->index = *countp;
420 #endif /* defined (BFD_ASSEMBLER) */
422 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
425 chain_frchains_together_1 (section, frchp)
427 struct frchain *frchp;
429 fragS dummy, *prev_frag = &dummy;
431 fixS fix_dummy, *prev_fix = &fix_dummy;
434 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
436 prev_frag->fr_next = frchp->frch_root;
437 prev_frag = frchp->frch_last;
438 assert (prev_frag->fr_type != 0);
440 if (frchp->fix_root != (fixS *) NULL)
442 if (seg_info (section)->fix_root == (fixS *) NULL)
443 seg_info (section)->fix_root = frchp->fix_root;
444 prev_fix->fx_next = frchp->fix_root;
445 seg_info (section)->fix_tail = frchp->fix_tail;
446 prev_fix = frchp->fix_tail;
450 assert (prev_frag->fr_type != 0);
451 prev_frag->fr_next = 0;
460 chain_frchains_together (abfd, section, xxx)
461 bfd *abfd ATTRIBUTE_UNUSED;
463 PTR xxx ATTRIBUTE_UNUSED;
465 segment_info_type *info;
467 /* BFD may have introduced its own sections without using
468 subseg_new, so it is possible that seg_info is NULL. */
469 info = seg_info (section);
470 if (info != (segment_info_type *) NULL)
471 info->frchainP->frch_last
472 = chain_frchains_together_1 (section, info->frchainP);
474 /* Now that we've chained the frags together, we must add new fixups
475 to the segment, not to the frag chain. */
481 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
484 remove_subsegs (head, seg, root, last)
490 *root = head->frch_root;
491 *last = chain_frchains_together_1 (seg, head);
496 #if defined (BFD_ASSEMBLER) || !defined (BFD)
500 cvt_frag_to_fill (sec, fragP)
501 segT sec ATTRIBUTE_UNUSED;
505 cvt_frag_to_fill (headersP, sec, fragP)
506 object_headers *headersP;
511 switch (fragP->fr_type)
519 HANDLE_ALIGN (fragP);
521 know (fragP->fr_next != NULL);
522 fragP->fr_offset = (fragP->fr_next->fr_address
524 - fragP->fr_fix) / fragP->fr_var;
525 if (fragP->fr_offset < 0)
527 as_bad_where (fragP->fr_file, fragP->fr_line,
528 _("attempt to .org/.space backwards? (%ld)"),
529 (long) fragP->fr_offset);
531 fragP->fr_type = rs_fill;
539 valueT value = S_GET_VALUE (fragP->fr_symbol);
542 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
545 fragP->fr_fix += size;
546 fragP->fr_type = rs_fill;
548 fragP->fr_offset = 0;
549 fragP->fr_symbol = NULL;
554 eh_frame_convert_frag (fragP);
558 dwarf2dbg_convert_frag (fragP);
561 case rs_machine_dependent:
563 md_convert_frag (stdoutput, sec, fragP);
565 md_convert_frag (headersP, sec, fragP);
568 assert (fragP->fr_next == NULL
569 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
572 /* After md_convert_frag, we make the frag into a ".space 0".
573 md_convert_frag() should set up any fixSs and constants
578 #ifndef WORKING_DOT_WORD
581 struct broken_word *lie;
583 if (fragP->fr_subtype)
585 fragP->fr_fix += md_short_jump_size;
586 for (lie = (struct broken_word *) (fragP->fr_symbol);
587 lie && lie->dispfrag == fragP;
588 lie = lie->next_broken_word)
590 fragP->fr_fix += md_long_jump_size;
598 BAD_CASE (fragP->fr_type);
603 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
606 static void relax_seg PARAMS ((bfd *, asection *, PTR));
608 relax_seg (abfd, sec, xxx)
609 bfd *abfd ATTRIBUTE_UNUSED;
613 segment_info_type *seginfo = seg_info (sec);
615 if (seginfo && seginfo->frchainP
616 && relax_segment (seginfo->frchainP->frch_root, sec))
618 int *result = (int *) xxx;
623 static void size_seg PARAMS ((bfd *, asection *, PTR));
625 size_seg (abfd, sec, xxx)
628 PTR xxx ATTRIBUTE_UNUSED;
632 segment_info_type *seginfo;
634 valueT size, newsize;
636 subseg_change (sec, 0);
638 seginfo = seg_info (sec);
639 if (seginfo && seginfo->frchainP)
641 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
642 cvt_frag_to_fill (sec, fragp);
643 for (fragp = seginfo->frchainP->frch_root;
645 fragp = fragp->fr_next)
646 /* Walk to last elt. */
648 size = fragp->fr_address + fragp->fr_fix;
653 flags = bfd_get_section_flags (abfd, sec);
655 if (size > 0 && ! seginfo->bss)
656 flags |= SEC_HAS_CONTENTS;
658 /* @@ This is just an approximation. */
659 if (seginfo && seginfo->fix_root)
663 x = bfd_set_section_flags (abfd, sec, flags);
666 newsize = md_section_align (sec, size);
667 x = bfd_set_section_size (abfd, sec, newsize);
670 /* If the size had to be rounded up, add some padding in the last
672 assert (newsize >= size);
675 fragS *last = seginfo->frchainP->frch_last;
676 fragp = seginfo->frchainP->frch_root;
677 while (fragp->fr_next != last)
678 fragp = fragp->fr_next;
679 last->fr_address = size;
680 fragp->fr_offset += newsize - size;
683 #ifdef tc_frob_section
684 tc_frob_section (sec);
686 #ifdef obj_frob_section
687 obj_frob_section (sec);
693 dump_section_relocs (abfd, sec, stream_)
694 bfd *abfd ATTRIBUTE_UNUSED;
698 FILE *stream = (FILE *) stream_;
699 segment_info_type *seginfo = seg_info (sec);
700 fixS *fixp = seginfo->fix_root;
705 fprintf (stream, "sec %s relocs:\n", sec->name);
708 symbolS *s = fixp->fx_addsy;
710 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
711 (int) fixp->fx_r_type);
713 fprintf (stream, "no sym\n");
716 print_symbol_value_1 (stream, s);
717 fprintf (stream, "\n");
719 fixp = fixp->fx_next;
723 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
726 #ifndef EMIT_SECTION_SYMBOLS
727 #define EMIT_SECTION_SYMBOLS 1
731 adjust_reloc_syms (abfd, sec, xxx)
732 bfd *abfd ATTRIBUTE_UNUSED;
734 PTR xxx ATTRIBUTE_UNUSED;
736 segment_info_type *seginfo = seg_info (sec);
742 dump_section_relocs (abfd, sec, stderr);
744 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
748 else if (fixp->fx_addsy)
754 fprintf (stderr, "\n\nadjusting fixup:\n");
758 sym = fixp->fx_addsy;
760 /* All symbols should have already been resolved at this
761 point. It is possible to see unresolved expression
762 symbols, though, since they are not in the regular symbol
765 resolve_symbol_value (sym);
767 if (fixp->fx_subsy != NULL)
768 resolve_symbol_value (fixp->fx_subsy);
770 /* If this symbol is equated to an undefined symbol, convert
771 the fixup to being against that symbol. */
772 if (sym != NULL && symbol_equated_reloc_p (sym))
774 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
775 sym = symbol_get_value_expression (sym)->X_add_symbol;
776 fixp->fx_addsy = sym;
779 if (sym != NULL && symbol_mri_common_p (sym))
781 /* These symbols are handled specially in fixup_segment. */
785 symsec = S_GET_SEGMENT (sym);
790 if (bfd_is_abs_section (symsec))
792 /* The fixup_segment routine will not use this symbol in a
793 relocation unless TC_FORCE_RELOCATION returns 1. */
794 if (TC_FORCE_RELOCATION (fixp))
796 symbol_mark_used_in_reloc (fixp->fx_addsy);
797 #ifdef UNDEFINED_DIFFERENCE_OK
798 if (fixp->fx_subsy != NULL)
799 symbol_mark_used_in_reloc (fixp->fx_subsy);
805 /* If it's one of these sections, assume the symbol is
806 definitely going to be output. The code in
807 md_estimate_size_before_relax in tc-mips.c uses this test
808 as well, so if you change this code you should look at that
810 if (bfd_is_und_section (symsec)
811 || bfd_is_com_section (symsec))
813 symbol_mark_used_in_reloc (fixp->fx_addsy);
814 #ifdef UNDEFINED_DIFFERENCE_OK
815 /* We have the difference of an undefined symbol and some
816 other symbol. Make sure to mark the other symbol as used
817 in a relocation so that it will always be output. */
819 symbol_mark_used_in_reloc (fixp->fx_subsy);
824 /* Don't try to reduce relocs which refer to non-local symbols
825 in .linkonce sections. It can lead to confusion when a
826 debugging section refers to a .linkonce section. I hope
827 this will always be correct. */
828 if (symsec != sec && ! S_IS_LOCAL (sym))
834 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
839 /* The GNU toolchain uses an extension for ELF: a section
840 beginning with the magic string .gnu.linkonce is a
842 if (strncmp (segment_name (symsec), ".gnu.linkonce",
843 sizeof ".gnu.linkonce" - 1) == 0)
849 symbol_mark_used_in_reloc (fixp->fx_addsy);
850 #ifdef UNDEFINED_DIFFERENCE_OK
851 if (fixp->fx_subsy != NULL)
852 symbol_mark_used_in_reloc (fixp->fx_subsy);
858 /* Since we're reducing to section symbols, don't attempt to reduce
859 anything that's already using one. */
860 if (symbol_section_p (sym))
862 symbol_mark_used_in_reloc (fixp->fx_addsy);
867 /* We can never adjust a reloc against a weak symbol. If we
868 did, and the weak symbol was overridden by a real symbol
869 somewhere else, then our relocation would be pointing at
870 the wrong area of memory. */
873 symbol_mark_used_in_reloc (fixp->fx_addsy);
877 /* Never adjust a reloc against local symbol in a merge section. */
878 if (symsec->flags & SEC_MERGE)
880 symbol_mark_used_in_reloc (fixp->fx_addsy);
885 /* Is there some other reason we can't adjust this one? (E.g.,
886 call/bal links in i960-bout symbols.) */
887 #ifdef obj_fix_adjustable
888 if (! obj_fix_adjustable (fixp))
890 symbol_mark_used_in_reloc (fixp->fx_addsy);
895 /* Is there some other (target cpu dependent) reason we can't adjust
896 this one? (E.g. relocations involving function addresses on
898 #ifdef tc_fix_adjustable
899 if (! tc_fix_adjustable (fixp))
901 symbol_mark_used_in_reloc (fixp->fx_addsy);
906 /* If the section symbol isn't going to be output, the relocs
907 at least should still work. If not, figure out what to do
908 when we run into that case.
910 We refetch the segment when calling section_symbol, rather
911 than using symsec, because S_GET_VALUE may wind up changing
912 the section when it calls resolve_symbol_value. */
913 fixp->fx_offset += S_GET_VALUE (sym);
914 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
915 symbol_mark_used_in_reloc (fixp->fx_addsy);
917 fprintf (stderr, "\nadjusted fixup:\n");
924 #if 1 /* def RELOC_REQUIRES_SYMBOL */
927 /* There was no symbol required by this relocation. However,
928 BFD doesn't really handle relocations without symbols well.
929 (At least, the COFF support doesn't.) So for now we fake up
930 a local symbol in the absolute section. */
932 fixp->fx_addsy = section_symbol (absolute_section);
934 fixp->fx_addsy->sy_used_in_reloc = 1;
939 dump_section_relocs (abfd, sec, stderr);
943 write_relocs (abfd, sec, xxx)
946 PTR xxx ATTRIBUTE_UNUSED;
948 segment_info_type *seginfo = seg_info (sec);
955 /* If seginfo is NULL, we did not create this section; don't do
960 fixup_segment (seginfo->fix_root, sec);
963 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
966 #ifndef RELOC_EXPANSION_POSSIBLE
967 /* Set up reloc information as well. */
968 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
969 memset ((char *) relocs, 0, n * sizeof (arelent *));
972 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
975 bfd_reloc_status_type s;
984 /* If this is an undefined symbol which was equated to another
985 symbol, then generate the reloc against the latter symbol
986 rather than the former. */
987 sym = fixp->fx_addsy;
988 while (symbol_equated_reloc_p (sym))
992 /* We must avoid looping, as that can occur with a badly
994 n = symbol_get_value_expression (sym)->X_add_symbol;
997 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
1000 fixp->fx_addsy = sym;
1002 reloc = tc_gen_reloc (sec, fixp);
1010 /* This test is triggered inappropriately for the SH. */
1011 if (fixp->fx_where + fixp->fx_size
1012 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1016 s = bfd_install_relocation (stdoutput, reloc,
1017 fixp->fx_frag->fr_literal,
1018 fixp->fx_frag->fr_address,
1024 case bfd_reloc_overflow:
1025 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
1027 case bfd_reloc_outofrange:
1028 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation out of range"));
1031 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1032 fixp->fx_file, fixp->fx_line, s);
1034 relocs[i++] = reloc;
1037 n = n * MAX_RELOC_EXPANSION;
1038 /* Set up reloc information as well. */
1039 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
1042 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1046 bfd_reloc_status_type s;
1056 /* If this is an undefined symbol which was equated to another
1057 symbol, then generate the reloc against the latter symbol
1058 rather than the former. */
1059 sym = fixp->fx_addsy;
1060 while (symbol_equated_reloc_p (sym))
1061 sym = symbol_get_value_expression (sym)->X_add_symbol;
1062 fixp->fx_addsy = sym;
1064 reloc = tc_gen_reloc (sec, fixp);
1066 for (j = 0; reloc[j]; j++)
1068 relocs[i++] = reloc[j];
1071 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1072 if (fixp->fx_where + fixp->fx_size
1073 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1074 as_bad_where (fixp->fx_file, fixp->fx_line,
1075 _("internal error: fixup not contained within frag"));
1076 for (j = 0; reloc[j]; j++)
1078 s = bfd_install_relocation (stdoutput, reloc[j],
1079 fixp->fx_frag->fr_literal,
1080 fixp->fx_frag->fr_address,
1086 case bfd_reloc_overflow:
1087 as_bad_where (fixp->fx_file, fixp->fx_line,
1088 _("relocation overflow"));
1091 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1092 fixp->fx_file, fixp->fx_line);
1103 sympp = bfd_get_outsymbols (stdoutput);
1104 nsyms = bfd_get_symcount (stdoutput);
1105 for (i = 0; i < n; i++)
1106 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1108 for (j = 0; j < nsyms; j++)
1109 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1118 bfd_set_reloc (stdoutput, sec, relocs, n);
1120 bfd_set_section_flags (abfd, sec,
1121 (bfd_get_section_flags (abfd, sec)
1122 & (flagword) ~SEC_RELOC));
1124 #ifdef SET_SECTION_RELOCS
1125 SET_SECTION_RELOCS (sec, relocs, n);
1133 fprintf (stderr, "relocs for sec %s\n", sec->name);
1134 for (i = 0; i < n; i++)
1137 s = *r->sym_ptr_ptr;
1138 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1139 i, r, r->address, s->name, r->addend);
1146 write_contents (abfd, sec, xxx)
1147 bfd *abfd ATTRIBUTE_UNUSED;
1149 PTR xxx ATTRIBUTE_UNUSED;
1151 segment_info_type *seginfo = seg_info (sec);
1152 unsigned long offset = 0;
1155 /* Write out the frags. */
1157 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1160 for (f = seginfo->frchainP->frch_root;
1165 unsigned long fill_size;
1169 assert (f->fr_type == rs_fill);
1172 x = bfd_set_section_contents (stdoutput, sec,
1173 f->fr_literal, (file_ptr) offset,
1174 (bfd_size_type) f->fr_fix);
1177 bfd_perror (stdoutput->filename);
1178 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1179 exit (EXIT_FAILURE);
1181 offset += f->fr_fix;
1183 fill_literal = f->fr_literal + f->fr_fix;
1184 fill_size = f->fr_var;
1185 count = f->fr_offset;
1186 assert (count >= 0);
1187 if (fill_size && count)
1190 if (fill_size > sizeof (buf))
1192 /* Do it the old way. Can this ever happen? */
1195 x = bfd_set_section_contents (stdoutput, sec,
1198 (bfd_size_type) fill_size);
1201 bfd_perror (stdoutput->filename);
1202 as_perror (_("FATAL: Can't write %s"),
1203 stdoutput->filename);
1204 exit (EXIT_FAILURE);
1206 offset += fill_size;
1211 /* Build a buffer full of fill objects and output it as
1212 often as necessary. This saves on the overhead of
1213 potentially lots of bfd_set_section_contents calls. */
1217 n_per_buf = sizeof (buf);
1218 memset (buf, *fill_literal, n_per_buf);
1223 n_per_buf = sizeof (buf) / fill_size;
1224 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1225 memcpy (bufp, fill_literal, fill_size);
1227 for (; count > 0; count -= n_per_buf)
1229 n_per_buf = n_per_buf > count ? count : n_per_buf;
1230 x = bfd_set_section_contents
1231 (stdoutput, sec, buf, (file_ptr) offset,
1232 (bfd_size_type) n_per_buf * fill_size);
1234 as_fatal (_("cannot write to output file"));
1235 offset += n_per_buf * fill_size;
1243 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1245 merge_data_into_text ()
1247 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1248 seg_info (text_section)->frchainP->frch_last->fr_next =
1249 seg_info (data_section)->frchainP->frch_root;
1250 seg_info (text_section)->frchainP->frch_last =
1251 seg_info (data_section)->frchainP->frch_last;
1252 seg_info (data_section)->frchainP = 0;
1256 text_last_frag->fr_next = data_frag_root;
1257 text_last_frag = data_last_frag;
1258 data_last_frag = NULL;
1259 data_frag_root = NULL;
1262 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1263 tmp->fx_next = data_fix_root;
1264 text_fix_tail = data_fix_tail;
1267 text_fix_root = data_fix_root;
1268 data_fix_root = NULL;
1271 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1273 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1275 relax_and_size_all_segments ()
1279 relax_segment (text_frag_root, SEG_TEXT);
1280 relax_segment (data_frag_root, SEG_DATA);
1281 relax_segment (bss_frag_root, SEG_BSS);
1283 /* Now the addresses of frags are correct within the segment. */
1284 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1285 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1286 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1288 /* Join the 2 segments into 1 huge segment.
1289 To do this, re-compute every rn_address in the SEG_DATA frags.
1290 Then join the data frags after the text frags.
1292 Determine a_data [length of data segment]. */
1295 register relax_addressT slide;
1297 know ((text_last_frag->fr_type == rs_fill)
1298 && (text_last_frag->fr_offset == 0));
1300 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1301 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1302 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1304 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1305 /* For b.out: If the data section has a strict alignment
1306 requirement, its load address in the .o file will be
1307 rounded up from the size of the text section. These
1308 two values are *not* the same! Similarly for the bss
1310 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1313 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1314 fragP->fr_address += slide;
1316 know (text_last_frag != 0);
1317 text_last_frag->fr_next = data_frag_root;
1321 H_SET_DATA_SIZE (&headers, 0);
1325 /* See above comments on b.out data section address. */
1328 if (data_last_frag == 0)
1329 bss_vma = H_GET_TEXT_SIZE (&headers);
1331 bss_vma = data_last_frag->fr_address;
1332 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1333 bss_address_frag.fr_address = bss_vma;
1335 #else /* ! OBJ_BOUT */
1336 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1337 H_GET_DATA_SIZE (&headers));
1339 #endif /* ! OBJ_BOUT */
1341 /* Slide all the frags. */
1344 relax_addressT slide = bss_address_frag.fr_address;
1346 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1347 fragP->fr_address += slide;
1351 H_SET_BSS_SIZE (&headers,
1352 bss_last_frag->fr_address - bss_frag_root->fr_address);
1354 H_SET_BSS_SIZE (&headers, 0);
1356 #endif /* ! BFD_ASSEMBLER && ! BFD */
1358 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1360 #ifdef BFD_ASSEMBLER
1368 extern PTR bfd_alloc PARAMS ((bfd *, bfd_size_type));
1370 /* Count symbols. We can't rely on a count made by the loop in
1371 write_object_file, because *_frob_file may add a new symbol or
1374 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1380 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1382 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1383 symp = symbol_rootP;
1384 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1386 asympp[i] = symbol_get_bfdsym (symp);
1387 symbol_mark_written (symp);
1392 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1393 assert (result == true);
1394 symbol_table_frozen = 1;
1398 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1400 set_segment_vma (abfd, sec, xxx)
1403 PTR xxx ATTRIBUTE_UNUSED;
1405 static bfd_vma addr = 0;
1407 bfd_set_section_vma (abfd, sec, addr);
1408 addr += bfd_section_size (abfd, sec);
1410 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1412 /* Finish the subsegments. After every sub-segment, we fake an
1413 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1414 ".fill 0" because that is the kind of frag that requires least
1415 thought. ".align" frags like to have a following frag since that
1416 makes calculating their intended length trivial. */
1418 #ifndef SUB_SEGMENT_ALIGN
1419 #ifdef BFD_ASSEMBLER
1420 #define SUB_SEGMENT_ALIGN(SEG) (0)
1422 #define SUB_SEGMENT_ALIGN(SEG) (2)
1429 struct frchain *frchainP;
1431 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1435 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1437 /* This now gets called even if we had errors. In that case,
1438 any alignment is meaningless, and, moreover, will look weird
1439 if we are generating a listing. */
1440 alignment = had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg);
1442 /* The last subsegment gets an aligment corresponding to the
1443 alignment of the section. This allows proper nop-filling
1444 at the end of code-bearing sections. */
1445 if (!frchainP->frch_next || frchainP->frch_next->frch_seg != now_seg)
1446 alignment = get_recorded_alignment (now_seg);
1448 if (subseg_text_p (now_seg))
1449 frag_align_code (alignment, 0);
1451 frag_align (alignment, 0, 0);
1453 /* frag_align will have left a new frag.
1454 Use this last frag for an empty ".fill".
1456 For this segment ...
1457 Create a last frag. Do not leave a "being filled in frag". */
1458 frag_wane (frag_now);
1459 frag_now->fr_fix = 0;
1460 know (frag_now->fr_next == NULL);
1464 /* Write the object file. */
1467 write_object_file ()
1469 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1470 fragS *fragP; /* Track along all frags. */
1473 /* Do we really want to write it? */
1475 int n_warns, n_errs;
1476 n_warns = had_warnings ();
1477 n_errs = had_errors ();
1478 /* The -Z flag indicates that an object file should be generated,
1479 regardless of warnings and errors. */
1480 if (flag_always_generate_output)
1482 if (n_warns || n_errs)
1483 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1484 n_errs, n_errs == 1 ? "" : "s",
1485 n_warns, n_warns == 1 ? "" : "s");
1490 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1491 n_errs, n_errs == 1 ? "" : "s",
1492 n_warns, n_warns == 1 ? "" : "s");
1497 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1498 a routine to check for the definition of the procedure "_main",
1499 and if so -- fix it up so that it can be program entry point. */
1500 vms_check_for_main ();
1501 #endif /* OBJ_VMS */
1503 /* From now on, we don't care about sub-segments. Build one frag chain
1504 for each segment. Linked thru fr_next. */
1506 #ifdef BFD_ASSEMBLER
1507 /* Remove the sections created by gas for its own purposes. */
1509 asection **seclist, *sec;
1512 seclist = &stdoutput->sections;
1513 while (seclist && *seclist)
1516 while (sec == reg_section || sec == expr_section)
1520 stdoutput->section_count--;
1525 seclist = &(*seclist)->next;
1528 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1531 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1533 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1534 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1535 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1538 /* We have two segments. If user gave -R flag, then we must put the
1539 data frags into the text segment. Do this before relaxing so
1540 we know to take advantage of -R and make shorter addresses. */
1541 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1542 if (flag_readonly_data_in_text)
1544 merge_data_into_text ();
1548 #ifdef BFD_ASSEMBLER
1553 #ifndef WORKING_DOT_WORD
1554 /* We need to reset the markers in the broken word list and
1555 associated frags between calls to relax_segment (via
1556 relax_seg). Since the broken word list is global, we do it
1557 once per round, rather than locally in relax_segment for each
1559 struct broken_word *brokp;
1561 for (brokp = broken_words;
1562 brokp != (struct broken_word *) NULL;
1563 brokp = brokp->next_broken_word)
1567 if (brokp->dispfrag != (fragS *) NULL
1568 && brokp->dispfrag->fr_type == rs_broken_word)
1569 brokp->dispfrag->fr_subtype = 0;
1574 bfd_map_over_sections (stdoutput, relax_seg, &changed);
1579 /* Note - Most ports will use the default value of
1580 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1581 local symbols to be resolved, removing their frag information.
1582 Some ports however, will not have finished relaxing all of
1583 their frags and will still need the local symbol frag
1584 information. These ports can set
1585 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1586 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1588 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1590 relax_and_size_all_segments ();
1591 #endif /* BFD_ASSEMBLER */
1593 /* Relaxation has completed. Freeze all syms. */
1596 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1597 /* Now that the segments have their final sizes, run through the
1598 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1599 should too. Currently, only DJGPP uses this code, but other
1600 COFF targets may need to execute this too. */
1601 bfd_map_over_sections (stdoutput, set_segment_vma, (char *) 0);
1604 #ifndef BFD_ASSEMBLER
1605 /* Crawl the symbol chain.
1607 For each symbol whose value depends on a frag, take the address of
1608 that frag and subsume it into the value of the symbol.
1609 After this, there is just one way to lookup a symbol value.
1610 Values are left in their final state for object file emission.
1611 We adjust the values of 'L' local symbols, even if we do
1612 not intend to emit them to the object file, because their values
1613 are needed for fix-ups.
1615 Unless we saw a -L flag, remove all symbols that begin with 'L'
1616 from the symbol chain. (They are still pointed to by the fixes.)
1618 Count the remaining symbols.
1619 Assign a symbol number to each symbol.
1620 Count the number of string-table chars we will emit.
1621 Put this info into the headers as appropriate. */
1622 know (zero_address_frag.fr_address == 0);
1623 string_byte_count = sizeof (string_byte_count);
1625 obj_crawl_symbol_chain (&headers);
1627 if (string_byte_count == sizeof (string_byte_count))
1628 string_byte_count = 0;
1630 H_SET_STRING_SIZE (&headers, string_byte_count);
1632 /* Addresses of frags now reflect addresses we use in the object file.
1633 Symbol values are correct.
1634 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1635 Also converting any machine-dependent frags using md_convert_frag(); */
1636 subseg_change (SEG_TEXT, 0);
1638 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1640 /* At this point we have linked all the frags into a single
1641 chain. However, cvt_frag_to_fill may call md_convert_frag
1642 which may call fix_new. We need to ensure that fix_new adds
1643 the fixup to the right section. */
1644 if (fragP == data_frag_root)
1645 subseg_change (SEG_DATA, 0);
1647 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1649 /* Some assert macros don't work with # directives mixed in. */
1651 if (!(fragP->fr_next == NULL
1653 || fragP->fr_next == data_frag_root
1655 || ((fragP->fr_next->fr_address - fragP->fr_address)
1656 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1660 #endif /* ! BFD_ASSEMBLER */
1662 #ifndef WORKING_DOT_WORD
1664 struct broken_word *lie;
1665 struct broken_word **prevP;
1667 prevP = &broken_words;
1668 for (lie = broken_words; lie; lie = lie->next_broken_word)
1673 subseg_change (lie->seg, lie->subseg);
1674 exp.X_op = O_subtract;
1675 exp.X_add_symbol = lie->add;
1676 exp.X_op_symbol = lie->sub;
1677 exp.X_add_number = lie->addnum;
1678 #ifdef BFD_ASSEMBLER
1679 #ifdef TC_CONS_FIX_NEW
1680 TC_CONS_FIX_NEW (lie->frag,
1681 lie->word_goes_here - lie->frag->fr_literal,
1684 fix_new_exp (lie->frag,
1685 lie->word_goes_here - lie->frag->fr_literal,
1686 2, &exp, 0, BFD_RELOC_16);
1689 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1690 fix_new_exp (lie->frag,
1691 lie->word_goes_here - lie->frag->fr_literal,
1692 2, &exp, 0, NO_RELOC);
1695 fix_new_ns32k_exp (lie->frag,
1696 lie->word_goes_here - lie->frag->fr_literal,
1697 2, &exp, 0, 0, 2, 0, 0);
1699 fix_new_exp (lie->frag,
1700 lie->word_goes_here - lie->frag->fr_literal,
1702 #endif /* TC_NS32K */
1703 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1704 #endif /* BFD_ASSEMBLER */
1705 *prevP = lie->next_broken_word;
1708 prevP = &(lie->next_broken_word);
1710 for (lie = broken_words; lie;)
1712 struct broken_word *untruth;
1714 addressT table_addr;
1715 addressT from_addr, to_addr;
1718 subseg_change (lie->seg, lie->subseg);
1719 fragP = lie->dispfrag;
1721 /* Find out how many broken_words go here. */
1724 untruth && untruth->dispfrag == fragP;
1725 untruth = untruth->next_broken_word)
1726 if (untruth->added == 1)
1729 table_ptr = lie->dispfrag->fr_opcode;
1730 table_addr = (lie->dispfrag->fr_address
1731 + (table_ptr - lie->dispfrag->fr_literal));
1732 /* Create the jump around the long jumps. This is a short
1733 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1734 from_addr = table_addr;
1735 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1736 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1738 table_ptr += md_short_jump_size;
1739 table_addr += md_short_jump_size;
1742 lie && lie->dispfrag == fragP;
1743 m++, lie = lie->next_broken_word)
1745 if (lie->added == 2)
1747 /* Patch the jump table. */
1748 /* This is the offset from ??? to table_ptr+0. */
1749 to_addr = table_addr - S_GET_VALUE (lie->sub);
1750 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1751 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1753 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1754 for (untruth = lie->next_broken_word;
1755 untruth && untruth->dispfrag == fragP;
1756 untruth = untruth->next_broken_word)
1758 if (untruth->use_jump == lie)
1759 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1762 /* Install the long jump. */
1763 /* This is a long jump from table_ptr+0 to the final target. */
1764 from_addr = table_addr;
1765 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1766 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1768 table_ptr += md_long_jump_size;
1769 table_addr += md_long_jump_size;
1773 #endif /* not WORKING_DOT_WORD */
1775 #ifndef BFD_ASSEMBLER
1778 char *the_object_file;
1779 long object_file_size;
1780 /* Scan every FixS performing fixups. We had to wait until now to
1781 do this because md_convert_frag() may have made some fixSs. */
1784 subseg_change (SEG_TEXT, 0);
1785 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1786 subseg_change (SEG_DATA, 0);
1787 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1788 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1790 /* FIXME: Move this stuff into the pre-write-hook. */
1791 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1792 H_SET_ENTRY_POINT (&headers, 0);
1794 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1796 object_file_size = H_GET_FILE_SIZE (&headers);
1797 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1799 output_file_create (out_file_name);
1801 obj_header_append (&next_object_file_charP, &headers);
1803 know ((next_object_file_charP - the_object_file)
1804 == H_GET_HEADER_SIZE (&headers));
1807 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1809 register long count;
1810 register char *fill_literal;
1811 register long fill_size;
1814 know (fragP->fr_type == rs_fill);
1815 append (&next_object_file_charP, fragP->fr_literal,
1816 (unsigned long) fragP->fr_fix);
1817 fill_literal = fragP->fr_literal + fragP->fr_fix;
1818 fill_size = fragP->fr_var;
1819 know (fragP->fr_offset >= 0);
1821 for (count = fragP->fr_offset; count; count--)
1822 append (&next_object_file_charP, fill_literal,
1823 (unsigned long) fill_size);
1826 know ((next_object_file_charP - the_object_file)
1827 == (H_GET_HEADER_SIZE (&headers)
1828 + H_GET_TEXT_SIZE (&headers)
1829 + H_GET_DATA_SIZE (&headers)));
1831 /* Emit relocations. */
1832 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1833 (relax_addressT) 0);
1834 know ((next_object_file_charP - the_object_file)
1835 == (H_GET_HEADER_SIZE (&headers)
1836 + H_GET_TEXT_SIZE (&headers)
1837 + H_GET_DATA_SIZE (&headers)
1838 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1840 /* Make addresses in data relocation directives relative to beginning of
1841 first data fragment, not end of last text fragment: alignment of the
1842 start of the data segment may place a gap between the segments. */
1843 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1844 data0_frchainP->frch_root->fr_address);
1846 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1847 text_last_frag->fr_address);
1848 #endif /* TC_I960 */
1850 know ((next_object_file_charP - the_object_file)
1851 == (H_GET_HEADER_SIZE (&headers)
1852 + H_GET_TEXT_SIZE (&headers)
1853 + H_GET_DATA_SIZE (&headers)
1854 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1855 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1857 /* Emit line number entries. */
1858 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1859 know ((next_object_file_charP - the_object_file)
1860 == (H_GET_HEADER_SIZE (&headers)
1861 + H_GET_TEXT_SIZE (&headers)
1862 + H_GET_DATA_SIZE (&headers)
1863 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1864 + H_GET_DATA_RELOCATION_SIZE (&headers)
1865 + H_GET_LINENO_SIZE (&headers)));
1868 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1869 know ((next_object_file_charP - the_object_file)
1870 == (H_GET_HEADER_SIZE (&headers)
1871 + H_GET_TEXT_SIZE (&headers)
1872 + H_GET_DATA_SIZE (&headers)
1873 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1874 + H_GET_DATA_RELOCATION_SIZE (&headers)
1875 + H_GET_LINENO_SIZE (&headers)
1876 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1879 if (string_byte_count > 0)
1880 obj_emit_strings (&next_object_file_charP);
1883 bfd_seek (stdoutput, (file_ptr) 0, 0);
1884 bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
1887 /* Write the data to the file. */
1888 output_file_append (the_object_file, object_file_size, out_file_name);
1889 free (the_object_file);
1893 /* Now do the VMS-dependent part of writing the object file. */
1894 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1895 H_GET_DATA_SIZE (&headers),
1896 H_GET_BSS_SIZE (&headers),
1897 text_frag_root, data_frag_root);
1898 #endif /* OBJ_VMS */
1899 #else /* BFD_ASSEMBLER */
1901 /* Resolve symbol values. This needs to be done before processing
1907 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1908 resolve_symbol_value (symp);
1910 resolve_local_symbol_values ();
1914 #ifdef tc_frob_file_before_adjust
1915 tc_frob_file_before_adjust ();
1917 #ifdef obj_frob_file_before_adjust
1918 obj_frob_file_before_adjust ();
1921 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1923 /* Set up symbol table, and write it out. */
1928 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1933 if (symbol_mri_common_p (symp))
1935 if (S_IS_EXTERNAL (symp))
1936 as_bad (_("%s: global symbols not supported in common sections"),
1938 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1942 name = S_GET_NAME (symp);
1946 decode_local_label_name ((char *) S_GET_NAME (symp));
1947 /* They only differ if `name' is a fb or dollar local
1949 if (name2 != name && ! S_IS_DEFINED (symp))
1950 as_bad (_("local label `%s' is not defined"), name2);
1953 /* Do it again, because adjust_reloc_syms might introduce
1954 more symbols. They'll probably only be section symbols,
1955 but they'll still need to have the values computed. */
1956 resolve_symbol_value (symp);
1958 /* Skip symbols which were equated to undefined or common
1960 if (symbol_equated_reloc_p (symp))
1962 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1966 /* So far, common symbols have been treated like undefined symbols.
1967 Put them in the common section now. */
1968 if (S_IS_DEFINED (symp) == 0
1969 && S_GET_VALUE (symp) != 0)
1970 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1972 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1973 S_GET_NAME (symp), symp,
1975 symbol_get_bfdsym (symp)->flags,
1976 segment_name (S_GET_SEGMENT (symp)));
1979 #ifdef obj_frob_symbol
1980 obj_frob_symbol (symp, punt);
1982 #ifdef tc_frob_symbol
1983 if (! punt || symbol_used_in_reloc_p (symp))
1984 tc_frob_symbol (symp, punt);
1987 /* If we don't want to keep this symbol, splice it out of
1988 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1989 want section symbols. Otherwise, we skip local symbols
1990 and symbols that the frob_symbol macros told us to punt,
1991 but we keep such symbols if they are used in relocs. */
1992 if ((! EMIT_SECTION_SYMBOLS
1993 && symbol_section_p (symp))
1994 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1995 opposites. Sometimes the former checks flags and the
1996 latter examines the name... */
1997 || (!S_IS_EXTERN (symp)
1998 && (S_IS_LOCAL (symp) || punt)
1999 && ! symbol_used_in_reloc_p (symp)))
2001 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2003 /* After symbol_remove, symbol_next(symp) still returns
2004 the one that came after it in the chain. So we don't
2005 need to do any extra cleanup work here. */
2009 /* Make sure we really got a value for the symbol. */
2010 if (! symbol_resolved_p (symp))
2012 as_bad (_("can't resolve value for symbol `%s'"),
2014 symbol_mark_resolved (symp);
2017 /* Set the value into the BFD symbol. Up til now the value
2018 has only been kept in the gas symbolS struct. */
2019 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2025 /* Now do any format-specific adjustments to the symbol table, such
2026 as adding file symbols. */
2027 #ifdef tc_adjust_symtab
2028 tc_adjust_symtab ();
2030 #ifdef obj_adjust_symtab
2031 obj_adjust_symtab ();
2034 /* Now that all the sizes are known, and contents correct, we can
2035 start writing to the file. */
2038 /* If *_frob_file changes the symbol value at this point, it is
2039 responsible for moving the changed value into symp->bsym->value
2040 as well. Hopefully all symbol value changing can be done in
2045 #ifdef obj_frob_file
2049 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2051 #ifdef tc_frob_file_after_relocs
2052 tc_frob_file_after_relocs ();
2054 #ifdef obj_frob_file_after_relocs
2055 obj_frob_file_after_relocs ();
2058 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2059 #endif /* BFD_ASSEMBLER */
2063 #ifdef TC_GENERIC_RELAX_TABLE
2065 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2068 relax_frag (segment, fragP, stretch)
2073 const relax_typeS *this_type;
2074 const relax_typeS *start_type;
2075 relax_substateT next_state;
2076 relax_substateT this_state;
2082 const relax_typeS *table;
2084 target = fragP->fr_offset;
2085 address = fragP->fr_address;
2086 table = TC_GENERIC_RELAX_TABLE;
2087 this_state = fragP->fr_subtype;
2088 start_type = this_type = table + this_state;
2089 symbolP = fragP->fr_symbol;
2095 sym_frag = symbol_get_frag (symbolP);
2097 #ifndef DIFF_EXPR_OK
2098 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2099 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2100 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2101 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2102 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2104 know (sym_frag != NULL);
2106 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2107 || sym_frag == &zero_address_frag);
2108 target += S_GET_VALUE (symbolP);
2110 /* If frag has yet to be reached on this pass,
2111 assume it will move by STRETCH just as we did.
2112 If this is not so, it will be because some frag
2113 between grows, and that will force another pass. */
2116 && sym_frag->relax_marker != fragP->relax_marker
2117 && S_GET_SEGMENT (symbolP) == segment)
2123 aim = target - address - fragP->fr_fix;
2124 #ifdef TC_PCREL_ADJUST
2125 /* Currently only the ns32k family needs this. */
2126 aim += TC_PCREL_ADJUST (fragP);
2128 /* This machine doesn't want to use pcrel_adjust.
2129 In that case, pcrel_adjust should be zero. */
2131 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2134 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2135 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2140 /* Look backwards. */
2141 for (next_state = this_type->rlx_more; next_state;)
2142 if (aim >= this_type->rlx_backward)
2146 /* Grow to next state. */
2147 this_state = next_state;
2148 this_type = table + this_state;
2149 next_state = this_type->rlx_more;
2154 /* Look forwards. */
2155 for (next_state = this_type->rlx_more; next_state;)
2156 if (aim <= this_type->rlx_forward)
2160 /* Grow to next state. */
2161 this_state = next_state;
2162 this_type = table + this_state;
2163 next_state = this_type->rlx_more;
2167 growth = this_type->rlx_length - start_type->rlx_length;
2169 fragP->fr_subtype = this_state;
2173 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2175 /* Relax_align. Advance location counter to next address that has 'alignment'
2176 lowest order bits all 0s, return size of adjustment made. */
2177 static relax_addressT
2178 relax_align (address, alignment)
2179 register relax_addressT address; /* Address now. */
2180 register int alignment; /* Alignment (binary). */
2182 relax_addressT mask;
2183 relax_addressT new_address;
2185 mask = ~((~0) << alignment);
2186 new_address = (address + mask) & (~mask);
2187 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2189 /* We must provide lots of padding, so the linker can discard it
2190 when needed. The linker will not add extra space, ever. */
2191 new_address += (1 << alignment);
2193 return (new_address - address);
2196 /* Now we have a segment, not a crowd of sub-segments, we can make
2201 After this, all frags in this segment have addresses that are correct
2202 within the segment. Since segments live in different file addresses,
2203 these frag addresses may not be the same as final object-file
2207 relax_segment (segment_frag_root, segment)
2208 struct frag *segment_frag_root;
2211 register struct frag *fragP;
2212 register relax_addressT address;
2215 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2216 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2218 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2219 subseg_change (segment, 0);
2221 /* For each frag in segment: count and store (a 1st guess of)
2224 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2226 fragP->relax_marker = 0;
2227 fragP->fr_address = address;
2228 address += fragP->fr_fix;
2230 switch (fragP->fr_type)
2233 address += fragP->fr_offset * fragP->fr_var;
2240 addressT offset = relax_align (address, (int) fragP->fr_offset);
2242 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2245 if (offset % fragP->fr_var != 0)
2247 as_bad_where (fragP->fr_file, fragP->fr_line,
2248 _("alignment padding (%lu bytes) not a multiple of %ld"),
2249 (unsigned long) offset, (long) fragP->fr_var);
2250 offset -= (offset % fragP->fr_var);
2259 /* Assume .org is nugatory. It will grow with 1st relax. */
2262 case rs_machine_dependent:
2263 /* If fr_symbol is an expression, this call to
2264 resolve_symbol_value sets up the correct segment, which will
2265 likely be needed in md_estimate_size_before_relax. */
2266 if (fragP->fr_symbol)
2267 resolve_symbol_value (fragP->fr_symbol);
2269 address += md_estimate_size_before_relax (fragP, segment);
2272 #ifndef WORKING_DOT_WORD
2273 /* Broken words don't concern us yet. */
2274 case rs_broken_word:
2279 /* Initial guess is always 1; doing otherwise can result in
2280 stable solutions that are larger than the minimum. */
2281 address += fragP->fr_offset = 1;
2285 address += eh_frame_estimate_size_before_relax (fragP);
2289 address += dwarf2dbg_estimate_size_before_relax (fragP);
2293 BAD_CASE (fragP->fr_type);
2300 long stretch; /* May be any size, 0 or negative. */
2301 /* Cumulative number of addresses we have relaxed this pass.
2302 We may have relaxed more than one address. */
2303 int stretched; /* Have we stretched on this pass? */
2304 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2305 grew, and another shrank. If a branch instruction doesn't fit anymore,
2306 we could be scrod. */
2313 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2316 addressT was_address;
2320 fragP->relax_marker ^= 1;
2321 was_address = fragP->fr_address;
2322 address = fragP->fr_address += stretch;
2323 symbolP = fragP->fr_symbol;
2324 offset = fragP->fr_offset;
2326 switch (fragP->fr_type)
2328 case rs_fill: /* .fill never relaxes. */
2332 #ifndef WORKING_DOT_WORD
2333 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2334 for it I do not want to write it. I do not want to have
2335 anything to do with it. This is not the proper way to
2336 implement this misfeature. */
2337 case rs_broken_word:
2339 struct broken_word *lie;
2340 struct broken_word *untruth;
2342 /* Yes this is ugly (storing the broken_word pointer
2343 in the symbol slot). Still, this whole chunk of
2344 code is ugly, and I don't feel like doing anything
2345 about it. Think of it as stubbornness in action. */
2347 for (lie = (struct broken_word *) (fragP->fr_symbol);
2348 lie && lie->dispfrag == fragP;
2349 lie = lie->next_broken_word)
2355 offset = (S_GET_VALUE (lie->add)
2357 - S_GET_VALUE (lie->sub));
2358 if (offset <= -32768 || offset >= 32767)
2360 if (flag_warn_displacement)
2363 sprint_value (buf, (addressT) lie->addnum);
2364 as_warn_where (fragP->fr_file, fragP->fr_line,
2365 _(".word %s-%s+%s didn't fit"),
2366 S_GET_NAME (lie->add),
2367 S_GET_NAME (lie->sub),
2371 if (fragP->fr_subtype == 0)
2373 fragP->fr_subtype++;
2374 growth += md_short_jump_size;
2376 for (untruth = lie->next_broken_word;
2377 untruth && untruth->dispfrag == lie->dispfrag;
2378 untruth = untruth->next_broken_word)
2379 if ((symbol_get_frag (untruth->add)
2380 == symbol_get_frag (lie->add))
2381 && (S_GET_VALUE (untruth->add)
2382 == S_GET_VALUE (lie->add)))
2385 untruth->use_jump = lie;
2387 growth += md_long_jump_size;
2392 } /* case rs_broken_word */
2398 addressT oldoff, newoff;
2400 oldoff = relax_align (was_address + fragP->fr_fix,
2402 newoff = relax_align (address + fragP->fr_fix,
2405 if (fragP->fr_subtype != 0)
2407 if (oldoff > fragP->fr_subtype)
2409 if (newoff > fragP->fr_subtype)
2413 growth = newoff - oldoff;
2419 addressT target = offset;
2424 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2425 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2426 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2427 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2428 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2429 know (symbolP->sy_frag);
2430 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2431 || (symbolP->sy_frag == &zero_address_frag));
2433 /* Convert from an actual address to an octet offset
2434 into the section. Here it is assumed that the
2435 section's VMA is zero, and can omit subtracting it
2436 from the symbol's value to get the address offset. */
2437 know (S_GET_SECTION (symbolP)->vma == 0);
2438 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2441 know (fragP->fr_next);
2442 after = fragP->fr_next->fr_address;
2443 growth = target - after;
2446 /* Growth may be negative, but variable part of frag
2447 cannot have fewer than 0 chars. That is, we can't
2449 as_bad_where (fragP->fr_file, fragP->fr_line,
2450 _("attempt to .org backwards"));
2452 /* We've issued an error message. Change the
2453 frag to avoid cascading errors. */
2454 fragP->fr_type = rs_align;
2455 fragP->fr_subtype = 0;
2456 fragP->fr_offset = 0;
2457 fragP->fr_fix = after - address;
2461 /* This is an absolute growth factor */
2472 amount = S_GET_VALUE (symbolP);
2473 if (S_GET_SEGMENT (symbolP) != absolute_section
2474 || S_IS_COMMON (symbolP)
2475 || ! S_IS_DEFINED (symbolP))
2477 as_bad_where (fragP->fr_file, fragP->fr_line,
2478 _(".space specifies non-absolute value"));
2479 /* Prevent repeat of this error message. */
2480 fragP->fr_symbol = 0;
2482 else if (amount < 0)
2484 as_warn_where (fragP->fr_file, fragP->fr_line,
2485 _(".space or .fill with negative value, ignored"));
2486 fragP->fr_symbol = 0;
2489 growth = (was_address + fragP->fr_fix + amount
2490 - fragP->fr_next->fr_address);
2494 case rs_machine_dependent:
2495 #ifdef md_relax_frag
2496 growth = md_relax_frag (segment, fragP, stretch);
2498 #ifdef TC_GENERIC_RELAX_TABLE
2499 /* The default way to relax a frag is to look through
2500 TC_GENERIC_RELAX_TABLE. */
2501 growth = relax_frag (segment, fragP, stretch);
2502 #endif /* TC_GENERIC_RELAX_TABLE */
2511 value = resolve_symbol_value (fragP->fr_symbol);
2512 size = sizeof_leb128 (value, fragP->fr_subtype);
2513 growth = size - fragP->fr_offset;
2514 fragP->fr_offset = size;
2519 growth = eh_frame_relax_frag (fragP);
2523 growth = dwarf2dbg_relax_frag (fragP);
2527 BAD_CASE (fragP->fr_type);
2535 } /* For each frag in the segment. */
2537 while (stretched); /* Until nothing further to relax. */
2541 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2542 if (fragP->last_fr_address != fragP->fr_address)
2544 fragP->last_fr_address = fragP->fr_address;
2550 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2552 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2553 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2558 Go through all the fixS's in a segment and see which ones can be
2559 handled now. (These consist of fixS where we have since discovered
2560 the value of a symbol, or the address of the frag involved.)
2561 For each one, call md_apply_fix3 to put the fix into the frag data.
2563 Result is a count of how many relocation structs will be needed to
2564 handle the remaining fixS's that we couldn't completely handle here.
2565 These will be output later by emit_relocations(). */
2568 fixup_segment (fixP, this_segment_type)
2569 register fixS *fixP;
2570 segT this_segment_type; /* N_TYPE bits for segment. */
2572 long seg_reloc_count = 0;
2573 symbolS *add_symbolP;
2574 symbolS *sub_symbolP;
2581 segT add_symbol_segment = absolute_section;
2583 /* If the linker is doing the relaxing, we must not do any fixups.
2585 Well, strictly speaking that's not true -- we could do any that are
2586 PC-relative and don't cross regions that could change size. And for the
2587 i960 (the only machine for which we've got a relaxing linker right now),
2588 we might be able to turn callx/callj into bal anyways in cases where we
2589 know the maximum displacement. */
2590 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment_type))
2592 for (; fixP; fixP = fixP->fx_next)
2594 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2595 return seg_reloc_count;
2598 for (; fixP; fixP = fixP->fx_next)
2601 fprintf (stderr, "\nprocessing fixup:\n");
2605 fragP = fixP->fx_frag;
2607 where = fixP->fx_where;
2608 place = fragP->fr_literal + where;
2609 size = fixP->fx_size;
2610 add_symbolP = fixP->fx_addsy;
2611 #ifdef TC_VALIDATE_FIX
2612 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2614 sub_symbolP = fixP->fx_subsy;
2615 add_number = fixP->fx_offset;
2616 pcrel = fixP->fx_pcrel;
2619 if (add_symbolP != NULL
2620 && symbol_mri_common_p (add_symbolP))
2622 know (add_symbolP->sy_value.X_op == O_symbol);
2623 add_number += S_GET_VALUE (add_symbolP);
2624 fixP->fx_offset = add_number;
2625 add_symbolP = fixP->fx_addsy =
2626 symbol_get_value_expression (add_symbolP)->X_add_symbol;
2630 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2634 resolve_symbol_value (sub_symbolP);
2635 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2637 if (add_symbolP != NULL)
2639 add_number += S_GET_VALUE (add_symbolP);
2641 fixP->fx_addsy = NULL;
2644 /* It's just -sym. */
2645 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2647 add_number -= S_GET_VALUE (sub_symbolP);
2648 fixP->fx_subsy = NULL;
2651 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2653 /* Should try converting to a constant. */
2658 as_bad_where (fixP->fx_file, fixP->fx_line,
2659 _("negative of non-absolute symbol `%s'"),
2660 S_GET_NAME (sub_symbolP));
2662 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2663 && SEG_NORMAL (add_symbol_segment))
2665 /* Difference of 2 symbols from same segment.
2666 Can't make difference of 2 undefineds: 'value' means
2667 something different for N_UNDF. */
2669 /* Makes no sense to use the difference of 2 arbitrary symbols
2670 as the target of a call instruction. */
2672 as_bad_where (fixP->fx_file, fixP->fx_line,
2673 _("callj to difference of two symbols"));
2674 #endif /* TC_I960 */
2675 add_number += (S_GET_VALUE (add_symbolP)
2676 - S_GET_VALUE (sub_symbolP));
2679 /* See the comment below about 68k weirdness. */
2683 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2686 pcrel = 0; /* No further pcrel processing. */
2688 /* Let the target machine make the final determination
2689 as to whether or not a relocation will be needed to
2690 handle this fixup. */
2691 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2694 fixP->fx_addsy = NULL;
2695 fixP->fx_subsy = NULL;
2700 /* Different segments in subtraction. */
2701 know (!(S_IS_EXTERNAL (sub_symbolP)
2702 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2704 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2705 add_number -= S_GET_VALUE (sub_symbolP);
2708 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2710 /* Make it pc-relative. */
2713 /* Do this for m68k even if it's already described
2714 as pc-relative. On the m68k, an operand of
2715 "pc@(foo-.-2)" should address "foo" in a
2716 pc-relative mode. */
2721 add_number += MD_PCREL_FROM_SECTION (fixP,
2727 add_number -= S_GET_VALUE (sub_symbolP);
2732 #ifdef UNDEFINED_DIFFERENCE_OK
2733 /* The PA needs this for PIC code generation. We basically
2734 don't want to do anything if we have the difference of two
2735 symbols at this point. */
2738 /* Leave it alone. */
2741 #ifdef BFD_ASSEMBLER
2742 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2743 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2745 /* Leave it alone. */
2751 sprint_value (buf, fragP->fr_address + where);
2752 as_bad_where (fixP->fx_file, fixP->fx_line,
2753 _("subtraction of two symbols in different sections `%s' {%s section} - `%s' {%s section} at file address %s"),
2754 S_GET_NAME (add_symbolP),
2755 segment_name (S_GET_SEGMENT (add_symbolP)),
2756 S_GET_NAME (sub_symbolP),
2757 segment_name (S_GET_SEGMENT (sub_symbolP)),
2765 if (add_symbol_segment == this_segment_type && pcrel && !plt
2766 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2768 /* This fixup was made when the symbol's segment was
2769 SEG_UNKNOWN, but it is now in the local segment.
2770 So we know how to do the address without relocation. */
2772 /* reloc_callj() may replace a 'call' with a 'calls' or a
2773 'bal', in which cases it modifies *fixP as appropriate.
2774 In the case of a 'calls', no further work is required,
2775 and *fixP has been set up to make the rest of the code
2778 #endif /* TC_I960 */
2780 add_number += S_GET_VALUE (add_symbolP);
2781 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2782 /* Lie. Don't want further pcrel processing. */
2785 /* Let the target machine make the final determination
2786 as to whether or not a relocation will be needed to
2787 handle this fixup. */
2788 if (!TC_FORCE_RELOCATION (fixP))
2791 fixP->fx_addsy = NULL;
2796 if (add_symbol_segment == absolute_section
2800 /* See comment about reloc_callj() above. */
2802 #endif /* TC_I960 */
2803 add_number += S_GET_VALUE (add_symbolP);
2805 /* Let the target machine make the final determination
2806 as to whether or not a relocation will be needed to
2807 handle this fixup. */
2809 if (!TC_FORCE_RELOCATION (fixP))
2811 fixP->fx_addsy = NULL;
2815 else if (add_symbol_segment == undefined_section
2816 #ifdef BFD_ASSEMBLER
2817 || bfd_is_com_section (add_symbol_segment)
2818 || (bfd_get_section_flags (stdoutput,
2825 if ((int) fixP->fx_bit_fixP == 13)
2827 /* This is a COBR instruction. They have only a
2828 13-bit displacement and are only to be used
2829 for local branches: flag as error, don't generate
2831 as_bad_where (fixP->fx_file, fixP->fx_line,
2832 _("can't use COBR format with external label"));
2833 fixP->fx_addsy = NULL;
2837 #endif /* TC_I960 */
2841 if (S_IS_COMMON (add_symbolP))
2842 add_number += S_GET_VALUE (add_symbolP);
2843 #endif /* TE_I386AIX */
2844 #endif /* OBJ_COFF */
2850 if (TC_FIX_ADJUSTABLE (fixP))
2851 add_number += S_GET_VALUE (add_symbolP);
2858 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2859 if (add_symbolP == 0)
2861 #ifndef BFD_ASSEMBLER
2862 fixP->fx_addsy = &abs_symbol;
2864 fixP->fx_addsy = section_symbol (absolute_section);
2866 symbol_mark_used_in_reloc (fixP->fx_addsy);
2872 md_apply_fix3 (fixP, & add_number, this_segment_type);
2874 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2876 if ((size_t) size < sizeof (valueT))
2881 mask--; /* Set all bits to one. */
2882 mask <<= size * 8 - (fixP->fx_signed ? 1 : 0);
2883 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2885 char buf[50], buf2[50];
2886 sprint_value (buf, fragP->fr_address + where);
2887 if (add_number > 1000)
2888 sprint_value (buf2, add_number);
2890 sprintf (buf2, "%ld", (long) add_number);
2891 as_bad_where (fixP->fx_file, fixP->fx_line,
2892 _("value of %s too large for field of %d bytes at %s"),
2894 } /* Generic error checking. */
2896 #ifdef WARN_SIGNED_OVERFLOW_WORD
2897 /* Warn if a .word value is too large when treated as a signed
2898 number. We already know it is not too negative. This is to
2899 catch over-large switches generated by gcc on the 68k. */
2900 if (!flag_signed_overflow_ok
2902 && add_number > 0x7fff)
2903 as_bad_where (fixP->fx_file, fixP->fx_line,
2904 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2906 (unsigned long) (fragP->fr_address + where));
2908 } /* Not a bit fix. */
2910 #ifdef TC_VALIDATE_FIX
2911 skip: ATTRIBUTE_UNUSED_LABEL
2915 fprintf (stderr, "result:\n");
2918 } /* For each fixS in this segment. */
2920 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2921 return seg_reloc_count;
2924 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2927 number_to_chars_bigendian (buf, val, n)
2932 if ((size_t) n > sizeof (val) || n <= 0)
2936 buf[n] = val & 0xff;
2942 number_to_chars_littleendian (buf, val, n)
2947 if ((size_t) n > sizeof (val) || n <= 0)
2951 *buf++ = val & 0xff;
2957 write_print_statistics (file)
2960 fprintf (file, "fixups: %d\n", n_fixups);
2963 /* For debugging. */
2964 extern int indent_level;
2971 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2973 fprintf (stderr, " pcrel");
2974 if (fixp->fx_pcrel_adjust)
2975 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2976 if (fixp->fx_im_disp)
2979 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2981 fprintf (stderr, " im_disp");
2985 fprintf (stderr, " tcbit");
2987 fprintf (stderr, " done");
2988 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2989 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2990 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2991 #ifdef BFD_ASSEMBLER
2992 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2995 #ifdef NEED_FX_R_TYPE
2996 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
3001 fprintf (stderr, "\n +<");
3002 print_symbol_value_1 (stderr, fixp->fx_addsy);
3003 fprintf (stderr, ">");
3007 fprintf (stderr, "\n -<");
3008 print_symbol_value_1 (stderr, fixp->fx_subsy);
3009 fprintf (stderr, ">");
3011 fprintf (stderr, "\n");
3012 #ifdef TC_FIX_DATA_PRINT
3013 TC_FIX_DATA_PRINT (stderr, fixp);