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