* subsegs.c (subsegs_begin): create bss0_frchainP in the same was
[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 static struct frag *text_frag_root;
38 static struct frag *data_frag_root;
39 static struct frag *bss_frag_root;
40
41 static struct frag *text_last_frag; /* Last frag in segment. */
42 static 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 long length; JF unused */ /* String length, including trailing '\0'. */
57
58
59 #if __STDC__ == 1
60
61 static int is_dnrange(struct frag *f1, struct frag *f2);
62 static long fixup_segment(fixS *fixP, segT this_segment_type);
63 static relax_addressT relax_align(relax_addressT address, long alignment);
64 void relax_segment(struct frag *segment_frag_root, segT segment_type);
65
66 #else
67
68 static int is_dnrange();
69 static long fixup_segment();
70 static relax_addressT relax_align();
71 void relax_segment();
72
73 #endif /* not __STDC__ */
74
75 /*
76 * fix_new()
77 *
78 * Create a fixS in obstack 'notes'.
79 */
80 fixS *fix_new(frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type)
81 fragS *frag; /* Which frag? */
82 int where; /* Where in that frag? */
83 short int size; /* 1, 2, or 4 usually. */
84 symbolS *add_symbol; /* X_add_symbol. */
85 symbolS *sub_symbol; /* X_subtract_symbol. */
86 long offset; /* X_add_number. */
87 int pcrel; /* TRUE if PC-relative relocation. */
88 int r_type; /* Relocation type */
89 {
90 fixS *fixP;
91
92 fixP = (fixS *) obstack_alloc(&notes, sizeof(fixS));
93
94 fixP->fx_frag = frag;
95 fixP->fx_where = where;
96 fixP->fx_size = size;
97 fixP->fx_addsy = add_symbol;
98 fixP->fx_subsy = sub_symbol;
99 fixP->fx_offset = offset;
100 fixP->fx_pcrel = pcrel;
101 #if defined(TC_SPARC) || defined(TC_A29K) || defined( NEED_FX_R_TYPE)
102 fixP->fx_r_type = r_type;
103 #endif
104 /* JF these 'cuz of the NS32K stuff */
105 fixP->fx_im_disp = 0;
106 fixP->fx_pcrel_adjust = 0;
107 fixP->fx_bsr = 0;
108 fixP->fx_bit_fixP = 0;
109
110 /* usually, we want relocs sorted numerically, but while
111 comparing to older versions of gas that have relocs
112 reverse sorted, it is convenient to have this compile
113 time option. xoxorich. */
114
115 #ifdef REVERSE_SORT_RELOCS
116
117 fixP->fx_next = *seg_fix_rootP;
118 *seg_fix_rootP = fixP;
119
120 #else /* REVERSE_SORT_RELOCS */
121
122 fixP->fx_next = NULL;
123
124 if (*seg_fix_tailP)
125 (*seg_fix_tailP)->fx_next = fixP;
126 else
127 *seg_fix_rootP = fixP;
128 *seg_fix_tailP = fixP;
129
130 #endif /* REVERSE_SORT_RELOCS */
131
132 fixP->fx_callj = 0;
133 return(fixP);
134 } /* fix_new() */
135
136 #ifndef BFD
137
138 void remove_subsegs(head, seg, root, last)
139 frchainS *head;
140 int seg;
141 fragS **root;
142 fragS ** last;
143 {
144 fragS dummy;
145 fragS * prev_frag = &dummy;
146 *root = head->frch_root;
147 while (head && head->frch_seg == seg)
148 {
149 prev_frag->fr_next = head->frch_root;
150
151 prev_frag = head->frch_last;
152 head = head->frch_next;
153 }
154 *last = prev_frag;
155 prev_frag->fr_next = 0;
156 }
157 void write_object_file()
158 {
159 register struct frchain * frchainP; /* Track along all frchains. */
160 register fragS * fragP; /* Track along all frags. */
161 register struct frchain * next_frchainP;
162 register fragS * * prev_fragPP;
163 /* register char * name; */
164 /* symbolS *symbolP; */
165 /* register symbolS ** symbolPP; */
166 /* register fixS * fixP; JF unused */
167 unsigned int data_siz;
168
169 long object_file_size;
170
171 #ifdef VMS
172 /*
173 * Under VMS we try to be compatible with VAX-11 "C". Thus, we
174 * call a routine to check for the definition of the procedure
175 * "_main", and if so -- fix it up so that it can be program
176 * entry point.
177 */
178 VMS_Check_For_Main();
179 #endif /* VMS */
180 /*
181 * After every sub-segment, we fake an ".align ...". This conforms to BSD4.2
182 * brane-damage. We then fake ".fill 0" because that is the kind of frag
183 * that requires least thought. ".align" frags like to have a following
184 * frag since that makes calculating their intended length trivial.
185 */
186 #define SUB_SEGMENT_ALIGN (2)
187 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next) {
188 #ifdef VMS
189 /*
190 * Under VAX/VMS, the linker (and PSECT specifications)
191 * take care of correctly aligning the segments.
192 * Doing the alignment here (on initialized data) can
193 * mess up the calculation of global data PSECT sizes.
194 */
195 #undef SUB_SEGMENT_ALIGN
196 #define SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0)
197 #endif /* VMS */
198 subseg_new (frchainP->frch_seg, frchainP->frch_subseg);
199 frag_align (SUB_SEGMENT_ALIGN, NOP_OPCODE);
200 /* frag_align will have left a new frag. */
201 /* Use this last frag for an empty ".fill". */
202 /*
203 * For this segment ...
204 * Create a last frag. Do not leave a "being filled in frag".
205 */
206 frag_wane (frag_now);
207 frag_now->fr_fix = 0;
208 know( frag_now->fr_next == NULL );
209 /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
210 /* Above shows we haven't left a half-completed object on obstack. */
211 } /* walk the frag chain */
212
213 /*
214 * From now on, we don't care about sub-segments.
215 * Build one frag chain for each segment. Linked thru fr_next.
216 * We know that there is at least 1 text frchain & at least 1 data frchain.
217 */
218
219 remove_subsegs(frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
220 remove_subsegs(data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
221 remove_subsegs(bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
222
223 /*
224 * We have two segments. If user gave -R flag, then we must put the
225 * data frags into the text segment. Do this before relaxing so
226 * we know to take advantage of -R and make shorter addresses.
227 */
228 #ifndef OBJ_AOUT
229 if (flagseen[ 'R' ]) {
230 fixS *tmp;
231
232 text_last_frag->fr_next = data_frag_root;
233 text_last_frag = data_last_frag;
234 data_last_frag = NULL;
235 data_frag_root = NULL;
236 if (text_fix_root) {
237 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next) ;;
238 tmp->fx_next=data_fix_root;
239 text_fix_tail = data_fix_tail;
240 } else
241 text_fix_root=data_fix_root;
242 data_fix_root=NULL;
243 }
244 #endif
245 relax_segment(text_frag_root, SEG_TEXT);
246 relax_segment(data_frag_root, SEG_DATA);
247 relax_segment(bss_frag_root, SEG_BSS);
248 /*
249 * Now the addresses of frags are correct within the segment.
250 */
251
252 know(text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
253 H_SET_TEXT_SIZE(&headers, text_last_frag->fr_address);
254 text_last_frag->fr_address = H_GET_TEXT_SIZE(&headers);
255
256 /*
257 * Join the 2 segments into 1 huge segment.
258 * To do this, re-compute every rn_address in the SEG_DATA frags.
259 * Then join the data frags after the text frags.
260 *
261 * Determine a_data [length of data segment].
262 */
263 if (data_frag_root) {
264 register relax_addressT slide;
265
266 know((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
267
268 H_SET_DATA_SIZE(&headers, data_last_frag->fr_address);
269 data_last_frag->fr_address = H_GET_DATA_SIZE(&headers);
270 slide = H_GET_TEXT_SIZE(&headers); /* & in file of the data segment. */
271 #ifdef OBJ_BOUT
272 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
273 /* For b.out: If the data section has a strict alignment
274 requirement, its load address in the .o file will be
275 rounded up from the size of the text section. These
276 two values are *not* the same! Similarly for the bss
277 section.... */
278 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
279 #endif
280
281 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next) {
282 fragP->fr_address += slide;
283 } /* for each data frag */
284
285 know(text_last_frag != 0);
286 text_last_frag->fr_next = data_frag_root;
287 } else {
288 H_SET_DATA_SIZE(&headers,0);
289 data_siz = 0;
290 }
291
292 #ifdef OBJ_BOUT
293 /* See above comments on b.out data section address. */
294 {
295 long bss_vma;
296 if (data_last_frag == 0)
297 bss_vma = H_GET_TEXT_SIZE (&headers);
298 else
299 bss_vma = data_last_frag->fr_address;
300 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
301 bss_address_frag.fr_address = bss_vma;
302 }
303 #else
304 bss_address_frag.fr_address = (H_GET_TEXT_SIZE(&headers) +
305 H_GET_DATA_SIZE(&headers));
306
307 /* Slide all the frags */
308 if (bss_frag_root)
309 {
310 relax_addressT slide = bss_address_frag.fr_address + local_bss_counter;
311
312 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
313 {
314 fragP->fr_address += slide;
315 } /* for each bss frag */
316 }
317
318 #endif
319 H_SET_BSS_SIZE(&headers,local_bss_counter + bss_last_frag ? bss_last_frag->fr_address: 0);
320
321 /*
322 *
323 * Crawl the symbol chain.
324 *
325 * For each symbol whose value depends on a frag, take the address of
326 * that frag and subsume it into the value of the symbol.
327 * After this, there is just one way to lookup a symbol value.
328 * Values are left in their final state for object file emission.
329 * We adjust the values of 'L' local symbols, even if we do
330 * not intend to emit them to the object file, because their values
331 * are needed for fix-ups.
332 *
333 * Unless we saw a -L flag, remove all symbols that begin with 'L'
334 * from the symbol chain. (They are still pointed to by the fixes.)
335 *
336 * Count the remaining symbols.
337 * Assign a symbol number to each symbol.
338 * Count the number of string-table chars we will emit.
339 * Put this info into the headers as appropriate.
340 *
341 */
342 know(zero_address_frag.fr_address == 0);
343 string_byte_count = sizeof(string_byte_count);
344
345 obj_crawl_symbol_chain(&headers);
346
347 if (string_byte_count == sizeof(string_byte_count)) {
348 string_byte_count = 0;
349 } /* if no strings, then no count. */
350
351 H_SET_STRING_SIZE(&headers, string_byte_count);
352
353 /*
354 * Addresses of frags now reflect addresses we use in the object file.
355 * Symbol values are correct.
356 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
357 * Also converting any machine-dependent frags using md_convert_frag();
358 */
359 subseg_change(SEG_TEXT, 0);
360
361 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next) {
362 switch (fragP->fr_type) {
363 case rs_align:
364 case rs_org:
365 fragP->fr_type = rs_fill;
366 know(fragP->fr_var == 1);
367 know(fragP->fr_next != NULL);
368
369 fragP->fr_offset = (fragP->fr_next->fr_address
370 - fragP->fr_address
371 - fragP->fr_fix);
372 break;
373
374 case rs_fill:
375 break;
376
377 case rs_machine_dependent:
378 md_convert_frag(&headers, fragP);
379
380 know((fragP->fr_next == NULL) || ((fragP->fr_next->fr_address - fragP->fr_address) == fragP->fr_fix));
381
382 /*
383 * After md_convert_frag, we make the frag into a ".space 0".
384 * Md_convert_frag() should set up any fixSs and constants
385 * required.
386 */
387 frag_wane(fragP);
388 break;
389
390 #ifndef WORKING_DOT_WORD
391 case rs_broken_word: {
392 struct broken_word *lie;
393 extern md_short_jump_size;
394 extern md_long_jump_size;
395
396 if (fragP->fr_subtype) {
397 fragP->fr_fix+=md_short_jump_size;
398 for (lie=(struct broken_word *)(fragP->fr_symbol);lie && lie->dispfrag==fragP;lie=lie->next_broken_word)
399 if (lie->added==1)
400 fragP->fr_fix+=md_long_jump_size;
401 }
402 frag_wane(fragP);
403 }
404 break;
405 #endif
406
407 default:
408 BAD_CASE( fragP->fr_type );
409 break;
410 } /* switch (fr_type) */
411
412 if (!((fragP->fr_next == NULL)
413 #ifdef OBJ_BOUT
414 || (fragP->fr_next == data_frag_root)
415 #endif
416 || ((fragP->fr_next->fr_address - fragP->fr_address)
417 == (fragP->fr_fix + (fragP->fr_offset * fragP->fr_var)))))
418 {
419 fprintf (stderr, "assertion failed: file `%s', line %d\n",
420 __FILE__, __LINE__ - 4);
421 exit (1);
422 }
423 } /* for each frag. */
424
425 #ifndef WORKING_DOT_WORD
426 {
427 struct broken_word *lie;
428 struct broken_word **prevP;
429
430 prevP= &broken_words;
431 for (lie=broken_words; lie; lie=lie->next_broken_word)
432 if (!lie->added) {
433 #ifdef TC_NS32K
434 fix_new_ns32k(lie->frag,
435 lie->word_goes_here - lie->frag->fr_literal,
436 2,
437 lie->add,
438 lie->sub,
439 lie->addnum,
440 0, 0, 2, 0, 0);
441 #else
442 # if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
443 fix_new( lie->frag, lie->word_goes_here - lie->frag->fr_literal,
444 2, lie->add,
445 lie->sub, lie->addnum,
446 0, NO_RELOC);
447 # else
448 fix_new( lie->frag, lie->word_goes_here - lie->frag->fr_literal,
449 2, lie->add,
450 lie->sub, lie->addnum,
451 0, 0);
452
453 # endif /* tc_sparc|tc_a29k|need_fx_r_type */
454 #endif /* TC_NS32K */
455 /* md_number_to_chars(lie->word_goes_here,
456 S_GET_VALUE(lie->add)
457 + lie->addnum
458 - S_GET_VALUE(lie->sub),
459 2); */
460 *prevP=lie->next_broken_word;
461 } else
462 prevP= &(lie->next_broken_word);
463
464 for (lie=broken_words;lie;) {
465 struct broken_word *untruth;
466 char *table_ptr;
467 long table_addr;
468 long from_addr,
469 to_addr;
470 int n,
471 m;
472
473 extern md_short_jump_size;
474 extern md_long_jump_size;
475
476 fragP=lie->dispfrag;
477
478 /* Find out how many broken_words go here */
479 n=0;
480 for (untruth=lie;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word)
481 if (untruth->added==1)
482 n++;
483
484 table_ptr=lie->dispfrag->fr_opcode;
485 table_addr=lie->dispfrag->fr_address+(table_ptr - lie->dispfrag->fr_literal);
486 /* Create the jump around the long jumps */
487 /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */
488 from_addr=table_addr;
489 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
490 md_create_short_jump(table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
491 table_ptr+=md_short_jump_size;
492 table_addr+=md_short_jump_size;
493
494 for (m=0;lie && lie->dispfrag==fragP;m++,lie=lie->next_broken_word) {
495 if (lie->added==2)
496 continue;
497 /* Patch the jump table */
498 /* This is the offset from ??? to table_ptr+0 */
499 to_addr = table_addr
500 - S_GET_VALUE(lie->sub);
501 md_number_to_chars(lie->word_goes_here,to_addr,2);
502 for (untruth=lie->next_broken_word;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word) {
503 if (untruth->use_jump==lie)
504 md_number_to_chars(untruth->word_goes_here,to_addr,2);
505 }
506
507 /* Install the long jump */
508 /* this is a long jump from table_ptr+0 to the final target */
509 from_addr=table_addr;
510 to_addr=S_GET_VALUE(lie->add) + lie->addnum;
511 md_create_long_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
512 table_ptr+=md_long_jump_size;
513 table_addr+=md_long_jump_size;
514 }
515 }
516 }
517 #endif /* not WORKING_DOT_WORD */
518
519 #ifndef VMS
520 { /* not vms */
521 /*
522 * Scan every FixS performing fixups. We had to wait until now to do
523 * this because md_convert_frag() may have made some fixSs.
524 */
525
526 H_SET_RELOCATION_SIZE(&headers,
527 md_reloc_size * fixup_segment(text_fix_root, SEG_TEXT),
528 md_reloc_size * fixup_segment(data_fix_root, SEG_DATA));
529
530
531 /* FIXME move this stuff into the pre-write-hook */
532 H_SET_MAGIC_NUMBER(&headers, magic_number_for_object_file);
533 H_SET_ENTRY_POINT(&headers, 0);
534
535 obj_pre_write_hook(&headers); /* extra coff stuff */
536 if ((had_warnings() && flagseen['Z'])
537 || had_errors() > 0) {
538 if (flagseen['Z']) {
539 as_warn("%d error%s, %d warning%s, generating bad object file.\n",
540 had_errors(), had_errors() == 1 ? "" : "s",
541 had_warnings(), had_warnings() == 1 ? "" : "s");
542 } else {
543 as_fatal("%d error%s, %d warning%s, no object file generated.\n",
544 had_errors(), had_errors() == 1 ? "" : "s",
545 had_warnings(), had_warnings() == 1 ? "" : "s");
546 } /* on want output */
547 } /* on error condition */
548
549 object_file_size = H_GET_FILE_SIZE(&headers);
550 next_object_file_charP = the_object_file = xmalloc(object_file_size);
551
552 output_file_create(out_file_name);
553
554 obj_header_append(&next_object_file_charP, &headers);
555
556 know((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE(&headers));
557
558 /*
559 * Emit code.
560 */
561 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next) {
562 register long count;
563 register char *fill_literal;
564 register long fill_size;
565
566 know(fragP->fr_type == rs_fill);
567 append(&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
568 fill_literal = fragP->fr_literal + fragP->fr_fix;
569 fill_size = fragP->fr_var;
570 know(fragP->fr_offset >= 0);
571
572 for (count = fragP->fr_offset; count; count--) {
573 append(&next_object_file_charP, fill_literal, (unsigned long) fill_size);
574 } /* for each */
575
576 } /* for each code frag. */
577
578 know((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE(&headers) + H_GET_TEXT_SIZE(&headers) + H_GET_DATA_SIZE(&headers)));
579
580 /*
581 * Emit relocations.
582 */
583 obj_emit_relocations(&next_object_file_charP, text_fix_root, (relax_addressT)0);
584 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)));
585 #ifdef TC_I960
586 /* Make addresses in data relocation directives relative to beginning of
587 * first data fragment, not end of last text fragment: alignment of the
588 * start of the data segment may place a gap between the segments.
589 */
590 obj_emit_relocations(&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
591 #else /* TC_I960 */
592 obj_emit_relocations(&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
593 #endif /* TC_I960 */
594
595 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)));
596
597 /*
598 * Emit line number entries.
599 */
600 OBJ_EMIT_LINENO(&next_object_file_charP, lineno_rootP, the_object_file);
601 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)));
602
603 /*
604 * Emit symbols.
605 */
606 obj_emit_symbols(&next_object_file_charP, symbol_rootP);
607 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)));
608
609 /*
610 * Emit strings.
611 */
612
613 if (string_byte_count > 0) {
614 obj_emit_strings(&next_object_file_charP);
615 } /* only if we have a string table */
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) + H_GET_LINENO_SIZE(&headers) + H_GET_SYMBOL_TABLE_SIZE(&headers) + H_GET_STRING_SIZE(&headers)));
618 */
619 /* know(next_object_file_charP == the_object_file + object_file_size);*/
620
621 #ifdef BFD_HEADERS
622 bfd_seek(stdoutput, 0, 0);
623 bfd_write(the_object_file, 1, object_file_size, stdoutput);
624 #else
625
626 /* Write the data to the file */
627 output_file_append(the_object_file,object_file_size,out_file_name);
628 #endif
629
630 output_file_close(out_file_name);
631 } /* non vms output */
632 #else /* VMS */
633 /*
634 * Now do the VMS-dependent part of writing the object file
635 */
636 VMS_write_object_file(text_siz, data_siz, text_frag_root, data_frag_root);
637 #endif /* VMS */
638 } /* write_object_file() */
639 #else
640 #endif
641
642 /*
643 * relax_segment()
644 *
645 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
646 * values.
647 *
648 * Relax the frags.
649 *
650 * After this, all frags in this segment have addresses that are correct
651 * within the segment. Since segments live in different file addresses,
652 * these frag addresses may not be the same as final object-file addresses.
653 */
654
655
656
657 void relax_segment(segment_frag_root, segment)
658 struct frag * segment_frag_root;
659 segT segment; /* SEG_DATA or SEG_TEXT */
660 {
661 register struct frag * fragP;
662 register relax_addressT address;
663 /* register relax_addressT old_address; JF unused */
664 /* register relax_addressT new_address; JF unused */
665 #ifndef MANY_SEGMENTS
666 know(segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS) ;
667 #endif
668 /* In case md_estimate_size_before_relax() wants to make fixSs. */
669 subseg_change(segment, 0);
670
671 /*
672 * For each frag in segment: count and store (a 1st guess of) fr_address.
673 */
674 address = 0;
675 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) {
676 fragP->fr_address = address;
677 address += fragP->fr_fix;
678
679 switch (fragP->fr_type) {
680 case rs_fill:
681 address += fragP->fr_offset * fragP->fr_var ;
682 break;
683
684 case rs_align:
685 address += relax_align(address, fragP->fr_offset);
686 break;
687
688 case rs_org:
689 /*
690 * Assume .org is nugatory. It will grow with 1st relax.
691 */
692 break;
693
694 case rs_machine_dependent:
695 address += md_estimate_size_before_relax(fragP, segment);
696 break;
697
698 #ifndef WORKING_DOT_WORD
699 /* Broken words don't concern us yet */
700 case rs_broken_word:
701 break;
702 #endif
703
704 default:
705 BAD_CASE(fragP->fr_type);
706 break;
707 } /* switch(fr_type) */
708 } /* for each frag in the segment */
709
710 /*
711 * Do relax().
712 */
713 {
714 register long stretch; /* May be any size, 0 or negative. */
715 /* Cumulative number of addresses we have */
716 /* relaxed this pass. */
717 /* We may have relaxed more than one address. */
718 register long stretched; /* Have we stretched on this pass? */
719 /* This is 'cuz stretch may be zero, when,
720 in fact some piece of code grew, and
721 another shrank. If a branch instruction
722 doesn't fit anymore, we could be scrod */
723
724 do {
725 stretch = stretched = 0;
726 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) {
727 register long growth = 0;
728 register unsigned long was_address;
729 /* register long var; */
730 register long offset;
731 register symbolS *symbolP;
732 register long target;
733 register long after;
734 register long aim;
735
736 was_address = fragP->fr_address;
737 address = fragP->fr_address += stretch;
738 symbolP = fragP->fr_symbol;
739 offset = fragP->fr_offset;
740 /* var = fragP->fr_var; */
741
742 switch (fragP->fr_type) {
743 case rs_fill: /* .fill never relaxes. */
744 growth = 0;
745 break;
746
747 #ifndef WORKING_DOT_WORD
748 /* JF: This is RMS's idea. I do *NOT* want to be blamed
749 for it I do not want to write it. I do not want to have
750 anything to do with it. This is not the proper way to
751 implement this misfeature. */
752 case rs_broken_word: {
753 struct broken_word *lie;
754 struct broken_word *untruth;
755 extern int md_short_jump_size;
756 extern int md_long_jump_size;
757
758 /* Yes this is ugly (storing the broken_word pointer
759 in the symbol slot). Still, this whole chunk of
760 code is ugly, and I don't feel like doing anything
761 about it. Think of it as stubbornness in action */
762 growth=0;
763 for (lie=(struct broken_word *)(fragP->fr_symbol);
764 lie && lie->dispfrag==fragP;
765 lie=lie->next_broken_word) {
766
767 if (lie->added)
768 continue;
769
770 offset= lie->add->sy_frag->fr_address+ S_GET_VALUE(lie->add) + lie->addnum -
771 (lie->sub->sy_frag->fr_address+ S_GET_VALUE(lie->sub));
772 if (offset<=-32768 || offset>=32767) {
773 if (flagseen['K'])
774 as_warn(".word %s-%s+%ld didn't fit",
775 S_GET_NAME(lie->add),
776 S_GET_NAME(lie->sub),
777 lie->addnum);
778 lie->added=1;
779 if (fragP->fr_subtype==0) {
780 fragP->fr_subtype++;
781 growth+=md_short_jump_size;
782 }
783 for (untruth=lie->next_broken_word;untruth && untruth->dispfrag==lie->dispfrag;untruth=untruth->next_broken_word)
784 if ((untruth->add->sy_frag == lie->add->sy_frag)
785 && S_GET_VALUE(untruth->add) == S_GET_VALUE(lie->add)) {
786 untruth->added=2;
787 untruth->use_jump=lie;
788 }
789 growth+=md_long_jump_size;
790 }
791 }
792
793 break;
794 } /* case rs_broken_word */
795 #endif
796 case rs_align:
797 growth = relax_align((relax_addressT) (address + fragP->fr_fix), offset)
798 - relax_align((relax_addressT) (was_address + fragP->fr_fix), offset);
799 break;
800
801 case rs_org:
802 target = offset;
803
804 if (symbolP) {
805 #ifdef MANY_SEGMENTS
806 #else
807 know((S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) || (S_GET_SEGMENT(symbolP) == SEG_DATA) || (S_GET_SEGMENT(symbolP) == SEG_TEXT));
808 know(symbolP->sy_frag);
809 know(!(S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) || (symbolP->sy_frag == &zero_address_frag));
810 #endif
811 target += S_GET_VALUE(symbolP)
812 + symbolP->sy_frag->fr_address;
813 } /* if we have a symbol */
814
815 know(fragP->fr_next);
816 after = fragP->fr_next->fr_address;
817 growth = ((target - after ) > 0) ? (target - after) : 0;
818 /* Growth may be -ve, but variable part */
819 /* of frag cannot have < 0 chars. */
820 /* That is, we can't .org backwards. */
821
822 growth -= stretch; /* This is an absolute growth factor */
823 break;
824
825 case rs_machine_dependent: {
826 register const relax_typeS * this_type;
827 register const relax_typeS * start_type;
828 register relax_substateT next_state;
829 register relax_substateT this_state;
830
831 start_type = this_type = md_relax_table + (this_state = fragP->fr_subtype);
832 target = offset;
833
834 if (symbolP) {
835 #ifndef MANY_SEGMENTS
836 know((S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) || (S_GET_SEGMENT(symbolP) == SEG_DATA) || (S_GET_SEGMENT(symbolP) == SEG_TEXT));
837 #endif
838 know(symbolP->sy_frag);
839 know(!(S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) || symbolP->sy_frag==&zero_address_frag );
840 target +=
841 S_GET_VALUE(symbolP)
842 + symbolP->sy_frag->fr_address;
843
844 /* If frag has yet to be reached on this pass,
845 assume it will move by STRETCH just as we did.
846 If this is not so, it will be because some frag
847 between grows, and that will force another pass. */
848
849 /* JF was just address */
850 /* JF also added is_dnrange hack */
851 /* There's gotta be a better/faster/etc way
852 to do this. . . */
853 /* gnu@cygnus.com: I changed this from > to >=
854 because I ran into a zero-length frag (fr_fix=0)
855 which was created when the obstack needed a new
856 chunk JUST AFTER the opcode of a branch. Since
857 fr_fix is zero, fr_address of this frag is the same
858 as fr_address of the next frag. This
859 zero-length frag was variable and jumped to .+2
860 (in the next frag), but since the > comparison
861 below failed (the two were =, not >), "stretch"
862 was not added to the target. Stretch was 178, so
863 the offset appeared to be .-176 instead, which did
864 not fit into a byte branch, so the assembler
865 relaxed the branch to a word. This didn't compare
866 with what happened when the same source file was
867 assembled on other machines, which is how I found it.
868 You might want to think about what other places have
869 trouble with zero length frags... */
870
871 if (symbolP->sy_frag->fr_address >= was_address
872 && is_dnrange(fragP,symbolP->sy_frag)) {
873 target += stretch;
874 } /* */
875
876 } /* if there's a symbol attached */
877
878 aim = target - address - fragP->fr_fix;
879 /* The displacement is affected by the instruction size
880 * for the 32k architecture. I think we ought to be able
881 * to add fragP->fr_pcrel_adjust in all cases (it should be
882 * zero if not used), but just in case it breaks something
883 * else we'll put this inside #ifdef NS32K ... #endif
884 */
885 #ifdef TC_NS32K
886 aim += fragP->fr_pcrel_adjust;
887 #endif /* TC_NS32K */
888
889 if (aim < 0) {
890 /* Look backwards. */
891 for (next_state = this_type->rlx_more; next_state; ) {
892 if (aim >= this_type->rlx_backward) {
893 next_state = 0;
894 } else { /* Grow to next state. */
895 this_type = md_relax_table + (this_state = next_state);
896 next_state = this_type->rlx_more;
897 }
898 }
899 } else {
900 #ifdef DONTDEF
901 /* JF these next few lines of code are for the mc68020 which can't handle short
902 offsets of zero in branch instructions. What a kludge! */
903 if (aim==0 && this_state==(1<<2+0)) { /* FOO hard encoded from m.c */
904 aim=this_type->rlx_forward+1; /* Force relaxation into word mode */
905 }
906 #endif
907 #ifdef M68K_AIM_KLUDGE
908 M68K_AIM_KLUDGE(aim, this_state, this_type);
909 #endif
910 /* JF end of 68020 code */
911 /* Look forwards. */
912 for (next_state = this_type->rlx_more; next_state; ) {
913 if (aim <= this_type->rlx_forward) {
914 next_state = 0;
915 } else { /* Grow to next state. */
916 this_type = md_relax_table + (this_state = next_state);
917 next_state = this_type->rlx_more;
918 }
919 }
920 }
921
922 if ((growth = this_type->rlx_length - start_type->rlx_length) != 0)
923 fragP->fr_subtype = this_state;
924
925 break;
926 } /* case rs_machine_dependent */
927
928 default:
929 BAD_CASE( fragP->fr_type );
930 break;
931 }
932 if (growth) {
933 stretch += growth;
934 stretched++;
935 }
936 } /* For each frag in the segment. */
937 } while (stretched); /* Until nothing further to relax. */
938 } /* do_relax */
939
940 /*
941 * We now have valid fr_address'es for each frag.
942 */
943
944 /*
945 * All fr_address's are correct, relative to their own segment.
946 * We have made all the fixS we will ever make.
947 */
948 } /* relax_segment() */
949
950 /*
951 * Relax_align. Advance location counter to next address that has 'alignment'
952 * lowest order bits all 0s.
953 */
954
955 /* How many addresses does the .align take? */
956 static relax_addressT relax_align(address, alignment)
957 register relax_addressT address; /* Address now. */
958 register long alignment; /* Alignment (binary). */
959 {
960 relax_addressT mask;
961 relax_addressT new_address;
962
963 mask = ~ ( (~0) << alignment );
964 new_address = (address + mask) & (~ mask);
965 return (new_address - address);
966 } /* relax_align() */
967
968 /* fixup_segment()
969
970 Go through all the fixS's in a segment and see which ones can be
971 handled now. (These consist of fixS where we have since discovered
972 the value of a symbol, or the address of the frag involved.)
973 For each one, call md_apply_fix to put the fix into the frag data.
974
975 Result is a count of how many relocation structs will be needed to
976 handle the remaining fixS's that we couldn't completely handle here.
977 These will be output later by emit_relocations(). */
978
979 static long fixup_segment(fixP, this_segment_type)
980 register fixS * fixP;
981 segT this_segment_type; /* N_TYPE bits for segment. */
982 {
983 register long seg_reloc_count;
984 register symbolS *add_symbolP;
985 register symbolS *sub_symbolP;
986 register long add_number;
987 register int size;
988 register char *place;
989 register long where;
990 register char pcrel;
991 register fragS *fragP;
992 register segT add_symbol_segment = SEG_ABSOLUTE;
993
994 /* FIXME: remove this line */ /* fixS *orig = fixP; */
995 seg_reloc_count = 0;
996
997 for ( ; fixP; fixP = fixP->fx_next) {
998 fragP = fixP->fx_frag;
999 know(fragP);
1000 where = fixP->fx_where;
1001 place = fragP->fr_literal + where;
1002 size = fixP->fx_size;
1003 add_symbolP = fixP->fx_addsy;
1004 #ifdef TC_I960
1005 if (fixP->fx_callj && TC_S_IS_CALLNAME(add_symbolP)) {
1006 /* Relocation should be done via the
1007 associated 'bal' entry point
1008 symbol. */
1009
1010 if (!TC_S_IS_BALNAME(tc_get_bal_of_call(add_symbolP))) {
1011 as_bad("No 'bal' entry point for leafproc %s",
1012 S_GET_NAME(add_symbolP));
1013 continue;
1014 }
1015 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call(add_symbolP);
1016 } /* callj relocation */
1017 #endif
1018 sub_symbolP = fixP->fx_subsy;
1019 add_number = fixP->fx_offset;
1020 pcrel = fixP->fx_pcrel;
1021
1022 if (add_symbolP) {
1023 add_symbol_segment = S_GET_SEGMENT(add_symbolP);
1024 } /* if there is an addend */
1025
1026 if (sub_symbolP) {
1027 if (!add_symbolP) {
1028 /* Its just -sym */
1029 if (S_GET_SEGMENT(sub_symbolP) != SEG_ABSOLUTE) {
1030 as_bad("Negative of non-absolute symbol %s", S_GET_NAME(sub_symbolP));
1031 } /* not absolute */
1032
1033 add_number -= S_GET_VALUE(sub_symbolP);
1034
1035 /* if sub_symbol is in the same segment that add_symbol
1036 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
1037 } else if ((S_GET_SEGMENT(sub_symbolP) == add_symbol_segment)
1038 && (SEG_NORMAL(add_symbol_segment)
1039 || (add_symbol_segment == SEG_ABSOLUTE))) {
1040 /* Difference of 2 symbols from same segment. */
1041 /* Can't make difference of 2 undefineds: 'value' means */
1042 /* something different for N_UNDF. */
1043 #ifdef TC_I960
1044 /* Makes no sense to use the difference of 2 arbitrary symbols
1045 * as the target of a call instruction.
1046 */
1047 if (fixP->fx_callj) {
1048 as_bad("callj to difference of 2 symbols");
1049 }
1050 #endif /* TC_I960 */
1051 add_number += S_GET_VALUE(add_symbolP) -
1052 S_GET_VALUE(sub_symbolP);
1053
1054 add_symbolP = NULL;
1055 fixP->fx_addsy = NULL;
1056 } else {
1057 /* Different segments in subtraction. */
1058 know(!(S_IS_EXTERNAL(sub_symbolP) && (S_GET_SEGMENT(sub_symbolP) == SEG_ABSOLUTE)));
1059
1060 if ((S_GET_SEGMENT(sub_symbolP) == SEG_ABSOLUTE)) {
1061 add_number -= S_GET_VALUE(sub_symbolP);
1062 } else {
1063 as_bad("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
1064 segment_name(S_GET_SEGMENT(sub_symbolP)),
1065 S_GET_NAME(sub_symbolP), fragP->fr_address + where);
1066 } /* if absolute */
1067 }
1068 } /* if sub_symbolP */
1069
1070 if (add_symbolP) {
1071 if (add_symbol_segment == this_segment_type && pcrel) {
1072 /*
1073 * This fixup was made when the symbol's segment was
1074 * SEG_UNKNOWN, but it is now in the local segment.
1075 * So we know how to do the address without relocation.
1076 */
1077 #ifdef TC_I960
1078 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
1079 * in which cases it modifies *fixP as appropriate. In the case
1080 * of a 'calls', no further work is required, and *fixP has been
1081 * set up to make the rest of the code below a no-op.
1082 */
1083 reloc_callj(fixP);
1084 #endif /* TC_I960 */
1085
1086 add_number += S_GET_VALUE(add_symbolP);
1087 add_number -= md_pcrel_from (fixP);
1088 pcrel = 0; /* Lie. Don't want further pcrel processing. */
1089 fixP->fx_addsy = NULL; /* No relocations please. */
1090 } else {
1091 switch (add_symbol_segment) {
1092 case SEG_ABSOLUTE:
1093 #ifdef TC_I960
1094 reloc_callj(fixP); /* See comment about reloc_callj() above*/
1095 #endif /* TC_I960 */
1096 add_number += S_GET_VALUE(add_symbolP);
1097 fixP->fx_addsy = NULL;
1098 add_symbolP = NULL;
1099 break;
1100 default:
1101 seg_reloc_count ++;
1102 add_number += S_GET_VALUE(add_symbolP);
1103 break;
1104
1105 case SEG_UNKNOWN:
1106 #ifdef TC_I960
1107 if ((int)fixP->fx_bit_fixP == 13) {
1108 /* This is a COBR instruction. They have only a
1109 * 13-bit displacement and are only to be used
1110 * for local branches: flag as error, don't generate
1111 * relocation.
1112 */
1113 as_bad("can't use COBR format with external label");
1114 fixP->fx_addsy = NULL; /* No relocations please. */
1115 continue;
1116 } /* COBR */
1117 #endif /* TC_I960 */
1118
1119 #ifdef OBJ_COFF
1120 #ifdef TE_I386AIX
1121 if (S_IS_COMMON(add_symbolP))
1122 add_number += S_GET_VALUE(add_symbolP);
1123 #endif /* TE_I386AIX */
1124 #endif /* OBJ_COFF */
1125 ++seg_reloc_count;
1126
1127 break;
1128
1129
1130 } /* switch on symbol seg */
1131 } /* if not in local seg */
1132 } /* if there was a + symbol */
1133
1134 if (pcrel) {
1135 add_number -= md_pcrel_from(fixP);
1136 if (add_symbolP == 0) {
1137 fixP->fx_addsy = & abs_symbol;
1138 ++seg_reloc_count;
1139 } /* if there's an add_symbol */
1140 } /* if pcrel */
1141
1142 if (!fixP->fx_bit_fixP) {
1143 if ((size==1 &&
1144 (add_number& ~0xFF) && (add_number&~0xFF!=(-1&~0xFF))) ||
1145 (size==2 &&
1146 (add_number& ~0xFFFF) && (add_number&~0xFFFF!=(-1&~0xFFFF)))) {
1147 as_bad("Value of %d too large for field of %d bytes at 0x%x",
1148 add_number, size, fragP->fr_address + where);
1149 } /* generic error checking */
1150 } /* not a bit fix */
1151
1152 md_apply_fix(fixP, add_number);
1153 } /* For each fixS in this segment. */
1154
1155 #ifdef OBJ_COFF
1156 #ifdef TC_I960
1157 {
1158 fixS *topP = fixP;
1159
1160 /* two relocs per callj under coff. */
1161 for (fixP = topP; fixP; fixP = fixP->fx_next) {
1162 if (fixP->fx_callj && fixP->fx_addsy != 0) {
1163 ++seg_reloc_count;
1164 } /* if callj and not already fixed. */
1165 } /* for each fix */
1166 }
1167 #endif /* TC_I960 */
1168
1169 #endif /* OBJ_COFF */
1170 return(seg_reloc_count);
1171 } /* fixup_segment() */
1172
1173
1174 static int is_dnrange(f1,f2)
1175 struct frag *f1;
1176 struct frag *f2;
1177 {
1178 while (f1) {
1179 if (f1->fr_next==f2)
1180 return 1;
1181 f1=f1->fr_next;
1182 }
1183 return 0;
1184 } /* is_dnrange() */
1185
1186 /* Append a string onto another string, bumping the pointer along. */
1187 void
1188 append (charPP, fromP, length)
1189 char **charPP;
1190 char *fromP;
1191 unsigned long length;
1192 {
1193 if (length) { /* Don't trust memcpy() of 0 chars. */
1194 memcpy(*charPP, fromP, (int) length);
1195 *charPP += length;
1196 }
1197 }
1198
1199 int section_alignment[SEG_MAXIMUM_ORDINAL];
1200
1201 /*
1202 * This routine records the largest alignment seen for each segment.
1203 * If the beginning of the segment is aligned on the worst-case
1204 * boundary, all of the other alignments within it will work. At
1205 * least one object format really uses this info.
1206 */
1207 void record_alignment(seg, align)
1208 segT seg; /* Segment to which alignment pertains */
1209 int align; /* Alignment, as a power of 2
1210 * (e.g., 1 => 2-byte boundary, 2 => 4-byte boundary, etc.)
1211 */
1212 {
1213
1214 if ( align > section_alignment[(int) seg] ){
1215 section_alignment[(int) seg] = align;
1216 } /* if highest yet */
1217
1218 return;
1219 } /* record_alignment() */
1220
1221 /*
1222 * Local Variables:
1223 * comment-column: 0
1224 * fill-column: 131
1225 * End:
1226 */
1227
1228 /* end of write.c */
This page took 0.054251 seconds and 5 git commands to generate.