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 /* 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));
146 /* Create a fixS in obstack 'notes'. */
149 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
151 fragS *frag; /* Which frag? */
152 int where; /* Where in that frag? */
153 int size; /* 1, 2, or 4 usually. */
154 symbolS *add_symbol; /* X_add_symbol. */
155 symbolS *sub_symbol; /* X_op_symbol. */
156 offsetT offset; /* X_add_number. */
157 int pcrel; /* TRUE if PC-relative relocation. */
159 bfd_reloc_code_real_type r_type; /* Relocation type. */
161 int r_type; /* Relocation type. */
168 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
170 fixP->fx_frag = frag;
171 fixP->fx_where = where;
172 fixP->fx_size = size;
173 /* We've made fx_size a narrow field; check that it's wide enough. */
174 if (fixP->fx_size != size)
176 as_bad (_("field fx_size too small to hold %d"), size);
179 fixP->fx_addsy = add_symbol;
180 fixP->fx_subsy = sub_symbol;
181 fixP->fx_offset = offset;
182 fixP->fx_pcrel = pcrel;
184 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
185 fixP->fx_r_type = r_type;
187 fixP->fx_im_disp = 0;
188 fixP->fx_pcrel_adjust = 0;
189 fixP->fx_bit_fixP = 0;
190 fixP->fx_addnumber = 0;
193 fixP->fx_no_overflow = 0;
197 fixP->fx_cgen.insn = NULL;
198 fixP->fx_cgen.opinfo = 0;
202 TC_INIT_FIX_DATA (fixP);
205 as_where (&fixP->fx_file, &fixP->fx_line);
207 /* Usually, we want relocs sorted numerically, but while
208 comparing to older versions of gas that have relocs
209 reverse sorted, it is convenient to have this compile
210 time option. xoxorich. */
214 fixS **seg_fix_rootP = (frags_chained
215 ? &seg_info (now_seg)->fix_root
216 : &frchain_now->fix_root);
217 fixS **seg_fix_tailP = (frags_chained
218 ? &seg_info (now_seg)->fix_tail
219 : &frchain_now->fix_tail);
222 #ifdef REVERSE_SORT_RELOCS
224 fixP->fx_next = *seg_fix_rootP;
225 *seg_fix_rootP = fixP;
227 #else /* REVERSE_SORT_RELOCS */
229 fixP->fx_next = NULL;
232 (*seg_fix_tailP)->fx_next = fixP;
234 *seg_fix_rootP = fixP;
235 *seg_fix_tailP = fixP;
237 #endif /* REVERSE_SORT_RELOCS */
243 /* Create a fixup relative to a symbol (plus a constant). */
246 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
247 fragS *frag; /* Which frag? */
248 int where; /* Where in that frag? */
249 int size; /* 1, 2, or 4 usually. */
250 symbolS *add_symbol; /* X_add_symbol. */
251 offsetT offset; /* X_add_number. */
252 int pcrel; /* TRUE if PC-relative relocation. */
254 bfd_reloc_code_real_type r_type; /* Relocation type. */
256 int r_type; /* Relocation type. */
259 return fix_new_internal (frag, where, size, add_symbol,
260 (symbolS *) NULL, offset, pcrel, r_type);
263 /* Create a fixup for an expression. Currently we only support fixups
264 for difference expressions. That is itself more than most object
265 file formats support anyhow. */
268 fix_new_exp (frag, where, size, exp, pcrel, r_type)
269 fragS *frag; /* Which frag? */
270 int where; /* Where in that frag? */
271 int size; /* 1, 2, or 4 usually. */
272 expressionS *exp; /* Expression. */
273 int pcrel; /* TRUE if PC-relative relocation. */
275 bfd_reloc_code_real_type r_type; /* Relocation type. */
277 int r_type; /* Relocation type. */
290 as_bad (_("register value used as expression"));
294 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
295 the difference expression cannot immediately be reduced. */
297 symbolS *stmp = make_expr_symbol (exp);
299 exp->X_op = O_symbol;
300 exp->X_op_symbol = 0;
301 exp->X_add_symbol = stmp;
302 exp->X_add_number = 0;
304 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
308 add = exp->X_add_symbol;
309 off = exp->X_add_number;
311 #if defined(BFD_ASSEMBLER)
312 r_type = BFD_RELOC_RVA;
314 #if defined(TC_RVA_RELOC)
315 r_type = TC_RVA_RELOC;
317 as_fatal (_("rva not supported"));
323 sub = exp->X_add_symbol;
324 off = exp->X_add_number;
328 sub = exp->X_op_symbol;
331 add = exp->X_add_symbol;
334 off = exp->X_add_number;
338 add = make_expr_symbol (exp);
342 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
345 /* Append a string onto another string, bumping the pointer along. */
347 append (charPP, fromP, length)
350 unsigned long length;
352 /* Don't trust memcpy() of 0 chars. */
356 memcpy (*charPP, fromP, length);
360 #ifndef BFD_ASSEMBLER
361 int section_alignment[SEG_MAXIMUM_ORDINAL];
364 /* This routine records the largest alignment seen for each segment.
365 If the beginning of the segment is aligned on the worst-case
366 boundary, all of the other alignments within it will work. At
367 least one object format really uses this info. */
370 record_alignment (seg, align)
371 /* Segment to which alignment pertains. */
373 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
377 if (seg == absolute_section)
380 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
381 bfd_set_section_alignment (stdoutput, seg, align);
383 if (align > section_alignment[(int) seg])
384 section_alignment[(int) seg] = align;
389 get_recorded_alignment (seg)
392 if (seg == absolute_section)
395 return bfd_get_section_alignment (stdoutput, seg);
397 return section_alignment[(int) seg];
403 /* Reset the section indices after removing the gas created sections. */
406 renumber_sections (abfd, sec, countparg)
407 bfd *abfd ATTRIBUTE_UNUSED;
411 int *countp = (int *) countparg;
413 sec->index = *countp;
417 #endif /* defined (BFD_ASSEMBLER) */
419 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
422 chain_frchains_together_1 (section, frchp)
424 struct frchain *frchp;
426 fragS dummy, *prev_frag = &dummy;
428 fixS fix_dummy, *prev_fix = &fix_dummy;
431 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
433 prev_frag->fr_next = frchp->frch_root;
434 prev_frag = frchp->frch_last;
435 assert (prev_frag->fr_type != 0);
437 if (frchp->fix_root != (fixS *) NULL)
439 if (seg_info (section)->fix_root == (fixS *) NULL)
440 seg_info (section)->fix_root = frchp->fix_root;
441 prev_fix->fx_next = frchp->fix_root;
442 seg_info (section)->fix_tail = frchp->fix_tail;
443 prev_fix = frchp->fix_tail;
447 assert (prev_frag->fr_type != 0);
448 prev_frag->fr_next = 0;
457 chain_frchains_together (abfd, section, xxx)
458 bfd *abfd ATTRIBUTE_UNUSED;
460 PTR xxx ATTRIBUTE_UNUSED;
462 segment_info_type *info;
464 /* BFD may have introduced its own sections without using
465 subseg_new, so it is possible that seg_info is NULL. */
466 info = seg_info (section);
467 if (info != (segment_info_type *) NULL)
468 info->frchainP->frch_last
469 = chain_frchains_together_1 (section, info->frchainP);
471 /* Now that we've chained the frags together, we must add new fixups
472 to the segment, not to the frag chain. */
478 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
481 remove_subsegs (head, seg, root, last)
487 *root = head->frch_root;
488 *last = chain_frchains_together_1 (seg, head);
493 #if defined (BFD_ASSEMBLER) || !defined (BFD)
497 cvt_frag_to_fill (sec, fragP)
498 segT sec ATTRIBUTE_UNUSED;
502 cvt_frag_to_fill (headersP, sec, fragP)
503 object_headers *headersP;
508 switch (fragP->fr_type)
516 HANDLE_ALIGN (fragP);
518 know (fragP->fr_next != NULL);
519 fragP->fr_offset = (fragP->fr_next->fr_address
521 - fragP->fr_fix) / fragP->fr_var;
522 if (fragP->fr_offset < 0)
524 as_bad_where (fragP->fr_file, fragP->fr_line,
525 _("attempt to .org/.space backwards? (%ld)"),
526 (long) fragP->fr_offset);
527 fragP->fr_offset = 0;
529 fragP->fr_type = rs_fill;
537 valueT value = S_GET_VALUE (fragP->fr_symbol);
540 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
543 fragP->fr_fix += size;
544 fragP->fr_type = rs_fill;
546 fragP->fr_offset = 0;
547 fragP->fr_symbol = NULL;
552 eh_frame_convert_frag (fragP);
556 dwarf2dbg_convert_frag (fragP);
559 case rs_machine_dependent:
561 md_convert_frag (stdoutput, sec, fragP);
563 md_convert_frag (headersP, sec, fragP);
566 assert (fragP->fr_next == NULL
567 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
570 /* After md_convert_frag, we make the frag into a ".space 0".
571 md_convert_frag() should set up any fixSs and constants
576 #ifndef WORKING_DOT_WORD
579 struct broken_word *lie;
581 if (fragP->fr_subtype)
583 fragP->fr_fix += md_short_jump_size;
584 for (lie = (struct broken_word *) (fragP->fr_symbol);
585 lie && lie->dispfrag == fragP;
586 lie = lie->next_broken_word)
588 fragP->fr_fix += md_long_jump_size;
596 BAD_CASE (fragP->fr_type);
601 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
604 static void relax_seg PARAMS ((bfd *, asection *, PTR));
606 relax_seg (abfd, sec, xxx)
607 bfd *abfd ATTRIBUTE_UNUSED;
611 segment_info_type *seginfo = seg_info (sec);
613 if (seginfo && seginfo->frchainP
614 && relax_segment (seginfo->frchainP->frch_root, sec))
616 int *result = (int *) xxx;
621 static void size_seg PARAMS ((bfd *, asection *, PTR));
623 size_seg (abfd, sec, xxx)
626 PTR xxx ATTRIBUTE_UNUSED;
630 segment_info_type *seginfo;
632 valueT size, newsize;
634 subseg_change (sec, 0);
636 seginfo = seg_info (sec);
637 if (seginfo && seginfo->frchainP)
639 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
640 cvt_frag_to_fill (sec, fragp);
641 for (fragp = seginfo->frchainP->frch_root;
643 fragp = fragp->fr_next)
644 /* Walk to last elt. */
646 size = fragp->fr_address + fragp->fr_fix;
651 flags = bfd_get_section_flags (abfd, sec);
653 if (size > 0 && ! seginfo->bss)
654 flags |= SEC_HAS_CONTENTS;
656 /* @@ This is just an approximation. */
657 if (seginfo && seginfo->fix_root)
661 x = bfd_set_section_flags (abfd, sec, flags);
664 newsize = md_section_align (sec, size);
665 x = bfd_set_section_size (abfd, sec, newsize);
668 /* If the size had to be rounded up, add some padding in the last
670 assert (newsize >= size);
673 fragS *last = seginfo->frchainP->frch_last;
674 fragp = seginfo->frchainP->frch_root;
675 while (fragp->fr_next != last)
676 fragp = fragp->fr_next;
677 last->fr_address = size;
678 if ((newsize - size) % fragp->fr_var == 0)
679 fragp->fr_offset += (newsize - size) / fragp->fr_var;
681 /* If we hit this abort, it's likely due to subsegs_finish not
682 providing sufficient alignment on the last frag, and the
683 machine dependent code using alignment frags with fr_var
688 #ifdef tc_frob_section
689 tc_frob_section (sec);
691 #ifdef obj_frob_section
692 obj_frob_section (sec);
698 dump_section_relocs (abfd, sec, stream_)
699 bfd *abfd ATTRIBUTE_UNUSED;
703 FILE *stream = (FILE *) stream_;
704 segment_info_type *seginfo = seg_info (sec);
705 fixS *fixp = seginfo->fix_root;
710 fprintf (stream, "sec %s relocs:\n", sec->name);
713 symbolS *s = fixp->fx_addsy;
715 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
716 (int) fixp->fx_r_type);
718 fprintf (stream, "no sym\n");
721 print_symbol_value_1 (stream, s);
722 fprintf (stream, "\n");
724 fixp = fixp->fx_next;
728 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
731 #ifndef EMIT_SECTION_SYMBOLS
732 #define EMIT_SECTION_SYMBOLS 1
736 adjust_reloc_syms (abfd, sec, xxx)
737 bfd *abfd ATTRIBUTE_UNUSED;
739 PTR xxx ATTRIBUTE_UNUSED;
741 segment_info_type *seginfo = seg_info (sec);
747 dump_section_relocs (abfd, sec, stderr);
749 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
753 else if (fixp->fx_addsy)
759 fprintf (stderr, "\n\nadjusting fixup:\n");
763 sym = fixp->fx_addsy;
765 /* All symbols should have already been resolved at this
766 point. It is possible to see unresolved expression
767 symbols, though, since they are not in the regular symbol
770 resolve_symbol_value (sym);
772 if (fixp->fx_subsy != NULL)
773 resolve_symbol_value (fixp->fx_subsy);
775 /* If this symbol is equated to an undefined symbol, convert
776 the fixup to being against that symbol. */
777 if (sym != NULL && symbol_equated_reloc_p (sym))
779 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
780 sym = symbol_get_value_expression (sym)->X_add_symbol;
781 fixp->fx_addsy = sym;
784 if (sym != NULL && symbol_mri_common_p (sym))
786 /* These symbols are handled specially in fixup_segment. */
790 symsec = S_GET_SEGMENT (sym);
795 if (bfd_is_abs_section (symsec))
797 /* The fixup_segment routine will not use this symbol in a
798 relocation unless TC_FORCE_RELOCATION returns 1. */
799 if (TC_FORCE_RELOCATION (fixp))
801 symbol_mark_used_in_reloc (fixp->fx_addsy);
802 #ifdef UNDEFINED_DIFFERENCE_OK
803 if (fixp->fx_subsy != NULL)
804 symbol_mark_used_in_reloc (fixp->fx_subsy);
810 /* If it's one of these sections, assume the symbol is
811 definitely going to be output. The code in
812 md_estimate_size_before_relax in tc-mips.c uses this test
813 as well, so if you change this code you should look at that
815 if (bfd_is_und_section (symsec)
816 || bfd_is_com_section (symsec))
818 symbol_mark_used_in_reloc (fixp->fx_addsy);
819 #ifdef UNDEFINED_DIFFERENCE_OK
820 /* We have the difference of an undefined symbol and some
821 other symbol. Make sure to mark the other symbol as used
822 in a relocation so that it will always be output. */
824 symbol_mark_used_in_reloc (fixp->fx_subsy);
829 /* Don't try to reduce relocs which refer to non-local symbols
830 in .linkonce sections. It can lead to confusion when a
831 debugging section refers to a .linkonce section. I hope
832 this will always be correct. */
833 if (symsec != sec && ! S_IS_LOCAL (sym))
839 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
844 /* The GNU toolchain uses an extension for ELF: a section
845 beginning with the magic string .gnu.linkonce is a
847 if (strncmp (segment_name (symsec), ".gnu.linkonce",
848 sizeof ".gnu.linkonce" - 1) == 0)
854 symbol_mark_used_in_reloc (fixp->fx_addsy);
855 #ifdef UNDEFINED_DIFFERENCE_OK
856 if (fixp->fx_subsy != NULL)
857 symbol_mark_used_in_reloc (fixp->fx_subsy);
863 /* Since we're reducing to section symbols, don't attempt to reduce
864 anything that's already using one. */
865 if (symbol_section_p (sym))
867 symbol_mark_used_in_reloc (fixp->fx_addsy);
872 /* We can never adjust a reloc against a weak symbol. If we
873 did, and the weak symbol was overridden by a real symbol
874 somewhere else, then our relocation would be pointing at
875 the wrong area of memory. */
878 symbol_mark_used_in_reloc (fixp->fx_addsy);
882 /* Never adjust a reloc against local symbol in a merge section
883 with non-zero addend. */
884 if ((symsec->flags & SEC_MERGE) != 0
885 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
887 symbol_mark_used_in_reloc (fixp->fx_addsy);
891 /* Never adjust a reloc against TLS local symbol. */
892 if (symsec->flags & SEC_THREAD_LOCAL)
894 symbol_mark_used_in_reloc (fixp->fx_addsy);
899 /* Is there some other reason we can't adjust this one? (E.g.,
900 call/bal links in i960-bout symbols.) */
901 #ifdef obj_fix_adjustable
902 if (! obj_fix_adjustable (fixp))
904 symbol_mark_used_in_reloc (fixp->fx_addsy);
909 /* Is there some other (target cpu dependent) reason we can't adjust
910 this one? (E.g. relocations involving function addresses on
912 #ifdef tc_fix_adjustable
913 if (! tc_fix_adjustable (fixp))
915 symbol_mark_used_in_reloc (fixp->fx_addsy);
920 /* If the section symbol isn't going to be output, the relocs
921 at least should still work. If not, figure out what to do
922 when we run into that case.
924 We refetch the segment when calling section_symbol, rather
925 than using symsec, because S_GET_VALUE may wind up changing
926 the section when it calls resolve_symbol_value. */
927 fixp->fx_offset += S_GET_VALUE (sym);
928 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
929 symbol_mark_used_in_reloc (fixp->fx_addsy);
931 fprintf (stderr, "\nadjusted fixup:\n");
938 #if 1 /* def RELOC_REQUIRES_SYMBOL */
941 /* There was no symbol required by this relocation. However,
942 BFD doesn't really handle relocations without symbols well.
943 (At least, the COFF support doesn't.) So for now we fake up
944 a local symbol in the absolute section. */
946 fixp->fx_addsy = section_symbol (absolute_section);
948 fixp->fx_addsy->sy_used_in_reloc = 1;
953 dump_section_relocs (abfd, sec, stderr);
957 write_relocs (abfd, sec, xxx)
960 PTR xxx ATTRIBUTE_UNUSED;
962 segment_info_type *seginfo = seg_info (sec);
969 /* If seginfo is NULL, we did not create this section; don't do
974 fixup_segment (seginfo->fix_root, sec);
977 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
980 #ifndef RELOC_EXPANSION_POSSIBLE
981 /* Set up reloc information as well. */
982 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
983 memset ((char *) relocs, 0, n * sizeof (arelent *));
986 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
989 bfd_reloc_status_type s;
998 /* If this is an undefined symbol which was equated to another
999 symbol, then generate the reloc against the latter symbol
1000 rather than the former. */
1001 sym = fixp->fx_addsy;
1002 while (symbol_equated_reloc_p (sym))
1006 /* We must avoid looping, as that can occur with a badly
1008 n = symbol_get_value_expression (sym)->X_add_symbol;
1011 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
1014 fixp->fx_addsy = sym;
1016 reloc = tc_gen_reloc (sec, fixp);
1024 /* This test is triggered inappropriately for the SH. */
1025 if (fixp->fx_where + fixp->fx_size
1026 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1030 s = bfd_install_relocation (stdoutput, reloc,
1031 fixp->fx_frag->fr_literal,
1032 fixp->fx_frag->fr_address,
1038 case bfd_reloc_overflow:
1039 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
1041 case bfd_reloc_outofrange:
1042 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation out of range"));
1045 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1046 fixp->fx_file, fixp->fx_line, s);
1048 relocs[i++] = reloc;
1051 n = n * MAX_RELOC_EXPANSION;
1052 /* Set up reloc information as well. */
1053 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
1056 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1060 bfd_reloc_status_type s;
1070 /* If this is an undefined symbol which was equated to another
1071 symbol, then generate the reloc against the latter symbol
1072 rather than the former. */
1073 sym = fixp->fx_addsy;
1074 while (symbol_equated_reloc_p (sym))
1075 sym = symbol_get_value_expression (sym)->X_add_symbol;
1076 fixp->fx_addsy = sym;
1078 reloc = tc_gen_reloc (sec, fixp);
1080 for (j = 0; reloc[j]; j++)
1082 relocs[i++] = reloc[j];
1085 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1086 if (fixp->fx_where + fixp->fx_size
1087 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1088 as_bad_where (fixp->fx_file, fixp->fx_line,
1089 _("internal error: fixup not contained within frag"));
1090 for (j = 0; reloc[j]; j++)
1092 s = bfd_install_relocation (stdoutput, reloc[j],
1093 fixp->fx_frag->fr_literal,
1094 fixp->fx_frag->fr_address,
1100 case bfd_reloc_overflow:
1101 as_bad_where (fixp->fx_file, fixp->fx_line,
1102 _("relocation overflow"));
1105 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1106 fixp->fx_file, fixp->fx_line);
1117 sympp = bfd_get_outsymbols (stdoutput);
1118 nsyms = bfd_get_symcount (stdoutput);
1119 for (i = 0; i < n; i++)
1120 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1122 for (j = 0; j < nsyms; j++)
1123 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1132 bfd_set_reloc (stdoutput, sec, relocs, n);
1134 bfd_set_section_flags (abfd, sec,
1135 (bfd_get_section_flags (abfd, sec)
1136 & (flagword) ~SEC_RELOC));
1138 #ifdef SET_SECTION_RELOCS
1139 SET_SECTION_RELOCS (sec, relocs, n);
1147 fprintf (stderr, "relocs for sec %s\n", sec->name);
1148 for (i = 0; i < n; i++)
1151 s = *r->sym_ptr_ptr;
1152 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1153 i, r, r->address, s->name, r->addend);
1160 write_contents (abfd, sec, xxx)
1161 bfd *abfd ATTRIBUTE_UNUSED;
1163 PTR xxx ATTRIBUTE_UNUSED;
1165 segment_info_type *seginfo = seg_info (sec);
1166 unsigned long offset = 0;
1169 /* Write out the frags. */
1171 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1174 for (f = seginfo->frchainP->frch_root;
1179 unsigned long fill_size;
1183 assert (f->fr_type == rs_fill);
1186 x = bfd_set_section_contents (stdoutput, sec,
1187 f->fr_literal, (file_ptr) offset,
1188 (bfd_size_type) f->fr_fix);
1191 bfd_perror (stdoutput->filename);
1192 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1193 exit (EXIT_FAILURE);
1195 offset += f->fr_fix;
1197 fill_literal = f->fr_literal + f->fr_fix;
1198 fill_size = f->fr_var;
1199 count = f->fr_offset;
1200 assert (count >= 0);
1201 if (fill_size && count)
1204 if (fill_size > sizeof (buf))
1206 /* Do it the old way. Can this ever happen? */
1209 x = bfd_set_section_contents (stdoutput, sec,
1212 (bfd_size_type) fill_size);
1215 bfd_perror (stdoutput->filename);
1216 as_perror (_("FATAL: Can't write %s"),
1217 stdoutput->filename);
1218 exit (EXIT_FAILURE);
1220 offset += fill_size;
1225 /* Build a buffer full of fill objects and output it as
1226 often as necessary. This saves on the overhead of
1227 potentially lots of bfd_set_section_contents calls. */
1231 n_per_buf = sizeof (buf);
1232 memset (buf, *fill_literal, n_per_buf);
1237 n_per_buf = sizeof (buf) / fill_size;
1238 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1239 memcpy (bufp, fill_literal, fill_size);
1241 for (; count > 0; count -= n_per_buf)
1243 n_per_buf = n_per_buf > count ? count : n_per_buf;
1244 x = bfd_set_section_contents
1245 (stdoutput, sec, buf, (file_ptr) offset,
1246 (bfd_size_type) n_per_buf * fill_size);
1248 as_fatal (_("cannot write to output file"));
1249 offset += n_per_buf * fill_size;
1257 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1259 merge_data_into_text ()
1261 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1262 seg_info (text_section)->frchainP->frch_last->fr_next =
1263 seg_info (data_section)->frchainP->frch_root;
1264 seg_info (text_section)->frchainP->frch_last =
1265 seg_info (data_section)->frchainP->frch_last;
1266 seg_info (data_section)->frchainP = 0;
1270 text_last_frag->fr_next = data_frag_root;
1271 text_last_frag = data_last_frag;
1272 data_last_frag = NULL;
1273 data_frag_root = NULL;
1276 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1277 tmp->fx_next = data_fix_root;
1278 text_fix_tail = data_fix_tail;
1281 text_fix_root = data_fix_root;
1282 data_fix_root = NULL;
1285 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1287 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1289 relax_and_size_all_segments ()
1293 relax_segment (text_frag_root, SEG_TEXT);
1294 relax_segment (data_frag_root, SEG_DATA);
1295 relax_segment (bss_frag_root, SEG_BSS);
1297 /* Now the addresses of frags are correct within the segment. */
1298 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1299 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1300 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1302 /* Join the 2 segments into 1 huge segment.
1303 To do this, re-compute every rn_address in the SEG_DATA frags.
1304 Then join the data frags after the text frags.
1306 Determine a_data [length of data segment]. */
1309 register relax_addressT slide;
1311 know ((text_last_frag->fr_type == rs_fill)
1312 && (text_last_frag->fr_offset == 0));
1314 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1315 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1316 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1318 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1319 /* For b.out: If the data section has a strict alignment
1320 requirement, its load address in the .o file will be
1321 rounded up from the size of the text section. These
1322 two values are *not* the same! Similarly for the bss
1324 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1327 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1328 fragP->fr_address += slide;
1330 know (text_last_frag != 0);
1331 text_last_frag->fr_next = data_frag_root;
1335 H_SET_DATA_SIZE (&headers, 0);
1339 /* See above comments on b.out data section address. */
1342 if (data_last_frag == 0)
1343 bss_vma = H_GET_TEXT_SIZE (&headers);
1345 bss_vma = data_last_frag->fr_address;
1346 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1347 bss_address_frag.fr_address = bss_vma;
1349 #else /* ! OBJ_BOUT */
1350 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1351 H_GET_DATA_SIZE (&headers));
1353 #endif /* ! OBJ_BOUT */
1355 /* Slide all the frags. */
1358 relax_addressT slide = bss_address_frag.fr_address;
1360 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1361 fragP->fr_address += slide;
1365 H_SET_BSS_SIZE (&headers,
1366 bss_last_frag->fr_address - bss_frag_root->fr_address);
1368 H_SET_BSS_SIZE (&headers, 0);
1370 #endif /* ! BFD_ASSEMBLER && ! BFD */
1372 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1374 #ifdef BFD_ASSEMBLER
1382 extern PTR bfd_alloc PARAMS ((bfd *, bfd_size_type));
1384 /* Count symbols. We can't rely on a count made by the loop in
1385 write_object_file, because *_frob_file may add a new symbol or
1388 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1394 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1396 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1397 symp = symbol_rootP;
1398 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1400 asympp[i] = symbol_get_bfdsym (symp);
1401 symbol_mark_written (symp);
1406 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1407 assert (result == true);
1408 symbol_table_frozen = 1;
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
1420 /* The last subsegment gets an aligment corresponding to the alignment
1421 of the section. This allows proper nop-filling at the end of
1422 code-bearing sections. */
1423 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1424 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1425 ? get_recorded_alignment (SEG) : 0)
1427 #ifdef BFD_ASSEMBLER
1428 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1430 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 2
1438 struct frchain *frchainP;
1440 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1444 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1446 /* This now gets called even if we had errors. In that case,
1447 any alignment is meaningless, and, moreover, will look weird
1448 if we are generating a listing. */
1450 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1452 if (subseg_text_p (now_seg))
1453 frag_align_code (alignment, 0);
1455 frag_align (alignment, 0, 0);
1457 /* frag_align will have left a new frag.
1458 Use this last frag for an empty ".fill".
1460 For this segment ...
1461 Create a last frag. Do not leave a "being filled in frag". */
1462 frag_wane (frag_now);
1463 frag_now->fr_fix = 0;
1464 know (frag_now->fr_next == NULL);
1468 /* Write the object file. */
1471 write_object_file ()
1473 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1474 fragS *fragP; /* Track along all frags. */
1477 /* Do we really want to write it? */
1479 int n_warns, n_errs;
1480 n_warns = had_warnings ();
1481 n_errs = had_errors ();
1482 /* The -Z flag indicates that an object file should be generated,
1483 regardless of warnings and errors. */
1484 if (flag_always_generate_output)
1486 if (n_warns || n_errs)
1487 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1488 n_errs, n_errs == 1 ? "" : "s",
1489 n_warns, n_warns == 1 ? "" : "s");
1494 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1495 n_errs, n_errs == 1 ? "" : "s",
1496 n_warns, n_warns == 1 ? "" : "s");
1501 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1502 a routine to check for the definition of the procedure "_main",
1503 and if so -- fix it up so that it can be program entry point. */
1504 vms_check_for_main ();
1505 #endif /* OBJ_VMS */
1507 /* From now on, we don't care about sub-segments. Build one frag chain
1508 for each segment. Linked thru fr_next. */
1510 #ifdef BFD_ASSEMBLER
1511 /* Remove the sections created by gas for its own purposes. */
1516 seclist = &stdoutput->sections;
1519 if (*seclist == reg_section || *seclist == expr_section)
1521 bfd_section_list_remove (stdoutput, seclist);
1522 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 #ifndef BFD_ASSEMBLER
1597 /* Crawl the symbol chain.
1599 For each symbol whose value depends on a frag, take the address of
1600 that frag and subsume it into the value of the symbol.
1601 After this, there is just one way to lookup a symbol value.
1602 Values are left in their final state for object file emission.
1603 We adjust the values of 'L' local symbols, even if we do
1604 not intend to emit them to the object file, because their values
1605 are needed for fix-ups.
1607 Unless we saw a -L flag, remove all symbols that begin with 'L'
1608 from the symbol chain. (They are still pointed to by the fixes.)
1610 Count the remaining symbols.
1611 Assign a symbol number to each symbol.
1612 Count the number of string-table chars we will emit.
1613 Put this info into the headers as appropriate. */
1614 know (zero_address_frag.fr_address == 0);
1615 string_byte_count = sizeof (string_byte_count);
1617 obj_crawl_symbol_chain (&headers);
1619 if (string_byte_count == sizeof (string_byte_count))
1620 string_byte_count = 0;
1622 H_SET_STRING_SIZE (&headers, string_byte_count);
1624 /* Addresses of frags now reflect addresses we use in the object file.
1625 Symbol values are correct.
1626 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1627 Also converting any machine-dependent frags using md_convert_frag(); */
1628 subseg_change (SEG_TEXT, 0);
1630 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1632 /* At this point we have linked all the frags into a single
1633 chain. However, cvt_frag_to_fill may call md_convert_frag
1634 which may call fix_new. We need to ensure that fix_new adds
1635 the fixup to the right section. */
1636 if (fragP == data_frag_root)
1637 subseg_change (SEG_DATA, 0);
1639 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1641 /* Some assert macros don't work with # directives mixed in. */
1643 if (!(fragP->fr_next == NULL
1645 || fragP->fr_next == data_frag_root
1647 || ((fragP->fr_next->fr_address - fragP->fr_address)
1648 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1652 #endif /* ! BFD_ASSEMBLER */
1654 #ifndef WORKING_DOT_WORD
1656 struct broken_word *lie;
1657 struct broken_word **prevP;
1659 prevP = &broken_words;
1660 for (lie = broken_words; lie; lie = lie->next_broken_word)
1665 subseg_change (lie->seg, lie->subseg);
1666 exp.X_op = O_subtract;
1667 exp.X_add_symbol = lie->add;
1668 exp.X_op_symbol = lie->sub;
1669 exp.X_add_number = lie->addnum;
1670 #ifdef BFD_ASSEMBLER
1671 #ifdef TC_CONS_FIX_NEW
1672 TC_CONS_FIX_NEW (lie->frag,
1673 lie->word_goes_here - lie->frag->fr_literal,
1676 fix_new_exp (lie->frag,
1677 lie->word_goes_here - lie->frag->fr_literal,
1678 2, &exp, 0, BFD_RELOC_16);
1681 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1682 fix_new_exp (lie->frag,
1683 lie->word_goes_here - lie->frag->fr_literal,
1684 2, &exp, 0, NO_RELOC);
1687 fix_new_ns32k_exp (lie->frag,
1688 lie->word_goes_here - lie->frag->fr_literal,
1689 2, &exp, 0, 0, 2, 0, 0);
1691 fix_new_exp (lie->frag,
1692 lie->word_goes_here - lie->frag->fr_literal,
1694 #endif /* TC_NS32K */
1695 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1696 #endif /* BFD_ASSEMBLER */
1697 *prevP = lie->next_broken_word;
1700 prevP = &(lie->next_broken_word);
1702 for (lie = broken_words; lie;)
1704 struct broken_word *untruth;
1706 addressT table_addr;
1707 addressT from_addr, to_addr;
1710 subseg_change (lie->seg, lie->subseg);
1711 fragP = lie->dispfrag;
1713 /* Find out how many broken_words go here. */
1716 untruth && untruth->dispfrag == fragP;
1717 untruth = untruth->next_broken_word)
1718 if (untruth->added == 1)
1721 table_ptr = lie->dispfrag->fr_opcode;
1722 table_addr = (lie->dispfrag->fr_address
1723 + (table_ptr - lie->dispfrag->fr_literal));
1724 /* Create the jump around the long jumps. This is a short
1725 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1726 from_addr = table_addr;
1727 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1728 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1730 table_ptr += md_short_jump_size;
1731 table_addr += md_short_jump_size;
1734 lie && lie->dispfrag == fragP;
1735 m++, lie = lie->next_broken_word)
1737 if (lie->added == 2)
1739 /* Patch the jump table. */
1740 /* This is the offset from ??? to table_ptr+0. */
1741 to_addr = table_addr - S_GET_VALUE (lie->sub);
1742 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1743 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1745 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1746 for (untruth = lie->next_broken_word;
1747 untruth && untruth->dispfrag == fragP;
1748 untruth = untruth->next_broken_word)
1750 if (untruth->use_jump == lie)
1751 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1754 /* Install the long jump. */
1755 /* This is a long jump from table_ptr+0 to the final target. */
1756 from_addr = table_addr;
1757 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1758 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1760 table_ptr += md_long_jump_size;
1761 table_addr += md_long_jump_size;
1765 #endif /* not WORKING_DOT_WORD */
1767 #ifndef BFD_ASSEMBLER
1770 char *the_object_file;
1771 long object_file_size;
1772 /* Scan every FixS performing fixups. We had to wait until now to
1773 do this because md_convert_frag() may have made some fixSs. */
1776 subseg_change (SEG_TEXT, 0);
1777 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1778 subseg_change (SEG_DATA, 0);
1779 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1780 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1782 /* FIXME: Move this stuff into the pre-write-hook. */
1783 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1784 H_SET_ENTRY_POINT (&headers, 0);
1786 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1788 object_file_size = H_GET_FILE_SIZE (&headers);
1789 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1791 output_file_create (out_file_name);
1793 obj_header_append (&next_object_file_charP, &headers);
1795 know ((next_object_file_charP - the_object_file)
1796 == H_GET_HEADER_SIZE (&headers));
1799 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1801 register long count;
1802 register char *fill_literal;
1803 register long fill_size;
1806 know (fragP->fr_type == rs_fill);
1807 append (&next_object_file_charP, fragP->fr_literal,
1808 (unsigned long) fragP->fr_fix);
1809 fill_literal = fragP->fr_literal + fragP->fr_fix;
1810 fill_size = fragP->fr_var;
1811 know (fragP->fr_offset >= 0);
1813 for (count = fragP->fr_offset; count; count--)
1814 append (&next_object_file_charP, fill_literal,
1815 (unsigned long) fill_size);
1818 know ((next_object_file_charP - the_object_file)
1819 == (H_GET_HEADER_SIZE (&headers)
1820 + H_GET_TEXT_SIZE (&headers)
1821 + H_GET_DATA_SIZE (&headers)));
1823 /* Emit relocations. */
1824 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1825 (relax_addressT) 0);
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)
1830 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1832 /* Make addresses in data relocation directives relative to beginning of
1833 first data fragment, not end of last text fragment: alignment of the
1834 start of the data segment may place a gap between the segments. */
1835 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1836 data0_frchainP->frch_root->fr_address);
1838 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1839 text_last_frag->fr_address);
1840 #endif /* TC_I960 */
1842 know ((next_object_file_charP - the_object_file)
1843 == (H_GET_HEADER_SIZE (&headers)
1844 + H_GET_TEXT_SIZE (&headers)
1845 + H_GET_DATA_SIZE (&headers)
1846 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1847 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1849 /* Emit line number entries. */
1850 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1851 know ((next_object_file_charP - the_object_file)
1852 == (H_GET_HEADER_SIZE (&headers)
1853 + H_GET_TEXT_SIZE (&headers)
1854 + H_GET_DATA_SIZE (&headers)
1855 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1856 + H_GET_DATA_RELOCATION_SIZE (&headers)
1857 + H_GET_LINENO_SIZE (&headers)));
1860 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1861 know ((next_object_file_charP - the_object_file)
1862 == (H_GET_HEADER_SIZE (&headers)
1863 + H_GET_TEXT_SIZE (&headers)
1864 + H_GET_DATA_SIZE (&headers)
1865 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1866 + H_GET_DATA_RELOCATION_SIZE (&headers)
1867 + H_GET_LINENO_SIZE (&headers)
1868 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1871 if (string_byte_count > 0)
1872 obj_emit_strings (&next_object_file_charP);
1875 bfd_seek (stdoutput, (file_ptr) 0, 0);
1876 bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
1879 /* Write the data to the file. */
1880 output_file_append (the_object_file, object_file_size, out_file_name);
1881 free (the_object_file);
1885 /* Now do the VMS-dependent part of writing the object file. */
1886 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1887 H_GET_DATA_SIZE (&headers),
1888 H_GET_BSS_SIZE (&headers),
1889 text_frag_root, data_frag_root);
1890 #endif /* OBJ_VMS */
1891 #else /* BFD_ASSEMBLER */
1893 /* Resolve symbol values. This needs to be done before processing
1899 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1900 resolve_symbol_value (symp);
1902 resolve_local_symbol_values ();
1906 #ifdef tc_frob_file_before_adjust
1907 tc_frob_file_before_adjust ();
1909 #ifdef obj_frob_file_before_adjust
1910 obj_frob_file_before_adjust ();
1913 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1915 /* Set up symbol table, and write it out. */
1920 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1925 if (symbol_mri_common_p (symp))
1927 if (S_IS_EXTERNAL (symp))
1928 as_bad (_("%s: global symbols not supported in common sections"),
1930 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1934 name = S_GET_NAME (symp);
1938 decode_local_label_name ((char *) S_GET_NAME (symp));
1939 /* They only differ if `name' is a fb or dollar local
1941 if (name2 != name && ! S_IS_DEFINED (symp))
1942 as_bad (_("local label `%s' is not defined"), name2);
1945 /* Do it again, because adjust_reloc_syms might introduce
1946 more symbols. They'll probably only be section symbols,
1947 but they'll still need to have the values computed. */
1948 resolve_symbol_value (symp);
1950 /* Skip symbols which were equated to undefined or common
1952 if (symbol_equated_reloc_p (symp))
1954 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1958 /* So far, common symbols have been treated like undefined symbols.
1959 Put them in the common section now. */
1960 if (S_IS_DEFINED (symp) == 0
1961 && S_GET_VALUE (symp) != 0)
1962 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1964 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1965 S_GET_NAME (symp), symp,
1967 symbol_get_bfdsym (symp)->flags,
1968 segment_name (S_GET_SEGMENT (symp)));
1971 #ifdef obj_frob_symbol
1972 obj_frob_symbol (symp, punt);
1974 #ifdef tc_frob_symbol
1975 if (! punt || symbol_used_in_reloc_p (symp))
1976 tc_frob_symbol (symp, punt);
1979 /* If we don't want to keep this symbol, splice it out of
1980 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1981 want section symbols. Otherwise, we skip local symbols
1982 and symbols that the frob_symbol macros told us to punt,
1983 but we keep such symbols if they are used in relocs. */
1984 if ((! EMIT_SECTION_SYMBOLS
1985 && symbol_section_p (symp))
1986 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1987 opposites. Sometimes the former checks flags and the
1988 latter examines the name... */
1989 || (!S_IS_EXTERN (symp)
1990 && (S_IS_LOCAL (symp) || punt)
1991 && ! symbol_used_in_reloc_p (symp)))
1993 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1995 /* After symbol_remove, symbol_next(symp) still returns
1996 the one that came after it in the chain. So we don't
1997 need to do any extra cleanup work here. */
2001 /* Make sure we really got a value for the symbol. */
2002 if (! symbol_resolved_p (symp))
2004 as_bad (_("can't resolve value for symbol `%s'"),
2006 symbol_mark_resolved (symp);
2009 /* Set the value into the BFD symbol. Up til now the value
2010 has only been kept in the gas symbolS struct. */
2011 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2017 /* Now do any format-specific adjustments to the symbol table, such
2018 as adding file symbols. */
2019 #ifdef tc_adjust_symtab
2020 tc_adjust_symtab ();
2022 #ifdef obj_adjust_symtab
2023 obj_adjust_symtab ();
2026 /* Now that all the sizes are known, and contents correct, we can
2027 start writing to the file. */
2030 /* If *_frob_file changes the symbol value at this point, it is
2031 responsible for moving the changed value into symp->bsym->value
2032 as well. Hopefully all symbol value changing can be done in
2037 #ifdef obj_frob_file
2041 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2043 #ifdef tc_frob_file_after_relocs
2044 tc_frob_file_after_relocs ();
2046 #ifdef obj_frob_file_after_relocs
2047 obj_frob_file_after_relocs ();
2050 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2051 #endif /* BFD_ASSEMBLER */
2055 #ifdef TC_GENERIC_RELAX_TABLE
2057 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2060 relax_frag (segment, fragP, stretch)
2065 const relax_typeS *this_type;
2066 const relax_typeS *start_type;
2067 relax_substateT next_state;
2068 relax_substateT this_state;
2074 const relax_typeS *table;
2076 target = fragP->fr_offset;
2077 address = fragP->fr_address;
2078 table = TC_GENERIC_RELAX_TABLE;
2079 this_state = fragP->fr_subtype;
2080 start_type = this_type = table + this_state;
2081 symbolP = fragP->fr_symbol;
2087 sym_frag = symbol_get_frag (symbolP);
2089 #ifndef DIFF_EXPR_OK
2090 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2091 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2092 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2093 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2094 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2096 know (sym_frag != NULL);
2098 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2099 || sym_frag == &zero_address_frag);
2100 target += S_GET_VALUE (symbolP);
2102 /* If frag has yet to be reached on this pass,
2103 assume it will move by STRETCH just as we did.
2104 If this is not so, it will be because some frag
2105 between grows, and that will force another pass. */
2108 && sym_frag->relax_marker != fragP->relax_marker
2109 && S_GET_SEGMENT (symbolP) == segment)
2115 aim = target - address - fragP->fr_fix;
2116 #ifdef TC_PCREL_ADJUST
2117 /* Currently only the ns32k family needs this. */
2118 aim += TC_PCREL_ADJUST (fragP);
2120 /* This machine doesn't want to use pcrel_adjust.
2121 In that case, pcrel_adjust should be zero. */
2123 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2126 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2127 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2132 /* Look backwards. */
2133 for (next_state = this_type->rlx_more; next_state;)
2134 if (aim >= this_type->rlx_backward)
2138 /* Grow to next state. */
2139 this_state = next_state;
2140 this_type = table + this_state;
2141 next_state = this_type->rlx_more;
2146 /* Look forwards. */
2147 for (next_state = this_type->rlx_more; next_state;)
2148 if (aim <= this_type->rlx_forward)
2152 /* Grow to next state. */
2153 this_state = next_state;
2154 this_type = table + this_state;
2155 next_state = this_type->rlx_more;
2159 growth = this_type->rlx_length - start_type->rlx_length;
2161 fragP->fr_subtype = this_state;
2165 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2167 /* Relax_align. Advance location counter to next address that has 'alignment'
2168 lowest order bits all 0s, return size of adjustment made. */
2169 static relax_addressT
2170 relax_align (address, alignment)
2171 register relax_addressT address; /* Address now. */
2172 register int alignment; /* Alignment (binary). */
2174 relax_addressT mask;
2175 relax_addressT new_address;
2177 mask = ~((~0) << alignment);
2178 new_address = (address + mask) & (~mask);
2179 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2181 /* We must provide lots of padding, so the linker can discard it
2182 when needed. The linker will not add extra space, ever. */
2183 new_address += (1 << alignment);
2185 return (new_address - address);
2188 /* Now we have a segment, not a crowd of sub-segments, we can make
2193 After this, all frags in this segment have addresses that are correct
2194 within the segment. Since segments live in different file addresses,
2195 these frag addresses may not be the same as final object-file
2199 relax_segment (segment_frag_root, segment)
2200 struct frag *segment_frag_root;
2203 register struct frag *fragP;
2204 register relax_addressT address;
2207 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2208 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2210 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2211 subseg_change (segment, 0);
2213 /* For each frag in segment: count and store (a 1st guess of)
2216 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2218 fragP->relax_marker = 0;
2219 fragP->fr_address = address;
2220 address += fragP->fr_fix;
2222 switch (fragP->fr_type)
2225 address += fragP->fr_offset * fragP->fr_var;
2232 addressT offset = relax_align (address, (int) fragP->fr_offset);
2234 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2237 if (offset % fragP->fr_var != 0)
2239 as_bad_where (fragP->fr_file, fragP->fr_line,
2240 _("alignment padding (%lu bytes) not a multiple of %ld"),
2241 (unsigned long) offset, (long) fragP->fr_var);
2242 offset -= (offset % fragP->fr_var);
2251 /* Assume .org is nugatory. It will grow with 1st relax. */
2254 case rs_machine_dependent:
2255 /* If fr_symbol is an expression, this call to
2256 resolve_symbol_value sets up the correct segment, which will
2257 likely be needed in md_estimate_size_before_relax. */
2258 if (fragP->fr_symbol)
2259 resolve_symbol_value (fragP->fr_symbol);
2261 address += md_estimate_size_before_relax (fragP, segment);
2264 #ifndef WORKING_DOT_WORD
2265 /* Broken words don't concern us yet. */
2266 case rs_broken_word:
2271 /* Initial guess is always 1; doing otherwise can result in
2272 stable solutions that are larger than the minimum. */
2273 address += fragP->fr_offset = 1;
2277 address += eh_frame_estimate_size_before_relax (fragP);
2281 address += dwarf2dbg_estimate_size_before_relax (fragP);
2285 BAD_CASE (fragP->fr_type);
2292 long stretch; /* May be any size, 0 or negative. */
2293 /* Cumulative number of addresses we have relaxed this pass.
2294 We may have relaxed more than one address. */
2295 int stretched; /* Have we stretched on this pass? */
2296 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2297 grew, and another shrank. If a branch instruction doesn't fit anymore,
2298 we could be scrod. */
2305 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2308 addressT was_address;
2312 fragP->relax_marker ^= 1;
2313 was_address = fragP->fr_address;
2314 address = fragP->fr_address += stretch;
2315 symbolP = fragP->fr_symbol;
2316 offset = fragP->fr_offset;
2318 switch (fragP->fr_type)
2320 case rs_fill: /* .fill never relaxes. */
2324 #ifndef WORKING_DOT_WORD
2325 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2326 for it I do not want to write it. I do not want to have
2327 anything to do with it. This is not the proper way to
2328 implement this misfeature. */
2329 case rs_broken_word:
2331 struct broken_word *lie;
2332 struct broken_word *untruth;
2334 /* Yes this is ugly (storing the broken_word pointer
2335 in the symbol slot). Still, this whole chunk of
2336 code is ugly, and I don't feel like doing anything
2337 about it. Think of it as stubbornness in action. */
2339 for (lie = (struct broken_word *) (fragP->fr_symbol);
2340 lie && lie->dispfrag == fragP;
2341 lie = lie->next_broken_word)
2347 offset = (S_GET_VALUE (lie->add)
2349 - S_GET_VALUE (lie->sub));
2350 if (offset <= -32768 || offset >= 32767)
2352 if (flag_warn_displacement)
2355 sprint_value (buf, (addressT) lie->addnum);
2356 as_warn_where (fragP->fr_file, fragP->fr_line,
2357 _(".word %s-%s+%s didn't fit"),
2358 S_GET_NAME (lie->add),
2359 S_GET_NAME (lie->sub),
2363 if (fragP->fr_subtype == 0)
2365 fragP->fr_subtype++;
2366 growth += md_short_jump_size;
2368 for (untruth = lie->next_broken_word;
2369 untruth && untruth->dispfrag == lie->dispfrag;
2370 untruth = untruth->next_broken_word)
2371 if ((symbol_get_frag (untruth->add)
2372 == symbol_get_frag (lie->add))
2373 && (S_GET_VALUE (untruth->add)
2374 == S_GET_VALUE (lie->add)))
2377 untruth->use_jump = lie;
2379 growth += md_long_jump_size;
2384 } /* case rs_broken_word */
2390 addressT oldoff, newoff;
2392 oldoff = relax_align (was_address + fragP->fr_fix,
2394 newoff = relax_align (address + fragP->fr_fix,
2397 if (fragP->fr_subtype != 0)
2399 if (oldoff > fragP->fr_subtype)
2401 if (newoff > fragP->fr_subtype)
2405 growth = newoff - oldoff;
2411 addressT target = offset;
2416 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2417 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2418 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2419 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2420 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2421 know (symbolP->sy_frag);
2422 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2423 || (symbolP->sy_frag == &zero_address_frag));
2425 /* Convert from an actual address to an octet offset
2426 into the section. Here it is assumed that the
2427 section's VMA is zero, and can omit subtracting it
2428 from the symbol's value to get the address offset. */
2429 know (S_GET_SECTION (symbolP)->vma == 0);
2430 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2433 know (fragP->fr_next);
2434 after = fragP->fr_next->fr_address;
2435 growth = target - after;
2438 /* Growth may be negative, but variable part of frag
2439 cannot have fewer than 0 chars. That is, we can't
2441 as_bad_where (fragP->fr_file, fragP->fr_line,
2442 _("attempt to .org backwards"));
2444 /* We've issued an error message. Change the
2445 frag to avoid cascading errors. */
2446 fragP->fr_type = rs_align;
2447 fragP->fr_subtype = 0;
2448 fragP->fr_offset = 0;
2449 fragP->fr_fix = after - address;
2453 /* This is an absolute growth factor */
2464 amount = S_GET_VALUE (symbolP);
2465 if (S_GET_SEGMENT (symbolP) != absolute_section
2466 || S_IS_COMMON (symbolP)
2467 || ! S_IS_DEFINED (symbolP))
2469 as_bad_where (fragP->fr_file, fragP->fr_line,
2470 _(".space specifies non-absolute value"));
2471 /* Prevent repeat of this error message. */
2472 fragP->fr_symbol = 0;
2474 else if (amount < 0)
2476 as_warn_where (fragP->fr_file, fragP->fr_line,
2477 _(".space or .fill with negative value, ignored"));
2478 fragP->fr_symbol = 0;
2481 growth = (was_address + fragP->fr_fix + amount
2482 - fragP->fr_next->fr_address);
2486 case rs_machine_dependent:
2487 #ifdef md_relax_frag
2488 growth = md_relax_frag (segment, fragP, stretch);
2490 #ifdef TC_GENERIC_RELAX_TABLE
2491 /* The default way to relax a frag is to look through
2492 TC_GENERIC_RELAX_TABLE. */
2493 growth = relax_frag (segment, fragP, stretch);
2494 #endif /* TC_GENERIC_RELAX_TABLE */
2503 value = resolve_symbol_value (fragP->fr_symbol);
2504 size = sizeof_leb128 (value, fragP->fr_subtype);
2505 growth = size - fragP->fr_offset;
2506 fragP->fr_offset = size;
2511 growth = eh_frame_relax_frag (fragP);
2515 growth = dwarf2dbg_relax_frag (fragP);
2519 BAD_CASE (fragP->fr_type);
2527 } /* For each frag in the segment. */
2529 while (stretched); /* Until nothing further to relax. */
2533 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2534 if (fragP->last_fr_address != fragP->fr_address)
2536 fragP->last_fr_address = fragP->fr_address;
2542 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2544 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2545 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2550 Go through all the fixS's in a segment and see which ones can be
2551 handled now. (These consist of fixS where we have since discovered
2552 the value of a symbol, or the address of the frag involved.)
2553 For each one, call md_apply_fix3 to put the fix into the frag data.
2555 Result is a count of how many relocation structs will be needed to
2556 handle the remaining fixS's that we couldn't completely handle here.
2557 These will be output later by emit_relocations(). */
2560 fixup_segment (fixP, this_segment_type)
2561 register fixS *fixP;
2562 segT this_segment_type; /* N_TYPE bits for segment. */
2564 long seg_reloc_count = 0;
2565 symbolS *add_symbolP;
2566 symbolS *sub_symbolP;
2573 segT add_symbol_segment = absolute_section;
2575 /* If the linker is doing the relaxing, we must not do any fixups.
2577 Well, strictly speaking that's not true -- we could do any that are
2578 PC-relative and don't cross regions that could change size. And for the
2579 i960 (the only machine for which we've got a relaxing linker right now),
2580 we might be able to turn callx/callj into bal anyways in cases where we
2581 know the maximum displacement. */
2582 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment_type))
2584 for (; fixP; fixP = fixP->fx_next)
2586 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2587 return seg_reloc_count;
2590 for (; fixP; fixP = fixP->fx_next)
2593 fprintf (stderr, "\nprocessing fixup:\n");
2597 fragP = fixP->fx_frag;
2599 where = fixP->fx_where;
2600 place = fragP->fr_literal + where;
2601 size = fixP->fx_size;
2602 add_symbolP = fixP->fx_addsy;
2603 #ifdef TC_VALIDATE_FIX
2604 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2606 sub_symbolP = fixP->fx_subsy;
2607 add_number = fixP->fx_offset;
2608 pcrel = fixP->fx_pcrel;
2611 if (add_symbolP != NULL
2612 && symbol_mri_common_p (add_symbolP))
2614 know (add_symbolP->sy_value.X_op == O_symbol);
2615 add_number += S_GET_VALUE (add_symbolP);
2616 fixP->fx_offset = add_number;
2617 add_symbolP = fixP->fx_addsy =
2618 symbol_get_value_expression (add_symbolP)->X_add_symbol;
2622 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2626 resolve_symbol_value (sub_symbolP);
2627 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2629 if (add_symbolP != NULL)
2631 add_number += S_GET_VALUE (add_symbolP);
2633 fixP->fx_addsy = NULL;
2636 /* It's just -sym. */
2637 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2639 add_number -= S_GET_VALUE (sub_symbolP);
2640 fixP->fx_subsy = NULL;
2643 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2645 /* Should try converting to a constant. */
2650 as_bad_where (fixP->fx_file, fixP->fx_line,
2651 _("negative of non-absolute symbol `%s'"),
2652 S_GET_NAME (sub_symbolP));
2654 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2655 && SEG_NORMAL (add_symbol_segment))
2657 /* Difference of 2 symbols from same segment.
2658 Can't make difference of 2 undefineds: 'value' means
2659 something different for N_UNDF. */
2661 /* Makes no sense to use the difference of 2 arbitrary symbols
2662 as the target of a call instruction. */
2664 as_bad_where (fixP->fx_file, fixP->fx_line,
2665 _("callj to difference of two symbols"));
2666 #endif /* TC_I960 */
2667 add_number += (S_GET_VALUE (add_symbolP)
2668 - S_GET_VALUE (sub_symbolP));
2671 /* See the comment below about 68k weirdness. */
2675 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2678 pcrel = 0; /* No further pcrel processing. */
2680 /* Let the target machine make the final determination
2681 as to whether or not a relocation will be needed to
2682 handle this fixup. */
2683 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2686 fixP->fx_addsy = NULL;
2687 fixP->fx_subsy = NULL;
2692 /* Different segments in subtraction. */
2693 know (!(S_IS_EXTERNAL (sub_symbolP)
2694 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2696 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2697 add_number -= S_GET_VALUE (sub_symbolP);
2700 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2702 /* Make it pc-relative. */
2705 /* Do this for m68k even if it's already described
2706 as pc-relative. On the m68k, an operand of
2707 "pc@(foo-.-2)" should address "foo" in a
2708 pc-relative mode. */
2713 add_number += MD_PCREL_FROM_SECTION (fixP,
2719 add_number -= S_GET_VALUE (sub_symbolP);
2724 #ifdef UNDEFINED_DIFFERENCE_OK
2725 /* The PA needs this for PIC code generation. We basically
2726 don't want to do anything if we have the difference of two
2727 symbols at this point. */
2730 /* Leave it alone. */
2733 #ifdef BFD_ASSEMBLER
2734 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2735 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2737 /* Leave it alone. */
2743 sprint_value (buf, fragP->fr_address + where);
2744 as_bad_where (fixP->fx_file, fixP->fx_line,
2745 _("subtraction of two symbols in different sections `%s' {%s section} - `%s' {%s section} at file address %s"),
2746 S_GET_NAME (add_symbolP),
2747 segment_name (S_GET_SEGMENT (add_symbolP)),
2748 S_GET_NAME (sub_symbolP),
2749 segment_name (S_GET_SEGMENT (sub_symbolP)),
2757 if (add_symbol_segment == this_segment_type && pcrel && !plt
2758 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2760 /* This fixup was made when the symbol's segment was
2761 SEG_UNKNOWN, but it is now in the local segment.
2762 So we know how to do the address without relocation. */
2764 /* reloc_callj() may replace a 'call' with a 'calls' or a
2765 'bal', in which cases it modifies *fixP as appropriate.
2766 In the case of a 'calls', no further work is required,
2767 and *fixP has been set up to make the rest of the code
2770 #endif /* TC_I960 */
2772 add_number += S_GET_VALUE (add_symbolP);
2773 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2774 /* Lie. Don't want further pcrel processing. */
2777 /* Let the target machine make the final determination
2778 as to whether or not a relocation will be needed to
2779 handle this fixup. */
2780 if (!TC_FORCE_RELOCATION (fixP))
2783 fixP->fx_addsy = NULL;
2788 if (add_symbol_segment == absolute_section
2792 /* See comment about reloc_callj() above. */
2794 #endif /* TC_I960 */
2795 add_number += S_GET_VALUE (add_symbolP);
2797 /* Let the target machine make the final determination
2798 as to whether or not a relocation will be needed to
2799 handle this fixup. */
2801 if (!TC_FORCE_RELOCATION (fixP))
2803 fixP->fx_addsy = NULL;
2807 else if (add_symbol_segment == undefined_section
2808 #ifdef BFD_ASSEMBLER
2809 || bfd_is_com_section (add_symbol_segment)
2814 if ((int) fixP->fx_bit_fixP == 13)
2816 /* This is a COBR instruction. They have only a
2817 13-bit displacement and are only to be used
2818 for local branches: flag as error, don't generate
2820 as_bad_where (fixP->fx_file, fixP->fx_line,
2821 _("can't use COBR format with external label"));
2822 fixP->fx_addsy = NULL;
2826 #endif /* TC_I960 */
2830 if (S_IS_COMMON (add_symbolP))
2831 add_number += S_GET_VALUE (add_symbolP);
2832 #endif /* TE_I386AIX */
2833 #endif /* OBJ_COFF */
2839 if (TC_FIX_ADJUSTABLE (fixP))
2840 add_number += S_GET_VALUE (add_symbolP);
2847 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2848 if (add_symbolP == 0)
2850 #ifndef BFD_ASSEMBLER
2851 fixP->fx_addsy = &abs_symbol;
2853 fixP->fx_addsy = section_symbol (absolute_section);
2855 symbol_mark_used_in_reloc (fixP->fx_addsy);
2861 md_apply_fix3 (fixP, & add_number, this_segment_type);
2863 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2865 if ((size_t) size < sizeof (valueT))
2870 mask--; /* Set all bits to one. */
2871 mask <<= size * 8 - (fixP->fx_signed ? 1 : 0);
2872 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2874 char buf[50], buf2[50];
2875 sprint_value (buf, fragP->fr_address + where);
2876 if (add_number > 1000)
2877 sprint_value (buf2, add_number);
2879 sprintf (buf2, "%ld", (long) add_number);
2880 as_bad_where (fixP->fx_file, fixP->fx_line,
2881 _("value of %s too large for field of %d bytes at %s"),
2883 } /* Generic error checking. */
2885 #ifdef WARN_SIGNED_OVERFLOW_WORD
2886 /* Warn if a .word value is too large when treated as a signed
2887 number. We already know it is not too negative. This is to
2888 catch over-large switches generated by gcc on the 68k. */
2889 if (!flag_signed_overflow_ok
2891 && add_number > 0x7fff)
2892 as_bad_where (fixP->fx_file, fixP->fx_line,
2893 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2895 (unsigned long) (fragP->fr_address + where));
2897 } /* Not a bit fix. */
2899 #ifdef TC_VALIDATE_FIX
2900 skip: ATTRIBUTE_UNUSED_LABEL
2904 fprintf (stderr, "result:\n");
2907 } /* For each fixS in this segment. */
2909 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2910 return seg_reloc_count;
2913 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2916 number_to_chars_bigendian (buf, val, n)
2925 buf[n] = val & 0xff;
2931 number_to_chars_littleendian (buf, val, n)
2940 *buf++ = val & 0xff;
2946 write_print_statistics (file)
2949 fprintf (file, "fixups: %d\n", n_fixups);
2952 /* For debugging. */
2953 extern int indent_level;
2960 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2962 fprintf (stderr, " pcrel");
2963 if (fixp->fx_pcrel_adjust)
2964 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2965 if (fixp->fx_im_disp)
2968 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2970 fprintf (stderr, " im_disp");
2974 fprintf (stderr, " tcbit");
2976 fprintf (stderr, " done");
2977 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2978 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2979 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2980 #ifdef BFD_ASSEMBLER
2981 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2984 #ifdef NEED_FX_R_TYPE
2985 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2990 fprintf (stderr, "\n +<");
2991 print_symbol_value_1 (stderr, fixp->fx_addsy);
2992 fprintf (stderr, ">");
2996 fprintf (stderr, "\n -<");
2997 print_symbol_value_1 (stderr, fixp->fx_subsy);
2998 fprintf (stderr, ">");
3000 fprintf (stderr, "\n");
3001 #ifdef TC_FIX_DATA_PRINT
3002 TC_FIX_DATA_PRINT (stderr, fixp);