Test common synthetic insns (applicable to v8 or v9).
[platform/upstream/binutils.git] / gas / write.c
1 /* write.c - emit .o file
2    Copyright (C) 1986, 87, 90, 91, 92, 93, 94, 95, 1996
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* This thing should be set up to do byteordering correctly.  But... */
22
23 #include "as.h"
24 #include "subsegs.h"
25 #include "obstack.h"
26 #include "output-file.h"
27
28 /* This looks like a good idea.  Let's try turning it on always, for now.  */
29 #undef  BFD_FAST_SECTION_FILL
30 #define BFD_FAST_SECTION_FILL
31
32 /* The NOP_OPCODE is for the alignment fill value.  Fill it with a nop
33    instruction so that the disassembler does not choke on it.  */
34 #ifndef NOP_OPCODE
35 #define NOP_OPCODE 0x00
36 #endif
37
38 #ifndef TC_ADJUST_RELOC_COUNT
39 #define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
40 #endif
41
42 #ifndef TC_FORCE_RELOCATION
43 #define TC_FORCE_RELOCATION(FIXP) 0
44 #endif
45
46 #ifndef TC_FORCE_RELOCATION_SECTION
47 #define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
48 #endif
49
50 #ifndef MD_PCREL_FROM_SECTION
51 #define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
52 #endif
53
54 #ifndef WORKING_DOT_WORD
55 extern CONST int md_short_jump_size;
56 extern CONST int md_long_jump_size;
57 #endif
58
59 int symbol_table_frozen;
60 void print_fixup PARAMS ((fixS *));
61
62 #ifdef BFD_ASSEMBLER
63 static void renumber_sections PARAMS ((bfd *, asection *, PTR));
64
65 /* We generally attach relocs to frag chains.  However, after we have
66    chained these all together into a segment, any relocs we add after
67    that must be attached to a segment.  This will include relocs added
68    in md_estimate_size_for_relax, for example.  */
69 static int frags_chained = 0;
70 #endif
71
72 #ifndef BFD_ASSEMBLER
73
74 #ifndef MANY_SEGMENTS
75 struct frag *text_frag_root;
76 struct frag *data_frag_root;
77 struct frag *bss_frag_root;
78
79 struct frag *text_last_frag;    /* Last frag in segment. */
80 struct frag *data_last_frag;    /* Last frag in segment. */
81 static struct frag *bss_last_frag;      /* Last frag in segment. */
82 #endif
83
84 #ifndef BFD
85 static object_headers headers;
86 #endif
87
88 long string_byte_count;
89 char *next_object_file_charP;   /* Tracks object file bytes. */
90
91 #ifndef OBJ_VMS
92 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
93 #endif
94
95 #endif /* BFD_ASSEMBLER */
96
97 static int n_fixups;
98
99 #ifdef BFD_ASSEMBLER
100 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
101                                        symbolS *add, symbolS *sub,
102                                        offsetT offset, int pcrel,
103                                        bfd_reloc_code_real_type r_type));
104 #else
105 static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
106                                        symbolS *add, symbolS *sub,
107                                        offsetT offset, int pcrel,
108                                        int r_type));
109 #endif
110 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
111 static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
112 #endif
113 static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
114
115 /*
116  *                      fix_new()
117  *
118  * Create a fixS in obstack 'notes'.
119  */
120 static fixS *
121 fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
122                   r_type)
123      fragS *frag;               /* Which frag? */
124      int where;                 /* Where in that frag? */
125      int size;                  /* 1, 2, or 4 usually. */
126      symbolS *add_symbol;       /* X_add_symbol. */
127      symbolS *sub_symbol;       /* X_op_symbol. */
128      offsetT offset;            /* X_add_number. */
129      int pcrel;                 /* TRUE if PC-relative relocation. */
130 #ifdef BFD_ASSEMBLER
131      bfd_reloc_code_real_type r_type; /* Relocation type */
132 #else
133      int r_type;                /* Relocation type */
134 #endif
135 {
136   fixS *fixP;
137
138   n_fixups++;
139
140   fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
141
142   fixP->fx_frag = frag;
143   fixP->fx_where = where;
144   fixP->fx_size = size;
145   /* We've made fx_size a narrow field; check that it's wide enough.  */
146   if (fixP->fx_size != size)
147     {
148       as_bad ("field fx_size too small to hold %d", size);
149       abort ();
150     }
151   fixP->fx_addsy = add_symbol;
152   fixP->fx_subsy = sub_symbol;
153   fixP->fx_offset = offset;
154   fixP->fx_pcrel = pcrel;
155   fixP->fx_plt = 0;
156 #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
157   fixP->fx_r_type = r_type;
158 #endif
159   fixP->fx_im_disp = 0;
160   fixP->fx_pcrel_adjust = 0;
161   fixP->fx_bit_fixP = 0;
162   fixP->fx_addnumber = 0;
163   fixP->fx_tcbit = 0;
164   fixP->fx_done = 0;
165
166 #ifdef TC_FIX_TYPE
167   TC_INIT_FIX_DATA(fixP);
168 #endif
169
170   as_where (&fixP->fx_file, &fixP->fx_line);
171
172   /* Usually, we want relocs sorted numerically, but while
173      comparing to older versions of gas that have relocs
174      reverse sorted, it is convenient to have this compile
175      time option.  xoxorich. */
176
177   {
178
179 #ifdef BFD_ASSEMBLER
180     fixS **seg_fix_rootP = (frags_chained
181                             ? &seg_info (now_seg)->fix_root
182                             : &frchain_now->fix_root);
183     fixS **seg_fix_tailP = (frags_chained
184                             ? &seg_info (now_seg)->fix_tail
185                             : &frchain_now->fix_tail);
186 #endif
187
188 #ifdef REVERSE_SORT_RELOCS
189
190     fixP->fx_next = *seg_fix_rootP;
191     *seg_fix_rootP = fixP;
192
193 #else /* REVERSE_SORT_RELOCS */
194
195     fixP->fx_next = NULL;
196
197     if (*seg_fix_tailP)
198       (*seg_fix_tailP)->fx_next = fixP;
199     else
200       *seg_fix_rootP = fixP;
201     *seg_fix_tailP = fixP;
202
203 #endif /* REVERSE_SORT_RELOCS */
204
205   }
206
207   return fixP;
208 }
209
210 /* Create a fixup relative to a symbol (plus a constant).  */
211
212 fixS *
213 fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
214      fragS *frag;               /* Which frag? */
215      int where;                 /* Where in that frag? */
216      int size;                  /* 1, 2, or 4 usually. */
217      symbolS *add_symbol;       /* X_add_symbol. */
218      offsetT offset;            /* X_add_number. */
219      int pcrel;                 /* TRUE if PC-relative relocation. */
220 #ifdef BFD_ASSEMBLER
221      bfd_reloc_code_real_type r_type; /* Relocation type */
222 #else
223      int r_type;                /* Relocation type */
224 #endif
225 {
226   return fix_new_internal (frag, where, size, add_symbol,
227                            (symbolS *) NULL, offset, pcrel, r_type);
228 }
229
230 /* Create a fixup for an expression.  Currently we only support fixups
231    for difference expressions.  That is itself more than most object
232    file formats support anyhow.  */
233
234 fixS *
235 fix_new_exp (frag, where, size, exp, pcrel, r_type)
236      fragS *frag;               /* Which frag? */
237      int where;                 /* Where in that frag? */
238      int size;                  /* 1, 2, or 4 usually. */
239      expressionS *exp;          /* Expression.  */
240      int pcrel;                 /* TRUE if PC-relative relocation. */
241 #ifdef BFD_ASSEMBLER
242      bfd_reloc_code_real_type r_type; /* Relocation type */
243 #else
244      int r_type;                /* Relocation type */
245 #endif
246 {
247   symbolS *add = NULL;
248   symbolS *sub = NULL;
249   offsetT off = 0;
250
251   switch (exp->X_op)
252     {
253     case O_absent:
254       break;
255
256     case O_add:
257       /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
258          the difference expression cannot immediately be reduced.  */
259       {
260         extern symbolS *make_expr_symbol ();
261         symbolS *stmp = make_expr_symbol (exp);
262         exp->X_op = O_symbol;
263         exp->X_op_symbol = 0;
264         exp->X_add_symbol = stmp;
265         exp->X_add_number = 0;
266         return fix_new_exp (frag, where, size, exp, pcrel, r_type);
267       }
268
269     case O_symbol_rva:
270       add = exp->X_add_symbol;
271       off = exp->X_add_number;
272
273 #if defined(BFD_ASSEMBLER)
274       r_type = BFD_RELOC_RVA;
275 #elif defined(TC_RVA_RELOC)
276       r_type = TC_RVA_RELOC;
277 #else
278       as_fatal("rva not supported");
279 #endif
280       break;
281
282     case O_uminus:
283       sub = exp->X_add_symbol;
284       off = exp->X_add_number;
285       break;
286
287     case O_subtract:
288       sub = exp->X_op_symbol;
289       /* Fall through.  */
290     case O_symbol:
291       add = exp->X_add_symbol;
292       /* Fall through.   */
293     case O_constant:
294       off = exp->X_add_number;
295       break;
296       
297     default:
298       as_bad ("expression too complex for fixup");
299     }
300
301   return fix_new_internal (frag, where, size, add, sub, off,
302                            pcrel, r_type);
303 }
304
305 /* Append a string onto another string, bumping the pointer along.  */
306 void
307 append (charPP, fromP, length)
308      char **charPP;
309      char *fromP;
310      unsigned long length;
311 {
312   /* Don't trust memcpy() of 0 chars. */
313   if (length == 0)
314     return;
315
316   memcpy (*charPP, fromP, length);
317   *charPP += length;
318 }
319
320 #ifndef BFD_ASSEMBLER 
321 int section_alignment[SEG_MAXIMUM_ORDINAL];
322 #endif
323
324 /*
325  * This routine records the largest alignment seen for each segment.
326  * If the beginning of the segment is aligned on the worst-case
327  * boundary, all of the other alignments within it will work.  At
328  * least one object format really uses this info.
329  */
330 void 
331 record_alignment (seg, align)
332      /* Segment to which alignment pertains */
333      segT seg;
334      /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
335         boundary, etc.)  */
336      int align;
337 {
338   if (seg == absolute_section)
339     return;
340 #ifdef BFD_ASSEMBLER
341   if (align > bfd_get_section_alignment (stdoutput, seg))
342     bfd_set_section_alignment (stdoutput, seg, align);
343 #else
344   if (align > section_alignment[(int) seg])
345     section_alignment[(int) seg] = align;
346 #endif
347 }
348
349 #ifdef BFD_ASSEMBLER
350
351 /* Reset the section indices after removing the gas created sections.  */
352
353 static void
354 renumber_sections (abfd, sec, countparg)
355      bfd *abfd;
356      asection *sec;
357      PTR countparg;
358 {
359   int *countp = (int *) countparg;
360
361   sec->index = *countp;
362   ++*countp;
363 }
364
365 #endif /* defined (BFD_ASSEMBLER) */
366
367 #if defined (BFD_ASSEMBLER) || ! defined (BFD)
368
369 static fragS *
370 chain_frchains_together_1 (section, frchp)
371      segT section;
372      struct frchain *frchp;
373 {
374   fragS dummy, *prev_frag = &dummy;
375 #ifdef BFD_ASSEMBLER
376   fixS fix_dummy, *prev_fix = &fix_dummy;
377 #endif
378
379   for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
380     {
381       prev_frag->fr_next = frchp->frch_root;
382       prev_frag = frchp->frch_last;
383       assert (prev_frag->fr_type != 0);
384 #ifdef BFD_ASSEMBLER
385       if (frchp->fix_root != (fixS *) NULL)
386         {
387           if (seg_info (section)->fix_root == (fixS *) NULL)
388             seg_info (section)->fix_root = frchp->fix_root;
389           prev_fix->fx_next = frchp->fix_root;
390           seg_info (section)->fix_tail = frchp->fix_tail;
391           prev_fix = frchp->fix_tail;
392         }
393 #endif
394     }
395   assert (prev_frag->fr_type != 0);
396   prev_frag->fr_next = 0;
397   return prev_frag;
398 }
399
400 #endif
401
402 #ifdef BFD_ASSEMBLER
403
404 static void
405 chain_frchains_together (abfd, section, xxx)
406      bfd *abfd;                 /* unused */
407      segT section;
408      PTR xxx;                   /* unused */
409 {
410   segment_info_type *info;
411
412   /* BFD may have introduced its own sections without using
413      subseg_new, so it is possible that seg_info is NULL.  */
414   info = seg_info (section);
415   if (info != (segment_info_type *) NULL)
416    info->frchainP->frch_last
417      = chain_frchains_together_1 (section, info->frchainP);
418
419   /* Now that we've chained the frags together, we must add new fixups
420      to the segment, not to the frag chain.  */
421   frags_chained = 1;
422 }
423
424 #endif
425
426 #if !defined (BFD) && !defined (BFD_ASSEMBLER)
427
428 void 
429 remove_subsegs (head, seg, root, last)
430      frchainS *head;
431      int seg;
432      fragS **root;
433      fragS **last;
434 {
435   *root = head->frch_root;
436   *last = chain_frchains_together_1 (seg, head);
437 }
438
439 #endif /* BFD */
440
441 #if defined (BFD_ASSEMBLER) || !defined (BFD)
442
443 #ifdef BFD_ASSEMBLER
444 static void
445 cvt_frag_to_fill (sec, fragP)
446      segT sec;
447      fragS *fragP;
448 #else
449 static void
450 cvt_frag_to_fill (headersP, sec, fragP)
451      object_headers *headersP;
452      segT sec;
453      fragS *fragP;
454 #endif
455 {
456   switch (fragP->fr_type)
457     {
458     case rs_align:
459     case rs_align_code:
460     case rs_org:
461     case rs_space:
462 #ifdef HANDLE_ALIGN
463       HANDLE_ALIGN (fragP);
464 #endif
465       know (fragP->fr_next != NULL);
466       fragP->fr_offset = (fragP->fr_next->fr_address
467                           - fragP->fr_address
468                           - fragP->fr_fix) / fragP->fr_var;
469       if (fragP->fr_offset < 0)
470         {
471           as_bad ("attempt to .org/.space backwards? (%ld)",
472                   (long) fragP->fr_offset);
473         }
474       fragP->fr_type = rs_fill;
475       break;
476
477     case rs_fill:
478       break;
479
480     case rs_machine_dependent:
481 #ifdef BFD_ASSEMBLER
482       md_convert_frag (stdoutput, sec, fragP);
483 #else
484       md_convert_frag (headersP, sec, fragP);
485 #endif
486
487       assert (fragP->fr_next == NULL || (fragP->fr_next->fr_address - fragP->fr_address == fragP->fr_fix));
488
489       /*
490        * After md_convert_frag, we make the frag into a ".space 0".
491        * Md_convert_frag() should set up any fixSs and constants
492        * required.
493        */
494       frag_wane (fragP);
495       break;
496
497 #ifndef WORKING_DOT_WORD
498     case rs_broken_word:
499       {
500         struct broken_word *lie;
501
502         if (fragP->fr_subtype)
503           {
504             fragP->fr_fix += md_short_jump_size;
505             for (lie = (struct broken_word *) (fragP->fr_symbol);
506                  lie && lie->dispfrag == fragP;
507                  lie = lie->next_broken_word)
508               if (lie->added == 1)
509                 fragP->fr_fix += md_long_jump_size;
510           }
511         frag_wane (fragP);
512       }
513       break;
514 #endif
515
516     default:
517       BAD_CASE (fragP->fr_type);
518       break;
519     }
520 }
521
522 #endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
523
524 #ifdef BFD_ASSEMBLER
525 static void
526 relax_and_size_seg (abfd, sec, xxx)
527      bfd *abfd;
528      asection *sec;
529      PTR xxx;
530 {
531   flagword flags;
532   fragS *fragp;
533   segment_info_type *seginfo;
534   int x;
535   valueT size, newsize;
536
537   subseg_change (sec, 0);
538
539   flags = bfd_get_section_flags (abfd, sec);
540
541   seginfo = seg_info (sec);
542   if (seginfo && seginfo->frchainP)
543     {
544       relax_segment (seginfo->frchainP->frch_root, sec);
545       for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
546         cvt_frag_to_fill (sec, fragp);
547       for (fragp = seginfo->frchainP->frch_root;
548            fragp->fr_next;
549            fragp = fragp->fr_next)
550         /* walk to last elt */;
551       size = fragp->fr_address + fragp->fr_fix;
552     }
553   else
554     size = 0;
555
556   if (size > 0 && ! seginfo->bss)
557     flags |= SEC_HAS_CONTENTS;
558
559   /* @@ This is just an approximation.  */
560   if (seginfo && seginfo->fix_root)
561     flags |= SEC_RELOC;
562   else
563     flags &= ~SEC_RELOC;
564   x = bfd_set_section_flags (abfd, sec, flags);
565   assert (x == true);
566
567   newsize = md_section_align (sec, size);
568   x = bfd_set_section_size (abfd, sec, newsize);
569   assert (x == true);
570
571   /* If the size had to be rounded up, add some padding in the last
572      non-empty frag.  */
573   assert (newsize >= size);
574   if (size != newsize)
575     {
576       fragS *last = seginfo->frchainP->frch_last;
577       fragp = seginfo->frchainP->frch_root;
578       while (fragp->fr_next != last)
579         fragp = fragp->fr_next;
580       last->fr_address = size;
581       fragp->fr_offset += newsize - size;
582     }
583
584 #ifdef tc_frob_section
585   tc_frob_section (sec);
586 #endif
587 #ifdef obj_frob_section
588   obj_frob_section (sec);
589 #endif
590 }
591
592 #ifdef DEBUG2
593 static void
594 dump_section_relocs (abfd, sec, stream_)
595      bfd *abfd;
596      asection *sec;
597      char *stream_;
598 {
599   FILE *stream = (FILE *) stream_;
600   segment_info_type *seginfo = seg_info (sec);
601   fixS *fixp = seginfo->fix_root;
602
603   if (!fixp)
604     return;
605
606   fprintf (stream, "sec %s relocs:\n", sec->name);
607   while (fixp)
608     {
609       symbolS *s = fixp->fx_addsy;
610       if (s)
611         {
612           fprintf (stream, "  %08x: %s(%s", fixp, S_GET_NAME (s),
613                    s->bsym->section->name);
614           if (s->bsym->flags & BSF_SECTION_SYM)
615             {
616               fprintf (stream, " section sym");
617               if (S_GET_VALUE (s))
618                 fprintf (stream, "+%x", S_GET_VALUE (s));
619             }
620           else
621             fprintf (stream, "+%x", S_GET_VALUE (s));
622           fprintf (stream, ")+%x\n", fixp->fx_offset);
623         }
624       else
625         fprintf (stream, "  %08x: type %d no sym\n", fixp, fixp->fx_r_type);
626       fixp = fixp->fx_next;
627     }
628 }
629 #else
630 #define dump_section_relocs(ABFD,SEC,STREAM)    (void)(ABFD,SEC,STREAM)
631 #endif
632
633 #ifndef EMIT_SECTION_SYMBOLS
634 #define EMIT_SECTION_SYMBOLS 1
635 #endif
636
637 static void
638 adjust_reloc_syms (abfd, sec, xxx)
639      bfd *abfd;
640      asection *sec;
641      PTR xxx;
642 {
643   segment_info_type *seginfo = seg_info (sec);
644   fixS *fixp;
645
646   if (seginfo == NULL)
647     return;
648
649   dump_section_relocs (abfd, sec, stderr);
650
651   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
652     if (fixp->fx_done)
653       /* ignore it */;
654     else if (fixp->fx_addsy)
655       {
656         symbolS *sym;
657         asection *symsec;
658
659       reduce_fixup:
660
661 #ifdef DEBUG5
662         fprintf (stderr, "\n\nadjusting fixup:\n");
663         print_fixup (fixp);
664 #endif
665
666         sym = fixp->fx_addsy;
667         symsec = sym->bsym->section;
668
669         if (sym != NULL && sym->sy_mri_common)
670           {
671             /* These symbols are handled specially in fixup_segment.  */
672             goto done;
673           }
674
675         /* If it's one of these sections, assume the symbol is
676            definitely going to be output.  The code in
677            md_estimate_size_before_relax in tc-mips.c uses this test
678            as well, so if you change this code you should look at that
679            code.  */
680         if (bfd_is_und_section (symsec)
681             || bfd_is_abs_section (symsec)
682             || bfd_is_com_section (symsec))
683           {
684             fixp->fx_addsy->sy_used_in_reloc = 1;
685 #ifdef UNDEFINED_DIFFERENCE_OK
686             /* We have the difference of an undefined symbol and some
687                other symbol.  Make sure to mark the other symbol as used
688                in a relocation so that it will always be output.  */
689             if (fixp->fx_subsy)
690               fixp->fx_subsy->sy_used_in_reloc = 1;
691 #endif
692             goto done;
693           }
694
695         /* Since we're reducing to section symbols, don't attempt to reduce
696            anything that's already using one.  */
697         if (sym->bsym->flags & BSF_SECTION_SYM)
698           {
699             fixp->fx_addsy->sy_used_in_reloc = 1;
700             goto done;
701           }
702
703         /* Is there some other reason we can't adjust this one?  (E.g.,
704            call/bal links in i960-bout symbols.)  */
705 #ifdef obj_fix_adjustable
706         if (! obj_fix_adjustable (fixp))
707           {
708             fixp->fx_addsy->sy_used_in_reloc = 1;
709             goto done;
710           }
711 #endif
712
713         /* Is there some other (target cpu dependent) reason we can't adjust
714            this one?  (E.g. relocations involving function addresses on 
715            the PA.  */
716 #ifdef tc_fix_adjustable
717         if (! tc_fix_adjustable (fixp))
718           {
719             fixp->fx_addsy->sy_used_in_reloc = 1;
720             goto done;
721           }
722 #endif
723
724         /* For PIC support: We may get expressions like
725            "_GLOBAL_OFFSET_TABLE_+(.-L5)" where "." and "L5" may not
726            necessarily have had a fixed difference initially.  But now
727            it should be a known constant, so we can reduce it.  Since
728            we can't easily handle a symbol value that looks like
729            someUndefinedSymbol+const, though, we convert the fixup to
730            access the undefined symbol directly, and discard the
731            intermediate symbol.  */
732         if (S_GET_SEGMENT (sym) == expr_section
733             && sym->sy_value.X_op == O_add
734             && (resolve_symbol_value (sym->sy_value.X_add_symbol),
735                 S_GET_SEGMENT (sym->sy_value.X_add_symbol) == undefined_section)
736             && (resolve_symbol_value (sym->sy_value.X_op_symbol),
737                 S_GET_SEGMENT (sym->sy_value.X_op_symbol) == absolute_section))
738           {
739             fixp->fx_offset += S_GET_VALUE (sym->sy_value.X_op_symbol);
740             fixp->fx_offset += sym->sy_value.X_add_number;
741             fixp->fx_addsy = sym->sy_value.X_add_symbol;
742             goto reduce_fixup;
743           }
744
745         /* If the section symbol isn't going to be output, the relocs
746            at least should still work.  If not, figure out what to do
747            when we run into that case.  */
748         fixp->fx_offset += S_GET_VALUE (sym);
749         fixp->fx_addsy = section_symbol (symsec);
750         fixp->fx_addsy->sy_used_in_reloc = 1;
751
752       done:
753         ;
754       }
755 #if 1/*def RELOC_REQUIRES_SYMBOL*/
756     else
757       {
758         /* There was no symbol required by this relocation.  However,
759            BFD doesn't really handle relocations without symbols well.
760            (At least, the COFF support doesn't.)  So for now we fake up
761            a local symbol in the absolute section.  */
762
763         fixp->fx_addsy = section_symbol (absolute_section);
764 /*      fixp->fx_addsy->sy_used_in_reloc = 1; */
765       }
766 #endif
767
768   dump_section_relocs (abfd, sec, stderr);
769 }
770
771 static void
772 write_relocs (abfd, sec, xxx)
773      bfd *abfd;
774      asection *sec;
775      PTR xxx;
776 {
777   segment_info_type *seginfo = seg_info (sec);
778   int i;
779   unsigned int n;
780   arelent **relocs;
781   fixS *fixp;
782   char *err;
783
784   /* If seginfo is NULL, we did not create this section; don't do
785      anything with it.  */
786   if (seginfo == NULL)
787     return;
788
789   fixup_segment (seginfo->fix_root, sec);
790
791   n = 0;
792   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
793     n++;
794
795 #ifndef RELOC_EXPANSION_POSSIBLE
796   /* Set up reloc information as well.  */
797   relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
798                                              n * sizeof (arelent *));
799   memset ((char*)relocs, 0, n * sizeof (arelent*));
800
801   i = 0;
802   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
803     {
804       arelent *reloc;
805       bfd_reloc_status_type s;
806       symbolS *sym;
807
808       if (fixp->fx_done)
809         {
810           n--;
811           continue;
812         }
813
814       /* If this is an undefined symbol which was equated to another
815          symbol, then use generate the reloc against the latter symbol
816          rather than the former.  */
817       sym = fixp->fx_addsy;
818       while (sym->sy_value.X_op == O_symbol
819              && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
820         sym = sym->sy_value.X_add_symbol;
821       fixp->fx_addsy = sym;
822
823       reloc = tc_gen_reloc (sec, fixp);
824       if (!reloc)
825         {
826           n--;
827           continue;
828         }
829       if (fixp->fx_where + fixp->fx_size
830           > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
831         abort ();
832
833       s = bfd_install_relocation (stdoutput, reloc,
834                                   fixp->fx_frag->fr_literal,
835                                   fixp->fx_frag->fr_address,
836                                   sec, &err);
837       switch (s)
838         {
839         case bfd_reloc_ok:
840           break;
841         case bfd_reloc_overflow:
842           as_bad_where (fixp->fx_file, fixp->fx_line, "relocation overflow");
843           break;
844         default:
845           as_fatal ("%s:%u: bad return from bfd_perform_relocation",
846                     fixp->fx_file, fixp->fx_line);
847         }
848       relocs[i++] = reloc;
849     }
850 #else
851   n = n * MAX_RELOC_EXPANSION;
852   /* Set up reloc information as well.  */
853   relocs = (arelent **) bfd_alloc_by_size_t (stdoutput,
854                                              n * sizeof (arelent *));
855
856   i = 0;
857   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
858     {
859       arelent **reloc;
860       char *data;
861       bfd_reloc_status_type s;
862       symbolS *sym;
863       int j;
864
865       if (fixp->fx_done)
866         {
867           n--;
868           continue;
869         }
870
871       /* If this is an undefined symbol which was equated to another
872          symbol, then use generate the reloc against the latter symbol
873          rather than the former.  */
874       sym = fixp->fx_addsy;
875       while (sym->sy_value.X_op == O_symbol
876              && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
877         sym = sym->sy_value.X_add_symbol;
878       fixp->fx_addsy = sym;
879
880       reloc = tc_gen_reloc (sec, fixp);
881
882       for (j = 0; reloc[j]; j++)
883         {
884           relocs[i++] = reloc[j];
885           assert(i <= n);
886         }
887       data = fixp->fx_frag->fr_literal + fixp->fx_where;
888       if (fixp->fx_where + fixp->fx_size
889           > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
890         abort ();
891       for (j = 0; reloc[j]; j++)
892         {
893           s = bfd_install_relocation (stdoutput, reloc[j],
894                                       fixp->fx_frag->fr_literal,
895                                       fixp->fx_frag->fr_address,
896                                       sec, &err);
897           switch (s)
898             {
899             case bfd_reloc_ok:
900               break;
901             case bfd_reloc_overflow:
902               as_bad_where (fixp->fx_file, fixp->fx_line,
903                             "relocation overflow");
904               break;
905             default:
906               as_fatal ("%s:%u: bad return from bfd_perform_relocation",
907                         fixp->fx_file, fixp->fx_line);
908             }
909         }
910     }
911   n = i;
912 #endif
913
914 #ifdef DEBUG4
915   {
916     int i, j, nsyms;
917     asymbol **sympp;
918     sympp = bfd_get_outsymbols (stdoutput);
919     nsyms = bfd_get_symcount (stdoutput);
920     for (i = 0; i < n; i++)
921       if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
922         {
923           for (j = 0; j < nsyms; j++)
924             if (sympp[j] == *relocs[i]->sym_ptr_ptr)
925               break;
926           if (j == nsyms)
927             abort ();
928         }
929   }
930 #endif
931
932   if (n)
933     bfd_set_reloc (stdoutput, sec, relocs, n);
934   else
935     bfd_set_section_flags (abfd, sec,
936                            (bfd_get_section_flags (abfd, sec)
937                             & (flagword) ~SEC_RELOC));
938
939 #ifdef DEBUG3
940   {
941     int i;
942     arelent *r;
943     asymbol *s;
944     fprintf (stderr, "relocs for sec %s\n", sec->name);
945     for (i = 0; i < n; i++)
946       {
947         r = relocs[i];
948         s = *r->sym_ptr_ptr;
949         fprintf (stderr, "  reloc %2d @%08x off %4x : sym %-10s addend %x\n",
950                  i, r, r->address, s->name, r->addend);
951       }
952   }
953 #endif
954 }
955
956 static void
957 write_contents (abfd, sec, xxx)
958      bfd *abfd;
959      asection *sec;
960      PTR xxx;
961 {
962   segment_info_type *seginfo = seg_info (sec);
963   unsigned long offset = 0;
964   fragS *f;
965
966   /* Write out the frags.  */
967   if (seginfo == NULL
968       || ! (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
969     return;
970
971   for (f = seginfo->frchainP->frch_root;
972        f;
973        f = f->fr_next)
974     {
975       int x;
976       unsigned long fill_size;
977       char *fill_literal;
978       long count;
979
980       assert (f->fr_type == rs_fill);
981       if (f->fr_fix)
982         {
983           x = bfd_set_section_contents (stdoutput, sec,
984                                         f->fr_literal, (file_ptr) offset,
985                                         (bfd_size_type) f->fr_fix);
986           if (x == false)
987             {
988               bfd_perror (stdoutput->filename);
989               as_perror ("FATAL: Can't write %s", stdoutput->filename);
990               exit (EXIT_FAILURE);
991             }
992           offset += f->fr_fix;
993         }
994       fill_literal = f->fr_literal + f->fr_fix;
995       fill_size = f->fr_var;
996       count = f->fr_offset;
997       assert (count >= 0);
998       if (fill_size && count)
999         {
1000           char buf[256];
1001           if (fill_size > sizeof(buf))
1002             {
1003               /* Do it the old way. Can this ever happen? */
1004               while (count--)
1005                 {
1006                   x = bfd_set_section_contents (stdoutput, sec,
1007                                                 fill_literal,
1008                                                 (file_ptr) offset,
1009                                                 (bfd_size_type) fill_size);
1010                   if (x == false)
1011                     {
1012                       bfd_perror (stdoutput->filename);
1013                       as_perror ("FATAL: Can't write %s", stdoutput->filename);
1014                       exit (EXIT_FAILURE);
1015                     }
1016                   offset += fill_size;
1017                 }
1018             }
1019           else
1020             {
1021               /* Build a buffer full of fill objects and output it as
1022                  often as necessary. This saves on the overhead of
1023                  potentially lots of bfd_set_section_contents calls.  */
1024               int n_per_buf, i;
1025               if (fill_size == 1)
1026                 {
1027                   n_per_buf = sizeof (buf);
1028                   memset (buf, *fill_literal, n_per_buf);
1029                 }
1030               else
1031                 {
1032                   char *bufp;
1033                   n_per_buf = sizeof(buf)/fill_size;
1034                   for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1035                     memcpy(bufp, fill_literal, fill_size);
1036                 }
1037               for (; count > 0; count -= n_per_buf)
1038                 {
1039                   n_per_buf = n_per_buf > count ? count : n_per_buf;
1040                   x = bfd_set_section_contents (stdoutput, sec,
1041                                                 buf, (file_ptr) offset,
1042                                                 (bfd_size_type) n_per_buf * fill_size);
1043                   if (x != true)
1044                     as_fatal ("Cannot write to output file.");
1045                   offset += n_per_buf * fill_size;
1046                 }
1047             }
1048         }
1049     }
1050 }
1051 #endif
1052
1053 #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
1054 static void
1055 merge_data_into_text ()
1056 {
1057 #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
1058   seg_info (text_section)->frchainP->frch_last->fr_next =
1059     seg_info (data_section)->frchainP->frch_root;
1060   seg_info (text_section)->frchainP->frch_last =
1061     seg_info (data_section)->frchainP->frch_last;
1062   seg_info (data_section)->frchainP = 0;
1063 #else
1064   fixS *tmp;
1065
1066   text_last_frag->fr_next = data_frag_root;
1067   text_last_frag = data_last_frag;
1068   data_last_frag = NULL;
1069   data_frag_root = NULL;
1070   if (text_fix_root)
1071     {
1072       for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1073       tmp->fx_next = data_fix_root;
1074       text_fix_tail = data_fix_tail;
1075     }
1076   else
1077     text_fix_root = data_fix_root;
1078   data_fix_root = NULL;
1079 #endif
1080 }
1081 #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
1082
1083 #if !defined (BFD_ASSEMBLER) && !defined (BFD)
1084 static void
1085 relax_and_size_all_segments ()
1086 {
1087   fragS *fragP;
1088
1089   relax_segment (text_frag_root, SEG_TEXT);
1090   relax_segment (data_frag_root, SEG_DATA);
1091   relax_segment (bss_frag_root, SEG_BSS);
1092   /*
1093    * Now the addresses of frags are correct within the segment.
1094    */
1095
1096   know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1097   H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1098   text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1099
1100   /*
1101    * Join the 2 segments into 1 huge segment.
1102    * To do this, re-compute every rn_address in the SEG_DATA frags.
1103    * Then join the data frags after the text frags.
1104    *
1105    * Determine a_data [length of data segment].
1106    */
1107   if (data_frag_root)
1108     {
1109       register relax_addressT slide;
1110
1111       know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
1112
1113       H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1114       data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1115       slide = H_GET_TEXT_SIZE (&headers);       /* & in file of the data segment. */
1116 #ifdef OBJ_BOUT
1117 #define RoundUp(N,S) (((N)+(S)-1)&-(S))
1118       /* For b.out: If the data section has a strict alignment
1119          requirement, its load address in the .o file will be
1120          rounded up from the size of the text section.  These
1121          two values are *not* the same!  Similarly for the bss
1122          section....  */
1123       slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1124 #endif
1125
1126       for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1127         {
1128           fragP->fr_address += slide;
1129         }                       /* for each data frag */
1130
1131       know (text_last_frag != 0);
1132       text_last_frag->fr_next = data_frag_root;
1133     }
1134   else
1135     {
1136       H_SET_DATA_SIZE (&headers, 0);
1137     }
1138
1139 #ifdef OBJ_BOUT
1140   /* See above comments on b.out data section address.  */
1141   {
1142     long bss_vma;
1143     if (data_last_frag == 0)
1144       bss_vma = H_GET_TEXT_SIZE (&headers);
1145     else
1146       bss_vma = data_last_frag->fr_address;
1147     bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1148     bss_address_frag.fr_address = bss_vma;
1149   }
1150 #else /* ! OBJ_BOUT */
1151   bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1152                                  H_GET_DATA_SIZE (&headers));
1153
1154 #endif /* ! OBJ_BOUT */
1155
1156   /* Slide all the frags */
1157   if (bss_frag_root)
1158     {
1159       relax_addressT slide = bss_address_frag.fr_address;
1160
1161       for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1162         {
1163           fragP->fr_address += slide;
1164         }                       /* for each bss frag */
1165     }
1166
1167   if (bss_last_frag)
1168     H_SET_BSS_SIZE (&headers,
1169                     bss_last_frag->fr_address - bss_frag_root->fr_address);
1170   else
1171     H_SET_BSS_SIZE (&headers, 0);
1172 }
1173 #endif /* ! BFD_ASSEMBLER && ! BFD */
1174
1175 #if defined (BFD_ASSEMBLER) || !defined (BFD)
1176
1177 #ifdef BFD_ASSEMBLER
1178 static void
1179 set_symtab ()
1180 {
1181   int nsyms;
1182   asymbol **asympp;
1183   symbolS *symp;
1184   boolean result;
1185   extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1186
1187   /* Count symbols.  We can't rely on a count made by the loop in
1188      write_object_file, because *_frob_file may add a new symbol or
1189      two.  */
1190   nsyms = 0;
1191   for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1192     nsyms++;
1193
1194   if (nsyms)
1195     {
1196       int i;
1197
1198       asympp = (asymbol **) bfd_alloc (stdoutput,
1199                                        nsyms * sizeof (asymbol *));
1200       symp = symbol_rootP;
1201       for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1202         {
1203           asympp[i] = symp->bsym;
1204           symp->written = 1;
1205         }
1206     }
1207   else
1208     asympp = 0;
1209   result = bfd_set_symtab (stdoutput, asympp, nsyms);
1210   assert (result == true);
1211   symbol_table_frozen = 1;
1212 }
1213 #endif
1214
1215 void 
1216 write_object_file ()
1217 {
1218   struct frchain *frchainP;     /* Track along all frchains. */
1219 #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
1220   fragS *fragP;                 /* Track along all frags. */
1221 #endif
1222
1223   /* Do we really want to write it?  */
1224   {
1225     int n_warns, n_errs;
1226     n_warns = had_warnings ();
1227     n_errs = had_errors ();
1228     /* The -Z flag indicates that an object file should be generated,
1229        regardless of warnings and errors.  */
1230     if (flag_always_generate_output)
1231       {
1232         if (n_warns || n_errs)
1233           as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
1234                    n_errs, n_errs == 1 ? "" : "s",
1235                    n_warns, n_warns == 1 ? "" : "s");
1236       }
1237     else
1238       {
1239         if (n_errs)
1240           as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
1241                     n_errs, n_errs == 1 ? "" : "s",
1242                     n_warns, n_warns == 1 ? "" : "s");
1243       }
1244   }
1245
1246 #ifdef  OBJ_VMS
1247   /* Under VMS we try to be compatible with VAX-11 "C".  Thus, we call
1248      a routine to check for the definition of the procedure "_main",
1249      and if so -- fix it up so that it can be program entry point. */
1250   vms_check_for_main ();
1251 #endif /* OBJ_VMS */
1252
1253   /* After every sub-segment, we fake an ".align ...". This conforms to
1254      BSD4.2 brane-damage. We then fake ".fill 0" because that is the kind of
1255      frag that requires least thought. ".align" frags like to have a
1256      following frag since that makes calculating their intended length
1257      trivial.
1258
1259      @@ Is this really necessary??  */
1260 #ifndef SUB_SEGMENT_ALIGN
1261 #ifdef BFD_ASSEMBLER
1262 #define SUB_SEGMENT_ALIGN(SEG) (0)
1263 #else
1264 #define SUB_SEGMENT_ALIGN(SEG) (2)
1265 #endif
1266 #endif
1267   for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1268     {
1269       subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1270       frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
1271       /* frag_align will have left a new frag.
1272          Use this last frag for an empty ".fill".
1273
1274          For this segment ...
1275          Create a last frag. Do not leave a "being filled in frag".  */
1276       frag_wane (frag_now);
1277       frag_now->fr_fix = 0;
1278       know (frag_now->fr_next == NULL);
1279     }
1280
1281   /* From now on, we don't care about sub-segments.  Build one frag chain
1282      for each segment. Linked thru fr_next.  */
1283
1284 #ifdef BFD_ASSEMBLER
1285   /* Remove the sections created by gas for its own purposes.  */
1286   {
1287     asection **seclist, *sec;
1288     int i;
1289
1290     seclist = &stdoutput->sections;
1291     while (seclist && *seclist)
1292       {
1293         sec = *seclist;
1294         while (sec == reg_section || sec == expr_section)
1295           {
1296             sec = sec->next;
1297             *seclist = sec;
1298             stdoutput->section_count--;
1299             if (!sec)
1300               break;
1301           }
1302         if (*seclist)
1303           seclist = &(*seclist)->next;
1304       }
1305     i = 0;
1306     bfd_map_over_sections (stdoutput, renumber_sections, &i);
1307   }
1308
1309   bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1310 #else
1311   remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1312   remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1313   remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
1314 #endif
1315
1316   /* We have two segments. If user gave -R flag, then we must put the
1317      data frags into the text segment. Do this before relaxing so
1318      we know to take advantage of -R and make shorter addresses.  */
1319 #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
1320   if (flag_readonly_data_in_text)
1321     {
1322       merge_data_into_text ();
1323     }
1324 #endif
1325
1326 #ifdef BFD_ASSEMBLER
1327   bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1328 #else
1329   relax_and_size_all_segments ();
1330 #endif /* BFD_ASSEMBLER */
1331
1332 #ifndef BFD_ASSEMBLER
1333   /*
1334    *
1335    * Crawl the symbol chain.
1336    *
1337    * For each symbol whose value depends on a frag, take the address of
1338    * that frag and subsume it into the value of the symbol.
1339    * After this, there is just one way to lookup a symbol value.
1340    * Values are left in their final state for object file emission.
1341    * We adjust the values of 'L' local symbols, even if we do
1342    * not intend to emit them to the object file, because their values
1343    * are needed for fix-ups.
1344    *
1345    * Unless we saw a -L flag, remove all symbols that begin with 'L'
1346    * from the symbol chain.  (They are still pointed to by the fixes.)
1347    *
1348    * Count the remaining symbols.
1349    * Assign a symbol number to each symbol.
1350    * Count the number of string-table chars we will emit.
1351    * Put this info into the headers as appropriate.
1352    *
1353    */
1354   know (zero_address_frag.fr_address == 0);
1355   string_byte_count = sizeof (string_byte_count);
1356
1357   obj_crawl_symbol_chain (&headers);
1358
1359   if (string_byte_count == sizeof (string_byte_count))
1360     string_byte_count = 0;
1361
1362   H_SET_STRING_SIZE (&headers, string_byte_count);
1363
1364   /*
1365    * Addresses of frags now reflect addresses we use in the object file.
1366    * Symbol values are correct.
1367    * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1368    * Also converting any machine-dependent frags using md_convert_frag();
1369    */
1370   subseg_change (SEG_TEXT, 0);
1371
1372   for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1373     {
1374       cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
1375
1376       /* Some assert macros don't work with # directives mixed in.  */
1377 #ifndef NDEBUG
1378       if (!(fragP->fr_next == NULL
1379 #ifdef OBJ_BOUT
1380             || fragP->fr_next == data_frag_root
1381 #endif
1382             || ((fragP->fr_next->fr_address - fragP->fr_address)
1383                 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1384         abort ();
1385 #endif
1386     }
1387 #endif /* ! BFD_ASSEMBLER */
1388
1389 #ifndef WORKING_DOT_WORD
1390   {
1391     struct broken_word *lie;
1392     struct broken_word **prevP;
1393
1394     prevP = &broken_words;
1395     for (lie = broken_words; lie; lie = lie->next_broken_word)
1396       if (!lie->added)
1397         {
1398           expressionS exp;
1399
1400           exp.X_op = O_subtract;
1401           exp.X_add_symbol = lie->add;
1402           exp.X_op_symbol = lie->sub;
1403           exp.X_add_number = lie->addnum;
1404 #ifdef BFD_ASSEMBLER
1405 #ifdef TC_CONS_FIX_NEW
1406           TC_CONS_FIX_NEW (lie->frag,
1407                        lie->word_goes_here - lie->frag->fr_literal,
1408                        2, &exp);
1409 #else
1410           fix_new_exp (lie->frag,
1411                        lie->word_goes_here - lie->frag->fr_literal,
1412                        2, &exp, 0, BFD_RELOC_NONE);
1413 #endif
1414 #else
1415 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
1416           fix_new_exp (lie->frag,
1417                        lie->word_goes_here - lie->frag->fr_literal,
1418                        2, &exp, 0, NO_RELOC);
1419 #else
1420 #ifdef TC_NS32K
1421           fix_new_ns32k_exp (lie->frag,
1422                              lie->word_goes_here - lie->frag->fr_literal,
1423                              2, &exp, 0, 0, 2, 0, 0);
1424 #else
1425           fix_new_exp (lie->frag,
1426                        lie->word_goes_here - lie->frag->fr_literal,
1427                        2, &exp, 0, 0);
1428 #endif /* TC_NS32K */
1429 #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1430 #endif /* BFD_ASSEMBLER */
1431           *prevP = lie->next_broken_word;
1432         }
1433       else
1434         prevP = &(lie->next_broken_word);
1435
1436     for (lie = broken_words; lie;)
1437       {
1438         struct broken_word *untruth;
1439         char *table_ptr;
1440         addressT table_addr;
1441         addressT from_addr, to_addr;
1442         int n, m;
1443
1444         fragP = lie->dispfrag;
1445
1446         /* Find out how many broken_words go here.  */
1447         n = 0;
1448         for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1449           if (untruth->added == 1)
1450             n++;
1451
1452         table_ptr = lie->dispfrag->fr_opcode;
1453         table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
1454         /* Create the jump around the long jumps.  This is a short
1455            jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
1456         from_addr = table_addr;
1457         to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1458         md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1459         table_ptr += md_short_jump_size;
1460         table_addr += md_short_jump_size;
1461
1462         for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
1463           {
1464             if (lie->added == 2)
1465               continue;
1466             /* Patch the jump table */
1467             /* This is the offset from ??? to table_ptr+0 */
1468             to_addr = table_addr - S_GET_VALUE (lie->sub);
1469 #ifdef BFD_ASSEMBLER
1470             to_addr -= lie->sub->sy_frag->fr_address;
1471 #endif
1472             md_number_to_chars (lie->word_goes_here, to_addr, 2);
1473             for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1474               {
1475                 if (untruth->use_jump == lie)
1476                   md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1477               }
1478
1479             /* Install the long jump */
1480             /* this is a long jump from table_ptr+0 to the final target */
1481             from_addr = table_addr;
1482             to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1483 #ifdef BFD_ASSEMBLER
1484             to_addr += lie->add->sy_frag->fr_address;
1485 #endif
1486             md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1487             table_ptr += md_long_jump_size;
1488             table_addr += md_long_jump_size;
1489           }
1490       }
1491   }
1492 #endif /* not WORKING_DOT_WORD */
1493
1494 #ifndef BFD_ASSEMBLER
1495 #ifndef OBJ_VMS
1496   {                             /* not vms */
1497     char *the_object_file;
1498     long object_file_size;
1499     /*
1500      * Scan every FixS performing fixups. We had to wait until now to do
1501      * this because md_convert_frag() may have made some fixSs.
1502      */
1503     int trsize, drsize;
1504
1505     subseg_change (SEG_TEXT, 0);
1506     trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
1507     subseg_change (SEG_DATA, 0);
1508     drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
1509     H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1510
1511     /* FIXME move this stuff into the pre-write-hook */
1512     H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1513     H_SET_ENTRY_POINT (&headers, 0);
1514
1515     obj_pre_write_hook (&headers);      /* extra coff stuff */
1516
1517     object_file_size = H_GET_FILE_SIZE (&headers);
1518     next_object_file_charP = the_object_file = xmalloc (object_file_size);
1519
1520     output_file_create (out_file_name);
1521
1522     obj_header_append (&next_object_file_charP, &headers);
1523
1524     know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
1525
1526     /*
1527      * Emit code.
1528      */
1529     for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1530       {
1531         register long count;
1532         register char *fill_literal;
1533         register long fill_size;
1534
1535         PROGRESS (1);
1536         know (fragP->fr_type == rs_fill);
1537         append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
1538         fill_literal = fragP->fr_literal + fragP->fr_fix;
1539         fill_size = fragP->fr_var;
1540         know (fragP->fr_offset >= 0);
1541
1542         for (count = fragP->fr_offset; count; count--)
1543           {
1544             append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
1545           }                     /* for each  */
1546
1547       }                         /* for each code frag. */
1548
1549     know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
1550
1551     /*
1552      * Emit relocations.
1553      */
1554     obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
1555     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)));
1556 #ifdef TC_I960
1557     /* Make addresses in data relocation directives relative to beginning of
1558      * first data fragment, not end of last text fragment:  alignment of the
1559      * start of the data segment may place a gap between the segments.
1560      */
1561     obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
1562 #else /* TC_I960 */
1563     obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
1564 #endif /* TC_I960 */
1565
1566     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)));
1567
1568     /*
1569      * Emit line number entries.
1570      */
1571     OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1572     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)));
1573
1574     /*
1575      * Emit symbols.
1576      */
1577     obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1578     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)));
1579
1580     /*
1581      * Emit strings.
1582      */
1583
1584     if (string_byte_count > 0)
1585       {
1586         obj_emit_strings (&next_object_file_charP);
1587       }                         /* only if we have a string table */
1588
1589 #ifdef BFD_HEADERS
1590     bfd_seek (stdoutput, 0, 0);
1591     bfd_write (the_object_file, 1, object_file_size, stdoutput);
1592 #else
1593
1594     /* Write the data to the file */
1595     output_file_append (the_object_file, object_file_size, out_file_name);
1596     free (the_object_file);
1597 #endif
1598   }                             /* non vms output */
1599 #else /* OBJ_VMS */
1600   /*
1601    *    Now do the VMS-dependent part of writing the object file
1602    */
1603   vms_write_object_file (H_GET_TEXT_SIZE (&headers),
1604                          H_GET_DATA_SIZE (&headers),
1605                          H_GET_BSS_SIZE (&headers),
1606                          text_frag_root, data_frag_root);
1607 #endif /* OBJ_VMS */
1608 #else /* BFD_ASSEMBLER */
1609
1610   /* Resolve symbol values.  This needs to be done before processing
1611      the relocations.  */
1612   if (symbol_rootP)
1613     {
1614       symbolS *symp;
1615
1616       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1617         if (!symp->sy_resolved)
1618           resolve_symbol_value (symp);
1619     }
1620
1621   PROGRESS (1);
1622
1623   bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *)0);
1624
1625   /* Set up symbol table, and write it out.  */
1626   if (symbol_rootP)
1627     {
1628       symbolS *symp;
1629
1630       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1631         {
1632           int punt = 0;
1633           const char *name;
1634
1635           if (symp->sy_mri_common)
1636             {
1637               if (S_IS_EXTERNAL (symp))
1638                 as_bad ("%s: global symbols not supported in common sections",
1639                         S_GET_NAME (symp));
1640               symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1641               continue;
1642             }
1643
1644           name = S_GET_NAME (symp);
1645           if (name)
1646             {
1647               const char *name2 = decode_local_label_name ((char *)S_GET_NAME (symp));
1648               /* They only differ if `name' is a fb or dollar local
1649                  label name.  */
1650               if (name2 != name && ! S_IS_DEFINED (symp))
1651                 as_bad ("local label %s is not defined", name2);
1652             }
1653
1654           /* Do it again, because adjust_reloc_syms might introduce
1655              more symbols.  They'll probably only be section symbols,
1656              but they'll still need to have the values computed.  */
1657           if (! symp->sy_resolved)
1658             {
1659               if (symp->sy_value.X_op == O_constant)
1660                 {
1661                   /* This is the normal case; skip the call.  */
1662                   S_SET_VALUE (symp,
1663                                (S_GET_VALUE (symp)
1664                                 + symp->sy_frag->fr_address));
1665                   symp->sy_resolved = 1;
1666                 }
1667               else
1668                 resolve_symbol_value (symp);
1669             }
1670
1671           /* Skip symbols which were equated to undefined or common
1672              symbols.  */
1673           if (symp->sy_value.X_op == O_symbol
1674               && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
1675             {
1676               symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1677               continue;
1678             }
1679
1680           /* So far, common symbols have been treated like undefined symbols.
1681              Put them in the common section now.  */
1682           if (S_IS_DEFINED (symp) == 0
1683               && S_GET_VALUE (symp) != 0)
1684             S_SET_SEGMENT (symp, bfd_com_section_ptr);
1685 #if 0
1686           printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
1687                   S_GET_NAME (symp), symp,
1688                   S_GET_VALUE (symp),
1689                   symp->bsym->flags,
1690                   segment_name (symp->bsym->section));
1691 #endif
1692
1693 #ifdef obj_frob_symbol
1694           obj_frob_symbol (symp, punt);
1695 #endif
1696 #ifdef tc_frob_symbol
1697           if (! punt || symp->sy_used_in_reloc)
1698             tc_frob_symbol (symp, punt);
1699 #endif
1700
1701           /* If we don't want to keep this symbol, splice it out of
1702              the chain now.  If EMIT_SECTION_SYMBOLS is 0, we never
1703              want section symbols.  Otherwise, we skip local symbols
1704              and symbols that the frob_symbol macros told us to punt,
1705              but we keep such symbols if they are used in relocs.  */
1706           if ((! EMIT_SECTION_SYMBOLS
1707                && (symp->bsym->flags & BSF_SECTION_SYM) != 0)
1708               /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1709                  opposites.  Sometimes the former checks flags and the
1710                  latter examines the name...  */
1711               || (!S_IS_EXTERN (symp)
1712                   && (S_IS_LOCAL (symp) || punt)
1713                   && ! symp->sy_used_in_reloc))
1714             {
1715               symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1716               /* After symbol_remove, symbol_next(symp) still returns
1717                  the one that came after it in the chain.  So we don't
1718                  need to do any extra cleanup work here.  */
1719
1720               continue;
1721             }
1722
1723           /* Make sure we really got a value for the symbol.  */
1724           if (! symp->sy_resolved)
1725             {
1726               as_bad ("can't resolve value for symbol \"%s\"",
1727                       S_GET_NAME (symp));
1728               symp->sy_resolved = 1;
1729             }
1730
1731           /* Set the value into the BFD symbol.  Up til now the value
1732              has only been kept in the gas symbolS struct.  */
1733           symp->bsym->value = S_GET_VALUE (symp);
1734         }
1735     }
1736
1737   PROGRESS (1);
1738
1739   /* Now do any format-specific adjustments to the symbol table, such
1740      as adding file symbols.  */
1741 #ifdef tc_adjust_symtab
1742   tc_adjust_symtab ();
1743 #endif
1744 #ifdef obj_adjust_symtab
1745   obj_adjust_symtab ();
1746 #endif
1747
1748   /* Now that all the sizes are known, and contents correct, we can
1749      start writing to the file.  */
1750   set_symtab ();
1751
1752   /* If *_frob_file changes the symbol value at this point, it is
1753      responsible for moving the changed value into symp->bsym->value
1754      as well.  Hopefully all symbol value changing can be done in
1755      *_frob_symbol.  */
1756 #ifdef tc_frob_file
1757   tc_frob_file ();
1758 #endif
1759 #ifdef obj_frob_file
1760   obj_frob_file ();
1761 #endif
1762
1763   bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1764
1765   bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1766 #endif /* BFD_ASSEMBLER */
1767 }
1768 #endif /* ! BFD */
1769
1770 /*
1771  *                      relax_segment()
1772  *
1773  * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1774  * values.
1775  *
1776  * Relax the frags.
1777  *
1778  * After this, all frags in this segment have addresses that are correct
1779  * within the segment. Since segments live in different file addresses,
1780  * these frag addresses may not be the same as final object-file addresses.
1781  */
1782
1783 #ifndef md_relax_frag
1784
1785 /* Subroutines of relax_segment.  */
1786 static int 
1787 is_dnrange (f1, f2)
1788      struct frag *f1;
1789      struct frag *f2;
1790 {
1791   for (; f1; f1 = f1->fr_next)
1792     if (f1->fr_next == f2)
1793       return 1;
1794   return 0;
1795 }
1796
1797 #endif /* ! defined (md_relax_frag) */
1798
1799 /* Relax_align. Advance location counter to next address that has 'alignment'
1800    lowest order bits all 0s, return size of adjustment made.  */
1801 static relax_addressT
1802 relax_align (address, alignment)
1803      register relax_addressT address;   /* Address now. */
1804      register int alignment;    /* Alignment (binary). */
1805 {
1806   relax_addressT mask;
1807   relax_addressT new_address;
1808
1809   mask = ~((~0) << alignment);
1810   new_address = (address + mask) & (~mask);
1811 #ifdef LINKER_RELAXING_SHRINKS_ONLY
1812   if (linkrelax)
1813     /* We must provide lots of padding, so the linker can discard it
1814        when needed.  The linker will not add extra space, ever.  */
1815     new_address += (1 << alignment);
1816 #endif
1817   return (new_address - address);
1818 }
1819
1820 void 
1821 relax_segment (segment_frag_root, segment)
1822      struct frag *segment_frag_root;
1823      segT segment;
1824 {
1825   register struct frag *fragP;
1826   register relax_addressT address;
1827 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1828   know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
1829 #endif
1830   /* In case md_estimate_size_before_relax() wants to make fixSs. */
1831   subseg_change (segment, 0);
1832
1833   /* For each frag in segment: count and store  (a 1st guess of)
1834      fr_address.  */
1835   address = 0;
1836   for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1837     {
1838       fragP->fr_address = address;
1839       address += fragP->fr_fix;
1840
1841       switch (fragP->fr_type)
1842         {
1843         case rs_fill:
1844           address += fragP->fr_offset * fragP->fr_var;
1845           break;
1846
1847         case rs_align:
1848         case rs_align_code:
1849           {
1850             int offset = relax_align (address, (int) fragP->fr_offset);
1851             if (offset % fragP->fr_var != 0)
1852               {
1853                 as_bad ("alignment padding (%d bytes) not a multiple of %ld",
1854                         offset, (long) fragP->fr_var);
1855                 offset -= (offset % fragP->fr_var);
1856               }
1857             address += offset;
1858           }
1859           break;
1860
1861         case rs_org:
1862         case rs_space:
1863           /* Assume .org is nugatory. It will grow with 1st relax.  */
1864           break;
1865
1866         case rs_machine_dependent:
1867           address += md_estimate_size_before_relax (fragP, segment);
1868           break;
1869
1870 #ifndef WORKING_DOT_WORD
1871           /* Broken words don't concern us yet */
1872         case rs_broken_word:
1873           break;
1874 #endif
1875
1876         default:
1877           BAD_CASE (fragP->fr_type);
1878           break;
1879         }                       /* switch(fr_type) */
1880     }                           /* for each frag in the segment */
1881
1882   /* Do relax().  */
1883   {
1884     long stretch;       /* May be any size, 0 or negative. */
1885     /* Cumulative number of addresses we have */
1886     /* relaxed this pass. */
1887     /* We may have relaxed more than one address. */
1888     long stretched;     /* Have we stretched on this pass? */
1889     /* This is 'cuz stretch may be zero, when, in fact some piece of code
1890        grew, and another shrank.  If a branch instruction doesn't fit anymore,
1891        we could be scrod.  */
1892
1893     do
1894       {
1895         stretch = stretched = 0;
1896         for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
1897           {
1898             long growth = 0;
1899             unsigned long was_address;
1900             long offset;
1901             symbolS *symbolP;
1902             long target;
1903             long after;
1904
1905             was_address = fragP->fr_address;
1906             address = fragP->fr_address += stretch;
1907             symbolP = fragP->fr_symbol;
1908             offset = fragP->fr_offset;
1909
1910             switch (fragP->fr_type)
1911               {
1912               case rs_fill:     /* .fill never relaxes. */
1913                 growth = 0;
1914                 break;
1915
1916 #ifndef WORKING_DOT_WORD
1917                 /* JF:  This is RMS's idea.  I do *NOT* want to be blamed
1918                    for it I do not want to write it.  I do not want to have
1919                    anything to do with it.  This is not the proper way to
1920                    implement this misfeature.  */
1921               case rs_broken_word:
1922                 {
1923                   struct broken_word *lie;
1924                   struct broken_word *untruth;
1925
1926                   /* Yes this is ugly (storing the broken_word pointer
1927                      in the symbol slot).  Still, this whole chunk of
1928                      code is ugly, and I don't feel like doing anything
1929                      about it.  Think of it as stubbornness in action.  */
1930                   growth = 0;
1931                   for (lie = (struct broken_word *) (fragP->fr_symbol);
1932                        lie && lie->dispfrag == fragP;
1933                        lie = lie->next_broken_word)
1934                     {
1935
1936                       if (lie->added)
1937                         continue;
1938
1939                       offset = (lie->add->sy_frag->fr_address
1940                                 + S_GET_VALUE (lie->add)
1941                                 + lie->addnum
1942                                 - (lie->sub->sy_frag->fr_address
1943                                    + S_GET_VALUE (lie->sub)));
1944                       if (offset <= -32768 || offset >= 32767)
1945                         {
1946                           if (flag_warn_displacement)
1947                             {
1948                               char buf[50];
1949                               sprint_value (buf, (addressT) lie->addnum);
1950                               as_warn (".word %s-%s+%s didn't fit",
1951                                        S_GET_NAME (lie->add),
1952                                        S_GET_NAME (lie->sub),
1953                                        buf);
1954                             }
1955                           lie->added = 1;
1956                           if (fragP->fr_subtype == 0)
1957                             {
1958                               fragP->fr_subtype++;
1959                               growth += md_short_jump_size;
1960                             }
1961                           for (untruth = lie->next_broken_word;
1962                                untruth && untruth->dispfrag == lie->dispfrag;
1963                                untruth = untruth->next_broken_word)
1964                             if ((untruth->add->sy_frag == lie->add->sy_frag)
1965                                 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
1966                               {
1967                                 untruth->added = 2;
1968                                 untruth->use_jump = lie;
1969                               }
1970                           growth += md_long_jump_size;
1971                         }
1972                     }
1973
1974                   break;
1975                 }               /* case rs_broken_word */
1976 #endif
1977               case rs_align:
1978               case rs_align_code:
1979                 growth = (relax_align ((relax_addressT) (address
1980                                                          + fragP->fr_fix),
1981                                        (int) offset)
1982                           - relax_align ((relax_addressT) (was_address
1983                                                            + fragP->fr_fix),
1984                                          (int) offset));
1985                 break;
1986
1987               case rs_org:
1988                 target = offset;
1989
1990                 if (symbolP)
1991                   {
1992 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1993                     know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1994                           || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1995                           || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1996                           || S_GET_SEGMENT (symbolP) == SEG_BSS);
1997                     know (symbolP->sy_frag);
1998                     know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1999                           || (symbolP->sy_frag == &zero_address_frag));
2000 #endif
2001                     target += S_GET_VALUE (symbolP)
2002                       + symbolP->sy_frag->fr_address;
2003                   }             /* if we have a symbol */
2004
2005                 know (fragP->fr_next);
2006                 after = fragP->fr_next->fr_address;
2007                 growth = target - after;
2008                 if (growth < 0)
2009                   {
2010                     /* Growth may be negative, but variable part of frag
2011                        cannot have fewer than 0 chars.  That is, we can't
2012                        .org backwards. */
2013                     as_bad ("attempt to .org backwards ignored");
2014                     growth = 0;
2015                   }
2016
2017                 growth -= stretch;      /* This is an absolute growth factor */
2018                 break;
2019
2020               case rs_space:
2021                 if (symbolP)
2022                   {
2023                     growth = S_GET_VALUE (symbolP);
2024                     if (symbolP->sy_frag != &zero_address_frag)
2025                       as_bad (".space specifies non-absolute value");
2026                     fragP->fr_symbol = 0;
2027                     if (growth < 0)
2028                       {
2029                         as_warn (".space or .fill with negative value, ignored");
2030                         growth = 0;
2031                       }
2032                   }
2033                 else
2034                   growth = 0;
2035                 break;
2036
2037               case rs_machine_dependent:
2038 #ifdef md_relax_frag
2039                 growth = md_relax_frag (fragP, stretch);
2040 #else
2041 #ifdef TC_GENERIC_RELAX_TABLE
2042                 /* The default way to relax a frag is to look through
2043                    md_relax_table.  */
2044                 {
2045                   const relax_typeS *this_type;
2046                   const relax_typeS *start_type;
2047                   relax_substateT next_state;
2048                   relax_substateT this_state;
2049                   long aim;
2050                   const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
2051
2052                   this_state = fragP->fr_subtype;
2053                   start_type = this_type = table + this_state;
2054                   target = offset;
2055
2056                   if (symbolP)
2057                     {
2058 #ifndef DIFF_EXPR_OK
2059 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2060                       know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2061                             || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2062                             || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2063                             || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2064 #endif
2065                       know (symbolP->sy_frag);
2066 #endif
2067                       know (!(S_GET_SEGMENT (symbolP) == absolute_section)
2068                             || symbolP->sy_frag == &zero_address_frag);
2069                       target +=
2070                         S_GET_VALUE (symbolP)
2071                         + symbolP->sy_frag->fr_address;
2072
2073                       /* If frag has yet to be reached on this pass,
2074                          assume it will move by STRETCH just as we did.
2075                          If this is not so, it will be because some frag
2076                          between grows, and that will force another pass.
2077
2078                          Beware zero-length frags.
2079
2080                          There should be a faster way to do this.  */
2081
2082                       if (symbolP->sy_frag->fr_address >= was_address
2083                           && is_dnrange (fragP, symbolP->sy_frag))
2084                         {
2085                           target += stretch;
2086                         }
2087                     }
2088
2089                   aim = target - address - fragP->fr_fix;
2090 #ifdef TC_PCREL_ADJUST
2091                   /* Currently only the ns32k family needs this */
2092                   aim += TC_PCREL_ADJUST(fragP);
2093 #else
2094                   /* This machine doesn't want to use pcrel_adjust.
2095                      In that case, pcrel_adjust should be zero.  */
2096                   assert (fragP->fr_pcrel_adjust == 0);
2097 #endif
2098
2099                   if (aim < 0)
2100                     {
2101                       /* Look backwards. */
2102                       for (next_state = this_type->rlx_more; next_state;)
2103                         if (aim >= this_type->rlx_backward)
2104                           next_state = 0;
2105                         else
2106                           {
2107                             /* Grow to next state. */
2108                             this_state = next_state;
2109                             this_type = table + this_state;
2110                             next_state = this_type->rlx_more;
2111                           }
2112                     }
2113                   else
2114                     {
2115 #ifdef M68K_AIM_KLUDGE
2116                       M68K_AIM_KLUDGE (aim, this_state, this_type);
2117 #endif
2118                       /* Look forwards. */
2119                       for (next_state = this_type->rlx_more; next_state;)
2120                         if (aim <= this_type->rlx_forward)
2121                           next_state = 0;
2122                         else
2123                           {
2124                             /* Grow to next state. */
2125                             this_state = next_state;
2126                             this_type = table + this_state;
2127                             next_state = this_type->rlx_more;
2128                           }
2129                     }
2130
2131                   growth = this_type->rlx_length - start_type->rlx_length;
2132                   if (growth != 0)
2133                     fragP->fr_subtype = this_state;
2134                 }
2135 #endif /* TC_GENERIC_RELAX_TABLE */
2136 #endif
2137                 break;
2138
2139               default:
2140                 BAD_CASE (fragP->fr_type);
2141                 break;
2142               }
2143             if (growth)
2144               {
2145                 stretch += growth;
2146                 stretched++;
2147               }
2148           }                     /* For each frag in the segment. */
2149       }
2150     while (stretched);          /* Until nothing further to relax. */
2151   }                             /* do_relax */
2152
2153   /*
2154    * We now have valid fr_address'es for each frag.
2155    */
2156
2157   /*
2158    * All fr_address's are correct, relative to their own segment.
2159    * We have made all the fixS we will ever make.
2160    */
2161 }                               /* relax_segment() */
2162
2163 #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
2164
2165 #ifndef TC_RELOC_RTSYM_LOC_FIXUP
2166 #define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2167 #endif
2168
2169 /* fixup_segment()
2170
2171    Go through all the fixS's in a segment and see which ones can be
2172    handled now.  (These consist of fixS where we have since discovered
2173    the value of a symbol, or the address of the frag involved.)
2174    For each one, call md_apply_fix to put the fix into the frag data.
2175
2176    Result is a count of how many relocation structs will be needed to
2177    handle the remaining fixS's that we couldn't completely handle here.
2178    These will be output later by emit_relocations().  */
2179
2180 static long
2181 fixup_segment (fixP, this_segment_type)
2182      register fixS *fixP;
2183      segT this_segment_type;    /* N_TYPE bits for segment. */
2184 {
2185   long seg_reloc_count = 0;
2186   symbolS *add_symbolP;
2187   symbolS *sub_symbolP;
2188   valueT add_number;
2189   int size;
2190   char *place;
2191   long where;
2192   int pcrel, plt;
2193   fragS *fragP;
2194   segT add_symbol_segment = absolute_section;
2195   
2196   /* If the linker is doing the relaxing, we must not do any fixups.
2197
2198      Well, strictly speaking that's not true -- we could do any that are
2199      PC-relative and don't cross regions that could change size.  And for the
2200      i960 (the only machine for which we've got a relaxing linker right now),
2201      we might be able to turn callx/callj into bal anyways in cases where we
2202      know the maximum displacement.  */
2203   if (linkrelax)
2204     {
2205       for (; fixP; fixP = fixP->fx_next)
2206         seg_reloc_count++;
2207       TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2208       return seg_reloc_count;
2209     }
2210
2211   for (; fixP; fixP = fixP->fx_next)
2212     {
2213 #ifdef DEBUG5
2214       fprintf (stderr, "\nprocessing fixup:\n");
2215       print_fixup (fixP);
2216 #endif
2217
2218       fragP = fixP->fx_frag;
2219       know (fragP);
2220       where = fixP->fx_where;
2221       place = fragP->fr_literal + where;
2222       size = fixP->fx_size;
2223       add_symbolP = fixP->fx_addsy;
2224 #ifdef TC_VALIDATE_FIX
2225       TC_VALIDATE_FIX (fixP, this_segment_type, skip);
2226 #endif
2227       sub_symbolP = fixP->fx_subsy;
2228       add_number = fixP->fx_offset;
2229       pcrel = fixP->fx_pcrel;
2230       plt = fixP->fx_plt;
2231
2232       if (add_symbolP != NULL
2233           && add_symbolP->sy_mri_common)
2234         {
2235           know (add_symbolP->sy_value.X_op == O_symbol);
2236           add_number += S_GET_VALUE (add_symbolP);
2237           fixP->fx_offset = add_number;
2238           add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
2239         }
2240
2241       if (add_symbolP)
2242         add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2243       
2244       if (sub_symbolP)
2245         {
2246           resolve_symbol_value (sub_symbolP);
2247           if (add_symbolP == NULL || add_symbol_segment == absolute_section)
2248             {
2249               if (add_symbolP != NULL)
2250                 {
2251                   add_number += S_GET_VALUE (add_symbolP);
2252                   add_symbolP = NULL;
2253                   fixP->fx_addsy = NULL;
2254                 }
2255
2256               /* It's just -sym */
2257               if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
2258                 {
2259                   add_number -= S_GET_VALUE (sub_symbolP);
2260                   fixP->fx_subsy = NULL;
2261                 }
2262               else if (pcrel
2263                        && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2264                 {
2265                   /* Should try converting to a constant.  */
2266                   goto bad_sub_reloc;
2267                 }
2268               else
2269               bad_sub_reloc:
2270                 as_bad_where (fixP->fx_file, fixP->fx_line,
2271                               "Negative of non-absolute symbol %s",
2272                               S_GET_NAME (sub_symbolP));
2273             }
2274           else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2275                    && SEG_NORMAL (add_symbol_segment))
2276             {
2277               /* Difference of 2 symbols from same segment.
2278                  Can't make difference of 2 undefineds: 'value' means
2279                  something different for N_UNDF. */
2280 #ifdef TC_I960
2281               /* Makes no sense to use the difference of 2 arbitrary symbols
2282                  as the target of a call instruction.  */
2283               if (fixP->fx_tcbit)
2284                 as_bad_where (fixP->fx_file, fixP->fx_line,
2285                               "callj to difference of 2 symbols");
2286 #endif /* TC_I960 */
2287               add_number += S_GET_VALUE (add_symbolP) -
2288                 S_GET_VALUE (sub_symbolP);
2289
2290               add_symbolP = NULL;
2291               pcrel = 0;        /* No further pcrel processing. */
2292
2293               /* Let the target machine make the final determination
2294                  as to whether or not a relocation will be needed to
2295                  handle this fixup.  */
2296               if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
2297                 {
2298                   fixP->fx_pcrel = 0;
2299                   fixP->fx_addsy = NULL;
2300                   fixP->fx_subsy = NULL;
2301                 }
2302             }
2303           else
2304             {
2305               /* Different segments in subtraction. */
2306               know (!(S_IS_EXTERNAL (sub_symbolP)
2307                       && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2308
2309               if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2310                 add_number -= S_GET_VALUE (sub_symbolP);
2311
2312 #ifdef DIFF_EXPR_OK
2313               else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2314 #if 0 /* Do this even if it's already described as pc-relative.  For example,
2315          on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
2316          pc-relative mode.  */
2317                        && pcrel
2318 #endif
2319                        )
2320                 {
2321                   /* Make it pc-relative.  */
2322                   add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
2323                                  - S_GET_VALUE (sub_symbolP));
2324                   pcrel = 1;
2325                   fixP->fx_pcrel = 1;
2326                   sub_symbolP = 0;
2327                   fixP->fx_subsy = 0;
2328                 }
2329 #endif
2330 #ifdef UNDEFINED_DIFFERENCE_OK
2331               /* The PA needs this for PIC code generation.  We basically
2332                  don't want to do anything if we have the difference of two
2333                  symbols at this point.  */
2334               else if (1)
2335                 {
2336                   /* Leave it alone.  */
2337                 }
2338 #endif
2339 #ifdef BFD_ASSEMBLER
2340               else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2341                        || fixP->fx_r_type == BFD_RELOC_GPREL16)
2342                 {
2343                   /* Leave it alone.  */
2344                 }
2345 #endif
2346               else
2347                 {
2348                   char buf[50];
2349                   sprint_value (buf, fragP->fr_address + where);
2350                   as_bad_where (fixP->fx_file, fixP->fx_line,
2351                                 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
2352                                 segment_name (S_GET_SEGMENT (sub_symbolP)),
2353                                 S_GET_NAME (sub_symbolP), buf);
2354                 }
2355             }
2356         }
2357
2358       if (add_symbolP)
2359         {
2360           if (add_symbol_segment == this_segment_type && pcrel && !plt
2361               && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
2362             {
2363               /*
2364                * This fixup was made when the symbol's segment was
2365                * SEG_UNKNOWN, but it is now in the local segment.
2366                * So we know how to do the address without relocation.
2367                */
2368 #ifdef TC_I960
2369               /* reloc_callj() may replace a 'call' with a 'calls' or a
2370                  'bal', in which cases it modifies *fixP as appropriate.
2371                  In the case of a 'calls', no further work is required,
2372                  and *fixP has been set up to make the rest of the code
2373                  below a no-op. */
2374               reloc_callj (fixP);
2375 #endif /* TC_I960 */
2376
2377               add_number += S_GET_VALUE (add_symbolP);
2378               add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2379               pcrel = 0;        /* Lie. Don't want further pcrel processing. */
2380               
2381               /* Let the target machine make the final determination
2382                  as to whether or not a relocation will be needed to
2383                  handle this fixup.  */
2384               if (!TC_FORCE_RELOCATION (fixP))
2385                 {
2386                   fixP->fx_pcrel = 0;
2387                   fixP->fx_addsy = NULL;
2388                 }
2389             }
2390           else
2391             {
2392               if (add_symbol_segment == absolute_section)
2393                 {
2394 #ifdef TC_I960
2395                   /* See comment about reloc_callj() above.  */
2396                   reloc_callj (fixP);
2397 #endif /* TC_I960 */
2398                   add_number += S_GET_VALUE (add_symbolP);
2399
2400                   /* Let the target machine make the final determination
2401                      as to whether or not a relocation will be needed to
2402                      handle this fixup.  */
2403                   
2404                   if (!TC_FORCE_RELOCATION (fixP))
2405                     {
2406                       fixP->fx_addsy = NULL;
2407                       add_symbolP = NULL;
2408                     }
2409                 }
2410               else if (add_symbol_segment == undefined_section
2411 #ifdef BFD_ASSEMBLER
2412                        || bfd_is_com_section (add_symbol_segment)
2413 #endif
2414                        )
2415                 {
2416 #ifdef TC_I960
2417                   if ((int) fixP->fx_bit_fixP == 13)
2418                     {
2419                       /* This is a COBR instruction.  They have only a
2420                        * 13-bit displacement and are only to be used
2421                        * for local branches: flag as error, don't generate
2422                        * relocation.
2423                        */
2424                       as_bad_where (fixP->fx_file, fixP->fx_line,
2425                                     "can't use COBR format with external label");
2426                       fixP->fx_addsy = NULL;
2427                       fixP->fx_done = 1;
2428                       continue;
2429                     }           /* COBR */
2430 #endif /* TC_I960 */
2431                   
2432 #ifdef OBJ_COFF
2433 #ifdef TE_I386AIX
2434                   if (S_IS_COMMON (add_symbolP))
2435                     add_number += S_GET_VALUE (add_symbolP);
2436 #endif /* TE_I386AIX */
2437 #endif /* OBJ_COFF */
2438                   ++seg_reloc_count;
2439                 }
2440               else
2441                 {
2442                   seg_reloc_count++;
2443 #if !(defined (TC_M68K) && defined (OBJ_ELF))
2444 #if !defined (TC_I386) || !(defined (OBJ_ELF) || defined (OBJ_COFF))
2445                   add_number += S_GET_VALUE (add_symbolP);
2446 #endif
2447 #endif
2448                 }
2449             }
2450         }
2451
2452       if (pcrel)
2453         {
2454           add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
2455           if (add_symbolP == 0)
2456             {
2457 #ifndef BFD_ASSEMBLER
2458               fixP->fx_addsy = &abs_symbol;
2459 #else
2460               fixP->fx_addsy = section_symbol (absolute_section);
2461 #endif
2462               fixP->fx_addsy->sy_used_in_reloc = 1;
2463               ++seg_reloc_count;
2464             }
2465         }
2466
2467       if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
2468         {
2469           valueT mask = 0;
2470           if (size < sizeof (mask))
2471             {
2472               /* set all bits to one */
2473               mask--;
2474               /* Technically, combining these produces an undefined result
2475                  if size is sizeof (valueT), though I think these two
2476                  half-way operations should both be defined.  And the
2477                  compiler should be able to combine them if it's valid on
2478                  the host architecture.  */
2479               mask <<= size * 4;
2480               mask <<= size * 4;
2481               if ((add_number & mask) != 0
2482                   && (add_number & mask) != mask)
2483                 {
2484                   char buf[50], buf2[50];
2485                   sprint_value (buf, fragP->fr_address + where);
2486                   if (add_number > 1000)
2487                     sprint_value (buf2, add_number);
2488                   else
2489                     sprintf (buf2, "%ld", (long) add_number);
2490                   as_bad_where (fixP->fx_file, fixP->fx_line,
2491                                 "Value of %s too large for field of %d bytes at %s",
2492                                 buf2, size, buf);
2493                 } /* generic error checking */
2494             }
2495 #ifdef WARN_SIGNED_OVERFLOW_WORD
2496           /* Warn if a .word value is too large when treated as a signed
2497              number.  We already know it is not too negative.  This is to
2498              catch over-large switches generated by gcc on the 68k.  */
2499           if (!flag_signed_overflow_ok
2500               && size == 2
2501               && add_number > 0x7fff)
2502             as_bad_where (fixP->fx_file, fixP->fx_line,
2503                           "Signed .word overflow; switch may be too large; %ld at 0x%lx",
2504                           (long) add_number,
2505                           (unsigned long) (fragP->fr_address + where));
2506 #endif
2507         }                       /* not a bit fix */
2508
2509       if (!fixP->fx_done)
2510         {
2511 #ifdef MD_APPLY_FIX3
2512           md_apply_fix3 (fixP, &add_number, this_segment_type);
2513 #else
2514 #ifdef BFD_ASSEMBLER
2515           md_apply_fix (fixP, &add_number);
2516 #else
2517           md_apply_fix (fixP, add_number);
2518 #endif
2519 #endif
2520
2521 #ifndef TC_HANDLES_FX_DONE
2522           /* If the tc-* files haven't been converted, assume it's handling
2523              it the old way, where a null fx_addsy means that the fix has
2524              been applied completely, and no further work is needed.  */
2525           if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2526             fixP->fx_done = 1;
2527 #endif
2528         }
2529 #ifdef TC_VALIDATE_FIX
2530     skip: ;
2531 #endif
2532 #ifdef DEBUG5
2533       fprintf (stderr, "result:\n");
2534       print_fixup (fixP);
2535 #endif
2536     }                           /* For each fixS in this segment. */
2537
2538   TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2539   return seg_reloc_count;
2540 }
2541
2542 #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
2543
2544 void
2545 number_to_chars_bigendian (buf, val, n)
2546      char *buf;
2547      valueT val;
2548      int n;
2549 {
2550   if (n > sizeof (val)|| n <= 0)
2551     abort ();
2552   while (n--)
2553     {
2554       buf[n] = val & 0xff;
2555       val >>= 8;
2556     }
2557 }
2558
2559 void
2560 number_to_chars_littleendian (buf, val, n)
2561      char *buf;
2562      valueT val;
2563      int n;
2564 {
2565   if (n > sizeof (val) || n <= 0)
2566     abort ();
2567   while (n--)
2568     {
2569       *buf++ = val & 0xff;
2570       val >>= 8;
2571     }
2572 }
2573
2574 void
2575 write_print_statistics (file)
2576      FILE *file;
2577 {
2578   fprintf (stderr, "fixups: %d\n", n_fixups);
2579 }
2580
2581 /* for debugging */
2582 extern int indent_level;
2583 extern void print_symbol_value_1 ();
2584
2585 void
2586 print_fixup (fixp)
2587      fixS *fixp;
2588 {
2589   indent_level = 1;
2590   fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2591   if (fixp->fx_pcrel)
2592     fprintf (stderr, " pcrel");
2593   if (fixp->fx_pcrel_adjust)
2594     fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2595   if (fixp->fx_im_disp)
2596     {
2597 #ifdef TC_NS32K
2598       fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2599 #else
2600       fprintf (stderr, " im_disp");
2601 #endif
2602     }
2603   if (fixp->fx_tcbit)
2604     fprintf (stderr, " tcbit");
2605   if (fixp->fx_done)
2606     fprintf (stderr, " done");
2607   fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2608            fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2609            (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2610 #ifdef BFD_ASSEMBLER
2611   fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2612            fixp->fx_r_type);
2613 #else
2614 #ifdef NEED_FX_R_TYPE
2615   fprintf (stderr, " r_type=%d", fixp->fx_r_type);
2616 #endif
2617 #endif
2618   if (fixp->fx_addsy)
2619     {
2620       fprintf (stderr, "\n   +<");
2621       print_symbol_value_1 (stderr, fixp->fx_addsy);
2622       fprintf (stderr, ">");
2623     }
2624   if (fixp->fx_subsy)
2625     {
2626       fprintf (stderr, "\n   -<");
2627       print_symbol_value_1 (stderr, fixp->fx_subsy);
2628       fprintf (stderr, ">");
2629     }
2630   fprintf (stderr, "\n");
2631 }
2632
2633 /* end of write.c */