Ran "indent", for GNU coding style; some code & comments still need fixup.
[deliverable/binutils-gdb.git] / gas / write.c
1 /* write.c - emit .o file
2
3 Copyright (C) 1986, 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
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)
10 any later version.
11
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.
16
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. */
20
21 /* This thing should be set up to do byteordering correctly. But... */
22
23 #include "as.h"
24 #include "subsegs.h"
25 #include "obstack.h"
26 #include "output-file.h"
27
28 /* The NOP_OPCODE is for the alignment fill value.
29 * fill it a nop instruction so that the disassembler does not choke
30 * on it
31 */
32 #ifndef NOP_OPCODE
33 #define NOP_OPCODE 0x00
34 #endif
35
36 #ifndef MANY_SEGMENTS
37 struct frag *text_frag_root;
38 struct frag *data_frag_root;
39 struct frag *bss_frag_root;
40
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. */
44 #endif
45
46 static object_headers headers;
47
48 long string_byte_count;
49
50 static char *the_object_file;
51
52 char *next_object_file_charP; /* Tracks object file bytes. */
53
54 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
55
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));
60
61 /*
62 * fix_new()
63 *
64 * Create a fixS in obstack 'notes'.
65 */
66 fixS *
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 */
76 {
77 fixS *fixP;
78
79 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
80
81 fixP->fx_frag = frag;
82 fixP->fx_where = where;
83 fixP->fx_size = size;
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;
90 #endif
91 /* JF these 'cuz of the NS32K stuff */
92 fixP->fx_im_disp = 0;
93 fixP->fx_pcrel_adjust = 0;
94 fixP->fx_bsr = 0;
95 fixP->fx_bit_fixP = 0;
96
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. */
101
102 #ifdef REVERSE_SORT_RELOCS
103
104 fixP->fx_next = *seg_fix_rootP;
105 *seg_fix_rootP = fixP;
106
107 #else /* REVERSE_SORT_RELOCS */
108
109 fixP->fx_next = NULL;
110
111 if (*seg_fix_tailP)
112 (*seg_fix_tailP)->fx_next = fixP;
113 else
114 *seg_fix_rootP = fixP;
115 *seg_fix_tailP = fixP;
116
117 #endif /* REVERSE_SORT_RELOCS */
118
119 fixP->fx_callj = 0;
120 return (fixP);
121 } /* fix_new() */
122
123 #ifndef BFD
124
125 void
126 remove_subsegs (head, seg, root, last)
127 frchainS *head;
128 int seg;
129 fragS **root;
130 fragS **last;
131 {
132 fragS dummy;
133 fragS *prev_frag = &dummy;
134 *root = head->frch_root;
135 while (head && head->frch_seg == seg)
136 {
137 prev_frag->fr_next = head->frch_root;
138
139 prev_frag = head->frch_last;
140 head = head->frch_next;
141 }
142 *last = prev_frag;
143 prev_frag->fr_next = 0;
144 }
145
146 void
147 write_object_file ()
148 {
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;
154
155 long object_file_size;
156
157 #ifdef VMS
158 /*
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
162 * entry point.
163 */
164 VMS_Check_For_Main ();
165 #endif /* VMS */
166 /*
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
171 * length trivial.
172 */
173 #define SUB_SEGMENT_ALIGN (2)
174 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
175 {
176 #ifdef VMS
177 /*
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.
182 */
183 #undef SUB_SEGMENT_ALIGN
184 #define SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0)
185 #endif /* VMS */
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". */
190 /*
191 * For this segment ...
192 * Create a last frag. Do not leave a "being filled in frag".
193 */
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 */
200
201 /*
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
205 * frchain.
206 */
207
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);
211
212 /*
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.
216 */
217 #ifndef OBJ_AOUT
218 if (flagseen['R'])
219 {
220 fixS *tmp;
221
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;
226 if (text_fix_root)
227 {
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;
231 }
232 else
233 text_fix_root = data_fix_root;
234 data_fix_root = NULL;
235 }
236 #endif
237 relax_segment (text_frag_root, SEG_TEXT);
238 relax_segment (data_frag_root, SEG_DATA);
239 relax_segment (bss_frag_root, SEG_BSS);
240 /*
241 * Now the addresses of frags are correct within the segment.
242 */
243
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);
247
248 /*
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.
252 *
253 * Determine a_data [length of data segment].
254 */
255 if (data_frag_root)
256 {
257 register relax_addressT slide;
258
259 know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
260
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. */
264 #ifdef OBJ_BOUT
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
270 section.... */
271 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
272 #endif
273
274 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
275 {
276 fragP->fr_address += slide;
277 } /* for each data frag */
278
279 know (text_last_frag != 0);
280 text_last_frag->fr_next = data_frag_root;
281 }
282 else
283 {
284 H_SET_DATA_SIZE (&headers, 0);
285 data_siz = 0;
286 }
287
288 #ifdef OBJ_BOUT
289 /* See above comments on b.out data section address. */
290 {
291 long bss_vma;
292 if (data_last_frag == 0)
293 bss_vma = H_GET_TEXT_SIZE (&headers);
294 else
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;
298 }
299 #else
300 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
301 H_GET_DATA_SIZE (&headers));
302
303
304 /* Slide all the frags */
305 if (bss_frag_root)
306 {
307 relax_addressT slide = bss_address_frag.fr_address + local_bss_counter;
308
309 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
310 {
311 fragP->fr_address += slide;
312 } /* for each bss frag */
313 }
314
315 #endif
316 if (bss_last_frag)
317 {
318 local_bss_counter += bss_last_frag->fr_address - bss_frag_root->fr_address;
319 }
320 H_SET_BSS_SIZE (&headers, local_bss_counter);
321
322 /*
323 *
324 * Crawl the symbol chain.
325 *
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.
333 *
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.)
336 *
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.
341 *
342 */
343 know (zero_address_frag.fr_address == 0);
344 string_byte_count = sizeof (string_byte_count);
345
346 obj_crawl_symbol_chain (&headers);
347
348 if (string_byte_count == sizeof (string_byte_count))
349 {
350 string_byte_count = 0;
351 } /* if no strings, then no count. */
352
353 H_SET_STRING_SIZE (&headers, string_byte_count);
354
355 /*
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();
360 */
361 subseg_change (SEG_TEXT, 0);
362
363 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
364 {
365 switch (fragP->fr_type)
366 {
367 case rs_align:
368 case rs_org:
369 #ifdef HANDLE_ALIGN
370 HANDLE_ALIGN (fragP);
371 #endif
372 fragP->fr_type = rs_fill;
373 know (fragP->fr_var == 1);
374 know (fragP->fr_next != NULL);
375
376 fragP->fr_offset = (fragP->fr_next->fr_address
377 - fragP->fr_address
378 - fragP->fr_fix);
379 break;
380
381 case rs_fill:
382 break;
383
384 case rs_machine_dependent:
385 md_convert_frag (&headers, fragP);
386
387 know ((fragP->fr_next == NULL) || ((fragP->fr_next->fr_address - fragP->fr_address) == fragP->fr_fix));
388
389 /*
390 * After md_convert_frag, we make the frag into a ".space 0".
391 * Md_convert_frag() should set up any fixSs and constants
392 * required.
393 */
394 frag_wane (fragP);
395 break;
396
397 #ifndef WORKING_DOT_WORD
398 case rs_broken_word:
399 {
400 struct broken_word *lie;
401 extern md_short_jump_size;
402 extern md_long_jump_size;
403
404 if (fragP->fr_subtype)
405 {
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)
408 if (lie->added == 1)
409 fragP->fr_fix += md_long_jump_size;
410 }
411 frag_wane (fragP);
412 }
413 break;
414 #endif
415
416 default:
417 BAD_CASE (fragP->fr_type);
418 break;
419 } /* switch (fr_type) */
420
421 if (!((fragP->fr_next == NULL)
422 #ifdef OBJ_BOUT
423 || (fragP->fr_next == data_frag_root)
424 #endif
425 || ((fragP->fr_next->fr_address - fragP->fr_address)
426 == (fragP->fr_fix + (fragP->fr_offset * fragP->fr_var)))))
427 {
428 fprintf (stderr, "assertion failed: file `%s', line %d\n",
429 __FILE__, __LINE__ - 4);
430 exit (1);
431 }
432 } /* for each frag. */
433
434 #ifndef WORKING_DOT_WORD
435 {
436 struct broken_word *lie;
437 struct broken_word **prevP;
438
439 prevP = &broken_words;
440 for (lie = broken_words; lie; lie = lie->next_broken_word)
441 if (!lie->added)
442 {
443 #ifdef TC_NS32K
444 fix_new_ns32k (lie->frag,
445 lie->word_goes_here - lie->frag->fr_literal,
446 2,
447 lie->add,
448 lie->sub,
449 lie->addnum,
450 0, 0, 2, 0, 0);
451 #else
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,
454 2, lie->add,
455 lie->sub, lie->addnum,
456 0, NO_RELOC);
457 # else
458 fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
459 2, lie->add,
460 lie->sub, lie->addnum,
461 0, 0);
462
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)
467 + lie->addnum
468 - S_GET_VALUE(lie->sub),
469 2); */
470 *prevP = lie->next_broken_word;
471 }
472 else
473 prevP = &(lie->next_broken_word);
474
475 for (lie = broken_words; lie;)
476 {
477 struct broken_word *untruth;
478 char *table_ptr;
479 long table_addr;
480 long from_addr, to_addr;
481 int n, m;
482
483 extern md_short_jump_size;
484 extern md_long_jump_size;
485
486 fragP = lie->dispfrag;
487
488 /* Find out how many broken_words go here */
489 n = 0;
490 for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
491 if (untruth->added == 1)
492 n++;
493
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;
503
504 for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
505 {
506 if (lie->added == 2)
507 continue;
508 /* Patch the jump table */
509 /* This is the offset from ??? to table_ptr+0 */
510 to_addr = table_addr
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)
514 {
515 if (untruth->use_jump == lie)
516 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
517 }
518
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;
526 }
527 }
528 }
529 #endif /* not WORKING_DOT_WORD */
530
531 #ifndef VMS
532 { /* not vms */
533 /*
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.
536 */
537 int trsize, drsize;
538
539 subseg_change (SEG_TEXT, 0);
540 trsize = md_reloc_size * fixup_segment (text_fix_root,
541 SEG_TEXT);
542 subseg_change (SEG_DATA, 0);
543 drsize = md_reloc_size * fixup_segment (data_fix_root,
544 SEG_DATA);
545 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
546
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);
550
551 obj_pre_write_hook (&headers); /* extra coff stuff */
552 if ((had_warnings () && flagseen['Z'])
553 || had_errors () > 0)
554 {
555 if (flagseen['Z'])
556 {
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");
560 }
561 else
562 {
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 */
568
569 object_file_size = H_GET_FILE_SIZE (&headers);
570 next_object_file_charP = the_object_file = xmalloc (object_file_size);
571
572 output_file_create (out_file_name);
573
574 obj_header_append (&next_object_file_charP, &headers);
575
576 know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
577
578 /*
579 * Emit code.
580 */
581 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
582 {
583 register long count;
584 register char *fill_literal;
585 register long fill_size;
586
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);
592
593 for (count = fragP->fr_offset; count; count--)
594 {
595 append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
596 } /* for each */
597
598 } /* for each code frag. */
599
600 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
601
602 /*
603 * Emit relocations.
604 */
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)));
607 #ifdef TC_I960
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.
611 */
612 obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
613 #else /* TC_I960 */
614 obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
615 #endif /* TC_I960 */
616
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)));
618
619 /*
620 * Emit line number entries.
621 */
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)));
624
625 /*
626 * Emit symbols.
627 */
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)));
630
631 /*
632 * Emit strings.
633 */
634
635 if (string_byte_count > 0)
636 {
637 obj_emit_strings (&next_object_file_charP);
638 } /* only if we have a string table */
639
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)));
641 */
642 /* know(next_object_file_charP == the_object_file + object_file_size);*/
643
644 #ifdef BFD_HEADERS
645 bfd_seek (stdoutput, 0, 0);
646 bfd_write (the_object_file, 1, object_file_size, stdoutput);
647 #else
648
649 /* Write the data to the file */
650 output_file_append (the_object_file, object_file_size, out_file_name);
651 #endif
652
653 output_file_close (out_file_name);
654 } /* non vms output */
655 #else /* VMS */
656 /*
657 * Now do the VMS-dependent part of writing the object file
658 */
659 VMS_write_object_file (text_siz, data_siz, text_frag_root, data_frag_root);
660 #endif /* VMS */
661 } /* write_object_file() */
662
663 #else
664 #endif
665
666 /*
667 * relax_segment()
668 *
669 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
670 * values.
671 *
672 * Relax the frags.
673 *
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.
677 */
678
679
680
681 void
682 relax_segment (segment_frag_root, segment)
683 struct frag *segment_frag_root;
684 segT segment; /* SEG_DATA or SEG_TEXT */
685 {
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);
690 #endif
691 /* In case md_estimate_size_before_relax() wants to make fixSs. */
692 subseg_change (segment, 0);
693
694 /*
695 * For each frag in segment: count and store (a 1st guess of)
696 * fr_address.
697 */
698 address = 0;
699 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
700 {
701 fragP->fr_address = address;
702 address += fragP->fr_fix;
703
704 switch (fragP->fr_type)
705 {
706 case rs_fill:
707 address += fragP->fr_offset * fragP->fr_var;
708 break;
709
710 case rs_align:
711 address += relax_align (address, fragP->fr_offset);
712 break;
713
714 case rs_org:
715 /*
716 * Assume .org is nugatory. It will grow with 1st
717 * relax.
718 */
719 break;
720
721 case rs_machine_dependent:
722 address += md_estimate_size_before_relax (fragP, segment);
723 break;
724
725 #ifndef WORKING_DOT_WORD
726 /* Broken words don't concern us yet */
727 case rs_broken_word:
728 break;
729 #endif
730
731 default:
732 BAD_CASE (fragP->fr_type);
733 break;
734 } /* switch(fr_type) */
735 } /* for each frag in the segment */
736
737 /*
738 * Do relax().
739 */
740 {
741 register long stretch; /* May be any size, 0 or negative. */
742 /* Cumulative number of addresses we have */
743 /* relaxed this pass. */
744 /* We may have relaxed more than one address. */
745 register long stretched; /* Have we stretched on this pass? */
746 /* This is 'cuz stretch may be zero, when,
747 in fact some piece of code grew, and
748 another shrank. If a branch instruction
749 doesn't fit anymore, we could be scrod */
750
751 do
752 {
753 stretch = stretched = 0;
754 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
755 {
756 register long growth = 0;
757 register unsigned long was_address;
758 /* register long var; */
759 register long offset;
760 register symbolS *symbolP;
761 register long target;
762 register long after;
763 register long aim;
764
765 was_address = fragP->fr_address;
766 address = fragP->fr_address += stretch;
767 symbolP = fragP->fr_symbol;
768 offset = fragP->fr_offset;
769 /* var = fragP->fr_var; */
770
771 switch (fragP->fr_type)
772 {
773 case rs_fill: /* .fill never relaxes. */
774 growth = 0;
775 break;
776
777 #ifndef WORKING_DOT_WORD
778 /* JF: This is RMS's idea. I do *NOT* want to be blamed
779 for it I do not want to write it. I do not want to have
780 anything to do with it. This is not the proper way to
781 implement this misfeature. */
782 case rs_broken_word:
783 {
784 struct broken_word *lie;
785 struct broken_word *untruth;
786 extern int md_short_jump_size;
787 extern int md_long_jump_size;
788
789 /* Yes this is ugly (storing the broken_word pointer
790 in the symbol slot). Still, this whole chunk of
791 code is ugly, and I don't feel like doing anything
792 about it. Think of it as stubbornness in action */
793 growth = 0;
794 for (lie = (struct broken_word *) (fragP->fr_symbol);
795 lie && lie->dispfrag == fragP;
796 lie = lie->next_broken_word)
797 {
798
799 if (lie->added)
800 continue;
801
802 offset = lie->add->sy_frag->fr_address + S_GET_VALUE (lie->add) + lie->addnum -
803 (lie->sub->sy_frag->fr_address + S_GET_VALUE (lie->sub));
804 if (offset <= -32768 || offset >= 32767)
805 {
806 if (flagseen['K'])
807 as_warn (".word %s-%s+%ld didn't fit",
808 S_GET_NAME (lie->add),
809 S_GET_NAME (lie->sub),
810 lie->addnum);
811 lie->added = 1;
812 if (fragP->fr_subtype == 0)
813 {
814 fragP->fr_subtype++;
815 growth += md_short_jump_size;
816 }
817 for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == lie->dispfrag; untruth = untruth->next_broken_word)
818 if ((untruth->add->sy_frag == lie->add->sy_frag)
819 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
820 {
821 untruth->added = 2;
822 untruth->use_jump = lie;
823 }
824 growth += md_long_jump_size;
825 }
826 }
827
828 break;
829 } /* case rs_broken_word */
830 #endif
831 case rs_align:
832 growth = relax_align ((relax_addressT) (address + fragP->fr_fix), offset)
833 - relax_align ((relax_addressT) (was_address + fragP->fr_fix), offset);
834 break;
835
836 case rs_org:
837 target = offset;
838
839 if (symbolP)
840 {
841 #ifdef MANY_SEGMENTS
842 #else
843 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);
844 know (symbolP->sy_frag);
845 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) || (symbolP->sy_frag == &zero_address_frag));
846 #endif
847 target += S_GET_VALUE (symbolP)
848 + symbolP->sy_frag->fr_address;
849 } /* if we have a symbol */
850
851 know (fragP->fr_next);
852 after = fragP->fr_next->fr_address;
853 growth = ((target - after) > 0) ? (target - after) : 0;
854 /* Growth may be -ve, but variable part */
855 /* of frag cannot have < 0 chars. */
856 /* That is, we can't .org backwards. */
857
858 growth -= stretch; /* This is an absolute growth factor */
859 break;
860
861 case rs_machine_dependent:
862 {
863 register const relax_typeS *this_type;
864 register const relax_typeS *start_type;
865 register relax_substateT next_state;
866 register relax_substateT this_state;
867
868 start_type = this_type = md_relax_table + (this_state = fragP->fr_subtype);
869 target = offset;
870
871 if (symbolP)
872 {
873 #ifndef MANY_SEGMENTS
874 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));
875 #endif
876 know (symbolP->sy_frag);
877 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE) || symbolP->sy_frag == &zero_address_frag);
878 target +=
879 S_GET_VALUE (symbolP)
880 + symbolP->sy_frag->fr_address;
881
882 /* If frag has yet to be reached on this pass,
883 assume it will move by STRETCH just as we did.
884 If this is not so, it will be because some frag
885 between grows, and that will force another pass. */
886
887 /* JF was just address */
888 /* JF also added is_dnrange hack */
889 /* There's gotta be a better/faster/etc way
890 to do this. . . */
891 /* gnu@cygnus.com: I changed this from > to >=
892 because I ran into a zero-length frag (fr_fix=0)
893 which was created when the obstack needed a new
894 chunk JUST AFTER the opcode of a branch. Since
895 fr_fix is zero, fr_address of this frag is the same
896 as fr_address of the next frag. This
897 zero-length frag was variable and jumped to .+2
898 (in the next frag), but since the > comparison
899 below failed (the two were =, not >), "stretch"
900 was not added to the target. Stretch was 178, so
901 the offset appeared to be .-176 instead, which did
902 not fit into a byte branch, so the assembler
903 relaxed the branch to a word. This didn't compare
904 with what happened when the same source file was
905 assembled on other machines, which is how I found it.
906 You might want to think about what other places have
907 trouble with zero length frags... */
908
909 if (symbolP->sy_frag->fr_address >= was_address
910 && is_dnrange (fragP, symbolP->sy_frag))
911 {
912 target += stretch;
913 } /* */
914
915 } /* if there's a symbol attached */
916
917 aim = target - address - fragP->fr_fix;
918 /* The displacement is affected by the instruction size
919 * for the 32k architecture. I think we ought to be able
920 * to add fragP->fr_pcrel_adjust in all cases (it should be
921 * zero if not used), but just in case it breaks something
922 * else we'll put this inside #ifdef NS32K ... #endif
923 */
924 #ifdef TC_NS32K
925 aim += fragP->fr_pcrel_adjust;
926 #endif /* TC_NS32K */
927
928 if (aim < 0)
929 {
930 /* Look backwards. */
931 for (next_state = this_type->rlx_more; next_state;)
932 {
933 if (aim >= this_type->rlx_backward)
934 {
935 next_state = 0;
936 }
937 else
938 { /* Grow to next state. */
939 this_type = md_relax_table + (this_state = next_state);
940 next_state = this_type->rlx_more;
941 }
942 }
943 }
944 else
945 {
946 #ifdef DONTDEF
947 /* JF these next few lines of code are for the mc68020 which can't handle short
948 offsets of zero in branch instructions. What a kludge! */
949 if (aim == 0 && this_state == (1 << 2 + 0))
950 { /* FOO hard encoded from m.c */
951 aim = this_type->rlx_forward + 1; /* Force relaxation into word mode */
952 }
953 #endif
954 #ifdef M68K_AIM_KLUDGE
955 M68K_AIM_KLUDGE (aim, this_state, this_type);
956 #endif
957 /* JF end of 68020 code */
958 /* Look forwards. */
959 for (next_state = this_type->rlx_more; next_state;)
960 {
961 if (aim <= this_type->rlx_forward)
962 {
963 next_state = 0;
964 }
965 else
966 { /* Grow to next state. */
967 this_type = md_relax_table + (this_state = next_state);
968 next_state = this_type->rlx_more;
969 }
970 }
971 }
972
973 if ((growth = this_type->rlx_length - start_type->rlx_length) != 0)
974 fragP->fr_subtype = this_state;
975
976 break;
977 } /* case rs_machine_dependent */
978
979 default:
980 BAD_CASE (fragP->fr_type);
981 break;
982 }
983 if (growth)
984 {
985 stretch += growth;
986 stretched++;
987 }
988 } /* For each frag in the segment. */
989 }
990 while (stretched); /* Until nothing further to relax. */
991 } /* do_relax */
992
993 /*
994 * We now have valid fr_address'es for each frag.
995 */
996
997 /*
998 * All fr_address's are correct, relative to their own segment.
999 * We have made all the fixS we will ever make.
1000 */
1001 } /* relax_segment() */
1002
1003 /*
1004 * Relax_align. Advance location counter to next address that has 'alignment'
1005 * lowest order bits all 0s.
1006 */
1007
1008 /* How many addresses does the .align take? */
1009 static relax_addressT
1010 relax_align (address, alignment)
1011 register relax_addressT address; /* Address now. */
1012 register long alignment; /* Alignment (binary). */
1013 {
1014 relax_addressT mask;
1015 relax_addressT new_address;
1016
1017 mask = ~((~0) << alignment);
1018 new_address = (address + mask) & (~mask);
1019 if (linkrelax)
1020 /* We must provide lots of padding, so the linker can discard it
1021 when needed. The linker will not add extra space, ever. */
1022 new_address += (1 << alignment);
1023 return (new_address - address);
1024 } /* relax_align() */
1025
1026 /* fixup_segment()
1027
1028 Go through all the fixS's in a segment and see which ones can be
1029 handled now. (These consist of fixS where we have since discovered
1030 the value of a symbol, or the address of the frag involved.)
1031 For each one, call md_apply_fix to put the fix into the frag data.
1032
1033 Result is a count of how many relocation structs will be needed to
1034 handle the remaining fixS's that we couldn't completely handle here.
1035 These will be output later by emit_relocations(). */
1036
1037 static long
1038 fixup_segment (fixP, this_segment_type)
1039 register fixS *fixP;
1040 segT this_segment_type; /* N_TYPE bits for segment. */
1041 {
1042 register long seg_reloc_count;
1043 register symbolS *add_symbolP;
1044 register symbolS *sub_symbolP;
1045 register long add_number;
1046 register int size;
1047 register char *place;
1048 register long where;
1049 register char pcrel;
1050 register fragS *fragP;
1051 register segT add_symbol_segment = SEG_ABSOLUTE;
1052
1053 /* FIXME: remove this line *//* fixS *orig = fixP; */
1054 seg_reloc_count = 0;
1055 #ifdef TC_I960
1056 /* If the linker is doing the relaxing, we must not do any fixups */
1057 if (linkrelax)
1058 {
1059 for (; fixP; fixP = fixP->fx_next)
1060 {
1061 seg_reloc_count++;
1062 }
1063 }
1064 else
1065 #endif
1066 for (; fixP; fixP = fixP->fx_next)
1067 {
1068 fragP = fixP->fx_frag;
1069 know (fragP);
1070 where = fixP->fx_where;
1071 place = fragP->fr_literal + where;
1072 size = fixP->fx_size;
1073 add_symbolP = fixP->fx_addsy;
1074 #ifdef TC_I960
1075 if (fixP->fx_callj && TC_S_IS_CALLNAME (add_symbolP))
1076 {
1077 /* Relocation should be done via the
1078 associated 'bal' entry point
1079 symbol. */
1080
1081 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP)))
1082 {
1083 as_bad ("No 'bal' entry point for leafproc %s",
1084 S_GET_NAME (add_symbolP));
1085 continue;
1086 }
1087 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
1088 } /* callj relocation */
1089 #endif
1090 sub_symbolP = fixP->fx_subsy;
1091 add_number = fixP->fx_offset;
1092 pcrel = fixP->fx_pcrel;
1093
1094 if (add_symbolP)
1095 {
1096 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
1097 } /* if there is an addend */
1098
1099 if (sub_symbolP)
1100 {
1101 if (!add_symbolP)
1102 {
1103 /* Its just -sym */
1104 if (S_GET_SEGMENT (sub_symbolP) != SEG_ABSOLUTE)
1105 {
1106 as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP));
1107 } /* not absolute */
1108
1109 add_number -= S_GET_VALUE (sub_symbolP);
1110
1111 /* if sub_symbol is in the same segment that add_symbol
1112 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
1113 }
1114 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
1115 && (SEG_NORMAL (add_symbol_segment)
1116 || (add_symbol_segment == SEG_ABSOLUTE)))
1117 {
1118 /* Difference of 2 symbols from same segment. */
1119 /* Can't make difference of 2 undefineds: 'value' means */
1120 /* something different for N_UNDF. */
1121 #ifdef TC_I960
1122 /* Makes no sense to use the difference of 2 arbitrary symbols
1123 * as the target of a call instruction.
1124 */
1125 if (fixP->fx_callj)
1126 {
1127 as_bad ("callj to difference of 2 symbols");
1128 }
1129 #endif /* TC_I960 */
1130 add_number += S_GET_VALUE (add_symbolP) -
1131 S_GET_VALUE (sub_symbolP);
1132
1133 add_symbolP = NULL;
1134 fixP->fx_addsy = NULL;
1135 }
1136 else
1137 {
1138 /* Different segments in subtraction. */
1139 know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE)));
1140
1141 if ((S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE))
1142 {
1143 add_number -= S_GET_VALUE (sub_symbolP);
1144 }
1145 else
1146 {
1147 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
1148 segment_name (S_GET_SEGMENT (sub_symbolP)),
1149 S_GET_NAME (sub_symbolP), fragP->fr_address + where);
1150 } /* if absolute */
1151 }
1152 } /* if sub_symbolP */
1153
1154 if (add_symbolP)
1155 {
1156 if (add_symbol_segment == this_segment_type && pcrel)
1157 {
1158 /*
1159 * This fixup was made when the symbol's segment was
1160 * SEG_UNKNOWN, but it is now in the local segment.
1161 * So we know how to do the address without relocation.
1162 */
1163 #ifdef TC_I960
1164 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
1165 * in which cases it modifies *fixP as appropriate. In the case
1166 * of a 'calls', no further work is required, and *fixP has been
1167 * set up to make the rest of the code below a no-op.
1168 */
1169 reloc_callj (fixP);
1170 #endif /* TC_I960 */
1171
1172 add_number += S_GET_VALUE (add_symbolP);
1173 add_number -= md_pcrel_from (fixP);
1174 pcrel = 0; /* Lie. Don't want further pcrel processing. */
1175 fixP->fx_addsy = NULL; /* No relocations please. */
1176 }
1177 else
1178 {
1179 switch (add_symbol_segment)
1180 {
1181 case SEG_ABSOLUTE:
1182 #ifdef TC_I960
1183 reloc_callj (fixP); /* See comment about reloc_callj() above*/
1184 #endif /* TC_I960 */
1185 add_number += S_GET_VALUE (add_symbolP);
1186 fixP->fx_addsy = NULL;
1187 add_symbolP = NULL;
1188 break;
1189 default:
1190 seg_reloc_count++;
1191 add_number += S_GET_VALUE (add_symbolP);
1192 break;
1193
1194 case SEG_UNKNOWN:
1195 #ifdef TC_I960
1196 if ((int) fixP->fx_bit_fixP == 13)
1197 {
1198 /* This is a COBR instruction. They have only a
1199 * 13-bit displacement and are only to be used
1200 * for local branches: flag as error, don't generate
1201 * relocation.
1202 */
1203 as_bad ("can't use COBR format with external label");
1204 fixP->fx_addsy = NULL; /* No relocations please. */
1205 continue;
1206 } /* COBR */
1207 #endif /* TC_I960 */
1208
1209 #ifdef OBJ_COFF
1210 #ifdef TE_I386AIX
1211 if (S_IS_COMMON (add_symbolP))
1212 add_number += S_GET_VALUE (add_symbolP);
1213 #endif /* TE_I386AIX */
1214 #endif /* OBJ_COFF */
1215 ++seg_reloc_count;
1216
1217 break;
1218
1219
1220 } /* switch on symbol seg */
1221 } /* if not in local seg */
1222 } /* if there was a + symbol */
1223
1224 if (pcrel)
1225 {
1226 add_number -= md_pcrel_from (fixP);
1227 if (add_symbolP == 0)
1228 {
1229 fixP->fx_addsy = &abs_symbol;
1230 ++seg_reloc_count;
1231 } /* if there's an add_symbol */
1232 } /* if pcrel */
1233
1234 if (!fixP->fx_bit_fixP)
1235 {
1236 if ((size == 1 &&
1237 (add_number & ~0xFF) && ((add_number & ~0xFF) != (-1 & ~0xFF))) ||
1238 (size == 2 &&
1239 (add_number & ~0xFFFF) && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
1240 {
1241 as_bad ("Value of %d too large for field of %d bytes at 0x%x",
1242 add_number, size, fragP->fr_address + where);
1243 } /* generic error checking */
1244 #ifdef WARN_SIGNED_OVERFLOW_WORD
1245 /* Warn if a .word value is too large when
1246 treated as a signed number. We already
1247 know it is not too negative. This is to
1248 catch over-large switches generated by gcc
1249 on the 68k. */
1250 if (!flagseen['J']
1251 && size == 2
1252 && add_number > 0x7fff)
1253 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
1254 add_number, fragP->fr_address + where);
1255 #endif
1256 } /* not a bit fix */
1257
1258 md_apply_fix (fixP, add_number);
1259 } /* For each fixS in this segment. */
1260
1261 #ifdef OBJ_COFF
1262 #ifdef TC_I960
1263 {
1264 fixS *topP = fixP;
1265
1266 /* two relocs per callj under coff. */
1267 for (fixP = topP; fixP; fixP = fixP->fx_next)
1268 {
1269 if (fixP->fx_callj && fixP->fx_addsy != 0)
1270 {
1271 ++seg_reloc_count;
1272 } /* if callj and not already fixed. */
1273 } /* for each fix */
1274 }
1275 #endif /* TC_I960 */
1276
1277 #endif /* OBJ_COFF */
1278 return (seg_reloc_count);
1279 } /* fixup_segment() */
1280
1281
1282 static int
1283 is_dnrange (f1, f2)
1284 struct frag *f1;
1285 struct frag *f2;
1286 {
1287 while (f1)
1288 {
1289 if (f1->fr_next == f2)
1290 return 1;
1291 f1 = f1->fr_next;
1292 }
1293 return 0;
1294 } /* is_dnrange() */
1295
1296 /* Append a string onto another string, bumping the pointer along. */
1297 void
1298 append (charPP, fromP, length)
1299 char **charPP;
1300 char *fromP;
1301 unsigned long length;
1302 {
1303 if (length)
1304 { /* Don't trust memcpy() of 0 chars. */
1305 memcpy (*charPP, fromP, (int) length);
1306 *charPP += length;
1307 }
1308 }
1309
1310 int section_alignment[SEG_MAXIMUM_ORDINAL];
1311
1312 /*
1313 * This routine records the largest alignment seen for each segment.
1314 * If the beginning of the segment is aligned on the worst-case
1315 * boundary, all of the other alignments within it will work. At
1316 * least one object format really uses this info.
1317 */
1318 void
1319 record_alignment (seg, align)
1320 segT seg; /* Segment to which alignment pertains */
1321 int align; /* Alignment, as a power of 2
1322 * (e.g., 1 => 2-byte boundary, 2 => 4-byte boundary, etc.)
1323 */
1324 {
1325
1326 if (align > section_alignment[(int) seg])
1327 {
1328 section_alignment[(int) seg] = align;
1329 } /* if highest yet */
1330
1331 return;
1332 } /* record_alignment() */
1333
1334 /*
1335 * Local Variables:
1336 * comment-column: 0
1337 * fill-column: 131
1338 * End:
1339 */
1340
1341 /* end of write.c */
This page took 0.072782 seconds and 5 git commands to generate.