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;
557 x = bfd_set_section_flags (abfd, sec, flags);
560 newsize = md_section_align (sec, size);
561 x = bfd_set_section_size (abfd, sec, newsize);
564 /* If the size had to be rounded up, add some padding in the last
566 assert (newsize >= size);
569 fragS *last = seginfo->frchainP->frch_last;
570 fragp = seginfo->frchainP->frch_root;
571 while (fragp->fr_next != last)
572 fragp = fragp->fr_next;
573 last->fr_address = size;
574 if ((newsize - size) % fragp->fr_var == 0)
575 fragp->fr_offset += (newsize - size) / fragp->fr_var;
577 /* If we hit this abort, it's likely due to subsegs_finish not
578 providing sufficient alignment on the last frag, and the
579 machine dependent code using alignment frags with fr_var
584 #ifdef tc_frob_section
585 tc_frob_section (sec);
587 #ifdef obj_frob_section
588 obj_frob_section (sec);
594 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
596 segment_info_type *seginfo = seg_info (sec);
597 fixS *fixp = seginfo->fix_root;
602 fprintf (stream, "sec %s relocs:\n", sec->name);
605 symbolS *s = fixp->fx_addsy;
607 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
608 (int) fixp->fx_r_type);
610 fprintf (stream, "no sym\n");
613 print_symbol_value_1 (stream, s);
614 fprintf (stream, "\n");
616 fixp = fixp->fx_next;
620 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
623 #ifndef EMIT_SECTION_SYMBOLS
624 #define EMIT_SECTION_SYMBOLS 1
627 /* This pass over fixups decides whether symbols can be replaced with
631 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
633 void *xxx ATTRIBUTE_UNUSED)
635 segment_info_type *seginfo = seg_info (sec);
641 dump_section_relocs (abfd, sec, stderr);
643 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
647 else if (fixp->fx_addsy)
653 fprintf (stderr, "\n\nadjusting fixup:\n");
657 sym = fixp->fx_addsy;
659 /* All symbols should have already been resolved at this
660 point. It is possible to see unresolved expression
661 symbols, though, since they are not in the regular symbol
663 resolve_symbol_value (sym);
665 if (fixp->fx_subsy != NULL)
666 resolve_symbol_value (fixp->fx_subsy);
668 /* If this symbol is equated to an undefined or common symbol,
669 convert the fixup to being against that symbol. */
670 while (symbol_equated_reloc_p (sym)
671 || S_IS_WEAKREFR (sym))
673 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
676 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
677 fixp->fx_addsy = newsym;
681 if (symbol_mri_common_p (sym))
683 fixp->fx_offset += S_GET_VALUE (sym);
684 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
688 /* If the symbol is undefined, common, weak, or global (ELF
689 shared libs), we can't replace it with the section symbol. */
690 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
693 /* Is there some other (target cpu dependent) reason we can't adjust
694 this one? (E.g. relocations involving function addresses on
696 #ifdef tc_fix_adjustable
697 if (! tc_fix_adjustable (fixp))
701 /* Since we're reducing to section symbols, don't attempt to reduce
702 anything that's already using one. */
703 if (symbol_section_p (sym))
706 symsec = S_GET_SEGMENT (sym);
710 if (bfd_is_abs_section (symsec))
712 /* The fixup_segment routine normally will not use this
713 symbol in a relocation. */
717 /* Don't try to reduce relocs which refer to non-local symbols
718 in .linkonce sections. It can lead to confusion when a
719 debugging section refers to a .linkonce section. I hope
720 this will always be correct. */
721 if (symsec != sec && ! S_IS_LOCAL (sym))
723 if ((symsec->flags & SEC_LINK_ONCE) != 0
725 /* The GNU toolchain uses an extension for ELF: a
726 section beginning with the magic string
727 .gnu.linkonce is a linkonce section. */
728 && strncmp (segment_name (symsec), ".gnu.linkonce",
729 sizeof ".gnu.linkonce" - 1) == 0))
733 /* Never adjust a reloc against local symbol in a merge section
734 with non-zero addend. */
735 if ((symsec->flags & SEC_MERGE) != 0
736 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
739 /* Never adjust a reloc against TLS local symbol. */
740 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
743 /* We refetch the segment when calling section_symbol, rather
744 than using symsec, because S_GET_VALUE may wind up changing
745 the section when it calls resolve_symbol_value. */
746 fixp->fx_offset += S_GET_VALUE (sym);
747 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
749 fprintf (stderr, "\nadjusted fixup:\n");
754 dump_section_relocs (abfd, sec, stderr);
759 Go through all the fixS's in a segment and see which ones can be
760 handled now. (These consist of fixS where we have since discovered
761 the value of a symbol, or the address of the frag involved.)
762 For each one, call md_apply_fix to put the fix into the frag data.
764 Result is a count of how many relocation structs will be needed to
765 handle the remaining fixS's that we couldn't completely handle here.
766 These will be output later by emit_relocations(). */
769 fixup_segment (fixS *fixP, segT this_segment)
771 long seg_reloc_count = 0;
774 segT add_symbol_segment = absolute_section;
776 if (fixP != NULL && abs_section_sym == NULL)
777 abs_section_sym = section_symbol (absolute_section);
779 /* If the linker is doing the relaxing, we must not do any fixups.
781 Well, strictly speaking that's not true -- we could do any that
782 are PC-relative and don't cross regions that could change size.
783 And for the i960 we might be able to turn callx/callj into bal
784 anyways in cases where we know the maximum displacement. */
785 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
787 for (; fixP; fixP = fixP->fx_next)
790 if (fixP->fx_addsy == NULL)
792 /* There was no symbol required by this relocation.
793 However, BFD doesn't really handle relocations
794 without symbols well. So fake up a local symbol in
795 the absolute section. */
796 fixP->fx_addsy = abs_section_sym;
798 symbol_mark_used_in_reloc (fixP->fx_addsy);
799 if (fixP->fx_subsy != NULL)
800 symbol_mark_used_in_reloc (fixP->fx_subsy);
803 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
804 return seg_reloc_count;
807 for (; fixP; fixP = fixP->fx_next)
810 fprintf (stderr, "\nprocessing fixup:\n");
814 fragP = fixP->fx_frag;
816 #ifdef TC_VALIDATE_FIX
817 TC_VALIDATE_FIX (fixP, this_segment, skip);
819 add_number = fixP->fx_offset;
821 if (fixP->fx_addsy != NULL)
822 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
824 if (fixP->fx_subsy != NULL)
826 segT sub_symbol_segment;
827 resolve_symbol_value (fixP->fx_subsy);
828 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
829 if (fixP->fx_addsy != NULL
830 && sub_symbol_segment == add_symbol_segment
831 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
833 add_number += S_GET_VALUE (fixP->fx_addsy);
834 add_number -= S_GET_VALUE (fixP->fx_subsy);
835 fixP->fx_offset = add_number;
836 fixP->fx_addsy = NULL;
837 fixP->fx_subsy = NULL;
839 /* See the comment below about 68k weirdness. */
843 else if (sub_symbol_segment == absolute_section
844 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
846 add_number -= S_GET_VALUE (fixP->fx_subsy);
847 fixP->fx_offset = add_number;
848 fixP->fx_subsy = NULL;
850 else if (sub_symbol_segment == this_segment
851 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
853 add_number -= S_GET_VALUE (fixP->fx_subsy);
854 fixP->fx_offset = (add_number + fixP->fx_dot_value
855 + fixP->fx_frag->fr_address);
857 /* Make it pc-relative. If the back-end code has not
858 selected a pc-relative reloc, cancel the adjustment
859 we do later on all pc-relative relocs. */
862 /* Do this for m68k even if it's already described
863 as pc-relative. On the m68k, an operand of
864 "pc@(foo-.-2)" should address "foo" in a
869 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
870 fixP->fx_subsy = NULL;
873 else if (!TC_VALIDATE_FIX_SUB (fixP))
875 as_bad_where (fixP->fx_file, fixP->fx_line,
876 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
877 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
878 segment_name (add_symbol_segment),
879 S_GET_NAME (fixP->fx_subsy),
880 segment_name (sub_symbol_segment));
886 if (add_symbol_segment == this_segment
887 && !TC_FORCE_RELOCATION_LOCAL (fixP))
889 /* This fixup was made when the symbol's segment was
890 SEG_UNKNOWN, but it is now in the local segment.
891 So we know how to do the address without relocation. */
892 add_number += S_GET_VALUE (fixP->fx_addsy);
893 fixP->fx_offset = add_number;
895 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
896 fixP->fx_addsy = NULL;
899 else if (add_symbol_segment == absolute_section
900 && !TC_FORCE_RELOCATION_ABS (fixP))
902 add_number += S_GET_VALUE (fixP->fx_addsy);
903 fixP->fx_offset = add_number;
904 fixP->fx_addsy = NULL;
906 else if (add_symbol_segment != undefined_section
907 && ! bfd_is_com_section (add_symbol_segment)
908 && MD_APPLY_SYM_VALUE (fixP))
909 add_number += S_GET_VALUE (fixP->fx_addsy);
914 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
915 if (!fixP->fx_done && fixP->fx_addsy == NULL)
917 /* There was no symbol required by this relocation.
918 However, BFD doesn't really handle relocations
919 without symbols well. So fake up a local symbol in
920 the absolute section. */
921 fixP->fx_addsy = abs_section_sym;
926 md_apply_fix (fixP, &add_number, this_segment);
931 if (fixP->fx_addsy == NULL)
932 fixP->fx_addsy = abs_section_sym;
933 symbol_mark_used_in_reloc (fixP->fx_addsy);
934 if (fixP->fx_subsy != NULL)
935 symbol_mark_used_in_reloc (fixP->fx_subsy);
938 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
940 if (fixP->fx_size < sizeof (valueT))
945 mask--; /* Set all bits to one. */
946 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
947 if ((add_number & mask) != 0 && (add_number & mask) != mask)
949 char buf[50], buf2[50];
950 sprint_value (buf, fragP->fr_address + fixP->fx_where);
951 if (add_number > 1000)
952 sprint_value (buf2, add_number);
954 sprintf (buf2, "%ld", (long) add_number);
955 as_bad_where (fixP->fx_file, fixP->fx_line,
956 _("value of %s too large for field of %d bytes at %s"),
957 buf2, fixP->fx_size, buf);
958 } /* Generic error checking. */
960 #ifdef WARN_SIGNED_OVERFLOW_WORD
961 /* Warn if a .word value is too large when treated as a signed
962 number. We already know it is not too negative. This is to
963 catch over-large switches generated by gcc on the 68k. */
964 if (!flag_signed_overflow_ok
965 && fixP->fx_size == 2
966 && add_number > 0x7fff)
967 as_bad_where (fixP->fx_file, fixP->fx_line,
968 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
970 (long) (fragP->fr_address + fixP->fx_where));
972 } /* Not a bit fix. */
974 #ifdef TC_VALIDATE_FIX
975 skip: ATTRIBUTE_UNUSED_LABEL
979 fprintf (stderr, "result:\n");
982 } /* For each fixS in this segment. */
984 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
985 return seg_reloc_count;
989 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
991 void *xxx ATTRIBUTE_UNUSED)
993 segment_info_type *seginfo = seg_info (sec);
995 fixup_segment (seginfo->fix_root, sec);
999 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1000 char *file, unsigned int line)
1003 bfd_reloc_status_type s;
1005 s = bfd_install_relocation (stdoutput, reloc,
1006 fragp->fr_literal, fragp->fr_address,
1012 case bfd_reloc_overflow:
1013 as_bad_where (file, line, _("relocation overflow"));
1015 case bfd_reloc_outofrange:
1016 as_bad_where (file, line, _("relocation out of range"));
1019 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1025 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1027 segment_info_type *seginfo = seg_info (sec);
1033 /* If seginfo is NULL, we did not create this section; don't do
1034 anything with it. */
1035 if (seginfo == NULL)
1039 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1043 #ifdef RELOC_EXPANSION_POSSIBLE
1044 n *= MAX_RELOC_EXPANSION;
1047 relocs = xcalloc (n, sizeof (arelent *));
1050 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1059 fx_size = fixp->fx_size;
1060 slack = TC_FX_SIZE_SLACK (fixp);
1062 fx_size = fx_size > slack ? fx_size - slack : 0;
1063 loc = fixp->fx_where + fx_size;
1064 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1065 as_bad_where (fixp->fx_file, fixp->fx_line,
1066 _("internal error: fixup not contained within frag"));
1068 #ifndef RELOC_EXPANSION_POSSIBLE
1070 arelent *reloc = tc_gen_reloc (sec, fixp);
1074 relocs[i++] = reloc;
1079 arelent **reloc = tc_gen_reloc (sec, fixp);
1081 for (j = 0; reloc[j]; j++)
1082 relocs[i++] = reloc[j];
1086 for ( ; j != 0; --j)
1087 install_reloc (sec, relocs[i - j], fixp->fx_frag,
1088 fixp->fx_file, fixp->fx_line);
1094 unsigned int i, j, nsyms;
1096 sympp = bfd_get_outsymbols (stdoutput);
1097 nsyms = bfd_get_symcount (stdoutput);
1098 for (i = 0; i < n; i++)
1099 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1101 for (j = 0; j < nsyms; j++)
1102 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1112 flagword flags = bfd_get_section_flags (abfd, sec);
1114 bfd_set_section_flags (abfd, sec, flags);
1115 bfd_set_reloc (stdoutput, sec, relocs, n);
1118 #ifdef SET_SECTION_RELOCS
1119 SET_SECTION_RELOCS (sec, relocs, n);
1127 fprintf (stderr, "relocs for sec %s\n", sec->name);
1128 for (i = 0; i < n; i++)
1131 s = *r->sym_ptr_ptr;
1132 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1133 i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
1140 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1142 void *xxx ATTRIBUTE_UNUSED)
1144 segment_info_type *seginfo = seg_info (sec);
1145 addressT offset = 0;
1148 /* Write out the frags. */
1150 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1153 for (f = seginfo->frchainP->frch_root;
1162 assert (f->fr_type == rs_fill);
1165 x = bfd_set_section_contents (stdoutput, sec,
1166 f->fr_literal, (file_ptr) offset,
1167 (bfd_size_type) f->fr_fix);
1169 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1170 bfd_errmsg (bfd_get_error ()));
1171 offset += f->fr_fix;
1173 fill_literal = f->fr_literal + f->fr_fix;
1174 fill_size = f->fr_var;
1175 count = f->fr_offset;
1176 assert (count >= 0);
1177 if (fill_size && count)
1180 if (fill_size > sizeof (buf))
1182 /* Do it the old way. Can this ever happen? */
1185 x = bfd_set_section_contents (stdoutput, sec,
1188 (bfd_size_type) fill_size);
1190 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1191 bfd_errmsg (bfd_get_error ()));
1192 offset += fill_size;
1197 /* Build a buffer full of fill objects and output it as
1198 often as necessary. This saves on the overhead of
1199 potentially lots of bfd_set_section_contents calls. */
1203 n_per_buf = sizeof (buf);
1204 memset (buf, *fill_literal, n_per_buf);
1209 n_per_buf = sizeof (buf) / fill_size;
1210 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1211 memcpy (bufp, fill_literal, fill_size);
1213 for (; count > 0; count -= n_per_buf)
1215 n_per_buf = n_per_buf > count ? count : n_per_buf;
1216 x = bfd_set_section_contents
1217 (stdoutput, sec, buf, (file_ptr) offset,
1218 (bfd_size_type) n_per_buf * fill_size);
1220 as_fatal (_("cannot write to output file"));
1221 offset += n_per_buf * fill_size;
1229 merge_data_into_text (void)
1231 seg_info (text_section)->frchainP->frch_last->fr_next =
1232 seg_info (data_section)->frchainP->frch_root;
1233 seg_info (text_section)->frchainP->frch_last =
1234 seg_info (data_section)->frchainP->frch_last;
1235 seg_info (data_section)->frchainP = 0;
1246 /* Count symbols. We can't rely on a count made by the loop in
1247 write_object_file, because *_frob_file may add a new symbol or
1250 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1256 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1258 asympp = bfd_alloc (stdoutput, amt);
1259 symp = symbol_rootP;
1260 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1262 asympp[i] = symbol_get_bfdsym (symp);
1263 symbol_mark_written (symp);
1268 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1270 symbol_table_frozen = 1;
1273 /* Finish the subsegments. After every sub-segment, we fake an
1274 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1275 ".fill 0" because that is the kind of frag that requires least
1276 thought. ".align" frags like to have a following frag since that
1277 makes calculating their intended length trivial. */
1279 #ifndef SUB_SEGMENT_ALIGN
1281 /* The last subsegment gets an alignment corresponding to the alignment
1282 of the section. This allows proper nop-filling at the end of
1283 code-bearing sections. */
1284 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1285 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1287 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1292 subsegs_finish (void)
1294 struct frchain *frchainP;
1297 for (s = stdoutput->sections; s; s = s->next)
1299 segment_info_type *seginfo = seg_info (s);
1303 for (frchainP = seginfo->frchainP;
1305 frchainP = frchainP->frch_next)
1309 subseg_set (s, frchainP->frch_subseg);
1311 /* This now gets called even if we had errors. In that case,
1312 any alignment is meaningless, and, moreover, will look weird
1313 if we are generating a listing. */
1316 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1317 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1318 && now_seg->entsize)
1320 unsigned int entsize = now_seg->entsize;
1323 while ((entsize & 1) == 0)
1328 if (entalign > alignment)
1329 alignment = entalign;
1333 if (subseg_text_p (now_seg))
1334 frag_align_code (alignment, 0);
1336 frag_align (alignment, 0, 0);
1338 /* frag_align will have left a new frag.
1339 Use this last frag for an empty ".fill".
1341 For this segment ...
1342 Create a last frag. Do not leave a "being filled in frag". */
1343 frag_wane (frag_now);
1344 frag_now->fr_fix = 0;
1345 know (frag_now->fr_next == NULL);
1350 /* Write the object file. */
1353 write_object_file (void)
1355 struct relax_seg_info rsi;
1356 #ifndef WORKING_DOT_WORD
1357 fragS *fragP; /* Track along all frags. */
1360 /* Do we really want to write it? */
1362 int n_warns, n_errs;
1363 n_warns = had_warnings ();
1364 n_errs = had_errors ();
1365 /* The -Z flag indicates that an object file should be generated,
1366 regardless of warnings and errors. */
1367 if (flag_always_generate_output)
1369 if (n_warns || n_errs)
1370 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1371 n_errs, n_errs == 1 ? "" : "s",
1372 n_warns, n_warns == 1 ? "" : "s");
1377 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1378 n_errs, n_errs == 1 ? "" : "s",
1379 n_warns, n_warns == 1 ? "" : "s");
1384 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1385 a routine to check for the definition of the procedure "_main",
1386 and if so -- fix it up so that it can be program entry point. */
1387 vms_check_for_main ();
1388 #endif /* OBJ_VMS */
1390 /* From now on, we don't care about sub-segments. Build one frag chain
1391 for each segment. Linked thru fr_next. */
1393 /* Remove the sections created by gas for its own purposes. */
1397 bfd_section_list_remove (stdoutput, reg_section);
1398 bfd_section_list_remove (stdoutput, expr_section);
1399 stdoutput->section_count -= 2;
1401 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1404 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1406 /* We have two segments. If user gave -R flag, then we must put the
1407 data frags into the text segment. Do this before relaxing so
1408 we know to take advantage of -R and make shorter addresses. */
1409 if (flag_readonly_data_in_text)
1411 merge_data_into_text ();
1417 #ifndef WORKING_DOT_WORD
1418 /* We need to reset the markers in the broken word list and
1419 associated frags between calls to relax_segment (via
1420 relax_seg). Since the broken word list is global, we do it
1421 once per round, rather than locally in relax_segment for each
1423 struct broken_word *brokp;
1425 for (brokp = broken_words;
1426 brokp != (struct broken_word *) NULL;
1427 brokp = brokp->next_broken_word)
1431 if (brokp->dispfrag != (fragS *) NULL
1432 && brokp->dispfrag->fr_type == rs_broken_word)
1433 brokp->dispfrag->fr_subtype = 0;
1438 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1444 /* Note - Most ports will use the default value of
1445 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1446 local symbols to be resolved, removing their frag information.
1447 Some ports however, will not have finished relaxing all of
1448 their frags and will still need the local symbol frag
1449 information. These ports can set
1450 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1451 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1453 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1455 /* Relaxation has completed. Freeze all syms. */
1458 #ifdef md_post_relax_hook
1462 #ifndef WORKING_DOT_WORD
1464 struct broken_word *lie;
1465 struct broken_word **prevP;
1467 prevP = &broken_words;
1468 for (lie = broken_words; lie; lie = lie->next_broken_word)
1473 subseg_change (lie->seg, lie->subseg);
1474 exp.X_op = O_subtract;
1475 exp.X_add_symbol = lie->add;
1476 exp.X_op_symbol = lie->sub;
1477 exp.X_add_number = lie->addnum;
1478 #ifdef TC_CONS_FIX_NEW
1479 TC_CONS_FIX_NEW (lie->frag,
1480 lie->word_goes_here - lie->frag->fr_literal,
1483 fix_new_exp (lie->frag,
1484 lie->word_goes_here - lie->frag->fr_literal,
1485 2, &exp, 0, BFD_RELOC_16);
1487 *prevP = lie->next_broken_word;
1490 prevP = &(lie->next_broken_word);
1492 for (lie = broken_words; lie;)
1494 struct broken_word *untruth;
1496 addressT table_addr;
1497 addressT from_addr, to_addr;
1500 subseg_change (lie->seg, lie->subseg);
1501 fragP = lie->dispfrag;
1503 /* Find out how many broken_words go here. */
1506 untruth && untruth->dispfrag == fragP;
1507 untruth = untruth->next_broken_word)
1508 if (untruth->added == 1)
1511 table_ptr = lie->dispfrag->fr_opcode;
1512 table_addr = (lie->dispfrag->fr_address
1513 + (table_ptr - lie->dispfrag->fr_literal));
1514 /* Create the jump around the long jumps. This is a short
1515 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1516 from_addr = table_addr;
1517 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1518 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1520 table_ptr += md_short_jump_size;
1521 table_addr += md_short_jump_size;
1524 lie && lie->dispfrag == fragP;
1525 m++, lie = lie->next_broken_word)
1527 if (lie->added == 2)
1529 /* Patch the jump table. */
1530 /* This is the offset from ??? to table_ptr+0. */
1531 to_addr = table_addr - S_GET_VALUE (lie->sub);
1532 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1533 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1535 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1536 for (untruth = lie->next_broken_word;
1537 untruth && untruth->dispfrag == fragP;
1538 untruth = untruth->next_broken_word)
1540 if (untruth->use_jump == lie)
1541 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1544 /* Install the long jump. */
1545 /* This is a long jump from table_ptr+0 to the final target. */
1546 from_addr = table_addr;
1547 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1548 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1550 table_ptr += md_long_jump_size;
1551 table_addr += md_long_jump_size;
1555 #endif /* not WORKING_DOT_WORD */
1557 /* Resolve symbol values. This needs to be done before processing
1563 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1564 resolve_symbol_value (symp);
1566 resolve_local_symbol_values ();
1570 #ifdef tc_frob_file_before_adjust
1571 tc_frob_file_before_adjust ();
1573 #ifdef obj_frob_file_before_adjust
1574 obj_frob_file_before_adjust ();
1577 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1579 #ifdef tc_frob_file_before_fix
1580 tc_frob_file_before_fix ();
1582 #ifdef obj_frob_file_before_fix
1583 obj_frob_file_before_fix ();
1586 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1588 /* Set up symbol table, and write it out. */
1592 bfd_boolean skip_next_symbol = FALSE;
1594 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1599 if (skip_next_symbol)
1601 /* Don't do anything besides moving the value of the
1602 symbol from the GAS value-field to the BFD value-field. */
1603 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1604 skip_next_symbol = FALSE;
1608 if (symbol_mri_common_p (symp))
1610 if (S_IS_EXTERNAL (symp))
1611 as_bad (_("%s: global symbols not supported in common sections"),
1613 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1617 name = S_GET_NAME (symp);
1621 decode_local_label_name ((char *) S_GET_NAME (symp));
1622 /* They only differ if `name' is a fb or dollar local
1624 if (name2 != name && ! S_IS_DEFINED (symp))
1625 as_bad (_("local label `%s' is not defined"), name2);
1628 /* Do it again, because adjust_reloc_syms might introduce
1629 more symbols. They'll probably only be section symbols,
1630 but they'll still need to have the values computed. */
1631 resolve_symbol_value (symp);
1633 /* Skip symbols which were equated to undefined or common
1635 if (symbol_equated_reloc_p (symp)
1636 || S_IS_WEAKREFR (symp))
1638 const char *name = S_GET_NAME (symp);
1639 if (S_IS_COMMON (symp)
1640 && !TC_FAKE_LABEL (name)
1641 && !S_IS_WEAKREFR (symp)
1642 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1644 expressionS *e = symbol_get_value_expression (symp);
1645 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1646 name, S_GET_NAME (e->X_add_symbol));
1648 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1652 #ifdef obj_frob_symbol
1653 obj_frob_symbol (symp, punt);
1655 #ifdef tc_frob_symbol
1656 if (! punt || symbol_used_in_reloc_p (symp))
1657 tc_frob_symbol (symp, punt);
1660 /* If we don't want to keep this symbol, splice it out of
1661 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1662 want section symbols. Otherwise, we skip local symbols
1663 and symbols that the frob_symbol macros told us to punt,
1664 but we keep such symbols if they are used in relocs. */
1665 if (symp == abs_section_sym
1666 || (! EMIT_SECTION_SYMBOLS
1667 && symbol_section_p (symp))
1668 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1669 opposites. Sometimes the former checks flags and the
1670 latter examines the name... */
1671 || (!S_IS_EXTERNAL (symp)
1672 && (punt || S_IS_LOCAL (symp) ||
1673 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
1674 && ! symbol_used_in_reloc_p (symp)))
1676 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1678 /* After symbol_remove, symbol_next(symp) still returns
1679 the one that came after it in the chain. So we don't
1680 need to do any extra cleanup work here. */
1684 /* Make sure we really got a value for the symbol. */
1685 if (! symbol_resolved_p (symp))
1687 as_bad (_("can't resolve value for symbol `%s'"),
1689 symbol_mark_resolved (symp);
1692 /* Set the value into the BFD symbol. Up til now the value
1693 has only been kept in the gas symbolS struct. */
1694 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1696 /* A warning construct is a warning symbol followed by the
1697 symbol warned about. Don't let anything object-format or
1698 target-specific muck with it; it's ready for output. */
1699 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1700 skip_next_symbol = TRUE;
1706 /* Now do any format-specific adjustments to the symbol table, such
1707 as adding file symbols. */
1708 #ifdef tc_adjust_symtab
1709 tc_adjust_symtab ();
1711 #ifdef obj_adjust_symtab
1712 obj_adjust_symtab ();
1715 /* Now that all the sizes are known, and contents correct, we can
1716 start writing to the file. */
1719 /* If *_frob_file changes the symbol value at this point, it is
1720 responsible for moving the changed value into symp->bsym->value
1721 as well. Hopefully all symbol value changing can be done in
1726 #ifdef obj_frob_file
1730 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1732 #ifdef tc_frob_file_after_relocs
1733 tc_frob_file_after_relocs ();
1735 #ifdef obj_frob_file_after_relocs
1736 obj_frob_file_after_relocs ();
1739 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1742 #ifdef TC_GENERIC_RELAX_TABLE
1743 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1746 relax_frag (segT segment, fragS *fragP, long stretch)
1748 const relax_typeS *this_type;
1749 const relax_typeS *start_type;
1750 relax_substateT next_state;
1751 relax_substateT this_state;
1757 const relax_typeS *table;
1759 target = fragP->fr_offset;
1760 address = fragP->fr_address;
1761 table = TC_GENERIC_RELAX_TABLE;
1762 this_state = fragP->fr_subtype;
1763 start_type = this_type = table + this_state;
1764 symbolP = fragP->fr_symbol;
1770 sym_frag = symbol_get_frag (symbolP);
1772 #ifndef DIFF_EXPR_OK
1773 know (sym_frag != NULL);
1775 know (S_GET_SEGMENT (symbolP) != absolute_section
1776 || sym_frag == &zero_address_frag);
1777 target += S_GET_VALUE (symbolP);
1779 /* If frag has yet to be reached on this pass,
1780 assume it will move by STRETCH just as we did.
1781 If this is not so, it will be because some frag
1782 between grows, and that will force another pass. */
1785 && sym_frag->relax_marker != fragP->relax_marker
1786 && S_GET_SEGMENT (symbolP) == segment)
1792 aim = target - address - fragP->fr_fix;
1793 #ifdef TC_PCREL_ADJUST
1794 /* Currently only the ns32k family needs this. */
1795 aim += TC_PCREL_ADJUST (fragP);
1798 #ifdef md_prepare_relax_scan
1799 /* Formerly called M68K_AIM_KLUDGE. */
1800 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
1805 /* Look backwards. */
1806 for (next_state = this_type->rlx_more; next_state;)
1807 if (aim >= this_type->rlx_backward)
1811 /* Grow to next state. */
1812 this_state = next_state;
1813 this_type = table + this_state;
1814 next_state = this_type->rlx_more;
1819 /* Look forwards. */
1820 for (next_state = this_type->rlx_more; next_state;)
1821 if (aim <= this_type->rlx_forward)
1825 /* Grow to next state. */
1826 this_state = next_state;
1827 this_type = table + this_state;
1828 next_state = this_type->rlx_more;
1832 growth = this_type->rlx_length - start_type->rlx_length;
1834 fragP->fr_subtype = this_state;
1838 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
1840 /* Relax_align. Advance location counter to next address that has 'alignment'
1841 lowest order bits all 0s, return size of adjustment made. */
1842 static relax_addressT
1843 relax_align (register relax_addressT address, /* Address now. */
1844 register int alignment /* Alignment (binary). */)
1846 relax_addressT mask;
1847 relax_addressT new_address;
1849 mask = ~((~0) << alignment);
1850 new_address = (address + mask) & (~mask);
1851 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1853 /* We must provide lots of padding, so the linker can discard it
1854 when needed. The linker will not add extra space, ever. */
1855 new_address += (1 << alignment);
1857 return (new_address - address);
1860 /* Now we have a segment, not a crowd of sub-segments, we can make
1865 After this, all frags in this segment have addresses that are correct
1866 within the segment. Since segments live in different file addresses,
1867 these frag addresses may not be the same as final object-file
1871 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
1873 unsigned long frag_count;
1875 relax_addressT address;
1878 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1879 subseg_change (segment, 0);
1881 /* For each frag in segment: count and store (a 1st guess of)
1884 for (frag_count = 0, fragP = segment_frag_root;
1886 fragP = fragP->fr_next, frag_count ++)
1888 fragP->relax_marker = 0;
1889 fragP->fr_address = address;
1890 address += fragP->fr_fix;
1892 switch (fragP->fr_type)
1895 address += fragP->fr_offset * fragP->fr_var;
1902 addressT offset = relax_align (address, (int) fragP->fr_offset);
1904 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
1907 if (offset % fragP->fr_var != 0)
1909 as_bad_where (fragP->fr_file, fragP->fr_line,
1910 _("alignment padding (%lu bytes) not a multiple of %ld"),
1911 (unsigned long) offset, (long) fragP->fr_var);
1912 offset -= (offset % fragP->fr_var);
1921 /* Assume .org is nugatory. It will grow with 1st relax. */
1924 case rs_machine_dependent:
1925 /* If fr_symbol is an expression, this call to
1926 resolve_symbol_value sets up the correct segment, which will
1927 likely be needed in md_estimate_size_before_relax. */
1928 if (fragP->fr_symbol)
1929 resolve_symbol_value (fragP->fr_symbol);
1931 address += md_estimate_size_before_relax (fragP, segment);
1934 #ifndef WORKING_DOT_WORD
1935 /* Broken words don't concern us yet. */
1936 case rs_broken_word:
1941 /* Initial guess is always 1; doing otherwise can result in
1942 stable solutions that are larger than the minimum. */
1943 address += fragP->fr_offset = 1;
1947 address += eh_frame_estimate_size_before_relax (fragP);
1951 address += dwarf2dbg_estimate_size_before_relax (fragP);
1955 BAD_CASE (fragP->fr_type);
1962 unsigned long max_iterations;
1964 /* Cumulative address adjustment. */
1967 /* Have we made any adjustment this pass? We can't just test
1968 stretch because one piece of code may have grown and another
1972 /* Most horrible, but gcc may give us some exception data that
1973 is impossible to assemble, of the form
1977 .uleb128 end - start
1983 If the leb128 is two bytes in size, then end-start is 128*128,
1984 which requires a three byte leb128. If the leb128 is three
1985 bytes in size, then end-start is 128*128-1, which requires a
1986 two byte leb128. We work around this dilemma by inserting
1987 an extra 4 bytes of alignment just after the .align. This
1988 works because the data after the align is accessed relative to
1991 This counter is used in a tiny state machine to detect
1992 whether a leb128 followed by an align is impossible to
1994 int rs_leb128_fudge = 0;
1996 /* We want to prevent going into an infinite loop where one frag grows
1997 depending upon the location of a symbol which is in turn moved by
1998 the growing frag. eg:
2004 So we dictate that this algorithm can be at most O2. */
2005 max_iterations = frag_count * frag_count;
2006 /* Check for overflow. */
2007 if (max_iterations < frag_count)
2008 max_iterations = frag_count;
2016 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2019 addressT was_address;
2023 fragP->relax_marker ^= 1;
2024 was_address = fragP->fr_address;
2025 address = fragP->fr_address += stretch;
2026 symbolP = fragP->fr_symbol;
2027 offset = fragP->fr_offset;
2029 switch (fragP->fr_type)
2031 case rs_fill: /* .fill never relaxes. */
2035 #ifndef WORKING_DOT_WORD
2036 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2037 for it I do not want to write it. I do not want to have
2038 anything to do with it. This is not the proper way to
2039 implement this misfeature. */
2040 case rs_broken_word:
2042 struct broken_word *lie;
2043 struct broken_word *untruth;
2045 /* Yes this is ugly (storing the broken_word pointer
2046 in the symbol slot). Still, this whole chunk of
2047 code is ugly, and I don't feel like doing anything
2048 about it. Think of it as stubbornness in action. */
2050 for (lie = (struct broken_word *) (fragP->fr_symbol);
2051 lie && lie->dispfrag == fragP;
2052 lie = lie->next_broken_word)
2058 offset = (S_GET_VALUE (lie->add)
2060 - S_GET_VALUE (lie->sub));
2061 if (offset <= -32768 || offset >= 32767)
2063 if (flag_warn_displacement)
2066 sprint_value (buf, (addressT) lie->addnum);
2067 as_warn_where (fragP->fr_file, fragP->fr_line,
2068 _(".word %s-%s+%s didn't fit"),
2069 S_GET_NAME (lie->add),
2070 S_GET_NAME (lie->sub),
2074 if (fragP->fr_subtype == 0)
2076 fragP->fr_subtype++;
2077 growth += md_short_jump_size;
2079 for (untruth = lie->next_broken_word;
2080 untruth && untruth->dispfrag == lie->dispfrag;
2081 untruth = untruth->next_broken_word)
2082 if ((symbol_get_frag (untruth->add)
2083 == symbol_get_frag (lie->add))
2084 && (S_GET_VALUE (untruth->add)
2085 == S_GET_VALUE (lie->add)))
2088 untruth->use_jump = lie;
2090 growth += md_long_jump_size;
2095 } /* case rs_broken_word */
2101 addressT oldoff, newoff;
2103 oldoff = relax_align (was_address + fragP->fr_fix,
2105 newoff = relax_align (address + fragP->fr_fix,
2108 if (fragP->fr_subtype != 0)
2110 if (oldoff > fragP->fr_subtype)
2112 if (newoff > fragP->fr_subtype)
2116 growth = newoff - oldoff;
2118 /* If this align happens to follow a leb128 and
2119 we have determined that the leb128 is bouncing
2120 in size, then break the cycle by inserting an
2123 && (rs_leb128_fudge & 16) != 0
2124 && (rs_leb128_fudge & 15) >= 2)
2126 segment_info_type *seginfo = seg_info (segment);
2127 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2130 newf = frag_alloc (ob);
2131 obstack_blank_fast (ob, fragP->fr_var);
2132 obstack_finish (ob);
2133 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2134 memcpy (newf->fr_literal,
2135 fragP->fr_literal + fragP->fr_fix,
2137 newf->fr_type = rs_fill;
2139 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2141 if (newf->fr_offset * newf->fr_var
2142 != (offsetT) 1 << fragP->fr_offset)
2144 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2147 /* Include growth of new frag, because rs_fill
2148 frags don't normally grow. */
2149 growth += newf->fr_offset * newf->fr_var;
2150 /* The new frag address is newoff. Adjust this
2151 for the amount we'll add when we process the
2153 newf->fr_address = newoff - stretch - growth;
2154 newf->relax_marker ^= 1;
2155 fragP->fr_next = newf;
2157 as_warn (_("padding added"));
2165 addressT target = offset;
2170 /* Convert from an actual address to an octet offset
2171 into the section. Here it is assumed that the
2172 section's VMA is zero, and can omit subtracting it
2173 from the symbol's value to get the address offset. */
2174 know (S_GET_SEGMENT (symbolP)->vma == 0);
2175 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2178 know (fragP->fr_next);
2179 after = fragP->fr_next->fr_address;
2180 growth = target - after;
2185 /* Don't error on first few frag relax passes.
2186 The symbol might be an expression involving
2187 symbol values from other sections. If those
2188 sections have not yet been processed their
2189 frags will all have zero addresses, so we
2190 will calculate incorrect values for them. The
2191 number of passes we allow before giving an
2192 error is somewhat arbitrary. It should be at
2193 least one, with larger values requiring
2194 increasingly contrived dependencies between
2195 frags to trigger a false error. */
2198 /* Force another pass. */
2203 /* Growth may be negative, but variable part of frag
2204 cannot have fewer than 0 chars. That is, we can't
2206 as_bad_where (fragP->fr_file, fragP->fr_line,
2207 _("attempt to move .org backwards"));
2209 /* We've issued an error message. Change the
2210 frag to avoid cascading errors. */
2211 fragP->fr_type = rs_align;
2212 fragP->fr_subtype = 0;
2213 fragP->fr_offset = 0;
2214 fragP->fr_fix = after - was_address;
2218 /* This is an absolute growth factor */
2229 amount = S_GET_VALUE (symbolP);
2230 if (S_GET_SEGMENT (symbolP) != absolute_section
2231 || S_IS_COMMON (symbolP)
2232 || ! S_IS_DEFINED (symbolP))
2234 as_bad_where (fragP->fr_file, fragP->fr_line,
2235 _(".space specifies non-absolute value"));
2236 /* Prevent repeat of this error message. */
2237 fragP->fr_symbol = 0;
2239 else if (amount < 0)
2241 /* Don't error on first few frag relax passes.
2242 See rs_org comment for a longer explanation. */
2249 as_warn_where (fragP->fr_file, fragP->fr_line,
2250 _(".space or .fill with negative value, ignored"));
2251 fragP->fr_symbol = 0;
2254 growth = (was_address + fragP->fr_fix + amount
2255 - fragP->fr_next->fr_address);
2259 case rs_machine_dependent:
2260 #ifdef md_relax_frag
2261 growth = md_relax_frag (segment, fragP, stretch);
2263 #ifdef TC_GENERIC_RELAX_TABLE
2264 /* The default way to relax a frag is to look through
2265 TC_GENERIC_RELAX_TABLE. */
2266 growth = relax_frag (segment, fragP, stretch);
2267 #endif /* TC_GENERIC_RELAX_TABLE */
2276 value = resolve_symbol_value (fragP->fr_symbol);
2277 size = sizeof_leb128 (value, fragP->fr_subtype);
2278 growth = size - fragP->fr_offset;
2279 fragP->fr_offset = size;
2284 growth = eh_frame_relax_frag (fragP);
2288 growth = dwarf2dbg_relax_frag (fragP);
2292 BAD_CASE (fragP->fr_type);
2299 if (fragP->fr_type == rs_leb128)
2300 rs_leb128_fudge += 16;
2301 else if (fragP->fr_type == rs_align
2302 && (rs_leb128_fudge & 16) != 0
2304 rs_leb128_fudge += 16;
2306 rs_leb128_fudge = 0;
2311 && (rs_leb128_fudge & 16) == 0
2312 && (rs_leb128_fudge & -16) != 0)
2313 rs_leb128_fudge += 1;
2315 rs_leb128_fudge = 0;
2317 /* Until nothing further to relax. */
2318 while (stretched && -- max_iterations);
2321 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2322 segment_name (segment));
2325 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2326 if (fragP->last_fr_address != fragP->fr_address)
2328 fragP->last_fr_address = fragP->fr_address;
2335 number_to_chars_bigendian (char *buf, valueT val, int n)
2341 buf[n] = val & 0xff;
2347 number_to_chars_littleendian (char *buf, valueT val, int n)
2353 *buf++ = val & 0xff;
2359 write_print_statistics (FILE *file)
2361 fprintf (file, "fixups: %d\n", n_fixups);
2364 /* For debugging. */
2365 extern int indent_level;
2368 print_fixup (fixS *fixp)
2371 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2373 fprintf (stderr, " pcrel");
2374 if (fixp->fx_pcrel_adjust)
2375 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2376 if (fixp->fx_im_disp)
2379 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2381 fprintf (stderr, " im_disp");
2385 fprintf (stderr, " tcbit");
2387 fprintf (stderr, " done");
2388 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2389 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2390 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2391 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2395 fprintf (stderr, "\n +<");
2396 print_symbol_value_1 (stderr, fixp->fx_addsy);
2397 fprintf (stderr, ">");
2401 fprintf (stderr, "\n -<");
2402 print_symbol_value_1 (stderr, fixp->fx_subsy);
2403 fprintf (stderr, ">");
2405 fprintf (stderr, "\n");
2406 #ifdef TC_FIX_DATA_PRINT
2407 TC_FIX_DATA_PRINT (stderr, fixp);