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 MD_PCREL_FROM_SECTION
56 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
59 #ifndef WORKING_DOT_WORD
60 extern CONST int md_short_jump_size;
61 extern CONST int md_long_jump_size;
64 int symbol_table_frozen;
65 void print_fixup PARAMS ((fixS *));
68 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
70 /* We generally attach relocs to frag chains. However, after we have
71 chained these all together into a segment, any relocs we add after
72 that must be attached to a segment. This will include relocs added
73 in md_estimate_size_for_relax, for example. */
74 static int frags_chained = 0;
80 struct frag *text_frag_root;
81 struct frag *data_frag_root;
82 struct frag *bss_frag_root;
84 struct frag *text_last_frag; /* Last frag in segment. */
85 struct frag *data_last_frag; /* Last frag in segment. */
86 static struct frag *bss_last_frag; /* Last frag in segment. */
90 static object_headers headers;
93 long string_byte_count;
94 char *next_object_file_charP; /* Tracks object file bytes. */
97 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
100 #endif /* BFD_ASSEMBLER */
105 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
106 symbolS *add, symbolS *sub,
107 offsetT offset, int pcrel,
108 bfd_reloc_code_real_type r_type));
110 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
111 symbolS *add, symbolS *sub,
112 offsetT offset, int pcrel,
115 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
116 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
118 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
119 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
120 static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
123 static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
124 static void cvt_frag_to_fill PARAMS ((segT, fragS *));
125 static void relax_and_size_seg PARAMS ((bfd *, asection *, PTR));
126 static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
127 static void write_relocs PARAMS ((bfd *, asection *, PTR));
128 static void write_contents PARAMS ((bfd *, asection *, PTR));
129 static void set_symtab PARAMS ((void));
131 #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
132 static void merge_data_into_text PARAMS ((void));
134 #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
135 static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
136 static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
137 static void relax_and_size_all_segments PARAMS ((void));
139 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
140 static void set_segment_vma PARAMS ((bfd *, asection *, PTR));
143 /* Create a fixS in obstack 'notes'. */
146 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
148 fragS *frag; /* Which frag? */
149 int where; /* Where in that frag? */
150 int size; /* 1, 2, or 4 usually. */
151 symbolS *add_symbol; /* X_add_symbol. */
152 symbolS *sub_symbol; /* X_op_symbol. */
153 offsetT offset; /* X_add_number. */
154 int pcrel; /* TRUE if PC-relative relocation. */
156 bfd_reloc_code_real_type r_type; /* Relocation type. */
158 int r_type; /* Relocation type. */
165 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
167 fixP->fx_frag = frag;
168 fixP->fx_where = where;
169 fixP->fx_size = size;
170 /* We've made fx_size a narrow field; check that it's wide enough. */
171 if (fixP->fx_size != size)
173 as_bad (_("field fx_size too small to hold %d"), size);
176 fixP->fx_addsy = add_symbol;
177 fixP->fx_subsy = sub_symbol;
178 fixP->fx_offset = offset;
179 fixP->fx_pcrel = pcrel;
181 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
182 fixP->fx_r_type = r_type;
184 fixP->fx_im_disp = 0;
185 fixP->fx_pcrel_adjust = 0;
186 fixP->fx_bit_fixP = 0;
187 fixP->fx_addnumber = 0;
190 fixP->fx_no_overflow = 0;
194 fixP->fx_cgen.insn = NULL;
195 fixP->fx_cgen.opinfo = 0;
199 TC_INIT_FIX_DATA (fixP);
202 as_where (&fixP->fx_file, &fixP->fx_line);
204 /* Usually, we want relocs sorted numerically, but while
205 comparing to older versions of gas that have relocs
206 reverse sorted, it is convenient to have this compile
207 time option. xoxorich. */
211 fixS **seg_fix_rootP = (frags_chained
212 ? &seg_info (now_seg)->fix_root
213 : &frchain_now->fix_root);
214 fixS **seg_fix_tailP = (frags_chained
215 ? &seg_info (now_seg)->fix_tail
216 : &frchain_now->fix_tail);
219 #ifdef REVERSE_SORT_RELOCS
221 fixP->fx_next = *seg_fix_rootP;
222 *seg_fix_rootP = fixP;
224 #else /* REVERSE_SORT_RELOCS */
226 fixP->fx_next = NULL;
229 (*seg_fix_tailP)->fx_next = fixP;
231 *seg_fix_rootP = fixP;
232 *seg_fix_tailP = fixP;
234 #endif /* REVERSE_SORT_RELOCS */
240 /* Create a fixup relative to a symbol (plus a constant). */
243 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
244 fragS *frag; /* Which frag? */
245 int where; /* Where in that frag? */
246 int size; /* 1, 2, or 4 usually. */
247 symbolS *add_symbol; /* X_add_symbol. */
248 offsetT offset; /* X_add_number. */
249 int pcrel; /* TRUE if PC-relative relocation. */
251 bfd_reloc_code_real_type r_type; /* Relocation type. */
253 int r_type; /* Relocation type. */
256 return fix_new_internal (frag, where, size, add_symbol,
257 (symbolS *) NULL, offset, pcrel, r_type);
260 /* Create a fixup for an expression. Currently we only support fixups
261 for difference expressions. That is itself more than most object
262 file formats support anyhow. */
265 fix_new_exp (frag, where, size, exp, pcrel, r_type)
266 fragS *frag; /* Which frag? */
267 int where; /* Where in that frag? */
268 int size; /* 1, 2, or 4 usually. */
269 expressionS *exp; /* Expression. */
270 int pcrel; /* TRUE if PC-relative relocation. */
272 bfd_reloc_code_real_type r_type; /* Relocation type. */
274 int r_type; /* Relocation type. */
287 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
288 the difference expression cannot immediately be reduced. */
290 symbolS *stmp = make_expr_symbol (exp);
292 exp->X_op = O_symbol;
293 exp->X_op_symbol = 0;
294 exp->X_add_symbol = stmp;
295 exp->X_add_number = 0;
297 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
301 add = exp->X_add_symbol;
302 off = exp->X_add_number;
304 #if defined(BFD_ASSEMBLER)
305 r_type = BFD_RELOC_RVA;
307 #if defined(TC_RVA_RELOC)
308 r_type = TC_RVA_RELOC;
310 as_fatal (_("rva not supported"));
316 sub = exp->X_add_symbol;
317 off = exp->X_add_number;
321 sub = exp->X_op_symbol;
324 add = exp->X_add_symbol;
327 off = exp->X_add_number;
331 add = make_expr_symbol (exp);
335 return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
338 /* Append a string onto another string, bumping the pointer along. */
340 append (charPP, fromP, length)
343 unsigned long length;
345 /* Don't trust memcpy() of 0 chars. */
349 memcpy (*charPP, fromP, length);
353 #ifndef BFD_ASSEMBLER
354 int section_alignment[SEG_MAXIMUM_ORDINAL];
357 /* This routine records the largest alignment seen for each segment.
358 If the beginning of the segment is aligned on the worst-case
359 boundary, all of the other alignments within it will work. At
360 least one object format really uses this info. */
363 record_alignment (seg, align)
364 /* Segment to which alignment pertains. */
366 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
370 if (seg == absolute_section)
373 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
374 bfd_set_section_alignment (stdoutput, seg, align);
376 if (align > section_alignment[(int) seg])
377 section_alignment[(int) seg] = align;
383 /* Reset the section indices after removing the gas created sections. */
386 renumber_sections (abfd, sec, countparg)
387 bfd *abfd ATTRIBUTE_UNUSED;
391 int *countp = (int *) countparg;
393 sec->index = *countp;
397 #endif /* defined (BFD_ASSEMBLER) */
399 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
402 chain_frchains_together_1 (section, frchp)
404 struct frchain *frchp;
406 fragS dummy, *prev_frag = &dummy;
408 fixS fix_dummy, *prev_fix = &fix_dummy;
411 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
413 prev_frag->fr_next = frchp->frch_root;
414 prev_frag = frchp->frch_last;
415 assert (prev_frag->fr_type != 0);
417 if (frchp->fix_root != (fixS *) NULL)
419 if (seg_info (section)->fix_root == (fixS *) NULL)
420 seg_info (section)->fix_root = frchp->fix_root;
421 prev_fix->fx_next = frchp->fix_root;
422 seg_info (section)->fix_tail = frchp->fix_tail;
423 prev_fix = frchp->fix_tail;
427 assert (prev_frag->fr_type != 0);
428 prev_frag->fr_next = 0;
437 chain_frchains_together (abfd, section, xxx)
438 bfd *abfd ATTRIBUTE_UNUSED;
440 PTR xxx ATTRIBUTE_UNUSED;
442 segment_info_type *info;
444 /* BFD may have introduced its own sections without using
445 subseg_new, so it is possible that seg_info is NULL. */
446 info = seg_info (section);
447 if (info != (segment_info_type *) NULL)
448 info->frchainP->frch_last
449 = chain_frchains_together_1 (section, info->frchainP);
451 /* Now that we've chained the frags together, we must add new fixups
452 to the segment, not to the frag chain. */
458 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
461 remove_subsegs (head, seg, root, last)
467 *root = head->frch_root;
468 *last = chain_frchains_together_1 (seg, head);
473 #if defined (BFD_ASSEMBLER) || !defined (BFD)
477 cvt_frag_to_fill (sec, fragP)
478 segT sec ATTRIBUTE_UNUSED;
482 cvt_frag_to_fill (headersP, sec, fragP)
483 object_headers *headersP;
488 switch (fragP->fr_type)
495 HANDLE_ALIGN (fragP);
497 know (fragP->fr_next != NULL);
498 fragP->fr_offset = (fragP->fr_next->fr_address
500 - fragP->fr_fix) / fragP->fr_var;
501 if (fragP->fr_offset < 0)
503 as_bad_where (fragP->fr_file, fragP->fr_line,
504 _("attempt to .org/.space backwards? (%ld)"),
505 (long) fragP->fr_offset);
507 fragP->fr_type = rs_fill;
515 valueT value = S_GET_VALUE (fragP->fr_symbol);
518 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
521 fragP->fr_fix += size;
522 fragP->fr_type = rs_fill;
524 fragP->fr_offset = 0;
525 fragP->fr_symbol = NULL;
530 eh_frame_convert_frag (fragP);
533 case rs_machine_dependent:
535 md_convert_frag (stdoutput, sec, fragP);
537 md_convert_frag (headersP, sec, fragP);
540 assert (fragP->fr_next == NULL
541 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
544 /* After md_convert_frag, we make the frag into a ".space 0".
545 md_convert_frag() should set up any fixSs and constants
550 #ifndef WORKING_DOT_WORD
553 struct broken_word *lie;
555 if (fragP->fr_subtype)
557 fragP->fr_fix += md_short_jump_size;
558 for (lie = (struct broken_word *) (fragP->fr_symbol);
559 lie && lie->dispfrag == fragP;
560 lie = lie->next_broken_word)
562 fragP->fr_fix += md_long_jump_size;
570 BAD_CASE (fragP->fr_type);
575 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
579 relax_and_size_seg (abfd, sec, xxx)
582 PTR xxx ATTRIBUTE_UNUSED;
586 segment_info_type *seginfo;
588 valueT size, newsize;
590 subseg_change (sec, 0);
592 flags = bfd_get_section_flags (abfd, sec);
594 seginfo = seg_info (sec);
595 if (seginfo && seginfo->frchainP)
597 relax_segment (seginfo->frchainP->frch_root, sec);
598 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
599 cvt_frag_to_fill (sec, fragp);
600 for (fragp = seginfo->frchainP->frch_root;
602 fragp = fragp->fr_next)
603 /* Walk to last elt. */
605 size = fragp->fr_address + fragp->fr_fix;
610 if (size > 0 && ! seginfo->bss)
611 flags |= SEC_HAS_CONTENTS;
613 /* @@ This is just an approximation. */
614 if (seginfo && seginfo->fix_root)
618 x = bfd_set_section_flags (abfd, sec, flags);
621 newsize = md_section_align (sec, size);
622 x = bfd_set_section_size (abfd, sec, newsize);
625 /* If the size had to be rounded up, add some padding in the last
627 assert (newsize >= size);
630 fragS *last = seginfo->frchainP->frch_last;
631 fragp = seginfo->frchainP->frch_root;
632 while (fragp->fr_next != last)
633 fragp = fragp->fr_next;
634 last->fr_address = size;
635 fragp->fr_offset += newsize - size;
638 #ifdef tc_frob_section
639 tc_frob_section (sec);
641 #ifdef obj_frob_section
642 obj_frob_section (sec);
648 dump_section_relocs (abfd, sec, stream_)
649 bfd *abfd ATTRIBUTE_UNUSED;
653 FILE *stream = (FILE *) stream_;
654 segment_info_type *seginfo = seg_info (sec);
655 fixS *fixp = seginfo->fix_root;
660 fprintf (stream, "sec %s relocs:\n", sec->name);
663 symbolS *s = fixp->fx_addsy;
665 fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
666 (int) fixp->fx_r_type);
668 fprintf (stream, "no sym\n");
671 print_symbol_value_1 (stream, s);
672 fprintf (stream, "\n");
674 fixp = fixp->fx_next;
678 #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
681 #ifndef EMIT_SECTION_SYMBOLS
682 #define EMIT_SECTION_SYMBOLS 1
686 adjust_reloc_syms (abfd, sec, xxx)
687 bfd *abfd ATTRIBUTE_UNUSED;
689 PTR xxx ATTRIBUTE_UNUSED;
691 segment_info_type *seginfo = seg_info (sec);
697 dump_section_relocs (abfd, sec, stderr);
699 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
703 else if (fixp->fx_addsy)
709 fprintf (stderr, "\n\nadjusting fixup:\n");
713 sym = fixp->fx_addsy;
715 /* All symbols should have already been resolved at this
716 point. It is possible to see unresolved expression
717 symbols, though, since they are not in the regular symbol
720 resolve_symbol_value (sym, 1);
722 if (fixp->fx_subsy != NULL)
723 resolve_symbol_value (fixp->fx_subsy, 1);
725 /* If this symbol is equated to an undefined symbol, convert
726 the fixup to being against that symbol. */
727 if (sym != NULL && symbol_equated_p (sym)
728 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
730 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
731 sym = symbol_get_value_expression (sym)->X_add_symbol;
732 fixp->fx_addsy = sym;
735 if (sym != NULL && symbol_mri_common_p (sym))
737 /* These symbols are handled specially in fixup_segment. */
741 symsec = S_GET_SEGMENT (sym);
746 if (bfd_is_abs_section (symsec))
748 /* The fixup_segment routine will not use this symbol in a
749 relocation unless TC_FORCE_RELOCATION returns 1. */
750 if (TC_FORCE_RELOCATION (fixp))
752 symbol_mark_used_in_reloc (fixp->fx_addsy);
753 #ifdef UNDEFINED_DIFFERENCE_OK
754 if (fixp->fx_subsy != NULL)
755 symbol_mark_used_in_reloc (fixp->fx_subsy);
761 /* If it's one of these sections, assume the symbol is
762 definitely going to be output. The code in
763 md_estimate_size_before_relax in tc-mips.c uses this test
764 as well, so if you change this code you should look at that
766 if (bfd_is_und_section (symsec)
767 || bfd_is_com_section (symsec))
769 symbol_mark_used_in_reloc (fixp->fx_addsy);
770 #ifdef UNDEFINED_DIFFERENCE_OK
771 /* We have the difference of an undefined symbol and some
772 other symbol. Make sure to mark the other symbol as used
773 in a relocation so that it will always be output. */
775 symbol_mark_used_in_reloc (fixp->fx_subsy);
780 /* Don't try to reduce relocs which refer to non-local symbols
781 in .linkonce sections. It can lead to confusion when a
782 debugging section refers to a .linkonce section. I hope
783 this will always be correct. */
784 if (symsec != sec && ! S_IS_LOCAL (sym))
790 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
795 /* The GNU toolchain uses an extension for ELF: a section
796 beginning with the magic string .gnu.linkonce is a
798 if (strncmp (segment_name (symsec), ".gnu.linkonce",
799 sizeof ".gnu.linkonce" - 1) == 0)
805 symbol_mark_used_in_reloc (fixp->fx_addsy);
806 #ifdef UNDEFINED_DIFFERENCE_OK
807 if (fixp->fx_subsy != NULL)
808 symbol_mark_used_in_reloc (fixp->fx_subsy);
814 /* Since we're reducing to section symbols, don't attempt to reduce
815 anything that's already using one. */
816 if (symbol_section_p (sym))
818 symbol_mark_used_in_reloc (fixp->fx_addsy);
823 /* We can never adjust a reloc against a weak symbol. If we
824 did, and the weak symbol was overridden by a real symbol
825 somewhere else, then our relocation would be pointing at
826 the wrong area of memory. */
829 symbol_mark_used_in_reloc (fixp->fx_addsy);
834 /* Is there some other reason we can't adjust this one? (E.g.,
835 call/bal links in i960-bout symbols.) */
836 #ifdef obj_fix_adjustable
837 if (! obj_fix_adjustable (fixp))
839 symbol_mark_used_in_reloc (fixp->fx_addsy);
844 /* Is there some other (target cpu dependent) reason we can't adjust
845 this one? (E.g. relocations involving function addresses on
847 #ifdef tc_fix_adjustable
848 if (! tc_fix_adjustable (fixp))
850 symbol_mark_used_in_reloc (fixp->fx_addsy);
855 /* If the section symbol isn't going to be output, the relocs
856 at least should still work. If not, figure out what to do
857 when we run into that case.
859 We refetch the segment when calling section_symbol, rather
860 than using symsec, because S_GET_VALUE may wind up changing
861 the section when it calls resolve_symbol_value. */
862 fixp->fx_offset += S_GET_VALUE (sym);
863 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
864 symbol_mark_used_in_reloc (fixp->fx_addsy);
866 fprintf (stderr, "\nadjusted fixup:\n");
873 #if 1 /* def RELOC_REQUIRES_SYMBOL */
876 /* There was no symbol required by this relocation. However,
877 BFD doesn't really handle relocations without symbols well.
878 (At least, the COFF support doesn't.) So for now we fake up
879 a local symbol in the absolute section. */
881 fixp->fx_addsy = section_symbol (absolute_section);
883 fixp->fx_addsy->sy_used_in_reloc = 1;
888 dump_section_relocs (abfd, sec, stderr);
892 write_relocs (abfd, sec, xxx)
895 PTR xxx ATTRIBUTE_UNUSED;
897 segment_info_type *seginfo = seg_info (sec);
904 /* If seginfo is NULL, we did not create this section; don't do
909 fixup_segment (seginfo->fix_root, sec);
912 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
915 #ifndef RELOC_EXPANSION_POSSIBLE
916 /* Set up reloc information as well. */
917 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
918 memset ((char *) relocs, 0, n * sizeof (arelent *));
921 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
924 bfd_reloc_status_type s;
933 /* If this is an undefined symbol which was equated to another
934 symbol, then use generate the reloc against the latter symbol
935 rather than the former. */
936 sym = fixp->fx_addsy;
937 while (symbol_equated_p (sym)
938 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
942 /* We must avoid looping, as that can occur with a badly
944 n = symbol_get_value_expression (sym)->X_add_symbol;
947 fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
950 fixp->fx_addsy = sym;
952 reloc = tc_gen_reloc (sec, fixp);
960 /* This test is triggered inappropriately for the SH. */
961 if (fixp->fx_where + fixp->fx_size
962 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
966 s = bfd_install_relocation (stdoutput, reloc,
967 fixp->fx_frag->fr_literal,
968 fixp->fx_frag->fr_address,
974 case bfd_reloc_overflow:
975 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
977 case bfd_reloc_outofrange:
978 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation out of range"));
981 as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
982 fixp->fx_file, fixp->fx_line, s);
987 n = n * MAX_RELOC_EXPANSION;
988 /* Set up reloc information as well. */
989 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
992 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
996 bfd_reloc_status_type s;
1006 /* If this is an undefined symbol which was equated to another
1007 symbol, then use generate the reloc against the latter symbol
1008 rather than the former. */
1009 sym = fixp->fx_addsy;
1010 while (symbol_equated_p (sym)
1011 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
1012 sym = symbol_get_value_expression (sym)->X_add_symbol;
1013 fixp->fx_addsy = sym;
1015 reloc = tc_gen_reloc (sec, fixp);
1017 for (j = 0; reloc[j]; j++)
1019 relocs[i++] = reloc[j];
1022 data = fixp->fx_frag->fr_literal + fixp->fx_where;
1023 if (fixp->fx_where + fixp->fx_size
1024 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1025 as_bad_where (fixp->fx_file, fixp->fx_line,
1026 _("internal error: fixup not contained within frag"));
1027 for (j = 0; reloc[j]; j++)
1029 s = bfd_install_relocation (stdoutput, reloc[j],
1030 fixp->fx_frag->fr_literal,
1031 fixp->fx_frag->fr_address,
1037 case bfd_reloc_overflow:
1038 as_bad_where (fixp->fx_file, fixp->fx_line,
1039 _("relocation overflow"));
1042 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
1043 fixp->fx_file, fixp->fx_line);
1054 sympp = bfd_get_outsymbols (stdoutput);
1055 nsyms = bfd_get_symcount (stdoutput);
1056 for (i = 0; i < n; i++)
1057 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1059 for (j = 0; j < nsyms; j++)
1060 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1069 bfd_set_reloc (stdoutput, sec, relocs, n);
1071 bfd_set_section_flags (abfd, sec,
1072 (bfd_get_section_flags (abfd, sec)
1073 & (flagword) ~SEC_RELOC));
1075 #ifdef SET_SECTION_RELOCS
1076 SET_SECTION_RELOCS (sec, relocs, n);
1084 fprintf (stderr, "relocs for sec %s\n", sec->name);
1085 for (i = 0; i < n; i++)
1088 s = *r->sym_ptr_ptr;
1089 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1090 i, r, r->address, s->name, r->addend);
1097 write_contents (abfd, sec, xxx)
1098 bfd *abfd ATTRIBUTE_UNUSED;
1100 PTR xxx ATTRIBUTE_UNUSED;
1102 segment_info_type *seginfo = seg_info (sec);
1103 unsigned long offset = 0;
1106 /* Write out the frags. */
1108 || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1111 for (f = seginfo->frchainP->frch_root;
1116 unsigned long fill_size;
1120 assert (f->fr_type == rs_fill);
1123 x = bfd_set_section_contents (stdoutput, sec,
1124 f->fr_literal, (file_ptr) offset,
1125 (bfd_size_type) f->fr_fix);
1128 bfd_perror (stdoutput->filename);
1129 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
1130 exit (EXIT_FAILURE);
1132 offset += f->fr_fix;
1134 fill_literal = f->fr_literal + f->fr_fix;
1135 fill_size = f->fr_var;
1136 count = f->fr_offset;
1137 assert (count >= 0);
1138 if (fill_size && count)
1141 if (fill_size > sizeof (buf))
1143 /* Do it the old way. Can this ever happen? */
1146 x = bfd_set_section_contents (stdoutput, sec,
1149 (bfd_size_type) fill_size);
1152 bfd_perror (stdoutput->filename);
1153 as_perror (_("FATAL: Can't write %s"),
1154 stdoutput->filename);
1155 exit (EXIT_FAILURE);
1157 offset += fill_size;
1162 /* Build a buffer full of fill objects and output it as
1163 often as necessary. This saves on the overhead of
1164 potentially lots of bfd_set_section_contents calls. */
1168 n_per_buf = sizeof (buf);
1169 memset (buf, *fill_literal, n_per_buf);
1174 n_per_buf = sizeof (buf) / fill_size;
1175 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1176 memcpy (bufp, fill_literal, fill_size);
1178 for (; count > 0; count -= n_per_buf)
1180 n_per_buf = n_per_buf > count ? count : n_per_buf;
1181 x = bfd_set_section_contents
1182 (stdoutput, sec, buf, (file_ptr) offset,
1183 (bfd_size_type) n_per_buf * fill_size);
1185 as_fatal (_("Cannot write to output file."));
1186 offset += n_per_buf * fill_size;
1194 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1196 merge_data_into_text ()
1198 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1199 seg_info (text_section)->frchainP->frch_last->fr_next =
1200 seg_info (data_section)->frchainP->frch_root;
1201 seg_info (text_section)->frchainP->frch_last =
1202 seg_info (data_section)->frchainP->frch_last;
1203 seg_info (data_section)->frchainP = 0;
1207 text_last_frag->fr_next = data_frag_root;
1208 text_last_frag = data_last_frag;
1209 data_last_frag = NULL;
1210 data_frag_root = NULL;
1213 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1214 tmp->fx_next = data_fix_root;
1215 text_fix_tail = data_fix_tail;
1218 text_fix_root = data_fix_root;
1219 data_fix_root = NULL;
1222 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1224 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1226 relax_and_size_all_segments ()
1230 relax_segment (text_frag_root, SEG_TEXT);
1231 relax_segment (data_frag_root, SEG_DATA);
1232 relax_segment (bss_frag_root, SEG_BSS);
1234 /* Now the addresses of frags are correct within the segment. */
1235 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1236 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1237 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1239 /* Join the 2 segments into 1 huge segment.
1240 To do this, re-compute every rn_address in the SEG_DATA frags.
1241 Then join the data frags after the text frags.
1243 Determine a_data [length of data segment]. */
1246 register relax_addressT slide;
1248 know ((text_last_frag->fr_type == rs_fill)
1249 && (text_last_frag->fr_offset == 0));
1251 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1252 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1253 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1255 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1256 /* For b.out: If the data section has a strict alignment
1257 requirement, its load address in the .o file will be
1258 rounded up from the size of the text section. These
1259 two values are *not* the same! Similarly for the bss
1261 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1264 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1265 fragP->fr_address += slide;
1267 know (text_last_frag != 0);
1268 text_last_frag->fr_next = data_frag_root;
1272 H_SET_DATA_SIZE (&headers, 0);
1276 /* See above comments on b.out data section address. */
1279 if (data_last_frag == 0)
1280 bss_vma = H_GET_TEXT_SIZE (&headers);
1282 bss_vma = data_last_frag->fr_address;
1283 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1284 bss_address_frag.fr_address = bss_vma;
1286 #else /* ! OBJ_BOUT */
1287 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1288 H_GET_DATA_SIZE (&headers));
1290 #endif /* ! OBJ_BOUT */
1292 /* Slide all the frags. */
1295 relax_addressT slide = bss_address_frag.fr_address;
1297 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1298 fragP->fr_address += slide;
1302 H_SET_BSS_SIZE (&headers,
1303 bss_last_frag->fr_address - bss_frag_root->fr_address);
1305 H_SET_BSS_SIZE (&headers, 0);
1307 #endif /* ! BFD_ASSEMBLER && ! BFD */
1309 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1311 #ifdef BFD_ASSEMBLER
1319 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1321 /* Count symbols. We can't rely on a count made by the loop in
1322 write_object_file, because *_frob_file may add a new symbol or
1325 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1332 asympp = (asymbol **) bfd_alloc (stdoutput,
1333 nsyms * sizeof (asymbol *));
1334 symp = symbol_rootP;
1335 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1337 asympp[i] = symbol_get_bfdsym (symp);
1338 symbol_mark_written (symp);
1343 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1344 assert (result == true);
1345 symbol_table_frozen = 1;
1349 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1351 set_segment_vma (abfd, sec, xxx)
1354 PTR xxx ATTRIBUTE_UNUSED;
1356 static bfd_vma addr = 0;
1358 bfd_set_section_vma (abfd, sec, addr);
1359 addr += bfd_section_size (abfd, sec);
1361 #endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE */
1363 /* Finish the subsegments. After every sub-segment, we fake an
1364 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1365 ".fill 0" because that is the kind of frag that requires least
1366 thought. ".align" frags like to have a following frag since that
1367 makes calculating their intended length trivial. */
1369 #ifndef SUB_SEGMENT_ALIGN
1370 #ifdef BFD_ASSEMBLER
1371 #define SUB_SEGMENT_ALIGN(SEG) (0)
1373 #define SUB_SEGMENT_ALIGN(SEG) (2)
1380 struct frchain *frchainP;
1382 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1384 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1386 /* This now gets called even if we had errors. In that case,
1387 any alignment is meaningless, and, moreover, will look weird
1388 if we are generating a listing. */
1389 frag_align (had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg),
1390 subseg_text_p (now_seg) ? NOP_OPCODE : 0,
1393 /* frag_align will have left a new frag.
1394 Use this last frag for an empty ".fill".
1396 For this segment ...
1397 Create a last frag. Do not leave a "being filled in frag". */
1398 frag_wane (frag_now);
1399 frag_now->fr_fix = 0;
1400 know (frag_now->fr_next == NULL);
1404 /* Write the object file. */
1407 write_object_file ()
1409 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1410 fragS *fragP; /* Track along all frags. */
1413 /* Do we really want to write it? */
1415 int n_warns, n_errs;
1416 n_warns = had_warnings ();
1417 n_errs = had_errors ();
1418 /* The -Z flag indicates that an object file should be generated,
1419 regardless of warnings and errors. */
1420 if (flag_always_generate_output)
1422 if (n_warns || n_errs)
1423 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
1424 n_errs, n_errs == 1 ? "" : "s",
1425 n_warns, n_warns == 1 ? "" : "s");
1430 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
1431 n_errs, n_errs == 1 ? "" : "s",
1432 n_warns, n_warns == 1 ? "" : "s");
1437 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1438 a routine to check for the definition of the procedure "_main",
1439 and if so -- fix it up so that it can be program entry point. */
1440 vms_check_for_main ();
1441 #endif /* OBJ_VMS */
1443 /* From now on, we don't care about sub-segments. Build one frag chain
1444 for each segment. Linked thru fr_next. */
1446 #ifdef BFD_ASSEMBLER
1447 /* Remove the sections created by gas for its own purposes. */
1449 asection **seclist, *sec;
1452 seclist = &stdoutput->sections;
1453 while (seclist && *seclist)
1456 while (sec == reg_section || sec == expr_section)
1460 stdoutput->section_count--;
1465 seclist = &(*seclist)->next;
1468 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1471 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1473 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1474 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1475 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1478 /* We have two segments. If user gave -R flag, then we must put the
1479 data frags into the text segment. Do this before relaxing so
1480 we know to take advantage of -R and make shorter addresses. */
1481 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1482 if (flag_readonly_data_in_text)
1484 merge_data_into_text ();
1488 #ifdef BFD_ASSEMBLER
1489 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1491 relax_and_size_all_segments ();
1492 #endif /* BFD_ASSEMBLER */
1494 #if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
1495 /* Now that the segments have their final sizes, run through the
1496 sections and set their vma and lma. !BFD gas sets them, and BFD gas
1497 should too. Currently, only DJGPP uses this code, but other
1498 COFF targets may need to execute this too. */
1499 bfd_map_over_sections (stdoutput, set_segment_vma, (char *) 0);
1502 #ifndef BFD_ASSEMBLER
1503 /* Crawl the symbol chain.
1505 For each symbol whose value depends on a frag, take the address of
1506 that frag and subsume it into the value of the symbol.
1507 After this, there is just one way to lookup a symbol value.
1508 Values are left in their final state for object file emission.
1509 We adjust the values of 'L' local symbols, even if we do
1510 not intend to emit them to the object file, because their values
1511 are needed for fix-ups.
1513 Unless we saw a -L flag, remove all symbols that begin with 'L'
1514 from the symbol chain. (They are still pointed to by the fixes.)
1516 Count the remaining symbols.
1517 Assign a symbol number to each symbol.
1518 Count the number of string-table chars we will emit.
1519 Put this info into the headers as appropriate. */
1520 know (zero_address_frag.fr_address == 0);
1521 string_byte_count = sizeof (string_byte_count);
1523 obj_crawl_symbol_chain (&headers);
1525 if (string_byte_count == sizeof (string_byte_count))
1526 string_byte_count = 0;
1528 H_SET_STRING_SIZE (&headers, string_byte_count);
1530 /* Addresses of frags now reflect addresses we use in the object file.
1531 Symbol values are correct.
1532 Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1533 Also converting any machine-dependent frags using md_convert_frag(); */
1534 subseg_change (SEG_TEXT, 0);
1536 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1538 /* At this point we have linked all the frags into a single
1539 chain. However, cvt_frag_to_fill may call md_convert_frag
1540 which may call fix_new. We need to ensure that fix_new adds
1541 the fixup to the right section. */
1542 if (fragP == data_frag_root)
1543 subseg_change (SEG_DATA, 0);
1545 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1547 /* Some assert macros don't work with # directives mixed in. */
1549 if (!(fragP->fr_next == NULL
1551 || fragP->fr_next == data_frag_root
1553 || ((fragP->fr_next->fr_address - fragP->fr_address)
1554 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1558 #endif /* ! BFD_ASSEMBLER */
1560 #ifndef WORKING_DOT_WORD
1562 struct broken_word *lie;
1563 struct broken_word **prevP;
1565 prevP = &broken_words;
1566 for (lie = broken_words; lie; lie = lie->next_broken_word)
1571 subseg_change (lie->seg, lie->subseg);
1572 exp.X_op = O_subtract;
1573 exp.X_add_symbol = lie->add;
1574 exp.X_op_symbol = lie->sub;
1575 exp.X_add_number = lie->addnum;
1576 #ifdef BFD_ASSEMBLER
1577 #ifdef TC_CONS_FIX_NEW
1578 TC_CONS_FIX_NEW (lie->frag,
1579 lie->word_goes_here - lie->frag->fr_literal,
1582 fix_new_exp (lie->frag,
1583 lie->word_goes_here - lie->frag->fr_literal,
1584 2, &exp, 0, BFD_RELOC_16);
1587 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1588 fix_new_exp (lie->frag,
1589 lie->word_goes_here - lie->frag->fr_literal,
1590 2, &exp, 0, NO_RELOC);
1593 fix_new_ns32k_exp (lie->frag,
1594 lie->word_goes_here - lie->frag->fr_literal,
1595 2, &exp, 0, 0, 2, 0, 0);
1597 fix_new_exp (lie->frag,
1598 lie->word_goes_here - lie->frag->fr_literal,
1600 #endif /* TC_NS32K */
1601 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1602 #endif /* BFD_ASSEMBLER */
1603 *prevP = lie->next_broken_word;
1606 prevP = &(lie->next_broken_word);
1608 for (lie = broken_words; lie;)
1610 struct broken_word *untruth;
1612 addressT table_addr;
1613 addressT from_addr, to_addr;
1616 subseg_change (lie->seg, lie->subseg);
1617 fragP = lie->dispfrag;
1619 /* Find out how many broken_words go here. */
1622 untruth && untruth->dispfrag == fragP;
1623 untruth = untruth->next_broken_word)
1624 if (untruth->added == 1)
1627 table_ptr = lie->dispfrag->fr_opcode;
1628 table_addr = (lie->dispfrag->fr_address
1629 + (table_ptr - lie->dispfrag->fr_literal));
1630 /* Create the jump around the long jumps. This is a short
1631 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1632 from_addr = table_addr;
1633 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1634 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1636 table_ptr += md_short_jump_size;
1637 table_addr += md_short_jump_size;
1640 lie && lie->dispfrag == fragP;
1641 m++, lie = lie->next_broken_word)
1643 if (lie->added == 2)
1645 /* Patch the jump table. */
1646 /* This is the offset from ??? to table_ptr+0. */
1647 to_addr = table_addr - S_GET_VALUE (lie->sub);
1648 #ifdef BFD_ASSEMBLER
1649 to_addr -= symbol_get_frag (lie->sub)->fr_address;
1651 #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1652 TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1654 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1655 for (untruth = lie->next_broken_word;
1656 untruth && untruth->dispfrag == fragP;
1657 untruth = untruth->next_broken_word)
1659 if (untruth->use_jump == lie)
1660 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1663 /* Install the long jump. */
1664 /* This is a long jump from table_ptr+0 to the final target. */
1665 from_addr = table_addr;
1666 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1667 #ifdef BFD_ASSEMBLER
1668 to_addr += symbol_get_frag (lie->add)->fr_address;
1670 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1672 table_ptr += md_long_jump_size;
1673 table_addr += md_long_jump_size;
1677 #endif /* not WORKING_DOT_WORD */
1679 #ifndef BFD_ASSEMBLER
1682 char *the_object_file;
1683 long object_file_size;
1684 /* Scan every FixS performing fixups. We had to wait until now to
1685 do this because md_convert_frag() may have made some fixSs. */
1688 subseg_change (SEG_TEXT, 0);
1689 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1690 subseg_change (SEG_DATA, 0);
1691 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1692 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1694 /* FIXME: Move this stuff into the pre-write-hook. */
1695 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1696 H_SET_ENTRY_POINT (&headers, 0);
1698 obj_pre_write_hook (&headers); /* Extra coff stuff. */
1700 object_file_size = H_GET_FILE_SIZE (&headers);
1701 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1703 output_file_create (out_file_name);
1705 obj_header_append (&next_object_file_charP, &headers);
1707 know ((next_object_file_charP - the_object_file)
1708 == H_GET_HEADER_SIZE (&headers));
1711 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1713 register long count;
1714 register char *fill_literal;
1715 register long fill_size;
1718 know (fragP->fr_type == rs_fill);
1719 append (&next_object_file_charP, fragP->fr_literal,
1720 (unsigned long) fragP->fr_fix);
1721 fill_literal = fragP->fr_literal + fragP->fr_fix;
1722 fill_size = fragP->fr_var;
1723 know (fragP->fr_offset >= 0);
1725 for (count = fragP->fr_offset; count; count--)
1726 append (&next_object_file_charP, fill_literal,
1727 (unsigned long) fill_size);
1730 know ((next_object_file_charP - the_object_file)
1731 == (H_GET_HEADER_SIZE (&headers)
1732 + H_GET_TEXT_SIZE (&headers)
1733 + H_GET_DATA_SIZE (&headers)));
1735 /* Emit relocations. */
1736 obj_emit_relocations (&next_object_file_charP, text_fix_root,
1737 (relax_addressT) 0);
1738 know ((next_object_file_charP - the_object_file)
1739 == (H_GET_HEADER_SIZE (&headers)
1740 + H_GET_TEXT_SIZE (&headers)
1741 + H_GET_DATA_SIZE (&headers)
1742 + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1744 /* Make addresses in data relocation directives relative to beginning of
1745 first data fragment, not end of last text fragment: alignment of the
1746 start of the data segment may place a gap between the segments. */
1747 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1748 data0_frchainP->frch_root->fr_address);
1750 obj_emit_relocations (&next_object_file_charP, data_fix_root,
1751 text_last_frag->fr_address);
1752 #endif /* TC_I960 */
1754 know ((next_object_file_charP - the_object_file)
1755 == (H_GET_HEADER_SIZE (&headers)
1756 + H_GET_TEXT_SIZE (&headers)
1757 + H_GET_DATA_SIZE (&headers)
1758 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1759 + H_GET_DATA_RELOCATION_SIZE (&headers)));
1761 /* Emit line number entries. */
1762 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1763 know ((next_object_file_charP - the_object_file)
1764 == (H_GET_HEADER_SIZE (&headers)
1765 + H_GET_TEXT_SIZE (&headers)
1766 + H_GET_DATA_SIZE (&headers)
1767 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1768 + H_GET_DATA_RELOCATION_SIZE (&headers)
1769 + H_GET_LINENO_SIZE (&headers)));
1772 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1773 know ((next_object_file_charP - the_object_file)
1774 == (H_GET_HEADER_SIZE (&headers)
1775 + H_GET_TEXT_SIZE (&headers)
1776 + H_GET_DATA_SIZE (&headers)
1777 + H_GET_TEXT_RELOCATION_SIZE (&headers)
1778 + H_GET_DATA_RELOCATION_SIZE (&headers)
1779 + H_GET_LINENO_SIZE (&headers)
1780 + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1783 if (string_byte_count > 0)
1784 obj_emit_strings (&next_object_file_charP);
1787 bfd_seek (stdoutput, 0, 0);
1788 bfd_write (the_object_file, 1, object_file_size, stdoutput);
1791 /* Write the data to the file. */
1792 output_file_append (the_object_file, object_file_size, out_file_name);
1793 free (the_object_file);
1797 /* Now do the VMS-dependent part of writing the object file. */
1798 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1799 H_GET_DATA_SIZE (&headers),
1800 H_GET_BSS_SIZE (&headers),
1801 text_frag_root, data_frag_root);
1802 #endif /* OBJ_VMS */
1803 #else /* BFD_ASSEMBLER */
1805 /* Resolve symbol values. This needs to be done before processing
1811 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1812 resolve_symbol_value (symp, 1);
1814 resolve_local_symbol_values ();
1818 #ifdef tc_frob_file_before_adjust
1819 tc_frob_file_before_adjust ();
1821 #ifdef obj_frob_file_before_adjust
1822 obj_frob_file_before_adjust ();
1825 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1827 /* Set up symbol table, and write it out. */
1832 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1837 if (symbol_mri_common_p (symp))
1839 if (S_IS_EXTERNAL (symp))
1840 as_bad (_("%s: global symbols not supported in common sections"),
1842 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1846 name = S_GET_NAME (symp);
1850 decode_local_label_name ((char *) S_GET_NAME (symp));
1851 /* They only differ if `name' is a fb or dollar local
1853 if (name2 != name && ! S_IS_DEFINED (symp))
1854 as_bad (_("local label %s is not defined"), name2);
1857 /* Do it again, because adjust_reloc_syms might introduce
1858 more symbols. They'll probably only be section symbols,
1859 but they'll still need to have the values computed. */
1860 resolve_symbol_value (symp, 1);
1862 /* Skip symbols which were equated to undefined or common
1864 if (symbol_equated_p (symp)
1865 && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
1867 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1871 /* So far, common symbols have been treated like undefined symbols.
1872 Put them in the common section now. */
1873 if (S_IS_DEFINED (symp) == 0
1874 && S_GET_VALUE (symp) != 0)
1875 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1877 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1878 S_GET_NAME (symp), symp,
1880 symbol_get_bfdsym (symp)->flags,
1881 segment_name (S_GET_SEGMENT (symp)));
1884 #ifdef obj_frob_symbol
1885 obj_frob_symbol (symp, punt);
1887 #ifdef tc_frob_symbol
1888 if (! punt || symbol_used_in_reloc_p (symp))
1889 tc_frob_symbol (symp, punt);
1892 /* If we don't want to keep this symbol, splice it out of
1893 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1894 want section symbols. Otherwise, we skip local symbols
1895 and symbols that the frob_symbol macros told us to punt,
1896 but we keep such symbols if they are used in relocs. */
1897 if ((! EMIT_SECTION_SYMBOLS
1898 && symbol_section_p (symp))
1899 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1900 opposites. Sometimes the former checks flags and the
1901 latter examines the name... */
1902 || (!S_IS_EXTERN (symp)
1903 && (S_IS_LOCAL (symp) || punt)
1904 && ! symbol_used_in_reloc_p (symp)))
1906 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1908 /* After symbol_remove, symbol_next(symp) still returns
1909 the one that came after it in the chain. So we don't
1910 need to do any extra cleanup work here. */
1914 /* Make sure we really got a value for the symbol. */
1915 if (! symbol_resolved_p (symp))
1917 as_bad (_("can't resolve value for symbol \"%s\""),
1919 symbol_mark_resolved (symp);
1922 /* Set the value into the BFD symbol. Up til now the value
1923 has only been kept in the gas symbolS struct. */
1924 symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1930 /* Now do any format-specific adjustments to the symbol table, such
1931 as adding file symbols. */
1932 #ifdef tc_adjust_symtab
1933 tc_adjust_symtab ();
1935 #ifdef obj_adjust_symtab
1936 obj_adjust_symtab ();
1939 /* Now that all the sizes are known, and contents correct, we can
1940 start writing to the file. */
1943 /* If *_frob_file changes the symbol value at this point, it is
1944 responsible for moving the changed value into symp->bsym->value
1945 as well. Hopefully all symbol value changing can be done in
1950 #ifdef obj_frob_file
1954 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1956 #ifdef tc_frob_file_after_relocs
1957 tc_frob_file_after_relocs ();
1959 #ifdef obj_frob_file_after_relocs
1960 obj_frob_file_after_relocs ();
1963 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1964 #endif /* BFD_ASSEMBLER */
1968 #ifdef TC_GENERIC_RELAX_TABLE
1970 static int is_dnrange PARAMS ((fragS *, fragS *));
1972 /* Subroutines of relax_segment. */
1978 for (; f1; f1 = f1->fr_next)
1979 if (f1->fr_next == f2)
1984 /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1987 relax_frag (fragP, stretch)
1991 const relax_typeS *this_type;
1992 const relax_typeS *start_type;
1993 relax_substateT next_state;
1994 relax_substateT this_state;
1995 long aim, target, growth;
1996 symbolS *symbolP = fragP->fr_symbol;
1997 long offset = fragP->fr_offset;
1998 /* Recompute was_address by undoing "+= stretch" done by relax_segment. */
1999 unsigned long was_address = fragP->fr_address - stretch;
2000 unsigned long address = fragP->fr_address;
2001 const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
2003 this_state = fragP->fr_subtype;
2004 start_type = this_type = table + this_state;
2009 #ifndef DIFF_EXPR_OK
2010 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2011 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2012 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2013 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2014 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2016 know (symbolP->sy_frag);
2018 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2019 || symbolP->sy_frag == &zero_address_frag);
2020 target += S_GET_VALUE (symbolP) + symbol_get_frag (symbolP)->fr_address;
2022 /* If frag has yet to be reached on this pass,
2023 assume it will move by STRETCH just as we did.
2024 If this is not so, it will be because some frag
2025 between grows, and that will force another pass.
2027 Beware zero-length frags.
2029 There should be a faster way to do this. */
2031 if (symbol_get_frag (symbolP)->fr_address >= was_address
2032 && is_dnrange (fragP, symbol_get_frag (symbolP)))
2038 aim = target - address - fragP->fr_fix;
2039 #ifdef TC_PCREL_ADJUST
2040 /* Currently only the ns32k family needs this. */
2041 aim += TC_PCREL_ADJUST (fragP);
2043 /* This machine doesn't want to use pcrel_adjust.
2044 In that case, pcrel_adjust should be zero. */
2046 assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2049 #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2050 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2055 /* Look backwards. */
2056 for (next_state = this_type->rlx_more; next_state;)
2057 if (aim >= this_type->rlx_backward)
2061 /* Grow to next state. */
2062 this_state = next_state;
2063 this_type = table + this_state;
2064 next_state = this_type->rlx_more;
2069 /* Look forwards. */
2070 for (next_state = this_type->rlx_more; next_state;)
2071 if (aim <= this_type->rlx_forward)
2075 /* Grow to next state. */
2076 this_state = next_state;
2077 this_type = table + this_state;
2078 next_state = this_type->rlx_more;
2082 growth = this_type->rlx_length - start_type->rlx_length;
2084 fragP->fr_subtype = this_state;
2088 #endif /* defined (TC_GENERIC_RELAX_TABLE) */
2090 /* Relax_align. Advance location counter to next address that has 'alignment'
2091 lowest order bits all 0s, return size of adjustment made. */
2092 static relax_addressT
2093 relax_align (address, alignment)
2094 register relax_addressT address; /* Address now. */
2095 register int alignment; /* Alignment (binary). */
2097 relax_addressT mask;
2098 relax_addressT new_address;
2100 mask = ~((~0) << alignment);
2101 new_address = (address + mask) & (~mask);
2102 #ifdef LINKER_RELAXING_SHRINKS_ONLY
2104 /* We must provide lots of padding, so the linker can discard it
2105 when needed. The linker will not add extra space, ever. */
2106 new_address += (1 << alignment);
2108 return (new_address - address);
2111 /* Now we have a segment, not a crowd of sub-segments, we can make
2116 After this, all frags in this segment have addresses that are correct
2117 within the segment. Since segments live in different file addresses,
2118 these frag addresses may not be the same as final object-file
2122 relax_segment (segment_frag_root, segment)
2123 struct frag *segment_frag_root;
2126 register struct frag *fragP;
2127 register relax_addressT address;
2128 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2129 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
2131 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2132 subseg_change (segment, 0);
2134 /* For each frag in segment: count and store (a 1st guess of)
2137 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2139 fragP->fr_address = address;
2140 address += fragP->fr_fix;
2142 switch (fragP->fr_type)
2145 address += fragP->fr_offset * fragP->fr_var;
2151 addressT offset = relax_align (address, (int) fragP->fr_offset);
2153 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2156 if (offset % fragP->fr_var != 0)
2158 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
2159 (unsigned long) offset, (long) fragP->fr_var);
2160 offset -= (offset % fragP->fr_var);
2169 /* Assume .org is nugatory. It will grow with 1st relax. */
2172 case rs_machine_dependent:
2173 address += md_estimate_size_before_relax (fragP, segment);
2176 #ifndef WORKING_DOT_WORD
2177 /* Broken words don't concern us yet. */
2178 case rs_broken_word:
2183 /* Initial guess is always 1; doing otherwise can result in
2184 stable solutions that are larger than the minimum. */
2185 address += fragP->fr_offset = 1;
2189 address += eh_frame_estimate_size_before_relax (fragP);
2193 BAD_CASE (fragP->fr_type);
2200 long stretch; /* May be any size, 0 or negative. */
2201 /* Cumulative number of addresses we have relaxed this pass.
2202 We may have relaxed more than one address. */
2203 long stretched; /* Have we stretched on this pass? */
2204 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2205 grew, and another shrank. If a branch instruction doesn't fit anymore,
2206 we could be scrod. */
2210 stretch = stretched = 0;
2212 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2215 addressT was_address;
2219 was_address = fragP->fr_address;
2220 address = fragP->fr_address += stretch;
2221 symbolP = fragP->fr_symbol;
2222 offset = fragP->fr_offset;
2224 switch (fragP->fr_type)
2226 case rs_fill: /* .fill never relaxes. */
2230 #ifndef WORKING_DOT_WORD
2231 /* JF: This is RMS's idea. I do *NOT* want to be blamed
2232 for it I do not want to write it. I do not want to have
2233 anything to do with it. This is not the proper way to
2234 implement this misfeature. */
2235 case rs_broken_word:
2237 struct broken_word *lie;
2238 struct broken_word *untruth;
2240 /* Yes this is ugly (storing the broken_word pointer
2241 in the symbol slot). Still, this whole chunk of
2242 code is ugly, and I don't feel like doing anything
2243 about it. Think of it as stubbornness in action. */
2245 for (lie = (struct broken_word *) (fragP->fr_symbol);
2246 lie && lie->dispfrag == fragP;
2247 lie = lie->next_broken_word)
2253 offset = (symbol_get_frag (lie->add)->fr_address
2254 + S_GET_VALUE (lie->add)
2256 - (symbol_get_frag (lie->sub)->fr_address
2257 + S_GET_VALUE (lie->sub)));
2258 if (offset <= -32768 || offset >= 32767)
2260 if (flag_warn_displacement)
2263 sprint_value (buf, (addressT) lie->addnum);
2264 as_warn (_(".word %s-%s+%s didn't fit"),
2265 S_GET_NAME (lie->add),
2266 S_GET_NAME (lie->sub),
2270 if (fragP->fr_subtype == 0)
2272 fragP->fr_subtype++;
2273 growth += md_short_jump_size;
2275 for (untruth = lie->next_broken_word;
2276 untruth && untruth->dispfrag == lie->dispfrag;
2277 untruth = untruth->next_broken_word)
2278 if ((symbol_get_frag (untruth->add)
2279 == symbol_get_frag (lie->add))
2280 && (S_GET_VALUE (untruth->add)
2281 == S_GET_VALUE (lie->add)))
2284 untruth->use_jump = lie;
2286 growth += md_long_jump_size;
2291 } /* case rs_broken_word */
2296 addressT oldoff, newoff;
2298 oldoff = relax_align (was_address + fragP->fr_fix,
2300 newoff = relax_align (address + fragP->fr_fix,
2303 if (fragP->fr_subtype != 0)
2305 if (oldoff > fragP->fr_subtype)
2307 if (newoff > fragP->fr_subtype)
2311 growth = newoff - oldoff;
2317 long target = offset;
2322 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2323 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2324 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2325 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2326 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2327 know (symbolP->sy_frag);
2328 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2329 || (symbolP->sy_frag == &zero_address_frag));
2331 target += (S_GET_VALUE (symbolP)
2332 + symbol_get_frag (symbolP)->fr_address);
2333 } /* if we have a symbol */
2335 know (fragP->fr_next);
2336 after = fragP->fr_next->fr_address;
2337 growth = target - after;
2340 /* Growth may be negative, but variable part of frag
2341 cannot have fewer than 0 chars. That is, we can't
2343 as_bad_where (fragP->fr_file, fragP->fr_line,
2344 _("attempt to .org backwards ignored"));
2346 /* We've issued an error message. Change the
2347 frag to avoid cascading errors. */
2348 fragP->fr_type = rs_align;
2349 fragP->fr_subtype = 0;
2350 fragP->fr_offset = 0;
2351 fragP->fr_fix = after - address;
2355 /* This is an absolute growth factor */
2363 growth = S_GET_VALUE (symbolP);
2364 if (symbol_get_frag (symbolP) != &zero_address_frag
2365 || S_IS_COMMON (symbolP)
2366 || ! S_IS_DEFINED (symbolP))
2367 as_bad_where (fragP->fr_file, fragP->fr_line,
2368 _(".space specifies non-absolute value"));
2369 fragP->fr_symbol = 0;
2372 as_warn (_(".space or .fill with negative value, ignored"));
2380 case rs_machine_dependent:
2381 #ifdef md_relax_frag
2382 growth = md_relax_frag (fragP, stretch);
2384 #ifdef TC_GENERIC_RELAX_TABLE
2385 /* The default way to relax a frag is to look through
2386 TC_GENERIC_RELAX_TABLE. */
2387 growth = relax_frag (fragP, stretch);
2388 #endif /* TC_GENERIC_RELAX_TABLE */
2397 value = resolve_symbol_value (fragP->fr_symbol, 0);
2398 size = sizeof_leb128 (value, fragP->fr_subtype);
2399 growth = size - fragP->fr_offset;
2400 fragP->fr_offset = size;
2405 growth = eh_frame_relax_frag (fragP);
2409 BAD_CASE (fragP->fr_type);
2417 } /* For each frag in the segment. */
2419 while (stretched); /* Until nothing further to relax. */
2422 /* We now have valid fr_address'es for each frag. */
2424 /* All fr_address's are correct, relative to their own segment.
2425 We have made all the fixS we will ever make. */
2428 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2430 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2431 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2436 Go through all the fixS's in a segment and see which ones can be
2437 handled now. (These consist of fixS where we have since discovered
2438 the value of a symbol, or the address of the frag involved.)
2439 For each one, call md_apply_fix to put the fix into the frag data.
2441 Result is a count of how many relocation structs will be needed to
2442 handle the remaining fixS's that we couldn't completely handle here.
2443 These will be output later by emit_relocations(). */
2446 fixup_segment (fixP, this_segment_type)
2447 register fixS *fixP;
2448 segT this_segment_type; /* N_TYPE bits for segment. */
2450 long seg_reloc_count = 0;
2451 symbolS *add_symbolP;
2452 symbolS *sub_symbolP;
2459 segT add_symbol_segment = absolute_section;
2461 /* If the linker is doing the relaxing, we must not do any fixups.
2463 Well, strictly speaking that's not true -- we could do any that are
2464 PC-relative and don't cross regions that could change size. And for the
2465 i960 (the only machine for which we've got a relaxing linker right now),
2466 we might be able to turn callx/callj into bal anyways in cases where we
2467 know the maximum displacement. */
2468 if (linkrelax && TC_LINKRELAX_FIXUP (this_segment_type))
2470 for (; fixP; fixP = fixP->fx_next)
2472 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2473 return seg_reloc_count;
2476 for (; fixP; fixP = fixP->fx_next)
2479 fprintf (stderr, "\nprocessing fixup:\n");
2483 fragP = fixP->fx_frag;
2485 where = fixP->fx_where;
2486 place = fragP->fr_literal + where;
2487 size = fixP->fx_size;
2488 add_symbolP = fixP->fx_addsy;
2489 #ifdef TC_VALIDATE_FIX
2490 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2492 sub_symbolP = fixP->fx_subsy;
2493 add_number = fixP->fx_offset;
2494 pcrel = fixP->fx_pcrel;
2497 if (add_symbolP != NULL
2498 && symbol_mri_common_p (add_symbolP))
2500 know (add_symbolP->sy_value.X_op == O_symbol);
2501 add_number += S_GET_VALUE (add_symbolP);
2502 fixP->fx_offset = add_number;
2503 add_symbolP = fixP->fx_addsy =
2504 symbol_get_value_expression (add_symbolP)->X_add_symbol;
2508 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2512 resolve_symbol_value (sub_symbolP, 1);
2513 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2515 if (add_symbolP != NULL)
2517 add_number += S_GET_VALUE (add_symbolP);
2519 fixP->fx_addsy = NULL;
2522 /* It's just -sym. */
2523 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2525 add_number -= S_GET_VALUE (sub_symbolP);
2526 fixP->fx_subsy = NULL;
2529 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2531 /* Should try converting to a constant. */
2536 as_bad_where (fixP->fx_file, fixP->fx_line,
2537 _("Negative of non-absolute symbol %s"),
2538 S_GET_NAME (sub_symbolP));
2540 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2541 && SEG_NORMAL (add_symbol_segment))
2543 /* Difference of 2 symbols from same segment.
2544 Can't make difference of 2 undefineds: 'value' means
2545 something different for N_UNDF. */
2547 /* Makes no sense to use the difference of 2 arbitrary symbols
2548 as the target of a call instruction. */
2550 as_bad_where (fixP->fx_file, fixP->fx_line,
2551 _("callj to difference of 2 symbols"));
2552 #endif /* TC_I960 */
2553 add_number += S_GET_VALUE (add_symbolP) -
2554 S_GET_VALUE (sub_symbolP);
2557 pcrel = 0; /* No further pcrel processing. */
2559 /* Let the target machine make the final determination
2560 as to whether or not a relocation will be needed to
2561 handle this fixup. */
2562 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2565 fixP->fx_addsy = NULL;
2566 fixP->fx_subsy = NULL;
2571 /* Different segments in subtraction. */
2572 know (!(S_IS_EXTERNAL (sub_symbolP)
2573 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2575 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2576 add_number -= S_GET_VALUE (sub_symbolP);
2579 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2581 /* Do this even if it's already described as
2582 pc-relative. For example, on the m68k, an
2583 operand of "pc@(foo-.-2)" should address
2584 "foo" in a pc-relative mode. */
2589 /* Make it pc-relative. */
2590 add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
2591 - S_GET_VALUE (sub_symbolP));
2598 #ifdef UNDEFINED_DIFFERENCE_OK
2599 /* The PA needs this for PIC code generation. We basically
2600 don't want to do anything if we have the difference of two
2601 symbols at this point. */
2604 /* Leave it alone. */
2607 #ifdef BFD_ASSEMBLER
2608 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2609 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2611 /* Leave it alone. */
2617 sprint_value (buf, fragP->fr_address + where);
2618 as_bad_where (fixP->fx_file, fixP->fx_line,
2619 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
2620 S_GET_NAME (add_symbolP),
2621 segment_name (S_GET_SEGMENT (add_symbolP)),
2622 S_GET_NAME (sub_symbolP),
2623 segment_name (S_GET_SEGMENT (sub_symbolP)),
2631 if (add_symbol_segment == this_segment_type && pcrel && !plt
2632 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2634 /* This fixup was made when the symbol's segment was
2635 SEG_UNKNOWN, but it is now in the local segment.
2636 So we know how to do the address without relocation. */
2638 /* reloc_callj() may replace a 'call' with a 'calls' or a
2639 'bal', in which cases it modifies *fixP as appropriate.
2640 In the case of a 'calls', no further work is required,
2641 and *fixP has been set up to make the rest of the code
2644 #endif /* TC_I960 */
2646 add_number += S_GET_VALUE (add_symbolP);
2647 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2648 /* Lie. Don't want further pcrel processing. */
2651 /* Let the target machine make the final determination
2652 as to whether or not a relocation will be needed to
2653 handle this fixup. */
2654 if (!TC_FORCE_RELOCATION (fixP))
2657 fixP->fx_addsy = NULL;
2662 if (add_symbol_segment == absolute_section
2666 /* See comment about reloc_callj() above. */
2668 #endif /* TC_I960 */
2669 add_number += S_GET_VALUE (add_symbolP);
2671 /* Let the target machine make the final determination
2672 as to whether or not a relocation will be needed to
2673 handle this fixup. */
2675 if (!TC_FORCE_RELOCATION (fixP))
2677 fixP->fx_addsy = NULL;
2681 else if (add_symbol_segment == undefined_section
2682 #ifdef BFD_ASSEMBLER
2683 || bfd_is_com_section (add_symbol_segment)
2688 if ((int) fixP->fx_bit_fixP == 13)
2690 /* This is a COBR instruction. They have only a
2691 13-bit displacement and are only to be used
2692 for local branches: flag as error, don't generate
2694 as_bad_where (fixP->fx_file, fixP->fx_line,
2695 _("can't use COBR format with external label"));
2696 fixP->fx_addsy = NULL;
2700 #endif /* TC_I960 */
2704 if (S_IS_COMMON (add_symbolP))
2705 add_number += S_GET_VALUE (add_symbolP);
2706 #endif /* TE_I386AIX */
2707 #endif /* OBJ_COFF */
2713 #ifdef TC_DONT_FIX_NON_ADJUSTABLE
2715 #ifdef obj_fix_adjustable
2716 && obj_fix_adjustable (fixP)
2718 #ifdef tc_fix_adjustable
2719 && tc_fix_adjustable (fixP)
2723 add_number += S_GET_VALUE (add_symbolP);
2730 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2731 if (add_symbolP == 0)
2733 #ifndef BFD_ASSEMBLER
2734 fixP->fx_addsy = &abs_symbol;
2736 fixP->fx_addsy = section_symbol (absolute_section);
2738 symbol_mark_used_in_reloc (fixP->fx_addsy);
2745 #ifdef MD_APPLY_FIX3
2746 md_apply_fix3 (fixP, &add_number, this_segment_type);
2748 #ifdef BFD_ASSEMBLER
2749 md_apply_fix (fixP, &add_number);
2751 md_apply_fix (fixP, add_number);
2755 #ifndef TC_HANDLES_FX_DONE
2756 /* If the tc-* files haven't been converted, assume it's handling
2757 it the old way, where a null fx_addsy means that the fix has
2758 been applied completely, and no further work is needed. */
2759 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2764 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2766 if ((size_t) size < sizeof (valueT))
2771 mask--; /* Set all bits to one. */
2772 mask <<= size * 8 - (fixP->fx_signed ? 1 : 0);
2773 if ((add_number & mask) != 0 && (add_number & mask) != mask)
2775 char buf[50], buf2[50];
2776 sprint_value (buf, fragP->fr_address + where);
2777 if (add_number > 1000)
2778 sprint_value (buf2, add_number);
2780 sprintf (buf2, "%ld", (long) add_number);
2781 as_bad_where (fixP->fx_file, fixP->fx_line,
2782 _("Value of %s too large for field of %d bytes at %s"),
2784 } /* Generic error checking. */
2786 #ifdef WARN_SIGNED_OVERFLOW_WORD
2787 /* Warn if a .word value is too large when treated as a signed
2788 number. We already know it is not too negative. This is to
2789 catch over-large switches generated by gcc on the 68k. */
2790 if (!flag_signed_overflow_ok
2792 && add_number > 0x7fff)
2793 as_bad_where (fixP->fx_file, fixP->fx_line,
2794 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
2796 (unsigned long) (fragP->fr_address + where));
2798 } /* Not a bit fix. */
2800 #ifdef TC_VALIDATE_FIX
2801 skip: ATTRIBUTE_UNUSED_LABEL
2805 fprintf (stderr, "result:\n");
2808 } /* For each fixS in this segment. */
2810 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2811 return seg_reloc_count;
2814 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2817 number_to_chars_bigendian (buf, val, n)
2822 if ((size_t) n > sizeof (val) || n <= 0)
2826 buf[n] = val & 0xff;
2832 number_to_chars_littleendian (buf, val, n)
2837 if ((size_t) n > sizeof (val) || n <= 0)
2841 *buf++ = val & 0xff;
2847 write_print_statistics (file)
2850 fprintf (file, "fixups: %d\n", n_fixups);
2853 /* For debugging. */
2854 extern int indent_level;
2861 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2863 fprintf (stderr, " pcrel");
2864 if (fixp->fx_pcrel_adjust)
2865 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2866 if (fixp->fx_im_disp)
2869 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2871 fprintf (stderr, " im_disp");
2875 fprintf (stderr, " tcbit");
2877 fprintf (stderr, " done");
2878 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2879 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2880 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2881 #ifdef BFD_ASSEMBLER
2882 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2885 #ifdef NEED_FX_R_TYPE
2886 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2891 fprintf (stderr, "\n +<");
2892 print_symbol_value_1 (stderr, fixp->fx_addsy);
2893 fprintf (stderr, ">");
2897 fprintf (stderr, "\n -<");
2898 print_symbol_value_1 (stderr, fixp->fx_subsy);
2899 fprintf (stderr, ">");
2901 fprintf (stderr, "\n");
2902 #ifdef TC_FIX_DATA_PRINT
2903 TC_FIX_DATA_PRINT (stderr, fixp);