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