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