1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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, 51 Franklin Street - Fifth Floor, Boston, MA
23 /* This thing should be set up to do byteordering correctly. But... */
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
32 #ifndef TC_ADJUST_RELOC_COUNT
33 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
36 #ifndef TC_FORCE_RELOCATION
37 #define TC_FORCE_RELOCATION(FIX) \
38 (generic_force_reloc (FIX))
41 #ifndef TC_FORCE_RELOCATION_ABS
42 #define TC_FORCE_RELOCATION_ABS(FIX) \
43 (TC_FORCE_RELOCATION (FIX))
46 #ifndef TC_FORCE_RELOCATION_LOCAL
47 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
49 || TC_FORCE_RELOCATION (FIX))
52 #ifndef TC_FORCE_RELOCATION_SUB_SAME
53 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
57 #ifndef TC_FORCE_RELOCATION_SUB_ABS
58 #define TC_FORCE_RELOCATION_SUB_ABS(FIX) 0
61 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
63 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 0
65 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX) 1
69 #ifndef TC_VALIDATE_FIX_SUB
70 #ifdef UNDEFINED_DIFFERENCE_OK
71 /* The PA needs this for PIC code generation. */
72 #define TC_VALIDATE_FIX_SUB(FIX) 1
74 #define TC_VALIDATE_FIX_SUB(FIX) \
75 ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
76 || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
80 #ifndef TC_LINKRELAX_FIXUP
81 #define TC_LINKRELAX_FIXUP(SEG) 1
84 #ifndef MD_APPLY_SYM_VALUE
85 #define MD_APPLY_SYM_VALUE(FIX) 1
88 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
89 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
92 #ifndef MD_PCREL_FROM_SECTION
93 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
97 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
100 /* Positive values of TC_FX_SIZE_SLACK allow a target to define
101 fixups that far past the end of a frag. Having such fixups
102 is of course most most likely a bug in setting fx_size correctly.
103 A negative value disables the fixup check entirely, which is
104 appropriate for something like the Renesas / SuperH SH_COUNT
106 #ifndef TC_FX_SIZE_SLACK
107 #define TC_FX_SIZE_SLACK(FIX) 0
110 /* Used to control final evaluation of expressions. */
111 int finalize_syms = 0;
113 int symbol_table_frozen;
115 symbolS *abs_section_sym;
117 /* Remember the value of dot when parsing expressions. */
120 void print_fixup (fixS *);
122 /* We generally attach relocs to frag chains. However, after we have
123 chained these all together into a segment, any relocs we add after
124 that must be attached to a segment. This will include relocs added
125 in md_estimate_size_for_relax, for example. */
126 static int frags_chained = 0;
130 #define RELOC_ENUM enum bfd_reloc_code_real
132 /* Create a fixS in obstack 'notes'. */
135 fix_new_internal (fragS *frag, /* Which frag? */
136 int where, /* Where in that frag? */
137 int size, /* 1, 2, or 4 usually. */
138 symbolS *add_symbol, /* X_add_symbol. */
139 symbolS *sub_symbol, /* X_op_symbol. */
140 offsetT offset, /* X_add_number. */
141 int pcrel, /* TRUE if PC-relative relocation. */
142 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */)
148 fixP = obstack_alloc (¬es, sizeof (fixS));
150 fixP->fx_frag = frag;
151 fixP->fx_where = where;
152 fixP->fx_size = size;
153 /* We've made fx_size a narrow field; check that it's wide enough. */
154 if (fixP->fx_size != size)
156 as_bad (_("field fx_size too small to hold %d"), size);
159 fixP->fx_addsy = add_symbol;
160 fixP->fx_subsy = sub_symbol;
161 fixP->fx_offset = offset;
162 fixP->fx_dot_value = dot_value;
163 fixP->fx_pcrel = pcrel;
164 fixP->fx_r_type = r_type;
165 fixP->fx_im_disp = 0;
166 fixP->fx_pcrel_adjust = 0;
167 fixP->fx_bit_fixP = 0;
168 fixP->fx_addnumber = 0;
172 fixP->fx_no_overflow = 0;
176 fixP->fx_cgen.insn = NULL;
177 fixP->fx_cgen.opinfo = 0;
181 TC_INIT_FIX_DATA (fixP);
184 as_where (&fixP->fx_file, &fixP->fx_line);
186 /* Usually, we want relocs sorted numerically, but while
187 comparing to older versions of gas that have relocs
188 reverse sorted, it is convenient to have this compile
189 time option. xoxorich. */
192 fixS **seg_fix_rootP = (frags_chained
193 ? &seg_info (now_seg)->fix_root
194 : &frchain_now->fix_root);
195 fixS **seg_fix_tailP = (frags_chained
196 ? &seg_info (now_seg)->fix_tail
197 : &frchain_now->fix_tail);
199 #ifdef REVERSE_SORT_RELOCS
201 fixP->fx_next = *seg_fix_rootP;
202 *seg_fix_rootP = fixP;
204 #else /* REVERSE_SORT_RELOCS */
206 fixP->fx_next = NULL;
209 (*seg_fix_tailP)->fx_next = fixP;
211 *seg_fix_rootP = fixP;
212 *seg_fix_tailP = fixP;
214 #endif /* REVERSE_SORT_RELOCS */
220 /* Create a fixup relative to a symbol (plus a constant). */
223 fix_new (fragS *frag, /* Which frag? */
224 int where, /* Where in that frag? */
225 int size, /* 1, 2, or 4 usually. */
226 symbolS *add_symbol, /* X_add_symbol. */
227 offsetT offset, /* X_add_number. */
228 int pcrel, /* TRUE if PC-relative relocation. */
229 RELOC_ENUM r_type /* Relocation type. */)
231 return fix_new_internal (frag, where, size, add_symbol,
232 (symbolS *) NULL, offset, pcrel, r_type);
235 /* Create a fixup for an expression. Currently we only support fixups
236 for difference expressions. That is itself more than most object
237 file formats support anyhow. */
240 fix_new_exp (fragS *frag, /* Which frag? */
241 int where, /* Where in that frag? */
242 int size, /* 1, 2, or 4 usually. */
243 expressionS *exp, /* Expression. */
244 int pcrel, /* TRUE if PC-relative relocation. */
245 RELOC_ENUM r_type /* Relocation type. */)
257 as_bad (_("register value used as expression"));
261 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
262 the difference expression cannot immediately be reduced. */
264 symbolS *stmp = make_expr_symbol (exp);
266 exp->X_op = O_symbol;
267 exp->X_op_symbol = 0;
268 exp->X_add_symbol = stmp;
269 exp->X_add_number = 0;
271 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
275 add = exp->X_add_symbol;
276 off = exp->X_add_number;
277 r_type = BFD_RELOC_RVA;
281 sub = exp->X_add_symbol;
282 off = exp->X_add_number;
286 sub = exp->X_op_symbol;
289 add = exp->X_add_symbol;
292 off = exp->X_add_number;
296 add = make_expr_symbol (exp);
300 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
303 /* Generic function to determine whether a fixup requires a relocation. */
305 generic_force_reloc (fixS *fix)
307 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
308 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
311 if (fix->fx_addsy == NULL)
314 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
317 /* Append a string onto another string, bumping the pointer along. */
319 append (char **charPP, char *fromP, unsigned long length)
321 /* Don't trust memcpy() of 0 chars. */
325 memcpy (*charPP, fromP, length);
329 /* This routine records the largest alignment seen for each segment.
330 If the beginning of the segment is aligned on the worst-case
331 boundary, all of the other alignments within it will work. At
332 least one object format really uses this info. */
335 record_alignment (/* Segment to which alignment pertains. */
337 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
338 boundary, 2 => 4-byte boundary, etc.) */
341 if (seg == absolute_section)
344 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
345 bfd_set_section_alignment (stdoutput, seg, align);
349 get_recorded_alignment (segT seg)
351 if (seg == absolute_section)
354 return bfd_get_section_alignment (stdoutput, seg);
357 /* Reset the section indices after removing the gas created sections. */
360 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
362 int *countp = (int *) countparg;
364 sec->index = *countp;
369 chain_frchains_together_1 (segT section, struct frchain *frchp)
371 fragS dummy, *prev_frag = &dummy;
372 fixS fix_dummy, *prev_fix = &fix_dummy;
374 for (; frchp; frchp = frchp->frch_next)
376 prev_frag->fr_next = frchp->frch_root;
377 prev_frag = frchp->frch_last;
378 assert (prev_frag->fr_type != 0);
379 if (frchp->fix_root != (fixS *) NULL)
381 if (seg_info (section)->fix_root == (fixS *) NULL)
382 seg_info (section)->fix_root = frchp->fix_root;
383 prev_fix->fx_next = frchp->fix_root;
384 seg_info (section)->fix_tail = frchp->fix_tail;
385 prev_fix = frchp->fix_tail;
388 assert (prev_frag->fr_type != 0);
389 assert (prev_frag != &dummy);
390 prev_frag->fr_next = 0;
395 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
397 void *xxx ATTRIBUTE_UNUSED)
399 segment_info_type *info;
401 /* BFD may have introduced its own sections without using
402 subseg_new, so it is possible that seg_info is NULL. */
403 info = seg_info (section);
404 if (info != (segment_info_type *) NULL)
405 info->frchainP->frch_last
406 = chain_frchains_together_1 (section, info->frchainP);
408 /* Now that we've chained the frags together, we must add new fixups
409 to the segment, not to the frag chain. */
414 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
416 switch (fragP->fr_type)
424 HANDLE_ALIGN (fragP);
426 know (fragP->fr_next != NULL);
427 fragP->fr_offset = (fragP->fr_next->fr_address
429 - fragP->fr_fix) / fragP->fr_var;
430 if (fragP->fr_offset < 0)
432 as_bad_where (fragP->fr_file, fragP->fr_line,
433 _("attempt to .org/.space backwards? (%ld)"),
434 (long) fragP->fr_offset);
435 fragP->fr_offset = 0;
437 fragP->fr_type = rs_fill;
445 valueT value = S_GET_VALUE (fragP->fr_symbol);
448 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
451 fragP->fr_fix += size;
452 fragP->fr_type = rs_fill;
454 fragP->fr_offset = 0;
455 fragP->fr_symbol = NULL;
460 eh_frame_convert_frag (fragP);
464 dwarf2dbg_convert_frag (fragP);
467 case rs_machine_dependent:
468 md_convert_frag (stdoutput, sec, fragP);
470 assert (fragP->fr_next == NULL
471 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
474 /* After md_convert_frag, we make the frag into a ".space 0".
475 md_convert_frag() should set up any fixSs and constants
480 #ifndef WORKING_DOT_WORD
483 struct broken_word *lie;
485 if (fragP->fr_subtype)
487 fragP->fr_fix += md_short_jump_size;
488 for (lie = (struct broken_word *) (fragP->fr_symbol);
489 lie && lie->dispfrag == fragP;
490 lie = lie->next_broken_word)
492 fragP->fr_fix += md_long_jump_size;
500 BAD_CASE (fragP->fr_type);
504 md_frag_check (fragP);
508 struct relax_seg_info
515 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
517 segment_info_type *seginfo = seg_info (sec);
518 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
520 if (seginfo && seginfo->frchainP
521 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
526 size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
530 segment_info_type *seginfo;
532 valueT size, newsize;
534 subseg_change (sec, 0);
536 seginfo = seg_info (sec);
537 if (seginfo && seginfo->frchainP)
539 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
540 cvt_frag_to_fill (sec, fragp);
541 for (fragp = seginfo->frchainP->frch_root;
543 fragp = fragp->fr_next)
544 /* Walk to last elt. */
546 size = fragp->fr_address + fragp->fr_fix;
551 flags = bfd_get_section_flags (abfd, sec);
553 if (size > 0 && ! seginfo->bss)
554 flags |= SEC_HAS_CONTENTS;
556 /* @@ This is just an approximation. */
557 if (seginfo && seginfo->fix_root)
561 x = bfd_set_section_flags (abfd, sec, flags);
564 newsize = md_section_align (sec, size);
565 x = bfd_set_section_size (abfd, sec, newsize);
568 /* If the size had to be rounded up, add some padding in the last
570 assert (newsize >= size);
573 fragS *last = seginfo->frchainP->frch_last;
574 fragp = seginfo->frchainP->frch_root;
575 while (fragp->fr_next != last)
576 fragp = fragp->fr_next;
577 last->fr_address = size;
578 if ((newsize - size) % fragp->fr_var == 0)
579 fragp->fr_offset += (newsize - size) / fragp->fr_var;
581 /* If we hit this abort, it's likely due to subsegs_finish not
582 providing sufficient alignment on the last frag, and the
583 machine dependent code using alignment frags with fr_var
588 #ifdef tc_frob_section
589 tc_frob_section (sec);
591 #ifdef obj_frob_section
592 obj_frob_section (sec);
598 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
600 segment_info_type *seginfo = seg_info (sec);
601 fixS *fixp = seginfo->fix_root;
606 fprintf (stream, "sec %s relocs:\n", sec->name);
609 symbolS *s = fixp->fx_addsy;
611 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
612 (int) fixp->fx_r_type);
614 fprintf (stream, "no sym\n");
617 print_symbol_value_1 (stream, s);
618 fprintf (stream, "\n");
620 fixp = fixp->fx_next;
624 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
627 #ifndef EMIT_SECTION_SYMBOLS
628 #define EMIT_SECTION_SYMBOLS 1
631 /* This pass over fixups decides whether symbols can be replaced with
635 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
637 void *xxx ATTRIBUTE_UNUSED)
639 segment_info_type *seginfo = seg_info (sec);
645 dump_section_relocs (abfd, sec, stderr);
647 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
651 else if (fixp->fx_addsy)
657 fprintf (stderr, "\n\nadjusting fixup:\n");
661 sym = fixp->fx_addsy;
663 /* All symbols should have already been resolved at this
664 point. It is possible to see unresolved expression
665 symbols, though, since they are not in the regular symbol
667 resolve_symbol_value (sym);
669 if (fixp->fx_subsy != NULL)
670 resolve_symbol_value (fixp->fx_subsy);
672 /* If this symbol is equated to an undefined or common symbol,
673 convert the fixup to being against that symbol. */
674 while (symbol_equated_reloc_p (sym)
675 || S_IS_WEAKREFR (sym))
677 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
680 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
681 fixp->fx_addsy = newsym;
685 if (symbol_mri_common_p (sym))
687 fixp->fx_offset += S_GET_VALUE (sym);
688 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
692 /* If the symbol is undefined, common, weak, or global (ELF
693 shared libs), we can't replace it with the section symbol. */
694 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
697 /* Is there some other (target cpu dependent) reason we can't adjust
698 this one? (E.g. relocations involving function addresses on
700 #ifdef tc_fix_adjustable
701 if (! tc_fix_adjustable (fixp))
705 /* Since we're reducing to section symbols, don't attempt to reduce
706 anything that's already using one. */
707 if (symbol_section_p (sym))
710 symsec = S_GET_SEGMENT (sym);
714 if (bfd_is_abs_section (symsec))
716 /* The fixup_segment routine normally will not use this
717 symbol in a relocation. */
721 /* Don't try to reduce relocs which refer to non-local symbols
722 in .linkonce sections. It can lead to confusion when a
723 debugging section refers to a .linkonce section. I hope
724 this will always be correct. */
725 if (symsec != sec && ! S_IS_LOCAL (sym))
727 if ((symsec->flags & SEC_LINK_ONCE) != 0
729 /* The GNU toolchain uses an extension for ELF: a
730 section beginning with the magic string
731 .gnu.linkonce is a linkonce section. */
732 && strncmp (segment_name (symsec), ".gnu.linkonce",
733 sizeof ".gnu.linkonce" - 1) == 0))
737 /* Never adjust a reloc against local symbol in a merge section
738 with non-zero addend. */
739 if ((symsec->flags & SEC_MERGE) != 0
740 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
743 /* Never adjust a reloc against TLS local symbol. */
744 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
747 /* We refetch the segment when calling section_symbol, rather
748 than using symsec, because S_GET_VALUE may wind up changing
749 the section when it calls resolve_symbol_value. */
750 fixp->fx_offset += S_GET_VALUE (sym);
751 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
753 fprintf (stderr, "\nadjusted fixup:\n");
758 dump_section_relocs (abfd, sec, stderr);
763 Go through all the fixS's in a segment and see which ones can be
764 handled now. (These consist of fixS where we have since discovered
765 the value of a symbol, or the address of the frag involved.)
766 For each one, call md_apply_fix to put the fix into the frag data.
768 Result is a count of how many relocation structs will be needed to
769 handle the remaining fixS's that we couldn't completely handle here.
770 These will be output later by emit_relocations(). */
773 fixup_segment (fixS *fixP, segT this_segment)
775 long seg_reloc_count = 0;
778 segT add_symbol_segment = absolute_section;
780 if (fixP != NULL && abs_section_sym == NULL)
781 abs_section_sym = section_symbol (absolute_section);
783 /* If the linker is doing the relaxing, we must not do any fixups.
785 Well, strictly speaking that's not true -- we could do any that
786 are PC-relative and don't cross regions that could change size.
787 And for the i960 we might be able to turn callx/callj into bal
788 anyways in cases where we know the maximum displacement. */
789 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
791 for (; fixP; fixP = fixP->fx_next)
794 if (fixP->fx_addsy == NULL)
796 /* There was no symbol required by this relocation.
797 However, BFD doesn't really handle relocations
798 without symbols well. So fake up a local symbol in
799 the absolute section. */
800 fixP->fx_addsy = abs_section_sym;
802 symbol_mark_used_in_reloc (fixP->fx_addsy);
803 if (fixP->fx_subsy != NULL)
804 symbol_mark_used_in_reloc (fixP->fx_subsy);
807 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
808 return seg_reloc_count;
811 for (; fixP; fixP = fixP->fx_next)
814 fprintf (stderr, "\nprocessing fixup:\n");
818 fragP = fixP->fx_frag;
820 #ifdef TC_VALIDATE_FIX
821 TC_VALIDATE_FIX (fixP, this_segment, skip);
823 add_number = fixP->fx_offset;
825 if (fixP->fx_addsy != NULL)
826 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
828 if (fixP->fx_subsy != NULL)
830 segT sub_symbol_segment;
831 resolve_symbol_value (fixP->fx_subsy);
832 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
833 if (fixP->fx_addsy != NULL
834 && sub_symbol_segment == add_symbol_segment
835 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
837 add_number += S_GET_VALUE (fixP->fx_addsy);
838 add_number -= S_GET_VALUE (fixP->fx_subsy);
839 fixP->fx_offset = add_number;
840 fixP->fx_addsy = NULL;
841 fixP->fx_subsy = NULL;
843 /* See the comment below about 68k weirdness. */
847 else if (sub_symbol_segment == absolute_section
848 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
850 add_number -= S_GET_VALUE (fixP->fx_subsy);
851 fixP->fx_offset = add_number;
852 fixP->fx_subsy = NULL;
854 else if (sub_symbol_segment == this_segment
855 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
857 add_number -= S_GET_VALUE (fixP->fx_subsy);
858 fixP->fx_offset = (add_number + fixP->fx_dot_value
859 + fixP->fx_frag->fr_address);
861 /* Make it pc-relative. If the back-end code has not
862 selected a pc-relative reloc, cancel the adjustment
863 we do later on all pc-relative relocs. */
866 /* Do this for m68k even if it's already described
867 as pc-relative. On the m68k, an operand of
868 "pc@(foo-.-2)" should address "foo" in a
873 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
874 fixP->fx_subsy = NULL;
877 else if (!TC_VALIDATE_FIX_SUB (fixP))
879 as_bad_where (fixP->fx_file, fixP->fx_line,
880 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
881 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
882 segment_name (add_symbol_segment),
883 S_GET_NAME (fixP->fx_subsy),
884 segment_name (sub_symbol_segment));
890 if (add_symbol_segment == this_segment
891 && !TC_FORCE_RELOCATION_LOCAL (fixP))
893 /* This fixup was made when the symbol's segment was
894 SEG_UNKNOWN, but it is now in the local segment.
895 So we know how to do the address without relocation. */
896 add_number += S_GET_VALUE (fixP->fx_addsy);
897 fixP->fx_offset = add_number;
899 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
900 fixP->fx_addsy = NULL;
903 else if (add_symbol_segment == absolute_section
904 && !TC_FORCE_RELOCATION_ABS (fixP))
906 add_number += S_GET_VALUE (fixP->fx_addsy);
907 fixP->fx_offset = add_number;
908 fixP->fx_addsy = NULL;
910 else if (add_symbol_segment != undefined_section
911 && ! bfd_is_com_section (add_symbol_segment)
912 && MD_APPLY_SYM_VALUE (fixP))
913 add_number += S_GET_VALUE (fixP->fx_addsy);
918 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
919 if (!fixP->fx_done && fixP->fx_addsy == NULL)
921 /* There was no symbol required by this relocation.
922 However, BFD doesn't really handle relocations
923 without symbols well. So fake up a local symbol in
924 the absolute section. */
925 fixP->fx_addsy = abs_section_sym;
930 md_apply_fix (fixP, &add_number, this_segment);
935 if (fixP->fx_addsy == NULL)
936 fixP->fx_addsy = abs_section_sym;
937 symbol_mark_used_in_reloc (fixP->fx_addsy);
938 if (fixP->fx_subsy != NULL)
939 symbol_mark_used_in_reloc (fixP->fx_subsy);
942 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
944 if (fixP->fx_size < sizeof (valueT))
949 mask--; /* Set all bits to one. */
950 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
951 if ((add_number & mask) != 0 && (add_number & mask) != mask)
953 char buf[50], buf2[50];
954 sprint_value (buf, fragP->fr_address + fixP->fx_where);
955 if (add_number > 1000)
956 sprint_value (buf2, add_number);
958 sprintf (buf2, "%ld", (long) add_number);
959 as_bad_where (fixP->fx_file, fixP->fx_line,
960 _("value of %s too large for field of %d bytes at %s"),
961 buf2, fixP->fx_size, buf);
962 } /* Generic error checking. */
964 #ifdef WARN_SIGNED_OVERFLOW_WORD
965 /* Warn if a .word value is too large when treated as a signed
966 number. We already know it is not too negative. This is to
967 catch over-large switches generated by gcc on the 68k. */
968 if (!flag_signed_overflow_ok
969 && fixP->fx_size == 2
970 && add_number > 0x7fff)
971 as_bad_where (fixP->fx_file, fixP->fx_line,
972 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
974 (long) (fragP->fr_address + fixP->fx_where));
976 } /* Not a bit fix. */
978 #ifdef TC_VALIDATE_FIX
979 skip: ATTRIBUTE_UNUSED_LABEL
983 fprintf (stderr, "result:\n");
986 } /* For each fixS in this segment. */
988 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
989 return seg_reloc_count;
993 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
995 void *xxx ATTRIBUTE_UNUSED)
997 segment_info_type *seginfo = seg_info (sec);
999 fixup_segment (seginfo->fix_root, sec);
1003 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1005 segment_info_type *seginfo = seg_info (sec);
1012 /* If seginfo is NULL, we did not create this section; don't do
1013 anything with it. */
1014 if (seginfo == NULL)
1018 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1021 #ifndef RELOC_EXPANSION_POSSIBLE
1022 /* Set up reloc information as well. */
1023 relocs = xcalloc (n, sizeof (arelent *));
1026 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1029 bfd_reloc_status_type s;
1039 reloc = tc_gen_reloc (sec, fixp);
1046 fx_size = fixp->fx_size;
1047 slack = TC_FX_SIZE_SLACK (fixp);
1049 fx_size = fx_size > slack ? fx_size - slack : 0;
1050 loc = fixp->fx_where + fx_size;
1052 && loc > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1053 as_bad_where (fixp->fx_file, fixp->fx_line,
1054 _("internal error: fixup not contained within frag"));
1056 s = bfd_install_relocation (stdoutput, reloc,
1057 fixp->fx_frag->fr_literal,
1058 fixp->fx_frag->fr_address,
1064 case bfd_reloc_overflow:
1065 as_bad_where (fixp->fx_file, fixp->fx_line,
1066 _("relocation overflow"));
1068 case bfd_reloc_outofrange:
1069 as_bad_where (fixp->fx_file, fixp->fx_line,
1070 _("relocation out of range"));
1073 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1074 fixp->fx_file, fixp->fx_line, s);
1076 relocs[i++] = reloc;
1079 n = n * MAX_RELOC_EXPANSION;
1080 /* Set up reloc information as well. */
1081 relocs = xcalloc (n, sizeof (arelent *));
1084 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1087 bfd_reloc_status_type s;
1098 reloc = tc_gen_reloc (sec, fixp);
1100 for (j = 0; reloc[j]; j++)
1102 relocs[i++] = reloc[j];
1106 fx_size = fixp->fx_size;
1107 slack = TC_FX_SIZE_SLACK (fixp);
1109 fx_size = fx_size > slack ? fx_size - slack : 0;
1110 loc = fixp->fx_where + fx_size;
1112 && loc > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1113 as_bad_where (fixp->fx_file, fixp->fx_line,
1114 _("internal error: fixup not contained within frag"));
1116 for (j = 0; reloc[j]; j++)
1118 s = bfd_install_relocation (stdoutput, reloc[j],
1119 fixp->fx_frag->fr_literal,
1120 fixp->fx_frag->fr_address,
1126 case bfd_reloc_overflow:
1127 as_bad_where (fixp->fx_file, fixp->fx_line,
1128 _("relocation overflow"));
1130 case bfd_reloc_outofrange:
1131 as_bad_where (fixp->fx_file, fixp->fx_line,
1132 _("relocation out of range"));
1135 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1136 fixp->fx_file, fixp->fx_line, s);
1145 unsigned int i, j, nsyms;
1147 sympp = bfd_get_outsymbols (stdoutput);
1148 nsyms = bfd_get_symcount (stdoutput);
1149 for (i = 0; i < n; i++)
1150 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1152 for (j = 0; j < nsyms; j++)
1153 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1162 bfd_set_reloc (stdoutput, sec, relocs, n);
1164 bfd_set_section_flags (abfd, sec,
1165 (bfd_get_section_flags (abfd, sec)
1166 & (flagword) ~SEC_RELOC));
1168 #ifdef SET_SECTION_RELOCS
1169 SET_SECTION_RELOCS (sec, relocs, n);
1177 fprintf (stderr, "relocs for sec %s\n", sec->name);
1178 for (i = 0; i < n; i++)
1181 s = *r->sym_ptr_ptr;
1182 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1183 i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
1190 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1192 void *xxx ATTRIBUTE_UNUSED)
1194 segment_info_type *seginfo = seg_info (sec);
1195 addressT offset = 0;
1198 /* Write out the frags. */
1200 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1203 for (f = seginfo->frchainP->frch_root;
1212 assert (f->fr_type == rs_fill);
1215 x = bfd_set_section_contents (stdoutput, sec,
1216 f->fr_literal, (file_ptr) offset,
1217 (bfd_size_type) f->fr_fix);
1219 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1220 bfd_errmsg (bfd_get_error ()));
1221 offset += f->fr_fix;
1223 fill_literal = f->fr_literal + f->fr_fix;
1224 fill_size = f->fr_var;
1225 count = f->fr_offset;
1226 assert (count >= 0);
1227 if (fill_size && count)
1230 if (fill_size > sizeof (buf))
1232 /* Do it the old way. Can this ever happen? */
1235 x = bfd_set_section_contents (stdoutput, sec,
1238 (bfd_size_type) fill_size);
1240 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1241 bfd_errmsg (bfd_get_error ()));
1242 offset += fill_size;
1247 /* Build a buffer full of fill objects and output it as
1248 often as necessary. This saves on the overhead of
1249 potentially lots of bfd_set_section_contents calls. */
1253 n_per_buf = sizeof (buf);
1254 memset (buf, *fill_literal, n_per_buf);
1259 n_per_buf = sizeof (buf) / fill_size;
1260 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1261 memcpy (bufp, fill_literal, fill_size);
1263 for (; count > 0; count -= n_per_buf)
1265 n_per_buf = n_per_buf > count ? count : n_per_buf;
1266 x = bfd_set_section_contents
1267 (stdoutput, sec, buf, (file_ptr) offset,
1268 (bfd_size_type) n_per_buf * fill_size);
1270 as_fatal (_("cannot write to output file"));
1271 offset += n_per_buf * fill_size;
1279 merge_data_into_text (void)
1281 seg_info (text_section)->frchainP->frch_last->fr_next =
1282 seg_info (data_section)->frchainP->frch_root;
1283 seg_info (text_section)->frchainP->frch_last =
1284 seg_info (data_section)->frchainP->frch_last;
1285 seg_info (data_section)->frchainP = 0;
1296 /* Count symbols. We can't rely on a count made by the loop in
1297 write_object_file, because *_frob_file may add a new symbol or
1300 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1306 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1308 asympp = bfd_alloc (stdoutput, amt);
1309 symp = symbol_rootP;
1310 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1312 asympp[i] = symbol_get_bfdsym (symp);
1313 symbol_mark_written (symp);
1318 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1320 symbol_table_frozen = 1;
1323 /* Finish the subsegments. After every sub-segment, we fake an
1324 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1325 ".fill 0" because that is the kind of frag that requires least
1326 thought. ".align" frags like to have a following frag since that
1327 makes calculating their intended length trivial. */
1329 #ifndef SUB_SEGMENT_ALIGN
1331 /* The last subsegment gets an alignment corresponding to the alignment
1332 of the section. This allows proper nop-filling at the end of
1333 code-bearing sections. */
1334 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1335 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1337 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1342 subsegs_finish (void)
1344 struct frchain *frchainP;
1347 for (s = stdoutput->sections; s; s = s->next)
1349 segment_info_type *seginfo = seg_info (s);
1353 for (frchainP = seginfo->frchainP;
1355 frchainP = frchainP->frch_next)
1359 subseg_set (s, frchainP->frch_subseg);
1361 /* This now gets called even if we had errors. In that case,
1362 any alignment is meaningless, and, moreover, will look weird
1363 if we are generating a listing. */
1366 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1367 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1368 && now_seg->entsize)
1370 unsigned int entsize = now_seg->entsize;
1373 while ((entsize & 1) == 0)
1378 if (entalign > alignment)
1379 alignment = entalign;
1383 if (subseg_text_p (now_seg))
1384 frag_align_code (alignment, 0);
1386 frag_align (alignment, 0, 0);
1388 /* frag_align will have left a new frag.
1389 Use this last frag for an empty ".fill".
1391 For this segment ...
1392 Create a last frag. Do not leave a "being filled in frag". */
1393 frag_wane (frag_now);
1394 frag_now->fr_fix = 0;
1395 know (frag_now->fr_next == NULL);
1400 /* Write the object file. */
1403 write_object_file (void)
1405 struct relax_seg_info rsi;
1406 #ifndef WORKING_DOT_WORD
1407 fragS *fragP; /* Track along all frags. */
1410 /* Do we really want to write it? */
1412 int n_warns, n_errs;
1413 n_warns = had_warnings ();
1414 n_errs = had_errors ();
1415 /* The -Z flag indicates that an object file should be generated,
1416 regardless of warnings and errors. */
1417 if (flag_always_generate_output)
1419 if (n_warns || n_errs)
1420 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1421 n_errs, n_errs == 1 ? "" : "s",
1422 n_warns, n_warns == 1 ? "" : "s");
1427 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1428 n_errs, n_errs == 1 ? "" : "s",
1429 n_warns, n_warns == 1 ? "" : "s");
1434 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1435 a routine to check for the definition of the procedure "_main",
1436 and if so -- fix it up so that it can be program entry point. */
1437 vms_check_for_main ();
1438 #endif /* OBJ_VMS */
1440 /* From now on, we don't care about sub-segments. Build one frag chain
1441 for each segment. Linked thru fr_next. */
1443 /* Remove the sections created by gas for its own purposes. */
1447 bfd_section_list_remove (stdoutput, reg_section);
1448 bfd_section_list_remove (stdoutput, expr_section);
1449 stdoutput->section_count -= 2;
1451 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1454 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1456 /* We have two segments. If user gave -R flag, then we must put the
1457 data frags into the text segment. Do this before relaxing so
1458 we know to take advantage of -R and make shorter addresses. */
1459 if (flag_readonly_data_in_text)
1461 merge_data_into_text ();
1467 #ifndef WORKING_DOT_WORD
1468 /* We need to reset the markers in the broken word list and
1469 associated frags between calls to relax_segment (via
1470 relax_seg). Since the broken word list is global, we do it
1471 once per round, rather than locally in relax_segment for each
1473 struct broken_word *brokp;
1475 for (brokp = broken_words;
1476 brokp != (struct broken_word *) NULL;
1477 brokp = brokp->next_broken_word)
1481 if (brokp->dispfrag != (fragS *) NULL
1482 && brokp->dispfrag->fr_type == rs_broken_word)
1483 brokp->dispfrag->fr_subtype = 0;
1488 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1494 /* Note - Most ports will use the default value of
1495 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1496 local symbols to be resolved, removing their frag information.
1497 Some ports however, will not have finished relaxing all of
1498 their frags and will still need the local symbol frag
1499 information. These ports can set
1500 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1501 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1503 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1505 /* Relaxation has completed. Freeze all syms. */
1508 #ifdef md_post_relax_hook
1512 #ifndef WORKING_DOT_WORD
1514 struct broken_word *lie;
1515 struct broken_word **prevP;
1517 prevP = &broken_words;
1518 for (lie = broken_words; lie; lie = lie->next_broken_word)
1523 subseg_change (lie->seg, lie->subseg);
1524 exp.X_op = O_subtract;
1525 exp.X_add_symbol = lie->add;
1526 exp.X_op_symbol = lie->sub;
1527 exp.X_add_number = lie->addnum;
1528 #ifdef TC_CONS_FIX_NEW
1529 TC_CONS_FIX_NEW (lie->frag,
1530 lie->word_goes_here - lie->frag->fr_literal,
1533 fix_new_exp (lie->frag,
1534 lie->word_goes_here - lie->frag->fr_literal,
1535 2, &exp, 0, BFD_RELOC_16);
1537 *prevP = lie->next_broken_word;
1540 prevP = &(lie->next_broken_word);
1542 for (lie = broken_words; lie;)
1544 struct broken_word *untruth;
1546 addressT table_addr;
1547 addressT from_addr, to_addr;
1550 subseg_change (lie->seg, lie->subseg);
1551 fragP = lie->dispfrag;
1553 /* Find out how many broken_words go here. */
1556 untruth && untruth->dispfrag == fragP;
1557 untruth = untruth->next_broken_word)
1558 if (untruth->added == 1)
1561 table_ptr = lie->dispfrag->fr_opcode;
1562 table_addr = (lie->dispfrag->fr_address
1563 + (table_ptr - lie->dispfrag->fr_literal));
1564 /* Create the jump around the long jumps. This is a short
1565 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1566 from_addr = table_addr;
1567 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1568 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1570 table_ptr += md_short_jump_size;
1571 table_addr += md_short_jump_size;
1574 lie && lie->dispfrag == fragP;
1575 m++, lie = lie->next_broken_word)
1577 if (lie->added == 2)
1579 /* Patch the jump table. */
1580 /* This is the offset from ??? to table_ptr+0. */
1581 to_addr = table_addr - S_GET_VALUE (lie->sub);
1582 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1583 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1585 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1586 for (untruth = lie->next_broken_word;
1587 untruth && untruth->dispfrag == fragP;
1588 untruth = untruth->next_broken_word)
1590 if (untruth->use_jump == lie)
1591 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1594 /* Install the long jump. */
1595 /* This is a long jump from table_ptr+0 to the final target. */
1596 from_addr = table_addr;
1597 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1598 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1600 table_ptr += md_long_jump_size;
1601 table_addr += md_long_jump_size;
1605 #endif /* not WORKING_DOT_WORD */
1607 /* Resolve symbol values. This needs to be done before processing
1613 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1614 resolve_symbol_value (symp);
1616 resolve_local_symbol_values ();
1620 #ifdef tc_frob_file_before_adjust
1621 tc_frob_file_before_adjust ();
1623 #ifdef obj_frob_file_before_adjust
1624 obj_frob_file_before_adjust ();
1627 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1629 #ifdef tc_frob_file_before_fix
1630 tc_frob_file_before_fix ();
1632 #ifdef obj_frob_file_before_fix
1633 obj_frob_file_before_fix ();
1636 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1638 /* Set up symbol table, and write it out. */
1642 bfd_boolean skip_next_symbol = FALSE;
1644 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1649 if (skip_next_symbol)
1651 /* Don't do anything besides moving the value of the
1652 symbol from the GAS value-field to the BFD value-field. */
1653 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1654 skip_next_symbol = FALSE;
1658 if (symbol_mri_common_p (symp))
1660 if (S_IS_EXTERNAL (symp))
1661 as_bad (_("%s: global symbols not supported in common sections"),
1663 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1667 name = S_GET_NAME (symp);
1671 decode_local_label_name ((char *) S_GET_NAME (symp));
1672 /* They only differ if `name' is a fb or dollar local
1674 if (name2 != name && ! S_IS_DEFINED (symp))
1675 as_bad (_("local label `%s' is not defined"), name2);
1678 /* Do it again, because adjust_reloc_syms might introduce
1679 more symbols. They'll probably only be section symbols,
1680 but they'll still need to have the values computed. */
1681 resolve_symbol_value (symp);
1683 /* Skip symbols which were equated to undefined or common
1685 if (symbol_equated_reloc_p (symp)
1686 || S_IS_WEAKREFR (symp))
1688 const char *name = S_GET_NAME (symp);
1689 if (S_IS_COMMON (symp)
1690 && !TC_FAKE_LABEL (name)
1691 && !S_IS_WEAKREFR (symp)
1692 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1694 expressionS *e = symbol_get_value_expression (symp);
1695 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1696 name, S_GET_NAME (e->X_add_symbol));
1698 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1702 #ifdef obj_frob_symbol
1703 obj_frob_symbol (symp, punt);
1705 #ifdef tc_frob_symbol
1706 if (! punt || symbol_used_in_reloc_p (symp))
1707 tc_frob_symbol (symp, punt);
1710 /* If we don't want to keep this symbol, splice it out of
1711 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1712 want section symbols. Otherwise, we skip local symbols
1713 and symbols that the frob_symbol macros told us to punt,
1714 but we keep such symbols if they are used in relocs. */
1715 if (symp == abs_section_sym
1716 || (! EMIT_SECTION_SYMBOLS
1717 && symbol_section_p (symp))
1718 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1719 opposites. Sometimes the former checks flags and the
1720 latter examines the name... */
1721 || (!S_IS_EXTERNAL (symp)
1722 && (punt || S_IS_LOCAL (symp) ||
1723 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
1724 && ! symbol_used_in_reloc_p (symp)))
1726 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1728 /* After symbol_remove, symbol_next(symp) still returns
1729 the one that came after it in the chain. So we don't
1730 need to do any extra cleanup work here. */
1734 /* Make sure we really got a value for the symbol. */
1735 if (! symbol_resolved_p (symp))
1737 as_bad (_("can't resolve value for symbol `%s'"),
1739 symbol_mark_resolved (symp);
1742 /* Set the value into the BFD symbol. Up til now the value
1743 has only been kept in the gas symbolS struct. */
1744 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1746 /* A warning construct is a warning symbol followed by the
1747 symbol warned about. Don't let anything object-format or
1748 target-specific muck with it; it's ready for output. */
1749 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1750 skip_next_symbol = TRUE;
1756 /* Now do any format-specific adjustments to the symbol table, such
1757 as adding file symbols. */
1758 #ifdef tc_adjust_symtab
1759 tc_adjust_symtab ();
1761 #ifdef obj_adjust_symtab
1762 obj_adjust_symtab ();
1765 /* Now that all the sizes are known, and contents correct, we can
1766 start writing to the file. */
1769 /* If *_frob_file changes the symbol value at this point, it is
1770 responsible for moving the changed value into symp->bsym->value
1771 as well. Hopefully all symbol value changing can be done in
1776 #ifdef obj_frob_file
1780 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1782 #ifdef tc_frob_file_after_relocs
1783 tc_frob_file_after_relocs ();
1785 #ifdef obj_frob_file_after_relocs
1786 obj_frob_file_after_relocs ();
1789 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1792 #ifdef TC_GENERIC_RELAX_TABLE
1793 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1796 relax_frag (segT segment, fragS *fragP, long stretch)
1798 const relax_typeS *this_type;
1799 const relax_typeS *start_type;
1800 relax_substateT next_state;
1801 relax_substateT this_state;
1807 const relax_typeS *table;
1809 target = fragP->fr_offset;
1810 address = fragP->fr_address;
1811 table = TC_GENERIC_RELAX_TABLE;
1812 this_state = fragP->fr_subtype;
1813 start_type = this_type = table + this_state;
1814 symbolP = fragP->fr_symbol;
1820 sym_frag = symbol_get_frag (symbolP);
1822 #ifndef DIFF_EXPR_OK
1823 know (sym_frag != NULL);
1825 know (S_GET_SEGMENT (symbolP) != absolute_section
1826 || sym_frag == &zero_address_frag);
1827 target += S_GET_VALUE (symbolP);
1829 /* If frag has yet to be reached on this pass,
1830 assume it will move by STRETCH just as we did.
1831 If this is not so, it will be because some frag
1832 between grows, and that will force another pass. */
1835 && sym_frag->relax_marker != fragP->relax_marker
1836 && S_GET_SEGMENT (symbolP) == segment)
1842 aim = target - address - fragP->fr_fix;
1843 #ifdef TC_PCREL_ADJUST
1844 /* Currently only the ns32k family needs this. */
1845 aim += TC_PCREL_ADJUST (fragP);
1848 #ifdef md_prepare_relax_scan
1849 /* Formerly called M68K_AIM_KLUDGE. */
1850 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
1855 /* Look backwards. */
1856 for (next_state = this_type->rlx_more; next_state;)
1857 if (aim >= this_type->rlx_backward)
1861 /* Grow to next state. */
1862 this_state = next_state;
1863 this_type = table + this_state;
1864 next_state = this_type->rlx_more;
1869 /* Look forwards. */
1870 for (next_state = this_type->rlx_more; next_state;)
1871 if (aim <= this_type->rlx_forward)
1875 /* Grow to next state. */
1876 this_state = next_state;
1877 this_type = table + this_state;
1878 next_state = this_type->rlx_more;
1882 growth = this_type->rlx_length - start_type->rlx_length;
1884 fragP->fr_subtype = this_state;
1888 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
1890 /* Relax_align. Advance location counter to next address that has 'alignment'
1891 lowest order bits all 0s, return size of adjustment made. */
1892 static relax_addressT
1893 relax_align (register relax_addressT address, /* Address now. */
1894 register int alignment /* Alignment (binary). */)
1896 relax_addressT mask;
1897 relax_addressT new_address;
1899 mask = ~((~0) << alignment);
1900 new_address = (address + mask) & (~mask);
1901 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1903 /* We must provide lots of padding, so the linker can discard it
1904 when needed. The linker will not add extra space, ever. */
1905 new_address += (1 << alignment);
1907 return (new_address - address);
1910 /* Now we have a segment, not a crowd of sub-segments, we can make
1915 After this, all frags in this segment have addresses that are correct
1916 within the segment. Since segments live in different file addresses,
1917 these frag addresses may not be the same as final object-file
1921 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
1923 unsigned long frag_count;
1925 relax_addressT address;
1928 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1929 subseg_change (segment, 0);
1931 /* For each frag in segment: count and store (a 1st guess of)
1934 for (frag_count = 0, fragP = segment_frag_root;
1936 fragP = fragP->fr_next, frag_count ++)
1938 fragP->relax_marker = 0;
1939 fragP->fr_address = address;
1940 address += fragP->fr_fix;
1942 switch (fragP->fr_type)
1945 address += fragP->fr_offset * fragP->fr_var;
1952 addressT offset = relax_align (address, (int) fragP->fr_offset);
1954 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
1957 if (offset % fragP->fr_var != 0)
1959 as_bad_where (fragP->fr_file, fragP->fr_line,
1960 _("alignment padding (%lu bytes) not a multiple of %ld"),
1961 (unsigned long) offset, (long) fragP->fr_var);
1962 offset -= (offset % fragP->fr_var);
1971 /* Assume .org is nugatory. It will grow with 1st relax. */
1974 case rs_machine_dependent:
1975 /* If fr_symbol is an expression, this call to
1976 resolve_symbol_value sets up the correct segment, which will
1977 likely be needed in md_estimate_size_before_relax. */
1978 if (fragP->fr_symbol)
1979 resolve_symbol_value (fragP->fr_symbol);
1981 address += md_estimate_size_before_relax (fragP, segment);
1984 #ifndef WORKING_DOT_WORD
1985 /* Broken words don't concern us yet. */
1986 case rs_broken_word:
1991 /* Initial guess is always 1; doing otherwise can result in
1992 stable solutions that are larger than the minimum. */
1993 address += fragP->fr_offset = 1;
1997 address += eh_frame_estimate_size_before_relax (fragP);
2001 address += dwarf2dbg_estimate_size_before_relax (fragP);
2005 BAD_CASE (fragP->fr_type);
2012 unsigned long max_iterations;
2013 offsetT stretch; /* May be any size, 0 or negative. */
2014 /* Cumulative number of addresses we have relaxed this pass.
2015 We may have relaxed more than one address. */
2016 int stretched; /* Have we stretched on this pass? */
2017 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2018 grew, and another shrank. If a branch instruction doesn't fit anymore,
2019 we could be scrod. */
2021 /* We want to prevent going into an infinite loop where one frag grows
2022 depending upon the location of a symbol which is in turn moved by
2023 the growing frag. eg:
2029 So we dictate that this algorithm can be at most O2. */
2030 max_iterations = frag_count * frag_count;
2031 /* Check for overflow. */
2032 if (max_iterations < frag_count)
2033 max_iterations = frag_count;
2041 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2044 addressT was_address;
2048 fragP->relax_marker ^= 1;
2049 was_address = fragP->fr_address;
2050 address = fragP->fr_address += stretch;
2051 symbolP = fragP->fr_symbol;
2052 offset = fragP->fr_offset;
2054 switch (fragP->fr_type)
2056 case rs_fill: /* .fill never relaxes. */
2060 #ifndef WORKING_DOT_WORD
2061 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2062 for it I do not want to write it. I do not want to have
2063 anything to do with it. This is not the proper way to
2064 implement this misfeature. */
2065 case rs_broken_word:
2067 struct broken_word *lie;
2068 struct broken_word *untruth;
2070 /* Yes this is ugly (storing the broken_word pointer
2071 in the symbol slot). Still, this whole chunk of
2072 code is ugly, and I don't feel like doing anything
2073 about it. Think of it as stubbornness in action. */
2075 for (lie = (struct broken_word *) (fragP->fr_symbol);
2076 lie && lie->dispfrag == fragP;
2077 lie = lie->next_broken_word)
2083 offset = (S_GET_VALUE (lie->add)
2085 - S_GET_VALUE (lie->sub));
2086 if (offset <= -32768 || offset >= 32767)
2088 if (flag_warn_displacement)
2091 sprint_value (buf, (addressT) lie->addnum);
2092 as_warn_where (fragP->fr_file, fragP->fr_line,
2093 _(".word %s-%s+%s didn't fit"),
2094 S_GET_NAME (lie->add),
2095 S_GET_NAME (lie->sub),
2099 if (fragP->fr_subtype == 0)
2101 fragP->fr_subtype++;
2102 growth += md_short_jump_size;
2104 for (untruth = lie->next_broken_word;
2105 untruth && untruth->dispfrag == lie->dispfrag;
2106 untruth = untruth->next_broken_word)
2107 if ((symbol_get_frag (untruth->add)
2108 == symbol_get_frag (lie->add))
2109 && (S_GET_VALUE (untruth->add)
2110 == S_GET_VALUE (lie->add)))
2113 untruth->use_jump = lie;
2115 growth += md_long_jump_size;
2120 } /* case rs_broken_word */
2126 addressT oldoff, newoff;
2128 oldoff = relax_align (was_address + fragP->fr_fix,
2130 newoff = relax_align (address + fragP->fr_fix,
2133 if (fragP->fr_subtype != 0)
2135 if (oldoff > fragP->fr_subtype)
2137 if (newoff > fragP->fr_subtype)
2141 growth = newoff - oldoff;
2147 addressT target = offset;
2152 /* Convert from an actual address to an octet offset
2153 into the section. Here it is assumed that the
2154 section's VMA is zero, and can omit subtracting it
2155 from the symbol's value to get the address offset. */
2156 know (S_GET_SEGMENT (symbolP)->vma == 0);
2157 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2160 know (fragP->fr_next);
2161 after = fragP->fr_next->fr_address;
2162 growth = target - after;
2167 /* Don't error on first few frag relax passes.
2168 The symbol might be an expression involving
2169 symbol values from other sections. If those
2170 sections have not yet been processed their
2171 frags will all have zero addresses, so we
2172 will calculate incorrect values for them. The
2173 number of passes we allow before giving an
2174 error is somewhat arbitrary. It should be at
2175 least one, with larger values requiring
2176 increasingly contrived dependencies between
2177 frags to trigger a false error. */
2180 /* Force another pass. */
2185 /* Growth may be negative, but variable part of frag
2186 cannot have fewer than 0 chars. That is, we can't
2188 as_bad_where (fragP->fr_file, fragP->fr_line,
2189 _("attempt to move .org backwards"));
2191 /* We've issued an error message. Change the
2192 frag to avoid cascading errors. */
2193 fragP->fr_type = rs_align;
2194 fragP->fr_subtype = 0;
2195 fragP->fr_offset = 0;
2196 fragP->fr_fix = after - was_address;
2200 /* This is an absolute growth factor */
2211 amount = S_GET_VALUE (symbolP);
2212 if (S_GET_SEGMENT (symbolP) != absolute_section
2213 || S_IS_COMMON (symbolP)
2214 || ! S_IS_DEFINED (symbolP))
2216 as_bad_where (fragP->fr_file, fragP->fr_line,
2217 _(".space specifies non-absolute value"));
2218 /* Prevent repeat of this error message. */
2219 fragP->fr_symbol = 0;
2221 else if (amount < 0)
2223 /* Don't error on first few frag relax passes.
2224 See rs_org comment for a longer explanation. */
2231 as_warn_where (fragP->fr_file, fragP->fr_line,
2232 _(".space or .fill with negative value, ignored"));
2233 fragP->fr_symbol = 0;
2236 growth = (was_address + fragP->fr_fix + amount
2237 - fragP->fr_next->fr_address);
2241 case rs_machine_dependent:
2242 #ifdef md_relax_frag
2243 growth = md_relax_frag (segment, fragP, stretch);
2245 #ifdef TC_GENERIC_RELAX_TABLE
2246 /* The default way to relax a frag is to look through
2247 TC_GENERIC_RELAX_TABLE. */
2248 growth = relax_frag (segment, fragP, stretch);
2249 #endif /* TC_GENERIC_RELAX_TABLE */
2258 value = resolve_symbol_value (fragP->fr_symbol);
2259 size = sizeof_leb128 (value, fragP->fr_subtype);
2260 growth = size - fragP->fr_offset;
2261 fragP->fr_offset = size;
2266 growth = eh_frame_relax_frag (fragP);
2270 growth = dwarf2dbg_relax_frag (fragP);
2274 BAD_CASE (fragP->fr_type);
2284 /* Until nothing further to relax. */
2285 while (stretched && -- max_iterations);
2288 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2289 segment_name (segment));
2292 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2293 if (fragP->last_fr_address != fragP->fr_address)
2295 fragP->last_fr_address = fragP->fr_address;
2302 number_to_chars_bigendian (char *buf, valueT val, int n)
2308 buf[n] = val & 0xff;
2314 number_to_chars_littleendian (char *buf, valueT val, int n)
2320 *buf++ = val & 0xff;
2326 write_print_statistics (FILE *file)
2328 fprintf (file, "fixups: %d\n", n_fixups);
2331 /* For debugging. */
2332 extern int indent_level;
2335 print_fixup (fixS *fixp)
2338 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2340 fprintf (stderr, " pcrel");
2341 if (fixp->fx_pcrel_adjust)
2342 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2343 if (fixp->fx_im_disp)
2346 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2348 fprintf (stderr, " im_disp");
2352 fprintf (stderr, " tcbit");
2354 fprintf (stderr, " done");
2355 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2356 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2357 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2358 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2362 fprintf (stderr, "\n +<");
2363 print_symbol_value_1 (stderr, fixp->fx_addsy);
2364 fprintf (stderr, ">");
2368 fprintf (stderr, "\n -<");
2369 print_symbol_value_1 (stderr, fixp->fx_subsy);
2370 fprintf (stderr, ">");
2372 fprintf (stderr, "\n");
2373 #ifdef TC_FIX_DATA_PRINT
2374 TC_FIX_DATA_PRINT (stderr, fixp);