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