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