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, 2008, 2009,
4 2010, 2011, 2012 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 3, 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"
31 #include "compress-debug.h"
33 #ifndef TC_ADJUST_RELOC_COUNT
34 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
37 #ifndef TC_FORCE_RELOCATION
38 #define TC_FORCE_RELOCATION(FIX) \
39 (generic_force_reloc (FIX))
42 #ifndef TC_FORCE_RELOCATION_ABS
43 #define TC_FORCE_RELOCATION_ABS(FIX) \
44 (TC_FORCE_RELOCATION (FIX))
47 #ifndef TC_FORCE_RELOCATION_LOCAL
48 #define TC_FORCE_RELOCATION_LOCAL(FIX) \
50 || TC_FORCE_RELOCATION (FIX))
53 #ifndef TC_FORCE_RELOCATION_SUB_SAME
54 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
58 #ifndef md_register_arithmetic
59 # define md_register_arithmetic 1
62 #ifndef TC_FORCE_RELOCATION_SUB_ABS
63 #define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG) \
64 (!md_register_arithmetic && (SEG) == reg_section)
67 #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
69 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) \
70 (!md_register_arithmetic && (SEG) == reg_section)
72 #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) 1
76 #ifndef TC_VALIDATE_FIX_SUB
77 #ifdef UNDEFINED_DIFFERENCE_OK
78 /* The PA needs this for PIC code generation. */
79 #define TC_VALIDATE_FIX_SUB(FIX, SEG) \
80 (md_register_arithmetic || (SEG) != reg_section)
82 #define TC_VALIDATE_FIX_SUB(FIX, SEG) \
83 ((md_register_arithmetic || (SEG) != reg_section) \
84 && ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
85 || (FIX)->fx_r_type == BFD_RELOC_GPREL16))
89 #ifndef TC_LINKRELAX_FIXUP
90 #define TC_LINKRELAX_FIXUP(SEG) 1
93 #ifndef MD_APPLY_SYM_VALUE
94 #define MD_APPLY_SYM_VALUE(FIX) 1
97 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
98 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
101 #ifndef MD_PCREL_FROM_SECTION
102 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
105 #ifndef TC_FAKE_LABEL
106 #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
109 /* Positive values of TC_FX_SIZE_SLACK allow a target to define
110 fixups that far past the end of a frag. Having such fixups
111 is of course most most likely a bug in setting fx_size correctly.
112 A negative value disables the fixup check entirely, which is
113 appropriate for something like the Renesas / SuperH SH_COUNT
115 #ifndef TC_FX_SIZE_SLACK
116 #define TC_FX_SIZE_SLACK(FIX) 0
119 /* Used to control final evaluation of expressions. */
120 int finalize_syms = 0;
122 int symbol_table_frozen;
124 symbolS *abs_section_sym;
126 /* Remember the value of dot when parsing expressions. */
129 /* Relocs generated by ".reloc" pseudo. */
130 struct reloc_list* reloc_list;
132 void print_fixup (fixS *);
134 /* We generally attach relocs to frag chains. However, after we have
135 chained these all together into a segment, any relocs we add after
136 that must be attached to a segment. This will include relocs added
137 in md_estimate_size_for_relax, for example. */
138 static int frags_chained = 0;
142 #define RELOC_ENUM enum bfd_reloc_code_real
144 /* Create a fixS in obstack 'notes'. */
147 fix_new_internal (fragS *frag, /* Which frag? */
148 int where, /* Where in that frag? */
149 int size, /* 1, 2, or 4 usually. */
150 symbolS *add_symbol, /* X_add_symbol. */
151 symbolS *sub_symbol, /* X_op_symbol. */
152 offsetT offset, /* X_add_number. */
153 int pcrel, /* TRUE if PC-relative relocation. */
154 RELOC_ENUM r_type /* Relocation type. */,
155 int at_beginning) /* Add to the start of the list? */
161 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
163 fixP->fx_frag = frag;
164 fixP->fx_where = where;
165 fixP->fx_size = size;
166 /* We've made fx_size a narrow field; check that it's wide enough. */
167 if (fixP->fx_size != size)
169 as_bad (_("field fx_size too small to hold %d"), size);
172 fixP->fx_addsy = add_symbol;
173 fixP->fx_subsy = sub_symbol;
174 fixP->fx_offset = offset;
175 fixP->fx_dot_value = dot_value;
176 fixP->fx_pcrel = pcrel;
177 fixP->fx_r_type = r_type;
178 fixP->fx_im_disp = 0;
179 fixP->fx_pcrel_adjust = 0;
180 fixP->fx_bit_fixP = 0;
181 fixP->fx_addnumber = 0;
185 fixP->fx_no_overflow = 0;
189 fixP->fx_cgen.insn = NULL;
190 fixP->fx_cgen.opinfo = 0;
194 TC_INIT_FIX_DATA (fixP);
197 as_where (&fixP->fx_file, &fixP->fx_line);
201 fixS **seg_fix_rootP = (frags_chained
202 ? &seg_info (now_seg)->fix_root
203 : &frchain_now->fix_root);
204 fixS **seg_fix_tailP = (frags_chained
205 ? &seg_info (now_seg)->fix_tail
206 : &frchain_now->fix_tail);
210 fixP->fx_next = *seg_fix_rootP;
211 *seg_fix_rootP = fixP;
212 if (fixP->fx_next == NULL)
213 *seg_fix_tailP = fixP;
217 fixP->fx_next = NULL;
219 (*seg_fix_tailP)->fx_next = fixP;
221 *seg_fix_rootP = fixP;
222 *seg_fix_tailP = fixP;
229 /* Create a fixup relative to a symbol (plus a constant). */
232 fix_new (fragS *frag, /* Which frag? */
233 int where, /* Where in that frag? */
234 int size, /* 1, 2, or 4 usually. */
235 symbolS *add_symbol, /* X_add_symbol. */
236 offsetT offset, /* X_add_number. */
237 int pcrel, /* TRUE if PC-relative relocation. */
238 RELOC_ENUM r_type /* Relocation type. */)
240 return fix_new_internal (frag, where, size, add_symbol,
241 (symbolS *) NULL, offset, pcrel, r_type, FALSE);
244 /* Create a fixup for an expression. Currently we only support fixups
245 for difference expressions. That is itself more than most object
246 file formats support anyhow. */
249 fix_new_exp (fragS *frag, /* Which frag? */
250 int where, /* Where in that frag? */
251 int size, /* 1, 2, or 4 usually. */
252 expressionS *exp, /* Expression. */
253 int pcrel, /* TRUE if PC-relative relocation. */
254 RELOC_ENUM r_type /* Relocation type. */)
266 as_bad (_("register value used as expression"));
270 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
271 the difference expression cannot immediately be reduced. */
273 symbolS *stmp = make_expr_symbol (exp);
275 exp->X_op = O_symbol;
276 exp->X_op_symbol = 0;
277 exp->X_add_symbol = stmp;
278 exp->X_add_number = 0;
280 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
284 add = exp->X_add_symbol;
285 off = exp->X_add_number;
286 r_type = BFD_RELOC_RVA;
290 sub = exp->X_add_symbol;
291 off = exp->X_add_number;
295 sub = exp->X_op_symbol;
298 add = exp->X_add_symbol;
301 off = exp->X_add_number;
305 add = make_expr_symbol (exp);
309 return fix_new_internal (frag, where, size, add, sub, off, pcrel,
313 /* Create a fixup at the beginning of FRAG. The arguments are the same
314 as for fix_new, except that WHERE is implicitly 0. */
317 fix_at_start (fragS *frag, int size, symbolS *add_symbol,
318 offsetT offset, int pcrel, RELOC_ENUM r_type)
320 return fix_new_internal (frag, 0, size, add_symbol,
321 (symbolS *) NULL, offset, pcrel, r_type, TRUE);
324 /* Generic function to determine whether a fixup requires a relocation. */
326 generic_force_reloc (fixS *fix)
328 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
329 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
332 if (fix->fx_addsy == NULL)
335 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
338 /* Append a string onto another string, bumping the pointer along. */
340 append (char **charPP, char *fromP, unsigned long length)
342 /* Don't trust memcpy() of 0 chars. */
346 memcpy (*charPP, fromP, length);
350 /* This routine records the largest alignment seen for each segment.
351 If the beginning of the segment is aligned on the worst-case
352 boundary, all of the other alignments within it will work. At
353 least one object format really uses this info. */
356 record_alignment (/* Segment to which alignment pertains. */
358 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
359 boundary, 2 => 4-byte boundary, etc.) */
362 if (seg == absolute_section)
365 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
366 bfd_set_section_alignment (stdoutput, seg, align);
370 get_recorded_alignment (segT seg)
372 if (seg == absolute_section)
375 return bfd_get_section_alignment (stdoutput, seg);
378 /* Reset the section indices after removing the gas created sections. */
381 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
383 int *countp = (int *) countparg;
385 sec->index = *countp;
390 chain_frchains_together_1 (segT section, struct frchain *frchp)
392 fragS dummy, *prev_frag = &dummy;
393 fixS fix_dummy, *prev_fix = &fix_dummy;
395 for (; frchp; frchp = frchp->frch_next)
397 prev_frag->fr_next = frchp->frch_root;
398 prev_frag = frchp->frch_last;
399 gas_assert (prev_frag->fr_type != 0);
400 if (frchp->fix_root != (fixS *) NULL)
402 if (seg_info (section)->fix_root == (fixS *) NULL)
403 seg_info (section)->fix_root = frchp->fix_root;
404 prev_fix->fx_next = frchp->fix_root;
405 seg_info (section)->fix_tail = frchp->fix_tail;
406 prev_fix = frchp->fix_tail;
409 gas_assert (prev_frag->fr_type != 0);
410 gas_assert (prev_frag != &dummy);
411 prev_frag->fr_next = 0;
416 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
418 void *xxx ATTRIBUTE_UNUSED)
420 segment_info_type *info;
422 /* BFD may have introduced its own sections without using
423 subseg_new, so it is possible that seg_info is NULL. */
424 info = seg_info (section);
425 if (info != (segment_info_type *) NULL)
426 info->frchainP->frch_last
427 = chain_frchains_together_1 (section, info->frchainP);
429 /* Now that we've chained the frags together, we must add new fixups
430 to the segment, not to the frag chain. */
435 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
437 switch (fragP->fr_type)
445 HANDLE_ALIGN (fragP);
447 know (fragP->fr_next != NULL);
448 fragP->fr_offset = (fragP->fr_next->fr_address
450 - fragP->fr_fix) / fragP->fr_var;
451 if (fragP->fr_offset < 0)
453 as_bad_where (fragP->fr_file, fragP->fr_line,
454 _("attempt to .org/.space backwards? (%ld)"),
455 (long) fragP->fr_offset);
456 fragP->fr_offset = 0;
458 fragP->fr_type = rs_fill;
466 valueT value = S_GET_VALUE (fragP->fr_symbol);
469 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
472 fragP->fr_fix += size;
473 fragP->fr_type = rs_fill;
475 fragP->fr_offset = 0;
476 fragP->fr_symbol = NULL;
481 eh_frame_convert_frag (fragP);
485 dwarf2dbg_convert_frag (fragP);
488 case rs_machine_dependent:
489 md_convert_frag (stdoutput, sec, fragP);
491 gas_assert (fragP->fr_next == NULL
492 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
495 /* After md_convert_frag, we make the frag into a ".space 0".
496 md_convert_frag() should set up any fixSs and constants
501 #ifndef WORKING_DOT_WORD
504 struct broken_word *lie;
506 if (fragP->fr_subtype)
508 fragP->fr_fix += md_short_jump_size;
509 for (lie = (struct broken_word *) (fragP->fr_symbol);
510 lie && lie->dispfrag == fragP;
511 lie = lie->next_broken_word)
513 fragP->fr_fix += md_long_jump_size;
521 BAD_CASE (fragP->fr_type);
525 md_frag_check (fragP);
529 struct relax_seg_info
536 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
538 segment_info_type *seginfo = seg_info (sec);
539 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
541 if (seginfo && seginfo->frchainP
542 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
547 size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
551 segment_info_type *seginfo;
553 valueT size, newsize;
555 subseg_change (sec, 0);
557 seginfo = seg_info (sec);
558 if (seginfo && seginfo->frchainP)
560 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
561 cvt_frag_to_fill (sec, fragp);
562 for (fragp = seginfo->frchainP->frch_root;
564 fragp = fragp->fr_next)
565 /* Walk to last elt. */
567 size = fragp->fr_address + fragp->fr_fix;
572 flags = bfd_get_section_flags (abfd, sec);
573 if (size == 0 && bfd_get_section_size (sec) != 0 &&
574 (flags & SEC_HAS_CONTENTS) != 0)
577 if (size > 0 && ! seginfo->bss)
578 flags |= SEC_HAS_CONTENTS;
581 x = bfd_set_section_flags (abfd, sec, flags);
584 newsize = md_section_align (sec, size);
585 x = bfd_set_section_size (abfd, sec, newsize);
588 /* If the size had to be rounded up, add some padding in the last
590 gas_assert (newsize >= size);
593 fragS *last = seginfo->frchainP->frch_last;
594 fragp = seginfo->frchainP->frch_root;
595 while (fragp->fr_next != last)
596 fragp = fragp->fr_next;
597 last->fr_address = size;
598 if ((newsize - size) % fragp->fr_var == 0)
599 fragp->fr_offset += (newsize - size) / fragp->fr_var;
601 /* If we hit this abort, it's likely due to subsegs_finish not
602 providing sufficient alignment on the last frag, and the
603 machine dependent code using alignment frags with fr_var
608 #ifdef tc_frob_section
609 tc_frob_section (sec);
611 #ifdef obj_frob_section
612 obj_frob_section (sec);
618 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
620 segment_info_type *seginfo = seg_info (sec);
621 fixS *fixp = seginfo->fix_root;
626 fprintf (stream, "sec %s relocs:\n", sec->name);
629 symbolS *s = fixp->fx_addsy;
631 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
632 (int) fixp->fx_r_type);
634 fprintf (stream, "no sym\n");
637 print_symbol_value_1 (stream, s);
638 fprintf (stream, "\n");
640 fixp = fixp->fx_next;
644 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
647 #ifndef EMIT_SECTION_SYMBOLS
648 #define EMIT_SECTION_SYMBOLS 1
651 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
652 and check for validity. Convert RELOC_LIST from using U.A fields
655 resolve_reloc_expr_symbols (void)
657 bfd_vma addr_mask = 1;
658 struct reloc_list *r;
660 /* Avoid a shift by the width of type. */
661 addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1;
665 for (r = reloc_list; r; r = r->next)
667 reloc_howto_type *howto = r->u.a.howto;
670 bfd_vma offset, addend;
673 resolve_symbol_value (r->u.a.offset_sym);
674 symval = symbol_get_value_expression (r->u.a.offset_sym);
678 if (symval->X_op == O_constant)
679 sym = r->u.a.offset_sym;
680 else if (symval->X_op == O_symbol)
682 sym = symval->X_add_symbol;
683 offset = symval->X_add_number;
684 symval = symbol_get_value_expression (symval->X_add_symbol);
687 || symval->X_op != O_constant
688 || (sec = S_GET_SEGMENT (sym)) == NULL
689 || !SEG_NORMAL (sec))
691 as_bad_where (r->file, r->line, _("invalid offset expression"));
695 offset += S_GET_VALUE (sym);
698 addend = r->u.a.addend;
699 if (r->u.a.sym != NULL)
701 resolve_symbol_value (r->u.a.sym);
702 symval = symbol_get_value_expression (r->u.a.sym);
703 if (symval->X_op == O_constant)
705 else if (symval->X_op == O_symbol)
707 sym = symval->X_add_symbol;
708 addend += symval->X_add_number;
709 symval = symbol_get_value_expression (symval->X_add_symbol);
711 if (symval->X_op != O_constant)
713 as_bad_where (r->file, r->line, _("invalid reloc expression"));
716 else if (sym != NULL)
718 /* Convert relocs against local symbols to refer to the
719 corresponding section symbol plus offset instead. Keep
720 PC-relative relocs of the REL variety intact though to
721 prevent the offset from overflowing the relocated field,
722 unless it has enough bits to cover the whole address
724 if (S_IS_LOCAL (sym) && !symbol_section_p (sym)
725 && !(howto->partial_inplace
726 && howto->pc_relative
727 && howto->src_mask != addr_mask))
729 asection *symsec = S_GET_SEGMENT (sym);
730 if (!(((symsec->flags & SEC_MERGE) != 0
732 || (symsec->flags & SEC_THREAD_LOCAL) != 0))
734 addend += S_GET_VALUE (sym);
735 sym = section_symbol (symsec);
738 symbol_mark_used_in_reloc (sym);
743 if (abs_section_sym == NULL)
744 abs_section_sym = section_symbol (absolute_section);
745 sym = abs_section_sym;
749 r->u.b.s = symbol_get_bfdsym (sym);
750 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
751 r->u.b.r.address = offset;
752 r->u.b.r.addend = addend;
753 r->u.b.r.howto = howto;
757 /* This pass over fixups decides whether symbols can be replaced with
761 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
763 void *xxx ATTRIBUTE_UNUSED)
765 segment_info_type *seginfo = seg_info (sec);
771 dump_section_relocs (abfd, sec, stderr);
773 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
777 else if (fixp->fx_addsy)
783 fprintf (stderr, "\n\nadjusting fixup:\n");
787 sym = fixp->fx_addsy;
789 /* All symbols should have already been resolved at this
790 point. It is possible to see unresolved expression
791 symbols, though, since they are not in the regular symbol
793 resolve_symbol_value (sym);
795 if (fixp->fx_subsy != NULL)
796 resolve_symbol_value (fixp->fx_subsy);
798 /* If this symbol is equated to an undefined or common symbol,
799 convert the fixup to being against that symbol. */
800 while (symbol_equated_reloc_p (sym)
801 || S_IS_WEAKREFR (sym))
803 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
806 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
807 fixp->fx_addsy = newsym;
811 if (symbol_mri_common_p (sym))
813 fixp->fx_offset += S_GET_VALUE (sym);
814 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
818 /* If the symbol is undefined, common, weak, or global (ELF
819 shared libs), we can't replace it with the section symbol. */
820 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
823 /* Is there some other (target cpu dependent) reason we can't adjust
824 this one? (E.g. relocations involving function addresses on
826 #ifdef tc_fix_adjustable
827 if (! tc_fix_adjustable (fixp))
831 /* Since we're reducing to section symbols, don't attempt to reduce
832 anything that's already using one. */
833 if (symbol_section_p (sym))
836 symsec = S_GET_SEGMENT (sym);
840 if (bfd_is_abs_section (symsec))
842 /* The fixup_segment routine normally will not use this
843 symbol in a relocation. */
847 /* Don't try to reduce relocs which refer to non-local symbols
848 in .linkonce sections. It can lead to confusion when a
849 debugging section refers to a .linkonce section. I hope
850 this will always be correct. */
851 if (symsec != sec && ! S_IS_LOCAL (sym))
853 if ((symsec->flags & SEC_LINK_ONCE) != 0
855 /* The GNU toolchain uses an extension for ELF: a
856 section beginning with the magic string
857 .gnu.linkonce is a linkonce section. */
858 && strncmp (segment_name (symsec), ".gnu.linkonce",
859 sizeof ".gnu.linkonce" - 1) == 0))
863 /* Never adjust a reloc against local symbol in a merge section
864 with non-zero addend. */
865 if ((symsec->flags & SEC_MERGE) != 0
866 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
869 /* Never adjust a reloc against TLS local symbol. */
870 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
873 /* We refetch the segment when calling section_symbol, rather
874 than using symsec, because S_GET_VALUE may wind up changing
875 the section when it calls resolve_symbol_value. */
876 fixp->fx_offset += S_GET_VALUE (sym);
877 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
879 fprintf (stderr, "\nadjusted fixup:\n");
884 dump_section_relocs (abfd, sec, stderr);
889 Go through all the fixS's in a segment and see which ones can be
890 handled now. (These consist of fixS where we have since discovered
891 the value of a symbol, or the address of the frag involved.)
892 For each one, call md_apply_fix to put the fix into the frag data.
894 Result is a count of how many relocation structs will be needed to
895 handle the remaining fixS's that we couldn't completely handle here.
896 These will be output later by emit_relocations(). */
899 fixup_segment (fixS *fixP, segT this_segment)
901 long seg_reloc_count = 0;
904 segT add_symbol_segment = absolute_section;
906 if (fixP != NULL && abs_section_sym == NULL)
907 abs_section_sym = section_symbol (absolute_section);
909 /* If the linker is doing the relaxing, we must not do any fixups.
911 Well, strictly speaking that's not true -- we could do any that
912 are PC-relative and don't cross regions that could change size.
913 And for the i960 we might be able to turn callx/callj into bal
914 anyways in cases where we know the maximum displacement. */
915 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
917 for (; fixP; fixP = fixP->fx_next)
920 if (fixP->fx_addsy == NULL)
922 /* There was no symbol required by this relocation.
923 However, BFD doesn't really handle relocations
924 without symbols well. So fake up a local symbol in
925 the absolute section. */
926 fixP->fx_addsy = abs_section_sym;
928 symbol_mark_used_in_reloc (fixP->fx_addsy);
929 if (fixP->fx_subsy != NULL)
930 symbol_mark_used_in_reloc (fixP->fx_subsy);
933 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
934 return seg_reloc_count;
937 for (; fixP; fixP = fixP->fx_next)
940 fprintf (stderr, "\nprocessing fixup:\n");
944 fragP = fixP->fx_frag;
946 #ifdef TC_VALIDATE_FIX
947 TC_VALIDATE_FIX (fixP, this_segment, skip);
949 add_number = fixP->fx_offset;
951 if (fixP->fx_addsy != NULL)
952 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
954 if (fixP->fx_subsy != NULL)
956 segT sub_symbol_segment;
957 resolve_symbol_value (fixP->fx_subsy);
958 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
959 if (fixP->fx_addsy != NULL
960 && sub_symbol_segment == add_symbol_segment
961 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
962 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
963 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
965 add_number += S_GET_VALUE (fixP->fx_addsy);
966 add_number -= S_GET_VALUE (fixP->fx_subsy);
967 fixP->fx_offset = add_number;
968 fixP->fx_addsy = NULL;
969 fixP->fx_subsy = NULL;
971 /* See the comment below about 68k weirdness. */
975 else if (sub_symbol_segment == absolute_section
976 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
977 && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
979 add_number -= S_GET_VALUE (fixP->fx_subsy);
980 fixP->fx_offset = add_number;
981 fixP->fx_subsy = NULL;
983 else if (sub_symbol_segment == this_segment
984 && !S_FORCE_RELOC (fixP->fx_subsy, 0)
985 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
987 add_number -= S_GET_VALUE (fixP->fx_subsy);
988 fixP->fx_offset = (add_number + fixP->fx_dot_value
989 + fixP->fx_frag->fr_address);
991 /* Make it pc-relative. If the back-end code has not
992 selected a pc-relative reloc, cancel the adjustment
993 we do later on all pc-relative relocs. */
996 /* Do this for m68k even if it's already described
997 as pc-relative. On the m68k, an operand of
998 "pc@(foo-.-2)" should address "foo" in a
1003 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
1004 fixP->fx_subsy = NULL;
1007 else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
1009 if (!md_register_arithmetic
1010 && (add_symbol_segment == reg_section
1011 || sub_symbol_segment == reg_section))
1012 as_bad_where (fixP->fx_file, fixP->fx_line,
1013 _("register value used as expression"));
1015 as_bad_where (fixP->fx_file, fixP->fx_line,
1016 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
1017 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
1018 segment_name (add_symbol_segment),
1019 S_GET_NAME (fixP->fx_subsy),
1020 segment_name (sub_symbol_segment));
1022 else if (sub_symbol_segment != undefined_section
1023 && ! bfd_is_com_section (sub_symbol_segment)
1024 && MD_APPLY_SYM_VALUE (fixP))
1025 add_number -= S_GET_VALUE (fixP->fx_subsy);
1030 if (add_symbol_segment == this_segment
1031 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1032 && !TC_FORCE_RELOCATION_LOCAL (fixP))
1034 /* This fixup was made when the symbol's segment was
1035 SEG_UNKNOWN, but it is now in the local segment.
1036 So we know how to do the address without relocation. */
1037 add_number += S_GET_VALUE (fixP->fx_addsy);
1038 fixP->fx_offset = add_number;
1040 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1041 fixP->fx_addsy = NULL;
1044 else if (add_symbol_segment == absolute_section
1045 && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1046 && !TC_FORCE_RELOCATION_ABS (fixP))
1048 add_number += S_GET_VALUE (fixP->fx_addsy);
1049 fixP->fx_offset = add_number;
1050 fixP->fx_addsy = NULL;
1052 else if (add_symbol_segment != undefined_section
1053 && ! bfd_is_com_section (add_symbol_segment)
1054 && MD_APPLY_SYM_VALUE (fixP))
1055 add_number += S_GET_VALUE (fixP->fx_addsy);
1060 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
1061 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1063 /* There was no symbol required by this relocation.
1064 However, BFD doesn't really handle relocations
1065 without symbols well. So fake up a local symbol in
1066 the absolute section. */
1067 fixP->fx_addsy = abs_section_sym;
1072 md_apply_fix (fixP, &add_number, this_segment);
1077 if (fixP->fx_addsy == NULL)
1078 fixP->fx_addsy = abs_section_sym;
1079 symbol_mark_used_in_reloc (fixP->fx_addsy);
1080 if (fixP->fx_subsy != NULL)
1081 symbol_mark_used_in_reloc (fixP->fx_subsy);
1084 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
1086 if (fixP->fx_size < sizeof (valueT))
1091 mask--; /* Set all bits to one. */
1092 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1093 if ((add_number & mask) != 0 && (add_number & mask) != mask)
1095 char buf[50], buf2[50];
1096 sprint_value (buf, fragP->fr_address + fixP->fx_where);
1097 if (add_number > 1000)
1098 sprint_value (buf2, add_number);
1100 sprintf (buf2, "%ld", (long) add_number);
1101 as_bad_where (fixP->fx_file, fixP->fx_line,
1102 _("value of %s too large for field of %d bytes at %s"),
1103 buf2, fixP->fx_size, buf);
1104 } /* Generic error checking. */
1106 #ifdef WARN_SIGNED_OVERFLOW_WORD
1107 /* Warn if a .word value is too large when treated as a signed
1108 number. We already know it is not too negative. This is to
1109 catch over-large switches generated by gcc on the 68k. */
1110 if (!flag_signed_overflow_ok
1111 && fixP->fx_size == 2
1112 && add_number > 0x7fff)
1113 as_bad_where (fixP->fx_file, fixP->fx_line,
1114 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1116 (long) (fragP->fr_address + fixP->fx_where));
1118 } /* Not a bit fix. */
1120 #ifdef TC_VALIDATE_FIX
1121 skip: ATTRIBUTE_UNUSED_LABEL
1125 fprintf (stderr, "result:\n");
1128 } /* For each fixS in this segment. */
1130 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
1131 return seg_reloc_count;
1135 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1137 void *xxx ATTRIBUTE_UNUSED)
1139 segment_info_type *seginfo = seg_info (sec);
1141 fixup_segment (seginfo->fix_root, sec);
1145 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1146 char *file, unsigned int line)
1149 bfd_reloc_status_type s;
1152 if (reloc->sym_ptr_ptr != NULL
1153 && (sym = *reloc->sym_ptr_ptr) != NULL
1154 && (sym->flags & BSF_KEEP) == 0
1155 && ((sym->flags & BSF_SECTION_SYM) == 0
1156 || (EMIT_SECTION_SYMBOLS
1157 && !bfd_is_abs_section (sym->section))))
1158 as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
1160 s = bfd_install_relocation (stdoutput, reloc,
1161 fragp->fr_literal, fragp->fr_address,
1167 case bfd_reloc_overflow:
1168 as_bad_where (file, line, _("relocation overflow"));
1170 case bfd_reloc_outofrange:
1171 as_bad_where (file, line, _("relocation out of range"));
1174 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1180 get_frag_for_reloc (fragS *last_frag,
1181 const segment_info_type *seginfo,
1182 const struct reloc_list *r)
1186 for (f = last_frag; f != NULL; f = f->fr_next)
1187 if (f->fr_address <= r->u.b.r.address
1188 && r->u.b.r.address < f->fr_address + f->fr_fix)
1191 for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
1192 if (f->fr_address <= r->u.b.r.address
1193 && r->u.b.r.address < f->fr_address + f->fr_fix)
1196 as_bad_where (r->file, r->line,
1197 _("reloc not within (fixed part of) section"));
1202 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1204 segment_info_type *seginfo = seg_info (sec);
1206 struct reloc_list *my_reloc_list, **rp, *r;
1211 /* If seginfo is NULL, we did not create this section; don't do
1212 anything with it. */
1213 if (seginfo == NULL)
1217 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1221 #ifdef RELOC_EXPANSION_POSSIBLE
1222 n *= MAX_RELOC_EXPANSION;
1225 /* Extract relocs for this section from reloc_list. */
1227 my_reloc_list = NULL;
1228 while ((r = *rp) != NULL)
1230 if (r->u.b.sec == sec)
1233 r->next = my_reloc_list;
1241 relocs = (arelent **) xcalloc (n, sizeof (arelent *));
1246 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1251 #ifndef RELOC_EXPANSION_POSSIBLE
1260 fx_size = fixp->fx_size;
1261 slack = TC_FX_SIZE_SLACK (fixp);
1263 fx_size = fx_size > slack ? fx_size - slack : 0;
1264 loc = fixp->fx_where + fx_size;
1265 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1266 as_bad_where (fixp->fx_file, fixp->fx_line,
1267 _("internal error: fixup not contained within frag"));
1269 #ifndef RELOC_EXPANSION_POSSIBLE
1270 *reloc = tc_gen_reloc (sec, fixp);
1272 reloc = tc_gen_reloc (sec, fixp);
1277 while (r != NULL && r->u.b.r.address < (*reloc)->address)
1279 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1283 relocs[n++] = &r->u.b.r;
1284 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1288 relocs[n++] = *reloc;
1289 install_reloc (sec, *reloc, fixp->fx_frag,
1290 fixp->fx_file, fixp->fx_line);
1291 #ifndef RELOC_EXPANSION_POSSIBLE
1301 fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
1305 relocs[n++] = &r->u.b.r;
1306 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1313 unsigned int k, j, nsyms;
1315 sympp = bfd_get_outsymbols (stdoutput);
1316 nsyms = bfd_get_symcount (stdoutput);
1317 for (k = 0; k < n; k++)
1318 if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1320 for (j = 0; j < nsyms; j++)
1321 if (sympp[j] == *relocs[k]->sym_ptr_ptr)
1331 flagword flags = bfd_get_section_flags (abfd, sec);
1333 bfd_set_section_flags (abfd, sec, flags);
1334 bfd_set_reloc (stdoutput, sec, relocs, n);
1337 #ifdef SET_SECTION_RELOCS
1338 SET_SECTION_RELOCS (sec, relocs, n);
1345 fprintf (stderr, "relocs for sec %s\n", sec->name);
1346 for (k = 0; k < n; k++)
1348 arelent *rel = relocs[k];
1349 asymbol *s = *rel->sym_ptr_ptr;
1350 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1351 k, rel, (unsigned long)rel->address, s->name,
1352 (unsigned long)rel->addend);
1359 compress_frag (struct z_stream_s *strm, const char *contents, int in_size,
1360 fragS **last_newf, struct obstack *ob)
1363 int total_out_size = 0;
1364 fragS *f = *last_newf;
1368 /* Call the compression routine repeatedly until it has finished
1369 processing the frag. */
1372 /* Reserve all the space available in the current chunk.
1373 If none is available, start a new frag. */
1374 avail_out = obstack_room (ob);
1377 obstack_finish (ob);
1378 f = frag_alloc (ob);
1379 f->fr_type = rs_fill;
1380 (*last_newf)->fr_next = f;
1382 avail_out = obstack_room (ob);
1385 as_fatal (_("can't extend frag"));
1386 next_out = obstack_next_free (ob);
1387 obstack_blank_fast (ob, avail_out);
1388 out_size = compress_data (strm, &contents, &in_size,
1389 &next_out, &avail_out);
1393 f->fr_fix += out_size;
1394 total_out_size += out_size;
1396 /* Return unused space. */
1398 obstack_blank_fast (ob, -avail_out);
1401 return total_out_size;
1405 compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1407 segment_info_type *seginfo = seg_info (sec);
1411 struct obstack *ob = &seginfo->frchainP->frch_obstack;
1412 bfd_size_type uncompressed_size = (bfd_size_type) sec->size;
1413 bfd_size_type compressed_size;
1414 const char *section_name;
1415 char *compressed_name;
1417 struct z_stream_s *strm;
1419 flagword flags = bfd_get_section_flags (abfd, sec);
1423 || (flags & (SEC_ALLOC | SEC_HAS_CONTENTS)) == SEC_ALLOC)
1426 section_name = bfd_get_section_name (stdoutput, sec);
1427 if (strncmp (section_name, ".debug_", 7) != 0)
1430 strm = compress_init ();
1434 /* Create a new frag to contain the "ZLIB" header. */
1435 first_newf = frag_alloc (ob);
1436 if (obstack_room (ob) < 12)
1437 first_newf = frag_alloc (ob);
1438 if (obstack_room (ob) < 12)
1439 as_fatal (_("can't extend frag %u chars"), 12);
1440 last_newf = first_newf;
1441 obstack_blank_fast (ob, 12);
1442 last_newf->fr_type = rs_fill;
1443 last_newf->fr_fix = 12;
1444 header = last_newf->fr_literal;
1445 memcpy (header, "ZLIB", 4);
1446 header[11] = uncompressed_size; uncompressed_size >>= 8;
1447 header[10] = uncompressed_size; uncompressed_size >>= 8;
1448 header[9] = uncompressed_size; uncompressed_size >>= 8;
1449 header[8] = uncompressed_size; uncompressed_size >>= 8;
1450 header[7] = uncompressed_size; uncompressed_size >>= 8;
1451 header[6] = uncompressed_size; uncompressed_size >>= 8;
1452 header[5] = uncompressed_size; uncompressed_size >>= 8;
1453 header[4] = uncompressed_size;
1454 compressed_size = 12;
1456 /* Stream the frags through the compression engine, adding new frags
1457 as necessary to accomodate the compressed output. */
1458 for (f = seginfo->frchainP->frch_root;
1467 gas_assert (f->fr_type == rs_fill);
1470 out_size = compress_frag (strm, f->fr_literal, f->fr_fix,
1474 compressed_size += out_size;
1476 fill_literal = f->fr_literal + f->fr_fix;
1477 fill_size = f->fr_var;
1478 count = f->fr_offset;
1479 gas_assert (count >= 0);
1480 if (fill_size && count)
1484 out_size = compress_frag (strm, fill_literal, (int) fill_size,
1488 compressed_size += out_size;
1493 /* Flush the compression state. */
1500 /* Reserve all the space available in the current chunk.
1501 If none is available, start a new frag. */
1502 avail_out = obstack_room (ob);
1507 obstack_finish (ob);
1508 newf = frag_alloc (ob);
1509 newf->fr_type = rs_fill;
1510 last_newf->fr_next = newf;
1512 avail_out = obstack_room (ob);
1515 as_fatal (_("can't extend frag"));
1516 next_out = obstack_next_free (ob);
1517 obstack_blank_fast (ob, avail_out);
1518 x = compress_finish (strm, &next_out, &avail_out, &out_size);
1522 last_newf->fr_fix += out_size;
1523 compressed_size += out_size;
1525 /* Return unused space. */
1527 obstack_blank_fast (ob, -avail_out);
1533 /* Replace the uncompressed frag list with the compressed frag list. */
1534 seginfo->frchainP->frch_root = first_newf;
1535 seginfo->frchainP->frch_last = last_newf;
1537 /* Update the section size and its name. */
1538 x = bfd_set_section_size (abfd, sec, compressed_size);
1540 compressed_name = (char *) xmalloc (strlen (section_name) + 2);
1541 compressed_name[0] = '.';
1542 compressed_name[1] = 'z';
1543 strcpy (compressed_name + 2, section_name + 1);
1544 bfd_section_name (stdoutput, sec) = compressed_name;
1548 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1550 void *xxx ATTRIBUTE_UNUSED)
1552 segment_info_type *seginfo = seg_info (sec);
1553 addressT offset = 0;
1556 /* Write out the frags. */
1558 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1561 for (f = seginfo->frchainP->frch_root;
1570 gas_assert (f->fr_type == rs_fill);
1573 x = bfd_set_section_contents (stdoutput, sec,
1574 f->fr_literal, (file_ptr) offset,
1575 (bfd_size_type) f->fr_fix);
1577 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1578 bfd_errmsg (bfd_get_error ()));
1579 offset += f->fr_fix;
1581 fill_literal = f->fr_literal + f->fr_fix;
1582 fill_size = f->fr_var;
1583 count = f->fr_offset;
1584 gas_assert (count >= 0);
1585 if (fill_size && count)
1588 if (fill_size > sizeof (buf))
1590 /* Do it the old way. Can this ever happen? */
1593 x = bfd_set_section_contents (stdoutput, sec,
1596 (bfd_size_type) fill_size);
1598 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1599 bfd_errmsg (bfd_get_error ()));
1600 offset += fill_size;
1605 /* Build a buffer full of fill objects and output it as
1606 often as necessary. This saves on the overhead of
1607 potentially lots of bfd_set_section_contents calls. */
1611 n_per_buf = sizeof (buf);
1612 memset (buf, *fill_literal, n_per_buf);
1617 n_per_buf = sizeof (buf) / fill_size;
1618 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1619 memcpy (bufp, fill_literal, fill_size);
1621 for (; count > 0; count -= n_per_buf)
1623 n_per_buf = n_per_buf > count ? count : n_per_buf;
1624 x = bfd_set_section_contents
1625 (stdoutput, sec, buf, (file_ptr) offset,
1626 (bfd_size_type) n_per_buf * fill_size);
1628 as_fatal (_("cannot write to output file '%s': %s"),
1629 stdoutput->filename,
1630 bfd_errmsg (bfd_get_error ()));
1631 offset += n_per_buf * fill_size;
1639 merge_data_into_text (void)
1641 seg_info (text_section)->frchainP->frch_last->fr_next =
1642 seg_info (data_section)->frchainP->frch_root;
1643 seg_info (text_section)->frchainP->frch_last =
1644 seg_info (data_section)->frchainP->frch_last;
1645 seg_info (data_section)->frchainP = 0;
1656 /* Count symbols. We can't rely on a count made by the loop in
1657 write_object_file, because *_frob_file may add a new symbol or
1660 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1666 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1668 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1669 symp = symbol_rootP;
1670 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1672 asympp[i] = symbol_get_bfdsym (symp);
1673 if (asympp[i]->flags != BSF_SECTION_SYM
1674 || !(bfd_is_const_section (asympp[i]->section)
1675 && asympp[i]->section->symbol == asympp[i]))
1676 asympp[i]->flags |= BSF_KEEP;
1677 symbol_mark_written (symp);
1682 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1683 gas_assert (result);
1684 symbol_table_frozen = 1;
1687 /* Finish the subsegments. After every sub-segment, we fake an
1688 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1689 ".fill 0" because that is the kind of frag that requires least
1690 thought. ".align" frags like to have a following frag since that
1691 makes calculating their intended length trivial. */
1693 #ifndef SUB_SEGMENT_ALIGN
1695 /* The last subsegment gets an alignment corresponding to the alignment
1696 of the section. This allows proper nop-filling at the end of
1697 code-bearing sections. */
1698 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1699 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1701 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1706 subsegs_finish (void)
1708 struct frchain *frchainP;
1711 for (s = stdoutput->sections; s; s = s->next)
1713 segment_info_type *seginfo = seg_info (s);
1717 for (frchainP = seginfo->frchainP;
1719 frchainP = frchainP->frch_next)
1723 subseg_set (s, frchainP->frch_subseg);
1725 /* This now gets called even if we had errors. In that case,
1726 any alignment is meaningless, and, moreover, will look weird
1727 if we are generating a listing. */
1730 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1731 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1732 && now_seg->entsize)
1734 unsigned int entsize = now_seg->entsize;
1737 while ((entsize & 1) == 0)
1742 if (entalign > alignment)
1743 alignment = entalign;
1747 if (subseg_text_p (now_seg))
1748 frag_align_code (alignment, 0);
1750 frag_align (alignment, 0, 0);
1752 /* frag_align will have left a new frag.
1753 Use this last frag for an empty ".fill".
1755 For this segment ...
1756 Create a last frag. Do not leave a "being filled in frag". */
1757 frag_wane (frag_now);
1758 frag_now->fr_fix = 0;
1759 know (frag_now->fr_next == NULL);
1764 /* Write the object file. */
1767 write_object_file (void)
1769 struct relax_seg_info rsi;
1770 #ifndef WORKING_DOT_WORD
1771 fragS *fragP; /* Track along all frags. */
1774 #ifdef md_pre_output_hook
1778 /* Do we really want to write it? */
1780 int n_warns, n_errs;
1781 n_warns = had_warnings ();
1782 n_errs = had_errors ();
1783 /* The -Z flag indicates that an object file should be generated,
1784 regardless of warnings and errors. */
1785 if (flag_always_generate_output)
1787 if (n_warns || n_errs)
1788 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1789 n_errs, n_errs == 1 ? "" : "s",
1790 n_warns, n_warns == 1 ? "" : "s");
1795 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1796 n_errs, n_errs == 1 ? "" : "s",
1797 n_warns, n_warns == 1 ? "" : "s");
1801 #ifdef md_pre_relax_hook
1805 /* From now on, we don't care about sub-segments. Build one frag chain
1806 for each segment. Linked thru fr_next. */
1808 /* Remove the sections created by gas for its own purposes. */
1812 bfd_section_list_remove (stdoutput, reg_section);
1813 bfd_section_list_remove (stdoutput, expr_section);
1814 stdoutput->section_count -= 2;
1816 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1819 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1821 /* We have two segments. If user gave -R flag, then we must put the
1822 data frags into the text segment. Do this before relaxing so
1823 we know to take advantage of -R and make shorter addresses. */
1824 if (flag_readonly_data_in_text)
1826 merge_data_into_text ();
1832 #ifndef WORKING_DOT_WORD
1833 /* We need to reset the markers in the broken word list and
1834 associated frags between calls to relax_segment (via
1835 relax_seg). Since the broken word list is global, we do it
1836 once per round, rather than locally in relax_segment for each
1838 struct broken_word *brokp;
1840 for (brokp = broken_words;
1841 brokp != (struct broken_word *) NULL;
1842 brokp = brokp->next_broken_word)
1846 if (brokp->dispfrag != (fragS *) NULL
1847 && brokp->dispfrag->fr_type == rs_broken_word)
1848 brokp->dispfrag->fr_subtype = 0;
1853 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1859 /* Note - Most ports will use the default value of
1860 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1861 local symbols to be resolved, removing their frag information.
1862 Some ports however, will not have finished relaxing all of
1863 their frags and will still need the local symbol frag
1864 information. These ports can set
1865 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1866 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1868 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1870 /* Relaxation has completed. Freeze all syms. */
1873 #ifdef md_post_relax_hook
1877 #ifndef WORKING_DOT_WORD
1879 struct broken_word *lie;
1880 struct broken_word **prevP;
1882 prevP = &broken_words;
1883 for (lie = broken_words; lie; lie = lie->next_broken_word)
1888 subseg_change (lie->seg, lie->subseg);
1889 exp.X_op = O_subtract;
1890 exp.X_add_symbol = lie->add;
1891 exp.X_op_symbol = lie->sub;
1892 exp.X_add_number = lie->addnum;
1893 #ifdef TC_CONS_FIX_NEW
1894 TC_CONS_FIX_NEW (lie->frag,
1895 lie->word_goes_here - lie->frag->fr_literal,
1898 fix_new_exp (lie->frag,
1899 lie->word_goes_here - lie->frag->fr_literal,
1900 2, &exp, 0, BFD_RELOC_16);
1902 *prevP = lie->next_broken_word;
1905 prevP = &(lie->next_broken_word);
1907 for (lie = broken_words; lie;)
1909 struct broken_word *untruth;
1911 addressT table_addr;
1912 addressT from_addr, to_addr;
1915 subseg_change (lie->seg, lie->subseg);
1916 fragP = lie->dispfrag;
1918 /* Find out how many broken_words go here. */
1921 untruth && untruth->dispfrag == fragP;
1922 untruth = untruth->next_broken_word)
1923 if (untruth->added == 1)
1926 table_ptr = lie->dispfrag->fr_opcode;
1927 table_addr = (lie->dispfrag->fr_address
1928 + (table_ptr - lie->dispfrag->fr_literal));
1929 /* Create the jump around the long jumps. This is a short
1930 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1931 from_addr = table_addr;
1932 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1933 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1935 table_ptr += md_short_jump_size;
1936 table_addr += md_short_jump_size;
1939 lie && lie->dispfrag == fragP;
1940 m++, lie = lie->next_broken_word)
1942 if (lie->added == 2)
1944 /* Patch the jump table. */
1945 for (untruth = (struct broken_word *) (fragP->fr_symbol);
1946 untruth && untruth->dispfrag == fragP;
1947 untruth = untruth->next_broken_word)
1949 if (untruth->use_jump == lie)
1951 /* This is the offset from ??? to table_ptr+0.
1952 The target is the same for all users of this
1953 md_long_jump, but the "sub" bases (and hence the
1954 offsets) may be different. */
1955 addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
1956 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1957 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
1959 md_number_to_chars (untruth->word_goes_here, to_word, 2);
1963 /* Install the long jump. */
1964 /* This is a long jump from table_ptr+0 to the final target. */
1965 from_addr = table_addr;
1966 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1967 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1969 table_ptr += md_long_jump_size;
1970 table_addr += md_long_jump_size;
1974 #endif /* not WORKING_DOT_WORD */
1976 /* Resolve symbol values. This needs to be done before processing
1982 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1983 resolve_symbol_value (symp);
1985 resolve_local_symbol_values ();
1986 resolve_reloc_expr_symbols ();
1990 #ifdef tc_frob_file_before_adjust
1991 tc_frob_file_before_adjust ();
1993 #ifdef obj_frob_file_before_adjust
1994 obj_frob_file_before_adjust ();
1997 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1999 #ifdef tc_frob_file_before_fix
2000 tc_frob_file_before_fix ();
2002 #ifdef obj_frob_file_before_fix
2003 obj_frob_file_before_fix ();
2006 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
2008 /* Set up symbol table, and write it out. */
2012 bfd_boolean skip_next_symbol = FALSE;
2014 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2019 if (skip_next_symbol)
2021 /* Don't do anything besides moving the value of the
2022 symbol from the GAS value-field to the BFD value-field. */
2023 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2024 skip_next_symbol = FALSE;
2028 if (symbol_mri_common_p (symp))
2030 if (S_IS_EXTERNAL (symp))
2031 as_bad (_("%s: global symbols not supported in common sections"),
2033 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2037 name = S_GET_NAME (symp);
2041 decode_local_label_name ((char *) S_GET_NAME (symp));
2042 /* They only differ if `name' is a fb or dollar local
2044 if (name2 != name && ! S_IS_DEFINED (symp))
2045 as_bad (_("local label `%s' is not defined"), name2);
2048 /* Do it again, because adjust_reloc_syms might introduce
2049 more symbols. They'll probably only be section symbols,
2050 but they'll still need to have the values computed. */
2051 resolve_symbol_value (symp);
2053 /* Skip symbols which were equated to undefined or common
2055 if (symbol_equated_reloc_p (symp)
2056 || S_IS_WEAKREFR (symp))
2058 const char *sname = S_GET_NAME (symp);
2060 if (S_IS_COMMON (symp)
2061 && !TC_FAKE_LABEL (sname)
2062 && !S_IS_WEAKREFR (symp)
2063 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
2065 expressionS *e = symbol_get_value_expression (symp);
2067 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
2068 sname, S_GET_NAME (e->X_add_symbol));
2070 if (S_GET_SEGMENT (symp) == reg_section)
2072 /* Report error only if we know the symbol name. */
2073 if (S_GET_NAME (symp) != reg_section->name)
2074 as_bad (_("can't make global register symbol `%s'"),
2077 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2081 #ifdef obj_frob_symbol
2082 obj_frob_symbol (symp, punt);
2084 #ifdef tc_frob_symbol
2085 if (! punt || symbol_used_in_reloc_p (symp))
2086 tc_frob_symbol (symp, punt);
2089 /* If we don't want to keep this symbol, splice it out of
2090 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
2091 want section symbols. Otherwise, we skip local symbols
2092 and symbols that the frob_symbol macros told us to punt,
2093 but we keep such symbols if they are used in relocs. */
2094 if (symp == abs_section_sym
2095 || (! EMIT_SECTION_SYMBOLS
2096 && symbol_section_p (symp))
2097 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
2098 opposites. Sometimes the former checks flags and the
2099 latter examines the name... */
2100 || (!S_IS_EXTERNAL (symp)
2101 && (punt || S_IS_LOCAL (symp) ||
2102 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
2103 && ! symbol_used_in_reloc_p (symp)))
2105 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2107 /* After symbol_remove, symbol_next(symp) still returns
2108 the one that came after it in the chain. So we don't
2109 need to do any extra cleanup work here. */
2113 /* Make sure we really got a value for the symbol. */
2114 if (! symbol_resolved_p (symp))
2116 as_bad (_("can't resolve value for symbol `%s'"),
2118 symbol_mark_resolved (symp);
2121 /* Set the value into the BFD symbol. Up til now the value
2122 has only been kept in the gas symbolS struct. */
2123 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2125 /* A warning construct is a warning symbol followed by the
2126 symbol warned about. Don't let anything object-format or
2127 target-specific muck with it; it's ready for output. */
2128 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
2129 skip_next_symbol = TRUE;
2135 /* Now do any format-specific adjustments to the symbol table, such
2136 as adding file symbols. */
2137 #ifdef tc_adjust_symtab
2138 tc_adjust_symtab ();
2140 #ifdef obj_adjust_symtab
2141 obj_adjust_symtab ();
2144 /* Stop if there is an error. */
2148 /* Now that all the sizes are known, and contents correct, we can
2149 start writing to the file. */
2152 /* If *_frob_file changes the symbol value at this point, it is
2153 responsible for moving the changed value into symp->bsym->value
2154 as well. Hopefully all symbol value changing can be done in
2159 #ifdef obj_frob_file
2162 #ifdef obj_coff_generate_pdata
2163 obj_coff_generate_pdata ();
2165 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2167 #ifdef tc_frob_file_after_relocs
2168 tc_frob_file_after_relocs ();
2170 #ifdef obj_frob_file_after_relocs
2171 obj_frob_file_after_relocs ();
2174 /* Once all relocations have been written, we can compress the
2175 contents of the debug sections. This needs to be done before
2176 we start writing any sections, because it will affect the file
2177 layout, which is fixed once we start writing contents. */
2178 if (flag_compress_debug)
2179 bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
2181 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2184 #ifdef TC_GENERIC_RELAX_TABLE
2185 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2188 relax_frag (segT segment, fragS *fragP, long stretch)
2190 const relax_typeS *this_type;
2191 const relax_typeS *start_type;
2192 relax_substateT next_state;
2193 relax_substateT this_state;
2199 const relax_typeS *table;
2201 target = fragP->fr_offset;
2202 address = fragP->fr_address;
2203 table = TC_GENERIC_RELAX_TABLE;
2204 this_state = fragP->fr_subtype;
2205 start_type = this_type = table + this_state;
2206 symbolP = fragP->fr_symbol;
2212 sym_frag = symbol_get_frag (symbolP);
2214 #ifndef DIFF_EXPR_OK
2215 know (sym_frag != NULL);
2217 know (S_GET_SEGMENT (symbolP) != absolute_section
2218 || sym_frag == &zero_address_frag);
2219 target += S_GET_VALUE (symbolP);
2221 /* If SYM_FRAG has yet to be reached on this pass, assume it
2222 will move by STRETCH just as we did, unless there is an
2223 alignment frag between here and SYM_FRAG. An alignment may
2224 well absorb any STRETCH, and we don't want to choose a larger
2225 branch insn by overestimating the needed reach of this
2226 branch. It isn't critical to calculate TARGET exactly; We
2227 know we'll be doing another pass if STRETCH is non-zero. */
2230 && sym_frag->relax_marker != fragP->relax_marker
2231 && S_GET_SEGMENT (symbolP) == segment)
2234 || sym_frag->region == fragP->region)
2236 /* If we get here we know we have a forward branch. This
2237 relax pass may have stretched previous instructions so
2238 far that omitting STRETCH would make the branch
2239 negative. Don't allow this in case the negative reach is
2240 large enough to require a larger branch instruction. */
2241 else if (target < address)
2242 target = fragP->fr_next->fr_address + stretch;
2246 aim = target - address - fragP->fr_fix;
2247 #ifdef TC_PCREL_ADJUST
2248 /* Currently only the ns32k family needs this. */
2249 aim += TC_PCREL_ADJUST (fragP);
2252 #ifdef md_prepare_relax_scan
2253 /* Formerly called M68K_AIM_KLUDGE. */
2254 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2259 /* Look backwards. */
2260 for (next_state = this_type->rlx_more; next_state;)
2261 if (aim >= this_type->rlx_backward)
2265 /* Grow to next state. */
2266 this_state = next_state;
2267 this_type = table + this_state;
2268 next_state = this_type->rlx_more;
2273 /* Look forwards. */
2274 for (next_state = this_type->rlx_more; next_state;)
2275 if (aim <= this_type->rlx_forward)
2279 /* Grow to next state. */
2280 this_state = next_state;
2281 this_type = table + this_state;
2282 next_state = this_type->rlx_more;
2286 growth = this_type->rlx_length - start_type->rlx_length;
2288 fragP->fr_subtype = this_state;
2292 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2294 /* Relax_align. Advance location counter to next address that has 'alignment'
2295 lowest order bits all 0s, return size of adjustment made. */
2296 static relax_addressT
2297 relax_align (register relax_addressT address, /* Address now. */
2298 register int alignment /* Alignment (binary). */)
2300 relax_addressT mask;
2301 relax_addressT new_address;
2303 mask = ~((~0) << alignment);
2304 new_address = (address + mask) & (~mask);
2305 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2307 /* We must provide lots of padding, so the linker can discard it
2308 when needed. The linker will not add extra space, ever. */
2309 new_address += (1 << alignment);
2311 return (new_address - address);
2314 /* Now we have a segment, not a crowd of sub-segments, we can make
2319 After this, all frags in this segment have addresses that are correct
2320 within the segment. Since segments live in different file addresses,
2321 these frag addresses may not be the same as final object-file
2325 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2327 unsigned long frag_count;
2329 relax_addressT address;
2333 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2334 subseg_change (segment, 0);
2336 /* For each frag in segment: count and store (a 1st guess of)
2340 for (frag_count = 0, fragP = segment_frag_root;
2342 fragP = fragP->fr_next, frag_count ++)
2344 fragP->region = region;
2345 fragP->relax_marker = 0;
2346 fragP->fr_address = address;
2347 address += fragP->fr_fix;
2349 switch (fragP->fr_type)
2352 address += fragP->fr_offset * fragP->fr_var;
2359 addressT offset = relax_align (address, (int) fragP->fr_offset);
2361 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2364 if (offset % fragP->fr_var != 0)
2366 as_bad_where (fragP->fr_file, fragP->fr_line,
2367 _("alignment padding (%lu bytes) not a multiple of %ld"),
2368 (unsigned long) offset, (long) fragP->fr_var);
2369 offset -= (offset % fragP->fr_var);
2378 /* Assume .org is nugatory. It will grow with 1st relax. */
2385 case rs_machine_dependent:
2386 /* If fr_symbol is an expression, this call to
2387 resolve_symbol_value sets up the correct segment, which will
2388 likely be needed in md_estimate_size_before_relax. */
2389 if (fragP->fr_symbol)
2390 resolve_symbol_value (fragP->fr_symbol);
2392 address += md_estimate_size_before_relax (fragP, segment);
2395 #ifndef WORKING_DOT_WORD
2396 /* Broken words don't concern us yet. */
2397 case rs_broken_word:
2402 /* Initial guess is always 1; doing otherwise can result in
2403 stable solutions that are larger than the minimum. */
2404 address += fragP->fr_offset = 1;
2408 address += eh_frame_estimate_size_before_relax (fragP);
2412 address += dwarf2dbg_estimate_size_before_relax (fragP);
2416 BAD_CASE (fragP->fr_type);
2423 unsigned long max_iterations;
2425 /* Cumulative address adjustment. */
2428 /* Have we made any adjustment this pass? We can't just test
2429 stretch because one piece of code may have grown and another
2433 /* Most horrible, but gcc may give us some exception data that
2434 is impossible to assemble, of the form
2438 .uleb128 end - start
2444 If the leb128 is two bytes in size, then end-start is 128*128,
2445 which requires a three byte leb128. If the leb128 is three
2446 bytes in size, then end-start is 128*128-1, which requires a
2447 two byte leb128. We work around this dilemma by inserting
2448 an extra 4 bytes of alignment just after the .align. This
2449 works because the data after the align is accessed relative to
2452 This counter is used in a tiny state machine to detect
2453 whether a leb128 followed by an align is impossible to
2455 int rs_leb128_fudge = 0;
2457 /* We want to prevent going into an infinite loop where one frag grows
2458 depending upon the location of a symbol which is in turn moved by
2459 the growing frag. eg:
2465 So we dictate that this algorithm can be at most O2. */
2466 max_iterations = frag_count * frag_count;
2467 /* Check for overflow. */
2468 if (max_iterations < frag_count)
2469 max_iterations = frag_count;
2477 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2480 addressT was_address;
2484 fragP->relax_marker ^= 1;
2485 was_address = fragP->fr_address;
2486 address = fragP->fr_address += stretch;
2487 symbolP = fragP->fr_symbol;
2488 offset = fragP->fr_offset;
2490 switch (fragP->fr_type)
2492 case rs_fill: /* .fill never relaxes. */
2496 #ifndef WORKING_DOT_WORD
2497 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2498 for it I do not want to write it. I do not want to have
2499 anything to do with it. This is not the proper way to
2500 implement this misfeature. */
2501 case rs_broken_word:
2503 struct broken_word *lie;
2504 struct broken_word *untruth;
2506 /* Yes this is ugly (storing the broken_word pointer
2507 in the symbol slot). Still, this whole chunk of
2508 code is ugly, and I don't feel like doing anything
2509 about it. Think of it as stubbornness in action. */
2511 for (lie = (struct broken_word *) (fragP->fr_symbol);
2512 lie && lie->dispfrag == fragP;
2513 lie = lie->next_broken_word)
2519 offset = (S_GET_VALUE (lie->add)
2521 - S_GET_VALUE (lie->sub));
2522 if (offset <= -32768 || offset >= 32767)
2524 if (flag_warn_displacement)
2527 sprint_value (buf, (addressT) lie->addnum);
2528 as_warn_where (fragP->fr_file, fragP->fr_line,
2529 _(".word %s-%s+%s didn't fit"),
2530 S_GET_NAME (lie->add),
2531 S_GET_NAME (lie->sub),
2534 if (fragP->fr_subtype == 0)
2536 fragP->fr_subtype++;
2537 growth += md_short_jump_size;
2540 /* Redirect *all* words of this table with the same
2541 target, lest we have to handle the case where the
2542 same target but with a offset that fits on this
2543 round overflows at the next relaxation round. */
2544 for (untruth = (struct broken_word *) (fragP->fr_symbol);
2545 untruth && untruth->dispfrag == lie->dispfrag;
2546 untruth = untruth->next_broken_word)
2547 if ((symbol_get_frag (untruth->add)
2548 == symbol_get_frag (lie->add))
2549 && (S_GET_VALUE (untruth->add)
2550 == S_GET_VALUE (lie->add)))
2553 untruth->use_jump = lie;
2557 growth += md_long_jump_size;
2562 } /* case rs_broken_word */
2568 addressT oldoff, newoff;
2570 oldoff = relax_align (was_address + fragP->fr_fix,
2572 newoff = relax_align (address + fragP->fr_fix,
2575 if (fragP->fr_subtype != 0)
2577 if (oldoff > fragP->fr_subtype)
2579 if (newoff > fragP->fr_subtype)
2583 growth = newoff - oldoff;
2585 /* If this align happens to follow a leb128 and
2586 we have determined that the leb128 is bouncing
2587 in size, then break the cycle by inserting an
2590 && (rs_leb128_fudge & 16) != 0
2591 && (rs_leb128_fudge & 15) >= 2)
2593 segment_info_type *seginfo = seg_info (segment);
2594 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2597 newf = frag_alloc (ob);
2598 obstack_blank_fast (ob, fragP->fr_var);
2599 obstack_finish (ob);
2600 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2601 memcpy (newf->fr_literal,
2602 fragP->fr_literal + fragP->fr_fix,
2604 newf->fr_type = rs_fill;
2605 newf->fr_address = address + fragP->fr_fix + newoff;
2607 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2609 if (newf->fr_offset * newf->fr_var
2610 != (offsetT) 1 << fragP->fr_offset)
2612 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2615 /* Include size of new frag in GROWTH. */
2616 growth += newf->fr_offset * newf->fr_var;
2617 /* Adjust the new frag address for the amount
2618 we'll add when we process the new frag. */
2619 newf->fr_address -= stretch + growth;
2620 newf->relax_marker ^= 1;
2621 fragP->fr_next = newf;
2623 as_warn (_("padding added"));
2631 addressT target = offset;
2636 /* Convert from an actual address to an octet offset
2637 into the section. Here it is assumed that the
2638 section's VMA is zero, and can omit subtracting it
2639 from the symbol's value to get the address offset. */
2640 know (S_GET_SEGMENT (symbolP)->vma == 0);
2641 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2644 know (fragP->fr_next);
2645 after = fragP->fr_next->fr_address + stretch;
2646 growth = target - after;
2651 /* Don't error on first few frag relax passes.
2652 The symbol might be an expression involving
2653 symbol values from other sections. If those
2654 sections have not yet been processed their
2655 frags will all have zero addresses, so we
2656 will calculate incorrect values for them. The
2657 number of passes we allow before giving an
2658 error is somewhat arbitrary. It should be at
2659 least one, with larger values requiring
2660 increasingly contrived dependencies between
2661 frags to trigger a false error. */
2664 /* Force another pass. */
2669 /* Growth may be negative, but variable part of frag
2670 cannot have fewer than 0 chars. That is, we can't
2672 as_bad_where (fragP->fr_file, fragP->fr_line,
2673 _("attempt to move .org backwards"));
2675 /* We've issued an error message. Change the
2676 frag to avoid cascading errors. */
2677 fragP->fr_type = rs_align;
2678 fragP->fr_subtype = 0;
2679 fragP->fr_offset = 0;
2680 fragP->fr_fix = after - address;
2691 amount = S_GET_VALUE (symbolP);
2692 if (S_GET_SEGMENT (symbolP) != absolute_section
2693 || S_IS_COMMON (symbolP)
2694 || ! S_IS_DEFINED (symbolP))
2696 as_bad_where (fragP->fr_file, fragP->fr_line,
2697 _(".space specifies non-absolute value"));
2698 /* Prevent repeat of this error message. */
2699 fragP->fr_symbol = 0;
2701 else if (amount < 0)
2703 /* Don't error on first few frag relax passes.
2704 See rs_org comment for a longer explanation. */
2711 as_warn_where (fragP->fr_file, fragP->fr_line,
2712 _(".space or .fill with negative value, ignored"));
2713 fragP->fr_symbol = 0;
2716 growth = (was_address + fragP->fr_fix + amount
2717 - fragP->fr_next->fr_address);
2721 case rs_machine_dependent:
2722 #ifdef md_relax_frag
2723 growth = md_relax_frag (segment, fragP, stretch);
2725 #ifdef TC_GENERIC_RELAX_TABLE
2726 /* The default way to relax a frag is to look through
2727 TC_GENERIC_RELAX_TABLE. */
2728 growth = relax_frag (segment, fragP, stretch);
2729 #endif /* TC_GENERIC_RELAX_TABLE */
2738 value = resolve_symbol_value (fragP->fr_symbol);
2739 size = sizeof_leb128 (value, fragP->fr_subtype);
2740 growth = size - fragP->fr_offset;
2741 fragP->fr_offset = size;
2746 growth = eh_frame_relax_frag (fragP);
2750 growth = dwarf2dbg_relax_frag (fragP);
2754 BAD_CASE (fragP->fr_type);
2761 if (fragP->fr_type == rs_leb128)
2762 rs_leb128_fudge += 16;
2763 else if (fragP->fr_type == rs_align
2764 && (rs_leb128_fudge & 16) != 0
2766 rs_leb128_fudge += 16;
2768 rs_leb128_fudge = 0;
2773 && (rs_leb128_fudge & 16) == 0
2774 && (rs_leb128_fudge & -16) != 0)
2775 rs_leb128_fudge += 1;
2777 rs_leb128_fudge = 0;
2779 /* Until nothing further to relax. */
2780 while (stretched && -- max_iterations);
2783 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2784 segment_name (segment));
2787 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2788 if (fragP->last_fr_address != fragP->fr_address)
2790 fragP->last_fr_address = fragP->fr_address;
2797 number_to_chars_bigendian (char *buf, valueT val, int n)
2803 buf[n] = val & 0xff;
2809 number_to_chars_littleendian (char *buf, valueT val, int n)
2815 *buf++ = val & 0xff;
2821 write_print_statistics (FILE *file)
2823 fprintf (file, "fixups: %d\n", n_fixups);
2826 /* For debugging. */
2827 extern int indent_level;
2830 print_fixup (fixS *fixp)
2833 fprintf (stderr, "fix ");
2834 fprintf_vma (stderr, (bfd_vma)((bfd_hostptr_t) fixp));
2835 fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line);
2837 fprintf (stderr, " pcrel");
2838 if (fixp->fx_pcrel_adjust)
2839 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2840 if (fixp->fx_im_disp)
2843 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2845 fprintf (stderr, " im_disp");
2849 fprintf (stderr, " tcbit");
2851 fprintf (stderr, " done");
2852 fprintf (stderr, "\n size=%d frag=", fixp->fx_size);
2853 fprintf_vma (stderr, (bfd_vma) ((bfd_hostptr_t) fixp->fx_frag));
2854 fprintf (stderr, " where=%ld offset=%lx addnumber=%lx",
2855 (long) fixp->fx_where,
2856 (unsigned long) fixp->fx_offset,
2857 (unsigned long) fixp->fx_addnumber);
2858 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2862 fprintf (stderr, "\n +<");
2863 print_symbol_value_1 (stderr, fixp->fx_addsy);
2864 fprintf (stderr, ">");
2868 fprintf (stderr, "\n -<");
2869 print_symbol_value_1 (stderr, fixp->fx_subsy);
2870 fprintf (stderr, ">");
2872 fprintf (stderr, "\n");
2873 #ifdef TC_FIX_DATA_PRINT
2874 TC_FIX_DATA_PRINT (stderr, fixp);