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