1 /* write.c - emit .o file
3 Copyright (C) 1986, 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This thing should be set up to do byteordering correctly. But... */
26 #include "output-file.h"
28 /* The NOP_OPCODE is for the alignment fill value.
29 * fill it a nop instruction so that the disassembler does not choke
33 #define NOP_OPCODE 0x00
37 struct frag
*text_frag_root
;
38 struct frag
*data_frag_root
;
39 struct frag
*bss_frag_root
;
41 struct frag
*text_last_frag
; /* Last frag in segment. */
42 struct frag
*data_last_frag
; /* Last frag in segment. */
43 static struct frag
*bss_last_frag
; /* Last frag in segment. */
46 static object_headers headers
;
48 long string_byte_count
;
50 static char *the_object_file
;
52 char *next_object_file_charP
; /* Tracks object file bytes. */
54 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
56 static int is_dnrange
PARAMS ((struct frag
* f1
, struct frag
* f2
));
57 static long fixup_segment
PARAMS ((fixS
* fixP
, segT this_segment_type
));
58 static relax_addressT relax_align
PARAMS ((relax_addressT addr
, long align
));
59 void relax_segment
PARAMS ((struct frag
* seg_frag_root
, segT seg_type
));
64 * Create a fixS in obstack 'notes'.
67 fix_new (frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
, r_type
)
68 fragS
*frag
; /* Which frag? */
69 int where
; /* Where in that frag? */
70 short int size
; /* 1, 2, or 4 usually. */
71 symbolS
*add_symbol
; /* X_add_symbol. */
72 symbolS
*sub_symbol
; /* X_subtract_symbol. */
73 long offset
; /* X_add_number. */
74 int pcrel
; /* TRUE if PC-relative relocation. */
75 int r_type
; /* Relocation type */
79 fixP
= (fixS
*) obstack_alloc (¬es
, sizeof (fixS
));
82 fixP
->fx_where
= where
;
84 fixP
->fx_addsy
= add_symbol
;
85 fixP
->fx_subsy
= sub_symbol
;
86 fixP
->fx_offset
= offset
;
87 fixP
->fx_pcrel
= pcrel
;
88 #if defined(TC_SPARC) || defined(TC_A29K) || defined( NEED_FX_R_TYPE)
89 fixP
->fx_r_type
= r_type
;
91 /* JF these 'cuz of the NS32K stuff */
93 fixP
->fx_pcrel_adjust
= 0;
95 fixP
->fx_bit_fixP
= 0;
97 /* usually, we want relocs sorted numerically, but while
98 comparing to older versions of gas that have relocs
99 reverse sorted, it is convenient to have this compile
100 time option. xoxorich. */
102 #ifdef REVERSE_SORT_RELOCS
104 fixP
->fx_next
= *seg_fix_rootP
;
105 *seg_fix_rootP
= fixP
;
107 #else /* REVERSE_SORT_RELOCS */
109 fixP
->fx_next
= NULL
;
112 (*seg_fix_tailP
)->fx_next
= fixP
;
114 *seg_fix_rootP
= fixP
;
115 *seg_fix_tailP
= fixP
;
117 #endif /* REVERSE_SORT_RELOCS */
126 remove_subsegs (head
, seg
, root
, last
)
133 fragS
*prev_frag
= &dummy
;
134 *root
= head
->frch_root
;
135 while (head
&& head
->frch_seg
== seg
)
137 prev_frag
->fr_next
= head
->frch_root
;
139 prev_frag
= head
->frch_last
;
140 head
= head
->frch_next
;
143 prev_frag
->fr_next
= 0;
149 register struct frchain
*frchainP
; /* Track along all frchains. */
150 register fragS
*fragP
; /* Track along all frags. */
151 register struct frchain
*next_frchainP
;
152 register fragS
**prev_fragPP
;
153 unsigned int data_siz
;
155 long object_file_size
;
159 * Under VMS we try to be compatible with VAX-11 "C". Thus, we
160 * call a routine to check for the definition of the procedure
161 * "_main", and if so -- fix it up so that it can be program
164 VMS_Check_For_Main ();
167 * After every sub-segment, we fake an ".align ...". This conforms to
168 * BSD4.2 brane-damage. We then fake ".fill 0" because that is the
169 * kind of frag that requires least thought. ".align" frags like to
170 * have a following frag since that makes calculating their intended
173 #define SUB_SEGMENT_ALIGN (2)
174 for (frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
)
178 * Under VAX/VMS, the linker (and PSECT specifications)
179 * take care of correctly aligning the segments.
180 * Doing the alignment here (on initialized data) can
181 * mess up the calculation of global data PSECT sizes.
183 #undef SUB_SEGMENT_ALIGN
184 #define SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0)
186 subseg_new (frchainP
->frch_seg
, frchainP
->frch_subseg
);
187 frag_align (SUB_SEGMENT_ALIGN
, NOP_OPCODE
);
188 /* frag_align will have left a new frag. */
189 /* Use this last frag for an empty ".fill". */
191 * For this segment ...
192 * Create a last frag. Do not leave a "being filled in frag".
194 frag_wane (frag_now
);
195 frag_now
->fr_fix
= 0;
196 know (frag_now
->fr_next
== NULL
);
197 /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
198 /* Above shows we haven't left a half-completed object on obstack. */
199 } /* walk the frag chain */
202 * From now on, we don't care about sub-segments.
203 * Build one frag chain for each segment. Linked thru fr_next.
204 * We know that there is at least 1 text frchain & at least 1 data
208 remove_subsegs (frchain_root
, SEG_TEXT
, &text_frag_root
, &text_last_frag
);
209 remove_subsegs (data0_frchainP
, SEG_DATA
, &data_frag_root
, &data_last_frag
);
210 remove_subsegs (bss0_frchainP
, SEG_BSS
, &bss_frag_root
, &bss_last_frag
);
213 * We have two segments. If user gave -R flag, then we must put the
214 * data frags into the text segment. Do this before relaxing so
215 * we know to take advantage of -R and make shorter addresses.
222 text_last_frag
->fr_next
= data_frag_root
;
223 text_last_frag
= data_last_frag
;
224 data_last_frag
= NULL
;
225 data_frag_root
= NULL
;
228 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
);;
229 tmp
->fx_next
= data_fix_root
;
230 text_fix_tail
= data_fix_tail
;
233 text_fix_root
= data_fix_root
;
234 data_fix_root
= NULL
;
237 relax_segment (text_frag_root
, SEG_TEXT
);
238 relax_segment (data_frag_root
, SEG_DATA
);
239 relax_segment (bss_frag_root
, SEG_BSS
);
241 * Now the addresses of frags are correct within the segment.
244 know (text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
245 H_SET_TEXT_SIZE (&headers
, text_last_frag
->fr_address
);
246 text_last_frag
->fr_address
= H_GET_TEXT_SIZE (&headers
);
249 * Join the 2 segments into 1 huge segment.
250 * To do this, re-compute every rn_address in the SEG_DATA frags.
251 * Then join the data frags after the text frags.
253 * Determine a_data [length of data segment].
257 register relax_addressT slide
;
259 know ((text_last_frag
->fr_type
== rs_fill
) && (text_last_frag
->fr_offset
== 0));
261 H_SET_DATA_SIZE (&headers
, data_last_frag
->fr_address
);
262 data_last_frag
->fr_address
= H_GET_DATA_SIZE (&headers
);
263 slide
= H_GET_TEXT_SIZE (&headers
); /* & in file of the data segment. */
265 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
266 /* For b.out: If the data section has a strict alignment
267 requirement, its load address in the .o file will be
268 rounded up from the size of the text section. These
269 two values are *not* the same! Similarly for the bss
271 slide
= RoundUp (slide
, 1 << section_alignment
[SEG_DATA
]);
274 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
)
276 fragP
->fr_address
+= slide
;
277 } /* for each data frag */
279 know (text_last_frag
!= 0);
280 text_last_frag
->fr_next
= data_frag_root
;
284 H_SET_DATA_SIZE (&headers
, 0);
289 /* See above comments on b.out data section address. */
292 if (data_last_frag
== 0)
293 bss_vma
= H_GET_TEXT_SIZE (&headers
);
295 bss_vma
= data_last_frag
->fr_address
;
296 bss_vma
= RoundUp (bss_vma
, 1 << section_alignment
[SEG_BSS
]);
297 bss_address_frag
.fr_address
= bss_vma
;
300 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE (&headers
) +
301 H_GET_DATA_SIZE (&headers
));
304 /* Slide all the frags */
307 relax_addressT slide
= bss_address_frag
.fr_address
+ local_bss_counter
;
309 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
311 fragP
->fr_address
+= slide
;
312 } /* for each bss frag */
318 local_bss_counter
+= bss_last_frag
->fr_address
- bss_frag_root
->fr_address
;
320 H_SET_BSS_SIZE (&headers
, local_bss_counter
);
324 * Crawl the symbol chain.
326 * For each symbol whose value depends on a frag, take the address of
327 * that frag and subsume it into the value of the symbol.
328 * After this, there is just one way to lookup a symbol value.
329 * Values are left in their final state for object file emission.
330 * We adjust the values of 'L' local symbols, even if we do
331 * not intend to emit them to the object file, because their values
332 * are needed for fix-ups.
334 * Unless we saw a -L flag, remove all symbols that begin with 'L'
335 * from the symbol chain. (They are still pointed to by the fixes.)
337 * Count the remaining symbols.
338 * Assign a symbol number to each symbol.
339 * Count the number of string-table chars we will emit.
340 * Put this info into the headers as appropriate.
343 know (zero_address_frag
.fr_address
== 0);
344 string_byte_count
= sizeof (string_byte_count
);
346 obj_crawl_symbol_chain (&headers
);
348 if (string_byte_count
== sizeof (string_byte_count
))
350 string_byte_count
= 0;
351 } /* if no strings, then no count. */
353 H_SET_STRING_SIZE (&headers
, string_byte_count
);
356 * Addresses of frags now reflect addresses we use in the object file.
357 * Symbol values are correct.
358 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
359 * Also converting any machine-dependent frags using md_convert_frag();
361 subseg_change (SEG_TEXT
, 0);
363 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
365 switch (fragP
->fr_type
)
370 HANDLE_ALIGN (fragP
);
372 fragP
->fr_type
= rs_fill
;
373 know (fragP
->fr_var
== 1);
374 know (fragP
->fr_next
!= NULL
);
376 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
384 case rs_machine_dependent
:
385 md_convert_frag (&headers
, fragP
);
387 know ((fragP
->fr_next
== NULL
) || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
) == fragP
->fr_fix
));
390 * After md_convert_frag, we make the frag into a ".space 0".
391 * Md_convert_frag() should set up any fixSs and constants
397 #ifndef WORKING_DOT_WORD
400 struct broken_word
*lie
;
401 extern md_short_jump_size
;
402 extern md_long_jump_size
;
404 if (fragP
->fr_subtype
)
406 fragP
->fr_fix
+= md_short_jump_size
;
407 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
); lie
&& lie
->dispfrag
== fragP
; lie
= lie
->next_broken_word
)
409 fragP
->fr_fix
+= md_long_jump_size
;
417 BAD_CASE (fragP
->fr_type
);
419 } /* switch (fr_type) */
421 if (!((fragP
->fr_next
== NULL
)
423 || (fragP
->fr_next
== data_frag_root
)
425 || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
)
426 == (fragP
->fr_fix
+ (fragP
->fr_offset
* fragP
->fr_var
)))))
428 fprintf (stderr
, "assertion failed: file `%s', line %d\n",
429 __FILE__
, __LINE__
- 4);
432 } /* for each frag. */
434 #ifndef WORKING_DOT_WORD
436 struct broken_word
*lie
;
437 struct broken_word
**prevP
;
439 prevP
= &broken_words
;
440 for (lie
= broken_words
; lie
; lie
= lie
->next_broken_word
)
444 fix_new_ns32k (lie
->frag
,
445 lie
->word_goes_here
- lie
->frag
->fr_literal
,
452 # if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
453 fix_new (lie
->frag
, lie
->word_goes_here
- lie
->frag
->fr_literal
,
455 lie
->sub
, lie
->addnum
,
458 fix_new (lie
->frag
, lie
->word_goes_here
- lie
->frag
->fr_literal
,
460 lie
->sub
, lie
->addnum
,
463 # endif /* tc_sparc|tc_a29k|need_fx_r_type */
464 #endif /* TC_NS32K */
465 /* md_number_to_chars(lie->word_goes_here,
466 S_GET_VALUE(lie->add)
468 - S_GET_VALUE(lie->sub),
470 *prevP
= lie
->next_broken_word
;
473 prevP
= &(lie
->next_broken_word
);
475 for (lie
= broken_words
; lie
;)
477 struct broken_word
*untruth
;
480 long from_addr
, to_addr
;
483 extern md_short_jump_size
;
484 extern md_long_jump_size
;
486 fragP
= lie
->dispfrag
;
488 /* Find out how many broken_words go here */
490 for (untruth
= lie
; untruth
&& untruth
->dispfrag
== fragP
; untruth
= untruth
->next_broken_word
)
491 if (untruth
->added
== 1)
494 table_ptr
= lie
->dispfrag
->fr_opcode
;
495 table_addr
= lie
->dispfrag
->fr_address
+ (table_ptr
- lie
->dispfrag
->fr_literal
);
496 /* Create the jump around the long jumps */
497 /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */
498 from_addr
= table_addr
;
499 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
500 md_create_short_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
, lie
->add
);
501 table_ptr
+= md_short_jump_size
;
502 table_addr
+= md_short_jump_size
;
504 for (m
= 0; lie
&& lie
->dispfrag
== fragP
; m
++, lie
= lie
->next_broken_word
)
508 /* Patch the jump table */
509 /* This is the offset from ??? to table_ptr+0 */
511 - S_GET_VALUE (lie
->sub
);
512 md_number_to_chars (lie
->word_goes_here
, to_addr
, 2);
513 for (untruth
= lie
->next_broken_word
; untruth
&& untruth
->dispfrag
== fragP
; untruth
= untruth
->next_broken_word
)
515 if (untruth
->use_jump
== lie
)
516 md_number_to_chars (untruth
->word_goes_here
, to_addr
, 2);
519 /* Install the long jump */
520 /* this is a long jump from table_ptr+0 to the final target */
521 from_addr
= table_addr
;
522 to_addr
= S_GET_VALUE (lie
->add
) + lie
->addnum
;
523 md_create_long_jump (table_ptr
, from_addr
, to_addr
, lie
->dispfrag
, lie
->add
);
524 table_ptr
+= md_long_jump_size
;
525 table_addr
+= md_long_jump_size
;
529 #endif /* not WORKING_DOT_WORD */
534 * Scan every FixS performing fixups. We had to wait until now to do
535 * this because md_convert_frag() may have made some fixSs.
539 subseg_change (SEG_TEXT
, 0);
540 trsize
= md_reloc_size
* fixup_segment (text_fix_root
,
542 subseg_change (SEG_DATA
, 0);
543 drsize
= md_reloc_size
* fixup_segment (data_fix_root
,
545 H_SET_RELOCATION_SIZE (&headers
, trsize
, drsize
);
547 /* FIXME move this stuff into the pre-write-hook */
548 H_SET_MAGIC_NUMBER (&headers
, magic_number_for_object_file
);
549 H_SET_ENTRY_POINT (&headers
, 0);
551 obj_pre_write_hook (&headers
); /* extra coff stuff */
552 if ((had_warnings () && flagseen
['Z'])
553 || had_errors () > 0)
557 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
558 had_errors (), had_errors () == 1 ? "" : "s",
559 had_warnings (), had_warnings () == 1 ? "" : "s");
563 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
564 had_errors (), had_errors () == 1 ? "" : "s",
565 had_warnings (), had_warnings () == 1 ? "" : "s");
566 } /* on want output */
567 } /* on error condition */
569 object_file_size
= H_GET_FILE_SIZE (&headers
);
570 next_object_file_charP
= the_object_file
= xmalloc (object_file_size
);
572 output_file_create (out_file_name
);
574 obj_header_append (&next_object_file_charP
, &headers
);
576 know ((next_object_file_charP
- the_object_file
) == H_GET_HEADER_SIZE (&headers
));
581 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
)
584 register char *fill_literal
;
585 register long fill_size
;
587 know (fragP
->fr_type
== rs_fill
);
588 append (&next_object_file_charP
, fragP
->fr_literal
, (unsigned long) fragP
->fr_fix
);
589 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
590 fill_size
= fragP
->fr_var
;
591 know (fragP
->fr_offset
>= 0);
593 for (count
= fragP
->fr_offset
; count
; count
--)
595 append (&next_object_file_charP
, fill_literal
, (unsigned long) fill_size
);
598 } /* for each code frag. */
600 know ((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE (&headers
) + H_GET_TEXT_SIZE (&headers
) + H_GET_DATA_SIZE (&headers
)));
605 obj_emit_relocations (&next_object_file_charP
, text_fix_root
, (relax_addressT
) 0);
606 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
)));
608 /* Make addresses in data relocation directives relative to beginning of
609 * first data fragment, not end of last text fragment: alignment of the
610 * start of the data segment may place a gap between the segments.
612 obj_emit_relocations (&next_object_file_charP
, data_fix_root
, data0_frchainP
->frch_root
->fr_address
);
614 obj_emit_relocations (&next_object_file_charP
, data_fix_root
, text_last_frag
->fr_address
);
617 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
)));
620 * Emit line number entries.
622 OBJ_EMIT_LINENO (&next_object_file_charP
, lineno_rootP
, the_object_file
);
623 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
)));
628 obj_emit_symbols (&next_object_file_charP
, symbol_rootP
);
629 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
)));
635 if (string_byte_count
> 0)
637 obj_emit_strings (&next_object_file_charP
);
638 } /* only if we have a string table */
640 /* 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) + H_GET_STRING_SIZE(&headers)));
642 /* know(next_object_file_charP == the_object_file + object_file_size);*/
645 bfd_seek (stdoutput
, 0, 0);
646 bfd_write (the_object_file
, 1, object_file_size
, stdoutput
);
649 /* Write the data to the file */
650 output_file_append (the_object_file
, object_file_size
, out_file_name
);
653 output_file_close (out_file_name
);
654 } /* non vms output */
657 * Now do the VMS-dependent part of writing the object file
659 VMS_write_object_file (text_siz
, data_siz
, text_frag_root
, data_frag_root
);
661 } /* write_object_file() */
669 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
674 * After this, all frags in this segment have addresses that are correct
675 * within the segment. Since segments live in different file addresses,
676 * these frag addresses may not be the same as final object-file addresses.
682 relax_segment (segment_frag_root
, segment
)
683 struct frag
*segment_frag_root
;
684 segT segment
; /* SEG_DATA or SEG_TEXT */
686 register struct frag
*fragP
;
687 register relax_addressT address
;
688 #ifndef MANY_SEGMENTS
689 know (segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
691 /* In case md_estimate_size_before_relax() wants to make fixSs. */
692 subseg_change (segment
, 0);
694 /* For each frag in segment: count and store (a 1st guess of)
697 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
699 fragP
->fr_address
= address
;
700 address
+= fragP
->fr_fix
;
702 switch (fragP
->fr_type
)
705 address
+= fragP
->fr_offset
* fragP
->fr_var
;
709 address
+= relax_align (address
, fragP
->fr_offset
);
713 /* Assume .org is nugatory. It will grow with 1st relax. */
716 case rs_machine_dependent
:
717 address
+= md_estimate_size_before_relax (fragP
, segment
);
720 #ifndef WORKING_DOT_WORD
721 /* Broken words don't concern us yet */
727 BAD_CASE (fragP
->fr_type
);
729 } /* switch(fr_type) */
730 } /* for each frag in the segment */
734 long stretch
; /* May be any size, 0 or negative. */
735 /* Cumulative number of addresses we have */
736 /* relaxed this pass. */
737 /* We may have relaxed more than one address. */
738 long stretched
; /* Have we stretched on this pass? */
739 /* This is 'cuz stretch may be zero, when, in fact some piece of code
740 grew, and another shrank. If a branch instruction doesn't fit anymore,
741 we could be scrod. */
745 stretch
= stretched
= 0;
746 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
)
749 unsigned long was_address
;
756 was_address
= fragP
->fr_address
;
757 address
= fragP
->fr_address
+= stretch
;
758 symbolP
= fragP
->fr_symbol
;
759 offset
= fragP
->fr_offset
;
761 switch (fragP
->fr_type
)
763 case rs_fill
: /* .fill never relaxes. */
767 #ifndef WORKING_DOT_WORD
768 /* JF: This is RMS's idea. I do *NOT* want to be blamed
769 for it I do not want to write it. I do not want to have
770 anything to do with it. This is not the proper way to
771 implement this misfeature. */
774 struct broken_word
*lie
;
775 struct broken_word
*untruth
;
776 extern int md_short_jump_size
;
777 extern int md_long_jump_size
;
779 /* Yes this is ugly (storing the broken_word pointer
780 in the symbol slot). Still, this whole chunk of
781 code is ugly, and I don't feel like doing anything
782 about it. Think of it as stubbornness in action. */
784 for (lie
= (struct broken_word
*) (fragP
->fr_symbol
);
785 lie
&& lie
->dispfrag
== fragP
;
786 lie
= lie
->next_broken_word
)
792 offset
= (lie
->add
->sy_frag
->fr_address
793 + S_GET_VALUE (lie
->add
)
795 - (lie
->sub
->sy_frag
->fr_address
796 + S_GET_VALUE (lie
->sub
)));
797 if (offset
<= -32768 || offset
>= 32767)
800 as_warn (".word %s-%s+%ld didn't fit",
801 S_GET_NAME (lie
->add
),
802 S_GET_NAME (lie
->sub
),
805 if (fragP
->fr_subtype
== 0)
808 growth
+= md_short_jump_size
;
810 for (untruth
= lie
->next_broken_word
;
811 untruth
&& untruth
->dispfrag
== lie
->dispfrag
;
812 untruth
= untruth
->next_broken_word
)
813 if ((untruth
->add
->sy_frag
== lie
->add
->sy_frag
)
814 && S_GET_VALUE (untruth
->add
) == S_GET_VALUE (lie
->add
))
817 untruth
->use_jump
= lie
;
819 growth
+= md_long_jump_size
;
824 } /* case rs_broken_word */
827 growth
= (relax_align ((relax_addressT
) (address
+ fragP
->fr_fix
), offset
)
828 - relax_align ((relax_addressT
) (was_address
+ fragP
->fr_fix
), offset
));
838 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
) || (S_GET_SEGMENT (symbolP
) == SEG_DATA
) || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
) || S_GET_SEGMENT (symbolP
) == SEG_BSS
);
839 know (symbolP
->sy_frag
);
840 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
) || (symbolP
->sy_frag
== &zero_address_frag
));
842 target
+= S_GET_VALUE (symbolP
)
843 + symbolP
->sy_frag
->fr_address
;
844 } /* if we have a symbol */
846 know (fragP
->fr_next
);
847 after
= fragP
->fr_next
->fr_address
;
848 growth
= ((target
- after
) > 0) ? (target
- after
) : 0;
849 /* Growth may be -ve, but variable part of frag cannot have
850 fewer than 0 chars. That is, we can't .org backwards. */
852 growth
-= stretch
; /* This is an absolute growth factor */
855 case rs_machine_dependent
:
857 const relax_typeS
*this_type
;
858 const relax_typeS
*start_type
;
859 relax_substateT next_state
;
860 relax_substateT this_state
;
862 this_state
= fragP
->fr_subtype
;
863 start_type
= this_type
= md_relax_table
+ this_state
;
868 #ifndef MANY_SEGMENTS
869 know ((S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
) || (S_GET_SEGMENT (symbolP
) == SEG_DATA
) || (S_GET_SEGMENT (symbolP
) == SEG_BSS
) || (S_GET_SEGMENT (symbolP
) == SEG_TEXT
));
871 know (symbolP
->sy_frag
);
872 know (!(S_GET_SEGMENT (symbolP
) == SEG_ABSOLUTE
) || symbolP
->sy_frag
== &zero_address_frag
);
874 S_GET_VALUE (symbolP
)
875 + symbolP
->sy_frag
->fr_address
;
877 /* If frag has yet to be reached on this pass,
878 assume it will move by STRETCH just as we did.
879 If this is not so, it will be because some frag
880 between grows, and that will force another pass. */
882 /* JF was just address */
883 /* JF also added is_dnrange hack */
884 /* There's gotta be a better/faster/etc way
886 /* gnu@cygnus.com: I changed this from > to >=
887 because I ran into a zero-length frag (fr_fix=0)
888 which was created when the obstack needed a new
889 chunk JUST AFTER the opcode of a branch. Since
890 fr_fix is zero, fr_address of this frag is the same
891 as fr_address of the next frag. This
892 zero-length frag was variable and jumped to .+2
893 (in the next frag), but since the > comparison
894 below failed (the two were =, not >), "stretch"
895 was not added to the target. Stretch was 178, so
896 the offset appeared to be .-176 instead, which did
897 not fit into a byte branch, so the assembler
898 relaxed the branch to a word. This didn't compare
899 with what happened when the same source file was
900 assembled on other machines, which is how I found it.
901 You might want to think about what other places have
902 trouble with zero length frags... */
904 if (symbolP
->sy_frag
->fr_address
>= was_address
905 && is_dnrange (fragP
, symbolP
->sy_frag
))
909 } /* if there's a symbol attached */
911 aim
= target
- address
- fragP
->fr_fix
;
912 /* The displacement is affected by the instruction size
913 for the 32k architecture. I think we ought to be able
914 to add fragP->fr_pcrel_adjust in all cases (it should be
915 zero if not used), but just in case it breaks something
916 else we'll put this inside #ifdef NS32K ... #endif */
918 aim
+= fragP
->fr_pcrel_adjust
;
919 #endif /* TC_NS32K */
923 /* Look backwards. */
924 for (next_state
= this_type
->rlx_more
; next_state
;)
925 if (aim
>= this_type
->rlx_backward
)
929 /* Grow to next state. */
930 this_state
= next_state
;
931 this_type
= md_relax_table
+ this_state
;
932 next_state
= this_type
->rlx_more
;
937 #ifdef M68K_AIM_KLUDGE
938 M68K_AIM_KLUDGE (aim
, this_state
, this_type
);
941 for (next_state
= this_type
->rlx_more
; next_state
;)
942 if (aim
<= this_type
->rlx_forward
)
946 /* Grow to next state. */
947 this_state
= next_state
;
948 this_type
= md_relax_table
+ this_state
;
949 next_state
= this_type
->rlx_more
;
953 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
955 fragP
->fr_subtype
= this_state
;
960 BAD_CASE (fragP
->fr_type
);
968 } /* For each frag in the segment. */
970 while (stretched
); /* Until nothing further to relax. */
974 * We now have valid fr_address'es for each frag.
978 * All fr_address's are correct, relative to their own segment.
979 * We have made all the fixS we will ever make.
981 } /* relax_segment() */
984 * Relax_align. Advance location counter to next address that has 'alignment'
985 * lowest order bits all 0s.
988 /* How many addresses does the .align take? */
989 static relax_addressT
990 relax_align (address
, alignment
)
991 register relax_addressT address
; /* Address now. */
992 register long alignment
; /* Alignment (binary). */
995 relax_addressT new_address
;
997 mask
= ~((~0) << alignment
);
998 new_address
= (address
+ mask
) & (~mask
);
1000 /* We must provide lots of padding, so the linker can discard it
1001 when needed. The linker will not add extra space, ever. */
1002 new_address
+= (1 << alignment
);
1003 return (new_address
- address
);
1004 } /* relax_align() */
1008 Go through all the fixS's in a segment and see which ones can be
1009 handled now. (These consist of fixS where we have since discovered
1010 the value of a symbol, or the address of the frag involved.)
1011 For each one, call md_apply_fix to put the fix into the frag data.
1013 Result is a count of how many relocation structs will be needed to
1014 handle the remaining fixS's that we couldn't completely handle here.
1015 These will be output later by emit_relocations(). */
1018 fixup_segment (fixP
, this_segment_type
)
1019 register fixS
*fixP
;
1020 segT this_segment_type
; /* N_TYPE bits for segment. */
1022 register long seg_reloc_count
;
1023 register symbolS
*add_symbolP
;
1024 register symbolS
*sub_symbolP
;
1025 register long add_number
;
1027 register char *place
;
1028 register long where
;
1029 register char pcrel
;
1030 register fragS
*fragP
;
1031 register segT add_symbol_segment
= SEG_ABSOLUTE
;
1033 /* FIXME: remove this line *//* fixS *orig = fixP; */
1034 seg_reloc_count
= 0;
1036 /* If the linker is doing the relaxing, we must not do any fixups */
1039 for (; fixP
; fixP
= fixP
->fx_next
)
1046 for (; fixP
; fixP
= fixP
->fx_next
)
1048 fragP
= fixP
->fx_frag
;
1050 where
= fixP
->fx_where
;
1051 place
= fragP
->fr_literal
+ where
;
1052 size
= fixP
->fx_size
;
1053 add_symbolP
= fixP
->fx_addsy
;
1055 if (fixP
->fx_callj
&& TC_S_IS_CALLNAME (add_symbolP
))
1057 /* Relocation should be done via the
1058 associated 'bal' entry point
1061 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP
)))
1063 as_bad ("No 'bal' entry point for leafproc %s",
1064 S_GET_NAME (add_symbolP
));
1067 fixP
->fx_addsy
= add_symbolP
= tc_get_bal_of_call (add_symbolP
);
1068 } /* callj relocation */
1070 sub_symbolP
= fixP
->fx_subsy
;
1071 add_number
= fixP
->fx_offset
;
1072 pcrel
= fixP
->fx_pcrel
;
1076 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
1077 } /* if there is an addend */
1084 if (S_GET_SEGMENT (sub_symbolP
) != SEG_ABSOLUTE
)
1086 as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP
));
1087 } /* not absolute */
1089 add_number
-= S_GET_VALUE (sub_symbolP
);
1091 /* if sub_symbol is in the same segment that add_symbol
1092 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
1094 else if ((S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
)
1095 && (SEG_NORMAL (add_symbol_segment
)
1096 || (add_symbol_segment
== SEG_ABSOLUTE
)))
1098 /* Difference of 2 symbols from same segment. */
1099 /* Can't make difference of 2 undefineds: 'value' means */
1100 /* something different for N_UNDF. */
1102 /* Makes no sense to use the difference of 2 arbitrary symbols
1103 * as the target of a call instruction.
1107 as_bad ("callj to difference of 2 symbols");
1109 #endif /* TC_I960 */
1110 add_number
+= S_GET_VALUE (add_symbolP
) -
1111 S_GET_VALUE (sub_symbolP
);
1114 fixP
->fx_addsy
= NULL
;
1118 /* Different segments in subtraction. */
1119 know (!(S_IS_EXTERNAL (sub_symbolP
) && (S_GET_SEGMENT (sub_symbolP
) == SEG_ABSOLUTE
)));
1121 if ((S_GET_SEGMENT (sub_symbolP
) == SEG_ABSOLUTE
))
1123 add_number
-= S_GET_VALUE (sub_symbolP
);
1127 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
1128 segment_name (S_GET_SEGMENT (sub_symbolP
)),
1129 S_GET_NAME (sub_symbolP
), fragP
->fr_address
+ where
);
1132 } /* if sub_symbolP */
1136 if (add_symbol_segment
== this_segment_type
&& pcrel
)
1139 * This fixup was made when the symbol's segment was
1140 * SEG_UNKNOWN, but it is now in the local segment.
1141 * So we know how to do the address without relocation.
1144 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
1145 * in which cases it modifies *fixP as appropriate. In the case
1146 * of a 'calls', no further work is required, and *fixP has been
1147 * set up to make the rest of the code below a no-op.
1150 #endif /* TC_I960 */
1152 add_number
+= S_GET_VALUE (add_symbolP
);
1153 add_number
-= md_pcrel_from (fixP
);
1154 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
1155 fixP
->fx_addsy
= NULL
; /* No relocations please. */
1159 switch (add_symbol_segment
)
1163 reloc_callj (fixP
); /* See comment about reloc_callj() above*/
1164 #endif /* TC_I960 */
1165 add_number
+= S_GET_VALUE (add_symbolP
);
1166 fixP
->fx_addsy
= NULL
;
1171 add_number
+= S_GET_VALUE (add_symbolP
);
1176 if ((int) fixP
->fx_bit_fixP
== 13)
1178 /* This is a COBR instruction. They have only a
1179 * 13-bit displacement and are only to be used
1180 * for local branches: flag as error, don't generate
1183 as_bad ("can't use COBR format with external label");
1184 fixP
->fx_addsy
= NULL
; /* No relocations please. */
1187 #endif /* TC_I960 */
1191 if (S_IS_COMMON (add_symbolP
))
1192 add_number
+= S_GET_VALUE (add_symbolP
);
1193 #endif /* TE_I386AIX */
1194 #endif /* OBJ_COFF */
1200 } /* switch on symbol seg */
1201 } /* if not in local seg */
1202 } /* if there was a + symbol */
1206 add_number
-= md_pcrel_from (fixP
);
1207 if (add_symbolP
== 0)
1209 fixP
->fx_addsy
= &abs_symbol
;
1211 } /* if there's an add_symbol */
1214 if (!fixP
->fx_bit_fixP
)
1217 (add_number
& ~0xFF) && ((add_number
& ~0xFF) != (-1 & ~0xFF))) ||
1219 (add_number
& ~0xFFFF) && ((add_number
& ~0xFFFF) != (-1 & ~0xFFFF))))
1221 as_bad ("Value of %d too large for field of %d bytes at 0x%x",
1222 add_number
, size
, fragP
->fr_address
+ where
);
1223 } /* generic error checking */
1224 #ifdef WARN_SIGNED_OVERFLOW_WORD
1225 /* Warn if a .word value is too large when
1226 treated as a signed number. We already
1227 know it is not too negative. This is to
1228 catch over-large switches generated by gcc
1232 && add_number
> 0x7fff)
1233 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
1234 add_number
, fragP
->fr_address
+ where
);
1236 } /* not a bit fix */
1238 md_apply_fix (fixP
, add_number
);
1239 } /* For each fixS in this segment. */
1246 /* two relocs per callj under coff. */
1247 for (fixP
= topP
; fixP
; fixP
= fixP
->fx_next
)
1249 if (fixP
->fx_callj
&& fixP
->fx_addsy
!= 0)
1252 } /* if callj and not already fixed. */
1253 } /* for each fix */
1255 #endif /* TC_I960 */
1257 #endif /* OBJ_COFF */
1258 return (seg_reloc_count
);
1259 } /* fixup_segment() */
1269 if (f1
->fr_next
== f2
)
1274 } /* is_dnrange() */
1276 /* Append a string onto another string, bumping the pointer along. */
1278 append (charPP
, fromP
, length
)
1281 unsigned long length
;
1284 { /* Don't trust memcpy() of 0 chars. */
1285 memcpy (*charPP
, fromP
, (int) length
);
1290 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
1293 * This routine records the largest alignment seen for each segment.
1294 * If the beginning of the segment is aligned on the worst-case
1295 * boundary, all of the other alignments within it will work. At
1296 * least one object format really uses this info.
1299 record_alignment (seg
, align
)
1300 segT seg
; /* Segment to which alignment pertains */
1301 int align
; /* Alignment, as a power of 2
1302 * (e.g., 1 => 2-byte boundary, 2 => 4-byte boundary, etc.)
1306 if (align
> section_alignment
[(int) seg
])
1308 section_alignment
[(int) seg
] = align
;
1309 } /* if highest yet */
1312 } /* record_alignment() */
1321 /* end of write.c */