* config/obj-coffbfd.c (obj_coff_endef): Correct test for .bf
[deliverable/binutils-gdb.git] / gas / write.c
1 /* write.c - emit .o file
2 Copyright (C) 1986, 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* This thing should be set up to do byteordering correctly. But... */
21
22 #include "as.h"
23 #include "subsegs.h"
24 #include "obstack.h"
25 #include "output-file.h"
26
27 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
28 instruction so that the disassembler does not choke on it. */
29 #ifndef NOP_OPCODE
30 #define NOP_OPCODE 0x00
31 #endif
32
33 #ifndef WORKING_DOT_WORD
34 extern CONST int md_short_jump_size;
35 extern CONST int md_long_jump_size;
36 #endif
37
38 #ifndef BFD_ASSEMBLER
39
40 #ifndef MANY_SEGMENTS
41 struct frag *text_frag_root;
42 struct frag *data_frag_root;
43 struct frag *bss_frag_root;
44
45 struct frag *text_last_frag; /* Last frag in segment. */
46 struct frag *data_last_frag; /* Last frag in segment. */
47 static struct frag *bss_last_frag; /* Last frag in segment. */
48 #endif
49
50 static object_headers headers;
51
52 long string_byte_count;
53
54 static char *the_object_file;
55
56 char *next_object_file_charP; /* Tracks object file bytes. */
57
58 #ifndef OBJ_VMS
59 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
60 #endif
61
62 #endif /* BFD_ASSEMBLER */
63
64 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
65 static relax_addressT relax_align PARAMS ((relax_addressT addr, long align));
66 void relax_segment PARAMS ((struct frag * seg_frag_root, segT seg_type));
67
68 /*
69 * fix_new()
70 *
71 * Create a fixS in obstack 'notes'.
72 */
73 fixS *
74 fix_new (frag, where, size, add_symbol, sub_symbol, offset, pcrel, r_type)
75 fragS *frag; /* Which frag? */
76 int where; /* Where in that frag? */
77 short int size; /* 1, 2, or 4 usually. */
78 symbolS *add_symbol; /* X_add_symbol. */
79 symbolS *sub_symbol; /* X_subtract_symbol. */
80 long offset; /* X_add_number. */
81 int pcrel; /* TRUE if PC-relative relocation. */
82 #ifdef BFD_ASSEMBLER
83 bfd_reloc_code_real_type r_type; /* Relocation type */
84 #else
85 int r_type; /* Relocation type */
86 #endif
87 {
88 fixS *fixP;
89
90 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
91
92 fixP->fx_frag = frag;
93 fixP->fx_where = where;
94 fixP->fx_size = size;
95 fixP->fx_addsy = add_symbol;
96 fixP->fx_subsy = sub_symbol;
97 fixP->fx_offset = offset;
98 fixP->fx_pcrel = pcrel;
99 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
100 fixP->fx_r_type = r_type;
101 #endif
102 fixP->fx_im_disp = 0;
103 fixP->fx_pcrel_adjust = 0;
104 fixP->fx_bit_fixP = 0;
105 fixP->fx_addnumber = 0;
106
107 #ifdef TC_something
108 fixP->fx_bsr = 0;
109 #endif
110 #ifdef TC_I960
111 fixP->fx_callj = 0;
112 #endif
113
114 /* Usually, we want relocs sorted numerically, but while
115 comparing to older versions of gas that have relocs
116 reverse sorted, it is convenient to have this compile
117 time option. xoxorich. */
118
119 {
120
121 #ifdef BFD_ASSEMBLER
122 fixS **seg_fix_rootP = & (seg_info (now_seg)->fix_root);
123 fixS **seg_fix_tailP = & (seg_info (now_seg)->fix_tail);
124 #endif
125
126 #ifdef REVERSE_SORT_RELOCS
127
128 fixP->fx_next = *seg_fix_rootP;
129 *seg_fix_rootP = fixP;
130
131 #else /* REVERSE_SORT_RELOCS */
132
133 fixP->fx_next = NULL;
134
135 if (*seg_fix_tailP)
136 (*seg_fix_tailP)->fx_next = fixP;
137 else
138 *seg_fix_rootP = fixP;
139 *seg_fix_tailP = fixP;
140
141 #endif /* REVERSE_SORT_RELOCS */
142
143 }
144
145 return fixP;
146 }
147
148 /* Append a string onto another string, bumping the pointer along. */
149 void
150 append (charPP, fromP, length)
151 char **charPP;
152 char *fromP;
153 unsigned long length;
154 {
155 /* Don't trust memcpy() of 0 chars. */
156 if (length == 0)
157 return;
158
159 memcpy (*charPP, fromP, (int) length);
160 *charPP += length;
161 }
162
163 #ifndef BFD_ASSEMBLER
164 int section_alignment[SEG_MAXIMUM_ORDINAL];
165 #endif
166
167 /*
168 * This routine records the largest alignment seen for each segment.
169 * If the beginning of the segment is aligned on the worst-case
170 * boundary, all of the other alignments within it will work. At
171 * least one object format really uses this info.
172 */
173 void
174 record_alignment (seg, align)
175 /* Segment to which alignment pertains */
176 segT seg;
177 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
178 boundary, etc.) */
179 int align;
180 {
181 #ifdef BFD_ASSEMBLER
182 if (align > bfd_get_section_alignment (stdoutput, seg))
183 bfd_set_section_alignment (stdoutput, seg, align);
184 #else
185 if (align > section_alignment[(int) seg])
186 section_alignment[(int) seg] = align;
187 #endif
188 }
189
190 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
191
192 static fragS *
193 chain_frchains_together_1 (section, frchp)
194 segT section;
195 struct frchain *frchp;
196 {
197 fragS dummy, *prev_frag = &dummy;
198 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
199 {
200 prev_frag->fr_next = frchp->frch_root;
201 prev_frag = frchp->frch_last;
202 }
203 prev_frag->fr_next = 0;
204 return prev_frag;
205 }
206
207 #endif
208
209 #ifdef BFD_ASSEMBLER
210
211 static void
212 chain_frchains_together (abfd, section, xxx)
213 bfd *abfd; /* unused */
214 segT section;
215 char *xxx; /* unused */
216 {
217 segment_info_type *info;
218
219 /* BFD may have introduced its own sections without using
220 subseg_new, so it is possible that seg_info is NULL. */
221 info = seg_info (section);
222 if (info != (segment_info_type *) NULL)
223 chain_frchains_together_1 (section, info->frchainP);
224 }
225
226 #endif
227
228 #ifndef BFD
229
230 void
231 remove_subsegs (head, seg, root, last)
232 frchainS *head;
233 int seg;
234 fragS **root;
235 fragS **last;
236 {
237 *root = head->frch_root;
238 *last = chain_frchains_together_1 (seg, head);
239 }
240
241 #endif /* BFD */
242
243 #ifndef BFD
244
245 static void
246 cvt_frag_to_fill (x, fragP)
247 #ifdef BFD_ASSEMBLER
248 segT x;
249 #else
250 object_headers *x;
251 #endif
252 fragS *fragP;
253 {
254 #ifdef BFD_ASSEMBLER
255 segT sec = x;
256 #else
257 object_headers *headers = x;
258 #endif
259
260 switch (fragP->fr_type)
261 {
262 case rs_align:
263 case rs_org:
264 #ifdef HANDLE_ALIGN
265 HANDLE_ALIGN (fragP);
266 #endif
267 fragP->fr_type = rs_fill;
268 know (fragP->fr_var == 1);
269 know (fragP->fr_next != NULL);
270
271 fragP->fr_offset = (fragP->fr_next->fr_address
272 - fragP->fr_address
273 - fragP->fr_fix);
274 break;
275
276 case rs_fill:
277 break;
278
279 case rs_machine_dependent:
280 #ifdef BFD_ASSEMBLER
281 md_convert_frag (stdoutput, sec, fragP);
282 #else
283 md_convert_frag (headers, fragP);
284 #endif
285
286 assert (fragP->fr_next == NULL \
287 || (fragP->fr_next->fr_address - fragP->fr_address \
288 == fragP->fr_fix));
289
290 /*
291 * After md_convert_frag, we make the frag into a ".space 0".
292 * Md_convert_frag() should set up any fixSs and constants
293 * required.
294 */
295 frag_wane (fragP);
296 break;
297
298 #ifndef WORKING_DOT_WORD
299 case rs_broken_word:
300 {
301 struct broken_word *lie;
302
303 if (fragP->fr_subtype)
304 {
305 fragP->fr_fix += md_short_jump_size;
306 for (lie = (struct broken_word *) (fragP->fr_symbol);
307 lie && lie->dispfrag == fragP;
308 lie = lie->next_broken_word)
309 if (lie->added == 1)
310 fragP->fr_fix += md_long_jump_size;
311 }
312 frag_wane (fragP);
313 }
314 break;
315 #endif
316
317 default:
318 BAD_CASE (fragP->fr_type);
319 break;
320 }
321 }
322
323 #ifdef BFD_ASSEMBLER
324 static void
325 relax_and_size_seg (abfd, sec, xxx)
326 bfd *abfd;
327 asection *sec;
328 char *xxx;
329 {
330 flagword flags;
331
332 flags = bfd_get_section_flags (abfd, sec);
333
334 if (flags & SEC_ALLOC)
335 {
336 fragS *fragp;
337 segment_info_type *seginfo;
338 int x;
339 unsigned long size, newsize;
340
341 seginfo = (segment_info_type *) bfd_get_section_userdata (abfd, sec);
342 relax_segment (seginfo->frchainP->frch_root, sec);
343 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
344 cvt_frag_to_fill (sec, fragp);
345 for (fragp = seginfo->frchainP->frch_root;
346 fragp->fr_next;
347 fragp = fragp->fr_next)
348 /* walk to last elt */;
349 size = fragp->fr_address;
350 if (size > 0)
351 {
352 flags |= SEC_HAS_CONTENTS;
353 /* @@ This is just an approximation. */
354 if (seginfo->fix_root)
355 flags |= SEC_RELOC;
356 x = bfd_set_section_flags (abfd, sec, flags);
357 assert (x == true);
358 }
359 size = md_section_align (sec, size);
360 x = bfd_set_section_size (abfd, sec, size);
361 assert (x == true);
362
363 /* If the size had to be rounded up, add some padding in the last
364 non-empty frag. */
365 newsize = bfd_get_section_size_before_reloc (sec);
366 assert (newsize >= size);
367 if (size != newsize)
368 {
369 fragS *last = seginfo->frchainP->frch_last;
370 fragp = seginfo->frchainP->frch_root;
371 while (fragp->fr_next != last)
372 fragp = fragp->fr_next;
373 last->fr_address = size;
374 fragp->fr_offset += newsize - size;
375 }
376 }
377 #ifdef tc_frob_section
378 tc_frob_section (sec);
379 #endif
380 #ifdef obj_frob_section
381 obj_frob_section (sec);
382 #endif
383 }
384
385 static void
386 write_contents (abfd, sec, xxx)
387 bfd *abfd;
388 asection *sec;
389 char *xxx;
390 {
391 segment_info_type *seginfo = seg_info (sec);
392 unsigned long offset = 0;
393 fragS *frags;
394 int i, n;
395 arelent **relocs;
396 fixS *fixp;
397
398 if (! (bfd_get_section_flags (abfd, sec) & SEC_LOAD))
399 return;
400
401 fixup_segment (seginfo->fix_root, sec);
402
403 for (i = 0, fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
404 if (fixp->fx_addsy)
405 {
406 symbolS *sym = fixp->fx_addsy;
407 asection *sec = sym->bsym->section;
408 if (sec == &bfd_und_section
409 || sec == &bfd_abs_section
410 || sec == &bfd_com_section)
411 continue;
412 if (sym->bsym == sec->symbol)
413 continue;
414 /* If the section symbol isn't going to be output, the relocs
415 at least should still work. If not, figure out what to do
416 when we run into that case. */
417 fixp->fx_offset += S_GET_VALUE (sym);
418 fixp->fx_addsy = symbol_find (sec->name);
419 if (!fixp->fx_addsy)
420 {
421 fixp->fx_addsy = symbol_make (sec->name);
422 fixp->fx_addsy->bsym = sec->symbol;
423 }
424 }
425
426 /* Force calculations (size, vma) to get done. */
427 bfd_set_section_contents (stdoutput, sec, "", 0, 0);
428
429 /* Set up reloc information as well. */
430 n = 0;
431 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
432 n++;
433 relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
434 n * sizeof (arelent *));
435
436 for (frags = seginfo->frchainP->frch_root, fixp = seginfo->fix_root, i = 0;
437 frags;
438 frags = frags->fr_next)
439 {
440 int x;
441 unsigned long fill_size;
442 char *fill_literal;
443 long count;
444
445 assert (frags->fr_type == rs_fill);
446 while (fixp
447 && fixp->fx_frag == frags)
448 {
449 arelent *reloc;
450 extern arelent *tc_gen_reloc ();
451 char *data;
452 static bfd_reloc_status_type s;
453
454 if (fixp->fx_addsy == 0)
455 {
456 /* @@ Need some other flag to indicate which have already
457 been performed... */
458 n--;
459 goto next;
460 }
461 reloc = tc_gen_reloc (sec, fixp);
462 if (!reloc)
463 {
464 n--;
465 goto next;
466 }
467 data = frags->fr_literal + fixp->fx_where;
468 if (fixp->fx_where + 4 > frags->fr_fix + frags->fr_offset)
469 abort ();
470 s = bfd_perform_relocation (stdoutput, reloc, data - reloc->address,
471 sec, stdoutput);
472 switch (s)
473 {
474 case bfd_reloc_ok:
475 break;
476 default:
477 printf ("bad s value\n");
478 abort ();
479 }
480 relocs[i++] = reloc;
481 next:
482 fixp = fixp->fx_next;
483 }
484 if (frags->fr_fix)
485 {
486 x = bfd_set_section_contents (stdoutput, sec,
487 frags->fr_literal, offset,
488 frags->fr_fix);
489 assert (x == true);
490 offset += frags->fr_fix;
491 }
492 fill_literal = frags->fr_literal + frags->fr_fix;
493 fill_size = frags->fr_var;
494 count = frags->fr_offset;
495 assert (count >= 0);
496 if (fill_size && count)
497 while (count--)
498 {
499 x = bfd_set_section_contents (stdoutput, sec,
500 fill_literal, offset, fill_size);
501 assert (x == true);
502 offset += fill_size;
503 }
504 }
505 /* Did we miss any relocs? */
506 if (fixp != 0)
507 abort ();
508
509 if (n)
510 bfd_set_reloc (stdoutput, sec, relocs, n);
511 }
512 #endif
513
514 void
515 write_object_file ()
516 {
517 register struct frchain *frchainP; /* Track along all frchains. */
518 register fragS *fragP; /* Track along all frags. */
519 register struct frchain *next_frchainP;
520 register fragS **prev_fragPP;
521
522 long object_file_size;
523
524 /* Do we really want to write it? */
525 {
526 int n_warns, n_errs;
527 n_warns = had_warnings ();
528 n_errs = had_errors ();
529 /* The -Z flag indicates that an object file should be generated,
530 regardless of warnings and errors. */
531 if (flagseen['Z'])
532 {
533 if (n_warns || n_errs)
534 as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
535 n_errs, n_errs == 1 ? "" : "s",
536 n_warns, n_warns == 1 ? "" : "s");
537 }
538 else
539 {
540 if (n_errs)
541 as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
542 n_errs, n_errs == 1 ? "" : "s",
543 n_warns, n_warns == 1 ? "" : "s");
544 }
545 }
546
547 #ifdef OBJ_VMS
548 /*
549 * Under VMS we try to be compatible with VAX-11 "C". Thus, we
550 * call a routine to check for the definition of the procedure
551 * "_main", and if so -- fix it up so that it can be program
552 * entry point.
553 */
554 VMS_Check_For_Main ();
555 #endif /* VMS */
556
557 /* After every sub-segment, we fake an ".align ...". This conforms to
558 BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
559 frag that requires least thought. ".align" frags like to have a
560 following frag since that makes calculating their intended length
561 trivial.
562
563 @@ Is this really necessary?? */
564 #ifndef SUB_SEGMENT_ALIGN
565 #ifdef BFD_ASSEMBLER
566 #define SUB_SEGMENT_ALIGN(SEG) (0)
567 #else
568 #define SUB_SEGMENT_ALIGN(SEG) (2)
569 #endif
570 #endif
571 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
572 {
573 #ifdef BFD_ASSEMBLER
574 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
575 #else
576 subseg_new (frchainP->frch_seg, frchainP->frch_subseg);
577 #endif
578 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
579 /* frag_align will have left a new frag.
580 Use this last frag for an empty ".fill".
581
582 For this segment ...
583 Create a last frag. Do not leave a "being filled in frag". */
584 frag_wane (frag_now);
585 frag_now->fr_fix = 0;
586 know (frag_now->fr_next == NULL);
587 /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
588 /* Above shows we haven't left a half-completed object on obstack. */
589 }
590
591 /* From now on, we don't care about sub-segments. Build one frag chain
592 for each segment. Linked thru fr_next. */
593
594 #ifdef BFD_ASSEMBLER
595 /* Remove the sections created by gas for its own purposes. */
596 {
597 asection **seclist, *sec;
598 seclist = &stdoutput->sections;
599 while (seclist && *seclist)
600 {
601 sec = *seclist;
602 while (sec == big_section
603 || sec == reg_section
604 || sec == pass1_section
605 || sec == diff_section
606 || sec == absent_section)
607 {
608 sec = sec->next;
609 *seclist = sec;
610 stdoutput->section_count--;
611 if (!sec)
612 break;
613 }
614 if (*seclist)
615 seclist = &(*seclist)->next;
616 }
617 }
618
619 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
620 #else
621 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
622 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
623 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
624 #endif
625
626 /* We have two segments. If user gave -R flag, then we must put the
627 data frags into the text segment. Do this before relaxing so
628 we know to take advantage of -R and make shorter addresses. */
629 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
630 if (flagseen['R'])
631 {
632 #ifdef BFD_ASSEMBLER
633 seg_info (text_section)->frchainP->frch_last->fr_next =
634 seg_info (data_section)->frchainP->frch_root;
635 seg_info (text_section)->frchainP->frch_last =
636 seg_info (data_section)->frchainP->frch_last;
637 seg_info (data_section)->frchainP = 0;
638 #else
639 fixS *tmp;
640
641 text_last_frag->fr_next = data_frag_root;
642 text_last_frag = data_last_frag;
643 data_last_frag = NULL;
644 data_frag_root = NULL;
645 if (text_fix_root)
646 {
647 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
648 tmp->fx_next = data_fix_root;
649 text_fix_tail = data_fix_tail;
650 }
651 else
652 text_fix_root = data_fix_root;
653 data_fix_root = NULL;
654 #endif
655 }
656 #endif
657
658 #ifdef BFD_ASSEMBLER
659 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
660 #else
661 relax_segment (text_frag_root, SEG_TEXT);
662 relax_segment (data_frag_root, SEG_DATA);
663 relax_segment (bss_frag_root, SEG_BSS);
664 /*
665 * Now the addresses of frags are correct within the segment.
666 */
667
668 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
669 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
670 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
671
672 /*
673 * Join the 2 segments into 1 huge segment.
674 * To do this, re-compute every rn_address in the SEG_DATA frags.
675 * Then join the data frags after the text frags.
676 *
677 * Determine a_data [length of data segment].
678 */
679 if (data_frag_root)
680 {
681 register relax_addressT slide;
682
683 know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
684
685 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
686 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
687 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
688 #ifdef OBJ_BOUT
689 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
690 /* For b.out: If the data section has a strict alignment
691 requirement, its load address in the .o file will be
692 rounded up from the size of the text section. These
693 two values are *not* the same! Similarly for the bss
694 section.... */
695 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
696 #endif
697
698 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
699 {
700 fragP->fr_address += slide;
701 } /* for each data frag */
702
703 know (text_last_frag != 0);
704 text_last_frag->fr_next = data_frag_root;
705 }
706 else
707 {
708 H_SET_DATA_SIZE (&headers, 0);
709 }
710
711 #ifdef OBJ_BOUT
712 /* See above comments on b.out data section address. */
713 {
714 long bss_vma;
715 if (data_last_frag == 0)
716 bss_vma = H_GET_TEXT_SIZE (&headers);
717 else
718 bss_vma = data_last_frag->fr_address;
719 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
720 bss_address_frag.fr_address = bss_vma;
721 }
722 #else /* ! OBJ_BOUT */
723 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
724 H_GET_DATA_SIZE (&headers));
725
726
727 /* Slide all the frags */
728 if (bss_frag_root)
729 {
730 relax_addressT slide = bss_address_frag.fr_address;
731
732 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
733 {
734 fragP->fr_address += slide;
735 } /* for each bss frag */
736 }
737
738 #endif /* ! OBJ_BOUT */
739
740 if (bss_last_frag)
741 H_SET_BSS_SIZE (&headers,
742 bss_last_frag->fr_address - bss_frag_root->fr_address);
743 else
744 H_SET_BSS_SIZE (&headers, 0);
745 #endif /* BFD_ASSEMBLER */
746
747 #ifndef BFD_ASSEMBLER
748 /*
749 *
750 * Crawl the symbol chain.
751 *
752 * For each symbol whose value depends on a frag, take the address of
753 * that frag and subsume it into the value of the symbol.
754 * After this, there is just one way to lookup a symbol value.
755 * Values are left in their final state for object file emission.
756 * We adjust the values of 'L' local symbols, even if we do
757 * not intend to emit them to the object file, because their values
758 * are needed for fix-ups.
759 *
760 * Unless we saw a -L flag, remove all symbols that begin with 'L'
761 * from the symbol chain. (They are still pointed to by the fixes.)
762 *
763 * Count the remaining symbols.
764 * Assign a symbol number to each symbol.
765 * Count the number of string-table chars we will emit.
766 * Put this info into the headers as appropriate.
767 *
768 */
769 know (zero_address_frag.fr_address == 0);
770 string_byte_count = sizeof (string_byte_count);
771
772 obj_crawl_symbol_chain (&headers);
773
774 if (string_byte_count == sizeof (string_byte_count))
775 string_byte_count = 0;
776
777 H_SET_STRING_SIZE (&headers, string_byte_count);
778
779 /*
780 * Addresses of frags now reflect addresses we use in the object file.
781 * Symbol values are correct.
782 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
783 * Also converting any machine-dependent frags using md_convert_frag();
784 */
785 subseg_change (SEG_TEXT, 0);
786
787 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
788 {
789 cvt_frag_to_fill (&headers, fragP);
790
791 /* Some assert macros don't work with # directives mixed in. */
792 #ifndef NDEBUG
793 if (!(fragP->fr_next == NULL
794 #ifdef OBJ_BOUT
795 || fragP->fr_next == data_frag_root
796 #endif
797 || ((fragP->fr_next->fr_address - fragP->fr_address)
798 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
799 abort ();
800 #endif
801 }
802 #endif /* ! BFD_ASSEMBLER */
803
804 #ifndef WORKING_DOT_WORD
805 {
806 struct broken_word *lie;
807 struct broken_word **prevP;
808
809 prevP = &broken_words;
810 for (lie = broken_words; lie; lie = lie->next_broken_word)
811 if (!lie->added)
812 {
813 #ifdef BFD_ASSEMBLER
814 fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
815 2, lie->add, lie->sub, lie->addnum, 0, BFD_RELOC_NONE);
816 #else
817 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
818 fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
819 2, lie->add,
820 lie->sub, lie->addnum,
821 0, NO_RELOC);
822 #else
823 #ifdef TC_NS32K
824 fix_new_ns32k (lie->frag,
825 lie->word_goes_here - lie->frag->fr_literal,
826 2,
827 lie->add,
828 lie->sub,
829 lie->addnum,
830 0, 0, 2, 0, 0);
831 #else
832 fix_new (lie->frag, lie->word_goes_here - lie->frag->fr_literal,
833 2, lie->add,
834 lie->sub, lie->addnum,
835 0, 0);
836 #endif /* TC_NS32K */
837 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
838 #endif /* BFD_ASSEMBLER */
839 *prevP = lie->next_broken_word;
840 }
841 else
842 prevP = &(lie->next_broken_word);
843
844 for (lie = broken_words; lie;)
845 {
846 struct broken_word *untruth;
847 char *table_ptr;
848 long table_addr;
849 long from_addr, to_addr;
850 int n, m;
851
852 fragP = lie->dispfrag;
853
854 /* Find out how many broken_words go here. */
855 n = 0;
856 for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
857 if (untruth->added == 1)
858 n++;
859
860 table_ptr = lie->dispfrag->fr_opcode;
861 table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
862 /* Create the jump around the long jumps. This is a short
863 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
864 from_addr = table_addr;
865 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
866 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
867 table_ptr += md_short_jump_size;
868 table_addr += md_short_jump_size;
869
870 for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
871 {
872 if (lie->added == 2)
873 continue;
874 /* Patch the jump table */
875 /* This is the offset from ??? to table_ptr+0 */
876 to_addr = table_addr
877 - S_GET_VALUE (lie->sub);
878 md_number_to_chars (lie->word_goes_here, to_addr, 2);
879 for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
880 {
881 if (untruth->use_jump == lie)
882 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
883 }
884
885 /* Install the long jump */
886 /* this is a long jump from table_ptr+0 to the final target */
887 from_addr = table_addr;
888 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
889 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
890 table_ptr += md_long_jump_size;
891 table_addr += md_long_jump_size;
892 }
893 }
894 }
895 #endif /* not WORKING_DOT_WORD */
896
897 #ifndef BFD_ASSEMBLER
898 #ifndef OBJ_VMS
899 { /* not vms */
900 /*
901 * Scan every FixS performing fixups. We had to wait until now to do
902 * this because md_convert_frag() may have made some fixSs.
903 */
904 int trsize, drsize;
905
906 subseg_change (SEG_TEXT, 0);
907 trsize = md_reloc_size * fixup_segment (text_fix_root,
908 SEG_TEXT);
909 subseg_change (SEG_DATA, 0);
910 drsize = md_reloc_size * fixup_segment (data_fix_root,
911 SEG_DATA);
912 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
913
914 /* FIXME move this stuff into the pre-write-hook */
915 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
916 H_SET_ENTRY_POINT (&headers, 0);
917
918 obj_pre_write_hook (&headers); /* extra coff stuff */
919
920 object_file_size = H_GET_FILE_SIZE (&headers);
921 next_object_file_charP = the_object_file = xmalloc (object_file_size);
922
923 output_file_create (out_file_name);
924
925 obj_header_append (&next_object_file_charP, &headers);
926
927 know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
928
929 /*
930 * Emit code.
931 */
932 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
933 {
934 register long count;
935 register char *fill_literal;
936 register long fill_size;
937
938 know (fragP->fr_type == rs_fill);
939 append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
940 fill_literal = fragP->fr_literal + fragP->fr_fix;
941 fill_size = fragP->fr_var;
942 know (fragP->fr_offset >= 0);
943
944 for (count = fragP->fr_offset; count; count--)
945 {
946 append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
947 } /* for each */
948
949 } /* for each code frag. */
950
951 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
952
953 /*
954 * Emit relocations.
955 */
956 obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
957 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)));
958 #ifdef TC_I960
959 /* Make addresses in data relocation directives relative to beginning of
960 * first data fragment, not end of last text fragment: alignment of the
961 * start of the data segment may place a gap between the segments.
962 */
963 obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
964 #else /* TC_I960 */
965 obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
966 #endif /* TC_I960 */
967
968 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)));
969
970 /*
971 * Emit line number entries.
972 */
973 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
974 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)));
975
976 /*
977 * Emit symbols.
978 */
979 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
980 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)));
981
982 /*
983 * Emit strings.
984 */
985
986 if (string_byte_count > 0)
987 {
988 obj_emit_strings (&next_object_file_charP);
989 } /* only if we have a string table */
990
991 #ifdef BFD_HEADERS
992 bfd_seek (stdoutput, 0, 0);
993 bfd_write (the_object_file, 1, object_file_size, stdoutput);
994 #else
995
996 /* Write the data to the file */
997 output_file_append (the_object_file, object_file_size, out_file_name);
998 #endif
999
1000 output_file_close (out_file_name);
1001 } /* non vms output */
1002 #else /* VMS */
1003 /*
1004 * Now do the VMS-dependent part of writing the object file
1005 */
1006 VMS_write_object_file (H_GET_TEXT_SIZE (&headers),
1007 H_GET_DATA_SIZE (&headers),
1008 H_GET_BSS_SIZE (&headers),
1009 text_frag_root, data_frag_root);
1010 #endif /* VMS */
1011 #else /* BFD_ASSEMBLER */
1012
1013 /* Set up symbol table, and write it out. */
1014 if (symbol_rootP)
1015 {
1016 int i = 0, n;
1017 symbolS *symp;
1018
1019 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1020 {
1021 S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
1022 /* So far, common symbols have been treated like undefined symbols.
1023 Put them in the common section now. */
1024 if (S_IS_DEFINED (symp) == 0
1025 && S_GET_VALUE (symp) != 0)
1026 S_SET_SEGMENT (symp, &bfd_com_section);
1027 #if 0
1028 printf ("symbol `%s'\n\t@%x: value=%d type=%d forward=%x seg=%s\n",
1029 S_GET_NAME (symp), symp,
1030 S_GET_VALUE (symp),
1031 S_GET_DATA_TYPE (symp),
1032 symp->sy_forward,
1033 segment_name (symp->bsym->section));
1034 #endif
1035 {
1036 int punt = 0;
1037 #ifdef obj_frob_symbol
1038 obj_frob_symbol (symp, punt);
1039 if (punt)
1040 goto punt_it;
1041 #endif
1042 #ifdef tc_frob_symbol
1043 tc_frob_symbol (symp, punt);
1044 if (punt)
1045 goto punt_it;
1046 #endif
1047 }
1048 /* If we don't want to keep this symbol, splice it out of the
1049 chain now. */
1050 if (S_IS_LOCAL (symp))
1051 {
1052 symbolS *prev, *next;
1053 punt_it:
1054 prev = symbol_previous (symp);
1055 next = symbol_next (symp);
1056 #ifdef DEBUG
1057 /* debugging: verify consistency */
1058 {
1059 symbolS *p = symp, *n = symp;
1060 while (symbol_previous (p))
1061 p = symbol_previous (p);
1062 while (symbol_next (n))
1063 n = symbol_next (n);
1064 verify_symbol_chain (p, n);
1065 }
1066 #endif
1067 if (prev)
1068 {
1069 symbol_next (prev) = next;
1070 symp = prev;
1071 }
1072 else
1073 abort ();
1074 if (next)
1075 symbol_previous (next) = prev;
1076 else
1077 symbol_lastP = prev;
1078 #ifdef DEBUG
1079 /* debugging: verify consistency */
1080 {
1081 symbolS *p = symp, *n = symp;
1082 while (symbol_previous (p))
1083 p = symbol_previous (p);
1084 while (symbol_next (n))
1085 n = symbol_next (n);
1086 verify_symbol_chain (p, n);
1087 }
1088 #endif
1089 continue;
1090 }
1091 i++;
1092 }
1093 n = i;
1094 if (n)
1095 {
1096 asymbol **asympp;
1097 boolean result;
1098
1099 asympp = (asymbol **) bfd_alloc (stdoutput, n * sizeof (asymbol *));
1100 symp = symbol_rootP;
1101 for (i = 0; i < n; i++, symp = symbol_next (symp))
1102 {
1103 asympp[i] = symp->bsym;
1104 symp->written = 1;
1105 }
1106 result = bfd_set_symtab (stdoutput, asympp, n);
1107 assert (result == true);
1108 }
1109 }
1110
1111 #ifdef obj_frob_file
1112 obj_frob_file ();
1113 #endif
1114
1115 /* Now that all the sizes are known, and contents correct, we can
1116 start writing the file. */
1117 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1118
1119 output_file_close (out_file_name);
1120 #endif /* BFD_ASSEMBLER */
1121 }
1122 #endif /* BFD */
1123
1124 /*
1125 * relax_segment()
1126 *
1127 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1128 * values.
1129 *
1130 * Relax the frags.
1131 *
1132 * After this, all frags in this segment have addresses that are correct
1133 * within the segment. Since segments live in different file addresses,
1134 * these frag addresses may not be the same as final object-file addresses.
1135 */
1136
1137
1138 static int
1139 is_dnrange (f1, f2)
1140 struct frag *f1;
1141 struct frag *f2;
1142 {
1143 for (; f1; f1 = f1->fr_next)
1144 if (f1->fr_next == f2)
1145 return 1;
1146 return 0;
1147 }
1148
1149 /* Relax_align. Advance location counter to next address that has 'alignment'
1150 lowest order bits all 0s. */
1151
1152 /* How many addresses does the .align take? */
1153 static relax_addressT
1154 relax_align (address, alignment)
1155 register relax_addressT address; /* Address now. */
1156 register long alignment; /* Alignment (binary). */
1157 {
1158 relax_addressT mask;
1159 relax_addressT new_address;
1160
1161 mask = ~((~0) << alignment);
1162 new_address = (address + mask) & (~mask);
1163 if (linkrelax)
1164 /* We must provide lots of padding, so the linker can discard it
1165 when needed. The linker will not add extra space, ever. */
1166 new_address += (1 << alignment);
1167 return (new_address - address);
1168 }
1169
1170 void
1171 relax_segment (segment_frag_root, segment)
1172 struct frag *segment_frag_root;
1173 segT segment;
1174 {
1175 register struct frag *fragP;
1176 register relax_addressT address;
1177 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1178 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
1179 #endif
1180 /* In case md_estimate_size_before_relax() wants to make fixSs. */
1181 subseg_change (segment, 0);
1182
1183 /* For each frag in segment: count and store (a 1st guess of)
1184 fr_address. */
1185 address = 0;
1186 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1187 {
1188 fragP->fr_address = address;
1189 address += fragP->fr_fix;
1190
1191 switch (fragP->fr_type)
1192 {
1193 case rs_fill:
1194 address += fragP->fr_offset * fragP->fr_var;
1195 break;
1196
1197 case rs_align:
1198 address += relax_align (address, fragP->fr_offset);
1199 break;
1200
1201 case rs_org:
1202 /* Assume .org is nugatory. It will grow with 1st relax. */
1203 break;
1204
1205 case rs_machine_dependent:
1206 address += md_estimate_size_before_relax (fragP, segment);
1207 break;
1208
1209 #ifndef WORKING_DOT_WORD
1210 /* Broken words don't concern us yet */
1211 case rs_broken_word:
1212 break;
1213 #endif
1214
1215 default:
1216 BAD_CASE (fragP->fr_type);
1217 break;
1218 } /* switch(fr_type) */
1219 } /* for each frag in the segment */
1220
1221 /* Do relax(). */
1222 {
1223 long stretch; /* May be any size, 0 or negative. */
1224 /* Cumulative number of addresses we have */
1225 /* relaxed this pass. */
1226 /* We may have relaxed more than one address. */
1227 long stretched; /* Have we stretched on this pass? */
1228 /* This is 'cuz stretch may be zero, when, in fact some piece of code
1229 grew, and another shrank. If a branch instruction doesn't fit anymore,
1230 we could be scrod. */
1231
1232 do
1233 {
1234 stretch = stretched = 0;
1235 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1236 {
1237 long growth = 0;
1238 unsigned long was_address;
1239 long offset;
1240 symbolS *symbolP;
1241 long target;
1242 long after;
1243 long aim;
1244
1245 was_address = fragP->fr_address;
1246 address = fragP->fr_address += stretch;
1247 symbolP = fragP->fr_symbol;
1248 offset = fragP->fr_offset;
1249
1250 switch (fragP->fr_type)
1251 {
1252 case rs_fill: /* .fill never relaxes. */
1253 growth = 0;
1254 break;
1255
1256 #ifndef WORKING_DOT_WORD
1257 /* JF: This is RMS's idea. I do *NOT* want to be blamed
1258 for it I do not want to write it. I do not want to have
1259 anything to do with it. This is not the proper way to
1260 implement this misfeature. */
1261 case rs_broken_word:
1262 {
1263 struct broken_word *lie;
1264 struct broken_word *untruth;
1265
1266 /* Yes this is ugly (storing the broken_word pointer
1267 in the symbol slot). Still, this whole chunk of
1268 code is ugly, and I don't feel like doing anything
1269 about it. Think of it as stubbornness in action. */
1270 growth = 0;
1271 for (lie = (struct broken_word *) (fragP->fr_symbol);
1272 lie && lie->dispfrag == fragP;
1273 lie = lie->next_broken_word)
1274 {
1275
1276 if (lie->added)
1277 continue;
1278
1279 offset = (lie->add->sy_frag->fr_address
1280 + S_GET_VALUE (lie->add)
1281 + lie->addnum
1282 - (lie->sub->sy_frag->fr_address
1283 + S_GET_VALUE (lie->sub)));
1284 if (offset <= -32768 || offset >= 32767)
1285 {
1286 if (flagseen['K'])
1287 as_warn (".word %s-%s+%ld didn't fit",
1288 S_GET_NAME (lie->add),
1289 S_GET_NAME (lie->sub),
1290 lie->addnum);
1291 lie->added = 1;
1292 if (fragP->fr_subtype == 0)
1293 {
1294 fragP->fr_subtype++;
1295 growth += md_short_jump_size;
1296 }
1297 for (untruth = lie->next_broken_word;
1298 untruth && untruth->dispfrag == lie->dispfrag;
1299 untruth = untruth->next_broken_word)
1300 if ((untruth->add->sy_frag == lie->add->sy_frag)
1301 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
1302 {
1303 untruth->added = 2;
1304 untruth->use_jump = lie;
1305 }
1306 growth += md_long_jump_size;
1307 }
1308 }
1309
1310 break;
1311 } /* case rs_broken_word */
1312 #endif
1313 case rs_align:
1314 growth = (relax_align ((relax_addressT) (address
1315 + fragP->fr_fix),
1316 offset)
1317 - relax_align ((relax_addressT) (was_address
1318 + fragP->fr_fix),
1319 offset));
1320 break;
1321
1322 case rs_org:
1323 target = offset;
1324
1325 if (symbolP)
1326 {
1327 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1328 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1329 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1330 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1331 || S_GET_SEGMENT (symbolP) == SEG_BSS);
1332 know (symbolP->sy_frag);
1333 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1334 || (symbolP->sy_frag == &zero_address_frag));
1335 #endif
1336 target += S_GET_VALUE (symbolP)
1337 + symbolP->sy_frag->fr_address;
1338 } /* if we have a symbol */
1339
1340 know (fragP->fr_next);
1341 after = fragP->fr_next->fr_address;
1342 growth = ((target - after) > 0) ? (target - after) : 0;
1343 /* Growth may be negative, but variable part of frag
1344 cannot have fewer than 0 chars. That is, we can't
1345 .org backwards. */
1346
1347 growth -= stretch; /* This is an absolute growth factor */
1348 break;
1349
1350 case rs_machine_dependent:
1351 {
1352 const relax_typeS *this_type;
1353 const relax_typeS *start_type;
1354 relax_substateT next_state;
1355 relax_substateT this_state;
1356
1357 this_state = fragP->fr_subtype;
1358 start_type = this_type = md_relax_table + this_state;
1359 target = offset;
1360
1361 if (symbolP)
1362 {
1363 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1364 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1365 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1366 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
1367 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
1368 #endif
1369 know (symbolP->sy_frag);
1370 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
1371 || symbolP->sy_frag == &zero_address_frag);
1372 target +=
1373 S_GET_VALUE (symbolP)
1374 + symbolP->sy_frag->fr_address;
1375
1376 /* If frag has yet to be reached on this pass,
1377 assume it will move by STRETCH just as we did.
1378 If this is not so, it will be because some frag
1379 between grows, and that will force another pass. */
1380
1381 /* JF was just address */
1382 /* JF also added is_dnrange hack */
1383 /* There's gotta be a better/faster/etc way
1384 to do this. . . */
1385 /* gnu@cygnus.com: I changed this from > to >=
1386 because I ran into a zero-length frag (fr_fix=0)
1387 which was created when the obstack needed a new
1388 chunk JUST AFTER the opcode of a branch. Since
1389 fr_fix is zero, fr_address of this frag is the same
1390 as fr_address of the next frag. This
1391 zero-length frag was variable and jumped to .+2
1392 (in the next frag), but since the > comparison
1393 below failed (the two were =, not >), "stretch"
1394 was not added to the target. Stretch was 178, so
1395 the offset appeared to be .-176 instead, which did
1396 not fit into a byte branch, so the assembler
1397 relaxed the branch to a word. This didn't compare
1398 with what happened when the same source file was
1399 assembled on other machines, which is how I found it.
1400 You might want to think about what other places have
1401 trouble with zero length frags... */
1402
1403 if (symbolP->sy_frag->fr_address >= was_address
1404 && is_dnrange (fragP, symbolP->sy_frag))
1405 {
1406 target += stretch;
1407 }
1408 } /* if there's a symbol attached */
1409
1410 aim = target - address - fragP->fr_fix;
1411 /* The displacement is affected by the instruction size
1412 for the 32k architecture. I think we ought to be able
1413 to add fragP->fr_pcrel_adjust in all cases (it should be
1414 zero if not used), but just in case it breaks something
1415 else we'll put this inside #ifdef NS32K ... #endif */
1416 #ifdef TC_NS32K
1417 aim += fragP->fr_pcrel_adjust;
1418 #endif /* TC_NS32K */
1419
1420 if (aim < 0)
1421 {
1422 /* Look backwards. */
1423 for (next_state = this_type->rlx_more; next_state;)
1424 if (aim >= this_type->rlx_backward)
1425 next_state = 0;
1426 else
1427 {
1428 /* Grow to next state. */
1429 this_state = next_state;
1430 this_type = md_relax_table + this_state;
1431 next_state = this_type->rlx_more;
1432 }
1433 }
1434 else
1435 {
1436 #ifdef M68K_AIM_KLUDGE
1437 M68K_AIM_KLUDGE (aim, this_state, this_type);
1438 #endif
1439 /* Look forwards. */
1440 for (next_state = this_type->rlx_more; next_state;)
1441 if (aim <= this_type->rlx_forward)
1442 next_state = 0;
1443 else
1444 {
1445 /* Grow to next state. */
1446 this_state = next_state;
1447 this_type = md_relax_table + this_state;
1448 next_state = this_type->rlx_more;
1449 }
1450 }
1451
1452 growth = this_type->rlx_length - start_type->rlx_length;
1453 if (growth != 0)
1454 fragP->fr_subtype = this_state;
1455 }
1456 break;
1457
1458 default:
1459 BAD_CASE (fragP->fr_type);
1460 break;
1461 }
1462 if (growth)
1463 {
1464 stretch += growth;
1465 stretched++;
1466 }
1467 } /* For each frag in the segment. */
1468 }
1469 while (stretched); /* Until nothing further to relax. */
1470 } /* do_relax */
1471
1472 /*
1473 * We now have valid fr_address'es for each frag.
1474 */
1475
1476 /*
1477 * All fr_address's are correct, relative to their own segment.
1478 * We have made all the fixS we will ever make.
1479 */
1480 } /* relax_segment() */
1481
1482 /* fixup_segment()
1483
1484 Go through all the fixS's in a segment and see which ones can be
1485 handled now. (These consist of fixS where we have since discovered
1486 the value of a symbol, or the address of the frag involved.)
1487 For each one, call md_apply_fix to put the fix into the frag data.
1488
1489 Result is a count of how many relocation structs will be needed to
1490 handle the remaining fixS's that we couldn't completely handle here.
1491 These will be output later by emit_relocations(). */
1492
1493 static long
1494 fixup_segment (fixP, this_segment_type)
1495 register fixS *fixP;
1496 segT this_segment_type; /* N_TYPE bits for segment. */
1497 {
1498 register long seg_reloc_count;
1499 register symbolS *add_symbolP;
1500 register symbolS *sub_symbolP;
1501 long add_number;
1502 register int size;
1503 register char *place;
1504 register long where;
1505 register char pcrel;
1506 register fragS *fragP;
1507 register segT add_symbol_segment = absolute_section;
1508
1509 seg_reloc_count = 0;
1510 /* If the linker is doing the relaxing, we must not do any fixups */
1511 if (linkrelax)
1512 for (; fixP; fixP = fixP->fx_next)
1513 seg_reloc_count++;
1514 else
1515 for (; fixP; fixP = fixP->fx_next)
1516 {
1517 fragP = fixP->fx_frag;
1518 know (fragP);
1519 where = fixP->fx_where;
1520 place = fragP->fr_literal + where;
1521 size = fixP->fx_size;
1522 add_symbolP = fixP->fx_addsy;
1523 #ifdef TC_I960
1524 if (fixP->fx_callj && TC_S_IS_CALLNAME (add_symbolP))
1525 {
1526 /* Relocation should be done via the associated 'bal'
1527 entry point symbol. */
1528
1529 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP)))
1530 {
1531 as_bad ("No 'bal' entry point for leafproc %s",
1532 S_GET_NAME (add_symbolP));
1533 continue;
1534 }
1535 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
1536 }
1537 #endif
1538 sub_symbolP = fixP->fx_subsy;
1539 add_number = fixP->fx_offset;
1540 pcrel = fixP->fx_pcrel;
1541
1542 if (add_symbolP)
1543 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
1544
1545 if (sub_symbolP)
1546 {
1547 if (!add_symbolP)
1548 {
1549 /* Its just -sym */
1550 if (S_GET_SEGMENT (sub_symbolP) != absolute_section)
1551 as_bad ("Negative of non-absolute symbol %s",
1552 S_GET_NAME (sub_symbolP));
1553
1554 add_number -= S_GET_VALUE (sub_symbolP);
1555 }
1556 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
1557 && (SEG_NORMAL (add_symbol_segment)
1558 || (add_symbol_segment == absolute_section)))
1559 {
1560 /* Difference of 2 symbols from same segment.
1561 Can't make difference of 2 undefineds: 'value' means
1562 something different for N_UNDF. */
1563 #ifdef TC_I960
1564 /* Makes no sense to use the difference of 2 arbitrary symbols
1565 as the target of a call instruction. */
1566 if (fixP->fx_callj)
1567 {
1568 as_bad ("callj to difference of 2 symbols");
1569 }
1570 #endif /* TC_I960 */
1571 add_number += S_GET_VALUE (add_symbolP) -
1572 S_GET_VALUE (sub_symbolP);
1573
1574 add_symbolP = NULL;
1575 fixP->fx_addsy = NULL;
1576 }
1577 else
1578 {
1579 /* Different segments in subtraction. */
1580 know (!(S_IS_EXTERNAL (sub_symbolP)
1581 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
1582
1583 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
1584 {
1585 add_number -= S_GET_VALUE (sub_symbolP);
1586 }
1587 else
1588 {
1589 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
1590 segment_name (S_GET_SEGMENT (sub_symbolP)),
1591 S_GET_NAME (sub_symbolP), fragP->fr_address + where);
1592 } /* if absolute */
1593 }
1594 } /* if sub_symbolP */
1595
1596 if (add_symbolP)
1597 {
1598 if (add_symbol_segment == this_segment_type && pcrel)
1599 {
1600 /*
1601 * This fixup was made when the symbol's segment was
1602 * SEG_UNKNOWN, but it is now in the local segment.
1603 * So we know how to do the address without relocation.
1604 */
1605 #ifdef TC_I960
1606 /* reloc_callj() may replace a 'call' with a 'calls' or a
1607 'bal', in which cases it modifies *fixP as appropriate.
1608 In the case of a 'calls', no further work is required,
1609 and *fixP has been set up to make the rest of the code
1610 below a no-op. */
1611 reloc_callj (fixP);
1612 #endif /* TC_I960 */
1613
1614 add_number += S_GET_VALUE (add_symbolP);
1615 add_number -= md_pcrel_from (fixP);
1616 pcrel = 0; /* Lie. Don't want further pcrel processing. */
1617 fixP->fx_addsy = NULL; /* No relocations please. */
1618 }
1619 else
1620 {
1621 if (add_symbol_segment == absolute_section)
1622 {
1623 #ifdef TC_I960
1624 /* See comment about reloc_callj() above. */
1625 reloc_callj (fixP);
1626 #endif /* TC_I960 */
1627 add_number += S_GET_VALUE (add_symbolP);
1628 fixP->fx_addsy = NULL;
1629 add_symbolP = NULL;
1630 }
1631 else if (add_symbol_segment == undefined_section
1632 #ifdef BFD_ASSEMBLER
1633 || add_symbol_segment == &bfd_com_section
1634 #endif
1635 )
1636 {
1637 #ifdef TC_I960
1638 if ((int) fixP->fx_bit_fixP == 13)
1639 {
1640 /* This is a COBR instruction. They have only a
1641 * 13-bit displacement and are only to be used
1642 * for local branches: flag as error, don't generate
1643 * relocation.
1644 */
1645 as_bad ("can't use COBR format with external label");
1646 fixP->fx_addsy = NULL; /* No relocations please. */
1647 continue;
1648 } /* COBR */
1649 #endif /* TC_I960 */
1650
1651 #ifdef OBJ_COFF
1652 #ifdef TE_I386AIX
1653 if (S_IS_COMMON (add_symbolP))
1654 add_number += S_GET_VALUE (add_symbolP);
1655 #endif /* TE_I386AIX */
1656 #endif /* OBJ_COFF */
1657 ++seg_reloc_count;
1658 }
1659 else
1660 {
1661 seg_reloc_count++;
1662 add_number += S_GET_VALUE (add_symbolP);
1663 }
1664 } /* if not in local seg */
1665 } /* if there was a + symbol */
1666
1667 if (pcrel)
1668 {
1669 add_number -= md_pcrel_from (fixP);
1670 if (add_symbolP == 0)
1671 {
1672 fixP->fx_addsy = &abs_symbol;
1673 ++seg_reloc_count;
1674 } /* if there's an add_symbol */
1675 } /* if pcrel */
1676
1677 if (!fixP->fx_bit_fixP)
1678 {
1679 if ((size == 1 &&
1680 (add_number & ~0xFF)
1681 && ((add_number & ~0xFF) != (-1 & ~0xFF)))
1682 || (size == 2
1683 && (add_number & ~0xFFFF)
1684 && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
1685 {
1686 as_bad ("Value of %d too large for field of %d bytes at 0x%x",
1687 add_number, size, fragP->fr_address + where);
1688 } /* generic error checking */
1689 #ifdef WARN_SIGNED_OVERFLOW_WORD
1690 /* Warn if a .word value is too large when treated as a signed
1691 number. We already know it is not too negative. This is to
1692 catch over-large switches generated by gcc on the 68k. */
1693 if (!flagseen['J']
1694 && size == 2
1695 && add_number > 0x7fff)
1696 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
1697 add_number, fragP->fr_address + where);
1698 #endif
1699 } /* not a bit fix */
1700
1701 #ifdef BFD_ASSEMBLER
1702 md_apply_fix (fixP, &add_number);
1703 #else
1704 md_apply_fix (fixP, add_number);
1705 #endif
1706 } /* For each fixS in this segment. */
1707
1708 #ifdef OBJ_COFF
1709 #ifdef TC_I960
1710 {
1711 fixS *topP = fixP;
1712
1713 /* two relocs per callj under coff. */
1714 for (fixP = topP; fixP; fixP = fixP->fx_next)
1715 {
1716 if (fixP->fx_callj && fixP->fx_addsy != 0)
1717 {
1718 ++seg_reloc_count;
1719 } /* if callj and not already fixed. */
1720 } /* for each fix */
1721 }
1722 #endif /* TC_I960 */
1723
1724 #endif /* OBJ_COFF */
1725 return (seg_reloc_count);
1726 } /* fixup_segment() */
1727
1728 /* end of write.c */
This page took 0.066799 seconds and 4 git commands to generate.