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