1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This thing should be set up to do byteordering correctly. But... */
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
31 /* This looks like a good idea. Let's try turning it on always, for now. */
32 #undef BFD_FAST_SECTION_FILL
33 #define BFD_FAST_SECTION_FILL
35 #ifndef TC_ADJUST_RELOC_COUNT
36 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
39 #ifndef TC_FORCE_RELOCATION
40 #define TC_FORCE_RELOCATION(FIXP) 0
43 #ifndef TC_FORCE_RELOCATION_SECTION
44 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
47 #ifndef TC_LINKRELAX_FIXUP
48 #define TC_LINKRELAX_FIXUP(SEG) 1
51 #ifndef TC_FIX_ADJUSTABLE
52 #define TC_FIX_ADJUSTABLE(fix) 1
55 #ifndef MD_PCREL_FROM_SECTION
56 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
59 #ifndef WORKING_DOT_WORD
60 extern CONST int md_short_jump_size;
61 extern CONST int md_long_jump_size;
64 int symbol_table_frozen;
65 void print_fixup PARAMS ((fixS *));
68 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
70 /* We generally attach relocs to frag chains. However, after we have
71 chained these all together into a segment, any relocs we add after
72 that must be attached to a segment. This will include relocs added
73 in md_estimate_size_for_relax, for example. */
74 static int frags_chained = 0;
80 struct frag *text_frag_root;
81 struct frag *data_frag_root;
82 struct frag *bss_frag_root;
84 struct frag *text_last_frag; /* Last frag in segment. */
85 struct frag *data_last_frag; /* Last frag in segment. */
86 static struct frag *bss_last_frag; /* Last frag in segment. */
90 static object_headers headers;
93 long string_byte_count;
94 char *next_object_file_charP; /* Tracks object file bytes. */
97 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
100 #endif /* BFD_ASSEMBLER */
105 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
106 symbolS *add, symbolS *sub,
107 offsetT offset, int pcrel,
108 bfd_reloc_code_real_type r_type));
110 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
111 symbolS *add, symbolS *sub,
112 offsetT offset, int pcrel,
115 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
116 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
118 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
119 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
120 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
123 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
124 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
125 static void relax_and_size_seg PARAMS ((bfd *, asection *, PTR));
126 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
127 static void write_relocs PARAMS ((bfd *, asection *, PTR));
128 static void write_contents PARAMS ((bfd *, asection *, PTR));
129 static void set_symtab PARAMS ((void));
131 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
132 static void merge_data_into_text PARAMS ((void));
134 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
135 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
136 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
137 static void relax_and_size_all_segments PARAMS ((void));
139 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
140 static void set_segment_vma PARAMS ((bfd *, asection *, PTR));
143 /* Create a fixS in obstack 'notes'. */
146 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
148 fragS *frag; /* Which frag? */
149 int where; /* Where in that frag? */
150 int size; /* 1, 2, or 4 usually. */
151 symbolS *add_symbol; /* X_add_symbol. */
152 symbolS *sub_symbol; /* X_op_symbol. */
153 offsetT offset; /* X_add_number. */
154 int pcrel; /* TRUE if PC-relative relocation. */
156 bfd_reloc_code_real_type r_type; /* Relocation type. */
158 int r_type; /* Relocation type. */
165 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
167 fixP->fx_frag = frag;
168 fixP->fx_where = where;
169 fixP->fx_size = size;
170 /* We've made fx_size a narrow field; check that it's wide enough. */
171 if (fixP->fx_size != size)
173 as_bad (_("field fx_size too small to hold %d"), size);
176 fixP->fx_addsy = add_symbol;
177 fixP->fx_subsy = sub_symbol;
178 fixP->fx_offset = offset;
179 fixP->fx_pcrel = pcrel;
181 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
182 fixP->fx_r_type = r_type;
184 fixP->fx_im_disp = 0;
185 fixP->fx_pcrel_adjust = 0;
186 fixP->fx_bit_fixP = 0;
187 fixP->fx_addnumber = 0;
190 fixP->fx_no_overflow = 0;
194 fixP->fx_cgen.insn = NULL;
195 fixP->fx_cgen.opinfo = 0;
199 TC_INIT_FIX_DATA (fixP);
202 as_where (&fixP->fx_file, &fixP->fx_line);
204 /* Usually, we want relocs sorted numerically, but while
205 comparing to older versions of gas that have relocs
206 reverse sorted, it is convenient to have this compile
207 time option. xoxorich. */
211 fixS **seg_fix_rootP = (frags_chained
212 ? &seg_info (now_seg)->fix_root
213 : &frchain_now->fix_root);
214 fixS **seg_fix_tailP = (frags_chained
215 ? &seg_info (now_seg)->fix_tail
216 : &frchain_now->fix_tail);
219 #ifdef REVERSE_SORT_RELOCS
221 fixP->fx_next = *seg_fix_rootP;
222 *seg_fix_rootP = fixP;
224 #else /* REVERSE_SORT_RELOCS */
226 fixP->fx_next = NULL;
229 (*seg_fix_tailP)->fx_next = fixP;
231 *seg_fix_rootP = fixP;
232 *seg_fix_tailP = fixP;
234 #endif /* REVERSE_SORT_RELOCS */
240 /* Create a fixup relative to a symbol (plus a constant). */
243 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
244 fragS *frag; /* Which frag? */
245 int where; /* Where in that frag? */
246 int size; /* 1, 2, or 4 usually. */
247 symbolS *add_symbol; /* X_add_symbol. */
248 offsetT offset; /* X_add_number. */
249 int pcrel; /* TRUE if PC-relative relocation. */
251 bfd_reloc_code_real_type r_type; /* Relocation type. */
253 int r_type; /* Relocation type. */
256 return fix_new_internal (frag, where, size, add_symbol,
257 (symbolS *) NULL, offset, pcrel, r_type);
260 /* Create a fixup for an expression. Currently we only support fixups
261 for difference expressions. That is itself more than most object
262 file formats support anyhow. */
265 fix_new_exp (frag, where, size, exp, pcrel, r_type)
266 fragS *frag; /* Which frag? */
267 int where; /* Where in that frag? */
268 int size; /* 1, 2, or 4 usually. */
269 expressionS *exp; /* Expression. */
270 int pcrel; /* TRUE if PC-relative relocation. */
272 bfd_reloc_code_real_type r_type; /* Relocation type. */
274 int r_type; /* Relocation type. */
287 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
288 the difference expression cannot immediately be reduced. */
290 symbolS *stmp = make_expr_symbol (exp);
292 exp->X_op = O_symbol;
293 exp->X_op_symbol = 0;
294 exp->X_add_symbol = stmp;
295 exp->X_add_number = 0;
297 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
301 add = exp->X_add_symbol;
302 off = exp->X_add_number;
304 #if defined(BFD_ASSEMBLER)
305 r_type = BFD_RELOC_RVA;
307 #if defined(TC_RVA_RELOC)
308 r_type = TC_RVA_RELOC;
310 as_fatal (_("rva not supported"));
316 sub = exp->X_add_symbol;
317 off = exp->X_add_number;
321 sub = exp->X_op_symbol;
324 add = exp->X_add_symbol;
327 off = exp->X_add_number;
331 add = make_expr_symbol (exp);
335 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
338 /* Append a string onto another string, bumping the pointer along. */
340 append (charPP, fromP, length)
343 unsigned long length;
345 /* Don't trust memcpy() of 0 chars. */
349 memcpy (*charPP, fromP, length);
353 #ifndef BFD_ASSEMBLER
354 int section_alignment[SEG_MAXIMUM_ORDINAL];
357 /* This routine records the largest alignment seen for each segment.
358 If the beginning of the segment is aligned on the worst-case
359 boundary, all of the other alignments within it will work. At
360 least one object format really uses this info. */
363 record_alignment (seg, align)
364 /* Segment to which alignment pertains. */
366 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
370 if (seg == absolute_section)
373 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
374 bfd_set_section_alignment (stdoutput, seg, align);
376 if (align > section_alignment[(int) seg])
377 section_alignment[(int) seg] = align;
382 get_recorded_alignment (seg)
385 if (seg == absolute_section)
388 return bfd_get_section_alignment (stdoutput, seg);
390 return section_alignment[(int) seg];
396 /* Reset the section indices after removing the gas created sections. */
399 renumber_sections (abfd, sec, countparg)
400 bfd *abfd ATTRIBUTE_UNUSED;
404 int *countp = (int *) countparg;
406 sec->index = *countp;
410 #endif /* defined (BFD_ASSEMBLER) */
412 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
415 chain_frchains_together_1 (section, frchp)
417 struct frchain *frchp;
419 fragS dummy, *prev_frag = &dummy;
421 fixS fix_dummy, *prev_fix = &fix_dummy;
424 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
426 prev_frag->fr_next = frchp->frch_root;
427 prev_frag = frchp->frch_last;
428 assert (prev_frag->fr_type != 0);
430 if (frchp->fix_root != (fixS *) NULL)
432 if (seg_info (section)->fix_root == (fixS *) NULL)
433 seg_info (section)->fix_root = frchp->fix_root;
434 prev_fix->fx_next = frchp->fix_root;
435 seg_info (section)->fix_tail = frchp->fix_tail;
436 prev_fix = frchp->fix_tail;
440 assert (prev_frag->fr_type != 0);
441 prev_frag->fr_next = 0;
450 chain_frchains_together (abfd, section, xxx)
451 bfd *abfd ATTRIBUTE_UNUSED;
453 PTR xxx ATTRIBUTE_UNUSED;
455 segment_info_type *info;
457 /* BFD may have introduced its own sections without using
458 subseg_new, so it is possible that seg_info is NULL. */
459 info = seg_info (section);
460 if (info != (segment_info_type *) NULL)
461 info->frchainP->frch_last
462 = chain_frchains_together_1 (section, info->frchainP);
464 /* Now that we've chained the frags together, we must add new fixups
465 to the segment, not to the frag chain. */
471 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
474 remove_subsegs (head, seg, root, last)
480 *root = head->frch_root;
481 *last = chain_frchains_together_1 (seg, head);
486 #if defined (BFD_ASSEMBLER) || !defined (BFD)
490 cvt_frag_to_fill (sec, fragP)
491 segT sec ATTRIBUTE_UNUSED;
495 cvt_frag_to_fill (headersP, sec, fragP)
496 object_headers *headersP;
501 switch (fragP->fr_type)
509 HANDLE_ALIGN (fragP);
511 know (fragP->fr_next != NULL);
512 fragP->fr_offset = (fragP->fr_next->fr_address
514 - fragP->fr_fix) / fragP->fr_var;
515 if (fragP->fr_offset < 0)
517 as_bad_where (fragP->fr_file, fragP->fr_line,
518 _("attempt to .org/.space backwards? (%ld)"),
519 (long) fragP->fr_offset);
521 fragP->fr_type = rs_fill;
529 valueT value = S_GET_VALUE (fragP->fr_symbol);
532 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
535 fragP->fr_fix += size;
536 fragP->fr_type = rs_fill;
538 fragP->fr_offset = 0;
539 fragP->fr_symbol = NULL;
544 eh_frame_convert_frag (fragP);
548 dwarf2dbg_convert_frag (fragP);
551 case rs_machine_dependent:
553 md_convert_frag (stdoutput, sec, fragP);
555 md_convert_frag (headersP, sec, fragP);
558 assert (fragP->fr_next == NULL
559 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
562 /* After md_convert_frag, we make the frag into a ".space 0".
563 md_convert_frag() should set up any fixSs and constants
568 #ifndef WORKING_DOT_WORD
571 struct broken_word *lie;
573 if (fragP->fr_subtype)
575 fragP->fr_fix += md_short_jump_size;
576 for (lie = (struct broken_word *) (fragP->fr_symbol);
577 lie && lie->dispfrag == fragP;
578 lie = lie->next_broken_word)
580 fragP->fr_fix += md_long_jump_size;
588 BAD_CASE (fragP->fr_type);
593 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
597 relax_and_size_seg (abfd, sec, xxx)
600 PTR xxx ATTRIBUTE_UNUSED;
604 segment_info_type *seginfo;
606 valueT size, newsize;
608 subseg_change (sec, 0);
610 flags = bfd_get_section_flags (abfd, sec);
612 seginfo = seg_info (sec);
613 if (seginfo && seginfo->frchainP)
615 relax_segment (seginfo->frchainP->frch_root, sec);
616 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
617 cvt_frag_to_fill (sec, fragp);
618 for (fragp = seginfo->frchainP->frch_root;
620 fragp = fragp->fr_next)
621 /* Walk to last elt. */
623 size = fragp->fr_address + fragp->fr_fix;
628 if (size > 0 && ! seginfo->bss)
629 flags |= SEC_HAS_CONTENTS;
631 /* @@ This is just an approximation. */
632 if (seginfo && seginfo->fix_root)
636 x = bfd_set_section_flags (abfd, sec, flags);
639 newsize = md_section_align (sec, size);
640 x = bfd_set_section_size (abfd, sec, newsize);
643 /* If the size had to be rounded up, add some padding in the last
645 assert (newsize >= size);
648 fragS *last = seginfo->frchainP->frch_last;
649 fragp = seginfo->frchainP->frch_root;
650 while (fragp->fr_next != last)
651 fragp = fragp->fr_next;
652 last->fr_address = size;
653 fragp->fr_offset += newsize - size;
656 #ifdef tc_frob_section
657 tc_frob_section (sec);
659 #ifdef obj_frob_section
660 obj_frob_section (sec);
666 dump_section_relocs (abfd, sec, stream_)
667 bfd *abfd ATTRIBUTE_UNUSED;
671 FILE *stream = (FILE *) stream_;
672 segment_info_type *seginfo = seg_info (sec);
673 fixS *fixp = seginfo->fix_root;
678 fprintf (stream, "sec %s relocs:\n", sec->name);
681 symbolS *s = fixp->fx_addsy;
683 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
684 (int) fixp->fx_r_type);
686 fprintf (stream, "no sym\n");
689 print_symbol_value_1 (stream, s);
690 fprintf (stream, "\n");
692 fixp = fixp->fx_next;
696 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
699 #ifndef EMIT_SECTION_SYMBOLS
700 #define EMIT_SECTION_SYMBOLS 1
704 adjust_reloc_syms (abfd, sec, xxx)
705 bfd *abfd ATTRIBUTE_UNUSED;
707 PTR xxx ATTRIBUTE_UNUSED;
709 segment_info_type *seginfo = seg_info (sec);
715 dump_section_relocs (abfd, sec, stderr);
717 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
721 else if (fixp->fx_addsy)
727 fprintf (stderr, "\n\nadjusting fixup:\n");
731 sym = fixp->fx_addsy;
733 /* All symbols should have already been resolved at this
734 point. It is possible to see unresolved expression
735 symbols, though, since they are not in the regular symbol
738 resolve_symbol_value (sym, 1);
740 if (fixp->fx_subsy != NULL)
741 resolve_symbol_value (fixp->fx_subsy, 1);
743 /* If this symbol is equated to an undefined symbol, convert
744 the fixup to being against that symbol. */
745 if (sym != NULL && symbol_equated_p (sym)
746 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
748 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
749 sym = symbol_get_value_expression (sym)->X_add_symbol;
750 fixp->fx_addsy = sym;
753 if (sym != NULL && symbol_mri_common_p (sym))
755 /* These symbols are handled specially in fixup_segment. */
759 symsec = S_GET_SEGMENT (sym);
764 if (bfd_is_abs_section (symsec))
766 /* The fixup_segment routine will not use this symbol in a
767 relocation unless TC_FORCE_RELOCATION returns 1. */
768 if (TC_FORCE_RELOCATION (fixp))
770 symbol_mark_used_in_reloc (fixp->fx_addsy);
771 #ifdef UNDEFINED_DIFFERENCE_OK
772 if (fixp->fx_subsy != NULL)
773 symbol_mark_used_in_reloc (fixp->fx_subsy);
779 /* If it's one of these sections, assume the symbol is
780 definitely going to be output. The code in
781 md_estimate_size_before_relax in tc-mips.c uses this test
782 as well, so if you change this code you should look at that
784 if (bfd_is_und_section (symsec)
785 || bfd_is_com_section (symsec))
787 symbol_mark_used_in_reloc (fixp->fx_addsy);
788 #ifdef UNDEFINED_DIFFERENCE_OK
789 /* We have the difference of an undefined symbol and some
790 other symbol. Make sure to mark the other symbol as used
791 in a relocation so that it will always be output. */
793 symbol_mark_used_in_reloc (fixp->fx_subsy);
798 /* Don't try to reduce relocs which refer to non-local symbols
799 in .linkonce sections. It can lead to confusion when a
800 debugging section refers to a .linkonce section. I hope
801 this will always be correct. */
802 if (symsec != sec && ! S_IS_LOCAL (sym))
808 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
813 /* The GNU toolchain uses an extension for ELF: a section
814 beginning with the magic string .gnu.linkonce is a
816 if (strncmp (segment_name (symsec), ".gnu.linkonce",
817 sizeof ".gnu.linkonce" - 1) == 0)
823 symbol_mark_used_in_reloc (fixp->fx_addsy);
824 #ifdef UNDEFINED_DIFFERENCE_OK
825 if (fixp->fx_subsy != NULL)
826 symbol_mark_used_in_reloc (fixp->fx_subsy);
832 /* Since we're reducing to section symbols, don't attempt to reduce
833 anything that's already using one. */
834 if (symbol_section_p (sym))
836 symbol_mark_used_in_reloc (fixp->fx_addsy);
841 /* We can never adjust a reloc against a weak symbol. If we
842 did, and the weak symbol was overridden by a real symbol
843 somewhere else, then our relocation would be pointing at
844 the wrong area of memory. */
847 symbol_mark_used_in_reloc (fixp->fx_addsy);
852 /* Is there some other reason we can't adjust this one? (E.g.,
853 call/bal links in i960-bout symbols.) */
854 #ifdef obj_fix_adjustable
855 if (! obj_fix_adjustable (fixp))
857 symbol_mark_used_in_reloc (fixp->fx_addsy);
862 /* Is there some other (target cpu dependent) reason we can't adjust
863 this one? (E.g. relocations involving function addresses on
865 #ifdef tc_fix_adjustable
866 if (! tc_fix_adjustable (fixp))
868 symbol_mark_used_in_reloc (fixp->fx_addsy);
873 /* If the section symbol isn't going to be output, the relocs
874 at least should still work. If not, figure out what to do
875 when we run into that case.
877 We refetch the segment when calling section_symbol, rather
878 than using symsec, because S_GET_VALUE may wind up changing
879 the section when it calls resolve_symbol_value. */
880 fixp->fx_offset += S_GET_VALUE (sym);
881 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
882 symbol_mark_used_in_reloc (fixp->fx_addsy);
884 fprintf (stderr, "\nadjusted fixup:\n");
891 #if 1 /* def RELOC_REQUIRES_SYMBOL */
894 /* There was no symbol required by this relocation. However,
895 BFD doesn't really handle relocations without symbols well.
896 (At least, the COFF support doesn't.) So for now we fake up
897 a local symbol in the absolute section. */
899 fixp->fx_addsy = section_symbol (absolute_section);
901 fixp->fx_addsy->sy_used_in_reloc = 1;
906 dump_section_relocs (abfd, sec, stderr);
910 write_relocs (abfd, sec, xxx)
913 PTR xxx ATTRIBUTE_UNUSED;
915 segment_info_type *seginfo = seg_info (sec);
922 /* If seginfo is NULL, we did not create this section; don't do
927 fixup_segment (seginfo->fix_root, sec);
930 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
933 #ifndef RELOC_EXPANSION_POSSIBLE
934 /* Set up reloc information as well. */
935 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
936 memset ((char *) relocs, 0, n * sizeof (arelent *));
939 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
942 bfd_reloc_status_type s;
951 /* If this is an undefined symbol which was equated to another
952 symbol, then use generate the reloc against the latter symbol
953 rather than the former. */
954 sym = fixp->fx_addsy;
955 while (symbol_equated_p (sym)
956 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
960 /* We must avoid looping, as that can occur with a badly
962 n = symbol_get_value_expression (sym)->X_add_symbol;
965 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
968 fixp->fx_addsy = sym;
970 reloc = tc_gen_reloc (sec, fixp);
978 /* This test is triggered inappropriately for the SH. */
979 if (fixp->fx_where + fixp->fx_size
980 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
984 s = bfd_install_relocation (stdoutput, reloc,
985 fixp->fx_frag->fr_literal,
986 fixp->fx_frag->fr_address,
992 case bfd_reloc_overflow:
993 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
995 case bfd_reloc_outofrange:
996 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation out of range"));
999 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1000 fixp->fx_file, fixp->fx_line, s);
1002 relocs[i++] = reloc;
1005 n = n * MAX_RELOC_EXPANSION;
1006 /* Set up reloc information as well. */
1007 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
1010 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1014 bfd_reloc_status_type s;
1024 /* If this is an undefined symbol which was equated to another
1025 symbol, then generate the reloc against the latter symbol
1026 rather than the former. */
1027 sym = fixp->fx_addsy;
1028 while (symbol_equated_p (sym)
1029 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
1030 sym = symbol_get_value_expression (sym)->X_add_symbol;
1031 fixp->fx_addsy = sym;
1033 reloc = tc_gen_reloc (sec, fixp);
1035 for (j = 0; reloc[j]; j++)
1037 relocs[i++] = reloc[j];
1040 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1041 if (fixp->fx_where + fixp->fx_size
1042 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1043 as_bad_where (fixp->fx_file, fixp->fx_line,
1044 _("internal error: fixup not contained within frag"));
1045 for (j = 0; reloc[j]; j++)
1047 s = bfd_install_relocation (stdoutput, reloc[j],
1048 fixp->fx_frag->fr_literal,
1049 fixp->fx_frag->fr_address,
1055 case bfd_reloc_overflow:
1056 as_bad_where (fixp->fx_file, fixp->fx_line,
1057 _("relocation overflow"));
1060 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1061 fixp->fx_file, fixp->fx_line);
1072 sympp = bfd_get_outsymbols (stdoutput);
1073 nsyms = bfd_get_symcount (stdoutput);
1074 for (i = 0; i < n; i++)
1075 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1077 for (j = 0; j < nsyms; j++)
1078 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1087 bfd_set_reloc (stdoutput, sec, relocs, n);
1089 bfd_set_section_flags (abfd, sec,
1090 (bfd_get_section_flags (abfd, sec)
1091 & (flagword) ~SEC_RELOC));
1093 #ifdef SET_SECTION_RELOCS
1094 SET_SECTION_RELOCS (sec, relocs, n);
1102 fprintf (stderr, "relocs for sec %s\n", sec->name);
1103 for (i = 0; i < n; i++)
1106 s = *r->sym_ptr_ptr;
1107 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1108 i, r, r->address, s->name, r->addend);
1115 write_contents (abfd, sec, xxx)
1116 bfd *abfd ATTRIBUTE_UNUSED;
1118 PTR xxx ATTRIBUTE_UNUSED;
1120 segment_info_type *seginfo = seg_info (sec);
1121 unsigned long offset = 0;
1124 /* Write out the frags. */
1126 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1129 for (f = seginfo->frchainP->frch_root;
1134 unsigned long fill_size;
1138 assert (f->fr_type == rs_fill);
1141 x = bfd_set_section_contents (stdoutput, sec,
1142 f->fr_literal, (file_ptr) offset,
1143 (bfd_size_type) f->fr_fix);
1146 bfd_perror (stdoutput->filename);
1147 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1148 exit (EXIT_FAILURE);
1150 offset += f->fr_fix;
1152 fill_literal = f->fr_literal + f->fr_fix;
1153 fill_size = f->fr_var;
1154 count = f->fr_offset;
1155 assert (count >= 0);
1156 if (fill_size && count)
1159 if (fill_size > sizeof (buf))
1161 /* Do it the old way. Can this ever happen? */
1164 x = bfd_set_section_contents (stdoutput, sec,
1167 (bfd_size_type) fill_size);
1170 bfd_perror (stdoutput->filename);
1171 as_perror (_("FATAL: Can't write %s"),
1172 stdoutput->filename);
1173 exit (EXIT_FAILURE);
1175 offset += fill_size;
1180 /* Build a buffer full of fill objects and output it as
1181 often as necessary. This saves on the overhead of
1182 potentially lots of bfd_set_section_contents calls. */
1186 n_per_buf = sizeof (buf);
1187 memset (buf, *fill_literal, n_per_buf);
1192 n_per_buf = sizeof (buf) / fill_size;
1193 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1194 memcpy (bufp, fill_literal, fill_size);
1196 for (; count > 0; count -= n_per_buf)
1198 n_per_buf = n_per_buf > count ? count : n_per_buf;
1199 x = bfd_set_section_contents
1200 (stdoutput, sec, buf, (file_ptr) offset,
1201 (bfd_size_type) n_per_buf * fill_size);
1203 as_fatal (_("Cannot write to output file."));
1204 offset += n_per_buf * fill_size;
1212 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1214 merge_data_into_text ()
1216 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1217 seg_info (text_section)->frchainP->frch_last->fr_next =
1218 seg_info (data_section)->frchainP->frch_root;
1219 seg_info (text_section)->frchainP->frch_last =
1220 seg_info (data_section)->frchainP->frch_last;
1221 seg_info (data_section)->frchainP = 0;
1225 text_last_frag->fr_next = data_frag_root;
1226 text_last_frag = data_last_frag;
1227 data_last_frag = NULL;
1228 data_frag_root = NULL;
1231 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1232 tmp->fx_next = data_fix_root;
1233 text_fix_tail = data_fix_tail;
1236 text_fix_root = data_fix_root;
1237 data_fix_root = NULL;
1240 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1242 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1244 relax_and_size_all_segments ()
1248 relax_segment (text_frag_root, SEG_TEXT);
1249 relax_segment (data_frag_root, SEG_DATA);
1250 relax_segment (bss_frag_root, SEG_BSS);
1252 /* Now the addresses of frags are correct within the segment. */
1253 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1254 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1255 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1257 /* Join the 2 segments into 1 huge segment.
1258 To do this, re-compute every rn_address in the SEG_DATA frags.
1259 Then join the data frags after the text frags.
1261 Determine a_data [length of data segment]. */
1264 register relax_addressT slide;
1266 know ((text_last_frag->fr_type == rs_fill)
1267 && (text_last_frag->fr_offset == 0));
1269 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1270 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1271 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1273 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1274 /* For b.out: If the data section has a strict alignment
1275 requirement, its load address in the .o file will be
1276 rounded up from the size of the text section. These
1277 two values are *not* the same! Similarly for the bss
1279 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1282 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1283 fragP->fr_address += slide;
1285 know (text_last_frag != 0);
1286 text_last_frag->fr_next = data_frag_root;
1290 H_SET_DATA_SIZE (&headers, 0);
1294 /* See above comments on b.out data section address. */
1297 if (data_last_frag == 0)
1298 bss_vma = H_GET_TEXT_SIZE (&headers);
1300 bss_vma = data_last_frag->fr_address;
1301 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1302 bss_address_frag.fr_address = bss_vma;
1304 #else /* ! OBJ_BOUT */
1305 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1306 H_GET_DATA_SIZE (&headers));
1308 #endif /* ! OBJ_BOUT */
1310 /* Slide all the frags. */
1313 relax_addressT slide = bss_address_frag.fr_address;
1315 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1316 fragP->fr_address += slide;
1320 H_SET_BSS_SIZE (&headers,
1321 bss_last_frag->fr_address - bss_frag_root->fr_address);
1323 H_SET_BSS_SIZE (&headers, 0);
1325 #endif /* ! BFD_ASSEMBLER && ! BFD */
1327 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1329 #ifdef BFD_ASSEMBLER
1337 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1339 /* Count symbols. We can't rely on a count made by the loop in
1340 write_object_file, because *_frob_file may add a new symbol or
1343 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1350 asympp = (asymbol **) bfd_alloc (stdoutput,
1351 nsyms * sizeof (asymbol *));
1352 symp = symbol_rootP;
1353 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1355 asympp[i] = symbol_get_bfdsym (symp);
1356 symbol_mark_written (symp);
1361 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1362 assert (result == true);
1363 symbol_table_frozen = 1;
1367 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1369 set_segment_vma (abfd, sec, xxx)
1372 PTR xxx ATTRIBUTE_UNUSED;
1374 static bfd_vma addr = 0;
1376 bfd_set_section_vma (abfd, sec, addr);
1377 addr += bfd_section_size (abfd, sec);
1379 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1381 /* Finish the subsegments. After every sub-segment, we fake an
1382 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1383 ".fill 0" because that is the kind of frag that requires least
1384 thought. ".align" frags like to have a following frag since that
1385 makes calculating their intended length trivial. */
1387 #ifndef SUB_SEGMENT_ALIGN
1388 #ifdef BFD_ASSEMBLER
1389 #define SUB_SEGMENT_ALIGN(SEG) (0)
1391 #define SUB_SEGMENT_ALIGN(SEG) (2)
1398 struct frchain *frchainP;
1400 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1404 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1406 /* This now gets called even if we had errors. In that case,
1407 any alignment is meaningless, and, moreover, will look weird
1408 if we are generating a listing. */
1409 alignment = had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg);
1411 /* The last subsegment gets an aligment corresponding to the
1412 alignment of the section. This allows proper nop-filling
1413 at the end of code-bearing sections. */
1414 if (!frchainP->frch_next || frchainP->frch_next->frch_seg != now_seg)
1415 alignment = get_recorded_alignment (now_seg);
1417 if (subseg_text_p (now_seg))
1418 frag_align_code (alignment, 0);
1420 frag_align (alignment, 0, 0);
1422 /* frag_align will have left a new frag.
1423 Use this last frag for an empty ".fill".
1425 For this segment ...
1426 Create a last frag. Do not leave a "being filled in frag". */
1427 frag_wane (frag_now);
1428 frag_now->fr_fix = 0;
1429 know (frag_now->fr_next == NULL);
1433 /* Write the object file. */
1436 write_object_file ()
1438 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1439 fragS *fragP; /* Track along all frags. */
1442 /* Do we really want to write it? */
1444 int n_warns, n_errs;
1445 n_warns = had_warnings ();
1446 n_errs = had_errors ();
1447 /* The -Z flag indicates that an object file should be generated,
1448 regardless of warnings and errors. */
1449 if (flag_always_generate_output)
1451 if (n_warns || n_errs)
1452 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1453 n_errs, n_errs == 1 ? "" : "s",
1454 n_warns, n_warns == 1 ? "" : "s");
1459 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1460 n_errs, n_errs == 1 ? "" : "s",
1461 n_warns, n_warns == 1 ? "" : "s");
1466 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1467 a routine to check for the definition of the procedure "_main",
1468 and if so -- fix it up so that it can be program entry point. */
1469 vms_check_for_main ();
1470 #endif /* OBJ_VMS */
1472 /* From now on, we don't care about sub-segments. Build one frag chain
1473 for each segment. Linked thru fr_next. */
1475 #ifdef BFD_ASSEMBLER
1476 /* Remove the sections created by gas for its own purposes. */
1478 asection **seclist, *sec;
1481 seclist = &stdoutput->sections;
1482 while (seclist && *seclist)
1485 while (sec == reg_section || sec == expr_section)
1489 stdoutput->section_count--;
1494 seclist = &(*seclist)->next;
1497 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1500 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1502 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1503 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1504 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1507 /* We have two segments. If user gave -R flag, then we must put the
1508 data frags into the text segment. Do this before relaxing so
1509 we know to take advantage of -R and make shorter addresses. */
1510 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1511 if (flag_readonly_data_in_text)
1513 merge_data_into_text ();
1517 #ifdef BFD_ASSEMBLER
1518 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1520 relax_and_size_all_segments ();
1521 #endif /* BFD_ASSEMBLER */
1523 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1524 /* Now that the segments have their final sizes, run through the
1525 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1526 should too. Currently, only DJGPP uses this code, but other
1527 COFF targets may need to execute this too. */
1528 bfd_map_over_sections (stdoutput, set_segment_vma, (char *) 0);
1531 #ifndef BFD_ASSEMBLER
1532 /* Crawl the symbol chain.
1534 For each symbol whose value depends on a frag, take the address of
1535 that frag and subsume it into the value of the symbol.
1536 After this, there is just one way to lookup a symbol value.
1537 Values are left in their final state for object file emission.
1538 We adjust the values of 'L' local symbols, even if we do
1539 not intend to emit them to the object file, because their values
1540 are needed for fix-ups.
1542 Unless we saw a -L flag, remove all symbols that begin with 'L'
1543 from the symbol chain. (They are still pointed to by the fixes.)
1545 Count the remaining symbols.
1546 Assign a symbol number to each symbol.
1547 Count the number of string-table chars we will emit.
1548 Put this info into the headers as appropriate. */
1549 know (zero_address_frag.fr_address == 0);
1550 string_byte_count = sizeof (string_byte_count);
1552 obj_crawl_symbol_chain (&headers);
1554 if (string_byte_count == sizeof (string_byte_count))
1555 string_byte_count = 0;
1557 H_SET_STRING_SIZE (&headers, string_byte_count);
1559 /* Addresses of frags now reflect addresses we use in the object file.
1560 Symbol values are correct.
1561 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1562 Also converting any machine-dependent frags using md_convert_frag(); */
1563 subseg_change (SEG_TEXT, 0);
1565 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1567 /* At this point we have linked all the frags into a single
1568 chain. However, cvt_frag_to_fill may call md_convert_frag
1569 which may call fix_new. We need to ensure that fix_new adds
1570 the fixup to the right section. */
1571 if (fragP == data_frag_root)
1572 subseg_change (SEG_DATA, 0);
1574 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1576 /* Some assert macros don't work with # directives mixed in. */
1578 if (!(fragP->fr_next == NULL
1580 || fragP->fr_next == data_frag_root
1582 || ((fragP->fr_next->fr_address - fragP->fr_address)
1583 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1587 #endif /* ! BFD_ASSEMBLER */
1589 #ifndef WORKING_DOT_WORD
1591 struct broken_word *lie;
1592 struct broken_word **prevP;
1594 prevP = &broken_words;
1595 for (lie = broken_words; lie; lie = lie->next_broken_word)
1600 subseg_change (lie->seg, lie->subseg);
1601 exp.X_op = O_subtract;
1602 exp.X_add_symbol = lie->add;
1603 exp.X_op_symbol = lie->sub;
1604 exp.X_add_number = lie->addnum;
1605 #ifdef BFD_ASSEMBLER
1606 #ifdef TC_CONS_FIX_NEW
1607 TC_CONS_FIX_NEW (lie->frag,
1608 lie->word_goes_here - lie->frag->fr_literal,
1611 fix_new_exp (lie->frag,
1612 lie->word_goes_here - lie->frag->fr_literal,
1613 2, &exp, 0, BFD_RELOC_16);
1616 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1617 fix_new_exp (lie->frag,
1618 lie->word_goes_here - lie->frag->fr_literal,
1619 2, &exp, 0, NO_RELOC);
1622 fix_new_ns32k_exp (lie->frag,
1623 lie->word_goes_here - lie->frag->fr_literal,
1624 2, &exp, 0, 0, 2, 0, 0);
1626 fix_new_exp (lie->frag,
1627 lie->word_goes_here - lie->frag->fr_literal,
1629 #endif /* TC_NS32K */
1630 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1631 #endif /* BFD_ASSEMBLER */
1632 *prevP = lie->next_broken_word;
1635 prevP = &(lie->next_broken_word);
1637 for (lie = broken_words; lie;)
1639 struct broken_word *untruth;
1641 addressT table_addr;
1642 addressT from_addr, to_addr;
1645 subseg_change (lie->seg, lie->subseg);
1646 fragP = lie->dispfrag;
1648 /* Find out how many broken_words go here. */
1651 untruth && untruth->dispfrag == fragP;
1652 untruth = untruth->next_broken_word)
1653 if (untruth->added == 1)
1656 table_ptr = lie->dispfrag->fr_opcode;
1657 table_addr = (lie->dispfrag->fr_address
1658 + (table_ptr - lie->dispfrag->fr_literal));
1659 /* Create the jump around the long jumps. This is a short
1660 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1661 from_addr = table_addr;
1662 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1663 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1665 table_ptr += md_short_jump_size;
1666 table_addr += md_short_jump_size;
1669 lie && lie->dispfrag == fragP;
1670 m++, lie = lie->next_broken_word)
1672 if (lie->added == 2)
1674 /* Patch the jump table. */
1675 /* This is the offset from ??? to table_ptr+0. */
1676 to_addr = table_addr - S_GET_VALUE (lie->sub);
1677 #ifdef BFD_ASSEMBLER
1678 to_addr -= symbol_get_frag (lie->sub)->fr_address;
1680 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1681 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1683 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1684 for (untruth = lie->next_broken_word;
1685 untruth && untruth->dispfrag == fragP;
1686 untruth = untruth->next_broken_word)
1688 if (untruth->use_jump == lie)
1689 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1692 /* Install the long jump. */
1693 /* This is a long jump from table_ptr+0 to the final target. */
1694 from_addr = table_addr;
1695 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1696 #ifdef BFD_ASSEMBLER
1697 to_addr += symbol_get_frag (lie->add)->fr_address;
1699 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1701 table_ptr += md_long_jump_size;
1702 table_addr += md_long_jump_size;
1706 #endif /* not WORKING_DOT_WORD */
1708 #ifndef BFD_ASSEMBLER
1711 char *the_object_file;
1712 long object_file_size;
1713 /* Scan every FixS performing fixups. We had to wait until now to
1714 do this because md_convert_frag() may have made some fixSs. */
1717 subseg_change (SEG_TEXT, 0);
1718 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1719 subseg_change (SEG_DATA, 0);
1720 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1721 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1723 /* FIXME: Move this stuff into the pre-write-hook. */
1724 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1725 H_SET_ENTRY_POINT (&headers, 0);
1727 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1729 object_file_size = H_GET_FILE_SIZE (&headers);
1730 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1732 output_file_create (out_file_name);
1734 obj_header_append (&next_object_file_charP, &headers);
1736 know ((next_object_file_charP - the_object_file)
1737 == H_GET_HEADER_SIZE (&headers));
1740 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1742 register long count;
1743 register char *fill_literal;
1744 register long fill_size;
1747 know (fragP->fr_type == rs_fill);
1748 append (&next_object_file_charP, fragP->fr_literal,
1749 (unsigned long) fragP->fr_fix);
1750 fill_literal = fragP->fr_literal + fragP->fr_fix;
1751 fill_size = fragP->fr_var;
1752 know (fragP->fr_offset >= 0);
1754 for (count = fragP->fr_offset; count; count--)
1755 append (&next_object_file_charP, fill_literal,
1756 (unsigned long) fill_size);
1759 know ((next_object_file_charP - the_object_file)
1760 == (H_GET_HEADER_SIZE (&headers)
1761 + H_GET_TEXT_SIZE (&headers)
1762 + H_GET_DATA_SIZE (&headers)));
1764 /* Emit relocations. */
1765 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1766 (relax_addressT) 0);
1767 know ((next_object_file_charP - the_object_file)
1768 == (H_GET_HEADER_SIZE (&headers)
1769 + H_GET_TEXT_SIZE (&headers)
1770 + H_GET_DATA_SIZE (&headers)
1771 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1773 /* Make addresses in data relocation directives relative to beginning of
1774 first data fragment, not end of last text fragment: alignment of the
1775 start of the data segment may place a gap between the segments. */
1776 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1777 data0_frchainP->frch_root->fr_address);
1779 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1780 text_last_frag->fr_address);
1781 #endif /* TC_I960 */
1783 know ((next_object_file_charP - the_object_file)
1784 == (H_GET_HEADER_SIZE (&headers)
1785 + H_GET_TEXT_SIZE (&headers)
1786 + H_GET_DATA_SIZE (&headers)
1787 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1788 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1790 /* Emit line number entries. */
1791 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1792 know ((next_object_file_charP - the_object_file)
1793 == (H_GET_HEADER_SIZE (&headers)
1794 + H_GET_TEXT_SIZE (&headers)
1795 + H_GET_DATA_SIZE (&headers)
1796 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1797 + H_GET_DATA_RELOCATION_SIZE (&headers)
1798 + H_GET_LINENO_SIZE (&headers)));
1801 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1802 know ((next_object_file_charP - the_object_file)
1803 == (H_GET_HEADER_SIZE (&headers)
1804 + H_GET_TEXT_SIZE (&headers)
1805 + H_GET_DATA_SIZE (&headers)
1806 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1807 + H_GET_DATA_RELOCATION_SIZE (&headers)
1808 + H_GET_LINENO_SIZE (&headers)
1809 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1812 if (string_byte_count > 0)
1813 obj_emit_strings (&next_object_file_charP);
1816 bfd_seek (stdoutput, 0, 0);
1817 bfd_write (the_object_file, 1, object_file_size, stdoutput);
1820 /* Write the data to the file. */
1821 output_file_append (the_object_file, object_file_size, out_file_name);
1822 free (the_object_file);
1826 /* Now do the VMS-dependent part of writing the object file. */
1827 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1828 H_GET_DATA_SIZE (&headers),
1829 H_GET_BSS_SIZE (&headers),
1830 text_frag_root, data_frag_root);
1831 #endif /* OBJ_VMS */
1832 #else /* BFD_ASSEMBLER */
1834 /* Resolve symbol values. This needs to be done before processing
1840 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1841 resolve_symbol_value (symp, 1);
1843 resolve_local_symbol_values ();
1847 #ifdef tc_frob_file_before_adjust
1848 tc_frob_file_before_adjust ();
1850 #ifdef obj_frob_file_before_adjust
1851 obj_frob_file_before_adjust ();
1854 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1856 /* Set up symbol table, and write it out. */
1861 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1866 if (symbol_mri_common_p (symp))
1868 if (S_IS_EXTERNAL (symp))
1869 as_bad (_("%s: global symbols not supported in common sections"),
1871 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1875 name = S_GET_NAME (symp);
1879 decode_local_label_name ((char *) S_GET_NAME (symp));
1880 /* They only differ if `name' is a fb or dollar local
1882 if (name2 != name && ! S_IS_DEFINED (symp))
1883 as_bad (_("local label %s is not defined"), name2);
1886 /* Do it again, because adjust_reloc_syms might introduce
1887 more symbols. They'll probably only be section symbols,
1888 but they'll still need to have the values computed. */
1889 resolve_symbol_value (symp, 1);
1891 /* Skip symbols which were equated to undefined or common
1893 if (symbol_equated_p (symp)
1894 && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
1896 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1900 /* So far, common symbols have been treated like undefined symbols.
1901 Put them in the common section now. */
1902 if (S_IS_DEFINED (symp) == 0
1903 && S_GET_VALUE (symp) != 0)
1904 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1906 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1907 S_GET_NAME (symp), symp,
1909 symbol_get_bfdsym (symp)->flags,
1910 segment_name (S_GET_SEGMENT (symp)));
1913 #ifdef obj_frob_symbol
1914 obj_frob_symbol (symp, punt);
1916 #ifdef tc_frob_symbol
1917 if (! punt || symbol_used_in_reloc_p (symp))
1918 tc_frob_symbol (symp, punt);
1921 /* If we don't want to keep this symbol, splice it out of
1922 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1923 want section symbols. Otherwise, we skip local symbols
1924 and symbols that the frob_symbol macros told us to punt,
1925 but we keep such symbols if they are used in relocs. */
1926 if ((! EMIT_SECTION_SYMBOLS
1927 && symbol_section_p (symp))
1928 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1929 opposites. Sometimes the former checks flags and the
1930 latter examines the name... */
1931 || (!S_IS_EXTERN (symp)
1932 && (S_IS_LOCAL (symp) || punt)
1933 && ! symbol_used_in_reloc_p (symp)))
1935 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1937 /* After symbol_remove, symbol_next(symp) still returns
1938 the one that came after it in the chain. So we don't
1939 need to do any extra cleanup work here. */
1943 /* Make sure we really got a value for the symbol. */
1944 if (! symbol_resolved_p (symp))
1946 as_bad (_("can't resolve value for symbol \"%s\""),
1948 symbol_mark_resolved (symp);
1951 /* Set the value into the BFD symbol. Up til now the value
1952 has only been kept in the gas symbolS struct. */
1953 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1959 /* Now do any format-specific adjustments to the symbol table, such
1960 as adding file symbols. */
1961 #ifdef tc_adjust_symtab
1962 tc_adjust_symtab ();
1964 #ifdef obj_adjust_symtab
1965 obj_adjust_symtab ();
1968 /* Now that all the sizes are known, and contents correct, we can
1969 start writing to the file. */
1972 /* If *_frob_file changes the symbol value at this point, it is
1973 responsible for moving the changed value into symp->bsym->value
1974 as well. Hopefully all symbol value changing can be done in
1979 #ifdef obj_frob_file
1983 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1985 #ifdef tc_frob_file_after_relocs
1986 tc_frob_file_after_relocs ();
1988 #ifdef obj_frob_file_after_relocs
1989 obj_frob_file_after_relocs ();
1992 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1993 #endif /* BFD_ASSEMBLER */
1997 #ifdef TC_GENERIC_RELAX_TABLE
1999 static int is_dnrange PARAMS ((fragS *, fragS *));
2001 /* Subroutines of relax_segment. */
2010 f2addr = f2->fr_address;
2011 for (; f1; f1 = f1->fr_next)
2013 if (f1->fr_next == f2)
2015 if (f1->fr_address > f2addr)
2021 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2024 relax_frag (segment, fragP, stretch)
2029 const relax_typeS *this_type;
2030 const relax_typeS *start_type;
2031 relax_substateT next_state;
2032 relax_substateT this_state;
2033 long aim, target, growth;
2034 symbolS *symbolP = fragP->fr_symbol;
2035 long offset = fragP->fr_offset;
2036 /* Recompute was_address by undoing "+= stretch" done by relax_segment. */
2037 unsigned long was_address = fragP->fr_address - stretch;
2038 unsigned long address = fragP->fr_address;
2039 const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
2041 this_state = fragP->fr_subtype;
2042 start_type = this_type = table + this_state;
2049 sym_frag = symbol_get_frag (symbolP);
2051 #ifndef DIFF_EXPR_OK
2052 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2053 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2054 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2055 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2056 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2058 know (sym_frag != NULL);
2060 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2061 || sym_frag == &zero_address_frag);
2062 target += S_GET_VALUE (symbolP) + sym_frag->fr_address;
2064 /* If frag has yet to be reached on this pass,
2065 assume it will move by STRETCH just as we did.
2066 If this is not so, it will be because some frag
2067 between grows, and that will force another pass.
2069 Beware zero-length frags. */
2072 && S_GET_SEGMENT (symbolP) == segment
2073 && (sym_frag->fr_address > was_address
2074 || (sym_frag->fr_address == was_address
2075 && is_dnrange (fragP, sym_frag))))
2081 aim = target - address - fragP->fr_fix;
2082 #ifdef TC_PCREL_ADJUST
2083 /* Currently only the ns32k family needs this. */
2084 aim += TC_PCREL_ADJUST (fragP);
2086 /* This machine doesn't want to use pcrel_adjust.
2087 In that case, pcrel_adjust should be zero. */
2089 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2092 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2093 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2098 /* Look backwards. */
2099 for (next_state = this_type->rlx_more; next_state;)
2100 if (aim >= this_type->rlx_backward)
2104 /* Grow to next state. */
2105 this_state = next_state;
2106 this_type = table + this_state;
2107 next_state = this_type->rlx_more;
2112 /* Look forwards. */
2113 for (next_state = this_type->rlx_more; next_state;)
2114 if (aim <= this_type->rlx_forward)
2118 /* Grow to next state. */
2119 this_state = next_state;
2120 this_type = table + this_state;
2121 next_state = this_type->rlx_more;
2125 growth = this_type->rlx_length - start_type->rlx_length;
2127 fragP->fr_subtype = this_state;
2131 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2133 /* Relax_align. Advance location counter to next address that has 'alignment'
2134 lowest order bits all 0s, return size of adjustment made. */
2135 static relax_addressT
2136 relax_align (address, alignment)
2137 register relax_addressT address; /* Address now. */
2138 register int alignment; /* Alignment (binary). */
2140 relax_addressT mask;
2141 relax_addressT new_address;
2143 mask = ~((~0) << alignment);
2144 new_address = (address + mask) & (~mask);
2145 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2147 /* We must provide lots of padding, so the linker can discard it
2148 when needed. The linker will not add extra space, ever. */
2149 new_address += (1 << alignment);
2151 return (new_address - address);
2154 /* Now we have a segment, not a crowd of sub-segments, we can make
2159 After this, all frags in this segment have addresses that are correct
2160 within the segment. Since segments live in different file addresses,
2161 these frag addresses may not be the same as final object-file
2165 relax_segment (segment_frag_root, segment)
2166 struct frag *segment_frag_root;
2169 register struct frag *fragP;
2170 register relax_addressT address;
2171 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2172 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2174 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2175 subseg_change (segment, 0);
2177 /* For each frag in segment: count and store (a 1st guess of)
2180 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2182 fragP->fr_address = address;
2183 address += fragP->fr_fix;
2185 switch (fragP->fr_type)
2188 address += fragP->fr_offset * fragP->fr_var;
2195 addressT offset = relax_align (address, (int) fragP->fr_offset);
2197 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2200 if (offset % fragP->fr_var != 0)
2202 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2203 (unsigned long) offset, (long) fragP->fr_var);
2204 offset -= (offset % fragP->fr_var);
2213 /* Assume .org is nugatory. It will grow with 1st relax. */
2216 case rs_machine_dependent:
2217 address += md_estimate_size_before_relax (fragP, segment);
2220 #ifndef WORKING_DOT_WORD
2221 /* Broken words don't concern us yet. */
2222 case rs_broken_word:
2227 /* Initial guess is always 1; doing otherwise can result in
2228 stable solutions that are larger than the minimum. */
2229 address += fragP->fr_offset = 1;
2233 address += eh_frame_estimate_size_before_relax (fragP);
2237 address += dwarf2dbg_estimate_size_before_relax (fragP);
2241 BAD_CASE (fragP->fr_type);
2248 long stretch; /* May be any size, 0 or negative. */
2249 /* Cumulative number of addresses we have relaxed this pass.
2250 We may have relaxed more than one address. */
2251 long stretched; /* Have we stretched on this pass? */
2252 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2253 grew, and another shrank. If a branch instruction doesn't fit anymore,
2254 we could be scrod. */
2258 stretch = stretched = 0;
2260 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2263 addressT was_address;
2267 was_address = fragP->fr_address;
2268 address = fragP->fr_address += stretch;
2269 symbolP = fragP->fr_symbol;
2270 offset = fragP->fr_offset;
2272 switch (fragP->fr_type)
2274 case rs_fill: /* .fill never relaxes. */
2278 #ifndef WORKING_DOT_WORD
2279 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2280 for it I do not want to write it. I do not want to have
2281 anything to do with it. This is not the proper way to
2282 implement this misfeature. */
2283 case rs_broken_word:
2285 struct broken_word *lie;
2286 struct broken_word *untruth;
2288 /* Yes this is ugly (storing the broken_word pointer
2289 in the symbol slot). Still, this whole chunk of
2290 code is ugly, and I don't feel like doing anything
2291 about it. Think of it as stubbornness in action. */
2293 for (lie = (struct broken_word *) (fragP->fr_symbol);
2294 lie && lie->dispfrag == fragP;
2295 lie = lie->next_broken_word)
2301 offset = (symbol_get_frag (lie->add)->fr_address
2302 + S_GET_VALUE (lie->add)
2304 - (symbol_get_frag (lie->sub)->fr_address
2305 + S_GET_VALUE (lie->sub)));
2306 if (offset <= -32768 || offset >= 32767)
2308 if (flag_warn_displacement)
2311 sprint_value (buf, (addressT) lie->addnum);
2312 as_warn (_(".word %s-%s+%s didn't fit"),
2313 S_GET_NAME (lie->add),
2314 S_GET_NAME (lie->sub),
2318 if (fragP->fr_subtype == 0)
2320 fragP->fr_subtype++;
2321 growth += md_short_jump_size;
2323 for (untruth = lie->next_broken_word;
2324 untruth && untruth->dispfrag == lie->dispfrag;
2325 untruth = untruth->next_broken_word)
2326 if ((symbol_get_frag (untruth->add)
2327 == symbol_get_frag (lie->add))
2328 && (S_GET_VALUE (untruth->add)
2329 == S_GET_VALUE (lie->add)))
2332 untruth->use_jump = lie;
2334 growth += md_long_jump_size;
2339 } /* case rs_broken_word */
2345 addressT oldoff, newoff;
2347 oldoff = relax_align (was_address + fragP->fr_fix,
2349 newoff = relax_align (address + fragP->fr_fix,
2352 if (fragP->fr_subtype != 0)
2354 if (oldoff > fragP->fr_subtype)
2356 if (newoff > fragP->fr_subtype)
2360 growth = newoff - oldoff;
2366 long target = offset;
2371 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2372 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2373 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2374 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2375 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2376 know (symbolP->sy_frag);
2377 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2378 || (symbolP->sy_frag == &zero_address_frag));
2380 target += (S_GET_VALUE (symbolP)
2381 + symbol_get_frag (symbolP)->fr_address);
2382 } /* if we have a symbol */
2384 know (fragP->fr_next);
2385 after = fragP->fr_next->fr_address;
2386 growth = target - after;
2389 /* Growth may be negative, but variable part of frag
2390 cannot have fewer than 0 chars. That is, we can't
2392 as_bad_where (fragP->fr_file, fragP->fr_line,
2393 _("attempt to .org backwards ignored"));
2395 /* We've issued an error message. Change the
2396 frag to avoid cascading errors. */
2397 fragP->fr_type = rs_align;
2398 fragP->fr_subtype = 0;
2399 fragP->fr_offset = 0;
2400 fragP->fr_fix = after - address;
2404 /* This is an absolute growth factor */
2412 growth = S_GET_VALUE (symbolP);
2413 if (symbol_get_frag (symbolP) != &zero_address_frag
2414 || S_IS_COMMON (symbolP)
2415 || ! S_IS_DEFINED (symbolP))
2416 as_bad_where (fragP->fr_file, fragP->fr_line,
2417 _(".space specifies non-absolute value"));
2418 fragP->fr_symbol = 0;
2421 as_warn (_(".space or .fill with negative value, ignored"));
2429 case rs_machine_dependent:
2430 #ifdef md_relax_frag
2431 growth = md_relax_frag (segment, fragP, stretch);
2433 #ifdef TC_GENERIC_RELAX_TABLE
2434 /* The default way to relax a frag is to look through
2435 TC_GENERIC_RELAX_TABLE. */
2436 growth = relax_frag (segment, fragP, stretch);
2437 #endif /* TC_GENERIC_RELAX_TABLE */
2446 value = resolve_symbol_value (fragP->fr_symbol, 0);
2447 size = sizeof_leb128 (value, fragP->fr_subtype);
2448 growth = size - fragP->fr_offset;
2449 fragP->fr_offset = size;
2454 growth = eh_frame_relax_frag (fragP);
2458 growth = dwarf2dbg_relax_frag (fragP);
2462 BAD_CASE (fragP->fr_type);
2470 } /* For each frag in the segment. */
2472 while (stretched); /* Until nothing further to relax. */
2475 /* We now have valid fr_address'es for each frag. */
2477 /* All fr_address's are correct, relative to their own segment.
2478 We have made all the fixS we will ever make. */
2481 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2483 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2484 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2489 Go through all the fixS's in a segment and see which ones can be
2490 handled now. (These consist of fixS where we have since discovered
2491 the value of a symbol, or the address of the frag involved.)
2492 For each one, call md_apply_fix to put the fix into the frag data.
2494 Result is a count of how many relocation structs will be needed to
2495 handle the remaining fixS's that we couldn't completely handle here.
2496 These will be output later by emit_relocations(). */
2499 fixup_segment (fixP, this_segment_type)
2500 register fixS *fixP;
2501 segT this_segment_type; /* N_TYPE bits for segment. */
2503 long seg_reloc_count = 0;
2504 symbolS *add_symbolP;
2505 symbolS *sub_symbolP;
2512 segT add_symbol_segment = absolute_section;
2514 /* If the linker is doing the relaxing, we must not do any fixups.
2516 Well, strictly speaking that's not true -- we could do any that are
2517 PC-relative and don't cross regions that could change size. And for the
2518 i960 (the only machine for which we've got a relaxing linker right now),
2519 we might be able to turn callx/callj into bal anyways in cases where we
2520 know the maximum displacement. */
2521 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment_type))
2523 for (; fixP; fixP = fixP->fx_next)
2525 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2526 return seg_reloc_count;
2529 for (; fixP; fixP = fixP->fx_next)
2532 fprintf (stderr, "\nprocessing fixup:\n");
2536 fragP = fixP->fx_frag;
2538 where = fixP->fx_where;
2539 place = fragP->fr_literal + where;
2540 size = fixP->fx_size;
2541 add_symbolP = fixP->fx_addsy;
2542 #ifdef TC_VALIDATE_FIX
2543 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2545 sub_symbolP = fixP->fx_subsy;
2546 add_number = fixP->fx_offset;
2547 pcrel = fixP->fx_pcrel;
2550 if (add_symbolP != NULL
2551 && symbol_mri_common_p (add_symbolP))
2553 know (add_symbolP->sy_value.X_op == O_symbol);
2554 add_number += S_GET_VALUE (add_symbolP);
2555 fixP->fx_offset = add_number;
2556 add_symbolP = fixP->fx_addsy =
2557 symbol_get_value_expression (add_symbolP)->X_add_symbol;
2561 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2565 resolve_symbol_value (sub_symbolP, 1);
2566 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2568 if (add_symbolP != NULL)
2570 add_number += S_GET_VALUE (add_symbolP);
2572 fixP->fx_addsy = NULL;
2575 /* It's just -sym. */
2576 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2578 add_number -= S_GET_VALUE (sub_symbolP);
2579 fixP->fx_subsy = NULL;
2582 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2584 /* Should try converting to a constant. */
2589 as_bad_where (fixP->fx_file, fixP->fx_line,
2590 _("Negative of non-absolute symbol %s"),
2591 S_GET_NAME (sub_symbolP));
2593 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2594 && SEG_NORMAL (add_symbol_segment))
2596 /* Difference of 2 symbols from same segment.
2597 Can't make difference of 2 undefineds: 'value' means
2598 something different for N_UNDF. */
2600 /* Makes no sense to use the difference of 2 arbitrary symbols
2601 as the target of a call instruction. */
2603 as_bad_where (fixP->fx_file, fixP->fx_line,
2604 _("callj to difference of 2 symbols"));
2605 #endif /* TC_I960 */
2606 add_number += S_GET_VALUE (add_symbolP) -
2607 S_GET_VALUE (sub_symbolP);
2610 pcrel = 0; /* No further pcrel processing. */
2612 /* Let the target machine make the final determination
2613 as to whether or not a relocation will be needed to
2614 handle this fixup. */
2615 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2618 fixP->fx_addsy = NULL;
2619 fixP->fx_subsy = NULL;
2624 /* Different segments in subtraction. */
2625 know (!(S_IS_EXTERNAL (sub_symbolP)
2626 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2628 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2629 add_number -= S_GET_VALUE (sub_symbolP);
2632 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2634 /* Do this even if it's already described as
2635 pc-relative. For example, on the m68k, an
2636 operand of "pc@(foo-.-2)" should address
2637 "foo" in a pc-relative mode. */
2642 /* Make it pc-relative. */
2643 add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
2644 - S_GET_VALUE (sub_symbolP));
2651 #ifdef UNDEFINED_DIFFERENCE_OK
2652 /* The PA needs this for PIC code generation. We basically
2653 don't want to do anything if we have the difference of two
2654 symbols at this point. */
2657 /* Leave it alone. */
2660 #ifdef BFD_ASSEMBLER
2661 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2662 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2664 /* Leave it alone. */
2670 sprint_value (buf, fragP->fr_address + where);
2671 as_bad_where (fixP->fx_file, fixP->fx_line,
2672 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2673 S_GET_NAME (add_symbolP),
2674 segment_name (S_GET_SEGMENT (add_symbolP)),
2675 S_GET_NAME (sub_symbolP),
2676 segment_name (S_GET_SEGMENT (sub_symbolP)),
2684 if (add_symbol_segment == this_segment_type && pcrel && !plt
2685 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2687 /* This fixup was made when the symbol's segment was
2688 SEG_UNKNOWN, but it is now in the local segment.
2689 So we know how to do the address without relocation. */
2691 /* reloc_callj() may replace a 'call' with a 'calls' or a
2692 'bal', in which cases it modifies *fixP as appropriate.
2693 In the case of a 'calls', no further work is required,
2694 and *fixP has been set up to make the rest of the code
2697 #endif /* TC_I960 */
2699 add_number += S_GET_VALUE (add_symbolP);
2700 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2701 /* Lie. Don't want further pcrel processing. */
2704 /* Let the target machine make the final determination
2705 as to whether or not a relocation will be needed to
2706 handle this fixup. */
2707 if (!TC_FORCE_RELOCATION (fixP))
2710 fixP->fx_addsy = NULL;
2715 if (add_symbol_segment == absolute_section
2719 /* See comment about reloc_callj() above. */
2721 #endif /* TC_I960 */
2722 add_number += S_GET_VALUE (add_symbolP);
2724 /* Let the target machine make the final determination
2725 as to whether or not a relocation will be needed to
2726 handle this fixup. */
2728 if (!TC_FORCE_RELOCATION (fixP))
2730 fixP->fx_addsy = NULL;
2734 else if (add_symbol_segment == undefined_section
2735 #ifdef BFD_ASSEMBLER
2736 || bfd_is_com_section (add_symbol_segment)
2741 if ((int) fixP->fx_bit_fixP == 13)
2743 /* This is a COBR instruction. They have only a
2744 13-bit displacement and are only to be used
2745 for local branches: flag as error, don't generate
2747 as_bad_where (fixP->fx_file, fixP->fx_line,
2748 _("can't use COBR format with external label"));
2749 fixP->fx_addsy = NULL;
2753 #endif /* TC_I960 */
2757 if (S_IS_COMMON (add_symbolP))
2758 add_number += S_GET_VALUE (add_symbolP);
2759 #endif /* TE_I386AIX */
2760 #endif /* OBJ_COFF */
2766 if (TC_FIX_ADJUSTABLE (fixP))
2767 add_number += S_GET_VALUE (add_symbolP);
2774 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2775 if (add_symbolP == 0)
2777 #ifndef BFD_ASSEMBLER
2778 fixP->fx_addsy = &abs_symbol;
2780 fixP->fx_addsy = section_symbol (absolute_section);
2782 symbol_mark_used_in_reloc (fixP->fx_addsy);
2789 #ifdef MD_APPLY_FIX3
2790 md_apply_fix3 (fixP, &add_number, this_segment_type);
2792 #ifdef BFD_ASSEMBLER
2793 md_apply_fix (fixP, &add_number);
2795 md_apply_fix (fixP, add_number);
2799 #ifndef TC_HANDLES_FX_DONE
2800 /* If the tc-* files haven't been converted, assume it's handling
2801 it the old way, where a null fx_addsy means that the fix has
2802 been applied completely, and no further work is needed. */
2803 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2808 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2810 if ((size_t) size < sizeof (valueT))
2815 mask--; /* Set all bits to one. */
2816 mask <<= size * 8 - (fixP->fx_signed ? 1 : 0);
2817 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2819 char buf[50], buf2[50];
2820 sprint_value (buf, fragP->fr_address + where);
2821 if (add_number > 1000)
2822 sprint_value (buf2, add_number);
2824 sprintf (buf2, "%ld", (long) add_number);
2825 as_bad_where (fixP->fx_file, fixP->fx_line,
2826 _("Value of %s too large for field of %d bytes at %s"),
2828 } /* Generic error checking. */
2830 #ifdef WARN_SIGNED_OVERFLOW_WORD
2831 /* Warn if a .word value is too large when treated as a signed
2832 number. We already know it is not too negative. This is to
2833 catch over-large switches generated by gcc on the 68k. */
2834 if (!flag_signed_overflow_ok
2836 && add_number > 0x7fff)
2837 as_bad_where (fixP->fx_file, fixP->fx_line,
2838 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2840 (unsigned long) (fragP->fr_address + where));
2842 } /* Not a bit fix. */
2844 #ifdef TC_VALIDATE_FIX
2845 skip: ATTRIBUTE_UNUSED_LABEL
2849 fprintf (stderr, "result:\n");
2852 } /* For each fixS in this segment. */
2854 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2855 return seg_reloc_count;
2858 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2861 number_to_chars_bigendian (buf, val, n)
2866 if ((size_t) n > sizeof (val) || n <= 0)
2870 buf[n] = val & 0xff;
2876 number_to_chars_littleendian (buf, val, n)
2881 if ((size_t) n > sizeof (val) || n <= 0)
2885 *buf++ = val & 0xff;
2891 write_print_statistics (file)
2894 fprintf (file, "fixups: %d\n", n_fixups);
2897 /* For debugging. */
2898 extern int indent_level;
2905 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2907 fprintf (stderr, " pcrel");
2908 if (fixp->fx_pcrel_adjust)
2909 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2910 if (fixp->fx_im_disp)
2913 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2915 fprintf (stderr, " im_disp");
2919 fprintf (stderr, " tcbit");
2921 fprintf (stderr, " done");
2922 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2923 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2924 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2925 #ifdef BFD_ASSEMBLER
2926 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2929 #ifdef NEED_FX_R_TYPE
2930 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2935 fprintf (stderr, "\n +<");
2936 print_symbol_value_1 (stderr, fixp->fx_addsy);
2937 fprintf (stderr, ">");
2941 fprintf (stderr, "\n -<");
2942 print_symbol_value_1 (stderr, fixp->fx_subsy);
2943 fprintf (stderr, ">");
2945 fprintf (stderr, "\n");
2946 #ifdef TC_FIX_DATA_PRINT
2947 TC_FIX_DATA_PRINT (stderr, fixp);