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 sub
= exp
->X_add_symbol
;
237 off
= exp
->X_add_number
;
241 sub
= exp
->X_op_symbol
;
244 add
= exp
->X_add_symbol
;
247 off
= exp
->X_add_number
;
251 as_bad ("expression too complex for fixup");
254 return fix_new_internal (frag
, where
, size
, add
, sub
, off
,
258 /* Append a string onto another string, bumping the pointer along. */
260 append (charPP
, fromP
, length
)
263 unsigned long length
;
265 /* Don't trust memcpy() of 0 chars. */
269 memcpy (*charPP
, fromP
, length
);
273 #ifndef BFD_ASSEMBLER
274 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
278 * This routine records the largest alignment seen for each segment.
279 * If the beginning of the segment is aligned on the worst-case
280 * boundary, all of the other alignments within it will work. At
281 * least one object format really uses this info.
284 record_alignment (seg
, align
)
285 /* Segment to which alignment pertains */
287 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
292 if (align
> bfd_get_section_alignment (stdoutput
, seg
))
293 bfd_set_section_alignment (stdoutput
, seg
, align
);
295 if (align
> section_alignment
[(int) seg
])
296 section_alignment
[(int) seg
] = align
;
300 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
303 chain_frchains_together_1 (section
, frchp
)
305 struct frchain
*frchp
;
307 fragS dummy
, *prev_frag
= &dummy
;
308 fixS fix_dummy
, *prev_fix
= &fix_dummy
;
310 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
312 prev_frag
->fr_next
= frchp
->frch_root
;
313 prev_frag
= frchp
->frch_last
;
315 if (frchp
->fix_root
!= (fixS
*) NULL
)
317 if (seg_info (section
)->fix_root
== (fixS
*) NULL
)
318 seg_info (section
)->fix_root
= frchp
->fix_root
;
319 prev_fix
->fx_next
= frchp
->fix_root
;
320 seg_info (section
)->fix_tail
= frchp
->fix_tail
;
321 prev_fix
= frchp
->fix_tail
;
325 prev_frag
->fr_next
= 0;
334 chain_frchains_together (abfd
, section
, xxx
)
335 bfd
*abfd
; /* unused */
337 PTR xxx
; /* unused */
339 segment_info_type
*info
;
341 /* BFD may have introduced its own sections without using
342 subseg_new, so it is possible that seg_info is NULL. */
343 info
= seg_info (section
);
344 if (info
!= (segment_info_type
*) NULL
)
345 info
->frchainP
->frch_last
346 = chain_frchains_together_1 (section
, info
->frchainP
);
348 /* Now that we've chained the frags together, we must add new fixups
349 to the segment, not to the frag chain. */
355 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
358 remove_subsegs (head
, seg
, root
, last
)
364 *root
= head
->frch_root
;
365 *last
= chain_frchains_together_1 (seg
, head
);
370 #if defined (BFD_ASSEMBLER) || !defined (BFD)
374 cvt_frag_to_fill (sec
, fragP
)
379 cvt_frag_to_fill (headers
, fragP
)
380 object_headers
*headers
;
384 switch (fragP
->fr_type
)
389 HANDLE_ALIGN (fragP
);
391 fragP
->fr_type
= rs_fill
;
392 know (fragP
->fr_next
!= NULL
);
393 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
395 - fragP
->fr_fix
) / fragP
->fr_var
;
401 case rs_machine_dependent
:
403 md_convert_frag (stdoutput
, sec
, fragP
);
405 md_convert_frag (headers
, fragP
);
408 assert (fragP
->fr_next
== NULL
|| (fragP
->fr_next
->fr_address
- fragP
->fr_address
== fragP
->fr_fix
));
411 * After md_convert_frag, we make the frag into a ".space 0".
412 * Md_convert_frag() should set up any fixSs and constants
418 #ifndef WORKING_DOT_WORD
421 struct broken_word
*lie
;
423 if (fragP
->fr_subtype
)
425 fragP
->fr_fix
+= md_short_jump_size
;
426 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
427 lie
&& lie
->dispfrag
== fragP
;
428 lie
= lie
->next_broken_word
)
430 fragP
->fr_fix
+= md_long_jump_size
;
438 BAD_CASE (fragP
->fr_type
);
443 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
447 relax_and_size_seg (abfd
, sec
, xxx
)
454 segment_info_type
*seginfo
;
456 valueT size
, newsize
;
458 flags
= bfd_get_section_flags (abfd
, sec
);
460 seginfo
= seg_info (sec
);
461 if (seginfo
&& seginfo
->frchainP
)
463 relax_segment (seginfo
->frchainP
->frch_root
, sec
);
464 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
465 cvt_frag_to_fill (sec
, fragp
);
466 for (fragp
= seginfo
->frchainP
->frch_root
;
468 fragp
= fragp
->fr_next
)
469 /* walk to last elt */;
470 size
= fragp
->fr_address
+ fragp
->fr_fix
;
475 if (size
> 0 && ! seginfo
->bss
)
476 flags
|= SEC_HAS_CONTENTS
;
478 /* @@ This is just an approximation. */
479 if (seginfo
&& seginfo
->fix_root
)
483 x
= bfd_set_section_flags (abfd
, sec
, flags
);
486 newsize
= md_section_align (sec
, size
);
487 x
= bfd_set_section_size (abfd
, sec
, newsize
);
490 /* If the size had to be rounded up, add some padding in the last
492 assert (newsize
>= size
);
495 fragS
*last
= seginfo
->frchainP
->frch_last
;
496 fragp
= seginfo
->frchainP
->frch_root
;
497 while (fragp
->fr_next
!= last
)
498 fragp
= fragp
->fr_next
;
499 last
->fr_address
= size
;
500 fragp
->fr_offset
+= newsize
- size
;
503 #ifdef tc_frob_section
504 tc_frob_section (sec
);
506 #ifdef obj_frob_section
507 obj_frob_section (sec
);
513 dump_section_relocs (abfd
, sec
, stream_
)
518 FILE *stream
= (FILE *) stream_
;
519 segment_info_type
*seginfo
= seg_info (sec
);
520 fixS
*fixp
= seginfo
->fix_root
;
525 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
528 symbolS
*s
= fixp
->fx_addsy
;
531 fprintf (stream
, " %08x: %s(%s", fixp
, S_GET_NAME (s
),
532 s
->bsym
->section
->name
);
533 if (s
->bsym
->flags
& BSF_SECTION_SYM
)
535 fprintf (stream
, " section sym");
537 fprintf (stream
, "+%x", S_GET_VALUE (s
));
540 fprintf (stream
, "+%x", S_GET_VALUE (s
));
541 fprintf (stream
, ")+%x\n", fixp
->fx_offset
);
544 fprintf (stream
, " %08x: type %d no sym\n", fixp
, fixp
->fx_r_type
);
545 fixp
= fixp
->fx_next
;
549 #define dump_section_relocs(ABFD,SEC,STREAM) (void)(ABFD,SEC,STREAM)
552 #ifndef EMIT_SECTION_SYMBOLS
553 #define EMIT_SECTION_SYMBOLS 1
557 adjust_reloc_syms (abfd
, sec
, xxx
)
562 segment_info_type
*seginfo
= seg_info (sec
);
568 dump_section_relocs (abfd
, sec
, stderr
);
570 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
573 else if (fixp
->fx_addsy
)
575 symbolS
*sym
= fixp
->fx_addsy
;
576 asection
*symsec
= sym
->bsym
->section
;
578 /* If it's one of these sections, assume the symbol is
579 definitely going to be output. The code in
580 md_estimate_size_before_relax in tc-mips.c uses this test
581 as well, so if you change this code you should look at that
583 if (symsec
== &bfd_und_section
584 || symsec
== &bfd_abs_section
585 || bfd_is_com_section (symsec
))
587 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
591 /* Since we're reducing to section symbols, don't attempt to reduce
592 anything that's already using one. */
593 if (sym
->bsym
->flags
& BSF_SECTION_SYM
)
595 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
599 /* Is there some other reason we can't adjust this one? (E.g.,
600 call/bal links in i960-bout symbols.) */
601 #ifdef obj_fix_adjustable
602 if (! obj_fix_adjustable (fixp
))
604 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
609 /* Is there some other (target cpu dependent) reason we can't adjust
610 this one? (E.g. relocations involving function addresses on
612 #ifdef tc_fix_adjustable
613 if (! tc_fix_adjustable (fixp
))
615 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
620 /* If the section symbol isn't going to be output, the relocs
621 at least should still work. If not, figure out what to do
622 when we run into that case. */
623 fixp
->fx_offset
+= S_GET_VALUE (sym
);
625 fixp
->fx_offset
+= sym
->sy_frag
->fr_address
;
626 fixp
->fx_addsy
= section_symbol (symsec
);
627 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
629 #if 1/*def RELOC_REQUIRES_SYMBOL*/
632 /* There was no symbol required by this relocation. However,
633 BFD doesn't really handle relocations without symbols well.
634 (At least, the COFF support doesn't.) So for now we fake up
635 a local symbol in the absolute section. */
637 fixp
->fx_addsy
= section_symbol (absolute_section
);
638 fixp
->fx_addsy
->sy_used_in_reloc
= 1;
642 dump_section_relocs (abfd
, sec
, stderr
);
646 write_relocs (abfd
, sec
, xxx
)
651 segment_info_type
*seginfo
= seg_info (sec
);
658 /* If seginfo is NULL, we did not create this section; don't do
663 fixup_segment (seginfo
->fix_root
, sec
);
666 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
669 #ifndef RELOC_EXPANSION_POSSIBLE
670 /* Set up reloc information as well. */
671 relocs
= (arelent
**) bfd_alloc_by_size_t (stdoutput
,
672 n
* sizeof (arelent
*));
673 memset ((char*)relocs
, 0, n
* sizeof (arelent
*));
676 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
680 bfd_reloc_status_type s
;
687 reloc
= tc_gen_reloc (sec
, fixp
);
693 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
694 if (fixp
->fx_where
+ fixp
->fx_size
695 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
698 if (reloc
->howto
->partial_inplace
== false
699 && reloc
->howto
->pcrel_offset
== true
700 && reloc
->howto
->pc_relative
== true)
702 /* bfd_perform_relocation screws this up */
703 reloc
->addend
+= reloc
->address
;
705 /* Pass bogus address so that when bfd_perform_relocation adds
706 `reloc->address' back in, it'll come up with `data', which is where
707 we want it to operate. We can't just do it by fudging reloc->address,
708 since that might be used in the calculations(?). */
709 s
= bfd_perform_relocation (stdoutput
, reloc
, data
- reloc
->address
,
710 sec
, stdoutput
, &err
);
715 case bfd_reloc_overflow
:
716 as_bad_where (fixp
->fx_file
, fixp
->fx_line
, "relocation overflow");
719 as_fatal ("bad return from bfd_perform_relocation");
724 n
= n
* MAX_RELOC_EXPANSION
;
725 /* Set up reloc information as well. */
726 relocs
= (arelent
**) bfd_alloc_by_size_t (stdoutput
,
727 n
* sizeof (arelent
*));
730 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
734 bfd_reloc_status_type s
;
742 reloc
= tc_gen_reloc (sec
, fixp
);
744 for (j
= 0; reloc
[j
]; j
++)
746 relocs
[i
++] = reloc
[j
];
749 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
750 if (fixp
->fx_where
+ fixp
->fx_size
751 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
753 for (j
= 0; reloc
[j
]; j
++)
755 s
= bfd_perform_relocation (stdoutput
, reloc
[j
],
756 data
- reloc
[0]->address
,
757 sec
, stdoutput
, &err
);
763 as_fatal ("bad return from bfd_perform_relocation");
774 sympp
= bfd_get_outsymbols (stdoutput
);
775 nsyms
= bfd_get_symcount (stdoutput
);
776 for (i
= 0; i
< n
; i
++)
777 if (((*relocs
[i
]->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) == 0)
779 for (j
= 0; j
< nsyms
; j
++)
780 if (sympp
[j
] == *relocs
[i
]->sym_ptr_ptr
)
789 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
791 bfd_set_section_flags (abfd
, sec
,
792 (bfd_get_section_flags (abfd
, sec
)
793 & (flagword
) ~SEC_RELOC
));
800 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
801 for (i
= 0; i
< n
; i
++)
805 fprintf (stderr
, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
806 i
, r
, r
->address
, s
->name
, r
->addend
);
813 write_contents (abfd
, sec
, xxx
)
818 segment_info_type
*seginfo
= seg_info (sec
);
819 unsigned long offset
= 0;
822 /* Write out the frags. */
824 || ! (bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
827 for (f
= seginfo
->frchainP
->frch_root
;
832 unsigned long fill_size
;
836 assert (f
->fr_type
== rs_fill
);
839 x
= bfd_set_section_contents (stdoutput
, sec
,
840 f
->fr_literal
, (file_ptr
) offset
,
841 (bfd_size_type
) f
->fr_fix
);
844 bfd_perror (stdoutput
->filename
);
845 as_perror ("FATAL: Can't write %s", stdoutput
->filename
);
850 fill_literal
= f
->fr_literal
+ f
->fr_fix
;
851 fill_size
= f
->fr_var
;
852 count
= f
->fr_offset
;
854 if (fill_size
&& count
)
855 #ifdef BFD_FAST_SECTION_FILL
858 if (fill_size
> sizeof(buf
)) {
859 /* Do it the old way. Can this ever happen? */
862 x
= bfd_set_section_contents (stdoutput
, sec
,
863 fill_literal
, (file_ptr
) offset
,
864 (bfd_size_type
) fill_size
);
867 bfd_perror (stdoutput
->filename
);
868 as_perror ("FATAL: Can't write %s", stdoutput
->filename
);
875 /* Build a buffer full of fill objects and output it as
876 * often as necessary. This saves on the overhead of potentially
877 * lots of bfd_set_section_contents calls.
879 int n_per_buf
, bytes
, i
;
882 n_per_buf
= sizeof (buf
);
883 memset (buf
, *fill_literal
, n_per_buf
);
888 n_per_buf
= sizeof(buf
)/fill_size
;
889 for (i
= n_per_buf
, bufp
= buf
; i
; i
--, bufp
+= fill_size
)
890 memcpy(bufp
, fill_literal
, fill_size
);
892 for (; count
> 0; count
-= n_per_buf
)
894 n_per_buf
= n_per_buf
> count
? count
: n_per_buf
;
895 x
= bfd_set_section_contents (stdoutput
, sec
,
896 buf
, (file_ptr
) offset
,
897 (bfd_size_type
) n_per_buf
* fill_size
);
899 offset
+= n_per_buf
* fill_size
;
906 x
= bfd_set_section_contents (stdoutput
, sec
,
907 fill_literal
, (file_ptr
) offset
,
908 (bfd_size_type
) fill_size
);
917 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
919 merge_data_into_text ()
921 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
922 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
923 seg_info (data_section
)->frchainP
->frch_root
;
924 seg_info (text_section
)->frchainP
->frch_last
=
925 seg_info (data_section
)->frchainP
->frch_last
;
926 seg_info (data_section
)->frchainP
= 0;
930 text_last_frag
->fr_next
= data_frag_root
;
931 text_last_frag
= data_last_frag
;
932 data_last_frag
= NULL
;
933 data_frag_root
= NULL
;
936 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
937 tmp
->fx_next
= data_fix_root
;
938 text_fix_tail
= data_fix_tail
;
941 text_fix_root
= data_fix_root
;
942 data_fix_root
= NULL
;
945 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
947 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
949 relax_and_size_all_segments ()
953 relax_segment (text_frag_root
, SEG_TEXT
);
954 relax_segment (data_frag_root
, SEG_DATA
);
955 relax_segment (bss_frag_root
, SEG_BSS
);
957 * Now the addresses of frags are correct within the segment.
960 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
961 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
962 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
965 * Join the 2 segments into 1 huge segment.
966 * To do this, re-compute every rn_address in the SEG_DATA frags.
967 * Then join the data frags after the text frags.
969 * Determine a_data [length of data segment].
973 register relax_addressT slide
;
975 know ((text_last_frag
->fr_type
== rs_fill
) && (text_last_frag
->fr_offset
== 0));
977 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
978 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
979 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
981 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
982 /* For b.out: If the data section has a strict alignment
983 requirement, its load address in the .o file will be
984 rounded up from the size of the text section. These
985 two values are *not* the same! Similarly for the bss
987 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
990 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
992 fragP
->fr_address
+= slide
;
993 } /* for each data frag */
995 know (text_last_frag
!= 0);
996 text_last_frag
->fr_next
= data_frag_root
;
1000 H_SET_DATA_SIZE (&headers
, 0);
1004 /* See above comments on b.out data section address. */
1007 if (data_last_frag
== 0)
1008 bss_vma
= H_GET_TEXT_SIZE (&headers
);
1010 bss_vma
= data_last_frag
->fr_address
;
1011 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
1012 bss_address_frag
.fr_address
= bss_vma
;
1014 #else /* ! OBJ_BOUT */
1015 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
1016 H_GET_DATA_SIZE (&headers
));
1018 #endif /* ! OBJ_BOUT */
1020 /* Slide all the frags */
1023 relax_addressT slide
= bss_address_frag
.fr_address
;
1025 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1027 fragP
->fr_address
+= slide
;
1028 } /* for each bss frag */
1032 H_SET_BSS_SIZE (&headers
,
1033 bss_last_frag
->fr_address
- bss_frag_root
->fr_address
);
1035 H_SET_BSS_SIZE (&headers
, 0);
1037 #endif /* ! BFD_ASSEMBLER && ! BFD */
1039 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1041 #ifdef BFD_ASSEMBLER
1049 extern PTR bfd_alloc
PARAMS ((bfd
*, size_t));
1051 /* Count symbols. We can't rely on a count made by the loop in
1052 write_object_file, because *_frob_file may add a new symbol or
1055 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1062 asympp
= (asymbol
**) bfd_alloc (stdoutput
,
1063 nsyms
* sizeof (asymbol
*));
1064 symp
= symbol_rootP
;
1065 for (i
= 0; i
< nsyms
; i
++, symp
= symbol_next (symp
))
1067 asympp
[i
] = symp
->bsym
;
1073 result
= bfd_set_symtab (stdoutput
, asympp
, nsyms
);
1074 assert (result
== true);
1075 symbol_table_frozen
= 1;
1080 write_object_file ()
1082 struct frchain
*frchainP
; /* Track along all frchains. */
1083 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1084 fragS
*fragP
; /* Track along all frags. */
1087 /* Do we really want to write it? */
1089 int n_warns
, n_errs
;
1090 n_warns
= had_warnings ();
1091 n_errs
= had_errors ();
1092 /* The -Z flag indicates that an object file should be generated,
1093 regardless of warnings and errors. */
1094 if (flag_always_generate_output
)
1096 if (n_warns
|| n_errs
)
1097 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
1098 n_errs
, n_errs
== 1 ? "" : "s",
1099 n_warns
, n_warns
== 1 ? "" : "s");
1104 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
1105 n_errs
, n_errs
== 1 ? "" : "s",
1106 n_warns
, n_warns
== 1 ? "" : "s");
1111 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1112 a routine to check for the definition of the procedure "_main",
1113 and if so -- fix it up so that it can be program entry point. */
1114 VMS_Check_For_Main ();
1117 /* After every sub-segment, we fake an ".align ...". This conforms to
1118 BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
1119 frag that requires least thought. ".align" frags like to have a
1120 following frag since that makes calculating their intended length
1123 @@ Is this really necessary?? */
1124 #ifndef SUB_SEGMENT_ALIGN
1125 #ifdef BFD_ASSEMBLER
1126 #define SUB_SEGMENT_ALIGN(SEG) (0)
1128 #define SUB_SEGMENT_ALIGN(SEG) (2)
1131 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
1133 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
1134 frag_align (SUB_SEGMENT_ALIGN (now_seg
), NOP_OPCODE
);
1135 /* frag_align will have left a new frag.
1136 Use this last frag for an empty ".fill".
1138 For this segment ...
1139 Create a last frag. Do not leave a "being filled in frag". */
1140 frag_wane (frag_now
);
1141 frag_now
->fr_fix
= 0;
1142 know (frag_now
->fr_next
== NULL
);
1145 /* From now on, we don't care about sub-segments. Build one frag chain
1146 for each segment. Linked thru fr_next. */
1148 #ifdef BFD_ASSEMBLER
1149 /* Remove the sections created by gas for its own purposes. */
1151 asection
**seclist
, *sec
;
1152 seclist
= &stdoutput
->sections
;
1153 while (seclist
&& *seclist
)
1156 while (sec
== reg_section
|| sec
== expr_section
)
1160 stdoutput
->section_count
--;
1165 seclist
= &(*seclist
)->next
;
1169 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
1171 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
1172 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
1173 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
1176 /* We have two segments. If user gave -R flag, then we must put the
1177 data frags into the text segment. Do this before relaxing so
1178 we know to take advantage of -R and make shorter addresses. */
1179 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1180 if (flag_readonly_data_in_text
)
1182 merge_data_into_text ();
1186 #ifdef BFD_ASSEMBLER
1187 bfd_map_over_sections (stdoutput
, relax_and_size_seg
, (char *) 0);
1189 relax_and_size_all_segments ();
1190 #endif /* BFD_ASSEMBLER */
1192 #ifndef BFD_ASSEMBLER
1195 * Crawl the symbol chain.
1197 * For each symbol whose value depends on a frag, take the address of
1198 * that frag and subsume it into the value of the symbol.
1199 * After this, there is just one way to lookup a symbol value.
1200 * Values are left in their final state for object file emission.
1201 * We adjust the values of 'L' local symbols, even if we do
1202 * not intend to emit them to the object file, because their values
1203 * are needed for fix-ups.
1205 * Unless we saw a -L flag, remove all symbols that begin with 'L'
1206 * from the symbol chain. (They are still pointed to by the fixes.)
1208 * Count the remaining symbols.
1209 * Assign a symbol number to each symbol.
1210 * Count the number of string-table chars we will emit.
1211 * Put this info into the headers as appropriate.
1214 know (zero_address_frag
.fr_address
== 0);
1215 string_byte_count
= sizeof (string_byte_count
);
1217 obj_crawl_symbol_chain (&headers
);
1219 if (string_byte_count
== sizeof (string_byte_count
))
1220 string_byte_count
= 0;
1222 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1225 * Addresses of frags now reflect addresses we use in the object file.
1226 * Symbol values are correct.
1227 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1228 * Also converting any machine-dependent frags using md_convert_frag();
1230 subseg_change (SEG_TEXT
, 0);
1232 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1234 cvt_frag_to_fill (&headers
, fragP
);
1236 /* Some assert macros don't work with # directives mixed in. */
1238 if (!(fragP
->fr_next
== NULL
1240 || fragP
->fr_next
== data_frag_root
1242 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
1243 == (fragP
->fr_fix
+ fragP
->fr_offset
* fragP
->fr_var
))))
1247 #endif /* ! BFD_ASSEMBLER */
1249 #ifndef WORKING_DOT_WORD
1251 struct broken_word
*lie
;
1252 struct broken_word
**prevP
;
1254 prevP
= &broken_words
;
1255 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1260 exp
.X_op
= O_subtract
;
1261 exp
.X_add_symbol
= lie
->add
;
1262 exp
.X_op_symbol
= lie
->sub
;
1263 exp
.X_add_number
= lie
->addnum
;
1264 #ifdef BFD_ASSEMBLER
1265 #ifdef TC_CONS_FIX_NEW
1266 TC_CONS_FIX_NEW (lie
->frag
,
1267 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1270 fix_new_exp (lie
->frag
,
1271 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1272 2, &exp
, 0, BFD_RELOC_NONE
);
1275 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1276 fix_new_exp (lie
->frag
,
1277 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1278 2, &exp
, 0, NO_RELOC
);
1281 fix_new_ns32k_exp (lie
->frag
,
1282 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1283 2, &exp
, 0, 0, 2, 0, 0);
1285 fix_new_exp (lie
->frag
,
1286 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1288 #endif /* TC_NS32K */
1289 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1290 #endif /* BFD_ASSEMBLER */
1291 *prevP
= lie
->next_broken_word
;
1294 prevP
= &(lie
->next_broken_word
);
1296 for (lie
= broken_words
; lie
;)
1298 struct broken_word
*untruth
;
1300 addressT table_addr
;
1301 addressT from_addr
, to_addr
;
1304 fragP
= lie
->dispfrag
;
1306 /* Find out how many broken_words go here. */
1308 for (untruth
= lie
; untruth
&& untruth
->dispfrag
== fragP
; untruth
= untruth
->next_broken_word
)
1309 if (untruth
->added
== 1)
1312 table_ptr
= lie
->dispfrag
->fr_opcode
;
1313 table_addr
= lie
->dispfrag
->fr_address
+ (table_ptr
- lie
->dispfrag
->fr_literal
);
1314 /* Create the jump around the long jumps. This is a short
1315 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1316 from_addr
= table_addr
;
1317 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1318 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
, lie
->add
);
1319 table_ptr
+= md_short_jump_size
;
1320 table_addr
+= md_short_jump_size
;
1322 for (m
= 0; lie
&& lie
->dispfrag
== fragP
; m
++, lie
= lie
->next_broken_word
)
1324 if (lie
->added
== 2)
1326 /* Patch the jump table */
1327 /* This is the offset from ??? to table_ptr+0 */
1328 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1329 #ifdef BFD_ASSEMBLER
1330 to_addr
-= lie
->sub
->sy_frag
->fr_address
;
1332 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1333 for (untruth
= lie
->next_broken_word
; untruth
&& untruth
->dispfrag
== fragP
; untruth
= untruth
->next_broken_word
)
1335 if (untruth
->use_jump
== lie
)
1336 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1339 /* Install the long jump */
1340 /* this is a long jump from table_ptr+0 to the final target */
1341 from_addr
= table_addr
;
1342 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1343 #ifdef BFD_ASSEMBLER
1344 to_addr
+= lie
->add
->sy_frag
->fr_address
;
1346 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
, lie
->add
);
1347 table_ptr
+= md_long_jump_size
;
1348 table_addr
+= md_long_jump_size
;
1352 #endif /* not WORKING_DOT_WORD */
1354 #ifndef BFD_ASSEMBLER
1357 long object_file_size
;
1359 * Scan every FixS performing fixups. We had to wait until now to do
1360 * this because md_convert_frag() may have made some fixSs.
1364 subseg_change (SEG_TEXT
, 0);
1365 trsize
= md_reloc_size
* fixup_segment (text_fix_root
, SEG_TEXT
);
1366 subseg_change (SEG_DATA
, 0);
1367 drsize
= md_reloc_size
* fixup_segment (data_fix_root
, SEG_DATA
);
1368 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
1370 /* FIXME move this stuff into the pre-write-hook */
1371 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
1372 H_SET_ENTRY_POINT (&headers
, 0);
1374 obj_pre_write_hook (&headers
); /* extra coff stuff */
1376 object_file_size
= H_GET_FILE_SIZE (&headers
);
1377 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
1379 output_file_create (out_file_name
);
1381 obj_header_append (&next_object_file_charP
, &headers
);
1383 know ((next_object_file_charP
- the_object_file
) == H_GET_HEADER_SIZE (&headers
));
1388 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1390 register long count
;
1391 register char *fill_literal
;
1392 register long fill_size
;
1394 know (fragP
->fr_type
== rs_fill
);
1395 append (&next_object_file_charP
, fragP
->fr_literal
, (unsigned long) fragP
->fr_fix
);
1396 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
1397 fill_size
= fragP
->fr_var
;
1398 know (fragP
->fr_offset
>= 0);
1400 for (count
= fragP
->fr_offset
; count
; count
--)
1402 append (&next_object_file_charP
, fill_literal
, (unsigned long) fill_size
);
1405 } /* for each code frag. */
1407 know ((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE (&headers
) + H_GET_TEXT_SIZE (&headers
) + H_GET_DATA_SIZE (&headers
)));
1412 obj_emit_relocations (&next_object_file_charP
, text_fix_root
, (relax_addressT
) 0);
1413 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
)));
1415 /* Make addresses in data relocation directives relative to beginning of
1416 * first data fragment, not end of last text fragment: alignment of the
1417 * start of the data segment may place a gap between the segments.
1419 obj_emit_relocations (&next_object_file_charP
, data_fix_root
, data0_frchainP
->frch_root
->fr_address
);
1421 obj_emit_relocations (&next_object_file_charP
, data_fix_root
, text_last_frag
->fr_address
);
1422 #endif /* TC_I960 */
1424 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
)));
1427 * Emit line number entries.
1429 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
1430 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
)));
1435 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
1436 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
)));
1442 if (string_byte_count
> 0)
1444 obj_emit_strings (&next_object_file_charP
);
1445 } /* only if we have a string table */
1448 bfd_seek (stdoutput
, 0, 0);
1449 bfd_write (the_object_file
, 1, object_file_size
, stdoutput
);
1452 /* Write the data to the file */
1453 output_file_append (the_object_file
, object_file_size
, out_file_name
);
1455 } /* non vms output */
1458 * Now do the VMS-dependent part of writing the object file
1460 VMS_write_object_file (H_GET_TEXT_SIZE (&headers
),
1461 H_GET_DATA_SIZE (&headers
),
1462 H_GET_BSS_SIZE (&headers
),
1463 text_frag_root
, data_frag_root
);
1465 #else /* BFD_ASSEMBLER */
1467 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *)0);
1469 /* Set up symbol table, and write it out. */
1474 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1478 if (! symp
->sy_resolved
)
1480 if (symp
->sy_value
.X_op
== O_constant
)
1482 /* This is the normal case; skip the call. */
1485 + symp
->sy_frag
->fr_address
));
1486 symp
->sy_resolved
= 1;
1489 resolve_symbol_value (symp
);
1492 /* So far, common symbols have been treated like undefined symbols.
1493 Put them in the common section now. */
1494 if (S_IS_DEFINED (symp
) == 0
1495 && S_GET_VALUE (symp
) != 0)
1496 S_SET_SEGMENT (symp
, bfd_com_section_ptr
);
1498 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1499 S_GET_NAME (symp
), symp
,
1502 segment_name (symp
->bsym
->section
));
1505 #ifdef obj_frob_symbol
1506 obj_frob_symbol (symp
, punt
);
1508 #ifdef tc_frob_symbol
1509 if (! punt
|| symp
->sy_used_in_reloc
)
1510 tc_frob_symbol (symp
, punt
);
1513 /* If we don't want to keep this symbol, splice it out of
1514 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1515 want section symbols. Otherwise, we skip local symbols
1516 and symbols that the frob_symbol macros told us to punt,
1517 but we keep such symbols if they are used in relocs. */
1518 if ((! EMIT_SECTION_SYMBOLS
1519 && (symp
->bsym
->flags
& BSF_SECTION_SYM
) != 0)
1520 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1521 opposites. Sometimes the former checks flags and the
1522 latter examines the name... */
1523 || (!S_IS_EXTERN (symp
)
1524 && (S_IS_LOCAL (symp
) || punt
)
1525 && ! symp
->sy_used_in_reloc
))
1527 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
1528 /* After symbol_remove, symbol_next(symp) still returns
1529 the one that came after it in the chain. So we don't
1530 need to do any extra cleanup work here. */
1535 /* Make sure we really got a value for the symbol. */
1536 if (! symp
->sy_resolved
)
1538 as_bad ("can't resolve value for symbol \"%s\"",
1540 symp
->sy_resolved
= 1;
1543 /* Set the value into the BFD symbol. Up til now the value
1544 has only been kept in the gas symbolS struct. */
1545 symp
->bsym
->value
= S_GET_VALUE (symp
);
1549 /* Now do any format-specific adjustments to the symbol table, such
1550 as adding file symbols. */
1551 #ifdef obj_adjust_symtab
1552 obj_adjust_symtab ();
1555 /* Now that all the sizes are known, and contents correct, we can
1556 start writing to the file. */
1559 /* If *_frob_file changes the symbol value at this point, it is
1560 responsible for moving the changed value into symp->bsym->value
1561 as well. Hopefully all symbol value changing can be done in
1566 #ifdef obj_frob_file
1570 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
1572 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
1573 #endif /* BFD_ASSEMBLER */
1580 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1585 * After this, all frags in this segment have addresses that are correct
1586 * within the segment. Since segments live in different file addresses,
1587 * these frag addresses may not be the same as final object-file addresses.
1590 #ifndef md_relax_frag
1592 /* Subroutines of relax_segment. */
1598 for (; f1
; f1
= f1
->fr_next
)
1599 if (f1
->fr_next
== f2
)
1604 #endif /* ! defined (md_relax_frag) */
1606 /* Relax_align. Advance location counter to next address that has 'alignment'
1607 lowest order bits all 0s, return size of adjustment made. */
1608 static relax_addressT
1609 relax_align (address
, alignment
)
1610 register relax_addressT address
; /* Address now. */
1611 register int alignment
; /* Alignment (binary). */
1613 relax_addressT mask
;
1614 relax_addressT new_address
;
1616 mask
= ~((~0) << alignment
);
1617 new_address
= (address
+ mask
) & (~mask
);
1619 /* We must provide lots of padding, so the linker can discard it
1620 when needed. The linker will not add extra space, ever. */
1621 new_address
+= (1 << alignment
);
1622 return (new_address
- address
);
1626 relax_segment (segment_frag_root
, segment
)
1627 struct frag
*segment_frag_root
;
1630 register struct frag
*fragP
;
1631 register relax_addressT address
;
1632 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1633 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
1635 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1636 subseg_change (segment
, 0);
1638 /* For each frag in segment: count and store (a 1st guess of)
1641 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1643 fragP
->fr_address
= address
;
1644 address
+= fragP
->fr_fix
;
1646 switch (fragP
->fr_type
)
1649 address
+= fragP
->fr_offset
* fragP
->fr_var
;
1654 int offset
= relax_align (address
, (int) fragP
->fr_offset
);
1655 if (offset
% fragP
->fr_var
!= 0)
1657 as_bad ("alignment padding (%d bytes) not a multiple of %ld",
1658 offset
, (long) fragP
->fr_var
);
1659 offset
-= (offset
% fragP
->fr_var
);
1666 /* Assume .org is nugatory. It will grow with 1st relax. */
1669 case rs_machine_dependent
:
1670 address
+= md_estimate_size_before_relax (fragP
, segment
);
1673 #ifndef WORKING_DOT_WORD
1674 /* Broken words don't concern us yet */
1675 case rs_broken_word
:
1680 BAD_CASE (fragP
->fr_type
);
1682 } /* switch(fr_type) */
1683 } /* for each frag in the segment */
1687 long stretch
; /* May be any size, 0 or negative. */
1688 /* Cumulative number of addresses we have */
1689 /* relaxed this pass. */
1690 /* We may have relaxed more than one address. */
1691 long stretched
; /* Have we stretched on this pass? */
1692 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1693 grew, and another shrank. If a branch instruction doesn't fit anymore,
1694 we could be scrod. */
1698 stretch
= stretched
= 0;
1699 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1702 unsigned long was_address
;
1708 was_address
= fragP
->fr_address
;
1709 address
= fragP
->fr_address
+= stretch
;
1710 symbolP
= fragP
->fr_symbol
;
1711 offset
= fragP
->fr_offset
;
1713 switch (fragP
->fr_type
)
1715 case rs_fill
: /* .fill never relaxes. */
1719 #ifndef WORKING_DOT_WORD
1720 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1721 for it I do not want to write it. I do not want to have
1722 anything to do with it. This is not the proper way to
1723 implement this misfeature. */
1724 case rs_broken_word
:
1726 struct broken_word
*lie
;
1727 struct broken_word
*untruth
;
1729 /* Yes this is ugly (storing the broken_word pointer
1730 in the symbol slot). Still, this whole chunk of
1731 code is ugly, and I don't feel like doing anything
1732 about it. Think of it as stubbornness in action. */
1734 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
1735 lie
&& lie
->dispfrag
== fragP
;
1736 lie
= lie
->next_broken_word
)
1742 offset
= (lie
->add
->sy_frag
->fr_address
1743 + S_GET_VALUE (lie
->add
)
1745 - (lie
->sub
->sy_frag
->fr_address
1746 + S_GET_VALUE (lie
->sub
)));
1747 if (offset
<= -32768 || offset
>= 32767)
1749 if (flag_warn_displacement
)
1752 sprint_value (buf
, (addressT
) lie
->addnum
);
1753 as_warn (".word %s-%s+%s didn't fit",
1754 S_GET_NAME (lie
->add
),
1755 S_GET_NAME (lie
->sub
),
1759 if (fragP
->fr_subtype
== 0)
1761 fragP
->fr_subtype
++;
1762 growth
+= md_short_jump_size
;
1764 for (untruth
= lie
->next_broken_word
;
1765 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
1766 untruth
= untruth
->next_broken_word
)
1767 if ((untruth
->add
->sy_frag
== lie
->add
->sy_frag
)
1768 && S_GET_VALUE (untruth
->add
) == S_GET_VALUE (lie
->add
))
1771 untruth
->use_jump
= lie
;
1773 growth
+= md_long_jump_size
;
1778 } /* case rs_broken_word */
1781 growth
= (relax_align ((relax_addressT
) (address
1784 - relax_align ((relax_addressT
) (was_address
1794 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1795 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
1796 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
1797 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
)
1798 || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
1799 know (symbolP
->sy_frag
);
1800 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
1801 || (symbolP
->sy_frag
== &zero_address_frag
));
1803 target
+= S_GET_VALUE (symbolP
)
1804 + symbolP
->sy_frag
->fr_address
;
1805 } /* if we have a symbol */
1807 know (fragP
->fr_next
);
1808 after
= fragP
->fr_next
->fr_address
;
1809 growth
= ((target
- after
) > 0) ? (target
- after
) : 0;
1810 /* Growth may be negative, but variable part of frag
1811 cannot have fewer than 0 chars. That is, we can't
1814 growth
-= stretch
; /* This is an absolute growth factor */
1817 case rs_machine_dependent
:
1818 #ifdef md_relax_frag
1819 growth
= md_relax_frag (fragP
, stretch
);
1821 /* The default way to relax a frag is to look through
1824 const relax_typeS
*this_type
;
1825 const relax_typeS
*start_type
;
1826 relax_substateT next_state
;
1827 relax_substateT this_state
;
1830 this_state
= fragP
->fr_subtype
;
1831 start_type
= this_type
= md_relax_table
+ this_state
;
1836 #ifndef DIFF_EXPR_OK
1837 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1838 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
1839 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
1840 || (S_GET_SEGMENT (symbolP
) == SEG_BSS
)
1841 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
1843 know (symbolP
->sy_frag
);
1845 know (!(S_GET_SEGMENT (symbolP
) == absolute_section
)
1846 || symbolP
->sy_frag
== &zero_address_frag
);
1848 S_GET_VALUE (symbolP
)
1849 + symbolP
->sy_frag
->fr_address
;
1851 /* If frag has yet to be reached on this pass,
1852 assume it will move by STRETCH just as we did.
1853 If this is not so, it will be because some frag
1854 between grows, and that will force another pass.
1856 Beware zero-length frags.
1858 There should be a faster way to do this. */
1860 if (symbolP
->sy_frag
->fr_address
>= was_address
1861 && is_dnrange (fragP
, symbolP
->sy_frag
))
1867 aim
= target
- address
- fragP
->fr_fix
;
1868 #ifdef TC_PCREL_ADJUST
1869 /* Currently only the ns32k family needs this */
1870 aim
+= TC_PCREL_ADJUST(fragP
);
1872 /* This machine doesn't want to use pcrel_adjust.
1873 In that case, pcrel_adjust should be zero. */
1874 assert (fragP
->fr_pcrel_adjust
== 0);
1879 /* Look backwards. */
1880 for (next_state
= this_type
->rlx_more
; next_state
;)
1881 if (aim
>= this_type
->rlx_backward
)
1885 /* Grow to next state. */
1886 this_state
= next_state
;
1887 this_type
= md_relax_table
+ this_state
;
1888 next_state
= this_type
->rlx_more
;
1893 #ifdef M68K_AIM_KLUDGE
1894 M68K_AIM_KLUDGE (aim
, this_state
, this_type
);
1896 /* Look forwards. */
1897 for (next_state
= this_type
->rlx_more
; next_state
;)
1898 if (aim
<= this_type
->rlx_forward
)
1902 /* Grow to next state. */
1903 this_state
= next_state
;
1904 this_type
= md_relax_table
+ this_state
;
1905 next_state
= this_type
->rlx_more
;
1909 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
1911 fragP
->fr_subtype
= this_state
;
1917 BAD_CASE (fragP
->fr_type
);
1925 } /* For each frag in the segment. */
1927 while (stretched
); /* Until nothing further to relax. */
1931 * We now have valid fr_address'es for each frag.
1935 * All fr_address's are correct, relative to their own segment.
1936 * We have made all the fixS we will ever make.
1938 } /* relax_segment() */
1940 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1942 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
1943 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
1948 Go through all the fixS's in a segment and see which ones can be
1949 handled now. (These consist of fixS where we have since discovered
1950 the value of a symbol, or the address of the frag involved.)
1951 For each one, call md_apply_fix to put the fix into the frag data.
1953 Result is a count of how many relocation structs will be needed to
1954 handle the remaining fixS's that we couldn't completely handle here.
1955 These will be output later by emit_relocations(). */
1958 fixup_segment (fixP
, this_segment_type
)
1959 register fixS
*fixP
;
1960 segT this_segment_type
; /* N_TYPE bits for segment. */
1962 long seg_reloc_count
= 0;
1963 symbolS
*add_symbolP
;
1964 symbolS
*sub_symbolP
;
1971 segT add_symbol_segment
= absolute_section
;
1973 /* If the linker is doing the relaxing, we must not do any fixups.
1975 Well, strictly speaking that's not true -- we could do any that are
1976 PC-relative and don't cross regions that could change size. And for the
1977 i960 (the only machine for which we've got a relaxing linker right now),
1978 we might be able to turn callx/callj into bal anyways in cases where we
1979 know the maximum displacement. */
1982 for (; fixP
; fixP
= fixP
->fx_next
)
1984 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
1985 return seg_reloc_count
;
1988 for (; fixP
; fixP
= fixP
->fx_next
)
1990 fragP
= fixP
->fx_frag
;
1992 where
= fixP
->fx_where
;
1993 place
= fragP
->fr_literal
+ where
;
1994 size
= fixP
->fx_size
;
1995 add_symbolP
= fixP
->fx_addsy
;
1996 #ifdef TC_VALIDATE_FIX
1997 TC_VALIDATE_FIX (fixP
, this_segment_type
, skip
);
1999 sub_symbolP
= fixP
->fx_subsy
;
2000 add_number
= fixP
->fx_offset
;
2001 pcrel
= fixP
->fx_pcrel
;
2004 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2011 if (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)
2012 add_number
-= S_GET_VALUE (sub_symbolP
);
2014 && S_GET_SEGMENT (sub_symbolP
) == this_segment_type
)
2016 /* Should try converting to a constant. */
2021 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2022 "Negative of non-absolute symbol %s",
2023 S_GET_NAME (sub_symbolP
));
2025 else if ((S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
)
2026 && (SEG_NORMAL (add_symbol_segment
)
2027 || (add_symbol_segment
== absolute_section
)))
2029 /* Difference of 2 symbols from same segment.
2030 Can't make difference of 2 undefineds: 'value' means
2031 something different for N_UNDF. */
2033 /* Makes no sense to use the difference of 2 arbitrary symbols
2034 as the target of a call instruction. */
2036 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2037 "callj to difference of 2 symbols");
2038 #endif /* TC_I960 */
2039 add_number
+= S_GET_VALUE (add_symbolP
) -
2040 S_GET_VALUE (sub_symbolP
);
2044 /* Let the target machine make the final determination
2045 as to whether or not a relocation will be needed to
2046 handle this fixup. */
2047 if (!TC_FORCE_RELOCATION (fixP
))
2049 fixP
->fx_addsy
= NULL
;
2054 /* Different segments in subtraction. */
2055 know (!(S_IS_EXTERNAL (sub_symbolP
)
2056 && (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)));
2058 if ((S_GET_SEGMENT (sub_symbolP
) == absolute_section
))
2059 add_number
-= S_GET_VALUE (sub_symbolP
);
2062 else if (S_GET_SEGMENT (sub_symbolP
) == this_segment_type
2063 #if 0 /* Do this even if it's already described as pc-relative. For example,
2064 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
2065 pc-relative mode. */
2070 /* Make it pc-relative. */
2071 add_number
+= (md_pcrel_from (fixP
)
2072 - S_GET_VALUE (sub_symbolP
));
2079 #ifdef BFD_ASSEMBLER
2080 else if (fixP
->fx_r_type
== BFD_RELOC_GPREL32
2081 || fixP
->fx_r_type
== BFD_RELOC_GPREL16
)
2083 /* Leave it alone. */
2089 sprint_value (buf
, fragP
->fr_address
+ where
);
2090 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2091 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
2092 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2093 S_GET_NAME (sub_symbolP
), buf
);
2100 if (add_symbol_segment
== this_segment_type
&& pcrel
2101 && TC_RELOC_RTSYM_LOC_FIXUP (fixP
->fx_r_type
))
2104 * This fixup was made when the symbol's segment was
2105 * SEG_UNKNOWN, but it is now in the local segment.
2106 * So we know how to do the address without relocation.
2109 /* reloc_callj() may replace a 'call' with a 'calls' or a
2110 'bal', in which cases it modifies *fixP as appropriate.
2111 In the case of a 'calls', no further work is required,
2112 and *fixP has been set up to make the rest of the code
2115 #endif /* TC_I960 */
2117 add_number
+= S_GET_VALUE (add_symbolP
);
2118 add_number
-= md_pcrel_from (fixP
);
2119 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
2121 /* Let the target machine make the final determination
2122 as to whether or not a relocation will be needed to
2123 handle this fixup. */
2124 if (!TC_FORCE_RELOCATION (fixP
))
2127 fixP
->fx_addsy
= NULL
;
2132 if (add_symbol_segment
== absolute_section
)
2135 /* See comment about reloc_callj() above. */
2137 #endif /* TC_I960 */
2138 add_number
+= S_GET_VALUE (add_symbolP
);
2140 /* Let the target machine make the final determination
2141 as to whether or not a relocation will be needed to
2142 handle this fixup. */
2143 if (!TC_FORCE_RELOCATION (fixP
))
2145 fixP
->fx_addsy
= NULL
;
2149 else if (add_symbol_segment
== undefined_section
2150 #ifdef BFD_ASSEMBLER
2151 || bfd_is_com_section (add_symbol_segment
)
2156 if ((int) fixP
->fx_bit_fixP
== 13)
2158 /* This is a COBR instruction. They have only a
2159 * 13-bit displacement and are only to be used
2160 * for local branches: flag as error, don't generate
2163 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2164 "can't use COBR format with external label");
2165 fixP
->fx_addsy
= NULL
;
2169 #endif /* TC_I960 */
2173 if (S_IS_COMMON (add_symbolP
))
2174 add_number
+= S_GET_VALUE (add_symbolP
);
2175 #endif /* TE_I386AIX */
2176 #endif /* OBJ_COFF */
2182 add_number
+= S_GET_VALUE (add_symbolP
);
2189 add_number
-= md_pcrel_from (fixP
);
2190 if (add_symbolP
== 0)
2192 #ifndef BFD_ASSEMBLER
2193 fixP
->fx_addsy
= &abs_symbol
;
2195 fixP
->fx_addsy
= section_symbol (absolute_section
);
2197 fixP
->fx_addsy
->sy_used_in_reloc
= 1;
2202 if (!fixP
->fx_bit_fixP
&& size
> 0)
2205 if (size
< sizeof (mask
))
2207 /* set all bits to one */
2209 /* Technically, combining these produces an undefined result
2210 if size is sizeof (valueT), though I think these two
2211 half-way operations should both be defined. And the
2212 compiler should be able to combine them if it's valid on
2213 the host architecture. */
2216 if ((add_number
& mask
) != 0
2217 && (add_number
& mask
) != mask
)
2219 char buf
[50], buf2
[50];
2220 sprint_value (buf
, fragP
->fr_address
+ where
);
2221 if (add_number
> 1000)
2222 sprint_value (buf2
, add_number
);
2224 sprintf (buf2
, "%ld", (long) add_number
);
2225 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2226 "Value of %s too large for field of %d bytes at %s",
2228 } /* generic error checking */
2230 #ifdef WARN_SIGNED_OVERFLOW_WORD
2231 /* Warn if a .word value is too large when treated as a signed
2232 number. We already know it is not too negative. This is to
2233 catch over-large switches generated by gcc on the 68k. */
2234 if (!flag_signed_overflow_ok
2236 && add_number
> 0x7fff)
2237 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2238 "Signed .word overflow; switch may be too large; %ld at 0x%lx",
2240 (unsigned long) (fragP
->fr_address
+ where
));
2242 } /* not a bit fix */
2246 #ifdef BFD_ASSEMBLER
2247 md_apply_fix (fixP
, &add_number
);
2249 md_apply_fix (fixP
, add_number
);
2252 #ifndef TC_HANDLES_FX_DONE
2253 /* If the tc-* files haven't been converted, assume it's handling
2254 it the old way, where a null fx_addsy means that the fix has
2255 been applied completely, and no further work is needed. */
2256 if (fixP
->fx_addsy
== 0 && fixP
->fx_pcrel
== 0)
2260 #ifdef TC_VALIDATE_FIX
2264 } /* For each fixS in this segment. */
2266 TC_ADJUST_RELOC_COUNT (fixP
, seg_reloc_count
);
2267 return seg_reloc_count
;
2270 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
2273 number_to_chars_bigendian (buf
, val
, n
)
2278 if (n
> sizeof (val
)|| n
<= 0)
2282 buf
[n
] = val
& 0xff;
2288 number_to_chars_littleendian (buf
, val
, n
)
2293 if (n
> sizeof (val
) || n
<= 0)
2297 *buf
++ = val
& 0xff;
2302 /* end of write.c */