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