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