1 /* write.c - emit .o file
2 Copyright (C) 1986, 87, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This thing should be set up to do byteordering correctly. But... */
27 #include "output-file.h"
29 /* This looks like a good idea. Let's try turning it on always, for now. */
30 #undef BFD_FAST_SECTION_FILL
31 #define BFD_FAST_SECTION_FILL
33 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
34 instruction so that the disassembler does not choke on it. */
36 #define NOP_OPCODE 0x00
39 #ifndef TC_ADJUST_RELOC_COUNT
40 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
43 #ifndef TC_FORCE_RELOCATION
44 #define TC_FORCE_RELOCATION(FIXP) 0
47 #ifndef TC_FORCE_RELOCATION_SECTION
48 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
51 #ifndef TC_LINKRELAX_FIXUP
52 #define TC_LINKRELAX_FIXUP(SEG) 1
55 #ifndef TC_FIX_ADJUSTABLE
56 #define TC_FIX_ADJUSTABLE(fix) 1
59 #ifndef MD_PCREL_FROM_SECTION
60 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
63 #ifndef WORKING_DOT_WORD
64 extern CONST int md_short_jump_size;
65 extern CONST int md_long_jump_size;
68 int symbol_table_frozen;
69 void print_fixup PARAMS ((fixS *));
72 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
74 /* We generally attach relocs to frag chains. However, after we have
75 chained these all together into a segment, any relocs we add after
76 that must be attached to a segment. This will include relocs added
77 in md_estimate_size_for_relax, for example. */
78 static int frags_chained = 0;
84 struct frag *text_frag_root;
85 struct frag *data_frag_root;
86 struct frag *bss_frag_root;
88 struct frag *text_last_frag; /* Last frag in segment. */
89 struct frag *data_last_frag; /* Last frag in segment. */
90 static struct frag *bss_last_frag; /* Last frag in segment. */
94 static object_headers headers;
97 long string_byte_count;
98 char *next_object_file_charP; /* Tracks object file bytes. */
101 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
104 #endif /* BFD_ASSEMBLER */
109 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
110 symbolS *add, symbolS *sub,
111 offsetT offset, int pcrel,
112 bfd_reloc_code_real_type r_type));
114 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
115 symbolS *add, symbolS *sub,
116 offsetT offset, int pcrel,
119 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
120 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
122 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
123 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
124 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
127 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
128 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
129 static void relax_and_size_seg PARAMS ((bfd *, asection *, PTR));
130 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
131 static void write_relocs PARAMS ((bfd *, asection *, PTR));
132 static void write_contents PARAMS ((bfd *, asection *, PTR));
133 static void set_symtab PARAMS ((void));
135 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
136 static void merge_data_into_text PARAMS ((void));
138 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
139 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
140 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
141 static void relax_and_size_all_segments PARAMS ((void));
143 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
144 static void set_segment_vma PARAMS ((bfd *, asection *, PTR));
147 /* Create a fixS in obstack 'notes'. */
150 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
152 fragS *frag; /* Which frag? */
153 int where; /* Where in that frag? */
154 int size; /* 1, 2, or 4 usually. */
155 symbolS *add_symbol; /* X_add_symbol. */
156 symbolS *sub_symbol; /* X_op_symbol. */
157 offsetT offset; /* X_add_number. */
158 int pcrel; /* TRUE if PC-relative relocation. */
160 bfd_reloc_code_real_type r_type; /* Relocation type. */
162 int r_type; /* Relocation type. */
169 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
171 fixP->fx_frag = frag;
172 fixP->fx_where = where;
173 fixP->fx_size = size;
174 /* We've made fx_size a narrow field; check that it's wide enough. */
175 if (fixP->fx_size != size)
177 as_bad (_("field fx_size too small to hold %d"), size);
180 fixP->fx_addsy = add_symbol;
181 fixP->fx_subsy = sub_symbol;
182 fixP->fx_offset = offset;
183 fixP->fx_pcrel = pcrel;
185 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
186 fixP->fx_r_type = r_type;
188 fixP->fx_im_disp = 0;
189 fixP->fx_pcrel_adjust = 0;
190 fixP->fx_bit_fixP = 0;
191 fixP->fx_addnumber = 0;
194 fixP->fx_no_overflow = 0;
198 fixP->fx_cgen.insn = NULL;
199 fixP->fx_cgen.opinfo = 0;
203 TC_INIT_FIX_DATA (fixP);
206 as_where (&fixP->fx_file, &fixP->fx_line);
208 /* Usually, we want relocs sorted numerically, but while
209 comparing to older versions of gas that have relocs
210 reverse sorted, it is convenient to have this compile
211 time option. xoxorich. */
215 fixS **seg_fix_rootP = (frags_chained
216 ? &seg_info (now_seg)->fix_root
217 : &frchain_now->fix_root);
218 fixS **seg_fix_tailP = (frags_chained
219 ? &seg_info (now_seg)->fix_tail
220 : &frchain_now->fix_tail);
223 #ifdef REVERSE_SORT_RELOCS
225 fixP->fx_next = *seg_fix_rootP;
226 *seg_fix_rootP = fixP;
228 #else /* REVERSE_SORT_RELOCS */
230 fixP->fx_next = NULL;
233 (*seg_fix_tailP)->fx_next = fixP;
235 *seg_fix_rootP = fixP;
236 *seg_fix_tailP = fixP;
238 #endif /* REVERSE_SORT_RELOCS */
244 /* Create a fixup relative to a symbol (plus a constant). */
247 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
248 fragS *frag; /* Which frag? */
249 int where; /* Where in that frag? */
250 int size; /* 1, 2, or 4 usually. */
251 symbolS *add_symbol; /* X_add_symbol. */
252 offsetT offset; /* X_add_number. */
253 int pcrel; /* TRUE if PC-relative relocation. */
255 bfd_reloc_code_real_type r_type; /* Relocation type. */
257 int r_type; /* Relocation type. */
260 return fix_new_internal (frag, where, size, add_symbol,
261 (symbolS *) NULL, offset, pcrel, r_type);
264 /* Create a fixup for an expression. Currently we only support fixups
265 for difference expressions. That is itself more than most object
266 file formats support anyhow. */
269 fix_new_exp (frag, where, size, exp, pcrel, r_type)
270 fragS *frag; /* Which frag? */
271 int where; /* Where in that frag? */
272 int size; /* 1, 2, or 4 usually. */
273 expressionS *exp; /* Expression. */
274 int pcrel; /* TRUE if PC-relative relocation. */
276 bfd_reloc_code_real_type r_type; /* Relocation type. */
278 int r_type; /* Relocation type. */
291 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
292 the difference expression cannot immediately be reduced. */
294 symbolS *stmp = make_expr_symbol (exp);
296 exp->X_op = O_symbol;
297 exp->X_op_symbol = 0;
298 exp->X_add_symbol = stmp;
299 exp->X_add_number = 0;
301 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
305 add = exp->X_add_symbol;
306 off = exp->X_add_number;
308 #if defined(BFD_ASSEMBLER)
309 r_type = BFD_RELOC_RVA;
311 #if defined(TC_RVA_RELOC)
312 r_type = TC_RVA_RELOC;
314 as_fatal (_("rva not supported"));
320 sub = exp->X_add_symbol;
321 off = exp->X_add_number;
325 sub = exp->X_op_symbol;
328 add = exp->X_add_symbol;
331 off = exp->X_add_number;
335 add = make_expr_symbol (exp);
339 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
342 /* Append a string onto another string, bumping the pointer along. */
344 append (charPP, fromP, length)
347 unsigned long length;
349 /* Don't trust memcpy() of 0 chars. */
353 memcpy (*charPP, fromP, length);
357 #ifndef BFD_ASSEMBLER
358 int section_alignment[SEG_MAXIMUM_ORDINAL];
361 /* This routine records the largest alignment seen for each segment.
362 If the beginning of the segment is aligned on the worst-case
363 boundary, all of the other alignments within it will work. At
364 least one object format really uses this info. */
367 record_alignment (seg, align)
368 /* Segment to which alignment pertains. */
370 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
374 if (seg == absolute_section)
377 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
378 bfd_set_section_alignment (stdoutput, seg, align);
380 if (align > section_alignment[(int) seg])
381 section_alignment[(int) seg] = align;
387 /* Reset the section indices after removing the gas created sections. */
390 renumber_sections (abfd, sec, countparg)
391 bfd *abfd ATTRIBUTE_UNUSED;
395 int *countp = (int *) countparg;
397 sec->index = *countp;
401 #endif /* defined (BFD_ASSEMBLER) */
403 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
406 chain_frchains_together_1 (section, frchp)
408 struct frchain *frchp;
410 fragS dummy, *prev_frag = &dummy;
412 fixS fix_dummy, *prev_fix = &fix_dummy;
415 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
417 prev_frag->fr_next = frchp->frch_root;
418 prev_frag = frchp->frch_last;
419 assert (prev_frag->fr_type != 0);
421 if (frchp->fix_root != (fixS *) NULL)
423 if (seg_info (section)->fix_root == (fixS *) NULL)
424 seg_info (section)->fix_root = frchp->fix_root;
425 prev_fix->fx_next = frchp->fix_root;
426 seg_info (section)->fix_tail = frchp->fix_tail;
427 prev_fix = frchp->fix_tail;
431 assert (prev_frag->fr_type != 0);
432 prev_frag->fr_next = 0;
441 chain_frchains_together (abfd, section, xxx)
442 bfd *abfd ATTRIBUTE_UNUSED;
444 PTR xxx ATTRIBUTE_UNUSED;
446 segment_info_type *info;
448 /* BFD may have introduced its own sections without using
449 subseg_new, so it is possible that seg_info is NULL. */
450 info = seg_info (section);
451 if (info != (segment_info_type *) NULL)
452 info->frchainP->frch_last
453 = chain_frchains_together_1 (section, info->frchainP);
455 /* Now that we've chained the frags together, we must add new fixups
456 to the segment, not to the frag chain. */
462 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
465 remove_subsegs (head, seg, root, last)
471 *root = head->frch_root;
472 *last = chain_frchains_together_1 (seg, head);
477 #if defined (BFD_ASSEMBLER) || !defined (BFD)
481 cvt_frag_to_fill (sec, fragP)
482 segT sec ATTRIBUTE_UNUSED;
486 cvt_frag_to_fill (headersP, sec, fragP)
487 object_headers *headersP;
492 switch (fragP->fr_type)
499 HANDLE_ALIGN (fragP);
501 know (fragP->fr_next != NULL);
502 fragP->fr_offset = (fragP->fr_next->fr_address
504 - fragP->fr_fix) / fragP->fr_var;
505 if (fragP->fr_offset < 0)
507 as_bad_where (fragP->fr_file, fragP->fr_line,
508 _("attempt to .org/.space backwards? (%ld)"),
509 (long) fragP->fr_offset);
511 fragP->fr_type = rs_fill;
519 valueT value = S_GET_VALUE (fragP->fr_symbol);
522 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
525 fragP->fr_fix += size;
526 fragP->fr_type = rs_fill;
528 fragP->fr_offset = 0;
529 fragP->fr_symbol = NULL;
534 eh_frame_convert_frag (fragP);
537 case rs_machine_dependent:
539 md_convert_frag (stdoutput, sec, fragP);
541 md_convert_frag (headersP, sec, fragP);
544 assert (fragP->fr_next == NULL
545 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
548 /* After md_convert_frag, we make the frag into a ".space 0".
549 md_convert_frag() should set up any fixSs and constants
554 #ifndef WORKING_DOT_WORD
557 struct broken_word *lie;
559 if (fragP->fr_subtype)
561 fragP->fr_fix += md_short_jump_size;
562 for (lie = (struct broken_word *) (fragP->fr_symbol);
563 lie && lie->dispfrag == fragP;
564 lie = lie->next_broken_word)
566 fragP->fr_fix += md_long_jump_size;
574 BAD_CASE (fragP->fr_type);
579 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
583 relax_and_size_seg (abfd, sec, xxx)
586 PTR xxx ATTRIBUTE_UNUSED;
590 segment_info_type *seginfo;
592 valueT size, newsize;
594 subseg_change (sec, 0);
596 flags = bfd_get_section_flags (abfd, sec);
598 seginfo = seg_info (sec);
599 if (seginfo && seginfo->frchainP)
601 relax_segment (seginfo->frchainP->frch_root, sec);
602 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
603 cvt_frag_to_fill (sec, fragp);
604 for (fragp = seginfo->frchainP->frch_root;
606 fragp = fragp->fr_next)
607 /* Walk to last elt. */
609 size = fragp->fr_address + fragp->fr_fix;
614 if (size > 0 && ! seginfo->bss)
615 flags |= SEC_HAS_CONTENTS;
617 /* @@ This is just an approximation. */
618 if (seginfo && seginfo->fix_root)
622 x = bfd_set_section_flags (abfd, sec, flags);
625 newsize = md_section_align (sec, size);
626 x = bfd_set_section_size (abfd, sec, newsize);
629 /* If the size had to be rounded up, add some padding in the last
631 assert (newsize >= size);
634 fragS *last = seginfo->frchainP->frch_last;
635 fragp = seginfo->frchainP->frch_root;
636 while (fragp->fr_next != last)
637 fragp = fragp->fr_next;
638 last->fr_address = size;
639 fragp->fr_offset += newsize - size;
642 #ifdef tc_frob_section
643 tc_frob_section (sec);
645 #ifdef obj_frob_section
646 obj_frob_section (sec);
652 dump_section_relocs (abfd, sec, stream_)
653 bfd *abfd ATTRIBUTE_UNUSED;
657 FILE *stream = (FILE *) stream_;
658 segment_info_type *seginfo = seg_info (sec);
659 fixS *fixp = seginfo->fix_root;
664 fprintf (stream, "sec %s relocs:\n", sec->name);
667 symbolS *s = fixp->fx_addsy;
669 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
670 (int) fixp->fx_r_type);
672 fprintf (stream, "no sym\n");
675 print_symbol_value_1 (stream, s);
676 fprintf (stream, "\n");
678 fixp = fixp->fx_next;
682 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
685 #ifndef EMIT_SECTION_SYMBOLS
686 #define EMIT_SECTION_SYMBOLS 1
690 adjust_reloc_syms (abfd, sec, xxx)
691 bfd *abfd ATTRIBUTE_UNUSED;
693 PTR xxx ATTRIBUTE_UNUSED;
695 segment_info_type *seginfo = seg_info (sec);
701 dump_section_relocs (abfd, sec, stderr);
703 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
707 else if (fixp->fx_addsy)
713 fprintf (stderr, "\n\nadjusting fixup:\n");
717 sym = fixp->fx_addsy;
719 /* All symbols should have already been resolved at this
720 point. It is possible to see unresolved expression
721 symbols, though, since they are not in the regular symbol
724 resolve_symbol_value (sym, 1);
726 if (fixp->fx_subsy != NULL)
727 resolve_symbol_value (fixp->fx_subsy, 1);
729 /* If this symbol is equated to an undefined symbol, convert
730 the fixup to being against that symbol. */
731 if (sym != NULL && symbol_equated_p (sym)
732 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
734 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
735 sym = symbol_get_value_expression (sym)->X_add_symbol;
736 fixp->fx_addsy = sym;
739 if (sym != NULL && symbol_mri_common_p (sym))
741 /* These symbols are handled specially in fixup_segment. */
745 symsec = S_GET_SEGMENT (sym);
750 if (bfd_is_abs_section (symsec))
752 /* The fixup_segment routine will not use this symbol in a
753 relocation unless TC_FORCE_RELOCATION returns 1. */
754 if (TC_FORCE_RELOCATION (fixp))
756 symbol_mark_used_in_reloc (fixp->fx_addsy);
757 #ifdef UNDEFINED_DIFFERENCE_OK
758 if (fixp->fx_subsy != NULL)
759 symbol_mark_used_in_reloc (fixp->fx_subsy);
765 /* If it's one of these sections, assume the symbol is
766 definitely going to be output. The code in
767 md_estimate_size_before_relax in tc-mips.c uses this test
768 as well, so if you change this code you should look at that
770 if (bfd_is_und_section (symsec)
771 || bfd_is_com_section (symsec))
773 symbol_mark_used_in_reloc (fixp->fx_addsy);
774 #ifdef UNDEFINED_DIFFERENCE_OK
775 /* We have the difference of an undefined symbol and some
776 other symbol. Make sure to mark the other symbol as used
777 in a relocation so that it will always be output. */
779 symbol_mark_used_in_reloc (fixp->fx_subsy);
784 /* Don't try to reduce relocs which refer to non-local symbols
785 in .linkonce sections. It can lead to confusion when a
786 debugging section refers to a .linkonce section. I hope
787 this will always be correct. */
788 if (symsec != sec && ! S_IS_LOCAL (sym))
794 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
799 /* The GNU toolchain uses an extension for ELF: a section
800 beginning with the magic string .gnu.linkonce is a
802 if (strncmp (segment_name (symsec), ".gnu.linkonce",
803 sizeof ".gnu.linkonce" - 1) == 0)
809 symbol_mark_used_in_reloc (fixp->fx_addsy);
810 #ifdef UNDEFINED_DIFFERENCE_OK
811 if (fixp->fx_subsy != NULL)
812 symbol_mark_used_in_reloc (fixp->fx_subsy);
818 /* Since we're reducing to section symbols, don't attempt to reduce
819 anything that's already using one. */
820 if (symbol_section_p (sym))
822 symbol_mark_used_in_reloc (fixp->fx_addsy);
827 /* We can never adjust a reloc against a weak symbol. If we
828 did, and the weak symbol was overridden by a real symbol
829 somewhere else, then our relocation would be pointing at
830 the wrong area of memory. */
833 symbol_mark_used_in_reloc (fixp->fx_addsy);
838 /* Is there some other reason we can't adjust this one? (E.g.,
839 call/bal links in i960-bout symbols.) */
840 #ifdef obj_fix_adjustable
841 if (! obj_fix_adjustable (fixp))
843 symbol_mark_used_in_reloc (fixp->fx_addsy);
848 /* Is there some other (target cpu dependent) reason we can't adjust
849 this one? (E.g. relocations involving function addresses on
851 #ifdef tc_fix_adjustable
852 if (! tc_fix_adjustable (fixp))
854 symbol_mark_used_in_reloc (fixp->fx_addsy);
859 /* If the section symbol isn't going to be output, the relocs
860 at least should still work. If not, figure out what to do
861 when we run into that case.
863 We refetch the segment when calling section_symbol, rather
864 than using symsec, because S_GET_VALUE may wind up changing
865 the section when it calls resolve_symbol_value. */
866 fixp->fx_offset += S_GET_VALUE (sym);
867 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
868 symbol_mark_used_in_reloc (fixp->fx_addsy);
870 fprintf (stderr, "\nadjusted fixup:\n");
877 #if 1 /* def RELOC_REQUIRES_SYMBOL */
880 /* There was no symbol required by this relocation. However,
881 BFD doesn't really handle relocations without symbols well.
882 (At least, the COFF support doesn't.) So for now we fake up
883 a local symbol in the absolute section. */
885 fixp->fx_addsy = section_symbol (absolute_section);
887 fixp->fx_addsy->sy_used_in_reloc = 1;
892 dump_section_relocs (abfd, sec, stderr);
896 write_relocs (abfd, sec, xxx)
899 PTR xxx ATTRIBUTE_UNUSED;
901 segment_info_type *seginfo = seg_info (sec);
908 /* If seginfo is NULL, we did not create this section; don't do
913 fixup_segment (seginfo->fix_root, sec);
916 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
919 #ifndef RELOC_EXPANSION_POSSIBLE
920 /* Set up reloc information as well. */
921 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
922 memset ((char *) relocs, 0, n * sizeof (arelent *));
925 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
928 bfd_reloc_status_type s;
937 /* If this is an undefined symbol which was equated to another
938 symbol, then use generate the reloc against the latter symbol
939 rather than the former. */
940 sym = fixp->fx_addsy;
941 while (symbol_equated_p (sym)
942 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
946 /* We must avoid looping, as that can occur with a badly
948 n = symbol_get_value_expression (sym)->X_add_symbol;
951 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
954 fixp->fx_addsy = sym;
956 reloc = tc_gen_reloc (sec, fixp);
964 /* This test is triggered inappropriately for the SH. */
965 if (fixp->fx_where + fixp->fx_size
966 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
970 s = bfd_install_relocation (stdoutput, reloc,
971 fixp->fx_frag->fr_literal,
972 fixp->fx_frag->fr_address,
978 case bfd_reloc_overflow:
979 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
981 case bfd_reloc_outofrange:
982 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation out of range"));
985 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
986 fixp->fx_file, fixp->fx_line, s);
991 n = n * MAX_RELOC_EXPANSION;
992 /* Set up reloc information as well. */
993 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
996 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
1000 bfd_reloc_status_type s;
1010 /* If this is an undefined symbol which was equated to another
1011 symbol, then generate the reloc against the latter symbol
1012 rather than the former. */
1013 sym = fixp->fx_addsy;
1014 while (symbol_equated_p (sym)
1015 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
1016 sym = symbol_get_value_expression (sym)->X_add_symbol;
1017 fixp->fx_addsy = sym;
1019 reloc = tc_gen_reloc (sec, fixp);
1021 for (j = 0; reloc[j]; j++)
1023 relocs[i++] = reloc[j];
1026 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1027 if (fixp->fx_where + fixp->fx_size
1028 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1029 as_bad_where (fixp->fx_file, fixp->fx_line,
1030 _("internal error: fixup not contained within frag"));
1031 for (j = 0; reloc[j]; j++)
1033 s = bfd_install_relocation (stdoutput, reloc[j],
1034 fixp->fx_frag->fr_literal,
1035 fixp->fx_frag->fr_address,
1041 case bfd_reloc_overflow:
1042 as_bad_where (fixp->fx_file, fixp->fx_line,
1043 _("relocation overflow"));
1046 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1047 fixp->fx_file, fixp->fx_line);
1058 sympp = bfd_get_outsymbols (stdoutput);
1059 nsyms = bfd_get_symcount (stdoutput);
1060 for (i = 0; i < n; i++)
1061 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1063 for (j = 0; j < nsyms; j++)
1064 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1073 bfd_set_reloc (stdoutput, sec, relocs, n);
1075 bfd_set_section_flags (abfd, sec,
1076 (bfd_get_section_flags (abfd, sec)
1077 & (flagword) ~SEC_RELOC));
1079 #ifdef SET_SECTION_RELOCS
1080 SET_SECTION_RELOCS (sec, relocs, n);
1088 fprintf (stderr, "relocs for sec %s\n", sec->name);
1089 for (i = 0; i < n; i++)
1092 s = *r->sym_ptr_ptr;
1093 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1094 i, r, r->address, s->name, r->addend);
1101 write_contents (abfd, sec, xxx)
1102 bfd *abfd ATTRIBUTE_UNUSED;
1104 PTR xxx ATTRIBUTE_UNUSED;
1106 segment_info_type *seginfo = seg_info (sec);
1107 unsigned long offset = 0;
1110 /* Write out the frags. */
1112 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1115 for (f = seginfo->frchainP->frch_root;
1120 unsigned long fill_size;
1124 assert (f->fr_type == rs_fill);
1127 x = bfd_set_section_contents (stdoutput, sec,
1128 f->fr_literal, (file_ptr) offset,
1129 (bfd_size_type) f->fr_fix);
1132 bfd_perror (stdoutput->filename);
1133 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1134 exit (EXIT_FAILURE);
1136 offset += f->fr_fix;
1138 fill_literal = f->fr_literal + f->fr_fix;
1139 fill_size = f->fr_var;
1140 count = f->fr_offset;
1141 assert (count >= 0);
1142 if (fill_size && count)
1145 if (fill_size > sizeof (buf))
1147 /* Do it the old way. Can this ever happen? */
1150 x = bfd_set_section_contents (stdoutput, sec,
1153 (bfd_size_type) fill_size);
1156 bfd_perror (stdoutput->filename);
1157 as_perror (_("FATAL: Can't write %s"),
1158 stdoutput->filename);
1159 exit (EXIT_FAILURE);
1161 offset += fill_size;
1166 /* Build a buffer full of fill objects and output it as
1167 often as necessary. This saves on the overhead of
1168 potentially lots of bfd_set_section_contents calls. */
1172 n_per_buf = sizeof (buf);
1173 memset (buf, *fill_literal, n_per_buf);
1178 n_per_buf = sizeof (buf) / fill_size;
1179 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1180 memcpy (bufp, fill_literal, fill_size);
1182 for (; count > 0; count -= n_per_buf)
1184 n_per_buf = n_per_buf > count ? count : n_per_buf;
1185 x = bfd_set_section_contents
1186 (stdoutput, sec, buf, (file_ptr) offset,
1187 (bfd_size_type) n_per_buf * fill_size);
1189 as_fatal (_("Cannot write to output file."));
1190 offset += n_per_buf * fill_size;
1198 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1200 merge_data_into_text ()
1202 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1203 seg_info (text_section)->frchainP->frch_last->fr_next =
1204 seg_info (data_section)->frchainP->frch_root;
1205 seg_info (text_section)->frchainP->frch_last =
1206 seg_info (data_section)->frchainP->frch_last;
1207 seg_info (data_section)->frchainP = 0;
1211 text_last_frag->fr_next = data_frag_root;
1212 text_last_frag = data_last_frag;
1213 data_last_frag = NULL;
1214 data_frag_root = NULL;
1217 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1218 tmp->fx_next = data_fix_root;
1219 text_fix_tail = data_fix_tail;
1222 text_fix_root = data_fix_root;
1223 data_fix_root = NULL;
1226 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1228 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1230 relax_and_size_all_segments ()
1234 relax_segment (text_frag_root, SEG_TEXT);
1235 relax_segment (data_frag_root, SEG_DATA);
1236 relax_segment (bss_frag_root, SEG_BSS);
1238 /* Now the addresses of frags are correct within the segment. */
1239 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1240 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1241 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1243 /* Join the 2 segments into 1 huge segment.
1244 To do this, re-compute every rn_address in the SEG_DATA frags.
1245 Then join the data frags after the text frags.
1247 Determine a_data [length of data segment]. */
1250 register relax_addressT slide;
1252 know ((text_last_frag->fr_type == rs_fill)
1253 && (text_last_frag->fr_offset == 0));
1255 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1256 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1257 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1259 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1260 /* For b.out: If the data section has a strict alignment
1261 requirement, its load address in the .o file will be
1262 rounded up from the size of the text section. These
1263 two values are *not* the same! Similarly for the bss
1265 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1268 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1269 fragP->fr_address += slide;
1271 know (text_last_frag != 0);
1272 text_last_frag->fr_next = data_frag_root;
1276 H_SET_DATA_SIZE (&headers, 0);
1280 /* See above comments on b.out data section address. */
1283 if (data_last_frag == 0)
1284 bss_vma = H_GET_TEXT_SIZE (&headers);
1286 bss_vma = data_last_frag->fr_address;
1287 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1288 bss_address_frag.fr_address = bss_vma;
1290 #else /* ! OBJ_BOUT */
1291 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1292 H_GET_DATA_SIZE (&headers));
1294 #endif /* ! OBJ_BOUT */
1296 /* Slide all the frags. */
1299 relax_addressT slide = bss_address_frag.fr_address;
1301 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1302 fragP->fr_address += slide;
1306 H_SET_BSS_SIZE (&headers,
1307 bss_last_frag->fr_address - bss_frag_root->fr_address);
1309 H_SET_BSS_SIZE (&headers, 0);
1311 #endif /* ! BFD_ASSEMBLER && ! BFD */
1313 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1315 #ifdef BFD_ASSEMBLER
1323 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1325 /* Count symbols. We can't rely on a count made by the loop in
1326 write_object_file, because *_frob_file may add a new symbol or
1329 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1336 asympp = (asymbol **) bfd_alloc (stdoutput,
1337 nsyms * sizeof (asymbol *));
1338 symp = symbol_rootP;
1339 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1341 asympp[i] = symbol_get_bfdsym (symp);
1342 symbol_mark_written (symp);
1347 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1348 assert (result == true);
1349 symbol_table_frozen = 1;
1353 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1355 set_segment_vma (abfd, sec, xxx)
1358 PTR xxx ATTRIBUTE_UNUSED;
1360 static bfd_vma addr = 0;
1362 bfd_set_section_vma (abfd, sec, addr);
1363 addr += bfd_section_size (abfd, sec);
1365 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1367 /* Finish the subsegments. After every sub-segment, we fake an
1368 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1369 ".fill 0" because that is the kind of frag that requires least
1370 thought. ".align" frags like to have a following frag since that
1371 makes calculating their intended length trivial. */
1373 #ifndef SUB_SEGMENT_ALIGN
1374 #ifdef BFD_ASSEMBLER
1375 #define SUB_SEGMENT_ALIGN(SEG) (0)
1377 #define SUB_SEGMENT_ALIGN(SEG) (2)
1384 struct frchain *frchainP;
1386 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1388 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1390 /* This now gets called even if we had errors. In that case,
1391 any alignment is meaningless, and, moreover, will look weird
1392 if we are generating a listing. */
1393 frag_align (had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg),
1394 subseg_text_p (now_seg) ? NOP_OPCODE : 0,
1397 /* frag_align will have left a new frag.
1398 Use this last frag for an empty ".fill".
1400 For this segment ...
1401 Create a last frag. Do not leave a "being filled in frag". */
1402 frag_wane (frag_now);
1403 frag_now->fr_fix = 0;
1404 know (frag_now->fr_next == NULL);
1408 /* Write the object file. */
1411 write_object_file ()
1413 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1414 fragS *fragP; /* Track along all frags. */
1417 /* Do we really want to write it? */
1419 int n_warns, n_errs;
1420 n_warns = had_warnings ();
1421 n_errs = had_errors ();
1422 /* The -Z flag indicates that an object file should be generated,
1423 regardless of warnings and errors. */
1424 if (flag_always_generate_output)
1426 if (n_warns || n_errs)
1427 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1428 n_errs, n_errs == 1 ? "" : "s",
1429 n_warns, n_warns == 1 ? "" : "s");
1434 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1435 n_errs, n_errs == 1 ? "" : "s",
1436 n_warns, n_warns == 1 ? "" : "s");
1441 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1442 a routine to check for the definition of the procedure "_main",
1443 and if so -- fix it up so that it can be program entry point. */
1444 vms_check_for_main ();
1445 #endif /* OBJ_VMS */
1447 /* From now on, we don't care about sub-segments. Build one frag chain
1448 for each segment. Linked thru fr_next. */
1450 #ifdef BFD_ASSEMBLER
1451 /* Remove the sections created by gas for its own purposes. */
1453 asection **seclist, *sec;
1456 seclist = &stdoutput->sections;
1457 while (seclist && *seclist)
1460 while (sec == reg_section || sec == expr_section)
1464 stdoutput->section_count--;
1469 seclist = &(*seclist)->next;
1472 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1475 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1477 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1478 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1479 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1482 /* We have two segments. If user gave -R flag, then we must put the
1483 data frags into the text segment. Do this before relaxing so
1484 we know to take advantage of -R and make shorter addresses. */
1485 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1486 if (flag_readonly_data_in_text)
1488 merge_data_into_text ();
1492 #ifdef BFD_ASSEMBLER
1493 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1495 relax_and_size_all_segments ();
1496 #endif /* BFD_ASSEMBLER */
1498 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1499 /* Now that the segments have their final sizes, run through the
1500 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1501 should too. Currently, only DJGPP uses this code, but other
1502 COFF targets may need to execute this too. */
1503 bfd_map_over_sections (stdoutput, set_segment_vma, (char *) 0);
1506 #ifndef BFD_ASSEMBLER
1507 /* Crawl the symbol chain.
1509 For each symbol whose value depends on a frag, take the address of
1510 that frag and subsume it into the value of the symbol.
1511 After this, there is just one way to lookup a symbol value.
1512 Values are left in their final state for object file emission.
1513 We adjust the values of 'L' local symbols, even if we do
1514 not intend to emit them to the object file, because their values
1515 are needed for fix-ups.
1517 Unless we saw a -L flag, remove all symbols that begin with 'L'
1518 from the symbol chain. (They are still pointed to by the fixes.)
1520 Count the remaining symbols.
1521 Assign a symbol number to each symbol.
1522 Count the number of string-table chars we will emit.
1523 Put this info into the headers as appropriate. */
1524 know (zero_address_frag.fr_address == 0);
1525 string_byte_count = sizeof (string_byte_count);
1527 obj_crawl_symbol_chain (&headers);
1529 if (string_byte_count == sizeof (string_byte_count))
1530 string_byte_count = 0;
1532 H_SET_STRING_SIZE (&headers, string_byte_count);
1534 /* Addresses of frags now reflect addresses we use in the object file.
1535 Symbol values are correct.
1536 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1537 Also converting any machine-dependent frags using md_convert_frag(); */
1538 subseg_change (SEG_TEXT, 0);
1540 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1542 /* At this point we have linked all the frags into a single
1543 chain. However, cvt_frag_to_fill may call md_convert_frag
1544 which may call fix_new. We need to ensure that fix_new adds
1545 the fixup to the right section. */
1546 if (fragP == data_frag_root)
1547 subseg_change (SEG_DATA, 0);
1549 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1551 /* Some assert macros don't work with # directives mixed in. */
1553 if (!(fragP->fr_next == NULL
1555 || fragP->fr_next == data_frag_root
1557 || ((fragP->fr_next->fr_address - fragP->fr_address)
1558 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1562 #endif /* ! BFD_ASSEMBLER */
1564 #ifndef WORKING_DOT_WORD
1566 struct broken_word *lie;
1567 struct broken_word **prevP;
1569 prevP = &broken_words;
1570 for (lie = broken_words; lie; lie = lie->next_broken_word)
1575 subseg_change (lie->seg, lie->subseg);
1576 exp.X_op = O_subtract;
1577 exp.X_add_symbol = lie->add;
1578 exp.X_op_symbol = lie->sub;
1579 exp.X_add_number = lie->addnum;
1580 #ifdef BFD_ASSEMBLER
1581 #ifdef TC_CONS_FIX_NEW
1582 TC_CONS_FIX_NEW (lie->frag,
1583 lie->word_goes_here - lie->frag->fr_literal,
1586 fix_new_exp (lie->frag,
1587 lie->word_goes_here - lie->frag->fr_literal,
1588 2, &exp, 0, BFD_RELOC_16);
1591 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1592 fix_new_exp (lie->frag,
1593 lie->word_goes_here - lie->frag->fr_literal,
1594 2, &exp, 0, NO_RELOC);
1597 fix_new_ns32k_exp (lie->frag,
1598 lie->word_goes_here - lie->frag->fr_literal,
1599 2, &exp, 0, 0, 2, 0, 0);
1601 fix_new_exp (lie->frag,
1602 lie->word_goes_here - lie->frag->fr_literal,
1604 #endif /* TC_NS32K */
1605 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1606 #endif /* BFD_ASSEMBLER */
1607 *prevP = lie->next_broken_word;
1610 prevP = &(lie->next_broken_word);
1612 for (lie = broken_words; lie;)
1614 struct broken_word *untruth;
1616 addressT table_addr;
1617 addressT from_addr, to_addr;
1620 subseg_change (lie->seg, lie->subseg);
1621 fragP = lie->dispfrag;
1623 /* Find out how many broken_words go here. */
1626 untruth && untruth->dispfrag == fragP;
1627 untruth = untruth->next_broken_word)
1628 if (untruth->added == 1)
1631 table_ptr = lie->dispfrag->fr_opcode;
1632 table_addr = (lie->dispfrag->fr_address
1633 + (table_ptr - lie->dispfrag->fr_literal));
1634 /* Create the jump around the long jumps. This is a short
1635 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1636 from_addr = table_addr;
1637 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1638 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1640 table_ptr += md_short_jump_size;
1641 table_addr += md_short_jump_size;
1644 lie && lie->dispfrag == fragP;
1645 m++, lie = lie->next_broken_word)
1647 if (lie->added == 2)
1649 /* Patch the jump table. */
1650 /* This is the offset from ??? to table_ptr+0. */
1651 to_addr = table_addr - S_GET_VALUE (lie->sub);
1652 #ifdef BFD_ASSEMBLER
1653 to_addr -= symbol_get_frag (lie->sub)->fr_address;
1655 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1656 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1658 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1659 for (untruth = lie->next_broken_word;
1660 untruth && untruth->dispfrag == fragP;
1661 untruth = untruth->next_broken_word)
1663 if (untruth->use_jump == lie)
1664 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1667 /* Install the long jump. */
1668 /* This is a long jump from table_ptr+0 to the final target. */
1669 from_addr = table_addr;
1670 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1671 #ifdef BFD_ASSEMBLER
1672 to_addr += symbol_get_frag (lie->add)->fr_address;
1674 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1676 table_ptr += md_long_jump_size;
1677 table_addr += md_long_jump_size;
1681 #endif /* not WORKING_DOT_WORD */
1683 #ifndef BFD_ASSEMBLER
1686 char *the_object_file;
1687 long object_file_size;
1688 /* Scan every FixS performing fixups. We had to wait until now to
1689 do this because md_convert_frag() may have made some fixSs. */
1692 subseg_change (SEG_TEXT, 0);
1693 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1694 subseg_change (SEG_DATA, 0);
1695 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1696 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1698 /* FIXME: Move this stuff into the pre-write-hook. */
1699 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1700 H_SET_ENTRY_POINT (&headers, 0);
1702 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1704 object_file_size = H_GET_FILE_SIZE (&headers);
1705 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1707 output_file_create (out_file_name);
1709 obj_header_append (&next_object_file_charP, &headers);
1711 know ((next_object_file_charP - the_object_file)
1712 == H_GET_HEADER_SIZE (&headers));
1715 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1717 register long count;
1718 register char *fill_literal;
1719 register long fill_size;
1722 know (fragP->fr_type == rs_fill);
1723 append (&next_object_file_charP, fragP->fr_literal,
1724 (unsigned long) fragP->fr_fix);
1725 fill_literal = fragP->fr_literal + fragP->fr_fix;
1726 fill_size = fragP->fr_var;
1727 know (fragP->fr_offset >= 0);
1729 for (count = fragP->fr_offset; count; count--)
1730 append (&next_object_file_charP, fill_literal,
1731 (unsigned long) fill_size);
1734 know ((next_object_file_charP - the_object_file)
1735 == (H_GET_HEADER_SIZE (&headers)
1736 + H_GET_TEXT_SIZE (&headers)
1737 + H_GET_DATA_SIZE (&headers)));
1739 /* Emit relocations. */
1740 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1741 (relax_addressT) 0);
1742 know ((next_object_file_charP - the_object_file)
1743 == (H_GET_HEADER_SIZE (&headers)
1744 + H_GET_TEXT_SIZE (&headers)
1745 + H_GET_DATA_SIZE (&headers)
1746 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1748 /* Make addresses in data relocation directives relative to beginning of
1749 first data fragment, not end of last text fragment: alignment of the
1750 start of the data segment may place a gap between the segments. */
1751 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1752 data0_frchainP->frch_root->fr_address);
1754 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1755 text_last_frag->fr_address);
1756 #endif /* TC_I960 */
1758 know ((next_object_file_charP - the_object_file)
1759 == (H_GET_HEADER_SIZE (&headers)
1760 + H_GET_TEXT_SIZE (&headers)
1761 + H_GET_DATA_SIZE (&headers)
1762 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1763 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1765 /* Emit line number entries. */
1766 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1767 know ((next_object_file_charP - the_object_file)
1768 == (H_GET_HEADER_SIZE (&headers)
1769 + H_GET_TEXT_SIZE (&headers)
1770 + H_GET_DATA_SIZE (&headers)
1771 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1772 + H_GET_DATA_RELOCATION_SIZE (&headers)
1773 + H_GET_LINENO_SIZE (&headers)));
1776 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1777 know ((next_object_file_charP - the_object_file)
1778 == (H_GET_HEADER_SIZE (&headers)
1779 + H_GET_TEXT_SIZE (&headers)
1780 + H_GET_DATA_SIZE (&headers)
1781 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1782 + H_GET_DATA_RELOCATION_SIZE (&headers)
1783 + H_GET_LINENO_SIZE (&headers)
1784 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1787 if (string_byte_count > 0)
1788 obj_emit_strings (&next_object_file_charP);
1791 bfd_seek (stdoutput, 0, 0);
1792 bfd_write (the_object_file, 1, object_file_size, stdoutput);
1795 /* Write the data to the file. */
1796 output_file_append (the_object_file, object_file_size, out_file_name);
1797 free (the_object_file);
1801 /* Now do the VMS-dependent part of writing the object file. */
1802 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1803 H_GET_DATA_SIZE (&headers),
1804 H_GET_BSS_SIZE (&headers),
1805 text_frag_root, data_frag_root);
1806 #endif /* OBJ_VMS */
1807 #else /* BFD_ASSEMBLER */
1809 /* Resolve symbol values. This needs to be done before processing
1815 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1816 resolve_symbol_value (symp, 1);
1818 resolve_local_symbol_values ();
1822 #ifdef tc_frob_file_before_adjust
1823 tc_frob_file_before_adjust ();
1825 #ifdef obj_frob_file_before_adjust
1826 obj_frob_file_before_adjust ();
1829 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1831 /* Set up symbol table, and write it out. */
1836 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1841 if (symbol_mri_common_p (symp))
1843 if (S_IS_EXTERNAL (symp))
1844 as_bad (_("%s: global symbols not supported in common sections"),
1846 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1850 name = S_GET_NAME (symp);
1854 decode_local_label_name ((char *) S_GET_NAME (symp));
1855 /* They only differ if `name' is a fb or dollar local
1857 if (name2 != name && ! S_IS_DEFINED (symp))
1858 as_bad (_("local label %s is not defined"), name2);
1861 /* Do it again, because adjust_reloc_syms might introduce
1862 more symbols. They'll probably only be section symbols,
1863 but they'll still need to have the values computed. */
1864 resolve_symbol_value (symp, 1);
1866 /* Skip symbols which were equated to undefined or common
1868 if (symbol_equated_p (symp)
1869 && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
1871 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1875 /* So far, common symbols have been treated like undefined symbols.
1876 Put them in the common section now. */
1877 if (S_IS_DEFINED (symp) == 0
1878 && S_GET_VALUE (symp) != 0)
1879 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1881 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1882 S_GET_NAME (symp), symp,
1884 symbol_get_bfdsym (symp)->flags,
1885 segment_name (S_GET_SEGMENT (symp)));
1888 #ifdef obj_frob_symbol
1889 obj_frob_symbol (symp, punt);
1891 #ifdef tc_frob_symbol
1892 if (! punt || symbol_used_in_reloc_p (symp))
1893 tc_frob_symbol (symp, punt);
1896 /* If we don't want to keep this symbol, splice it out of
1897 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1898 want section symbols. Otherwise, we skip local symbols
1899 and symbols that the frob_symbol macros told us to punt,
1900 but we keep such symbols if they are used in relocs. */
1901 if ((! EMIT_SECTION_SYMBOLS
1902 && symbol_section_p (symp))
1903 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1904 opposites. Sometimes the former checks flags and the
1905 latter examines the name... */
1906 || (!S_IS_EXTERN (symp)
1907 && (S_IS_LOCAL (symp) || punt)
1908 && ! symbol_used_in_reloc_p (symp)))
1910 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1912 /* After symbol_remove, symbol_next(symp) still returns
1913 the one that came after it in the chain. So we don't
1914 need to do any extra cleanup work here. */
1918 /* Make sure we really got a value for the symbol. */
1919 if (! symbol_resolved_p (symp))
1921 as_bad (_("can't resolve value for symbol \"%s\""),
1923 symbol_mark_resolved (symp);
1926 /* Set the value into the BFD symbol. Up til now the value
1927 has only been kept in the gas symbolS struct. */
1928 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1934 /* Now do any format-specific adjustments to the symbol table, such
1935 as adding file symbols. */
1936 #ifdef tc_adjust_symtab
1937 tc_adjust_symtab ();
1939 #ifdef obj_adjust_symtab
1940 obj_adjust_symtab ();
1943 /* Now that all the sizes are known, and contents correct, we can
1944 start writing to the file. */
1947 /* If *_frob_file changes the symbol value at this point, it is
1948 responsible for moving the changed value into symp->bsym->value
1949 as well. Hopefully all symbol value changing can be done in
1954 #ifdef obj_frob_file
1958 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1960 #ifdef tc_frob_file_after_relocs
1961 tc_frob_file_after_relocs ();
1963 #ifdef obj_frob_file_after_relocs
1964 obj_frob_file_after_relocs ();
1967 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1968 #endif /* BFD_ASSEMBLER */
1972 #ifdef TC_GENERIC_RELAX_TABLE
1974 static int is_dnrange PARAMS ((fragS *, fragS *));
1976 /* Subroutines of relax_segment. */
1982 for (; f1; f1 = f1->fr_next)
1983 if (f1->fr_next == f2)
1988 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1991 relax_frag (fragP, stretch)
1995 const relax_typeS *this_type;
1996 const relax_typeS *start_type;
1997 relax_substateT next_state;
1998 relax_substateT this_state;
1999 long aim, target, growth;
2000 symbolS *symbolP = fragP->fr_symbol;
2001 long offset = fragP->fr_offset;
2002 /* Recompute was_address by undoing "+= stretch" done by relax_segment. */
2003 unsigned long was_address = fragP->fr_address - stretch;
2004 unsigned long address = fragP->fr_address;
2005 const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
2007 this_state = fragP->fr_subtype;
2008 start_type = this_type = table + this_state;
2013 #ifndef DIFF_EXPR_OK
2014 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2015 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2016 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2017 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2018 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2020 know (symbolP->sy_frag);
2022 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2023 || symbolP->sy_frag == &zero_address_frag);
2024 target += S_GET_VALUE (symbolP) + symbol_get_frag (symbolP)->fr_address;
2026 /* If frag has yet to be reached on this pass,
2027 assume it will move by STRETCH just as we did.
2028 If this is not so, it will be because some frag
2029 between grows, and that will force another pass.
2031 Beware zero-length frags.
2033 There should be a faster way to do this. */
2035 if (symbol_get_frag (symbolP)->fr_address >= was_address
2036 && is_dnrange (fragP, symbol_get_frag (symbolP)))
2042 aim = target - address - fragP->fr_fix;
2043 #ifdef TC_PCREL_ADJUST
2044 /* Currently only the ns32k family needs this. */
2045 aim += TC_PCREL_ADJUST (fragP);
2047 /* This machine doesn't want to use pcrel_adjust.
2048 In that case, pcrel_adjust should be zero. */
2050 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2053 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2054 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2059 /* Look backwards. */
2060 for (next_state = this_type->rlx_more; next_state;)
2061 if (aim >= this_type->rlx_backward)
2065 /* Grow to next state. */
2066 this_state = next_state;
2067 this_type = table + this_state;
2068 next_state = this_type->rlx_more;
2073 /* Look forwards. */
2074 for (next_state = this_type->rlx_more; next_state;)
2075 if (aim <= this_type->rlx_forward)
2079 /* Grow to next state. */
2080 this_state = next_state;
2081 this_type = table + this_state;
2082 next_state = this_type->rlx_more;
2086 growth = this_type->rlx_length - start_type->rlx_length;
2088 fragP->fr_subtype = this_state;
2092 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2094 /* Relax_align. Advance location counter to next address that has 'alignment'
2095 lowest order bits all 0s, return size of adjustment made. */
2096 static relax_addressT
2097 relax_align (address, alignment)
2098 register relax_addressT address; /* Address now. */
2099 register int alignment; /* Alignment (binary). */
2101 relax_addressT mask;
2102 relax_addressT new_address;
2104 mask = ~((~0) << alignment);
2105 new_address = (address + mask) & (~mask);
2106 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2108 /* We must provide lots of padding, so the linker can discard it
2109 when needed. The linker will not add extra space, ever. */
2110 new_address += (1 << alignment);
2112 return (new_address - address);
2115 /* Now we have a segment, not a crowd of sub-segments, we can make
2120 After this, all frags in this segment have addresses that are correct
2121 within the segment. Since segments live in different file addresses,
2122 these frag addresses may not be the same as final object-file
2126 relax_segment (segment_frag_root, segment)
2127 struct frag *segment_frag_root;
2130 register struct frag *fragP;
2131 register relax_addressT address;
2132 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2133 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2135 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2136 subseg_change (segment, 0);
2138 /* For each frag in segment: count and store (a 1st guess of)
2141 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2143 fragP->fr_address = address;
2144 address += fragP->fr_fix;
2146 switch (fragP->fr_type)
2149 address += fragP->fr_offset * fragP->fr_var;
2155 addressT offset = relax_align (address, (int) fragP->fr_offset);
2157 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2160 if (offset % fragP->fr_var != 0)
2162 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2163 (unsigned long) offset, (long) fragP->fr_var);
2164 offset -= (offset % fragP->fr_var);
2173 /* Assume .org is nugatory. It will grow with 1st relax. */
2176 case rs_machine_dependent:
2177 address += md_estimate_size_before_relax (fragP, segment);
2180 #ifndef WORKING_DOT_WORD
2181 /* Broken words don't concern us yet. */
2182 case rs_broken_word:
2187 /* Initial guess is always 1; doing otherwise can result in
2188 stable solutions that are larger than the minimum. */
2189 address += fragP->fr_offset = 1;
2193 address += eh_frame_estimate_size_before_relax (fragP);
2197 BAD_CASE (fragP->fr_type);
2204 long stretch; /* May be any size, 0 or negative. */
2205 /* Cumulative number of addresses we have relaxed this pass.
2206 We may have relaxed more than one address. */
2207 long stretched; /* Have we stretched on this pass? */
2208 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2209 grew, and another shrank. If a branch instruction doesn't fit anymore,
2210 we could be scrod. */
2214 stretch = stretched = 0;
2216 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2219 addressT was_address;
2223 was_address = fragP->fr_address;
2224 address = fragP->fr_address += stretch;
2225 symbolP = fragP->fr_symbol;
2226 offset = fragP->fr_offset;
2228 switch (fragP->fr_type)
2230 case rs_fill: /* .fill never relaxes. */
2234 #ifndef WORKING_DOT_WORD
2235 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2236 for it I do not want to write it. I do not want to have
2237 anything to do with it. This is not the proper way to
2238 implement this misfeature. */
2239 case rs_broken_word:
2241 struct broken_word *lie;
2242 struct broken_word *untruth;
2244 /* Yes this is ugly (storing the broken_word pointer
2245 in the symbol slot). Still, this whole chunk of
2246 code is ugly, and I don't feel like doing anything
2247 about it. Think of it as stubbornness in action. */
2249 for (lie = (struct broken_word *) (fragP->fr_symbol);
2250 lie && lie->dispfrag == fragP;
2251 lie = lie->next_broken_word)
2257 offset = (symbol_get_frag (lie->add)->fr_address
2258 + S_GET_VALUE (lie->add)
2260 - (symbol_get_frag (lie->sub)->fr_address
2261 + S_GET_VALUE (lie->sub)));
2262 if (offset <= -32768 || offset >= 32767)
2264 if (flag_warn_displacement)
2267 sprint_value (buf, (addressT) lie->addnum);
2268 as_warn (_(".word %s-%s+%s didn't fit"),
2269 S_GET_NAME (lie->add),
2270 S_GET_NAME (lie->sub),
2274 if (fragP->fr_subtype == 0)
2276 fragP->fr_subtype++;
2277 growth += md_short_jump_size;
2279 for (untruth = lie->next_broken_word;
2280 untruth && untruth->dispfrag == lie->dispfrag;
2281 untruth = untruth->next_broken_word)
2282 if ((symbol_get_frag (untruth->add)
2283 == symbol_get_frag (lie->add))
2284 && (S_GET_VALUE (untruth->add)
2285 == S_GET_VALUE (lie->add)))
2288 untruth->use_jump = lie;
2290 growth += md_long_jump_size;
2295 } /* case rs_broken_word */
2300 addressT oldoff, newoff;
2302 oldoff = relax_align (was_address + fragP->fr_fix,
2304 newoff = relax_align (address + fragP->fr_fix,
2307 if (fragP->fr_subtype != 0)
2309 if (oldoff > fragP->fr_subtype)
2311 if (newoff > fragP->fr_subtype)
2315 growth = newoff - oldoff;
2321 long target = offset;
2326 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2327 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2328 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2329 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2330 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2331 know (symbolP->sy_frag);
2332 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2333 || (symbolP->sy_frag == &zero_address_frag));
2335 target += (S_GET_VALUE (symbolP)
2336 + symbol_get_frag (symbolP)->fr_address);
2337 } /* if we have a symbol */
2339 know (fragP->fr_next);
2340 after = fragP->fr_next->fr_address;
2341 growth = target - after;
2344 /* Growth may be negative, but variable part of frag
2345 cannot have fewer than 0 chars. That is, we can't
2347 as_bad_where (fragP->fr_file, fragP->fr_line,
2348 _("attempt to .org backwards ignored"));
2350 /* We've issued an error message. Change the
2351 frag to avoid cascading errors. */
2352 fragP->fr_type = rs_align;
2353 fragP->fr_subtype = 0;
2354 fragP->fr_offset = 0;
2355 fragP->fr_fix = after - address;
2359 /* This is an absolute growth factor */
2367 growth = S_GET_VALUE (symbolP);
2368 if (symbol_get_frag (symbolP) != &zero_address_frag
2369 || S_IS_COMMON (symbolP)
2370 || ! S_IS_DEFINED (symbolP))
2371 as_bad_where (fragP->fr_file, fragP->fr_line,
2372 _(".space specifies non-absolute value"));
2373 fragP->fr_symbol = 0;
2376 as_warn (_(".space or .fill with negative value, ignored"));
2384 case rs_machine_dependent:
2385 #ifdef md_relax_frag
2386 growth = md_relax_frag (fragP, stretch);
2388 #ifdef TC_GENERIC_RELAX_TABLE
2389 /* The default way to relax a frag is to look through
2390 TC_GENERIC_RELAX_TABLE. */
2391 growth = relax_frag (fragP, stretch);
2392 #endif /* TC_GENERIC_RELAX_TABLE */
2401 value = resolve_symbol_value (fragP->fr_symbol, 0);
2402 size = sizeof_leb128 (value, fragP->fr_subtype);
2403 growth = size - fragP->fr_offset;
2404 fragP->fr_offset = size;
2409 growth = eh_frame_relax_frag (fragP);
2413 BAD_CASE (fragP->fr_type);
2421 } /* For each frag in the segment. */
2423 while (stretched); /* Until nothing further to relax. */
2426 /* We now have valid fr_address'es for each frag. */
2428 /* All fr_address's are correct, relative to their own segment.
2429 We have made all the fixS we will ever make. */
2432 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2434 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2435 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2440 Go through all the fixS's in a segment and see which ones can be
2441 handled now. (These consist of fixS where we have since discovered
2442 the value of a symbol, or the address of the frag involved.)
2443 For each one, call md_apply_fix to put the fix into the frag data.
2445 Result is a count of how many relocation structs will be needed to
2446 handle the remaining fixS's that we couldn't completely handle here.
2447 These will be output later by emit_relocations(). */
2450 fixup_segment (fixP, this_segment_type)
2451 register fixS *fixP;
2452 segT this_segment_type; /* N_TYPE bits for segment. */
2454 long seg_reloc_count = 0;
2455 symbolS *add_symbolP;
2456 symbolS *sub_symbolP;
2463 segT add_symbol_segment = absolute_section;
2465 /* If the linker is doing the relaxing, we must not do any fixups.
2467 Well, strictly speaking that's not true -- we could do any that are
2468 PC-relative and don't cross regions that could change size. And for the
2469 i960 (the only machine for which we've got a relaxing linker right now),
2470 we might be able to turn callx/callj into bal anyways in cases where we
2471 know the maximum displacement. */
2472 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment_type))
2474 for (; fixP; fixP = fixP->fx_next)
2476 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2477 return seg_reloc_count;
2480 for (; fixP; fixP = fixP->fx_next)
2483 fprintf (stderr, "\nprocessing fixup:\n");
2487 fragP = fixP->fx_frag;
2489 where = fixP->fx_where;
2490 place = fragP->fr_literal + where;
2491 size = fixP->fx_size;
2492 add_symbolP = fixP->fx_addsy;
2493 #ifdef TC_VALIDATE_FIX
2494 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2496 sub_symbolP = fixP->fx_subsy;
2497 add_number = fixP->fx_offset;
2498 pcrel = fixP->fx_pcrel;
2501 if (add_symbolP != NULL
2502 && symbol_mri_common_p (add_symbolP))
2504 know (add_symbolP->sy_value.X_op == O_symbol);
2505 add_number += S_GET_VALUE (add_symbolP);
2506 fixP->fx_offset = add_number;
2507 add_symbolP = fixP->fx_addsy =
2508 symbol_get_value_expression (add_symbolP)->X_add_symbol;
2512 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2516 resolve_symbol_value (sub_symbolP, 1);
2517 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2519 if (add_symbolP != NULL)
2521 add_number += S_GET_VALUE (add_symbolP);
2523 fixP->fx_addsy = NULL;
2526 /* It's just -sym. */
2527 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2529 add_number -= S_GET_VALUE (sub_symbolP);
2530 fixP->fx_subsy = NULL;
2533 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2535 /* Should try converting to a constant. */
2540 as_bad_where (fixP->fx_file, fixP->fx_line,
2541 _("Negative of non-absolute symbol %s"),
2542 S_GET_NAME (sub_symbolP));
2544 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2545 && SEG_NORMAL (add_symbol_segment))
2547 /* Difference of 2 symbols from same segment.
2548 Can't make difference of 2 undefineds: 'value' means
2549 something different for N_UNDF. */
2551 /* Makes no sense to use the difference of 2 arbitrary symbols
2552 as the target of a call instruction. */
2554 as_bad_where (fixP->fx_file, fixP->fx_line,
2555 _("callj to difference of 2 symbols"));
2556 #endif /* TC_I960 */
2557 add_number += S_GET_VALUE (add_symbolP) -
2558 S_GET_VALUE (sub_symbolP);
2561 pcrel = 0; /* No further pcrel processing. */
2563 /* Let the target machine make the final determination
2564 as to whether or not a relocation will be needed to
2565 handle this fixup. */
2566 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2569 fixP->fx_addsy = NULL;
2570 fixP->fx_subsy = NULL;
2575 /* Different segments in subtraction. */
2576 know (!(S_IS_EXTERNAL (sub_symbolP)
2577 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2579 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2580 add_number -= S_GET_VALUE (sub_symbolP);
2583 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2585 /* Do this even if it's already described as
2586 pc-relative. For example, on the m68k, an
2587 operand of "pc@(foo-.-2)" should address
2588 "foo" in a pc-relative mode. */
2593 /* Make it pc-relative. */
2594 add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
2595 - S_GET_VALUE (sub_symbolP));
2602 #ifdef UNDEFINED_DIFFERENCE_OK
2603 /* The PA needs this for PIC code generation. We basically
2604 don't want to do anything if we have the difference of two
2605 symbols at this point. */
2608 /* Leave it alone. */
2611 #ifdef BFD_ASSEMBLER
2612 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2613 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2615 /* Leave it alone. */
2621 sprint_value (buf, fragP->fr_address + where);
2622 as_bad_where (fixP->fx_file, fixP->fx_line,
2623 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2624 S_GET_NAME (add_symbolP),
2625 segment_name (S_GET_SEGMENT (add_symbolP)),
2626 S_GET_NAME (sub_symbolP),
2627 segment_name (S_GET_SEGMENT (sub_symbolP)),
2635 if (add_symbol_segment == this_segment_type && pcrel && !plt
2636 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2638 /* This fixup was made when the symbol's segment was
2639 SEG_UNKNOWN, but it is now in the local segment.
2640 So we know how to do the address without relocation. */
2642 /* reloc_callj() may replace a 'call' with a 'calls' or a
2643 'bal', in which cases it modifies *fixP as appropriate.
2644 In the case of a 'calls', no further work is required,
2645 and *fixP has been set up to make the rest of the code
2648 #endif /* TC_I960 */
2650 add_number += S_GET_VALUE (add_symbolP);
2651 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2652 /* Lie. Don't want further pcrel processing. */
2655 /* Let the target machine make the final determination
2656 as to whether or not a relocation will be needed to
2657 handle this fixup. */
2658 if (!TC_FORCE_RELOCATION (fixP))
2661 fixP->fx_addsy = NULL;
2666 if (add_symbol_segment == absolute_section
2670 /* See comment about reloc_callj() above. */
2672 #endif /* TC_I960 */
2673 add_number += S_GET_VALUE (add_symbolP);
2675 /* Let the target machine make the final determination
2676 as to whether or not a relocation will be needed to
2677 handle this fixup. */
2679 if (!TC_FORCE_RELOCATION (fixP))
2681 fixP->fx_addsy = NULL;
2685 else if (add_symbol_segment == undefined_section
2686 #ifdef BFD_ASSEMBLER
2687 || bfd_is_com_section (add_symbol_segment)
2692 if ((int) fixP->fx_bit_fixP == 13)
2694 /* This is a COBR instruction. They have only a
2695 13-bit displacement and are only to be used
2696 for local branches: flag as error, don't generate
2698 as_bad_where (fixP->fx_file, fixP->fx_line,
2699 _("can't use COBR format with external label"));
2700 fixP->fx_addsy = NULL;
2704 #endif /* TC_I960 */
2708 if (S_IS_COMMON (add_symbolP))
2709 add_number += S_GET_VALUE (add_symbolP);
2710 #endif /* TE_I386AIX */
2711 #endif /* OBJ_COFF */
2717 if (TC_FIX_ADJUSTABLE (fixP))
2718 add_number += S_GET_VALUE (add_symbolP);
2725 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2726 if (add_symbolP == 0)
2728 #ifndef BFD_ASSEMBLER
2729 fixP->fx_addsy = &abs_symbol;
2731 fixP->fx_addsy = section_symbol (absolute_section);
2733 symbol_mark_used_in_reloc (fixP->fx_addsy);
2740 #ifdef MD_APPLY_FIX3
2741 md_apply_fix3 (fixP, &add_number, this_segment_type);
2743 #ifdef BFD_ASSEMBLER
2744 md_apply_fix (fixP, &add_number);
2746 md_apply_fix (fixP, add_number);
2750 #ifndef TC_HANDLES_FX_DONE
2751 /* If the tc-* files haven't been converted, assume it's handling
2752 it the old way, where a null fx_addsy means that the fix has
2753 been applied completely, and no further work is needed. */
2754 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2759 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2761 if ((size_t) size < sizeof (valueT))
2766 mask--; /* Set all bits to one. */
2767 mask <<= size * 8 - (fixP->fx_signed ? 1 : 0);
2768 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2770 char buf[50], buf2[50];
2771 sprint_value (buf, fragP->fr_address + where);
2772 if (add_number > 1000)
2773 sprint_value (buf2, add_number);
2775 sprintf (buf2, "%ld", (long) add_number);
2776 as_bad_where (fixP->fx_file, fixP->fx_line,
2777 _("Value of %s too large for field of %d bytes at %s"),
2779 } /* Generic error checking. */
2781 #ifdef WARN_SIGNED_OVERFLOW_WORD
2782 /* Warn if a .word value is too large when treated as a signed
2783 number. We already know it is not too negative. This is to
2784 catch over-large switches generated by gcc on the 68k. */
2785 if (!flag_signed_overflow_ok
2787 && add_number > 0x7fff)
2788 as_bad_where (fixP->fx_file, fixP->fx_line,
2789 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2791 (unsigned long) (fragP->fr_address + where));
2793 } /* Not a bit fix. */
2795 #ifdef TC_VALIDATE_FIX
2796 skip: ATTRIBUTE_UNUSED_LABEL
2800 fprintf (stderr, "result:\n");
2803 } /* For each fixS in this segment. */
2805 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2806 return seg_reloc_count;
2809 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2812 number_to_chars_bigendian (buf, val, n)
2817 if ((size_t) n > sizeof (val) || n <= 0)
2821 buf[n] = val & 0xff;
2827 number_to_chars_littleendian (buf, val, n)
2832 if ((size_t) n > sizeof (val) || n <= 0)
2836 *buf++ = val & 0xff;
2842 write_print_statistics (file)
2845 fprintf (file, "fixups: %d\n", n_fixups);
2848 /* For debugging. */
2849 extern int indent_level;
2856 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2858 fprintf (stderr, " pcrel");
2859 if (fixp->fx_pcrel_adjust)
2860 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2861 if (fixp->fx_im_disp)
2864 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2866 fprintf (stderr, " im_disp");
2870 fprintf (stderr, " tcbit");
2872 fprintf (stderr, " done");
2873 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2874 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2875 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2876 #ifdef BFD_ASSEMBLER
2877 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2880 #ifdef NEED_FX_R_TYPE
2881 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2886 fprintf (stderr, "\n +<");
2887 print_symbol_value_1 (stderr, fixp->fx_addsy);
2888 fprintf (stderr, ">");
2892 fprintf (stderr, "\n -<");
2893 print_symbol_value_1 (stderr, fixp->fx_subsy);
2894 fprintf (stderr, ">");
2896 fprintf (stderr, "\n");
2897 #ifdef TC_FIX_DATA_PRINT
2898 TC_FIX_DATA_PRINT (stderr, fixp);