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