write.c revision 222205
159243Sobrien/* write.c - emit .o file
259243Sobrien   Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
359243Sobrien   1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
459243Sobrien   Free Software Foundation, Inc.
559243Sobrien
659243Sobrien   This file is part of GAS, the GNU Assembler.
759243Sobrien
859243Sobrien   GAS is free software; you can redistribute it and/or modify
959243Sobrien   it under the terms of the GNU General Public License as published by
1059243Sobrien   the Free Software Foundation; either version 2, or (at your option)
1159243Sobrien   any later version.
1259243Sobrien
1359243Sobrien   GAS is distributed in the hope that it will be useful,
1459243Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1559243Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1659243Sobrien   GNU General Public License for more details.
1759243Sobrien
1859243Sobrien   You should have received a copy of the GNU General Public License
1959243Sobrien   along with GAS; see the file COPYING.  If not, write to the Free
2059243Sobrien   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
2159243Sobrien   02110-1301, USA.  */
2259243Sobrien
2359243Sobrien/* This thing should be set up to do byteordering correctly.  But...  */
2459243Sobrien
2559243Sobrien#include "as.h"
2659243Sobrien#include "subsegs.h"
2759243Sobrien#include "obstack.h"
2859243Sobrien#include "output-file.h"
2959243Sobrien#include "dwarf2dbg.h"
3059243Sobrien#include "libbfd.h"
3159243Sobrien
3259243Sobrien#ifndef TC_ADJUST_RELOC_COUNT
3359243Sobrien#define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
3459243Sobrien#endif
3559243Sobrien
3659243Sobrien#ifndef TC_FORCE_RELOCATION
3759243Sobrien#define TC_FORCE_RELOCATION(FIX)		\
3859243Sobrien  (generic_force_reloc (FIX))
3959243Sobrien#endif
4059243Sobrien
4159243Sobrien#ifndef TC_FORCE_RELOCATION_ABS
4259243Sobrien#define TC_FORCE_RELOCATION_ABS(FIX)		\
4359243Sobrien  (TC_FORCE_RELOCATION (FIX))
4459243Sobrien#endif
4559243Sobrien
4659243Sobrien#ifndef TC_FORCE_RELOCATION_LOCAL
4759243Sobrien#define TC_FORCE_RELOCATION_LOCAL(FIX)		\
4859243Sobrien  (!(FIX)->fx_pcrel				\
4959243Sobrien   || TC_FORCE_RELOCATION (FIX))
5059243Sobrien#endif
5159243Sobrien
5259243Sobrien#ifndef TC_FORCE_RELOCATION_SUB_SAME
5359243Sobrien#define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG)	\
5459243Sobrien  (! SEG_NORMAL (SEG))
5559243Sobrien#endif
5659243Sobrien
5759243Sobrien#ifndef TC_FORCE_RELOCATION_SUB_ABS
5859243Sobrien#define TC_FORCE_RELOCATION_SUB_ABS(FIX)	0
5959243Sobrien#endif
6059243Sobrien
6159243Sobrien#ifndef TC_FORCE_RELOCATION_SUB_LOCAL
6259243Sobrien#ifdef DIFF_EXPR_OK
6359243Sobrien#define TC_FORCE_RELOCATION_SUB_LOCAL(FIX)	0
6459243Sobrien#else
6559243Sobrien#define TC_FORCE_RELOCATION_SUB_LOCAL(FIX)	1
6659243Sobrien#endif
6759243Sobrien#endif
6859243Sobrien
6959243Sobrien#ifndef TC_VALIDATE_FIX_SUB
7059243Sobrien#ifdef UNDEFINED_DIFFERENCE_OK
7159243Sobrien/* The PA needs this for PIC code generation.  */
7259243Sobrien#define TC_VALIDATE_FIX_SUB(FIX) 1
7359243Sobrien#else
7459243Sobrien#define TC_VALIDATE_FIX_SUB(FIX)		\
7559243Sobrien  ((FIX)->fx_r_type == BFD_RELOC_GPREL32	\
7659243Sobrien   || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
7759243Sobrien#endif
7859243Sobrien#endif
7959243Sobrien
8059243Sobrien#ifndef TC_LINKRELAX_FIXUP
8159243Sobrien#define TC_LINKRELAX_FIXUP(SEG) 1
8259243Sobrien#endif
8359243Sobrien
8459243Sobrien#ifndef MD_APPLY_SYM_VALUE
8559243Sobrien#define MD_APPLY_SYM_VALUE(FIX) 1
8659243Sobrien#endif
8759243Sobrien
8859243Sobrien#ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
8959243Sobrien#define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
9059243Sobrien#endif
9159243Sobrien
9259243Sobrien#ifndef	MD_PCREL_FROM_SECTION
9359243Sobrien#define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
9459243Sobrien#endif
9559243Sobrien
9659243Sobrien#ifndef TC_FAKE_LABEL
9759243Sobrien#define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
9859243Sobrien#endif
9959243Sobrien
10059243Sobrien/* Positive values of TC_FX_SIZE_SLACK allow a target to define
10159243Sobrien   fixups that far past the end of a frag.  Having such fixups
10259243Sobrien   is of course most most likely a bug in setting fx_size correctly.
10359243Sobrien   A negative value disables the fixup check entirely, which is
10459243Sobrien   appropriate for something like the Renesas / SuperH SH_COUNT
10559243Sobrien   reloc.  */
10659243Sobrien#ifndef TC_FX_SIZE_SLACK
10759243Sobrien#define TC_FX_SIZE_SLACK(FIX) 0
10859243Sobrien#endif
10959243Sobrien
11059243Sobrien/* Used to control final evaluation of expressions.  */
11159243Sobrienint finalize_syms = 0;
11259243Sobrien
11359243Sobrienint symbol_table_frozen;
11459243Sobrien
11559243SobriensymbolS *abs_section_sym;
11659243Sobrien
11759243Sobrien/* Remember the value of dot when parsing expressions.  */
11859243SobrienaddressT dot_value;
11959243Sobrien
12059243Sobrien/* Relocs generated by ".reloc" pseudo.  */
12159243Sobrienstruct reloc_list* reloc_list;
12259243Sobrien
12359243Sobrienvoid print_fixup (fixS *);
12459243Sobrien
12559243Sobrien/* We generally attach relocs to frag chains.  However, after we have
12659243Sobrien   chained these all together into a segment, any relocs we add after
12759243Sobrien   that must be attached to a segment.  This will include relocs added
12859243Sobrien   in md_estimate_size_for_relax, for example.  */
12959243Sobrienstatic int frags_chained = 0;
13059243Sobrien
13159243Sobrienstatic int n_fixups;
13259243Sobrien
13359243Sobrien#define RELOC_ENUM enum bfd_reloc_code_real
13459243Sobrien
13559243Sobrien/* Create a fixS in obstack 'notes'.  */
13659243Sobrien
13759243Sobrienstatic fixS *
13859243Sobrienfix_new_internal (fragS *frag,		/* Which frag?  */
13959243Sobrien		  int where,		/* Where in that frag?  */
14059243Sobrien		  int size,		/* 1, 2, or 4 usually.  */
14159243Sobrien		  symbolS *add_symbol,	/* X_add_symbol.  */
14259243Sobrien		  symbolS *sub_symbol,	/* X_op_symbol.  */
14359243Sobrien		  offsetT offset,	/* X_add_number.  */
14459243Sobrien		  int pcrel,		/* TRUE if PC-relative relocation.  */
14559243Sobrien		  RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type.  */)
14659243Sobrien{
14759243Sobrien  fixS *fixP;
14859243Sobrien
14959243Sobrien  n_fixups++;
15059243Sobrien
15159243Sobrien  fixP = obstack_alloc (&notes, sizeof (fixS));
15259243Sobrien
15359243Sobrien  fixP->fx_frag = frag;
15459243Sobrien  fixP->fx_where = where;
15559243Sobrien  fixP->fx_size = size;
15659243Sobrien  /* We've made fx_size a narrow field; check that it's wide enough.  */
15759243Sobrien  if (fixP->fx_size != size)
15859243Sobrien    {
15959243Sobrien      as_bad (_("field fx_size too small to hold %d"), size);
16059243Sobrien      abort ();
16159243Sobrien    }
16259243Sobrien  fixP->fx_addsy = add_symbol;
16359243Sobrien  fixP->fx_subsy = sub_symbol;
16459243Sobrien  fixP->fx_offset = offset;
16559243Sobrien  fixP->fx_dot_value = dot_value;
16659243Sobrien  fixP->fx_pcrel = pcrel;
16759243Sobrien  fixP->fx_r_type = r_type;
16859243Sobrien  fixP->fx_im_disp = 0;
16959243Sobrien  fixP->fx_pcrel_adjust = 0;
17059243Sobrien  fixP->fx_bit_fixP = 0;
17159243Sobrien  fixP->fx_addnumber = 0;
17259243Sobrien  fixP->fx_tcbit = 0;
17359243Sobrien  fixP->fx_tcbit2 = 0;
17459243Sobrien  fixP->fx_done = 0;
17559243Sobrien  fixP->fx_no_overflow = 0;
17659243Sobrien  fixP->fx_signed = 0;
17759243Sobrien
17859243Sobrien#ifdef USING_CGEN
17959243Sobrien  fixP->fx_cgen.insn = NULL;
18059243Sobrien  fixP->fx_cgen.opinfo = 0;
18159243Sobrien#endif
18259243Sobrien
18359243Sobrien#ifdef TC_FIX_TYPE
18459243Sobrien  TC_INIT_FIX_DATA (fixP);
18559243Sobrien#endif
18659243Sobrien
18759243Sobrien  as_where (&fixP->fx_file, &fixP->fx_line);
18859243Sobrien
18959243Sobrien  /* Usually, we want relocs sorted numerically, but while
19059243Sobrien     comparing to older versions of gas that have relocs
19159243Sobrien     reverse sorted, it is convenient to have this compile
19259243Sobrien     time option.  xoxorich.  */
19359243Sobrien  {
19459243Sobrien
19559243Sobrien    fixS **seg_fix_rootP = (frags_chained
19659243Sobrien			    ? &seg_info (now_seg)->fix_root
19759243Sobrien			    : &frchain_now->fix_root);
19859243Sobrien    fixS **seg_fix_tailP = (frags_chained
19959243Sobrien			    ? &seg_info (now_seg)->fix_tail
20059243Sobrien			    : &frchain_now->fix_tail);
20159243Sobrien
20259243Sobrien#ifdef REVERSE_SORT_RELOCS
20359243Sobrien
20459243Sobrien    fixP->fx_next = *seg_fix_rootP;
20559243Sobrien    *seg_fix_rootP = fixP;
20659243Sobrien
20759243Sobrien#else /* REVERSE_SORT_RELOCS  */
20859243Sobrien
20959243Sobrien    fixP->fx_next = NULL;
21059243Sobrien
21159243Sobrien    if (*seg_fix_tailP)
21259243Sobrien      (*seg_fix_tailP)->fx_next = fixP;
21359243Sobrien    else
21459243Sobrien      *seg_fix_rootP = fixP;
21559243Sobrien    *seg_fix_tailP = fixP;
21659243Sobrien
21759243Sobrien#endif /* REVERSE_SORT_RELOCS  */
21859243Sobrien  }
21959243Sobrien
22059243Sobrien  return fixP;
22159243Sobrien}
22259243Sobrien
22359243Sobrien/* Create a fixup relative to a symbol (plus a constant).  */
22459243Sobrien
22559243SobrienfixS *
22659243Sobrienfix_new (fragS *frag,		/* Which frag?  */
22759243Sobrien	 int where,			/* Where in that frag?  */
22859243Sobrien	 int size,			/* 1, 2, or 4 usually.  */
22959243Sobrien	 symbolS *add_symbol,	/* X_add_symbol.  */
23059243Sobrien	 offsetT offset,		/* X_add_number.  */
23159243Sobrien	 int pcrel,			/* TRUE if PC-relative relocation.  */
23259243Sobrien	 RELOC_ENUM r_type		/* Relocation type.  */)
23359243Sobrien{
23459243Sobrien  return fix_new_internal (frag, where, size, add_symbol,
23559243Sobrien			   (symbolS *) NULL, offset, pcrel, r_type);
23659243Sobrien}
23759243Sobrien
23859243Sobrien/* Create a fixup for an expression.  Currently we only support fixups
23959243Sobrien   for difference expressions.  That is itself more than most object
24059243Sobrien   file formats support anyhow.  */
24159243Sobrien
24259243SobrienfixS *
24359243Sobrienfix_new_exp (fragS *frag,		/* Which frag?  */
24459243Sobrien	     int where,			/* Where in that frag?  */
24559243Sobrien	     int size,			/* 1, 2, or 4 usually.  */
24659243Sobrien	     expressionS *exp,		/* Expression.  */
24759243Sobrien	     int pcrel,			/* TRUE if PC-relative relocation.  */
24859243Sobrien	     RELOC_ENUM r_type		/* Relocation type.  */)
24959243Sobrien{
25059243Sobrien  symbolS *add = NULL;
25159243Sobrien  symbolS *sub = NULL;
25259243Sobrien  offsetT off = 0;
25359243Sobrien
25459243Sobrien  switch (exp->X_op)
25559243Sobrien    {
25659243Sobrien    case O_absent:
25759243Sobrien      break;
25859243Sobrien
25959243Sobrien    case O_register:
26059243Sobrien      as_bad (_("register value used as expression"));
26159243Sobrien      break;
26259243Sobrien
26359243Sobrien    case O_add:
26459243Sobrien      /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
26559243Sobrien	 the difference expression cannot immediately be reduced.  */
26659243Sobrien      {
26759243Sobrien	symbolS *stmp = make_expr_symbol (exp);
26859243Sobrien
26959243Sobrien	exp->X_op = O_symbol;
27059243Sobrien	exp->X_op_symbol = 0;
27159243Sobrien	exp->X_add_symbol = stmp;
27259243Sobrien	exp->X_add_number = 0;
27359243Sobrien
27459243Sobrien	return fix_new_exp (frag, where, size, exp, pcrel, r_type);
27559243Sobrien      }
27659243Sobrien
27759243Sobrien    case O_symbol_rva:
27859243Sobrien      add = exp->X_add_symbol;
27959243Sobrien      off = exp->X_add_number;
28059243Sobrien      r_type = BFD_RELOC_RVA;
28159243Sobrien      break;
28259243Sobrien
28359243Sobrien    case O_uminus:
28459243Sobrien      sub = exp->X_add_symbol;
28559243Sobrien      off = exp->X_add_number;
28659243Sobrien      break;
28759243Sobrien
28859243Sobrien    case O_subtract:
28959243Sobrien      sub = exp->X_op_symbol;
29059243Sobrien      /* Fall through.  */
29159243Sobrien    case O_symbol:
29259243Sobrien      add = exp->X_add_symbol;
29359243Sobrien      /* Fall through.  */
29459243Sobrien    case O_constant:
29559243Sobrien      off = exp->X_add_number;
29659243Sobrien      break;
29759243Sobrien
29859243Sobrien    default:
29959243Sobrien      add = make_expr_symbol (exp);
30059243Sobrien      break;
30159243Sobrien    }
30259243Sobrien
30359243Sobrien  return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
30459243Sobrien}
30559243Sobrien
30659243Sobrien/* Generic function to determine whether a fixup requires a relocation.  */
30759243Sobrienint
30859243Sobriengeneric_force_reloc (fixS *fix)
30959243Sobrien{
31059243Sobrien  if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
31159243Sobrien      || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
31259243Sobrien    return 1;
31359243Sobrien
31459243Sobrien  if (fix->fx_addsy == NULL)
31559243Sobrien    return 0;
31659243Sobrien
31759243Sobrien  return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
31859243Sobrien}
31959243Sobrien
32059243Sobrien/* Append a string onto another string, bumping the pointer along.  */
32159243Sobrienvoid
32259243Sobrienappend (char **charPP, char *fromP, unsigned long length)
32359243Sobrien{
32459243Sobrien  /* Don't trust memcpy() of 0 chars.  */
32559243Sobrien  if (length == 0)
32659243Sobrien    return;
32759243Sobrien
32859243Sobrien  memcpy (*charPP, fromP, length);
32959243Sobrien  *charPP += length;
33059243Sobrien}
33159243Sobrien
33259243Sobrien/* This routine records the largest alignment seen for each segment.
33359243Sobrien   If the beginning of the segment is aligned on the worst-case
33459243Sobrien   boundary, all of the other alignments within it will work.  At
33559243Sobrien   least one object format really uses this info.  */
33659243Sobrien
33759243Sobrienvoid
33859243Sobrienrecord_alignment (/* Segment to which alignment pertains.  */
33959243Sobrien		  segT seg,
34059243Sobrien		  /* Alignment, as a power of 2 (e.g., 1 => 2-byte
34159243Sobrien		     boundary, 2 => 4-byte boundary, etc.)  */
34259243Sobrien		  int align)
34359243Sobrien{
34459243Sobrien  if (seg == absolute_section)
34559243Sobrien    return;
34659243Sobrien
34759243Sobrien  if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
34859243Sobrien    (void) bfd_set_section_alignment (stdoutput, seg, align);
34959243Sobrien}
35059243Sobrien
35159243Sobrienint
35259243Sobrienget_recorded_alignment (segT seg)
35359243Sobrien{
35459243Sobrien  if (seg == absolute_section)
35559243Sobrien    return 0;
35659243Sobrien
35759243Sobrien  return bfd_get_section_alignment (stdoutput, seg);
35859243Sobrien}
35959243Sobrien
36059243Sobrien/* Reset the section indices after removing the gas created sections.  */
36159243Sobrien
36259243Sobrienstatic void
36359243Sobrienrenumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
36459243Sobrien{
36559243Sobrien  int *countp = (int *) countparg;
36659243Sobrien
36759243Sobrien  sec->index = *countp;
36859243Sobrien  ++*countp;
36959243Sobrien}
37059243Sobrien
37159243Sobrienstatic fragS *
37259243Sobrienchain_frchains_together_1 (segT section, struct frchain *frchp)
37359243Sobrien{
37459243Sobrien  fragS dummy, *prev_frag = &dummy;
37559243Sobrien  fixS fix_dummy, *prev_fix = &fix_dummy;
37659243Sobrien
37759243Sobrien  for (; frchp; frchp = frchp->frch_next)
37859243Sobrien    {
37959243Sobrien      prev_frag->fr_next = frchp->frch_root;
38059243Sobrien      prev_frag = frchp->frch_last;
38159243Sobrien      assert (prev_frag->fr_type != 0);
38259243Sobrien      if (frchp->fix_root != (fixS *) NULL)
38359243Sobrien	{
38459243Sobrien	  if (seg_info (section)->fix_root == (fixS *) NULL)
38559243Sobrien	    seg_info (section)->fix_root = frchp->fix_root;
38659243Sobrien	  prev_fix->fx_next = frchp->fix_root;
38759243Sobrien	  seg_info (section)->fix_tail = frchp->fix_tail;
38859243Sobrien	  prev_fix = frchp->fix_tail;
38959243Sobrien	}
39059243Sobrien    }
39159243Sobrien  assert (prev_frag->fr_type != 0);
39259243Sobrien  assert (prev_frag != &dummy);
39359243Sobrien  prev_frag->fr_next = 0;
39459243Sobrien  return prev_frag;
39559243Sobrien}
39659243Sobrien
39759243Sobrienstatic void
39859243Sobrienchain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
39959243Sobrien			 segT section,
40059243Sobrien			 void *xxx ATTRIBUTE_UNUSED)
40159243Sobrien{
40259243Sobrien  segment_info_type *info;
40359243Sobrien
40459243Sobrien  /* BFD may have introduced its own sections without using
40559243Sobrien     subseg_new, so it is possible that seg_info is NULL.  */
40659243Sobrien  info = seg_info (section);
40759243Sobrien  if (info != (segment_info_type *) NULL)
40859243Sobrien    info->frchainP->frch_last
40959243Sobrien      = chain_frchains_together_1 (section, info->frchainP);
41059243Sobrien
41159243Sobrien  /* Now that we've chained the frags together, we must add new fixups
41259243Sobrien     to the segment, not to the frag chain.  */
41359243Sobrien  frags_chained = 1;
41459243Sobrien}
41559243Sobrien
41659243Sobrienstatic void
41759243Sobriencvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
41859243Sobrien{
41959243Sobrien  switch (fragP->fr_type)
42059243Sobrien    {
42159243Sobrien    case rs_align:
42259243Sobrien    case rs_align_code:
42359243Sobrien    case rs_align_test:
42459243Sobrien    case rs_org:
42559243Sobrien    case rs_space:
42659243Sobrien#ifdef HANDLE_ALIGN
42759243Sobrien      HANDLE_ALIGN (fragP);
42859243Sobrien#endif
42959243Sobrien      know (fragP->fr_next != NULL);
43059243Sobrien      fragP->fr_offset = (fragP->fr_next->fr_address
43159243Sobrien			  - fragP->fr_address
43259243Sobrien			  - fragP->fr_fix) / fragP->fr_var;
43359243Sobrien      if (fragP->fr_offset < 0)
43459243Sobrien	{
43559243Sobrien	  as_bad_where (fragP->fr_file, fragP->fr_line,
43659243Sobrien			_("attempt to .org/.space backwards? (%ld)"),
43759243Sobrien			(long) fragP->fr_offset);
43859243Sobrien	  fragP->fr_offset = 0;
43959243Sobrien	}
44059243Sobrien      fragP->fr_type = rs_fill;
44159243Sobrien      break;
44259243Sobrien
44359243Sobrien    case rs_fill:
44459243Sobrien      break;
44559243Sobrien
44659243Sobrien    case rs_leb128:
44759243Sobrien      {
44859243Sobrien	valueT value = S_GET_VALUE (fragP->fr_symbol);
44959243Sobrien	int size;
45059243Sobrien
45159243Sobrien	size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
45259243Sobrien			      fragP->fr_subtype);
45359243Sobrien
45459243Sobrien	fragP->fr_fix += size;
45559243Sobrien	fragP->fr_type = rs_fill;
45659243Sobrien	fragP->fr_var = 0;
45759243Sobrien	fragP->fr_offset = 0;
45859243Sobrien	fragP->fr_symbol = NULL;
45959243Sobrien      }
46059243Sobrien      break;
46159243Sobrien
46259243Sobrien    case rs_cfa:
46359243Sobrien      eh_frame_convert_frag (fragP);
46459243Sobrien      break;
46559243Sobrien
46659243Sobrien    case rs_dwarf2dbg:
46759243Sobrien      dwarf2dbg_convert_frag (fragP);
46859243Sobrien      break;
46959243Sobrien
47059243Sobrien    case rs_machine_dependent:
47159243Sobrien      md_convert_frag (stdoutput, sec, fragP);
47259243Sobrien
47359243Sobrien      assert (fragP->fr_next == NULL
47459243Sobrien	      || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
47559243Sobrien		  == fragP->fr_fix));
47659243Sobrien
47759243Sobrien      /* After md_convert_frag, we make the frag into a ".space 0".
47859243Sobrien	 md_convert_frag() should set up any fixSs and constants
47959243Sobrien	 required.  */
48059243Sobrien      frag_wane (fragP);
48159243Sobrien      break;
48259243Sobrien
48359243Sobrien#ifndef WORKING_DOT_WORD
48459243Sobrien    case rs_broken_word:
48559243Sobrien      {
48659243Sobrien	struct broken_word *lie;
48759243Sobrien
48859243Sobrien	if (fragP->fr_subtype)
48959243Sobrien	  {
49059243Sobrien	    fragP->fr_fix += md_short_jump_size;
49159243Sobrien	    for (lie = (struct broken_word *) (fragP->fr_symbol);
49259243Sobrien		 lie && lie->dispfrag == fragP;
49359243Sobrien		 lie = lie->next_broken_word)
49459243Sobrien	      if (lie->added == 1)
49559243Sobrien		fragP->fr_fix += md_long_jump_size;
49659243Sobrien	  }
49759243Sobrien	frag_wane (fragP);
49859243Sobrien      }
49959243Sobrien      break;
50059243Sobrien#endif
50159243Sobrien
50259243Sobrien    default:
50359243Sobrien      BAD_CASE (fragP->fr_type);
50459243Sobrien      break;
50559243Sobrien    }
50659243Sobrien#ifdef md_frag_check
50759243Sobrien  md_frag_check (fragP);
50859243Sobrien#endif
50959243Sobrien}
51059243Sobrien
51159243Sobrienstruct relax_seg_info
51259243Sobrien{
51359243Sobrien  int pass;
51459243Sobrien  int changed;
51559243Sobrien};
51659243Sobrien
51759243Sobrienstatic void
51859243Sobrienrelax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
51959243Sobrien{
52059243Sobrien  segment_info_type *seginfo = seg_info (sec);
52159243Sobrien  struct relax_seg_info *info = (struct relax_seg_info *) xxx;
52259243Sobrien
52359243Sobrien  if (seginfo && seginfo->frchainP
52459243Sobrien      && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
52559243Sobrien    info->changed = 1;
52659243Sobrien}
52759243Sobrien
52859243Sobrienstatic void
52959243Sobriensize_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
53059243Sobrien{
53159243Sobrien  flagword flags;
53259243Sobrien  fragS *fragp;
53359243Sobrien  segment_info_type *seginfo;
53459243Sobrien  int x;
53559243Sobrien  valueT size, newsize;
53659243Sobrien
53759243Sobrien  subseg_change (sec, 0);
53859243Sobrien
53959243Sobrien  seginfo = seg_info (sec);
54059243Sobrien  if (seginfo && seginfo->frchainP)
54159243Sobrien    {
54259243Sobrien      for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
54359243Sobrien	cvt_frag_to_fill (sec, fragp);
54459243Sobrien      for (fragp = seginfo->frchainP->frch_root;
54559243Sobrien	   fragp->fr_next;
54659243Sobrien	   fragp = fragp->fr_next)
54759243Sobrien	/* Walk to last elt.  */
54859243Sobrien	;
54959243Sobrien      size = fragp->fr_address + fragp->fr_fix;
55059243Sobrien    }
55159243Sobrien  else
55259243Sobrien    size = 0;
55359243Sobrien
55459243Sobrien  flags = bfd_get_section_flags (abfd, sec);
55559243Sobrien
55659243Sobrien  if (size > 0 && ! seginfo->bss)
55759243Sobrien    flags |= SEC_HAS_CONTENTS;
55859243Sobrien
55959243Sobrien  flags &= ~SEC_RELOC;
56059243Sobrien  x = bfd_set_section_flags (abfd, sec, flags);
56159243Sobrien  assert (x);
56259243Sobrien
56359243Sobrien  newsize = md_section_align (sec, size);
56459243Sobrien  x = bfd_set_section_size (abfd, sec, newsize);
56559243Sobrien  assert (x);
56659243Sobrien
56759243Sobrien  /* If the size had to be rounded up, add some padding in the last
56859243Sobrien     non-empty frag.  */
56959243Sobrien  assert (newsize >= size);
57059243Sobrien  if (size != newsize)
57159243Sobrien    {
57259243Sobrien      fragS *last = seginfo->frchainP->frch_last;
57359243Sobrien      fragp = seginfo->frchainP->frch_root;
57459243Sobrien      while (fragp->fr_next != last)
57559243Sobrien	fragp = fragp->fr_next;
57659243Sobrien      last->fr_address = size;
57759243Sobrien      if ((newsize - size) % fragp->fr_var == 0)
57859243Sobrien	fragp->fr_offset += (newsize - size) / fragp->fr_var;
57959243Sobrien      else
58059243Sobrien	/* If we hit this abort, it's likely due to subsegs_finish not
58159243Sobrien	   providing sufficient alignment on the last frag, and the
58259243Sobrien	   machine dependent code using alignment frags with fr_var
58359243Sobrien	   greater than 1.  */
58459243Sobrien	abort ();
58559243Sobrien    }
58659243Sobrien
58759243Sobrien#ifdef tc_frob_section
58859243Sobrien  tc_frob_section (sec);
58959243Sobrien#endif
59059243Sobrien#ifdef obj_frob_section
59159243Sobrien  obj_frob_section (sec);
59259243Sobrien#endif
59359243Sobrien}
59459243Sobrien
59559243Sobrien#ifdef DEBUG2
59659243Sobrienstatic void
59759243Sobriendump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
59859243Sobrien{
59959243Sobrien  segment_info_type *seginfo = seg_info (sec);
60059243Sobrien  fixS *fixp = seginfo->fix_root;
60159243Sobrien
60259243Sobrien  if (!fixp)
60359243Sobrien    return;
60459243Sobrien
60559243Sobrien  fprintf (stream, "sec %s relocs:\n", sec->name);
60659243Sobrien  while (fixp)
60759243Sobrien    {
60859243Sobrien      symbolS *s = fixp->fx_addsy;
60959243Sobrien
61059243Sobrien      fprintf (stream, "  %08lx: type %d ", (unsigned long) fixp,
61159243Sobrien	       (int) fixp->fx_r_type);
61259243Sobrien      if (s == NULL)
61359243Sobrien	fprintf (stream, "no sym\n");
61459243Sobrien      else
61559243Sobrien	{
61659243Sobrien	  print_symbol_value_1 (stream, s);
61759243Sobrien	  fprintf (stream, "\n");
61859243Sobrien	}
61959243Sobrien      fixp = fixp->fx_next;
62059243Sobrien    }
62159243Sobrien}
62259243Sobrien#else
62359243Sobrien#define dump_section_relocs(ABFD,SEC,STREAM)	((void) 0)
62459243Sobrien#endif
62559243Sobrien
62659243Sobrien#ifndef EMIT_SECTION_SYMBOLS
62759243Sobrien#define EMIT_SECTION_SYMBOLS 1
62859243Sobrien#endif
62959243Sobrien
63059243Sobrien/* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
63159243Sobrien   and check for validity.  Convert RELOC_LIST from using U.A fields
63259243Sobrien   to U.B fields.  */
63359243Sobrienstatic void
63459243Sobrienresolve_reloc_expr_symbols (void)
63559243Sobrien{
63659243Sobrien  struct reloc_list *r;
63759243Sobrien
63859243Sobrien  for (r = reloc_list; r; r = r->next)
63959243Sobrien    {
64059243Sobrien      expressionS *symval;
64159243Sobrien      symbolS *sym;
64259243Sobrien      bfd_vma offset, addend;
64359243Sobrien      asection *sec;
64459243Sobrien      reloc_howto_type *howto;
64559243Sobrien
64659243Sobrien      resolve_symbol_value (r->u.a.offset_sym);
64759243Sobrien      symval = symbol_get_value_expression (r->u.a.offset_sym);
64859243Sobrien
64959243Sobrien      offset = 0;
65059243Sobrien      sym = NULL;
65159243Sobrien      if (symval->X_op == O_constant)
65259243Sobrien	sym = r->u.a.offset_sym;
65359243Sobrien      else if (symval->X_op == O_symbol)
65459243Sobrien	{
65559243Sobrien	  sym = symval->X_add_symbol;
65659243Sobrien	  offset = symval->X_add_number;
65759243Sobrien	  symval = symbol_get_value_expression (symval->X_add_symbol);
65859243Sobrien	}
65959243Sobrien      if (sym == NULL
66059243Sobrien	  || symval->X_op != O_constant
66159243Sobrien	  || (sec = S_GET_SEGMENT (sym)) == NULL
66259243Sobrien	  || !SEG_NORMAL (sec))
66359243Sobrien	{
66459243Sobrien	  as_bad_where (r->file, r->line, _("invalid offset expression"));
66559243Sobrien	  sec = NULL;
66659243Sobrien	}
66759243Sobrien      else
66859243Sobrien	offset += S_GET_VALUE (sym);
66959243Sobrien
67059243Sobrien      sym = NULL;
67159243Sobrien      addend = r->u.a.addend;
67259243Sobrien      if (r->u.a.sym != NULL)
67359243Sobrien	{
67459243Sobrien	  resolve_symbol_value (r->u.a.sym);
67559243Sobrien	  symval = symbol_get_value_expression (r->u.a.sym);
67659243Sobrien	  if (symval->X_op == O_constant)
67759243Sobrien	    sym = r->u.a.sym;
67859243Sobrien	  else if (symval->X_op == O_symbol)
67959243Sobrien	    {
68059243Sobrien	      sym = symval->X_add_symbol;
68159243Sobrien	      addend += symval->X_add_number;
68259243Sobrien	      symval = symbol_get_value_expression (symval->X_add_symbol);
68359243Sobrien	    }
68459243Sobrien	  if (symval->X_op != O_constant)
68559243Sobrien	    {
68659243Sobrien	      as_bad_where (r->file, r->line, _("invalid reloc expression"));
68759243Sobrien	      sec = NULL;
68859243Sobrien	    }
68959243Sobrien	  else if (sym != NULL)
69059243Sobrien	    symbol_mark_used_in_reloc (sym);
69159243Sobrien	}
69259243Sobrien      if (sym == NULL)
69359243Sobrien	{
69459243Sobrien	  if (abs_section_sym == NULL)
69559243Sobrien	    abs_section_sym = section_symbol (absolute_section);
69659243Sobrien	  sym = abs_section_sym;
69759243Sobrien	}
69859243Sobrien
69959243Sobrien      howto = r->u.a.howto;
70059243Sobrien
70159243Sobrien      r->u.b.sec = sec;
70259243Sobrien      r->u.b.s = symbol_get_bfdsym (sym);
70359243Sobrien      r->u.b.r.sym_ptr_ptr = &r->u.b.s;
70459243Sobrien      r->u.b.r.address = offset;
70559243Sobrien      r->u.b.r.addend = addend;
70659243Sobrien      r->u.b.r.howto = howto;
70759243Sobrien    }
70859243Sobrien}
70959243Sobrien
71059243Sobrien/* This pass over fixups decides whether symbols can be replaced with
71159243Sobrien   section symbols.  */
71259243Sobrien
71359243Sobrienstatic void
71459243Sobrienadjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
71559243Sobrien		   asection *sec,
71659243Sobrien		   void *xxx ATTRIBUTE_UNUSED)
71759243Sobrien{
71859243Sobrien  segment_info_type *seginfo = seg_info (sec);
71959243Sobrien  fixS *fixp;
72059243Sobrien
72159243Sobrien  if (seginfo == NULL)
72259243Sobrien    return;
72359243Sobrien
72459243Sobrien  dump_section_relocs (abfd, sec, stderr);
72559243Sobrien
72659243Sobrien  for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
72759243Sobrien    if (fixp->fx_done)
72859243Sobrien      /* Ignore it.  */
72959243Sobrien      ;
73059243Sobrien    else if (fixp->fx_addsy)
73159243Sobrien      {
73259243Sobrien	symbolS *sym;
73359243Sobrien	asection *symsec;
73459243Sobrien
73559243Sobrien#ifdef DEBUG5
73659243Sobrien	fprintf (stderr, "\n\nadjusting fixup:\n");
73759243Sobrien	print_fixup (fixp);
73859243Sobrien#endif
73959243Sobrien
74059243Sobrien	sym = fixp->fx_addsy;
74159243Sobrien
74259243Sobrien	/* All symbols should have already been resolved at this
74359243Sobrien	   point.  It is possible to see unresolved expression
74459243Sobrien	   symbols, though, since they are not in the regular symbol
74559243Sobrien	   table.  */
74659243Sobrien	resolve_symbol_value (sym);
74759243Sobrien
74859243Sobrien	if (fixp->fx_subsy != NULL)
74959243Sobrien	  resolve_symbol_value (fixp->fx_subsy);
75059243Sobrien
75159243Sobrien	/* If this symbol is equated to an undefined or common symbol,
75259243Sobrien	   convert the fixup to being against that symbol.  */
75359243Sobrien	while (symbol_equated_reloc_p (sym)
75459243Sobrien	       || S_IS_WEAKREFR (sym))
75559243Sobrien	  {
75659243Sobrien	    symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
75759243Sobrien	    if (sym == newsym)
75859243Sobrien	      break;
75959243Sobrien	    fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
76059243Sobrien	    fixp->fx_addsy = newsym;
76159243Sobrien	    sym = newsym;
76259243Sobrien	  }
76359243Sobrien
76459243Sobrien	if (symbol_mri_common_p (sym))
76559243Sobrien	  {
76659243Sobrien	    fixp->fx_offset += S_GET_VALUE (sym);
76759243Sobrien	    fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
76859243Sobrien	    continue;
76959243Sobrien	  }
77059243Sobrien
77159243Sobrien	/* If the symbol is undefined, common, weak, or global (ELF
77259243Sobrien	   shared libs), we can't replace it with the section symbol.  */
77359243Sobrien	if (S_FORCE_RELOC (fixp->fx_addsy, 1))
77459243Sobrien	  continue;
77559243Sobrien
77659243Sobrien	/* Is there some other (target cpu dependent) reason we can't adjust
77759243Sobrien	   this one?  (E.g. relocations involving function addresses on
77859243Sobrien	   the PA.  */
77959243Sobrien#ifdef tc_fix_adjustable
78059243Sobrien	if (! tc_fix_adjustable (fixp))
78159243Sobrien	  continue;
78259243Sobrien#endif
78359243Sobrien
78459243Sobrien	/* Since we're reducing to section symbols, don't attempt to reduce
78559243Sobrien	   anything that's already using one.  */
78659243Sobrien	if (symbol_section_p (sym))
78759243Sobrien	  continue;
78859243Sobrien
78959243Sobrien	symsec = S_GET_SEGMENT (sym);
79059243Sobrien	if (symsec == NULL)
79159243Sobrien	  abort ();
79259243Sobrien
79359243Sobrien	if (bfd_is_abs_section (symsec))
79459243Sobrien	  {
79559243Sobrien	    /* The fixup_segment routine normally will not use this
79659243Sobrien	       symbol in a relocation.  */
79759243Sobrien	    continue;
79859243Sobrien	  }
79959243Sobrien
80059243Sobrien	/* Don't try to reduce relocs which refer to non-local symbols
80159243Sobrien	   in .linkonce sections.  It can lead to confusion when a
80259243Sobrien	   debugging section refers to a .linkonce section.  I hope
80359243Sobrien	   this will always be correct.  */
80459243Sobrien	if (symsec != sec && ! S_IS_LOCAL (sym))
80559243Sobrien	  {
80659243Sobrien	    if ((symsec->flags & SEC_LINK_ONCE) != 0
80759243Sobrien		|| (IS_ELF
80859243Sobrien		    /* The GNU toolchain uses an extension for ELF: a
80959243Sobrien		       section beginning with the magic string
81059243Sobrien		       .gnu.linkonce is a linkonce section.  */
81159243Sobrien		    && strncmp (segment_name (symsec), ".gnu.linkonce",
81259243Sobrien				sizeof ".gnu.linkonce" - 1) == 0))
81359243Sobrien	      continue;
81459243Sobrien	  }
81559243Sobrien
81659243Sobrien	/* Never adjust a reloc against local symbol in a merge section
81759243Sobrien	   with non-zero addend.  */
81859243Sobrien	if ((symsec->flags & SEC_MERGE) != 0
81959243Sobrien	    && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
82059243Sobrien	  continue;
82159243Sobrien
82259243Sobrien	/* Never adjust a reloc against TLS local symbol.  */
82359243Sobrien	if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
82459243Sobrien	  continue;
82559243Sobrien
82659243Sobrien	/* We refetch the segment when calling section_symbol, rather
82759243Sobrien	   than using symsec, because S_GET_VALUE may wind up changing
82859243Sobrien	   the section when it calls resolve_symbol_value.  */
82959243Sobrien	fixp->fx_offset += S_GET_VALUE (sym);
83059243Sobrien	fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
83159243Sobrien#ifdef DEBUG5
83259243Sobrien	fprintf (stderr, "\nadjusted fixup:\n");
83359243Sobrien	print_fixup (fixp);
83459243Sobrien#endif
83559243Sobrien      }
83659243Sobrien
83759243Sobrien  dump_section_relocs (abfd, sec, stderr);
83859243Sobrien}
83959243Sobrien
84059243Sobrien/* fixup_segment()
84159243Sobrien
84259243Sobrien   Go through all the fixS's in a segment and see which ones can be
84359243Sobrien   handled now.  (These consist of fixS where we have since discovered
84459243Sobrien   the value of a symbol, or the address of the frag involved.)
84559243Sobrien   For each one, call md_apply_fix to put the fix into the frag data.
84659243Sobrien
84759243Sobrien   Result is a count of how many relocation structs will be needed to
84859243Sobrien   handle the remaining fixS's that we couldn't completely handle here.
84959243Sobrien   These will be output later by emit_relocations().  */
85059243Sobrien
85159243Sobrienstatic long
85259243Sobrienfixup_segment (fixS *fixP, segT this_segment)
85359243Sobrien{
85459243Sobrien  long seg_reloc_count = 0;
85559243Sobrien  valueT add_number;
85659243Sobrien  fragS *fragP;
85759243Sobrien  segT add_symbol_segment = absolute_section;
85859243Sobrien
85959243Sobrien  if (fixP != NULL && abs_section_sym == NULL)
86059243Sobrien    abs_section_sym = section_symbol (absolute_section);
86159243Sobrien
86259243Sobrien  /* If the linker is doing the relaxing, we must not do any fixups.
86359243Sobrien
86459243Sobrien     Well, strictly speaking that's not true -- we could do any that
86559243Sobrien     are PC-relative and don't cross regions that could change size.
86659243Sobrien     And for the i960 we might be able to turn callx/callj into bal
86759243Sobrien     anyways in cases where we know the maximum displacement.  */
86859243Sobrien  if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
86959243Sobrien    {
87059243Sobrien      for (; fixP; fixP = fixP->fx_next)
87159243Sobrien	if (!fixP->fx_done)
87259243Sobrien	  {
87359243Sobrien	    if (fixP->fx_addsy == NULL)
87459243Sobrien	      {
87559243Sobrien		/* There was no symbol required by this relocation.
87659243Sobrien		   However, BFD doesn't really handle relocations
87759243Sobrien		   without symbols well. So fake up a local symbol in
87859243Sobrien		   the absolute section.  */
87959243Sobrien		fixP->fx_addsy = abs_section_sym;
88059243Sobrien	      }
88159243Sobrien	    symbol_mark_used_in_reloc (fixP->fx_addsy);
88259243Sobrien	    if (fixP->fx_subsy != NULL)
88359243Sobrien	      symbol_mark_used_in_reloc (fixP->fx_subsy);
88459243Sobrien	    seg_reloc_count++;
88559243Sobrien	  }
88659243Sobrien      TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
88759243Sobrien      return seg_reloc_count;
88859243Sobrien    }
88959243Sobrien
89059243Sobrien  for (; fixP; fixP = fixP->fx_next)
89159243Sobrien    {
89259243Sobrien#ifdef DEBUG5
89359243Sobrien      fprintf (stderr, "\nprocessing fixup:\n");
89459243Sobrien      print_fixup (fixP);
89559243Sobrien#endif
89659243Sobrien
89759243Sobrien      fragP = fixP->fx_frag;
89859243Sobrien      know (fragP);
89959243Sobrien#ifdef TC_VALIDATE_FIX
90059243Sobrien      TC_VALIDATE_FIX (fixP, this_segment, skip);
90159243Sobrien#endif
90259243Sobrien      add_number = fixP->fx_offset;
90359243Sobrien
90459243Sobrien      if (fixP->fx_addsy != NULL)
90559243Sobrien	add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
90659243Sobrien
90759243Sobrien      if (fixP->fx_subsy != NULL)
90859243Sobrien	{
90959243Sobrien	  segT sub_symbol_segment;
91059243Sobrien	  resolve_symbol_value (fixP->fx_subsy);
91159243Sobrien	  sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
91259243Sobrien	  if (fixP->fx_addsy != NULL
91359243Sobrien	      && sub_symbol_segment == add_symbol_segment
91459243Sobrien	      && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
91559243Sobrien	    {
91659243Sobrien	      add_number += S_GET_VALUE (fixP->fx_addsy);
91759243Sobrien	      add_number -= S_GET_VALUE (fixP->fx_subsy);
91859243Sobrien	      fixP->fx_offset = add_number;
91959243Sobrien	      fixP->fx_addsy = NULL;
92059243Sobrien	      fixP->fx_subsy = NULL;
92159243Sobrien#ifdef TC_M68K
92259243Sobrien	      /* See the comment below about 68k weirdness.  */
92359243Sobrien	      fixP->fx_pcrel = 0;
92459243Sobrien#endif
92559243Sobrien	    }
92659243Sobrien	  else if (sub_symbol_segment == absolute_section
92759243Sobrien		   && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
92859243Sobrien	    {
92959243Sobrien	      add_number -= S_GET_VALUE (fixP->fx_subsy);
93059243Sobrien	      fixP->fx_offset = add_number;
93159243Sobrien	      fixP->fx_subsy = NULL;
93259243Sobrien	    }
93359243Sobrien	  else if (sub_symbol_segment == this_segment
93459243Sobrien		   && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
93559243Sobrien	    {
93659243Sobrien	      add_number -= S_GET_VALUE (fixP->fx_subsy);
93759243Sobrien	      fixP->fx_offset = (add_number + fixP->fx_dot_value
93859243Sobrien				 + fixP->fx_frag->fr_address);
93959243Sobrien
94059243Sobrien	      /* Make it pc-relative.  If the back-end code has not
94159243Sobrien		 selected a pc-relative reloc, cancel the adjustment
94259243Sobrien		 we do later on all pc-relative relocs.  */
94359243Sobrien	      if (0
94459243Sobrien#ifdef TC_M68K
94559243Sobrien		  /* Do this for m68k even if it's already described
94659243Sobrien		     as pc-relative.  On the m68k, an operand of
94759243Sobrien		     "pc@(foo-.-2)" should address "foo" in a
94859243Sobrien		     pc-relative mode.  */
94959243Sobrien		  || 1
95059243Sobrien#endif
95159243Sobrien		  || !fixP->fx_pcrel)
95259243Sobrien		add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
95359243Sobrien	      fixP->fx_subsy = NULL;
95459243Sobrien	      fixP->fx_pcrel = 1;
95559243Sobrien	    }
95659243Sobrien	  else if (!TC_VALIDATE_FIX_SUB (fixP))
95759243Sobrien	    {
95859243Sobrien	      as_bad_where (fixP->fx_file, fixP->fx_line,
95959243Sobrien			    _("can't resolve `%s' {%s section} - `%s' {%s section}"),
96059243Sobrien			    fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
96159243Sobrien			    segment_name (add_symbol_segment),
96259243Sobrien			    S_GET_NAME (fixP->fx_subsy),
96359243Sobrien			    segment_name (sub_symbol_segment));
96459243Sobrien	    }
96559243Sobrien	}
96659243Sobrien
96759243Sobrien      if (fixP->fx_addsy)
96859243Sobrien	{
96959243Sobrien	  if (add_symbol_segment == this_segment
97059243Sobrien	      && !TC_FORCE_RELOCATION_LOCAL (fixP))
97159243Sobrien	    {
97259243Sobrien	      /* This fixup was made when the symbol's segment was
97359243Sobrien		 SEG_UNKNOWN, but it is now in the local segment.
97459243Sobrien		 So we know how to do the address without relocation.  */
97559243Sobrien	      add_number += S_GET_VALUE (fixP->fx_addsy);
97659243Sobrien	      fixP->fx_offset = add_number;
97759243Sobrien	      if (fixP->fx_pcrel)
97859243Sobrien		add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
97959243Sobrien	      fixP->fx_addsy = NULL;
98059243Sobrien	      fixP->fx_pcrel = 0;
98159243Sobrien	    }
98259243Sobrien	  else if (add_symbol_segment == absolute_section
98359243Sobrien		   && !TC_FORCE_RELOCATION_ABS (fixP))
98459243Sobrien	    {
98559243Sobrien	      add_number += S_GET_VALUE (fixP->fx_addsy);
98659243Sobrien	      fixP->fx_offset = add_number;
98759243Sobrien	      fixP->fx_addsy = NULL;
98859243Sobrien	    }
98959243Sobrien	  else if (add_symbol_segment != undefined_section
99059243Sobrien		   && ! bfd_is_com_section (add_symbol_segment)
99159243Sobrien		   && MD_APPLY_SYM_VALUE (fixP))
99259243Sobrien	    add_number += S_GET_VALUE (fixP->fx_addsy);
99359243Sobrien	}
99459243Sobrien
99559243Sobrien      if (fixP->fx_pcrel)
99659243Sobrien	{
99759243Sobrien	  add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
99859243Sobrien	  if (!fixP->fx_done && fixP->fx_addsy == NULL)
99959243Sobrien	    {
100059243Sobrien	      /* There was no symbol required by this relocation.
100159243Sobrien		 However, BFD doesn't really handle relocations
100259243Sobrien		 without symbols well. So fake up a local symbol in
100359243Sobrien		 the absolute section.  */
100459243Sobrien	      fixP->fx_addsy = abs_section_sym;
100559243Sobrien	    }
100659243Sobrien	}
100759243Sobrien
100859243Sobrien      if (!fixP->fx_done)
100959243Sobrien	md_apply_fix (fixP, &add_number, this_segment);
101059243Sobrien
101159243Sobrien      if (!fixP->fx_done)
101259243Sobrien	{
101359243Sobrien	  ++seg_reloc_count;
101459243Sobrien	  if (fixP->fx_addsy == NULL)
101559243Sobrien	    fixP->fx_addsy = abs_section_sym;
101659243Sobrien	  symbol_mark_used_in_reloc (fixP->fx_addsy);
101759243Sobrien	  if (fixP->fx_subsy != NULL)
101859243Sobrien	    symbol_mark_used_in_reloc (fixP->fx_subsy);
101959243Sobrien	}
102059243Sobrien
102159243Sobrien      if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
102259243Sobrien	{
102359243Sobrien	  if (fixP->fx_size < sizeof (valueT))
102459243Sobrien	    {
102559243Sobrien	      valueT mask;
102659243Sobrien
102759243Sobrien	      mask = 0;
102859243Sobrien	      mask--;		/* Set all bits to one.  */
102959243Sobrien	      mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
103059243Sobrien	      if ((add_number & mask) != 0 && (add_number & mask) != mask)
103159243Sobrien		{
103259243Sobrien		  char buf[50], buf2[50];
103359243Sobrien		  sprint_value (buf, fragP->fr_address + fixP->fx_where);
103459243Sobrien		  if (add_number > 1000)
103559243Sobrien		    sprint_value (buf2, add_number);
103659243Sobrien		  else
103759243Sobrien		    sprintf (buf2, "%ld", (long) add_number);
103859243Sobrien		  as_bad_where (fixP->fx_file, fixP->fx_line,
103959243Sobrien				_("value of %s too large for field of %d bytes at %s"),
104059243Sobrien				buf2, fixP->fx_size, buf);
104159243Sobrien		} /* Generic error checking.  */
104259243Sobrien	    }
104359243Sobrien#ifdef WARN_SIGNED_OVERFLOW_WORD
104459243Sobrien	  /* Warn if a .word value is too large when treated as a signed
104559243Sobrien	     number.  We already know it is not too negative.  This is to
104659243Sobrien	     catch over-large switches generated by gcc on the 68k.  */
104759243Sobrien	  if (!flag_signed_overflow_ok
104859243Sobrien	      && fixP->fx_size == 2
104959243Sobrien	      && add_number > 0x7fff)
105059243Sobrien	    as_bad_where (fixP->fx_file, fixP->fx_line,
105159243Sobrien			  _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
105259243Sobrien			  (long) add_number,
105359243Sobrien			  (long) (fragP->fr_address + fixP->fx_where));
105459243Sobrien#endif
105559243Sobrien	}			/* Not a bit fix.  */
105659243Sobrien
105759243Sobrien#ifdef TC_VALIDATE_FIX
105859243Sobrien    skip:  ATTRIBUTE_UNUSED_LABEL
105959243Sobrien      ;
106059243Sobrien#endif
106159243Sobrien#ifdef DEBUG5
106259243Sobrien      fprintf (stderr, "result:\n");
106359243Sobrien      print_fixup (fixP);
106459243Sobrien#endif
106559243Sobrien    }				/* For each fixS in this segment.  */
106659243Sobrien
106759243Sobrien  TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
106859243Sobrien  return seg_reloc_count;
106959243Sobrien}
107059243Sobrien
107159243Sobrienstatic void
107259243Sobrienfix_segment (bfd *abfd ATTRIBUTE_UNUSED,
107359243Sobrien	     asection *sec,
107459243Sobrien	     void *xxx ATTRIBUTE_UNUSED)
107559243Sobrien{
107659243Sobrien  segment_info_type *seginfo = seg_info (sec);
107759243Sobrien
107859243Sobrien  fixup_segment (seginfo->fix_root, sec);
107959243Sobrien}
108059243Sobrien
108159243Sobrienstatic void
108259243Sobrieninstall_reloc (asection *sec, arelent *reloc, fragS *fragp,
108359243Sobrien	       char *file, unsigned int line)
108459243Sobrien{
108559243Sobrien  char *err;
108659243Sobrien  bfd_reloc_status_type s;
108759243Sobrien
108859243Sobrien  s = bfd_install_relocation (stdoutput, reloc,
108959243Sobrien			      fragp->fr_literal, fragp->fr_address,
109059243Sobrien			      sec, &err);
109159243Sobrien  switch (s)
109259243Sobrien    {
109359243Sobrien    case bfd_reloc_ok:
109459243Sobrien      break;
109559243Sobrien    case bfd_reloc_overflow:
109659243Sobrien      as_bad_where (file, line, _("relocation overflow"));
109759243Sobrien      break;
109859243Sobrien    case bfd_reloc_outofrange:
109959243Sobrien      as_bad_where (file, line, _("relocation out of range"));
110059243Sobrien      break;
110159243Sobrien    default:
110259243Sobrien      as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
110359243Sobrien		file, line, s);
110459243Sobrien    }
110559243Sobrien}
110659243Sobrien
110759243Sobrienstatic void
110859243Sobrienwrite_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
110959243Sobrien{
111059243Sobrien  segment_info_type *seginfo = seg_info (sec);
111159243Sobrien  unsigned int i;
111259243Sobrien  unsigned int n;
111359243Sobrien  struct reloc_list *my_reloc_list, **rp, *r;
111459243Sobrien  arelent **relocs;
111559243Sobrien  fixS *fixp;
111659243Sobrien
111759243Sobrien  /* If seginfo is NULL, we did not create this section; don't do
111859243Sobrien     anything with it.  */
111959243Sobrien  if (seginfo == NULL)
112059243Sobrien    return;
112159243Sobrien
112259243Sobrien  n = 0;
112359243Sobrien  for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
112459243Sobrien    if (!fixp->fx_done)
112559243Sobrien      n++;
112659243Sobrien
112759243Sobrien#ifdef RELOC_EXPANSION_POSSIBLE
112859243Sobrien  n *= MAX_RELOC_EXPANSION;
112959243Sobrien#endif
113059243Sobrien
113159243Sobrien  /* Extract relocs for this section from reloc_list.  */
113259243Sobrien  rp = &reloc_list;
113359243Sobrien  my_reloc_list = NULL;
113459243Sobrien  while ((r = *rp) != NULL)
113559243Sobrien    {
113659243Sobrien      if (r->u.b.sec == sec)
113759243Sobrien	{
113859243Sobrien	  *rp = r->next;
113959243Sobrien	  r->next = my_reloc_list;
114059243Sobrien	  my_reloc_list = r;
114159243Sobrien	  n++;
114259243Sobrien	}
114359243Sobrien      else
114459243Sobrien	rp = &r->next;
114559243Sobrien    }
114659243Sobrien
114759243Sobrien  relocs = xcalloc (n, sizeof (arelent *));
114859243Sobrien
114959243Sobrien  i = 0;
115059243Sobrien  for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
115159243Sobrien    {
115259243Sobrien      int j;
115359243Sobrien      int fx_size, slack;
115459243Sobrien      offsetT loc;
115559243Sobrien
115659243Sobrien      if (fixp->fx_done)
115759243Sobrien	continue;
115859243Sobrien
115959243Sobrien      fx_size = fixp->fx_size;
116059243Sobrien      slack = TC_FX_SIZE_SLACK (fixp);
116159243Sobrien      if (slack > 0)
116259243Sobrien	fx_size = fx_size > slack ? fx_size - slack : 0;
116359243Sobrien      loc = fixp->fx_where + fx_size;
116459243Sobrien      if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
116559243Sobrien	as_bad_where (fixp->fx_file, fixp->fx_line,
116659243Sobrien		      _("internal error: fixup not contained within frag"));
116759243Sobrien
116859243Sobrien#ifndef RELOC_EXPANSION_POSSIBLE
116959243Sobrien      {
117059243Sobrien	arelent *reloc = tc_gen_reloc (sec, fixp);
117159243Sobrien
117259243Sobrien	if (!reloc)
117359243Sobrien	  continue;
117459243Sobrien	relocs[i++] = reloc;
117559243Sobrien	j = 1;
117659243Sobrien      }
117759243Sobrien#else
117859243Sobrien      {
117959243Sobrien	arelent **reloc = tc_gen_reloc (sec, fixp);
118059243Sobrien
118159243Sobrien	for (j = 0; reloc[j]; j++)
118259243Sobrien	  relocs[i++] = reloc[j];
118359243Sobrien      }
118459243Sobrien#endif
118559243Sobrien
118659243Sobrien      for ( ; j != 0; --j)
118759243Sobrien	install_reloc (sec, relocs[i - j], fixp->fx_frag,
118859243Sobrien		       fixp->fx_file, fixp->fx_line);
118959243Sobrien    }
119059243Sobrien  n = i;
119159243Sobrien
119259243Sobrien#ifdef DEBUG4
119359243Sobrien  {
119459243Sobrien    unsigned int i, j, nsyms;
119559243Sobrien    asymbol **sympp;
119659243Sobrien    sympp = bfd_get_outsymbols (stdoutput);
119759243Sobrien    nsyms = bfd_get_symcount (stdoutput);
119859243Sobrien    for (i = 0; i < n; i++)
119959243Sobrien      if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
120059243Sobrien	{
120159243Sobrien	  for (j = 0; j < nsyms; j++)
120259243Sobrien	    if (sympp[j] == *relocs[i]->sym_ptr_ptr)
120359243Sobrien	      break;
120459243Sobrien	  if (j == nsyms)
120559243Sobrien	    abort ();
120659243Sobrien	}
120759243Sobrien  }
120859243Sobrien#endif
120959243Sobrien
121059243Sobrien  for (r = my_reloc_list; r != NULL; r = r->next)
121159243Sobrien    {
121259243Sobrien      fragS *f;
121359243Sobrien      for (f = seginfo->frchainP->frch_root; f; f = f->fr_next)
121459243Sobrien	if (f->fr_address <= r->u.b.r.address
121559243Sobrien	    && r->u.b.r.address < f->fr_address + f->fr_fix)
121659243Sobrien	  break;
121759243Sobrien      if (f == NULL)
121859243Sobrien	as_bad_where (r->file, r->line,
121959243Sobrien		      _("reloc not within (fixed part of) section"));
122059243Sobrien      else
122159243Sobrien	{
122259243Sobrien	  relocs[n++] = &r->u.b.r;
122359243Sobrien	  install_reloc (sec, &r->u.b.r, f, r->file, r->line);
122459243Sobrien	}
122559243Sobrien    }
122659243Sobrien
122759243Sobrien  if (n)
122859243Sobrien    {
1229      flagword flags = bfd_get_section_flags (abfd, sec);
1230      flags |= SEC_RELOC;
1231      bfd_set_section_flags (abfd, sec, flags);
1232      bfd_set_reloc (stdoutput, sec, relocs, n);
1233    }
1234
1235#ifdef SET_SECTION_RELOCS
1236  SET_SECTION_RELOCS (sec, relocs, n);
1237#endif
1238
1239#ifdef DEBUG3
1240  {
1241    unsigned int i;
1242    arelent *r;
1243    asymbol *s;
1244    fprintf (stderr, "relocs for sec %s\n", sec->name);
1245    for (i = 0; i < n; i++)
1246      {
1247	r = relocs[i];
1248	s = *r->sym_ptr_ptr;
1249	fprintf (stderr, "  reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1250		 i, r, (unsigned long)r->address, s->name, (unsigned long)r->addend);
1251      }
1252  }
1253#endif
1254}
1255
1256static void
1257write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1258		asection *sec,
1259		void *xxx ATTRIBUTE_UNUSED)
1260{
1261  segment_info_type *seginfo = seg_info (sec);
1262  addressT offset = 0;
1263  fragS *f;
1264
1265  /* Write out the frags.  */
1266  if (seginfo == NULL
1267      || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
1268    return;
1269
1270  for (f = seginfo->frchainP->frch_root;
1271       f;
1272       f = f->fr_next)
1273    {
1274      int x;
1275      addressT fill_size;
1276      char *fill_literal;
1277      offsetT count;
1278
1279      assert (f->fr_type == rs_fill);
1280      if (f->fr_fix)
1281	{
1282	  x = bfd_set_section_contents (stdoutput, sec,
1283					f->fr_literal, (file_ptr) offset,
1284					(bfd_size_type) f->fr_fix);
1285	  if (!x)
1286	    as_fatal (_("can't write %s: %s"), stdoutput->filename,
1287		      bfd_errmsg (bfd_get_error ()));
1288	  offset += f->fr_fix;
1289	}
1290      fill_literal = f->fr_literal + f->fr_fix;
1291      fill_size = f->fr_var;
1292      count = f->fr_offset;
1293      assert (count >= 0);
1294      if (fill_size && count)
1295	{
1296	  char buf[256];
1297	  if (fill_size > sizeof (buf))
1298	    {
1299	      /* Do it the old way. Can this ever happen?  */
1300	      while (count--)
1301		{
1302		  x = bfd_set_section_contents (stdoutput, sec,
1303						fill_literal,
1304						(file_ptr) offset,
1305						(bfd_size_type) fill_size);
1306		  if (!x)
1307		    as_fatal (_("can't write %s: %s"), stdoutput->filename,
1308			      bfd_errmsg (bfd_get_error ()));
1309		  offset += fill_size;
1310		}
1311	    }
1312	  else
1313	    {
1314	      /* Build a buffer full of fill objects and output it as
1315		 often as necessary. This saves on the overhead of
1316		 potentially lots of bfd_set_section_contents calls.  */
1317	      int n_per_buf, i;
1318	      if (fill_size == 1)
1319		{
1320		  n_per_buf = sizeof (buf);
1321		  memset (buf, *fill_literal, n_per_buf);
1322		}
1323	      else
1324		{
1325		  char *bufp;
1326		  n_per_buf = sizeof (buf) / fill_size;
1327		  for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1328		    memcpy (bufp, fill_literal, fill_size);
1329		}
1330	      for (; count > 0; count -= n_per_buf)
1331		{
1332		  n_per_buf = n_per_buf > count ? count : n_per_buf;
1333		  x = bfd_set_section_contents
1334		    (stdoutput, sec, buf, (file_ptr) offset,
1335		     (bfd_size_type) n_per_buf * fill_size);
1336		  if (!x)
1337		    as_fatal (_("cannot write to output file"));
1338		  offset += n_per_buf * fill_size;
1339		}
1340	    }
1341	}
1342    }
1343}
1344
1345static void
1346merge_data_into_text (void)
1347{
1348  seg_info (text_section)->frchainP->frch_last->fr_next =
1349    seg_info (data_section)->frchainP->frch_root;
1350  seg_info (text_section)->frchainP->frch_last =
1351    seg_info (data_section)->frchainP->frch_last;
1352  seg_info (data_section)->frchainP = 0;
1353}
1354
1355static void
1356set_symtab (void)
1357{
1358  int nsyms;
1359  asymbol **asympp;
1360  symbolS *symp;
1361  bfd_boolean result;
1362
1363  /* Count symbols.  We can't rely on a count made by the loop in
1364     write_object_file, because *_frob_file may add a new symbol or
1365     two.  */
1366  nsyms = 0;
1367  for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1368    nsyms++;
1369
1370  if (nsyms)
1371    {
1372      int i;
1373      bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
1374
1375      asympp = bfd_alloc (stdoutput, amt);
1376      symp = symbol_rootP;
1377      for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1378	{
1379	  asympp[i] = symbol_get_bfdsym (symp);
1380	  symbol_mark_written (symp);
1381	}
1382    }
1383  else
1384    asympp = 0;
1385  result = bfd_set_symtab (stdoutput, asympp, nsyms);
1386  assert (result);
1387  symbol_table_frozen = 1;
1388}
1389
1390/* Finish the subsegments.  After every sub-segment, we fake an
1391   ".align ...".  This conforms to BSD4.2 brane-damage.  We then fake
1392   ".fill 0" because that is the kind of frag that requires least
1393   thought.  ".align" frags like to have a following frag since that
1394   makes calculating their intended length trivial.  */
1395
1396#ifndef SUB_SEGMENT_ALIGN
1397#ifdef HANDLE_ALIGN
1398/* The last subsegment gets an alignment corresponding to the alignment
1399   of the section.  This allows proper nop-filling at the end of
1400   code-bearing sections.  */
1401#define SUB_SEGMENT_ALIGN(SEG, FRCHAIN)					\
1402  (!(FRCHAIN)->frch_next ? get_recorded_alignment (SEG) : 0)
1403#else
1404#define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1405#endif
1406#endif
1407
1408void
1409subsegs_finish (void)
1410{
1411  struct frchain *frchainP;
1412  asection *s;
1413
1414  for (s = stdoutput->sections; s; s = s->next)
1415    {
1416      segment_info_type *seginfo = seg_info (s);
1417      if (!seginfo)
1418	continue;
1419
1420      for (frchainP = seginfo->frchainP;
1421	   frchainP != NULL;
1422	   frchainP = frchainP->frch_next)
1423	{
1424	  int alignment = 0;
1425
1426	  subseg_set (s, frchainP->frch_subseg);
1427
1428	  /* This now gets called even if we had errors.  In that case,
1429	     any alignment is meaningless, and, moreover, will look weird
1430	     if we are generating a listing.  */
1431	  if (!had_errors ())
1432	    {
1433	      alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1434	      if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1435		  && now_seg->entsize)
1436		{
1437		  unsigned int entsize = now_seg->entsize;
1438		  int entalign = 0;
1439
1440		  while ((entsize & 1) == 0)
1441		    {
1442		      ++entalign;
1443		      entsize >>= 1;
1444		    }
1445		  if (entalign > alignment)
1446		    alignment = entalign;
1447		}
1448	    }
1449
1450	  if (subseg_text_p (now_seg))
1451	    frag_align_code (alignment, 0);
1452	  else
1453	    frag_align (alignment, 0, 0);
1454
1455	  /* frag_align will have left a new frag.
1456	     Use this last frag for an empty ".fill".
1457
1458	     For this segment ...
1459	     Create a last frag. Do not leave a "being filled in frag".  */
1460	  frag_wane (frag_now);
1461	  frag_now->fr_fix = 0;
1462	  know (frag_now->fr_next == NULL);
1463	}
1464    }
1465}
1466
1467/* Write the object file.  */
1468
1469void
1470write_object_file (void)
1471{
1472  struct relax_seg_info rsi;
1473#ifndef WORKING_DOT_WORD
1474  fragS *fragP;			/* Track along all frags.  */
1475#endif
1476
1477  /* Do we really want to write it?  */
1478  {
1479    int n_warns, n_errs;
1480    n_warns = had_warnings ();
1481    n_errs = had_errors ();
1482    /* The -Z flag indicates that an object file should be generated,
1483       regardless of warnings and errors.  */
1484    if (flag_always_generate_output)
1485      {
1486	if (n_warns || n_errs)
1487	  as_warn (_("%d error%s, %d warning%s, generating bad object file"),
1488		   n_errs, n_errs == 1 ? "" : "s",
1489		   n_warns, n_warns == 1 ? "" : "s");
1490      }
1491    else
1492      {
1493	if (n_errs)
1494	  as_fatal (_("%d error%s, %d warning%s, no object file generated"),
1495		    n_errs, n_errs == 1 ? "" : "s",
1496		    n_warns, n_warns == 1 ? "" : "s");
1497      }
1498  }
1499
1500#ifdef	OBJ_VMS
1501  /* Under VMS we try to be compatible with VAX-11 "C".  Thus, we call
1502     a routine to check for the definition of the procedure "_main",
1503     and if so -- fix it up so that it can be program entry point.  */
1504  vms_check_for_main ();
1505#endif /* OBJ_VMS  */
1506
1507  /* From now on, we don't care about sub-segments.  Build one frag chain
1508     for each segment. Linked thru fr_next.  */
1509
1510  /* Remove the sections created by gas for its own purposes.  */
1511  {
1512    int i;
1513
1514    bfd_section_list_remove (stdoutput, reg_section);
1515    bfd_section_list_remove (stdoutput, expr_section);
1516    stdoutput->section_count -= 2;
1517    i = 0;
1518    bfd_map_over_sections (stdoutput, renumber_sections, &i);
1519  }
1520
1521  bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1522
1523  /* We have two segments. If user gave -R flag, then we must put the
1524     data frags into the text segment. Do this before relaxing so
1525     we know to take advantage of -R and make shorter addresses.  */
1526  if (flag_readonly_data_in_text)
1527    {
1528      merge_data_into_text ();
1529    }
1530
1531  rsi.pass = 0;
1532  while (1)
1533    {
1534#ifndef WORKING_DOT_WORD
1535      /* We need to reset the markers in the broken word list and
1536	 associated frags between calls to relax_segment (via
1537	 relax_seg).  Since the broken word list is global, we do it
1538	 once per round, rather than locally in relax_segment for each
1539	 segment.  */
1540      struct broken_word *brokp;
1541
1542      for (brokp = broken_words;
1543	   brokp != (struct broken_word *) NULL;
1544	   brokp = brokp->next_broken_word)
1545	{
1546	  brokp->added = 0;
1547
1548	  if (brokp->dispfrag != (fragS *) NULL
1549	      && brokp->dispfrag->fr_type == rs_broken_word)
1550	    brokp->dispfrag->fr_subtype = 0;
1551	}
1552#endif
1553
1554      rsi.changed = 0;
1555      bfd_map_over_sections (stdoutput, relax_seg, &rsi);
1556      rsi.pass++;
1557      if (!rsi.changed)
1558	break;
1559    }
1560
1561  /* Note - Most ports will use the default value of
1562     TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1.  This will force
1563     local symbols to be resolved, removing their frag information.
1564     Some ports however, will not have finished relaxing all of
1565     their frags and will still need the local symbol frag
1566     information.  These ports can set
1567     TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0.  */
1568  finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1569
1570  bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
1571
1572  /* Relaxation has completed.  Freeze all syms.  */
1573  finalize_syms = 1;
1574
1575#ifdef md_post_relax_hook
1576  md_post_relax_hook;
1577#endif
1578
1579#ifndef WORKING_DOT_WORD
1580  {
1581    struct broken_word *lie;
1582    struct broken_word **prevP;
1583
1584    prevP = &broken_words;
1585    for (lie = broken_words; lie; lie = lie->next_broken_word)
1586      if (!lie->added)
1587	{
1588	  expressionS exp;
1589
1590	  subseg_change (lie->seg, lie->subseg);
1591	  exp.X_op = O_subtract;
1592	  exp.X_add_symbol = lie->add;
1593	  exp.X_op_symbol = lie->sub;
1594	  exp.X_add_number = lie->addnum;
1595#ifdef TC_CONS_FIX_NEW
1596	  TC_CONS_FIX_NEW (lie->frag,
1597			   lie->word_goes_here - lie->frag->fr_literal,
1598			   2, &exp);
1599#else
1600	  fix_new_exp (lie->frag,
1601		       lie->word_goes_here - lie->frag->fr_literal,
1602		       2, &exp, 0, BFD_RELOC_16);
1603#endif
1604	  *prevP = lie->next_broken_word;
1605	}
1606      else
1607	prevP = &(lie->next_broken_word);
1608
1609    for (lie = broken_words; lie;)
1610      {
1611	struct broken_word *untruth;
1612	char *table_ptr;
1613	addressT table_addr;
1614	addressT from_addr, to_addr;
1615	int n, m;
1616
1617	subseg_change (lie->seg, lie->subseg);
1618	fragP = lie->dispfrag;
1619
1620	/* Find out how many broken_words go here.  */
1621	n = 0;
1622	for (untruth = lie;
1623	     untruth && untruth->dispfrag == fragP;
1624	     untruth = untruth->next_broken_word)
1625	  if (untruth->added == 1)
1626	    n++;
1627
1628	table_ptr = lie->dispfrag->fr_opcode;
1629	table_addr = (lie->dispfrag->fr_address
1630		      + (table_ptr - lie->dispfrag->fr_literal));
1631	/* Create the jump around the long jumps.  This is a short
1632	   jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
1633	from_addr = table_addr;
1634	to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1635	md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1636			      lie->add);
1637	table_ptr += md_short_jump_size;
1638	table_addr += md_short_jump_size;
1639
1640	for (m = 0;
1641	     lie && lie->dispfrag == fragP;
1642	     m++, lie = lie->next_broken_word)
1643	  {
1644	    if (lie->added == 2)
1645	      continue;
1646	    /* Patch the jump table.  */
1647	    /* This is the offset from ??? to table_ptr+0.  */
1648	    to_addr = table_addr - S_GET_VALUE (lie->sub);
1649#ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1650	    TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1651#endif
1652	    md_number_to_chars (lie->word_goes_here, to_addr, 2);
1653	    for (untruth = lie->next_broken_word;
1654		 untruth && untruth->dispfrag == fragP;
1655		 untruth = untruth->next_broken_word)
1656	      {
1657		if (untruth->use_jump == lie)
1658		  md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1659	      }
1660
1661	    /* Install the long jump.  */
1662	    /* This is a long jump from table_ptr+0 to the final target.  */
1663	    from_addr = table_addr;
1664	    to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1665	    md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1666				 lie->add);
1667	    table_ptr += md_long_jump_size;
1668	    table_addr += md_long_jump_size;
1669	  }
1670      }
1671  }
1672#endif /* not WORKING_DOT_WORD  */
1673
1674  /* Resolve symbol values.  This needs to be done before processing
1675     the relocations.  */
1676  if (symbol_rootP)
1677    {
1678      symbolS *symp;
1679
1680      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1681	resolve_symbol_value (symp);
1682    }
1683  resolve_local_symbol_values ();
1684  resolve_reloc_expr_symbols ();
1685
1686  PROGRESS (1);
1687
1688#ifdef tc_frob_file_before_adjust
1689  tc_frob_file_before_adjust ();
1690#endif
1691#ifdef obj_frob_file_before_adjust
1692  obj_frob_file_before_adjust ();
1693#endif
1694
1695  bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
1696
1697#ifdef tc_frob_file_before_fix
1698  tc_frob_file_before_fix ();
1699#endif
1700#ifdef obj_frob_file_before_fix
1701  obj_frob_file_before_fix ();
1702#endif
1703
1704  bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1705
1706  /* Set up symbol table, and write it out.  */
1707  if (symbol_rootP)
1708    {
1709      symbolS *symp;
1710      bfd_boolean skip_next_symbol = FALSE;
1711
1712      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1713	{
1714	  int punt = 0;
1715	  const char *name;
1716
1717	  if (skip_next_symbol)
1718	    {
1719	      /* Don't do anything besides moving the value of the
1720		 symbol from the GAS value-field to the BFD value-field.  */
1721	      symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1722	      skip_next_symbol = FALSE;
1723	      continue;
1724	    }
1725
1726	  if (symbol_mri_common_p (symp))
1727	    {
1728	      if (S_IS_EXTERNAL (symp))
1729		as_bad (_("%s: global symbols not supported in common sections"),
1730			S_GET_NAME (symp));
1731	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1732	      continue;
1733	    }
1734
1735	  name = S_GET_NAME (symp);
1736	  if (name)
1737	    {
1738	      const char *name2 =
1739		decode_local_label_name ((char *) S_GET_NAME (symp));
1740	      /* They only differ if `name' is a fb or dollar local
1741		 label name.  */
1742	      if (name2 != name && ! S_IS_DEFINED (symp))
1743		as_bad (_("local label `%s' is not defined"), name2);
1744	    }
1745
1746	  /* Do it again, because adjust_reloc_syms might introduce
1747	     more symbols.  They'll probably only be section symbols,
1748	     but they'll still need to have the values computed.  */
1749	  resolve_symbol_value (symp);
1750
1751	  /* Skip symbols which were equated to undefined or common
1752	     symbols.  */
1753	  if (symbol_equated_reloc_p (symp)
1754	      || S_IS_WEAKREFR (symp))
1755	    {
1756	      const char *name = S_GET_NAME (symp);
1757	      if (S_IS_COMMON (symp)
1758		  && !TC_FAKE_LABEL (name)
1759		  && !S_IS_WEAKREFR (symp)
1760		  && (!S_IS_EXTERNAL (symp) || S_IS_LOCAL (symp)))
1761		{
1762		  expressionS *e = symbol_get_value_expression (symp);
1763		  as_bad (_("Local symbol `%s' can't be equated to common symbol `%s'"),
1764			  name, S_GET_NAME (e->X_add_symbol));
1765		}
1766	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1767	      continue;
1768	    }
1769
1770#ifdef obj_frob_symbol
1771	  obj_frob_symbol (symp, punt);
1772#endif
1773#ifdef tc_frob_symbol
1774	  if (! punt || symbol_used_in_reloc_p (symp))
1775	    tc_frob_symbol (symp, punt);
1776#endif
1777
1778	  /* If we don't want to keep this symbol, splice it out of
1779	     the chain now.  If EMIT_SECTION_SYMBOLS is 0, we never
1780	     want section symbols.  Otherwise, we skip local symbols
1781	     and symbols that the frob_symbol macros told us to punt,
1782	     but we keep such symbols if they are used in relocs.  */
1783	  if (symp == abs_section_sym
1784	      || (! EMIT_SECTION_SYMBOLS
1785		  && symbol_section_p (symp))
1786	      /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
1787		 opposites.  Sometimes the former checks flags and the
1788		 latter examines the name...  */
1789	      || (!S_IS_EXTERNAL (symp)
1790		  && (punt || S_IS_LOCAL (symp) ||
1791		      (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
1792		  && ! symbol_used_in_reloc_p (symp)))
1793	    {
1794	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1795
1796	      /* After symbol_remove, symbol_next(symp) still returns
1797		 the one that came after it in the chain.  So we don't
1798		 need to do any extra cleanup work here.  */
1799	      continue;
1800	    }
1801
1802	  /* Make sure we really got a value for the symbol.  */
1803	  if (! symbol_resolved_p (symp))
1804	    {
1805	      as_bad (_("can't resolve value for symbol `%s'"),
1806		      S_GET_NAME (symp));
1807	      symbol_mark_resolved (symp);
1808	    }
1809
1810	  /* Set the value into the BFD symbol.  Up til now the value
1811	     has only been kept in the gas symbolS struct.  */
1812	  symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
1813
1814	  /* A warning construct is a warning symbol followed by the
1815	     symbol warned about.  Don't let anything object-format or
1816	     target-specific muck with it; it's ready for output.  */
1817	  if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
1818	    skip_next_symbol = TRUE;
1819	}
1820    }
1821
1822  PROGRESS (1);
1823
1824  /* Now do any format-specific adjustments to the symbol table, such
1825     as adding file symbols.  */
1826#ifdef tc_adjust_symtab
1827  tc_adjust_symtab ();
1828#endif
1829#ifdef obj_adjust_symtab
1830  obj_adjust_symtab ();
1831#endif
1832
1833  /* Now that all the sizes are known, and contents correct, we can
1834     start writing to the file.  */
1835  set_symtab ();
1836
1837  /* If *_frob_file changes the symbol value at this point, it is
1838     responsible for moving the changed value into symp->bsym->value
1839     as well.  Hopefully all symbol value changing can be done in
1840     *_frob_symbol.  */
1841#ifdef tc_frob_file
1842  tc_frob_file ();
1843#endif
1844#ifdef obj_frob_file
1845  obj_frob_file ();
1846#endif
1847
1848  bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1849
1850#ifdef tc_frob_file_after_relocs
1851  tc_frob_file_after_relocs ();
1852#endif
1853#ifdef obj_frob_file_after_relocs
1854  obj_frob_file_after_relocs ();
1855#endif
1856
1857  bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
1858}
1859
1860#ifdef TC_GENERIC_RELAX_TABLE
1861/* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE.  */
1862
1863long
1864relax_frag (segT segment, fragS *fragP, long stretch)
1865{
1866  const relax_typeS *this_type;
1867  const relax_typeS *start_type;
1868  relax_substateT next_state;
1869  relax_substateT this_state;
1870  offsetT growth;
1871  offsetT aim;
1872  addressT target;
1873  addressT address;
1874  symbolS *symbolP;
1875  const relax_typeS *table;
1876
1877  target = fragP->fr_offset;
1878  address = fragP->fr_address;
1879  table = TC_GENERIC_RELAX_TABLE;
1880  this_state = fragP->fr_subtype;
1881  start_type = this_type = table + this_state;
1882  symbolP = fragP->fr_symbol;
1883
1884  if (symbolP)
1885    {
1886      fragS *sym_frag;
1887
1888      sym_frag = symbol_get_frag (symbolP);
1889
1890#ifndef DIFF_EXPR_OK
1891      know (sym_frag != NULL);
1892#endif
1893      know (S_GET_SEGMENT (symbolP) != absolute_section
1894	    || sym_frag == &zero_address_frag);
1895      target += S_GET_VALUE (symbolP);
1896
1897      /* If frag has yet to be reached on this pass,
1898	 assume it will move by STRETCH just as we did.
1899	 If this is not so, it will be because some frag
1900	 between grows, and that will force another pass.  */
1901
1902      if (stretch != 0
1903	  && sym_frag->relax_marker != fragP->relax_marker
1904	  && S_GET_SEGMENT (symbolP) == segment)
1905	{
1906	  target += stretch;
1907	}
1908    }
1909
1910  aim = target - address - fragP->fr_fix;
1911#ifdef TC_PCREL_ADJUST
1912  /* Currently only the ns32k family needs this.  */
1913  aim += TC_PCREL_ADJUST (fragP);
1914#endif
1915
1916#ifdef md_prepare_relax_scan
1917  /* Formerly called M68K_AIM_KLUDGE.  */
1918  md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
1919#endif
1920
1921  if (aim < 0)
1922    {
1923      /* Look backwards.  */
1924      for (next_state = this_type->rlx_more; next_state;)
1925	if (aim >= this_type->rlx_backward)
1926	  next_state = 0;
1927	else
1928	  {
1929	    /* Grow to next state.  */
1930	    this_state = next_state;
1931	    this_type = table + this_state;
1932	    next_state = this_type->rlx_more;
1933	  }
1934    }
1935  else
1936    {
1937      /* Look forwards.  */
1938      for (next_state = this_type->rlx_more; next_state;)
1939	if (aim <= this_type->rlx_forward)
1940	  next_state = 0;
1941	else
1942	  {
1943	    /* Grow to next state.  */
1944	    this_state = next_state;
1945	    this_type = table + this_state;
1946	    next_state = this_type->rlx_more;
1947	  }
1948    }
1949
1950  growth = this_type->rlx_length - start_type->rlx_length;
1951  if (growth != 0)
1952    fragP->fr_subtype = this_state;
1953  return growth;
1954}
1955
1956#endif /* defined (TC_GENERIC_RELAX_TABLE)  */
1957
1958/* Relax_align. Advance location counter to next address that has 'alignment'
1959   lowest order bits all 0s, return size of adjustment made.  */
1960static relax_addressT
1961relax_align (register relax_addressT address,	/* Address now.  */
1962	     register int alignment	/* Alignment (binary).  */)
1963{
1964  relax_addressT mask;
1965  relax_addressT new_address;
1966
1967  mask = ~((~0) << alignment);
1968  new_address = (address + mask) & (~mask);
1969#ifdef LINKER_RELAXING_SHRINKS_ONLY
1970  if (linkrelax)
1971    /* We must provide lots of padding, so the linker can discard it
1972       when needed.  The linker will not add extra space, ever.  */
1973    new_address += (1 << alignment);
1974#endif
1975  return (new_address - address);
1976}
1977
1978/* Now we have a segment, not a crowd of sub-segments, we can make
1979   fr_address values.
1980
1981   Relax the frags.
1982
1983   After this, all frags in this segment have addresses that are correct
1984   within the segment. Since segments live in different file addresses,
1985   these frag addresses may not be the same as final object-file
1986   addresses.  */
1987
1988int
1989relax_segment (struct frag *segment_frag_root, segT segment, int pass)
1990{
1991  unsigned long frag_count;
1992  struct frag *fragP;
1993  relax_addressT address;
1994  int ret;
1995
1996  /* In case md_estimate_size_before_relax() wants to make fixSs.  */
1997  subseg_change (segment, 0);
1998
1999  /* For each frag in segment: count and store  (a 1st guess of)
2000     fr_address.  */
2001  address = 0;
2002  for (frag_count = 0, fragP = segment_frag_root;
2003       fragP;
2004       fragP = fragP->fr_next, frag_count ++)
2005    {
2006      fragP->relax_marker = 0;
2007      fragP->fr_address = address;
2008      address += fragP->fr_fix;
2009
2010      switch (fragP->fr_type)
2011	{
2012	case rs_fill:
2013	  address += fragP->fr_offset * fragP->fr_var;
2014	  break;
2015
2016	case rs_align:
2017	case rs_align_code:
2018	case rs_align_test:
2019	  {
2020	    addressT offset = relax_align (address, (int) fragP->fr_offset);
2021
2022	    if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2023	      offset = 0;
2024
2025	    if (offset % fragP->fr_var != 0)
2026	      {
2027		as_bad_where (fragP->fr_file, fragP->fr_line,
2028			      _("alignment padding (%lu bytes) not a multiple of %ld"),
2029			      (unsigned long) offset, (long) fragP->fr_var);
2030		offset -= (offset % fragP->fr_var);
2031	      }
2032
2033	    address += offset;
2034	  }
2035	  break;
2036
2037	case rs_org:
2038	case rs_space:
2039	  /* Assume .org is nugatory. It will grow with 1st relax.  */
2040	  break;
2041
2042	case rs_machine_dependent:
2043	  /* If fr_symbol is an expression, this call to
2044	     resolve_symbol_value sets up the correct segment, which will
2045	     likely be needed in md_estimate_size_before_relax.  */
2046	  if (fragP->fr_symbol)
2047	    resolve_symbol_value (fragP->fr_symbol);
2048
2049	  address += md_estimate_size_before_relax (fragP, segment);
2050	  break;
2051
2052#ifndef WORKING_DOT_WORD
2053	  /* Broken words don't concern us yet.  */
2054	case rs_broken_word:
2055	  break;
2056#endif
2057
2058	case rs_leb128:
2059	  /* Initial guess is always 1; doing otherwise can result in
2060	     stable solutions that are larger than the minimum.  */
2061	  address += fragP->fr_offset = 1;
2062	  break;
2063
2064	case rs_cfa:
2065	  address += eh_frame_estimate_size_before_relax (fragP);
2066	  break;
2067
2068	case rs_dwarf2dbg:
2069	  address += dwarf2dbg_estimate_size_before_relax (fragP);
2070	  break;
2071
2072	default:
2073	  BAD_CASE (fragP->fr_type);
2074	  break;
2075	}
2076    }
2077
2078  /* Do relax().  */
2079  {
2080    unsigned long max_iterations;
2081
2082    /* Cumulative address adjustment.  */
2083    offsetT stretch;
2084
2085    /* Have we made any adjustment this pass?  We can't just test
2086       stretch because one piece of code may have grown and another
2087       shrank.  */
2088    int stretched;
2089
2090    /* Most horrible, but gcc may give us some exception data that
2091       is impossible to assemble, of the form
2092
2093       .align 4
2094       .byte 0, 0
2095       .uleb128 end - start
2096       start:
2097       .space 128*128 - 1
2098       .align 4
2099       end:
2100
2101       If the leb128 is two bytes in size, then end-start is 128*128,
2102       which requires a three byte leb128.  If the leb128 is three
2103       bytes in size, then end-start is 128*128-1, which requires a
2104       two byte leb128.  We work around this dilemma by inserting
2105       an extra 4 bytes of alignment just after the .align.  This
2106       works because the data after the align is accessed relative to
2107       the end label.
2108
2109       This counter is used in a tiny state machine to detect
2110       whether a leb128 followed by an align is impossible to
2111       relax.  */
2112    int rs_leb128_fudge = 0;
2113
2114    /* We want to prevent going into an infinite loop where one frag grows
2115       depending upon the location of a symbol which is in turn moved by
2116       the growing frag.  eg:
2117
2118	 foo = .
2119	 .org foo+16
2120	 foo = .
2121
2122       So we dictate that this algorithm can be at most O2.  */
2123    max_iterations = frag_count * frag_count;
2124    /* Check for overflow.  */
2125    if (max_iterations < frag_count)
2126      max_iterations = frag_count;
2127
2128    ret = 0;
2129    do
2130      {
2131	stretch = 0;
2132	stretched = 0;
2133
2134	for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2135	  {
2136	    offsetT growth = 0;
2137	    addressT was_address;
2138	    offsetT offset;
2139	    symbolS *symbolP;
2140
2141	    fragP->relax_marker ^= 1;
2142	    was_address = fragP->fr_address;
2143	    address = fragP->fr_address += stretch;
2144	    symbolP = fragP->fr_symbol;
2145	    offset = fragP->fr_offset;
2146
2147	    switch (fragP->fr_type)
2148	      {
2149	      case rs_fill:	/* .fill never relaxes.  */
2150		growth = 0;
2151		break;
2152
2153#ifndef WORKING_DOT_WORD
2154		/* JF:  This is RMS's idea.  I do *NOT* want to be blamed
2155		   for it I do not want to write it.  I do not want to have
2156		   anything to do with it.  This is not the proper way to
2157		   implement this misfeature.  */
2158	      case rs_broken_word:
2159		{
2160		  struct broken_word *lie;
2161		  struct broken_word *untruth;
2162
2163		  /* Yes this is ugly (storing the broken_word pointer
2164		     in the symbol slot).  Still, this whole chunk of
2165		     code is ugly, and I don't feel like doing anything
2166		     about it.  Think of it as stubbornness in action.  */
2167		  growth = 0;
2168		  for (lie = (struct broken_word *) (fragP->fr_symbol);
2169		       lie && lie->dispfrag == fragP;
2170		       lie = lie->next_broken_word)
2171		    {
2172
2173		      if (lie->added)
2174			continue;
2175
2176		      offset = (S_GET_VALUE (lie->add)
2177				+ lie->addnum
2178				- S_GET_VALUE (lie->sub));
2179		      if (offset <= -32768 || offset >= 32767)
2180			{
2181			  if (flag_warn_displacement)
2182			    {
2183			      char buf[50];
2184			      sprint_value (buf, (addressT) lie->addnum);
2185			      as_warn_where (fragP->fr_file, fragP->fr_line,
2186					     _(".word %s-%s+%s didn't fit"),
2187					     S_GET_NAME (lie->add),
2188					     S_GET_NAME (lie->sub),
2189					     buf);
2190			    }
2191			  lie->added = 1;
2192			  if (fragP->fr_subtype == 0)
2193			    {
2194			      fragP->fr_subtype++;
2195			      growth += md_short_jump_size;
2196			    }
2197			  for (untruth = lie->next_broken_word;
2198			       untruth && untruth->dispfrag == lie->dispfrag;
2199			       untruth = untruth->next_broken_word)
2200			    if ((symbol_get_frag (untruth->add)
2201				 == symbol_get_frag (lie->add))
2202				&& (S_GET_VALUE (untruth->add)
2203				    == S_GET_VALUE (lie->add)))
2204			      {
2205				untruth->added = 2;
2206				untruth->use_jump = lie;
2207			      }
2208			  growth += md_long_jump_size;
2209			}
2210		    }
2211
2212		  break;
2213		}		/* case rs_broken_word  */
2214#endif
2215	      case rs_align:
2216	      case rs_align_code:
2217	      case rs_align_test:
2218		{
2219		  addressT oldoff, newoff;
2220
2221		  oldoff = relax_align (was_address + fragP->fr_fix,
2222					(int) offset);
2223		  newoff = relax_align (address + fragP->fr_fix,
2224					(int) offset);
2225
2226		  if (fragP->fr_subtype != 0)
2227		    {
2228		      if (oldoff > fragP->fr_subtype)
2229			oldoff = 0;
2230		      if (newoff > fragP->fr_subtype)
2231			newoff = 0;
2232		    }
2233
2234		  growth = newoff - oldoff;
2235
2236		  /* If this align happens to follow a leb128 and
2237		     we have determined that the leb128 is bouncing
2238		     in size, then break the cycle by inserting an
2239		     extra alignment.  */
2240		  if (growth < 0
2241		      && (rs_leb128_fudge & 16) != 0
2242		      && (rs_leb128_fudge & 15) >= 2)
2243		    {
2244		      segment_info_type *seginfo = seg_info (segment);
2245		      struct obstack *ob = &seginfo->frchainP->frch_obstack;
2246		      struct frag *newf;
2247
2248		      newf = frag_alloc (ob);
2249		      obstack_blank_fast (ob, fragP->fr_var);
2250		      (void) obstack_finish (ob);
2251		      memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
2252		      memcpy (newf->fr_literal,
2253			      fragP->fr_literal + fragP->fr_fix,
2254			      fragP->fr_var);
2255		      newf->fr_type = rs_fill;
2256		      newf->fr_fix = 0;
2257		      newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
2258					 / fragP->fr_var);
2259		      if (newf->fr_offset * newf->fr_var
2260			  != (offsetT) 1 << fragP->fr_offset)
2261			{
2262			  newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
2263			  newf->fr_var = 1;
2264			}
2265		      /* Include growth of new frag, because rs_fill
2266			 frags don't normally grow.  */
2267		      growth += newf->fr_offset * newf->fr_var;
2268		      /* The new frag address is newoff.  Adjust this
2269			 for the amount we'll add when we process the
2270			 new frag.  */
2271		      newf->fr_address = newoff - stretch - growth;
2272		      newf->relax_marker ^= 1;
2273		      fragP->fr_next = newf;
2274#ifdef DEBUG
2275		      as_warn (_("padding added"));
2276#endif
2277		    }
2278		}
2279		break;
2280
2281	      case rs_org:
2282		{
2283		  addressT target = offset;
2284		  addressT after;
2285
2286		  if (symbolP)
2287		    {
2288		      /* Convert from an actual address to an octet offset
2289			 into the section.  Here it is assumed that the
2290			 section's VMA is zero, and can omit subtracting it
2291			 from the symbol's value to get the address offset.  */
2292		      know (S_GET_SEGMENT (symbolP)->vma == 0);
2293		      target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2294		    }
2295
2296		  know (fragP->fr_next);
2297		  after = fragP->fr_next->fr_address;
2298		  growth = target - after;
2299		  if (growth < 0)
2300		    {
2301		      growth = 0;
2302
2303		      /* Don't error on first few frag relax passes.
2304			 The symbol might be an expression involving
2305			 symbol values from other sections.  If those
2306			 sections have not yet been processed their
2307			 frags will all have zero addresses, so we
2308			 will calculate incorrect values for them.  The
2309			 number of passes we allow before giving an
2310			 error is somewhat arbitrary.  It should be at
2311			 least one, with larger values requiring
2312			 increasingly contrived dependencies between
2313			 frags to trigger a false error.  */
2314		      if (pass < 2)
2315			{
2316			  /* Force another pass.  */
2317			  ret = 1;
2318			  break;
2319			}
2320
2321		      /* Growth may be negative, but variable part of frag
2322			 cannot have fewer than 0 chars.  That is, we can't
2323			 .org backwards.  */
2324		      as_bad_where (fragP->fr_file, fragP->fr_line,
2325				    _("attempt to move .org backwards"));
2326
2327		      /* We've issued an error message.  Change the
2328			 frag to avoid cascading errors.  */
2329		      fragP->fr_type = rs_align;
2330		      fragP->fr_subtype = 0;
2331		      fragP->fr_offset = 0;
2332		      fragP->fr_fix = after - was_address;
2333		      break;
2334		    }
2335
2336		  /* This is an absolute growth factor  */
2337		  growth -= stretch;
2338		  break;
2339		}
2340
2341	      case rs_space:
2342		growth = 0;
2343		if (symbolP)
2344		  {
2345		    offsetT amount;
2346
2347		    amount = S_GET_VALUE (symbolP);
2348		    if (S_GET_SEGMENT (symbolP) != absolute_section
2349			|| S_IS_COMMON (symbolP)
2350			|| ! S_IS_DEFINED (symbolP))
2351		      {
2352			as_bad_where (fragP->fr_file, fragP->fr_line,
2353				      _(".space specifies non-absolute value"));
2354			/* Prevent repeat of this error message.  */
2355			fragP->fr_symbol = 0;
2356		      }
2357		    else if (amount < 0)
2358		      {
2359			/* Don't error on first few frag relax passes.
2360			   See rs_org comment for a longer explanation.  */
2361			if (pass < 2)
2362			  {
2363			    ret = 1;
2364			    break;
2365			  }
2366
2367			as_warn_where (fragP->fr_file, fragP->fr_line,
2368				       _(".space or .fill with negative value, ignored"));
2369			fragP->fr_symbol = 0;
2370		      }
2371		    else
2372		      growth = (was_address + fragP->fr_fix + amount
2373				- fragP->fr_next->fr_address);
2374		  }
2375		break;
2376
2377	      case rs_machine_dependent:
2378#ifdef md_relax_frag
2379		growth = md_relax_frag (segment, fragP, stretch);
2380#else
2381#ifdef TC_GENERIC_RELAX_TABLE
2382		/* The default way to relax a frag is to look through
2383		   TC_GENERIC_RELAX_TABLE.  */
2384		growth = relax_frag (segment, fragP, stretch);
2385#endif /* TC_GENERIC_RELAX_TABLE  */
2386#endif
2387		break;
2388
2389	      case rs_leb128:
2390		{
2391		  valueT value;
2392		  offsetT size;
2393
2394		  value = resolve_symbol_value (fragP->fr_symbol);
2395		  size = sizeof_leb128 (value, fragP->fr_subtype);
2396		  growth = size - fragP->fr_offset;
2397		  fragP->fr_offset = size;
2398		}
2399		break;
2400
2401	      case rs_cfa:
2402		growth = eh_frame_relax_frag (fragP);
2403		break;
2404
2405	      case rs_dwarf2dbg:
2406		growth = dwarf2dbg_relax_frag (fragP);
2407		break;
2408
2409	      default:
2410		BAD_CASE (fragP->fr_type);
2411		break;
2412	      }
2413	    if (growth)
2414	      {
2415		stretch += growth;
2416		stretched = 1;
2417		if (fragP->fr_type == rs_leb128)
2418		  rs_leb128_fudge += 16;
2419		else if (fragP->fr_type == rs_align
2420			 && (rs_leb128_fudge & 16) != 0
2421			 && stretch == 0)
2422		  rs_leb128_fudge += 16;
2423		else
2424		  rs_leb128_fudge = 0;
2425	      }
2426	  }
2427
2428	if (stretch == 0
2429	    && (rs_leb128_fudge & 16) == 0
2430	    && (rs_leb128_fudge & -16) != 0)
2431	  rs_leb128_fudge += 1;
2432	else
2433	  rs_leb128_fudge = 0;
2434      }
2435    /* Until nothing further to relax.  */
2436    while (stretched && -- max_iterations);
2437
2438    if (stretched)
2439      as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
2440		segment_name (segment));
2441  }
2442
2443  for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2444    if (fragP->last_fr_address != fragP->fr_address)
2445      {
2446	fragP->last_fr_address = fragP->fr_address;
2447	ret = 1;
2448      }
2449  return ret;
2450}
2451
2452void
2453number_to_chars_bigendian (char *buf, valueT val, int n)
2454{
2455  if (n <= 0)
2456    abort ();
2457  while (n--)
2458    {
2459      buf[n] = val & 0xff;
2460      val >>= 8;
2461    }
2462}
2463
2464void
2465number_to_chars_littleendian (char *buf, valueT val, int n)
2466{
2467  if (n <= 0)
2468    abort ();
2469  while (n--)
2470    {
2471      *buf++ = val & 0xff;
2472      val >>= 8;
2473    }
2474}
2475
2476void
2477write_print_statistics (FILE *file)
2478{
2479  fprintf (file, "fixups: %d\n", n_fixups);
2480}
2481
2482/* For debugging.  */
2483extern int indent_level;
2484
2485void
2486print_fixup (fixS *fixp)
2487{
2488  indent_level = 1;
2489  fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
2490  if (fixp->fx_pcrel)
2491    fprintf (stderr, " pcrel");
2492  if (fixp->fx_pcrel_adjust)
2493    fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2494  if (fixp->fx_im_disp)
2495    {
2496#ifdef TC_NS32K
2497      fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2498#else
2499      fprintf (stderr, " im_disp");
2500#endif
2501    }
2502  if (fixp->fx_tcbit)
2503    fprintf (stderr, " tcbit");
2504  if (fixp->fx_done)
2505    fprintf (stderr, " done");
2506  fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2507	   fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2508	   (long) fixp->fx_offset, (long) fixp->fx_addnumber);
2509  fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2510	   fixp->fx_r_type);
2511  if (fixp->fx_addsy)
2512    {
2513      fprintf (stderr, "\n   +<");
2514      print_symbol_value_1 (stderr, fixp->fx_addsy);
2515      fprintf (stderr, ">");
2516    }
2517  if (fixp->fx_subsy)
2518    {
2519      fprintf (stderr, "\n   -<");
2520      print_symbol_value_1 (stderr, fixp->fx_subsy);
2521      fprintf (stderr, ">");
2522    }
2523  fprintf (stderr, "\n");
2524#ifdef TC_FIX_DATA_PRINT
2525  TC_FIX_DATA_PRINT (stderr, fixp);
2526#endif
2527}
2528