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