write.c revision 38889
133965Sjdp/* write.c - emit .o file
238889Sjdp   Copyright (C) 1986, 87, 90, 91, 92, 93, 94, 95, 96, 97, 1998
333965Sjdp   Free Software Foundation, Inc.
433965Sjdp
533965Sjdp   This file is part of GAS, the GNU Assembler.
633965Sjdp
733965Sjdp   GAS is free software; you can redistribute it and/or modify
833965Sjdp   it under the terms of the GNU General Public License as published by
933965Sjdp   the Free Software Foundation; either version 2, or (at your option)
1033965Sjdp   any later version.
1133965Sjdp
1233965Sjdp   GAS is distributed in the hope that it will be useful,
1333965Sjdp   but WITHOUT ANY WARRANTY; without even the implied warranty of
1433965Sjdp   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1533965Sjdp   GNU General Public License for more details.
1633965Sjdp
1733965Sjdp   You should have received a copy of the GNU General Public License
1833965Sjdp   along with GAS; see the file COPYING.  If not, write to the Free
1933965Sjdp   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2033965Sjdp   02111-1307, USA.  */
2133965Sjdp
2233965Sjdp/* This thing should be set up to do byteordering correctly.  But... */
2333965Sjdp
2433965Sjdp#include "as.h"
2533965Sjdp#include "subsegs.h"
2633965Sjdp#include "obstack.h"
2733965Sjdp#include "output-file.h"
2833965Sjdp
2933965Sjdp/* This looks like a good idea.  Let's try turning it on always, for now.  */
3033965Sjdp#undef  BFD_FAST_SECTION_FILL
3133965Sjdp#define BFD_FAST_SECTION_FILL
3233965Sjdp
3333965Sjdp/* The NOP_OPCODE is for the alignment fill value.  Fill it with a nop
3433965Sjdp   instruction so that the disassembler does not choke on it.  */
3533965Sjdp#ifndef NOP_OPCODE
3633965Sjdp#define NOP_OPCODE 0x00
3733965Sjdp#endif
3833965Sjdp
3933965Sjdp#ifndef TC_ADJUST_RELOC_COUNT
4033965Sjdp#define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
4133965Sjdp#endif
4233965Sjdp
4333965Sjdp#ifndef TC_FORCE_RELOCATION
4433965Sjdp#define TC_FORCE_RELOCATION(FIXP) 0
4533965Sjdp#endif
4633965Sjdp
4733965Sjdp#ifndef TC_FORCE_RELOCATION_SECTION
4833965Sjdp#define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
4933965Sjdp#endif
5033965Sjdp
5133965Sjdp#ifndef	MD_PCREL_FROM_SECTION
5233965Sjdp#define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
5333965Sjdp#endif
5433965Sjdp
5533965Sjdp#ifndef WORKING_DOT_WORD
5633965Sjdpextern CONST int md_short_jump_size;
5733965Sjdpextern CONST int md_long_jump_size;
5833965Sjdp#endif
5933965Sjdp
6033965Sjdpint symbol_table_frozen;
6133965Sjdpvoid print_fixup PARAMS ((fixS *));
6233965Sjdp
6333965Sjdp#ifdef BFD_ASSEMBLER
6433965Sjdpstatic void renumber_sections PARAMS ((bfd *, asection *, PTR));
6533965Sjdp
6633965Sjdp/* We generally attach relocs to frag chains.  However, after we have
6733965Sjdp   chained these all together into a segment, any relocs we add after
6833965Sjdp   that must be attached to a segment.  This will include relocs added
6933965Sjdp   in md_estimate_size_for_relax, for example.  */
7033965Sjdpstatic int frags_chained = 0;
7133965Sjdp#endif
7233965Sjdp
7333965Sjdp#ifndef BFD_ASSEMBLER
7433965Sjdp
7533965Sjdp#ifndef MANY_SEGMENTS
7633965Sjdpstruct frag *text_frag_root;
7733965Sjdpstruct frag *data_frag_root;
7833965Sjdpstruct frag *bss_frag_root;
7933965Sjdp
8033965Sjdpstruct frag *text_last_frag;	/* Last frag in segment. */
8133965Sjdpstruct frag *data_last_frag;	/* Last frag in segment. */
8233965Sjdpstatic struct frag *bss_last_frag;	/* Last frag in segment. */
8333965Sjdp#endif
8433965Sjdp
8533965Sjdp#ifndef BFD
8633965Sjdpstatic object_headers headers;
8733965Sjdp#endif
8833965Sjdp
8933965Sjdplong string_byte_count;
9033965Sjdpchar *next_object_file_charP;	/* Tracks object file bytes. */
9133965Sjdp
9233965Sjdp#ifndef OBJ_VMS
9333965Sjdpint magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
9433965Sjdp#endif
9533965Sjdp
9633965Sjdp#endif /* BFD_ASSEMBLER */
9733965Sjdp
9833965Sjdpstatic int n_fixups;
9933965Sjdp
10033965Sjdp#ifdef BFD_ASSEMBLER
10133965Sjdpstatic fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
10233965Sjdp				       symbolS *add, symbolS *sub,
10333965Sjdp				       offsetT offset, int pcrel,
10433965Sjdp				       bfd_reloc_code_real_type r_type));
10533965Sjdp#else
10633965Sjdpstatic fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
10733965Sjdp				       symbolS *add, symbolS *sub,
10833965Sjdp				       offsetT offset, int pcrel,
10933965Sjdp				       int r_type));
11033965Sjdp#endif
11133965Sjdp#if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
11233965Sjdpstatic long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
11333965Sjdp#endif
11433965Sjdpstatic relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
11533965Sjdp#if defined (BFD_ASSEMBLER) || ! defined (BFD)
11633965Sjdpstatic fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
11733965Sjdp#endif
11833965Sjdp#ifdef BFD_ASSEMBLER
11933965Sjdpstatic void chain_frchains_together PARAMS ((bfd *, segT, PTR));
12033965Sjdpstatic void cvt_frag_to_fill PARAMS ((segT, fragS *));
12133965Sjdpstatic void relax_and_size_seg PARAMS ((bfd *, asection *, PTR));
12233965Sjdpstatic void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
12333965Sjdpstatic void write_relocs PARAMS ((bfd *, asection *, PTR));
12433965Sjdpstatic void write_contents PARAMS ((bfd *, asection *, PTR));
12533965Sjdpstatic void set_symtab PARAMS ((void));
12633965Sjdp#endif
12733965Sjdp#if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
12833965Sjdpstatic void merge_data_into_text PARAMS ((void));
12933965Sjdp#endif
13033965Sjdp#if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
13133965Sjdpstatic void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
13233965Sjdpstatic void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
13333965Sjdpstatic void relax_and_size_all_segments PARAMS ((void));
13433965Sjdp#endif
13533965Sjdp
13633965Sjdp/*
13733965Sjdp *			fix_new()
13833965Sjdp *
13933965Sjdp * Create a fixS in obstack 'notes'.
14033965Sjdp */
14133965Sjdpstatic fixS *
14233965Sjdpfix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
14333965Sjdp		  r_type)
14433965Sjdp     fragS *frag;		/* Which frag? */
14533965Sjdp     int where;			/* Where in that frag? */
14633965Sjdp     int size;			/* 1, 2, or 4 usually. */
14733965Sjdp     symbolS *add_symbol;	/* X_add_symbol. */
14833965Sjdp     symbolS *sub_symbol;	/* X_op_symbol. */
14933965Sjdp     offsetT offset;		/* X_add_number. */
15033965Sjdp     int pcrel;			/* TRUE if PC-relative relocation. */
15133965Sjdp#ifdef BFD_ASSEMBLER
15233965Sjdp     bfd_reloc_code_real_type r_type; /* Relocation type */
15333965Sjdp#else
15433965Sjdp     int r_type;		/* Relocation type */
15533965Sjdp#endif
15633965Sjdp{
15733965Sjdp  fixS *fixP;
15833965Sjdp
15933965Sjdp  n_fixups++;
16033965Sjdp
16133965Sjdp  fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
16233965Sjdp
16333965Sjdp  fixP->fx_frag = frag;
16433965Sjdp  fixP->fx_where = where;
16533965Sjdp  fixP->fx_size = size;
16633965Sjdp  /* We've made fx_size a narrow field; check that it's wide enough.  */
16733965Sjdp  if (fixP->fx_size != size)
16833965Sjdp    {
16933965Sjdp      as_bad ("field fx_size too small to hold %d", size);
17033965Sjdp      abort ();
17133965Sjdp    }
17233965Sjdp  fixP->fx_addsy = add_symbol;
17333965Sjdp  fixP->fx_subsy = sub_symbol;
17433965Sjdp  fixP->fx_offset = offset;
17533965Sjdp  fixP->fx_pcrel = pcrel;
17633965Sjdp  fixP->fx_plt = 0;
17733965Sjdp#if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
17833965Sjdp  fixP->fx_r_type = r_type;
17933965Sjdp#endif
18033965Sjdp  fixP->fx_im_disp = 0;
18133965Sjdp  fixP->fx_pcrel_adjust = 0;
18233965Sjdp  fixP->fx_bit_fixP = 0;
18333965Sjdp  fixP->fx_addnumber = 0;
18433965Sjdp  fixP->fx_tcbit = 0;
18533965Sjdp  fixP->fx_done = 0;
18633965Sjdp  fixP->fx_no_overflow = 0;
18733965Sjdp  fixP->fx_signed = 0;
18833965Sjdp
18933965Sjdp#ifdef TC_FIX_TYPE
19033965Sjdp  TC_INIT_FIX_DATA(fixP);
19133965Sjdp#endif
19233965Sjdp
19333965Sjdp  as_where (&fixP->fx_file, &fixP->fx_line);
19433965Sjdp
19533965Sjdp  /* Usually, we want relocs sorted numerically, but while
19633965Sjdp     comparing to older versions of gas that have relocs
19733965Sjdp     reverse sorted, it is convenient to have this compile
19833965Sjdp     time option.  xoxorich. */
19933965Sjdp
20033965Sjdp  {
20133965Sjdp
20233965Sjdp#ifdef BFD_ASSEMBLER
20333965Sjdp    fixS **seg_fix_rootP = (frags_chained
20433965Sjdp			    ? &seg_info (now_seg)->fix_root
20533965Sjdp			    : &frchain_now->fix_root);
20633965Sjdp    fixS **seg_fix_tailP = (frags_chained
20733965Sjdp			    ? &seg_info (now_seg)->fix_tail
20833965Sjdp			    : &frchain_now->fix_tail);
20933965Sjdp#endif
21033965Sjdp
21133965Sjdp#ifdef REVERSE_SORT_RELOCS
21233965Sjdp
21333965Sjdp    fixP->fx_next = *seg_fix_rootP;
21433965Sjdp    *seg_fix_rootP = fixP;
21533965Sjdp
21633965Sjdp#else /* REVERSE_SORT_RELOCS */
21733965Sjdp
21833965Sjdp    fixP->fx_next = NULL;
21933965Sjdp
22033965Sjdp    if (*seg_fix_tailP)
22133965Sjdp      (*seg_fix_tailP)->fx_next = fixP;
22233965Sjdp    else
22333965Sjdp      *seg_fix_rootP = fixP;
22433965Sjdp    *seg_fix_tailP = fixP;
22533965Sjdp
22633965Sjdp#endif /* REVERSE_SORT_RELOCS */
22733965Sjdp
22833965Sjdp  }
22933965Sjdp
23033965Sjdp  return fixP;
23133965Sjdp}
23233965Sjdp
23333965Sjdp/* Create a fixup relative to a symbol (plus a constant).  */
23433965Sjdp
23533965SjdpfixS *
23633965Sjdpfix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
23733965Sjdp     fragS *frag;		/* Which frag? */
23833965Sjdp     int where;			/* Where in that frag? */
23933965Sjdp     int size;			/* 1, 2, or 4 usually. */
24033965Sjdp     symbolS *add_symbol;	/* X_add_symbol. */
24133965Sjdp     offsetT offset;		/* X_add_number. */
24233965Sjdp     int pcrel;			/* TRUE if PC-relative relocation. */
24333965Sjdp#ifdef BFD_ASSEMBLER
24433965Sjdp     bfd_reloc_code_real_type r_type; /* Relocation type */
24533965Sjdp#else
24633965Sjdp     int r_type;		/* Relocation type */
24733965Sjdp#endif
24833965Sjdp{
24933965Sjdp  return fix_new_internal (frag, where, size, add_symbol,
25033965Sjdp			   (symbolS *) NULL, offset, pcrel, r_type);
25133965Sjdp}
25233965Sjdp
25333965Sjdp/* Create a fixup for an expression.  Currently we only support fixups
25433965Sjdp   for difference expressions.  That is itself more than most object
25533965Sjdp   file formats support anyhow.  */
25633965Sjdp
25733965SjdpfixS *
25833965Sjdpfix_new_exp (frag, where, size, exp, pcrel, r_type)
25933965Sjdp     fragS *frag;		/* Which frag? */
26033965Sjdp     int where;			/* Where in that frag? */
26133965Sjdp     int size;			/* 1, 2, or 4 usually. */
26233965Sjdp     expressionS *exp;		/* Expression.  */
26333965Sjdp     int pcrel;			/* TRUE if PC-relative relocation. */
26433965Sjdp#ifdef BFD_ASSEMBLER
26533965Sjdp     bfd_reloc_code_real_type r_type; /* Relocation type */
26633965Sjdp#else
26733965Sjdp     int r_type;		/* Relocation type */
26833965Sjdp#endif
26933965Sjdp{
27033965Sjdp  symbolS *add = NULL;
27133965Sjdp  symbolS *sub = NULL;
27233965Sjdp  offsetT off = 0;
27333965Sjdp
27433965Sjdp  switch (exp->X_op)
27533965Sjdp    {
27633965Sjdp    case O_absent:
27733965Sjdp      break;
27833965Sjdp
27933965Sjdp    case O_add:
28033965Sjdp      /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
28133965Sjdp	 the difference expression cannot immediately be reduced.  */
28233965Sjdp      {
28333965Sjdp	symbolS *stmp = make_expr_symbol (exp);
28433965Sjdp	exp->X_op = O_symbol;
28533965Sjdp	exp->X_op_symbol = 0;
28633965Sjdp	exp->X_add_symbol = stmp;
28733965Sjdp	exp->X_add_number = 0;
28833965Sjdp	return fix_new_exp (frag, where, size, exp, pcrel, r_type);
28933965Sjdp      }
29033965Sjdp
29133965Sjdp    case O_symbol_rva:
29233965Sjdp      add = exp->X_add_symbol;
29333965Sjdp      off = exp->X_add_number;
29433965Sjdp
29533965Sjdp#if defined(BFD_ASSEMBLER)
29633965Sjdp      r_type = BFD_RELOC_RVA;
29733965Sjdp#else
29833965Sjdp#if defined(TC_RVA_RELOC)
29933965Sjdp      r_type = TC_RVA_RELOC;
30033965Sjdp#else
30133965Sjdp      as_fatal("rva not supported");
30233965Sjdp#endif
30333965Sjdp#endif
30433965Sjdp      break;
30533965Sjdp
30633965Sjdp    case O_uminus:
30733965Sjdp      sub = exp->X_add_symbol;
30833965Sjdp      off = exp->X_add_number;
30933965Sjdp      break;
31033965Sjdp
31133965Sjdp    case O_subtract:
31233965Sjdp      sub = exp->X_op_symbol;
31333965Sjdp      /* Fall through.  */
31433965Sjdp    case O_symbol:
31533965Sjdp      add = exp->X_add_symbol;
31633965Sjdp      /* Fall through.   */
31733965Sjdp    case O_constant:
31833965Sjdp      off = exp->X_add_number;
31933965Sjdp      break;
32033965Sjdp
32133965Sjdp    default:
32233965Sjdp      add = make_expr_symbol (exp);
32333965Sjdp      break;
32433965Sjdp    }
32533965Sjdp
32633965Sjdp  return fix_new_internal (frag, where, size, add, sub, off,
32733965Sjdp			   pcrel, r_type);
32833965Sjdp}
32933965Sjdp
33033965Sjdp/* Append a string onto another string, bumping the pointer along.  */
33133965Sjdpvoid
33233965Sjdpappend (charPP, fromP, length)
33333965Sjdp     char **charPP;
33433965Sjdp     char *fromP;
33533965Sjdp     unsigned long length;
33633965Sjdp{
33733965Sjdp  /* Don't trust memcpy() of 0 chars. */
33833965Sjdp  if (length == 0)
33933965Sjdp    return;
34033965Sjdp
34133965Sjdp  memcpy (*charPP, fromP, length);
34233965Sjdp  *charPP += length;
34333965Sjdp}
34433965Sjdp
34533965Sjdp#ifndef BFD_ASSEMBLER
34633965Sjdpint section_alignment[SEG_MAXIMUM_ORDINAL];
34733965Sjdp#endif
34833965Sjdp
34933965Sjdp/*
35033965Sjdp * This routine records the largest alignment seen for each segment.
35133965Sjdp * If the beginning of the segment is aligned on the worst-case
35233965Sjdp * boundary, all of the other alignments within it will work.  At
35333965Sjdp * least one object format really uses this info.
35433965Sjdp */
35533965Sjdpvoid
35633965Sjdprecord_alignment (seg, align)
35733965Sjdp     /* Segment to which alignment pertains */
35833965Sjdp     segT seg;
35933965Sjdp     /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
36033965Sjdp	boundary, etc.)  */
36133965Sjdp     int align;
36233965Sjdp{
36333965Sjdp  if (seg == absolute_section)
36433965Sjdp    return;
36533965Sjdp#ifdef BFD_ASSEMBLER
36638889Sjdp  if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
36733965Sjdp    bfd_set_section_alignment (stdoutput, seg, align);
36833965Sjdp#else
36933965Sjdp  if (align > section_alignment[(int) seg])
37033965Sjdp    section_alignment[(int) seg] = align;
37133965Sjdp#endif
37233965Sjdp}
37333965Sjdp
37433965Sjdp#ifdef BFD_ASSEMBLER
37533965Sjdp
37633965Sjdp/* Reset the section indices after removing the gas created sections.  */
37733965Sjdp
37833965Sjdpstatic void
37933965Sjdprenumber_sections (abfd, sec, countparg)
38033965Sjdp     bfd *abfd;
38133965Sjdp     asection *sec;
38233965Sjdp     PTR countparg;
38333965Sjdp{
38433965Sjdp  int *countp = (int *) countparg;
38533965Sjdp
38633965Sjdp  sec->index = *countp;
38733965Sjdp  ++*countp;
38833965Sjdp}
38933965Sjdp
39033965Sjdp#endif /* defined (BFD_ASSEMBLER) */
39133965Sjdp
39233965Sjdp#if defined (BFD_ASSEMBLER) || ! defined (BFD)
39333965Sjdp
39433965Sjdpstatic fragS *
39533965Sjdpchain_frchains_together_1 (section, frchp)
39633965Sjdp     segT section;
39733965Sjdp     struct frchain *frchp;
39833965Sjdp{
39933965Sjdp  fragS dummy, *prev_frag = &dummy;
40033965Sjdp#ifdef BFD_ASSEMBLER
40133965Sjdp  fixS fix_dummy, *prev_fix = &fix_dummy;
40233965Sjdp#endif
40333965Sjdp
40433965Sjdp  for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
40533965Sjdp    {
40633965Sjdp      prev_frag->fr_next = frchp->frch_root;
40733965Sjdp      prev_frag = frchp->frch_last;
40833965Sjdp      assert (prev_frag->fr_type != 0);
40933965Sjdp#ifdef BFD_ASSEMBLER
41033965Sjdp      if (frchp->fix_root != (fixS *) NULL)
41133965Sjdp	{
41233965Sjdp	  if (seg_info (section)->fix_root == (fixS *) NULL)
41333965Sjdp	    seg_info (section)->fix_root = frchp->fix_root;
41433965Sjdp	  prev_fix->fx_next = frchp->fix_root;
41533965Sjdp	  seg_info (section)->fix_tail = frchp->fix_tail;
41633965Sjdp	  prev_fix = frchp->fix_tail;
41733965Sjdp	}
41833965Sjdp#endif
41933965Sjdp    }
42033965Sjdp  assert (prev_frag->fr_type != 0);
42133965Sjdp  prev_frag->fr_next = 0;
42233965Sjdp  return prev_frag;
42333965Sjdp}
42433965Sjdp
42533965Sjdp#endif
42633965Sjdp
42733965Sjdp#ifdef BFD_ASSEMBLER
42833965Sjdp
42933965Sjdpstatic void
43033965Sjdpchain_frchains_together (abfd, section, xxx)
43133965Sjdp     bfd *abfd;			/* unused */
43233965Sjdp     segT section;
43333965Sjdp     PTR xxx;			/* unused */
43433965Sjdp{
43533965Sjdp  segment_info_type *info;
43633965Sjdp
43733965Sjdp  /* BFD may have introduced its own sections without using
43833965Sjdp     subseg_new, so it is possible that seg_info is NULL.  */
43933965Sjdp  info = seg_info (section);
44033965Sjdp  if (info != (segment_info_type *) NULL)
44133965Sjdp   info->frchainP->frch_last
44233965Sjdp     = chain_frchains_together_1 (section, info->frchainP);
44333965Sjdp
44433965Sjdp  /* Now that we've chained the frags together, we must add new fixups
44533965Sjdp     to the segment, not to the frag chain.  */
44633965Sjdp  frags_chained = 1;
44733965Sjdp}
44833965Sjdp
44933965Sjdp#endif
45033965Sjdp
45133965Sjdp#if !defined (BFD) && !defined (BFD_ASSEMBLER)
45233965Sjdp
45333965Sjdpstatic void
45433965Sjdpremove_subsegs (head, seg, root, last)
45533965Sjdp     frchainS *head;
45633965Sjdp     int seg;
45733965Sjdp     fragS **root;
45833965Sjdp     fragS **last;
45933965Sjdp{
46033965Sjdp  *root = head->frch_root;
46133965Sjdp  *last = chain_frchains_together_1 (seg, head);
46233965Sjdp}
46333965Sjdp
46433965Sjdp#endif /* BFD */
46533965Sjdp
46633965Sjdp#if defined (BFD_ASSEMBLER) || !defined (BFD)
46733965Sjdp
46833965Sjdp#ifdef BFD_ASSEMBLER
46933965Sjdpstatic void
47033965Sjdpcvt_frag_to_fill (sec, fragP)
47133965Sjdp     segT sec;
47233965Sjdp     fragS *fragP;
47333965Sjdp#else
47433965Sjdpstatic void
47533965Sjdpcvt_frag_to_fill (headersP, sec, fragP)
47633965Sjdp     object_headers *headersP;
47733965Sjdp     segT sec;
47833965Sjdp     fragS *fragP;
47933965Sjdp#endif
48033965Sjdp{
48133965Sjdp  switch (fragP->fr_type)
48233965Sjdp    {
48333965Sjdp    case rs_align:
48433965Sjdp    case rs_align_code:
48533965Sjdp    case rs_org:
48633965Sjdp    case rs_space:
48733965Sjdp#ifdef HANDLE_ALIGN
48833965Sjdp      HANDLE_ALIGN (fragP);
48933965Sjdp#endif
49033965Sjdp      know (fragP->fr_next != NULL);
49133965Sjdp      fragP->fr_offset = (fragP->fr_next->fr_address
49233965Sjdp			  - fragP->fr_address
49333965Sjdp			  - fragP->fr_fix) / fragP->fr_var;
49433965Sjdp      if (fragP->fr_offset < 0)
49533965Sjdp	{
49633965Sjdp	  as_bad ("attempt to .org/.space backwards? (%ld)",
49733965Sjdp		  (long) fragP->fr_offset);
49833965Sjdp	}
49933965Sjdp      fragP->fr_type = rs_fill;
50033965Sjdp      break;
50133965Sjdp
50233965Sjdp    case rs_fill:
50333965Sjdp      break;
50433965Sjdp
50538889Sjdp    case rs_leb128:
50638889Sjdp      {
50738889Sjdp	valueT value = S_GET_VALUE (fragP->fr_symbol);
50838889Sjdp	int size;
50938889Sjdp
51038889Sjdp	size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
51138889Sjdp			      fragP->fr_subtype);
51238889Sjdp
51338889Sjdp	fragP->fr_fix += size;
51438889Sjdp	fragP->fr_type = rs_fill;
51538889Sjdp	fragP->fr_var = 0;
51638889Sjdp	fragP->fr_offset = 0;
51738889Sjdp	fragP->fr_symbol = NULL;
51838889Sjdp      }
51938889Sjdp      break;
52038889Sjdp
52138889Sjdp    case rs_cfa:
52238889Sjdp      eh_frame_convert_frag (fragP);
52338889Sjdp      break;
52438889Sjdp
52533965Sjdp    case rs_machine_dependent:
52633965Sjdp#ifdef BFD_ASSEMBLER
52733965Sjdp      md_convert_frag (stdoutput, sec, fragP);
52833965Sjdp#else
52933965Sjdp      md_convert_frag (headersP, sec, fragP);
53033965Sjdp#endif
53133965Sjdp
53238889Sjdp      assert (fragP->fr_next == NULL
53338889Sjdp	      || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
53438889Sjdp		  == fragP->fr_fix));
53533965Sjdp
53633965Sjdp      /*
53733965Sjdp       * After md_convert_frag, we make the frag into a ".space 0".
53833965Sjdp       * Md_convert_frag() should set up any fixSs and constants
53933965Sjdp       * required.
54033965Sjdp       */
54133965Sjdp      frag_wane (fragP);
54233965Sjdp      break;
54333965Sjdp
54433965Sjdp#ifndef WORKING_DOT_WORD
54533965Sjdp    case rs_broken_word:
54633965Sjdp      {
54733965Sjdp	struct broken_word *lie;
54833965Sjdp
54933965Sjdp	if (fragP->fr_subtype)
55033965Sjdp	  {
55133965Sjdp	    fragP->fr_fix += md_short_jump_size;
55233965Sjdp	    for (lie = (struct broken_word *) (fragP->fr_symbol);
55333965Sjdp		 lie && lie->dispfrag == fragP;
55433965Sjdp		 lie = lie->next_broken_word)
55533965Sjdp	      if (lie->added == 1)
55633965Sjdp		fragP->fr_fix += md_long_jump_size;
55733965Sjdp	  }
55833965Sjdp	frag_wane (fragP);
55933965Sjdp      }
56033965Sjdp      break;
56133965Sjdp#endif
56233965Sjdp
56333965Sjdp    default:
56433965Sjdp      BAD_CASE (fragP->fr_type);
56533965Sjdp      break;
56633965Sjdp    }
56733965Sjdp}
56833965Sjdp
56933965Sjdp#endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
57033965Sjdp
57133965Sjdp#ifdef BFD_ASSEMBLER
57233965Sjdpstatic void
57333965Sjdprelax_and_size_seg (abfd, sec, xxx)
57433965Sjdp     bfd *abfd;
57533965Sjdp     asection *sec;
57633965Sjdp     PTR xxx;
57733965Sjdp{
57833965Sjdp  flagword flags;
57933965Sjdp  fragS *fragp;
58033965Sjdp  segment_info_type *seginfo;
58133965Sjdp  int x;
58233965Sjdp  valueT size, newsize;
58333965Sjdp
58433965Sjdp  subseg_change (sec, 0);
58533965Sjdp
58633965Sjdp  flags = bfd_get_section_flags (abfd, sec);
58733965Sjdp
58833965Sjdp  seginfo = seg_info (sec);
58933965Sjdp  if (seginfo && seginfo->frchainP)
59033965Sjdp    {
59133965Sjdp      relax_segment (seginfo->frchainP->frch_root, sec);
59233965Sjdp      for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
59333965Sjdp	cvt_frag_to_fill (sec, fragp);
59433965Sjdp      for (fragp = seginfo->frchainP->frch_root;
59533965Sjdp	   fragp->fr_next;
59633965Sjdp	   fragp = fragp->fr_next)
59733965Sjdp	/* walk to last elt */;
59833965Sjdp      size = fragp->fr_address + fragp->fr_fix;
59933965Sjdp    }
60033965Sjdp  else
60133965Sjdp    size = 0;
60233965Sjdp
60333965Sjdp  if (size > 0 && ! seginfo->bss)
60433965Sjdp    flags |= SEC_HAS_CONTENTS;
60533965Sjdp
60633965Sjdp  /* @@ This is just an approximation.  */
60733965Sjdp  if (seginfo && seginfo->fix_root)
60833965Sjdp    flags |= SEC_RELOC;
60933965Sjdp  else
61033965Sjdp    flags &= ~SEC_RELOC;
61133965Sjdp  x = bfd_set_section_flags (abfd, sec, flags);
61233965Sjdp  assert (x == true);
61333965Sjdp
61433965Sjdp  newsize = md_section_align (sec, size);
61533965Sjdp  x = bfd_set_section_size (abfd, sec, newsize);
61633965Sjdp  assert (x == true);
61733965Sjdp
61833965Sjdp  /* If the size had to be rounded up, add some padding in the last
61933965Sjdp     non-empty frag.  */
62033965Sjdp  assert (newsize >= size);
62133965Sjdp  if (size != newsize)
62233965Sjdp    {
62333965Sjdp      fragS *last = seginfo->frchainP->frch_last;
62433965Sjdp      fragp = seginfo->frchainP->frch_root;
62533965Sjdp      while (fragp->fr_next != last)
62633965Sjdp	fragp = fragp->fr_next;
62733965Sjdp      last->fr_address = size;
62833965Sjdp      fragp->fr_offset += newsize - size;
62933965Sjdp    }
63033965Sjdp
63133965Sjdp#ifdef tc_frob_section
63233965Sjdp  tc_frob_section (sec);
63333965Sjdp#endif
63433965Sjdp#ifdef obj_frob_section
63533965Sjdp  obj_frob_section (sec);
63633965Sjdp#endif
63733965Sjdp}
63833965Sjdp
63933965Sjdp#ifdef DEBUG2
64033965Sjdpstatic void
64133965Sjdpdump_section_relocs (abfd, sec, stream_)
64233965Sjdp     bfd *abfd;
64333965Sjdp     asection *sec;
64433965Sjdp     char *stream_;
64533965Sjdp{
64633965Sjdp  FILE *stream = (FILE *) stream_;
64733965Sjdp  segment_info_type *seginfo = seg_info (sec);
64833965Sjdp  fixS *fixp = seginfo->fix_root;
64933965Sjdp
65033965Sjdp  if (!fixp)
65133965Sjdp    return;
65233965Sjdp
65333965Sjdp  fprintf (stream, "sec %s relocs:\n", sec->name);
65433965Sjdp  while (fixp)
65533965Sjdp    {
65633965Sjdp      symbolS *s = fixp->fx_addsy;
65733965Sjdp      if (s)
65833965Sjdp	{
65933965Sjdp	  fprintf (stream, "  %08x: %s(%s", fixp, S_GET_NAME (s),
66033965Sjdp		   s->bsym->section->name);
66133965Sjdp	  if (s->bsym->flags & BSF_SECTION_SYM)
66233965Sjdp	    {
66333965Sjdp	      fprintf (stream, " section sym");
66433965Sjdp	      if (S_GET_VALUE (s))
66533965Sjdp		fprintf (stream, "+%x", S_GET_VALUE (s));
66633965Sjdp	    }
66733965Sjdp	  else
66833965Sjdp	    fprintf (stream, "+%x", S_GET_VALUE (s));
66933965Sjdp	  fprintf (stream, ")+%x\n", fixp->fx_offset);
67033965Sjdp	}
67133965Sjdp      else
67233965Sjdp	fprintf (stream, "  %08x: type %d no sym\n", fixp, fixp->fx_r_type);
67333965Sjdp      fixp = fixp->fx_next;
67433965Sjdp    }
67533965Sjdp}
67633965Sjdp#else
67733965Sjdp#define dump_section_relocs(ABFD,SEC,STREAM)	((void) 0)
67833965Sjdp#endif
67933965Sjdp
68033965Sjdp#ifndef EMIT_SECTION_SYMBOLS
68133965Sjdp#define EMIT_SECTION_SYMBOLS 1
68233965Sjdp#endif
68333965Sjdp
68433965Sjdpstatic void
68533965Sjdpadjust_reloc_syms (abfd, sec, xxx)
68633965Sjdp     bfd *abfd;
68733965Sjdp     asection *sec;
68833965Sjdp     PTR xxx;
68933965Sjdp{
69033965Sjdp  segment_info_type *seginfo = seg_info (sec);
69133965Sjdp  fixS *fixp;
69233965Sjdp
69333965Sjdp  if (seginfo == NULL)
69433965Sjdp    return;
69533965Sjdp
69633965Sjdp  dump_section_relocs (abfd, sec, stderr);
69733965Sjdp
69833965Sjdp  for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
69933965Sjdp    if (fixp->fx_done)
70033965Sjdp      /* ignore it */;
70133965Sjdp    else if (fixp->fx_addsy)
70233965Sjdp      {
70333965Sjdp	symbolS *sym;
70433965Sjdp	asection *symsec;
70533965Sjdp
70633965Sjdp#ifdef DEBUG5
70733965Sjdp	fprintf (stderr, "\n\nadjusting fixup:\n");
70833965Sjdp	print_fixup (fixp);
70933965Sjdp#endif
71033965Sjdp
71133965Sjdp	sym = fixp->fx_addsy;
71233965Sjdp
71333965Sjdp	/* All symbols should have already been resolved at this
71433965Sjdp	   point.  It is possible to see unresolved expression
71533965Sjdp	   symbols, though, since they are not in the regular symbol
71633965Sjdp	   table.  */
71733965Sjdp	if (sym != NULL && ! sym->sy_resolved)
71838889Sjdp	  resolve_symbol_value (sym, 1);
71933965Sjdp	if (fixp->fx_subsy != NULL && ! fixp->fx_subsy->sy_resolved)
72038889Sjdp	  resolve_symbol_value (fixp->fx_subsy, 1);
72133965Sjdp
72233965Sjdp	/* If this symbol is equated to an undefined symbol, convert
72333965Sjdp           the fixup to being against that symbol.  */
72433965Sjdp	if (sym->sy_value.X_op == O_symbol
72533965Sjdp	    && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
72633965Sjdp	  {
72733965Sjdp	    fixp->fx_offset += sym->sy_value.X_add_number;
72833965Sjdp	    sym = sym->sy_value.X_add_symbol;
72933965Sjdp	    fixp->fx_addsy = sym;
73033965Sjdp	  }
73133965Sjdp
73233965Sjdp	symsec = S_GET_SEGMENT (sym);
73333965Sjdp
73433965Sjdp	if (sym != NULL && sym->sy_mri_common)
73533965Sjdp	  {
73633965Sjdp	    /* These symbols are handled specially in fixup_segment.  */
73733965Sjdp	    goto done;
73833965Sjdp	  }
73933965Sjdp
74033965Sjdp	if (bfd_is_abs_section (symsec))
74133965Sjdp	  {
74233965Sjdp	    /* The fixup_segment routine will not use this symbol in a
74333965Sjdp               relocation unless TC_FORCE_RELOCATION returns 1.  */
74433965Sjdp	    if (TC_FORCE_RELOCATION (fixp))
74533965Sjdp	      {
74633965Sjdp		fixp->fx_addsy->sy_used_in_reloc = 1;
74733965Sjdp#ifdef UNDEFINED_DIFFERENCE_OK
74833965Sjdp		if (fixp->fx_subsy != NULL)
74933965Sjdp		  fixp->fx_subsy->sy_used_in_reloc = 1;
75033965Sjdp#endif
75133965Sjdp	      }
75233965Sjdp	    goto done;
75333965Sjdp	  }
75433965Sjdp
75533965Sjdp	/* If it's one of these sections, assume the symbol is
75633965Sjdp	   definitely going to be output.  The code in
75733965Sjdp	   md_estimate_size_before_relax in tc-mips.c uses this test
75833965Sjdp	   as well, so if you change this code you should look at that
75933965Sjdp	   code.  */
76033965Sjdp	if (bfd_is_und_section (symsec)
76133965Sjdp	    || bfd_is_com_section (symsec))
76233965Sjdp	  {
76333965Sjdp	    fixp->fx_addsy->sy_used_in_reloc = 1;
76433965Sjdp#ifdef UNDEFINED_DIFFERENCE_OK
76533965Sjdp	    /* We have the difference of an undefined symbol and some
76633965Sjdp	       other symbol.  Make sure to mark the other symbol as used
76733965Sjdp	       in a relocation so that it will always be output.  */
76833965Sjdp	    if (fixp->fx_subsy)
76933965Sjdp	      fixp->fx_subsy->sy_used_in_reloc = 1;
77033965Sjdp#endif
77133965Sjdp	    goto done;
77233965Sjdp	  }
77333965Sjdp
77433965Sjdp	/* Don't try to reduce relocs which refer to .linkonce
77533965Sjdp           sections.  It can lead to confusion when a debugging
77633965Sjdp           section refers to a .linkonce section.  I hope this will
77733965Sjdp           always be correct.  */
77833965Sjdp	if (symsec != sec)
77933965Sjdp	  {
78033965Sjdp	    boolean linkonce;
78133965Sjdp
78233965Sjdp	    linkonce = false;
78333965Sjdp#ifdef BFD_ASSEMBLER
78433965Sjdp	    if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
78533965Sjdp		!= 0)
78633965Sjdp	      linkonce = true;
78733965Sjdp#endif
78833965Sjdp#ifdef OBJ_ELF
78933965Sjdp	    /* The GNU toolchain uses an extension for ELF: a section
79033965Sjdp               beginning with the magic string .gnu.linkonce is a
79133965Sjdp               linkonce section.  */
79233965Sjdp	    if (strncmp (segment_name (symsec), ".gnu.linkonce",
79333965Sjdp			 sizeof ".gnu.linkonce" - 1) == 0)
79433965Sjdp	      linkonce = true;
79533965Sjdp#endif
79633965Sjdp
79733965Sjdp	    if (linkonce)
79833965Sjdp	      {
79933965Sjdp		fixp->fx_addsy->sy_used_in_reloc = 1;
80033965Sjdp#ifdef UNDEFINED_DIFFERENCE_OK
80133965Sjdp		if (fixp->fx_subsy != NULL)
80233965Sjdp		  fixp->fx_subsy->sy_used_in_reloc = 1;
80333965Sjdp#endif
80433965Sjdp		goto done;
80533965Sjdp	      }
80633965Sjdp	  }
80733965Sjdp
80833965Sjdp	/* Since we're reducing to section symbols, don't attempt to reduce
80933965Sjdp	   anything that's already using one.  */
81033965Sjdp	if (sym->bsym->flags & BSF_SECTION_SYM)
81133965Sjdp	  {
81233965Sjdp	    fixp->fx_addsy->sy_used_in_reloc = 1;
81333965Sjdp	    goto done;
81433965Sjdp	  }
81533965Sjdp
81633965Sjdp	/* Is there some other reason we can't adjust this one?  (E.g.,
81733965Sjdp	   call/bal links in i960-bout symbols.)  */
81833965Sjdp#ifdef obj_fix_adjustable
81933965Sjdp	if (! obj_fix_adjustable (fixp))
82033965Sjdp	  {
82133965Sjdp	    fixp->fx_addsy->sy_used_in_reloc = 1;
82233965Sjdp	    goto done;
82333965Sjdp	  }
82433965Sjdp#endif
82533965Sjdp
82633965Sjdp	/* Is there some other (target cpu dependent) reason we can't adjust
82733965Sjdp	   this one?  (E.g. relocations involving function addresses on
82833965Sjdp	   the PA.  */
82933965Sjdp#ifdef tc_fix_adjustable
83033965Sjdp	if (! tc_fix_adjustable (fixp))
83133965Sjdp	  {
83233965Sjdp	    fixp->fx_addsy->sy_used_in_reloc = 1;
83333965Sjdp	    goto done;
83433965Sjdp	  }
83533965Sjdp#endif
83633965Sjdp
83733965Sjdp	/* If the section symbol isn't going to be output, the relocs
83833965Sjdp	   at least should still work.  If not, figure out what to do
83933965Sjdp	   when we run into that case.
84033965Sjdp
84133965Sjdp	   We refetch the segment when calling section_symbol, rather
84233965Sjdp	   than using symsec, because S_GET_VALUE may wind up changing
84333965Sjdp	   the section when it calls resolve_symbol_value. */
84433965Sjdp	fixp->fx_offset += S_GET_VALUE (sym);
84533965Sjdp	fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
84633965Sjdp	fixp->fx_addsy->sy_used_in_reloc = 1;
84733965Sjdp
84833965Sjdp      done:
84933965Sjdp	;
85033965Sjdp      }
85133965Sjdp#if 1/*def RELOC_REQUIRES_SYMBOL*/
85233965Sjdp    else
85333965Sjdp      {
85433965Sjdp	/* There was no symbol required by this relocation.  However,
85533965Sjdp	   BFD doesn't really handle relocations without symbols well.
85633965Sjdp	   (At least, the COFF support doesn't.)  So for now we fake up
85733965Sjdp	   a local symbol in the absolute section.  */
85833965Sjdp
85933965Sjdp	fixp->fx_addsy = section_symbol (absolute_section);
86033965Sjdp/*	fixp->fx_addsy->sy_used_in_reloc = 1; */
86133965Sjdp      }
86233965Sjdp#endif
86333965Sjdp
86433965Sjdp  dump_section_relocs (abfd, sec, stderr);
86533965Sjdp}
86633965Sjdp
86733965Sjdpstatic void
86833965Sjdpwrite_relocs (abfd, sec, xxx)
86933965Sjdp     bfd *abfd;
87033965Sjdp     asection *sec;
87133965Sjdp     PTR xxx;
87233965Sjdp{
87333965Sjdp  segment_info_type *seginfo = seg_info (sec);
87433965Sjdp  int i;
87533965Sjdp  unsigned int n;
87633965Sjdp  arelent **relocs;
87733965Sjdp  fixS *fixp;
87833965Sjdp  char *err;
87933965Sjdp
88033965Sjdp  /* If seginfo is NULL, we did not create this section; don't do
88133965Sjdp     anything with it.  */
88233965Sjdp  if (seginfo == NULL)
88333965Sjdp    return;
88433965Sjdp
88533965Sjdp  fixup_segment (seginfo->fix_root, sec);
88633965Sjdp
88733965Sjdp  n = 0;
88833965Sjdp  for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
88933965Sjdp    n++;
89033965Sjdp
89133965Sjdp#ifndef RELOC_EXPANSION_POSSIBLE
89233965Sjdp  /* Set up reloc information as well.  */
89333965Sjdp  relocs = (arelent **) xmalloc (n * sizeof (arelent *));
89433965Sjdp  memset ((char*)relocs, 0, n * sizeof (arelent*));
89533965Sjdp
89633965Sjdp  i = 0;
89733965Sjdp  for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
89833965Sjdp    {
89933965Sjdp      arelent *reloc;
90033965Sjdp      bfd_reloc_status_type s;
90133965Sjdp      symbolS *sym;
90233965Sjdp
90333965Sjdp      if (fixp->fx_done)
90433965Sjdp	{
90533965Sjdp	  n--;
90633965Sjdp	  continue;
90733965Sjdp	}
90833965Sjdp
90933965Sjdp      /* If this is an undefined symbol which was equated to another
91033965Sjdp         symbol, then use generate the reloc against the latter symbol
91133965Sjdp         rather than the former.  */
91233965Sjdp      sym = fixp->fx_addsy;
91333965Sjdp      while (sym->sy_value.X_op == O_symbol
91433965Sjdp	     && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
91533965Sjdp	{
91633965Sjdp	  symbolS *n;
91733965Sjdp
91833965Sjdp	  /* We must avoid looping, as that can occur with a badly
91933965Sjdp	     written program.  */
92033965Sjdp	  n = sym->sy_value.X_add_symbol;
92133965Sjdp	  if (n == sym)
92233965Sjdp	    break;
92333965Sjdp	  fixp->fx_offset += sym->sy_value.X_add_number;
92433965Sjdp	  sym = n;
92533965Sjdp	}
92633965Sjdp      fixp->fx_addsy = sym;
92733965Sjdp
92833965Sjdp      reloc = tc_gen_reloc (sec, fixp);
92933965Sjdp      if (!reloc)
93033965Sjdp	{
93133965Sjdp	  n--;
93233965Sjdp	  continue;
93333965Sjdp	}
93433965Sjdp
93533965Sjdp#if 0
93633965Sjdp      /* This test is triggered inappropriately for the SH.  */
93733965Sjdp      if (fixp->fx_where + fixp->fx_size
93833965Sjdp	  > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
93933965Sjdp	abort ();
94033965Sjdp#endif
94133965Sjdp
94233965Sjdp      s = bfd_install_relocation (stdoutput, reloc,
94333965Sjdp				  fixp->fx_frag->fr_literal,
94433965Sjdp				  fixp->fx_frag->fr_address,
94533965Sjdp				  sec, &err);
94633965Sjdp      switch (s)
94733965Sjdp	{
94833965Sjdp	case bfd_reloc_ok:
94933965Sjdp	  break;
95033965Sjdp	case bfd_reloc_overflow:
95133965Sjdp	  as_bad_where (fixp->fx_file, fixp->fx_line, "relocation overflow");
95233965Sjdp	  break;
95333965Sjdp	default:
95433965Sjdp	  as_fatal ("%s:%u: bad return from bfd_install_relocation",
95533965Sjdp		    fixp->fx_file, fixp->fx_line);
95633965Sjdp	}
95733965Sjdp      relocs[i++] = reloc;
95833965Sjdp    }
95933965Sjdp#else
96033965Sjdp  n = n * MAX_RELOC_EXPANSION;
96133965Sjdp  /* Set up reloc information as well.  */
96233965Sjdp  relocs = (arelent **) xmalloc (n * sizeof (arelent *));
96333965Sjdp
96433965Sjdp  i = 0;
96533965Sjdp  for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
96633965Sjdp    {
96733965Sjdp      arelent **reloc;
96833965Sjdp      char *data;
96933965Sjdp      bfd_reloc_status_type s;
97033965Sjdp      symbolS *sym;
97133965Sjdp      int j;
97233965Sjdp
97333965Sjdp      if (fixp->fx_done)
97433965Sjdp	{
97533965Sjdp	  n--;
97633965Sjdp	  continue;
97733965Sjdp	}
97833965Sjdp
97933965Sjdp      /* If this is an undefined symbol which was equated to another
98033965Sjdp         symbol, then use generate the reloc against the latter symbol
98133965Sjdp         rather than the former.  */
98233965Sjdp      sym = fixp->fx_addsy;
98333965Sjdp      while (sym->sy_value.X_op == O_symbol
98433965Sjdp	     && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
98533965Sjdp	sym = sym->sy_value.X_add_symbol;
98633965Sjdp      fixp->fx_addsy = sym;
98733965Sjdp
98833965Sjdp      reloc = tc_gen_reloc (sec, fixp);
98933965Sjdp
99033965Sjdp      for (j = 0; reloc[j]; j++)
99133965Sjdp	{
99233965Sjdp          relocs[i++] = reloc[j];
99333965Sjdp          assert(i <= n);
99433965Sjdp	}
99533965Sjdp      data = fixp->fx_frag->fr_literal + fixp->fx_where;
99633965Sjdp      if (fixp->fx_where + fixp->fx_size
99733965Sjdp	  > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
99833965Sjdp	as_bad_where (fixp->fx_file, fixp->fx_line,
99933965Sjdp		      "internal error: fixup not contained within frag");
100033965Sjdp      for (j = 0; reloc[j]; j++)
100133965Sjdp        {
100233965Sjdp	  s = bfd_install_relocation (stdoutput, reloc[j],
100333965Sjdp				      fixp->fx_frag->fr_literal,
100433965Sjdp				      fixp->fx_frag->fr_address,
100533965Sjdp				      sec, &err);
100633965Sjdp          switch (s)
100733965Sjdp	    {
100833965Sjdp	    case bfd_reloc_ok:
100933965Sjdp	      break;
101033965Sjdp	    case bfd_reloc_overflow:
101133965Sjdp	      as_bad_where (fixp->fx_file, fixp->fx_line,
101233965Sjdp			    "relocation overflow");
101333965Sjdp	      break;
101433965Sjdp	    default:
101533965Sjdp	      as_fatal ("%s:%u: bad return from bfd_install_relocation",
101633965Sjdp			fixp->fx_file, fixp->fx_line);
101733965Sjdp	    }
101833965Sjdp        }
101933965Sjdp    }
102033965Sjdp  n = i;
102133965Sjdp#endif
102233965Sjdp
102333965Sjdp#ifdef DEBUG4
102433965Sjdp  {
102533965Sjdp    int i, j, nsyms;
102633965Sjdp    asymbol **sympp;
102733965Sjdp    sympp = bfd_get_outsymbols (stdoutput);
102833965Sjdp    nsyms = bfd_get_symcount (stdoutput);
102933965Sjdp    for (i = 0; i < n; i++)
103033965Sjdp      if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
103133965Sjdp	{
103233965Sjdp	  for (j = 0; j < nsyms; j++)
103333965Sjdp	    if (sympp[j] == *relocs[i]->sym_ptr_ptr)
103433965Sjdp	      break;
103533965Sjdp	  if (j == nsyms)
103633965Sjdp	    abort ();
103733965Sjdp	}
103833965Sjdp  }
103933965Sjdp#endif
104033965Sjdp
104133965Sjdp  if (n)
104233965Sjdp    bfd_set_reloc (stdoutput, sec, relocs, n);
104333965Sjdp  else
104433965Sjdp    bfd_set_section_flags (abfd, sec,
104533965Sjdp			   (bfd_get_section_flags (abfd, sec)
104633965Sjdp			    & (flagword) ~SEC_RELOC));
104733965Sjdp
104833965Sjdp#ifdef DEBUG3
104933965Sjdp  {
105033965Sjdp    int i;
105133965Sjdp    arelent *r;
105233965Sjdp    asymbol *s;
105333965Sjdp    fprintf (stderr, "relocs for sec %s\n", sec->name);
105433965Sjdp    for (i = 0; i < n; i++)
105533965Sjdp      {
105633965Sjdp	r = relocs[i];
105733965Sjdp	s = *r->sym_ptr_ptr;
105833965Sjdp	fprintf (stderr, "  reloc %2d @%08x off %4x : sym %-10s addend %x\n",
105933965Sjdp		 i, r, r->address, s->name, r->addend);
106033965Sjdp      }
106133965Sjdp  }
106233965Sjdp#endif
106333965Sjdp}
106433965Sjdp
106533965Sjdpstatic void
106633965Sjdpwrite_contents (abfd, sec, xxx)
106733965Sjdp     bfd *abfd;
106833965Sjdp     asection *sec;
106933965Sjdp     PTR xxx;
107033965Sjdp{
107133965Sjdp  segment_info_type *seginfo = seg_info (sec);
107233965Sjdp  unsigned long offset = 0;
107333965Sjdp  fragS *f;
107433965Sjdp
107533965Sjdp  /* Write out the frags.  */
107633965Sjdp  if (seginfo == NULL
107733965Sjdp      || ! (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
107833965Sjdp    return;
107933965Sjdp
108033965Sjdp  for (f = seginfo->frchainP->frch_root;
108133965Sjdp       f;
108233965Sjdp       f = f->fr_next)
108333965Sjdp    {
108433965Sjdp      int x;
108533965Sjdp      unsigned long fill_size;
108633965Sjdp      char *fill_literal;
108733965Sjdp      long count;
108833965Sjdp
108933965Sjdp      assert (f->fr_type == rs_fill);
109033965Sjdp      if (f->fr_fix)
109133965Sjdp	{
109233965Sjdp	  x = bfd_set_section_contents (stdoutput, sec,
109333965Sjdp					f->fr_literal, (file_ptr) offset,
109433965Sjdp					(bfd_size_type) f->fr_fix);
109533965Sjdp	  if (x == false)
109633965Sjdp	    {
109733965Sjdp	      bfd_perror (stdoutput->filename);
109833965Sjdp	      as_perror ("FATAL: Can't write %s", stdoutput->filename);
109933965Sjdp	      exit (EXIT_FAILURE);
110033965Sjdp	    }
110133965Sjdp	  offset += f->fr_fix;
110233965Sjdp	}
110333965Sjdp      fill_literal = f->fr_literal + f->fr_fix;
110433965Sjdp      fill_size = f->fr_var;
110533965Sjdp      count = f->fr_offset;
110633965Sjdp      assert (count >= 0);
110733965Sjdp      if (fill_size && count)
110833965Sjdp	{
110933965Sjdp	  char buf[256];
111033965Sjdp	  if (fill_size > sizeof(buf))
111133965Sjdp	    {
111233965Sjdp	      /* Do it the old way. Can this ever happen? */
111333965Sjdp	      while (count--)
111433965Sjdp		{
111533965Sjdp		  x = bfd_set_section_contents (stdoutput, sec,
111633965Sjdp						fill_literal,
111733965Sjdp						(file_ptr) offset,
111833965Sjdp						(bfd_size_type) fill_size);
111933965Sjdp		  if (x == false)
112033965Sjdp		    {
112133965Sjdp		      bfd_perror (stdoutput->filename);
112233965Sjdp		      as_perror ("FATAL: Can't write %s", stdoutput->filename);
112333965Sjdp		      exit (EXIT_FAILURE);
112433965Sjdp		    }
112533965Sjdp		  offset += fill_size;
112633965Sjdp		}
112733965Sjdp	    }
112833965Sjdp	  else
112933965Sjdp	    {
113033965Sjdp	      /* Build a buffer full of fill objects and output it as
113133965Sjdp		 often as necessary. This saves on the overhead of
113233965Sjdp		 potentially lots of bfd_set_section_contents calls.  */
113333965Sjdp	      int n_per_buf, i;
113433965Sjdp	      if (fill_size == 1)
113533965Sjdp		{
113633965Sjdp		  n_per_buf = sizeof (buf);
113733965Sjdp		  memset (buf, *fill_literal, n_per_buf);
113833965Sjdp		}
113933965Sjdp	      else
114033965Sjdp		{
114133965Sjdp		  char *bufp;
114233965Sjdp		  n_per_buf = sizeof(buf)/fill_size;
114333965Sjdp		  for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
114433965Sjdp		    memcpy(bufp, fill_literal, fill_size);
114533965Sjdp		}
114633965Sjdp	      for (; count > 0; count -= n_per_buf)
114733965Sjdp		{
114833965Sjdp		  n_per_buf = n_per_buf > count ? count : n_per_buf;
114933965Sjdp		  x = bfd_set_section_contents (stdoutput, sec,
115033965Sjdp						buf, (file_ptr) offset,
115133965Sjdp						(bfd_size_type) n_per_buf * fill_size);
115233965Sjdp		  if (x != true)
115333965Sjdp		    as_fatal ("Cannot write to output file.");
115433965Sjdp		  offset += n_per_buf * fill_size;
115533965Sjdp		}
115633965Sjdp	    }
115733965Sjdp	}
115833965Sjdp    }
115933965Sjdp}
116033965Sjdp#endif
116133965Sjdp
116233965Sjdp#if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
116333965Sjdpstatic void
116433965Sjdpmerge_data_into_text ()
116533965Sjdp{
116633965Sjdp#if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
116733965Sjdp  seg_info (text_section)->frchainP->frch_last->fr_next =
116833965Sjdp    seg_info (data_section)->frchainP->frch_root;
116933965Sjdp  seg_info (text_section)->frchainP->frch_last =
117033965Sjdp    seg_info (data_section)->frchainP->frch_last;
117133965Sjdp  seg_info (data_section)->frchainP = 0;
117233965Sjdp#else
117333965Sjdp  fixS *tmp;
117433965Sjdp
117533965Sjdp  text_last_frag->fr_next = data_frag_root;
117633965Sjdp  text_last_frag = data_last_frag;
117733965Sjdp  data_last_frag = NULL;
117833965Sjdp  data_frag_root = NULL;
117933965Sjdp  if (text_fix_root)
118033965Sjdp    {
118133965Sjdp      for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
118233965Sjdp      tmp->fx_next = data_fix_root;
118333965Sjdp      text_fix_tail = data_fix_tail;
118433965Sjdp    }
118533965Sjdp  else
118633965Sjdp    text_fix_root = data_fix_root;
118733965Sjdp  data_fix_root = NULL;
118833965Sjdp#endif
118933965Sjdp}
119033965Sjdp#endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
119133965Sjdp
119233965Sjdp#if !defined (BFD_ASSEMBLER) && !defined (BFD)
119333965Sjdpstatic void
119433965Sjdprelax_and_size_all_segments ()
119533965Sjdp{
119633965Sjdp  fragS *fragP;
119733965Sjdp
119833965Sjdp  relax_segment (text_frag_root, SEG_TEXT);
119933965Sjdp  relax_segment (data_frag_root, SEG_DATA);
120033965Sjdp  relax_segment (bss_frag_root, SEG_BSS);
120133965Sjdp  /*
120233965Sjdp   * Now the addresses of frags are correct within the segment.
120333965Sjdp   */
120433965Sjdp
120533965Sjdp  know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
120633965Sjdp  H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
120733965Sjdp  text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
120833965Sjdp
120933965Sjdp  /*
121033965Sjdp   * Join the 2 segments into 1 huge segment.
121133965Sjdp   * To do this, re-compute every rn_address in the SEG_DATA frags.
121233965Sjdp   * Then join the data frags after the text frags.
121333965Sjdp   *
121433965Sjdp   * Determine a_data [length of data segment].
121533965Sjdp   */
121633965Sjdp  if (data_frag_root)
121733965Sjdp    {
121833965Sjdp      register relax_addressT slide;
121933965Sjdp
122033965Sjdp      know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
122133965Sjdp
122233965Sjdp      H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
122333965Sjdp      data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
122433965Sjdp      slide = H_GET_TEXT_SIZE (&headers);	/* & in file of the data segment. */
122533965Sjdp#ifdef OBJ_BOUT
122633965Sjdp#define RoundUp(N,S) (((N)+(S)-1)&-(S))
122733965Sjdp      /* For b.out: If the data section has a strict alignment
122833965Sjdp	 requirement, its load address in the .o file will be
122933965Sjdp	 rounded up from the size of the text section.  These
123033965Sjdp	 two values are *not* the same!  Similarly for the bss
123133965Sjdp	 section....  */
123233965Sjdp      slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
123333965Sjdp#endif
123433965Sjdp
123533965Sjdp      for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
123633965Sjdp	{
123733965Sjdp	  fragP->fr_address += slide;
123833965Sjdp	}			/* for each data frag */
123933965Sjdp
124033965Sjdp      know (text_last_frag != 0);
124133965Sjdp      text_last_frag->fr_next = data_frag_root;
124233965Sjdp    }
124333965Sjdp  else
124433965Sjdp    {
124533965Sjdp      H_SET_DATA_SIZE (&headers, 0);
124633965Sjdp    }
124733965Sjdp
124833965Sjdp#ifdef OBJ_BOUT
124933965Sjdp  /* See above comments on b.out data section address.  */
125033965Sjdp  {
125133965Sjdp    long bss_vma;
125233965Sjdp    if (data_last_frag == 0)
125333965Sjdp      bss_vma = H_GET_TEXT_SIZE (&headers);
125433965Sjdp    else
125533965Sjdp      bss_vma = data_last_frag->fr_address;
125633965Sjdp    bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
125733965Sjdp    bss_address_frag.fr_address = bss_vma;
125833965Sjdp  }
125933965Sjdp#else /* ! OBJ_BOUT */
126033965Sjdp  bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
126133965Sjdp				 H_GET_DATA_SIZE (&headers));
126233965Sjdp
126333965Sjdp#endif /* ! OBJ_BOUT */
126433965Sjdp
126533965Sjdp  /* Slide all the frags */
126633965Sjdp  if (bss_frag_root)
126733965Sjdp    {
126833965Sjdp      relax_addressT slide = bss_address_frag.fr_address;
126933965Sjdp
127033965Sjdp      for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
127133965Sjdp	{
127233965Sjdp	  fragP->fr_address += slide;
127333965Sjdp	}			/* for each bss frag */
127433965Sjdp    }
127533965Sjdp
127633965Sjdp  if (bss_last_frag)
127733965Sjdp    H_SET_BSS_SIZE (&headers,
127833965Sjdp		    bss_last_frag->fr_address - bss_frag_root->fr_address);
127933965Sjdp  else
128033965Sjdp    H_SET_BSS_SIZE (&headers, 0);
128133965Sjdp}
128233965Sjdp#endif /* ! BFD_ASSEMBLER && ! BFD */
128333965Sjdp
128433965Sjdp#if defined (BFD_ASSEMBLER) || !defined (BFD)
128533965Sjdp
128633965Sjdp#ifdef BFD_ASSEMBLER
128733965Sjdpstatic void
128833965Sjdpset_symtab ()
128933965Sjdp{
129033965Sjdp  int nsyms;
129133965Sjdp  asymbol **asympp;
129233965Sjdp  symbolS *symp;
129333965Sjdp  boolean result;
129433965Sjdp  extern PTR bfd_alloc PARAMS ((bfd *, size_t));
129533965Sjdp
129633965Sjdp  /* Count symbols.  We can't rely on a count made by the loop in
129733965Sjdp     write_object_file, because *_frob_file may add a new symbol or
129833965Sjdp     two.  */
129933965Sjdp  nsyms = 0;
130033965Sjdp  for (symp = symbol_rootP; symp; symp = symbol_next (symp))
130133965Sjdp    nsyms++;
130233965Sjdp
130333965Sjdp  if (nsyms)
130433965Sjdp    {
130533965Sjdp      int i;
130633965Sjdp
130733965Sjdp      asympp = (asymbol **) bfd_alloc (stdoutput,
130833965Sjdp				       nsyms * sizeof (asymbol *));
130933965Sjdp      symp = symbol_rootP;
131033965Sjdp      for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
131133965Sjdp	{
131233965Sjdp	  asympp[i] = symp->bsym;
131333965Sjdp	  symp->written = 1;
131433965Sjdp	}
131533965Sjdp    }
131633965Sjdp  else
131733965Sjdp    asympp = 0;
131833965Sjdp  result = bfd_set_symtab (stdoutput, asympp, nsyms);
131933965Sjdp  assert (result == true);
132033965Sjdp  symbol_table_frozen = 1;
132133965Sjdp}
132233965Sjdp#endif
132333965Sjdp
132438889Sjdp/* Finish the subsegments.  After every sub-segment, we fake an
132538889Sjdp   ".align ...".  This conforms to BSD4.2 brane-damage.  We then fake
132638889Sjdp   ".fill 0" because that is the kind of frag that requires least
132738889Sjdp   thought.  ".align" frags like to have a following frag since that
132838889Sjdp   makes calculating their intended length trivial.  */
132938889Sjdp
133038889Sjdp#ifndef SUB_SEGMENT_ALIGN
133138889Sjdp#ifdef BFD_ASSEMBLER
133238889Sjdp#define SUB_SEGMENT_ALIGN(SEG) (0)
133338889Sjdp#else
133438889Sjdp#define SUB_SEGMENT_ALIGN(SEG) (2)
133538889Sjdp#endif
133638889Sjdp#endif
133738889Sjdp
133833965Sjdpvoid
133938889Sjdpsubsegs_finish ()
134038889Sjdp{
134138889Sjdp  struct frchain *frchainP;
134238889Sjdp
134338889Sjdp  for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
134438889Sjdp    {
134538889Sjdp      subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
134638889Sjdp      frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE, 0);
134738889Sjdp
134838889Sjdp      /* frag_align will have left a new frag.
134938889Sjdp	 Use this last frag for an empty ".fill".
135038889Sjdp
135138889Sjdp	 For this segment ...
135238889Sjdp	 Create a last frag. Do not leave a "being filled in frag".  */
135338889Sjdp
135438889Sjdp      frag_wane (frag_now);
135538889Sjdp      frag_now->fr_fix = 0;
135638889Sjdp      know (frag_now->fr_next == NULL);
135738889Sjdp    }
135838889Sjdp}
135938889Sjdp
136038889Sjdp/* Write the object file.  */
136138889Sjdp
136238889Sjdpvoid
136333965Sjdpwrite_object_file ()
136433965Sjdp{
136533965Sjdp#if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
136633965Sjdp  fragS *fragP;			/* Track along all frags. */
136733965Sjdp#endif
136833965Sjdp
136933965Sjdp  /* Do we really want to write it?  */
137033965Sjdp  {
137133965Sjdp    int n_warns, n_errs;
137233965Sjdp    n_warns = had_warnings ();
137333965Sjdp    n_errs = had_errors ();
137433965Sjdp    /* The -Z flag indicates that an object file should be generated,
137533965Sjdp       regardless of warnings and errors.  */
137633965Sjdp    if (flag_always_generate_output)
137733965Sjdp      {
137833965Sjdp	if (n_warns || n_errs)
137933965Sjdp	  as_warn ("%d error%s, %d warning%s, generating bad object file.\n",
138033965Sjdp		   n_errs, n_errs == 1 ? "" : "s",
138133965Sjdp		   n_warns, n_warns == 1 ? "" : "s");
138233965Sjdp      }
138333965Sjdp    else
138433965Sjdp      {
138533965Sjdp	if (n_errs)
138633965Sjdp	  as_fatal ("%d error%s, %d warning%s, no object file generated.\n",
138733965Sjdp		    n_errs, n_errs == 1 ? "" : "s",
138833965Sjdp		    n_warns, n_warns == 1 ? "" : "s");
138933965Sjdp      }
139033965Sjdp  }
139133965Sjdp
139233965Sjdp#ifdef	OBJ_VMS
139333965Sjdp  /* Under VMS we try to be compatible with VAX-11 "C".  Thus, we call
139433965Sjdp     a routine to check for the definition of the procedure "_main",
139533965Sjdp     and if so -- fix it up so that it can be program entry point. */
139633965Sjdp  vms_check_for_main ();
139733965Sjdp#endif /* OBJ_VMS */
139833965Sjdp
139933965Sjdp  /* From now on, we don't care about sub-segments.  Build one frag chain
140033965Sjdp     for each segment. Linked thru fr_next.  */
140133965Sjdp
140233965Sjdp#ifdef BFD_ASSEMBLER
140333965Sjdp  /* Remove the sections created by gas for its own purposes.  */
140433965Sjdp  {
140533965Sjdp    asection **seclist, *sec;
140633965Sjdp    int i;
140733965Sjdp
140833965Sjdp    seclist = &stdoutput->sections;
140933965Sjdp    while (seclist && *seclist)
141033965Sjdp      {
141133965Sjdp	sec = *seclist;
141233965Sjdp	while (sec == reg_section || sec == expr_section)
141333965Sjdp	  {
141433965Sjdp	    sec = sec->next;
141533965Sjdp	    *seclist = sec;
141633965Sjdp	    stdoutput->section_count--;
141733965Sjdp	    if (!sec)
141833965Sjdp	      break;
141933965Sjdp	  }
142033965Sjdp	if (*seclist)
142133965Sjdp	  seclist = &(*seclist)->next;
142233965Sjdp      }
142333965Sjdp    i = 0;
142433965Sjdp    bfd_map_over_sections (stdoutput, renumber_sections, &i);
142533965Sjdp  }
142633965Sjdp
142733965Sjdp  bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
142833965Sjdp#else
142933965Sjdp  remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
143033965Sjdp  remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
143133965Sjdp  remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
143233965Sjdp#endif
143333965Sjdp
143433965Sjdp  /* We have two segments. If user gave -R flag, then we must put the
143533965Sjdp     data frags into the text segment. Do this before relaxing so
143633965Sjdp     we know to take advantage of -R and make shorter addresses.  */
143733965Sjdp#if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
143833965Sjdp  if (flag_readonly_data_in_text)
143933965Sjdp    {
144033965Sjdp      merge_data_into_text ();
144133965Sjdp    }
144233965Sjdp#endif
144333965Sjdp
144433965Sjdp#ifdef BFD_ASSEMBLER
144533965Sjdp  bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
144633965Sjdp#else
144733965Sjdp  relax_and_size_all_segments ();
144833965Sjdp#endif /* BFD_ASSEMBLER */
144933965Sjdp
145033965Sjdp#ifndef BFD_ASSEMBLER
145133965Sjdp  /*
145233965Sjdp   *
145333965Sjdp   * Crawl the symbol chain.
145433965Sjdp   *
145533965Sjdp   * For each symbol whose value depends on a frag, take the address of
145633965Sjdp   * that frag and subsume it into the value of the symbol.
145733965Sjdp   * After this, there is just one way to lookup a symbol value.
145833965Sjdp   * Values are left in their final state for object file emission.
145933965Sjdp   * We adjust the values of 'L' local symbols, even if we do
146033965Sjdp   * not intend to emit them to the object file, because their values
146133965Sjdp   * are needed for fix-ups.
146233965Sjdp   *
146333965Sjdp   * Unless we saw a -L flag, remove all symbols that begin with 'L'
146433965Sjdp   * from the symbol chain.  (They are still pointed to by the fixes.)
146533965Sjdp   *
146633965Sjdp   * Count the remaining symbols.
146733965Sjdp   * Assign a symbol number to each symbol.
146833965Sjdp   * Count the number of string-table chars we will emit.
146933965Sjdp   * Put this info into the headers as appropriate.
147033965Sjdp   *
147133965Sjdp   */
147233965Sjdp  know (zero_address_frag.fr_address == 0);
147333965Sjdp  string_byte_count = sizeof (string_byte_count);
147433965Sjdp
147533965Sjdp  obj_crawl_symbol_chain (&headers);
147633965Sjdp
147733965Sjdp  if (string_byte_count == sizeof (string_byte_count))
147833965Sjdp    string_byte_count = 0;
147933965Sjdp
148033965Sjdp  H_SET_STRING_SIZE (&headers, string_byte_count);
148133965Sjdp
148233965Sjdp  /*
148333965Sjdp   * Addresses of frags now reflect addresses we use in the object file.
148433965Sjdp   * Symbol values are correct.
148533965Sjdp   * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
148633965Sjdp   * Also converting any machine-dependent frags using md_convert_frag();
148733965Sjdp   */
148833965Sjdp  subseg_change (SEG_TEXT, 0);
148933965Sjdp
149033965Sjdp  for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
149133965Sjdp    {
149238889Sjdp      /* At this point we have linked all the frags into a single
149338889Sjdp         chain.  However, cvt_frag_to_fill may call md_convert_frag
149438889Sjdp         which may call fix_new.  We need to ensure that fix_new adds
149538889Sjdp         the fixup to the right section.  */
149638889Sjdp      if (fragP == data_frag_root)
149738889Sjdp	subseg_change (SEG_DATA, 0);
149838889Sjdp
149933965Sjdp      cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
150033965Sjdp
150133965Sjdp      /* Some assert macros don't work with # directives mixed in.  */
150233965Sjdp#ifndef NDEBUG
150333965Sjdp      if (!(fragP->fr_next == NULL
150433965Sjdp#ifdef OBJ_BOUT
150533965Sjdp	    || fragP->fr_next == data_frag_root
150633965Sjdp#endif
150733965Sjdp	    || ((fragP->fr_next->fr_address - fragP->fr_address)
150833965Sjdp		== (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
150933965Sjdp	abort ();
151033965Sjdp#endif
151133965Sjdp    }
151233965Sjdp#endif /* ! BFD_ASSEMBLER */
151333965Sjdp
151433965Sjdp#ifndef WORKING_DOT_WORD
151533965Sjdp  {
151633965Sjdp    struct broken_word *lie;
151733965Sjdp    struct broken_word **prevP;
151833965Sjdp
151933965Sjdp    prevP = &broken_words;
152033965Sjdp    for (lie = broken_words; lie; lie = lie->next_broken_word)
152133965Sjdp      if (!lie->added)
152233965Sjdp	{
152333965Sjdp	  expressionS exp;
152433965Sjdp
152533965Sjdp	  exp.X_op = O_subtract;
152633965Sjdp	  exp.X_add_symbol = lie->add;
152733965Sjdp	  exp.X_op_symbol = lie->sub;
152833965Sjdp	  exp.X_add_number = lie->addnum;
152933965Sjdp#ifdef BFD_ASSEMBLER
153033965Sjdp#ifdef TC_CONS_FIX_NEW
153133965Sjdp	  TC_CONS_FIX_NEW (lie->frag,
153233965Sjdp		       lie->word_goes_here - lie->frag->fr_literal,
153333965Sjdp		       2, &exp);
153433965Sjdp#else
153533965Sjdp	  fix_new_exp (lie->frag,
153633965Sjdp		       lie->word_goes_here - lie->frag->fr_literal,
153733965Sjdp		       2, &exp, 0, BFD_RELOC_16);
153833965Sjdp#endif
153933965Sjdp#else
154033965Sjdp#if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
154133965Sjdp	  fix_new_exp (lie->frag,
154233965Sjdp		       lie->word_goes_here - lie->frag->fr_literal,
154333965Sjdp		       2, &exp, 0, NO_RELOC);
154433965Sjdp#else
154533965Sjdp#ifdef TC_NS32K
154633965Sjdp	  fix_new_ns32k_exp (lie->frag,
154733965Sjdp			     lie->word_goes_here - lie->frag->fr_literal,
154833965Sjdp			     2, &exp, 0, 0, 2, 0, 0);
154933965Sjdp#else
155033965Sjdp	  fix_new_exp (lie->frag,
155133965Sjdp		       lie->word_goes_here - lie->frag->fr_literal,
155233965Sjdp		       2, &exp, 0, 0);
155333965Sjdp#endif /* TC_NS32K */
155433965Sjdp#endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
155533965Sjdp#endif /* BFD_ASSEMBLER */
155633965Sjdp	  *prevP = lie->next_broken_word;
155733965Sjdp	}
155833965Sjdp      else
155933965Sjdp	prevP = &(lie->next_broken_word);
156033965Sjdp
156133965Sjdp    for (lie = broken_words; lie;)
156233965Sjdp      {
156333965Sjdp	struct broken_word *untruth;
156433965Sjdp	char *table_ptr;
156533965Sjdp	addressT table_addr;
156633965Sjdp	addressT from_addr, to_addr;
156733965Sjdp	int n, m;
156833965Sjdp
156933965Sjdp	fragP = lie->dispfrag;
157033965Sjdp
157133965Sjdp	/* Find out how many broken_words go here.  */
157233965Sjdp	n = 0;
157333965Sjdp	for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
157433965Sjdp	  if (untruth->added == 1)
157533965Sjdp	    n++;
157633965Sjdp
157733965Sjdp	table_ptr = lie->dispfrag->fr_opcode;
157833965Sjdp	table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
157933965Sjdp	/* Create the jump around the long jumps.  This is a short
158033965Sjdp	   jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
158133965Sjdp	from_addr = table_addr;
158233965Sjdp	to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
158333965Sjdp	md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
158433965Sjdp	table_ptr += md_short_jump_size;
158533965Sjdp	table_addr += md_short_jump_size;
158633965Sjdp
158733965Sjdp	for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
158833965Sjdp	  {
158933965Sjdp	    if (lie->added == 2)
159033965Sjdp	      continue;
159133965Sjdp	    /* Patch the jump table */
159233965Sjdp	    /* This is the offset from ??? to table_ptr+0 */
159333965Sjdp	    to_addr = table_addr - S_GET_VALUE (lie->sub);
159433965Sjdp#ifdef BFD_ASSEMBLER
159533965Sjdp	    to_addr -= lie->sub->sy_frag->fr_address;
159633965Sjdp#endif
159733965Sjdp	    md_number_to_chars (lie->word_goes_here, to_addr, 2);
159833965Sjdp	    for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
159933965Sjdp	      {
160033965Sjdp		if (untruth->use_jump == lie)
160133965Sjdp		  md_number_to_chars (untruth->word_goes_here, to_addr, 2);
160233965Sjdp	      }
160333965Sjdp
160433965Sjdp	    /* Install the long jump */
160533965Sjdp	    /* this is a long jump from table_ptr+0 to the final target */
160633965Sjdp	    from_addr = table_addr;
160733965Sjdp	    to_addr = S_GET_VALUE (lie->add) + lie->addnum;
160833965Sjdp#ifdef BFD_ASSEMBLER
160933965Sjdp	    to_addr += lie->add->sy_frag->fr_address;
161033965Sjdp#endif
161133965Sjdp	    md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
161233965Sjdp	    table_ptr += md_long_jump_size;
161333965Sjdp	    table_addr += md_long_jump_size;
161433965Sjdp	  }
161533965Sjdp      }
161633965Sjdp  }
161733965Sjdp#endif /* not WORKING_DOT_WORD */
161833965Sjdp
161933965Sjdp#ifndef BFD_ASSEMBLER
162033965Sjdp#ifndef	OBJ_VMS
162133965Sjdp  {				/* not vms */
162233965Sjdp    char *the_object_file;
162333965Sjdp    long object_file_size;
162433965Sjdp    /*
162533965Sjdp     * Scan every FixS performing fixups. We had to wait until now to do
162633965Sjdp     * this because md_convert_frag() may have made some fixSs.
162733965Sjdp     */
162833965Sjdp    int trsize, drsize;
162933965Sjdp
163033965Sjdp    subseg_change (SEG_TEXT, 0);
163133965Sjdp    trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
163233965Sjdp    subseg_change (SEG_DATA, 0);
163333965Sjdp    drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
163433965Sjdp    H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
163533965Sjdp
163633965Sjdp    /* FIXME move this stuff into the pre-write-hook */
163733965Sjdp    H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
163833965Sjdp    H_SET_ENTRY_POINT (&headers, 0);
163933965Sjdp
164033965Sjdp    obj_pre_write_hook (&headers);	/* extra coff stuff */
164133965Sjdp
164233965Sjdp    object_file_size = H_GET_FILE_SIZE (&headers);
164333965Sjdp    next_object_file_charP = the_object_file = xmalloc (object_file_size);
164433965Sjdp
164533965Sjdp    output_file_create (out_file_name);
164633965Sjdp
164733965Sjdp    obj_header_append (&next_object_file_charP, &headers);
164833965Sjdp
164933965Sjdp    know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
165033965Sjdp
165133965Sjdp    /*
165233965Sjdp     * Emit code.
165333965Sjdp     */
165433965Sjdp    for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
165533965Sjdp      {
165633965Sjdp	register long count;
165733965Sjdp	register char *fill_literal;
165833965Sjdp	register long fill_size;
165933965Sjdp
166033965Sjdp	PROGRESS (1);
166133965Sjdp	know (fragP->fr_type == rs_fill);
166233965Sjdp	append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
166333965Sjdp	fill_literal = fragP->fr_literal + fragP->fr_fix;
166433965Sjdp	fill_size = fragP->fr_var;
166533965Sjdp	know (fragP->fr_offset >= 0);
166633965Sjdp
166733965Sjdp	for (count = fragP->fr_offset; count; count--)
166833965Sjdp	  {
166933965Sjdp	    append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
167033965Sjdp	  }			/* for each  */
167133965Sjdp
167233965Sjdp      }				/* for each code frag. */
167333965Sjdp
167433965Sjdp    know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
167533965Sjdp
167633965Sjdp    /*
167733965Sjdp     * Emit relocations.
167833965Sjdp     */
167933965Sjdp    obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
168033965Sjdp    know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers)));
168133965Sjdp#ifdef TC_I960
168233965Sjdp    /* Make addresses in data relocation directives relative to beginning of
168333965Sjdp     * first data fragment, not end of last text fragment:  alignment of the
168433965Sjdp     * start of the data segment may place a gap between the segments.
168533965Sjdp     */
168633965Sjdp    obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
168733965Sjdp#else /* TC_I960 */
168833965Sjdp    obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
168933965Sjdp#endif /* TC_I960 */
169033965Sjdp
169133965Sjdp    know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers)));
169233965Sjdp
169333965Sjdp    /*
169433965Sjdp     * Emit line number entries.
169533965Sjdp     */
169633965Sjdp    OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
169733965Sjdp    know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers)));
169833965Sjdp
169933965Sjdp    /*
170033965Sjdp     * Emit symbols.
170133965Sjdp     */
170233965Sjdp    obj_emit_symbols (&next_object_file_charP, symbol_rootP);
170333965Sjdp    know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers) + H_GET_SYMBOL_TABLE_SIZE (&headers)));
170433965Sjdp
170533965Sjdp    /*
170633965Sjdp     * Emit strings.
170733965Sjdp     */
170833965Sjdp
170933965Sjdp    if (string_byte_count > 0)
171033965Sjdp      {
171133965Sjdp	obj_emit_strings (&next_object_file_charP);
171233965Sjdp      }				/* only if we have a string table */
171333965Sjdp
171433965Sjdp#ifdef BFD_HEADERS
171533965Sjdp    bfd_seek (stdoutput, 0, 0);
171633965Sjdp    bfd_write (the_object_file, 1, object_file_size, stdoutput);
171733965Sjdp#else
171833965Sjdp
171933965Sjdp    /* Write the data to the file */
172033965Sjdp    output_file_append (the_object_file, object_file_size, out_file_name);
172133965Sjdp    free (the_object_file);
172233965Sjdp#endif
172333965Sjdp  }				/* non vms output */
172433965Sjdp#else /* OBJ_VMS */
172533965Sjdp  /*
172633965Sjdp   *	Now do the VMS-dependent part of writing the object file
172733965Sjdp   */
172833965Sjdp  vms_write_object_file (H_GET_TEXT_SIZE (&headers),
172933965Sjdp			 H_GET_DATA_SIZE (&headers),
173033965Sjdp			 H_GET_BSS_SIZE (&headers),
173133965Sjdp			 text_frag_root, data_frag_root);
173233965Sjdp#endif /* OBJ_VMS */
173333965Sjdp#else /* BFD_ASSEMBLER */
173433965Sjdp
173533965Sjdp  /* Resolve symbol values.  This needs to be done before processing
173633965Sjdp     the relocations.  */
173733965Sjdp  if (symbol_rootP)
173833965Sjdp    {
173933965Sjdp      symbolS *symp;
174033965Sjdp
174133965Sjdp      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
174233965Sjdp	if (!symp->sy_resolved)
174338889Sjdp	  resolve_symbol_value (symp, 1);
174433965Sjdp    }
174533965Sjdp
174633965Sjdp  PROGRESS (1);
174733965Sjdp
174833965Sjdp#ifdef tc_frob_file_before_adjust
174933965Sjdp  tc_frob_file_before_adjust ();
175033965Sjdp#endif
175133965Sjdp#ifdef obj_frob_file_before_adjust
175233965Sjdp  obj_frob_file_before_adjust ();
175333965Sjdp#endif
175433965Sjdp
175533965Sjdp  bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *)0);
175633965Sjdp
175733965Sjdp  /* Set up symbol table, and write it out.  */
175833965Sjdp  if (symbol_rootP)
175933965Sjdp    {
176033965Sjdp      symbolS *symp;
176133965Sjdp
176233965Sjdp      for (symp = symbol_rootP; symp; symp = symbol_next (symp))
176333965Sjdp	{
176433965Sjdp	  int punt = 0;
176533965Sjdp	  const char *name;
176633965Sjdp
176733965Sjdp	  if (symp->sy_mri_common)
176833965Sjdp	    {
176933965Sjdp	      if (S_IS_EXTERNAL (symp))
177033965Sjdp		as_bad ("%s: global symbols not supported in common sections",
177133965Sjdp			S_GET_NAME (symp));
177233965Sjdp	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
177333965Sjdp	      continue;
177433965Sjdp	    }
177533965Sjdp
177633965Sjdp	  name = S_GET_NAME (symp);
177733965Sjdp	  if (name)
177833965Sjdp	    {
177933965Sjdp	      const char *name2 = decode_local_label_name ((char *)S_GET_NAME (symp));
178033965Sjdp	      /* They only differ if `name' is a fb or dollar local
178133965Sjdp		 label name.  */
178233965Sjdp	      if (name2 != name && ! S_IS_DEFINED (symp))
178333965Sjdp		as_bad ("local label %s is not defined", name2);
178433965Sjdp	    }
178533965Sjdp
178633965Sjdp	  /* Do it again, because adjust_reloc_syms might introduce
178733965Sjdp	     more symbols.  They'll probably only be section symbols,
178833965Sjdp	     but they'll still need to have the values computed.  */
178933965Sjdp	  if (! symp->sy_resolved)
179033965Sjdp	    {
179133965Sjdp	      if (symp->sy_value.X_op == O_constant)
179233965Sjdp		{
179333965Sjdp		  /* This is the normal case; skip the call.  */
179433965Sjdp		  S_SET_VALUE (symp,
179533965Sjdp			       (S_GET_VALUE (symp)
179633965Sjdp				+ symp->sy_frag->fr_address));
179733965Sjdp		  symp->sy_resolved = 1;
179833965Sjdp		}
179933965Sjdp	      else
180038889Sjdp		resolve_symbol_value (symp, 1);
180133965Sjdp	    }
180233965Sjdp
180333965Sjdp	  /* Skip symbols which were equated to undefined or common
180433965Sjdp             symbols.  */
180533965Sjdp	  if (symp->sy_value.X_op == O_symbol
180633965Sjdp	      && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
180733965Sjdp	    {
180833965Sjdp	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
180933965Sjdp	      continue;
181033965Sjdp	    }
181133965Sjdp
181233965Sjdp	  /* So far, common symbols have been treated like undefined symbols.
181333965Sjdp	     Put them in the common section now.  */
181433965Sjdp	  if (S_IS_DEFINED (symp) == 0
181533965Sjdp	      && S_GET_VALUE (symp) != 0)
181633965Sjdp	    S_SET_SEGMENT (symp, bfd_com_section_ptr);
181733965Sjdp#if 0
181833965Sjdp	  printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
181933965Sjdp		  S_GET_NAME (symp), symp,
182033965Sjdp		  S_GET_VALUE (symp),
182133965Sjdp		  symp->bsym->flags,
182233965Sjdp		  segment_name (symp->bsym->section));
182333965Sjdp#endif
182433965Sjdp
182533965Sjdp#ifdef obj_frob_symbol
182633965Sjdp	  obj_frob_symbol (symp, punt);
182733965Sjdp#endif
182833965Sjdp#ifdef tc_frob_symbol
182933965Sjdp	  if (! punt || symp->sy_used_in_reloc)
183033965Sjdp	    tc_frob_symbol (symp, punt);
183133965Sjdp#endif
183233965Sjdp
183333965Sjdp	  /* If we don't want to keep this symbol, splice it out of
183433965Sjdp	     the chain now.  If EMIT_SECTION_SYMBOLS is 0, we never
183533965Sjdp	     want section symbols.  Otherwise, we skip local symbols
183633965Sjdp	     and symbols that the frob_symbol macros told us to punt,
183733965Sjdp	     but we keep such symbols if they are used in relocs.  */
183833965Sjdp	  if ((! EMIT_SECTION_SYMBOLS
183933965Sjdp	       && (symp->bsym->flags & BSF_SECTION_SYM) != 0)
184033965Sjdp	      /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
184133965Sjdp		 opposites.  Sometimes the former checks flags and the
184233965Sjdp		 latter examines the name...  */
184333965Sjdp	      || (!S_IS_EXTERN (symp)
184433965Sjdp		  && (S_IS_LOCAL (symp) || punt)
184533965Sjdp		  && ! symp->sy_used_in_reloc))
184633965Sjdp	    {
184733965Sjdp	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
184833965Sjdp	      /* After symbol_remove, symbol_next(symp) still returns
184933965Sjdp		 the one that came after it in the chain.  So we don't
185033965Sjdp		 need to do any extra cleanup work here.  */
185133965Sjdp
185233965Sjdp	      continue;
185333965Sjdp	    }
185433965Sjdp
185533965Sjdp	  /* Make sure we really got a value for the symbol.  */
185633965Sjdp	  if (! symp->sy_resolved)
185733965Sjdp	    {
185833965Sjdp	      as_bad ("can't resolve value for symbol \"%s\"",
185933965Sjdp		      S_GET_NAME (symp));
186033965Sjdp	      symp->sy_resolved = 1;
186133965Sjdp	    }
186233965Sjdp
186333965Sjdp	  /* Set the value into the BFD symbol.  Up til now the value
186433965Sjdp	     has only been kept in the gas symbolS struct.  */
186533965Sjdp	  symp->bsym->value = S_GET_VALUE (symp);
186633965Sjdp	}
186733965Sjdp    }
186833965Sjdp
186933965Sjdp  PROGRESS (1);
187033965Sjdp
187133965Sjdp  /* Now do any format-specific adjustments to the symbol table, such
187233965Sjdp     as adding file symbols.  */
187333965Sjdp#ifdef tc_adjust_symtab
187433965Sjdp  tc_adjust_symtab ();
187533965Sjdp#endif
187633965Sjdp#ifdef obj_adjust_symtab
187733965Sjdp  obj_adjust_symtab ();
187833965Sjdp#endif
187933965Sjdp
188033965Sjdp  /* Now that all the sizes are known, and contents correct, we can
188133965Sjdp     start writing to the file.  */
188233965Sjdp  set_symtab ();
188333965Sjdp
188433965Sjdp  /* If *_frob_file changes the symbol value at this point, it is
188533965Sjdp     responsible for moving the changed value into symp->bsym->value
188633965Sjdp     as well.  Hopefully all symbol value changing can be done in
188733965Sjdp     *_frob_symbol.  */
188833965Sjdp#ifdef tc_frob_file
188933965Sjdp  tc_frob_file ();
189033965Sjdp#endif
189133965Sjdp#ifdef obj_frob_file
189233965Sjdp  obj_frob_file ();
189333965Sjdp#endif
189433965Sjdp
189533965Sjdp  bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
189633965Sjdp
189733965Sjdp#ifdef tc_frob_file_after_relocs
189833965Sjdp  tc_frob_file_after_relocs ();
189933965Sjdp#endif
190033965Sjdp#ifdef obj_frob_file_after_relocs
190133965Sjdp  obj_frob_file_after_relocs ();
190233965Sjdp#endif
190333965Sjdp
190433965Sjdp  bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
190533965Sjdp#endif /* BFD_ASSEMBLER */
190633965Sjdp}
190733965Sjdp#endif /* ! BFD */
190833965Sjdp
190933965Sjdp/*
191033965Sjdp *			relax_segment()
191133965Sjdp *
191233965Sjdp * Now we have a segment, not a crowd of sub-segments, we can make fr_address
191333965Sjdp * values.
191433965Sjdp *
191533965Sjdp * Relax the frags.
191633965Sjdp *
191733965Sjdp * After this, all frags in this segment have addresses that are correct
191833965Sjdp * within the segment. Since segments live in different file addresses,
191933965Sjdp * these frag addresses may not be the same as final object-file addresses.
192033965Sjdp */
192133965Sjdp
192233965Sjdp#ifdef TC_GENERIC_RELAX_TABLE
192333965Sjdp
192433965Sjdpstatic int is_dnrange PARAMS ((fragS *, fragS *));
192533965Sjdp
192633965Sjdp/* Subroutines of relax_segment.  */
192733965Sjdpstatic int
192833965Sjdpis_dnrange (f1, f2)
192933965Sjdp     fragS *f1;
193033965Sjdp     fragS *f2;
193133965Sjdp{
193233965Sjdp  for (; f1; f1 = f1->fr_next)
193333965Sjdp    if (f1->fr_next == f2)
193433965Sjdp      return 1;
193533965Sjdp  return 0;
193633965Sjdp}
193733965Sjdp
193833965Sjdp/* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE.  */
193933965Sjdp
194033965Sjdplong
194133965Sjdprelax_frag (fragP, stretch)
194233965Sjdp     fragS *fragP;
194333965Sjdp     long stretch;
194433965Sjdp{
194533965Sjdp  const relax_typeS *this_type;
194633965Sjdp  const relax_typeS *start_type;
194733965Sjdp  relax_substateT next_state;
194833965Sjdp  relax_substateT this_state;
194933965Sjdp  long aim, target, growth;
195033965Sjdp  symbolS *symbolP = fragP->fr_symbol;
195133965Sjdp  long offset = fragP->fr_offset;
195233965Sjdp  /* Recompute was_address by undoing "+= stretch" done by relax_segment.  */
195333965Sjdp  unsigned long was_address = fragP->fr_address - stretch;
195433965Sjdp  unsigned long address = fragP->fr_address;
195533965Sjdp  const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
195633965Sjdp
195733965Sjdp  this_state = fragP->fr_subtype;
195833965Sjdp  start_type = this_type = table + this_state;
195933965Sjdp  target = offset;
196033965Sjdp
196133965Sjdp  if (symbolP)
196233965Sjdp    {
196333965Sjdp#ifndef DIFF_EXPR_OK
196433965Sjdp#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
196533965Sjdp      know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
196633965Sjdp	    || (S_GET_SEGMENT (symbolP) == SEG_DATA)
196733965Sjdp	    || (S_GET_SEGMENT (symbolP) == SEG_BSS)
196833965Sjdp	    || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
196933965Sjdp#endif
197033965Sjdp      know (symbolP->sy_frag);
197133965Sjdp#endif
197233965Sjdp      know (!(S_GET_SEGMENT (symbolP) == absolute_section)
197333965Sjdp	    || symbolP->sy_frag == &zero_address_frag);
197433965Sjdp      target +=
197533965Sjdp	S_GET_VALUE (symbolP)
197633965Sjdp	  + symbolP->sy_frag->fr_address;
197733965Sjdp
197833965Sjdp      /* If frag has yet to be reached on this pass,
197933965Sjdp	 assume it will move by STRETCH just as we did.
198033965Sjdp	 If this is not so, it will be because some frag
198133965Sjdp	 between grows, and that will force another pass.
198233965Sjdp
198333965Sjdp	 Beware zero-length frags.
198433965Sjdp
198533965Sjdp	 There should be a faster way to do this.  */
198633965Sjdp
198733965Sjdp      if (symbolP->sy_frag->fr_address >= was_address
198833965Sjdp	  && is_dnrange (fragP, symbolP->sy_frag))
198933965Sjdp	{
199033965Sjdp	  target += stretch;
199133965Sjdp	}
199233965Sjdp    }
199333965Sjdp
199433965Sjdp  aim = target - address - fragP->fr_fix;
199533965Sjdp#ifdef TC_PCREL_ADJUST
199633965Sjdp  /* Currently only the ns32k family needs this */
199733965Sjdp  aim += TC_PCREL_ADJUST(fragP);
199833965Sjdp/*#else*/
199933965Sjdp  /* This machine doesn't want to use pcrel_adjust.
200033965Sjdp     In that case, pcrel_adjust should be zero.  */
200133965Sjdp/*  assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);*/
200233965Sjdp#endif
200333965Sjdp#ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
200433965Sjdp  md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
200533965Sjdp#endif
200633965Sjdp
200733965Sjdp  if (aim < 0)
200833965Sjdp    {
200933965Sjdp      /* Look backwards. */
201033965Sjdp      for (next_state = this_type->rlx_more; next_state;)
201133965Sjdp	if (aim >= this_type->rlx_backward)
201233965Sjdp	  next_state = 0;
201333965Sjdp	else
201433965Sjdp	  {
201533965Sjdp	    /* Grow to next state. */
201633965Sjdp	    this_state = next_state;
201733965Sjdp	    this_type = table + this_state;
201833965Sjdp	    next_state = this_type->rlx_more;
201933965Sjdp	  }
202033965Sjdp    }
202133965Sjdp  else
202233965Sjdp    {
202333965Sjdp      /* Look forwards. */
202433965Sjdp      for (next_state = this_type->rlx_more; next_state;)
202533965Sjdp	if (aim <= this_type->rlx_forward)
202633965Sjdp	  next_state = 0;
202733965Sjdp	else
202833965Sjdp	  {
202933965Sjdp	    /* Grow to next state. */
203033965Sjdp	    this_state = next_state;
203133965Sjdp	    this_type = table + this_state;
203233965Sjdp	    next_state = this_type->rlx_more;
203333965Sjdp	  }
203433965Sjdp    }
203533965Sjdp
203633965Sjdp  growth = this_type->rlx_length - start_type->rlx_length;
203733965Sjdp  if (growth != 0)
203833965Sjdp    fragP->fr_subtype = this_state;
203933965Sjdp  return growth;
204033965Sjdp}
204133965Sjdp
204233965Sjdp#endif /* defined (TC_GENERIC_RELAX_TABLE) */
204333965Sjdp
204433965Sjdp/* Relax_align. Advance location counter to next address that has 'alignment'
204533965Sjdp   lowest order bits all 0s, return size of adjustment made.  */
204633965Sjdpstatic relax_addressT
204733965Sjdprelax_align (address, alignment)
204833965Sjdp     register relax_addressT address;	/* Address now. */
204933965Sjdp     register int alignment;	/* Alignment (binary). */
205033965Sjdp{
205133965Sjdp  relax_addressT mask;
205233965Sjdp  relax_addressT new_address;
205333965Sjdp
205433965Sjdp  mask = ~((~0) << alignment);
205533965Sjdp  new_address = (address + mask) & (~mask);
205633965Sjdp#ifdef LINKER_RELAXING_SHRINKS_ONLY
205733965Sjdp  if (linkrelax)
205833965Sjdp    /* We must provide lots of padding, so the linker can discard it
205933965Sjdp       when needed.  The linker will not add extra space, ever.  */
206033965Sjdp    new_address += (1 << alignment);
206133965Sjdp#endif
206233965Sjdp  return (new_address - address);
206333965Sjdp}
206433965Sjdp
206533965Sjdpvoid
206633965Sjdprelax_segment (segment_frag_root, segment)
206733965Sjdp     struct frag *segment_frag_root;
206833965Sjdp     segT segment;
206933965Sjdp{
207033965Sjdp  register struct frag *fragP;
207133965Sjdp  register relax_addressT address;
207233965Sjdp#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
207333965Sjdp  know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
207433965Sjdp#endif
207533965Sjdp  /* In case md_estimate_size_before_relax() wants to make fixSs. */
207633965Sjdp  subseg_change (segment, 0);
207733965Sjdp
207833965Sjdp  /* For each frag in segment: count and store  (a 1st guess of)
207933965Sjdp     fr_address.  */
208033965Sjdp  address = 0;
208133965Sjdp  for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
208233965Sjdp    {
208333965Sjdp      fragP->fr_address = address;
208433965Sjdp      address += fragP->fr_fix;
208533965Sjdp
208633965Sjdp      switch (fragP->fr_type)
208733965Sjdp	{
208833965Sjdp	case rs_fill:
208933965Sjdp	  address += fragP->fr_offset * fragP->fr_var;
209033965Sjdp	  break;
209133965Sjdp
209233965Sjdp	case rs_align:
209333965Sjdp	case rs_align_code:
209433965Sjdp	  {
209533965Sjdp	    addressT offset = relax_align (address, (int) fragP->fr_offset);
209633965Sjdp
209733965Sjdp	    if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
209833965Sjdp	      offset = 0;
209933965Sjdp
210033965Sjdp	    if (offset % fragP->fr_var != 0)
210133965Sjdp	      {
210233965Sjdp		as_bad ("alignment padding (%lu bytes) not a multiple of %ld",
210333965Sjdp			(unsigned long) offset, (long) fragP->fr_var);
210433965Sjdp		offset -= (offset % fragP->fr_var);
210533965Sjdp	      }
210633965Sjdp
210733965Sjdp	    address += offset;
210833965Sjdp	  }
210933965Sjdp	  break;
211033965Sjdp
211133965Sjdp	case rs_org:
211233965Sjdp	case rs_space:
211333965Sjdp	  /* Assume .org is nugatory. It will grow with 1st relax.  */
211433965Sjdp	  break;
211533965Sjdp
211633965Sjdp	case rs_machine_dependent:
211733965Sjdp	  address += md_estimate_size_before_relax (fragP, segment);
211833965Sjdp	  break;
211933965Sjdp
212033965Sjdp#ifndef WORKING_DOT_WORD
212133965Sjdp	  /* Broken words don't concern us yet */
212233965Sjdp	case rs_broken_word:
212333965Sjdp	  break;
212433965Sjdp#endif
212533965Sjdp
212638889Sjdp	case rs_leb128:
212738889Sjdp	  /* Initial guess is always 1; doing otherwise can result in
212838889Sjdp	     stable solutions that are larger than the minimum.  */
212938889Sjdp	  address += fragP->fr_offset = 1;
213038889Sjdp	  break;
213138889Sjdp
213238889Sjdp	case rs_cfa:
213338889Sjdp	  address += eh_frame_estimate_size_before_relax (fragP);
213438889Sjdp	  break;
213538889Sjdp
213633965Sjdp	default:
213733965Sjdp	  BAD_CASE (fragP->fr_type);
213833965Sjdp	  break;
213933965Sjdp	}			/* switch(fr_type) */
214033965Sjdp    }				/* for each frag in the segment */
214133965Sjdp
214233965Sjdp  /* Do relax().  */
214333965Sjdp  {
214433965Sjdp    long stretch;	/* May be any size, 0 or negative. */
214533965Sjdp    /* Cumulative number of addresses we have */
214633965Sjdp    /* relaxed this pass. */
214733965Sjdp    /* We may have relaxed more than one address. */
214833965Sjdp    long stretched;	/* Have we stretched on this pass? */
214933965Sjdp    /* This is 'cuz stretch may be zero, when, in fact some piece of code
215033965Sjdp       grew, and another shrank.  If a branch instruction doesn't fit anymore,
215133965Sjdp       we could be scrod.  */
215233965Sjdp
215333965Sjdp    do
215433965Sjdp      {
215533965Sjdp	stretch = stretched = 0;
215633965Sjdp	for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
215733965Sjdp	  {
215833965Sjdp	    long growth = 0;
215933965Sjdp	    addressT was_address;
216033965Sjdp	    offsetT offset;
216133965Sjdp	    symbolS *symbolP;
216233965Sjdp
216333965Sjdp	    was_address = fragP->fr_address;
216433965Sjdp	    address = fragP->fr_address += stretch;
216533965Sjdp	    symbolP = fragP->fr_symbol;
216633965Sjdp	    offset = fragP->fr_offset;
216733965Sjdp
216833965Sjdp	    switch (fragP->fr_type)
216933965Sjdp	      {
217033965Sjdp	      case rs_fill:	/* .fill never relaxes. */
217133965Sjdp		growth = 0;
217233965Sjdp		break;
217333965Sjdp
217433965Sjdp#ifndef WORKING_DOT_WORD
217533965Sjdp		/* JF:  This is RMS's idea.  I do *NOT* want to be blamed
217633965Sjdp		   for it I do not want to write it.  I do not want to have
217733965Sjdp		   anything to do with it.  This is not the proper way to
217833965Sjdp		   implement this misfeature.  */
217933965Sjdp	      case rs_broken_word:
218033965Sjdp		{
218133965Sjdp		  struct broken_word *lie;
218233965Sjdp		  struct broken_word *untruth;
218333965Sjdp
218433965Sjdp		  /* Yes this is ugly (storing the broken_word pointer
218533965Sjdp		     in the symbol slot).  Still, this whole chunk of
218633965Sjdp		     code is ugly, and I don't feel like doing anything
218733965Sjdp		     about it.  Think of it as stubbornness in action.  */
218833965Sjdp		  growth = 0;
218933965Sjdp		  for (lie = (struct broken_word *) (fragP->fr_symbol);
219033965Sjdp		       lie && lie->dispfrag == fragP;
219133965Sjdp		       lie = lie->next_broken_word)
219233965Sjdp		    {
219333965Sjdp
219433965Sjdp		      if (lie->added)
219533965Sjdp			continue;
219633965Sjdp
219733965Sjdp		      offset = (lie->add->sy_frag->fr_address
219833965Sjdp				+ S_GET_VALUE (lie->add)
219933965Sjdp				+ lie->addnum
220033965Sjdp				- (lie->sub->sy_frag->fr_address
220133965Sjdp				   + S_GET_VALUE (lie->sub)));
220233965Sjdp		      if (offset <= -32768 || offset >= 32767)
220333965Sjdp			{
220433965Sjdp			  if (flag_warn_displacement)
220533965Sjdp			    {
220633965Sjdp			      char buf[50];
220733965Sjdp			      sprint_value (buf, (addressT) lie->addnum);
220833965Sjdp			      as_warn (".word %s-%s+%s didn't fit",
220933965Sjdp				       S_GET_NAME (lie->add),
221033965Sjdp				       S_GET_NAME (lie->sub),
221133965Sjdp				       buf);
221233965Sjdp			    }
221333965Sjdp			  lie->added = 1;
221433965Sjdp			  if (fragP->fr_subtype == 0)
221533965Sjdp			    {
221633965Sjdp			      fragP->fr_subtype++;
221733965Sjdp			      growth += md_short_jump_size;
221833965Sjdp			    }
221933965Sjdp			  for (untruth = lie->next_broken_word;
222033965Sjdp			       untruth && untruth->dispfrag == lie->dispfrag;
222133965Sjdp			       untruth = untruth->next_broken_word)
222233965Sjdp			    if ((untruth->add->sy_frag == lie->add->sy_frag)
222333965Sjdp				&& S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
222433965Sjdp			      {
222533965Sjdp				untruth->added = 2;
222633965Sjdp				untruth->use_jump = lie;
222733965Sjdp			      }
222833965Sjdp			  growth += md_long_jump_size;
222933965Sjdp			}
223033965Sjdp		    }
223133965Sjdp
223233965Sjdp		  break;
223333965Sjdp		}		/* case rs_broken_word */
223433965Sjdp#endif
223533965Sjdp	      case rs_align:
223633965Sjdp	      case rs_align_code:
223733965Sjdp		{
223833965Sjdp		  addressT oldoff, newoff;
223933965Sjdp
224033965Sjdp		  oldoff = relax_align (was_address + fragP->fr_fix,
224133965Sjdp					(int) offset);
224233965Sjdp		  newoff = relax_align (address + fragP->fr_fix,
224333965Sjdp					(int) offset);
224433965Sjdp
224533965Sjdp		  if (fragP->fr_subtype != 0)
224633965Sjdp		    {
224733965Sjdp		      if (oldoff > fragP->fr_subtype)
224833965Sjdp			oldoff = 0;
224933965Sjdp		      if (newoff > fragP->fr_subtype)
225033965Sjdp			newoff = 0;
225133965Sjdp		    }
225233965Sjdp
225333965Sjdp		  growth = newoff - oldoff;
225433965Sjdp		}
225533965Sjdp		break;
225633965Sjdp
225733965Sjdp	      case rs_org:
225833965Sjdp		{
225933965Sjdp		  long target = offset;
226033965Sjdp		  long after;
226133965Sjdp
226233965Sjdp		  if (symbolP)
226333965Sjdp		    {
226433965Sjdp#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
226533965Sjdp		      know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
226633965Sjdp			    || (S_GET_SEGMENT (symbolP) == SEG_DATA)
226733965Sjdp			    || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
226833965Sjdp			    || S_GET_SEGMENT (symbolP) == SEG_BSS);
226933965Sjdp		      know (symbolP->sy_frag);
227033965Sjdp		      know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
227133965Sjdp			    || (symbolP->sy_frag == &zero_address_frag));
227233965Sjdp#endif
227333965Sjdp		      target += S_GET_VALUE (symbolP)
227433965Sjdp			+ symbolP->sy_frag->fr_address;
227533965Sjdp		    }		/* if we have a symbol */
227633965Sjdp
227733965Sjdp		  know (fragP->fr_next);
227833965Sjdp		  after = fragP->fr_next->fr_address;
227933965Sjdp		  growth = target - after;
228033965Sjdp		  if (growth < 0)
228133965Sjdp		    {
228233965Sjdp		      /* Growth may be negative, but variable part of frag
228333965Sjdp			 cannot have fewer than 0 chars.  That is, we can't
228433965Sjdp			 .org backwards. */
228533965Sjdp		      as_bad ("attempt to .org backwards ignored");
228633965Sjdp		      growth = 0;
228733965Sjdp		    }
228833965Sjdp
228933965Sjdp		  growth -= stretch;	/* This is an absolute growth factor */
229033965Sjdp		  break;
229133965Sjdp		}
229233965Sjdp
229333965Sjdp	      case rs_space:
229433965Sjdp		if (symbolP)
229533965Sjdp		  {
229633965Sjdp		    growth = S_GET_VALUE (symbolP);
229733965Sjdp		    if (symbolP->sy_frag != &zero_address_frag
229833965Sjdp			|| S_IS_COMMON (symbolP)
229933965Sjdp			|| ! S_IS_DEFINED (symbolP))
230033965Sjdp		      as_bad_where (fragP->fr_file, fragP->fr_line,
230133965Sjdp				    ".space specifies non-absolute value");
230233965Sjdp		    fragP->fr_symbol = 0;
230333965Sjdp		    if (growth < 0)
230433965Sjdp		      {
230533965Sjdp			as_warn (".space or .fill with negative value, ignored");
230633965Sjdp			growth = 0;
230733965Sjdp		      }
230833965Sjdp		  }
230933965Sjdp		else
231033965Sjdp		  growth = 0;
231133965Sjdp		break;
231233965Sjdp
231333965Sjdp	      case rs_machine_dependent:
231433965Sjdp#ifdef md_relax_frag
231533965Sjdp		growth = md_relax_frag (fragP, stretch);
231633965Sjdp#else
231733965Sjdp#ifdef TC_GENERIC_RELAX_TABLE
231833965Sjdp		/* The default way to relax a frag is to look through
231933965Sjdp		   TC_GENERIC_RELAX_TABLE.  */
232033965Sjdp		growth = relax_frag (fragP, stretch);
232133965Sjdp#endif /* TC_GENERIC_RELAX_TABLE */
232233965Sjdp#endif
232333965Sjdp		break;
232433965Sjdp
232538889Sjdp	      case rs_leb128:
232638889Sjdp		{
232738889Sjdp		  valueT value;
232838889Sjdp		  int size;
232938889Sjdp
233038889Sjdp		  value = resolve_symbol_value (fragP->fr_symbol, 0);
233138889Sjdp		  size = sizeof_leb128 (value, fragP->fr_subtype);
233238889Sjdp		  growth = size - fragP->fr_offset;
233338889Sjdp		  fragP->fr_offset = size;
233438889Sjdp		}
233538889Sjdp		break;
233638889Sjdp
233738889Sjdp	      case rs_cfa:
233838889Sjdp		growth = eh_frame_relax_frag (fragP);
233938889Sjdp		break;
234038889Sjdp
234133965Sjdp	      default:
234233965Sjdp		BAD_CASE (fragP->fr_type);
234333965Sjdp		break;
234433965Sjdp	      }
234533965Sjdp	    if (growth)
234633965Sjdp	      {
234733965Sjdp		stretch += growth;
234833965Sjdp		stretched++;
234933965Sjdp	      }
235033965Sjdp	  }			/* For each frag in the segment. */
235133965Sjdp      }
235233965Sjdp    while (stretched);		/* Until nothing further to relax. */
235333965Sjdp  }				/* do_relax */
235433965Sjdp
235533965Sjdp  /*
235633965Sjdp   * We now have valid fr_address'es for each frag.
235733965Sjdp   */
235833965Sjdp
235933965Sjdp  /*
236033965Sjdp   * All fr_address's are correct, relative to their own segment.
236133965Sjdp   * We have made all the fixS we will ever make.
236233965Sjdp   */
236333965Sjdp}				/* relax_segment() */
236433965Sjdp
236533965Sjdp#if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
236633965Sjdp
236733965Sjdp#ifndef TC_RELOC_RTSYM_LOC_FIXUP
236833965Sjdp#define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
236933965Sjdp#endif
237033965Sjdp
237133965Sjdp/* fixup_segment()
237233965Sjdp
237333965Sjdp   Go through all the fixS's in a segment and see which ones can be
237433965Sjdp   handled now.  (These consist of fixS where we have since discovered
237533965Sjdp   the value of a symbol, or the address of the frag involved.)
237633965Sjdp   For each one, call md_apply_fix to put the fix into the frag data.
237733965Sjdp
237833965Sjdp   Result is a count of how many relocation structs will be needed to
237933965Sjdp   handle the remaining fixS's that we couldn't completely handle here.
238033965Sjdp   These will be output later by emit_relocations().  */
238133965Sjdp
238233965Sjdpstatic long
238333965Sjdpfixup_segment (fixP, this_segment_type)
238433965Sjdp     register fixS *fixP;
238533965Sjdp     segT this_segment_type;	/* N_TYPE bits for segment. */
238633965Sjdp{
238733965Sjdp  long seg_reloc_count = 0;
238833965Sjdp  symbolS *add_symbolP;
238933965Sjdp  symbolS *sub_symbolP;
239033965Sjdp  valueT add_number;
239133965Sjdp  int size;
239233965Sjdp  char *place;
239333965Sjdp  long where;
239433965Sjdp  int pcrel, plt;
239533965Sjdp  fragS *fragP;
239633965Sjdp  segT add_symbol_segment = absolute_section;
239733965Sjdp
239833965Sjdp  /* If the linker is doing the relaxing, we must not do any fixups.
239933965Sjdp
240033965Sjdp     Well, strictly speaking that's not true -- we could do any that are
240133965Sjdp     PC-relative and don't cross regions that could change size.  And for the
240233965Sjdp     i960 (the only machine for which we've got a relaxing linker right now),
240333965Sjdp     we might be able to turn callx/callj into bal anyways in cases where we
240433965Sjdp     know the maximum displacement.  */
240533965Sjdp  if (linkrelax)
240633965Sjdp    {
240733965Sjdp      for (; fixP; fixP = fixP->fx_next)
240833965Sjdp	seg_reloc_count++;
240933965Sjdp      TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
241033965Sjdp      return seg_reloc_count;
241133965Sjdp    }
241233965Sjdp
241333965Sjdp  for (; fixP; fixP = fixP->fx_next)
241433965Sjdp    {
241533965Sjdp#ifdef DEBUG5
241633965Sjdp      fprintf (stderr, "\nprocessing fixup:\n");
241733965Sjdp      print_fixup (fixP);
241833965Sjdp#endif
241933965Sjdp
242033965Sjdp      fragP = fixP->fx_frag;
242133965Sjdp      know (fragP);
242233965Sjdp      where = fixP->fx_where;
242333965Sjdp      place = fragP->fr_literal + where;
242433965Sjdp      size = fixP->fx_size;
242533965Sjdp      add_symbolP = fixP->fx_addsy;
242633965Sjdp#ifdef TC_VALIDATE_FIX
242733965Sjdp      TC_VALIDATE_FIX (fixP, this_segment_type, skip);
242833965Sjdp#endif
242933965Sjdp      sub_symbolP = fixP->fx_subsy;
243033965Sjdp      add_number = fixP->fx_offset;
243133965Sjdp      pcrel = fixP->fx_pcrel;
243233965Sjdp      plt = fixP->fx_plt;
243333965Sjdp
243433965Sjdp      if (add_symbolP != NULL
243533965Sjdp	  && add_symbolP->sy_mri_common)
243633965Sjdp	{
243733965Sjdp	  know (add_symbolP->sy_value.X_op == O_symbol);
243833965Sjdp	  add_number += S_GET_VALUE (add_symbolP);
243933965Sjdp	  fixP->fx_offset = add_number;
244033965Sjdp	  add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
244133965Sjdp	}
244233965Sjdp
244333965Sjdp      if (add_symbolP)
244433965Sjdp	add_symbol_segment = S_GET_SEGMENT (add_symbolP);
244533965Sjdp
244633965Sjdp      if (sub_symbolP)
244733965Sjdp	{
244838889Sjdp	  resolve_symbol_value (sub_symbolP, 1);
244933965Sjdp	  if (add_symbolP == NULL || add_symbol_segment == absolute_section)
245033965Sjdp	    {
245133965Sjdp	      if (add_symbolP != NULL)
245233965Sjdp		{
245333965Sjdp		  add_number += S_GET_VALUE (add_symbolP);
245433965Sjdp		  add_symbolP = NULL;
245533965Sjdp		  fixP->fx_addsy = NULL;
245633965Sjdp		}
245733965Sjdp
245833965Sjdp	      /* It's just -sym */
245933965Sjdp	      if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
246033965Sjdp		{
246133965Sjdp		  add_number -= S_GET_VALUE (sub_symbolP);
246233965Sjdp		  fixP->fx_subsy = NULL;
246333965Sjdp		}
246433965Sjdp	      else if (pcrel
246533965Sjdp		       && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
246633965Sjdp		{
246733965Sjdp		  /* Should try converting to a constant.  */
246833965Sjdp		  goto bad_sub_reloc;
246933965Sjdp		}
247033965Sjdp	      else
247133965Sjdp	      bad_sub_reloc:
247233965Sjdp		as_bad_where (fixP->fx_file, fixP->fx_line,
247333965Sjdp			      "Negative of non-absolute symbol %s",
247433965Sjdp			      S_GET_NAME (sub_symbolP));
247533965Sjdp	    }
247633965Sjdp	  else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
247733965Sjdp		   && SEG_NORMAL (add_symbol_segment))
247833965Sjdp	    {
247933965Sjdp	      /* Difference of 2 symbols from same segment.
248033965Sjdp		 Can't make difference of 2 undefineds: 'value' means
248133965Sjdp		 something different for N_UNDF. */
248233965Sjdp#ifdef TC_I960
248333965Sjdp	      /* Makes no sense to use the difference of 2 arbitrary symbols
248433965Sjdp		 as the target of a call instruction.  */
248533965Sjdp	      if (fixP->fx_tcbit)
248633965Sjdp		as_bad_where (fixP->fx_file, fixP->fx_line,
248733965Sjdp			      "callj to difference of 2 symbols");
248833965Sjdp#endif /* TC_I960 */
248933965Sjdp	      add_number += S_GET_VALUE (add_symbolP) -
249033965Sjdp		S_GET_VALUE (sub_symbolP);
249133965Sjdp
249233965Sjdp	      add_symbolP = NULL;
249333965Sjdp	      pcrel = 0;	/* No further pcrel processing. */
249433965Sjdp
249533965Sjdp	      /* Let the target machine make the final determination
249633965Sjdp		 as to whether or not a relocation will be needed to
249733965Sjdp		 handle this fixup.  */
249833965Sjdp	      if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
249933965Sjdp		{
250033965Sjdp		  fixP->fx_pcrel = 0;
250133965Sjdp		  fixP->fx_addsy = NULL;
250233965Sjdp		  fixP->fx_subsy = NULL;
250333965Sjdp		}
250433965Sjdp	    }
250533965Sjdp	  else
250633965Sjdp	    {
250733965Sjdp	      /* Different segments in subtraction. */
250833965Sjdp	      know (!(S_IS_EXTERNAL (sub_symbolP)
250933965Sjdp		      && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
251033965Sjdp
251133965Sjdp	      if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
251233965Sjdp		add_number -= S_GET_VALUE (sub_symbolP);
251333965Sjdp
251433965Sjdp#ifdef DIFF_EXPR_OK
251533965Sjdp	      else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
251633965Sjdp#if 0 /* Do this even if it's already described as pc-relative.  For example,
251733965Sjdp	 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
251833965Sjdp	 pc-relative mode.  */
251933965Sjdp		       && pcrel
252033965Sjdp#endif
252133965Sjdp		       )
252233965Sjdp		{
252333965Sjdp		  /* Make it pc-relative.  */
252433965Sjdp		  add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
252533965Sjdp				 - S_GET_VALUE (sub_symbolP));
252633965Sjdp		  pcrel = 1;
252733965Sjdp		  fixP->fx_pcrel = 1;
252833965Sjdp		  sub_symbolP = 0;
252933965Sjdp		  fixP->fx_subsy = 0;
253033965Sjdp		}
253133965Sjdp#endif
253233965Sjdp#ifdef UNDEFINED_DIFFERENCE_OK
253333965Sjdp	      /* The PA needs this for PIC code generation.  We basically
253433965Sjdp		 don't want to do anything if we have the difference of two
253533965Sjdp		 symbols at this point.  */
253633965Sjdp	      else if (1)
253733965Sjdp		{
253833965Sjdp		  /* Leave it alone.  */
253933965Sjdp		}
254033965Sjdp#endif
254133965Sjdp#ifdef BFD_ASSEMBLER
254233965Sjdp	      else if (fixP->fx_r_type == BFD_RELOC_GPREL32
254333965Sjdp		       || fixP->fx_r_type == BFD_RELOC_GPREL16)
254433965Sjdp		{
254533965Sjdp		  /* Leave it alone.  */
254633965Sjdp		}
254733965Sjdp#endif
254833965Sjdp	      else
254933965Sjdp		{
255033965Sjdp		  char buf[50];
255133965Sjdp		  sprint_value (buf, fragP->fr_address + where);
255233965Sjdp		  as_bad_where (fixP->fx_file, fixP->fx_line,
255333965Sjdp				"Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %s.",
255433965Sjdp				segment_name (S_GET_SEGMENT (sub_symbolP)),
255533965Sjdp				S_GET_NAME (sub_symbolP), buf);
255633965Sjdp		}
255733965Sjdp	    }
255833965Sjdp	}
255933965Sjdp
256033965Sjdp      if (add_symbolP)
256133965Sjdp	{
256233965Sjdp	  if (add_symbol_segment == this_segment_type && pcrel && !plt
256333965Sjdp	      && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
256433965Sjdp	    {
256533965Sjdp	      /*
256633965Sjdp	       * This fixup was made when the symbol's segment was
256733965Sjdp	       * SEG_UNKNOWN, but it is now in the local segment.
256833965Sjdp	       * So we know how to do the address without relocation.
256933965Sjdp	       */
257033965Sjdp#ifdef TC_I960
257133965Sjdp	      /* reloc_callj() may replace a 'call' with a 'calls' or a
257233965Sjdp		 'bal', in which cases it modifies *fixP as appropriate.
257333965Sjdp		 In the case of a 'calls', no further work is required,
257433965Sjdp		 and *fixP has been set up to make the rest of the code
257533965Sjdp		 below a no-op. */
257633965Sjdp	      reloc_callj (fixP);
257733965Sjdp#endif /* TC_I960 */
257833965Sjdp
257933965Sjdp	      add_number += S_GET_VALUE (add_symbolP);
258033965Sjdp	      add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
258133965Sjdp	      pcrel = 0;	/* Lie. Don't want further pcrel processing. */
258233965Sjdp
258333965Sjdp	      /* Let the target machine make the final determination
258433965Sjdp		 as to whether or not a relocation will be needed to
258533965Sjdp		 handle this fixup.  */
258633965Sjdp	      if (!TC_FORCE_RELOCATION (fixP))
258733965Sjdp		{
258833965Sjdp		  fixP->fx_pcrel = 0;
258933965Sjdp		  fixP->fx_addsy = NULL;
259033965Sjdp		}
259133965Sjdp	    }
259233965Sjdp	  else
259333965Sjdp	    {
259433965Sjdp	      if (add_symbol_segment == absolute_section
259533965Sjdp		  && ! pcrel)
259633965Sjdp		{
259733965Sjdp#ifdef TC_I960
259833965Sjdp		  /* See comment about reloc_callj() above.  */
259933965Sjdp		  reloc_callj (fixP);
260033965Sjdp#endif /* TC_I960 */
260133965Sjdp		  add_number += S_GET_VALUE (add_symbolP);
260233965Sjdp
260333965Sjdp		  /* Let the target machine make the final determination
260433965Sjdp		     as to whether or not a relocation will be needed to
260533965Sjdp		     handle this fixup.  */
260633965Sjdp
260733965Sjdp		  if (!TC_FORCE_RELOCATION (fixP))
260833965Sjdp		    {
260933965Sjdp		      fixP->fx_addsy = NULL;
261033965Sjdp		      add_symbolP = NULL;
261133965Sjdp		    }
261233965Sjdp		}
261333965Sjdp	      else if (add_symbol_segment == undefined_section
261433965Sjdp#ifdef BFD_ASSEMBLER
261533965Sjdp		       || bfd_is_com_section (add_symbol_segment)
261633965Sjdp#endif
261733965Sjdp		       )
261833965Sjdp		{
261933965Sjdp#ifdef TC_I960
262033965Sjdp		  if ((int) fixP->fx_bit_fixP == 13)
262133965Sjdp		    {
262233965Sjdp		      /* This is a COBR instruction.  They have only a
262333965Sjdp		       * 13-bit displacement and are only to be used
262433965Sjdp		       * for local branches: flag as error, don't generate
262533965Sjdp		       * relocation.
262633965Sjdp		       */
262733965Sjdp		      as_bad_where (fixP->fx_file, fixP->fx_line,
262833965Sjdp				    "can't use COBR format with external label");
262933965Sjdp		      fixP->fx_addsy = NULL;
263033965Sjdp		      fixP->fx_done = 1;
263133965Sjdp		      continue;
263233965Sjdp		    }		/* COBR */
263333965Sjdp#endif /* TC_I960 */
263433965Sjdp
263533965Sjdp#ifdef OBJ_COFF
263633965Sjdp#ifdef TE_I386AIX
263733965Sjdp		  if (S_IS_COMMON (add_symbolP))
263833965Sjdp		    add_number += S_GET_VALUE (add_symbolP);
263933965Sjdp#endif /* TE_I386AIX */
264033965Sjdp#endif /* OBJ_COFF */
264133965Sjdp		  ++seg_reloc_count;
264233965Sjdp		}
264333965Sjdp	      else
264433965Sjdp		{
264533965Sjdp		  seg_reloc_count++;
264638889Sjdp#if !(defined (TC_V850) && defined (OBJ_ELF))
264733965Sjdp#if !(defined (TC_M68K) && defined (OBJ_ELF))
264838889Sjdp#if !defined (TC_I386) || !(defined (OBJ_ELF) || defined (OBJ_COFF)) || defined (TE_PE)
264933965Sjdp		  add_number += S_GET_VALUE (add_symbolP);
265033965Sjdp#endif
265133965Sjdp#endif
265238889Sjdp#endif
265333965Sjdp		}
265433965Sjdp	    }
265533965Sjdp	}
265633965Sjdp
265733965Sjdp      if (pcrel)
265833965Sjdp	{
265933965Sjdp	  add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
266033965Sjdp	  if (add_symbolP == 0)
266133965Sjdp	    {
266233965Sjdp#ifndef BFD_ASSEMBLER
266333965Sjdp	      fixP->fx_addsy = &abs_symbol;
266433965Sjdp#else
266533965Sjdp	      fixP->fx_addsy = section_symbol (absolute_section);
266633965Sjdp#endif
266733965Sjdp	      fixP->fx_addsy->sy_used_in_reloc = 1;
266833965Sjdp	      ++seg_reloc_count;
266933965Sjdp	    }
267033965Sjdp	}
267133965Sjdp
267233965Sjdp      if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
267333965Sjdp	{
267438889Sjdp	  if ((size_t) size < sizeof (valueT))
267533965Sjdp	    {
267633965Sjdp	      valueT mask, hibit;
267733965Sjdp
267833965Sjdp	      /* set all bits to one */
267933965Sjdp	      mask = 0;
268033965Sjdp	      mask--;
268133965Sjdp	      /* Technically, combining these produces an undefined result
268233965Sjdp		 if size is sizeof (valueT), though I think these two
268333965Sjdp		 half-way operations should both be defined.  And the
268433965Sjdp		 compiler should be able to combine them if it's valid on
268533965Sjdp		 the host architecture.  */
268633965Sjdp	      mask <<= size * 4;
268733965Sjdp	      mask <<= size * 4;
268833965Sjdp	      hibit = (valueT) 1 << (size * 8 - 1);
268933965Sjdp	      if (((add_number & mask) != 0
269033965Sjdp		   || (fixP->fx_signed
269133965Sjdp		       && (add_number & hibit) != 0))
269233965Sjdp		  && ((add_number & mask) != mask
269333965Sjdp		      || (add_number & hibit) == 0))
269433965Sjdp		{
269533965Sjdp		  char buf[50], buf2[50];
269633965Sjdp		  sprint_value (buf, fragP->fr_address + where);
269733965Sjdp		  if (add_number > 1000)
269833965Sjdp		    sprint_value (buf2, add_number);
269933965Sjdp		  else
270033965Sjdp		    sprintf (buf2, "%ld", (long) add_number);
270133965Sjdp		  as_bad_where (fixP->fx_file, fixP->fx_line,
270233965Sjdp				"Value of %s too large for field of %d bytes at %s",
270333965Sjdp				buf2, size, buf);
270433965Sjdp		} /* generic error checking */
270533965Sjdp	    }
270633965Sjdp#ifdef WARN_SIGNED_OVERFLOW_WORD
270733965Sjdp	  /* Warn if a .word value is too large when treated as a signed
270833965Sjdp	     number.  We already know it is not too negative.  This is to
270933965Sjdp	     catch over-large switches generated by gcc on the 68k.  */
271033965Sjdp	  if (!flag_signed_overflow_ok
271133965Sjdp	      && size == 2
271233965Sjdp	      && add_number > 0x7fff)
271333965Sjdp	    as_bad_where (fixP->fx_file, fixP->fx_line,
271433965Sjdp			  "Signed .word overflow; switch may be too large; %ld at 0x%lx",
271533965Sjdp			  (long) add_number,
271633965Sjdp			  (unsigned long) (fragP->fr_address + where));
271733965Sjdp#endif
271833965Sjdp	}			/* not a bit fix */
271933965Sjdp
272033965Sjdp      if (!fixP->fx_done)
272133965Sjdp	{
272233965Sjdp#ifdef MD_APPLY_FIX3
272333965Sjdp	  md_apply_fix3 (fixP, &add_number, this_segment_type);
272433965Sjdp#else
272533965Sjdp#ifdef BFD_ASSEMBLER
272633965Sjdp	  md_apply_fix (fixP, &add_number);
272733965Sjdp#else
272833965Sjdp	  md_apply_fix (fixP, add_number);
272933965Sjdp#endif
273033965Sjdp#endif
273133965Sjdp
273233965Sjdp#ifndef TC_HANDLES_FX_DONE
273333965Sjdp	  /* If the tc-* files haven't been converted, assume it's handling
273433965Sjdp	     it the old way, where a null fx_addsy means that the fix has
273533965Sjdp	     been applied completely, and no further work is needed.  */
273633965Sjdp	  if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
273733965Sjdp	    fixP->fx_done = 1;
273833965Sjdp#endif
273933965Sjdp	}
274033965Sjdp#ifdef TC_VALIDATE_FIX
274133965Sjdp    skip: ;
274233965Sjdp#endif
274333965Sjdp#ifdef DEBUG5
274433965Sjdp      fprintf (stderr, "result:\n");
274533965Sjdp      print_fixup (fixP);
274633965Sjdp#endif
274733965Sjdp    }				/* For each fixS in this segment. */
274833965Sjdp
274933965Sjdp  TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
275033965Sjdp  return seg_reloc_count;
275133965Sjdp}
275233965Sjdp
275333965Sjdp#endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
275433965Sjdp
275533965Sjdpvoid
275633965Sjdpnumber_to_chars_bigendian (buf, val, n)
275733965Sjdp     char *buf;
275833965Sjdp     valueT val;
275933965Sjdp     int n;
276033965Sjdp{
276138889Sjdp  if ((size_t) n > sizeof (val) || n <= 0)
276233965Sjdp    abort ();
276333965Sjdp  while (n--)
276433965Sjdp    {
276533965Sjdp      buf[n] = val & 0xff;
276633965Sjdp      val >>= 8;
276733965Sjdp    }
276833965Sjdp}
276933965Sjdp
277033965Sjdpvoid
277133965Sjdpnumber_to_chars_littleendian (buf, val, n)
277233965Sjdp     char *buf;
277333965Sjdp     valueT val;
277433965Sjdp     int n;
277533965Sjdp{
277638889Sjdp  if ((size_t) n > sizeof (val) || n <= 0)
277733965Sjdp    abort ();
277833965Sjdp  while (n--)
277933965Sjdp    {
278033965Sjdp      *buf++ = val & 0xff;
278133965Sjdp      val >>= 8;
278233965Sjdp    }
278333965Sjdp}
278433965Sjdp
278533965Sjdpvoid
278633965Sjdpwrite_print_statistics (file)
278733965Sjdp     FILE *file;
278833965Sjdp{
278933965Sjdp  fprintf (stderr, "fixups: %d\n", n_fixups);
279033965Sjdp}
279133965Sjdp
279233965Sjdp/* for debugging */
279333965Sjdpextern int indent_level;
279433965Sjdp
279533965Sjdpvoid
279633965Sjdpprint_fixup (fixp)
279733965Sjdp     fixS *fixp;
279833965Sjdp{
279933965Sjdp  indent_level = 1;
280033965Sjdp  fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
280133965Sjdp  if (fixp->fx_pcrel)
280233965Sjdp    fprintf (stderr, " pcrel");
280333965Sjdp  if (fixp->fx_pcrel_adjust)
280433965Sjdp    fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
280533965Sjdp  if (fixp->fx_im_disp)
280633965Sjdp    {
280733965Sjdp#ifdef TC_NS32K
280833965Sjdp      fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
280933965Sjdp#else
281033965Sjdp      fprintf (stderr, " im_disp");
281133965Sjdp#endif
281233965Sjdp    }
281333965Sjdp  if (fixp->fx_tcbit)
281433965Sjdp    fprintf (stderr, " tcbit");
281533965Sjdp  if (fixp->fx_done)
281633965Sjdp    fprintf (stderr, " done");
281733965Sjdp  fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
281833965Sjdp	   fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
281933965Sjdp	   (long) fixp->fx_offset, (long) fixp->fx_addnumber);
282033965Sjdp#ifdef BFD_ASSEMBLER
282133965Sjdp  fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
282233965Sjdp	   fixp->fx_r_type);
282333965Sjdp#else
282433965Sjdp#ifdef NEED_FX_R_TYPE
282533965Sjdp  fprintf (stderr, " r_type=%d", fixp->fx_r_type);
282633965Sjdp#endif
282733965Sjdp#endif
282833965Sjdp  if (fixp->fx_addsy)
282933965Sjdp    {
283033965Sjdp      fprintf (stderr, "\n   +<");
283133965Sjdp      print_symbol_value_1 (stderr, fixp->fx_addsy);
283233965Sjdp      fprintf (stderr, ">");
283333965Sjdp    }
283433965Sjdp  if (fixp->fx_subsy)
283533965Sjdp    {
283633965Sjdp      fprintf (stderr, "\n   -<");
283733965Sjdp      print_symbol_value_1 (stderr, fixp->fx_subsy);
283833965Sjdp      fprintf (stderr, ">");
283933965Sjdp    }
284033965Sjdp  fprintf (stderr, "\n");
284138889Sjdp#ifdef TC_FIX_DATA_PRINT
284238889Sjdp  TC_FIX_DATA_PRINT (stderr, fixp);
284338889Sjdp#endif
284433965Sjdp}
284533965Sjdp
284633965Sjdp/* end of write.c */
2847