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 /* This looks like a good idea. Let's try turning it on always, for now. */
28 #undef BFD_FAST_SECTION_FILL
29 #define BFD_FAST_SECTION_FILL
31 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
32 instruction so that the disassembler does not choke on it. */
34 #define NOP_OPCODE 0x00
37 #ifndef TC_ADJUST_RELOC_COUNT
38 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
41 #ifndef TC_FORCE_RELOCATION
42 #define TC_FORCE_RELOCATION(FIXP) 0
45 #ifndef WORKING_DOT_WORD
46 extern CONST int md_short_jump_size;
47 extern CONST int md_long_jump_size;
50 int symbol_table_frozen;
53 /* We generally attach relocs to frag chains. However, after we have
54 chained these all together into a segment, any relocs we add after
55 that must be attached to a segment. This will include relocs added
56 in md_estimate_size_for_relax, for example. */
57 static int frags_chained = 0;
63 struct frag *text_frag_root;
64 struct frag *data_frag_root;
65 struct frag *bss_frag_root;
67 struct frag *text_last_frag; /* Last frag in segment. */
68 struct frag *data_last_frag; /* Last frag in segment. */
69 static struct frag *bss_last_frag; /* Last frag in segment. */
73 static object_headers headers;
74 static char *the_object_file;
77 long string_byte_count;
78 char *next_object_file_charP; /* Tracks object file bytes. */
81 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
84 #endif /* BFD_ASSEMBLER */
87 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
88 symbolS *add, symbolS *sub,
89 offsetT offset, int pcrel,
90 bfd_reloc_code_real_type r_type));
92 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
93 symbolS *add, symbolS *sub,
94 offsetT offset, int pcrel,
97 #if defined (BFD_ASSEMBLER) || !defined (BFD)
98 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
100 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
105 * Create a fixS in obstack 'notes'.
108 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
110 fragS *frag; /* Which frag? */
111 int where; /* Where in that frag? */
112 int size; /* 1, 2, or 4 usually. */
113 symbolS *add_symbol; /* X_add_symbol. */
114 symbolS *sub_symbol; /* X_op_symbol. */
115 offsetT offset; /* X_add_number. */
116 int pcrel; /* TRUE if PC-relative relocation. */
118 bfd_reloc_code_real_type r_type; /* Relocation type */
120 int r_type; /* Relocation type */
125 fixP = (fixS *) obstack_alloc (¬es, sizeof (fixS));
127 fixP->fx_frag = frag;
128 fixP->fx_where = where;
129 fixP->fx_size = size;
130 fixP->fx_addsy = add_symbol;
131 fixP->fx_subsy = sub_symbol;
132 fixP->fx_offset = offset;
133 fixP->fx_pcrel = pcrel;
134 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
135 fixP->fx_r_type = r_type;
137 fixP->fx_im_disp = 0;
138 fixP->fx_pcrel_adjust = 0;
139 fixP->fx_bit_fixP = 0;
140 fixP->fx_addnumber = 0;
141 fixP->tc_fix_data = NULL;
149 as_where (&fixP->fx_file, &fixP->fx_line);
151 /* Usually, we want relocs sorted numerically, but while
152 comparing to older versions of gas that have relocs
153 reverse sorted, it is convenient to have this compile
154 time option. xoxorich. */
159 fixS **seg_fix_rootP = (frags_chained
160 ? &seg_info (now_seg)->fix_root
161 : &frchain_now->fix_root);
162 fixS **seg_fix_tailP = (frags_chained
163 ? &seg_info (now_seg)->fix_tail
164 : &frchain_now->fix_tail);
167 #ifdef REVERSE_SORT_RELOCS
169 fixP->fx_next = *seg_fix_rootP;
170 *seg_fix_rootP = fixP;
172 #else /* REVERSE_SORT_RELOCS */
174 fixP->fx_next = NULL;
177 (*seg_fix_tailP)->fx_next = fixP;
179 *seg_fix_rootP = fixP;
180 *seg_fix_tailP = fixP;
182 #endif /* REVERSE_SORT_RELOCS */
189 /* Create a fixup relative to a symbol (plus a constant). */
192 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
193 fragS *frag; /* Which frag? */
194 int where; /* Where in that frag? */
195 short int size; /* 1, 2, or 4 usually. */
196 symbolS *add_symbol; /* X_add_symbol. */
197 offsetT offset; /* X_add_number. */
198 int pcrel; /* TRUE if PC-relative relocation. */
200 bfd_reloc_code_real_type r_type; /* Relocation type */
202 int r_type; /* Relocation type */
205 return fix_new_internal (frag, where, size, add_symbol,
206 (symbolS *) NULL, offset, pcrel, r_type);
209 /* Create a fixup for an expression. Currently we only support fixups
210 for difference expressions. That is itself more than most object
211 file formats support anyhow. */
214 fix_new_exp (frag, where, size, exp, pcrel, r_type)
215 fragS *frag; /* Which frag? */
216 int where; /* Where in that frag? */
217 short int size; /* 1, 2, or 4 usually. */
218 expressionS *exp; /* Expression. */
219 int pcrel; /* TRUE if PC-relative relocation. */
221 bfd_reloc_code_real_type r_type; /* Relocation type */
223 int r_type; /* Relocation type */
236 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
237 the difference expression cannot immediately be reduced. */
239 extern symbolS *make_expr_symbol ();
240 symbolS *stmp = make_expr_symbol (exp);
241 exp->X_op = O_symbol;
242 exp->X_op_symbol = 0;
243 exp->X_add_symbol = stmp;
244 exp->X_add_number = 0;
245 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
249 sub = exp->X_add_symbol;
250 off = exp->X_add_number;
254 sub = exp->X_op_symbol;
257 add = exp->X_add_symbol;
260 off = exp->X_add_number;
264 as_bad ("expression too complex for fixup");
267 return fix_new_internal (frag, where, size, add, sub, off,
271 /* Append a string onto another string, bumping the pointer along. */
273 append (charPP, fromP, length)
276 unsigned long length;
278 /* Don't trust memcpy() of 0 chars. */
282 memcpy (*charPP, fromP, length);
286 #ifndef BFD_ASSEMBLER
287 int section_alignment[SEG_MAXIMUM_ORDINAL];
291 * This routine records the largest alignment seen for each segment.
292 * If the beginning of the segment is aligned on the worst-case
293 * boundary, all of the other alignments within it will work. At
294 * least one object format really uses this info.
297 record_alignment (seg, align)
298 /* Segment to which alignment pertains */
300 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
305 if (align > bfd_get_section_alignment (stdoutput, seg))
306 bfd_set_section_alignment (stdoutput, seg, align);
308 if (align > section_alignment[(int) seg])
309 section_alignment[(int) seg] = align;
313 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
316 chain_frchains_together_1 (section, frchp)
318 struct frchain *frchp;
320 fragS dummy, *prev_frag = &dummy;
321 fixS fix_dummy, *prev_fix = &fix_dummy;
323 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
325 prev_frag->fr_next = frchp->frch_root;
326 prev_frag = frchp->frch_last;
328 if (frchp->fix_root != (fixS *) NULL)
330 if (seg_info (section)->fix_root == (fixS *) NULL)
331 seg_info (section)->fix_root = frchp->fix_root;
332 prev_fix->fx_next = frchp->fix_root;
333 seg_info (section)->fix_tail = frchp->fix_tail;
334 prev_fix = frchp->fix_tail;
338 prev_frag->fr_next = 0;
347 chain_frchains_together (abfd, section, xxx)
348 bfd *abfd; /* unused */
350 PTR xxx; /* unused */
352 segment_info_type *info;
354 /* BFD may have introduced its own sections without using
355 subseg_new, so it is possible that seg_info is NULL. */
356 info = seg_info (section);
357 if (info != (segment_info_type *) NULL)
358 info->frchainP->frch_last
359 = chain_frchains_together_1 (section, info->frchainP);
361 /* Now that we've chained the frags together, we must add new fixups
362 to the segment, not to the frag chain. */
368 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
371 remove_subsegs (head, seg, root, last)
377 *root = head->frch_root;
378 *last = chain_frchains_together_1 (seg, head);
383 #if defined (BFD_ASSEMBLER) || !defined (BFD)
387 cvt_frag_to_fill (sec, fragP)
392 cvt_frag_to_fill (headers, fragP)
393 object_headers *headers;
397 switch (fragP->fr_type)
402 HANDLE_ALIGN (fragP);
404 fragP->fr_type = rs_fill;
405 know (fragP->fr_next != NULL);
406 fragP->fr_offset = (fragP->fr_next->fr_address
408 - fragP->fr_fix) / fragP->fr_var;
414 case rs_machine_dependent:
416 md_convert_frag (stdoutput, sec, fragP);
418 md_convert_frag (headers, fragP);
421 assert (fragP->fr_next == NULL || (fragP->fr_next->fr_address - fragP->fr_address == fragP->fr_fix));
424 * After md_convert_frag, we make the frag into a ".space 0".
425 * Md_convert_frag() should set up any fixSs and constants
431 #ifndef WORKING_DOT_WORD
434 struct broken_word *lie;
436 if (fragP->fr_subtype)
438 fragP->fr_fix += md_short_jump_size;
439 for (lie = (struct broken_word *) (fragP->fr_symbol);
440 lie && lie->dispfrag == fragP;
441 lie = lie->next_broken_word)
443 fragP->fr_fix += md_long_jump_size;
451 BAD_CASE (fragP->fr_type);
456 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
460 relax_and_size_seg (abfd, sec, xxx)
467 segment_info_type *seginfo;
469 valueT size, newsize;
471 flags = bfd_get_section_flags (abfd, sec);
473 seginfo = seg_info (sec);
474 if (seginfo && seginfo->frchainP)
476 relax_segment (seginfo->frchainP->frch_root, sec);
477 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
478 cvt_frag_to_fill (sec, fragp);
479 for (fragp = seginfo->frchainP->frch_root;
481 fragp = fragp->fr_next)
482 /* walk to last elt */;
483 size = fragp->fr_address + fragp->fr_fix;
488 if (size > 0 && ! seginfo->bss)
489 flags |= SEC_HAS_CONTENTS;
491 /* @@ This is just an approximation. */
492 if (seginfo && seginfo->fix_root)
496 x = bfd_set_section_flags (abfd, sec, flags);
499 newsize = md_section_align (sec, size);
500 x = bfd_set_section_size (abfd, sec, newsize);
503 /* If the size had to be rounded up, add some padding in the last
505 assert (newsize >= size);
508 fragS *last = seginfo->frchainP->frch_last;
509 fragp = seginfo->frchainP->frch_root;
510 while (fragp->fr_next != last)
511 fragp = fragp->fr_next;
512 last->fr_address = size;
513 fragp->fr_offset += newsize - size;
516 #ifdef tc_frob_section
517 tc_frob_section (sec);
519 #ifdef obj_frob_section
520 obj_frob_section (sec);
526 dump_section_relocs (abfd, sec, stream_)
531 FILE *stream = (FILE *) stream_;
532 segment_info_type *seginfo = seg_info (sec);
533 fixS *fixp = seginfo->fix_root;
538 fprintf (stream, "sec %s relocs:\n", sec->name);
541 symbolS *s = fixp->fx_addsy;
544 fprintf (stream, " %08x: %s(%s", fixp, S_GET_NAME (s),
545 s->bsym->section->name);
546 if (s->bsym->flags & BSF_SECTION_SYM)
548 fprintf (stream, " section sym");
550 fprintf (stream, "+%x", S_GET_VALUE (s));
553 fprintf (stream, "+%x", S_GET_VALUE (s));
554 fprintf (stream, ")+%x\n", fixp->fx_offset);
557 fprintf (stream, " %08x: type %d no sym\n", fixp, fixp->fx_r_type);
558 fixp = fixp->fx_next;
562 #define dump_section_relocs(ABFD,SEC,STREAM) (void)(ABFD,SEC,STREAM)
565 #ifndef EMIT_SECTION_SYMBOLS
566 #define EMIT_SECTION_SYMBOLS 1
570 adjust_reloc_syms (abfd, sec, xxx)
575 segment_info_type *seginfo = seg_info (sec);
581 dump_section_relocs (abfd, sec, stderr);
583 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
586 else if (fixp->fx_addsy)
588 symbolS *sym = fixp->fx_addsy;
593 symsec = sym->bsym->section;
595 /* If it's one of these sections, assume the symbol is
596 definitely going to be output. The code in
597 md_estimate_size_before_relax in tc-mips.c uses this test
598 as well, so if you change this code you should look at that
600 if (symsec == &bfd_und_section
601 || symsec == &bfd_abs_section
602 || bfd_is_com_section (symsec))
604 fixp->fx_addsy->sy_used_in_reloc = 1;
608 /* Since we're reducing to section symbols, don't attempt to reduce
609 anything that's already using one. */
610 if (sym->bsym->flags & BSF_SECTION_SYM)
612 fixp->fx_addsy->sy_used_in_reloc = 1;
616 /* Is there some other reason we can't adjust this one? (E.g.,
617 call/bal links in i960-bout symbols.) */
618 #ifdef obj_fix_adjustable
619 if (! obj_fix_adjustable (fixp))
621 fixp->fx_addsy->sy_used_in_reloc = 1;
626 /* Is there some other (target cpu dependent) reason we can't adjust
627 this one? (E.g. relocations involving function addresses on
629 #ifdef tc_fix_adjustable
630 if (! tc_fix_adjustable (fixp))
632 fixp->fx_addsy->sy_used_in_reloc = 1;
637 /* For PIC support: We may get expressions like
638 "_GLOBAL_OFFSET_TABLE_+(.-L5)" where "." and "L5" may not
639 necessarily have had a fixed difference initially. But now
640 it should be a known constant, so we can reduce it. Since
641 we can't easily handle a symbol value that looks like
642 someUndefinedSymbol+const, though, we convert the fixup to
643 access the undefined symbol directly, and discard the
644 intermediate symbol. */
645 if (S_GET_SEGMENT (sym) == expr_section
646 && sym->sy_value.X_op == O_add
647 && (resolve_symbol_value (sym->sy_value.X_add_symbol),
648 S_GET_SEGMENT (sym->sy_value.X_add_symbol) == undefined_section)
649 && (resolve_symbol_value (sym->sy_value.X_op_symbol),
650 S_GET_SEGMENT (sym->sy_value.X_op_symbol) == absolute_section))
652 fixp->fx_offset += S_GET_VALUE (sym->sy_value.X_op_symbol);
653 fixp->fx_offset += sym->sy_value.X_add_number;
654 fixp->fx_addsy = sym = sym->sy_value.X_add_symbol;
658 /* If the section symbol isn't going to be output, the relocs
659 at least should still work. If not, figure out what to do
660 when we run into that case. */
661 fixp->fx_offset += S_GET_VALUE (sym);
663 fixp->fx_offset += sym->sy_frag->fr_address;
664 fixp->fx_addsy = section_symbol (symsec);
665 fixp->fx_addsy->sy_used_in_reloc = 1;
667 #if 1/*def RELOC_REQUIRES_SYMBOL*/
670 /* There was no symbol required by this relocation. However,
671 BFD doesn't really handle relocations without symbols well.
672 (At least, the COFF support doesn't.) So for now we fake up
673 a local symbol in the absolute section. */
675 fixp->fx_addsy = section_symbol (absolute_section);
676 fixp->fx_addsy->sy_used_in_reloc = 1;
680 dump_section_relocs (abfd, sec, stderr);
684 write_relocs (abfd, sec, xxx)
689 segment_info_type *seginfo = seg_info (sec);
696 /* If seginfo is NULL, we did not create this section; don't do
701 fixup_segment (seginfo->fix_root, sec);
704 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
707 #ifndef RELOC_EXPANSION_POSSIBLE
708 /* Set up reloc information as well. */
709 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
710 n * sizeof (arelent *));
711 memset ((char*)relocs, 0, n * sizeof (arelent*));
714 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
717 bfd_reloc_status_type s;
724 reloc = tc_gen_reloc (sec, fixp);
730 if (fixp->fx_where + fixp->fx_size
731 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
734 s = bfd_install_relocation (stdoutput, reloc,
735 fixp->fx_frag->fr_literal,
736 fixp->fx_frag->fr_address,
742 case bfd_reloc_overflow:
743 as_bad_where (fixp->fx_file, fixp->fx_line, "relocation overflow");
746 as_fatal ("%s:%u: bad return from bfd_perform_relocation",
747 fixp->fx_file, fixp->fx_line);
752 n = n * MAX_RELOC_EXPANSION;
753 /* Set up reloc information as well. */
754 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
755 n * sizeof (arelent *));
758 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
762 bfd_reloc_status_type s;
770 reloc = tc_gen_reloc (sec, fixp);
772 for (j = 0; reloc[j]; j++)
774 relocs[i++] = reloc[j];
777 data = fixp->fx_frag->fr_literal + fixp->fx_where;
778 if (fixp->fx_where + fixp->fx_size
779 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
781 for (j = 0; reloc[j]; j++)
783 s = bfd_perform_relocation (stdoutput, reloc[j],
784 data - reloc[0]->address,
785 sec, stdoutput, &err);
790 case bfd_reloc_overflow:
791 as_bad_where (fixp->fx_file, fixp->fx_line,
792 "relocation overflow");
795 as_fatal ("%s:%u: bad return from bfd_perform_relocation",
796 fixp->fx_file, fixp->fx_line);
807 sympp = bfd_get_outsymbols (stdoutput);
808 nsyms = bfd_get_symcount (stdoutput);
809 for (i = 0; i < n; i++)
810 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
812 for (j = 0; j < nsyms; j++)
813 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
822 bfd_set_reloc (stdoutput, sec, relocs, n);
824 bfd_set_section_flags (abfd, sec,
825 (bfd_get_section_flags (abfd, sec)
826 & (flagword) ~SEC_RELOC));
833 fprintf (stderr, "relocs for sec %s\n", sec->name);
834 for (i = 0; i < n; i++)
838 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
839 i, r, r->address, s->name, r->addend);
846 write_contents (abfd, sec, xxx)
851 segment_info_type *seginfo = seg_info (sec);
852 unsigned long offset = 0;
855 /* Write out the frags. */
857 || ! (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
860 for (f = seginfo->frchainP->frch_root;
865 unsigned long fill_size;
869 assert (f->fr_type == rs_fill);
872 x = bfd_set_section_contents (stdoutput, sec,
873 f->fr_literal, (file_ptr) offset,
874 (bfd_size_type) f->fr_fix);
877 bfd_perror (stdoutput->filename);
878 as_perror ("FATAL: Can't write %s", stdoutput->filename);
883 fill_literal = f->fr_literal + f->fr_fix;
884 fill_size = f->fr_var;
885 count = f->fr_offset;
887 if (fill_size && count)
888 #ifdef BFD_FAST_SECTION_FILL
891 if (fill_size > sizeof(buf)) {
892 /* Do it the old way. Can this ever happen? */
895 x = bfd_set_section_contents (stdoutput, sec,
896 fill_literal, (file_ptr) offset,
897 (bfd_size_type) fill_size);
900 bfd_perror (stdoutput->filename);
901 as_perror ("FATAL: Can't write %s", stdoutput->filename);
908 /* Build a buffer full of fill objects and output it as
909 * often as necessary. This saves on the overhead of potentially
910 * lots of bfd_set_section_contents calls.
912 int n_per_buf, bytes, i;
915 n_per_buf = sizeof (buf);
916 memset (buf, *fill_literal, n_per_buf);
921 n_per_buf = sizeof(buf)/fill_size;
922 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
923 memcpy(bufp, fill_literal, fill_size);
925 for (; count > 0; count -= n_per_buf)
927 n_per_buf = n_per_buf > count ? count : n_per_buf;
928 x = bfd_set_section_contents (stdoutput, sec,
929 buf, (file_ptr) offset,
930 (bfd_size_type) n_per_buf * fill_size);
932 offset += n_per_buf * fill_size;
939 x = bfd_set_section_contents (stdoutput, sec,
940 fill_literal, (file_ptr) offset,
941 (bfd_size_type) fill_size);
950 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
952 merge_data_into_text ()
954 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
955 seg_info (text_section)->frchainP->frch_last->fr_next =
956 seg_info (data_section)->frchainP->frch_root;
957 seg_info (text_section)->frchainP->frch_last =
958 seg_info (data_section)->frchainP->frch_last;
959 seg_info (data_section)->frchainP = 0;
963 text_last_frag->fr_next = data_frag_root;
964 text_last_frag = data_last_frag;
965 data_last_frag = NULL;
966 data_frag_root = NULL;
969 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
970 tmp->fx_next = data_fix_root;
971 text_fix_tail = data_fix_tail;
974 text_fix_root = data_fix_root;
975 data_fix_root = NULL;
978 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
980 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
982 relax_and_size_all_segments ()
986 relax_segment (text_frag_root, SEG_TEXT);
987 relax_segment (data_frag_root, SEG_DATA);
988 relax_segment (bss_frag_root, SEG_BSS);
990 * Now the addresses of frags are correct within the segment.
993 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
994 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
995 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
998 * Join the 2 segments into 1 huge segment.
999 * To do this, re-compute every rn_address in the SEG_DATA frags.
1000 * Then join the data frags after the text frags.
1002 * Determine a_data [length of data segment].
1006 register relax_addressT slide;
1008 know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
1010 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1011 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1012 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1014 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1015 /* For b.out: If the data section has a strict alignment
1016 requirement, its load address in the .o file will be
1017 rounded up from the size of the text section. These
1018 two values are *not* the same! Similarly for the bss
1020 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1023 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1025 fragP->fr_address += slide;
1026 } /* for each data frag */
1028 know (text_last_frag != 0);
1029 text_last_frag->fr_next = data_frag_root;
1033 H_SET_DATA_SIZE (&headers, 0);
1037 /* See above comments on b.out data section address. */
1040 if (data_last_frag == 0)
1041 bss_vma = H_GET_TEXT_SIZE (&headers);
1043 bss_vma = data_last_frag->fr_address;
1044 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1045 bss_address_frag.fr_address = bss_vma;
1047 #else /* ! OBJ_BOUT */
1048 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1049 H_GET_DATA_SIZE (&headers));
1051 #endif /* ! OBJ_BOUT */
1053 /* Slide all the frags */
1056 relax_addressT slide = bss_address_frag.fr_address;
1058 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1060 fragP->fr_address += slide;
1061 } /* for each bss frag */
1065 H_SET_BSS_SIZE (&headers,
1066 bss_last_frag->fr_address - bss_frag_root->fr_address);
1068 H_SET_BSS_SIZE (&headers, 0);
1070 #endif /* ! BFD_ASSEMBLER && ! BFD */
1072 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1074 #ifdef BFD_ASSEMBLER
1082 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1084 /* Count symbols. We can't rely on a count made by the loop in
1085 write_object_file, because *_frob_file may add a new symbol or
1088 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1095 asympp = (asymbol **) bfd_alloc (stdoutput,
1096 nsyms * sizeof (asymbol *));
1097 symp = symbol_rootP;
1098 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1100 asympp[i] = symp->bsym;
1106 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1107 assert (result == true);
1108 symbol_table_frozen = 1;
1113 write_object_file ()
1115 struct frchain *frchainP; /* Track along all frchains. */
1116 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1117 fragS *fragP; /* Track along all frags. */
1120 /* Do we really want to write it? */
1122 int n_warns, n_errs;
1123 n_warns = had_warnings ();
1124 n_errs = had_errors ();
1125 /* The -Z flag indicates that an object file should be generated,
1126 regardless of warnings and errors. */
1127 if (flag_always_generate_output)
1129 if (n_warns || n_errs)
1130 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
1131 n_errs, n_errs == 1 ? "" : "s",
1132 n_warns, n_warns == 1 ? "" : "s");
1137 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
1138 n_errs, n_errs == 1 ? "" : "s",
1139 n_warns, n_warns == 1 ? "" : "s");
1144 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1145 a routine to check for the definition of the procedure "_main",
1146 and if so -- fix it up so that it can be program entry point. */
1147 VMS_Check_For_Main ();
1150 /* After every sub-segment, we fake an ".align ...". This conforms to
1151 BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
1152 frag that requires least thought. ".align" frags like to have a
1153 following frag since that makes calculating their intended length
1156 @@ Is this really necessary?? */
1157 #ifndef SUB_SEGMENT_ALIGN
1158 #ifdef BFD_ASSEMBLER
1159 #define SUB_SEGMENT_ALIGN(SEG) (0)
1161 #define SUB_SEGMENT_ALIGN(SEG) (2)
1164 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1166 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1167 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
1168 /* frag_align will have left a new frag.
1169 Use this last frag for an empty ".fill".
1171 For this segment ...
1172 Create a last frag. Do not leave a "being filled in frag". */
1173 frag_wane (frag_now);
1174 frag_now->fr_fix = 0;
1175 know (frag_now->fr_next == NULL);
1178 /* From now on, we don't care about sub-segments. Build one frag chain
1179 for each segment. Linked thru fr_next. */
1181 #ifdef BFD_ASSEMBLER
1182 /* Remove the sections created by gas for its own purposes. */
1184 asection **seclist, *sec;
1185 seclist = &stdoutput->sections;
1186 while (seclist && *seclist)
1189 while (sec == reg_section || sec == expr_section)
1193 stdoutput->section_count--;
1198 seclist = &(*seclist)->next;
1202 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1204 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1205 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1206 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1209 /* We have two segments. If user gave -R flag, then we must put the
1210 data frags into the text segment. Do this before relaxing so
1211 we know to take advantage of -R and make shorter addresses. */
1212 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1213 if (flag_readonly_data_in_text)
1215 merge_data_into_text ();
1219 #ifdef BFD_ASSEMBLER
1220 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1222 relax_and_size_all_segments ();
1223 #endif /* BFD_ASSEMBLER */
1225 #ifndef BFD_ASSEMBLER
1228 * Crawl the symbol chain.
1230 * For each symbol whose value depends on a frag, take the address of
1231 * that frag and subsume it into the value of the symbol.
1232 * After this, there is just one way to lookup a symbol value.
1233 * Values are left in their final state for object file emission.
1234 * We adjust the values of 'L' local symbols, even if we do
1235 * not intend to emit them to the object file, because their values
1236 * are needed for fix-ups.
1238 * Unless we saw a -L flag, remove all symbols that begin with 'L'
1239 * from the symbol chain. (They are still pointed to by the fixes.)
1241 * Count the remaining symbols.
1242 * Assign a symbol number to each symbol.
1243 * Count the number of string-table chars we will emit.
1244 * Put this info into the headers as appropriate.
1247 know (zero_address_frag.fr_address == 0);
1248 string_byte_count = sizeof (string_byte_count);
1250 obj_crawl_symbol_chain (&headers);
1252 if (string_byte_count == sizeof (string_byte_count))
1253 string_byte_count = 0;
1255 H_SET_STRING_SIZE (&headers, string_byte_count);
1258 * Addresses of frags now reflect addresses we use in the object file.
1259 * Symbol values are correct.
1260 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1261 * Also converting any machine-dependent frags using md_convert_frag();
1263 subseg_change (SEG_TEXT, 0);
1265 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1267 cvt_frag_to_fill (&headers, fragP);
1269 /* Some assert macros don't work with # directives mixed in. */
1271 if (!(fragP->fr_next == NULL
1273 || fragP->fr_next == data_frag_root
1275 || ((fragP->fr_next->fr_address - fragP->fr_address)
1276 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1280 #endif /* ! BFD_ASSEMBLER */
1282 #ifndef WORKING_DOT_WORD
1284 struct broken_word *lie;
1285 struct broken_word **prevP;
1287 prevP = &broken_words;
1288 for (lie = broken_words; lie; lie = lie->next_broken_word)
1293 exp.X_op = O_subtract;
1294 exp.X_add_symbol = lie->add;
1295 exp.X_op_symbol = lie->sub;
1296 exp.X_add_number = lie->addnum;
1297 #ifdef BFD_ASSEMBLER
1298 #ifdef TC_CONS_FIX_NEW
1299 TC_CONS_FIX_NEW (lie->frag,
1300 lie->word_goes_here - lie->frag->fr_literal,
1303 fix_new_exp (lie->frag,
1304 lie->word_goes_here - lie->frag->fr_literal,
1305 2, &exp, 0, BFD_RELOC_NONE);
1308 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1309 fix_new_exp (lie->frag,
1310 lie->word_goes_here - lie->frag->fr_literal,
1311 2, &exp, 0, NO_RELOC);
1314 fix_new_ns32k_exp (lie->frag,
1315 lie->word_goes_here - lie->frag->fr_literal,
1316 2, &exp, 0, 0, 2, 0, 0);
1318 fix_new_exp (lie->frag,
1319 lie->word_goes_here - lie->frag->fr_literal,
1321 #endif /* TC_NS32K */
1322 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1323 #endif /* BFD_ASSEMBLER */
1324 *prevP = lie->next_broken_word;
1327 prevP = &(lie->next_broken_word);
1329 for (lie = broken_words; lie;)
1331 struct broken_word *untruth;
1333 addressT table_addr;
1334 addressT from_addr, to_addr;
1337 fragP = lie->dispfrag;
1339 /* Find out how many broken_words go here. */
1341 for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1342 if (untruth->added == 1)
1345 table_ptr = lie->dispfrag->fr_opcode;
1346 table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
1347 /* Create the jump around the long jumps. This is a short
1348 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1349 from_addr = table_addr;
1350 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1351 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1352 table_ptr += md_short_jump_size;
1353 table_addr += md_short_jump_size;
1355 for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
1357 if (lie->added == 2)
1359 /* Patch the jump table */
1360 /* This is the offset from ??? to table_ptr+0 */
1361 to_addr = table_addr - S_GET_VALUE (lie->sub);
1362 #ifdef BFD_ASSEMBLER
1363 to_addr -= lie->sub->sy_frag->fr_address;
1365 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1366 for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1368 if (untruth->use_jump == lie)
1369 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1372 /* Install the long jump */
1373 /* this is a long jump from table_ptr+0 to the final target */
1374 from_addr = table_addr;
1375 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1376 #ifdef BFD_ASSEMBLER
1377 to_addr += lie->add->sy_frag->fr_address;
1379 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1380 table_ptr += md_long_jump_size;
1381 table_addr += md_long_jump_size;
1385 #endif /* not WORKING_DOT_WORD */
1387 #ifndef BFD_ASSEMBLER
1390 long object_file_size;
1392 * Scan every FixS performing fixups. We had to wait until now to do
1393 * this because md_convert_frag() may have made some fixSs.
1397 subseg_change (SEG_TEXT, 0);
1398 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1399 subseg_change (SEG_DATA, 0);
1400 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1401 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1403 /* FIXME move this stuff into the pre-write-hook */
1404 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1405 H_SET_ENTRY_POINT (&headers, 0);
1407 obj_pre_write_hook (&headers); /* extra coff stuff */
1409 object_file_size = H_GET_FILE_SIZE (&headers);
1410 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1412 output_file_create (out_file_name);
1414 obj_header_append (&next_object_file_charP, &headers);
1416 know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
1421 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1423 register long count;
1424 register char *fill_literal;
1425 register long fill_size;
1427 know (fragP->fr_type == rs_fill);
1428 append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
1429 fill_literal = fragP->fr_literal + fragP->fr_fix;
1430 fill_size = fragP->fr_var;
1431 know (fragP->fr_offset >= 0);
1433 for (count = fragP->fr_offset; count; count--)
1435 append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
1438 } /* for each code frag. */
1440 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
1445 obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
1446 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)));
1448 /* Make addresses in data relocation directives relative to beginning of
1449 * first data fragment, not end of last text fragment: alignment of the
1450 * start of the data segment may place a gap between the segments.
1452 obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
1454 obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
1455 #endif /* TC_I960 */
1457 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)));
1460 * Emit line number entries.
1462 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1463 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)));
1468 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1469 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)));
1475 if (string_byte_count > 0)
1477 obj_emit_strings (&next_object_file_charP);
1478 } /* only if we have a string table */
1481 bfd_seek (stdoutput, 0, 0);
1482 bfd_write (the_object_file, 1, object_file_size, stdoutput);
1485 /* Write the data to the file */
1486 output_file_append (the_object_file, object_file_size, out_file_name);
1488 } /* non vms output */
1491 * Now do the VMS-dependent part of writing the object file
1493 VMS_write_object_file (H_GET_TEXT_SIZE (&headers),
1494 H_GET_DATA_SIZE (&headers),
1495 H_GET_BSS_SIZE (&headers),
1496 text_frag_root, data_frag_root);
1498 #else /* BFD_ASSEMBLER */
1500 /* Resolve symbol values. This needs to be done before processing
1506 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1507 if (!symp->sy_resolved)
1508 resolve_symbol_value (symp);
1511 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *)0);
1513 /* Set up symbol table, and write it out. */
1518 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1522 /* Do it again, because adjust_reloc_syms might introduce
1523 more symbols. They'll probably only be section symbols,
1524 but they'll still need to have the values computed. */
1525 if (! symp->sy_resolved)
1527 if (symp->sy_value.X_op == O_constant)
1529 /* This is the normal case; skip the call. */
1532 + symp->sy_frag->fr_address));
1533 symp->sy_resolved = 1;
1536 resolve_symbol_value (symp);
1539 /* So far, common symbols have been treated like undefined symbols.
1540 Put them in the common section now. */
1541 if (S_IS_DEFINED (symp) == 0
1542 && S_GET_VALUE (symp) != 0)
1543 S_SET_SEGMENT (symp, bfd_com_section_ptr);
1545 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1546 S_GET_NAME (symp), symp,
1549 segment_name (symp->bsym->section));
1552 #ifdef obj_frob_symbol
1553 obj_frob_symbol (symp, punt);
1555 #ifdef tc_frob_symbol
1556 if (! punt || symp->sy_used_in_reloc)
1557 tc_frob_symbol (symp, punt);
1560 /* If we don't want to keep this symbol, splice it out of
1561 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1562 want section symbols. Otherwise, we skip local symbols
1563 and symbols that the frob_symbol macros told us to punt,
1564 but we keep such symbols if they are used in relocs. */
1565 if ((! EMIT_SECTION_SYMBOLS
1566 && (symp->bsym->flags & BSF_SECTION_SYM) != 0)
1567 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1568 opposites. Sometimes the former checks flags and the
1569 latter examines the name... */
1570 || (!S_IS_EXTERN (symp)
1571 && (S_IS_LOCAL (symp) || punt)
1572 && ! symp->sy_used_in_reloc))
1574 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1575 /* After symbol_remove, symbol_next(symp) still returns
1576 the one that came after it in the chain. So we don't
1577 need to do any extra cleanup work here. */
1582 /* Make sure we really got a value for the symbol. */
1583 if (! symp->sy_resolved)
1585 as_bad ("can't resolve value for symbol \"%s\"",
1587 symp->sy_resolved = 1;
1590 /* Set the value into the BFD symbol. Up til now the value
1591 has only been kept in the gas symbolS struct. */
1592 symp->bsym->value = S_GET_VALUE (symp);
1596 /* Now do any format-specific adjustments to the symbol table, such
1597 as adding file symbols. */
1598 #ifdef obj_adjust_symtab
1599 obj_adjust_symtab ();
1602 /* Now that all the sizes are known, and contents correct, we can
1603 start writing to the file. */
1606 /* If *_frob_file changes the symbol value at this point, it is
1607 responsible for moving the changed value into symp->bsym->value
1608 as well. Hopefully all symbol value changing can be done in
1613 #ifdef obj_frob_file
1617 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1619 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1620 #endif /* BFD_ASSEMBLER */
1627 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1632 * After this, all frags in this segment have addresses that are correct
1633 * within the segment. Since segments live in different file addresses,
1634 * these frag addresses may not be the same as final object-file addresses.
1637 #ifndef md_relax_frag
1639 /* Subroutines of relax_segment. */
1645 for (; f1; f1 = f1->fr_next)
1646 if (f1->fr_next == f2)
1651 #endif /* ! defined (md_relax_frag) */
1653 /* Relax_align. Advance location counter to next address that has 'alignment'
1654 lowest order bits all 0s, return size of adjustment made. */
1655 static relax_addressT
1656 relax_align (address, alignment)
1657 register relax_addressT address; /* Address now. */
1658 register int alignment; /* Alignment (binary). */
1660 relax_addressT mask;
1661 relax_addressT new_address;
1663 mask = ~((~0) << alignment);
1664 new_address = (address + mask) & (~mask);
1666 /* We must provide lots of padding, so the linker can discard it
1667 when needed. The linker will not add extra space, ever. */
1668 new_address += (1 << alignment);
1669 return (new_address - address);
1673 relax_segment (segment_frag_root, segment)
1674 struct frag *segment_frag_root;
1677 register struct frag *fragP;
1678 register relax_addressT address;
1679 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1680 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
1682 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1683 subseg_change (segment, 0);
1685 /* For each frag in segment: count and store (a 1st guess of)
1688 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1690 fragP->fr_address = address;
1691 address += fragP->fr_fix;
1693 switch (fragP->fr_type)
1696 address += fragP->fr_offset * fragP->fr_var;
1701 int offset = relax_align (address, (int) fragP->fr_offset);
1702 if (offset % fragP->fr_var != 0)
1704 as_bad ("alignment padding (%d bytes) not a multiple of %ld",
1705 offset, (long) fragP->fr_var);
1706 offset -= (offset % fragP->fr_var);
1713 /* Assume .org is nugatory. It will grow with 1st relax. */
1716 case rs_machine_dependent:
1717 address += md_estimate_size_before_relax (fragP, segment);
1720 #ifndef WORKING_DOT_WORD
1721 /* Broken words don't concern us yet */
1722 case rs_broken_word:
1727 BAD_CASE (fragP->fr_type);
1729 } /* switch(fr_type) */
1730 } /* for each frag in the segment */
1734 long stretch; /* May be any size, 0 or negative. */
1735 /* Cumulative number of addresses we have */
1736 /* relaxed this pass. */
1737 /* We may have relaxed more than one address. */
1738 long stretched; /* Have we stretched on this pass? */
1739 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1740 grew, and another shrank. If a branch instruction doesn't fit anymore,
1741 we could be scrod. */
1745 stretch = stretched = 0;
1746 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1749 unsigned long was_address;
1755 was_address = fragP->fr_address;
1756 address = fragP->fr_address += stretch;
1757 symbolP = fragP->fr_symbol;
1758 offset = fragP->fr_offset;
1760 switch (fragP->fr_type)
1762 case rs_fill: /* .fill never relaxes. */
1766 #ifndef WORKING_DOT_WORD
1767 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1768 for it I do not want to write it. I do not want to have
1769 anything to do with it. This is not the proper way to
1770 implement this misfeature. */
1771 case rs_broken_word:
1773 struct broken_word *lie;
1774 struct broken_word *untruth;
1776 /* Yes this is ugly (storing the broken_word pointer
1777 in the symbol slot). Still, this whole chunk of
1778 code is ugly, and I don't feel like doing anything
1779 about it. Think of it as stubbornness in action. */
1781 for (lie = (struct broken_word *) (fragP->fr_symbol);
1782 lie && lie->dispfrag == fragP;
1783 lie = lie->next_broken_word)
1789 offset = (lie->add->sy_frag->fr_address
1790 + S_GET_VALUE (lie->add)
1792 - (lie->sub->sy_frag->fr_address
1793 + S_GET_VALUE (lie->sub)));
1794 if (offset <= -32768 || offset >= 32767)
1796 if (flag_warn_displacement)
1799 sprint_value (buf, (addressT) lie->addnum);
1800 as_warn (".word %s-%s+%s didn't fit",
1801 S_GET_NAME (lie->add),
1802 S_GET_NAME (lie->sub),
1806 if (fragP->fr_subtype == 0)
1808 fragP->fr_subtype++;
1809 growth += md_short_jump_size;
1811 for (untruth = lie->next_broken_word;
1812 untruth && untruth->dispfrag == lie->dispfrag;
1813 untruth = untruth->next_broken_word)
1814 if ((untruth->add->sy_frag == lie->add->sy_frag)
1815 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
1818 untruth->use_jump = lie;
1820 growth += md_long_jump_size;
1825 } /* case rs_broken_word */
1828 growth = (relax_align ((relax_addressT) (address
1831 - relax_align ((relax_addressT) (was_address
1841 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1842 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1843 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1844 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1845 || S_GET_SEGMENT (symbolP) == SEG_BSS);
1846 know (symbolP->sy_frag);
1847 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1848 || (symbolP->sy_frag == &zero_address_frag));
1850 target += S_GET_VALUE (symbolP)
1851 + symbolP->sy_frag->fr_address;
1852 } /* if we have a symbol */
1854 know (fragP->fr_next);
1855 after = fragP->fr_next->fr_address;
1856 growth = ((target - after) > 0) ? (target - after) : 0;
1857 /* Growth may be negative, but variable part of frag
1858 cannot have fewer than 0 chars. That is, we can't
1861 growth -= stretch; /* This is an absolute growth factor */
1864 case rs_machine_dependent:
1865 #ifdef md_relax_frag
1866 growth = md_relax_frag (fragP, stretch);
1868 /* The default way to relax a frag is to look through
1871 const relax_typeS *this_type;
1872 const relax_typeS *start_type;
1873 relax_substateT next_state;
1874 relax_substateT this_state;
1877 this_state = fragP->fr_subtype;
1878 start_type = this_type = md_relax_table + this_state;
1883 #ifndef DIFF_EXPR_OK
1884 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1885 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1886 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1887 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
1888 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
1890 know (symbolP->sy_frag);
1892 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
1893 || symbolP->sy_frag == &zero_address_frag);
1895 S_GET_VALUE (symbolP)
1896 + symbolP->sy_frag->fr_address;
1898 /* If frag has yet to be reached on this pass,
1899 assume it will move by STRETCH just as we did.
1900 If this is not so, it will be because some frag
1901 between grows, and that will force another pass.
1903 Beware zero-length frags.
1905 There should be a faster way to do this. */
1907 if (symbolP->sy_frag->fr_address >= was_address
1908 && is_dnrange (fragP, symbolP->sy_frag))
1914 aim = target - address - fragP->fr_fix;
1915 #ifdef TC_PCREL_ADJUST
1916 /* Currently only the ns32k family needs this */
1917 aim += TC_PCREL_ADJUST(fragP);
1919 /* This machine doesn't want to use pcrel_adjust.
1920 In that case, pcrel_adjust should be zero. */
1921 assert (fragP->fr_pcrel_adjust == 0);
1926 /* Look backwards. */
1927 for (next_state = this_type->rlx_more; next_state;)
1928 if (aim >= this_type->rlx_backward)
1932 /* Grow to next state. */
1933 this_state = next_state;
1934 this_type = md_relax_table + this_state;
1935 next_state = this_type->rlx_more;
1940 #ifdef M68K_AIM_KLUDGE
1941 M68K_AIM_KLUDGE (aim, this_state, this_type);
1943 /* Look forwards. */
1944 for (next_state = this_type->rlx_more; next_state;)
1945 if (aim <= this_type->rlx_forward)
1949 /* Grow to next state. */
1950 this_state = next_state;
1951 this_type = md_relax_table + this_state;
1952 next_state = this_type->rlx_more;
1956 growth = this_type->rlx_length - start_type->rlx_length;
1958 fragP->fr_subtype = this_state;
1964 BAD_CASE (fragP->fr_type);
1972 } /* For each frag in the segment. */
1974 while (stretched); /* Until nothing further to relax. */
1978 * We now have valid fr_address'es for each frag.
1982 * All fr_address's are correct, relative to their own segment.
1983 * We have made all the fixS we will ever make.
1985 } /* relax_segment() */
1987 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1989 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
1990 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
1995 Go through all the fixS's in a segment and see which ones can be
1996 handled now. (These consist of fixS where we have since discovered
1997 the value of a symbol, or the address of the frag involved.)
1998 For each one, call md_apply_fix to put the fix into the frag data.
2000 Result is a count of how many relocation structs will be needed to
2001 handle the remaining fixS's that we couldn't completely handle here.
2002 These will be output later by emit_relocations(). */
2005 fixup_segment (fixP, this_segment_type)
2006 register fixS *fixP;
2007 segT this_segment_type; /* N_TYPE bits for segment. */
2009 long seg_reloc_count = 0;
2010 symbolS *add_symbolP;
2011 symbolS *sub_symbolP;
2018 segT add_symbol_segment = absolute_section;
2020 /* If the linker is doing the relaxing, we must not do any fixups.
2022 Well, strictly speaking that's not true -- we could do any that are
2023 PC-relative and don't cross regions that could change size. And for the
2024 i960 (the only machine for which we've got a relaxing linker right now),
2025 we might be able to turn callx/callj into bal anyways in cases where we
2026 know the maximum displacement. */
2029 for (; fixP; fixP = fixP->fx_next)
2031 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2032 return seg_reloc_count;
2035 for (; fixP; fixP = fixP->fx_next)
2037 fragP = fixP->fx_frag;
2039 where = fixP->fx_where;
2040 place = fragP->fr_literal + where;
2041 size = fixP->fx_size;
2042 add_symbolP = fixP->fx_addsy;
2043 #ifdef TC_VALIDATE_FIX
2044 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2046 sub_symbolP = fixP->fx_subsy;
2047 add_number = fixP->fx_offset;
2048 pcrel = fixP->fx_pcrel;
2051 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2058 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2059 add_number -= S_GET_VALUE (sub_symbolP);
2061 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2063 /* Should try converting to a constant. */
2068 as_bad_where (fixP->fx_file, fixP->fx_line,
2069 "Negative of non-absolute symbol %s",
2070 S_GET_NAME (sub_symbolP));
2072 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
2073 && (SEG_NORMAL (add_symbol_segment)
2074 || (add_symbol_segment == absolute_section)))
2076 /* Difference of 2 symbols from same segment.
2077 Can't make difference of 2 undefineds: 'value' means
2078 something different for N_UNDF. */
2080 /* Makes no sense to use the difference of 2 arbitrary symbols
2081 as the target of a call instruction. */
2083 as_bad_where (fixP->fx_file, fixP->fx_line,
2084 "callj to difference of 2 symbols");
2085 #endif /* TC_I960 */
2086 add_number += S_GET_VALUE (add_symbolP) -
2087 S_GET_VALUE (sub_symbolP);
2091 /* Let the target machine make the final determination
2092 as to whether or not a relocation will be needed to
2093 handle this fixup. */
2094 if (!TC_FORCE_RELOCATION (fixP))
2096 fixP->fx_addsy = NULL;
2101 /* Different segments in subtraction. */
2102 know (!(S_IS_EXTERNAL (sub_symbolP)
2103 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2105 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2106 add_number -= S_GET_VALUE (sub_symbolP);
2109 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2110 #if 0 /* Do this even if it's already described as pc-relative. For example,
2111 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
2112 pc-relative mode. */
2117 /* Make it pc-relative. */
2118 add_number += (md_pcrel_from (fixP)
2119 - S_GET_VALUE (sub_symbolP));
2126 #ifdef BFD_ASSEMBLER
2127 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2128 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2130 /* Leave it alone. */
2136 sprint_value (buf, fragP->fr_address + where);
2137 as_bad_where (fixP->fx_file, fixP->fx_line,
2138 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
2139 segment_name (S_GET_SEGMENT (sub_symbolP)),
2140 S_GET_NAME (sub_symbolP), buf);
2147 if (add_symbol_segment == this_segment_type && pcrel
2148 && TC_RELOC_RTSYM_LOC_FIXUP (fixP->fx_r_type))
2151 * This fixup was made when the symbol's segment was
2152 * SEG_UNKNOWN, but it is now in the local segment.
2153 * So we know how to do the address without relocation.
2156 /* reloc_callj() may replace a 'call' with a 'calls' or a
2157 'bal', in which cases it modifies *fixP as appropriate.
2158 In the case of a 'calls', no further work is required,
2159 and *fixP has been set up to make the rest of the code
2162 #endif /* TC_I960 */
2164 add_number += S_GET_VALUE (add_symbolP);
2165 add_number -= md_pcrel_from (fixP);
2166 pcrel = 0; /* Lie. Don't want further pcrel processing. */
2168 /* Let the target machine make the final determination
2169 as to whether or not a relocation will be needed to
2170 handle this fixup. */
2171 if (!TC_FORCE_RELOCATION (fixP))
2174 fixP->fx_addsy = NULL;
2179 if (add_symbol_segment == absolute_section)
2182 /* See comment about reloc_callj() above. */
2184 #endif /* TC_I960 */
2185 add_number += S_GET_VALUE (add_symbolP);
2187 /* Let the target machine make the final determination
2188 as to whether or not a relocation will be needed to
2189 handle this fixup. */
2190 if (!TC_FORCE_RELOCATION (fixP))
2192 fixP->fx_addsy = NULL;
2196 else if (add_symbol_segment == undefined_section
2197 #ifdef BFD_ASSEMBLER
2198 || bfd_is_com_section (add_symbol_segment)
2203 if ((int) fixP->fx_bit_fixP == 13)
2205 /* This is a COBR instruction. They have only a
2206 * 13-bit displacement and are only to be used
2207 * for local branches: flag as error, don't generate
2210 as_bad_where (fixP->fx_file, fixP->fx_line,
2211 "can't use COBR format with external label");
2212 fixP->fx_addsy = NULL;
2216 #endif /* TC_I960 */
2220 if (S_IS_COMMON (add_symbolP))
2221 add_number += S_GET_VALUE (add_symbolP);
2222 #endif /* TE_I386AIX */
2223 #endif /* OBJ_COFF */
2229 add_number += S_GET_VALUE (add_symbolP);
2236 add_number -= md_pcrel_from (fixP);
2237 if (add_symbolP == 0)
2239 #ifndef BFD_ASSEMBLER
2240 fixP->fx_addsy = &abs_symbol;
2242 fixP->fx_addsy = section_symbol (absolute_section);
2244 fixP->fx_addsy->sy_used_in_reloc = 1;
2249 if (!fixP->fx_bit_fixP && size > 0)
2252 if (size < sizeof (mask))
2254 /* set all bits to one */
2256 /* Technically, combining these produces an undefined result
2257 if size is sizeof (valueT), though I think these two
2258 half-way operations should both be defined. And the
2259 compiler should be able to combine them if it's valid on
2260 the host architecture. */
2263 if ((add_number & mask) != 0
2264 && (add_number & mask) != mask)
2266 char buf[50], buf2[50];
2267 sprint_value (buf, fragP->fr_address + where);
2268 if (add_number > 1000)
2269 sprint_value (buf2, add_number);
2271 sprintf (buf2, "%ld", (long) add_number);
2272 as_bad_where (fixP->fx_file, fixP->fx_line,
2273 "Value of %s too large for field of %d bytes at %s",
2275 } /* generic error checking */
2277 #ifdef WARN_SIGNED_OVERFLOW_WORD
2278 /* Warn if a .word value is too large when treated as a signed
2279 number. We already know it is not too negative. This is to
2280 catch over-large switches generated by gcc on the 68k. */
2281 if (!flag_signed_overflow_ok
2283 && add_number > 0x7fff)
2284 as_bad_where (fixP->fx_file, fixP->fx_line,
2285 "Signed .word overflow; switch may be too large; %ld at 0x%lx",
2287 (unsigned long) (fragP->fr_address + where));
2289 } /* not a bit fix */
2293 #ifdef BFD_ASSEMBLER
2294 md_apply_fix (fixP, &add_number);
2296 md_apply_fix (fixP, add_number);
2299 #ifndef TC_HANDLES_FX_DONE
2300 /* If the tc-* files haven't been converted, assume it's handling
2301 it the old way, where a null fx_addsy means that the fix has
2302 been applied completely, and no further work is needed. */
2303 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2307 #ifdef TC_VALIDATE_FIX
2311 } /* For each fixS in this segment. */
2313 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2314 return seg_reloc_count;
2317 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
2320 number_to_chars_bigendian (buf, val, n)
2325 if (n > sizeof (val)|| n <= 0)
2329 buf[n] = val & 0xff;
2335 number_to_chars_littleendian (buf, val, n)
2340 if (n > sizeof (val) || n <= 0)
2344 *buf++ = val & 0xff;
2350 extern int indent_level;
2357 fprintf (stderr, "fix %s:%d", fixp->fx_file, fixp->fx_line);
2359 fprintf (stderr, " pcrel");
2360 if (fixp->fx_pcrel_adjust)
2361 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2362 if (fixp->fx_im_disp)
2365 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2367 fprintf (stderr, " im_disp");
2371 fprintf (stderr, " tcbit");
2373 fprintf (stderr, " done");
2374 fprintf (stderr, "\n size=%d frag=%lx where=%ld addnumber=%lx",
2375 fixp->fx_size, (long) fixp->fx_frag, fixp->fx_where,
2376 (long) fixp->fx_addnumber);
2377 #ifdef BFD_ASSEMBLER
2378 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2381 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2385 fprintf (stderr, "\n <");
2386 print_symbol_value_1 (stderr, fixp->fx_addsy);
2387 fprintf (stderr, ">");
2389 fprintf (stderr, "\n");
2392 /* end of write.c */