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