Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.1.1.11
      1 /* ELF executable support for BFD.
      2 
      3    Copyright (C) 1993-2024 Free Software Foundation, Inc.
      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 
     23 /*
     24 SECTION
     25 	ELF backends
     26 
     27 	BFD support for ELF formats is being worked on.
     28 	Currently, the best supported back ends are for sparc and i386
     29 	(running svr4 or Solaris 2).
     30 
     31 	Documentation of the internals of the support code still needs
     32 	to be written.  The code is changing quickly enough that we
     33 	haven't bothered yet.  */
     34 
     35 /* For sparc64-cross-sparc32.  */
     36 #define _SYSCALL32
     37 #include "sysdep.h"
     38 #include <limits.h>
     39 #include "bfd.h"
     40 #include "bfdlink.h"
     41 #include "libbfd.h"
     42 #define ARCH_SIZE 0
     43 #include "elf-bfd.h"
     44 #include "libiberty.h"
     45 #include "safe-ctype.h"
     46 #include "elf-linux-core.h"
     47 
     48 #ifdef CORE_HEADER
     49 #include CORE_HEADER
     50 #endif
     51 
     52 static int elf_sort_sections (const void *, const void *);
     53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
     54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
     55 			   struct bfd_link_info *);
     56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
     57 			     file_ptr offset, size_t align);
     58 
     59 /* Swap version information in and out.  The version information is
     60    currently size independent.  If that ever changes, this code will
     61    need to move into elfcode.h.  */
     62 
     63 /* Swap in a Verdef structure.  */
     64 
     65 void
     66 _bfd_elf_swap_verdef_in (bfd *abfd,
     67 			 const Elf_External_Verdef *src,
     68 			 Elf_Internal_Verdef *dst)
     69 {
     70   dst->vd_version = H_GET_16 (abfd, src->vd_version);
     71   dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
     72   dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
     73   dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
     74   dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
     75   dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
     76   dst->vd_next    = H_GET_32 (abfd, src->vd_next);
     77 }
     78 
     79 /* Swap out a Verdef structure.  */
     80 
     81 void
     82 _bfd_elf_swap_verdef_out (bfd *abfd,
     83 			  const Elf_Internal_Verdef *src,
     84 			  Elf_External_Verdef *dst)
     85 {
     86   H_PUT_16 (abfd, src->vd_version, dst->vd_version);
     87   H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
     88   H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
     89   H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
     90   H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
     91   H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
     92   H_PUT_32 (abfd, src->vd_next, dst->vd_next);
     93 }
     94 
     95 /* Swap in a Verdaux structure.  */
     96 
     97 void
     98 _bfd_elf_swap_verdaux_in (bfd *abfd,
     99 			  const Elf_External_Verdaux *src,
    100 			  Elf_Internal_Verdaux *dst)
    101 {
    102   dst->vda_name = H_GET_32 (abfd, src->vda_name);
    103   dst->vda_next = H_GET_32 (abfd, src->vda_next);
    104 }
    105 
    106 /* Swap out a Verdaux structure.  */
    107 
    108 void
    109 _bfd_elf_swap_verdaux_out (bfd *abfd,
    110 			   const Elf_Internal_Verdaux *src,
    111 			   Elf_External_Verdaux *dst)
    112 {
    113   H_PUT_32 (abfd, src->vda_name, dst->vda_name);
    114   H_PUT_32 (abfd, src->vda_next, dst->vda_next);
    115 }
    116 
    117 /* Swap in a Verneed structure.  */
    118 
    119 void
    120 _bfd_elf_swap_verneed_in (bfd *abfd,
    121 			  const Elf_External_Verneed *src,
    122 			  Elf_Internal_Verneed *dst)
    123 {
    124   dst->vn_version = H_GET_16 (abfd, src->vn_version);
    125   dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
    126   dst->vn_file    = H_GET_32 (abfd, src->vn_file);
    127   dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
    128   dst->vn_next    = H_GET_32 (abfd, src->vn_next);
    129 }
    130 
    131 /* Swap out a Verneed structure.  */
    132 
    133 void
    134 _bfd_elf_swap_verneed_out (bfd *abfd,
    135 			   const Elf_Internal_Verneed *src,
    136 			   Elf_External_Verneed *dst)
    137 {
    138   H_PUT_16 (abfd, src->vn_version, dst->vn_version);
    139   H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
    140   H_PUT_32 (abfd, src->vn_file, dst->vn_file);
    141   H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
    142   H_PUT_32 (abfd, src->vn_next, dst->vn_next);
    143 }
    144 
    145 /* Swap in a Vernaux structure.  */
    146 
    147 void
    148 _bfd_elf_swap_vernaux_in (bfd *abfd,
    149 			  const Elf_External_Vernaux *src,
    150 			  Elf_Internal_Vernaux *dst)
    151 {
    152   dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
    153   dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
    154   dst->vna_other = H_GET_16 (abfd, src->vna_other);
    155   dst->vna_name  = H_GET_32 (abfd, src->vna_name);
    156   dst->vna_next  = H_GET_32 (abfd, src->vna_next);
    157 }
    158 
    159 /* Swap out a Vernaux structure.  */
    160 
    161 void
    162 _bfd_elf_swap_vernaux_out (bfd *abfd,
    163 			   const Elf_Internal_Vernaux *src,
    164 			   Elf_External_Vernaux *dst)
    165 {
    166   H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
    167   H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
    168   H_PUT_16 (abfd, src->vna_other, dst->vna_other);
    169   H_PUT_32 (abfd, src->vna_name, dst->vna_name);
    170   H_PUT_32 (abfd, src->vna_next, dst->vna_next);
    171 }
    172 
    173 /* Swap in a Versym structure.  */
    174 
    175 void
    176 _bfd_elf_swap_versym_in (bfd *abfd,
    177 			 const Elf_External_Versym *src,
    178 			 Elf_Internal_Versym *dst)
    179 {
    180   dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
    181 }
    182 
    183 /* Swap out a Versym structure.  */
    184 
    185 void
    186 _bfd_elf_swap_versym_out (bfd *abfd,
    187 			  const Elf_Internal_Versym *src,
    188 			  Elf_External_Versym *dst)
    189 {
    190   H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
    191 }
    192 
    193 /* Standard ELF hash function.  Do not change this function; you will
    194    cause invalid hash tables to be generated.  */
    195 
    196 unsigned long
    197 bfd_elf_hash (const char *namearg)
    198 {
    199   uint32_t h = 0;
    200 
    201   for (const unsigned char *name = (const unsigned char *) namearg;
    202        *name; name++)
    203     {
    204       h = (h << 4) + *name;
    205       h ^= (h >> 24) & 0xf0;
    206     }
    207   return h & 0x0fffffff;
    208 }
    209 
    210 /* DT_GNU_HASH hash function.  Do not change this function; you will
    211    cause invalid hash tables to be generated.  */
    212 
    213 unsigned long
    214 bfd_elf_gnu_hash (const char *namearg)
    215 {
    216   uint32_t h = 5381;
    217 
    218   for (const unsigned char *name = (const unsigned char *) namearg;
    219        *name; name++)
    220     h = (h << 5) + h + *name;
    221   return h;
    222 }
    223 
    224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    225    the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
    226 bool
    227 bfd_elf_allocate_object (bfd *abfd,
    228 			 size_t object_size,
    229 			 enum elf_target_id object_id)
    230 {
    231   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    232   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    233   if (abfd->tdata.any == NULL)
    234     return false;
    235 
    236   elf_object_id (abfd) = object_id;
    237   if (abfd->direction != read_direction)
    238     {
    239       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    240       if (o == NULL)
    241 	return false;
    242       elf_tdata (abfd)->o = o;
    243       elf_program_header_size (abfd) = (bfd_size_type) -1;
    244     }
    245   return true;
    246 }
    247 
    248 
    249 bool
    250 bfd_elf_make_object (bfd *abfd)
    251 {
    252   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    253   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
    254 				  bed->target_id);
    255 }
    256 
    257 bool
    258 bfd_elf_mkcorefile (bfd *abfd)
    259 {
    260   /* I think this can be done just like an object file.  */
    261   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    262     return false;
    263   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    264   return elf_tdata (abfd)->core != NULL;
    265 }
    266 
    267 char *
    268 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    269 {
    270   Elf_Internal_Shdr **i_shdrp;
    271   bfd_byte *shstrtab = NULL;
    272   file_ptr offset;
    273   bfd_size_type shstrtabsize;
    274 
    275   i_shdrp = elf_elfsections (abfd);
    276   if (i_shdrp == 0
    277       || shindex >= elf_numsections (abfd)
    278       || i_shdrp[shindex] == 0)
    279     return NULL;
    280 
    281   shstrtab = i_shdrp[shindex]->contents;
    282   if (shstrtab == NULL)
    283     {
    284       /* No cached one, attempt to read, and cache what we read.  */
    285       offset = i_shdrp[shindex]->sh_offset;
    286       shstrtabsize = i_shdrp[shindex]->sh_size;
    287 
    288       /* Allocate and clear an extra byte at the end, to prevent crashes
    289 	 in case the string table is not terminated.  */
    290       if (shstrtabsize + 1 <= 1
    291 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    292 	  || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
    293 					      shstrtabsize)) == NULL)
    294 	{
    295 	  /* Once we've failed to read it, make sure we don't keep
    296 	     trying.  Otherwise, we'll keep allocating space for
    297 	     the string table over and over.  */
    298 	  i_shdrp[shindex]->sh_size = 0;
    299 	}
    300       else
    301 	shstrtab[shstrtabsize] = '\0';
    302       i_shdrp[shindex]->contents = shstrtab;
    303     }
    304   return (char *) shstrtab;
    305 }
    306 
    307 char *
    308 bfd_elf_string_from_elf_section (bfd *abfd,
    309 				 unsigned int shindex,
    310 				 unsigned int strindex)
    311 {
    312   Elf_Internal_Shdr *hdr;
    313 
    314   if (strindex == 0)
    315     return "";
    316 
    317   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    318     return NULL;
    319 
    320   hdr = elf_elfsections (abfd)[shindex];
    321 
    322   if (hdr->contents == NULL)
    323     {
    324       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    325 	{
    326 	  /* PR 17512: file: f057ec89.  */
    327 	  /* xgettext:c-format */
    328 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    329 				" a non-string section (number %d)"),
    330 			      abfd, shindex);
    331 	  return NULL;
    332 	}
    333 
    334       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    335 	return NULL;
    336     }
    337   else
    338     {
    339       /* PR 24273: The string section's contents may have already
    340 	 been loaded elsewhere, eg because a corrupt file has the
    341 	 string section index in the ELF header pointing at a group
    342 	 section.  So be paranoid, and test that the last byte of
    343 	 the section is zero.  */
    344       if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
    345 	return NULL;
    346     }
    347 
    348   if (strindex >= hdr->sh_size)
    349     {
    350       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    351       _bfd_error_handler
    352 	/* xgettext:c-format */
    353 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    354 	 abfd, strindex, (uint64_t) hdr->sh_size,
    355 	 (shindex == shstrndx && strindex == hdr->sh_name
    356 	  ? ".shstrtab"
    357 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    358       return NULL;
    359     }
    360 
    361   return ((char *) hdr->contents) + strindex;
    362 }
    363 
    364 /* Read and convert symbols to internal format.
    365    SYMCOUNT specifies the number of symbols to read, starting from
    366    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    367    are non-NULL, they are used to store the internal symbols, external
    368    symbols, and symbol section index extensions, respectively.
    369    Returns a pointer to the internal symbol buffer (malloced if necessary)
    370    or NULL if there were no symbols or some kind of problem.  */
    371 
    372 Elf_Internal_Sym *
    373 bfd_elf_get_elf_syms (bfd *ibfd,
    374 		      Elf_Internal_Shdr *symtab_hdr,
    375 		      size_t symcount,
    376 		      size_t symoffset,
    377 		      Elf_Internal_Sym *intsym_buf,
    378 		      void *extsym_buf,
    379 		      Elf_External_Sym_Shndx *extshndx_buf)
    380 {
    381   Elf_Internal_Shdr *shndx_hdr;
    382   void *alloc_ext;
    383   const bfd_byte *esym;
    384   Elf_External_Sym_Shndx *alloc_extshndx;
    385   Elf_External_Sym_Shndx *shndx;
    386   Elf_Internal_Sym *alloc_intsym;
    387   Elf_Internal_Sym *isym;
    388   Elf_Internal_Sym *isymend;
    389   const struct elf_backend_data *bed;
    390   size_t extsym_size;
    391   size_t amt;
    392   file_ptr pos;
    393 
    394   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    395     abort ();
    396 
    397   if (symcount == 0)
    398     return intsym_buf;
    399 
    400   if (elf_use_dt_symtab_p (ibfd))
    401     {
    402       /* Use dynamic symbol table.  */
    403       if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
    404 	{
    405 	  bfd_set_error (bfd_error_invalid_operation);
    406 	  return NULL;
    407 	}
    408       return elf_tdata (ibfd)->dt_symtab + symoffset;
    409     }
    410 
    411   /* Normal syms might have section extension entries.  */
    412   shndx_hdr = NULL;
    413   if (elf_symtab_shndx_list (ibfd) != NULL)
    414     {
    415       elf_section_list * entry;
    416       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    417 
    418       /* Find an index section that is linked to this symtab section.  */
    419       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    420 	{
    421 	  /* PR 20063.  */
    422 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    423 	    continue;
    424 
    425 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    426 	    {
    427 	      shndx_hdr = & entry->hdr;
    428 	      break;
    429 	    };
    430 	}
    431 
    432       if (shndx_hdr == NULL)
    433 	{
    434 	  if (symtab_hdr == &elf_symtab_hdr (ibfd))
    435 	    /* Not really accurate, but this was how the old code used
    436 	       to work.  */
    437 	    shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
    438 	  /* Otherwise we do nothing.  The assumption is that
    439 	     the index table will not be needed.  */
    440 	}
    441     }
    442 
    443   /* Read the symbols.  */
    444   alloc_ext = NULL;
    445   alloc_extshndx = NULL;
    446   alloc_intsym = NULL;
    447   bed = get_elf_backend_data (ibfd);
    448   extsym_size = bed->s->sizeof_sym;
    449   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
    450     {
    451       bfd_set_error (bfd_error_file_too_big);
    452       intsym_buf = NULL;
    453       goto out;
    454     }
    455   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    456   if (extsym_buf == NULL)
    457     {
    458       alloc_ext = bfd_malloc (amt);
    459       extsym_buf = alloc_ext;
    460     }
    461   if (extsym_buf == NULL
    462       || bfd_seek (ibfd, pos, SEEK_SET) != 0
    463       || bfd_read (extsym_buf, amt, ibfd) != amt)
    464     {
    465       intsym_buf = NULL;
    466       goto out;
    467     }
    468 
    469   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    470     extshndx_buf = NULL;
    471   else
    472     {
    473       if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
    474 	{
    475 	  bfd_set_error (bfd_error_file_too_big);
    476 	  intsym_buf = NULL;
    477 	  goto out;
    478 	}
    479       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    480       if (extshndx_buf == NULL)
    481 	{
    482 	  alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
    483 	  extshndx_buf = alloc_extshndx;
    484 	}
    485       if (extshndx_buf == NULL
    486 	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
    487 	  || bfd_read (extshndx_buf, amt, ibfd) != amt)
    488 	{
    489 	  intsym_buf = NULL;
    490 	  goto out;
    491 	}
    492     }
    493 
    494   if (intsym_buf == NULL)
    495     {
    496       if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
    497 	{
    498 	  bfd_set_error (bfd_error_file_too_big);
    499 	  goto out;
    500 	}
    501       alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
    502       intsym_buf = alloc_intsym;
    503       if (intsym_buf == NULL)
    504 	goto out;
    505     }
    506 
    507   /* Convert the symbols to internal form.  */
    508   isymend = intsym_buf + symcount;
    509   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    510 	   shndx = extshndx_buf;
    511        isym < isymend;
    512        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    513     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    514       {
    515 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    516 	/* xgettext:c-format */
    517 	_bfd_error_handler (_("%pB symbol number %lu references"
    518 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    519 			    ibfd, (unsigned long) symoffset);
    520 	free (alloc_intsym);
    521 	intsym_buf = NULL;
    522 	goto out;
    523       }
    524 
    525  out:
    526   free (alloc_ext);
    527   free (alloc_extshndx);
    528 
    529   return intsym_buf;
    530 }
    531 
    532 /* Look up a symbol name.  */
    533 const char *
    534 bfd_elf_sym_name (bfd *abfd,
    535 		  Elf_Internal_Shdr *symtab_hdr,
    536 		  Elf_Internal_Sym *isym,
    537 		  asection *sym_sec)
    538 {
    539   const char *name;
    540   unsigned int iname = isym->st_name;
    541   unsigned int shindex = symtab_hdr->sh_link;
    542 
    543   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    544       /* Check for a bogus st_shndx to avoid crashing.  */
    545       && isym->st_shndx < elf_numsections (abfd))
    546     {
    547       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    548       shindex = elf_elfheader (abfd)->e_shstrndx;
    549     }
    550 
    551   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    552   if (name == NULL)
    553     name = "(null)";
    554   else if (sym_sec && *name == '\0')
    555     name = bfd_section_name (sym_sec);
    556 
    557   return name;
    558 }
    559 
    560 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    561    sections.  The first element is the flags, the rest are section
    562    pointers.  */
    563 
    564 typedef union elf_internal_group {
    565   Elf_Internal_Shdr *shdr;
    566   unsigned int flags;
    567 } Elf_Internal_Group;
    568 
    569 /* Return the name of the group signature symbol.  Why isn't the
    570    signature just a string?  */
    571 
    572 static const char *
    573 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    574 {
    575   Elf_Internal_Shdr *hdr;
    576   unsigned char esym[sizeof (Elf64_External_Sym)];
    577   Elf_External_Sym_Shndx eshndx;
    578   Elf_Internal_Sym isym;
    579 
    580   /* First we need to ensure the symbol table is available.  Make sure
    581      that it is a symbol table section.  */
    582   if (ghdr->sh_link >= elf_numsections (abfd))
    583     return NULL;
    584   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    585   if (hdr->sh_type != SHT_SYMTAB
    586       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    587     return NULL;
    588 
    589   /* Go read the symbol.  */
    590   hdr = &elf_tdata (abfd)->symtab_hdr;
    591   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    592 			    &isym, esym, &eshndx) == NULL)
    593     return NULL;
    594 
    595   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    596 }
    597 
    598 /* Set next_in_group list pointer, and group name for NEWSECT.  */
    599 
    600 static bool
    601 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    602 {
    603   unsigned int num_group = elf_tdata (abfd)->num_group;
    604 
    605   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    606      is set to -1 if there are no SHT_GROUP sections.  */
    607   if (num_group == 0)
    608     {
    609       unsigned int i, shnum;
    610 
    611       /* First count the number of groups.  If we have a SHT_GROUP
    612 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    613       shnum = elf_numsections (abfd);
    614       num_group = 0;
    615 
    616 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    617 	(   (shdr)->sh_type == SHT_GROUP		\
    618 	 && (shdr)->sh_size >= minsize			\
    619 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    620 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    621 
    622       for (i = 0; i < shnum; i++)
    623 	{
    624 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    625 
    626 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    627 	    num_group += 1;
    628 	}
    629 
    630       if (num_group == 0)
    631 	{
    632 	  num_group = (unsigned) -1;
    633 	  elf_tdata (abfd)->num_group = num_group;
    634 	  elf_tdata (abfd)->group_sect_ptr = NULL;
    635 	}
    636       else
    637 	{
    638 	  /* We keep a list of elf section headers for group sections,
    639 	     so we can find them quickly.  */
    640 	  size_t amt;
    641 
    642 	  elf_tdata (abfd)->num_group = num_group;
    643 	  amt = num_group * sizeof (Elf_Internal_Shdr *);
    644 	  elf_tdata (abfd)->group_sect_ptr
    645 	    = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
    646 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    647 	    return false;
    648 	  num_group = 0;
    649 
    650 	  for (i = 0; i < shnum; i++)
    651 	    {
    652 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    653 
    654 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    655 		{
    656 		  unsigned char *src;
    657 		  Elf_Internal_Group *dest;
    658 
    659 		  /* Make sure the group section has a BFD section
    660 		     attached to it.  */
    661 		  if (!bfd_section_from_shdr (abfd, i))
    662 		    return false;
    663 
    664 		  /* Add to list of sections.  */
    665 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    666 		  num_group += 1;
    667 
    668 		  /* Read the raw contents.  */
    669 		  BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
    670 		  shdr->contents = NULL;
    671 		  if (_bfd_mul_overflow (shdr->sh_size,
    672 					 sizeof (*dest) / 4, &amt)
    673 		      || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    674 		      || !(shdr->contents
    675 			   = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
    676 		    {
    677 		      _bfd_error_handler
    678 			/* xgettext:c-format */
    679 			(_("%pB: invalid size field in group section"
    680 			   " header: %#" PRIx64 ""),
    681 			 abfd, (uint64_t) shdr->sh_size);
    682 		      bfd_set_error (bfd_error_bad_value);
    683 		      -- num_group;
    684 		      continue;
    685 		    }
    686 
    687 		  /* Translate raw contents, a flag word followed by an
    688 		     array of elf section indices all in target byte order,
    689 		     to the flag word followed by an array of elf section
    690 		     pointers.  */
    691 		  src = shdr->contents + shdr->sh_size;
    692 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    693 
    694 		  while (1)
    695 		    {
    696 		      unsigned int idx;
    697 
    698 		      src -= 4;
    699 		      --dest;
    700 		      idx = H_GET_32 (abfd, src);
    701 		      if (src == shdr->contents)
    702 			{
    703 			  dest->shdr = NULL;
    704 			  dest->flags = idx;
    705 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    706 			    shdr->bfd_section->flags
    707 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    708 			  break;
    709 			}
    710 		      if (idx < shnum)
    711 			{
    712 			  dest->shdr = elf_elfsections (abfd)[idx];
    713 			  /* PR binutils/23199: All sections in a
    714 			     section group should be marked with
    715 			     SHF_GROUP.  But some tools generate
    716 			     broken objects without SHF_GROUP.  Fix
    717 			     them up here.  */
    718 			  dest->shdr->sh_flags |= SHF_GROUP;
    719 			}
    720 		      if (idx >= shnum
    721 			  || dest->shdr->sh_type == SHT_GROUP)
    722 			{
    723 			  _bfd_error_handler
    724 			    (_("%pB: invalid entry in SHT_GROUP section [%u]"),
    725 			       abfd, i);
    726 			  dest->shdr = NULL;
    727 			}
    728 		    }
    729 		}
    730 	    }
    731 
    732 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    733 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    734 	    {
    735 	      elf_tdata (abfd)->num_group = num_group;
    736 
    737 	      /* If all groups are invalid then fail.  */
    738 	      if (num_group == 0)
    739 		{
    740 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    741 		  elf_tdata (abfd)->num_group = num_group = -1;
    742 		  _bfd_error_handler
    743 		    (_("%pB: no valid group sections found"), abfd);
    744 		  bfd_set_error (bfd_error_bad_value);
    745 		}
    746 	    }
    747 	}
    748     }
    749 
    750   if (num_group != (unsigned) -1)
    751     {
    752       unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
    753       unsigned int j;
    754 
    755       for (j = 0; j < num_group; j++)
    756 	{
    757 	  /* Begin search from previous found group.  */
    758 	  unsigned i = (j + search_offset) % num_group;
    759 
    760 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    761 	  Elf_Internal_Group *idx;
    762 	  bfd_size_type n_elt;
    763 
    764 	  if (shdr == NULL)
    765 	    continue;
    766 
    767 	  idx = (Elf_Internal_Group *) shdr->contents;
    768 	  if (idx == NULL || shdr->sh_size < 4)
    769 	    {
    770 	      /* See PR 21957 for a reproducer.  */
    771 	      /* xgettext:c-format */
    772 	      _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
    773 				  abfd, shdr->bfd_section);
    774 	      elf_tdata (abfd)->group_sect_ptr[i] = NULL;
    775 	      bfd_set_error (bfd_error_bad_value);
    776 	      return false;
    777 	    }
    778 	  n_elt = shdr->sh_size / 4;
    779 
    780 	  /* Look through this group's sections to see if current
    781 	     section is a member.  */
    782 	  while (--n_elt != 0)
    783 	    if ((++idx)->shdr == hdr)
    784 	      {
    785 		asection *s = NULL;
    786 
    787 		/* We are a member of this group.  Go looking through
    788 		   other members to see if any others are linked via
    789 		   next_in_group.  */
    790 		idx = (Elf_Internal_Group *) shdr->contents;
    791 		n_elt = shdr->sh_size / 4;
    792 		while (--n_elt != 0)
    793 		  if ((++idx)->shdr != NULL
    794 		      && (s = idx->shdr->bfd_section) != NULL
    795 		      && elf_next_in_group (s) != NULL)
    796 		    break;
    797 		if (n_elt != 0)
    798 		  {
    799 		    /* Snarf the group name from other member, and
    800 		       insert current section in circular list.  */
    801 		    elf_group_name (newsect) = elf_group_name (s);
    802 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    803 		    elf_next_in_group (s) = newsect;
    804 		  }
    805 		else
    806 		  {
    807 		    const char *gname;
    808 
    809 		    gname = group_signature (abfd, shdr);
    810 		    if (gname == NULL)
    811 		      return false;
    812 		    elf_group_name (newsect) = gname;
    813 
    814 		    /* Start a circular list with one element.  */
    815 		    elf_next_in_group (newsect) = newsect;
    816 		  }
    817 
    818 		/* If the group section has been created, point to the
    819 		   new member.  */
    820 		if (shdr->bfd_section != NULL)
    821 		  elf_next_in_group (shdr->bfd_section) = newsect;
    822 
    823 		elf_tdata (abfd)->group_search_offset = i;
    824 		j = num_group - 1;
    825 		break;
    826 	      }
    827 	}
    828     }
    829 
    830   if (elf_group_name (newsect) == NULL)
    831     {
    832       /* xgettext:c-format */
    833       _bfd_error_handler (_("%pB: no group info for section '%pA'"),
    834 			  abfd, newsect);
    835       /* PR 29532: Return true here, even though the group info has not been
    836 	 read.  Separate debug info files can have empty group sections, but
    837 	 we do not want this to prevent them from being loaded as otherwise
    838 	 GDB will not be able to use them.  */
    839       return true;
    840     }
    841   return true;
    842 }
    843 
    844 bool
    845 _bfd_elf_setup_sections (bfd *abfd)
    846 {
    847   unsigned int i;
    848   unsigned int num_group = elf_tdata (abfd)->num_group;
    849   bool result = true;
    850   asection *s;
    851 
    852   /* Process SHF_LINK_ORDER.  */
    853   for (s = abfd->sections; s != NULL; s = s->next)
    854     {
    855       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    856       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    857 	{
    858 	  unsigned int elfsec = this_hdr->sh_link;
    859 	  /* An sh_link value of 0 is now allowed.  It indicates that linked
    860 	     to section has already been discarded, but that the current
    861 	     section has been retained for some other reason.  This linking
    862 	     section is still a candidate for later garbage collection
    863 	     however.  */
    864 	  if (elfsec == 0)
    865 	    {
    866 	      elf_linked_to_section (s) = NULL;
    867 	    }
    868 	  else
    869 	    {
    870 	      asection *linksec = NULL;
    871 
    872 	      if (elfsec < elf_numsections (abfd))
    873 		{
    874 		  this_hdr = elf_elfsections (abfd)[elfsec];
    875 		  linksec = this_hdr->bfd_section;
    876 		}
    877 
    878 	      /* PR 1991, 2008:
    879 		 Some strip/objcopy may leave an incorrect value in
    880 		 sh_link.  We don't want to proceed.  */
    881 	      if (linksec == NULL)
    882 		{
    883 		  _bfd_error_handler
    884 		    /* xgettext:c-format */
    885 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    886 		     s->owner, elfsec, s);
    887 		  result = false;
    888 		}
    889 
    890 	      elf_linked_to_section (s) = linksec;
    891 	    }
    892 	}
    893       else if (this_hdr->sh_type == SHT_GROUP
    894 	       && elf_next_in_group (s) == NULL)
    895 	{
    896 	  _bfd_error_handler
    897 	    /* xgettext:c-format */
    898 	    (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
    899 	     abfd, elf_section_data (s)->this_idx);
    900 	  result = false;
    901 	}
    902     }
    903 
    904   /* Process section groups.  */
    905   if (num_group == (unsigned) -1)
    906     return result;
    907 
    908   for (i = 0; i < num_group; i++)
    909     {
    910       Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    911       Elf_Internal_Group *idx;
    912       unsigned int n_elt;
    913 
    914       /* PR binutils/18758: Beware of corrupt binaries with invalid
    915 	 group data.  */
    916       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    917 	{
    918 	  _bfd_error_handler
    919 	    /* xgettext:c-format */
    920 	    (_("%pB: section group entry number %u is corrupt"),
    921 	     abfd, i);
    922 	  result = false;
    923 	  continue;
    924 	}
    925 
    926       idx = (Elf_Internal_Group *) shdr->contents;
    927       n_elt = shdr->sh_size / 4;
    928 
    929       while (--n_elt != 0)
    930 	{
    931 	  ++ idx;
    932 
    933 	  if (idx->shdr == NULL)
    934 	    continue;
    935 	  else if (idx->shdr->bfd_section)
    936 	    elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    937 	  else if (idx->shdr->sh_type != SHT_RELA
    938 		   && idx->shdr->sh_type != SHT_REL)
    939 	    {
    940 	      /* There are some unknown sections in the group.  */
    941 	      _bfd_error_handler
    942 		/* xgettext:c-format */
    943 		(_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
    944 		 abfd,
    945 		 idx->shdr->sh_type,
    946 		 bfd_elf_string_from_elf_section (abfd,
    947 						  (elf_elfheader (abfd)
    948 						   ->e_shstrndx),
    949 						  idx->shdr->sh_name),
    950 		 shdr->bfd_section);
    951 	      result = false;
    952 	    }
    953 	}
    954     }
    955 
    956   return result;
    957 }
    958 
    959 bool
    960 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    961 {
    962   return elf_next_in_group (sec) != NULL;
    963 }
    964 
    965 const char *
    966 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    967 {
    968   if (elf_sec_group (sec) != NULL)
    969     return elf_group_name (sec);
    970   return NULL;
    971 }
    972 
    973 /* This a copy of lto_section defined in GCC (lto-streamer.h).  */
    974 
    975 struct lto_section
    976 {
    977   int16_t major_version;
    978   int16_t minor_version;
    979   unsigned char slim_object;
    980 
    981   /* Flags is a private field that is not defined publicly.  */
    982   uint16_t flags;
    983 };
    984 
    985 /* Make a BFD section from an ELF section.  We store a pointer to the
    986    BFD section in the bfd_section field of the header.  */
    987 
    988 bool
    989 _bfd_elf_make_section_from_shdr (bfd *abfd,
    990 				 Elf_Internal_Shdr *hdr,
    991 				 const char *name,
    992 				 int shindex)
    993 {
    994   asection *newsect;
    995   flagword flags;
    996   const struct elf_backend_data *bed;
    997   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
    998 
    999   if (hdr->bfd_section != NULL)
   1000     return true;
   1001 
   1002   newsect = bfd_make_section_anyway (abfd, name);
   1003   if (newsect == NULL)
   1004     return false;
   1005 
   1006   hdr->bfd_section = newsect;
   1007   elf_section_data (newsect)->this_hdr = *hdr;
   1008   elf_section_data (newsect)->this_idx = shindex;
   1009 
   1010   /* Always use the real type/flags.  */
   1011   elf_section_type (newsect) = hdr->sh_type;
   1012   elf_section_flags (newsect) = hdr->sh_flags;
   1013 
   1014   newsect->filepos = hdr->sh_offset;
   1015 
   1016   flags = SEC_NO_FLAGS;
   1017   if (hdr->sh_type != SHT_NOBITS)
   1018     flags |= SEC_HAS_CONTENTS;
   1019   if (hdr->sh_type == SHT_GROUP)
   1020     flags |= SEC_GROUP;
   1021   if ((hdr->sh_flags & SHF_ALLOC) != 0)
   1022     {
   1023       flags |= SEC_ALLOC;
   1024       if (hdr->sh_type != SHT_NOBITS)
   1025 	flags |= SEC_LOAD;
   1026     }
   1027   if ((hdr->sh_flags & SHF_WRITE) == 0)
   1028     flags |= SEC_READONLY;
   1029   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
   1030     flags |= SEC_CODE;
   1031   else if ((flags & SEC_LOAD) != 0)
   1032     flags |= SEC_DATA;
   1033   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1034     {
   1035       flags |= SEC_MERGE;
   1036       newsect->entsize = hdr->sh_entsize;
   1037     }
   1038   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1039     flags |= SEC_STRINGS;
   1040   if (hdr->sh_flags & SHF_GROUP)
   1041     if (!setup_group (abfd, hdr, newsect))
   1042       return false;
   1043   if ((hdr->sh_flags & SHF_TLS) != 0)
   1044     flags |= SEC_THREAD_LOCAL;
   1045   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1046     flags |= SEC_EXCLUDE;
   1047 
   1048   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
   1049     {
   1050       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
   1051 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
   1052 	 byte.  */
   1053     case ELFOSABI_GNU:
   1054     case ELFOSABI_FREEBSD:
   1055       if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
   1056 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
   1057       /* Fall through */
   1058     case ELFOSABI_NONE:
   1059       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
   1060 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
   1061       break;
   1062     }
   1063 
   1064   if ((flags & SEC_ALLOC) == 0)
   1065     {
   1066       /* The debugging sections appear to be recognized only by name,
   1067 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1068       if (name [0] == '.')
   1069 	{
   1070 	  if (startswith (name, ".debug")
   1071 	      || startswith (name, ".gnu.debuglto_.debug_")
   1072 	      || startswith (name, ".gnu.linkonce.wi.")
   1073 	      || startswith (name, ".zdebug"))
   1074 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
   1075 	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
   1076 		   || startswith (name, ".note.gnu"))
   1077 	    {
   1078 	      flags |= SEC_ELF_OCTETS;
   1079 	      opb = 1;
   1080 	    }
   1081 	  else if (startswith (name, ".line")
   1082 		   || startswith (name, ".stab")
   1083 		   || strcmp (name, ".gdb_index") == 0)
   1084 	    flags |= SEC_DEBUGGING;
   1085 	}
   1086     }
   1087 
   1088   if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
   1089       || !bfd_set_section_size (newsect, hdr->sh_size)
   1090       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
   1091 							& -hdr->sh_addralign)))
   1092     return false;
   1093 
   1094   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1095      only link a single copy of the section.  This is used to support
   1096      g++.  g++ will emit each template expansion in its own section.
   1097      The symbols will be defined as weak, so that multiple definitions
   1098      are permitted.  The GNU linker extension is to actually discard
   1099      all but one of the sections.  */
   1100   if (startswith (name, ".gnu.linkonce")
   1101       && elf_next_in_group (newsect) == NULL)
   1102     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1103 
   1104   if (!bfd_set_section_flags (newsect, flags))
   1105     return false;
   1106 
   1107   bed = get_elf_backend_data (abfd);
   1108   if (bed->elf_backend_section_flags)
   1109     if (!bed->elf_backend_section_flags (hdr))
   1110       return false;
   1111 
   1112   /* We do not parse the PT_NOTE segments as we are interested even in the
   1113      separate debug info files which may have the segments offsets corrupted.
   1114      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1115   if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
   1116     {
   1117       bfd_byte *contents;
   1118 
   1119       if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
   1120 	return false;
   1121 
   1122       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1123 		       hdr->sh_offset, hdr->sh_addralign);
   1124       free (contents);
   1125     }
   1126 
   1127   if ((newsect->flags & SEC_ALLOC) != 0)
   1128     {
   1129       Elf_Internal_Phdr *phdr;
   1130       unsigned int i, nload;
   1131 
   1132       /* Some ELF linkers produce binaries with all the program header
   1133 	 p_paddr fields zero.  If we have such a binary with more than
   1134 	 one PT_LOAD header, then leave the section lma equal to vma
   1135 	 so that we don't create sections with overlapping lma.  */
   1136       phdr = elf_tdata (abfd)->phdr;
   1137       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1138 	if (phdr->p_paddr != 0)
   1139 	  break;
   1140 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1141 	  ++nload;
   1142       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1143 	return true;
   1144 
   1145       phdr = elf_tdata (abfd)->phdr;
   1146       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1147 	{
   1148 	  if (((phdr->p_type == PT_LOAD
   1149 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1150 	       || phdr->p_type == PT_TLS)
   1151 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1152 	    {
   1153 	      if ((newsect->flags & SEC_LOAD) == 0)
   1154 		newsect->lma = (phdr->p_paddr
   1155 				+ hdr->sh_addr - phdr->p_vaddr) / opb;
   1156 	      else
   1157 		/* We used to use the same adjustment for SEC_LOAD
   1158 		   sections, but that doesn't work if the segment
   1159 		   is packed with code from multiple VMAs.
   1160 		   Instead we calculate the section LMA based on
   1161 		   the segment LMA.  It is assumed that the
   1162 		   segment will contain sections with contiguous
   1163 		   LMAs, even if the VMAs are not.  */
   1164 		newsect->lma = (phdr->p_paddr
   1165 				+ hdr->sh_offset - phdr->p_offset) / opb;
   1166 
   1167 	      /* With contiguous segments, we can't tell from file
   1168 		 offsets whether a section with zero size should
   1169 		 be placed at the end of one segment or the
   1170 		 beginning of the next.  Decide based on vaddr.  */
   1171 	      if (hdr->sh_addr >= phdr->p_vaddr
   1172 		  && (hdr->sh_addr + hdr->sh_size
   1173 		      <= phdr->p_vaddr + phdr->p_memsz))
   1174 		break;
   1175 	    }
   1176 	}
   1177     }
   1178 
   1179   /* Compress/decompress DWARF debug sections with names: .debug_*,
   1180      .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
   1181   if ((newsect->flags & SEC_DEBUGGING) != 0
   1182       && (newsect->flags & SEC_HAS_CONTENTS) != 0
   1183       && (newsect->flags & SEC_ELF_OCTETS) != 0)
   1184     {
   1185       enum { nothing, compress, decompress } action = nothing;
   1186       int compression_header_size;
   1187       bfd_size_type uncompressed_size;
   1188       unsigned int uncompressed_align_power;
   1189       enum compression_type ch_type = ch_none;
   1190       bool compressed
   1191 	= bfd_is_section_compressed_info (abfd, newsect,
   1192 					  &compression_header_size,
   1193 					  &uncompressed_size,
   1194 					  &uncompressed_align_power,
   1195 					  &ch_type);
   1196 
   1197       /* Should we decompress?  */
   1198       if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
   1199 	action = decompress;
   1200 
   1201       /* Should we compress?  Or convert to a different compression?  */
   1202       else if ((abfd->flags & BFD_COMPRESS) != 0
   1203 	       && newsect->size != 0
   1204 	       && compression_header_size >= 0
   1205 	       && uncompressed_size > 0)
   1206 	{
   1207 	  if (!compressed)
   1208 	    action = compress;
   1209 	  else
   1210 	    {
   1211 	      enum compression_type new_ch_type = ch_none;
   1212 	      if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
   1213 		new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
   1214 			       ? ch_compress_zstd : ch_compress_zlib);
   1215 	      if (new_ch_type != ch_type)
   1216 		action = compress;
   1217 	    }
   1218 	}
   1219 
   1220       if (action == compress)
   1221 	{
   1222 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1223 	    {
   1224 	      _bfd_error_handler
   1225 		/* xgettext:c-format */
   1226 		(_("%pB: unable to compress section %s"), abfd, name);
   1227 	      return false;
   1228 	    }
   1229 	}
   1230       else if (action == decompress)
   1231 	{
   1232 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1233 	    {
   1234 	      _bfd_error_handler
   1235 		/* xgettext:c-format */
   1236 		(_("%pB: unable to decompress section %s"), abfd, name);
   1237 	      return false;
   1238 	    }
   1239 #ifndef HAVE_ZSTD
   1240 	  if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
   1241 	    {
   1242 	      _bfd_error_handler
   1243 		  /* xgettext:c-format */
   1244 		  (_ ("%pB: section %s is compressed with zstd, but BFD "
   1245 		      "is not built with zstd support"),
   1246 		   abfd, name);
   1247 	      newsect->compress_status = COMPRESS_SECTION_NONE;
   1248 	      return false;
   1249 	    }
   1250 #endif
   1251 	  if (abfd->is_linker_input
   1252 	      && name[1] == 'z')
   1253 	    {
   1254 	      /* Rename section from .zdebug_* to .debug_* so that ld
   1255 		 scripts will see this section as a debug section.  */
   1256 	      char *new_name = bfd_zdebug_name_to_debug (abfd, name);
   1257 	      if (new_name == NULL)
   1258 		return false;
   1259 	      bfd_rename_section (newsect, new_name);
   1260 	    }
   1261 	}
   1262     }
   1263 
   1264   /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
   1265      section.  */
   1266   if (startswith (name, ".gnu.lto_.lto."))
   1267     {
   1268       struct lto_section lsection;
   1269       if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
   1270 				    sizeof (struct lto_section)))
   1271 	abfd->lto_slim_object = lsection.slim_object;
   1272     }
   1273 
   1274   return true;
   1275 }
   1276 
   1277 const char *const bfd_elf_section_type_names[] =
   1278 {
   1279   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1280   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1281   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1282 };
   1283 
   1284 /* ELF relocs are against symbols.  If we are producing relocatable
   1285    output, and the reloc is against an external symbol, and nothing
   1286    has given us any additional addend, the resulting reloc will also
   1287    be against the same symbol.  In such a case, we don't want to
   1288    change anything about the way the reloc is handled, since it will
   1289    all be done at final link time.  Rather than put special case code
   1290    into bfd_perform_relocation, all the reloc types use this howto
   1291    function, or should call this function for relocatable output.  */
   1292 
   1293 bfd_reloc_status_type
   1294 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1295 		       arelent *reloc_entry,
   1296 		       asymbol *symbol,
   1297 		       void *data ATTRIBUTE_UNUSED,
   1298 		       asection *input_section,
   1299 		       bfd *output_bfd,
   1300 		       char **error_message ATTRIBUTE_UNUSED)
   1301 {
   1302   if (output_bfd != NULL
   1303       && (symbol->flags & BSF_SECTION_SYM) == 0
   1304       && (! reloc_entry->howto->partial_inplace
   1305 	  || reloc_entry->addend == 0))
   1306     {
   1307       reloc_entry->address += input_section->output_offset;
   1308       return bfd_reloc_ok;
   1309     }
   1310 
   1311   /* In some cases the relocation should be treated as output section
   1312      relative, as when linking ELF DWARF into PE COFF.  Many ELF
   1313      targets lack section relative relocations and instead use
   1314      ordinary absolute relocations for references between DWARF
   1315      sections.  That is arguably a bug in those targets but it happens
   1316      to work for the usual case of linking to non-loaded ELF debug
   1317      sections with VMAs forced to zero.  PE COFF on the other hand
   1318      doesn't allow a section VMA of zero.  */
   1319   if (output_bfd == NULL
   1320       && !reloc_entry->howto->pc_relative
   1321       && (symbol->section->flags & SEC_DEBUGGING) != 0
   1322       && (input_section->flags & SEC_DEBUGGING) != 0)
   1323     reloc_entry->addend -= symbol->section->output_section->vma;
   1324 
   1325   return bfd_reloc_continue;
   1326 }
   1327 
   1328 /* Returns TRUE if section A matches section B.
   1330    Names, addresses and links may be different, but everything else
   1331    should be the same.  */
   1332 
   1333 static bool
   1334 section_match (const Elf_Internal_Shdr * a,
   1335 	       const Elf_Internal_Shdr * b)
   1336 {
   1337   if (a->sh_type != b->sh_type
   1338       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1339       || a->sh_addralign != b->sh_addralign
   1340       || a->sh_entsize != b->sh_entsize)
   1341     return false;
   1342   if (a->sh_type == SHT_SYMTAB
   1343       || a->sh_type == SHT_STRTAB)
   1344     return true;
   1345   return a->sh_size == b->sh_size;
   1346 }
   1347 
   1348 /* Find a section in OBFD that has the same characteristics
   1349    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1350    none can be found.  Check's section HINT first, as this is likely
   1351    to be the correct section.  */
   1352 
   1353 static unsigned int
   1354 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1355 	   const unsigned int hint)
   1356 {
   1357   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1358   unsigned int i;
   1359 
   1360   BFD_ASSERT (iheader != NULL);
   1361 
   1362   /* See PR 20922 for a reproducer of the NULL test.  */
   1363   if (hint < elf_numsections (obfd)
   1364       && oheaders[hint] != NULL
   1365       && section_match (oheaders[hint], iheader))
   1366     return hint;
   1367 
   1368   for (i = 1; i < elf_numsections (obfd); i++)
   1369     {
   1370       Elf_Internal_Shdr * oheader = oheaders[i];
   1371 
   1372       if (oheader == NULL)
   1373 	continue;
   1374       if (section_match (oheader, iheader))
   1375 	/* FIXME: Do we care if there is a potential for
   1376 	   multiple matches ?  */
   1377 	return i;
   1378     }
   1379 
   1380   return SHN_UNDEF;
   1381 }
   1382 
   1383 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1384    Processor specific section, based upon a matching input section.
   1385    Returns TRUE upon success, FALSE otherwise.  */
   1386 
   1387 static bool
   1388 copy_special_section_fields (const bfd *ibfd,
   1389 			     bfd *obfd,
   1390 			     const Elf_Internal_Shdr *iheader,
   1391 			     Elf_Internal_Shdr *oheader,
   1392 			     const unsigned int secnum)
   1393 {
   1394   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1395   const Elf_Internal_Shdr **iheaders
   1396     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1397   bool changed = false;
   1398   unsigned int sh_link;
   1399 
   1400   if (oheader->sh_type == SHT_NOBITS)
   1401     {
   1402       /* This is a feature for objcopy --only-keep-debug:
   1403 	 When a section's type is changed to NOBITS, we preserve
   1404 	 the sh_link and sh_info fields so that they can be
   1405 	 matched up with the original.
   1406 
   1407 	 Note: Strictly speaking these assignments are wrong.
   1408 	 The sh_link and sh_info fields should point to the
   1409 	 relevent sections in the output BFD, which may not be in
   1410 	 the same location as they were in the input BFD.  But
   1411 	 the whole point of this action is to preserve the
   1412 	 original values of the sh_link and sh_info fields, so
   1413 	 that they can be matched up with the section headers in
   1414 	 the original file.  So strictly speaking we may be
   1415 	 creating an invalid ELF file, but it is only for a file
   1416 	 that just contains debug info and only for sections
   1417 	 without any contents.  */
   1418       if (oheader->sh_link == 0)
   1419 	oheader->sh_link = iheader->sh_link;
   1420       if (oheader->sh_info == 0)
   1421 	oheader->sh_info = iheader->sh_info;
   1422       return true;
   1423     }
   1424 
   1425   /* Allow the target a chance to decide how these fields should be set.  */
   1426   if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1427 						    iheader, oheader))
   1428     return true;
   1429 
   1430   /* We have an iheader which might match oheader, and which has non-zero
   1431      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1432      the section in the output bfd which corresponds to the linked section
   1433      in the input bfd.  */
   1434   if (iheader->sh_link != SHN_UNDEF)
   1435     {
   1436       /* See PR 20931 for a reproducer.  */
   1437       if (iheader->sh_link >= elf_numsections (ibfd))
   1438 	{
   1439 	  _bfd_error_handler
   1440 	    /* xgettext:c-format */
   1441 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1442 	     ibfd, iheader->sh_link, secnum);
   1443 	  return false;
   1444 	}
   1445 
   1446       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1447       if (sh_link != SHN_UNDEF)
   1448 	{
   1449 	  oheader->sh_link = sh_link;
   1450 	  changed = true;
   1451 	}
   1452       else
   1453 	/* FIXME: Should we install iheader->sh_link
   1454 	   if we could not find a match ?  */
   1455 	_bfd_error_handler
   1456 	  /* xgettext:c-format */
   1457 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1458     }
   1459 
   1460   if (iheader->sh_info)
   1461     {
   1462       /* The sh_info field can hold arbitrary information, but if the
   1463 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1464 	 section index.  */
   1465       if (iheader->sh_flags & SHF_INFO_LINK)
   1466 	{
   1467 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1468 			       iheader->sh_info);
   1469 	  if (sh_link != SHN_UNDEF)
   1470 	    oheader->sh_flags |= SHF_INFO_LINK;
   1471 	}
   1472       else
   1473 	/* No idea what it means - just copy it.  */
   1474 	sh_link = iheader->sh_info;
   1475 
   1476       if (sh_link != SHN_UNDEF)
   1477 	{
   1478 	  oheader->sh_info = sh_link;
   1479 	  changed = true;
   1480 	}
   1481       else
   1482 	_bfd_error_handler
   1483 	  /* xgettext:c-format */
   1484 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1485     }
   1486 
   1487   return changed;
   1488 }
   1489 
   1490 /* Copy the program header and other data from one object module to
   1491    another.  */
   1492 
   1493 bool
   1494 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1495 {
   1496   const Elf_Internal_Shdr **iheaders
   1497     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1498   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1499   const struct elf_backend_data *bed;
   1500   unsigned int i;
   1501 
   1502   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1503     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1504     return true;
   1505 
   1506   if (!elf_flags_init (obfd))
   1507     {
   1508       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1509       elf_flags_init (obfd) = true;
   1510     }
   1511 
   1512   elf_gp (obfd) = elf_gp (ibfd);
   1513 
   1514   /* Also copy the EI_OSABI field.  */
   1515   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1516     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1517 
   1518   /* If set, copy the EI_ABIVERSION field.  */
   1519   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1520     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1521       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1522 
   1523   /* Copy object attributes.  */
   1524   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1525 
   1526   if (iheaders == NULL || oheaders == NULL)
   1527     return true;
   1528 
   1529   bed = get_elf_backend_data (obfd);
   1530 
   1531   /* Possibly copy other fields in the section header.  */
   1532   for (i = 1; i < elf_numsections (obfd); i++)
   1533     {
   1534       unsigned int j;
   1535       Elf_Internal_Shdr * oheader = oheaders[i];
   1536 
   1537       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1538 	 because of a special case need for generating separate debug info
   1539 	 files.  See below for more details.  */
   1540       if (oheader == NULL
   1541 	  || (oheader->sh_type != SHT_NOBITS
   1542 	      && oheader->sh_type < SHT_LOOS))
   1543 	continue;
   1544 
   1545       /* Ignore empty sections, and sections whose
   1546 	 fields have already been initialised.  */
   1547       if (oheader->sh_size == 0
   1548 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1549 	continue;
   1550 
   1551       /* Scan for the matching section in the input bfd.
   1552 	 First we try for a direct mapping between the input and
   1553 	 output sections.  */
   1554       for (j = 1; j < elf_numsections (ibfd); j++)
   1555 	{
   1556 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1557 
   1558 	  if (iheader == NULL)
   1559 	    continue;
   1560 
   1561 	  if (oheader->bfd_section != NULL
   1562 	      && iheader->bfd_section != NULL
   1563 	      && iheader->bfd_section->output_section != NULL
   1564 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1565 	    {
   1566 	      /* We have found a connection from the input section to
   1567 		 the output section.  Attempt to copy the header fields.
   1568 		 If this fails then do not try any further sections -
   1569 		 there should only be a one-to-one mapping between
   1570 		 input and output.  */
   1571 	      if (!copy_special_section_fields (ibfd, obfd,
   1572 						iheader, oheader, i))
   1573 		j = elf_numsections (ibfd);
   1574 	      break;
   1575 	    }
   1576 	}
   1577 
   1578       if (j < elf_numsections (ibfd))
   1579 	continue;
   1580 
   1581       /* That failed.  So try to deduce the corresponding input section.
   1582 	 Unfortunately we cannot compare names as the output string table
   1583 	 is empty, so instead we check size, address and type.  */
   1584       for (j = 1; j < elf_numsections (ibfd); j++)
   1585 	{
   1586 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1587 
   1588 	  if (iheader == NULL)
   1589 	    continue;
   1590 
   1591 	  /* Try matching fields in the input section's header.
   1592 	     Since --only-keep-debug turns all non-debug sections into
   1593 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1594 	     input type.  */
   1595 	  if ((oheader->sh_type == SHT_NOBITS
   1596 	       || iheader->sh_type == oheader->sh_type)
   1597 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1598 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1599 	      && iheader->sh_addralign == oheader->sh_addralign
   1600 	      && iheader->sh_entsize == oheader->sh_entsize
   1601 	      && iheader->sh_size == oheader->sh_size
   1602 	      && iheader->sh_addr == oheader->sh_addr
   1603 	      && (iheader->sh_info != oheader->sh_info
   1604 		  || iheader->sh_link != oheader->sh_link))
   1605 	    {
   1606 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1607 		break;
   1608 	    }
   1609 	}
   1610 
   1611       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1612 	{
   1613 	  /* Final attempt.  Call the backend copy function
   1614 	     with a NULL input section.  */
   1615 	  (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1616 							       NULL, oheader);
   1617 	}
   1618     }
   1619 
   1620   return true;
   1621 }
   1622 
   1623 static const char *
   1624 get_segment_type (unsigned int p_type)
   1625 {
   1626   const char *pt;
   1627   switch (p_type)
   1628     {
   1629     case PT_NULL: pt = "NULL"; break;
   1630     case PT_LOAD: pt = "LOAD"; break;
   1631     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1632     case PT_INTERP: pt = "INTERP"; break;
   1633     case PT_NOTE: pt = "NOTE"; break;
   1634     case PT_SHLIB: pt = "SHLIB"; break;
   1635     case PT_PHDR: pt = "PHDR"; break;
   1636     case PT_TLS: pt = "TLS"; break;
   1637     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1638     case PT_GNU_STACK: pt = "STACK"; break;
   1639     case PT_GNU_RELRO: pt = "RELRO"; break;
   1640     case PT_GNU_SFRAME: pt = "SFRAME"; break;
   1641     default: pt = NULL; break;
   1642     }
   1643   return pt;
   1644 }
   1645 
   1646 /* Print out the program headers.  */
   1647 
   1648 bool
   1649 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1650 {
   1651   FILE *f = (FILE *) farg;
   1652   Elf_Internal_Phdr *p;
   1653   asection *s;
   1654   bfd_byte *dynbuf = NULL;
   1655 
   1656   p = elf_tdata (abfd)->phdr;
   1657   if (p != NULL)
   1658     {
   1659       unsigned int i, c;
   1660 
   1661       fprintf (f, _("\nProgram Header:\n"));
   1662       c = elf_elfheader (abfd)->e_phnum;
   1663       for (i = 0; i < c; i++, p++)
   1664 	{
   1665 	  const char *pt = get_segment_type (p->p_type);
   1666 	  char buf[20];
   1667 
   1668 	  if (pt == NULL)
   1669 	    {
   1670 	      sprintf (buf, "0x%lx", p->p_type);
   1671 	      pt = buf;
   1672 	    }
   1673 	  fprintf (f, "%8s off    0x", pt);
   1674 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1675 	  fprintf (f, " vaddr 0x");
   1676 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1677 	  fprintf (f, " paddr 0x");
   1678 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1679 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1680 	  fprintf (f, "         filesz 0x");
   1681 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1682 	  fprintf (f, " memsz 0x");
   1683 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1684 	  fprintf (f, " flags %c%c%c",
   1685 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1686 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1687 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1688 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1689 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1690 	  fprintf (f, "\n");
   1691 	}
   1692     }
   1693 
   1694   s = bfd_get_section_by_name (abfd, ".dynamic");
   1695   if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
   1696     {
   1697       unsigned int elfsec;
   1698       unsigned long shlink;
   1699       bfd_byte *extdyn, *extdynend;
   1700       size_t extdynsize;
   1701       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1702 
   1703       fprintf (f, _("\nDynamic Section:\n"));
   1704 
   1705       if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   1706 	goto error_return;
   1707 
   1708       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1709       if (elfsec == SHN_BAD)
   1710 	goto error_return;
   1711       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1712 
   1713       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1714       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1715 
   1716       for (extdyn = dynbuf, extdynend = dynbuf + s->size;
   1717 	   (size_t) (extdynend - extdyn) >= extdynsize;
   1718 	   extdyn += extdynsize)
   1719 	{
   1720 	  Elf_Internal_Dyn dyn;
   1721 	  const char *name = "";
   1722 	  char ab[20];
   1723 	  bool stringp;
   1724 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1725 
   1726 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1727 
   1728 	  if (dyn.d_tag == DT_NULL)
   1729 	    break;
   1730 
   1731 	  stringp = false;
   1732 	  switch (dyn.d_tag)
   1733 	    {
   1734 	    default:
   1735 	      if (bed->elf_backend_get_target_dtag)
   1736 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1737 
   1738 	      if (!strcmp (name, ""))
   1739 		{
   1740 		  sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
   1741 		  name = ab;
   1742 		}
   1743 	      break;
   1744 
   1745 	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
   1746 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1747 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1748 	    case DT_HASH: name = "HASH"; break;
   1749 	    case DT_STRTAB: name = "STRTAB"; break;
   1750 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1751 	    case DT_RELA: name = "RELA"; break;
   1752 	    case DT_RELASZ: name = "RELASZ"; break;
   1753 	    case DT_RELAENT: name = "RELAENT"; break;
   1754 	    case DT_STRSZ: name = "STRSZ"; break;
   1755 	    case DT_SYMENT: name = "SYMENT"; break;
   1756 	    case DT_INIT: name = "INIT"; break;
   1757 	    case DT_FINI: name = "FINI"; break;
   1758 	    case DT_SONAME: name = "SONAME"; stringp = true; break;
   1759 	    case DT_RPATH: name = "RPATH"; stringp = true; break;
   1760 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1761 	    case DT_REL: name = "REL"; break;
   1762 	    case DT_RELSZ: name = "RELSZ"; break;
   1763 	    case DT_RELENT: name = "RELENT"; break;
   1764 	    case DT_RELR: name = "RELR"; break;
   1765 	    case DT_RELRSZ: name = "RELRSZ"; break;
   1766 	    case DT_RELRENT: name = "RELRENT"; break;
   1767 	    case DT_PLTREL: name = "PLTREL"; break;
   1768 	    case DT_DEBUG: name = "DEBUG"; break;
   1769 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1770 	    case DT_JMPREL: name = "JMPREL"; break;
   1771 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1772 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1773 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1774 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1775 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1776 	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
   1777 	    case DT_FLAGS: name = "FLAGS"; break;
   1778 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1779 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1780 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1781 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1782 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1783 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1784 	    case DT_FEATURE: name = "FEATURE"; break;
   1785 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1786 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1787 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1788 	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
   1789 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
   1790 	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
   1791 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1792 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1793 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1794 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1795 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1796 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1797 	    case DT_VERSYM: name = "VERSYM"; break;
   1798 	    case DT_VERDEF: name = "VERDEF"; break;
   1799 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1800 	    case DT_VERNEED: name = "VERNEED"; break;
   1801 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1802 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
   1803 	    case DT_USED: name = "USED"; break;
   1804 	    case DT_FILTER: name = "FILTER"; stringp = true; break;
   1805 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1806 	    }
   1807 
   1808 	  fprintf (f, "  %-20s ", name);
   1809 	  if (! stringp)
   1810 	    {
   1811 	      fprintf (f, "0x");
   1812 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1813 	    }
   1814 	  else
   1815 	    {
   1816 	      const char *string;
   1817 	      unsigned int tagv = dyn.d_un.d_val;
   1818 
   1819 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1820 	      if (string == NULL)
   1821 		goto error_return;
   1822 	      fprintf (f, "%s", string);
   1823 	    }
   1824 	  fprintf (f, "\n");
   1825 	}
   1826 
   1827       free (dynbuf);
   1828       dynbuf = NULL;
   1829     }
   1830 
   1831   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1832       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1833     {
   1834       if (! _bfd_elf_slurp_version_tables (abfd, false))
   1835 	return false;
   1836     }
   1837 
   1838   if (elf_dynverdef (abfd) != 0)
   1839     {
   1840       Elf_Internal_Verdef *t;
   1841 
   1842       fprintf (f, _("\nVersion definitions:\n"));
   1843       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1844 	{
   1845 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1846 		   t->vd_flags, t->vd_hash,
   1847 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1848 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1849 	    {
   1850 	      Elf_Internal_Verdaux *a;
   1851 
   1852 	      fprintf (f, "\t");
   1853 	      for (a = t->vd_auxptr->vda_nextptr;
   1854 		   a != NULL;
   1855 		   a = a->vda_nextptr)
   1856 		fprintf (f, "%s ",
   1857 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1858 	      fprintf (f, "\n");
   1859 	    }
   1860 	}
   1861     }
   1862 
   1863   if (elf_dynverref (abfd) != 0)
   1864     {
   1865       Elf_Internal_Verneed *t;
   1866 
   1867       fprintf (f, _("\nVersion References:\n"));
   1868       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1869 	{
   1870 	  Elf_Internal_Vernaux *a;
   1871 
   1872 	  fprintf (f, _("  required from %s:\n"),
   1873 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1874 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1875 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1876 		     a->vna_flags, a->vna_other,
   1877 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1878 	}
   1879     }
   1880 
   1881   return true;
   1882 
   1883  error_return:
   1884   free (dynbuf);
   1885   return false;
   1886 }
   1887 
   1888 /* Find the file offset corresponding to VMA by using the program
   1889    headers.  */
   1890 
   1891 static file_ptr
   1892 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
   1893 		 size_t size, size_t *max_size_p)
   1894 {
   1895   Elf_Internal_Phdr *seg;
   1896   size_t i;
   1897 
   1898   for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
   1899     if (seg->p_type == PT_LOAD
   1900 	&& vma >= (seg->p_vaddr & -seg->p_align)
   1901 	&& vma + size <= seg->p_vaddr + seg->p_filesz)
   1902       {
   1903 	if (max_size_p)
   1904 	  *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
   1905 	return vma - seg->p_vaddr + seg->p_offset;
   1906       }
   1907 
   1908   if (max_size_p)
   1909     *max_size_p = 0;
   1910   bfd_set_error (bfd_error_invalid_operation);
   1911   return (file_ptr) -1;
   1912 }
   1913 
   1914 /* Convert hash table to internal form.  */
   1915 
   1916 static bfd_vma *
   1917 get_hash_table_data (bfd *abfd, bfd_size_type number,
   1918 		     unsigned int ent_size, bfd_size_type filesize)
   1919 {
   1920   unsigned char *e_data = NULL;
   1921   bfd_vma *i_data = NULL;
   1922   bfd_size_type size;
   1923 
   1924   if (ent_size != 4 && ent_size != 8)
   1925     return NULL;
   1926 
   1927   if ((size_t) number != number)
   1928     {
   1929       bfd_set_error (bfd_error_file_too_big);
   1930       return NULL;
   1931     }
   1932 
   1933   size = ent_size * number;
   1934   /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
   1935      attempting to allocate memory when the read is bound to fail.  */
   1936   if (size > filesize
   1937       || number >= ~(size_t) 0 / ent_size
   1938       || number >= ~(size_t) 0 / sizeof (*i_data))
   1939     {
   1940       bfd_set_error (bfd_error_file_too_big);
   1941       return NULL;
   1942     }
   1943 
   1944   e_data = _bfd_malloc_and_read (abfd, size, size);
   1945   if (e_data == NULL)
   1946     return NULL;
   1947 
   1948   i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
   1949   if (i_data == NULL)
   1950     {
   1951       free (e_data);
   1952       return NULL;
   1953     }
   1954 
   1955   if (ent_size == 4)
   1956     while (number--)
   1957       i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
   1958   else
   1959     while (number--)
   1960       i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
   1961 
   1962   free (e_data);
   1963   return i_data;
   1964 }
   1965 
   1966 /* Address of .MIPS.xhash section.  FIXME: What is the best way to
   1967    support DT_MIPS_XHASH?  */
   1968 #define DT_MIPS_XHASH	       0x70000036
   1969 
   1970 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
   1971 
   1972 bool
   1973 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
   1974 			      Elf_Internal_Phdr *phdrs, size_t phnum,
   1975 			      bfd_size_type filesize)
   1976 {
   1977   bfd_byte *extdyn, *extdynend;
   1978   size_t extdynsize;
   1979   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1980   bool (*swap_symbol_in) (bfd *, const void *, const void *,
   1981 			  Elf_Internal_Sym *);
   1982   Elf_Internal_Dyn dyn;
   1983   bfd_vma dt_hash = 0;
   1984   bfd_vma dt_gnu_hash = 0;
   1985   bfd_vma dt_mips_xhash = 0;
   1986   bfd_vma dt_strtab = 0;
   1987   bfd_vma dt_symtab = 0;
   1988   size_t dt_strsz = 0;
   1989   bfd_vma dt_versym = 0;
   1990   bfd_vma dt_verdef = 0;
   1991   bfd_vma dt_verneed = 0;
   1992   bfd_byte *dynbuf = NULL;
   1993   char *strbuf = NULL;
   1994   bfd_vma *gnubuckets = NULL;
   1995   bfd_vma *gnuchains = NULL;
   1996   bfd_vma *mipsxlat = NULL;
   1997   file_ptr saved_filepos, filepos;
   1998   bool res = false;
   1999   size_t amt;
   2000   bfd_byte *esymbuf = NULL, *esym;
   2001   bfd_size_type symcount;
   2002   Elf_Internal_Sym *isymbuf = NULL;
   2003   Elf_Internal_Sym *isym, *isymend;
   2004   bfd_byte *versym = NULL;
   2005   bfd_byte *verdef = NULL;
   2006   bfd_byte *verneed = NULL;
   2007   size_t verdef_size = 0;
   2008   size_t verneed_size = 0;
   2009   size_t extsym_size;
   2010   const struct elf_backend_data *bed;
   2011 
   2012   /* Return TRUE if symbol table is bad.  */
   2013   if (elf_bad_symtab (abfd))
   2014     return true;
   2015 
   2016   /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
   2017   if (elf_tdata (abfd)->dt_strtab != NULL)
   2018     return true;
   2019 
   2020   bed = get_elf_backend_data (abfd);
   2021 
   2022   /* Save file position for elf_object_p.  */
   2023   saved_filepos = bfd_tell (abfd);
   2024 
   2025   if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
   2026     goto error_return;
   2027 
   2028   dynbuf = _bfd_malloc_and_read (abfd, phdr->p_filesz, phdr->p_filesz);
   2029   if (dynbuf == NULL)
   2030     goto error_return;
   2031 
   2032   extsym_size = bed->s->sizeof_sym;
   2033   extdynsize = bed->s->sizeof_dyn;
   2034   swap_dyn_in = bed->s->swap_dyn_in;
   2035 
   2036   extdyn = dynbuf;
   2037   if (phdr->p_filesz < extdynsize)
   2038     goto error_return;
   2039   extdynend = extdyn + phdr->p_filesz;
   2040   for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   2041     {
   2042       swap_dyn_in (abfd, extdyn, &dyn);
   2043 
   2044       if (dyn.d_tag == DT_NULL)
   2045 	break;
   2046 
   2047       switch (dyn.d_tag)
   2048 	{
   2049 	case DT_HASH:
   2050 	  dt_hash = dyn.d_un.d_val;
   2051 	  break;
   2052 	case DT_GNU_HASH:
   2053 	  if (bed->elf_machine_code != EM_MIPS
   2054 	      && bed->elf_machine_code != EM_MIPS_RS3_LE)
   2055 	    dt_gnu_hash = dyn.d_un.d_val;
   2056 	  break;
   2057 	case DT_STRTAB:
   2058 	  dt_strtab = dyn.d_un.d_val;
   2059 	  break;
   2060 	case DT_SYMTAB:
   2061 	  dt_symtab = dyn.d_un.d_val;
   2062 	  break;
   2063 	case DT_STRSZ:
   2064 	  dt_strsz = dyn.d_un.d_val;
   2065 	  break;
   2066 	case DT_SYMENT:
   2067 	  if (dyn.d_un.d_val != extsym_size)
   2068 	    goto error_return;
   2069 	  break;
   2070 	case DT_VERSYM:
   2071 	  dt_versym = dyn.d_un.d_val;
   2072 	  break;
   2073 	case DT_VERDEF:
   2074 	  dt_verdef = dyn.d_un.d_val;
   2075 	  break;
   2076 	case DT_VERNEED:
   2077 	  dt_verneed = dyn.d_un.d_val;
   2078 	  break;
   2079 	default:
   2080 	  if (dyn.d_tag == DT_MIPS_XHASH
   2081 	      && (bed->elf_machine_code == EM_MIPS
   2082 		  || bed->elf_machine_code == EM_MIPS_RS3_LE))
   2083 	    {
   2084 	      dt_gnu_hash = dyn.d_un.d_val;
   2085 	      dt_mips_xhash = dyn.d_un.d_val;
   2086 	    }
   2087 	  break;
   2088 	}
   2089     }
   2090 
   2091   /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
   2092      segment.  */
   2093   if ((!dt_hash && !dt_gnu_hash)
   2094       || !dt_strtab
   2095       || !dt_symtab
   2096       || !dt_strsz)
   2097     goto error_return;
   2098 
   2099   /* Get dynamic string table.  */
   2100   filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
   2101   if (filepos == (file_ptr) -1
   2102       || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2103     goto error_return;
   2104 
   2105   /* Dynamic string table must be valid until ABFD is closed.  */
   2106   strbuf = (char *) _bfd_alloc_and_read (abfd, dt_strsz + 1, dt_strsz);
   2107   if (strbuf == NULL)
   2108     goto error_return;
   2109   /* Since this is a string table, make sure that it is terminated.  */
   2110   strbuf[dt_strsz] = 0;
   2111 
   2112   /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
   2113      DT_HASH since it is simpler than DT_GNU_HASH.  */
   2114   if (dt_hash)
   2115     {
   2116       unsigned char nb[16];
   2117       unsigned int hash_ent_size;
   2118 
   2119       switch (bed->elf_machine_code)
   2120 	{
   2121 	case EM_ALPHA:
   2122 	case EM_S390:
   2123 	case EM_S390_OLD:
   2124 	  if (bed->s->elfclass == ELFCLASS64)
   2125 	    {
   2126 	      hash_ent_size = 8;
   2127 	      break;
   2128 	    }
   2129 	  /* FALLTHROUGH */
   2130 	default:
   2131 	  hash_ent_size = 4;
   2132 	  break;
   2133 	}
   2134 
   2135       filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb),
   2136 				 NULL);
   2137       if (filepos == (file_ptr) -1
   2138 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0
   2139 	  || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
   2140 	goto error_return;
   2141 
   2142       /* The number of dynamic symbol table entries equals the number
   2143 	 of chains.  */
   2144       if (hash_ent_size == 8)
   2145 	symcount = bfd_get_64 (abfd, nb + hash_ent_size);
   2146       else
   2147 	symcount = bfd_get_32 (abfd, nb + hash_ent_size);
   2148     }
   2149   else
   2150     {
   2151       /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
   2152 	 bindings are in hash table.  Since in dynamic symbol table,
   2153 	 all symbols with STB_LOCAL binding are placed before symbols
   2154 	 with other bindings and all undefined symbols are placed
   2155 	 before defined ones, the highest symbol index in DT_GNU_HASH
   2156 	 is the highest dynamic symbol table index.  */
   2157       unsigned char nb[16];
   2158       bfd_vma ngnubuckets;
   2159       bfd_vma gnusymidx;
   2160       size_t i, ngnuchains;
   2161       bfd_vma maxchain = 0xffffffff, bitmaskwords;
   2162       bfd_vma buckets_vma;
   2163 
   2164       filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
   2165 				 sizeof (nb), NULL);
   2166       if (filepos == (file_ptr) -1
   2167 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0
   2168 	  || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
   2169 	goto error_return;
   2170 
   2171       ngnubuckets = bfd_get_32 (abfd, nb);
   2172       gnusymidx = bfd_get_32 (abfd, nb + 4);
   2173       bitmaskwords = bfd_get_32 (abfd, nb + 8);
   2174       buckets_vma = dt_gnu_hash + 16;
   2175       if (bed->s->elfclass == ELFCLASS32)
   2176 	buckets_vma += bitmaskwords * 4;
   2177       else
   2178 	buckets_vma += bitmaskwords * 8;
   2179       filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
   2180       if (filepos == (file_ptr) -1
   2181 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2182 	goto error_return;
   2183 
   2184       gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
   2185       if (gnubuckets == NULL)
   2186 	goto error_return;
   2187 
   2188       for (i = 0; i < ngnubuckets; i++)
   2189 	if (gnubuckets[i] != 0)
   2190 	  {
   2191 	    if (gnubuckets[i] < gnusymidx)
   2192 	      goto error_return;
   2193 
   2194 	    if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
   2195 	      maxchain = gnubuckets[i];
   2196 	  }
   2197 
   2198       if (maxchain == 0xffffffff)
   2199 	{
   2200 	  symcount = 0;
   2201 	  goto empty_gnu_hash;
   2202 	}
   2203 
   2204       maxchain -= gnusymidx;
   2205       filepos = offset_from_vma (phdrs, phnum,
   2206 				 (buckets_vma +
   2207 				  4 * (ngnubuckets + maxchain)),
   2208 				 4, NULL);
   2209       if (filepos == (file_ptr) -1
   2210 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2211 	goto error_return;
   2212 
   2213       do
   2214 	{
   2215 	  if (bfd_read (nb, 4, abfd) != 4)
   2216 	    goto error_return;
   2217 	  ++maxchain;
   2218 	  if (maxchain == 0)
   2219 	    goto error_return;
   2220 	}
   2221       while ((bfd_get_32 (abfd, nb) & 1) == 0);
   2222 
   2223       filepos = offset_from_vma (phdrs, phnum,
   2224 				 (buckets_vma + 4 * ngnubuckets),
   2225 				 4, NULL);
   2226       if (filepos == (file_ptr) -1
   2227 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2228 	goto error_return;
   2229 
   2230       gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
   2231       if (gnuchains == NULL)
   2232 	goto error_return;
   2233       ngnuchains = maxchain;
   2234 
   2235       if (dt_mips_xhash)
   2236 	{
   2237 	  filepos = offset_from_vma (phdrs, phnum,
   2238 				     (buckets_vma
   2239 				      + 4 * (ngnubuckets + maxchain)),
   2240 				     4, NULL);
   2241 	  if (filepos == (file_ptr) -1
   2242 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2243 	    goto error_return;
   2244 
   2245 	  mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
   2246 	  if (mipsxlat == NULL)
   2247 	    goto error_return;
   2248 	}
   2249 
   2250       symcount = 0;
   2251       for (i = 0; i < ngnubuckets; ++i)
   2252 	if (gnubuckets[i] != 0)
   2253 	  {
   2254 	    bfd_vma si = gnubuckets[i];
   2255 	    bfd_vma off = si - gnusymidx;
   2256 	    do
   2257 	      {
   2258 		if (mipsxlat)
   2259 		  {
   2260 		    if (mipsxlat[off] >= symcount)
   2261 		      symcount = mipsxlat[off] + 1;
   2262 		  }
   2263 		else
   2264 		  {
   2265 		    if (si >= symcount)
   2266 		      symcount = si + 1;
   2267 		  }
   2268 		si++;
   2269 	      }
   2270 	    while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
   2271 	  }
   2272     }
   2273 
   2274   /* Swap in dynamic symbol table.  */
   2275   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
   2276     {
   2277       bfd_set_error (bfd_error_file_too_big);
   2278       goto error_return;
   2279     }
   2280 
   2281   filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
   2282   if (filepos == (file_ptr) -1
   2283       || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2284     goto error_return;
   2285   esymbuf = _bfd_malloc_and_read (abfd, amt, amt);
   2286   if (esymbuf == NULL)
   2287     goto error_return;
   2288 
   2289   if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
   2290     {
   2291       bfd_set_error (bfd_error_file_too_big);
   2292       goto error_return;
   2293     }
   2294 
   2295   /* Dynamic symbol table must be valid until ABFD is closed.  */
   2296   isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
   2297   if (isymbuf == NULL)
   2298     goto error_return;
   2299 
   2300   swap_symbol_in = bed->s->swap_symbol_in;
   2301 
   2302   /* Convert the symbols to internal form.  */
   2303   isymend = isymbuf + symcount;
   2304   for (esym = esymbuf, isym = isymbuf;
   2305        isym < isymend;
   2306        esym += extsym_size, isym++)
   2307     if (!swap_symbol_in (abfd, esym, NULL, isym)
   2308 	|| isym->st_name >= dt_strsz)
   2309       {
   2310 	bfd_set_error (bfd_error_invalid_operation);
   2311 	goto error_return;
   2312       }
   2313 
   2314   if (dt_versym)
   2315     {
   2316       /* Swap in DT_VERSYM.  */
   2317       if (_bfd_mul_overflow (symcount, 2, &amt))
   2318 	{
   2319 	  bfd_set_error (bfd_error_file_too_big);
   2320 	  goto error_return;
   2321 	}
   2322 
   2323       filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
   2324       if (filepos == (file_ptr) -1
   2325 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2326 	goto error_return;
   2327 
   2328       /* DT_VERSYM info must be valid until ABFD is closed.  */
   2329       versym = _bfd_alloc_and_read (abfd, amt, amt);
   2330 
   2331       if (dt_verdef)
   2332 	{
   2333 	  /* Read in DT_VERDEF.  */
   2334 	  filepos = offset_from_vma (phdrs, phnum, dt_verdef,
   2335 				     0, &verdef_size);
   2336 	  if (filepos == (file_ptr) -1
   2337 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2338 	    goto error_return;
   2339 
   2340 	  /* DT_VERDEF info must be valid until ABFD is closed.  */
   2341 	  verdef = _bfd_alloc_and_read (abfd, verdef_size,
   2342 					verdef_size);
   2343 	}
   2344 
   2345       if (dt_verneed)
   2346 	{
   2347 	  /* Read in DT_VERNEED.  */
   2348 	  filepos = offset_from_vma (phdrs, phnum, dt_verneed,
   2349 				     0, &verneed_size);
   2350 	  if (filepos == (file_ptr) -1
   2351 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2352 	    goto error_return;
   2353 
   2354 	  /* DT_VERNEED info must be valid until ABFD is closed.  */
   2355 	  verneed = _bfd_alloc_and_read (abfd, verneed_size,
   2356 					 verneed_size);
   2357 	}
   2358     }
   2359 
   2360  empty_gnu_hash:
   2361   elf_tdata (abfd)->dt_strtab = strbuf;
   2362   elf_tdata (abfd)->dt_strsz = dt_strsz;
   2363   elf_tdata (abfd)->dt_symtab = isymbuf;
   2364   elf_tdata (abfd)->dt_symtab_count = symcount;
   2365   elf_tdata (abfd)->dt_versym = versym;
   2366   elf_tdata (abfd)->dt_verdef = verdef;
   2367   elf_tdata (abfd)->dt_verneed = verneed;
   2368   elf_tdata (abfd)->dt_verdef_count
   2369     = verdef_size / sizeof (Elf_External_Verdef);
   2370   elf_tdata (abfd)->dt_verneed_count
   2371     = verneed_size / sizeof (Elf_External_Verneed);
   2372 
   2373   res = true;
   2374 
   2375  error_return:
   2376   /* Restore file position for elf_object_p.  */
   2377   if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
   2378     res = false;
   2379   free (dynbuf);
   2380   free (esymbuf);
   2381   free (gnubuckets);
   2382   free (gnuchains);
   2383   free (mipsxlat);
   2384   return res;
   2385 }
   2386 
   2387 /* Reconstruct section from dynamic symbol.  */
   2388 
   2389 asection *
   2390 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
   2391 					  Elf_Internal_Sym *isym)
   2392 {
   2393   asection *sec;
   2394   flagword flags;
   2395 
   2396   if (!elf_use_dt_symtab_p (abfd))
   2397     return NULL;
   2398 
   2399   flags = SEC_ALLOC | SEC_LOAD;
   2400   switch (ELF_ST_TYPE (isym->st_info))
   2401     {
   2402     case STT_FUNC:
   2403     case STT_GNU_IFUNC:
   2404       sec = bfd_get_section_by_name (abfd, ".text");
   2405       if (sec == NULL)
   2406 	sec = bfd_make_section_with_flags (abfd,
   2407 					   ".text",
   2408 					   flags | SEC_CODE);
   2409       break;
   2410     case STT_COMMON:
   2411       sec = bfd_com_section_ptr;
   2412       break;
   2413     case STT_OBJECT:
   2414       sec = bfd_get_section_by_name (abfd, ".data");
   2415       if (sec == NULL)
   2416 	sec = bfd_make_section_with_flags (abfd,
   2417 					   ".data",
   2418 					   flags | SEC_DATA);
   2419       break;
   2420     case STT_TLS:
   2421       sec = bfd_get_section_by_name (abfd, ".tdata");
   2422       if (sec == NULL)
   2423 	sec = bfd_make_section_with_flags (abfd,
   2424 					   ".tdata",
   2425 					   (flags
   2426 					    | SEC_DATA
   2427 					    | SEC_THREAD_LOCAL));
   2428       break;
   2429     default:
   2430       sec = bfd_abs_section_ptr;
   2431       break;
   2432     }
   2433 
   2434   return sec;
   2435 }
   2436 
   2437 /* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
   2438    and return symbol version for symbol version itself.   */
   2439 
   2440 const char *
   2441 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   2442 				    bool base_p,
   2443 				    bool *hidden)
   2444 {
   2445   const char *version_string = NULL;
   2446   if ((elf_dynversym (abfd) != 0
   2447        && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   2448       || (elf_tdata (abfd)->dt_versym != NULL
   2449 	  && (elf_tdata (abfd)->dt_verdef != NULL
   2450 	      || elf_tdata (abfd)->dt_verneed != NULL)))
   2451     {
   2452       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   2453 
   2454       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   2455       vernum &= VERSYM_VERSION;
   2456 
   2457       if (vernum == 0)
   2458 	version_string = "";
   2459       else if (vernum == 1
   2460 	       && (vernum > elf_tdata (abfd)->cverdefs
   2461 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   2462 		       == VER_FLG_BASE)))
   2463 	version_string = base_p ? "Base" : "";
   2464       else if (vernum <= elf_tdata (abfd)->cverdefs)
   2465 	{
   2466 	  const char *nodename
   2467 	    = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   2468 	  version_string = "";
   2469 	  if (base_p
   2470 	      || nodename == NULL
   2471 	      || symbol->name == NULL
   2472 	      || strcmp (symbol->name, nodename) != 0)
   2473 	    version_string = nodename;
   2474 	}
   2475       else
   2476 	{
   2477 	  Elf_Internal_Verneed *t;
   2478 
   2479 	  version_string = _("<corrupt>");
   2480 	  for (t = elf_tdata (abfd)->verref;
   2481 	       t != NULL;
   2482 	       t = t->vn_nextref)
   2483 	    {
   2484 	      Elf_Internal_Vernaux *a;
   2485 
   2486 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2487 		{
   2488 		  if (a->vna_other == vernum)
   2489 		    {
   2490 		      *hidden = true;
   2491 		      version_string = a->vna_nodename;
   2492 		      break;
   2493 		    }
   2494 		}
   2495 	    }
   2496 	}
   2497     }
   2498   return version_string;
   2499 }
   2500 
   2501 /* Display ELF-specific fields of a symbol.  */
   2502 
   2503 void
   2504 bfd_elf_print_symbol (bfd *abfd,
   2505 		      void *filep,
   2506 		      asymbol *symbol,
   2507 		      bfd_print_symbol_type how)
   2508 {
   2509   FILE *file = (FILE *) filep;
   2510   switch (how)
   2511     {
   2512     case bfd_print_symbol_name:
   2513       fprintf (file, "%s", symbol->name);
   2514       break;
   2515     case bfd_print_symbol_more:
   2516       fprintf (file, "elf ");
   2517       bfd_fprintf_vma (abfd, file, symbol->value);
   2518       fprintf (file, " %x", symbol->flags);
   2519       break;
   2520     case bfd_print_symbol_all:
   2521       {
   2522 	const char *section_name;
   2523 	const char *name = NULL;
   2524 	const struct elf_backend_data *bed;
   2525 	unsigned char st_other;
   2526 	bfd_vma val;
   2527 	const char *version_string;
   2528 	bool hidden;
   2529 
   2530 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   2531 
   2532 	bed = get_elf_backend_data (abfd);
   2533 	if (bed->elf_backend_print_symbol_all)
   2534 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   2535 
   2536 	if (name == NULL)
   2537 	  {
   2538 	    name = symbol->name;
   2539 	    bfd_print_symbol_vandf (abfd, file, symbol);
   2540 	  }
   2541 
   2542 	fprintf (file, " %s\t", section_name);
   2543 	/* Print the "other" value for a symbol.  For common symbols,
   2544 	   we've already printed the size; now print the alignment.
   2545 	   For other symbols, we have no specified alignment, and
   2546 	   we've printed the address; now print the size.  */
   2547 	if (symbol->section && bfd_is_com_section (symbol->section))
   2548 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   2549 	else
   2550 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   2551 	bfd_fprintf_vma (abfd, file, val);
   2552 
   2553 	/* If we have version information, print it.  */
   2554 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2555 							     symbol,
   2556 							     true,
   2557 							     &hidden);
   2558 	if (version_string)
   2559 	  {
   2560 	    if (!hidden)
   2561 	      fprintf (file, "  %-11s", version_string);
   2562 	    else
   2563 	      {
   2564 		int i;
   2565 
   2566 		fprintf (file, " (%s)", version_string);
   2567 		for (i = 10 - strlen (version_string); i > 0; --i)
   2568 		  putc (' ', file);
   2569 	      }
   2570 	  }
   2571 
   2572 	/* If the st_other field is not zero, print it.  */
   2573 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2574 
   2575 	switch (st_other)
   2576 	  {
   2577 	  case 0: break;
   2578 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2579 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2580 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2581 	  default:
   2582 	    /* Some other non-defined flags are also present, so print
   2583 	       everything hex.  */
   2584 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2585 	  }
   2586 
   2587 	fprintf (file, " %s", name);
   2588       }
   2589       break;
   2590     }
   2591 }
   2592 
   2593 /* ELF .o/exec file reading */
   2595 
   2596 /* Create a new bfd section from an ELF section header.  */
   2597 
   2598 bool
   2599 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2600 {
   2601   Elf_Internal_Shdr *hdr;
   2602   Elf_Internal_Ehdr *ehdr;
   2603   const struct elf_backend_data *bed;
   2604   const char *name;
   2605   bool ret = true;
   2606 
   2607   if (shindex >= elf_numsections (abfd))
   2608     return false;
   2609 
   2610   /* PR17512: A corrupt ELF binary might contain a loop of sections via
   2611      sh_link or sh_info.  Detect this here, by refusing to load a
   2612      section that we are already in the process of loading.  */
   2613   if (elf_tdata (abfd)->being_created[shindex])
   2614     {
   2615       _bfd_error_handler
   2616 	(_("%pB: warning: loop in section dependencies detected"), abfd);
   2617       return false;
   2618     }
   2619   elf_tdata (abfd)->being_created[shindex] = true;
   2620 
   2621   hdr = elf_elfsections (abfd)[shindex];
   2622   ehdr = elf_elfheader (abfd);
   2623   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2624 					  hdr->sh_name);
   2625   if (name == NULL)
   2626     goto fail;
   2627 
   2628   bed = get_elf_backend_data (abfd);
   2629   switch (hdr->sh_type)
   2630     {
   2631     case SHT_NULL:
   2632       /* Inactive section. Throw it away.  */
   2633       goto success;
   2634 
   2635     case SHT_PROGBITS:		/* Normal section with contents.  */
   2636     case SHT_NOBITS:		/* .bss section.  */
   2637     case SHT_HASH:		/* .hash section.  */
   2638     case SHT_NOTE:		/* .note section.  */
   2639     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2640     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2641     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2642     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2643     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2644       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2645       goto success;
   2646 
   2647     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2648       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2649 	goto fail;
   2650 
   2651       if (hdr->sh_link > elf_numsections (abfd))
   2652 	{
   2653 	  /* PR 10478: Accept Solaris binaries with a sh_link field
   2654 	     set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
   2655 	  switch (bfd_get_arch (abfd))
   2656 	    {
   2657 	    case bfd_arch_i386:
   2658 	    case bfd_arch_sparc:
   2659 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
   2660 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
   2661 		break;
   2662 	      /* Otherwise fall through.  */
   2663 	    default:
   2664 	      goto fail;
   2665 	    }
   2666 	}
   2667       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2668 	goto fail;
   2669       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2670 	{
   2671 	  Elf_Internal_Shdr *dynsymhdr;
   2672 
   2673 	  /* The shared libraries distributed with hpux11 have a bogus
   2674 	     sh_link field for the ".dynamic" section.  Find the
   2675 	     string table for the ".dynsym" section instead.  */
   2676 	  if (elf_dynsymtab (abfd) != 0)
   2677 	    {
   2678 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2679 	      hdr->sh_link = dynsymhdr->sh_link;
   2680 	    }
   2681 	  else
   2682 	    {
   2683 	      unsigned int i, num_sec;
   2684 
   2685 	      num_sec = elf_numsections (abfd);
   2686 	      for (i = 1; i < num_sec; i++)
   2687 		{
   2688 		  dynsymhdr = elf_elfsections (abfd)[i];
   2689 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2690 		    {
   2691 		      hdr->sh_link = dynsymhdr->sh_link;
   2692 		      break;
   2693 		    }
   2694 		}
   2695 	    }
   2696 	}
   2697       goto success;
   2698 
   2699     case SHT_SYMTAB:		/* A symbol table.  */
   2700       if (elf_onesymtab (abfd) == shindex)
   2701 	goto success;
   2702 
   2703       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2704 	goto fail;
   2705 
   2706       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2707 	{
   2708 	  if (hdr->sh_size != 0)
   2709 	    goto fail;
   2710 	  /* Some assemblers erroneously set sh_info to one with a
   2711 	     zero sh_size.  ld sees this as a global symbol count
   2712 	     of (unsigned) -1.  Fix it here.  */
   2713 	  hdr->sh_info = 0;
   2714 	  goto success;
   2715 	}
   2716 
   2717       /* PR 18854: A binary might contain more than one symbol table.
   2718 	 Unusual, but possible.  Warn, but continue.  */
   2719       if (elf_onesymtab (abfd) != 0)
   2720 	{
   2721 	  _bfd_error_handler
   2722 	    /* xgettext:c-format */
   2723 	    (_("%pB: warning: multiple symbol tables detected"
   2724 	       " - ignoring the table in section %u"),
   2725 	     abfd, shindex);
   2726 	  goto success;
   2727 	}
   2728       elf_onesymtab (abfd) = shindex;
   2729       elf_symtab_hdr (abfd) = *hdr;
   2730       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2731       abfd->flags |= HAS_SYMS;
   2732 
   2733       /* Sometimes a shared object will map in the symbol table.  If
   2734 	 SHF_ALLOC is set, and this is a shared object, then we also
   2735 	 treat this section as a BFD section.  We can not base the
   2736 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2737 	 set in a relocatable object file, which would confuse the
   2738 	 linker.  */
   2739       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2740 	  && (abfd->flags & DYNAMIC) != 0
   2741 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2742 						shindex))
   2743 	goto fail;
   2744 
   2745       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2746 	 can't read symbols without that section loaded as well.  It
   2747 	 is most likely specified by the next section header.  */
   2748       {
   2749 	elf_section_list * entry;
   2750 	unsigned int i, num_sec;
   2751 
   2752 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2753 	  if (entry->hdr.sh_link == shindex)
   2754 	    goto success;
   2755 
   2756 	num_sec = elf_numsections (abfd);
   2757 	for (i = shindex + 1; i < num_sec; i++)
   2758 	  {
   2759 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2760 
   2761 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2762 		&& hdr2->sh_link == shindex)
   2763 	      break;
   2764 	  }
   2765 
   2766 	if (i == num_sec)
   2767 	  for (i = 1; i < shindex; i++)
   2768 	    {
   2769 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2770 
   2771 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2772 		  && hdr2->sh_link == shindex)
   2773 		break;
   2774 	    }
   2775 
   2776 	if (i != shindex)
   2777 	  ret = bfd_section_from_shdr (abfd, i);
   2778 	/* else FIXME: we have failed to find the symbol table.
   2779 	   Should we issue an error?  */
   2780 	goto success;
   2781       }
   2782 
   2783     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2784       if (elf_dynsymtab (abfd) == shindex)
   2785 	goto success;
   2786 
   2787       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2788 	goto fail;
   2789 
   2790       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2791 	{
   2792 	  if (hdr->sh_size != 0)
   2793 	    goto fail;
   2794 
   2795 	  /* Some linkers erroneously set sh_info to one with a
   2796 	     zero sh_size.  ld sees this as a global symbol count
   2797 	     of (unsigned) -1.  Fix it here.  */
   2798 	  hdr->sh_info = 0;
   2799 	  goto success;
   2800 	}
   2801 
   2802       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2803 	 Unusual, but possible.  Warn, but continue.  */
   2804       if (elf_dynsymtab (abfd) != 0)
   2805 	{
   2806 	  _bfd_error_handler
   2807 	    /* xgettext:c-format */
   2808 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2809 	       " - ignoring the table in section %u"),
   2810 	     abfd, shindex);
   2811 	  goto success;
   2812 	}
   2813       elf_dynsymtab (abfd) = shindex;
   2814       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2815       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2816       abfd->flags |= HAS_SYMS;
   2817 
   2818       /* Besides being a symbol table, we also treat this as a regular
   2819 	 section, so that objcopy can handle it.  */
   2820       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2821       goto success;
   2822 
   2823     case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
   2824       {
   2825 	elf_section_list * entry;
   2826 
   2827 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2828 	  if (entry->ndx == shindex)
   2829 	    goto success;
   2830 
   2831 	entry = bfd_alloc (abfd, sizeof (*entry));
   2832 	if (entry == NULL)
   2833 	  goto fail;
   2834 	entry->ndx = shindex;
   2835 	entry->hdr = * hdr;
   2836 	entry->next = elf_symtab_shndx_list (abfd);
   2837 	elf_symtab_shndx_list (abfd) = entry;
   2838 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2839 	goto success;
   2840       }
   2841 
   2842     case SHT_STRTAB:		/* A string table.  */
   2843       if (hdr->bfd_section != NULL)
   2844 	goto success;
   2845 
   2846       if (ehdr->e_shstrndx == shindex)
   2847 	{
   2848 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2849 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2850 	  goto success;
   2851 	}
   2852 
   2853       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2854 	{
   2855 	symtab_strtab:
   2856 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2857 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2858 	  goto success;
   2859 	}
   2860 
   2861       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2862 	{
   2863 	dynsymtab_strtab:
   2864 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2865 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2866 	  elf_elfsections (abfd)[shindex] = hdr;
   2867 	  /* We also treat this as a regular section, so that objcopy
   2868 	     can handle it.  */
   2869 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2870 						 shindex);
   2871 	  goto success;
   2872 	}
   2873 
   2874       /* If the string table isn't one of the above, then treat it as a
   2875 	 regular section.  We need to scan all the headers to be sure,
   2876 	 just in case this strtab section appeared before the above.  */
   2877       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2878 	{
   2879 	  unsigned int i, num_sec;
   2880 
   2881 	  num_sec = elf_numsections (abfd);
   2882 	  for (i = 1; i < num_sec; i++)
   2883 	    {
   2884 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2885 	      if (hdr2->sh_link == shindex)
   2886 		{
   2887 		  /* Prevent endless recursion on broken objects.  */
   2888 		  if (i == shindex)
   2889 		    goto fail;
   2890 		  if (! bfd_section_from_shdr (abfd, i))
   2891 		    goto fail;
   2892 		  if (elf_onesymtab (abfd) == i)
   2893 		    goto symtab_strtab;
   2894 		  if (elf_dynsymtab (abfd) == i)
   2895 		    goto dynsymtab_strtab;
   2896 		}
   2897 	    }
   2898 	}
   2899       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2900       goto success;
   2901 
   2902     case SHT_REL:
   2903     case SHT_RELA:
   2904     case SHT_RELR:
   2905       /* *These* do a lot of work -- but build no sections!  */
   2906       {
   2907 	asection *target_sect;
   2908 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2909 	unsigned int num_sec = elf_numsections (abfd);
   2910 	struct bfd_elf_section_data *esdt;
   2911 	bfd_size_type size;
   2912 
   2913 	if (hdr->sh_type == SHT_REL)
   2914 	  size = bed->s->sizeof_rel;
   2915 	else if (hdr->sh_type == SHT_RELA)
   2916 	  size = bed->s->sizeof_rela;
   2917 	else
   2918 	  size = bed->s->arch_size / 8;
   2919 	if (hdr->sh_entsize != size)
   2920 	  goto fail;
   2921 
   2922 	/* Check for a bogus link to avoid crashing.  */
   2923 	if (hdr->sh_link >= num_sec)
   2924 	  {
   2925 	    _bfd_error_handler
   2926 	      /* xgettext:c-format */
   2927 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2928 	       abfd, hdr->sh_link, name, shindex);
   2929 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2930 	    goto success;
   2931 	  }
   2932 
   2933 	/* Get the symbol table.  */
   2934 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2935 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2936 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2937 	  goto fail;
   2938 
   2939 	/* If this is an alloc section in an executable or shared
   2940 	   library, or the reloc section does not use the main symbol
   2941 	   table we don't treat it as a reloc section.  BFD can't
   2942 	   adequately represent such a section, so at least for now,
   2943 	   we don't try.  We just present it as a normal section.  We
   2944 	   also can't use it as a reloc section if it points to the
   2945 	   null section, an invalid section, another reloc section, or
   2946 	   its sh_link points to the null section.  */
   2947 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2948 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2949 	    || (hdr->sh_flags & SHF_COMPRESSED) != 0
   2950 	    || hdr->sh_type == SHT_RELR
   2951 	    || hdr->sh_link == SHN_UNDEF
   2952 	    || hdr->sh_link != elf_onesymtab (abfd)
   2953 	    || hdr->sh_info == SHN_UNDEF
   2954 	    || hdr->sh_info >= num_sec
   2955 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2956 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2957 	  {
   2958 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2959 	    goto success;
   2960 	  }
   2961 
   2962 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2963 	  goto fail;
   2964 
   2965 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2966 	if (target_sect == NULL)
   2967 	  goto fail;
   2968 
   2969 	esdt = elf_section_data (target_sect);
   2970 	if (hdr->sh_type == SHT_RELA)
   2971 	  p_hdr = &esdt->rela.hdr;
   2972 	else
   2973 	  p_hdr = &esdt->rel.hdr;
   2974 
   2975 	/* PR 17512: file: 0b4f81b7.
   2976 	   Also see PR 24456, for a file which deliberately has two reloc
   2977 	   sections.  */
   2978 	if (*p_hdr != NULL)
   2979 	  {
   2980 	    if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
   2981 	      {
   2982 		_bfd_error_handler
   2983 		  /* xgettext:c-format */
   2984 		  (_("%pB: warning: secondary relocation section '%s' "
   2985 		     "for section %pA found - ignoring"),
   2986 		   abfd, name, target_sect);
   2987 	      }
   2988 	    else
   2989 	      esdt->has_secondary_relocs = true;
   2990 	    goto success;
   2991 	  }
   2992 
   2993 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2994 	if (hdr2 == NULL)
   2995 	  goto fail;
   2996 	*hdr2 = *hdr;
   2997 	*p_hdr = hdr2;
   2998 	elf_elfsections (abfd)[shindex] = hdr2;
   2999 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   3000 				     * bed->s->int_rels_per_ext_rel);
   3001 	target_sect->flags |= SEC_RELOC;
   3002 	target_sect->relocation = NULL;
   3003 	target_sect->rel_filepos = hdr->sh_offset;
   3004 	/* In the section to which the relocations apply, mark whether
   3005 	   its relocations are of the REL or RELA variety.  */
   3006 	if (hdr->sh_size != 0)
   3007 	  {
   3008 	    if (hdr->sh_type == SHT_RELA)
   3009 	      target_sect->use_rela_p = 1;
   3010 	  }
   3011 	abfd->flags |= HAS_RELOC;
   3012 	goto success;
   3013       }
   3014 
   3015     case SHT_GNU_verdef:
   3016       if (hdr->sh_info != 0)
   3017 	elf_dynverdef (abfd) = shindex;
   3018       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   3019       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3020       goto success;
   3021 
   3022     case SHT_GNU_versym:
   3023       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   3024 	goto fail;
   3025 
   3026       elf_dynversym (abfd) = shindex;
   3027       elf_tdata (abfd)->dynversym_hdr = *hdr;
   3028       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3029       goto success;
   3030 
   3031     case SHT_GNU_verneed:
   3032       if (hdr->sh_info != 0)
   3033 	elf_dynverref (abfd) = shindex;
   3034       elf_tdata (abfd)->dynverref_hdr = *hdr;
   3035       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3036       goto success;
   3037 
   3038     case SHT_SHLIB:
   3039       goto success;
   3040 
   3041     case SHT_GROUP:
   3042       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   3043 	goto fail;
   3044 
   3045       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   3046 	goto fail;
   3047 
   3048       goto success;
   3049 
   3050     default:
   3051       /* Possibly an attributes section.  */
   3052       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   3053 	  || hdr->sh_type == bed->obj_attrs_section_type)
   3054 	{
   3055 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   3056 	    goto fail;
   3057 	  _bfd_elf_parse_attributes (abfd, hdr);
   3058 	  goto success;
   3059 	}
   3060 
   3061       /* Check for any processor-specific section types.  */
   3062       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   3063 	goto success;
   3064 
   3065       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   3066 	{
   3067 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   3068 	    /* FIXME: How to properly handle allocated section reserved
   3069 	       for applications?  */
   3070 	    _bfd_error_handler
   3071 	      /* xgettext:c-format */
   3072 	      (_("%pB: unknown type [%#x] section `%s'"),
   3073 	       abfd, hdr->sh_type, name);
   3074 	  else
   3075 	    {
   3076 	      /* Allow sections reserved for applications.  */
   3077 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3078 	      goto success;
   3079 	    }
   3080 	}
   3081       else if (hdr->sh_type >= SHT_LOPROC
   3082 	       && hdr->sh_type <= SHT_HIPROC)
   3083 	/* FIXME: We should handle this section.  */
   3084 	_bfd_error_handler
   3085 	  /* xgettext:c-format */
   3086 	  (_("%pB: unknown type [%#x] section `%s'"),
   3087 	   abfd, hdr->sh_type, name);
   3088       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   3089 	{
   3090 	  /* Unrecognised OS-specific sections.  */
   3091 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   3092 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   3093 	       required to correctly process the section and the file should
   3094 	       be rejected with an error message.  */
   3095 	    _bfd_error_handler
   3096 	      /* xgettext:c-format */
   3097 	      (_("%pB: unknown type [%#x] section `%s'"),
   3098 	       abfd, hdr->sh_type, name);
   3099 	  else
   3100 	    {
   3101 	      /* Otherwise it should be processed.  */
   3102 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3103 	      goto success;
   3104 	    }
   3105 	}
   3106       else
   3107 	/* FIXME: We should handle this section.  */
   3108 	_bfd_error_handler
   3109 	  /* xgettext:c-format */
   3110 	  (_("%pB: unknown type [%#x] section `%s'"),
   3111 	   abfd, hdr->sh_type, name);
   3112 
   3113       goto fail;
   3114     }
   3115 
   3116  fail:
   3117   ret = false;
   3118  success:
   3119   elf_tdata (abfd)->being_created[shindex] = false;
   3120   return ret;
   3121 }
   3122 
   3123 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   3124 
   3125 Elf_Internal_Sym *
   3126 bfd_sym_from_r_symndx (struct sym_cache *cache,
   3127 		       bfd *abfd,
   3128 		       unsigned long r_symndx)
   3129 {
   3130   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   3131 
   3132   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   3133     {
   3134       Elf_Internal_Shdr *symtab_hdr;
   3135       unsigned char esym[sizeof (Elf64_External_Sym)];
   3136       Elf_External_Sym_Shndx eshndx;
   3137 
   3138       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   3139       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   3140 				&cache->sym[ent], esym, &eshndx) == NULL)
   3141 	return NULL;
   3142 
   3143       if (cache->abfd != abfd)
   3144 	{
   3145 	  memset (cache->indx, -1, sizeof (cache->indx));
   3146 	  cache->abfd = abfd;
   3147 	}
   3148       cache->indx[ent] = r_symndx;
   3149     }
   3150 
   3151   return &cache->sym[ent];
   3152 }
   3153 
   3154 /* Given an ELF section number, retrieve the corresponding BFD
   3155    section.  */
   3156 
   3157 asection *
   3158 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   3159 {
   3160   if (sec_index >= elf_numsections (abfd))
   3161     return NULL;
   3162   return elf_elfsections (abfd)[sec_index]->bfd_section;
   3163 }
   3164 
   3165 static const struct bfd_elf_special_section special_sections_b[] =
   3166 {
   3167   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3168   { NULL,		    0,	0, 0,		 0 }
   3169 };
   3170 
   3171 static const struct bfd_elf_special_section special_sections_c[] =
   3172 {
   3173   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   3174   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   3175   { NULL,			0, 0, 0,	    0 }
   3176 };
   3177 
   3178 static const struct bfd_elf_special_section special_sections_d[] =
   3179 {
   3180   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3181   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3182   /* There are more DWARF sections than these, but they needn't be added here
   3183      unless you have to cope with broken compilers that don't emit section
   3184      attributes or you want to help the user writing assembler.  */
   3185   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   3186   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   3187   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   3188   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   3189   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   3190   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   3191   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   3192   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   3193   { NULL,		       0,	 0, 0,		  0 }
   3194 };
   3195 
   3196 static const struct bfd_elf_special_section special_sections_f[] =
   3197 {
   3198   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   3199   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   3200   { NULL,			   0 , 0, 0,		  0 }
   3201 };
   3202 
   3203 static const struct bfd_elf_special_section special_sections_g[] =
   3204 {
   3205   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   3206   { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   3207   { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   3208   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   3209   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   3210   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   3211   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   3212   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   3213   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   3214   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   3215   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   3216   { NULL,			 0,	   0, 0,	       0 }
   3217 };
   3218 
   3219 static const struct bfd_elf_special_section special_sections_h[] =
   3220 {
   3221   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   3222   { NULL,		     0, 0, 0,		 0 }
   3223 };
   3224 
   3225 static const struct bfd_elf_special_section special_sections_i[] =
   3226 {
   3227   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   3228   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   3229   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   3230   { NULL,		       0,      0, 0,		  0 }
   3231 };
   3232 
   3233 static const struct bfd_elf_special_section special_sections_l[] =
   3234 {
   3235   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   3236   { NULL,		     0, 0, 0,		 0 }
   3237 };
   3238 
   3239 static const struct bfd_elf_special_section special_sections_n[] =
   3240 {
   3241   { STRING_COMMA_LEN (".noinit"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3242   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   3243   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   3244   { NULL,		     0,		  0, 0,		   0 }
   3245 };
   3246 
   3247 static const struct bfd_elf_special_section special_sections_p[] =
   3248 {
   3249   { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,	SHF_ALLOC + SHF_WRITE },
   3250   { STRING_COMMA_LEN (".persistent"),	 -2, SHT_PROGBITS,	SHF_ALLOC + SHF_WRITE },
   3251   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   3252   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   3253   { NULL,		    0,		  0, 0,			0 }
   3254 };
   3255 
   3256 static const struct bfd_elf_special_section special_sections_r[] =
   3257 {
   3258   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   3259   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   3260   { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
   3261   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   3262   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   3263   { NULL,		    0,	   0, 0,	    0 }
   3264 };
   3265 
   3266 static const struct bfd_elf_special_section special_sections_s[] =
   3267 {
   3268   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   3269   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   3270   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   3271   /* See struct bfd_elf_special_section declaration for the semantics of
   3272      this special case where .prefix_length != strlen (.prefix).  */
   3273   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   3274   { NULL,			0,  0, 0,	   0 }
   3275 };
   3276 
   3277 static const struct bfd_elf_special_section special_sections_t[] =
   3278 {
   3279   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   3280   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   3281   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   3282   { NULL,		      0,  0, 0,		   0 }
   3283 };
   3284 
   3285 static const struct bfd_elf_special_section special_sections_z[] =
   3286 {
   3287   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   3288   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   3289   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   3290   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   3291   { NULL,		      0,  0, 0,		   0 }
   3292 };
   3293 
   3294 static const struct bfd_elf_special_section * const special_sections[] =
   3295 {
   3296   special_sections_b,		/* 'b' */
   3297   special_sections_c,		/* 'c' */
   3298   special_sections_d,		/* 'd' */
   3299   NULL,				/* 'e' */
   3300   special_sections_f,		/* 'f' */
   3301   special_sections_g,		/* 'g' */
   3302   special_sections_h,		/* 'h' */
   3303   special_sections_i,		/* 'i' */
   3304   NULL,				/* 'j' */
   3305   NULL,				/* 'k' */
   3306   special_sections_l,		/* 'l' */
   3307   NULL,				/* 'm' */
   3308   special_sections_n,		/* 'n' */
   3309   NULL,				/* 'o' */
   3310   special_sections_p,		/* 'p' */
   3311   NULL,				/* 'q' */
   3312   special_sections_r,		/* 'r' */
   3313   special_sections_s,		/* 's' */
   3314   special_sections_t,		/* 't' */
   3315   NULL,				/* 'u' */
   3316   NULL,				/* 'v' */
   3317   NULL,				/* 'w' */
   3318   NULL,				/* 'x' */
   3319   NULL,				/* 'y' */
   3320   special_sections_z		/* 'z' */
   3321 };
   3322 
   3323 const struct bfd_elf_special_section *
   3324 _bfd_elf_get_special_section (const char *name,
   3325 			      const struct bfd_elf_special_section *spec,
   3326 			      unsigned int rela)
   3327 {
   3328   int i;
   3329   int len;
   3330 
   3331   len = strlen (name);
   3332 
   3333   for (i = 0; spec[i].prefix != NULL; i++)
   3334     {
   3335       int suffix_len;
   3336       int prefix_len = spec[i].prefix_length;
   3337 
   3338       if (len < prefix_len)
   3339 	continue;
   3340       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   3341 	continue;
   3342 
   3343       suffix_len = spec[i].suffix_length;
   3344       if (suffix_len <= 0)
   3345 	{
   3346 	  if (name[prefix_len] != 0)
   3347 	    {
   3348 	      if (suffix_len == 0)
   3349 		continue;
   3350 	      if (name[prefix_len] != '.'
   3351 		  && (suffix_len == -2
   3352 		      || (rela && spec[i].type == SHT_REL)))
   3353 		continue;
   3354 	    }
   3355 	}
   3356       else
   3357 	{
   3358 	  if (len < prefix_len + suffix_len)
   3359 	    continue;
   3360 	  if (memcmp (name + len - suffix_len,
   3361 		      spec[i].prefix + prefix_len,
   3362 		      suffix_len) != 0)
   3363 	    continue;
   3364 	}
   3365       return &spec[i];
   3366     }
   3367 
   3368   return NULL;
   3369 }
   3370 
   3371 const struct bfd_elf_special_section *
   3372 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   3373 {
   3374   int i;
   3375   const struct bfd_elf_special_section *spec;
   3376   const struct elf_backend_data *bed;
   3377 
   3378   /* See if this is one of the special sections.  */
   3379   if (sec->name == NULL)
   3380     return NULL;
   3381 
   3382   bed = get_elf_backend_data (abfd);
   3383   spec = bed->special_sections;
   3384   if (spec)
   3385     {
   3386       spec = _bfd_elf_get_special_section (sec->name,
   3387 					   bed->special_sections,
   3388 					   sec->use_rela_p);
   3389       if (spec != NULL)
   3390 	return spec;
   3391     }
   3392 
   3393   if (sec->name[0] != '.')
   3394     return NULL;
   3395 
   3396   i = sec->name[1] - 'b';
   3397   if (i < 0 || i > 'z' - 'b')
   3398     return NULL;
   3399 
   3400   spec = special_sections[i];
   3401 
   3402   if (spec == NULL)
   3403     return NULL;
   3404 
   3405   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   3406 }
   3407 
   3408 bool
   3409 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   3410 {
   3411   struct bfd_elf_section_data *sdata;
   3412   const struct elf_backend_data *bed;
   3413   const struct bfd_elf_special_section *ssect;
   3414 
   3415   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   3416   if (sdata == NULL)
   3417     {
   3418       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   3419 							  sizeof (*sdata));
   3420       if (sdata == NULL)
   3421 	return false;
   3422       sec->used_by_bfd = sdata;
   3423     }
   3424 
   3425   /* Indicate whether or not this section should use RELA relocations.  */
   3426   bed = get_elf_backend_data (abfd);
   3427   sec->use_rela_p = bed->default_use_rela_p;
   3428 
   3429   /* Set up ELF section type and flags for newly created sections, if
   3430      there is an ABI mandated section.  */
   3431   ssect = (*bed->get_sec_type_attr) (abfd, sec);
   3432   if (ssect != NULL)
   3433     {
   3434       elf_section_type (sec) = ssect->type;
   3435       elf_section_flags (sec) = ssect->attr;
   3436     }
   3437 
   3438   return _bfd_generic_new_section_hook (abfd, sec);
   3439 }
   3440 
   3441 /* Create a new bfd section from an ELF program header.
   3442 
   3443    Since program segments have no names, we generate a synthetic name
   3444    of the form segment<NUM>, where NUM is generally the index in the
   3445    program header table.  For segments that are split (see below) we
   3446    generate the names segment<NUM>a and segment<NUM>b.
   3447 
   3448    Note that some program segments may have a file size that is different than
   3449    (less than) the memory size.  All this means is that at execution the
   3450    system must allocate the amount of memory specified by the memory size,
   3451    but only initialize it with the first "file size" bytes read from the
   3452    file.  This would occur for example, with program segments consisting
   3453    of combined data+bss.
   3454 
   3455    To handle the above situation, this routine generates TWO bfd sections
   3456    for the single program segment.  The first has the length specified by
   3457    the file size of the segment, and the second has the length specified
   3458    by the difference between the two sizes.  In effect, the segment is split
   3459    into its initialized and uninitialized parts.  */
   3460 
   3461 bool
   3462 _bfd_elf_make_section_from_phdr (bfd *abfd,
   3463 				 Elf_Internal_Phdr *hdr,
   3464 				 int hdr_index,
   3465 				 const char *type_name)
   3466 {
   3467   asection *newsect;
   3468   char *name;
   3469   char namebuf[64];
   3470   size_t len;
   3471   int split;
   3472   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   3473 
   3474   split = ((hdr->p_memsz > 0)
   3475 	    && (hdr->p_filesz > 0)
   3476 	    && (hdr->p_memsz > hdr->p_filesz));
   3477 
   3478   if (hdr->p_filesz > 0)
   3479     {
   3480       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   3481       len = strlen (namebuf) + 1;
   3482       name = (char *) bfd_alloc (abfd, len);
   3483       if (!name)
   3484 	return false;
   3485       memcpy (name, namebuf, len);
   3486       newsect = bfd_make_section (abfd, name);
   3487       if (newsect == NULL)
   3488 	return false;
   3489       newsect->vma = hdr->p_vaddr / opb;
   3490       newsect->lma = hdr->p_paddr / opb;
   3491       newsect->size = hdr->p_filesz;
   3492       newsect->filepos = hdr->p_offset;
   3493       newsect->flags |= SEC_HAS_CONTENTS;
   3494       newsect->alignment_power = bfd_log2 (hdr->p_align);
   3495       if (hdr->p_type == PT_LOAD)
   3496 	{
   3497 	  newsect->flags |= SEC_ALLOC;
   3498 	  newsect->flags |= SEC_LOAD;
   3499 	  if (hdr->p_flags & PF_X)
   3500 	    {
   3501 	      /* FIXME: all we known is that it has execute PERMISSION,
   3502 		 may be data.  */
   3503 	      newsect->flags |= SEC_CODE;
   3504 	    }
   3505 	}
   3506       if (!(hdr->p_flags & PF_W))
   3507 	{
   3508 	  newsect->flags |= SEC_READONLY;
   3509 	}
   3510     }
   3511 
   3512   if (hdr->p_memsz > hdr->p_filesz)
   3513     {
   3514       bfd_vma align;
   3515 
   3516       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   3517       len = strlen (namebuf) + 1;
   3518       name = (char *) bfd_alloc (abfd, len);
   3519       if (!name)
   3520 	return false;
   3521       memcpy (name, namebuf, len);
   3522       newsect = bfd_make_section (abfd, name);
   3523       if (newsect == NULL)
   3524 	return false;
   3525       newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
   3526       newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
   3527       newsect->size = hdr->p_memsz - hdr->p_filesz;
   3528       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   3529       align = newsect->vma & -newsect->vma;
   3530       if (align == 0 || align > hdr->p_align)
   3531 	align = hdr->p_align;
   3532       newsect->alignment_power = bfd_log2 (align);
   3533       if (hdr->p_type == PT_LOAD)
   3534 	{
   3535 	  newsect->flags |= SEC_ALLOC;
   3536 	  if (hdr->p_flags & PF_X)
   3537 	    newsect->flags |= SEC_CODE;
   3538 	}
   3539       if (!(hdr->p_flags & PF_W))
   3540 	newsect->flags |= SEC_READONLY;
   3541     }
   3542 
   3543   return true;
   3544 }
   3545 
   3546 static bool
   3547 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   3548 {
   3549   /* The return value is ignored.  Build-ids are considered optional.  */
   3550   if (templ->xvec->flavour == bfd_target_elf_flavour)
   3551     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   3552       (templ, offset);
   3553   return false;
   3554 }
   3555 
   3556 bool
   3557 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3558 {
   3559   const struct elf_backend_data *bed;
   3560 
   3561   switch (hdr->p_type)
   3562     {
   3563     case PT_NULL:
   3564       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3565 
   3566     case PT_LOAD:
   3567       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3568 	return false;
   3569       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3570 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3571       return true;
   3572 
   3573     case PT_DYNAMIC:
   3574       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3575 
   3576     case PT_INTERP:
   3577       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3578 
   3579     case PT_NOTE:
   3580       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3581 	return false;
   3582       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3583 			    hdr->p_align))
   3584 	return false;
   3585       return true;
   3586 
   3587     case PT_SHLIB:
   3588       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3589 
   3590     case PT_PHDR:
   3591       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3592 
   3593     case PT_GNU_EH_FRAME:
   3594       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3595 					      "eh_frame_hdr");
   3596 
   3597     case PT_GNU_STACK:
   3598       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3599 
   3600     case PT_GNU_RELRO:
   3601       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3602 
   3603     case PT_GNU_SFRAME:
   3604       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3605 					      "sframe");
   3606 
   3607     default:
   3608       /* Check for any processor-specific program segment types.  */
   3609       bed = get_elf_backend_data (abfd);
   3610       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3611     }
   3612 }
   3613 
   3614 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3615    REL or RELA.  */
   3616 
   3617 Elf_Internal_Shdr *
   3618 _bfd_elf_single_rel_hdr (asection *sec)
   3619 {
   3620   if (elf_section_data (sec)->rel.hdr)
   3621     {
   3622       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3623       return elf_section_data (sec)->rel.hdr;
   3624     }
   3625   else
   3626     return elf_section_data (sec)->rela.hdr;
   3627 }
   3628 
   3629 static bool
   3630 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3631 			    Elf_Internal_Shdr *rel_hdr,
   3632 			    const char *sec_name,
   3633 			    bool use_rela_p)
   3634 {
   3635   char *name = (char *) bfd_alloc (abfd,
   3636 				   sizeof ".rela" + strlen (sec_name));
   3637   if (name == NULL)
   3638     return false;
   3639 
   3640   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3641   rel_hdr->sh_name =
   3642     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3643 					false);
   3644   if (rel_hdr->sh_name == (unsigned int) -1)
   3645     return false;
   3646 
   3647   return true;
   3648 }
   3649 
   3650 /* Allocate and initialize a section-header for a new reloc section,
   3651    containing relocations against ASECT.  It is stored in RELDATA.  If
   3652    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3653    relocations.  */
   3654 
   3655 static bool
   3656 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3657 			  struct bfd_elf_section_reloc_data *reldata,
   3658 			  const char *sec_name,
   3659 			  bool use_rela_p,
   3660 			  bool delay_st_name_p)
   3661 {
   3662   Elf_Internal_Shdr *rel_hdr;
   3663   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3664 
   3665   BFD_ASSERT (reldata->hdr == NULL);
   3666   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3667   if (rel_hdr == NULL)
   3668     return false;
   3669   reldata->hdr = rel_hdr;
   3670 
   3671   if (delay_st_name_p)
   3672     rel_hdr->sh_name = (unsigned int) -1;
   3673   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3674 					use_rela_p))
   3675     return false;
   3676   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3677   rel_hdr->sh_entsize = (use_rela_p
   3678 			 ? bed->s->sizeof_rela
   3679 			 : bed->s->sizeof_rel);
   3680   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3681   rel_hdr->sh_flags = 0;
   3682   rel_hdr->sh_addr = 0;
   3683   rel_hdr->sh_size = 0;
   3684   rel_hdr->sh_offset = 0;
   3685 
   3686   return true;
   3687 }
   3688 
   3689 /* Return the default section type based on the passed in section flags.  */
   3690 
   3691 int
   3692 bfd_elf_get_default_section_type (flagword flags)
   3693 {
   3694   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3695       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3696     return SHT_NOBITS;
   3697   return SHT_PROGBITS;
   3698 }
   3699 
   3700 struct fake_section_arg
   3701 {
   3702   struct bfd_link_info *link_info;
   3703   bool failed;
   3704 };
   3705 
   3706 /* Set up an ELF internal section header for a section.  */
   3707 
   3708 static void
   3709 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3710 {
   3711   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3712   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3713   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3714   Elf_Internal_Shdr *this_hdr;
   3715   unsigned int sh_type;
   3716   const char *name = asect->name;
   3717   bool delay_st_name_p = false;
   3718   bfd_vma mask;
   3719 
   3720   if (arg->failed)
   3721     {
   3722       /* We already failed; just get out of the bfd_map_over_sections
   3723 	 loop.  */
   3724       return;
   3725     }
   3726 
   3727   this_hdr = &esd->this_hdr;
   3728 
   3729   /* ld: compress DWARF debug sections with names: .debug_*.  */
   3730   if (arg->link_info
   3731       && (abfd->flags & BFD_COMPRESS) != 0
   3732       && (asect->flags & SEC_DEBUGGING) != 0
   3733       && name[1] == 'd'
   3734       && name[6] == '_')
   3735     {
   3736       /* If this section will be compressed, delay adding section
   3737 	 name to section name section after it is compressed in
   3738 	 _bfd_elf_assign_file_positions_for_non_load.  */
   3739       delay_st_name_p = true;
   3740     }
   3741 
   3742   if (delay_st_name_p)
   3743     this_hdr->sh_name = (unsigned int) -1;
   3744   else
   3745     {
   3746       this_hdr->sh_name
   3747 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3748 					      name, false);
   3749       if (this_hdr->sh_name == (unsigned int) -1)
   3750 	{
   3751 	  arg->failed = true;
   3752 	  return;
   3753 	}
   3754     }
   3755 
   3756   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3757 
   3758   if ((asect->flags & SEC_ALLOC) != 0
   3759       || asect->user_set_vma)
   3760     this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
   3761   else
   3762     this_hdr->sh_addr = 0;
   3763 
   3764   this_hdr->sh_offset = 0;
   3765   this_hdr->sh_size = asect->size;
   3766   this_hdr->sh_link = 0;
   3767   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3768   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3769     {
   3770       _bfd_error_handler
   3771 	/* xgettext:c-format */
   3772 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3773 	 abfd, asect->alignment_power, asect);
   3774       arg->failed = true;
   3775       return;
   3776     }
   3777   /* Set sh_addralign to the highest power of two given by alignment
   3778      consistent with the section VMA.  Linker scripts can force VMA.  */
   3779   mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
   3780   this_hdr->sh_addralign = mask & -mask;
   3781   /* The sh_entsize and sh_info fields may have been set already by
   3782      copy_private_section_data.  */
   3783 
   3784   this_hdr->bfd_section = asect;
   3785   this_hdr->contents = NULL;
   3786 
   3787   /* If the section type is unspecified, we set it based on
   3788      asect->flags.  */
   3789   if (asect->type != 0)
   3790     sh_type = asect->type;
   3791   else if ((asect->flags & SEC_GROUP) != 0)
   3792     sh_type = SHT_GROUP;
   3793   else
   3794     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3795 
   3796   if (this_hdr->sh_type == SHT_NULL)
   3797     this_hdr->sh_type = sh_type;
   3798   else if (this_hdr->sh_type == SHT_NOBITS
   3799 	   && sh_type == SHT_PROGBITS
   3800 	   && (asect->flags & SEC_ALLOC) != 0)
   3801     {
   3802       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3803 	 allow the link to proceed.  This can happen when users link
   3804 	 non-bss input sections to bss output sections, or emit data
   3805 	 to a bss output section via a linker script.  */
   3806       _bfd_error_handler
   3807 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3808       this_hdr->sh_type = sh_type;
   3809     }
   3810 
   3811   switch (this_hdr->sh_type)
   3812     {
   3813     default:
   3814       break;
   3815 
   3816     case SHT_STRTAB:
   3817     case SHT_NOTE:
   3818     case SHT_NOBITS:
   3819     case SHT_PROGBITS:
   3820       break;
   3821 
   3822     case SHT_INIT_ARRAY:
   3823     case SHT_FINI_ARRAY:
   3824     case SHT_PREINIT_ARRAY:
   3825       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3826       break;
   3827 
   3828     case SHT_HASH:
   3829       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3830       break;
   3831 
   3832     case SHT_DYNSYM:
   3833       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3834       break;
   3835 
   3836     case SHT_DYNAMIC:
   3837       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3838       break;
   3839 
   3840     case SHT_RELA:
   3841       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3842 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3843       break;
   3844 
   3845      case SHT_REL:
   3846       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3847 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3848       break;
   3849 
   3850      case SHT_GNU_versym:
   3851       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3852       break;
   3853 
   3854      case SHT_GNU_verdef:
   3855       this_hdr->sh_entsize = 0;
   3856       /* objcopy or strip will copy over sh_info, but may not set
   3857 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3858 	 zero.  */
   3859       if (this_hdr->sh_info == 0)
   3860 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3861       else
   3862 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3863 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3864       break;
   3865 
   3866     case SHT_GNU_verneed:
   3867       this_hdr->sh_entsize = 0;
   3868       /* objcopy or strip will copy over sh_info, but may not set
   3869 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3870 	 zero.  */
   3871       if (this_hdr->sh_info == 0)
   3872 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3873       else
   3874 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3875 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3876       break;
   3877 
   3878     case SHT_GROUP:
   3879       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3880       break;
   3881 
   3882     case SHT_GNU_HASH:
   3883       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3884       break;
   3885     }
   3886 
   3887   if ((asect->flags & SEC_ALLOC) != 0)
   3888     this_hdr->sh_flags |= SHF_ALLOC;
   3889   if ((asect->flags & SEC_READONLY) == 0)
   3890     this_hdr->sh_flags |= SHF_WRITE;
   3891   if ((asect->flags & SEC_CODE) != 0)
   3892     this_hdr->sh_flags |= SHF_EXECINSTR;
   3893   if ((asect->flags & SEC_MERGE) != 0)
   3894     {
   3895       this_hdr->sh_flags |= SHF_MERGE;
   3896       this_hdr->sh_entsize = asect->entsize;
   3897     }
   3898   if ((asect->flags & SEC_STRINGS) != 0)
   3899     this_hdr->sh_flags |= SHF_STRINGS;
   3900   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3901     this_hdr->sh_flags |= SHF_GROUP;
   3902   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3903     {
   3904       this_hdr->sh_flags |= SHF_TLS;
   3905       if (asect->size == 0
   3906 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3907 	{
   3908 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3909 
   3910 	  this_hdr->sh_size = 0;
   3911 	  if (o != NULL)
   3912 	    {
   3913 	      this_hdr->sh_size = o->offset + o->size;
   3914 	      if (this_hdr->sh_size != 0)
   3915 		this_hdr->sh_type = SHT_NOBITS;
   3916 	    }
   3917 	}
   3918     }
   3919   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3920     this_hdr->sh_flags |= SHF_EXCLUDE;
   3921 
   3922   /* If the section has relocs, set up a section header for the
   3923      SHT_REL[A] section.  If two relocation sections are required for
   3924      this section, it is up to the processor-specific back-end to
   3925      create the other.  */
   3926   if ((asect->flags & SEC_RELOC) != 0)
   3927     {
   3928       /* When doing a relocatable link, create both REL and RELA sections if
   3929 	 needed.  */
   3930       if (arg->link_info
   3931 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3932 	  && esd->rel.count + esd->rela.count > 0
   3933 	  && (bfd_link_relocatable (arg->link_info)
   3934 	      || arg->link_info->emitrelocations))
   3935 	{
   3936 	  if (esd->rel.count && esd->rel.hdr == NULL
   3937 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3938 					    false, delay_st_name_p))
   3939 	    {
   3940 	      arg->failed = true;
   3941 	      return;
   3942 	    }
   3943 	  if (esd->rela.count && esd->rela.hdr == NULL
   3944 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3945 					    true, delay_st_name_p))
   3946 	    {
   3947 	      arg->failed = true;
   3948 	      return;
   3949 	    }
   3950 	}
   3951       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3952 					  (asect->use_rela_p
   3953 					   ? &esd->rela : &esd->rel),
   3954 					  name,
   3955 					  asect->use_rela_p,
   3956 					  delay_st_name_p))
   3957 	{
   3958 	  arg->failed = true;
   3959 	  return;
   3960 	}
   3961     }
   3962 
   3963   /* Check for processor-specific section types.  */
   3964   sh_type = this_hdr->sh_type;
   3965   if (bed->elf_backend_fake_sections
   3966       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3967     {
   3968       arg->failed = true;
   3969       return;
   3970     }
   3971 
   3972   if (sh_type == SHT_NOBITS && asect->size != 0)
   3973     {
   3974       /* Don't change the header type from NOBITS if we are being
   3975 	 called for objcopy --only-keep-debug.  */
   3976       this_hdr->sh_type = sh_type;
   3977     }
   3978 }
   3979 
   3980 /* Fill in the contents of a SHT_GROUP section.  Called from
   3981    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3982    when ELF targets use the generic linker, ld.  Called for ld -r
   3983    from bfd_elf_final_link.  */
   3984 
   3985 void
   3986 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3987 {
   3988   bool *failedptr = (bool *) failedptrarg;
   3989   asection *elt, *first;
   3990   unsigned char *loc;
   3991   bool gas;
   3992 
   3993   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3994      elfxx-ia64.c.  */
   3995   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3996       || sec->size == 0
   3997       || *failedptr)
   3998     return;
   3999 
   4000   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   4001     {
   4002       unsigned long symindx = 0;
   4003 
   4004       /* elf_group_id will have been set up by objcopy and the
   4005 	 generic linker.  */
   4006       if (elf_group_id (sec) != NULL)
   4007 	symindx = elf_group_id (sec)->udata.i;
   4008 
   4009       if (symindx == 0)
   4010 	{
   4011 	  /* If called from the assembler, swap_out_syms will have set up
   4012 	     elf_section_syms.
   4013 	     PR 25699: A corrupt input file could contain bogus group info.  */
   4014 	  if (sec->index >= elf_num_section_syms (abfd)
   4015 	      || elf_section_syms (abfd)[sec->index] == NULL)
   4016 	    {
   4017 	      *failedptr = true;
   4018 	      return;
   4019 	    }
   4020 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   4021 	}
   4022       elf_section_data (sec)->this_hdr.sh_info = symindx;
   4023     }
   4024   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   4025     {
   4026       /* The ELF backend linker sets sh_info to -2 when the group
   4027 	 signature symbol is global, and thus the index can't be
   4028 	 set until all local symbols are output.  */
   4029       asection *igroup;
   4030       struct bfd_elf_section_data *sec_data;
   4031       unsigned long symndx;
   4032       unsigned long extsymoff;
   4033       struct elf_link_hash_entry *h;
   4034 
   4035       /* The point of this little dance to the first SHF_GROUP section
   4036 	 then back to the SHT_GROUP section is that this gets us to
   4037 	 the SHT_GROUP in the input object.  */
   4038       igroup = elf_sec_group (elf_next_in_group (sec));
   4039       sec_data = elf_section_data (igroup);
   4040       symndx = sec_data->this_hdr.sh_info;
   4041       extsymoff = 0;
   4042       if (!elf_bad_symtab (igroup->owner))
   4043 	{
   4044 	  Elf_Internal_Shdr *symtab_hdr;
   4045 
   4046 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   4047 	  extsymoff = symtab_hdr->sh_info;
   4048 	}
   4049       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   4050       while (h->root.type == bfd_link_hash_indirect
   4051 	     || h->root.type == bfd_link_hash_warning)
   4052 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4053 
   4054       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   4055     }
   4056 
   4057   /* The contents won't be allocated for "ld -r" or objcopy.  */
   4058   gas = true;
   4059   if (sec->contents == NULL)
   4060     {
   4061       gas = false;
   4062       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   4063 
   4064       /* Arrange for the section to be written out.  */
   4065       elf_section_data (sec)->this_hdr.contents = sec->contents;
   4066       if (sec->contents == NULL)
   4067 	{
   4068 	  *failedptr = true;
   4069 	  return;
   4070 	}
   4071     }
   4072 
   4073   loc = sec->contents + sec->size;
   4074 
   4075   /* Get the pointer to the first section in the group that gas
   4076      squirreled away here.  objcopy arranges for this to be set to the
   4077      start of the input section group.  */
   4078   first = elt = elf_next_in_group (sec);
   4079 
   4080   /* First element is a flag word.  Rest of section is elf section
   4081      indices for all the sections of the group.  Write them backwards
   4082      just to keep the group in the same order as given in .section
   4083      directives, not that it matters.  */
   4084   while (elt != NULL)
   4085     {
   4086       asection *s;
   4087 
   4088       s = elt;
   4089       if (!gas)
   4090 	s = s->output_section;
   4091       if (s != NULL
   4092 	  && !bfd_is_abs_section (s))
   4093 	{
   4094 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   4095 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   4096 
   4097 	  if (elf_sec->rel.hdr != NULL
   4098 	      && (gas
   4099 		  || (input_elf_sec->rel.hdr != NULL
   4100 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   4101 	    {
   4102 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   4103 	      loc -= 4;
   4104 	      if (loc == sec->contents)
   4105 		break;
   4106 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   4107 	    }
   4108 	  if (elf_sec->rela.hdr != NULL
   4109 	      && (gas
   4110 		  || (input_elf_sec->rela.hdr != NULL
   4111 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   4112 	    {
   4113 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   4114 	      loc -= 4;
   4115 	      if (loc == sec->contents)
   4116 		break;
   4117 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   4118 	    }
   4119 	  loc -= 4;
   4120 	  if (loc == sec->contents)
   4121 	    break;
   4122 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   4123 	}
   4124       elt = elf_next_in_group (elt);
   4125       if (elt == first)
   4126 	break;
   4127     }
   4128 
   4129   /* We should always get here with loc == sec->contents + 4, but it is
   4130      possible to craft bogus SHT_GROUP sections that will cause segfaults
   4131      in objcopy without checking loc here and in the loop above.  */
   4132   if (loc == sec->contents)
   4133     BFD_ASSERT (0);
   4134   else
   4135     {
   4136       loc -= 4;
   4137       if (loc != sec->contents)
   4138 	{
   4139 	  BFD_ASSERT (0);
   4140 	  memset (sec->contents + 4, 0, loc - sec->contents);
   4141 	  loc = sec->contents;
   4142 	}
   4143     }
   4144 
   4145   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   4146 }
   4147 
   4148 /* Given NAME, the name of a relocation section stripped of its
   4149    .rel/.rela prefix, return the section in ABFD to which the
   4150    relocations apply.  */
   4151 
   4152 asection *
   4153 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   4154 {
   4155   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   4156      section likely apply to .got.plt or .got section.  */
   4157   if (get_elf_backend_data (abfd)->want_got_plt
   4158       && strcmp (name, ".plt") == 0)
   4159     {
   4160       asection *sec;
   4161 
   4162       name = ".got.plt";
   4163       sec = bfd_get_section_by_name (abfd, name);
   4164       if (sec != NULL)
   4165 	return sec;
   4166       name = ".got";
   4167     }
   4168 
   4169   return bfd_get_section_by_name (abfd, name);
   4170 }
   4171 
   4172 /* Return the section to which RELOC_SEC applies.  */
   4173 
   4174 static asection *
   4175 elf_get_reloc_section (asection *reloc_sec)
   4176 {
   4177   const char *name;
   4178   unsigned int type;
   4179   bfd *abfd;
   4180   const struct elf_backend_data *bed;
   4181 
   4182   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   4183   if (type != SHT_REL && type != SHT_RELA)
   4184     return NULL;
   4185 
   4186   /* We look up the section the relocs apply to by name.  */
   4187   name = reloc_sec->name;
   4188   if (!startswith (name, ".rel"))
   4189     return NULL;
   4190   name += 4;
   4191   if (type == SHT_RELA && *name++ != 'a')
   4192     return NULL;
   4193 
   4194   abfd = reloc_sec->owner;
   4195   bed = get_elf_backend_data (abfd);
   4196   return bed->get_reloc_section (abfd, name);
   4197 }
   4198 
   4199 /* Assign all ELF section numbers.  The dummy first section is handled here
   4200    too.  The link/info pointers for the standard section types are filled
   4201    in here too, while we're at it.  LINK_INFO will be 0 when arriving
   4202    here for gas, objcopy, and when using the generic ELF linker.  */
   4203 
   4204 static bool
   4205 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   4206 {
   4207   struct elf_obj_tdata *t = elf_tdata (abfd);
   4208   asection *sec;
   4209   unsigned int section_number;
   4210   Elf_Internal_Shdr **i_shdrp;
   4211   struct bfd_elf_section_data *d;
   4212   bool need_symtab;
   4213   size_t amt;
   4214 
   4215   section_number = 1;
   4216 
   4217   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   4218 
   4219   /* SHT_GROUP sections are in relocatable files only.  */
   4220   if (link_info == NULL || !link_info->resolve_section_groups)
   4221     {
   4222       size_t reloc_count = 0;
   4223 
   4224       /* Put SHT_GROUP sections first.  */
   4225       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   4226 	{
   4227 	  d = elf_section_data (sec);
   4228 
   4229 	  if (d->this_hdr.sh_type == SHT_GROUP)
   4230 	    {
   4231 	      if (sec->flags & SEC_LINKER_CREATED)
   4232 		{
   4233 		  /* Remove the linker created SHT_GROUP sections.  */
   4234 		  bfd_section_list_remove (abfd, sec);
   4235 		  abfd->section_count--;
   4236 		}
   4237 	      else
   4238 		d->this_idx = section_number++;
   4239 	    }
   4240 
   4241 	  /* Count relocations.  */
   4242 	  reloc_count += sec->reloc_count;
   4243 	}
   4244 
   4245       /* Set/clear HAS_RELOC depending on whether there are relocations.  */
   4246       if (reloc_count == 0)
   4247 	abfd->flags &= ~HAS_RELOC;
   4248       else
   4249 	abfd->flags |= HAS_RELOC;
   4250     }
   4251 
   4252   for (sec = abfd->sections; sec; sec = sec->next)
   4253     {
   4254       d = elf_section_data (sec);
   4255 
   4256       if (d->this_hdr.sh_type != SHT_GROUP)
   4257 	d->this_idx = section_number++;
   4258       if (d->this_hdr.sh_name != (unsigned int) -1)
   4259 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   4260       if (d->rel.hdr)
   4261 	{
   4262 	  d->rel.idx = section_number++;
   4263 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   4264 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   4265 	}
   4266       else
   4267 	d->rel.idx = 0;
   4268 
   4269       if (d->rela.hdr)
   4270 	{
   4271 	  d->rela.idx = section_number++;
   4272 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   4273 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   4274 	}
   4275       else
   4276 	d->rela.idx = 0;
   4277     }
   4278 
   4279   need_symtab = (bfd_get_symcount (abfd) > 0
   4280 		 || (link_info == NULL
   4281 		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4282 			 == HAS_RELOC)));
   4283   if (need_symtab)
   4284     {
   4285       elf_onesymtab (abfd) = section_number++;
   4286       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   4287       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   4288 	{
   4289 	  elf_section_list *entry;
   4290 
   4291 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   4292 
   4293 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   4294 	  entry->ndx = section_number++;
   4295 	  elf_symtab_shndx_list (abfd) = entry;
   4296 	  entry->hdr.sh_name
   4297 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   4298 						  ".symtab_shndx", false);
   4299 	  if (entry->hdr.sh_name == (unsigned int) -1)
   4300 	    return false;
   4301 	}
   4302       elf_strtab_sec (abfd) = section_number++;
   4303       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   4304     }
   4305 
   4306   elf_shstrtab_sec (abfd) = section_number++;
   4307   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   4308   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   4309 
   4310   if (section_number >= SHN_LORESERVE)
   4311     {
   4312       /* xgettext:c-format */
   4313       _bfd_error_handler (_("%pB: too many sections: %u"),
   4314 			  abfd, section_number);
   4315       return false;
   4316     }
   4317 
   4318   elf_numsections (abfd) = section_number;
   4319   elf_elfheader (abfd)->e_shnum = section_number;
   4320 
   4321   /* Set up the list of section header pointers, in agreement with the
   4322      indices.  */
   4323   amt = section_number * sizeof (Elf_Internal_Shdr *);
   4324   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
   4325   if (i_shdrp == NULL)
   4326     return false;
   4327 
   4328   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   4329 						 sizeof (Elf_Internal_Shdr));
   4330   if (i_shdrp[0] == NULL)
   4331     {
   4332       bfd_release (abfd, i_shdrp);
   4333       return false;
   4334     }
   4335 
   4336   elf_elfsections (abfd) = i_shdrp;
   4337 
   4338   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   4339   if (need_symtab)
   4340     {
   4341       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   4342       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   4343 	{
   4344 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   4345 	  BFD_ASSERT (entry != NULL);
   4346 	  i_shdrp[entry->ndx] = & entry->hdr;
   4347 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   4348 	}
   4349       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   4350       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   4351     }
   4352 
   4353   for (sec = abfd->sections; sec; sec = sec->next)
   4354     {
   4355       asection *s;
   4356 
   4357       d = elf_section_data (sec);
   4358 
   4359       i_shdrp[d->this_idx] = &d->this_hdr;
   4360       if (d->rel.idx != 0)
   4361 	i_shdrp[d->rel.idx] = d->rel.hdr;
   4362       if (d->rela.idx != 0)
   4363 	i_shdrp[d->rela.idx] = d->rela.hdr;
   4364 
   4365       /* Fill in the sh_link and sh_info fields while we're at it.  */
   4366 
   4367       /* sh_link of a reloc section is the section index of the symbol
   4368 	 table.  sh_info is the section index of the section to which
   4369 	 the relocation entries apply.  */
   4370       if (d->rel.idx != 0)
   4371 	{
   4372 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   4373 	  d->rel.hdr->sh_info = d->this_idx;
   4374 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   4375 	}
   4376       if (d->rela.idx != 0)
   4377 	{
   4378 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   4379 	  d->rela.hdr->sh_info = d->this_idx;
   4380 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   4381 	}
   4382 
   4383       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   4384       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   4385 	{
   4386 	  s = elf_linked_to_section (sec);
   4387 	  /* We can now have a NULL linked section pointer.
   4388 	     This happens when the sh_link field is 0, which is done
   4389 	     when a linked to section is discarded but the linking
   4390 	     section has been retained for some reason.  */
   4391 	  if (s)
   4392 	    {
   4393 	      /* Check discarded linkonce section.  */
   4394 	      if (discarded_section (s))
   4395 		{
   4396 		  asection *kept;
   4397 		  _bfd_error_handler
   4398 		    /* xgettext:c-format */
   4399 		    (_("%pB: sh_link of section `%pA' points to"
   4400 		       " discarded section `%pA' of `%pB'"),
   4401 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   4402 		  /* Point to the kept section if it has the same
   4403 		     size as the discarded one.  */
   4404 		  kept = _bfd_elf_check_kept_section (s, link_info);
   4405 		  if (kept == NULL)
   4406 		    {
   4407 		      bfd_set_error (bfd_error_bad_value);
   4408 		      return false;
   4409 		    }
   4410 		  s = kept;
   4411 		}
   4412 	      /* Handle objcopy. */
   4413 	      else if (s->output_section == NULL)
   4414 		{
   4415 		  _bfd_error_handler
   4416 		    /* xgettext:c-format */
   4417 		    (_("%pB: sh_link of section `%pA' points to"
   4418 		       " removed section `%pA' of `%pB'"),
   4419 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   4420 		  bfd_set_error (bfd_error_bad_value);
   4421 		  return false;
   4422 		}
   4423 	      s = s->output_section;
   4424 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4425 	    }
   4426 	}
   4427 
   4428       switch (d->this_hdr.sh_type)
   4429 	{
   4430 	case SHT_REL:
   4431 	case SHT_RELA:
   4432 	  /* sh_link is the section index of the symbol table.
   4433 	     sh_info is the section index of the section to which the
   4434 	     relocation entries apply.  */
   4435 	  if (d->this_hdr.sh_link == 0)
   4436 	    {
   4437 	      /* FIXME maybe: If this is a reloc section which we are
   4438 		 treating as a normal section then we likely should
   4439 		 not be assuming its sh_link is .dynsym or .symtab.  */
   4440 	      if ((sec->flags & SEC_ALLOC) != 0)
   4441 		{
   4442 		  s = bfd_get_section_by_name (abfd, ".dynsym");
   4443 		  if (s != NULL)
   4444 		    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4445 		}
   4446 	      else
   4447 		d->this_hdr.sh_link = elf_onesymtab (abfd);
   4448 	    }
   4449 
   4450 	  s = elf_get_reloc_section (sec);
   4451 	  if (s != NULL)
   4452 	    {
   4453 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   4454 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   4455 	    }
   4456 	  break;
   4457 
   4458 	case SHT_STRTAB:
   4459 	  /* We assume that a section named .stab*str is a stabs
   4460 	     string section.  We look for a section with the same name
   4461 	     but without the trailing ``str'', and set its sh_link
   4462 	     field to point to this section.  */
   4463 	  if (startswith (sec->name, ".stab")
   4464 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   4465 	    {
   4466 	      size_t len;
   4467 	      char *alc;
   4468 
   4469 	      len = strlen (sec->name);
   4470 	      alc = (char *) bfd_malloc (len - 2);
   4471 	      if (alc == NULL)
   4472 		return false;
   4473 	      memcpy (alc, sec->name, len - 3);
   4474 	      alc[len - 3] = '\0';
   4475 	      s = bfd_get_section_by_name (abfd, alc);
   4476 	      free (alc);
   4477 	      if (s != NULL)
   4478 		{
   4479 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   4480 
   4481 		  /* This is a .stab section.  */
   4482 		  elf_section_data (s)->this_hdr.sh_entsize = 12;
   4483 		}
   4484 	    }
   4485 	  break;
   4486 
   4487 	case SHT_DYNAMIC:
   4488 	case SHT_DYNSYM:
   4489 	case SHT_GNU_verneed:
   4490 	case SHT_GNU_verdef:
   4491 	  /* sh_link is the section header index of the string table
   4492 	     used for the dynamic entries, or the symbol table, or the
   4493 	     version strings.  */
   4494 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   4495 	  if (s != NULL)
   4496 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4497 	  break;
   4498 
   4499 	case SHT_GNU_LIBLIST:
   4500 	  /* sh_link is the section header index of the prelink library
   4501 	     list used for the dynamic entries, or the symbol table, or
   4502 	     the version strings.  */
   4503 	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
   4504 					      ? ".dynstr" : ".gnu.libstr"));
   4505 	  if (s != NULL)
   4506 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4507 	  break;
   4508 
   4509 	case SHT_HASH:
   4510 	case SHT_GNU_HASH:
   4511 	case SHT_GNU_versym:
   4512 	  /* sh_link is the section header index of the symbol table
   4513 	     this hash table or version table is for.  */
   4514 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   4515 	  if (s != NULL)
   4516 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4517 	  break;
   4518 
   4519 	case SHT_GROUP:
   4520 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   4521 	}
   4522     }
   4523 
   4524   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   4525      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   4526      debug section name from .debug_* to .zdebug_* if needed.  */
   4527 
   4528   return true;
   4529 }
   4530 
   4531 static bool
   4532 sym_is_global (bfd *abfd, asymbol *sym)
   4533 {
   4534   /* If the backend has a special mapping, use it.  */
   4535   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4536   if (bed->elf_backend_sym_is_global)
   4537     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   4538 
   4539   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   4540 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   4541 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   4542 }
   4543 
   4544 /* Filter global symbols of ABFD to include in the import library.  All
   4545    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4546    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4547    the beginning of that array.
   4548 
   4549    Returns the number of symbols to keep.  */
   4550 
   4551 unsigned int
   4552 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4553 				asymbol **syms, long symcount)
   4554 {
   4555   long src_count, dst_count = 0;
   4556 
   4557   for (src_count = 0; src_count < symcount; src_count++)
   4558     {
   4559       asymbol *sym = syms[src_count];
   4560       char *name = (char *) bfd_asymbol_name (sym);
   4561       struct bfd_link_hash_entry *h;
   4562 
   4563       if (!sym_is_global (abfd, sym))
   4564 	continue;
   4565 
   4566       h = bfd_link_hash_lookup (info->hash, name, false, false, false);
   4567       if (h == NULL)
   4568 	continue;
   4569       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4570 	continue;
   4571       if (h->linker_def || h->ldscript_def)
   4572 	continue;
   4573 
   4574       syms[dst_count++] = sym;
   4575     }
   4576 
   4577   syms[dst_count] = NULL;
   4578 
   4579   return dst_count;
   4580 }
   4581 
   4582 /* Don't output section symbols for sections that are not going to be
   4583    output, that are duplicates or there is no BFD section.  */
   4584 
   4585 static bool
   4586 ignore_section_sym (bfd *abfd, asymbol *sym)
   4587 {
   4588   elf_symbol_type *type_ptr;
   4589 
   4590   if (sym == NULL)
   4591     return false;
   4592 
   4593   if ((sym->flags & BSF_SECTION_SYM) == 0)
   4594     return false;
   4595 
   4596   /* Ignore the section symbol if it isn't used.  */
   4597   if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
   4598     return true;
   4599 
   4600   if (sym->section == NULL)
   4601     return true;
   4602 
   4603   type_ptr = elf_symbol_from (sym);
   4604   return ((type_ptr != NULL
   4605 	   && type_ptr->internal_elf_sym.st_shndx != 0
   4606 	   && bfd_is_abs_section (sym->section))
   4607 	  || !(sym->section->owner == abfd
   4608 	       || (sym->section->output_section != NULL
   4609 		   && sym->section->output_section->owner == abfd
   4610 		   && sym->section->output_offset == 0)
   4611 	       || bfd_is_abs_section (sym->section)));
   4612 }
   4613 
   4614 /* Map symbol from it's internal number to the external number, moving
   4615    all local symbols to be at the head of the list.  */
   4616 
   4617 static bool
   4618 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4619 {
   4620   unsigned int symcount = bfd_get_symcount (abfd);
   4621   asymbol **syms = bfd_get_outsymbols (abfd);
   4622   asymbol **sect_syms;
   4623   unsigned int num_locals = 0;
   4624   unsigned int num_globals = 0;
   4625   unsigned int num_locals2 = 0;
   4626   unsigned int num_globals2 = 0;
   4627   unsigned int max_index = 0;
   4628   unsigned int idx;
   4629   asection *asect;
   4630   asymbol **new_syms;
   4631   size_t amt;
   4632 
   4633 #ifdef DEBUG
   4634   fprintf (stderr, "elf_map_symbols\n");
   4635   fflush (stderr);
   4636 #endif
   4637 
   4638   for (asect = abfd->sections; asect; asect = asect->next)
   4639     {
   4640       if (max_index < asect->index)
   4641 	max_index = asect->index;
   4642     }
   4643 
   4644   max_index++;
   4645   amt = max_index * sizeof (asymbol *);
   4646   sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
   4647   if (sect_syms == NULL)
   4648     return false;
   4649   elf_section_syms (abfd) = sect_syms;
   4650   elf_num_section_syms (abfd) = max_index;
   4651 
   4652   /* Init sect_syms entries for any section symbols we have already
   4653      decided to output.  */
   4654   for (idx = 0; idx < symcount; idx++)
   4655     {
   4656       asymbol *sym = syms[idx];
   4657 
   4658       if ((sym->flags & BSF_SECTION_SYM) != 0
   4659 	  && sym->value == 0
   4660 	  && !ignore_section_sym (abfd, sym)
   4661 	  && !bfd_is_abs_section (sym->section))
   4662 	{
   4663 	  asection *sec = sym->section;
   4664 
   4665 	  if (sec->owner != abfd)
   4666 	    sec = sec->output_section;
   4667 
   4668 	  sect_syms[sec->index] = syms[idx];
   4669 	}
   4670     }
   4671 
   4672   /* Classify all of the symbols.  */
   4673   for (idx = 0; idx < symcount; idx++)
   4674     {
   4675       if (sym_is_global (abfd, syms[idx]))
   4676 	num_globals++;
   4677       else if (!ignore_section_sym (abfd, syms[idx]))
   4678 	num_locals++;
   4679     }
   4680 
   4681   /* We will be adding a section symbol for each normal BFD section.  Most
   4682      sections will already have a section symbol in outsymbols, but
   4683      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4684      at least in that case.  */
   4685   for (asect = abfd->sections; asect; asect = asect->next)
   4686     {
   4687       asymbol *sym = asect->symbol;
   4688       /* Don't include ignored section symbols.  */
   4689       if (!ignore_section_sym (abfd, sym)
   4690 	  && sect_syms[asect->index] == NULL)
   4691 	{
   4692 	  if (!sym_is_global (abfd, asect->symbol))
   4693 	    num_locals++;
   4694 	  else
   4695 	    num_globals++;
   4696 	}
   4697     }
   4698 
   4699   /* Now sort the symbols so the local symbols are first.  */
   4700   amt = (num_locals + num_globals) * sizeof (asymbol *);
   4701   new_syms = (asymbol **) bfd_alloc (abfd, amt);
   4702   if (new_syms == NULL)
   4703     return false;
   4704 
   4705   for (idx = 0; idx < symcount; idx++)
   4706     {
   4707       asymbol *sym = syms[idx];
   4708       unsigned int i;
   4709 
   4710       if (sym_is_global (abfd, sym))
   4711 	i = num_locals + num_globals2++;
   4712       /* Don't include ignored section symbols.  */
   4713       else if (!ignore_section_sym (abfd, sym))
   4714 	i = num_locals2++;
   4715       else
   4716 	continue;
   4717       new_syms[i] = sym;
   4718       sym->udata.i = i + 1;
   4719     }
   4720   for (asect = abfd->sections; asect; asect = asect->next)
   4721     {
   4722       asymbol *sym = asect->symbol;
   4723       if (!ignore_section_sym (abfd, sym)
   4724 	  && sect_syms[asect->index] == NULL)
   4725 	{
   4726 	  unsigned int i;
   4727 
   4728 	  sect_syms[asect->index] = sym;
   4729 	  if (!sym_is_global (abfd, sym))
   4730 	    i = num_locals2++;
   4731 	  else
   4732 	    i = num_locals + num_globals2++;
   4733 	  new_syms[i] = sym;
   4734 	  sym->udata.i = i + 1;
   4735 	}
   4736     }
   4737 
   4738   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4739 
   4740   *pnum_locals = num_locals;
   4741   return true;
   4742 }
   4743 
   4744 /* Align to the maximum file alignment that could be required for any
   4745    ELF data structure.  */
   4746 
   4747 static inline file_ptr
   4748 align_file_position (file_ptr off, int align)
   4749 {
   4750   return (off + align - 1) & ~(align - 1);
   4751 }
   4752 
   4753 /* Assign a file position to a section, optionally aligning to the
   4754    required section alignment.  */
   4755 
   4756 file_ptr
   4757 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4758 					   file_ptr offset,
   4759 					   bool align)
   4760 {
   4761   if (align && i_shdrp->sh_addralign > 1)
   4762     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
   4763   i_shdrp->sh_offset = offset;
   4764   if (i_shdrp->bfd_section != NULL)
   4765     i_shdrp->bfd_section->filepos = offset;
   4766   if (i_shdrp->sh_type != SHT_NOBITS)
   4767     offset += i_shdrp->sh_size;
   4768   return offset;
   4769 }
   4770 
   4771 /* Compute the file positions we are going to put the sections at, and
   4772    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4773    is not NULL, this is being called by the ELF backend linker.  */
   4774 
   4775 bool
   4776 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4777 					 struct bfd_link_info *link_info)
   4778 {
   4779   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4780   struct fake_section_arg fsargs;
   4781   bool failed;
   4782   struct elf_strtab_hash *strtab = NULL;
   4783   Elf_Internal_Shdr *shstrtab_hdr;
   4784   bool need_symtab;
   4785 
   4786   if (abfd->output_has_begun)
   4787     return true;
   4788 
   4789   /* Do any elf backend specific processing first.  */
   4790   if (bed->elf_backend_begin_write_processing)
   4791     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4792 
   4793   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4794     return false;
   4795 
   4796   fsargs.failed = false;
   4797   fsargs.link_info = link_info;
   4798   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4799   if (fsargs.failed)
   4800     return false;
   4801 
   4802   if (!assign_section_numbers (abfd, link_info))
   4803     return false;
   4804 
   4805   /* The backend linker builds symbol table information itself.  */
   4806   need_symtab = (link_info == NULL
   4807 		 && (bfd_get_symcount (abfd) > 0
   4808 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4809 			 == HAS_RELOC)));
   4810   if (need_symtab)
   4811     {
   4812       /* Non-zero if doing a relocatable link.  */
   4813       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4814 
   4815       if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
   4816 	return false;
   4817     }
   4818 
   4819   failed = false;
   4820   if (link_info == NULL)
   4821     {
   4822       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4823       if (failed)
   4824 	goto err_free_strtab;
   4825     }
   4826 
   4827   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4828   /* sh_name was set in init_file_header.  */
   4829   shstrtab_hdr->sh_type = SHT_STRTAB;
   4830   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4831   shstrtab_hdr->sh_addr = 0;
   4832   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4833   shstrtab_hdr->sh_entsize = 0;
   4834   shstrtab_hdr->sh_link = 0;
   4835   shstrtab_hdr->sh_info = 0;
   4836   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4837   shstrtab_hdr->sh_addralign = 1;
   4838 
   4839   if (!assign_file_positions_except_relocs (abfd, link_info))
   4840     goto err_free_strtab;
   4841 
   4842   if (strtab != NULL)
   4843     {
   4844       file_ptr off;
   4845       Elf_Internal_Shdr *hdr;
   4846 
   4847       off = elf_next_file_pos (abfd);
   4848 
   4849       hdr = & elf_symtab_hdr (abfd);
   4850       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4851 
   4852       if (elf_symtab_shndx_list (abfd) != NULL)
   4853 	{
   4854 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4855 	  if (hdr->sh_size != 0)
   4856 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4857 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4858 	}
   4859 
   4860       hdr = &elf_tdata (abfd)->strtab_hdr;
   4861       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4862 
   4863       elf_next_file_pos (abfd) = off;
   4864 
   4865       /* Now that we know where the .strtab section goes, write it
   4866 	 out.  */
   4867       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4868 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4869 	goto err_free_strtab;
   4870       _bfd_elf_strtab_free (strtab);
   4871     }
   4872 
   4873   abfd->output_has_begun = true;
   4874   return true;
   4875 
   4876  err_free_strtab:
   4877   if (strtab != NULL)
   4878     _bfd_elf_strtab_free (strtab);
   4879   return false;
   4880 }
   4881 
   4882 /* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
   4883    function effectively returns whether --eh-frame-hdr is given on the
   4884    command line.  After size_dynamic_sections the result reflects
   4885    whether .eh_frame_hdr will actually be output (sizing isn't done
   4886    until ldemul_after_allocation).  */
   4887 
   4888 static asection *
   4889 elf_eh_frame_hdr (const struct bfd_link_info *info)
   4890 {
   4891   if (info != NULL && is_elf_hash_table (info->hash))
   4892     return elf_hash_table (info)->eh_info.hdr_sec;
   4893   return NULL;
   4894 }
   4895 
   4896 /* Make an initial estimate of the size of the program header.  If we
   4897    get the number wrong here, we'll redo section placement.  */
   4898 
   4899 static bfd_size_type
   4900 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4901 {
   4902   size_t segs;
   4903   asection *s;
   4904   const struct elf_backend_data *bed;
   4905 
   4906   /* Assume we will need exactly two PT_LOAD segments: one for text
   4907      and one for data.  */
   4908   segs = 2;
   4909 
   4910   s = bfd_get_section_by_name (abfd, ".interp");
   4911   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4912     {
   4913       /* If we have a loadable interpreter section, we need a
   4914 	 PT_INTERP segment.  In this case, assume we also need a
   4915 	 PT_PHDR segment, although that may not be true for all
   4916 	 targets.  */
   4917       segs += 2;
   4918     }
   4919 
   4920   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
   4921     {
   4922       /* We need a PT_DYNAMIC segment.  */
   4923       ++segs;
   4924     }
   4925 
   4926   if (info != NULL && info->relro)
   4927     {
   4928       /* We need a PT_GNU_RELRO segment.  */
   4929       ++segs;
   4930     }
   4931 
   4932   if (elf_eh_frame_hdr (info))
   4933     {
   4934       /* We need a PT_GNU_EH_FRAME segment.  */
   4935       ++segs;
   4936     }
   4937 
   4938   if (elf_stack_flags (abfd))
   4939     {
   4940       /* We need a PT_GNU_STACK segment.  */
   4941       ++segs;
   4942     }
   4943 
   4944   if (elf_sframe (abfd))
   4945     {
   4946       /* We need a PT_GNU_SFRAME segment.  */
   4947       ++segs;
   4948     }
   4949 
   4950   s = bfd_get_section_by_name (abfd,
   4951 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4952   if (s != NULL && s->size != 0)
   4953     {
   4954       /* We need a PT_GNU_PROPERTY segment.  */
   4955       ++segs;
   4956     }
   4957 
   4958   for (s = abfd->sections; s != NULL; s = s->next)
   4959     {
   4960       if ((s->flags & SEC_LOAD) != 0
   4961 	  && elf_section_type (s) == SHT_NOTE)
   4962 	{
   4963 	  unsigned int alignment_power;
   4964 	  /* We need a PT_NOTE segment.  */
   4965 	  ++segs;
   4966 	  /* Try to create just one PT_NOTE segment for all adjacent
   4967 	     loadable SHT_NOTE sections.  gABI requires that within a
   4968 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4969 	     each note should have the same alignment.  So we check
   4970 	     whether the sections are correctly aligned.  */
   4971 	  alignment_power = s->alignment_power;
   4972 	  while (s->next != NULL
   4973 		 && s->next->alignment_power == alignment_power
   4974 		 && (s->next->flags & SEC_LOAD) != 0
   4975 		 && elf_section_type (s->next) == SHT_NOTE)
   4976 	    s = s->next;
   4977 	}
   4978     }
   4979 
   4980   for (s = abfd->sections; s != NULL; s = s->next)
   4981     {
   4982       if (s->flags & SEC_THREAD_LOCAL)
   4983 	{
   4984 	  /* We need a PT_TLS segment.  */
   4985 	  ++segs;
   4986 	  break;
   4987 	}
   4988     }
   4989 
   4990   bed = get_elf_backend_data (abfd);
   4991 
   4992   if ((abfd->flags & D_PAGED) != 0
   4993       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4994     {
   4995       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4996       bfd_vma commonpagesize;
   4997       unsigned int page_align_power;
   4998 
   4999       if (info != NULL)
   5000 	commonpagesize = info->commonpagesize;
   5001       else
   5002 	commonpagesize = bed->commonpagesize;
   5003       page_align_power = bfd_log2 (commonpagesize);
   5004       for (s = abfd->sections; s != NULL; s = s->next)
   5005 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   5006 	  {
   5007 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   5008 	      {
   5009 		_bfd_error_handler
   5010 		  /* xgettext:c-format */
   5011 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   5012 		     "sh_info field: %d"),
   5013 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   5014 		continue;
   5015 	      }
   5016 	    /* Align mbind section to page size.  */
   5017 	    if (s->alignment_power < page_align_power)
   5018 	      s->alignment_power = page_align_power;
   5019 	    segs ++;
   5020 	  }
   5021     }
   5022 
   5023   /* Let the backend count up any program headers it might need.  */
   5024   if (bed->elf_backend_additional_program_headers)
   5025     {
   5026       int a;
   5027 
   5028       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   5029       if (a == -1)
   5030 	abort ();
   5031       segs += a;
   5032     }
   5033 
   5034   return segs * bed->s->sizeof_phdr;
   5035 }
   5036 
   5037 /* Find the segment that contains the output_section of section.  */
   5038 
   5039 Elf_Internal_Phdr *
   5040 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   5041 {
   5042   struct elf_segment_map *m;
   5043   Elf_Internal_Phdr *p;
   5044 
   5045   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   5046        m != NULL;
   5047        m = m->next, p++)
   5048     {
   5049       int i;
   5050 
   5051       for (i = m->count - 1; i >= 0; i--)
   5052 	if (m->sections[i] == section)
   5053 	  return p;
   5054     }
   5055 
   5056   return NULL;
   5057 }
   5058 
   5059 /* Create a mapping from a set of sections to a program segment.  */
   5060 
   5061 static struct elf_segment_map *
   5062 make_mapping (bfd *abfd,
   5063 	      asection **sections,
   5064 	      unsigned int from,
   5065 	      unsigned int to,
   5066 	      bool phdr)
   5067 {
   5068   struct elf_segment_map *m;
   5069   unsigned int i;
   5070   asection **hdrpp;
   5071   size_t amt;
   5072 
   5073   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5074   amt += (to - from) * sizeof (asection *);
   5075   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5076   if (m == NULL)
   5077     return NULL;
   5078   m->next = NULL;
   5079   m->p_type = PT_LOAD;
   5080   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   5081     m->sections[i - from] = *hdrpp;
   5082   m->count = to - from;
   5083 
   5084   if (from == 0 && phdr)
   5085     {
   5086       /* Include the headers in the first PT_LOAD segment.  */
   5087       m->includes_filehdr = 1;
   5088       m->includes_phdrs = 1;
   5089     }
   5090 
   5091   return m;
   5092 }
   5093 
   5094 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   5095    on failure.  */
   5096 
   5097 struct elf_segment_map *
   5098 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   5099 {
   5100   struct elf_segment_map *m;
   5101 
   5102   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   5103 					     sizeof (struct elf_segment_map));
   5104   if (m == NULL)
   5105     return NULL;
   5106   m->next = NULL;
   5107   m->p_type = PT_DYNAMIC;
   5108   m->count = 1;
   5109   m->sections[0] = dynsec;
   5110 
   5111   return m;
   5112 }
   5113 
   5114 /* Possibly add or remove segments from the segment map.  */
   5115 
   5116 static bool
   5117 elf_modify_segment_map (bfd *abfd,
   5118 			struct bfd_link_info *info,
   5119 			bool remove_empty_load)
   5120 {
   5121   struct elf_segment_map **m;
   5122   const struct elf_backend_data *bed;
   5123 
   5124   /* The placement algorithm assumes that non allocated sections are
   5125      not in PT_LOAD segments.  We ensure this here by removing such
   5126      sections from the segment map.  We also remove excluded
   5127      sections.  Finally, any PT_LOAD segment without sections is
   5128      removed.  */
   5129   m = &elf_seg_map (abfd);
   5130   while (*m)
   5131     {
   5132       unsigned int i, new_count;
   5133 
   5134       for (new_count = 0, i = 0; i < (*m)->count; i++)
   5135 	{
   5136 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   5137 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   5138 		  || (*m)->p_type != PT_LOAD))
   5139 	    {
   5140 	      (*m)->sections[new_count] = (*m)->sections[i];
   5141 	      new_count++;
   5142 	    }
   5143 	}
   5144       (*m)->count = new_count;
   5145 
   5146       if (remove_empty_load
   5147 	  && (*m)->p_type == PT_LOAD
   5148 	  && (*m)->count == 0
   5149 	  && !(*m)->includes_phdrs)
   5150 	*m = (*m)->next;
   5151       else
   5152 	m = &(*m)->next;
   5153     }
   5154 
   5155   bed = get_elf_backend_data (abfd);
   5156   if (bed->elf_backend_modify_segment_map != NULL)
   5157     {
   5158       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   5159 	return false;
   5160     }
   5161 
   5162   return true;
   5163 }
   5164 
   5165 #define IS_TBSS(s) \
   5166   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   5167 
   5168 /* Set up a mapping from BFD sections to program segments.  Update
   5169    NEED_LAYOUT if the section layout is changed.  */
   5170 
   5171 bool
   5172 _bfd_elf_map_sections_to_segments (bfd *abfd,
   5173 				   struct bfd_link_info *info,
   5174 				   bool *need_layout)
   5175 {
   5176   unsigned int count;
   5177   struct elf_segment_map *m;
   5178   asection **sections = NULL;
   5179   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5180   bool no_user_phdrs;
   5181 
   5182   no_user_phdrs = elf_seg_map (abfd) == NULL;
   5183 
   5184   if (info != NULL)
   5185     {
   5186       info->user_phdrs = !no_user_phdrs;
   5187 
   5188       /* Size the relative relocations if DT_RELR is enabled.  */
   5189       if (info->enable_dt_relr
   5190 	  && need_layout != NULL
   5191 	  && bed->size_relative_relocs
   5192 	  && !bed->size_relative_relocs (info, need_layout))
   5193 	info->callbacks->einfo
   5194 	  (_("%F%P: failed to size relative relocations\n"));
   5195     }
   5196 
   5197   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   5198     {
   5199       asection *s;
   5200       unsigned int i;
   5201       struct elf_segment_map *mfirst;
   5202       struct elf_segment_map **pm;
   5203       asection *last_hdr;
   5204       bfd_vma last_size;
   5205       unsigned int hdr_index;
   5206       bfd_vma maxpagesize;
   5207       asection **hdrpp;
   5208       bool phdr_in_segment;
   5209       bool writable;
   5210       bool executable;
   5211       unsigned int tls_count = 0;
   5212       asection *first_tls = NULL;
   5213       asection *first_mbind = NULL;
   5214       asection *dynsec, *eh_frame_hdr;
   5215       asection *sframe;
   5216       size_t amt;
   5217       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   5218       bfd_size_type phdr_size;  /* Octets/bytes.  */
   5219       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5220 
   5221       /* Select the allocated sections, and sort them.  */
   5222 
   5223       amt = bfd_count_sections (abfd) * sizeof (asection *);
   5224       sections = (asection **) bfd_malloc (amt);
   5225       if (sections == NULL)
   5226 	goto error_return;
   5227 
   5228       /* Calculate top address, avoiding undefined behaviour of shift
   5229 	 left operator when shift count is equal to size of type
   5230 	 being shifted.  */
   5231       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   5232       addr_mask = (addr_mask << 1) + 1;
   5233 
   5234       i = 0;
   5235       for (s = abfd->sections; s != NULL; s = s->next)
   5236 	{
   5237 	  if ((s->flags & SEC_ALLOC) != 0)
   5238 	    {
   5239 	      /* target_index is unused until bfd_elf_final_link
   5240 		 starts output of section symbols.  Use it to make
   5241 		 qsort stable.  */
   5242 	      s->target_index = i;
   5243 	      sections[i] = s;
   5244 	      ++i;
   5245 	      /* A wrapping section potentially clashes with header.  */
   5246 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   5247 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   5248 	    }
   5249 	}
   5250       BFD_ASSERT (i <= bfd_count_sections (abfd));
   5251       count = i;
   5252 
   5253       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   5254 
   5255       phdr_size = elf_program_header_size (abfd);
   5256       if (phdr_size == (bfd_size_type) -1)
   5257 	phdr_size = get_program_header_size (abfd, info);
   5258       phdr_size += bed->s->sizeof_ehdr;
   5259       /* phdr_size is compared to LMA values which are in bytes.  */
   5260       phdr_size /= opb;
   5261       if (info != NULL)
   5262 	maxpagesize = info->maxpagesize;
   5263       else
   5264 	maxpagesize = bed->maxpagesize;
   5265       if (maxpagesize == 0)
   5266 	maxpagesize = 1;
   5267       phdr_in_segment = info != NULL && info->load_phdrs;
   5268       if (count != 0
   5269 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   5270 	      >= (phdr_size & (maxpagesize - 1))))
   5271 	/* For compatibility with old scripts that may not be using
   5272 	   SIZEOF_HEADERS, add headers when it looks like space has
   5273 	   been left for them.  */
   5274 	phdr_in_segment = true;
   5275 
   5276       /* Build the mapping.  */
   5277       mfirst = NULL;
   5278       pm = &mfirst;
   5279 
   5280       /* If we have a .interp section, then create a PT_PHDR segment for
   5281 	 the program headers and a PT_INTERP segment for the .interp
   5282 	 section.  */
   5283       s = bfd_get_section_by_name (abfd, ".interp");
   5284       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   5285 	{
   5286 	  amt = sizeof (struct elf_segment_map);
   5287 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5288 	  if (m == NULL)
   5289 	    goto error_return;
   5290 	  m->next = NULL;
   5291 	  m->p_type = PT_PHDR;
   5292 	  m->p_flags = PF_R;
   5293 	  m->p_flags_valid = 1;
   5294 	  m->includes_phdrs = 1;
   5295 	  phdr_in_segment = true;
   5296 	  *pm = m;
   5297 	  pm = &m->next;
   5298 
   5299 	  amt = sizeof (struct elf_segment_map);
   5300 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5301 	  if (m == NULL)
   5302 	    goto error_return;
   5303 	  m->next = NULL;
   5304 	  m->p_type = PT_INTERP;
   5305 	  m->count = 1;
   5306 	  m->sections[0] = s;
   5307 
   5308 	  *pm = m;
   5309 	  pm = &m->next;
   5310 	}
   5311 
   5312       /* Look through the sections.  We put sections in the same program
   5313 	 segment when the start of the second section can be placed within
   5314 	 a few bytes of the end of the first section.  */
   5315       last_hdr = NULL;
   5316       last_size = 0;
   5317       hdr_index = 0;
   5318       writable = false;
   5319       executable = false;
   5320       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   5321       if (dynsec != NULL
   5322 	  && (dynsec->flags & SEC_LOAD) == 0)
   5323 	dynsec = NULL;
   5324 
   5325       if ((abfd->flags & D_PAGED) == 0)
   5326 	phdr_in_segment = false;
   5327 
   5328       /* Deal with -Ttext or something similar such that the first section
   5329 	 is not adjacent to the program headers.  This is an
   5330 	 approximation, since at this point we don't know exactly how many
   5331 	 program headers we will need.  */
   5332       if (phdr_in_segment && count > 0)
   5333 	{
   5334 	  bfd_vma phdr_lma;  /* Bytes.  */
   5335 	  bool separate_phdr = false;
   5336 
   5337 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   5338 	  if (info != NULL
   5339 	      && info->separate_code
   5340 	      && (sections[0]->flags & SEC_CODE) != 0)
   5341 	    {
   5342 	      /* If data sections should be separate from code and
   5343 		 thus not executable, and the first section is
   5344 		 executable then put the file and program headers in
   5345 		 their own PT_LOAD.  */
   5346 	      separate_phdr = true;
   5347 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   5348 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   5349 		{
   5350 		  /* The file and program headers are currently on the
   5351 		     same page as the first section.  Put them on the
   5352 		     previous page if we can.  */
   5353 		  if (phdr_lma >= maxpagesize)
   5354 		    phdr_lma -= maxpagesize;
   5355 		  else
   5356 		    separate_phdr = false;
   5357 		}
   5358 	    }
   5359 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   5360 	      || (sections[0]->lma & addr_mask) < phdr_size)
   5361 	    /* If file and program headers would be placed at the end
   5362 	       of memory then it's probably better to omit them.  */
   5363 	    phdr_in_segment = false;
   5364 	  else if (phdr_lma < wrap_to)
   5365 	    /* If a section wraps around to where we'll be placing
   5366 	       file and program headers, then the headers will be
   5367 	       overwritten.  */
   5368 	    phdr_in_segment = false;
   5369 	  else if (separate_phdr)
   5370 	    {
   5371 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   5372 	      if (m == NULL)
   5373 		goto error_return;
   5374 	      m->p_paddr = phdr_lma * opb;
   5375 	      m->p_vaddr_offset
   5376 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   5377 	      m->p_paddr_valid = 1;
   5378 	      *pm = m;
   5379 	      pm = &m->next;
   5380 	      phdr_in_segment = false;
   5381 	    }
   5382 	}
   5383 
   5384       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   5385 	{
   5386 	  asection *hdr;
   5387 	  bool new_segment;
   5388 
   5389 	  hdr = *hdrpp;
   5390 
   5391 	  /* See if this section and the last one will fit in the same
   5392 	     segment.  */
   5393 
   5394 	  if (last_hdr == NULL)
   5395 	    {
   5396 	      /* If we don't have a segment yet, then we don't need a new
   5397 		 one (we build the last one after this loop).  */
   5398 	      new_segment = false;
   5399 	    }
   5400 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   5401 	    {
   5402 	      /* If this section has a different relation between the
   5403 		 virtual address and the load address, then we need a new
   5404 		 segment.  */
   5405 	      new_segment = true;
   5406 	    }
   5407 	  else if (hdr->lma < last_hdr->lma + last_size
   5408 		   || last_hdr->lma + last_size < last_hdr->lma)
   5409 	    {
   5410 	      /* If this section has a load address that makes it overlap
   5411 		 the previous section, then we need a new segment.  */
   5412 	      new_segment = true;
   5413 	    }
   5414 	  else if ((abfd->flags & D_PAGED) != 0
   5415 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   5416 		       == (hdr->lma & -maxpagesize)))
   5417 	    {
   5418 	      /* If we are demand paged then we can't map two disk
   5419 		 pages onto the same memory page.  */
   5420 	      new_segment = false;
   5421 	    }
   5422 	  /* In the next test we have to be careful when last_hdr->lma is close
   5423 	     to the end of the address space.  If the aligned address wraps
   5424 	     around to the start of the address space, then there are no more
   5425 	     pages left in memory and it is OK to assume that the current
   5426 	     section can be included in the current segment.  */
   5427 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5428 		    + maxpagesize > last_hdr->lma)
   5429 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5430 		       + maxpagesize <= hdr->lma))
   5431 	    {
   5432 	      /* If putting this section in this segment would force us to
   5433 		 skip a page in the segment, then we need a new segment.  */
   5434 	      new_segment = true;
   5435 	    }
   5436 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   5437 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   5438 	    {
   5439 	      /* We don't want to put a loaded section after a
   5440 		 nonloaded (ie. bss style) section in the same segment
   5441 		 as that will force the non-loaded section to be loaded.
   5442 		 Consider .tbss sections as loaded for this purpose.  */
   5443 	      new_segment = true;
   5444 	    }
   5445 	  else if ((abfd->flags & D_PAGED) == 0)
   5446 	    {
   5447 	      /* If the file is not demand paged, which means that we
   5448 		 don't require the sections to be correctly aligned in the
   5449 		 file, then there is no other reason for a new segment.  */
   5450 	      new_segment = false;
   5451 	    }
   5452 	  else if (info != NULL
   5453 		   && info->separate_code
   5454 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   5455 	    {
   5456 	      new_segment = true;
   5457 	    }
   5458 	  else if (! writable
   5459 		   && (hdr->flags & SEC_READONLY) == 0)
   5460 	    {
   5461 	      /* We don't want to put a writable section in a read only
   5462 		 segment.  */
   5463 	      new_segment = true;
   5464 	    }
   5465 	  else
   5466 	    {
   5467 	      /* Otherwise, we can use the same segment.  */
   5468 	      new_segment = false;
   5469 	    }
   5470 
   5471 	  /* Allow interested parties a chance to override our decision.  */
   5472 	  if (last_hdr != NULL
   5473 	      && info != NULL
   5474 	      && info->callbacks->override_segment_assignment != NULL)
   5475 	    new_segment
   5476 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   5477 							      last_hdr,
   5478 							      new_segment);
   5479 
   5480 	  if (! new_segment)
   5481 	    {
   5482 	      if ((hdr->flags & SEC_READONLY) == 0)
   5483 		writable = true;
   5484 	      if ((hdr->flags & SEC_CODE) != 0)
   5485 		executable = true;
   5486 	      last_hdr = hdr;
   5487 	      /* .tbss sections effectively have zero size.  */
   5488 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5489 	      continue;
   5490 	    }
   5491 
   5492 	  /* We need a new program segment.  We must create a new program
   5493 	     header holding all the sections from hdr_index until hdr.  */
   5494 
   5495 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5496 	  if (m == NULL)
   5497 	    goto error_return;
   5498 
   5499 	  *pm = m;
   5500 	  pm = &m->next;
   5501 
   5502 	  if ((hdr->flags & SEC_READONLY) == 0)
   5503 	    writable = true;
   5504 	  else
   5505 	    writable = false;
   5506 
   5507 	  if ((hdr->flags & SEC_CODE) == 0)
   5508 	    executable = false;
   5509 	  else
   5510 	    executable = true;
   5511 
   5512 	  last_hdr = hdr;
   5513 	  /* .tbss sections effectively have zero size.  */
   5514 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5515 	  hdr_index = i;
   5516 	  phdr_in_segment = false;
   5517 	}
   5518 
   5519       /* Create a final PT_LOAD program segment, but not if it's just
   5520 	 for .tbss.  */
   5521       if (last_hdr != NULL
   5522 	  && (i - hdr_index != 1
   5523 	      || !IS_TBSS (last_hdr)))
   5524 	{
   5525 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5526 	  if (m == NULL)
   5527 	    goto error_return;
   5528 
   5529 	  *pm = m;
   5530 	  pm = &m->next;
   5531 	}
   5532 
   5533       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   5534       if (dynsec != NULL)
   5535 	{
   5536 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   5537 	  if (m == NULL)
   5538 	    goto error_return;
   5539 	  *pm = m;
   5540 	  pm = &m->next;
   5541 	}
   5542 
   5543       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5544 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5545 	 because if we link together nonloadable .note sections and
   5546 	 loadable .note sections, we will generate two .note sections
   5547 	 in the output file.  */
   5548       for (s = abfd->sections; s != NULL; s = s->next)
   5549 	{
   5550 	  if ((s->flags & SEC_LOAD) != 0
   5551 	      && elf_section_type (s) == SHT_NOTE)
   5552 	    {
   5553 	      asection *s2;
   5554 	      unsigned int alignment_power = s->alignment_power;
   5555 
   5556 	      count = 1;
   5557 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5558 		{
   5559 		  if (s2->next->alignment_power == alignment_power
   5560 		      && (s2->next->flags & SEC_LOAD) != 0
   5561 		      && elf_section_type (s2->next) == SHT_NOTE
   5562 		      && align_power (s2->lma + s2->size / opb,
   5563 				      alignment_power)
   5564 		      == s2->next->lma)
   5565 		    count++;
   5566 		  else
   5567 		    break;
   5568 		}
   5569 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5570 	      amt += count * sizeof (asection *);
   5571 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5572 	      if (m == NULL)
   5573 		goto error_return;
   5574 	      m->next = NULL;
   5575 	      m->p_type = PT_NOTE;
   5576 	      m->count = count;
   5577 	      while (count > 1)
   5578 		{
   5579 		  m->sections[m->count - count--] = s;
   5580 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5581 		  s = s->next;
   5582 		}
   5583 	      m->sections[m->count - 1] = s;
   5584 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5585 	      *pm = m;
   5586 	      pm = &m->next;
   5587 	    }
   5588 	  if (s->flags & SEC_THREAD_LOCAL)
   5589 	    {
   5590 	      if (! tls_count)
   5591 		first_tls = s;
   5592 	      tls_count++;
   5593 	    }
   5594 	  if (first_mbind == NULL
   5595 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5596 	    first_mbind = s;
   5597 	}
   5598 
   5599       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5600       if (tls_count > 0)
   5601 	{
   5602 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5603 	  amt += tls_count * sizeof (asection *);
   5604 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5605 	  if (m == NULL)
   5606 	    goto error_return;
   5607 	  m->next = NULL;
   5608 	  m->p_type = PT_TLS;
   5609 	  m->count = tls_count;
   5610 	  /* Mandated PF_R.  */
   5611 	  m->p_flags = PF_R;
   5612 	  m->p_flags_valid = 1;
   5613 	  s = first_tls;
   5614 	  for (i = 0; i < tls_count; ++i)
   5615 	    {
   5616 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5617 		{
   5618 		  _bfd_error_handler
   5619 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5620 		  s = first_tls;
   5621 		  i = 0;
   5622 		  while (i < tls_count)
   5623 		    {
   5624 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5625 			{
   5626 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5627 			  i++;
   5628 			}
   5629 		      else
   5630 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5631 		      s = s->next;
   5632 		    }
   5633 		  bfd_set_error (bfd_error_bad_value);
   5634 		  goto error_return;
   5635 		}
   5636 	      m->sections[i] = s;
   5637 	      s = s->next;
   5638 	    }
   5639 
   5640 	  *pm = m;
   5641 	  pm = &m->next;
   5642 	}
   5643 
   5644       if (first_mbind
   5645 	  && (abfd->flags & D_PAGED) != 0
   5646 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5647 	for (s = first_mbind; s != NULL; s = s->next)
   5648 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5649 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5650 	    {
   5651 	      /* Mandated PF_R.  */
   5652 	      unsigned long p_flags = PF_R;
   5653 	      if ((s->flags & SEC_READONLY) == 0)
   5654 		p_flags |= PF_W;
   5655 	      if ((s->flags & SEC_CODE) != 0)
   5656 		p_flags |= PF_X;
   5657 
   5658 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5659 	      m = bfd_zalloc (abfd, amt);
   5660 	      if (m == NULL)
   5661 		goto error_return;
   5662 	      m->next = NULL;
   5663 	      m->p_type = (PT_GNU_MBIND_LO
   5664 			   + elf_section_data (s)->this_hdr.sh_info);
   5665 	      m->count = 1;
   5666 	      m->p_flags_valid = 1;
   5667 	      m->sections[0] = s;
   5668 	      m->p_flags = p_flags;
   5669 
   5670 	      *pm = m;
   5671 	      pm = &m->next;
   5672 	    }
   5673 
   5674       s = bfd_get_section_by_name (abfd,
   5675 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5676       if (s != NULL && s->size != 0)
   5677 	{
   5678 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5679 	  m = bfd_zalloc (abfd, amt);
   5680 	  if (m == NULL)
   5681 	    goto error_return;
   5682 	  m->next = NULL;
   5683 	  m->p_type = PT_GNU_PROPERTY;
   5684 	  m->count = 1;
   5685 	  m->p_flags_valid = 1;
   5686 	  m->sections[0] = s;
   5687 	  m->p_flags = PF_R;
   5688 	  *pm = m;
   5689 	  pm = &m->next;
   5690 	}
   5691 
   5692       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5693 	 segment.  */
   5694       eh_frame_hdr = elf_eh_frame_hdr (info);
   5695       if (eh_frame_hdr != NULL
   5696 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5697 	{
   5698 	  amt = sizeof (struct elf_segment_map);
   5699 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5700 	  if (m == NULL)
   5701 	    goto error_return;
   5702 	  m->next = NULL;
   5703 	  m->p_type = PT_GNU_EH_FRAME;
   5704 	  m->count = 1;
   5705 	  m->sections[0] = eh_frame_hdr->output_section;
   5706 
   5707 	  *pm = m;
   5708 	  pm = &m->next;
   5709 	}
   5710 
   5711       /* If there is a .sframe section, throw in a PT_GNU_SFRAME
   5712 	 segment.  */
   5713       sframe = elf_sframe (abfd);
   5714       if (sframe != NULL
   5715 	  && (sframe->output_section->flags & SEC_LOAD) != 0
   5716 	  && sframe->size != 0)
   5717 	{
   5718 	  amt = sizeof (struct elf_segment_map);
   5719 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5720 	  if (m == NULL)
   5721 	    goto error_return;
   5722 	  m->next = NULL;
   5723 	  m->p_type = PT_GNU_SFRAME;
   5724 	  m->count = 1;
   5725 	  m->sections[0] = sframe->output_section;
   5726 
   5727 	  *pm = m;
   5728 	  pm = &m->next;
   5729 	}
   5730 
   5731       if (elf_stack_flags (abfd))
   5732 	{
   5733 	  amt = sizeof (struct elf_segment_map);
   5734 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5735 	  if (m == NULL)
   5736 	    goto error_return;
   5737 	  m->next = NULL;
   5738 	  m->p_type = PT_GNU_STACK;
   5739 	  m->p_flags = elf_stack_flags (abfd);
   5740 	  m->p_align = bed->stack_align;
   5741 	  m->p_flags_valid = 1;
   5742 	  m->p_align_valid = m->p_align != 0;
   5743 	  if (info->stacksize > 0)
   5744 	    {
   5745 	      m->p_size = info->stacksize;
   5746 	      m->p_size_valid = 1;
   5747 	    }
   5748 
   5749 	  *pm = m;
   5750 	  pm = &m->next;
   5751 	}
   5752 
   5753       if (info != NULL && info->relro)
   5754 	{
   5755 	  for (m = mfirst; m != NULL; m = m->next)
   5756 	    {
   5757 	      if (m->p_type == PT_LOAD
   5758 		  && m->count != 0
   5759 		  && m->sections[0]->vma >= info->relro_start
   5760 		  && m->sections[0]->vma < info->relro_end)
   5761 		{
   5762 		  i = m->count;
   5763 		  while (--i != (unsigned) -1)
   5764 		    {
   5765 		      if (m->sections[i]->size > 0
   5766 			  && (m->sections[i]->flags & SEC_LOAD) != 0
   5767 			  && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
   5768 			break;
   5769 		    }
   5770 
   5771 		  if (i != (unsigned) -1)
   5772 		    break;
   5773 		}
   5774 	    }
   5775 
   5776 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5777 	  if (m != NULL)
   5778 	    {
   5779 	      amt = sizeof (struct elf_segment_map);
   5780 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5781 	      if (m == NULL)
   5782 		goto error_return;
   5783 	      m->next = NULL;
   5784 	      m->p_type = PT_GNU_RELRO;
   5785 	      *pm = m;
   5786 	      pm = &m->next;
   5787 	    }
   5788 	}
   5789 
   5790       free (sections);
   5791       elf_seg_map (abfd) = mfirst;
   5792     }
   5793 
   5794   if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
   5795     return false;
   5796 
   5797   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5798     ++count;
   5799   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5800 
   5801   return true;
   5802 
   5803  error_return:
   5804   free (sections);
   5805   return false;
   5806 }
   5807 
   5808 /* Sort sections by address.  */
   5809 
   5810 static int
   5811 elf_sort_sections (const void *arg1, const void *arg2)
   5812 {
   5813   const asection *sec1 = *(const asection **) arg1;
   5814   const asection *sec2 = *(const asection **) arg2;
   5815   bfd_size_type size1, size2;
   5816 
   5817   /* Sort by LMA first, since this is the address used to
   5818      place the section into a segment.  */
   5819   if (sec1->lma < sec2->lma)
   5820     return -1;
   5821   else if (sec1->lma > sec2->lma)
   5822     return 1;
   5823 
   5824   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5825      the same, and this will do nothing.  */
   5826   if (sec1->vma < sec2->vma)
   5827     return -1;
   5828   else if (sec1->vma > sec2->vma)
   5829     return 1;
   5830 
   5831   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5832 
   5833 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5834 		  && (x)->size != 0)
   5835 
   5836   if (TOEND (sec1))
   5837     {
   5838       if (!TOEND (sec2))
   5839 	return 1;
   5840     }
   5841   else if (TOEND (sec2))
   5842     return -1;
   5843 
   5844 #undef TOEND
   5845 
   5846   /* Sort by size, to put zero sized sections
   5847      before others at the same address.  */
   5848 
   5849   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5850   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5851 
   5852   if (size1 < size2)
   5853     return -1;
   5854   if (size1 > size2)
   5855     return 1;
   5856 
   5857   return sec1->target_index - sec2->target_index;
   5858 }
   5859 
   5860 /* This qsort comparison functions sorts PT_LOAD segments first and
   5861    by p_paddr, for assign_file_positions_for_load_sections.  */
   5862 
   5863 static int
   5864 elf_sort_segments (const void *arg1, const void *arg2)
   5865 {
   5866   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5867   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5868 
   5869   if (m1->p_type != m2->p_type)
   5870     {
   5871       if (m1->p_type == PT_NULL)
   5872 	return 1;
   5873       if (m2->p_type == PT_NULL)
   5874 	return -1;
   5875       return m1->p_type < m2->p_type ? -1 : 1;
   5876     }
   5877   if (m1->includes_filehdr != m2->includes_filehdr)
   5878     return m1->includes_filehdr ? -1 : 1;
   5879   if (m1->no_sort_lma != m2->no_sort_lma)
   5880     return m1->no_sort_lma ? -1 : 1;
   5881   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5882     {
   5883       bfd_vma lma1, lma2;  /* Octets.  */
   5884       lma1 = 0;
   5885       if (m1->p_paddr_valid)
   5886 	lma1 = m1->p_paddr;
   5887       else if (m1->count != 0)
   5888 	{
   5889 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5890 						  m1->sections[0]);
   5891 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5892 	}
   5893       lma2 = 0;
   5894       if (m2->p_paddr_valid)
   5895 	lma2 = m2->p_paddr;
   5896       else if (m2->count != 0)
   5897 	{
   5898 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5899 						  m2->sections[0]);
   5900 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5901 	}
   5902       if (lma1 != lma2)
   5903 	return lma1 < lma2 ? -1 : 1;
   5904     }
   5905   if (m1->idx != m2->idx)
   5906     return m1->idx < m2->idx ? -1 : 1;
   5907   return 0;
   5908 }
   5909 
   5910 /* Ian Lance Taylor writes:
   5911 
   5912    We shouldn't be using % with a negative signed number.  That's just
   5913    not good.  We have to make sure either that the number is not
   5914    negative, or that the number has an unsigned type.  When the types
   5915    are all the same size they wind up as unsigned.  When file_ptr is a
   5916    larger signed type, the arithmetic winds up as signed long long,
   5917    which is wrong.
   5918 
   5919    What we're trying to say here is something like ``increase OFF by
   5920    the least amount that will cause it to be equal to the VMA modulo
   5921    the page size.''  */
   5922 /* In other words, something like:
   5923 
   5924    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5925    off_offset = off % bed->maxpagesize;
   5926    if (vma_offset < off_offset)
   5927      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5928    else
   5929      adjustment = vma_offset - off_offset;
   5930 
   5931    which can be collapsed into the expression below.  */
   5932 
   5933 static file_ptr
   5934 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5935 {
   5936   /* PR binutils/16199: Handle an alignment of zero.  */
   5937   if (maxpagesize == 0)
   5938     maxpagesize = 1;
   5939   return ((vma - off) % maxpagesize);
   5940 }
   5941 
   5942 static void
   5943 print_segment_map (const struct elf_segment_map *m)
   5944 {
   5945   unsigned int j;
   5946   const char *pt = get_segment_type (m->p_type);
   5947   char buf[32];
   5948 
   5949   if (pt == NULL)
   5950     {
   5951       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5952 	sprintf (buf, "LOPROC+%7.7x",
   5953 		 (unsigned int) (m->p_type - PT_LOPROC));
   5954       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5955 	sprintf (buf, "LOOS+%7.7x",
   5956 		 (unsigned int) (m->p_type - PT_LOOS));
   5957       else
   5958 	snprintf (buf, sizeof (buf), "%8.8x",
   5959 		  (unsigned int) m->p_type);
   5960       pt = buf;
   5961     }
   5962   fflush (stdout);
   5963   fprintf (stderr, "%s:", pt);
   5964   for (j = 0; j < m->count; j++)
   5965     fprintf (stderr, " %s", m->sections [j]->name);
   5966   putc ('\n',stderr);
   5967   fflush (stderr);
   5968 }
   5969 
   5970 /* Assign file positions to the sections based on the mapping from
   5971    sections to segments.  This function also sets up some fields in
   5972    the file header.  */
   5973 
   5974 static bool
   5975 assign_file_positions_for_load_sections (bfd *abfd,
   5976 					 struct bfd_link_info *link_info)
   5977 {
   5978   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5979   struct elf_segment_map *m;
   5980   struct elf_segment_map *phdr_load_seg;
   5981   Elf_Internal_Phdr *phdrs;
   5982   Elf_Internal_Phdr *p;
   5983   file_ptr off;  /* Octets.  */
   5984   bfd_size_type maxpagesize;
   5985   unsigned int alloc, actual;
   5986   unsigned int i, j;
   5987   struct elf_segment_map **sorted_seg_map;
   5988   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5989 
   5990   if (link_info == NULL
   5991       && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5992     return false;
   5993 
   5994   alloc = 0;
   5995   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5996     m->idx = alloc++;
   5997 
   5998   if (alloc)
   5999     {
   6000       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   6001       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   6002     }
   6003   else
   6004     {
   6005       /* PR binutils/12467.  */
   6006       elf_elfheader (abfd)->e_phoff = 0;
   6007       elf_elfheader (abfd)->e_phentsize = 0;
   6008     }
   6009 
   6010   elf_elfheader (abfd)->e_phnum = alloc;
   6011 
   6012   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   6013     {
   6014       actual = alloc;
   6015       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   6016     }
   6017   else
   6018     {
   6019       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   6020       BFD_ASSERT (elf_program_header_size (abfd)
   6021 		  == actual * bed->s->sizeof_phdr);
   6022       BFD_ASSERT (actual >= alloc);
   6023     }
   6024 
   6025   if (alloc == 0)
   6026     {
   6027       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   6028       return true;
   6029     }
   6030 
   6031   /* We're writing the size in elf_program_header_size (abfd),
   6032      see assign_file_positions_except_relocs, so make sure we have
   6033      that amount allocated, with trailing space cleared.
   6034      The variable alloc contains the computed need, while
   6035      elf_program_header_size (abfd) contains the size used for the
   6036      layout.
   6037      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   6038      where the layout is forced to according to a larger size in the
   6039      last iterations for the testcase ld-elf/header.  */
   6040   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   6041 			     + alloc * sizeof (*sorted_seg_map)));
   6042   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   6043   elf_tdata (abfd)->phdr = phdrs;
   6044   if (phdrs == NULL)
   6045     return false;
   6046 
   6047   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   6048     {
   6049       sorted_seg_map[j] = m;
   6050       /* If elf_segment_map is not from map_sections_to_segments, the
   6051 	 sections may not be correctly ordered.  NOTE: sorting should
   6052 	 not be done to the PT_NOTE section of a corefile, which may
   6053 	 contain several pseudo-sections artificially created by bfd.
   6054 	 Sorting these pseudo-sections breaks things badly.  */
   6055       if (m->count > 1
   6056 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   6057 	       && m->p_type == PT_NOTE))
   6058 	{
   6059 	  for (i = 0; i < m->count; i++)
   6060 	    m->sections[i]->target_index = i;
   6061 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   6062 		 elf_sort_sections);
   6063 	}
   6064     }
   6065   if (alloc > 1)
   6066     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   6067 	   elf_sort_segments);
   6068 
   6069   maxpagesize = 1;
   6070   if ((abfd->flags & D_PAGED) != 0)
   6071     {
   6072       if (link_info != NULL)
   6073 	maxpagesize = link_info->maxpagesize;
   6074       else
   6075 	maxpagesize = bed->maxpagesize;
   6076     }
   6077 
   6078   /* Sections must map to file offsets past the ELF file header.  */
   6079   off = bed->s->sizeof_ehdr;
   6080   /* And if one of the PT_LOAD headers doesn't include the program
   6081      headers then we'll be mapping program headers in the usual
   6082      position after the ELF file header.  */
   6083   phdr_load_seg = NULL;
   6084   for (j = 0; j < alloc; j++)
   6085     {
   6086       m = sorted_seg_map[j];
   6087       if (m->p_type != PT_LOAD)
   6088 	break;
   6089       if (m->includes_phdrs)
   6090 	{
   6091 	  phdr_load_seg = m;
   6092 	  break;
   6093 	}
   6094     }
   6095   if (phdr_load_seg == NULL)
   6096     off += actual * bed->s->sizeof_phdr;
   6097 
   6098   for (j = 0; j < alloc; j++)
   6099     {
   6100       asection **secpp;
   6101       bfd_vma off_adjust;  /* Octets.  */
   6102       bool no_contents;
   6103       bfd_size_type p_align;
   6104       bool p_align_p;
   6105 
   6106       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   6107 	 number of sections with contents contributing to both p_filesz
   6108 	 and p_memsz, followed by a number of sections with no contents
   6109 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   6110 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   6111       m = sorted_seg_map[j];
   6112       p = phdrs + m->idx;
   6113       p->p_type = m->p_type;
   6114       p->p_flags = m->p_flags;
   6115       p_align = bed->p_align;
   6116       p_align_p = false;
   6117 
   6118       if (m->count == 0)
   6119 	p->p_vaddr = m->p_vaddr_offset * opb;
   6120       else
   6121 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   6122 
   6123       if (m->p_paddr_valid)
   6124 	p->p_paddr = m->p_paddr;
   6125       else if (m->count == 0)
   6126 	p->p_paddr = 0;
   6127       else
   6128 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   6129 
   6130       if (p->p_type == PT_LOAD
   6131 	  && (abfd->flags & D_PAGED) != 0)
   6132 	{
   6133 	  /* p_align in demand paged PT_LOAD segments effectively stores
   6134 	     the maximum page size.  When copying an executable with
   6135 	     objcopy, we set m->p_align from the input file.  Use this
   6136 	     value for maxpagesize rather than bed->maxpagesize, which
   6137 	     may be different.  Note that we use maxpagesize for PT_TLS
   6138 	     segment alignment later in this function, so we are relying
   6139 	     on at least one PT_LOAD segment appearing before a PT_TLS
   6140 	     segment.  */
   6141 	  if (m->p_align_valid)
   6142 	    maxpagesize = m->p_align;
   6143 	  else if (p_align != 0
   6144 		   && (link_info == NULL
   6145 		       || !link_info->maxpagesize_is_set))
   6146 	    /* Set p_align to the default p_align value while laying
   6147 	       out segments aligning to the maximum page size or the
   6148 	       largest section alignment.  The run-time loader can
   6149 	       align segments to the default p_align value or the
   6150 	       maximum page size, depending on system page size.  */
   6151 	    p_align_p = true;
   6152 
   6153 	  p->p_align = maxpagesize;
   6154 	}
   6155       else if (m->p_align_valid)
   6156 	p->p_align = m->p_align;
   6157       else if (m->count == 0)
   6158 	p->p_align = 1 << bed->s->log_file_align;
   6159 
   6160       if (m == phdr_load_seg)
   6161 	{
   6162 	  if (!m->includes_filehdr)
   6163 	    p->p_offset = off;
   6164 	  off += actual * bed->s->sizeof_phdr;
   6165 	}
   6166 
   6167       no_contents = false;
   6168       off_adjust = 0;
   6169       if (p->p_type == PT_LOAD
   6170 	  && m->count > 0)
   6171 	{
   6172 	  bfd_size_type align;  /* Bytes.  */
   6173 	  unsigned int align_power = 0;
   6174 
   6175 	  if (m->p_align_valid)
   6176 	    align = p->p_align;
   6177 	  else
   6178 	    {
   6179 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6180 		{
   6181 		  unsigned int secalign;
   6182 
   6183 		  secalign = bfd_section_alignment (*secpp);
   6184 		  if (secalign > align_power)
   6185 		    align_power = secalign;
   6186 		}
   6187 	      align = (bfd_size_type) 1 << align_power;
   6188 	      if (align < maxpagesize)
   6189 		{
   6190 		  /* If a section requires alignment higher than the
   6191 		     default p_align value, don't set p_align to the
   6192 		     default p_align value.  */
   6193 		  if (align > p_align)
   6194 		    p_align_p = false;
   6195 		  align = maxpagesize;
   6196 		}
   6197 	      else
   6198 		{
   6199 		  /* If a section requires alignment higher than the
   6200 		     maximum page size, set p_align to the section
   6201 		     alignment.  */
   6202 		  p_align_p = true;
   6203 		  p_align = align;
   6204 		}
   6205 	    }
   6206 
   6207 	  for (i = 0; i < m->count; i++)
   6208 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   6209 	      /* If we aren't making room for this section, then
   6210 		 it must be SHT_NOBITS regardless of what we've
   6211 		 set via struct bfd_elf_special_section.  */
   6212 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   6213 
   6214 	  /* Find out whether this segment contains any loadable
   6215 	     sections.  */
   6216 	  no_contents = true;
   6217 	  for (i = 0; i < m->count; i++)
   6218 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   6219 	      {
   6220 		no_contents = false;
   6221 		break;
   6222 	      }
   6223 
   6224 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   6225 
   6226 	  /* Broken hardware and/or kernel require that files do not
   6227 	     map the same page with different permissions on some hppa
   6228 	     processors.  */
   6229 	  if (j != 0
   6230 	      && (abfd->flags & D_PAGED) != 0
   6231 	      && bed->no_page_alias
   6232 	      && (off & (maxpagesize - 1)) != 0
   6233 	      && ((off & -maxpagesize)
   6234 		  == ((off + off_adjust) & -maxpagesize)))
   6235 	    off_adjust += maxpagesize;
   6236 	  off += off_adjust;
   6237 	  if (no_contents)
   6238 	    {
   6239 	      /* We shouldn't need to align the segment on disk since
   6240 		 the segment doesn't need file space, but the gABI
   6241 		 arguably requires the alignment and glibc ld.so
   6242 		 checks it.  So to comply with the alignment
   6243 		 requirement but not waste file space, we adjust
   6244 		 p_offset for just this segment.  (OFF_ADJUST is
   6245 		 subtracted from OFF later.)  This may put p_offset
   6246 		 past the end of file, but that shouldn't matter.  */
   6247 	    }
   6248 	  else
   6249 	    off_adjust = 0;
   6250 	}
   6251       /* Make sure the .dynamic section is the first section in the
   6252 	 PT_DYNAMIC segment.  */
   6253       else if (p->p_type == PT_DYNAMIC
   6254 	       && m->count > 1
   6255 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   6256 	{
   6257 	  _bfd_error_handler
   6258 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   6259 	       " is not the .dynamic section"),
   6260 	     abfd);
   6261 	  bfd_set_error (bfd_error_bad_value);
   6262 	  return false;
   6263 	}
   6264       /* Set the note section type to SHT_NOTE.  */
   6265       else if (p->p_type == PT_NOTE)
   6266 	for (i = 0; i < m->count; i++)
   6267 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   6268 
   6269       if (m->includes_filehdr)
   6270 	{
   6271 	  if (!m->p_flags_valid)
   6272 	    p->p_flags |= PF_R;
   6273 	  p->p_filesz = bed->s->sizeof_ehdr;
   6274 	  p->p_memsz = bed->s->sizeof_ehdr;
   6275 	  if (p->p_type == PT_LOAD)
   6276 	    {
   6277 	      if (m->count > 0)
   6278 		{
   6279 		  if (p->p_vaddr < (bfd_vma) off
   6280 		      || (!m->p_paddr_valid
   6281 			  && p->p_paddr < (bfd_vma) off))
   6282 		    {
   6283 		      _bfd_error_handler
   6284 			(_("%pB: not enough room for program headers,"
   6285 			   " try linking with -N"),
   6286 			 abfd);
   6287 		      bfd_set_error (bfd_error_bad_value);
   6288 		      return false;
   6289 		    }
   6290 		  p->p_vaddr -= off;
   6291 		  if (!m->p_paddr_valid)
   6292 		    p->p_paddr -= off;
   6293 		}
   6294 	    }
   6295 	  else if (sorted_seg_map[0]->includes_filehdr)
   6296 	    {
   6297 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   6298 	      p->p_vaddr = filehdr->p_vaddr;
   6299 	      if (!m->p_paddr_valid)
   6300 		p->p_paddr = filehdr->p_paddr;
   6301 	    }
   6302 	}
   6303 
   6304       if (m->includes_phdrs)
   6305 	{
   6306 	  if (!m->p_flags_valid)
   6307 	    p->p_flags |= PF_R;
   6308 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   6309 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   6310 	  if (!m->includes_filehdr)
   6311 	    {
   6312 	      if (p->p_type == PT_LOAD)
   6313 		{
   6314 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   6315 		  if (m->count > 0)
   6316 		    {
   6317 		      p->p_vaddr -= off - p->p_offset;
   6318 		      if (!m->p_paddr_valid)
   6319 			p->p_paddr -= off - p->p_offset;
   6320 		    }
   6321 		}
   6322 	      else if (phdr_load_seg != NULL)
   6323 		{
   6324 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   6325 		  bfd_vma phdr_off = 0;  /* Octets.  */
   6326 		  if (phdr_load_seg->includes_filehdr)
   6327 		    phdr_off = bed->s->sizeof_ehdr;
   6328 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   6329 		  if (!m->p_paddr_valid)
   6330 		    p->p_paddr = phdr->p_paddr + phdr_off;
   6331 		  p->p_offset = phdr->p_offset + phdr_off;
   6332 		}
   6333 	      else
   6334 		p->p_offset = bed->s->sizeof_ehdr;
   6335 	    }
   6336 	}
   6337 
   6338       if (p->p_type == PT_LOAD
   6339 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   6340 	{
   6341 	  if (!m->includes_filehdr && !m->includes_phdrs)
   6342 	    {
   6343 	      p->p_offset = off;
   6344 	      if (no_contents)
   6345 		{
   6346 		  /* Put meaningless p_offset for PT_LOAD segments
   6347 		     without file contents somewhere within the first
   6348 		     page, in an attempt to not point past EOF.  */
   6349 		  bfd_size_type align = maxpagesize;
   6350 		  if (align < p->p_align)
   6351 		    align = p->p_align;
   6352 		  if (align < 1)
   6353 		    align = 1;
   6354 		  p->p_offset = off % align;
   6355 		}
   6356 	    }
   6357 	  else
   6358 	    {
   6359 	      file_ptr adjust;  /* Octets.  */
   6360 
   6361 	      adjust = off - (p->p_offset + p->p_filesz);
   6362 	      if (!no_contents)
   6363 		p->p_filesz += adjust;
   6364 	      p->p_memsz += adjust;
   6365 	    }
   6366 	}
   6367 
   6368       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   6369 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   6370 	 core files, for sections in PT_NOTE segments.
   6371 	 assign_file_positions_for_non_load_sections will set filepos
   6372 	 for other sections and update p_filesz for other segments.  */
   6373       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6374 	{
   6375 	  asection *sec;
   6376 	  bfd_size_type align;
   6377 	  Elf_Internal_Shdr *this_hdr;
   6378 
   6379 	  sec = *secpp;
   6380 	  this_hdr = &elf_section_data (sec)->this_hdr;
   6381 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   6382 
   6383 	  if ((p->p_type == PT_LOAD
   6384 	       || p->p_type == PT_TLS)
   6385 	      && (this_hdr->sh_type != SHT_NOBITS
   6386 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   6387 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   6388 			  || p->p_type == PT_TLS))))
   6389 	    {
   6390 	      bfd_vma p_start = p->p_paddr;		/* Octets.  */
   6391 	      bfd_vma p_end = p_start + p->p_memsz;	/* Octets.  */
   6392 	      bfd_vma s_start = sec->lma * opb;		/* Octets.  */
   6393 	      bfd_vma adjust = s_start - p_end;		/* Octets.  */
   6394 
   6395 	      if (adjust != 0
   6396 		  && (s_start < p_end
   6397 		      || p_end < p_start))
   6398 		{
   6399 		  _bfd_error_handler
   6400 		    /* xgettext:c-format */
   6401 		    (_("%pB: section %pA lma %#" PRIx64
   6402 		       " adjusted to %#" PRIx64),
   6403 		     abfd, sec, (uint64_t) s_start / opb,
   6404 		     (uint64_t) p_end / opb);
   6405 		  adjust = 0;
   6406 		  sec->lma = p_end / opb;
   6407 		}
   6408 	      p->p_memsz += adjust;
   6409 
   6410 	      if (p->p_type == PT_LOAD)
   6411 		{
   6412 		  if (this_hdr->sh_type != SHT_NOBITS)
   6413 		    {
   6414 		      off_adjust = 0;
   6415 		      if (p->p_filesz + adjust < p->p_memsz)
   6416 			{
   6417 			  /* We have a PROGBITS section following NOBITS ones.
   6418 			     Allocate file space for the NOBITS section(s).
   6419 			     We don't need to write out the zeros, posix
   6420 			     fseek past the end of data already written
   6421 			     followed by a write at that location is
   6422 			     guaranteed to result in zeros being read
   6423 			     from the gap.  */
   6424 			  adjust = p->p_memsz - p->p_filesz;
   6425 			}
   6426 		    }
   6427 		  /* We only adjust sh_offset in SHT_NOBITS sections
   6428 		     as would seem proper for their address when the
   6429 		     section is first in the segment.  sh_offset
   6430 		     doesn't really have any significance for
   6431 		     SHT_NOBITS anyway, apart from a notional position
   6432 		     relative to other sections.  Historically we
   6433 		     didn't bother with adjusting sh_offset and some
   6434 		     programs depend on it not being adjusted.  See
   6435 		     pr12921 and pr25662.  */
   6436 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   6437 		    {
   6438 		      off += adjust;
   6439 		      if (this_hdr->sh_type == SHT_NOBITS)
   6440 			off_adjust += adjust;
   6441 		    }
   6442 		}
   6443 	      if (this_hdr->sh_type != SHT_NOBITS)
   6444 		p->p_filesz += adjust;
   6445 	    }
   6446 
   6447 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   6448 	    {
   6449 	      /* The section at i == 0 is the one that actually contains
   6450 		 everything.  */
   6451 	      if (i == 0)
   6452 		{
   6453 		  this_hdr->sh_offset = sec->filepos = off;
   6454 		  off += this_hdr->sh_size;
   6455 		  p->p_filesz = this_hdr->sh_size;
   6456 		  p->p_memsz = 0;
   6457 		  p->p_align = 1;
   6458 		}
   6459 	      else
   6460 		{
   6461 		  /* The rest are fake sections that shouldn't be written.  */
   6462 		  sec->filepos = 0;
   6463 		  sec->size = 0;
   6464 		  sec->flags = 0;
   6465 		  continue;
   6466 		}
   6467 	    }
   6468 	  else
   6469 	    {
   6470 	      if (p->p_type == PT_LOAD)
   6471 		{
   6472 		  this_hdr->sh_offset = sec->filepos = off;
   6473 		  if (this_hdr->sh_type != SHT_NOBITS)
   6474 		    off += this_hdr->sh_size;
   6475 		}
   6476 	      else if (this_hdr->sh_type == SHT_NOBITS
   6477 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   6478 		       && this_hdr->sh_offset == 0)
   6479 		{
   6480 		  /* This is a .tbss section that didn't get a PT_LOAD.
   6481 		     (See _bfd_elf_map_sections_to_segments "Create a
   6482 		     final PT_LOAD".)  Set sh_offset to the value it
   6483 		     would have if we had created a zero p_filesz and
   6484 		     p_memsz PT_LOAD header for the section.  This
   6485 		     also makes the PT_TLS header have the same
   6486 		     p_offset value.  */
   6487 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   6488 							  off, align);
   6489 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   6490 		}
   6491 
   6492 	      if (this_hdr->sh_type != SHT_NOBITS)
   6493 		{
   6494 		  p->p_filesz += this_hdr->sh_size;
   6495 		  /* A load section without SHF_ALLOC is something like
   6496 		     a note section in a PT_NOTE segment.  These take
   6497 		     file space but are not loaded into memory.  */
   6498 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6499 		    p->p_memsz += this_hdr->sh_size;
   6500 		}
   6501 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6502 		{
   6503 		  if (p->p_type == PT_TLS)
   6504 		    p->p_memsz += this_hdr->sh_size;
   6505 
   6506 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   6507 		     normal segments.  */
   6508 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   6509 		    p->p_memsz += this_hdr->sh_size;
   6510 		}
   6511 
   6512 	      if (align > p->p_align
   6513 		  && !m->p_align_valid
   6514 		  && (p->p_type != PT_LOAD
   6515 		      || (abfd->flags & D_PAGED) == 0))
   6516 		p->p_align = align;
   6517 	    }
   6518 
   6519 	  if (!m->p_flags_valid)
   6520 	    {
   6521 	      p->p_flags |= PF_R;
   6522 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   6523 		p->p_flags |= PF_X;
   6524 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   6525 		p->p_flags |= PF_W;
   6526 	    }
   6527 	}
   6528 
   6529       off -= off_adjust;
   6530 
   6531       /* PR ld/20815 - Check that the program header segment, if
   6532 	 present, will be loaded into memory.  */
   6533       if (p->p_type == PT_PHDR
   6534 	  && phdr_load_seg == NULL
   6535 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   6536 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   6537 	{
   6538 	  /* The fix for this error is usually to edit the linker script being
   6539 	     used and set up the program headers manually.  Either that or
   6540 	     leave room for the headers at the start of the SECTIONS.  */
   6541 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   6542 				" by LOAD segment"),
   6543 			      abfd);
   6544 	  if (link_info == NULL)
   6545 	    return false;
   6546 	  /* Arrange for the linker to exit with an error, deleting
   6547 	     the output file unless --noinhibit-exec is given.  */
   6548 	  link_info->callbacks->info ("%X");
   6549 	}
   6550 
   6551       /* Check that all sections are in a PT_LOAD segment.
   6552 	 Don't check funky gdb generated core files.  */
   6553       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6554 	{
   6555 	  bool check_vma = true;
   6556 
   6557 	  for (i = 1; i < m->count; i++)
   6558 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6559 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6560 				       ->this_hdr), p) != 0
   6561 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6562 				       ->this_hdr), p) != 0)
   6563 	      {
   6564 		/* Looks like we have overlays packed into the segment.  */
   6565 		check_vma = false;
   6566 		break;
   6567 	      }
   6568 
   6569 	  for (i = 0; i < m->count; i++)
   6570 	    {
   6571 	      Elf_Internal_Shdr *this_hdr;
   6572 	      asection *sec;
   6573 
   6574 	      sec = m->sections[i];
   6575 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6576 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6577 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6578 		{
   6579 		  _bfd_error_handler
   6580 		    /* xgettext:c-format */
   6581 		    (_("%pB: section `%pA' can't be allocated in segment %d"),
   6582 		     abfd, sec, j);
   6583 		  print_segment_map (m);
   6584 		}
   6585 	    }
   6586 
   6587 	  if (p_align_p)
   6588 	    p->p_align = p_align;
   6589 	}
   6590     }
   6591 
   6592   elf_next_file_pos (abfd) = off;
   6593 
   6594   if (link_info != NULL
   6595       && phdr_load_seg != NULL
   6596       && phdr_load_seg->includes_filehdr)
   6597     {
   6598       /* There is a segment that contains both the file headers and the
   6599 	 program headers, so provide a symbol __ehdr_start pointing there.
   6600 	 A program can use this to examine itself robustly.  */
   6601 
   6602       struct elf_link_hash_entry *hash
   6603 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   6604 				false, false, true);
   6605       /* If the symbol was referenced and not defined, define it.  */
   6606       if (hash != NULL
   6607 	  && (hash->root.type == bfd_link_hash_new
   6608 	      || hash->root.type == bfd_link_hash_undefined
   6609 	      || hash->root.type == bfd_link_hash_undefweak
   6610 	      || hash->root.type == bfd_link_hash_common))
   6611 	{
   6612 	  asection *s = NULL;
   6613 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6614 
   6615 	  if (phdr_load_seg->count != 0)
   6616 	    /* The segment contains sections, so use the first one.  */
   6617 	    s = phdr_load_seg->sections[0];
   6618 	  else
   6619 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6620 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6621 	      if (m->p_type == PT_LOAD && m->count != 0)
   6622 		{
   6623 		  s = m->sections[0];
   6624 		  break;
   6625 		}
   6626 
   6627 	  if (s != NULL)
   6628 	    {
   6629 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6630 	      hash->root.u.def.section = s;
   6631 	    }
   6632 	  else
   6633 	    {
   6634 	      hash->root.u.def.value = filehdr_vaddr;
   6635 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6636 	    }
   6637 
   6638 	  hash->root.type = bfd_link_hash_defined;
   6639 	  hash->def_regular = 1;
   6640 	  hash->non_elf = 0;
   6641 	}
   6642     }
   6643 
   6644   return true;
   6645 }
   6646 
   6647 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6648    is no defined method for detecting such files, so we have to
   6649    use heuristics instead.  */
   6650 
   6651 bool
   6652 is_debuginfo_file (bfd *abfd)
   6653 {
   6654   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6655     return false;
   6656 
   6657   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6658   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6659   Elf_Internal_Shdr **headerp;
   6660 
   6661   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6662     {
   6663       Elf_Internal_Shdr *header = * headerp;
   6664 
   6665       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6666 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6667       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6668 	  && header->sh_type != SHT_NOBITS
   6669 	  && header->sh_type != SHT_NOTE)
   6670 	return false;
   6671     }
   6672 
   6673   return true;
   6674 }
   6675 
   6676 /* Assign file positions for other sections, except for compressed debug
   6677    and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
   6678 
   6679 static bool
   6680 assign_file_positions_for_non_load_sections (bfd *abfd,
   6681 					     struct bfd_link_info *link_info)
   6682 {
   6683   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6684   Elf_Internal_Shdr **i_shdrpp;
   6685   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6686   Elf_Internal_Phdr *phdrs;
   6687   Elf_Internal_Phdr *p;
   6688   struct elf_segment_map *m;
   6689   file_ptr off;
   6690   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6691   bfd_vma maxpagesize;
   6692 
   6693   if (link_info != NULL)
   6694     maxpagesize = link_info->maxpagesize;
   6695   else
   6696     maxpagesize = bed->maxpagesize;
   6697   i_shdrpp = elf_elfsections (abfd);
   6698   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6699   off = elf_next_file_pos (abfd);
   6700   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6701     {
   6702       Elf_Internal_Shdr *hdr;
   6703       bfd_vma align;
   6704 
   6705       hdr = *hdrpp;
   6706       if (hdr->bfd_section != NULL
   6707 	  && (hdr->bfd_section->filepos != 0
   6708 	      || (hdr->sh_type == SHT_NOBITS
   6709 		  && hdr->contents == NULL)))
   6710 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6711       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6712 	{
   6713 	  if (hdr->sh_size != 0
   6714 	      /* PR 24717 - debuginfo files are known to be not strictly
   6715 		 compliant with the ELF standard.  In particular they often
   6716 		 have .note.gnu.property sections that are outside of any
   6717 		 loadable segment.  This is not a problem for such files,
   6718 		 so do not warn about them.  */
   6719 	      && ! is_debuginfo_file (abfd))
   6720 	    _bfd_error_handler
   6721 	      /* xgettext:c-format */
   6722 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6723 	       abfd,
   6724 	       (hdr->bfd_section == NULL
   6725 		? "*unknown*"
   6726 		: hdr->bfd_section->name));
   6727 	  /* We don't need to page align empty sections.  */
   6728 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6729 	    align = maxpagesize;
   6730 	  else
   6731 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6732 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6733 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   6734 							   false);
   6735 	}
   6736       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6737 		&& hdr->bfd_section == NULL)
   6738 	       /* We don't know the offset of these sections yet:
   6739 		  their size has not been decided.  */
   6740 	       || (abfd->is_linker_output
   6741 		   && hdr->bfd_section != NULL
   6742 		   && (hdr->sh_name == -1u
   6743 		       || bfd_section_is_ctf (hdr->bfd_section)))
   6744 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6745 	       || (elf_symtab_shndx_list (abfd) != NULL
   6746 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6747 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6748 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6749 	hdr->sh_offset = -1;
   6750       else
   6751 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6752     }
   6753   elf_next_file_pos (abfd) = off;
   6754 
   6755   /* Now that we have set the section file positions, we can set up
   6756      the file positions for the non PT_LOAD segments.  */
   6757   phdrs = elf_tdata (abfd)->phdr;
   6758   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6759     {
   6760       if (p->p_type == PT_GNU_RELRO)
   6761 	{
   6762 	  bfd_vma start, end;  /* Bytes.  */
   6763 	  bool ok;
   6764 
   6765 	  if (link_info != NULL)
   6766 	    {
   6767 	      /* During linking the range of the RELRO segment is passed
   6768 		 in link_info.  Note that there may be padding between
   6769 		 relro_start and the first RELRO section.  */
   6770 	      start = link_info->relro_start;
   6771 	      end = link_info->relro_end;
   6772 	    }
   6773 	  else if (m->count != 0)
   6774 	    {
   6775 	      if (!m->p_size_valid)
   6776 		abort ();
   6777 	      start = m->sections[0]->vma;
   6778 	      end = start + m->p_size / opb;
   6779 	    }
   6780 	  else
   6781 	    {
   6782 	      start = 0;
   6783 	      end = 0;
   6784 	    }
   6785 
   6786 	  ok = false;
   6787 	  if (start < end)
   6788 	    {
   6789 	      struct elf_segment_map *lm;
   6790 	      const Elf_Internal_Phdr *lp;
   6791 	      unsigned int i;
   6792 
   6793 	      /* Find a LOAD segment containing a section in the RELRO
   6794 		 segment.  */
   6795 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6796 		   lm != NULL;
   6797 		   lm = lm->next, lp++)
   6798 		{
   6799 		  if (lp->p_type == PT_LOAD
   6800 		      && lm->count != 0
   6801 		      && (lm->sections[lm->count - 1]->vma
   6802 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6803 			     ? lm->sections[lm->count - 1]->size / opb
   6804 			     : 0)) > start
   6805 		      && lm->sections[0]->vma < end)
   6806 		    break;
   6807 		}
   6808 
   6809 	      if (lm != NULL)
   6810 		{
   6811 		  /* Find the section starting the RELRO segment.  */
   6812 		  for (i = 0; i < lm->count; i++)
   6813 		    {
   6814 		      asection *s = lm->sections[i];
   6815 		      if (s->vma >= start
   6816 			  && s->vma < end
   6817 			  && s->size != 0)
   6818 			break;
   6819 		    }
   6820 
   6821 		  if (i < lm->count)
   6822 		    {
   6823 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6824 		      p->p_paddr = lm->sections[i]->lma * opb;
   6825 		      p->p_offset = lm->sections[i]->filepos;
   6826 		      p->p_memsz = end * opb - p->p_vaddr;
   6827 		      p->p_filesz = p->p_memsz;
   6828 
   6829 		      /* The RELRO segment typically ends a few bytes
   6830 			 into .got.plt but other layouts are possible.
   6831 			 In cases where the end does not match any
   6832 			 loaded section (for instance is in file
   6833 			 padding), trim p_filesz back to correspond to
   6834 			 the end of loaded section contents.  */
   6835 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6836 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6837 
   6838 		      /* Preserve the alignment and flags if they are
   6839 			 valid.  The gold linker generates RW/4 for
   6840 			 the PT_GNU_RELRO section.  It is better for
   6841 			 objcopy/strip to honor these attributes
   6842 			 otherwise gdb will choke when using separate
   6843 			 debug files.  */
   6844 		      if (!m->p_align_valid)
   6845 			p->p_align = 1;
   6846 		      if (!m->p_flags_valid)
   6847 			p->p_flags = PF_R;
   6848 		      ok = true;
   6849 		    }
   6850 		}
   6851 	    }
   6852 
   6853 	  if (!ok)
   6854 	    {
   6855 	      if (link_info != NULL)
   6856 		_bfd_error_handler
   6857 		  (_("%pB: warning: unable to allocate any sections"
   6858 		     " to PT_GNU_RELRO segment"),
   6859 		   abfd);
   6860 	      memset (p, 0, sizeof *p);
   6861 	    }
   6862 	}
   6863       else if (p->p_type == PT_GNU_STACK)
   6864 	{
   6865 	  if (m->p_size_valid)
   6866 	    p->p_memsz = m->p_size;
   6867 	}
   6868       else if (m->count != 0)
   6869 	{
   6870 	  unsigned int i;
   6871 
   6872 	  if (p->p_type != PT_LOAD
   6873 	      && (p->p_type != PT_NOTE
   6874 		  || bfd_get_format (abfd) != bfd_core))
   6875 	    {
   6876 	      /* A user specified segment layout may include a PHDR
   6877 		 segment that overlaps with a LOAD segment...  */
   6878 	      if (p->p_type == PT_PHDR)
   6879 		{
   6880 		  m->count = 0;
   6881 		  continue;
   6882 		}
   6883 
   6884 	      if (m->includes_filehdr || m->includes_phdrs)
   6885 		{
   6886 		  /* PR 17512: file: 2195325e.  */
   6887 		  _bfd_error_handler
   6888 		    (_("%pB: error: non-load segment %d includes file header "
   6889 		       "and/or program header"),
   6890 		     abfd, (int) (p - phdrs));
   6891 		  return false;
   6892 		}
   6893 
   6894 	      p->p_filesz = 0;
   6895 	      p->p_offset = m->sections[0]->filepos;
   6896 	      for (i = m->count; i-- != 0;)
   6897 		{
   6898 		  asection *sect = m->sections[i];
   6899 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6900 		  if (hdr->sh_type != SHT_NOBITS)
   6901 		    {
   6902 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6903 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6904 			 should be the same as p_filesz.  */
   6905 		      if (p->p_type == PT_NOTE
   6906 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6907 			p->p_memsz = p->p_filesz;
   6908 		      break;
   6909 		    }
   6910 		}
   6911 	    }
   6912 	}
   6913     }
   6914 
   6915   return true;
   6916 }
   6917 
   6918 static elf_section_list *
   6919 find_section_in_list (unsigned int i, elf_section_list * list)
   6920 {
   6921   for (;list != NULL; list = list->next)
   6922     if (list->ndx == i)
   6923       break;
   6924   return list;
   6925 }
   6926 
   6927 /* Work out the file positions of all the sections.  This is called by
   6928    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6929    VMAs must be known before this is called.
   6930 
   6931    Reloc sections come in two flavours: Those processed specially as
   6932    "side-channel" data attached to a section to which they apply, and
   6933    those that bfd doesn't process as relocations.  The latter sort are
   6934    stored in a normal bfd section by bfd_section_from_shdr.  We don't
   6935    consider the former sort here, unless they form part of the loadable
   6936    image.  Reloc sections not assigned here (and compressed debugging
   6937    sections and CTF sections which nothing else in the file can rely
   6938    upon) will be handled later by assign_file_positions_for_relocs.
   6939 
   6940    We also don't set the positions of the .symtab and .strtab here.  */
   6941 
   6942 static bool
   6943 assign_file_positions_except_relocs (bfd *abfd,
   6944 				     struct bfd_link_info *link_info)
   6945 {
   6946   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6947   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6948   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6949   unsigned int alloc;
   6950 
   6951   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6952       && bfd_get_format (abfd) != bfd_core)
   6953     {
   6954       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6955       unsigned int num_sec = elf_numsections (abfd);
   6956       Elf_Internal_Shdr **hdrpp;
   6957       unsigned int i;
   6958       file_ptr off;
   6959 
   6960       /* Start after the ELF header.  */
   6961       off = i_ehdrp->e_ehsize;
   6962 
   6963       /* We are not creating an executable, which means that we are
   6964 	 not creating a program header, and that the actual order of
   6965 	 the sections in the file is unimportant.  */
   6966       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6967 	{
   6968 	  Elf_Internal_Shdr *hdr;
   6969 
   6970 	  hdr = *hdrpp;
   6971 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6972 	       && hdr->bfd_section == NULL)
   6973 	      /* Do not assign offsets for these sections yet: we don't know
   6974 		 their sizes.  */
   6975 	      || (abfd->is_linker_output
   6976 		  && hdr->bfd_section != NULL
   6977 		  && (hdr->sh_name == -1u
   6978 		      || bfd_section_is_ctf (hdr->bfd_section)))
   6979 	      || i == elf_onesymtab (abfd)
   6980 	      || (elf_symtab_shndx_list (abfd) != NULL
   6981 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6982 	      || i == elf_strtab_sec (abfd)
   6983 	      || i == elf_shstrtab_sec (abfd))
   6984 	    {
   6985 	      hdr->sh_offset = -1;
   6986 	    }
   6987 	  else
   6988 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6989 	}
   6990 
   6991       elf_next_file_pos (abfd) = off;
   6992       elf_program_header_size (abfd) = 0;
   6993     }
   6994   else
   6995     {
   6996       /* Assign file positions for the loaded sections based on the
   6997 	 assignment of sections to segments.  */
   6998       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6999 	return false;
   7000 
   7001       /* And for non-load sections.  */
   7002       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   7003 	return false;
   7004     }
   7005 
   7006   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   7007     return false;
   7008 
   7009   /* Write out the program headers.  */
   7010   alloc = i_ehdrp->e_phnum;
   7011   if (alloc != 0)
   7012     {
   7013       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   7014 	{
   7015 	  bool warned_tls = false;
   7016 	  bool warned_rwx = false;
   7017 
   7018 	  /* Memory resident segments with non-zero size and RWX
   7019 	     permissions are a security risk, so we generate a warning
   7020 	     here if we are creating any.  */
   7021 	  unsigned int i;
   7022 
   7023 	  for (i = 0; i < alloc; i++)
   7024 	    {
   7025 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   7026 
   7027 	      if (phdr->p_memsz == 0)
   7028 		continue;
   7029 
   7030 	      if (! warned_tls
   7031 		  && phdr->p_type == PT_TLS
   7032 		  && (phdr->p_flags & PF_X))
   7033 		{
   7034 		  if (link_info->warn_is_error_for_rwx_segments)
   7035 		    {
   7036 		      _bfd_error_handler (_("\
   7037 error: %pB has a TLS segment with execute permission"),
   7038 					  abfd);
   7039 		      return false;
   7040 		    }
   7041 
   7042 		  _bfd_error_handler (_("\
   7043 warning: %pB has a TLS segment with execute permission"),
   7044 				      abfd);
   7045 		  if (warned_rwx)
   7046 		    break;
   7047 
   7048 		  warned_tls = true;
   7049 		}
   7050 	      else if (! warned_rwx
   7051 		       && phdr->p_type == PT_LOAD
   7052 		       && ((phdr->p_flags & (PF_R | PF_W | PF_X))
   7053 			   == (PF_R | PF_W | PF_X)))
   7054 		{
   7055 		  if (link_info->warn_is_error_for_rwx_segments)
   7056 		    {
   7057 		      _bfd_error_handler (_("\
   7058 error: %pB has a LOAD segment with RWX permissions"),
   7059 					  abfd);
   7060 		      return false;
   7061 		    }
   7062 
   7063 		  _bfd_error_handler (_("\
   7064 warning: %pB has a LOAD segment with RWX permissions"),
   7065 				      abfd);
   7066 		  if (warned_tls)
   7067 		    break;
   7068 
   7069 		  warned_rwx = true;
   7070 		}
   7071 	    }
   7072 	}
   7073 
   7074       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   7075 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   7076 	return false;
   7077     }
   7078 
   7079   return true;
   7080 }
   7081 
   7082 bool
   7083 _bfd_elf_init_file_header (bfd *abfd,
   7084 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   7085 {
   7086   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   7087   struct elf_strtab_hash *shstrtab;
   7088   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7089 
   7090   i_ehdrp = elf_elfheader (abfd);
   7091 
   7092   shstrtab = _bfd_elf_strtab_init ();
   7093   if (shstrtab == NULL)
   7094     return false;
   7095 
   7096   elf_shstrtab (abfd) = shstrtab;
   7097 
   7098   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   7099   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   7100   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   7101   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   7102 
   7103   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   7104   i_ehdrp->e_ident[EI_DATA] =
   7105     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   7106   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   7107 
   7108   if ((abfd->flags & DYNAMIC) != 0)
   7109     i_ehdrp->e_type = ET_DYN;
   7110   else if ((abfd->flags & EXEC_P) != 0)
   7111     i_ehdrp->e_type = ET_EXEC;
   7112   else if (bfd_get_format (abfd) == bfd_core)
   7113     i_ehdrp->e_type = ET_CORE;
   7114   else
   7115     i_ehdrp->e_type = ET_REL;
   7116 
   7117   switch (bfd_get_arch (abfd))
   7118     {
   7119     case bfd_arch_unknown:
   7120       i_ehdrp->e_machine = EM_NONE;
   7121       break;
   7122 
   7123       /* There used to be a long list of cases here, each one setting
   7124 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   7125 	 in the corresponding bfd definition.  To avoid duplication,
   7126 	 the switch was removed.  Machines that need special handling
   7127 	 can generally do it in elf_backend_final_write_processing(),
   7128 	 unless they need the information earlier than the final write.
   7129 	 Such need can generally be supplied by replacing the tests for
   7130 	 e_machine with the conditions used to determine it.  */
   7131     default:
   7132       i_ehdrp->e_machine = bed->elf_machine_code;
   7133     }
   7134 
   7135   i_ehdrp->e_version = bed->s->ev_current;
   7136   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   7137 
   7138   /* No program header, for now.  */
   7139   i_ehdrp->e_phoff = 0;
   7140   i_ehdrp->e_phentsize = 0;
   7141   i_ehdrp->e_phnum = 0;
   7142 
   7143   /* Each bfd section is section header entry.  */
   7144   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   7145   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   7146 
   7147   elf_tdata (abfd)->symtab_hdr.sh_name =
   7148     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   7149   elf_tdata (abfd)->strtab_hdr.sh_name =
   7150     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   7151   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   7152     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   7153   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   7154       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   7155       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   7156     return false;
   7157 
   7158   return true;
   7159 }
   7160 
   7161 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   7162 
   7163    FIXME: We used to have code here to sort the PT_LOAD segments into
   7164    ascending order, as per the ELF spec.  But this breaks some programs,
   7165    including the Linux kernel.  But really either the spec should be
   7166    changed or the programs updated.  */
   7167 
   7168 bool
   7169 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   7170 {
   7171   if (link_info != NULL && bfd_link_pie (link_info))
   7172     {
   7173       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   7174       unsigned int num_segments = i_ehdrp->e_phnum;
   7175       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   7176       Elf_Internal_Phdr *segment = tdata->phdr;
   7177       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   7178 
   7179       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   7180       bfd_vma p_vaddr = (bfd_vma) -1;
   7181       for (; segment < end_segment; segment++)
   7182 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   7183 	  p_vaddr = segment->p_vaddr;
   7184 
   7185       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   7186 	 segments is non-zero.  */
   7187       if (p_vaddr)
   7188 	i_ehdrp->e_type = ET_EXEC;
   7189     }
   7190   return true;
   7191 }
   7192 
   7193 /* Assign file positions for all the reloc sections which are not part
   7194    of the loadable file image, and the file position of section headers.  */
   7195 
   7196 static bool
   7197 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   7198 {
   7199   file_ptr off;
   7200   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   7201   Elf_Internal_Shdr *shdrp;
   7202   Elf_Internal_Ehdr *i_ehdrp;
   7203   const struct elf_backend_data *bed;
   7204 
   7205   /* Skip non-load sections without section header.  */
   7206   if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
   7207     return true;
   7208 
   7209   off = elf_next_file_pos (abfd);
   7210 
   7211   shdrpp = elf_elfsections (abfd);
   7212   end_shdrpp = shdrpp + elf_numsections (abfd);
   7213   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   7214     {
   7215       shdrp = *shdrpp;
   7216       if (shdrp->sh_offset == -1)
   7217 	{
   7218 	  asection *sec = shdrp->bfd_section;
   7219 	  if (sec == NULL
   7220 	      || shdrp->sh_type == SHT_REL
   7221 	      || shdrp->sh_type == SHT_RELA)
   7222 	    ;
   7223 	  else if (bfd_section_is_ctf (sec))
   7224 	    {
   7225 	      /* Update section size and contents.	*/
   7226 	      shdrp->sh_size = sec->size;
   7227 	      shdrp->contents = sec->contents;
   7228 	    }
   7229 	  else if (shdrp->sh_name == -1u)
   7230 	    {
   7231 	      const char *name = sec->name;
   7232 	      struct bfd_elf_section_data *d;
   7233 
   7234 	      /* Compress DWARF debug sections.  */
   7235 	      if (!bfd_compress_section (abfd, sec, shdrp->contents))
   7236 		return false;
   7237 
   7238 	      if (sec->compress_status == COMPRESS_SECTION_DONE
   7239 		  && (abfd->flags & BFD_COMPRESS_GABI) == 0
   7240 		  && name[1] == 'd')
   7241 		{
   7242 		  /* If section is compressed with zlib-gnu, convert
   7243 		     section name from .debug_* to .zdebug_*.  */
   7244 		  char *new_name = bfd_debug_name_to_zdebug (abfd, name);
   7245 		  if (new_name == NULL)
   7246 		    return false;
   7247 		  name = new_name;
   7248 		}
   7249 	      /* Add section name to section name section.  */
   7250 	      shdrp->sh_name
   7251 		= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   7252 						      name, false);
   7253 	      d = elf_section_data (sec);
   7254 
   7255 	      /* Add reloc section name to section name section.  */
   7256 	      if (d->rel.hdr
   7257 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
   7258 						  name, false))
   7259 		return false;
   7260 	      if (d->rela.hdr
   7261 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
   7262 						  name, true))
   7263 		return false;
   7264 
   7265 	      /* Update section size and contents.  */
   7266 	      shdrp->sh_size = sec->size;
   7267 	      shdrp->contents = sec->contents;
   7268 	      sec->contents = NULL;
   7269 	    }
   7270 
   7271 	  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   7272 	}
   7273     }
   7274 
   7275   /* Place section name section after DWARF debug sections have been
   7276      compressed.  */
   7277   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   7278   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   7279   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   7280   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   7281 
   7282   /* Place the section headers.  */
   7283   i_ehdrp = elf_elfheader (abfd);
   7284   bed = get_elf_backend_data (abfd);
   7285   off = align_file_position (off, 1 << bed->s->log_file_align);
   7286   i_ehdrp->e_shoff = off;
   7287   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   7288   elf_next_file_pos (abfd) = off;
   7289 
   7290   return true;
   7291 }
   7292 
   7293 bool
   7294 _bfd_elf_write_object_contents (bfd *abfd)
   7295 {
   7296   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7297   Elf_Internal_Shdr **i_shdrp;
   7298   bool failed;
   7299   unsigned int count, num_sec;
   7300   struct elf_obj_tdata *t;
   7301 
   7302   if (! abfd->output_has_begun
   7303       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   7304     return false;
   7305   /* Do not rewrite ELF data when the BFD has been opened for update.
   7306      abfd->output_has_begun was set to TRUE on opening, so creation of
   7307      new sections, and modification of existing section sizes was
   7308      restricted.  This means the ELF header, program headers and
   7309      section headers can't have changed.  If the contents of any
   7310      sections has been modified, then those changes have already been
   7311      written to the BFD.  */
   7312   else if (abfd->direction == both_direction)
   7313     {
   7314       BFD_ASSERT (abfd->output_has_begun);
   7315       return true;
   7316     }
   7317 
   7318   i_shdrp = elf_elfsections (abfd);
   7319 
   7320   failed = false;
   7321   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   7322   if (failed)
   7323     return false;
   7324 
   7325   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   7326     return false;
   7327 
   7328   /* After writing the headers, we need to write the sections too...  */
   7329   num_sec = elf_numsections (abfd);
   7330   for (count = 1; count < num_sec; count++)
   7331     {
   7332       /* Don't set the sh_name field without section header.  */
   7333       if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
   7334 	i_shdrp[count]->sh_name
   7335 	  = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   7336 				    i_shdrp[count]->sh_name);
   7337       if (bed->elf_backend_section_processing)
   7338 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   7339 	  return false;
   7340       if (i_shdrp[count]->contents)
   7341 	{
   7342 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   7343 
   7344 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   7345 	      || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
   7346 	    return false;
   7347 	}
   7348     }
   7349 
   7350   /* Write out the section header names.  */
   7351   t = elf_tdata (abfd);
   7352   if (elf_shstrtab (abfd) != NULL
   7353       && t->shstrtab_hdr.sh_offset != -1
   7354       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   7355 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   7356     return false;
   7357 
   7358   if (!(*bed->elf_backend_final_write_processing) (abfd))
   7359     return false;
   7360 
   7361   if (!bed->s->write_shdrs_and_ehdr (abfd))
   7362     return false;
   7363 
   7364   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   7365   if (t->o->build_id.after_write_object_contents != NULL
   7366       && !(*t->o->build_id.after_write_object_contents) (abfd))
   7367     return false;
   7368   if (t->o->package_metadata.after_write_object_contents != NULL
   7369       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   7370     return false;
   7371 
   7372   return true;
   7373 }
   7374 
   7375 bool
   7376 _bfd_elf_write_corefile_contents (bfd *abfd)
   7377 {
   7378   /* Hopefully this can be done just like an object file.  */
   7379   return _bfd_elf_write_object_contents (abfd);
   7380 }
   7381 
   7382 /* Given a section, search the header to find them.  */
   7383 
   7384 unsigned int
   7385 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   7386 {
   7387   const struct elf_backend_data *bed;
   7388   unsigned int sec_index;
   7389 
   7390   if (elf_section_data (asect) != NULL
   7391       && elf_section_data (asect)->this_idx != 0)
   7392     return elf_section_data (asect)->this_idx;
   7393 
   7394   if (bfd_is_abs_section (asect))
   7395     sec_index = SHN_ABS;
   7396   else if (bfd_is_com_section (asect))
   7397     sec_index = SHN_COMMON;
   7398   else if (bfd_is_und_section (asect))
   7399     sec_index = SHN_UNDEF;
   7400   else
   7401     sec_index = SHN_BAD;
   7402 
   7403   bed = get_elf_backend_data (abfd);
   7404   if (bed->elf_backend_section_from_bfd_section)
   7405     {
   7406       int retval = sec_index;
   7407 
   7408       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   7409 	return retval;
   7410     }
   7411 
   7412   if (sec_index == SHN_BAD)
   7413     bfd_set_error (bfd_error_nonrepresentable_section);
   7414 
   7415   return sec_index;
   7416 }
   7417 
   7418 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   7419    on error.  */
   7420 
   7421 int
   7422 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   7423 {
   7424   asymbol *asym_ptr = *asym_ptr_ptr;
   7425   int idx;
   7426   flagword flags = asym_ptr->flags;
   7427 
   7428   /* When gas creates relocations against local labels, it creates its
   7429      own symbol for the section, but does put the symbol into the
   7430      symbol chain, so udata is 0.  When the linker is generating
   7431      relocatable output, this section symbol may be for one of the
   7432      input sections rather than the output section.  */
   7433   if (asym_ptr->udata.i == 0
   7434       && (flags & BSF_SECTION_SYM)
   7435       && asym_ptr->section)
   7436     {
   7437       asection *sec;
   7438 
   7439       sec = asym_ptr->section;
   7440       if (sec->owner != abfd && sec->output_section != NULL)
   7441 	sec = sec->output_section;
   7442       if (sec->owner == abfd
   7443 	  && sec->index < elf_num_section_syms (abfd)
   7444 	  && elf_section_syms (abfd)[sec->index] != NULL)
   7445 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   7446     }
   7447 
   7448   idx = asym_ptr->udata.i;
   7449 
   7450   if (idx == 0)
   7451     {
   7452       /* This case can occur when using --strip-symbol on a symbol
   7453 	 which is used in a relocation entry.  */
   7454       _bfd_error_handler
   7455 	/* xgettext:c-format */
   7456 	(_("%pB: symbol `%s' required but not present"),
   7457 	 abfd, bfd_asymbol_name (asym_ptr));
   7458       bfd_set_error (bfd_error_no_symbols);
   7459       return -1;
   7460     }
   7461 
   7462 #if DEBUG & 4
   7463   {
   7464     fprintf (stderr,
   7465 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
   7466 	     " flags = 0x%.8x\n",
   7467 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   7468     fflush (stderr);
   7469   }
   7470 #endif
   7471 
   7472   return idx;
   7473 }
   7474 
   7475 static inline bfd_vma
   7476 segment_size (Elf_Internal_Phdr *segment)
   7477 {
   7478   return (segment->p_memsz > segment->p_filesz
   7479 	  ? segment->p_memsz : segment->p_filesz);
   7480 }
   7481 
   7482 
   7483 /* Returns the end address of the segment + 1.  */
   7484 static inline bfd_vma
   7485 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
   7486 {
   7487   return start + segment_size (segment);
   7488 }
   7489 
   7490 static inline bfd_size_type
   7491 section_size (asection *section, Elf_Internal_Phdr *segment)
   7492 {
   7493   if ((section->flags & SEC_HAS_CONTENTS) != 0
   7494       || (section->flags & SEC_THREAD_LOCAL) == 0
   7495       || segment->p_type == PT_TLS)
   7496     return section->size;
   7497   return 0;
   7498 }
   7499 
   7500 /* Returns TRUE if the given section is contained within the given
   7501    segment.  LMA addresses are compared against PADDR when
   7502    USE_VADDR is false, VMA against VADDR when true.  */
   7503 static bool
   7504 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
   7505 		 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
   7506 		 bool use_vaddr)
   7507 {
   7508   bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
   7509   bfd_vma addr = !use_vaddr ? section->lma : section->vma;
   7510   bfd_vma octet;
   7511   if (_bfd_mul_overflow (addr, opb, &octet))
   7512     return false;
   7513   /* The third and fourth lines below are testing that the section end
   7514      address is within the segment.  It's written this way to avoid
   7515      overflow.  Add seg_addr + section_size to both sides of the
   7516      inequality to make it obvious.  */
   7517   return (octet >= seg_addr
   7518 	  && segment_size (segment) >= section_size (section, segment)
   7519 	  && (octet - seg_addr
   7520 	      <= segment_size (segment) - section_size (section, segment)));
   7521 }
   7522 
   7523 /* Handle PT_NOTE segment.  */
   7524 static bool
   7525 is_note (asection *s, Elf_Internal_Phdr *p)
   7526 {
   7527   return (p->p_type == PT_NOTE
   7528 	  && elf_section_type (s) == SHT_NOTE
   7529 	  && (ufile_ptr) s->filepos >= p->p_offset
   7530 	  && p->p_filesz >= s->size
   7531 	  && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
   7532 }
   7533 
   7534 /* Rewrite program header information.  */
   7535 
   7536 static bool
   7537 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   7538 {
   7539   Elf_Internal_Ehdr *iehdr;
   7540   struct elf_segment_map *map;
   7541   struct elf_segment_map *map_first;
   7542   struct elf_segment_map **pointer_to_map;
   7543   Elf_Internal_Phdr *segment;
   7544   asection *section;
   7545   unsigned int i;
   7546   unsigned int num_segments;
   7547   bool phdr_included = false;
   7548   bool p_paddr_valid;
   7549   struct elf_segment_map *phdr_adjust_seg = NULL;
   7550   unsigned int phdr_adjust_num = 0;
   7551   const struct elf_backend_data *bed;
   7552   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7553 
   7554   bed = get_elf_backend_data (ibfd);
   7555   iehdr = elf_elfheader (ibfd);
   7556 
   7557   map_first = NULL;
   7558   pointer_to_map = &map_first;
   7559 
   7560   num_segments = elf_elfheader (ibfd)->e_phnum;
   7561 
   7562   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   7563      linker, which generates a PT_INTERP section with p_vaddr and
   7564      p_memsz set to 0.  */
   7565 #define IS_SOLARIS_PT_INTERP(p, s)					\
   7566   (p->p_vaddr == 0							\
   7567    && p->p_paddr == 0							\
   7568    && p->p_memsz == 0							\
   7569    && p->p_filesz > 0							\
   7570    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   7571    && s->size > 0							\
   7572    && (bfd_vma) s->filepos >= p->p_offset				\
   7573    && ((bfd_vma) s->filepos + s->size					\
   7574        <= p->p_offset + p->p_filesz))
   7575 
   7576   /* Decide if the given section should be included in the given segment.
   7577      A section will be included if:
   7578        1. It is within the address space of the segment -- we use the LMA
   7579 	  if that is set for the segment and the VMA otherwise,
   7580        2. It is an allocated section or a NOTE section in a PT_NOTE
   7581 	  segment.
   7582        3. There is an output section associated with it,
   7583        4. The section has not already been allocated to a previous segment.
   7584        5. PT_GNU_STACK segments do not include any sections.
   7585        6. PT_TLS segment includes only SHF_TLS sections.
   7586        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   7587        8. PT_DYNAMIC should not contain empty sections at the beginning
   7588 	  (with the possible exception of .dynamic).  */
   7589 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb)		\
   7590   (((is_contained_by (section, segment, segment->p_paddr,		\
   7591 		      segment->p_vaddr, opb,				\
   7592 		      bed->want_p_paddr_set_to_zero)			\
   7593      && (section->flags & SEC_ALLOC) != 0)				\
   7594     || is_note (section, segment))					\
   7595    && segment->p_type != PT_GNU_STACK					\
   7596    && (segment->p_type != PT_TLS					\
   7597        || (section->flags & SEC_THREAD_LOCAL))				\
   7598    && (segment->p_type == PT_LOAD					\
   7599        || segment->p_type == PT_TLS					\
   7600        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7601    && (segment->p_type != PT_DYNAMIC					\
   7602        || section_size (section, segment) > 0				\
   7603        || (segment->p_paddr						\
   7604 	   ? segment->p_paddr != section->lma * (opb)			\
   7605 	   : segment->p_vaddr != section->vma * (opb))			\
   7606        || (strcmp (bfd_section_name (section), ".dynamic") == 0))	\
   7607    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7608 
   7609 /* If the output section of a section in the input segment is NULL,
   7610    it is removed from the corresponding output segment.   */
   7611 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb)		\
   7612   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)		\
   7613    && section->output_section != NULL)
   7614 
   7615   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7616 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7617   (seg1->field >= segment_end (seg2, seg2->field))
   7618 
   7619   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7620      their VMA address ranges and their LMA address ranges overlap.
   7621      It is possible to have overlapping VMA ranges without overlapping LMA
   7622      ranges.  RedBoot images for example can have both .data and .bss mapped
   7623      to the same VMA range, but with the .data section mapped to a different
   7624      LMA.  */
   7625 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7626   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7627 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7628    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7629 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7630 
   7631   /* Initialise the segment mark field, and discard stupid alignment.  */
   7632   for (section = ibfd->sections; section != NULL; section = section->next)
   7633     {
   7634       asection *o = section->output_section;
   7635       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7636 	o->alignment_power = 0;
   7637       section->segment_mark = false;
   7638     }
   7639 
   7640   /* The Solaris linker creates program headers in which all the
   7641      p_paddr fields are zero.  When we try to objcopy or strip such a
   7642      file, we get confused.  Check for this case, and if we find it
   7643      don't set the p_paddr_valid fields.  */
   7644   p_paddr_valid = false;
   7645   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7646        i < num_segments;
   7647        i++, segment++)
   7648     if (segment->p_paddr != 0)
   7649       {
   7650 	p_paddr_valid = true;
   7651 	break;
   7652       }
   7653 
   7654   /* Scan through the segments specified in the program header
   7655      of the input BFD.  For this first scan we look for overlaps
   7656      in the loadable segments.  These can be created by weird
   7657      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7658   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7659        i < num_segments;
   7660        i++, segment++)
   7661     {
   7662       unsigned int j;
   7663       Elf_Internal_Phdr *segment2;
   7664 
   7665       if (segment->p_type == PT_INTERP)
   7666 	for (section = ibfd->sections; section; section = section->next)
   7667 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7668 	    {
   7669 	      /* Mininal change so that the normal section to segment
   7670 		 assignment code will work.  */
   7671 	      segment->p_vaddr = section->vma * opb;
   7672 	      break;
   7673 	    }
   7674 
   7675       if (segment->p_type != PT_LOAD)
   7676 	{
   7677 	  /* Remove PT_GNU_RELRO segment.  */
   7678 	  if (segment->p_type == PT_GNU_RELRO)
   7679 	    segment->p_type = PT_NULL;
   7680 	  continue;
   7681 	}
   7682 
   7683       /* Determine if this segment overlaps any previous segments.  */
   7684       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7685 	{
   7686 	  bfd_signed_vma extra_length;
   7687 
   7688 	  if (segment2->p_type != PT_LOAD
   7689 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7690 	    continue;
   7691 
   7692 	  /* Merge the two segments together.  */
   7693 	  if (segment2->p_vaddr < segment->p_vaddr)
   7694 	    {
   7695 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7696 		 SEGMENT.  */
   7697 	      extra_length = (segment_end (segment, segment->p_vaddr)
   7698 			      - segment_end (segment2, segment2->p_vaddr));
   7699 
   7700 	      if (extra_length > 0)
   7701 		{
   7702 		  segment2->p_memsz += extra_length;
   7703 		  segment2->p_filesz += extra_length;
   7704 		}
   7705 
   7706 	      segment->p_type = PT_NULL;
   7707 
   7708 	      /* Since we have deleted P we must restart the outer loop.  */
   7709 	      i = 0;
   7710 	      segment = elf_tdata (ibfd)->phdr;
   7711 	      break;
   7712 	    }
   7713 	  else
   7714 	    {
   7715 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7716 		 SEGMENT2.  */
   7717 	      extra_length = (segment_end (segment2, segment2->p_vaddr)
   7718 			      - segment_end (segment, segment->p_vaddr));
   7719 
   7720 	      if (extra_length > 0)
   7721 		{
   7722 		  segment->p_memsz += extra_length;
   7723 		  segment->p_filesz += extra_length;
   7724 		}
   7725 
   7726 	      segment2->p_type = PT_NULL;
   7727 	    }
   7728 	}
   7729     }
   7730 
   7731   /* The second scan attempts to assign sections to segments.  */
   7732   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7733        i < num_segments;
   7734        i++, segment++)
   7735     {
   7736       unsigned int section_count;
   7737       asection **sections;
   7738       asection *output_section;
   7739       unsigned int isec;
   7740       asection *matching_lma;
   7741       asection *suggested_lma;
   7742       unsigned int j;
   7743       size_t amt;
   7744       asection *first_section;
   7745 
   7746       if (segment->p_type == PT_NULL)
   7747 	continue;
   7748 
   7749       first_section = NULL;
   7750       /* Compute how many sections might be placed into this segment.  */
   7751       for (section = ibfd->sections, section_count = 0;
   7752 	   section != NULL;
   7753 	   section = section->next)
   7754 	{
   7755 	  /* Find the first section in the input segment, which may be
   7756 	     removed from the corresponding output segment.   */
   7757 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
   7758 	    {
   7759 	      if (first_section == NULL)
   7760 		first_section = section;
   7761 	      if (section->output_section != NULL)
   7762 		++section_count;
   7763 	    }
   7764 	}
   7765 
   7766       /* Allocate a segment map big enough to contain
   7767 	 all of the sections we have selected.  */
   7768       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7769       amt += section_count * sizeof (asection *);
   7770       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7771       if (map == NULL)
   7772 	return false;
   7773 
   7774       /* Initialise the fields of the segment map.  Default to
   7775 	 using the physical address of the segment in the input BFD.  */
   7776       map->next = NULL;
   7777       map->p_type = segment->p_type;
   7778       map->p_flags = segment->p_flags;
   7779       map->p_flags_valid = 1;
   7780 
   7781       if (map->p_type == PT_LOAD
   7782 	  && (ibfd->flags & D_PAGED) != 0
   7783 	  && maxpagesize > 1
   7784 	  && segment->p_align > 1)
   7785 	{
   7786 	  map->p_align = segment->p_align;
   7787 	  if (segment->p_align > maxpagesize)
   7788 	    map->p_align = maxpagesize;
   7789 	  map->p_align_valid = 1;
   7790 	}
   7791 
   7792       /* If the first section in the input segment is removed, there is
   7793 	 no need to preserve segment physical address in the corresponding
   7794 	 output segment.  */
   7795       if (!first_section || first_section->output_section != NULL)
   7796 	{
   7797 	  map->p_paddr = segment->p_paddr;
   7798 	  map->p_paddr_valid = p_paddr_valid;
   7799 	}
   7800 
   7801       /* Determine if this segment contains the ELF file header
   7802 	 and if it contains the program headers themselves.  */
   7803       map->includes_filehdr = (segment->p_offset == 0
   7804 			       && segment->p_filesz >= iehdr->e_ehsize);
   7805       map->includes_phdrs = 0;
   7806 
   7807       if (!phdr_included || segment->p_type != PT_LOAD)
   7808 	{
   7809 	  map->includes_phdrs =
   7810 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7811 	     && (segment->p_offset + segment->p_filesz
   7812 		 >= ((bfd_vma) iehdr->e_phoff
   7813 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7814 
   7815 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7816 	    phdr_included = true;
   7817 	}
   7818 
   7819       if (section_count == 0)
   7820 	{
   7821 	  /* Special segments, such as the PT_PHDR segment, may contain
   7822 	     no sections, but ordinary, loadable segments should contain
   7823 	     something.  They are allowed by the ELF spec however, so only
   7824 	     a warning is produced.
   7825 	     Don't warn if an empty PT_LOAD contains the program headers.
   7826 	     There is however the valid use case of embedded systems which
   7827 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7828 	     flash memory with zeros.  No warning is shown for that case.  */
   7829 	  if (segment->p_type == PT_LOAD
   7830 	      && !map->includes_phdrs
   7831 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7832 	    /* xgettext:c-format */
   7833 	    _bfd_error_handler
   7834 	      (_("%pB: warning: empty loadable segment detected"
   7835 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7836 	       ibfd, (uint64_t) segment->p_vaddr);
   7837 
   7838 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7839 	  map->count = 0;
   7840 	  *pointer_to_map = map;
   7841 	  pointer_to_map = &map->next;
   7842 
   7843 	  continue;
   7844 	}
   7845 
   7846       /* Now scan the sections in the input BFD again and attempt
   7847 	 to add their corresponding output sections to the segment map.
   7848 	 The problem here is how to handle an output section which has
   7849 	 been moved (ie had its LMA changed).  There are four possibilities:
   7850 
   7851 	 1. None of the sections have been moved.
   7852 	    In this case we can continue to use the segment LMA from the
   7853 	    input BFD.
   7854 
   7855 	 2. All of the sections have been moved by the same amount.
   7856 	    In this case we can change the segment's LMA to match the LMA
   7857 	    of the first section.
   7858 
   7859 	 3. Some of the sections have been moved, others have not.
   7860 	    In this case those sections which have not been moved can be
   7861 	    placed in the current segment which will have to have its size,
   7862 	    and possibly its LMA changed, and a new segment or segments will
   7863 	    have to be created to contain the other sections.
   7864 
   7865 	 4. The sections have been moved, but not by the same amount.
   7866 	    In this case we can change the segment's LMA to match the LMA
   7867 	    of the first section and we will have to create a new segment
   7868 	    or segments to contain the other sections.
   7869 
   7870 	 In order to save time, we allocate an array to hold the section
   7871 	 pointers that we are interested in.  As these sections get assigned
   7872 	 to a segment, they are removed from this array.  */
   7873 
   7874       amt = section_count * sizeof (asection *);
   7875       sections = (asection **) bfd_malloc (amt);
   7876       if (sections == NULL)
   7877 	return false;
   7878 
   7879       /* Step One: Scan for segment vs section LMA conflicts.
   7880 	 Also add the sections to the section array allocated above.
   7881 	 Also add the sections to the current segment.  In the common
   7882 	 case, where the sections have not been moved, this means that
   7883 	 we have completely filled the segment, and there is nothing
   7884 	 more to do.  */
   7885       isec = 0;
   7886       matching_lma = NULL;
   7887       suggested_lma = NULL;
   7888 
   7889       for (section = first_section, j = 0;
   7890 	   section != NULL;
   7891 	   section = section->next)
   7892 	{
   7893 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
   7894 	    {
   7895 	      output_section = section->output_section;
   7896 
   7897 	      sections[j++] = section;
   7898 
   7899 	      /* The Solaris native linker always sets p_paddr to 0.
   7900 		 We try to catch that case here, and set it to the
   7901 		 correct value.  Note - some backends require that
   7902 		 p_paddr be left as zero.  */
   7903 	      if (!p_paddr_valid
   7904 		  && segment->p_vaddr != 0
   7905 		  && !bed->want_p_paddr_set_to_zero
   7906 		  && isec == 0
   7907 		  && output_section->lma != 0
   7908 		  && (align_power (segment->p_vaddr
   7909 				   + (map->includes_filehdr
   7910 				      ? iehdr->e_ehsize : 0)
   7911 				   + (map->includes_phdrs
   7912 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7913 				      : 0),
   7914 				   output_section->alignment_power * opb)
   7915 		      == (output_section->vma * opb)))
   7916 		map->p_paddr = segment->p_vaddr;
   7917 
   7918 	      /* Match up the physical address of the segment with the
   7919 		 LMA address of the output section.  */
   7920 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7921 				   0, opb, false)
   7922 		  || is_note (section, segment))
   7923 		{
   7924 		  if (matching_lma == NULL
   7925 		      || output_section->lma < matching_lma->lma)
   7926 		    matching_lma = output_section;
   7927 
   7928 		  /* We assume that if the section fits within the segment
   7929 		     then it does not overlap any other section within that
   7930 		     segment.  */
   7931 		  map->sections[isec++] = output_section;
   7932 		}
   7933 	      else if (suggested_lma == NULL)
   7934 		suggested_lma = output_section;
   7935 
   7936 	      if (j == section_count)
   7937 		break;
   7938 	    }
   7939 	}
   7940 
   7941       BFD_ASSERT (j == section_count);
   7942 
   7943       /* Step Two: Adjust the physical address of the current segment,
   7944 	 if necessary.  */
   7945       if (isec == section_count)
   7946 	{
   7947 	  /* All of the sections fitted within the segment as currently
   7948 	     specified.  This is the default case.  Add the segment to
   7949 	     the list of built segments and carry on to process the next
   7950 	     program header in the input BFD.  */
   7951 	  map->count = section_count;
   7952 	  *pointer_to_map = map;
   7953 	  pointer_to_map = &map->next;
   7954 
   7955 	  if (p_paddr_valid
   7956 	      && !bed->want_p_paddr_set_to_zero)
   7957 	    {
   7958 	      bfd_vma hdr_size = 0;
   7959 	      if (map->includes_filehdr)
   7960 		hdr_size = iehdr->e_ehsize;
   7961 	      if (map->includes_phdrs)
   7962 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7963 
   7964 	      /* Account for padding before the first section in the
   7965 		 segment.  */
   7966 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7967 				     - matching_lma->lma);
   7968 	    }
   7969 
   7970 	  free (sections);
   7971 	  continue;
   7972 	}
   7973       else
   7974 	{
   7975 	  /* Change the current segment's physical address to match
   7976 	     the LMA of the first section that fitted, or if no
   7977 	     section fitted, the first section.  */
   7978 	  if (matching_lma == NULL)
   7979 	    matching_lma = suggested_lma;
   7980 
   7981 	  map->p_paddr = matching_lma->lma * opb;
   7982 
   7983 	  /* Offset the segment physical address from the lma
   7984 	     to allow for space taken up by elf headers.  */
   7985 	  if (map->includes_phdrs)
   7986 	    {
   7987 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7988 
   7989 	      /* iehdr->e_phnum is just an estimate of the number
   7990 		 of program headers that we will need.  Make a note
   7991 		 here of the number we used and the segment we chose
   7992 		 to hold these headers, so that we can adjust the
   7993 		 offset when we know the correct value.  */
   7994 	      phdr_adjust_num = iehdr->e_phnum;
   7995 	      phdr_adjust_seg = map;
   7996 	    }
   7997 
   7998 	  if (map->includes_filehdr)
   7999 	    {
   8000 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   8001 	      map->p_paddr -= iehdr->e_ehsize;
   8002 	      /* We've subtracted off the size of headers from the
   8003 		 first section lma, but there may have been some
   8004 		 alignment padding before that section too.  Try to
   8005 		 account for that by adjusting the segment lma down to
   8006 		 the same alignment.  */
   8007 	      if (segment->p_align != 0 && segment->p_align < align)
   8008 		align = segment->p_align;
   8009 	      map->p_paddr &= -(align * opb);
   8010 	    }
   8011 	}
   8012 
   8013       /* Step Three: Loop over the sections again, this time assigning
   8014 	 those that fit to the current segment and removing them from the
   8015 	 sections array; but making sure not to leave large gaps.  Once all
   8016 	 possible sections have been assigned to the current segment it is
   8017 	 added to the list of built segments and if sections still remain
   8018 	 to be assigned, a new segment is constructed before repeating
   8019 	 the loop.  */
   8020       isec = 0;
   8021       do
   8022 	{
   8023 	  map->count = 0;
   8024 	  suggested_lma = NULL;
   8025 
   8026 	  /* Fill the current segment with sections that fit.  */
   8027 	  for (j = 0; j < section_count; j++)
   8028 	    {
   8029 	      section = sections[j];
   8030 
   8031 	      if (section == NULL)
   8032 		continue;
   8033 
   8034 	      output_section = section->output_section;
   8035 
   8036 	      BFD_ASSERT (output_section != NULL);
   8037 
   8038 	      if (is_contained_by (output_section, segment, map->p_paddr,
   8039 				   0, opb, false)
   8040 		  || is_note (section, segment))
   8041 		{
   8042 		  if (map->count == 0)
   8043 		    {
   8044 		      /* If the first section in a segment does not start at
   8045 			 the beginning of the segment, then something is
   8046 			 wrong.  */
   8047 		      if (align_power (map->p_paddr
   8048 				       + (map->includes_filehdr
   8049 					  ? iehdr->e_ehsize : 0)
   8050 				       + (map->includes_phdrs
   8051 					  ? iehdr->e_phnum * iehdr->e_phentsize
   8052 					  : 0),
   8053 				       output_section->alignment_power * opb)
   8054 			  != output_section->lma * opb)
   8055 			goto sorry;
   8056 		    }
   8057 		  else
   8058 		    {
   8059 		      asection *prev_sec;
   8060 
   8061 		      prev_sec = map->sections[map->count - 1];
   8062 
   8063 		      /* If the gap between the end of the previous section
   8064 			 and the start of this section is more than
   8065 			 maxpagesize then we need to start a new segment.  */
   8066 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   8067 				      maxpagesize)
   8068 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   8069 			  || (prev_sec->lma + prev_sec->size
   8070 			      > output_section->lma))
   8071 			{
   8072 			  if (suggested_lma == NULL)
   8073 			    suggested_lma = output_section;
   8074 
   8075 			  continue;
   8076 			}
   8077 		    }
   8078 
   8079 		  map->sections[map->count++] = output_section;
   8080 		  ++isec;
   8081 		  sections[j] = NULL;
   8082 		  if (segment->p_type == PT_LOAD)
   8083 		    section->segment_mark = true;
   8084 		}
   8085 	      else if (suggested_lma == NULL)
   8086 		suggested_lma = output_section;
   8087 	    }
   8088 
   8089 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   8090 	     be assigned to any segment - because for example they have a
   8091 	     negative size - or segments that do not contain any sections.
   8092 	     But there are also valid reasons why a segment can be empty.
   8093 	     So allow a count of zero.  */
   8094 
   8095 	  /* Add the current segment to the list of built segments.  */
   8096 	  *pointer_to_map = map;
   8097 	  pointer_to_map = &map->next;
   8098 
   8099 	  if (isec < section_count)
   8100 	    {
   8101 	      /* We still have not allocated all of the sections to
   8102 		 segments.  Create a new segment here, initialise it
   8103 		 and carry on looping.  */
   8104 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8105 	      amt += section_count * sizeof (asection *);
   8106 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8107 	      if (map == NULL)
   8108 		{
   8109 		  free (sections);
   8110 		  return false;
   8111 		}
   8112 
   8113 	      /* Initialise the fields of the segment map.  Set the physical
   8114 		 physical address to the LMA of the first section that has
   8115 		 not yet been assigned.  */
   8116 	      map->next = NULL;
   8117 	      map->p_type = segment->p_type;
   8118 	      map->p_flags = segment->p_flags;
   8119 	      map->p_flags_valid = 1;
   8120 	      map->p_paddr = suggested_lma->lma * opb;
   8121 	      map->p_paddr_valid = p_paddr_valid;
   8122 	      map->includes_filehdr = 0;
   8123 	      map->includes_phdrs = 0;
   8124 	    }
   8125 
   8126 	  continue;
   8127 	sorry:
   8128 	  bfd_set_error (bfd_error_sorry);
   8129 	  free (sections);
   8130 	  return false;
   8131 	}
   8132       while (isec < section_count);
   8133 
   8134       free (sections);
   8135     }
   8136 
   8137   elf_seg_map (obfd) = map_first;
   8138 
   8139   /* If we had to estimate the number of program headers that were
   8140      going to be needed, then check our estimate now and adjust
   8141      the offset if necessary.  */
   8142   if (phdr_adjust_seg != NULL)
   8143     {
   8144       unsigned int count;
   8145 
   8146       for (count = 0, map = map_first; map != NULL; map = map->next)
   8147 	count++;
   8148 
   8149       if (count > phdr_adjust_num)
   8150 	phdr_adjust_seg->p_paddr
   8151 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   8152 
   8153       for (map = map_first; map != NULL; map = map->next)
   8154 	if (map->p_type == PT_PHDR)
   8155 	  {
   8156 	    bfd_vma adjust
   8157 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   8158 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   8159 	    break;
   8160 	  }
   8161     }
   8162 
   8163 #undef IS_SOLARIS_PT_INTERP
   8164 #undef IS_SECTION_IN_INPUT_SEGMENT
   8165 #undef INCLUDE_SECTION_IN_SEGMENT
   8166 #undef SEGMENT_AFTER_SEGMENT
   8167 #undef SEGMENT_OVERLAPS
   8168   return true;
   8169 }
   8170 
   8171 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   8172 
   8173 static bool
   8174 elf_is_p_align_valid (bfd *abfd)
   8175 {
   8176   unsigned int i;
   8177   Elf_Internal_Phdr *segment;
   8178   unsigned int num_segments;
   8179   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8180   bfd_size_type maxpagesize = bed->maxpagesize;
   8181   bfd_size_type p_align = bed->p_align;
   8182 
   8183   /* Return true if the default p_align value isn't set or the maximum
   8184      page size is the same as the minimum page size.  */
   8185   if (p_align == 0 || maxpagesize == bed->minpagesize)
   8186     return true;
   8187 
   8188   /* When the default p_align value is set, p_align may be set to the
   8189      default p_align value while segments are aligned to the maximum
   8190      page size.  In this case, the input p_align will be ignored and
   8191      the maximum page size will be used to align the output segments.  */
   8192   segment = elf_tdata (abfd)->phdr;
   8193   num_segments = elf_elfheader (abfd)->e_phnum;
   8194   for (i = 0; i < num_segments; i++, segment++)
   8195     if (segment->p_type == PT_LOAD
   8196 	&& (segment->p_align != p_align
   8197 	    || vma_page_aligned_bias (segment->p_vaddr,
   8198 				      segment->p_offset,
   8199 				      maxpagesize) != 0))
   8200       return true;
   8201 
   8202   return false;
   8203 }
   8204 
   8205 /* Copy ELF program header information.  */
   8206 
   8207 static bool
   8208 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   8209 {
   8210   Elf_Internal_Ehdr *iehdr;
   8211   struct elf_segment_map *map;
   8212   struct elf_segment_map *map_first;
   8213   struct elf_segment_map **pointer_to_map;
   8214   Elf_Internal_Phdr *segment;
   8215   unsigned int i;
   8216   unsigned int num_segments;
   8217   bool phdr_included = false;
   8218   bool p_paddr_valid;
   8219   bool p_palign_valid;
   8220   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   8221 
   8222   iehdr = elf_elfheader (ibfd);
   8223 
   8224   map_first = NULL;
   8225   pointer_to_map = &map_first;
   8226 
   8227   /* If all the segment p_paddr fields are zero, don't set
   8228      map->p_paddr_valid.  */
   8229   p_paddr_valid = false;
   8230   num_segments = elf_elfheader (ibfd)->e_phnum;
   8231   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8232        i < num_segments;
   8233        i++, segment++)
   8234     if (segment->p_paddr != 0)
   8235       {
   8236 	p_paddr_valid = true;
   8237 	break;
   8238       }
   8239 
   8240   p_palign_valid = elf_is_p_align_valid (ibfd);
   8241 
   8242   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8243        i < num_segments;
   8244        i++, segment++)
   8245     {
   8246       asection *section;
   8247       unsigned int section_count;
   8248       size_t amt;
   8249       Elf_Internal_Shdr *this_hdr;
   8250       asection *first_section = NULL;
   8251       asection *lowest_section;
   8252 
   8253       /* Compute how many sections are in this segment.  */
   8254       for (section = ibfd->sections, section_count = 0;
   8255 	   section != NULL;
   8256 	   section = section->next)
   8257 	{
   8258 	  this_hdr = &(elf_section_data(section)->this_hdr);
   8259 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8260 	    {
   8261 	      if (first_section == NULL)
   8262 		first_section = section;
   8263 	      section_count++;
   8264 	    }
   8265 	}
   8266 
   8267       /* Allocate a segment map big enough to contain
   8268 	 all of the sections we have selected.  */
   8269       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8270       amt += section_count * sizeof (asection *);
   8271       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8272       if (map == NULL)
   8273 	return false;
   8274 
   8275       /* Initialize the fields of the output segment map with the
   8276 	 input segment.  */
   8277       map->next = NULL;
   8278       map->p_type = segment->p_type;
   8279       map->p_flags = segment->p_flags;
   8280       map->p_flags_valid = 1;
   8281       map->p_paddr = segment->p_paddr;
   8282       map->p_paddr_valid = p_paddr_valid;
   8283       map->p_align = segment->p_align;
   8284       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   8285       map->p_align_valid = (map->p_type == PT_GNU_STACK
   8286 			    || p_palign_valid);
   8287       map->p_vaddr_offset = 0;
   8288 
   8289       if (map->p_type == PT_GNU_RELRO
   8290 	  || map->p_type == PT_GNU_STACK)
   8291 	{
   8292 	  /* The PT_GNU_RELRO segment may contain the first a few
   8293 	     bytes in the .got.plt section even if the whole .got.plt
   8294 	     section isn't in the PT_GNU_RELRO segment.  We won't
   8295 	     change the size of the PT_GNU_RELRO segment.
   8296 	     Similarly, PT_GNU_STACK size is significant on uclinux
   8297 	     systems.    */
   8298 	  map->p_size = segment->p_memsz;
   8299 	  map->p_size_valid = 1;
   8300 	}
   8301 
   8302       /* Determine if this segment contains the ELF file header
   8303 	 and if it contains the program headers themselves.  */
   8304       map->includes_filehdr = (segment->p_offset == 0
   8305 			       && segment->p_filesz >= iehdr->e_ehsize);
   8306 
   8307       map->includes_phdrs = 0;
   8308       if (! phdr_included || segment->p_type != PT_LOAD)
   8309 	{
   8310 	  map->includes_phdrs =
   8311 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   8312 	     && (segment->p_offset + segment->p_filesz
   8313 		 >= ((bfd_vma) iehdr->e_phoff
   8314 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   8315 
   8316 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   8317 	    phdr_included = true;
   8318 	}
   8319 
   8320       lowest_section = NULL;
   8321       if (section_count != 0)
   8322 	{
   8323 	  unsigned int isec = 0;
   8324 
   8325 	  for (section = first_section;
   8326 	       section != NULL;
   8327 	       section = section->next)
   8328 	    {
   8329 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8330 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8331 		{
   8332 		  map->sections[isec++] = section->output_section;
   8333 		  if ((section->flags & SEC_ALLOC) != 0)
   8334 		    {
   8335 		      bfd_vma seg_off;
   8336 
   8337 		      if (lowest_section == NULL
   8338 			  || section->lma < lowest_section->lma)
   8339 			lowest_section = section;
   8340 
   8341 		      /* Section lmas are set up from PT_LOAD header
   8342 			 p_paddr in _bfd_elf_make_section_from_shdr.
   8343 			 If this header has a p_paddr that disagrees
   8344 			 with the section lma, flag the p_paddr as
   8345 			 invalid.  */
   8346 		      if ((section->flags & SEC_LOAD) != 0)
   8347 			seg_off = this_hdr->sh_offset - segment->p_offset;
   8348 		      else
   8349 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   8350 		      if (section->lma * opb - segment->p_paddr != seg_off)
   8351 			map->p_paddr_valid = false;
   8352 		    }
   8353 		  if (isec == section_count)
   8354 		    break;
   8355 		}
   8356 	    }
   8357 	}
   8358 
   8359       if (section_count == 0)
   8360 	map->p_vaddr_offset = segment->p_vaddr / opb;
   8361       else if (map->p_paddr_valid)
   8362 	{
   8363 	  /* Account for padding before the first section in the segment.  */
   8364 	  bfd_vma hdr_size = 0;
   8365 	  if (map->includes_filehdr)
   8366 	    hdr_size = iehdr->e_ehsize;
   8367 	  if (map->includes_phdrs)
   8368 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   8369 
   8370 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   8371 				 - (lowest_section ? lowest_section->lma : 0));
   8372 	}
   8373 
   8374       map->count = section_count;
   8375       *pointer_to_map = map;
   8376       pointer_to_map = &map->next;
   8377     }
   8378 
   8379   elf_seg_map (obfd) = map_first;
   8380   return true;
   8381 }
   8382 
   8383 /* Copy private BFD data.  This copies or rewrites ELF program header
   8384    information.  */
   8385 
   8386 static bool
   8387 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   8388 {
   8389   bfd_vma maxpagesize;
   8390 
   8391   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8392       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8393     return true;
   8394 
   8395   if (elf_tdata (ibfd)->phdr == NULL)
   8396     return true;
   8397 
   8398   if (ibfd->xvec == obfd->xvec)
   8399     {
   8400       /* Check to see if any sections in the input BFD
   8401 	 covered by ELF program header have changed.  */
   8402       Elf_Internal_Phdr *segment;
   8403       asection *section, *osec;
   8404       unsigned int i, num_segments;
   8405       Elf_Internal_Shdr *this_hdr;
   8406       const struct elf_backend_data *bed;
   8407 
   8408       bed = get_elf_backend_data (ibfd);
   8409 
   8410       /* Regenerate the segment map if p_paddr is set to 0.  */
   8411       if (bed->want_p_paddr_set_to_zero)
   8412 	goto rewrite;
   8413 
   8414       /* Initialize the segment mark field.  */
   8415       for (section = obfd->sections; section != NULL;
   8416 	   section = section->next)
   8417 	section->segment_mark = false;
   8418 
   8419       num_segments = elf_elfheader (ibfd)->e_phnum;
   8420       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8421 	   i < num_segments;
   8422 	   i++, segment++)
   8423 	{
   8424 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   8425 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   8426 	     which severly confuses things, so always regenerate the segment
   8427 	     map in this case.  */
   8428 	  if (segment->p_paddr == 0
   8429 	      && segment->p_memsz == 0
   8430 	      && (segment->p_type == PT_INTERP
   8431 		  || segment->p_type == PT_DYNAMIC))
   8432 	    goto rewrite;
   8433 
   8434 	  for (section = ibfd->sections;
   8435 	       section != NULL; section = section->next)
   8436 	    {
   8437 	      /* We mark the output section so that we know it comes
   8438 		 from the input BFD.  */
   8439 	      osec = section->output_section;
   8440 	      if (osec)
   8441 		osec->segment_mark = true;
   8442 
   8443 	      /* Check if this section is covered by the segment.  */
   8444 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8445 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8446 		{
   8447 		  /* FIXME: Check if its output section is changed or
   8448 		     removed.  What else do we need to check?  */
   8449 		  if (osec == NULL
   8450 		      || section->flags != osec->flags
   8451 		      || section->lma != osec->lma
   8452 		      || section->vma != osec->vma
   8453 		      || section->size != osec->size
   8454 		      || section->rawsize != osec->rawsize
   8455 		      || section->alignment_power != osec->alignment_power)
   8456 		    goto rewrite;
   8457 		}
   8458 	    }
   8459 	}
   8460 
   8461       /* Check to see if any output section do not come from the
   8462 	 input BFD.  */
   8463       for (section = obfd->sections; section != NULL;
   8464 	   section = section->next)
   8465 	{
   8466 	  if (!section->segment_mark)
   8467 	    goto rewrite;
   8468 	  else
   8469 	    section->segment_mark = false;
   8470 	}
   8471 
   8472       return copy_elf_program_header (ibfd, obfd);
   8473     }
   8474 
   8475  rewrite:
   8476   maxpagesize = 0;
   8477   if (ibfd->xvec == obfd->xvec)
   8478     {
   8479       /* When rewriting program header, set the output maxpagesize to
   8480 	 the maximum alignment of input PT_LOAD segments.  */
   8481       Elf_Internal_Phdr *segment;
   8482       unsigned int i;
   8483       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   8484 
   8485       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8486 	   i < num_segments;
   8487 	   i++, segment++)
   8488 	if (segment->p_type == PT_LOAD
   8489 	    && maxpagesize < segment->p_align)
   8490 	  {
   8491 	    /* PR 17512: file: f17299af.  */
   8492 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   8493 	      /* xgettext:c-format */
   8494 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   8495 				    PRIx64 " is too large"),
   8496 				  ibfd, (uint64_t) segment->p_align);
   8497 	    else
   8498 	      maxpagesize = segment->p_align;
   8499 	  }
   8500     }
   8501   if (maxpagesize == 0)
   8502     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   8503 
   8504   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   8505 }
   8506 
   8507 /* Initialize private output section information from input section.  */
   8508 
   8509 bool
   8510 _bfd_elf_init_private_section_data (bfd *ibfd,
   8511 				    asection *isec,
   8512 				    bfd *obfd,
   8513 				    asection *osec,
   8514 				    struct bfd_link_info *link_info)
   8515 
   8516 {
   8517   Elf_Internal_Shdr *ihdr, *ohdr;
   8518   bool final_link = (link_info != NULL
   8519 		     && !bfd_link_relocatable (link_info));
   8520 
   8521   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8522       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8523     return true;
   8524 
   8525   BFD_ASSERT (elf_section_data (osec) != NULL);
   8526 
   8527   /* If this is a known ABI section, ELF section type and flags may
   8528      have been set up when OSEC was created.  For normal sections we
   8529      allow the user to override the type and flags other than
   8530      SHF_MASKOS and SHF_MASKPROC.  */
   8531   if (elf_section_type (osec) == SHT_PROGBITS
   8532       || elf_section_type (osec) == SHT_NOTE
   8533       || elf_section_type (osec) == SHT_NOBITS)
   8534     elf_section_type (osec) = SHT_NULL;
   8535   /* For objcopy and relocatable link, copy the ELF section type from
   8536      the input file if the BFD section flags are the same.  (If they
   8537      are different the user may be doing something like
   8538      "objcopy --set-section-flags .text=alloc,data".)  For a final
   8539      link allow some flags that the linker clears to differ.  */
   8540   if (elf_section_type (osec) == SHT_NULL
   8541       && (osec->flags == isec->flags
   8542 	  || (final_link
   8543 	      && ((osec->flags ^ isec->flags)
   8544 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   8545     elf_section_type (osec) = elf_section_type (isec);
   8546 
   8547   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   8548   elf_section_flags (osec) = (elf_section_flags (isec)
   8549 			      & (SHF_MASKOS | SHF_MASKPROC));
   8550 
   8551   /* Copy sh_info from input for mbind section.  */
   8552   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   8553       && elf_section_flags (isec) & SHF_GNU_MBIND)
   8554     elf_section_data (osec)->this_hdr.sh_info
   8555       = elf_section_data (isec)->this_hdr.sh_info;
   8556 
   8557   /* Set things up for objcopy and relocatable link.  The output
   8558      SHT_GROUP section will have its elf_next_in_group pointing back
   8559      to the input group members.  Ignore linker created group section.
   8560      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   8561   if ((link_info == NULL
   8562        || !link_info->resolve_section_groups)
   8563       && (elf_sec_group (isec) == NULL
   8564 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   8565     {
   8566       if (elf_section_flags (isec) & SHF_GROUP)
   8567 	elf_section_flags (osec) |= SHF_GROUP;
   8568       elf_next_in_group (osec) = elf_next_in_group (isec);
   8569       elf_section_data (osec)->group = elf_section_data (isec)->group;
   8570     }
   8571 
   8572   /* If not decompress, preserve SHF_COMPRESSED.  */
   8573   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   8574     elf_section_flags (osec) |= (elf_section_flags (isec)
   8575 				 & SHF_COMPRESSED);
   8576 
   8577   ihdr = &elf_section_data (isec)->this_hdr;
   8578 
   8579   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   8580      don't use the output section of the linked-to section since it
   8581      may be NULL at this point.  */
   8582   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   8583     {
   8584       ohdr = &elf_section_data (osec)->this_hdr;
   8585       ohdr->sh_flags |= SHF_LINK_ORDER;
   8586       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   8587     }
   8588 
   8589   osec->use_rela_p = isec->use_rela_p;
   8590 
   8591   return true;
   8592 }
   8593 
   8594 /* Copy private section information.  This copies over the entsize
   8595    field, and sometimes the info field.  */
   8596 
   8597 bool
   8598 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8599 				    asection *isec,
   8600 				    bfd *obfd,
   8601 				    asection *osec)
   8602 {
   8603   Elf_Internal_Shdr *ihdr, *ohdr;
   8604 
   8605   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8606       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8607     return true;
   8608 
   8609   ihdr = &elf_section_data (isec)->this_hdr;
   8610   ohdr = &elf_section_data (osec)->this_hdr;
   8611 
   8612   ohdr->sh_entsize = ihdr->sh_entsize;
   8613 
   8614   if (ihdr->sh_type == SHT_SYMTAB
   8615       || ihdr->sh_type == SHT_DYNSYM
   8616       || ihdr->sh_type == SHT_GNU_verneed
   8617       || ihdr->sh_type == SHT_GNU_verdef)
   8618     ohdr->sh_info = ihdr->sh_info;
   8619 
   8620   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   8621 					     NULL);
   8622 }
   8623 
   8624 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8625    necessary if we are removing either the SHT_GROUP section or any of
   8626    the group member sections.  DISCARDED is the value that a section's
   8627    output_section has if the section will be discarded, NULL when this
   8628    function is called from objcopy, bfd_abs_section_ptr when called
   8629    from the linker.  */
   8630 
   8631 bool
   8632 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8633 {
   8634   asection *isec;
   8635 
   8636   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8637     if (elf_section_type (isec) == SHT_GROUP)
   8638       {
   8639 	asection *first = elf_next_in_group (isec);
   8640 	asection *s = first;
   8641 	bfd_size_type removed = 0;
   8642 
   8643 	while (s != NULL)
   8644 	  {
   8645 	    /* If this member section is being output but the
   8646 	       SHT_GROUP section is not, then clear the group info
   8647 	       set up by _bfd_elf_copy_private_section_data.  */
   8648 	    if (s->output_section != discarded
   8649 		&& isec->output_section == discarded)
   8650 	      {
   8651 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8652 		elf_group_name (s->output_section) = NULL;
   8653 	      }
   8654 	    else
   8655 	      {
   8656 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8657 		if (s->output_section == discarded
   8658 		    && isec->output_section != discarded)
   8659 		  {
   8660 		    /* Conversely, if the member section is not being
   8661 		       output but the SHT_GROUP section is, then adjust
   8662 		       its size.  */
   8663 		    removed += 4;
   8664 		    if (elf_sec->rel.hdr != NULL
   8665 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8666 		      removed += 4;
   8667 		    if (elf_sec->rela.hdr != NULL
   8668 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8669 		      removed += 4;
   8670 		  }
   8671 		else
   8672 		  {
   8673 		    /* Also adjust for zero-sized relocation member
   8674 		       section.  */
   8675 		    if (elf_sec->rel.hdr != NULL
   8676 			&& elf_sec->rel.hdr->sh_size == 0)
   8677 		      removed += 4;
   8678 		    if (elf_sec->rela.hdr != NULL
   8679 			&& elf_sec->rela.hdr->sh_size == 0)
   8680 		      removed += 4;
   8681 		  }
   8682 	      }
   8683 	    s = elf_next_in_group (s);
   8684 	    if (s == first)
   8685 	      break;
   8686 	  }
   8687 	if (removed != 0)
   8688 	  {
   8689 	    if (discarded != NULL)
   8690 	      {
   8691 		/* If we've been called for ld -r, then we need to
   8692 		   adjust the input section size.  */
   8693 		if (isec->rawsize == 0)
   8694 		  isec->rawsize = isec->size;
   8695 		isec->size = isec->rawsize - removed;
   8696 		if (isec->size <= 4)
   8697 		  {
   8698 		    isec->size = 0;
   8699 		    isec->flags |= SEC_EXCLUDE;
   8700 		  }
   8701 	      }
   8702 	    else if (isec->output_section != NULL)
   8703 	      {
   8704 		/* Adjust the output section size when called from
   8705 		   objcopy. */
   8706 		isec->output_section->size -= removed;
   8707 		if (isec->output_section->size <= 4)
   8708 		  {
   8709 		    isec->output_section->size = 0;
   8710 		    isec->output_section->flags |= SEC_EXCLUDE;
   8711 		  }
   8712 	      }
   8713 	  }
   8714       }
   8715 
   8716   return true;
   8717 }
   8718 
   8719 /* Copy private header information.  */
   8720 
   8721 bool
   8722 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8723 {
   8724   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8725       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8726     return true;
   8727 
   8728   /* Copy over private BFD data if it has not already been copied.
   8729      This must be done here, rather than in the copy_private_bfd_data
   8730      entry point, because the latter is called after the section
   8731      contents have been set, which means that the program headers have
   8732      already been worked out.  */
   8733   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8734     {
   8735       if (! copy_private_bfd_data (ibfd, obfd))
   8736 	return false;
   8737     }
   8738 
   8739   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8740 }
   8741 
   8742 /* Copy private symbol information.  If this symbol is in a section
   8743    which we did not map into a BFD section, try to map the section
   8744    index correctly.  We use special macro definitions for the mapped
   8745    section indices; these definitions are interpreted by the
   8746    swap_out_syms function.  */
   8747 
   8748 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8749 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8750 #define MAP_STRTAB    (SHN_HIOS + 3)
   8751 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8752 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8753 
   8754 bool
   8755 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8756 				   asymbol *isymarg,
   8757 				   bfd *obfd,
   8758 				   asymbol *osymarg)
   8759 {
   8760   elf_symbol_type *isym, *osym;
   8761 
   8762   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8763       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8764     return true;
   8765 
   8766   isym = elf_symbol_from (isymarg);
   8767   osym = elf_symbol_from (osymarg);
   8768 
   8769   if (isym != NULL
   8770       && isym->internal_elf_sym.st_shndx != 0
   8771       && osym != NULL
   8772       && bfd_is_abs_section (isym->symbol.section))
   8773     {
   8774       unsigned int shndx;
   8775 
   8776       shndx = isym->internal_elf_sym.st_shndx;
   8777       if (shndx == elf_onesymtab (ibfd))
   8778 	shndx = MAP_ONESYMTAB;
   8779       else if (shndx == elf_dynsymtab (ibfd))
   8780 	shndx = MAP_DYNSYMTAB;
   8781       else if (shndx == elf_strtab_sec (ibfd))
   8782 	shndx = MAP_STRTAB;
   8783       else if (shndx == elf_shstrtab_sec (ibfd))
   8784 	shndx = MAP_SHSTRTAB;
   8785       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8786 	shndx = MAP_SYM_SHNDX;
   8787       osym->internal_elf_sym.st_shndx = shndx;
   8788     }
   8789 
   8790   return true;
   8791 }
   8792 
   8793 /* Swap out the symbols.  */
   8794 
   8795 static bool
   8796 swap_out_syms (bfd *abfd,
   8797 	       struct elf_strtab_hash **sttp,
   8798 	       int relocatable_p,
   8799 	       struct bfd_link_info *info)
   8800 {
   8801   const struct elf_backend_data *bed;
   8802   unsigned int symcount;
   8803   asymbol **syms;
   8804   struct elf_strtab_hash *stt;
   8805   Elf_Internal_Shdr *symtab_hdr;
   8806   Elf_Internal_Shdr *symtab_shndx_hdr;
   8807   Elf_Internal_Shdr *symstrtab_hdr;
   8808   struct elf_sym_strtab *symstrtab;
   8809   bfd_byte *outbound_syms;
   8810   bfd_byte *outbound_shndx;
   8811   unsigned long outbound_syms_index;
   8812   unsigned int idx;
   8813   unsigned int num_locals;
   8814   size_t amt;
   8815   bool name_local_sections;
   8816 
   8817   if (!elf_map_symbols (abfd, &num_locals))
   8818     return false;
   8819 
   8820   /* Dump out the symtabs.  */
   8821   stt = _bfd_elf_strtab_init ();
   8822   if (stt == NULL)
   8823     return false;
   8824 
   8825   bed = get_elf_backend_data (abfd);
   8826   symcount = bfd_get_symcount (abfd);
   8827   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8828   symtab_hdr->sh_type = SHT_SYMTAB;
   8829   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8830   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8831   symtab_hdr->sh_info = num_locals + 1;
   8832   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8833 
   8834   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8835   symstrtab_hdr->sh_type = SHT_STRTAB;
   8836 
   8837   /* Allocate buffer to swap out the .strtab section.  */
   8838   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8839       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8840     {
   8841       bfd_set_error (bfd_error_no_memory);
   8842       _bfd_elf_strtab_free (stt);
   8843       return false;
   8844     }
   8845 
   8846   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8847       || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
   8848     {
   8849     error_no_mem:
   8850       bfd_set_error (bfd_error_no_memory);
   8851     error_return:
   8852       free (symstrtab);
   8853       _bfd_elf_strtab_free (stt);
   8854       return false;
   8855     }
   8856   symtab_hdr->contents = outbound_syms;
   8857   outbound_syms_index = 0;
   8858 
   8859   outbound_shndx = NULL;
   8860 
   8861   if (elf_symtab_shndx_list (abfd))
   8862     {
   8863       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8864       if (symtab_shndx_hdr->sh_name != 0)
   8865 	{
   8866 	  if (_bfd_mul_overflow (symcount + 1,
   8867 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8868 	    goto error_no_mem;
   8869 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8870 	  if (outbound_shndx == NULL)
   8871 	    goto error_return;
   8872 
   8873 	  symtab_shndx_hdr->contents = outbound_shndx;
   8874 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8875 	  symtab_shndx_hdr->sh_size = amt;
   8876 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8877 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8878 	}
   8879       /* FIXME: What about any other headers in the list ?  */
   8880     }
   8881 
   8882   /* Now generate the data (for "contents").  */
   8883   {
   8884     /* Fill in zeroth symbol and swap it out.  */
   8885     Elf_Internal_Sym sym;
   8886     sym.st_name = 0;
   8887     sym.st_value = 0;
   8888     sym.st_size = 0;
   8889     sym.st_info = 0;
   8890     sym.st_other = 0;
   8891     sym.st_shndx = SHN_UNDEF;
   8892     sym.st_target_internal = 0;
   8893     symstrtab[0].sym = sym;
   8894     symstrtab[0].dest_index = outbound_syms_index;
   8895     outbound_syms_index++;
   8896   }
   8897 
   8898   name_local_sections
   8899     = (bed->elf_backend_name_local_section_symbols
   8900        && bed->elf_backend_name_local_section_symbols (abfd));
   8901 
   8902   syms = bfd_get_outsymbols (abfd);
   8903   for (idx = 0; idx < symcount;)
   8904     {
   8905       Elf_Internal_Sym sym;
   8906       bfd_vma value = syms[idx]->value;
   8907       elf_symbol_type *type_ptr;
   8908       flagword flags = syms[idx]->flags;
   8909       int type;
   8910 
   8911       if (!name_local_sections
   8912 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8913 	{
   8914 	  /* Local section symbols have no name.  */
   8915 	  sym.st_name = (unsigned long) -1;
   8916 	}
   8917       else
   8918 	{
   8919 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8920 	     to get the final offset for st_name.  */
   8921 	  sym.st_name
   8922 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   8923 						   false);
   8924 	  if (sym.st_name == (unsigned long) -1)
   8925 	    goto error_return;
   8926 	}
   8927 
   8928       type_ptr = elf_symbol_from (syms[idx]);
   8929 
   8930       if ((flags & BSF_SECTION_SYM) == 0
   8931 	  && bfd_is_com_section (syms[idx]->section))
   8932 	{
   8933 	  /* ELF common symbols put the alignment into the `value' field,
   8934 	     and the size into the `size' field.  This is backwards from
   8935 	     how BFD handles it, so reverse it here.  */
   8936 	  sym.st_size = value;
   8937 	  if (type_ptr == NULL
   8938 	      || type_ptr->internal_elf_sym.st_value == 0)
   8939 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8940 	  else
   8941 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8942 	  sym.st_shndx = _bfd_elf_section_from_bfd_section
   8943 	    (abfd, syms[idx]->section);
   8944 	}
   8945       else
   8946 	{
   8947 	  asection *sec = syms[idx]->section;
   8948 	  unsigned int shndx;
   8949 
   8950 	  if (sec->output_section)
   8951 	    {
   8952 	      value += sec->output_offset;
   8953 	      sec = sec->output_section;
   8954 	    }
   8955 
   8956 	  /* Don't add in the section vma for relocatable output.  */
   8957 	  if (! relocatable_p)
   8958 	    value += sec->vma;
   8959 	  sym.st_value = value;
   8960 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8961 
   8962 	  if (bfd_is_abs_section (sec)
   8963 	      && type_ptr != NULL
   8964 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8965 	    {
   8966 	      /* This symbol is in a real ELF section which we did
   8967 		 not create as a BFD section.  Undo the mapping done
   8968 		 by copy_private_symbol_data.  */
   8969 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8970 	      switch (shndx)
   8971 		{
   8972 		case MAP_ONESYMTAB:
   8973 		  shndx = elf_onesymtab (abfd);
   8974 		  break;
   8975 		case MAP_DYNSYMTAB:
   8976 		  shndx = elf_dynsymtab (abfd);
   8977 		  break;
   8978 		case MAP_STRTAB:
   8979 		  shndx = elf_strtab_sec (abfd);
   8980 		  break;
   8981 		case MAP_SHSTRTAB:
   8982 		  shndx = elf_shstrtab_sec (abfd);
   8983 		  break;
   8984 		case MAP_SYM_SHNDX:
   8985 		  if (elf_symtab_shndx_list (abfd))
   8986 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8987 		  break;
   8988 		case SHN_COMMON:
   8989 		case SHN_ABS:
   8990 		  shndx = SHN_ABS;
   8991 		  break;
   8992 		default:
   8993 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8994 		    {
   8995 		      if (bed->symbol_section_index)
   8996 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8997 		      /* Otherwise just leave the index alone.  */
   8998 		    }
   8999 		  else
   9000 		    {
   9001 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   9002 			_bfd_error_handler (_("%pB: \
   9003 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   9004 					  abfd, shndx);
   9005 		      shndx = SHN_ABS;
   9006 		    }
   9007 		  break;
   9008 		}
   9009 	    }
   9010 	  else
   9011 	    {
   9012 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   9013 
   9014 	      if (shndx == SHN_BAD)
   9015 		{
   9016 		  asection *sec2;
   9017 
   9018 		  /* Writing this would be a hell of a lot easier if
   9019 		     we had some decent documentation on bfd, and
   9020 		     knew what to expect of the library, and what to
   9021 		     demand of applications.  For example, it
   9022 		     appears that `objcopy' might not set the
   9023 		     section of a symbol to be a section that is
   9024 		     actually in the output file.  */
   9025 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   9026 		  if (sec2 != NULL)
   9027 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   9028 		  if (shndx == SHN_BAD)
   9029 		    {
   9030 		      /* xgettext:c-format */
   9031 		      _bfd_error_handler
   9032 			(_("unable to find equivalent output section"
   9033 			   " for symbol '%s' from section '%s'"),
   9034 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   9035 			 sec->name);
   9036 		      bfd_set_error (bfd_error_invalid_operation);
   9037 		      goto error_return;
   9038 		    }
   9039 		}
   9040 	    }
   9041 
   9042 	  sym.st_shndx = shndx;
   9043 	}
   9044 
   9045       if ((flags & BSF_THREAD_LOCAL) != 0)
   9046 	type = STT_TLS;
   9047       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   9048 	type = STT_GNU_IFUNC;
   9049       else if ((flags & BSF_FUNCTION) != 0)
   9050 	type = STT_FUNC;
   9051       else if ((flags & BSF_OBJECT) != 0)
   9052 	type = STT_OBJECT;
   9053       else if ((flags & BSF_RELC) != 0)
   9054 	type = STT_RELC;
   9055       else if ((flags & BSF_SRELC) != 0)
   9056 	type = STT_SRELC;
   9057       else
   9058 	type = STT_NOTYPE;
   9059 
   9060       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   9061 	type = STT_TLS;
   9062 
   9063       /* Processor-specific types.  */
   9064       if (type_ptr != NULL
   9065 	  && bed->elf_backend_get_symbol_type)
   9066 	type = ((*bed->elf_backend_get_symbol_type)
   9067 		(&type_ptr->internal_elf_sym, type));
   9068 
   9069       if (flags & BSF_SECTION_SYM)
   9070 	{
   9071 	  if (flags & BSF_GLOBAL)
   9072 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   9073 	  else
   9074 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   9075 	}
   9076       else if (bfd_is_com_section (syms[idx]->section))
   9077 	{
   9078 	  if (type != STT_TLS)
   9079 	    {
   9080 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   9081 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   9082 			? STT_COMMON : STT_OBJECT);
   9083 	      else
   9084 		type = ((flags & BSF_ELF_COMMON) != 0
   9085 			? STT_COMMON : STT_OBJECT);
   9086 	    }
   9087 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   9088 	}
   9089       else if (bfd_is_und_section (syms[idx]->section))
   9090 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   9091 				    ? STB_WEAK
   9092 				    : STB_GLOBAL),
   9093 				   type);
   9094       else if (flags & BSF_FILE)
   9095 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   9096       else
   9097 	{
   9098 	  int bind = STB_LOCAL;
   9099 
   9100 	  if (flags & BSF_LOCAL)
   9101 	    bind = STB_LOCAL;
   9102 	  else if (flags & BSF_GNU_UNIQUE)
   9103 	    bind = STB_GNU_UNIQUE;
   9104 	  else if (flags & BSF_WEAK)
   9105 	    bind = STB_WEAK;
   9106 	  else if (flags & BSF_GLOBAL)
   9107 	    bind = STB_GLOBAL;
   9108 
   9109 	  sym.st_info = ELF_ST_INFO (bind, type);
   9110 	}
   9111 
   9112       if (type_ptr != NULL)
   9113 	{
   9114 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   9115 	  sym.st_target_internal
   9116 	    = type_ptr->internal_elf_sym.st_target_internal;
   9117 	}
   9118       else
   9119 	{
   9120 	  sym.st_other = 0;
   9121 	  sym.st_target_internal = 0;
   9122 	}
   9123 
   9124       idx++;
   9125       symstrtab[idx].sym = sym;
   9126       symstrtab[idx].dest_index = outbound_syms_index;
   9127 
   9128       outbound_syms_index++;
   9129     }
   9130 
   9131   /* Finalize the .strtab section.  */
   9132   _bfd_elf_strtab_finalize (stt);
   9133 
   9134   /* Swap out the .strtab section.  */
   9135   for (idx = 0; idx <= symcount; idx++)
   9136     {
   9137       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   9138       if (elfsym->sym.st_name == (unsigned long) -1)
   9139 	elfsym->sym.st_name = 0;
   9140       else
   9141 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   9142 						      elfsym->sym.st_name);
   9143       if (info && info->callbacks->ctf_new_symbol)
   9144 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   9145 					 &elfsym->sym);
   9146 
   9147       /* Inform the linker of the addition of this symbol.  */
   9148 
   9149       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   9150 			       (outbound_syms
   9151 				+ (elfsym->dest_index
   9152 				   * bed->s->sizeof_sym)),
   9153 			       NPTR_ADD (outbound_shndx,
   9154 					 (elfsym->dest_index
   9155 					  * sizeof (Elf_External_Sym_Shndx))));
   9156     }
   9157   free (symstrtab);
   9158 
   9159   *sttp = stt;
   9160   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   9161   symstrtab_hdr->sh_type = SHT_STRTAB;
   9162   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   9163   symstrtab_hdr->sh_addr = 0;
   9164   symstrtab_hdr->sh_entsize = 0;
   9165   symstrtab_hdr->sh_link = 0;
   9166   symstrtab_hdr->sh_info = 0;
   9167   symstrtab_hdr->sh_addralign = 1;
   9168 
   9169   return true;
   9170 }
   9171 
   9172 /* Return the number of bytes required to hold the symtab vector.
   9173 
   9174    Note that we base it on the count plus 1, since we will null terminate
   9175    the vector allocated based on this size.  However, the ELF symbol table
   9176    always has a dummy entry as symbol #0, so it ends up even.  */
   9177 
   9178 long
   9179 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   9180 {
   9181   bfd_size_type symcount;
   9182   long symtab_size;
   9183   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   9184 
   9185   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9186   if (symcount > LONG_MAX / sizeof (asymbol *))
   9187     {
   9188       bfd_set_error (bfd_error_file_too_big);
   9189       return -1;
   9190     }
   9191   symtab_size = symcount * (sizeof (asymbol *));
   9192   if (symcount == 0)
   9193     symtab_size = sizeof (asymbol *);
   9194   else if (!bfd_write_p (abfd))
   9195     {
   9196       ufile_ptr filesize = bfd_get_file_size (abfd);
   9197 
   9198       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9199 	{
   9200 	  bfd_set_error (bfd_error_file_truncated);
   9201 	  return -1;
   9202 	}
   9203     }
   9204 
   9205   return symtab_size;
   9206 }
   9207 
   9208 long
   9209 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   9210 {
   9211   bfd_size_type symcount;
   9212   long symtab_size;
   9213   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   9214 
   9215   if (elf_dynsymtab (abfd) == 0)
   9216     {
   9217       /* Check if there is dynamic symbol table.  */
   9218       symcount = elf_tdata (abfd)->dt_symtab_count;
   9219       if (symcount)
   9220 	goto compute_symtab_size;
   9221 
   9222       bfd_set_error (bfd_error_invalid_operation);
   9223       return -1;
   9224     }
   9225 
   9226   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9227   if (symcount > LONG_MAX / sizeof (asymbol *))
   9228     {
   9229       bfd_set_error (bfd_error_file_too_big);
   9230       return -1;
   9231     }
   9232 
   9233  compute_symtab_size:
   9234   symtab_size = symcount * (sizeof (asymbol *));
   9235   if (symcount == 0)
   9236     symtab_size = sizeof (asymbol *);
   9237   else if (!bfd_write_p (abfd))
   9238     {
   9239       ufile_ptr filesize = bfd_get_file_size (abfd);
   9240 
   9241       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9242 	{
   9243 	  bfd_set_error (bfd_error_file_truncated);
   9244 	  return -1;
   9245 	}
   9246     }
   9247 
   9248   return symtab_size;
   9249 }
   9250 
   9251 long
   9252 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   9253 {
   9254   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   9255     {
   9256       /* Sanity check reloc section size.  */
   9257       ufile_ptr filesize = bfd_get_file_size (abfd);
   9258 
   9259       if (filesize != 0)
   9260 	{
   9261 	  struct bfd_elf_section_data *d = elf_section_data (asect);
   9262 	  bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
   9263 	  bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
   9264 
   9265 	  if (rel_size + rela_size > filesize
   9266 	      || rel_size + rela_size < rel_size)
   9267 	    {
   9268 	      bfd_set_error (bfd_error_file_truncated);
   9269 	      return -1;
   9270 	    }
   9271 	}
   9272     }
   9273 
   9274 #if SIZEOF_LONG == SIZEOF_INT
   9275   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   9276     {
   9277       bfd_set_error (bfd_error_file_too_big);
   9278       return -1;
   9279     }
   9280 #endif
   9281   return (asect->reloc_count + 1L) * sizeof (arelent *);
   9282 }
   9283 
   9284 /* Canonicalize the relocs.  */
   9285 
   9286 long
   9287 _bfd_elf_canonicalize_reloc (bfd *abfd,
   9288 			     sec_ptr section,
   9289 			     arelent **relptr,
   9290 			     asymbol **symbols)
   9291 {
   9292   arelent *tblptr;
   9293   unsigned int i;
   9294   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9295 
   9296   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   9297     return -1;
   9298 
   9299   tblptr = section->relocation;
   9300   for (i = 0; i < section->reloc_count; i++)
   9301     *relptr++ = tblptr++;
   9302 
   9303   *relptr = NULL;
   9304 
   9305   return section->reloc_count;
   9306 }
   9307 
   9308 long
   9309 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   9310 {
   9311   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9312   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   9313 
   9314   if (symcount >= 0)
   9315     abfd->symcount = symcount;
   9316   return symcount;
   9317 }
   9318 
   9319 long
   9320 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   9321 				      asymbol **allocation)
   9322 {
   9323   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9324   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   9325 
   9326   if (symcount >= 0)
   9327     abfd->dynsymcount = symcount;
   9328   return symcount;
   9329 }
   9330 
   9331 /* Return the size required for the dynamic reloc entries.  Any loadable
   9332    section that was actually installed in the BFD, and has type SHT_REL
   9333    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   9334    dynamic reloc section.  */
   9335 
   9336 long
   9337 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   9338 {
   9339   bfd_size_type count, ext_rel_size;
   9340   asection *s;
   9341 
   9342   if (elf_dynsymtab (abfd) == 0)
   9343     {
   9344       bfd_set_error (bfd_error_invalid_operation);
   9345       return -1;
   9346     }
   9347 
   9348   count = 1;
   9349   ext_rel_size = 0;
   9350   for (s = abfd->sections; s != NULL; s = s->next)
   9351     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9352 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9353 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9354 	&& (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9355       {
   9356 	ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
   9357 	if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
   9358 	  {
   9359 	    bfd_set_error (bfd_error_file_truncated);
   9360 	    return -1;
   9361 	  }
   9362 	count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9363 	if (count > LONG_MAX / sizeof (arelent *))
   9364 	  {
   9365 	    bfd_set_error (bfd_error_file_too_big);
   9366 	    return -1;
   9367 	  }
   9368       }
   9369   if (count > 1 && !bfd_write_p (abfd))
   9370     {
   9371       /* Sanity check reloc section sizes.  */
   9372       ufile_ptr filesize = bfd_get_file_size (abfd);
   9373       if (filesize != 0 && ext_rel_size > filesize)
   9374 	{
   9375 	  bfd_set_error (bfd_error_file_truncated);
   9376 	  return -1;
   9377 	}
   9378     }
   9379   return count * sizeof (arelent *);
   9380 }
   9381 
   9382 /* Canonicalize the dynamic relocation entries.  Note that we return the
   9383    dynamic relocations as a single block, although they are actually
   9384    associated with particular sections; the interface, which was
   9385    designed for SunOS style shared libraries, expects that there is only
   9386    one set of dynamic relocs.  Any loadable section that was actually
   9387    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   9388    dynamic symbol table, is considered to be a dynamic reloc section.  */
   9389 
   9390 long
   9391 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   9392 				     arelent **storage,
   9393 				     asymbol **syms)
   9394 {
   9395   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   9396   asection *s;
   9397   long ret;
   9398 
   9399   if (elf_dynsymtab (abfd) == 0)
   9400     {
   9401       bfd_set_error (bfd_error_invalid_operation);
   9402       return -1;
   9403     }
   9404 
   9405   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   9406   ret = 0;
   9407   for (s = abfd->sections; s != NULL; s = s->next)
   9408     {
   9409       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9410 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9411 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9412 	  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9413 	{
   9414 	  arelent *p;
   9415 	  long count, i;
   9416 
   9417 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   9418 	    return -1;
   9419 	  count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9420 	  p = s->relocation;
   9421 	  for (i = 0; i < count; i++)
   9422 	    *storage++ = p++;
   9423 	  ret += count;
   9424 	}
   9425     }
   9426 
   9427   *storage = NULL;
   9428 
   9429   return ret;
   9430 }
   9431 
   9432 /* Read in the version information.  */
   9434 
   9435 bool
   9436 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   9437 {
   9438   bfd_byte *contents = NULL;
   9439   unsigned int freeidx = 0;
   9440   size_t amt;
   9441 
   9442   if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
   9443     {
   9444       Elf_Internal_Shdr *hdr;
   9445       Elf_External_Verneed *everneed;
   9446       Elf_Internal_Verneed *iverneed;
   9447       unsigned int i;
   9448       bfd_byte *contents_end;
   9449       size_t verneed_count;
   9450       size_t verneed_size;
   9451 
   9452       if (elf_tdata (abfd)->dt_verneed != NULL)
   9453 	{
   9454 	  hdr = NULL;
   9455 	  contents = elf_tdata (abfd)->dt_verneed;
   9456 	  verneed_count = elf_tdata (abfd)->dt_verneed_count;
   9457 	  verneed_size = verneed_count * sizeof (Elf_External_Verneed);
   9458 	}
   9459       else
   9460 	{
   9461 	  hdr = &elf_tdata (abfd)->dynverref_hdr;
   9462 
   9463 	  if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   9464 	    {
   9465 	    error_return_bad_verref:
   9466 	      _bfd_error_handler
   9467 		(_("%pB: .gnu.version_r invalid entry"), abfd);
   9468 	      bfd_set_error (bfd_error_bad_value);
   9469 	    error_return_verref:
   9470 	      elf_tdata (abfd)->verref = NULL;
   9471 	      elf_tdata (abfd)->cverrefs = 0;
   9472 	      goto error_return;
   9473 	    }
   9474 
   9475 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9476 	    goto error_return_verref;
   9477 	  contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   9478 	  if (contents == NULL)
   9479 	    goto error_return_verref;
   9480 
   9481 	  verneed_size = hdr->sh_size;
   9482 	  verneed_count = hdr->sh_info;
   9483 	}
   9484 
   9485       if (_bfd_mul_overflow (verneed_count,
   9486 			     sizeof (Elf_Internal_Verneed), &amt))
   9487 	{
   9488 	  bfd_set_error (bfd_error_file_too_big);
   9489 	  goto error_return_verref;
   9490 	}
   9491       if (amt == 0)
   9492 	goto error_return_verref;
   9493       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
   9494       if (elf_tdata (abfd)->verref == NULL)
   9495 	goto error_return_verref;
   9496 
   9497       BFD_ASSERT (sizeof (Elf_External_Verneed)
   9498 		  == sizeof (Elf_External_Vernaux));
   9499       contents_end = (contents + verneed_size
   9500 		      - sizeof (Elf_External_Verneed));
   9501       everneed = (Elf_External_Verneed *) contents;
   9502       iverneed = elf_tdata (abfd)->verref;
   9503       for (i = 0; i < verneed_count; i++, iverneed++)
   9504 	{
   9505 	  Elf_External_Vernaux *evernaux;
   9506 	  Elf_Internal_Vernaux *ivernaux;
   9507 	  unsigned int j;
   9508 
   9509 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   9510 
   9511 	  iverneed->vn_bfd = abfd;
   9512 
   9513 	  if (elf_use_dt_symtab_p (abfd))
   9514 	    {
   9515 	      if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
   9516 		iverneed->vn_filename
   9517 		  = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
   9518 	      else
   9519 		iverneed->vn_filename = NULL;
   9520 	    }
   9521 	  else if (hdr == NULL)
   9522 	    goto error_return_bad_verref;
   9523 	  else
   9524 	    iverneed->vn_filename
   9525 	      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9526 						 iverneed->vn_file);
   9527 	  if (iverneed->vn_filename == NULL)
   9528 	    goto error_return_bad_verref;
   9529 
   9530 	  if (iverneed->vn_cnt == 0)
   9531 	    iverneed->vn_auxptr = NULL;
   9532 	  else
   9533 	    {
   9534 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   9535 				     sizeof (Elf_Internal_Vernaux), &amt))
   9536 		{
   9537 		  bfd_set_error (bfd_error_file_too_big);
   9538 		  goto error_return_verref;
   9539 		}
   9540 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   9541 		bfd_alloc (abfd, amt);
   9542 	      if (iverneed->vn_auxptr == NULL)
   9543 		goto error_return_verref;
   9544 	    }
   9545 
   9546 	  if (iverneed->vn_aux
   9547 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9548 	    goto error_return_bad_verref;
   9549 
   9550 	  evernaux = ((Elf_External_Vernaux *)
   9551 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   9552 	  ivernaux = iverneed->vn_auxptr;
   9553 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   9554 	    {
   9555 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   9556 
   9557 	      if (elf_use_dt_symtab_p (abfd))
   9558 		{
   9559 		  if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
   9560 		    ivernaux->vna_nodename
   9561 		      = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
   9562 		  else
   9563 		    ivernaux->vna_nodename = NULL;
   9564 		}
   9565 	      else if (hdr == NULL)
   9566 		goto error_return_bad_verref;
   9567 	      else
   9568 		ivernaux->vna_nodename
   9569 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9570 						     ivernaux->vna_name);
   9571 	      if (ivernaux->vna_nodename == NULL)
   9572 		goto error_return_bad_verref;
   9573 
   9574 	      if (ivernaux->vna_other > freeidx)
   9575 		freeidx = ivernaux->vna_other;
   9576 
   9577 	      ivernaux->vna_nextptr = NULL;
   9578 	      if (ivernaux->vna_next == 0)
   9579 		{
   9580 		  iverneed->vn_cnt = j + 1;
   9581 		  break;
   9582 		}
   9583 	      if (j + 1 < iverneed->vn_cnt)
   9584 		ivernaux->vna_nextptr = ivernaux + 1;
   9585 
   9586 	      if (ivernaux->vna_next
   9587 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   9588 		goto error_return_bad_verref;
   9589 
   9590 	      evernaux = ((Elf_External_Vernaux *)
   9591 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   9592 	    }
   9593 
   9594 	  iverneed->vn_nextref = NULL;
   9595 	  if (iverneed->vn_next == 0)
   9596 	    break;
   9597 	  if (hdr != NULL && (i + 1 < hdr->sh_info))
   9598 	    iverneed->vn_nextref = iverneed + 1;
   9599 
   9600 	  if (iverneed->vn_next
   9601 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9602 	    goto error_return_bad_verref;
   9603 
   9604 	  everneed = ((Elf_External_Verneed *)
   9605 		      ((bfd_byte *) everneed + iverneed->vn_next));
   9606 	}
   9607       elf_tdata (abfd)->cverrefs = i;
   9608 
   9609       if (contents != elf_tdata (abfd)->dt_verneed)
   9610 	free (contents);
   9611       contents = NULL;
   9612     }
   9613 
   9614   if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
   9615     {
   9616       Elf_Internal_Shdr *hdr;
   9617       Elf_External_Verdef *everdef;
   9618       Elf_Internal_Verdef *iverdef;
   9619       Elf_Internal_Verdef *iverdefarr;
   9620       Elf_Internal_Verdef iverdefmem;
   9621       unsigned int i;
   9622       unsigned int maxidx;
   9623       bfd_byte *contents_end_def, *contents_end_aux;
   9624       size_t verdef_count;
   9625       size_t verdef_size;
   9626 
   9627       if (elf_tdata (abfd)->dt_verdef != NULL)
   9628 	{
   9629 	  hdr = NULL;
   9630 	  contents = elf_tdata (abfd)->dt_verdef;
   9631 	  verdef_count = elf_tdata (abfd)->dt_verdef_count;
   9632 	  verdef_size = verdef_count * sizeof (Elf_External_Verdef);
   9633 	}
   9634       else
   9635 	{
   9636 	  hdr = &elf_tdata (abfd)->dynverdef_hdr;
   9637 
   9638 	  if (hdr->sh_size < sizeof (Elf_External_Verdef))
   9639 	    {
   9640 	    error_return_bad_verdef:
   9641 	      _bfd_error_handler
   9642 		(_("%pB: .gnu.version_d invalid entry"), abfd);
   9643 	      bfd_set_error (bfd_error_bad_value);
   9644 	    error_return_verdef:
   9645 	      elf_tdata (abfd)->verdef = NULL;
   9646 	      elf_tdata (abfd)->cverdefs = 0;
   9647 	      goto error_return;
   9648 	    }
   9649 
   9650 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9651 	    goto error_return_verdef;
   9652 	  contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   9653 	  if (contents == NULL)
   9654 	    goto error_return_verdef;
   9655 
   9656 	  BFD_ASSERT (sizeof (Elf_External_Verdef)
   9657 		      >= sizeof (Elf_External_Verdaux));
   9658 
   9659 	  verdef_count = hdr->sh_info;
   9660 	  verdef_size = hdr->sh_size;
   9661 	}
   9662 
   9663       contents_end_def = (contents + verdef_size
   9664 			  - sizeof (Elf_External_Verdef));
   9665       contents_end_aux = (contents + verdef_size
   9666 			  - sizeof (Elf_External_Verdaux));
   9667 
   9668       /* We know the number of entries in the section but not the maximum
   9669 	 index.  Therefore we have to run through all entries and find
   9670 	 the maximum.  */
   9671       everdef = (Elf_External_Verdef *) contents;
   9672       maxidx = 0;
   9673       for (i = 0; i < verdef_count; ++i)
   9674 	{
   9675 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9676 
   9677 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9678 	    goto error_return_bad_verdef;
   9679 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9680 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9681 
   9682 	  if (iverdefmem.vd_next == 0)
   9683 	    break;
   9684 
   9685 	  if (iverdefmem.vd_next
   9686 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9687 	    goto error_return_bad_verdef;
   9688 
   9689 	  everdef = ((Elf_External_Verdef *)
   9690 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9691 	}
   9692 
   9693       if (default_imported_symver)
   9694 	{
   9695 	  if (freeidx > maxidx)
   9696 	    maxidx = ++freeidx;
   9697 	  else
   9698 	    freeidx = ++maxidx;
   9699 	}
   9700       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9701 	{
   9702 	  bfd_set_error (bfd_error_file_too_big);
   9703 	  goto error_return_verdef;
   9704 	}
   9705 
   9706       if (amt == 0)
   9707 	goto error_return_verdef;
   9708       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9709       if (elf_tdata (abfd)->verdef == NULL)
   9710 	goto error_return_verdef;
   9711 
   9712       elf_tdata (abfd)->cverdefs = maxidx;
   9713 
   9714       everdef = (Elf_External_Verdef *) contents;
   9715       iverdefarr = elf_tdata (abfd)->verdef;
   9716       for (i = 0; i < verdef_count; ++i)
   9717 	{
   9718 	  Elf_External_Verdaux *everdaux;
   9719 	  Elf_Internal_Verdaux *iverdaux;
   9720 	  unsigned int j;
   9721 
   9722 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9723 
   9724 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9725 	    goto error_return_bad_verdef;
   9726 
   9727 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9728 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9729 
   9730 	  iverdef->vd_bfd = abfd;
   9731 
   9732 	  if (iverdef->vd_cnt == 0)
   9733 	    iverdef->vd_auxptr = NULL;
   9734 	  else
   9735 	    {
   9736 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9737 				     sizeof (Elf_Internal_Verdaux), &amt))
   9738 		{
   9739 		  bfd_set_error (bfd_error_file_too_big);
   9740 		  goto error_return_verdef;
   9741 		}
   9742 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9743 		bfd_alloc (abfd, amt);
   9744 	      if (iverdef->vd_auxptr == NULL)
   9745 		goto error_return_verdef;
   9746 	    }
   9747 
   9748 	  if (iverdef->vd_aux
   9749 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9750 	    goto error_return_bad_verdef;
   9751 
   9752 	  everdaux = ((Elf_External_Verdaux *)
   9753 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9754 	  iverdaux = iverdef->vd_auxptr;
   9755 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9756 	    {
   9757 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9758 
   9759 	      if (elf_use_dt_symtab_p (abfd))
   9760 		{
   9761 		  if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
   9762 		    iverdaux->vda_nodename
   9763 		      = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
   9764 		  else
   9765 		    iverdaux->vda_nodename = NULL;
   9766 		}
   9767 	      else
   9768 		iverdaux->vda_nodename
   9769 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9770 						     iverdaux->vda_name);
   9771 	      if (iverdaux->vda_nodename == NULL)
   9772 		goto error_return_bad_verdef;
   9773 
   9774 	      iverdaux->vda_nextptr = NULL;
   9775 	      if (iverdaux->vda_next == 0)
   9776 		{
   9777 		  iverdef->vd_cnt = j + 1;
   9778 		  break;
   9779 		}
   9780 	      if (j + 1 < iverdef->vd_cnt)
   9781 		iverdaux->vda_nextptr = iverdaux + 1;
   9782 
   9783 	      if (iverdaux->vda_next
   9784 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9785 		goto error_return_bad_verdef;
   9786 
   9787 	      everdaux = ((Elf_External_Verdaux *)
   9788 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9789 	    }
   9790 
   9791 	  iverdef->vd_nodename = NULL;
   9792 	  if (iverdef->vd_cnt)
   9793 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9794 
   9795 	  iverdef->vd_nextdef = NULL;
   9796 	  if (iverdef->vd_next == 0)
   9797 	    break;
   9798 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9799 	    iverdef->vd_nextdef = iverdef + 1;
   9800 
   9801 	  everdef = ((Elf_External_Verdef *)
   9802 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9803 	}
   9804 
   9805       if (contents != elf_tdata (abfd)->dt_verdef)
   9806 	free (contents);
   9807       contents = NULL;
   9808     }
   9809   else if (default_imported_symver)
   9810     {
   9811       if (freeidx < 3)
   9812 	freeidx = 3;
   9813       else
   9814 	freeidx++;
   9815 
   9816       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9817 	{
   9818 	  bfd_set_error (bfd_error_file_too_big);
   9819 	  goto error_return;
   9820 	}
   9821       if (amt == 0)
   9822 	goto error_return;
   9823       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9824       if (elf_tdata (abfd)->verdef == NULL)
   9825 	goto error_return;
   9826 
   9827       elf_tdata (abfd)->cverdefs = freeidx;
   9828     }
   9829 
   9830   /* Create a default version based on the soname.  */
   9831   if (default_imported_symver)
   9832     {
   9833       Elf_Internal_Verdef *iverdef;
   9834       Elf_Internal_Verdaux *iverdaux;
   9835 
   9836       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9837 
   9838       iverdef->vd_version = VER_DEF_CURRENT;
   9839       iverdef->vd_flags = 0;
   9840       iverdef->vd_ndx = freeidx;
   9841       iverdef->vd_cnt = 1;
   9842 
   9843       iverdef->vd_bfd = abfd;
   9844 
   9845       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9846       if (iverdef->vd_nodename == NULL)
   9847 	goto error_return_verdef;
   9848       iverdef->vd_nextdef = NULL;
   9849       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9850 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9851       if (iverdef->vd_auxptr == NULL)
   9852 	goto error_return_verdef;
   9853 
   9854       iverdaux = iverdef->vd_auxptr;
   9855       iverdaux->vda_nodename = iverdef->vd_nodename;
   9856     }
   9857 
   9858   return true;
   9859 
   9860  error_return:
   9861   if (contents != elf_tdata (abfd)->dt_verneed
   9862       && contents != elf_tdata (abfd)->dt_verdef)
   9863     free (contents);
   9864   return false;
   9865 }
   9866 
   9867 asymbol *
   9869 _bfd_elf_make_empty_symbol (bfd *abfd)
   9870 {
   9871   elf_symbol_type *newsym;
   9872 
   9873   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9874   if (!newsym)
   9875     return NULL;
   9876   newsym->symbol.the_bfd = abfd;
   9877   return &newsym->symbol;
   9878 }
   9879 
   9880 void
   9881 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9882 			  asymbol *symbol,
   9883 			  symbol_info *ret)
   9884 {
   9885   bfd_symbol_info (symbol, ret);
   9886 }
   9887 
   9888 /* Return whether a symbol name implies a local symbol.  Most targets
   9889    use this function for the is_local_label_name entry point, but some
   9890    override it.  */
   9891 
   9892 bool
   9893 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9894 			      const char *name)
   9895 {
   9896   /* Normal local symbols start with ``.L''.  */
   9897   if (name[0] == '.' && name[1] == 'L')
   9898     return true;
   9899 
   9900   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9901      DWARF debugging symbols starting with ``..''.  */
   9902   if (name[0] == '.' && name[1] == '.')
   9903     return true;
   9904 
   9905   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9906      emitting DWARF debugging output.  I suspect this is actually a
   9907      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9908      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9909      underscore to be emitted on some ELF targets).  For ease of use,
   9910      we treat such symbols as local.  */
   9911   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9912     return true;
   9913 
   9914   /* Treat assembler generated fake symbols, dollar local labels and
   9915      forward-backward labels (aka local labels) as locals.
   9916      These labels have the form:
   9917 
   9918        L0^A.*				       (fake symbols)
   9919 
   9920        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9921 
   9922      Versions which start with .L will have already been matched above,
   9923      so we only need to match the rest.  */
   9924   if (name[0] == 'L' && ISDIGIT (name[1]))
   9925     {
   9926       bool ret = false;
   9927       const char * p;
   9928       char c;
   9929 
   9930       for (p = name + 2; (c = *p); p++)
   9931 	{
   9932 	  if (c == 1 || c == 2)
   9933 	    {
   9934 	      if (c == 1 && p == name + 2)
   9935 		/* A fake symbol.  */
   9936 		return true;
   9937 
   9938 	      /* FIXME: We are being paranoid here and treating symbols like
   9939 		 L0^Bfoo as if there were non-local, on the grounds that the
   9940 		 assembler will never generate them.  But can any symbol
   9941 		 containing an ASCII value in the range 1-31 ever be anything
   9942 		 other than some kind of local ?  */
   9943 	      ret = true;
   9944 	    }
   9945 
   9946 	  if (! ISDIGIT (c))
   9947 	    {
   9948 	      ret = false;
   9949 	      break;
   9950 	    }
   9951 	}
   9952       return ret;
   9953     }
   9954 
   9955   return false;
   9956 }
   9957 
   9958 alent *
   9959 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9960 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9961 {
   9962   abort ();
   9963   return NULL;
   9964 }
   9965 
   9966 bool
   9967 _bfd_elf_set_arch_mach (bfd *abfd,
   9968 			enum bfd_architecture arch,
   9969 			unsigned long machine)
   9970 {
   9971   /* If this isn't the right architecture for this backend, and this
   9972      isn't the generic backend, fail.  */
   9973   if (arch != get_elf_backend_data (abfd)->arch
   9974       && arch != bfd_arch_unknown
   9975       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9976     return false;
   9977 
   9978   return bfd_default_set_arch_mach (abfd, arch, machine);
   9979 }
   9980 
   9981 /* Find the nearest line to a particular section and offset,
   9982    for error reporting.  */
   9983 
   9984 bool
   9985 _bfd_elf_find_nearest_line (bfd *abfd,
   9986 			    asymbol **symbols,
   9987 			    asection *section,
   9988 			    bfd_vma offset,
   9989 			    const char **filename_ptr,
   9990 			    const char **functionname_ptr,
   9991 			    unsigned int *line_ptr,
   9992 			    unsigned int *discriminator_ptr)
   9993 {
   9994   return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
   9995 					      offset, filename_ptr,
   9996 					      functionname_ptr, line_ptr,
   9997 					      discriminator_ptr);
   9998 }
   9999 
   10000 /* Find the nearest line to a particular section and offset,
   10001    for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
   10002    can be optionally specified.  */
   10003 
   10004 bool
   10005 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
   10006 				     const char *alt_filename,
   10007 				     asymbol **symbols,
   10008 				     asection *section,
   10009 				     bfd_vma offset,
   10010 				     const char **filename_ptr,
   10011 				     const char **functionname_ptr,
   10012 				     unsigned int *line_ptr,
   10013 				     unsigned int *discriminator_ptr)
   10014 {
   10015   bool found;
   10016 
   10017   if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
   10018 					      section, offset, filename_ptr,
   10019 					      functionname_ptr, line_ptr,
   10020 					      discriminator_ptr,
   10021 					      dwarf_debug_sections,
   10022 					      &elf_tdata (abfd)->dwarf2_find_line_info))
   10023     return true;
   10024 
   10025   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   10026 				     filename_ptr, functionname_ptr, line_ptr))
   10027     {
   10028       if (!*functionname_ptr)
   10029 	_bfd_elf_find_function (abfd, symbols, section, offset,
   10030 				*filename_ptr ? NULL : filename_ptr,
   10031 				functionname_ptr);
   10032       return true;
   10033     }
   10034 
   10035   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   10036 					     &found, filename_ptr,
   10037 					     functionname_ptr, line_ptr,
   10038 					     &elf_tdata (abfd)->line_info))
   10039     return false;
   10040   if (found && (*functionname_ptr || *line_ptr))
   10041     return true;
   10042 
   10043   if (symbols == NULL)
   10044     return false;
   10045 
   10046   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   10047 				filename_ptr, functionname_ptr))
   10048     return false;
   10049 
   10050   *line_ptr = 0;
   10051   return true;
   10052 }
   10053 
   10054 /* Find the line for a symbol.  */
   10055 
   10056 bool
   10057 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   10058 		    const char **filename_ptr, unsigned int *line_ptr)
   10059 {
   10060   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   10061   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   10062 					filename_ptr, NULL, line_ptr, NULL,
   10063 					dwarf_debug_sections,
   10064 					&tdata->dwarf2_find_line_info);
   10065 }
   10066 
   10067 /* After a call to bfd_find_nearest_line, successive calls to
   10068    bfd_find_inliner_info can be used to get source information about
   10069    each level of function inlining that terminated at the address
   10070    passed to bfd_find_nearest_line.  Currently this is only supported
   10071    for DWARF2 with appropriate DWARF3 extensions. */
   10072 
   10073 bool
   10074 _bfd_elf_find_inliner_info (bfd *abfd,
   10075 			    const char **filename_ptr,
   10076 			    const char **functionname_ptr,
   10077 			    unsigned int *line_ptr)
   10078 {
   10079   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   10080   return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   10081 					functionname_ptr, line_ptr,
   10082 					&tdata->dwarf2_find_line_info);
   10083 }
   10084 
   10085 int
   10086 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   10087 {
   10088   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10089   int ret = bed->s->sizeof_ehdr;
   10090 
   10091   if (!bfd_link_relocatable (info))
   10092     {
   10093       bfd_size_type phdr_size = elf_program_header_size (abfd);
   10094 
   10095       if (phdr_size == (bfd_size_type) -1)
   10096 	{
   10097 	  struct elf_segment_map *m;
   10098 
   10099 	  phdr_size = 0;
   10100 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   10101 	    phdr_size += bed->s->sizeof_phdr;
   10102 
   10103 	  if (phdr_size == 0)
   10104 	    phdr_size = get_program_header_size (abfd, info);
   10105 	}
   10106 
   10107       elf_program_header_size (abfd) = phdr_size;
   10108       ret += phdr_size;
   10109     }
   10110 
   10111   return ret;
   10112 }
   10113 
   10114 bool
   10115 _bfd_elf_set_section_contents (bfd *abfd,
   10116 			       sec_ptr section,
   10117 			       const void *location,
   10118 			       file_ptr offset,
   10119 			       bfd_size_type count)
   10120 {
   10121   Elf_Internal_Shdr *hdr;
   10122 
   10123   if (! abfd->output_has_begun
   10124       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   10125     return false;
   10126 
   10127   if (!count)
   10128     return true;
   10129 
   10130   hdr = &elf_section_data (section)->this_hdr;
   10131   if (hdr->sh_offset == (file_ptr) -1)
   10132     {
   10133       unsigned char *contents;
   10134 
   10135       if (bfd_section_is_ctf (section))
   10136 	/* Nothing to do with this section: the contents are generated
   10137 	   later.  */
   10138 	return true;
   10139 
   10140       if ((offset + count) > hdr->sh_size)
   10141 	{
   10142 	  _bfd_error_handler
   10143 	    (_("%pB:%pA: error: attempting to write"
   10144 	       " over the end of the section"),
   10145 	     abfd, section);
   10146 
   10147 	  bfd_set_error (bfd_error_invalid_operation);
   10148 	  return false;
   10149 	}
   10150 
   10151       contents = hdr->contents;
   10152       if (contents == NULL)
   10153 	{
   10154 	  _bfd_error_handler
   10155 	    (_("%pB:%pA: error: attempting to write"
   10156 	       " section into an empty buffer"),
   10157 	     abfd, section);
   10158 
   10159 	  bfd_set_error (bfd_error_invalid_operation);
   10160 	  return false;
   10161 	}
   10162 
   10163       memcpy (contents + offset, location, count);
   10164       return true;
   10165     }
   10166 
   10167   return _bfd_generic_set_section_contents (abfd, section,
   10168 					    location, offset, count);
   10169 }
   10170 
   10171 bool
   10172 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   10173 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   10174 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   10175 {
   10176   abort ();
   10177   return false;
   10178 }
   10179 
   10180 /* Try to convert a non-ELF reloc into an ELF one.  */
   10181 
   10182 bool
   10183 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   10184 {
   10185   /* Check whether we really have an ELF howto.  */
   10186 
   10187   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   10188     {
   10189       bfd_reloc_code_real_type code;
   10190       reloc_howto_type *howto;
   10191 
   10192       /* Alien reloc: Try to determine its type to replace it with an
   10193 	 equivalent ELF reloc.  */
   10194 
   10195       if (areloc->howto->pc_relative)
   10196 	{
   10197 	  switch (areloc->howto->bitsize)
   10198 	    {
   10199 	    case 8:
   10200 	      code = BFD_RELOC_8_PCREL;
   10201 	      break;
   10202 	    case 12:
   10203 	      code = BFD_RELOC_12_PCREL;
   10204 	      break;
   10205 	    case 16:
   10206 	      code = BFD_RELOC_16_PCREL;
   10207 	      break;
   10208 	    case 24:
   10209 	      code = BFD_RELOC_24_PCREL;
   10210 	      break;
   10211 	    case 32:
   10212 	      code = BFD_RELOC_32_PCREL;
   10213 	      break;
   10214 	    case 64:
   10215 	      code = BFD_RELOC_64_PCREL;
   10216 	      break;
   10217 	    default:
   10218 	      goto fail;
   10219 	    }
   10220 
   10221 	  howto = bfd_reloc_type_lookup (abfd, code);
   10222 
   10223 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   10224 	    {
   10225 	      if (howto->pcrel_offset)
   10226 		areloc->addend += areloc->address;
   10227 	      else
   10228 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   10229 	    }
   10230 	}
   10231       else
   10232 	{
   10233 	  switch (areloc->howto->bitsize)
   10234 	    {
   10235 	    case 8:
   10236 	      code = BFD_RELOC_8;
   10237 	      break;
   10238 	    case 14:
   10239 	      code = BFD_RELOC_14;
   10240 	      break;
   10241 	    case 16:
   10242 	      code = BFD_RELOC_16;
   10243 	      break;
   10244 	    case 26:
   10245 	      code = BFD_RELOC_26;
   10246 	      break;
   10247 	    case 32:
   10248 	      code = BFD_RELOC_32;
   10249 	      break;
   10250 	    case 64:
   10251 	      code = BFD_RELOC_64;
   10252 	      break;
   10253 	    default:
   10254 	      goto fail;
   10255 	    }
   10256 
   10257 	  howto = bfd_reloc_type_lookup (abfd, code);
   10258 	}
   10259 
   10260       if (howto)
   10261 	areloc->howto = howto;
   10262       else
   10263 	goto fail;
   10264     }
   10265 
   10266   return true;
   10267 
   10268  fail:
   10269   /* xgettext:c-format */
   10270   _bfd_error_handler (_("%pB: %s unsupported"),
   10271 		      abfd, areloc->howto->name);
   10272   bfd_set_error (bfd_error_sorry);
   10273   return false;
   10274 }
   10275 
   10276 bool
   10277 _bfd_elf_free_cached_info (bfd *abfd)
   10278 {
   10279   struct elf_obj_tdata *tdata;
   10280 
   10281   if ((bfd_get_format (abfd) == bfd_object
   10282        || bfd_get_format (abfd) == bfd_core)
   10283       && (tdata = elf_tdata (abfd)) != NULL)
   10284     {
   10285       if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
   10286 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   10287       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   10288       _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
   10289       _bfd_stab_cleanup (abfd, &tdata->line_info);
   10290     }
   10291 
   10292   return _bfd_generic_bfd_free_cached_info (abfd);
   10293 }
   10294 
   10295 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   10296    in the relocation's offset.  Thus we cannot allow any sort of sanity
   10297    range-checking to interfere.  There is nothing else to do in processing
   10298    this reloc.  */
   10299 
   10300 bfd_reloc_status_type
   10301 _bfd_elf_rel_vtable_reloc_fn
   10302   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   10303    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   10304    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   10305    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   10306 {
   10307   return bfd_reloc_ok;
   10308 }
   10309 
   10310 /* Elf core file support.  Much of this only works on native
   10312    toolchains, since we rely on knowing the
   10313    machine-dependent procfs structure in order to pick
   10314    out details about the corefile.  */
   10315 
   10316 #ifdef HAVE_SYS_PROCFS_H
   10317 # include <sys/procfs.h>
   10318 #endif
   10319 
   10320 /* Return a PID that identifies a "thread" for threaded cores, or the
   10321    PID of the main process for non-threaded cores.  */
   10322 
   10323 static int
   10324 elfcore_make_pid (bfd *abfd)
   10325 {
   10326   int pid;
   10327 
   10328   pid = elf_tdata (abfd)->core->lwpid;
   10329   if (pid == 0)
   10330     pid = elf_tdata (abfd)->core->pid;
   10331 
   10332   return pid;
   10333 }
   10334 
   10335 /* If there isn't a section called NAME, make one, using data from
   10336    SECT.  Note, this function will generate a reference to NAME, so
   10337    you shouldn't deallocate or overwrite it.  */
   10338 
   10339 static bool
   10340 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   10341 {
   10342   asection *sect2;
   10343 
   10344   if (bfd_get_section_by_name (abfd, name) != NULL)
   10345     return true;
   10346 
   10347   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   10348   if (sect2 == NULL)
   10349     return false;
   10350 
   10351   sect2->size = sect->size;
   10352   sect2->filepos = sect->filepos;
   10353   sect2->alignment_power = sect->alignment_power;
   10354   return true;
   10355 }
   10356 
   10357 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   10358    actually creates up to two pseudosections:
   10359    - For the single-threaded case, a section named NAME, unless
   10360      such a section already exists.
   10361    - For the multi-threaded case, a section named "NAME/PID", where
   10362      PID is elfcore_make_pid (abfd).
   10363    Both pseudosections have identical contents.  */
   10364 bool
   10365 _bfd_elfcore_make_pseudosection (bfd *abfd,
   10366 				 char *name,
   10367 				 size_t size,
   10368 				 ufile_ptr filepos)
   10369 {
   10370   char buf[100];
   10371   char *threaded_name;
   10372   size_t len;
   10373   asection *sect;
   10374 
   10375   /* Build the section name.  */
   10376 
   10377   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   10378   len = strlen (buf) + 1;
   10379   threaded_name = (char *) bfd_alloc (abfd, len);
   10380   if (threaded_name == NULL)
   10381     return false;
   10382   memcpy (threaded_name, buf, len);
   10383 
   10384   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   10385 					     SEC_HAS_CONTENTS);
   10386   if (sect == NULL)
   10387     return false;
   10388   sect->size = size;
   10389   sect->filepos = filepos;
   10390   sect->alignment_power = 2;
   10391 
   10392   return elfcore_maybe_make_sect (abfd, name, sect);
   10393 }
   10394 
   10395 static bool
   10396 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   10397 				size_t offs)
   10398 {
   10399   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   10400 						       SEC_HAS_CONTENTS);
   10401 
   10402   if (sect == NULL)
   10403     return false;
   10404 
   10405   sect->size = note->descsz - offs;
   10406   sect->filepos = note->descpos + offs;
   10407   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10408 
   10409   return true;
   10410 }
   10411 
   10412 /* prstatus_t exists on:
   10413      solaris 2.5+
   10414      linux 2.[01] + glibc
   10415      unixware 4.2
   10416 */
   10417 
   10418 #if defined (HAVE_PRSTATUS_T)
   10419 
   10420 static bool
   10421 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10422 {
   10423   size_t size;
   10424   int offset;
   10425 
   10426   if (note->descsz == sizeof (prstatus_t))
   10427     {
   10428       prstatus_t prstat;
   10429 
   10430       size = sizeof (prstat.pr_reg);
   10431       offset   = offsetof (prstatus_t, pr_reg);
   10432       memcpy (&prstat, note->descdata, sizeof (prstat));
   10433 
   10434       /* Do not overwrite the core signal if it
   10435 	 has already been set by another thread.  */
   10436       if (elf_tdata (abfd)->core->signal == 0)
   10437 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10438       if (elf_tdata (abfd)->core->pid == 0)
   10439 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10440 
   10441       /* pr_who exists on:
   10442 	 solaris 2.5+
   10443 	 unixware 4.2
   10444 	 pr_who doesn't exist on:
   10445 	 linux 2.[01]
   10446 	 */
   10447 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   10448       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10449 #else
   10450       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10451 #endif
   10452     }
   10453 #if defined (HAVE_PRSTATUS32_T)
   10454   else if (note->descsz == sizeof (prstatus32_t))
   10455     {
   10456       /* 64-bit host, 32-bit corefile */
   10457       prstatus32_t prstat;
   10458 
   10459       size = sizeof (prstat.pr_reg);
   10460       offset   = offsetof (prstatus32_t, pr_reg);
   10461       memcpy (&prstat, note->descdata, sizeof (prstat));
   10462 
   10463       /* Do not overwrite the core signal if it
   10464 	 has already been set by another thread.  */
   10465       if (elf_tdata (abfd)->core->signal == 0)
   10466 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10467       if (elf_tdata (abfd)->core->pid == 0)
   10468 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10469 
   10470       /* pr_who exists on:
   10471 	 solaris 2.5+
   10472 	 unixware 4.2
   10473 	 pr_who doesn't exist on:
   10474 	 linux 2.[01]
   10475 	 */
   10476 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   10477       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10478 #else
   10479       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10480 #endif
   10481     }
   10482 #endif /* HAVE_PRSTATUS32_T */
   10483   else
   10484     {
   10485       /* Fail - we don't know how to handle any other
   10486 	 note size (ie. data object type).  */
   10487       return true;
   10488     }
   10489 
   10490   /* Make a ".reg/999" section and a ".reg" section.  */
   10491   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   10492 					  size, note->descpos + offset);
   10493 }
   10494 #endif /* defined (HAVE_PRSTATUS_T) */
   10495 
   10496 /* Create a pseudosection containing the exact contents of NOTE.  */
   10497 static bool
   10498 elfcore_make_note_pseudosection (bfd *abfd,
   10499 				 char *name,
   10500 				 Elf_Internal_Note *note)
   10501 {
   10502   return _bfd_elfcore_make_pseudosection (abfd, name,
   10503 					  note->descsz, note->descpos);
   10504 }
   10505 
   10506 /* There isn't a consistent prfpregset_t across platforms,
   10507    but it doesn't matter, because we don't have to pick this
   10508    data structure apart.  */
   10509 
   10510 static bool
   10511 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   10512 {
   10513   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10514 }
   10515 
   10516 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   10517    type of NT_PRXFPREG.  Just include the whole note's contents
   10518    literally.  */
   10519 
   10520 static bool
   10521 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   10522 {
   10523   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   10524 }
   10525 
   10526 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   10527    with a note type of NT_X86_XSTATE.  Just include the whole note's
   10528    contents literally.  */
   10529 
   10530 static bool
   10531 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   10532 {
   10533   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   10534 }
   10535 
   10536 static bool
   10537 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   10538 {
   10539   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   10540 }
   10541 
   10542 static bool
   10543 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   10544 {
   10545   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   10546 }
   10547 
   10548 static bool
   10549 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   10550 {
   10551   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
   10552 }
   10553 
   10554 static bool
   10555 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   10556 {
   10557   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
   10558 }
   10559 
   10560 static bool
   10561 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   10562 {
   10563   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
   10564 }
   10565 
   10566 static bool
   10567 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   10568 {
   10569   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
   10570 }
   10571 
   10572 static bool
   10573 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   10574 {
   10575   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
   10576 }
   10577 
   10578 static bool
   10579 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   10580 {
   10581   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
   10582 }
   10583 
   10584 static bool
   10585 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   10586 {
   10587   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
   10588 }
   10589 
   10590 static bool
   10591 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   10592 {
   10593   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
   10594 }
   10595 
   10596 static bool
   10597 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   10598 {
   10599   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
   10600 }
   10601 
   10602 static bool
   10603 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   10604 {
   10605   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
   10606 }
   10607 
   10608 static bool
   10609 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   10610 {
   10611   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
   10612 }
   10613 
   10614 static bool
   10615 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   10616 {
   10617   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
   10618 }
   10619 
   10620 static bool
   10621 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   10622 {
   10623   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
   10624 }
   10625 
   10626 static bool
   10627 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   10628 {
   10629   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   10630 }
   10631 
   10632 static bool
   10633 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   10634 {
   10635   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   10636 }
   10637 
   10638 static bool
   10639 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   10640 {
   10641   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   10642 }
   10643 
   10644 static bool
   10645 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   10646 {
   10647   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   10648 }
   10649 
   10650 static bool
   10651 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   10652 {
   10653   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   10654 }
   10655 
   10656 static bool
   10657 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   10658 {
   10659   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   10660 }
   10661 
   10662 static bool
   10663 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   10664 {
   10665   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   10666 }
   10667 
   10668 static bool
   10669 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   10670 {
   10671   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   10672 }
   10673 
   10674 static bool
   10675 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   10676 {
   10677   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   10678 }
   10679 
   10680 static bool
   10681 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   10682 {
   10683   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   10684 }
   10685 
   10686 static bool
   10687 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   10688 {
   10689   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   10690 }
   10691 
   10692 static bool
   10693 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   10694 {
   10695   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   10696 }
   10697 
   10698 static bool
   10699 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10700 {
   10701   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   10702 }
   10703 
   10704 static bool
   10705 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10706 {
   10707   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   10708 }
   10709 
   10710 static bool
   10711 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10712 {
   10713   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   10714 }
   10715 
   10716 static bool
   10717 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10718 {
   10719   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   10720 }
   10721 
   10722 static bool
   10723 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10724 {
   10725   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   10726 }
   10727 
   10728 static bool
   10729 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10730 {
   10731   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
   10732 }
   10733 
   10734 static bool
   10735 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10736 {
   10737   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
   10738 }
   10739 
   10740 static bool
   10741 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10742 {
   10743   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
   10744 					  note);
   10745 }
   10746 
   10747 static bool
   10748 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
   10749 {
   10750   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
   10751 }
   10752 
   10753 static bool
   10754 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
   10755 {
   10756   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
   10757 }
   10758 
   10759 /* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
   10760    successful, otherwise return FALSE.  */
   10761 
   10762 static bool
   10763 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
   10764 {
   10765   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
   10766 }
   10767 
   10768 static bool
   10769 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10770 {
   10771   return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
   10772 }
   10773 
   10774 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10775    successful otherwise, return FALSE.  */
   10776 
   10777 static bool
   10778 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10779 {
   10780   return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
   10781 }
   10782 
   10783 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10784    successful otherwise, return FALSE.  */
   10785 
   10786 static bool
   10787 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10788 {
   10789   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10790 }
   10791 
   10792 static bool
   10793 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10794 {
   10795   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
   10796 }
   10797 
   10798 static bool
   10799 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10800 {
   10801   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
   10802 }
   10803 
   10804 static bool
   10805 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10806 {
   10807   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
   10808 }
   10809 
   10810 static bool
   10811 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10812 {
   10813   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
   10814 }
   10815 
   10816 #if defined (HAVE_PRPSINFO_T)
   10817 typedef prpsinfo_t   elfcore_psinfo_t;
   10818 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10819 typedef prpsinfo32_t elfcore_psinfo32_t;
   10820 #endif
   10821 #endif
   10822 
   10823 #if defined (HAVE_PSINFO_T)
   10824 typedef psinfo_t   elfcore_psinfo_t;
   10825 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10826 typedef psinfo32_t elfcore_psinfo32_t;
   10827 #endif
   10828 #endif
   10829 
   10830 /* return a malloc'ed copy of a string at START which is at
   10831    most MAX bytes long, possibly without a terminating '\0'.
   10832    the copy will always have a terminating '\0'.  */
   10833 
   10834 char *
   10835 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10836 {
   10837   char *dups;
   10838   char *end = (char *) memchr (start, '\0', max);
   10839   size_t len;
   10840 
   10841   if (end == NULL)
   10842     len = max;
   10843   else
   10844     len = end - start;
   10845 
   10846   dups = (char *) bfd_alloc (abfd, len + 1);
   10847   if (dups == NULL)
   10848     return NULL;
   10849 
   10850   memcpy (dups, start, len);
   10851   dups[len] = '\0';
   10852 
   10853   return dups;
   10854 }
   10855 
   10856 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10857 static bool
   10858 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10859 {
   10860   if (note->descsz == sizeof (elfcore_psinfo_t))
   10861     {
   10862       elfcore_psinfo_t psinfo;
   10863 
   10864       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10865 
   10866 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   10867       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10868 #endif
   10869       elf_tdata (abfd)->core->program
   10870 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10871 				sizeof (psinfo.pr_fname));
   10872 
   10873       elf_tdata (abfd)->core->command
   10874 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10875 				sizeof (psinfo.pr_psargs));
   10876     }
   10877 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10878   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10879     {
   10880       /* 64-bit host, 32-bit corefile */
   10881       elfcore_psinfo32_t psinfo;
   10882 
   10883       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10884 
   10885 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10886       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10887 #endif
   10888       elf_tdata (abfd)->core->program
   10889 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10890 				sizeof (psinfo.pr_fname));
   10891 
   10892       elf_tdata (abfd)->core->command
   10893 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10894 				sizeof (psinfo.pr_psargs));
   10895     }
   10896 #endif
   10897 
   10898   else
   10899     {
   10900       /* Fail - we don't know how to handle any other
   10901 	 note size (ie. data object type).  */
   10902       return true;
   10903     }
   10904 
   10905   /* Note that for some reason, a spurious space is tacked
   10906      onto the end of the args in some (at least one anyway)
   10907      implementations, so strip it off if it exists.  */
   10908 
   10909   {
   10910     char *command = elf_tdata (abfd)->core->command;
   10911     int n = strlen (command);
   10912 
   10913     if (0 < n && command[n - 1] == ' ')
   10914       command[n - 1] = '\0';
   10915   }
   10916 
   10917   return true;
   10918 }
   10919 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10920 
   10921 #if defined (HAVE_PSTATUS_T)
   10922 static bool
   10923 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10924 {
   10925   if (note->descsz == sizeof (pstatus_t)
   10926 #if defined (HAVE_PXSTATUS_T)
   10927       || note->descsz == sizeof (pxstatus_t)
   10928 #endif
   10929       )
   10930     {
   10931       pstatus_t pstat;
   10932 
   10933       memcpy (&pstat, note->descdata, sizeof (pstat));
   10934 
   10935       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10936     }
   10937 #if defined (HAVE_PSTATUS32_T)
   10938   else if (note->descsz == sizeof (pstatus32_t))
   10939     {
   10940       /* 64-bit host, 32-bit corefile */
   10941       pstatus32_t pstat;
   10942 
   10943       memcpy (&pstat, note->descdata, sizeof (pstat));
   10944 
   10945       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10946     }
   10947 #endif
   10948   /* Could grab some more details from the "representative"
   10949      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10950      NT_LWPSTATUS note, presumably.  */
   10951 
   10952   return true;
   10953 }
   10954 #endif /* defined (HAVE_PSTATUS_T) */
   10955 
   10956 #if defined (HAVE_LWPSTATUS_T)
   10957 static bool
   10958 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10959 {
   10960   lwpstatus_t lwpstat;
   10961   char buf[100];
   10962   char *name;
   10963   size_t len;
   10964   asection *sect;
   10965 
   10966   if (note->descsz != sizeof (lwpstat)
   10967 #if defined (HAVE_LWPXSTATUS_T)
   10968       && note->descsz != sizeof (lwpxstatus_t)
   10969 #endif
   10970       )
   10971     return true;
   10972 
   10973   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10974 
   10975   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10976   /* Do not overwrite the core signal if it has already been set by
   10977      another thread.  */
   10978   if (elf_tdata (abfd)->core->signal == 0)
   10979     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10980 
   10981   /* Make a ".reg/999" section.  */
   10982 
   10983   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   10984   len = strlen (buf) + 1;
   10985   name = bfd_alloc (abfd, len);
   10986   if (name == NULL)
   10987     return false;
   10988   memcpy (name, buf, len);
   10989 
   10990   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10991   if (sect == NULL)
   10992     return false;
   10993 
   10994 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10995   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   10996   sect->filepos = note->descpos
   10997     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   10998 #endif
   10999 
   11000 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   11001   sect->size = sizeof (lwpstat.pr_reg);
   11002   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   11003 #endif
   11004 
   11005   sect->alignment_power = 2;
   11006 
   11007   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   11008     return false;
   11009 
   11010   /* Make a ".reg2/999" section */
   11011 
   11012   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   11013   len = strlen (buf) + 1;
   11014   name = bfd_alloc (abfd, len);
   11015   if (name == NULL)
   11016     return false;
   11017   memcpy (name, buf, len);
   11018 
   11019   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11020   if (sect == NULL)
   11021     return false;
   11022 
   11023 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11024   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   11025   sect->filepos = note->descpos
   11026     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   11027 #endif
   11028 
   11029 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   11030   sect->size = sizeof (lwpstat.pr_fpreg);
   11031   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   11032 #endif
   11033 
   11034   sect->alignment_power = 2;
   11035 
   11036   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   11037 }
   11038 #endif /* defined (HAVE_LWPSTATUS_T) */
   11039 
   11040 /* These constants, and the structure offsets used below, are defined by
   11041    Cygwin's core_dump.h */
   11042 #define NOTE_INFO_PROCESS  1
   11043 #define NOTE_INFO_THREAD   2
   11044 #define NOTE_INFO_MODULE   3
   11045 #define NOTE_INFO_MODULE64 4
   11046 
   11047 static bool
   11048 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   11049 {
   11050   char buf[30];
   11051   char *name;
   11052   size_t len;
   11053   unsigned int name_size;
   11054   asection *sect;
   11055   unsigned int type;
   11056   int is_active_thread;
   11057   bfd_vma base_addr;
   11058 
   11059   if (note->descsz < 4)
   11060     return true;
   11061 
   11062   if (! startswith (note->namedata, "win32"))
   11063     return true;
   11064 
   11065   type = bfd_get_32 (abfd, note->descdata);
   11066 
   11067   struct
   11068   {
   11069     const char *type_name;
   11070     unsigned long min_size;
   11071   } size_check[] =
   11072       {
   11073        { "NOTE_INFO_PROCESS", 12 },
   11074        { "NOTE_INFO_THREAD", 12 },
   11075        { "NOTE_INFO_MODULE", 12 },
   11076        { "NOTE_INFO_MODULE64", 16 },
   11077       };
   11078 
   11079   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   11080       return true;
   11081 
   11082   if (note->descsz < size_check[type - 1].min_size)
   11083     {
   11084       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
   11085 			    " is too small"),
   11086 			  abfd, size_check[type - 1].type_name, note->descsz);
   11087       return true;
   11088     }
   11089 
   11090   switch (type)
   11091     {
   11092     case NOTE_INFO_PROCESS:
   11093       /* FIXME: need to add ->core->command.  */
   11094       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   11095       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   11096       break;
   11097 
   11098     case NOTE_INFO_THREAD:
   11099       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   11100 	 structure. */
   11101       /* thread_info.tid */
   11102       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   11103 
   11104       len = strlen (buf) + 1;
   11105       name = (char *) bfd_alloc (abfd, len);
   11106       if (name == NULL)
   11107 	return false;
   11108 
   11109       memcpy (name, buf, len);
   11110 
   11111       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11112       if (sect == NULL)
   11113 	return false;
   11114 
   11115       /* sizeof (thread_info.thread_context) */
   11116       sect->size = note->descsz - 12;
   11117       /* offsetof (thread_info.thread_context) */
   11118       sect->filepos = note->descpos + 12;
   11119       sect->alignment_power = 2;
   11120 
   11121       /* thread_info.is_active_thread */
   11122       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   11123 
   11124       if (is_active_thread)
   11125 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   11126 	  return false;
   11127       break;
   11128 
   11129     case NOTE_INFO_MODULE:
   11130     case NOTE_INFO_MODULE64:
   11131       /* Make a ".module/xxxxxxxx" section.  */
   11132       if (type == NOTE_INFO_MODULE)
   11133 	{
   11134 	  /* module_info.base_address */
   11135 	  base_addr = bfd_get_32 (abfd, note->descdata + 4);
   11136 	  sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   11137 	  /* module_info.module_name_size */
   11138 	  name_size = bfd_get_32 (abfd, note->descdata + 8);
   11139 	}
   11140       else /* NOTE_INFO_MODULE64 */
   11141 	{
   11142 	  /* module_info.base_address */
   11143 	  base_addr = bfd_get_64 (abfd, note->descdata + 4);
   11144 	  sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   11145 	  /* module_info.module_name_size */
   11146 	  name_size = bfd_get_32 (abfd, note->descdata + 12);
   11147 	}
   11148 
   11149       len = strlen (buf) + 1;
   11150       name = (char *) bfd_alloc (abfd, len);
   11151       if (name == NULL)
   11152 	return false;
   11153 
   11154       memcpy (name, buf, len);
   11155 
   11156       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11157 
   11158       if (sect == NULL)
   11159 	return false;
   11160 
   11161       if (note->descsz < 12 + name_size)
   11162 	{
   11163 	  _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
   11164 				" is too small to contain a name of size %u"),
   11165 			      abfd, note->descsz, name_size);
   11166 	  return true;
   11167 	}
   11168 
   11169       sect->size = note->descsz;
   11170       sect->filepos = note->descpos;
   11171       sect->alignment_power = 2;
   11172       break;
   11173 
   11174     default:
   11175       return true;
   11176     }
   11177 
   11178   return true;
   11179 }
   11180 
   11181 static bool
   11182 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   11183 {
   11184   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11185 
   11186   switch (note->type)
   11187     {
   11188     default:
   11189       return true;
   11190 
   11191     case NT_PRSTATUS:
   11192       if (bed->elf_backend_grok_prstatus)
   11193 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   11194 	  return true;
   11195 #if defined (HAVE_PRSTATUS_T)
   11196       return elfcore_grok_prstatus (abfd, note);
   11197 #else
   11198       return true;
   11199 #endif
   11200 
   11201 #if defined (HAVE_PSTATUS_T)
   11202     case NT_PSTATUS:
   11203       return elfcore_grok_pstatus (abfd, note);
   11204 #endif
   11205 
   11206 #if defined (HAVE_LWPSTATUS_T)
   11207     case NT_LWPSTATUS:
   11208       return elfcore_grok_lwpstatus (abfd, note);
   11209 #endif
   11210 
   11211     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   11212       return elfcore_grok_prfpreg (abfd, note);
   11213 
   11214     case NT_WIN32PSTATUS:
   11215       return elfcore_grok_win32pstatus (abfd, note);
   11216 
   11217     case NT_PRXFPREG:		/* Linux SSE extension */
   11218       if (note->namesz == 6
   11219 	  && strcmp (note->namedata, "LINUX") == 0)
   11220 	return elfcore_grok_prxfpreg (abfd, note);
   11221       else
   11222 	return true;
   11223 
   11224     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   11225       if (note->namesz == 6
   11226 	  && strcmp (note->namedata, "LINUX") == 0)
   11227 	return elfcore_grok_xstatereg (abfd, note);
   11228       else
   11229 	return true;
   11230 
   11231     case NT_PPC_VMX:
   11232       if (note->namesz == 6
   11233 	  && strcmp (note->namedata, "LINUX") == 0)
   11234 	return elfcore_grok_ppc_vmx (abfd, note);
   11235       else
   11236 	return true;
   11237 
   11238     case NT_PPC_VSX:
   11239       if (note->namesz == 6
   11240 	  && strcmp (note->namedata, "LINUX") == 0)
   11241 	return elfcore_grok_ppc_vsx (abfd, note);
   11242       else
   11243 	return true;
   11244 
   11245     case NT_PPC_TAR:
   11246       if (note->namesz == 6
   11247 	  && strcmp (note->namedata, "LINUX") == 0)
   11248 	return elfcore_grok_ppc_tar (abfd, note);
   11249       else
   11250 	return true;
   11251 
   11252     case NT_PPC_PPR:
   11253       if (note->namesz == 6
   11254 	  && strcmp (note->namedata, "LINUX") == 0)
   11255 	return elfcore_grok_ppc_ppr (abfd, note);
   11256       else
   11257 	return true;
   11258 
   11259     case NT_PPC_DSCR:
   11260       if (note->namesz == 6
   11261 	  && strcmp (note->namedata, "LINUX") == 0)
   11262 	return elfcore_grok_ppc_dscr (abfd, note);
   11263       else
   11264 	return true;
   11265 
   11266     case NT_PPC_EBB:
   11267       if (note->namesz == 6
   11268 	  && strcmp (note->namedata, "LINUX") == 0)
   11269 	return elfcore_grok_ppc_ebb (abfd, note);
   11270       else
   11271 	return true;
   11272 
   11273     case NT_PPC_PMU:
   11274       if (note->namesz == 6
   11275 	  && strcmp (note->namedata, "LINUX") == 0)
   11276 	return elfcore_grok_ppc_pmu (abfd, note);
   11277       else
   11278 	return true;
   11279 
   11280     case NT_PPC_TM_CGPR:
   11281       if (note->namesz == 6
   11282 	  && strcmp (note->namedata, "LINUX") == 0)
   11283 	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   11284       else
   11285 	return true;
   11286 
   11287     case NT_PPC_TM_CFPR:
   11288       if (note->namesz == 6
   11289 	  && strcmp (note->namedata, "LINUX") == 0)
   11290 	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   11291       else
   11292 	return true;
   11293 
   11294     case NT_PPC_TM_CVMX:
   11295       if (note->namesz == 6
   11296 	  && strcmp (note->namedata, "LINUX") == 0)
   11297 	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   11298       else
   11299 	return true;
   11300 
   11301     case NT_PPC_TM_CVSX:
   11302       if (note->namesz == 6
   11303 	  && strcmp (note->namedata, "LINUX") == 0)
   11304 	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   11305       else
   11306 	return true;
   11307 
   11308     case NT_PPC_TM_SPR:
   11309       if (note->namesz == 6
   11310 	  && strcmp (note->namedata, "LINUX") == 0)
   11311 	return elfcore_grok_ppc_tm_spr (abfd, note);
   11312       else
   11313 	return true;
   11314 
   11315     case NT_PPC_TM_CTAR:
   11316       if (note->namesz == 6
   11317 	  && strcmp (note->namedata, "LINUX") == 0)
   11318 	return elfcore_grok_ppc_tm_ctar (abfd, note);
   11319       else
   11320 	return true;
   11321 
   11322     case NT_PPC_TM_CPPR:
   11323       if (note->namesz == 6
   11324 	  && strcmp (note->namedata, "LINUX") == 0)
   11325 	return elfcore_grok_ppc_tm_cppr (abfd, note);
   11326       else
   11327 	return true;
   11328 
   11329     case NT_PPC_TM_CDSCR:
   11330       if (note->namesz == 6
   11331 	  && strcmp (note->namedata, "LINUX") == 0)
   11332 	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   11333       else
   11334 	return true;
   11335 
   11336     case NT_S390_HIGH_GPRS:
   11337       if (note->namesz == 6
   11338 	  && strcmp (note->namedata, "LINUX") == 0)
   11339 	return elfcore_grok_s390_high_gprs (abfd, note);
   11340       else
   11341 	return true;
   11342 
   11343     case NT_S390_TIMER:
   11344       if (note->namesz == 6
   11345 	  && strcmp (note->namedata, "LINUX") == 0)
   11346 	return elfcore_grok_s390_timer (abfd, note);
   11347       else
   11348 	return true;
   11349 
   11350     case NT_S390_TODCMP:
   11351       if (note->namesz == 6
   11352 	  && strcmp (note->namedata, "LINUX") == 0)
   11353 	return elfcore_grok_s390_todcmp (abfd, note);
   11354       else
   11355 	return true;
   11356 
   11357     case NT_S390_TODPREG:
   11358       if (note->namesz == 6
   11359 	  && strcmp (note->namedata, "LINUX") == 0)
   11360 	return elfcore_grok_s390_todpreg (abfd, note);
   11361       else
   11362 	return true;
   11363 
   11364     case NT_S390_CTRS:
   11365       if (note->namesz == 6
   11366 	  && strcmp (note->namedata, "LINUX") == 0)
   11367 	return elfcore_grok_s390_ctrs (abfd, note);
   11368       else
   11369 	return true;
   11370 
   11371     case NT_S390_PREFIX:
   11372       if (note->namesz == 6
   11373 	  && strcmp (note->namedata, "LINUX") == 0)
   11374 	return elfcore_grok_s390_prefix (abfd, note);
   11375       else
   11376 	return true;
   11377 
   11378     case NT_S390_LAST_BREAK:
   11379       if (note->namesz == 6
   11380 	  && strcmp (note->namedata, "LINUX") == 0)
   11381 	return elfcore_grok_s390_last_break (abfd, note);
   11382       else
   11383 	return true;
   11384 
   11385     case NT_S390_SYSTEM_CALL:
   11386       if (note->namesz == 6
   11387 	  && strcmp (note->namedata, "LINUX") == 0)
   11388 	return elfcore_grok_s390_system_call (abfd, note);
   11389       else
   11390 	return true;
   11391 
   11392     case NT_S390_TDB:
   11393       if (note->namesz == 6
   11394 	  && strcmp (note->namedata, "LINUX") == 0)
   11395 	return elfcore_grok_s390_tdb (abfd, note);
   11396       else
   11397 	return true;
   11398 
   11399     case NT_S390_VXRS_LOW:
   11400       if (note->namesz == 6
   11401 	  && strcmp (note->namedata, "LINUX") == 0)
   11402 	return elfcore_grok_s390_vxrs_low (abfd, note);
   11403       else
   11404 	return true;
   11405 
   11406     case NT_S390_VXRS_HIGH:
   11407       if (note->namesz == 6
   11408 	  && strcmp (note->namedata, "LINUX") == 0)
   11409 	return elfcore_grok_s390_vxrs_high (abfd, note);
   11410       else
   11411 	return true;
   11412 
   11413     case NT_S390_GS_CB:
   11414       if (note->namesz == 6
   11415 	  && strcmp (note->namedata, "LINUX") == 0)
   11416 	return elfcore_grok_s390_gs_cb (abfd, note);
   11417       else
   11418 	return true;
   11419 
   11420     case NT_S390_GS_BC:
   11421       if (note->namesz == 6
   11422 	  && strcmp (note->namedata, "LINUX") == 0)
   11423 	return elfcore_grok_s390_gs_bc (abfd, note);
   11424       else
   11425 	return true;
   11426 
   11427     case NT_ARC_V2:
   11428       if (note->namesz == 6
   11429 	  && strcmp (note->namedata, "LINUX") == 0)
   11430 	return elfcore_grok_arc_v2 (abfd, note);
   11431       else
   11432 	return true;
   11433 
   11434     case NT_ARM_VFP:
   11435       if (note->namesz == 6
   11436 	  && strcmp (note->namedata, "LINUX") == 0)
   11437 	return elfcore_grok_arm_vfp (abfd, note);
   11438       else
   11439 	return true;
   11440 
   11441     case NT_ARM_TLS:
   11442       if (note->namesz == 6
   11443 	  && strcmp (note->namedata, "LINUX") == 0)
   11444 	return elfcore_grok_aarch_tls (abfd, note);
   11445       else
   11446 	return true;
   11447 
   11448     case NT_ARM_HW_BREAK:
   11449       if (note->namesz == 6
   11450 	  && strcmp (note->namedata, "LINUX") == 0)
   11451 	return elfcore_grok_aarch_hw_break (abfd, note);
   11452       else
   11453 	return true;
   11454 
   11455     case NT_ARM_HW_WATCH:
   11456       if (note->namesz == 6
   11457 	  && strcmp (note->namedata, "LINUX") == 0)
   11458 	return elfcore_grok_aarch_hw_watch (abfd, note);
   11459       else
   11460 	return true;
   11461 
   11462     case NT_ARM_SVE:
   11463       if (note->namesz == 6
   11464 	  && strcmp (note->namedata, "LINUX") == 0)
   11465 	return elfcore_grok_aarch_sve (abfd, note);
   11466       else
   11467 	return true;
   11468 
   11469     case NT_ARM_PAC_MASK:
   11470       if (note->namesz == 6
   11471 	  && strcmp (note->namedata, "LINUX") == 0)
   11472 	return elfcore_grok_aarch_pauth (abfd, note);
   11473       else
   11474 	return true;
   11475 
   11476     case NT_ARM_TAGGED_ADDR_CTRL:
   11477       if (note->namesz == 6
   11478 	  && strcmp (note->namedata, "LINUX") == 0)
   11479 	return elfcore_grok_aarch_mte (abfd, note);
   11480       else
   11481 	return true;
   11482 
   11483     case NT_ARM_SSVE:
   11484       if (note->namesz == 6
   11485 	  && strcmp (note->namedata, "LINUX") == 0)
   11486 	return elfcore_grok_aarch_ssve (abfd, note);
   11487       else
   11488 	return true;
   11489 
   11490     case NT_ARM_ZA:
   11491       if (note->namesz == 6
   11492 	  && strcmp (note->namedata, "LINUX") == 0)
   11493 	return elfcore_grok_aarch_za (abfd, note);
   11494       else
   11495 	return true;
   11496 
   11497     case NT_ARM_ZT:
   11498       if (note->namesz == 6
   11499 	  && strcmp (note->namedata, "LINUX") == 0)
   11500 	return elfcore_grok_aarch_zt (abfd, note);
   11501       else
   11502 	return true;
   11503 
   11504     case NT_GDB_TDESC:
   11505       if (note->namesz == 4
   11506 	  && strcmp (note->namedata, "GDB") == 0)
   11507 	return elfcore_grok_gdb_tdesc (abfd, note);
   11508       else
   11509 	return true;
   11510 
   11511     case NT_RISCV_CSR:
   11512       if (note->namesz == 4
   11513 	  && strcmp (note->namedata, "GDB") == 0)
   11514 	return elfcore_grok_riscv_csr (abfd, note);
   11515       else
   11516 	return true;
   11517 
   11518     case NT_LARCH_CPUCFG:
   11519       if (note->namesz == 6
   11520 	  && strcmp (note->namedata, "LINUX") == 0)
   11521 	return elfcore_grok_loongarch_cpucfg (abfd, note);
   11522       else
   11523 	return true;
   11524 
   11525     case NT_LARCH_LBT:
   11526       if (note->namesz == 6
   11527 	  && strcmp (note->namedata, "LINUX") == 0)
   11528 	return elfcore_grok_loongarch_lbt (abfd, note);
   11529       else
   11530 	return true;
   11531 
   11532     case NT_LARCH_LSX:
   11533       if (note->namesz == 6
   11534 	  && strcmp (note->namedata, "LINUX") == 0)
   11535 	return elfcore_grok_loongarch_lsx (abfd, note);
   11536       else
   11537 	return true;
   11538 
   11539     case NT_LARCH_LASX:
   11540       if (note->namesz == 6
   11541 	  && strcmp (note->namedata, "LINUX") == 0)
   11542 	return elfcore_grok_loongarch_lasx (abfd, note);
   11543       else
   11544 	return true;
   11545 
   11546     case NT_PRPSINFO:
   11547     case NT_PSINFO:
   11548       if (bed->elf_backend_grok_psinfo)
   11549 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   11550 	  return true;
   11551 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11552       return elfcore_grok_psinfo (abfd, note);
   11553 #else
   11554       return true;
   11555 #endif
   11556 
   11557     case NT_AUXV:
   11558       return elfcore_make_auxv_note_section (abfd, note, 0);
   11559 
   11560     case NT_FILE:
   11561       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   11562 					      note);
   11563 
   11564     case NT_SIGINFO:
   11565       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   11566 					      note);
   11567 
   11568     }
   11569 }
   11570 
   11571 static bool
   11572 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   11573 {
   11574   struct bfd_build_id* build_id;
   11575 
   11576   if (note->descsz == 0)
   11577     return false;
   11578 
   11579   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   11580   if (build_id == NULL)
   11581     return false;
   11582 
   11583   build_id->size = note->descsz;
   11584   memcpy (build_id->data, note->descdata, note->descsz);
   11585   abfd->build_id = build_id;
   11586 
   11587   return true;
   11588 }
   11589 
   11590 static bool
   11591 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   11592 {
   11593   switch (note->type)
   11594     {
   11595     default:
   11596       return true;
   11597 
   11598     case NT_GNU_PROPERTY_TYPE_0:
   11599       return _bfd_elf_parse_gnu_properties (abfd, note);
   11600 
   11601     case NT_GNU_BUILD_ID:
   11602       return elfobj_grok_gnu_build_id (abfd, note);
   11603     }
   11604 }
   11605 
   11606 static bool
   11607 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   11608 {
   11609   struct sdt_note *cur =
   11610     (struct sdt_note *) bfd_alloc (abfd,
   11611 				   sizeof (struct sdt_note) + note->descsz);
   11612 
   11613   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   11614   cur->size = (bfd_size_type) note->descsz;
   11615   memcpy (cur->data, note->descdata, note->descsz);
   11616 
   11617   elf_tdata (abfd)->sdt_note_head = cur;
   11618 
   11619   return true;
   11620 }
   11621 
   11622 static bool
   11623 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   11624 {
   11625   switch (note->type)
   11626     {
   11627     case NT_STAPSDT:
   11628       return elfobj_grok_stapsdt_note_1 (abfd, note);
   11629 
   11630     default:
   11631       return true;
   11632     }
   11633 }
   11634 
   11635 static bool
   11636 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   11637 {
   11638   size_t offset;
   11639 
   11640   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11641     {
   11642     case ELFCLASS32:
   11643       if (note->descsz < 108)
   11644 	return false;
   11645       break;
   11646 
   11647     case ELFCLASS64:
   11648       if (note->descsz < 120)
   11649 	return false;
   11650       break;
   11651 
   11652     default:
   11653       return false;
   11654     }
   11655 
   11656   /* Check for version 1 in pr_version.  */
   11657   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11658     return false;
   11659 
   11660   offset = 4;
   11661 
   11662   /* Skip over pr_psinfosz. */
   11663   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11664     offset += 4;
   11665   else
   11666     {
   11667       offset += 4;	/* Padding before pr_psinfosz. */
   11668       offset += 8;
   11669     }
   11670 
   11671   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   11672   elf_tdata (abfd)->core->program
   11673     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   11674   offset += 17;
   11675 
   11676   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   11677   elf_tdata (abfd)->core->command
   11678     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   11679   offset += 81;
   11680 
   11681   /* Padding before pr_pid.  */
   11682   offset += 2;
   11683 
   11684   /* The pr_pid field was added in version "1a".  */
   11685   if (note->descsz < offset + 4)
   11686     return true;
   11687 
   11688   elf_tdata (abfd)->core->pid
   11689     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11690 
   11691   return true;
   11692 }
   11693 
   11694 static bool
   11695 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   11696 {
   11697   size_t offset;
   11698   size_t size;
   11699   size_t min_size;
   11700 
   11701   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   11702      Also compute minimum size of this note.  */
   11703   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11704     {
   11705     case ELFCLASS32:
   11706       offset = 4 + 4;
   11707       min_size = offset + (4 * 2) + 4 + 4 + 4;
   11708       break;
   11709 
   11710     case ELFCLASS64:
   11711       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   11712       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   11713       break;
   11714 
   11715     default:
   11716       return false;
   11717     }
   11718 
   11719   if (note->descsz < min_size)
   11720     return false;
   11721 
   11722   /* Check for version 1 in pr_version.  */
   11723   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11724     return false;
   11725 
   11726   /* Extract size of pr_reg from pr_gregsetsz.  */
   11727   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   11728   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11729     {
   11730       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11731       offset += 4 * 2;
   11732     }
   11733   else
   11734     {
   11735       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   11736       offset += 8 * 2;
   11737     }
   11738 
   11739   /* Skip over pr_osreldate.  */
   11740   offset += 4;
   11741 
   11742   /* Read signal from pr_cursig.  */
   11743   if (elf_tdata (abfd)->core->signal == 0)
   11744     elf_tdata (abfd)->core->signal
   11745       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11746   offset += 4;
   11747 
   11748   /* Read TID from pr_pid.  */
   11749   elf_tdata (abfd)->core->lwpid
   11750       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11751   offset += 4;
   11752 
   11753   /* Padding before pr_reg.  */
   11754   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11755     offset += 4;
   11756 
   11757   /* Make sure that there is enough data remaining in the note.  */
   11758   if ((note->descsz - offset) < size)
   11759     return false;
   11760 
   11761   /* Make a ".reg/999" section and a ".reg" section.  */
   11762   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   11763 					  size, note->descpos + offset);
   11764 }
   11765 
   11766 static bool
   11767 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11768 {
   11769   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11770 
   11771   switch (note->type)
   11772     {
   11773     case NT_PRSTATUS:
   11774       if (bed->elf_backend_grok_freebsd_prstatus)
   11775 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11776 	  return true;
   11777       return elfcore_grok_freebsd_prstatus (abfd, note);
   11778 
   11779     case NT_FPREGSET:
   11780       return elfcore_grok_prfpreg (abfd, note);
   11781 
   11782     case NT_PRPSINFO:
   11783       return elfcore_grok_freebsd_psinfo (abfd, note);
   11784 
   11785     case NT_FREEBSD_THRMISC:
   11786       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11787 
   11788     case NT_FREEBSD_PROCSTAT_PROC:
   11789       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11790 					      note);
   11791 
   11792     case NT_FREEBSD_PROCSTAT_FILES:
   11793       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11794 					      note);
   11795 
   11796     case NT_FREEBSD_PROCSTAT_VMMAP:
   11797       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11798 					      note);
   11799 
   11800     case NT_FREEBSD_PROCSTAT_AUXV:
   11801       return elfcore_make_auxv_note_section (abfd, note, 4);
   11802 
   11803     case NT_FREEBSD_X86_SEGBASES:
   11804       return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
   11805 
   11806     case NT_X86_XSTATE:
   11807       return elfcore_grok_xstatereg (abfd, note);
   11808 
   11809     case NT_FREEBSD_PTLWPINFO:
   11810       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11811 					      note);
   11812 
   11813     case NT_ARM_TLS:
   11814       return elfcore_grok_aarch_tls (abfd, note);
   11815 
   11816     case NT_ARM_VFP:
   11817       return elfcore_grok_arm_vfp (abfd, note);
   11818 
   11819     default:
   11820       return true;
   11821     }
   11822 }
   11823 
   11824 static bool
   11825 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11826 {
   11827   char *cp;
   11828 
   11829   cp = strchr (note->namedata, '@');
   11830   if (cp != NULL)
   11831     {
   11832       *lwpidp = atoi(cp + 1);
   11833       return true;
   11834     }
   11835   return false;
   11836 }
   11837 
   11838 static bool
   11839 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11840 {
   11841   if (note->descsz <= 0x7c + 31)
   11842     return false;
   11843 
   11844   /* Signal number at offset 0x08. */
   11845   elf_tdata (abfd)->core->signal
   11846     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11847 
   11848   /* Process ID at offset 0x50. */
   11849   elf_tdata (abfd)->core->pid
   11850     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11851 
   11852   /* Command name at 0x7c (max 32 bytes, including nul). */
   11853   elf_tdata (abfd)->core->command
   11854     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11855 
   11856   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11857 					  note);
   11858 }
   11859 
   11860 static bool
   11861 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11862 {
   11863   int lwp;
   11864 
   11865   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11866     elf_tdata (abfd)->core->lwpid = lwp;
   11867 
   11868   switch (note->type)
   11869     {
   11870     case NT_NETBSDCORE_PROCINFO:
   11871       /* NetBSD-specific core "procinfo".  Note that we expect to
   11872 	 find this note before any of the others, which is fine,
   11873 	 since the kernel writes this note out first when it
   11874 	 creates a core file.  */
   11875       return elfcore_grok_netbsd_procinfo (abfd, note);
   11876     case NT_NETBSDCORE_AUXV:
   11877       /* NetBSD-specific Elf Auxiliary Vector data. */
   11878       return elfcore_make_auxv_note_section (abfd, note, 4);
   11879     case NT_NETBSDCORE_LWPSTATUS:
   11880       return elfcore_make_note_pseudosection (abfd,
   11881 					      ".note.netbsdcore.lwpstatus",
   11882 					      note);
   11883     default:
   11884       break;
   11885     }
   11886 
   11887   /* As of March 2020 there are no other machine-independent notes
   11888      defined for NetBSD core files.  If the note type is less
   11889      than the start of the machine-dependent note types, we don't
   11890      understand it.  */
   11891 
   11892   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11893     return true;
   11894 
   11895 
   11896   switch (bfd_get_arch (abfd))
   11897     {
   11898       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11899 	 PT_GETFPREGS == mach+2.  */
   11900 
   11901     case bfd_arch_aarch64:
   11902     case bfd_arch_alpha:
   11903     case bfd_arch_sparc:
   11904       switch (note->type)
   11905 	{
   11906 	case NT_NETBSDCORE_FIRSTMACH+0:
   11907 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11908 
   11909 	case NT_NETBSDCORE_FIRSTMACH+2:
   11910 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11911 
   11912 	default:
   11913 	  return true;
   11914 	}
   11915 
   11916       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11917 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11918 	 structure which lacks GBR.  */
   11919 
   11920     case bfd_arch_sh:
   11921       switch (note->type)
   11922 	{
   11923 	case NT_NETBSDCORE_FIRSTMACH+3:
   11924 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11925 
   11926 	case NT_NETBSDCORE_FIRSTMACH+5:
   11927 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11928 
   11929 	default:
   11930 	  return true;
   11931 	}
   11932 
   11933       /* On all other arch's, PT_GETREGS == mach+1 and
   11934 	 PT_GETFPREGS == mach+3.  */
   11935 
   11936     default:
   11937       switch (note->type)
   11938 	{
   11939 	case NT_NETBSDCORE_FIRSTMACH+1:
   11940 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11941 
   11942 	case NT_NETBSDCORE_FIRSTMACH+3:
   11943 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11944 
   11945 	default:
   11946 	  return true;
   11947 	}
   11948     }
   11949     /* NOTREACHED */
   11950 }
   11951 
   11952 static bool
   11953 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11954 {
   11955   if (note->descsz <= 0x48 + 31)
   11956     return false;
   11957 
   11958   /* Signal number at offset 0x08. */
   11959   elf_tdata (abfd)->core->signal
   11960     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11961 
   11962   /* Process ID at offset 0x20. */
   11963   elf_tdata (abfd)->core->pid
   11964     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11965 
   11966   /* Command name at 0x48 (max 32 bytes, including nul). */
   11967   elf_tdata (abfd)->core->command
   11968     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11969 
   11970   return true;
   11971 }
   11972 
   11973 /* Processes Solaris's process status note.
   11974    sig_off ~ offsetof(prstatus_t, pr_cursig)
   11975    pid_off ~ offsetof(prstatus_t, pr_pid)
   11976    lwpid_off ~ offsetof(prstatus_t, pr_who)
   11977    gregset_size ~ sizeof(gregset_t)
   11978    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   11979 
   11980 static bool
   11981 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   11982 			       int pid_off, int lwpid_off, size_t gregset_size,
   11983 			       size_t gregset_offset)
   11984 {
   11985   asection *sect = NULL;
   11986   elf_tdata (abfd)->core->signal
   11987     = bfd_get_16 (abfd, note->descdata + sig_off);
   11988   elf_tdata (abfd)->core->pid
   11989     = bfd_get_32 (abfd, note->descdata + pid_off);
   11990   elf_tdata (abfd)->core->lwpid
   11991     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   11992 
   11993   sect = bfd_get_section_by_name (abfd, ".reg");
   11994   if (sect != NULL)
   11995     sect->size = gregset_size;
   11996 
   11997   return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11998 					  note->descpos + gregset_offset);
   11999 }
   12000 
   12001 /* Gets program and arguments from a core.
   12002    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   12003    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   12004 
   12005 static bool
   12006 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   12007 			  int prog_off, int comm_off)
   12008 {
   12009   elf_tdata (abfd)->core->program
   12010     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   12011   elf_tdata (abfd)->core->command
   12012     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   12013 
   12014   return true;
   12015 }
   12016 
   12017 /* Processes Solaris's LWP status note.
   12018    gregset_size ~ sizeof(gregset_t)
   12019    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   12020    fpregset_size ~ sizeof(fpregset_t)
   12021    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   12022 
   12023 static bool
   12024 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   12025 				size_t gregset_size, int gregset_off,
   12026 				size_t fpregset_size, int fpregset_off)
   12027 {
   12028   asection *sect = NULL;
   12029   char reg2_section_name[16] = { 0 };
   12030 
   12031   (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
   12032 		   elf_tdata (abfd)->core->lwpid);
   12033 
   12034   /* offsetof(lwpstatus_t, pr_lwpid) */
   12035   elf_tdata (abfd)->core->lwpid
   12036     = bfd_get_32 (abfd, note->descdata + 4);
   12037   /* offsetof(lwpstatus_t, pr_cursig) */
   12038   elf_tdata (abfd)->core->signal
   12039     = bfd_get_16 (abfd, note->descdata + 12);
   12040 
   12041   sect = bfd_get_section_by_name (abfd, ".reg");
   12042   if (sect != NULL)
   12043     sect->size = gregset_size;
   12044   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   12045 					     note->descpos + gregset_off))
   12046     return false;
   12047 
   12048   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   12049   if (sect != NULL)
   12050     {
   12051       sect->size = fpregset_size;
   12052       sect->filepos = note->descpos + fpregset_off;
   12053       sect->alignment_power = 2;
   12054     }
   12055   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
   12056 					     note->descpos + fpregset_off))
   12057     return false;
   12058 
   12059   return true;
   12060 }
   12061 
   12062 static bool
   12063 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   12064 {
   12065   if (note == NULL)
   12066     return false;
   12067 
   12068   /* core files are identified as 32- or 64-bit, SPARC or x86,
   12069      by the size of the descsz which matches the sizeof()
   12070      the type appropriate for that note type (e.g., prstatus_t for
   12071      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   12072      on Solaris. The core file bitness may differ from the bitness of
   12073      gdb itself, so fixed values are used instead of sizeof().
   12074      Appropriate fixed offsets are also used to obtain data from
   12075      the note.  */
   12076 
   12077   switch ((int) note->type)
   12078     {
   12079     case SOLARIS_NT_PRSTATUS:
   12080       switch (note->descsz)
   12081 	{
   12082 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   12083 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12084 					       136, 216, 308, 152, 356);
   12085 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   12086 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12087 					       264, 360, 520, 304, 600);
   12088 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   12089 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12090 					       136, 216, 308, 76, 356);
   12091 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   12092 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12093 					       264, 360, 520, 224, 600);
   12094 	default:
   12095 	  return true;
   12096 	}
   12097 
   12098     case SOLARIS_NT_PSINFO:
   12099     case SOLARIS_NT_PRPSINFO:
   12100       switch (note->descsz)
   12101 	{
   12102 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   12103 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   12104 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   12105 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   12106 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   12107 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   12108 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   12109 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   12110 	default:
   12111 	  return true;
   12112 	}
   12113 
   12114     case SOLARIS_NT_LWPSTATUS:
   12115       switch (note->descsz)
   12116 	{
   12117 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   12118 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12119 						152, 344, 400, 496);
   12120 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   12121 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12122 						304, 544, 544, 848);
   12123 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   12124 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12125 						76, 344, 380, 420);
   12126 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   12127 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12128 						224, 544, 528, 768);
   12129 	default:
   12130 	  return true;
   12131 	}
   12132 
   12133     case SOLARIS_NT_LWPSINFO:
   12134       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   12135       if (note->descsz == 128 || note->descsz == 152)
   12136 	elf_tdata (abfd)->core->lwpid =
   12137 	  bfd_get_32 (abfd, note->descdata + 4);
   12138       break;
   12139 
   12140     default:
   12141       break;
   12142     }
   12143 
   12144   return true;
   12145 }
   12146 
   12147 /* For name starting with "CORE" this may be either a Solaris
   12148    core file or a gdb-generated core file.  Do Solaris-specific
   12149    processing on selected note types first with
   12150    elfcore_grok_solaris_note(), then process the note
   12151    in elfcore_grok_note().  */
   12152 
   12153 static bool
   12154 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   12155 {
   12156   if (!elfcore_grok_solaris_note_impl (abfd, note))
   12157     return false;
   12158 
   12159   return elfcore_grok_note (abfd, note);
   12160 }
   12161 
   12162 static bool
   12163 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   12164 {
   12165   if (note->type == NT_OPENBSD_PROCINFO)
   12166     return elfcore_grok_openbsd_procinfo (abfd, note);
   12167 
   12168   if (note->type == NT_OPENBSD_REGS)
   12169     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   12170 
   12171   if (note->type == NT_OPENBSD_FPREGS)
   12172     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   12173 
   12174   if (note->type == NT_OPENBSD_XFPREGS)
   12175     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   12176 
   12177   if (note->type == NT_OPENBSD_AUXV)
   12178     return elfcore_make_auxv_note_section (abfd, note, 0);
   12179 
   12180   if (note->type == NT_OPENBSD_WCOOKIE)
   12181     {
   12182       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   12183 							   SEC_HAS_CONTENTS);
   12184 
   12185       if (sect == NULL)
   12186 	return false;
   12187       sect->size = note->descsz;
   12188       sect->filepos = note->descpos;
   12189       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   12190 
   12191       return true;
   12192     }
   12193 
   12194   return true;
   12195 }
   12196 
   12197 static bool
   12198 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   12199 {
   12200   void *ddata = note->descdata;
   12201   char buf[100];
   12202   char *name;
   12203   asection *sect;
   12204   short sig;
   12205   unsigned flags;
   12206 
   12207   if (note->descsz < 16)
   12208     return false;
   12209 
   12210   /* nto_procfs_status 'pid' field is at offset 0.  */
   12211   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   12212 
   12213   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   12214   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   12215 
   12216   /* nto_procfs_status 'flags' field is at offset 8.  */
   12217   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   12218 
   12219   /* nto_procfs_status 'what' field is at offset 14.  */
   12220   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   12221     {
   12222       elf_tdata (abfd)->core->signal = sig;
   12223       elf_tdata (abfd)->core->lwpid = *tid;
   12224     }
   12225 
   12226   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   12227      do not come from signals so we make sure we set the current
   12228      thread just in case.  */
   12229   if (flags & 0x00000080)
   12230     elf_tdata (abfd)->core->lwpid = *tid;
   12231 
   12232   /* Make a ".qnx_core_status/%d" section.  */
   12233   sprintf (buf, ".qnx_core_status/%ld", *tid);
   12234 
   12235   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   12236   if (name == NULL)
   12237     return false;
   12238   strcpy (name, buf);
   12239 
   12240   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12241   if (sect == NULL)
   12242     return false;
   12243 
   12244   sect->size		= note->descsz;
   12245   sect->filepos		= note->descpos;
   12246   sect->alignment_power = 2;
   12247 
   12248   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   12249 }
   12250 
   12251 static bool
   12252 elfcore_grok_nto_regs (bfd *abfd,
   12253 		       Elf_Internal_Note *note,
   12254 		       long tid,
   12255 		       char *base)
   12256 {
   12257   char buf[100];
   12258   char *name;
   12259   asection *sect;
   12260 
   12261   /* Make a "(base)/%d" section.  */
   12262   sprintf (buf, "%s/%ld", base, tid);
   12263 
   12264   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   12265   if (name == NULL)
   12266     return false;
   12267   strcpy (name, buf);
   12268 
   12269   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12270   if (sect == NULL)
   12271     return false;
   12272 
   12273   sect->size		= note->descsz;
   12274   sect->filepos		= note->descpos;
   12275   sect->alignment_power = 2;
   12276 
   12277   /* This is the current thread.  */
   12278   if (elf_tdata (abfd)->core->lwpid == tid)
   12279     return elfcore_maybe_make_sect (abfd, base, sect);
   12280 
   12281   return true;
   12282 }
   12283 
   12284 static bool
   12285 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   12286 {
   12287   /* Every GREG section has a STATUS section before it.  Store the
   12288      tid from the previous call to pass down to the next gregs
   12289      function.  */
   12290   static long tid = 1;
   12291 
   12292   switch (note->type)
   12293     {
   12294     case QNT_CORE_INFO:
   12295       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   12296     case QNT_CORE_STATUS:
   12297       return elfcore_grok_nto_status (abfd, note, &tid);
   12298     case QNT_CORE_GREG:
   12299       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   12300     case QNT_CORE_FPREG:
   12301       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   12302     default:
   12303       return true;
   12304     }
   12305 }
   12306 
   12307 static bool
   12308 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   12309 {
   12310   char *name;
   12311   asection *sect;
   12312   size_t len;
   12313 
   12314   /* Use note name as section name.  */
   12315   len = note->namesz;
   12316   name = (char *) bfd_alloc (abfd, len);
   12317   if (name == NULL)
   12318     return false;
   12319   memcpy (name, note->namedata, len);
   12320   name[len - 1] = '\0';
   12321 
   12322   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12323   if (sect == NULL)
   12324     return false;
   12325 
   12326   sect->size		= note->descsz;
   12327   sect->filepos		= note->descpos;
   12328   sect->alignment_power = 1;
   12329 
   12330   return true;
   12331 }
   12332 
   12333 /* Function: elfcore_write_note
   12334 
   12335    Inputs:
   12336      buffer to hold note, and current size of buffer
   12337      name of note
   12338      type of note
   12339      data for note
   12340      size of data for note
   12341 
   12342    Writes note to end of buffer.  ELF64 notes are written exactly as
   12343    for ELF32, despite the current (as of 2006) ELF gabi specifying
   12344    that they ought to have 8-byte namesz and descsz field, and have
   12345    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   12346 
   12347    Return:
   12348    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   12349 
   12350 char *
   12351 elfcore_write_note (bfd *abfd,
   12352 		    char *buf,
   12353 		    int *bufsiz,
   12354 		    const char *name,
   12355 		    int type,
   12356 		    const void *input,
   12357 		    int size)
   12358 {
   12359   Elf_External_Note *xnp;
   12360   size_t namesz;
   12361   size_t newspace;
   12362   char *dest;
   12363 
   12364   namesz = 0;
   12365   if (name != NULL)
   12366     namesz = strlen (name) + 1;
   12367 
   12368   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   12369 
   12370   buf = (char *) realloc (buf, *bufsiz + newspace);
   12371   if (buf == NULL)
   12372     return buf;
   12373   dest = buf + *bufsiz;
   12374   *bufsiz += newspace;
   12375   xnp = (Elf_External_Note *) dest;
   12376   H_PUT_32 (abfd, namesz, xnp->namesz);
   12377   H_PUT_32 (abfd, size, xnp->descsz);
   12378   H_PUT_32 (abfd, type, xnp->type);
   12379   dest = xnp->name;
   12380   if (name != NULL)
   12381     {
   12382       memcpy (dest, name, namesz);
   12383       dest += namesz;
   12384       while (namesz & 3)
   12385 	{
   12386 	  *dest++ = '\0';
   12387 	  ++namesz;
   12388 	}
   12389     }
   12390   memcpy (dest, input, size);
   12391   dest += size;
   12392   while (size & 3)
   12393     {
   12394       *dest++ = '\0';
   12395       ++size;
   12396     }
   12397   return buf;
   12398 }
   12399 
   12400 /* gcc-8 warns (*) on all the strncpy calls in this function about
   12401    possible string truncation.  The "truncation" is not a bug.  We
   12402    have an external representation of structs with fields that are not
   12403    necessarily NULL terminated and corresponding internal
   12404    representation fields that are one larger so that they can always
   12405    be NULL terminated.
   12406    gcc versions between 4.2 and 4.6 do not allow pragma control of
   12407    diagnostics inside functions, giving a hard error if you try to use
   12408    the finer control available with later versions.
   12409    gcc prior to 4.2 warns about diagnostic push and pop.
   12410    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   12411    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   12412    (*) Depending on your system header files!  */
   12413 #if GCC_VERSION >= 8000
   12414 # pragma GCC diagnostic push
   12415 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   12416 #endif
   12417 char *
   12418 elfcore_write_prpsinfo (bfd  *abfd,
   12419 			char *buf,
   12420 			int  *bufsiz,
   12421 			const char *fname,
   12422 			const char *psargs)
   12423 {
   12424   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12425 
   12426   if (bed->elf_backend_write_core_note != NULL)
   12427     {
   12428       char *ret;
   12429       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12430 						 NT_PRPSINFO, fname, psargs);
   12431       if (ret != NULL)
   12432 	return ret;
   12433     }
   12434 
   12435 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   12436 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   12437   if (bed->s->elfclass == ELFCLASS32)
   12438     {
   12439 #  if defined (HAVE_PSINFO32_T)
   12440       psinfo32_t data;
   12441       int note_type = NT_PSINFO;
   12442 #  else
   12443       prpsinfo32_t data;
   12444       int note_type = NT_PRPSINFO;
   12445 #  endif
   12446 
   12447       memset (&data, 0, sizeof (data));
   12448       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12449       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12450       return elfcore_write_note (abfd, buf, bufsiz,
   12451 				 "CORE", note_type, &data, sizeof (data));
   12452     }
   12453   else
   12454 # endif
   12455     {
   12456 # if defined (HAVE_PSINFO_T)
   12457       psinfo_t data;
   12458       int note_type = NT_PSINFO;
   12459 # else
   12460       prpsinfo_t data;
   12461       int note_type = NT_PRPSINFO;
   12462 # endif
   12463 
   12464       memset (&data, 0, sizeof (data));
   12465       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12466       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12467       return elfcore_write_note (abfd, buf, bufsiz,
   12468 				 "CORE", note_type, &data, sizeof (data));
   12469     }
   12470 #endif	/* PSINFO_T or PRPSINFO_T */
   12471 
   12472   free (buf);
   12473   return NULL;
   12474 }
   12475 #if GCC_VERSION >= 8000
   12476 # pragma GCC diagnostic pop
   12477 #endif
   12478 
   12479 char *
   12480 elfcore_write_linux_prpsinfo32
   12481   (bfd *abfd, char *buf, int *bufsiz,
   12482    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12483 {
   12484   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   12485     {
   12486       struct elf_external_linux_prpsinfo32_ugid16 data;
   12487 
   12488       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   12489       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   12490 				 &data, sizeof (data));
   12491     }
   12492   else
   12493     {
   12494       struct elf_external_linux_prpsinfo32_ugid32 data;
   12495 
   12496       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   12497       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   12498 				 &data, sizeof (data));
   12499     }
   12500 }
   12501 
   12502 char *
   12503 elfcore_write_linux_prpsinfo64
   12504   (bfd *abfd, char *buf, int *bufsiz,
   12505    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12506 {
   12507   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   12508     {
   12509       struct elf_external_linux_prpsinfo64_ugid16 data;
   12510 
   12511       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   12512       return elfcore_write_note (abfd, buf, bufsiz,
   12513 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   12514     }
   12515   else
   12516     {
   12517       struct elf_external_linux_prpsinfo64_ugid32 data;
   12518 
   12519       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   12520       return elfcore_write_note (abfd, buf, bufsiz,
   12521 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   12522     }
   12523 }
   12524 
   12525 char *
   12526 elfcore_write_prstatus (bfd *abfd,
   12527 			char *buf,
   12528 			int *bufsiz,
   12529 			long pid,
   12530 			int cursig,
   12531 			const void *gregs)
   12532 {
   12533   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12534 
   12535   if (bed->elf_backend_write_core_note != NULL)
   12536     {
   12537       char *ret;
   12538       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12539 						 NT_PRSTATUS,
   12540 						 pid, cursig, gregs);
   12541       if (ret != NULL)
   12542 	return ret;
   12543     }
   12544 
   12545 #if defined (HAVE_PRSTATUS_T)
   12546 #if defined (HAVE_PRSTATUS32_T)
   12547   if (bed->s->elfclass == ELFCLASS32)
   12548     {
   12549       prstatus32_t prstat;
   12550 
   12551       memset (&prstat, 0, sizeof (prstat));
   12552       prstat.pr_pid = pid;
   12553       prstat.pr_cursig = cursig;
   12554       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12555       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   12556 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12557     }
   12558   else
   12559 #endif
   12560     {
   12561       prstatus_t prstat;
   12562 
   12563       memset (&prstat, 0, sizeof (prstat));
   12564       prstat.pr_pid = pid;
   12565       prstat.pr_cursig = cursig;
   12566       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12567       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   12568 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12569     }
   12570 #endif /* HAVE_PRSTATUS_T */
   12571 
   12572   free (buf);
   12573   return NULL;
   12574 }
   12575 
   12576 #if defined (HAVE_LWPSTATUS_T)
   12577 char *
   12578 elfcore_write_lwpstatus (bfd *abfd,
   12579 			 char *buf,
   12580 			 int *bufsiz,
   12581 			 long pid,
   12582 			 int cursig,
   12583 			 const void *gregs)
   12584 {
   12585   lwpstatus_t lwpstat;
   12586   const char *note_name = "CORE";
   12587 
   12588   memset (&lwpstat, 0, sizeof (lwpstat));
   12589   lwpstat.pr_lwpid  = pid >> 16;
   12590   lwpstat.pr_cursig = cursig;
   12591 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   12592   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   12593 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   12594 #if !defined(gregs)
   12595   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   12596 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   12597 #else
   12598   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   12599 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   12600 #endif
   12601 #endif
   12602   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   12603 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   12604 }
   12605 #endif /* HAVE_LWPSTATUS_T */
   12606 
   12607 #if defined (HAVE_PSTATUS_T)
   12608 char *
   12609 elfcore_write_pstatus (bfd *abfd,
   12610 		       char *buf,
   12611 		       int *bufsiz,
   12612 		       long pid,
   12613 		       int cursig ATTRIBUTE_UNUSED,
   12614 		       const void *gregs ATTRIBUTE_UNUSED)
   12615 {
   12616   const char *note_name = "CORE";
   12617 #if defined (HAVE_PSTATUS32_T)
   12618   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12619 
   12620   if (bed->s->elfclass == ELFCLASS32)
   12621     {
   12622       pstatus32_t pstat;
   12623 
   12624       memset (&pstat, 0, sizeof (pstat));
   12625       pstat.pr_pid = pid & 0xffff;
   12626       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   12627 				NT_PSTATUS, &pstat, sizeof (pstat));
   12628       return buf;
   12629     }
   12630   else
   12631 #endif
   12632     {
   12633       pstatus_t pstat;
   12634 
   12635       memset (&pstat, 0, sizeof (pstat));
   12636       pstat.pr_pid = pid & 0xffff;
   12637       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   12638 				NT_PSTATUS, &pstat, sizeof (pstat));
   12639       return buf;
   12640     }
   12641 }
   12642 #endif /* HAVE_PSTATUS_T */
   12643 
   12644 char *
   12645 elfcore_write_prfpreg (bfd *abfd,
   12646 		       char *buf,
   12647 		       int *bufsiz,
   12648 		       const void *fpregs,
   12649 		       int size)
   12650 {
   12651   const char *note_name = "CORE";
   12652   return elfcore_write_note (abfd, buf, bufsiz,
   12653 			     note_name, NT_FPREGSET, fpregs, size);
   12654 }
   12655 
   12656 char *
   12657 elfcore_write_prxfpreg (bfd *abfd,
   12658 			char *buf,
   12659 			int *bufsiz,
   12660 			const void *xfpregs,
   12661 			int size)
   12662 {
   12663   char *note_name = "LINUX";
   12664   return elfcore_write_note (abfd, buf, bufsiz,
   12665 			     note_name, NT_PRXFPREG, xfpregs, size);
   12666 }
   12667 
   12668 char *
   12669 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   12670 			 const void *xfpregs, int size)
   12671 {
   12672   char *note_name;
   12673   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   12674     note_name = "FreeBSD";
   12675   else
   12676     note_name = "LINUX";
   12677   return elfcore_write_note (abfd, buf, bufsiz,
   12678 			     note_name, NT_X86_XSTATE, xfpregs, size);
   12679 }
   12680 
   12681 char *
   12682 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   12683 			    const void *regs, int size)
   12684 {
   12685   char *note_name = "FreeBSD";
   12686   return elfcore_write_note (abfd, buf, bufsiz,
   12687 			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
   12688 }
   12689 
   12690 char *
   12691 elfcore_write_ppc_vmx (bfd *abfd,
   12692 		       char *buf,
   12693 		       int *bufsiz,
   12694 		       const void *ppc_vmx,
   12695 		       int size)
   12696 {
   12697   char *note_name = "LINUX";
   12698   return elfcore_write_note (abfd, buf, bufsiz,
   12699 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   12700 }
   12701 
   12702 char *
   12703 elfcore_write_ppc_vsx (bfd *abfd,
   12704 		       char *buf,
   12705 		       int *bufsiz,
   12706 		       const void *ppc_vsx,
   12707 		       int size)
   12708 {
   12709   char *note_name = "LINUX";
   12710   return elfcore_write_note (abfd, buf, bufsiz,
   12711 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   12712 }
   12713 
   12714 char *
   12715 elfcore_write_ppc_tar (bfd *abfd,
   12716 		       char *buf,
   12717 		       int *bufsiz,
   12718 		       const void *ppc_tar,
   12719 		       int size)
   12720 {
   12721   char *note_name = "LINUX";
   12722   return elfcore_write_note (abfd, buf, bufsiz,
   12723 			     note_name, NT_PPC_TAR, ppc_tar, size);
   12724 }
   12725 
   12726 char *
   12727 elfcore_write_ppc_ppr (bfd *abfd,
   12728 		       char *buf,
   12729 		       int *bufsiz,
   12730 		       const void *ppc_ppr,
   12731 		       int size)
   12732 {
   12733   char *note_name = "LINUX";
   12734   return elfcore_write_note (abfd, buf, bufsiz,
   12735 			     note_name, NT_PPC_PPR, ppc_ppr, size);
   12736 }
   12737 
   12738 char *
   12739 elfcore_write_ppc_dscr (bfd *abfd,
   12740 			char *buf,
   12741 			int *bufsiz,
   12742 			const void *ppc_dscr,
   12743 			int size)
   12744 {
   12745   char *note_name = "LINUX";
   12746   return elfcore_write_note (abfd, buf, bufsiz,
   12747 			     note_name, NT_PPC_DSCR, ppc_dscr, size);
   12748 }
   12749 
   12750 char *
   12751 elfcore_write_ppc_ebb (bfd *abfd,
   12752 		       char *buf,
   12753 		       int *bufsiz,
   12754 		       const void *ppc_ebb,
   12755 		       int size)
   12756 {
   12757   char *note_name = "LINUX";
   12758   return elfcore_write_note (abfd, buf, bufsiz,
   12759 			     note_name, NT_PPC_EBB, ppc_ebb, size);
   12760 }
   12761 
   12762 char *
   12763 elfcore_write_ppc_pmu (bfd *abfd,
   12764 		       char *buf,
   12765 		       int *bufsiz,
   12766 		       const void *ppc_pmu,
   12767 		       int size)
   12768 {
   12769   char *note_name = "LINUX";
   12770   return elfcore_write_note (abfd, buf, bufsiz,
   12771 			     note_name, NT_PPC_PMU, ppc_pmu, size);
   12772 }
   12773 
   12774 char *
   12775 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12776 			   char *buf,
   12777 			   int *bufsiz,
   12778 			   const void *ppc_tm_cgpr,
   12779 			   int size)
   12780 {
   12781   char *note_name = "LINUX";
   12782   return elfcore_write_note (abfd, buf, bufsiz,
   12783 			     note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
   12784 }
   12785 
   12786 char *
   12787 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12788 			   char *buf,
   12789 			   int *bufsiz,
   12790 			   const void *ppc_tm_cfpr,
   12791 			   int size)
   12792 {
   12793   char *note_name = "LINUX";
   12794   return elfcore_write_note (abfd, buf, bufsiz,
   12795 			     note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
   12796 }
   12797 
   12798 char *
   12799 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12800 			   char *buf,
   12801 			   int *bufsiz,
   12802 			   const void *ppc_tm_cvmx,
   12803 			   int size)
   12804 {
   12805   char *note_name = "LINUX";
   12806   return elfcore_write_note (abfd, buf, bufsiz,
   12807 			     note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
   12808 }
   12809 
   12810 char *
   12811 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12812 			   char *buf,
   12813 			   int *bufsiz,
   12814 			   const void *ppc_tm_cvsx,
   12815 			   int size)
   12816 {
   12817   char *note_name = "LINUX";
   12818   return elfcore_write_note (abfd, buf, bufsiz,
   12819 			     note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
   12820 }
   12821 
   12822 char *
   12823 elfcore_write_ppc_tm_spr (bfd *abfd,
   12824 			  char *buf,
   12825 			  int *bufsiz,
   12826 			  const void *ppc_tm_spr,
   12827 			  int size)
   12828 {
   12829   char *note_name = "LINUX";
   12830   return elfcore_write_note (abfd, buf, bufsiz,
   12831 			     note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
   12832 }
   12833 
   12834 char *
   12835 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12836 			   char *buf,
   12837 			   int *bufsiz,
   12838 			   const void *ppc_tm_ctar,
   12839 			   int size)
   12840 {
   12841   char *note_name = "LINUX";
   12842   return elfcore_write_note (abfd, buf, bufsiz,
   12843 			     note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
   12844 }
   12845 
   12846 char *
   12847 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12848 			   char *buf,
   12849 			   int *bufsiz,
   12850 			   const void *ppc_tm_cppr,
   12851 			   int size)
   12852 {
   12853   char *note_name = "LINUX";
   12854   return elfcore_write_note (abfd, buf, bufsiz,
   12855 			     note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
   12856 }
   12857 
   12858 char *
   12859 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12860 			    char *buf,
   12861 			    int *bufsiz,
   12862 			    const void *ppc_tm_cdscr,
   12863 			    int size)
   12864 {
   12865   char *note_name = "LINUX";
   12866   return elfcore_write_note (abfd, buf, bufsiz,
   12867 			     note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
   12868 }
   12869 
   12870 static char *
   12871 elfcore_write_s390_high_gprs (bfd *abfd,
   12872 			      char *buf,
   12873 			      int *bufsiz,
   12874 			      const void *s390_high_gprs,
   12875 			      int size)
   12876 {
   12877   char *note_name = "LINUX";
   12878   return elfcore_write_note (abfd, buf, bufsiz,
   12879 			     note_name, NT_S390_HIGH_GPRS,
   12880 			     s390_high_gprs, size);
   12881 }
   12882 
   12883 char *
   12884 elfcore_write_s390_timer (bfd *abfd,
   12885 			  char *buf,
   12886 			  int *bufsiz,
   12887 			  const void *s390_timer,
   12888 			  int size)
   12889 {
   12890   char *note_name = "LINUX";
   12891   return elfcore_write_note (abfd, buf, bufsiz,
   12892 			     note_name, NT_S390_TIMER, s390_timer, size);
   12893 }
   12894 
   12895 char *
   12896 elfcore_write_s390_todcmp (bfd *abfd,
   12897 			   char *buf,
   12898 			   int *bufsiz,
   12899 			   const void *s390_todcmp,
   12900 			   int size)
   12901 {
   12902   char *note_name = "LINUX";
   12903   return elfcore_write_note (abfd, buf, bufsiz,
   12904 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   12905 }
   12906 
   12907 char *
   12908 elfcore_write_s390_todpreg (bfd *abfd,
   12909 			    char *buf,
   12910 			    int *bufsiz,
   12911 			    const void *s390_todpreg,
   12912 			    int size)
   12913 {
   12914   char *note_name = "LINUX";
   12915   return elfcore_write_note (abfd, buf, bufsiz,
   12916 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   12917 }
   12918 
   12919 char *
   12920 elfcore_write_s390_ctrs (bfd *abfd,
   12921 			 char *buf,
   12922 			 int *bufsiz,
   12923 			 const void *s390_ctrs,
   12924 			 int size)
   12925 {
   12926   char *note_name = "LINUX";
   12927   return elfcore_write_note (abfd, buf, bufsiz,
   12928 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   12929 }
   12930 
   12931 char *
   12932 elfcore_write_s390_prefix (bfd *abfd,
   12933 			   char *buf,
   12934 			   int *bufsiz,
   12935 			   const void *s390_prefix,
   12936 			   int size)
   12937 {
   12938   char *note_name = "LINUX";
   12939   return elfcore_write_note (abfd, buf, bufsiz,
   12940 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   12941 }
   12942 
   12943 char *
   12944 elfcore_write_s390_last_break (bfd *abfd,
   12945 			       char *buf,
   12946 			       int *bufsiz,
   12947 			       const void *s390_last_break,
   12948 			       int size)
   12949 {
   12950   char *note_name = "LINUX";
   12951   return elfcore_write_note (abfd, buf, bufsiz,
   12952 			     note_name, NT_S390_LAST_BREAK,
   12953 			     s390_last_break, size);
   12954 }
   12955 
   12956 char *
   12957 elfcore_write_s390_system_call (bfd *abfd,
   12958 				char *buf,
   12959 				int *bufsiz,
   12960 				const void *s390_system_call,
   12961 				int size)
   12962 {
   12963   char *note_name = "LINUX";
   12964   return elfcore_write_note (abfd, buf, bufsiz,
   12965 			     note_name, NT_S390_SYSTEM_CALL,
   12966 			     s390_system_call, size);
   12967 }
   12968 
   12969 char *
   12970 elfcore_write_s390_tdb (bfd *abfd,
   12971 			char *buf,
   12972 			int *bufsiz,
   12973 			const void *s390_tdb,
   12974 			int size)
   12975 {
   12976   char *note_name = "LINUX";
   12977   return elfcore_write_note (abfd, buf, bufsiz,
   12978 			     note_name, NT_S390_TDB, s390_tdb, size);
   12979 }
   12980 
   12981 char *
   12982 elfcore_write_s390_vxrs_low (bfd *abfd,
   12983 			     char *buf,
   12984 			     int *bufsiz,
   12985 			     const void *s390_vxrs_low,
   12986 			     int size)
   12987 {
   12988   char *note_name = "LINUX";
   12989   return elfcore_write_note (abfd, buf, bufsiz,
   12990 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   12991 }
   12992 
   12993 char *
   12994 elfcore_write_s390_vxrs_high (bfd *abfd,
   12995 			     char *buf,
   12996 			     int *bufsiz,
   12997 			     const void *s390_vxrs_high,
   12998 			     int size)
   12999 {
   13000   char *note_name = "LINUX";
   13001   return elfcore_write_note (abfd, buf, bufsiz,
   13002 			     note_name, NT_S390_VXRS_HIGH,
   13003 			     s390_vxrs_high, size);
   13004 }
   13005 
   13006 char *
   13007 elfcore_write_s390_gs_cb (bfd *abfd,
   13008 			  char *buf,
   13009 			  int *bufsiz,
   13010 			  const void *s390_gs_cb,
   13011 			  int size)
   13012 {
   13013   char *note_name = "LINUX";
   13014   return elfcore_write_note (abfd, buf, bufsiz,
   13015 			     note_name, NT_S390_GS_CB,
   13016 			     s390_gs_cb, size);
   13017 }
   13018 
   13019 char *
   13020 elfcore_write_s390_gs_bc (bfd *abfd,
   13021 			  char *buf,
   13022 			  int *bufsiz,
   13023 			  const void *s390_gs_bc,
   13024 			  int size)
   13025 {
   13026   char *note_name = "LINUX";
   13027   return elfcore_write_note (abfd, buf, bufsiz,
   13028 			     note_name, NT_S390_GS_BC,
   13029 			     s390_gs_bc, size);
   13030 }
   13031 
   13032 char *
   13033 elfcore_write_arm_vfp (bfd *abfd,
   13034 		       char *buf,
   13035 		       int *bufsiz,
   13036 		       const void *arm_vfp,
   13037 		       int size)
   13038 {
   13039   char *note_name = "LINUX";
   13040   return elfcore_write_note (abfd, buf, bufsiz,
   13041 			     note_name, NT_ARM_VFP, arm_vfp, size);
   13042 }
   13043 
   13044 char *
   13045 elfcore_write_aarch_tls (bfd *abfd,
   13046 		       char *buf,
   13047 		       int *bufsiz,
   13048 		       const void *aarch_tls,
   13049 		       int size)
   13050 {
   13051   char *note_name = "LINUX";
   13052   return elfcore_write_note (abfd, buf, bufsiz,
   13053 			     note_name, NT_ARM_TLS, aarch_tls, size);
   13054 }
   13055 
   13056 char *
   13057 elfcore_write_aarch_hw_break (bfd *abfd,
   13058 			    char *buf,
   13059 			    int *bufsiz,
   13060 			    const void *aarch_hw_break,
   13061 			    int size)
   13062 {
   13063   char *note_name = "LINUX";
   13064   return elfcore_write_note (abfd, buf, bufsiz,
   13065 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   13066 }
   13067 
   13068 char *
   13069 elfcore_write_aarch_hw_watch (bfd *abfd,
   13070 			    char *buf,
   13071 			    int *bufsiz,
   13072 			    const void *aarch_hw_watch,
   13073 			    int size)
   13074 {
   13075   char *note_name = "LINUX";
   13076   return elfcore_write_note (abfd, buf, bufsiz,
   13077 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   13078 }
   13079 
   13080 char *
   13081 elfcore_write_aarch_sve (bfd *abfd,
   13082 			 char *buf,
   13083 			 int *bufsiz,
   13084 			 const void *aarch_sve,
   13085 			 int size)
   13086 {
   13087   char *note_name = "LINUX";
   13088   return elfcore_write_note (abfd, buf, bufsiz,
   13089 			     note_name, NT_ARM_SVE, aarch_sve, size);
   13090 }
   13091 
   13092 char *
   13093 elfcore_write_aarch_pauth (bfd *abfd,
   13094 			   char *buf,
   13095 			   int *bufsiz,
   13096 			   const void *aarch_pauth,
   13097 			   int size)
   13098 {
   13099   char *note_name = "LINUX";
   13100   return elfcore_write_note (abfd, buf, bufsiz,
   13101 			     note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
   13102 }
   13103 
   13104 char *
   13105 elfcore_write_aarch_mte (bfd *abfd,
   13106 				      char *buf,
   13107 				      int *bufsiz,
   13108 				      const void *aarch_mte,
   13109 				      int size)
   13110 {
   13111   char *note_name = "LINUX";
   13112   return elfcore_write_note (abfd, buf, bufsiz,
   13113 			     note_name, NT_ARM_TAGGED_ADDR_CTRL,
   13114 			     aarch_mte,
   13115 			     size);
   13116 }
   13117 
   13118 char *
   13119 elfcore_write_aarch_ssve (bfd *abfd,
   13120 			  char *buf,
   13121 			  int *bufsiz,
   13122 			  const void *aarch_ssve,
   13123 			  int size)
   13124 {
   13125   char *note_name = "LINUX";
   13126   return elfcore_write_note (abfd, buf, bufsiz,
   13127 			     note_name, NT_ARM_SSVE,
   13128 			     aarch_ssve,
   13129 			     size);
   13130 }
   13131 
   13132 char *
   13133 elfcore_write_aarch_za (bfd *abfd,
   13134 			char *buf,
   13135 			int *bufsiz,
   13136 			const void *aarch_za,
   13137 			int size)
   13138 {
   13139   char *note_name = "LINUX";
   13140   return elfcore_write_note (abfd, buf, bufsiz,
   13141 			     note_name, NT_ARM_ZA,
   13142 			     aarch_za,
   13143 			     size);
   13144 }
   13145 
   13146 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
   13147    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   13148    written into.  Return a pointer to the new start of the note buffer, to
   13149    replace BUF which may no longer be valid.  */
   13150 
   13151 char *
   13152 elfcore_write_aarch_zt (bfd *abfd,
   13153 			char *buf,
   13154 			int *bufsiz,
   13155 			const void *aarch_zt,
   13156 			int size)
   13157 {
   13158   char *note_name = "LINUX";
   13159   return elfcore_write_note (abfd, buf, bufsiz,
   13160 			     note_name, NT_ARM_ZT,
   13161 			     aarch_zt,
   13162 			     size);
   13163 }
   13164 
   13165 char *
   13166 elfcore_write_arc_v2 (bfd *abfd,
   13167 		      char *buf,
   13168 		      int *bufsiz,
   13169 		      const void *arc_v2,
   13170 		      int size)
   13171 {
   13172   char *note_name = "LINUX";
   13173   return elfcore_write_note (abfd, buf, bufsiz,
   13174 			     note_name, NT_ARC_V2, arc_v2, size);
   13175 }
   13176 
   13177 char *
   13178 elfcore_write_loongarch_cpucfg (bfd *abfd,
   13179 				char *buf,
   13180 				int *bufsiz,
   13181 				const void *loongarch_cpucfg,
   13182 				int size)
   13183 {
   13184   char *note_name = "LINUX";
   13185   return elfcore_write_note (abfd, buf, bufsiz,
   13186 			     note_name, NT_LARCH_CPUCFG,
   13187 			     loongarch_cpucfg, size);
   13188 }
   13189 
   13190 char *
   13191 elfcore_write_loongarch_lbt (bfd *abfd,
   13192 			     char *buf,
   13193 			     int *bufsiz,
   13194 			     const void *loongarch_lbt,
   13195 			     int size)
   13196 {
   13197   char *note_name = "LINUX";
   13198   return elfcore_write_note (abfd, buf, bufsiz,
   13199 			     note_name, NT_LARCH_LBT, loongarch_lbt, size);
   13200 }
   13201 
   13202 char *
   13203 elfcore_write_loongarch_lsx (bfd *abfd,
   13204 			     char *buf,
   13205 			     int *bufsiz,
   13206 			     const void *loongarch_lsx,
   13207 			     int size)
   13208 {
   13209   char *note_name = "LINUX";
   13210   return elfcore_write_note (abfd, buf, bufsiz,
   13211 			     note_name, NT_LARCH_LSX, loongarch_lsx, size);
   13212 }
   13213 
   13214 char *
   13215 elfcore_write_loongarch_lasx (bfd *abfd,
   13216 			      char *buf,
   13217 			      int *bufsiz,
   13218 			      const void *loongarch_lasx,
   13219 			      int size)
   13220 {
   13221   char *note_name = "LINUX";
   13222   return elfcore_write_note (abfd, buf, bufsiz,
   13223 			     note_name, NT_LARCH_LASX, loongarch_lasx, size);
   13224 }
   13225 
   13226 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   13227    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   13228    written into.  Return a pointer to the new start of the note buffer, to
   13229    replace BUF which may no longer be valid.  */
   13230 
   13231 char *
   13232 elfcore_write_riscv_csr (bfd *abfd,
   13233 			 char *buf,
   13234 			 int *bufsiz,
   13235 			 const void *csrs,
   13236 			 int size)
   13237 {
   13238   const char *note_name = "GDB";
   13239   return elfcore_write_note (abfd, buf, bufsiz,
   13240 			     note_name, NT_RISCV_CSR, csrs, size);
   13241 }
   13242 
   13243 /* Write the target description (a string) pointed to by TDESC, length
   13244    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   13245    note is being written into.  Return a pointer to the new start of the
   13246    note buffer, to replace BUF which may no longer be valid.  */
   13247 
   13248 char *
   13249 elfcore_write_gdb_tdesc (bfd *abfd,
   13250 			 char *buf,
   13251 			 int *bufsiz,
   13252 			 const void *tdesc,
   13253 			 int size)
   13254 {
   13255   const char *note_name = "GDB";
   13256   return elfcore_write_note (abfd, buf, bufsiz,
   13257 			     note_name, NT_GDB_TDESC, tdesc, size);
   13258 }
   13259 
   13260 char *
   13261 elfcore_write_register_note (bfd *abfd,
   13262 			     char *buf,
   13263 			     int *bufsiz,
   13264 			     const char *section,
   13265 			     const void *data,
   13266 			     int size)
   13267 {
   13268   if (strcmp (section, ".reg2") == 0)
   13269     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   13270   if (strcmp (section, ".reg-xfp") == 0)
   13271     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   13272   if (strcmp (section, ".reg-xstate") == 0)
   13273     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   13274   if (strcmp (section, ".reg-x86-segbases") == 0)
   13275     return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
   13276   if (strcmp (section, ".reg-ppc-vmx") == 0)
   13277     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   13278   if (strcmp (section, ".reg-ppc-vsx") == 0)
   13279     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   13280   if (strcmp (section, ".reg-ppc-tar") == 0)
   13281     return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
   13282   if (strcmp (section, ".reg-ppc-ppr") == 0)
   13283     return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
   13284   if (strcmp (section, ".reg-ppc-dscr") == 0)
   13285     return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
   13286   if (strcmp (section, ".reg-ppc-ebb") == 0)
   13287     return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
   13288   if (strcmp (section, ".reg-ppc-pmu") == 0)
   13289     return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
   13290   if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
   13291     return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
   13292   if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
   13293     return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
   13294   if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
   13295     return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
   13296   if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
   13297     return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
   13298   if (strcmp (section, ".reg-ppc-tm-spr") == 0)
   13299     return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
   13300   if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
   13301     return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
   13302   if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
   13303     return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
   13304   if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
   13305     return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
   13306   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   13307     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   13308   if (strcmp (section, ".reg-s390-timer") == 0)
   13309     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   13310   if (strcmp (section, ".reg-s390-todcmp") == 0)
   13311     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   13312   if (strcmp (section, ".reg-s390-todpreg") == 0)
   13313     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   13314   if (strcmp (section, ".reg-s390-ctrs") == 0)
   13315     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   13316   if (strcmp (section, ".reg-s390-prefix") == 0)
   13317     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   13318   if (strcmp (section, ".reg-s390-last-break") == 0)
   13319     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   13320   if (strcmp (section, ".reg-s390-system-call") == 0)
   13321     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   13322   if (strcmp (section, ".reg-s390-tdb") == 0)
   13323     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   13324   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   13325     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   13326   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   13327     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   13328   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   13329     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   13330   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   13331     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   13332   if (strcmp (section, ".reg-arm-vfp") == 0)
   13333     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   13334   if (strcmp (section, ".reg-aarch-tls") == 0)
   13335     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   13336   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   13337     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   13338   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   13339     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   13340   if (strcmp (section, ".reg-aarch-sve") == 0)
   13341     return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
   13342   if (strcmp (section, ".reg-aarch-pauth") == 0)
   13343     return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
   13344   if (strcmp (section, ".reg-aarch-mte") == 0)
   13345     return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
   13346   if (strcmp (section, ".reg-aarch-ssve") == 0)
   13347     return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
   13348   if (strcmp (section, ".reg-aarch-za") == 0)
   13349     return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
   13350   if (strcmp (section, ".reg-aarch-zt") == 0)
   13351     return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
   13352   if (strcmp (section, ".reg-arc-v2") == 0)
   13353     return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
   13354   if (strcmp (section, ".gdb-tdesc") == 0)
   13355     return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
   13356   if (strcmp (section, ".reg-riscv-csr") == 0)
   13357     return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
   13358   if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
   13359     return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
   13360   if (strcmp (section, ".reg-loongarch-lbt") == 0)
   13361     return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
   13362   if (strcmp (section, ".reg-loongarch-lsx") == 0)
   13363     return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
   13364   if (strcmp (section, ".reg-loongarch-lasx") == 0)
   13365     return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
   13366   return NULL;
   13367 }
   13368 
   13369 char *
   13370 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   13371 			 const void *buf, int bufsiz)
   13372 {
   13373   return elfcore_write_note (obfd, note_data, note_size,
   13374 			     "CORE", NT_FILE, buf, bufsiz);
   13375 }
   13376 
   13377 static bool
   13378 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   13379 		 size_t align)
   13380 {
   13381   char *p;
   13382 
   13383   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   13384      gABI specifies that PT_NOTE alignment should be aligned to 4
   13385      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   13386      align is less than 4, we use 4 byte alignment.   */
   13387   if (align < 4)
   13388     align = 4;
   13389   if (align != 4 && align != 8)
   13390     return false;
   13391 
   13392   p = buf;
   13393   while (p < buf + size)
   13394     {
   13395       Elf_External_Note *xnp = (Elf_External_Note *) p;
   13396       Elf_Internal_Note in;
   13397 
   13398       if (offsetof (Elf_External_Note, name) > buf - p + size)
   13399 	return false;
   13400 
   13401       in.type = H_GET_32 (abfd, xnp->type);
   13402 
   13403       in.namesz = H_GET_32 (abfd, xnp->namesz);
   13404       in.namedata = xnp->name;
   13405       if (in.namesz > buf - in.namedata + size)
   13406 	return false;
   13407 
   13408       in.descsz = H_GET_32 (abfd, xnp->descsz);
   13409       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   13410       in.descpos = offset + (in.descdata - buf);
   13411       if (in.descsz != 0
   13412 	  && (in.descdata >= buf + size
   13413 	      || in.descsz > buf - in.descdata + size))
   13414 	return false;
   13415 
   13416       switch (bfd_get_format (abfd))
   13417 	{
   13418 	default:
   13419 	  return true;
   13420 
   13421 	case bfd_core:
   13422 	  {
   13423 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   13424 	    struct
   13425 	    {
   13426 	      const char * string;
   13427 	      size_t len;
   13428 	      bool (*func) (bfd *, Elf_Internal_Note *);
   13429 	    }
   13430 	    grokers[] =
   13431 	    {
   13432 	      GROKER_ELEMENT ("", elfcore_grok_note),
   13433 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   13434 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   13435 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   13436 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   13437 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   13438 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   13439 	      GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
   13440 	    };
   13441 #undef GROKER_ELEMENT
   13442 	    int i;
   13443 
   13444 	    for (i = ARRAY_SIZE (grokers); i--;)
   13445 	      {
   13446 		if (in.namesz >= grokers[i].len
   13447 		    && strncmp (in.namedata, grokers[i].string,
   13448 				grokers[i].len) == 0)
   13449 		  {
   13450 		    if (! grokers[i].func (abfd, & in))
   13451 		      return false;
   13452 		    break;
   13453 		  }
   13454 	      }
   13455 	    break;
   13456 	  }
   13457 
   13458 	case bfd_object:
   13459 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   13460 	    {
   13461 	      if (! elfobj_grok_gnu_note (abfd, &in))
   13462 		return false;
   13463 	    }
   13464 	  else if (in.namesz == sizeof "stapsdt"
   13465 		   && strcmp (in.namedata, "stapsdt") == 0)
   13466 	    {
   13467 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   13468 		return false;
   13469 	    }
   13470 	  break;
   13471 	}
   13472 
   13473       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   13474     }
   13475 
   13476   return true;
   13477 }
   13478 
   13479 bool
   13480 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   13481 		size_t align)
   13482 {
   13483   char *buf;
   13484 
   13485   if (size == 0 || (size + 1) == 0)
   13486     return true;
   13487 
   13488   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   13489     return false;
   13490 
   13491   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   13492   if (buf == NULL)
   13493     return false;
   13494 
   13495   /* PR 17512: file: ec08f814
   13496      0-termintate the buffer so that string searches will not overflow.  */
   13497   buf[size] = 0;
   13498 
   13499   if (!elf_parse_notes (abfd, buf, size, offset, align))
   13500     {
   13501       free (buf);
   13502       return false;
   13503     }
   13504 
   13505   free (buf);
   13506   return true;
   13507 }
   13508 
   13509 /* Providing external access to the ELF program header table.  */
   13511 
   13512 /* Return an upper bound on the number of bytes required to store a
   13513    copy of ABFD's program header table entries.  Return -1 if an error
   13514    occurs; bfd_get_error will return an appropriate code.  */
   13515 
   13516 long
   13517 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   13518 {
   13519   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13520     {
   13521       bfd_set_error (bfd_error_wrong_format);
   13522       return -1;
   13523     }
   13524 
   13525   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   13526 }
   13527 
   13528 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   13529    will be stored as an array of Elf_Internal_Phdr structures, as
   13530    defined in include/elf/internal.h.  To find out how large the
   13531    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   13532 
   13533    Return the number of program header table entries read, or -1 if an
   13534    error occurs; bfd_get_error will return an appropriate code.  */
   13535 
   13536 int
   13537 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   13538 {
   13539   int num_phdrs;
   13540 
   13541   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13542     {
   13543       bfd_set_error (bfd_error_wrong_format);
   13544       return -1;
   13545     }
   13546 
   13547   num_phdrs = elf_elfheader (abfd)->e_phnum;
   13548   if (num_phdrs != 0)
   13549     memcpy (phdrs, elf_tdata (abfd)->phdr,
   13550 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   13551 
   13552   return num_phdrs;
   13553 }
   13554 
   13555 enum elf_reloc_type_class
   13556 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13557 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   13558 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   13559 {
   13560   return reloc_class_normal;
   13561 }
   13562 
   13563 /* For RELA architectures, return the relocation value for a
   13564    relocation against a local symbol.  */
   13565 
   13566 bfd_vma
   13567 _bfd_elf_rela_local_sym (bfd *abfd,
   13568 			 Elf_Internal_Sym *sym,
   13569 			 asection **psec,
   13570 			 Elf_Internal_Rela *rel)
   13571 {
   13572   asection *sec = *psec;
   13573   bfd_vma relocation;
   13574 
   13575   relocation = (sec->output_section->vma
   13576 		+ sec->output_offset
   13577 		+ sym->st_value);
   13578   if ((sec->flags & SEC_MERGE)
   13579       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   13580       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   13581     {
   13582       rel->r_addend =
   13583 	_bfd_merged_section_offset (abfd, psec,
   13584 				    elf_section_data (sec)->sec_info,
   13585 				    sym->st_value + rel->r_addend);
   13586       if (sec != *psec)
   13587 	{
   13588 	  /* If we have changed the section, and our original section is
   13589 	     marked with SEC_EXCLUDE, it means that the original
   13590 	     SEC_MERGE section has been completely subsumed in some
   13591 	     other SEC_MERGE section.  In this case, we need to leave
   13592 	     some info around for --emit-relocs.  */
   13593 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   13594 	    sec->kept_section = *psec;
   13595 	  sec = *psec;
   13596 	}
   13597       rel->r_addend -= relocation;
   13598       rel->r_addend += sec->output_section->vma + sec->output_offset;
   13599     }
   13600   return relocation;
   13601 }
   13602 
   13603 bfd_vma
   13604 _bfd_elf_rel_local_sym (bfd *abfd,
   13605 			Elf_Internal_Sym *sym,
   13606 			asection **psec,
   13607 			bfd_vma addend)
   13608 {
   13609   asection *sec = *psec;
   13610 
   13611   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   13612     return sym->st_value + addend;
   13613 
   13614   return _bfd_merged_section_offset (abfd, psec,
   13615 				     elf_section_data (sec)->sec_info,
   13616 				     sym->st_value + addend);
   13617 }
   13618 
   13619 /* Adjust an address within a section.  Given OFFSET within SEC, return
   13620    the new offset within the section, based upon changes made to the
   13621    section.  Returns -1 if the offset is now invalid.
   13622    The offset (in abnd out) is in target sized bytes, however big a
   13623    byte may be.  */
   13624 
   13625 bfd_vma
   13626 _bfd_elf_section_offset (bfd *abfd,
   13627 			 struct bfd_link_info *info,
   13628 			 asection *sec,
   13629 			 bfd_vma offset)
   13630 {
   13631   switch (sec->sec_info_type)
   13632     {
   13633     case SEC_INFO_TYPE_STABS:
   13634       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   13635 				       offset);
   13636     case SEC_INFO_TYPE_EH_FRAME:
   13637       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   13638 
   13639     default:
   13640       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   13641 	{
   13642 	  /* Reverse the offset.  */
   13643 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13644 	  bfd_size_type address_size = bed->s->arch_size / 8;
   13645 
   13646 	  /* address_size and sec->size are in octets.  Convert
   13647 	     to bytes before subtracting the original offset.  */
   13648 	  offset = ((sec->size - address_size)
   13649 		    / bfd_octets_per_byte (abfd, sec) - offset);
   13650 	}
   13651       return offset;
   13652     }
   13653 }
   13654 
   13655 long
   13657 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   13658 			       long symcount ATTRIBUTE_UNUSED,
   13659 			       asymbol **syms ATTRIBUTE_UNUSED,
   13660 			       long dynsymcount,
   13661 			       asymbol **dynsyms,
   13662 			       asymbol **ret)
   13663 {
   13664   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13665   asection *relplt;
   13666   asymbol *s;
   13667   const char *relplt_name;
   13668   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   13669   arelent *p;
   13670   long count, i, n;
   13671   size_t size;
   13672   Elf_Internal_Shdr *hdr;
   13673   char *names;
   13674   asection *plt;
   13675 
   13676   *ret = NULL;
   13677 
   13678   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   13679     return 0;
   13680 
   13681   if (dynsymcount <= 0)
   13682     return 0;
   13683 
   13684   if (!bed->plt_sym_val)
   13685     return 0;
   13686 
   13687   relplt_name = bed->relplt_name;
   13688   if (relplt_name == NULL)
   13689     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   13690   relplt = bfd_get_section_by_name (abfd, relplt_name);
   13691   if (relplt == NULL)
   13692     return 0;
   13693 
   13694   hdr = &elf_section_data (relplt)->this_hdr;
   13695   if (hdr->sh_link != elf_dynsymtab (abfd)
   13696       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   13697     return 0;
   13698 
   13699   plt = bfd_get_section_by_name (abfd, ".plt");
   13700   if (plt == NULL)
   13701     return 0;
   13702 
   13703   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   13704   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   13705     return -1;
   13706 
   13707   count = NUM_SHDR_ENTRIES (hdr);
   13708   size = count * sizeof (asymbol);
   13709   p = relplt->relocation;
   13710   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13711     {
   13712       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   13713       if (p->addend != 0)
   13714 	{
   13715 #ifdef BFD64
   13716 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   13717 #else
   13718 	  size += sizeof ("+0x") - 1 + 8;
   13719 #endif
   13720 	}
   13721     }
   13722 
   13723   s = *ret = (asymbol *) bfd_malloc (size);
   13724   if (s == NULL)
   13725     return -1;
   13726 
   13727   names = (char *) (s + count);
   13728   p = relplt->relocation;
   13729   n = 0;
   13730   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13731     {
   13732       size_t len;
   13733       bfd_vma addr;
   13734 
   13735       addr = bed->plt_sym_val (i, plt, p);
   13736       if (addr == (bfd_vma) -1)
   13737 	continue;
   13738 
   13739       *s = **p->sym_ptr_ptr;
   13740       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   13741 	 we are defining a symbol, ensure one of them is set.  */
   13742       if ((s->flags & BSF_LOCAL) == 0)
   13743 	s->flags |= BSF_GLOBAL;
   13744       s->flags |= BSF_SYNTHETIC;
   13745       s->section = plt;
   13746       s->value = addr - plt->vma;
   13747       s->name = names;
   13748       s->udata.p = NULL;
   13749       len = strlen ((*p->sym_ptr_ptr)->name);
   13750       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   13751       names += len;
   13752       if (p->addend != 0)
   13753 	{
   13754 	  char buf[30], *a;
   13755 
   13756 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   13757 	  names += sizeof ("+0x") - 1;
   13758 	  bfd_sprintf_vma (abfd, buf, p->addend);
   13759 	  for (a = buf; *a == '0'; ++a)
   13760 	    ;
   13761 	  len = strlen (a);
   13762 	  memcpy (names, a, len);
   13763 	  names += len;
   13764 	}
   13765       memcpy (names, "@plt", sizeof ("@plt"));
   13766       names += sizeof ("@plt");
   13767       ++s, ++n;
   13768     }
   13769 
   13770   return n;
   13771 }
   13772 
   13773 /* It is only used by x86-64 so far.
   13774    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   13775    but current usage would allow all of _bfd_std_section to be zero.  */
   13776 static const asymbol lcomm_sym
   13777   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   13778 asection _bfd_elf_large_com_section
   13779   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   13780 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   13781 
   13782 bool
   13783 _bfd_elf_final_write_processing (bfd *abfd)
   13784 {
   13785   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   13786 
   13787   i_ehdrp = elf_elfheader (abfd);
   13788 
   13789   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13790     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   13791 
   13792   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13793      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13794      or STB_GNU_UNIQUE binding.  */
   13795   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13796     {
   13797       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13798 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13799       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13800 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13801 	{
   13802 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13803 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13804 				  "and FreeBSD targets"));
   13805 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13806 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13807 				  "only by GNU and FreeBSD targets"));
   13808 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13809 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13810 				  "only by GNU and FreeBSD targets"));
   13811 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13812 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13813 				  "only by GNU and FreeBSD targets"));
   13814 	  bfd_set_error (bfd_error_sorry);
   13815 	  return false;
   13816 	}
   13817     }
   13818   return true;
   13819 }
   13820 
   13821 
   13822 /* Return TRUE for ELF symbol types that represent functions.
   13823    This is the default version of this function, which is sufficient for
   13824    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13825 
   13826 bool
   13827 _bfd_elf_is_function_type (unsigned int type)
   13828 {
   13829   return (type == STT_FUNC
   13830 	  || type == STT_GNU_IFUNC);
   13831 }
   13832 
   13833 /* If the ELF symbol SYM might be a function in SEC, return the
   13834    function size and set *CODE_OFF to the function's entry point,
   13835    otherwise return zero.  */
   13836 
   13837 bfd_size_type
   13838 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13839 			     bfd_vma *code_off)
   13840 {
   13841   bfd_size_type size;
   13842   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13843 
   13844   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13845 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13846       || sym->section != sec)
   13847     return 0;
   13848 
   13849   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13850 
   13851   /* In theory we should check that the symbol's type satisfies
   13852      _bfd_elf_is_function_type(), but there are some function-like
   13853      symbols which would fail this test.  (eg _start).  Instead
   13854      we check for hidden, local, notype symbols with zero size.
   13855      This type of symbol is generated by the annobin plugin for gcc
   13856      and clang, and should not be considered to be a function symbol.  */
   13857   if (size == 0
   13858       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13859       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13860       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13861     return 0;
   13862 
   13863   *code_off = sym->value;
   13864   /* Do not return 0 for the function's size.  */
   13865   return size ? size : 1;
   13866 }
   13867 
   13868 /* Set to non-zero to enable some debug messages.  */
   13869 #define DEBUG_SECONDARY_RELOCS	 0
   13870 
   13871 /* An internal-to-the-bfd-library only section type
   13872    used to indicate a cached secondary reloc section.  */
   13873 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13874 
   13875 /* Create a BFD section to hold a secondary reloc section.  */
   13876 
   13877 bool
   13878 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13879 				       Elf_Internal_Shdr *hdr,
   13880 				       const char * name,
   13881 				       unsigned int shindex)
   13882 {
   13883   /* We only support RELA secondary relocs.  */
   13884   if (hdr->sh_type != SHT_RELA)
   13885     return false;
   13886 
   13887 #if DEBUG_SECONDARY_RELOCS
   13888   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13889 #endif
   13890   hdr->sh_type = SHT_SECONDARY_RELOC;
   13891   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13892 }
   13893 
   13894 /* Read in any secondary relocs associated with SEC.  */
   13895 
   13896 bool
   13897 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13898 					asection *  sec,
   13899 					asymbol **  symbols,
   13900 					bool dynamic)
   13901 {
   13902   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13903   asection * relsec;
   13904   bool result = true;
   13905   bfd_vma (*r_sym) (bfd_vma);
   13906   ufile_ptr filesize;
   13907 
   13908 #if BFD_DEFAULT_TARGET_SIZE > 32
   13909   if (bfd_arch_bits_per_address (abfd) != 32)
   13910     r_sym = elf64_r_sym;
   13911   else
   13912 #endif
   13913     r_sym = elf32_r_sym;
   13914 
   13915   if (!elf_section_data (sec)->has_secondary_relocs)
   13916     return true;
   13917 
   13918   /* Discover if there are any secondary reloc sections
   13919      associated with SEC.  */
   13920   filesize = bfd_get_file_size (abfd);
   13921   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13922     {
   13923       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13924 
   13925       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13926 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13927 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13928 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13929 	{
   13930 	  bfd_byte * native_relocs;
   13931 	  bfd_byte * native_reloc;
   13932 	  arelent * internal_relocs;
   13933 	  arelent * internal_reloc;
   13934 	  size_t i;
   13935 	  unsigned int entsize;
   13936 	  unsigned int symcount;
   13937 	  bfd_size_type reloc_count;
   13938 	  size_t amt;
   13939 
   13940 	  if (ebd->elf_info_to_howto == NULL)
   13941 	    return false;
   13942 
   13943 #if DEBUG_SECONDARY_RELOCS
   13944 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13945 		   sec->name, relsec->name);
   13946 #endif
   13947 	  entsize = hdr->sh_entsize;
   13948 
   13949 	  if (filesize != 0
   13950 	      && ((ufile_ptr) hdr->sh_offset > filesize
   13951 		  || hdr->sh_size > filesize - hdr->sh_offset))
   13952 	    {
   13953 	      bfd_set_error (bfd_error_file_truncated);
   13954 	      result = false;
   13955 	      continue;
   13956 	    }
   13957 
   13958 	  native_relocs = bfd_malloc (hdr->sh_size);
   13959 	  if (native_relocs == NULL)
   13960 	    {
   13961 	      result = false;
   13962 	      continue;
   13963 	    }
   13964 
   13965 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13966 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13967 	    {
   13968 	      free (native_relocs);
   13969 	      bfd_set_error (bfd_error_file_too_big);
   13970 	      result = false;
   13971 	      continue;
   13972 	    }
   13973 
   13974 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   13975 	  if (internal_relocs == NULL)
   13976 	    {
   13977 	      free (native_relocs);
   13978 	      result = false;
   13979 	      continue;
   13980 	    }
   13981 
   13982 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   13983 	      || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
   13984 	    {
   13985 	      free (native_relocs);
   13986 	      /* The internal_relocs will be freed when
   13987 		 the memory for the bfd is released.  */
   13988 	      result = false;
   13989 	      continue;
   13990 	    }
   13991 
   13992 	  if (dynamic)
   13993 	    symcount = bfd_get_dynamic_symcount (abfd);
   13994 	  else
   13995 	    symcount = bfd_get_symcount (abfd);
   13996 
   13997 	  for (i = 0, internal_reloc = internal_relocs,
   13998 		 native_reloc = native_relocs;
   13999 	       i < reloc_count;
   14000 	       i++, internal_reloc++, native_reloc += entsize)
   14001 	    {
   14002 	      bool res;
   14003 	      Elf_Internal_Rela rela;
   14004 
   14005 	      if (entsize == ebd->s->sizeof_rel)
   14006 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   14007 	      else /* entsize == ebd->s->sizeof_rela */
   14008 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   14009 
   14010 	      /* The address of an ELF reloc is section relative for an object
   14011 		 file, and absolute for an executable file or shared library.
   14012 		 The address of a normal BFD reloc is always section relative,
   14013 		 and the address of a dynamic reloc is absolute..  */
   14014 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   14015 		internal_reloc->address = rela.r_offset;
   14016 	      else
   14017 		internal_reloc->address = rela.r_offset - sec->vma;
   14018 
   14019 	      if (r_sym (rela.r_info) == STN_UNDEF)
   14020 		{
   14021 		  /* FIXME: This and the error case below mean that we
   14022 		     have a symbol on relocs that is not elf_symbol_type.  */
   14023 		  internal_reloc->sym_ptr_ptr =
   14024 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   14025 		}
   14026 	      else if (r_sym (rela.r_info) > symcount)
   14027 		{
   14028 		  _bfd_error_handler
   14029 		    /* xgettext:c-format */
   14030 		    (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
   14031 		     abfd, sec, i, (long) r_sym (rela.r_info));
   14032 		  bfd_set_error (bfd_error_bad_value);
   14033 		  internal_reloc->sym_ptr_ptr =
   14034 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   14035 		  result = false;
   14036 		}
   14037 	      else
   14038 		{
   14039 		  asymbol **ps;
   14040 
   14041 		  ps = symbols + r_sym (rela.r_info) - 1;
   14042 		  internal_reloc->sym_ptr_ptr = ps;
   14043 		  /* Make sure that this symbol is not removed by strip.  */
   14044 		  (*ps)->flags |= BSF_KEEP;
   14045 		}
   14046 
   14047 	      internal_reloc->addend = rela.r_addend;
   14048 
   14049 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   14050 	      if (! res || internal_reloc->howto == NULL)
   14051 		{
   14052 #if DEBUG_SECONDARY_RELOCS
   14053 		  fprintf (stderr,
   14054 			   "there is no howto associated with reloc %lx\n",
   14055 			   rela.r_info);
   14056 #endif
   14057 		  result = false;
   14058 		}
   14059 	    }
   14060 
   14061 	  free (native_relocs);
   14062 	  /* Store the internal relocs.  */
   14063 	  elf_section_data (relsec)->sec_info = internal_relocs;
   14064 	}
   14065     }
   14066 
   14067   return result;
   14068 }
   14069 
   14070 /* Set the ELF section header fields of an output secondary reloc section.  */
   14071 
   14072 bool
   14073 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
   14074 				      bfd *obfd ATTRIBUTE_UNUSED,
   14075 				      const Elf_Internal_Shdr *isection,
   14076 				      Elf_Internal_Shdr *osection)
   14077 {
   14078   asection * isec;
   14079   asection * osec;
   14080   struct bfd_elf_section_data * esd;
   14081 
   14082   if (isection == NULL)
   14083     return false;
   14084 
   14085   if (isection->sh_type != SHT_SECONDARY_RELOC)
   14086     return true;
   14087 
   14088   isec = isection->bfd_section;
   14089   if (isec == NULL)
   14090     return false;
   14091 
   14092   osec = osection->bfd_section;
   14093   if (osec == NULL)
   14094     return false;
   14095 
   14096   esd = elf_section_data (osec);
   14097   BFD_ASSERT (esd->sec_info == NULL);
   14098   esd->sec_info = elf_section_data (isec)->sec_info;
   14099   osection->sh_type = SHT_RELA;
   14100   osection->sh_link = elf_onesymtab (obfd);
   14101   if (osection->sh_link == 0)
   14102     {
   14103       /* There is no symbol table - we are hosed...  */
   14104       _bfd_error_handler
   14105 	/* xgettext:c-format */
   14106 	(_("%pB(%pA): link section cannot be set"
   14107 	   " because the output file does not have a symbol table"),
   14108 	obfd, osec);
   14109       bfd_set_error (bfd_error_bad_value);
   14110       return false;
   14111     }
   14112 
   14113   /* Find the output section that corresponds to the isection's
   14114      sh_info link.  */
   14115   if (isection->sh_info == 0
   14116       || isection->sh_info >= elf_numsections (ibfd))
   14117     {
   14118       _bfd_error_handler
   14119 	/* xgettext:c-format */
   14120 	(_("%pB(%pA): info section index is invalid"),
   14121 	obfd, osec);
   14122       bfd_set_error (bfd_error_bad_value);
   14123       return false;
   14124     }
   14125 
   14126   isection = elf_elfsections (ibfd)[isection->sh_info];
   14127 
   14128   if (isection == NULL
   14129       || isection->bfd_section == NULL
   14130       || isection->bfd_section->output_section == NULL)
   14131     {
   14132       _bfd_error_handler
   14133 	/* xgettext:c-format */
   14134 	(_("%pB(%pA): info section index cannot be set"
   14135 	   " because the section is not in the output"),
   14136 	obfd, osec);
   14137       bfd_set_error (bfd_error_bad_value);
   14138       return false;
   14139     }
   14140 
   14141   esd = elf_section_data (isection->bfd_section->output_section);
   14142   BFD_ASSERT (esd != NULL);
   14143   osection->sh_info = esd->this_idx;
   14144   esd->has_secondary_relocs = true;
   14145 #if DEBUG_SECONDARY_RELOCS
   14146   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   14147 	   osec->name, osection->sh_link, osection->sh_info);
   14148   fprintf (stderr, "mark section %s as having secondary relocs\n",
   14149 	   bfd_section_name (isection->bfd_section->output_section));
   14150 #endif
   14151 
   14152   return true;
   14153 }
   14154 
   14155 /* Write out a secondary reloc section.
   14156 
   14157    FIXME: Currently this function can result in a serious performance penalty
   14158    for files with secondary relocs and lots of sections.  The proper way to
   14159    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   14160    relocs together and then to have this function just walk that chain.  */
   14161 
   14162 bool
   14163 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   14164 {
   14165   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   14166   bfd_vma addr_offset;
   14167   asection * relsec;
   14168   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   14169   bool result = true;
   14170 
   14171   if (sec == NULL)
   14172     return false;
   14173 
   14174 #if BFD_DEFAULT_TARGET_SIZE > 32
   14175   if (bfd_arch_bits_per_address (abfd) != 32)
   14176     r_info = elf64_r_info;
   14177   else
   14178 #endif
   14179     r_info = elf32_r_info;
   14180 
   14181   /* The address of an ELF reloc is section relative for an object
   14182      file, and absolute for an executable file or shared library.
   14183      The address of a BFD reloc is always section relative.  */
   14184   addr_offset = 0;
   14185   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   14186     addr_offset = sec->vma;
   14187 
   14188   /* Discover if there are any secondary reloc sections
   14189      associated with SEC.  */
   14190   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   14191     {
   14192       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   14193       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   14194 
   14195       if (hdr->sh_type == SHT_RELA
   14196 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   14197 	{
   14198 	  asymbol *    last_sym;
   14199 	  int          last_sym_idx;
   14200 	  size_t       reloc_count;
   14201 	  size_t       idx;
   14202 	  bfd_size_type entsize;
   14203 	  arelent *    src_irel;
   14204 	  bfd_byte *   dst_rela;
   14205 
   14206 	  if (hdr->contents != NULL)
   14207 	    {
   14208 	      _bfd_error_handler
   14209 		/* xgettext:c-format */
   14210 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   14211 		 abfd, relsec);
   14212 	      bfd_set_error (bfd_error_bad_value);
   14213 	      result = false;
   14214 	      continue;
   14215 	    }
   14216 
   14217 	  entsize = hdr->sh_entsize;
   14218 	  if (entsize == 0)
   14219 	    {
   14220 	      _bfd_error_handler
   14221 		/* xgettext:c-format */
   14222 		(_("%pB(%pA): error: secondary reloc section"
   14223 		   " has zero sized entries"),
   14224 		 abfd, relsec);
   14225 	      bfd_set_error (bfd_error_bad_value);
   14226 	      result = false;
   14227 	      continue;
   14228 	    }
   14229 	  else if (entsize != ebd->s->sizeof_rel
   14230 		   && entsize != ebd->s->sizeof_rela)
   14231 	    {
   14232 	      _bfd_error_handler
   14233 		/* xgettext:c-format */
   14234 		(_("%pB(%pA): error: secondary reloc section"
   14235 		   " has non-standard sized entries"),
   14236 		 abfd, relsec);
   14237 	      bfd_set_error (bfd_error_bad_value);
   14238 	      result = false;
   14239 	      continue;
   14240 	    }
   14241 
   14242 	  reloc_count = hdr->sh_size / entsize;
   14243 	  hdr->sh_size = entsize * reloc_count;
   14244 	  if (reloc_count == 0)
   14245 	    {
   14246 	      _bfd_error_handler
   14247 		/* xgettext:c-format */
   14248 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   14249 		 abfd, relsec);
   14250 	      bfd_set_error (bfd_error_bad_value);
   14251 	      result = false;
   14252 	      continue;
   14253 	    }
   14254 
   14255 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   14256 	  if (hdr->contents == NULL)
   14257 	    continue;
   14258 
   14259 #if DEBUG_SECONDARY_RELOCS
   14260 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   14261 		   reloc_count, sec->name, relsec->name);
   14262 #endif
   14263 	  last_sym = NULL;
   14264 	  last_sym_idx = 0;
   14265 	  dst_rela = hdr->contents;
   14266 	  src_irel = (arelent *) esd->sec_info;
   14267 	  if (src_irel == NULL)
   14268 	    {
   14269 	      _bfd_error_handler
   14270 		/* xgettext:c-format */
   14271 		(_("%pB(%pA): error: internal relocs missing"
   14272 		   " for secondary reloc section"),
   14273 		 abfd, relsec);
   14274 	      bfd_set_error (bfd_error_bad_value);
   14275 	      result = false;
   14276 	      continue;
   14277 	    }
   14278 
   14279 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   14280 	    {
   14281 	      Elf_Internal_Rela src_rela;
   14282 	      arelent *ptr;
   14283 	      asymbol *sym;
   14284 	      int n;
   14285 
   14286 	      ptr = src_irel + idx;
   14287 	      if (ptr == NULL)
   14288 		{
   14289 		  _bfd_error_handler
   14290 		    /* xgettext:c-format */
   14291 		    (_("%pB(%pA): error: reloc table entry %zu is empty"),
   14292 		     abfd, relsec, idx);
   14293 		  bfd_set_error (bfd_error_bad_value);
   14294 		  result = false;
   14295 		  break;
   14296 		}
   14297 
   14298 	      if (ptr->sym_ptr_ptr == NULL)
   14299 		{
   14300 		  /* FIXME: Is this an error ? */
   14301 		  n = 0;
   14302 		}
   14303 	      else
   14304 		{
   14305 		  sym = *ptr->sym_ptr_ptr;
   14306 
   14307 		  if (sym == last_sym)
   14308 		    n = last_sym_idx;
   14309 		  else
   14310 		    {
   14311 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   14312 		      if (n < 0)
   14313 			{
   14314 			  _bfd_error_handler
   14315 			    /* xgettext:c-format */
   14316 			    (_("%pB(%pA): error: secondary reloc %zu"
   14317 			       " references a missing symbol"),
   14318 			     abfd, relsec, idx);
   14319 			  bfd_set_error (bfd_error_bad_value);
   14320 			  result = false;
   14321 			  n = 0;
   14322 			}
   14323 
   14324 		      last_sym = sym;
   14325 		      last_sym_idx = n;
   14326 		    }
   14327 
   14328 		  if (sym->the_bfd != NULL
   14329 		      && sym->the_bfd->xvec != abfd->xvec
   14330 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   14331 		    {
   14332 		      _bfd_error_handler
   14333 			/* xgettext:c-format */
   14334 			(_("%pB(%pA): error: secondary reloc %zu"
   14335 			   " references a deleted symbol"),
   14336 			 abfd, relsec, idx);
   14337 		      bfd_set_error (bfd_error_bad_value);
   14338 		      result = false;
   14339 		      n = 0;
   14340 		    }
   14341 		}
   14342 
   14343 	      src_rela.r_offset = ptr->address + addr_offset;
   14344 	      if (ptr->howto == NULL)
   14345 		{
   14346 		  _bfd_error_handler
   14347 		    /* xgettext:c-format */
   14348 		    (_("%pB(%pA): error: secondary reloc %zu"
   14349 		       " is of an unknown type"),
   14350 		     abfd, relsec, idx);
   14351 		  bfd_set_error (bfd_error_bad_value);
   14352 		  result = false;
   14353 		  src_rela.r_info = r_info (0, 0);
   14354 		}
   14355 	      else
   14356 		src_rela.r_info = r_info (n, ptr->howto->type);
   14357 	      src_rela.r_addend = ptr->addend;
   14358 
   14359 	      if (entsize == ebd->s->sizeof_rel)
   14360 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   14361 	      else /* entsize == ebd->s->sizeof_rela */
   14362 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   14363 	    }
   14364 	}
   14365     }
   14366 
   14367   return result;
   14368 }
   14369