1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This thing should be set up to do byteordering correctly. But... */
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
31 #ifndef TC_ADJUST_RELOC_COUNT
32 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
35 #ifndef TC_FORCE_RELOCATION
36 #define TC_FORCE_RELOCATION(FIX) 0
39 #ifndef TC_FORCE_RELOCATION_SECTION
40 #define TC_FORCE_RELOCATION_SECTION(FIX, SEG) TC_FORCE_RELOCATION (FIX)
43 #ifndef TC_LINKRELAX_FIXUP
44 #define TC_LINKRELAX_FIXUP(SEG) 1
47 #ifndef TC_FIX_ADJUSTABLE
48 #define TC_FIX_ADJUSTABLE(FIX) 1
51 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
52 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
55 #ifndef MD_PCREL_FROM_SECTION
56 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
59 #ifndef WORKING_DOT_WORD
60 extern const int md_short_jump_size;
61 extern const int md_long_jump_size;
64 /* Used to control final evaluation of expressions. */
65 int finalize_syms = 0;
67 int symbol_table_frozen;
68 void print_fixup PARAMS ((fixS *));
71 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
73 /* We generally attach relocs to frag chains. However, after we have
74 chained these all together into a segment, any relocs we add after
75 that must be attached to a segment. This will include relocs added
76 in md_estimate_size_for_relax, for example. */
77 static int frags_chained = 0;
83 struct frag *text_frag_root;
84 struct frag *data_frag_root;
85 struct frag *bss_frag_root;
87 struct frag *text_last_frag; /* Last frag in segment. */
88 struct frag *data_last_frag; /* Last frag in segment. */
89 static struct frag *bss_last_frag; /* Last frag in segment. */
93 static object_headers headers;
96 long string_byte_count;
97 char *next_object_file_charP; /* Tracks object file bytes. */
100 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
103 #endif /* BFD_ASSEMBLER */
108 #define RELOC_ENUM enum bfd_reloc_code_real
110 #define RELOC_ENUM int
113 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
114 symbolS *add, symbolS *sub,
115 offsetT offset, int pcrel,
117 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
118 static long fixup_segment PARAMS ((fixS *, segT));
120 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
121 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
122 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
125 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
126 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
127 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
128 static void write_relocs PARAMS ((bfd *, asection *, PTR));
129 static void write_contents PARAMS ((bfd *, asection *, PTR));
130 static void set_symtab PARAMS ((void));
132 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
133 static void merge_data_into_text PARAMS ((void));
135 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
136 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
137 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
138 static void relax_and_size_all_segments PARAMS ((void));
140 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
141 static void set_segment_vma PARAMS ((bfd *, asection *, PTR));
144 /* Create a fixS in obstack 'notes'. */
147 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
149 fragS *frag; /* Which frag? */
150 int where; /* Where in that frag? */
151 int size; /* 1, 2, or 4 usually. */
152 symbolS *add_symbol; /* X_add_symbol. */
153 symbolS *sub_symbol; /* X_op_symbol. */
154 offsetT offset; /* X_add_number. */
155 int pcrel; /* TRUE if PC-relative relocation. */
156 RELOC_ENUM r_type ATTRIBUTE_UNUSED; /* Relocation type. */
162 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
164 fixP->fx_frag = frag;
165 fixP->fx_where = where;
166 fixP->fx_size = size;
167 /* We've made fx_size a narrow field; check that it's wide enough. */
168 if (fixP->fx_size != size)
170 as_bad (_("field fx_size too small to hold %d"), size);
173 fixP->fx_addsy = add_symbol;
174 fixP->fx_subsy = sub_symbol;
175 fixP->fx_offset = offset;
176 fixP->fx_pcrel = pcrel;
178 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
179 fixP->fx_r_type = r_type;
181 fixP->fx_im_disp = 0;
182 fixP->fx_pcrel_adjust = 0;
183 fixP->fx_bit_fixP = 0;
184 fixP->fx_addnumber = 0;
187 fixP->fx_no_overflow = 0;
191 fixP->fx_cgen.insn = NULL;
192 fixP->fx_cgen.opinfo = 0;
196 TC_INIT_FIX_DATA (fixP);
199 as_where (&fixP->fx_file, &fixP->fx_line);
201 /* Usually, we want relocs sorted numerically, but while
202 comparing to older versions of gas that have relocs
203 reverse sorted, it is convenient to have this compile
204 time option. xoxorich. */
208 fixS **seg_fix_rootP = (frags_chained
209 ? &seg_info (now_seg)->fix_root
210 : &frchain_now->fix_root);
211 fixS **seg_fix_tailP = (frags_chained
212 ? &seg_info (now_seg)->fix_tail
213 : &frchain_now->fix_tail);
216 #ifdef REVERSE_SORT_RELOCS
218 fixP->fx_next = *seg_fix_rootP;
219 *seg_fix_rootP = fixP;
221 #else /* REVERSE_SORT_RELOCS */
223 fixP->fx_next = NULL;
226 (*seg_fix_tailP)->fx_next = fixP;
228 *seg_fix_rootP = fixP;
229 *seg_fix_tailP = fixP;
231 #endif /* REVERSE_SORT_RELOCS */
237 /* Create a fixup relative to a symbol (plus a constant). */
240 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
241 fragS *frag; /* Which frag? */
242 int where; /* Where in that frag? */
243 int size; /* 1, 2, or 4 usually. */
244 symbolS *add_symbol; /* X_add_symbol. */
245 offsetT offset; /* X_add_number. */
246 int pcrel; /* TRUE if PC-relative relocation. */
247 RELOC_ENUM r_type; /* Relocation type. */
249 return fix_new_internal (frag, where, size, add_symbol,
250 (symbolS *) NULL, offset, pcrel, r_type);
253 /* Create a fixup for an expression. Currently we only support fixups
254 for difference expressions. That is itself more than most object
255 file formats support anyhow. */
258 fix_new_exp (frag, where, size, exp, pcrel, r_type)
259 fragS *frag; /* Which frag? */
260 int where; /* Where in that frag? */
261 int size; /* 1, 2, or 4 usually. */
262 expressionS *exp; /* Expression. */
263 int pcrel; /* TRUE if PC-relative relocation. */
264 RELOC_ENUM r_type; /* Relocation type. */
276 as_bad (_("register value used as expression"));
280 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
281 the difference expression cannot immediately be reduced. */
283 symbolS *stmp = make_expr_symbol (exp);
285 exp->X_op = O_symbol;
286 exp->X_op_symbol = 0;
287 exp->X_add_symbol = stmp;
288 exp->X_add_number = 0;
290 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
294 add = exp->X_add_symbol;
295 off = exp->X_add_number;
297 #if defined(BFD_ASSEMBLER)
298 r_type = BFD_RELOC_RVA;
300 #if defined(TC_RVA_RELOC)
301 r_type = TC_RVA_RELOC;
303 as_fatal (_("rva not supported"));
309 sub = exp->X_add_symbol;
310 off = exp->X_add_number;
314 sub = exp->X_op_symbol;
317 add = exp->X_add_symbol;
320 off = exp->X_add_number;
324 add = make_expr_symbol (exp);
328 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
331 /* Append a string onto another string, bumping the pointer along. */
333 append (charPP, fromP, length)
336 unsigned long length;
338 /* Don't trust memcpy() of 0 chars. */
342 memcpy (*charPP, fromP, length);
346 #ifndef BFD_ASSEMBLER
347 int section_alignment[SEG_MAXIMUM_ORDINAL];
350 /* This routine records the largest alignment seen for each segment.
351 If the beginning of the segment is aligned on the worst-case
352 boundary, all of the other alignments within it will work. At
353 least one object format really uses this info. */
356 record_alignment (seg, align)
357 /* Segment to which alignment pertains. */
359 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
363 if (seg == absolute_section)
366 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
367 bfd_set_section_alignment (stdoutput, seg, align);
369 if (align > section_alignment[(int) seg])
370 section_alignment[(int) seg] = align;
375 get_recorded_alignment (seg)
378 if (seg == absolute_section)
381 return bfd_get_section_alignment (stdoutput, seg);
383 return section_alignment[(int) seg];
389 /* Reset the section indices after removing the gas created sections. */
392 renumber_sections (abfd, sec, countparg)
393 bfd *abfd ATTRIBUTE_UNUSED;
397 int *countp = (int *) countparg;
399 sec->index = *countp;
403 #endif /* defined (BFD_ASSEMBLER) */
405 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
408 chain_frchains_together_1 (section, frchp)
410 struct frchain *frchp;
412 fragS dummy, *prev_frag = &dummy;
414 fixS fix_dummy, *prev_fix = &fix_dummy;
417 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
419 prev_frag->fr_next = frchp->frch_root;
420 prev_frag = frchp->frch_last;
421 assert (prev_frag->fr_type != 0);
423 if (frchp->fix_root != (fixS *) NULL)
425 if (seg_info (section)->fix_root == (fixS *) NULL)
426 seg_info (section)->fix_root = frchp->fix_root;
427 prev_fix->fx_next = frchp->fix_root;
428 seg_info (section)->fix_tail = frchp->fix_tail;
429 prev_fix = frchp->fix_tail;
433 assert (prev_frag->fr_type != 0);
434 prev_frag->fr_next = 0;
443 chain_frchains_together (abfd, section, xxx)
444 bfd *abfd ATTRIBUTE_UNUSED;
446 PTR xxx ATTRIBUTE_UNUSED;
448 segment_info_type *info;
450 /* BFD may have introduced its own sections without using
451 subseg_new, so it is possible that seg_info is NULL. */
452 info = seg_info (section);
453 if (info != (segment_info_type *) NULL)
454 info->frchainP->frch_last
455 = chain_frchains_together_1 (section, info->frchainP);
457 /* Now that we've chained the frags together, we must add new fixups
458 to the segment, not to the frag chain. */
464 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
467 remove_subsegs (head, seg, root, last)
473 *root = head->frch_root;
474 *last = chain_frchains_together_1 (seg, head);
479 #if defined (BFD_ASSEMBLER) || !defined (BFD)
483 cvt_frag_to_fill (sec, fragP)
484 segT sec ATTRIBUTE_UNUSED;
488 cvt_frag_to_fill (headersP, sec, fragP)
489 object_headers *headersP;
494 switch (fragP->fr_type)
502 HANDLE_ALIGN (fragP);
504 know (fragP->fr_next != NULL);
505 fragP->fr_offset = (fragP->fr_next->fr_address
507 - fragP->fr_fix) / fragP->fr_var;
508 if (fragP->fr_offset < 0)
510 as_bad_where (fragP->fr_file, fragP->fr_line,
511 _("attempt to .org/.space backwards? (%ld)"),
512 (long) fragP->fr_offset);
513 fragP->fr_offset = 0;
515 fragP->fr_type = rs_fill;
523 valueT value = S_GET_VALUE (fragP->fr_symbol);
526 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
529 fragP->fr_fix += size;
530 fragP->fr_type = rs_fill;
532 fragP->fr_offset = 0;
533 fragP->fr_symbol = NULL;
538 eh_frame_convert_frag (fragP);
542 dwarf2dbg_convert_frag (fragP);
545 case rs_machine_dependent:
547 md_convert_frag (stdoutput, sec, fragP);
549 md_convert_frag (headersP, sec, fragP);
552 assert (fragP->fr_next == NULL
553 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
556 /* After md_convert_frag, we make the frag into a ".space 0".
557 md_convert_frag() should set up any fixSs and constants
562 #ifndef WORKING_DOT_WORD
565 struct broken_word *lie;
567 if (fragP->fr_subtype)
569 fragP->fr_fix += md_short_jump_size;
570 for (lie = (struct broken_word *) (fragP->fr_symbol);
571 lie && lie->dispfrag == fragP;
572 lie = lie->next_broken_word)
574 fragP->fr_fix += md_long_jump_size;
582 BAD_CASE (fragP->fr_type);
587 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
590 static void relax_seg PARAMS ((bfd *, asection *, PTR));
592 relax_seg (abfd, sec, xxx)
593 bfd *abfd ATTRIBUTE_UNUSED;
597 segment_info_type *seginfo = seg_info (sec);
599 if (seginfo && seginfo->frchainP
600 && relax_segment (seginfo->frchainP->frch_root, sec))
602 int *result = (int *) xxx;
607 static void size_seg PARAMS ((bfd *, asection *, PTR));
609 size_seg (abfd, sec, xxx)
612 PTR xxx ATTRIBUTE_UNUSED;
616 segment_info_type *seginfo;
618 valueT size, newsize;
620 subseg_change (sec, 0);
622 seginfo = seg_info (sec);
623 if (seginfo && seginfo->frchainP)
625 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
626 cvt_frag_to_fill (sec, fragp);
627 for (fragp = seginfo->frchainP->frch_root;
629 fragp = fragp->fr_next)
630 /* Walk to last elt. */
632 size = fragp->fr_address + fragp->fr_fix;
637 flags = bfd_get_section_flags (abfd, sec);
639 if (size > 0 && ! seginfo->bss)
640 flags |= SEC_HAS_CONTENTS;
642 /* @@ This is just an approximation. */
643 if (seginfo && seginfo->fix_root)
647 x = bfd_set_section_flags (abfd, sec, flags);
650 newsize = md_section_align (sec, size);
651 x = bfd_set_section_size (abfd, sec, newsize);
654 /* If the size had to be rounded up, add some padding in the last
656 assert (newsize >= size);
659 fragS *last = seginfo->frchainP->frch_last;
660 fragp = seginfo->frchainP->frch_root;
661 while (fragp->fr_next != last)
662 fragp = fragp->fr_next;
663 last->fr_address = size;
664 if ((newsize - size) % fragp->fr_var == 0)
665 fragp->fr_offset += (newsize - size) / fragp->fr_var;
667 /* If we hit this abort, it's likely due to subsegs_finish not
668 providing sufficient alignment on the last frag, and the
669 machine dependent code using alignment frags with fr_var
674 #ifdef tc_frob_section
675 tc_frob_section (sec);
677 #ifdef obj_frob_section
678 obj_frob_section (sec);
684 dump_section_relocs (abfd, sec, stream_)
685 bfd *abfd ATTRIBUTE_UNUSED;
689 FILE *stream = (FILE *) stream_;
690 segment_info_type *seginfo = seg_info (sec);
691 fixS *fixp = seginfo->fix_root;
696 fprintf (stream, "sec %s relocs:\n", sec->name);
699 symbolS *s = fixp->fx_addsy;
701 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
702 (int) fixp->fx_r_type);
704 fprintf (stream, "no sym\n");
707 print_symbol_value_1 (stream, s);
708 fprintf (stream, "\n");
710 fixp = fixp->fx_next;
714 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
717 #ifndef EMIT_SECTION_SYMBOLS
718 #define EMIT_SECTION_SYMBOLS 1
721 /* This pass over fixups decides whether symbols can be replaced with
725 adjust_reloc_syms (abfd, sec, xxx)
726 bfd *abfd ATTRIBUTE_UNUSED;
728 PTR xxx ATTRIBUTE_UNUSED;
730 segment_info_type *seginfo = seg_info (sec);
736 dump_section_relocs (abfd, sec, stderr);
738 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
742 else if (fixp->fx_addsy)
748 fprintf (stderr, "\n\nadjusting fixup:\n");
752 sym = fixp->fx_addsy;
754 /* All symbols should have already been resolved at this
755 point. It is possible to see unresolved expression
756 symbols, though, since they are not in the regular symbol
758 resolve_symbol_value (sym);
760 if (fixp->fx_subsy != NULL)
761 resolve_symbol_value (fixp->fx_subsy);
763 /* If this symbol is equated to an undefined symbol, convert
764 the fixup to being against that symbol. */
765 if (symbol_equated_reloc_p (sym))
767 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
768 sym = symbol_get_value_expression (sym)->X_add_symbol;
769 fixp->fx_addsy = sym;
772 if (symbol_mri_common_p (sym))
774 /* These symbols are handled specially in fixup_segment. */
778 symsec = S_GET_SEGMENT (sym);
782 if (bfd_is_abs_section (symsec))
784 /* The fixup_segment routine will not use this symbol in a
785 relocation unless TC_FORCE_RELOCATION returns 1. */
786 if (TC_FORCE_RELOCATION (fixp))
788 symbol_mark_used_in_reloc (fixp->fx_addsy);
789 #ifdef UNDEFINED_DIFFERENCE_OK
790 if (fixp->fx_subsy != NULL)
791 symbol_mark_used_in_reloc (fixp->fx_subsy);
797 /* If it's one of these sections, assume the symbol is
798 definitely going to be output. The code in
799 md_estimate_size_before_relax in tc-mips.c uses this test
800 as well, so if you change this code you should look at that
802 if (bfd_is_und_section (symsec)
803 || bfd_is_com_section (symsec))
805 symbol_mark_used_in_reloc (fixp->fx_addsy);
806 #ifdef UNDEFINED_DIFFERENCE_OK
807 /* We have the difference of an undefined symbol and some
808 other symbol. Make sure to mark the other symbol as used
809 in a relocation so that it will always be output. */
811 symbol_mark_used_in_reloc (fixp->fx_subsy);
816 /* Don't try to reduce relocs which refer to non-local symbols
817 in .linkonce sections. It can lead to confusion when a
818 debugging section refers to a .linkonce section. I hope
819 this will always be correct. */
820 if (symsec != sec && ! S_IS_LOCAL (sym))
826 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
831 /* The GNU toolchain uses an extension for ELF: a section
832 beginning with the magic string .gnu.linkonce is a
834 if (strncmp (segment_name (symsec), ".gnu.linkonce",
835 sizeof ".gnu.linkonce" - 1) == 0)
841 symbol_mark_used_in_reloc (fixp->fx_addsy);
842 #ifdef UNDEFINED_DIFFERENCE_OK
843 if (fixp->fx_subsy != NULL)
844 symbol_mark_used_in_reloc (fixp->fx_subsy);
850 /* Since we're reducing to section symbols, don't attempt to reduce
851 anything that's already using one. */
852 if (symbol_section_p (sym))
854 symbol_mark_used_in_reloc (fixp->fx_addsy);
859 /* We can never adjust a reloc against a weak symbol. If we
860 did, and the weak symbol was overridden by a real symbol
861 somewhere else, then our relocation would be pointing at
862 the wrong area of memory. */
865 symbol_mark_used_in_reloc (fixp->fx_addsy);
869 /* Never adjust a reloc against local symbol in a merge section
870 with non-zero addend. */
871 if ((symsec->flags & SEC_MERGE) != 0 && fixp->fx_offset != 0)
873 symbol_mark_used_in_reloc (fixp->fx_addsy);
877 /* Never adjust a reloc against TLS local symbol. */
878 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
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");
923 /* There was no symbol required by this relocation. However,
924 BFD doesn't really handle relocations without symbols well.
925 So fake up a local symbol in the absolute section. */
926 fixp->fx_addsy = section_symbol (absolute_section);
929 dump_section_relocs (abfd, sec, stderr);
933 write_relocs (abfd, sec, xxx)
936 PTR xxx ATTRIBUTE_UNUSED;
938 segment_info_type *seginfo = seg_info (sec);
945 /* If seginfo is NULL, we did not create this section; don't do
950 fixup_segment (seginfo->fix_root, sec);
953 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
956 #ifndef RELOC_EXPANSION_POSSIBLE
957 /* Set up reloc information as well. */
958 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
959 memset ((char *) relocs, 0, n * sizeof (arelent *));
962 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
965 bfd_reloc_status_type s;
974 /* If this is an undefined symbol which was equated to another
975 symbol, then generate the reloc against the latter symbol
976 rather than the former. */
977 sym = fixp->fx_addsy;
978 while (symbol_equated_reloc_p (sym))
982 /* We must avoid looping, as that can occur with a badly
984 n = symbol_get_value_expression (sym)->X_add_symbol;
987 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
990 fixp->fx_addsy = sym;
992 reloc = tc_gen_reloc (sec, fixp);
1000 /* This test is triggered inappropriately for the SH. */
1001 if (fixp->fx_where + fixp->fx_size
1002 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1006 s = bfd_install_relocation (stdoutput, reloc,
1007 fixp->fx_frag->fr_literal,
1008 fixp->fx_frag->fr_address,
1014 case bfd_reloc_overflow:
1015 as_bad_where (fixp->fx_file, fixp->fx_line,
1016 _("relocation overflow"));
1018 case bfd_reloc_outofrange:
1019 as_bad_where (fixp->fx_file, fixp->fx_line,
1020 _("relocation out of range"));
1023 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1024 fixp->fx_file, fixp->fx_line, s);
1026 relocs[i++] = reloc;
1029 n = n * MAX_RELOC_EXPANSION;
1030 /* Set up reloc information as well. */
1031 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
1034 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1038 bfd_reloc_status_type s;
1048 /* If this is an undefined symbol which was equated to another
1049 symbol, then generate the reloc against the latter symbol
1050 rather than the former. */
1051 sym = fixp->fx_addsy;
1052 while (symbol_equated_reloc_p (sym))
1056 /* We must avoid looping, as that can occur with a badly
1058 n = symbol_get_value_expression (sym)->X_add_symbol;
1061 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
1064 fixp->fx_addsy = sym;
1066 reloc = tc_gen_reloc (sec, fixp);
1068 for (j = 0; reloc[j]; j++)
1070 relocs[i++] = reloc[j];
1073 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1074 if (fixp->fx_where + fixp->fx_size
1075 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1076 as_bad_where (fixp->fx_file, fixp->fx_line,
1077 _("internal error: fixup not contained within frag"));
1078 for (j = 0; reloc[j]; j++)
1080 s = bfd_install_relocation (stdoutput, reloc[j],
1081 fixp->fx_frag->fr_literal,
1082 fixp->fx_frag->fr_address,
1088 case bfd_reloc_overflow:
1089 as_bad_where (fixp->fx_file, fixp->fx_line,
1090 _("relocation overflow"));
1092 case bfd_reloc_outofrange:
1093 as_bad_where (fixp->fx_file, fixp->fx_line,
1094 _("relocation out of range"));
1097 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1098 fixp->fx_file, fixp->fx_line, s);
1109 sympp = bfd_get_outsymbols (stdoutput);
1110 nsyms = bfd_get_symcount (stdoutput);
1111 for (i = 0; i < n; i++)
1112 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1114 for (j = 0; j < nsyms; j++)
1115 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1124 bfd_set_reloc (stdoutput, sec, relocs, n);
1126 bfd_set_section_flags (abfd, sec,
1127 (bfd_get_section_flags (abfd, sec)
1128 & (flagword) ~SEC_RELOC));
1130 #ifdef SET_SECTION_RELOCS
1131 SET_SECTION_RELOCS (sec, relocs, n);
1139 fprintf (stderr, "relocs for sec %s\n", sec->name);
1140 for (i = 0; i < n; i++)
1143 s = *r->sym_ptr_ptr;
1144 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1145 i, r, r->address, s->name, r->addend);
1152 write_contents (abfd, sec, xxx)
1153 bfd *abfd ATTRIBUTE_UNUSED;
1155 PTR xxx ATTRIBUTE_UNUSED;
1157 segment_info_type *seginfo = seg_info (sec);
1158 unsigned long offset = 0;
1161 /* Write out the frags. */
1163 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1166 for (f = seginfo->frchainP->frch_root;
1171 unsigned long fill_size;
1175 assert (f->fr_type == rs_fill);
1178 x = bfd_set_section_contents (stdoutput, sec,
1179 f->fr_literal, (file_ptr) offset,
1180 (bfd_size_type) f->fr_fix);
1183 bfd_perror (stdoutput->filename);
1184 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1185 exit (EXIT_FAILURE);
1187 offset += f->fr_fix;
1189 fill_literal = f->fr_literal + f->fr_fix;
1190 fill_size = f->fr_var;
1191 count = f->fr_offset;
1192 assert (count >= 0);
1193 if (fill_size && count)
1196 if (fill_size > sizeof (buf))
1198 /* Do it the old way. Can this ever happen? */
1201 x = bfd_set_section_contents (stdoutput, sec,
1204 (bfd_size_type) fill_size);
1207 bfd_perror (stdoutput->filename);
1208 as_perror (_("FATAL: Can't write %s"),
1209 stdoutput->filename);
1210 exit (EXIT_FAILURE);
1212 offset += fill_size;
1217 /* Build a buffer full of fill objects and output it as
1218 often as necessary. This saves on the overhead of
1219 potentially lots of bfd_set_section_contents calls. */
1223 n_per_buf = sizeof (buf);
1224 memset (buf, *fill_literal, n_per_buf);
1229 n_per_buf = sizeof (buf) / fill_size;
1230 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1231 memcpy (bufp, fill_literal, fill_size);
1233 for (; count > 0; count -= n_per_buf)
1235 n_per_buf = n_per_buf > count ? count : n_per_buf;
1236 x = bfd_set_section_contents
1237 (stdoutput, sec, buf, (file_ptr) offset,
1238 (bfd_size_type) n_per_buf * fill_size);
1240 as_fatal (_("cannot write to output file"));
1241 offset += n_per_buf * fill_size;
1249 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1251 merge_data_into_text ()
1253 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1254 seg_info (text_section)->frchainP->frch_last->fr_next =
1255 seg_info (data_section)->frchainP->frch_root;
1256 seg_info (text_section)->frchainP->frch_last =
1257 seg_info (data_section)->frchainP->frch_last;
1258 seg_info (data_section)->frchainP = 0;
1262 text_last_frag->fr_next = data_frag_root;
1263 text_last_frag = data_last_frag;
1264 data_last_frag = NULL;
1265 data_frag_root = NULL;
1268 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1269 tmp->fx_next = data_fix_root;
1270 text_fix_tail = data_fix_tail;
1273 text_fix_root = data_fix_root;
1274 data_fix_root = NULL;
1277 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1279 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1281 relax_and_size_all_segments ()
1285 relax_segment (text_frag_root, SEG_TEXT);
1286 relax_segment (data_frag_root, SEG_DATA);
1287 relax_segment (bss_frag_root, SEG_BSS);
1289 /* Now the addresses of frags are correct within the segment. */
1290 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1291 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1292 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1294 /* Join the 2 segments into 1 huge segment.
1295 To do this, re-compute every rn_address in the SEG_DATA frags.
1296 Then join the data frags after the text frags.
1298 Determine a_data [length of data segment]. */
1301 register relax_addressT slide;
1303 know ((text_last_frag->fr_type == rs_fill)
1304 && (text_last_frag->fr_offset == 0));
1306 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1307 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1308 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1310 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1311 /* For b.out: If the data section has a strict alignment
1312 requirement, its load address in the .o file will be
1313 rounded up from the size of the text section. These
1314 two values are *not* the same! Similarly for the bss
1316 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1319 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1320 fragP->fr_address += slide;
1322 know (text_last_frag != 0);
1323 text_last_frag->fr_next = data_frag_root;
1327 H_SET_DATA_SIZE (&headers, 0);
1331 /* See above comments on b.out data section address. */
1334 if (data_last_frag == 0)
1335 bss_vma = H_GET_TEXT_SIZE (&headers);
1337 bss_vma = data_last_frag->fr_address;
1338 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1339 bss_address_frag.fr_address = bss_vma;
1341 #else /* ! OBJ_BOUT */
1342 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1343 H_GET_DATA_SIZE (&headers));
1345 #endif /* ! OBJ_BOUT */
1347 /* Slide all the frags. */
1350 relax_addressT slide = bss_address_frag.fr_address;
1352 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1353 fragP->fr_address += slide;
1357 H_SET_BSS_SIZE (&headers,
1358 bss_last_frag->fr_address - bss_frag_root->fr_address);
1360 H_SET_BSS_SIZE (&headers, 0);
1362 #endif /* ! BFD_ASSEMBLER && ! BFD */
1364 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1366 #ifdef BFD_ASSEMBLER
1374 extern PTR bfd_alloc PARAMS ((bfd *, bfd_size_type));
1376 /* Count symbols. We can't rely on a count made by the loop in
1377 write_object_file, because *_frob_file may add a new symbol or
1380 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1386 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1388 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1389 symp = symbol_rootP;
1390 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1392 asympp[i] = symbol_get_bfdsym (symp);
1393 symbol_mark_written (symp);
1398 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1399 assert (result == true);
1400 symbol_table_frozen = 1;
1404 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1406 set_segment_vma (abfd, sec, xxx)
1409 PTR xxx ATTRIBUTE_UNUSED;
1411 static bfd_vma addr = 0;
1413 bfd_set_section_vma (abfd, sec, addr);
1414 addr += bfd_section_size (abfd, sec);
1416 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1418 /* Finish the subsegments. After every sub-segment, we fake an
1419 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1420 ".fill 0" because that is the kind of frag that requires least
1421 thought. ".align" frags like to have a following frag since that
1422 makes calculating their intended length trivial. */
1424 #ifndef SUB_SEGMENT_ALIGN
1426 /* The last subsegment gets an aligment corresponding to the alignment
1427 of the section. This allows proper nop-filling at the end of
1428 code-bearing sections. */
1429 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1430 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1431 ? get_recorded_alignment (SEG) : 0)
1433 #ifdef BFD_ASSEMBLER
1434 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1436 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 2
1444 struct frchain *frchainP;
1446 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1450 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1452 /* This now gets called even if we had errors. In that case,
1453 any alignment is meaningless, and, moreover, will look weird
1454 if we are generating a listing. */
1456 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1458 if (subseg_text_p (now_seg))
1459 frag_align_code (alignment, 0);
1461 frag_align (alignment, 0, 0);
1463 /* frag_align will have left a new frag.
1464 Use this last frag for an empty ".fill".
1466 For this segment ...
1467 Create a last frag. Do not leave a "being filled in frag". */
1468 frag_wane (frag_now);
1469 frag_now->fr_fix = 0;
1470 know (frag_now->fr_next == NULL);
1474 /* Write the object file. */
1477 write_object_file ()
1479 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1480 fragS *fragP; /* Track along all frags. */
1483 /* Do we really want to write it? */
1485 int n_warns, n_errs;
1486 n_warns = had_warnings ();
1487 n_errs = had_errors ();
1488 /* The -Z flag indicates that an object file should be generated,
1489 regardless of warnings and errors. */
1490 if (flag_always_generate_output)
1492 if (n_warns || n_errs)
1493 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1494 n_errs, n_errs == 1 ? "" : "s",
1495 n_warns, n_warns == 1 ? "" : "s");
1500 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1501 n_errs, n_errs == 1 ? "" : "s",
1502 n_warns, n_warns == 1 ? "" : "s");
1507 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1508 a routine to check for the definition of the procedure "_main",
1509 and if so -- fix it up so that it can be program entry point. */
1510 vms_check_for_main ();
1511 #endif /* OBJ_VMS */
1513 /* From now on, we don't care about sub-segments. Build one frag chain
1514 for each segment. Linked thru fr_next. */
1516 #ifdef BFD_ASSEMBLER
1517 /* Remove the sections created by gas for its own purposes. */
1522 seclist = &stdoutput->sections;
1525 if (*seclist == reg_section || *seclist == expr_section)
1527 bfd_section_list_remove (stdoutput, seclist);
1528 stdoutput->section_count--;
1531 seclist = &(*seclist)->next;
1534 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1537 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1539 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1540 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1541 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1544 /* We have two segments. If user gave -R flag, then we must put the
1545 data frags into the text segment. Do this before relaxing so
1546 we know to take advantage of -R and make shorter addresses. */
1547 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1548 if (flag_readonly_data_in_text)
1550 merge_data_into_text ();
1554 #ifdef BFD_ASSEMBLER
1559 #ifndef WORKING_DOT_WORD
1560 /* We need to reset the markers in the broken word list and
1561 associated frags between calls to relax_segment (via
1562 relax_seg). Since the broken word list is global, we do it
1563 once per round, rather than locally in relax_segment for each
1565 struct broken_word *brokp;
1567 for (brokp = broken_words;
1568 brokp != (struct broken_word *) NULL;
1569 brokp = brokp->next_broken_word)
1573 if (brokp->dispfrag != (fragS *) NULL
1574 && brokp->dispfrag->fr_type == rs_broken_word)
1575 brokp->dispfrag->fr_subtype = 0;
1580 bfd_map_over_sections (stdoutput, relax_seg, &changed);
1585 /* Note - Most ports will use the default value of
1586 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1587 local symbols to be resolved, removing their frag information.
1588 Some ports however, will not have finished relaxing all of
1589 their frags and will still need the local symbol frag
1590 information. These ports can set
1591 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1592 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1594 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1596 relax_and_size_all_segments ();
1597 #endif /* BFD_ASSEMBLER */
1599 /* Relaxation has completed. Freeze all syms. */
1602 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1603 /* Now that the segments have their final sizes, run through the
1604 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1605 should too. Currently, only DJGPP uses this code, but other
1606 COFF targets may need to execute this too. */
1607 bfd_map_over_sections (stdoutput, set_segment_vma, (char *) 0);
1610 #ifndef BFD_ASSEMBLER
1611 /* Crawl the symbol chain.
1613 For each symbol whose value depends on a frag, take the address of
1614 that frag and subsume it into the value of the symbol.
1615 After this, there is just one way to lookup a symbol value.
1616 Values are left in their final state for object file emission.
1617 We adjust the values of 'L' local symbols, even if we do
1618 not intend to emit them to the object file, because their values
1619 are needed for fix-ups.
1621 Unless we saw a -L flag, remove all symbols that begin with 'L'
1622 from the symbol chain. (They are still pointed to by the fixes.)
1624 Count the remaining symbols.
1625 Assign a symbol number to each symbol.
1626 Count the number of string-table chars we will emit.
1627 Put this info into the headers as appropriate. */
1628 know (zero_address_frag.fr_address == 0);
1629 string_byte_count = sizeof (string_byte_count);
1631 obj_crawl_symbol_chain (&headers);
1633 if (string_byte_count == sizeof (string_byte_count))
1634 string_byte_count = 0;
1636 H_SET_STRING_SIZE (&headers, string_byte_count);
1638 /* Addresses of frags now reflect addresses we use in the object file.
1639 Symbol values are correct.
1640 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1641 Also converting any machine-dependent frags using md_convert_frag(); */
1642 subseg_change (SEG_TEXT, 0);
1644 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1646 /* At this point we have linked all the frags into a single
1647 chain. However, cvt_frag_to_fill may call md_convert_frag
1648 which may call fix_new. We need to ensure that fix_new adds
1649 the fixup to the right section. */
1650 if (fragP == data_frag_root)
1651 subseg_change (SEG_DATA, 0);
1653 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1655 /* Some assert macros don't work with # directives mixed in. */
1657 if (!(fragP->fr_next == NULL
1659 || fragP->fr_next == data_frag_root
1661 || ((fragP->fr_next->fr_address - fragP->fr_address)
1662 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1666 #endif /* ! BFD_ASSEMBLER */
1668 #ifndef WORKING_DOT_WORD
1670 struct broken_word *lie;
1671 struct broken_word **prevP;
1673 prevP = &broken_words;
1674 for (lie = broken_words; lie; lie = lie->next_broken_word)
1679 subseg_change (lie->seg, lie->subseg);
1680 exp.X_op = O_subtract;
1681 exp.X_add_symbol = lie->add;
1682 exp.X_op_symbol = lie->sub;
1683 exp.X_add_number = lie->addnum;
1684 #ifdef BFD_ASSEMBLER
1685 #ifdef TC_CONS_FIX_NEW
1686 TC_CONS_FIX_NEW (lie->frag,
1687 lie->word_goes_here - lie->frag->fr_literal,
1690 fix_new_exp (lie->frag,
1691 lie->word_goes_here - lie->frag->fr_literal,
1692 2, &exp, 0, BFD_RELOC_16);
1695 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1696 fix_new_exp (lie->frag,
1697 lie->word_goes_here - lie->frag->fr_literal,
1698 2, &exp, 0, NO_RELOC);
1701 fix_new_ns32k_exp (lie->frag,
1702 lie->word_goes_here - lie->frag->fr_literal,
1703 2, &exp, 0, 0, 2, 0, 0);
1705 fix_new_exp (lie->frag,
1706 lie->word_goes_here - lie->frag->fr_literal,
1708 #endif /* TC_NS32K */
1709 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1710 #endif /* BFD_ASSEMBLER */
1711 *prevP = lie->next_broken_word;
1714 prevP = &(lie->next_broken_word);
1716 for (lie = broken_words; lie;)
1718 struct broken_word *untruth;
1720 addressT table_addr;
1721 addressT from_addr, to_addr;
1724 subseg_change (lie->seg, lie->subseg);
1725 fragP = lie->dispfrag;
1727 /* Find out how many broken_words go here. */
1730 untruth && untruth->dispfrag == fragP;
1731 untruth = untruth->next_broken_word)
1732 if (untruth->added == 1)
1735 table_ptr = lie->dispfrag->fr_opcode;
1736 table_addr = (lie->dispfrag->fr_address
1737 + (table_ptr - lie->dispfrag->fr_literal));
1738 /* Create the jump around the long jumps. This is a short
1739 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1740 from_addr = table_addr;
1741 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1742 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1744 table_ptr += md_short_jump_size;
1745 table_addr += md_short_jump_size;
1748 lie && lie->dispfrag == fragP;
1749 m++, lie = lie->next_broken_word)
1751 if (lie->added == 2)
1753 /* Patch the jump table. */
1754 /* This is the offset from ??? to table_ptr+0. */
1755 to_addr = table_addr - S_GET_VALUE (lie->sub);
1756 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1757 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1759 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1760 for (untruth = lie->next_broken_word;
1761 untruth && untruth->dispfrag == fragP;
1762 untruth = untruth->next_broken_word)
1764 if (untruth->use_jump == lie)
1765 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1768 /* Install the long jump. */
1769 /* This is a long jump from table_ptr+0 to the final target. */
1770 from_addr = table_addr;
1771 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1772 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1774 table_ptr += md_long_jump_size;
1775 table_addr += md_long_jump_size;
1779 #endif /* not WORKING_DOT_WORD */
1781 #ifndef BFD_ASSEMBLER
1784 char *the_object_file;
1785 long object_file_size;
1786 /* Scan every FixS performing fixups. We had to wait until now to
1787 do this because md_convert_frag() may have made some fixSs. */
1790 subseg_change (SEG_TEXT, 0);
1791 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1792 subseg_change (SEG_DATA, 0);
1793 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1794 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1796 /* FIXME: Move this stuff into the pre-write-hook. */
1797 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1798 H_SET_ENTRY_POINT (&headers, 0);
1800 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1802 object_file_size = H_GET_FILE_SIZE (&headers);
1803 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1805 output_file_create (out_file_name);
1807 obj_header_append (&next_object_file_charP, &headers);
1809 know ((next_object_file_charP - the_object_file)
1810 == H_GET_HEADER_SIZE (&headers));
1813 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1815 register long count;
1816 register char *fill_literal;
1817 register long fill_size;
1820 know (fragP->fr_type == rs_fill);
1821 append (&next_object_file_charP, fragP->fr_literal,
1822 (unsigned long) fragP->fr_fix);
1823 fill_literal = fragP->fr_literal + fragP->fr_fix;
1824 fill_size = fragP->fr_var;
1825 know (fragP->fr_offset >= 0);
1827 for (count = fragP->fr_offset; count; count--)
1828 append (&next_object_file_charP, fill_literal,
1829 (unsigned long) fill_size);
1832 know ((next_object_file_charP - the_object_file)
1833 == (H_GET_HEADER_SIZE (&headers)
1834 + H_GET_TEXT_SIZE (&headers)
1835 + H_GET_DATA_SIZE (&headers)));
1837 /* Emit relocations. */
1838 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1839 (relax_addressT) 0);
1840 know ((next_object_file_charP - the_object_file)
1841 == (H_GET_HEADER_SIZE (&headers)
1842 + H_GET_TEXT_SIZE (&headers)
1843 + H_GET_DATA_SIZE (&headers)
1844 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1846 /* Make addresses in data relocation directives relative to beginning of
1847 first data fragment, not end of last text fragment: alignment of the
1848 start of the data segment may place a gap between the segments. */
1849 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1850 data0_frchainP->frch_root->fr_address);
1852 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1853 text_last_frag->fr_address);
1854 #endif /* TC_I960 */
1856 know ((next_object_file_charP - the_object_file)
1857 == (H_GET_HEADER_SIZE (&headers)
1858 + H_GET_TEXT_SIZE (&headers)
1859 + H_GET_DATA_SIZE (&headers)
1860 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1861 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1863 /* Emit line number entries. */
1864 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1865 know ((next_object_file_charP - the_object_file)
1866 == (H_GET_HEADER_SIZE (&headers)
1867 + H_GET_TEXT_SIZE (&headers)
1868 + H_GET_DATA_SIZE (&headers)
1869 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1870 + H_GET_DATA_RELOCATION_SIZE (&headers)
1871 + H_GET_LINENO_SIZE (&headers)));
1874 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1875 know ((next_object_file_charP - the_object_file)
1876 == (H_GET_HEADER_SIZE (&headers)
1877 + H_GET_TEXT_SIZE (&headers)
1878 + H_GET_DATA_SIZE (&headers)
1879 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1880 + H_GET_DATA_RELOCATION_SIZE (&headers)
1881 + H_GET_LINENO_SIZE (&headers)
1882 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1885 if (string_byte_count > 0)
1886 obj_emit_strings (&next_object_file_charP);
1889 bfd_seek (stdoutput, (file_ptr) 0, 0);
1890 bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
1893 /* Write the data to the file. */
1894 output_file_append (the_object_file, object_file_size, out_file_name);
1895 free (the_object_file);
1899 /* Now do the VMS-dependent part of writing the object file. */
1900 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1901 H_GET_DATA_SIZE (&headers),
1902 H_GET_BSS_SIZE (&headers),
1903 text_frag_root, data_frag_root);
1904 #endif /* OBJ_VMS */
1905 #else /* BFD_ASSEMBLER */
1907 /* Resolve symbol values. This needs to be done before processing
1913 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1914 resolve_symbol_value (symp);
1916 resolve_local_symbol_values ();
1920 #ifdef tc_frob_file_before_adjust
1921 tc_frob_file_before_adjust ();
1923 #ifdef obj_frob_file_before_adjust
1924 obj_frob_file_before_adjust ();
1927 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1929 /* Set up symbol table, and write it out. */
1934 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1939 if (symbol_mri_common_p (symp))
1941 if (S_IS_EXTERNAL (symp))
1942 as_bad (_("%s: global symbols not supported in common sections"),
1944 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1948 name = S_GET_NAME (symp);
1952 decode_local_label_name ((char *) S_GET_NAME (symp));
1953 /* They only differ if `name' is a fb or dollar local
1955 if (name2 != name && ! S_IS_DEFINED (symp))
1956 as_bad (_("local label `%s' is not defined"), name2);
1959 /* Do it again, because adjust_reloc_syms might introduce
1960 more symbols. They'll probably only be section symbols,
1961 but they'll still need to have the values computed. */
1962 resolve_symbol_value (symp);
1964 /* Skip symbols which were equated to undefined or common
1966 if (symbol_equated_reloc_p (symp))
1968 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1972 /* So far, common symbols have been treated like undefined symbols.
1973 Put them in the common section now. */
1974 if (S_IS_DEFINED (symp) == 0
1975 && S_GET_VALUE (symp) != 0)
1976 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1978 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1979 S_GET_NAME (symp), symp,
1981 symbol_get_bfdsym (symp)->flags,
1982 segment_name (S_GET_SEGMENT (symp)));
1985 #ifdef obj_frob_symbol
1986 obj_frob_symbol (symp, punt);
1988 #ifdef tc_frob_symbol
1989 if (! punt || symbol_used_in_reloc_p (symp))
1990 tc_frob_symbol (symp, punt);
1993 /* If we don't want to keep this symbol, splice it out of
1994 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1995 want section symbols. Otherwise, we skip local symbols
1996 and symbols that the frob_symbol macros told us to punt,
1997 but we keep such symbols if they are used in relocs. */
1998 if ((! EMIT_SECTION_SYMBOLS
1999 && symbol_section_p (symp))
2000 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
2001 opposites. Sometimes the former checks flags and the
2002 latter examines the name... */
2003 || (!S_IS_EXTERN (symp)
2004 && (S_IS_LOCAL (symp) || punt)
2005 && ! symbol_used_in_reloc_p (symp)))
2007 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2009 /* After symbol_remove, symbol_next(symp) still returns
2010 the one that came after it in the chain. So we don't
2011 need to do any extra cleanup work here. */
2015 /* Make sure we really got a value for the symbol. */
2016 if (! symbol_resolved_p (symp))
2018 as_bad (_("can't resolve value for symbol `%s'"),
2020 symbol_mark_resolved (symp);
2023 /* Set the value into the BFD symbol. Up til now the value
2024 has only been kept in the gas symbolS struct. */
2025 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2031 /* Now do any format-specific adjustments to the symbol table, such
2032 as adding file symbols. */
2033 #ifdef tc_adjust_symtab
2034 tc_adjust_symtab ();
2036 #ifdef obj_adjust_symtab
2037 obj_adjust_symtab ();
2040 /* Now that all the sizes are known, and contents correct, we can
2041 start writing to the file. */
2044 /* If *_frob_file changes the symbol value at this point, it is
2045 responsible for moving the changed value into symp->bsym->value
2046 as well. Hopefully all symbol value changing can be done in
2051 #ifdef obj_frob_file
2055 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2057 #ifdef tc_frob_file_after_relocs
2058 tc_frob_file_after_relocs ();
2060 #ifdef obj_frob_file_after_relocs
2061 obj_frob_file_after_relocs ();
2064 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2065 #endif /* BFD_ASSEMBLER */
2069 #ifdef TC_GENERIC_RELAX_TABLE
2071 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2074 relax_frag (segment, fragP, stretch)
2079 const relax_typeS *this_type;
2080 const relax_typeS *start_type;
2081 relax_substateT next_state;
2082 relax_substateT this_state;
2088 const relax_typeS *table;
2090 target = fragP->fr_offset;
2091 address = fragP->fr_address;
2092 table = TC_GENERIC_RELAX_TABLE;
2093 this_state = fragP->fr_subtype;
2094 start_type = this_type = table + this_state;
2095 symbolP = fragP->fr_symbol;
2101 sym_frag = symbol_get_frag (symbolP);
2103 #ifndef DIFF_EXPR_OK
2104 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2105 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2106 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2107 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2108 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2110 know (sym_frag != NULL);
2112 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2113 || sym_frag == &zero_address_frag);
2114 target += S_GET_VALUE (symbolP);
2116 /* If frag has yet to be reached on this pass,
2117 assume it will move by STRETCH just as we did.
2118 If this is not so, it will be because some frag
2119 between grows, and that will force another pass. */
2122 && sym_frag->relax_marker != fragP->relax_marker
2123 && S_GET_SEGMENT (symbolP) == segment)
2129 aim = target - address - fragP->fr_fix;
2130 #ifdef TC_PCREL_ADJUST
2131 /* Currently only the ns32k family needs this. */
2132 aim += TC_PCREL_ADJUST (fragP);
2134 /* This machine doesn't want to use pcrel_adjust.
2135 In that case, pcrel_adjust should be zero. */
2137 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2140 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2141 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2146 /* Look backwards. */
2147 for (next_state = this_type->rlx_more; next_state;)
2148 if (aim >= this_type->rlx_backward)
2152 /* Grow to next state. */
2153 this_state = next_state;
2154 this_type = table + this_state;
2155 next_state = this_type->rlx_more;
2160 /* Look forwards. */
2161 for (next_state = this_type->rlx_more; next_state;)
2162 if (aim <= this_type->rlx_forward)
2166 /* Grow to next state. */
2167 this_state = next_state;
2168 this_type = table + this_state;
2169 next_state = this_type->rlx_more;
2173 growth = this_type->rlx_length - start_type->rlx_length;
2175 fragP->fr_subtype = this_state;
2179 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2181 /* Relax_align. Advance location counter to next address that has 'alignment'
2182 lowest order bits all 0s, return size of adjustment made. */
2183 static relax_addressT
2184 relax_align (address, alignment)
2185 register relax_addressT address; /* Address now. */
2186 register int alignment; /* Alignment (binary). */
2188 relax_addressT mask;
2189 relax_addressT new_address;
2191 mask = ~((~0) << alignment);
2192 new_address = (address + mask) & (~mask);
2193 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2195 /* We must provide lots of padding, so the linker can discard it
2196 when needed. The linker will not add extra space, ever. */
2197 new_address += (1 << alignment);
2199 return (new_address - address);
2202 /* Now we have a segment, not a crowd of sub-segments, we can make
2207 After this, all frags in this segment have addresses that are correct
2208 within the segment. Since segments live in different file addresses,
2209 these frag addresses may not be the same as final object-file
2213 relax_segment (segment_frag_root, segment)
2214 struct frag *segment_frag_root;
2217 register struct frag *fragP;
2218 register relax_addressT address;
2221 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2222 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2224 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2225 subseg_change (segment, 0);
2227 /* For each frag in segment: count and store (a 1st guess of)
2230 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2232 fragP->relax_marker = 0;
2233 fragP->fr_address = address;
2234 address += fragP->fr_fix;
2236 switch (fragP->fr_type)
2239 address += fragP->fr_offset * fragP->fr_var;
2246 addressT offset = relax_align (address, (int) fragP->fr_offset);
2248 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2251 if (offset % fragP->fr_var != 0)
2253 as_bad_where (fragP->fr_file, fragP->fr_line,
2254 _("alignment padding (%lu bytes) not a multiple of %ld"),
2255 (unsigned long) offset, (long) fragP->fr_var);
2256 offset -= (offset % fragP->fr_var);
2265 /* Assume .org is nugatory. It will grow with 1st relax. */
2268 case rs_machine_dependent:
2269 /* If fr_symbol is an expression, this call to
2270 resolve_symbol_value sets up the correct segment, which will
2271 likely be needed in md_estimate_size_before_relax. */
2272 if (fragP->fr_symbol)
2273 resolve_symbol_value (fragP->fr_symbol);
2275 address += md_estimate_size_before_relax (fragP, segment);
2278 #ifndef WORKING_DOT_WORD
2279 /* Broken words don't concern us yet. */
2280 case rs_broken_word:
2285 /* Initial guess is always 1; doing otherwise can result in
2286 stable solutions that are larger than the minimum. */
2287 address += fragP->fr_offset = 1;
2291 address += eh_frame_estimate_size_before_relax (fragP);
2295 address += dwarf2dbg_estimate_size_before_relax (fragP);
2299 BAD_CASE (fragP->fr_type);
2306 long stretch; /* May be any size, 0 or negative. */
2307 /* Cumulative number of addresses we have relaxed this pass.
2308 We may have relaxed more than one address. */
2309 int stretched; /* Have we stretched on this pass? */
2310 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2311 grew, and another shrank. If a branch instruction doesn't fit anymore,
2312 we could be scrod. */
2319 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2322 addressT was_address;
2326 fragP->relax_marker ^= 1;
2327 was_address = fragP->fr_address;
2328 address = fragP->fr_address += stretch;
2329 symbolP = fragP->fr_symbol;
2330 offset = fragP->fr_offset;
2332 switch (fragP->fr_type)
2334 case rs_fill: /* .fill never relaxes. */
2338 #ifndef WORKING_DOT_WORD
2339 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2340 for it I do not want to write it. I do not want to have
2341 anything to do with it. This is not the proper way to
2342 implement this misfeature. */
2343 case rs_broken_word:
2345 struct broken_word *lie;
2346 struct broken_word *untruth;
2348 /* Yes this is ugly (storing the broken_word pointer
2349 in the symbol slot). Still, this whole chunk of
2350 code is ugly, and I don't feel like doing anything
2351 about it. Think of it as stubbornness in action. */
2353 for (lie = (struct broken_word *) (fragP->fr_symbol);
2354 lie && lie->dispfrag == fragP;
2355 lie = lie->next_broken_word)
2361 offset = (S_GET_VALUE (lie->add)
2363 - S_GET_VALUE (lie->sub));
2364 if (offset <= -32768 || offset >= 32767)
2366 if (flag_warn_displacement)
2369 sprint_value (buf, (addressT) lie->addnum);
2370 as_warn_where (fragP->fr_file, fragP->fr_line,
2371 _(".word %s-%s+%s didn't fit"),
2372 S_GET_NAME (lie->add),
2373 S_GET_NAME (lie->sub),
2377 if (fragP->fr_subtype == 0)
2379 fragP->fr_subtype++;
2380 growth += md_short_jump_size;
2382 for (untruth = lie->next_broken_word;
2383 untruth && untruth->dispfrag == lie->dispfrag;
2384 untruth = untruth->next_broken_word)
2385 if ((symbol_get_frag (untruth->add)
2386 == symbol_get_frag (lie->add))
2387 && (S_GET_VALUE (untruth->add)
2388 == S_GET_VALUE (lie->add)))
2391 untruth->use_jump = lie;
2393 growth += md_long_jump_size;
2398 } /* case rs_broken_word */
2404 addressT oldoff, newoff;
2406 oldoff = relax_align (was_address + fragP->fr_fix,
2408 newoff = relax_align (address + fragP->fr_fix,
2411 if (fragP->fr_subtype != 0)
2413 if (oldoff > fragP->fr_subtype)
2415 if (newoff > fragP->fr_subtype)
2419 growth = newoff - oldoff;
2425 addressT target = offset;
2430 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2431 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2432 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2433 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2434 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2435 know (symbolP->sy_frag);
2436 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2437 || (symbolP->sy_frag == &zero_address_frag));
2439 /* Convert from an actual address to an octet offset
2440 into the section. Here it is assumed that the
2441 section's VMA is zero, and can omit subtracting it
2442 from the symbol's value to get the address offset. */
2443 know (S_GET_SECTION (symbolP)->vma == 0);
2444 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2447 know (fragP->fr_next);
2448 after = fragP->fr_next->fr_address;
2449 growth = target - after;
2452 /* Growth may be negative, but variable part of frag
2453 cannot have fewer than 0 chars. That is, we can't
2455 as_bad_where (fragP->fr_file, fragP->fr_line,
2456 _("attempt to .org backwards"));
2458 /* We've issued an error message. Change the
2459 frag to avoid cascading errors. */
2460 fragP->fr_type = rs_align;
2461 fragP->fr_subtype = 0;
2462 fragP->fr_offset = 0;
2463 fragP->fr_fix = after - address;
2467 /* This is an absolute growth factor */
2478 amount = S_GET_VALUE (symbolP);
2479 if (S_GET_SEGMENT (symbolP) != absolute_section
2480 || S_IS_COMMON (symbolP)
2481 || ! S_IS_DEFINED (symbolP))
2483 as_bad_where (fragP->fr_file, fragP->fr_line,
2484 _(".space specifies non-absolute value"));
2485 /* Prevent repeat of this error message. */
2486 fragP->fr_symbol = 0;
2488 else if (amount < 0)
2490 as_warn_where (fragP->fr_file, fragP->fr_line,
2491 _(".space or .fill with negative value, ignored"));
2492 fragP->fr_symbol = 0;
2495 growth = (was_address + fragP->fr_fix + amount
2496 - fragP->fr_next->fr_address);
2500 case rs_machine_dependent:
2501 #ifdef md_relax_frag
2502 growth = md_relax_frag (segment, fragP, stretch);
2504 #ifdef TC_GENERIC_RELAX_TABLE
2505 /* The default way to relax a frag is to look through
2506 TC_GENERIC_RELAX_TABLE. */
2507 growth = relax_frag (segment, fragP, stretch);
2508 #endif /* TC_GENERIC_RELAX_TABLE */
2517 value = resolve_symbol_value (fragP->fr_symbol);
2518 size = sizeof_leb128 (value, fragP->fr_subtype);
2519 growth = size - fragP->fr_offset;
2520 fragP->fr_offset = size;
2525 growth = eh_frame_relax_frag (fragP);
2529 growth = dwarf2dbg_relax_frag (fragP);
2533 BAD_CASE (fragP->fr_type);
2541 } /* For each frag in the segment. */
2543 while (stretched); /* Until nothing further to relax. */
2547 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2548 if (fragP->last_fr_address != fragP->fr_address)
2550 fragP->last_fr_address = fragP->fr_address;
2556 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2558 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2559 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2564 Go through all the fixS's in a segment and see which ones can be
2565 handled now. (These consist of fixS where we have since discovered
2566 the value of a symbol, or the address of the frag involved.)
2567 For each one, call md_apply_fix3 to put the fix into the frag data.
2569 Result is a count of how many relocation structs will be needed to
2570 handle the remaining fixS's that we couldn't completely handle here.
2571 These will be output later by emit_relocations(). */
2574 fixup_segment (fixP, this_segment)
2578 long seg_reloc_count = 0;
2579 symbolS *add_symbolP;
2580 symbolS *sub_symbolP;
2584 segT add_symbol_segment = absolute_section;
2586 /* If the linker is doing the relaxing, we must not do any fixups.
2588 Well, strictly speaking that's not true -- we could do any that
2589 are PC-relative and don't cross regions that could change size.
2590 And for the i960 we might be able to turn callx/callj into bal
2591 anyways in cases where we know the maximum displacement. */
2592 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
2594 for (; fixP; fixP = fixP->fx_next)
2596 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2597 return seg_reloc_count;
2600 for (; fixP; fixP = fixP->fx_next)
2603 fprintf (stderr, "\nprocessing fixup:\n");
2607 fragP = fixP->fx_frag;
2609 add_symbolP = fixP->fx_addsy;
2610 #ifdef TC_VALIDATE_FIX
2611 TC_VALIDATE_FIX (fixP, this_segment, skip);
2613 sub_symbolP = fixP->fx_subsy;
2614 add_number = fixP->fx_offset;
2615 pcrel = fixP->fx_pcrel;
2618 if (add_symbolP != NULL
2619 && symbol_mri_common_p (add_symbolP))
2621 know (add_symbolP->sy_value.X_op == O_symbol);
2622 add_number += S_GET_VALUE (add_symbolP);
2623 fixP->fx_offset = add_number;
2624 add_symbolP = fixP->fx_addsy =
2625 symbol_get_value_expression (add_symbolP)->X_add_symbol;
2629 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2633 resolve_symbol_value (sub_symbolP);
2634 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2636 if (add_symbolP != NULL)
2638 add_number += S_GET_VALUE (add_symbolP);
2640 fixP->fx_addsy = NULL;
2643 /* It's just -sym. */
2644 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2646 add_number -= S_GET_VALUE (sub_symbolP);
2647 fixP->fx_subsy = NULL;
2650 && S_GET_SEGMENT (sub_symbolP) == this_segment)
2652 /* Should try converting to a constant. */
2657 as_bad_where (fixP->fx_file, fixP->fx_line,
2658 _("negative of non-absolute symbol `%s'"),
2659 S_GET_NAME (sub_symbolP));
2661 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2662 && SEG_NORMAL (add_symbol_segment))
2664 /* Difference of 2 symbols from same segment.
2665 Can't make difference of 2 undefineds: 'value' means
2666 something different for N_UNDF. */
2668 /* Makes no sense to use the difference of 2 arbitrary symbols
2669 as the target of a call instruction. */
2671 as_bad_where (fixP->fx_file, fixP->fx_line,
2672 _("callj to difference of two symbols"));
2673 #endif /* TC_I960 */
2674 add_number += (S_GET_VALUE (add_symbolP)
2675 - S_GET_VALUE (sub_symbolP));
2678 /* See the comment below about 68k weirdness. */
2682 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2685 pcrel = 0; /* No further pcrel processing. */
2687 /* Let the target machine make the final determination
2688 as to whether or not a relocation will be needed to
2689 handle this fixup. */
2690 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment))
2693 fixP->fx_addsy = NULL;
2694 fixP->fx_subsy = NULL;
2699 /* Different segments in subtraction. */
2700 know (!(S_IS_EXTERNAL (sub_symbolP)
2701 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2703 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2704 add_number -= S_GET_VALUE (sub_symbolP);
2707 else if (S_GET_SEGMENT (sub_symbolP) == this_segment)
2709 /* Make it pc-relative. */
2712 /* Do this for m68k even if it's already described
2713 as pc-relative. On the m68k, an operand of
2714 "pc@(foo-.-2)" should address "foo" in a
2715 pc-relative mode. */
2720 add_number += MD_PCREL_FROM_SECTION (fixP,
2726 add_number -= S_GET_VALUE (sub_symbolP);
2731 #ifdef UNDEFINED_DIFFERENCE_OK
2732 /* The PA needs this for PIC code generation. We basically
2733 don't want to do anything if we have the difference of two
2734 symbols at this point. */
2737 /* Leave it alone. */
2740 #ifdef BFD_ASSEMBLER
2741 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2742 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2744 /* Leave it alone. */
2750 sprint_value (buf, fragP->fr_address + fixP->fx_where);
2751 as_bad_where (fixP->fx_file, fixP->fx_line,
2752 _("subtraction of two symbols in different sections `%s' {%s section} - `%s' {%s section} at file address %s"),
2753 S_GET_NAME (add_symbolP),
2754 segment_name (S_GET_SEGMENT (add_symbolP)),
2755 S_GET_NAME (sub_symbolP),
2756 segment_name (S_GET_SEGMENT (sub_symbolP)),
2764 if (add_symbol_segment == this_segment && pcrel && !plt
2765 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2767 /* This fixup was made when the symbol's segment was
2768 SEG_UNKNOWN, but it is now in the local segment.
2769 So we know how to do the address without relocation. */
2771 /* reloc_callj() may replace a 'call' with a 'calls' or a
2772 'bal', in which cases it modifies *fixP as appropriate.
2773 In the case of a 'calls', no further work is required,
2774 and *fixP has been set up to make the rest of the code
2777 #endif /* TC_I960 */
2779 add_number += S_GET_VALUE (add_symbolP);
2780 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2781 /* Lie. Don't want further pcrel processing. */
2784 /* Let the target machine make the final determination
2785 as to whether or not a relocation will be needed to
2786 handle this fixup. */
2787 if (!TC_FORCE_RELOCATION (fixP))
2790 fixP->fx_addsy = NULL;
2795 if (add_symbol_segment == absolute_section
2799 /* See comment about reloc_callj() above. */
2801 #endif /* TC_I960 */
2802 add_number += S_GET_VALUE (add_symbolP);
2804 /* Let the target machine make the final determination
2805 as to whether or not a relocation will be needed to
2806 handle this fixup. */
2808 if (!TC_FORCE_RELOCATION (fixP))
2810 fixP->fx_addsy = NULL;
2814 else if (add_symbol_segment == undefined_section
2815 #ifdef BFD_ASSEMBLER
2816 || bfd_is_com_section (add_symbol_segment)
2821 if ((int) fixP->fx_bit_fixP == 13)
2823 /* This is a COBR instruction. They have only a
2824 13-bit displacement and are only to be used
2825 for local branches: flag as error, don't generate
2827 as_bad_where (fixP->fx_file, fixP->fx_line,
2828 _("can't use COBR format with external label"));
2829 fixP->fx_addsy = NULL;
2833 #endif /* TC_I960 */
2837 if (S_IS_COMMON (add_symbolP))
2838 add_number += S_GET_VALUE (add_symbolP);
2839 #endif /* TE_I386AIX */
2840 #endif /* OBJ_COFF */
2846 if (TC_FIX_ADJUSTABLE (fixP))
2847 add_number += S_GET_VALUE (add_symbolP);
2854 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2855 if (add_symbolP == 0)
2857 #ifndef BFD_ASSEMBLER
2858 fixP->fx_addsy = &abs_symbol;
2860 fixP->fx_addsy = section_symbol (absolute_section);
2862 symbol_mark_used_in_reloc (fixP->fx_addsy);
2868 md_apply_fix3 (fixP, &add_number, this_segment);
2870 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
2872 if (fixP->fx_size < sizeof (valueT))
2877 mask--; /* Set all bits to one. */
2878 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
2879 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2881 char buf[50], buf2[50];
2882 sprint_value (buf, fragP->fr_address + fixP->fx_where);
2883 if (add_number > 1000)
2884 sprint_value (buf2, add_number);
2886 sprintf (buf2, "%ld", (long) add_number);
2887 as_bad_where (fixP->fx_file, fixP->fx_line,
2888 _("value of %s too large for field of %d bytes at %s"),
2889 buf2, fixP->fx_size, buf);
2890 } /* Generic error checking. */
2892 #ifdef WARN_SIGNED_OVERFLOW_WORD
2893 /* Warn if a .word value is too large when treated as a signed
2894 number. We already know it is not too negative. This is to
2895 catch over-large switches generated by gcc on the 68k. */
2896 if (!flag_signed_overflow_ok
2897 && fixP->fx_size == 2
2898 && add_number > 0x7fff)
2899 as_bad_where (fixP->fx_file, fixP->fx_line,
2900 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2902 (long) (fragP->fr_address + fixP->fx_where));
2904 } /* Not a bit fix. */
2906 #ifdef TC_VALIDATE_FIX
2907 skip: ATTRIBUTE_UNUSED_LABEL
2911 fprintf (stderr, "result:\n");
2914 } /* For each fixS in this segment. */
2916 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2917 return seg_reloc_count;
2920 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2923 number_to_chars_bigendian (buf, val, n)
2932 buf[n] = val & 0xff;
2938 number_to_chars_littleendian (buf, val, n)
2947 *buf++ = val & 0xff;
2953 write_print_statistics (file)
2956 fprintf (file, "fixups: %d\n", n_fixups);
2959 /* For debugging. */
2960 extern int indent_level;
2967 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2969 fprintf (stderr, " pcrel");
2970 if (fixp->fx_pcrel_adjust)
2971 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2972 if (fixp->fx_im_disp)
2975 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2977 fprintf (stderr, " im_disp");
2981 fprintf (stderr, " tcbit");
2983 fprintf (stderr, " done");
2984 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2985 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2986 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2987 #ifdef BFD_ASSEMBLER
2988 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2991 #ifdef NEED_FX_R_TYPE
2992 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2997 fprintf (stderr, "\n +<");
2998 print_symbol_value_1 (stderr, fixp->fx_addsy);
2999 fprintf (stderr, ">");
3003 fprintf (stderr, "\n -<");
3004 print_symbol_value_1 (stderr, fixp->fx_subsy);
3005 fprintf (stderr, ">");
3007 fprintf (stderr, "\n");
3008 #ifdef TC_FIX_DATA_PRINT
3009 TC_FIX_DATA_PRINT (stderr, fixp);