Home | History | Annotate | Line # | Download | only in bfd
cofflink.c revision 1.6
      1  1.1  christos /* COFF specific linker code.
      2  1.6  christos    Copyright (C) 1994-2016 Free Software Foundation, Inc.
      3  1.1  christos    Written by Ian Lance Taylor, Cygnus Support.
      4  1.1  christos 
      5  1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program; if not, write to the Free Software
     19  1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20  1.1  christos    MA 02110-1301, USA.  */
     21  1.1  christos 
     22  1.1  christos /* This file contains the COFF backend linker code.  */
     23  1.1  christos 
     24  1.1  christos #include "sysdep.h"
     25  1.1  christos #include "bfd.h"
     26  1.1  christos #include "bfdlink.h"
     27  1.1  christos #include "libbfd.h"
     28  1.1  christos #include "coff/internal.h"
     29  1.1  christos #include "libcoff.h"
     30  1.1  christos #include "safe-ctype.h"
     31  1.1  christos 
     32  1.3  christos static bfd_boolean coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
     33  1.3  christos static bfd_boolean coff_link_check_archive_element
     34  1.3  christos   (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
     35  1.3  christos    bfd_boolean *);
     36  1.3  christos static bfd_boolean coff_link_add_symbols (bfd *, struct bfd_link_info *);
     37  1.1  christos 
     38  1.1  christos /* Return TRUE if SYM is a weak, external symbol.  */
     39  1.1  christos #define IS_WEAK_EXTERNAL(abfd, sym)			\
     40  1.1  christos   ((sym).n_sclass == C_WEAKEXT				\
     41  1.1  christos    || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
     42  1.1  christos 
     43  1.1  christos /* Return TRUE if SYM is an external symbol.  */
     44  1.1  christos #define IS_EXTERNAL(abfd, sym)				\
     45  1.1  christos   ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
     46  1.1  christos 
     47  1.1  christos /* Define macros so that the ISFCN, et. al., macros work correctly.
     48  1.1  christos    These macros are defined in include/coff/internal.h in terms of
     49  1.1  christos    N_TMASK, etc.  These definitions require a user to define local
     50  1.1  christos    variables with the appropriate names, and with values from the
     51  1.1  christos    coff_data (abfd) structure.  */
     52  1.1  christos 
     53  1.1  christos #define N_TMASK n_tmask
     54  1.1  christos #define N_BTSHFT n_btshft
     55  1.1  christos #define N_BTMASK n_btmask
     56  1.1  christos 
     57  1.1  christos /* Create an entry in a COFF linker hash table.  */
     58  1.1  christos 
     59  1.1  christos struct bfd_hash_entry *
     60  1.1  christos _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
     61  1.1  christos 			     struct bfd_hash_table *table,
     62  1.1  christos 			     const char *string)
     63  1.1  christos {
     64  1.1  christos   struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
     65  1.1  christos 
     66  1.1  christos   /* Allocate the structure if it has not already been allocated by a
     67  1.1  christos      subclass.  */
     68  1.1  christos   if (ret == (struct coff_link_hash_entry *) NULL)
     69  1.1  christos     ret = ((struct coff_link_hash_entry *)
     70  1.1  christos 	   bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
     71  1.1  christos   if (ret == (struct coff_link_hash_entry *) NULL)
     72  1.1  christos     return (struct bfd_hash_entry *) ret;
     73  1.1  christos 
     74  1.1  christos   /* Call the allocation method of the superclass.  */
     75  1.1  christos   ret = ((struct coff_link_hash_entry *)
     76  1.1  christos 	 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
     77  1.1  christos 				 table, string));
     78  1.1  christos   if (ret != (struct coff_link_hash_entry *) NULL)
     79  1.1  christos     {
     80  1.1  christos       /* Set local fields.  */
     81  1.1  christos       ret->indx = -1;
     82  1.1  christos       ret->type = T_NULL;
     83  1.1  christos       ret->symbol_class = C_NULL;
     84  1.1  christos       ret->numaux = 0;
     85  1.1  christos       ret->auxbfd = NULL;
     86  1.1  christos       ret->aux = NULL;
     87  1.1  christos     }
     88  1.1  christos 
     89  1.1  christos   return (struct bfd_hash_entry *) ret;
     90  1.1  christos }
     91  1.1  christos 
     92  1.1  christos /* Initialize a COFF linker hash table.  */
     93  1.1  christos 
     94  1.1  christos bfd_boolean
     95  1.1  christos _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
     96  1.1  christos 				bfd *abfd,
     97  1.1  christos 				struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
     98  1.1  christos 								   struct bfd_hash_table *,
     99  1.1  christos 								   const char *),
    100  1.1  christos 				unsigned int entsize)
    101  1.1  christos {
    102  1.1  christos   memset (&table->stab_info, 0, sizeof (table->stab_info));
    103  1.1  christos   return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos /* Create a COFF linker hash table.  */
    107  1.1  christos 
    108  1.1  christos struct bfd_link_hash_table *
    109  1.1  christos _bfd_coff_link_hash_table_create (bfd *abfd)
    110  1.1  christos {
    111  1.1  christos   struct coff_link_hash_table *ret;
    112  1.1  christos   bfd_size_type amt = sizeof (struct coff_link_hash_table);
    113  1.1  christos 
    114  1.1  christos   ret = (struct coff_link_hash_table *) bfd_malloc (amt);
    115  1.1  christos   if (ret == NULL)
    116  1.1  christos     return NULL;
    117  1.1  christos 
    118  1.1  christos   if (! _bfd_coff_link_hash_table_init (ret, abfd,
    119  1.1  christos 					_bfd_coff_link_hash_newfunc,
    120  1.1  christos 					sizeof (struct coff_link_hash_entry)))
    121  1.1  christos     {
    122  1.1  christos       free (ret);
    123  1.1  christos       return (struct bfd_link_hash_table *) NULL;
    124  1.1  christos     }
    125  1.1  christos   return &ret->root;
    126  1.1  christos }
    127  1.1  christos 
    128  1.1  christos /* Create an entry in a COFF debug merge hash table.  */
    129  1.1  christos 
    130  1.1  christos struct bfd_hash_entry *
    131  1.1  christos _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
    132  1.1  christos 				    struct bfd_hash_table *table,
    133  1.1  christos 				    const char *string)
    134  1.1  christos {
    135  1.1  christos   struct coff_debug_merge_hash_entry *ret =
    136  1.1  christos     (struct coff_debug_merge_hash_entry *) entry;
    137  1.1  christos 
    138  1.1  christos   /* Allocate the structure if it has not already been allocated by a
    139  1.1  christos      subclass.  */
    140  1.1  christos   if (ret == (struct coff_debug_merge_hash_entry *) NULL)
    141  1.1  christos     ret = ((struct coff_debug_merge_hash_entry *)
    142  1.1  christos 	   bfd_hash_allocate (table,
    143  1.1  christos 			      sizeof (struct coff_debug_merge_hash_entry)));
    144  1.1  christos   if (ret == (struct coff_debug_merge_hash_entry *) NULL)
    145  1.1  christos     return (struct bfd_hash_entry *) ret;
    146  1.1  christos 
    147  1.1  christos   /* Call the allocation method of the superclass.  */
    148  1.1  christos   ret = ((struct coff_debug_merge_hash_entry *)
    149  1.1  christos 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
    150  1.1  christos   if (ret != (struct coff_debug_merge_hash_entry *) NULL)
    151  1.1  christos     {
    152  1.1  christos       /* Set local fields.  */
    153  1.1  christos       ret->types = NULL;
    154  1.1  christos     }
    155  1.1  christos 
    156  1.1  christos   return (struct bfd_hash_entry *) ret;
    157  1.1  christos }
    158  1.1  christos 
    159  1.1  christos /* Given a COFF BFD, add symbols to the global hash table as
    160  1.1  christos    appropriate.  */
    161  1.1  christos 
    162  1.1  christos bfd_boolean
    163  1.1  christos _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
    164  1.1  christos {
    165  1.1  christos   switch (bfd_get_format (abfd))
    166  1.1  christos     {
    167  1.1  christos     case bfd_object:
    168  1.1  christos       return coff_link_add_object_symbols (abfd, info);
    169  1.1  christos     case bfd_archive:
    170  1.1  christos       return _bfd_generic_link_add_archive_symbols
    171  1.1  christos 	(abfd, info, coff_link_check_archive_element);
    172  1.1  christos     default:
    173  1.1  christos       bfd_set_error (bfd_error_wrong_format);
    174  1.1  christos       return FALSE;
    175  1.1  christos     }
    176  1.1  christos }
    177  1.1  christos 
    178  1.1  christos /* Add symbols from a COFF object file.  */
    179  1.1  christos 
    180  1.1  christos static bfd_boolean
    181  1.1  christos coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
    182  1.1  christos {
    183  1.1  christos   if (! _bfd_coff_get_external_symbols (abfd))
    184  1.1  christos     return FALSE;
    185  1.1  christos   if (! coff_link_add_symbols (abfd, info))
    186  1.1  christos     return FALSE;
    187  1.1  christos 
    188  1.1  christos   if (! info->keep_memory
    189  1.1  christos       && ! _bfd_coff_free_symbols (abfd))
    190  1.1  christos     return FALSE;
    191  1.1  christos 
    192  1.1  christos   return TRUE;
    193  1.1  christos }
    194  1.1  christos 
    195  1.1  christos /* Check a single archive element to see if we need to include it in
    196  1.1  christos    the link.  *PNEEDED is set according to whether this element is
    197  1.1  christos    needed in the link or not.  This is called via
    198  1.1  christos    _bfd_generic_link_add_archive_symbols.  */
    199  1.1  christos 
    200  1.1  christos static bfd_boolean
    201  1.1  christos coff_link_check_archive_element (bfd *abfd,
    202  1.1  christos 				 struct bfd_link_info *info,
    203  1.3  christos 				 struct bfd_link_hash_entry *h,
    204  1.3  christos 				 const char *name,
    205  1.1  christos 				 bfd_boolean *pneeded)
    206  1.1  christos {
    207  1.3  christos   *pneeded = FALSE;
    208  1.1  christos 
    209  1.3  christos   /* We are only interested in symbols that are currently undefined.
    210  1.3  christos      If a symbol is currently known to be common, COFF linkers do not
    211  1.3  christos      bring in an object file which defines it.  */
    212  1.3  christos   if (h->type != bfd_link_hash_undefined)
    213  1.3  christos     return TRUE;
    214  1.1  christos 
    215  1.6  christos   /* Include this element?  */
    216  1.3  christos   if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
    217  1.6  christos     return TRUE;
    218  1.3  christos   *pneeded = TRUE;
    219  1.1  christos 
    220  1.3  christos   return coff_link_add_object_symbols (abfd, info);
    221  1.1  christos }
    222  1.1  christos 
    223  1.1  christos /* Add all the symbols from an object file to the hash table.  */
    224  1.1  christos 
    225  1.1  christos static bfd_boolean
    226  1.1  christos coff_link_add_symbols (bfd *abfd,
    227  1.1  christos 		       struct bfd_link_info *info)
    228  1.1  christos {
    229  1.1  christos   unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
    230  1.1  christos   unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
    231  1.1  christos   unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
    232  1.1  christos   bfd_boolean keep_syms;
    233  1.1  christos   bfd_boolean default_copy;
    234  1.1  christos   bfd_size_type symcount;
    235  1.1  christos   struct coff_link_hash_entry **sym_hash;
    236  1.1  christos   bfd_size_type symesz;
    237  1.1  christos   bfd_byte *esym;
    238  1.1  christos   bfd_byte *esym_end;
    239  1.1  christos   bfd_size_type amt;
    240  1.1  christos 
    241  1.1  christos   symcount = obj_raw_syment_count (abfd);
    242  1.1  christos 
    243  1.1  christos   if (symcount == 0)
    244  1.1  christos     return TRUE;		/* Nothing to do.  */
    245  1.1  christos 
    246  1.1  christos   /* Keep the symbols during this function, in case the linker needs
    247  1.1  christos      to read the generic symbols in order to report an error message.  */
    248  1.1  christos   keep_syms = obj_coff_keep_syms (abfd);
    249  1.1  christos   obj_coff_keep_syms (abfd) = TRUE;
    250  1.1  christos 
    251  1.1  christos   if (info->keep_memory)
    252  1.1  christos     default_copy = FALSE;
    253  1.1  christos   else
    254  1.1  christos     default_copy = TRUE;
    255  1.1  christos 
    256  1.1  christos   /* We keep a list of the linker hash table entries that correspond
    257  1.1  christos      to particular symbols.  */
    258  1.1  christos   amt = symcount * sizeof (struct coff_link_hash_entry *);
    259  1.1  christos   sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
    260  1.1  christos   if (sym_hash == NULL)
    261  1.1  christos     goto error_return;
    262  1.1  christos   obj_coff_sym_hashes (abfd) = sym_hash;
    263  1.1  christos 
    264  1.1  christos   symesz = bfd_coff_symesz (abfd);
    265  1.1  christos   BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
    266  1.1  christos   esym = (bfd_byte *) obj_coff_external_syms (abfd);
    267  1.1  christos   esym_end = esym + symcount * symesz;
    268  1.1  christos   while (esym < esym_end)
    269  1.1  christos     {
    270  1.1  christos       struct internal_syment sym;
    271  1.1  christos       enum coff_symbol_classification classification;
    272  1.1  christos       bfd_boolean copy;
    273  1.1  christos 
    274  1.1  christos       bfd_coff_swap_sym_in (abfd, esym, &sym);
    275  1.1  christos 
    276  1.1  christos       classification = bfd_coff_classify_symbol (abfd, &sym);
    277  1.1  christos       if (classification != COFF_SYMBOL_LOCAL)
    278  1.1  christos 	{
    279  1.1  christos 	  const char *name;
    280  1.1  christos 	  char buf[SYMNMLEN + 1];
    281  1.1  christos 	  flagword flags;
    282  1.1  christos 	  asection *section;
    283  1.1  christos 	  bfd_vma value;
    284  1.1  christos 	  bfd_boolean addit;
    285  1.1  christos 
    286  1.1  christos 	  /* This symbol is externally visible.  */
    287  1.1  christos 
    288  1.1  christos 	  name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
    289  1.1  christos 	  if (name == NULL)
    290  1.1  christos 	    goto error_return;
    291  1.1  christos 
    292  1.1  christos 	  /* We must copy the name into memory if we got it from the
    293  1.1  christos              syment itself, rather than the string table.  */
    294  1.1  christos 	  copy = default_copy;
    295  1.1  christos 	  if (sym._n._n_n._n_zeroes != 0
    296  1.1  christos 	      || sym._n._n_n._n_offset == 0)
    297  1.1  christos 	    copy = TRUE;
    298  1.1  christos 
    299  1.1  christos 	  value = sym.n_value;
    300  1.1  christos 
    301  1.1  christos 	  switch (classification)
    302  1.1  christos 	    {
    303  1.1  christos 	    default:
    304  1.1  christos 	      abort ();
    305  1.1  christos 
    306  1.1  christos 	    case COFF_SYMBOL_GLOBAL:
    307  1.1  christos 	      flags = BSF_EXPORT | BSF_GLOBAL;
    308  1.1  christos 	      section = coff_section_from_bfd_index (abfd, sym.n_scnum);
    309  1.1  christos 	      if (! obj_pe (abfd))
    310  1.1  christos 		value -= section->vma;
    311  1.1  christos 	      break;
    312  1.1  christos 
    313  1.1  christos 	    case COFF_SYMBOL_UNDEFINED:
    314  1.1  christos 	      flags = 0;
    315  1.1  christos 	      section = bfd_und_section_ptr;
    316  1.1  christos 	      break;
    317  1.1  christos 
    318  1.1  christos 	    case COFF_SYMBOL_COMMON:
    319  1.1  christos 	      flags = BSF_GLOBAL;
    320  1.1  christos 	      section = bfd_com_section_ptr;
    321  1.1  christos 	      break;
    322  1.1  christos 
    323  1.1  christos 	    case COFF_SYMBOL_PE_SECTION:
    324  1.1  christos 	      flags = BSF_SECTION_SYM | BSF_GLOBAL;
    325  1.1  christos 	      section = coff_section_from_bfd_index (abfd, sym.n_scnum);
    326  1.1  christos 	      break;
    327  1.1  christos 	    }
    328  1.1  christos 
    329  1.1  christos 	  if (IS_WEAK_EXTERNAL (abfd, sym))
    330  1.1  christos 	    flags = BSF_WEAK;
    331  1.1  christos 
    332  1.1  christos 	  addit = TRUE;
    333  1.1  christos 
    334  1.1  christos 	  /* In the PE format, section symbols actually refer to the
    335  1.1  christos              start of the output section.  We handle them specially
    336  1.1  christos              here.  */
    337  1.1  christos 	  if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
    338  1.1  christos 	    {
    339  1.1  christos 	      *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
    340  1.1  christos 						 name, FALSE, copy, FALSE);
    341  1.1  christos 	      if (*sym_hash != NULL)
    342  1.1  christos 		{
    343  1.1  christos 		  if (((*sym_hash)->coff_link_hash_flags
    344  1.1  christos 		       & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
    345  1.1  christos 		      && (*sym_hash)->root.type != bfd_link_hash_undefined
    346  1.1  christos 		      && (*sym_hash)->root.type != bfd_link_hash_undefweak)
    347  1.1  christos 		    (*_bfd_error_handler)
    348  1.1  christos 		      ("Warning: symbol `%s' is both section and non-section",
    349  1.1  christos 		       name);
    350  1.1  christos 
    351  1.1  christos 		  addit = FALSE;
    352  1.1  christos 		}
    353  1.1  christos 	    }
    354  1.1  christos 
    355  1.1  christos 	  /* The Microsoft Visual C compiler does string pooling by
    356  1.1  christos 	     hashing the constants to an internal symbol name, and
    357  1.1  christos 	     relying on the linker comdat support to discard
    358  1.1  christos 	     duplicate names.  However, if one string is a literal and
    359  1.1  christos 	     one is a data initializer, one will end up in the .data
    360  1.1  christos 	     section and one will end up in the .rdata section.  The
    361  1.1  christos 	     Microsoft linker will combine them into the .data
    362  1.1  christos 	     section, which seems to be wrong since it might cause the
    363  1.1  christos 	     literal to change.
    364  1.1  christos 
    365  1.1  christos 	     As long as there are no external references to the
    366  1.1  christos 	     symbols, which there shouldn't be, we can treat the .data
    367  1.1  christos 	     and .rdata instances as separate symbols.  The comdat
    368  1.1  christos 	     code in the linker will do the appropriate merging.  Here
    369  1.1  christos 	     we avoid getting a multiple definition error for one of
    370  1.1  christos 	     these special symbols.
    371  1.1  christos 
    372  1.1  christos 	     FIXME: I don't think this will work in the case where
    373  1.1  christos 	     there are two object files which use the constants as a
    374  1.1  christos 	     literal and two object files which use it as a data
    375  1.1  christos 	     initializer.  One or the other of the second object files
    376  1.1  christos 	     is going to wind up with an inappropriate reference.  */
    377  1.1  christos 	  if (obj_pe (abfd)
    378  1.1  christos 	      && (classification == COFF_SYMBOL_GLOBAL
    379  1.1  christos 		  || classification == COFF_SYMBOL_PE_SECTION)
    380  1.1  christos 	      && coff_section_data (abfd, section) != NULL
    381  1.1  christos 	      && coff_section_data (abfd, section)->comdat != NULL
    382  1.1  christos 	      && CONST_STRNEQ (name, "??_")
    383  1.1  christos 	      && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
    384  1.1  christos 	    {
    385  1.1  christos 	      if (*sym_hash == NULL)
    386  1.1  christos 		*sym_hash = coff_link_hash_lookup (coff_hash_table (info),
    387  1.1  christos 						   name, FALSE, copy, FALSE);
    388  1.1  christos 	      if (*sym_hash != NULL
    389  1.1  christos 		  && (*sym_hash)->root.type == bfd_link_hash_defined
    390  1.1  christos 		  && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
    391  1.1  christos 		  && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
    392  1.1  christos 			     coff_section_data (abfd, section)->comdat->name) == 0)
    393  1.1  christos 		addit = FALSE;
    394  1.1  christos 	    }
    395  1.1  christos 
    396  1.1  christos 	  if (addit)
    397  1.1  christos 	    {
    398  1.1  christos 	      if (! (bfd_coff_link_add_one_symbol
    399  1.1  christos 		     (info, abfd, name, flags, section, value,
    400  1.1  christos 		      (const char *) NULL, copy, FALSE,
    401  1.1  christos 		      (struct bfd_link_hash_entry **) sym_hash)))
    402  1.1  christos 		goto error_return;
    403  1.1  christos 	    }
    404  1.1  christos 
    405  1.1  christos 	  if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
    406  1.1  christos 	    (*sym_hash)->coff_link_hash_flags |=
    407  1.1  christos 	      COFF_LINK_HASH_PE_SECTION_SYMBOL;
    408  1.1  christos 
    409  1.1  christos 	  /* Limit the alignment of a common symbol to the possible
    410  1.1  christos              alignment of a section.  There is no point to permitting
    411  1.1  christos              a higher alignment for a common symbol: we can not
    412  1.1  christos              guarantee it, and it may cause us to allocate extra space
    413  1.1  christos              in the common section.  */
    414  1.1  christos 	  if (section == bfd_com_section_ptr
    415  1.1  christos 	      && (*sym_hash)->root.type == bfd_link_hash_common
    416  1.1  christos 	      && ((*sym_hash)->root.u.c.p->alignment_power
    417  1.1  christos 		  > bfd_coff_default_section_alignment_power (abfd)))
    418  1.1  christos 	    (*sym_hash)->root.u.c.p->alignment_power
    419  1.1  christos 	      = bfd_coff_default_section_alignment_power (abfd);
    420  1.1  christos 
    421  1.1  christos 	  if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
    422  1.1  christos 	    {
    423  1.1  christos 	      /* If we don't have any symbol information currently in
    424  1.1  christos                  the hash table, or if we are looking at a symbol
    425  1.1  christos                  definition, then update the symbol class and type in
    426  1.1  christos                  the hash table.  */
    427  1.1  christos   	      if (((*sym_hash)->symbol_class == C_NULL
    428  1.1  christos   		   && (*sym_hash)->type == T_NULL)
    429  1.1  christos   		  || sym.n_scnum != 0
    430  1.1  christos   		  || (sym.n_value != 0
    431  1.1  christos   		      && (*sym_hash)->root.type != bfd_link_hash_defined
    432  1.1  christos   		      && (*sym_hash)->root.type != bfd_link_hash_defweak))
    433  1.1  christos   		{
    434  1.1  christos   		  (*sym_hash)->symbol_class = sym.n_sclass;
    435  1.1  christos   		  if (sym.n_type != T_NULL)
    436  1.1  christos   		    {
    437  1.1  christos   		      /* We want to warn if the type changed, but not
    438  1.1  christos   			 if it changed from an unspecified type.
    439  1.1  christos   			 Testing the whole type byte may work, but the
    440  1.1  christos   			 change from (e.g.) a function of unspecified
    441  1.1  christos   			 type to function of known type also wants to
    442  1.1  christos   			 skip the warning.  */
    443  1.1  christos   		      if ((*sym_hash)->type != T_NULL
    444  1.1  christos   			  && (*sym_hash)->type != sym.n_type
    445  1.1  christos   		          && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
    446  1.1  christos   		               && (BTYPE ((*sym_hash)->type) == T_NULL
    447  1.1  christos   		                   || BTYPE (sym.n_type) == T_NULL)))
    448  1.1  christos   			(*_bfd_error_handler)
    449  1.1  christos   			  (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
    450  1.1  christos   			   abfd, name, (*sym_hash)->type, sym.n_type);
    451  1.1  christos 
    452  1.1  christos   		      /* We don't want to change from a meaningful
    453  1.1  christos   			 base type to a null one, but if we know
    454  1.1  christos   			 nothing, take what little we might now know.  */
    455  1.1  christos   		      if (BTYPE (sym.n_type) != T_NULL
    456  1.1  christos   			  || (*sym_hash)->type == T_NULL)
    457  1.1  christos 			(*sym_hash)->type = sym.n_type;
    458  1.1  christos   		    }
    459  1.1  christos   		  (*sym_hash)->auxbfd = abfd;
    460  1.1  christos 		  if (sym.n_numaux != 0)
    461  1.1  christos 		    {
    462  1.1  christos 		      union internal_auxent *alloc;
    463  1.1  christos 		      unsigned int i;
    464  1.1  christos 		      bfd_byte *eaux;
    465  1.1  christos 		      union internal_auxent *iaux;
    466  1.1  christos 
    467  1.1  christos 		      (*sym_hash)->numaux = sym.n_numaux;
    468  1.1  christos 		      alloc = ((union internal_auxent *)
    469  1.1  christos 			       bfd_hash_allocate (&info->hash->table,
    470  1.1  christos 						  (sym.n_numaux
    471  1.1  christos 						   * sizeof (*alloc))));
    472  1.1  christos 		      if (alloc == NULL)
    473  1.1  christos 			goto error_return;
    474  1.1  christos 		      for (i = 0, eaux = esym + symesz, iaux = alloc;
    475  1.1  christos 			   i < sym.n_numaux;
    476  1.1  christos 			   i++, eaux += symesz, iaux++)
    477  1.1  christos 			bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
    478  1.1  christos 					      sym.n_sclass, (int) i,
    479  1.1  christos 					      sym.n_numaux, iaux);
    480  1.1  christos 		      (*sym_hash)->aux = alloc;
    481  1.1  christos 		    }
    482  1.1  christos 		}
    483  1.1  christos 	    }
    484  1.1  christos 
    485  1.1  christos 	  if (classification == COFF_SYMBOL_PE_SECTION
    486  1.1  christos 	      && (*sym_hash)->numaux != 0)
    487  1.1  christos 	    {
    488  1.1  christos 	      /* Some PE sections (such as .bss) have a zero size in
    489  1.1  christos                  the section header, but a non-zero size in the AUX
    490  1.1  christos                  record.  Correct that here.
    491  1.1  christos 
    492  1.1  christos 		 FIXME: This is not at all the right place to do this.
    493  1.1  christos 		 For example, it won't help objdump.  This needs to be
    494  1.1  christos 		 done when we swap in the section header.  */
    495  1.1  christos 	      BFD_ASSERT ((*sym_hash)->numaux == 1);
    496  1.1  christos 	      if (section->size == 0)
    497  1.1  christos 		section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
    498  1.1  christos 
    499  1.1  christos 	      /* FIXME: We could test whether the section sizes
    500  1.1  christos                  matches the size in the aux entry, but apparently
    501  1.1  christos                  that sometimes fails unexpectedly.  */
    502  1.1  christos 	    }
    503  1.1  christos 	}
    504  1.1  christos 
    505  1.1  christos       esym += (sym.n_numaux + 1) * symesz;
    506  1.1  christos       sym_hash += sym.n_numaux + 1;
    507  1.1  christos     }
    508  1.1  christos 
    509  1.1  christos   /* If this is a non-traditional, non-relocatable link, try to
    510  1.1  christos      optimize the handling of any .stab/.stabstr sections.  */
    511  1.6  christos   if (! bfd_link_relocatable (info)
    512  1.1  christos       && ! info->traditional_format
    513  1.1  christos       && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
    514  1.1  christos       && (info->strip != strip_all && info->strip != strip_debugger))
    515  1.1  christos     {
    516  1.1  christos       asection *stabstr;
    517  1.1  christos 
    518  1.1  christos       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
    519  1.1  christos 
    520  1.1  christos       if (stabstr != NULL)
    521  1.1  christos 	{
    522  1.1  christos 	  bfd_size_type string_offset = 0;
    523  1.1  christos 	  asection *stab;
    524  1.1  christos 
    525  1.1  christos 	  for (stab = abfd->sections; stab; stab = stab->next)
    526  1.1  christos 	    if (CONST_STRNEQ (stab->name, ".stab")
    527  1.1  christos 		&& (!stab->name[5]
    528  1.1  christos 		    || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
    529  1.1  christos 	    {
    530  1.1  christos 	      struct coff_link_hash_table *table;
    531  1.1  christos 	      struct coff_section_tdata *secdata
    532  1.1  christos 		= coff_section_data (abfd, stab);
    533  1.1  christos 
    534  1.1  christos 	      if (secdata == NULL)
    535  1.1  christos 		{
    536  1.1  christos 		  amt = sizeof (struct coff_section_tdata);
    537  1.1  christos 		  stab->used_by_bfd = bfd_zalloc (abfd, amt);
    538  1.1  christos 		  if (stab->used_by_bfd == NULL)
    539  1.1  christos 		    goto error_return;
    540  1.1  christos 		  secdata = coff_section_data (abfd, stab);
    541  1.1  christos 		}
    542  1.1  christos 
    543  1.1  christos 	      table = coff_hash_table (info);
    544  1.1  christos 
    545  1.1  christos 	      if (! _bfd_link_section_stabs (abfd, &table->stab_info,
    546  1.1  christos 					     stab, stabstr,
    547  1.1  christos 					     &secdata->stab_info,
    548  1.1  christos 					     &string_offset))
    549  1.1  christos 		goto error_return;
    550  1.1  christos 	    }
    551  1.1  christos 	}
    552  1.1  christos     }
    553  1.1  christos 
    554  1.1  christos   obj_coff_keep_syms (abfd) = keep_syms;
    555  1.1  christos 
    556  1.1  christos   return TRUE;
    557  1.1  christos 
    558  1.1  christos  error_return:
    559  1.1  christos   obj_coff_keep_syms (abfd) = keep_syms;
    560  1.1  christos   return FALSE;
    561  1.1  christos }
    562  1.1  christos 
    563  1.1  christos /* Do the final link step.  */
    565  1.1  christos 
    566  1.1  christos bfd_boolean
    567  1.1  christos _bfd_coff_final_link (bfd *abfd,
    568  1.1  christos 		      struct bfd_link_info *info)
    569  1.1  christos {
    570  1.1  christos   bfd_size_type symesz;
    571  1.1  christos   struct coff_final_link_info flaginfo;
    572  1.1  christos   bfd_boolean debug_merge_allocated;
    573  1.1  christos   bfd_boolean long_section_names;
    574  1.1  christos   asection *o;
    575  1.1  christos   struct bfd_link_order *p;
    576  1.1  christos   bfd_size_type max_sym_count;
    577  1.1  christos   bfd_size_type max_lineno_count;
    578  1.1  christos   bfd_size_type max_reloc_count;
    579  1.1  christos   bfd_size_type max_output_reloc_count;
    580  1.1  christos   bfd_size_type max_contents_size;
    581  1.1  christos   file_ptr rel_filepos;
    582  1.1  christos   unsigned int relsz;
    583  1.1  christos   file_ptr line_filepos;
    584  1.1  christos   unsigned int linesz;
    585  1.1  christos   bfd *sub;
    586  1.1  christos   bfd_byte *external_relocs = NULL;
    587  1.1  christos   char strbuf[STRING_SIZE_SIZE];
    588  1.1  christos   bfd_size_type amt;
    589  1.1  christos 
    590  1.1  christos   symesz = bfd_coff_symesz (abfd);
    591  1.1  christos 
    592  1.1  christos   flaginfo.info = info;
    593  1.1  christos   flaginfo.output_bfd = abfd;
    594  1.1  christos   flaginfo.strtab = NULL;
    595  1.1  christos   flaginfo.section_info = NULL;
    596  1.1  christos   flaginfo.last_file_index = -1;
    597  1.1  christos   flaginfo.last_bf_index = -1;
    598  1.1  christos   flaginfo.internal_syms = NULL;
    599  1.1  christos   flaginfo.sec_ptrs = NULL;
    600  1.1  christos   flaginfo.sym_indices = NULL;
    601  1.1  christos   flaginfo.outsyms = NULL;
    602  1.1  christos   flaginfo.linenos = NULL;
    603  1.1  christos   flaginfo.contents = NULL;
    604  1.1  christos   flaginfo.external_relocs = NULL;
    605  1.1  christos   flaginfo.internal_relocs = NULL;
    606  1.1  christos   flaginfo.global_to_static = FALSE;
    607  1.1  christos   debug_merge_allocated = FALSE;
    608  1.1  christos 
    609  1.1  christos   coff_data (abfd)->link_info = info;
    610  1.1  christos 
    611  1.1  christos   flaginfo.strtab = _bfd_stringtab_init ();
    612  1.1  christos   if (flaginfo.strtab == NULL)
    613  1.1  christos     goto error_return;
    614  1.1  christos 
    615  1.1  christos   if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
    616  1.1  christos     goto error_return;
    617  1.1  christos   debug_merge_allocated = TRUE;
    618  1.1  christos 
    619  1.1  christos   /* Compute the file positions for all the sections.  */
    620  1.1  christos   if (! abfd->output_has_begun)
    621  1.1  christos     {
    622  1.1  christos       if (! bfd_coff_compute_section_file_positions (abfd))
    623  1.1  christos 	goto error_return;
    624  1.1  christos     }
    625  1.1  christos 
    626  1.1  christos   /* Count the line numbers and relocation entries required for the
    627  1.1  christos      output file.  Set the file positions for the relocs.  */
    628  1.1  christos   rel_filepos = obj_relocbase (abfd);
    629  1.1  christos   relsz = bfd_coff_relsz (abfd);
    630  1.1  christos   max_contents_size = 0;
    631  1.1  christos   max_lineno_count = 0;
    632  1.1  christos   max_reloc_count = 0;
    633  1.1  christos 
    634  1.1  christos   long_section_names = FALSE;
    635  1.1  christos   for (o = abfd->sections; o != NULL; o = o->next)
    636  1.1  christos     {
    637  1.1  christos       o->reloc_count = 0;
    638  1.1  christos       o->lineno_count = 0;
    639  1.1  christos       for (p = o->map_head.link_order; p != NULL; p = p->next)
    640  1.1  christos 	{
    641  1.1  christos 	  if (p->type == bfd_indirect_link_order)
    642  1.1  christos 	    {
    643  1.1  christos 	      asection *sec;
    644  1.1  christos 
    645  1.1  christos 	      sec = p->u.indirect.section;
    646  1.1  christos 
    647  1.1  christos 	      /* Mark all sections which are to be included in the
    648  1.1  christos 		 link.  This will normally be every section.  We need
    649  1.1  christos 		 to do this so that we can identify any sections which
    650  1.1  christos 		 the linker has decided to not include.  */
    651  1.1  christos 	      sec->linker_mark = TRUE;
    652  1.1  christos 
    653  1.1  christos 	      if (info->strip == strip_none
    654  1.1  christos 		  || info->strip == strip_some)
    655  1.1  christos 		o->lineno_count += sec->lineno_count;
    656  1.6  christos 
    657  1.1  christos 	      if (bfd_link_relocatable (info))
    658  1.1  christos 		o->reloc_count += sec->reloc_count;
    659  1.1  christos 
    660  1.1  christos 	      if (sec->rawsize > max_contents_size)
    661  1.1  christos 		max_contents_size = sec->rawsize;
    662  1.1  christos 	      if (sec->size > max_contents_size)
    663  1.1  christos 		max_contents_size = sec->size;
    664  1.1  christos 	      if (sec->lineno_count > max_lineno_count)
    665  1.1  christos 		max_lineno_count = sec->lineno_count;
    666  1.1  christos 	      if (sec->reloc_count > max_reloc_count)
    667  1.1  christos 		max_reloc_count = sec->reloc_count;
    668  1.6  christos 	    }
    669  1.1  christos 	  else if (bfd_link_relocatable (info)
    670  1.1  christos 		   && (p->type == bfd_section_reloc_link_order
    671  1.1  christos 		       || p->type == bfd_symbol_reloc_link_order))
    672  1.1  christos 	    ++o->reloc_count;
    673  1.1  christos 	}
    674  1.1  christos       if (o->reloc_count == 0)
    675  1.1  christos 	o->rel_filepos = 0;
    676  1.1  christos       else
    677  1.1  christos 	{
    678  1.1  christos 	  o->flags |= SEC_RELOC;
    679  1.1  christos 	  o->rel_filepos = rel_filepos;
    680  1.1  christos 	  rel_filepos += o->reloc_count * relsz;
    681  1.1  christos 	  /* In PE COFF, if there are at least 0xffff relocations an
    682  1.1  christos 	     extra relocation will be written out to encode the count.  */
    683  1.1  christos 	  if (obj_pe (abfd) && o->reloc_count >= 0xffff)
    684  1.1  christos 	    rel_filepos += relsz;
    685  1.1  christos 	}
    686  1.1  christos 
    687  1.1  christos       if (bfd_coff_long_section_names (abfd)
    688  1.1  christos 	  && strlen (o->name) > SCNNMLEN)
    689  1.1  christos 	{
    690  1.1  christos 	  /* This section has a long name which must go in the string
    691  1.1  christos              table.  This must correspond to the code in
    692  1.1  christos              coff_write_object_contents which puts the string index
    693  1.1  christos              into the s_name field of the section header.  That is why
    694  1.1  christos              we pass hash as FALSE.  */
    695  1.1  christos 	  if (_bfd_stringtab_add (flaginfo.strtab, o->name, FALSE, FALSE)
    696  1.1  christos 	      == (bfd_size_type) -1)
    697  1.1  christos 	    goto error_return;
    698  1.1  christos 	  long_section_names = TRUE;
    699  1.1  christos 	}
    700  1.1  christos     }
    701  1.1  christos 
    702  1.1  christos   /* If doing a relocatable link, allocate space for the pointers we
    703  1.6  christos      need to keep.  */
    704  1.1  christos   if (bfd_link_relocatable (info))
    705  1.1  christos     {
    706  1.1  christos       unsigned int i;
    707  1.1  christos 
    708  1.1  christos       /* We use section_count + 1, rather than section_count, because
    709  1.1  christos          the target_index fields are 1 based.  */
    710  1.1  christos       amt = abfd->section_count + 1;
    711  1.1  christos       amt *= sizeof (struct coff_link_section_info);
    712  1.1  christos       flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
    713  1.1  christos       if (flaginfo.section_info == NULL)
    714  1.1  christos 	goto error_return;
    715  1.1  christos       for (i = 0; i <= abfd->section_count; i++)
    716  1.1  christos 	{
    717  1.1  christos 	  flaginfo.section_info[i].relocs = NULL;
    718  1.1  christos 	  flaginfo.section_info[i].rel_hashes = NULL;
    719  1.1  christos 	}
    720  1.1  christos     }
    721  1.1  christos 
    722  1.1  christos   /* We now know the size of the relocs, so we can determine the file
    723  1.1  christos      positions of the line numbers.  */
    724  1.1  christos   line_filepos = rel_filepos;
    725  1.1  christos   linesz = bfd_coff_linesz (abfd);
    726  1.1  christos   max_output_reloc_count = 0;
    727  1.1  christos   for (o = abfd->sections; o != NULL; o = o->next)
    728  1.1  christos     {
    729  1.1  christos       if (o->lineno_count == 0)
    730  1.1  christos 	o->line_filepos = 0;
    731  1.1  christos       else
    732  1.1  christos 	{
    733  1.1  christos 	  o->line_filepos = line_filepos;
    734  1.1  christos 	  line_filepos += o->lineno_count * linesz;
    735  1.1  christos 	}
    736  1.1  christos 
    737  1.1  christos       if (o->reloc_count != 0)
    738  1.1  christos 	{
    739  1.1  christos 	  /* We don't know the indices of global symbols until we have
    740  1.1  christos              written out all the local symbols.  For each section in
    741  1.1  christos              the output file, we keep an array of pointers to hash
    742  1.1  christos              table entries.  Each entry in the array corresponds to a
    743  1.1  christos              reloc.  When we find a reloc against a global symbol, we
    744  1.1  christos              set the corresponding entry in this array so that we can
    745  1.1  christos              fix up the symbol index after we have written out all the
    746  1.1  christos              local symbols.
    747  1.1  christos 
    748  1.1  christos 	     Because of this problem, we also keep the relocs in
    749  1.1  christos 	     memory until the end of the link.  This wastes memory,
    750  1.1  christos 	     but only when doing a relocatable link, which is not the
    751  1.6  christos 	     common case.  */
    752  1.1  christos 	  BFD_ASSERT (bfd_link_relocatable (info));
    753  1.1  christos 	  amt = o->reloc_count;
    754  1.1  christos 	  amt *= sizeof (struct internal_reloc);
    755  1.1  christos 	  flaginfo.section_info[o->target_index].relocs =
    756  1.1  christos               (struct internal_reloc *) bfd_malloc (amt);
    757  1.1  christos 	  amt = o->reloc_count;
    758  1.1  christos 	  amt *= sizeof (struct coff_link_hash_entry *);
    759  1.1  christos 	  flaginfo.section_info[o->target_index].rel_hashes =
    760  1.1  christos               (struct coff_link_hash_entry **) bfd_malloc (amt);
    761  1.1  christos 	  if (flaginfo.section_info[o->target_index].relocs == NULL
    762  1.1  christos 	      || flaginfo.section_info[o->target_index].rel_hashes == NULL)
    763  1.1  christos 	    goto error_return;
    764  1.1  christos 
    765  1.1  christos 	  if (o->reloc_count > max_output_reloc_count)
    766  1.1  christos 	    max_output_reloc_count = o->reloc_count;
    767  1.1  christos 	}
    768  1.1  christos 
    769  1.1  christos       /* Reset the reloc and lineno counts, so that we can use them to
    770  1.1  christos 	 count the number of entries we have output so far.  */
    771  1.1  christos       o->reloc_count = 0;
    772  1.1  christos       o->lineno_count = 0;
    773  1.1  christos     }
    774  1.1  christos 
    775  1.1  christos   obj_sym_filepos (abfd) = line_filepos;
    776  1.1  christos 
    777  1.1  christos   /* Figure out the largest number of symbols in an input BFD.  Take
    778  1.1  christos      the opportunity to clear the output_has_begun fields of all the
    779  1.1  christos      input BFD's.  */
    780  1.3  christos   max_sym_count = 0;
    781  1.1  christos   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
    782  1.1  christos     {
    783  1.1  christos       size_t sz;
    784  1.1  christos 
    785  1.1  christos       sub->output_has_begun = FALSE;
    786  1.1  christos       sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
    787  1.1  christos       if (sz > max_sym_count)
    788  1.1  christos 	max_sym_count = sz;
    789  1.1  christos     }
    790  1.1  christos 
    791  1.1  christos   /* Allocate some buffers used while linking.  */
    792  1.1  christos   amt = max_sym_count * sizeof (struct internal_syment);
    793  1.1  christos   flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
    794  1.1  christos   amt = max_sym_count * sizeof (asection *);
    795  1.1  christos   flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
    796  1.1  christos   amt = max_sym_count * sizeof (long);
    797  1.1  christos   flaginfo.sym_indices = (long int *) bfd_malloc (amt);
    798  1.1  christos   flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
    799  1.1  christos   amt = max_lineno_count * bfd_coff_linesz (abfd);
    800  1.1  christos   flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
    801  1.1  christos   flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
    802  1.1  christos   amt = max_reloc_count * relsz;
    803  1.6  christos   flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
    804  1.1  christos   if (! bfd_link_relocatable (info))
    805  1.1  christos     {
    806  1.1  christos       amt = max_reloc_count * sizeof (struct internal_reloc);
    807  1.1  christos       flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
    808  1.1  christos     }
    809  1.1  christos   if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
    810  1.1  christos       || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
    811  1.1  christos       || (flaginfo.sym_indices == NULL && max_sym_count > 0)
    812  1.1  christos       || flaginfo.outsyms == NULL
    813  1.1  christos       || (flaginfo.linenos == NULL && max_lineno_count > 0)
    814  1.1  christos       || (flaginfo.contents == NULL && max_contents_size > 0)
    815  1.6  christos       || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
    816  1.1  christos       || (! bfd_link_relocatable (info)
    817  1.1  christos 	  && flaginfo.internal_relocs == NULL
    818  1.1  christos 	  && max_reloc_count > 0))
    819  1.1  christos     goto error_return;
    820  1.1  christos 
    821  1.1  christos   /* We now know the position of everything in the file, except that
    822  1.1  christos      we don't know the size of the symbol table and therefore we don't
    823  1.1  christos      know where the string table starts.  We just build the string
    824  1.1  christos      table in memory as we go along.  We process all the relocations
    825  1.1  christos      for a single input file at once.  */
    826  1.1  christos   obj_raw_syment_count (abfd) = 0;
    827  1.1  christos 
    828  1.1  christos   if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
    829  1.1  christos     {
    830  1.1  christos       if (! bfd_coff_start_final_link (abfd, info))
    831  1.1  christos 	goto error_return;
    832  1.1  christos     }
    833  1.1  christos 
    834  1.1  christos   for (o = abfd->sections; o != NULL; o = o->next)
    835  1.1  christos     {
    836  1.1  christos       for (p = o->map_head.link_order; p != NULL; p = p->next)
    837  1.1  christos 	{
    838  1.1  christos 	  if (p->type == bfd_indirect_link_order
    839  1.1  christos 	      && bfd_family_coff (p->u.indirect.section->owner))
    840  1.1  christos 	    {
    841  1.1  christos 	      sub = p->u.indirect.section->owner;
    842  1.1  christos 	      if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
    843  1.1  christos 		{
    844  1.1  christos 		  if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
    845  1.1  christos 		    goto error_return;
    846  1.1  christos 		  sub->output_has_begun = TRUE;
    847  1.1  christos 		}
    848  1.1  christos 	    }
    849  1.1  christos 	  else if (p->type == bfd_section_reloc_link_order
    850  1.1  christos 		   || p->type == bfd_symbol_reloc_link_order)
    851  1.1  christos 	    {
    852  1.1  christos 	      if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
    853  1.1  christos 		goto error_return;
    854  1.1  christos 	    }
    855  1.1  christos 	  else
    856  1.1  christos 	    {
    857  1.1  christos 	      if (! _bfd_default_link_order (abfd, info, o, p))
    858  1.1  christos 		goto error_return;
    859  1.1  christos 	    }
    860  1.1  christos 	}
    861  1.1  christos     }
    862  1.1  christos 
    863  1.1  christos   if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
    864  1.1  christos     {
    865  1.3  christos       /* Add local symbols from foreign inputs.  */
    866  1.1  christos       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
    867  1.1  christos 	{
    868  1.1  christos 	  unsigned int i;
    869  1.1  christos 
    870  1.1  christos 	  if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
    871  1.1  christos 	    continue;
    872  1.1  christos 	  for (i = 0; i < bfd_get_symcount (sub); ++i)
    873  1.1  christos 	    {
    874  1.1  christos 	      asymbol *sym = bfd_get_outsymbols (sub) [i];
    875  1.1  christos 	      file_ptr pos;
    876  1.6  christos 	      struct internal_syment isym;
    877  1.6  christos 	      union internal_auxent iaux;
    878  1.1  christos 	      bfd_size_type string_size = 0, indx;
    879  1.6  christos 	      bfd_vma written = 0;
    880  1.1  christos 	      bfd_boolean rewrite = FALSE, hash;
    881  1.1  christos 
    882  1.1  christos 	      if (! (sym->flags & BSF_LOCAL)
    883  1.1  christos 		  || (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
    884  1.1  christos 				    | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
    885  1.1  christos 				    | BSF_SYNTHETIC))
    886  1.1  christos 		  || ((sym->flags & BSF_DEBUGGING)
    887  1.1  christos 		      && ! (sym->flags & BSF_FILE)))
    888  1.1  christos 		continue;
    889  1.1  christos 
    890  1.1  christos 	      /* See if we are discarding symbols with this name.  */
    891  1.1  christos 	      if ((flaginfo.info->strip == strip_some
    892  1.1  christos 		   && (bfd_hash_lookup (flaginfo.info->keep_hash,
    893  1.1  christos 					bfd_asymbol_name(sym), FALSE, FALSE)
    894  1.1  christos 		       == NULL))
    895  1.1  christos 		  || (((flaginfo.info->discard == discard_sec_merge
    896  1.6  christos 			&& (bfd_get_section (sym)->flags & SEC_MERGE)
    897  1.1  christos 			&& ! bfd_link_relocatable (flaginfo.info))
    898  1.1  christos 		       || flaginfo.info->discard == discard_l)
    899  1.1  christos 		      && bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
    900  1.1  christos 		continue;
    901  1.1  christos 
    902  1.1  christos 	      pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
    903  1.1  christos 					     * symesz;
    904  1.1  christos 	      if (bfd_seek (abfd, pos, SEEK_SET) != 0)
    905  1.6  christos 		goto error_return;
    906  1.1  christos 	      if (! coff_write_alien_symbol(abfd, sym, &isym, &iaux, &written,
    907  1.1  christos 					    &string_size, NULL, NULL))
    908  1.1  christos 		goto error_return;
    909  1.6  christos 
    910  1.6  christos 	      hash = !flaginfo.info->traditional_format;
    911  1.6  christos 
    912  1.6  christos 	      if (string_size >= 6 && isym.n_sclass == C_FILE
    913  1.6  christos 		  && ! isym._n._n_n._n_zeroes && isym.n_numaux)
    914  1.6  christos 		{
    915  1.6  christos 		  indx = _bfd_stringtab_add (flaginfo.strtab, ".file", hash,
    916  1.6  christos 					     FALSE);
    917  1.6  christos 		  if (indx == (bfd_size_type) -1)
    918  1.6  christos 		    goto error_return;
    919  1.6  christos 		  isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
    920  1.6  christos 		  bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
    921  1.6  christos 		  if (bfd_seek (abfd, pos, SEEK_SET) != 0
    922  1.6  christos 		      || bfd_bwrite (flaginfo.outsyms, symesz,
    923  1.6  christos 				     abfd) != symesz)
    924  1.6  christos 		    goto error_return;
    925  1.6  christos 		  string_size -= 6;
    926  1.6  christos 		}
    927  1.1  christos 
    928  1.1  christos 	      if (string_size)
    929  1.1  christos 		{
    930  1.1  christos 		  indx = _bfd_stringtab_add (flaginfo.strtab,
    931  1.1  christos 					     bfd_asymbol_name (sym), hash,
    932  1.1  christos 					     FALSE);
    933  1.1  christos 		  if (indx == (bfd_size_type) -1)
    934  1.6  christos 		    goto error_return;
    935  1.6  christos 		  if (isym.n_sclass != C_FILE)
    936  1.6  christos 		    {
    937  1.6  christos 		      isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
    938  1.6  christos 		      bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
    939  1.6  christos 		      rewrite = TRUE;
    940  1.6  christos 		    }
    941  1.6  christos 		  else
    942  1.6  christos 		    {
    943  1.6  christos 		      BFD_ASSERT (isym.n_numaux == 1);
    944  1.6  christos 		      iaux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
    945  1.6  christos 		      bfd_coff_swap_aux_out (abfd, &iaux, isym.n_type, C_FILE,
    946  1.6  christos 					     0, 1, flaginfo.outsyms + symesz);
    947  1.6  christos 		      if (bfd_seek (abfd, pos + symesz, SEEK_SET) != 0
    948  1.6  christos 			  || bfd_bwrite (flaginfo.outsyms + symesz, symesz,
    949  1.6  christos 					 abfd) != symesz)
    950  1.6  christos 			goto error_return;
    951  1.1  christos 		    }
    952  1.1  christos 		}
    953  1.1  christos 
    954  1.1  christos 	      if (isym.n_sclass == C_FILE)
    955  1.1  christos 		{
    956  1.1  christos 		  if (flaginfo.last_file_index != -1)
    957  1.1  christos 		    {
    958  1.1  christos 		      flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
    959  1.1  christos 		      bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
    960  1.1  christos 					     flaginfo.outsyms);
    961  1.1  christos 		      pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
    962  1.1  christos 						     * symesz;
    963  1.1  christos 		      rewrite = TRUE;
    964  1.1  christos 		    }
    965  1.1  christos 		  flaginfo.last_file_index = obj_raw_syment_count (abfd);
    966  1.1  christos 		  flaginfo.last_file = isym;
    967  1.1  christos 		}
    968  1.1  christos 
    969  1.1  christos 	      if (rewrite
    970  1.1  christos 		  && (bfd_seek (abfd, pos, SEEK_SET) != 0
    971  1.1  christos 		      || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz))
    972  1.1  christos 		goto error_return;
    973  1.1  christos 
    974  1.1  christos 	      obj_raw_syment_count (abfd) += written;
    975  1.1  christos 	    }
    976  1.1  christos 	}
    977  1.1  christos     }
    978  1.1  christos 
    979  1.1  christos   if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
    980  1.1  christos     goto error_return;
    981  1.1  christos 
    982  1.1  christos   /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
    983  1.1  christos 
    984  1.1  christos   coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
    985  1.1  christos   debug_merge_allocated = FALSE;
    986  1.1  christos 
    987  1.1  christos   if (flaginfo.internal_syms != NULL)
    988  1.1  christos     {
    989  1.1  christos       free (flaginfo.internal_syms);
    990  1.1  christos       flaginfo.internal_syms = NULL;
    991  1.1  christos     }
    992  1.1  christos   if (flaginfo.sec_ptrs != NULL)
    993  1.1  christos     {
    994  1.1  christos       free (flaginfo.sec_ptrs);
    995  1.1  christos       flaginfo.sec_ptrs = NULL;
    996  1.1  christos     }
    997  1.1  christos   if (flaginfo.sym_indices != NULL)
    998  1.1  christos     {
    999  1.1  christos       free (flaginfo.sym_indices);
   1000  1.1  christos       flaginfo.sym_indices = NULL;
   1001  1.1  christos     }
   1002  1.1  christos   if (flaginfo.linenos != NULL)
   1003  1.1  christos     {
   1004  1.1  christos       free (flaginfo.linenos);
   1005  1.1  christos       flaginfo.linenos = NULL;
   1006  1.1  christos     }
   1007  1.1  christos   if (flaginfo.contents != NULL)
   1008  1.1  christos     {
   1009  1.1  christos       free (flaginfo.contents);
   1010  1.1  christos       flaginfo.contents = NULL;
   1011  1.1  christos     }
   1012  1.1  christos   if (flaginfo.external_relocs != NULL)
   1013  1.1  christos     {
   1014  1.1  christos       free (flaginfo.external_relocs);
   1015  1.1  christos       flaginfo.external_relocs = NULL;
   1016  1.1  christos     }
   1017  1.1  christos   if (flaginfo.internal_relocs != NULL)
   1018  1.1  christos     {
   1019  1.1  christos       free (flaginfo.internal_relocs);
   1020  1.1  christos       flaginfo.internal_relocs = NULL;
   1021  1.1  christos     }
   1022  1.1  christos 
   1023  1.1  christos   /* The value of the last C_FILE symbol is supposed to be the symbol
   1024  1.1  christos      index of the first external symbol.  Write it out again if
   1025  1.1  christos      necessary.  */
   1026  1.1  christos   if (flaginfo.last_file_index != -1
   1027  1.1  christos       && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
   1028  1.1  christos     {
   1029  1.1  christos       file_ptr pos;
   1030  1.1  christos 
   1031  1.1  christos       flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
   1032  1.1  christos       bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
   1033  1.1  christos 			     flaginfo.outsyms);
   1034  1.1  christos 
   1035  1.1  christos       pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
   1036  1.1  christos       if (bfd_seek (abfd, pos, SEEK_SET) != 0
   1037  1.1  christos 	  || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
   1038  1.1  christos 	return FALSE;
   1039  1.1  christos     }
   1040  1.1  christos 
   1041  1.1  christos   /* If doing task linking (ld --task-link) then make a pass through the
   1042  1.1  christos      global symbols, writing out any that are defined, and making them
   1043  1.1  christos      static.  */
   1044  1.1  christos   if (info->task_link)
   1045  1.1  christos     {
   1046  1.1  christos       flaginfo.failed = FALSE;
   1047  1.1  christos       coff_link_hash_traverse (coff_hash_table (info),
   1048  1.1  christos 			       _bfd_coff_write_task_globals, &flaginfo);
   1049  1.1  christos       if (flaginfo.failed)
   1050  1.1  christos 	goto error_return;
   1051  1.1  christos     }
   1052  1.1  christos 
   1053  1.1  christos   /* Write out the global symbols.  */
   1054  1.1  christos   flaginfo.failed = FALSE;
   1055  1.1  christos   bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
   1056  1.1  christos   if (flaginfo.failed)
   1057  1.1  christos     goto error_return;
   1058  1.1  christos 
   1059  1.1  christos   /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
   1060  1.1  christos   if (flaginfo.outsyms != NULL)
   1061  1.1  christos     {
   1062  1.1  christos       free (flaginfo.outsyms);
   1063  1.1  christos       flaginfo.outsyms = NULL;
   1064  1.1  christos     }
   1065  1.6  christos 
   1066  1.1  christos   if (bfd_link_relocatable (info) && max_output_reloc_count > 0)
   1067  1.1  christos     {
   1068  1.1  christos       /* Now that we have written out all the global symbols, we know
   1069  1.1  christos 	 the symbol indices to use for relocs against them, and we can
   1070  1.1  christos 	 finally write out the relocs.  */
   1071  1.1  christos       amt = max_output_reloc_count * relsz;
   1072  1.1  christos       external_relocs = (bfd_byte *) bfd_malloc (amt);
   1073  1.1  christos       if (external_relocs == NULL)
   1074  1.1  christos 	goto error_return;
   1075  1.1  christos 
   1076  1.1  christos       for (o = abfd->sections; o != NULL; o = o->next)
   1077  1.1  christos 	{
   1078  1.1  christos 	  struct internal_reloc *irel;
   1079  1.1  christos 	  struct internal_reloc *irelend;
   1080  1.1  christos 	  struct coff_link_hash_entry **rel_hash;
   1081  1.1  christos 	  bfd_byte *erel;
   1082  1.1  christos 
   1083  1.1  christos 	  if (o->reloc_count == 0)
   1084  1.1  christos 	    continue;
   1085  1.1  christos 
   1086  1.1  christos 	  irel = flaginfo.section_info[o->target_index].relocs;
   1087  1.1  christos 	  irelend = irel + o->reloc_count;
   1088  1.1  christos 	  rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
   1089  1.1  christos 	  erel = external_relocs;
   1090  1.1  christos 	  for (; irel < irelend; irel++, rel_hash++, erel += relsz)
   1091  1.1  christos 	    {
   1092  1.1  christos 	      if (*rel_hash != NULL)
   1093  1.1  christos 		{
   1094  1.1  christos 		  BFD_ASSERT ((*rel_hash)->indx >= 0);
   1095  1.1  christos 		  irel->r_symndx = (*rel_hash)->indx;
   1096  1.1  christos 		}
   1097  1.1  christos 	      bfd_coff_swap_reloc_out (abfd, irel, erel);
   1098  1.1  christos 	    }
   1099  1.1  christos 
   1100  1.1  christos 	  if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
   1101  1.1  christos 	    goto error_return;
   1102  1.1  christos 	  if (obj_pe (abfd) && o->reloc_count >= 0xffff)
   1103  1.1  christos 	    {
   1104  1.1  christos 	      /* In PE COFF, write the count of relocs as the first
   1105  1.1  christos 		 reloc.  The header overflow bit will be set
   1106  1.1  christos 		 elsewhere. */
   1107  1.1  christos 	      struct internal_reloc incount;
   1108  1.1  christos 	      bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
   1109  1.1  christos 
   1110  1.1  christos 	      memset (&incount, 0, sizeof (incount));
   1111  1.1  christos 	      incount.r_vaddr = o->reloc_count + 1;
   1112  1.1  christos 	      bfd_coff_swap_reloc_out (abfd, &incount, excount);
   1113  1.1  christos 	      if (bfd_bwrite (excount, relsz, abfd) != relsz)
   1114  1.1  christos 		/* We'll leak, but it's an error anyway. */
   1115  1.1  christos 		goto error_return;
   1116  1.1  christos 	      free (excount);
   1117  1.1  christos 	    }
   1118  1.1  christos 	  if (bfd_bwrite (external_relocs,
   1119  1.1  christos 			  (bfd_size_type) relsz * o->reloc_count, abfd)
   1120  1.1  christos 	      != (bfd_size_type) relsz * o->reloc_count)
   1121  1.1  christos 	    goto error_return;
   1122  1.1  christos 	}
   1123  1.1  christos 
   1124  1.1  christos       free (external_relocs);
   1125  1.1  christos       external_relocs = NULL;
   1126  1.1  christos     }
   1127  1.1  christos 
   1128  1.1  christos   /* Free up the section information.  */
   1129  1.1  christos   if (flaginfo.section_info != NULL)
   1130  1.1  christos     {
   1131  1.1  christos       unsigned int i;
   1132  1.1  christos 
   1133  1.1  christos       for (i = 0; i < abfd->section_count; i++)
   1134  1.1  christos 	{
   1135  1.1  christos 	  if (flaginfo.section_info[i].relocs != NULL)
   1136  1.1  christos 	    free (flaginfo.section_info[i].relocs);
   1137  1.1  christos 	  if (flaginfo.section_info[i].rel_hashes != NULL)
   1138  1.1  christos 	    free (flaginfo.section_info[i].rel_hashes);
   1139  1.1  christos 	}
   1140  1.1  christos       free (flaginfo.section_info);
   1141  1.1  christos       flaginfo.section_info = NULL;
   1142  1.1  christos     }
   1143  1.1  christos 
   1144  1.1  christos   /* If we have optimized stabs strings, output them.  */
   1145  1.1  christos   if (coff_hash_table (info)->stab_info.stabstr != NULL)
   1146  1.1  christos     {
   1147  1.1  christos       if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
   1148  1.1  christos 	return FALSE;
   1149  1.1  christos     }
   1150  1.1  christos 
   1151  1.1  christos   /* Write out the string table.  */
   1152  1.1  christos   if (obj_raw_syment_count (abfd) != 0 || long_section_names)
   1153  1.1  christos     {
   1154  1.1  christos       file_ptr pos;
   1155  1.1  christos 
   1156  1.1  christos       pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
   1157  1.1  christos       if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   1158  1.1  christos 	return FALSE;
   1159  1.1  christos 
   1160  1.1  christos #if STRING_SIZE_SIZE == 4
   1161  1.1  christos       H_PUT_32 (abfd,
   1162  1.1  christos 		_bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
   1163  1.1  christos 		strbuf);
   1164  1.1  christos #else
   1165  1.1  christos  #error Change H_PUT_32 above
   1166  1.1  christos #endif
   1167  1.1  christos 
   1168  1.1  christos       if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
   1169  1.1  christos 	  != STRING_SIZE_SIZE)
   1170  1.1  christos 	return FALSE;
   1171  1.1  christos 
   1172  1.1  christos       if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
   1173  1.1  christos 	return FALSE;
   1174  1.1  christos 
   1175  1.1  christos       obj_coff_strings_written (abfd) = TRUE;
   1176  1.1  christos     }
   1177  1.1  christos 
   1178  1.1  christos   _bfd_stringtab_free (flaginfo.strtab);
   1179  1.1  christos 
   1180  1.1  christos   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
   1181  1.1  christos      not try to write out the symbols.  */
   1182  1.1  christos   bfd_get_symcount (abfd) = 0;
   1183  1.1  christos 
   1184  1.1  christos   return TRUE;
   1185  1.1  christos 
   1186  1.1  christos  error_return:
   1187  1.1  christos   if (debug_merge_allocated)
   1188  1.1  christos     coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
   1189  1.1  christos   if (flaginfo.strtab != NULL)
   1190  1.1  christos     _bfd_stringtab_free (flaginfo.strtab);
   1191  1.1  christos   if (flaginfo.section_info != NULL)
   1192  1.1  christos     {
   1193  1.1  christos       unsigned int i;
   1194  1.1  christos 
   1195  1.1  christos       for (i = 0; i < abfd->section_count; i++)
   1196  1.1  christos 	{
   1197  1.1  christos 	  if (flaginfo.section_info[i].relocs != NULL)
   1198  1.1  christos 	    free (flaginfo.section_info[i].relocs);
   1199  1.1  christos 	  if (flaginfo.section_info[i].rel_hashes != NULL)
   1200  1.1  christos 	    free (flaginfo.section_info[i].rel_hashes);
   1201  1.1  christos 	}
   1202  1.1  christos       free (flaginfo.section_info);
   1203  1.1  christos     }
   1204  1.1  christos   if (flaginfo.internal_syms != NULL)
   1205  1.1  christos     free (flaginfo.internal_syms);
   1206  1.1  christos   if (flaginfo.sec_ptrs != NULL)
   1207  1.1  christos     free (flaginfo.sec_ptrs);
   1208  1.1  christos   if (flaginfo.sym_indices != NULL)
   1209  1.1  christos     free (flaginfo.sym_indices);
   1210  1.1  christos   if (flaginfo.outsyms != NULL)
   1211  1.1  christos     free (flaginfo.outsyms);
   1212  1.1  christos   if (flaginfo.linenos != NULL)
   1213  1.1  christos     free (flaginfo.linenos);
   1214  1.1  christos   if (flaginfo.contents != NULL)
   1215  1.1  christos     free (flaginfo.contents);
   1216  1.1  christos   if (flaginfo.external_relocs != NULL)
   1217  1.1  christos     free (flaginfo.external_relocs);
   1218  1.1  christos   if (flaginfo.internal_relocs != NULL)
   1219  1.1  christos     free (flaginfo.internal_relocs);
   1220  1.1  christos   if (external_relocs != NULL)
   1221  1.1  christos     free (external_relocs);
   1222  1.1  christos   return FALSE;
   1223  1.1  christos }
   1224  1.1  christos 
   1225  1.1  christos /* Parse out a -heap <reserved>,<commit> line.  */
   1226  1.1  christos 
   1227  1.1  christos static char *
   1228  1.1  christos dores_com (char *ptr, bfd *output_bfd, int heap)
   1229  1.1  christos {
   1230  1.1  christos   if (coff_data(output_bfd)->pe)
   1231  1.1  christos     {
   1232  1.1  christos       int val = strtoul (ptr, &ptr, 0);
   1233  1.1  christos 
   1234  1.1  christos       if (heap)
   1235  1.1  christos 	pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
   1236  1.1  christos       else
   1237  1.1  christos 	pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
   1238  1.1  christos 
   1239  1.1  christos       if (ptr[0] == ',')
   1240  1.1  christos 	{
   1241  1.1  christos 	  val = strtoul (ptr+1, &ptr, 0);
   1242  1.1  christos 	  if (heap)
   1243  1.1  christos 	    pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
   1244  1.1  christos 	  else
   1245  1.1  christos 	    pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
   1246  1.1  christos 	}
   1247  1.1  christos     }
   1248  1.1  christos   return ptr;
   1249  1.1  christos }
   1250  1.1  christos 
   1251  1.1  christos static char *
   1252  1.1  christos get_name (char *ptr, char **dst)
   1253  1.1  christos {
   1254  1.1  christos   while (*ptr == ' ')
   1255  1.1  christos     ptr++;
   1256  1.1  christos   *dst = ptr;
   1257  1.1  christos   while (*ptr && *ptr != ' ')
   1258  1.1  christos     ptr++;
   1259  1.1  christos   *ptr = 0;
   1260  1.1  christos   return ptr+1;
   1261  1.1  christos }
   1262  1.1  christos 
   1263  1.1  christos /* Process any magic embedded commands in a section called .drectve.  */
   1264  1.1  christos 
   1265  1.1  christos static int
   1266  1.1  christos process_embedded_commands (bfd *output_bfd,
   1267  1.1  christos 			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1268  1.1  christos 			   bfd *abfd)
   1269  1.1  christos {
   1270  1.1  christos   asection *sec = bfd_get_section_by_name (abfd, ".drectve");
   1271  1.1  christos   char *s;
   1272  1.1  christos   char *e;
   1273  1.1  christos   bfd_byte *copy;
   1274  1.1  christos 
   1275  1.1  christos   if (!sec)
   1276  1.1  christos     return 1;
   1277  1.1  christos 
   1278  1.1  christos   if (!bfd_malloc_and_get_section (abfd, sec, &copy))
   1279  1.1  christos     {
   1280  1.1  christos       if (copy != NULL)
   1281  1.1  christos 	free (copy);
   1282  1.1  christos       return 0;
   1283  1.1  christos     }
   1284  1.1  christos   e = (char *) copy + sec->size;
   1285  1.1  christos 
   1286  1.1  christos   for (s = (char *) copy; s < e ; )
   1287  1.1  christos     {
   1288  1.1  christos       if (s[0] != '-')
   1289  1.1  christos 	{
   1290  1.1  christos 	  s++;
   1291  1.1  christos 	  continue;
   1292  1.1  christos 	}
   1293  1.1  christos       if (CONST_STRNEQ (s, "-attr"))
   1294  1.1  christos 	{
   1295  1.1  christos 	  char *name;
   1296  1.1  christos 	  char *attribs;
   1297  1.1  christos 	  asection *asec;
   1298  1.1  christos 	  int loop = 1;
   1299  1.1  christos 	  int had_write = 0;
   1300  1.1  christos 	  int had_exec= 0;
   1301  1.1  christos 
   1302  1.1  christos 	  s += 5;
   1303  1.1  christos 	  s = get_name (s, &name);
   1304  1.1  christos 	  s = get_name (s, &attribs);
   1305  1.1  christos 
   1306  1.1  christos 	  while (loop)
   1307  1.1  christos 	    {
   1308  1.1  christos 	      switch (*attribs++)
   1309  1.1  christos 		{
   1310  1.1  christos 		case 'W':
   1311  1.1  christos 		  had_write = 1;
   1312  1.1  christos 		  break;
   1313  1.1  christos 		case 'R':
   1314  1.1  christos 		  break;
   1315  1.1  christos 		case 'S':
   1316  1.1  christos 		  break;
   1317  1.1  christos 		case 'X':
   1318  1.1  christos 		  had_exec = 1;
   1319  1.1  christos 		  break;
   1320  1.1  christos 		default:
   1321  1.1  christos 		  loop = 0;
   1322  1.1  christos 		}
   1323  1.1  christos 	    }
   1324  1.1  christos 	  asec = bfd_get_section_by_name (abfd, name);
   1325  1.1  christos 	  if (asec)
   1326  1.1  christos 	    {
   1327  1.1  christos 	      if (had_exec)
   1328  1.1  christos 		asec->flags |= SEC_CODE;
   1329  1.1  christos 	      if (!had_write)
   1330  1.1  christos 		asec->flags |= SEC_READONLY;
   1331  1.1  christos 	    }
   1332  1.1  christos 	}
   1333  1.1  christos       else if (CONST_STRNEQ (s, "-heap"))
   1334  1.1  christos 	s = dores_com (s + 5, output_bfd, 1);
   1335  1.1  christos 
   1336  1.1  christos       else if (CONST_STRNEQ (s, "-stack"))
   1337  1.1  christos 	s = dores_com (s + 6, output_bfd, 0);
   1338  1.1  christos 
   1339  1.1  christos       /* GNU extension for aligned commons.  */
   1340  1.1  christos       else if (CONST_STRNEQ (s, "-aligncomm:"))
   1341  1.1  christos 	{
   1342  1.1  christos 	  /* Common symbols must be aligned on reading, as it
   1343  1.1  christos 	  is too late to do anything here, after they have
   1344  1.1  christos 	  already been allocated, so just skip the directive.  */
   1345  1.1  christos 	  s += 11;
   1346  1.1  christos 	}
   1347  1.1  christos 
   1348  1.1  christos       else
   1349  1.1  christos 	s++;
   1350  1.1  christos     }
   1351  1.1  christos   free (copy);
   1352  1.1  christos   return 1;
   1353  1.1  christos }
   1354  1.1  christos 
   1355  1.1  christos /* Place a marker against all symbols which are used by relocations.
   1356  1.1  christos    This marker can be picked up by the 'do we skip this symbol ?'
   1357  1.1  christos    loop in _bfd_coff_link_input_bfd() and used to prevent skipping
   1358  1.1  christos    that symbol.  */
   1359  1.1  christos 
   1360  1.1  christos static void
   1361  1.1  christos mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd)
   1362  1.1  christos {
   1363  1.1  christos   asection * a;
   1364  1.1  christos 
   1365  1.1  christos   if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
   1366  1.1  christos     return;
   1367  1.1  christos 
   1368  1.1  christos   for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
   1369  1.1  christos     {
   1370  1.1  christos       struct internal_reloc *	internal_relocs;
   1371  1.1  christos       struct internal_reloc *	irel;
   1372  1.1  christos       struct internal_reloc *	irelend;
   1373  1.1  christos 
   1374  1.1  christos       if ((a->flags & SEC_RELOC) == 0 || a->reloc_count  < 1
   1375  1.1  christos 	  || a->linker_mark == 0)
   1376  1.1  christos 	continue;
   1377  1.1  christos       /* Don't mark relocs in excluded sections.  */
   1378  1.1  christos       if (a->output_section == bfd_abs_section_ptr)
   1379  1.1  christos 	continue;
   1380  1.1  christos 
   1381  1.1  christos       /* Read in the relocs.  */
   1382  1.1  christos       internal_relocs = _bfd_coff_read_internal_relocs
   1383  1.1  christos 	(input_bfd, a, FALSE,
   1384  1.6  christos 	 flaginfo->external_relocs,
   1385  1.6  christos 	 bfd_link_relocatable (flaginfo->info),
   1386  1.1  christos 	 (bfd_link_relocatable (flaginfo->info)
   1387  1.1  christos 	  ? (flaginfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
   1388  1.1  christos 	  : flaginfo->internal_relocs)
   1389  1.1  christos 	);
   1390  1.1  christos 
   1391  1.1  christos       if (internal_relocs == NULL)
   1392  1.1  christos 	continue;
   1393  1.1  christos 
   1394  1.1  christos       irel     = internal_relocs;
   1395  1.1  christos       irelend  = irel + a->reloc_count;
   1396  1.1  christos 
   1397  1.1  christos       /* Place a mark in the sym_indices array (whose entries have
   1398  1.1  christos 	 been initialised to 0) for all of the symbols that are used
   1399  1.1  christos 	 in the relocation table.  This will then be picked up in the
   1400  1.1  christos 	 skip/don't-skip pass.  */
   1401  1.6  christos       for (; irel < irelend; irel++)
   1402  1.6  christos 	if ((unsigned long) irel->r_symndx < obj_raw_syment_count (input_bfd))
   1403  1.1  christos 	  flaginfo->sym_indices[irel->r_symndx] = -1;
   1404  1.1  christos     }
   1405  1.1  christos }
   1406  1.1  christos 
   1407  1.1  christos /* Link an input file into the linker output file.  This function
   1408  1.1  christos    handles all the sections and relocations of the input file at once.  */
   1409  1.1  christos 
   1410  1.1  christos bfd_boolean
   1411  1.1  christos _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
   1412  1.1  christos {
   1413  1.1  christos   unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
   1414  1.1  christos   unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
   1415  1.1  christos   bfd_boolean (*adjust_symndx)
   1416  1.1  christos     (bfd *, struct bfd_link_info *, bfd *, asection *,
   1417  1.1  christos      struct internal_reloc *, bfd_boolean *);
   1418  1.1  christos   bfd *output_bfd;
   1419  1.1  christos   const char *strings;
   1420  1.1  christos   bfd_size_type syment_base;
   1421  1.1  christos   bfd_boolean copy, hash;
   1422  1.1  christos   bfd_size_type isymesz;
   1423  1.1  christos   bfd_size_type osymesz;
   1424  1.1  christos   bfd_size_type linesz;
   1425  1.1  christos   bfd_byte *esym;
   1426  1.1  christos   bfd_byte *esym_end;
   1427  1.1  christos   struct internal_syment *isymp;
   1428  1.1  christos   asection **secpp;
   1429  1.1  christos   long *indexp;
   1430  1.1  christos   unsigned long output_index;
   1431  1.1  christos   bfd_byte *outsym;
   1432  1.1  christos   struct coff_link_hash_entry **sym_hash;
   1433  1.1  christos   asection *o;
   1434  1.1  christos 
   1435  1.1  christos   /* Move all the symbols to the output file.  */
   1436  1.1  christos 
   1437  1.1  christos   output_bfd = flaginfo->output_bfd;
   1438  1.1  christos   strings = NULL;
   1439  1.1  christos   syment_base = obj_raw_syment_count (output_bfd);
   1440  1.1  christos   isymesz = bfd_coff_symesz (input_bfd);
   1441  1.1  christos   osymesz = bfd_coff_symesz (output_bfd);
   1442  1.1  christos   linesz = bfd_coff_linesz (input_bfd);
   1443  1.1  christos   BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
   1444  1.1  christos 
   1445  1.1  christos   copy = FALSE;
   1446  1.1  christos   if (! flaginfo->info->keep_memory)
   1447  1.1  christos     copy = TRUE;
   1448  1.6  christos   hash = TRUE;
   1449  1.1  christos   if (flaginfo->info->traditional_format)
   1450  1.1  christos     hash = FALSE;
   1451  1.1  christos 
   1452  1.1  christos   if (! _bfd_coff_get_external_symbols (input_bfd))
   1453  1.1  christos     return FALSE;
   1454  1.1  christos 
   1455  1.1  christos   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
   1456  1.1  christos   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
   1457  1.1  christos   isymp = flaginfo->internal_syms;
   1458  1.1  christos   secpp = flaginfo->sec_ptrs;
   1459  1.1  christos   indexp = flaginfo->sym_indices;
   1460  1.1  christos   output_index = syment_base;
   1461  1.1  christos   outsym = flaginfo->outsyms;
   1462  1.1  christos 
   1463  1.1  christos   if (coff_data (output_bfd)->pe
   1464  1.1  christos       && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd))
   1465  1.1  christos     return FALSE;
   1466  1.1  christos 
   1467  1.1  christos   /* If we are going to perform relocations and also strip/discard some
   1468  1.1  christos      symbols then we must make sure that we do not strip/discard those
   1469  1.1  christos      symbols that are going to be involved in the relocations.  */
   1470  1.1  christos   if ((   flaginfo->info->strip   != strip_none
   1471  1.6  christos        || flaginfo->info->discard != discard_none)
   1472  1.1  christos       && bfd_link_relocatable (flaginfo->info))
   1473  1.1  christos     {
   1474  1.1  christos       /* Mark the symbol array as 'not-used'.  */
   1475  1.1  christos       memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
   1476  1.1  christos 
   1477  1.1  christos       mark_relocs (flaginfo, input_bfd);
   1478  1.1  christos     }
   1479  1.1  christos 
   1480  1.1  christos   while (esym < esym_end)
   1481  1.1  christos     {
   1482  1.1  christos       struct internal_syment isym;
   1483  1.1  christos       enum coff_symbol_classification classification;
   1484  1.1  christos       bfd_boolean skip;
   1485  1.1  christos       bfd_boolean global;
   1486  1.1  christos       bfd_boolean dont_skip_symbol;
   1487  1.1  christos       int add;
   1488  1.1  christos 
   1489  1.1  christos       bfd_coff_swap_sym_in (input_bfd, esym, isymp);
   1490  1.1  christos 
   1491  1.1  christos       /* Make a copy of *isymp so that the relocate_section function
   1492  1.1  christos 	 always sees the original values.  This is more reliable than
   1493  1.1  christos 	 always recomputing the symbol value even if we are stripping
   1494  1.1  christos 	 the symbol.  */
   1495  1.1  christos       isym = *isymp;
   1496  1.1  christos 
   1497  1.1  christos       classification = bfd_coff_classify_symbol (input_bfd, &isym);
   1498  1.1  christos       switch (classification)
   1499  1.1  christos 	{
   1500  1.1  christos 	default:
   1501  1.1  christos 	  abort ();
   1502  1.1  christos 	case COFF_SYMBOL_GLOBAL:
   1503  1.1  christos 	case COFF_SYMBOL_PE_SECTION:
   1504  1.1  christos 	case COFF_SYMBOL_LOCAL:
   1505  1.1  christos 	  *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
   1506  1.1  christos 	  break;
   1507  1.1  christos 	case COFF_SYMBOL_COMMON:
   1508  1.1  christos 	  *secpp = bfd_com_section_ptr;
   1509  1.1  christos 	  break;
   1510  1.1  christos 	case COFF_SYMBOL_UNDEFINED:
   1511  1.1  christos 	  *secpp = bfd_und_section_ptr;
   1512  1.1  christos 	  break;
   1513  1.1  christos 	}
   1514  1.1  christos 
   1515  1.1  christos       /* Extract the flag indicating if this symbol is used by a
   1516  1.1  christos          relocation.  */
   1517  1.1  christos       if ((flaginfo->info->strip != strip_none
   1518  1.6  christos 	   || flaginfo->info->discard != discard_none)
   1519  1.1  christos 	  && bfd_link_relocatable (flaginfo->info))
   1520  1.1  christos 	dont_skip_symbol = *indexp;
   1521  1.1  christos       else
   1522  1.1  christos 	dont_skip_symbol = FALSE;
   1523  1.1  christos 
   1524  1.1  christos       *indexp = -1;
   1525  1.1  christos 
   1526  1.1  christos       skip = FALSE;
   1527  1.1  christos       global = FALSE;
   1528  1.1  christos       add = 1 + isym.n_numaux;
   1529  1.1  christos 
   1530  1.1  christos       /* If we are stripping all symbols, we want to skip this one.  */
   1531  1.1  christos       if (flaginfo->info->strip == strip_all && ! dont_skip_symbol)
   1532  1.1  christos 	skip = TRUE;
   1533  1.1  christos 
   1534  1.1  christos       if (! skip)
   1535  1.1  christos 	{
   1536  1.1  christos 	  switch (classification)
   1537  1.1  christos 	    {
   1538  1.1  christos 	    default:
   1539  1.1  christos 	      abort ();
   1540  1.1  christos 	    case COFF_SYMBOL_GLOBAL:
   1541  1.1  christos 	    case COFF_SYMBOL_COMMON:
   1542  1.1  christos 	    case COFF_SYMBOL_PE_SECTION:
   1543  1.1  christos 	      /* This is a global symbol.  Global symbols come at the
   1544  1.1  christos 		 end of the symbol table, so skip them for now.
   1545  1.1  christos 		 Locally defined function symbols, however, are an
   1546  1.1  christos 		 exception, and are not moved to the end.  */
   1547  1.1  christos 	      global = TRUE;
   1548  1.1  christos 	      if (! ISFCN (isym.n_type))
   1549  1.1  christos 		skip = TRUE;
   1550  1.1  christos 	      break;
   1551  1.1  christos 
   1552  1.1  christos 	    case COFF_SYMBOL_UNDEFINED:
   1553  1.1  christos 	      /* Undefined symbols are left for the end.  */
   1554  1.1  christos 	      global = TRUE;
   1555  1.1  christos 	      skip = TRUE;
   1556  1.1  christos 	      break;
   1557  1.1  christos 
   1558  1.1  christos 	    case COFF_SYMBOL_LOCAL:
   1559  1.1  christos 	      /* This is a local symbol.  Skip it if we are discarding
   1560  1.1  christos                  local symbols.  */
   1561  1.1  christos 	      if (flaginfo->info->discard == discard_all && ! dont_skip_symbol)
   1562  1.1  christos 		skip = TRUE;
   1563  1.1  christos 	      break;
   1564  1.1  christos 	    }
   1565  1.1  christos 	}
   1566  1.1  christos 
   1567  1.1  christos #ifndef COFF_WITH_PE
   1568  1.1  christos       /* Skip section symbols for sections which are not going to be
   1569  1.1  christos 	 emitted.  */
   1570  1.1  christos       if (!skip
   1571  1.1  christos 	  && !dont_skip_symbol
   1572  1.1  christos 	  && isym.n_sclass == C_STAT
   1573  1.1  christos 	  && isym.n_type == T_NULL
   1574  1.1  christos 	  && isym.n_numaux > 0
   1575  1.1  christos 	  && ((*secpp)->output_section == bfd_abs_section_ptr
   1576  1.1  christos 	      || bfd_section_removed_from_list (output_bfd,
   1577  1.1  christos 						(*secpp)->output_section)))
   1578  1.1  christos 	skip = TRUE;
   1579  1.1  christos #endif
   1580  1.1  christos 
   1581  1.1  christos       /* If we stripping debugging symbols, and this is a debugging
   1582  1.1  christos          symbol, then skip it.  FIXME: gas sets the section to N_ABS
   1583  1.1  christos          for some types of debugging symbols; I don't know if this is
   1584  1.1  christos          a bug or not.  In any case, we handle it here.  */
   1585  1.1  christos       if (! skip
   1586  1.1  christos 	  && flaginfo->info->strip == strip_debugger
   1587  1.1  christos 	  && ! dont_skip_symbol
   1588  1.1  christos 	  && (isym.n_scnum == N_DEBUG
   1589  1.1  christos 	      || (isym.n_scnum == N_ABS
   1590  1.1  christos 		  && (isym.n_sclass == C_AUTO
   1591  1.1  christos 		      || isym.n_sclass == C_REG
   1592  1.1  christos 		      || isym.n_sclass == C_MOS
   1593  1.1  christos 		      || isym.n_sclass == C_MOE
   1594  1.1  christos 		      || isym.n_sclass == C_MOU
   1595  1.1  christos 		      || isym.n_sclass == C_ARG
   1596  1.1  christos 		      || isym.n_sclass == C_REGPARM
   1597  1.1  christos 		      || isym.n_sclass == C_FIELD
   1598  1.1  christos 		      || isym.n_sclass == C_EOS))))
   1599  1.1  christos 	skip = TRUE;
   1600  1.1  christos 
   1601  1.1  christos       /* If some symbols are stripped based on the name, work out the
   1602  1.1  christos 	 name and decide whether to skip this symbol.  */
   1603  1.1  christos       if (! skip
   1604  1.1  christos 	  && (flaginfo->info->strip == strip_some
   1605  1.1  christos 	      || flaginfo->info->discard == discard_l))
   1606  1.1  christos 	{
   1607  1.1  christos 	  const char *name;
   1608  1.1  christos 	  char buf[SYMNMLEN + 1];
   1609  1.1  christos 
   1610  1.1  christos 	  name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
   1611  1.1  christos 	  if (name == NULL)
   1612  1.1  christos 	    return FALSE;
   1613  1.1  christos 
   1614  1.1  christos 	  if (! dont_skip_symbol
   1615  1.1  christos 	      && ((flaginfo->info->strip == strip_some
   1616  1.1  christos 		   && (bfd_hash_lookup (flaginfo->info->keep_hash, name, FALSE,
   1617  1.1  christos 				    FALSE) == NULL))
   1618  1.1  christos 		   || (! global
   1619  1.1  christos 		       && flaginfo->info->discard == discard_l
   1620  1.1  christos 		       && bfd_is_local_label_name (input_bfd, name))))
   1621  1.1  christos 	    skip = TRUE;
   1622  1.1  christos 	}
   1623  1.1  christos 
   1624  1.1  christos       /* If this is an enum, struct, or union tag, see if we have
   1625  1.1  christos          already output an identical type.  */
   1626  1.6  christos       if (! skip
   1627  1.1  christos 	  && !flaginfo->info->traditional_format
   1628  1.1  christos 	  && (isym.n_sclass == C_ENTAG
   1629  1.1  christos 	      || isym.n_sclass == C_STRTAG
   1630  1.1  christos 	      || isym.n_sclass == C_UNTAG)
   1631  1.1  christos 	  && isym.n_numaux == 1)
   1632  1.1  christos 	{
   1633  1.1  christos 	  const char *name;
   1634  1.1  christos 	  char buf[SYMNMLEN + 1];
   1635  1.1  christos 	  struct coff_debug_merge_hash_entry *mh;
   1636  1.1  christos 	  struct coff_debug_merge_type *mt;
   1637  1.1  christos 	  union internal_auxent aux;
   1638  1.1  christos 	  struct coff_debug_merge_element **epp;
   1639  1.1  christos 	  bfd_byte *esl, *eslend;
   1640  1.1  christos 	  struct internal_syment *islp;
   1641  1.1  christos 	  bfd_size_type amt;
   1642  1.1  christos 
   1643  1.1  christos 	  name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
   1644  1.1  christos 	  if (name == NULL)
   1645  1.1  christos 	    return FALSE;
   1646  1.1  christos 
   1647  1.1  christos 	  /* Ignore fake names invented by compiler; treat them all as
   1648  1.1  christos              the same name.  */
   1649  1.1  christos 	  if (*name == '~' || *name == '.' || *name == '$'
   1650  1.1  christos 	      || (*name == bfd_get_symbol_leading_char (input_bfd)
   1651  1.1  christos 		  && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
   1652  1.1  christos 	    name = "";
   1653  1.1  christos 
   1654  1.1  christos 	  mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name,
   1655  1.1  christos 					     TRUE, TRUE);
   1656  1.1  christos 	  if (mh == NULL)
   1657  1.1  christos 	    return FALSE;
   1658  1.1  christos 
   1659  1.1  christos 	  /* Allocate memory to hold type information.  If this turns
   1660  1.1  christos              out to be a duplicate, we pass this address to
   1661  1.1  christos              bfd_release.  */
   1662  1.1  christos 	  amt = sizeof (struct coff_debug_merge_type);
   1663  1.1  christos 	  mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
   1664  1.1  christos 	  if (mt == NULL)
   1665  1.1  christos 	    return FALSE;
   1666  1.1  christos 	  mt->type_class = isym.n_sclass;
   1667  1.1  christos 
   1668  1.1  christos 	  /* Pick up the aux entry, which points to the end of the tag
   1669  1.1  christos              entries.  */
   1670  1.1  christos 	  bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
   1671  1.1  christos 				isym.n_type, isym.n_sclass, 0, isym.n_numaux,
   1672  1.1  christos 				&aux);
   1673  1.1  christos 
   1674  1.1  christos 	  /* Gather the elements.  */
   1675  1.1  christos 	  epp = &mt->elements;
   1676  1.1  christos 	  mt->elements = NULL;
   1677  1.1  christos 	  islp = isymp + 2;
   1678  1.1  christos 	  esl = esym + 2 * isymesz;
   1679  1.1  christos 	  eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
   1680  1.1  christos 		    + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
   1681  1.1  christos 	  while (esl < eslend)
   1682  1.1  christos 	    {
   1683  1.1  christos 	      const char *elename;
   1684  1.1  christos 	      char elebuf[SYMNMLEN + 1];
   1685  1.1  christos 	      char *name_copy;
   1686  1.1  christos 
   1687  1.1  christos 	      bfd_coff_swap_sym_in (input_bfd, esl, islp);
   1688  1.1  christos 
   1689  1.1  christos 	      amt = sizeof (struct coff_debug_merge_element);
   1690  1.1  christos 	      *epp = (struct coff_debug_merge_element *)
   1691  1.1  christos                   bfd_alloc (input_bfd, amt);
   1692  1.1  christos 	      if (*epp == NULL)
   1693  1.1  christos 		return FALSE;
   1694  1.1  christos 
   1695  1.1  christos 	      elename = _bfd_coff_internal_syment_name (input_bfd, islp,
   1696  1.1  christos 							elebuf);
   1697  1.1  christos 	      if (elename == NULL)
   1698  1.1  christos 		return FALSE;
   1699  1.1  christos 
   1700  1.1  christos 	      amt = strlen (elename) + 1;
   1701  1.1  christos 	      name_copy = (char *) bfd_alloc (input_bfd, amt);
   1702  1.1  christos 	      if (name_copy == NULL)
   1703  1.1  christos 		return FALSE;
   1704  1.1  christos 	      strcpy (name_copy, elename);
   1705  1.1  christos 
   1706  1.1  christos 	      (*epp)->name = name_copy;
   1707  1.1  christos 	      (*epp)->type = islp->n_type;
   1708  1.1  christos 	      (*epp)->tagndx = 0;
   1709  1.1  christos 	      if (islp->n_numaux >= 1
   1710  1.1  christos 		  && islp->n_type != T_NULL
   1711  1.1  christos 		  && islp->n_sclass != C_EOS)
   1712  1.1  christos 		{
   1713  1.1  christos 		  union internal_auxent eleaux;
   1714  1.1  christos 		  long indx;
   1715  1.1  christos 
   1716  1.1  christos 		  bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
   1717  1.1  christos 					islp->n_type, islp->n_sclass, 0,
   1718  1.1  christos 					islp->n_numaux, &eleaux);
   1719  1.1  christos 		  indx = eleaux.x_sym.x_tagndx.l;
   1720  1.1  christos 
   1721  1.1  christos 		  /* FIXME: If this tagndx entry refers to a symbol
   1722  1.1  christos 		     defined later in this file, we just ignore it.
   1723  1.1  christos 		     Handling this correctly would be tedious, and may
   1724  1.1  christos 		     not be required.  */
   1725  1.1  christos 		  if (indx > 0
   1726  1.1  christos 		      && (indx
   1727  1.1  christos 			  < ((esym -
   1728  1.1  christos 			      (bfd_byte *) obj_coff_external_syms (input_bfd))
   1729  1.1  christos 			     / (long) isymesz)))
   1730  1.1  christos 		    {
   1731  1.1  christos 		      (*epp)->tagndx = flaginfo->sym_indices[indx];
   1732  1.1  christos 		      if ((*epp)->tagndx < 0)
   1733  1.1  christos 			(*epp)->tagndx = 0;
   1734  1.1  christos 		    }
   1735  1.1  christos 		}
   1736  1.1  christos 	      epp = &(*epp)->next;
   1737  1.1  christos 	      *epp = NULL;
   1738  1.1  christos 
   1739  1.1  christos 	      esl += (islp->n_numaux + 1) * isymesz;
   1740  1.1  christos 	      islp += islp->n_numaux + 1;
   1741  1.1  christos 	    }
   1742  1.1  christos 
   1743  1.1  christos 	  /* See if we already have a definition which matches this
   1744  1.1  christos              type.  We always output the type if it has no elements,
   1745  1.1  christos              for simplicity.  */
   1746  1.1  christos 	  if (mt->elements == NULL)
   1747  1.1  christos 	    bfd_release (input_bfd, mt);
   1748  1.1  christos 	  else
   1749  1.1  christos 	    {
   1750  1.1  christos 	      struct coff_debug_merge_type *mtl;
   1751  1.1  christos 
   1752  1.1  christos 	      for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
   1753  1.1  christos 		{
   1754  1.1  christos 		  struct coff_debug_merge_element *me, *mel;
   1755  1.1  christos 
   1756  1.1  christos 		  if (mtl->type_class != mt->type_class)
   1757  1.1  christos 		    continue;
   1758  1.1  christos 
   1759  1.1  christos 		  for (me = mt->elements, mel = mtl->elements;
   1760  1.1  christos 		       me != NULL && mel != NULL;
   1761  1.1  christos 		       me = me->next, mel = mel->next)
   1762  1.1  christos 		    {
   1763  1.1  christos 		      if (strcmp (me->name, mel->name) != 0
   1764  1.1  christos 			  || me->type != mel->type
   1765  1.1  christos 			  || me->tagndx != mel->tagndx)
   1766  1.1  christos 			break;
   1767  1.1  christos 		    }
   1768  1.1  christos 
   1769  1.1  christos 		  if (me == NULL && mel == NULL)
   1770  1.1  christos 		    break;
   1771  1.1  christos 		}
   1772  1.1  christos 
   1773  1.1  christos 	      if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
   1774  1.1  christos 		{
   1775  1.1  christos 		  /* This is the first definition of this type.  */
   1776  1.1  christos 		  mt->indx = output_index;
   1777  1.1  christos 		  mt->next = mh->types;
   1778  1.1  christos 		  mh->types = mt;
   1779  1.1  christos 		}
   1780  1.1  christos 	      else
   1781  1.1  christos 		{
   1782  1.1  christos 		  /* This is a redefinition which can be merged.  */
   1783  1.1  christos 		  bfd_release (input_bfd, mt);
   1784  1.1  christos 		  *indexp = mtl->indx;
   1785  1.1  christos 		  add = (eslend - esym) / isymesz;
   1786  1.1  christos 		  skip = TRUE;
   1787  1.1  christos 		}
   1788  1.1  christos 	    }
   1789  1.1  christos 	}
   1790  1.1  christos 
   1791  1.1  christos       /* We now know whether we are to skip this symbol or not.  */
   1792  1.1  christos       if (! skip)
   1793  1.1  christos 	{
   1794  1.1  christos 	  /* Adjust the symbol in order to output it.  */
   1795  1.1  christos 
   1796  1.1  christos 	  if (isym._n._n_n._n_zeroes == 0
   1797  1.1  christos 	      && isym._n._n_n._n_offset != 0)
   1798  1.1  christos 	    {
   1799  1.1  christos 	      const char *name;
   1800  1.1  christos 	      bfd_size_type indx;
   1801  1.1  christos 
   1802  1.1  christos 	      /* This symbol has a long name.  Enter it in the string
   1803  1.1  christos 		 table we are building.  Note that we do not check
   1804  1.1  christos 		 bfd_coff_symname_in_debug.  That is only true for
   1805  1.1  christos 		 XCOFF, and XCOFF requires different linking code
   1806  1.1  christos 		 anyhow.  */
   1807  1.1  christos 	      name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
   1808  1.1  christos 	      if (name == NULL)
   1809  1.1  christos 		return FALSE;
   1810  1.1  christos 	      indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy);
   1811  1.1  christos 	      if (indx == (bfd_size_type) -1)
   1812  1.1  christos 		return FALSE;
   1813  1.1  christos 	      isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
   1814  1.1  christos 	    }
   1815  1.1  christos 
   1816  1.1  christos 	  switch (isym.n_sclass)
   1817  1.1  christos 	    {
   1818  1.1  christos 	    case C_AUTO:
   1819  1.1  christos 	    case C_MOS:
   1820  1.1  christos 	    case C_EOS:
   1821  1.1  christos 	    case C_MOE:
   1822  1.1  christos 	    case C_MOU:
   1823  1.1  christos 	    case C_UNTAG:
   1824  1.1  christos 	    case C_STRTAG:
   1825  1.1  christos 	    case C_ENTAG:
   1826  1.1  christos 	    case C_TPDEF:
   1827  1.1  christos 	    case C_ARG:
   1828  1.1  christos 	    case C_USTATIC:
   1829  1.1  christos 	    case C_REG:
   1830  1.1  christos 	    case C_REGPARM:
   1831  1.1  christos 	    case C_FIELD:
   1832  1.1  christos 	      /* The symbol value should not be modified.  */
   1833  1.1  christos 	      break;
   1834  1.1  christos 
   1835  1.1  christos 	    case C_FCN:
   1836  1.1  christos 	      if (obj_pe (input_bfd)
   1837  1.1  christos 		  && strcmp (isym.n_name, ".bf") != 0
   1838  1.1  christos 		  && isym.n_scnum > 0)
   1839  1.1  christos 		{
   1840  1.1  christos 		  /* For PE, .lf and .ef get their value left alone,
   1841  1.1  christos 		     while .bf gets relocated.  However, they all have
   1842  1.1  christos 		     "real" section numbers, and need to be moved into
   1843  1.1  christos 		     the new section.  */
   1844  1.1  christos 		  isym.n_scnum = (*secpp)->output_section->target_index;
   1845  1.1  christos 		  break;
   1846  1.1  christos 		}
   1847  1.1  christos 	      /* Fall through.  */
   1848  1.1  christos 	    default:
   1849  1.1  christos 	    case C_LABEL:  /* Not completely sure about these 2 */
   1850  1.1  christos 	    case C_EXTDEF:
   1851  1.1  christos 	    case C_BLOCK:
   1852  1.1  christos 	    case C_EFCN:
   1853  1.1  christos 	    case C_NULL:
   1854  1.1  christos 	    case C_EXT:
   1855  1.1  christos 	    case C_STAT:
   1856  1.1  christos 	    case C_SECTION:
   1857  1.1  christos 	    case C_NT_WEAK:
   1858  1.1  christos 	      /* Compute new symbol location.  */
   1859  1.1  christos 	    if (isym.n_scnum > 0)
   1860  1.1  christos 	      {
   1861  1.1  christos 		isym.n_scnum = (*secpp)->output_section->target_index;
   1862  1.1  christos 		isym.n_value += (*secpp)->output_offset;
   1863  1.1  christos 		if (! obj_pe (input_bfd))
   1864  1.1  christos 		  isym.n_value -= (*secpp)->vma;
   1865  1.1  christos 		if (! obj_pe (flaginfo->output_bfd))
   1866  1.1  christos 		  isym.n_value += (*secpp)->output_section->vma;
   1867  1.1  christos 	      }
   1868  1.1  christos 	    break;
   1869  1.1  christos 
   1870  1.1  christos 	    case C_FILE:
   1871  1.1  christos 	      /* The value of a C_FILE symbol is the symbol index of
   1872  1.1  christos 		 the next C_FILE symbol.  The value of the last C_FILE
   1873  1.1  christos 		 symbol is the symbol index to the first external
   1874  1.1  christos 		 symbol (actually, coff_renumber_symbols does not get
   1875  1.1  christos 		 this right--it just sets the value of the last C_FILE
   1876  1.1  christos 		 symbol to zero--and nobody has ever complained about
   1877  1.1  christos 		 it).  We try to get this right, below, just before we
   1878  1.1  christos 		 write the symbols out, but in the general case we may
   1879  1.1  christos 		 have to write the symbol out twice.  */
   1880  1.1  christos 	      if (flaginfo->last_file_index != -1
   1881  1.1  christos 		  && flaginfo->last_file.n_value != (bfd_vma) output_index)
   1882  1.1  christos 		{
   1883  1.1  christos 		  /* We must correct the value of the last C_FILE
   1884  1.1  christos                      entry.  */
   1885  1.1  christos 		  flaginfo->last_file.n_value = output_index;
   1886  1.1  christos 		  if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
   1887  1.1  christos 		    {
   1888  1.1  christos 		      /* The last C_FILE symbol is in this input file.  */
   1889  1.1  christos 		      bfd_coff_swap_sym_out (output_bfd,
   1890  1.1  christos 					     &flaginfo->last_file,
   1891  1.1  christos 					     (flaginfo->outsyms
   1892  1.1  christos 					      + ((flaginfo->last_file_index
   1893  1.1  christos 						  - syment_base)
   1894  1.1  christos 						 * osymesz)));
   1895  1.1  christos 		    }
   1896  1.1  christos 		  else
   1897  1.1  christos 		    {
   1898  1.1  christos 		      file_ptr pos;
   1899  1.1  christos 
   1900  1.1  christos 		      /* We have already written out the last C_FILE
   1901  1.1  christos 			 symbol.  We need to write it out again.  We
   1902  1.1  christos 			 borrow *outsym temporarily.  */
   1903  1.1  christos 		      bfd_coff_swap_sym_out (output_bfd,
   1904  1.1  christos 					     &flaginfo->last_file, outsym);
   1905  1.1  christos 		      pos = obj_sym_filepos (output_bfd);
   1906  1.1  christos 		      pos += flaginfo->last_file_index * osymesz;
   1907  1.1  christos 		      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
   1908  1.1  christos 			  || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
   1909  1.1  christos 			return FALSE;
   1910  1.1  christos 		    }
   1911  1.1  christos 		}
   1912  1.1  christos 
   1913  1.1  christos 	      flaginfo->last_file_index = output_index;
   1914  1.1  christos 	      flaginfo->last_file = isym;
   1915  1.1  christos 	      break;
   1916  1.1  christos 	    }
   1917  1.1  christos 
   1918  1.1  christos 	  /* If doing task linking, convert normal global function symbols to
   1919  1.1  christos 	     static functions.  */
   1920  1.1  christos 	  if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
   1921  1.1  christos 	    isym.n_sclass = C_STAT;
   1922  1.1  christos 
   1923  1.1  christos 	  /* Output the symbol.  */
   1924  1.1  christos 	  bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
   1925  1.1  christos 
   1926  1.1  christos 	  *indexp = output_index;
   1927  1.1  christos 
   1928  1.1  christos 	  if (global)
   1929  1.1  christos 	    {
   1930  1.1  christos 	      long indx;
   1931  1.1  christos 	      struct coff_link_hash_entry *h;
   1932  1.1  christos 
   1933  1.1  christos 	      indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
   1934  1.1  christos 		      / isymesz);
   1935  1.1  christos 	      h = obj_coff_sym_hashes (input_bfd)[indx];
   1936  1.1  christos 	      if (h == NULL)
   1937  1.1  christos 		{
   1938  1.1  christos 		  /* This can happen if there were errors earlier in
   1939  1.1  christos                      the link.  */
   1940  1.1  christos 		  bfd_set_error (bfd_error_bad_value);
   1941  1.1  christos 		  return FALSE;
   1942  1.1  christos 		}
   1943  1.1  christos 	      h->indx = output_index;
   1944  1.1  christos 	    }
   1945  1.1  christos 
   1946  1.1  christos 	  output_index += add;
   1947  1.1  christos 	  outsym += add * osymesz;
   1948  1.1  christos 	}
   1949  1.1  christos 
   1950  1.1  christos       esym += add * isymesz;
   1951  1.1  christos       isymp += add;
   1952  1.1  christos       ++secpp;
   1953  1.1  christos       ++indexp;
   1954  1.1  christos       for (--add; add > 0; --add)
   1955  1.1  christos 	{
   1956  1.1  christos 	  *secpp++ = NULL;
   1957  1.1  christos 	  *indexp++ = -1;
   1958  1.1  christos 	}
   1959  1.1  christos     }
   1960  1.1  christos 
   1961  1.1  christos   /* Fix up the aux entries.  This must be done in a separate pass,
   1962  1.1  christos      because we don't know the correct symbol indices until we have
   1963  1.1  christos      already decided which symbols we are going to keep.  */
   1964  1.1  christos   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
   1965  1.1  christos   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
   1966  1.1  christos   isymp = flaginfo->internal_syms;
   1967  1.1  christos   indexp = flaginfo->sym_indices;
   1968  1.1  christos   sym_hash = obj_coff_sym_hashes (input_bfd);
   1969  1.1  christos   outsym = flaginfo->outsyms;
   1970  1.1  christos 
   1971  1.1  christos   while (esym < esym_end)
   1972  1.1  christos     {
   1973  1.1  christos       int add;
   1974  1.1  christos 
   1975  1.1  christos       add = 1 + isymp->n_numaux;
   1976  1.1  christos 
   1977  1.1  christos       if ((*indexp < 0
   1978  1.1  christos 	   || (bfd_size_type) *indexp < syment_base)
   1979  1.1  christos 	  && (*sym_hash == NULL
   1980  1.1  christos 	      || (*sym_hash)->auxbfd != input_bfd))
   1981  1.1  christos 	esym += add * isymesz;
   1982  1.1  christos       else
   1983  1.1  christos 	{
   1984  1.1  christos 	  struct coff_link_hash_entry *h;
   1985  1.1  christos 	  int i;
   1986  1.1  christos 
   1987  1.1  christos 	  h = NULL;
   1988  1.1  christos 	  if (*indexp < 0)
   1989  1.1  christos 	    {
   1990  1.1  christos 	      h = *sym_hash;
   1991  1.1  christos 
   1992  1.1  christos 	      /* The m68k-motorola-sysv assembler will sometimes
   1993  1.1  christos                  generate two symbols with the same name, but only one
   1994  1.1  christos                  will have aux entries.  */
   1995  1.1  christos 	      BFD_ASSERT (isymp->n_numaux == 0
   1996  1.1  christos 			  || h->numaux == 0
   1997  1.1  christos 			  || h->numaux == isymp->n_numaux);
   1998  1.1  christos 	    }
   1999  1.1  christos 
   2000  1.1  christos 	  esym += isymesz;
   2001  1.1  christos 
   2002  1.1  christos 	  if (h == NULL)
   2003  1.1  christos 	    outsym += osymesz;
   2004  1.1  christos 
   2005  1.1  christos 	  /* Handle the aux entries.  This handling is based on
   2006  1.1  christos 	     coff_pointerize_aux.  I don't know if it always correct.  */
   2007  1.1  christos 	  for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
   2008  1.1  christos 	    {
   2009  1.1  christos 	      union internal_auxent aux;
   2010  1.1  christos 	      union internal_auxent *auxp;
   2011  1.1  christos 
   2012  1.1  christos 	      if (h != NULL && h->aux != NULL && (h->numaux > i))
   2013  1.1  christos 		auxp = h->aux + i;
   2014  1.1  christos 	      else
   2015  1.1  christos 		{
   2016  1.1  christos 		  bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
   2017  1.1  christos 					isymp->n_sclass, i, isymp->n_numaux, &aux);
   2018  1.1  christos 		  auxp = &aux;
   2019  1.1  christos 		}
   2020  1.1  christos 
   2021  1.1  christos 	      if (isymp->n_sclass == C_FILE)
   2022  1.1  christos 		{
   2023  1.1  christos 		  /* If this is a long filename, we must put it in the
   2024  1.1  christos 		     string table.  */
   2025  1.1  christos 		  if (auxp->x_file.x_n.x_zeroes == 0
   2026  1.1  christos 		      && auxp->x_file.x_n.x_offset != 0)
   2027  1.1  christos 		    {
   2028  1.1  christos 		      const char *filename;
   2029  1.1  christos 		      bfd_size_type indx;
   2030  1.1  christos 
   2031  1.1  christos 		      BFD_ASSERT (auxp->x_file.x_n.x_offset
   2032  1.1  christos 				  >= STRING_SIZE_SIZE);
   2033  1.1  christos 		      if (strings == NULL)
   2034  1.1  christos 			{
   2035  1.1  christos 			  strings = _bfd_coff_read_string_table (input_bfd);
   2036  1.1  christos 			  if (strings == NULL)
   2037  1.1  christos 			    return FALSE;
   2038  1.3  christos 			}
   2039  1.3  christos 		      if ((bfd_size_type) auxp->x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd))
   2040  1.3  christos 			filename = _("<corrupt>");
   2041  1.3  christos 		      else
   2042  1.1  christos 			filename = strings + auxp->x_file.x_n.x_offset;
   2043  1.1  christos 		      indx = _bfd_stringtab_add (flaginfo->strtab, filename,
   2044  1.1  christos 						 hash, copy);
   2045  1.1  christos 		      if (indx == (bfd_size_type) -1)
   2046  1.1  christos 			return FALSE;
   2047  1.1  christos 		      auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
   2048  1.1  christos 		    }
   2049  1.1  christos 		}
   2050  1.1  christos 	      else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
   2051  1.1  christos 		       && isymp->n_sclass != C_NT_WEAK)
   2052  1.1  christos 		{
   2053  1.1  christos 		  unsigned long indx;
   2054  1.1  christos 
   2055  1.1  christos 		  if (ISFCN (isymp->n_type)
   2056  1.1  christos 		      || ISTAG (isymp->n_sclass)
   2057  1.1  christos 		      || isymp->n_sclass == C_BLOCK
   2058  1.1  christos 		      || isymp->n_sclass == C_FCN)
   2059  1.1  christos 		    {
   2060  1.1  christos 		      indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
   2061  1.1  christos 		      if (indx > 0
   2062  1.1  christos 			  && indx < obj_raw_syment_count (input_bfd))
   2063  1.1  christos 			{
   2064  1.1  christos 			  /* We look forward through the symbol for
   2065  1.1  christos                              the index of the next symbol we are going
   2066  1.1  christos                              to include.  I don't know if this is
   2067  1.1  christos                              entirely right.  */
   2068  1.1  christos 			  while ((flaginfo->sym_indices[indx] < 0
   2069  1.1  christos 				  || ((bfd_size_type) flaginfo->sym_indices[indx]
   2070  1.1  christos 				      < syment_base))
   2071  1.1  christos 				 && indx < obj_raw_syment_count (input_bfd))
   2072  1.1  christos 			    ++indx;
   2073  1.1  christos 			  if (indx >= obj_raw_syment_count (input_bfd))
   2074  1.1  christos 			    indx = output_index;
   2075  1.1  christos 			  else
   2076  1.1  christos 			    indx = flaginfo->sym_indices[indx];
   2077  1.1  christos 			  auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
   2078  1.1  christos 			}
   2079  1.1  christos 		    }
   2080  1.1  christos 
   2081  1.1  christos 		  indx = auxp->x_sym.x_tagndx.l;
   2082  1.1  christos 		  if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
   2083  1.1  christos 		    {
   2084  1.1  christos 		      long symindx;
   2085  1.1  christos 
   2086  1.1  christos 		      symindx = flaginfo->sym_indices[indx];
   2087  1.1  christos 		      if (symindx < 0)
   2088  1.1  christos 			auxp->x_sym.x_tagndx.l = 0;
   2089  1.1  christos 		      else
   2090  1.1  christos 			auxp->x_sym.x_tagndx.l = symindx;
   2091  1.1  christos 		    }
   2092  1.1  christos 
   2093  1.1  christos 		  /* The .bf symbols are supposed to be linked through
   2094  1.1  christos 		     the endndx field.  We need to carry this list
   2095  1.1  christos 		     across object files.  */
   2096  1.1  christos 		  if (i == 0
   2097  1.1  christos 		      && h == NULL
   2098  1.1  christos 		      && isymp->n_sclass == C_FCN
   2099  1.1  christos 		      && (isymp->_n._n_n._n_zeroes != 0
   2100  1.1  christos 			  || isymp->_n._n_n._n_offset == 0)
   2101  1.1  christos 		      && isymp->_n._n_name[0] == '.'
   2102  1.1  christos 		      && isymp->_n._n_name[1] == 'b'
   2103  1.1  christos 		      && isymp->_n._n_name[2] == 'f'
   2104  1.1  christos 		      && isymp->_n._n_name[3] == '\0')
   2105  1.1  christos 		    {
   2106  1.1  christos 		      if (flaginfo->last_bf_index != -1)
   2107  1.1  christos 			{
   2108  1.1  christos 			  flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
   2109  1.1  christos 			    *indexp;
   2110  1.1  christos 
   2111  1.1  christos 			  if ((bfd_size_type) flaginfo->last_bf_index
   2112  1.1  christos 			      >= syment_base)
   2113  1.1  christos 			    {
   2114  1.1  christos 			      void *auxout;
   2115  1.1  christos 
   2116  1.1  christos 			      /* The last .bf symbol is in this input
   2117  1.1  christos 				 file.  This will only happen if the
   2118  1.1  christos 				 assembler did not set up the .bf
   2119  1.1  christos 				 endndx symbols correctly.  */
   2120  1.1  christos 			      auxout = (flaginfo->outsyms
   2121  1.1  christos 					+ ((flaginfo->last_bf_index
   2122  1.1  christos 					    - syment_base)
   2123  1.1  christos 					   * osymesz));
   2124  1.1  christos 
   2125  1.1  christos 			      bfd_coff_swap_aux_out (output_bfd,
   2126  1.1  christos 						     &flaginfo->last_bf,
   2127  1.1  christos 						     isymp->n_type,
   2128  1.1  christos 						     isymp->n_sclass,
   2129  1.1  christos 						     0, isymp->n_numaux,
   2130  1.1  christos 						     auxout);
   2131  1.1  christos 			    }
   2132  1.1  christos 			  else
   2133  1.1  christos 			    {
   2134  1.1  christos 			      file_ptr pos;
   2135  1.1  christos 
   2136  1.1  christos 			      /* We have already written out the last
   2137  1.1  christos                                  .bf aux entry.  We need to write it
   2138  1.1  christos                                  out again.  We borrow *outsym
   2139  1.1  christos                                  temporarily.  FIXME: This case should
   2140  1.1  christos                                  be made faster.  */
   2141  1.1  christos 			      bfd_coff_swap_aux_out (output_bfd,
   2142  1.1  christos 						     &flaginfo->last_bf,
   2143  1.1  christos 						     isymp->n_type,
   2144  1.1  christos 						     isymp->n_sclass,
   2145  1.1  christos 						     0, isymp->n_numaux,
   2146  1.1  christos 						     outsym);
   2147  1.1  christos 			      pos = obj_sym_filepos (output_bfd);
   2148  1.1  christos 			      pos += flaginfo->last_bf_index * osymesz;
   2149  1.1  christos 			      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
   2150  1.1  christos 				  || (bfd_bwrite (outsym, osymesz, output_bfd)
   2151  1.1  christos 				      != osymesz))
   2152  1.1  christos 				return FALSE;
   2153  1.1  christos 			    }
   2154  1.1  christos 			}
   2155  1.1  christos 
   2156  1.1  christos 		      if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
   2157  1.1  christos 			flaginfo->last_bf_index = -1;
   2158  1.1  christos 		      else
   2159  1.1  christos 			{
   2160  1.1  christos 			  /* The endndx field of this aux entry must
   2161  1.1  christos                              be updated with the symbol number of the
   2162  1.1  christos                              next .bf symbol.  */
   2163  1.1  christos 			  flaginfo->last_bf = *auxp;
   2164  1.1  christos 			  flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
   2165  1.1  christos 						   / osymesz)
   2166  1.1  christos 						  + syment_base);
   2167  1.1  christos 			}
   2168  1.1  christos 		    }
   2169  1.1  christos 		}
   2170  1.1  christos 
   2171  1.1  christos 	      if (h == NULL)
   2172  1.1  christos 		{
   2173  1.1  christos 		  bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
   2174  1.1  christos 					 isymp->n_sclass, i, isymp->n_numaux,
   2175  1.1  christos 					 outsym);
   2176  1.1  christos 		  outsym += osymesz;
   2177  1.1  christos 		}
   2178  1.1  christos 
   2179  1.1  christos 	      esym += isymesz;
   2180  1.1  christos 	    }
   2181  1.1  christos 	}
   2182  1.1  christos 
   2183  1.1  christos       indexp += add;
   2184  1.1  christos       isymp += add;
   2185  1.1  christos       sym_hash += add;
   2186  1.1  christos     }
   2187  1.1  christos 
   2188  1.1  christos   /* Relocate the line numbers, unless we are stripping them.  */
   2189  1.1  christos   if (flaginfo->info->strip == strip_none
   2190  1.1  christos       || flaginfo->info->strip == strip_some)
   2191  1.1  christos     {
   2192  1.1  christos       for (o = input_bfd->sections; o != NULL; o = o->next)
   2193  1.1  christos 	{
   2194  1.1  christos 	  bfd_vma offset;
   2195  1.1  christos 	  bfd_byte *eline;
   2196  1.1  christos 	  bfd_byte *elineend;
   2197  1.1  christos 	  bfd_byte *oeline;
   2198  1.1  christos 	  bfd_boolean skipping;
   2199  1.1  christos 	  file_ptr pos;
   2200  1.1  christos 	  bfd_size_type amt;
   2201  1.1  christos 
   2202  1.1  christos 	  /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
   2203  1.1  christos 	     build_link_order in ldwrite.c will not have created a
   2204  1.1  christos 	     link order, which means that we will not have seen this
   2205  1.1  christos 	     input section in _bfd_coff_final_link, which means that
   2206  1.1  christos 	     we will not have allocated space for the line numbers of
   2207  1.1  christos 	     this section.  I don't think line numbers can be
   2208  1.1  christos 	     meaningful for a section which does not have
   2209  1.1  christos 	     SEC_HAS_CONTENTS set, but, if they do, this must be
   2210  1.1  christos 	     changed.  */
   2211  1.1  christos 	  if (o->lineno_count == 0
   2212  1.1  christos 	      || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
   2213  1.1  christos 	    continue;
   2214  1.1  christos 
   2215  1.1  christos 	  if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
   2216  1.1  christos 	      || bfd_bread (flaginfo->linenos, linesz * o->lineno_count,
   2217  1.1  christos 			   input_bfd) != linesz * o->lineno_count)
   2218  1.1  christos 	    return FALSE;
   2219  1.1  christos 
   2220  1.1  christos 	  offset = o->output_section->vma + o->output_offset - o->vma;
   2221  1.1  christos 	  eline = flaginfo->linenos;
   2222  1.1  christos 	  oeline = flaginfo->linenos;
   2223  1.1  christos 	  elineend = eline + linesz * o->lineno_count;
   2224  1.1  christos 	  skipping = FALSE;
   2225  1.1  christos 	  for (; eline < elineend; eline += linesz)
   2226  1.1  christos 	    {
   2227  1.1  christos 	      struct internal_lineno iline;
   2228  1.1  christos 
   2229  1.1  christos 	      bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
   2230  1.1  christos 
   2231  1.1  christos 	      if (iline.l_lnno != 0)
   2232  1.1  christos 		iline.l_addr.l_paddr += offset;
   2233  1.1  christos 	      else if (iline.l_addr.l_symndx >= 0
   2234  1.1  christos 		       && ((unsigned long) iline.l_addr.l_symndx
   2235  1.1  christos 			   < obj_raw_syment_count (input_bfd)))
   2236  1.1  christos 		{
   2237  1.1  christos 		  long indx;
   2238  1.1  christos 
   2239  1.1  christos 		  indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
   2240  1.1  christos 
   2241  1.1  christos 		  if (indx < 0)
   2242  1.1  christos 		    {
   2243  1.1  christos 		      /* These line numbers are attached to a symbol
   2244  1.1  christos 			 which we are stripping.  We must discard the
   2245  1.1  christos 			 line numbers because reading them back with
   2246  1.1  christos 			 no associated symbol (or associating them all
   2247  1.1  christos 			 with symbol #0) will fail.  We can't regain
   2248  1.1  christos 			 the space in the output file, but at least
   2249  1.1  christos 			 they're dense.  */
   2250  1.1  christos 		      skipping = TRUE;
   2251  1.1  christos 		    }
   2252  1.1  christos 		  else
   2253  1.1  christos 		    {
   2254  1.1  christos 		      struct internal_syment is;
   2255  1.1  christos 		      union internal_auxent ia;
   2256  1.1  christos 
   2257  1.1  christos 		      /* Fix up the lnnoptr field in the aux entry of
   2258  1.1  christos 			 the symbol.  It turns out that we can't do
   2259  1.1  christos 			 this when we modify the symbol aux entries,
   2260  1.1  christos 			 because gas sometimes screws up the lnnoptr
   2261  1.1  christos 			 field and makes it an offset from the start
   2262  1.1  christos 			 of the line numbers rather than an absolute
   2263  1.1  christos 			 file index.  */
   2264  1.1  christos 		      bfd_coff_swap_sym_in (output_bfd,
   2265  1.1  christos 					    (flaginfo->outsyms
   2266  1.1  christos 					     + ((indx - syment_base)
   2267  1.1  christos 						* osymesz)), &is);
   2268  1.1  christos 		      if ((ISFCN (is.n_type)
   2269  1.1  christos 			   || is.n_sclass == C_BLOCK)
   2270  1.1  christos 			  && is.n_numaux >= 1)
   2271  1.1  christos 			{
   2272  1.1  christos 			  void *auxptr;
   2273  1.1  christos 
   2274  1.1  christos 			  auxptr = (flaginfo->outsyms
   2275  1.1  christos 				    + ((indx - syment_base + 1)
   2276  1.1  christos 				       * osymesz));
   2277  1.1  christos 			  bfd_coff_swap_aux_in (output_bfd, auxptr,
   2278  1.1  christos 						is.n_type, is.n_sclass,
   2279  1.1  christos 						0, is.n_numaux, &ia);
   2280  1.1  christos 			  ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
   2281  1.1  christos 			    (o->output_section->line_filepos
   2282  1.1  christos 			     + o->output_section->lineno_count * linesz
   2283  1.1  christos 			     + eline - flaginfo->linenos);
   2284  1.1  christos 			  bfd_coff_swap_aux_out (output_bfd, &ia,
   2285  1.1  christos 						 is.n_type, is.n_sclass, 0,
   2286  1.1  christos 						 is.n_numaux, auxptr);
   2287  1.1  christos 			}
   2288  1.1  christos 
   2289  1.1  christos 		      skipping = FALSE;
   2290  1.1  christos 		    }
   2291  1.1  christos 
   2292  1.1  christos 		  iline.l_addr.l_symndx = indx;
   2293  1.1  christos 		}
   2294  1.1  christos 
   2295  1.1  christos 	      if (!skipping)
   2296  1.1  christos 	        {
   2297  1.1  christos 		  bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
   2298  1.1  christos 		  oeline += linesz;
   2299  1.1  christos 		}
   2300  1.1  christos 	    }
   2301  1.1  christos 
   2302  1.1  christos 	  pos = o->output_section->line_filepos;
   2303  1.1  christos 	  pos += o->output_section->lineno_count * linesz;
   2304  1.1  christos 	  amt = oeline - flaginfo->linenos;
   2305  1.1  christos 	  if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
   2306  1.1  christos 	      || bfd_bwrite (flaginfo->linenos, amt, output_bfd) != amt)
   2307  1.1  christos 	    return FALSE;
   2308  1.1  christos 
   2309  1.1  christos 	  o->output_section->lineno_count += amt / linesz;
   2310  1.1  christos 	}
   2311  1.1  christos     }
   2312  1.1  christos 
   2313  1.1  christos   /* If we swapped out a C_FILE symbol, guess that the next C_FILE
   2314  1.1  christos      symbol will be the first symbol in the next input file.  In the
   2315  1.1  christos      normal case, this will save us from writing out the C_FILE symbol
   2316  1.1  christos      again.  */
   2317  1.1  christos   if (flaginfo->last_file_index != -1
   2318  1.1  christos       && (bfd_size_type) flaginfo->last_file_index >= syment_base)
   2319  1.1  christos     {
   2320  1.1  christos       flaginfo->last_file.n_value = output_index;
   2321  1.1  christos       bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
   2322  1.1  christos 			     (flaginfo->outsyms
   2323  1.1  christos 			      + ((flaginfo->last_file_index - syment_base)
   2324  1.1  christos 				 * osymesz)));
   2325  1.1  christos     }
   2326  1.1  christos 
   2327  1.1  christos   /* Write the modified symbols to the output file.  */
   2328  1.1  christos   if (outsym > flaginfo->outsyms)
   2329  1.1  christos     {
   2330  1.1  christos       file_ptr pos;
   2331  1.1  christos       bfd_size_type amt;
   2332  1.1  christos 
   2333  1.1  christos       pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
   2334  1.1  christos       amt = outsym - flaginfo->outsyms;
   2335  1.1  christos       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
   2336  1.1  christos 	  || bfd_bwrite (flaginfo->outsyms, amt, output_bfd) != amt)
   2337  1.1  christos 	return FALSE;
   2338  1.1  christos 
   2339  1.1  christos       BFD_ASSERT ((obj_raw_syment_count (output_bfd)
   2340  1.1  christos 		   + (outsym - flaginfo->outsyms) / osymesz)
   2341  1.1  christos 		  == output_index);
   2342  1.1  christos 
   2343  1.1  christos       obj_raw_syment_count (output_bfd) = output_index;
   2344  1.1  christos     }
   2345  1.1  christos 
   2346  1.1  christos   /* Relocate the contents of each section.  */
   2347  1.1  christos   adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
   2348  1.1  christos   for (o = input_bfd->sections; o != NULL; o = o->next)
   2349  1.1  christos     {
   2350  1.1  christos       bfd_byte *contents;
   2351  1.1  christos       struct coff_section_tdata *secdata;
   2352  1.1  christos 
   2353  1.1  christos       if (! o->linker_mark)
   2354  1.1  christos 	/* This section was omitted from the link.  */
   2355  1.1  christos 	continue;
   2356  1.1  christos 
   2357  1.1  christos       if ((o->flags & SEC_LINKER_CREATED) != 0)
   2358  1.1  christos 	continue;
   2359  1.1  christos 
   2360  1.1  christos       if ((o->flags & SEC_HAS_CONTENTS) == 0
   2361  1.1  christos 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
   2362  1.1  christos 	{
   2363  1.1  christos 	  if ((o->flags & SEC_RELOC) != 0
   2364  1.1  christos 	      && o->reloc_count != 0)
   2365  1.1  christos 	    {
   2366  1.1  christos 	      (*_bfd_error_handler)
   2367  1.1  christos 		(_("%B: relocs in section `%A', but it has no contents"),
   2368  1.1  christos 		 input_bfd, o);
   2369  1.1  christos 	      bfd_set_error (bfd_error_no_contents);
   2370  1.1  christos 	      return FALSE;
   2371  1.1  christos 	    }
   2372  1.1  christos 
   2373  1.1  christos 	  continue;
   2374  1.1  christos 	}
   2375  1.1  christos 
   2376  1.1  christos       secdata = coff_section_data (input_bfd, o);
   2377  1.1  christos       if (secdata != NULL && secdata->contents != NULL)
   2378  1.1  christos 	contents = secdata->contents;
   2379  1.1  christos       else
   2380  1.1  christos 	{
   2381  1.1  christos 	  contents = flaginfo->contents;
   2382  1.1  christos 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
   2383  1.1  christos 	    return FALSE;
   2384  1.1  christos 	}
   2385  1.1  christos 
   2386  1.1  christos       if ((o->flags & SEC_RELOC) != 0)
   2387  1.1  christos 	{
   2388  1.1  christos 	  int target_index;
   2389  1.1  christos 	  struct internal_reloc *internal_relocs;
   2390  1.1  christos 	  struct internal_reloc *irel;
   2391  1.1  christos 
   2392  1.1  christos 	  /* Read in the relocs.  */
   2393  1.1  christos 	  target_index = o->output_section->target_index;
   2394  1.1  christos 	  internal_relocs = (_bfd_coff_read_internal_relocs
   2395  1.6  christos 			     (input_bfd, o, FALSE, flaginfo->external_relocs,
   2396  1.6  christos 			      bfd_link_relocatable (flaginfo->info),
   2397  1.1  christos 			      (bfd_link_relocatable (flaginfo->info)
   2398  1.1  christos 			       ? (flaginfo->section_info[target_index].relocs
   2399  1.1  christos 				  + o->output_section->reloc_count)
   2400  1.1  christos 			       : flaginfo->internal_relocs)));
   2401  1.1  christos 	  if (internal_relocs == NULL
   2402  1.1  christos 	      && o->reloc_count > 0)
   2403  1.1  christos 	    return FALSE;
   2404  1.1  christos 
   2405  1.1  christos 	  /* Run through the relocs looking for relocs against symbols
   2406  1.1  christos 	     coming from discarded sections and complain about them.  */
   2407  1.1  christos 	  irel = internal_relocs;
   2408  1.1  christos 	  for (; irel < &internal_relocs[o->reloc_count]; irel++)
   2409  1.1  christos 	    {
   2410  1.1  christos 	      struct coff_link_hash_entry *h;
   2411  1.1  christos 	      asection *ps = NULL;
   2412  1.1  christos 	      long symndx = irel->r_symndx;
   2413  1.1  christos 	      if (symndx < 0)
   2414  1.1  christos 		continue;
   2415  1.1  christos 	      h = obj_coff_sym_hashes (input_bfd)[symndx];
   2416  1.1  christos 	      if (h == NULL)
   2417  1.1  christos 		continue;
   2418  1.1  christos 	      while (h->root.type == bfd_link_hash_indirect
   2419  1.1  christos 		     || h->root.type == bfd_link_hash_warning)
   2420  1.1  christos 		h = (struct coff_link_hash_entry *) h->root.u.i.link;
   2421  1.1  christos 	      if (h->root.type == bfd_link_hash_defined
   2422  1.1  christos 		  || h->root.type == bfd_link_hash_defweak)
   2423  1.1  christos 		ps = h->root.u.def.section;
   2424  1.1  christos 	      if (ps == NULL)
   2425  1.1  christos 		continue;
   2426  1.1  christos 	      /* Complain if definition comes from an excluded section.  */
   2427  1.1  christos 	      if (ps->flags & SEC_EXCLUDE)
   2428  1.1  christos 		(*flaginfo->info->callbacks->einfo)
   2429  1.1  christos 		  (_("%X`%s' referenced in section `%A' of %B: "
   2430  1.1  christos 		     "defined in discarded section `%A' of %B\n"),
   2431  1.1  christos 		   h->root.root.string, o, input_bfd, ps, ps->owner);
   2432  1.1  christos 	    }
   2433  1.1  christos 
   2434  1.1  christos 	  /* Call processor specific code to relocate the section
   2435  1.1  christos              contents.  */
   2436  1.1  christos 	  if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
   2437  1.1  christos 					   input_bfd, o,
   2438  1.1  christos 					   contents,
   2439  1.1  christos 					   internal_relocs,
   2440  1.1  christos 					   flaginfo->internal_syms,
   2441  1.1  christos 					   flaginfo->sec_ptrs))
   2442  1.1  christos 	    return FALSE;
   2443  1.6  christos 
   2444  1.1  christos 	  if (bfd_link_relocatable (flaginfo->info))
   2445  1.1  christos 	    {
   2446  1.1  christos 	      bfd_vma offset;
   2447  1.1  christos 	      struct internal_reloc *irelend;
   2448  1.1  christos 	      struct coff_link_hash_entry **rel_hash;
   2449  1.1  christos 
   2450  1.1  christos 	      offset = o->output_section->vma + o->output_offset - o->vma;
   2451  1.1  christos 	      irel = internal_relocs;
   2452  1.1  christos 	      irelend = irel + o->reloc_count;
   2453  1.1  christos 	      rel_hash = (flaginfo->section_info[target_index].rel_hashes
   2454  1.1  christos 			  + o->output_section->reloc_count);
   2455  1.1  christos 	      for (; irel < irelend; irel++, rel_hash++)
   2456  1.1  christos 		{
   2457  1.1  christos 		  struct coff_link_hash_entry *h;
   2458  1.1  christos 		  bfd_boolean adjusted;
   2459  1.1  christos 
   2460  1.1  christos 		  *rel_hash = NULL;
   2461  1.1  christos 
   2462  1.1  christos 		  /* Adjust the reloc address and symbol index.  */
   2463  1.1  christos 		  irel->r_vaddr += offset;
   2464  1.1  christos 
   2465  1.1  christos 		  if (irel->r_symndx == -1)
   2466  1.1  christos 		    continue;
   2467  1.1  christos 
   2468  1.1  christos 		  if (adjust_symndx)
   2469  1.1  christos 		    {
   2470  1.1  christos 		      if (! (*adjust_symndx) (output_bfd, flaginfo->info,
   2471  1.1  christos 					      input_bfd, o, irel,
   2472  1.1  christos 					      &adjusted))
   2473  1.1  christos 			return FALSE;
   2474  1.1  christos 		      if (adjusted)
   2475  1.1  christos 			continue;
   2476  1.1  christos 		    }
   2477  1.1  christos 
   2478  1.1  christos 		  h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
   2479  1.1  christos 		  if (h != NULL)
   2480  1.1  christos 		    {
   2481  1.1  christos 		      /* This is a global symbol.  */
   2482  1.1  christos 		      if (h->indx >= 0)
   2483  1.1  christos 			irel->r_symndx = h->indx;
   2484  1.1  christos 		      else
   2485  1.1  christos 			{
   2486  1.1  christos 			  /* This symbol is being written at the end
   2487  1.1  christos 			     of the file, and we do not yet know the
   2488  1.1  christos 			     symbol index.  We save the pointer to the
   2489  1.1  christos 			     hash table entry in the rel_hash list.
   2490  1.1  christos 			     We set the indx field to -2 to indicate
   2491  1.1  christos 			     that this symbol must not be stripped.  */
   2492  1.1  christos 			  *rel_hash = h;
   2493  1.1  christos 			  h->indx = -2;
   2494  1.1  christos 			}
   2495  1.1  christos 		    }
   2496  1.1  christos 		  else
   2497  1.1  christos 		    {
   2498  1.1  christos 		      long indx;
   2499  1.1  christos 
   2500  1.1  christos 		      indx = flaginfo->sym_indices[irel->r_symndx];
   2501  1.1  christos 		      if (indx != -1)
   2502  1.1  christos 			irel->r_symndx = indx;
   2503  1.1  christos 		      else
   2504  1.1  christos 			{
   2505  1.1  christos 			  struct internal_syment *is;
   2506  1.1  christos 			  const char *name;
   2507  1.1  christos 			  char buf[SYMNMLEN + 1];
   2508  1.1  christos 
   2509  1.1  christos 			  /* This reloc is against a symbol we are
   2510  1.1  christos                              stripping.  This should have been handled
   2511  1.1  christos 			     by the 'dont_skip_symbol' code in the while
   2512  1.1  christos 			     loop at the top of this function.  */
   2513  1.1  christos 			  is = flaginfo->internal_syms + irel->r_symndx;
   2514  1.1  christos 
   2515  1.1  christos 			  name = (_bfd_coff_internal_syment_name
   2516  1.1  christos 				  (input_bfd, is, buf));
   2517  1.1  christos 			  if (name == NULL)
   2518  1.1  christos 			    return FALSE;
   2519  1.6  christos 
   2520  1.6  christos 			  (*flaginfo->info->callbacks->unattached_reloc)
   2521  1.1  christos 			    (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
   2522  1.1  christos 			}
   2523  1.1  christos 		    }
   2524  1.1  christos 		}
   2525  1.1  christos 
   2526  1.1  christos 	      o->output_section->reloc_count += o->reloc_count;
   2527  1.1  christos 	    }
   2528  1.1  christos 	}
   2529  1.1  christos 
   2530  1.1  christos       /* Write out the modified section contents.  */
   2531  1.1  christos       if (secdata == NULL || secdata->stab_info == NULL)
   2532  1.1  christos 	{
   2533  1.1  christos 	  file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
   2534  1.1  christos 	  if (! bfd_set_section_contents (output_bfd, o->output_section,
   2535  1.1  christos 					  contents, loc, o->size))
   2536  1.1  christos 	    return FALSE;
   2537  1.1  christos 	}
   2538  1.1  christos       else
   2539  1.1  christos 	{
   2540  1.1  christos 	  if (! (_bfd_write_section_stabs
   2541  1.1  christos 		 (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
   2542  1.1  christos 		  o, &secdata->stab_info, contents)))
   2543  1.1  christos 	    return FALSE;
   2544  1.1  christos 	}
   2545  1.1  christos     }
   2546  1.1  christos 
   2547  1.1  christos   if (! flaginfo->info->keep_memory
   2548  1.1  christos       && ! _bfd_coff_free_symbols (input_bfd))
   2549  1.1  christos     return FALSE;
   2550  1.1  christos 
   2551  1.1  christos   return TRUE;
   2552  1.1  christos }
   2553  1.1  christos 
   2554  1.1  christos /* Write out a global symbol.  Called via bfd_hash_traverse.  */
   2555  1.1  christos 
   2556  1.1  christos bfd_boolean
   2557  1.1  christos _bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
   2558  1.1  christos {
   2559  1.1  christos   struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
   2560  1.1  christos   struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
   2561  1.1  christos   bfd *output_bfd;
   2562  1.1  christos   struct internal_syment isym;
   2563  1.1  christos   bfd_size_type symesz;
   2564  1.1  christos   unsigned int i;
   2565  1.1  christos   file_ptr pos;
   2566  1.1  christos 
   2567  1.1  christos   output_bfd = flaginfo->output_bfd;
   2568  1.1  christos 
   2569  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   2570  1.1  christos     {
   2571  1.1  christos       h = (struct coff_link_hash_entry *) h->root.u.i.link;
   2572  1.1  christos       if (h->root.type == bfd_link_hash_new)
   2573  1.1  christos 	return TRUE;
   2574  1.1  christos     }
   2575  1.1  christos 
   2576  1.1  christos   if (h->indx >= 0)
   2577  1.1  christos     return TRUE;
   2578  1.1  christos 
   2579  1.1  christos   if (h->indx != -2
   2580  1.1  christos       && (flaginfo->info->strip == strip_all
   2581  1.1  christos 	  || (flaginfo->info->strip == strip_some
   2582  1.1  christos 	      && (bfd_hash_lookup (flaginfo->info->keep_hash,
   2583  1.1  christos 				   h->root.root.string, FALSE, FALSE)
   2584  1.1  christos 		  == NULL))))
   2585  1.1  christos     return TRUE;
   2586  1.1  christos 
   2587  1.1  christos   switch (h->root.type)
   2588  1.1  christos     {
   2589  1.1  christos     default:
   2590  1.1  christos     case bfd_link_hash_new:
   2591  1.1  christos     case bfd_link_hash_warning:
   2592  1.1  christos       abort ();
   2593  1.1  christos       return FALSE;
   2594  1.1  christos 
   2595  1.1  christos     case bfd_link_hash_undefined:
   2596  1.1  christos     case bfd_link_hash_undefweak:
   2597  1.1  christos       isym.n_scnum = N_UNDEF;
   2598  1.1  christos       isym.n_value = 0;
   2599  1.1  christos       break;
   2600  1.1  christos 
   2601  1.1  christos     case bfd_link_hash_defined:
   2602  1.1  christos     case bfd_link_hash_defweak:
   2603  1.1  christos       {
   2604  1.1  christos 	asection *sec;
   2605  1.1  christos 
   2606  1.1  christos 	sec = h->root.u.def.section->output_section;
   2607  1.1  christos 	if (bfd_is_abs_section (sec))
   2608  1.1  christos 	  isym.n_scnum = N_ABS;
   2609  1.1  christos 	else
   2610  1.1  christos 	  isym.n_scnum = sec->target_index;
   2611  1.1  christos 	isym.n_value = (h->root.u.def.value
   2612  1.1  christos 			+ h->root.u.def.section->output_offset);
   2613  1.1  christos 	if (! obj_pe (flaginfo->output_bfd))
   2614  1.1  christos 	  isym.n_value += sec->vma;
   2615  1.1  christos       }
   2616  1.1  christos       break;
   2617  1.1  christos 
   2618  1.1  christos     case bfd_link_hash_common:
   2619  1.1  christos       isym.n_scnum = N_UNDEF;
   2620  1.1  christos       isym.n_value = h->root.u.c.size;
   2621  1.1  christos       break;
   2622  1.1  christos 
   2623  1.1  christos     case bfd_link_hash_indirect:
   2624  1.1  christos       /* Just ignore these.  They can't be handled anyhow.  */
   2625  1.1  christos       return TRUE;
   2626  1.1  christos     }
   2627  1.1  christos 
   2628  1.1  christos   if (strlen (h->root.root.string) <= SYMNMLEN)
   2629  1.1  christos     strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
   2630  1.1  christos   else
   2631  1.1  christos     {
   2632  1.1  christos       bfd_boolean hash;
   2633  1.1  christos       bfd_size_type indx;
   2634  1.1  christos 
   2635  1.6  christos       hash = TRUE;
   2636  1.1  christos       if (flaginfo->info->traditional_format)
   2637  1.1  christos 	hash = FALSE;
   2638  1.1  christos       indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
   2639  1.1  christos 				 FALSE);
   2640  1.1  christos       if (indx == (bfd_size_type) -1)
   2641  1.1  christos 	{
   2642  1.1  christos 	  flaginfo->failed = TRUE;
   2643  1.1  christos 	  return FALSE;
   2644  1.1  christos 	}
   2645  1.1  christos       isym._n._n_n._n_zeroes = 0;
   2646  1.1  christos       isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
   2647  1.1  christos     }
   2648  1.1  christos 
   2649  1.1  christos   isym.n_sclass = h->symbol_class;
   2650  1.1  christos   isym.n_type = h->type;
   2651  1.1  christos 
   2652  1.1  christos   if (isym.n_sclass == C_NULL)
   2653  1.1  christos     isym.n_sclass = C_EXT;
   2654  1.1  christos 
   2655  1.1  christos   /* If doing task linking and this is the pass where we convert
   2656  1.1  christos      defined globals to statics, then do that conversion now.  If the
   2657  1.1  christos      symbol is not being converted, just ignore it and it will be
   2658  1.1  christos      output during a later pass.  */
   2659  1.1  christos   if (flaginfo->global_to_static)
   2660  1.1  christos     {
   2661  1.1  christos       if (! IS_EXTERNAL (output_bfd, isym))
   2662  1.1  christos 	return TRUE;
   2663  1.1  christos 
   2664  1.1  christos       isym.n_sclass = C_STAT;
   2665  1.1  christos     }
   2666  1.1  christos 
   2667  1.1  christos   /* When a weak symbol is not overridden by a strong one,
   2668  1.1  christos      turn it into an external symbol when not building a
   2669  1.6  christos      shared or relocatable object.  */
   2670  1.6  christos   if (! bfd_link_pic (flaginfo->info)
   2671  1.1  christos       && ! bfd_link_relocatable (flaginfo->info)
   2672  1.1  christos       && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
   2673  1.1  christos     isym.n_sclass = C_EXT;
   2674  1.1  christos 
   2675  1.1  christos   isym.n_numaux = h->numaux;
   2676  1.1  christos 
   2677  1.1  christos   bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
   2678  1.1  christos 
   2679  1.1  christos   symesz = bfd_coff_symesz (output_bfd);
   2680  1.1  christos 
   2681  1.1  christos   pos = obj_sym_filepos (output_bfd);
   2682  1.1  christos   pos += obj_raw_syment_count (output_bfd) * symesz;
   2683  1.1  christos   if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
   2684  1.1  christos       || bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
   2685  1.1  christos     {
   2686  1.1  christos       flaginfo->failed = TRUE;
   2687  1.1  christos       return FALSE;
   2688  1.1  christos     }
   2689  1.1  christos 
   2690  1.1  christos   h->indx = obj_raw_syment_count (output_bfd);
   2691  1.1  christos 
   2692  1.1  christos   ++obj_raw_syment_count (output_bfd);
   2693  1.1  christos 
   2694  1.1  christos   /* Write out any associated aux entries.  Most of the aux entries
   2695  1.1  christos      will have been modified in _bfd_coff_link_input_bfd.  We have to
   2696  1.1  christos      handle section aux entries here, now that we have the final
   2697  1.1  christos      relocation and line number counts.  */
   2698  1.1  christos   for (i = 0; i < isym.n_numaux; i++)
   2699  1.1  christos     {
   2700  1.1  christos       union internal_auxent *auxp;
   2701  1.1  christos 
   2702  1.1  christos       auxp = h->aux + i;
   2703  1.1  christos 
   2704  1.1  christos       /* Look for a section aux entry here using the same tests that
   2705  1.1  christos          coff_swap_aux_out uses.  */
   2706  1.1  christos       if (i == 0
   2707  1.1  christos 	  && (isym.n_sclass == C_STAT
   2708  1.1  christos 	      || isym.n_sclass == C_HIDDEN)
   2709  1.1  christos 	  && isym.n_type == T_NULL
   2710  1.1  christos 	  && (h->root.type == bfd_link_hash_defined
   2711  1.1  christos 	      || h->root.type == bfd_link_hash_defweak))
   2712  1.1  christos 	{
   2713  1.1  christos 	  asection *sec;
   2714  1.1  christos 
   2715  1.1  christos 	  sec = h->root.u.def.section->output_section;
   2716  1.1  christos 	  if (sec != NULL)
   2717  1.1  christos 	    {
   2718  1.1  christos 	      auxp->x_scn.x_scnlen = sec->size;
   2719  1.1  christos 
   2720  1.1  christos 	      /* For PE, an overflow on the final link reportedly does
   2721  1.1  christos                  not matter.  FIXME: Why not?  */
   2722  1.1  christos 	      if (sec->reloc_count > 0xffff
   2723  1.6  christos 		  && (! obj_pe (output_bfd)
   2724  1.1  christos 		      || bfd_link_relocatable (flaginfo->info)))
   2725  1.1  christos 		(*_bfd_error_handler)
   2726  1.1  christos 		  (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
   2727  1.1  christos 		   bfd_get_filename (output_bfd),
   2728  1.1  christos 		   bfd_get_section_name (output_bfd, sec),
   2729  1.1  christos 		   sec->reloc_count);
   2730  1.1  christos 
   2731  1.1  christos 	      if (sec->lineno_count > 0xffff
   2732  1.6  christos 		  && (! obj_pe (output_bfd)
   2733  1.1  christos 		      || bfd_link_relocatable (flaginfo->info)))
   2734  1.1  christos 		(*_bfd_error_handler)
   2735  1.1  christos 		  (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
   2736  1.1  christos 		   bfd_get_filename (output_bfd),
   2737  1.1  christos 		   bfd_get_section_name (output_bfd, sec),
   2738  1.1  christos 		   sec->lineno_count);
   2739  1.1  christos 
   2740  1.1  christos 	      auxp->x_scn.x_nreloc = sec->reloc_count;
   2741  1.1  christos 	      auxp->x_scn.x_nlinno = sec->lineno_count;
   2742  1.1  christos 	      auxp->x_scn.x_checksum = 0;
   2743  1.1  christos 	      auxp->x_scn.x_associated = 0;
   2744  1.1  christos 	      auxp->x_scn.x_comdat = 0;
   2745  1.1  christos 	    }
   2746  1.1  christos 	}
   2747  1.1  christos 
   2748  1.1  christos       bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
   2749  1.1  christos 			     isym.n_sclass, (int) i, isym.n_numaux,
   2750  1.1  christos 			     flaginfo->outsyms);
   2751  1.1  christos       if (bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
   2752  1.1  christos 	{
   2753  1.1  christos 	  flaginfo->failed = TRUE;
   2754  1.1  christos 	  return FALSE;
   2755  1.1  christos 	}
   2756  1.1  christos       ++obj_raw_syment_count (output_bfd);
   2757  1.1  christos     }
   2758  1.1  christos 
   2759  1.1  christos   return TRUE;
   2760  1.1  christos }
   2761  1.1  christos 
   2762  1.1  christos /* Write out task global symbols, converting them to statics.  Called
   2763  1.1  christos    via coff_link_hash_traverse.  Calls bfd_coff_write_global_sym to do
   2764  1.1  christos    the dirty work, if the symbol we are processing needs conversion.  */
   2765  1.1  christos 
   2766  1.1  christos bfd_boolean
   2767  1.1  christos _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
   2768  1.1  christos {
   2769  1.1  christos   struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
   2770  1.1  christos   bfd_boolean rtnval = TRUE;
   2771  1.1  christos   bfd_boolean save_global_to_static;
   2772  1.1  christos 
   2773  1.1  christos   if (h->root.type == bfd_link_hash_warning)
   2774  1.1  christos     h = (struct coff_link_hash_entry *) h->root.u.i.link;
   2775  1.1  christos 
   2776  1.1  christos   if (h->indx < 0)
   2777  1.1  christos     {
   2778  1.1  christos       switch (h->root.type)
   2779  1.1  christos 	{
   2780  1.1  christos 	case bfd_link_hash_defined:
   2781  1.1  christos 	case bfd_link_hash_defweak:
   2782  1.1  christos 	  save_global_to_static = flaginfo->global_to_static;
   2783  1.1  christos 	  flaginfo->global_to_static = TRUE;
   2784  1.1  christos 	  rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
   2785  1.1  christos 	  flaginfo->global_to_static = save_global_to_static;
   2786  1.1  christos 	  break;
   2787  1.1  christos 	default:
   2788  1.1  christos 	  break;
   2789  1.1  christos 	}
   2790  1.1  christos     }
   2791  1.1  christos   return (rtnval);
   2792  1.1  christos }
   2793  1.1  christos 
   2794  1.1  christos /* Handle a link order which is supposed to generate a reloc.  */
   2795  1.1  christos 
   2796  1.1  christos bfd_boolean
   2797  1.1  christos _bfd_coff_reloc_link_order (bfd *output_bfd,
   2798  1.1  christos 			    struct coff_final_link_info *flaginfo,
   2799  1.1  christos 			    asection *output_section,
   2800  1.1  christos 			    struct bfd_link_order *link_order)
   2801  1.1  christos {
   2802  1.1  christos   reloc_howto_type *howto;
   2803  1.1  christos   struct internal_reloc *irel;
   2804  1.1  christos   struct coff_link_hash_entry **rel_hash_ptr;
   2805  1.1  christos 
   2806  1.1  christos   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   2807  1.1  christos   if (howto == NULL)
   2808  1.1  christos     {
   2809  1.1  christos       bfd_set_error (bfd_error_bad_value);
   2810  1.1  christos       return FALSE;
   2811  1.1  christos     }
   2812  1.1  christos 
   2813  1.1  christos   if (link_order->u.reloc.p->addend != 0)
   2814  1.1  christos     {
   2815  1.1  christos       bfd_size_type size;
   2816  1.1  christos       bfd_byte *buf;
   2817  1.1  christos       bfd_reloc_status_type rstat;
   2818  1.1  christos       bfd_boolean ok;
   2819  1.1  christos       file_ptr loc;
   2820  1.1  christos 
   2821  1.1  christos       size = bfd_get_reloc_size (howto);
   2822  1.5  christos       buf = (bfd_byte *) bfd_zmalloc (size);
   2823  1.1  christos       if (buf == NULL && size != 0)
   2824  1.1  christos 	return FALSE;
   2825  1.1  christos 
   2826  1.1  christos       rstat = _bfd_relocate_contents (howto, output_bfd,
   2827  1.1  christos 				      (bfd_vma) link_order->u.reloc.p->addend,\
   2828  1.1  christos 				      buf);
   2829  1.1  christos       switch (rstat)
   2830  1.1  christos 	{
   2831  1.1  christos 	case bfd_reloc_ok:
   2832  1.1  christos 	  break;
   2833  1.1  christos 	default:
   2834  1.1  christos 	case bfd_reloc_outofrange:
   2835  1.1  christos 	  abort ();
   2836  1.6  christos 	case bfd_reloc_overflow:
   2837  1.6  christos 	  (*flaginfo->info->callbacks->reloc_overflow)
   2838  1.6  christos 	    (flaginfo->info, NULL,
   2839  1.6  christos 	     (link_order->type == bfd_section_reloc_link_order
   2840  1.6  christos 	      ? bfd_section_name (output_bfd,
   2841  1.6  christos 				  link_order->u.reloc.p->u.section)
   2842  1.6  christos 	      : link_order->u.reloc.p->u.name),
   2843  1.6  christos 	     howto->name, link_order->u.reloc.p->addend,
   2844  1.1  christos 	     (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
   2845  1.1  christos 	  break;
   2846  1.1  christos 	}
   2847  1.1  christos       loc = link_order->offset * bfd_octets_per_byte (output_bfd);
   2848  1.1  christos       ok = bfd_set_section_contents (output_bfd, output_section, buf,
   2849  1.1  christos                                      loc, size);
   2850  1.1  christos       free (buf);
   2851  1.1  christos       if (! ok)
   2852  1.1  christos 	return FALSE;
   2853  1.1  christos     }
   2854  1.1  christos 
   2855  1.1  christos   /* Store the reloc information in the right place.  It will get
   2856  1.1  christos      swapped and written out at the end of the final_link routine.  */
   2857  1.1  christos   irel = (flaginfo->section_info[output_section->target_index].relocs
   2858  1.1  christos 	  + output_section->reloc_count);
   2859  1.1  christos   rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
   2860  1.1  christos 		  + output_section->reloc_count);
   2861  1.1  christos 
   2862  1.1  christos   memset (irel, 0, sizeof (struct internal_reloc));
   2863  1.1  christos   *rel_hash_ptr = NULL;
   2864  1.1  christos 
   2865  1.1  christos   irel->r_vaddr = output_section->vma + link_order->offset;
   2866  1.1  christos 
   2867  1.1  christos   if (link_order->type == bfd_section_reloc_link_order)
   2868  1.1  christos     {
   2869  1.1  christos       /* We need to somehow locate a symbol in the right section.  The
   2870  1.1  christos          symbol must either have a value of zero, or we must adjust
   2871  1.1  christos          the addend by the value of the symbol.  FIXME: Write this
   2872  1.1  christos          when we need it.  The old linker couldn't handle this anyhow.  */
   2873  1.1  christos       abort ();
   2874  1.1  christos       *rel_hash_ptr = NULL;
   2875  1.1  christos       irel->r_symndx = 0;
   2876  1.1  christos     }
   2877  1.1  christos   else
   2878  1.1  christos     {
   2879  1.1  christos       struct coff_link_hash_entry *h;
   2880  1.1  christos 
   2881  1.1  christos       h = ((struct coff_link_hash_entry *)
   2882  1.1  christos 	   bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
   2883  1.1  christos 					 link_order->u.reloc.p->u.name,
   2884  1.1  christos 					 FALSE, FALSE, TRUE));
   2885  1.1  christos       if (h != NULL)
   2886  1.1  christos 	{
   2887  1.1  christos 	  if (h->indx >= 0)
   2888  1.1  christos 	    irel->r_symndx = h->indx;
   2889  1.1  christos 	  else
   2890  1.1  christos 	    {
   2891  1.1  christos 	      /* Set the index to -2 to force this symbol to get
   2892  1.1  christos 		 written out.  */
   2893  1.1  christos 	      h->indx = -2;
   2894  1.1  christos 	      *rel_hash_ptr = h;
   2895  1.1  christos 	      irel->r_symndx = 0;
   2896  1.1  christos 	    }
   2897  1.1  christos 	}
   2898  1.1  christos       else
   2899  1.6  christos 	{
   2900  1.6  christos 	  (*flaginfo->info->callbacks->unattached_reloc)
   2901  1.6  christos 	    (flaginfo->info, link_order->u.reloc.p->u.name,
   2902  1.1  christos 	     (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
   2903  1.1  christos 	  irel->r_symndx = 0;
   2904  1.1  christos 	}
   2905  1.1  christos     }
   2906  1.1  christos 
   2907  1.1  christos   /* FIXME: Is this always right?  */
   2908  1.1  christos   irel->r_type = howto->type;
   2909  1.1  christos 
   2910  1.1  christos   /* r_size is only used on the RS/6000, which needs its own linker
   2911  1.1  christos      routines anyhow.  r_extern is only used for ECOFF.  */
   2912  1.1  christos 
   2913  1.1  christos   /* FIXME: What is the right value for r_offset?  Is zero OK?  */
   2914  1.1  christos   ++output_section->reloc_count;
   2915  1.1  christos 
   2916  1.1  christos   return TRUE;
   2917  1.1  christos }
   2918  1.1  christos 
   2919  1.1  christos /* A basic reloc handling routine which may be used by processors with
   2920  1.1  christos    simple relocs.  */
   2921  1.1  christos 
   2922  1.1  christos bfd_boolean
   2923  1.1  christos _bfd_coff_generic_relocate_section (bfd *output_bfd,
   2924  1.1  christos 				    struct bfd_link_info *info,
   2925  1.1  christos 				    bfd *input_bfd,
   2926  1.1  christos 				    asection *input_section,
   2927  1.1  christos 				    bfd_byte *contents,
   2928  1.1  christos 				    struct internal_reloc *relocs,
   2929  1.1  christos 				    struct internal_syment *syms,
   2930  1.1  christos 				    asection **sections)
   2931  1.1  christos {
   2932  1.1  christos   struct internal_reloc *rel;
   2933  1.1  christos   struct internal_reloc *relend;
   2934  1.1  christos 
   2935  1.1  christos   rel = relocs;
   2936  1.1  christos   relend = rel + input_section->reloc_count;
   2937  1.1  christos   for (; rel < relend; rel++)
   2938  1.1  christos     {
   2939  1.1  christos       long symndx;
   2940  1.1  christos       struct coff_link_hash_entry *h;
   2941  1.1  christos       struct internal_syment *sym;
   2942  1.1  christos       bfd_vma addend;
   2943  1.5  christos       bfd_vma val;
   2944  1.1  christos       asection *sec;
   2945  1.1  christos       reloc_howto_type *howto;
   2946  1.1  christos       bfd_reloc_status_type rstat;
   2947  1.1  christos 
   2948  1.1  christos       symndx = rel->r_symndx;
   2949  1.1  christos 
   2950  1.1  christos       if (symndx == -1)
   2951  1.1  christos 	{
   2952  1.1  christos 	  h = NULL;
   2953  1.1  christos 	  sym = NULL;
   2954  1.1  christos 	}
   2955  1.1  christos       else if (symndx < 0
   2956  1.1  christos 	       || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
   2957  1.1  christos 	{
   2958  1.1  christos 	  (*_bfd_error_handler)
   2959  1.1  christos 	    ("%B: illegal symbol index %ld in relocs", input_bfd, symndx);
   2960  1.1  christos 	  return FALSE;
   2961  1.1  christos 	}
   2962  1.1  christos       else
   2963  1.1  christos 	{
   2964  1.1  christos 	  h = obj_coff_sym_hashes (input_bfd)[symndx];
   2965  1.1  christos 	  sym = syms + symndx;
   2966  1.1  christos 	}
   2967  1.1  christos 
   2968  1.1  christos       /* COFF treats common symbols in one of two ways.  Either the
   2969  1.1  christos          size of the symbol is included in the section contents, or it
   2970  1.1  christos          is not.  We assume that the size is not included, and force
   2971  1.1  christos          the rtype_to_howto function to adjust the addend as needed.  */
   2972  1.1  christos       if (sym != NULL && sym->n_scnum != 0)
   2973  1.1  christos 	addend = - sym->n_value;
   2974  1.1  christos       else
   2975  1.1  christos 	addend = 0;
   2976  1.1  christos 
   2977  1.1  christos       howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
   2978  1.1  christos 				       sym, &addend);
   2979  1.1  christos       if (howto == NULL)
   2980  1.1  christos 	return FALSE;
   2981  1.1  christos 
   2982  1.1  christos       /* If we are doing a relocatable link, then we can just ignore
   2983  1.1  christos          a PC relative reloc that is pcrel_offset.  It will already
   2984  1.1  christos          have the correct value.  If this is not a relocatable link,
   2985  1.1  christos          then we should ignore the symbol value.  */
   2986  1.1  christos       if (howto->pc_relative && howto->pcrel_offset)
   2987  1.6  christos 	{
   2988  1.1  christos 	  if (bfd_link_relocatable (info))
   2989  1.1  christos 	    continue;
   2990  1.1  christos 	  if (sym != NULL && sym->n_scnum != 0)
   2991  1.1  christos 	    addend += sym->n_value;
   2992  1.1  christos 	}
   2993  1.1  christos 
   2994  1.5  christos       val = 0;
   2995  1.1  christos       sec = NULL;
   2996  1.1  christos       if (h == NULL)
   2997  1.1  christos 	{
   2998  1.1  christos 	  if (symndx == -1)
   2999  1.1  christos 	    {
   3000  1.1  christos 	      sec = bfd_abs_section_ptr;
   3001  1.1  christos 	      val = 0;
   3002  1.1  christos 	    }
   3003  1.1  christos 	  else
   3004  1.1  christos 	    {
   3005  1.6  christos 	      sec = sections[symndx];
   3006  1.6  christos 
   3007  1.6  christos 	      /* PR 19623: Relocations against symbols in
   3008  1.6  christos 		 the absolute sections should ignored.  */
   3009  1.6  christos               if (bfd_is_abs_section (sec))
   3010  1.6  christos 		continue;
   3011  1.1  christos 
   3012  1.1  christos               val = (sec->output_section->vma
   3013  1.1  christos 		     + sec->output_offset
   3014  1.1  christos 		     + sym->n_value);
   3015  1.1  christos 	      if (! obj_pe (input_bfd))
   3016  1.1  christos 		val -= sec->vma;
   3017  1.1  christos 	    }
   3018  1.1  christos 	}
   3019  1.1  christos       else
   3020  1.1  christos 	{
   3021  1.1  christos 	  if (h->root.type == bfd_link_hash_defined
   3022  1.1  christos 	      || h->root.type == bfd_link_hash_defweak)
   3023  1.1  christos 	    {
   3024  1.1  christos 	      /* Defined weak symbols are a GNU extension. */
   3025  1.1  christos 	      sec = h->root.u.def.section;
   3026  1.1  christos 	      val = (h->root.u.def.value
   3027  1.1  christos 		     + sec->output_section->vma
   3028  1.1  christos 		     + sec->output_offset);
   3029  1.1  christos 	    }
   3030  1.1  christos 
   3031  1.1  christos 	  else if (h->root.type == bfd_link_hash_undefweak)
   3032  1.1  christos 	    {
   3033  1.1  christos               if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
   3034  1.1  christos 		{
   3035  1.1  christos 		  /* See _Microsoft Portable Executable and Common Object
   3036  1.1  christos                      File Format Specification_, section 5.5.3.
   3037  1.1  christos 		     Note that weak symbols without aux records are a GNU
   3038  1.1  christos 		     extension.
   3039  1.1  christos 		     FIXME: All weak externals are treated as having
   3040  1.1  christos 		     characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
   3041  1.1  christos 		     These behave as per SVR4 ABI:  A library member
   3042  1.1  christos 		     will resolve a weak external only if a normal
   3043  1.1  christos 		     external causes the library member to be linked.
   3044  1.1  christos 		     See also linker.c: generic_link_check_archive_element. */
   3045  1.1  christos 		  struct coff_link_hash_entry *h2 =
   3046  1.1  christos 		    h->auxbfd->tdata.coff_obj_data->sym_hashes[
   3047  1.1  christos 		    h->aux->x_sym.x_tagndx.l];
   3048  1.1  christos 
   3049  1.1  christos 		  if (!h2 || h2->root.type == bfd_link_hash_undefined)
   3050  1.1  christos 		    {
   3051  1.1  christos 		      sec = bfd_abs_section_ptr;
   3052  1.1  christos 		      val = 0;
   3053  1.1  christos 		    }
   3054  1.1  christos 		  else
   3055  1.1  christos 		    {
   3056  1.1  christos 		      sec = h2->root.u.def.section;
   3057  1.1  christos 		      val = h2->root.u.def.value
   3058  1.1  christos 			+ sec->output_section->vma + sec->output_offset;
   3059  1.1  christos 		    }
   3060  1.1  christos 		}
   3061  1.1  christos 	      else
   3062  1.1  christos                 /* This is a GNU extension.  */
   3063  1.1  christos 		val = 0;
   3064  1.1  christos 	    }
   3065  1.6  christos 
   3066  1.6  christos 	  else if (! bfd_link_relocatable (info))
   3067  1.6  christos 	    (*info->callbacks->undefined_symbol)
   3068  1.6  christos 	      (info, h->root.root.string, input_bfd, input_section,
   3069  1.1  christos 	       rel->r_vaddr - input_section->vma, TRUE);
   3070  1.1  christos 	}
   3071  1.5  christos 
   3072  1.5  christos       /* If the input section defining the symbol has been discarded
   3073  1.5  christos 	 then zero this reloc field.  */
   3074  1.5  christos       if (sec != NULL && discarded_section (sec))
   3075  1.5  christos 	{
   3076  1.5  christos 	  _bfd_clear_contents (howto, input_bfd, input_section,
   3077  1.5  christos 			       contents + (rel->r_vaddr - input_section->vma));
   3078  1.5  christos 	  continue;
   3079  1.5  christos 	}
   3080  1.1  christos 
   3081  1.1  christos       if (info->base_file)
   3082  1.1  christos 	{
   3083  1.1  christos 	  /* Emit a reloc if the backend thinks it needs it.  */
   3084  1.1  christos 	  if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
   3085  1.1  christos 	    {
   3086  1.1  christos 	      /* Relocation to a symbol in a section which isn't
   3087  1.1  christos 		 absolute.  We output the address here to a file.
   3088  1.1  christos 		 This file is then read by dlltool when generating the
   3089  1.1  christos 		 reloc section.  Note that the base file is not
   3090  1.1  christos 		 portable between systems.  We write out a bfd_vma here,
   3091  1.1  christos 		 and dlltool reads in a bfd_vma.  */
   3092  1.1  christos 	      bfd_vma addr = (rel->r_vaddr
   3093  1.1  christos 			   - input_section->vma
   3094  1.1  christos 			   + input_section->output_offset
   3095  1.1  christos 			   + input_section->output_section->vma);
   3096  1.1  christos 	      if (coff_data (output_bfd)->pe)
   3097  1.1  christos 		addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
   3098  1.1  christos 	      if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
   3099  1.1  christos 		  != sizeof (bfd_vma))
   3100  1.1  christos 		{
   3101  1.1  christos 		  bfd_set_error (bfd_error_system_call);
   3102  1.1  christos 		  return FALSE;
   3103  1.1  christos 		}
   3104  1.1  christos 	    }
   3105  1.1  christos 	}
   3106  1.1  christos 
   3107  1.1  christos       rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
   3108  1.1  christos 					contents,
   3109  1.1  christos 					rel->r_vaddr - input_section->vma,
   3110  1.1  christos 					val, addend);
   3111  1.1  christos 
   3112  1.1  christos       switch (rstat)
   3113  1.1  christos 	{
   3114  1.1  christos 	default:
   3115  1.1  christos 	  abort ();
   3116  1.1  christos 	case bfd_reloc_ok:
   3117  1.1  christos 	  break;
   3118  1.1  christos 	case bfd_reloc_outofrange:
   3119  1.1  christos 	  (*_bfd_error_handler)
   3120  1.1  christos 	    (_("%B: bad reloc address 0x%lx in section `%A'"),
   3121  1.1  christos 	     input_bfd, input_section, (unsigned long) rel->r_vaddr);
   3122  1.1  christos 	  return FALSE;
   3123  1.1  christos 	case bfd_reloc_overflow:
   3124  1.1  christos 	  {
   3125  1.1  christos 	    const char *name;
   3126  1.1  christos 	    char buf[SYMNMLEN + 1];
   3127  1.1  christos 
   3128  1.1  christos 	    if (symndx == -1)
   3129  1.1  christos 	      name = "*ABS*";
   3130  1.1  christos 	    else if (h != NULL)
   3131  1.1  christos 	      name = NULL;
   3132  1.1  christos 	    else
   3133  1.1  christos 	      {
   3134  1.1  christos 		name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
   3135  1.1  christos 		if (name == NULL)
   3136  1.1  christos 		  return FALSE;
   3137  1.1  christos 	      }
   3138  1.6  christos 
   3139  1.6  christos 	    (*info->callbacks->reloc_overflow)
   3140  1.6  christos 	      (info, (h ? &h->root : NULL), name, howto->name,
   3141  1.6  christos 	       (bfd_vma) 0, input_bfd, input_section,
   3142  1.1  christos 	       rel->r_vaddr - input_section->vma);
   3143  1.1  christos 	  }
   3144  1.1  christos 	}
   3145  1.1  christos     }
   3146  1.1  christos   return TRUE;
   3147                }
   3148