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