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