1 /* write.c - emit .o file
2 Copyright (C) 1986, 87, 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* This thing should be set up to do byteordering correctly. But... */
25 #include "output-file.h"
27 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
28 instruction so that the disassembler does not choke on it. */
30 #define NOP_OPCODE 0x00
33 #ifndef TC_ADJUST_RELOC_COUNT
34 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
37 #ifndef TC_FORCE_RELOCATION
38 #define TC_FORCE_RELOCATION(FIXP) 0
41 #ifndef WORKING_DOT_WORD
42 extern CONST int md_short_jump_size;
43 extern CONST int md_long_jump_size;
46 int symbol_table_frozen;
49 /* We generally attach relocs to frag chains. However, after we have
50 chained these all together into a segment, any relocs we add after
51 that must be attached to a segment. This will include relocs added
52 in md_estimate_size_for_relax, for example. */
53 static int frags_chained = 0;
59 struct frag *text_frag_root;
60 struct frag *data_frag_root;
61 struct frag *bss_frag_root;
63 struct frag *text_last_frag; /* Last frag in segment. */
64 struct frag *data_last_frag; /* Last frag in segment. */
65 static struct frag *bss_last_frag; /* Last frag in segment. */
69 static object_headers headers;
70 static char *the_object_file;
73 long string_byte_count;
74 char *next_object_file_charP; /* Tracks object file bytes. */
77 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
80 #endif /* BFD_ASSEMBLER */
83 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
84 symbolS *add, symbolS *sub,
85 offsetT offset, int pcrel,
86 bfd_reloc_code_real_type r_type));
88 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
89 symbolS *add, symbolS *sub,
90 offsetT offset, int pcrel,
93 #if defined (BFD_ASSEMBLER) || !defined (BFD)
94 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
96 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
101 * Create a fixS in obstack 'notes'.
104 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
106 fragS *frag; /* Which frag? */
107 int where; /* Where in that frag? */
108 int size; /* 1, 2, or 4 usually. */
109 symbolS *add_symbol; /* X_add_symbol. */
110 symbolS *sub_symbol; /* X_op_symbol. */
111 offsetT offset; /* X_add_number. */
112 int pcrel; /* TRUE if PC-relative relocation. */
114 bfd_reloc_code_real_type r_type; /* Relocation type */
116 int r_type; /* Relocation type */
121 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
123 fixP->fx_frag = frag;
124 fixP->fx_where = where;
125 fixP->fx_size = size;
126 fixP->fx_addsy = add_symbol;
127 fixP->fx_subsy = sub_symbol;
128 fixP->fx_offset = offset;
129 fixP->fx_pcrel = pcrel;
130 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
131 fixP->fx_r_type = r_type;
133 fixP->fx_im_disp = 0;
134 fixP->fx_pcrel_adjust = 0;
135 fixP->fx_bit_fixP = 0;
136 fixP->fx_addnumber = 0;
137 fixP->tc_fix_data = NULL;
145 as_where (&fixP->fx_file, &fixP->fx_line);
147 /* Usually, we want relocs sorted numerically, but while
148 comparing to older versions of gas that have relocs
149 reverse sorted, it is convenient to have this compile
150 time option. xoxorich. */
155 fixS **seg_fix_rootP = (frags_chained
156 ? &seg_info (now_seg)->fix_root
157 : &frchain_now->fix_root);
158 fixS **seg_fix_tailP = (frags_chained
159 ? &seg_info (now_seg)->fix_tail
160 : &frchain_now->fix_tail);
163 #ifdef REVERSE_SORT_RELOCS
165 fixP->fx_next = *seg_fix_rootP;
166 *seg_fix_rootP = fixP;
168 #else /* REVERSE_SORT_RELOCS */
170 fixP->fx_next = NULL;
173 (*seg_fix_tailP)->fx_next = fixP;
175 *seg_fix_rootP = fixP;
176 *seg_fix_tailP = fixP;
178 #endif /* REVERSE_SORT_RELOCS */
185 /* Create a fixup relative to a symbol (plus a constant). */
188 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
189 fragS *frag; /* Which frag? */
190 int where; /* Where in that frag? */
191 short int size; /* 1, 2, or 4 usually. */
192 symbolS *add_symbol; /* X_add_symbol. */
193 offsetT offset; /* X_add_number. */
194 int pcrel; /* TRUE if PC-relative relocation. */
196 bfd_reloc_code_real_type r_type; /* Relocation type */
198 int r_type; /* Relocation type */
201 return fix_new_internal (frag, where, size, add_symbol,
202 (symbolS *) NULL, offset, pcrel, r_type);
205 /* Create a fixup for an expression. Currently we only support fixups
206 for difference expressions. That is itself more than most object
207 file formats support anyhow. */
210 fix_new_exp (frag, where, size, exp, pcrel, r_type)
211 fragS *frag; /* Which frag? */
212 int where; /* Where in that frag? */
213 short int size; /* 1, 2, or 4 usually. */
214 expressionS *exp; /* Expression. */
215 int pcrel; /* TRUE if PC-relative relocation. */
217 bfd_reloc_code_real_type r_type; /* Relocation type */
219 int r_type; /* Relocation type */
232 sub = exp->X_add_symbol;
233 off = exp->X_add_number;
237 sub = exp->X_op_symbol;
240 add = exp->X_add_symbol;
243 off = exp->X_add_number;
247 as_bad ("expression too complex for fixup");
250 return fix_new_internal (frag, where, size, add, sub, off,
254 /* Append a string onto another string, bumping the pointer along. */
256 append (charPP, fromP, length)
259 unsigned long length;
261 /* Don't trust memcpy() of 0 chars. */
265 memcpy (*charPP, fromP, length);
269 #ifndef BFD_ASSEMBLER
270 int section_alignment[SEG_MAXIMUM_ORDINAL];
274 * This routine records the largest alignment seen for each segment.
275 * If the beginning of the segment is aligned on the worst-case
276 * boundary, all of the other alignments within it will work. At
277 * least one object format really uses this info.
280 record_alignment (seg, align)
281 /* Segment to which alignment pertains */
283 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
288 if (align > bfd_get_section_alignment (stdoutput, seg))
289 bfd_set_section_alignment (stdoutput, seg, align);
291 if (align > section_alignment[(int) seg])
292 section_alignment[(int) seg] = align;
296 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
299 chain_frchains_together_1 (section, frchp)
301 struct frchain *frchp;
303 fragS dummy, *prev_frag = &dummy;
304 fixS fix_dummy, *prev_fix = &fix_dummy;
306 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
308 prev_frag->fr_next = frchp->frch_root;
309 prev_frag = frchp->frch_last;
311 if (frchp->fix_root != (fixS *) NULL)
313 if (seg_info (section)->fix_root == (fixS *) NULL)
314 seg_info (section)->fix_root = frchp->fix_root;
315 prev_fix->fx_next = frchp->fix_root;
316 seg_info (section)->fix_tail = frchp->fix_tail;
317 prev_fix = frchp->fix_tail;
321 prev_frag->fr_next = 0;
330 chain_frchains_together (abfd, section, xxx)
331 bfd *abfd; /* unused */
333 PTR xxx; /* unused */
335 segment_info_type *info;
337 /* BFD may have introduced its own sections without using
338 subseg_new, so it is possible that seg_info is NULL. */
339 info = seg_info (section);
340 if (info != (segment_info_type *) NULL)
341 info->frchainP->frch_last
342 = chain_frchains_together_1 (section, info->frchainP);
344 /* Now that we've chained the frags together, we must add new fixups
345 to the segment, not to the frag chain. */
351 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
354 remove_subsegs (head, seg, root, last)
360 *root = head->frch_root;
361 *last = chain_frchains_together_1 (seg, head);
366 #if defined (BFD_ASSEMBLER) || !defined (BFD)
370 cvt_frag_to_fill (sec, fragP)
375 cvt_frag_to_fill (headers, fragP)
376 object_headers *headers;
380 switch (fragP->fr_type)
385 HANDLE_ALIGN (fragP);
387 fragP->fr_type = rs_fill;
388 know (fragP->fr_next != NULL);
389 fragP->fr_offset = (fragP->fr_next->fr_address
391 - fragP->fr_fix) / fragP->fr_var;
397 case rs_machine_dependent:
399 md_convert_frag (stdoutput, sec, fragP);
401 md_convert_frag (headers, fragP);
404 assert (fragP->fr_next == NULL || (fragP->fr_next->fr_address - fragP->fr_address == fragP->fr_fix));
407 * After md_convert_frag, we make the frag into a ".space 0".
408 * Md_convert_frag() should set up any fixSs and constants
414 #ifndef WORKING_DOT_WORD
417 struct broken_word *lie;
419 if (fragP->fr_subtype)
421 fragP->fr_fix += md_short_jump_size;
422 for (lie = (struct broken_word *) (fragP->fr_symbol);
423 lie && lie->dispfrag == fragP;
424 lie = lie->next_broken_word)
426 fragP->fr_fix += md_long_jump_size;
434 BAD_CASE (fragP->fr_type);
439 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
443 relax_and_size_seg (abfd, sec, xxx)
450 segment_info_type *seginfo;
452 valueT size, newsize;
454 flags = bfd_get_section_flags (abfd, sec);
456 seginfo = (segment_info_type *) bfd_get_section_userdata (abfd, sec);
457 if (seginfo && seginfo->frchainP)
459 relax_segment (seginfo->frchainP->frch_root, sec);
460 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
461 cvt_frag_to_fill (sec, fragp);
462 for (fragp = seginfo->frchainP->frch_root;
464 fragp = fragp->fr_next)
465 /* walk to last elt */;
466 size = fragp->fr_address + fragp->fr_fix;
471 if (size > 0 && ! seginfo->bss)
472 flags |= SEC_HAS_CONTENTS;
474 /* @@ This is just an approximation. */
475 if (seginfo && seginfo->fix_root)
479 x = bfd_set_section_flags (abfd, sec, flags);
482 newsize = md_section_align (sec, size);
483 x = bfd_set_section_size (abfd, sec, newsize);
486 /* If the size had to be rounded up, add some padding in the last
488 assert (newsize >= size);
491 fragS *last = seginfo->frchainP->frch_last;
492 fragp = seginfo->frchainP->frch_root;
493 while (fragp->fr_next != last)
494 fragp = fragp->fr_next;
495 last->fr_address = size;
496 fragp->fr_offset += newsize - size;
499 #ifdef tc_frob_section
500 tc_frob_section (sec);
502 #ifdef obj_frob_section
503 obj_frob_section (sec);
509 dump_section_relocs (abfd, sec, stream_)
514 FILE *stream = (FILE *) stream_;
515 segment_info_type *seginfo = seg_info (sec);
516 fixS *fixp = seginfo->fix_root;
521 fprintf (stream, "sec %s relocs:\n", sec->name);
524 symbolS *s = fixp->fx_addsy;
527 fprintf (stream, " %08x: %s(%s", fixp, S_GET_NAME (s),
528 s->bsym->section->name);
529 if (s->bsym->flags & BSF_SECTION_SYM)
531 fprintf (stream, " section sym");
533 fprintf (stream, "+%x", S_GET_VALUE (s));
536 fprintf (stream, "+%x", S_GET_VALUE (s));
537 fprintf (stream, ")+%x\n", fixp->fx_offset);
540 fprintf (stream, " %08x: type %d no sym\n", fixp, fixp->fx_r_type);
541 fixp = fixp->fx_next;
545 #define dump_section_relocs(ABFD,SEC,STREAM) (void)(ABFD,SEC,STREAM)
548 #ifndef EMIT_SECTION_SYMBOLS
549 #define EMIT_SECTION_SYMBOLS 1
553 adjust_reloc_syms (abfd, sec, xxx)
558 segment_info_type *seginfo = seg_info (sec);
564 dump_section_relocs (abfd, sec, stderr);
566 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
569 else if (fixp->fx_addsy)
571 symbolS *sym = fixp->fx_addsy;
572 asection *symsec = sym->bsym->section;
574 /* If it's one of these sections, assume the symbol is
575 definitely going to be output. The code in
576 md_estimate_size_before_relax in tc-mips.c uses this test
577 as well, so if you change this code you should look at that
579 if (symsec == &bfd_und_section
580 || symsec == &bfd_abs_section
581 || bfd_is_com_section (symsec))
583 fixp->fx_addsy->sy_used_in_reloc = 1;
587 /* Since we're reducing to section symbols, don't attempt to reduce
588 anything that's already using one. */
589 if (sym->bsym->flags & BSF_SECTION_SYM)
591 fixp->fx_addsy->sy_used_in_reloc = 1;
595 /* Is there some other reason we can't adjust this one? (E.g.,
596 call/bal links in i960-bout symbols.) */
597 #ifdef obj_fix_adjustable
598 if (! obj_fix_adjustable (fixp))
600 fixp->fx_addsy->sy_used_in_reloc = 1;
605 /* Is there some other (target cpu dependent) reason we can't adjust
606 this one? (E.g. relocations involving function addresses on
608 #ifdef tc_fix_adjustable
609 if (! tc_fix_adjustable (fixp))
611 fixp->fx_addsy->sy_used_in_reloc = 1;
616 /* If the section symbol isn't going to be output, the relocs
617 at least should still work. If not, figure out what to do
618 when we run into that case. */
619 fixp->fx_offset += S_GET_VALUE (sym);
621 fixp->fx_offset += sym->sy_frag->fr_address;
622 fixp->fx_addsy = section_symbol (symsec);
623 fixp->fx_addsy->sy_used_in_reloc = 1;
625 #if 1/*def RELOC_REQUIRES_SYMBOL*/
628 /* There was no symbol required by this relocation. However,
629 BFD doesn't really handle relocations without symbols well.
630 (At least, the COFF support doesn't.) So for now we fake up
631 a local symbol in the absolute section. */
633 fixp->fx_addsy = section_symbol (absolute_section);
634 fixp->fx_addsy->sy_used_in_reloc = 1;
638 dump_section_relocs (abfd, sec, stderr);
642 write_relocs (abfd, sec, xxx)
647 segment_info_type *seginfo = seg_info (sec);
654 /* If seginfo is NULL, we did not create this section; don't do
659 fixup_segment (seginfo->fix_root, sec);
662 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
665 #ifndef RELOC_EXPANSION_POSSIBLE
666 /* Set up reloc information as well. */
667 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
668 n * sizeof (arelent *));
669 memset ((char*)relocs, 0, n * sizeof (arelent*));
672 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
676 bfd_reloc_status_type s;
683 reloc = tc_gen_reloc (sec, fixp);
689 data = fixp->fx_frag->fr_literal + fixp->fx_where;
690 if (fixp->fx_where + fixp->fx_size
691 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
694 if (reloc->howto->partial_inplace == false
695 && reloc->howto->pcrel_offset == true
696 && reloc->howto->pc_relative == true)
698 /* bfd_perform_relocation screws this up */
699 reloc->addend += reloc->address;
701 /* Pass bogus address so that when bfd_perform_relocation adds
702 `reloc->address' back in, it'll come up with `data', which is where
703 we want it to operate. We can't just do it by fudging reloc->address,
704 since that might be used in the calculations(?). */
705 s = bfd_perform_relocation (stdoutput, reloc, data - reloc->address,
706 sec, stdoutput, &err);
711 case bfd_reloc_overflow:
712 as_bad_where (fixp->fx_file, fixp->fx_line, "relocation overflow");
715 as_fatal ("bad return from bfd_perform_relocation");
720 n = n * MAX_RELOC_EXPANSION;
721 /* Set up reloc information as well. */
722 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
723 n * sizeof (arelent *));
726 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
730 bfd_reloc_status_type s;
738 reloc = tc_gen_reloc (sec, fixp);
740 for (j = 0; reloc[j]; j++)
742 relocs[i++] = reloc[j];
745 data = fixp->fx_frag->fr_literal + fixp->fx_where;
746 if (fixp->fx_where + fixp->fx_size
747 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
749 for (j = 0; reloc[j]; j++)
751 s = bfd_perform_relocation (stdoutput, reloc[j],
752 data - reloc[0]->address,
753 sec, stdoutput, &err);
759 as_fatal ("bad return from bfd_perform_relocation");
770 sympp = bfd_get_outsymbols (stdoutput);
771 nsyms = bfd_get_symcount (stdoutput);
772 for (i = 0; i < n; i++)
773 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
775 for (j = 0; j < nsyms; j++)
776 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
785 bfd_set_reloc (stdoutput, sec, relocs, n);
787 bfd_set_section_flags (abfd, sec,
788 (bfd_get_section_flags (abfd, sec)
789 & (flagword) ~SEC_RELOC));
796 fprintf (stderr, "relocs for sec %s\n", sec->name);
797 for (i = 0; i < n; i++)
801 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
802 i, r, r->address, s->name, r->addend);
809 write_contents (abfd, sec, xxx)
814 segment_info_type *seginfo = seg_info (sec);
815 unsigned long offset = 0;
818 /* Write out the frags. */
820 || ! (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
823 for (f = seginfo->frchainP->frch_root;
828 unsigned long fill_size;
832 assert (f->fr_type == rs_fill);
835 x = bfd_set_section_contents (stdoutput, sec,
836 f->fr_literal, (file_ptr) offset,
837 (bfd_size_type) f->fr_fix);
840 bfd_perror (stdoutput->filename);
841 as_perror ("FATAL: Can't write %s", stdoutput->filename);
846 fill_literal = f->fr_literal + f->fr_fix;
847 fill_size = f->fr_var;
848 count = f->fr_offset;
850 if (fill_size && count)
853 x = bfd_set_section_contents (stdoutput, sec,
854 fill_literal, (file_ptr) offset,
855 (bfd_size_type) fill_size);
858 bfd_perror (stdoutput->filename);
859 as_perror ("FATAL: Can't write %s", stdoutput->filename);
868 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
870 merge_data_into_text ()
872 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
873 seg_info (text_section)->frchainP->frch_last->fr_next =
874 seg_info (data_section)->frchainP->frch_root;
875 seg_info (text_section)->frchainP->frch_last =
876 seg_info (data_section)->frchainP->frch_last;
877 seg_info (data_section)->frchainP = 0;
881 text_last_frag->fr_next = data_frag_root;
882 text_last_frag = data_last_frag;
883 data_last_frag = NULL;
884 data_frag_root = NULL;
887 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
888 tmp->fx_next = data_fix_root;
889 text_fix_tail = data_fix_tail;
892 text_fix_root = data_fix_root;
893 data_fix_root = NULL;
896 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
898 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
900 relax_and_size_all_segments ()
904 relax_segment (text_frag_root, SEG_TEXT);
905 relax_segment (data_frag_root, SEG_DATA);
906 relax_segment (bss_frag_root, SEG_BSS);
908 * Now the addresses of frags are correct within the segment.
911 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
912 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
913 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
916 * Join the 2 segments into 1 huge segment.
917 * To do this, re-compute every rn_address in the SEG_DATA frags.
918 * Then join the data frags after the text frags.
920 * Determine a_data [length of data segment].
924 register relax_addressT slide;
926 know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
928 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
929 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
930 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
932 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
933 /* For b.out: If the data section has a strict alignment
934 requirement, its load address in the .o file will be
935 rounded up from the size of the text section. These
936 two values are *not* the same! Similarly for the bss
938 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
941 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
943 fragP->fr_address += slide;
944 } /* for each data frag */
946 know (text_last_frag != 0);
947 text_last_frag->fr_next = data_frag_root;
951 H_SET_DATA_SIZE (&headers, 0);
955 /* See above comments on b.out data section address. */
958 if (data_last_frag == 0)
959 bss_vma = H_GET_TEXT_SIZE (&headers);
961 bss_vma = data_last_frag->fr_address;
962 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
963 bss_address_frag.fr_address = bss_vma;
965 #else /* ! OBJ_BOUT */
966 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
967 H_GET_DATA_SIZE (&headers));
969 #endif /* ! OBJ_BOUT */
971 /* Slide all the frags */
974 relax_addressT slide = bss_address_frag.fr_address;
976 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
978 fragP->fr_address += slide;
979 } /* for each bss frag */
983 H_SET_BSS_SIZE (&headers,
984 bss_last_frag->fr_address - bss_frag_root->fr_address);
986 H_SET_BSS_SIZE (&headers, 0);
988 #endif /* ! BFD_ASSEMBLER && ! BFD */
990 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1000 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1002 /* Count symbols. We can't rely on a count made by the loop in
1003 write_object_file, because *_frob_file may add a new symbol or
1006 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1013 asympp = (asymbol **) bfd_alloc (stdoutput,
1014 nsyms * sizeof (asymbol *));
1015 symp = symbol_rootP;
1016 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1018 asympp[i] = symp->bsym;
1024 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1025 assert (result == true);
1026 symbol_table_frozen = 1;
1031 write_object_file ()
1033 struct frchain *frchainP; /* Track along all frchains. */
1034 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1035 fragS *fragP; /* Track along all frags. */
1038 /* Do we really want to write it? */
1040 int n_warns, n_errs;
1041 n_warns = had_warnings ();
1042 n_errs = had_errors ();
1043 /* The -Z flag indicates that an object file should be generated,
1044 regardless of warnings and errors. */
1047 if (n_warns || n_errs)
1048 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
1049 n_errs, n_errs == 1 ? "" : "s",
1050 n_warns, n_warns == 1 ? "" : "s");
1055 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
1056 n_errs, n_errs == 1 ? "" : "s",
1057 n_warns, n_warns == 1 ? "" : "s");
1062 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1063 a routine to check for the definition of the procedure "_main",
1064 and if so -- fix it up so that it can be program entry point. */
1065 VMS_Check_For_Main ();
1068 /* After every sub-segment, we fake an ".align ...". This conforms to
1069 BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
1070 frag that requires least thought. ".align" frags like to have a
1071 following frag since that makes calculating their intended length
1074 @@ Is this really necessary?? */
1075 #ifndef SUB_SEGMENT_ALIGN
1076 #ifdef BFD_ASSEMBLER
1077 #define SUB_SEGMENT_ALIGN(SEG) (0)
1079 #define SUB_SEGMENT_ALIGN(SEG) (2)
1082 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1084 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1085 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
1086 /* frag_align will have left a new frag.
1087 Use this last frag for an empty ".fill".
1089 For this segment ...
1090 Create a last frag. Do not leave a "being filled in frag". */
1091 frag_wane (frag_now);
1092 frag_now->fr_fix = 0;
1093 know (frag_now->fr_next == NULL);
1096 /* From now on, we don't care about sub-segments. Build one frag chain
1097 for each segment. Linked thru fr_next. */
1099 #ifdef BFD_ASSEMBLER
1100 /* Remove the sections created by gas for its own purposes. */
1102 asection **seclist, *sec;
1103 seclist = &stdoutput->sections;
1104 while (seclist && *seclist)
1107 while (sec == reg_section || sec == expr_section)
1111 stdoutput->section_count--;
1116 seclist = &(*seclist)->next;
1120 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1122 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1123 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1124 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1127 /* We have two segments. If user gave -R flag, then we must put the
1128 data frags into the text segment. Do this before relaxing so
1129 we know to take advantage of -R and make shorter addresses. */
1130 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1133 merge_data_into_text ();
1137 #ifdef BFD_ASSEMBLER
1138 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1140 relax_and_size_all_segments ();
1141 #endif /* BFD_ASSEMBLER */
1143 #ifndef BFD_ASSEMBLER
1146 * Crawl the symbol chain.
1148 * For each symbol whose value depends on a frag, take the address of
1149 * that frag and subsume it into the value of the symbol.
1150 * After this, there is just one way to lookup a symbol value.
1151 * Values are left in their final state for object file emission.
1152 * We adjust the values of 'L' local symbols, even if we do
1153 * not intend to emit them to the object file, because their values
1154 * are needed for fix-ups.
1156 * Unless we saw a -L flag, remove all symbols that begin with 'L'
1157 * from the symbol chain. (They are still pointed to by the fixes.)
1159 * Count the remaining symbols.
1160 * Assign a symbol number to each symbol.
1161 * Count the number of string-table chars we will emit.
1162 * Put this info into the headers as appropriate.
1165 know (zero_address_frag.fr_address == 0);
1166 string_byte_count = sizeof (string_byte_count);
1168 obj_crawl_symbol_chain (&headers);
1170 if (string_byte_count == sizeof (string_byte_count))
1171 string_byte_count = 0;
1173 H_SET_STRING_SIZE (&headers, string_byte_count);
1176 * Addresses of frags now reflect addresses we use in the object file.
1177 * Symbol values are correct.
1178 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1179 * Also converting any machine-dependent frags using md_convert_frag();
1181 subseg_change (SEG_TEXT, 0);
1183 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1185 cvt_frag_to_fill (&headers, fragP);
1187 /* Some assert macros don't work with # directives mixed in. */
1189 if (!(fragP->fr_next == NULL
1191 || fragP->fr_next == data_frag_root
1193 || ((fragP->fr_next->fr_address - fragP->fr_address)
1194 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1198 #endif /* ! BFD_ASSEMBLER */
1200 #ifndef WORKING_DOT_WORD
1202 struct broken_word *lie;
1203 struct broken_word **prevP;
1205 prevP = &broken_words;
1206 for (lie = broken_words; lie; lie = lie->next_broken_word)
1211 exp.X_op = O_subtract;
1212 exp.X_add_symbol = lie->add;
1213 exp.X_op_symbol = lie->sub;
1214 exp.X_add_number = lie->addnum;
1215 #ifdef BFD_ASSEMBLER
1216 fix_new_exp (lie->frag,
1217 lie->word_goes_here - lie->frag->fr_literal,
1218 2, &exp, 0, BFD_RELOC_NONE);
1220 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1221 fix_new_exp (lie->frag,
1222 lie->word_goes_here - lie->frag->fr_literal,
1223 2, &exp, 0, NO_RELOC);
1226 fix_new_ns32k_exp (lie->frag,
1227 lie->word_goes_here - lie->frag->fr_literal,
1228 2, &exp, 0, 0, 2, 0, 0);
1230 fix_new_exp (lie->frag,
1231 lie->word_goes_here - lie->frag->fr_literal,
1233 #endif /* TC_NS32K */
1234 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1235 #endif /* BFD_ASSEMBLER */
1236 *prevP = lie->next_broken_word;
1239 prevP = &(lie->next_broken_word);
1241 for (lie = broken_words; lie;)
1243 struct broken_word *untruth;
1245 addressT table_addr;
1246 addressT from_addr, to_addr;
1249 fragP = lie->dispfrag;
1251 /* Find out how many broken_words go here. */
1253 for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1254 if (untruth->added == 1)
1257 table_ptr = lie->dispfrag->fr_opcode;
1258 table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
1259 /* Create the jump around the long jumps. This is a short
1260 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1261 from_addr = table_addr;
1262 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1263 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1264 table_ptr += md_short_jump_size;
1265 table_addr += md_short_jump_size;
1267 for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
1269 if (lie->added == 2)
1271 /* Patch the jump table */
1272 /* This is the offset from ??? to table_ptr+0 */
1273 to_addr = table_addr - S_GET_VALUE (lie->sub);
1274 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1275 for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1277 if (untruth->use_jump == lie)
1278 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1281 /* Install the long jump */
1282 /* this is a long jump from table_ptr+0 to the final target */
1283 from_addr = table_addr;
1284 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1285 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1286 table_ptr += md_long_jump_size;
1287 table_addr += md_long_jump_size;
1291 #endif /* not WORKING_DOT_WORD */
1293 #ifndef BFD_ASSEMBLER
1296 long object_file_size;
1298 * Scan every FixS performing fixups. We had to wait until now to do
1299 * this because md_convert_frag() may have made some fixSs.
1303 subseg_change (SEG_TEXT, 0);
1304 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1305 subseg_change (SEG_DATA, 0);
1306 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1307 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1309 /* FIXME move this stuff into the pre-write-hook */
1310 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1311 H_SET_ENTRY_POINT (&headers, 0);
1313 obj_pre_write_hook (&headers); /* extra coff stuff */
1315 object_file_size = H_GET_FILE_SIZE (&headers);
1316 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1318 output_file_create (out_file_name);
1320 obj_header_append (&next_object_file_charP, &headers);
1322 know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
1327 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1329 register long count;
1330 register char *fill_literal;
1331 register long fill_size;
1333 know (fragP->fr_type == rs_fill);
1334 append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
1335 fill_literal = fragP->fr_literal + fragP->fr_fix;
1336 fill_size = fragP->fr_var;
1337 know (fragP->fr_offset >= 0);
1339 for (count = fragP->fr_offset; count; count--)
1341 append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
1344 } /* for each code frag. */
1346 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
1351 obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
1352 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers)));
1354 /* Make addresses in data relocation directives relative to beginning of
1355 * first data fragment, not end of last text fragment: alignment of the
1356 * start of the data segment may place a gap between the segments.
1358 obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
1360 obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
1361 #endif /* TC_I960 */
1363 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers)));
1366 * Emit line number entries.
1368 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1369 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers)));
1374 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1375 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers) + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1381 if (string_byte_count > 0)
1383 obj_emit_strings (&next_object_file_charP);
1384 } /* only if we have a string table */
1387 bfd_seek (stdoutput, 0, 0);
1388 bfd_write (the_object_file, 1, object_file_size, stdoutput);
1391 /* Write the data to the file */
1392 output_file_append (the_object_file, object_file_size, out_file_name);
1394 } /* non vms output */
1397 * Now do the VMS-dependent part of writing the object file
1399 VMS_write_object_file (H_GET_TEXT_SIZE (&headers),
1400 H_GET_DATA_SIZE (&headers),
1401 H_GET_BSS_SIZE (&headers),
1402 text_frag_root, data_frag_root);
1404 #else /* BFD_ASSEMBLER */
1406 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *)0);
1408 /* Set up symbol table, and write it out. */
1413 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1417 if (! symp->sy_resolved)
1419 if (symp->sy_value.X_op == O_constant)
1421 /* This is the normal case; skip the call. */
1424 + symp->sy_frag->fr_address));
1425 symp->sy_resolved = 1;
1428 resolve_symbol_value (symp);
1431 /* So far, common symbols have been treated like undefined symbols.
1432 Put them in the common section now. */
1433 if (S_IS_DEFINED (symp) == 0
1434 && S_GET_VALUE (symp) != 0)
1435 S_SET_SEGMENT (symp, &bfd_com_section);
1437 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1438 S_GET_NAME (symp), symp,
1441 segment_name (symp->bsym->section));
1444 #ifdef obj_frob_symbol
1445 obj_frob_symbol (symp, punt);
1447 #ifdef tc_frob_symbol
1448 if (! punt || symp->sy_used_in_reloc)
1449 tc_frob_symbol (symp, punt);
1452 /* If we don't want to keep this symbol, splice it out of
1453 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1454 want section symbols. Otherwise, we skip local symbols
1455 and symbols that the frob_symbol macros told us to punt,
1456 but we keep such symbols if they are used in relocs. */
1457 if ((! EMIT_SECTION_SYMBOLS
1458 && (symp->bsym->flags & BSF_SECTION_SYM) != 0)
1459 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1460 opposites. Sometimes the former checks flags and the
1461 latter examines the name... */
1462 || (!S_IS_EXTERN (symp)
1463 && (S_IS_LOCAL (symp) || punt)
1464 && ! symp->sy_used_in_reloc))
1466 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1467 /* After symbol_remove, symbol_next(symp) still returns
1468 the one that came after it in the chain. So we don't
1469 need to do any extra cleanup work here. */
1474 /* Make sure we really got a value for the symbol. */
1475 if (! symp->sy_resolved)
1477 as_bad ("can't resolve value for symbol \"%s\"",
1479 symp->sy_resolved = 1;
1482 /* Set the value into the BFD symbol. Up til now the value
1483 has only been kept in the gas symbolS struct. */
1484 symp->bsym->value = S_GET_VALUE (symp);
1488 /* If *_frob_file changes the symbol value at this point, it is
1489 responsible for moving the changed value into symp->bsym->value
1490 as well. Hopefully all symbol value changing can be done in
1495 #ifdef obj_frob_file
1499 /* Now that all the sizes are known, and contents correct, we can
1500 start writing to the file. */
1503 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1505 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1506 #endif /* BFD_ASSEMBLER */
1513 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1518 * After this, all frags in this segment have addresses that are correct
1519 * within the segment. Since segments live in different file addresses,
1520 * these frag addresses may not be the same as final object-file addresses.
1523 #ifndef md_relax_frag
1525 /* Subroutines of relax_segment. */
1531 for (; f1; f1 = f1->fr_next)
1532 if (f1->fr_next == f2)
1537 #endif /* ! defined (md_relax_frag) */
1539 /* Relax_align. Advance location counter to next address that has 'alignment'
1540 lowest order bits all 0s, return size of adjustment made. */
1541 static relax_addressT
1542 relax_align (address, alignment)
1543 register relax_addressT address; /* Address now. */
1544 register int alignment; /* Alignment (binary). */
1546 relax_addressT mask;
1547 relax_addressT new_address;
1549 mask = ~((~0) << alignment);
1550 new_address = (address + mask) & (~mask);
1552 /* We must provide lots of padding, so the linker can discard it
1553 when needed. The linker will not add extra space, ever. */
1554 new_address += (1 << alignment);
1555 return (new_address - address);
1559 relax_segment (segment_frag_root, segment)
1560 struct frag *segment_frag_root;
1563 register struct frag *fragP;
1564 register relax_addressT address;
1565 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1566 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
1568 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1569 subseg_change (segment, 0);
1571 /* For each frag in segment: count and store (a 1st guess of)
1574 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1576 fragP->fr_address = address;
1577 address += fragP->fr_fix;
1579 switch (fragP->fr_type)
1582 address += fragP->fr_offset * fragP->fr_var;
1587 int offset = relax_align (address, (int) fragP->fr_offset);
1588 if (offset % fragP->fr_var != 0)
1590 as_bad ("alignment padding (%d bytes) not a multiple of %ld",
1591 offset, (long) fragP->fr_var);
1592 offset -= (offset % fragP->fr_var);
1599 /* Assume .org is nugatory. It will grow with 1st relax. */
1602 case rs_machine_dependent:
1603 address += md_estimate_size_before_relax (fragP, segment);
1606 #ifndef WORKING_DOT_WORD
1607 /* Broken words don't concern us yet */
1608 case rs_broken_word:
1613 BAD_CASE (fragP->fr_type);
1615 } /* switch(fr_type) */
1616 } /* for each frag in the segment */
1620 long stretch; /* May be any size, 0 or negative. */
1621 /* Cumulative number of addresses we have */
1622 /* relaxed this pass. */
1623 /* We may have relaxed more than one address. */
1624 long stretched; /* Have we stretched on this pass? */
1625 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1626 grew, and another shrank. If a branch instruction doesn't fit anymore,
1627 we could be scrod. */
1631 stretch = stretched = 0;
1632 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1635 unsigned long was_address;
1641 was_address = fragP->fr_address;
1642 address = fragP->fr_address += stretch;
1643 symbolP = fragP->fr_symbol;
1644 offset = fragP->fr_offset;
1646 switch (fragP->fr_type)
1648 case rs_fill: /* .fill never relaxes. */
1652 #ifndef WORKING_DOT_WORD
1653 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1654 for it I do not want to write it. I do not want to have
1655 anything to do with it. This is not the proper way to
1656 implement this misfeature. */
1657 case rs_broken_word:
1659 struct broken_word *lie;
1660 struct broken_word *untruth;
1662 /* Yes this is ugly (storing the broken_word pointer
1663 in the symbol slot). Still, this whole chunk of
1664 code is ugly, and I don't feel like doing anything
1665 about it. Think of it as stubbornness in action. */
1667 for (lie = (struct broken_word *) (fragP->fr_symbol);
1668 lie && lie->dispfrag == fragP;
1669 lie = lie->next_broken_word)
1675 offset = (lie->add->sy_frag->fr_address
1676 + S_GET_VALUE (lie->add)
1678 - (lie->sub->sy_frag->fr_address
1679 + S_GET_VALUE (lie->sub)));
1680 if (offset <= -32768 || offset >= 32767)
1685 sprint_value (buf, (addressT) lie->addnum);
1686 as_warn (".word %s-%s+%s didn't fit",
1687 S_GET_NAME (lie->add),
1688 S_GET_NAME (lie->sub),
1692 if (fragP->fr_subtype == 0)
1694 fragP->fr_subtype++;
1695 growth += md_short_jump_size;
1697 for (untruth = lie->next_broken_word;
1698 untruth && untruth->dispfrag == lie->dispfrag;
1699 untruth = untruth->next_broken_word)
1700 if ((untruth->add->sy_frag == lie->add->sy_frag)
1701 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
1704 untruth->use_jump = lie;
1706 growth += md_long_jump_size;
1711 } /* case rs_broken_word */
1714 growth = (relax_align ((relax_addressT) (address
1717 - relax_align ((relax_addressT) (was_address
1727 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1728 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1729 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1730 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1731 || S_GET_SEGMENT (symbolP) == SEG_BSS);
1732 know (symbolP->sy_frag);
1733 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1734 || (symbolP->sy_frag == &zero_address_frag));
1736 target += S_GET_VALUE (symbolP)
1737 + symbolP->sy_frag->fr_address;
1738 } /* if we have a symbol */
1740 know (fragP->fr_next);
1741 after = fragP->fr_next->fr_address;
1742 growth = ((target - after) > 0) ? (target - after) : 0;
1743 /* Growth may be negative, but variable part of frag
1744 cannot have fewer than 0 chars. That is, we can't
1747 growth -= stretch; /* This is an absolute growth factor */
1750 case rs_machine_dependent:
1751 #ifdef md_relax_frag
1752 growth = md_relax_frag (fragP, stretch);
1754 /* The default way to relax a frag is to look through
1757 const relax_typeS *this_type;
1758 const relax_typeS *start_type;
1759 relax_substateT next_state;
1760 relax_substateT this_state;
1763 this_state = fragP->fr_subtype;
1764 start_type = this_type = md_relax_table + this_state;
1769 #ifndef DIFF_EXPR_OK
1770 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1771 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1772 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1773 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
1774 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
1776 know (symbolP->sy_frag);
1778 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
1779 || symbolP->sy_frag == &zero_address_frag);
1781 S_GET_VALUE (symbolP)
1782 + symbolP->sy_frag->fr_address;
1784 /* If frag has yet to be reached on this pass,
1785 assume it will move by STRETCH just as we did.
1786 If this is not so, it will be because some frag
1787 between grows, and that will force another pass.
1789 Beware zero-length frags.
1791 There should be a faster way to do this. */
1793 if (symbolP->sy_frag->fr_address >= was_address
1794 && is_dnrange (fragP, symbolP->sy_frag))
1800 aim = target - address - fragP->fr_fix;
1801 /* The displacement is affected by the instruction size
1802 for the 32k architecture. I think we ought to be able
1803 to add fragP->fr_pcrel_adjust in all cases (it should be
1804 zero if not used), but just in case it breaks something
1805 else we'll put this inside #ifdef NS32K ... #endif */
1807 if (fragP->fr_pcrel_adjust)
1810 aim += fragP->fr_pcrel_adjust;
1814 /* Look backwards. */
1815 for (next_state = this_type->rlx_more; next_state;)
1816 if (aim >= this_type->rlx_backward)
1820 /* Grow to next state. */
1821 this_state = next_state;
1822 this_type = md_relax_table + this_state;
1823 next_state = this_type->rlx_more;
1828 #ifdef M68K_AIM_KLUDGE
1829 M68K_AIM_KLUDGE (aim, this_state, this_type);
1831 /* Look forwards. */
1832 for (next_state = this_type->rlx_more; next_state;)
1833 if (aim <= this_type->rlx_forward)
1837 /* Grow to next state. */
1838 this_state = next_state;
1839 this_type = md_relax_table + this_state;
1840 next_state = this_type->rlx_more;
1844 growth = this_type->rlx_length - start_type->rlx_length;
1846 fragP->fr_subtype = this_state;
1852 BAD_CASE (fragP->fr_type);
1860 } /* For each frag in the segment. */
1862 while (stretched); /* Until nothing further to relax. */
1866 * We now have valid fr_address'es for each frag.
1870 * All fr_address's are correct, relative to their own segment.
1871 * We have made all the fixS we will ever make.
1873 } /* relax_segment() */
1875 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1879 Go through all the fixS's in a segment and see which ones can be
1880 handled now. (These consist of fixS where we have since discovered
1881 the value of a symbol, or the address of the frag involved.)
1882 For each one, call md_apply_fix to put the fix into the frag data.
1884 Result is a count of how many relocation structs will be needed to
1885 handle the remaining fixS's that we couldn't completely handle here.
1886 These will be output later by emit_relocations(). */
1889 fixup_segment (fixP, this_segment_type)
1890 register fixS *fixP;
1891 segT this_segment_type; /* N_TYPE bits for segment. */
1893 long seg_reloc_count = 0;
1894 symbolS *add_symbolP;
1895 symbolS *sub_symbolP;
1902 segT add_symbol_segment = absolute_section;
1904 /* If the linker is doing the relaxing, we must not do any fixups.
1906 Well, strictly speaking that's not true -- we could do any that are
1907 PC-relative and don't cross regions that could change size. And for the
1908 i960 (the only machine for which we've got a relaxing linker right now),
1909 we might be able to turn callx/callj into bal anyways in cases where we
1910 know the maximum displacement. */
1913 for (; fixP; fixP = fixP->fx_next)
1915 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
1916 return seg_reloc_count;
1919 for (; fixP; fixP = fixP->fx_next)
1921 fragP = fixP->fx_frag;
1923 where = fixP->fx_where;
1924 place = fragP->fr_literal + where;
1925 size = fixP->fx_size;
1926 add_symbolP = fixP->fx_addsy;
1927 #ifdef TC_VALIDATE_FIX
1928 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
1930 sub_symbolP = fixP->fx_subsy;
1931 add_number = fixP->fx_offset;
1932 pcrel = fixP->fx_pcrel;
1935 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
1942 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
1943 add_number -= S_GET_VALUE (sub_symbolP);
1945 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
1947 /* Should try converting to a constant. */
1952 as_bad_where (fixP->fx_file, fixP->fx_line,
1953 "Negative of non-absolute symbol %s",
1954 S_GET_NAME (sub_symbolP));
1956 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
1957 && (SEG_NORMAL (add_symbol_segment)
1958 || (add_symbol_segment == absolute_section)))
1960 /* Difference of 2 symbols from same segment.
1961 Can't make difference of 2 undefineds: 'value' means
1962 something different for N_UNDF. */
1964 /* Makes no sense to use the difference of 2 arbitrary symbols
1965 as the target of a call instruction. */
1967 as_bad_where (fixP->fx_file, fixP->fx_line,
1968 "callj to difference of 2 symbols");
1969 #endif /* TC_I960 */
1970 add_number += S_GET_VALUE (add_symbolP) -
1971 S_GET_VALUE (sub_symbolP);
1975 /* Let the target machine make the final determination
1976 as to whether or not a relocation will be needed to
1977 handle this fixup. */
1978 if (!TC_FORCE_RELOCATION (fixP))
1980 fixP->fx_addsy = NULL;
1985 /* Different segments in subtraction. */
1986 know (!(S_IS_EXTERNAL (sub_symbolP)
1987 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
1989 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
1990 add_number -= S_GET_VALUE (sub_symbolP);
1993 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
1994 #if 0 /* Do this even if it's already described as pc-relative. For example,
1995 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
1996 pc-relative mode. */
2001 /* Make it pc-relative. */
2002 add_number += (md_pcrel_from (fixP)
2003 - S_GET_VALUE (sub_symbolP));
2010 #ifdef BFD_ASSEMBLER
2011 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2012 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2014 /* Leave it alone. */
2020 sprint_value (buf, fragP->fr_address + where);
2021 as_bad_where (fixP->fx_file, fixP->fx_line,
2022 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
2023 segment_name (S_GET_SEGMENT (sub_symbolP)),
2024 S_GET_NAME (sub_symbolP), buf);
2031 if (add_symbol_segment == this_segment_type && pcrel)
2034 * This fixup was made when the symbol's segment was
2035 * SEG_UNKNOWN, but it is now in the local segment.
2036 * So we know how to do the address without relocation.
2039 /* reloc_callj() may replace a 'call' with a 'calls' or a
2040 'bal', in which cases it modifies *fixP as appropriate.
2041 In the case of a 'calls', no further work is required,
2042 and *fixP has been set up to make the rest of the code
2045 #endif /* TC_I960 */
2047 add_number += S_GET_VALUE (add_symbolP);
2048 add_number -= md_pcrel_from (fixP);
2049 pcrel = 0; /* Lie. Don't want further pcrel processing. */
2051 /* Let the target machine make the final determination
2052 as to whether or not a relocation will be needed to
2053 handle this fixup. */
2054 if (!TC_FORCE_RELOCATION (fixP))
2057 fixP->fx_addsy = NULL;
2062 if (add_symbol_segment == absolute_section)
2065 /* See comment about reloc_callj() above. */
2067 #endif /* TC_I960 */
2068 add_number += S_GET_VALUE (add_symbolP);
2070 /* Let the target machine make the final determination
2071 as to whether or not a relocation will be needed to
2072 handle this fixup. */
2073 if (!TC_FORCE_RELOCATION (fixP))
2075 fixP->fx_addsy = NULL;
2079 else if (add_symbol_segment == undefined_section
2080 #ifdef BFD_ASSEMBLER
2081 || bfd_is_com_section (add_symbol_segment)
2086 if ((int) fixP->fx_bit_fixP == 13)
2088 /* This is a COBR instruction. They have only a
2089 * 13-bit displacement and are only to be used
2090 * for local branches: flag as error, don't generate
2093 as_bad_where (fixP->fx_file, fixP->fx_line,
2094 "can't use COBR format with external label");
2095 fixP->fx_addsy = NULL;
2099 #endif /* TC_I960 */
2103 if (S_IS_COMMON (add_symbolP))
2104 add_number += S_GET_VALUE (add_symbolP);
2105 #endif /* TE_I386AIX */
2106 #endif /* OBJ_COFF */
2112 add_number += S_GET_VALUE (add_symbolP);
2119 add_number -= md_pcrel_from (fixP);
2120 if (add_symbolP == 0)
2122 #ifndef BFD_ASSEMBLER
2123 fixP->fx_addsy = &abs_symbol;
2125 fixP->fx_addsy = section_symbol (absolute_section);
2127 fixP->fx_addsy->sy_used_in_reloc = 1;
2132 if (!fixP->fx_bit_fixP && size > 0)
2135 if (size < sizeof (mask))
2137 /* set all bits to one */
2139 /* Technically, combining these produces an undefined result
2140 if size is sizeof (valueT), though I think these two
2141 half-way operations should both be defined. And the
2142 compiler should be able to combine them if it's valid on
2143 the host architecture. */
2146 if ((add_number & mask) != 0
2147 && (add_number & mask) != mask)
2149 char buf[50], buf2[50];
2150 sprint_value (buf, fragP->fr_address + where);
2151 if (add_number > 1000)
2152 sprint_value (buf2, add_number);
2154 sprintf (buf2, "%ld", (long) add_number);
2155 as_bad_where (fixP->fx_file, fixP->fx_line,
2156 "Value of %s too large for field of %d bytes at %s",
2158 } /* generic error checking */
2160 #ifdef WARN_SIGNED_OVERFLOW_WORD
2161 /* Warn if a .word value is too large when treated as a signed
2162 number. We already know it is not too negative. This is to
2163 catch over-large switches generated by gcc on the 68k. */
2166 && add_number > 0x7fff)
2167 as_bad_where (fixP->fx_file, fixP->fx_line,
2168 "Signed .word overflow; switch may be too large; %ld at 0x%lx",
2170 (unsigned long) (fragP->fr_address + where));
2172 } /* not a bit fix */
2176 #ifdef BFD_ASSEMBLER
2177 md_apply_fix (fixP, &add_number);
2179 md_apply_fix (fixP, add_number);
2182 #ifndef TC_HANDLES_FX_DONE
2183 /* If the tc-* files haven't been converted, assume it's handling
2184 it the old way, where a null fx_addsy means that the fix has
2185 been applied completely, and no further work is needed. */
2186 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2190 #ifdef TC_VALIDATE_FIX
2194 } /* For each fixS in this segment. */
2196 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2197 return seg_reloc_count;
2200 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
2203 number_to_chars_bigendian (buf, val, n)
2208 if (n > sizeof (val)|| n <= 0)
2212 buf[n] = val & 0xff;
2218 number_to_chars_littleendian (buf, val, n)
2223 if (n > sizeof (val) || n <= 0)
2227 *buf++ = val & 0xff;
2232 /* end of write.c */