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