1 /* write.c - emit .o file
2 Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This thing should be set up to do byteordering correctly. But... */
28 #include "output-file.h"
29 #include "dwarf2dbg.h"
31 #ifndef TC_ADJUST_RELOC_COUNT
32 #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
35 #ifndef TC_FORCE_RELOCATION
36 #define TC_FORCE_RELOCATION(FIX) \
37 (S_FORCE_RELOC ((FIX)->fx_addsy))
40 #ifndef TC_FORCE_RELOCATION_ABS
41 #define TC_FORCE_RELOCATION_ABS(FIX) \
42 (TC_FORCE_RELOCATION (FIX))
45 #ifndef TC_FORCE_RELOCATION_LOCAL
46 #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
75 #define TC_VALIDATE_FIX_SUB(FIX) \
76 ((FIX)->fx_r_type == BFD_RELOC_GPREL32 \
77 || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
79 #define TC_VALIDATE_FIX_SUB(FIX) 0
84 #ifndef TC_LINKRELAX_FIXUP
85 #define TC_LINKRELAX_FIXUP(SEG) 1
88 #ifndef MD_APPLY_SYM_VALUE
89 #define MD_APPLY_SYM_VALUE(FIX) 1
92 #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
93 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
96 #ifndef MD_PCREL_FROM_SECTION
97 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
100 #ifndef WORKING_DOT_WORD
101 extern const int md_short_jump_size;
102 extern const int md_long_jump_size;
105 /* Used to control final evaluation of expressions. */
106 int finalize_syms = 0;
108 int symbol_table_frozen;
110 symbolS *abs_section_sym;
112 /* Remember the value of dot when parsing expressions. */
115 void print_fixup PARAMS ((fixS *));
118 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
120 /* We generally attach relocs to frag chains. However, after we have
121 chained these all together into a segment, any relocs we add after
122 that must be attached to a segment. This will include relocs added
123 in md_estimate_size_for_relax, for example. */
124 static int frags_chained = 0;
127 #ifndef BFD_ASSEMBLER
129 #ifndef MANY_SEGMENTS
130 struct frag *text_frag_root;
131 struct frag *data_frag_root;
132 struct frag *bss_frag_root;
134 struct frag *text_last_frag; /* Last frag in segment. */
135 struct frag *data_last_frag; /* Last frag in segment. */
136 static struct frag *bss_last_frag; /* Last frag in segment. */
140 static object_headers headers;
143 long string_byte_count;
144 char *next_object_file_charP; /* Tracks object file bytes. */
147 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
150 #endif /* BFD_ASSEMBLER */
155 #define RELOC_ENUM enum bfd_reloc_code_real
157 #define RELOC_ENUM int
160 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
161 symbolS *add, symbolS *sub,
162 offsetT offset, int pcrel,
164 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
165 static long fixup_segment PARAMS ((fixS *, segT));
167 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
168 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
169 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
172 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
173 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
174 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
175 static void fix_segment PARAMS ((bfd *, asection *, PTR));
176 static void write_relocs PARAMS ((bfd *, asection *, PTR));
177 static void write_contents PARAMS ((bfd *, asection *, PTR));
178 static void set_symtab PARAMS ((void));
180 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
181 static void merge_data_into_text PARAMS ((void));
183 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
184 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
185 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
186 static void relax_and_size_all_segments PARAMS ((void));
189 /* Create a fixS in obstack 'notes'. */
192 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
194 fragS *frag; /* Which frag? */
195 int where; /* Where in that frag? */
196 int size; /* 1, 2, or 4 usually. */
197 symbolS *add_symbol; /* X_add_symbol. */
198 symbolS *sub_symbol; /* X_op_symbol. */
199 offsetT offset; /* X_add_number. */
200 int pcrel; /* TRUE if PC-relative relocation. */
201 RELOC_ENUM r_type ATTRIBUTE_UNUSED; /* Relocation type. */
207 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
209 fixP->fx_frag = frag;
210 fixP->fx_where = where;
211 fixP->fx_size = size;
212 /* We've made fx_size a narrow field; check that it's wide enough. */
213 if (fixP->fx_size != size)
215 as_bad (_("field fx_size too small to hold %d"), size);
218 fixP->fx_addsy = add_symbol;
219 fixP->fx_subsy = sub_symbol;
220 fixP->fx_offset = offset;
221 fixP->fx_dot_value = dot_value;
222 fixP->fx_pcrel = pcrel;
224 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
225 fixP->fx_r_type = r_type;
227 fixP->fx_im_disp = 0;
228 fixP->fx_pcrel_adjust = 0;
229 fixP->fx_bit_fixP = 0;
230 fixP->fx_addnumber = 0;
233 fixP->fx_no_overflow = 0;
237 fixP->fx_cgen.insn = NULL;
238 fixP->fx_cgen.opinfo = 0;
242 TC_INIT_FIX_DATA (fixP);
245 as_where (&fixP->fx_file, &fixP->fx_line);
247 /* Usually, we want relocs sorted numerically, but while
248 comparing to older versions of gas that have relocs
249 reverse sorted, it is convenient to have this compile
250 time option. xoxorich. */
254 fixS **seg_fix_rootP = (frags_chained
255 ? &seg_info (now_seg)->fix_root
256 : &frchain_now->fix_root);
257 fixS **seg_fix_tailP = (frags_chained
258 ? &seg_info (now_seg)->fix_tail
259 : &frchain_now->fix_tail);
262 #ifdef REVERSE_SORT_RELOCS
264 fixP->fx_next = *seg_fix_rootP;
265 *seg_fix_rootP = fixP;
267 #else /* REVERSE_SORT_RELOCS */
269 fixP->fx_next = NULL;
272 (*seg_fix_tailP)->fx_next = fixP;
274 *seg_fix_rootP = fixP;
275 *seg_fix_tailP = fixP;
277 #endif /* REVERSE_SORT_RELOCS */
283 /* Create a fixup relative to a symbol (plus a constant). */
286 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
287 fragS *frag; /* Which frag? */
288 int where; /* Where in that frag? */
289 int size; /* 1, 2, or 4 usually. */
290 symbolS *add_symbol; /* X_add_symbol. */
291 offsetT offset; /* X_add_number. */
292 int pcrel; /* TRUE if PC-relative relocation. */
293 RELOC_ENUM r_type; /* Relocation type. */
295 return fix_new_internal (frag, where, size, add_symbol,
296 (symbolS *) NULL, offset, pcrel, r_type);
299 /* Create a fixup for an expression. Currently we only support fixups
300 for difference expressions. That is itself more than most object
301 file formats support anyhow. */
304 fix_new_exp (frag, where, size, exp, pcrel, r_type)
305 fragS *frag; /* Which frag? */
306 int where; /* Where in that frag? */
307 int size; /* 1, 2, or 4 usually. */
308 expressionS *exp; /* Expression. */
309 int pcrel; /* TRUE if PC-relative relocation. */
310 RELOC_ENUM r_type; /* Relocation type. */
322 as_bad (_("register value used as expression"));
326 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
327 the difference expression cannot immediately be reduced. */
329 symbolS *stmp = make_expr_symbol (exp);
331 exp->X_op = O_symbol;
332 exp->X_op_symbol = 0;
333 exp->X_add_symbol = stmp;
334 exp->X_add_number = 0;
336 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
340 add = exp->X_add_symbol;
341 off = exp->X_add_number;
343 #if defined(BFD_ASSEMBLER)
344 r_type = BFD_RELOC_RVA;
346 #if defined(TC_RVA_RELOC)
347 r_type = TC_RVA_RELOC;
349 as_fatal (_("rva not supported"));
355 sub = exp->X_add_symbol;
356 off = exp->X_add_number;
360 sub = exp->X_op_symbol;
363 add = exp->X_add_symbol;
366 off = exp->X_add_number;
370 add = make_expr_symbol (exp);
374 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
377 /* Append a string onto another string, bumping the pointer along. */
379 append (charPP, fromP, length)
382 unsigned long length;
384 /* Don't trust memcpy() of 0 chars. */
388 memcpy (*charPP, fromP, length);
392 #ifndef BFD_ASSEMBLER
393 int section_alignment[SEG_MAXIMUM_ORDINAL];
396 /* This routine records the largest alignment seen for each segment.
397 If the beginning of the segment is aligned on the worst-case
398 boundary, all of the other alignments within it will work. At
399 least one object format really uses this info. */
402 record_alignment (seg, align)
403 /* Segment to which alignment pertains. */
405 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
409 if (seg == absolute_section)
412 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
413 bfd_set_section_alignment (stdoutput, seg, align);
415 if (align > section_alignment[(int) seg])
416 section_alignment[(int) seg] = align;
421 get_recorded_alignment (seg)
424 if (seg == absolute_section)
427 return bfd_get_section_alignment (stdoutput, seg);
429 return section_alignment[(int) seg];
435 /* Reset the section indices after removing the gas created sections. */
438 renumber_sections (abfd, sec, countparg)
439 bfd *abfd ATTRIBUTE_UNUSED;
443 int *countp = (int *) countparg;
445 sec->index = *countp;
449 #endif /* defined (BFD_ASSEMBLER) */
451 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
454 chain_frchains_together_1 (section, frchp)
456 struct frchain *frchp;
458 fragS dummy, *prev_frag = &dummy;
460 fixS fix_dummy, *prev_fix = &fix_dummy;
463 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
465 prev_frag->fr_next = frchp->frch_root;
466 prev_frag = frchp->frch_last;
467 assert (prev_frag->fr_type != 0);
469 if (frchp->fix_root != (fixS *) NULL)
471 if (seg_info (section)->fix_root == (fixS *) NULL)
472 seg_info (section)->fix_root = frchp->fix_root;
473 prev_fix->fx_next = frchp->fix_root;
474 seg_info (section)->fix_tail = frchp->fix_tail;
475 prev_fix = frchp->fix_tail;
479 assert (prev_frag->fr_type != 0);
480 prev_frag->fr_next = 0;
489 chain_frchains_together (abfd, section, xxx)
490 bfd *abfd ATTRIBUTE_UNUSED;
492 PTR xxx ATTRIBUTE_UNUSED;
494 segment_info_type *info;
496 /* BFD may have introduced its own sections without using
497 subseg_new, so it is possible that seg_info is NULL. */
498 info = seg_info (section);
499 if (info != (segment_info_type *) NULL)
500 info->frchainP->frch_last
501 = chain_frchains_together_1 (section, info->frchainP);
503 /* Now that we've chained the frags together, we must add new fixups
504 to the segment, not to the frag chain. */
510 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
513 remove_subsegs (head, seg, root, last)
519 *root = head->frch_root;
520 *last = chain_frchains_together_1 (seg, head);
525 #if defined (BFD_ASSEMBLER) || !defined (BFD)
529 cvt_frag_to_fill (sec, fragP)
530 segT sec ATTRIBUTE_UNUSED;
534 cvt_frag_to_fill (headersP, sec, fragP)
535 object_headers *headersP;
540 switch (fragP->fr_type)
548 HANDLE_ALIGN (fragP);
550 know (fragP->fr_next != NULL);
551 fragP->fr_offset = (fragP->fr_next->fr_address
553 - fragP->fr_fix) / fragP->fr_var;
554 if (fragP->fr_offset < 0)
556 as_bad_where (fragP->fr_file, fragP->fr_line,
557 _("attempt to .org/.space backwards? (%ld)"),
558 (long) fragP->fr_offset);
559 fragP->fr_offset = 0;
561 fragP->fr_type = rs_fill;
569 valueT value = S_GET_VALUE (fragP->fr_symbol);
572 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
575 fragP->fr_fix += size;
576 fragP->fr_type = rs_fill;
578 fragP->fr_offset = 0;
579 fragP->fr_symbol = NULL;
584 eh_frame_convert_frag (fragP);
588 dwarf2dbg_convert_frag (fragP);
591 case rs_machine_dependent:
593 md_convert_frag (stdoutput, sec, fragP);
595 md_convert_frag (headersP, sec, fragP);
598 assert (fragP->fr_next == NULL
599 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
602 /* After md_convert_frag, we make the frag into a ".space 0".
603 md_convert_frag() should set up any fixSs and constants
608 #ifndef WORKING_DOT_WORD
611 struct broken_word *lie;
613 if (fragP->fr_subtype)
615 fragP->fr_fix += md_short_jump_size;
616 for (lie = (struct broken_word *) (fragP->fr_symbol);
617 lie && lie->dispfrag == fragP;
618 lie = lie->next_broken_word)
620 fragP->fr_fix += md_long_jump_size;
628 BAD_CASE (fragP->fr_type);
633 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
636 static void relax_seg PARAMS ((bfd *, asection *, PTR));
638 relax_seg (abfd, sec, xxx)
639 bfd *abfd ATTRIBUTE_UNUSED;
643 segment_info_type *seginfo = seg_info (sec);
645 if (seginfo && seginfo->frchainP
646 && relax_segment (seginfo->frchainP->frch_root, sec))
648 int *result = (int *) xxx;
653 static void size_seg PARAMS ((bfd *, asection *, PTR));
655 size_seg (abfd, sec, xxx)
658 PTR xxx ATTRIBUTE_UNUSED;
662 segment_info_type *seginfo;
664 valueT size, newsize;
666 subseg_change (sec, 0);
668 seginfo = seg_info (sec);
669 if (seginfo && seginfo->frchainP)
671 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
672 cvt_frag_to_fill (sec, fragp);
673 for (fragp = seginfo->frchainP->frch_root;
675 fragp = fragp->fr_next)
676 /* Walk to last elt. */
678 size = fragp->fr_address + fragp->fr_fix;
683 flags = bfd_get_section_flags (abfd, sec);
685 if (size > 0 && ! seginfo->bss)
686 flags |= SEC_HAS_CONTENTS;
688 /* @@ This is just an approximation. */
689 if (seginfo && seginfo->fix_root)
693 x = bfd_set_section_flags (abfd, sec, flags);
696 newsize = md_section_align (sec, size);
697 x = bfd_set_section_size (abfd, sec, newsize);
700 /* If the size had to be rounded up, add some padding in the last
702 assert (newsize >= size);
705 fragS *last = seginfo->frchainP->frch_last;
706 fragp = seginfo->frchainP->frch_root;
707 while (fragp->fr_next != last)
708 fragp = fragp->fr_next;
709 last->fr_address = size;
710 if ((newsize - size) % fragp->fr_var == 0)
711 fragp->fr_offset += (newsize - size) / fragp->fr_var;
713 /* If we hit this abort, it's likely due to subsegs_finish not
714 providing sufficient alignment on the last frag, and the
715 machine dependent code using alignment frags with fr_var
720 #ifdef tc_frob_section
721 tc_frob_section (sec);
723 #ifdef obj_frob_section
724 obj_frob_section (sec);
730 dump_section_relocs (abfd, sec, stream_)
731 bfd *abfd ATTRIBUTE_UNUSED;
735 FILE *stream = (FILE *) stream_;
736 segment_info_type *seginfo = seg_info (sec);
737 fixS *fixp = seginfo->fix_root;
742 fprintf (stream, "sec %s relocs:\n", sec->name);
745 symbolS *s = fixp->fx_addsy;
747 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
748 (int) fixp->fx_r_type);
750 fprintf (stream, "no sym\n");
753 print_symbol_value_1 (stream, s);
754 fprintf (stream, "\n");
756 fixp = fixp->fx_next;
760 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
763 #ifndef EMIT_SECTION_SYMBOLS
764 #define EMIT_SECTION_SYMBOLS 1
767 /* This pass over fixups decides whether symbols can be replaced with
771 adjust_reloc_syms (abfd, sec, xxx)
772 bfd *abfd ATTRIBUTE_UNUSED;
774 PTR xxx ATTRIBUTE_UNUSED;
776 segment_info_type *seginfo = seg_info (sec);
782 dump_section_relocs (abfd, sec, stderr);
784 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
788 else if (fixp->fx_addsy)
794 fprintf (stderr, "\n\nadjusting fixup:\n");
798 sym = fixp->fx_addsy;
800 /* All symbols should have already been resolved at this
801 point. It is possible to see unresolved expression
802 symbols, though, since they are not in the regular symbol
804 resolve_symbol_value (sym);
806 if (fixp->fx_subsy != NULL)
807 resolve_symbol_value (fixp->fx_subsy);
809 /* If this symbol is equated to an undefined symbol, convert
810 the fixup to being against that symbol. */
811 if (symbol_equated_reloc_p (sym))
813 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
814 sym = symbol_get_value_expression (sym)->X_add_symbol;
815 fixp->fx_addsy = sym;
818 if (symbol_mri_common_p (sym))
820 /* These symbols are handled specially in fixup_segment. */
824 /* If the symbol is undefined, common, weak, or global (ELF
825 shared libs), we can't replace it with the section symbol. */
826 if (S_FORCE_RELOC (fixp->fx_addsy))
829 /* Is there some other (target cpu dependent) reason we can't adjust
830 this one? (E.g. relocations involving function addresses on
832 #ifdef tc_fix_adjustable
833 if (! tc_fix_adjustable (fixp))
837 /* Since we're reducing to section symbols, don't attempt to reduce
838 anything that's already using one. */
839 if (symbol_section_p (sym))
842 symsec = S_GET_SEGMENT (sym);
846 if (bfd_is_abs_section (symsec))
848 /* The fixup_segment routine normally will not use this
849 symbol in a relocation. */
853 /* Don't try to reduce relocs which refer to non-local symbols
854 in .linkonce sections. It can lead to confusion when a
855 debugging section refers to a .linkonce section. I hope
856 this will always be correct. */
857 if (symsec != sec && ! S_IS_LOCAL (sym))
859 if ((symsec->flags & SEC_LINK_ONCE) != 0
861 /* The GNU toolchain uses an extension for ELF: a
862 section beginning with the magic string
863 .gnu.linkonce is a linkonce section. */
864 && strncmp (segment_name (symsec), ".gnu.linkonce",
865 sizeof ".gnu.linkonce" - 1) == 0))
869 /* Never adjust a reloc against local symbol in a merge section
870 with non-zero addend. */
871 if ((symsec->flags & SEC_MERGE) != 0
872 && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
875 /* Never adjust a reloc against TLS local symbol. */
876 if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
879 /* We refetch the segment when calling section_symbol, rather
880 than using symsec, because S_GET_VALUE may wind up changing
881 the section when it calls resolve_symbol_value. */
882 fixp->fx_offset += S_GET_VALUE (sym);
883 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
885 fprintf (stderr, "\nadjusted fixup:\n");
890 dump_section_relocs (abfd, sec, stderr);
894 fix_segment (abfd, sec, xxx)
895 bfd *abfd ATTRIBUTE_UNUSED;
897 PTR xxx ATTRIBUTE_UNUSED;
899 segment_info_type *seginfo = seg_info (sec);
901 fixup_segment (seginfo->fix_root, sec);
905 write_relocs (abfd, sec, xxx)
908 PTR xxx ATTRIBUTE_UNUSED;
910 segment_info_type *seginfo = seg_info (sec);
917 /* If seginfo is NULL, we did not create this section; don't do
923 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
926 #ifndef RELOC_EXPANSION_POSSIBLE
927 /* Set up reloc information as well. */
928 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
929 memset ((char *) relocs, 0, n * sizeof (arelent *));
932 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
935 bfd_reloc_status_type s;
944 /* If this is an undefined symbol which was equated to another
945 symbol, then generate the reloc against the latter symbol
946 rather than the former. */
947 sym = fixp->fx_addsy;
948 while (symbol_equated_reloc_p (sym))
952 /* We must avoid looping, as that can occur with a badly
954 n = symbol_get_value_expression (sym)->X_add_symbol;
957 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
960 fixp->fx_addsy = sym;
962 reloc = tc_gen_reloc (sec, fixp);
970 /* This test is triggered inappropriately for the SH. */
971 if (fixp->fx_where + fixp->fx_size
972 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
976 s = bfd_install_relocation (stdoutput, reloc,
977 fixp->fx_frag->fr_literal,
978 fixp->fx_frag->fr_address,
984 case bfd_reloc_overflow:
985 as_bad_where (fixp->fx_file, fixp->fx_line,
986 _("relocation overflow"));
988 case bfd_reloc_outofrange:
989 as_bad_where (fixp->fx_file, fixp->fx_line,
990 _("relocation out of range"));
993 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
994 fixp->fx_file, fixp->fx_line, s);
999 n = n * MAX_RELOC_EXPANSION;
1000 /* Set up reloc information as well. */
1001 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
1004 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1008 bfd_reloc_status_type s;
1018 /* If this is an undefined symbol which was equated to another
1019 symbol, then generate the reloc against the latter symbol
1020 rather than the former. */
1021 sym = fixp->fx_addsy;
1022 while (symbol_equated_reloc_p (sym))
1026 /* We must avoid looping, as that can occur with a badly
1028 n = symbol_get_value_expression (sym)->X_add_symbol;
1031 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
1034 fixp->fx_addsy = sym;
1036 reloc = tc_gen_reloc (sec, fixp);
1038 for (j = 0; reloc[j]; j++)
1040 relocs[i++] = reloc[j];
1043 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1044 if (fixp->fx_where + fixp->fx_size
1045 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1046 as_bad_where (fixp->fx_file, fixp->fx_line,
1047 _("internal error: fixup not contained within frag"));
1048 for (j = 0; reloc[j]; j++)
1050 s = bfd_install_relocation (stdoutput, reloc[j],
1051 fixp->fx_frag->fr_literal,
1052 fixp->fx_frag->fr_address,
1058 case bfd_reloc_overflow:
1059 as_bad_where (fixp->fx_file, fixp->fx_line,
1060 _("relocation overflow"));
1062 case bfd_reloc_outofrange:
1063 as_bad_where (fixp->fx_file, fixp->fx_line,
1064 _("relocation out of range"));
1067 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1068 fixp->fx_file, fixp->fx_line, s);
1079 sympp = bfd_get_outsymbols (stdoutput);
1080 nsyms = bfd_get_symcount (stdoutput);
1081 for (i = 0; i < n; i++)
1082 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1084 for (j = 0; j < nsyms; j++)
1085 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1094 bfd_set_reloc (stdoutput, sec, relocs, n);
1096 bfd_set_section_flags (abfd, sec,
1097 (bfd_get_section_flags (abfd, sec)
1098 & (flagword) ~SEC_RELOC));
1100 #ifdef SET_SECTION_RELOCS
1101 SET_SECTION_RELOCS (sec, relocs, n);
1109 fprintf (stderr, "relocs for sec %s\n", sec->name);
1110 for (i = 0; i < n; i++)
1113 s = *r->sym_ptr_ptr;
1114 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1115 i, r, r->address, s->name, r->addend);
1122 write_contents (abfd, sec, xxx)
1123 bfd *abfd ATTRIBUTE_UNUSED;
1125 PTR xxx ATTRIBUTE_UNUSED;
1127 segment_info_type *seginfo = seg_info (sec);
1128 unsigned long offset = 0;
1131 /* Write out the frags. */
1133 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1136 for (f = seginfo->frchainP->frch_root;
1141 unsigned long fill_size;
1145 assert (f->fr_type == rs_fill);
1148 x = bfd_set_section_contents (stdoutput, sec,
1149 f->fr_literal, (file_ptr) offset,
1150 (bfd_size_type) f->fr_fix);
1153 bfd_perror (stdoutput->filename);
1154 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1155 exit (EXIT_FAILURE);
1157 offset += f->fr_fix;
1159 fill_literal = f->fr_literal + f->fr_fix;
1160 fill_size = f->fr_var;
1161 count = f->fr_offset;
1162 assert (count >= 0);
1163 if (fill_size && count)
1166 if (fill_size > sizeof (buf))
1168 /* Do it the old way. Can this ever happen? */
1171 x = bfd_set_section_contents (stdoutput, sec,
1174 (bfd_size_type) fill_size);
1177 bfd_perror (stdoutput->filename);
1178 as_perror (_("FATAL: Can't write %s"),
1179 stdoutput->filename);
1180 exit (EXIT_FAILURE);
1182 offset += fill_size;
1187 /* Build a buffer full of fill objects and output it as
1188 often as necessary. This saves on the overhead of
1189 potentially lots of bfd_set_section_contents calls. */
1193 n_per_buf = sizeof (buf);
1194 memset (buf, *fill_literal, n_per_buf);
1199 n_per_buf = sizeof (buf) / fill_size;
1200 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1201 memcpy (bufp, fill_literal, fill_size);
1203 for (; count > 0; count -= n_per_buf)
1205 n_per_buf = n_per_buf > count ? count : n_per_buf;
1206 x = bfd_set_section_contents
1207 (stdoutput, sec, buf, (file_ptr) offset,
1208 (bfd_size_type) n_per_buf * fill_size);
1210 as_fatal (_("cannot write to output file"));
1211 offset += n_per_buf * fill_size;
1219 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1221 merge_data_into_text ()
1223 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1224 seg_info (text_section)->frchainP->frch_last->fr_next =
1225 seg_info (data_section)->frchainP->frch_root;
1226 seg_info (text_section)->frchainP->frch_last =
1227 seg_info (data_section)->frchainP->frch_last;
1228 seg_info (data_section)->frchainP = 0;
1232 text_last_frag->fr_next = data_frag_root;
1233 text_last_frag = data_last_frag;
1234 data_last_frag = NULL;
1235 data_frag_root = NULL;
1238 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1239 tmp->fx_next = data_fix_root;
1240 text_fix_tail = data_fix_tail;
1243 text_fix_root = data_fix_root;
1244 data_fix_root = NULL;
1247 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1249 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1251 relax_and_size_all_segments ()
1255 relax_segment (text_frag_root, SEG_TEXT);
1256 relax_segment (data_frag_root, SEG_DATA);
1257 relax_segment (bss_frag_root, SEG_BSS);
1259 /* Now the addresses of frags are correct within the segment. */
1260 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1261 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1262 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1264 /* Join the 2 segments into 1 huge segment.
1265 To do this, re-compute every rn_address in the SEG_DATA frags.
1266 Then join the data frags after the text frags.
1268 Determine a_data [length of data segment]. */
1271 register relax_addressT slide;
1273 know ((text_last_frag->fr_type == rs_fill)
1274 && (text_last_frag->fr_offset == 0));
1276 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1277 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1278 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1280 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1281 /* For b.out: If the data section has a strict alignment
1282 requirement, its load address in the .o file will be
1283 rounded up from the size of the text section. These
1284 two values are *not* the same! Similarly for the bss
1286 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1289 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1290 fragP->fr_address += slide;
1292 know (text_last_frag != 0);
1293 text_last_frag->fr_next = data_frag_root;
1297 H_SET_DATA_SIZE (&headers, 0);
1301 /* See above comments on b.out data section address. */
1304 if (data_last_frag == 0)
1305 bss_vma = H_GET_TEXT_SIZE (&headers);
1307 bss_vma = data_last_frag->fr_address;
1308 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1309 bss_address_frag.fr_address = bss_vma;
1311 #else /* ! OBJ_BOUT */
1312 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1313 H_GET_DATA_SIZE (&headers));
1315 #endif /* ! OBJ_BOUT */
1317 /* Slide all the frags. */
1320 relax_addressT slide = bss_address_frag.fr_address;
1322 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1323 fragP->fr_address += slide;
1327 H_SET_BSS_SIZE (&headers,
1328 bss_last_frag->fr_address - bss_frag_root->fr_address);
1330 H_SET_BSS_SIZE (&headers, 0);
1332 #endif /* ! BFD_ASSEMBLER && ! BFD */
1334 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1336 #ifdef BFD_ASSEMBLER
1344 extern PTR bfd_alloc PARAMS ((bfd *, bfd_size_type));
1346 /* Count symbols. We can't rely on a count made by the loop in
1347 write_object_file, because *_frob_file may add a new symbol or
1350 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1356 bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1358 asympp = (asymbol **) bfd_alloc (stdoutput, amt);
1359 symp = symbol_rootP;
1360 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1362 asympp[i] = symbol_get_bfdsym (symp);
1363 symbol_mark_written (symp);
1368 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1370 symbol_table_frozen = 1;
1374 /* Finish the subsegments. After every sub-segment, we fake an
1375 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1376 ".fill 0" because that is the kind of frag that requires least
1377 thought. ".align" frags like to have a following frag since that
1378 makes calculating their intended length trivial. */
1380 #ifndef SUB_SEGMENT_ALIGN
1382 /* The last subsegment gets an aligment corresponding to the alignment
1383 of the section. This allows proper nop-filling at the end of
1384 code-bearing sections. */
1385 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
1386 (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG) \
1387 ? get_recorded_alignment (SEG) : 0)
1389 #ifdef BFD_ASSEMBLER
1390 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1392 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 2
1400 struct frchain *frchainP;
1402 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1406 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1408 /* This now gets called even if we had errors. In that case,
1409 any alignment is meaningless, and, moreover, will look weird
1410 if we are generating a listing. */
1413 alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1414 #ifdef BFD_ASSEMBLER
1415 if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1416 && now_seg->entsize)
1418 unsigned int entsize = now_seg->entsize;
1421 while ((entsize & 1) == 0)
1426 if (entalign > alignment)
1427 alignment = entalign;
1432 if (subseg_text_p (now_seg))
1433 frag_align_code (alignment, 0);
1435 frag_align (alignment, 0, 0);
1437 /* frag_align will have left a new frag.
1438 Use this last frag for an empty ".fill".
1440 For this segment ...
1441 Create a last frag. Do not leave a "being filled in frag". */
1442 frag_wane (frag_now);
1443 frag_now->fr_fix = 0;
1444 know (frag_now->fr_next == NULL);
1448 /* Write the object file. */
1451 write_object_file ()
1453 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1454 fragS *fragP; /* Track along all frags. */
1457 /* Do we really want to write it? */
1459 int n_warns, n_errs;
1460 n_warns = had_warnings ();
1461 n_errs = had_errors ();
1462 /* The -Z flag indicates that an object file should be generated,
1463 regardless of warnings and errors. */
1464 if (flag_always_generate_output)
1466 if (n_warns || n_errs)
1467 as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1468 n_errs, n_errs == 1 ? "" : "s",
1469 n_warns, n_warns == 1 ? "" : "s");
1474 as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1475 n_errs, n_errs == 1 ? "" : "s",
1476 n_warns, n_warns == 1 ? "" : "s");
1481 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1482 a routine to check for the definition of the procedure "_main",
1483 and if so -- fix it up so that it can be program entry point. */
1484 vms_check_for_main ();
1485 #endif /* OBJ_VMS */
1487 /* From now on, we don't care about sub-segments. Build one frag chain
1488 for each segment. Linked thru fr_next. */
1490 #ifdef BFD_ASSEMBLER
1491 /* Remove the sections created by gas for its own purposes. */
1496 seclist = &stdoutput->sections;
1499 if (*seclist == reg_section || *seclist == expr_section)
1501 bfd_section_list_remove (stdoutput, seclist);
1502 stdoutput->section_count--;
1505 seclist = &(*seclist)->next;
1508 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1511 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1513 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1514 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1515 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1518 /* We have two segments. If user gave -R flag, then we must put the
1519 data frags into the text segment. Do this before relaxing so
1520 we know to take advantage of -R and make shorter addresses. */
1521 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1522 if (flag_readonly_data_in_text)
1524 merge_data_into_text ();
1528 #ifdef BFD_ASSEMBLER
1533 #ifndef WORKING_DOT_WORD
1534 /* We need to reset the markers in the broken word list and
1535 associated frags between calls to relax_segment (via
1536 relax_seg). Since the broken word list is global, we do it
1537 once per round, rather than locally in relax_segment for each
1539 struct broken_word *brokp;
1541 for (brokp = broken_words;
1542 brokp != (struct broken_word *) NULL;
1543 brokp = brokp->next_broken_word)
1547 if (brokp->dispfrag != (fragS *) NULL
1548 && brokp->dispfrag->fr_type == rs_broken_word)
1549 brokp->dispfrag->fr_subtype = 0;
1554 bfd_map_over_sections (stdoutput, relax_seg, &changed);
1559 /* Note - Most ports will use the default value of
1560 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
1561 local symbols to be resolved, removing their frag information.
1562 Some ports however, will not have finished relaxing all of
1563 their frags and will still need the local symbol frag
1564 information. These ports can set
1565 TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
1566 finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1568 bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1570 relax_and_size_all_segments ();
1571 #endif /* BFD_ASSEMBLER */
1573 /* Relaxation has completed. Freeze all syms. */
1576 #ifndef BFD_ASSEMBLER
1577 /* Crawl the symbol chain.
1579 For each symbol whose value depends on a frag, take the address of
1580 that frag and subsume it into the value of the symbol.
1581 After this, there is just one way to lookup a symbol value.
1582 Values are left in their final state for object file emission.
1583 We adjust the values of 'L' local symbols, even if we do
1584 not intend to emit them to the object file, because their values
1585 are needed for fix-ups.
1587 Unless we saw a -L flag, remove all symbols that begin with 'L'
1588 from the symbol chain. (They are still pointed to by the fixes.)
1590 Count the remaining symbols.
1591 Assign a symbol number to each symbol.
1592 Count the number of string-table chars we will emit.
1593 Put this info into the headers as appropriate. */
1594 know (zero_address_frag.fr_address == 0);
1595 string_byte_count = sizeof (string_byte_count);
1597 obj_crawl_symbol_chain (&headers);
1599 if (string_byte_count == sizeof (string_byte_count))
1600 string_byte_count = 0;
1602 H_SET_STRING_SIZE (&headers, string_byte_count);
1604 /* Addresses of frags now reflect addresses we use in the object file.
1605 Symbol values are correct.
1606 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1607 Also converting any machine-dependent frags using md_convert_frag(); */
1608 subseg_change (SEG_TEXT, 0);
1610 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1612 /* At this point we have linked all the frags into a single
1613 chain. However, cvt_frag_to_fill may call md_convert_frag
1614 which may call fix_new. We need to ensure that fix_new adds
1615 the fixup to the right section. */
1616 if (fragP == data_frag_root)
1617 subseg_change (SEG_DATA, 0);
1619 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1621 /* Some assert macros don't work with # directives mixed in. */
1623 if (!(fragP->fr_next == NULL
1625 || fragP->fr_next == data_frag_root
1627 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
1628 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1632 #endif /* ! BFD_ASSEMBLER */
1634 #ifndef WORKING_DOT_WORD
1636 struct broken_word *lie;
1637 struct broken_word **prevP;
1639 prevP = &broken_words;
1640 for (lie = broken_words; lie; lie = lie->next_broken_word)
1645 subseg_change (lie->seg, lie->subseg);
1646 exp.X_op = O_subtract;
1647 exp.X_add_symbol = lie->add;
1648 exp.X_op_symbol = lie->sub;
1649 exp.X_add_number = lie->addnum;
1650 #ifdef BFD_ASSEMBLER
1651 #ifdef TC_CONS_FIX_NEW
1652 TC_CONS_FIX_NEW (lie->frag,
1653 lie->word_goes_here - lie->frag->fr_literal,
1656 fix_new_exp (lie->frag,
1657 lie->word_goes_here - lie->frag->fr_literal,
1658 2, &exp, 0, BFD_RELOC_16);
1661 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1662 fix_new_exp (lie->frag,
1663 lie->word_goes_here - lie->frag->fr_literal,
1664 2, &exp, 0, NO_RELOC);
1667 fix_new_ns32k_exp (lie->frag,
1668 lie->word_goes_here - lie->frag->fr_literal,
1669 2, &exp, 0, 0, 2, 0, 0);
1671 fix_new_exp (lie->frag,
1672 lie->word_goes_here - lie->frag->fr_literal,
1674 #endif /* TC_NS32K */
1675 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1676 #endif /* BFD_ASSEMBLER */
1677 *prevP = lie->next_broken_word;
1680 prevP = &(lie->next_broken_word);
1682 for (lie = broken_words; lie;)
1684 struct broken_word *untruth;
1686 addressT table_addr;
1687 addressT from_addr, to_addr;
1690 subseg_change (lie->seg, lie->subseg);
1691 fragP = lie->dispfrag;
1693 /* Find out how many broken_words go here. */
1696 untruth && untruth->dispfrag == fragP;
1697 untruth = untruth->next_broken_word)
1698 if (untruth->added == 1)
1701 table_ptr = lie->dispfrag->fr_opcode;
1702 table_addr = (lie->dispfrag->fr_address
1703 + (table_ptr - lie->dispfrag->fr_literal));
1704 /* Create the jump around the long jumps. This is a short
1705 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1706 from_addr = table_addr;
1707 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1708 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1710 table_ptr += md_short_jump_size;
1711 table_addr += md_short_jump_size;
1714 lie && lie->dispfrag == fragP;
1715 m++, lie = lie->next_broken_word)
1717 if (lie->added == 2)
1719 /* Patch the jump table. */
1720 /* This is the offset from ??? to table_ptr+0. */
1721 to_addr = table_addr - S_GET_VALUE (lie->sub);
1722 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1723 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1725 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1726 for (untruth = lie->next_broken_word;
1727 untruth && untruth->dispfrag == fragP;
1728 untruth = untruth->next_broken_word)
1730 if (untruth->use_jump == lie)
1731 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1734 /* Install the long jump. */
1735 /* This is a long jump from table_ptr+0 to the final target. */
1736 from_addr = table_addr;
1737 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1738 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1740 table_ptr += md_long_jump_size;
1741 table_addr += md_long_jump_size;
1745 #endif /* not WORKING_DOT_WORD */
1747 #ifndef BFD_ASSEMBLER
1750 char *the_object_file;
1751 long object_file_size;
1752 /* Scan every FixS performing fixups. We had to wait until now to
1753 do this because md_convert_frag() may have made some fixSs. */
1756 subseg_change (SEG_TEXT, 0);
1757 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1758 subseg_change (SEG_DATA, 0);
1759 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1760 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1762 /* FIXME: Move this stuff into the pre-write-hook. */
1763 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1764 H_SET_ENTRY_POINT (&headers, 0);
1766 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1768 object_file_size = H_GET_FILE_SIZE (&headers);
1769 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1771 output_file_create (out_file_name);
1773 obj_header_append (&next_object_file_charP, &headers);
1775 know ((next_object_file_charP - the_object_file)
1776 == H_GET_HEADER_SIZE (&headers));
1779 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1781 register long count;
1782 register char *fill_literal;
1783 register long fill_size;
1786 know (fragP->fr_type == rs_fill);
1787 append (&next_object_file_charP, fragP->fr_literal,
1788 (unsigned long) fragP->fr_fix);
1789 fill_literal = fragP->fr_literal + fragP->fr_fix;
1790 fill_size = fragP->fr_var;
1791 know (fragP->fr_offset >= 0);
1793 for (count = fragP->fr_offset; count; count--)
1794 append (&next_object_file_charP, fill_literal,
1795 (unsigned long) fill_size);
1798 know ((next_object_file_charP - the_object_file)
1799 == (H_GET_HEADER_SIZE (&headers)
1800 + H_GET_TEXT_SIZE (&headers)
1801 + H_GET_DATA_SIZE (&headers)));
1803 /* Emit relocations. */
1804 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1805 (relax_addressT) 0);
1806 know ((next_object_file_charP - the_object_file)
1807 == (H_GET_HEADER_SIZE (&headers)
1808 + H_GET_TEXT_SIZE (&headers)
1809 + H_GET_DATA_SIZE (&headers)
1810 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1812 /* Make addresses in data relocation directives relative to beginning of
1813 first data fragment, not end of last text fragment: alignment of the
1814 start of the data segment may place a gap between the segments. */
1815 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1816 data0_frchainP->frch_root->fr_address);
1818 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1819 text_last_frag->fr_address);
1820 #endif /* TC_I960 */
1822 know ((next_object_file_charP - the_object_file)
1823 == (H_GET_HEADER_SIZE (&headers)
1824 + H_GET_TEXT_SIZE (&headers)
1825 + H_GET_DATA_SIZE (&headers)
1826 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1827 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1829 /* Emit line number entries. */
1830 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1831 know ((next_object_file_charP - the_object_file)
1832 == (H_GET_HEADER_SIZE (&headers)
1833 + H_GET_TEXT_SIZE (&headers)
1834 + H_GET_DATA_SIZE (&headers)
1835 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1836 + H_GET_DATA_RELOCATION_SIZE (&headers)
1837 + H_GET_LINENO_SIZE (&headers)));
1840 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1841 know ((next_object_file_charP - the_object_file)
1842 == (H_GET_HEADER_SIZE (&headers)
1843 + H_GET_TEXT_SIZE (&headers)
1844 + H_GET_DATA_SIZE (&headers)
1845 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1846 + H_GET_DATA_RELOCATION_SIZE (&headers)
1847 + H_GET_LINENO_SIZE (&headers)
1848 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1851 if (string_byte_count > 0)
1852 obj_emit_strings (&next_object_file_charP);
1855 bfd_seek (stdoutput, (file_ptr) 0, 0);
1856 bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
1859 /* Write the data to the file. */
1860 output_file_append (the_object_file, object_file_size, out_file_name);
1861 free (the_object_file);
1865 /* Now do the VMS-dependent part of writing the object file. */
1866 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1867 H_GET_DATA_SIZE (&headers),
1868 H_GET_BSS_SIZE (&headers),
1869 text_frag_root, data_frag_root);
1870 #endif /* OBJ_VMS */
1871 #else /* BFD_ASSEMBLER */
1873 /* Resolve symbol values. This needs to be done before processing
1879 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1880 resolve_symbol_value (symp);
1882 resolve_local_symbol_values ();
1886 #ifdef tc_frob_file_before_adjust
1887 tc_frob_file_before_adjust ();
1889 #ifdef obj_frob_file_before_adjust
1890 obj_frob_file_before_adjust ();
1893 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1895 #ifdef tc_frob_file_before_fix
1896 tc_frob_file_before_fix ();
1898 #ifdef obj_frob_file_before_fix
1899 obj_frob_file_before_fix ();
1902 bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1904 /* Set up symbol table, and write it out. */
1909 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1914 if (symbol_mri_common_p (symp))
1916 if (S_IS_EXTERNAL (symp))
1917 as_bad (_("%s: global symbols not supported in common sections"),
1919 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1923 name = S_GET_NAME (symp);
1927 decode_local_label_name ((char *) S_GET_NAME (symp));
1928 /* They only differ if `name' is a fb or dollar local
1930 if (name2 != name && ! S_IS_DEFINED (symp))
1931 as_bad (_("local label `%s' is not defined"), name2);
1934 /* Do it again, because adjust_reloc_syms might introduce
1935 more symbols. They'll probably only be section symbols,
1936 but they'll still need to have the values computed. */
1937 resolve_symbol_value (symp);
1939 /* Skip symbols which were equated to undefined or common
1941 if (symbol_equated_reloc_p (symp))
1943 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1947 /* So far, common symbols have been treated like undefined symbols.
1948 Put them in the common section now. */
1949 if (S_IS_DEFINED (symp) == 0
1950 && S_GET_VALUE (symp) != 0)
1951 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1953 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1954 S_GET_NAME (symp), symp,
1956 symbol_get_bfdsym (symp)->flags,
1957 segment_name (S_GET_SEGMENT (symp)));
1960 #ifdef obj_frob_symbol
1961 obj_frob_symbol (symp, punt);
1963 #ifdef tc_frob_symbol
1964 if (! punt || symbol_used_in_reloc_p (symp))
1965 tc_frob_symbol (symp, punt);
1968 /* If we don't want to keep this symbol, splice it out of
1969 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1970 want section symbols. Otherwise, we skip local symbols
1971 and symbols that the frob_symbol macros told us to punt,
1972 but we keep such symbols if they are used in relocs. */
1973 if (symp == abs_section_sym
1974 || (! EMIT_SECTION_SYMBOLS
1975 && symbol_section_p (symp))
1976 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1977 opposites. Sometimes the former checks flags and the
1978 latter examines the name... */
1979 || (!S_IS_EXTERN (symp)
1980 && (punt || S_IS_LOCAL (symp))
1981 && ! symbol_used_in_reloc_p (symp)))
1983 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1985 /* After symbol_remove, symbol_next(symp) still returns
1986 the one that came after it in the chain. So we don't
1987 need to do any extra cleanup work here. */
1991 /* Make sure we really got a value for the symbol. */
1992 if (! symbol_resolved_p (symp))
1994 as_bad (_("can't resolve value for symbol `%s'"),
1996 symbol_mark_resolved (symp);
1999 /* Set the value into the BFD symbol. Up til now the value
2000 has only been kept in the gas symbolS struct. */
2001 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
2007 /* Now do any format-specific adjustments to the symbol table, such
2008 as adding file symbols. */
2009 #ifdef tc_adjust_symtab
2010 tc_adjust_symtab ();
2012 #ifdef obj_adjust_symtab
2013 obj_adjust_symtab ();
2016 /* Now that all the sizes are known, and contents correct, we can
2017 start writing to the file. */
2020 /* If *_frob_file changes the symbol value at this point, it is
2021 responsible for moving the changed value into symp->bsym->value
2022 as well. Hopefully all symbol value changing can be done in
2027 #ifdef obj_frob_file
2031 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
2033 #ifdef tc_frob_file_after_relocs
2034 tc_frob_file_after_relocs ();
2036 #ifdef obj_frob_file_after_relocs
2037 obj_frob_file_after_relocs ();
2040 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
2041 #endif /* BFD_ASSEMBLER */
2045 #ifdef TC_GENERIC_RELAX_TABLE
2047 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
2050 relax_frag (segment, fragP, stretch)
2055 const relax_typeS *this_type;
2056 const relax_typeS *start_type;
2057 relax_substateT next_state;
2058 relax_substateT this_state;
2064 const relax_typeS *table;
2066 target = fragP->fr_offset;
2067 address = fragP->fr_address;
2068 table = TC_GENERIC_RELAX_TABLE;
2069 this_state = fragP->fr_subtype;
2070 start_type = this_type = table + this_state;
2071 symbolP = fragP->fr_symbol;
2077 sym_frag = symbol_get_frag (symbolP);
2079 #ifndef DIFF_EXPR_OK
2080 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2081 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2082 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2083 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2084 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2086 know (sym_frag != NULL);
2088 know (S_GET_SEGMENT (symbolP) != absolute_section
2089 || sym_frag == &zero_address_frag);
2090 target += S_GET_VALUE (symbolP);
2092 /* If frag has yet to be reached on this pass,
2093 assume it will move by STRETCH just as we did.
2094 If this is not so, it will be because some frag
2095 between grows, and that will force another pass. */
2098 && sym_frag->relax_marker != fragP->relax_marker
2099 && S_GET_SEGMENT (symbolP) == segment)
2105 aim = target - address - fragP->fr_fix;
2106 #ifdef TC_PCREL_ADJUST
2107 /* Currently only the ns32k family needs this. */
2108 aim += TC_PCREL_ADJUST (fragP);
2110 /* This machine doesn't want to use pcrel_adjust.
2111 In that case, pcrel_adjust should be zero. */
2113 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2116 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2117 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2122 /* Look backwards. */
2123 for (next_state = this_type->rlx_more; next_state;)
2124 if (aim >= this_type->rlx_backward)
2128 /* Grow to next state. */
2129 this_state = next_state;
2130 this_type = table + this_state;
2131 next_state = this_type->rlx_more;
2136 /* Look forwards. */
2137 for (next_state = this_type->rlx_more; next_state;)
2138 if (aim <= this_type->rlx_forward)
2142 /* Grow to next state. */
2143 this_state = next_state;
2144 this_type = table + this_state;
2145 next_state = this_type->rlx_more;
2149 growth = this_type->rlx_length - start_type->rlx_length;
2151 fragP->fr_subtype = this_state;
2155 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2157 /* Relax_align. Advance location counter to next address that has 'alignment'
2158 lowest order bits all 0s, return size of adjustment made. */
2159 static relax_addressT
2160 relax_align (address, alignment)
2161 register relax_addressT address; /* Address now. */
2162 register int alignment; /* Alignment (binary). */
2164 relax_addressT mask;
2165 relax_addressT new_address;
2167 mask = ~((~0) << alignment);
2168 new_address = (address + mask) & (~mask);
2169 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2171 /* We must provide lots of padding, so the linker can discard it
2172 when needed. The linker will not add extra space, ever. */
2173 new_address += (1 << alignment);
2175 return (new_address - address);
2178 /* Now we have a segment, not a crowd of sub-segments, we can make
2183 After this, all frags in this segment have addresses that are correct
2184 within the segment. Since segments live in different file addresses,
2185 these frag addresses may not be the same as final object-file
2189 relax_segment (segment_frag_root, segment)
2190 struct frag *segment_frag_root;
2193 register struct frag *fragP;
2194 register relax_addressT address;
2197 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2198 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2200 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2201 subseg_change (segment, 0);
2203 /* For each frag in segment: count and store (a 1st guess of)
2206 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2208 fragP->relax_marker = 0;
2209 fragP->fr_address = address;
2210 address += fragP->fr_fix;
2212 switch (fragP->fr_type)
2215 address += fragP->fr_offset * fragP->fr_var;
2222 addressT offset = relax_align (address, (int) fragP->fr_offset);
2224 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2227 if (offset % fragP->fr_var != 0)
2229 as_bad_where (fragP->fr_file, fragP->fr_line,
2230 _("alignment padding (%lu bytes) not a multiple of %ld"),
2231 (unsigned long) offset, (long) fragP->fr_var);
2232 offset -= (offset % fragP->fr_var);
2241 /* Assume .org is nugatory. It will grow with 1st relax. */
2244 case rs_machine_dependent:
2245 /* If fr_symbol is an expression, this call to
2246 resolve_symbol_value sets up the correct segment, which will
2247 likely be needed in md_estimate_size_before_relax. */
2248 if (fragP->fr_symbol)
2249 resolve_symbol_value (fragP->fr_symbol);
2251 address += md_estimate_size_before_relax (fragP, segment);
2254 #ifndef WORKING_DOT_WORD
2255 /* Broken words don't concern us yet. */
2256 case rs_broken_word:
2261 /* Initial guess is always 1; doing otherwise can result in
2262 stable solutions that are larger than the minimum. */
2263 address += fragP->fr_offset = 1;
2267 address += eh_frame_estimate_size_before_relax (fragP);
2271 address += dwarf2dbg_estimate_size_before_relax (fragP);
2275 BAD_CASE (fragP->fr_type);
2282 long stretch; /* May be any size, 0 or negative. */
2283 /* Cumulative number of addresses we have relaxed this pass.
2284 We may have relaxed more than one address. */
2285 int stretched; /* Have we stretched on this pass? */
2286 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2287 grew, and another shrank. If a branch instruction doesn't fit anymore,
2288 we could be scrod. */
2295 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2298 addressT was_address;
2302 fragP->relax_marker ^= 1;
2303 was_address = fragP->fr_address;
2304 address = fragP->fr_address += stretch;
2305 symbolP = fragP->fr_symbol;
2306 offset = fragP->fr_offset;
2308 switch (fragP->fr_type)
2310 case rs_fill: /* .fill never relaxes. */
2314 #ifndef WORKING_DOT_WORD
2315 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2316 for it I do not want to write it. I do not want to have
2317 anything to do with it. This is not the proper way to
2318 implement this misfeature. */
2319 case rs_broken_word:
2321 struct broken_word *lie;
2322 struct broken_word *untruth;
2324 /* Yes this is ugly (storing the broken_word pointer
2325 in the symbol slot). Still, this whole chunk of
2326 code is ugly, and I don't feel like doing anything
2327 about it. Think of it as stubbornness in action. */
2329 for (lie = (struct broken_word *) (fragP->fr_symbol);
2330 lie && lie->dispfrag == fragP;
2331 lie = lie->next_broken_word)
2337 offset = (S_GET_VALUE (lie->add)
2339 - S_GET_VALUE (lie->sub));
2340 if (offset <= -32768 || offset >= 32767)
2342 if (flag_warn_displacement)
2345 sprint_value (buf, (addressT) lie->addnum);
2346 as_warn_where (fragP->fr_file, fragP->fr_line,
2347 _(".word %s-%s+%s didn't fit"),
2348 S_GET_NAME (lie->add),
2349 S_GET_NAME (lie->sub),
2353 if (fragP->fr_subtype == 0)
2355 fragP->fr_subtype++;
2356 growth += md_short_jump_size;
2358 for (untruth = lie->next_broken_word;
2359 untruth && untruth->dispfrag == lie->dispfrag;
2360 untruth = untruth->next_broken_word)
2361 if ((symbol_get_frag (untruth->add)
2362 == symbol_get_frag (lie->add))
2363 && (S_GET_VALUE (untruth->add)
2364 == S_GET_VALUE (lie->add)))
2367 untruth->use_jump = lie;
2369 growth += md_long_jump_size;
2374 } /* case rs_broken_word */
2380 addressT oldoff, newoff;
2382 oldoff = relax_align (was_address + fragP->fr_fix,
2384 newoff = relax_align (address + fragP->fr_fix,
2387 if (fragP->fr_subtype != 0)
2389 if (oldoff > fragP->fr_subtype)
2391 if (newoff > fragP->fr_subtype)
2395 growth = newoff - oldoff;
2401 addressT target = offset;
2406 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2407 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2408 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2409 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2410 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2411 know (symbolP->sy_frag);
2412 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2413 || (symbolP->sy_frag == &zero_address_frag));
2415 /* Convert from an actual address to an octet offset
2416 into the section. Here it is assumed that the
2417 section's VMA is zero, and can omit subtracting it
2418 from the symbol's value to get the address offset. */
2419 know (S_GET_SECTION (symbolP)->vma == 0);
2420 target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2423 know (fragP->fr_next);
2424 after = fragP->fr_next->fr_address;
2425 growth = target - after;
2428 /* Growth may be negative, but variable part of frag
2429 cannot have fewer than 0 chars. That is, we can't
2431 as_bad_where (fragP->fr_file, fragP->fr_line,
2432 _("attempt to .org backwards"));
2434 /* We've issued an error message. Change the
2435 frag to avoid cascading errors. */
2436 fragP->fr_type = rs_align;
2437 fragP->fr_subtype = 0;
2438 fragP->fr_offset = 0;
2439 fragP->fr_fix = after - address;
2443 /* This is an absolute growth factor */
2454 amount = S_GET_VALUE (symbolP);
2455 if (S_GET_SEGMENT (symbolP) != absolute_section
2456 || S_IS_COMMON (symbolP)
2457 || ! S_IS_DEFINED (symbolP))
2459 as_bad_where (fragP->fr_file, fragP->fr_line,
2460 _(".space specifies non-absolute value"));
2461 /* Prevent repeat of this error message. */
2462 fragP->fr_symbol = 0;
2464 else if (amount < 0)
2466 as_warn_where (fragP->fr_file, fragP->fr_line,
2467 _(".space or .fill with negative value, ignored"));
2468 fragP->fr_symbol = 0;
2471 growth = (was_address + fragP->fr_fix + amount
2472 - fragP->fr_next->fr_address);
2476 case rs_machine_dependent:
2477 #ifdef md_relax_frag
2478 growth = md_relax_frag (segment, fragP, stretch);
2480 #ifdef TC_GENERIC_RELAX_TABLE
2481 /* The default way to relax a frag is to look through
2482 TC_GENERIC_RELAX_TABLE. */
2483 growth = relax_frag (segment, fragP, stretch);
2484 #endif /* TC_GENERIC_RELAX_TABLE */
2493 value = resolve_symbol_value (fragP->fr_symbol);
2494 size = sizeof_leb128 (value, fragP->fr_subtype);
2495 growth = size - fragP->fr_offset;
2496 fragP->fr_offset = size;
2501 growth = eh_frame_relax_frag (fragP);
2505 growth = dwarf2dbg_relax_frag (fragP);
2509 BAD_CASE (fragP->fr_type);
2517 } /* For each frag in the segment. */
2519 while (stretched); /* Until nothing further to relax. */
2523 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2524 if (fragP->last_fr_address != fragP->fr_address)
2526 fragP->last_fr_address = fragP->fr_address;
2532 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2536 Go through all the fixS's in a segment and see which ones can be
2537 handled now. (These consist of fixS where we have since discovered
2538 the value of a symbol, or the address of the frag involved.)
2539 For each one, call md_apply_fix3 to put the fix into the frag data.
2541 Result is a count of how many relocation structs will be needed to
2542 handle the remaining fixS's that we couldn't completely handle here.
2543 These will be output later by emit_relocations(). */
2546 fixup_segment (fixP, this_segment)
2550 long seg_reloc_count = 0;
2553 segT add_symbol_segment = absolute_section;
2555 if (fixP != NULL && abs_section_sym == NULL)
2557 #ifndef BFD_ASSEMBLER
2558 abs_section_sym = &abs_symbol;
2560 abs_section_sym = section_symbol (absolute_section);
2564 /* If the linker is doing the relaxing, we must not do any fixups.
2566 Well, strictly speaking that's not true -- we could do any that
2567 are PC-relative and don't cross regions that could change size.
2568 And for the i960 we might be able to turn callx/callj into bal
2569 anyways in cases where we know the maximum displacement. */
2570 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
2572 for (; fixP; fixP = fixP->fx_next)
2575 if (fixP->fx_addsy == NULL)
2577 /* There was no symbol required by this relocation.
2578 However, BFD doesn't really handle relocations
2579 without symbols well. So fake up a local symbol in
2580 the absolute section. */
2581 fixP->fx_addsy = abs_section_sym;
2583 symbol_mark_used_in_reloc (fixP->fx_addsy);
2584 if (fixP->fx_subsy != NULL)
2585 symbol_mark_used_in_reloc (fixP->fx_subsy);
2588 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2589 return seg_reloc_count;
2592 for (; fixP; fixP = fixP->fx_next)
2595 fprintf (stderr, "\nprocessing fixup:\n");
2599 fragP = fixP->fx_frag;
2601 #ifdef TC_VALIDATE_FIX
2602 TC_VALIDATE_FIX (fixP, this_segment, skip);
2604 add_number = fixP->fx_offset;
2606 if (fixP->fx_addsy != NULL
2607 && symbol_mri_common_p (fixP->fx_addsy))
2609 know (fixP->fx_addsy->sy_value.X_op == O_symbol);
2610 add_number += S_GET_VALUE (fixP->fx_addsy);
2611 fixP->fx_offset = add_number;
2613 = symbol_get_value_expression (fixP->fx_addsy)->X_add_symbol;
2616 if (fixP->fx_addsy != NULL)
2617 add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
2619 if (fixP->fx_subsy != NULL)
2621 segT sub_symbol_segment;
2622 resolve_symbol_value (fixP->fx_subsy);
2623 sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
2624 if (fixP->fx_addsy != NULL
2625 && sub_symbol_segment == add_symbol_segment
2626 && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
2628 add_number += S_GET_VALUE (fixP->fx_addsy);
2629 add_number -= S_GET_VALUE (fixP->fx_subsy);
2630 fixP->fx_offset = add_number;
2631 /* If the back-end code has selected a pc-relative
2632 reloc, adjust the value to be pc-relative. */
2635 /* See the comment below about 68k weirdness. */
2639 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2640 fixP->fx_addsy = NULL;
2641 fixP->fx_subsy = NULL;
2644 else if (sub_symbol_segment == absolute_section
2645 && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
2647 add_number -= S_GET_VALUE (fixP->fx_subsy);
2648 fixP->fx_offset = add_number;
2649 fixP->fx_subsy = NULL;
2651 else if (sub_symbol_segment == this_segment
2652 && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
2654 add_number -= S_GET_VALUE (fixP->fx_subsy);
2655 fixP->fx_offset = (add_number + fixP->fx_dot_value
2656 + fixP->fx_frag->fr_address);
2658 /* Make it pc-relative. If the back-end code has not
2659 selected a pc-relative reloc, cancel the adjustment
2660 we do later on all pc-relative relocs. */
2663 /* Do this for m68k even if it's already described
2664 as pc-relative. On the m68k, an operand of
2665 "pc@(foo-.-2)" should address "foo" in a
2666 pc-relative mode. */
2670 add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
2671 fixP->fx_subsy = NULL;
2674 else if (!TC_VALIDATE_FIX_SUB (fixP))
2676 as_bad_where (fixP->fx_file, fixP->fx_line,
2677 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2678 fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
2679 segment_name (add_symbol_segment),
2680 S_GET_NAME (fixP->fx_subsy),
2681 segment_name (sub_symbol_segment));
2687 if (add_symbol_segment == this_segment
2688 && !TC_FORCE_RELOCATION_LOCAL (fixP))
2690 /* This fixup was made when the symbol's segment was
2691 SEG_UNKNOWN, but it is now in the local segment.
2692 So we know how to do the address without relocation. */
2693 add_number += S_GET_VALUE (fixP->fx_addsy);
2694 fixP->fx_offset = add_number;
2696 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2697 fixP->fx_addsy = NULL;
2700 else if (add_symbol_segment == absolute_section
2701 && !TC_FORCE_RELOCATION_ABS (fixP))
2703 add_number += S_GET_VALUE (fixP->fx_addsy);
2704 fixP->fx_offset = add_number;
2705 fixP->fx_addsy = NULL;
2707 else if (add_symbol_segment != undefined_section
2708 #ifdef BFD_ASSEMBLER
2709 && ! bfd_is_com_section (add_symbol_segment)
2711 && MD_APPLY_SYM_VALUE (fixP))
2712 add_number += S_GET_VALUE (fixP->fx_addsy);
2717 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2718 if (!fixP->fx_done && fixP->fx_addsy == NULL)
2720 /* There was no symbol required by this relocation.
2721 However, BFD doesn't really handle relocations
2722 without symbols well. So fake up a local symbol in
2723 the absolute section. */
2724 fixP->fx_addsy = abs_section_sym;
2729 md_apply_fix3 (fixP, &add_number, this_segment);
2734 if (fixP->fx_addsy == NULL)
2735 fixP->fx_addsy = abs_section_sym;
2736 symbol_mark_used_in_reloc (fixP->fx_addsy);
2737 if (fixP->fx_subsy != NULL)
2738 symbol_mark_used_in_reloc (fixP->fx_subsy);
2741 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
2743 if (fixP->fx_size < sizeof (valueT))
2748 mask--; /* Set all bits to one. */
2749 mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
2750 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2752 char buf[50], buf2[50];
2753 sprint_value (buf, fragP->fr_address + fixP->fx_where);
2754 if (add_number > 1000)
2755 sprint_value (buf2, add_number);
2757 sprintf (buf2, "%ld", (long) add_number);
2758 as_bad_where (fixP->fx_file, fixP->fx_line,
2759 _("value of %s too large for field of %d bytes at %s"),
2760 buf2, fixP->fx_size, buf);
2761 } /* Generic error checking. */
2763 #ifdef WARN_SIGNED_OVERFLOW_WORD
2764 /* Warn if a .word value is too large when treated as a signed
2765 number. We already know it is not too negative. This is to
2766 catch over-large switches generated by gcc on the 68k. */
2767 if (!flag_signed_overflow_ok
2768 && fixP->fx_size == 2
2769 && add_number > 0x7fff)
2770 as_bad_where (fixP->fx_file, fixP->fx_line,
2771 _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2773 (long) (fragP->fr_address + fixP->fx_where));
2775 } /* Not a bit fix. */
2777 #ifdef TC_VALIDATE_FIX
2778 skip: ATTRIBUTE_UNUSED_LABEL
2782 fprintf (stderr, "result:\n");
2785 } /* For each fixS in this segment. */
2787 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2788 return seg_reloc_count;
2791 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2794 number_to_chars_bigendian (buf, val, n)
2803 buf[n] = val & 0xff;
2809 number_to_chars_littleendian (buf, val, n)
2818 *buf++ = val & 0xff;
2824 write_print_statistics (file)
2827 fprintf (file, "fixups: %d\n", n_fixups);
2830 /* For debugging. */
2831 extern int indent_level;
2838 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2840 fprintf (stderr, " pcrel");
2841 if (fixp->fx_pcrel_adjust)
2842 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2843 if (fixp->fx_im_disp)
2846 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2848 fprintf (stderr, " im_disp");
2852 fprintf (stderr, " tcbit");
2854 fprintf (stderr, " done");
2855 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2856 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2857 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2858 #ifdef BFD_ASSEMBLER
2859 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2862 #ifdef NEED_FX_R_TYPE
2863 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2868 fprintf (stderr, "\n +<");
2869 print_symbol_value_1 (stderr, fixp->fx_addsy);
2870 fprintf (stderr, ">");
2874 fprintf (stderr, "\n -<");
2875 print_symbol_value_1 (stderr, fixp->fx_subsy);
2876 fprintf (stderr, ">");
2878 fprintf (stderr, "\n");
2879 #ifdef TC_FIX_DATA_PRINT
2880 TC_FIX_DATA_PRINT (stderr, fixp);