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
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 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"
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 /* Relocs generated by ".reloc" pseudo. */
121 struct reloc_list* reloc_list;
123 void print_fixup (fixS *);
125 /* We generally attach relocs to frag chains. However, after we have
126 chained these all together into a segment, any relocs we add after
127 that must be attached to a segment. This will include relocs added
128 in md_estimate_size_for_relax, for example. */
129 static int frags_chained = 0;
133 #define RELOC_ENUM enum bfd_reloc_code_real
135 /* Create a fixS in obstack 'notes'. */
138 fix_new_internal (fragS *frag, /* Which frag? */
139 int where, /* Where in that frag? */
140 int size, /* 1, 2, or 4 usually. */
141 symbolS *add_symbol, /* X_add_symbol. */
142 symbolS *sub_symbol, /* X_op_symbol. */
143 offsetT offset, /* X_add_number. */
144 int pcrel, /* TRUE if PC-relative relocation. */
145 RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type. */)
151 fixP = obstack_alloc (¬es, sizeof (fixS));
153 fixP->fx_frag = frag;
154 fixP->fx_where = where;
155 fixP->fx_size = size;
156 /* We've made fx_size a narrow field; check that it's wide enough. */
157 if (fixP->fx_size != size)
159 as_bad (_("field fx_size too small to hold %d"), size);
162 fixP->fx_addsy = add_symbol;
163 fixP->fx_subsy = sub_symbol;
164 fixP->fx_offset = offset;
165 fixP->fx_dot_value = dot_value;
166 fixP->fx_pcrel = pcrel;
167 fixP->fx_r_type = r_type;
168 fixP->fx_im_disp = 0;
169 fixP->fx_pcrel_adjust = 0;
170 fixP->fx_bit_fixP = 0;
171 fixP->fx_addnumber = 0;
175 fixP->fx_no_overflow = 0;
179 fixP->fx_cgen.insn = NULL;
180 fixP->fx_cgen.opinfo = 0;
184 TC_INIT_FIX_DATA (fixP);
187 as_where (&fixP->fx_file, &fixP->fx_line);
189 /* Usually, we want relocs sorted numerically, but while
190 comparing to older versions of gas that have relocs
191 reverse sorted, it is convenient to have this compile
192 time option. xoxorich. */
195 fixS **seg_fix_rootP = (frags_chained
196 ? &seg_info (now_seg)->fix_root
197 : &frchain_now->fix_root);
198 fixS **seg_fix_tailP = (frags_chained
199 ? &seg_info (now_seg)->fix_tail
200 : &frchain_now->fix_tail);
202 #ifdef REVERSE_SORT_RELOCS
204 fixP->fx_next = *seg_fix_rootP;
205 *seg_fix_rootP = fixP;
207 #else /* REVERSE_SORT_RELOCS */
209 fixP->fx_next = NULL;
212 (*seg_fix_tailP)->fx_next = fixP;
214 *seg_fix_rootP = fixP;
215 *seg_fix_tailP = fixP;
217 #endif /* REVERSE_SORT_RELOCS */
223 /* Create a fixup relative to a symbol (plus a constant). */
226 fix_new (fragS *frag, /* Which frag? */
227 int where, /* Where in that frag? */
228 int size, /* 1, 2, or 4 usually. */
229 symbolS *add_symbol, /* X_add_symbol. */
230 offsetT offset, /* X_add_number. */
231 int pcrel, /* TRUE if PC-relative relocation. */
232 RELOC_ENUM r_type /* Relocation type. */)
234 return fix_new_internal (frag, where, size, add_symbol,
235 (symbolS *) NULL, offset, pcrel, r_type);
238 /* Create a fixup for an expression. Currently we only support fixups
239 for difference expressions. That is itself more than most object
240 file formats support anyhow. */
243 fix_new_exp (fragS *frag, /* Which frag? */
244 int where, /* Where in that frag? */
245 int size, /* 1, 2, or 4 usually. */
246 expressionS *exp, /* Expression. */
247 int pcrel, /* TRUE if PC-relative relocation. */
248 RELOC_ENUM r_type /* Relocation type. */)
260 as_bad (_("register value used as expression"));
264 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
265 the difference expression cannot immediately be reduced. */
267 symbolS *stmp = make_expr_symbol (exp);
269 exp->X_op = O_symbol;
270 exp->X_op_symbol = 0;
271 exp->X_add_symbol = stmp;
272 exp->X_add_number = 0;
274 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
278 add = exp->X_add_symbol;
279 off = exp->X_add_number;
280 r_type = BFD_RELOC_RVA;
284 sub = exp->X_add_symbol;
285 off = exp->X_add_number;
289 sub = exp->X_op_symbol;
292 add = exp->X_add_symbol;
295 off = exp->X_add_number;
299 add = make_expr_symbol (exp);
303 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
306 /* Generic function to determine whether a fixup requires a relocation. */
308 generic_force_reloc (fixS *fix)
310 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
311 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
314 if (fix->fx_addsy == NULL)
317 return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
320 /* Append a string onto another string, bumping the pointer along. */
322 append (char **charPP, char *fromP, unsigned long length)
324 /* Don't trust memcpy() of 0 chars. */
328 memcpy (*charPP, fromP, length);
332 /* This routine records the largest alignment seen for each segment.
333 If the beginning of the segment is aligned on the worst-case
334 boundary, all of the other alignments within it will work. At
335 least one object format really uses this info. */
338 record_alignment (/* Segment to which alignment pertains. */
340 /* Alignment, as a power of 2 (e.g., 1 => 2-byte
341 boundary, 2 => 4-byte boundary, etc.) */
344 if (seg == absolute_section)
347 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
348 bfd_set_section_alignment (stdoutput, seg, align);
352 get_recorded_alignment (segT seg)
354 if (seg == absolute_section)
357 return bfd_get_section_alignment (stdoutput, seg);
360 /* Reset the section indices after removing the gas created sections. */
363 renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
365 int *countp = (int *) countparg;
367 sec->index = *countp;
372 chain_frchains_together_1 (segT section, struct frchain *frchp)
374 fragS dummy, *prev_frag = &dummy;
375 fixS fix_dummy, *prev_fix = &fix_dummy;
377 for (; frchp; frchp = frchp->frch_next)
379 prev_frag->fr_next = frchp->frch_root;
380 prev_frag = frchp->frch_last;
381 assert (prev_frag->fr_type != 0);
382 if (frchp->fix_root != (fixS *) NULL)
384 if (seg_info (section)->fix_root == (fixS *) NULL)
385 seg_info (section)->fix_root = frchp->fix_root;
386 prev_fix->fx_next = frchp->fix_root;
387 seg_info (section)->fix_tail = frchp->fix_tail;
388 prev_fix = frchp->fix_tail;
391 assert (prev_frag->fr_type != 0);
392 assert (prev_frag != &dummy);
393 prev_frag->fr_next = 0;
398 chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
400 void *xxx ATTRIBUTE_UNUSED)
402 segment_info_type *info;
404 /* BFD may have introduced its own sections without using
405 subseg_new, so it is possible that seg_info is NULL. */
406 info = seg_info (section);
407 if (info != (segment_info_type *) NULL)
408 info->frchainP->frch_last
409 = chain_frchains_together_1 (section, info->frchainP);
411 /* Now that we've chained the frags together, we must add new fixups
412 to the segment, not to the frag chain. */
417 cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
419 switch (fragP->fr_type)
427 HANDLE_ALIGN (fragP);
429 know (fragP->fr_next != NULL);
430 fragP->fr_offset = (fragP->fr_next->fr_address
432 - fragP->fr_fix) / fragP->fr_var;
433 if (fragP->fr_offset < 0)
435 as_bad_where (fragP->fr_file, fragP->fr_line,
436 _("attempt to .org/.space backwards? (%ld)"),
437 (long) fragP->fr_offset);
438 fragP->fr_offset = 0;
440 fragP->fr_type = rs_fill;
448 valueT value = S_GET_VALUE (fragP->fr_symbol);
451 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
454 fragP->fr_fix += size;
455 fragP->fr_type = rs_fill;
457 fragP->fr_offset = 0;
458 fragP->fr_symbol = NULL;
463 eh_frame_convert_frag (fragP);
467 dwarf2dbg_convert_frag (fragP);
470 case rs_machine_dependent:
471 md_convert_frag (stdoutput, sec, fragP);
473 assert (fragP->fr_next == NULL
474 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
477 /* After md_convert_frag, we make the frag into a ".space 0".
478 md_convert_frag() should set up any fixSs and constants
483 #ifndef WORKING_DOT_WORD
486 struct broken_word *lie;
488 if (fragP->fr_subtype)
490 fragP->fr_fix += md_short_jump_size;
491 for (lie = (struct broken_word *) (fragP->fr_symbol);
492 lie && lie->dispfrag == fragP;
493 lie = lie->next_broken_word)
495 fragP->fr_fix += md_long_jump_size;
503 BAD_CASE (fragP->fr_type);
507 md_frag_check (fragP);
511 struct relax_seg_info
518 relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
520 segment_info_type *seginfo = seg_info (sec);
521 struct relax_seg_info *info = (struct relax_seg_info *) xxx;
523 if (seginfo && seginfo->frchainP
524 && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
529 size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
533 segment_info_type *seginfo;
535 valueT size, newsize;
537 subseg_change (sec, 0);
539 seginfo = seg_info (sec);
540 if (seginfo && seginfo->frchainP)
542 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
543 cvt_frag_to_fill (sec, fragp);
544 for (fragp = seginfo->frchainP->frch_root;
546 fragp = fragp->fr_next)
547 /* Walk to last elt. */
549 size = fragp->fr_address + fragp->fr_fix;
554 flags = bfd_get_section_flags (abfd, sec);
556 if (size > 0 && ! seginfo->bss)
557 flags |= SEC_HAS_CONTENTS;
560 x = bfd_set_section_flags (abfd, sec, flags);
563 newsize = md_section_align (sec, size);
564 x = bfd_set_section_size (abfd, sec, newsize);
567 /* If the size had to be rounded up, add some padding in the last
569 assert (newsize >= size);
572 fragS *last = seginfo->frchainP->frch_last;
573 fragp = seginfo->frchainP->frch_root;
574 while (fragp->fr_next != last)
575 fragp = fragp->fr_next;
576 last->fr_address = size;
577 if ((newsize - size) % fragp->fr_var == 0)
578 fragp->fr_offset += (newsize - size) / fragp->fr_var;
580 /* If we hit this abort, it's likely due to subsegs_finish not
581 providing sufficient alignment on the last frag, and the
582 machine dependent code using alignment frags with fr_var
587 #ifdef tc_frob_section
588 tc_frob_section (sec);
590 #ifdef obj_frob_section
591 obj_frob_section (sec);
597 dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
599 segment_info_type *seginfo = seg_info (sec);
600 fixS *fixp = seginfo->fix_root;
605 fprintf (stream, "sec %s relocs:\n", sec->name);
608 symbolS *s = fixp->fx_addsy;
610 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
611 (int) fixp->fx_r_type);
613 fprintf (stream, "no sym\n");
616 print_symbol_value_1 (stream, s);
617 fprintf (stream, "\n");
619 fixp = fixp->fx_next;
623 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
626 #ifndef EMIT_SECTION_SYMBOLS
627 #define EMIT_SECTION_SYMBOLS 1
630 /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
631 and check for validity. Convert RELOC_LIST from using U.A fields
634 resolve_reloc_expr_symbols (void)
636 struct reloc_list *r;
638 for (r = reloc_list; r; r = r->next)
642 bfd_vma offset, addend;
644 reloc_howto_type *howto;
646 resolve_symbol_value (r->u.a.offset_sym);
647 symval = symbol_get_value_expression (r->u.a.offset_sym);
651 if (symval->X_op == O_constant)
652 sym = r->u.a.offset_sym;
653 else if (symval->X_op == O_symbol)
655 sym = symval->X_add_symbol;
656 offset = symval->X_add_number;
657 symval = symbol_get_value_expression (symval->X_add_symbol);
660 || symval->X_op != O_constant
661 || (sec = S_GET_SEGMENT (sym)) == NULL
662 || !SEG_NORMAL (sec))
664 as_bad_where (r->file, r->line, _("invalid offset expression"));
668 offset += S_GET_VALUE (sym);
671 addend = r->u.a.addend;
672 if (r->u.a.sym != NULL)
674 resolve_symbol_value (r->u.a.sym);
675 symval = symbol_get_value_expression (r->u.a.sym);
676 if (symval->X_op == O_constant)
678 else if (symval->X_op == O_symbol)
680 sym = symval->X_add_symbol;
681 addend += symval->X_add_number;
682 symval = symbol_get_value_expression (symval->X_add_symbol);
684 if (symval->X_op != O_constant)
686 as_bad_where (r->file, r->line, _("invalid reloc expression"));
689 else if (sym != NULL)
690 symbol_mark_used_in_reloc (sym);
694 if (abs_section_sym == NULL)
695 abs_section_sym = section_symbol (absolute_section);
696 sym = abs_section_sym;
699 howto = r->u.a.howto;
702 r->u.b.s = symbol_get_bfdsym (sym);
703 r->u.b.r.sym_ptr_ptr = &r->u.b.s;
704 r->u.b.r.address = offset;
705 r->u.b.r.addend = addend;
706 r->u.b.r.howto = howto;
710 /* This pass over fixups decides whether symbols can be replaced with
714 adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
716 void *xxx ATTRIBUTE_UNUSED)
718 segment_info_type *seginfo = seg_info (sec);
724 dump_section_relocs (abfd, sec, stderr);
726 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
730 else if (fixp->fx_addsy)
736 fprintf (stderr, "\n\nadjusting fixup:\n");
740 sym = fixp->fx_addsy;
742 /* All symbols should have already been resolved at this
743 point. It is possible to see unresolved expression
744 symbols, though, since they are not in the regular symbol
746 resolve_symbol_value (sym);
748 if (fixp->fx_subsy != NULL)
749 resolve_symbol_value (fixp->fx_subsy);
751 /* If this symbol is equated to an undefined or common symbol,
752 convert the fixup to being against that symbol. */
753 while (symbol_equated_reloc_p (sym)
754 || S_IS_WEAKREFR (sym))
756 symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
759 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
760 fixp->fx_addsy = newsym;
764 if (symbol_mri_common_p (sym))
766 fixp->fx_offset += S_GET_VALUE (sym);
767 fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
771 /* If the symbol is undefined, common, weak, or global (ELF
772 shared libs), we can't replace it with the section symbol. */
773 if (S_FORCE_RELOC (fixp->fx_addsy, 1))
776 /* Is there some other (target cpu dependent) reason we can't adjust
777 this one? (E.g. relocations involving function addresses on
779 #ifdef tc_fix_adjustable
780 if (! tc_fix_adjustable (fixp))
784 /* Since we're reducing to section symbols, don't attempt to reduce
785 anything that's already using one. */
786 if (symbol_section_p (sym))
789 symsec = S_GET_SEGMENT (sym);
793 if (bfd_is_abs_section (symsec))
795 /* The fixup_segment routine normally will not use this
796 symbol in a relocation. */
800 /* Don't try to reduce relocs which refer to non-local symbols
801 in .linkonce sections. It can lead to confusion when a
802 debugging section refers to a .linkonce section. I hope
803 this will always be correct. */
804 if (symsec != sec && ! S_IS_LOCAL (sym))
806 if ((symsec->flags & SEC_LINK_ONCE) != 0
808 /* The GNU toolchain uses an extension for ELF: a
809 section beginning with the magic string
810 .gnu.linkonce is a linkonce section. */
811 && strncmp (segment_name (symsec), ".gnu.linkonce",
812 sizeof ".gnu.linkonce" - 1) == 0))
816 /* Never adjust a reloc against local symbol in a merge section
817 with non-zero addend. */
818 if ((symsec->flags & SEC_MERGE) != 0
819 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
822 /* Never adjust a reloc against TLS local symbol. */
823 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
826 /* We refetch the segment when calling section_symbol, rather
827 than using symsec, because S_GET_VALUE may wind up changing
828 the section when it calls resolve_symbol_value. */
829 fixp->fx_offset += S_GET_VALUE (sym);
830 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
832 fprintf (stderr, "\nadjusted fixup:\n");
837 dump_section_relocs (abfd, sec, stderr);
842 Go through all the fixS's in a segment and see which ones can be
843 handled now. (These consist of fixS where we have since discovered
844 the value of a symbol, or the address of the frag involved.)
845 For each one, call md_apply_fix to put the fix into the frag data.
847 Result is a count of how many relocation structs will be needed to
848 handle the remaining fixS's that we couldn't completely handle here.
849 These will be output later by emit_relocations(). */
852 fixup_segment (fixS *fixP, segT this_segment)
854 long seg_reloc_count = 0;
857 segT add_symbol_segment = absolute_section;
859 if (fixP != NULL && abs_section_sym == NULL)
860 abs_section_sym = section_symbol (absolute_section);
862 /* If the linker is doing the relaxing, we must not do any fixups.
864 Well, strictly speaking that's not true -- we could do any that
865 are PC-relative and don't cross regions that could change size.
866 And for the i960 we might be able to turn callx/callj into bal
867 anyways in cases where we know the maximum displacement. */
868 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
870 for (; fixP; fixP = fixP->fx_next)
873 if (fixP->fx_addsy == NULL)
875 /* There was no symbol required by this relocation.
876 However, BFD doesn't really handle relocations
877 without symbols well. So fake up a local symbol in
878 the absolute section. */
879 fixP->fx_addsy = abs_section_sym;
881 symbol_mark_used_in_reloc (fixP->fx_addsy);
882 if (fixP->fx_subsy != NULL)
883 symbol_mark_used_in_reloc (fixP->fx_subsy);
886 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
887 return seg_reloc_count;
890 for (; fixP; fixP = fixP->fx_next)
893 fprintf (stderr, "\nprocessing fixup:\n");
897 fragP = fixP->fx_frag;
899 #ifdef TC_VALIDATE_FIX
900 TC_VALIDATE_FIX (fixP, this_segment, skip);
902 add_number = fixP->fx_offset;
904 if (fixP->fx_addsy != NULL)
905 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
907 if (fixP->fx_subsy != NULL)
909 segT sub_symbol_segment;
910 resolve_symbol_value (fixP->fx_subsy);
911 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
912 if (fixP->fx_addsy != NULL
913 && sub_symbol_segment == add_symbol_segment
914 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
916 add_number += S_GET_VALUE (fixP->fx_addsy);
917 add_number -= S_GET_VALUE (fixP->fx_subsy);
918 fixP->fx_offset = add_number;
919 fixP->fx_addsy = NULL;
920 fixP->fx_subsy = NULL;
922 /* See the comment below about 68k weirdness. */
926 else if (sub_symbol_segment == absolute_section
927 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
929 add_number -= S_GET_VALUE (fixP->fx_subsy);
930 fixP->fx_offset = add_number;
931 fixP->fx_subsy = NULL;
933 else if (sub_symbol_segment == this_segment
934 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
936 add_number -= S_GET_VALUE (fixP->fx_subsy);
937 fixP->fx_offset = (add_number + fixP->fx_dot_value
938 + fixP->fx_frag->fr_address);
940 /* Make it pc-relative. If the back-end code has not
941 selected a pc-relative reloc, cancel the adjustment
942 we do later on all pc-relative relocs. */
945 /* Do this for m68k even if it's already described
946 as pc-relative. On the m68k, an operand of
947 "pc@(foo-.-2)" should address "foo" in a
952 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
953 fixP->fx_subsy = NULL;
956 else if (!TC_VALIDATE_FIX_SUB (fixP))
958 as_bad_where (fixP->fx_file, fixP->fx_line,
959 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
960 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
961 segment_name (add_symbol_segment),
962 S_GET_NAME (fixP->fx_subsy),
963 segment_name (sub_symbol_segment));
969 if (add_symbol_segment == this_segment
970 && !TC_FORCE_RELOCATION_LOCAL (fixP))
972 /* This fixup was made when the symbol's segment was
973 SEG_UNKNOWN, but it is now in the local segment.
974 So we know how to do the address without relocation. */
975 add_number += S_GET_VALUE (fixP->fx_addsy);
976 fixP->fx_offset = add_number;
978 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
979 fixP->fx_addsy = NULL;
982 else if (add_symbol_segment == absolute_section
983 && !TC_FORCE_RELOCATION_ABS (fixP))
985 add_number += S_GET_VALUE (fixP->fx_addsy);
986 fixP->fx_offset = add_number;
987 fixP->fx_addsy = NULL;
989 else if (add_symbol_segment != undefined_section
990 && ! bfd_is_com_section (add_symbol_segment)
991 && MD_APPLY_SYM_VALUE (fixP))
992 add_number += S_GET_VALUE (fixP->fx_addsy);
997 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
998 if (!fixP->fx_done && fixP->fx_addsy == NULL)
1000 /* There was no symbol required by this relocation.
1001 However, BFD doesn't really handle relocations
1002 without symbols well. So fake up a local symbol in
1003 the absolute section. */
1004 fixP->fx_addsy = abs_section_sym;
1009 md_apply_fix (fixP, &add_number, this_segment);
1014 if (fixP->fx_addsy == NULL)
1015 fixP->fx_addsy = abs_section_sym;
1016 symbol_mark_used_in_reloc (fixP->fx_addsy);
1017 if (fixP->fx_subsy != NULL)
1018 symbol_mark_used_in_reloc (fixP->fx_subsy);
1021 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
1023 if (fixP->fx_size < sizeof (valueT))
1028 mask--; /* Set all bits to one. */
1029 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
1030 if ((add_number & mask) != 0 && (add_number & mask) != mask)
1032 char buf[50], buf2[50];
1033 sprint_value (buf, fragP->fr_address + fixP->fx_where);
1034 if (add_number > 1000)
1035 sprint_value (buf2, add_number);
1037 sprintf (buf2, "%ld", (long) add_number);
1038 as_bad_where (fixP->fx_file, fixP->fx_line,
1039 _("value of %s too large for field of %d bytes at %s"),
1040 buf2, fixP->fx_size, buf);
1041 } /* Generic error checking. */
1043 #ifdef WARN_SIGNED_OVERFLOW_WORD
1044 /* Warn if a .word value is too large when treated as a signed
1045 number. We already know it is not too negative. This is to
1046 catch over-large switches generated by gcc on the 68k. */
1047 if (!flag_signed_overflow_ok
1048 && fixP->fx_size == 2
1049 && add_number > 0x7fff)
1050 as_bad_where (fixP->fx_file, fixP->fx_line,
1051 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
1053 (long) (fragP->fr_address + fixP->fx_where));
1055 } /* Not a bit fix. */
1057 #ifdef TC_VALIDATE_FIX
1058 skip: ATTRIBUTE_UNUSED_LABEL
1062 fprintf (stderr, "result:\n");
1065 } /* For each fixS in this segment. */
1067 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
1068 return seg_reloc_count;
1072 fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
1074 void *xxx ATTRIBUTE_UNUSED)
1076 segment_info_type *seginfo = seg_info (sec);
1078 fixup_segment (seginfo->fix_root, sec);
1082 install_reloc (asection *sec, arelent *reloc, fragS *fragp,
1083 char *file, unsigned int line)
1086 bfd_reloc_status_type s;
1089 if (reloc->sym_ptr_ptr != NULL
1090 && (sym = *reloc->sym_ptr_ptr) != NULL
1091 && (sym->flags & BSF_KEEP) == 0
1092 && ((sym->flags & BSF_SECTION_SYM) == 0
1093 || !EMIT_SECTION_SYMBOLS
1094 || !bfd_is_abs_section (sym->section)))
1095 as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
1097 s = bfd_install_relocation (stdoutput, reloc,
1098 fragp->fr_literal, fragp->fr_address,
1104 case bfd_reloc_overflow:
1105 as_bad_where (file, line, _("relocation overflow"));
1107 case bfd_reloc_outofrange:
1108 as_bad_where (file, line, _("relocation out of range"));
1111 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1117 write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
1119 segment_info_type *seginfo = seg_info (sec);
1122 struct reloc_list *my_reloc_list, **rp, *r;
1126 /* If seginfo is NULL, we did not create this section; don't do
1127 anything with it. */
1128 if (seginfo == NULL)
1132 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1136 #ifdef RELOC_EXPANSION_POSSIBLE
1137 n *= MAX_RELOC_EXPANSION;
1140 /* Extract relocs for this section from reloc_list. */
1142 my_reloc_list = NULL;
1143 while ((r = *rp) != NULL)
1145 if (r->u.b.sec == sec)
1148 r->next = my_reloc_list;
1156 relocs = xcalloc (n, sizeof (arelent *));
1159 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1168 fx_size = fixp->fx_size;
1169 slack = TC_FX_SIZE_SLACK (fixp);
1171 fx_size = fx_size > slack ? fx_size - slack : 0;
1172 loc = fixp->fx_where + fx_size;
1173 if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1174 as_bad_where (fixp->fx_file, fixp->fx_line,
1175 _("internal error: fixup not contained within frag"));
1177 #ifndef RELOC_EXPANSION_POSSIBLE
1179 arelent *reloc = tc_gen_reloc (sec, fixp);
1183 relocs[i++] = reloc;
1188 arelent **reloc = tc_gen_reloc (sec, fixp);
1190 for (j = 0; reloc[j]; j++)
1191 relocs[i++] = reloc[j];
1195 for ( ; j != 0; --j)
1196 install_reloc (sec, relocs[i - j], fixp->fx_frag,
1197 fixp->fx_file, fixp->fx_line);
1203 unsigned int i, j, nsyms;
1205 sympp = bfd_get_outsymbols (stdoutput);
1206 nsyms = bfd_get_symcount (stdoutput);
1207 for (i = 0; i < n; i++)
1208 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1210 for (j = 0; j < nsyms; j++)
1211 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1219 for (r = my_reloc_list; r != NULL; r = r->next)
1222 for (f = seginfo->frchainP->frch_root; f; f = f->fr_next)
1223 if (f->fr_address <= r->u.b.r.address
1224 && r->u.b.r.address < f->fr_address + f->fr_fix)
1227 as_bad_where (r->file, r->line,
1228 _("reloc not within (fixed part of) section"));
1231 relocs[n++] = &r->u.b.r;
1232 install_reloc (sec, &r->u.b.r, f, r->file, r->line);
1238 flagword flags = bfd_get_section_flags (abfd, sec);
1240 bfd_set_section_flags (abfd, sec, flags);
1241 bfd_set_reloc (stdoutput, sec, relocs, n);
1244 #ifdef SET_SECTION_RELOCS
1245 SET_SECTION_RELOCS (sec, relocs, n);
1253 fprintf (stderr, "relocs for sec %s\n", sec->name);
1254 for (i = 0; i < n; i++)
1257 s = *r->sym_ptr_ptr;
1258 fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1259 i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
1266 write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1268 void *xxx ATTRIBUTE_UNUSED)
1270 segment_info_type *seginfo = seg_info (sec);
1271 addressT offset = 0;
1274 /* Write out the frags. */
1276 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1279 for (f = seginfo->frchainP->frch_root;
1288 assert (f->fr_type == rs_fill);
1291 x = bfd_set_section_contents (stdoutput, sec,
1292 f->fr_literal, (file_ptr) offset,
1293 (bfd_size_type) f->fr_fix);
1295 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1296 bfd_errmsg (bfd_get_error ()));
1297 offset += f->fr_fix;
1299 fill_literal = f->fr_literal + f->fr_fix;
1300 fill_size = f->fr_var;
1301 count = f->fr_offset;
1302 assert (count >= 0);
1303 if (fill_size && count)
1306 if (fill_size > sizeof (buf))
1308 /* Do it the old way. Can this ever happen? */
1311 x = bfd_set_section_contents (stdoutput, sec,
1314 (bfd_size_type) fill_size);
1316 as_fatal (_("can't write %s: %s"), stdoutput->filename,
1317 bfd_errmsg (bfd_get_error ()));
1318 offset += fill_size;
1323 /* Build a buffer full of fill objects and output it as
1324 often as necessary. This saves on the overhead of
1325 potentially lots of bfd_set_section_contents calls. */
1329 n_per_buf = sizeof (buf);
1330 memset (buf, *fill_literal, n_per_buf);
1335 n_per_buf = sizeof (buf) / fill_size;
1336 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1337 memcpy (bufp, fill_literal, fill_size);
1339 for (; count > 0; count -= n_per_buf)
1341 n_per_buf = n_per_buf > count ? count : n_per_buf;
1342 x = bfd_set_section_contents
1343 (stdoutput, sec, buf, (file_ptr) offset,
1344 (bfd_size_type) n_per_buf * fill_size);
1346 as_fatal (_("cannot write to output file"));
1347 offset += n_per_buf * fill_size;
1355 merge_data_into_text (void)
1357 seg_info (text_section)->frchainP->frch_last->fr_next =
1358 seg_info (data_section)->frchainP->frch_root;
1359 seg_info (text_section)->frchainP->frch_last =
1360 seg_info (data_section)->frchainP->frch_last;
1361 seg_info (data_section)->frchainP = 0;
1372 /* Count symbols. We can't rely on a count made by the loop in
1373 write_object_file, because *_frob_file may add a new symbol or
1376 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1382 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1384 asympp = bfd_alloc (stdoutput, amt);
1385 symp = symbol_rootP;
1386 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1388 asympp[i] = symbol_get_bfdsym (symp);
1389 if (asympp[i]->flags != BSF_SECTION_SYM
1390 || !(bfd_is_const_section (asympp[i]->section)
1391 && asympp[i]->section->symbol == asympp[i]))
1392 asympp[i]->flags |= BSF_KEEP;
1393 symbol_mark_written (symp);
1398 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1400 symbol_table_frozen = 1;
1403 /* Finish the subsegments. After every sub-segment, we fake an
1404 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1405 ".fill 0" because that is the kind of frag that requires least
1406 thought. ".align" frags like to have a following frag since that
1407 makes calculating their intended length trivial. */
1409 #ifndef SUB_SEGMENT_ALIGN
1411 /* The last subsegment gets an alignment corresponding to the alignment
1412 of the section. This allows proper nop-filling at the end of
1413 code-bearing sections. */
1414 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1415 (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1417 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1422 subsegs_finish (void)
1424 struct frchain *frchainP;
1427 for (s = stdoutput->sections; s; s = s->next)
1429 segment_info_type *seginfo = seg_info (s);
1433 for (frchainP = seginfo->frchainP;
1435 frchainP = frchainP->frch_next)
1439 subseg_set (s, frchainP->frch_subseg);
1441 /* This now gets called even if we had errors. In that case,
1442 any alignment is meaningless, and, moreover, will look weird
1443 if we are generating a listing. */
1446 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1447 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1448 && now_seg->entsize)
1450 unsigned int entsize = now_seg->entsize;
1453 while ((entsize & 1) == 0)
1458 if (entalign > alignment)
1459 alignment = entalign;
1463 if (subseg_text_p (now_seg))
1464 frag_align_code (alignment, 0);
1466 frag_align (alignment, 0, 0);
1468 /* frag_align will have left a new frag.
1469 Use this last frag for an empty ".fill".
1471 For this segment ...
1472 Create a last frag. Do not leave a "being filled in frag". */
1473 frag_wane (frag_now);
1474 frag_now->fr_fix = 0;
1475 know (frag_now->fr_next == NULL);
1480 /* Write the object file. */
1483 write_object_file (void)
1485 struct relax_seg_info rsi;
1486 #ifndef WORKING_DOT_WORD
1487 fragS *fragP; /* Track along all frags. */
1490 /* Do we really want to write it? */
1492 int n_warns, n_errs;
1493 n_warns = had_warnings ();
1494 n_errs = had_errors ();
1495 /* The -Z flag indicates that an object file should be generated,
1496 regardless of warnings and errors. */
1497 if (flag_always_generate_output)
1499 if (n_warns || n_errs)
1500 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1501 n_errs, n_errs == 1 ? "" : "s",
1502 n_warns, n_warns == 1 ? "" : "s");
1507 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1508 n_errs, n_errs == 1 ? "" : "s",
1509 n_warns, n_warns == 1 ? "" : "s");
1514 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1515 a routine to check for the definition of the procedure "_main",
1516 and if so -- fix it up so that it can be program entry point. */
1517 vms_check_for_main ();
1518 #endif /* OBJ_VMS */
1520 /* From now on, we don't care about sub-segments. Build one frag chain
1521 for each segment. Linked thru fr_next. */
1523 /* Remove the sections created by gas for its own purposes. */
1527 bfd_section_list_remove (stdoutput, reg_section);
1528 bfd_section_list_remove (stdoutput, expr_section);
1529 stdoutput->section_count -= 2;
1531 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1534 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1536 /* We have two segments. If user gave -R flag, then we must put the
1537 data frags into the text segment. Do this before relaxing so
1538 we know to take advantage of -R and make shorter addresses. */
1539 if (flag_readonly_data_in_text)
1541 merge_data_into_text ();
1547 #ifndef WORKING_DOT_WORD
1548 /* We need to reset the markers in the broken word list and
1549 associated frags between calls to relax_segment (via
1550 relax_seg). Since the broken word list is global, we do it
1551 once per round, rather than locally in relax_segment for each
1553 struct broken_word *brokp;
1555 for (brokp = broken_words;
1556 brokp != (struct broken_word *) NULL;
1557 brokp = brokp->next_broken_word)
1561 if (brokp->dispfrag != (fragS *) NULL
1562 && brokp->dispfrag->fr_type == rs_broken_word)
1563 brokp->dispfrag->fr_subtype = 0;
1568 bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1574 /* Note - Most ports will use the default value of
1575 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1576 local symbols to be resolved, removing their frag information.
1577 Some ports however, will not have finished relaxing all of
1578 their frags and will still need the local symbol frag
1579 information. These ports can set
1580 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1581 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1583 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1585 /* Relaxation has completed. Freeze all syms. */
1588 #ifdef md_post_relax_hook
1592 #ifndef WORKING_DOT_WORD
1594 struct broken_word *lie;
1595 struct broken_word **prevP;
1597 prevP = &broken_words;
1598 for (lie = broken_words; lie; lie = lie->next_broken_word)
1603 subseg_change (lie->seg, lie->subseg);
1604 exp.X_op = O_subtract;
1605 exp.X_add_symbol = lie->add;
1606 exp.X_op_symbol = lie->sub;
1607 exp.X_add_number = lie->addnum;
1608 #ifdef TC_CONS_FIX_NEW
1609 TC_CONS_FIX_NEW (lie->frag,
1610 lie->word_goes_here - lie->frag->fr_literal,
1613 fix_new_exp (lie->frag,
1614 lie->word_goes_here - lie->frag->fr_literal,
1615 2, &exp, 0, BFD_RELOC_16);
1617 *prevP = lie->next_broken_word;
1620 prevP = &(lie->next_broken_word);
1622 for (lie = broken_words; lie;)
1624 struct broken_word *untruth;
1626 addressT table_addr;
1627 addressT from_addr, to_addr;
1630 subseg_change (lie->seg, lie->subseg);
1631 fragP = lie->dispfrag;
1633 /* Find out how many broken_words go here. */
1636 untruth && untruth->dispfrag == fragP;
1637 untruth = untruth->next_broken_word)
1638 if (untruth->added == 1)
1641 table_ptr = lie->dispfrag->fr_opcode;
1642 table_addr = (lie->dispfrag->fr_address
1643 + (table_ptr - lie->dispfrag->fr_literal));
1644 /* Create the jump around the long jumps. This is a short
1645 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1646 from_addr = table_addr;
1647 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1648 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1650 table_ptr += md_short_jump_size;
1651 table_addr += md_short_jump_size;
1654 lie && lie->dispfrag == fragP;
1655 m++, lie = lie->next_broken_word)
1657 if (lie->added == 2)
1659 /* Patch the jump table. */
1660 /* This is the offset from ??? to table_ptr+0. */
1661 to_addr = table_addr - S_GET_VALUE (lie->sub);
1662 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1663 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1665 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1666 for (untruth = lie->next_broken_word;
1667 untruth && untruth->dispfrag == fragP;
1668 untruth = untruth->next_broken_word)
1670 if (untruth->use_jump == lie)
1671 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1674 /* Install the long jump. */
1675 /* This is a long jump from table_ptr+0 to the final target. */
1676 from_addr = table_addr;
1677 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1678 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1680 table_ptr += md_long_jump_size;
1681 table_addr += md_long_jump_size;
1685 #endif /* not WORKING_DOT_WORD */
1687 /* Resolve symbol values. This needs to be done before processing
1693 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1694 resolve_symbol_value (symp);
1696 resolve_local_symbol_values ();
1697 resolve_reloc_expr_symbols ();
1701 #ifdef tc_frob_file_before_adjust
1702 tc_frob_file_before_adjust ();
1704 #ifdef obj_frob_file_before_adjust
1705 obj_frob_file_before_adjust ();
1708 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1710 #ifdef tc_frob_file_before_fix
1711 tc_frob_file_before_fix ();
1713 #ifdef obj_frob_file_before_fix
1714 obj_frob_file_before_fix ();
1717 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1719 /* Set up symbol table, and write it out. */
1723 bfd_boolean skip_next_symbol = FALSE;
1725 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1730 if (skip_next_symbol)
1732 /* Don't do anything besides moving the value of the
1733 symbol from the GAS value-field to the BFD value-field. */
1734 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1735 skip_next_symbol = FALSE;
1739 if (symbol_mri_common_p (symp))
1741 if (S_IS_EXTERNAL (symp))
1742 as_bad (_("%s: global symbols not supported in common sections"),
1744 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1748 name = S_GET_NAME (symp);
1752 decode_local_label_name ((char *) S_GET_NAME (symp));
1753 /* They only differ if `name' is a fb or dollar local
1755 if (name2 != name && ! S_IS_DEFINED (symp))
1756 as_bad (_("local label `%s' is not defined"), name2);
1759 /* Do it again, because adjust_reloc_syms might introduce
1760 more symbols. They'll probably only be section symbols,
1761 but they'll still need to have the values computed. */
1762 resolve_symbol_value (symp);
1764 /* Skip symbols which were equated to undefined or common
1766 if (symbol_equated_reloc_p (symp)
1767 || S_IS_WEAKREFR (symp))
1769 const char *name = S_GET_NAME (symp);
1770 if (S_IS_COMMON (symp)
1771 && !TC_FAKE_LABEL (name)
1772 && !S_IS_WEAKREFR (symp)
1773 && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1775 expressionS *e = symbol_get_value_expression (symp);
1776 as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1777 name, S_GET_NAME (e->X_add_symbol));
1779 if (S_GET_SEGMENT (symp) == reg_section)
1781 /* Report error only if we know the symbol name. */
1782 if (S_GET_NAME (symp) != reg_section->name)
1783 as_bad (_("can't make global register symbol `%s'"),
1786 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1790 #ifdef obj_frob_symbol
1791 obj_frob_symbol (symp, punt);
1793 #ifdef tc_frob_symbol
1794 if (! punt || symbol_used_in_reloc_p (symp))
1795 tc_frob_symbol (symp, punt);
1798 /* If we don't want to keep this symbol, splice it out of
1799 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1800 want section symbols. Otherwise, we skip local symbols
1801 and symbols that the frob_symbol macros told us to punt,
1802 but we keep such symbols if they are used in relocs. */
1803 if (symp == abs_section_sym
1804 || (! EMIT_SECTION_SYMBOLS
1805 && symbol_section_p (symp))
1806 /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1807 opposites. Sometimes the former checks flags and the
1808 latter examines the name... */
1809 || (!S_IS_EXTERNAL (symp)
1810 && (punt || S_IS_LOCAL (symp) ||
1811 (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
1812 && ! symbol_used_in_reloc_p (symp)))
1814 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1816 /* After symbol_remove, symbol_next(symp) still returns
1817 the one that came after it in the chain. So we don't
1818 need to do any extra cleanup work here. */
1822 /* Make sure we really got a value for the symbol. */
1823 if (! symbol_resolved_p (symp))
1825 as_bad (_("can't resolve value for symbol `%s'"),
1827 symbol_mark_resolved (symp);
1830 /* Set the value into the BFD symbol. Up til now the value
1831 has only been kept in the gas symbolS struct. */
1832 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1834 /* A warning construct is a warning symbol followed by the
1835 symbol warned about. Don't let anything object-format or
1836 target-specific muck with it; it's ready for output. */
1837 if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1838 skip_next_symbol = TRUE;
1844 /* Now do any format-specific adjustments to the symbol table, such
1845 as adding file symbols. */
1846 #ifdef tc_adjust_symtab
1847 tc_adjust_symtab ();
1849 #ifdef obj_adjust_symtab
1850 obj_adjust_symtab ();
1853 /* Stop if there is an error. */
1857 /* Now that all the sizes are known, and contents correct, we can
1858 start writing to the file. */
1861 /* If *_frob_file changes the symbol value at this point, it is
1862 responsible for moving the changed value into symp->bsym->value
1863 as well. Hopefully all symbol value changing can be done in
1868 #ifdef obj_frob_file
1872 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1874 #ifdef tc_frob_file_after_relocs
1875 tc_frob_file_after_relocs ();
1877 #ifdef obj_frob_file_after_relocs
1878 obj_frob_file_after_relocs ();
1881 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1884 #ifdef TC_GENERIC_RELAX_TABLE
1885 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1888 relax_frag (segT segment, fragS *fragP, long stretch)
1890 const relax_typeS *this_type;
1891 const relax_typeS *start_type;
1892 relax_substateT next_state;
1893 relax_substateT this_state;
1899 const relax_typeS *table;
1901 target = fragP->fr_offset;
1902 address = fragP->fr_address;
1903 table = TC_GENERIC_RELAX_TABLE;
1904 this_state = fragP->fr_subtype;
1905 start_type = this_type = table + this_state;
1906 symbolP = fragP->fr_symbol;
1912 sym_frag = symbol_get_frag (symbolP);
1914 #ifndef DIFF_EXPR_OK
1915 know (sym_frag != NULL);
1917 know (S_GET_SEGMENT (symbolP) != absolute_section
1918 || sym_frag == &zero_address_frag);
1919 target += S_GET_VALUE (symbolP);
1921 /* If frag has yet to be reached on this pass,
1922 assume it will move by STRETCH just as we did.
1923 If this is not so, it will be because some frag
1924 between grows, and that will force another pass. */
1927 && sym_frag->relax_marker != fragP->relax_marker
1928 && S_GET_SEGMENT (symbolP) == segment)
1934 aim = target - address - fragP->fr_fix;
1935 #ifdef TC_PCREL_ADJUST
1936 /* Currently only the ns32k family needs this. */
1937 aim += TC_PCREL_ADJUST (fragP);
1940 #ifdef md_prepare_relax_scan
1941 /* Formerly called M68K_AIM_KLUDGE. */
1942 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
1947 /* Look backwards. */
1948 for (next_state = this_type->rlx_more; next_state;)
1949 if (aim >= this_type->rlx_backward)
1953 /* Grow to next state. */
1954 this_state = next_state;
1955 this_type = table + this_state;
1956 next_state = this_type->rlx_more;
1961 /* Look forwards. */
1962 for (next_state = this_type->rlx_more; next_state;)
1963 if (aim <= this_type->rlx_forward)
1967 /* Grow to next state. */
1968 this_state = next_state;
1969 this_type = table + this_state;
1970 next_state = this_type->rlx_more;
1974 growth = this_type->rlx_length - start_type->rlx_length;
1976 fragP->fr_subtype = this_state;
1980 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
1982 /* Relax_align. Advance location counter to next address that has 'alignment'
1983 lowest order bits all 0s, return size of adjustment made. */
1984 static relax_addressT
1985 relax_align (register relax_addressT address, /* Address now. */
1986 register int alignment /* Alignment (binary). */)
1988 relax_addressT mask;
1989 relax_addressT new_address;
1991 mask = ~((~0) << alignment);
1992 new_address = (address + mask) & (~mask);
1993 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1995 /* We must provide lots of padding, so the linker can discard it
1996 when needed. The linker will not add extra space, ever. */
1997 new_address += (1 << alignment);
1999 return (new_address - address);
2002 /* Now we have a segment, not a crowd of sub-segments, we can make
2007 After this, all frags in this segment have addresses that are correct
2008 within the segment. Since segments live in different file addresses,
2009 these frag addresses may not be the same as final object-file
2013 relax_segment (struct frag *segment_frag_root, segT segment, int pass)
2015 unsigned long frag_count;
2017 relax_addressT address;
2020 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2021 subseg_change (segment, 0);
2023 /* For each frag in segment: count and store (a 1st guess of)
2026 for (frag_count = 0, fragP = segment_frag_root;
2028 fragP = fragP->fr_next, frag_count ++)
2030 fragP->relax_marker = 0;
2031 fragP->fr_address = address;
2032 address += fragP->fr_fix;
2034 switch (fragP->fr_type)
2037 address += fragP->fr_offset * fragP->fr_var;
2044 addressT offset = relax_align (address, (int) fragP->fr_offset);
2046 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2049 if (offset % fragP->fr_var != 0)
2051 as_bad_where (fragP->fr_file, fragP->fr_line,
2052 _("alignment padding (%lu bytes) not a multiple of %ld"),
2053 (unsigned long) offset, (long) fragP->fr_var);
2054 offset -= (offset % fragP->fr_var);
2063 /* Assume .org is nugatory. It will grow with 1st relax. */
2066 case rs_machine_dependent:
2067 /* If fr_symbol is an expression, this call to
2068 resolve_symbol_value sets up the correct segment, which will
2069 likely be needed in md_estimate_size_before_relax. */
2070 if (fragP->fr_symbol)
2071 resolve_symbol_value (fragP->fr_symbol);
2073 address += md_estimate_size_before_relax (fragP, segment);
2076 #ifndef WORKING_DOT_WORD
2077 /* Broken words don't concern us yet. */
2078 case rs_broken_word:
2083 /* Initial guess is always 1; doing otherwise can result in
2084 stable solutions that are larger than the minimum. */
2085 address += fragP->fr_offset = 1;
2089 address += eh_frame_estimate_size_before_relax (fragP);
2093 address += dwarf2dbg_estimate_size_before_relax (fragP);
2097 BAD_CASE (fragP->fr_type);
2104 unsigned long max_iterations;
2106 /* Cumulative address adjustment. */
2109 /* Have we made any adjustment this pass? We can't just test
2110 stretch because one piece of code may have grown and another
2114 /* Most horrible, but gcc may give us some exception data that
2115 is impossible to assemble, of the form
2119 .uleb128 end - start
2125 If the leb128 is two bytes in size, then end-start is 128*128,
2126 which requires a three byte leb128. If the leb128 is three
2127 bytes in size, then end-start is 128*128-1, which requires a
2128 two byte leb128. We work around this dilemma by inserting
2129 an extra 4 bytes of alignment just after the .align. This
2130 works because the data after the align is accessed relative to
2133 This counter is used in a tiny state machine to detect
2134 whether a leb128 followed by an align is impossible to
2136 int rs_leb128_fudge = 0;
2138 /* We want to prevent going into an infinite loop where one frag grows
2139 depending upon the location of a symbol which is in turn moved by
2140 the growing frag. eg:
2146 So we dictate that this algorithm can be at most O2. */
2147 max_iterations = frag_count * frag_count;
2148 /* Check for overflow. */
2149 if (max_iterations < frag_count)
2150 max_iterations = frag_count;
2158 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2161 addressT was_address;
2165 fragP->relax_marker ^= 1;
2166 was_address = fragP->fr_address;
2167 address = fragP->fr_address += stretch;
2168 symbolP = fragP->fr_symbol;
2169 offset = fragP->fr_offset;
2171 switch (fragP->fr_type)
2173 case rs_fill: /* .fill never relaxes. */
2177 #ifndef WORKING_DOT_WORD
2178 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2179 for it I do not want to write it. I do not want to have
2180 anything to do with it. This is not the proper way to
2181 implement this misfeature. */
2182 case rs_broken_word:
2184 struct broken_word *lie;
2185 struct broken_word *untruth;
2187 /* Yes this is ugly (storing the broken_word pointer
2188 in the symbol slot). Still, this whole chunk of
2189 code is ugly, and I don't feel like doing anything
2190 about it. Think of it as stubbornness in action. */
2192 for (lie = (struct broken_word *) (fragP->fr_symbol);
2193 lie && lie->dispfrag == fragP;
2194 lie = lie->next_broken_word)
2200 offset = (S_GET_VALUE (lie->add)
2202 - S_GET_VALUE (lie->sub));
2203 if (offset <= -32768 || offset >= 32767)
2205 if (flag_warn_displacement)
2208 sprint_value (buf, (addressT) lie->addnum);
2209 as_warn_where (fragP->fr_file, fragP->fr_line,
2210 _(".word %s-%s+%s didn't fit"),
2211 S_GET_NAME (lie->add),
2212 S_GET_NAME (lie->sub),
2216 if (fragP->fr_subtype == 0)
2218 fragP->fr_subtype++;
2219 growth += md_short_jump_size;
2221 for (untruth = lie->next_broken_word;
2222 untruth && untruth->dispfrag == lie->dispfrag;
2223 untruth = untruth->next_broken_word)
2224 if ((symbol_get_frag (untruth->add)
2225 == symbol_get_frag (lie->add))
2226 && (S_GET_VALUE (untruth->add)
2227 == S_GET_VALUE (lie->add)))
2230 untruth->use_jump = lie;
2232 growth += md_long_jump_size;
2237 } /* case rs_broken_word */
2243 addressT oldoff, newoff;
2245 oldoff = relax_align (was_address + fragP->fr_fix,
2247 newoff = relax_align (address + fragP->fr_fix,
2250 if (fragP->fr_subtype != 0)
2252 if (oldoff > fragP->fr_subtype)
2254 if (newoff > fragP->fr_subtype)
2258 growth = newoff - oldoff;
2260 /* If this align happens to follow a leb128 and
2261 we have determined that the leb128 is bouncing
2262 in size, then break the cycle by inserting an
2265 && (rs_leb128_fudge & 16) != 0
2266 && (rs_leb128_fudge & 15) >= 2)
2268 segment_info_type *seginfo = seg_info (segment);
2269 struct obstack *ob = &seginfo->frchainP->frch_obstack;
2272 newf = frag_alloc (ob);
2273 obstack_blank_fast (ob, fragP->fr_var);
2274 obstack_finish (ob);
2275 memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2276 memcpy (newf->fr_literal,
2277 fragP->fr_literal + fragP->fr_fix,
2279 newf->fr_type = rs_fill;
2281 newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2283 if (newf->fr_offset * newf->fr_var
2284 != (offsetT) 1 << fragP->fr_offset)
2286 newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2289 /* Include growth of new frag, because rs_fill
2290 frags don't normally grow. */
2291 growth += newf->fr_offset * newf->fr_var;
2292 /* The new frag address is newoff. Adjust this
2293 for the amount we'll add when we process the
2295 newf->fr_address = newoff - stretch - growth;
2296 newf->relax_marker ^= 1;
2297 fragP->fr_next = newf;
2299 as_warn (_("padding added"));
2307 addressT target = offset;
2312 /* Convert from an actual address to an octet offset
2313 into the section. Here it is assumed that the
2314 section's VMA is zero, and can omit subtracting it
2315 from the symbol's value to get the address offset. */
2316 know (S_GET_SEGMENT (symbolP)->vma == 0);
2317 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2320 know (fragP->fr_next);
2321 after = fragP->fr_next->fr_address + stretch;
2322 growth = target - after;
2327 /* Don't error on first few frag relax passes.
2328 The symbol might be an expression involving
2329 symbol values from other sections. If those
2330 sections have not yet been processed their
2331 frags will all have zero addresses, so we
2332 will calculate incorrect values for them. The
2333 number of passes we allow before giving an
2334 error is somewhat arbitrary. It should be at
2335 least one, with larger values requiring
2336 increasingly contrived dependencies between
2337 frags to trigger a false error. */
2340 /* Force another pass. */
2345 /* Growth may be negative, but variable part of frag
2346 cannot have fewer than 0 chars. That is, we can't
2348 as_bad_where (fragP->fr_file, fragP->fr_line,
2349 _("attempt to move .org backwards"));
2351 /* We've issued an error message. Change the
2352 frag to avoid cascading errors. */
2353 fragP->fr_type = rs_align;
2354 fragP->fr_subtype = 0;
2355 fragP->fr_offset = 0;
2356 fragP->fr_fix = after - address;
2367 amount = S_GET_VALUE (symbolP);
2368 if (S_GET_SEGMENT (symbolP) != absolute_section
2369 || S_IS_COMMON (symbolP)
2370 || ! S_IS_DEFINED (symbolP))
2372 as_bad_where (fragP->fr_file, fragP->fr_line,
2373 _(".space specifies non-absolute value"));
2374 /* Prevent repeat of this error message. */
2375 fragP->fr_symbol = 0;
2377 else if (amount < 0)
2379 /* Don't error on first few frag relax passes.
2380 See rs_org comment for a longer explanation. */
2387 as_warn_where (fragP->fr_file, fragP->fr_line,
2388 _(".space or .fill with negative value, ignored"));
2389 fragP->fr_symbol = 0;
2392 growth = (was_address + fragP->fr_fix + amount
2393 - fragP->fr_next->fr_address);
2397 case rs_machine_dependent:
2398 #ifdef md_relax_frag
2399 growth = md_relax_frag (segment, fragP, stretch);
2401 #ifdef TC_GENERIC_RELAX_TABLE
2402 /* The default way to relax a frag is to look through
2403 TC_GENERIC_RELAX_TABLE. */
2404 growth = relax_frag (segment, fragP, stretch);
2405 #endif /* TC_GENERIC_RELAX_TABLE */
2414 value = resolve_symbol_value (fragP->fr_symbol);
2415 size = sizeof_leb128 (value, fragP->fr_subtype);
2416 growth = size - fragP->fr_offset;
2417 fragP->fr_offset = size;
2422 growth = eh_frame_relax_frag (fragP);
2426 growth = dwarf2dbg_relax_frag (fragP);
2430 BAD_CASE (fragP->fr_type);
2437 if (fragP->fr_type == rs_leb128)
2438 rs_leb128_fudge += 16;
2439 else if (fragP->fr_type == rs_align
2440 && (rs_leb128_fudge & 16) != 0
2442 rs_leb128_fudge += 16;
2444 rs_leb128_fudge = 0;
2449 && (rs_leb128_fudge & 16) == 0
2450 && (rs_leb128_fudge & -16) != 0)
2451 rs_leb128_fudge += 1;
2453 rs_leb128_fudge = 0;
2455 /* Until nothing further to relax. */
2456 while (stretched && -- max_iterations);
2459 as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2460 segment_name (segment));
2463 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2464 if (fragP->last_fr_address != fragP->fr_address)
2466 fragP->last_fr_address = fragP->fr_address;
2473 number_to_chars_bigendian (char *buf, valueT val, int n)
2479 buf[n] = val & 0xff;
2485 number_to_chars_littleendian (char *buf, valueT val, int n)
2491 *buf++ = val & 0xff;
2497 write_print_statistics (FILE *file)
2499 fprintf (file, "fixups: %d\n", n_fixups);
2502 /* For debugging. */
2503 extern int indent_level;
2506 print_fixup (fixS *fixp)
2509 fprintf (stderr, "fix ");
2510 fprintf_vma (stderr, (bfd_vma)((bfd_hostptr_t) fixp));
2511 fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line);
2513 fprintf (stderr, " pcrel");
2514 if (fixp->fx_pcrel_adjust)
2515 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2516 if (fixp->fx_im_disp)
2519 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2521 fprintf (stderr, " im_disp");
2525 fprintf (stderr, " tcbit");
2527 fprintf (stderr, " done");
2528 fprintf (stderr, "\n size=%d frag=", fixp->fx_size);
2529 fprintf_vma (stderr, (bfd_vma) ((bfd_hostptr_t) fixp->fx_frag));
2530 fprintf (stderr, " where=%ld offset=%lx addnumber=%lx",
2531 (long) fixp->fx_where,
2532 (unsigned long) fixp->fx_offset,
2533 (unsigned long) fixp->fx_addnumber);
2534 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2538 fprintf (stderr, "\n +<");
2539 print_symbol_value_1 (stderr, fixp->fx_addsy);
2540 fprintf (stderr, ">");
2544 fprintf (stderr, "\n -<");
2545 print_symbol_value_1 (stderr, fixp->fx_subsy);
2546 fprintf (stderr, ">");
2548 fprintf (stderr, "\n");
2549 #ifdef TC_FIX_DATA_PRINT
2550 TC_FIX_DATA_PRINT (stderr, fixp);