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