Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.19
      1 /* ELF executable support for BFD.
      2 
      3    Copyright (C) 1993-2022 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   const unsigned char *name = (const unsigned char *) namearg;
    200   unsigned long h = 0;
    201   unsigned long g;
    202   int ch;
    203 
    204   while ((ch = *name++) != '\0')
    205     {
    206       h = (h << 4) + ch;
    207       if ((g = (h & 0xf0000000)) != 0)
    208 	{
    209 	  h ^= g >> 24;
    210 	  /* The ELF ABI says `h &= ~g', but this is equivalent in
    211 	     this case and on some machines one insn instead of two.  */
    212 	  h ^= g;
    213 	}
    214     }
    215   return h & 0xffffffff;
    216 }
    217 
    218 /* DT_GNU_HASH hash function.  Do not change this function; you will
    219    cause invalid hash tables to be generated.  */
    220 
    221 unsigned long
    222 bfd_elf_gnu_hash (const char *namearg)
    223 {
    224   const unsigned char *name = (const unsigned char *) namearg;
    225   unsigned long h = 5381;
    226   unsigned char ch;
    227 
    228   while ((ch = *name++) != '\0')
    229     h = (h << 5) + h + ch;
    230   return h & 0xffffffff;
    231 }
    232 
    233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    234    the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
    235 bool
    236 bfd_elf_allocate_object (bfd *abfd,
    237 			 size_t object_size,
    238 			 enum elf_target_id object_id)
    239 {
    240   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    241   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    242   if (abfd->tdata.any == NULL)
    243     return false;
    244 
    245   elf_object_id (abfd) = object_id;
    246   if (abfd->direction != read_direction)
    247     {
    248       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    249       if (o == NULL)
    250 	return false;
    251       elf_tdata (abfd)->o = o;
    252       elf_program_header_size (abfd) = (bfd_size_type) -1;
    253     }
    254   return true;
    255 }
    256 
    257 
    258 bool
    259 bfd_elf_make_object (bfd *abfd)
    260 {
    261   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    262   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
    263 				  bed->target_id);
    264 }
    265 
    266 bool
    267 bfd_elf_mkcorefile (bfd *abfd)
    268 {
    269   /* I think this can be done just like an object file.  */
    270   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    271     return false;
    272   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    273   return elf_tdata (abfd)->core != NULL;
    274 }
    275 
    276 char *
    277 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    278 {
    279   Elf_Internal_Shdr **i_shdrp;
    280   bfd_byte *shstrtab = NULL;
    281   file_ptr offset;
    282   bfd_size_type shstrtabsize;
    283 
    284   i_shdrp = elf_elfsections (abfd);
    285   if (i_shdrp == 0
    286       || shindex >= elf_numsections (abfd)
    287       || i_shdrp[shindex] == 0)
    288     return NULL;
    289 
    290   shstrtab = i_shdrp[shindex]->contents;
    291   if (shstrtab == NULL)
    292     {
    293       /* No cached one, attempt to read, and cache what we read.  */
    294       offset = i_shdrp[shindex]->sh_offset;
    295       shstrtabsize = i_shdrp[shindex]->sh_size;
    296 
    297       /* Allocate and clear an extra byte at the end, to prevent crashes
    298 	 in case the string table is not terminated.  */
    299       if (shstrtabsize + 1 <= 1
    300 	  || (bfd_get_file_size (abfd) > 0 /* not a character device */
    301 		&& shstrtabsize > bfd_get_file_size (abfd))
    302 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    303 	  || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
    304 					      shstrtabsize)) == NULL)
    305 	{
    306 	  /* Once we've failed to read it, make sure we don't keep
    307 	     trying.  Otherwise, we'll keep allocating space for
    308 	     the string table over and over.  */
    309 	  i_shdrp[shindex]->sh_size = 0;
    310 	}
    311       else
    312 	shstrtab[shstrtabsize] = '\0';
    313       i_shdrp[shindex]->contents = shstrtab;
    314     }
    315   return (char *) shstrtab;
    316 }
    317 
    318 char *
    319 bfd_elf_string_from_elf_section (bfd *abfd,
    320 				 unsigned int shindex,
    321 				 unsigned int strindex)
    322 {
    323   Elf_Internal_Shdr *hdr;
    324 
    325   if (strindex == 0)
    326     return "";
    327 
    328   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    329     return NULL;
    330 
    331   hdr = elf_elfsections (abfd)[shindex];
    332 
    333   if (hdr->contents == NULL)
    334     {
    335       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    336 	{
    337 	  /* PR 17512: file: f057ec89.  */
    338 	  /* xgettext:c-format */
    339 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    340 				" a non-string section (number %d)"),
    341 			      abfd, shindex);
    342 	  return NULL;
    343 	}
    344 
    345       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    346 	return NULL;
    347     }
    348   else
    349     {
    350       /* PR 24273: The string section's contents may have already
    351 	 been loaded elsewhere, eg because a corrupt file has the
    352 	 string section index in the ELF header pointing at a group
    353 	 section.  So be paranoid, and test that the last byte of
    354 	 the section is zero.  */
    355       if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
    356 	return NULL;
    357     }
    358 
    359   if (strindex >= hdr->sh_size)
    360     {
    361       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    362       _bfd_error_handler
    363 	/* xgettext:c-format */
    364 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    365 	 abfd, strindex, (uint64_t) hdr->sh_size,
    366 	 (shindex == shstrndx && strindex == hdr->sh_name
    367 	  ? ".shstrtab"
    368 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    369       return NULL;
    370     }
    371 
    372   return ((char *) hdr->contents) + strindex;
    373 }
    374 
    375 /* Read and convert symbols to internal format.
    376    SYMCOUNT specifies the number of symbols to read, starting from
    377    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    378    are non-NULL, they are used to store the internal symbols, external
    379    symbols, and symbol section index extensions, respectively.
    380    Returns a pointer to the internal symbol buffer (malloced if necessary)
    381    or NULL if there were no symbols or some kind of problem.  */
    382 
    383 Elf_Internal_Sym *
    384 bfd_elf_get_elf_syms (bfd *ibfd,
    385 		      Elf_Internal_Shdr *symtab_hdr,
    386 		      size_t symcount,
    387 		      size_t symoffset,
    388 		      Elf_Internal_Sym *intsym_buf,
    389 		      void *extsym_buf,
    390 		      Elf_External_Sym_Shndx *extshndx_buf)
    391 {
    392   Elf_Internal_Shdr *shndx_hdr;
    393   void *alloc_ext;
    394   const bfd_byte *esym;
    395   Elf_External_Sym_Shndx *alloc_extshndx;
    396   Elf_External_Sym_Shndx *shndx;
    397   Elf_Internal_Sym *alloc_intsym;
    398   Elf_Internal_Sym *isym;
    399   Elf_Internal_Sym *isymend;
    400   const struct elf_backend_data *bed;
    401   size_t extsym_size;
    402   size_t amt;
    403   file_ptr pos;
    404 
    405   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    406     abort ();
    407 
    408   if (symcount == 0)
    409     return intsym_buf;
    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 to work.  */
    436 	    shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
    437 	  /* Otherwise we do nothing.  The assumption is that
    438 	     the index table will not be needed.  */
    439 	}
    440     }
    441 
    442   /* Read the symbols.  */
    443   alloc_ext = NULL;
    444   alloc_extshndx = NULL;
    445   alloc_intsym = NULL;
    446   bed = get_elf_backend_data (ibfd);
    447   extsym_size = bed->s->sizeof_sym;
    448   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
    449     {
    450       bfd_set_error (bfd_error_file_too_big);
    451       intsym_buf = NULL;
    452       goto out;
    453     }
    454   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    455   if (extsym_buf == NULL)
    456     {
    457       alloc_ext = bfd_malloc (amt);
    458       extsym_buf = alloc_ext;
    459     }
    460   if (extsym_buf == NULL
    461       || bfd_seek (ibfd, pos, SEEK_SET) != 0
    462       || bfd_bread (extsym_buf, amt, ibfd) != amt)
    463     {
    464       intsym_buf = NULL;
    465       goto out;
    466     }
    467 
    468   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    469     extshndx_buf = NULL;
    470   else
    471     {
    472       if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
    473 	{
    474 	  bfd_set_error (bfd_error_file_too_big);
    475 	  intsym_buf = NULL;
    476 	  goto out;
    477 	}
    478       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    479       if (extshndx_buf == NULL)
    480 	{
    481 	  alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
    482 	  extshndx_buf = alloc_extshndx;
    483 	}
    484       if (extshndx_buf == NULL
    485 	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
    486 	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
    487 	{
    488 	  intsym_buf = NULL;
    489 	  goto out;
    490 	}
    491     }
    492 
    493   if (intsym_buf == NULL)
    494     {
    495       if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
    496 	{
    497 	  bfd_set_error (bfd_error_file_too_big);
    498 	  goto out;
    499 	}
    500       alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
    501       intsym_buf = alloc_intsym;
    502       if (intsym_buf == NULL)
    503 	goto out;
    504     }
    505 
    506   /* Convert the symbols to internal form.  */
    507   isymend = intsym_buf + symcount;
    508   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    509 	   shndx = extshndx_buf;
    510        isym < isymend;
    511        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    512     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    513       {
    514 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    515 	/* xgettext:c-format */
    516 	_bfd_error_handler (_("%pB symbol number %lu references"
    517 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    518 			    ibfd, (unsigned long) symoffset);
    519 	free (alloc_intsym);
    520 	intsym_buf = NULL;
    521 	goto out;
    522       }
    523 
    524  out:
    525   free (alloc_ext);
    526   free (alloc_extshndx);
    527 
    528   return intsym_buf;
    529 }
    530 
    531 /* Look up a symbol name.  */
    532 const char *
    533 bfd_elf_sym_name (bfd *abfd,
    534 		  Elf_Internal_Shdr *symtab_hdr,
    535 		  Elf_Internal_Sym *isym,
    536 		  asection *sym_sec)
    537 {
    538   const char *name;
    539   unsigned int iname = isym->st_name;
    540   unsigned int shindex = symtab_hdr->sh_link;
    541 
    542   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    543       /* Check for a bogus st_shndx to avoid crashing.  */
    544       && isym->st_shndx < elf_numsections (abfd))
    545     {
    546       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    547       shindex = elf_elfheader (abfd)->e_shstrndx;
    548     }
    549 
    550   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    551   if (name == NULL)
    552     name = "(null)";
    553   else if (sym_sec && *name == '\0')
    554     name = bfd_section_name (sym_sec);
    555 
    556   return name;
    557 }
    558 
    559 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    560    sections.  The first element is the flags, the rest are section
    561    pointers.  */
    562 
    563 typedef union elf_internal_group {
    564   Elf_Internal_Shdr *shdr;
    565   unsigned int flags;
    566 } Elf_Internal_Group;
    567 
    568 /* Return the name of the group signature symbol.  Why isn't the
    569    signature just a string?  */
    570 
    571 static const char *
    572 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    573 {
    574   Elf_Internal_Shdr *hdr;
    575   unsigned char esym[sizeof (Elf64_External_Sym)];
    576   Elf_External_Sym_Shndx eshndx;
    577   Elf_Internal_Sym isym;
    578 
    579   /* First we need to ensure the symbol table is available.  Make sure
    580      that it is a symbol table section.  */
    581   if (ghdr->sh_link >= elf_numsections (abfd))
    582     return NULL;
    583   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    584   if (hdr->sh_type != SHT_SYMTAB
    585       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    586     return NULL;
    587 
    588   /* Go read the symbol.  */
    589   hdr = &elf_tdata (abfd)->symtab_hdr;
    590   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    591 			    &isym, esym, &eshndx) == NULL)
    592     return NULL;
    593 
    594   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    595 }
    596 
    597 /* Set next_in_group list pointer, and group name for NEWSECT.  */
    598 
    599 static bool
    600 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    601 {
    602   unsigned int num_group = elf_tdata (abfd)->num_group;
    603 
    604   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    605      is set to -1 if there are no SHT_GROUP sections.  */
    606   if (num_group == 0)
    607     {
    608       unsigned int i, shnum;
    609 
    610       /* First count the number of groups.  If we have a SHT_GROUP
    611 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    612       shnum = elf_numsections (abfd);
    613       num_group = 0;
    614 
    615 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    616 	(   (shdr)->sh_type == SHT_GROUP		\
    617 	 && (shdr)->sh_size >= minsize			\
    618 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    619 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    620 
    621       for (i = 0; i < shnum; i++)
    622 	{
    623 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    624 
    625 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    626 	    num_group += 1;
    627 	}
    628 
    629       if (num_group == 0)
    630 	{
    631 	  num_group = (unsigned) -1;
    632 	  elf_tdata (abfd)->num_group = num_group;
    633 	  elf_tdata (abfd)->group_sect_ptr = NULL;
    634 	}
    635       else
    636 	{
    637 	  /* We keep a list of elf section headers for group sections,
    638 	     so we can find them quickly.  */
    639 	  size_t amt;
    640 
    641 	  elf_tdata (abfd)->num_group = num_group;
    642 	  amt = num_group * sizeof (Elf_Internal_Shdr *);
    643 	  elf_tdata (abfd)->group_sect_ptr
    644 	    = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
    645 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    646 	    return false;
    647 	  num_group = 0;
    648 
    649 	  for (i = 0; i < shnum; i++)
    650 	    {
    651 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    652 
    653 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    654 		{
    655 		  unsigned char *src;
    656 		  Elf_Internal_Group *dest;
    657 
    658 		  /* Make sure the group section has a BFD section
    659 		     attached to it.  */
    660 		  if (!bfd_section_from_shdr (abfd, i))
    661 		    return false;
    662 
    663 		  /* Add to list of sections.  */
    664 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    665 		  num_group += 1;
    666 
    667 		  /* Read the raw contents.  */
    668 		  BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
    669 		  shdr->contents = NULL;
    670 		  if (_bfd_mul_overflow (shdr->sh_size,
    671 					 sizeof (*dest) / 4, &amt)
    672 		      || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    673 		      || !(shdr->contents
    674 			   = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
    675 		    {
    676 		      _bfd_error_handler
    677 			/* xgettext:c-format */
    678 			(_("%pB: invalid size field in group section"
    679 			   " header: %#" PRIx64 ""),
    680 			 abfd, (uint64_t) shdr->sh_size);
    681 		      bfd_set_error (bfd_error_bad_value);
    682 		      -- num_group;
    683 		      continue;
    684 		    }
    685 
    686 		  /* Translate raw contents, a flag word followed by an
    687 		     array of elf section indices all in target byte order,
    688 		     to the flag word followed by an array of elf section
    689 		     pointers.  */
    690 		  src = shdr->contents + shdr->sh_size;
    691 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    692 
    693 		  while (1)
    694 		    {
    695 		      unsigned int idx;
    696 
    697 		      src -= 4;
    698 		      --dest;
    699 		      idx = H_GET_32 (abfd, src);
    700 		      if (src == shdr->contents)
    701 			{
    702 			  dest->shdr = NULL;
    703 			  dest->flags = idx;
    704 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    705 			    shdr->bfd_section->flags
    706 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    707 			  break;
    708 			}
    709 		      if (idx < shnum)
    710 			{
    711 			  dest->shdr = elf_elfsections (abfd)[idx];
    712 			  /* PR binutils/23199: All sections in a
    713 			     section group should be marked with
    714 			     SHF_GROUP.  But some tools generate
    715 			     broken objects without SHF_GROUP.  Fix
    716 			     them up here.  */
    717 			  dest->shdr->sh_flags |= SHF_GROUP;
    718 			}
    719 		      if (idx >= shnum
    720 			  || dest->shdr->sh_type == SHT_GROUP)
    721 			{
    722 			  _bfd_error_handler
    723 			    (_("%pB: invalid entry in SHT_GROUP section [%u]"),
    724 			       abfd, i);
    725 			  dest->shdr = NULL;
    726 			}
    727 		    }
    728 		}
    729 	    }
    730 
    731 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    732 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    733 	    {
    734 	      elf_tdata (abfd)->num_group = num_group;
    735 
    736 	      /* If all groups are invalid then fail.  */
    737 	      if (num_group == 0)
    738 		{
    739 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    740 		  elf_tdata (abfd)->num_group = num_group = -1;
    741 		  _bfd_error_handler
    742 		    (_("%pB: no valid group sections found"), abfd);
    743 		  bfd_set_error (bfd_error_bad_value);
    744 		}
    745 	    }
    746 	}
    747     }
    748 
    749   if (num_group != (unsigned) -1)
    750     {
    751       unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
    752       unsigned int j;
    753 
    754       for (j = 0; j < num_group; j++)
    755 	{
    756 	  /* Begin search from previous found group.  */
    757 	  unsigned i = (j + search_offset) % num_group;
    758 
    759 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    760 	  Elf_Internal_Group *idx;
    761 	  bfd_size_type n_elt;
    762 
    763 	  if (shdr == NULL)
    764 	    continue;
    765 
    766 	  idx = (Elf_Internal_Group *) shdr->contents;
    767 	  if (idx == NULL || shdr->sh_size < 4)
    768 	    {
    769 	      /* See PR 21957 for a reproducer.  */
    770 	      /* xgettext:c-format */
    771 	      _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
    772 				  abfd, shdr->bfd_section);
    773 	      elf_tdata (abfd)->group_sect_ptr[i] = NULL;
    774 	      bfd_set_error (bfd_error_bad_value);
    775 	      return false;
    776 	    }
    777 	  n_elt = shdr->sh_size / 4;
    778 
    779 	  /* Look through this group's sections to see if current
    780 	     section is a member.  */
    781 	  while (--n_elt != 0)
    782 	    if ((++idx)->shdr == hdr)
    783 	      {
    784 		asection *s = NULL;
    785 
    786 		/* We are a member of this group.  Go looking through
    787 		   other members to see if any others are linked via
    788 		   next_in_group.  */
    789 		idx = (Elf_Internal_Group *) shdr->contents;
    790 		n_elt = shdr->sh_size / 4;
    791 		while (--n_elt != 0)
    792 		  if ((++idx)->shdr != NULL
    793 		      && (s = idx->shdr->bfd_section) != NULL
    794 		      && elf_next_in_group (s) != NULL)
    795 		    break;
    796 		if (n_elt != 0)
    797 		  {
    798 		    /* Snarf the group name from other member, and
    799 		       insert current section in circular list.  */
    800 		    elf_group_name (newsect) = elf_group_name (s);
    801 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    802 		    elf_next_in_group (s) = newsect;
    803 		  }
    804 		else
    805 		  {
    806 		    const char *gname;
    807 
    808 		    gname = group_signature (abfd, shdr);
    809 		    if (gname == NULL)
    810 		      return false;
    811 		    elf_group_name (newsect) = gname;
    812 
    813 		    /* Start a circular list with one element.  */
    814 		    elf_next_in_group (newsect) = newsect;
    815 		  }
    816 
    817 		/* If the group section has been created, point to the
    818 		   new member.  */
    819 		if (shdr->bfd_section != NULL)
    820 		  elf_next_in_group (shdr->bfd_section) = newsect;
    821 
    822 		elf_tdata (abfd)->group_search_offset = i;
    823 		j = num_group - 1;
    824 		break;
    825 	      }
    826 	}
    827     }
    828 
    829   if (elf_group_name (newsect) == NULL)
    830     {
    831       /* xgettext:c-format */
    832       _bfd_error_handler (_("%pB: no group info for section '%pA'"),
    833 			  abfd, newsect);
    834       return false;
    835     }
    836   return true;
    837 }
    838 
    839 bool
    840 _bfd_elf_setup_sections (bfd *abfd)
    841 {
    842   unsigned int i;
    843   unsigned int num_group = elf_tdata (abfd)->num_group;
    844   bool result = true;
    845   asection *s;
    846 
    847   /* Process SHF_LINK_ORDER.  */
    848   for (s = abfd->sections; s != NULL; s = s->next)
    849     {
    850       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    851       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    852 	{
    853 	  unsigned int elfsec = this_hdr->sh_link;
    854 	  /* An sh_link value of 0 is now allowed.  It indicates that linked
    855 	     to section has already been discarded, but that the current
    856 	     section has been retained for some other reason.  This linking
    857 	     section is still a candidate for later garbage collection
    858 	     however.  */
    859 	  if (elfsec == 0)
    860 	    {
    861 	      elf_linked_to_section (s) = NULL;
    862 	    }
    863 	  else
    864 	    {
    865 	      asection *linksec = NULL;
    866 
    867 	      if (elfsec < elf_numsections (abfd))
    868 		{
    869 		  this_hdr = elf_elfsections (abfd)[elfsec];
    870 		  linksec = this_hdr->bfd_section;
    871 		}
    872 
    873 	      /* PR 1991, 2008:
    874 		 Some strip/objcopy may leave an incorrect value in
    875 		 sh_link.  We don't want to proceed.  */
    876 	      if (linksec == NULL)
    877 		{
    878 		  _bfd_error_handler
    879 		    /* xgettext:c-format */
    880 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    881 		     s->owner, elfsec, s);
    882 		  result = false;
    883 		}
    884 
    885 	      elf_linked_to_section (s) = linksec;
    886 	    }
    887 	}
    888       else if (this_hdr->sh_type == SHT_GROUP
    889 	       && elf_next_in_group (s) == NULL)
    890 	{
    891 	  _bfd_error_handler
    892 	    /* xgettext:c-format */
    893 	    (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
    894 	     abfd, elf_section_data (s)->this_idx);
    895 	  result = false;
    896 	}
    897     }
    898 
    899   /* Process section groups.  */
    900   if (num_group == (unsigned) -1)
    901     return result;
    902 
    903   for (i = 0; i < num_group; i++)
    904     {
    905       Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    906       Elf_Internal_Group *idx;
    907       unsigned int n_elt;
    908 
    909       /* PR binutils/18758: Beware of corrupt binaries with invalid group data.  */
    910       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    911 	{
    912 	  _bfd_error_handler
    913 	    /* xgettext:c-format */
    914 	    (_("%pB: section group entry number %u is corrupt"),
    915 	     abfd, i);
    916 	  result = false;
    917 	  continue;
    918 	}
    919 
    920       idx = (Elf_Internal_Group *) shdr->contents;
    921       n_elt = shdr->sh_size / 4;
    922 
    923       while (--n_elt != 0)
    924 	{
    925 	  ++ idx;
    926 
    927 	  if (idx->shdr == NULL)
    928 	    continue;
    929 	  else if (idx->shdr->bfd_section)
    930 	    elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    931 	  else if (idx->shdr->sh_type != SHT_RELA
    932 		   && idx->shdr->sh_type != SHT_REL)
    933 	    {
    934 	      /* There are some unknown sections in the group.  */
    935 	      _bfd_error_handler
    936 		/* xgettext:c-format */
    937 		(_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
    938 		 abfd,
    939 		 idx->shdr->sh_type,
    940 		 bfd_elf_string_from_elf_section (abfd,
    941 						  (elf_elfheader (abfd)
    942 						   ->e_shstrndx),
    943 						  idx->shdr->sh_name),
    944 		 shdr->bfd_section);
    945 	      result = false;
    946 	    }
    947 	}
    948     }
    949 
    950   return result;
    951 }
    952 
    953 bool
    954 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    955 {
    956   return elf_next_in_group (sec) != NULL;
    957 }
    958 
    959 const char *
    960 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    961 {
    962   if (elf_sec_group (sec) != NULL)
    963     return elf_group_name (sec);
    964   return NULL;
    965 }
    966 
    967 static char *
    968 convert_debug_to_zdebug (bfd *abfd, const char *name)
    969 {
    970   unsigned int len = strlen (name);
    971   char *new_name = bfd_alloc (abfd, len + 2);
    972   if (new_name == NULL)
    973     return NULL;
    974   new_name[0] = '.';
    975   new_name[1] = 'z';
    976   memcpy (new_name + 2, name + 1, len);
    977   return new_name;
    978 }
    979 
    980 static char *
    981 convert_zdebug_to_debug (bfd *abfd, const char *name)
    982 {
    983   unsigned int len = strlen (name);
    984   char *new_name = bfd_alloc (abfd, len);
    985   if (new_name == NULL)
    986     return NULL;
    987   new_name[0] = '.';
    988   memcpy (new_name + 1, name + 2, len - 1);
    989   return new_name;
    990 }
    991 
    992 /* This a copy of lto_section defined in GCC (lto-streamer.h).  */
    993 
    994 struct lto_section
    995 {
    996   int16_t major_version;
    997   int16_t minor_version;
    998   unsigned char slim_object;
    999 
   1000   /* Flags is a private field that is not defined publicly.  */
   1001   uint16_t flags;
   1002 };
   1003 
   1004 /* Make a BFD section from an ELF section.  We store a pointer to the
   1005    BFD section in the bfd_section field of the header.  */
   1006 
   1007 bool
   1008 _bfd_elf_make_section_from_shdr (bfd *abfd,
   1009 				 Elf_Internal_Shdr *hdr,
   1010 				 const char *name,
   1011 				 int shindex)
   1012 {
   1013   asection *newsect;
   1014   flagword flags;
   1015   const struct elf_backend_data *bed;
   1016   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   1017 
   1018   if (hdr->bfd_section != NULL)
   1019     return true;
   1020 
   1021   newsect = bfd_make_section_anyway (abfd, name);
   1022   if (newsect == NULL)
   1023     return false;
   1024 
   1025   hdr->bfd_section = newsect;
   1026   elf_section_data (newsect)->this_hdr = *hdr;
   1027   elf_section_data (newsect)->this_idx = shindex;
   1028 
   1029   /* Always use the real type/flags.  */
   1030   elf_section_type (newsect) = hdr->sh_type;
   1031   elf_section_flags (newsect) = hdr->sh_flags;
   1032 
   1033   newsect->filepos = hdr->sh_offset;
   1034 
   1035   flags = SEC_NO_FLAGS;
   1036   if (hdr->sh_type != SHT_NOBITS)
   1037     flags |= SEC_HAS_CONTENTS;
   1038   if (hdr->sh_type == SHT_GROUP)
   1039     flags |= SEC_GROUP;
   1040   if ((hdr->sh_flags & SHF_ALLOC) != 0)
   1041     {
   1042       flags |= SEC_ALLOC;
   1043       if (hdr->sh_type != SHT_NOBITS)
   1044 	flags |= SEC_LOAD;
   1045     }
   1046   if ((hdr->sh_flags & SHF_WRITE) == 0)
   1047     flags |= SEC_READONLY;
   1048   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
   1049     flags |= SEC_CODE;
   1050   else if ((flags & SEC_LOAD) != 0)
   1051     flags |= SEC_DATA;
   1052   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1053     {
   1054       flags |= SEC_MERGE;
   1055       newsect->entsize = hdr->sh_entsize;
   1056     }
   1057   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1058     flags |= SEC_STRINGS;
   1059   if (hdr->sh_flags & SHF_GROUP)
   1060     if (!setup_group (abfd, hdr, newsect))
   1061       return false;
   1062   if ((hdr->sh_flags & SHF_TLS) != 0)
   1063     flags |= SEC_THREAD_LOCAL;
   1064   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1065     flags |= SEC_EXCLUDE;
   1066 
   1067   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
   1068     {
   1069       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
   1070 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
   1071 	 byte.  */
   1072     case ELFOSABI_GNU:
   1073     case ELFOSABI_FREEBSD:
   1074       if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
   1075 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
   1076       /* Fall through */
   1077     case ELFOSABI_NONE:
   1078       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
   1079 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
   1080       break;
   1081     }
   1082 
   1083   if ((flags & SEC_ALLOC) == 0)
   1084     {
   1085       /* The debugging sections appear to be recognized only by name,
   1086 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1087       if (name [0] == '.')
   1088 	{
   1089 	  if (startswith (name, ".debug")
   1090 	      || startswith (name, ".gnu.debuglto_.debug_")
   1091 	      || startswith (name, ".gnu.linkonce.wi.")
   1092 	      || startswith (name, ".zdebug"))
   1093 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
   1094 	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
   1095 		   || startswith (name, ".note.gnu"))
   1096 	    {
   1097 	      flags |= SEC_ELF_OCTETS;
   1098 	      opb = 1;
   1099 	    }
   1100 	  else if (startswith (name, ".line")
   1101 		   || startswith (name, ".stab")
   1102 		   || strcmp (name, ".gdb_index") == 0)
   1103 	    flags |= SEC_DEBUGGING;
   1104 	}
   1105     }
   1106 
   1107   if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
   1108       || !bfd_set_section_size (newsect, hdr->sh_size)
   1109       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
   1110 							& -hdr->sh_addralign)))
   1111     return false;
   1112 
   1113   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1114      only link a single copy of the section.  This is used to support
   1115      g++.  g++ will emit each template expansion in its own section.
   1116      The symbols will be defined as weak, so that multiple definitions
   1117      are permitted.  The GNU linker extension is to actually discard
   1118      all but one of the sections.  */
   1119   if (startswith (name, ".gnu.linkonce")
   1120       && elf_next_in_group (newsect) == NULL)
   1121     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1122 
   1123   if (!bfd_set_section_flags (newsect, flags))
   1124     return false;
   1125 
   1126   bed = get_elf_backend_data (abfd);
   1127   if (bed->elf_backend_section_flags)
   1128     if (!bed->elf_backend_section_flags (hdr))
   1129       return false;
   1130 
   1131   /* We do not parse the PT_NOTE segments as we are interested even in the
   1132      separate debug info files which may have the segments offsets corrupted.
   1133      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1134   if (hdr->sh_type == SHT_NOTE)
   1135     {
   1136       bfd_byte *contents;
   1137 
   1138       if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
   1139 	return false;
   1140 
   1141       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1142 		       hdr->sh_offset, hdr->sh_addralign);
   1143       free (contents);
   1144     }
   1145 
   1146   if ((newsect->flags & SEC_ALLOC) != 0)
   1147     {
   1148       Elf_Internal_Phdr *phdr;
   1149       unsigned int i, nload;
   1150 
   1151       /* Some ELF linkers produce binaries with all the program header
   1152 	 p_paddr fields zero.  If we have such a binary with more than
   1153 	 one PT_LOAD header, then leave the section lma equal to vma
   1154 	 so that we don't create sections with overlapping lma.  */
   1155       phdr = elf_tdata (abfd)->phdr;
   1156       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1157 	if (phdr->p_paddr != 0)
   1158 	  break;
   1159 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1160 	  ++nload;
   1161       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1162 	return true;
   1163 
   1164       phdr = elf_tdata (abfd)->phdr;
   1165       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1166 	{
   1167 	  if (((phdr->p_type == PT_LOAD
   1168 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1169 	       || phdr->p_type == PT_TLS)
   1170 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1171 	    {
   1172 	      if ((newsect->flags & SEC_LOAD) == 0)
   1173 		newsect->lma = (phdr->p_paddr
   1174 				+ hdr->sh_addr - phdr->p_vaddr) / opb;
   1175 	      else
   1176 		/* We used to use the same adjustment for SEC_LOAD
   1177 		   sections, but that doesn't work if the segment
   1178 		   is packed with code from multiple VMAs.
   1179 		   Instead we calculate the section LMA based on
   1180 		   the segment LMA.  It is assumed that the
   1181 		   segment will contain sections with contiguous
   1182 		   LMAs, even if the VMAs are not.  */
   1183 		newsect->lma = (phdr->p_paddr
   1184 				+ hdr->sh_offset - phdr->p_offset) / opb;
   1185 
   1186 	      /* With contiguous segments, we can't tell from file
   1187 		 offsets whether a section with zero size should
   1188 		 be placed at the end of one segment or the
   1189 		 beginning of the next.  Decide based on vaddr.  */
   1190 	      if (hdr->sh_addr >= phdr->p_vaddr
   1191 		  && (hdr->sh_addr + hdr->sh_size
   1192 		      <= phdr->p_vaddr + phdr->p_memsz))
   1193 		break;
   1194 	    }
   1195 	}
   1196     }
   1197 
   1198   /* Compress/decompress DWARF debug sections with names: .debug_* and
   1199      .zdebug_*, after the section flags is set.  */
   1200   if ((newsect->flags & SEC_DEBUGGING)
   1201       && ((name[1] == 'd' && name[6] == '_')
   1202 	  || (name[1] == 'z' && name[7] == '_')))
   1203     {
   1204       enum { nothing, compress, decompress } action = nothing;
   1205       int compression_header_size;
   1206       bfd_size_type uncompressed_size;
   1207       unsigned int uncompressed_align_power;
   1208       bool compressed
   1209 	= bfd_is_section_compressed_with_header (abfd, newsect,
   1210 						 &compression_header_size,
   1211 						 &uncompressed_size,
   1212 						 &uncompressed_align_power);
   1213       if (compressed)
   1214 	{
   1215 	  /* Compressed section.  Check if we should decompress.  */
   1216 	  if ((abfd->flags & BFD_DECOMPRESS))
   1217 	    action = decompress;
   1218 	}
   1219 
   1220       /* Compress the uncompressed section or convert from/to .zdebug*
   1221 	 section.  Check if we should compress.  */
   1222       if (action == nothing)
   1223 	{
   1224 	  if (newsect->size != 0
   1225 	      && (abfd->flags & BFD_COMPRESS)
   1226 	      && compression_header_size >= 0
   1227 	      && uncompressed_size > 0
   1228 	      && (!compressed
   1229 		  || ((compression_header_size > 0)
   1230 		      != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
   1231 	    action = compress;
   1232 	  else
   1233 	    return true;
   1234 	}
   1235 
   1236       if (action == compress)
   1237 	{
   1238 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1239 	    {
   1240 	      _bfd_error_handler
   1241 		/* xgettext:c-format */
   1242 		(_("%pB: unable to initialize compress status for section %s"),
   1243 		 abfd, name);
   1244 	      return false;
   1245 	    }
   1246 	}
   1247       else
   1248 	{
   1249 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1250 	    {
   1251 	      _bfd_error_handler
   1252 		/* xgettext:c-format */
   1253 		(_("%pB: unable to initialize decompress status for section %s"),
   1254 		 abfd, name);
   1255 	      return false;
   1256 	    }
   1257 	}
   1258 
   1259       if (abfd->is_linker_input)
   1260 	{
   1261 	  if (name[1] == 'z'
   1262 	      && (action == decompress
   1263 		  || (action == compress
   1264 		      && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
   1265 	    {
   1266 	      /* Convert section name from .zdebug_* to .debug_* so
   1267 		 that linker will consider this section as a debug
   1268 		 section.  */
   1269 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   1270 	      if (new_name == NULL)
   1271 		return false;
   1272 	      bfd_rename_section (newsect, new_name);
   1273 	    }
   1274 	}
   1275       else
   1276 	/* For objdump, don't rename the section.  For objcopy, delay
   1277 	   section rename to elf_fake_sections.  */
   1278 	newsect->flags |= SEC_ELF_RENAME;
   1279     }
   1280 
   1281   /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
   1282      section.  */
   1283   if (startswith (name, ".gnu.lto_.lto."))
   1284     {
   1285       struct lto_section lsection;
   1286       if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
   1287 				    sizeof (struct lto_section)))
   1288 	abfd->lto_slim_object = lsection.slim_object;
   1289     }
   1290 
   1291   return true;
   1292 }
   1293 
   1294 const char *const bfd_elf_section_type_names[] =
   1295 {
   1296   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1297   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1298   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1299 };
   1300 
   1301 /* ELF relocs are against symbols.  If we are producing relocatable
   1302    output, and the reloc is against an external symbol, and nothing
   1303    has given us any additional addend, the resulting reloc will also
   1304    be against the same symbol.  In such a case, we don't want to
   1305    change anything about the way the reloc is handled, since it will
   1306    all be done at final link time.  Rather than put special case code
   1307    into bfd_perform_relocation, all the reloc types use this howto
   1308    function, or should call this function for relocatable output.  */
   1309 
   1310 bfd_reloc_status_type
   1311 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1312 		       arelent *reloc_entry,
   1313 		       asymbol *symbol,
   1314 		       void *data ATTRIBUTE_UNUSED,
   1315 		       asection *input_section,
   1316 		       bfd *output_bfd,
   1317 		       char **error_message ATTRIBUTE_UNUSED)
   1318 {
   1319   if (output_bfd != NULL
   1320       && (symbol->flags & BSF_SECTION_SYM) == 0
   1321       && (! reloc_entry->howto->partial_inplace
   1322 	  || reloc_entry->addend == 0))
   1323     {
   1324       reloc_entry->address += input_section->output_offset;
   1325       return bfd_reloc_ok;
   1326     }
   1327 
   1328   /* In some cases the relocation should be treated as output section
   1329      relative, as when linking ELF DWARF into PE COFF.  Many ELF
   1330      targets lack section relative relocations and instead use
   1331      ordinary absolute relocations for references between DWARF
   1332      sections.  That is arguably a bug in those targets but it happens
   1333      to work for the usual case of linking to non-loaded ELF debug
   1334      sections with VMAs forced to zero.  PE COFF on the other hand
   1335      doesn't allow a section VMA of zero.  */
   1336   if (output_bfd == NULL
   1337       && !reloc_entry->howto->pc_relative
   1338       && (symbol->section->flags & SEC_DEBUGGING) != 0
   1339       && (input_section->flags & SEC_DEBUGGING) != 0)
   1340     reloc_entry->addend -= symbol->section->output_section->vma;
   1341 
   1342   return bfd_reloc_continue;
   1343 }
   1344 
   1345 /* Returns TRUE if section A matches section B.
   1347    Names, addresses and links may be different, but everything else
   1348    should be the same.  */
   1349 
   1350 static bool
   1351 section_match (const Elf_Internal_Shdr * a,
   1352 	       const Elf_Internal_Shdr * b)
   1353 {
   1354   if (a->sh_type != b->sh_type
   1355       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1356       || a->sh_addralign != b->sh_addralign
   1357       || a->sh_entsize != b->sh_entsize)
   1358     return false;
   1359   if (a->sh_type == SHT_SYMTAB
   1360       || a->sh_type == SHT_STRTAB)
   1361     return true;
   1362   return a->sh_size == b->sh_size;
   1363 }
   1364 
   1365 /* Find a section in OBFD that has the same characteristics
   1366    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1367    none can be found.  Check's section HINT first, as this is likely
   1368    to be the correct section.  */
   1369 
   1370 static unsigned int
   1371 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1372 	   const unsigned int hint)
   1373 {
   1374   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1375   unsigned int i;
   1376 
   1377   BFD_ASSERT (iheader != NULL);
   1378 
   1379   /* See PR 20922 for a reproducer of the NULL test.  */
   1380   if (hint < elf_numsections (obfd)
   1381       && oheaders[hint] != NULL
   1382       && section_match (oheaders[hint], iheader))
   1383     return hint;
   1384 
   1385   for (i = 1; i < elf_numsections (obfd); i++)
   1386     {
   1387       Elf_Internal_Shdr * oheader = oheaders[i];
   1388 
   1389       if (oheader == NULL)
   1390 	continue;
   1391       if (section_match (oheader, iheader))
   1392 	/* FIXME: Do we care if there is a potential for
   1393 	   multiple matches ?  */
   1394 	return i;
   1395     }
   1396 
   1397   return SHN_UNDEF;
   1398 }
   1399 
   1400 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1401    Processor specific section, based upon a matching input section.
   1402    Returns TRUE upon success, FALSE otherwise.  */
   1403 
   1404 static bool
   1405 copy_special_section_fields (const bfd *ibfd,
   1406 			     bfd *obfd,
   1407 			     const Elf_Internal_Shdr *iheader,
   1408 			     Elf_Internal_Shdr *oheader,
   1409 			     const unsigned int secnum)
   1410 {
   1411   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1412   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1413   bool changed = false;
   1414   unsigned int sh_link;
   1415 
   1416   if (oheader->sh_type == SHT_NOBITS)
   1417     {
   1418       /* This is a feature for objcopy --only-keep-debug:
   1419 	 When a section's type is changed to NOBITS, we preserve
   1420 	 the sh_link and sh_info fields so that they can be
   1421 	 matched up with the original.
   1422 
   1423 	 Note: Strictly speaking these assignments are wrong.
   1424 	 The sh_link and sh_info fields should point to the
   1425 	 relevent sections in the output BFD, which may not be in
   1426 	 the same location as they were in the input BFD.  But
   1427 	 the whole point of this action is to preserve the
   1428 	 original values of the sh_link and sh_info fields, so
   1429 	 that they can be matched up with the section headers in
   1430 	 the original file.  So strictly speaking we may be
   1431 	 creating an invalid ELF file, but it is only for a file
   1432 	 that just contains debug info and only for sections
   1433 	 without any contents.  */
   1434       if (oheader->sh_link == 0)
   1435 	oheader->sh_link = iheader->sh_link;
   1436       if (oheader->sh_info == 0)
   1437 	oheader->sh_info = iheader->sh_info;
   1438       return true;
   1439     }
   1440 
   1441   /* Allow the target a chance to decide how these fields should be set.  */
   1442   if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1443 						    iheader, oheader))
   1444     return true;
   1445 
   1446   /* We have an iheader which might match oheader, and which has non-zero
   1447      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1448      the section in the output bfd which corresponds to the linked section
   1449      in the input bfd.  */
   1450   if (iheader->sh_link != SHN_UNDEF)
   1451     {
   1452       /* See PR 20931 for a reproducer.  */
   1453       if (iheader->sh_link >= elf_numsections (ibfd))
   1454 	{
   1455 	  _bfd_error_handler
   1456 	    /* xgettext:c-format */
   1457 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1458 	     ibfd, iheader->sh_link, secnum);
   1459 	  return false;
   1460 	}
   1461 
   1462       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1463       if (sh_link != SHN_UNDEF)
   1464 	{
   1465 	  oheader->sh_link = sh_link;
   1466 	  changed = true;
   1467 	}
   1468       else
   1469 	/* FIXME: Should we install iheader->sh_link
   1470 	   if we could not find a match ?  */
   1471 	_bfd_error_handler
   1472 	  /* xgettext:c-format */
   1473 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1474     }
   1475 
   1476   if (iheader->sh_info)
   1477     {
   1478       /* The sh_info field can hold arbitrary information, but if the
   1479 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1480 	 section index.  */
   1481       if (iheader->sh_flags & SHF_INFO_LINK)
   1482 	{
   1483 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1484 			       iheader->sh_info);
   1485 	  if (sh_link != SHN_UNDEF)
   1486 	    oheader->sh_flags |= SHF_INFO_LINK;
   1487 	}
   1488       else
   1489 	/* No idea what it means - just copy it.  */
   1490 	sh_link = iheader->sh_info;
   1491 
   1492       if (sh_link != SHN_UNDEF)
   1493 	{
   1494 	  oheader->sh_info = sh_link;
   1495 	  changed = true;
   1496 	}
   1497       else
   1498 	_bfd_error_handler
   1499 	  /* xgettext:c-format */
   1500 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1501     }
   1502 
   1503   return changed;
   1504 }
   1505 
   1506 /* Copy the program header and other data from one object module to
   1507    another.  */
   1508 
   1509 bool
   1510 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1511 {
   1512   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1513   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1514   const struct elf_backend_data *bed;
   1515   unsigned int i;
   1516 
   1517   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1518     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1519     return true;
   1520 
   1521   if (!elf_flags_init (obfd))
   1522     {
   1523       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1524       elf_flags_init (obfd) = true;
   1525     }
   1526 
   1527   elf_gp (obfd) = elf_gp (ibfd);
   1528 
   1529   /* Also copy the EI_OSABI field.  */
   1530   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1531     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1532 
   1533   /* If set, copy the EI_ABIVERSION field.  */
   1534   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1535     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1536       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1537 
   1538   /* Copy object attributes.  */
   1539   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1540 
   1541   if (iheaders == NULL || oheaders == NULL)
   1542     return true;
   1543 
   1544   bed = get_elf_backend_data (obfd);
   1545 
   1546   /* Possibly copy other fields in the section header.  */
   1547   for (i = 1; i < elf_numsections (obfd); i++)
   1548     {
   1549       unsigned int j;
   1550       Elf_Internal_Shdr * oheader = oheaders[i];
   1551 
   1552       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1553 	 because of a special case need for generating separate debug info
   1554 	 files.  See below for more details.  */
   1555       if (oheader == NULL
   1556 	  || (oheader->sh_type != SHT_NOBITS
   1557 	      && oheader->sh_type < SHT_LOOS))
   1558 	continue;
   1559 
   1560       /* Ignore empty sections, and sections whose
   1561 	 fields have already been initialised.  */
   1562       if (oheader->sh_size == 0
   1563 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1564 	continue;
   1565 
   1566       /* Scan for the matching section in the input bfd.
   1567 	 First we try for a direct mapping between the input and output sections.  */
   1568       for (j = 1; j < elf_numsections (ibfd); j++)
   1569 	{
   1570 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1571 
   1572 	  if (iheader == NULL)
   1573 	    continue;
   1574 
   1575 	  if (oheader->bfd_section != NULL
   1576 	      && iheader->bfd_section != NULL
   1577 	      && iheader->bfd_section->output_section != NULL
   1578 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1579 	    {
   1580 	      /* We have found a connection from the input section to the
   1581 		 output section.  Attempt to copy the header fields.  If
   1582 		 this fails then do not try any further sections - there
   1583 		 should only be a one-to-one mapping between input and output. */
   1584 	      if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1585 		j = elf_numsections (ibfd);
   1586 	      break;
   1587 	    }
   1588 	}
   1589 
   1590       if (j < elf_numsections (ibfd))
   1591 	continue;
   1592 
   1593       /* That failed.  So try to deduce the corresponding input section.
   1594 	 Unfortunately we cannot compare names as the output string table
   1595 	 is empty, so instead we check size, address and type.  */
   1596       for (j = 1; j < elf_numsections (ibfd); j++)
   1597 	{
   1598 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1599 
   1600 	  if (iheader == NULL)
   1601 	    continue;
   1602 
   1603 	  /* Try matching fields in the input section's header.
   1604 	     Since --only-keep-debug turns all non-debug sections into
   1605 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1606 	     input type.  */
   1607 	  if ((oheader->sh_type == SHT_NOBITS
   1608 	       || iheader->sh_type == oheader->sh_type)
   1609 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1610 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1611 	      && iheader->sh_addralign == oheader->sh_addralign
   1612 	      && iheader->sh_entsize == oheader->sh_entsize
   1613 	      && iheader->sh_size == oheader->sh_size
   1614 	      && iheader->sh_addr == oheader->sh_addr
   1615 	      && (iheader->sh_info != oheader->sh_info
   1616 		  || iheader->sh_link != oheader->sh_link))
   1617 	    {
   1618 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1619 		break;
   1620 	    }
   1621 	}
   1622 
   1623       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1624 	{
   1625 	  /* Final attempt.  Call the backend copy function
   1626 	     with a NULL input section.  */
   1627 	  (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1628 							       NULL, oheader);
   1629 	}
   1630     }
   1631 
   1632   return true;
   1633 }
   1634 
   1635 static const char *
   1636 get_segment_type (unsigned int p_type)
   1637 {
   1638   const char *pt;
   1639   switch (p_type)
   1640     {
   1641     case PT_NULL: pt = "NULL"; break;
   1642     case PT_LOAD: pt = "LOAD"; break;
   1643     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1644     case PT_INTERP: pt = "INTERP"; break;
   1645     case PT_NOTE: pt = "NOTE"; break;
   1646     case PT_SHLIB: pt = "SHLIB"; break;
   1647     case PT_PHDR: pt = "PHDR"; break;
   1648     case PT_TLS: pt = "TLS"; break;
   1649     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1650     case PT_GNU_STACK: pt = "STACK"; break;
   1651     case PT_GNU_RELRO: pt = "RELRO"; break;
   1652     default: pt = NULL; break;
   1653     }
   1654   return pt;
   1655 }
   1656 
   1657 /* Print out the program headers.  */
   1658 
   1659 bool
   1660 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1661 {
   1662   FILE *f = (FILE *) farg;
   1663   Elf_Internal_Phdr *p;
   1664   asection *s;
   1665   bfd_byte *dynbuf = NULL;
   1666 
   1667   p = elf_tdata (abfd)->phdr;
   1668   if (p != NULL)
   1669     {
   1670       unsigned int i, c;
   1671 
   1672       fprintf (f, _("\nProgram Header:\n"));
   1673       c = elf_elfheader (abfd)->e_phnum;
   1674       for (i = 0; i < c; i++, p++)
   1675 	{
   1676 	  const char *pt = get_segment_type (p->p_type);
   1677 	  char buf[20];
   1678 
   1679 	  if (pt == NULL)
   1680 	    {
   1681 	      sprintf (buf, "0x%lx", p->p_type);
   1682 	      pt = buf;
   1683 	    }
   1684 	  fprintf (f, "%8s off    0x", pt);
   1685 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1686 	  fprintf (f, " vaddr 0x");
   1687 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1688 	  fprintf (f, " paddr 0x");
   1689 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1690 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1691 	  fprintf (f, "         filesz 0x");
   1692 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1693 	  fprintf (f, " memsz 0x");
   1694 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1695 	  fprintf (f, " flags %c%c%c",
   1696 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1697 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1698 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1699 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1700 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1701 	  fprintf (f, "\n");
   1702 	}
   1703     }
   1704 
   1705   s = bfd_get_section_by_name (abfd, ".dynamic");
   1706   if (s != NULL)
   1707     {
   1708       unsigned int elfsec;
   1709       unsigned long shlink;
   1710       bfd_byte *extdyn, *extdynend;
   1711       size_t extdynsize;
   1712       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1713 
   1714       fprintf (f, _("\nDynamic Section:\n"));
   1715 
   1716       if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   1717 	goto error_return;
   1718 
   1719       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1720       if (elfsec == SHN_BAD)
   1721 	goto error_return;
   1722       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1723 
   1724       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1725       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1726 
   1727       extdyn = dynbuf;
   1728       /* PR 17512: file: 6f427532.  */
   1729       if (s->size < extdynsize)
   1730 	goto error_return;
   1731       extdynend = extdyn + s->size;
   1732       /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
   1733 	 Fix range check.  */
   1734       for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   1735 	{
   1736 	  Elf_Internal_Dyn dyn;
   1737 	  const char *name = "";
   1738 	  char ab[20];
   1739 	  bool stringp;
   1740 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1741 
   1742 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1743 
   1744 	  if (dyn.d_tag == DT_NULL)
   1745 	    break;
   1746 
   1747 	  stringp = false;
   1748 	  switch (dyn.d_tag)
   1749 	    {
   1750 	    default:
   1751 	      if (bed->elf_backend_get_target_dtag)
   1752 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1753 
   1754 	      if (!strcmp (name, ""))
   1755 		{
   1756 		  sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
   1757 		  name = ab;
   1758 		}
   1759 	      break;
   1760 
   1761 	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
   1762 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1763 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1764 	    case DT_HASH: name = "HASH"; break;
   1765 	    case DT_STRTAB: name = "STRTAB"; break;
   1766 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1767 	    case DT_RELA: name = "RELA"; break;
   1768 	    case DT_RELASZ: name = "RELASZ"; break;
   1769 	    case DT_RELAENT: name = "RELAENT"; break;
   1770 	    case DT_STRSZ: name = "STRSZ"; break;
   1771 	    case DT_SYMENT: name = "SYMENT"; break;
   1772 	    case DT_INIT: name = "INIT"; break;
   1773 	    case DT_FINI: name = "FINI"; break;
   1774 	    case DT_SONAME: name = "SONAME"; stringp = true; break;
   1775 	    case DT_RPATH: name = "RPATH"; stringp = true; break;
   1776 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1777 	    case DT_REL: name = "REL"; break;
   1778 	    case DT_RELSZ: name = "RELSZ"; break;
   1779 	    case DT_RELENT: name = "RELENT"; break;
   1780 	    case DT_RELR: name = "RELR"; break;
   1781 	    case DT_RELRSZ: name = "RELRSZ"; break;
   1782 	    case DT_RELRENT: name = "RELRENT"; break;
   1783 	    case DT_PLTREL: name = "PLTREL"; break;
   1784 	    case DT_DEBUG: name = "DEBUG"; break;
   1785 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1786 	    case DT_JMPREL: name = "JMPREL"; break;
   1787 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1788 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1789 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1790 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1791 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1792 	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
   1793 	    case DT_FLAGS: name = "FLAGS"; break;
   1794 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1795 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1796 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1797 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1798 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1799 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1800 	    case DT_FEATURE: name = "FEATURE"; break;
   1801 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1802 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1803 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1804 	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
   1805 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
   1806 	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
   1807 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1808 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1809 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1810 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1811 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1812 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1813 	    case DT_VERSYM: name = "VERSYM"; break;
   1814 	    case DT_VERDEF: name = "VERDEF"; break;
   1815 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1816 	    case DT_VERNEED: name = "VERNEED"; break;
   1817 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1818 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
   1819 	    case DT_USED: name = "USED"; break;
   1820 	    case DT_FILTER: name = "FILTER"; stringp = true; break;
   1821 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1822 	    }
   1823 
   1824 	  fprintf (f, "  %-20s ", name);
   1825 	  if (! stringp)
   1826 	    {
   1827 	      fprintf (f, "0x");
   1828 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1829 	    }
   1830 	  else
   1831 	    {
   1832 	      const char *string;
   1833 	      unsigned int tagv = dyn.d_un.d_val;
   1834 
   1835 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1836 	      if (string == NULL)
   1837 		goto error_return;
   1838 	      fprintf (f, "%s", string);
   1839 	    }
   1840 	  fprintf (f, "\n");
   1841 	}
   1842 
   1843       free (dynbuf);
   1844       dynbuf = NULL;
   1845     }
   1846 
   1847   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1848       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1849     {
   1850       if (! _bfd_elf_slurp_version_tables (abfd, false))
   1851 	return false;
   1852     }
   1853 
   1854   if (elf_dynverdef (abfd) != 0)
   1855     {
   1856       Elf_Internal_Verdef *t;
   1857 
   1858       fprintf (f, _("\nVersion definitions:\n"));
   1859       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1860 	{
   1861 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1862 		   t->vd_flags, t->vd_hash,
   1863 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1864 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1865 	    {
   1866 	      Elf_Internal_Verdaux *a;
   1867 
   1868 	      fprintf (f, "\t");
   1869 	      for (a = t->vd_auxptr->vda_nextptr;
   1870 		   a != NULL;
   1871 		   a = a->vda_nextptr)
   1872 		fprintf (f, "%s ",
   1873 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1874 	      fprintf (f, "\n");
   1875 	    }
   1876 	}
   1877     }
   1878 
   1879   if (elf_dynverref (abfd) != 0)
   1880     {
   1881       Elf_Internal_Verneed *t;
   1882 
   1883       fprintf (f, _("\nVersion References:\n"));
   1884       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1885 	{
   1886 	  Elf_Internal_Vernaux *a;
   1887 
   1888 	  fprintf (f, _("  required from %s:\n"),
   1889 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1890 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1891 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1892 		     a->vna_flags, a->vna_other,
   1893 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1894 	}
   1895     }
   1896 
   1897   return true;
   1898 
   1899  error_return:
   1900   free (dynbuf);
   1901   return false;
   1902 }
   1903 
   1904 /* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
   1905    and return symbol version for symbol version itself.   */
   1906 
   1907 const char *
   1908 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   1909 				    bool base_p,
   1910 				    bool *hidden)
   1911 {
   1912   const char *version_string = NULL;
   1913   if (elf_dynversym (abfd) != 0
   1914       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   1915     {
   1916       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   1917 
   1918       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   1919       vernum &= VERSYM_VERSION;
   1920 
   1921       if (vernum == 0)
   1922 	version_string = "";
   1923       else if (vernum == 1
   1924 	       && (vernum > elf_tdata (abfd)->cverdefs
   1925 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   1926 		       == VER_FLG_BASE)))
   1927 	version_string = base_p ? "Base" : "";
   1928       else if (vernum <= elf_tdata (abfd)->cverdefs)
   1929 	{
   1930 	  const char *nodename
   1931 	    = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   1932 	  version_string = "";
   1933 	  if (base_p
   1934 	      || nodename == NULL
   1935 	      || symbol->name == NULL
   1936 	      || strcmp (symbol->name, nodename) != 0)
   1937 	    version_string = nodename;
   1938 	}
   1939       else
   1940 	{
   1941 	  Elf_Internal_Verneed *t;
   1942 
   1943 	  version_string = _("<corrupt>");
   1944 	  for (t = elf_tdata (abfd)->verref;
   1945 	       t != NULL;
   1946 	       t = t->vn_nextref)
   1947 	    {
   1948 	      Elf_Internal_Vernaux *a;
   1949 
   1950 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1951 		{
   1952 		  if (a->vna_other == vernum)
   1953 		    {
   1954 		      *hidden = true;
   1955 		      version_string = a->vna_nodename;
   1956 		      break;
   1957 		    }
   1958 		}
   1959 	    }
   1960 	}
   1961     }
   1962   return version_string;
   1963 }
   1964 
   1965 /* Display ELF-specific fields of a symbol.  */
   1966 
   1967 void
   1968 bfd_elf_print_symbol (bfd *abfd,
   1969 		      void *filep,
   1970 		      asymbol *symbol,
   1971 		      bfd_print_symbol_type how)
   1972 {
   1973   FILE *file = (FILE *) filep;
   1974   switch (how)
   1975     {
   1976     case bfd_print_symbol_name:
   1977       fprintf (file, "%s", symbol->name);
   1978       break;
   1979     case bfd_print_symbol_more:
   1980       fprintf (file, "elf ");
   1981       bfd_fprintf_vma (abfd, file, symbol->value);
   1982       fprintf (file, " %x", symbol->flags);
   1983       break;
   1984     case bfd_print_symbol_all:
   1985       {
   1986 	const char *section_name;
   1987 	const char *name = NULL;
   1988 	const struct elf_backend_data *bed;
   1989 	unsigned char st_other;
   1990 	bfd_vma val;
   1991 	const char *version_string;
   1992 	bool hidden;
   1993 
   1994 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   1995 
   1996 	bed = get_elf_backend_data (abfd);
   1997 	if (bed->elf_backend_print_symbol_all)
   1998 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   1999 
   2000 	if (name == NULL)
   2001 	  {
   2002 	    name = symbol->name;
   2003 	    bfd_print_symbol_vandf (abfd, file, symbol);
   2004 	  }
   2005 
   2006 	fprintf (file, " %s\t", section_name);
   2007 	/* Print the "other" value for a symbol.  For common symbols,
   2008 	   we've already printed the size; now print the alignment.
   2009 	   For other symbols, we have no specified alignment, and
   2010 	   we've printed the address; now print the size.  */
   2011 	if (symbol->section && bfd_is_com_section (symbol->section))
   2012 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   2013 	else
   2014 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   2015 	bfd_fprintf_vma (abfd, file, val);
   2016 
   2017 	/* If we have version information, print it.  */
   2018 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2019 							     symbol,
   2020 							     true,
   2021 							     &hidden);
   2022 	if (version_string)
   2023 	  {
   2024 	    if (!hidden)
   2025 	      fprintf (file, "  %-11s", version_string);
   2026 	    else
   2027 	      {
   2028 		int i;
   2029 
   2030 		fprintf (file, " (%s)", version_string);
   2031 		for (i = 10 - strlen (version_string); i > 0; --i)
   2032 		  putc (' ', file);
   2033 	      }
   2034 	  }
   2035 
   2036 	/* If the st_other field is not zero, print it.  */
   2037 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2038 
   2039 	switch (st_other)
   2040 	  {
   2041 	  case 0: break;
   2042 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2043 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2044 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2045 	  default:
   2046 	    /* Some other non-defined flags are also present, so print
   2047 	       everything hex.  */
   2048 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2049 	  }
   2050 
   2051 	fprintf (file, " %s", name);
   2052       }
   2053       break;
   2054     }
   2055 }
   2056 
   2057 /* ELF .o/exec file reading */
   2059 
   2060 /* Create a new bfd section from an ELF section header.  */
   2061 
   2062 bool
   2063 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2064 {
   2065   Elf_Internal_Shdr *hdr;
   2066   Elf_Internal_Ehdr *ehdr;
   2067   const struct elf_backend_data *bed;
   2068   const char *name;
   2069   bool ret = true;
   2070 
   2071   if (shindex >= elf_numsections (abfd))
   2072     return false;
   2073 
   2074   /* PR17512: A corrupt ELF binary might contain a loop of sections via
   2075      sh_link or sh_info.  Detect this here, by refusing to load a
   2076      section that we are already in the process of loading.  */
   2077   if (elf_tdata (abfd)->being_created[shindex])
   2078     {
   2079       _bfd_error_handler
   2080 	(_("%pB: warning: loop in section dependencies detected"), abfd);
   2081       return false;
   2082     }
   2083   elf_tdata (abfd)->being_created[shindex] = true;
   2084 
   2085   hdr = elf_elfsections (abfd)[shindex];
   2086   ehdr = elf_elfheader (abfd);
   2087   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2088 					  hdr->sh_name);
   2089   if (name == NULL)
   2090     goto fail;
   2091 
   2092   bed = get_elf_backend_data (abfd);
   2093   switch (hdr->sh_type)
   2094     {
   2095     case SHT_NULL:
   2096       /* Inactive section. Throw it away.  */
   2097       goto success;
   2098 
   2099     case SHT_PROGBITS:		/* Normal section with contents.  */
   2100     case SHT_NOBITS:		/* .bss section.  */
   2101     case SHT_HASH:		/* .hash section.  */
   2102     case SHT_NOTE:		/* .note section.  */
   2103     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2104     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2105     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2106     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2107     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2108       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2109       goto success;
   2110 
   2111     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2112       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2113 	goto fail;
   2114 
   2115       if (hdr->sh_link > elf_numsections (abfd))
   2116 	{
   2117 	  /* PR 10478: Accept Solaris binaries with a sh_link
   2118 	     field set to SHN_BEFORE or SHN_AFTER.  */
   2119 	  switch (bfd_get_arch (abfd))
   2120 	    {
   2121 	    case bfd_arch_i386:
   2122 	    case bfd_arch_sparc:
   2123 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
   2124 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
   2125 		break;
   2126 	      /* Otherwise fall through.  */
   2127 	    default:
   2128 	      goto fail;
   2129 	    }
   2130 	}
   2131       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2132 	goto fail;
   2133       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2134 	{
   2135 	  Elf_Internal_Shdr *dynsymhdr;
   2136 
   2137 	  /* The shared libraries distributed with hpux11 have a bogus
   2138 	     sh_link field for the ".dynamic" section.  Find the
   2139 	     string table for the ".dynsym" section instead.  */
   2140 	  if (elf_dynsymtab (abfd) != 0)
   2141 	    {
   2142 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2143 	      hdr->sh_link = dynsymhdr->sh_link;
   2144 	    }
   2145 	  else
   2146 	    {
   2147 	      unsigned int i, num_sec;
   2148 
   2149 	      num_sec = elf_numsections (abfd);
   2150 	      for (i = 1; i < num_sec; i++)
   2151 		{
   2152 		  dynsymhdr = elf_elfsections (abfd)[i];
   2153 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2154 		    {
   2155 		      hdr->sh_link = dynsymhdr->sh_link;
   2156 		      break;
   2157 		    }
   2158 		}
   2159 	    }
   2160 	}
   2161       goto success;
   2162 
   2163     case SHT_SYMTAB:		/* A symbol table.  */
   2164       if (elf_onesymtab (abfd) == shindex)
   2165 	goto success;
   2166 
   2167       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2168 	goto fail;
   2169 
   2170       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2171 	{
   2172 	  if (hdr->sh_size != 0)
   2173 	    goto fail;
   2174 	  /* Some assemblers erroneously set sh_info to one with a
   2175 	     zero sh_size.  ld sees this as a global symbol count
   2176 	     of (unsigned) -1.  Fix it here.  */
   2177 	  hdr->sh_info = 0;
   2178 	  goto success;
   2179 	}
   2180 
   2181       /* PR 18854: A binary might contain more than one symbol table.
   2182 	 Unusual, but possible.  Warn, but continue.  */
   2183       if (elf_onesymtab (abfd) != 0)
   2184 	{
   2185 	  _bfd_error_handler
   2186 	    /* xgettext:c-format */
   2187 	    (_("%pB: warning: multiple symbol tables detected"
   2188 	       " - ignoring the table in section %u"),
   2189 	     abfd, shindex);
   2190 	  goto success;
   2191 	}
   2192       elf_onesymtab (abfd) = shindex;
   2193       elf_symtab_hdr (abfd) = *hdr;
   2194       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2195       abfd->flags |= HAS_SYMS;
   2196 
   2197       /* Sometimes a shared object will map in the symbol table.  If
   2198 	 SHF_ALLOC is set, and this is a shared object, then we also
   2199 	 treat this section as a BFD section.  We can not base the
   2200 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2201 	 set in a relocatable object file, which would confuse the
   2202 	 linker.  */
   2203       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2204 	  && (abfd->flags & DYNAMIC) != 0
   2205 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2206 						shindex))
   2207 	goto fail;
   2208 
   2209       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2210 	 can't read symbols without that section loaded as well.  It
   2211 	 is most likely specified by the next section header.  */
   2212       {
   2213 	elf_section_list * entry;
   2214 	unsigned int i, num_sec;
   2215 
   2216 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2217 	  if (entry->hdr.sh_link == shindex)
   2218 	    goto success;
   2219 
   2220 	num_sec = elf_numsections (abfd);
   2221 	for (i = shindex + 1; i < num_sec; i++)
   2222 	  {
   2223 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2224 
   2225 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2226 		&& hdr2->sh_link == shindex)
   2227 	      break;
   2228 	  }
   2229 
   2230 	if (i == num_sec)
   2231 	  for (i = 1; i < shindex; i++)
   2232 	    {
   2233 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2234 
   2235 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2236 		  && hdr2->sh_link == shindex)
   2237 		break;
   2238 	    }
   2239 
   2240 	if (i != shindex)
   2241 	  ret = bfd_section_from_shdr (abfd, i);
   2242 	/* else FIXME: we have failed to find the symbol table - should we issue an error ? */
   2243 	goto success;
   2244       }
   2245 
   2246     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2247       if (elf_dynsymtab (abfd) == shindex)
   2248 	goto success;
   2249 
   2250       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2251 	goto fail;
   2252 
   2253       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2254 	{
   2255 	  if (hdr->sh_size != 0)
   2256 	    goto fail;
   2257 
   2258 	  /* Some linkers erroneously set sh_info to one with a
   2259 	     zero sh_size.  ld sees this as a global symbol count
   2260 	     of (unsigned) -1.  Fix it here.  */
   2261 	  hdr->sh_info = 0;
   2262 	  goto success;
   2263 	}
   2264 
   2265       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2266 	 Unusual, but possible.  Warn, but continue.  */
   2267       if (elf_dynsymtab (abfd) != 0)
   2268 	{
   2269 	  _bfd_error_handler
   2270 	    /* xgettext:c-format */
   2271 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2272 	       " - ignoring the table in section %u"),
   2273 	     abfd, shindex);
   2274 	  goto success;
   2275 	}
   2276       elf_dynsymtab (abfd) = shindex;
   2277       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2278       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2279       abfd->flags |= HAS_SYMS;
   2280 
   2281       /* Besides being a symbol table, we also treat this as a regular
   2282 	 section, so that objcopy can handle it.  */
   2283       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2284       goto success;
   2285 
   2286     case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections.  */
   2287       {
   2288 	elf_section_list * entry;
   2289 
   2290 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2291 	  if (entry->ndx == shindex)
   2292 	    goto success;
   2293 
   2294 	entry = bfd_alloc (abfd, sizeof (*entry));
   2295 	if (entry == NULL)
   2296 	  goto fail;
   2297 	entry->ndx = shindex;
   2298 	entry->hdr = * hdr;
   2299 	entry->next = elf_symtab_shndx_list (abfd);
   2300 	elf_symtab_shndx_list (abfd) = entry;
   2301 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2302 	goto success;
   2303       }
   2304 
   2305     case SHT_STRTAB:		/* A string table.  */
   2306       if (hdr->bfd_section != NULL)
   2307 	goto success;
   2308 
   2309       if (ehdr->e_shstrndx == shindex)
   2310 	{
   2311 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2312 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2313 	  goto success;
   2314 	}
   2315 
   2316       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2317 	{
   2318 	symtab_strtab:
   2319 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2320 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2321 	  goto success;
   2322 	}
   2323 
   2324       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2325 	{
   2326 	dynsymtab_strtab:
   2327 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2328 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2329 	  elf_elfsections (abfd)[shindex] = hdr;
   2330 	  /* We also treat this as a regular section, so that objcopy
   2331 	     can handle it.  */
   2332 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2333 						 shindex);
   2334 	  goto success;
   2335 	}
   2336 
   2337       /* If the string table isn't one of the above, then treat it as a
   2338 	 regular section.  We need to scan all the headers to be sure,
   2339 	 just in case this strtab section appeared before the above.  */
   2340       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2341 	{
   2342 	  unsigned int i, num_sec;
   2343 
   2344 	  num_sec = elf_numsections (abfd);
   2345 	  for (i = 1; i < num_sec; i++)
   2346 	    {
   2347 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2348 	      if (hdr2->sh_link == shindex)
   2349 		{
   2350 		  /* Prevent endless recursion on broken objects.  */
   2351 		  if (i == shindex)
   2352 		    goto fail;
   2353 		  if (! bfd_section_from_shdr (abfd, i))
   2354 		    goto fail;
   2355 		  if (elf_onesymtab (abfd) == i)
   2356 		    goto symtab_strtab;
   2357 		  if (elf_dynsymtab (abfd) == i)
   2358 		    goto dynsymtab_strtab;
   2359 		}
   2360 	    }
   2361 	}
   2362       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2363       goto success;
   2364 
   2365     case SHT_REL:
   2366     case SHT_RELA:
   2367     case SHT_RELR:
   2368       /* *These* do a lot of work -- but build no sections!  */
   2369       {
   2370 	asection *target_sect;
   2371 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2372 	unsigned int num_sec = elf_numsections (abfd);
   2373 	struct bfd_elf_section_data *esdt;
   2374 	bfd_size_type size;
   2375 
   2376 	if (hdr->sh_type == SHT_REL)
   2377 	  size = bed->s->sizeof_rel;
   2378 	else if (hdr->sh_type == SHT_RELA)
   2379 	  size = bed->s->sizeof_rela;
   2380 	else
   2381 	  size = bed->s->arch_size / 8;
   2382 	if (hdr->sh_entsize != size)
   2383 	  goto fail;
   2384 
   2385 	/* Check for a bogus link to avoid crashing.  */
   2386 	if (hdr->sh_link >= num_sec)
   2387 	  {
   2388 	    _bfd_error_handler
   2389 	      /* xgettext:c-format */
   2390 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2391 	       abfd, hdr->sh_link, name, shindex);
   2392 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2393 						   shindex);
   2394 	    goto success;
   2395 	  }
   2396 
   2397 	/* Get the symbol table.  */
   2398 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2399 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2400 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2401 	  goto fail;
   2402 
   2403 	/* If this is an alloc section in an executable or shared
   2404 	   library, or the reloc section does not use the main symbol
   2405 	   table we don't treat it as a reloc section.  BFD can't
   2406 	   adequately represent such a section, so at least for now,
   2407 	   we don't try.  We just present it as a normal section.  We
   2408 	   also can't use it as a reloc section if it points to the
   2409 	   null section, an invalid section, another reloc section, or
   2410 	   its sh_link points to the null section.  */
   2411 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2412 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2413 	    || hdr->sh_link == SHN_UNDEF
   2414 	    || hdr->sh_link != elf_onesymtab (abfd)
   2415 	    || hdr->sh_info == SHN_UNDEF
   2416 	    || hdr->sh_info >= num_sec
   2417 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2418 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2419 	  {
   2420 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2421 						   shindex);
   2422 	    goto success;
   2423 	  }
   2424 
   2425 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2426 	  goto fail;
   2427 
   2428 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2429 	if (target_sect == NULL)
   2430 	  goto fail;
   2431 
   2432 	esdt = elf_section_data (target_sect);
   2433 	if (hdr->sh_type == SHT_RELA)
   2434 	  p_hdr = &esdt->rela.hdr;
   2435 	else
   2436 	  p_hdr = &esdt->rel.hdr;
   2437 
   2438 	/* PR 17512: file: 0b4f81b7.
   2439 	   Also see PR 24456, for a file which deliberately has two reloc
   2440 	   sections.  */
   2441 	if (*p_hdr != NULL)
   2442 	  {
   2443 	    if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
   2444 	      {
   2445 		_bfd_error_handler
   2446 		  /* xgettext:c-format */
   2447 		  (_("%pB: warning: secondary relocation section '%s' "
   2448 		     "for section %pA found - ignoring"),
   2449 		   abfd, name, target_sect);
   2450 	      }
   2451 	    else
   2452 	      esdt->has_secondary_relocs = true;
   2453 	    goto success;
   2454 	  }
   2455 
   2456 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2457 	if (hdr2 == NULL)
   2458 	  goto fail;
   2459 	*hdr2 = *hdr;
   2460 	*p_hdr = hdr2;
   2461 	elf_elfsections (abfd)[shindex] = hdr2;
   2462 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2463 				     * bed->s->int_rels_per_ext_rel);
   2464 	target_sect->flags |= SEC_RELOC;
   2465 	target_sect->relocation = NULL;
   2466 	target_sect->rel_filepos = hdr->sh_offset;
   2467 	/* In the section to which the relocations apply, mark whether
   2468 	   its relocations are of the REL or RELA variety.  */
   2469 	if (hdr->sh_size != 0)
   2470 	  {
   2471 	    if (hdr->sh_type == SHT_RELA)
   2472 	      target_sect->use_rela_p = 1;
   2473 	  }
   2474 	abfd->flags |= HAS_RELOC;
   2475 	goto success;
   2476       }
   2477 
   2478     case SHT_GNU_verdef:
   2479       elf_dynverdef (abfd) = shindex;
   2480       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2481       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2482       goto success;
   2483 
   2484     case SHT_GNU_versym:
   2485       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2486 	goto fail;
   2487 
   2488       elf_dynversym (abfd) = shindex;
   2489       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2490       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2491       goto success;
   2492 
   2493     case SHT_GNU_verneed:
   2494       elf_dynverref (abfd) = shindex;
   2495       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2496       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2497       goto success;
   2498 
   2499     case SHT_SHLIB:
   2500       goto success;
   2501 
   2502     case SHT_GROUP:
   2503       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   2504 	goto fail;
   2505 
   2506       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2507 	goto fail;
   2508 
   2509       goto success;
   2510 
   2511     default:
   2512       /* Possibly an attributes section.  */
   2513       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2514 	  || hdr->sh_type == bed->obj_attrs_section_type)
   2515 	{
   2516 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2517 	    goto fail;
   2518 	  _bfd_elf_parse_attributes (abfd, hdr);
   2519 	  goto success;
   2520 	}
   2521 
   2522       /* Check for any processor-specific section types.  */
   2523       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2524 	goto success;
   2525 
   2526       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2527 	{
   2528 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2529 	    /* FIXME: How to properly handle allocated section reserved
   2530 	       for applications?  */
   2531 	    _bfd_error_handler
   2532 	      /* xgettext:c-format */
   2533 	      (_("%pB: unknown type [%#x] section `%s'"),
   2534 	       abfd, hdr->sh_type, name);
   2535 	  else
   2536 	    {
   2537 	      /* Allow sections reserved for applications.  */
   2538 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2539 						     shindex);
   2540 	      goto success;
   2541 	    }
   2542 	}
   2543       else if (hdr->sh_type >= SHT_LOPROC
   2544 	       && hdr->sh_type <= SHT_HIPROC)
   2545 	/* FIXME: We should handle this section.  */
   2546 	_bfd_error_handler
   2547 	  /* xgettext:c-format */
   2548 	  (_("%pB: unknown type [%#x] section `%s'"),
   2549 	   abfd, hdr->sh_type, name);
   2550       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2551 	{
   2552 	  /* Unrecognised OS-specific sections.  */
   2553 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2554 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2555 	       required to correctly process the section and the file should
   2556 	       be rejected with an error message.  */
   2557 	    _bfd_error_handler
   2558 	      /* xgettext:c-format */
   2559 	      (_("%pB: unknown type [%#x] section `%s'"),
   2560 	       abfd, hdr->sh_type, name);
   2561 	  else
   2562 	    {
   2563 	      /* Otherwise it should be processed.  */
   2564 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2565 	      goto success;
   2566 	    }
   2567 	}
   2568       else
   2569 	/* FIXME: We should handle this section.  */
   2570 	_bfd_error_handler
   2571 	  /* xgettext:c-format */
   2572 	  (_("%pB: unknown type [%#x] section `%s'"),
   2573 	   abfd, hdr->sh_type, name);
   2574 
   2575       goto fail;
   2576     }
   2577 
   2578  fail:
   2579   ret = false;
   2580  success:
   2581   elf_tdata (abfd)->being_created[shindex] = false;
   2582   return ret;
   2583 }
   2584 
   2585 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   2586 
   2587 Elf_Internal_Sym *
   2588 bfd_sym_from_r_symndx (struct sym_cache *cache,
   2589 		       bfd *abfd,
   2590 		       unsigned long r_symndx)
   2591 {
   2592   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   2593 
   2594   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   2595     {
   2596       Elf_Internal_Shdr *symtab_hdr;
   2597       unsigned char esym[sizeof (Elf64_External_Sym)];
   2598       Elf_External_Sym_Shndx eshndx;
   2599 
   2600       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2601       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   2602 				&cache->sym[ent], esym, &eshndx) == NULL)
   2603 	return NULL;
   2604 
   2605       if (cache->abfd != abfd)
   2606 	{
   2607 	  memset (cache->indx, -1, sizeof (cache->indx));
   2608 	  cache->abfd = abfd;
   2609 	}
   2610       cache->indx[ent] = r_symndx;
   2611     }
   2612 
   2613   return &cache->sym[ent];
   2614 }
   2615 
   2616 /* Given an ELF section number, retrieve the corresponding BFD
   2617    section.  */
   2618 
   2619 asection *
   2620 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   2621 {
   2622   if (sec_index >= elf_numsections (abfd))
   2623     return NULL;
   2624   return elf_elfsections (abfd)[sec_index]->bfd_section;
   2625 }
   2626 
   2627 static const struct bfd_elf_special_section special_sections_b[] =
   2628 {
   2629   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2630   { NULL,		    0,	0, 0,		 0 }
   2631 };
   2632 
   2633 static const struct bfd_elf_special_section special_sections_c[] =
   2634 {
   2635   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   2636   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   2637   { NULL,			0, 0, 0,	    0 }
   2638 };
   2639 
   2640 static const struct bfd_elf_special_section special_sections_d[] =
   2641 {
   2642   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2643   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2644   /* There are more DWARF sections than these, but they needn't be added here
   2645      unless you have to cope with broken compilers that don't emit section
   2646      attributes or you want to help the user writing assembler.  */
   2647   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   2648   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   2649   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   2650   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   2651   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   2652   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   2653   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   2654   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   2655   { NULL,		       0,	 0, 0,		  0 }
   2656 };
   2657 
   2658 static const struct bfd_elf_special_section special_sections_f[] =
   2659 {
   2660   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2661   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   2662   { NULL,			   0 , 0, 0,		  0 }
   2663 };
   2664 
   2665 static const struct bfd_elf_special_section special_sections_g[] =
   2666 {
   2667   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2668   { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2669   { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2670   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   2671   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2672   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   2673   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   2674   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   2675   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   2676   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   2677   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   2678   { NULL,			 0,	   0, 0,	       0 }
   2679 };
   2680 
   2681 static const struct bfd_elf_special_section special_sections_h[] =
   2682 {
   2683   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   2684   { NULL,		     0, 0, 0,		 0 }
   2685 };
   2686 
   2687 static const struct bfd_elf_special_section special_sections_i[] =
   2688 {
   2689   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2690   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2691   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   2692   { NULL,		       0,      0, 0,		  0 }
   2693 };
   2694 
   2695 static const struct bfd_elf_special_section special_sections_l[] =
   2696 {
   2697   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   2698   { NULL,		     0, 0, 0,		 0 }
   2699 };
   2700 
   2701 static const struct bfd_elf_special_section special_sections_n[] =
   2702 {
   2703   { STRING_COMMA_LEN (".noinit"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2704   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   2705   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   2706   { NULL,		     0,		  0, 0,		   0 }
   2707 };
   2708 
   2709 static const struct bfd_elf_special_section special_sections_p[] =
   2710 {
   2711   { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,	SHF_ALLOC + SHF_WRITE },
   2712   { STRING_COMMA_LEN (".persistent"),	 -2, SHT_PROGBITS,	SHF_ALLOC + SHF_WRITE },
   2713   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2714   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   2715   { NULL,		    0,		  0, 0,			0 }
   2716 };
   2717 
   2718 static const struct bfd_elf_special_section special_sections_r[] =
   2719 {
   2720   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   2721   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   2722   { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
   2723   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   2724   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   2725   { NULL,		    0,	   0, 0,	    0 }
   2726 };
   2727 
   2728 static const struct bfd_elf_special_section special_sections_s[] =
   2729 {
   2730   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   2731   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   2732   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   2733   /* See struct bfd_elf_special_section declaration for the semantics of
   2734      this special case where .prefix_length != strlen (.prefix).  */
   2735   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   2736   { NULL,			0,  0, 0,	   0 }
   2737 };
   2738 
   2739 static const struct bfd_elf_special_section special_sections_t[] =
   2740 {
   2741   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   2742   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2743   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2744   { NULL,		      0,  0, 0,		   0 }
   2745 };
   2746 
   2747 static const struct bfd_elf_special_section special_sections_z[] =
   2748 {
   2749   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   2750   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   2751   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   2752   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   2753   { NULL,		      0,  0, 0,		   0 }
   2754 };
   2755 
   2756 static const struct bfd_elf_special_section * const special_sections[] =
   2757 {
   2758   special_sections_b,		/* 'b' */
   2759   special_sections_c,		/* 'c' */
   2760   special_sections_d,		/* 'd' */
   2761   NULL,				/* 'e' */
   2762   special_sections_f,		/* 'f' */
   2763   special_sections_g,		/* 'g' */
   2764   special_sections_h,		/* 'h' */
   2765   special_sections_i,		/* 'i' */
   2766   NULL,				/* 'j' */
   2767   NULL,				/* 'k' */
   2768   special_sections_l,		/* 'l' */
   2769   NULL,				/* 'm' */
   2770   special_sections_n,		/* 'n' */
   2771   NULL,				/* 'o' */
   2772   special_sections_p,		/* 'p' */
   2773   NULL,				/* 'q' */
   2774   special_sections_r,		/* 'r' */
   2775   special_sections_s,		/* 's' */
   2776   special_sections_t,		/* 't' */
   2777   NULL,				/* 'u' */
   2778   NULL,				/* 'v' */
   2779   NULL,				/* 'w' */
   2780   NULL,				/* 'x' */
   2781   NULL,				/* 'y' */
   2782   special_sections_z		/* 'z' */
   2783 };
   2784 
   2785 const struct bfd_elf_special_section *
   2786 _bfd_elf_get_special_section (const char *name,
   2787 			      const struct bfd_elf_special_section *spec,
   2788 			      unsigned int rela)
   2789 {
   2790   int i;
   2791   int len;
   2792 
   2793   len = strlen (name);
   2794 
   2795   for (i = 0; spec[i].prefix != NULL; i++)
   2796     {
   2797       int suffix_len;
   2798       int prefix_len = spec[i].prefix_length;
   2799 
   2800       if (len < prefix_len)
   2801 	continue;
   2802       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   2803 	continue;
   2804 
   2805       suffix_len = spec[i].suffix_length;
   2806       if (suffix_len <= 0)
   2807 	{
   2808 	  if (name[prefix_len] != 0)
   2809 	    {
   2810 	      if (suffix_len == 0)
   2811 		continue;
   2812 	      if (name[prefix_len] != '.'
   2813 		  && (suffix_len == -2
   2814 		      || (rela && spec[i].type == SHT_REL)))
   2815 		continue;
   2816 	    }
   2817 	}
   2818       else
   2819 	{
   2820 	  if (len < prefix_len + suffix_len)
   2821 	    continue;
   2822 	  if (memcmp (name + len - suffix_len,
   2823 		      spec[i].prefix + prefix_len,
   2824 		      suffix_len) != 0)
   2825 	    continue;
   2826 	}
   2827       return &spec[i];
   2828     }
   2829 
   2830   return NULL;
   2831 }
   2832 
   2833 const struct bfd_elf_special_section *
   2834 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   2835 {
   2836   int i;
   2837   const struct bfd_elf_special_section *spec;
   2838   const struct elf_backend_data *bed;
   2839 
   2840   /* See if this is one of the special sections.  */
   2841   if (sec->name == NULL)
   2842     return NULL;
   2843 
   2844   bed = get_elf_backend_data (abfd);
   2845   spec = bed->special_sections;
   2846   if (spec)
   2847     {
   2848       spec = _bfd_elf_get_special_section (sec->name,
   2849 					   bed->special_sections,
   2850 					   sec->use_rela_p);
   2851       if (spec != NULL)
   2852 	return spec;
   2853     }
   2854 
   2855   if (sec->name[0] != '.')
   2856     return NULL;
   2857 
   2858   i = sec->name[1] - 'b';
   2859   if (i < 0 || i > 'z' - 'b')
   2860     return NULL;
   2861 
   2862   spec = special_sections[i];
   2863 
   2864   if (spec == NULL)
   2865     return NULL;
   2866 
   2867   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   2868 }
   2869 
   2870 bool
   2871 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   2872 {
   2873   struct bfd_elf_section_data *sdata;
   2874   const struct elf_backend_data *bed;
   2875   const struct bfd_elf_special_section *ssect;
   2876 
   2877   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   2878   if (sdata == NULL)
   2879     {
   2880       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   2881 							  sizeof (*sdata));
   2882       if (sdata == NULL)
   2883 	return false;
   2884       sec->used_by_bfd = sdata;
   2885     }
   2886 
   2887   /* Indicate whether or not this section should use RELA relocations.  */
   2888   bed = get_elf_backend_data (abfd);
   2889   sec->use_rela_p = bed->default_use_rela_p;
   2890 
   2891   /* Set up ELF section type and flags for newly created sections, if
   2892      there is an ABI mandated section.  */
   2893   ssect = (*bed->get_sec_type_attr) (abfd, sec);
   2894   if (ssect != NULL)
   2895     {
   2896       elf_section_type (sec) = ssect->type;
   2897       elf_section_flags (sec) = ssect->attr;
   2898     }
   2899 
   2900   return _bfd_generic_new_section_hook (abfd, sec);
   2901 }
   2902 
   2903 /* Create a new bfd section from an ELF program header.
   2904 
   2905    Since program segments have no names, we generate a synthetic name
   2906    of the form segment<NUM>, where NUM is generally the index in the
   2907    program header table.  For segments that are split (see below) we
   2908    generate the names segment<NUM>a and segment<NUM>b.
   2909 
   2910    Note that some program segments may have a file size that is different than
   2911    (less than) the memory size.  All this means is that at execution the
   2912    system must allocate the amount of memory specified by the memory size,
   2913    but only initialize it with the first "file size" bytes read from the
   2914    file.  This would occur for example, with program segments consisting
   2915    of combined data+bss.
   2916 
   2917    To handle the above situation, this routine generates TWO bfd sections
   2918    for the single program segment.  The first has the length specified by
   2919    the file size of the segment, and the second has the length specified
   2920    by the difference between the two sizes.  In effect, the segment is split
   2921    into its initialized and uninitialized parts.
   2922 
   2923  */
   2924 
   2925 bool
   2926 _bfd_elf_make_section_from_phdr (bfd *abfd,
   2927 				 Elf_Internal_Phdr *hdr,
   2928 				 int hdr_index,
   2929 				 const char *type_name)
   2930 {
   2931   asection *newsect;
   2932   char *name;
   2933   char namebuf[64];
   2934   size_t len;
   2935   int split;
   2936   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   2937 
   2938   split = ((hdr->p_memsz > 0)
   2939 	    && (hdr->p_filesz > 0)
   2940 	    && (hdr->p_memsz > hdr->p_filesz));
   2941 
   2942   if (hdr->p_filesz > 0)
   2943     {
   2944       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   2945       len = strlen (namebuf) + 1;
   2946       name = (char *) bfd_alloc (abfd, len);
   2947       if (!name)
   2948 	return false;
   2949       memcpy (name, namebuf, len);
   2950       newsect = bfd_make_section (abfd, name);
   2951       if (newsect == NULL)
   2952 	return false;
   2953       newsect->vma = hdr->p_vaddr / opb;
   2954       newsect->lma = hdr->p_paddr / opb;
   2955       newsect->size = hdr->p_filesz;
   2956       newsect->filepos = hdr->p_offset;
   2957       newsect->flags |= SEC_HAS_CONTENTS;
   2958       newsect->alignment_power = bfd_log2 (hdr->p_align);
   2959       if (hdr->p_type == PT_LOAD)
   2960 	{
   2961 	  newsect->flags |= SEC_ALLOC;
   2962 	  newsect->flags |= SEC_LOAD;
   2963 	  if (hdr->p_flags & PF_X)
   2964 	    {
   2965 	      /* FIXME: all we known is that it has execute PERMISSION,
   2966 		 may be data.  */
   2967 	      newsect->flags |= SEC_CODE;
   2968 	    }
   2969 	}
   2970       if (!(hdr->p_flags & PF_W))
   2971 	{
   2972 	  newsect->flags |= SEC_READONLY;
   2973 	}
   2974     }
   2975 
   2976   if (hdr->p_memsz > hdr->p_filesz)
   2977     {
   2978       bfd_vma align;
   2979 
   2980       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   2981       len = strlen (namebuf) + 1;
   2982       name = (char *) bfd_alloc (abfd, len);
   2983       if (!name)
   2984 	return false;
   2985       memcpy (name, namebuf, len);
   2986       newsect = bfd_make_section (abfd, name);
   2987       if (newsect == NULL)
   2988 	return false;
   2989       newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
   2990       newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
   2991       newsect->size = hdr->p_memsz - hdr->p_filesz;
   2992       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   2993       align = newsect->vma & -newsect->vma;
   2994       if (align == 0 || align > hdr->p_align)
   2995 	align = hdr->p_align;
   2996       newsect->alignment_power = bfd_log2 (align);
   2997       if (hdr->p_type == PT_LOAD)
   2998 	{
   2999 	  newsect->flags |= SEC_ALLOC;
   3000 	  if (hdr->p_flags & PF_X)
   3001 	    newsect->flags |= SEC_CODE;
   3002 	}
   3003       if (!(hdr->p_flags & PF_W))
   3004 	newsect->flags |= SEC_READONLY;
   3005     }
   3006 
   3007   return true;
   3008 }
   3009 
   3010 static bool
   3011 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   3012 {
   3013   /* The return value is ignored.  Build-ids are considered optional.  */
   3014   if (templ->xvec->flavour == bfd_target_elf_flavour)
   3015     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   3016       (templ, offset);
   3017   return false;
   3018 }
   3019 
   3020 bool
   3021 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3022 {
   3023   const struct elf_backend_data *bed;
   3024 
   3025   switch (hdr->p_type)
   3026     {
   3027     case PT_NULL:
   3028       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3029 
   3030     case PT_LOAD:
   3031       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3032 	return false;
   3033       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3034 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3035       return true;
   3036 
   3037     case PT_DYNAMIC:
   3038       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3039 
   3040     case PT_INTERP:
   3041       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3042 
   3043     case PT_NOTE:
   3044       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3045 	return false;
   3046       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3047 			    hdr->p_align))
   3048 	return false;
   3049       return true;
   3050 
   3051     case PT_SHLIB:
   3052       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3053 
   3054     case PT_PHDR:
   3055       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3056 
   3057     case PT_GNU_EH_FRAME:
   3058       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3059 					      "eh_frame_hdr");
   3060 
   3061     case PT_GNU_STACK:
   3062       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3063 
   3064     case PT_GNU_RELRO:
   3065       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3066 
   3067     default:
   3068       /* Check for any processor-specific program segment types.  */
   3069       bed = get_elf_backend_data (abfd);
   3070       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3071     }
   3072 }
   3073 
   3074 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3075    REL or RELA.  */
   3076 
   3077 Elf_Internal_Shdr *
   3078 _bfd_elf_single_rel_hdr (asection *sec)
   3079 {
   3080   if (elf_section_data (sec)->rel.hdr)
   3081     {
   3082       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3083       return elf_section_data (sec)->rel.hdr;
   3084     }
   3085   else
   3086     return elf_section_data (sec)->rela.hdr;
   3087 }
   3088 
   3089 static bool
   3090 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3091 			    Elf_Internal_Shdr *rel_hdr,
   3092 			    const char *sec_name,
   3093 			    bool use_rela_p)
   3094 {
   3095   char *name = (char *) bfd_alloc (abfd,
   3096 				   sizeof ".rela" + strlen (sec_name));
   3097   if (name == NULL)
   3098     return false;
   3099 
   3100   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3101   rel_hdr->sh_name =
   3102     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3103 					false);
   3104   if (rel_hdr->sh_name == (unsigned int) -1)
   3105     return false;
   3106 
   3107   return true;
   3108 }
   3109 
   3110 /* Allocate and initialize a section-header for a new reloc section,
   3111    containing relocations against ASECT.  It is stored in RELDATA.  If
   3112    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3113    relocations.  */
   3114 
   3115 static bool
   3116 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3117 			  struct bfd_elf_section_reloc_data *reldata,
   3118 			  const char *sec_name,
   3119 			  bool use_rela_p,
   3120 			  bool delay_st_name_p)
   3121 {
   3122   Elf_Internal_Shdr *rel_hdr;
   3123   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3124 
   3125   BFD_ASSERT (reldata->hdr == NULL);
   3126   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3127   reldata->hdr = rel_hdr;
   3128 
   3129   if (delay_st_name_p)
   3130     rel_hdr->sh_name = (unsigned int) -1;
   3131   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3132 					use_rela_p))
   3133     return false;
   3134   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3135   rel_hdr->sh_entsize = (use_rela_p
   3136 			 ? bed->s->sizeof_rela
   3137 			 : bed->s->sizeof_rel);
   3138   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3139   rel_hdr->sh_flags = 0;
   3140   rel_hdr->sh_addr = 0;
   3141   rel_hdr->sh_size = 0;
   3142   rel_hdr->sh_offset = 0;
   3143 
   3144   return true;
   3145 }
   3146 
   3147 /* Return the default section type based on the passed in section flags.  */
   3148 
   3149 int
   3150 bfd_elf_get_default_section_type (flagword flags)
   3151 {
   3152   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3153       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3154     return SHT_NOBITS;
   3155   return SHT_PROGBITS;
   3156 }
   3157 
   3158 struct fake_section_arg
   3159 {
   3160   struct bfd_link_info *link_info;
   3161   bool failed;
   3162 };
   3163 
   3164 /* Set up an ELF internal section header for a section.  */
   3165 
   3166 static void
   3167 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3168 {
   3169   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3170   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3171   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3172   Elf_Internal_Shdr *this_hdr;
   3173   unsigned int sh_type;
   3174   const char *name = asect->name;
   3175   bool delay_st_name_p = false;
   3176   bfd_vma mask;
   3177 
   3178   if (arg->failed)
   3179     {
   3180       /* We already failed; just get out of the bfd_map_over_sections
   3181 	 loop.  */
   3182       return;
   3183     }
   3184 
   3185   this_hdr = &esd->this_hdr;
   3186 
   3187   if (arg->link_info)
   3188     {
   3189       /* ld: compress DWARF debug sections with names: .debug_*.  */
   3190       if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
   3191 	  && (asect->flags & SEC_DEBUGGING)
   3192 	  && name[1] == 'd'
   3193 	  && name[6] == '_')
   3194 	{
   3195 	  /* Set SEC_ELF_COMPRESS to indicate this section should be
   3196 	     compressed.  */
   3197 	  asect->flags |= SEC_ELF_COMPRESS;
   3198 	  /* If this section will be compressed, delay adding section
   3199 	     name to section name section after it is compressed in
   3200 	     _bfd_elf_assign_file_positions_for_non_load.  */
   3201 	  delay_st_name_p = true;
   3202 	}
   3203     }
   3204   else if ((asect->flags & SEC_ELF_RENAME))
   3205     {
   3206       /* objcopy: rename output DWARF debug section.  */
   3207       if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
   3208 	{
   3209 	  /* When we decompress or compress with SHF_COMPRESSED,
   3210 	     convert section name from .zdebug_* to .debug_* if
   3211 	     needed.  */
   3212 	  if (name[1] == 'z')
   3213 	    {
   3214 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   3215 	      if (new_name == NULL)
   3216 		{
   3217 		  arg->failed = true;
   3218 		  return;
   3219 		}
   3220 	      name = new_name;
   3221 	    }
   3222 	}
   3223       else if (asect->compress_status == COMPRESS_SECTION_DONE)
   3224 	{
   3225 	  /* PR binutils/18087: Compression does not always make a
   3226 	     section smaller.  So only rename the section when
   3227 	     compression has actually taken place.  If input section
   3228 	     name is .zdebug_*, we should never compress it again.  */
   3229 	  char *new_name = convert_debug_to_zdebug (abfd, name);
   3230 	  if (new_name == NULL)
   3231 	    {
   3232 	      arg->failed = true;
   3233 	      return;
   3234 	    }
   3235 	  BFD_ASSERT (name[1] != 'z');
   3236 	  name = new_name;
   3237 	}
   3238     }
   3239 
   3240   if (delay_st_name_p)
   3241     this_hdr->sh_name = (unsigned int) -1;
   3242   else
   3243     {
   3244       this_hdr->sh_name
   3245 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3246 					      name, false);
   3247       if (this_hdr->sh_name == (unsigned int) -1)
   3248 	{
   3249 	  arg->failed = true;
   3250 	  return;
   3251 	}
   3252     }
   3253 
   3254   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3255 
   3256   if ((asect->flags & SEC_ALLOC) != 0
   3257       || asect->user_set_vma)
   3258     this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
   3259   else
   3260     this_hdr->sh_addr = 0;
   3261 
   3262   this_hdr->sh_offset = 0;
   3263   this_hdr->sh_size = asect->size;
   3264   this_hdr->sh_link = 0;
   3265   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3266   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3267     {
   3268       _bfd_error_handler
   3269 	/* xgettext:c-format */
   3270 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3271 	 abfd, asect->alignment_power, asect);
   3272       arg->failed = true;
   3273       return;
   3274     }
   3275   /* Set sh_addralign to the highest power of two given by alignment
   3276      consistent with the section VMA.  Linker scripts can force VMA.  */
   3277   mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
   3278   this_hdr->sh_addralign = mask & -mask;
   3279   /* The sh_entsize and sh_info fields may have been set already by
   3280      copy_private_section_data.  */
   3281 
   3282   this_hdr->bfd_section = asect;
   3283   this_hdr->contents = NULL;
   3284 
   3285   /* If the section type is unspecified, we set it based on
   3286      asect->flags.  */
   3287   if (asect->type != 0)
   3288     sh_type = asect->type;
   3289   else if ((asect->flags & SEC_GROUP) != 0)
   3290     sh_type = SHT_GROUP;
   3291   else
   3292     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3293 
   3294   if (this_hdr->sh_type == SHT_NULL)
   3295     this_hdr->sh_type = sh_type;
   3296   else if (this_hdr->sh_type == SHT_NOBITS
   3297 	   && sh_type == SHT_PROGBITS
   3298 	   && (asect->flags & SEC_ALLOC) != 0)
   3299     {
   3300       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3301 	 allow the link to proceed.  This can happen when users link
   3302 	 non-bss input sections to bss output sections, or emit data
   3303 	 to a bss output section via a linker script.  */
   3304       _bfd_error_handler
   3305 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3306       this_hdr->sh_type = sh_type;
   3307     }
   3308 
   3309   switch (this_hdr->sh_type)
   3310     {
   3311     default:
   3312       break;
   3313 
   3314     case SHT_STRTAB:
   3315     case SHT_NOTE:
   3316     case SHT_NOBITS:
   3317     case SHT_PROGBITS:
   3318       break;
   3319 
   3320     case SHT_INIT_ARRAY:
   3321     case SHT_FINI_ARRAY:
   3322     case SHT_PREINIT_ARRAY:
   3323       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3324       break;
   3325 
   3326     case SHT_HASH:
   3327       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3328       break;
   3329 
   3330     case SHT_DYNSYM:
   3331       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3332       break;
   3333 
   3334     case SHT_DYNAMIC:
   3335       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3336       break;
   3337 
   3338     case SHT_RELA:
   3339       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3340 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3341       break;
   3342 
   3343      case SHT_REL:
   3344       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3345 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3346       break;
   3347 
   3348      case SHT_GNU_versym:
   3349       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3350       break;
   3351 
   3352      case SHT_GNU_verdef:
   3353       this_hdr->sh_entsize = 0;
   3354       /* objcopy or strip will copy over sh_info, but may not set
   3355 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3356 	 zero.  */
   3357       if (this_hdr->sh_info == 0)
   3358 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3359       else
   3360 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3361 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3362       break;
   3363 
   3364     case SHT_GNU_verneed:
   3365       this_hdr->sh_entsize = 0;
   3366       /* objcopy or strip will copy over sh_info, but may not set
   3367 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3368 	 zero.  */
   3369       if (this_hdr->sh_info == 0)
   3370 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3371       else
   3372 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3373 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3374       break;
   3375 
   3376     case SHT_GROUP:
   3377       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3378       break;
   3379 
   3380     case SHT_GNU_HASH:
   3381       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3382       break;
   3383     }
   3384 
   3385   if ((asect->flags & SEC_ALLOC) != 0)
   3386     this_hdr->sh_flags |= SHF_ALLOC;
   3387   if ((asect->flags & SEC_READONLY) == 0)
   3388     this_hdr->sh_flags |= SHF_WRITE;
   3389   if ((asect->flags & SEC_CODE) != 0)
   3390     this_hdr->sh_flags |= SHF_EXECINSTR;
   3391   if ((asect->flags & SEC_MERGE) != 0)
   3392     {
   3393       this_hdr->sh_flags |= SHF_MERGE;
   3394       this_hdr->sh_entsize = asect->entsize;
   3395     }
   3396   if ((asect->flags & SEC_STRINGS) != 0)
   3397     this_hdr->sh_flags |= SHF_STRINGS;
   3398   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3399     this_hdr->sh_flags |= SHF_GROUP;
   3400   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3401     {
   3402       this_hdr->sh_flags |= SHF_TLS;
   3403       if (asect->size == 0
   3404 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3405 	{
   3406 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3407 
   3408 	  this_hdr->sh_size = 0;
   3409 	  if (o != NULL)
   3410 	    {
   3411 	      this_hdr->sh_size = o->offset + o->size;
   3412 	      if (this_hdr->sh_size != 0)
   3413 		this_hdr->sh_type = SHT_NOBITS;
   3414 	    }
   3415 	}
   3416     }
   3417   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3418     this_hdr->sh_flags |= SHF_EXCLUDE;
   3419 
   3420   /* If the section has relocs, set up a section header for the
   3421      SHT_REL[A] section.  If two relocation sections are required for
   3422      this section, it is up to the processor-specific back-end to
   3423      create the other.  */
   3424   if ((asect->flags & SEC_RELOC) != 0)
   3425     {
   3426       /* When doing a relocatable link, create both REL and RELA sections if
   3427 	 needed.  */
   3428       if (arg->link_info
   3429 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3430 	  && esd->rel.count + esd->rela.count > 0
   3431 	  && (bfd_link_relocatable (arg->link_info)
   3432 	      || arg->link_info->emitrelocations))
   3433 	{
   3434 	  if (esd->rel.count && esd->rel.hdr == NULL
   3435 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3436 					    false, delay_st_name_p))
   3437 	    {
   3438 	      arg->failed = true;
   3439 	      return;
   3440 	    }
   3441 	  if (esd->rela.count && esd->rela.hdr == NULL
   3442 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3443 					    true, delay_st_name_p))
   3444 	    {
   3445 	      arg->failed = true;
   3446 	      return;
   3447 	    }
   3448 	}
   3449       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3450 					  (asect->use_rela_p
   3451 					   ? &esd->rela : &esd->rel),
   3452 					  name,
   3453 					  asect->use_rela_p,
   3454 					  delay_st_name_p))
   3455 	{
   3456 	  arg->failed = true;
   3457 	  return;
   3458 	}
   3459     }
   3460 
   3461   /* Check for processor-specific section types.  */
   3462   sh_type = this_hdr->sh_type;
   3463   if (bed->elf_backend_fake_sections
   3464       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3465     {
   3466       arg->failed = true;
   3467       return;
   3468     }
   3469 
   3470   if (sh_type == SHT_NOBITS && asect->size != 0)
   3471     {
   3472       /* Don't change the header type from NOBITS if we are being
   3473 	 called for objcopy --only-keep-debug.  */
   3474       this_hdr->sh_type = sh_type;
   3475     }
   3476 }
   3477 
   3478 /* Fill in the contents of a SHT_GROUP section.  Called from
   3479    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3480    when ELF targets use the generic linker, ld.  Called for ld -r
   3481    from bfd_elf_final_link.  */
   3482 
   3483 void
   3484 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3485 {
   3486   bool *failedptr = (bool *) failedptrarg;
   3487   asection *elt, *first;
   3488   unsigned char *loc;
   3489   bool gas;
   3490 
   3491   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3492      elfxx-ia64.c.  */
   3493   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3494       || sec->size == 0
   3495       || *failedptr)
   3496     return;
   3497 
   3498   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3499     {
   3500       unsigned long symindx = 0;
   3501 
   3502       /* elf_group_id will have been set up by objcopy and the
   3503 	 generic linker.  */
   3504       if (elf_group_id (sec) != NULL)
   3505 	symindx = elf_group_id (sec)->udata.i;
   3506 
   3507       if (symindx == 0)
   3508 	{
   3509 	  /* If called from the assembler, swap_out_syms will have set up
   3510 	     elf_section_syms.
   3511 	     PR 25699: A corrupt input file could contain bogus group info.  */
   3512 	  if (sec->index >= elf_num_section_syms (abfd)
   3513 	      || elf_section_syms (abfd)[sec->index] == NULL)
   3514 	    {
   3515 	      *failedptr = true;
   3516 	      return;
   3517 	    }
   3518 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3519 	}
   3520       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3521     }
   3522   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3523     {
   3524       /* The ELF backend linker sets sh_info to -2 when the group
   3525 	 signature symbol is global, and thus the index can't be
   3526 	 set until all local symbols are output.  */
   3527       asection *igroup;
   3528       struct bfd_elf_section_data *sec_data;
   3529       unsigned long symndx;
   3530       unsigned long extsymoff;
   3531       struct elf_link_hash_entry *h;
   3532 
   3533       /* The point of this little dance to the first SHF_GROUP section
   3534 	 then back to the SHT_GROUP section is that this gets us to
   3535 	 the SHT_GROUP in the input object.  */
   3536       igroup = elf_sec_group (elf_next_in_group (sec));
   3537       sec_data = elf_section_data (igroup);
   3538       symndx = sec_data->this_hdr.sh_info;
   3539       extsymoff = 0;
   3540       if (!elf_bad_symtab (igroup->owner))
   3541 	{
   3542 	  Elf_Internal_Shdr *symtab_hdr;
   3543 
   3544 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3545 	  extsymoff = symtab_hdr->sh_info;
   3546 	}
   3547       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3548       while (h->root.type == bfd_link_hash_indirect
   3549 	     || h->root.type == bfd_link_hash_warning)
   3550 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3551 
   3552       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3553     }
   3554 
   3555   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3556   gas = true;
   3557   if (sec->contents == NULL)
   3558     {
   3559       gas = false;
   3560       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3561 
   3562       /* Arrange for the section to be written out.  */
   3563       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3564       if (sec->contents == NULL)
   3565 	{
   3566 	  *failedptr = true;
   3567 	  return;
   3568 	}
   3569     }
   3570 
   3571   loc = sec->contents + sec->size;
   3572 
   3573   /* Get the pointer to the first section in the group that gas
   3574      squirreled away here.  objcopy arranges for this to be set to the
   3575      start of the input section group.  */
   3576   first = elt = elf_next_in_group (sec);
   3577 
   3578   /* First element is a flag word.  Rest of section is elf section
   3579      indices for all the sections of the group.  Write them backwards
   3580      just to keep the group in the same order as given in .section
   3581      directives, not that it matters.  */
   3582   while (elt != NULL)
   3583     {
   3584       asection *s;
   3585 
   3586       s = elt;
   3587       if (!gas)
   3588 	s = s->output_section;
   3589       if (s != NULL
   3590 	  && !bfd_is_abs_section (s))
   3591 	{
   3592 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   3593 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   3594 
   3595 	  if (elf_sec->rel.hdr != NULL
   3596 	      && (gas
   3597 		  || (input_elf_sec->rel.hdr != NULL
   3598 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   3599 	    {
   3600 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   3601 	      loc -= 4;
   3602 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   3603 	    }
   3604 	  if (elf_sec->rela.hdr != NULL
   3605 	      && (gas
   3606 		  || (input_elf_sec->rela.hdr != NULL
   3607 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   3608 	    {
   3609 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   3610 	      loc -= 4;
   3611 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   3612 	    }
   3613 	  loc -= 4;
   3614 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   3615 	}
   3616       elt = elf_next_in_group (elt);
   3617       if (elt == first)
   3618 	break;
   3619     }
   3620 
   3621   loc -= 4;
   3622   BFD_ASSERT (loc == sec->contents);
   3623 
   3624   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   3625 }
   3626 
   3627 /* Given NAME, the name of a relocation section stripped of its
   3628    .rel/.rela prefix, return the section in ABFD to which the
   3629    relocations apply.  */
   3630 
   3631 asection *
   3632 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   3633 {
   3634   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   3635      section likely apply to .got.plt or .got section.  */
   3636   if (get_elf_backend_data (abfd)->want_got_plt
   3637       && strcmp (name, ".plt") == 0)
   3638     {
   3639       asection *sec;
   3640 
   3641       name = ".got.plt";
   3642       sec = bfd_get_section_by_name (abfd, name);
   3643       if (sec != NULL)
   3644 	return sec;
   3645       name = ".got";
   3646     }
   3647 
   3648   return bfd_get_section_by_name (abfd, name);
   3649 }
   3650 
   3651 /* Return the section to which RELOC_SEC applies.  */
   3652 
   3653 static asection *
   3654 elf_get_reloc_section (asection *reloc_sec)
   3655 {
   3656   const char *name;
   3657   unsigned int type;
   3658   bfd *abfd;
   3659   const struct elf_backend_data *bed;
   3660 
   3661   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   3662   if (type != SHT_REL && type != SHT_RELA)
   3663     return NULL;
   3664 
   3665   /* We look up the section the relocs apply to by name.  */
   3666   name = reloc_sec->name;
   3667   if (!startswith (name, ".rel"))
   3668     return NULL;
   3669   name += 4;
   3670   if (type == SHT_RELA && *name++ != 'a')
   3671     return NULL;
   3672 
   3673   abfd = reloc_sec->owner;
   3674   bed = get_elf_backend_data (abfd);
   3675   return bed->get_reloc_section (abfd, name);
   3676 }
   3677 
   3678 /* Assign all ELF section numbers.  The dummy first section is handled here
   3679    too.  The link/info pointers for the standard section types are filled
   3680    in here too, while we're at it.  LINK_INFO will be 0 when arriving
   3681    here for objcopy, and when using the generic ELF linker.  */
   3682 
   3683 static bool
   3684 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   3685 {
   3686   struct elf_obj_tdata *t = elf_tdata (abfd);
   3687   asection *sec;
   3688   unsigned int section_number;
   3689   Elf_Internal_Shdr **i_shdrp;
   3690   struct bfd_elf_section_data *d;
   3691   bool need_symtab;
   3692   size_t amt;
   3693 
   3694   section_number = 1;
   3695 
   3696   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   3697 
   3698   /* SHT_GROUP sections are in relocatable files only.  */
   3699   if (link_info == NULL || !link_info->resolve_section_groups)
   3700     {
   3701       size_t reloc_count = 0;
   3702 
   3703       /* Put SHT_GROUP sections first.  */
   3704       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   3705 	{
   3706 	  d = elf_section_data (sec);
   3707 
   3708 	  if (d->this_hdr.sh_type == SHT_GROUP)
   3709 	    {
   3710 	      if (sec->flags & SEC_LINKER_CREATED)
   3711 		{
   3712 		  /* Remove the linker created SHT_GROUP sections.  */
   3713 		  bfd_section_list_remove (abfd, sec);
   3714 		  abfd->section_count--;
   3715 		}
   3716 	      else
   3717 		d->this_idx = section_number++;
   3718 	    }
   3719 
   3720 	  /* Count relocations.  */
   3721 	  reloc_count += sec->reloc_count;
   3722 	}
   3723 
   3724       /* Clear HAS_RELOC if there are no relocations.  */
   3725       if (reloc_count == 0)
   3726 	abfd->flags &= ~HAS_RELOC;
   3727     }
   3728 
   3729   for (sec = abfd->sections; sec; sec = sec->next)
   3730     {
   3731       d = elf_section_data (sec);
   3732 
   3733       if (d->this_hdr.sh_type != SHT_GROUP)
   3734 	d->this_idx = section_number++;
   3735       if (d->this_hdr.sh_name != (unsigned int) -1)
   3736 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   3737       if (d->rel.hdr)
   3738 	{
   3739 	  d->rel.idx = section_number++;
   3740 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   3741 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   3742 	}
   3743       else
   3744 	d->rel.idx = 0;
   3745 
   3746       if (d->rela.hdr)
   3747 	{
   3748 	  d->rela.idx = section_number++;
   3749 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   3750 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   3751 	}
   3752       else
   3753 	d->rela.idx = 0;
   3754     }
   3755 
   3756   need_symtab = (bfd_get_symcount (abfd) > 0
   3757 		 || (link_info == NULL
   3758 		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   3759 			 == HAS_RELOC)));
   3760   if (need_symtab)
   3761     {
   3762       elf_onesymtab (abfd) = section_number++;
   3763       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   3764       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   3765 	{
   3766 	  elf_section_list *entry;
   3767 
   3768 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   3769 
   3770 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   3771 	  entry->ndx = section_number++;
   3772 	  elf_symtab_shndx_list (abfd) = entry;
   3773 	  entry->hdr.sh_name
   3774 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3775 						  ".symtab_shndx", false);
   3776 	  if (entry->hdr.sh_name == (unsigned int) -1)
   3777 	    return false;
   3778 	}
   3779       elf_strtab_sec (abfd) = section_number++;
   3780       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   3781     }
   3782 
   3783   elf_shstrtab_sec (abfd) = section_number++;
   3784   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   3785   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   3786 
   3787   if (section_number >= SHN_LORESERVE)
   3788     {
   3789       /* xgettext:c-format */
   3790       _bfd_error_handler (_("%pB: too many sections: %u"),
   3791 			  abfd, section_number);
   3792       return false;
   3793     }
   3794 
   3795   elf_numsections (abfd) = section_number;
   3796   elf_elfheader (abfd)->e_shnum = section_number;
   3797 
   3798   /* Set up the list of section header pointers, in agreement with the
   3799      indices.  */
   3800   amt = section_number * sizeof (Elf_Internal_Shdr *);
   3801   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
   3802   if (i_shdrp == NULL)
   3803     return false;
   3804 
   3805   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   3806 						 sizeof (Elf_Internal_Shdr));
   3807   if (i_shdrp[0] == NULL)
   3808     {
   3809       bfd_release (abfd, i_shdrp);
   3810       return false;
   3811     }
   3812 
   3813   elf_elfsections (abfd) = i_shdrp;
   3814 
   3815   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   3816   if (need_symtab)
   3817     {
   3818       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   3819       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   3820 	{
   3821 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   3822 	  BFD_ASSERT (entry != NULL);
   3823 	  i_shdrp[entry->ndx] = & entry->hdr;
   3824 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   3825 	}
   3826       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   3827       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   3828     }
   3829 
   3830   for (sec = abfd->sections; sec; sec = sec->next)
   3831     {
   3832       asection *s;
   3833 
   3834       d = elf_section_data (sec);
   3835 
   3836       i_shdrp[d->this_idx] = &d->this_hdr;
   3837       if (d->rel.idx != 0)
   3838 	i_shdrp[d->rel.idx] = d->rel.hdr;
   3839       if (d->rela.idx != 0)
   3840 	i_shdrp[d->rela.idx] = d->rela.hdr;
   3841 
   3842       /* Fill in the sh_link and sh_info fields while we're at it.  */
   3843 
   3844       /* sh_link of a reloc section is the section index of the symbol
   3845 	 table.  sh_info is the section index of the section to which
   3846 	 the relocation entries apply.  */
   3847       if (d->rel.idx != 0)
   3848 	{
   3849 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   3850 	  d->rel.hdr->sh_info = d->this_idx;
   3851 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   3852 	}
   3853       if (d->rela.idx != 0)
   3854 	{
   3855 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   3856 	  d->rela.hdr->sh_info = d->this_idx;
   3857 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   3858 	}
   3859 
   3860       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   3861       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   3862 	{
   3863 	  s = elf_linked_to_section (sec);
   3864 	  /* We can now have a NULL linked section pointer.
   3865 	     This happens when the sh_link field is 0, which is done
   3866 	     when a linked to section is discarded but the linking
   3867 	     section has been retained for some reason.  */
   3868 	  if (s)
   3869 	    {
   3870 	      /* Check discarded linkonce section.  */
   3871 	      if (discarded_section (s))
   3872 		{
   3873 		  asection *kept;
   3874 		  _bfd_error_handler
   3875 		    /* xgettext:c-format */
   3876 		    (_("%pB: sh_link of section `%pA' points to"
   3877 		       " discarded section `%pA' of `%pB'"),
   3878 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   3879 		  /* Point to the kept section if it has the same
   3880 		     size as the discarded one.  */
   3881 		  kept = _bfd_elf_check_kept_section (s, link_info);
   3882 		  if (kept == NULL)
   3883 		    {
   3884 		      bfd_set_error (bfd_error_bad_value);
   3885 		      return false;
   3886 		    }
   3887 		  s = kept;
   3888 		}
   3889 	      /* Handle objcopy. */
   3890 	      else if (s->output_section == NULL)
   3891 		{
   3892 		  _bfd_error_handler
   3893 		    /* xgettext:c-format */
   3894 		    (_("%pB: sh_link of section `%pA' points to"
   3895 		       " removed section `%pA' of `%pB'"),
   3896 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   3897 		  bfd_set_error (bfd_error_bad_value);
   3898 		  return false;
   3899 		}
   3900 	      s = s->output_section;
   3901 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3902 	    }
   3903 	}
   3904 
   3905       switch (d->this_hdr.sh_type)
   3906 	{
   3907 	case SHT_REL:
   3908 	case SHT_RELA:
   3909 	  /* A reloc section which we are treating as a normal BFD
   3910 	     section.  sh_link is the section index of the symbol
   3911 	     table.  sh_info is the section index of the section to
   3912 	     which the relocation entries apply.  We assume that an
   3913 	     allocated reloc section uses the dynamic symbol table
   3914 	     if there is one.  Otherwise we guess the normal symbol
   3915 	     table.  FIXME: How can we be sure?  */
   3916 	  if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
   3917 	    {
   3918 	      s = bfd_get_section_by_name (abfd, ".dynsym");
   3919 	      if (s != NULL)
   3920 		d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3921 	    }
   3922 	  if (d->this_hdr.sh_link == 0)
   3923 	    d->this_hdr.sh_link = elf_onesymtab (abfd);
   3924 
   3925 	  s = elf_get_reloc_section (sec);
   3926 	  if (s != NULL)
   3927 	    {
   3928 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   3929 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   3930 	    }
   3931 	  break;
   3932 
   3933 	case SHT_STRTAB:
   3934 	  /* We assume that a section named .stab*str is a stabs
   3935 	     string section.  We look for a section with the same name
   3936 	     but without the trailing ``str'', and set its sh_link
   3937 	     field to point to this section.  */
   3938 	  if (startswith (sec->name, ".stab")
   3939 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   3940 	    {
   3941 	      size_t len;
   3942 	      char *alc;
   3943 
   3944 	      len = strlen (sec->name);
   3945 	      alc = (char *) bfd_malloc (len - 2);
   3946 	      if (alc == NULL)
   3947 		return false;
   3948 	      memcpy (alc, sec->name, len - 3);
   3949 	      alc[len - 3] = '\0';
   3950 	      s = bfd_get_section_by_name (abfd, alc);
   3951 	      free (alc);
   3952 	      if (s != NULL)
   3953 		{
   3954 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   3955 
   3956 		  /* This is a .stab section.  */
   3957 		  elf_section_data (s)->this_hdr.sh_entsize = 12;
   3958 		}
   3959 	    }
   3960 	  break;
   3961 
   3962 	case SHT_DYNAMIC:
   3963 	case SHT_DYNSYM:
   3964 	case SHT_GNU_verneed:
   3965 	case SHT_GNU_verdef:
   3966 	  /* sh_link is the section header index of the string table
   3967 	     used for the dynamic entries, or the symbol table, or the
   3968 	     version strings.  */
   3969 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   3970 	  if (s != NULL)
   3971 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3972 	  break;
   3973 
   3974 	case SHT_GNU_LIBLIST:
   3975 	  /* sh_link is the section header index of the prelink library
   3976 	     list used for the dynamic entries, or the symbol table, or
   3977 	     the version strings.  */
   3978 	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
   3979 					      ? ".dynstr" : ".gnu.libstr"));
   3980 	  if (s != NULL)
   3981 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3982 	  break;
   3983 
   3984 	case SHT_HASH:
   3985 	case SHT_GNU_HASH:
   3986 	case SHT_GNU_versym:
   3987 	  /* sh_link is the section header index of the symbol table
   3988 	     this hash table or version table is for.  */
   3989 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3990 	  if (s != NULL)
   3991 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3992 	  break;
   3993 
   3994 	case SHT_GROUP:
   3995 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   3996 	}
   3997     }
   3998 
   3999   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   4000      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   4001      debug section name from .debug_* to .zdebug_* if needed.  */
   4002 
   4003   return true;
   4004 }
   4005 
   4006 static bool
   4007 sym_is_global (bfd *abfd, asymbol *sym)
   4008 {
   4009   /* If the backend has a special mapping, use it.  */
   4010   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4011   if (bed->elf_backend_sym_is_global)
   4012     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   4013 
   4014   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   4015 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   4016 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   4017 }
   4018 
   4019 /* Filter global symbols of ABFD to include in the import library.  All
   4020    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4021    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4022    the beginning of that array.
   4023 
   4024    Returns the number of symbols to keep.  */
   4025 
   4026 unsigned int
   4027 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4028 				asymbol **syms, long symcount)
   4029 {
   4030   long src_count, dst_count = 0;
   4031 
   4032   for (src_count = 0; src_count < symcount; src_count++)
   4033     {
   4034       asymbol *sym = syms[src_count];
   4035       char *name = (char *) bfd_asymbol_name (sym);
   4036       struct bfd_link_hash_entry *h;
   4037 
   4038       if (!sym_is_global (abfd, sym))
   4039 	continue;
   4040 
   4041       h = bfd_link_hash_lookup (info->hash, name, false, false, false);
   4042       if (h == NULL)
   4043 	continue;
   4044       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4045 	continue;
   4046       if (h->linker_def || h->ldscript_def)
   4047 	continue;
   4048 
   4049       syms[dst_count++] = sym;
   4050     }
   4051 
   4052   syms[dst_count] = NULL;
   4053 
   4054   return dst_count;
   4055 }
   4056 
   4057 /* Don't output section symbols for sections that are not going to be
   4058    output, that are duplicates or there is no BFD section.  */
   4059 
   4060 static bool
   4061 ignore_section_sym (bfd *abfd, asymbol *sym)
   4062 {
   4063   elf_symbol_type *type_ptr;
   4064 
   4065   if (sym == NULL)
   4066     return false;
   4067 
   4068   if ((sym->flags & BSF_SECTION_SYM) == 0)
   4069     return false;
   4070 
   4071   /* Ignore the section symbol if it isn't used.  */
   4072   if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
   4073     return true;
   4074 
   4075   if (sym->section == NULL)
   4076     return true;
   4077 
   4078   type_ptr = elf_symbol_from (sym);
   4079   return ((type_ptr != NULL
   4080 	   && type_ptr->internal_elf_sym.st_shndx != 0
   4081 	   && bfd_is_abs_section (sym->section))
   4082 	  || !(sym->section->owner == abfd
   4083 	       || (sym->section->output_section != NULL
   4084 		   && sym->section->output_section->owner == abfd
   4085 		   && sym->section->output_offset == 0)
   4086 	       || bfd_is_abs_section (sym->section)));
   4087 }
   4088 
   4089 /* Map symbol from it's internal number to the external number, moving
   4090    all local symbols to be at the head of the list.  */
   4091 
   4092 static bool
   4093 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4094 {
   4095   unsigned int symcount = bfd_get_symcount (abfd);
   4096   asymbol **syms = bfd_get_outsymbols (abfd);
   4097   asymbol **sect_syms;
   4098   unsigned int num_locals = 0;
   4099   unsigned int num_globals = 0;
   4100   unsigned int num_locals2 = 0;
   4101   unsigned int num_globals2 = 0;
   4102   unsigned int max_index = 0;
   4103   unsigned int idx;
   4104   asection *asect;
   4105   asymbol **new_syms;
   4106   size_t amt;
   4107 
   4108 #ifdef DEBUG
   4109   fprintf (stderr, "elf_map_symbols\n");
   4110   fflush (stderr);
   4111 #endif
   4112 
   4113   for (asect = abfd->sections; asect; asect = asect->next)
   4114     {
   4115       if (max_index < asect->index)
   4116 	max_index = asect->index;
   4117     }
   4118 
   4119   max_index++;
   4120   amt = max_index * sizeof (asymbol *);
   4121   sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
   4122   if (sect_syms == NULL)
   4123     return false;
   4124   elf_section_syms (abfd) = sect_syms;
   4125   elf_num_section_syms (abfd) = max_index;
   4126 
   4127   /* Init sect_syms entries for any section symbols we have already
   4128      decided to output.  */
   4129   for (idx = 0; idx < symcount; idx++)
   4130     {
   4131       asymbol *sym = syms[idx];
   4132 
   4133       if ((sym->flags & BSF_SECTION_SYM) != 0
   4134 	  && sym->value == 0
   4135 	  && !ignore_section_sym (abfd, sym)
   4136 	  && !bfd_is_abs_section (sym->section))
   4137 	{
   4138 	  asection *sec = sym->section;
   4139 
   4140 	  if (sec->owner != abfd)
   4141 	    sec = sec->output_section;
   4142 
   4143 	  sect_syms[sec->index] = syms[idx];
   4144 	}
   4145     }
   4146 
   4147   /* Classify all of the symbols.  */
   4148   for (idx = 0; idx < symcount; idx++)
   4149     {
   4150       if (sym_is_global (abfd, syms[idx]))
   4151 	num_globals++;
   4152       else if (!ignore_section_sym (abfd, syms[idx]))
   4153 	num_locals++;
   4154     }
   4155 
   4156   /* We will be adding a section symbol for each normal BFD section.  Most
   4157      sections will already have a section symbol in outsymbols, but
   4158      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4159      at least in that case.  */
   4160   for (asect = abfd->sections; asect; asect = asect->next)
   4161     {
   4162       asymbol *sym = asect->symbol;
   4163       /* Don't include ignored section symbols.  */
   4164       if (!ignore_section_sym (abfd, sym)
   4165 	  && sect_syms[asect->index] == NULL)
   4166 	{
   4167 	  if (!sym_is_global (abfd, asect->symbol))
   4168 	    num_locals++;
   4169 	  else
   4170 	    num_globals++;
   4171 	}
   4172     }
   4173 
   4174   /* Now sort the symbols so the local symbols are first.  */
   4175   amt = (num_locals + num_globals) * sizeof (asymbol *);
   4176   new_syms = (asymbol **) bfd_alloc (abfd, amt);
   4177   if (new_syms == NULL)
   4178     return false;
   4179 
   4180   for (idx = 0; idx < symcount; idx++)
   4181     {
   4182       asymbol *sym = syms[idx];
   4183       unsigned int i;
   4184 
   4185       if (sym_is_global (abfd, sym))
   4186 	i = num_locals + num_globals2++;
   4187       /* Don't include ignored section symbols.  */
   4188       else if (!ignore_section_sym (abfd, sym))
   4189 	i = num_locals2++;
   4190       else
   4191 	continue;
   4192       new_syms[i] = sym;
   4193       sym->udata.i = i + 1;
   4194     }
   4195   for (asect = abfd->sections; asect; asect = asect->next)
   4196     {
   4197       asymbol *sym = asect->symbol;
   4198       if (!ignore_section_sym (abfd, sym)
   4199 	  && sect_syms[asect->index] == NULL)
   4200 	{
   4201 	  unsigned int i;
   4202 
   4203 	  sect_syms[asect->index] = sym;
   4204 	  if (!sym_is_global (abfd, sym))
   4205 	    i = num_locals2++;
   4206 	  else
   4207 	    i = num_locals + num_globals2++;
   4208 	  new_syms[i] = sym;
   4209 	  sym->udata.i = i + 1;
   4210 	}
   4211     }
   4212 
   4213   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4214 
   4215   *pnum_locals = num_locals;
   4216   return true;
   4217 }
   4218 
   4219 /* Align to the maximum file alignment that could be required for any
   4220    ELF data structure.  */
   4221 
   4222 static inline file_ptr
   4223 align_file_position (file_ptr off, int align)
   4224 {
   4225   return (off + align - 1) & ~(align - 1);
   4226 }
   4227 
   4228 /* Assign a file position to a section, optionally aligning to the
   4229    required section alignment.  */
   4230 
   4231 file_ptr
   4232 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4233 					   file_ptr offset,
   4234 					   bool align)
   4235 {
   4236   if (align && i_shdrp->sh_addralign > 1)
   4237     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
   4238   i_shdrp->sh_offset = offset;
   4239   if (i_shdrp->bfd_section != NULL)
   4240     i_shdrp->bfd_section->filepos = offset;
   4241   if (i_shdrp->sh_type != SHT_NOBITS)
   4242     offset += i_shdrp->sh_size;
   4243   return offset;
   4244 }
   4245 
   4246 /* Compute the file positions we are going to put the sections at, and
   4247    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4248    is not NULL, this is being called by the ELF backend linker.  */
   4249 
   4250 bool
   4251 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4252 					 struct bfd_link_info *link_info)
   4253 {
   4254   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4255   struct fake_section_arg fsargs;
   4256   bool failed;
   4257   struct elf_strtab_hash *strtab = NULL;
   4258   Elf_Internal_Shdr *shstrtab_hdr;
   4259   bool need_symtab;
   4260 
   4261   if (abfd->output_has_begun)
   4262     return true;
   4263 
   4264   /* Do any elf backend specific processing first.  */
   4265   if (bed->elf_backend_begin_write_processing)
   4266     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4267 
   4268   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4269     return false;
   4270 
   4271   fsargs.failed = false;
   4272   fsargs.link_info = link_info;
   4273   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4274   if (fsargs.failed)
   4275     return false;
   4276 
   4277   if (!assign_section_numbers (abfd, link_info))
   4278     return false;
   4279 
   4280   /* The backend linker builds symbol table information itself.  */
   4281   need_symtab = (link_info == NULL
   4282 		 && (bfd_get_symcount (abfd) > 0
   4283 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4284 			 == HAS_RELOC)));
   4285   if (need_symtab)
   4286     {
   4287       /* Non-zero if doing a relocatable link.  */
   4288       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4289 
   4290       if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
   4291 	return false;
   4292     }
   4293 
   4294   failed = false;
   4295   if (link_info == NULL)
   4296     {
   4297       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4298       if (failed)
   4299 	return false;
   4300     }
   4301 
   4302   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4303   /* sh_name was set in init_file_header.  */
   4304   shstrtab_hdr->sh_type = SHT_STRTAB;
   4305   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4306   shstrtab_hdr->sh_addr = 0;
   4307   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4308   shstrtab_hdr->sh_entsize = 0;
   4309   shstrtab_hdr->sh_link = 0;
   4310   shstrtab_hdr->sh_info = 0;
   4311   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4312   shstrtab_hdr->sh_addralign = 1;
   4313 
   4314   if (!assign_file_positions_except_relocs (abfd, link_info))
   4315     return false;
   4316 
   4317   if (need_symtab)
   4318     {
   4319       file_ptr off;
   4320       Elf_Internal_Shdr *hdr;
   4321 
   4322       off = elf_next_file_pos (abfd);
   4323 
   4324       hdr = & elf_symtab_hdr (abfd);
   4325       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4326 
   4327       if (elf_symtab_shndx_list (abfd) != NULL)
   4328 	{
   4329 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4330 	  if (hdr->sh_size != 0)
   4331 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4332 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4333 	}
   4334 
   4335       hdr = &elf_tdata (abfd)->strtab_hdr;
   4336       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4337 
   4338       elf_next_file_pos (abfd) = off;
   4339 
   4340       /* Now that we know where the .strtab section goes, write it
   4341 	 out.  */
   4342       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4343 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4344 	return false;
   4345       _bfd_elf_strtab_free (strtab);
   4346     }
   4347 
   4348   abfd->output_has_begun = true;
   4349 
   4350   return true;
   4351 }
   4352 
   4353 /* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
   4354    function effectively returns whether --eh-frame-hdr is given on the
   4355    command line.  After size_dynamic_sections the result reflects
   4356    whether .eh_frame_hdr will actually be output (sizing isn't done
   4357    until ldemul_after_allocation).  */
   4358 
   4359 static asection *
   4360 elf_eh_frame_hdr (const struct bfd_link_info *info)
   4361 {
   4362   if (info != NULL && is_elf_hash_table (info->hash))
   4363     return elf_hash_table (info)->eh_info.hdr_sec;
   4364   return NULL;
   4365 }
   4366 
   4367 /* Make an initial estimate of the size of the program header.  If we
   4368    get the number wrong here, we'll redo section placement.  */
   4369 
   4370 static bfd_size_type
   4371 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4372 {
   4373   size_t segs;
   4374   asection *s, *s2;
   4375   const struct elf_backend_data *bed;
   4376 
   4377   /* Assume we will need exactly two PT_LOAD segments: one for text
   4378      and one for data.  */
   4379   segs = 2;
   4380 
   4381   s = bfd_get_section_by_name (abfd, ".interp");
   4382   s2 = bfd_get_section_by_name (abfd, ".dynamic");
   4383   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4384     {
   4385       ++segs;
   4386     }
   4387 
   4388   if (s2 != NULL && (s2->flags & SEC_LOAD) != 0)
   4389     {
   4390       /* We need a PT_DYNAMIC segment.  */
   4391       ++segs;
   4392     }
   4393 
   4394   if ((s != NULL && (s->flags & SEC_LOAD) != 0) ||
   4395       (s2 != NULL && (s2->flags & SEC_LOAD) != 0))
   4396     {
   4397       /*
   4398        * If either a PT_INTERP or PT_DYNAMIC segment is created,
   4399        * also create a PT_PHDR segment.
   4400        */
   4401       ++segs;
   4402     }
   4403 
   4404   if (info != NULL && info->relro)
   4405     {
   4406       /* We need a PT_GNU_RELRO segment.  */
   4407       ++segs;
   4408     }
   4409 
   4410   if (elf_eh_frame_hdr (info))
   4411     {
   4412       /* We need a PT_GNU_EH_FRAME segment.  */
   4413       ++segs;
   4414     }
   4415 
   4416   if (elf_stack_flags (abfd))
   4417     {
   4418       /* We need a PT_GNU_STACK segment.  */
   4419       ++segs;
   4420     }
   4421 
   4422   s = bfd_get_section_by_name (abfd,
   4423 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4424   if (s != NULL && s->size != 0)
   4425     {
   4426       /* We need a PT_GNU_PROPERTY segment.  */
   4427       ++segs;
   4428     }
   4429 
   4430   for (s = abfd->sections; s != NULL; s = s->next)
   4431     {
   4432       if ((s->flags & SEC_LOAD) != 0
   4433 	  && elf_section_type (s) == SHT_NOTE)
   4434 	{
   4435 	  unsigned int alignment_power;
   4436 	  /* We need a PT_NOTE segment.  */
   4437 	  ++segs;
   4438 	  /* Try to create just one PT_NOTE segment for all adjacent
   4439 	     loadable SHT_NOTE sections.  gABI requires that within a
   4440 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4441 	     each note should have the same alignment.  So we check
   4442 	     whether the sections are correctly aligned.  */
   4443 	  alignment_power = s->alignment_power;
   4444 	  while (s->next != NULL
   4445 		 && s->next->alignment_power == alignment_power
   4446 		 && (s->next->flags & SEC_LOAD) != 0
   4447 		 && elf_section_type (s->next) == SHT_NOTE)
   4448 	    s = s->next;
   4449 	}
   4450     }
   4451 
   4452   for (s = abfd->sections; s != NULL; s = s->next)
   4453     {
   4454       if (s->flags & SEC_THREAD_LOCAL)
   4455 	{
   4456 	  /* We need a PT_TLS segment.  */
   4457 	  ++segs;
   4458 	  break;
   4459 	}
   4460     }
   4461 
   4462   bed = get_elf_backend_data (abfd);
   4463 
   4464   if ((abfd->flags & D_PAGED) != 0
   4465       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4466     {
   4467       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4468       bfd_vma commonpagesize;
   4469       unsigned int page_align_power;
   4470 
   4471       if (info != NULL)
   4472 	commonpagesize = info->commonpagesize;
   4473       else
   4474 	commonpagesize = bed->commonpagesize;
   4475       page_align_power = bfd_log2 (commonpagesize);
   4476       for (s = abfd->sections; s != NULL; s = s->next)
   4477 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   4478 	  {
   4479 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   4480 	      {
   4481 		_bfd_error_handler
   4482 		  /* xgettext:c-format */
   4483 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   4484 		     "sh_info field: %d"),
   4485 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4486 		continue;
   4487 	      }
   4488 	    /* Align mbind section to page size.  */
   4489 	    if (s->alignment_power < page_align_power)
   4490 	      s->alignment_power = page_align_power;
   4491 	    segs ++;
   4492 	  }
   4493     }
   4494 
   4495   /* Let the backend count up any program headers it might need.  */
   4496   if (bed->elf_backend_additional_program_headers)
   4497     {
   4498       int a;
   4499 
   4500       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4501       if (a == -1)
   4502 	abort ();
   4503       segs += a;
   4504     }
   4505 
   4506   return segs * bed->s->sizeof_phdr;
   4507 }
   4508 
   4509 /* Find the segment that contains the output_section of section.  */
   4510 
   4511 Elf_Internal_Phdr *
   4512 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4513 {
   4514   struct elf_segment_map *m;
   4515   Elf_Internal_Phdr *p;
   4516 
   4517   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4518        m != NULL;
   4519        m = m->next, p++)
   4520     {
   4521       int i;
   4522 
   4523       for (i = m->count - 1; i >= 0; i--)
   4524 	if (m->sections[i] == section)
   4525 	  return p;
   4526     }
   4527 
   4528   return NULL;
   4529 }
   4530 
   4531 /* Create a mapping from a set of sections to a program segment.  */
   4532 
   4533 static struct elf_segment_map *
   4534 make_mapping (bfd *abfd,
   4535 	      asection **sections,
   4536 	      unsigned int from,
   4537 	      unsigned int to,
   4538 	      bool phdr)
   4539 {
   4540   struct elf_segment_map *m;
   4541   unsigned int i;
   4542   asection **hdrpp;
   4543   size_t amt;
   4544 
   4545   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   4546   amt += (to - from) * sizeof (asection *);
   4547   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4548   if (m == NULL)
   4549     return NULL;
   4550   m->next = NULL;
   4551   m->p_type = PT_LOAD;
   4552   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4553     m->sections[i - from] = *hdrpp;
   4554   m->count = to - from;
   4555 
   4556   if (from == 0 && phdr)
   4557     {
   4558       /* Include the headers in the first PT_LOAD segment.  */
   4559       m->includes_filehdr = 1;
   4560       m->includes_phdrs = 1;
   4561     }
   4562 
   4563   return m;
   4564 }
   4565 
   4566 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   4567    on failure.  */
   4568 
   4569 struct elf_segment_map *
   4570 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   4571 {
   4572   struct elf_segment_map *m;
   4573 
   4574   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   4575 					     sizeof (struct elf_segment_map));
   4576   if (m == NULL)
   4577     return NULL;
   4578   m->next = NULL;
   4579   m->p_type = PT_DYNAMIC;
   4580   m->count = 1;
   4581   m->sections[0] = dynsec;
   4582 
   4583   return m;
   4584 }
   4585 
   4586 /* Possibly add or remove segments from the segment map.  */
   4587 
   4588 static bool
   4589 elf_modify_segment_map (bfd *abfd,
   4590 			struct bfd_link_info *info,
   4591 			bool remove_empty_load)
   4592 {
   4593   struct elf_segment_map **m;
   4594   const struct elf_backend_data *bed;
   4595 
   4596   /* The placement algorithm assumes that non allocated sections are
   4597      not in PT_LOAD segments.  We ensure this here by removing such
   4598      sections from the segment map.  We also remove excluded
   4599      sections.  Finally, any PT_LOAD segment without sections is
   4600      removed.  */
   4601   m = &elf_seg_map (abfd);
   4602   while (*m)
   4603     {
   4604       unsigned int i, new_count;
   4605 
   4606       for (new_count = 0, i = 0; i < (*m)->count; i++)
   4607 	{
   4608 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   4609 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   4610 		  || (*m)->p_type != PT_LOAD))
   4611 	    {
   4612 	      (*m)->sections[new_count] = (*m)->sections[i];
   4613 	      new_count++;
   4614 	    }
   4615 	}
   4616       (*m)->count = new_count;
   4617 
   4618       if (remove_empty_load
   4619 	  && (*m)->p_type == PT_LOAD
   4620 	  && (*m)->count == 0
   4621 	  && !(*m)->includes_phdrs)
   4622 	*m = (*m)->next;
   4623       else
   4624 	m = &(*m)->next;
   4625     }
   4626 
   4627   bed = get_elf_backend_data (abfd);
   4628   if (bed->elf_backend_modify_segment_map != NULL)
   4629     {
   4630       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   4631 	return false;
   4632     }
   4633 
   4634   return true;
   4635 }
   4636 
   4637 #define IS_TBSS(s) \
   4638   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   4639 
   4640 /* Set up a mapping from BFD sections to program segments.  Update
   4641    NEED_LAYOUT if the section layout is changed.  */
   4642 
   4643 bool
   4644 _bfd_elf_map_sections_to_segments (bfd *abfd,
   4645 				   struct bfd_link_info *info,
   4646 				   bool *need_layout)
   4647 {
   4648   unsigned int count;
   4649   struct elf_segment_map *m;
   4650   asection **sections = NULL;
   4651   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4652   bool no_user_phdrs;
   4653 
   4654   no_user_phdrs = elf_seg_map (abfd) == NULL;
   4655 
   4656   if (info != NULL)
   4657     {
   4658       info->user_phdrs = !no_user_phdrs;
   4659 
   4660       /* Size the relative relocations if DT_RELR is enabled.  */
   4661       if (info->enable_dt_relr
   4662 	  && need_layout != NULL
   4663 	  && bed->size_relative_relocs
   4664 	  && !bed->size_relative_relocs (info, need_layout))
   4665 	info->callbacks->einfo
   4666 	  (_("%F%P: failed to size relative relocations\n"));
   4667     }
   4668 
   4669   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   4670     {
   4671       asection *s;
   4672       unsigned int i;
   4673       struct elf_segment_map *mfirst;
   4674       struct elf_segment_map **pm;
   4675       asection *last_hdr;
   4676       bfd_vma last_size;
   4677       unsigned int hdr_index;
   4678       bfd_vma maxpagesize;
   4679       asection **hdrpp;
   4680       bool phdr_in_segment;
   4681       bool writable;
   4682       bool executable;
   4683       unsigned int tls_count = 0;
   4684       asection *first_tls = NULL;
   4685       asection *first_mbind = NULL;
   4686       asection *dynsec, *eh_frame_hdr;
   4687       size_t amt;
   4688       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   4689       bfd_size_type phdr_size;  /* Octets/bytes.  */
   4690       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   4691 
   4692       /* Select the allocated sections, and sort them.  */
   4693 
   4694       amt = bfd_count_sections (abfd) * sizeof (asection *);
   4695       sections = (asection **) bfd_malloc (amt);
   4696       if (sections == NULL)
   4697 	goto error_return;
   4698 
   4699       /* Calculate top address, avoiding undefined behaviour of shift
   4700 	 left operator when shift count is equal to size of type
   4701 	 being shifted.  */
   4702       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   4703       addr_mask = (addr_mask << 1) + 1;
   4704 
   4705       i = 0;
   4706       for (s = abfd->sections; s != NULL; s = s->next)
   4707 	{
   4708 	  if ((s->flags & SEC_ALLOC) != 0)
   4709 	    {
   4710 	      /* target_index is unused until bfd_elf_final_link
   4711 		 starts output of section symbols.  Use it to make
   4712 		 qsort stable.  */
   4713 	      s->target_index = i;
   4714 	      sections[i] = s;
   4715 	      ++i;
   4716 	      /* A wrapping section potentially clashes with header.  */
   4717 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   4718 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   4719 	    }
   4720 	}
   4721       BFD_ASSERT (i <= bfd_count_sections (abfd));
   4722       count = i;
   4723 
   4724       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   4725 
   4726       phdr_size = elf_program_header_size (abfd);
   4727       if (phdr_size == (bfd_size_type) -1)
   4728 	phdr_size = get_program_header_size (abfd, info);
   4729       phdr_size += bed->s->sizeof_ehdr;
   4730       /* phdr_size is compared to LMA values which are in bytes.  */
   4731       phdr_size /= opb;
   4732       if (info != NULL)
   4733 	maxpagesize = info->maxpagesize;
   4734       else
   4735 	maxpagesize = bed->maxpagesize;
   4736       if (maxpagesize == 0)
   4737 	maxpagesize = 1;
   4738       phdr_in_segment = info != NULL && info->load_phdrs;
   4739       if (count != 0
   4740 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   4741 	      >= (phdr_size & (maxpagesize - 1))))
   4742 	/* For compatibility with old scripts that may not be using
   4743 	   SIZEOF_HEADERS, add headers when it looks like space has
   4744 	   been left for them.  */
   4745 	phdr_in_segment = true;
   4746 
   4747       /* Build the mapping.  */
   4748       mfirst = NULL;
   4749       pm = &mfirst;
   4750 
   4751       /* If we have a .interp section, then create a PT_PHDR segment for
   4752 	 the program headers and a PT_INTERP segment for the .interp
   4753 	 section.  */
   4754       s = bfd_get_section_by_name (abfd, ".interp");
   4755       if (s != NULL && (s->flags & SEC_LOAD) == 0)
   4756       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4757 	s = NULL;
   4758       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   4759       if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0)
   4760 	dynsec = NULL;
   4761 
   4762       if (s != NULL || dynsec != NULL)
   4763 	{
   4764 	  amt = sizeof (struct elf_segment_map);
   4765 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4766 	  if (m == NULL)
   4767 	    goto error_return;
   4768 	  m->next = NULL;
   4769 	  m->p_type = PT_PHDR;
   4770 	  m->p_flags = PF_R;
   4771 	  m->p_flags_valid = 1;
   4772 	  m->includes_phdrs = 1;
   4773 	  phdr_in_segment = true;
   4774 	  *pm = m;
   4775 	  pm = &m->next;
   4776 	}
   4777 
   4778       if (s != NULL)
   4779 	{
   4780 	  amt = sizeof (struct elf_segment_map);
   4781 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4782 	  if (m == NULL)
   4783 	    goto error_return;
   4784 	  m->next = NULL;
   4785 	  m->p_type = PT_INTERP;
   4786 	  m->count = 1;
   4787 	  m->sections[0] = s;
   4788 
   4789 	  *pm = m;
   4790 	  pm = &m->next;
   4791 	}
   4792 
   4793       /* Look through the sections.  We put sections in the same program
   4794 	 segment when the start of the second section can be placed within
   4795 	 a few bytes of the end of the first section.  */
   4796       last_hdr = NULL;
   4797       last_size = 0;
   4798       hdr_index = 0;
   4799       writable = false;
   4800       executable = false;
   4801 
   4802       if ((abfd->flags & D_PAGED) == 0)
   4803 	phdr_in_segment = false;
   4804 
   4805       /* Deal with -Ttext or something similar such that the first section
   4806 	 is not adjacent to the program headers.  This is an
   4807 	 approximation, since at this point we don't know exactly how many
   4808 	 program headers we will need.  */
   4809       if (phdr_in_segment && count > 0)
   4810 	{
   4811 	  bfd_vma phdr_lma;  /* Bytes.  */
   4812 	  bool separate_phdr = false;
   4813 
   4814 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   4815 	  if (info != NULL
   4816 	      && info->separate_code
   4817 	      && (sections[0]->flags & SEC_CODE) != 0)
   4818 	    {
   4819 	      /* If data sections should be separate from code and
   4820 		 thus not executable, and the first section is
   4821 		 executable then put the file and program headers in
   4822 		 their own PT_LOAD.  */
   4823 	      separate_phdr = true;
   4824 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   4825 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   4826 		{
   4827 		  /* The file and program headers are currently on the
   4828 		     same page as the first section.  Put them on the
   4829 		     previous page if we can.  */
   4830 		  if (phdr_lma >= maxpagesize)
   4831 		    phdr_lma -= maxpagesize;
   4832 		  else
   4833 		    separate_phdr = false;
   4834 		}
   4835 	    }
   4836 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   4837 	      || (sections[0]->lma & addr_mask) < phdr_size)
   4838 	    /* If file and program headers would be placed at the end
   4839 	       of memory then it's probably better to omit them.  */
   4840 	    phdr_in_segment = false;
   4841 	  else if (phdr_lma < wrap_to)
   4842 	    /* If a section wraps around to where we'll be placing
   4843 	       file and program headers, then the headers will be
   4844 	       overwritten.  */
   4845 	    phdr_in_segment = false;
   4846 	  else if (separate_phdr)
   4847 	    {
   4848 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   4849 	      if (m == NULL)
   4850 		goto error_return;
   4851 	      m->p_paddr = phdr_lma * opb;
   4852 	      m->p_vaddr_offset
   4853 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   4854 	      m->p_paddr_valid = 1;
   4855 	      *pm = m;
   4856 	      pm = &m->next;
   4857 	      phdr_in_segment = false;
   4858 	    }
   4859 	}
   4860 
   4861       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   4862 	{
   4863 	  asection *hdr;
   4864 	  bool new_segment;
   4865 
   4866 	  hdr = *hdrpp;
   4867 
   4868 	  /* See if this section and the last one will fit in the same
   4869 	     segment.  */
   4870 
   4871 	  if (last_hdr == NULL)
   4872 	    {
   4873 	      /* If we don't have a segment yet, then we don't need a new
   4874 		 one (we build the last one after this loop).  */
   4875 	      new_segment = false;
   4876 	    }
   4877 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   4878 	    {
   4879 	      /* If this section has a different relation between the
   4880 		 virtual address and the load address, then we need a new
   4881 		 segment.  */
   4882 	      new_segment = true;
   4883 	    }
   4884 	  else if (hdr->lma < last_hdr->lma + last_size
   4885 		   || last_hdr->lma + last_size < last_hdr->lma)
   4886 	    {
   4887 	      /* If this section has a load address that makes it overlap
   4888 		 the previous section, then we need a new segment.  */
   4889 	      new_segment = true;
   4890 	    }
   4891 	  else if ((abfd->flags & D_PAGED) != 0
   4892 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   4893 		       == (hdr->lma & -maxpagesize)))
   4894 	    {
   4895 	      /* If we are demand paged then we can't map two disk
   4896 		 pages onto the same memory page.  */
   4897 	      new_segment = false;
   4898 	    }
   4899 	  /* In the next test we have to be careful when last_hdr->lma is close
   4900 	     to the end of the address space.  If the aligned address wraps
   4901 	     around to the start of the address space, then there are no more
   4902 	     pages left in memory and it is OK to assume that the current
   4903 	     section can be included in the current segment.  */
   4904 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4905 		    + maxpagesize > last_hdr->lma)
   4906 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4907 		       + maxpagesize <= hdr->lma))
   4908 	    {
   4909 	      /* If putting this section in this segment would force us to
   4910 		 skip a page in the segment, then we need a new segment.  */
   4911 	      new_segment = true;
   4912 	    }
   4913 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   4914 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   4915 	    {
   4916 	      /* We don't want to put a loaded section after a
   4917 		 nonloaded (ie. bss style) section in the same segment
   4918 		 as that will force the non-loaded section to be loaded.
   4919 		 Consider .tbss sections as loaded for this purpose.  */
   4920 	      new_segment = true;
   4921 	    }
   4922 	  else if ((abfd->flags & D_PAGED) == 0)
   4923 	    {
   4924 	      /* If the file is not demand paged, which means that we
   4925 		 don't require the sections to be correctly aligned in the
   4926 		 file, then there is no other reason for a new segment.  */
   4927 	      new_segment = false;
   4928 	    }
   4929 	  else if (info != NULL
   4930 		   && info->separate_code
   4931 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   4932 	    {
   4933 	      new_segment = true;
   4934 	    }
   4935 	  else if (! writable
   4936 		   && (hdr->flags & SEC_READONLY) == 0)
   4937 	    {
   4938 	      /* We don't want to put a writable section in a read only
   4939 		 segment.  */
   4940 	      new_segment = true;
   4941 	    }
   4942 	  else
   4943 	    {
   4944 	      /* Otherwise, we can use the same segment.  */
   4945 	      new_segment = false;
   4946 	    }
   4947 
   4948 	  /* Allow interested parties a chance to override our decision.  */
   4949 	  if (last_hdr != NULL
   4950 	      && info != NULL
   4951 	      && info->callbacks->override_segment_assignment != NULL)
   4952 	    new_segment
   4953 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   4954 							      last_hdr,
   4955 							      new_segment);
   4956 
   4957 	  if (! new_segment)
   4958 	    {
   4959 	      if ((hdr->flags & SEC_READONLY) == 0)
   4960 		writable = true;
   4961 	      if ((hdr->flags & SEC_CODE) != 0)
   4962 		executable = true;
   4963 	      last_hdr = hdr;
   4964 	      /* .tbss sections effectively have zero size.  */
   4965 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   4966 	      continue;
   4967 	    }
   4968 
   4969 	  /* We need a new program segment.  We must create a new program
   4970 	     header holding all the sections from hdr_index until hdr.  */
   4971 
   4972 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   4973 	  if (m == NULL)
   4974 	    goto error_return;
   4975 
   4976 	  *pm = m;
   4977 	  pm = &m->next;
   4978 
   4979 	  if ((hdr->flags & SEC_READONLY) == 0)
   4980 	    writable = true;
   4981 	  else
   4982 	    writable = false;
   4983 
   4984 	  if ((hdr->flags & SEC_CODE) == 0)
   4985 	    executable = false;
   4986 	  else
   4987 	    executable = true;
   4988 
   4989 	  last_hdr = hdr;
   4990 	  /* .tbss sections effectively have zero size.  */
   4991 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   4992 	  hdr_index = i;
   4993 	  phdr_in_segment = false;
   4994 	}
   4995 
   4996       /* Create a final PT_LOAD program segment, but not if it's just
   4997 	 for .tbss.  */
   4998       if (last_hdr != NULL
   4999 	  && (i - hdr_index != 1
   5000 	      || !IS_TBSS (last_hdr)))
   5001 	{
   5002 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5003 	  if (m == NULL)
   5004 	    goto error_return;
   5005 
   5006 	  *pm = m;
   5007 	  pm = &m->next;
   5008 	}
   5009 
   5010       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   5011       if (dynsec != NULL)
   5012 	{
   5013 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   5014 	  if (m == NULL)
   5015 	    goto error_return;
   5016 	  *pm = m;
   5017 	  pm = &m->next;
   5018 	}
   5019 
   5020       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5021 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5022 	 because if we link together nonloadable .note sections and
   5023 	 loadable .note sections, we will generate two .note sections
   5024 	 in the output file.  */
   5025       for (s = abfd->sections; s != NULL; s = s->next)
   5026 	{
   5027 	  if ((s->flags & SEC_LOAD) != 0
   5028 	      && elf_section_type (s) == SHT_NOTE)
   5029 	    {
   5030 	      asection *s2;
   5031 	      unsigned int alignment_power = s->alignment_power;
   5032 
   5033 	      count = 1;
   5034 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5035 		{
   5036 		  if (s2->next->alignment_power == alignment_power
   5037 		      && (s2->next->flags & SEC_LOAD) != 0
   5038 		      && elf_section_type (s2->next) == SHT_NOTE
   5039 		      && align_power (s2->lma + s2->size / opb,
   5040 				      alignment_power)
   5041 		      == s2->next->lma)
   5042 		    count++;
   5043 		  else
   5044 		    break;
   5045 		}
   5046 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5047 	      amt += count * sizeof (asection *);
   5048 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5049 	      if (m == NULL)
   5050 		goto error_return;
   5051 	      m->next = NULL;
   5052 	      m->p_type = PT_NOTE;
   5053 	      m->count = count;
   5054 	      while (count > 1)
   5055 		{
   5056 		  m->sections[m->count - count--] = s;
   5057 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5058 		  s = s->next;
   5059 		}
   5060 	      m->sections[m->count - 1] = s;
   5061 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5062 	      *pm = m;
   5063 	      pm = &m->next;
   5064 	    }
   5065 	  if (s->flags & SEC_THREAD_LOCAL)
   5066 	    {
   5067 	      if (! tls_count)
   5068 		first_tls = s;
   5069 	      tls_count++;
   5070 	    }
   5071 	  if (first_mbind == NULL
   5072 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5073 	    first_mbind = s;
   5074 	}
   5075 
   5076       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5077       if (tls_count > 0)
   5078 	{
   5079 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5080 	  amt += tls_count * sizeof (asection *);
   5081 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5082 	  if (m == NULL)
   5083 	    goto error_return;
   5084 	  m->next = NULL;
   5085 	  m->p_type = PT_TLS;
   5086 	  m->count = tls_count;
   5087 	  /* Mandated PF_R.  */
   5088 	  m->p_flags = PF_R;
   5089 	  m->p_flags_valid = 1;
   5090 	  s = first_tls;
   5091 	  for (i = 0; i < tls_count; ++i)
   5092 	    {
   5093 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5094 		{
   5095 		  _bfd_error_handler
   5096 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5097 		  s = first_tls;
   5098 		  i = 0;
   5099 		  while (i < tls_count)
   5100 		    {
   5101 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5102 			{
   5103 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5104 			  i++;
   5105 			}
   5106 		      else
   5107 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5108 		      s = s->next;
   5109 		    }
   5110 		  bfd_set_error (bfd_error_bad_value);
   5111 		  goto error_return;
   5112 		}
   5113 	      m->sections[i] = s;
   5114 	      s = s->next;
   5115 	    }
   5116 
   5117 	  *pm = m;
   5118 	  pm = &m->next;
   5119 	}
   5120 
   5121       if (first_mbind
   5122 	  && (abfd->flags & D_PAGED) != 0
   5123 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5124 	for (s = first_mbind; s != NULL; s = s->next)
   5125 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5126 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5127 	    {
   5128 	      /* Mandated PF_R.  */
   5129 	      unsigned long p_flags = PF_R;
   5130 	      if ((s->flags & SEC_READONLY) == 0)
   5131 		p_flags |= PF_W;
   5132 	      if ((s->flags & SEC_CODE) != 0)
   5133 		p_flags |= PF_X;
   5134 
   5135 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5136 	      m = bfd_zalloc (abfd, amt);
   5137 	      if (m == NULL)
   5138 		goto error_return;
   5139 	      m->next = NULL;
   5140 	      m->p_type = (PT_GNU_MBIND_LO
   5141 			   + elf_section_data (s)->this_hdr.sh_info);
   5142 	      m->count = 1;
   5143 	      m->p_flags_valid = 1;
   5144 	      m->sections[0] = s;
   5145 	      m->p_flags = p_flags;
   5146 
   5147 	      *pm = m;
   5148 	      pm = &m->next;
   5149 	    }
   5150 
   5151       s = bfd_get_section_by_name (abfd,
   5152 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5153       if (s != NULL && s->size != 0)
   5154 	{
   5155 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5156 	  m = bfd_zalloc (abfd, amt);
   5157 	  if (m == NULL)
   5158 	    goto error_return;
   5159 	  m->next = NULL;
   5160 	  m->p_type = PT_GNU_PROPERTY;
   5161 	  m->count = 1;
   5162 	  m->p_flags_valid = 1;
   5163 	  m->sections[0] = s;
   5164 	  m->p_flags = PF_R;
   5165 	  *pm = m;
   5166 	  pm = &m->next;
   5167 	}
   5168 
   5169       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5170 	 segment.  */
   5171       eh_frame_hdr = elf_eh_frame_hdr (info);
   5172       if (eh_frame_hdr != NULL
   5173 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5174 	{
   5175 	  amt = sizeof (struct elf_segment_map);
   5176 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5177 	  if (m == NULL)
   5178 	    goto error_return;
   5179 	  m->next = NULL;
   5180 	  m->p_type = PT_GNU_EH_FRAME;
   5181 	  m->count = 1;
   5182 	  m->sections[0] = eh_frame_hdr->output_section;
   5183 
   5184 	  *pm = m;
   5185 	  pm = &m->next;
   5186 	}
   5187 
   5188       if (elf_stack_flags (abfd))
   5189 	{
   5190 	  amt = sizeof (struct elf_segment_map);
   5191 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5192 	  if (m == NULL)
   5193 	    goto error_return;
   5194 	  m->next = NULL;
   5195 	  m->p_type = PT_GNU_STACK;
   5196 	  m->p_flags = elf_stack_flags (abfd);
   5197 	  m->p_align = bed->stack_align;
   5198 	  m->p_flags_valid = 1;
   5199 	  m->p_align_valid = m->p_align != 0;
   5200 	  if (info->stacksize > 0)
   5201 	    {
   5202 	      m->p_size = info->stacksize;
   5203 	      m->p_size_valid = 1;
   5204 	    }
   5205 
   5206 	  *pm = m;
   5207 	  pm = &m->next;
   5208 	}
   5209 
   5210       if (info != NULL && info->relro)
   5211 	{
   5212 	  for (m = mfirst; m != NULL; m = m->next)
   5213 	    {
   5214 	      if (m->p_type == PT_LOAD
   5215 		  && m->count != 0
   5216 		  && m->sections[0]->vma >= info->relro_start
   5217 		  && m->sections[0]->vma < info->relro_end)
   5218 		{
   5219 		  i = m->count;
   5220 		  while (--i != (unsigned) -1)
   5221 		    {
   5222 		      if (m->sections[i]->size > 0
   5223 			  && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
   5224 			  == (SEC_LOAD | SEC_HAS_CONTENTS))
   5225 			break;
   5226 		    }
   5227 
   5228 		  if (i != (unsigned) -1)
   5229 		    break;
   5230 		}
   5231 	    }
   5232 
   5233 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5234 	  if (m != NULL)
   5235 	    {
   5236 	      amt = sizeof (struct elf_segment_map);
   5237 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5238 	      if (m == NULL)
   5239 		goto error_return;
   5240 	      m->next = NULL;
   5241 	      m->p_type = PT_GNU_RELRO;
   5242 	      *pm = m;
   5243 	      pm = &m->next;
   5244 	    }
   5245 	}
   5246 
   5247       free (sections);
   5248       elf_seg_map (abfd) = mfirst;
   5249     }
   5250 
   5251   if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
   5252     return false;
   5253 
   5254   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5255     ++count;
   5256   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5257 
   5258   return true;
   5259 
   5260  error_return:
   5261   free (sections);
   5262   return false;
   5263 }
   5264 
   5265 /* Sort sections by address.  */
   5266 
   5267 static int
   5268 elf_sort_sections (const void *arg1, const void *arg2)
   5269 {
   5270   const asection *sec1 = *(const asection **) arg1;
   5271   const asection *sec2 = *(const asection **) arg2;
   5272   bfd_size_type size1, size2;
   5273 
   5274   /* Sort by LMA first, since this is the address used to
   5275      place the section into a segment.  */
   5276   if (sec1->lma < sec2->lma)
   5277     return -1;
   5278   else if (sec1->lma > sec2->lma)
   5279     return 1;
   5280 
   5281   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5282      the same, and this will do nothing.  */
   5283   if (sec1->vma < sec2->vma)
   5284     return -1;
   5285   else if (sec1->vma > sec2->vma)
   5286     return 1;
   5287 
   5288   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5289 
   5290 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5291 		  && (x)->size != 0)
   5292 
   5293   if (TOEND (sec1))
   5294     {
   5295       if (!TOEND (sec2))
   5296 	return 1;
   5297     }
   5298   else if (TOEND (sec2))
   5299     return -1;
   5300 
   5301 #undef TOEND
   5302 
   5303   /* Sort by size, to put zero sized sections
   5304      before others at the same address.  */
   5305 
   5306   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5307   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5308 
   5309   if (size1 < size2)
   5310     return -1;
   5311   if (size1 > size2)
   5312     return 1;
   5313 
   5314   return sec1->target_index - sec2->target_index;
   5315 }
   5316 
   5317 /* This qsort comparison functions sorts PT_LOAD segments first and
   5318    by p_paddr, for assign_file_positions_for_load_sections.  */
   5319 
   5320 static int
   5321 elf_sort_segments (const void *arg1, const void *arg2)
   5322 {
   5323   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5324   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5325 
   5326   if (m1->p_type != m2->p_type)
   5327     {
   5328       if (m1->p_type == PT_NULL)
   5329 	return 1;
   5330       if (m2->p_type == PT_NULL)
   5331 	return -1;
   5332       return m1->p_type < m2->p_type ? -1 : 1;
   5333     }
   5334   if (m1->includes_filehdr != m2->includes_filehdr)
   5335     return m1->includes_filehdr ? -1 : 1;
   5336   if (m1->no_sort_lma != m2->no_sort_lma)
   5337     return m1->no_sort_lma ? -1 : 1;
   5338   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5339     {
   5340       bfd_vma lma1, lma2;  /* Octets.  */
   5341       lma1 = 0;
   5342       if (m1->p_paddr_valid)
   5343 	lma1 = m1->p_paddr;
   5344       else if (m1->count != 0)
   5345 	{
   5346 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5347 						  m1->sections[0]);
   5348 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5349 	}
   5350       lma2 = 0;
   5351       if (m2->p_paddr_valid)
   5352 	lma2 = m2->p_paddr;
   5353       else if (m2->count != 0)
   5354 	{
   5355 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5356 						  m2->sections[0]);
   5357 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5358 	}
   5359       if (lma1 != lma2)
   5360 	return lma1 < lma2 ? -1 : 1;
   5361     }
   5362   if (m1->idx != m2->idx)
   5363     return m1->idx < m2->idx ? -1 : 1;
   5364   return 0;
   5365 }
   5366 
   5367 /* Ian Lance Taylor writes:
   5368 
   5369    We shouldn't be using % with a negative signed number.  That's just
   5370    not good.  We have to make sure either that the number is not
   5371    negative, or that the number has an unsigned type.  When the types
   5372    are all the same size they wind up as unsigned.  When file_ptr is a
   5373    larger signed type, the arithmetic winds up as signed long long,
   5374    which is wrong.
   5375 
   5376    What we're trying to say here is something like ``increase OFF by
   5377    the least amount that will cause it to be equal to the VMA modulo
   5378    the page size.''  */
   5379 /* In other words, something like:
   5380 
   5381    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5382    off_offset = off % bed->maxpagesize;
   5383    if (vma_offset < off_offset)
   5384      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5385    else
   5386      adjustment = vma_offset - off_offset;
   5387 
   5388    which can be collapsed into the expression below.  */
   5389 
   5390 static file_ptr
   5391 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5392 {
   5393   /* PR binutils/16199: Handle an alignment of zero.  */
   5394   if (maxpagesize == 0)
   5395     maxpagesize = 1;
   5396   return ((vma - off) % maxpagesize);
   5397 }
   5398 
   5399 static void
   5400 print_segment_map (const struct elf_segment_map *m)
   5401 {
   5402   unsigned int j;
   5403   const char *pt = get_segment_type (m->p_type);
   5404   char buf[32];
   5405 
   5406   if (pt == NULL)
   5407     {
   5408       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5409 	sprintf (buf, "LOPROC+%7.7x",
   5410 		 (unsigned int) (m->p_type - PT_LOPROC));
   5411       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5412 	sprintf (buf, "LOOS+%7.7x",
   5413 		 (unsigned int) (m->p_type - PT_LOOS));
   5414       else
   5415 	snprintf (buf, sizeof (buf), "%8.8x",
   5416 		  (unsigned int) m->p_type);
   5417       pt = buf;
   5418     }
   5419   fflush (stdout);
   5420   fprintf (stderr, "%s:", pt);
   5421   for (j = 0; j < m->count; j++)
   5422     fprintf (stderr, " %s", m->sections [j]->name);
   5423   putc ('\n',stderr);
   5424   fflush (stderr);
   5425 }
   5426 
   5427 static bool
   5428 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
   5429 {
   5430   void *buf;
   5431   bool ret;
   5432 
   5433   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   5434     return false;
   5435   buf = bfd_zmalloc (len);
   5436   if (buf == NULL)
   5437     return false;
   5438   ret = bfd_bwrite (buf, len, abfd) == len;
   5439   free (buf);
   5440   return ret;
   5441 }
   5442 
   5443 /* Assign file positions to the sections based on the mapping from
   5444    sections to segments.  This function also sets up some fields in
   5445    the file header.  */
   5446 
   5447 static bool
   5448 assign_file_positions_for_load_sections (bfd *abfd,
   5449 					 struct bfd_link_info *link_info)
   5450 {
   5451   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5452   struct elf_segment_map *m;
   5453   struct elf_segment_map *phdr_load_seg;
   5454   Elf_Internal_Phdr *phdrs;
   5455   Elf_Internal_Phdr *p;
   5456   file_ptr off;  /* Octets.  */
   5457   bfd_size_type maxpagesize;
   5458   bfd_size_type p_align;
   5459   bool p_align_p = false;
   5460   unsigned int alloc, actual;
   5461   unsigned int i, j;
   5462   struct elf_segment_map **sorted_seg_map;
   5463   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5464 
   5465   if (link_info == NULL
   5466       && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5467     return false;
   5468 
   5469   alloc = 0;
   5470   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5471     m->idx = alloc++;
   5472 
   5473   if (alloc)
   5474     {
   5475       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5476       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5477     }
   5478   else
   5479     {
   5480       /* PR binutils/12467.  */
   5481       elf_elfheader (abfd)->e_phoff = 0;
   5482       elf_elfheader (abfd)->e_phentsize = 0;
   5483     }
   5484 
   5485   elf_elfheader (abfd)->e_phnum = alloc;
   5486 
   5487   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5488     {
   5489       actual = alloc;
   5490       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5491     }
   5492   else
   5493     {
   5494       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   5495       BFD_ASSERT (elf_program_header_size (abfd)
   5496 		  == actual * bed->s->sizeof_phdr);
   5497       BFD_ASSERT (actual >= alloc);
   5498     }
   5499 
   5500   if (alloc == 0)
   5501     {
   5502       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5503       return true;
   5504     }
   5505 
   5506   /* We're writing the size in elf_program_header_size (abfd),
   5507      see assign_file_positions_except_relocs, so make sure we have
   5508      that amount allocated, with trailing space cleared.
   5509      The variable alloc contains the computed need, while
   5510      elf_program_header_size (abfd) contains the size used for the
   5511      layout.
   5512      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5513      where the layout is forced to according to a larger size in the
   5514      last iterations for the testcase ld-elf/header.  */
   5515   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   5516 			     + alloc * sizeof (*sorted_seg_map)));
   5517   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   5518   elf_tdata (abfd)->phdr = phdrs;
   5519   if (phdrs == NULL)
   5520     return false;
   5521 
   5522   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   5523     {
   5524       sorted_seg_map[j] = m;
   5525       /* If elf_segment_map is not from map_sections_to_segments, the
   5526 	 sections may not be correctly ordered.  NOTE: sorting should
   5527 	 not be done to the PT_NOTE section of a corefile, which may
   5528 	 contain several pseudo-sections artificially created by bfd.
   5529 	 Sorting these pseudo-sections breaks things badly.  */
   5530       if (m->count > 1
   5531 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5532 	       && m->p_type == PT_NOTE))
   5533 	{
   5534 	  for (i = 0; i < m->count; i++)
   5535 	    m->sections[i]->target_index = i;
   5536 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5537 		 elf_sort_sections);
   5538 	}
   5539     }
   5540   if (alloc > 1)
   5541     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   5542 	   elf_sort_segments);
   5543 
   5544   p_align = bed->p_align;
   5545   maxpagesize = 1;
   5546   if ((abfd->flags & D_PAGED) != 0)
   5547     {
   5548       if (link_info != NULL)
   5549 	maxpagesize = link_info->maxpagesize;
   5550       else
   5551 	maxpagesize = bed->maxpagesize;
   5552     }
   5553 
   5554   /* Sections must map to file offsets past the ELF file header.  */
   5555   off = bed->s->sizeof_ehdr;
   5556   /* And if one of the PT_LOAD headers doesn't include the program
   5557      headers then we'll be mapping program headers in the usual
   5558      position after the ELF file header.  */
   5559   phdr_load_seg = NULL;
   5560   for (j = 0; j < alloc; j++)
   5561     {
   5562       m = sorted_seg_map[j];
   5563       if (m->p_type != PT_LOAD)
   5564 	break;
   5565       if (m->includes_phdrs)
   5566 	{
   5567 	  phdr_load_seg = m;
   5568 	  break;
   5569 	}
   5570     }
   5571   if (phdr_load_seg == NULL)
   5572     off += actual * bed->s->sizeof_phdr;
   5573 
   5574   for (j = 0; j < alloc; j++)
   5575     {
   5576       asection **secpp;
   5577       bfd_vma off_adjust;  /* Octets.  */
   5578       bool no_contents;
   5579 
   5580       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   5581 	 number of sections with contents contributing to both p_filesz
   5582 	 and p_memsz, followed by a number of sections with no contents
   5583 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   5584 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   5585       m = sorted_seg_map[j];
   5586       p = phdrs + m->idx;
   5587       p->p_type = m->p_type;
   5588       p->p_flags = m->p_flags;
   5589 
   5590       if (m->count == 0)
   5591 	p->p_vaddr = m->p_vaddr_offset * opb;
   5592       else
   5593 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   5594 
   5595       if (m->p_paddr_valid)
   5596 	p->p_paddr = m->p_paddr;
   5597       else if (m->count == 0)
   5598 	p->p_paddr = 0;
   5599       else
   5600 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   5601 
   5602       if (p->p_type == PT_LOAD
   5603 	  && (abfd->flags & D_PAGED) != 0)
   5604 	{
   5605 	  /* p_align in demand paged PT_LOAD segments effectively stores
   5606 	     the maximum page size.  When copying an executable with
   5607 	     objcopy, we set m->p_align from the input file.  Use this
   5608 	     value for maxpagesize rather than bed->maxpagesize, which
   5609 	     may be different.  Note that we use maxpagesize for PT_TLS
   5610 	     segment alignment later in this function, so we are relying
   5611 	     on at least one PT_LOAD segment appearing before a PT_TLS
   5612 	     segment.  */
   5613 	  if (m->p_align_valid)
   5614 	    maxpagesize = m->p_align;
   5615 	  else if (p_align != 0
   5616 		   && (link_info == NULL
   5617 		       || !link_info->maxpagesize_is_set))
   5618 	    /* Set p_align to the default p_align value while laying
   5619 	       out segments aligning to the maximum page size or the
   5620 	       largest section alignment.  The run-time loader can
   5621 	       align segments to the default p_align value or the
   5622 	       maximum page size, depending on system page size.  */
   5623 	    p_align_p = true;
   5624 
   5625 	  p->p_align = maxpagesize;
   5626 	}
   5627       else if (m->p_align_valid)
   5628 	p->p_align = m->p_align;
   5629       else if (m->count == 0)
   5630 	p->p_align = 1 << bed->s->log_file_align;
   5631 
   5632       if (m == phdr_load_seg)
   5633 	{
   5634 	  if (!m->includes_filehdr)
   5635 	    p->p_offset = off;
   5636 	  off += actual * bed->s->sizeof_phdr;
   5637 	}
   5638 
   5639       no_contents = false;
   5640       off_adjust = 0;
   5641       if (p->p_type == PT_LOAD
   5642 	  && m->count > 0)
   5643 	{
   5644 	  bfd_size_type align;  /* Bytes.  */
   5645 	  unsigned int align_power = 0;
   5646 
   5647 	  if (m->p_align_valid)
   5648 	    align = p->p_align;
   5649 	  else
   5650 	    {
   5651 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5652 		{
   5653 		  unsigned int secalign;
   5654 
   5655 		  secalign = bfd_section_alignment (*secpp);
   5656 		  if (secalign > align_power)
   5657 		    align_power = secalign;
   5658 		}
   5659 	      align = (bfd_size_type) 1 << align_power;
   5660 	      if (align < maxpagesize)
   5661 		{
   5662 		  /* If a section requires alignment higher than the
   5663 		     default p_align value, don't set p_align to the
   5664 		     default p_align value.  */
   5665 		  if (align > p_align)
   5666 		    p_align_p = false;
   5667 		  align = maxpagesize;
   5668 		}
   5669 	      else
   5670 		{
   5671 		  /* If a section requires alignment higher than the
   5672 		     maximum page size, set p_align to the section
   5673 		     alignment.  */
   5674 		  p_align_p = true;
   5675 		  p_align = align;
   5676 		}
   5677 	    }
   5678 
   5679 	  for (i = 0; i < m->count; i++)
   5680 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   5681 	      /* If we aren't making room for this section, then
   5682 		 it must be SHT_NOBITS regardless of what we've
   5683 		 set via struct bfd_elf_special_section.  */
   5684 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   5685 
   5686 	  /* Find out whether this segment contains any loadable
   5687 	     sections.  */
   5688 	  no_contents = true;
   5689 	  for (i = 0; i < m->count; i++)
   5690 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   5691 	      {
   5692 		no_contents = false;
   5693 		break;
   5694 	      }
   5695 
   5696 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   5697 
   5698 	  /* Broken hardware and/or kernel require that files do not
   5699 	     map the same page with different permissions on some hppa
   5700 	     processors.  */
   5701 	  if (j != 0
   5702 	      && (abfd->flags & D_PAGED) != 0
   5703 	      && bed->no_page_alias
   5704 	      && (off & (maxpagesize - 1)) != 0
   5705 	      && ((off & -maxpagesize)
   5706 		  == ((off + off_adjust) & -maxpagesize)))
   5707 	    off_adjust += maxpagesize;
   5708 	  off += off_adjust;
   5709 	  if (no_contents)
   5710 	    {
   5711 	      /* We shouldn't need to align the segment on disk since
   5712 		 the segment doesn't need file space, but the gABI
   5713 		 arguably requires the alignment and glibc ld.so
   5714 		 checks it.  So to comply with the alignment
   5715 		 requirement but not waste file space, we adjust
   5716 		 p_offset for just this segment.  (OFF_ADJUST is
   5717 		 subtracted from OFF later.)  This may put p_offset
   5718 		 past the end of file, but that shouldn't matter.  */
   5719 	    }
   5720 	  else
   5721 	    off_adjust = 0;
   5722 	}
   5723       /* Make sure the .dynamic section is the first section in the
   5724 	 PT_DYNAMIC segment.  */
   5725       else if (p->p_type == PT_DYNAMIC
   5726 	       && m->count > 1
   5727 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   5728 	{
   5729 	  _bfd_error_handler
   5730 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   5731 	       " is not the .dynamic section"),
   5732 	     abfd);
   5733 	  bfd_set_error (bfd_error_bad_value);
   5734 	  return false;
   5735 	}
   5736       /* Set the note section type to SHT_NOTE.  */
   5737       else if (p->p_type == PT_NOTE)
   5738 	for (i = 0; i < m->count; i++)
   5739 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   5740 
   5741       if (m->includes_filehdr)
   5742 	{
   5743 	  if (!m->p_flags_valid)
   5744 	    p->p_flags |= PF_R;
   5745 	  p->p_filesz = bed->s->sizeof_ehdr;
   5746 	  p->p_memsz = bed->s->sizeof_ehdr;
   5747 	  if (p->p_type == PT_LOAD)
   5748 	    {
   5749 	      if (m->count > 0)
   5750 		{
   5751 		  if (p->p_vaddr < (bfd_vma) off
   5752 		      || (!m->p_paddr_valid
   5753 			  && p->p_paddr < (bfd_vma) off))
   5754 		    {
   5755 		      _bfd_error_handler
   5756 			(_("%pB: not enough room for program headers,"
   5757 			   " try linking with -N"),
   5758 			 abfd);
   5759 		      bfd_set_error (bfd_error_bad_value);
   5760 		      return false;
   5761 		    }
   5762 		  p->p_vaddr -= off;
   5763 		  if (!m->p_paddr_valid)
   5764 		    p->p_paddr -= off;
   5765 		}
   5766 	    }
   5767 	  else if (sorted_seg_map[0]->includes_filehdr)
   5768 	    {
   5769 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   5770 	      p->p_vaddr = filehdr->p_vaddr;
   5771 	      if (!m->p_paddr_valid)
   5772 		p->p_paddr = filehdr->p_paddr;
   5773 	    }
   5774 	}
   5775 
   5776       if (m->includes_phdrs)
   5777 	{
   5778 	  if (!m->p_flags_valid)
   5779 	    p->p_flags |= PF_R;
   5780 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   5781 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   5782 	  if (!m->includes_filehdr)
   5783 	    {
   5784 	      if (p->p_type == PT_LOAD)
   5785 		{
   5786 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   5787 		  if (m->count > 0)
   5788 		    {
   5789 		      p->p_vaddr -= off - p->p_offset;
   5790 		      if (!m->p_paddr_valid)
   5791 			p->p_paddr -= off - p->p_offset;
   5792 		    }
   5793 		}
   5794 	      else if (phdr_load_seg != NULL)
   5795 		{
   5796 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   5797 		  bfd_vma phdr_off = 0;  /* Octets.  */
   5798 		  if (phdr_load_seg->includes_filehdr)
   5799 		    phdr_off = bed->s->sizeof_ehdr;
   5800 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   5801 		  if (!m->p_paddr_valid)
   5802 		    p->p_paddr = phdr->p_paddr + phdr_off;
   5803 		  p->p_offset = phdr->p_offset + phdr_off;
   5804 		}
   5805 	      else
   5806 		p->p_offset = bed->s->sizeof_ehdr;
   5807 	    }
   5808 	}
   5809 
   5810       if (p->p_type == PT_LOAD
   5811 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   5812 	{
   5813 	  if (!m->includes_filehdr && !m->includes_phdrs)
   5814 	    {
   5815 	      p->p_offset = off;
   5816 	      if (no_contents)
   5817 		{
   5818 		  /* Put meaningless p_offset for PT_LOAD segments
   5819 		     without file contents somewhere within the first
   5820 		     page, in an attempt to not point past EOF.  */
   5821 		  bfd_size_type align = maxpagesize;
   5822 		  if (align < p->p_align)
   5823 		    align = p->p_align;
   5824 		  if (align < 1)
   5825 		    align = 1;
   5826 		  p->p_offset = off % align;
   5827 		}
   5828 	    }
   5829 	  else
   5830 	    {
   5831 	      file_ptr adjust;  /* Octets.  */
   5832 
   5833 	      adjust = off - (p->p_offset + p->p_filesz);
   5834 	      if (!no_contents)
   5835 		p->p_filesz += adjust;
   5836 	      p->p_memsz += adjust;
   5837 	    }
   5838 	}
   5839 
   5840       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   5841 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   5842 	 core files, for sections in PT_NOTE segments.
   5843 	 assign_file_positions_for_non_load_sections will set filepos
   5844 	 for other sections and update p_filesz for other segments.  */
   5845       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5846 	{
   5847 	  asection *sec;
   5848 	  bfd_size_type align;
   5849 	  Elf_Internal_Shdr *this_hdr;
   5850 
   5851 	  sec = *secpp;
   5852 	  this_hdr = &elf_section_data (sec)->this_hdr;
   5853 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   5854 
   5855 	  if ((p->p_type == PT_LOAD
   5856 	       || p->p_type == PT_TLS)
   5857 	      && (this_hdr->sh_type != SHT_NOBITS
   5858 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   5859 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   5860 			  || p->p_type == PT_TLS))))
   5861 	    {
   5862 	      bfd_vma p_start = p->p_paddr;                /* Octets.  */
   5863 	      bfd_vma p_end = p_start + p->p_memsz;        /* Octets.  */
   5864 	      bfd_vma s_start = sec->lma * opb;            /* Octets.  */
   5865 	      bfd_vma adjust = s_start - p_end;            /* Octets.  */
   5866 
   5867 	      if (adjust != 0
   5868 		  && (s_start < p_end
   5869 		      || p_end < p_start))
   5870 		{
   5871 		  _bfd_error_handler
   5872 		    /* xgettext:c-format */
   5873 		    (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
   5874 		     abfd, sec, (uint64_t) s_start / opb,
   5875 		     (uint64_t) p_end / opb);
   5876 		  adjust = 0;
   5877 		  sec->lma = p_end / opb;
   5878 		}
   5879 	      p->p_memsz += adjust;
   5880 
   5881 	      if (p->p_type == PT_LOAD)
   5882 		{
   5883 		  if (this_hdr->sh_type != SHT_NOBITS)
   5884 		    {
   5885 		      off_adjust = 0;
   5886 		      if (p->p_filesz + adjust < p->p_memsz)
   5887 			{
   5888 			  /* We have a PROGBITS section following NOBITS ones.
   5889 			     Allocate file space for the NOBITS section(s) and
   5890 			     zero it.  */
   5891 			  adjust = p->p_memsz - p->p_filesz;
   5892 			  if (!write_zeros (abfd, off, adjust))
   5893 			    return false;
   5894 			}
   5895 		    }
   5896 		  /* We only adjust sh_offset in SHT_NOBITS sections
   5897 		     as would seem proper for their address when the
   5898 		     section is first in the segment.  sh_offset
   5899 		     doesn't really have any significance for
   5900 		     SHT_NOBITS anyway, apart from a notional position
   5901 		     relative to other sections.  Historically we
   5902 		     didn't bother with adjusting sh_offset and some
   5903 		     programs depend on it not being adjusted.  See
   5904 		     pr12921 and pr25662.  */
   5905 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   5906 		    {
   5907 		      off += adjust;
   5908 		      if (this_hdr->sh_type == SHT_NOBITS)
   5909 			off_adjust += adjust;
   5910 		    }
   5911 		}
   5912 	      if (this_hdr->sh_type != SHT_NOBITS)
   5913 		p->p_filesz += adjust;
   5914 	    }
   5915 
   5916 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   5917 	    {
   5918 	      /* The section at i == 0 is the one that actually contains
   5919 		 everything.  */
   5920 	      if (i == 0)
   5921 		{
   5922 		  this_hdr->sh_offset = sec->filepos = off;
   5923 		  off += this_hdr->sh_size;
   5924 		  p->p_filesz = this_hdr->sh_size;
   5925 		  p->p_memsz = 0;
   5926 		  p->p_align = 1;
   5927 		}
   5928 	      else
   5929 		{
   5930 		  /* The rest are fake sections that shouldn't be written.  */
   5931 		  sec->filepos = 0;
   5932 		  sec->size = 0;
   5933 		  sec->flags = 0;
   5934 		  continue;
   5935 		}
   5936 	    }
   5937 	  else
   5938 	    {
   5939 	      if (p->p_type == PT_LOAD)
   5940 		{
   5941 		  this_hdr->sh_offset = sec->filepos = off;
   5942 		  if (this_hdr->sh_type != SHT_NOBITS)
   5943 		    off += this_hdr->sh_size;
   5944 		}
   5945 	      else if (this_hdr->sh_type == SHT_NOBITS
   5946 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   5947 		       && this_hdr->sh_offset == 0)
   5948 		{
   5949 		  /* This is a .tbss section that didn't get a PT_LOAD.
   5950 		     (See _bfd_elf_map_sections_to_segments "Create a
   5951 		     final PT_LOAD".)  Set sh_offset to the value it
   5952 		     would have if we had created a zero p_filesz and
   5953 		     p_memsz PT_LOAD header for the section.  This
   5954 		     also makes the PT_TLS header have the same
   5955 		     p_offset value.  */
   5956 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   5957 							  off, align);
   5958 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   5959 		}
   5960 
   5961 	      if (this_hdr->sh_type != SHT_NOBITS)
   5962 		{
   5963 		  p->p_filesz += this_hdr->sh_size;
   5964 		  /* A load section without SHF_ALLOC is something like
   5965 		     a note section in a PT_NOTE segment.  These take
   5966 		     file space but are not loaded into memory.  */
   5967 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5968 		    p->p_memsz += this_hdr->sh_size;
   5969 		}
   5970 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5971 		{
   5972 		  if (p->p_type == PT_TLS)
   5973 		    p->p_memsz += this_hdr->sh_size;
   5974 
   5975 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   5976 		     normal segments.  */
   5977 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   5978 		    p->p_memsz += this_hdr->sh_size;
   5979 		}
   5980 
   5981 	      if (align > p->p_align
   5982 		  && !m->p_align_valid
   5983 		  && (p->p_type != PT_LOAD
   5984 		      || (abfd->flags & D_PAGED) == 0))
   5985 		p->p_align = align;
   5986 	    }
   5987 
   5988 	  if (!m->p_flags_valid)
   5989 	    {
   5990 	      p->p_flags |= PF_R;
   5991 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   5992 		p->p_flags |= PF_X;
   5993 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   5994 		p->p_flags |= PF_W;
   5995 	    }
   5996 	}
   5997 
   5998       off -= off_adjust;
   5999 
   6000       /* PR ld/20815 - Check that the program header segment, if
   6001 	 present, will be loaded into memory.  */
   6002       if (p->p_type == PT_PHDR
   6003 	  && phdr_load_seg == NULL
   6004 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   6005 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   6006 	{
   6007 	  /* The fix for this error is usually to edit the linker script being
   6008 	     used and set up the program headers manually.  Either that or
   6009 	     leave room for the headers at the start of the SECTIONS.  */
   6010 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   6011 				" by LOAD segment"),
   6012 			      abfd);
   6013 	  if (link_info == NULL)
   6014 	    return false;
   6015 	  /* Arrange for the linker to exit with an error, deleting
   6016 	     the output file unless --noinhibit-exec is given.  */
   6017 	  link_info->callbacks->info ("%X");
   6018 	}
   6019 
   6020       /* Check that all sections are in a PT_LOAD segment.
   6021 	 Don't check funky gdb generated core files.  */
   6022       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6023 	{
   6024 	  bool check_vma = true;
   6025 
   6026 	  for (i = 1; i < m->count; i++)
   6027 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6028 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6029 				       ->this_hdr), p) != 0
   6030 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6031 				       ->this_hdr), p) != 0)
   6032 	      {
   6033 		/* Looks like we have overlays packed into the segment.  */
   6034 		check_vma = false;
   6035 		break;
   6036 	      }
   6037 
   6038 	  for (i = 0; i < m->count; i++)
   6039 	    {
   6040 	      Elf_Internal_Shdr *this_hdr;
   6041 	      asection *sec;
   6042 
   6043 	      sec = m->sections[i];
   6044 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6045 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6046 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6047 		{
   6048 		  _bfd_error_handler
   6049 		    /* xgettext:c-format */
   6050 		    (_("%pB: section `%pA' can't be allocated in segment %d"),
   6051 		     abfd, sec, j);
   6052 		  print_segment_map (m);
   6053 		}
   6054 	    }
   6055 
   6056 	  if (p_align_p)
   6057 	    p->p_align = p_align;
   6058 	}
   6059     }
   6060 
   6061   elf_next_file_pos (abfd) = off;
   6062 
   6063   if (link_info != NULL
   6064       && phdr_load_seg != NULL
   6065       && phdr_load_seg->includes_filehdr)
   6066     {
   6067       /* There is a segment that contains both the file headers and the
   6068 	 program headers, so provide a symbol __ehdr_start pointing there.
   6069 	 A program can use this to examine itself robustly.  */
   6070 
   6071       struct elf_link_hash_entry *hash
   6072 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   6073 				false, false, true);
   6074       /* If the symbol was referenced and not defined, define it.  */
   6075       if (hash != NULL
   6076 	  && (hash->root.type == bfd_link_hash_new
   6077 	      || hash->root.type == bfd_link_hash_undefined
   6078 	      || hash->root.type == bfd_link_hash_undefweak
   6079 	      || hash->root.type == bfd_link_hash_common))
   6080 	{
   6081 	  asection *s = NULL;
   6082 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6083 
   6084 	  if (phdr_load_seg->count != 0)
   6085 	    /* The segment contains sections, so use the first one.  */
   6086 	    s = phdr_load_seg->sections[0];
   6087 	  else
   6088 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6089 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6090 	      if (m->p_type == PT_LOAD && m->count != 0)
   6091 		{
   6092 		  s = m->sections[0];
   6093 		  break;
   6094 		}
   6095 
   6096 	  if (s != NULL)
   6097 	    {
   6098 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6099 	      hash->root.u.def.section = s;
   6100 	    }
   6101 	  else
   6102 	    {
   6103 	      hash->root.u.def.value = filehdr_vaddr;
   6104 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6105 	    }
   6106 
   6107 	  hash->root.type = bfd_link_hash_defined;
   6108 	  hash->def_regular = 1;
   6109 	  hash->non_elf = 0;
   6110 	}
   6111     }
   6112 
   6113   return true;
   6114 }
   6115 
   6116 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6117    is no defined method for detecting such files, so we have to
   6118    use heuristics instead.  */
   6119 
   6120 bool
   6121 is_debuginfo_file (bfd *abfd)
   6122 {
   6123   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6124     return false;
   6125 
   6126   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6127   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6128   Elf_Internal_Shdr **headerp;
   6129 
   6130   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6131     {
   6132       Elf_Internal_Shdr *header = * headerp;
   6133 
   6134       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6135 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6136       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6137 	  && header->sh_type != SHT_NOBITS
   6138 	  && header->sh_type != SHT_NOTE)
   6139 	return false;
   6140     }
   6141 
   6142   return true;
   6143 }
   6144 
   6145 /* Assign file positions for the other sections, except for compressed debugging
   6146    and other sections assigned in _bfd_elf_assign_file_positions_for_non_load().  */
   6147 
   6148 static bool
   6149 assign_file_positions_for_non_load_sections (bfd *abfd,
   6150 					     struct bfd_link_info *link_info)
   6151 {
   6152   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6153   Elf_Internal_Shdr **i_shdrpp;
   6154   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6155   Elf_Internal_Phdr *phdrs;
   6156   Elf_Internal_Phdr *p;
   6157   struct elf_segment_map *m;
   6158   file_ptr off;
   6159   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6160   bfd_vma maxpagesize;
   6161 
   6162   if (link_info != NULL)
   6163     maxpagesize = link_info->maxpagesize;
   6164   else
   6165     maxpagesize = bed->maxpagesize;
   6166   i_shdrpp = elf_elfsections (abfd);
   6167   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6168   off = elf_next_file_pos (abfd);
   6169   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6170     {
   6171       Elf_Internal_Shdr *hdr;
   6172       bfd_vma align;
   6173 
   6174       hdr = *hdrpp;
   6175       if (hdr->bfd_section != NULL
   6176 	  && (hdr->bfd_section->filepos != 0
   6177 	      || (hdr->sh_type == SHT_NOBITS
   6178 		  && hdr->contents == NULL)))
   6179 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6180       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6181 	{
   6182 	  if (hdr->sh_size != 0
   6183 	      /* PR 24717 - debuginfo files are known to be not strictly
   6184 		 compliant with the ELF standard.  In particular they often
   6185 		 have .note.gnu.property sections that are outside of any
   6186 		 loadable segment.  This is not a problem for such files,
   6187 		 so do not warn about them.  */
   6188 	      && ! is_debuginfo_file (abfd))
   6189 	    _bfd_error_handler
   6190 	      /* xgettext:c-format */
   6191 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6192 	       abfd,
   6193 	       (hdr->bfd_section == NULL
   6194 		? "*unknown*"
   6195 		: hdr->bfd_section->name));
   6196 	  /* We don't need to page align empty sections.  */
   6197 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6198 	    align = maxpagesize;
   6199 	  else
   6200 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6201 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6202 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   6203 							   false);
   6204 	}
   6205       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6206 		&& hdr->bfd_section == NULL)
   6207 	       /* We don't know the offset of these sections yet: their size has
   6208 		  not been decided.  */
   6209 	       || (hdr->bfd_section != NULL
   6210 		   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
   6211 		       || (bfd_section_is_ctf (hdr->bfd_section)
   6212 			   && abfd->is_linker_output)))
   6213 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6214 	       || (elf_symtab_shndx_list (abfd) != NULL
   6215 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6216 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6217 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6218 	hdr->sh_offset = -1;
   6219       else
   6220 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6221     }
   6222   elf_next_file_pos (abfd) = off;
   6223 
   6224   /* Now that we have set the section file positions, we can set up
   6225      the file positions for the non PT_LOAD segments.  */
   6226   phdrs = elf_tdata (abfd)->phdr;
   6227   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6228     {
   6229       if (p->p_type == PT_GNU_RELRO)
   6230 	{
   6231 	  bfd_vma start, end;  /* Bytes.  */
   6232 	  bool ok;
   6233 
   6234 	  if (link_info != NULL)
   6235 	    {
   6236 	      /* During linking the range of the RELRO segment is passed
   6237 		 in link_info.  Note that there may be padding between
   6238 		 relro_start and the first RELRO section.  */
   6239 	      start = link_info->relro_start;
   6240 	      end = link_info->relro_end;
   6241 	    }
   6242 	  else if (m->count != 0)
   6243 	    {
   6244 	      if (!m->p_size_valid)
   6245 		abort ();
   6246 	      start = m->sections[0]->vma;
   6247 	      end = start + m->p_size / opb;
   6248 	    }
   6249 	  else
   6250 	    {
   6251 	      start = 0;
   6252 	      end = 0;
   6253 	    }
   6254 
   6255 	  ok = false;
   6256 	  if (start < end)
   6257 	    {
   6258 	      struct elf_segment_map *lm;
   6259 	      const Elf_Internal_Phdr *lp;
   6260 	      unsigned int i;
   6261 
   6262 	      /* Find a LOAD segment containing a section in the RELRO
   6263 		 segment.  */
   6264 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6265 		   lm != NULL;
   6266 		   lm = lm->next, lp++)
   6267 		{
   6268 		  if (lp->p_type == PT_LOAD
   6269 		      && lm->count != 0
   6270 		      && (lm->sections[lm->count - 1]->vma
   6271 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6272 			     ? lm->sections[lm->count - 1]->size / opb
   6273 			     : 0)) > start
   6274 		      && lm->sections[0]->vma < end)
   6275 		    break;
   6276 		}
   6277 
   6278 	      if (lm != NULL)
   6279 		{
   6280 		  /* Find the section starting the RELRO segment.  */
   6281 		  for (i = 0; i < lm->count; i++)
   6282 		    {
   6283 		      asection *s = lm->sections[i];
   6284 		      if (s->vma >= start
   6285 			  && s->vma < end
   6286 			  && s->size != 0)
   6287 			break;
   6288 		    }
   6289 
   6290 		  if (i < lm->count)
   6291 		    {
   6292 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6293 		      p->p_paddr = lm->sections[i]->lma * opb;
   6294 		      p->p_offset = lm->sections[i]->filepos;
   6295 		      p->p_memsz = end * opb - p->p_vaddr;
   6296 		      p->p_filesz = p->p_memsz;
   6297 
   6298 		      /* The RELRO segment typically ends a few bytes
   6299 			 into .got.plt but other layouts are possible.
   6300 			 In cases where the end does not match any
   6301 			 loaded section (for instance is in file
   6302 			 padding), trim p_filesz back to correspond to
   6303 			 the end of loaded section contents.  */
   6304 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6305 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6306 
   6307 		      /* Preserve the alignment and flags if they are
   6308 			 valid.  The gold linker generates RW/4 for
   6309 			 the PT_GNU_RELRO section.  It is better for
   6310 			 objcopy/strip to honor these attributes
   6311 			 otherwise gdb will choke when using separate
   6312 			 debug files.  */
   6313 		      if (!m->p_align_valid)
   6314 			p->p_align = 1;
   6315 		      if (!m->p_flags_valid)
   6316 			p->p_flags = PF_R;
   6317 		      ok = true;
   6318 		    }
   6319 		}
   6320 	    }
   6321 
   6322 	  if (!ok)
   6323 	    {
   6324 	      if (link_info != NULL)
   6325 		_bfd_error_handler
   6326 		  (_("%pB: warning: unable to allocate any sections to PT_GNU_RELRO segment"),
   6327 		   abfd);
   6328 	      memset (p, 0, sizeof *p);
   6329 	    }
   6330 	}
   6331       else if (p->p_type == PT_GNU_STACK)
   6332 	{
   6333 	  if (m->p_size_valid)
   6334 	    p->p_memsz = m->p_size;
   6335 	}
   6336       else if (m->count != 0)
   6337 	{
   6338 	  unsigned int i;
   6339 
   6340 	  if (p->p_type != PT_LOAD
   6341 	      && (p->p_type != PT_NOTE
   6342 		  || bfd_get_format (abfd) != bfd_core))
   6343 	    {
   6344 	      /* A user specified segment layout may include a PHDR
   6345 		 segment that overlaps with a LOAD segment...  */
   6346 	      if (p->p_type == PT_PHDR)
   6347 		{
   6348 		  m->count = 0;
   6349 		  continue;
   6350 		}
   6351 
   6352 	      if (m->includes_filehdr || m->includes_phdrs)
   6353 		{
   6354 		  /* PR 17512: file: 2195325e.  */
   6355 		  _bfd_error_handler
   6356 		    (_("%pB: error: non-load segment %d includes file header "
   6357 		       "and/or program header"),
   6358 		     abfd, (int) (p - phdrs));
   6359 		  return false;
   6360 		}
   6361 
   6362 	      p->p_filesz = 0;
   6363 	      p->p_offset = m->sections[0]->filepos;
   6364 	      for (i = m->count; i-- != 0;)
   6365 		{
   6366 		  asection *sect = m->sections[i];
   6367 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6368 		  if (hdr->sh_type != SHT_NOBITS)
   6369 		    {
   6370 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6371 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6372 			 should be the same as p_filesz.  */
   6373 		      if (p->p_type == PT_NOTE
   6374 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6375 			p->p_memsz = p->p_filesz;
   6376 		      break;
   6377 		    }
   6378 		}
   6379 	    }
   6380 	}
   6381     }
   6382 
   6383   return true;
   6384 }
   6385 
   6386 static elf_section_list *
   6387 find_section_in_list (unsigned int i, elf_section_list * list)
   6388 {
   6389   for (;list != NULL; list = list->next)
   6390     if (list->ndx == i)
   6391       break;
   6392   return list;
   6393 }
   6394 
   6395 /* Work out the file positions of all the sections.  This is called by
   6396    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6397    VMAs must be known before this is called.
   6398 
   6399    Reloc sections come in two flavours: Those processed specially as
   6400    "side-channel" data attached to a section to which they apply, and those that
   6401    bfd doesn't process as relocations.  The latter sort are stored in a normal
   6402    bfd section by bfd_section_from_shdr.  We don't consider the former sort
   6403    here, unless they form part of the loadable image.  Reloc sections not
   6404    assigned here (and compressed debugging sections and CTF sections which
   6405    nothing else in the file can rely upon) will be handled later by
   6406    assign_file_positions_for_relocs.
   6407 
   6408    We also don't set the positions of the .symtab and .strtab here.  */
   6409 
   6410 static bool
   6411 assign_file_positions_except_relocs (bfd *abfd,
   6412 				     struct bfd_link_info *link_info)
   6413 {
   6414   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6415   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6416   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6417   unsigned int alloc;
   6418 
   6419   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6420       && bfd_get_format (abfd) != bfd_core)
   6421     {
   6422       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6423       unsigned int num_sec = elf_numsections (abfd);
   6424       Elf_Internal_Shdr **hdrpp;
   6425       unsigned int i;
   6426       file_ptr off;
   6427 
   6428       /* Start after the ELF header.  */
   6429       off = i_ehdrp->e_ehsize;
   6430 
   6431       /* We are not creating an executable, which means that we are
   6432 	 not creating a program header, and that the actual order of
   6433 	 the sections in the file is unimportant.  */
   6434       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6435 	{
   6436 	  Elf_Internal_Shdr *hdr;
   6437 
   6438 	  hdr = *hdrpp;
   6439 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6440 	       && hdr->bfd_section == NULL)
   6441 	      /* Do not assign offsets for these sections yet: we don't know
   6442 		 their sizes.  */
   6443 	      || (hdr->bfd_section != NULL
   6444 		  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
   6445 		      || (bfd_section_is_ctf (hdr->bfd_section)
   6446 			  && abfd->is_linker_output)))
   6447 	      || i == elf_onesymtab (abfd)
   6448 	      || (elf_symtab_shndx_list (abfd) != NULL
   6449 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6450 	      || i == elf_strtab_sec (abfd)
   6451 	      || i == elf_shstrtab_sec (abfd))
   6452 	    {
   6453 	      hdr->sh_offset = -1;
   6454 	    }
   6455 	  else
   6456 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6457 	}
   6458 
   6459       elf_next_file_pos (abfd) = off;
   6460       elf_program_header_size (abfd) = 0;
   6461     }
   6462   else
   6463     {
   6464       /* Assign file positions for the loaded sections based on the
   6465 	 assignment of sections to segments.  */
   6466       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6467 	return false;
   6468 
   6469       /* And for non-load sections.  */
   6470       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6471 	return false;
   6472     }
   6473 
   6474   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   6475     return false;
   6476 
   6477   /* Write out the program headers.  */
   6478   alloc = i_ehdrp->e_phnum;
   6479   if (alloc != 0)
   6480     {
   6481       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   6482 	{
   6483 	  /* Memory resident segments with non-zero size and RWX permissions are a
   6484 	     security risk, so we generate a warning here if we are creating any.  */
   6485 	  unsigned int i;
   6486 
   6487 	  for (i = 0; i < alloc; i++)
   6488 	    {
   6489 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   6490 
   6491 	      if (phdr->p_memsz == 0)
   6492 		continue;
   6493 
   6494 	      if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
   6495 		_bfd_error_handler (_("warning: %pB has a TLS segment with execute permission"),
   6496 				    abfd);
   6497 	      else if (phdr->p_type == PT_LOAD
   6498 		       && (phdr->p_flags & (PF_R | PF_W | PF_X)) == (PF_R | PF_W | PF_X))
   6499 		_bfd_error_handler (_("warning: %pB has a LOAD segment with RWX permissions"),
   6500 				    abfd);
   6501 	    }
   6502 	}
   6503 
   6504       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   6505 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   6506 	return false;
   6507     }
   6508 
   6509   return true;
   6510 }
   6511 
   6512 bool
   6513 _bfd_elf_init_file_header (bfd *abfd,
   6514 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   6515 {
   6516   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   6517   struct elf_strtab_hash *shstrtab;
   6518   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6519 
   6520   i_ehdrp = elf_elfheader (abfd);
   6521 
   6522   shstrtab = _bfd_elf_strtab_init ();
   6523   if (shstrtab == NULL)
   6524     return false;
   6525 
   6526   elf_shstrtab (abfd) = shstrtab;
   6527 
   6528   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   6529   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   6530   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   6531   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   6532 
   6533   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   6534   i_ehdrp->e_ident[EI_DATA] =
   6535     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   6536   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   6537 
   6538   if ((abfd->flags & DYNAMIC) != 0)
   6539     i_ehdrp->e_type = ET_DYN;
   6540   else if ((abfd->flags & EXEC_P) != 0)
   6541     i_ehdrp->e_type = ET_EXEC;
   6542   else if (bfd_get_format (abfd) == bfd_core)
   6543     i_ehdrp->e_type = ET_CORE;
   6544   else
   6545     i_ehdrp->e_type = ET_REL;
   6546 
   6547   switch (bfd_get_arch (abfd))
   6548     {
   6549     case bfd_arch_unknown:
   6550       i_ehdrp->e_machine = EM_NONE;
   6551       break;
   6552 
   6553       /* There used to be a long list of cases here, each one setting
   6554 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   6555 	 in the corresponding bfd definition.  To avoid duplication,
   6556 	 the switch was removed.  Machines that need special handling
   6557 	 can generally do it in elf_backend_final_write_processing(),
   6558 	 unless they need the information earlier than the final write.
   6559 	 Such need can generally be supplied by replacing the tests for
   6560 	 e_machine with the conditions used to determine it.  */
   6561     default:
   6562       i_ehdrp->e_machine = bed->elf_machine_code;
   6563     }
   6564 
   6565   i_ehdrp->e_version = bed->s->ev_current;
   6566   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   6567 
   6568   /* No program header, for now.  */
   6569   i_ehdrp->e_phoff = 0;
   6570   i_ehdrp->e_phentsize = 0;
   6571   i_ehdrp->e_phnum = 0;
   6572 
   6573   /* Each bfd section is section header entry.  */
   6574   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   6575   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   6576 
   6577   elf_tdata (abfd)->symtab_hdr.sh_name =
   6578     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   6579   elf_tdata (abfd)->strtab_hdr.sh_name =
   6580     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   6581   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   6582     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   6583   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   6584       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   6585       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   6586     return false;
   6587 
   6588   return true;
   6589 }
   6590 
   6591 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   6592 
   6593    FIXME: We used to have code here to sort the PT_LOAD segments into
   6594    ascending order, as per the ELF spec.  But this breaks some programs,
   6595    including the Linux kernel.  But really either the spec should be
   6596    changed or the programs updated.  */
   6597 
   6598 bool
   6599 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   6600 {
   6601   if (link_info != NULL && bfd_link_pie (link_info))
   6602     {
   6603       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   6604       unsigned int num_segments = i_ehdrp->e_phnum;
   6605       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   6606       Elf_Internal_Phdr *segment = tdata->phdr;
   6607       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   6608 
   6609       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   6610       bfd_vma p_vaddr = (bfd_vma) -1;
   6611       for (; segment < end_segment; segment++)
   6612 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   6613 	  p_vaddr = segment->p_vaddr;
   6614 
   6615       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   6616 	 segments is non-zero.  */
   6617       if (p_vaddr)
   6618 	i_ehdrp->e_type = ET_EXEC;
   6619     }
   6620   return true;
   6621 }
   6622 
   6623 /* Assign file positions for all the reloc sections which are not part
   6624    of the loadable file image, and the file position of section headers.  */
   6625 
   6626 static bool
   6627 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   6628 {
   6629   file_ptr off;
   6630   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   6631   Elf_Internal_Shdr *shdrp;
   6632   Elf_Internal_Ehdr *i_ehdrp;
   6633   const struct elf_backend_data *bed;
   6634 
   6635   off = elf_next_file_pos (abfd);
   6636 
   6637   shdrpp = elf_elfsections (abfd);
   6638   end_shdrpp = shdrpp + elf_numsections (abfd);
   6639   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   6640     {
   6641       shdrp = *shdrpp;
   6642       if (shdrp->sh_offset == -1)
   6643 	{
   6644 	  asection *sec = shdrp->bfd_section;
   6645 	  bool is_rel = (shdrp->sh_type == SHT_REL
   6646 			 || shdrp->sh_type == SHT_RELA);
   6647 	  bool is_ctf = sec && bfd_section_is_ctf (sec);
   6648 	  if (is_rel
   6649 	      || is_ctf
   6650 	      || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
   6651 	    {
   6652 	      if (!is_rel && !is_ctf)
   6653 		{
   6654 		  const char *name = sec->name;
   6655 		  struct bfd_elf_section_data *d;
   6656 
   6657 		  /* Compress DWARF debug sections.  */
   6658 		  if (!bfd_compress_section (abfd, sec,
   6659 					     shdrp->contents))
   6660 		    return false;
   6661 
   6662 		  if (sec->compress_status == COMPRESS_SECTION_DONE
   6663 		      && (abfd->flags & BFD_COMPRESS_GABI) == 0)
   6664 		    {
   6665 		      /* If section is compressed with zlib-gnu, convert
   6666 			 section name from .debug_* to .zdebug_*.  */
   6667 		      char *new_name
   6668 			= convert_debug_to_zdebug (abfd, name);
   6669 		      if (new_name == NULL)
   6670 			return false;
   6671 		      name = new_name;
   6672 		    }
   6673 		  /* Add section name to section name section.  */
   6674 		  if (shdrp->sh_name != (unsigned int) -1)
   6675 		    abort ();
   6676 		  shdrp->sh_name
   6677 		    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   6678 							  name, false);
   6679 		  d = elf_section_data (sec);
   6680 
   6681 		  /* Add reloc section name to section name section.  */
   6682 		  if (d->rel.hdr
   6683 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6684 						      d->rel.hdr,
   6685 						      name, false))
   6686 		    return false;
   6687 		  if (d->rela.hdr
   6688 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6689 						      d->rela.hdr,
   6690 						      name, true))
   6691 		    return false;
   6692 
   6693 		  /* Update section size and contents.  */
   6694 		  shdrp->sh_size = sec->size;
   6695 		  shdrp->contents = sec->contents;
   6696 		  shdrp->bfd_section->contents = NULL;
   6697 		}
   6698 	      else if (is_ctf)
   6699 		{
   6700 		  /* Update section size and contents.	*/
   6701 		  shdrp->sh_size = sec->size;
   6702 		  shdrp->contents = sec->contents;
   6703 		}
   6704 
   6705 	      off = _bfd_elf_assign_file_position_for_section (shdrp,
   6706 							       off,
   6707 							       true);
   6708 	    }
   6709 	}
   6710     }
   6711 
   6712   /* Place section name section after DWARF debug sections have been
   6713      compressed.  */
   6714   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   6715   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   6716   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   6717   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   6718 
   6719   /* Place the section headers.  */
   6720   i_ehdrp = elf_elfheader (abfd);
   6721   bed = get_elf_backend_data (abfd);
   6722   off = align_file_position (off, 1 << bed->s->log_file_align);
   6723   i_ehdrp->e_shoff = off;
   6724   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   6725   elf_next_file_pos (abfd) = off;
   6726 
   6727   return true;
   6728 }
   6729 
   6730 bool
   6731 _bfd_elf_write_object_contents (bfd *abfd)
   6732 {
   6733   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6734   Elf_Internal_Shdr **i_shdrp;
   6735   bool failed;
   6736   unsigned int count, num_sec;
   6737   struct elf_obj_tdata *t;
   6738 
   6739   if (! abfd->output_has_begun
   6740       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   6741     return false;
   6742   /* Do not rewrite ELF data when the BFD has been opened for update.
   6743      abfd->output_has_begun was set to TRUE on opening, so creation of new
   6744      sections, and modification of existing section sizes was restricted.
   6745      This means the ELF header, program headers and section headers can't have
   6746      changed.
   6747      If the contents of any sections has been modified, then those changes have
   6748      already been written to the BFD.  */
   6749   else if (abfd->direction == both_direction)
   6750     {
   6751       BFD_ASSERT (abfd->output_has_begun);
   6752       return true;
   6753     }
   6754 
   6755   i_shdrp = elf_elfsections (abfd);
   6756 
   6757   failed = false;
   6758   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   6759   if (failed)
   6760     return false;
   6761 
   6762   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   6763     return false;
   6764 
   6765   /* After writing the headers, we need to write the sections too...  */
   6766   num_sec = elf_numsections (abfd);
   6767   for (count = 1; count < num_sec; count++)
   6768     {
   6769       i_shdrp[count]->sh_name
   6770 	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   6771 				  i_shdrp[count]->sh_name);
   6772       if (bed->elf_backend_section_processing)
   6773 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   6774 	  return false;
   6775       if (i_shdrp[count]->contents)
   6776 	{
   6777 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   6778 
   6779 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   6780 	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
   6781 	    return false;
   6782 	}
   6783     }
   6784 
   6785   /* Write out the section header names.  */
   6786   t = elf_tdata (abfd);
   6787   if (elf_shstrtab (abfd) != NULL
   6788       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   6789 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   6790     return false;
   6791 
   6792   if (!(*bed->elf_backend_final_write_processing) (abfd))
   6793     return false;
   6794 
   6795   if (!bed->s->write_shdrs_and_ehdr (abfd))
   6796     return false;
   6797 
   6798   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   6799   if (t->o->build_id.after_write_object_contents != NULL
   6800       && !(*t->o->build_id.after_write_object_contents) (abfd))
   6801     return false;
   6802   if (t->o->package_metadata.after_write_object_contents != NULL
   6803       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   6804     return false;
   6805 
   6806   return true;
   6807 }
   6808 
   6809 bool
   6810 _bfd_elf_write_corefile_contents (bfd *abfd)
   6811 {
   6812   /* Hopefully this can be done just like an object file.  */
   6813   return _bfd_elf_write_object_contents (abfd);
   6814 }
   6815 
   6816 /* Given a section, search the header to find them.  */
   6817 
   6818 unsigned int
   6819 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   6820 {
   6821   const struct elf_backend_data *bed;
   6822   unsigned int sec_index;
   6823 
   6824   if (elf_section_data (asect) != NULL
   6825       && elf_section_data (asect)->this_idx != 0)
   6826     return elf_section_data (asect)->this_idx;
   6827 
   6828   if (bfd_is_abs_section (asect))
   6829     sec_index = SHN_ABS;
   6830   else if (bfd_is_com_section (asect))
   6831     sec_index = SHN_COMMON;
   6832   else if (bfd_is_und_section (asect))
   6833     sec_index = SHN_UNDEF;
   6834   else
   6835     sec_index = SHN_BAD;
   6836 
   6837   bed = get_elf_backend_data (abfd);
   6838   if (bed->elf_backend_section_from_bfd_section)
   6839     {
   6840       int retval = sec_index;
   6841 
   6842       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   6843 	return retval;
   6844     }
   6845 
   6846   if (sec_index == SHN_BAD)
   6847     bfd_set_error (bfd_error_nonrepresentable_section);
   6848 
   6849   return sec_index;
   6850 }
   6851 
   6852 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   6853    on error.  */
   6854 
   6855 int
   6856 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   6857 {
   6858   asymbol *asym_ptr = *asym_ptr_ptr;
   6859   int idx;
   6860   flagword flags = asym_ptr->flags;
   6861 
   6862   /* When gas creates relocations against local labels, it creates its
   6863      own symbol for the section, but does put the symbol into the
   6864      symbol chain, so udata is 0.  When the linker is generating
   6865      relocatable output, this section symbol may be for one of the
   6866      input sections rather than the output section.  */
   6867   if (asym_ptr->udata.i == 0
   6868       && (flags & BSF_SECTION_SYM)
   6869       && asym_ptr->section)
   6870     {
   6871       asection *sec;
   6872 
   6873       sec = asym_ptr->section;
   6874       if (sec->owner != abfd && sec->output_section != NULL)
   6875 	sec = sec->output_section;
   6876       if (sec->owner == abfd
   6877 	  && sec->index < elf_num_section_syms (abfd)
   6878 	  && elf_section_syms (abfd)[sec->index] != NULL)
   6879 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   6880     }
   6881 
   6882   idx = asym_ptr->udata.i;
   6883 
   6884   if (idx == 0)
   6885     {
   6886       /* This case can occur when using --strip-symbol on a symbol
   6887 	 which is used in a relocation entry.  */
   6888       _bfd_error_handler
   6889 	/* xgettext:c-format */
   6890 	(_("%pB: symbol `%s' required but not present"),
   6891 	 abfd, bfd_asymbol_name (asym_ptr));
   6892       bfd_set_error (bfd_error_no_symbols);
   6893       return -1;
   6894     }
   6895 
   6896 #if DEBUG & 4
   6897   {
   6898     fprintf (stderr,
   6899 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
   6900 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   6901     fflush (stderr);
   6902   }
   6903 #endif
   6904 
   6905   return idx;
   6906 }
   6907 
   6908 /* Rewrite program header information.  */
   6909 
   6910 static bool
   6911 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   6912 {
   6913   Elf_Internal_Ehdr *iehdr;
   6914   struct elf_segment_map *map;
   6915   struct elf_segment_map *map_first;
   6916   struct elf_segment_map **pointer_to_map;
   6917   Elf_Internal_Phdr *segment;
   6918   asection *section;
   6919   unsigned int i;
   6920   unsigned int num_segments;
   6921   bool phdr_included = false;
   6922   bool p_paddr_valid;
   6923   struct elf_segment_map *phdr_adjust_seg = NULL;
   6924   unsigned int phdr_adjust_num = 0;
   6925   const struct elf_backend_data *bed;
   6926   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   6927 
   6928   bed = get_elf_backend_data (ibfd);
   6929   iehdr = elf_elfheader (ibfd);
   6930 
   6931   map_first = NULL;
   6932   pointer_to_map = &map_first;
   6933 
   6934   num_segments = elf_elfheader (ibfd)->e_phnum;
   6935 
   6936   /* Returns the end address of the segment + 1.  */
   6937 #define SEGMENT_END(segment, start)					\
   6938   (start + (segment->p_memsz > segment->p_filesz			\
   6939 	    ? segment->p_memsz : segment->p_filesz))
   6940 
   6941 #define SECTION_SIZE(section, segment)					\
   6942   (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))		\
   6943     != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)			\
   6944    ? section->size : 0)
   6945 
   6946   /* Returns TRUE if the given section is contained within
   6947      the given segment.  VMA addresses are compared.  */
   6948 #define IS_CONTAINED_BY_VMA(section, segment, opb)			\
   6949   (section->vma * (opb) >= segment->p_vaddr				\
   6950    && (section->vma * (opb) + SECTION_SIZE (section, segment)		\
   6951        <= (SEGMENT_END (segment, segment->p_vaddr))))
   6952 
   6953   /* Returns TRUE if the given section is contained within
   6954      the given segment.  LMA addresses are compared.  */
   6955 #define IS_CONTAINED_BY_LMA(section, segment, base, opb)		\
   6956   (section->lma * (opb) >= base						\
   6957    && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
   6958    && (section->lma * (opb) + SECTION_SIZE (section, segment)		\
   6959        <= SEGMENT_END (segment, base)))
   6960 
   6961   /* Handle PT_NOTE segment.  */
   6962 #define IS_NOTE(p, s)							\
   6963   (p->p_type == PT_NOTE							\
   6964    && elf_section_type (s) == SHT_NOTE					\
   6965    && (bfd_vma) s->filepos >= p->p_offset				\
   6966    && ((bfd_vma) s->filepos + s->size					\
   6967        <= p->p_offset + p->p_filesz))
   6968 
   6969   /* Special case: corefile "NOTE" section containing regs, prpsinfo
   6970      etc.  */
   6971 #define IS_COREFILE_NOTE(p, s)						\
   6972   (IS_NOTE (p, s)							\
   6973    && bfd_get_format (ibfd) == bfd_core					\
   6974    && s->vma == 0							\
   6975    && s->lma == 0)
   6976 
   6977   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   6978      linker, which generates a PT_INTERP section with p_vaddr and
   6979      p_memsz set to 0.  */
   6980 #define IS_SOLARIS_PT_INTERP(p, s)					\
   6981   (p->p_vaddr == 0							\
   6982    && p->p_paddr == 0							\
   6983    && p->p_memsz == 0							\
   6984    && p->p_filesz > 0							\
   6985    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   6986    && s->size > 0							\
   6987    && (bfd_vma) s->filepos >= p->p_offset				\
   6988    && ((bfd_vma) s->filepos + s->size					\
   6989        <= p->p_offset + p->p_filesz))
   6990 
   6991   /* Decide if the given section should be included in the given segment.
   6992      A section will be included if:
   6993        1. It is within the address space of the segment -- we use the LMA
   6994 	  if that is set for the segment and the VMA otherwise,
   6995        2. It is an allocated section or a NOTE section in a PT_NOTE
   6996 	  segment.
   6997        3. There is an output section associated with it,
   6998        4. The section has not already been allocated to a previous segment.
   6999        5. PT_GNU_STACK segments do not include any sections.
   7000        6. PT_TLS segment includes only SHF_TLS sections.
   7001        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   7002        8. PT_DYNAMIC should not contain empty sections at the beginning
   7003 	  (with the possible exception of .dynamic).  */
   7004 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb)		\
   7005   ((((segment->p_paddr							\
   7006       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb)	\
   7007       : IS_CONTAINED_BY_VMA (section, segment, opb))			\
   7008      && (section->flags & SEC_ALLOC) != 0)				\
   7009     || IS_NOTE (segment, section))					\
   7010    && segment->p_type != PT_GNU_STACK					\
   7011    && (segment->p_type != PT_TLS					\
   7012        || (section->flags & SEC_THREAD_LOCAL))				\
   7013    && (segment->p_type == PT_LOAD					\
   7014        || segment->p_type == PT_TLS					\
   7015        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7016    && (segment->p_type != PT_DYNAMIC					\
   7017        || SECTION_SIZE (section, segment) > 0				\
   7018        || (segment->p_paddr						\
   7019 	   ? segment->p_paddr != section->lma * (opb)			\
   7020 	   : segment->p_vaddr != section->vma * (opb))			\
   7021        || (strcmp (bfd_section_name (section), ".dynamic") == 0))	\
   7022    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7023 
   7024 /* If the output section of a section in the input segment is NULL,
   7025    it is removed from the corresponding output segment.   */
   7026 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb)		\
   7027   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)		\
   7028    && section->output_section != NULL)
   7029 
   7030   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7031 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7032   (seg1->field >= SEGMENT_END (seg2, seg2->field))
   7033 
   7034   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7035      their VMA address ranges and their LMA address ranges overlap.
   7036      It is possible to have overlapping VMA ranges without overlapping LMA
   7037      ranges.  RedBoot images for example can have both .data and .bss mapped
   7038      to the same VMA range, but with the .data section mapped to a different
   7039      LMA.  */
   7040 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7041   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7042 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7043    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7044 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7045 
   7046   /* Initialise the segment mark field, and discard stupid alignment.  */
   7047   for (section = ibfd->sections; section != NULL; section = section->next)
   7048     {
   7049       asection *o = section->output_section;
   7050       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7051 	o->alignment_power = 0;
   7052       section->segment_mark = false;
   7053     }
   7054 
   7055   /* The Solaris linker creates program headers in which all the
   7056      p_paddr fields are zero.  When we try to objcopy or strip such a
   7057      file, we get confused.  Check for this case, and if we find it
   7058      don't set the p_paddr_valid fields.  */
   7059   p_paddr_valid = false;
   7060   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7061        i < num_segments;
   7062        i++, segment++)
   7063     if (segment->p_paddr != 0)
   7064       {
   7065 	p_paddr_valid = true;
   7066 	break;
   7067       }
   7068 
   7069   /* Scan through the segments specified in the program header
   7070      of the input BFD.  For this first scan we look for overlaps
   7071      in the loadable segments.  These can be created by weird
   7072      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7073   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7074        i < num_segments;
   7075        i++, segment++)
   7076     {
   7077       unsigned int j;
   7078       Elf_Internal_Phdr *segment2;
   7079 
   7080       if (segment->p_type == PT_INTERP)
   7081 	for (section = ibfd->sections; section; section = section->next)
   7082 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7083 	    {
   7084 	      /* Mininal change so that the normal section to segment
   7085 		 assignment code will work.  */
   7086 	      segment->p_vaddr = section->vma * opb;
   7087 	      break;
   7088 	    }
   7089 
   7090       if (segment->p_type != PT_LOAD)
   7091 	{
   7092 	  /* Remove PT_GNU_RELRO segment.  */
   7093 	  if (segment->p_type == PT_GNU_RELRO)
   7094 	    segment->p_type = PT_NULL;
   7095 	  continue;
   7096 	}
   7097 
   7098       /* Determine if this segment overlaps any previous segments.  */
   7099       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7100 	{
   7101 	  bfd_signed_vma extra_length;
   7102 
   7103 	  if (segment2->p_type != PT_LOAD
   7104 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7105 	    continue;
   7106 
   7107 	  /* Merge the two segments together.  */
   7108 	  if (segment2->p_vaddr < segment->p_vaddr)
   7109 	    {
   7110 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7111 		 SEGMENT.  */
   7112 	      extra_length = (SEGMENT_END (segment, segment->p_vaddr)
   7113 			      - SEGMENT_END (segment2, segment2->p_vaddr));
   7114 
   7115 	      if (extra_length > 0)
   7116 		{
   7117 		  segment2->p_memsz += extra_length;
   7118 		  segment2->p_filesz += extra_length;
   7119 		}
   7120 
   7121 	      segment->p_type = PT_NULL;
   7122 
   7123 	      /* Since we have deleted P we must restart the outer loop.  */
   7124 	      i = 0;
   7125 	      segment = elf_tdata (ibfd)->phdr;
   7126 	      break;
   7127 	    }
   7128 	  else
   7129 	    {
   7130 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7131 		 SEGMENT2.  */
   7132 	      extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
   7133 			      - SEGMENT_END (segment, segment->p_vaddr));
   7134 
   7135 	      if (extra_length > 0)
   7136 		{
   7137 		  segment->p_memsz += extra_length;
   7138 		  segment->p_filesz += extra_length;
   7139 		}
   7140 
   7141 	      segment2->p_type = PT_NULL;
   7142 	    }
   7143 	}
   7144     }
   7145 
   7146   /* The second scan attempts to assign sections to segments.  */
   7147   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7148        i < num_segments;
   7149        i++, segment++)
   7150     {
   7151       unsigned int section_count;
   7152       asection **sections;
   7153       asection *output_section;
   7154       unsigned int isec;
   7155       asection *matching_lma;
   7156       asection *suggested_lma;
   7157       unsigned int j;
   7158       size_t amt;
   7159       asection *first_section;
   7160 
   7161       if (segment->p_type == PT_NULL)
   7162 	continue;
   7163 
   7164       first_section = NULL;
   7165       /* Compute how many sections might be placed into this segment.  */
   7166       for (section = ibfd->sections, section_count = 0;
   7167 	   section != NULL;
   7168 	   section = section->next)
   7169 	{
   7170 	  /* Find the first section in the input segment, which may be
   7171 	     removed from the corresponding output segment.   */
   7172 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
   7173 	    {
   7174 	      if (first_section == NULL)
   7175 		first_section = section;
   7176 	      if (section->output_section != NULL)
   7177 		++section_count;
   7178 	    }
   7179 	}
   7180 
   7181       /* Allocate a segment map big enough to contain
   7182 	 all of the sections we have selected.  */
   7183       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7184       amt += section_count * sizeof (asection *);
   7185       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7186       if (map == NULL)
   7187 	return false;
   7188 
   7189       /* Initialise the fields of the segment map.  Default to
   7190 	 using the physical address of the segment in the input BFD.  */
   7191       map->next = NULL;
   7192       map->p_type = segment->p_type;
   7193       map->p_flags = segment->p_flags;
   7194       map->p_flags_valid = 1;
   7195 
   7196       if (map->p_type == PT_LOAD
   7197 	  && (ibfd->flags & D_PAGED) != 0
   7198 	  && maxpagesize > 1
   7199 	  && segment->p_align > 1)
   7200 	{
   7201 	  map->p_align = segment->p_align;
   7202 	  if (segment->p_align > maxpagesize)
   7203 	    map->p_align = maxpagesize;
   7204 	  map->p_align_valid = 1;
   7205 	}
   7206 
   7207       /* If the first section in the input segment is removed, there is
   7208 	 no need to preserve segment physical address in the corresponding
   7209 	 output segment.  */
   7210       if (!first_section || first_section->output_section != NULL)
   7211 	{
   7212 	  map->p_paddr = segment->p_paddr;
   7213 	  map->p_paddr_valid = p_paddr_valid;
   7214 	}
   7215 
   7216       /* Determine if this segment contains the ELF file header
   7217 	 and if it contains the program headers themselves.  */
   7218       map->includes_filehdr = (segment->p_offset == 0
   7219 			       && segment->p_filesz >= iehdr->e_ehsize);
   7220       map->includes_phdrs = 0;
   7221 
   7222       if (!phdr_included || segment->p_type != PT_LOAD)
   7223 	{
   7224 	  map->includes_phdrs =
   7225 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7226 	     && (segment->p_offset + segment->p_filesz
   7227 		 >= ((bfd_vma) iehdr->e_phoff
   7228 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7229 
   7230 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7231 	    phdr_included = true;
   7232 	}
   7233 
   7234       if (section_count == 0)
   7235 	{
   7236 	  /* Special segments, such as the PT_PHDR segment, may contain
   7237 	     no sections, but ordinary, loadable segments should contain
   7238 	     something.  They are allowed by the ELF spec however, so only
   7239 	     a warning is produced.
   7240 	     There is however the valid use case of embedded systems which
   7241 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7242 	     flash memory with zeros.  No warning is shown for that case.  */
   7243 	  if (segment->p_type == PT_LOAD
   7244 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7245 	    /* xgettext:c-format */
   7246 	    _bfd_error_handler
   7247 	      (_("%pB: warning: empty loadable segment detected"
   7248 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7249 	       ibfd, (uint64_t) segment->p_vaddr);
   7250 
   7251 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7252 	  map->count = 0;
   7253 	  *pointer_to_map = map;
   7254 	  pointer_to_map = &map->next;
   7255 
   7256 	  continue;
   7257 	}
   7258 
   7259       /* Now scan the sections in the input BFD again and attempt
   7260 	 to add their corresponding output sections to the segment map.
   7261 	 The problem here is how to handle an output section which has
   7262 	 been moved (ie had its LMA changed).  There are four possibilities:
   7263 
   7264 	 1. None of the sections have been moved.
   7265 	    In this case we can continue to use the segment LMA from the
   7266 	    input BFD.
   7267 
   7268 	 2. All of the sections have been moved by the same amount.
   7269 	    In this case we can change the segment's LMA to match the LMA
   7270 	    of the first section.
   7271 
   7272 	 3. Some of the sections have been moved, others have not.
   7273 	    In this case those sections which have not been moved can be
   7274 	    placed in the current segment which will have to have its size,
   7275 	    and possibly its LMA changed, and a new segment or segments will
   7276 	    have to be created to contain the other sections.
   7277 
   7278 	 4. The sections have been moved, but not by the same amount.
   7279 	    In this case we can change the segment's LMA to match the LMA
   7280 	    of the first section and we will have to create a new segment
   7281 	    or segments to contain the other sections.
   7282 
   7283 	 In order to save time, we allocate an array to hold the section
   7284 	 pointers that we are interested in.  As these sections get assigned
   7285 	 to a segment, they are removed from this array.  */
   7286 
   7287       amt = section_count * sizeof (asection *);
   7288       sections = (asection **) bfd_malloc (amt);
   7289       if (sections == NULL)
   7290 	return false;
   7291 
   7292       /* Step One: Scan for segment vs section LMA conflicts.
   7293 	 Also add the sections to the section array allocated above.
   7294 	 Also add the sections to the current segment.  In the common
   7295 	 case, where the sections have not been moved, this means that
   7296 	 we have completely filled the segment, and there is nothing
   7297 	 more to do.  */
   7298       isec = 0;
   7299       matching_lma = NULL;
   7300       suggested_lma = NULL;
   7301 
   7302       for (section = first_section, j = 0;
   7303 	   section != NULL;
   7304 	   section = section->next)
   7305 	{
   7306 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
   7307 	    {
   7308 	      output_section = section->output_section;
   7309 
   7310 	      sections[j++] = section;
   7311 
   7312 	      /* The Solaris native linker always sets p_paddr to 0.
   7313 		 We try to catch that case here, and set it to the
   7314 		 correct value.  Note - some backends require that
   7315 		 p_paddr be left as zero.  */
   7316 	      if (!p_paddr_valid
   7317 		  && segment->p_vaddr != 0
   7318 		  && !bed->want_p_paddr_set_to_zero
   7319 		  && isec == 0
   7320 		  && output_section->lma != 0
   7321 		  && (align_power (segment->p_vaddr
   7322 				   + (map->includes_filehdr
   7323 				      ? iehdr->e_ehsize : 0)
   7324 				   + (map->includes_phdrs
   7325 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7326 				      : 0),
   7327 				   output_section->alignment_power * opb)
   7328 		      == (output_section->vma * opb)))
   7329 		map->p_paddr = segment->p_vaddr;
   7330 
   7331 	      /* Match up the physical address of the segment with the
   7332 		 LMA address of the output section.  */
   7333 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
   7334 				       opb)
   7335 		  || IS_COREFILE_NOTE (segment, section)
   7336 		  || (bed->want_p_paddr_set_to_zero
   7337 		      && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
   7338 		{
   7339 		  if (matching_lma == NULL
   7340 		      || output_section->lma < matching_lma->lma)
   7341 		    matching_lma = output_section;
   7342 
   7343 		  /* We assume that if the section fits within the segment
   7344 		     then it does not overlap any other section within that
   7345 		     segment.  */
   7346 		  map->sections[isec++] = output_section;
   7347 		}
   7348 	      else if (suggested_lma == NULL)
   7349 		suggested_lma = output_section;
   7350 
   7351 	      if (j == section_count)
   7352 		break;
   7353 	    }
   7354 	}
   7355 
   7356       BFD_ASSERT (j == section_count);
   7357 
   7358       /* Step Two: Adjust the physical address of the current segment,
   7359 	 if necessary.  */
   7360       if (isec == section_count)
   7361 	{
   7362 	  /* All of the sections fitted within the segment as currently
   7363 	     specified.  This is the default case.  Add the segment to
   7364 	     the list of built segments and carry on to process the next
   7365 	     program header in the input BFD.  */
   7366 	  map->count = section_count;
   7367 	  *pointer_to_map = map;
   7368 	  pointer_to_map = &map->next;
   7369 
   7370 	  if (p_paddr_valid
   7371 	      && !bed->want_p_paddr_set_to_zero)
   7372 	    {
   7373 	      bfd_vma hdr_size = 0;
   7374 	      if (map->includes_filehdr)
   7375 		hdr_size = iehdr->e_ehsize;
   7376 	      if (map->includes_phdrs)
   7377 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7378 
   7379 	      /* Account for padding before the first section in the
   7380 		 segment.  */
   7381 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7382 				     - matching_lma->lma);
   7383 	    }
   7384 
   7385 	  free (sections);
   7386 	  continue;
   7387 	}
   7388       else
   7389 	{
   7390 	  /* Change the current segment's physical address to match
   7391 	     the LMA of the first section that fitted, or if no
   7392 	     section fitted, the first section.  */
   7393 	  if (matching_lma == NULL)
   7394 	    matching_lma = suggested_lma;
   7395 
   7396 	  map->p_paddr = matching_lma->lma * opb;
   7397 
   7398 	  /* Offset the segment physical address from the lma
   7399 	     to allow for space taken up by elf headers.  */
   7400 	  if (map->includes_phdrs)
   7401 	    {
   7402 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7403 
   7404 	      /* iehdr->e_phnum is just an estimate of the number
   7405 		 of program headers that we will need.  Make a note
   7406 		 here of the number we used and the segment we chose
   7407 		 to hold these headers, so that we can adjust the
   7408 		 offset when we know the correct value.  */
   7409 	      phdr_adjust_num = iehdr->e_phnum;
   7410 	      phdr_adjust_seg = map;
   7411 	    }
   7412 
   7413 	  if (map->includes_filehdr)
   7414 	    {
   7415 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7416 	      map->p_paddr -= iehdr->e_ehsize;
   7417 	      /* We've subtracted off the size of headers from the
   7418 		 first section lma, but there may have been some
   7419 		 alignment padding before that section too.  Try to
   7420 		 account for that by adjusting the segment lma down to
   7421 		 the same alignment.  */
   7422 	      if (segment->p_align != 0 && segment->p_align < align)
   7423 		align = segment->p_align;
   7424 	      map->p_paddr &= -(align * opb);
   7425 	    }
   7426 	}
   7427 
   7428       /* Step Three: Loop over the sections again, this time assigning
   7429 	 those that fit to the current segment and removing them from the
   7430 	 sections array; but making sure not to leave large gaps.  Once all
   7431 	 possible sections have been assigned to the current segment it is
   7432 	 added to the list of built segments and if sections still remain
   7433 	 to be assigned, a new segment is constructed before repeating
   7434 	 the loop.  */
   7435       isec = 0;
   7436       do
   7437 	{
   7438 	  map->count = 0;
   7439 	  suggested_lma = NULL;
   7440 
   7441 	  /* Fill the current segment with sections that fit.  */
   7442 	  for (j = 0; j < section_count; j++)
   7443 	    {
   7444 	      section = sections[j];
   7445 
   7446 	      if (section == NULL)
   7447 		continue;
   7448 
   7449 	      output_section = section->output_section;
   7450 
   7451 	      BFD_ASSERT (output_section != NULL);
   7452 
   7453 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
   7454 				       opb)
   7455 		  || IS_COREFILE_NOTE (segment, section))
   7456 		{
   7457 		  if (map->count == 0)
   7458 		    {
   7459 		      /* If the first section in a segment does not start at
   7460 			 the beginning of the segment, then something is
   7461 			 wrong.  */
   7462 		      if (align_power (map->p_paddr
   7463 				       + (map->includes_filehdr
   7464 					  ? iehdr->e_ehsize : 0)
   7465 				       + (map->includes_phdrs
   7466 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7467 					  : 0),
   7468 				       output_section->alignment_power * opb)
   7469 			  != output_section->lma * opb)
   7470 			goto sorry;
   7471 		    }
   7472 		  else
   7473 		    {
   7474 		      asection *prev_sec;
   7475 
   7476 		      prev_sec = map->sections[map->count - 1];
   7477 
   7478 		      /* If the gap between the end of the previous section
   7479 			 and the start of this section is more than
   7480 			 maxpagesize then we need to start a new segment.  */
   7481 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7482 				      maxpagesize)
   7483 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7484 			  || (prev_sec->lma + prev_sec->size
   7485 			      > output_section->lma))
   7486 			{
   7487 			  if (suggested_lma == NULL)
   7488 			    suggested_lma = output_section;
   7489 
   7490 			  continue;
   7491 			}
   7492 		    }
   7493 
   7494 		  map->sections[map->count++] = output_section;
   7495 		  ++isec;
   7496 		  sections[j] = NULL;
   7497 		  if (segment->p_type == PT_LOAD)
   7498 		    section->segment_mark = true;
   7499 		}
   7500 	      else if (suggested_lma == NULL)
   7501 		suggested_lma = output_section;
   7502 	    }
   7503 
   7504 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   7505 	     be assigned to any segment - because for example they have a
   7506 	     negative size - or segments that do not contain any sections.
   7507 	     But there are also valid reasons why a segment can be empty.
   7508 	     So allow a count of zero.  */
   7509 
   7510 	  /* Add the current segment to the list of built segments.  */
   7511 	  *pointer_to_map = map;
   7512 	  pointer_to_map = &map->next;
   7513 
   7514 	  if (isec < section_count)
   7515 	    {
   7516 	      /* We still have not allocated all of the sections to
   7517 		 segments.  Create a new segment here, initialise it
   7518 		 and carry on looping.  */
   7519 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7520 	      amt += section_count * sizeof (asection *);
   7521 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7522 	      if (map == NULL)
   7523 		{
   7524 		  free (sections);
   7525 		  return false;
   7526 		}
   7527 
   7528 	      /* Initialise the fields of the segment map.  Set the physical
   7529 		 physical address to the LMA of the first section that has
   7530 		 not yet been assigned.  */
   7531 	      map->next = NULL;
   7532 	      map->p_type = segment->p_type;
   7533 	      map->p_flags = segment->p_flags;
   7534 	      map->p_flags_valid = 1;
   7535 	      map->p_paddr = suggested_lma->lma * opb;
   7536 	      map->p_paddr_valid = p_paddr_valid;
   7537 	      map->includes_filehdr = 0;
   7538 	      map->includes_phdrs = 0;
   7539 	    }
   7540 
   7541 	  continue;
   7542 	sorry:
   7543 	  bfd_set_error (bfd_error_sorry);
   7544 	  free (sections);
   7545 	  return false;
   7546 	}
   7547       while (isec < section_count);
   7548 
   7549       free (sections);
   7550     }
   7551 
   7552   elf_seg_map (obfd) = map_first;
   7553 
   7554   /* If we had to estimate the number of program headers that were
   7555      going to be needed, then check our estimate now and adjust
   7556      the offset if necessary.  */
   7557   if (phdr_adjust_seg != NULL)
   7558     {
   7559       unsigned int count;
   7560 
   7561       for (count = 0, map = map_first; map != NULL; map = map->next)
   7562 	count++;
   7563 
   7564       if (count > phdr_adjust_num)
   7565 	phdr_adjust_seg->p_paddr
   7566 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   7567 
   7568       for (map = map_first; map != NULL; map = map->next)
   7569 	if (map->p_type == PT_PHDR)
   7570 	  {
   7571 	    bfd_vma adjust
   7572 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   7573 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   7574 	    break;
   7575 	  }
   7576     }
   7577 
   7578 #undef SEGMENT_END
   7579 #undef SECTION_SIZE
   7580 #undef IS_CONTAINED_BY_VMA
   7581 #undef IS_CONTAINED_BY_LMA
   7582 #undef IS_NOTE
   7583 #undef IS_COREFILE_NOTE
   7584 #undef IS_SOLARIS_PT_INTERP
   7585 #undef IS_SECTION_IN_INPUT_SEGMENT
   7586 #undef INCLUDE_SECTION_IN_SEGMENT
   7587 #undef SEGMENT_AFTER_SEGMENT
   7588 #undef SEGMENT_OVERLAPS
   7589   return true;
   7590 }
   7591 
   7592 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   7593 
   7594 static bool
   7595 elf_is_p_align_valid (bfd *abfd)
   7596 {
   7597   unsigned int i;
   7598   Elf_Internal_Phdr *segment;
   7599   unsigned int num_segments;
   7600   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7601   bfd_size_type maxpagesize = bed->maxpagesize;
   7602   bfd_size_type p_align = bed->p_align;
   7603 
   7604   /* Return true if the default p_align value isn't set or the maximum
   7605      page size is the same as the minimum page size.  */
   7606   if (p_align == 0 || maxpagesize == bed->minpagesize)
   7607     return true;
   7608 
   7609   /* When the default p_align value is set, p_align may be set to the
   7610      default p_align value while segments are aligned to the maximum
   7611      page size.  In this case, the input p_align will be ignored and
   7612      the maximum page size will be used to align the output segments.  */
   7613   segment = elf_tdata (abfd)->phdr;
   7614   num_segments = elf_elfheader (abfd)->e_phnum;
   7615   for (i = 0; i < num_segments; i++, segment++)
   7616     if (segment->p_type == PT_LOAD
   7617 	&& (segment->p_align != p_align
   7618 	    || vma_page_aligned_bias (segment->p_vaddr,
   7619 				      segment->p_offset,
   7620 				      maxpagesize) != 0))
   7621       return true;
   7622 
   7623   return false;
   7624 }
   7625 
   7626 /* Copy ELF program header information.  */
   7627 
   7628 static bool
   7629 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   7630 {
   7631   Elf_Internal_Ehdr *iehdr;
   7632   struct elf_segment_map *map;
   7633   struct elf_segment_map *map_first;
   7634   struct elf_segment_map **pointer_to_map;
   7635   Elf_Internal_Phdr *segment;
   7636   unsigned int i;
   7637   unsigned int num_segments;
   7638   bool phdr_included = false;
   7639   bool p_paddr_valid;
   7640   bool p_palign_valid;
   7641   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7642 
   7643   iehdr = elf_elfheader (ibfd);
   7644 
   7645   map_first = NULL;
   7646   pointer_to_map = &map_first;
   7647 
   7648   /* If all the segment p_paddr fields are zero, don't set
   7649      map->p_paddr_valid.  */
   7650   p_paddr_valid = false;
   7651   num_segments = elf_elfheader (ibfd)->e_phnum;
   7652   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7653        i < num_segments;
   7654        i++, segment++)
   7655     if (segment->p_paddr != 0)
   7656       {
   7657 	p_paddr_valid = true;
   7658 	break;
   7659       }
   7660 
   7661   p_palign_valid = elf_is_p_align_valid (ibfd);
   7662 
   7663   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7664        i < num_segments;
   7665        i++, segment++)
   7666     {
   7667       asection *section;
   7668       unsigned int section_count;
   7669       size_t amt;
   7670       Elf_Internal_Shdr *this_hdr;
   7671       asection *first_section = NULL;
   7672       asection *lowest_section;
   7673 
   7674       /* Compute how many sections are in this segment.  */
   7675       for (section = ibfd->sections, section_count = 0;
   7676 	   section != NULL;
   7677 	   section = section->next)
   7678 	{
   7679 	  this_hdr = &(elf_section_data(section)->this_hdr);
   7680 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7681 	    {
   7682 	      if (first_section == NULL)
   7683 		first_section = section;
   7684 	      section_count++;
   7685 	    }
   7686 	}
   7687 
   7688       /* Allocate a segment map big enough to contain
   7689 	 all of the sections we have selected.  */
   7690       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7691       amt += section_count * sizeof (asection *);
   7692       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7693       if (map == NULL)
   7694 	return false;
   7695 
   7696       /* Initialize the fields of the output segment map with the
   7697 	 input segment.  */
   7698       map->next = NULL;
   7699       map->p_type = segment->p_type;
   7700       map->p_flags = segment->p_flags;
   7701       map->p_flags_valid = 1;
   7702       map->p_paddr = segment->p_paddr;
   7703       map->p_paddr_valid = p_paddr_valid;
   7704       map->p_align = segment->p_align;
   7705       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   7706       map->p_align_valid = (map->p_type == PT_GNU_STACK
   7707 			    || p_palign_valid);
   7708       map->p_vaddr_offset = 0;
   7709 
   7710       if (map->p_type == PT_GNU_RELRO
   7711 	  || map->p_type == PT_GNU_STACK)
   7712 	{
   7713 	  /* The PT_GNU_RELRO segment may contain the first a few
   7714 	     bytes in the .got.plt section even if the whole .got.plt
   7715 	     section isn't in the PT_GNU_RELRO segment.  We won't
   7716 	     change the size of the PT_GNU_RELRO segment.
   7717 	     Similarly, PT_GNU_STACK size is significant on uclinux
   7718 	     systems.    */
   7719 	  map->p_size = segment->p_memsz;
   7720 	  map->p_size_valid = 1;
   7721 	}
   7722 
   7723       /* Determine if this segment contains the ELF file header
   7724 	 and if it contains the program headers themselves.  */
   7725       map->includes_filehdr = (segment->p_offset == 0
   7726 			       && segment->p_filesz >= iehdr->e_ehsize);
   7727 
   7728       map->includes_phdrs = 0;
   7729       if (! phdr_included || segment->p_type != PT_LOAD)
   7730 	{
   7731 	  map->includes_phdrs =
   7732 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7733 	     && (segment->p_offset + segment->p_filesz
   7734 		 >= ((bfd_vma) iehdr->e_phoff
   7735 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7736 
   7737 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7738 	    phdr_included = true;
   7739 	}
   7740 
   7741       lowest_section = NULL;
   7742       if (section_count != 0)
   7743 	{
   7744 	  unsigned int isec = 0;
   7745 
   7746 	  for (section = first_section;
   7747 	       section != NULL;
   7748 	       section = section->next)
   7749 	    {
   7750 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7751 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7752 		{
   7753 		  map->sections[isec++] = section->output_section;
   7754 		  if ((section->flags & SEC_ALLOC) != 0)
   7755 		    {
   7756 		      bfd_vma seg_off;
   7757 
   7758 		      if (lowest_section == NULL
   7759 			  || section->lma < lowest_section->lma)
   7760 			lowest_section = section;
   7761 
   7762 		      /* Section lmas are set up from PT_LOAD header
   7763 			 p_paddr in _bfd_elf_make_section_from_shdr.
   7764 			 If this header has a p_paddr that disagrees
   7765 			 with the section lma, flag the p_paddr as
   7766 			 invalid.  */
   7767 		      if ((section->flags & SEC_LOAD) != 0)
   7768 			seg_off = this_hdr->sh_offset - segment->p_offset;
   7769 		      else
   7770 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   7771 		      if (section->lma * opb - segment->p_paddr != seg_off)
   7772 			map->p_paddr_valid = false;
   7773 		    }
   7774 		  if (isec == section_count)
   7775 		    break;
   7776 		}
   7777 	    }
   7778 	}
   7779 
   7780       if (section_count == 0)
   7781 	map->p_vaddr_offset = segment->p_vaddr / opb;
   7782       else if (map->p_paddr_valid)
   7783 	{
   7784 	  /* Account for padding before the first section in the segment.  */
   7785 	  bfd_vma hdr_size = 0;
   7786 	  if (map->includes_filehdr)
   7787 	    hdr_size = iehdr->e_ehsize;
   7788 	  if (map->includes_phdrs)
   7789 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7790 
   7791 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7792 				 - (lowest_section ? lowest_section->lma : 0));
   7793 	}
   7794 
   7795       map->count = section_count;
   7796       *pointer_to_map = map;
   7797       pointer_to_map = &map->next;
   7798     }
   7799 
   7800   elf_seg_map (obfd) = map_first;
   7801   return true;
   7802 }
   7803 
   7804 /* Copy private BFD data.  This copies or rewrites ELF program header
   7805    information.  */
   7806 
   7807 static bool
   7808 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   7809 {
   7810   bfd_vma maxpagesize;
   7811 
   7812   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7813       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7814     return true;
   7815 
   7816   if (elf_tdata (ibfd)->phdr == NULL)
   7817     return true;
   7818 
   7819   if (ibfd->xvec == obfd->xvec)
   7820     {
   7821       /* Check to see if any sections in the input BFD
   7822 	 covered by ELF program header have changed.  */
   7823       Elf_Internal_Phdr *segment;
   7824       asection *section, *osec;
   7825       unsigned int i, num_segments;
   7826       Elf_Internal_Shdr *this_hdr;
   7827       const struct elf_backend_data *bed;
   7828 
   7829       bed = get_elf_backend_data (ibfd);
   7830 
   7831       /* Regenerate the segment map if p_paddr is set to 0.  */
   7832       if (bed->want_p_paddr_set_to_zero)
   7833 	goto rewrite;
   7834 
   7835       /* Initialize the segment mark field.  */
   7836       for (section = obfd->sections; section != NULL;
   7837 	   section = section->next)
   7838 	section->segment_mark = false;
   7839 
   7840       num_segments = elf_elfheader (ibfd)->e_phnum;
   7841       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7842 	   i < num_segments;
   7843 	   i++, segment++)
   7844 	{
   7845 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   7846 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   7847 	     which severly confuses things, so always regenerate the segment
   7848 	     map in this case.  */
   7849 	  if (segment->p_paddr == 0
   7850 	      && segment->p_memsz == 0
   7851 	      && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
   7852 	    goto rewrite;
   7853 
   7854 	  for (section = ibfd->sections;
   7855 	       section != NULL; section = section->next)
   7856 	    {
   7857 	      /* We mark the output section so that we know it comes
   7858 		 from the input BFD.  */
   7859 	      osec = section->output_section;
   7860 	      if (osec)
   7861 		osec->segment_mark = true;
   7862 
   7863 	      /* Check if this section is covered by the segment.  */
   7864 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7865 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7866 		{
   7867 		  /* FIXME: Check if its output section is changed or
   7868 		     removed.  What else do we need to check?  */
   7869 		  if (osec == NULL
   7870 		      || section->flags != osec->flags
   7871 		      || section->lma != osec->lma
   7872 		      || section->vma != osec->vma
   7873 		      || section->size != osec->size
   7874 		      || section->rawsize != osec->rawsize
   7875 		      || section->alignment_power != osec->alignment_power)
   7876 		    goto rewrite;
   7877 		}
   7878 	    }
   7879 	}
   7880 
   7881       /* Check to see if any output section do not come from the
   7882 	 input BFD.  */
   7883       for (section = obfd->sections; section != NULL;
   7884 	   section = section->next)
   7885 	{
   7886 	  if (!section->segment_mark)
   7887 	    goto rewrite;
   7888 	  else
   7889 	    section->segment_mark = false;
   7890 	}
   7891 
   7892       return copy_elf_program_header (ibfd, obfd);
   7893     }
   7894 
   7895  rewrite:
   7896   maxpagesize = 0;
   7897   if (ibfd->xvec == obfd->xvec)
   7898     {
   7899       /* When rewriting program header, set the output maxpagesize to
   7900 	 the maximum alignment of input PT_LOAD segments.  */
   7901       Elf_Internal_Phdr *segment;
   7902       unsigned int i;
   7903       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   7904 
   7905       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7906 	   i < num_segments;
   7907 	   i++, segment++)
   7908 	if (segment->p_type == PT_LOAD
   7909 	    && maxpagesize < segment->p_align)
   7910 	  {
   7911 	    /* PR 17512: file: f17299af.  */
   7912 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   7913 	      /* xgettext:c-format */
   7914 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   7915 				    PRIx64 " is too large"),
   7916 				  ibfd, (uint64_t) segment->p_align);
   7917 	    else
   7918 	      maxpagesize = segment->p_align;
   7919 	  }
   7920     }
   7921   if (maxpagesize == 0)
   7922     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   7923 
   7924   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   7925 }
   7926 
   7927 /* Initialize private output section information from input section.  */
   7928 
   7929 bool
   7930 _bfd_elf_init_private_section_data (bfd *ibfd,
   7931 				    asection *isec,
   7932 				    bfd *obfd,
   7933 				    asection *osec,
   7934 				    struct bfd_link_info *link_info)
   7935 
   7936 {
   7937   Elf_Internal_Shdr *ihdr, *ohdr;
   7938   bool final_link = (link_info != NULL
   7939 		     && !bfd_link_relocatable (link_info));
   7940 
   7941   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7942       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7943     return true;
   7944 
   7945   BFD_ASSERT (elf_section_data (osec) != NULL);
   7946 
   7947   /* If this is a known ABI section, ELF section type and flags may
   7948      have been set up when OSEC was created.  For normal sections we
   7949      allow the user to override the type and flags other than
   7950      SHF_MASKOS and SHF_MASKPROC.  */
   7951   if (elf_section_type (osec) == SHT_PROGBITS
   7952       || elf_section_type (osec) == SHT_NOTE
   7953       || elf_section_type (osec) == SHT_NOBITS)
   7954     elf_section_type (osec) = SHT_NULL;
   7955   /* For objcopy and relocatable link, copy the ELF section type from
   7956      the input file if the BFD section flags are the same.  (If they
   7957      are different the user may be doing something like
   7958      "objcopy --set-section-flags .text=alloc,data".)  For a final
   7959      link allow some flags that the linker clears to differ.  */
   7960   if (elf_section_type (osec) == SHT_NULL
   7961       && (osec->flags == isec->flags
   7962 	  || (final_link
   7963 	      && ((osec->flags ^ isec->flags)
   7964 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   7965     elf_section_type (osec) = elf_section_type (isec);
   7966 
   7967   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   7968   elf_section_flags (osec) = (elf_section_flags (isec)
   7969 			      & (SHF_MASKOS | SHF_MASKPROC));
   7970 
   7971   /* Copy sh_info from input for mbind section.  */
   7972   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   7973       && elf_section_flags (isec) & SHF_GNU_MBIND)
   7974     elf_section_data (osec)->this_hdr.sh_info
   7975       = elf_section_data (isec)->this_hdr.sh_info;
   7976 
   7977   /* Set things up for objcopy and relocatable link.  The output
   7978      SHT_GROUP section will have its elf_next_in_group pointing back
   7979      to the input group members.  Ignore linker created group section.
   7980      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   7981   if ((link_info == NULL
   7982        || !link_info->resolve_section_groups)
   7983       && (elf_sec_group (isec) == NULL
   7984 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   7985     {
   7986       if (elf_section_flags (isec) & SHF_GROUP)
   7987 	elf_section_flags (osec) |= SHF_GROUP;
   7988       elf_next_in_group (osec) = elf_next_in_group (isec);
   7989       elf_section_data (osec)->group = elf_section_data (isec)->group;
   7990     }
   7991 
   7992   /* If not decompress, preserve SHF_COMPRESSED.  */
   7993   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   7994     elf_section_flags (osec) |= (elf_section_flags (isec)
   7995 				 & SHF_COMPRESSED);
   7996 
   7997   ihdr = &elf_section_data (isec)->this_hdr;
   7998 
   7999   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   8000      don't use the output section of the linked-to section since it
   8001      may be NULL at this point.  */
   8002   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   8003     {
   8004       ohdr = &elf_section_data (osec)->this_hdr;
   8005       ohdr->sh_flags |= SHF_LINK_ORDER;
   8006       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   8007     }
   8008 
   8009   osec->use_rela_p = isec->use_rela_p;
   8010 
   8011   return true;
   8012 }
   8013 
   8014 /* Copy private section information.  This copies over the entsize
   8015    field, and sometimes the info field.  */
   8016 
   8017 bool
   8018 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8019 				    asection *isec,
   8020 				    bfd *obfd,
   8021 				    asection *osec)
   8022 {
   8023   Elf_Internal_Shdr *ihdr, *ohdr;
   8024 
   8025   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8026       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8027     return true;
   8028 
   8029   ihdr = &elf_section_data (isec)->this_hdr;
   8030   ohdr = &elf_section_data (osec)->this_hdr;
   8031 
   8032   ohdr->sh_entsize = ihdr->sh_entsize;
   8033 
   8034   if (ihdr->sh_type == SHT_SYMTAB
   8035       || ihdr->sh_type == SHT_DYNSYM
   8036       || ihdr->sh_type == SHT_GNU_verneed
   8037       || ihdr->sh_type == SHT_GNU_verdef)
   8038     ohdr->sh_info = ihdr->sh_info;
   8039 
   8040   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   8041 					     NULL);
   8042 }
   8043 
   8044 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8045    necessary if we are removing either the SHT_GROUP section or any of
   8046    the group member sections.  DISCARDED is the value that a section's
   8047    output_section has if the section will be discarded, NULL when this
   8048    function is called from objcopy, bfd_abs_section_ptr when called
   8049    from the linker.  */
   8050 
   8051 bool
   8052 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8053 {
   8054   asection *isec;
   8055 
   8056   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8057     if (elf_section_type (isec) == SHT_GROUP)
   8058       {
   8059 	asection *first = elf_next_in_group (isec);
   8060 	asection *s = first;
   8061 	bfd_size_type removed = 0;
   8062 
   8063 	while (s != NULL)
   8064 	  {
   8065 	    /* If this member section is being output but the
   8066 	       SHT_GROUP section is not, then clear the group info
   8067 	       set up by _bfd_elf_copy_private_section_data.  */
   8068 	    if (s->output_section != discarded
   8069 		&& isec->output_section == discarded)
   8070 	      {
   8071 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8072 		elf_group_name (s->output_section) = NULL;
   8073 	      }
   8074 	    else
   8075 	      {
   8076 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8077 		if (s->output_section == discarded
   8078 		    && isec->output_section != discarded)
   8079 		  {
   8080 		    /* Conversely, if the member section is not being
   8081 		       output but the SHT_GROUP section is, then adjust
   8082 		       its size.  */
   8083 		    removed += 4;
   8084 		    if (elf_sec->rel.hdr != NULL
   8085 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8086 		      removed += 4;
   8087 		    if (elf_sec->rela.hdr != NULL
   8088 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8089 		      removed += 4;
   8090 		  }
   8091 		else
   8092 		  {
   8093 		    /* Also adjust for zero-sized relocation member
   8094 		       section.  */
   8095 		    if (elf_sec->rel.hdr != NULL
   8096 			&& elf_sec->rel.hdr->sh_size == 0)
   8097 		      removed += 4;
   8098 		    if (elf_sec->rela.hdr != NULL
   8099 			&& elf_sec->rela.hdr->sh_size == 0)
   8100 		      removed += 4;
   8101 		  }
   8102 	      }
   8103 	    s = elf_next_in_group (s);
   8104 	    if (s == first)
   8105 	      break;
   8106 	  }
   8107 	if (removed != 0)
   8108 	  {
   8109 	    if (discarded != NULL)
   8110 	      {
   8111 		/* If we've been called for ld -r, then we need to
   8112 		   adjust the input section size.  */
   8113 		if (isec->rawsize == 0)
   8114 		  isec->rawsize = isec->size;
   8115 		isec->size = isec->rawsize - removed;
   8116 		if (isec->size <= 4)
   8117 		  {
   8118 		    isec->size = 0;
   8119 		    isec->flags |= SEC_EXCLUDE;
   8120 		  }
   8121 	      }
   8122 	    else if (isec->output_section != NULL)
   8123 	      {
   8124 		/* Adjust the output section size when called from
   8125 		   objcopy. */
   8126 		isec->output_section->size -= removed;
   8127 		if (isec->output_section->size <= 4)
   8128 		  {
   8129 		    isec->output_section->size = 0;
   8130 		    isec->output_section->flags |= SEC_EXCLUDE;
   8131 		  }
   8132 	      }
   8133 	  }
   8134       }
   8135 
   8136   return true;
   8137 }
   8138 
   8139 /* Copy private header information.  */
   8140 
   8141 bool
   8142 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8143 {
   8144   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8145       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8146     return true;
   8147 
   8148   /* Copy over private BFD data if it has not already been copied.
   8149      This must be done here, rather than in the copy_private_bfd_data
   8150      entry point, because the latter is called after the section
   8151      contents have been set, which means that the program headers have
   8152      already been worked out.  */
   8153   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8154     {
   8155       if (! copy_private_bfd_data (ibfd, obfd))
   8156 	return false;
   8157     }
   8158 
   8159   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8160 }
   8161 
   8162 /* Copy private symbol information.  If this symbol is in a section
   8163    which we did not map into a BFD section, try to map the section
   8164    index correctly.  We use special macro definitions for the mapped
   8165    section indices; these definitions are interpreted by the
   8166    swap_out_syms function.  */
   8167 
   8168 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8169 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8170 #define MAP_STRTAB    (SHN_HIOS + 3)
   8171 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8172 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8173 
   8174 bool
   8175 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8176 				   asymbol *isymarg,
   8177 				   bfd *obfd,
   8178 				   asymbol *osymarg)
   8179 {
   8180   elf_symbol_type *isym, *osym;
   8181 
   8182   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8183       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8184     return true;
   8185 
   8186   isym = elf_symbol_from (isymarg);
   8187   osym = elf_symbol_from (osymarg);
   8188 
   8189   if (isym != NULL
   8190       && isym->internal_elf_sym.st_shndx != 0
   8191       && osym != NULL
   8192       && bfd_is_abs_section (isym->symbol.section))
   8193     {
   8194       unsigned int shndx;
   8195 
   8196       shndx = isym->internal_elf_sym.st_shndx;
   8197       if (shndx == elf_onesymtab (ibfd))
   8198 	shndx = MAP_ONESYMTAB;
   8199       else if (shndx == elf_dynsymtab (ibfd))
   8200 	shndx = MAP_DYNSYMTAB;
   8201       else if (shndx == elf_strtab_sec (ibfd))
   8202 	shndx = MAP_STRTAB;
   8203       else if (shndx == elf_shstrtab_sec (ibfd))
   8204 	shndx = MAP_SHSTRTAB;
   8205       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8206 	shndx = MAP_SYM_SHNDX;
   8207       osym->internal_elf_sym.st_shndx = shndx;
   8208     }
   8209 
   8210   return true;
   8211 }
   8212 
   8213 /* Swap out the symbols.  */
   8214 
   8215 static bool
   8216 swap_out_syms (bfd *abfd,
   8217 	       struct elf_strtab_hash **sttp,
   8218 	       int relocatable_p,
   8219 	       struct bfd_link_info *info)
   8220 {
   8221   const struct elf_backend_data *bed;
   8222   unsigned int symcount;
   8223   asymbol **syms;
   8224   struct elf_strtab_hash *stt;
   8225   Elf_Internal_Shdr *symtab_hdr;
   8226   Elf_Internal_Shdr *symtab_shndx_hdr;
   8227   Elf_Internal_Shdr *symstrtab_hdr;
   8228   struct elf_sym_strtab *symstrtab;
   8229   bfd_byte *outbound_syms;
   8230   bfd_byte *outbound_shndx;
   8231   unsigned long outbound_syms_index;
   8232   unsigned int idx;
   8233   unsigned int num_locals;
   8234   size_t amt;
   8235   bool name_local_sections;
   8236 
   8237   if (!elf_map_symbols (abfd, &num_locals))
   8238     return false;
   8239 
   8240   /* Dump out the symtabs.  */
   8241   stt = _bfd_elf_strtab_init ();
   8242   if (stt == NULL)
   8243     return false;
   8244 
   8245   bed = get_elf_backend_data (abfd);
   8246   symcount = bfd_get_symcount (abfd);
   8247   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8248   symtab_hdr->sh_type = SHT_SYMTAB;
   8249   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8250   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8251   symtab_hdr->sh_info = num_locals + 1;
   8252   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8253 
   8254   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8255   symstrtab_hdr->sh_type = SHT_STRTAB;
   8256 
   8257   /* Allocate buffer to swap out the .strtab section.  */
   8258   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8259       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8260     {
   8261       bfd_set_error (bfd_error_no_memory);
   8262       _bfd_elf_strtab_free (stt);
   8263       return false;
   8264     }
   8265 
   8266   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8267       || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
   8268     {
   8269     error_no_mem:
   8270       bfd_set_error (bfd_error_no_memory);
   8271     error_return:
   8272       free (symstrtab);
   8273       _bfd_elf_strtab_free (stt);
   8274       return false;
   8275     }
   8276   symtab_hdr->contents = outbound_syms;
   8277   outbound_syms_index = 0;
   8278 
   8279   outbound_shndx = NULL;
   8280 
   8281   if (elf_symtab_shndx_list (abfd))
   8282     {
   8283       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8284       if (symtab_shndx_hdr->sh_name != 0)
   8285 	{
   8286 	  if (_bfd_mul_overflow (symcount + 1,
   8287 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8288 	    goto error_no_mem;
   8289 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8290 	  if (outbound_shndx == NULL)
   8291 	    goto error_return;
   8292 
   8293 	  symtab_shndx_hdr->contents = outbound_shndx;
   8294 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8295 	  symtab_shndx_hdr->sh_size = amt;
   8296 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8297 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8298 	}
   8299       /* FIXME: What about any other headers in the list ?  */
   8300     }
   8301 
   8302   /* Now generate the data (for "contents").  */
   8303   {
   8304     /* Fill in zeroth symbol and swap it out.  */
   8305     Elf_Internal_Sym sym;
   8306     sym.st_name = 0;
   8307     sym.st_value = 0;
   8308     sym.st_size = 0;
   8309     sym.st_info = 0;
   8310     sym.st_other = 0;
   8311     sym.st_shndx = SHN_UNDEF;
   8312     sym.st_target_internal = 0;
   8313     symstrtab[0].sym = sym;
   8314     symstrtab[0].dest_index = outbound_syms_index;
   8315     outbound_syms_index++;
   8316   }
   8317 
   8318   name_local_sections
   8319     = (bed->elf_backend_name_local_section_symbols
   8320        && bed->elf_backend_name_local_section_symbols (abfd));
   8321 
   8322   syms = bfd_get_outsymbols (abfd);
   8323   for (idx = 0; idx < symcount;)
   8324     {
   8325       Elf_Internal_Sym sym;
   8326       bfd_vma value = syms[idx]->value;
   8327       elf_symbol_type *type_ptr;
   8328       flagword flags = syms[idx]->flags;
   8329       int type;
   8330 
   8331       if (!name_local_sections
   8332 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8333 	{
   8334 	  /* Local section symbols have no name.  */
   8335 	  sym.st_name = (unsigned long) -1;
   8336 	}
   8337       else
   8338 	{
   8339 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8340 	     to get the final offset for st_name.  */
   8341 	  sym.st_name
   8342 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   8343 						   false);
   8344 	  if (sym.st_name == (unsigned long) -1)
   8345 	    goto error_return;
   8346 	}
   8347 
   8348       type_ptr = elf_symbol_from (syms[idx]);
   8349 
   8350       if ((flags & BSF_SECTION_SYM) == 0
   8351 	  && bfd_is_com_section (syms[idx]->section))
   8352 	{
   8353 	  /* ELF common symbols put the alignment into the `value' field,
   8354 	     and the size into the `size' field.  This is backwards from
   8355 	     how BFD handles it, so reverse it here.  */
   8356 	  sym.st_size = value;
   8357 	  if (type_ptr == NULL
   8358 	      || type_ptr->internal_elf_sym.st_value == 0)
   8359 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8360 	  else
   8361 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8362 	  sym.st_shndx = _bfd_elf_section_from_bfd_section
   8363 	    (abfd, syms[idx]->section);
   8364 	}
   8365       else
   8366 	{
   8367 	  asection *sec = syms[idx]->section;
   8368 	  unsigned int shndx;
   8369 
   8370 	  if (sec->output_section)
   8371 	    {
   8372 	      value += sec->output_offset;
   8373 	      sec = sec->output_section;
   8374 	    }
   8375 
   8376 	  /* Don't add in the section vma for relocatable output.  */
   8377 	  if (! relocatable_p)
   8378 	    value += sec->vma;
   8379 	  sym.st_value = value;
   8380 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8381 
   8382 	  if (bfd_is_abs_section (sec)
   8383 	      && type_ptr != NULL
   8384 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8385 	    {
   8386 	      /* This symbol is in a real ELF section which we did
   8387 		 not create as a BFD section.  Undo the mapping done
   8388 		 by copy_private_symbol_data.  */
   8389 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8390 	      switch (shndx)
   8391 		{
   8392 		case MAP_ONESYMTAB:
   8393 		  shndx = elf_onesymtab (abfd);
   8394 		  break;
   8395 		case MAP_DYNSYMTAB:
   8396 		  shndx = elf_dynsymtab (abfd);
   8397 		  break;
   8398 		case MAP_STRTAB:
   8399 		  shndx = elf_strtab_sec (abfd);
   8400 		  break;
   8401 		case MAP_SHSTRTAB:
   8402 		  shndx = elf_shstrtab_sec (abfd);
   8403 		  break;
   8404 		case MAP_SYM_SHNDX:
   8405 		  if (elf_symtab_shndx_list (abfd))
   8406 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8407 		  break;
   8408 		case SHN_COMMON:
   8409 		case SHN_ABS:
   8410 		  shndx = SHN_ABS;
   8411 		  break;
   8412 		default:
   8413 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8414 		    {
   8415 		      if (bed->symbol_section_index)
   8416 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8417 		      /* Otherwise just leave the index alone.  */
   8418 		    }
   8419 		  else
   8420 		    {
   8421 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   8422 			_bfd_error_handler (_("%pB: \
   8423 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   8424 					  abfd, shndx);
   8425 		      shndx = SHN_ABS;
   8426 		    }
   8427 		  break;
   8428 		}
   8429 	    }
   8430 	  else
   8431 	    {
   8432 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8433 
   8434 	      if (shndx == SHN_BAD)
   8435 		{
   8436 		  asection *sec2;
   8437 
   8438 		  /* Writing this would be a hell of a lot easier if
   8439 		     we had some decent documentation on bfd, and
   8440 		     knew what to expect of the library, and what to
   8441 		     demand of applications.  For example, it
   8442 		     appears that `objcopy' might not set the
   8443 		     section of a symbol to be a section that is
   8444 		     actually in the output file.  */
   8445 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   8446 		  if (sec2 != NULL)
   8447 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   8448 		  if (shndx == SHN_BAD)
   8449 		    {
   8450 		      /* xgettext:c-format */
   8451 		      _bfd_error_handler
   8452 			(_("unable to find equivalent output section"
   8453 			   " for symbol '%s' from section '%s'"),
   8454 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   8455 			 sec->name);
   8456 		      bfd_set_error (bfd_error_invalid_operation);
   8457 		      goto error_return;
   8458 		    }
   8459 		}
   8460 	    }
   8461 
   8462 	  sym.st_shndx = shndx;
   8463 	}
   8464 
   8465       if ((flags & BSF_THREAD_LOCAL) != 0)
   8466 	type = STT_TLS;
   8467       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8468 	type = STT_GNU_IFUNC;
   8469       else if ((flags & BSF_FUNCTION) != 0)
   8470 	type = STT_FUNC;
   8471       else if ((flags & BSF_OBJECT) != 0)
   8472 	type = STT_OBJECT;
   8473       else if ((flags & BSF_RELC) != 0)
   8474 	type = STT_RELC;
   8475       else if ((flags & BSF_SRELC) != 0)
   8476 	type = STT_SRELC;
   8477       else
   8478 	type = STT_NOTYPE;
   8479 
   8480       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8481 	type = STT_TLS;
   8482 
   8483       /* Processor-specific types.  */
   8484       if (type_ptr != NULL
   8485 	  && bed->elf_backend_get_symbol_type)
   8486 	type = ((*bed->elf_backend_get_symbol_type)
   8487 		(&type_ptr->internal_elf_sym, type));
   8488 
   8489       if (flags & BSF_SECTION_SYM)
   8490 	{
   8491 	  if (flags & BSF_GLOBAL)
   8492 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   8493 	  else
   8494 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   8495 	}
   8496       else if (bfd_is_com_section (syms[idx]->section))
   8497 	{
   8498 	  if (type != STT_TLS)
   8499 	    {
   8500 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   8501 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   8502 			? STT_COMMON : STT_OBJECT);
   8503 	      else
   8504 		type = ((flags & BSF_ELF_COMMON) != 0
   8505 			? STT_COMMON : STT_OBJECT);
   8506 	    }
   8507 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   8508 	}
   8509       else if (bfd_is_und_section (syms[idx]->section))
   8510 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   8511 				    ? STB_WEAK
   8512 				    : STB_GLOBAL),
   8513 				   type);
   8514       else if (flags & BSF_FILE)
   8515 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   8516       else
   8517 	{
   8518 	  int bind = STB_LOCAL;
   8519 
   8520 	  if (flags & BSF_LOCAL)
   8521 	    bind = STB_LOCAL;
   8522 	  else if (flags & BSF_GNU_UNIQUE)
   8523 	    bind = STB_GNU_UNIQUE;
   8524 	  else if (flags & BSF_WEAK)
   8525 	    bind = STB_WEAK;
   8526 	  else if (flags & BSF_GLOBAL)
   8527 	    bind = STB_GLOBAL;
   8528 
   8529 	  sym.st_info = ELF_ST_INFO (bind, type);
   8530 	}
   8531 
   8532       if (type_ptr != NULL)
   8533 	{
   8534 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   8535 	  sym.st_target_internal
   8536 	    = type_ptr->internal_elf_sym.st_target_internal;
   8537 	}
   8538       else
   8539 	{
   8540 	  sym.st_other = 0;
   8541 	  sym.st_target_internal = 0;
   8542 	}
   8543 
   8544       idx++;
   8545       symstrtab[idx].sym = sym;
   8546       symstrtab[idx].dest_index = outbound_syms_index;
   8547 
   8548       outbound_syms_index++;
   8549     }
   8550 
   8551   /* Finalize the .strtab section.  */
   8552   _bfd_elf_strtab_finalize (stt);
   8553 
   8554   /* Swap out the .strtab section.  */
   8555   for (idx = 0; idx <= symcount; idx++)
   8556     {
   8557       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   8558       if (elfsym->sym.st_name == (unsigned long) -1)
   8559 	elfsym->sym.st_name = 0;
   8560       else
   8561 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   8562 						      elfsym->sym.st_name);
   8563       if (info && info->callbacks->ctf_new_symbol)
   8564 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   8565 					 &elfsym->sym);
   8566 
   8567       /* Inform the linker of the addition of this symbol.  */
   8568 
   8569       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   8570 			       (outbound_syms
   8571 				+ (elfsym->dest_index
   8572 				   * bed->s->sizeof_sym)),
   8573 			       NPTR_ADD (outbound_shndx,
   8574 					 (elfsym->dest_index
   8575 					  * sizeof (Elf_External_Sym_Shndx))));
   8576     }
   8577   free (symstrtab);
   8578 
   8579   *sttp = stt;
   8580   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   8581   symstrtab_hdr->sh_type = SHT_STRTAB;
   8582   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   8583   symstrtab_hdr->sh_addr = 0;
   8584   symstrtab_hdr->sh_entsize = 0;
   8585   symstrtab_hdr->sh_link = 0;
   8586   symstrtab_hdr->sh_info = 0;
   8587   symstrtab_hdr->sh_addralign = 1;
   8588 
   8589   return true;
   8590 }
   8591 
   8592 /* Return the number of bytes required to hold the symtab vector.
   8593 
   8594    Note that we base it on the count plus 1, since we will null terminate
   8595    the vector allocated based on this size.  However, the ELF symbol table
   8596    always has a dummy entry as symbol #0, so it ends up even.  */
   8597 
   8598 long
   8599 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   8600 {
   8601   bfd_size_type symcount;
   8602   long symtab_size;
   8603   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   8604 
   8605   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8606   if (symcount > LONG_MAX / sizeof (asymbol *))
   8607     {
   8608       bfd_set_error (bfd_error_file_too_big);
   8609       return -1;
   8610     }
   8611   symtab_size = symcount * (sizeof (asymbol *));
   8612   if (symcount == 0)
   8613     symtab_size = sizeof (asymbol *);
   8614   else if (!bfd_write_p (abfd))
   8615     {
   8616       ufile_ptr filesize = bfd_get_file_size (abfd);
   8617 
   8618       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   8619 	{
   8620 	  bfd_set_error (bfd_error_file_truncated);
   8621 	  return -1;
   8622 	}
   8623     }
   8624 
   8625   return symtab_size;
   8626 }
   8627 
   8628 long
   8629 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   8630 {
   8631   bfd_size_type symcount;
   8632   long symtab_size;
   8633   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   8634 
   8635   if (elf_dynsymtab (abfd) == 0)
   8636     {
   8637       bfd_set_error (bfd_error_invalid_operation);
   8638       return -1;
   8639     }
   8640 
   8641   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8642   if (symcount > LONG_MAX / sizeof (asymbol *))
   8643     {
   8644       bfd_set_error (bfd_error_file_too_big);
   8645       return -1;
   8646     }
   8647   symtab_size = symcount * (sizeof (asymbol *));
   8648   if (symcount == 0)
   8649     symtab_size = sizeof (asymbol *);
   8650   else if (!bfd_write_p (abfd))
   8651     {
   8652       ufile_ptr filesize = bfd_get_file_size (abfd);
   8653 
   8654       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   8655 	{
   8656 	  bfd_set_error (bfd_error_file_truncated);
   8657 	  return -1;
   8658 	}
   8659     }
   8660 
   8661   return symtab_size;
   8662 }
   8663 
   8664 long
   8665 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   8666 {
   8667   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   8668     {
   8669       /* Sanity check reloc section size.  */
   8670       struct bfd_elf_section_data *d = elf_section_data (asect);
   8671       Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
   8672       bfd_size_type ext_rel_size = rel_hdr->sh_size;
   8673       ufile_ptr filesize = bfd_get_file_size (abfd);
   8674 
   8675       if (filesize != 0 && ext_rel_size > filesize)
   8676 	{
   8677 	  bfd_set_error (bfd_error_file_truncated);
   8678 	  return -1;
   8679 	}
   8680     }
   8681 
   8682 #if SIZEOF_LONG == SIZEOF_INT
   8683   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   8684     {
   8685       bfd_set_error (bfd_error_file_too_big);
   8686       return -1;
   8687     }
   8688 #endif
   8689   return (asect->reloc_count + 1L) * sizeof (arelent *);
   8690 }
   8691 
   8692 /* Canonicalize the relocs.  */
   8693 
   8694 long
   8695 _bfd_elf_canonicalize_reloc (bfd *abfd,
   8696 			     sec_ptr section,
   8697 			     arelent **relptr,
   8698 			     asymbol **symbols)
   8699 {
   8700   arelent *tblptr;
   8701   unsigned int i;
   8702   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8703 
   8704   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   8705     return -1;
   8706 
   8707   tblptr = section->relocation;
   8708   for (i = 0; i < section->reloc_count; i++)
   8709     *relptr++ = tblptr++;
   8710 
   8711   *relptr = NULL;
   8712 
   8713   return section->reloc_count;
   8714 }
   8715 
   8716 long
   8717 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   8718 {
   8719   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8720   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   8721 
   8722   if (symcount >= 0)
   8723     abfd->symcount = symcount;
   8724   return symcount;
   8725 }
   8726 
   8727 long
   8728 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   8729 				      asymbol **allocation)
   8730 {
   8731   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8732   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   8733 
   8734   if (symcount >= 0)
   8735     abfd->dynsymcount = symcount;
   8736   return symcount;
   8737 }
   8738 
   8739 /* Return the size required for the dynamic reloc entries.  Any loadable
   8740    section that was actually installed in the BFD, and has type SHT_REL
   8741    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   8742    dynamic reloc section.  */
   8743 
   8744 long
   8745 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   8746 {
   8747   bfd_size_type count, ext_rel_size;
   8748   asection *s;
   8749 
   8750   if (elf_dynsymtab (abfd) == 0)
   8751     {
   8752       bfd_set_error (bfd_error_invalid_operation);
   8753       return -1;
   8754     }
   8755 
   8756   count = 1;
   8757   ext_rel_size = 0;
   8758   for (s = abfd->sections; s != NULL; s = s->next)
   8759     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8760 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8761 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8762       {
   8763 	ext_rel_size += s->size;
   8764 	if (ext_rel_size < s->size)
   8765 	  {
   8766 	    bfd_set_error (bfd_error_file_truncated);
   8767 	    return -1;
   8768 	  }
   8769 	count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8770 	if (count > LONG_MAX / sizeof (arelent *))
   8771 	  {
   8772 	    bfd_set_error (bfd_error_file_too_big);
   8773 	    return -1;
   8774 	  }
   8775       }
   8776   if (count > 1 && !bfd_write_p (abfd))
   8777     {
   8778       /* Sanity check reloc section sizes.  */
   8779       ufile_ptr filesize = bfd_get_file_size (abfd);
   8780       if (filesize != 0 && ext_rel_size > filesize)
   8781 	{
   8782 	  bfd_set_error (bfd_error_file_truncated);
   8783 	  return -1;
   8784 	}
   8785     }
   8786   return count * sizeof (arelent *);
   8787 }
   8788 
   8789 /* Canonicalize the dynamic relocation entries.  Note that we return the
   8790    dynamic relocations as a single block, although they are actually
   8791    associated with particular sections; the interface, which was
   8792    designed for SunOS style shared libraries, expects that there is only
   8793    one set of dynamic relocs.  Any loadable section that was actually
   8794    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   8795    dynamic symbol table, is considered to be a dynamic reloc section.  */
   8796 
   8797 long
   8798 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   8799 				     arelent **storage,
   8800 				     asymbol **syms)
   8801 {
   8802   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   8803   asection *s;
   8804   long ret;
   8805 
   8806   if (elf_dynsymtab (abfd) == 0)
   8807     {
   8808       bfd_set_error (bfd_error_invalid_operation);
   8809       return -1;
   8810     }
   8811 
   8812   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   8813   ret = 0;
   8814   for (s = abfd->sections; s != NULL; s = s->next)
   8815     {
   8816       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8817 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8818 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8819 	{
   8820 	  arelent *p;
   8821 	  long count, i;
   8822 
   8823 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   8824 	    return -1;
   8825 	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8826 	  p = s->relocation;
   8827 	  for (i = 0; i < count; i++)
   8828 	    *storage++ = p++;
   8829 	  ret += count;
   8830 	}
   8831     }
   8832 
   8833   *storage = NULL;
   8834 
   8835   return ret;
   8836 }
   8837 
   8838 /* Read in the version information.  */
   8840 
   8841 bool
   8842 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   8843 {
   8844   bfd_byte *contents = NULL;
   8845   unsigned int freeidx = 0;
   8846   size_t amt;
   8847 
   8848   if (elf_dynverref (abfd) != 0)
   8849     {
   8850       Elf_Internal_Shdr *hdr;
   8851       Elf_External_Verneed *everneed;
   8852       Elf_Internal_Verneed *iverneed;
   8853       unsigned int i;
   8854       bfd_byte *contents_end;
   8855 
   8856       hdr = &elf_tdata (abfd)->dynverref_hdr;
   8857 
   8858       if (hdr->sh_info == 0
   8859 	  || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   8860 	{
   8861 	error_return_bad_verref:
   8862 	  _bfd_error_handler
   8863 	    (_("%pB: .gnu.version_r invalid entry"), abfd);
   8864 	  bfd_set_error (bfd_error_bad_value);
   8865 	error_return_verref:
   8866 	  elf_tdata (abfd)->verref = NULL;
   8867 	  elf_tdata (abfd)->cverrefs = 0;
   8868 	  goto error_return;
   8869 	}
   8870 
   8871       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   8872 	goto error_return_verref;
   8873       contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   8874       if (contents == NULL)
   8875 	goto error_return_verref;
   8876 
   8877       if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
   8878 	{
   8879 	  bfd_set_error (bfd_error_file_too_big);
   8880 	  goto error_return_verref;
   8881 	}
   8882       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
   8883       if (elf_tdata (abfd)->verref == NULL)
   8884 	goto error_return_verref;
   8885 
   8886       BFD_ASSERT (sizeof (Elf_External_Verneed)
   8887 		  == sizeof (Elf_External_Vernaux));
   8888       contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
   8889       everneed = (Elf_External_Verneed *) contents;
   8890       iverneed = elf_tdata (abfd)->verref;
   8891       for (i = 0; i < hdr->sh_info; i++, iverneed++)
   8892 	{
   8893 	  Elf_External_Vernaux *evernaux;
   8894 	  Elf_Internal_Vernaux *ivernaux;
   8895 	  unsigned int j;
   8896 
   8897 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   8898 
   8899 	  iverneed->vn_bfd = abfd;
   8900 
   8901 	  iverneed->vn_filename =
   8902 	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8903 					     iverneed->vn_file);
   8904 	  if (iverneed->vn_filename == NULL)
   8905 	    goto error_return_bad_verref;
   8906 
   8907 	  if (iverneed->vn_cnt == 0)
   8908 	    iverneed->vn_auxptr = NULL;
   8909 	  else
   8910 	    {
   8911 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   8912 				     sizeof (Elf_Internal_Vernaux), &amt))
   8913 		{
   8914 		  bfd_set_error (bfd_error_file_too_big);
   8915 		  goto error_return_verref;
   8916 		}
   8917 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   8918 		bfd_alloc (abfd, amt);
   8919 	      if (iverneed->vn_auxptr == NULL)
   8920 		goto error_return_verref;
   8921 	    }
   8922 
   8923 	  if (iverneed->vn_aux
   8924 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8925 	    goto error_return_bad_verref;
   8926 
   8927 	  evernaux = ((Elf_External_Vernaux *)
   8928 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   8929 	  ivernaux = iverneed->vn_auxptr;
   8930 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   8931 	    {
   8932 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   8933 
   8934 	      ivernaux->vna_nodename =
   8935 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8936 						 ivernaux->vna_name);
   8937 	      if (ivernaux->vna_nodename == NULL)
   8938 		goto error_return_bad_verref;
   8939 
   8940 	      if (ivernaux->vna_other > freeidx)
   8941 		freeidx = ivernaux->vna_other;
   8942 
   8943 	      ivernaux->vna_nextptr = NULL;
   8944 	      if (ivernaux->vna_next == 0)
   8945 		{
   8946 		  iverneed->vn_cnt = j + 1;
   8947 		  break;
   8948 		}
   8949 	      if (j + 1 < iverneed->vn_cnt)
   8950 		ivernaux->vna_nextptr = ivernaux + 1;
   8951 
   8952 	      if (ivernaux->vna_next
   8953 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   8954 		goto error_return_bad_verref;
   8955 
   8956 	      evernaux = ((Elf_External_Vernaux *)
   8957 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   8958 	    }
   8959 
   8960 	  iverneed->vn_nextref = NULL;
   8961 	  if (iverneed->vn_next == 0)
   8962 	    break;
   8963 	  if (i + 1 < hdr->sh_info)
   8964 	    iverneed->vn_nextref = iverneed + 1;
   8965 
   8966 	  if (iverneed->vn_next
   8967 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8968 	    goto error_return_bad_verref;
   8969 
   8970 	  everneed = ((Elf_External_Verneed *)
   8971 		      ((bfd_byte *) everneed + iverneed->vn_next));
   8972 	}
   8973       elf_tdata (abfd)->cverrefs = i;
   8974 
   8975       free (contents);
   8976       contents = NULL;
   8977     }
   8978 
   8979   if (elf_dynverdef (abfd) != 0)
   8980     {
   8981       Elf_Internal_Shdr *hdr;
   8982       Elf_External_Verdef *everdef;
   8983       Elf_Internal_Verdef *iverdef;
   8984       Elf_Internal_Verdef *iverdefarr;
   8985       Elf_Internal_Verdef iverdefmem;
   8986       unsigned int i;
   8987       unsigned int maxidx;
   8988       bfd_byte *contents_end_def, *contents_end_aux;
   8989 
   8990       hdr = &elf_tdata (abfd)->dynverdef_hdr;
   8991 
   8992       if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
   8993 	{
   8994 	error_return_bad_verdef:
   8995 	  _bfd_error_handler
   8996 	    (_("%pB: .gnu.version_d invalid entry"), abfd);
   8997 	  bfd_set_error (bfd_error_bad_value);
   8998 	error_return_verdef:
   8999 	  elf_tdata (abfd)->verdef = NULL;
   9000 	  elf_tdata (abfd)->cverdefs = 0;
   9001 	  goto error_return;
   9002 	}
   9003 
   9004       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9005 	goto error_return_verdef;
   9006       contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   9007       if (contents == NULL)
   9008 	goto error_return_verdef;
   9009 
   9010       BFD_ASSERT (sizeof (Elf_External_Verdef)
   9011 		  >= sizeof (Elf_External_Verdaux));
   9012       contents_end_def = contents + hdr->sh_size
   9013 			 - sizeof (Elf_External_Verdef);
   9014       contents_end_aux = contents + hdr->sh_size
   9015 			 - sizeof (Elf_External_Verdaux);
   9016 
   9017       /* We know the number of entries in the section but not the maximum
   9018 	 index.  Therefore we have to run through all entries and find
   9019 	 the maximum.  */
   9020       everdef = (Elf_External_Verdef *) contents;
   9021       maxidx = 0;
   9022       for (i = 0; i < hdr->sh_info; ++i)
   9023 	{
   9024 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9025 
   9026 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9027 	    goto error_return_bad_verdef;
   9028 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9029 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9030 
   9031 	  if (iverdefmem.vd_next == 0)
   9032 	    break;
   9033 
   9034 	  if (iverdefmem.vd_next
   9035 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9036 	    goto error_return_bad_verdef;
   9037 
   9038 	  everdef = ((Elf_External_Verdef *)
   9039 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9040 	}
   9041 
   9042       if (default_imported_symver)
   9043 	{
   9044 	  if (freeidx > maxidx)
   9045 	    maxidx = ++freeidx;
   9046 	  else
   9047 	    freeidx = ++maxidx;
   9048 	}
   9049       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9050 	{
   9051 	  bfd_set_error (bfd_error_file_too_big);
   9052 	  goto error_return_verdef;
   9053 	}
   9054       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9055       if (elf_tdata (abfd)->verdef == NULL)
   9056 	goto error_return_verdef;
   9057 
   9058       elf_tdata (abfd)->cverdefs = maxidx;
   9059 
   9060       everdef = (Elf_External_Verdef *) contents;
   9061       iverdefarr = elf_tdata (abfd)->verdef;
   9062       for (i = 0; i < hdr->sh_info; i++)
   9063 	{
   9064 	  Elf_External_Verdaux *everdaux;
   9065 	  Elf_Internal_Verdaux *iverdaux;
   9066 	  unsigned int j;
   9067 
   9068 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9069 
   9070 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9071 	    goto error_return_bad_verdef;
   9072 
   9073 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9074 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9075 
   9076 	  iverdef->vd_bfd = abfd;
   9077 
   9078 	  if (iverdef->vd_cnt == 0)
   9079 	    iverdef->vd_auxptr = NULL;
   9080 	  else
   9081 	    {
   9082 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9083 				     sizeof (Elf_Internal_Verdaux), &amt))
   9084 		{
   9085 		  bfd_set_error (bfd_error_file_too_big);
   9086 		  goto error_return_verdef;
   9087 		}
   9088 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9089 		bfd_alloc (abfd, amt);
   9090 	      if (iverdef->vd_auxptr == NULL)
   9091 		goto error_return_verdef;
   9092 	    }
   9093 
   9094 	  if (iverdef->vd_aux
   9095 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9096 	    goto error_return_bad_verdef;
   9097 
   9098 	  everdaux = ((Elf_External_Verdaux *)
   9099 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9100 	  iverdaux = iverdef->vd_auxptr;
   9101 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9102 	    {
   9103 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9104 
   9105 	      iverdaux->vda_nodename =
   9106 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9107 						 iverdaux->vda_name);
   9108 	      if (iverdaux->vda_nodename == NULL)
   9109 		goto error_return_bad_verdef;
   9110 
   9111 	      iverdaux->vda_nextptr = NULL;
   9112 	      if (iverdaux->vda_next == 0)
   9113 		{
   9114 		  iverdef->vd_cnt = j + 1;
   9115 		  break;
   9116 		}
   9117 	      if (j + 1 < iverdef->vd_cnt)
   9118 		iverdaux->vda_nextptr = iverdaux + 1;
   9119 
   9120 	      if (iverdaux->vda_next
   9121 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9122 		goto error_return_bad_verdef;
   9123 
   9124 	      everdaux = ((Elf_External_Verdaux *)
   9125 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9126 	    }
   9127 
   9128 	  iverdef->vd_nodename = NULL;
   9129 	  if (iverdef->vd_cnt)
   9130 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9131 
   9132 	  iverdef->vd_nextdef = NULL;
   9133 	  if (iverdef->vd_next == 0)
   9134 	    break;
   9135 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9136 	    iverdef->vd_nextdef = iverdef + 1;
   9137 
   9138 	  everdef = ((Elf_External_Verdef *)
   9139 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9140 	}
   9141 
   9142       free (contents);
   9143       contents = NULL;
   9144     }
   9145   else if (default_imported_symver)
   9146     {
   9147       if (freeidx < 3)
   9148 	freeidx = 3;
   9149       else
   9150 	freeidx++;
   9151 
   9152       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9153 	{
   9154 	  bfd_set_error (bfd_error_file_too_big);
   9155 	  goto error_return;
   9156 	}
   9157       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9158       if (elf_tdata (abfd)->verdef == NULL)
   9159 	goto error_return;
   9160 
   9161       elf_tdata (abfd)->cverdefs = freeidx;
   9162     }
   9163 
   9164   /* Create a default version based on the soname.  */
   9165   if (default_imported_symver)
   9166     {
   9167       Elf_Internal_Verdef *iverdef;
   9168       Elf_Internal_Verdaux *iverdaux;
   9169 
   9170       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9171 
   9172       iverdef->vd_version = VER_DEF_CURRENT;
   9173       iverdef->vd_flags = 0;
   9174       iverdef->vd_ndx = freeidx;
   9175       iverdef->vd_cnt = 1;
   9176 
   9177       iverdef->vd_bfd = abfd;
   9178 
   9179       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9180       if (iverdef->vd_nodename == NULL)
   9181 	goto error_return_verdef;
   9182       iverdef->vd_nextdef = NULL;
   9183       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9184 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9185       if (iverdef->vd_auxptr == NULL)
   9186 	goto error_return_verdef;
   9187 
   9188       iverdaux = iverdef->vd_auxptr;
   9189       iverdaux->vda_nodename = iverdef->vd_nodename;
   9190     }
   9191 
   9192   return true;
   9193 
   9194  error_return:
   9195   free (contents);
   9196   return false;
   9197 }
   9198 
   9199 asymbol *
   9201 _bfd_elf_make_empty_symbol (bfd *abfd)
   9202 {
   9203   elf_symbol_type *newsym;
   9204 
   9205   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9206   if (!newsym)
   9207     return NULL;
   9208   newsym->symbol.the_bfd = abfd;
   9209   return &newsym->symbol;
   9210 }
   9211 
   9212 void
   9213 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9214 			  asymbol *symbol,
   9215 			  symbol_info *ret)
   9216 {
   9217   bfd_symbol_info (symbol, ret);
   9218 }
   9219 
   9220 /* Return whether a symbol name implies a local symbol.  Most targets
   9221    use this function for the is_local_label_name entry point, but some
   9222    override it.  */
   9223 
   9224 bool
   9225 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9226 			      const char *name)
   9227 {
   9228   /* Normal local symbols start with ``.L''.  */
   9229   if (name[0] == '.' && name[1] == 'L')
   9230     return true;
   9231 
   9232   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9233      DWARF debugging symbols starting with ``..''.  */
   9234   if (name[0] == '.' && name[1] == '.')
   9235     return true;
   9236 
   9237   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9238      emitting DWARF debugging output.  I suspect this is actually a
   9239      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9240      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9241      underscore to be emitted on some ELF targets).  For ease of use,
   9242      we treat such symbols as local.  */
   9243   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9244     return true;
   9245 
   9246   /* Treat assembler generated fake symbols, dollar local labels and
   9247      forward-backward labels (aka local labels) as locals.
   9248      These labels have the form:
   9249 
   9250        L0^A.*				       (fake symbols)
   9251 
   9252        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9253 
   9254      Versions which start with .L will have already been matched above,
   9255      so we only need to match the rest.  */
   9256   if (name[0] == 'L' && ISDIGIT (name[1]))
   9257     {
   9258       bool ret = false;
   9259       const char * p;
   9260       char c;
   9261 
   9262       for (p = name + 2; (c = *p); p++)
   9263 	{
   9264 	  if (c == 1 || c == 2)
   9265 	    {
   9266 	      if (c == 1 && p == name + 2)
   9267 		/* A fake symbol.  */
   9268 		return true;
   9269 
   9270 	      /* FIXME: We are being paranoid here and treating symbols like
   9271 		 L0^Bfoo as if there were non-local, on the grounds that the
   9272 		 assembler will never generate them.  But can any symbol
   9273 		 containing an ASCII value in the range 1-31 ever be anything
   9274 		 other than some kind of local ?  */
   9275 	      ret = true;
   9276 	    }
   9277 
   9278 	  if (! ISDIGIT (c))
   9279 	    {
   9280 	      ret = false;
   9281 	      break;
   9282 	    }
   9283 	}
   9284       return ret;
   9285     }
   9286 
   9287   return false;
   9288 }
   9289 
   9290 alent *
   9291 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9292 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9293 {
   9294   abort ();
   9295   return NULL;
   9296 }
   9297 
   9298 bool
   9299 _bfd_elf_set_arch_mach (bfd *abfd,
   9300 			enum bfd_architecture arch,
   9301 			unsigned long machine)
   9302 {
   9303   /* If this isn't the right architecture for this backend, and this
   9304      isn't the generic backend, fail.  */
   9305   if (arch != get_elf_backend_data (abfd)->arch
   9306       && arch != bfd_arch_unknown
   9307       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9308     return false;
   9309 
   9310   return bfd_default_set_arch_mach (abfd, arch, machine);
   9311 }
   9312 
   9313 /* Find the nearest line to a particular section and offset,
   9314    for error reporting.  */
   9315 
   9316 bool
   9317 _bfd_elf_find_nearest_line (bfd *abfd,
   9318 			    asymbol **symbols,
   9319 			    asection *section,
   9320 			    bfd_vma offset,
   9321 			    const char **filename_ptr,
   9322 			    const char **functionname_ptr,
   9323 			    unsigned int *line_ptr,
   9324 			    unsigned int *discriminator_ptr)
   9325 {
   9326   bool found;
   9327 
   9328   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   9329 				     filename_ptr, functionname_ptr,
   9330 				     line_ptr, discriminator_ptr,
   9331 				     dwarf_debug_sections,
   9332 				     &elf_tdata (abfd)->dwarf2_find_line_info))
   9333     return true;
   9334 
   9335   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   9336 				     filename_ptr, functionname_ptr, line_ptr))
   9337     {
   9338       if (!*functionname_ptr)
   9339 	_bfd_elf_find_function (abfd, symbols, section, offset,
   9340 				*filename_ptr ? NULL : filename_ptr,
   9341 				functionname_ptr);
   9342       return true;
   9343     }
   9344 
   9345   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   9346 					     &found, filename_ptr,
   9347 					     functionname_ptr, line_ptr,
   9348 					     &elf_tdata (abfd)->line_info))
   9349     return false;
   9350   if (found && (*functionname_ptr || *line_ptr))
   9351     return true;
   9352 
   9353   if (symbols == NULL)
   9354     return false;
   9355 
   9356   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   9357 				filename_ptr, functionname_ptr))
   9358     return false;
   9359 
   9360   *line_ptr = 0;
   9361   return true;
   9362 }
   9363 
   9364 /* Find the line for a symbol.  */
   9365 
   9366 bool
   9367 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   9368 		    const char **filename_ptr, unsigned int *line_ptr)
   9369 {
   9370   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   9371 					filename_ptr, NULL, line_ptr, NULL,
   9372 					dwarf_debug_sections,
   9373 					&elf_tdata (abfd)->dwarf2_find_line_info);
   9374 }
   9375 
   9376 /* After a call to bfd_find_nearest_line, successive calls to
   9377    bfd_find_inliner_info can be used to get source information about
   9378    each level of function inlining that terminated at the address
   9379    passed to bfd_find_nearest_line.  Currently this is only supported
   9380    for DWARF2 with appropriate DWARF3 extensions. */
   9381 
   9382 bool
   9383 _bfd_elf_find_inliner_info (bfd *abfd,
   9384 			    const char **filename_ptr,
   9385 			    const char **functionname_ptr,
   9386 			    unsigned int *line_ptr)
   9387 {
   9388   bool found;
   9389   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   9390 					 functionname_ptr, line_ptr,
   9391 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   9392   return found;
   9393 }
   9394 
   9395 int
   9396 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   9397 {
   9398   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9399   int ret = bed->s->sizeof_ehdr;
   9400 
   9401   if (!bfd_link_relocatable (info))
   9402     {
   9403       bfd_size_type phdr_size = elf_program_header_size (abfd);
   9404 
   9405       if (phdr_size == (bfd_size_type) -1)
   9406 	{
   9407 	  struct elf_segment_map *m;
   9408 
   9409 	  phdr_size = 0;
   9410 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   9411 	    phdr_size += bed->s->sizeof_phdr;
   9412 
   9413 	  if (phdr_size == 0)
   9414 	    phdr_size = get_program_header_size (abfd, info);
   9415 	}
   9416 
   9417       elf_program_header_size (abfd) = phdr_size;
   9418       ret += phdr_size;
   9419     }
   9420 
   9421   return ret;
   9422 }
   9423 
   9424 bool
   9425 _bfd_elf_set_section_contents (bfd *abfd,
   9426 			       sec_ptr section,
   9427 			       const void *location,
   9428 			       file_ptr offset,
   9429 			       bfd_size_type count)
   9430 {
   9431   Elf_Internal_Shdr *hdr;
   9432 
   9433   if (! abfd->output_has_begun
   9434       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   9435     return false;
   9436 
   9437   if (!count)
   9438     return true;
   9439 
   9440   hdr = &elf_section_data (section)->this_hdr;
   9441   if (hdr->sh_offset == (file_ptr) -1)
   9442     {
   9443       unsigned char *contents;
   9444 
   9445       if (bfd_section_is_ctf (section))
   9446 	/* Nothing to do with this section: the contents are generated
   9447 	   later.  */
   9448 	return true;
   9449 
   9450       if ((section->flags & SEC_ELF_COMPRESS) == 0)
   9451 	{
   9452 	  _bfd_error_handler
   9453 	    (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
   9454 	     abfd, section);
   9455 	  bfd_set_error (bfd_error_invalid_operation);
   9456 	  return false;
   9457 	}
   9458 
   9459       if ((offset + count) > hdr->sh_size)
   9460 	{
   9461 	  _bfd_error_handler
   9462 	    (_("%pB:%pA: error: attempting to write over the end of the section"),
   9463 	     abfd, section);
   9464 
   9465 	  bfd_set_error (bfd_error_invalid_operation);
   9466 	  return false;
   9467 	}
   9468 
   9469       contents = hdr->contents;
   9470       if (contents == NULL)
   9471 	{
   9472 	  _bfd_error_handler
   9473 	    (_("%pB:%pA: error: attempting to write section into an empty buffer"),
   9474 	     abfd, section);
   9475 
   9476 	  bfd_set_error (bfd_error_invalid_operation);
   9477 	  return false;
   9478 	}
   9479 
   9480       memcpy (contents + offset, location, count);
   9481       return true;
   9482     }
   9483 
   9484   return _bfd_generic_set_section_contents (abfd, section,
   9485 					    location, offset, count);
   9486 }
   9487 
   9488 bool
   9489 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   9490 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   9491 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   9492 {
   9493   abort ();
   9494   return false;
   9495 }
   9496 
   9497 /* Try to convert a non-ELF reloc into an ELF one.  */
   9498 
   9499 bool
   9500 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   9501 {
   9502   /* Check whether we really have an ELF howto.  */
   9503 
   9504   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   9505     {
   9506       bfd_reloc_code_real_type code;
   9507       reloc_howto_type *howto;
   9508 
   9509       /* Alien reloc: Try to determine its type to replace it with an
   9510 	 equivalent ELF reloc.  */
   9511 
   9512       if (areloc->howto->pc_relative)
   9513 	{
   9514 	  switch (areloc->howto->bitsize)
   9515 	    {
   9516 	    case 8:
   9517 	      code = BFD_RELOC_8_PCREL;
   9518 	      break;
   9519 	    case 12:
   9520 	      code = BFD_RELOC_12_PCREL;
   9521 	      break;
   9522 	    case 16:
   9523 	      code = BFD_RELOC_16_PCREL;
   9524 	      break;
   9525 	    case 24:
   9526 	      code = BFD_RELOC_24_PCREL;
   9527 	      break;
   9528 	    case 32:
   9529 	      code = BFD_RELOC_32_PCREL;
   9530 	      break;
   9531 	    case 64:
   9532 	      code = BFD_RELOC_64_PCREL;
   9533 	      break;
   9534 	    default:
   9535 	      goto fail;
   9536 	    }
   9537 
   9538 	  howto = bfd_reloc_type_lookup (abfd, code);
   9539 
   9540 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   9541 	    {
   9542 	      if (howto->pcrel_offset)
   9543 		areloc->addend += areloc->address;
   9544 	      else
   9545 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   9546 	    }
   9547 	}
   9548       else
   9549 	{
   9550 	  switch (areloc->howto->bitsize)
   9551 	    {
   9552 	    case 8:
   9553 	      code = BFD_RELOC_8;
   9554 	      break;
   9555 	    case 14:
   9556 	      code = BFD_RELOC_14;
   9557 	      break;
   9558 	    case 16:
   9559 	      code = BFD_RELOC_16;
   9560 	      break;
   9561 	    case 26:
   9562 	      code = BFD_RELOC_26;
   9563 	      break;
   9564 	    case 32:
   9565 	      code = BFD_RELOC_32;
   9566 	      break;
   9567 	    case 64:
   9568 	      code = BFD_RELOC_64;
   9569 	      break;
   9570 	    default:
   9571 	      goto fail;
   9572 	    }
   9573 
   9574 	  howto = bfd_reloc_type_lookup (abfd, code);
   9575 	}
   9576 
   9577       if (howto)
   9578 	areloc->howto = howto;
   9579       else
   9580 	goto fail;
   9581     }
   9582 
   9583   return true;
   9584 
   9585  fail:
   9586   /* xgettext:c-format */
   9587   _bfd_error_handler (_("%pB: %s unsupported"),
   9588 		      abfd, areloc->howto->name);
   9589   bfd_set_error (bfd_error_sorry);
   9590   return false;
   9591 }
   9592 
   9593 bool
   9594 _bfd_elf_close_and_cleanup (bfd *abfd)
   9595 {
   9596   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9597   if (tdata != NULL
   9598       && (bfd_get_format (abfd) == bfd_object
   9599 	  || bfd_get_format (abfd) == bfd_core))
   9600     {
   9601       if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
   9602 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   9603       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   9604     }
   9605 
   9606   return _bfd_generic_close_and_cleanup (abfd);
   9607 }
   9608 
   9609 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   9610    in the relocation's offset.  Thus we cannot allow any sort of sanity
   9611    range-checking to interfere.  There is nothing else to do in processing
   9612    this reloc.  */
   9613 
   9614 bfd_reloc_status_type
   9615 _bfd_elf_rel_vtable_reloc_fn
   9616   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   9617    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   9618    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   9619    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   9620 {
   9621   return bfd_reloc_ok;
   9622 }
   9623 
   9624 /* Elf core file support.  Much of this only works on native
   9626    toolchains, since we rely on knowing the
   9627    machine-dependent procfs structure in order to pick
   9628    out details about the corefile.  */
   9629 
   9630 #ifdef HAVE_SYS_PROCFS_H
   9631 # include <sys/procfs.h>
   9632 #endif
   9633 
   9634 /* Return a PID that identifies a "thread" for threaded cores, or the
   9635    PID of the main process for non-threaded cores.  */
   9636 
   9637 static int
   9638 elfcore_make_pid (bfd *abfd)
   9639 {
   9640   int pid;
   9641 
   9642   pid = elf_tdata (abfd)->core->lwpid;
   9643   if (pid == 0)
   9644     pid = elf_tdata (abfd)->core->pid;
   9645 
   9646   return pid;
   9647 }
   9648 
   9649 /* If there isn't a section called NAME, make one, using
   9650    data from SECT.  Note, this function will generate a
   9651    reference to NAME, so you shouldn't deallocate or
   9652    overwrite it.  */
   9653 
   9654 static bool
   9655 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   9656 {
   9657   asection *sect2;
   9658 
   9659   if (bfd_get_section_by_name (abfd, name) != NULL)
   9660     return true;
   9661 
   9662   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   9663   if (sect2 == NULL)
   9664     return false;
   9665 
   9666   sect2->size = sect->size;
   9667   sect2->filepos = sect->filepos;
   9668   sect2->alignment_power = sect->alignment_power;
   9669   return true;
   9670 }
   9671 
   9672 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   9673    actually creates up to two pseudosections:
   9674    - For the single-threaded case, a section named NAME, unless
   9675      such a section already exists.
   9676    - For the multi-threaded case, a section named "NAME/PID", where
   9677      PID is elfcore_make_pid (abfd).
   9678    Both pseudosections have identical contents.  */
   9679 bool
   9680 _bfd_elfcore_make_pseudosection (bfd *abfd,
   9681 				 char *name,
   9682 				 size_t size,
   9683 				 ufile_ptr filepos)
   9684 {
   9685   char buf[100];
   9686   char *threaded_name;
   9687   size_t len;
   9688   asection *sect;
   9689 
   9690   /* Build the section name.  */
   9691 
   9692   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   9693   len = strlen (buf) + 1;
   9694   threaded_name = (char *) bfd_alloc (abfd, len);
   9695   if (threaded_name == NULL)
   9696     return false;
   9697   memcpy (threaded_name, buf, len);
   9698 
   9699   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   9700 					     SEC_HAS_CONTENTS);
   9701   if (sect == NULL)
   9702     return false;
   9703   sect->size = size;
   9704   sect->filepos = filepos;
   9705   sect->alignment_power = 2;
   9706 
   9707   return elfcore_maybe_make_sect (abfd, name, sect);
   9708 }
   9709 
   9710 static bool
   9711 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   9712 				size_t offs)
   9713 {
   9714   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   9715 						       SEC_HAS_CONTENTS);
   9716 
   9717   if (sect == NULL)
   9718     return false;
   9719 
   9720   sect->size = note->descsz - offs;
   9721   sect->filepos = note->descpos + offs;
   9722   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   9723 
   9724   return true;
   9725 }
   9726 
   9727 /* prstatus_t exists on:
   9728      solaris 2.5+
   9729      linux 2.[01] + glibc
   9730      unixware 4.2
   9731 */
   9732 
   9733 #if defined (HAVE_PRSTATUS_T)
   9734 
   9735 static bool
   9736 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   9737 {
   9738   size_t size;
   9739   int offset;
   9740 
   9741   if (note->descsz == sizeof (prstatus_t))
   9742     {
   9743       prstatus_t prstat;
   9744 
   9745       size = sizeof (prstat.pr_reg);
   9746       offset   = offsetof (prstatus_t, pr_reg);
   9747       memcpy (&prstat, note->descdata, sizeof (prstat));
   9748 
   9749       /* Do not overwrite the core signal if it
   9750 	 has already been set by another thread.  */
   9751       if (elf_tdata (abfd)->core->signal == 0)
   9752 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9753       if (elf_tdata (abfd)->core->pid == 0)
   9754 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9755 
   9756       /* pr_who exists on:
   9757 	 solaris 2.5+
   9758 	 unixware 4.2
   9759 	 pr_who doesn't exist on:
   9760 	 linux 2.[01]
   9761 	 */
   9762 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   9763       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9764 #else
   9765       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9766 #endif
   9767     }
   9768 #if defined (HAVE_PRSTATUS32_T)
   9769   else if (note->descsz == sizeof (prstatus32_t))
   9770     {
   9771       /* 64-bit host, 32-bit corefile */
   9772       prstatus32_t prstat;
   9773 
   9774       size = sizeof (prstat.pr_reg);
   9775       offset   = offsetof (prstatus32_t, pr_reg);
   9776       memcpy (&prstat, note->descdata, sizeof (prstat));
   9777 
   9778       /* Do not overwrite the core signal if it
   9779 	 has already been set by another thread.  */
   9780       if (elf_tdata (abfd)->core->signal == 0)
   9781 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9782       if (elf_tdata (abfd)->core->pid == 0)
   9783 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9784 
   9785       /* pr_who exists on:
   9786 	 solaris 2.5+
   9787 	 unixware 4.2
   9788 	 pr_who doesn't exist on:
   9789 	 linux 2.[01]
   9790 	 */
   9791 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   9792       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9793 #else
   9794       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9795 #endif
   9796     }
   9797 #endif /* HAVE_PRSTATUS32_T */
   9798   else
   9799     {
   9800       /* Fail - we don't know how to handle any other
   9801 	 note size (ie. data object type).  */
   9802       return true;
   9803     }
   9804 
   9805   /* Make a ".reg/999" section and a ".reg" section.  */
   9806   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   9807 					  size, note->descpos + offset);
   9808 }
   9809 #endif /* defined (HAVE_PRSTATUS_T) */
   9810 
   9811 /* Create a pseudosection containing the exact contents of NOTE.  */
   9812 static bool
   9813 elfcore_make_note_pseudosection (bfd *abfd,
   9814 				 char *name,
   9815 				 Elf_Internal_Note *note)
   9816 {
   9817   return _bfd_elfcore_make_pseudosection (abfd, name,
   9818 					  note->descsz, note->descpos);
   9819 }
   9820 
   9821 /* There isn't a consistent prfpregset_t across platforms,
   9822    but it doesn't matter, because we don't have to pick this
   9823    data structure apart.  */
   9824 
   9825 static bool
   9826 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   9827 {
   9828   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   9829 }
   9830 
   9831 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   9832    type of NT_PRXFPREG.  Just include the whole note's contents
   9833    literally.  */
   9834 
   9835 static bool
   9836 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   9837 {
   9838   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   9839 }
   9840 
   9841 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   9842    with a note type of NT_X86_XSTATE.  Just include the whole note's
   9843    contents literally.  */
   9844 
   9845 static bool
   9846 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   9847 {
   9848   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   9849 }
   9850 
   9851 static bool
   9852 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   9853 {
   9854   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   9855 }
   9856 
   9857 static bool
   9858 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   9859 {
   9860   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   9861 }
   9862 
   9863 static bool
   9864 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   9865 {
   9866   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
   9867 }
   9868 
   9869 static bool
   9870 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   9871 {
   9872   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
   9873 }
   9874 
   9875 static bool
   9876 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   9877 {
   9878   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
   9879 }
   9880 
   9881 static bool
   9882 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   9883 {
   9884   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
   9885 }
   9886 
   9887 static bool
   9888 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   9889 {
   9890   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
   9891 }
   9892 
   9893 static bool
   9894 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   9895 {
   9896   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
   9897 }
   9898 
   9899 static bool
   9900 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   9901 {
   9902   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
   9903 }
   9904 
   9905 static bool
   9906 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   9907 {
   9908   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
   9909 }
   9910 
   9911 static bool
   9912 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   9913 {
   9914   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
   9915 }
   9916 
   9917 static bool
   9918 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   9919 {
   9920   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
   9921 }
   9922 
   9923 static bool
   9924 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   9925 {
   9926   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
   9927 }
   9928 
   9929 static bool
   9930 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   9931 {
   9932   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
   9933 }
   9934 
   9935 static bool
   9936 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   9937 {
   9938   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
   9939 }
   9940 
   9941 static bool
   9942 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   9943 {
   9944   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   9945 }
   9946 
   9947 static bool
   9948 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   9949 {
   9950   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   9951 }
   9952 
   9953 static bool
   9954 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   9955 {
   9956   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   9957 }
   9958 
   9959 static bool
   9960 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   9961 {
   9962   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   9963 }
   9964 
   9965 static bool
   9966 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   9967 {
   9968   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   9969 }
   9970 
   9971 static bool
   9972 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   9973 {
   9974   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   9975 }
   9976 
   9977 static bool
   9978 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   9979 {
   9980   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   9981 }
   9982 
   9983 static bool
   9984 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   9985 {
   9986   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   9987 }
   9988 
   9989 static bool
   9990 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   9991 {
   9992   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   9993 }
   9994 
   9995 static bool
   9996 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   9997 {
   9998   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   9999 }
   10000 
   10001 static bool
   10002 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   10003 {
   10004   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   10005 }
   10006 
   10007 static bool
   10008 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   10009 {
   10010   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   10011 }
   10012 
   10013 static bool
   10014 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10015 {
   10016   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   10017 }
   10018 
   10019 static bool
   10020 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10021 {
   10022   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   10023 }
   10024 
   10025 static bool
   10026 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10027 {
   10028   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   10029 }
   10030 
   10031 static bool
   10032 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10033 {
   10034   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   10035 }
   10036 
   10037 static bool
   10038 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10039 {
   10040   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   10041 }
   10042 
   10043 static bool
   10044 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10045 {
   10046   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
   10047 }
   10048 
   10049 static bool
   10050 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10051 {
   10052   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
   10053 }
   10054 
   10055 static bool
   10056 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10057 {
   10058   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
   10059 					  note);
   10060 }
   10061 
   10062 static bool
   10063 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10064 {
   10065   return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
   10066 }
   10067 
   10068 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10069    successful otherwise, return FALSE.  */
   10070 
   10071 static bool
   10072 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10073 {
   10074   return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
   10075 }
   10076 
   10077 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10078    successful otherwise, return FALSE.  */
   10079 
   10080 static bool
   10081 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10082 {
   10083   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10084 }
   10085 
   10086 static bool
   10087 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10088 {
   10089   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
   10090 }
   10091 
   10092 static bool
   10093 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10094 {
   10095   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
   10096 }
   10097 
   10098 static bool
   10099 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10100 {
   10101   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
   10102 }
   10103 
   10104 static bool
   10105 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10106 {
   10107   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
   10108 }
   10109 
   10110 #if defined (HAVE_PRPSINFO_T)
   10111 typedef prpsinfo_t   elfcore_psinfo_t;
   10112 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10113 typedef prpsinfo32_t elfcore_psinfo32_t;
   10114 #endif
   10115 #endif
   10116 
   10117 #if defined (HAVE_PSINFO_T)
   10118 typedef psinfo_t   elfcore_psinfo_t;
   10119 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10120 typedef psinfo32_t elfcore_psinfo32_t;
   10121 #endif
   10122 #endif
   10123 
   10124 /* return a malloc'ed copy of a string at START which is at
   10125    most MAX bytes long, possibly without a terminating '\0'.
   10126    the copy will always have a terminating '\0'.  */
   10127 
   10128 char *
   10129 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10130 {
   10131   char *dups;
   10132   char *end = (char *) memchr (start, '\0', max);
   10133   size_t len;
   10134 
   10135   if (end == NULL)
   10136     len = max;
   10137   else
   10138     len = end - start;
   10139 
   10140   dups = (char *) bfd_alloc (abfd, len + 1);
   10141   if (dups == NULL)
   10142     return NULL;
   10143 
   10144   memcpy (dups, start, len);
   10145   dups[len] = '\0';
   10146 
   10147   return dups;
   10148 }
   10149 
   10150 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10151 static bool
   10152 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10153 {
   10154   if (note->descsz == sizeof (elfcore_psinfo_t))
   10155     {
   10156       elfcore_psinfo_t psinfo;
   10157 
   10158       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10159 
   10160 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   10161       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10162 #endif
   10163       elf_tdata (abfd)->core->program
   10164 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10165 				sizeof (psinfo.pr_fname));
   10166 
   10167       elf_tdata (abfd)->core->command
   10168 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10169 				sizeof (psinfo.pr_psargs));
   10170     }
   10171 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10172   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10173     {
   10174       /* 64-bit host, 32-bit corefile */
   10175       elfcore_psinfo32_t psinfo;
   10176 
   10177       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10178 
   10179 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10180       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10181 #endif
   10182       elf_tdata (abfd)->core->program
   10183 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10184 				sizeof (psinfo.pr_fname));
   10185 
   10186       elf_tdata (abfd)->core->command
   10187 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10188 				sizeof (psinfo.pr_psargs));
   10189     }
   10190 #endif
   10191 
   10192   else
   10193     {
   10194       /* Fail - we don't know how to handle any other
   10195 	 note size (ie. data object type).  */
   10196       return true;
   10197     }
   10198 
   10199   /* Note that for some reason, a spurious space is tacked
   10200      onto the end of the args in some (at least one anyway)
   10201      implementations, so strip it off if it exists.  */
   10202 
   10203   {
   10204     char *command = elf_tdata (abfd)->core->command;
   10205     int n = strlen (command);
   10206 
   10207     if (0 < n && command[n - 1] == ' ')
   10208       command[n - 1] = '\0';
   10209   }
   10210 
   10211   return true;
   10212 }
   10213 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10214 
   10215 #if defined (HAVE_PSTATUS_T)
   10216 static bool
   10217 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10218 {
   10219   if (note->descsz == sizeof (pstatus_t)
   10220 #if defined (HAVE_PXSTATUS_T)
   10221       || note->descsz == sizeof (pxstatus_t)
   10222 #endif
   10223       )
   10224     {
   10225       pstatus_t pstat;
   10226 
   10227       memcpy (&pstat, note->descdata, sizeof (pstat));
   10228 
   10229       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10230     }
   10231 #if defined (HAVE_PSTATUS32_T)
   10232   else if (note->descsz == sizeof (pstatus32_t))
   10233     {
   10234       /* 64-bit host, 32-bit corefile */
   10235       pstatus32_t pstat;
   10236 
   10237       memcpy (&pstat, note->descdata, sizeof (pstat));
   10238 
   10239       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10240     }
   10241 #endif
   10242   /* Could grab some more details from the "representative"
   10243      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10244      NT_LWPSTATUS note, presumably.  */
   10245 
   10246   return true;
   10247 }
   10248 #endif /* defined (HAVE_PSTATUS_T) */
   10249 
   10250 #if defined (HAVE_LWPSTATUS_T)
   10251 static bool
   10252 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10253 {
   10254   lwpstatus_t lwpstat;
   10255   char buf[100];
   10256   char *name;
   10257   size_t len;
   10258   asection *sect;
   10259 
   10260   if (note->descsz != sizeof (lwpstat)
   10261 #if defined (HAVE_LWPXSTATUS_T)
   10262       && note->descsz != sizeof (lwpxstatus_t)
   10263 #endif
   10264       )
   10265     return true;
   10266 
   10267   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10268 
   10269   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10270   /* Do not overwrite the core signal if it has already been set by
   10271      another thread.  */
   10272   if (elf_tdata (abfd)->core->signal == 0)
   10273     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10274 
   10275   /* Make a ".reg/999" section.  */
   10276 
   10277   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   10278   len = strlen (buf) + 1;
   10279   name = bfd_alloc (abfd, len);
   10280   if (name == NULL)
   10281     return false;
   10282   memcpy (name, buf, len);
   10283 
   10284   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10285   if (sect == NULL)
   10286     return false;
   10287 
   10288 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10289   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   10290   sect->filepos = note->descpos
   10291     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   10292 #endif
   10293 
   10294 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10295   sect->size = sizeof (lwpstat.pr_reg);
   10296   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   10297 #endif
   10298 
   10299   sect->alignment_power = 2;
   10300 
   10301   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   10302     return false;
   10303 
   10304   /* Make a ".reg2/999" section */
   10305 
   10306   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   10307   len = strlen (buf) + 1;
   10308   name = bfd_alloc (abfd, len);
   10309   if (name == NULL)
   10310     return false;
   10311   memcpy (name, buf, len);
   10312 
   10313   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10314   if (sect == NULL)
   10315     return false;
   10316 
   10317 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10318   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   10319   sect->filepos = note->descpos
   10320     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   10321 #endif
   10322 
   10323 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   10324   sect->size = sizeof (lwpstat.pr_fpreg);
   10325   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   10326 #endif
   10327 
   10328   sect->alignment_power = 2;
   10329 
   10330   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   10331 }
   10332 #endif /* defined (HAVE_LWPSTATUS_T) */
   10333 
   10334 /* These constants, and the structure offsets used below, are defined by
   10335    Cygwin's core_dump.h */
   10336 #define NOTE_INFO_PROCESS  1
   10337 #define NOTE_INFO_THREAD   2
   10338 #define NOTE_INFO_MODULE   3
   10339 #define NOTE_INFO_MODULE64 4
   10340 
   10341 static bool
   10342 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   10343 {
   10344   char buf[30];
   10345   char *name;
   10346   size_t len;
   10347   unsigned int name_size;
   10348   asection *sect;
   10349   unsigned int type;
   10350   int is_active_thread;
   10351   bfd_vma base_addr;
   10352 
   10353   if (note->descsz < 4)
   10354     return true;
   10355 
   10356   if (! startswith (note->namedata, "win32"))
   10357     return true;
   10358 
   10359   type = bfd_get_32 (abfd, note->descdata);
   10360 
   10361   struct
   10362   {
   10363     const char *type_name;
   10364     unsigned long min_size;
   10365   } size_check[] =
   10366       {
   10367        { "NOTE_INFO_PROCESS", 12 },
   10368        { "NOTE_INFO_THREAD", 12 },
   10369        { "NOTE_INFO_MODULE", 12 },
   10370        { "NOTE_INFO_MODULE64", 16 },
   10371       };
   10372 
   10373   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   10374       return true;
   10375 
   10376   if (note->descsz < size_check[type - 1].min_size)
   10377     {
   10378       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
   10379                           abfd, size_check[type - 1].type_name, note->descsz);
   10380       return true;
   10381     }
   10382 
   10383   switch (type)
   10384     {
   10385     case NOTE_INFO_PROCESS:
   10386       /* FIXME: need to add ->core->command.  */
   10387       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   10388       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   10389       break;
   10390 
   10391     case NOTE_INFO_THREAD:
   10392       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   10393          structure. */
   10394       /* thread_info.tid */
   10395       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   10396 
   10397       len = strlen (buf) + 1;
   10398       name = (char *) bfd_alloc (abfd, len);
   10399       if (name == NULL)
   10400 	return false;
   10401 
   10402       memcpy (name, buf, len);
   10403 
   10404       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10405       if (sect == NULL)
   10406 	return false;
   10407 
   10408       /* sizeof (thread_info.thread_context) */
   10409       sect->size = note->descsz - 12;
   10410       /* offsetof (thread_info.thread_context) */
   10411       sect->filepos = note->descpos + 12;
   10412       sect->alignment_power = 2;
   10413 
   10414       /* thread_info.is_active_thread */
   10415       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   10416 
   10417       if (is_active_thread)
   10418 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   10419 	  return false;
   10420       break;
   10421 
   10422     case NOTE_INFO_MODULE:
   10423     case NOTE_INFO_MODULE64:
   10424       /* Make a ".module/xxxxxxxx" section.  */
   10425       if (type == NOTE_INFO_MODULE)
   10426         {
   10427           /* module_info.base_address */
   10428           base_addr = bfd_get_32 (abfd, note->descdata + 4);
   10429           sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   10430           /* module_info.module_name_size */
   10431           name_size = bfd_get_32 (abfd, note->descdata + 8);
   10432         }
   10433       else /* NOTE_INFO_MODULE64 */
   10434         {
   10435           /* module_info.base_address */
   10436           base_addr = bfd_get_64 (abfd, note->descdata + 4);
   10437           sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   10438           /* module_info.module_name_size */
   10439           name_size = bfd_get_32 (abfd, note->descdata + 12);
   10440         }
   10441 
   10442       len = strlen (buf) + 1;
   10443       name = (char *) bfd_alloc (abfd, len);
   10444       if (name == NULL)
   10445 	return false;
   10446 
   10447       memcpy (name, buf, len);
   10448 
   10449       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10450 
   10451       if (sect == NULL)
   10452 	return false;
   10453 
   10454       if (note->descsz < 12 + name_size)
   10455         {
   10456           _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
   10457                               abfd, note->descsz, name_size);
   10458           return true;
   10459         }
   10460 
   10461       sect->size = note->descsz;
   10462       sect->filepos = note->descpos;
   10463       sect->alignment_power = 2;
   10464       break;
   10465 
   10466     default:
   10467       return true;
   10468     }
   10469 
   10470   return true;
   10471 }
   10472 
   10473 static bool
   10474 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   10475 {
   10476   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10477 
   10478   switch (note->type)
   10479     {
   10480     default:
   10481       return true;
   10482 
   10483     case NT_PRSTATUS:
   10484       if (bed->elf_backend_grok_prstatus)
   10485 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   10486 	  return true;
   10487 #if defined (HAVE_PRSTATUS_T)
   10488       return elfcore_grok_prstatus (abfd, note);
   10489 #else
   10490       return true;
   10491 #endif
   10492 
   10493 #if defined (HAVE_PSTATUS_T)
   10494     case NT_PSTATUS:
   10495       return elfcore_grok_pstatus (abfd, note);
   10496 #endif
   10497 
   10498 #if defined (HAVE_LWPSTATUS_T)
   10499     case NT_LWPSTATUS:
   10500       return elfcore_grok_lwpstatus (abfd, note);
   10501 #endif
   10502 
   10503     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   10504       return elfcore_grok_prfpreg (abfd, note);
   10505 
   10506     case NT_WIN32PSTATUS:
   10507       return elfcore_grok_win32pstatus (abfd, note);
   10508 
   10509     case NT_PRXFPREG:		/* Linux SSE extension */
   10510       if (note->namesz == 6
   10511 	  && strcmp (note->namedata, "LINUX") == 0)
   10512 	return elfcore_grok_prxfpreg (abfd, note);
   10513       else
   10514 	return true;
   10515 
   10516     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   10517       if (note->namesz == 6
   10518 	  && strcmp (note->namedata, "LINUX") == 0)
   10519 	return elfcore_grok_xstatereg (abfd, note);
   10520       else
   10521 	return true;
   10522 
   10523     case NT_PPC_VMX:
   10524       if (note->namesz == 6
   10525 	  && strcmp (note->namedata, "LINUX") == 0)
   10526 	return elfcore_grok_ppc_vmx (abfd, note);
   10527       else
   10528 	return true;
   10529 
   10530     case NT_PPC_VSX:
   10531       if (note->namesz == 6
   10532 	  && strcmp (note->namedata, "LINUX") == 0)
   10533 	return elfcore_grok_ppc_vsx (abfd, note);
   10534       else
   10535 	return true;
   10536 
   10537     case NT_PPC_TAR:
   10538       if (note->namesz == 6
   10539 	  && strcmp (note->namedata, "LINUX") == 0)
   10540 	return elfcore_grok_ppc_tar (abfd, note);
   10541       else
   10542 	return true;
   10543 
   10544     case NT_PPC_PPR:
   10545       if (note->namesz == 6
   10546 	  && strcmp (note->namedata, "LINUX") == 0)
   10547 	return elfcore_grok_ppc_ppr (abfd, note);
   10548       else
   10549 	return true;
   10550 
   10551     case NT_PPC_DSCR:
   10552       if (note->namesz == 6
   10553 	  && strcmp (note->namedata, "LINUX") == 0)
   10554 	return elfcore_grok_ppc_dscr (abfd, note);
   10555       else
   10556 	return true;
   10557 
   10558     case NT_PPC_EBB:
   10559       if (note->namesz == 6
   10560 	  && strcmp (note->namedata, "LINUX") == 0)
   10561 	return elfcore_grok_ppc_ebb (abfd, note);
   10562       else
   10563 	return true;
   10564 
   10565     case NT_PPC_PMU:
   10566       if (note->namesz == 6
   10567 	  && strcmp (note->namedata, "LINUX") == 0)
   10568 	return elfcore_grok_ppc_pmu (abfd, note);
   10569       else
   10570 	return true;
   10571 
   10572     case NT_PPC_TM_CGPR:
   10573       if (note->namesz == 6
   10574 	  && strcmp (note->namedata, "LINUX") == 0)
   10575 	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   10576       else
   10577 	return true;
   10578 
   10579     case NT_PPC_TM_CFPR:
   10580       if (note->namesz == 6
   10581 	  && strcmp (note->namedata, "LINUX") == 0)
   10582 	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   10583       else
   10584 	return true;
   10585 
   10586     case NT_PPC_TM_CVMX:
   10587       if (note->namesz == 6
   10588 	  && strcmp (note->namedata, "LINUX") == 0)
   10589 	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   10590       else
   10591 	return true;
   10592 
   10593     case NT_PPC_TM_CVSX:
   10594       if (note->namesz == 6
   10595 	  && strcmp (note->namedata, "LINUX") == 0)
   10596 	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   10597       else
   10598 	return true;
   10599 
   10600     case NT_PPC_TM_SPR:
   10601       if (note->namesz == 6
   10602 	  && strcmp (note->namedata, "LINUX") == 0)
   10603 	return elfcore_grok_ppc_tm_spr (abfd, note);
   10604       else
   10605 	return true;
   10606 
   10607     case NT_PPC_TM_CTAR:
   10608       if (note->namesz == 6
   10609 	  && strcmp (note->namedata, "LINUX") == 0)
   10610 	return elfcore_grok_ppc_tm_ctar (abfd, note);
   10611       else
   10612 	return true;
   10613 
   10614     case NT_PPC_TM_CPPR:
   10615       if (note->namesz == 6
   10616 	  && strcmp (note->namedata, "LINUX") == 0)
   10617 	return elfcore_grok_ppc_tm_cppr (abfd, note);
   10618       else
   10619 	return true;
   10620 
   10621     case NT_PPC_TM_CDSCR:
   10622       if (note->namesz == 6
   10623 	  && strcmp (note->namedata, "LINUX") == 0)
   10624 	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   10625       else
   10626 	return true;
   10627 
   10628     case NT_S390_HIGH_GPRS:
   10629       if (note->namesz == 6
   10630 	  && strcmp (note->namedata, "LINUX") == 0)
   10631 	return elfcore_grok_s390_high_gprs (abfd, note);
   10632       else
   10633 	return true;
   10634 
   10635     case NT_S390_TIMER:
   10636       if (note->namesz == 6
   10637 	  && strcmp (note->namedata, "LINUX") == 0)
   10638 	return elfcore_grok_s390_timer (abfd, note);
   10639       else
   10640 	return true;
   10641 
   10642     case NT_S390_TODCMP:
   10643       if (note->namesz == 6
   10644 	  && strcmp (note->namedata, "LINUX") == 0)
   10645 	return elfcore_grok_s390_todcmp (abfd, note);
   10646       else
   10647 	return true;
   10648 
   10649     case NT_S390_TODPREG:
   10650       if (note->namesz == 6
   10651 	  && strcmp (note->namedata, "LINUX") == 0)
   10652 	return elfcore_grok_s390_todpreg (abfd, note);
   10653       else
   10654 	return true;
   10655 
   10656     case NT_S390_CTRS:
   10657       if (note->namesz == 6
   10658 	  && strcmp (note->namedata, "LINUX") == 0)
   10659 	return elfcore_grok_s390_ctrs (abfd, note);
   10660       else
   10661 	return true;
   10662 
   10663     case NT_S390_PREFIX:
   10664       if (note->namesz == 6
   10665 	  && strcmp (note->namedata, "LINUX") == 0)
   10666 	return elfcore_grok_s390_prefix (abfd, note);
   10667       else
   10668 	return true;
   10669 
   10670     case NT_S390_LAST_BREAK:
   10671       if (note->namesz == 6
   10672 	  && strcmp (note->namedata, "LINUX") == 0)
   10673 	return elfcore_grok_s390_last_break (abfd, note);
   10674       else
   10675 	return true;
   10676 
   10677     case NT_S390_SYSTEM_CALL:
   10678       if (note->namesz == 6
   10679 	  && strcmp (note->namedata, "LINUX") == 0)
   10680 	return elfcore_grok_s390_system_call (abfd, note);
   10681       else
   10682 	return true;
   10683 
   10684     case NT_S390_TDB:
   10685       if (note->namesz == 6
   10686 	  && strcmp (note->namedata, "LINUX") == 0)
   10687 	return elfcore_grok_s390_tdb (abfd, note);
   10688       else
   10689 	return true;
   10690 
   10691     case NT_S390_VXRS_LOW:
   10692       if (note->namesz == 6
   10693 	  && strcmp (note->namedata, "LINUX") == 0)
   10694 	return elfcore_grok_s390_vxrs_low (abfd, note);
   10695       else
   10696 	return true;
   10697 
   10698     case NT_S390_VXRS_HIGH:
   10699       if (note->namesz == 6
   10700 	  && strcmp (note->namedata, "LINUX") == 0)
   10701 	return elfcore_grok_s390_vxrs_high (abfd, note);
   10702       else
   10703 	return true;
   10704 
   10705     case NT_S390_GS_CB:
   10706       if (note->namesz == 6
   10707 	  && strcmp (note->namedata, "LINUX") == 0)
   10708 	return elfcore_grok_s390_gs_cb (abfd, note);
   10709       else
   10710 	return true;
   10711 
   10712     case NT_S390_GS_BC:
   10713       if (note->namesz == 6
   10714 	  && strcmp (note->namedata, "LINUX") == 0)
   10715 	return elfcore_grok_s390_gs_bc (abfd, note);
   10716       else
   10717 	return true;
   10718 
   10719     case NT_ARC_V2:
   10720       if (note->namesz == 6
   10721 	  && strcmp (note->namedata, "LINUX") == 0)
   10722 	return elfcore_grok_arc_v2 (abfd, note);
   10723       else
   10724 	return true;
   10725 
   10726     case NT_ARM_VFP:
   10727       if (note->namesz == 6
   10728 	  && strcmp (note->namedata, "LINUX") == 0)
   10729 	return elfcore_grok_arm_vfp (abfd, note);
   10730       else
   10731 	return true;
   10732 
   10733     case NT_ARM_TLS:
   10734       if (note->namesz == 6
   10735 	  && strcmp (note->namedata, "LINUX") == 0)
   10736 	return elfcore_grok_aarch_tls (abfd, note);
   10737       else
   10738 	return true;
   10739 
   10740     case NT_ARM_HW_BREAK:
   10741       if (note->namesz == 6
   10742 	  && strcmp (note->namedata, "LINUX") == 0)
   10743 	return elfcore_grok_aarch_hw_break (abfd, note);
   10744       else
   10745 	return true;
   10746 
   10747     case NT_ARM_HW_WATCH:
   10748       if (note->namesz == 6
   10749 	  && strcmp (note->namedata, "LINUX") == 0)
   10750 	return elfcore_grok_aarch_hw_watch (abfd, note);
   10751       else
   10752 	return true;
   10753 
   10754     case NT_ARM_SVE:
   10755       if (note->namesz == 6
   10756 	  && strcmp (note->namedata, "LINUX") == 0)
   10757 	return elfcore_grok_aarch_sve (abfd, note);
   10758       else
   10759 	return true;
   10760 
   10761     case NT_ARM_PAC_MASK:
   10762       if (note->namesz == 6
   10763 	  && strcmp (note->namedata, "LINUX") == 0)
   10764 	return elfcore_grok_aarch_pauth (abfd, note);
   10765       else
   10766 	return true;
   10767 
   10768     case NT_ARM_TAGGED_ADDR_CTRL:
   10769       if (note->namesz == 6
   10770 	  && strcmp (note->namedata, "LINUX") == 0)
   10771 	return elfcore_grok_aarch_mte (abfd, note);
   10772       else
   10773 	return true;
   10774 
   10775     case NT_GDB_TDESC:
   10776       if (note->namesz == 4
   10777           && strcmp (note->namedata, "GDB") == 0)
   10778         return elfcore_grok_gdb_tdesc (abfd, note);
   10779       else
   10780         return true;
   10781 
   10782     case NT_RISCV_CSR:
   10783       if (note->namesz == 4
   10784           && strcmp (note->namedata, "GDB") == 0)
   10785         return elfcore_grok_riscv_csr (abfd, note);
   10786       else
   10787 	return true;
   10788 
   10789     case NT_LARCH_CPUCFG:
   10790       if (note->namesz == 6
   10791 	  && strcmp (note->namedata, "LINUX") == 0)
   10792 	return elfcore_grok_loongarch_cpucfg (abfd, note);
   10793       else
   10794 	return true;
   10795 
   10796     case NT_LARCH_LBT:
   10797       if (note->namesz == 6
   10798 	  && strcmp (note->namedata, "LINUX") == 0)
   10799 	return elfcore_grok_loongarch_lbt (abfd, note);
   10800       else
   10801 	return true;
   10802 
   10803     case NT_LARCH_LSX:
   10804       if (note->namesz == 6
   10805 	  && strcmp (note->namedata, "LINUX") == 0)
   10806 	return elfcore_grok_loongarch_lsx (abfd, note);
   10807       else
   10808 	return true;
   10809 
   10810     case NT_LARCH_LASX:
   10811       if (note->namesz == 6
   10812 	  && strcmp (note->namedata, "LINUX") == 0)
   10813 	return elfcore_grok_loongarch_lasx (abfd, note);
   10814       else
   10815 	return true;
   10816 
   10817     case NT_PRPSINFO:
   10818     case NT_PSINFO:
   10819       if (bed->elf_backend_grok_psinfo)
   10820 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   10821 	  return true;
   10822 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10823       return elfcore_grok_psinfo (abfd, note);
   10824 #else
   10825       return true;
   10826 #endif
   10827 
   10828     case NT_AUXV:
   10829       return elfcore_make_auxv_note_section (abfd, note, 0);
   10830 
   10831     case NT_FILE:
   10832       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   10833 					      note);
   10834 
   10835     case NT_SIGINFO:
   10836       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   10837 					      note);
   10838 
   10839     }
   10840 }
   10841 
   10842 static bool
   10843 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   10844 {
   10845   struct bfd_build_id* build_id;
   10846 
   10847   if (note->descsz == 0)
   10848     return false;
   10849 
   10850   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   10851   if (build_id == NULL)
   10852     return false;
   10853 
   10854   build_id->size = note->descsz;
   10855   memcpy (build_id->data, note->descdata, note->descsz);
   10856   abfd->build_id = build_id;
   10857 
   10858   return true;
   10859 }
   10860 
   10861 static bool
   10862 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   10863 {
   10864   switch (note->type)
   10865     {
   10866     default:
   10867       return true;
   10868 
   10869     case NT_GNU_PROPERTY_TYPE_0:
   10870       return _bfd_elf_parse_gnu_properties (abfd, note);
   10871 
   10872     case NT_GNU_BUILD_ID:
   10873       return elfobj_grok_gnu_build_id (abfd, note);
   10874     }
   10875 }
   10876 
   10877 static bool
   10878 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   10879 {
   10880   struct sdt_note *cur =
   10881     (struct sdt_note *) bfd_alloc (abfd,
   10882 				   sizeof (struct sdt_note) + note->descsz);
   10883 
   10884   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   10885   cur->size = (bfd_size_type) note->descsz;
   10886   memcpy (cur->data, note->descdata, note->descsz);
   10887 
   10888   elf_tdata (abfd)->sdt_note_head = cur;
   10889 
   10890   return true;
   10891 }
   10892 
   10893 static bool
   10894 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   10895 {
   10896   switch (note->type)
   10897     {
   10898     case NT_STAPSDT:
   10899       return elfobj_grok_stapsdt_note_1 (abfd, note);
   10900 
   10901     default:
   10902       return true;
   10903     }
   10904 }
   10905 
   10906 static bool
   10907 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10908 {
   10909   size_t offset;
   10910 
   10911   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10912     {
   10913     case ELFCLASS32:
   10914       if (note->descsz < 108)
   10915 	return false;
   10916       break;
   10917 
   10918     case ELFCLASS64:
   10919       if (note->descsz < 120)
   10920 	return false;
   10921       break;
   10922 
   10923     default:
   10924       return false;
   10925     }
   10926 
   10927   /* Check for version 1 in pr_version.  */
   10928   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10929     return false;
   10930 
   10931   offset = 4;
   10932 
   10933   /* Skip over pr_psinfosz. */
   10934   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   10935     offset += 4;
   10936   else
   10937     {
   10938       offset += 4;	/* Padding before pr_psinfosz. */
   10939       offset += 8;
   10940     }
   10941 
   10942   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   10943   elf_tdata (abfd)->core->program
   10944     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   10945   offset += 17;
   10946 
   10947   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   10948   elf_tdata (abfd)->core->command
   10949     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   10950   offset += 81;
   10951 
   10952   /* Padding before pr_pid.  */
   10953   offset += 2;
   10954 
   10955   /* The pr_pid field was added in version "1a".  */
   10956   if (note->descsz < offset + 4)
   10957     return true;
   10958 
   10959   elf_tdata (abfd)->core->pid
   10960     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10961 
   10962   return true;
   10963 }
   10964 
   10965 static bool
   10966 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10967 {
   10968   size_t offset;
   10969   size_t size;
   10970   size_t min_size;
   10971 
   10972   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   10973      Also compute minimum size of this note.  */
   10974   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10975     {
   10976     case ELFCLASS32:
   10977       offset = 4 + 4;
   10978       min_size = offset + (4 * 2) + 4 + 4 + 4;
   10979       break;
   10980 
   10981     case ELFCLASS64:
   10982       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   10983       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   10984       break;
   10985 
   10986     default:
   10987       return false;
   10988     }
   10989 
   10990   if (note->descsz < min_size)
   10991     return false;
   10992 
   10993   /* Check for version 1 in pr_version.  */
   10994   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10995     return false;
   10996 
   10997   /* Extract size of pr_reg from pr_gregsetsz.  */
   10998   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   10999   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11000     {
   11001       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11002       offset += 4 * 2;
   11003     }
   11004   else
   11005     {
   11006       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   11007       offset += 8 * 2;
   11008     }
   11009 
   11010   /* Skip over pr_osreldate.  */
   11011   offset += 4;
   11012 
   11013   /* Read signal from pr_cursig.  */
   11014   if (elf_tdata (abfd)->core->signal == 0)
   11015     elf_tdata (abfd)->core->signal
   11016       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11017   offset += 4;
   11018 
   11019   /* Read TID from pr_pid.  */
   11020   elf_tdata (abfd)->core->lwpid
   11021       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11022   offset += 4;
   11023 
   11024   /* Padding before pr_reg.  */
   11025   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11026     offset += 4;
   11027 
   11028   /* Make sure that there is enough data remaining in the note.  */
   11029   if ((note->descsz - offset) < size)
   11030     return false;
   11031 
   11032   /* Make a ".reg/999" section and a ".reg" section.  */
   11033   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   11034 					  size, note->descpos + offset);
   11035 }
   11036 
   11037 static bool
   11038 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11039 {
   11040   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11041 
   11042   switch (note->type)
   11043     {
   11044     case NT_PRSTATUS:
   11045       if (bed->elf_backend_grok_freebsd_prstatus)
   11046 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11047 	  return true;
   11048       return elfcore_grok_freebsd_prstatus (abfd, note);
   11049 
   11050     case NT_FPREGSET:
   11051       return elfcore_grok_prfpreg (abfd, note);
   11052 
   11053     case NT_PRPSINFO:
   11054       return elfcore_grok_freebsd_psinfo (abfd, note);
   11055 
   11056     case NT_FREEBSD_THRMISC:
   11057       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11058 
   11059     case NT_FREEBSD_PROCSTAT_PROC:
   11060       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11061 					      note);
   11062 
   11063     case NT_FREEBSD_PROCSTAT_FILES:
   11064       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11065 					      note);
   11066 
   11067     case NT_FREEBSD_PROCSTAT_VMMAP:
   11068       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11069 					      note);
   11070 
   11071     case NT_FREEBSD_PROCSTAT_AUXV:
   11072       return elfcore_make_auxv_note_section (abfd, note, 4);
   11073 
   11074     case NT_FREEBSD_X86_SEGBASES:
   11075       return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
   11076 
   11077     case NT_X86_XSTATE:
   11078       return elfcore_grok_xstatereg (abfd, note);
   11079 
   11080     case NT_FREEBSD_PTLWPINFO:
   11081       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11082 					      note);
   11083 
   11084     case NT_ARM_TLS:
   11085       return elfcore_grok_aarch_tls (abfd, note);
   11086 
   11087     case NT_ARM_VFP:
   11088       return elfcore_grok_arm_vfp (abfd, note);
   11089 
   11090     default:
   11091       return true;
   11092     }
   11093 }
   11094 
   11095 static bool
   11096 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11097 {
   11098   char *cp;
   11099 
   11100   cp = strchr (note->namedata, '@');
   11101   if (cp != NULL)
   11102     {
   11103       *lwpidp = atoi(cp + 1);
   11104       return true;
   11105     }
   11106   return false;
   11107 }
   11108 
   11109 static bool
   11110 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11111 {
   11112   if (note->descsz <= 0x7c + 31)
   11113     return false;
   11114 
   11115   /* Signal number at offset 0x08. */
   11116   elf_tdata (abfd)->core->signal
   11117     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11118 
   11119   /* Process ID at offset 0x50. */
   11120   elf_tdata (abfd)->core->pid
   11121     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11122 
   11123   /* Command name at 0x7c (max 32 bytes, including nul). */
   11124   elf_tdata (abfd)->core->command
   11125     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11126 
   11127   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11128 					  note);
   11129 }
   11130 
   11131 static bool
   11132 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11133 {
   11134   int lwp;
   11135 
   11136   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11137     elf_tdata (abfd)->core->lwpid = lwp;
   11138 
   11139   switch (note->type)
   11140     {
   11141     case NT_NETBSDCORE_PROCINFO:
   11142       /* NetBSD-specific core "procinfo".  Note that we expect to
   11143 	 find this note before any of the others, which is fine,
   11144 	 since the kernel writes this note out first when it
   11145 	 creates a core file.  */
   11146       return elfcore_grok_netbsd_procinfo (abfd, note);
   11147     case NT_NETBSDCORE_AUXV:
   11148       /* NetBSD-specific Elf Auxiliary Vector data. */
   11149       return elfcore_make_auxv_note_section (abfd, note, 0);
   11150     case NT_NETBSDCORE_LWPSTATUS:
   11151       return elfcore_make_note_pseudosection (abfd,
   11152 					      ".note.netbsdcore.lwpstatus",
   11153 					      note);
   11154     default:
   11155       break;
   11156     }
   11157 
   11158   /* As of March 2020 there are no other machine-independent notes
   11159      defined for NetBSD core files.  If the note type is less
   11160      than the start of the machine-dependent note types, we don't
   11161      understand it.  */
   11162 
   11163   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11164     return true;
   11165 
   11166 
   11167   switch (bfd_get_arch (abfd))
   11168     {
   11169       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11170 	 PT_GETFPREGS == mach+2.  */
   11171 
   11172     case bfd_arch_aarch64:
   11173     case bfd_arch_alpha:
   11174     case bfd_arch_sparc:
   11175       switch (note->type)
   11176 	{
   11177 	case NT_NETBSDCORE_FIRSTMACH+0:
   11178 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11179 
   11180 	case NT_NETBSDCORE_FIRSTMACH+2:
   11181 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11182 
   11183 	default:
   11184 	  return true;
   11185 	}
   11186 
   11187       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11188 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11189 	 structure which lacks GBR.  */
   11190 
   11191     case bfd_arch_sh:
   11192       switch (note->type)
   11193 	{
   11194 	case NT_NETBSDCORE_FIRSTMACH+3:
   11195 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11196 
   11197 	case NT_NETBSDCORE_FIRSTMACH+5:
   11198 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11199 
   11200 	default:
   11201 	  return true;
   11202 	}
   11203 
   11204       /* On all other arch's, PT_GETREGS == mach+1 and
   11205 	 PT_GETFPREGS == mach+3.  */
   11206 
   11207     default:
   11208       switch (note->type)
   11209 	{
   11210 	case NT_NETBSDCORE_FIRSTMACH+1:
   11211 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11212 
   11213 	case NT_NETBSDCORE_FIRSTMACH+3:
   11214 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11215 
   11216 	default:
   11217 	  return true;
   11218 	}
   11219     }
   11220     /* NOTREACHED */
   11221 }
   11222 
   11223 static bool
   11224 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11225 {
   11226   if (note->descsz <= 0x48 + 31)
   11227     return false;
   11228 
   11229   /* Signal number at offset 0x08. */
   11230   elf_tdata (abfd)->core->signal
   11231     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11232 
   11233   /* Process ID at offset 0x20. */
   11234   elf_tdata (abfd)->core->pid
   11235     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11236 
   11237   /* Command name at 0x48 (max 32 bytes, including nul). */
   11238   elf_tdata (abfd)->core->command
   11239     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11240 
   11241   return true;
   11242 }
   11243 
   11244 /* Processes Solaris's process status note.
   11245    sig_off ~ offsetof(prstatus_t, pr_cursig)
   11246    pid_off ~ offsetof(prstatus_t, pr_pid)
   11247    lwpid_off ~ offsetof(prstatus_t, pr_who)
   11248    gregset_size ~ sizeof(gregset_t)
   11249    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   11250 
   11251 static bool
   11252 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   11253 			       int pid_off, int lwpid_off, size_t gregset_size,
   11254 			       size_t gregset_offset)
   11255 {
   11256   asection *sect = NULL;
   11257   elf_tdata (abfd)->core->signal
   11258     = bfd_get_16 (abfd, note->descdata + sig_off);
   11259   elf_tdata (abfd)->core->pid
   11260     = bfd_get_32 (abfd, note->descdata + pid_off);
   11261   elf_tdata (abfd)->core->lwpid
   11262     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   11263 
   11264   sect = bfd_get_section_by_name (abfd, ".reg");
   11265   if (sect != NULL)
   11266     sect->size = gregset_size;
   11267 
   11268   return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11269 					  note->descpos + gregset_offset);
   11270 }
   11271 
   11272 /* Gets program and arguments from a core.
   11273    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   11274    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   11275 
   11276 static bool
   11277 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   11278 			  int prog_off, int comm_off)
   11279 {
   11280   elf_tdata (abfd)->core->program
   11281     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   11282   elf_tdata (abfd)->core->command
   11283     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   11284 
   11285   return true;
   11286 }
   11287 
   11288 /* Processes Solaris's LWP status note.
   11289    gregset_size ~ sizeof(gregset_t)
   11290    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   11291    fpregset_size ~ sizeof(fpregset_t)
   11292    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   11293 
   11294 static bool
   11295 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   11296 				size_t gregset_size, int gregset_off,
   11297 				size_t fpregset_size, int fpregset_off)
   11298 {
   11299   asection *sect = NULL;
   11300   char reg2_section_name[16] = { 0 };
   11301 
   11302   (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
   11303 		   elf_tdata (abfd)->core->lwpid);
   11304 
   11305   /* offsetof(lwpstatus_t, pr_lwpid) */
   11306   elf_tdata (abfd)->core->lwpid
   11307     = bfd_get_32 (abfd, note->descdata + 4);
   11308   /* offsetof(lwpstatus_t, pr_cursig) */
   11309   elf_tdata (abfd)->core->signal
   11310     = bfd_get_16 (abfd, note->descdata + 12);
   11311 
   11312   sect = bfd_get_section_by_name (abfd, ".reg");
   11313   if (sect != NULL)
   11314     sect->size = gregset_size;
   11315   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11316 					     note->descpos + gregset_off))
   11317     return false;
   11318 
   11319   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   11320   if (sect != NULL)
   11321     {
   11322       sect->size = fpregset_size;
   11323       sect->filepos = note->descpos + fpregset_off;
   11324       sect->alignment_power = 2;
   11325     }
   11326   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
   11327 					     note->descpos + fpregset_off))
   11328     return false;
   11329 
   11330   return true;
   11331 }
   11332 
   11333 static bool
   11334 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   11335 {
   11336   if (note == NULL)
   11337     return false;
   11338 
   11339   /* core files are identified as 32- or 64-bit, SPARC or x86,
   11340      by the size of the descsz which matches the sizeof()
   11341      the type appropriate for that note type (e.g., prstatus_t for
   11342      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   11343      on Solaris. The core file bitness may differ from the bitness of
   11344      gdb itself, so fixed values are used instead of sizeof().
   11345      Appropriate fixed offsets are also used to obtain data from
   11346      the note.  */
   11347 
   11348   switch ((int) note->type)
   11349     {
   11350     case SOLARIS_NT_PRSTATUS:
   11351       switch (note->descsz)
   11352 	{
   11353 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   11354 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11355 					       136, 216, 308, 152, 356);
   11356 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   11357 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11358 					       264, 360, 520, 304, 600);
   11359 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   11360 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11361 					       136, 216, 308, 76, 356);
   11362 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   11363 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11364 					       264, 360, 520, 224, 600);
   11365 	default:
   11366 	  return true;
   11367 	}
   11368 
   11369     case SOLARIS_NT_PSINFO:
   11370     case SOLARIS_NT_PRPSINFO:
   11371       switch (note->descsz)
   11372 	{
   11373 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   11374 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   11375 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   11376 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   11377 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   11378 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   11379 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   11380 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   11381 	default:
   11382 	  return true;
   11383 	}
   11384 
   11385     case SOLARIS_NT_LWPSTATUS:
   11386       switch (note->descsz)
   11387 	{
   11388 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   11389 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11390 						152, 344, 400, 496);
   11391 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   11392 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11393 						304, 544, 544, 848);
   11394 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   11395 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11396 						76, 344, 380, 420);
   11397 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   11398 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11399 						224, 544, 528, 768);
   11400 	default:
   11401 	  return true;
   11402 	}
   11403 
   11404     case SOLARIS_NT_LWPSINFO:
   11405       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   11406       if (note->descsz == 128 || note->descsz == 152)
   11407 	elf_tdata (abfd)->core->lwpid =
   11408 	  bfd_get_32 (abfd, note->descdata + 4);
   11409       break;
   11410 
   11411     default:
   11412       break;
   11413     }
   11414 
   11415   return true;
   11416 }
   11417 
   11418 /* For name starting with "CORE" this may be either a Solaris
   11419    core file or a gdb-generated core file.  Do Solaris-specific
   11420    processing on selected note types first with
   11421    elfcore_grok_solaris_note(), then process the note
   11422    in elfcore_grok_note().  */
   11423 
   11424 static bool
   11425 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   11426 {
   11427   if (!elfcore_grok_solaris_note_impl (abfd, note))
   11428     return false;
   11429 
   11430   return elfcore_grok_note (abfd, note);
   11431 }
   11432 
   11433 static bool
   11434 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11435 {
   11436   if (note->type == NT_OPENBSD_PROCINFO)
   11437     return elfcore_grok_openbsd_procinfo (abfd, note);
   11438 
   11439   if (note->type == NT_OPENBSD_REGS)
   11440     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11441 
   11442   if (note->type == NT_OPENBSD_FPREGS)
   11443     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11444 
   11445   if (note->type == NT_OPENBSD_XFPREGS)
   11446     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   11447 
   11448   if (note->type == NT_OPENBSD_AUXV)
   11449     return elfcore_make_auxv_note_section (abfd, note, 0);
   11450 
   11451   if (note->type == NT_OPENBSD_WCOOKIE)
   11452     {
   11453       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   11454 							   SEC_HAS_CONTENTS);
   11455 
   11456       if (sect == NULL)
   11457 	return false;
   11458       sect->size = note->descsz;
   11459       sect->filepos = note->descpos;
   11460       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   11461 
   11462       return true;
   11463     }
   11464 
   11465   return true;
   11466 }
   11467 
   11468 static bool
   11469 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   11470 {
   11471   void *ddata = note->descdata;
   11472   char buf[100];
   11473   char *name;
   11474   asection *sect;
   11475   short sig;
   11476   unsigned flags;
   11477 
   11478   if (note->descsz < 16)
   11479     return false;
   11480 
   11481   /* nto_procfs_status 'pid' field is at offset 0.  */
   11482   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   11483 
   11484   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   11485   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   11486 
   11487   /* nto_procfs_status 'flags' field is at offset 8.  */
   11488   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   11489 
   11490   /* nto_procfs_status 'what' field is at offset 14.  */
   11491   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   11492     {
   11493       elf_tdata (abfd)->core->signal = sig;
   11494       elf_tdata (abfd)->core->lwpid = *tid;
   11495     }
   11496 
   11497   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   11498      do not come from signals so we make sure we set the current
   11499      thread just in case.  */
   11500   if (flags & 0x00000080)
   11501     elf_tdata (abfd)->core->lwpid = *tid;
   11502 
   11503   /* Make a ".qnx_core_status/%d" section.  */
   11504   sprintf (buf, ".qnx_core_status/%ld", *tid);
   11505 
   11506   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11507   if (name == NULL)
   11508     return false;
   11509   strcpy (name, buf);
   11510 
   11511   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11512   if (sect == NULL)
   11513     return false;
   11514 
   11515   sect->size		= note->descsz;
   11516   sect->filepos		= note->descpos;
   11517   sect->alignment_power = 2;
   11518 
   11519   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   11520 }
   11521 
   11522 static bool
   11523 elfcore_grok_nto_regs (bfd *abfd,
   11524 		       Elf_Internal_Note *note,
   11525 		       long tid,
   11526 		       char *base)
   11527 {
   11528   char buf[100];
   11529   char *name;
   11530   asection *sect;
   11531 
   11532   /* Make a "(base)/%d" section.  */
   11533   sprintf (buf, "%s/%ld", base, tid);
   11534 
   11535   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11536   if (name == NULL)
   11537     return false;
   11538   strcpy (name, buf);
   11539 
   11540   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11541   if (sect == NULL)
   11542     return false;
   11543 
   11544   sect->size		= note->descsz;
   11545   sect->filepos		= note->descpos;
   11546   sect->alignment_power = 2;
   11547 
   11548   /* This is the current thread.  */
   11549   if (elf_tdata (abfd)->core->lwpid == tid)
   11550     return elfcore_maybe_make_sect (abfd, base, sect);
   11551 
   11552   return true;
   11553 }
   11554 
   11555 #define BFD_QNT_CORE_INFO	7
   11556 #define BFD_QNT_CORE_STATUS	8
   11557 #define BFD_QNT_CORE_GREG	9
   11558 #define BFD_QNT_CORE_FPREG	10
   11559 
   11560 static bool
   11561 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   11562 {
   11563   /* Every GREG section has a STATUS section before it.  Store the
   11564      tid from the previous call to pass down to the next gregs
   11565      function.  */
   11566   static long tid = 1;
   11567 
   11568   switch (note->type)
   11569     {
   11570     case BFD_QNT_CORE_INFO:
   11571       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   11572     case BFD_QNT_CORE_STATUS:
   11573       return elfcore_grok_nto_status (abfd, note, &tid);
   11574     case BFD_QNT_CORE_GREG:
   11575       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   11576     case BFD_QNT_CORE_FPREG:
   11577       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   11578     default:
   11579       return true;
   11580     }
   11581 }
   11582 
   11583 static bool
   11584 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   11585 {
   11586   char *name;
   11587   asection *sect;
   11588   size_t len;
   11589 
   11590   /* Use note name as section name.  */
   11591   len = note->namesz;
   11592   name = (char *) bfd_alloc (abfd, len);
   11593   if (name == NULL)
   11594     return false;
   11595   memcpy (name, note->namedata, len);
   11596   name[len - 1] = '\0';
   11597 
   11598   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11599   if (sect == NULL)
   11600     return false;
   11601 
   11602   sect->size		= note->descsz;
   11603   sect->filepos		= note->descpos;
   11604   sect->alignment_power = 1;
   11605 
   11606   return true;
   11607 }
   11608 
   11609 /* Function: elfcore_write_note
   11610 
   11611    Inputs:
   11612      buffer to hold note, and current size of buffer
   11613      name of note
   11614      type of note
   11615      data for note
   11616      size of data for note
   11617 
   11618    Writes note to end of buffer.  ELF64 notes are written exactly as
   11619    for ELF32, despite the current (as of 2006) ELF gabi specifying
   11620    that they ought to have 8-byte namesz and descsz field, and have
   11621    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   11622 
   11623    Return:
   11624    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   11625 
   11626 char *
   11627 elfcore_write_note (bfd *abfd,
   11628 		    char *buf,
   11629 		    int *bufsiz,
   11630 		    const char *name,
   11631 		    int type,
   11632 		    const void *input,
   11633 		    int size)
   11634 {
   11635   Elf_External_Note *xnp;
   11636   size_t namesz;
   11637   size_t newspace;
   11638   char *dest;
   11639 
   11640   namesz = 0;
   11641   if (name != NULL)
   11642     namesz = strlen (name) + 1;
   11643 
   11644   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   11645 
   11646   buf = (char *) realloc (buf, *bufsiz + newspace);
   11647   if (buf == NULL)
   11648     return buf;
   11649   dest = buf + *bufsiz;
   11650   *bufsiz += newspace;
   11651   xnp = (Elf_External_Note *) dest;
   11652   H_PUT_32 (abfd, namesz, xnp->namesz);
   11653   H_PUT_32 (abfd, size, xnp->descsz);
   11654   H_PUT_32 (abfd, type, xnp->type);
   11655   dest = xnp->name;
   11656   if (name != NULL)
   11657     {
   11658       memcpy (dest, name, namesz);
   11659       dest += namesz;
   11660       while (namesz & 3)
   11661 	{
   11662 	  *dest++ = '\0';
   11663 	  ++namesz;
   11664 	}
   11665     }
   11666   memcpy (dest, input, size);
   11667   dest += size;
   11668   while (size & 3)
   11669     {
   11670       *dest++ = '\0';
   11671       ++size;
   11672     }
   11673   return buf;
   11674 }
   11675 
   11676 /* gcc-8 warns (*) on all the strncpy calls in this function about
   11677    possible string truncation.  The "truncation" is not a bug.  We
   11678    have an external representation of structs with fields that are not
   11679    necessarily NULL terminated and corresponding internal
   11680    representation fields that are one larger so that they can always
   11681    be NULL terminated.
   11682    gcc versions between 4.2 and 4.6 do not allow pragma control of
   11683    diagnostics inside functions, giving a hard error if you try to use
   11684    the finer control available with later versions.
   11685    gcc prior to 4.2 warns about diagnostic push and pop.
   11686    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   11687    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   11688    (*) Depending on your system header files!  */
   11689 #if GCC_VERSION >= 8000
   11690 # pragma GCC diagnostic push
   11691 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   11692 #endif
   11693 char *
   11694 elfcore_write_prpsinfo (bfd  *abfd,
   11695 			char *buf,
   11696 			int  *bufsiz,
   11697 			const char *fname,
   11698 			const char *psargs)
   11699 {
   11700   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11701 
   11702   if (bed->elf_backend_write_core_note != NULL)
   11703     {
   11704       char *ret;
   11705       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11706 						 NT_PRPSINFO, fname, psargs);
   11707       if (ret != NULL)
   11708 	return ret;
   11709     }
   11710 
   11711 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11712 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   11713   if (bed->s->elfclass == ELFCLASS32)
   11714     {
   11715 #  if defined (HAVE_PSINFO32_T)
   11716       psinfo32_t data;
   11717       int note_type = NT_PSINFO;
   11718 #  else
   11719       prpsinfo32_t data;
   11720       int note_type = NT_PRPSINFO;
   11721 #  endif
   11722 
   11723       memset (&data, 0, sizeof (data));
   11724       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11725       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11726       return elfcore_write_note (abfd, buf, bufsiz,
   11727 				 "CORE", note_type, &data, sizeof (data));
   11728     }
   11729   else
   11730 # endif
   11731     {
   11732 # if defined (HAVE_PSINFO_T)
   11733       psinfo_t data;
   11734       int note_type = NT_PSINFO;
   11735 # else
   11736       prpsinfo_t data;
   11737       int note_type = NT_PRPSINFO;
   11738 # endif
   11739 
   11740       memset (&data, 0, sizeof (data));
   11741       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11742       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11743       return elfcore_write_note (abfd, buf, bufsiz,
   11744 				 "CORE", note_type, &data, sizeof (data));
   11745     }
   11746 #endif	/* PSINFO_T or PRPSINFO_T */
   11747 
   11748   free (buf);
   11749   return NULL;
   11750 }
   11751 #if GCC_VERSION >= 8000
   11752 # pragma GCC diagnostic pop
   11753 #endif
   11754 
   11755 char *
   11756 elfcore_write_linux_prpsinfo32
   11757   (bfd *abfd, char *buf, int *bufsiz,
   11758    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11759 {
   11760   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   11761     {
   11762       struct elf_external_linux_prpsinfo32_ugid16 data;
   11763 
   11764       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   11765       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11766 				 &data, sizeof (data));
   11767     }
   11768   else
   11769     {
   11770       struct elf_external_linux_prpsinfo32_ugid32 data;
   11771 
   11772       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   11773       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11774 				 &data, sizeof (data));
   11775     }
   11776 }
   11777 
   11778 char *
   11779 elfcore_write_linux_prpsinfo64
   11780   (bfd *abfd, char *buf, int *bufsiz,
   11781    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11782 {
   11783   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   11784     {
   11785       struct elf_external_linux_prpsinfo64_ugid16 data;
   11786 
   11787       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   11788       return elfcore_write_note (abfd, buf, bufsiz,
   11789 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11790     }
   11791   else
   11792     {
   11793       struct elf_external_linux_prpsinfo64_ugid32 data;
   11794 
   11795       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   11796       return elfcore_write_note (abfd, buf, bufsiz,
   11797 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11798     }
   11799 }
   11800 
   11801 char *
   11802 elfcore_write_prstatus (bfd *abfd,
   11803 			char *buf,
   11804 			int *bufsiz,
   11805 			long pid,
   11806 			int cursig,
   11807 			const void *gregs)
   11808 {
   11809   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11810 
   11811   if (bed->elf_backend_write_core_note != NULL)
   11812     {
   11813       char *ret;
   11814       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11815 						 NT_PRSTATUS,
   11816 						 pid, cursig, gregs);
   11817       if (ret != NULL)
   11818 	return ret;
   11819     }
   11820 
   11821 #if defined (HAVE_PRSTATUS_T)
   11822 #if defined (HAVE_PRSTATUS32_T)
   11823   if (bed->s->elfclass == ELFCLASS32)
   11824     {
   11825       prstatus32_t prstat;
   11826 
   11827       memset (&prstat, 0, sizeof (prstat));
   11828       prstat.pr_pid = pid;
   11829       prstat.pr_cursig = cursig;
   11830       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11831       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11832 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11833     }
   11834   else
   11835 #endif
   11836     {
   11837       prstatus_t prstat;
   11838 
   11839       memset (&prstat, 0, sizeof (prstat));
   11840       prstat.pr_pid = pid;
   11841       prstat.pr_cursig = cursig;
   11842       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11843       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11844 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11845     }
   11846 #endif /* HAVE_PRSTATUS_T */
   11847 
   11848   free (buf);
   11849   return NULL;
   11850 }
   11851 
   11852 #if defined (HAVE_LWPSTATUS_T)
   11853 char *
   11854 elfcore_write_lwpstatus (bfd *abfd,
   11855 			 char *buf,
   11856 			 int *bufsiz,
   11857 			 long pid,
   11858 			 int cursig,
   11859 			 const void *gregs)
   11860 {
   11861   lwpstatus_t lwpstat;
   11862   const char *note_name = "CORE";
   11863 
   11864   memset (&lwpstat, 0, sizeof (lwpstat));
   11865   lwpstat.pr_lwpid  = pid >> 16;
   11866   lwpstat.pr_cursig = cursig;
   11867 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   11868   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   11869 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11870 #if !defined(gregs)
   11871   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   11872 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   11873 #else
   11874   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   11875 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   11876 #endif
   11877 #endif
   11878   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   11879 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   11880 }
   11881 #endif /* HAVE_LWPSTATUS_T */
   11882 
   11883 #if defined (HAVE_PSTATUS_T)
   11884 char *
   11885 elfcore_write_pstatus (bfd *abfd,
   11886 		       char *buf,
   11887 		       int *bufsiz,
   11888 		       long pid,
   11889 		       int cursig ATTRIBUTE_UNUSED,
   11890 		       const void *gregs ATTRIBUTE_UNUSED)
   11891 {
   11892   const char *note_name = "CORE";
   11893 #if defined (HAVE_PSTATUS32_T)
   11894   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11895 
   11896   if (bed->s->elfclass == ELFCLASS32)
   11897     {
   11898       pstatus32_t pstat;
   11899 
   11900       memset (&pstat, 0, sizeof (pstat));
   11901       pstat.pr_pid = pid & 0xffff;
   11902       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11903 				NT_PSTATUS, &pstat, sizeof (pstat));
   11904       return buf;
   11905     }
   11906   else
   11907 #endif
   11908     {
   11909       pstatus_t pstat;
   11910 
   11911       memset (&pstat, 0, sizeof (pstat));
   11912       pstat.pr_pid = pid & 0xffff;
   11913       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11914 				NT_PSTATUS, &pstat, sizeof (pstat));
   11915       return buf;
   11916     }
   11917 }
   11918 #endif /* HAVE_PSTATUS_T */
   11919 
   11920 char *
   11921 elfcore_write_prfpreg (bfd *abfd,
   11922 		       char *buf,
   11923 		       int *bufsiz,
   11924 		       const void *fpregs,
   11925 		       int size)
   11926 {
   11927   const char *note_name = "CORE";
   11928   return elfcore_write_note (abfd, buf, bufsiz,
   11929 			     note_name, NT_FPREGSET, fpregs, size);
   11930 }
   11931 
   11932 char *
   11933 elfcore_write_prxfpreg (bfd *abfd,
   11934 			char *buf,
   11935 			int *bufsiz,
   11936 			const void *xfpregs,
   11937 			int size)
   11938 {
   11939   char *note_name = "LINUX";
   11940   return elfcore_write_note (abfd, buf, bufsiz,
   11941 			     note_name, NT_PRXFPREG, xfpregs, size);
   11942 }
   11943 
   11944 char *
   11945 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   11946 			 const void *xfpregs, int size)
   11947 {
   11948   char *note_name;
   11949   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   11950     note_name = "FreeBSD";
   11951   else
   11952     note_name = "LINUX";
   11953   return elfcore_write_note (abfd, buf, bufsiz,
   11954 			     note_name, NT_X86_XSTATE, xfpregs, size);
   11955 }
   11956 
   11957 char *
   11958 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   11959 			    const void *regs, int size)
   11960 {
   11961   char *note_name = "FreeBSD";
   11962   return elfcore_write_note (abfd, buf, bufsiz,
   11963 			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
   11964 }
   11965 
   11966 char *
   11967 elfcore_write_ppc_vmx (bfd *abfd,
   11968 		       char *buf,
   11969 		       int *bufsiz,
   11970 		       const void *ppc_vmx,
   11971 		       int size)
   11972 {
   11973   char *note_name = "LINUX";
   11974   return elfcore_write_note (abfd, buf, bufsiz,
   11975 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   11976 }
   11977 
   11978 char *
   11979 elfcore_write_ppc_vsx (bfd *abfd,
   11980 		       char *buf,
   11981 		       int *bufsiz,
   11982 		       const void *ppc_vsx,
   11983 		       int size)
   11984 {
   11985   char *note_name = "LINUX";
   11986   return elfcore_write_note (abfd, buf, bufsiz,
   11987 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   11988 }
   11989 
   11990 char *
   11991 elfcore_write_ppc_tar (bfd *abfd,
   11992 		       char *buf,
   11993 		       int *bufsiz,
   11994 		       const void *ppc_tar,
   11995 		       int size)
   11996 {
   11997   char *note_name = "LINUX";
   11998   return elfcore_write_note (abfd, buf, bufsiz,
   11999 			     note_name, NT_PPC_TAR, ppc_tar, size);
   12000 }
   12001 
   12002 char *
   12003 elfcore_write_ppc_ppr (bfd *abfd,
   12004 		       char *buf,
   12005 		       int *bufsiz,
   12006 		       const void *ppc_ppr,
   12007 		       int size)
   12008 {
   12009   char *note_name = "LINUX";
   12010   return elfcore_write_note (abfd, buf, bufsiz,
   12011 			     note_name, NT_PPC_PPR, ppc_ppr, size);
   12012 }
   12013 
   12014 char *
   12015 elfcore_write_ppc_dscr (bfd *abfd,
   12016 			char *buf,
   12017 			int *bufsiz,
   12018 			const void *ppc_dscr,
   12019 			int size)
   12020 {
   12021   char *note_name = "LINUX";
   12022   return elfcore_write_note (abfd, buf, bufsiz,
   12023 			     note_name, NT_PPC_DSCR, ppc_dscr, size);
   12024 }
   12025 
   12026 char *
   12027 elfcore_write_ppc_ebb (bfd *abfd,
   12028 		       char *buf,
   12029 		       int *bufsiz,
   12030 		       const void *ppc_ebb,
   12031 		       int size)
   12032 {
   12033   char *note_name = "LINUX";
   12034   return elfcore_write_note (abfd, buf, bufsiz,
   12035 			     note_name, NT_PPC_EBB, ppc_ebb, size);
   12036 }
   12037 
   12038 char *
   12039 elfcore_write_ppc_pmu (bfd *abfd,
   12040 		       char *buf,
   12041 		       int *bufsiz,
   12042 		       const void *ppc_pmu,
   12043 		       int size)
   12044 {
   12045   char *note_name = "LINUX";
   12046   return elfcore_write_note (abfd, buf, bufsiz,
   12047 			     note_name, NT_PPC_PMU, ppc_pmu, size);
   12048 }
   12049 
   12050 char *
   12051 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12052 			   char *buf,
   12053 			   int *bufsiz,
   12054 			   const void *ppc_tm_cgpr,
   12055 			   int size)
   12056 {
   12057   char *note_name = "LINUX";
   12058   return elfcore_write_note (abfd, buf, bufsiz,
   12059 			     note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
   12060 }
   12061 
   12062 char *
   12063 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12064 			   char *buf,
   12065 			   int *bufsiz,
   12066 			   const void *ppc_tm_cfpr,
   12067 			   int size)
   12068 {
   12069   char *note_name = "LINUX";
   12070   return elfcore_write_note (abfd, buf, bufsiz,
   12071 			     note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
   12072 }
   12073 
   12074 char *
   12075 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12076 			   char *buf,
   12077 			   int *bufsiz,
   12078 			   const void *ppc_tm_cvmx,
   12079 			   int size)
   12080 {
   12081   char *note_name = "LINUX";
   12082   return elfcore_write_note (abfd, buf, bufsiz,
   12083 			     note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
   12084 }
   12085 
   12086 char *
   12087 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12088 			   char *buf,
   12089 			   int *bufsiz,
   12090 			   const void *ppc_tm_cvsx,
   12091 			   int size)
   12092 {
   12093   char *note_name = "LINUX";
   12094   return elfcore_write_note (abfd, buf, bufsiz,
   12095 			     note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
   12096 }
   12097 
   12098 char *
   12099 elfcore_write_ppc_tm_spr (bfd *abfd,
   12100 			  char *buf,
   12101 			  int *bufsiz,
   12102 			  const void *ppc_tm_spr,
   12103 			  int size)
   12104 {
   12105   char *note_name = "LINUX";
   12106   return elfcore_write_note (abfd, buf, bufsiz,
   12107 			     note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
   12108 }
   12109 
   12110 char *
   12111 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12112 			   char *buf,
   12113 			   int *bufsiz,
   12114 			   const void *ppc_tm_ctar,
   12115 			   int size)
   12116 {
   12117   char *note_name = "LINUX";
   12118   return elfcore_write_note (abfd, buf, bufsiz,
   12119 			     note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
   12120 }
   12121 
   12122 char *
   12123 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12124 			   char *buf,
   12125 			   int *bufsiz,
   12126 			   const void *ppc_tm_cppr,
   12127 			   int size)
   12128 {
   12129   char *note_name = "LINUX";
   12130   return elfcore_write_note (abfd, buf, bufsiz,
   12131 			     note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
   12132 }
   12133 
   12134 char *
   12135 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12136 			    char *buf,
   12137 			    int *bufsiz,
   12138 			    const void *ppc_tm_cdscr,
   12139 			    int size)
   12140 {
   12141   char *note_name = "LINUX";
   12142   return elfcore_write_note (abfd, buf, bufsiz,
   12143 			     note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
   12144 }
   12145 
   12146 static char *
   12147 elfcore_write_s390_high_gprs (bfd *abfd,
   12148 			      char *buf,
   12149 			      int *bufsiz,
   12150 			      const void *s390_high_gprs,
   12151 			      int size)
   12152 {
   12153   char *note_name = "LINUX";
   12154   return elfcore_write_note (abfd, buf, bufsiz,
   12155 			     note_name, NT_S390_HIGH_GPRS,
   12156 			     s390_high_gprs, size);
   12157 }
   12158 
   12159 char *
   12160 elfcore_write_s390_timer (bfd *abfd,
   12161 			  char *buf,
   12162 			  int *bufsiz,
   12163 			  const void *s390_timer,
   12164 			  int size)
   12165 {
   12166   char *note_name = "LINUX";
   12167   return elfcore_write_note (abfd, buf, bufsiz,
   12168 			     note_name, NT_S390_TIMER, s390_timer, size);
   12169 }
   12170 
   12171 char *
   12172 elfcore_write_s390_todcmp (bfd *abfd,
   12173 			   char *buf,
   12174 			   int *bufsiz,
   12175 			   const void *s390_todcmp,
   12176 			   int size)
   12177 {
   12178   char *note_name = "LINUX";
   12179   return elfcore_write_note (abfd, buf, bufsiz,
   12180 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   12181 }
   12182 
   12183 char *
   12184 elfcore_write_s390_todpreg (bfd *abfd,
   12185 			    char *buf,
   12186 			    int *bufsiz,
   12187 			    const void *s390_todpreg,
   12188 			    int size)
   12189 {
   12190   char *note_name = "LINUX";
   12191   return elfcore_write_note (abfd, buf, bufsiz,
   12192 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   12193 }
   12194 
   12195 char *
   12196 elfcore_write_s390_ctrs (bfd *abfd,
   12197 			 char *buf,
   12198 			 int *bufsiz,
   12199 			 const void *s390_ctrs,
   12200 			 int size)
   12201 {
   12202   char *note_name = "LINUX";
   12203   return elfcore_write_note (abfd, buf, bufsiz,
   12204 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   12205 }
   12206 
   12207 char *
   12208 elfcore_write_s390_prefix (bfd *abfd,
   12209 			   char *buf,
   12210 			   int *bufsiz,
   12211 			   const void *s390_prefix,
   12212 			   int size)
   12213 {
   12214   char *note_name = "LINUX";
   12215   return elfcore_write_note (abfd, buf, bufsiz,
   12216 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   12217 }
   12218 
   12219 char *
   12220 elfcore_write_s390_last_break (bfd *abfd,
   12221 			       char *buf,
   12222 			       int *bufsiz,
   12223 			       const void *s390_last_break,
   12224 			       int size)
   12225 {
   12226   char *note_name = "LINUX";
   12227   return elfcore_write_note (abfd, buf, bufsiz,
   12228 			     note_name, NT_S390_LAST_BREAK,
   12229 			     s390_last_break, size);
   12230 }
   12231 
   12232 char *
   12233 elfcore_write_s390_system_call (bfd *abfd,
   12234 				char *buf,
   12235 				int *bufsiz,
   12236 				const void *s390_system_call,
   12237 				int size)
   12238 {
   12239   char *note_name = "LINUX";
   12240   return elfcore_write_note (abfd, buf, bufsiz,
   12241 			     note_name, NT_S390_SYSTEM_CALL,
   12242 			     s390_system_call, size);
   12243 }
   12244 
   12245 char *
   12246 elfcore_write_s390_tdb (bfd *abfd,
   12247 			char *buf,
   12248 			int *bufsiz,
   12249 			const void *s390_tdb,
   12250 			int size)
   12251 {
   12252   char *note_name = "LINUX";
   12253   return elfcore_write_note (abfd, buf, bufsiz,
   12254 			     note_name, NT_S390_TDB, s390_tdb, size);
   12255 }
   12256 
   12257 char *
   12258 elfcore_write_s390_vxrs_low (bfd *abfd,
   12259 			     char *buf,
   12260 			     int *bufsiz,
   12261 			     const void *s390_vxrs_low,
   12262 			     int size)
   12263 {
   12264   char *note_name = "LINUX";
   12265   return elfcore_write_note (abfd, buf, bufsiz,
   12266 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   12267 }
   12268 
   12269 char *
   12270 elfcore_write_s390_vxrs_high (bfd *abfd,
   12271 			     char *buf,
   12272 			     int *bufsiz,
   12273 			     const void *s390_vxrs_high,
   12274 			     int size)
   12275 {
   12276   char *note_name = "LINUX";
   12277   return elfcore_write_note (abfd, buf, bufsiz,
   12278 			     note_name, NT_S390_VXRS_HIGH,
   12279 			     s390_vxrs_high, size);
   12280 }
   12281 
   12282 char *
   12283 elfcore_write_s390_gs_cb (bfd *abfd,
   12284 			  char *buf,
   12285 			  int *bufsiz,
   12286 			  const void *s390_gs_cb,
   12287 			  int size)
   12288 {
   12289   char *note_name = "LINUX";
   12290   return elfcore_write_note (abfd, buf, bufsiz,
   12291 			     note_name, NT_S390_GS_CB,
   12292 			     s390_gs_cb, size);
   12293 }
   12294 
   12295 char *
   12296 elfcore_write_s390_gs_bc (bfd *abfd,
   12297 			  char *buf,
   12298 			  int *bufsiz,
   12299 			  const void *s390_gs_bc,
   12300 			  int size)
   12301 {
   12302   char *note_name = "LINUX";
   12303   return elfcore_write_note (abfd, buf, bufsiz,
   12304 			     note_name, NT_S390_GS_BC,
   12305 			     s390_gs_bc, size);
   12306 }
   12307 
   12308 char *
   12309 elfcore_write_arm_vfp (bfd *abfd,
   12310 		       char *buf,
   12311 		       int *bufsiz,
   12312 		       const void *arm_vfp,
   12313 		       int size)
   12314 {
   12315   char *note_name = "LINUX";
   12316   return elfcore_write_note (abfd, buf, bufsiz,
   12317 			     note_name, NT_ARM_VFP, arm_vfp, size);
   12318 }
   12319 
   12320 char *
   12321 elfcore_write_aarch_tls (bfd *abfd,
   12322 		       char *buf,
   12323 		       int *bufsiz,
   12324 		       const void *aarch_tls,
   12325 		       int size)
   12326 {
   12327   char *note_name = "LINUX";
   12328   return elfcore_write_note (abfd, buf, bufsiz,
   12329 			     note_name, NT_ARM_TLS, aarch_tls, size);
   12330 }
   12331 
   12332 char *
   12333 elfcore_write_aarch_hw_break (bfd *abfd,
   12334 			    char *buf,
   12335 			    int *bufsiz,
   12336 			    const void *aarch_hw_break,
   12337 			    int size)
   12338 {
   12339   char *note_name = "LINUX";
   12340   return elfcore_write_note (abfd, buf, bufsiz,
   12341 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   12342 }
   12343 
   12344 char *
   12345 elfcore_write_aarch_hw_watch (bfd *abfd,
   12346 			    char *buf,
   12347 			    int *bufsiz,
   12348 			    const void *aarch_hw_watch,
   12349 			    int size)
   12350 {
   12351   char *note_name = "LINUX";
   12352   return elfcore_write_note (abfd, buf, bufsiz,
   12353 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   12354 }
   12355 
   12356 char *
   12357 elfcore_write_aarch_sve (bfd *abfd,
   12358 			 char *buf,
   12359 			 int *bufsiz,
   12360 			 const void *aarch_sve,
   12361 			 int size)
   12362 {
   12363   char *note_name = "LINUX";
   12364   return elfcore_write_note (abfd, buf, bufsiz,
   12365 			     note_name, NT_ARM_SVE, aarch_sve, size);
   12366 }
   12367 
   12368 char *
   12369 elfcore_write_aarch_pauth (bfd *abfd,
   12370 			   char *buf,
   12371 			   int *bufsiz,
   12372 			   const void *aarch_pauth,
   12373 			   int size)
   12374 {
   12375   char *note_name = "LINUX";
   12376   return elfcore_write_note (abfd, buf, bufsiz,
   12377 			     note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
   12378 }
   12379 
   12380 char *
   12381 elfcore_write_aarch_mte (bfd *abfd,
   12382 				      char *buf,
   12383 				      int *bufsiz,
   12384 				      const void *aarch_mte,
   12385 				      int size)
   12386 {
   12387   char *note_name = "LINUX";
   12388   return elfcore_write_note (abfd, buf, bufsiz,
   12389 			     note_name, NT_ARM_TAGGED_ADDR_CTRL,
   12390 			     aarch_mte,
   12391 			     size);
   12392 }
   12393 
   12394 char *
   12395 elfcore_write_arc_v2 (bfd *abfd,
   12396 		      char *buf,
   12397 		      int *bufsiz,
   12398 		      const void *arc_v2,
   12399 		      int size)
   12400 {
   12401   char *note_name = "LINUX";
   12402   return elfcore_write_note (abfd, buf, bufsiz,
   12403 			     note_name, NT_ARC_V2, arc_v2, size);
   12404 }
   12405 
   12406 char *
   12407 elfcore_write_loongarch_cpucfg (bfd *abfd,
   12408 				char *buf,
   12409 				int *bufsiz,
   12410 				const void *loongarch_cpucfg,
   12411 				int size)
   12412 {
   12413   char *note_name = "LINUX";
   12414   return elfcore_write_note (abfd, buf, bufsiz,
   12415 			     note_name, NT_LARCH_CPUCFG,
   12416 			     loongarch_cpucfg, size);
   12417 }
   12418 
   12419 char *
   12420 elfcore_write_loongarch_lbt (bfd *abfd,
   12421 			     char *buf,
   12422 			     int *bufsiz,
   12423 			     const void *loongarch_lbt,
   12424 			     int size)
   12425 {
   12426   char *note_name = "LINUX";
   12427   return elfcore_write_note (abfd, buf, bufsiz,
   12428 			     note_name, NT_LARCH_LBT, loongarch_lbt, size);
   12429 }
   12430 
   12431 char *
   12432 elfcore_write_loongarch_lsx (bfd *abfd,
   12433 			     char *buf,
   12434 			     int *bufsiz,
   12435 			     const void *loongarch_lsx,
   12436 			     int size)
   12437 {
   12438   char *note_name = "LINUX";
   12439   return elfcore_write_note (abfd, buf, bufsiz,
   12440 			     note_name, NT_LARCH_LSX, loongarch_lsx, size);
   12441 }
   12442 
   12443 char *
   12444 elfcore_write_loongarch_lasx (bfd *abfd,
   12445 			      char *buf,
   12446 			      int *bufsiz,
   12447 			      const void *loongarch_lasx,
   12448 			      int size)
   12449 {
   12450   char *note_name = "LINUX";
   12451   return elfcore_write_note (abfd, buf, bufsiz,
   12452 			     note_name, NT_LARCH_LASX, loongarch_lasx, size);
   12453 }
   12454 
   12455 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   12456    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12457    written into.  Return a pointer to the new start of the note buffer, to
   12458    replace BUF which may no longer be valid.  */
   12459 
   12460 char *
   12461 elfcore_write_riscv_csr (bfd *abfd,
   12462                          char *buf,
   12463                          int *bufsiz,
   12464                          const void *csrs,
   12465                          int size)
   12466 {
   12467   const char *note_name = "GDB";
   12468   return elfcore_write_note (abfd, buf, bufsiz,
   12469 			     note_name, NT_RISCV_CSR, csrs, size);
   12470 }
   12471 
   12472 /* Write the target description (a string) pointed to by TDESC, length
   12473    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   12474    note is being written into.  Return a pointer to the new start of the
   12475    note buffer, to replace BUF which may no longer be valid.  */
   12476 
   12477 char *
   12478 elfcore_write_gdb_tdesc (bfd *abfd,
   12479 			 char *buf,
   12480 			 int *bufsiz,
   12481 			 const void *tdesc,
   12482 			 int size)
   12483 {
   12484   const char *note_name = "GDB";
   12485   return elfcore_write_note (abfd, buf, bufsiz,
   12486                              note_name, NT_GDB_TDESC, tdesc, size);
   12487 }
   12488 
   12489 char *
   12490 elfcore_write_register_note (bfd *abfd,
   12491 			     char *buf,
   12492 			     int *bufsiz,
   12493 			     const char *section,
   12494 			     const void *data,
   12495 			     int size)
   12496 {
   12497   if (strcmp (section, ".reg2") == 0)
   12498     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   12499   if (strcmp (section, ".reg-xfp") == 0)
   12500     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   12501   if (strcmp (section, ".reg-xstate") == 0)
   12502     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   12503   if (strcmp (section, ".reg-x86-segbases") == 0)
   12504     return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
   12505   if (strcmp (section, ".reg-ppc-vmx") == 0)
   12506     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   12507   if (strcmp (section, ".reg-ppc-vsx") == 0)
   12508     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   12509   if (strcmp (section, ".reg-ppc-tar") == 0)
   12510     return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
   12511   if (strcmp (section, ".reg-ppc-ppr") == 0)
   12512     return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
   12513   if (strcmp (section, ".reg-ppc-dscr") == 0)
   12514     return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
   12515   if (strcmp (section, ".reg-ppc-ebb") == 0)
   12516     return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
   12517   if (strcmp (section, ".reg-ppc-pmu") == 0)
   12518     return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
   12519   if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
   12520     return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
   12521   if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
   12522     return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
   12523   if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
   12524     return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
   12525   if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
   12526     return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
   12527   if (strcmp (section, ".reg-ppc-tm-spr") == 0)
   12528     return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
   12529   if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
   12530     return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
   12531   if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
   12532     return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
   12533   if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
   12534     return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
   12535   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   12536     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   12537   if (strcmp (section, ".reg-s390-timer") == 0)
   12538     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   12539   if (strcmp (section, ".reg-s390-todcmp") == 0)
   12540     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   12541   if (strcmp (section, ".reg-s390-todpreg") == 0)
   12542     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   12543   if (strcmp (section, ".reg-s390-ctrs") == 0)
   12544     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   12545   if (strcmp (section, ".reg-s390-prefix") == 0)
   12546     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   12547   if (strcmp (section, ".reg-s390-last-break") == 0)
   12548     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   12549   if (strcmp (section, ".reg-s390-system-call") == 0)
   12550     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   12551   if (strcmp (section, ".reg-s390-tdb") == 0)
   12552     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   12553   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   12554     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   12555   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   12556     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   12557   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   12558     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   12559   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   12560     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   12561   if (strcmp (section, ".reg-arm-vfp") == 0)
   12562     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   12563   if (strcmp (section, ".reg-aarch-tls") == 0)
   12564     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   12565   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   12566     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   12567   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   12568     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   12569   if (strcmp (section, ".reg-aarch-sve") == 0)
   12570     return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
   12571   if (strcmp (section, ".reg-aarch-pauth") == 0)
   12572     return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
   12573   if (strcmp (section, ".reg-aarch-mte") == 0)
   12574     return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
   12575   if (strcmp (section, ".reg-arc-v2") == 0)
   12576     return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
   12577   if (strcmp (section, ".gdb-tdesc") == 0)
   12578     return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
   12579   if (strcmp (section, ".reg-riscv-csr") == 0)
   12580     return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
   12581   if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
   12582     return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
   12583   if (strcmp (section, ".reg-loongarch-lbt") == 0)
   12584     return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
   12585   if (strcmp (section, ".reg-loongarch-lsx") == 0)
   12586     return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
   12587   if (strcmp (section, ".reg-loongarch-lasx") == 0)
   12588     return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
   12589   return NULL;
   12590 }
   12591 
   12592 char *
   12593 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   12594 			 const void *buf, int bufsiz)
   12595 {
   12596   return elfcore_write_note (obfd, note_data, note_size,
   12597 			     "CORE", NT_FILE, buf, bufsiz);
   12598 }
   12599 
   12600 static bool
   12601 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   12602 		 size_t align)
   12603 {
   12604   char *p;
   12605 
   12606   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   12607      gABI specifies that PT_NOTE alignment should be aligned to 4
   12608      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   12609      align is less than 4, we use 4 byte alignment.   */
   12610   if (align < 4)
   12611     align = 4;
   12612   if (align != 4 && align != 8)
   12613     return false;
   12614 
   12615   p = buf;
   12616   while (p < buf + size)
   12617     {
   12618       Elf_External_Note *xnp = (Elf_External_Note *) p;
   12619       Elf_Internal_Note in;
   12620 
   12621       if (offsetof (Elf_External_Note, name) > buf - p + size)
   12622 	return false;
   12623 
   12624       in.type = H_GET_32 (abfd, xnp->type);
   12625 
   12626       in.namesz = H_GET_32 (abfd, xnp->namesz);
   12627       in.namedata = xnp->name;
   12628       if (in.namesz > buf - in.namedata + size)
   12629 	return false;
   12630 
   12631       in.descsz = H_GET_32 (abfd, xnp->descsz);
   12632       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   12633       in.descpos = offset + (in.descdata - buf);
   12634       if (in.descsz != 0
   12635 	  && (in.descdata >= buf + size
   12636 	      || in.descsz > buf - in.descdata + size))
   12637 	return false;
   12638 
   12639       switch (bfd_get_format (abfd))
   12640 	{
   12641 	default:
   12642 	  return true;
   12643 
   12644 	case bfd_core:
   12645 	  {
   12646 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   12647 	    struct
   12648 	    {
   12649 	      const char * string;
   12650 	      size_t len;
   12651 	      bool (*func) (bfd *, Elf_Internal_Note *);
   12652 	    }
   12653 	    grokers[] =
   12654 	    {
   12655 	      GROKER_ELEMENT ("", elfcore_grok_note),
   12656 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   12657 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   12658 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   12659 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   12660 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   12661 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   12662 	      GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
   12663 	    };
   12664 #undef GROKER_ELEMENT
   12665 	    int i;
   12666 
   12667 	    for (i = ARRAY_SIZE (grokers); i--;)
   12668 	      {
   12669 		if (in.namesz >= grokers[i].len
   12670 		    && strncmp (in.namedata, grokers[i].string,
   12671 				grokers[i].len) == 0)
   12672 		  {
   12673 		    if (! grokers[i].func (abfd, & in))
   12674 		      return false;
   12675 		    break;
   12676 		  }
   12677 	      }
   12678 	    break;
   12679 	  }
   12680 
   12681 	case bfd_object:
   12682 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   12683 	    {
   12684 	      if (! elfobj_grok_gnu_note (abfd, &in))
   12685 		return false;
   12686 	    }
   12687 	  else if (in.namesz == sizeof "stapsdt"
   12688 		   && strcmp (in.namedata, "stapsdt") == 0)
   12689 	    {
   12690 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   12691 		return false;
   12692 	    }
   12693 	  break;
   12694 	}
   12695 
   12696       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   12697     }
   12698 
   12699   return true;
   12700 }
   12701 
   12702 bool
   12703 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   12704 		size_t align)
   12705 {
   12706   char *buf;
   12707 
   12708   if (size == 0 || (size + 1) == 0)
   12709     return true;
   12710 
   12711   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   12712     return false;
   12713 
   12714   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   12715   if (buf == NULL)
   12716     return false;
   12717 
   12718   /* PR 17512: file: ec08f814
   12719      0-termintate the buffer so that string searches will not overflow.  */
   12720   buf[size] = 0;
   12721 
   12722   if (!elf_parse_notes (abfd, buf, size, offset, align))
   12723     {
   12724       free (buf);
   12725       return false;
   12726     }
   12727 
   12728   free (buf);
   12729   return true;
   12730 }
   12731 
   12732 /* Providing external access to the ELF program header table.  */
   12734 
   12735 /* Return an upper bound on the number of bytes required to store a
   12736    copy of ABFD's program header table entries.  Return -1 if an error
   12737    occurs; bfd_get_error will return an appropriate code.  */
   12738 
   12739 long
   12740 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   12741 {
   12742   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   12743     {
   12744       bfd_set_error (bfd_error_wrong_format);
   12745       return -1;
   12746     }
   12747 
   12748   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   12749 }
   12750 
   12751 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   12752    will be stored as an array of Elf_Internal_Phdr structures, as
   12753    defined in include/elf/internal.h.  To find out how large the
   12754    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   12755 
   12756    Return the number of program header table entries read, or -1 if an
   12757    error occurs; bfd_get_error will return an appropriate code.  */
   12758 
   12759 int
   12760 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   12761 {
   12762   int num_phdrs;
   12763 
   12764   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   12765     {
   12766       bfd_set_error (bfd_error_wrong_format);
   12767       return -1;
   12768     }
   12769 
   12770   num_phdrs = elf_elfheader (abfd)->e_phnum;
   12771   if (num_phdrs != 0)
   12772     memcpy (phdrs, elf_tdata (abfd)->phdr,
   12773 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   12774 
   12775   return num_phdrs;
   12776 }
   12777 
   12778 enum elf_reloc_type_class
   12779 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12780 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   12781 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   12782 {
   12783   return reloc_class_normal;
   12784 }
   12785 
   12786 /* For RELA architectures, return the relocation value for a
   12787    relocation against a local symbol.  */
   12788 
   12789 bfd_vma
   12790 _bfd_elf_rela_local_sym (bfd *abfd,
   12791 			 Elf_Internal_Sym *sym,
   12792 			 asection **psec,
   12793 			 Elf_Internal_Rela *rel)
   12794 {
   12795   asection *sec = *psec;
   12796   bfd_vma relocation;
   12797 
   12798   relocation = (sec->output_section->vma
   12799 		+ sec->output_offset
   12800 		+ sym->st_value);
   12801   if ((sec->flags & SEC_MERGE)
   12802       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   12803       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   12804     {
   12805       rel->r_addend =
   12806 	_bfd_merged_section_offset (abfd, psec,
   12807 				    elf_section_data (sec)->sec_info,
   12808 				    sym->st_value + rel->r_addend);
   12809       if (sec != *psec)
   12810 	{
   12811 	  /* If we have changed the section, and our original section is
   12812 	     marked with SEC_EXCLUDE, it means that the original
   12813 	     SEC_MERGE section has been completely subsumed in some
   12814 	     other SEC_MERGE section.  In this case, we need to leave
   12815 	     some info around for --emit-relocs.  */
   12816 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   12817 	    sec->kept_section = *psec;
   12818 	  sec = *psec;
   12819 	}
   12820       rel->r_addend -= relocation;
   12821       rel->r_addend += sec->output_section->vma + sec->output_offset;
   12822     }
   12823   return relocation;
   12824 }
   12825 
   12826 bfd_vma
   12827 _bfd_elf_rel_local_sym (bfd *abfd,
   12828 			Elf_Internal_Sym *sym,
   12829 			asection **psec,
   12830 			bfd_vma addend)
   12831 {
   12832   asection *sec = *psec;
   12833 
   12834   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   12835     return sym->st_value + addend;
   12836 
   12837   return _bfd_merged_section_offset (abfd, psec,
   12838 				     elf_section_data (sec)->sec_info,
   12839 				     sym->st_value + addend);
   12840 }
   12841 
   12842 /* Adjust an address within a section.  Given OFFSET within SEC, return
   12843    the new offset within the section, based upon changes made to the
   12844    section.  Returns -1 if the offset is now invalid.
   12845    The offset (in abnd out) is in target sized bytes, however big a
   12846    byte may be.  */
   12847 
   12848 bfd_vma
   12849 _bfd_elf_section_offset (bfd *abfd,
   12850 			 struct bfd_link_info *info,
   12851 			 asection *sec,
   12852 			 bfd_vma offset)
   12853 {
   12854   switch (sec->sec_info_type)
   12855     {
   12856     case SEC_INFO_TYPE_STABS:
   12857       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   12858 				       offset);
   12859     case SEC_INFO_TYPE_EH_FRAME:
   12860       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   12861 
   12862     default:
   12863       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   12864 	{
   12865 	  /* Reverse the offset.  */
   12866 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12867 	  bfd_size_type address_size = bed->s->arch_size / 8;
   12868 
   12869 	  /* address_size and sec->size are in octets.  Convert
   12870 	     to bytes before subtracting the original offset.  */
   12871 	  offset = ((sec->size - address_size)
   12872 		    / bfd_octets_per_byte (abfd, sec) - offset);
   12873 	}
   12874       return offset;
   12875     }
   12876 }
   12877 
   12878 /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   12880    reconstruct an ELF file by reading the segments out of remote memory
   12881    based on the ELF file header at EHDR_VMA and the ELF program headers it
   12882    points to.  If not null, *LOADBASEP is filled in with the difference
   12883    between the VMAs from which the segments were read, and the VMAs the
   12884    file headers (and hence BFD's idea of each section's VMA) put them at.
   12885 
   12886    The function TARGET_READ_MEMORY is called to copy LEN bytes from the
   12887    remote memory at target address VMA into the local buffer at MYADDR; it
   12888    should return zero on success or an `errno' code on failure.  TEMPL must
   12889    be a BFD for an ELF target with the word size and byte order found in
   12890    the remote memory.  */
   12891 
   12892 bfd *
   12893 bfd_elf_bfd_from_remote_memory
   12894   (bfd *templ,
   12895    bfd_vma ehdr_vma,
   12896    bfd_size_type size,
   12897    bfd_vma *loadbasep,
   12898    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   12899 {
   12900   return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
   12901     (templ, ehdr_vma, size, loadbasep, target_read_memory);
   12902 }
   12903 
   12904 long
   12906 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   12907 			       long symcount ATTRIBUTE_UNUSED,
   12908 			       asymbol **syms ATTRIBUTE_UNUSED,
   12909 			       long dynsymcount,
   12910 			       asymbol **dynsyms,
   12911 			       asymbol **ret)
   12912 {
   12913   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12914   asection *relplt;
   12915   asymbol *s;
   12916   const char *relplt_name;
   12917   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   12918   arelent *p;
   12919   long count, i, n;
   12920   size_t size;
   12921   Elf_Internal_Shdr *hdr;
   12922   char *names;
   12923   asection *plt;
   12924 
   12925   *ret = NULL;
   12926 
   12927   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   12928     return 0;
   12929 
   12930   if (dynsymcount <= 0)
   12931     return 0;
   12932 
   12933   if (!bed->plt_sym_val)
   12934     return 0;
   12935 
   12936   relplt_name = bed->relplt_name;
   12937   if (relplt_name == NULL)
   12938     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   12939   relplt = bfd_get_section_by_name (abfd, relplt_name);
   12940   if (relplt == NULL)
   12941     return 0;
   12942 
   12943   hdr = &elf_section_data (relplt)->this_hdr;
   12944   if (hdr->sh_link != elf_dynsymtab (abfd)
   12945       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   12946     return 0;
   12947 
   12948   plt = bfd_get_section_by_name (abfd, ".plt");
   12949   if (plt == NULL)
   12950     return 0;
   12951 
   12952   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   12953   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   12954     return -1;
   12955 
   12956   count = relplt->size / hdr->sh_entsize;
   12957   size = count * sizeof (asymbol);
   12958   p = relplt->relocation;
   12959   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12960     {
   12961       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   12962       if (p->addend != 0)
   12963 	{
   12964 #ifdef BFD64
   12965 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   12966 #else
   12967 	  size += sizeof ("+0x") - 1 + 8;
   12968 #endif
   12969 	}
   12970     }
   12971 
   12972   s = *ret = (asymbol *) bfd_malloc (size);
   12973   if (s == NULL)
   12974     return -1;
   12975 
   12976   names = (char *) (s + count);
   12977   p = relplt->relocation;
   12978   n = 0;
   12979   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12980     {
   12981       size_t len;
   12982       bfd_vma addr;
   12983 
   12984       addr = bed->plt_sym_val (i, plt, p);
   12985       if (addr == (bfd_vma) -1)
   12986 	continue;
   12987 
   12988       *s = **p->sym_ptr_ptr;
   12989       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   12990 	 we are defining a symbol, ensure one of them is set.  */
   12991       if ((s->flags & BSF_LOCAL) == 0)
   12992 	s->flags |= BSF_GLOBAL;
   12993       s->flags |= BSF_SYNTHETIC;
   12994       s->section = plt;
   12995       s->value = addr - plt->vma;
   12996       s->name = names;
   12997       s->udata.p = NULL;
   12998       len = strlen ((*p->sym_ptr_ptr)->name);
   12999       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   13000       names += len;
   13001       if (p->addend != 0)
   13002 	{
   13003 	  char buf[30], *a;
   13004 
   13005 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   13006 	  names += sizeof ("+0x") - 1;
   13007 	  bfd_sprintf_vma (abfd, buf, p->addend);
   13008 	  for (a = buf; *a == '0'; ++a)
   13009 	    ;
   13010 	  len = strlen (a);
   13011 	  memcpy (names, a, len);
   13012 	  names += len;
   13013 	}
   13014       memcpy (names, "@plt", sizeof ("@plt"));
   13015       names += sizeof ("@plt");
   13016       ++s, ++n;
   13017     }
   13018 
   13019   return n;
   13020 }
   13021 
   13022 /* It is only used by x86-64 so far.
   13023    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   13024    but current usage would allow all of _bfd_std_section to be zero.  */
   13025 static const asymbol lcomm_sym
   13026   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   13027 asection _bfd_elf_large_com_section
   13028   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   13029 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   13030 
   13031 bool
   13032 _bfd_elf_final_write_processing (bfd *abfd)
   13033 {
   13034   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   13035 
   13036   i_ehdrp = elf_elfheader (abfd);
   13037 
   13038   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13039     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   13040 
   13041   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13042      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13043      or STB_GNU_UNIQUE binding.  */
   13044   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13045     {
   13046       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13047 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13048       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13049 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13050 	{
   13051 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13052 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13053 				  "and FreeBSD targets"));
   13054 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13055 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13056 				  "only by GNU and FreeBSD targets"));
   13057 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13058 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13059 				  "only by GNU and FreeBSD targets"));
   13060 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13061 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13062 				  "only by GNU and FreeBSD targets"));
   13063 	  bfd_set_error (bfd_error_sorry);
   13064 	  return false;
   13065 	}
   13066     }
   13067   return true;
   13068 }
   13069 
   13070 
   13071 /* Return TRUE for ELF symbol types that represent functions.
   13072    This is the default version of this function, which is sufficient for
   13073    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13074 
   13075 bool
   13076 _bfd_elf_is_function_type (unsigned int type)
   13077 {
   13078   return (type == STT_FUNC
   13079 	  || type == STT_GNU_IFUNC);
   13080 }
   13081 
   13082 /* If the ELF symbol SYM might be a function in SEC, return the
   13083    function size and set *CODE_OFF to the function's entry point,
   13084    otherwise return zero.  */
   13085 
   13086 bfd_size_type
   13087 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13088 			     bfd_vma *code_off)
   13089 {
   13090   bfd_size_type size;
   13091   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13092 
   13093   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13094 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13095       || sym->section != sec)
   13096     return 0;
   13097 
   13098   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13099 
   13100   /* In theory we should check that the symbol's type satisfies
   13101      _bfd_elf_is_function_type(), but there are some function-like
   13102      symbols which would fail this test.  (eg _start).  Instead
   13103      we check for hidden, local, notype symbols with zero size.
   13104      This type of symbol is generated by the annobin plugin for gcc
   13105      and clang, and should not be considered to be a function symbol.  */
   13106   if (size == 0
   13107       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13108       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13109       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13110     return 0;
   13111 
   13112   *code_off = sym->value;
   13113   /* Do not return 0 for the function's size.  */
   13114   return size ? size : 1;
   13115 }
   13116 
   13117 /* Set to non-zero to enable some debug messages.  */
   13118 #define DEBUG_SECONDARY_RELOCS	 0
   13119 
   13120 /* An internal-to-the-bfd-library only section type
   13121    used to indicate a cached secondary reloc section.  */
   13122 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13123 
   13124 /* Create a BFD section to hold a secondary reloc section.  */
   13125 
   13126 bool
   13127 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13128 				       Elf_Internal_Shdr *hdr,
   13129 				       const char * name,
   13130 				       unsigned int shindex)
   13131 {
   13132   /* We only support RELA secondary relocs.  */
   13133   if (hdr->sh_type != SHT_RELA)
   13134     return false;
   13135 
   13136 #if DEBUG_SECONDARY_RELOCS
   13137   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13138 #endif
   13139   hdr->sh_type = SHT_SECONDARY_RELOC;
   13140   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13141 }
   13142 
   13143 /* Read in any secondary relocs associated with SEC.  */
   13144 
   13145 bool
   13146 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13147 					asection *  sec,
   13148 					asymbol **  symbols,
   13149 					bool dynamic)
   13150 {
   13151   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13152   asection * relsec;
   13153   bool result = true;
   13154   bfd_vma (*r_sym) (bfd_vma);
   13155 
   13156 #if BFD_DEFAULT_TARGET_SIZE > 32
   13157   if (bfd_arch_bits_per_address (abfd) != 32)
   13158     r_sym = elf64_r_sym;
   13159   else
   13160 #endif
   13161     r_sym = elf32_r_sym;
   13162 
   13163   if (!elf_section_data (sec)->has_secondary_relocs)
   13164     return true;
   13165 
   13166   /* Discover if there are any secondary reloc sections
   13167      associated with SEC.  */
   13168   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13169     {
   13170       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13171 
   13172       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13173 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13174 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13175 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13176 	{
   13177 	  bfd_byte * native_relocs;
   13178 	  bfd_byte * native_reloc;
   13179 	  arelent * internal_relocs;
   13180 	  arelent * internal_reloc;
   13181 	  unsigned int i;
   13182 	  unsigned int entsize;
   13183 	  unsigned int symcount;
   13184 	  unsigned int reloc_count;
   13185 	  size_t amt;
   13186 
   13187 	  if (ebd->elf_info_to_howto == NULL)
   13188 	    return false;
   13189 
   13190 #if DEBUG_SECONDARY_RELOCS
   13191 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13192 		   sec->name, relsec->name);
   13193 #endif
   13194 	  entsize = hdr->sh_entsize;
   13195 
   13196 	  native_relocs = bfd_malloc (hdr->sh_size);
   13197 	  if (native_relocs == NULL)
   13198 	    {
   13199 	      result = false;
   13200 	      continue;
   13201 	    }
   13202 
   13203 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13204 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13205 	    {
   13206 	      free (native_relocs);
   13207 	      bfd_set_error (bfd_error_file_too_big);
   13208 	      result = false;
   13209 	      continue;
   13210 	    }
   13211 
   13212 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   13213 	  if (internal_relocs == NULL)
   13214 	    {
   13215 	      free (native_relocs);
   13216 	      result = false;
   13217 	      continue;
   13218 	    }
   13219 
   13220 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   13221 	      || (bfd_bread (native_relocs, hdr->sh_size, abfd)
   13222 		  != hdr->sh_size))
   13223 	    {
   13224 	      free (native_relocs);
   13225 	      /* The internal_relocs will be freed when
   13226 		 the memory for the bfd is released.  */
   13227 	      result = false;
   13228 	      continue;
   13229 	    }
   13230 
   13231 	  if (dynamic)
   13232 	    symcount = bfd_get_dynamic_symcount (abfd);
   13233 	  else
   13234 	    symcount = bfd_get_symcount (abfd);
   13235 
   13236 	  for (i = 0, internal_reloc = internal_relocs,
   13237 		 native_reloc = native_relocs;
   13238 	       i < reloc_count;
   13239 	       i++, internal_reloc++, native_reloc += entsize)
   13240 	    {
   13241 	      bool res;
   13242 	      Elf_Internal_Rela rela;
   13243 
   13244 	      if (entsize == ebd->s->sizeof_rel)
   13245 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   13246 	      else /* entsize == ebd->s->sizeof_rela */
   13247 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   13248 
   13249 	      /* The address of an ELF reloc is section relative for an object
   13250 		 file, and absolute for an executable file or shared library.
   13251 		 The address of a normal BFD reloc is always section relative,
   13252 		 and the address of a dynamic reloc is absolute..  */
   13253 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   13254 		internal_reloc->address = rela.r_offset;
   13255 	      else
   13256 		internal_reloc->address = rela.r_offset - sec->vma;
   13257 
   13258 	      if (r_sym (rela.r_info) == STN_UNDEF)
   13259 		{
   13260 		  /* FIXME: This and the error case below mean that we
   13261 		     have a symbol on relocs that is not elf_symbol_type.  */
   13262 		  internal_reloc->sym_ptr_ptr =
   13263 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   13264 		}
   13265 	      else if (r_sym (rela.r_info) > symcount)
   13266 		{
   13267 		  _bfd_error_handler
   13268 		    /* xgettext:c-format */
   13269 		    (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
   13270 		     abfd, sec, i, (long) r_sym (rela.r_info));
   13271 		  bfd_set_error (bfd_error_bad_value);
   13272 		  internal_reloc->sym_ptr_ptr =
   13273 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   13274 		  result = false;
   13275 		}
   13276 	      else
   13277 		{
   13278 		  asymbol **ps;
   13279 
   13280 		  ps = symbols + r_sym (rela.r_info) - 1;
   13281 		  internal_reloc->sym_ptr_ptr = ps;
   13282 		  /* Make sure that this symbol is not removed by strip.  */
   13283 		  (*ps)->flags |= BSF_KEEP;
   13284 		}
   13285 
   13286 	      internal_reloc->addend = rela.r_addend;
   13287 
   13288 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   13289 	      if (! res || internal_reloc->howto == NULL)
   13290 		{
   13291 #if DEBUG_SECONDARY_RELOCS
   13292 		  fprintf (stderr, "there is no howto associated with reloc %lx\n",
   13293 			   rela.r_info);
   13294 #endif
   13295 		  result = false;
   13296 		}
   13297 	    }
   13298 
   13299 	  free (native_relocs);
   13300 	  /* Store the internal relocs.  */
   13301 	  elf_section_data (relsec)->sec_info = internal_relocs;
   13302 	}
   13303     }
   13304 
   13305   return result;
   13306 }
   13307 
   13308 /* Set the ELF section header fields of an output secondary reloc section.  */
   13309 
   13310 bool
   13311 _bfd_elf_copy_special_section_fields (const bfd *   ibfd ATTRIBUTE_UNUSED,
   13312 				      bfd *         obfd ATTRIBUTE_UNUSED,
   13313 				      const Elf_Internal_Shdr * isection,
   13314 				      Elf_Internal_Shdr *       osection)
   13315 {
   13316   asection * isec;
   13317   asection * osec;
   13318   struct bfd_elf_section_data * esd;
   13319 
   13320   if (isection == NULL)
   13321     return false;
   13322 
   13323   if (isection->sh_type != SHT_SECONDARY_RELOC)
   13324     return true;
   13325 
   13326   isec = isection->bfd_section;
   13327   if (isec == NULL)
   13328     return false;
   13329 
   13330   osec = osection->bfd_section;
   13331   if (osec == NULL)
   13332     return false;
   13333 
   13334   esd = elf_section_data (osec);
   13335   BFD_ASSERT (esd->sec_info == NULL);
   13336   esd->sec_info = elf_section_data (isec)->sec_info;
   13337   osection->sh_type = SHT_RELA;
   13338   osection->sh_link = elf_onesymtab (obfd);
   13339   if (osection->sh_link == 0)
   13340     {
   13341       /* There is no symbol table - we are hosed...  */
   13342       _bfd_error_handler
   13343 	/* xgettext:c-format */
   13344 	(_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
   13345 	obfd, osec);
   13346       bfd_set_error (bfd_error_bad_value);
   13347       return false;
   13348     }
   13349 
   13350   /* Find the output section that corresponds to the isection's sh_info link.  */
   13351   if (isection->sh_info == 0
   13352       || isection->sh_info >= elf_numsections (ibfd))
   13353     {
   13354       _bfd_error_handler
   13355 	/* xgettext:c-format */
   13356 	(_("%pB(%pA): info section index is invalid"),
   13357 	obfd, osec);
   13358       bfd_set_error (bfd_error_bad_value);
   13359       return false;
   13360     }
   13361 
   13362   isection = elf_elfsections (ibfd)[isection->sh_info];
   13363 
   13364   if (isection == NULL
   13365       || isection->bfd_section == NULL
   13366       || isection->bfd_section->output_section == NULL)
   13367     {
   13368       _bfd_error_handler
   13369 	/* xgettext:c-format */
   13370 	(_("%pB(%pA): info section index cannot be set because the section is not in the output"),
   13371 	obfd, osec);
   13372       bfd_set_error (bfd_error_bad_value);
   13373       return false;
   13374     }
   13375 
   13376   esd = elf_section_data (isection->bfd_section->output_section);
   13377   BFD_ASSERT (esd != NULL);
   13378   osection->sh_info = esd->this_idx;
   13379   esd->has_secondary_relocs = true;
   13380 #if DEBUG_SECONDARY_RELOCS
   13381   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   13382 	   osec->name, osection->sh_link, osection->sh_info);
   13383   fprintf (stderr, "mark section %s as having secondary relocs\n",
   13384 	   bfd_section_name (isection->bfd_section->output_section));
   13385 #endif
   13386 
   13387   return true;
   13388 }
   13389 
   13390 /* Write out a secondary reloc section.
   13391 
   13392    FIXME: Currently this function can result in a serious performance penalty
   13393    for files with secondary relocs and lots of sections.  The proper way to
   13394    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   13395    relocs together and then to have this function just walk that chain.  */
   13396 
   13397 bool
   13398 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   13399 {
   13400   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13401   bfd_vma addr_offset;
   13402   asection * relsec;
   13403   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   13404   bool result = true;
   13405 
   13406   if (sec == NULL)
   13407     return false;
   13408 
   13409 #if BFD_DEFAULT_TARGET_SIZE > 32
   13410   if (bfd_arch_bits_per_address (abfd) != 32)
   13411     r_info = elf64_r_info;
   13412   else
   13413 #endif
   13414     r_info = elf32_r_info;
   13415 
   13416   /* The address of an ELF reloc is section relative for an object
   13417      file, and absolute for an executable file or shared library.
   13418      The address of a BFD reloc is always section relative.  */
   13419   addr_offset = 0;
   13420   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   13421     addr_offset = sec->vma;
   13422 
   13423   /* Discover if there are any secondary reloc sections
   13424      associated with SEC.  */
   13425   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13426     {
   13427       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   13428       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   13429 
   13430       if (hdr->sh_type == SHT_RELA
   13431 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   13432 	{
   13433 	  asymbol *    last_sym;
   13434 	  int          last_sym_idx;
   13435 	  unsigned int reloc_count;
   13436 	  unsigned int idx;
   13437 	  unsigned int entsize;
   13438 	  arelent *    src_irel;
   13439 	  bfd_byte *   dst_rela;
   13440 
   13441 	  if (hdr->contents != NULL)
   13442 	    {
   13443 	      _bfd_error_handler
   13444 		/* xgettext:c-format */
   13445 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   13446 		 abfd, relsec);
   13447 	      bfd_set_error (bfd_error_bad_value);
   13448 	      result = false;
   13449 	      continue;
   13450 	    }
   13451 
   13452 	  entsize = hdr->sh_entsize;
   13453 	  if (entsize == 0)
   13454 	    {
   13455 	      _bfd_error_handler
   13456 		/* xgettext:c-format */
   13457 		(_("%pB(%pA): error: secondary reloc section has zero sized entries"),
   13458 		 abfd, relsec);
   13459 	      bfd_set_error (bfd_error_bad_value);
   13460 	      result = false;
   13461 	      continue;
   13462 	    }
   13463 	  else if (entsize != ebd->s->sizeof_rel
   13464 		   && entsize != ebd->s->sizeof_rela)
   13465 	    {
   13466 	      _bfd_error_handler
   13467 		/* xgettext:c-format */
   13468 		(_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
   13469 		 abfd, relsec);
   13470 	      bfd_set_error (bfd_error_bad_value);
   13471 	      result = false;
   13472 	      continue;
   13473 	    }
   13474 
   13475 	  reloc_count = hdr->sh_size / entsize;
   13476 	  if (reloc_count <= 0)
   13477 	    {
   13478 	      _bfd_error_handler
   13479 		/* xgettext:c-format */
   13480 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   13481 		 abfd, relsec);
   13482 	      bfd_set_error (bfd_error_bad_value);
   13483 	      result = false;
   13484 	      continue;
   13485 	    }
   13486 
   13487 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   13488 	  if (hdr->contents == NULL)
   13489 	    continue;
   13490 
   13491 #if DEBUG_SECONDARY_RELOCS
   13492 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   13493 		   reloc_count, sec->name, relsec->name);
   13494 #endif
   13495 	  last_sym = NULL;
   13496 	  last_sym_idx = 0;
   13497 	  dst_rela = hdr->contents;
   13498 	  src_irel = (arelent *) esd->sec_info;
   13499 	  if (src_irel == NULL)
   13500 	    {
   13501 	      _bfd_error_handler
   13502 		/* xgettext:c-format */
   13503 		(_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
   13504 		 abfd, relsec);
   13505 	      bfd_set_error (bfd_error_bad_value);
   13506 	      result = false;
   13507 	      continue;
   13508 	    }
   13509 
   13510 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   13511 	    {
   13512 	      Elf_Internal_Rela src_rela;
   13513 	      arelent *ptr;
   13514 	      asymbol *sym;
   13515 	      int n;
   13516 
   13517 	      ptr = src_irel + idx;
   13518 	      if (ptr == NULL)
   13519 		{
   13520 		  _bfd_error_handler
   13521 		    /* xgettext:c-format */
   13522 		    (_("%pB(%pA): error: reloc table entry %u is empty"),
   13523 		     abfd, relsec, idx);
   13524 		  bfd_set_error (bfd_error_bad_value);
   13525 		  result = false;
   13526 		  break;
   13527 		}
   13528 
   13529 	      if (ptr->sym_ptr_ptr == NULL)
   13530 		{
   13531 		  /* FIXME: Is this an error ? */
   13532 		  n = 0;
   13533 		}
   13534 	      else
   13535 		{
   13536 		  sym = *ptr->sym_ptr_ptr;
   13537 
   13538 		  if (sym == last_sym)
   13539 		    n = last_sym_idx;
   13540 		  else
   13541 		    {
   13542 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   13543 		      if (n < 0)
   13544 			{
   13545 			  _bfd_error_handler
   13546 			    /* xgettext:c-format */
   13547 			    (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
   13548 			     abfd, relsec, idx);
   13549 			  bfd_set_error (bfd_error_bad_value);
   13550 			  result = false;
   13551 			  n = 0;
   13552 			}
   13553 
   13554 		      last_sym = sym;
   13555 		      last_sym_idx = n;
   13556 		    }
   13557 
   13558 		  if (sym->the_bfd != NULL
   13559 		      && sym->the_bfd->xvec != abfd->xvec
   13560 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   13561 		    {
   13562 		      _bfd_error_handler
   13563 			/* xgettext:c-format */
   13564 			(_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
   13565 			 abfd, relsec, idx);
   13566 		      bfd_set_error (bfd_error_bad_value);
   13567 		      result = false;
   13568 		      n = 0;
   13569 		    }
   13570 		}
   13571 
   13572 	      src_rela.r_offset = ptr->address + addr_offset;
   13573 	      if (ptr->howto == NULL)
   13574 		{
   13575 		  _bfd_error_handler
   13576 		    /* xgettext:c-format */
   13577 		    (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
   13578 		     abfd, relsec, idx);
   13579 		  bfd_set_error (bfd_error_bad_value);
   13580 		  result = false;
   13581 		  src_rela.r_info = r_info (0, 0);
   13582 		}
   13583 	      else
   13584 		src_rela.r_info = r_info (n, ptr->howto->type);
   13585 	      src_rela.r_addend = ptr->addend;
   13586 
   13587 	      if (entsize == ebd->s->sizeof_rel)
   13588 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   13589 	      else /* entsize == ebd->s->sizeof_rela */
   13590 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   13591 	    }
   13592 	}
   13593     }
   13594 
   13595   return result;
   13596 }
   13597