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