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