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