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. */
23 This thing should be set up to do byteordering correctly. But...
25 In order to cross-assemble the target machine must have an a.out header
26 similar to the one in a.out.h on THIS machine. Byteorder doesn't matter,
27 we take special care of it, but the numbers must be the same SIZE (# of
28 bytes) and in the same PLACE. If this is not true, you will have some
35 #include "output-file.h"
37 /* The NOP_OPCODE is for the alignment fill value.
38 * fill it a nop instruction so that the disassembler does not choke
42 #define NOP_OPCODE 0x00
46 static struct frag
*text_frag_root
;
47 static struct frag
*data_frag_root
;
48 static struct frag
*bss_frag_root
;
50 static struct frag
*text_last_frag
; /* Last frag in segment. */
51 static struct frag
*data_last_frag
; /* Last frag in segment. */
52 static struct frag
*bss_last_frag
; /* Last frag in segment. */
55 static object_headers headers
;
57 long string_byte_count
;
59 static char *the_object_file
;
61 char *next_object_file_charP
; /* Tracks object file bytes. */
63 int magic_number_for_object_file
= DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE
;
65 /* static long length; JF unused */ /* String length, including trailing '\0'. */
70 static int is_dnrange(struct frag
*f1
, struct frag
*f2
);
71 static long fixup_segment(fixS
*fixP
, segT this_segment_type
);
72 static relax_addressT
relax_align(relax_addressT address
, long alignment
);
73 void relax_segment(struct frag
*segment_frag_root
, segT segment_type
);
77 static int is_dnrange();
78 static long fixup_segment();
79 static relax_addressT
relax_align();
82 #endif /* not __STDC__ */
87 * Create a fixS in obstack 'notes'.
89 fixS
*fix_new(frag
, where
, size
, add_symbol
, sub_symbol
, offset
, pcrel
, r_type
)
90 fragS
*frag
; /* Which frag? */
91 int where
; /* Where in that frag? */
92 short int size
; /* 1, 2, or 4 usually. */
93 symbolS
*add_symbol
; /* X_add_symbol. */
94 symbolS
*sub_symbol
; /* X_subtract_symbol. */
95 long offset
; /* X_add_number. */
96 int pcrel
; /* TRUE if PC-relative relocation. */
97 enum reloc_type r_type
; /* Relocation type */
101 fixP
= (fixS
*) obstack_alloc(¬es
, sizeof(fixS
));
103 fixP
->fx_frag
= frag
;
104 fixP
->fx_where
= where
;
105 fixP
->fx_size
= size
;
106 fixP
->fx_addsy
= add_symbol
;
107 fixP
->fx_subsy
= sub_symbol
;
108 fixP
->fx_offset
= offset
;
109 fixP
->fx_pcrel
= pcrel
;
110 fixP
->fx_r_type
= r_type
;
112 /* JF these 'cuz of the NS32K stuff */
113 fixP
->fx_im_disp
= 0;
114 fixP
->fx_pcrel_adjust
= 0;
116 fixP
->fx_bit_fixP
= 0;
118 /* usually, we want relocs sorted numerically, but while
119 comparing to older versions of gas that have relocs
120 reverse sorted, it is convenient to have this compile
121 time option. xoxorich. */
123 #ifdef REVERSE_SORT_RELOCS
125 fixP
->fx_next
= *seg_fix_rootP
;
126 *seg_fix_rootP
= fixP
;
128 #else /* REVERSE_SORT_RELOCS */
130 fixP
->fx_next
= NULL
;
133 (*seg_fix_tailP
)->fx_next
= fixP
;
135 *seg_fix_rootP
= fixP
;
136 *seg_fix_tailP
= fixP
;
138 #endif /* REVERSE_SORT_RELOCS */
145 void write_object_file()
147 register struct frchain
* frchainP
; /* Track along all frchains. */
148 register fragS
* fragP
; /* Track along all frags. */
149 register struct frchain
* next_frchainP
;
150 register fragS
* * prev_fragPP
;
151 /* register char * name; */
152 /* symbolS *symbolP; */
153 /* register symbolS ** symbolPP; */
154 /* register fixS * fixP; JF unused */
155 unsigned int data_siz
;
157 long object_file_size
;
161 * Under VMS we try to be compatible with VAX-11 "C". Thus, we
162 * call a routine to check for the definition of the procedure
163 * "_main", and if so -- fix it up so that it can be program
166 VMS_Check_For_Main();
169 * After every sub-segment, we fake an ".align ...". This conforms to BSD4.2
170 * brane-damage. We then fake ".fill 0" because that is the kind of frag
171 * that requires least thought. ".align" frags like to have a following
172 * frag since that makes calculating their intended length trivial.
174 #define SUB_SEGMENT_ALIGN (2)
175 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 frchain.
206 prev_fragPP
= &text_frag_root
;
207 for (frchainP
= frchain_root
; frchainP
; frchainP
= next_frchainP
) {
208 know( frchainP
->frch_root
);
209 * prev_fragPP
= frchainP
->frch_root
;
210 prev_fragPP
= & frchainP
->frch_last
->fr_next
;
211 next_frchainP
= frchainP
->frch_next
;
213 if (next_frchainP
== NULL
)
215 bss_last_frag
= frchainP
->frch_last
;
217 else if (next_frchainP
== data0_frchainP
)
219 text_last_frag
= frchainP
->frch_last
;
220 prev_fragPP
= & data_frag_root
;
222 else if (next_frchainP
== bss0_frchainP
)
224 data_last_frag
= frchainP
->frch_last
;
225 prev_fragPP
= & bss_frag_root
;
227 } /* walk the frag chain */
230 * We have two segments. If user gave -R flag, then we must put the
231 * data frags into the text segment. Do this before relaxing so
232 * we know to take advantage of -R and make shorter addresses.
234 if (flagseen
[ 'R' ]) {
237 text_last_frag
->fr_next
= data_frag_root
;
238 text_last_frag
= data_last_frag
;
239 data_last_frag
= NULL
;
240 data_frag_root
= NULL
;
242 for (tmp
= text_fix_root
; tmp
->fx_next
; tmp
= tmp
->fx_next
) ;;
243 tmp
->fx_next
=data_fix_root
;
245 text_fix_root
=data_fix_root
;
249 relax_segment(text_frag_root
, SEG_TEXT
);
250 relax_segment(data_frag_root
, SEG_DATA
);
251 relax_segment(bss_frag_root
, SEG_BSS
);
253 * Now the addresses of frags are correct within the segment.
256 know(text_last_frag
->fr_type
== rs_fill
&& text_last_frag
->fr_offset
== 0);
257 H_SET_TEXT_SIZE(&headers
, text_last_frag
->fr_address
);
258 text_last_frag
->fr_address
= H_GET_TEXT_SIZE(&headers
);
261 * Join the 2 segments into 1 huge segment.
262 * To do this, re-compute every rn_address in the SEG_DATA frags.
263 * Then join the data frags after the text frags.
265 * Determine a_data [length of data segment].
267 if (data_frag_root
) {
268 register relax_addressT slide
;
270 know((text_last_frag
->fr_type
== rs_fill
) && (text_last_frag
->fr_offset
== 0));
272 H_SET_DATA_SIZE(&headers
, data_last_frag
->fr_address
);
273 data_last_frag
->fr_address
= H_GET_DATA_SIZE(&headers
);
274 slide
= H_GET_TEXT_SIZE(&headers
); /* & in file of the data segment. */
276 for (fragP
= data_frag_root
; fragP
; fragP
= fragP
->fr_next
) {
277 fragP
->fr_address
+= slide
;
278 } /* for each data frag */
280 know(text_last_frag
!= 0);
281 text_last_frag
->fr_next
= data_frag_root
;
283 H_SET_DATA_SIZE(&headers
,0);
287 bss_address_frag
.fr_address
= (H_GET_TEXT_SIZE(&headers
) +
288 H_GET_DATA_SIZE(&headers
));
290 H_SET_BSS_SIZE(&headers
, bss_last_frag
->fr_address
);
293 * now fixup all bss frags addresses
297 relax_addressT slide
;
298 slide
= bss_address_frag
.fr_address
;
299 for (fragP
= bss_frag_root
; fragP
; fragP
= fragP
->fr_next
)
300 fragP
->fr_address
+= slide
;
305 * Crawl the symbol chain.
307 * For each symbol whose value depends on a frag, take the address of
308 * that frag and subsume it into the value of the symbol.
309 * After this, there is just one way to lookup a symbol value.
310 * Values are left in their final state for object file emission.
311 * We adjust the values of 'L' local symbols, even if we do
312 * not intend to emit them to the object file, because their values
313 * are needed for fix-ups.
315 * Unless we saw a -L flag, remove all symbols that begin with 'L'
316 * from the symbol chain. (They are still pointed to by the fixes.)
318 * Count the remaining symbols.
319 * Assign a symbol number to each symbol.
320 * Count the number of string-table chars we will emit.
321 * Put this info into the headers as appropriate.
324 know(zero_address_frag
.fr_address
== 0);
325 string_byte_count
= sizeof(string_byte_count
);
327 obj_crawl_symbol_chain(&headers
);
329 if (string_byte_count
== sizeof(string_byte_count
)) {
330 string_byte_count
= 0;
331 } /* if no strings, then no count. */
333 H_SET_STRING_SIZE(&headers
, string_byte_count
);
336 * Addresses of frags now reflect addresses we use in the object file.
337 * Symbol values are correct.
338 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
339 * Also converting any machine-dependent frags using md_convert_frag();
341 subseg_change(SEG_TEXT
, 0);
343 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
) {
344 switch (fragP
->fr_type
) {
347 fragP
->fr_type
= rs_fill
;
348 know(fragP
->fr_var
== 1);
349 know(fragP
->fr_next
!= NULL
);
351 fragP
->fr_offset
= (fragP
->fr_next
->fr_address
359 case rs_machine_dependent
:
360 md_convert_frag(&headers
, fragP
);
362 know((fragP
->fr_next
== NULL
) || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
) == fragP
->fr_fix
));
365 * After md_convert_frag, we make the frag into a ".space 0".
366 * Md_convert_frag() should set up any fixSs and constants
372 #ifndef WORKING_DOT_WORD
373 case rs_broken_word
: {
374 struct broken_word
*lie
;
375 extern md_short_jump_size
;
376 extern md_long_jump_size
;
378 if (fragP
->fr_subtype
) {
379 fragP
->fr_fix
+=md_short_jump_size
;
380 for (lie
=(struct broken_word
*)(fragP
->fr_symbol
);lie
&& lie
->dispfrag
==fragP
;lie
=lie
->next_broken_word
)
382 fragP
->fr_fix
+=md_long_jump_size
;
390 BAD_CASE( fragP
->fr_type
);
392 } /* switch (fr_type) */
394 know((fragP
->fr_next
== NULL
) || ((fragP
->fr_next
->fr_address
- fragP
->fr_address
) == (fragP
->fr_fix
+ (fragP
->fr_offset
* fragP
->fr_var
))));
395 } /* for each frag. */
397 #ifndef WORKING_DOT_WORD
399 struct broken_word
*lie
;
400 struct broken_word
**prevP
;
402 prevP
= &broken_words
;
403 for (lie
=broken_words
; lie
; lie
=lie
->next_broken_word
)
406 fix_new_ns32k(lie
->frag
,
407 lie
->word_goes_here
- lie
->frag
->fr_literal
,
414 fix_new( lie
->frag
, lie
->word_goes_here
- lie
->frag
->fr_literal
,
416 lie
->sub
, lie
->addnum
,
418 #endif /* TC_NS32K */
419 /* md_number_to_chars(lie->word_goes_here,
420 S_GET_VALUE(lie->add)
422 - S_GET_VALUE(lie->sub),
424 *prevP
=lie
->next_broken_word
;
426 prevP
= &(lie
->next_broken_word
);
428 for (lie
=broken_words
;lie
;) {
429 struct broken_word
*untruth
;
437 extern md_short_jump_size
;
438 extern md_long_jump_size
;
442 /* Find out how many broken_words go here */
444 for (untruth
=lie
;untruth
&& untruth
->dispfrag
==fragP
;untruth
=untruth
->next_broken_word
)
445 if (untruth
->added
==1)
448 table_ptr
=lie
->dispfrag
->fr_opcode
;
449 table_addr
=lie
->dispfrag
->fr_address
+(table_ptr
- lie
->dispfrag
->fr_literal
);
450 /* Create the jump around the long jumps */
451 /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */
452 from_addr
=table_addr
;
453 to_addr
= table_addr
+ md_short_jump_size
+ n
* md_long_jump_size
;
454 md_create_short_jump(table_ptr
, from_addr
, to_addr
, lie
->dispfrag
, lie
->add
);
455 table_ptr
+=md_short_jump_size
;
456 table_addr
+=md_short_jump_size
;
458 for (m
=0;lie
&& lie
->dispfrag
==fragP
;m
++,lie
=lie
->next_broken_word
) {
461 /* Patch the jump table */
462 /* This is the offset from ??? to table_ptr+0 */
464 - S_GET_VALUE(lie
->sub
);
465 md_number_to_chars(lie
->word_goes_here
,to_addr
,2);
466 for (untruth
=lie
->next_broken_word
;untruth
&& untruth
->dispfrag
==fragP
;untruth
=untruth
->next_broken_word
) {
467 if (untruth
->use_jump
==lie
)
468 md_number_to_chars(untruth
->word_goes_here
,to_addr
,2);
471 /* Install the long jump */
472 /* this is a long jump from table_ptr+0 to the final target */
473 from_addr
=table_addr
;
474 to_addr
=S_GET_VALUE(lie
->add
) + lie
->addnum
;
475 md_create_long_jump(table_ptr
,from_addr
,to_addr
,lie
->dispfrag
,lie
->add
);
476 table_ptr
+=md_long_jump_size
;
477 table_addr
+=md_long_jump_size
;
481 #endif /* not WORKING_DOT_WORD */
486 * Scan every FixS performing fixups. We had to wait until now to do
487 * this because md_convert_frag() may have made some fixSs.
490 H_SET_RELOCATION_SIZE(&headers
,
491 md_reloc_size
* fixup_segment(text_fix_root
, SEG_TEXT
),
492 md_reloc_size
* fixup_segment(data_fix_root
, SEG_DATA
));
495 /* FIXME move this stuff into the pre-write-hook */
496 H_SET_MAGIC_NUMBER(&headers
, magic_number_for_object_file
);
497 H_SET_ENTRY_POINT(&headers
, 0);
499 obj_pre_write_hook(&headers
); /* extra coff stuff */
500 if ((had_warnings() && flagseen
['Z'])
501 || had_errors() > 0) {
503 as_warn("%d error%s, %d warning%s, generating bad object file.\n",
504 had_errors(), had_errors() == 1 ? "" : "s",
505 had_warnings(), had_warnings() == 1 ? "" : "s");
507 as_fatal("%d error%s, %d warning%s, no object file generated.\n",
508 had_errors(), had_errors() == 1 ? "" : "s",
509 had_warnings(), had_warnings() == 1 ? "" : "s");
510 } /* on want output */
511 } /* on error condition */
513 object_file_size
= H_GET_FILE_SIZE(&headers
);
514 next_object_file_charP
= the_object_file
= xmalloc(object_file_size
);
516 output_file_create(out_file_name
);
518 obj_header_append(&next_object_file_charP
, &headers
);
520 know((next_object_file_charP
- the_object_file
) == H_GET_HEADER_SIZE(&headers
));
525 for (fragP
= text_frag_root
; fragP
; fragP
= fragP
->fr_next
) {
527 register char *fill_literal
;
528 register long fill_size
;
530 know(fragP
->fr_type
== rs_fill
);
531 append(&next_object_file_charP
, fragP
->fr_literal
, (unsigned long) fragP
->fr_fix
);
532 fill_literal
= fragP
->fr_literal
+ fragP
->fr_fix
;
533 fill_size
= fragP
->fr_var
;
534 know(fragP
->fr_offset
>= 0);
536 for (count
= fragP
->fr_offset
; count
; count
--) {
537 append(&next_object_file_charP
, fill_literal
, (unsigned long) fill_size
);
540 } /* for each code frag. */
542 know((next_object_file_charP
- the_object_file
) == (H_GET_HEADER_SIZE(&headers
) + H_GET_TEXT_SIZE(&headers
) + H_GET_DATA_SIZE(&headers
)));
547 obj_emit_relocations(&next_object_file_charP
, text_fix_root
, (relax_addressT
)0);
548 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
)));
550 /* Make addresses in data relocation directives relative to beginning of
551 * first data fragment, not end of last text fragment: alignment of the
552 * start of the data segment may place a gap between the segments.
554 obj_emit_relocations(&next_object_file_charP
, data_fix_root
, data0_frchainP
->frch_root
->fr_address
);
556 obj_emit_relocations(&next_object_file_charP
, data_fix_root
, text_last_frag
->fr_address
);
559 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
)));
562 * Emit line number entries.
564 OBJ_EMIT_LINENO(&next_object_file_charP
, lineno_rootP
, the_object_file
);
565 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
)));
570 obj_emit_symbols(&next_object_file_charP
, symbol_rootP
);
571 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
)));
577 if (string_byte_count
> 0) {
578 obj_emit_strings(&next_object_file_charP
);
579 } /* only if we have a string table */
581 /* 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)));
583 /* know(next_object_file_charP == the_object_file + object_file_size);*/
586 bfd_seek(stdoutput
, 0, 0);
587 bfd_write(the_object_file
, 1, object_file_size
, stdoutput
);
590 /* Write the data to the file */
591 output_file_append(the_object_file
,object_file_size
,out_file_name
);
594 output_file_close(out_file_name
);
595 } /* non vms output */
598 * Now do the VMS-dependent part of writing the object file
600 VMS_write_object_file(text_siz
, data_siz
, text_frag_root
, data_frag_root
);
602 } /* write_object_file() */
609 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
614 * After this, all frags in this segment have addresses that are correct
615 * within the segment. Since segments live in different file addresses,
616 * these frag addresses may not be the same as final object-file addresses.
621 void relax_segment(segment_frag_root
, segment
)
622 struct frag
* segment_frag_root
;
623 segT segment
; /* SEG_DATA or SEG_TEXT or SEG_BSS */
625 register struct frag
* fragP
;
626 register relax_addressT address
;
627 /* register relax_addressT old_address; JF unused */
628 /* register relax_addressT new_address; JF unused */
629 #ifndef MANY_SEGMENTS
630 know(segment
== SEG_DATA
|| segment
== SEG_TEXT
|| segment
== SEG_BSS
);
632 /* In case md_estimate_size_before_relax() wants to make fixSs. */
633 subseg_change(segment
, 0);
636 * For each frag in segment: count and store (a 1st guess of) fr_address.
639 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
) {
640 fragP
->fr_address
= address
;
641 address
+= fragP
->fr_fix
;
643 switch (fragP
->fr_type
) {
645 address
+= fragP
->fr_offset
* fragP
->fr_var
;
649 address
+= relax_align(address
, fragP
->fr_offset
);
654 * Assume .org is nugatory. It will grow with 1st relax.
658 case rs_machine_dependent
:
659 address
+= md_estimate_size_before_relax(fragP
, segment
);
662 #ifndef WORKING_DOT_WORD
663 /* Broken words don't concern us yet */
669 BAD_CASE(fragP
->fr_type
);
671 } /* switch(fr_type) */
672 } /* for each frag in the segment */
678 register long stretch
; /* May be any size, 0 or negative. */
679 /* Cumulative number of addresses we have */
680 /* relaxed this pass. */
681 /* We may have relaxed more than one address. */
682 register long stretched
; /* Have we stretched on this pass? */
683 /* This is 'cuz stretch may be zero, when,
684 in fact some piece of code grew, and
685 another shrank. If a branch instruction
686 doesn't fit anymore, we could be scrod */
689 stretch
= stretched
= 0;
690 for (fragP
= segment_frag_root
; fragP
; fragP
= fragP
->fr_next
) {
691 register long growth
= 0;
692 register unsigned long was_address
;
693 /* register long var; */
694 register long offset
;
695 register symbolS
*symbolP
;
696 register long target
;
700 was_address
= fragP
->fr_address
;
701 address
= fragP
->fr_address
+= stretch
;
702 symbolP
= fragP
->fr_symbol
;
703 offset
= fragP
->fr_offset
;
704 /* var = fragP->fr_var; */
706 switch (fragP
->fr_type
) {
707 case rs_fill
: /* .fill never relaxes. */
711 #ifndef WORKING_DOT_WORD
712 /* JF: This is RMS's idea. I do *NOT* want to be blamed
713 for it I do not want to write it. I do not want to have
714 anything to do with it. This is not the proper way to
715 implement this misfeature. */
716 case rs_broken_word
: {
717 struct broken_word
*lie
;
718 struct broken_word
*untruth
;
719 extern int md_short_jump_size
;
720 extern int md_long_jump_size
;
722 /* Yes this is ugly (storing the broken_word pointer
723 in the symbol slot). Still, this whole chunk of
724 code is ugly, and I don't feel like doing anything
725 about it. Think of it as stubbornness in action */
727 for (lie
=(struct broken_word
*)(fragP
->fr_symbol
);
728 lie
&& lie
->dispfrag
==fragP
;
729 lie
=lie
->next_broken_word
) {
734 offset
= lie
->add
->sy_frag
->fr_address
+ S_GET_VALUE(lie
->add
) + lie
->addnum
-
735 (lie
->sub
->sy_frag
->fr_address
+ S_GET_VALUE(lie
->sub
));
736 if (offset
<=-32768 || offset
>=32767) {
738 as_warn(".word %s-%s+%ld didn't fit",
739 S_GET_NAME(lie
->add
),
740 S_GET_NAME(lie
->sub
),
743 if (fragP
->fr_subtype
==0) {
745 growth
+=md_short_jump_size
;
747 for (untruth
=lie
->next_broken_word
;untruth
&& untruth
->dispfrag
==lie
->dispfrag
;untruth
=untruth
->next_broken_word
)
748 if ((untruth
->add
->sy_frag
== lie
->add
->sy_frag
)
749 && S_GET_VALUE(untruth
->add
) == S_GET_VALUE(lie
->add
)) {
751 untruth
->use_jump
=lie
;
753 growth
+=md_long_jump_size
;
758 } /* case rs_broken_word */
761 growth
= relax_align((relax_addressT
) (address
+ fragP
->fr_fix
), offset
)
762 - relax_align((relax_addressT
) (was_address
+ fragP
->fr_fix
), offset
);
771 know((S_GET_SEGMENT(symbolP
) == SEG_ABSOLUTE
)
772 || (S_GET_SEGMENT(symbolP
) == SEG_DATA
)
773 || (S_GET_SEGMENT(symbolP
) == SEG_TEXT
)
774 || (S_GET_SEGMENT(symbolP
) == SEG_BSS
));
775 know(symbolP
->sy_frag
);
776 know(!(S_GET_SEGMENT(symbolP
) == SEG_ABSOLUTE
) || (symbolP
->sy_frag
== &zero_address_frag
));
778 target
+= S_GET_VALUE(symbolP
)
779 + symbolP
->sy_frag
->fr_address
;
780 } /* if we have a symbol */
782 know(fragP
->fr_next
);
783 after
= fragP
->fr_next
->fr_address
;
784 growth
= ((target
- after
) > 0) ? (target
- after
) : 0;
785 /* Growth may be -ve, but variable part */
786 /* of frag cannot have < 0 chars. */
787 /* That is, we can't .org backwards. */
789 growth
-= stretch
; /* This is an absolute growth factor */
792 case rs_machine_dependent
: {
793 register const relax_typeS
* this_type
;
794 register const relax_typeS
* start_type
;
795 register relax_substateT next_state
;
796 register relax_substateT this_state
;
798 start_type
= this_type
= md_relax_table
+ (this_state
= fragP
->fr_subtype
);
802 #ifndef MANY_SEGMENTS
803 know((S_GET_SEGMENT(symbolP
) == SEG_ABSOLUTE
) || (S_GET_SEGMENT(symbolP
) == SEG_DATA
) || (S_GET_SEGMENT(symbolP
) == SEG_TEXT
));
805 know(symbolP
->sy_frag
);
806 know(!(S_GET_SEGMENT(symbolP
) == SEG_ABSOLUTE
) || symbolP
->sy_frag
==&zero_address_frag
);
809 + symbolP
->sy_frag
->fr_address
;
811 /* If frag has yet to be reached on this pass,
812 assume it will move by STRETCH just as we did.
813 If this is not so, it will be because some frag
814 between grows, and that will force another pass. */
816 /* JF was just address */
817 /* JF also added is_dnrange hack */
818 /* There's gotta be a better/faster/etc way
820 /* gnu@cygnus.com: I changed this from > to >=
821 because I ran into a zero-length frag (fr_fix=0)
822 which was created when the obstack needed a new
823 chunk JUST AFTER the opcode of a branch. Since
824 fr_fix is zero, fr_address of this frag is the same
825 as fr_address of the next frag. This
826 zero-length frag was variable and jumped to .+2
827 (in the next frag), but since the > comparison
828 below failed (the two were =, not >), "stretch"
829 was not added to the target. Stretch was 178, so
830 the offset appeared to be .-176 instead, which did
831 not fit into a byte branch, so the assembler
832 relaxed the branch to a word. This didn't compare
833 with what happened when the same source file was
834 assembled on other machines, which is how I found it.
835 You might want to think about what other places have
836 trouble with zero length frags... */
838 if (symbolP
->sy_frag
->fr_address
>= was_address
839 && is_dnrange(fragP
,symbolP
->sy_frag
)) {
843 } /* if there's a symbol attached */
845 aim
= target
- address
- fragP
->fr_fix
;
846 /* The displacement is affected by the instruction size
847 * for the 32k architecture. I think we ought to be able
848 * to add fragP->fr_pcrel_adjust in all cases (it should be
849 * zero if not used), but just in case it breaks something
850 * else we'll put this inside #ifdef NS32K ... #endif
853 aim
+= fragP
->fr_pcrel_adjust
;
854 #endif /* TC_NS32K */
857 /* Look backwards. */
858 for (next_state
= this_type
->rlx_more
; next_state
; ) {
859 if (aim
>= this_type
->rlx_backward
) {
861 } else { /* Grow to next state. */
862 this_type
= md_relax_table
+ (this_state
= next_state
);
863 next_state
= this_type
->rlx_more
;
868 /* JF these next few lines of code are for the mc68020 which can't handle short
869 offsets of zero in branch instructions. What a kludge! */
870 if (aim
==0 && this_state
==(1<<2+0)) { /* FOO hard encoded from m.c */
871 aim
=this_type
->rlx_forward
+1; /* Force relaxation into word mode */
874 #ifdef M68K_AIM_KLUDGE
875 M68K_AIM_KLUDGE(aim
, this_state
, this_type
);
877 /* JF end of 68020 code */
879 for (next_state
= this_type
->rlx_more
; next_state
; ) {
880 if (aim
<= this_type
->rlx_forward
) {
882 } else { /* Grow to next state. */
883 this_type
= md_relax_table
+ (this_state
= next_state
);
884 next_state
= this_type
->rlx_more
;
889 if ((growth
= this_type
->rlx_length
- start_type
->rlx_length
) != 0)
890 fragP
->fr_subtype
= this_state
;
893 } /* case rs_machine_dependent */
896 BAD_CASE( fragP
->fr_type
);
903 } /* For each frag in the segment. */
904 } while (stretched
); /* Until nothing further to relax. */
908 * We now have valid fr_address'es for each frag.
912 * All fr_address's are correct, relative to their own segment.
913 * We have made all the fixS we will ever make.
915 } /* relax_segment() */
918 * Relax_align. Advance location counter to next address that has 'alignment'
919 * lowest order bits all 0s.
922 /* How many addresses does the .align take? */
923 static relax_addressT
relax_align(address
, alignment
)
924 register relax_addressT address
; /* Address now. */
925 register long alignment
; /* Alignment (binary). */
928 relax_addressT new_address
;
930 mask
= ~ ( (~0) << alignment
);
931 new_address
= (address
+ mask
) & (~ mask
);
932 return (new_address
- address
);
933 } /* relax_align() */
937 Go through all the fixS's in a segment and see which ones can be
938 handled now. (These consist of fixS where we have since discovered
939 the value of a symbol, or the address of the frag involved.)
940 For each one, call md_apply_fix to put the fix into the frag data.
942 Result is a count of how many relocation structs will be needed to
943 handle the remaining fixS's that we couldn't completely handle here.
944 These will be output later by emit_relocations(). */
946 static long fixup_segment(fixP
, this_segment_type
)
947 register fixS
* fixP
;
948 segT this_segment_type
; /* N_TYPE bits for segment. */
950 register long seg_reloc_count
;
951 register symbolS
*add_symbolP
;
952 register symbolS
*sub_symbolP
;
953 register long add_number
;
955 register char *place
;
958 register fragS
*fragP
;
959 register segT add_symbol_segment
= SEG_ABSOLUTE
;
961 /* FIXME: remove this line */ /* fixS *orig = fixP; */
964 for ( ; fixP
; fixP
= fixP
->fx_next
) {
965 fragP
= fixP
->fx_frag
;
967 where
= fixP
->fx_where
;
968 place
= fragP
->fr_literal
+ where
;
969 size
= fixP
->fx_size
;
970 add_symbolP
= fixP
->fx_addsy
;
972 if (fixP
->fx_callj
&& TC_S_IS_CALLNAME(add_symbolP
)) {
973 /* Relocation should be done via the
974 associated 'bal' entry point
977 if (!TC_S_IS_BALNAME(tc_get_bal_of_call(add_symbolP
))) {
978 as_bad("No 'bal' entry point for leafproc %s",
979 S_GET_NAME(add_symbolP
));
982 fixP
->fx_addsy
= add_symbolP
= tc_get_bal_of_call(add_symbolP
);
983 } /* callj relocation */
985 sub_symbolP
= fixP
->fx_subsy
;
986 add_number
= fixP
->fx_offset
;
987 pcrel
= fixP
->fx_pcrel
;
990 add_symbol_segment
= S_GET_SEGMENT(add_symbolP
);
991 } /* if there is an addend */
996 if (S_GET_SEGMENT(sub_symbolP
) != SEG_ABSOLUTE
) {
997 as_bad("Negative of non-absolute symbol %s", S_GET_NAME(sub_symbolP
));
1000 add_number
-= S_GET_VALUE(sub_symbolP
);
1002 /* if sub_symbol is in the same segment that add_symbol
1003 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
1004 } else if ((S_GET_SEGMENT(sub_symbolP
) == add_symbol_segment
)
1005 && (SEG_NORMAL(add_symbol_segment
)
1006 || (add_symbol_segment
== SEG_ABSOLUTE
))) {
1007 /* Difference of 2 symbols from same segment. */
1008 /* Can't make difference of 2 undefineds: 'value' means */
1009 /* something different for N_UNDF. */
1011 /* Makes no sense to use the difference of 2 arbitrary symbols
1012 * as the target of a call instruction.
1014 if (fixP
->fx_callj
) {
1015 as_bad("callj to difference of 2 symbols");
1017 #endif /* TC_I960 */
1018 add_number
+= S_GET_VALUE(add_symbolP
) -
1019 S_GET_VALUE(sub_symbolP
);
1022 fixP
->fx_addsy
= NULL
;
1024 /* Different segments in subtraction. */
1025 know(!(S_IS_EXTERNAL(sub_symbolP
) && (S_GET_SEGMENT(sub_symbolP
) == SEG_ABSOLUTE
)));
1027 if ((S_GET_SEGMENT(sub_symbolP
) == SEG_ABSOLUTE
)) {
1028 add_number
-= S_GET_VALUE(sub_symbolP
);
1030 as_bad("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
1031 segment_name(S_GET_SEGMENT(sub_symbolP
)),
1032 S_GET_NAME(sub_symbolP
), fragP
->fr_address
+ where
);
1035 } /* if sub_symbolP */
1038 if (add_symbol_segment
== this_segment_type
&& pcrel
) {
1040 * This fixup was made when the symbol's segment was
1041 * SEG_UNKNOWN, but it is now in the local segment.
1042 * So we know how to do the address without relocation.
1045 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
1046 * in which cases it modifies *fixP as appropriate. In the case
1047 * of a 'calls', no further work is required, and *fixP has been
1048 * set up to make the rest of the code below a no-op.
1051 #endif /* TC_I960 */
1053 add_number
+= S_GET_VALUE(add_symbolP
);
1054 add_number
-= md_pcrel_from (fixP
);
1055 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
1056 fixP
->fx_addsy
= NULL
; /* No relocations please. */
1058 switch (add_symbol_segment
) {
1061 reloc_callj(fixP
); /* See comment about reloc_callj() above*/
1062 #endif /* TC_I960 */
1063 add_number
+= S_GET_VALUE(add_symbolP
);
1064 fixP
->fx_addsy
= NULL
;
1069 add_number
+= S_GET_VALUE(add_symbolP
);
1074 if ((int)fixP
->fx_bit_fixP
== 13) {
1075 /* This is a COBR instruction. They have only a
1076 * 13-bit displacement and are only to be used
1077 * for local branches: flag as error, don't generate
1080 as_bad("can't use COBR format with external label");
1081 fixP
->fx_addsy
= NULL
; /* No relocations please. */
1084 #endif /* TC_I960 */
1087 /* This really needed to be
1088 like this for COFF output.
1091 But I'm not sure it's right
1092 for i960 or a29k coff.
1095 if (S_IS_COMMON(add_symbolP
))
1096 add_number
+= S_GET_VALUE(add_symbolP
);
1097 #endif /* OBJ_COFF */
1103 } /* switch on symbol seg */
1104 } /* if not in local seg */
1105 } /* if there was a + symbol */
1108 add_number
-= md_pcrel_from(fixP
);
1109 if (add_symbolP
== 0) {
1110 fixP
->fx_addsy
= & abs_symbol
;
1112 } /* if there's an add_symbol */
1115 if (!fixP
->fx_bit_fixP
) {
1117 (add_number
& ~0xFF) && (add_number
&~0xFF!=(-1&~0xFF))) ||
1119 (add_number
& ~0xFFFF) && (add_number
&~0xFFFF!=(-1&~0xFFFF)))) {
1120 as_bad("Value of %d too large for field of %d bytes at 0x%x",
1121 add_number
, size
, fragP
->fr_address
+ where
);
1122 } /* generic error checking */
1123 } /* not a bit fix */
1125 md_apply_fix(fixP
, add_number
);
1126 } /* For each fixS in this segment. */
1133 /* two relocs per callj under coff. */
1134 for (fixP
= topP
; fixP
; fixP
= fixP
->fx_next
) {
1135 if (fixP
->fx_callj
&& fixP
->fx_addsy
!= 0) {
1137 } /* if callj and not already fixed. */
1138 } /* for each fix */
1140 #endif /* TC_I960 */
1142 #endif /* OBJ_COFF */
1143 return(seg_reloc_count
);
1144 } /* fixup_segment() */
1147 static int is_dnrange(f1
,f2
)
1152 if (f1
->fr_next
==f2
)
1157 } /* is_dnrange() */
1159 /* Append a string onto another string, bumping the pointer along. */
1161 append (charPP
, fromP
, length
)
1164 unsigned long length
;
1166 if (length
) { /* Don't trust memcpy() of 0 chars. */
1167 memcpy(*charPP
, fromP
, (int) length
);
1172 int section_alignment
[SEG_MAXIMUM_ORDINAL
];
1175 * This routine records the largest alignment seen for each segment.
1176 * If the beginning of the segment is aligned on the worst-case
1177 * boundary, all of the other alignments within it will work. At
1178 * least one object format really uses this info.
1180 void record_alignment(seg
, align
)
1181 segT seg
; /* Segment to which alignment pertains */
1182 int align
; /* Alignment, as a power of 2
1183 * (e.g., 1 => 2-byte boundary, 2 => 4-byte boundary, etc.)
1187 if ( align
> section_alignment
[(int) seg
] ){
1188 section_alignment
[(int) seg
] = align
;
1189 } /* if highest yet */
1192 } /* record_alignment() */
1201 /* end of write.c */