Home | History | Annotate | Line # | Download | only in bfd
coffgen.c revision 1.1.1.7
      1 /* Support for the generic parts of COFF, for BFD.
      2    Copyright (C) 1990-2024 Free Software Foundation, Inc.
      3    Written by 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 /* Most of this hacked by  Steve Chamberlain, sac (at) cygnus.com.
     23    Split out of coffcode.h by Ian Taylor, ian (at) cygnus.com.  */
     24 
     25 /* This file contains COFF code that is not dependent on any
     26    particular COFF target.  There is only one version of this file in
     27    libbfd.a, so no target specific code may be put in here.  Or, to
     28    put it another way,
     29 
     30    ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
     31 
     32    If you need to add some target specific behaviour, add a new hook
     33    function to bfd_coff_backend_data.
     34 
     35    Some of these functions are also called by the ECOFF routines.
     36    Those functions may not use any COFF specific information, such as
     37    coff_data (abfd).  */
     38 
     39 #include "sysdep.h"
     40 #include <limits.h>
     41 #include "bfd.h"
     42 #include "libbfd.h"
     43 #include "coff/internal.h"
     44 #include "libcoff.h"
     45 #include "hashtab.h"
     46 
     47 /* Extract a long section name at STRINDEX and copy it to the bfd objstack.
     48    Return NULL in case of error.  */
     49 
     50 static char *
     51 extract_long_section_name(bfd *abfd, unsigned long strindex)
     52 {
     53   const char *strings;
     54   char *name;
     55 
     56   strings = _bfd_coff_read_string_table (abfd);
     57   if (strings == NULL)
     58     return NULL;
     59   if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
     60     return NULL;
     61   strings += strindex;
     62   name = (char *) bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
     63   if (name == NULL)
     64     return NULL;
     65   strcpy (name, strings);
     66 
     67   return name;
     68 }
     69 
     70 /* Decode a base 64 coded string at STR of length LEN, and write the result
     71    to RES.  Return true on success.
     72    Return false in case of invalid character or overflow.  */
     73 
     74 static bool
     75 decode_base64 (const char *str, unsigned len, uint32_t *res)
     76 {
     77   unsigned i;
     78   uint32_t val;
     79 
     80   val = 0;
     81   for (i = 0; i < len; i++)
     82     {
     83       char c = str[i];
     84       unsigned d;
     85 
     86       if (c >= 'A' && c <= 'Z')
     87 	d = c - 'A';
     88       else if (c >= 'a' && c <= 'z')
     89 	d = c - 'a' + 26;
     90       else if (c >= '0' && c <= '9')
     91 	d = c - '0' + 52;
     92       else if (c == '+')
     93 	d = 62;
     94       else if (c == '/')
     95 	d = 63;
     96       else
     97 	return false;
     98 
     99       /* Check for overflow. */
    100       if ((val >> 26) != 0)
    101 	return false;
    102 
    103       val = (val << 6) + d;
    104     }
    105 
    106   *res = val;
    107   return true;
    108 }
    109 
    110 /* Take a section header read from a coff file (in HOST byte order),
    111    and make a BFD "section" out of it.  This is used by ECOFF.  */
    112 
    113 static bool
    114 make_a_section_from_file (bfd *abfd,
    115 			  struct internal_scnhdr *hdr,
    116 			  unsigned int target_index)
    117 {
    118   asection *newsect;
    119   char *name;
    120   bool result = true;
    121   flagword flags;
    122 
    123   name = NULL;
    124 
    125   /* Handle long section names as in PE.  On reading, we want to
    126     accept long names if the format permits them at all, regardless
    127     of the current state of the flag that dictates if we would generate
    128     them in outputs; this construct checks if that is the case by
    129     attempting to set the flag, without changing its state; the call
    130     will fail for formats that do not support long names at all.  */
    131   if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
    132       && hdr->s_name[0] == '/')
    133     {
    134       /* Flag that this BFD uses long names, even though the format might
    135 	 expect them to be off by default.  This won't directly affect the
    136 	 format of any output BFD created from this one, but the information
    137 	 can be used to decide what to do.  */
    138       bfd_coff_set_long_section_names (abfd, true);
    139 
    140       if (hdr->s_name[1] == '/')
    141 	{
    142 	  /* LLVM extension: the '/' is followed by another '/' and then by
    143 	     the index in the strtab encoded in base64 without NUL at the
    144 	     end.  */
    145 	  uint32_t strindex;
    146 
    147 	  /* Decode the index.  No overflow is expected as the string table
    148 	     length is at most 2^32 - 1 (the length is written on the first
    149 	     four bytes).
    150 	     Also, contrary to RFC 4648, all the characters must be decoded,
    151 	     there is no padding.  */
    152 	  if (!decode_base64 (hdr->s_name + 2, SCNNMLEN - 2, &strindex))
    153 	    return false;
    154 
    155 	  name = extract_long_section_name (abfd, strindex);
    156 	  if (name == NULL)
    157 	    return false;
    158 	}
    159       else
    160 	{
    161 	  /* PE classic long section name.  The '/' is followed by the index
    162 	     in the strtab.  The index is formatted as a decimal string.  */
    163 	  char buf[SCNNMLEN];
    164 	  long strindex;
    165 	  char *p;
    166 
    167 	  memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
    168 	  buf[SCNNMLEN - 1] = '\0';
    169 	  strindex = strtol (buf, &p, 10);
    170 	  if (*p == '\0' && strindex >= 0)
    171 	    {
    172 	      name = extract_long_section_name (abfd, strindex);
    173 	      if (name == NULL)
    174 		return false;
    175 	    }
    176 	}
    177     }
    178 
    179   if (name == NULL)
    180     {
    181       /* Assorted wastage to null-terminate the name, thanks AT&T! */
    182       name = (char *) bfd_alloc (abfd,
    183 				 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
    184       if (name == NULL)
    185 	return false;
    186       strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
    187       name[sizeof (hdr->s_name)] = 0;
    188     }
    189 
    190   newsect = bfd_make_section_anyway (abfd, name);
    191   if (newsect == NULL)
    192     return false;
    193 
    194   newsect->vma = hdr->s_vaddr;
    195   newsect->lma = hdr->s_paddr;
    196   newsect->size = hdr->s_size;
    197   newsect->filepos = hdr->s_scnptr;
    198   newsect->rel_filepos = hdr->s_relptr;
    199   newsect->reloc_count = hdr->s_nreloc;
    200 
    201   bfd_coff_set_alignment_hook (abfd, newsect, hdr);
    202 
    203   newsect->line_filepos = hdr->s_lnnoptr;
    204 
    205   newsect->lineno_count = hdr->s_nlnno;
    206   newsect->userdata = NULL;
    207   newsect->next = NULL;
    208   newsect->target_index = target_index;
    209 
    210   if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags))
    211     result = false;
    212 
    213   /* At least on i386-coff, the line number count for a shared library
    214      section must be ignored.  */
    215   if ((flags & SEC_COFF_SHARED_LIBRARY) != 0)
    216     newsect->lineno_count = 0;
    217 
    218   if (hdr->s_nreloc != 0)
    219     flags |= SEC_RELOC;
    220   /* FIXME: should this check 'hdr->s_size > 0'.  */
    221   if (hdr->s_scnptr != 0)
    222     flags |= SEC_HAS_CONTENTS;
    223 
    224   newsect->flags = flags;
    225 
    226   /* Compress/decompress DWARF debug sections.  */
    227   if ((flags & SEC_DEBUGGING) != 0
    228       && (flags & SEC_HAS_CONTENTS) != 0
    229       && (startswith (name, ".debug_")
    230 	  || startswith (name, ".zdebug_")
    231 	  || startswith (name, ".gnu.debuglto_.debug_")
    232 	  || startswith (name, ".gnu.linkonce.wi.")))
    233     {
    234       enum { nothing, compress, decompress } action = nothing;
    235 
    236       if (bfd_is_section_compressed (abfd, newsect))
    237 	{
    238 	  /* Compressed section.  Check if we should decompress.  */
    239 	  if ((abfd->flags & BFD_DECOMPRESS))
    240 	    action = decompress;
    241 	}
    242       else
    243 	{
    244 	  /* Normal section.  Check if we should compress.  */
    245 	  if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
    246 	    action = compress;
    247 	}
    248 
    249       if (action == compress)
    250 	{
    251 	  if (!bfd_init_section_compress_status (abfd, newsect))
    252 	    {
    253 	      _bfd_error_handler
    254 		/* xgettext:c-format */
    255 		(_("%pB: unable to compress section %s"), abfd, name);
    256 	      return false;
    257 	    }
    258 	}
    259       else if (action == decompress)
    260 	{
    261 	  if (!bfd_init_section_decompress_status (abfd, newsect))
    262 	    {
    263 	      _bfd_error_handler
    264 		/* xgettext:c-format */
    265 		(_("%pB: unable to decompress section %s"), abfd, name);
    266 	      return false;
    267 	    }
    268 	  if (abfd->is_linker_input
    269 	      && name[1] == 'z')
    270 	    {
    271 	      /* Rename section from .zdebug_* to .debug_* so that ld
    272 		 scripts will see this section as a debug section.  */
    273 	      char *new_name = bfd_zdebug_name_to_debug (abfd, name);
    274 	      if (new_name == NULL)
    275 		return false;
    276 	      bfd_rename_section (newsect, new_name);
    277 	    }
    278 	}
    279     }
    280 
    281   return result;
    282 }
    283 
    284 void
    285 coff_object_cleanup (bfd *abfd)
    286 {
    287   struct coff_tdata *td = coff_data (abfd);
    288   if (td != NULL)
    289     {
    290       if (td->section_by_index)
    291 	htab_delete (td->section_by_index);
    292       if (td->section_by_target_index)
    293 	htab_delete (td->section_by_target_index);
    294       if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
    295 	htab_delete (pe_data (abfd)->comdat_hash);
    296     }
    297 }
    298 
    299 /* Read in a COFF object and make it into a BFD.  This is used by
    300    ECOFF as well.  */
    301 bfd_cleanup
    302 coff_real_object_p (bfd *abfd,
    303 		    unsigned nscns,
    304 		    struct internal_filehdr *internal_f,
    305 		    struct internal_aouthdr *internal_a)
    306 {
    307   flagword oflags = abfd->flags;
    308   bfd_vma ostart = bfd_get_start_address (abfd);
    309   void * tdata;
    310   void * tdata_save;
    311   bfd_size_type readsize;	/* Length of file_info.  */
    312   unsigned int scnhsz;
    313   char *external_sections;
    314 
    315   if (!(internal_f->f_flags & F_RELFLG))
    316     abfd->flags |= HAS_RELOC;
    317   if ((internal_f->f_flags & F_EXEC))
    318     abfd->flags |= EXEC_P;
    319   if (!(internal_f->f_flags & F_LNNO))
    320     abfd->flags |= HAS_LINENO;
    321   if (!(internal_f->f_flags & F_LSYMS))
    322     abfd->flags |= HAS_LOCALS;
    323 
    324   /* FIXME: How can we set D_PAGED correctly?  */
    325   if ((internal_f->f_flags & F_EXEC) != 0)
    326     abfd->flags |= D_PAGED;
    327 
    328   abfd->symcount = internal_f->f_nsyms;
    329   if (internal_f->f_nsyms)
    330     abfd->flags |= HAS_SYMS;
    331 
    332   if (internal_a != (struct internal_aouthdr *) NULL)
    333     abfd->start_address = internal_a->entry;
    334   else
    335     abfd->start_address = 0;
    336 
    337   /* Set up the tdata area.  ECOFF uses its own routine, and overrides
    338      abfd->flags.  */
    339   tdata_save = abfd->tdata.any;
    340   tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
    341   if (tdata == NULL)
    342     goto fail2;
    343 
    344   scnhsz = bfd_coff_scnhsz (abfd);
    345   readsize = (bfd_size_type) nscns * scnhsz;
    346   external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
    347   if (!external_sections)
    348     goto fail;
    349 
    350   /* Set the arch/mach *before* swapping in sections; section header swapping
    351      may depend on arch/mach info.  */
    352   if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
    353     goto fail;
    354 
    355   /* Now copy data as required; construct all asections etc.  */
    356   if (nscns != 0)
    357     {
    358       unsigned int i;
    359       for (i = 0; i < nscns; i++)
    360 	{
    361 	  struct internal_scnhdr tmp;
    362 	  bfd_coff_swap_scnhdr_in (abfd,
    363 				   (void *) (external_sections + i * scnhsz),
    364 				   (void *) & tmp);
    365 	  if (! make_a_section_from_file (abfd, &tmp, i + 1))
    366 	    goto fail;
    367 	}
    368     }
    369 
    370   _bfd_coff_free_symbols (abfd);
    371   return coff_object_cleanup;
    372 
    373  fail:
    374   coff_object_cleanup (abfd);
    375   _bfd_coff_free_symbols (abfd);
    376   bfd_release (abfd, tdata);
    377  fail2:
    378   abfd->tdata.any = tdata_save;
    379   abfd->flags = oflags;
    380   abfd->start_address = ostart;
    381   return NULL;
    382 }
    383 
    384 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
    385    not a COFF file.  This is also used by ECOFF.  */
    386 
    387 bfd_cleanup
    388 coff_object_p (bfd *abfd)
    389 {
    390   bfd_size_type filhsz;
    391   bfd_size_type aoutsz;
    392   unsigned int nscns;
    393   void * filehdr;
    394   struct internal_filehdr internal_f;
    395   struct internal_aouthdr internal_a;
    396 
    397   /* Figure out how much to read.  */
    398   filhsz = bfd_coff_filhsz (abfd);
    399   aoutsz = bfd_coff_aoutsz (abfd);
    400 
    401   filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
    402   if (filehdr == NULL)
    403     {
    404       if (bfd_get_error () != bfd_error_system_call)
    405 	bfd_set_error (bfd_error_wrong_format);
    406       return NULL;
    407     }
    408   bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
    409   bfd_release (abfd, filehdr);
    410 
    411   /* The XCOFF format has two sizes for the f_opthdr.  SMALL_AOUTSZ
    412      (less than aoutsz) used in object files and AOUTSZ (equal to
    413      aoutsz) in executables.  The bfd_coff_swap_aouthdr_in function
    414      expects this header to be aoutsz bytes in length, so we use that
    415      value in the call to bfd_alloc below.  But we must be careful to
    416      only read in f_opthdr bytes in the call to bfd_read.  We should
    417      also attempt to catch corrupt or non-COFF binaries with a strange
    418      value for f_opthdr.  */
    419   if (! bfd_coff_bad_format_hook (abfd, &internal_f)
    420       || internal_f.f_opthdr > aoutsz)
    421     {
    422       bfd_set_error (bfd_error_wrong_format);
    423       return NULL;
    424     }
    425   nscns = internal_f.f_nscns;
    426 
    427   if (internal_f.f_opthdr)
    428     {
    429       void * opthdr;
    430 
    431       opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
    432       if (opthdr == NULL)
    433 	return NULL;
    434       /* PR 17512: file: 11056-1136-0.004.  */
    435       if (internal_f.f_opthdr < aoutsz)
    436 	memset (((char *) opthdr) + internal_f.f_opthdr, 0,
    437 		aoutsz - internal_f.f_opthdr);
    438 
    439       bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
    440       bfd_release (abfd, opthdr);
    441     }
    442 
    443   return coff_real_object_p (abfd, nscns, &internal_f,
    444 			     (internal_f.f_opthdr != 0
    445 			      ? &internal_a
    446 			      : (struct internal_aouthdr *) NULL));
    447 }
    448 
    449 static hashval_t
    450 htab_hash_section_target_index (const void * entry)
    451 {
    452   const struct bfd_section * sec = entry;
    453   return sec->target_index;
    454 }
    455 
    456 static int
    457 htab_eq_section_target_index (const void * e1, const void * e2)
    458 {
    459   const struct bfd_section * sec1 = e1;
    460   const struct bfd_section * sec2 = e2;
    461   return sec1->target_index == sec2->target_index;
    462 }
    463 
    464 /* Get the BFD section from a COFF symbol section number.  */
    465 
    466 asection *
    467 coff_section_from_bfd_index (bfd *abfd, int section_index)
    468 {
    469   if (section_index == N_ABS)
    470     return bfd_abs_section_ptr;
    471   if (section_index == N_UNDEF)
    472     return bfd_und_section_ptr;
    473   if (section_index == N_DEBUG)
    474     return bfd_abs_section_ptr;
    475 
    476   struct bfd_section *answer;
    477   htab_t table = coff_data (abfd)->section_by_target_index;
    478 
    479   if (!table)
    480     {
    481       table = htab_create (10, htab_hash_section_target_index,
    482 			   htab_eq_section_target_index, NULL);
    483       if (table == NULL)
    484 	return bfd_und_section_ptr;
    485       coff_data (abfd)->section_by_target_index = table;
    486     }
    487 
    488   if (htab_elements (table) == 0)
    489     {
    490       for (answer = abfd->sections; answer; answer = answer->next)
    491 	{
    492 	  void **slot = htab_find_slot (table, answer, INSERT);
    493 	  if (slot == NULL)
    494 	    return bfd_und_section_ptr;
    495 	  *slot = answer;
    496 	}
    497     }
    498 
    499   struct bfd_section needle;
    500   needle.target_index = section_index;
    501 
    502   answer = htab_find (table, &needle);
    503   if (answer != NULL)
    504     return answer;
    505 
    506   /* Cover the unlikely case of sections added after the first call to
    507      this function.  */
    508   for (answer = abfd->sections; answer; answer = answer->next)
    509     if (answer->target_index == section_index)
    510       {
    511 	void **slot = htab_find_slot (table, answer, INSERT);
    512 	if (slot != NULL)
    513 	  *slot = answer;
    514 	return answer;
    515       }
    516 
    517   /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
    518      has a bad symbol table in biglitpow.o.  */
    519   return bfd_und_section_ptr;
    520 }
    521 
    522 /* Get the upper bound of a COFF symbol table.  */
    523 
    524 long
    525 coff_get_symtab_upper_bound (bfd *abfd)
    526 {
    527   if (!bfd_coff_slurp_symbol_table (abfd))
    528     return -1;
    529 
    530   return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
    531 }
    532 
    533 /* Canonicalize a COFF symbol table.  */
    534 
    535 long
    536 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
    537 {
    538   unsigned int counter;
    539   coff_symbol_type *symbase;
    540   coff_symbol_type **location = (coff_symbol_type **) alocation;
    541 
    542   if (!bfd_coff_slurp_symbol_table (abfd))
    543     return -1;
    544 
    545   symbase = obj_symbols (abfd);
    546   counter = bfd_get_symcount (abfd);
    547   while (counter-- > 0)
    548     *location++ = symbase++;
    549 
    550   *location = NULL;
    551 
    552   return bfd_get_symcount (abfd);
    553 }
    554 
    555 /* Get the name of a symbol.  The caller must pass in a buffer of size
    556    >= SYMNMLEN + 1.  */
    557 
    558 const char *
    559 _bfd_coff_internal_syment_name (bfd *abfd,
    560 				const struct internal_syment *sym,
    561 				char *buf)
    562 {
    563   /* FIXME: It's not clear this will work correctly if sizeof
    564      (_n_zeroes) != 4.  */
    565   if (sym->_n._n_n._n_zeroes != 0
    566       || sym->_n._n_n._n_offset == 0)
    567     {
    568       memcpy (buf, sym->_n._n_name, SYMNMLEN);
    569       buf[SYMNMLEN] = '\0';
    570       return buf;
    571     }
    572   else
    573     {
    574       const char *strings;
    575 
    576       BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
    577       strings = obj_coff_strings (abfd);
    578       if (strings == NULL)
    579 	{
    580 	  strings = _bfd_coff_read_string_table (abfd);
    581 	  if (strings == NULL)
    582 	    return NULL;
    583 	}
    584       if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
    585 	return NULL;
    586       return strings + sym->_n._n_n._n_offset;
    587     }
    588 }
    589 
    590 /* Read in and swap the relocs.  This returns a buffer holding the
    591    relocs for section SEC in file ABFD.  If CACHE is TRUE and
    592    INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
    593    the function is called again.  If EXTERNAL_RELOCS is not NULL, it
    594    is a buffer large enough to hold the unswapped relocs.  If
    595    INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
    596    the swapped relocs.  If REQUIRE_INTERNAL is TRUE, then the return
    597    value must be INTERNAL_RELOCS.  The function returns NULL on error.  */
    598 
    599 struct internal_reloc *
    600 _bfd_coff_read_internal_relocs (bfd *abfd,
    601 				asection *sec,
    602 				bool cache,
    603 				bfd_byte *external_relocs,
    604 				bool require_internal,
    605 				struct internal_reloc *internal_relocs)
    606 {
    607   bfd_size_type relsz;
    608   bfd_byte *free_external = NULL;
    609   struct internal_reloc *free_internal = NULL;
    610   bfd_byte *erel;
    611   bfd_byte *erel_end;
    612   struct internal_reloc *irel;
    613   bfd_size_type amt;
    614 
    615   if (sec->reloc_count == 0)
    616     return internal_relocs;	/* Nothing to do.  */
    617 
    618   if (coff_section_data (abfd, sec) != NULL
    619       && coff_section_data (abfd, sec)->relocs != NULL)
    620     {
    621       if (! require_internal)
    622 	return coff_section_data (abfd, sec)->relocs;
    623       memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
    624 	      sec->reloc_count * sizeof (struct internal_reloc));
    625       return internal_relocs;
    626     }
    627 
    628   relsz = bfd_coff_relsz (abfd);
    629 
    630   amt = sec->reloc_count * relsz;
    631   if (external_relocs == NULL)
    632     {
    633       free_external = (bfd_byte *) bfd_malloc (amt);
    634       if (free_external == NULL)
    635 	goto error_return;
    636       external_relocs = free_external;
    637     }
    638 
    639   if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
    640       || bfd_read (external_relocs, amt, abfd) != amt)
    641     goto error_return;
    642 
    643   if (internal_relocs == NULL)
    644     {
    645       amt = sec->reloc_count;
    646       amt *= sizeof (struct internal_reloc);
    647       free_internal = (struct internal_reloc *) bfd_malloc (amt);
    648       if (free_internal == NULL)
    649 	goto error_return;
    650       internal_relocs = free_internal;
    651     }
    652 
    653   /* Swap in the relocs.  */
    654   erel = external_relocs;
    655   erel_end = erel + relsz * sec->reloc_count;
    656   irel = internal_relocs;
    657   for (; erel < erel_end; erel += relsz, irel++)
    658     bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
    659 
    660   free (free_external);
    661   free_external = NULL;
    662 
    663   if (cache && free_internal != NULL)
    664     {
    665       if (coff_section_data (abfd, sec) == NULL)
    666 	{
    667 	  amt = sizeof (struct coff_section_tdata);
    668 	  sec->used_by_bfd = bfd_zalloc (abfd, amt);
    669 	  if (sec->used_by_bfd == NULL)
    670 	    goto error_return;
    671 	  coff_section_data (abfd, sec)->contents = NULL;
    672 	}
    673       coff_section_data (abfd, sec)->relocs = free_internal;
    674     }
    675 
    676   return internal_relocs;
    677 
    678  error_return:
    679   free (free_external);
    680   free (free_internal);
    681   return NULL;
    682 }
    683 
    684 /* Set lineno_count for the output sections of a COFF file.  */
    685 
    686 int
    687 coff_count_linenumbers (bfd *abfd)
    688 {
    689   unsigned int limit = bfd_get_symcount (abfd);
    690   unsigned int i;
    691   int total = 0;
    692   asymbol **p;
    693   asection *s;
    694 
    695   if (limit == 0)
    696     {
    697       /* This may be from the backend linker, in which case the
    698 	 lineno_count in the sections is correct.  */
    699       for (s = abfd->sections; s != NULL; s = s->next)
    700 	total += s->lineno_count;
    701       return total;
    702     }
    703 
    704   for (s = abfd->sections; s != NULL; s = s->next)
    705     BFD_ASSERT (s->lineno_count == 0);
    706 
    707   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
    708     {
    709       asymbol *q_maybe = *p;
    710 
    711       if (bfd_asymbol_bfd (q_maybe) != NULL
    712 	  && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
    713 	{
    714 	  coff_symbol_type *q = coffsymbol (q_maybe);
    715 
    716 	  /* The AIX 4.1 compiler can sometimes generate line numbers
    717 	     attached to debugging symbols.  We try to simply ignore
    718 	     those here.  */
    719 	  if (q->lineno != NULL
    720 	      && q->symbol.section->owner != NULL)
    721 	    {
    722 	      /* This symbol has line numbers.  Increment the owning
    723 		 section's linenumber count.  */
    724 	      alent *l = q->lineno;
    725 
    726 	      do
    727 		{
    728 		  asection * sec = q->symbol.section->output_section;
    729 
    730 		  /* Do not try to update fields in read-only sections.  */
    731 		  if (! bfd_is_const_section (sec))
    732 		    sec->lineno_count ++;
    733 
    734 		  ++total;
    735 		  ++l;
    736 		}
    737 	      while (l->line_number != 0);
    738 	    }
    739 	}
    740     }
    741 
    742   return total;
    743 }
    744 
    745 static void
    746 fixup_symbol_value (bfd *abfd,
    747 		    coff_symbol_type *coff_symbol_ptr,
    748 		    struct internal_syment *syment)
    749 {
    750   /* Normalize the symbol flags.  */
    751   if (coff_symbol_ptr->symbol.section
    752       && bfd_is_com_section (coff_symbol_ptr->symbol.section))
    753     {
    754       /* A common symbol is undefined with a value.  */
    755       syment->n_scnum = N_UNDEF;
    756       syment->n_value = coff_symbol_ptr->symbol.value;
    757     }
    758   else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
    759 	   && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
    760     {
    761       syment->n_value = coff_symbol_ptr->symbol.value;
    762     }
    763   else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
    764     {
    765       syment->n_scnum = N_UNDEF;
    766       syment->n_value = 0;
    767     }
    768   /* FIXME: Do we need to handle the absolute section here?  */
    769   else
    770     {
    771       if (coff_symbol_ptr->symbol.section)
    772 	{
    773 	  syment->n_scnum =
    774 	    coff_symbol_ptr->symbol.section->output_section->target_index;
    775 
    776 	  syment->n_value = (coff_symbol_ptr->symbol.value
    777 			     + coff_symbol_ptr->symbol.section->output_offset);
    778 	  if (! obj_pe (abfd))
    779 	    {
    780 	      syment->n_value += (syment->n_sclass == C_STATLAB)
    781 		? coff_symbol_ptr->symbol.section->output_section->lma
    782 		: coff_symbol_ptr->symbol.section->output_section->vma;
    783 	    }
    784 	}
    785       else
    786 	{
    787 	  BFD_ASSERT (0);
    788 	  /* This can happen, but I don't know why yet (steve (at) cygnus.com) */
    789 	  syment->n_scnum = N_ABS;
    790 	  syment->n_value = coff_symbol_ptr->symbol.value;
    791 	}
    792     }
    793 }
    794 
    795 /* Run through all the symbols in the symbol table and work out what
    796    their indexes into the symbol table will be when output.
    797 
    798    Coff requires that each C_FILE symbol points to the next one in the
    799    chain, and that the last one points to the first external symbol. We
    800    do that here too.  */
    801 
    802 bool
    803 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
    804 {
    805   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
    806   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
    807   unsigned int native_index = 0;
    808   struct internal_syment *last_file = NULL;
    809   unsigned int symbol_index;
    810 
    811   /* COFF demands that undefined symbols come after all other symbols.
    812      Since we don't need to impose this extra knowledge on all our
    813      client programs, deal with that here.  Sort the symbol table;
    814      just move the undefined symbols to the end, leaving the rest
    815      alone.  The O'Reilly book says that defined global symbols come
    816      at the end before the undefined symbols, so we do that here as
    817      well.  */
    818   /* @@ Do we have some condition we could test for, so we don't always
    819      have to do this?  I don't think relocatability is quite right, but
    820      I'm not certain.  [raeburn:19920508.1711EST]  */
    821   {
    822     asymbol **newsyms;
    823     unsigned int i;
    824     bfd_size_type amt;
    825 
    826     amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
    827     newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
    828     if (!newsyms)
    829       return false;
    830     bfd_ptr->outsymbols = newsyms;
    831     for (i = 0; i < symbol_count; i++)
    832       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
    833 	  || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
    834 	      && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
    835 	      && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
    836 		  || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
    837 		      == 0))))
    838 	*newsyms++ = symbol_ptr_ptr[i];
    839 
    840     for (i = 0; i < symbol_count; i++)
    841       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
    842 	  && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
    843 	  && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
    844 	      || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
    845 		  && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
    846 		      != 0))))
    847 	*newsyms++ = symbol_ptr_ptr[i];
    848 
    849     *first_undef = newsyms - bfd_ptr->outsymbols;
    850 
    851     for (i = 0; i < symbol_count; i++)
    852       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
    853 	  && bfd_is_und_section (symbol_ptr_ptr[i]->section))
    854 	*newsyms++ = symbol_ptr_ptr[i];
    855     *newsyms = (asymbol *) NULL;
    856     symbol_ptr_ptr = bfd_ptr->outsymbols;
    857   }
    858 
    859   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
    860     {
    861       coff_symbol_type *coff_symbol_ptr;
    862 
    863       coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
    864       symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
    865       if (coff_symbol_ptr && coff_symbol_ptr->native)
    866 	{
    867 	  combined_entry_type *s = coff_symbol_ptr->native;
    868 	  int i;
    869 
    870 	  BFD_ASSERT (s->is_sym);
    871 	  if (s->u.syment.n_sclass == C_FILE)
    872 	    {
    873 	      if (last_file != NULL)
    874 		last_file->n_value = native_index;
    875 	      last_file = &(s->u.syment);
    876 	    }
    877 	  else
    878 	    /* Modify the symbol values according to their section and
    879 	       type.  */
    880 	    fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
    881 
    882 	  for (i = 0; i < s->u.syment.n_numaux + 1; i++)
    883 	    s[i].offset = native_index++;
    884 	}
    885       else
    886 	native_index++;
    887     }
    888 
    889   obj_conv_table_size (bfd_ptr) = native_index;
    890 
    891   return true;
    892 }
    893 
    894 /* Run thorough the symbol table again, and fix it so that all
    895    pointers to entries are changed to the entries' index in the output
    896    symbol table.  */
    897 
    898 void
    899 coff_mangle_symbols (bfd *bfd_ptr)
    900 {
    901   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
    902   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
    903   unsigned int symbol_index;
    904 
    905   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
    906     {
    907       coff_symbol_type *coff_symbol_ptr;
    908 
    909       coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
    910       if (coff_symbol_ptr && coff_symbol_ptr->native)
    911 	{
    912 	  int i;
    913 	  combined_entry_type *s = coff_symbol_ptr->native;
    914 
    915 	  BFD_ASSERT (s->is_sym);
    916 	  if (s->fix_value)
    917 	    {
    918 	      /* FIXME: We should use a union here.  */
    919 	      s->u.syment.n_value =
    920 		(uintptr_t) ((combined_entry_type *)
    921 			     (uintptr_t) s->u.syment.n_value)->offset;
    922 	      s->fix_value = 0;
    923 	    }
    924 	  if (s->fix_line)
    925 	    {
    926 	      /* The value is the offset into the line number entries
    927 		 for the symbol's section.  On output, the symbol's
    928 		 section should be N_DEBUG.  */
    929 	      s->u.syment.n_value =
    930 		(coff_symbol_ptr->symbol.section->output_section->line_filepos
    931 		 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
    932 	      coff_symbol_ptr->symbol.section =
    933 		coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
    934 	      BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
    935 	    }
    936 	  for (i = 0; i < s->u.syment.n_numaux; i++)
    937 	    {
    938 	      combined_entry_type *a = s + i + 1;
    939 
    940 	      BFD_ASSERT (! a->is_sym);
    941 	      if (a->fix_tag)
    942 		{
    943 		  a->u.auxent.x_sym.x_tagndx.u32 =
    944 		    a->u.auxent.x_sym.x_tagndx.p->offset;
    945 		  a->fix_tag = 0;
    946 		}
    947 	      if (a->fix_end)
    948 		{
    949 		  a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
    950 		    a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
    951 		  a->fix_end = 0;
    952 		}
    953 	      if (a->fix_scnlen)
    954 		{
    955 		  a->u.auxent.x_csect.x_scnlen.u64 =
    956 		    a->u.auxent.x_csect.x_scnlen.p->offset;
    957 		  a->fix_scnlen = 0;
    958 		}
    959 	    }
    960 	}
    961     }
    962 }
    963 
    964 static bool
    965 coff_write_auxent_fname (bfd *abfd,
    966 			 char *str,
    967 			 union internal_auxent *auxent,
    968 			 struct bfd_strtab_hash *strtab,
    969 			 bool hash)
    970 {
    971   unsigned int str_length = strlen (str);
    972   unsigned int filnmlen = bfd_coff_filnmlen (abfd);
    973 
    974   if (bfd_coff_long_filenames (abfd))
    975     {
    976       if (str_length <= filnmlen)
    977 	strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
    978       else
    979 	{
    980 	  bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
    981 
    982 	  if (indx == (bfd_size_type) -1)
    983 	    return false;
    984 
    985 	  auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
    986 	  auxent->x_file.x_n.x_n.x_zeroes = 0;
    987 	}
    988     }
    989   else
    990     {
    991       strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
    992       if (str_length > filnmlen)
    993 	str[filnmlen] = '\0';
    994     }
    995 
    996   return true;
    997 }
    998 
    999 static bool
   1000 coff_fix_symbol_name (bfd *abfd,
   1001 		      asymbol *symbol,
   1002 		      combined_entry_type *native,
   1003 		      struct bfd_strtab_hash *strtab,
   1004 		      bool hash,
   1005 		      asection **debug_string_section_p,
   1006 		      bfd_size_type *debug_string_size_p)
   1007 {
   1008   unsigned int name_length;
   1009   char *name = (char *) (symbol->name);
   1010   bfd_size_type indx;
   1011 
   1012   if (name == NULL)
   1013     {
   1014       /* COFF symbols always have names, so we'll make one up.  */
   1015       symbol->name = "strange";
   1016       name = (char *) symbol->name;
   1017     }
   1018   name_length = strlen (name);
   1019 
   1020   BFD_ASSERT (native->is_sym);
   1021   if (native->u.syment.n_sclass == C_FILE
   1022       && native->u.syment.n_numaux > 0)
   1023     {
   1024       if (bfd_coff_force_symnames_in_strings (abfd))
   1025 	{
   1026 	  indx = _bfd_stringtab_add (strtab, ".file", hash, false);
   1027 	  if (indx == (bfd_size_type) -1)
   1028 	    return false;
   1029 
   1030 	  native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
   1031 	  native->u.syment._n._n_n._n_zeroes = 0;
   1032 	}
   1033       else
   1034 	strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
   1035 
   1036       BFD_ASSERT (! (native + 1)->is_sym);
   1037       if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
   1038 			       strtab, hash))
   1039 	return false;
   1040     }
   1041   else
   1042     {
   1043       if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
   1044 	/* This name will fit into the symbol neatly.  */
   1045 	strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
   1046 
   1047       else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
   1048 	{
   1049 	  indx = _bfd_stringtab_add (strtab, name, hash, false);
   1050 	  if (indx == (bfd_size_type) -1)
   1051 	    return false;
   1052 
   1053 	  native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
   1054 	  native->u.syment._n._n_n._n_zeroes = 0;
   1055 	}
   1056       else
   1057 	{
   1058 	  file_ptr filepos;
   1059 	  bfd_byte buf[4];
   1060 	  int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
   1061 
   1062 	  /* This name should be written into the .debug section.  For
   1063 	     some reason each name is preceded by a two byte length
   1064 	     and also followed by a null byte.  FIXME: We assume that
   1065 	     the .debug section has already been created, and that it
   1066 	     is large enough.  */
   1067 	  if (*debug_string_section_p == (asection *) NULL)
   1068 	    *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
   1069 	  filepos = bfd_tell (abfd);
   1070 	  if (prefix_len == 4)
   1071 	    bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
   1072 	  else
   1073 	    bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
   1074 
   1075 	  if (!bfd_set_section_contents (abfd,
   1076 					 *debug_string_section_p,
   1077 					 (void *) buf,
   1078 					 (file_ptr) *debug_string_size_p,
   1079 					 (bfd_size_type) prefix_len)
   1080 	      || !bfd_set_section_contents (abfd,
   1081 					    *debug_string_section_p,
   1082 					    (void *) symbol->name,
   1083 					    (file_ptr) (*debug_string_size_p
   1084 							+ prefix_len),
   1085 					    (bfd_size_type) name_length + 1))
   1086 	    abort ();
   1087 	  if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
   1088 	    abort ();
   1089 	  native->u.syment._n._n_n._n_offset =
   1090 	      *debug_string_size_p + prefix_len;
   1091 	  native->u.syment._n._n_n._n_zeroes = 0;
   1092 	  *debug_string_size_p += name_length + 1 + prefix_len;
   1093 	}
   1094     }
   1095 
   1096   return true;
   1097 }
   1098 
   1099 /* We need to keep track of the symbol index so that when we write out
   1100    the relocs we can get the index for a symbol.  This method is a
   1101    hack.  FIXME.  */
   1102 
   1103 #define set_index(symbol, idx)	((symbol)->udata.i = (idx))
   1104 
   1105 /* Write a symbol out to a COFF file.  */
   1106 
   1107 static bool
   1108 coff_write_symbol (bfd *abfd,
   1109 		   asymbol *symbol,
   1110 		   combined_entry_type *native,
   1111 		   bfd_vma *written,
   1112 		   struct bfd_strtab_hash *strtab,
   1113 		   bool hash,
   1114 		   asection **debug_string_section_p,
   1115 		   bfd_size_type *debug_string_size_p)
   1116 {
   1117   unsigned int numaux = native->u.syment.n_numaux;
   1118   int type = native->u.syment.n_type;
   1119   int n_sclass = (int) native->u.syment.n_sclass;
   1120   asection *output_section = symbol->section->output_section
   1121 			       ? symbol->section->output_section
   1122 			       : symbol->section;
   1123   void * buf;
   1124   bfd_size_type symesz;
   1125 
   1126   BFD_ASSERT (native->is_sym);
   1127 
   1128   if (native->u.syment.n_sclass == C_FILE)
   1129     symbol->flags |= BSF_DEBUGGING;
   1130 
   1131   if (symbol->flags & BSF_DEBUGGING
   1132       && bfd_is_abs_section (symbol->section))
   1133     native->u.syment.n_scnum = N_DEBUG;
   1134 
   1135   else if (bfd_is_abs_section (symbol->section))
   1136     native->u.syment.n_scnum = N_ABS;
   1137 
   1138   else if (bfd_is_und_section (symbol->section))
   1139     native->u.syment.n_scnum = N_UNDEF;
   1140 
   1141   else
   1142     native->u.syment.n_scnum =
   1143       output_section->target_index;
   1144 
   1145   if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
   1146 			     debug_string_section_p, debug_string_size_p))
   1147     return false;
   1148 
   1149   symesz = bfd_coff_symesz (abfd);
   1150   buf = bfd_alloc (abfd, symesz);
   1151   if (!buf)
   1152     return false;
   1153   bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
   1154   if (bfd_write (buf, symesz, abfd) != symesz)
   1155     return false;
   1156   bfd_release (abfd, buf);
   1157 
   1158   if (native->u.syment.n_numaux > 0)
   1159     {
   1160       bfd_size_type auxesz;
   1161       unsigned int j;
   1162 
   1163       auxesz = bfd_coff_auxesz (abfd);
   1164       buf = bfd_alloc (abfd, auxesz);
   1165       if (!buf)
   1166 	return false;
   1167       for (j = 0; j < native->u.syment.n_numaux; j++)
   1168 	{
   1169 	  BFD_ASSERT (! (native + j + 1)->is_sym);
   1170 
   1171 	  /* Adjust auxent only if this isn't the filename
   1172 	     auxiliary entry.  */
   1173 	  if (native->u.syment.n_sclass == C_FILE
   1174 	      && (native + j + 1)->u.auxent.x_file.x_ftype
   1175 	      && (native + j + 1)->extrap)
   1176 	    coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap,
   1177 				     &(native + j + 1)->u.auxent, strtab, hash);
   1178 
   1179 	  bfd_coff_swap_aux_out (abfd,
   1180 				 &((native + j + 1)->u.auxent),
   1181 				 type, n_sclass, (int) j,
   1182 				 native->u.syment.n_numaux,
   1183 				 buf);
   1184 	  if (bfd_write (buf, auxesz, abfd) != auxesz)
   1185 	    return false;
   1186 	}
   1187       bfd_release (abfd, buf);
   1188     }
   1189 
   1190   /* Store the index for use when we write out the relocs.  */
   1191   set_index (symbol, *written);
   1192 
   1193   *written += numaux + 1;
   1194   return true;
   1195 }
   1196 
   1197 /* Write out a symbol to a COFF file that does not come from a COFF
   1198    file originally.  This symbol may have been created by the linker,
   1199    or we may be linking a non COFF file to a COFF file.  */
   1200 
   1201 bool
   1202 coff_write_alien_symbol (bfd *abfd,
   1203 			 asymbol *symbol,
   1204 			 struct internal_syment *isym,
   1205 			 bfd_vma *written,
   1206 			 struct bfd_strtab_hash *strtab,
   1207 			 bool hash,
   1208 			 asection **debug_string_section_p,
   1209 			 bfd_size_type *debug_string_size_p)
   1210 {
   1211   combined_entry_type *native;
   1212   combined_entry_type dummy[2];
   1213   asection *output_section = symbol->section->output_section
   1214 			       ? symbol->section->output_section
   1215 			       : symbol->section;
   1216   struct bfd_link_info *link_info = coff_data (abfd)->link_info;
   1217   bool ret;
   1218 
   1219   if ((!link_info || link_info->strip_discarded)
   1220       && !bfd_is_abs_section (symbol->section)
   1221       && symbol->section->output_section == bfd_abs_section_ptr)
   1222     {
   1223       symbol->name = "";
   1224       if (isym != NULL)
   1225 	memset (isym, 0, sizeof (*isym));
   1226       return true;
   1227     }
   1228   memset (dummy, 0, sizeof dummy);
   1229   native = dummy;
   1230   native->is_sym = true;
   1231   native[1].is_sym = false;
   1232   native->u.syment.n_type = T_NULL;
   1233   native->u.syment.n_flags = 0;
   1234   native->u.syment.n_numaux = 0;
   1235   if (bfd_is_und_section (symbol->section))
   1236     {
   1237       native->u.syment.n_scnum = N_UNDEF;
   1238       native->u.syment.n_value = symbol->value;
   1239     }
   1240   else if (bfd_is_com_section (symbol->section))
   1241     {
   1242       native->u.syment.n_scnum = N_UNDEF;
   1243       native->u.syment.n_value = symbol->value;
   1244     }
   1245   else if (symbol->flags & BSF_FILE)
   1246     {
   1247       native->u.syment.n_scnum = N_DEBUG;
   1248       native->u.syment.n_numaux = 1;
   1249     }
   1250   else if (symbol->flags & BSF_DEBUGGING)
   1251     {
   1252       /* There isn't much point to writing out a debugging symbol
   1253 	 unless we are prepared to convert it into COFF debugging
   1254 	 format.  So, we just ignore them.  We must clobber the symbol
   1255 	 name to keep it from being put in the string table.  */
   1256       symbol->name = "";
   1257       if (isym != NULL)
   1258 	memset (isym, 0, sizeof (*isym));
   1259       return true;
   1260     }
   1261   else
   1262     {
   1263       native->u.syment.n_scnum = output_section->target_index;
   1264       native->u.syment.n_value = (symbol->value
   1265 				  + symbol->section->output_offset);
   1266       if (! obj_pe (abfd))
   1267 	native->u.syment.n_value += output_section->vma;
   1268 
   1269       /* Copy the any flags from the file header into the symbol.
   1270 	 FIXME: Why?  */
   1271       {
   1272 	coff_symbol_type *c = coff_symbol_from (symbol);
   1273 	if (c != (coff_symbol_type *) NULL)
   1274 	  native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
   1275       }
   1276     }
   1277 
   1278   native->u.syment.n_type = 0;
   1279   if (symbol->flags & BSF_FILE)
   1280     native->u.syment.n_sclass = C_FILE;
   1281   else if (symbol->flags & BSF_LOCAL)
   1282     native->u.syment.n_sclass = C_STAT;
   1283   else if (symbol->flags & BSF_WEAK)
   1284     native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
   1285   else
   1286     native->u.syment.n_sclass = C_EXT;
   1287 
   1288   ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash,
   1289 			   debug_string_section_p, debug_string_size_p);
   1290   if (isym != NULL)
   1291     *isym = native->u.syment;
   1292   return ret;
   1293 }
   1294 
   1295 /* Write a native symbol to a COFF file.  */
   1296 
   1297 static bool
   1298 coff_write_native_symbol (bfd *abfd,
   1299 			  coff_symbol_type *symbol,
   1300 			  bfd_vma *written,
   1301 			  struct bfd_strtab_hash *strtab,
   1302 			  asection **debug_string_section_p,
   1303 			  bfd_size_type *debug_string_size_p)
   1304 {
   1305   combined_entry_type *native = symbol->native;
   1306   alent *lineno = symbol->lineno;
   1307   struct bfd_link_info *link_info = coff_data (abfd)->link_info;
   1308 
   1309   if ((!link_info || link_info->strip_discarded)
   1310       && !bfd_is_abs_section (symbol->symbol.section)
   1311       && symbol->symbol.section->output_section == bfd_abs_section_ptr)
   1312     {
   1313       symbol->symbol.name = "";
   1314       return true;
   1315     }
   1316 
   1317   BFD_ASSERT (native->is_sym);
   1318   /* If this symbol has an associated line number, we must store the
   1319      symbol index in the line number field.  We also tag the auxent to
   1320      point to the right place in the lineno table.  */
   1321   if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
   1322     {
   1323       unsigned int count = 0;
   1324 
   1325       lineno[count].u.offset = *written;
   1326       if (native->u.syment.n_numaux)
   1327 	{
   1328 	  union internal_auxent *a = &((native + 1)->u.auxent);
   1329 
   1330 	  a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
   1331 	    symbol->symbol.section->output_section->moving_line_filepos;
   1332 	}
   1333 
   1334       /* Count and relocate all other linenumbers.  */
   1335       count++;
   1336       while (lineno[count].line_number != 0)
   1337 	{
   1338 	  lineno[count].u.offset +=
   1339 	    (symbol->symbol.section->output_section->vma
   1340 	     + symbol->symbol.section->output_offset);
   1341 	  count++;
   1342 	}
   1343       symbol->done_lineno = true;
   1344 
   1345       if (! bfd_is_const_section (symbol->symbol.section->output_section))
   1346 	symbol->symbol.section->output_section->moving_line_filepos +=
   1347 	  count * bfd_coff_linesz (abfd);
   1348     }
   1349 
   1350   return coff_write_symbol (abfd, &(symbol->symbol), native, written,
   1351 			    strtab, true, debug_string_section_p,
   1352 			    debug_string_size_p);
   1353 }
   1354 
   1355 static void
   1356 null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
   1357 		    va_list ap ATTRIBUTE_UNUSED)
   1358 {
   1359 }
   1360 
   1361 /* Write out the COFF symbols.  */
   1362 
   1363 bool
   1364 coff_write_symbols (bfd *abfd)
   1365 {
   1366   struct bfd_strtab_hash *strtab;
   1367   asection *debug_string_section;
   1368   bfd_size_type debug_string_size;
   1369   unsigned int i;
   1370   unsigned int limit = bfd_get_symcount (abfd);
   1371   bfd_vma written = 0;
   1372   asymbol **p;
   1373 
   1374   debug_string_section = NULL;
   1375   debug_string_size = 0;
   1376 
   1377   strtab = _bfd_stringtab_init ();
   1378   if (strtab == NULL)
   1379     return false;
   1380 
   1381   /* If this target supports long section names, they must be put into
   1382      the string table.  This is supported by PE.  This code must
   1383      handle section names just as they are handled in
   1384      coff_write_object_contents.  This is why we pass hash as FALSE below.  */
   1385   if (bfd_coff_long_section_names (abfd))
   1386     {
   1387       asection *o;
   1388 
   1389       for (o = abfd->sections; o != NULL; o = o->next)
   1390 	if (strlen (o->name) > SCNNMLEN
   1391 	    && _bfd_stringtab_add (strtab, o->name, false, false)
   1392 	       == (bfd_size_type) -1)
   1393 	  return false;
   1394     }
   1395 
   1396   /* Seek to the right place.  */
   1397   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
   1398     return false;
   1399 
   1400   /* Output all the symbols we have.  */
   1401   written = 0;
   1402   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
   1403     {
   1404       asymbol *symbol = *p;
   1405       coff_symbol_type *c_symbol = coff_symbol_from (symbol);
   1406 
   1407       if (c_symbol == (coff_symbol_type *) NULL
   1408 	  || c_symbol->native == (combined_entry_type *) NULL)
   1409 	{
   1410 	  if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
   1411 					strtab, true, &debug_string_section,
   1412 					&debug_string_size))
   1413 	    return false;
   1414 	}
   1415       else
   1416 	{
   1417 	  if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
   1418 	    {
   1419 	      bfd_error_handler_type current_error_handler;
   1420 	      enum coff_symbol_classification sym_class;
   1421 	      unsigned char *n_sclass;
   1422 
   1423 	      /* Suppress error reporting by bfd_coff_classify_symbol.
   1424 		 Error messages can be generated when we are processing a local
   1425 		 symbol which has no associated section and we do not have to
   1426 		 worry about this, all we need to know is that it is local.  */
   1427 	      current_error_handler = bfd_set_error_handler (null_error_handler);
   1428 	      BFD_ASSERT (c_symbol->native->is_sym);
   1429 	      sym_class = bfd_coff_classify_symbol (abfd,
   1430 						    &c_symbol->native->u.syment);
   1431 	      (void) bfd_set_error_handler (current_error_handler);
   1432 
   1433 	      n_sclass = &c_symbol->native->u.syment.n_sclass;
   1434 
   1435 	      /* If the symbol class has been changed (eg objcopy/ld script/etc)
   1436 		 we cannot retain the existing sclass from the original symbol.
   1437 		 Weak symbols only have one valid sclass, so just set it always.
   1438 		 If it is not local class and should be, set it C_STAT.
   1439 		 If it is global and not classified as global, or if it is
   1440 		 weak (which is also classified as global), set it C_EXT.  */
   1441 
   1442 	      if (symbol->flags & BSF_WEAK)
   1443 		*n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
   1444 	      else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
   1445 		*n_sclass = C_STAT;
   1446 	      else if (symbol->flags & BSF_GLOBAL
   1447 		       && (sym_class != COFF_SYMBOL_GLOBAL
   1448 #ifdef COFF_WITH_PE
   1449 			   || *n_sclass == C_NT_WEAK
   1450 #endif
   1451 			   || *n_sclass == C_WEAKEXT))
   1452 		c_symbol->native->u.syment.n_sclass = C_EXT;
   1453 	    }
   1454 
   1455 	  if (!coff_write_native_symbol (abfd, c_symbol, &written,
   1456 					 strtab, &debug_string_section,
   1457 					 &debug_string_size))
   1458 	    return false;
   1459 	}
   1460     }
   1461 
   1462   obj_raw_syment_count (abfd) = written;
   1463 
   1464   /* Now write out strings.
   1465 
   1466      We would normally not write anything here if there are no strings, but
   1467      we'll write out 4 so that any stupid coff reader which tries to read the
   1468      string table even when there isn't one won't croak.  */
   1469   {
   1470     bfd_byte buffer[STRING_SIZE_SIZE];
   1471 
   1472 #if STRING_SIZE_SIZE == 4
   1473     H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
   1474 #else
   1475  #error Change H_PUT_32
   1476 #endif
   1477     if (bfd_write (buffer, sizeof (buffer), abfd) != sizeof (buffer))
   1478       return false;
   1479 
   1480     if (! _bfd_stringtab_emit (abfd, strtab))
   1481       return false;
   1482   }
   1483 
   1484   _bfd_stringtab_free (strtab);
   1485 
   1486   /* Make sure the .debug section was created to be the correct size.
   1487      We should create it ourselves on the fly, but we don't because
   1488      BFD won't let us write to any section until we know how large all
   1489      the sections are.  We could still do it by making another pass
   1490      over the symbols.  FIXME.  */
   1491   BFD_ASSERT (debug_string_size == 0
   1492 	      || (debug_string_section != (asection *) NULL
   1493 		  && (BFD_ALIGN (debug_string_size,
   1494 				 1 << debug_string_section->alignment_power)
   1495 		      == debug_string_section->size)));
   1496 
   1497   return true;
   1498 }
   1499 
   1500 bool
   1501 coff_write_linenumbers (bfd *abfd)
   1502 {
   1503   asection *s;
   1504   bfd_size_type linesz;
   1505   void * buff;
   1506 
   1507   linesz = bfd_coff_linesz (abfd);
   1508   buff = bfd_alloc (abfd, linesz);
   1509   if (!buff)
   1510     return false;
   1511   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
   1512     {
   1513       if (s->lineno_count)
   1514 	{
   1515 	  asymbol **q = abfd->outsymbols;
   1516 	  if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
   1517 	    return false;
   1518 	  /* Find all the linenumbers in this section.  */
   1519 	  while (*q)
   1520 	    {
   1521 	      asymbol *p = *q;
   1522 	      if (p->section->output_section == s)
   1523 		{
   1524 		  alent *l =
   1525 		  BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
   1526 			    (bfd_asymbol_bfd (p), p));
   1527 		  if (l)
   1528 		    {
   1529 		      /* Found a linenumber entry, output.  */
   1530 		      struct internal_lineno out;
   1531 
   1532 		      memset ((void *) & out, 0, sizeof (out));
   1533 		      out.l_lnno = 0;
   1534 		      out.l_addr.l_symndx = l->u.offset;
   1535 		      bfd_coff_swap_lineno_out (abfd, &out, buff);
   1536 		      if (bfd_write (buff, linesz, abfd) != linesz)
   1537 			return false;
   1538 		      l++;
   1539 		      while (l->line_number)
   1540 			{
   1541 			  out.l_lnno = l->line_number;
   1542 			  out.l_addr.l_symndx = l->u.offset;
   1543 			  bfd_coff_swap_lineno_out (abfd, &out, buff);
   1544 			  if (bfd_write (buff, linesz, abfd) != linesz)
   1545 			    return false;
   1546 			  l++;
   1547 			}
   1548 		    }
   1549 		}
   1550 	      q++;
   1551 	    }
   1552 	}
   1553     }
   1554   bfd_release (abfd, buff);
   1555   return true;
   1556 }
   1557 
   1558 alent *
   1559 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
   1560 {
   1561   return coffsymbol (symbol)->lineno;
   1562 }
   1563 
   1564 /* This function transforms the offsets into the symbol table into
   1565    pointers to syments.  */
   1566 
   1567 static void
   1568 coff_pointerize_aux (bfd *abfd,
   1569 		     combined_entry_type *table_base,
   1570 		     combined_entry_type *symbol,
   1571 		     unsigned int indaux,
   1572 		     combined_entry_type *auxent)
   1573 {
   1574   unsigned int type = symbol->u.syment.n_type;
   1575   unsigned int n_sclass = symbol->u.syment.n_sclass;
   1576 
   1577   BFD_ASSERT (symbol->is_sym);
   1578   if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
   1579     {
   1580       if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
   1581 	  (abfd, table_base, symbol, indaux, auxent))
   1582 	return;
   1583     }
   1584 
   1585   /* Don't bother if this is a file or a section.  */
   1586   if (n_sclass == C_STAT && type == T_NULL)
   1587     return;
   1588   if (n_sclass == C_FILE)
   1589     return;
   1590   if (n_sclass == C_DWARF)
   1591     return;
   1592 
   1593   BFD_ASSERT (! auxent->is_sym);
   1594   /* Otherwise patch up.  */
   1595 #define N_TMASK coff_data  (abfd)->local_n_tmask
   1596 #define N_BTSHFT coff_data (abfd)->local_n_btshft
   1597 
   1598   if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
   1599        || n_sclass == C_FCN)
   1600       && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 > 0
   1601       && (auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32
   1602 	  < obj_raw_syment_count (abfd)))
   1603     {
   1604       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
   1605 	table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
   1606       auxent->fix_end = 1;
   1607     }
   1608 
   1609   /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
   1610      generate one, so we must be careful to ignore it.  */
   1611   if (auxent->u.auxent.x_sym.x_tagndx.u32 < obj_raw_syment_count (abfd))
   1612     {
   1613       auxent->u.auxent.x_sym.x_tagndx.p =
   1614 	table_base + auxent->u.auxent.x_sym.x_tagndx.u32;
   1615       auxent->fix_tag = 1;
   1616     }
   1617 }
   1618 
   1619 /* Allocate space for the ".debug" section, and read it.
   1620    We did not read the debug section until now, because
   1621    we didn't want to go to the trouble until someone needed it.  */
   1622 
   1623 static char *
   1624 build_debug_section (bfd *abfd, asection ** sect_return)
   1625 {
   1626   char *debug_section;
   1627   file_ptr position;
   1628   bfd_size_type sec_size;
   1629 
   1630   asection *sect = bfd_get_section_by_name (abfd, ".debug");
   1631 
   1632   if (!sect)
   1633     {
   1634       bfd_set_error (bfd_error_no_debug_section);
   1635       return NULL;
   1636     }
   1637 
   1638   /* Seek to the beginning of the `.debug' section and read it.
   1639      Save the current position first; it is needed by our caller.
   1640      Then read debug section and reset the file pointer.  */
   1641 
   1642   position = bfd_tell (abfd);
   1643   if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
   1644     return NULL;
   1645 
   1646   sec_size = sect->size;
   1647   debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size + 1, sec_size);
   1648   if (debug_section == NULL)
   1649     return NULL;
   1650   debug_section[sec_size] = 0;
   1651 
   1652   if (bfd_seek (abfd, position, SEEK_SET) != 0)
   1653     return NULL;
   1654 
   1655   * sect_return = sect;
   1656   return debug_section;
   1657 }
   1658 
   1659 /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
   1660    \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
   1661    be \0-terminated.  */
   1662 
   1663 static char *
   1664 copy_name (bfd *abfd, char *name, size_t maxlen)
   1665 {
   1666   size_t len;
   1667   char *newname;
   1668 
   1669   for (len = 0; len < maxlen; ++len)
   1670     if (name[len] == '\0')
   1671       break;
   1672 
   1673   if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
   1674     return NULL;
   1675 
   1676   strncpy (newname, name, len);
   1677   newname[len] = '\0';
   1678   return newname;
   1679 }
   1680 
   1681 /* Read in the external symbols.  */
   1682 
   1683 bool
   1684 _bfd_coff_get_external_symbols (bfd *abfd)
   1685 {
   1686   size_t symesz;
   1687   size_t size;
   1688   void * syms;
   1689   ufile_ptr filesize;
   1690 
   1691   if (obj_coff_external_syms (abfd) != NULL)
   1692     return true;
   1693 
   1694   symesz = bfd_coff_symesz (abfd);
   1695   if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
   1696     {
   1697       bfd_set_error (bfd_error_file_truncated);
   1698       return false;
   1699     }
   1700 
   1701   if (size == 0)
   1702     return true;
   1703 
   1704   filesize = bfd_get_file_size (abfd);
   1705   if (filesize != 0
   1706       && ((ufile_ptr) obj_sym_filepos (abfd) > filesize
   1707 	  || size > filesize - obj_sym_filepos (abfd)))
   1708     {
   1709       bfd_set_error (bfd_error_file_truncated);
   1710       return false;
   1711     }
   1712 
   1713   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
   1714     return false;
   1715   syms = _bfd_malloc_and_read (abfd, size, size);
   1716   obj_coff_external_syms (abfd) = syms;
   1717   return syms != NULL;
   1718 }
   1719 
   1720 /* Read in the external strings.  The strings are not loaded until
   1721    they are needed.  This is because we have no simple way of
   1722    detecting a missing string table in an archive.  If the strings
   1723    are loaded then the STRINGS and STRINGS_LEN fields in the
   1724    coff_tdata structure will be set.  */
   1725 
   1726 const char *
   1727 _bfd_coff_read_string_table (bfd *abfd)
   1728 {
   1729   char extstrsize[STRING_SIZE_SIZE];
   1730   bfd_size_type strsize;
   1731   char *strings;
   1732   ufile_ptr pos;
   1733   ufile_ptr filesize;
   1734   size_t symesz;
   1735   size_t size;
   1736 
   1737   if (obj_coff_strings (abfd) != NULL)
   1738     return obj_coff_strings (abfd);
   1739 
   1740   if (obj_sym_filepos (abfd) == 0)
   1741     {
   1742       bfd_set_error (bfd_error_no_symbols);
   1743       return NULL;
   1744     }
   1745 
   1746   symesz = bfd_coff_symesz (abfd);
   1747   pos = obj_sym_filepos (abfd);
   1748   if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
   1749       || pos + size < pos)
   1750     {
   1751       bfd_set_error (bfd_error_file_truncated);
   1752       return NULL;
   1753     }
   1754 
   1755   if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
   1756     return NULL;
   1757 
   1758   if (bfd_read (extstrsize, sizeof extstrsize, abfd) != sizeof extstrsize)
   1759     {
   1760       if (bfd_get_error () != bfd_error_file_truncated)
   1761 	return NULL;
   1762 
   1763       /* There is no string table.  */
   1764       strsize = STRING_SIZE_SIZE;
   1765     }
   1766   else
   1767     {
   1768 #if STRING_SIZE_SIZE == 4
   1769       strsize = H_GET_32 (abfd, extstrsize);
   1770 #else
   1771  #error Change H_GET_32
   1772 #endif
   1773     }
   1774 
   1775   filesize = bfd_get_file_size (abfd);
   1776   if (strsize < STRING_SIZE_SIZE
   1777       || (filesize != 0 && strsize > filesize))
   1778     {
   1779       _bfd_error_handler
   1780 	/* xgettext: c-format */
   1781 	(_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
   1782       bfd_set_error (bfd_error_bad_value);
   1783       return NULL;
   1784     }
   1785 
   1786   strings = (char *) bfd_malloc (strsize + 1);
   1787   if (strings == NULL)
   1788     return NULL;
   1789 
   1790   /* PR 17521 file: 079-54929-0.004.
   1791      A corrupt file could contain an index that points into the first
   1792      STRING_SIZE_SIZE bytes of the string table, so make sure that
   1793      they are zero.  */
   1794   memset (strings, 0, STRING_SIZE_SIZE);
   1795 
   1796   if (bfd_read (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
   1797       != strsize - STRING_SIZE_SIZE)
   1798     {
   1799       free (strings);
   1800       return NULL;
   1801     }
   1802 
   1803   obj_coff_strings (abfd) = strings;
   1804   obj_coff_strings_len (abfd) = strsize;
   1805   /* Terminate the string table, just in case.  */
   1806   strings[strsize] = 0;
   1807   return strings;
   1808 }
   1809 
   1810 /* Free up the external symbols and strings read from a COFF file.  */
   1811 
   1812 bool
   1813 _bfd_coff_free_symbols (bfd *abfd)
   1814 {
   1815   if (! bfd_family_coff (abfd))
   1816     return false;
   1817 
   1818   if (obj_coff_external_syms (abfd) != NULL
   1819       && ! obj_coff_keep_syms (abfd))
   1820     {
   1821       free (obj_coff_external_syms (abfd));
   1822       obj_coff_external_syms (abfd) = NULL;
   1823     }
   1824 
   1825   if (obj_coff_strings (abfd) != NULL
   1826       && ! obj_coff_keep_strings (abfd))
   1827     {
   1828       free (obj_coff_strings (abfd));
   1829       obj_coff_strings (abfd) = NULL;
   1830       obj_coff_strings_len (abfd) = 0;
   1831     }
   1832 
   1833   return true;
   1834 }
   1835 
   1836 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
   1837    knit the symbol names into a normalized form.  By normalized here I
   1838    mean that all symbols have an n_offset pointer that points to a null-
   1839    terminated string.  */
   1840 
   1841 combined_entry_type *
   1842 coff_get_normalized_symtab (bfd *abfd)
   1843 {
   1844   combined_entry_type *internal;
   1845   combined_entry_type *internal_ptr;
   1846   size_t symesz;
   1847   char *raw_src;
   1848   char *raw_end;
   1849   const char *string_table = NULL;
   1850   asection * debug_sec = NULL;
   1851   char *debug_sec_data = NULL;
   1852   bfd_size_type size;
   1853 
   1854   if (obj_raw_syments (abfd) != NULL)
   1855     return obj_raw_syments (abfd);
   1856 
   1857   if (! _bfd_coff_get_external_symbols (abfd))
   1858     return NULL;
   1859 
   1860   size = obj_raw_syment_count (abfd);
   1861   /* Check for integer overflow.  */
   1862   if (size > (bfd_size_type) -1 / sizeof (combined_entry_type))
   1863     return NULL;
   1864   size *= sizeof (combined_entry_type);
   1865   internal = (combined_entry_type *) bfd_zalloc (abfd, size);
   1866   if (internal == NULL && size != 0)
   1867     return NULL;
   1868 
   1869   raw_src = (char *) obj_coff_external_syms (abfd);
   1870 
   1871   /* Mark the end of the symbols.  */
   1872   symesz = bfd_coff_symesz (abfd);
   1873   raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz);
   1874 
   1875   /* FIXME SOMEDAY.  A string table size of zero is very weird, but
   1876      probably possible.  If one shows up, it will probably kill us.  */
   1877 
   1878   /* Swap all the raw entries.  */
   1879   for (internal_ptr = internal;
   1880        raw_src < raw_end;
   1881        raw_src += symesz, internal_ptr++)
   1882     {
   1883       unsigned int i;
   1884 
   1885       bfd_coff_swap_sym_in (abfd, (void *) raw_src,
   1886 			    (void *) & internal_ptr->u.syment);
   1887       internal_ptr->is_sym = true;
   1888       combined_entry_type *sym = internal_ptr;
   1889 
   1890       /* PR 17512: Prevent buffer overrun.  */
   1891       if (sym->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
   1892 	return NULL;
   1893 
   1894       for (i = 0; i < sym->u.syment.n_numaux; i++)
   1895 	{
   1896 	  internal_ptr++;
   1897 	  raw_src += symesz;
   1898 
   1899 	  bfd_coff_swap_aux_in (abfd, (void *) raw_src,
   1900 				sym->u.syment.n_type,
   1901 				sym->u.syment.n_sclass,
   1902 				(int) i, sym->u.syment.n_numaux,
   1903 				&(internal_ptr->u.auxent));
   1904 
   1905 	  internal_ptr->is_sym = false;
   1906 	  coff_pointerize_aux (abfd, internal, sym, i, internal_ptr);
   1907 	}
   1908 
   1909       if (sym->u.syment.n_sclass == C_FILE
   1910 	  && sym->u.syment.n_numaux > 0)
   1911 	{
   1912 	  combined_entry_type * aux = sym + 1;
   1913 
   1914 	  /* Make a file symbol point to the name in the auxent, since
   1915 	     the text ".file" is redundant.  */
   1916 	  BFD_ASSERT (! aux->is_sym);
   1917 
   1918 	  if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
   1919 	    {
   1920 	      /* The filename is a long one, point into the string table.  */
   1921 	      if (string_table == NULL)
   1922 		{
   1923 		  string_table = _bfd_coff_read_string_table (abfd);
   1924 		  if (string_table == NULL)
   1925 		    return NULL;
   1926 		}
   1927 
   1928 	      if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
   1929 		  >= obj_coff_strings_len (abfd))
   1930 		sym->u.syment._n._n_n._n_offset =
   1931 		  (uintptr_t) _("<corrupt>");
   1932 	      else
   1933 		sym->u.syment._n._n_n._n_offset =
   1934 		  (uintptr_t) (string_table
   1935 			       + aux->u.auxent.x_file.x_n.x_n.x_offset);
   1936 	    }
   1937 	  else
   1938 	    {
   1939 	      /* Ordinary short filename, put into memory anyway.  The
   1940 		 Microsoft PE tools sometimes store a filename in
   1941 		 multiple AUX entries.  */
   1942 	      size_t len;
   1943 	      char *src;
   1944 	      if (sym->u.syment.n_numaux > 1 && obj_pe (abfd))
   1945 		{
   1946 		  len = sym->u.syment.n_numaux * symesz;
   1947 		  src = raw_src - (len - symesz);
   1948 		}
   1949 	      else
   1950 		{
   1951 		  len = bfd_coff_filnmlen (abfd);
   1952 		  src = aux->u.auxent.x_file.x_n.x_fname;
   1953 		}
   1954 	      sym->u.syment._n._n_n._n_offset =
   1955 		(uintptr_t) copy_name (abfd, src, len);
   1956 	    }
   1957 
   1958 	  /* Normalize other strings available in C_FILE aux entries.  */
   1959 	  if (!obj_pe (abfd))
   1960 	    for (int numaux = 1;
   1961 		 numaux < sym->u.syment.n_numaux;
   1962 		 numaux++)
   1963 	      {
   1964 		aux = sym + numaux + 1;
   1965 		BFD_ASSERT (! aux->is_sym);
   1966 
   1967 		if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
   1968 		  {
   1969 		    /* The string information is a long one, point
   1970 		       into the string table.  */
   1971 		    if (string_table == NULL)
   1972 		      {
   1973 			string_table = _bfd_coff_read_string_table (abfd);
   1974 			if (string_table == NULL)
   1975 			  return NULL;
   1976 		      }
   1977 
   1978 		    if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
   1979 			>= obj_coff_strings_len (abfd))
   1980 		      aux->u.auxent.x_file.x_n.x_n.x_offset =
   1981 			(uintptr_t) _("<corrupt>");
   1982 		    else
   1983 		      aux->u.auxent.x_file.x_n.x_n.x_offset =
   1984 			(uintptr_t) (string_table
   1985 				     + aux->u.auxent.x_file.x_n.x_n.x_offset);
   1986 		  }
   1987 		else
   1988 		  aux->u.auxent.x_file.x_n.x_n.x_offset =
   1989 		    ((uintptr_t)
   1990 		     copy_name (abfd,
   1991 				aux->u.auxent.x_file.x_n.x_fname,
   1992 				bfd_coff_filnmlen (abfd)));
   1993 	      }
   1994 
   1995 	}
   1996       else
   1997 	{
   1998 	  if (sym->u.syment._n._n_n._n_zeroes != 0)
   1999 	    {
   2000 	      /* This is a "short" name.  Make it long.  */
   2001 	      char *newstring;
   2002 
   2003 	      /* Find the length of this string without walking into memory
   2004 		 that isn't ours.  */
   2005 	      for (i = 0; i < SYMNMLEN; ++i)
   2006 		if (sym->u.syment._n._n_name[i] == '\0')
   2007 		  break;
   2008 
   2009 	      newstring = bfd_alloc (abfd, i + 1);
   2010 	      if (newstring == NULL)
   2011 		return NULL;
   2012 	      memcpy (newstring, sym->u.syment._n._n_name, i);
   2013 	      newstring[i] = 0;
   2014 	      sym->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
   2015 	      sym->u.syment._n._n_n._n_zeroes = 0;
   2016 	    }
   2017 	  else if (sym->u.syment._n._n_n._n_offset == 0)
   2018 	    sym->u.syment._n._n_n._n_offset = (uintptr_t) "";
   2019 	  else if (!bfd_coff_symname_in_debug (abfd, &sym->u.syment))
   2020 	    {
   2021 	      /* Long name already.  Point symbol at the string in the
   2022 		 table.  */
   2023 	      if (string_table == NULL)
   2024 		{
   2025 		  string_table = _bfd_coff_read_string_table (abfd);
   2026 		  if (string_table == NULL)
   2027 		    return NULL;
   2028 		}
   2029 	      if (sym->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd))
   2030 		sym->u.syment._n._n_n._n_offset =
   2031 		  (uintptr_t) _("<corrupt>");
   2032 	      else
   2033 		sym->u.syment._n._n_n._n_offset =
   2034 		  (uintptr_t) (string_table
   2035 			       + sym->u.syment._n._n_n._n_offset);
   2036 	    }
   2037 	  else
   2038 	    {
   2039 	      /* Long name in debug section.  Very similar.  */
   2040 	      if (debug_sec_data == NULL)
   2041 		{
   2042 		  debug_sec_data = build_debug_section (abfd, &debug_sec);
   2043 		  if (debug_sec_data == NULL)
   2044 		    return NULL;
   2045 		}
   2046 	      /* PR binutils/17512: Catch out of range offsets into
   2047 		 the debug data.  */
   2048 	      if (sym->u.syment._n._n_n._n_offset >= debug_sec->size)
   2049 		sym->u.syment._n._n_n._n_offset =
   2050 		  (uintptr_t) _("<corrupt>");
   2051 	      else
   2052 		sym->u.syment._n._n_n._n_offset =
   2053 		  (uintptr_t) (debug_sec_data
   2054 			       + sym->u.syment._n._n_n._n_offset);
   2055 	    }
   2056 	}
   2057     }
   2058 
   2059   /* Free the raw symbols.  */
   2060   if (obj_coff_external_syms (abfd) != NULL
   2061       && ! obj_coff_keep_syms (abfd))
   2062     {
   2063       free (obj_coff_external_syms (abfd));
   2064       obj_coff_external_syms (abfd) = NULL;
   2065     }
   2066 
   2067   obj_raw_syments (abfd) = internal;
   2068   BFD_ASSERT (obj_raw_syment_count (abfd)
   2069 	      == (size_t) (internal_ptr - internal));
   2070 
   2071   return internal;
   2072 }
   2073 
   2074 long
   2075 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   2076 {
   2077   size_t count, raw;
   2078 
   2079   count = asect->reloc_count;
   2080   if (count >= LONG_MAX / sizeof (arelent *)
   2081       || _bfd_mul_overflow (count, bfd_coff_relsz (abfd), &raw))
   2082     {
   2083       bfd_set_error (bfd_error_file_too_big);
   2084       return -1;
   2085     }
   2086   if (!bfd_write_p (abfd))
   2087     {
   2088       ufile_ptr filesize = bfd_get_file_size (abfd);
   2089       if (filesize != 0 && raw > filesize)
   2090 	{
   2091 	  bfd_set_error (bfd_error_file_truncated);
   2092 	  return -1;
   2093 	}
   2094     }
   2095   return (count + 1) * sizeof (arelent *);
   2096 }
   2097 
   2098 asymbol *
   2099 coff_make_empty_symbol (bfd *abfd)
   2100 {
   2101   size_t amt = sizeof (coff_symbol_type);
   2102   coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
   2103 
   2104   if (new_symbol == NULL)
   2105     return NULL;
   2106   new_symbol->symbol.section = 0;
   2107   new_symbol->native = NULL;
   2108   new_symbol->lineno = NULL;
   2109   new_symbol->done_lineno = false;
   2110   new_symbol->symbol.the_bfd = abfd;
   2111 
   2112   return & new_symbol->symbol;
   2113 }
   2114 
   2115 /* Make a debugging symbol.  */
   2116 
   2117 asymbol *
   2118 coff_bfd_make_debug_symbol (bfd *abfd)
   2119 {
   2120   size_t amt = sizeof (coff_symbol_type);
   2121   coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
   2122 
   2123   if (new_symbol == NULL)
   2124     return NULL;
   2125   /* @@ The 10 is a guess at a plausible maximum number of aux entries
   2126      (but shouldn't be a constant).  */
   2127   amt = sizeof (combined_entry_type) * 10;
   2128   new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
   2129   if (!new_symbol->native)
   2130     return NULL;
   2131   new_symbol->native->is_sym = true;
   2132   new_symbol->symbol.section = bfd_abs_section_ptr;
   2133   new_symbol->symbol.flags = BSF_DEBUGGING;
   2134   new_symbol->lineno = NULL;
   2135   new_symbol->done_lineno = false;
   2136   new_symbol->symbol.the_bfd = abfd;
   2137 
   2138   return & new_symbol->symbol;
   2139 }
   2140 
   2141 void
   2142 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
   2143 {
   2144   bfd_symbol_info (symbol, ret);
   2145 
   2146   if (coffsymbol (symbol)->native != NULL
   2147       && coffsymbol (symbol)->native->fix_value
   2148       && coffsymbol (symbol)->native->is_sym)
   2149     ret->value
   2150       = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value
   2151 	  - (uintptr_t) obj_raw_syments (abfd))
   2152 	 / sizeof (combined_entry_type));
   2153 }
   2154 
   2155 /* Print out information about COFF symbol.  */
   2156 
   2157 void
   2158 coff_print_symbol (bfd *abfd,
   2159 		   void * filep,
   2160 		   asymbol *symbol,
   2161 		   bfd_print_symbol_type how)
   2162 {
   2163   FILE * file = (FILE *) filep;
   2164 
   2165   switch (how)
   2166     {
   2167     case bfd_print_symbol_name:
   2168       fprintf (file, "%s", symbol->name);
   2169       break;
   2170 
   2171     case bfd_print_symbol_more:
   2172       fprintf (file, "coff %s %s",
   2173 	       coffsymbol (symbol)->native ? "n" : "g",
   2174 	       coffsymbol (symbol)->lineno ? "l" : " ");
   2175       break;
   2176 
   2177     case bfd_print_symbol_all:
   2178       if (coffsymbol (symbol)->native)
   2179 	{
   2180 	  bfd_vma val;
   2181 	  unsigned int aux;
   2182 	  combined_entry_type *combined = coffsymbol (symbol)->native;
   2183 	  combined_entry_type *root = obj_raw_syments (abfd);
   2184 	  struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
   2185 
   2186 	  fprintf (file, "[%3ld]", (long) (combined - root));
   2187 
   2188 	  /* PR 17512: file: 079-33786-0.001:0.1.  */
   2189 	  if (combined < obj_raw_syments (abfd)
   2190 	      || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
   2191 	    {
   2192 	      fprintf (file, _("<corrupt info> %s"), symbol->name);
   2193 	      break;
   2194 	    }
   2195 
   2196 	  BFD_ASSERT (combined->is_sym);
   2197 	  if (! combined->fix_value)
   2198 	    val = (bfd_vma) combined->u.syment.n_value;
   2199 	  else
   2200 	    val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root)
   2201 		   / sizeof (combined_entry_type));
   2202 
   2203 	  fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
   2204 		   combined->u.syment.n_scnum,
   2205 		   combined->u.syment.n_flags,
   2206 		   combined->u.syment.n_type,
   2207 		   combined->u.syment.n_sclass,
   2208 		   combined->u.syment.n_numaux);
   2209 	  bfd_fprintf_vma (abfd, file, val);
   2210 	  fprintf (file, " %s", symbol->name);
   2211 
   2212 	  for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
   2213 	    {
   2214 	      combined_entry_type *auxp = combined + aux + 1;
   2215 	      long tagndx;
   2216 
   2217 	      BFD_ASSERT (! auxp->is_sym);
   2218 	      if (auxp->fix_tag)
   2219 		tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
   2220 	      else
   2221 		tagndx = auxp->u.auxent.x_sym.x_tagndx.u32;
   2222 
   2223 	      fprintf (file, "\n");
   2224 
   2225 	      if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
   2226 		continue;
   2227 
   2228 	      switch (combined->u.syment.n_sclass)
   2229 		{
   2230 		case C_FILE:
   2231 		  fprintf (file, "File ");
   2232 		  /* Add additional information if this isn't the filename
   2233 		     auxiliary entry.  */
   2234 		  if (auxp->u.auxent.x_file.x_ftype)
   2235 		    fprintf (file, "ftype %d fname \"%s\"",
   2236 			     auxp->u.auxent.x_file.x_ftype,
   2237 			     (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset);
   2238 		  break;
   2239 
   2240 		case C_DWARF:
   2241 		  fprintf (file, "AUX scnlen %#" PRIx64 " nreloc %" PRId64,
   2242 			   auxp->u.auxent.x_sect.x_scnlen,
   2243 			   auxp->u.auxent.x_sect.x_nreloc);
   2244 		  break;
   2245 
   2246 		case C_STAT:
   2247 		  if (combined->u.syment.n_type == T_NULL)
   2248 		    /* Probably a section symbol ?  */
   2249 		    {
   2250 		      fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
   2251 			       (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
   2252 			       auxp->u.auxent.x_scn.x_nreloc,
   2253 			       auxp->u.auxent.x_scn.x_nlinno);
   2254 		      if (auxp->u.auxent.x_scn.x_checksum != 0
   2255 			  || auxp->u.auxent.x_scn.x_associated != 0
   2256 			  || auxp->u.auxent.x_scn.x_comdat != 0)
   2257 			fprintf (file, " checksum 0x%x assoc %d comdat %d",
   2258 				 auxp->u.auxent.x_scn.x_checksum,
   2259 				 auxp->u.auxent.x_scn.x_associated,
   2260 				 auxp->u.auxent.x_scn.x_comdat);
   2261 		      break;
   2262 		    }
   2263 		  /* Fall through.  */
   2264 		case C_EXT:
   2265 		case C_AIX_WEAKEXT:
   2266 		  if (ISFCN (combined->u.syment.n_type))
   2267 		    {
   2268 		      long next, llnos;
   2269 
   2270 		      if (auxp->fix_end)
   2271 			next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
   2272 			       - root);
   2273 		      else
   2274 			next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
   2275 		      llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
   2276 		      fprintf (file,
   2277 			       "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
   2278 			       tagndx,
   2279 			       (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
   2280 			       llnos, next);
   2281 		      break;
   2282 		    }
   2283 		  /* Fall through.  */
   2284 		default:
   2285 		  fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
   2286 			   auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
   2287 			   auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
   2288 			   tagndx);
   2289 		  if (auxp->fix_end)
   2290 		    fprintf (file, " endndx %ld",
   2291 			     ((long)
   2292 			      (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
   2293 			       - root)));
   2294 		  break;
   2295 		}
   2296 	    }
   2297 
   2298 	  if (l)
   2299 	    {
   2300 	      fprintf (file, "\n%s :", l->u.sym->name);
   2301 	      l++;
   2302 	      while (l->line_number)
   2303 		{
   2304 		  if (l->line_number > 0)
   2305 		    {
   2306 		      fprintf (file, "\n%4d : ", l->line_number);
   2307 		      bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
   2308 		    }
   2309 		  l++;
   2310 		}
   2311 	    }
   2312 	}
   2313       else
   2314 	{
   2315 	  bfd_print_symbol_vandf (abfd, (void *) file, symbol);
   2316 	  fprintf (file, " %-5s %s %s %s",
   2317 		   symbol->section->name,
   2318 		   coffsymbol (symbol)->native ? "n" : "g",
   2319 		   coffsymbol (symbol)->lineno ? "l" : " ",
   2320 		   symbol->name);
   2321 	}
   2322     }
   2323 }
   2324 
   2325 /* Return whether a symbol name implies a local symbol.  In COFF,
   2326    local symbols generally start with ``.L''.  Most targets use this
   2327    function for the is_local_label_name entry point, but some may
   2328    override it.  */
   2329 
   2330 bool
   2331 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   2332 			       const char *name)
   2333 {
   2334   return name[0] == '.' && name[1] == 'L';
   2335 }
   2336 
   2337 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
   2338    section, calculate and return the name of the source file and the line
   2339    nearest to the wanted location.  */
   2340 
   2341 bool
   2342 coff_find_nearest_line_with_names (bfd *abfd,
   2343 				   asymbol **symbols,
   2344 				   asection *section,
   2345 				   bfd_vma offset,
   2346 				   const char **filename_ptr,
   2347 				   const char **functionname_ptr,
   2348 				   unsigned int *line_ptr,
   2349 				   const struct dwarf_debug_section *debug_sections)
   2350 {
   2351   bool found;
   2352   unsigned int i;
   2353   unsigned int line_base;
   2354   coff_data_type *cof = coff_data (abfd);
   2355   /* Run through the raw syments if available.  */
   2356   combined_entry_type *p;
   2357   combined_entry_type *pend;
   2358   alent *l;
   2359   struct coff_section_tdata *sec_data;
   2360   size_t amt;
   2361 
   2362   /* Before looking through the symbol table, try to use a .stab
   2363      section to find the information.  */
   2364   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   2365 					     &found, filename_ptr,
   2366 					     functionname_ptr, line_ptr,
   2367 					     &coff_data(abfd)->line_info))
   2368     return false;
   2369 
   2370   if (found)
   2371     return true;
   2372 
   2373   /* Also try examining DWARF2 debugging information.  */
   2374   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   2375 				     filename_ptr, functionname_ptr,
   2376 				     line_ptr, NULL, debug_sections,
   2377 				     &coff_data(abfd)->dwarf2_find_line_info))
   2378     return true;
   2379 
   2380   sec_data = coff_section_data (abfd, section);
   2381 
   2382   /* If the DWARF lookup failed, but there is DWARF information available
   2383      then the problem might be that the file has been rebased.  This tool
   2384      changes the VMAs of all the sections, but it does not update the DWARF
   2385      information.  So try again, using a bias against the address sought.  */
   2386   if (coff_data (abfd)->dwarf2_find_line_info != NULL)
   2387     {
   2388       bfd_signed_vma bias = 0;
   2389 
   2390       /* Create a cache of the result for the next call.  */
   2391       if (sec_data == NULL && section->owner == abfd)
   2392 	{
   2393 	  amt = sizeof (struct coff_section_tdata);
   2394 	  section->used_by_bfd = bfd_zalloc (abfd, amt);
   2395 	  sec_data = (struct coff_section_tdata *) section->used_by_bfd;
   2396 	}
   2397 
   2398       if (sec_data != NULL && sec_data->saved_bias)
   2399 	bias = sec_data->bias;
   2400       else if (symbols)
   2401 	{
   2402 	  bias = _bfd_dwarf2_find_symbol_bias (symbols,
   2403 					       & coff_data (abfd)->dwarf2_find_line_info);
   2404 
   2405 	  if (sec_data)
   2406 	    {
   2407 	      sec_data->saved_bias = true;
   2408 	      sec_data->bias = bias;
   2409 	    }
   2410 	}
   2411 
   2412       if (bias
   2413 	  && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
   2414 					    offset + bias,
   2415 					    filename_ptr, functionname_ptr,
   2416 					    line_ptr, NULL, debug_sections,
   2417 					    &coff_data(abfd)->dwarf2_find_line_info))
   2418 	return true;
   2419     }
   2420 
   2421   *filename_ptr = 0;
   2422   *functionname_ptr = 0;
   2423   *line_ptr = 0;
   2424 
   2425   /* Don't try and find line numbers in a non coff file.  */
   2426   if (!bfd_family_coff (abfd))
   2427     return false;
   2428 
   2429   if (cof == NULL)
   2430     return false;
   2431 
   2432   /* Find the first C_FILE symbol.  */
   2433   p = cof->raw_syments;
   2434   if (!p)
   2435     return false;
   2436 
   2437   pend = p + cof->raw_syment_count;
   2438   while (p < pend)
   2439     {
   2440       BFD_ASSERT (p->is_sym);
   2441       if (p->u.syment.n_sclass == C_FILE)
   2442 	break;
   2443       p += 1 + p->u.syment.n_numaux;
   2444     }
   2445 
   2446   if (p < pend)
   2447     {
   2448       bfd_vma sec_vma;
   2449       bfd_vma maxdiff;
   2450 
   2451       /* Look through the C_FILE symbols to find the best one.  */
   2452       sec_vma = bfd_section_vma (section);
   2453       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
   2454       maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
   2455       while (1)
   2456 	{
   2457 	  bfd_vma file_addr;
   2458 	  combined_entry_type *p2;
   2459 
   2460 	  for (p2 = p + 1 + p->u.syment.n_numaux;
   2461 	       p2 < pend;
   2462 	       p2 += 1 + p2->u.syment.n_numaux)
   2463 	    {
   2464 	      BFD_ASSERT (p2->is_sym);
   2465 	      if (p2->u.syment.n_scnum > 0
   2466 		  && (section
   2467 		      == coff_section_from_bfd_index (abfd,
   2468 						      p2->u.syment.n_scnum)))
   2469 		break;
   2470 	      if (p2->u.syment.n_sclass == C_FILE)
   2471 		{
   2472 		  p2 = pend;
   2473 		  break;
   2474 		}
   2475 	    }
   2476 	  if (p2 >= pend)
   2477 	    break;
   2478 
   2479 	  file_addr = (bfd_vma) p2->u.syment.n_value;
   2480 	  /* PR 11512: Include the section address of the function name symbol.  */
   2481 	  if (p2->u.syment.n_scnum > 0)
   2482 	    file_addr += coff_section_from_bfd_index (abfd,
   2483 						      p2->u.syment.n_scnum)->vma;
   2484 	  /* We use <= MAXDIFF here so that if we get a zero length
   2485 	     file, we actually use the next file entry.  */
   2486 	  if (p2 < pend
   2487 	      && offset + sec_vma >= file_addr
   2488 	      && offset + sec_vma - file_addr <= maxdiff)
   2489 	    {
   2490 	      *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
   2491 	      maxdiff = offset + sec_vma - p2->u.syment.n_value;
   2492 	    }
   2493 
   2494 	  if (p->u.syment.n_value >= cof->raw_syment_count)
   2495 	    break;
   2496 
   2497 	  /* Avoid endless loops on erroneous files by ensuring that
   2498 	     we always move forward in the file.  */
   2499 	  if (p >= cof->raw_syments + p->u.syment.n_value)
   2500 	    break;
   2501 
   2502 	  p = cof->raw_syments + p->u.syment.n_value;
   2503 	  if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
   2504 	    break;
   2505 	}
   2506     }
   2507 
   2508   if (section->lineno_count == 0)
   2509     {
   2510       *functionname_ptr = NULL;
   2511       *line_ptr = 0;
   2512       return true;
   2513     }
   2514 
   2515   /* Now wander though the raw linenumbers of the section.
   2516      If we have been called on this section before, and the offset
   2517      we want is further down then we can prime the lookup loop.  */
   2518   if (sec_data != NULL
   2519       && sec_data->i > 0
   2520       && offset >= sec_data->offset)
   2521     {
   2522       i = sec_data->i;
   2523       *functionname_ptr = sec_data->function;
   2524       line_base = sec_data->line_base;
   2525     }
   2526   else
   2527     {
   2528       i = 0;
   2529       line_base = 0;
   2530     }
   2531 
   2532   if (section->lineno != NULL)
   2533     {
   2534       bfd_vma last_value = 0;
   2535 
   2536       l = &section->lineno[i];
   2537 
   2538       for (; i < section->lineno_count; i++)
   2539 	{
   2540 	  if (l->line_number == 0)
   2541 	    {
   2542 	      /* Get the symbol this line number points at.  */
   2543 	      coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
   2544 	      if (coff->symbol.value > offset)
   2545 		break;
   2546 
   2547 	      *functionname_ptr = coff->symbol.name;
   2548 	      last_value = coff->symbol.value;
   2549 	      if (coff->native)
   2550 		{
   2551 		  combined_entry_type *s = coff->native;
   2552 
   2553 		  BFD_ASSERT (s->is_sym);
   2554 		  s = s + 1 + s->u.syment.n_numaux;
   2555 
   2556 		  /* In XCOFF a debugging symbol can follow the
   2557 		     function symbol.  */
   2558 		  if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
   2559 		       < obj_raw_syment_count (abfd) * sizeof (*s))
   2560 		      && s->u.syment.n_scnum == N_DEBUG)
   2561 		    s = s + 1 + s->u.syment.n_numaux;
   2562 
   2563 		  /* S should now point to the .bf of the function.  */
   2564 		  if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
   2565 		       < obj_raw_syment_count (abfd) * sizeof (*s))
   2566 		      && s->u.syment.n_numaux)
   2567 		    {
   2568 		      /* The linenumber is stored in the auxent.  */
   2569 		      union internal_auxent *a = &((s + 1)->u.auxent);
   2570 
   2571 		      line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
   2572 		      *line_ptr = line_base;
   2573 		    }
   2574 		}
   2575 	    }
   2576 	  else
   2577 	    {
   2578 	      if (l->u.offset > offset)
   2579 		break;
   2580 	      *line_ptr = l->line_number + line_base - 1;
   2581 	    }
   2582 	  l++;
   2583 	}
   2584 
   2585       /* If we fell off the end of the loop, then assume that this
   2586 	 symbol has no line number info.  Otherwise, symbols with no
   2587 	 line number info get reported with the line number of the
   2588 	 last line of the last symbol which does have line number
   2589 	 info.  We use 0x100 as a slop to account for cases where the
   2590 	 last line has executable code.  */
   2591       if (i >= section->lineno_count
   2592 	  && last_value != 0
   2593 	  && offset - last_value > 0x100)
   2594 	{
   2595 	  *functionname_ptr = NULL;
   2596 	  *line_ptr = 0;
   2597 	}
   2598     }
   2599 
   2600   /* Cache the results for the next call.  */
   2601   if (sec_data == NULL && section->owner == abfd)
   2602     {
   2603       amt = sizeof (struct coff_section_tdata);
   2604       section->used_by_bfd = bfd_zalloc (abfd, amt);
   2605       sec_data = (struct coff_section_tdata *) section->used_by_bfd;
   2606     }
   2607 
   2608   if (sec_data != NULL)
   2609     {
   2610       sec_data->offset = offset;
   2611       sec_data->i = i - 1;
   2612       sec_data->function = *functionname_ptr;
   2613       sec_data->line_base = line_base;
   2614     }
   2615 
   2616   return true;
   2617 }
   2618 
   2619 bool
   2620 coff_find_nearest_line (bfd *abfd,
   2621 			asymbol **symbols,
   2622 			asection *section,
   2623 			bfd_vma offset,
   2624 			const char **filename_ptr,
   2625 			const char **functionname_ptr,
   2626 			unsigned int *line_ptr,
   2627 			unsigned int *discriminator_ptr)
   2628 {
   2629   if (discriminator_ptr)
   2630     *discriminator_ptr = 0;
   2631   return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
   2632 					    filename_ptr, functionname_ptr,
   2633 					    line_ptr, dwarf_debug_sections);
   2634 }
   2635 
   2636 bool
   2637 coff_find_inliner_info (bfd *abfd,
   2638 			const char **filename_ptr,
   2639 			const char **functionname_ptr,
   2640 			unsigned int *line_ptr)
   2641 {
   2642   bool found;
   2643 
   2644   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   2645 					 functionname_ptr, line_ptr,
   2646 					 &coff_data(abfd)->dwarf2_find_line_info);
   2647   return (found);
   2648 }
   2649 
   2650 int
   2651 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   2652 {
   2653   size_t size;
   2654 
   2655   if (!bfd_link_relocatable (info))
   2656     size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
   2657   else
   2658     size = bfd_coff_filhsz (abfd);
   2659 
   2660   size += abfd->section_count * bfd_coff_scnhsz (abfd);
   2661   return size;
   2662 }
   2663 
   2664 /* Change the class of a coff symbol held by BFD.  */
   2665 
   2666 bool
   2667 bfd_coff_set_symbol_class (bfd *	 abfd,
   2668 			   asymbol *	 symbol,
   2669 			   unsigned int	 symbol_class)
   2670 {
   2671   coff_symbol_type * csym;
   2672 
   2673   csym = coff_symbol_from (symbol);
   2674   if (csym == NULL)
   2675     {
   2676       bfd_set_error (bfd_error_invalid_operation);
   2677       return false;
   2678     }
   2679   else if (csym->native == NULL)
   2680     {
   2681       /* This is an alien symbol which no native coff backend data.
   2682 	 We cheat here by creating a fake native entry for it and
   2683 	 then filling in the class.  This code is based on that in
   2684 	 coff_write_alien_symbol().  */
   2685 
   2686       combined_entry_type * native;
   2687       size_t amt = sizeof (* native);
   2688 
   2689       native = (combined_entry_type *) bfd_zalloc (abfd, amt);
   2690       if (native == NULL)
   2691 	return false;
   2692 
   2693       native->is_sym = true;
   2694       native->u.syment.n_type   = T_NULL;
   2695       native->u.syment.n_sclass = symbol_class;
   2696 
   2697       if (bfd_is_und_section (symbol->section))
   2698 	{
   2699 	  native->u.syment.n_scnum = N_UNDEF;
   2700 	  native->u.syment.n_value = symbol->value;
   2701 	}
   2702       else if (bfd_is_com_section (symbol->section))
   2703 	{
   2704 	  native->u.syment.n_scnum = N_UNDEF;
   2705 	  native->u.syment.n_value = symbol->value;
   2706 	}
   2707       else
   2708 	{
   2709 	  native->u.syment.n_scnum =
   2710 	    symbol->section->output_section->target_index;
   2711 	  native->u.syment.n_value = (symbol->value
   2712 				      + symbol->section->output_offset);
   2713 	  if (! obj_pe (abfd))
   2714 	    native->u.syment.n_value += symbol->section->output_section->vma;
   2715 
   2716 	  /* Copy the any flags from the file header into the symbol.
   2717 	     FIXME: Why?  */
   2718 	  native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
   2719 	}
   2720 
   2721       csym->native = native;
   2722     }
   2723   else
   2724     csym->native->u.syment.n_sclass = symbol_class;
   2725 
   2726   return true;
   2727 }
   2728 
   2729 bool
   2730 _bfd_coff_section_already_linked (bfd *abfd,
   2731 				  asection *sec,
   2732 				  struct bfd_link_info *info)
   2733 {
   2734   flagword flags;
   2735   const char *name, *key;
   2736   struct bfd_section_already_linked *l;
   2737   struct bfd_section_already_linked_hash_entry *already_linked_list;
   2738   struct coff_comdat_info *s_comdat;
   2739 
   2740   if (sec->output_section == bfd_abs_section_ptr)
   2741     return false;
   2742 
   2743   flags = sec->flags;
   2744   if ((flags & SEC_LINK_ONCE) == 0)
   2745     return false;
   2746 
   2747   /* The COFF backend linker doesn't support group sections.  */
   2748   if ((flags & SEC_GROUP) != 0)
   2749     return false;
   2750 
   2751   name = bfd_section_name (sec);
   2752   s_comdat = bfd_coff_get_comdat_section (abfd, sec);
   2753 
   2754   if (s_comdat != NULL)
   2755     key = s_comdat->name;
   2756   else
   2757     {
   2758       if (startswith (name, ".gnu.linkonce.")
   2759 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
   2760 	key++;
   2761       else
   2762 	/* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
   2763 	   .xdata$<key> and .pdata$<key> only the first of which has a
   2764 	   comdat key.  Should these all match the LTO IR key?  */
   2765 	key = name;
   2766     }
   2767 
   2768   already_linked_list = bfd_section_already_linked_table_lookup (key);
   2769 
   2770   for (l = already_linked_list->entry; l != NULL; l = l->next)
   2771     {
   2772       struct coff_comdat_info *l_comdat;
   2773 
   2774       l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
   2775 
   2776       /* The section names must match, and both sections must be
   2777 	 comdat and have the same comdat name, or both sections must
   2778 	 be non-comdat.  LTO IR plugin sections are an exception.  They
   2779 	 are always named .gnu.linkonce.t.<key> (<key> is some string)
   2780 	 and match any comdat section with comdat name of <key>, and
   2781 	 any linkonce section with the same suffix, ie.
   2782 	 .gnu.linkonce.*.<key>.  */
   2783       if (((s_comdat != NULL) == (l_comdat != NULL)
   2784 	   && strcmp (name, l->sec->name) == 0)
   2785 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0
   2786 	  || (sec->owner->flags & BFD_PLUGIN) != 0)
   2787 	{
   2788 	  /* The section has already been linked.  See if we should
   2789 	     issue a warning.  */
   2790 	  return _bfd_handle_already_linked (sec, l, info);
   2791 	}
   2792     }
   2793 
   2794   /* This is the first section with this name.  Record it.  */
   2795   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
   2796     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
   2797   return false;
   2798 }
   2799 
   2800 /* Initialize COOKIE for input bfd ABFD. */
   2801 
   2802 static bool
   2803 init_reloc_cookie (struct coff_reloc_cookie *cookie,
   2804 		   struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2805 		   bfd *abfd)
   2806 {
   2807   /* Sometimes the symbol table does not yet have been loaded here.  */
   2808   bfd_coff_slurp_symbol_table (abfd);
   2809 
   2810   cookie->abfd = abfd;
   2811   cookie->sym_hashes = obj_coff_sym_hashes (abfd);
   2812 
   2813   cookie->symbols = obj_symbols (abfd);
   2814 
   2815   return true;
   2816 }
   2817 
   2818 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
   2819 
   2820 static void
   2821 fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED,
   2822 		   bfd *abfd ATTRIBUTE_UNUSED)
   2823 {
   2824   /* Nothing to do.  */
   2825 }
   2826 
   2827 /* Initialize the relocation information in COOKIE for input section SEC
   2828    of input bfd ABFD.  */
   2829 
   2830 static bool
   2831 init_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
   2832 			struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2833 			bfd *abfd,
   2834 			asection *sec)
   2835 {
   2836   if (sec->reloc_count == 0)
   2837     {
   2838       cookie->rels = NULL;
   2839       cookie->relend = NULL;
   2840       cookie->rel = NULL;
   2841       return true;
   2842     }
   2843 
   2844   cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL,
   2845 						 0, NULL);
   2846 
   2847   if (cookie->rels == NULL)
   2848     return false;
   2849 
   2850   cookie->rel = cookie->rels;
   2851   cookie->relend = (cookie->rels + sec->reloc_count);
   2852   return true;
   2853 }
   2854 
   2855 /* Free the memory allocated by init_reloc_cookie_rels,
   2856    if appropriate.  */
   2857 
   2858 static void
   2859 fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
   2860 			asection *sec)
   2861 {
   2862   if (cookie->rels
   2863       /* PR 20401.  The relocs may not have been cached, so check first.
   2864 	 If the relocs were loaded by init_reloc_cookie_rels() then this
   2865 	 will be the case.  FIXME: Would performance be improved if the
   2866 	 relocs *were* cached ?  */
   2867       && coff_section_data (NULL, sec)
   2868       && coff_section_data (NULL, sec)->relocs != cookie->rels)
   2869     free (cookie->rels);
   2870 }
   2871 
   2872 /* Initialize the whole of COOKIE for input section SEC.  */
   2873 
   2874 static bool
   2875 init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
   2876 			       struct bfd_link_info *info,
   2877 			       asection *sec)
   2878 {
   2879   if (!init_reloc_cookie (cookie, info, sec->owner))
   2880     return false;
   2881 
   2882   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
   2883     {
   2884       fini_reloc_cookie (cookie, sec->owner);
   2885       return false;
   2886     }
   2887   return true;
   2888 }
   2889 
   2890 /* Free the memory allocated by init_reloc_cookie_for_section,
   2891    if appropriate.  */
   2892 
   2893 static void
   2894 fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
   2895 			       asection *sec)
   2896 {
   2897   fini_reloc_cookie_rels (cookie, sec);
   2898   fini_reloc_cookie (cookie, sec->owner);
   2899 }
   2900 
   2901 static asection *
   2902 _bfd_coff_gc_mark_hook (asection *sec,
   2903 			struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2904 			struct internal_reloc *rel ATTRIBUTE_UNUSED,
   2905 			struct coff_link_hash_entry *h,
   2906 			struct internal_syment *sym)
   2907 {
   2908   if (h != NULL)
   2909     {
   2910       switch (h->root.type)
   2911 	{
   2912 	case bfd_link_hash_defined:
   2913 	case bfd_link_hash_defweak:
   2914 	  return h->root.u.def.section;
   2915 
   2916 	case bfd_link_hash_common:
   2917 	  return h->root.u.c.p->section;
   2918 
   2919 	case bfd_link_hash_undefweak:
   2920 	  if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
   2921 	    {
   2922 	      /* PE weak externals.  A weak symbol may include an auxiliary
   2923 		 record indicating that if the weak symbol is not resolved,
   2924 		 another external symbol is used instead.  */
   2925 	      struct coff_link_hash_entry *h2 =
   2926 		h->auxbfd->tdata.coff_obj_data->sym_hashes
   2927 		[h->aux->x_sym.x_tagndx.u32];
   2928 
   2929 	      if (h2 && h2->root.type != bfd_link_hash_undefined)
   2930 		return  h2->root.u.def.section;
   2931 	    }
   2932 	  break;
   2933 
   2934 	case bfd_link_hash_undefined:
   2935 	default:
   2936 	  break;
   2937 	}
   2938       return NULL;
   2939     }
   2940 
   2941   return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
   2942 }
   2943 
   2944 /* COOKIE->rel describes a relocation against section SEC, which is
   2945    a section we've decided to keep.  Return the section that contains
   2946    the relocation symbol, or NULL if no section contains it.  */
   2947 
   2948 static asection *
   2949 _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
   2950 			coff_gc_mark_hook_fn gc_mark_hook,
   2951 			struct coff_reloc_cookie *cookie)
   2952 {
   2953   struct coff_link_hash_entry *h;
   2954 
   2955   h = cookie->sym_hashes[cookie->rel->r_symndx];
   2956   if (h != NULL)
   2957     {
   2958       while (h->root.type == bfd_link_hash_indirect
   2959 	     || h->root.type == bfd_link_hash_warning)
   2960 	h = (struct coff_link_hash_entry *) h->root.u.i.link;
   2961 
   2962       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
   2963     }
   2964 
   2965   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
   2966 			  &(cookie->symbols
   2967 			    + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment);
   2968 }
   2969 
   2970 static bool _bfd_coff_gc_mark
   2971   (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
   2972 
   2973 /* COOKIE->rel describes a relocation against section SEC, which is
   2974    a section we've decided to keep.  Mark the section that contains
   2975    the relocation symbol.  */
   2976 
   2977 static bool
   2978 _bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
   2979 			 asection *sec,
   2980 			 coff_gc_mark_hook_fn gc_mark_hook,
   2981 			 struct coff_reloc_cookie *cookie)
   2982 {
   2983   asection *rsec;
   2984 
   2985   rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
   2986   if (rsec && !rsec->gc_mark)
   2987     {
   2988       if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
   2989 	rsec->gc_mark = 1;
   2990       else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
   2991 	return false;
   2992     }
   2993   return true;
   2994 }
   2995 
   2996 /* The mark phase of garbage collection.  For a given section, mark
   2997    it and any sections in this section's group, and all the sections
   2998    which define symbols to which it refers.  */
   2999 
   3000 static bool
   3001 _bfd_coff_gc_mark (struct bfd_link_info *info,
   3002 		   asection *sec,
   3003 		   coff_gc_mark_hook_fn gc_mark_hook)
   3004 {
   3005   bool ret = true;
   3006 
   3007   sec->gc_mark = 1;
   3008 
   3009   /* Look through the section relocs.  */
   3010   if ((sec->flags & SEC_RELOC) != 0
   3011       && sec->reloc_count > 0)
   3012     {
   3013       struct coff_reloc_cookie cookie;
   3014 
   3015       if (!init_reloc_cookie_for_section (&cookie, info, sec))
   3016 	ret = false;
   3017       else
   3018 	{
   3019 	  for (; cookie.rel < cookie.relend; cookie.rel++)
   3020 	    {
   3021 	      if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
   3022 		{
   3023 		  ret = false;
   3024 		  break;
   3025 		}
   3026 	    }
   3027 	  fini_reloc_cookie_for_section (&cookie, sec);
   3028 	}
   3029     }
   3030 
   3031   return ret;
   3032 }
   3033 
   3034 static bool
   3035 _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
   3036 				  coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
   3037 {
   3038   bfd *ibfd;
   3039 
   3040   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   3041     {
   3042       asection *isec;
   3043       bool some_kept;
   3044 
   3045       if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour)
   3046 	continue;
   3047 
   3048       /* Ensure all linker created sections are kept, and see whether
   3049 	 any other section is already marked.  */
   3050       some_kept = false;
   3051       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   3052 	{
   3053 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
   3054 	    isec->gc_mark = 1;
   3055 	  else if (isec->gc_mark)
   3056 	    some_kept = true;
   3057 	}
   3058 
   3059       /* If no section in this file will be kept, then we can
   3060 	 toss out debug sections.  */
   3061       if (!some_kept)
   3062 	continue;
   3063 
   3064       /* Keep debug and special sections like .comment when they are
   3065 	 not part of a group, or when we have single-member groups.  */
   3066       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   3067 	if ((isec->flags & SEC_DEBUGGING) != 0
   3068 	    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
   3069 	  isec->gc_mark = 1;
   3070     }
   3071   return true;
   3072 }
   3073 
   3074 /* Sweep symbols in swept sections.  Called via coff_link_hash_traverse.  */
   3075 
   3076 static bool
   3077 coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
   3078 		      void *data ATTRIBUTE_UNUSED)
   3079 {
   3080   if (h->root.type == bfd_link_hash_warning)
   3081     h = (struct coff_link_hash_entry *) h->root.u.i.link;
   3082 
   3083   if ((h->root.type == bfd_link_hash_defined
   3084        || h->root.type == bfd_link_hash_defweak)
   3085       && !h->root.u.def.section->gc_mark
   3086       && !(h->root.u.def.section->owner->flags & DYNAMIC))
   3087     {
   3088       /* Do our best to hide the symbol.  */
   3089       h->root.u.def.section = bfd_und_section_ptr;
   3090       h->symbol_class = C_HIDDEN;
   3091     }
   3092 
   3093   return true;
   3094 }
   3095 
   3096 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
   3097 
   3098 typedef bool (*gc_sweep_hook_fn)
   3099   (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
   3100 
   3101 static bool
   3102 coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
   3103 {
   3104   bfd *sub;
   3105 
   3106   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   3107     {
   3108       asection *o;
   3109 
   3110       if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
   3111 	continue;
   3112 
   3113       for (o = sub->sections; o != NULL; o = o->next)
   3114 	{
   3115 	    /* Keep debug and special sections.  */
   3116 	  if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
   3117 	      || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
   3118 	    o->gc_mark = 1;
   3119 	  else if (startswith (o->name, ".idata")
   3120 		   || startswith (o->name, ".pdata")
   3121 		   || startswith (o->name, ".xdata")
   3122 		   || startswith (o->name, ".rsrc"))
   3123 	    o->gc_mark = 1;
   3124 
   3125 	  if (o->gc_mark)
   3126 	    continue;
   3127 
   3128 	  /* Skip sweeping sections already excluded.  */
   3129 	  if (o->flags & SEC_EXCLUDE)
   3130 	    continue;
   3131 
   3132 	  /* Since this is early in the link process, it is simple
   3133 	     to remove a section from the output.  */
   3134 	  o->flags |= SEC_EXCLUDE;
   3135 
   3136 	  if (info->print_gc_sections && o->size != 0)
   3137 	    /* xgettext: c-format */
   3138 	    _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
   3139 				o, sub);
   3140 
   3141 #if 0
   3142 	  /* But we also have to update some of the relocation
   3143 	     info we collected before.  */
   3144 	  if (gc_sweep_hook
   3145 	      && (o->flags & SEC_RELOC) != 0
   3146 	      && o->reloc_count > 0
   3147 	      && !bfd_is_abs_section (o->output_section))
   3148 	    {
   3149 	      struct internal_reloc *internal_relocs;
   3150 	      bool r;
   3151 
   3152 	      internal_relocs
   3153 		= _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
   3154 					     info->keep_memory);
   3155 	      if (internal_relocs == NULL)
   3156 		return false;
   3157 
   3158 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
   3159 
   3160 	      if (coff_section_data (o)->relocs != internal_relocs)
   3161 		free (internal_relocs);
   3162 
   3163 	      if (!r)
   3164 		return false;
   3165 	    }
   3166 #endif
   3167 	}
   3168     }
   3169 
   3170   /* Remove the symbols that were in the swept sections from the dynamic
   3171      symbol table.  */
   3172   coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
   3173 			   NULL);
   3174 
   3175   return true;
   3176 }
   3177 
   3178 /* Keep all sections containing symbols undefined on the command-line,
   3179    and the section containing the entry symbol.  */
   3180 
   3181 static void
   3182 _bfd_coff_gc_keep (struct bfd_link_info *info)
   3183 {
   3184   struct bfd_sym_chain *sym;
   3185 
   3186   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   3187     {
   3188       struct coff_link_hash_entry *h;
   3189 
   3190       h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
   3191 				false, false, false);
   3192 
   3193       if (h != NULL
   3194 	  && (h->root.type == bfd_link_hash_defined
   3195 	      || h->root.type == bfd_link_hash_defweak)
   3196 	  && !bfd_is_abs_section (h->root.u.def.section))
   3197 	h->root.u.def.section->flags |= SEC_KEEP;
   3198     }
   3199 }
   3200 
   3201 /* Do mark and sweep of unused sections.  */
   3202 
   3203 bool
   3204 bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
   3205 {
   3206   bfd *sub;
   3207 
   3208   /* FIXME: Should we implement this? */
   3209 #if 0
   3210   const bfd_coff_backend_data *bed = coff_backend_info (abfd);
   3211 
   3212   if (!bed->can_gc_sections
   3213       || !is_coff_hash_table (info->hash))
   3214     {
   3215       _bfd_error_handler(_("warning: gc-sections option ignored"));
   3216       return true;
   3217     }
   3218 #endif
   3219 
   3220   _bfd_coff_gc_keep (info);
   3221 
   3222   /* Grovel through relocs to find out who stays ...  */
   3223   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   3224     {
   3225       asection *o;
   3226 
   3227       if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
   3228 	continue;
   3229 
   3230       for (o = sub->sections; o != NULL; o = o->next)
   3231 	{
   3232 	  if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
   3233 	       || startswith (o->name, ".vectors")
   3234 	       || startswith (o->name, ".ctors")
   3235 	       || startswith (o->name, ".dtors"))
   3236 	      && !o->gc_mark)
   3237 	    {
   3238 	      if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
   3239 		return false;
   3240 	    }
   3241 	}
   3242     }
   3243 
   3244   /* Allow the backend to mark additional target specific sections.  */
   3245   _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
   3246 
   3247   /* ... and mark SEC_EXCLUDE for those that go.  */
   3248   return coff_gc_sweep (abfd, info);
   3249 }
   3250 
   3251 /* Return name used to identify a comdat group.  */
   3252 
   3253 const char *
   3254 bfd_coff_group_name (bfd *abfd, const asection *sec)
   3255 {
   3256   struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec);
   3257   if (ci != NULL)
   3258     return ci->name;
   3259   return NULL;
   3260 }
   3261 
   3262 bool
   3263 _bfd_coff_free_cached_info (bfd *abfd)
   3264 {
   3265   struct coff_tdata *tdata;
   3266 
   3267   if (bfd_family_coff (abfd)
   3268       && (bfd_get_format (abfd) == bfd_object
   3269 	  || bfd_get_format (abfd) == bfd_core)
   3270       && (tdata = coff_data (abfd)) != NULL)
   3271     {
   3272       if (tdata->section_by_index)
   3273 	{
   3274 	  htab_delete (tdata->section_by_index);
   3275 	  tdata->section_by_index = NULL;
   3276 	}
   3277 
   3278       if (tdata->section_by_target_index)
   3279 	{
   3280 	  htab_delete (tdata->section_by_target_index);
   3281 	  tdata->section_by_target_index = NULL;
   3282 	}
   3283 
   3284       if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
   3285 	{
   3286 	  htab_delete (pe_data (abfd)->comdat_hash);
   3287 	  pe_data (abfd)->comdat_hash = NULL;
   3288 	}
   3289 
   3290       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   3291       _bfd_stab_cleanup (abfd, &tdata->line_info);
   3292 
   3293       /* PR 25447:
   3294 	 Do not clear the keep_syms and keep_strings flags.
   3295 	 These may have been set by pe_ILF_build_a_bfd() indicating
   3296 	 that the syms and strings pointers are not to be freed.  */
   3297       if (!_bfd_coff_free_symbols (abfd))
   3298 	return false;
   3299     }
   3300 
   3301   return _bfd_generic_bfd_free_cached_info (abfd);
   3302 }
   3303