OSDN Git Service

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