write.c revision 89857
133965Sjdp/* write.c - emit .o file
278828Sobrien   Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
378828Sobrien   1998, 1999, 2000, 2001
433965Sjdp   Free Software Foundation, Inc.
533965Sjdp
633965Sjdp   This file is part of GAS, the GNU Assembler.
733965Sjdp
833965Sjdp   GAS is free software; you can redistribute it and/or modify
933965Sjdp   it under the terms of the GNU General Public License as published by
1033965Sjdp   the Free Software Foundation; either version 2, or (at your option)
1133965Sjdp   any later version.
1233965Sjdp
1333965Sjdp   GAS is distributed in the hope that it will be useful,
1433965Sjdp   but WITHOUT ANY WARRANTY; without even the implied warranty of
1533965Sjdp   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1633965Sjdp   GNU General Public License for more details.
1733965Sjdp
1833965Sjdp   You should have received a copy of the GNU General Public License
1933965Sjdp   along with GAS; see the file COPYING.  If not, write to the Free
2033965Sjdp   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2133965Sjdp   02111-1307, USA.  */
2233965Sjdp
2377298Sobrien/* This thing should be set up to do byteordering correctly.  But...  */
2433965Sjdp
2533965Sjdp#include "as.h"
2633965Sjdp#include "subsegs.h"
2733965Sjdp#include "obstack.h"
2833965Sjdp#include "output-file.h"
2977298Sobrien#include "dwarf2dbg.h"
3033965Sjdp
3133965Sjdp/* This looks like a good idea.  Let's try turning it on always, for now.  */
3233965Sjdp#undef  BFD_FAST_SECTION_FILL
3333965Sjdp#define BFD_FAST_SECTION_FILL
3433965Sjdp
3533965Sjdp#ifndef TC_ADJUST_RELOC_COUNT
3633965Sjdp#define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
3733965Sjdp#endif
3833965Sjdp
3933965Sjdp#ifndef TC_FORCE_RELOCATION
4033965Sjdp#define TC_FORCE_RELOCATION(FIXP) 0
4133965Sjdp#endif
4233965Sjdp
4333965Sjdp#ifndef TC_FORCE_RELOCATION_SECTION
4433965Sjdp#define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
4533965Sjdp#endif
4633965Sjdp
4777298Sobrien#ifndef TC_LINKRELAX_FIXUP
4877298Sobrien#define TC_LINKRELAX_FIXUP(SEG) 1
4977298Sobrien#endif
5077298Sobrien
5177298Sobrien#ifndef TC_FIX_ADJUSTABLE
5277298Sobrien#define TC_FIX_ADJUSTABLE(fix) 1
5377298Sobrien#endif
5477298Sobrien
5589857Sobrien#ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
5689857Sobrien#define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
5789857Sobrien#endif
5889857Sobrien
5933965Sjdp#ifndef	MD_PCREL_FROM_SECTION
6033965Sjdp#define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
6133965Sjdp#endif
6233965Sjdp
6333965Sjdp#ifndef WORKING_DOT_WORD
6433965Sjdpextern CONST int md_short_jump_size;
6533965Sjdpextern CONST int md_long_jump_size;
6633965Sjdp#endif
6733965Sjdp
6889857Sobrien/* Used to control final evaluation of expressions.  */
6989857Sobrienint finalize_syms = 0;
7089857Sobrien
7133965Sjdpint symbol_table_frozen;
7233965Sjdpvoid print_fixup PARAMS ((fixS *));
7333965Sjdp
7433965Sjdp#ifdef BFD_ASSEMBLER
7533965Sjdpstatic void renumber_sections PARAMS ((bfd *, asection *, PTR));
7633965Sjdp
7733965Sjdp/* We generally attach relocs to frag chains.  However, after we have
7833965Sjdp   chained these all together into a segment, any relocs we add after
7933965Sjdp   that must be attached to a segment.  This will include relocs added
8033965Sjdp   in md_estimate_size_for_relax, for example.  */
8133965Sjdpstatic int frags_chained = 0;
8233965Sjdp#endif
8333965Sjdp
8433965Sjdp#ifndef BFD_ASSEMBLER
8533965Sjdp
8633965Sjdp#ifndef MANY_SEGMENTS
8733965Sjdpstruct frag *text_frag_root;
8833965Sjdpstruct frag *data_frag_root;
8933965Sjdpstruct frag *bss_frag_root;
9033965Sjdp
9177298Sobrienstruct frag *text_last_frag;	/* Last frag in segment.  */
9277298Sobrienstruct frag *data_last_frag;	/* Last frag in segment.  */
9377298Sobrienstatic struct frag *bss_last_frag;	/* Last frag in segment.  */
9433965Sjdp#endif
9533965Sjdp
9633965Sjdp#ifndef BFD
9733965Sjdpstatic object_headers headers;
9833965Sjdp#endif
9933965Sjdp
10033965Sjdplong string_byte_count;
10177298Sobrienchar *next_object_file_charP;	/* Tracks object file bytes.  */
10233965Sjdp
10333965Sjdp#ifndef OBJ_VMS
10433965Sjdpint magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
10533965Sjdp#endif
10633965Sjdp
10777298Sobrien#endif /* BFD_ASSEMBLER  */
10833965Sjdp
10933965Sjdpstatic int n_fixups;
11033965Sjdp
11133965Sjdp#ifdef BFD_ASSEMBLER
11233965Sjdpstatic fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
11333965Sjdp				       symbolS *add, symbolS *sub,
11433965Sjdp				       offsetT offset, int pcrel,
11533965Sjdp				       bfd_reloc_code_real_type r_type));
11633965Sjdp#else
11733965Sjdpstatic fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
11833965Sjdp				       symbolS *add, symbolS *sub,
11933965Sjdp				       offsetT offset, int pcrel,
12033965Sjdp				       int r_type));
12133965Sjdp#endif
12233965Sjdp#if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
12333965Sjdpstatic long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
12433965Sjdp#endif
12533965Sjdpstatic relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
12633965Sjdp#if defined (BFD_ASSEMBLER) || ! defined (BFD)
12733965Sjdpstatic fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
12833965Sjdp#endif
12933965Sjdp#ifdef BFD_ASSEMBLER
13033965Sjdpstatic void chain_frchains_together PARAMS ((bfd *, segT, PTR));
13133965Sjdpstatic void cvt_frag_to_fill PARAMS ((segT, fragS *));
13233965Sjdpstatic void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
13333965Sjdpstatic void write_relocs PARAMS ((bfd *, asection *, PTR));
13433965Sjdpstatic void write_contents PARAMS ((bfd *, asection *, PTR));
13533965Sjdpstatic void set_symtab PARAMS ((void));
13633965Sjdp#endif
13733965Sjdp#if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
13833965Sjdpstatic void merge_data_into_text PARAMS ((void));
13933965Sjdp#endif
14033965Sjdp#if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
14133965Sjdpstatic void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
14233965Sjdpstatic void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
14333965Sjdpstatic void relax_and_size_all_segments PARAMS ((void));
14433965Sjdp#endif
14577298Sobrien#if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
14677298Sobrienstatic void set_segment_vma PARAMS ((bfd *, asection *, PTR));
14777298Sobrien#endif
14833965Sjdp
14977298Sobrien/* Create a fixS in obstack 'notes'.  */
15077298Sobrien
15133965Sjdpstatic fixS *
15233965Sjdpfix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
15333965Sjdp		  r_type)
15477298Sobrien     fragS *frag;		/* Which frag?  */
15577298Sobrien     int where;			/* Where in that frag?  */
15677298Sobrien     int size;			/* 1, 2, or 4 usually.  */
15777298Sobrien     symbolS *add_symbol;	/* X_add_symbol.  */
15877298Sobrien     symbolS *sub_symbol;	/* X_op_symbol.  */
15977298Sobrien     offsetT offset;		/* X_add_number.  */
16077298Sobrien     int pcrel;			/* TRUE if PC-relative relocation.  */
16133965Sjdp#ifdef BFD_ASSEMBLER
16277298Sobrien     bfd_reloc_code_real_type r_type; /* Relocation type.  */
16333965Sjdp#else
16477298Sobrien     int r_type;		/* Relocation type.  */
16533965Sjdp#endif
16633965Sjdp{
16733965Sjdp  fixS *fixP;
16833965Sjdp
16933965Sjdp  n_fixups++;
17033965Sjdp
17133965Sjdp  fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
17233965Sjdp
17333965Sjdp  fixP->fx_frag = frag;
17433965Sjdp  fixP->fx_where = where;
17533965Sjdp  fixP->fx_size = size;
17633965Sjdp  /* We've made fx_size a narrow field; check that it's wide enough.  */
17733965Sjdp  if (fixP->fx_size != size)
17833965Sjdp    {
17960484Sobrien      as_bad (_("field fx_size too small to hold %d"), size);
18033965Sjdp      abort ();
18133965Sjdp    }
18233965Sjdp  fixP->fx_addsy = add_symbol;
18333965Sjdp  fixP->fx_subsy = sub_symbol;
18433965Sjdp  fixP->fx_offset = offset;
18533965Sjdp  fixP->fx_pcrel = pcrel;
18633965Sjdp  fixP->fx_plt = 0;
18733965Sjdp#if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
18833965Sjdp  fixP->fx_r_type = r_type;
18933965Sjdp#endif
19033965Sjdp  fixP->fx_im_disp = 0;
19133965Sjdp  fixP->fx_pcrel_adjust = 0;
19233965Sjdp  fixP->fx_bit_fixP = 0;
19333965Sjdp  fixP->fx_addnumber = 0;
19433965Sjdp  fixP->fx_tcbit = 0;
19533965Sjdp  fixP->fx_done = 0;
19633965Sjdp  fixP->fx_no_overflow = 0;
19733965Sjdp  fixP->fx_signed = 0;
19833965Sjdp
19960484Sobrien#ifdef USING_CGEN
20060484Sobrien  fixP->fx_cgen.insn = NULL;
20160484Sobrien  fixP->fx_cgen.opinfo = 0;
20260484Sobrien#endif
20360484Sobrien
20433965Sjdp#ifdef TC_FIX_TYPE
20577298Sobrien  TC_INIT_FIX_DATA (fixP);
20633965Sjdp#endif
20733965Sjdp
20833965Sjdp  as_where (&fixP->fx_file, &fixP->fx_line);
20933965Sjdp
21033965Sjdp  /* Usually, we want relocs sorted numerically, but while
21133965Sjdp     comparing to older versions of gas that have relocs
21233965Sjdp     reverse sorted, it is convenient to have this compile
21377298Sobrien     time option.  xoxorich.  */
21433965Sjdp  {
21533965Sjdp
21633965Sjdp#ifdef BFD_ASSEMBLER
21733965Sjdp    fixS **seg_fix_rootP = (frags_chained
21833965Sjdp			    ? &seg_info (now_seg)->fix_root
21933965Sjdp			    : &frchain_now->fix_root);
22033965Sjdp    fixS **seg_fix_tailP = (frags_chained
22133965Sjdp			    ? &seg_info (now_seg)->fix_tail
22233965Sjdp			    : &frchain_now->fix_tail);
22333965Sjdp#endif
22433965Sjdp
22533965Sjdp#ifdef REVERSE_SORT_RELOCS
22633965Sjdp
22733965Sjdp    fixP->fx_next = *seg_fix_rootP;
22833965Sjdp    *seg_fix_rootP = fixP;
22933965Sjdp
23077298Sobrien#else /* REVERSE_SORT_RELOCS  */
23133965Sjdp
23233965Sjdp    fixP->fx_next = NULL;
23333965Sjdp
23433965Sjdp    if (*seg_fix_tailP)
23533965Sjdp      (*seg_fix_tailP)->fx_next = fixP;
23633965Sjdp    else
23733965Sjdp      *seg_fix_rootP = fixP;
23833965Sjdp    *seg_fix_tailP = fixP;
23933965Sjdp
24077298Sobrien#endif /* REVERSE_SORT_RELOCS  */
24133965Sjdp  }
24233965Sjdp
24333965Sjdp  return fixP;
24433965Sjdp}
24533965Sjdp
24633965Sjdp/* Create a fixup relative to a symbol (plus a constant).  */
24733965Sjdp
24833965SjdpfixS *
24933965Sjdpfix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
25077298Sobrien     fragS *frag;		/* Which frag?  */
25177298Sobrien     int where;			/* Where in that frag?  */
25277298Sobrien     int size;			/* 1, 2, or 4 usually.  */
25377298Sobrien     symbolS *add_symbol;	/* X_add_symbol.  */
25477298Sobrien     offsetT offset;		/* X_add_number.  */
25577298Sobrien     int pcrel;			/* TRUE if PC-relative relocation.  */
25633965Sjdp#ifdef BFD_ASSEMBLER
25777298Sobrien     bfd_reloc_code_real_type r_type; /* Relocation type.  */
25833965Sjdp#else
25977298Sobrien     int r_type;		/* Relocation type.  */
26033965Sjdp#endif
26133965Sjdp{
26233965Sjdp  return fix_new_internal (frag, where, size, add_symbol,
26333965Sjdp			   (symbolS *) NULL, offset, pcrel, r_type);
26433965Sjdp}
26533965Sjdp
26633965Sjdp/* Create a fixup for an expression.  Currently we only support fixups
26733965Sjdp   for difference expressions.  That is itself more than most object
26833965Sjdp   file formats support anyhow.  */
26933965Sjdp
27033965SjdpfixS *
27133965Sjdpfix_new_exp (frag, where, size, exp, pcrel, r_type)
27277298Sobrien     fragS *frag;		/* Which frag?  */
27377298Sobrien     int where;			/* Where in that frag?  */
27477298Sobrien     int size;			/* 1, 2, or 4 usually.  */
27533965Sjdp     expressionS *exp;		/* Expression.  */
27677298Sobrien     int pcrel;			/* TRUE if PC-relative relocation.  */
27733965Sjdp#ifdef BFD_ASSEMBLER
27877298Sobrien     bfd_reloc_code_real_type r_type; /* Relocation type.  */
27933965Sjdp#else
28077298Sobrien     int r_type;		/* Relocation type.  */
28133965Sjdp#endif
28233965Sjdp{
28333965Sjdp  symbolS *add = NULL;
28433965Sjdp  symbolS *sub = NULL;
28533965Sjdp  offsetT off = 0;
28633965Sjdp
28733965Sjdp  switch (exp->X_op)
28833965Sjdp    {
28933965Sjdp    case O_absent:
29033965Sjdp      break;
29133965Sjdp
29278828Sobrien    case O_register:
29378828Sobrien      as_bad (_("register value used as expression"));
29478828Sobrien      break;
29578828Sobrien
29633965Sjdp    case O_add:
29733965Sjdp      /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
29833965Sjdp	 the difference expression cannot immediately be reduced.  */
29933965Sjdp      {
30033965Sjdp	symbolS *stmp = make_expr_symbol (exp);
30177298Sobrien
30233965Sjdp	exp->X_op = O_symbol;
30333965Sjdp	exp->X_op_symbol = 0;
30433965Sjdp	exp->X_add_symbol = stmp;
30533965Sjdp	exp->X_add_number = 0;
30677298Sobrien
30733965Sjdp	return fix_new_exp (frag, where, size, exp, pcrel, r_type);
30833965Sjdp      }
30933965Sjdp
31033965Sjdp    case O_symbol_rva:
31133965Sjdp      add = exp->X_add_symbol;
31233965Sjdp      off = exp->X_add_number;
31333965Sjdp
31433965Sjdp#if defined(BFD_ASSEMBLER)
31533965Sjdp      r_type = BFD_RELOC_RVA;
31633965Sjdp#else
31733965Sjdp#if defined(TC_RVA_RELOC)
31833965Sjdp      r_type = TC_RVA_RELOC;
31933965Sjdp#else
32077298Sobrien      as_fatal (_("rva not supported"));
32133965Sjdp#endif
32233965Sjdp#endif
32333965Sjdp      break;
32433965Sjdp
32533965Sjdp    case O_uminus:
32633965Sjdp      sub = exp->X_add_symbol;
32733965Sjdp      off = exp->X_add_number;
32833965Sjdp      break;
32933965Sjdp
33033965Sjdp    case O_subtract:
33133965Sjdp      sub = exp->X_op_symbol;
33233965Sjdp      /* Fall through.  */
33333965Sjdp    case O_symbol:
33433965Sjdp      add = exp->X_add_symbol;
33577298Sobrien      /* Fall through.  */
33633965Sjdp    case O_constant:
33733965Sjdp      off = exp->X_add_number;
33833965Sjdp      break;
33933965Sjdp
34033965Sjdp    default:
34133965Sjdp      add = make_expr_symbol (exp);
34233965Sjdp      break;
34333965Sjdp    }
34433965Sjdp
34577298Sobrien  return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
34633965Sjdp}
34733965Sjdp
34833965Sjdp/* Append a string onto another string, bumping the pointer along.  */
34933965Sjdpvoid
35033965Sjdpappend (charPP, fromP, length)
35133965Sjdp     char **charPP;
35233965Sjdp     char *fromP;
35333965Sjdp     unsigned long length;
35433965Sjdp{
35577298Sobrien  /* Don't trust memcpy() of 0 chars.  */
35633965Sjdp  if (length == 0)
35733965Sjdp    return;
35833965Sjdp
35933965Sjdp  memcpy (*charPP, fromP, length);
36033965Sjdp  *charPP += length;
36133965Sjdp}
36233965Sjdp
36333965Sjdp#ifndef BFD_ASSEMBLER
36433965Sjdpint section_alignment[SEG_MAXIMUM_ORDINAL];
36533965Sjdp#endif
36633965Sjdp
36777298Sobrien/* This routine records the largest alignment seen for each segment.
36877298Sobrien   If the beginning of the segment is aligned on the worst-case
36977298Sobrien   boundary, all of the other alignments within it will work.  At
37077298Sobrien   least one object format really uses this info.  */
37177298Sobrien
37233965Sjdpvoid
37333965Sjdprecord_alignment (seg, align)
37477298Sobrien     /* Segment to which alignment pertains.  */
37533965Sjdp     segT seg;
37633965Sjdp     /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
37733965Sjdp	boundary, etc.)  */
37833965Sjdp     int align;
37933965Sjdp{
38033965Sjdp  if (seg == absolute_section)
38133965Sjdp    return;
38233965Sjdp#ifdef BFD_ASSEMBLER
38338889Sjdp  if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
38433965Sjdp    bfd_set_section_alignment (stdoutput, seg, align);
38533965Sjdp#else
38633965Sjdp  if (align > section_alignment[(int) seg])
38733965Sjdp    section_alignment[(int) seg] = align;
38833965Sjdp#endif
38933965Sjdp}
39033965Sjdp
39177298Sobrienint
39277298Sobrienget_recorded_alignment (seg)
39377298Sobrien     segT seg;
39477298Sobrien{
39577298Sobrien  if (seg == absolute_section)
39677298Sobrien    return 0;
39733965Sjdp#ifdef BFD_ASSEMBLER
39877298Sobrien  return bfd_get_section_alignment (stdoutput, seg);
39977298Sobrien#else
40077298Sobrien  return section_alignment[(int) seg];
40177298Sobrien#endif
40277298Sobrien}
40333965Sjdp
40477298Sobrien#ifdef BFD_ASSEMBLER
40577298Sobrien
40633965Sjdp/* Reset the section indices after removing the gas created sections.  */
40733965Sjdp
40833965Sjdpstatic void
40933965Sjdprenumber_sections (abfd, sec, countparg)
41060484Sobrien     bfd *abfd ATTRIBUTE_UNUSED;
41133965Sjdp     asection *sec;
41233965Sjdp     PTR countparg;
41333965Sjdp{
41433965Sjdp  int *countp = (int *) countparg;
41533965Sjdp
41633965Sjdp  sec->index = *countp;
41733965Sjdp  ++*countp;
41833965Sjdp}
41933965Sjdp
42077298Sobrien#endif /* defined (BFD_ASSEMBLER)  */
42133965Sjdp
42233965Sjdp#if defined (BFD_ASSEMBLER) || ! defined (BFD)
42333965Sjdp
42433965Sjdpstatic fragS *
42533965Sjdpchain_frchains_together_1 (section, frchp)
42633965Sjdp     segT section;
42733965Sjdp     struct frchain *frchp;
42833965Sjdp{
42933965Sjdp  fragS dummy, *prev_frag = &dummy;
43033965Sjdp#ifdef BFD_ASSEMBLER
43133965Sjdp  fixS fix_dummy, *prev_fix = &fix_dummy;
43233965Sjdp#endif
43333965Sjdp
43433965Sjdp  for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
43533965Sjdp    {
43633965Sjdp      prev_frag->fr_next = frchp->frch_root;
43733965Sjdp      prev_frag = frchp->frch_last;
43833965Sjdp      assert (prev_frag->fr_type != 0);
43933965Sjdp#ifdef BFD_ASSEMBLER
44033965Sjdp      if (frchp->fix_root != (fixS *) NULL)
44133965Sjdp	{
44233965Sjdp	  if (seg_info (section)->fix_root == (fixS *) NULL)
44333965Sjdp	    seg_info (section)->fix_root = frchp->fix_root;
44433965Sjdp	  prev_fix->fx_next = frchp->fix_root;
44533965Sjdp	  seg_info (section)->fix_tail = frchp->fix_tail;
44633965Sjdp	  prev_fix = frchp->fix_tail;
44733965Sjdp	}
44833965Sjdp#endif
44933965Sjdp    }
45033965Sjdp  assert (prev_frag->fr_type != 0);
45133965Sjdp  prev_frag->fr_next = 0;
45233965Sjdp  return prev_frag;
45333965Sjdp}
45433965Sjdp
45533965Sjdp#endif
45633965Sjdp
45733965Sjdp#ifdef BFD_ASSEMBLER
45833965Sjdp
45933965Sjdpstatic void
46033965Sjdpchain_frchains_together (abfd, section, xxx)
46160484Sobrien     bfd *abfd ATTRIBUTE_UNUSED;
46233965Sjdp     segT section;
46360484Sobrien     PTR xxx ATTRIBUTE_UNUSED;
46433965Sjdp{
46533965Sjdp  segment_info_type *info;
46633965Sjdp
46733965Sjdp  /* BFD may have introduced its own sections without using
46833965Sjdp     subseg_new, so it is possible that seg_info is NULL.  */
46933965Sjdp  info = seg_info (section);
47033965Sjdp  if (info != (segment_info_type *) NULL)
47177298Sobrien    info->frchainP->frch_last
47277298Sobrien      = chain_frchains_together_1 (section, info->frchainP);
47333965Sjdp
47433965Sjdp  /* Now that we've chained the frags together, we must add new fixups
47533965Sjdp     to the segment, not to the frag chain.  */
47633965Sjdp  frags_chained = 1;
47733965Sjdp}
47833965Sjdp
47933965Sjdp#endif
48033965Sjdp
48133965Sjdp#if !defined (BFD) && !defined (BFD_ASSEMBLER)
48233965Sjdp
48333965Sjdpstatic void
48433965Sjdpremove_subsegs (head, seg, root, last)
48533965Sjdp     frchainS *head;
48633965Sjdp     int seg;
48733965Sjdp     fragS **root;
48833965Sjdp     fragS **last;
48933965Sjdp{
49033965Sjdp  *root = head->frch_root;
49133965Sjdp  *last = chain_frchains_together_1 (seg, head);
49233965Sjdp}
49333965Sjdp
49477298Sobrien#endif /* BFD  */
49533965Sjdp
49633965Sjdp#if defined (BFD_ASSEMBLER) || !defined (BFD)
49733965Sjdp
49833965Sjdp#ifdef BFD_ASSEMBLER
49933965Sjdpstatic void
50033965Sjdpcvt_frag_to_fill (sec, fragP)
50177298Sobrien     segT sec ATTRIBUTE_UNUSED;
50233965Sjdp     fragS *fragP;
50333965Sjdp#else
50433965Sjdpstatic void
50533965Sjdpcvt_frag_to_fill (headersP, sec, fragP)
50633965Sjdp     object_headers *headersP;
50733965Sjdp     segT sec;
50833965Sjdp     fragS *fragP;
50933965Sjdp#endif
51033965Sjdp{
51133965Sjdp  switch (fragP->fr_type)
51233965Sjdp    {
51333965Sjdp    case rs_align:
51433965Sjdp    case rs_align_code:
51577298Sobrien    case rs_align_test:
51633965Sjdp    case rs_org:
51733965Sjdp    case rs_space:
51833965Sjdp#ifdef HANDLE_ALIGN
51933965Sjdp      HANDLE_ALIGN (fragP);
52033965Sjdp#endif
52133965Sjdp      know (fragP->fr_next != NULL);
52233965Sjdp      fragP->fr_offset = (fragP->fr_next->fr_address
52333965Sjdp			  - fragP->fr_address
52433965Sjdp			  - fragP->fr_fix) / fragP->fr_var;
52533965Sjdp      if (fragP->fr_offset < 0)
52633965Sjdp	{
52760484Sobrien	  as_bad_where (fragP->fr_file, fragP->fr_line,
52860484Sobrien			_("attempt to .org/.space backwards? (%ld)"),
52960484Sobrien			(long) fragP->fr_offset);
53033965Sjdp	}
53133965Sjdp      fragP->fr_type = rs_fill;
53233965Sjdp      break;
53333965Sjdp
53433965Sjdp    case rs_fill:
53533965Sjdp      break;
53633965Sjdp
53738889Sjdp    case rs_leb128:
53838889Sjdp      {
53938889Sjdp	valueT value = S_GET_VALUE (fragP->fr_symbol);
54038889Sjdp	int size;
54138889Sjdp
54238889Sjdp	size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
54338889Sjdp			      fragP->fr_subtype);
54438889Sjdp
54538889Sjdp	fragP->fr_fix += size;
54638889Sjdp	fragP->fr_type = rs_fill;
54738889Sjdp	fragP->fr_var = 0;
54838889Sjdp	fragP->fr_offset = 0;
54938889Sjdp	fragP->fr_symbol = NULL;
55038889Sjdp      }
55138889Sjdp      break;
55238889Sjdp
55338889Sjdp    case rs_cfa:
55438889Sjdp      eh_frame_convert_frag (fragP);
55538889Sjdp      break;
55638889Sjdp
55777298Sobrien    case rs_dwarf2dbg:
55877298Sobrien      dwarf2dbg_convert_frag (fragP);
55977298Sobrien      break;
56077298Sobrien
56133965Sjdp    case rs_machine_dependent:
56233965Sjdp#ifdef BFD_ASSEMBLER
56333965Sjdp      md_convert_frag (stdoutput, sec, fragP);
56433965Sjdp#else
56533965Sjdp      md_convert_frag (headersP, sec, fragP);
56633965Sjdp#endif
56733965Sjdp
56838889Sjdp      assert (fragP->fr_next == NULL
56938889Sjdp	      || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
57038889Sjdp		  == fragP->fr_fix));
57133965Sjdp
57277298Sobrien      /* After md_convert_frag, we make the frag into a ".space 0".
57377298Sobrien	 md_convert_frag() should set up any fixSs and constants
57477298Sobrien	 required.  */
57533965Sjdp      frag_wane (fragP);
57633965Sjdp      break;
57733965Sjdp
57833965Sjdp#ifndef WORKING_DOT_WORD
57933965Sjdp    case rs_broken_word:
58033965Sjdp      {
58133965Sjdp	struct broken_word *lie;
58233965Sjdp
58333965Sjdp	if (fragP->fr_subtype)
58433965Sjdp	  {
58533965Sjdp	    fragP->fr_fix += md_short_jump_size;
58633965Sjdp	    for (lie = (struct broken_word *) (fragP->fr_symbol);
58733965Sjdp		 lie && lie->dispfrag == fragP;
58833965Sjdp		 lie = lie->next_broken_word)
58933965Sjdp	      if (lie->added == 1)
59033965Sjdp		fragP->fr_fix += md_long_jump_size;
59133965Sjdp	  }
59233965Sjdp	frag_wane (fragP);
59333965Sjdp      }
59433965Sjdp      break;
59533965Sjdp#endif
59633965Sjdp
59733965Sjdp    default:
59833965Sjdp      BAD_CASE (fragP->fr_type);
59933965Sjdp      break;
60033965Sjdp    }
60133965Sjdp}
60233965Sjdp
60377298Sobrien#endif /* defined (BFD_ASSEMBLER) || !defined (BFD)  */
60433965Sjdp
60533965Sjdp#ifdef BFD_ASSEMBLER
60689857Sobrienstatic void relax_seg PARAMS ((bfd *, asection *, PTR));
60733965Sjdpstatic void
60889857Sobrienrelax_seg (abfd, sec, xxx)
60989857Sobrien     bfd *abfd ATTRIBUTE_UNUSED;
61033965Sjdp     asection *sec;
61189857Sobrien     PTR xxx;
61277298Sobrien{
61377298Sobrien  segment_info_type *seginfo = seg_info (sec);
61477298Sobrien
61589857Sobrien  if (seginfo && seginfo->frchainP
61689857Sobrien      && relax_segment (seginfo->frchainP->frch_root, sec))
61789857Sobrien    {
61889857Sobrien      int *result = (int *) xxx;
61989857Sobrien      *result = 1;
62089857Sobrien    }
62177298Sobrien}
62277298Sobrien
62389857Sobrienstatic void size_seg PARAMS ((bfd *, asection *, PTR));
62477298Sobrienstatic void
62577298Sobriensize_seg (abfd, sec, xxx)
62677298Sobrien     bfd *abfd;
62777298Sobrien     asection *sec;
62860484Sobrien     PTR xxx ATTRIBUTE_UNUSED;
62933965Sjdp{
63033965Sjdp  flagword flags;
63133965Sjdp  fragS *fragp;
63233965Sjdp  segment_info_type *seginfo;
63333965Sjdp  int x;
63433965Sjdp  valueT size, newsize;
63533965Sjdp
63633965Sjdp  subseg_change (sec, 0);
63733965Sjdp
63833965Sjdp  seginfo = seg_info (sec);
63933965Sjdp  if (seginfo && seginfo->frchainP)
64033965Sjdp    {
64133965Sjdp      for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
64233965Sjdp	cvt_frag_to_fill (sec, fragp);
64333965Sjdp      for (fragp = seginfo->frchainP->frch_root;
64433965Sjdp	   fragp->fr_next;
64533965Sjdp	   fragp = fragp->fr_next)
64677298Sobrien	/* Walk to last elt.  */
64777298Sobrien	;
64833965Sjdp      size = fragp->fr_address + fragp->fr_fix;
64933965Sjdp    }
65033965Sjdp  else
65133965Sjdp    size = 0;
65233965Sjdp
65377298Sobrien  flags = bfd_get_section_flags (abfd, sec);
65477298Sobrien
65533965Sjdp  if (size > 0 && ! seginfo->bss)
65633965Sjdp    flags |= SEC_HAS_CONTENTS;
65733965Sjdp
65833965Sjdp  /* @@ This is just an approximation.  */
65933965Sjdp  if (seginfo && seginfo->fix_root)
66033965Sjdp    flags |= SEC_RELOC;
66133965Sjdp  else
66233965Sjdp    flags &= ~SEC_RELOC;
66333965Sjdp  x = bfd_set_section_flags (abfd, sec, flags);
66433965Sjdp  assert (x == true);
66533965Sjdp
66633965Sjdp  newsize = md_section_align (sec, size);
66733965Sjdp  x = bfd_set_section_size (abfd, sec, newsize);
66833965Sjdp  assert (x == true);
66933965Sjdp
67033965Sjdp  /* If the size had to be rounded up, add some padding in the last
67133965Sjdp     non-empty frag.  */
67233965Sjdp  assert (newsize >= size);
67333965Sjdp  if (size != newsize)
67433965Sjdp    {
67533965Sjdp      fragS *last = seginfo->frchainP->frch_last;
67633965Sjdp      fragp = seginfo->frchainP->frch_root;
67733965Sjdp      while (fragp->fr_next != last)
67833965Sjdp	fragp = fragp->fr_next;
67933965Sjdp      last->fr_address = size;
68033965Sjdp      fragp->fr_offset += newsize - size;
68133965Sjdp    }
68233965Sjdp
68333965Sjdp#ifdef tc_frob_section
68433965Sjdp  tc_frob_section (sec);
68533965Sjdp#endif
68633965Sjdp#ifdef obj_frob_section
68733965Sjdp  obj_frob_section (sec);
68833965Sjdp#endif
68933965Sjdp}
69033965Sjdp
69133965Sjdp#ifdef DEBUG2
69233965Sjdpstatic void
69333965Sjdpdump_section_relocs (abfd, sec, stream_)
69460484Sobrien     bfd *abfd ATTRIBUTE_UNUSED;
69533965Sjdp     asection *sec;
69633965Sjdp     char *stream_;
69733965Sjdp{
69833965Sjdp  FILE *stream = (FILE *) stream_;
69933965Sjdp  segment_info_type *seginfo = seg_info (sec);
70033965Sjdp  fixS *fixp = seginfo->fix_root;
70133965Sjdp
70233965Sjdp  if (!fixp)
70333965Sjdp    return;
70433965Sjdp
70533965Sjdp  fprintf (stream, "sec %s relocs:\n", sec->name);
70633965Sjdp  while (fixp)
70733965Sjdp    {
70833965Sjdp      symbolS *s = fixp->fx_addsy;
70960484Sobrien
71060484Sobrien      fprintf (stream, "  %08lx: type %d ", (unsigned long) fixp,
71160484Sobrien	       (int) fixp->fx_r_type);
71260484Sobrien      if (s == NULL)
71360484Sobrien	fprintf (stream, "no sym\n");
71460484Sobrien      else
71533965Sjdp	{
71660484Sobrien	  print_symbol_value_1 (stream, s);
71760484Sobrien	  fprintf (stream, "\n");
71833965Sjdp	}
71933965Sjdp      fixp = fixp->fx_next;
72033965Sjdp    }
72133965Sjdp}
72233965Sjdp#else
72333965Sjdp#define dump_section_relocs(ABFD,SEC,STREAM)	((void) 0)
72433965Sjdp#endif
72533965Sjdp
72633965Sjdp#ifndef EMIT_SECTION_SYMBOLS
72733965Sjdp#define EMIT_SECTION_SYMBOLS 1
72833965Sjdp#endif
72933965Sjdp
73033965Sjdpstatic void
73133965Sjdpadjust_reloc_syms (abfd, sec, xxx)
73260484Sobrien     bfd *abfd ATTRIBUTE_UNUSED;
73333965Sjdp     asection *sec;
73460484Sobrien     PTR xxx ATTRIBUTE_UNUSED;
73533965Sjdp{
73633965Sjdp  segment_info_type *seginfo = seg_info (sec);
73733965Sjdp  fixS *fixp;
73833965Sjdp
73933965Sjdp  if (seginfo == NULL)
74033965Sjdp    return;
74133965Sjdp
74233965Sjdp  dump_section_relocs (abfd, sec, stderr);
74333965Sjdp
74433965Sjdp  for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
74533965Sjdp    if (fixp->fx_done)
74677298Sobrien      /* Ignore it.  */
74777298Sobrien      ;
74833965Sjdp    else if (fixp->fx_addsy)
74933965Sjdp      {
75033965Sjdp	symbolS *sym;
75133965Sjdp	asection *symsec;
75233965Sjdp
75333965Sjdp#ifdef DEBUG5
75433965Sjdp	fprintf (stderr, "\n\nadjusting fixup:\n");
75533965Sjdp	print_fixup (fixp);
75633965Sjdp#endif
75733965Sjdp
75833965Sjdp	sym = fixp->fx_addsy;
75933965Sjdp
76033965Sjdp	/* All symbols should have already been resolved at this
76133965Sjdp	   point.  It is possible to see unresolved expression
76233965Sjdp	   symbols, though, since they are not in the regular symbol
76333965Sjdp	   table.  */
76460484Sobrien	if (sym != NULL)
76589857Sobrien	  resolve_symbol_value (sym);
76677298Sobrien
76760484Sobrien	if (fixp->fx_subsy != NULL)
76889857Sobrien	  resolve_symbol_value (fixp->fx_subsy);
76933965Sjdp
77033965Sjdp	/* If this symbol is equated to an undefined symbol, convert
77133965Sjdp           the fixup to being against that symbol.  */
77289857Sobrien	if (sym != NULL && symbol_equated_reloc_p (sym))
77333965Sjdp	  {
77460484Sobrien	    fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
77560484Sobrien	    sym = symbol_get_value_expression (sym)->X_add_symbol;
77633965Sjdp	    fixp->fx_addsy = sym;
77733965Sjdp	  }
77833965Sjdp
77960484Sobrien	if (sym != NULL && symbol_mri_common_p (sym))
78033965Sjdp	  {
78133965Sjdp	    /* These symbols are handled specially in fixup_segment.  */
78233965Sjdp	    goto done;
78333965Sjdp	  }
78433965Sjdp
78560484Sobrien	symsec = S_GET_SEGMENT (sym);
78660484Sobrien
78760484Sobrien	if (symsec == NULL)
78860484Sobrien	  abort ();
78977298Sobrien
79033965Sjdp	if (bfd_is_abs_section (symsec))
79133965Sjdp	  {
79233965Sjdp	    /* The fixup_segment routine will not use this symbol in a
79333965Sjdp               relocation unless TC_FORCE_RELOCATION returns 1.  */
79433965Sjdp	    if (TC_FORCE_RELOCATION (fixp))
79533965Sjdp	      {
79660484Sobrien		symbol_mark_used_in_reloc (fixp->fx_addsy);
79733965Sjdp#ifdef UNDEFINED_DIFFERENCE_OK
79833965Sjdp		if (fixp->fx_subsy != NULL)
79960484Sobrien		  symbol_mark_used_in_reloc (fixp->fx_subsy);
80033965Sjdp#endif
80133965Sjdp	      }
80233965Sjdp	    goto done;
80333965Sjdp	  }
80433965Sjdp
80533965Sjdp	/* If it's one of these sections, assume the symbol is
80633965Sjdp	   definitely going to be output.  The code in
80733965Sjdp	   md_estimate_size_before_relax in tc-mips.c uses this test
80833965Sjdp	   as well, so if you change this code you should look at that
80933965Sjdp	   code.  */
81033965Sjdp	if (bfd_is_und_section (symsec)
81133965Sjdp	    || bfd_is_com_section (symsec))
81233965Sjdp	  {
81360484Sobrien	    symbol_mark_used_in_reloc (fixp->fx_addsy);
81433965Sjdp#ifdef UNDEFINED_DIFFERENCE_OK
81533965Sjdp	    /* We have the difference of an undefined symbol and some
81633965Sjdp	       other symbol.  Make sure to mark the other symbol as used
81733965Sjdp	       in a relocation so that it will always be output.  */
81833965Sjdp	    if (fixp->fx_subsy)
81960484Sobrien	      symbol_mark_used_in_reloc (fixp->fx_subsy);
82033965Sjdp#endif
82133965Sjdp	    goto done;
82233965Sjdp	  }
82333965Sjdp
82460484Sobrien	/* Don't try to reduce relocs which refer to non-local symbols
82560484Sobrien           in .linkonce sections.  It can lead to confusion when a
82660484Sobrien           debugging section refers to a .linkonce section.  I hope
82760484Sobrien           this will always be correct.  */
82860484Sobrien	if (symsec != sec && ! S_IS_LOCAL (sym))
82933965Sjdp	  {
83033965Sjdp	    boolean linkonce;
83133965Sjdp
83233965Sjdp	    linkonce = false;
83333965Sjdp#ifdef BFD_ASSEMBLER
83433965Sjdp	    if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
83533965Sjdp		!= 0)
83633965Sjdp	      linkonce = true;
83733965Sjdp#endif
83833965Sjdp#ifdef OBJ_ELF
83933965Sjdp	    /* The GNU toolchain uses an extension for ELF: a section
84033965Sjdp               beginning with the magic string .gnu.linkonce is a
84133965Sjdp               linkonce section.  */
84233965Sjdp	    if (strncmp (segment_name (symsec), ".gnu.linkonce",
84333965Sjdp			 sizeof ".gnu.linkonce" - 1) == 0)
84433965Sjdp	      linkonce = true;
84533965Sjdp#endif
84633965Sjdp
84733965Sjdp	    if (linkonce)
84833965Sjdp	      {
84960484Sobrien		symbol_mark_used_in_reloc (fixp->fx_addsy);
85033965Sjdp#ifdef UNDEFINED_DIFFERENCE_OK
85133965Sjdp		if (fixp->fx_subsy != NULL)
85260484Sobrien		  symbol_mark_used_in_reloc (fixp->fx_subsy);
85333965Sjdp#endif
85433965Sjdp		goto done;
85533965Sjdp	      }
85633965Sjdp	  }
85733965Sjdp
85833965Sjdp	/* Since we're reducing to section symbols, don't attempt to reduce
85933965Sjdp	   anything that's already using one.  */
86060484Sobrien	if (symbol_section_p (sym))
86133965Sjdp	  {
86260484Sobrien	    symbol_mark_used_in_reloc (fixp->fx_addsy);
86333965Sjdp	    goto done;
86433965Sjdp	  }
86533965Sjdp
86660484Sobrien#ifdef BFD_ASSEMBLER
86760484Sobrien	/* We can never adjust a reloc against a weak symbol.  If we
86860484Sobrien           did, and the weak symbol was overridden by a real symbol
86960484Sobrien           somewhere else, then our relocation would be pointing at
87060484Sobrien           the wrong area of memory.  */
87160484Sobrien	if (S_IS_WEAK (sym))
87260484Sobrien	  {
87360484Sobrien	    symbol_mark_used_in_reloc (fixp->fx_addsy);
87460484Sobrien	    goto done;
87560484Sobrien	  }
87689857Sobrien
87789857Sobrien	/* Never adjust a reloc against local symbol in a merge section
87889857Sobrien	   with non-zero addend.  */
87989857Sobrien	if ((symsec->flags & SEC_MERGE) && fixp->fx_offset)
88089857Sobrien	  {
88189857Sobrien	    symbol_mark_used_in_reloc (fixp->fx_addsy);
88289857Sobrien	    goto done;
88389857Sobrien	  }
88460484Sobrien#endif
88560484Sobrien
88633965Sjdp	/* Is there some other reason we can't adjust this one?  (E.g.,
88733965Sjdp	   call/bal links in i960-bout symbols.)  */
88833965Sjdp#ifdef obj_fix_adjustable
88933965Sjdp	if (! obj_fix_adjustable (fixp))
89033965Sjdp	  {
89160484Sobrien	    symbol_mark_used_in_reloc (fixp->fx_addsy);
89233965Sjdp	    goto done;
89333965Sjdp	  }
89433965Sjdp#endif
89533965Sjdp
89633965Sjdp	/* Is there some other (target cpu dependent) reason we can't adjust
89733965Sjdp	   this one?  (E.g. relocations involving function addresses on
89833965Sjdp	   the PA.  */
89933965Sjdp#ifdef tc_fix_adjustable
90033965Sjdp	if (! tc_fix_adjustable (fixp))
90133965Sjdp	  {
90260484Sobrien	    symbol_mark_used_in_reloc (fixp->fx_addsy);
90333965Sjdp	    goto done;
90433965Sjdp	  }
90533965Sjdp#endif
90633965Sjdp
90733965Sjdp	/* If the section symbol isn't going to be output, the relocs
90833965Sjdp	   at least should still work.  If not, figure out what to do
90933965Sjdp	   when we run into that case.
91033965Sjdp
91133965Sjdp	   We refetch the segment when calling section_symbol, rather
91233965Sjdp	   than using symsec, because S_GET_VALUE may wind up changing
91377298Sobrien	   the section when it calls resolve_symbol_value.  */
91433965Sjdp	fixp->fx_offset += S_GET_VALUE (sym);
91533965Sjdp	fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
91660484Sobrien	symbol_mark_used_in_reloc (fixp->fx_addsy);
91760484Sobrien#ifdef DEBUG5
91860484Sobrien	fprintf (stderr, "\nadjusted fixup:\n");
91960484Sobrien	print_fixup (fixp);
92060484Sobrien#endif
92133965Sjdp
92233965Sjdp      done:
92333965Sjdp	;
92433965Sjdp      }
92577298Sobrien#if 1 /* def RELOC_REQUIRES_SYMBOL  */
92633965Sjdp    else
92733965Sjdp      {
92833965Sjdp	/* There was no symbol required by this relocation.  However,
92933965Sjdp	   BFD doesn't really handle relocations without symbols well.
93033965Sjdp	   (At least, the COFF support doesn't.)  So for now we fake up
93133965Sjdp	   a local symbol in the absolute section.  */
93233965Sjdp
93333965Sjdp	fixp->fx_addsy = section_symbol (absolute_section);
93477298Sobrien#if 0
93577298Sobrien	fixp->fx_addsy->sy_used_in_reloc = 1;
93677298Sobrien#endif
93733965Sjdp      }
93833965Sjdp#endif
93933965Sjdp
94033965Sjdp  dump_section_relocs (abfd, sec, stderr);
94133965Sjdp}
94233965Sjdp
94333965Sjdpstatic void
94433965Sjdpwrite_relocs (abfd, sec, xxx)
94533965Sjdp     bfd *abfd;
94633965Sjdp     asection *sec;
94760484Sobrien     PTR xxx ATTRIBUTE_UNUSED;
94833965Sjdp{
94933965Sjdp  segment_info_type *seginfo = seg_info (sec);
95077298Sobrien  unsigned int i;
95133965Sjdp  unsigned int n;
95233965Sjdp  arelent **relocs;
95333965Sjdp  fixS *fixp;
95433965Sjdp  char *err;
95533965Sjdp
95633965Sjdp  /* If seginfo is NULL, we did not create this section; don't do
95733965Sjdp     anything with it.  */
95833965Sjdp  if (seginfo == NULL)
95933965Sjdp    return;
96033965Sjdp
96133965Sjdp  fixup_segment (seginfo->fix_root, sec);
96233965Sjdp
96333965Sjdp  n = 0;
96433965Sjdp  for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
96533965Sjdp    n++;
96633965Sjdp
96733965Sjdp#ifndef RELOC_EXPANSION_POSSIBLE
96833965Sjdp  /* Set up reloc information as well.  */
96933965Sjdp  relocs = (arelent **) xmalloc (n * sizeof (arelent *));
97077298Sobrien  memset ((char *) relocs, 0, n * sizeof (arelent *));
97133965Sjdp
97233965Sjdp  i = 0;
97333965Sjdp  for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
97433965Sjdp    {
97533965Sjdp      arelent *reloc;
97633965Sjdp      bfd_reloc_status_type s;
97733965Sjdp      symbolS *sym;
97833965Sjdp
97933965Sjdp      if (fixp->fx_done)
98033965Sjdp	{
98133965Sjdp	  n--;
98233965Sjdp	  continue;
98333965Sjdp	}
98433965Sjdp
98533965Sjdp      /* If this is an undefined symbol which was equated to another
98689857Sobrien         symbol, then generate the reloc against the latter symbol
98733965Sjdp         rather than the former.  */
98833965Sjdp      sym = fixp->fx_addsy;
98989857Sobrien      while (symbol_equated_reloc_p (sym))
99033965Sjdp	{
99133965Sjdp	  symbolS *n;
99233965Sjdp
99333965Sjdp	  /* We must avoid looping, as that can occur with a badly
99433965Sjdp	     written program.  */
99560484Sobrien	  n = symbol_get_value_expression (sym)->X_add_symbol;
99633965Sjdp	  if (n == sym)
99733965Sjdp	    break;
99860484Sobrien	  fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
99933965Sjdp	  sym = n;
100033965Sjdp	}
100133965Sjdp      fixp->fx_addsy = sym;
100233965Sjdp
100333965Sjdp      reloc = tc_gen_reloc (sec, fixp);
100433965Sjdp      if (!reloc)
100533965Sjdp	{
100633965Sjdp	  n--;
100733965Sjdp	  continue;
100833965Sjdp	}
100933965Sjdp
101033965Sjdp#if 0
101133965Sjdp      /* This test is triggered inappropriately for the SH.  */
101233965Sjdp      if (fixp->fx_where + fixp->fx_size
101333965Sjdp	  > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
101433965Sjdp	abort ();
101533965Sjdp#endif
101633965Sjdp
101733965Sjdp      s = bfd_install_relocation (stdoutput, reloc,
101833965Sjdp				  fixp->fx_frag->fr_literal,
101933965Sjdp				  fixp->fx_frag->fr_address,
102033965Sjdp				  sec, &err);
102133965Sjdp      switch (s)
102233965Sjdp	{
102333965Sjdp	case bfd_reloc_ok:
102433965Sjdp	  break;
102533965Sjdp	case bfd_reloc_overflow:
102660484Sobrien	  as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
102733965Sjdp	  break;
102860484Sobrien	case bfd_reloc_outofrange:
102960484Sobrien	  as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation out of range"));
103060484Sobrien	  break;
103133965Sjdp	default:
103260484Sobrien	  as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
103360484Sobrien		    fixp->fx_file, fixp->fx_line, s);
103433965Sjdp	}
103533965Sjdp      relocs[i++] = reloc;
103633965Sjdp    }
103733965Sjdp#else
103833965Sjdp  n = n * MAX_RELOC_EXPANSION;
103933965Sjdp  /* Set up reloc information as well.  */
104033965Sjdp  relocs = (arelent **) xmalloc (n * sizeof (arelent *));
104133965Sjdp
104233965Sjdp  i = 0;
104333965Sjdp  for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
104433965Sjdp    {
104533965Sjdp      arelent **reloc;
104633965Sjdp      char *data;
104733965Sjdp      bfd_reloc_status_type s;
104833965Sjdp      symbolS *sym;
104933965Sjdp      int j;
105033965Sjdp
105133965Sjdp      if (fixp->fx_done)
105233965Sjdp	{
105333965Sjdp	  n--;
105433965Sjdp	  continue;
105533965Sjdp	}
105633965Sjdp
105733965Sjdp      /* If this is an undefined symbol which was equated to another
105877298Sobrien         symbol, then generate the reloc against the latter symbol
105933965Sjdp         rather than the former.  */
106033965Sjdp      sym = fixp->fx_addsy;
106189857Sobrien      while (symbol_equated_reloc_p (sym))
106260484Sobrien	sym = symbol_get_value_expression (sym)->X_add_symbol;
106333965Sjdp      fixp->fx_addsy = sym;
106433965Sjdp
106533965Sjdp      reloc = tc_gen_reloc (sec, fixp);
106633965Sjdp
106733965Sjdp      for (j = 0; reloc[j]; j++)
106833965Sjdp	{
106977298Sobrien	  relocs[i++] = reloc[j];
107077298Sobrien	  assert (i <= n);
107133965Sjdp	}
107233965Sjdp      data = fixp->fx_frag->fr_literal + fixp->fx_where;
107333965Sjdp      if (fixp->fx_where + fixp->fx_size
107433965Sjdp	  > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
107533965Sjdp	as_bad_where (fixp->fx_file, fixp->fx_line,
107660484Sobrien		      _("internal error: fixup not contained within frag"));
107733965Sjdp      for (j = 0; reloc[j]; j++)
107877298Sobrien	{
107933965Sjdp	  s = bfd_install_relocation (stdoutput, reloc[j],
108033965Sjdp				      fixp->fx_frag->fr_literal,
108133965Sjdp				      fixp->fx_frag->fr_address,
108233965Sjdp				      sec, &err);
108377298Sobrien	  switch (s)
108433965Sjdp	    {
108533965Sjdp	    case bfd_reloc_ok:
108633965Sjdp	      break;
108733965Sjdp	    case bfd_reloc_overflow:
108833965Sjdp	      as_bad_where (fixp->fx_file, fixp->fx_line,
108960484Sobrien			    _("relocation overflow"));
109033965Sjdp	      break;
109133965Sjdp	    default:
109260484Sobrien	      as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
109333965Sjdp			fixp->fx_file, fixp->fx_line);
109433965Sjdp	    }
109577298Sobrien	}
109633965Sjdp    }
109733965Sjdp  n = i;
109833965Sjdp#endif
109933965Sjdp
110033965Sjdp#ifdef DEBUG4
110133965Sjdp  {
110233965Sjdp    int i, j, nsyms;
110333965Sjdp    asymbol **sympp;
110433965Sjdp    sympp = bfd_get_outsymbols (stdoutput);
110533965Sjdp    nsyms = bfd_get_symcount (stdoutput);
110633965Sjdp    for (i = 0; i < n; i++)
110733965Sjdp      if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
110833965Sjdp	{
110933965Sjdp	  for (j = 0; j < nsyms; j++)
111033965Sjdp	    if (sympp[j] == *relocs[i]->sym_ptr_ptr)
111133965Sjdp	      break;
111233965Sjdp	  if (j == nsyms)
111333965Sjdp	    abort ();
111433965Sjdp	}
111533965Sjdp  }
111633965Sjdp#endif
111733965Sjdp
111833965Sjdp  if (n)
111933965Sjdp    bfd_set_reloc (stdoutput, sec, relocs, n);
112033965Sjdp  else
112133965Sjdp    bfd_set_section_flags (abfd, sec,
112233965Sjdp			   (bfd_get_section_flags (abfd, sec)
112333965Sjdp			    & (flagword) ~SEC_RELOC));
112433965Sjdp
112560484Sobrien#ifdef SET_SECTION_RELOCS
112660484Sobrien  SET_SECTION_RELOCS (sec, relocs, n);
112760484Sobrien#endif
112860484Sobrien
112933965Sjdp#ifdef DEBUG3
113033965Sjdp  {
113133965Sjdp    int i;
113233965Sjdp    arelent *r;
113333965Sjdp    asymbol *s;
113433965Sjdp    fprintf (stderr, "relocs for sec %s\n", sec->name);
113533965Sjdp    for (i = 0; i < n; i++)
113633965Sjdp      {
113733965Sjdp	r = relocs[i];
113833965Sjdp	s = *r->sym_ptr_ptr;
113933965Sjdp	fprintf (stderr, "  reloc %2d @%08x off %4x : sym %-10s addend %x\n",
114033965Sjdp		 i, r, r->address, s->name, r->addend);
114133965Sjdp      }
114233965Sjdp  }
114333965Sjdp#endif
114433965Sjdp}
114533965Sjdp
114633965Sjdpstatic void
114733965Sjdpwrite_contents (abfd, sec, xxx)
114860484Sobrien     bfd *abfd ATTRIBUTE_UNUSED;
114933965Sjdp     asection *sec;
115060484Sobrien     PTR xxx ATTRIBUTE_UNUSED;
115133965Sjdp{
115233965Sjdp  segment_info_type *seginfo = seg_info (sec);
115333965Sjdp  unsigned long offset = 0;
115433965Sjdp  fragS *f;
115533965Sjdp
115633965Sjdp  /* Write out the frags.  */
115733965Sjdp  if (seginfo == NULL
115877298Sobrien      || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
115933965Sjdp    return;
116033965Sjdp
116133965Sjdp  for (f = seginfo->frchainP->frch_root;
116233965Sjdp       f;
116333965Sjdp       f = f->fr_next)
116433965Sjdp    {
116533965Sjdp      int x;
116633965Sjdp      unsigned long fill_size;
116733965Sjdp      char *fill_literal;
116833965Sjdp      long count;
116933965Sjdp
117033965Sjdp      assert (f->fr_type == rs_fill);
117133965Sjdp      if (f->fr_fix)
117233965Sjdp	{
117333965Sjdp	  x = bfd_set_section_contents (stdoutput, sec,
117433965Sjdp					f->fr_literal, (file_ptr) offset,
117533965Sjdp					(bfd_size_type) f->fr_fix);
117633965Sjdp	  if (x == false)
117733965Sjdp	    {
117833965Sjdp	      bfd_perror (stdoutput->filename);
117960484Sobrien	      as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
118033965Sjdp	      exit (EXIT_FAILURE);
118133965Sjdp	    }
118233965Sjdp	  offset += f->fr_fix;
118333965Sjdp	}
118433965Sjdp      fill_literal = f->fr_literal + f->fr_fix;
118533965Sjdp      fill_size = f->fr_var;
118633965Sjdp      count = f->fr_offset;
118733965Sjdp      assert (count >= 0);
118833965Sjdp      if (fill_size && count)
118933965Sjdp	{
119033965Sjdp	  char buf[256];
119177298Sobrien	  if (fill_size > sizeof (buf))
119233965Sjdp	    {
119377298Sobrien	      /* Do it the old way. Can this ever happen?  */
119433965Sjdp	      while (count--)
119533965Sjdp		{
119633965Sjdp		  x = bfd_set_section_contents (stdoutput, sec,
119733965Sjdp						fill_literal,
119833965Sjdp						(file_ptr) offset,
119933965Sjdp						(bfd_size_type) fill_size);
120033965Sjdp		  if (x == false)
120133965Sjdp		    {
120233965Sjdp		      bfd_perror (stdoutput->filename);
120377298Sobrien		      as_perror (_("FATAL: Can't write %s"),
120477298Sobrien				 stdoutput->filename);
120533965Sjdp		      exit (EXIT_FAILURE);
120633965Sjdp		    }
120733965Sjdp		  offset += fill_size;
120833965Sjdp		}
120933965Sjdp	    }
121033965Sjdp	  else
121133965Sjdp	    {
121233965Sjdp	      /* Build a buffer full of fill objects and output it as
121333965Sjdp		 often as necessary. This saves on the overhead of
121433965Sjdp		 potentially lots of bfd_set_section_contents calls.  */
121533965Sjdp	      int n_per_buf, i;
121633965Sjdp	      if (fill_size == 1)
121733965Sjdp		{
121833965Sjdp		  n_per_buf = sizeof (buf);
121933965Sjdp		  memset (buf, *fill_literal, n_per_buf);
122033965Sjdp		}
122133965Sjdp	      else
122233965Sjdp		{
122333965Sjdp		  char *bufp;
122477298Sobrien		  n_per_buf = sizeof (buf) / fill_size;
122533965Sjdp		  for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
122677298Sobrien		    memcpy (bufp, fill_literal, fill_size);
122733965Sjdp		}
122833965Sjdp	      for (; count > 0; count -= n_per_buf)
122933965Sjdp		{
123033965Sjdp		  n_per_buf = n_per_buf > count ? count : n_per_buf;
123177298Sobrien		  x = bfd_set_section_contents
123277298Sobrien		    (stdoutput, sec, buf, (file_ptr) offset,
123377298Sobrien		     (bfd_size_type) n_per_buf * fill_size);
123433965Sjdp		  if (x != true)
123589857Sobrien		    as_fatal (_("cannot write to output file"));
123633965Sjdp		  offset += n_per_buf * fill_size;
123733965Sjdp		}
123833965Sjdp	    }
123933965Sjdp	}
124033965Sjdp    }
124133965Sjdp}
124233965Sjdp#endif
124333965Sjdp
124433965Sjdp#if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
124533965Sjdpstatic void
124633965Sjdpmerge_data_into_text ()
124733965Sjdp{
124833965Sjdp#if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
124933965Sjdp  seg_info (text_section)->frchainP->frch_last->fr_next =
125033965Sjdp    seg_info (data_section)->frchainP->frch_root;
125133965Sjdp  seg_info (text_section)->frchainP->frch_last =
125233965Sjdp    seg_info (data_section)->frchainP->frch_last;
125333965Sjdp  seg_info (data_section)->frchainP = 0;
125433965Sjdp#else
125533965Sjdp  fixS *tmp;
125633965Sjdp
125733965Sjdp  text_last_frag->fr_next = data_frag_root;
125833965Sjdp  text_last_frag = data_last_frag;
125933965Sjdp  data_last_frag = NULL;
126033965Sjdp  data_frag_root = NULL;
126133965Sjdp  if (text_fix_root)
126233965Sjdp    {
126333965Sjdp      for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
126433965Sjdp      tmp->fx_next = data_fix_root;
126533965Sjdp      text_fix_tail = data_fix_tail;
126633965Sjdp    }
126733965Sjdp  else
126833965Sjdp    text_fix_root = data_fix_root;
126933965Sjdp  data_fix_root = NULL;
127033965Sjdp#endif
127133965Sjdp}
127277298Sobrien#endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT)  */
127333965Sjdp
127433965Sjdp#if !defined (BFD_ASSEMBLER) && !defined (BFD)
127533965Sjdpstatic void
127633965Sjdprelax_and_size_all_segments ()
127733965Sjdp{
127833965Sjdp  fragS *fragP;
127933965Sjdp
128033965Sjdp  relax_segment (text_frag_root, SEG_TEXT);
128133965Sjdp  relax_segment (data_frag_root, SEG_DATA);
128233965Sjdp  relax_segment (bss_frag_root, SEG_BSS);
128333965Sjdp
128477298Sobrien  /* Now the addresses of frags are correct within the segment.  */
128533965Sjdp  know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
128633965Sjdp  H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
128733965Sjdp  text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
128833965Sjdp
128977298Sobrien  /* Join the 2 segments into 1 huge segment.
129077298Sobrien     To do this, re-compute every rn_address in the SEG_DATA frags.
129177298Sobrien     Then join the data frags after the text frags.
129277298Sobrien
129377298Sobrien     Determine a_data [length of data segment].  */
129433965Sjdp  if (data_frag_root)
129533965Sjdp    {
129633965Sjdp      register relax_addressT slide;
129733965Sjdp
129877298Sobrien      know ((text_last_frag->fr_type == rs_fill)
129977298Sobrien	    && (text_last_frag->fr_offset == 0));
130033965Sjdp
130133965Sjdp      H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
130233965Sjdp      data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
130377298Sobrien      slide = H_GET_TEXT_SIZE (&headers);	/* & in file of the data segment.  */
130433965Sjdp#ifdef OBJ_BOUT
130533965Sjdp#define RoundUp(N,S) (((N)+(S)-1)&-(S))
130633965Sjdp      /* For b.out: If the data section has a strict alignment
130733965Sjdp	 requirement, its load address in the .o file will be
130833965Sjdp	 rounded up from the size of the text section.  These
130933965Sjdp	 two values are *not* the same!  Similarly for the bss
131033965Sjdp	 section....  */
131133965Sjdp      slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
131233965Sjdp#endif
131333965Sjdp
131433965Sjdp      for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
131577298Sobrien	fragP->fr_address += slide;
131633965Sjdp
131733965Sjdp      know (text_last_frag != 0);
131833965Sjdp      text_last_frag->fr_next = data_frag_root;
131933965Sjdp    }
132033965Sjdp  else
132133965Sjdp    {
132233965Sjdp      H_SET_DATA_SIZE (&headers, 0);
132333965Sjdp    }
132433965Sjdp
132533965Sjdp#ifdef OBJ_BOUT
132633965Sjdp  /* See above comments on b.out data section address.  */
132733965Sjdp  {
132833965Sjdp    long bss_vma;
132933965Sjdp    if (data_last_frag == 0)
133033965Sjdp      bss_vma = H_GET_TEXT_SIZE (&headers);
133133965Sjdp    else
133233965Sjdp      bss_vma = data_last_frag->fr_address;
133333965Sjdp    bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
133433965Sjdp    bss_address_frag.fr_address = bss_vma;
133533965Sjdp  }
133677298Sobrien#else /* ! OBJ_BOUT  */
133733965Sjdp  bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
133833965Sjdp				 H_GET_DATA_SIZE (&headers));
133933965Sjdp
134077298Sobrien#endif /* ! OBJ_BOUT  */
134133965Sjdp
134277298Sobrien  /* Slide all the frags.  */
134333965Sjdp  if (bss_frag_root)
134433965Sjdp    {
134533965Sjdp      relax_addressT slide = bss_address_frag.fr_address;
134633965Sjdp
134733965Sjdp      for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
134877298Sobrien	fragP->fr_address += slide;
134933965Sjdp    }
135033965Sjdp
135133965Sjdp  if (bss_last_frag)
135233965Sjdp    H_SET_BSS_SIZE (&headers,
135333965Sjdp		    bss_last_frag->fr_address - bss_frag_root->fr_address);
135433965Sjdp  else
135533965Sjdp    H_SET_BSS_SIZE (&headers, 0);
135633965Sjdp}
135777298Sobrien#endif /* ! BFD_ASSEMBLER && ! BFD  */
135833965Sjdp
135933965Sjdp#if defined (BFD_ASSEMBLER) || !defined (BFD)
136033965Sjdp
136133965Sjdp#ifdef BFD_ASSEMBLER
136233965Sjdpstatic void
136333965Sjdpset_symtab ()
136433965Sjdp{
136533965Sjdp  int nsyms;
136633965Sjdp  asymbol **asympp;
136733965Sjdp  symbolS *symp;
136833965Sjdp  boolean result;
136989857Sobrien  extern PTR bfd_alloc PARAMS ((bfd *, bfd_size_type));
137033965Sjdp
137133965Sjdp  /* Count symbols.  We can't rely on a count made by the loop in
137233965Sjdp     write_object_file, because *_frob_file may add a new symbol or
137333965Sjdp     two.  */
137433965Sjdp  nsyms = 0;
137533965Sjdp  for (symp = symbol_rootP; symp; symp = symbol_next (symp))
137633965Sjdp    nsyms++;
137733965Sjdp
137833965Sjdp  if (nsyms)
137933965Sjdp    {
138033965Sjdp      int i;
138189857Sobrien      bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
138233965Sjdp
138389857Sobrien      asympp = (asymbol **) bfd_alloc (stdoutput, amt);
138433965Sjdp      symp = symbol_rootP;
138533965Sjdp      for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
138633965Sjdp	{
138760484Sobrien	  asympp[i] = symbol_get_bfdsym (symp);
138860484Sobrien	  symbol_mark_written (symp);
138933965Sjdp	}
139033965Sjdp    }
139133965Sjdp  else
139233965Sjdp    asympp = 0;
139333965Sjdp  result = bfd_set_symtab (stdoutput, asympp, nsyms);
139433965Sjdp  assert (result == true);
139533965Sjdp  symbol_table_frozen = 1;
139633965Sjdp}
139733965Sjdp#endif
139833965Sjdp
139977298Sobrien#if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
140077298Sobrienstatic void
140177298Sobrienset_segment_vma (abfd, sec, xxx)
140277298Sobrien     bfd *abfd;
140377298Sobrien     asection *sec;
140477298Sobrien     PTR xxx ATTRIBUTE_UNUSED;
140577298Sobrien{
140677298Sobrien  static bfd_vma addr = 0;
140777298Sobrien
140877298Sobrien  bfd_set_section_vma (abfd, sec, addr);
140977298Sobrien  addr += bfd_section_size (abfd, sec);
141077298Sobrien}
141177298Sobrien#endif /* BFD_ASSEMBLER && OBJ_COFF && !TE_PE  */
141277298Sobrien
141338889Sjdp/* Finish the subsegments.  After every sub-segment, we fake an
141438889Sjdp   ".align ...".  This conforms to BSD4.2 brane-damage.  We then fake
141538889Sjdp   ".fill 0" because that is the kind of frag that requires least
141638889Sjdp   thought.  ".align" frags like to have a following frag since that
141738889Sjdp   makes calculating their intended length trivial.  */
141838889Sjdp
141938889Sjdp#ifndef SUB_SEGMENT_ALIGN
142038889Sjdp#ifdef BFD_ASSEMBLER
142138889Sjdp#define SUB_SEGMENT_ALIGN(SEG) (0)
142238889Sjdp#else
142338889Sjdp#define SUB_SEGMENT_ALIGN(SEG) (2)
142438889Sjdp#endif
142538889Sjdp#endif
142638889Sjdp
142733965Sjdpvoid
142838889Sjdpsubsegs_finish ()
142938889Sjdp{
143038889Sjdp  struct frchain *frchainP;
143138889Sjdp
143238889Sjdp  for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
143338889Sjdp    {
143477298Sobrien      int alignment;
143577298Sobrien
143638889Sjdp      subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
143738889Sjdp
143860484Sobrien      /* This now gets called even if we had errors.  In that case,
143960484Sobrien         any alignment is meaningless, and, moreover, will look weird
144060484Sobrien         if we are generating a listing.  */
144177298Sobrien      alignment = had_errors () ? 0 : SUB_SEGMENT_ALIGN (now_seg);
144260484Sobrien
144377298Sobrien      /* The last subsegment gets an aligment corresponding to the
144477298Sobrien	 alignment of the section.  This allows proper nop-filling
144577298Sobrien	 at the end of code-bearing sections.  */
144677298Sobrien      if (!frchainP->frch_next || frchainP->frch_next->frch_seg != now_seg)
144777298Sobrien	alignment = get_recorded_alignment (now_seg);
144877298Sobrien
144977298Sobrien      if (subseg_text_p (now_seg))
145077298Sobrien	frag_align_code (alignment, 0);
145177298Sobrien      else
145277298Sobrien	frag_align (alignment, 0, 0);
145377298Sobrien
145438889Sjdp      /* frag_align will have left a new frag.
145538889Sjdp	 Use this last frag for an empty ".fill".
145638889Sjdp
145738889Sjdp	 For this segment ...
145838889Sjdp	 Create a last frag. Do not leave a "being filled in frag".  */
145938889Sjdp      frag_wane (frag_now);
146038889Sjdp      frag_now->fr_fix = 0;
146138889Sjdp      know (frag_now->fr_next == NULL);
146238889Sjdp    }
146338889Sjdp}
146438889Sjdp
146538889Sjdp/* Write the object file.  */
146638889Sjdp
146738889Sjdpvoid
146833965Sjdpwrite_object_file ()
146933965Sjdp{
147033965Sjdp#if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
147177298Sobrien  fragS *fragP;			/* Track along all frags.  */
147233965Sjdp#endif
147333965Sjdp
147433965Sjdp  /* Do we really want to write it?  */
147533965Sjdp  {
147633965Sjdp    int n_warns, n_errs;
147733965Sjdp    n_warns = had_warnings ();
147833965Sjdp    n_errs = had_errors ();
147933965Sjdp    /* The -Z flag indicates that an object file should be generated,
148033965Sjdp       regardless of warnings and errors.  */
148133965Sjdp    if (flag_always_generate_output)
148233965Sjdp      {
148333965Sjdp	if (n_warns || n_errs)
148489857Sobrien	  as_warn (_("%d error%s, %d warning%s, generating bad object file"),
148533965Sjdp		   n_errs, n_errs == 1 ? "" : "s",
148633965Sjdp		   n_warns, n_warns == 1 ? "" : "s");
148733965Sjdp      }
148833965Sjdp    else
148933965Sjdp      {
149033965Sjdp	if (n_errs)
149189857Sobrien	  as_fatal (_("%d error%s, %d warning%s, no object file generated"),
149233965Sjdp		    n_errs, n_errs == 1 ? "" : "s",
149333965Sjdp		    n_warns, n_warns == 1 ? "" : "s");
149433965Sjdp      }
149533965Sjdp  }
149633965Sjdp
149733965Sjdp#ifdef	OBJ_VMS
149833965Sjdp  /* Under VMS we try to be compatible with VAX-11 "C".  Thus, we call
149933965Sjdp     a routine to check for the definition of the procedure "_main",
150077298Sobrien     and if so -- fix it up so that it can be program entry point.  */
150133965Sjdp  vms_check_for_main ();
150277298Sobrien#endif /* OBJ_VMS  */
150333965Sjdp
150433965Sjdp  /* From now on, we don't care about sub-segments.  Build one frag chain
150533965Sjdp     for each segment. Linked thru fr_next.  */
150633965Sjdp
150733965Sjdp#ifdef BFD_ASSEMBLER
150833965Sjdp  /* Remove the sections created by gas for its own purposes.  */
150933965Sjdp  {
151089857Sobrien    asection **seclist;
151133965Sjdp    int i;
151233965Sjdp
151333965Sjdp    seclist = &stdoutput->sections;
151489857Sobrien    while (*seclist)
151533965Sjdp      {
151689857Sobrien	if (*seclist == reg_section || *seclist == expr_section)
151733965Sjdp	  {
151889857Sobrien	    bfd_section_list_remove (stdoutput, seclist);
151933965Sjdp	    stdoutput->section_count--;
152033965Sjdp	  }
152189857Sobrien	else
152233965Sjdp	  seclist = &(*seclist)->next;
152333965Sjdp      }
152433965Sjdp    i = 0;
152533965Sjdp    bfd_map_over_sections (stdoutput, renumber_sections, &i);
152633965Sjdp  }
152733965Sjdp
152833965Sjdp  bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
152933965Sjdp#else
153033965Sjdp  remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
153133965Sjdp  remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
153233965Sjdp  remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
153333965Sjdp#endif
153433965Sjdp
153533965Sjdp  /* We have two segments. If user gave -R flag, then we must put the
153633965Sjdp     data frags into the text segment. Do this before relaxing so
153733965Sjdp     we know to take advantage of -R and make shorter addresses.  */
153833965Sjdp#if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
153933965Sjdp  if (flag_readonly_data_in_text)
154033965Sjdp    {
154133965Sjdp      merge_data_into_text ();
154233965Sjdp    }
154333965Sjdp#endif
154433965Sjdp
154533965Sjdp#ifdef BFD_ASSEMBLER
154689857Sobrien  while (1)
154789857Sobrien    {
154889857Sobrien      int changed;
154989857Sobrien
155089857Sobrien#ifndef WORKING_DOT_WORD
155189857Sobrien      /* We need to reset the markers in the broken word list and
155289857Sobrien	 associated frags between calls to relax_segment (via
155389857Sobrien	 relax_seg).  Since the broken word list is global, we do it
155489857Sobrien	 once per round, rather than locally in relax_segment for each
155589857Sobrien	 segment.  */
155689857Sobrien      struct broken_word *brokp;
155789857Sobrien
155889857Sobrien      for (brokp = broken_words;
155989857Sobrien	   brokp != (struct broken_word *) NULL;
156089857Sobrien	   brokp = brokp->next_broken_word)
156189857Sobrien	{
156289857Sobrien	  brokp->added = 0;
156389857Sobrien
156489857Sobrien	  if (brokp->dispfrag != (fragS *) NULL
156589857Sobrien	      && brokp->dispfrag->fr_type == rs_broken_word)
156689857Sobrien	    brokp->dispfrag->fr_subtype = 0;
156789857Sobrien	}
156889857Sobrien#endif
156989857Sobrien
157089857Sobrien      changed = 0;
157189857Sobrien      bfd_map_over_sections (stdoutput, relax_seg, &changed);
157289857Sobrien      if (!changed)
157389857Sobrien	break;
157489857Sobrien    }
157589857Sobrien
157689857Sobrien  /* Note - Most ports will use the default value of
157789857Sobrien     TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1.  This will force
157889857Sobrien     local symbols to be resolved, removing their frag information.
157989857Sobrien     Some ports however, will not have finished relaxing all of
158089857Sobrien     their frags and will still need the local symbol frag
158189857Sobrien     information.  These ports can set
158289857Sobrien     TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0.  */
158389857Sobrien  finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
158489857Sobrien
158577298Sobrien  bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
158633965Sjdp#else
158733965Sjdp  relax_and_size_all_segments ();
158877298Sobrien#endif /* BFD_ASSEMBLER  */
158933965Sjdp
159089857Sobrien  /* Relaxation has completed.  Freeze all syms.  */
159189857Sobrien  finalize_syms = 1;
159289857Sobrien
159377298Sobrien#if defined (BFD_ASSEMBLER) && defined (OBJ_COFF) && defined (TE_GO32)
159477298Sobrien  /* Now that the segments have their final sizes, run through the
159577298Sobrien     sections and set their vma and lma. !BFD gas sets them, and BFD gas
159677298Sobrien     should too. Currently, only DJGPP uses this code, but other
159777298Sobrien     COFF targets may need to execute this too.  */
159877298Sobrien  bfd_map_over_sections (stdoutput, set_segment_vma, (char *) 0);
159977298Sobrien#endif
160077298Sobrien
160133965Sjdp#ifndef BFD_ASSEMBLER
160277298Sobrien  /* Crawl the symbol chain.
160377298Sobrien
160477298Sobrien     For each symbol whose value depends on a frag, take the address of
160577298Sobrien     that frag and subsume it into the value of the symbol.
160677298Sobrien     After this, there is just one way to lookup a symbol value.
160777298Sobrien     Values are left in their final state for object file emission.
160877298Sobrien     We adjust the values of 'L' local symbols, even if we do
160977298Sobrien     not intend to emit them to the object file, because their values
161077298Sobrien     are needed for fix-ups.
161177298Sobrien
161277298Sobrien     Unless we saw a -L flag, remove all symbols that begin with 'L'
161377298Sobrien     from the symbol chain.  (They are still pointed to by the fixes.)
161477298Sobrien
161577298Sobrien     Count the remaining symbols.
161677298Sobrien     Assign a symbol number to each symbol.
161777298Sobrien     Count the number of string-table chars we will emit.
161877298Sobrien     Put this info into the headers as appropriate.  */
161933965Sjdp  know (zero_address_frag.fr_address == 0);
162033965Sjdp  string_byte_count = sizeof (string_byte_count);
162133965Sjdp
162233965Sjdp  obj_crawl_symbol_chain (&headers);
162333965Sjdp
162433965Sjdp  if (string_byte_count == sizeof (string_byte_count))
162533965Sjdp    string_byte_count = 0;
162633965Sjdp
162733965Sjdp  H_SET_STRING_SIZE (&headers, string_byte_count);
162833965Sjdp
162977298Sobrien  /* Addresses of frags now reflect addresses we use in the object file.
163077298Sobrien     Symbol values are correct.
163177298Sobrien     Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
163277298Sobrien     Also converting any machine-dependent frags using md_convert_frag();  */
163333965Sjdp  subseg_change (SEG_TEXT, 0);
163433965Sjdp
163533965Sjdp  for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
163633965Sjdp    {
163738889Sjdp      /* At this point we have linked all the frags into a single
163838889Sjdp         chain.  However, cvt_frag_to_fill may call md_convert_frag
163938889Sjdp         which may call fix_new.  We need to ensure that fix_new adds
164038889Sjdp         the fixup to the right section.  */
164138889Sjdp      if (fragP == data_frag_root)
164238889Sjdp	subseg_change (SEG_DATA, 0);
164338889Sjdp
164433965Sjdp      cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
164533965Sjdp
164633965Sjdp      /* Some assert macros don't work with # directives mixed in.  */
164733965Sjdp#ifndef NDEBUG
164833965Sjdp      if (!(fragP->fr_next == NULL
164933965Sjdp#ifdef OBJ_BOUT
165033965Sjdp	    || fragP->fr_next == data_frag_root
165133965Sjdp#endif
165233965Sjdp	    || ((fragP->fr_next->fr_address - fragP->fr_address)
165333965Sjdp		== (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
165433965Sjdp	abort ();
165533965Sjdp#endif
165633965Sjdp    }
165777298Sobrien#endif /* ! BFD_ASSEMBLER  */
165833965Sjdp
165933965Sjdp#ifndef WORKING_DOT_WORD
166033965Sjdp  {
166133965Sjdp    struct broken_word *lie;
166233965Sjdp    struct broken_word **prevP;
166333965Sjdp
166433965Sjdp    prevP = &broken_words;
166533965Sjdp    for (lie = broken_words; lie; lie = lie->next_broken_word)
166633965Sjdp      if (!lie->added)
166733965Sjdp	{
166833965Sjdp	  expressionS exp;
166933965Sjdp
167060484Sobrien	  subseg_change (lie->seg, lie->subseg);
167133965Sjdp	  exp.X_op = O_subtract;
167233965Sjdp	  exp.X_add_symbol = lie->add;
167333965Sjdp	  exp.X_op_symbol = lie->sub;
167433965Sjdp	  exp.X_add_number = lie->addnum;
167533965Sjdp#ifdef BFD_ASSEMBLER
167633965Sjdp#ifdef TC_CONS_FIX_NEW
167733965Sjdp	  TC_CONS_FIX_NEW (lie->frag,
167877298Sobrien			   lie->word_goes_here - lie->frag->fr_literal,
167977298Sobrien			   2, &exp);
168033965Sjdp#else
168133965Sjdp	  fix_new_exp (lie->frag,
168233965Sjdp		       lie->word_goes_here - lie->frag->fr_literal,
168333965Sjdp		       2, &exp, 0, BFD_RELOC_16);
168433965Sjdp#endif
168533965Sjdp#else
168633965Sjdp#if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
168733965Sjdp	  fix_new_exp (lie->frag,
168833965Sjdp		       lie->word_goes_here - lie->frag->fr_literal,
168933965Sjdp		       2, &exp, 0, NO_RELOC);
169033965Sjdp#else
169133965Sjdp#ifdef TC_NS32K
169233965Sjdp	  fix_new_ns32k_exp (lie->frag,
169333965Sjdp			     lie->word_goes_here - lie->frag->fr_literal,
169433965Sjdp			     2, &exp, 0, 0, 2, 0, 0);
169533965Sjdp#else
169633965Sjdp	  fix_new_exp (lie->frag,
169733965Sjdp		       lie->word_goes_here - lie->frag->fr_literal,
169833965Sjdp		       2, &exp, 0, 0);
169977298Sobrien#endif /* TC_NS32K  */
170077298Sobrien#endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE  */
170177298Sobrien#endif /* BFD_ASSEMBLER  */
170233965Sjdp	  *prevP = lie->next_broken_word;
170333965Sjdp	}
170433965Sjdp      else
170533965Sjdp	prevP = &(lie->next_broken_word);
170633965Sjdp
170733965Sjdp    for (lie = broken_words; lie;)
170833965Sjdp      {
170933965Sjdp	struct broken_word *untruth;
171033965Sjdp	char *table_ptr;
171133965Sjdp	addressT table_addr;
171233965Sjdp	addressT from_addr, to_addr;
171333965Sjdp	int n, m;
171433965Sjdp
171560484Sobrien	subseg_change (lie->seg, lie->subseg);
171633965Sjdp	fragP = lie->dispfrag;
171733965Sjdp
171833965Sjdp	/* Find out how many broken_words go here.  */
171933965Sjdp	n = 0;
172077298Sobrien	for (untruth = lie;
172177298Sobrien	     untruth && untruth->dispfrag == fragP;
172277298Sobrien	     untruth = untruth->next_broken_word)
172333965Sjdp	  if (untruth->added == 1)
172433965Sjdp	    n++;
172533965Sjdp
172633965Sjdp	table_ptr = lie->dispfrag->fr_opcode;
172777298Sobrien	table_addr = (lie->dispfrag->fr_address
172877298Sobrien		      + (table_ptr - lie->dispfrag->fr_literal));
172933965Sjdp	/* Create the jump around the long jumps.  This is a short
173033965Sjdp	   jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
173133965Sjdp	from_addr = table_addr;
173233965Sjdp	to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
173377298Sobrien	md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
173477298Sobrien			      lie->add);
173533965Sjdp	table_ptr += md_short_jump_size;
173633965Sjdp	table_addr += md_short_jump_size;
173733965Sjdp
173877298Sobrien	for (m = 0;
173977298Sobrien	     lie && lie->dispfrag == fragP;
174077298Sobrien	     m++, lie = lie->next_broken_word)
174133965Sjdp	  {
174233965Sjdp	    if (lie->added == 2)
174333965Sjdp	      continue;
174477298Sobrien	    /* Patch the jump table.  */
174577298Sobrien	    /* This is the offset from ??? to table_ptr+0.  */
174633965Sjdp	    to_addr = table_addr - S_GET_VALUE (lie->sub);
174777298Sobrien#ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
174877298Sobrien	    TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
174977298Sobrien#endif
175033965Sjdp	    md_number_to_chars (lie->word_goes_here, to_addr, 2);
175177298Sobrien	    for (untruth = lie->next_broken_word;
175277298Sobrien		 untruth && untruth->dispfrag == fragP;
175377298Sobrien		 untruth = untruth->next_broken_word)
175433965Sjdp	      {
175533965Sjdp		if (untruth->use_jump == lie)
175633965Sjdp		  md_number_to_chars (untruth->word_goes_here, to_addr, 2);
175733965Sjdp	      }
175833965Sjdp
175977298Sobrien	    /* Install the long jump.  */
176077298Sobrien	    /* This is a long jump from table_ptr+0 to the final target.  */
176133965Sjdp	    from_addr = table_addr;
176233965Sjdp	    to_addr = S_GET_VALUE (lie->add) + lie->addnum;
176377298Sobrien	    md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
176477298Sobrien				 lie->add);
176533965Sjdp	    table_ptr += md_long_jump_size;
176633965Sjdp	    table_addr += md_long_jump_size;
176733965Sjdp	  }
176833965Sjdp      }
176933965Sjdp  }
177077298Sobrien#endif /* not WORKING_DOT_WORD  */
177133965Sjdp
177233965Sjdp#ifndef BFD_ASSEMBLER
177333965Sjdp#ifndef	OBJ_VMS
177477298Sobrien  {				/* not vms  */
177533965Sjdp    char *the_object_file;
177633965Sjdp    long object_file_size;
177777298Sobrien    /* Scan every FixS performing fixups. We had to wait until now to
177877298Sobrien       do this because md_convert_frag() may have made some fixSs.  */
177933965Sjdp    int trsize, drsize;
178033965Sjdp
178133965Sjdp    subseg_change (SEG_TEXT, 0);
178233965Sjdp    trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
178333965Sjdp    subseg_change (SEG_DATA, 0);
178433965Sjdp    drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
178533965Sjdp    H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
178633965Sjdp
178777298Sobrien    /* FIXME: Move this stuff into the pre-write-hook.  */
178833965Sjdp    H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
178933965Sjdp    H_SET_ENTRY_POINT (&headers, 0);
179033965Sjdp
179177298Sobrien    obj_pre_write_hook (&headers);	/* Extra coff stuff.  */
179233965Sjdp
179333965Sjdp    object_file_size = H_GET_FILE_SIZE (&headers);
179433965Sjdp    next_object_file_charP = the_object_file = xmalloc (object_file_size);
179533965Sjdp
179633965Sjdp    output_file_create (out_file_name);
179733965Sjdp
179833965Sjdp    obj_header_append (&next_object_file_charP, &headers);
179933965Sjdp
180077298Sobrien    know ((next_object_file_charP - the_object_file)
180177298Sobrien	  == H_GET_HEADER_SIZE (&headers));
180233965Sjdp
180377298Sobrien    /* Emit code.  */
180433965Sjdp    for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
180533965Sjdp      {
180633965Sjdp	register long count;
180733965Sjdp	register char *fill_literal;
180833965Sjdp	register long fill_size;
180933965Sjdp
181033965Sjdp	PROGRESS (1);
181133965Sjdp	know (fragP->fr_type == rs_fill);
181277298Sobrien	append (&next_object_file_charP, fragP->fr_literal,
181377298Sobrien		(unsigned long) fragP->fr_fix);
181433965Sjdp	fill_literal = fragP->fr_literal + fragP->fr_fix;
181533965Sjdp	fill_size = fragP->fr_var;
181633965Sjdp	know (fragP->fr_offset >= 0);
181733965Sjdp
181833965Sjdp	for (count = fragP->fr_offset; count; count--)
181977298Sobrien	  append (&next_object_file_charP, fill_literal,
182077298Sobrien		  (unsigned long) fill_size);
182177298Sobrien      }
182233965Sjdp
182377298Sobrien    know ((next_object_file_charP - the_object_file)
182477298Sobrien	  == (H_GET_HEADER_SIZE (&headers)
182577298Sobrien	      + H_GET_TEXT_SIZE (&headers)
182677298Sobrien	      + H_GET_DATA_SIZE (&headers)));
182733965Sjdp
182877298Sobrien    /* Emit relocations.  */
182977298Sobrien    obj_emit_relocations (&next_object_file_charP, text_fix_root,
183077298Sobrien			  (relax_addressT) 0);
183177298Sobrien    know ((next_object_file_charP - the_object_file)
183277298Sobrien	  == (H_GET_HEADER_SIZE (&headers)
183377298Sobrien	      + H_GET_TEXT_SIZE (&headers)
183477298Sobrien	      + H_GET_DATA_SIZE (&headers)
183577298Sobrien	      + H_GET_TEXT_RELOCATION_SIZE (&headers)));
183633965Sjdp#ifdef TC_I960
183733965Sjdp    /* Make addresses in data relocation directives relative to beginning of
183877298Sobrien       first data fragment, not end of last text fragment:  alignment of the
183977298Sobrien       start of the data segment may place a gap between the segments.  */
184077298Sobrien    obj_emit_relocations (&next_object_file_charP, data_fix_root,
184177298Sobrien			  data0_frchainP->frch_root->fr_address);
184277298Sobrien#else /* TC_I960  */
184377298Sobrien    obj_emit_relocations (&next_object_file_charP, data_fix_root,
184477298Sobrien			  text_last_frag->fr_address);
184577298Sobrien#endif /* TC_I960  */
184633965Sjdp
184777298Sobrien    know ((next_object_file_charP - the_object_file)
184877298Sobrien	  == (H_GET_HEADER_SIZE (&headers)
184977298Sobrien	      + H_GET_TEXT_SIZE (&headers)
185077298Sobrien	      + H_GET_DATA_SIZE (&headers)
185177298Sobrien	      + H_GET_TEXT_RELOCATION_SIZE (&headers)
185277298Sobrien	      + H_GET_DATA_RELOCATION_SIZE (&headers)));
185333965Sjdp
185477298Sobrien    /* Emit line number entries.  */
185533965Sjdp    OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
185677298Sobrien    know ((next_object_file_charP - the_object_file)
185777298Sobrien	  == (H_GET_HEADER_SIZE (&headers)
185877298Sobrien	      + H_GET_TEXT_SIZE (&headers)
185977298Sobrien	      + H_GET_DATA_SIZE (&headers)
186077298Sobrien	      + H_GET_TEXT_RELOCATION_SIZE (&headers)
186177298Sobrien	      + H_GET_DATA_RELOCATION_SIZE (&headers)
186277298Sobrien	      + H_GET_LINENO_SIZE (&headers)));
186333965Sjdp
186477298Sobrien    /* Emit symbols.  */
186533965Sjdp    obj_emit_symbols (&next_object_file_charP, symbol_rootP);
186677298Sobrien    know ((next_object_file_charP - the_object_file)
186777298Sobrien	  == (H_GET_HEADER_SIZE (&headers)
186877298Sobrien	      + H_GET_TEXT_SIZE (&headers)
186977298Sobrien	      + H_GET_DATA_SIZE (&headers)
187077298Sobrien	      + H_GET_TEXT_RELOCATION_SIZE (&headers)
187177298Sobrien	      + H_GET_DATA_RELOCATION_SIZE (&headers)
187277298Sobrien	      + H_GET_LINENO_SIZE (&headers)
187377298Sobrien	      + H_GET_SYMBOL_TABLE_SIZE (&headers)));
187433965Sjdp
187577298Sobrien    /* Emit strings.  */
187633965Sjdp    if (string_byte_count > 0)
187777298Sobrien      obj_emit_strings (&next_object_file_charP);
187833965Sjdp
187933965Sjdp#ifdef BFD_HEADERS
188089857Sobrien    bfd_seek (stdoutput, (file_ptr) 0, 0);
188189857Sobrien    bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
188233965Sjdp#else
188333965Sjdp
188477298Sobrien    /* Write the data to the file.  */
188533965Sjdp    output_file_append (the_object_file, object_file_size, out_file_name);
188633965Sjdp    free (the_object_file);
188733965Sjdp#endif
188877298Sobrien  }
188977298Sobrien#else /* OBJ_VMS  */
189077298Sobrien  /* Now do the VMS-dependent part of writing the object file.  */
189133965Sjdp  vms_write_object_file (H_GET_TEXT_SIZE (&headers),
189233965Sjdp			 H_GET_DATA_SIZE (&headers),
189333965Sjdp			 H_GET_BSS_SIZE (&headers),
189433965Sjdp			 text_frag_root, data_frag_root);
189577298Sobrien#endif /* OBJ_VMS  */
189677298Sobrien#else /* BFD_ASSEMBLER  */
189733965Sjdp
189833965Sjdp  /* Resolve symbol values.  This needs to be done before processing
189933965Sjdp     the relocations.  */
190033965Sjdp  if (symbol_rootP)
190133965Sjdp    {
190233965Sjdp      symbolS *symp;
190333965Sjdp
190433965Sjdp      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
190589857Sobrien	resolve_symbol_value (symp);
190633965Sjdp    }
190760484Sobrien  resolve_local_symbol_values ();
190833965Sjdp
190933965Sjdp  PROGRESS (1);
191033965Sjdp
191133965Sjdp#ifdef tc_frob_file_before_adjust
191233965Sjdp  tc_frob_file_before_adjust ();
191333965Sjdp#endif
191433965Sjdp#ifdef obj_frob_file_before_adjust
191533965Sjdp  obj_frob_file_before_adjust ();
191633965Sjdp#endif
191733965Sjdp
191877298Sobrien  bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
191933965Sjdp
192033965Sjdp  /* Set up symbol table, and write it out.  */
192133965Sjdp  if (symbol_rootP)
192233965Sjdp    {
192333965Sjdp      symbolS *symp;
192433965Sjdp
192533965Sjdp      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
192633965Sjdp	{
192733965Sjdp	  int punt = 0;
192833965Sjdp	  const char *name;
192933965Sjdp
193060484Sobrien	  if (symbol_mri_common_p (symp))
193133965Sjdp	    {
193233965Sjdp	      if (S_IS_EXTERNAL (symp))
193360484Sobrien		as_bad (_("%s: global symbols not supported in common sections"),
193433965Sjdp			S_GET_NAME (symp));
193533965Sjdp	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
193633965Sjdp	      continue;
193733965Sjdp	    }
193833965Sjdp
193933965Sjdp	  name = S_GET_NAME (symp);
194033965Sjdp	  if (name)
194133965Sjdp	    {
194277298Sobrien	      const char *name2 =
194377298Sobrien		decode_local_label_name ((char *) S_GET_NAME (symp));
194433965Sjdp	      /* They only differ if `name' is a fb or dollar local
194533965Sjdp		 label name.  */
194633965Sjdp	      if (name2 != name && ! S_IS_DEFINED (symp))
194789857Sobrien		as_bad (_("local label `%s' is not defined"), name2);
194833965Sjdp	    }
194933965Sjdp
195033965Sjdp	  /* Do it again, because adjust_reloc_syms might introduce
195133965Sjdp	     more symbols.  They'll probably only be section symbols,
195233965Sjdp	     but they'll still need to have the values computed.  */
195389857Sobrien	  resolve_symbol_value (symp);
195433965Sjdp
195533965Sjdp	  /* Skip symbols which were equated to undefined or common
195633965Sjdp             symbols.  */
195789857Sobrien	  if (symbol_equated_reloc_p (symp))
195833965Sjdp	    {
195933965Sjdp	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
196033965Sjdp	      continue;
196133965Sjdp	    }
196233965Sjdp
196333965Sjdp	  /* So far, common symbols have been treated like undefined symbols.
196433965Sjdp	     Put them in the common section now.  */
196533965Sjdp	  if (S_IS_DEFINED (symp) == 0
196633965Sjdp	      && S_GET_VALUE (symp) != 0)
196733965Sjdp	    S_SET_SEGMENT (symp, bfd_com_section_ptr);
196833965Sjdp#if 0
196933965Sjdp	  printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
197033965Sjdp		  S_GET_NAME (symp), symp,
197133965Sjdp		  S_GET_VALUE (symp),
197260484Sobrien		  symbol_get_bfdsym (symp)->flags,
197360484Sobrien		  segment_name (S_GET_SEGMENT (symp)));
197433965Sjdp#endif
197533965Sjdp
197633965Sjdp#ifdef obj_frob_symbol
197733965Sjdp	  obj_frob_symbol (symp, punt);
197833965Sjdp#endif
197933965Sjdp#ifdef tc_frob_symbol
198060484Sobrien	  if (! punt || symbol_used_in_reloc_p (symp))
198133965Sjdp	    tc_frob_symbol (symp, punt);
198233965Sjdp#endif
198333965Sjdp
198433965Sjdp	  /* If we don't want to keep this symbol, splice it out of
198533965Sjdp	     the chain now.  If EMIT_SECTION_SYMBOLS is 0, we never
198633965Sjdp	     want section symbols.  Otherwise, we skip local symbols
198733965Sjdp	     and symbols that the frob_symbol macros told us to punt,
198833965Sjdp	     but we keep such symbols if they are used in relocs.  */
198933965Sjdp	  if ((! EMIT_SECTION_SYMBOLS
199060484Sobrien	       && symbol_section_p (symp))
199133965Sjdp	      /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
199233965Sjdp		 opposites.  Sometimes the former checks flags and the
199333965Sjdp		 latter examines the name...  */
199433965Sjdp	      || (!S_IS_EXTERN (symp)
199533965Sjdp		  && (S_IS_LOCAL (symp) || punt)
199660484Sobrien		  && ! symbol_used_in_reloc_p (symp)))
199733965Sjdp	    {
199833965Sjdp	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
199977298Sobrien
200033965Sjdp	      /* After symbol_remove, symbol_next(symp) still returns
200133965Sjdp		 the one that came after it in the chain.  So we don't
200233965Sjdp		 need to do any extra cleanup work here.  */
200333965Sjdp	      continue;
200433965Sjdp	    }
200533965Sjdp
200633965Sjdp	  /* Make sure we really got a value for the symbol.  */
200760484Sobrien	  if (! symbol_resolved_p (symp))
200833965Sjdp	    {
200989857Sobrien	      as_bad (_("can't resolve value for symbol `%s'"),
201033965Sjdp		      S_GET_NAME (symp));
201160484Sobrien	      symbol_mark_resolved (symp);
201233965Sjdp	    }
201333965Sjdp
201433965Sjdp	  /* Set the value into the BFD symbol.  Up til now the value
201533965Sjdp	     has only been kept in the gas symbolS struct.  */
201660484Sobrien	  symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
201733965Sjdp	}
201833965Sjdp    }
201933965Sjdp
202033965Sjdp  PROGRESS (1);
202133965Sjdp
202233965Sjdp  /* Now do any format-specific adjustments to the symbol table, such
202333965Sjdp     as adding file symbols.  */
202433965Sjdp#ifdef tc_adjust_symtab
202533965Sjdp  tc_adjust_symtab ();
202633965Sjdp#endif
202733965Sjdp#ifdef obj_adjust_symtab
202833965Sjdp  obj_adjust_symtab ();
202933965Sjdp#endif
203033965Sjdp
203133965Sjdp  /* Now that all the sizes are known, and contents correct, we can
203233965Sjdp     start writing to the file.  */
203333965Sjdp  set_symtab ();
203433965Sjdp
203533965Sjdp  /* If *_frob_file changes the symbol value at this point, it is
203633965Sjdp     responsible for moving the changed value into symp->bsym->value
203733965Sjdp     as well.  Hopefully all symbol value changing can be done in
203833965Sjdp     *_frob_symbol.  */
203933965Sjdp#ifdef tc_frob_file
204033965Sjdp  tc_frob_file ();
204133965Sjdp#endif
204233965Sjdp#ifdef obj_frob_file
204333965Sjdp  obj_frob_file ();
204433965Sjdp#endif
204533965Sjdp
204633965Sjdp  bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
204733965Sjdp
204833965Sjdp#ifdef tc_frob_file_after_relocs
204933965Sjdp  tc_frob_file_after_relocs ();
205033965Sjdp#endif
205133965Sjdp#ifdef obj_frob_file_after_relocs
205233965Sjdp  obj_frob_file_after_relocs ();
205333965Sjdp#endif
205433965Sjdp
205533965Sjdp  bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
205677298Sobrien#endif /* BFD_ASSEMBLER  */
205733965Sjdp}
205877298Sobrien#endif /* ! BFD  */
205933965Sjdp
206033965Sjdp#ifdef TC_GENERIC_RELAX_TABLE
206133965Sjdp
206233965Sjdp/* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE.  */
206333965Sjdp
206433965Sjdplong
206578828Sobrienrelax_frag (segment, fragP, stretch)
206678828Sobrien     segT segment;
206733965Sjdp     fragS *fragP;
206833965Sjdp     long stretch;
206933965Sjdp{
207033965Sjdp  const relax_typeS *this_type;
207133965Sjdp  const relax_typeS *start_type;
207233965Sjdp  relax_substateT next_state;
207333965Sjdp  relax_substateT this_state;
207478828Sobrien  long growth;
207578828Sobrien  offsetT aim;
207678828Sobrien  addressT target;
207778828Sobrien  addressT address;
207878828Sobrien  symbolS *symbolP;
207978828Sobrien  const relax_typeS *table;
208033965Sjdp
208178828Sobrien  target = fragP->fr_offset;
208278828Sobrien  address = fragP->fr_address;
208378828Sobrien  table = TC_GENERIC_RELAX_TABLE;
208433965Sjdp  this_state = fragP->fr_subtype;
208533965Sjdp  start_type = this_type = table + this_state;
208678828Sobrien  symbolP = fragP->fr_symbol;
208733965Sjdp
208833965Sjdp  if (symbolP)
208933965Sjdp    {
209078828Sobrien      fragS *sym_frag;
209178828Sobrien
209278828Sobrien      sym_frag = symbol_get_frag (symbolP);
209378828Sobrien
209433965Sjdp#ifndef DIFF_EXPR_OK
209533965Sjdp#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
209633965Sjdp      know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
209733965Sjdp	    || (S_GET_SEGMENT (symbolP) == SEG_DATA)
209833965Sjdp	    || (S_GET_SEGMENT (symbolP) == SEG_BSS)
209933965Sjdp	    || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
210033965Sjdp#endif
210178828Sobrien      know (sym_frag != NULL);
210233965Sjdp#endif
210333965Sjdp      know (!(S_GET_SEGMENT (symbolP) == absolute_section)
210478828Sobrien	    || sym_frag == &zero_address_frag);
210589857Sobrien      target += S_GET_VALUE (symbolP);
210633965Sjdp
210733965Sjdp      /* If frag has yet to be reached on this pass,
210833965Sjdp	 assume it will move by STRETCH just as we did.
210933965Sjdp	 If this is not so, it will be because some frag
211078828Sobrien	 between grows, and that will force another pass.  */
211133965Sjdp
211278828Sobrien      if (stretch != 0
211378828Sobrien	  && sym_frag->relax_marker != fragP->relax_marker
211478828Sobrien	  && S_GET_SEGMENT (symbolP) == segment)
211533965Sjdp	{
211633965Sjdp	  target += stretch;
211733965Sjdp	}
211833965Sjdp    }
211933965Sjdp
212033965Sjdp  aim = target - address - fragP->fr_fix;
212133965Sjdp#ifdef TC_PCREL_ADJUST
212277298Sobrien  /* Currently only the ns32k family needs this.  */
212377298Sobrien  aim += TC_PCREL_ADJUST (fragP);
212477298Sobrien/* #else */
212533965Sjdp  /* This machine doesn't want to use pcrel_adjust.
212633965Sjdp     In that case, pcrel_adjust should be zero.  */
212777298Sobrien#if 0
212877298Sobrien  assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
212933965Sjdp#endif
213077298Sobrien#endif
213177298Sobrien#ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE  */
213233965Sjdp  md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
213333965Sjdp#endif
213433965Sjdp
213533965Sjdp  if (aim < 0)
213633965Sjdp    {
213777298Sobrien      /* Look backwards.  */
213833965Sjdp      for (next_state = this_type->rlx_more; next_state;)
213933965Sjdp	if (aim >= this_type->rlx_backward)
214033965Sjdp	  next_state = 0;
214133965Sjdp	else
214233965Sjdp	  {
214377298Sobrien	    /* Grow to next state.  */
214433965Sjdp	    this_state = next_state;
214533965Sjdp	    this_type = table + this_state;
214633965Sjdp	    next_state = this_type->rlx_more;
214733965Sjdp	  }
214833965Sjdp    }
214933965Sjdp  else
215033965Sjdp    {
215177298Sobrien      /* Look forwards.  */
215233965Sjdp      for (next_state = this_type->rlx_more; next_state;)
215333965Sjdp	if (aim <= this_type->rlx_forward)
215433965Sjdp	  next_state = 0;
215533965Sjdp	else
215633965Sjdp	  {
215777298Sobrien	    /* Grow to next state.  */
215833965Sjdp	    this_state = next_state;
215933965Sjdp	    this_type = table + this_state;
216033965Sjdp	    next_state = this_type->rlx_more;
216133965Sjdp	  }
216233965Sjdp    }
216333965Sjdp
216433965Sjdp  growth = this_type->rlx_length - start_type->rlx_length;
216533965Sjdp  if (growth != 0)
216633965Sjdp    fragP->fr_subtype = this_state;
216733965Sjdp  return growth;
216833965Sjdp}
216933965Sjdp
217077298Sobrien#endif /* defined (TC_GENERIC_RELAX_TABLE)  */
217133965Sjdp
217233965Sjdp/* Relax_align. Advance location counter to next address that has 'alignment'
217333965Sjdp   lowest order bits all 0s, return size of adjustment made.  */
217433965Sjdpstatic relax_addressT
217533965Sjdprelax_align (address, alignment)
217677298Sobrien     register relax_addressT address;	/* Address now.  */
217777298Sobrien     register int alignment;	/* Alignment (binary).  */
217833965Sjdp{
217933965Sjdp  relax_addressT mask;
218033965Sjdp  relax_addressT new_address;
218133965Sjdp
218233965Sjdp  mask = ~((~0) << alignment);
218333965Sjdp  new_address = (address + mask) & (~mask);
218433965Sjdp#ifdef LINKER_RELAXING_SHRINKS_ONLY
218533965Sjdp  if (linkrelax)
218633965Sjdp    /* We must provide lots of padding, so the linker can discard it
218733965Sjdp       when needed.  The linker will not add extra space, ever.  */
218833965Sjdp    new_address += (1 << alignment);
218933965Sjdp#endif
219033965Sjdp  return (new_address - address);
219133965Sjdp}
219233965Sjdp
219377298Sobrien/* Now we have a segment, not a crowd of sub-segments, we can make
219477298Sobrien   fr_address values.
219577298Sobrien
219677298Sobrien   Relax the frags.
219777298Sobrien
219877298Sobrien   After this, all frags in this segment have addresses that are correct
219977298Sobrien   within the segment. Since segments live in different file addresses,
220077298Sobrien   these frag addresses may not be the same as final object-file
220177298Sobrien   addresses.  */
220277298Sobrien
220389857Sobrienint
220433965Sjdprelax_segment (segment_frag_root, segment)
220533965Sjdp     struct frag *segment_frag_root;
220633965Sjdp     segT segment;
220733965Sjdp{
220833965Sjdp  register struct frag *fragP;
220933965Sjdp  register relax_addressT address;
221089857Sobrien  int ret;
221189857Sobrien
221233965Sjdp#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
221333965Sjdp  know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
221433965Sjdp#endif
221577298Sobrien  /* In case md_estimate_size_before_relax() wants to make fixSs.  */
221633965Sjdp  subseg_change (segment, 0);
221733965Sjdp
221833965Sjdp  /* For each frag in segment: count and store  (a 1st guess of)
221933965Sjdp     fr_address.  */
222033965Sjdp  address = 0;
222133965Sjdp  for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
222233965Sjdp    {
222378828Sobrien      fragP->relax_marker = 0;
222433965Sjdp      fragP->fr_address = address;
222533965Sjdp      address += fragP->fr_fix;
222633965Sjdp
222733965Sjdp      switch (fragP->fr_type)
222833965Sjdp	{
222933965Sjdp	case rs_fill:
223033965Sjdp	  address += fragP->fr_offset * fragP->fr_var;
223133965Sjdp	  break;
223233965Sjdp
223333965Sjdp	case rs_align:
223433965Sjdp	case rs_align_code:
223577298Sobrien	case rs_align_test:
223633965Sjdp	  {
223733965Sjdp	    addressT offset = relax_align (address, (int) fragP->fr_offset);
223833965Sjdp
223933965Sjdp	    if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
224033965Sjdp	      offset = 0;
224133965Sjdp
224233965Sjdp	    if (offset % fragP->fr_var != 0)
224333965Sjdp	      {
224489857Sobrien		as_bad_where (fragP->fr_file, fragP->fr_line,
224589857Sobrien			      _("alignment padding (%lu bytes) not a multiple of %ld"),
224689857Sobrien			      (unsigned long) offset, (long) fragP->fr_var);
224733965Sjdp		offset -= (offset % fragP->fr_var);
224833965Sjdp	      }
224933965Sjdp
225033965Sjdp	    address += offset;
225133965Sjdp	  }
225233965Sjdp	  break;
225333965Sjdp
225433965Sjdp	case rs_org:
225533965Sjdp	case rs_space:
225633965Sjdp	  /* Assume .org is nugatory. It will grow with 1st relax.  */
225733965Sjdp	  break;
225833965Sjdp
225933965Sjdp	case rs_machine_dependent:
226089857Sobrien	  /* If fr_symbol is an expression, this call to
226189857Sobrien	     resolve_symbol_value sets up the correct segment, which will
226289857Sobrien	     likely be needed in md_estimate_size_before_relax.  */
226389857Sobrien	  if (fragP->fr_symbol)
226489857Sobrien	    resolve_symbol_value (fragP->fr_symbol);
226589857Sobrien
226633965Sjdp	  address += md_estimate_size_before_relax (fragP, segment);
226733965Sjdp	  break;
226833965Sjdp
226933965Sjdp#ifndef WORKING_DOT_WORD
227077298Sobrien	  /* Broken words don't concern us yet.  */
227133965Sjdp	case rs_broken_word:
227233965Sjdp	  break;
227333965Sjdp#endif
227433965Sjdp
227538889Sjdp	case rs_leb128:
227677298Sobrien	  /* Initial guess is always 1; doing otherwise can result in
227738889Sjdp	     stable solutions that are larger than the minimum.  */
227838889Sjdp	  address += fragP->fr_offset = 1;
227938889Sjdp	  break;
228038889Sjdp
228138889Sjdp	case rs_cfa:
228238889Sjdp	  address += eh_frame_estimate_size_before_relax (fragP);
228338889Sjdp	  break;
228438889Sjdp
228577298Sobrien	case rs_dwarf2dbg:
228677298Sobrien	  address += dwarf2dbg_estimate_size_before_relax (fragP);
228777298Sobrien	  break;
228877298Sobrien
228933965Sjdp	default:
229033965Sjdp	  BAD_CASE (fragP->fr_type);
229133965Sjdp	  break;
229277298Sobrien	}
229377298Sobrien    }
229433965Sjdp
229533965Sjdp  /* Do relax().  */
229633965Sjdp  {
229777298Sobrien    long stretch;	/* May be any size, 0 or negative.  */
229877298Sobrien    /* Cumulative number of addresses we have relaxed this pass.
229977298Sobrien       We may have relaxed more than one address.  */
230078828Sobrien    int stretched;	/* Have we stretched on this pass?  */
230133965Sjdp    /* This is 'cuz stretch may be zero, when, in fact some piece of code
230233965Sjdp       grew, and another shrank.  If a branch instruction doesn't fit anymore,
230333965Sjdp       we could be scrod.  */
230433965Sjdp
230533965Sjdp    do
230633965Sjdp      {
230778828Sobrien	stretch = 0;
230878828Sobrien	stretched = 0;
230977298Sobrien
231033965Sjdp	for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
231133965Sjdp	  {
231233965Sjdp	    long growth = 0;
231333965Sjdp	    addressT was_address;
231433965Sjdp	    offsetT offset;
231533965Sjdp	    symbolS *symbolP;
231633965Sjdp
231778828Sobrien	    fragP->relax_marker ^= 1;
231833965Sjdp	    was_address = fragP->fr_address;
231933965Sjdp	    address = fragP->fr_address += stretch;
232033965Sjdp	    symbolP = fragP->fr_symbol;
232133965Sjdp	    offset = fragP->fr_offset;
232233965Sjdp
232333965Sjdp	    switch (fragP->fr_type)
232433965Sjdp	      {
232577298Sobrien	      case rs_fill:	/* .fill never relaxes.  */
232633965Sjdp		growth = 0;
232733965Sjdp		break;
232833965Sjdp
232933965Sjdp#ifndef WORKING_DOT_WORD
233033965Sjdp		/* JF:  This is RMS's idea.  I do *NOT* want to be blamed
233133965Sjdp		   for it I do not want to write it.  I do not want to have
233233965Sjdp		   anything to do with it.  This is not the proper way to
233333965Sjdp		   implement this misfeature.  */
233433965Sjdp	      case rs_broken_word:
233533965Sjdp		{
233633965Sjdp		  struct broken_word *lie;
233733965Sjdp		  struct broken_word *untruth;
233833965Sjdp
233933965Sjdp		  /* Yes this is ugly (storing the broken_word pointer
234033965Sjdp		     in the symbol slot).  Still, this whole chunk of
234133965Sjdp		     code is ugly, and I don't feel like doing anything
234233965Sjdp		     about it.  Think of it as stubbornness in action.  */
234333965Sjdp		  growth = 0;
234433965Sjdp		  for (lie = (struct broken_word *) (fragP->fr_symbol);
234533965Sjdp		       lie && lie->dispfrag == fragP;
234633965Sjdp		       lie = lie->next_broken_word)
234733965Sjdp		    {
234833965Sjdp
234933965Sjdp		      if (lie->added)
235033965Sjdp			continue;
235133965Sjdp
235289857Sobrien		      offset = (S_GET_VALUE (lie->add)
235333965Sjdp				+ lie->addnum
235489857Sobrien				- S_GET_VALUE (lie->sub));
235533965Sjdp		      if (offset <= -32768 || offset >= 32767)
235633965Sjdp			{
235733965Sjdp			  if (flag_warn_displacement)
235833965Sjdp			    {
235933965Sjdp			      char buf[50];
236033965Sjdp			      sprint_value (buf, (addressT) lie->addnum);
236189857Sobrien			      as_warn_where (fragP->fr_file, fragP->fr_line,
236289857Sobrien					     _(".word %s-%s+%s didn't fit"),
236389857Sobrien					     S_GET_NAME (lie->add),
236489857Sobrien					     S_GET_NAME (lie->sub),
236589857Sobrien					     buf);
236633965Sjdp			    }
236733965Sjdp			  lie->added = 1;
236833965Sjdp			  if (fragP->fr_subtype == 0)
236933965Sjdp			    {
237033965Sjdp			      fragP->fr_subtype++;
237133965Sjdp			      growth += md_short_jump_size;
237233965Sjdp			    }
237333965Sjdp			  for (untruth = lie->next_broken_word;
237433965Sjdp			       untruth && untruth->dispfrag == lie->dispfrag;
237533965Sjdp			       untruth = untruth->next_broken_word)
237660484Sobrien			    if ((symbol_get_frag (untruth->add)
237760484Sobrien				 == symbol_get_frag (lie->add))
237860484Sobrien				&& (S_GET_VALUE (untruth->add)
237960484Sobrien				    == S_GET_VALUE (lie->add)))
238033965Sjdp			      {
238133965Sjdp				untruth->added = 2;
238233965Sjdp				untruth->use_jump = lie;
238333965Sjdp			      }
238433965Sjdp			  growth += md_long_jump_size;
238533965Sjdp			}
238633965Sjdp		    }
238733965Sjdp
238833965Sjdp		  break;
238977298Sobrien		}		/* case rs_broken_word  */
239033965Sjdp#endif
239133965Sjdp	      case rs_align:
239233965Sjdp	      case rs_align_code:
239377298Sobrien	      case rs_align_test:
239433965Sjdp		{
239533965Sjdp		  addressT oldoff, newoff;
239633965Sjdp
239733965Sjdp		  oldoff = relax_align (was_address + fragP->fr_fix,
239833965Sjdp					(int) offset);
239933965Sjdp		  newoff = relax_align (address + fragP->fr_fix,
240033965Sjdp					(int) offset);
240133965Sjdp
240233965Sjdp		  if (fragP->fr_subtype != 0)
240333965Sjdp		    {
240433965Sjdp		      if (oldoff > fragP->fr_subtype)
240533965Sjdp			oldoff = 0;
240633965Sjdp		      if (newoff > fragP->fr_subtype)
240733965Sjdp			newoff = 0;
240833965Sjdp		    }
240933965Sjdp
241033965Sjdp		  growth = newoff - oldoff;
241133965Sjdp		}
241233965Sjdp		break;
241333965Sjdp
241433965Sjdp	      case rs_org:
241533965Sjdp		{
241678828Sobrien		  addressT target = offset;
241778828Sobrien		  addressT after;
241833965Sjdp
241933965Sjdp		  if (symbolP)
242033965Sjdp		    {
242133965Sjdp#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
242233965Sjdp		      know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
242333965Sjdp			    || (S_GET_SEGMENT (symbolP) == SEG_DATA)
242433965Sjdp			    || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
242533965Sjdp			    || S_GET_SEGMENT (symbolP) == SEG_BSS);
242633965Sjdp		      know (symbolP->sy_frag);
242733965Sjdp		      know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
242833965Sjdp			    || (symbolP->sy_frag == &zero_address_frag));
242933965Sjdp#endif
243089857Sobrien                      /* Convert from an actual address to an octet offset
243189857Sobrien                         into the section.  Here it is assumed that the
243289857Sobrien                         section's VMA is zero, and can omit subtracting it
243389857Sobrien                         from the symbol's value to get the address offset.  */
243489857Sobrien                      know (S_GET_SECTION (symbolP)->vma == 0);
243589857Sobrien		      target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
243689857Sobrien		    }
243733965Sjdp
243833965Sjdp		  know (fragP->fr_next);
243933965Sjdp		  after = fragP->fr_next->fr_address;
244033965Sjdp		  growth = target - after;
244133965Sjdp		  if (growth < 0)
244233965Sjdp		    {
244333965Sjdp		      /* Growth may be negative, but variable part of frag
244433965Sjdp			 cannot have fewer than 0 chars.  That is, we can't
244577298Sobrien			 .org backwards.  */
244660484Sobrien		      as_bad_where (fragP->fr_file, fragP->fr_line,
244789857Sobrien				    _("attempt to .org backwards"));
244860484Sobrien
244960484Sobrien		      /* We've issued an error message.  Change the
245060484Sobrien                         frag to avoid cascading errors.  */
245160484Sobrien		      fragP->fr_type = rs_align;
245260484Sobrien		      fragP->fr_subtype = 0;
245360484Sobrien		      fragP->fr_offset = 0;
245460484Sobrien		      fragP->fr_fix = after - address;
245560484Sobrien		      growth = stretch;
245633965Sjdp		    }
245733965Sjdp
245877298Sobrien		  /* This is an absolute growth factor  */
245977298Sobrien		  growth -= stretch;
246033965Sjdp		  break;
246133965Sjdp		}
246233965Sjdp
246333965Sjdp	      case rs_space:
246489857Sobrien		growth = 0;
246533965Sjdp		if (symbolP)
246633965Sjdp		  {
246789857Sobrien		    offsetT amount;
246889857Sobrien
246989857Sobrien		    amount = S_GET_VALUE (symbolP);
247089857Sobrien		    if (S_GET_SEGMENT (symbolP) != absolute_section
247133965Sjdp			|| S_IS_COMMON (symbolP)
247233965Sjdp			|| ! S_IS_DEFINED (symbolP))
247333965Sjdp		      {
247489857Sobrien			as_bad_where (fragP->fr_file, fragP->fr_line,
247589857Sobrien				      _(".space specifies non-absolute value"));
247689857Sobrien			/* Prevent repeat of this error message.  */
247789857Sobrien			fragP->fr_symbol = 0;
247833965Sjdp		      }
247989857Sobrien		    else if (amount < 0)
248089857Sobrien		      {
248189857Sobrien			as_warn_where (fragP->fr_file, fragP->fr_line,
248289857Sobrien				       _(".space or .fill with negative value, ignored"));
248389857Sobrien			fragP->fr_symbol = 0;
248489857Sobrien		      }
248589857Sobrien		    else
248689857Sobrien		      growth = (was_address + fragP->fr_fix + amount
248789857Sobrien				- fragP->fr_next->fr_address);
248833965Sjdp		  }
248933965Sjdp		break;
249033965Sjdp
249133965Sjdp	      case rs_machine_dependent:
249233965Sjdp#ifdef md_relax_frag
249378828Sobrien		growth = md_relax_frag (segment, fragP, stretch);
249433965Sjdp#else
249533965Sjdp#ifdef TC_GENERIC_RELAX_TABLE
249633965Sjdp		/* The default way to relax a frag is to look through
249733965Sjdp		   TC_GENERIC_RELAX_TABLE.  */
249878828Sobrien		growth = relax_frag (segment, fragP, stretch);
249977298Sobrien#endif /* TC_GENERIC_RELAX_TABLE  */
250033965Sjdp#endif
250133965Sjdp		break;
250233965Sjdp
250338889Sjdp	      case rs_leb128:
250438889Sjdp		{
250538889Sjdp		  valueT value;
250638889Sjdp		  int size;
250738889Sjdp
250889857Sobrien		  value = resolve_symbol_value (fragP->fr_symbol);
250938889Sjdp		  size = sizeof_leb128 (value, fragP->fr_subtype);
251038889Sjdp		  growth = size - fragP->fr_offset;
251138889Sjdp		  fragP->fr_offset = size;
251238889Sjdp		}
251338889Sjdp		break;
251438889Sjdp
251538889Sjdp	      case rs_cfa:
251638889Sjdp		growth = eh_frame_relax_frag (fragP);
251738889Sjdp		break;
251838889Sjdp
251977298Sobrien	      case rs_dwarf2dbg:
252077298Sobrien		growth = dwarf2dbg_relax_frag (fragP);
252177298Sobrien		break;
252277298Sobrien
252333965Sjdp	      default:
252433965Sjdp		BAD_CASE (fragP->fr_type);
252533965Sjdp		break;
252633965Sjdp	      }
252733965Sjdp	    if (growth)
252833965Sjdp	      {
252933965Sjdp		stretch += growth;
253078828Sobrien		stretched = 1;
253133965Sjdp	      }
253277298Sobrien	  }			/* For each frag in the segment.  */
253333965Sjdp      }
253477298Sobrien    while (stretched);		/* Until nothing further to relax.  */
253577298Sobrien  }				/* do_relax  */
253633965Sjdp
253789857Sobrien  ret = 0;
253889857Sobrien  for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
253989857Sobrien    if (fragP->last_fr_address != fragP->fr_address)
254089857Sobrien      {
254189857Sobrien	fragP->last_fr_address = fragP->fr_address;
254289857Sobrien	ret = 1;
254389857Sobrien      }
254489857Sobrien  return ret;
254577298Sobrien}
254633965Sjdp
254733965Sjdp#if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
254833965Sjdp
254933965Sjdp#ifndef TC_RELOC_RTSYM_LOC_FIXUP
255033965Sjdp#define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
255133965Sjdp#endif
255233965Sjdp
255333965Sjdp/* fixup_segment()
255433965Sjdp
255533965Sjdp   Go through all the fixS's in a segment and see which ones can be
255633965Sjdp   handled now.  (These consist of fixS where we have since discovered
255733965Sjdp   the value of a symbol, or the address of the frag involved.)
255889857Sobrien   For each one, call md_apply_fix3 to put the fix into the frag data.
255933965Sjdp
256033965Sjdp   Result is a count of how many relocation structs will be needed to
256133965Sjdp   handle the remaining fixS's that we couldn't completely handle here.
256233965Sjdp   These will be output later by emit_relocations().  */
256333965Sjdp
256433965Sjdpstatic long
256533965Sjdpfixup_segment (fixP, this_segment_type)
256633965Sjdp     register fixS *fixP;
256777298Sobrien     segT this_segment_type;	/* N_TYPE bits for segment.  */
256833965Sjdp{
256933965Sjdp  long seg_reloc_count = 0;
257033965Sjdp  symbolS *add_symbolP;
257133965Sjdp  symbolS *sub_symbolP;
257233965Sjdp  valueT add_number;
257333965Sjdp  int size;
257433965Sjdp  char *place;
257533965Sjdp  long where;
257633965Sjdp  int pcrel, plt;
257733965Sjdp  fragS *fragP;
257833965Sjdp  segT add_symbol_segment = absolute_section;
257933965Sjdp
258033965Sjdp  /* If the linker is doing the relaxing, we must not do any fixups.
258133965Sjdp
258233965Sjdp     Well, strictly speaking that's not true -- we could do any that are
258333965Sjdp     PC-relative and don't cross regions that could change size.  And for the
258433965Sjdp     i960 (the only machine for which we've got a relaxing linker right now),
258533965Sjdp     we might be able to turn callx/callj into bal anyways in cases where we
258633965Sjdp     know the maximum displacement.  */
258777298Sobrien  if (linkrelax && TC_LINKRELAX_FIXUP (this_segment_type))
258833965Sjdp    {
258933965Sjdp      for (; fixP; fixP = fixP->fx_next)
259033965Sjdp	seg_reloc_count++;
259133965Sjdp      TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
259233965Sjdp      return seg_reloc_count;
259333965Sjdp    }
259433965Sjdp
259533965Sjdp  for (; fixP; fixP = fixP->fx_next)
259633965Sjdp    {
259733965Sjdp#ifdef DEBUG5
259833965Sjdp      fprintf (stderr, "\nprocessing fixup:\n");
259933965Sjdp      print_fixup (fixP);
260033965Sjdp#endif
260133965Sjdp
260233965Sjdp      fragP = fixP->fx_frag;
260333965Sjdp      know (fragP);
260433965Sjdp      where = fixP->fx_where;
260533965Sjdp      place = fragP->fr_literal + where;
260633965Sjdp      size = fixP->fx_size;
260733965Sjdp      add_symbolP = fixP->fx_addsy;
260833965Sjdp#ifdef TC_VALIDATE_FIX
260933965Sjdp      TC_VALIDATE_FIX (fixP, this_segment_type, skip);
261033965Sjdp#endif
261133965Sjdp      sub_symbolP = fixP->fx_subsy;
261233965Sjdp      add_number = fixP->fx_offset;
261333965Sjdp      pcrel = fixP->fx_pcrel;
261433965Sjdp      plt = fixP->fx_plt;
261533965Sjdp
261633965Sjdp      if (add_symbolP != NULL
261760484Sobrien	  && symbol_mri_common_p (add_symbolP))
261833965Sjdp	{
261933965Sjdp	  know (add_symbolP->sy_value.X_op == O_symbol);
262033965Sjdp	  add_number += S_GET_VALUE (add_symbolP);
262133965Sjdp	  fixP->fx_offset = add_number;
262260484Sobrien	  add_symbolP = fixP->fx_addsy =
262360484Sobrien	    symbol_get_value_expression (add_symbolP)->X_add_symbol;
262433965Sjdp	}
262533965Sjdp
262633965Sjdp      if (add_symbolP)
262733965Sjdp	add_symbol_segment = S_GET_SEGMENT (add_symbolP);
262833965Sjdp
262933965Sjdp      if (sub_symbolP)
263033965Sjdp	{
263189857Sobrien	  resolve_symbol_value (sub_symbolP);
263233965Sjdp	  if (add_symbolP == NULL || add_symbol_segment == absolute_section)
263333965Sjdp	    {
263433965Sjdp	      if (add_symbolP != NULL)
263533965Sjdp		{
263633965Sjdp		  add_number += S_GET_VALUE (add_symbolP);
263733965Sjdp		  add_symbolP = NULL;
263833965Sjdp		  fixP->fx_addsy = NULL;
263933965Sjdp		}
264033965Sjdp
264177298Sobrien	      /* It's just -sym.  */
264233965Sjdp	      if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
264333965Sjdp		{
264433965Sjdp		  add_number -= S_GET_VALUE (sub_symbolP);
264533965Sjdp		  fixP->fx_subsy = NULL;
264633965Sjdp		}
264733965Sjdp	      else if (pcrel
264833965Sjdp		       && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
264933965Sjdp		{
265033965Sjdp		  /* Should try converting to a constant.  */
265133965Sjdp		  goto bad_sub_reloc;
265233965Sjdp		}
265333965Sjdp	      else
265433965Sjdp	      bad_sub_reloc:
265533965Sjdp		as_bad_where (fixP->fx_file, fixP->fx_line,
265689857Sobrien			      _("negative of non-absolute symbol `%s'"),
265733965Sjdp			      S_GET_NAME (sub_symbolP));
265833965Sjdp	    }
265933965Sjdp	  else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
266033965Sjdp		   && SEG_NORMAL (add_symbol_segment))
266133965Sjdp	    {
266233965Sjdp	      /* Difference of 2 symbols from same segment.
266333965Sjdp		 Can't make difference of 2 undefineds: 'value' means
266477298Sobrien		 something different for N_UNDF.  */
266533965Sjdp#ifdef TC_I960
266633965Sjdp	      /* Makes no sense to use the difference of 2 arbitrary symbols
266733965Sjdp		 as the target of a call instruction.  */
266833965Sjdp	      if (fixP->fx_tcbit)
266933965Sjdp		as_bad_where (fixP->fx_file, fixP->fx_line,
267089857Sobrien			      _("callj to difference of two symbols"));
267177298Sobrien#endif /* TC_I960  */
267289857Sobrien	      add_number += (S_GET_VALUE (add_symbolP)
267389857Sobrien			     - S_GET_VALUE (sub_symbolP));
267489857Sobrien	      if (1
267589857Sobrien#ifdef TC_M68K
267689857Sobrien		  /* See the comment below about 68k weirdness.  */
267789857Sobrien		  && 0
267889857Sobrien#endif
267989857Sobrien		  && pcrel)
268089857Sobrien		add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
268133965Sjdp
268233965Sjdp	      add_symbolP = NULL;
268377298Sobrien	      pcrel = 0;	/* No further pcrel processing.  */
268433965Sjdp
268533965Sjdp	      /* Let the target machine make the final determination
268633965Sjdp		 as to whether or not a relocation will be needed to
268733965Sjdp		 handle this fixup.  */
268833965Sjdp	      if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
268933965Sjdp		{
269033965Sjdp		  fixP->fx_pcrel = 0;
269133965Sjdp		  fixP->fx_addsy = NULL;
269233965Sjdp		  fixP->fx_subsy = NULL;
269333965Sjdp		}
269433965Sjdp	    }
269533965Sjdp	  else
269633965Sjdp	    {
269777298Sobrien	      /* Different segments in subtraction.  */
269833965Sjdp	      know (!(S_IS_EXTERNAL (sub_symbolP)
269933965Sjdp		      && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
270033965Sjdp
270133965Sjdp	      if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
270233965Sjdp		add_number -= S_GET_VALUE (sub_symbolP);
270333965Sjdp
270433965Sjdp#ifdef DIFF_EXPR_OK
270589857Sobrien	      else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type)
270633965Sjdp		{
270733965Sjdp		  /* Make it pc-relative.  */
270889857Sobrien		  if (0
270989857Sobrien#ifdef TC_M68K
271089857Sobrien		      /* Do this for m68k even if it's already described
271189857Sobrien			 as pc-relative.  On the m68k, an operand of
271289857Sobrien			 "pc@(foo-.-2)" should address "foo" in a
271389857Sobrien			 pc-relative mode.  */
271489857Sobrien		      || 1
271589857Sobrien#endif
271689857Sobrien		      || !pcrel)
271789857Sobrien		    {
271889857Sobrien		      add_number += MD_PCREL_FROM_SECTION (fixP,
271989857Sobrien							   this_segment_type);
272089857Sobrien		      pcrel = 1;
272189857Sobrien		      fixP->fx_pcrel = 1;
272289857Sobrien		    }
272389857Sobrien
272489857Sobrien		  add_number -= S_GET_VALUE (sub_symbolP);
272533965Sjdp		  sub_symbolP = 0;
272633965Sjdp		  fixP->fx_subsy = 0;
272733965Sjdp		}
272833965Sjdp#endif
272933965Sjdp#ifdef UNDEFINED_DIFFERENCE_OK
273033965Sjdp	      /* The PA needs this for PIC code generation.  We basically
273133965Sjdp		 don't want to do anything if we have the difference of two
273233965Sjdp		 symbols at this point.  */
273333965Sjdp	      else if (1)
273433965Sjdp		{
273533965Sjdp		  /* Leave it alone.  */
273633965Sjdp		}
273733965Sjdp#endif
273833965Sjdp#ifdef BFD_ASSEMBLER
273933965Sjdp	      else if (fixP->fx_r_type == BFD_RELOC_GPREL32
274033965Sjdp		       || fixP->fx_r_type == BFD_RELOC_GPREL16)
274133965Sjdp		{
274233965Sjdp		  /* Leave it alone.  */
274333965Sjdp		}
274433965Sjdp#endif
274533965Sjdp	      else
274633965Sjdp		{
274733965Sjdp		  char buf[50];
274833965Sjdp		  sprint_value (buf, fragP->fr_address + where);
274933965Sjdp		  as_bad_where (fixP->fx_file, fixP->fx_line,
275089857Sobrien				_("subtraction of two symbols in different sections `%s' {%s section} - `%s' {%s section} at file address %s"),
275160484Sobrien				S_GET_NAME (add_symbolP),
275260484Sobrien				segment_name (S_GET_SEGMENT (add_symbolP)),
275360484Sobrien				S_GET_NAME (sub_symbolP),
275433965Sjdp				segment_name (S_GET_SEGMENT (sub_symbolP)),
275560484Sobrien				buf);
275633965Sjdp		}
275733965Sjdp	    }
275833965Sjdp	}
275933965Sjdp
276033965Sjdp      if (add_symbolP)
276133965Sjdp	{
276233965Sjdp	  if (add_symbol_segment == this_segment_type && pcrel && !plt
276333965Sjdp	      && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
276433965Sjdp	    {
276577298Sobrien	      /* This fixup was made when the symbol's segment was
276677298Sobrien		 SEG_UNKNOWN, but it is now in the local segment.
276777298Sobrien		 So we know how to do the address without relocation.  */
276833965Sjdp#ifdef TC_I960
276933965Sjdp	      /* reloc_callj() may replace a 'call' with a 'calls' or a
277033965Sjdp		 'bal', in which cases it modifies *fixP as appropriate.
277133965Sjdp		 In the case of a 'calls', no further work is required,
277233965Sjdp		 and *fixP has been set up to make the rest of the code
277377298Sobrien		 below a no-op.  */
277433965Sjdp	      reloc_callj (fixP);
277577298Sobrien#endif /* TC_I960  */
277633965Sjdp
277733965Sjdp	      add_number += S_GET_VALUE (add_symbolP);
277833965Sjdp	      add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
277977298Sobrien	      /* Lie.  Don't want further pcrel processing.  */
278077298Sobrien	      pcrel = 0;
278133965Sjdp
278233965Sjdp	      /* Let the target machine make the final determination
278333965Sjdp		 as to whether or not a relocation will be needed to
278433965Sjdp		 handle this fixup.  */
278533965Sjdp	      if (!TC_FORCE_RELOCATION (fixP))
278633965Sjdp		{
278733965Sjdp		  fixP->fx_pcrel = 0;
278833965Sjdp		  fixP->fx_addsy = NULL;
278933965Sjdp		}
279033965Sjdp	    }
279133965Sjdp	  else
279233965Sjdp	    {
279333965Sjdp	      if (add_symbol_segment == absolute_section
279433965Sjdp		  && ! pcrel)
279533965Sjdp		{
279633965Sjdp#ifdef TC_I960
279733965Sjdp		  /* See comment about reloc_callj() above.  */
279833965Sjdp		  reloc_callj (fixP);
279977298Sobrien#endif /* TC_I960  */
280033965Sjdp		  add_number += S_GET_VALUE (add_symbolP);
280133965Sjdp
280233965Sjdp		  /* Let the target machine make the final determination
280333965Sjdp		     as to whether or not a relocation will be needed to
280433965Sjdp		     handle this fixup.  */
280533965Sjdp
280633965Sjdp		  if (!TC_FORCE_RELOCATION (fixP))
280733965Sjdp		    {
280833965Sjdp		      fixP->fx_addsy = NULL;
280933965Sjdp		      add_symbolP = NULL;
281033965Sjdp		    }
281133965Sjdp		}
281233965Sjdp	      else if (add_symbol_segment == undefined_section
281333965Sjdp#ifdef BFD_ASSEMBLER
281433965Sjdp		       || bfd_is_com_section (add_symbol_segment)
281533965Sjdp#endif
281633965Sjdp		       )
281733965Sjdp		{
281833965Sjdp#ifdef TC_I960
281933965Sjdp		  if ((int) fixP->fx_bit_fixP == 13)
282033965Sjdp		    {
282133965Sjdp		      /* This is a COBR instruction.  They have only a
282277298Sobrien			 13-bit displacement and are only to be used
282377298Sobrien			 for local branches: flag as error, don't generate
282477298Sobrien			 relocation.  */
282533965Sjdp		      as_bad_where (fixP->fx_file, fixP->fx_line,
282660484Sobrien				    _("can't use COBR format with external label"));
282733965Sjdp		      fixP->fx_addsy = NULL;
282833965Sjdp		      fixP->fx_done = 1;
282933965Sjdp		      continue;
283077298Sobrien		    }		/* COBR.  */
283177298Sobrien#endif /* TC_I960  */
283233965Sjdp
283333965Sjdp#ifdef OBJ_COFF
283433965Sjdp#ifdef TE_I386AIX
283533965Sjdp		  if (S_IS_COMMON (add_symbolP))
283633965Sjdp		    add_number += S_GET_VALUE (add_symbolP);
283777298Sobrien#endif /* TE_I386AIX  */
283877298Sobrien#endif /* OBJ_COFF  */
283933965Sjdp		  ++seg_reloc_count;
284033965Sjdp		}
284133965Sjdp	      else
284233965Sjdp		{
284333965Sjdp		  seg_reloc_count++;
284477298Sobrien		  if (TC_FIX_ADJUSTABLE (fixP))
284577298Sobrien		    add_number += S_GET_VALUE (add_symbolP);
284633965Sjdp		}
284733965Sjdp	    }
284833965Sjdp	}
284933965Sjdp
285033965Sjdp      if (pcrel)
285133965Sjdp	{
285233965Sjdp	  add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
285333965Sjdp	  if (add_symbolP == 0)
285433965Sjdp	    {
285533965Sjdp#ifndef BFD_ASSEMBLER
285633965Sjdp	      fixP->fx_addsy = &abs_symbol;
285733965Sjdp#else
285833965Sjdp	      fixP->fx_addsy = section_symbol (absolute_section);
285933965Sjdp#endif
286060484Sobrien	      symbol_mark_used_in_reloc (fixP->fx_addsy);
286133965Sjdp	      ++seg_reloc_count;
286233965Sjdp	    }
286333965Sjdp	}
286433965Sjdp
286560484Sobrien      if (!fixP->fx_done)
286689857Sobrien	md_apply_fix3 (fixP, & add_number, this_segment_type);
286760484Sobrien
286833965Sjdp      if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
286933965Sjdp	{
287038889Sjdp	  if ((size_t) size < sizeof (valueT))
287133965Sjdp	    {
287260484Sobrien	      valueT mask;
287333965Sjdp
287433965Sjdp	      mask = 0;
287577298Sobrien	      mask--;		/* Set all bits to one.  */
287660484Sobrien	      mask <<= size * 8 - (fixP->fx_signed ? 1 : 0);
287760484Sobrien	      if ((add_number & mask) != 0 && (add_number & mask) != mask)
287833965Sjdp		{
287933965Sjdp		  char buf[50], buf2[50];
288033965Sjdp		  sprint_value (buf, fragP->fr_address + where);
288133965Sjdp		  if (add_number > 1000)
288233965Sjdp		    sprint_value (buf2, add_number);
288333965Sjdp		  else
288433965Sjdp		    sprintf (buf2, "%ld", (long) add_number);
288533965Sjdp		  as_bad_where (fixP->fx_file, fixP->fx_line,
288689857Sobrien				_("value of %s too large for field of %d bytes at %s"),
288733965Sjdp				buf2, size, buf);
288877298Sobrien		} /* Generic error checking.  */
288933965Sjdp	    }
289033965Sjdp#ifdef WARN_SIGNED_OVERFLOW_WORD
289133965Sjdp	  /* Warn if a .word value is too large when treated as a signed
289233965Sjdp	     number.  We already know it is not too negative.  This is to
289333965Sjdp	     catch over-large switches generated by gcc on the 68k.  */
289433965Sjdp	  if (!flag_signed_overflow_ok
289533965Sjdp	      && size == 2
289633965Sjdp	      && add_number > 0x7fff)
289733965Sjdp	    as_bad_where (fixP->fx_file, fixP->fx_line,
289889857Sobrien			  _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
289933965Sjdp			  (long) add_number,
290033965Sjdp			  (unsigned long) (fragP->fr_address + where));
290133965Sjdp#endif
290277298Sobrien	}			/* Not a bit fix.  */
290333965Sjdp
290433965Sjdp#ifdef TC_VALIDATE_FIX
290560484Sobrien    skip:  ATTRIBUTE_UNUSED_LABEL
290660484Sobrien      ;
290733965Sjdp#endif
290833965Sjdp#ifdef DEBUG5
290933965Sjdp      fprintf (stderr, "result:\n");
291033965Sjdp      print_fixup (fixP);
291133965Sjdp#endif
291277298Sobrien    }				/* For each fixS in this segment.  */
291333965Sjdp
291433965Sjdp  TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
291533965Sjdp  return seg_reloc_count;
291633965Sjdp}
291733965Sjdp
291833965Sjdp#endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
291933965Sjdp
292033965Sjdpvoid
292133965Sjdpnumber_to_chars_bigendian (buf, val, n)
292233965Sjdp     char *buf;
292333965Sjdp     valueT val;
292433965Sjdp     int n;
292533965Sjdp{
292638889Sjdp  if ((size_t) n > sizeof (val) || n <= 0)
292733965Sjdp    abort ();
292833965Sjdp  while (n--)
292933965Sjdp    {
293033965Sjdp      buf[n] = val & 0xff;
293133965Sjdp      val >>= 8;
293233965Sjdp    }
293333965Sjdp}
293433965Sjdp
293533965Sjdpvoid
293633965Sjdpnumber_to_chars_littleendian (buf, val, n)
293733965Sjdp     char *buf;
293833965Sjdp     valueT val;
293933965Sjdp     int n;
294033965Sjdp{
294138889Sjdp  if ((size_t) n > sizeof (val) || n <= 0)
294233965Sjdp    abort ();
294333965Sjdp  while (n--)
294433965Sjdp    {
294533965Sjdp      *buf++ = val & 0xff;
294633965Sjdp      val >>= 8;
294733965Sjdp    }
294833965Sjdp}
294933965Sjdp
295033965Sjdpvoid
295133965Sjdpwrite_print_statistics (file)
295233965Sjdp     FILE *file;
295333965Sjdp{
295460484Sobrien  fprintf (file, "fixups: %d\n", n_fixups);
295533965Sjdp}
295633965Sjdp
295777298Sobrien/* For debugging.  */
295833965Sjdpextern int indent_level;
295933965Sjdp
296033965Sjdpvoid
296133965Sjdpprint_fixup (fixp)
296233965Sjdp     fixS *fixp;
296333965Sjdp{
296433965Sjdp  indent_level = 1;
296533965Sjdp  fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
296633965Sjdp  if (fixp->fx_pcrel)
296733965Sjdp    fprintf (stderr, " pcrel");
296833965Sjdp  if (fixp->fx_pcrel_adjust)
296933965Sjdp    fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
297033965Sjdp  if (fixp->fx_im_disp)
297133965Sjdp    {
297233965Sjdp#ifdef TC_NS32K
297333965Sjdp      fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
297433965Sjdp#else
297533965Sjdp      fprintf (stderr, " im_disp");
297633965Sjdp#endif
297733965Sjdp    }
297833965Sjdp  if (fixp->fx_tcbit)
297933965Sjdp    fprintf (stderr, " tcbit");
298033965Sjdp  if (fixp->fx_done)
298133965Sjdp    fprintf (stderr, " done");
298233965Sjdp  fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
298333965Sjdp	   fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
298433965Sjdp	   (long) fixp->fx_offset, (long) fixp->fx_addnumber);
298533965Sjdp#ifdef BFD_ASSEMBLER
298633965Sjdp  fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
298733965Sjdp	   fixp->fx_r_type);
298833965Sjdp#else
298933965Sjdp#ifdef NEED_FX_R_TYPE
299033965Sjdp  fprintf (stderr, " r_type=%d", fixp->fx_r_type);
299133965Sjdp#endif
299233965Sjdp#endif
299333965Sjdp  if (fixp->fx_addsy)
299433965Sjdp    {
299533965Sjdp      fprintf (stderr, "\n   +<");
299633965Sjdp      print_symbol_value_1 (stderr, fixp->fx_addsy);
299733965Sjdp      fprintf (stderr, ">");
299833965Sjdp    }
299933965Sjdp  if (fixp->fx_subsy)
300033965Sjdp    {
300133965Sjdp      fprintf (stderr, "\n   -<");
300233965Sjdp      print_symbol_value_1 (stderr, fixp->fx_subsy);
300333965Sjdp      fprintf (stderr, ">");
300433965Sjdp    }
300533965Sjdp  fprintf (stderr, "\n");
300638889Sjdp#ifdef TC_FIX_DATA_PRINT
300738889Sjdp  TC_FIX_DATA_PRINT (stderr, fixp);
300838889Sjdp#endif
300933965Sjdp}
3010