1 /* write.c - emit .o file
2 Copyright (C) 1986, 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* This thing should be set up to do byteordering correctly. But... */
25 #include "output-file.h"
27 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
28 instruction so that the disassembler does not choke on it. */
30 #define NOP_OPCODE 0x00
33 #ifndef WORKING_DOT_WORD
34 extern CONST
int md_short_jump_size
;
35 extern CONST
int md_long_jump_size
;
41 struct frag
*text_frag_root
;
42 struct frag
*data_frag_root
;
43 struct frag
*bss_frag_root
;
45 struct frag
*text_last_frag
; /* Last frag in segment. */
46 struct frag
*data_last_frag
; /* Last frag in segment. */
47 static struct frag
*bss_last_frag
; /* Last frag in segment. */
50 static object_headers headers
;
51 long string_byte_count
;
52 static char *the_object_file
;
53 char *next_object_file_charP
; /* Tracks object file bytes. */
56 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
59 #endif /* BFD_ASSEMBLER */
62 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, short int size
,
63 symbolS
*add
, symbolS
*sub
,
64 offsetT offset
, int pcrel
,
65 bfd_reloc_code_real_type r_type
));
67 static fixS
*fix_new_internal
PARAMS ((fragS
*, int where
, short int size
,
68 symbolS
*add
, symbolS
*sub
,
69 offsetT offset
, int pcrel
,
72 static long fixup_segment
PARAMS ((fixS
* fixP
, segT this_segment_type
));
73 static relax_addressT relax_align
PARAMS ((relax_addressT addr
, int align
));
74 void relax_segment
PARAMS ((struct frag
* seg_frag_root
, segT seg_type
));
79 * Create a fixS in obstack 'notes'.
82 fix_new_internal (frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
,
84 fragS
*frag
; /* Which frag? */
85 int where
; /* Where in that frag? */
86 short int size
; /* 1, 2, or 4 usually. */
87 symbolS
*add_symbol
; /* X_add_symbol. */
88 symbolS
*sub_symbol
; /* X_op_symbol. */
89 offsetT offset
; /* X_add_number. */
90 int pcrel
; /* TRUE if PC-relative relocation. */
92 bfd_reloc_code_real_type r_type
; /* Relocation type */
94 int r_type
; /* Relocation type */
99 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
101 fixP
->fx_frag
= frag
;
102 fixP
->fx_where
= where
;
103 fixP
->fx_size
= size
;
104 fixP
->fx_addsy
= add_symbol
;
105 fixP
->fx_subsy
= sub_symbol
;
106 fixP
->fx_offset
= offset
;
107 fixP
->fx_pcrel
= pcrel
;
108 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
109 fixP
->fx_r_type
= r_type
;
111 fixP
->fx_im_disp
= 0;
112 fixP
->fx_pcrel_adjust
= 0;
113 fixP
->fx_bit_fixP
= 0;
114 fixP
->fx_addnumber
= 0;
123 /* Usually, we want relocs sorted numerically, but while
124 comparing to older versions of gas that have relocs
125 reverse sorted, it is convenient to have this compile
126 time option. xoxorich. */
131 fixS
**seg_fix_rootP
= & (seg_info (now_seg
)->fix_root
);
132 fixS
**seg_fix_tailP
= & (seg_info (now_seg
)->fix_tail
);
135 #ifdef REVERSE_SORT_RELOCS
137 fixP
->fx_next
= *seg_fix_rootP
;
138 *seg_fix_rootP
= fixP
;
140 #else /* REVERSE_SORT_RELOCS */
142 fixP
->fx_next
= NULL
;
145 (*seg_fix_tailP
)->fx_next
= fixP
;
147 *seg_fix_rootP
= fixP
;
148 *seg_fix_tailP
= fixP
;
150 #endif /* REVERSE_SORT_RELOCS */
157 /* Create a fixup relative to a symbol (plus a constant). */
160 fix_new (frag
, where
, size
, add_symbol
, offset
, pcrel
, r_type
)
161 fragS
*frag
; /* Which frag? */
162 int where
; /* Where in that frag? */
163 short int size
; /* 1, 2, or 4 usually. */
164 symbolS
*add_symbol
; /* X_add_symbol. */
165 offsetT offset
; /* X_add_number. */
166 int pcrel
; /* TRUE if PC-relative relocation. */
168 bfd_reloc_code_real_type r_type
; /* Relocation type */
170 int r_type
; /* Relocation type */
173 return fix_new_internal (frag
, where
, size
, add_symbol
,
174 (symbolS
*) NULL
, offset
, pcrel
, r_type
);
177 /* Create a fixup for an expression. Currently we only support fixups
178 for difference expressions. That is itself more than most object
179 file formats support anyhow. */
182 fix_new_exp (frag
, where
, size
, exp
, pcrel
, r_type
)
183 fragS
*frag
; /* Which frag? */
184 int where
; /* Where in that frag? */
185 short int size
; /* 1, 2, or 4 usually. */
186 expressionS
*exp
; /* Expression. */
187 int pcrel
; /* TRUE if PC-relative relocation. */
189 bfd_reloc_code_real_type r_type
; /* Relocation type */
191 int r_type
; /* Relocation type */
204 sub
= exp
->X_op_symbol
;
207 add
= exp
->X_add_symbol
;
210 off
= exp
->X_add_number
;
214 as_bad ("expression too complex for fixup");
217 return fix_new_internal (frag
, where
, size
, add
, sub
, off
,
221 /* Append a string onto another string, bumping the pointer along. */
223 append (charPP
, fromP
, length
)
226 unsigned long length
;
228 /* Don't trust memcpy() of 0 chars. */
232 memcpy (*charPP
, fromP
, (int) length
);
236 #ifndef BFD_ASSEMBLER
237 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
241 * This routine records the largest alignment seen for each segment.
242 * If the beginning of the segment is aligned on the worst-case
243 * boundary, all of the other alignments within it will work. At
244 * least one object format really uses this info.
247 record_alignment (seg
, align
)
248 /* Segment to which alignment pertains */
250 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
255 if (align
> bfd_get_section_alignment (stdoutput
, seg
))
256 bfd_set_section_alignment (stdoutput
, seg
, align
);
258 if (align
> section_alignment
[(int) seg
])
259 section_alignment
[(int) seg
] = align
;
263 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
266 chain_frchains_together_1 (section
, frchp
)
268 struct frchain
*frchp
;
270 fragS dummy
, *prev_frag
= &dummy
;
271 for (; frchp
&& frchp
->frch_seg
== section
; frchp
= frchp
->frch_next
)
273 prev_frag
->fr_next
= frchp
->frch_root
;
274 prev_frag
= frchp
->frch_last
;
276 prev_frag
->fr_next
= 0;
285 chain_frchains_together (abfd
, section
, xxx
)
286 bfd
*abfd
; /* unused */
288 char *xxx
; /* unused */
290 segment_info_type
*info
;
292 /* BFD may have introduced its own sections without using
293 subseg_new, so it is possible that seg_info is NULL. */
294 info
= seg_info (section
);
295 if (info
!= (segment_info_type
*) NULL
)
296 chain_frchains_together_1 (section
, info
->frchainP
);
301 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
304 remove_subsegs (head
, seg
, root
, last
)
310 *root
= head
->frch_root
;
311 *last
= chain_frchains_together_1 (seg
, head
);
317 cvt_frag_to_fill (x
, fragP
)
328 object_headers
*headers
= x
;
331 switch (fragP
->fr_type
)
336 HANDLE_ALIGN (fragP
);
338 fragP
->fr_type
= rs_fill
;
339 know (fragP
->fr_var
== 1);
340 know (fragP
->fr_next
!= NULL
);
342 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
350 case rs_machine_dependent
:
352 md_convert_frag (stdoutput
, sec
, fragP
);
354 md_convert_frag (headers
, fragP
);
357 assert (fragP
->fr_next
== NULL
|| (fragP
->fr_next
->fr_address
- fragP
->fr_address
== fragP
->fr_fix
));
360 * After md_convert_frag, we make the frag into a ".space 0".
361 * Md_convert_frag() should set up any fixSs and constants
367 #ifndef WORKING_DOT_WORD
370 struct broken_word
*lie
;
372 if (fragP
->fr_subtype
)
374 fragP
->fr_fix
+= md_short_jump_size
;
375 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
376 lie
&& lie
->dispfrag
== fragP
;
377 lie
= lie
->next_broken_word
)
379 fragP
->fr_fix
+= md_long_jump_size
;
387 BAD_CASE (fragP
->fr_type
);
394 relax_and_size_seg (abfd
, sec
, xxx
)
401 flags
= bfd_get_section_flags (abfd
, sec
);
403 if (/*flags & SEC_ALLOC*/ 1)
406 segment_info_type
*seginfo
;
408 valueT size
, newsize
;
410 seginfo
= (segment_info_type
*) bfd_get_section_userdata (abfd
, sec
);
411 if (seginfo
&& seginfo
->frchainP
)
413 relax_segment (seginfo
->frchainP
->frch_root
, sec
);
414 for (fragp
= seginfo
->frchainP
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
415 cvt_frag_to_fill (sec
, fragp
);
416 for (fragp
= seginfo
->frchainP
->frch_root
;
418 fragp
= fragp
->fr_next
)
419 /* walk to last elt */;
420 size
= fragp
->fr_address
+ fragp
->fr_fix
;
426 flags
|= SEC_HAS_CONTENTS
;
427 /* @@ This is just an approximation. */
428 if (seginfo
->fix_root
)
432 x
= bfd_set_section_flags (abfd
, sec
, flags
);
435 size
= md_section_align (sec
, size
);
436 x
= bfd_set_section_size (abfd
, sec
, size
);
439 /* If the size had to be rounded up, add some padding in the last
441 newsize
= bfd_get_section_size_before_reloc (sec
);
442 assert (newsize
>= size
);
445 fragS
*last
= seginfo
->frchainP
->frch_last
;
446 fragp
= seginfo
->frchainP
->frch_root
;
447 while (fragp
->fr_next
!= last
)
448 fragp
= fragp
->fr_next
;
449 last
->fr_address
= size
;
450 fragp
->fr_offset
+= newsize
- size
;
453 #ifdef tc_frob_section
454 tc_frob_section (sec
);
456 #ifdef obj_frob_section
457 obj_frob_section (sec
);
463 dump_section_relocs (abfd
, sec
, stream_
)
468 FILE *stream
= (FILE *) stream_
;
469 segment_info_type
*seginfo
= seg_info (sec
);
470 fixS
*fixp
= seginfo
->fix_root
;
475 fprintf (stream
, "sec %s relocs:\n", sec
->name
);
478 symbolS
*s
= fixp
->fx_addsy
;
480 fprintf (stream
, " %08x: %s(%s+%x)+%x\n", fixp
,
481 S_GET_NAME (s
), s
->bsym
->section
->name
,
482 S_GET_VALUE (s
), fixp
->fx_offset
);
484 fprintf (stream
, " %08x: type %d no sym\n", fixp
, fixp
->fx_r_type
);
485 fixp
= fixp
->fx_next
;
489 #define dump_section_relocs(ABFD,SEC,STREAM) (void)(ABFD,SEC,STREAM)
493 adjust_reloc_syms (abfd
, sec
, xxx
)
498 segment_info_type
*seginfo
= seg_info (sec
);
504 dump_section_relocs (abfd
, sec
, stderr
);
506 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
509 symbolS
*sym
= fixp
->fx_addsy
;
510 asection
*symsec
= sym
->bsym
->section
;
511 segment_info_type
*symseginfo
= seg_info (symsec
);
513 /* If it's one of these sections, assume the symbol is definitely
514 going to be output. */
515 if (symsec
== &bfd_und_section
516 || symsec
== &bfd_abs_section
517 || bfd_is_com_section (symsec
))
520 /* Since we're reducing to section symbols, don't attempt to reduce
521 anything that's already using one. */
522 if (sym
->bsym
== symsec
->symbol
)
525 /* Is there some other reason we can't adjust this one? (E.g.,
526 call/bal links in i960-bout symbols.) */
527 #ifdef obj_fix_adjustable
528 if (! obj_fix_adjustable (fixp
))
531 /* If the section symbol isn't going to be output, the relocs
532 at least should still work. If not, figure out what to do
533 when we run into that case. */
534 fixp
->fx_offset
+= S_GET_VALUE (sym
);
536 fixp
->fx_offset
+= sym
->sy_frag
->fr_address
;
538 fixp
->fx_addsy
= symseginfo
->sym
;
541 fixp
->fx_addsy
= symbol_find (symsec
->name
);
544 fixp
->fx_addsy
= symbol_make (symsec
->name
);
545 fixp
->fx_addsy
->bsym
= symsec
->symbol
;
547 symseginfo
->sym
= fixp
->fx_addsy
;
551 dump_section_relocs (abfd
, sec
, stderr
);
555 write_relocs (abfd
, sec
, xxx
)
560 segment_info_type
*seginfo
= seg_info (sec
);
561 unsigned long offset
= 0;
567 /* If seginfo is NULL, we did not create this section; don't do
572 fixup_segment (seginfo
->fix_root
, sec
);
575 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
578 #ifndef RELOC_EXPANSION_POSSIBLE
579 /* Set up reloc information as well. */
580 relocs
= (arelent
**) bfd_alloc_by_size_t (stdoutput
,
581 n
* sizeof (arelent
*));
582 memset ((char*)relocs
, 0, n
* sizeof (arelent
*));
585 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
588 extern arelent
*tc_gen_reloc ();
590 bfd_reloc_status_type s
;
592 if (fixp
->fx_addsy
== 0)
594 /* @@ Need some other flag to indicate which have already
599 reloc
= tc_gen_reloc (sec
, fixp
);
605 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
606 /* @@ Assumes max size of reloc is 4. */
607 if (fixp
->fx_where
+ 4
608 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
610 /* Pass bogus address so that when bfd_perform_relocation adds
611 `address' back in, it'll come up with `data', which is where
612 we want it to operate. */
613 s
= bfd_perform_relocation (stdoutput
, reloc
, data
- reloc
->address
,
620 as_fatal ("bad return from bfd_perform_relocation");
625 n
= n
* MAX_RELOC_EXPANSION
;
626 /* Set up reloc information as well. */
627 relocs
= (arelent
**) bfd_alloc_by_size_t (stdoutput
,
628 n
* sizeof (arelent
*));
631 for (fixp
= seginfo
->fix_root
; fixp
!= (fixS
*) NULL
; fixp
= fixp
->fx_next
)
634 extern arelent
*tc_gen_reloc ();
636 bfd_reloc_status_type s
;
639 if (fixp
->fx_addsy
== 0)
641 /* @@ Need some other flag to indicate which have already
646 reloc
= tc_gen_reloc (sec
, fixp
);
648 for (j
= 0; reloc
[j
]; j
++)
650 relocs
[i
++] = reloc
[j
];
653 data
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
654 if (fixp
->fx_where
+ 4
655 > fixp
->fx_frag
->fr_fix
+ fixp
->fx_frag
->fr_offset
)
657 for (j
= 0; reloc
[j
]; j
++)
659 s
= bfd_perform_relocation (stdoutput
, reloc
[j
], data
- reloc
[j
]->address
,
666 as_fatal ("bad return from bfd_perform_relocation");
674 bfd_set_reloc (stdoutput
, sec
, relocs
, n
);
676 bfd_set_section_flags (abfd
, sec
,
677 bfd_get_section_flags (abfd
, sec
) & ~SEC_RELOC
);
683 fprintf (stderr
, "relocs for sec %s\n", sec
->name
);
684 for (i
= 0; i
< n
; i
++)
688 fprintf (stderr
, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
689 i
, r
, r
->address
, s
->name
, r
->addend
);
696 write_contents (abfd
, sec
, xxx
)
701 segment_info_type
*seginfo
= seg_info (sec
);
702 unsigned long offset
= 0;
708 /* Write out the frags. */
709 if (! (bfd_get_section_flags (abfd
, sec
) & SEC_HAS_CONTENTS
))
712 #if 0 /* Who cares? Let the first call, below, do it. */
713 /* Force calculations (size, vma) to get done. */
714 bfd_set_section_contents (stdoutput
, sec
, "", 0, (addressT
) 0);
717 for (frags
= seginfo
->frchainP
->frch_root
;
719 frags
= frags
->fr_next
)
722 unsigned long fill_size
;
726 assert (frags
->fr_type
== rs_fill
);
729 x
= bfd_set_section_contents (stdoutput
, sec
,
730 frags
->fr_literal
, offset
,
733 offset
+= frags
->fr_fix
;
735 fill_literal
= frags
->fr_literal
+ frags
->fr_fix
;
736 fill_size
= frags
->fr_var
;
737 count
= frags
->fr_offset
;
739 if (fill_size
&& count
)
742 x
= bfd_set_section_contents (stdoutput
, sec
,
743 fill_literal
, offset
,
744 (bfd_size_type
) fill_size
);
752 #if defined (BFD_ASSEMBLER) || !defined (BFD)
757 register struct frchain
*frchainP
; /* Track along all frchains. */
758 register fragS
*fragP
; /* Track along all frags. */
760 #if !defined (BFD_ASSEMBLER) && !defined (OBJ_VMS)
761 long object_file_size
;
764 /* Do we really want to write it? */
767 n_warns
= had_warnings ();
768 n_errs
= had_errors ();
769 /* The -Z flag indicates that an object file should be generated,
770 regardless of warnings and errors. */
773 if (n_warns
|| n_errs
)
774 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
775 n_errs
, n_errs
== 1 ? "" : "s",
776 n_warns
, n_warns
== 1 ? "" : "s");
781 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
782 n_errs
, n_errs
== 1 ? "" : "s",
783 n_warns
, n_warns
== 1 ? "" : "s");
789 * Under VMS we try to be compatible with VAX-11 "C". Thus, we
790 * call a routine to check for the definition of the procedure
791 * "_main", and if so -- fix it up so that it can be program
794 VMS_Check_For_Main ();
797 /* After every sub-segment, we fake an ".align ...". This conforms to
798 BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
799 frag that requires least thought. ".align" frags like to have a
800 following frag since that makes calculating their intended length
803 @@ Is this really necessary?? */
804 #ifndef SUB_SEGMENT_ALIGN
806 #define SUB_SEGMENT_ALIGN(SEG) (0)
808 #define SUB_SEGMENT_ALIGN(SEG) (2)
811 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
814 subseg_set (frchainP
->frch_seg
, frchainP
->frch_subseg
);
816 subseg_new (frchainP
->frch_seg
, frchainP
->frch_subseg
);
818 frag_align (SUB_SEGMENT_ALIGN (now_seg
), NOP_OPCODE
);
819 /* frag_align will have left a new frag.
820 Use this last frag for an empty ".fill".
823 Create a last frag. Do not leave a "being filled in frag". */
824 frag_wane (frag_now
);
825 frag_now
->fr_fix
= 0;
826 know (frag_now
->fr_next
== NULL
);
827 /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
828 /* Above shows we haven't left a half-completed object on obstack. */
831 /* From now on, we don't care about sub-segments. Build one frag chain
832 for each segment. Linked thru fr_next. */
835 /* Remove the sections created by gas for its own purposes. */
837 asection
**seclist
, *sec
;
838 seclist
= &stdoutput
->sections
;
839 while (seclist
&& *seclist
)
842 while (sec
== reg_section
|| sec
== expr_section
)
846 stdoutput
->section_count
--;
851 seclist
= &(*seclist
)->next
;
855 bfd_map_over_sections (stdoutput
, chain_frchains_together
, (char *) 0);
857 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
858 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
859 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
862 /* We have two segments. If user gave -R flag, then we must put the
863 data frags into the text segment. Do this before relaxing so
864 we know to take advantage of -R and make shorter addresses. */
865 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
869 seg_info (text_section
)->frchainP
->frch_last
->fr_next
=
870 seg_info (data_section
)->frchainP
->frch_root
;
871 seg_info (text_section
)->frchainP
->frch_last
=
872 seg_info (data_section
)->frchainP
->frch_last
;
873 seg_info (data_section
)->frchainP
= 0;
877 text_last_frag
->fr_next
= data_frag_root
;
878 text_last_frag
= data_last_frag
;
879 data_last_frag
= NULL
;
880 data_frag_root
= NULL
;
883 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
884 tmp
->fx_next
= data_fix_root
;
885 text_fix_tail
= data_fix_tail
;
888 text_fix_root
= data_fix_root
;
889 data_fix_root
= NULL
;
896 bfd_map_over_sections (stdoutput
, relax_and_size_seg
, (char *) 0);
898 relax_segment (text_frag_root
, SEG_TEXT
);
899 relax_segment (data_frag_root
, SEG_DATA
);
900 relax_segment (bss_frag_root
, SEG_BSS
);
902 * Now the addresses of frags are correct within the segment.
905 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
906 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
907 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
910 * Join the 2 segments into 1 huge segment.
911 * To do this, re-compute every rn_address in the SEG_DATA frags.
912 * Then join the data frags after the text frags.
914 * Determine a_data [length of data segment].
918 register relax_addressT slide
;
920 know ((text_last_frag
->fr_type
== rs_fill
) && (text_last_frag
->fr_offset
== 0));
922 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
923 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
924 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
926 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
927 /* For b.out: If the data section has a strict alignment
928 requirement, its load address in the .o file will be
929 rounded up from the size of the text section. These
930 two values are *not* the same! Similarly for the bss
932 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
935 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
937 fragP
->fr_address
+= slide
;
938 } /* for each data frag */
940 know (text_last_frag
!= 0);
941 text_last_frag
->fr_next
= data_frag_root
;
945 H_SET_DATA_SIZE (&headers
, 0);
949 /* See above comments on b.out data section address. */
952 if (data_last_frag
== 0)
953 bss_vma
= H_GET_TEXT_SIZE (&headers
);
955 bss_vma
= data_last_frag
->fr_address
;
956 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
957 bss_address_frag
.fr_address
= bss_vma
;
959 #else /* ! OBJ_BOUT */
960 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
961 H_GET_DATA_SIZE (&headers
));
964 /* Slide all the frags */
967 relax_addressT slide
= bss_address_frag
.fr_address
;
969 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
971 fragP
->fr_address
+= slide
;
972 } /* for each bss frag */
975 #endif /* ! OBJ_BOUT */
978 H_SET_BSS_SIZE (&headers
,
979 bss_last_frag
->fr_address
- bss_frag_root
->fr_address
);
981 H_SET_BSS_SIZE (&headers
, 0);
982 #endif /* BFD_ASSEMBLER */
984 #ifndef BFD_ASSEMBLER
987 * Crawl the symbol chain.
989 * For each symbol whose value depends on a frag, take the address of
990 * that frag and subsume it into the value of the symbol.
991 * After this, there is just one way to lookup a symbol value.
992 * Values are left in their final state for object file emission.
993 * We adjust the values of 'L' local symbols, even if we do
994 * not intend to emit them to the object file, because their values
995 * are needed for fix-ups.
997 * Unless we saw a -L flag, remove all symbols that begin with 'L'
998 * from the symbol chain. (They are still pointed to by the fixes.)
1000 * Count the remaining symbols.
1001 * Assign a symbol number to each symbol.
1002 * Count the number of string-table chars we will emit.
1003 * Put this info into the headers as appropriate.
1006 know (zero_address_frag
.fr_address
== 0);
1007 string_byte_count
= sizeof (string_byte_count
);
1009 obj_crawl_symbol_chain (&headers
);
1011 if (string_byte_count
== sizeof (string_byte_count
))
1012 string_byte_count
= 0;
1014 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1017 * Addresses of frags now reflect addresses we use in the object file.
1018 * Symbol values are correct.
1019 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1020 * Also converting any machine-dependent frags using md_convert_frag();
1022 subseg_change (SEG_TEXT
, 0);
1024 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1026 cvt_frag_to_fill (&headers
, fragP
);
1028 /* Some assert macros don't work with # directives mixed in. */
1030 if (!(fragP
->fr_next
== NULL
1032 || fragP
->fr_next
== data_frag_root
1034 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
1035 == (fragP
->fr_fix
+ fragP
->fr_offset
* fragP
->fr_var
))))
1039 #endif /* ! BFD_ASSEMBLER */
1041 #ifndef WORKING_DOT_WORD
1043 struct broken_word
*lie
;
1044 struct broken_word
**prevP
;
1046 prevP
= &broken_words
;
1047 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
1052 exp
.X_op
= O_subtract
;
1053 exp
.X_add_symbol
= lie
->add
;
1054 exp
.X_op_symbol
= lie
->sub
;
1055 exp
.X_add_number
= lie
->addnum
;
1056 #ifdef BFD_ASSEMBLER
1057 fix_new_exp (lie
->frag
,
1058 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1059 2, &exp
, 0, BFD_RELOC_NONE
);
1061 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1062 fix_new_exp (lie
->frag
,
1063 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1064 2, &exp
, 0, NO_RELOC
);
1067 fix_new_ns32k_exp (lie
->frag
,
1068 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1069 2, &exp
, 0, 0, 2, 0, 0);
1071 fix_new_exp (lie
->frag
,
1072 lie
->word_goes_here
- lie
->frag
->fr_literal
,
1074 #endif /* TC_NS32K */
1075 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1076 #endif /* BFD_ASSEMBLER */
1077 *prevP
= lie
->next_broken_word
;
1080 prevP
= &(lie
->next_broken_word
);
1082 for (lie
= broken_words
; lie
;)
1084 struct broken_word
*untruth
;
1086 addressT table_addr
;
1087 addressT from_addr
, to_addr
;
1090 fragP
= lie
->dispfrag
;
1092 /* Find out how many broken_words go here. */
1094 for (untruth
= lie
; untruth
&& untruth
->dispfrag
== fragP
; untruth
= untruth
->next_broken_word
)
1095 if (untruth
->added
== 1)
1098 table_ptr
= lie
->dispfrag
->fr_opcode
;
1099 table_addr
= lie
->dispfrag
->fr_address
+ (table_ptr
- lie
->dispfrag
->fr_literal
);
1100 /* Create the jump around the long jumps. This is a short
1101 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
1102 from_addr
= table_addr
;
1103 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
1104 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
, lie
->add
);
1105 table_ptr
+= md_short_jump_size
;
1106 table_addr
+= md_short_jump_size
;
1108 for (m
= 0; lie
&& lie
->dispfrag
== fragP
; m
++, lie
= lie
->next_broken_word
)
1110 if (lie
->added
== 2)
1112 /* Patch the jump table */
1113 /* This is the offset from ??? to table_ptr+0 */
1114 to_addr
= table_addr
- S_GET_VALUE (lie
->sub
);
1115 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
1116 for (untruth
= lie
->next_broken_word
; untruth
&& untruth
->dispfrag
== fragP
; untruth
= untruth
->next_broken_word
)
1118 if (untruth
->use_jump
== lie
)
1119 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
1122 /* Install the long jump */
1123 /* this is a long jump from table_ptr+0 to the final target */
1124 from_addr
= table_addr
;
1125 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
1126 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
, lie
->add
);
1127 table_ptr
+= md_long_jump_size
;
1128 table_addr
+= md_long_jump_size
;
1132 #endif /* not WORKING_DOT_WORD */
1134 #ifndef BFD_ASSEMBLER
1138 * Scan every FixS performing fixups. We had to wait until now to do
1139 * this because md_convert_frag() may have made some fixSs.
1143 subseg_change (SEG_TEXT
, 0);
1144 trsize
= md_reloc_size
* fixup_segment (text_fix_root
, SEG_TEXT
);
1145 subseg_change (SEG_DATA
, 0);
1146 drsize
= md_reloc_size
* fixup_segment (data_fix_root
, SEG_DATA
);
1147 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
1149 /* FIXME move this stuff into the pre-write-hook */
1150 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
1151 H_SET_ENTRY_POINT (&headers
, 0);
1153 obj_pre_write_hook (&headers
); /* extra coff stuff */
1155 object_file_size
= H_GET_FILE_SIZE (&headers
);
1156 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
1158 output_file_create (out_file_name
);
1160 obj_header_append (&next_object_file_charP
, &headers
);
1162 know ((next_object_file_charP
- the_object_file
) == H_GET_HEADER_SIZE (&headers
));
1167 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1169 register long count
;
1170 register char *fill_literal
;
1171 register long fill_size
;
1173 know (fragP
->fr_type
== rs_fill
);
1174 append (&next_object_file_charP
, fragP
->fr_literal
, (unsigned long) fragP
->fr_fix
);
1175 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
1176 fill_size
= fragP
->fr_var
;
1177 know (fragP
->fr_offset
>= 0);
1179 for (count
= fragP
->fr_offset
; count
; count
--)
1181 append (&next_object_file_charP
, fill_literal
, (unsigned long) fill_size
);
1184 } /* for each code frag. */
1186 know ((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE (&headers
) + H_GET_TEXT_SIZE (&headers
) + H_GET_DATA_SIZE (&headers
)));
1191 obj_emit_relocations (&next_object_file_charP
, text_fix_root
, (relax_addressT
) 0);
1192 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
)));
1194 /* Make addresses in data relocation directives relative to beginning of
1195 * first data fragment, not end of last text fragment: alignment of the
1196 * start of the data segment may place a gap between the segments.
1198 obj_emit_relocations (&next_object_file_charP
, data_fix_root
, data0_frchainP
->frch_root
->fr_address
);
1200 obj_emit_relocations (&next_object_file_charP
, data_fix_root
, text_last_frag
->fr_address
);
1201 #endif /* TC_I960 */
1203 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
)));
1206 * Emit line number entries.
1208 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
1209 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
)));
1214 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
1215 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
)));
1221 if (string_byte_count
> 0)
1223 obj_emit_strings (&next_object_file_charP
);
1224 } /* only if we have a string table */
1227 bfd_seek (stdoutput
, 0, 0);
1228 bfd_write (the_object_file
, 1, object_file_size
, stdoutput
);
1231 /* Write the data to the file */
1232 output_file_append (the_object_file
, object_file_size
, out_file_name
);
1235 output_file_close (out_file_name
);
1236 } /* non vms output */
1239 * Now do the VMS-dependent part of writing the object file
1241 VMS_write_object_file (H_GET_TEXT_SIZE (&headers
),
1242 H_GET_DATA_SIZE (&headers
),
1243 H_GET_BSS_SIZE (&headers
),
1244 text_frag_root
, data_frag_root
);
1246 #else /* BFD_ASSEMBLER */
1248 #ifdef obj_check_file_symbols
1249 obj_check_file_symbols ();
1252 bfd_map_over_sections (stdoutput
, adjust_reloc_syms
, (char *)0);
1254 /* Set up symbol table, and write it out. */
1260 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
1264 if (! symp
->sy_resolved
)
1266 if (symp
->sy_value
.X_op
== O_constant
)
1268 /* This is the normal case; skip the call. */
1271 + symp
->sy_frag
->fr_address
));
1272 symp
->sy_resolved
= 1;
1275 resolve_symbol_value (symp
);
1278 /* So far, common symbols have been treated like undefined symbols.
1279 Put them in the common section now. */
1280 if (S_IS_DEFINED (symp
) == 0
1281 && S_GET_VALUE (symp
) != 0)
1282 S_SET_SEGMENT (symp
, &bfd_com_section
);
1284 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1285 S_GET_NAME (symp
), symp
,
1288 segment_name (symp
->bsym
->section
));
1292 #ifdef obj_frob_symbol
1293 obj_frob_symbol (symp
, punt
);
1297 #ifdef tc_frob_symbol
1298 tc_frob_symbol (symp
, punt
);
1303 /* If we don't want to keep this symbol, splice it out of the
1305 if (S_IS_LOCAL (symp
))
1307 symbolS
*prev
, *next
;
1309 prev
= symbol_previous (symp
);
1310 next
= symbol_next (symp
);
1312 verify_symbol_chain_2 (symp
);
1316 symbol_next (prev
) = next
;
1319 else if (symp
== symbol_rootP
)
1320 symbol_rootP
= next
;
1324 symbol_previous (next
) = prev
;
1326 symbol_lastP
= prev
;
1329 verify_symbol_chain_2 (prev
);
1331 verify_symbol_chain_2 (next
);
1336 /* Set the value into the BFD symbol. Up til now the value
1337 has only been kept in the gas symbolS struct. */
1338 symp
->bsym
->value
= S_GET_VALUE (symp
);
1347 extern PTR bfd_alloc
PARAMS ((bfd
*, size_t));
1349 asympp
= (asymbol
**) bfd_alloc (stdoutput
,
1350 n
* sizeof (asymbol
*));
1351 symp
= symbol_rootP
;
1352 for (i
= 0; i
< n
; i
++, symp
= symbol_next (symp
))
1354 asympp
[i
] = symp
->bsym
;
1357 result
= bfd_set_symtab (stdoutput
, asympp
, n
);
1358 assert (result
== true);
1363 #ifdef obj_frob_file
1364 /* If obj_frob_file changes the symbol value at this point, it is
1365 responsible for moving the changed value into symp->bsym->value
1366 as well. Hopefully all symbol value changing can be done in
1367 {obj,tc}_frob_symbol. */
1371 /* Now that all the sizes are known, and contents correct, we can
1372 start writing the file. */
1373 bfd_map_over_sections (stdoutput
, write_relocs
, (char *) 0);
1375 bfd_map_over_sections (stdoutput
, write_contents
, (char *) 0);
1377 output_file_close (out_file_name
);
1378 #endif /* BFD_ASSEMBLER */
1385 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1390 * After this, all frags in this segment have addresses that are correct
1391 * within the segment. Since segments live in different file addresses,
1392 * these frag addresses may not be the same as final object-file addresses.
1395 /* Subroutines of relax_segment. */
1401 for (; f1
; f1
= f1
->fr_next
)
1402 if (f1
->fr_next
== f2
)
1407 /* Relax_align. Advance location counter to next address that has 'alignment'
1408 lowest order bits all 0s, return size of adjustment made. */
1409 static relax_addressT
1410 relax_align (address
, alignment
)
1411 register relax_addressT address
; /* Address now. */
1412 register int alignment
; /* Alignment (binary). */
1414 relax_addressT mask
;
1415 relax_addressT new_address
;
1417 mask
= ~((~0) << alignment
);
1418 new_address
= (address
+ mask
) & (~mask
);
1420 /* We must provide lots of padding, so the linker can discard it
1421 when needed. The linker will not add extra space, ever. */
1422 new_address
+= (1 << alignment
);
1423 return (new_address
- address
);
1427 relax_segment (segment_frag_root
, segment
)
1428 struct frag
*segment_frag_root
;
1431 register struct frag
*fragP
;
1432 register relax_addressT address
;
1433 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1434 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
1436 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1437 subseg_change (segment
, 0);
1439 /* For each frag in segment: count and store (a 1st guess of)
1442 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1444 fragP
->fr_address
= address
;
1445 address
+= fragP
->fr_fix
;
1447 switch (fragP
->fr_type
)
1450 address
+= fragP
->fr_offset
* fragP
->fr_var
;
1454 address
+= relax_align (address
, (int) fragP
->fr_offset
);
1458 /* Assume .org is nugatory. It will grow with 1st relax. */
1461 case rs_machine_dependent
:
1462 address
+= md_estimate_size_before_relax (fragP
, segment
);
1465 #ifndef WORKING_DOT_WORD
1466 /* Broken words don't concern us yet */
1467 case rs_broken_word
:
1472 BAD_CASE (fragP
->fr_type
);
1474 } /* switch(fr_type) */
1475 } /* for each frag in the segment */
1479 long stretch
; /* May be any size, 0 or negative. */
1480 /* Cumulative number of addresses we have */
1481 /* relaxed this pass. */
1482 /* We may have relaxed more than one address. */
1483 long stretched
; /* Have we stretched on this pass? */
1484 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1485 grew, and another shrank. If a branch instruction doesn't fit anymore,
1486 we could be scrod. */
1490 stretch
= stretched
= 0;
1491 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
1494 unsigned long was_address
;
1501 was_address
= fragP
->fr_address
;
1502 address
= fragP
->fr_address
+= stretch
;
1503 symbolP
= fragP
->fr_symbol
;
1504 offset
= fragP
->fr_offset
;
1506 switch (fragP
->fr_type
)
1508 case rs_fill
: /* .fill never relaxes. */
1512 #ifndef WORKING_DOT_WORD
1513 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1514 for it I do not want to write it. I do not want to have
1515 anything to do with it. This is not the proper way to
1516 implement this misfeature. */
1517 case rs_broken_word
:
1519 struct broken_word
*lie
;
1520 struct broken_word
*untruth
;
1522 /* Yes this is ugly (storing the broken_word pointer
1523 in the symbol slot). Still, this whole chunk of
1524 code is ugly, and I don't feel like doing anything
1525 about it. Think of it as stubbornness in action. */
1527 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
1528 lie
&& lie
->dispfrag
== fragP
;
1529 lie
= lie
->next_broken_word
)
1535 offset
= (lie
->add
->sy_frag
->fr_address
1536 + S_GET_VALUE (lie
->add
)
1538 - (lie
->sub
->sy_frag
->fr_address
1539 + S_GET_VALUE (lie
->sub
)));
1540 if (offset
<= -32768 || offset
>= 32767)
1545 sprint_value (buf
, lie
->addnum
);
1546 as_warn (".word %s-%s+%s didn't fit",
1547 S_GET_NAME (lie
->add
),
1548 S_GET_NAME (lie
->sub
),
1552 if (fragP
->fr_subtype
== 0)
1554 fragP
->fr_subtype
++;
1555 growth
+= md_short_jump_size
;
1557 for (untruth
= lie
->next_broken_word
;
1558 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
1559 untruth
= untruth
->next_broken_word
)
1560 if ((untruth
->add
->sy_frag
== lie
->add
->sy_frag
)
1561 && S_GET_VALUE (untruth
->add
) == S_GET_VALUE (lie
->add
))
1564 untruth
->use_jump
= lie
;
1566 growth
+= md_long_jump_size
;
1571 } /* case rs_broken_word */
1574 growth
= (relax_align ((relax_addressT
) (address
1577 - relax_align ((relax_addressT
) (was_address
1587 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1588 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
1589 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
1590 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
)
1591 || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
1592 know (symbolP
->sy_frag
);
1593 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
1594 || (symbolP
->sy_frag
== &zero_address_frag
));
1596 target
+= S_GET_VALUE (symbolP
)
1597 + symbolP
->sy_frag
->fr_address
;
1598 } /* if we have a symbol */
1600 know (fragP
->fr_next
);
1601 after
= fragP
->fr_next
->fr_address
;
1602 growth
= ((target
- after
) > 0) ? (target
- after
) : 0;
1603 /* Growth may be negative, but variable part of frag
1604 cannot have fewer than 0 chars. That is, we can't
1607 growth
-= stretch
; /* This is an absolute growth factor */
1610 case rs_machine_dependent
:
1612 const relax_typeS
*this_type
;
1613 const relax_typeS
*start_type
;
1614 relax_substateT next_state
;
1615 relax_substateT this_state
;
1617 this_state
= fragP
->fr_subtype
;
1618 start_type
= this_type
= md_relax_table
+ this_state
;
1623 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1624 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
)
1625 || (S_GET_SEGMENT (symbolP
) == SEG_DATA
)
1626 || (S_GET_SEGMENT (symbolP
) == SEG_BSS
)
1627 || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
1629 know (symbolP
->sy_frag
);
1630 know (!(S_GET_SEGMENT (symbolP
) == absolute_section
)
1631 || symbolP
->sy_frag
== &zero_address_frag
);
1633 S_GET_VALUE (symbolP
)
1634 + symbolP
->sy_frag
->fr_address
;
1636 /* If frag has yet to be reached on this pass,
1637 assume it will move by STRETCH just as we did.
1638 If this is not so, it will be because some frag
1639 between grows, and that will force another pass.
1641 Beware zero-length frags.
1643 There should be a faster way to do this. */
1645 if (symbolP
->sy_frag
->fr_address
>= was_address
1646 && is_dnrange (fragP
, symbolP
->sy_frag
))
1652 aim
= target
- address
- fragP
->fr_fix
;
1653 /* The displacement is affected by the instruction size
1654 for the 32k architecture. I think we ought to be able
1655 to add fragP->fr_pcrel_adjust in all cases (it should be
1656 zero if not used), but just in case it breaks something
1657 else we'll put this inside #ifdef NS32K ... #endif */
1659 if (fragP
->fr_pcrel_adjust
)
1662 aim
+= fragP
->fr_pcrel_adjust
;
1666 /* Look backwards. */
1667 for (next_state
= this_type
->rlx_more
; next_state
;)
1668 if (aim
>= this_type
->rlx_backward
)
1672 /* Grow to next state. */
1673 this_state
= next_state
;
1674 this_type
= md_relax_table
+ this_state
;
1675 next_state
= this_type
->rlx_more
;
1680 #ifdef M68K_AIM_KLUDGE
1681 M68K_AIM_KLUDGE (aim
, this_state
, this_type
);
1683 /* Look forwards. */
1684 for (next_state
= this_type
->rlx_more
; next_state
;)
1685 if (aim
<= this_type
->rlx_forward
)
1689 /* Grow to next state. */
1690 this_state
= next_state
;
1691 this_type
= md_relax_table
+ this_state
;
1692 next_state
= this_type
->rlx_more
;
1696 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
1698 fragP
->fr_subtype
= this_state
;
1703 BAD_CASE (fragP
->fr_type
);
1711 } /* For each frag in the segment. */
1713 while (stretched
); /* Until nothing further to relax. */
1717 * We now have valid fr_address'es for each frag.
1721 * All fr_address's are correct, relative to their own segment.
1722 * We have made all the fixS we will ever make.
1724 } /* relax_segment() */
1728 Go through all the fixS's in a segment and see which ones can be
1729 handled now. (These consist of fixS where we have since discovered
1730 the value of a symbol, or the address of the frag involved.)
1731 For each one, call md_apply_fix to put the fix into the frag data.
1733 Result is a count of how many relocation structs will be needed to
1734 handle the remaining fixS's that we couldn't completely handle here.
1735 These will be output later by emit_relocations(). */
1738 fixup_segment (fixP
, this_segment_type
)
1739 register fixS
*fixP
;
1740 segT this_segment_type
; /* N_TYPE bits for segment. */
1742 register long seg_reloc_count
;
1743 register symbolS
*add_symbolP
;
1744 register symbolS
*sub_symbolP
;
1747 register char *place
;
1748 register long where
;
1749 register char pcrel
;
1750 register fragS
*fragP
;
1751 register segT add_symbol_segment
= absolute_section
;
1753 seg_reloc_count
= 0;
1754 /* If the linker is doing the relaxing, we must not do any fixups */
1756 for (; fixP
; fixP
= fixP
->fx_next
)
1759 for (; fixP
; fixP
= fixP
->fx_next
)
1761 fragP
= fixP
->fx_frag
;
1763 where
= fixP
->fx_where
;
1764 place
= fragP
->fr_literal
+ where
;
1765 size
= fixP
->fx_size
;
1766 add_symbolP
= fixP
->fx_addsy
;
1768 if (fixP
->fx_callj
&& TC_S_IS_CALLNAME (add_symbolP
))
1770 /* Relocation should be done via the associated 'bal'
1771 entry point symbol. */
1773 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP
)))
1775 as_bad ("No 'bal' entry point for leafproc %s",
1776 S_GET_NAME (add_symbolP
));
1779 fixP
->fx_addsy
= add_symbolP
= tc_get_bal_of_call (add_symbolP
);
1782 sub_symbolP
= fixP
->fx_subsy
;
1783 add_number
= fixP
->fx_offset
;
1784 pcrel
= fixP
->fx_pcrel
;
1787 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
1794 if (S_GET_SEGMENT (sub_symbolP
) != absolute_section
)
1795 as_bad ("Negative of non-absolute symbol %s",
1796 S_GET_NAME (sub_symbolP
));
1798 add_number
-= S_GET_VALUE (sub_symbolP
);
1800 else if ((S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
)
1801 && (SEG_NORMAL (add_symbol_segment
)
1802 || (add_symbol_segment
== absolute_section
)))
1804 /* Difference of 2 symbols from same segment.
1805 Can't make difference of 2 undefineds: 'value' means
1806 something different for N_UNDF. */
1808 /* Makes no sense to use the difference of 2 arbitrary symbols
1809 as the target of a call instruction. */
1812 as_bad ("callj to difference of 2 symbols");
1814 #endif /* TC_I960 */
1815 add_number
+= S_GET_VALUE (add_symbolP
) -
1816 S_GET_VALUE (sub_symbolP
);
1819 fixP
->fx_addsy
= NULL
;
1823 /* Different segments in subtraction. */
1824 know (!(S_IS_EXTERNAL (sub_symbolP
)
1825 && (S_GET_SEGMENT (sub_symbolP
) == absolute_section
)));
1827 if ((S_GET_SEGMENT (sub_symbolP
) == absolute_section
))
1829 add_number
-= S_GET_VALUE (sub_symbolP
);
1834 sprint_value (buf
, fragP
->fr_address
+ where
);
1835 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
1836 segment_name (S_GET_SEGMENT (sub_symbolP
)),
1837 S_GET_NAME (sub_symbolP
), buf
);
1844 if (add_symbol_segment
== this_segment_type
&& pcrel
)
1847 * This fixup was made when the symbol's segment was
1848 * SEG_UNKNOWN, but it is now in the local segment.
1849 * So we know how to do the address without relocation.
1852 /* reloc_callj() may replace a 'call' with a 'calls' or a
1853 'bal', in which cases it modifies *fixP as appropriate.
1854 In the case of a 'calls', no further work is required,
1855 and *fixP has been set up to make the rest of the code
1858 #endif /* TC_I960 */
1860 add_number
+= S_GET_VALUE (add_symbolP
);
1861 add_number
-= md_pcrel_from (fixP
);
1862 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
1863 fixP
->fx_addsy
= NULL
; /* No relocations please. */
1867 if (add_symbol_segment
== absolute_section
)
1870 /* See comment about reloc_callj() above. */
1872 #endif /* TC_I960 */
1873 add_number
+= S_GET_VALUE (add_symbolP
);
1874 fixP
->fx_addsy
= NULL
;
1877 else if (add_symbol_segment
== undefined_section
1878 #ifdef BFD_ASSEMBLER
1879 || add_symbol_segment
== &bfd_com_section
1884 if ((int) fixP
->fx_bit_fixP
== 13)
1886 /* This is a COBR instruction. They have only a
1887 * 13-bit displacement and are only to be used
1888 * for local branches: flag as error, don't generate
1891 as_bad ("can't use COBR format with external label");
1892 fixP
->fx_addsy
= NULL
; /* No relocations please. */
1895 #endif /* TC_I960 */
1899 if (S_IS_COMMON (add_symbolP
))
1900 add_number
+= S_GET_VALUE (add_symbolP
);
1901 #endif /* TE_I386AIX */
1902 #endif /* OBJ_COFF */
1908 add_number
+= S_GET_VALUE (add_symbolP
);
1910 } /* if not in local seg */
1911 } /* if there was a + symbol */
1915 add_number
-= md_pcrel_from (fixP
);
1916 if (add_symbolP
== 0)
1918 fixP
->fx_addsy
= &abs_symbol
;
1920 } /* if there's an add_symbol */
1923 if (!fixP
->fx_bit_fixP
)
1926 && (add_number
& ~0xFF)
1927 && ((add_number
& ~0xFF) != (-1 & ~0xFF)))
1929 && (add_number
& ~0xFFFF)
1930 && ((add_number
& ~0xFFFF) != (-1 & ~0xFFFF)))
1932 && (add_number
& ~(valueT
)0xFFFFFFFF)
1933 && ((add_number
& ~(valueT
)0xFFFFFFFF) != (-1 & ~(valueT
)0xFFFFFFFF)))
1937 sprint_value (buf
, fragP
->fr_address
+ where
);
1938 as_bad ("Value of %d too large for field of %d bytes at %s",
1939 add_number
, size
, buf
);
1940 } /* generic error checking */
1941 #ifdef WARN_SIGNED_OVERFLOW_WORD
1942 /* Warn if a .word value is too large when treated as a signed
1943 number. We already know it is not too negative. This is to
1944 catch over-large switches generated by gcc on the 68k. */
1947 && add_number
> 0x7fff)
1948 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
1949 add_number
, fragP
->fr_address
+ where
);
1951 } /* not a bit fix */
1953 #ifdef BFD_ASSEMBLER
1954 md_apply_fix (fixP
, &add_number
);
1956 md_apply_fix (fixP
, add_number
);
1958 } /* For each fixS in this segment. */
1965 /* two relocs per callj under coff. */
1966 for (fixP
= topP
; fixP
; fixP
= fixP
->fx_next
)
1968 if (fixP
->fx_callj
&& fixP
->fx_addsy
!= 0)
1971 } /* if callj and not already fixed. */
1972 } /* for each fix */
1974 #endif /* TC_I960 */
1976 #endif /* OBJ_COFF */
1977 return (seg_reloc_count
);
1980 /* end of write.c */