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