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