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;
51 struct frag *text_frag_root;
52 struct frag *data_frag_root;
53 struct frag *bss_frag_root;
55 struct frag *text_last_frag; /* Last frag in segment. */
56 struct frag *data_last_frag; /* Last frag in segment. */
57 static struct frag *bss_last_frag; /* Last frag in segment. */
61 static object_headers headers;
62 static char *the_object_file;
65 long string_byte_count;
66 char *next_object_file_charP; /* Tracks object file bytes. */
69 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
72 #endif /* BFD_ASSEMBLER */
75 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
76 symbolS *add, symbolS *sub,
77 offsetT offset, int pcrel,
78 bfd_reloc_code_real_type r_type));
80 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
81 symbolS *add, symbolS *sub,
82 offsetT offset, int pcrel,
85 #if defined (BFD_ASSEMBLER) || !defined (BFD)
86 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
88 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
93 * Create a fixS in obstack 'notes'.
96 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
98 fragS *frag; /* Which frag? */
99 int where; /* Where in that frag? */
100 int size; /* 1, 2, or 4 usually. */
101 symbolS *add_symbol; /* X_add_symbol. */
102 symbolS *sub_symbol; /* X_op_symbol. */
103 offsetT offset; /* X_add_number. */
104 int pcrel; /* TRUE if PC-relative relocation. */
106 bfd_reloc_code_real_type r_type; /* Relocation type */
108 int r_type; /* Relocation type */
113 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
115 fixP->fx_frag = frag;
116 fixP->fx_where = where;
117 fixP->fx_size = size;
118 fixP->fx_addsy = add_symbol;
119 fixP->fx_subsy = sub_symbol;
120 fixP->fx_offset = offset;
121 fixP->fx_pcrel = pcrel;
122 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
123 fixP->fx_r_type = r_type;
125 fixP->fx_im_disp = 0;
126 fixP->fx_pcrel_adjust = 0;
127 fixP->fx_bit_fixP = 0;
128 fixP->fx_addnumber = 0;
129 fixP->tc_fix_data = NULL;
137 as_where (&fixP->fx_file, &fixP->fx_line);
139 /* Usually, we want relocs sorted numerically, but while
140 comparing to older versions of gas that have relocs
141 reverse sorted, it is convenient to have this compile
142 time option. xoxorich. */
147 fixS **seg_fix_rootP = &frchain_now->fix_root;
148 fixS **seg_fix_tailP = &frchain_now->fix_tail;
151 #ifdef REVERSE_SORT_RELOCS
153 fixP->fx_next = *seg_fix_rootP;
154 *seg_fix_rootP = fixP;
156 #else /* REVERSE_SORT_RELOCS */
158 fixP->fx_next = NULL;
161 (*seg_fix_tailP)->fx_next = fixP;
163 *seg_fix_rootP = fixP;
164 *seg_fix_tailP = fixP;
166 #endif /* REVERSE_SORT_RELOCS */
173 /* Create a fixup relative to a symbol (plus a constant). */
176 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
177 fragS *frag; /* Which frag? */
178 int where; /* Where in that frag? */
179 short int size; /* 1, 2, or 4 usually. */
180 symbolS *add_symbol; /* X_add_symbol. */
181 offsetT offset; /* X_add_number. */
182 int pcrel; /* TRUE if PC-relative relocation. */
184 bfd_reloc_code_real_type r_type; /* Relocation type */
186 int r_type; /* Relocation type */
189 return fix_new_internal (frag, where, size, add_symbol,
190 (symbolS *) NULL, offset, pcrel, r_type);
193 /* Create a fixup for an expression. Currently we only support fixups
194 for difference expressions. That is itself more than most object
195 file formats support anyhow. */
198 fix_new_exp (frag, where, size, exp, pcrel, r_type)
199 fragS *frag; /* Which frag? */
200 int where; /* Where in that frag? */
201 short int size; /* 1, 2, or 4 usually. */
202 expressionS *exp; /* Expression. */
203 int pcrel; /* TRUE if PC-relative relocation. */
205 bfd_reloc_code_real_type r_type; /* Relocation type */
207 int r_type; /* Relocation type */
220 sub = exp->X_add_symbol;
221 off = exp->X_add_number;
225 sub = exp->X_op_symbol;
228 add = exp->X_add_symbol;
231 off = exp->X_add_number;
235 as_bad ("expression too complex for fixup");
238 return fix_new_internal (frag, where, size, add, sub, off,
242 /* Append a string onto another string, bumping the pointer along. */
244 append (charPP, fromP, length)
247 unsigned long length;
249 /* Don't trust memcpy() of 0 chars. */
253 memcpy (*charPP, fromP, length);
257 #ifndef BFD_ASSEMBLER
258 int section_alignment[SEG_MAXIMUM_ORDINAL];
262 * This routine records the largest alignment seen for each segment.
263 * If the beginning of the segment is aligned on the worst-case
264 * boundary, all of the other alignments within it will work. At
265 * least one object format really uses this info.
268 record_alignment (seg, align)
269 /* Segment to which alignment pertains */
271 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
276 if (align > bfd_get_section_alignment (stdoutput, seg))
277 bfd_set_section_alignment (stdoutput, seg, align);
279 if (align > section_alignment[(int) seg])
280 section_alignment[(int) seg] = align;
284 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
287 chain_frchains_together_1 (section, frchp)
289 struct frchain *frchp;
291 fragS dummy, *prev_frag = &dummy;
292 fixS fix_dummy, *prev_fix = &fix_dummy;
294 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
296 prev_frag->fr_next = frchp->frch_root;
297 prev_frag = frchp->frch_last;
299 if (frchp->fix_root != (fixS *) NULL)
301 if (seg_info (section)->fix_root == (fixS *) NULL)
302 seg_info (section)->fix_root = frchp->fix_root;
303 prev_fix->fx_next = frchp->fix_root;
304 prev_fix = frchp->fix_tail;
308 prev_frag->fr_next = 0;
317 chain_frchains_together (abfd, section, xxx)
318 bfd *abfd; /* unused */
320 PTR xxx; /* unused */
322 segment_info_type *info;
324 /* BFD may have introduced its own sections without using
325 subseg_new, so it is possible that seg_info is NULL. */
326 info = seg_info (section);
327 if (info != (segment_info_type *) NULL)
328 info->frchainP->frch_last
329 = chain_frchains_together_1 (section, info->frchainP);
334 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
337 remove_subsegs (head, seg, root, last)
343 *root = head->frch_root;
344 *last = chain_frchains_together_1 (seg, head);
349 #if defined (BFD_ASSEMBLER) || !defined (BFD)
353 cvt_frag_to_fill (sec, fragP)
358 cvt_frag_to_fill (headers, fragP)
359 object_headers *headers;
363 switch (fragP->fr_type)
368 HANDLE_ALIGN (fragP);
370 fragP->fr_type = rs_fill;
371 know (fragP->fr_next != NULL);
372 fragP->fr_offset = (fragP->fr_next->fr_address
374 - fragP->fr_fix) / fragP->fr_var;
380 case rs_machine_dependent:
382 md_convert_frag (stdoutput, sec, fragP);
384 md_convert_frag (headers, fragP);
387 assert (fragP->fr_next == NULL || (fragP->fr_next->fr_address - fragP->fr_address == fragP->fr_fix));
390 * After md_convert_frag, we make the frag into a ".space 0".
391 * Md_convert_frag() should set up any fixSs and constants
397 #ifndef WORKING_DOT_WORD
400 struct broken_word *lie;
402 if (fragP->fr_subtype)
404 fragP->fr_fix += md_short_jump_size;
405 for (lie = (struct broken_word *) (fragP->fr_symbol);
406 lie && lie->dispfrag == fragP;
407 lie = lie->next_broken_word)
409 fragP->fr_fix += md_long_jump_size;
417 BAD_CASE (fragP->fr_type);
422 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
426 relax_and_size_seg (abfd, sec, xxx)
433 segment_info_type *seginfo;
435 valueT size, newsize;
437 flags = bfd_get_section_flags (abfd, sec);
439 seginfo = (segment_info_type *) bfd_get_section_userdata (abfd, sec);
440 if (seginfo && seginfo->frchainP)
442 relax_segment (seginfo->frchainP->frch_root, sec);
443 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
444 cvt_frag_to_fill (sec, fragp);
445 for (fragp = seginfo->frchainP->frch_root;
447 fragp = fragp->fr_next)
448 /* walk to last elt */;
449 size = fragp->fr_address + fragp->fr_fix;
454 if (size > 0 && ! seginfo->bss)
455 flags |= SEC_HAS_CONTENTS;
457 /* @@ This is just an approximation. */
458 if (seginfo && seginfo->fix_root)
462 x = bfd_set_section_flags (abfd, sec, flags);
465 newsize = md_section_align (sec, size);
466 x = bfd_set_section_size (abfd, sec, newsize);
469 /* If the size had to be rounded up, add some padding in the last
471 assert (newsize >= size);
474 fragS *last = seginfo->frchainP->frch_last;
475 fragp = seginfo->frchainP->frch_root;
476 while (fragp->fr_next != last)
477 fragp = fragp->fr_next;
478 last->fr_address = size;
479 fragp->fr_offset += newsize - size;
482 #ifdef tc_frob_section
483 tc_frob_section (sec);
485 #ifdef obj_frob_section
486 obj_frob_section (sec);
492 dump_section_relocs (abfd, sec, stream_)
497 FILE *stream = (FILE *) stream_;
498 segment_info_type *seginfo = seg_info (sec);
499 fixS *fixp = seginfo->fix_root;
504 fprintf (stream, "sec %s relocs:\n", sec->name);
507 symbolS *s = fixp->fx_addsy;
510 fprintf (stream, " %08x: %s(%s", fixp, S_GET_NAME (s),
511 s->bsym->section->name);
512 if (s->bsym->flags & BSF_SECTION_SYM)
514 fprintf (stream, " section sym");
516 fprintf (stream, "+%x", S_GET_VALUE (s));
519 fprintf (stream, "+%x", S_GET_VALUE (s));
520 fprintf (stream, ")+%x\n", fixp->fx_offset);
523 fprintf (stream, " %08x: type %d no sym\n", fixp, fixp->fx_r_type);
524 fixp = fixp->fx_next;
528 #define dump_section_relocs(ABFD,SEC,STREAM) (void)(ABFD,SEC,STREAM)
531 #ifndef EMIT_SECTION_SYMBOLS
532 #define EMIT_SECTION_SYMBOLS 1
536 adjust_reloc_syms (abfd, sec, xxx)
541 segment_info_type *seginfo = seg_info (sec);
547 dump_section_relocs (abfd, sec, stderr);
549 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
552 else if (fixp->fx_addsy)
554 symbolS *sym = fixp->fx_addsy;
555 asection *symsec = sym->bsym->section;
557 /* If it's one of these sections, assume the symbol is
558 definitely going to be output. The code in
559 md_estimate_size_before_relax in tc-mips.c uses this test
560 as well, so if you change this code you should look at that
562 if (symsec == &bfd_und_section
563 || symsec == &bfd_abs_section
564 || bfd_is_com_section (symsec))
566 fixp->fx_addsy->sy_used_in_reloc = 1;
570 /* Since we're reducing to section symbols, don't attempt to reduce
571 anything that's already using one. */
572 if (sym->bsym->flags & BSF_SECTION_SYM)
574 fixp->fx_addsy->sy_used_in_reloc = 1;
578 /* Is there some other reason we can't adjust this one? (E.g.,
579 call/bal links in i960-bout symbols.) */
580 #ifdef obj_fix_adjustable
581 if (! obj_fix_adjustable (fixp))
583 fixp->fx_addsy->sy_used_in_reloc = 1;
588 /* Is there some other (target cpu dependent) reason we can't adjust
589 this one? (E.g. relocations involving function addresses on
591 #ifdef tc_fix_adjustable
592 if (! tc_fix_adjustable (fixp))
594 fixp->fx_addsy->sy_used_in_reloc = 1;
599 /* If the section symbol isn't going to be output, the relocs
600 at least should still work. If not, figure out what to do
601 when we run into that case. */
602 fixp->fx_offset += S_GET_VALUE (sym);
604 fixp->fx_offset += sym->sy_frag->fr_address;
605 fixp->fx_addsy = section_symbol (symsec);
606 fixp->fx_addsy->sy_used_in_reloc = 1;
608 #if 1/*def RELOC_REQUIRES_SYMBOL*/
611 /* There was no symbol required by this relocation. However,
612 BFD doesn't really handle relocations without symbols well.
613 (At least, the COFF support doesn't.) So for now we fake up
614 a local symbol in the absolute section. */
616 fixp->fx_addsy = section_symbol (absolute_section);
617 fixp->fx_addsy->sy_used_in_reloc = 1;
621 dump_section_relocs (abfd, sec, stderr);
625 write_relocs (abfd, sec, xxx)
630 segment_info_type *seginfo = seg_info (sec);
637 /* If seginfo is NULL, we did not create this section; don't do
642 fixup_segment (seginfo->fix_root, sec);
645 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
648 #ifndef RELOC_EXPANSION_POSSIBLE
649 /* Set up reloc information as well. */
650 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
651 n * sizeof (arelent *));
652 memset ((char*)relocs, 0, n * sizeof (arelent*));
655 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
659 bfd_reloc_status_type s;
666 reloc = tc_gen_reloc (sec, fixp);
672 data = fixp->fx_frag->fr_literal + fixp->fx_where;
673 if (fixp->fx_where + fixp->fx_size
674 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
677 if (reloc->howto->partial_inplace == false
678 && reloc->howto->pcrel_offset == true
679 && reloc->howto->pc_relative == true)
681 /* bfd_perform_relocation screws this up */
682 reloc->addend += reloc->address;
684 /* Pass bogus address so that when bfd_perform_relocation adds
685 `reloc->address' back in, it'll come up with `data', which is where
686 we want it to operate. We can't just do it by fudging reloc->address,
687 since that might be used in the calculations(?). */
688 s = bfd_perform_relocation (stdoutput, reloc, data - reloc->address,
689 sec, stdoutput, &err);
694 case bfd_reloc_overflow:
695 as_bad_where (fixp->fx_file, fixp->fx_line, "relocation overflow");
698 as_fatal ("bad return from bfd_perform_relocation");
703 n = n * MAX_RELOC_EXPANSION;
704 /* Set up reloc information as well. */
705 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
706 n * sizeof (arelent *));
709 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
713 bfd_reloc_status_type s;
721 reloc = tc_gen_reloc (sec, fixp);
723 for (j = 0; reloc[j]; j++)
725 relocs[i++] = reloc[j];
728 data = fixp->fx_frag->fr_literal + fixp->fx_where;
729 if (fixp->fx_where + fixp->fx_size
730 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
732 for (j = 0; reloc[j]; j++)
734 s = bfd_perform_relocation (stdoutput, reloc[j],
735 data - reloc[0]->address,
736 sec, stdoutput, &err);
742 as_fatal ("bad return from bfd_perform_relocation");
753 sympp = bfd_get_outsymbols (stdoutput);
754 nsyms = bfd_get_symcount (stdoutput);
755 for (i = 0; i < n; i++)
756 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
758 for (j = 0; j < nsyms; j++)
759 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
768 bfd_set_reloc (stdoutput, sec, relocs, n);
770 bfd_set_section_flags (abfd, sec,
771 (bfd_get_section_flags (abfd, sec)
772 & (flagword) ~SEC_RELOC));
779 fprintf (stderr, "relocs for sec %s\n", sec->name);
780 for (i = 0; i < n; i++)
784 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
785 i, r, r->address, s->name, r->addend);
792 write_contents (abfd, sec, xxx)
797 segment_info_type *seginfo = seg_info (sec);
798 unsigned long offset = 0;
801 /* Write out the frags. */
803 || ! (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
806 for (f = seginfo->frchainP->frch_root;
811 unsigned long fill_size;
815 assert (f->fr_type == rs_fill);
818 x = bfd_set_section_contents (stdoutput, sec,
819 f->fr_literal, (file_ptr) offset,
820 (bfd_size_type) f->fr_fix);
823 bfd_perror (stdoutput->filename);
824 as_perror ("FATAL: Can't write %s", stdoutput->filename);
829 fill_literal = f->fr_literal + f->fr_fix;
830 fill_size = f->fr_var;
831 count = f->fr_offset;
833 if (fill_size && count)
836 x = bfd_set_section_contents (stdoutput, sec,
837 fill_literal, (file_ptr) offset,
838 (bfd_size_type) fill_size);
841 bfd_perror (stdoutput->filename);
842 as_perror ("FATAL: Can't write %s", stdoutput->filename);
851 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
853 merge_data_into_text ()
855 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
856 seg_info (text_section)->frchainP->frch_last->fr_next =
857 seg_info (data_section)->frchainP->frch_root;
858 seg_info (text_section)->frchainP->frch_last =
859 seg_info (data_section)->frchainP->frch_last;
860 seg_info (data_section)->frchainP = 0;
864 text_last_frag->fr_next = data_frag_root;
865 text_last_frag = data_last_frag;
866 data_last_frag = NULL;
867 data_frag_root = NULL;
870 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
871 tmp->fx_next = data_fix_root;
872 text_fix_tail = data_fix_tail;
875 text_fix_root = data_fix_root;
876 data_fix_root = NULL;
879 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
881 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
883 relax_and_size_all_segments ()
887 relax_segment (text_frag_root, SEG_TEXT);
888 relax_segment (data_frag_root, SEG_DATA);
889 relax_segment (bss_frag_root, SEG_BSS);
891 * Now the addresses of frags are correct within the segment.
894 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
895 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
896 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
899 * Join the 2 segments into 1 huge segment.
900 * To do this, re-compute every rn_address in the SEG_DATA frags.
901 * Then join the data frags after the text frags.
903 * Determine a_data [length of data segment].
907 register relax_addressT slide;
909 know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
911 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
912 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
913 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
915 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
916 /* For b.out: If the data section has a strict alignment
917 requirement, its load address in the .o file will be
918 rounded up from the size of the text section. These
919 two values are *not* the same! Similarly for the bss
921 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
924 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
926 fragP->fr_address += slide;
927 } /* for each data frag */
929 know (text_last_frag != 0);
930 text_last_frag->fr_next = data_frag_root;
934 H_SET_DATA_SIZE (&headers, 0);
938 /* See above comments on b.out data section address. */
941 if (data_last_frag == 0)
942 bss_vma = H_GET_TEXT_SIZE (&headers);
944 bss_vma = data_last_frag->fr_address;
945 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
946 bss_address_frag.fr_address = bss_vma;
948 #else /* ! OBJ_BOUT */
949 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
950 H_GET_DATA_SIZE (&headers));
952 #endif /* ! OBJ_BOUT */
954 /* Slide all the frags */
957 relax_addressT slide = bss_address_frag.fr_address;
959 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
961 fragP->fr_address += slide;
962 } /* for each bss frag */
966 H_SET_BSS_SIZE (&headers,
967 bss_last_frag->fr_address - bss_frag_root->fr_address);
969 H_SET_BSS_SIZE (&headers, 0);
971 #endif /* ! BFD_ASSEMBLER && ! BFD */
973 #if defined (BFD_ASSEMBLER) || !defined (BFD)
982 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
984 /* Count symbols. We can't rely on a count made by the loop in
985 write_object_file, because *_frob_file may add a new symbol or
988 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
995 asympp = (asymbol **) bfd_alloc (stdoutput,
996 nsyms * sizeof (asymbol *));
998 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1000 asympp[i] = symp->bsym;
1006 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1007 assert (result == true);
1008 symbol_table_frozen = 1;
1012 write_object_file ()
1014 struct frchain *frchainP; /* Track along all frchains. */
1015 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1016 fragS *fragP; /* Track along all frags. */
1019 /* Do we really want to write it? */
1021 int n_warns, n_errs;
1022 n_warns = had_warnings ();
1023 n_errs = had_errors ();
1024 /* The -Z flag indicates that an object file should be generated,
1025 regardless of warnings and errors. */
1028 if (n_warns || n_errs)
1029 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
1030 n_errs, n_errs == 1 ? "" : "s",
1031 n_warns, n_warns == 1 ? "" : "s");
1036 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
1037 n_errs, n_errs == 1 ? "" : "s",
1038 n_warns, n_warns == 1 ? "" : "s");
1043 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1044 a routine to check for the definition of the procedure "_main",
1045 and if so -- fix it up so that it can be program entry point. */
1046 VMS_Check_For_Main ();
1049 /* After every sub-segment, we fake an ".align ...". This conforms to
1050 BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
1051 frag that requires least thought. ".align" frags like to have a
1052 following frag since that makes calculating their intended length
1055 @@ Is this really necessary?? */
1056 #ifndef SUB_SEGMENT_ALIGN
1057 #ifdef BFD_ASSEMBLER
1058 #define SUB_SEGMENT_ALIGN(SEG) (0)
1060 #define SUB_SEGMENT_ALIGN(SEG) (2)
1063 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1065 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1066 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
1067 /* frag_align will have left a new frag.
1068 Use this last frag for an empty ".fill".
1070 For this segment ...
1071 Create a last frag. Do not leave a "being filled in frag". */
1072 frag_wane (frag_now);
1073 frag_now->fr_fix = 0;
1074 know (frag_now->fr_next == NULL);
1077 /* From now on, we don't care about sub-segments. Build one frag chain
1078 for each segment. Linked thru fr_next. */
1080 #ifdef BFD_ASSEMBLER
1081 /* Remove the sections created by gas for its own purposes. */
1083 asection **seclist, *sec;
1084 seclist = &stdoutput->sections;
1085 while (seclist && *seclist)
1088 while (sec == reg_section || sec == expr_section)
1092 stdoutput->section_count--;
1097 seclist = &(*seclist)->next;
1101 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1103 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1104 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1105 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1108 /* We have two segments. If user gave -R flag, then we must put the
1109 data frags into the text segment. Do this before relaxing so
1110 we know to take advantage of -R and make shorter addresses. */
1111 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1114 merge_data_into_text ();
1118 #ifdef BFD_ASSEMBLER
1119 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1121 relax_and_size_all_segments ();
1122 #endif /* BFD_ASSEMBLER */
1124 #ifndef BFD_ASSEMBLER
1127 * Crawl the symbol chain.
1129 * For each symbol whose value depends on a frag, take the address of
1130 * that frag and subsume it into the value of the symbol.
1131 * After this, there is just one way to lookup a symbol value.
1132 * Values are left in their final state for object file emission.
1133 * We adjust the values of 'L' local symbols, even if we do
1134 * not intend to emit them to the object file, because their values
1135 * are needed for fix-ups.
1137 * Unless we saw a -L flag, remove all symbols that begin with 'L'
1138 * from the symbol chain. (They are still pointed to by the fixes.)
1140 * Count the remaining symbols.
1141 * Assign a symbol number to each symbol.
1142 * Count the number of string-table chars we will emit.
1143 * Put this info into the headers as appropriate.
1146 know (zero_address_frag.fr_address == 0);
1147 string_byte_count = sizeof (string_byte_count);
1149 obj_crawl_symbol_chain (&headers);
1151 if (string_byte_count == sizeof (string_byte_count))
1152 string_byte_count = 0;
1154 H_SET_STRING_SIZE (&headers, string_byte_count);
1157 * Addresses of frags now reflect addresses we use in the object file.
1158 * Symbol values are correct.
1159 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1160 * Also converting any machine-dependent frags using md_convert_frag();
1162 subseg_change (SEG_TEXT, 0);
1164 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1166 cvt_frag_to_fill (&headers, fragP);
1168 /* Some assert macros don't work with # directives mixed in. */
1170 if (!(fragP->fr_next == NULL
1172 || fragP->fr_next == data_frag_root
1174 || ((fragP->fr_next->fr_address - fragP->fr_address)
1175 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1179 #endif /* ! BFD_ASSEMBLER */
1181 #ifndef WORKING_DOT_WORD
1183 struct broken_word *lie;
1184 struct broken_word **prevP;
1186 prevP = &broken_words;
1187 for (lie = broken_words; lie; lie = lie->next_broken_word)
1192 exp.X_op = O_subtract;
1193 exp.X_add_symbol = lie->add;
1194 exp.X_op_symbol = lie->sub;
1195 exp.X_add_number = lie->addnum;
1196 #ifdef BFD_ASSEMBLER
1197 fix_new_exp (lie->frag,
1198 lie->word_goes_here - lie->frag->fr_literal,
1199 2, &exp, 0, BFD_RELOC_NONE);
1201 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1202 fix_new_exp (lie->frag,
1203 lie->word_goes_here - lie->frag->fr_literal,
1204 2, &exp, 0, NO_RELOC);
1207 fix_new_ns32k_exp (lie->frag,
1208 lie->word_goes_here - lie->frag->fr_literal,
1209 2, &exp, 0, 0, 2, 0, 0);
1211 fix_new_exp (lie->frag,
1212 lie->word_goes_here - lie->frag->fr_literal,
1214 #endif /* TC_NS32K */
1215 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1216 #endif /* BFD_ASSEMBLER */
1217 *prevP = lie->next_broken_word;
1220 prevP = &(lie->next_broken_word);
1222 for (lie = broken_words; lie;)
1224 struct broken_word *untruth;
1226 addressT table_addr;
1227 addressT from_addr, to_addr;
1230 fragP = lie->dispfrag;
1232 /* Find out how many broken_words go here. */
1234 for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1235 if (untruth->added == 1)
1238 table_ptr = lie->dispfrag->fr_opcode;
1239 table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
1240 /* Create the jump around the long jumps. This is a short
1241 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1242 from_addr = table_addr;
1243 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1244 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1245 table_ptr += md_short_jump_size;
1246 table_addr += md_short_jump_size;
1248 for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
1250 if (lie->added == 2)
1252 /* Patch the jump table */
1253 /* This is the offset from ??? to table_ptr+0 */
1254 to_addr = table_addr - S_GET_VALUE (lie->sub);
1255 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1256 for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1258 if (untruth->use_jump == lie)
1259 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1262 /* Install the long jump */
1263 /* this is a long jump from table_ptr+0 to the final target */
1264 from_addr = table_addr;
1265 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1266 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1267 table_ptr += md_long_jump_size;
1268 table_addr += md_long_jump_size;
1272 #endif /* not WORKING_DOT_WORD */
1274 #ifndef BFD_ASSEMBLER
1277 long object_file_size;
1279 * Scan every FixS performing fixups. We had to wait until now to do
1280 * this because md_convert_frag() may have made some fixSs.
1284 subseg_change (SEG_TEXT, 0);
1285 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1286 subseg_change (SEG_DATA, 0);
1287 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1288 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1290 /* FIXME move this stuff into the pre-write-hook */
1291 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1292 H_SET_ENTRY_POINT (&headers, 0);
1294 obj_pre_write_hook (&headers); /* extra coff stuff */
1296 object_file_size = H_GET_FILE_SIZE (&headers);
1297 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1299 output_file_create (out_file_name);
1301 obj_header_append (&next_object_file_charP, &headers);
1303 know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
1308 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1310 register long count;
1311 register char *fill_literal;
1312 register long fill_size;
1314 know (fragP->fr_type == rs_fill);
1315 append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
1316 fill_literal = fragP->fr_literal + fragP->fr_fix;
1317 fill_size = fragP->fr_var;
1318 know (fragP->fr_offset >= 0);
1320 for (count = fragP->fr_offset; count; count--)
1322 append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
1325 } /* for each code frag. */
1327 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
1332 obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
1333 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)));
1335 /* Make addresses in data relocation directives relative to beginning of
1336 * first data fragment, not end of last text fragment: alignment of the
1337 * start of the data segment may place a gap between the segments.
1339 obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
1341 obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
1342 #endif /* TC_I960 */
1344 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)));
1347 * Emit line number entries.
1349 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1350 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)));
1355 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1356 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)));
1362 if (string_byte_count > 0)
1364 obj_emit_strings (&next_object_file_charP);
1365 } /* only if we have a string table */
1368 bfd_seek (stdoutput, 0, 0);
1369 bfd_write (the_object_file, 1, object_file_size, stdoutput);
1372 /* Write the data to the file */
1373 output_file_append (the_object_file, object_file_size, out_file_name);
1375 } /* non vms output */
1378 * Now do the VMS-dependent part of writing the object file
1380 VMS_write_object_file (H_GET_TEXT_SIZE (&headers),
1381 H_GET_DATA_SIZE (&headers),
1382 H_GET_BSS_SIZE (&headers),
1383 text_frag_root, data_frag_root);
1385 #else /* BFD_ASSEMBLER */
1387 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *)0);
1389 /* Set up symbol table, and write it out. */
1394 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1398 if (! symp->sy_resolved)
1400 if (symp->sy_value.X_op == O_constant)
1402 /* This is the normal case; skip the call. */
1405 + symp->sy_frag->fr_address));
1406 symp->sy_resolved = 1;
1409 resolve_symbol_value (symp);
1412 /* So far, common symbols have been treated like undefined symbols.
1413 Put them in the common section now. */
1414 if (S_IS_DEFINED (symp) == 0
1415 && S_GET_VALUE (symp) != 0)
1416 S_SET_SEGMENT (symp, &bfd_com_section);
1418 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1419 S_GET_NAME (symp), symp,
1422 segment_name (symp->bsym->section));
1425 #ifdef obj_frob_symbol
1426 obj_frob_symbol (symp, punt);
1428 #ifdef tc_frob_symbol
1429 if (! punt || symp->sy_used_in_reloc)
1430 tc_frob_symbol (symp, punt);
1433 /* If we don't want to keep this symbol, splice it out of
1434 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1435 want section symbols. Otherwise, we skip local symbols
1436 and symbols that the frob_symbol macros told us to punt,
1437 but we keep such symbols if they are used in relocs. */
1438 if ((! EMIT_SECTION_SYMBOLS
1439 && (symp->bsym->flags & BSF_SECTION_SYM) != 0)
1440 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1441 opposites. Sometimes the former checks flags and the
1442 latter examines the name... */
1443 || (!S_IS_EXTERN (symp)
1444 && (S_IS_LOCAL (symp) || punt)
1445 && ! symp->sy_used_in_reloc))
1447 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1448 /* After symbol_remove, symbol_next(symp) still returns
1449 the one that came after it in the chain. So we don't
1450 need to do any extra cleanup work here. */
1455 /* Make sure we really got a value for the symbol. */
1456 if (! symp->sy_resolved)
1458 as_bad ("can't resolve value for symbol \"%s\"",
1460 symp->sy_resolved = 1;
1463 /* Set the value into the BFD symbol. Up til now the value
1464 has only been kept in the gas symbolS struct. */
1465 symp->bsym->value = S_GET_VALUE (symp);
1469 /* If *_frob_file changes the symbol value at this point, it is
1470 responsible for moving the changed value into symp->bsym->value
1471 as well. Hopefully all symbol value changing can be done in
1476 #ifdef obj_frob_file
1480 /* Now that all the sizes are known, and contents correct, we can
1481 start writing to the file. */
1484 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1486 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1487 #endif /* BFD_ASSEMBLER */
1494 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1499 * After this, all frags in this segment have addresses that are correct
1500 * within the segment. Since segments live in different file addresses,
1501 * these frag addresses may not be the same as final object-file addresses.
1504 #ifndef md_relax_frag
1506 /* Subroutines of relax_segment. */
1512 for (; f1; f1 = f1->fr_next)
1513 if (f1->fr_next == f2)
1518 #endif /* ! defined (md_relax_frag) */
1520 /* Relax_align. Advance location counter to next address that has 'alignment'
1521 lowest order bits all 0s, return size of adjustment made. */
1522 static relax_addressT
1523 relax_align (address, alignment)
1524 register relax_addressT address; /* Address now. */
1525 register int alignment; /* Alignment (binary). */
1527 relax_addressT mask;
1528 relax_addressT new_address;
1530 mask = ~((~0) << alignment);
1531 new_address = (address + mask) & (~mask);
1533 /* We must provide lots of padding, so the linker can discard it
1534 when needed. The linker will not add extra space, ever. */
1535 new_address += (1 << alignment);
1536 return (new_address - address);
1540 relax_segment (segment_frag_root, segment)
1541 struct frag *segment_frag_root;
1544 register struct frag *fragP;
1545 register relax_addressT address;
1546 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1547 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
1549 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1550 subseg_change (segment, 0);
1552 /* For each frag in segment: count and store (a 1st guess of)
1555 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1557 fragP->fr_address = address;
1558 address += fragP->fr_fix;
1560 switch (fragP->fr_type)
1563 address += fragP->fr_offset * fragP->fr_var;
1568 int offset = relax_align (address, (int) fragP->fr_offset);
1569 if (offset % fragP->fr_var != 0)
1571 as_bad ("alignment padding (%d bytes) not a multiple of %ld",
1572 offset, (long) fragP->fr_var);
1573 offset -= (offset % fragP->fr_var);
1580 /* Assume .org is nugatory. It will grow with 1st relax. */
1583 case rs_machine_dependent:
1584 address += md_estimate_size_before_relax (fragP, segment);
1587 #ifndef WORKING_DOT_WORD
1588 /* Broken words don't concern us yet */
1589 case rs_broken_word:
1594 BAD_CASE (fragP->fr_type);
1596 } /* switch(fr_type) */
1597 } /* for each frag in the segment */
1601 long stretch; /* May be any size, 0 or negative. */
1602 /* Cumulative number of addresses we have */
1603 /* relaxed this pass. */
1604 /* We may have relaxed more than one address. */
1605 long stretched; /* Have we stretched on this pass? */
1606 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1607 grew, and another shrank. If a branch instruction doesn't fit anymore,
1608 we could be scrod. */
1612 stretch = stretched = 0;
1613 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1616 unsigned long was_address;
1622 was_address = fragP->fr_address;
1623 address = fragP->fr_address += stretch;
1624 symbolP = fragP->fr_symbol;
1625 offset = fragP->fr_offset;
1627 switch (fragP->fr_type)
1629 case rs_fill: /* .fill never relaxes. */
1633 #ifndef WORKING_DOT_WORD
1634 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1635 for it I do not want to write it. I do not want to have
1636 anything to do with it. This is not the proper way to
1637 implement this misfeature. */
1638 case rs_broken_word:
1640 struct broken_word *lie;
1641 struct broken_word *untruth;
1643 /* Yes this is ugly (storing the broken_word pointer
1644 in the symbol slot). Still, this whole chunk of
1645 code is ugly, and I don't feel like doing anything
1646 about it. Think of it as stubbornness in action. */
1648 for (lie = (struct broken_word *) (fragP->fr_symbol);
1649 lie && lie->dispfrag == fragP;
1650 lie = lie->next_broken_word)
1656 offset = (lie->add->sy_frag->fr_address
1657 + S_GET_VALUE (lie->add)
1659 - (lie->sub->sy_frag->fr_address
1660 + S_GET_VALUE (lie->sub)));
1661 if (offset <= -32768 || offset >= 32767)
1666 sprint_value (buf, (addressT) lie->addnum);
1667 as_warn (".word %s-%s+%s didn't fit",
1668 S_GET_NAME (lie->add),
1669 S_GET_NAME (lie->sub),
1673 if (fragP->fr_subtype == 0)
1675 fragP->fr_subtype++;
1676 growth += md_short_jump_size;
1678 for (untruth = lie->next_broken_word;
1679 untruth && untruth->dispfrag == lie->dispfrag;
1680 untruth = untruth->next_broken_word)
1681 if ((untruth->add->sy_frag == lie->add->sy_frag)
1682 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
1685 untruth->use_jump = lie;
1687 growth += md_long_jump_size;
1692 } /* case rs_broken_word */
1695 growth = (relax_align ((relax_addressT) (address
1698 - relax_align ((relax_addressT) (was_address
1708 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1709 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1710 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1711 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1712 || S_GET_SEGMENT (symbolP) == SEG_BSS);
1713 know (symbolP->sy_frag);
1714 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1715 || (symbolP->sy_frag == &zero_address_frag));
1717 target += S_GET_VALUE (symbolP)
1718 + symbolP->sy_frag->fr_address;
1719 } /* if we have a symbol */
1721 know (fragP->fr_next);
1722 after = fragP->fr_next->fr_address;
1723 growth = ((target - after) > 0) ? (target - after) : 0;
1724 /* Growth may be negative, but variable part of frag
1725 cannot have fewer than 0 chars. That is, we can't
1728 growth -= stretch; /* This is an absolute growth factor */
1731 case rs_machine_dependent:
1732 #ifdef md_relax_frag
1733 growth = md_relax_frag (fragP, stretch);
1735 /* The default way to relax a frag is to look through
1738 const relax_typeS *this_type;
1739 const relax_typeS *start_type;
1740 relax_substateT next_state;
1741 relax_substateT this_state;
1744 this_state = fragP->fr_subtype;
1745 start_type = this_type = md_relax_table + this_state;
1750 #ifndef DIFF_EXPR_OK
1751 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1752 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1753 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1754 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
1755 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
1757 know (symbolP->sy_frag);
1759 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
1760 || symbolP->sy_frag == &zero_address_frag);
1762 S_GET_VALUE (symbolP)
1763 + symbolP->sy_frag->fr_address;
1765 /* If frag has yet to be reached on this pass,
1766 assume it will move by STRETCH just as we did.
1767 If this is not so, it will be because some frag
1768 between grows, and that will force another pass.
1770 Beware zero-length frags.
1772 There should be a faster way to do this. */
1774 if (symbolP->sy_frag->fr_address >= was_address
1775 && is_dnrange (fragP, symbolP->sy_frag))
1781 aim = target - address - fragP->fr_fix;
1782 /* The displacement is affected by the instruction size
1783 for the 32k architecture. I think we ought to be able
1784 to add fragP->fr_pcrel_adjust in all cases (it should be
1785 zero if not used), but just in case it breaks something
1786 else we'll put this inside #ifdef NS32K ... #endif */
1788 if (fragP->fr_pcrel_adjust)
1791 aim += fragP->fr_pcrel_adjust;
1795 /* Look backwards. */
1796 for (next_state = this_type->rlx_more; next_state;)
1797 if (aim >= this_type->rlx_backward)
1801 /* Grow to next state. */
1802 this_state = next_state;
1803 this_type = md_relax_table + this_state;
1804 next_state = this_type->rlx_more;
1809 #ifdef M68K_AIM_KLUDGE
1810 M68K_AIM_KLUDGE (aim, this_state, this_type);
1812 /* Look forwards. */
1813 for (next_state = this_type->rlx_more; next_state;)
1814 if (aim <= this_type->rlx_forward)
1818 /* Grow to next state. */
1819 this_state = next_state;
1820 this_type = md_relax_table + this_state;
1821 next_state = this_type->rlx_more;
1825 growth = this_type->rlx_length - start_type->rlx_length;
1827 fragP->fr_subtype = this_state;
1833 BAD_CASE (fragP->fr_type);
1841 } /* For each frag in the segment. */
1843 while (stretched); /* Until nothing further to relax. */
1847 * We now have valid fr_address'es for each frag.
1851 * All fr_address's are correct, relative to their own segment.
1852 * We have made all the fixS we will ever make.
1854 } /* relax_segment() */
1856 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1860 Go through all the fixS's in a segment and see which ones can be
1861 handled now. (These consist of fixS where we have since discovered
1862 the value of a symbol, or the address of the frag involved.)
1863 For each one, call md_apply_fix to put the fix into the frag data.
1865 Result is a count of how many relocation structs will be needed to
1866 handle the remaining fixS's that we couldn't completely handle here.
1867 These will be output later by emit_relocations(). */
1870 fixup_segment (fixP, this_segment_type)
1871 register fixS *fixP;
1872 segT this_segment_type; /* N_TYPE bits for segment. */
1874 long seg_reloc_count = 0;
1875 symbolS *add_symbolP;
1876 symbolS *sub_symbolP;
1883 segT add_symbol_segment = absolute_section;
1885 /* If the linker is doing the relaxing, we must not do any fixups.
1887 Well, strictly speaking that's not true -- we could do any that are
1888 PC-relative and don't cross regions that could change size. And for the
1889 i960 (the only machine for which we've got a relaxing linker right now),
1890 we might be able to turn callx/callj into bal anyways in cases where we
1891 know the maximum displacement. */
1894 for (; fixP; fixP = fixP->fx_next)
1896 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
1897 return seg_reloc_count;
1900 for (; fixP; fixP = fixP->fx_next)
1902 fragP = fixP->fx_frag;
1904 where = fixP->fx_where;
1905 place = fragP->fr_literal + where;
1906 size = fixP->fx_size;
1907 add_symbolP = fixP->fx_addsy;
1908 #ifdef TC_VALIDATE_FIX
1909 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
1911 sub_symbolP = fixP->fx_subsy;
1912 add_number = fixP->fx_offset;
1913 pcrel = fixP->fx_pcrel;
1916 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
1923 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
1924 add_number -= S_GET_VALUE (sub_symbolP);
1926 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
1928 /* Should try converting to a constant. */
1933 as_bad_where (fixP->fx_file, fixP->fx_line,
1934 "Negative of non-absolute symbol %s",
1935 S_GET_NAME (sub_symbolP));
1937 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
1938 && (SEG_NORMAL (add_symbol_segment)
1939 || (add_symbol_segment == absolute_section)))
1941 /* Difference of 2 symbols from same segment.
1942 Can't make difference of 2 undefineds: 'value' means
1943 something different for N_UNDF. */
1945 /* Makes no sense to use the difference of 2 arbitrary symbols
1946 as the target of a call instruction. */
1948 as_bad_where (fixP->fx_file, fixP->fx_line,
1949 "callj to difference of 2 symbols");
1950 #endif /* TC_I960 */
1951 add_number += S_GET_VALUE (add_symbolP) -
1952 S_GET_VALUE (sub_symbolP);
1956 /* Let the target machine make the final determination
1957 as to whether or not a relocation will be needed to
1958 handle this fixup. */
1959 if (!TC_FORCE_RELOCATION (fixP))
1961 fixP->fx_addsy = NULL;
1966 /* Different segments in subtraction. */
1967 know (!(S_IS_EXTERNAL (sub_symbolP)
1968 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
1970 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
1971 add_number -= S_GET_VALUE (sub_symbolP);
1974 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
1975 #if 0 /* Do this even if it's already described as pc-relative. For example,
1976 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
1977 pc-relative mode. */
1982 /* Make it pc-relative. */
1983 add_number += (md_pcrel_from (fixP)
1984 - S_GET_VALUE (sub_symbolP));
1991 #ifdef BFD_ASSEMBLER
1992 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
1993 || fixP->fx_r_type == BFD_RELOC_GPREL16)
1995 /* Leave it alone. */
2001 sprint_value (buf, fragP->fr_address + where);
2002 as_bad_where (fixP->fx_file, fixP->fx_line,
2003 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
2004 segment_name (S_GET_SEGMENT (sub_symbolP)),
2005 S_GET_NAME (sub_symbolP), buf);
2012 if (add_symbol_segment == this_segment_type && pcrel)
2015 * This fixup was made when the symbol's segment was
2016 * SEG_UNKNOWN, but it is now in the local segment.
2017 * So we know how to do the address without relocation.
2020 /* reloc_callj() may replace a 'call' with a 'calls' or a
2021 'bal', in which cases it modifies *fixP as appropriate.
2022 In the case of a 'calls', no further work is required,
2023 and *fixP has been set up to make the rest of the code
2026 #endif /* TC_I960 */
2028 add_number += S_GET_VALUE (add_symbolP);
2029 add_number -= md_pcrel_from (fixP);
2030 pcrel = 0; /* Lie. Don't want further pcrel processing. */
2032 /* Let the target machine make the final determination
2033 as to whether or not a relocation will be needed to
2034 handle this fixup. */
2035 if (!TC_FORCE_RELOCATION (fixP))
2038 fixP->fx_addsy = NULL;
2043 if (add_symbol_segment == absolute_section)
2046 /* See comment about reloc_callj() above. */
2048 #endif /* TC_I960 */
2049 add_number += S_GET_VALUE (add_symbolP);
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))
2056 fixP->fx_addsy = NULL;
2060 else if (add_symbol_segment == undefined_section
2061 #ifdef BFD_ASSEMBLER
2062 || bfd_is_com_section (add_symbol_segment)
2067 if ((int) fixP->fx_bit_fixP == 13)
2069 /* This is a COBR instruction. They have only a
2070 * 13-bit displacement and are only to be used
2071 * for local branches: flag as error, don't generate
2074 as_bad_where (fixP->fx_file, fixP->fx_line,
2075 "can't use COBR format with external label");
2076 fixP->fx_addsy = NULL;
2080 #endif /* TC_I960 */
2084 if (S_IS_COMMON (add_symbolP))
2085 add_number += S_GET_VALUE (add_symbolP);
2086 #endif /* TE_I386AIX */
2087 #endif /* OBJ_COFF */
2093 add_number += S_GET_VALUE (add_symbolP);
2100 add_number -= md_pcrel_from (fixP);
2101 if (add_symbolP == 0)
2103 #ifndef BFD_ASSEMBLER
2104 fixP->fx_addsy = &abs_symbol;
2106 fixP->fx_addsy = section_symbol (absolute_section);
2108 fixP->fx_addsy->sy_used_in_reloc = 1;
2113 if (!fixP->fx_bit_fixP && size > 0)
2116 if (size < sizeof (mask))
2118 /* set all bits to one */
2120 /* Technically, combining these produces an undefined result
2121 if size is sizeof (valueT), though I think these two
2122 half-way operations should both be defined. And the
2123 compiler should be able to combine them if it's valid on
2124 the host architecture. */
2127 if ((add_number & mask) != 0
2128 && (add_number & mask) != mask)
2130 char buf[50], buf2[50];
2131 sprint_value (buf, fragP->fr_address + where);
2132 if (add_number > 1000)
2133 sprint_value (buf2, add_number);
2135 sprintf (buf2, "%ld", (long) add_number);
2136 as_bad_where (fixP->fx_file, fixP->fx_line,
2137 "Value of %s too large for field of %d bytes at %s",
2139 } /* generic error checking */
2141 #ifdef WARN_SIGNED_OVERFLOW_WORD
2142 /* Warn if a .word value is too large when treated as a signed
2143 number. We already know it is not too negative. This is to
2144 catch over-large switches generated by gcc on the 68k. */
2147 && add_number > 0x7fff)
2148 as_bad_where (fixP->fx_file, fixP->fx_line,
2149 "Signed .word overflow; switch may be too large; %ld at 0x%lx",
2151 (unsigned long) (fragP->fr_address + where));
2153 } /* not a bit fix */
2157 #ifdef BFD_ASSEMBLER
2158 md_apply_fix (fixP, &add_number);
2160 md_apply_fix (fixP, add_number);
2163 #ifndef TC_HANDLES_FX_DONE
2164 /* If the tc-* files haven't been converted, assume it's handling
2165 it the old way, where a null fx_addsy means that the fix has
2166 been applied completely, and no further work is needed. */
2167 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2171 #ifdef TC_VALIDATE_FIX
2175 } /* For each fixS in this segment. */
2177 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2178 return seg_reloc_count;
2181 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
2184 number_to_chars_bigendian (buf, val, n)
2189 if (n > sizeof (val)|| n <= 0)
2193 buf[n] = val & 0xff;
2199 number_to_chars_littleendian (buf, val, n)
2204 if (n > sizeof (val) || n <= 0)
2208 *buf++ = val & 0xff;
2213 /* end of write.c */