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