1 /* write.c - emit .o file
2 Copyright (C) 1986, 87, 90, 91, 92, 93, 94, 95, 1996
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This thing should be set up to do byteordering correctly. But... */
26 #include "output-file.h"
28 /* This looks like a good idea. Let's try turning it on always, for now. */
29 #undef BFD_FAST_SECTION_FILL
30 #define BFD_FAST_SECTION_FILL
32 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
33 instruction so that the disassembler does not choke on it. */
35 #define NOP_OPCODE 0x00
38 #ifndef TC_ADJUST_RELOC_COUNT
39 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
42 #ifndef TC_FORCE_RELOCATION
43 #define TC_FORCE_RELOCATION(FIXP) 0
46 #ifndef TC_FORCE_RELOCATION_SECTION
47 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
50 #ifndef MD_PCREL_FROM_SECTION
51 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
54 #ifndef WORKING_DOT_WORD
55 extern CONST int md_short_jump_size;
56 extern CONST int md_long_jump_size;
59 int symbol_table_frozen;
60 void print_fixup PARAMS ((fixS *));
63 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
65 /* We generally attach relocs to frag chains. However, after we have
66 chained these all together into a segment, any relocs we add after
67 that must be attached to a segment. This will include relocs added
68 in md_estimate_size_for_relax, for example. */
69 static int frags_chained = 0;
75 struct frag *text_frag_root;
76 struct frag *data_frag_root;
77 struct frag *bss_frag_root;
79 struct frag *text_last_frag; /* Last frag in segment. */
80 struct frag *data_last_frag; /* Last frag in segment. */
81 static struct frag *bss_last_frag; /* Last frag in segment. */
85 static object_headers headers;
88 long string_byte_count;
89 char *next_object_file_charP; /* Tracks object file bytes. */
92 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
95 #endif /* BFD_ASSEMBLER */
100 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
101 symbolS *add, symbolS *sub,
102 offsetT offset, int pcrel,
103 bfd_reloc_code_real_type r_type));
105 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
106 symbolS *add, symbolS *sub,
107 offsetT offset, int pcrel,
110 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
111 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
113 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
118 * Create a fixS in obstack 'notes'.
121 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
123 fragS *frag; /* Which frag? */
124 int where; /* Where in that frag? */
125 int size; /* 1, 2, or 4 usually. */
126 symbolS *add_symbol; /* X_add_symbol. */
127 symbolS *sub_symbol; /* X_op_symbol. */
128 offsetT offset; /* X_add_number. */
129 int pcrel; /* TRUE if PC-relative relocation. */
131 bfd_reloc_code_real_type r_type; /* Relocation type */
133 int r_type; /* Relocation type */
140 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
142 fixP->fx_frag = frag;
143 fixP->fx_where = where;
144 fixP->fx_size = size;
145 /* We've made fx_size a narrow field; check that it's wide enough. */
146 if (fixP->fx_size != size)
148 as_bad ("field fx_size too small to hold %d", size);
151 fixP->fx_addsy = add_symbol;
152 fixP->fx_subsy = sub_symbol;
153 fixP->fx_offset = offset;
154 fixP->fx_pcrel = pcrel;
156 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
157 fixP->fx_r_type = r_type;
159 fixP->fx_im_disp = 0;
160 fixP->fx_pcrel_adjust = 0;
161 fixP->fx_bit_fixP = 0;
162 fixP->fx_addnumber = 0;
167 TC_INIT_FIX_DATA(fixP);
170 as_where (&fixP->fx_file, &fixP->fx_line);
172 /* Usually, we want relocs sorted numerically, but while
173 comparing to older versions of gas that have relocs
174 reverse sorted, it is convenient to have this compile
175 time option. xoxorich. */
180 fixS **seg_fix_rootP = (frags_chained
181 ? &seg_info (now_seg)->fix_root
182 : &frchain_now->fix_root);
183 fixS **seg_fix_tailP = (frags_chained
184 ? &seg_info (now_seg)->fix_tail
185 : &frchain_now->fix_tail);
188 #ifdef REVERSE_SORT_RELOCS
190 fixP->fx_next = *seg_fix_rootP;
191 *seg_fix_rootP = fixP;
193 #else /* REVERSE_SORT_RELOCS */
195 fixP->fx_next = NULL;
198 (*seg_fix_tailP)->fx_next = fixP;
200 *seg_fix_rootP = fixP;
201 *seg_fix_tailP = fixP;
203 #endif /* REVERSE_SORT_RELOCS */
210 /* Create a fixup relative to a symbol (plus a constant). */
213 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
214 fragS *frag; /* Which frag? */
215 int where; /* Where in that frag? */
216 int size; /* 1, 2, or 4 usually. */
217 symbolS *add_symbol; /* X_add_symbol. */
218 offsetT offset; /* X_add_number. */
219 int pcrel; /* TRUE if PC-relative relocation. */
221 bfd_reloc_code_real_type r_type; /* Relocation type */
223 int r_type; /* Relocation type */
226 return fix_new_internal (frag, where, size, add_symbol,
227 (symbolS *) NULL, offset, pcrel, r_type);
230 /* Create a fixup for an expression. Currently we only support fixups
231 for difference expressions. That is itself more than most object
232 file formats support anyhow. */
235 fix_new_exp (frag, where, size, exp, pcrel, r_type)
236 fragS *frag; /* Which frag? */
237 int where; /* Where in that frag? */
238 int size; /* 1, 2, or 4 usually. */
239 expressionS *exp; /* Expression. */
240 int pcrel; /* TRUE if PC-relative relocation. */
242 bfd_reloc_code_real_type r_type; /* Relocation type */
244 int r_type; /* Relocation type */
257 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
258 the difference expression cannot immediately be reduced. */
260 extern symbolS *make_expr_symbol ();
261 symbolS *stmp = make_expr_symbol (exp);
262 exp->X_op = O_symbol;
263 exp->X_op_symbol = 0;
264 exp->X_add_symbol = stmp;
265 exp->X_add_number = 0;
266 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
270 add = exp->X_add_symbol;
271 off = exp->X_add_number;
273 #if defined(BFD_ASSEMBLER)
274 r_type = BFD_RELOC_RVA;
275 #elif defined(TC_RVA_RELOC)
276 r_type = TC_RVA_RELOC;
278 as_fatal("rva not supported");
283 sub = exp->X_add_symbol;
284 off = exp->X_add_number;
288 sub = exp->X_op_symbol;
291 add = exp->X_add_symbol;
294 off = exp->X_add_number;
298 as_bad ("expression too complex for fixup");
301 return fix_new_internal (frag, where, size, add, sub, off,
305 /* Append a string onto another string, bumping the pointer along. */
307 append (charPP, fromP, length)
310 unsigned long length;
312 /* Don't trust memcpy() of 0 chars. */
316 memcpy (*charPP, fromP, length);
320 #ifndef BFD_ASSEMBLER
321 int section_alignment[SEG_MAXIMUM_ORDINAL];
325 * This routine records the largest alignment seen for each segment.
326 * If the beginning of the segment is aligned on the worst-case
327 * boundary, all of the other alignments within it will work. At
328 * least one object format really uses this info.
331 record_alignment (seg, align)
332 /* Segment to which alignment pertains */
334 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
338 if (seg == absolute_section)
341 if (align > bfd_get_section_alignment (stdoutput, seg))
342 bfd_set_section_alignment (stdoutput, seg, align);
344 if (align > section_alignment[(int) seg])
345 section_alignment[(int) seg] = align;
351 /* Reset the section indices after removing the gas created sections. */
354 renumber_sections (abfd, sec, countparg)
359 int *countp = (int *) countparg;
361 sec->index = *countp;
365 #endif /* defined (BFD_ASSEMBLER) */
367 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
370 chain_frchains_together_1 (section, frchp)
372 struct frchain *frchp;
374 fragS dummy, *prev_frag = &dummy;
376 fixS fix_dummy, *prev_fix = &fix_dummy;
379 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
381 prev_frag->fr_next = frchp->frch_root;
382 prev_frag = frchp->frch_last;
383 assert (prev_frag->fr_type != 0);
385 if (frchp->fix_root != (fixS *) NULL)
387 if (seg_info (section)->fix_root == (fixS *) NULL)
388 seg_info (section)->fix_root = frchp->fix_root;
389 prev_fix->fx_next = frchp->fix_root;
390 seg_info (section)->fix_tail = frchp->fix_tail;
391 prev_fix = frchp->fix_tail;
395 assert (prev_frag->fr_type != 0);
396 prev_frag->fr_next = 0;
405 chain_frchains_together (abfd, section, xxx)
406 bfd *abfd; /* unused */
408 PTR xxx; /* unused */
410 segment_info_type *info;
412 /* BFD may have introduced its own sections without using
413 subseg_new, so it is possible that seg_info is NULL. */
414 info = seg_info (section);
415 if (info != (segment_info_type *) NULL)
416 info->frchainP->frch_last
417 = chain_frchains_together_1 (section, info->frchainP);
419 /* Now that we've chained the frags together, we must add new fixups
420 to the segment, not to the frag chain. */
426 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
429 remove_subsegs (head, seg, root, last)
435 *root = head->frch_root;
436 *last = chain_frchains_together_1 (seg, head);
441 #if defined (BFD_ASSEMBLER) || !defined (BFD)
445 cvt_frag_to_fill (sec, fragP)
450 cvt_frag_to_fill (headersP, sec, fragP)
451 object_headers *headersP;
456 switch (fragP->fr_type)
463 HANDLE_ALIGN (fragP);
465 know (fragP->fr_next != NULL);
466 fragP->fr_offset = (fragP->fr_next->fr_address
468 - fragP->fr_fix) / fragP->fr_var;
469 if (fragP->fr_offset < 0)
471 as_bad ("attempt to .org/.space backwards? (%ld)",
472 (long) fragP->fr_offset);
474 fragP->fr_type = rs_fill;
480 case rs_machine_dependent:
482 md_convert_frag (stdoutput, sec, fragP);
484 md_convert_frag (headersP, sec, fragP);
487 assert (fragP->fr_next == NULL || (fragP->fr_next->fr_address - fragP->fr_address == fragP->fr_fix));
490 * After md_convert_frag, we make the frag into a ".space 0".
491 * Md_convert_frag() should set up any fixSs and constants
497 #ifndef WORKING_DOT_WORD
500 struct broken_word *lie;
502 if (fragP->fr_subtype)
504 fragP->fr_fix += md_short_jump_size;
505 for (lie = (struct broken_word *) (fragP->fr_symbol);
506 lie && lie->dispfrag == fragP;
507 lie = lie->next_broken_word)
509 fragP->fr_fix += md_long_jump_size;
517 BAD_CASE (fragP->fr_type);
522 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
526 relax_and_size_seg (abfd, sec, xxx)
533 segment_info_type *seginfo;
535 valueT size, newsize;
537 subseg_change (sec, 0);
539 flags = bfd_get_section_flags (abfd, sec);
541 seginfo = seg_info (sec);
542 if (seginfo && seginfo->frchainP)
544 relax_segment (seginfo->frchainP->frch_root, sec);
545 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
546 cvt_frag_to_fill (sec, fragp);
547 for (fragp = seginfo->frchainP->frch_root;
549 fragp = fragp->fr_next)
550 /* walk to last elt */;
551 size = fragp->fr_address + fragp->fr_fix;
556 if (size > 0 && ! seginfo->bss)
557 flags |= SEC_HAS_CONTENTS;
559 /* @@ This is just an approximation. */
560 if (seginfo && seginfo->fix_root)
564 x = bfd_set_section_flags (abfd, sec, flags);
567 newsize = md_section_align (sec, size);
568 x = bfd_set_section_size (abfd, sec, newsize);
571 /* If the size had to be rounded up, add some padding in the last
573 assert (newsize >= size);
576 fragS *last = seginfo->frchainP->frch_last;
577 fragp = seginfo->frchainP->frch_root;
578 while (fragp->fr_next != last)
579 fragp = fragp->fr_next;
580 last->fr_address = size;
581 fragp->fr_offset += newsize - size;
584 #ifdef tc_frob_section
585 tc_frob_section (sec);
587 #ifdef obj_frob_section
588 obj_frob_section (sec);
594 dump_section_relocs (abfd, sec, stream_)
599 FILE *stream = (FILE *) stream_;
600 segment_info_type *seginfo = seg_info (sec);
601 fixS *fixp = seginfo->fix_root;
606 fprintf (stream, "sec %s relocs:\n", sec->name);
609 symbolS *s = fixp->fx_addsy;
612 fprintf (stream, " %08x: %s(%s", fixp, S_GET_NAME (s),
613 s->bsym->section->name);
614 if (s->bsym->flags & BSF_SECTION_SYM)
616 fprintf (stream, " section sym");
618 fprintf (stream, "+%x", S_GET_VALUE (s));
621 fprintf (stream, "+%x", S_GET_VALUE (s));
622 fprintf (stream, ")+%x\n", fixp->fx_offset);
625 fprintf (stream, " %08x: type %d no sym\n", fixp, fixp->fx_r_type);
626 fixp = fixp->fx_next;
630 #define dump_section_relocs(ABFD,SEC,STREAM) (void)(ABFD,SEC,STREAM)
633 #ifndef EMIT_SECTION_SYMBOLS
634 #define EMIT_SECTION_SYMBOLS 1
638 adjust_reloc_syms (abfd, sec, xxx)
643 segment_info_type *seginfo = seg_info (sec);
649 dump_section_relocs (abfd, sec, stderr);
651 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
654 else if (fixp->fx_addsy)
662 fprintf (stderr, "\n\nadjusting fixup:\n");
666 sym = fixp->fx_addsy;
667 symsec = sym->bsym->section;
669 if (sym != NULL && sym->sy_mri_common)
671 /* These symbols are handled specially in fixup_segment. */
675 /* If it's one of these sections, assume the symbol is
676 definitely going to be output. The code in
677 md_estimate_size_before_relax in tc-mips.c uses this test
678 as well, so if you change this code you should look at that
680 if (bfd_is_und_section (symsec)
681 || bfd_is_abs_section (symsec)
682 || bfd_is_com_section (symsec))
684 fixp->fx_addsy->sy_used_in_reloc = 1;
685 #ifdef UNDEFINED_DIFFERENCE_OK
686 /* We have the difference of an undefined symbol and some
687 other symbol. Make sure to mark the other symbol as used
688 in a relocation so that it will always be output. */
690 fixp->fx_subsy->sy_used_in_reloc = 1;
695 /* Since we're reducing to section symbols, don't attempt to reduce
696 anything that's already using one. */
697 if (sym->bsym->flags & BSF_SECTION_SYM)
699 fixp->fx_addsy->sy_used_in_reloc = 1;
703 /* Is there some other reason we can't adjust this one? (E.g.,
704 call/bal links in i960-bout symbols.) */
705 #ifdef obj_fix_adjustable
706 if (! obj_fix_adjustable (fixp))
708 fixp->fx_addsy->sy_used_in_reloc = 1;
713 /* Is there some other (target cpu dependent) reason we can't adjust
714 this one? (E.g. relocations involving function addresses on
716 #ifdef tc_fix_adjustable
717 if (! tc_fix_adjustable (fixp))
719 fixp->fx_addsy->sy_used_in_reloc = 1;
724 /* For PIC support: We may get expressions like
725 "_GLOBAL_OFFSET_TABLE_+(.-L5)" where "." and "L5" may not
726 necessarily have had a fixed difference initially. But now
727 it should be a known constant, so we can reduce it. Since
728 we can't easily handle a symbol value that looks like
729 someUndefinedSymbol+const, though, we convert the fixup to
730 access the undefined symbol directly, and discard the
731 intermediate symbol. */
732 if (S_GET_SEGMENT (sym) == expr_section
733 && sym->sy_value.X_op == O_add
734 && (resolve_symbol_value (sym->sy_value.X_add_symbol),
735 S_GET_SEGMENT (sym->sy_value.X_add_symbol) == undefined_section)
736 && (resolve_symbol_value (sym->sy_value.X_op_symbol),
737 S_GET_SEGMENT (sym->sy_value.X_op_symbol) == absolute_section))
739 fixp->fx_offset += S_GET_VALUE (sym->sy_value.X_op_symbol);
740 fixp->fx_offset += sym->sy_value.X_add_number;
741 fixp->fx_addsy = sym->sy_value.X_add_symbol;
745 /* If the section symbol isn't going to be output, the relocs
746 at least should still work. If not, figure out what to do
747 when we run into that case. */
748 fixp->fx_offset += S_GET_VALUE (sym);
749 fixp->fx_addsy = section_symbol (symsec);
750 fixp->fx_addsy->sy_used_in_reloc = 1;
755 #if 1/*def RELOC_REQUIRES_SYMBOL*/
758 /* There was no symbol required by this relocation. However,
759 BFD doesn't really handle relocations without symbols well.
760 (At least, the COFF support doesn't.) So for now we fake up
761 a local symbol in the absolute section. */
763 fixp->fx_addsy = section_symbol (absolute_section);
764 /* fixp->fx_addsy->sy_used_in_reloc = 1; */
768 dump_section_relocs (abfd, sec, stderr);
772 write_relocs (abfd, sec, xxx)
777 segment_info_type *seginfo = seg_info (sec);
784 /* If seginfo is NULL, we did not create this section; don't do
789 fixup_segment (seginfo->fix_root, sec);
792 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
795 #ifndef RELOC_EXPANSION_POSSIBLE
796 /* Set up reloc information as well. */
797 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
798 n * sizeof (arelent *));
799 memset ((char*)relocs, 0, n * sizeof (arelent*));
802 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
805 bfd_reloc_status_type s;
814 /* If this is an undefined symbol which was equated to another
815 symbol, then use generate the reloc against the latter symbol
816 rather than the former. */
817 sym = fixp->fx_addsy;
818 while (sym->sy_value.X_op == O_symbol
819 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
820 sym = sym->sy_value.X_add_symbol;
821 fixp->fx_addsy = sym;
823 reloc = tc_gen_reloc (sec, fixp);
829 if (fixp->fx_where + fixp->fx_size
830 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
833 s = bfd_install_relocation (stdoutput, reloc,
834 fixp->fx_frag->fr_literal,
835 fixp->fx_frag->fr_address,
841 case bfd_reloc_overflow:
842 as_bad_where (fixp->fx_file, fixp->fx_line, "relocation overflow");
845 as_fatal ("%s:%u: bad return from bfd_perform_relocation",
846 fixp->fx_file, fixp->fx_line);
851 n = n * MAX_RELOC_EXPANSION;
852 /* Set up reloc information as well. */
853 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
854 n * sizeof (arelent *));
857 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
861 bfd_reloc_status_type s;
871 /* If this is an undefined symbol which was equated to another
872 symbol, then use generate the reloc against the latter symbol
873 rather than the former. */
874 sym = fixp->fx_addsy;
875 while (sym->sy_value.X_op == O_symbol
876 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
877 sym = sym->sy_value.X_add_symbol;
878 fixp->fx_addsy = sym;
880 reloc = tc_gen_reloc (sec, fixp);
882 for (j = 0; reloc[j]; j++)
884 relocs[i++] = reloc[j];
887 data = fixp->fx_frag->fr_literal + fixp->fx_where;
888 if (fixp->fx_where + fixp->fx_size
889 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
891 for (j = 0; reloc[j]; j++)
893 s = bfd_install_relocation (stdoutput, reloc[j],
894 fixp->fx_frag->fr_literal,
895 fixp->fx_frag->fr_address,
901 case bfd_reloc_overflow:
902 as_bad_where (fixp->fx_file, fixp->fx_line,
903 "relocation overflow");
906 as_fatal ("%s:%u: bad return from bfd_perform_relocation",
907 fixp->fx_file, fixp->fx_line);
918 sympp = bfd_get_outsymbols (stdoutput);
919 nsyms = bfd_get_symcount (stdoutput);
920 for (i = 0; i < n; i++)
921 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
923 for (j = 0; j < nsyms; j++)
924 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
933 bfd_set_reloc (stdoutput, sec, relocs, n);
935 bfd_set_section_flags (abfd, sec,
936 (bfd_get_section_flags (abfd, sec)
937 & (flagword) ~SEC_RELOC));
944 fprintf (stderr, "relocs for sec %s\n", sec->name);
945 for (i = 0; i < n; i++)
949 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
950 i, r, r->address, s->name, r->addend);
957 write_contents (abfd, sec, xxx)
962 segment_info_type *seginfo = seg_info (sec);
963 unsigned long offset = 0;
966 /* Write out the frags. */
968 || ! (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
971 for (f = seginfo->frchainP->frch_root;
976 unsigned long fill_size;
980 assert (f->fr_type == rs_fill);
983 x = bfd_set_section_contents (stdoutput, sec,
984 f->fr_literal, (file_ptr) offset,
985 (bfd_size_type) f->fr_fix);
988 bfd_perror (stdoutput->filename);
989 as_perror ("FATAL: Can't write %s", stdoutput->filename);
994 fill_literal = f->fr_literal + f->fr_fix;
995 fill_size = f->fr_var;
996 count = f->fr_offset;
998 if (fill_size && count)
1001 if (fill_size > sizeof(buf))
1003 /* Do it the old way. Can this ever happen? */
1006 x = bfd_set_section_contents (stdoutput, sec,
1009 (bfd_size_type) fill_size);
1012 bfd_perror (stdoutput->filename);
1013 as_perror ("FATAL: Can't write %s", stdoutput->filename);
1014 exit (EXIT_FAILURE);
1016 offset += fill_size;
1021 /* Build a buffer full of fill objects and output it as
1022 often as necessary. This saves on the overhead of
1023 potentially lots of bfd_set_section_contents calls. */
1027 n_per_buf = sizeof (buf);
1028 memset (buf, *fill_literal, n_per_buf);
1033 n_per_buf = sizeof(buf)/fill_size;
1034 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1035 memcpy(bufp, fill_literal, fill_size);
1037 for (; count > 0; count -= n_per_buf)
1039 n_per_buf = n_per_buf > count ? count : n_per_buf;
1040 x = bfd_set_section_contents (stdoutput, sec,
1041 buf, (file_ptr) offset,
1042 (bfd_size_type) n_per_buf * fill_size);
1044 as_fatal ("Cannot write to output file.");
1045 offset += n_per_buf * fill_size;
1053 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1055 merge_data_into_text ()
1057 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1058 seg_info (text_section)->frchainP->frch_last->fr_next =
1059 seg_info (data_section)->frchainP->frch_root;
1060 seg_info (text_section)->frchainP->frch_last =
1061 seg_info (data_section)->frchainP->frch_last;
1062 seg_info (data_section)->frchainP = 0;
1066 text_last_frag->fr_next = data_frag_root;
1067 text_last_frag = data_last_frag;
1068 data_last_frag = NULL;
1069 data_frag_root = NULL;
1072 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1073 tmp->fx_next = data_fix_root;
1074 text_fix_tail = data_fix_tail;
1077 text_fix_root = data_fix_root;
1078 data_fix_root = NULL;
1081 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1083 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1085 relax_and_size_all_segments ()
1089 relax_segment (text_frag_root, SEG_TEXT);
1090 relax_segment (data_frag_root, SEG_DATA);
1091 relax_segment (bss_frag_root, SEG_BSS);
1093 * Now the addresses of frags are correct within the segment.
1096 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1097 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1098 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1101 * Join the 2 segments into 1 huge segment.
1102 * To do this, re-compute every rn_address in the SEG_DATA frags.
1103 * Then join the data frags after the text frags.
1105 * Determine a_data [length of data segment].
1109 register relax_addressT slide;
1111 know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
1113 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1114 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1115 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1117 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1118 /* For b.out: If the data section has a strict alignment
1119 requirement, its load address in the .o file will be
1120 rounded up from the size of the text section. These
1121 two values are *not* the same! Similarly for the bss
1123 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1126 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1128 fragP->fr_address += slide;
1129 } /* for each data frag */
1131 know (text_last_frag != 0);
1132 text_last_frag->fr_next = data_frag_root;
1136 H_SET_DATA_SIZE (&headers, 0);
1140 /* See above comments on b.out data section address. */
1143 if (data_last_frag == 0)
1144 bss_vma = H_GET_TEXT_SIZE (&headers);
1146 bss_vma = data_last_frag->fr_address;
1147 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1148 bss_address_frag.fr_address = bss_vma;
1150 #else /* ! OBJ_BOUT */
1151 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1152 H_GET_DATA_SIZE (&headers));
1154 #endif /* ! OBJ_BOUT */
1156 /* Slide all the frags */
1159 relax_addressT slide = bss_address_frag.fr_address;
1161 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1163 fragP->fr_address += slide;
1164 } /* for each bss frag */
1168 H_SET_BSS_SIZE (&headers,
1169 bss_last_frag->fr_address - bss_frag_root->fr_address);
1171 H_SET_BSS_SIZE (&headers, 0);
1173 #endif /* ! BFD_ASSEMBLER && ! BFD */
1175 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1177 #ifdef BFD_ASSEMBLER
1185 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1187 /* Count symbols. We can't rely on a count made by the loop in
1188 write_object_file, because *_frob_file may add a new symbol or
1191 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1198 asympp = (asymbol **) bfd_alloc (stdoutput,
1199 nsyms * sizeof (asymbol *));
1200 symp = symbol_rootP;
1201 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1203 asympp[i] = symp->bsym;
1209 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1210 assert (result == true);
1211 symbol_table_frozen = 1;
1216 write_object_file ()
1218 struct frchain *frchainP; /* Track along all frchains. */
1219 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1220 fragS *fragP; /* Track along all frags. */
1223 /* Do we really want to write it? */
1225 int n_warns, n_errs;
1226 n_warns = had_warnings ();
1227 n_errs = had_errors ();
1228 /* The -Z flag indicates that an object file should be generated,
1229 regardless of warnings and errors. */
1230 if (flag_always_generate_output)
1232 if (n_warns || n_errs)
1233 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
1234 n_errs, n_errs == 1 ? "" : "s",
1235 n_warns, n_warns == 1 ? "" : "s");
1240 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
1241 n_errs, n_errs == 1 ? "" : "s",
1242 n_warns, n_warns == 1 ? "" : "s");
1247 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1248 a routine to check for the definition of the procedure "_main",
1249 and if so -- fix it up so that it can be program entry point. */
1250 vms_check_for_main ();
1251 #endif /* OBJ_VMS */
1253 /* After every sub-segment, we fake an ".align ...". This conforms to
1254 BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
1255 frag that requires least thought. ".align" frags like to have a
1256 following frag since that makes calculating their intended length
1259 @@ Is this really necessary?? */
1260 #ifndef SUB_SEGMENT_ALIGN
1261 #ifdef BFD_ASSEMBLER
1262 #define SUB_SEGMENT_ALIGN(SEG) (0)
1264 #define SUB_SEGMENT_ALIGN(SEG) (2)
1267 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1269 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1270 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
1271 /* frag_align will have left a new frag.
1272 Use this last frag for an empty ".fill".
1274 For this segment ...
1275 Create a last frag. Do not leave a "being filled in frag". */
1276 frag_wane (frag_now);
1277 frag_now->fr_fix = 0;
1278 know (frag_now->fr_next == NULL);
1281 /* From now on, we don't care about sub-segments. Build one frag chain
1282 for each segment. Linked thru fr_next. */
1284 #ifdef BFD_ASSEMBLER
1285 /* Remove the sections created by gas for its own purposes. */
1287 asection **seclist, *sec;
1290 seclist = &stdoutput->sections;
1291 while (seclist && *seclist)
1294 while (sec == reg_section || sec == expr_section)
1298 stdoutput->section_count--;
1303 seclist = &(*seclist)->next;
1306 bfd_map_over_sections (stdoutput, renumber_sections, &i);
1309 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1311 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1312 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1313 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1316 /* We have two segments. If user gave -R flag, then we must put the
1317 data frags into the text segment. Do this before relaxing so
1318 we know to take advantage of -R and make shorter addresses. */
1319 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1320 if (flag_readonly_data_in_text)
1322 merge_data_into_text ();
1326 #ifdef BFD_ASSEMBLER
1327 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1329 relax_and_size_all_segments ();
1330 #endif /* BFD_ASSEMBLER */
1332 #ifndef BFD_ASSEMBLER
1335 * Crawl the symbol chain.
1337 * For each symbol whose value depends on a frag, take the address of
1338 * that frag and subsume it into the value of the symbol.
1339 * After this, there is just one way to lookup a symbol value.
1340 * Values are left in their final state for object file emission.
1341 * We adjust the values of 'L' local symbols, even if we do
1342 * not intend to emit them to the object file, because their values
1343 * are needed for fix-ups.
1345 * Unless we saw a -L flag, remove all symbols that begin with 'L'
1346 * from the symbol chain. (They are still pointed to by the fixes.)
1348 * Count the remaining symbols.
1349 * Assign a symbol number to each symbol.
1350 * Count the number of string-table chars we will emit.
1351 * Put this info into the headers as appropriate.
1354 know (zero_address_frag.fr_address == 0);
1355 string_byte_count = sizeof (string_byte_count);
1357 obj_crawl_symbol_chain (&headers);
1359 if (string_byte_count == sizeof (string_byte_count))
1360 string_byte_count = 0;
1362 H_SET_STRING_SIZE (&headers, string_byte_count);
1365 * Addresses of frags now reflect addresses we use in the object file.
1366 * Symbol values are correct.
1367 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1368 * Also converting any machine-dependent frags using md_convert_frag();
1370 subseg_change (SEG_TEXT, 0);
1372 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1374 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1376 /* Some assert macros don't work with # directives mixed in. */
1378 if (!(fragP->fr_next == NULL
1380 || fragP->fr_next == data_frag_root
1382 || ((fragP->fr_next->fr_address - fragP->fr_address)
1383 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1387 #endif /* ! BFD_ASSEMBLER */
1389 #ifndef WORKING_DOT_WORD
1391 struct broken_word *lie;
1392 struct broken_word **prevP;
1394 prevP = &broken_words;
1395 for (lie = broken_words; lie; lie = lie->next_broken_word)
1400 exp.X_op = O_subtract;
1401 exp.X_add_symbol = lie->add;
1402 exp.X_op_symbol = lie->sub;
1403 exp.X_add_number = lie->addnum;
1404 #ifdef BFD_ASSEMBLER
1405 #ifdef TC_CONS_FIX_NEW
1406 TC_CONS_FIX_NEW (lie->frag,
1407 lie->word_goes_here - lie->frag->fr_literal,
1410 fix_new_exp (lie->frag,
1411 lie->word_goes_here - lie->frag->fr_literal,
1412 2, &exp, 0, BFD_RELOC_NONE);
1415 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1416 fix_new_exp (lie->frag,
1417 lie->word_goes_here - lie->frag->fr_literal,
1418 2, &exp, 0, NO_RELOC);
1421 fix_new_ns32k_exp (lie->frag,
1422 lie->word_goes_here - lie->frag->fr_literal,
1423 2, &exp, 0, 0, 2, 0, 0);
1425 fix_new_exp (lie->frag,
1426 lie->word_goes_here - lie->frag->fr_literal,
1428 #endif /* TC_NS32K */
1429 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1430 #endif /* BFD_ASSEMBLER */
1431 *prevP = lie->next_broken_word;
1434 prevP = &(lie->next_broken_word);
1436 for (lie = broken_words; lie;)
1438 struct broken_word *untruth;
1440 addressT table_addr;
1441 addressT from_addr, to_addr;
1444 fragP = lie->dispfrag;
1446 /* Find out how many broken_words go here. */
1448 for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1449 if (untruth->added == 1)
1452 table_ptr = lie->dispfrag->fr_opcode;
1453 table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
1454 /* Create the jump around the long jumps. This is a short
1455 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1456 from_addr = table_addr;
1457 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1458 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1459 table_ptr += md_short_jump_size;
1460 table_addr += md_short_jump_size;
1462 for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
1464 if (lie->added == 2)
1466 /* Patch the jump table */
1467 /* This is the offset from ??? to table_ptr+0 */
1468 to_addr = table_addr - S_GET_VALUE (lie->sub);
1469 #ifdef BFD_ASSEMBLER
1470 to_addr -= lie->sub->sy_frag->fr_address;
1472 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1473 for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1475 if (untruth->use_jump == lie)
1476 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1479 /* Install the long jump */
1480 /* this is a long jump from table_ptr+0 to the final target */
1481 from_addr = table_addr;
1482 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1483 #ifdef BFD_ASSEMBLER
1484 to_addr += lie->add->sy_frag->fr_address;
1486 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1487 table_ptr += md_long_jump_size;
1488 table_addr += md_long_jump_size;
1492 #endif /* not WORKING_DOT_WORD */
1494 #ifndef BFD_ASSEMBLER
1497 char *the_object_file;
1498 long object_file_size;
1500 * Scan every FixS performing fixups. We had to wait until now to do
1501 * this because md_convert_frag() may have made some fixSs.
1505 subseg_change (SEG_TEXT, 0);
1506 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1507 subseg_change (SEG_DATA, 0);
1508 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1509 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1511 /* FIXME move this stuff into the pre-write-hook */
1512 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1513 H_SET_ENTRY_POINT (&headers, 0);
1515 obj_pre_write_hook (&headers); /* extra coff stuff */
1517 object_file_size = H_GET_FILE_SIZE (&headers);
1518 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1520 output_file_create (out_file_name);
1522 obj_header_append (&next_object_file_charP, &headers);
1524 know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
1529 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1531 register long count;
1532 register char *fill_literal;
1533 register long fill_size;
1536 know (fragP->fr_type == rs_fill);
1537 append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
1538 fill_literal = fragP->fr_literal + fragP->fr_fix;
1539 fill_size = fragP->fr_var;
1540 know (fragP->fr_offset >= 0);
1542 for (count = fragP->fr_offset; count; count--)
1544 append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
1547 } /* for each code frag. */
1549 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
1554 obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
1555 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)));
1557 /* Make addresses in data relocation directives relative to beginning of
1558 * first data fragment, not end of last text fragment: alignment of the
1559 * start of the data segment may place a gap between the segments.
1561 obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
1563 obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
1564 #endif /* TC_I960 */
1566 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)));
1569 * Emit line number entries.
1571 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1572 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)));
1577 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1578 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)));
1584 if (string_byte_count > 0)
1586 obj_emit_strings (&next_object_file_charP);
1587 } /* only if we have a string table */
1590 bfd_seek (stdoutput, 0, 0);
1591 bfd_write (the_object_file, 1, object_file_size, stdoutput);
1594 /* Write the data to the file */
1595 output_file_append (the_object_file, object_file_size, out_file_name);
1596 free (the_object_file);
1598 } /* non vms output */
1601 * Now do the VMS-dependent part of writing the object file
1603 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1604 H_GET_DATA_SIZE (&headers),
1605 H_GET_BSS_SIZE (&headers),
1606 text_frag_root, data_frag_root);
1607 #endif /* OBJ_VMS */
1608 #else /* BFD_ASSEMBLER */
1610 /* Resolve symbol values. This needs to be done before processing
1616 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1617 if (!symp->sy_resolved)
1618 resolve_symbol_value (symp);
1623 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *)0);
1625 /* Set up symbol table, and write it out. */
1630 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1635 if (symp->sy_mri_common)
1637 if (S_IS_EXTERNAL (symp))
1638 as_bad ("%s: global symbols not supported in common sections",
1640 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1644 name = S_GET_NAME (symp);
1647 const char *name2 = decode_local_label_name ((char *)S_GET_NAME (symp));
1648 /* They only differ if `name' is a fb or dollar local
1650 if (name2 != name && ! S_IS_DEFINED (symp))
1651 as_bad ("local label %s is not defined", name2);
1654 /* Do it again, because adjust_reloc_syms might introduce
1655 more symbols. They'll probably only be section symbols,
1656 but they'll still need to have the values computed. */
1657 if (! symp->sy_resolved)
1659 if (symp->sy_value.X_op == O_constant)
1661 /* This is the normal case; skip the call. */
1664 + symp->sy_frag->fr_address));
1665 symp->sy_resolved = 1;
1668 resolve_symbol_value (symp);
1671 /* Skip symbols which were equated to undefined or common
1673 if (symp->sy_value.X_op == O_symbol
1674 && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
1676 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1680 /* So far, common symbols have been treated like undefined symbols.
1681 Put them in the common section now. */
1682 if (S_IS_DEFINED (symp) == 0
1683 && S_GET_VALUE (symp) != 0)
1684 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1686 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1687 S_GET_NAME (symp), symp,
1690 segment_name (symp->bsym->section));
1693 #ifdef obj_frob_symbol
1694 obj_frob_symbol (symp, punt);
1696 #ifdef tc_frob_symbol
1697 if (! punt || symp->sy_used_in_reloc)
1698 tc_frob_symbol (symp, punt);
1701 /* If we don't want to keep this symbol, splice it out of
1702 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1703 want section symbols. Otherwise, we skip local symbols
1704 and symbols that the frob_symbol macros told us to punt,
1705 but we keep such symbols if they are used in relocs. */
1706 if ((! EMIT_SECTION_SYMBOLS
1707 && (symp->bsym->flags & BSF_SECTION_SYM) != 0)
1708 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1709 opposites. Sometimes the former checks flags and the
1710 latter examines the name... */
1711 || (!S_IS_EXTERN (symp)
1712 && (S_IS_LOCAL (symp) || punt)
1713 && ! symp->sy_used_in_reloc))
1715 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1716 /* After symbol_remove, symbol_next(symp) still returns
1717 the one that came after it in the chain. So we don't
1718 need to do any extra cleanup work here. */
1723 /* Make sure we really got a value for the symbol. */
1724 if (! symp->sy_resolved)
1726 as_bad ("can't resolve value for symbol \"%s\"",
1728 symp->sy_resolved = 1;
1731 /* Set the value into the BFD symbol. Up til now the value
1732 has only been kept in the gas symbolS struct. */
1733 symp->bsym->value = S_GET_VALUE (symp);
1739 /* Now do any format-specific adjustments to the symbol table, such
1740 as adding file symbols. */
1741 #ifdef tc_adjust_symtab
1742 tc_adjust_symtab ();
1744 #ifdef obj_adjust_symtab
1745 obj_adjust_symtab ();
1748 /* Now that all the sizes are known, and contents correct, we can
1749 start writing to the file. */
1752 /* If *_frob_file changes the symbol value at this point, it is
1753 responsible for moving the changed value into symp->bsym->value
1754 as well. Hopefully all symbol value changing can be done in
1759 #ifdef obj_frob_file
1763 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1765 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1766 #endif /* BFD_ASSEMBLER */
1773 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1778 * After this, all frags in this segment have addresses that are correct
1779 * within the segment. Since segments live in different file addresses,
1780 * these frag addresses may not be the same as final object-file addresses.
1783 #ifndef md_relax_frag
1785 /* Subroutines of relax_segment. */
1791 for (; f1; f1 = f1->fr_next)
1792 if (f1->fr_next == f2)
1797 #endif /* ! defined (md_relax_frag) */
1799 /* Relax_align. Advance location counter to next address that has 'alignment'
1800 lowest order bits all 0s, return size of adjustment made. */
1801 static relax_addressT
1802 relax_align (address, alignment)
1803 register relax_addressT address; /* Address now. */
1804 register int alignment; /* Alignment (binary). */
1806 relax_addressT mask;
1807 relax_addressT new_address;
1809 mask = ~((~0) << alignment);
1810 new_address = (address + mask) & (~mask);
1811 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1813 /* We must provide lots of padding, so the linker can discard it
1814 when needed. The linker will not add extra space, ever. */
1815 new_address += (1 << alignment);
1817 return (new_address - address);
1821 relax_segment (segment_frag_root, segment)
1822 struct frag *segment_frag_root;
1825 register struct frag *fragP;
1826 register relax_addressT address;
1827 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1828 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
1830 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1831 subseg_change (segment, 0);
1833 /* For each frag in segment: count and store (a 1st guess of)
1836 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1838 fragP->fr_address = address;
1839 address += fragP->fr_fix;
1841 switch (fragP->fr_type)
1844 address += fragP->fr_offset * fragP->fr_var;
1850 int offset = relax_align (address, (int) fragP->fr_offset);
1851 if (offset % fragP->fr_var != 0)
1853 as_bad ("alignment padding (%d bytes) not a multiple of %ld",
1854 offset, (long) fragP->fr_var);
1855 offset -= (offset % fragP->fr_var);
1863 /* Assume .org is nugatory. It will grow with 1st relax. */
1866 case rs_machine_dependent:
1867 address += md_estimate_size_before_relax (fragP, segment);
1870 #ifndef WORKING_DOT_WORD
1871 /* Broken words don't concern us yet */
1872 case rs_broken_word:
1877 BAD_CASE (fragP->fr_type);
1879 } /* switch(fr_type) */
1880 } /* for each frag in the segment */
1884 long stretch; /* May be any size, 0 or negative. */
1885 /* Cumulative number of addresses we have */
1886 /* relaxed this pass. */
1887 /* We may have relaxed more than one address. */
1888 long stretched; /* Have we stretched on this pass? */
1889 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1890 grew, and another shrank. If a branch instruction doesn't fit anymore,
1891 we could be scrod. */
1895 stretch = stretched = 0;
1896 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1899 unsigned long was_address;
1905 was_address = fragP->fr_address;
1906 address = fragP->fr_address += stretch;
1907 symbolP = fragP->fr_symbol;
1908 offset = fragP->fr_offset;
1910 switch (fragP->fr_type)
1912 case rs_fill: /* .fill never relaxes. */
1916 #ifndef WORKING_DOT_WORD
1917 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1918 for it I do not want to write it. I do not want to have
1919 anything to do with it. This is not the proper way to
1920 implement this misfeature. */
1921 case rs_broken_word:
1923 struct broken_word *lie;
1924 struct broken_word *untruth;
1926 /* Yes this is ugly (storing the broken_word pointer
1927 in the symbol slot). Still, this whole chunk of
1928 code is ugly, and I don't feel like doing anything
1929 about it. Think of it as stubbornness in action. */
1931 for (lie = (struct broken_word *) (fragP->fr_symbol);
1932 lie && lie->dispfrag == fragP;
1933 lie = lie->next_broken_word)
1939 offset = (lie->add->sy_frag->fr_address
1940 + S_GET_VALUE (lie->add)
1942 - (lie->sub->sy_frag->fr_address
1943 + S_GET_VALUE (lie->sub)));
1944 if (offset <= -32768 || offset >= 32767)
1946 if (flag_warn_displacement)
1949 sprint_value (buf, (addressT) lie->addnum);
1950 as_warn (".word %s-%s+%s didn't fit",
1951 S_GET_NAME (lie->add),
1952 S_GET_NAME (lie->sub),
1956 if (fragP->fr_subtype == 0)
1958 fragP->fr_subtype++;
1959 growth += md_short_jump_size;
1961 for (untruth = lie->next_broken_word;
1962 untruth && untruth->dispfrag == lie->dispfrag;
1963 untruth = untruth->next_broken_word)
1964 if ((untruth->add->sy_frag == lie->add->sy_frag)
1965 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
1968 untruth->use_jump = lie;
1970 growth += md_long_jump_size;
1975 } /* case rs_broken_word */
1979 growth = (relax_align ((relax_addressT) (address
1982 - relax_align ((relax_addressT) (was_address
1992 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1993 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1994 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1995 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1996 || S_GET_SEGMENT (symbolP) == SEG_BSS);
1997 know (symbolP->sy_frag);
1998 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1999 || (symbolP->sy_frag == &zero_address_frag));
2001 target += S_GET_VALUE (symbolP)
2002 + symbolP->sy_frag->fr_address;
2003 } /* if we have a symbol */
2005 know (fragP->fr_next);
2006 after = fragP->fr_next->fr_address;
2007 growth = target - after;
2010 /* Growth may be negative, but variable part of frag
2011 cannot have fewer than 0 chars. That is, we can't
2013 as_bad ("attempt to .org backwards ignored");
2017 growth -= stretch; /* This is an absolute growth factor */
2023 growth = S_GET_VALUE (symbolP);
2024 if (symbolP->sy_frag != &zero_address_frag)
2025 as_bad (".space specifies non-absolute value");
2026 fragP->fr_symbol = 0;
2029 as_warn (".space or .fill with negative value, ignored");
2037 case rs_machine_dependent:
2038 #ifdef md_relax_frag
2039 growth = md_relax_frag (fragP, stretch);
2041 #ifdef TC_GENERIC_RELAX_TABLE
2042 /* The default way to relax a frag is to look through
2045 const relax_typeS *this_type;
2046 const relax_typeS *start_type;
2047 relax_substateT next_state;
2048 relax_substateT this_state;
2050 const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
2052 this_state = fragP->fr_subtype;
2053 start_type = this_type = table + this_state;
2058 #ifndef DIFF_EXPR_OK
2059 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2060 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2061 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2062 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2063 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2065 know (symbolP->sy_frag);
2067 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2068 || symbolP->sy_frag == &zero_address_frag);
2070 S_GET_VALUE (symbolP)
2071 + symbolP->sy_frag->fr_address;
2073 /* If frag has yet to be reached on this pass,
2074 assume it will move by STRETCH just as we did.
2075 If this is not so, it will be because some frag
2076 between grows, and that will force another pass.
2078 Beware zero-length frags.
2080 There should be a faster way to do this. */
2082 if (symbolP->sy_frag->fr_address >= was_address
2083 && is_dnrange (fragP, symbolP->sy_frag))
2089 aim = target - address - fragP->fr_fix;
2090 #ifdef TC_PCREL_ADJUST
2091 /* Currently only the ns32k family needs this */
2092 aim += TC_PCREL_ADJUST(fragP);
2094 /* This machine doesn't want to use pcrel_adjust.
2095 In that case, pcrel_adjust should be zero. */
2096 assert (fragP->fr_pcrel_adjust == 0);
2101 /* Look backwards. */
2102 for (next_state = this_type->rlx_more; next_state;)
2103 if (aim >= this_type->rlx_backward)
2107 /* Grow to next state. */
2108 this_state = next_state;
2109 this_type = table + this_state;
2110 next_state = this_type->rlx_more;
2115 #ifdef M68K_AIM_KLUDGE
2116 M68K_AIM_KLUDGE (aim, this_state, this_type);
2118 /* Look forwards. */
2119 for (next_state = this_type->rlx_more; next_state;)
2120 if (aim <= this_type->rlx_forward)
2124 /* Grow to next state. */
2125 this_state = next_state;
2126 this_type = table + this_state;
2127 next_state = this_type->rlx_more;
2131 growth = this_type->rlx_length - start_type->rlx_length;
2133 fragP->fr_subtype = this_state;
2135 #endif /* TC_GENERIC_RELAX_TABLE */
2140 BAD_CASE (fragP->fr_type);
2148 } /* For each frag in the segment. */
2150 while (stretched); /* Until nothing further to relax. */
2154 * We now have valid fr_address'es for each frag.
2158 * All fr_address's are correct, relative to their own segment.
2159 * We have made all the fixS we will ever make.
2161 } /* relax_segment() */
2163 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2165 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2166 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2171 Go through all the fixS's in a segment and see which ones can be
2172 handled now. (These consist of fixS where we have since discovered
2173 the value of a symbol, or the address of the frag involved.)
2174 For each one, call md_apply_fix to put the fix into the frag data.
2176 Result is a count of how many relocation structs will be needed to
2177 handle the remaining fixS's that we couldn't completely handle here.
2178 These will be output later by emit_relocations(). */
2181 fixup_segment (fixP, this_segment_type)
2182 register fixS *fixP;
2183 segT this_segment_type; /* N_TYPE bits for segment. */
2185 long seg_reloc_count = 0;
2186 symbolS *add_symbolP;
2187 symbolS *sub_symbolP;
2194 segT add_symbol_segment = absolute_section;
2196 /* If the linker is doing the relaxing, we must not do any fixups.
2198 Well, strictly speaking that's not true -- we could do any that are
2199 PC-relative and don't cross regions that could change size. And for the
2200 i960 (the only machine for which we've got a relaxing linker right now),
2201 we might be able to turn callx/callj into bal anyways in cases where we
2202 know the maximum displacement. */
2205 for (; fixP; fixP = fixP->fx_next)
2207 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2208 return seg_reloc_count;
2211 for (; fixP; fixP = fixP->fx_next)
2214 fprintf (stderr, "\nprocessing fixup:\n");
2218 fragP = fixP->fx_frag;
2220 where = fixP->fx_where;
2221 place = fragP->fr_literal + where;
2222 size = fixP->fx_size;
2223 add_symbolP = fixP->fx_addsy;
2224 #ifdef TC_VALIDATE_FIX
2225 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2227 sub_symbolP = fixP->fx_subsy;
2228 add_number = fixP->fx_offset;
2229 pcrel = fixP->fx_pcrel;
2232 if (add_symbolP != NULL
2233 && add_symbolP->sy_mri_common)
2235 know (add_symbolP->sy_value.X_op == O_symbol);
2236 add_number += S_GET_VALUE (add_symbolP);
2237 fixP->fx_offset = add_number;
2238 add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
2242 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2246 resolve_symbol_value (sub_symbolP);
2247 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2249 if (add_symbolP != NULL)
2251 add_number += S_GET_VALUE (add_symbolP);
2253 fixP->fx_addsy = NULL;
2256 /* It's just -sym */
2257 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2259 add_number -= S_GET_VALUE (sub_symbolP);
2260 fixP->fx_subsy = NULL;
2263 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2265 /* Should try converting to a constant. */
2270 as_bad_where (fixP->fx_file, fixP->fx_line,
2271 "Negative of non-absolute symbol %s",
2272 S_GET_NAME (sub_symbolP));
2274 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2275 && SEG_NORMAL (add_symbol_segment))
2277 /* Difference of 2 symbols from same segment.
2278 Can't make difference of 2 undefineds: 'value' means
2279 something different for N_UNDF. */
2281 /* Makes no sense to use the difference of 2 arbitrary symbols
2282 as the target of a call instruction. */
2284 as_bad_where (fixP->fx_file, fixP->fx_line,
2285 "callj to difference of 2 symbols");
2286 #endif /* TC_I960 */
2287 add_number += S_GET_VALUE (add_symbolP) -
2288 S_GET_VALUE (sub_symbolP);
2291 pcrel = 0; /* No further pcrel processing. */
2293 /* Let the target machine make the final determination
2294 as to whether or not a relocation will be needed to
2295 handle this fixup. */
2296 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2299 fixP->fx_addsy = NULL;
2300 fixP->fx_subsy = NULL;
2305 /* Different segments in subtraction. */
2306 know (!(S_IS_EXTERNAL (sub_symbolP)
2307 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2309 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2310 add_number -= S_GET_VALUE (sub_symbolP);
2313 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2314 #if 0 /* Do this even if it's already described as pc-relative. For example,
2315 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
2316 pc-relative mode. */
2321 /* Make it pc-relative. */
2322 add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
2323 - S_GET_VALUE (sub_symbolP));
2330 #ifdef UNDEFINED_DIFFERENCE_OK
2331 /* The PA needs this for PIC code generation. We basically
2332 don't want to do anything if we have the difference of two
2333 symbols at this point. */
2336 /* Leave it alone. */
2339 #ifdef BFD_ASSEMBLER
2340 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2341 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2343 /* Leave it alone. */
2349 sprint_value (buf, fragP->fr_address + where);
2350 as_bad_where (fixP->fx_file, fixP->fx_line,
2351 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
2352 segment_name (S_GET_SEGMENT (sub_symbolP)),
2353 S_GET_NAME (sub_symbolP), buf);
2360 if (add_symbol_segment == this_segment_type && pcrel && !plt
2361 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2364 * This fixup was made when the symbol's segment was
2365 * SEG_UNKNOWN, but it is now in the local segment.
2366 * So we know how to do the address without relocation.
2369 /* reloc_callj() may replace a 'call' with a 'calls' or a
2370 'bal', in which cases it modifies *fixP as appropriate.
2371 In the case of a 'calls', no further work is required,
2372 and *fixP has been set up to make the rest of the code
2375 #endif /* TC_I960 */
2377 add_number += S_GET_VALUE (add_symbolP);
2378 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2379 pcrel = 0; /* Lie. Don't want further pcrel processing. */
2381 /* Let the target machine make the final determination
2382 as to whether or not a relocation will be needed to
2383 handle this fixup. */
2384 if (!TC_FORCE_RELOCATION (fixP))
2387 fixP->fx_addsy = NULL;
2392 if (add_symbol_segment == absolute_section)
2395 /* See comment about reloc_callj() above. */
2397 #endif /* TC_I960 */
2398 add_number += S_GET_VALUE (add_symbolP);
2400 /* Let the target machine make the final determination
2401 as to whether or not a relocation will be needed to
2402 handle this fixup. */
2404 if (!TC_FORCE_RELOCATION (fixP))
2406 fixP->fx_addsy = NULL;
2410 else if (add_symbol_segment == undefined_section
2411 #ifdef BFD_ASSEMBLER
2412 || bfd_is_com_section (add_symbol_segment)
2417 if ((int) fixP->fx_bit_fixP == 13)
2419 /* This is a COBR instruction. They have only a
2420 * 13-bit displacement and are only to be used
2421 * for local branches: flag as error, don't generate
2424 as_bad_where (fixP->fx_file, fixP->fx_line,
2425 "can't use COBR format with external label");
2426 fixP->fx_addsy = NULL;
2430 #endif /* TC_I960 */
2434 if (S_IS_COMMON (add_symbolP))
2435 add_number += S_GET_VALUE (add_symbolP);
2436 #endif /* TE_I386AIX */
2437 #endif /* OBJ_COFF */
2443 #if !(defined (TC_M68K) && defined (OBJ_ELF))
2444 #if !defined (TC_I386) || !(defined (OBJ_ELF) || defined (OBJ_COFF))
2445 add_number += S_GET_VALUE (add_symbolP);
2454 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2455 if (add_symbolP == 0)
2457 #ifndef BFD_ASSEMBLER
2458 fixP->fx_addsy = &abs_symbol;
2460 fixP->fx_addsy = section_symbol (absolute_section);
2462 fixP->fx_addsy->sy_used_in_reloc = 1;
2467 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2470 if (size < sizeof (mask))
2472 /* set all bits to one */
2474 /* Technically, combining these produces an undefined result
2475 if size is sizeof (valueT), though I think these two
2476 half-way operations should both be defined. And the
2477 compiler should be able to combine them if it's valid on
2478 the host architecture. */
2481 if ((add_number & mask) != 0
2482 && (add_number & mask) != mask)
2484 char buf[50], buf2[50];
2485 sprint_value (buf, fragP->fr_address + where);
2486 if (add_number > 1000)
2487 sprint_value (buf2, add_number);
2489 sprintf (buf2, "%ld", (long) add_number);
2490 as_bad_where (fixP->fx_file, fixP->fx_line,
2491 "Value of %s too large for field of %d bytes at %s",
2493 } /* generic error checking */
2495 #ifdef WARN_SIGNED_OVERFLOW_WORD
2496 /* Warn if a .word value is too large when treated as a signed
2497 number. We already know it is not too negative. This is to
2498 catch over-large switches generated by gcc on the 68k. */
2499 if (!flag_signed_overflow_ok
2501 && add_number > 0x7fff)
2502 as_bad_where (fixP->fx_file, fixP->fx_line,
2503 "Signed .word overflow; switch may be too large; %ld at 0x%lx",
2505 (unsigned long) (fragP->fr_address + where));
2507 } /* not a bit fix */
2511 #ifdef MD_APPLY_FIX3
2512 md_apply_fix3 (fixP, &add_number, this_segment_type);
2514 #ifdef BFD_ASSEMBLER
2515 md_apply_fix (fixP, &add_number);
2517 md_apply_fix (fixP, add_number);
2521 #ifndef TC_HANDLES_FX_DONE
2522 /* If the tc-* files haven't been converted, assume it's handling
2523 it the old way, where a null fx_addsy means that the fix has
2524 been applied completely, and no further work is needed. */
2525 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2529 #ifdef TC_VALIDATE_FIX
2533 fprintf (stderr, "result:\n");
2536 } /* For each fixS in this segment. */
2538 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2539 return seg_reloc_count;
2542 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2545 number_to_chars_bigendian (buf, val, n)
2550 if (n > sizeof (val)|| n <= 0)
2554 buf[n] = val & 0xff;
2560 number_to_chars_littleendian (buf, val, n)
2565 if (n > sizeof (val) || n <= 0)
2569 *buf++ = val & 0xff;
2575 write_print_statistics (file)
2578 fprintf (stderr, "fixups: %d\n", n_fixups);
2582 extern int indent_level;
2583 extern void print_symbol_value_1 ();
2590 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2592 fprintf (stderr, " pcrel");
2593 if (fixp->fx_pcrel_adjust)
2594 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2595 if (fixp->fx_im_disp)
2598 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2600 fprintf (stderr, " im_disp");
2604 fprintf (stderr, " tcbit");
2606 fprintf (stderr, " done");
2607 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2608 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2609 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2610 #ifdef BFD_ASSEMBLER
2611 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2614 #ifdef NEED_FX_R_TYPE
2615 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2620 fprintf (stderr, "\n +<");
2621 print_symbol_value_1 (stderr, fixp->fx_addsy);
2622 fprintf (stderr, ">");
2626 fprintf (stderr, "\n -<");
2627 print_symbol_value_1 (stderr, fixp->fx_subsy);
2628 fprintf (stderr, ">");
2630 fprintf (stderr, "\n");
2633 /* end of write.c */