Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.15
      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_seek (abfd, offset, SEEK_SET) != 0
    301 	  || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
    302 					      shstrtabsize)) == NULL)
    303 	{
    304 	  /* Once we've failed to read it, make sure we don't keep
    305 	     trying.  Otherwise, we'll keep allocating space for
    306 	     the string table over and over.  */
    307 	  i_shdrp[shindex]->sh_size = 0;
    308 	}
    309       else
    310 	shstrtab[shstrtabsize] = '\0';
    311       i_shdrp[shindex]->contents = shstrtab;
    312     }
    313   return (char *) shstrtab;
    314 }
    315 
    316 char *
    317 bfd_elf_string_from_elf_section (bfd *abfd,
    318 				 unsigned int shindex,
    319 				 unsigned int strindex)
    320 {
    321   Elf_Internal_Shdr *hdr;
    322 
    323   if (strindex == 0)
    324     return "";
    325 
    326   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    327     return NULL;
    328 
    329   hdr = elf_elfsections (abfd)[shindex];
    330 
    331   if (hdr->contents == NULL)
    332     {
    333       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    334 	{
    335 	  /* PR 17512: file: f057ec89.  */
    336 	  /* xgettext:c-format */
    337 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    338 				" a non-string section (number %d)"),
    339 			      abfd, shindex);
    340 	  return NULL;
    341 	}
    342 
    343       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    344 	return NULL;
    345     }
    346   else
    347     {
    348       /* PR 24273: The string section's contents may have already
    349 	 been loaded elsewhere, eg because a corrupt file has the
    350 	 string section index in the ELF header pointing at a group
    351 	 section.  So be paranoid, and test that the last byte of
    352 	 the section is zero.  */
    353       if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
    354 	return NULL;
    355     }
    356 
    357   if (strindex >= hdr->sh_size)
    358     {
    359       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    360       _bfd_error_handler
    361 	/* xgettext:c-format */
    362 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    363 	 abfd, strindex, (uint64_t) hdr->sh_size,
    364 	 (shindex == shstrndx && strindex == hdr->sh_name
    365 	  ? ".shstrtab"
    366 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    367       return NULL;
    368     }
    369 
    370   return ((char *) hdr->contents) + strindex;
    371 }
    372 
    373 /* Read and convert symbols to internal format.
    374    SYMCOUNT specifies the number of symbols to read, starting from
    375    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    376    are non-NULL, they are used to store the internal symbols, external
    377    symbols, and symbol section index extensions, respectively.
    378    Returns a pointer to the internal symbol buffer (malloced if necessary)
    379    or NULL if there were no symbols or some kind of problem.  */
    380 
    381 Elf_Internal_Sym *
    382 bfd_elf_get_elf_syms (bfd *ibfd,
    383 		      Elf_Internal_Shdr *symtab_hdr,
    384 		      size_t symcount,
    385 		      size_t symoffset,
    386 		      Elf_Internal_Sym *intsym_buf,
    387 		      void *extsym_buf,
    388 		      Elf_External_Sym_Shndx *extshndx_buf)
    389 {
    390   Elf_Internal_Shdr *shndx_hdr;
    391   void *alloc_ext;
    392   const bfd_byte *esym;
    393   Elf_External_Sym_Shndx *alloc_extshndx;
    394   Elf_External_Sym_Shndx *shndx;
    395   Elf_Internal_Sym *alloc_intsym;
    396   Elf_Internal_Sym *isym;
    397   Elf_Internal_Sym *isymend;
    398   const struct elf_backend_data *bed;
    399   size_t extsym_size;
    400   size_t amt;
    401   file_ptr pos;
    402 
    403   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    404     abort ();
    405 
    406   if (symcount == 0)
    407     return intsym_buf;
    408 
    409   /* Normal syms might have section extension entries.  */
    410   shndx_hdr = NULL;
    411   if (elf_symtab_shndx_list (ibfd) != NULL)
    412     {
    413       elf_section_list * entry;
    414       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    415 
    416       /* Find an index section that is linked to this symtab section.  */
    417       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    418 	{
    419 	  /* PR 20063.  */
    420 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    421 	    continue;
    422 
    423 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    424 	    {
    425 	      shndx_hdr = & entry->hdr;
    426 	      break;
    427 	    };
    428 	}
    429 
    430       if (shndx_hdr == NULL)
    431 	{
    432 	  if (symtab_hdr == &elf_symtab_hdr (ibfd))
    433 	    /* Not really accurate, but this was how the old code used
    434 	       to work.  */
    435 	    shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
    436 	  /* Otherwise we do nothing.  The assumption is that
    437 	     the index table will not be needed.  */
    438 	}
    439     }
    440 
    441   /* Read the symbols.  */
    442   alloc_ext = NULL;
    443   alloc_extshndx = NULL;
    444   alloc_intsym = NULL;
    445   bed = get_elf_backend_data (ibfd);
    446   extsym_size = bed->s->sizeof_sym;
    447   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
    448     {
    449       bfd_set_error (bfd_error_file_too_big);
    450       intsym_buf = NULL;
    451       goto out;
    452     }
    453   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    454   if (extsym_buf == NULL)
    455     {
    456       alloc_ext = bfd_malloc (amt);
    457       extsym_buf = alloc_ext;
    458     }
    459   if (extsym_buf == NULL
    460       || bfd_seek (ibfd, pos, SEEK_SET) != 0
    461       || bfd_bread (extsym_buf, amt, ibfd) != amt)
    462     {
    463       intsym_buf = NULL;
    464       goto out;
    465     }
    466 
    467   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    468     extshndx_buf = NULL;
    469   else
    470     {
    471       if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
    472 	{
    473 	  bfd_set_error (bfd_error_file_too_big);
    474 	  intsym_buf = NULL;
    475 	  goto out;
    476 	}
    477       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    478       if (extshndx_buf == NULL)
    479 	{
    480 	  alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
    481 	  extshndx_buf = alloc_extshndx;
    482 	}
    483       if (extshndx_buf == NULL
    484 	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
    485 	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
    486 	{
    487 	  intsym_buf = NULL;
    488 	  goto out;
    489 	}
    490     }
    491 
    492   if (intsym_buf == NULL)
    493     {
    494       if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
    495 	{
    496 	  bfd_set_error (bfd_error_file_too_big);
    497 	  goto out;
    498 	}
    499       alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
    500       intsym_buf = alloc_intsym;
    501       if (intsym_buf == NULL)
    502 	goto out;
    503     }
    504 
    505   /* Convert the symbols to internal form.  */
    506   isymend = intsym_buf + symcount;
    507   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    508 	   shndx = extshndx_buf;
    509        isym < isymend;
    510        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    511     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    512       {
    513 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    514 	/* xgettext:c-format */
    515 	_bfd_error_handler (_("%pB symbol number %lu references"
    516 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    517 			    ibfd, (unsigned long) symoffset);
    518 	free (alloc_intsym);
    519 	intsym_buf = NULL;
    520 	goto out;
    521       }
    522 
    523  out:
    524   free (alloc_ext);
    525   free (alloc_extshndx);
    526 
    527   return intsym_buf;
    528 }
    529 
    530 /* Look up a symbol name.  */
    531 const char *
    532 bfd_elf_sym_name (bfd *abfd,
    533 		  Elf_Internal_Shdr *symtab_hdr,
    534 		  Elf_Internal_Sym *isym,
    535 		  asection *sym_sec)
    536 {
    537   const char *name;
    538   unsigned int iname = isym->st_name;
    539   unsigned int shindex = symtab_hdr->sh_link;
    540 
    541   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    542       /* Check for a bogus st_shndx to avoid crashing.  */
    543       && isym->st_shndx < elf_numsections (abfd))
    544     {
    545       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    546       shindex = elf_elfheader (abfd)->e_shstrndx;
    547     }
    548 
    549   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    550   if (name == NULL)
    551     name = "(null)";
    552   else if (sym_sec && *name == '\0')
    553     name = bfd_section_name (sym_sec);
    554 
    555   return name;
    556 }
    557 
    558 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    559    sections.  The first element is the flags, the rest are section
    560    pointers.  */
    561 
    562 typedef union elf_internal_group {
    563   Elf_Internal_Shdr *shdr;
    564   unsigned int flags;
    565 } Elf_Internal_Group;
    566 
    567 /* Return the name of the group signature symbol.  Why isn't the
    568    signature just a string?  */
    569 
    570 static const char *
    571 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    572 {
    573   Elf_Internal_Shdr *hdr;
    574   unsigned char esym[sizeof (Elf64_External_Sym)];
    575   Elf_External_Sym_Shndx eshndx;
    576   Elf_Internal_Sym isym;
    577 
    578   /* First we need to ensure the symbol table is available.  Make sure
    579      that it is a symbol table section.  */
    580   if (ghdr->sh_link >= elf_numsections (abfd))
    581     return NULL;
    582   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    583   if (hdr->sh_type != SHT_SYMTAB
    584       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    585     return NULL;
    586 
    587   /* Go read the symbol.  */
    588   hdr = &elf_tdata (abfd)->symtab_hdr;
    589   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    590 			    &isym, esym, &eshndx) == NULL)
    591     return NULL;
    592 
    593   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    594 }
    595 
    596 /* Set next_in_group list pointer, and group name for NEWSECT.  */
    597 
    598 static bool
    599 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    600 {
    601   unsigned int num_group = elf_tdata (abfd)->num_group;
    602 
    603   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    604      is set to -1 if there are no SHT_GROUP sections.  */
    605   if (num_group == 0)
    606     {
    607       unsigned int i, shnum;
    608 
    609       /* First count the number of groups.  If we have a SHT_GROUP
    610 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    611       shnum = elf_numsections (abfd);
    612       num_group = 0;
    613 
    614 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    615 	(   (shdr)->sh_type == SHT_GROUP		\
    616 	 && (shdr)->sh_size >= minsize			\
    617 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    618 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    619 
    620       for (i = 0; i < shnum; i++)
    621 	{
    622 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    623 
    624 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    625 	    num_group += 1;
    626 	}
    627 
    628       if (num_group == 0)
    629 	{
    630 	  num_group = (unsigned) -1;
    631 	  elf_tdata (abfd)->num_group = num_group;
    632 	  elf_tdata (abfd)->group_sect_ptr = NULL;
    633 	}
    634       else
    635 	{
    636 	  /* We keep a list of elf section headers for group sections,
    637 	     so we can find them quickly.  */
    638 	  size_t amt;
    639 
    640 	  elf_tdata (abfd)->num_group = num_group;
    641 	  amt = num_group * sizeof (Elf_Internal_Shdr *);
    642 	  elf_tdata (abfd)->group_sect_ptr
    643 	    = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
    644 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    645 	    return false;
    646 	  num_group = 0;
    647 
    648 	  for (i = 0; i < shnum; i++)
    649 	    {
    650 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    651 
    652 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    653 		{
    654 		  unsigned char *src;
    655 		  Elf_Internal_Group *dest;
    656 
    657 		  /* Make sure the group section has a BFD section
    658 		     attached to it.  */
    659 		  if (!bfd_section_from_shdr (abfd, i))
    660 		    return false;
    661 
    662 		  /* Add to list of sections.  */
    663 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    664 		  num_group += 1;
    665 
    666 		  /* Read the raw contents.  */
    667 		  BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
    668 		  shdr->contents = NULL;
    669 		  if (_bfd_mul_overflow (shdr->sh_size,
    670 					 sizeof (*dest) / 4, &amt)
    671 		      || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    672 		      || !(shdr->contents
    673 			   = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
    674 		    {
    675 		      _bfd_error_handler
    676 			/* xgettext:c-format */
    677 			(_("%pB: invalid size field in group section"
    678 			   " header: %#" PRIx64 ""),
    679 			 abfd, (uint64_t) shdr->sh_size);
    680 		      bfd_set_error (bfd_error_bad_value);
    681 		      -- num_group;
    682 		      continue;
    683 		    }
    684 
    685 		  /* Translate raw contents, a flag word followed by an
    686 		     array of elf section indices all in target byte order,
    687 		     to the flag word followed by an array of elf section
    688 		     pointers.  */
    689 		  src = shdr->contents + shdr->sh_size;
    690 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    691 
    692 		  while (1)
    693 		    {
    694 		      unsigned int idx;
    695 
    696 		      src -= 4;
    697 		      --dest;
    698 		      idx = H_GET_32 (abfd, src);
    699 		      if (src == shdr->contents)
    700 			{
    701 			  dest->shdr = NULL;
    702 			  dest->flags = idx;
    703 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    704 			    shdr->bfd_section->flags
    705 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    706 			  break;
    707 			}
    708 		      if (idx < shnum)
    709 			{
    710 			  dest->shdr = elf_elfsections (abfd)[idx];
    711 			  /* PR binutils/23199: All sections in a
    712 			     section group should be marked with
    713 			     SHF_GROUP.  But some tools generate
    714 			     broken objects without SHF_GROUP.  Fix
    715 			     them up here.  */
    716 			  dest->shdr->sh_flags |= SHF_GROUP;
    717 			}
    718 		      if (idx >= shnum
    719 			  || dest->shdr->sh_type == SHT_GROUP)
    720 			{
    721 			  _bfd_error_handler
    722 			    (_("%pB: invalid entry in SHT_GROUP section [%u]"),
    723 			       abfd, i);
    724 			  dest->shdr = NULL;
    725 			}
    726 		    }
    727 		}
    728 	    }
    729 
    730 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    731 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    732 	    {
    733 	      elf_tdata (abfd)->num_group = num_group;
    734 
    735 	      /* If all groups are invalid then fail.  */
    736 	      if (num_group == 0)
    737 		{
    738 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    739 		  elf_tdata (abfd)->num_group = num_group = -1;
    740 		  _bfd_error_handler
    741 		    (_("%pB: no valid group sections found"), abfd);
    742 		  bfd_set_error (bfd_error_bad_value);
    743 		}
    744 	    }
    745 	}
    746     }
    747 
    748   if (num_group != (unsigned) -1)
    749     {
    750       unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
    751       unsigned int j;
    752 
    753       for (j = 0; j < num_group; j++)
    754 	{
    755 	  /* Begin search from previous found group.  */
    756 	  unsigned i = (j + search_offset) % num_group;
    757 
    758 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    759 	  Elf_Internal_Group *idx;
    760 	  bfd_size_type n_elt;
    761 
    762 	  if (shdr == NULL)
    763 	    continue;
    764 
    765 	  idx = (Elf_Internal_Group *) shdr->contents;
    766 	  if (idx == NULL || shdr->sh_size < 4)
    767 	    {
    768 	      /* See PR 21957 for a reproducer.  */
    769 	      /* xgettext:c-format */
    770 	      _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
    771 				  abfd, shdr->bfd_section);
    772 	      elf_tdata (abfd)->group_sect_ptr[i] = NULL;
    773 	      bfd_set_error (bfd_error_bad_value);
    774 	      return false;
    775 	    }
    776 	  n_elt = shdr->sh_size / 4;
    777 
    778 	  /* Look through this group's sections to see if current
    779 	     section is a member.  */
    780 	  while (--n_elt != 0)
    781 	    if ((++idx)->shdr == hdr)
    782 	      {
    783 		asection *s = NULL;
    784 
    785 		/* We are a member of this group.  Go looking through
    786 		   other members to see if any others are linked via
    787 		   next_in_group.  */
    788 		idx = (Elf_Internal_Group *) shdr->contents;
    789 		n_elt = shdr->sh_size / 4;
    790 		while (--n_elt != 0)
    791 		  if ((++idx)->shdr != NULL
    792 		      && (s = idx->shdr->bfd_section) != NULL
    793 		      && elf_next_in_group (s) != NULL)
    794 		    break;
    795 		if (n_elt != 0)
    796 		  {
    797 		    /* Snarf the group name from other member, and
    798 		       insert current section in circular list.  */
    799 		    elf_group_name (newsect) = elf_group_name (s);
    800 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    801 		    elf_next_in_group (s) = newsect;
    802 		  }
    803 		else
    804 		  {
    805 		    const char *gname;
    806 
    807 		    gname = group_signature (abfd, shdr);
    808 		    if (gname == NULL)
    809 		      return false;
    810 		    elf_group_name (newsect) = gname;
    811 
    812 		    /* Start a circular list with one element.  */
    813 		    elf_next_in_group (newsect) = newsect;
    814 		  }
    815 
    816 		/* If the group section has been created, point to the
    817 		   new member.  */
    818 		if (shdr->bfd_section != NULL)
    819 		  elf_next_in_group (shdr->bfd_section) = newsect;
    820 
    821 		elf_tdata (abfd)->group_search_offset = i;
    822 		j = num_group - 1;
    823 		break;
    824 	      }
    825 	}
    826     }
    827 
    828   if (elf_group_name (newsect) == NULL)
    829     {
    830       /* xgettext:c-format */
    831       _bfd_error_handler (_("%pB: no group info for section '%pA'"),
    832 			  abfd, newsect);
    833       /* PR 29532: Return true here, even though the group info has not been
    834 	 read.  Separate debug info files can have empty group sections, but
    835 	 we do not want this to prevent them from being loaded as otherwise
    836 	 GDB will not be able to use them.  */
    837       return true;
    838     }
    839   return true;
    840 }
    841 
    842 bool
    843 _bfd_elf_setup_sections (bfd *abfd)
    844 {
    845   unsigned int i;
    846   unsigned int num_group = elf_tdata (abfd)->num_group;
    847   bool result = true;
    848   asection *s;
    849 
    850   /* Process SHF_LINK_ORDER.  */
    851   for (s = abfd->sections; s != NULL; s = s->next)
    852     {
    853       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    854       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    855 	{
    856 	  unsigned int elfsec = this_hdr->sh_link;
    857 	  /* An sh_link value of 0 is now allowed.  It indicates that linked
    858 	     to section has already been discarded, but that the current
    859 	     section has been retained for some other reason.  This linking
    860 	     section is still a candidate for later garbage collection
    861 	     however.  */
    862 	  if (elfsec == 0)
    863 	    {
    864 	      elf_linked_to_section (s) = NULL;
    865 	    }
    866 	  else
    867 	    {
    868 	      asection *linksec = NULL;
    869 
    870 	      if (elfsec < elf_numsections (abfd))
    871 		{
    872 		  this_hdr = elf_elfsections (abfd)[elfsec];
    873 		  linksec = this_hdr->bfd_section;
    874 		}
    875 
    876 	      /* PR 1991, 2008:
    877 		 Some strip/objcopy may leave an incorrect value in
    878 		 sh_link.  We don't want to proceed.  */
    879 	      if (linksec == NULL)
    880 		{
    881 		  _bfd_error_handler
    882 		    /* xgettext:c-format */
    883 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    884 		     s->owner, elfsec, s);
    885 		  result = false;
    886 		}
    887 
    888 	      elf_linked_to_section (s) = linksec;
    889 	    }
    890 	}
    891       else if (this_hdr->sh_type == SHT_GROUP
    892 	       && elf_next_in_group (s) == NULL)
    893 	{
    894 	  _bfd_error_handler
    895 	    /* xgettext:c-format */
    896 	    (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
    897 	     abfd, elf_section_data (s)->this_idx);
    898 	  result = false;
    899 	}
    900     }
    901 
    902   /* Process section groups.  */
    903   if (num_group == (unsigned) -1)
    904     return result;
    905 
    906   for (i = 0; i < num_group; i++)
    907     {
    908       Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    909       Elf_Internal_Group *idx;
    910       unsigned int n_elt;
    911 
    912       /* PR binutils/18758: Beware of corrupt binaries with invalid
    913 	 group data.  */
    914       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    915 	{
    916 	  _bfd_error_handler
    917 	    /* xgettext:c-format */
    918 	    (_("%pB: section group entry number %u is corrupt"),
    919 	     abfd, i);
    920 	  result = false;
    921 	  continue;
    922 	}
    923 
    924       idx = (Elf_Internal_Group *) shdr->contents;
    925       n_elt = shdr->sh_size / 4;
    926 
    927       while (--n_elt != 0)
    928 	{
    929 	  ++ idx;
    930 
    931 	  if (idx->shdr == NULL)
    932 	    continue;
    933 	  else if (idx->shdr->bfd_section)
    934 	    elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    935 	  else if (idx->shdr->sh_type != SHT_RELA
    936 		   && idx->shdr->sh_type != SHT_REL)
    937 	    {
    938 	      /* There are some unknown sections in the group.  */
    939 	      _bfd_error_handler
    940 		/* xgettext:c-format */
    941 		(_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
    942 		 abfd,
    943 		 idx->shdr->sh_type,
    944 		 bfd_elf_string_from_elf_section (abfd,
    945 						  (elf_elfheader (abfd)
    946 						   ->e_shstrndx),
    947 						  idx->shdr->sh_name),
    948 		 shdr->bfd_section);
    949 	      result = false;
    950 	    }
    951 	}
    952     }
    953 
    954   return result;
    955 }
    956 
    957 bool
    958 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    959 {
    960   return elf_next_in_group (sec) != NULL;
    961 }
    962 
    963 const char *
    964 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    965 {
    966   if (elf_sec_group (sec) != NULL)
    967     return elf_group_name (sec);
    968   return NULL;
    969 }
    970 
    971 /* This a copy of lto_section defined in GCC (lto-streamer.h).  */
    972 
    973 struct lto_section
    974 {
    975   int16_t major_version;
    976   int16_t minor_version;
    977   unsigned char slim_object;
    978 
    979   /* Flags is a private field that is not defined publicly.  */
    980   uint16_t flags;
    981 };
    982 
    983 /* Make a BFD section from an ELF section.  We store a pointer to the
    984    BFD section in the bfd_section field of the header.  */
    985 
    986 bool
    987 _bfd_elf_make_section_from_shdr (bfd *abfd,
    988 				 Elf_Internal_Shdr *hdr,
    989 				 const char *name,
    990 				 int shindex)
    991 {
    992   asection *newsect;
    993   flagword flags;
    994   const struct elf_backend_data *bed;
    995   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
    996 
    997   if (hdr->bfd_section != NULL)
    998     return true;
    999 
   1000   newsect = bfd_make_section_anyway (abfd, name);
   1001   if (newsect == NULL)
   1002     return false;
   1003 
   1004   hdr->bfd_section = newsect;
   1005   elf_section_data (newsect)->this_hdr = *hdr;
   1006   elf_section_data (newsect)->this_idx = shindex;
   1007 
   1008   /* Always use the real type/flags.  */
   1009   elf_section_type (newsect) = hdr->sh_type;
   1010   elf_section_flags (newsect) = hdr->sh_flags;
   1011 
   1012   newsect->filepos = hdr->sh_offset;
   1013 
   1014   flags = SEC_NO_FLAGS;
   1015   if (hdr->sh_type != SHT_NOBITS)
   1016     flags |= SEC_HAS_CONTENTS;
   1017   if (hdr->sh_type == SHT_GROUP)
   1018     flags |= SEC_GROUP;
   1019   if ((hdr->sh_flags & SHF_ALLOC) != 0)
   1020     {
   1021       flags |= SEC_ALLOC;
   1022       if (hdr->sh_type != SHT_NOBITS)
   1023 	flags |= SEC_LOAD;
   1024     }
   1025   if ((hdr->sh_flags & SHF_WRITE) == 0)
   1026     flags |= SEC_READONLY;
   1027   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
   1028     flags |= SEC_CODE;
   1029   else if ((flags & SEC_LOAD) != 0)
   1030     flags |= SEC_DATA;
   1031   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1032     {
   1033       flags |= SEC_MERGE;
   1034       newsect->entsize = hdr->sh_entsize;
   1035     }
   1036   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1037     flags |= SEC_STRINGS;
   1038   if (hdr->sh_flags & SHF_GROUP)
   1039     if (!setup_group (abfd, hdr, newsect))
   1040       return false;
   1041   if ((hdr->sh_flags & SHF_TLS) != 0)
   1042     flags |= SEC_THREAD_LOCAL;
   1043   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1044     flags |= SEC_EXCLUDE;
   1045 
   1046   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
   1047     {
   1048       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
   1049 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
   1050 	 byte.  */
   1051     case ELFOSABI_GNU:
   1052     case ELFOSABI_FREEBSD:
   1053       if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
   1054 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
   1055       /* Fall through */
   1056     case ELFOSABI_NONE:
   1057       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
   1058 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
   1059       break;
   1060     }
   1061 
   1062   if ((flags & SEC_ALLOC) == 0)
   1063     {
   1064       /* The debugging sections appear to be recognized only by name,
   1065 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1066       if (name [0] == '.')
   1067 	{
   1068 	  if (startswith (name, ".debug")
   1069 	      || startswith (name, ".gnu.debuglto_.debug_")
   1070 	      || startswith (name, ".gnu.linkonce.wi.")
   1071 	      || startswith (name, ".zdebug"))
   1072 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
   1073 	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
   1074 		   || startswith (name, ".note.gnu"))
   1075 	    {
   1076 	      flags |= SEC_ELF_OCTETS;
   1077 	      opb = 1;
   1078 	    }
   1079 	  else if (startswith (name, ".line")
   1080 		   || startswith (name, ".stab")
   1081 		   || strcmp (name, ".gdb_index") == 0)
   1082 	    flags |= SEC_DEBUGGING;
   1083 	}
   1084     }
   1085 
   1086   if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
   1087       || !bfd_set_section_size (newsect, hdr->sh_size)
   1088       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
   1089 							& -hdr->sh_addralign)))
   1090     return false;
   1091 
   1092   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1093      only link a single copy of the section.  This is used to support
   1094      g++.  g++ will emit each template expansion in its own section.
   1095      The symbols will be defined as weak, so that multiple definitions
   1096      are permitted.  The GNU linker extension is to actually discard
   1097      all but one of the sections.  */
   1098   if (startswith (name, ".gnu.linkonce")
   1099       && elf_next_in_group (newsect) == NULL)
   1100     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1101 
   1102   if (!bfd_set_section_flags (newsect, flags))
   1103     return false;
   1104 
   1105   bed = get_elf_backend_data (abfd);
   1106   if (bed->elf_backend_section_flags)
   1107     if (!bed->elf_backend_section_flags (hdr))
   1108       return false;
   1109 
   1110   /* We do not parse the PT_NOTE segments as we are interested even in the
   1111      separate debug info files which may have the segments offsets corrupted.
   1112      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1113   if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
   1114     {
   1115       bfd_byte *contents;
   1116 
   1117       if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
   1118 	return false;
   1119 
   1120       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1121 		       hdr->sh_offset, hdr->sh_addralign);
   1122       free (contents);
   1123     }
   1124 
   1125   if ((newsect->flags & SEC_ALLOC) != 0)
   1126     {
   1127       Elf_Internal_Phdr *phdr;
   1128       unsigned int i, nload;
   1129 
   1130       /* Some ELF linkers produce binaries with all the program header
   1131 	 p_paddr fields zero.  If we have such a binary with more than
   1132 	 one PT_LOAD header, then leave the section lma equal to vma
   1133 	 so that we don't create sections with overlapping lma.  */
   1134       phdr = elf_tdata (abfd)->phdr;
   1135       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1136 	if (phdr->p_paddr != 0)
   1137 	  break;
   1138 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1139 	  ++nload;
   1140       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1141 	return true;
   1142 
   1143       phdr = elf_tdata (abfd)->phdr;
   1144       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1145 	{
   1146 	  if (((phdr->p_type == PT_LOAD
   1147 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1148 	       || phdr->p_type == PT_TLS)
   1149 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1150 	    {
   1151 	      if ((newsect->flags & SEC_LOAD) == 0)
   1152 		newsect->lma = (phdr->p_paddr
   1153 				+ hdr->sh_addr - phdr->p_vaddr) / opb;
   1154 	      else
   1155 		/* We used to use the same adjustment for SEC_LOAD
   1156 		   sections, but that doesn't work if the segment
   1157 		   is packed with code from multiple VMAs.
   1158 		   Instead we calculate the section LMA based on
   1159 		   the segment LMA.  It is assumed that the
   1160 		   segment will contain sections with contiguous
   1161 		   LMAs, even if the VMAs are not.  */
   1162 		newsect->lma = (phdr->p_paddr
   1163 				+ hdr->sh_offset - phdr->p_offset) / opb;
   1164 
   1165 	      /* With contiguous segments, we can't tell from file
   1166 		 offsets whether a section with zero size should
   1167 		 be placed at the end of one segment or the
   1168 		 beginning of the next.  Decide based on vaddr.  */
   1169 	      if (hdr->sh_addr >= phdr->p_vaddr
   1170 		  && (hdr->sh_addr + hdr->sh_size
   1171 		      <= phdr->p_vaddr + phdr->p_memsz))
   1172 		break;
   1173 	    }
   1174 	}
   1175     }
   1176 
   1177   /* Compress/decompress DWARF debug sections with names: .debug_*,
   1178      .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
   1179   if ((newsect->flags & SEC_DEBUGGING) != 0
   1180       && (newsect->flags & SEC_HAS_CONTENTS) != 0
   1181       && (newsect->flags & SEC_ELF_OCTETS) != 0)
   1182     {
   1183       enum { nothing, compress, decompress } action = nothing;
   1184       int compression_header_size;
   1185       bfd_size_type uncompressed_size;
   1186       unsigned int uncompressed_align_power;
   1187       enum compression_type ch_type = ch_none;
   1188       bool compressed
   1189 	= bfd_is_section_compressed_info (abfd, newsect,
   1190 					  &compression_header_size,
   1191 					  &uncompressed_size,
   1192 					  &uncompressed_align_power,
   1193 					  &ch_type);
   1194 
   1195       /* Should we decompress?  */
   1196       if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
   1197 	action = decompress;
   1198 
   1199       /* Should we compress?  Or convert to a different compression?  */
   1200       else if ((abfd->flags & BFD_COMPRESS) != 0
   1201 	       && newsect->size != 0
   1202 	       && compression_header_size >= 0
   1203 	       && uncompressed_size > 0)
   1204 	{
   1205 	  if (!compressed)
   1206 	    action = compress;
   1207 	  else
   1208 	    {
   1209 	      enum compression_type new_ch_type = ch_none;
   1210 	      if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
   1211 		new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
   1212 			       ? ch_compress_zstd : ch_compress_zlib);
   1213 	      if (new_ch_type != ch_type)
   1214 		action = compress;
   1215 	    }
   1216 	}
   1217 
   1218       if (action == compress)
   1219 	{
   1220 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1221 	    {
   1222 	      _bfd_error_handler
   1223 		/* xgettext:c-format */
   1224 		(_("%pB: unable to compress section %s"), abfd, name);
   1225 	      return false;
   1226 	    }
   1227 	}
   1228       else if (action == decompress)
   1229 	{
   1230 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1231 	    {
   1232 	      _bfd_error_handler
   1233 		/* xgettext:c-format */
   1234 		(_("%pB: unable to decompress section %s"), abfd, name);
   1235 	      return false;
   1236 	    }
   1237 #ifndef HAVE_ZSTD
   1238 	  if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
   1239 	    {
   1240 	      _bfd_error_handler
   1241 		  /* xgettext:c-format */
   1242 		  (_ ("%pB: section %s is compressed with zstd, but BFD "
   1243 		      "is not built with zstd support"),
   1244 		   abfd, name);
   1245 	      newsect->compress_status = COMPRESS_SECTION_NONE;
   1246 	      return false;
   1247 	    }
   1248 #endif
   1249 	  if (abfd->is_linker_input
   1250 	      && name[1] == 'z')
   1251 	    {
   1252 	      /* Rename section from .zdebug_* to .debug_* so that ld
   1253 		 scripts will see this section as a debug section.  */
   1254 	      char *new_name = bfd_zdebug_name_to_debug (abfd, name);
   1255 	      if (new_name == NULL)
   1256 		return false;
   1257 	      bfd_rename_section (newsect, new_name);
   1258 	    }
   1259 	}
   1260     }
   1261 
   1262   /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
   1263      section.  */
   1264   if (startswith (name, ".gnu.lto_.lto."))
   1265     {
   1266       struct lto_section lsection;
   1267       if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
   1268 				    sizeof (struct lto_section)))
   1269 	abfd->lto_slim_object = lsection.slim_object;
   1270     }
   1271 
   1272   return true;
   1273 }
   1274 
   1275 const char *const bfd_elf_section_type_names[] =
   1276 {
   1277   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1278   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1279   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1280 };
   1281 
   1282 /* ELF relocs are against symbols.  If we are producing relocatable
   1283    output, and the reloc is against an external symbol, and nothing
   1284    has given us any additional addend, the resulting reloc will also
   1285    be against the same symbol.  In such a case, we don't want to
   1286    change anything about the way the reloc is handled, since it will
   1287    all be done at final link time.  Rather than put special case code
   1288    into bfd_perform_relocation, all the reloc types use this howto
   1289    function, or should call this function for relocatable output.  */
   1290 
   1291 bfd_reloc_status_type
   1292 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1293 		       arelent *reloc_entry,
   1294 		       asymbol *symbol,
   1295 		       void *data ATTRIBUTE_UNUSED,
   1296 		       asection *input_section,
   1297 		       bfd *output_bfd,
   1298 		       char **error_message ATTRIBUTE_UNUSED)
   1299 {
   1300   if (output_bfd != NULL
   1301       && (symbol->flags & BSF_SECTION_SYM) == 0
   1302       && (! reloc_entry->howto->partial_inplace
   1303 	  || reloc_entry->addend == 0))
   1304     {
   1305       reloc_entry->address += input_section->output_offset;
   1306       return bfd_reloc_ok;
   1307     }
   1308 
   1309   /* In some cases the relocation should be treated as output section
   1310      relative, as when linking ELF DWARF into PE COFF.  Many ELF
   1311      targets lack section relative relocations and instead use
   1312      ordinary absolute relocations for references between DWARF
   1313      sections.  That is arguably a bug in those targets but it happens
   1314      to work for the usual case of linking to non-loaded ELF debug
   1315      sections with VMAs forced to zero.  PE COFF on the other hand
   1316      doesn't allow a section VMA of zero.  */
   1317   if (output_bfd == NULL
   1318       && !reloc_entry->howto->pc_relative
   1319       && (symbol->section->flags & SEC_DEBUGGING) != 0
   1320       && (input_section->flags & SEC_DEBUGGING) != 0)
   1321     reloc_entry->addend -= symbol->section->output_section->vma;
   1322 
   1323   return bfd_reloc_continue;
   1324 }
   1325 
   1326 /* Returns TRUE if section A matches section B.
   1328    Names, addresses and links may be different, but everything else
   1329    should be the same.  */
   1330 
   1331 static bool
   1332 section_match (const Elf_Internal_Shdr * a,
   1333 	       const Elf_Internal_Shdr * b)
   1334 {
   1335   if (a->sh_type != b->sh_type
   1336       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1337       || a->sh_addralign != b->sh_addralign
   1338       || a->sh_entsize != b->sh_entsize)
   1339     return false;
   1340   if (a->sh_type == SHT_SYMTAB
   1341       || a->sh_type == SHT_STRTAB)
   1342     return true;
   1343   return a->sh_size == b->sh_size;
   1344 }
   1345 
   1346 /* Find a section in OBFD that has the same characteristics
   1347    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1348    none can be found.  Check's section HINT first, as this is likely
   1349    to be the correct section.  */
   1350 
   1351 static unsigned int
   1352 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1353 	   const unsigned int hint)
   1354 {
   1355   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1356   unsigned int i;
   1357 
   1358   BFD_ASSERT (iheader != NULL);
   1359 
   1360   /* See PR 20922 for a reproducer of the NULL test.  */
   1361   if (hint < elf_numsections (obfd)
   1362       && oheaders[hint] != NULL
   1363       && section_match (oheaders[hint], iheader))
   1364     return hint;
   1365 
   1366   for (i = 1; i < elf_numsections (obfd); i++)
   1367     {
   1368       Elf_Internal_Shdr * oheader = oheaders[i];
   1369 
   1370       if (oheader == NULL)
   1371 	continue;
   1372       if (section_match (oheader, iheader))
   1373 	/* FIXME: Do we care if there is a potential for
   1374 	   multiple matches ?  */
   1375 	return i;
   1376     }
   1377 
   1378   return SHN_UNDEF;
   1379 }
   1380 
   1381 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1382    Processor specific section, based upon a matching input section.
   1383    Returns TRUE upon success, FALSE otherwise.  */
   1384 
   1385 static bool
   1386 copy_special_section_fields (const bfd *ibfd,
   1387 			     bfd *obfd,
   1388 			     const Elf_Internal_Shdr *iheader,
   1389 			     Elf_Internal_Shdr *oheader,
   1390 			     const unsigned int secnum)
   1391 {
   1392   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1393   const Elf_Internal_Shdr **iheaders
   1394     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1395   bool changed = false;
   1396   unsigned int sh_link;
   1397 
   1398   if (oheader->sh_type == SHT_NOBITS)
   1399     {
   1400       /* This is a feature for objcopy --only-keep-debug:
   1401 	 When a section's type is changed to NOBITS, we preserve
   1402 	 the sh_link and sh_info fields so that they can be
   1403 	 matched up with the original.
   1404 
   1405 	 Note: Strictly speaking these assignments are wrong.
   1406 	 The sh_link and sh_info fields should point to the
   1407 	 relevent sections in the output BFD, which may not be in
   1408 	 the same location as they were in the input BFD.  But
   1409 	 the whole point of this action is to preserve the
   1410 	 original values of the sh_link and sh_info fields, so
   1411 	 that they can be matched up with the section headers in
   1412 	 the original file.  So strictly speaking we may be
   1413 	 creating an invalid ELF file, but it is only for a file
   1414 	 that just contains debug info and only for sections
   1415 	 without any contents.  */
   1416       if (oheader->sh_link == 0)
   1417 	oheader->sh_link = iheader->sh_link;
   1418       if (oheader->sh_info == 0)
   1419 	oheader->sh_info = iheader->sh_info;
   1420       return true;
   1421     }
   1422 
   1423   /* Allow the target a chance to decide how these fields should be set.  */
   1424   if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1425 						    iheader, oheader))
   1426     return true;
   1427 
   1428   /* We have an iheader which might match oheader, and which has non-zero
   1429      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1430      the section in the output bfd which corresponds to the linked section
   1431      in the input bfd.  */
   1432   if (iheader->sh_link != SHN_UNDEF)
   1433     {
   1434       /* See PR 20931 for a reproducer.  */
   1435       if (iheader->sh_link >= elf_numsections (ibfd))
   1436 	{
   1437 	  _bfd_error_handler
   1438 	    /* xgettext:c-format */
   1439 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1440 	     ibfd, iheader->sh_link, secnum);
   1441 	  return false;
   1442 	}
   1443 
   1444       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1445       if (sh_link != SHN_UNDEF)
   1446 	{
   1447 	  oheader->sh_link = sh_link;
   1448 	  changed = true;
   1449 	}
   1450       else
   1451 	/* FIXME: Should we install iheader->sh_link
   1452 	   if we could not find a match ?  */
   1453 	_bfd_error_handler
   1454 	  /* xgettext:c-format */
   1455 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1456     }
   1457 
   1458   if (iheader->sh_info)
   1459     {
   1460       /* The sh_info field can hold arbitrary information, but if the
   1461 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1462 	 section index.  */
   1463       if (iheader->sh_flags & SHF_INFO_LINK)
   1464 	{
   1465 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1466 			       iheader->sh_info);
   1467 	  if (sh_link != SHN_UNDEF)
   1468 	    oheader->sh_flags |= SHF_INFO_LINK;
   1469 	}
   1470       else
   1471 	/* No idea what it means - just copy it.  */
   1472 	sh_link = iheader->sh_info;
   1473 
   1474       if (sh_link != SHN_UNDEF)
   1475 	{
   1476 	  oheader->sh_info = sh_link;
   1477 	  changed = true;
   1478 	}
   1479       else
   1480 	_bfd_error_handler
   1481 	  /* xgettext:c-format */
   1482 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1483     }
   1484 
   1485   return changed;
   1486 }
   1487 
   1488 /* Copy the program header and other data from one object module to
   1489    another.  */
   1490 
   1491 bool
   1492 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1493 {
   1494   const Elf_Internal_Shdr **iheaders
   1495     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1496   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1497   const struct elf_backend_data *bed;
   1498   unsigned int i;
   1499 
   1500   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1501     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1502     return true;
   1503 
   1504   if (!elf_flags_init (obfd))
   1505     {
   1506       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1507       elf_flags_init (obfd) = true;
   1508     }
   1509 
   1510   elf_gp (obfd) = elf_gp (ibfd);
   1511 
   1512   /* Also copy the EI_OSABI field.  */
   1513   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1514     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1515 
   1516   /* If set, copy the EI_ABIVERSION field.  */
   1517   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1518     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1519       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1520 
   1521   /* Copy object attributes.  */
   1522   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1523 
   1524   if (iheaders == NULL || oheaders == NULL)
   1525     return true;
   1526 
   1527   bed = get_elf_backend_data (obfd);
   1528 
   1529   /* Possibly copy other fields in the section header.  */
   1530   for (i = 1; i < elf_numsections (obfd); i++)
   1531     {
   1532       unsigned int j;
   1533       Elf_Internal_Shdr * oheader = oheaders[i];
   1534 
   1535       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1536 	 because of a special case need for generating separate debug info
   1537 	 files.  See below for more details.  */
   1538       if (oheader == NULL
   1539 	  || (oheader->sh_type != SHT_NOBITS
   1540 	      && oheader->sh_type < SHT_LOOS))
   1541 	continue;
   1542 
   1543       /* Ignore empty sections, and sections whose
   1544 	 fields have already been initialised.  */
   1545       if (oheader->sh_size == 0
   1546 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1547 	continue;
   1548 
   1549       /* Scan for the matching section in the input bfd.
   1550 	 First we try for a direct mapping between the input and
   1551 	 output sections.  */
   1552       for (j = 1; j < elf_numsections (ibfd); j++)
   1553 	{
   1554 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1555 
   1556 	  if (iheader == NULL)
   1557 	    continue;
   1558 
   1559 	  if (oheader->bfd_section != NULL
   1560 	      && iheader->bfd_section != NULL
   1561 	      && iheader->bfd_section->output_section != NULL
   1562 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1563 	    {
   1564 	      /* We have found a connection from the input section to
   1565 		 the output section.  Attempt to copy the header fields.
   1566 		 If this fails then do not try any further sections -
   1567 		 there should only be a one-to-one mapping between
   1568 		 input and output.  */
   1569 	      if (!copy_special_section_fields (ibfd, obfd,
   1570 						iheader, oheader, i))
   1571 		j = elf_numsections (ibfd);
   1572 	      break;
   1573 	    }
   1574 	}
   1575 
   1576       if (j < elf_numsections (ibfd))
   1577 	continue;
   1578 
   1579       /* That failed.  So try to deduce the corresponding input section.
   1580 	 Unfortunately we cannot compare names as the output string table
   1581 	 is empty, so instead we check size, address and type.  */
   1582       for (j = 1; j < elf_numsections (ibfd); j++)
   1583 	{
   1584 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1585 
   1586 	  if (iheader == NULL)
   1587 	    continue;
   1588 
   1589 	  /* Try matching fields in the input section's header.
   1590 	     Since --only-keep-debug turns all non-debug sections into
   1591 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1592 	     input type.  */
   1593 	  if ((oheader->sh_type == SHT_NOBITS
   1594 	       || iheader->sh_type == oheader->sh_type)
   1595 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1596 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1597 	      && iheader->sh_addralign == oheader->sh_addralign
   1598 	      && iheader->sh_entsize == oheader->sh_entsize
   1599 	      && iheader->sh_size == oheader->sh_size
   1600 	      && iheader->sh_addr == oheader->sh_addr
   1601 	      && (iheader->sh_info != oheader->sh_info
   1602 		  || iheader->sh_link != oheader->sh_link))
   1603 	    {
   1604 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1605 		break;
   1606 	    }
   1607 	}
   1608 
   1609       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1610 	{
   1611 	  /* Final attempt.  Call the backend copy function
   1612 	     with a NULL input section.  */
   1613 	  (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1614 							       NULL, oheader);
   1615 	}
   1616     }
   1617 
   1618   return true;
   1619 }
   1620 
   1621 static const char *
   1622 get_segment_type (unsigned int p_type)
   1623 {
   1624   const char *pt;
   1625   switch (p_type)
   1626     {
   1627     case PT_NULL: pt = "NULL"; break;
   1628     case PT_LOAD: pt = "LOAD"; break;
   1629     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1630     case PT_INTERP: pt = "INTERP"; break;
   1631     case PT_NOTE: pt = "NOTE"; break;
   1632     case PT_SHLIB: pt = "SHLIB"; break;
   1633     case PT_PHDR: pt = "PHDR"; break;
   1634     case PT_TLS: pt = "TLS"; break;
   1635     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1636     case PT_GNU_STACK: pt = "STACK"; break;
   1637     case PT_GNU_RELRO: pt = "RELRO"; break;
   1638     case PT_GNU_SFRAME: pt = "SFRAME"; break;
   1639     default: pt = NULL; break;
   1640     }
   1641   return pt;
   1642 }
   1643 
   1644 /* Print out the program headers.  */
   1645 
   1646 bool
   1647 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1648 {
   1649   FILE *f = (FILE *) farg;
   1650   Elf_Internal_Phdr *p;
   1651   asection *s;
   1652   bfd_byte *dynbuf = NULL;
   1653 
   1654   p = elf_tdata (abfd)->phdr;
   1655   if (p != NULL)
   1656     {
   1657       unsigned int i, c;
   1658 
   1659       fprintf (f, _("\nProgram Header:\n"));
   1660       c = elf_elfheader (abfd)->e_phnum;
   1661       for (i = 0; i < c; i++, p++)
   1662 	{
   1663 	  const char *pt = get_segment_type (p->p_type);
   1664 	  char buf[20];
   1665 
   1666 	  if (pt == NULL)
   1667 	    {
   1668 	      sprintf (buf, "0x%lx", p->p_type);
   1669 	      pt = buf;
   1670 	    }
   1671 	  fprintf (f, "%8s off    0x", pt);
   1672 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1673 	  fprintf (f, " vaddr 0x");
   1674 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1675 	  fprintf (f, " paddr 0x");
   1676 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1677 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1678 	  fprintf (f, "         filesz 0x");
   1679 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1680 	  fprintf (f, " memsz 0x");
   1681 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1682 	  fprintf (f, " flags %c%c%c",
   1683 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1684 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1685 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1686 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1687 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1688 	  fprintf (f, "\n");
   1689 	}
   1690     }
   1691 
   1692   s = bfd_get_section_by_name (abfd, ".dynamic");
   1693   if (s != NULL)
   1694     {
   1695       unsigned int elfsec;
   1696       unsigned long shlink;
   1697       bfd_byte *extdyn, *extdynend;
   1698       size_t extdynsize;
   1699       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1700 
   1701       fprintf (f, _("\nDynamic Section:\n"));
   1702 
   1703       if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   1704 	goto error_return;
   1705 
   1706       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1707       if (elfsec == SHN_BAD)
   1708 	goto error_return;
   1709       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1710 
   1711       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1712       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1713 
   1714       for (extdyn = dynbuf, extdynend = dynbuf + s->size;
   1715 	   (size_t) (extdynend - extdyn) >= extdynsize;
   1716 	   extdyn += extdynsize)
   1717 	{
   1718 	  Elf_Internal_Dyn dyn;
   1719 	  const char *name = "";
   1720 	  char ab[20];
   1721 	  bool stringp;
   1722 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1723 
   1724 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1725 
   1726 	  if (dyn.d_tag == DT_NULL)
   1727 	    break;
   1728 
   1729 	  stringp = false;
   1730 	  switch (dyn.d_tag)
   1731 	    {
   1732 	    default:
   1733 	      if (bed->elf_backend_get_target_dtag)
   1734 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1735 
   1736 	      if (!strcmp (name, ""))
   1737 		{
   1738 		  sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
   1739 		  name = ab;
   1740 		}
   1741 	      break;
   1742 
   1743 	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
   1744 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1745 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1746 	    case DT_HASH: name = "HASH"; break;
   1747 	    case DT_STRTAB: name = "STRTAB"; break;
   1748 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1749 	    case DT_RELA: name = "RELA"; break;
   1750 	    case DT_RELASZ: name = "RELASZ"; break;
   1751 	    case DT_RELAENT: name = "RELAENT"; break;
   1752 	    case DT_STRSZ: name = "STRSZ"; break;
   1753 	    case DT_SYMENT: name = "SYMENT"; break;
   1754 	    case DT_INIT: name = "INIT"; break;
   1755 	    case DT_FINI: name = "FINI"; break;
   1756 	    case DT_SONAME: name = "SONAME"; stringp = true; break;
   1757 	    case DT_RPATH: name = "RPATH"; stringp = true; break;
   1758 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1759 	    case DT_REL: name = "REL"; break;
   1760 	    case DT_RELSZ: name = "RELSZ"; break;
   1761 	    case DT_RELENT: name = "RELENT"; break;
   1762 	    case DT_RELR: name = "RELR"; break;
   1763 	    case DT_RELRSZ: name = "RELRSZ"; break;
   1764 	    case DT_RELRENT: name = "RELRENT"; break;
   1765 	    case DT_PLTREL: name = "PLTREL"; break;
   1766 	    case DT_DEBUG: name = "DEBUG"; break;
   1767 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1768 	    case DT_JMPREL: name = "JMPREL"; break;
   1769 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1770 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1771 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1772 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1773 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1774 	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
   1775 	    case DT_FLAGS: name = "FLAGS"; break;
   1776 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1777 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1778 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1779 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1780 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1781 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1782 	    case DT_FEATURE: name = "FEATURE"; break;
   1783 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1784 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1785 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1786 	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
   1787 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
   1788 	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
   1789 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1790 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1791 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1792 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1793 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1794 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1795 	    case DT_VERSYM: name = "VERSYM"; break;
   1796 	    case DT_VERDEF: name = "VERDEF"; break;
   1797 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1798 	    case DT_VERNEED: name = "VERNEED"; break;
   1799 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1800 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
   1801 	    case DT_USED: name = "USED"; break;
   1802 	    case DT_FILTER: name = "FILTER"; stringp = true; break;
   1803 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1804 	    }
   1805 
   1806 	  fprintf (f, "  %-20s ", name);
   1807 	  if (! stringp)
   1808 	    {
   1809 	      fprintf (f, "0x");
   1810 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1811 	    }
   1812 	  else
   1813 	    {
   1814 	      const char *string;
   1815 	      unsigned int tagv = dyn.d_un.d_val;
   1816 
   1817 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1818 	      if (string == NULL)
   1819 		goto error_return;
   1820 	      fprintf (f, "%s", string);
   1821 	    }
   1822 	  fprintf (f, "\n");
   1823 	}
   1824 
   1825       free (dynbuf);
   1826       dynbuf = NULL;
   1827     }
   1828 
   1829   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1830       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1831     {
   1832       if (! _bfd_elf_slurp_version_tables (abfd, false))
   1833 	return false;
   1834     }
   1835 
   1836   if (elf_dynverdef (abfd) != 0)
   1837     {
   1838       Elf_Internal_Verdef *t;
   1839 
   1840       fprintf (f, _("\nVersion definitions:\n"));
   1841       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1842 	{
   1843 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1844 		   t->vd_flags, t->vd_hash,
   1845 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1846 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1847 	    {
   1848 	      Elf_Internal_Verdaux *a;
   1849 
   1850 	      fprintf (f, "\t");
   1851 	      for (a = t->vd_auxptr->vda_nextptr;
   1852 		   a != NULL;
   1853 		   a = a->vda_nextptr)
   1854 		fprintf (f, "%s ",
   1855 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1856 	      fprintf (f, "\n");
   1857 	    }
   1858 	}
   1859     }
   1860 
   1861   if (elf_dynverref (abfd) != 0)
   1862     {
   1863       Elf_Internal_Verneed *t;
   1864 
   1865       fprintf (f, _("\nVersion References:\n"));
   1866       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1867 	{
   1868 	  Elf_Internal_Vernaux *a;
   1869 
   1870 	  fprintf (f, _("  required from %s:\n"),
   1871 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1872 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1873 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1874 		     a->vna_flags, a->vna_other,
   1875 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1876 	}
   1877     }
   1878 
   1879   return true;
   1880 
   1881  error_return:
   1882   free (dynbuf);
   1883   return false;
   1884 }
   1885 
   1886 /* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
   1887    and return symbol version for symbol version itself.   */
   1888 
   1889 const char *
   1890 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   1891 				    bool base_p,
   1892 				    bool *hidden)
   1893 {
   1894   const char *version_string = NULL;
   1895   if (elf_dynversym (abfd) != 0
   1896       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   1897     {
   1898       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   1899 
   1900       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   1901       vernum &= VERSYM_VERSION;
   1902 
   1903       if (vernum == 0)
   1904 	version_string = "";
   1905       else if (vernum == 1
   1906 	       && (vernum > elf_tdata (abfd)->cverdefs
   1907 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   1908 		       == VER_FLG_BASE)))
   1909 	version_string = base_p ? "Base" : "";
   1910       else if (vernum <= elf_tdata (abfd)->cverdefs)
   1911 	{
   1912 	  const char *nodename
   1913 	    = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   1914 	  version_string = "";
   1915 	  if (base_p
   1916 	      || nodename == NULL
   1917 	      || symbol->name == NULL
   1918 	      || strcmp (symbol->name, nodename) != 0)
   1919 	    version_string = nodename;
   1920 	}
   1921       else
   1922 	{
   1923 	  Elf_Internal_Verneed *t;
   1924 
   1925 	  version_string = _("<corrupt>");
   1926 	  for (t = elf_tdata (abfd)->verref;
   1927 	       t != NULL;
   1928 	       t = t->vn_nextref)
   1929 	    {
   1930 	      Elf_Internal_Vernaux *a;
   1931 
   1932 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1933 		{
   1934 		  if (a->vna_other == vernum)
   1935 		    {
   1936 		      *hidden = true;
   1937 		      version_string = a->vna_nodename;
   1938 		      break;
   1939 		    }
   1940 		}
   1941 	    }
   1942 	}
   1943     }
   1944   return version_string;
   1945 }
   1946 
   1947 /* Display ELF-specific fields of a symbol.  */
   1948 
   1949 void
   1950 bfd_elf_print_symbol (bfd *abfd,
   1951 		      void *filep,
   1952 		      asymbol *symbol,
   1953 		      bfd_print_symbol_type how)
   1954 {
   1955   FILE *file = (FILE *) filep;
   1956   switch (how)
   1957     {
   1958     case bfd_print_symbol_name:
   1959       fprintf (file, "%s", symbol->name);
   1960       break;
   1961     case bfd_print_symbol_more:
   1962       fprintf (file, "elf ");
   1963       bfd_fprintf_vma (abfd, file, symbol->value);
   1964       fprintf (file, " %x", symbol->flags);
   1965       break;
   1966     case bfd_print_symbol_all:
   1967       {
   1968 	const char *section_name;
   1969 	const char *name = NULL;
   1970 	const struct elf_backend_data *bed;
   1971 	unsigned char st_other;
   1972 	bfd_vma val;
   1973 	const char *version_string;
   1974 	bool hidden;
   1975 
   1976 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   1977 
   1978 	bed = get_elf_backend_data (abfd);
   1979 	if (bed->elf_backend_print_symbol_all)
   1980 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   1981 
   1982 	if (name == NULL)
   1983 	  {
   1984 	    name = symbol->name;
   1985 	    bfd_print_symbol_vandf (abfd, file, symbol);
   1986 	  }
   1987 
   1988 	fprintf (file, " %s\t", section_name);
   1989 	/* Print the "other" value for a symbol.  For common symbols,
   1990 	   we've already printed the size; now print the alignment.
   1991 	   For other symbols, we have no specified alignment, and
   1992 	   we've printed the address; now print the size.  */
   1993 	if (symbol->section && bfd_is_com_section (symbol->section))
   1994 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   1995 	else
   1996 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   1997 	bfd_fprintf_vma (abfd, file, val);
   1998 
   1999 	/* If we have version information, print it.  */
   2000 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2001 							     symbol,
   2002 							     true,
   2003 							     &hidden);
   2004 	if (version_string)
   2005 	  {
   2006 	    if (!hidden)
   2007 	      fprintf (file, "  %-11s", version_string);
   2008 	    else
   2009 	      {
   2010 		int i;
   2011 
   2012 		fprintf (file, " (%s)", version_string);
   2013 		for (i = 10 - strlen (version_string); i > 0; --i)
   2014 		  putc (' ', file);
   2015 	      }
   2016 	  }
   2017 
   2018 	/* If the st_other field is not zero, print it.  */
   2019 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2020 
   2021 	switch (st_other)
   2022 	  {
   2023 	  case 0: break;
   2024 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2025 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2026 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2027 	  default:
   2028 	    /* Some other non-defined flags are also present, so print
   2029 	       everything hex.  */
   2030 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2031 	  }
   2032 
   2033 	fprintf (file, " %s", name);
   2034       }
   2035       break;
   2036     }
   2037 }
   2038 
   2039 /* ELF .o/exec file reading */
   2041 
   2042 /* Create a new bfd section from an ELF section header.  */
   2043 
   2044 bool
   2045 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2046 {
   2047   Elf_Internal_Shdr *hdr;
   2048   Elf_Internal_Ehdr *ehdr;
   2049   const struct elf_backend_data *bed;
   2050   const char *name;
   2051   bool ret = true;
   2052 
   2053   if (shindex >= elf_numsections (abfd))
   2054     return false;
   2055 
   2056   /* PR17512: A corrupt ELF binary might contain a loop of sections via
   2057      sh_link or sh_info.  Detect this here, by refusing to load a
   2058      section that we are already in the process of loading.  */
   2059   if (elf_tdata (abfd)->being_created[shindex])
   2060     {
   2061       _bfd_error_handler
   2062 	(_("%pB: warning: loop in section dependencies detected"), abfd);
   2063       return false;
   2064     }
   2065   elf_tdata (abfd)->being_created[shindex] = true;
   2066 
   2067   hdr = elf_elfsections (abfd)[shindex];
   2068   ehdr = elf_elfheader (abfd);
   2069   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2070 					  hdr->sh_name);
   2071   if (name == NULL)
   2072     goto fail;
   2073 
   2074   bed = get_elf_backend_data (abfd);
   2075   switch (hdr->sh_type)
   2076     {
   2077     case SHT_NULL:
   2078       /* Inactive section. Throw it away.  */
   2079       goto success;
   2080 
   2081     case SHT_PROGBITS:		/* Normal section with contents.  */
   2082     case SHT_NOBITS:		/* .bss section.  */
   2083     case SHT_HASH:		/* .hash section.  */
   2084     case SHT_NOTE:		/* .note section.  */
   2085     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2086     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2087     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2088     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2089     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2090       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2091       goto success;
   2092 
   2093     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2094       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2095 	goto fail;
   2096 
   2097       if (hdr->sh_link > elf_numsections (abfd))
   2098 	{
   2099 	  /* PR 10478: Accept Solaris binaries with a sh_link field
   2100 	     set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
   2101 	  switch (bfd_get_arch (abfd))
   2102 	    {
   2103 	    case bfd_arch_i386:
   2104 	    case bfd_arch_sparc:
   2105 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
   2106 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
   2107 		break;
   2108 	      /* Otherwise fall through.  */
   2109 	    default:
   2110 	      goto fail;
   2111 	    }
   2112 	}
   2113       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2114 	goto fail;
   2115       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2116 	{
   2117 	  Elf_Internal_Shdr *dynsymhdr;
   2118 
   2119 	  /* The shared libraries distributed with hpux11 have a bogus
   2120 	     sh_link field for the ".dynamic" section.  Find the
   2121 	     string table for the ".dynsym" section instead.  */
   2122 	  if (elf_dynsymtab (abfd) != 0)
   2123 	    {
   2124 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2125 	      hdr->sh_link = dynsymhdr->sh_link;
   2126 	    }
   2127 	  else
   2128 	    {
   2129 	      unsigned int i, num_sec;
   2130 
   2131 	      num_sec = elf_numsections (abfd);
   2132 	      for (i = 1; i < num_sec; i++)
   2133 		{
   2134 		  dynsymhdr = elf_elfsections (abfd)[i];
   2135 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2136 		    {
   2137 		      hdr->sh_link = dynsymhdr->sh_link;
   2138 		      break;
   2139 		    }
   2140 		}
   2141 	    }
   2142 	}
   2143       goto success;
   2144 
   2145     case SHT_SYMTAB:		/* A symbol table.  */
   2146       if (elf_onesymtab (abfd) == shindex)
   2147 	goto success;
   2148 
   2149       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2150 	goto fail;
   2151 
   2152       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2153 	{
   2154 	  if (hdr->sh_size != 0)
   2155 	    goto fail;
   2156 	  /* Some assemblers erroneously set sh_info to one with a
   2157 	     zero sh_size.  ld sees this as a global symbol count
   2158 	     of (unsigned) -1.  Fix it here.  */
   2159 	  hdr->sh_info = 0;
   2160 	  goto success;
   2161 	}
   2162 
   2163       /* PR 18854: A binary might contain more than one symbol table.
   2164 	 Unusual, but possible.  Warn, but continue.  */
   2165       if (elf_onesymtab (abfd) != 0)
   2166 	{
   2167 	  _bfd_error_handler
   2168 	    /* xgettext:c-format */
   2169 	    (_("%pB: warning: multiple symbol tables detected"
   2170 	       " - ignoring the table in section %u"),
   2171 	     abfd, shindex);
   2172 	  goto success;
   2173 	}
   2174       elf_onesymtab (abfd) = shindex;
   2175       elf_symtab_hdr (abfd) = *hdr;
   2176       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2177       abfd->flags |= HAS_SYMS;
   2178 
   2179       /* Sometimes a shared object will map in the symbol table.  If
   2180 	 SHF_ALLOC is set, and this is a shared object, then we also
   2181 	 treat this section as a BFD section.  We can not base the
   2182 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2183 	 set in a relocatable object file, which would confuse the
   2184 	 linker.  */
   2185       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2186 	  && (abfd->flags & DYNAMIC) != 0
   2187 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2188 						shindex))
   2189 	goto fail;
   2190 
   2191       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2192 	 can't read symbols without that section loaded as well.  It
   2193 	 is most likely specified by the next section header.  */
   2194       {
   2195 	elf_section_list * entry;
   2196 	unsigned int i, num_sec;
   2197 
   2198 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2199 	  if (entry->hdr.sh_link == shindex)
   2200 	    goto success;
   2201 
   2202 	num_sec = elf_numsections (abfd);
   2203 	for (i = shindex + 1; i < num_sec; i++)
   2204 	  {
   2205 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2206 
   2207 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2208 		&& hdr2->sh_link == shindex)
   2209 	      break;
   2210 	  }
   2211 
   2212 	if (i == num_sec)
   2213 	  for (i = 1; i < shindex; i++)
   2214 	    {
   2215 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2216 
   2217 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2218 		  && hdr2->sh_link == shindex)
   2219 		break;
   2220 	    }
   2221 
   2222 	if (i != shindex)
   2223 	  ret = bfd_section_from_shdr (abfd, i);
   2224 	/* else FIXME: we have failed to find the symbol table.
   2225 	   Should we issue an error?  */
   2226 	goto success;
   2227       }
   2228 
   2229     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2230       if (elf_dynsymtab (abfd) == shindex)
   2231 	goto success;
   2232 
   2233       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2234 	goto fail;
   2235 
   2236       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2237 	{
   2238 	  if (hdr->sh_size != 0)
   2239 	    goto fail;
   2240 
   2241 	  /* Some linkers erroneously set sh_info to one with a
   2242 	     zero sh_size.  ld sees this as a global symbol count
   2243 	     of (unsigned) -1.  Fix it here.  */
   2244 	  hdr->sh_info = 0;
   2245 	  goto success;
   2246 	}
   2247 
   2248       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2249 	 Unusual, but possible.  Warn, but continue.  */
   2250       if (elf_dynsymtab (abfd) != 0)
   2251 	{
   2252 	  _bfd_error_handler
   2253 	    /* xgettext:c-format */
   2254 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2255 	       " - ignoring the table in section %u"),
   2256 	     abfd, shindex);
   2257 	  goto success;
   2258 	}
   2259       elf_dynsymtab (abfd) = shindex;
   2260       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2261       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2262       abfd->flags |= HAS_SYMS;
   2263 
   2264       /* Besides being a symbol table, we also treat this as a regular
   2265 	 section, so that objcopy can handle it.  */
   2266       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2267       goto success;
   2268 
   2269     case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
   2270       {
   2271 	elf_section_list * entry;
   2272 
   2273 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2274 	  if (entry->ndx == shindex)
   2275 	    goto success;
   2276 
   2277 	entry = bfd_alloc (abfd, sizeof (*entry));
   2278 	if (entry == NULL)
   2279 	  goto fail;
   2280 	entry->ndx = shindex;
   2281 	entry->hdr = * hdr;
   2282 	entry->next = elf_symtab_shndx_list (abfd);
   2283 	elf_symtab_shndx_list (abfd) = entry;
   2284 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2285 	goto success;
   2286       }
   2287 
   2288     case SHT_STRTAB:		/* A string table.  */
   2289       if (hdr->bfd_section != NULL)
   2290 	goto success;
   2291 
   2292       if (ehdr->e_shstrndx == shindex)
   2293 	{
   2294 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2295 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2296 	  goto success;
   2297 	}
   2298 
   2299       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2300 	{
   2301 	symtab_strtab:
   2302 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2303 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2304 	  goto success;
   2305 	}
   2306 
   2307       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2308 	{
   2309 	dynsymtab_strtab:
   2310 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2311 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2312 	  elf_elfsections (abfd)[shindex] = hdr;
   2313 	  /* We also treat this as a regular section, so that objcopy
   2314 	     can handle it.  */
   2315 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2316 						 shindex);
   2317 	  goto success;
   2318 	}
   2319 
   2320       /* If the string table isn't one of the above, then treat it as a
   2321 	 regular section.  We need to scan all the headers to be sure,
   2322 	 just in case this strtab section appeared before the above.  */
   2323       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2324 	{
   2325 	  unsigned int i, num_sec;
   2326 
   2327 	  num_sec = elf_numsections (abfd);
   2328 	  for (i = 1; i < num_sec; i++)
   2329 	    {
   2330 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2331 	      if (hdr2->sh_link == shindex)
   2332 		{
   2333 		  /* Prevent endless recursion on broken objects.  */
   2334 		  if (i == shindex)
   2335 		    goto fail;
   2336 		  if (! bfd_section_from_shdr (abfd, i))
   2337 		    goto fail;
   2338 		  if (elf_onesymtab (abfd) == i)
   2339 		    goto symtab_strtab;
   2340 		  if (elf_dynsymtab (abfd) == i)
   2341 		    goto dynsymtab_strtab;
   2342 		}
   2343 	    }
   2344 	}
   2345       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2346       goto success;
   2347 
   2348     case SHT_REL:
   2349     case SHT_RELA:
   2350     case SHT_RELR:
   2351       /* *These* do a lot of work -- but build no sections!  */
   2352       {
   2353 	asection *target_sect;
   2354 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2355 	unsigned int num_sec = elf_numsections (abfd);
   2356 	struct bfd_elf_section_data *esdt;
   2357 	bfd_size_type size;
   2358 
   2359 	if (hdr->sh_type == SHT_REL)
   2360 	  size = bed->s->sizeof_rel;
   2361 	else if (hdr->sh_type == SHT_RELA)
   2362 	  size = bed->s->sizeof_rela;
   2363 	else
   2364 	  size = bed->s->arch_size / 8;
   2365 	if (hdr->sh_entsize != size)
   2366 	  goto fail;
   2367 
   2368 	/* Check for a bogus link to avoid crashing.  */
   2369 	if (hdr->sh_link >= num_sec)
   2370 	  {
   2371 	    _bfd_error_handler
   2372 	      /* xgettext:c-format */
   2373 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2374 	       abfd, hdr->sh_link, name, shindex);
   2375 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2376 	    goto success;
   2377 	  }
   2378 
   2379 	/* Get the symbol table.  */
   2380 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2381 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2382 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2383 	  goto fail;
   2384 
   2385 	/* If this is an alloc section in an executable or shared
   2386 	   library, or the reloc section does not use the main symbol
   2387 	   table we don't treat it as a reloc section.  BFD can't
   2388 	   adequately represent such a section, so at least for now,
   2389 	   we don't try.  We just present it as a normal section.  We
   2390 	   also can't use it as a reloc section if it points to the
   2391 	   null section, an invalid section, another reloc section, or
   2392 	   its sh_link points to the null section.  */
   2393 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2394 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2395 	    || hdr->sh_type == SHT_RELR
   2396 	    || hdr->sh_link == SHN_UNDEF
   2397 	    || hdr->sh_link != elf_onesymtab (abfd)
   2398 	    || hdr->sh_info == SHN_UNDEF
   2399 	    || hdr->sh_info >= num_sec
   2400 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2401 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2402 	  {
   2403 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2404 	    goto success;
   2405 	  }
   2406 
   2407 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2408 	  goto fail;
   2409 
   2410 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2411 	if (target_sect == NULL)
   2412 	  goto fail;
   2413 
   2414 	esdt = elf_section_data (target_sect);
   2415 	if (hdr->sh_type == SHT_RELA)
   2416 	  p_hdr = &esdt->rela.hdr;
   2417 	else
   2418 	  p_hdr = &esdt->rel.hdr;
   2419 
   2420 	/* PR 17512: file: 0b4f81b7.
   2421 	   Also see PR 24456, for a file which deliberately has two reloc
   2422 	   sections.  */
   2423 	if (*p_hdr != NULL)
   2424 	  {
   2425 	    if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
   2426 	      {
   2427 		_bfd_error_handler
   2428 		  /* xgettext:c-format */
   2429 		  (_("%pB: warning: secondary relocation section '%s' "
   2430 		     "for section %pA found - ignoring"),
   2431 		   abfd, name, target_sect);
   2432 	      }
   2433 	    else
   2434 	      esdt->has_secondary_relocs = true;
   2435 	    goto success;
   2436 	  }
   2437 
   2438 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2439 	if (hdr2 == NULL)
   2440 	  goto fail;
   2441 	*hdr2 = *hdr;
   2442 	*p_hdr = hdr2;
   2443 	elf_elfsections (abfd)[shindex] = hdr2;
   2444 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2445 				     * bed->s->int_rels_per_ext_rel);
   2446 	target_sect->flags |= SEC_RELOC;
   2447 	target_sect->relocation = NULL;
   2448 	target_sect->rel_filepos = hdr->sh_offset;
   2449 	/* In the section to which the relocations apply, mark whether
   2450 	   its relocations are of the REL or RELA variety.  */
   2451 	if (hdr->sh_size != 0)
   2452 	  {
   2453 	    if (hdr->sh_type == SHT_RELA)
   2454 	      target_sect->use_rela_p = 1;
   2455 	  }
   2456 	abfd->flags |= HAS_RELOC;
   2457 	goto success;
   2458       }
   2459 
   2460     case SHT_GNU_verdef:
   2461       if (hdr->sh_info != 0)
   2462 	elf_dynverdef (abfd) = shindex;
   2463       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2464       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2465       goto success;
   2466 
   2467     case SHT_GNU_versym:
   2468       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2469 	goto fail;
   2470 
   2471       elf_dynversym (abfd) = shindex;
   2472       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2473       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2474       goto success;
   2475 
   2476     case SHT_GNU_verneed:
   2477       if (hdr->sh_info != 0)
   2478 	elf_dynverref (abfd) = shindex;
   2479       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2480       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2481       goto success;
   2482 
   2483     case SHT_SHLIB:
   2484       goto success;
   2485 
   2486     case SHT_GROUP:
   2487       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   2488 	goto fail;
   2489 
   2490       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2491 	goto fail;
   2492 
   2493       goto success;
   2494 
   2495     default:
   2496       /* Possibly an attributes section.  */
   2497       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2498 	  || hdr->sh_type == bed->obj_attrs_section_type)
   2499 	{
   2500 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2501 	    goto fail;
   2502 	  _bfd_elf_parse_attributes (abfd, hdr);
   2503 	  goto success;
   2504 	}
   2505 
   2506       /* Check for any processor-specific section types.  */
   2507       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2508 	goto success;
   2509 
   2510       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2511 	{
   2512 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2513 	    /* FIXME: How to properly handle allocated section reserved
   2514 	       for applications?  */
   2515 	    _bfd_error_handler
   2516 	      /* xgettext:c-format */
   2517 	      (_("%pB: unknown type [%#x] section `%s'"),
   2518 	       abfd, hdr->sh_type, name);
   2519 	  else
   2520 	    {
   2521 	      /* Allow sections reserved for applications.  */
   2522 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2523 	      goto success;
   2524 	    }
   2525 	}
   2526       else if (hdr->sh_type >= SHT_LOPROC
   2527 	       && hdr->sh_type <= SHT_HIPROC)
   2528 	/* FIXME: We should handle this section.  */
   2529 	_bfd_error_handler
   2530 	  /* xgettext:c-format */
   2531 	  (_("%pB: unknown type [%#x] section `%s'"),
   2532 	   abfd, hdr->sh_type, name);
   2533       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2534 	{
   2535 	  /* Unrecognised OS-specific sections.  */
   2536 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2537 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2538 	       required to correctly process the section and the file should
   2539 	       be rejected with an error message.  */
   2540 	    _bfd_error_handler
   2541 	      /* xgettext:c-format */
   2542 	      (_("%pB: unknown type [%#x] section `%s'"),
   2543 	       abfd, hdr->sh_type, name);
   2544 	  else
   2545 	    {
   2546 	      /* Otherwise it should be processed.  */
   2547 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2548 	      goto success;
   2549 	    }
   2550 	}
   2551       else
   2552 	/* FIXME: We should handle this section.  */
   2553 	_bfd_error_handler
   2554 	  /* xgettext:c-format */
   2555 	  (_("%pB: unknown type [%#x] section `%s'"),
   2556 	   abfd, hdr->sh_type, name);
   2557 
   2558       goto fail;
   2559     }
   2560 
   2561  fail:
   2562   ret = false;
   2563  success:
   2564   elf_tdata (abfd)->being_created[shindex] = false;
   2565   return ret;
   2566 }
   2567 
   2568 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   2569 
   2570 Elf_Internal_Sym *
   2571 bfd_sym_from_r_symndx (struct sym_cache *cache,
   2572 		       bfd *abfd,
   2573 		       unsigned long r_symndx)
   2574 {
   2575   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   2576 
   2577   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   2578     {
   2579       Elf_Internal_Shdr *symtab_hdr;
   2580       unsigned char esym[sizeof (Elf64_External_Sym)];
   2581       Elf_External_Sym_Shndx eshndx;
   2582 
   2583       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2584       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   2585 				&cache->sym[ent], esym, &eshndx) == NULL)
   2586 	return NULL;
   2587 
   2588       if (cache->abfd != abfd)
   2589 	{
   2590 	  memset (cache->indx, -1, sizeof (cache->indx));
   2591 	  cache->abfd = abfd;
   2592 	}
   2593       cache->indx[ent] = r_symndx;
   2594     }
   2595 
   2596   return &cache->sym[ent];
   2597 }
   2598 
   2599 /* Given an ELF section number, retrieve the corresponding BFD
   2600    section.  */
   2601 
   2602 asection *
   2603 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   2604 {
   2605   if (sec_index >= elf_numsections (abfd))
   2606     return NULL;
   2607   return elf_elfsections (abfd)[sec_index]->bfd_section;
   2608 }
   2609 
   2610 static const struct bfd_elf_special_section special_sections_b[] =
   2611 {
   2612   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2613   { NULL,		    0,	0, 0,		 0 }
   2614 };
   2615 
   2616 static const struct bfd_elf_special_section special_sections_c[] =
   2617 {
   2618   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   2619   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   2620   { NULL,			0, 0, 0,	    0 }
   2621 };
   2622 
   2623 static const struct bfd_elf_special_section special_sections_d[] =
   2624 {
   2625   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2626   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2627   /* There are more DWARF sections than these, but they needn't be added here
   2628      unless you have to cope with broken compilers that don't emit section
   2629      attributes or you want to help the user writing assembler.  */
   2630   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   2631   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   2632   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   2633   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   2634   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   2635   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   2636   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   2637   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   2638   { NULL,		       0,	 0, 0,		  0 }
   2639 };
   2640 
   2641 static const struct bfd_elf_special_section special_sections_f[] =
   2642 {
   2643   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2644   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   2645   { NULL,			   0 , 0, 0,		  0 }
   2646 };
   2647 
   2648 static const struct bfd_elf_special_section special_sections_g[] =
   2649 {
   2650   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2651   { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2652   { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2653   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   2654   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2655   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   2656   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   2657   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   2658   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   2659   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   2660   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   2661   { NULL,			 0,	   0, 0,	       0 }
   2662 };
   2663 
   2664 static const struct bfd_elf_special_section special_sections_h[] =
   2665 {
   2666   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   2667   { NULL,		     0, 0, 0,		 0 }
   2668 };
   2669 
   2670 static const struct bfd_elf_special_section special_sections_i[] =
   2671 {
   2672   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2673   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2674   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   2675   { NULL,		       0,      0, 0,		  0 }
   2676 };
   2677 
   2678 static const struct bfd_elf_special_section special_sections_l[] =
   2679 {
   2680   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   2681   { NULL,		     0, 0, 0,		 0 }
   2682 };
   2683 
   2684 static const struct bfd_elf_special_section special_sections_n[] =
   2685 {
   2686   { STRING_COMMA_LEN (".noinit"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2687   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   2688   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   2689   { NULL,		     0,		  0, 0,		   0 }
   2690 };
   2691 
   2692 static const struct bfd_elf_special_section special_sections_p[] =
   2693 {
   2694   { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,	SHF_ALLOC + SHF_WRITE },
   2695   { STRING_COMMA_LEN (".persistent"),	 -2, SHT_PROGBITS,	SHF_ALLOC + SHF_WRITE },
   2696   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2697   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   2698   { NULL,		    0,		  0, 0,			0 }
   2699 };
   2700 
   2701 static const struct bfd_elf_special_section special_sections_r[] =
   2702 {
   2703   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   2704   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   2705   { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
   2706   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   2707   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   2708   { NULL,		    0,	   0, 0,	    0 }
   2709 };
   2710 
   2711 static const struct bfd_elf_special_section special_sections_s[] =
   2712 {
   2713   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   2714   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   2715   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   2716   /* See struct bfd_elf_special_section declaration for the semantics of
   2717      this special case where .prefix_length != strlen (.prefix).  */
   2718   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   2719   { NULL,			0,  0, 0,	   0 }
   2720 };
   2721 
   2722 static const struct bfd_elf_special_section special_sections_t[] =
   2723 {
   2724   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   2725   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2726   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2727   { NULL,		      0,  0, 0,		   0 }
   2728 };
   2729 
   2730 static const struct bfd_elf_special_section special_sections_z[] =
   2731 {
   2732   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   2733   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   2734   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   2735   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   2736   { NULL,		      0,  0, 0,		   0 }
   2737 };
   2738 
   2739 static const struct bfd_elf_special_section * const special_sections[] =
   2740 {
   2741   special_sections_b,		/* 'b' */
   2742   special_sections_c,		/* 'c' */
   2743   special_sections_d,		/* 'd' */
   2744   NULL,				/* 'e' */
   2745   special_sections_f,		/* 'f' */
   2746   special_sections_g,		/* 'g' */
   2747   special_sections_h,		/* 'h' */
   2748   special_sections_i,		/* 'i' */
   2749   NULL,				/* 'j' */
   2750   NULL,				/* 'k' */
   2751   special_sections_l,		/* 'l' */
   2752   NULL,				/* 'm' */
   2753   special_sections_n,		/* 'n' */
   2754   NULL,				/* 'o' */
   2755   special_sections_p,		/* 'p' */
   2756   NULL,				/* 'q' */
   2757   special_sections_r,		/* 'r' */
   2758   special_sections_s,		/* 's' */
   2759   special_sections_t,		/* 't' */
   2760   NULL,				/* 'u' */
   2761   NULL,				/* 'v' */
   2762   NULL,				/* 'w' */
   2763   NULL,				/* 'x' */
   2764   NULL,				/* 'y' */
   2765   special_sections_z		/* 'z' */
   2766 };
   2767 
   2768 const struct bfd_elf_special_section *
   2769 _bfd_elf_get_special_section (const char *name,
   2770 			      const struct bfd_elf_special_section *spec,
   2771 			      unsigned int rela)
   2772 {
   2773   int i;
   2774   int len;
   2775 
   2776   len = strlen (name);
   2777 
   2778   for (i = 0; spec[i].prefix != NULL; i++)
   2779     {
   2780       int suffix_len;
   2781       int prefix_len = spec[i].prefix_length;
   2782 
   2783       if (len < prefix_len)
   2784 	continue;
   2785       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   2786 	continue;
   2787 
   2788       suffix_len = spec[i].suffix_length;
   2789       if (suffix_len <= 0)
   2790 	{
   2791 	  if (name[prefix_len] != 0)
   2792 	    {
   2793 	      if (suffix_len == 0)
   2794 		continue;
   2795 	      if (name[prefix_len] != '.'
   2796 		  && (suffix_len == -2
   2797 		      || (rela && spec[i].type == SHT_REL)))
   2798 		continue;
   2799 	    }
   2800 	}
   2801       else
   2802 	{
   2803 	  if (len < prefix_len + suffix_len)
   2804 	    continue;
   2805 	  if (memcmp (name + len - suffix_len,
   2806 		      spec[i].prefix + prefix_len,
   2807 		      suffix_len) != 0)
   2808 	    continue;
   2809 	}
   2810       return &spec[i];
   2811     }
   2812 
   2813   return NULL;
   2814 }
   2815 
   2816 const struct bfd_elf_special_section *
   2817 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   2818 {
   2819   int i;
   2820   const struct bfd_elf_special_section *spec;
   2821   const struct elf_backend_data *bed;
   2822 
   2823   /* See if this is one of the special sections.  */
   2824   if (sec->name == NULL)
   2825     return NULL;
   2826 
   2827   bed = get_elf_backend_data (abfd);
   2828   spec = bed->special_sections;
   2829   if (spec)
   2830     {
   2831       spec = _bfd_elf_get_special_section (sec->name,
   2832 					   bed->special_sections,
   2833 					   sec->use_rela_p);
   2834       if (spec != NULL)
   2835 	return spec;
   2836     }
   2837 
   2838   if (sec->name[0] != '.')
   2839     return NULL;
   2840 
   2841   i = sec->name[1] - 'b';
   2842   if (i < 0 || i > 'z' - 'b')
   2843     return NULL;
   2844 
   2845   spec = special_sections[i];
   2846 
   2847   if (spec == NULL)
   2848     return NULL;
   2849 
   2850   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   2851 }
   2852 
   2853 bool
   2854 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   2855 {
   2856   struct bfd_elf_section_data *sdata;
   2857   const struct elf_backend_data *bed;
   2858   const struct bfd_elf_special_section *ssect;
   2859 
   2860   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   2861   if (sdata == NULL)
   2862     {
   2863       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   2864 							  sizeof (*sdata));
   2865       if (sdata == NULL)
   2866 	return false;
   2867       sec->used_by_bfd = sdata;
   2868     }
   2869 
   2870   /* Indicate whether or not this section should use RELA relocations.  */
   2871   bed = get_elf_backend_data (abfd);
   2872   sec->use_rela_p = bed->default_use_rela_p;
   2873 
   2874   /* Set up ELF section type and flags for newly created sections, if
   2875      there is an ABI mandated section.  */
   2876   ssect = (*bed->get_sec_type_attr) (abfd, sec);
   2877   if (ssect != NULL)
   2878     {
   2879       elf_section_type (sec) = ssect->type;
   2880       elf_section_flags (sec) = ssect->attr;
   2881     }
   2882 
   2883   return _bfd_generic_new_section_hook (abfd, sec);
   2884 }
   2885 
   2886 /* Create a new bfd section from an ELF program header.
   2887 
   2888    Since program segments have no names, we generate a synthetic name
   2889    of the form segment<NUM>, where NUM is generally the index in the
   2890    program header table.  For segments that are split (see below) we
   2891    generate the names segment<NUM>a and segment<NUM>b.
   2892 
   2893    Note that some program segments may have a file size that is different than
   2894    (less than) the memory size.  All this means is that at execution the
   2895    system must allocate the amount of memory specified by the memory size,
   2896    but only initialize it with the first "file size" bytes read from the
   2897    file.  This would occur for example, with program segments consisting
   2898    of combined data+bss.
   2899 
   2900    To handle the above situation, this routine generates TWO bfd sections
   2901    for the single program segment.  The first has the length specified by
   2902    the file size of the segment, and the second has the length specified
   2903    by the difference between the two sizes.  In effect, the segment is split
   2904    into its initialized and uninitialized parts.  */
   2905 
   2906 bool
   2907 _bfd_elf_make_section_from_phdr (bfd *abfd,
   2908 				 Elf_Internal_Phdr *hdr,
   2909 				 int hdr_index,
   2910 				 const char *type_name)
   2911 {
   2912   asection *newsect;
   2913   char *name;
   2914   char namebuf[64];
   2915   size_t len;
   2916   int split;
   2917   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   2918 
   2919   split = ((hdr->p_memsz > 0)
   2920 	    && (hdr->p_filesz > 0)
   2921 	    && (hdr->p_memsz > hdr->p_filesz));
   2922 
   2923   if (hdr->p_filesz > 0)
   2924     {
   2925       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   2926       len = strlen (namebuf) + 1;
   2927       name = (char *) bfd_alloc (abfd, len);
   2928       if (!name)
   2929 	return false;
   2930       memcpy (name, namebuf, len);
   2931       newsect = bfd_make_section (abfd, name);
   2932       if (newsect == NULL)
   2933 	return false;
   2934       newsect->vma = hdr->p_vaddr / opb;
   2935       newsect->lma = hdr->p_paddr / opb;
   2936       newsect->size = hdr->p_filesz;
   2937       newsect->filepos = hdr->p_offset;
   2938       newsect->flags |= SEC_HAS_CONTENTS;
   2939       newsect->alignment_power = bfd_log2 (hdr->p_align);
   2940       if (hdr->p_type == PT_LOAD)
   2941 	{
   2942 	  newsect->flags |= SEC_ALLOC;
   2943 	  newsect->flags |= SEC_LOAD;
   2944 	  if (hdr->p_flags & PF_X)
   2945 	    {
   2946 	      /* FIXME: all we known is that it has execute PERMISSION,
   2947 		 may be data.  */
   2948 	      newsect->flags |= SEC_CODE;
   2949 	    }
   2950 	}
   2951       if (!(hdr->p_flags & PF_W))
   2952 	{
   2953 	  newsect->flags |= SEC_READONLY;
   2954 	}
   2955     }
   2956 
   2957   if (hdr->p_memsz > hdr->p_filesz)
   2958     {
   2959       bfd_vma align;
   2960 
   2961       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   2962       len = strlen (namebuf) + 1;
   2963       name = (char *) bfd_alloc (abfd, len);
   2964       if (!name)
   2965 	return false;
   2966       memcpy (name, namebuf, len);
   2967       newsect = bfd_make_section (abfd, name);
   2968       if (newsect == NULL)
   2969 	return false;
   2970       newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
   2971       newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
   2972       newsect->size = hdr->p_memsz - hdr->p_filesz;
   2973       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   2974       align = newsect->vma & -newsect->vma;
   2975       if (align == 0 || align > hdr->p_align)
   2976 	align = hdr->p_align;
   2977       newsect->alignment_power = bfd_log2 (align);
   2978       if (hdr->p_type == PT_LOAD)
   2979 	{
   2980 	  newsect->flags |= SEC_ALLOC;
   2981 	  if (hdr->p_flags & PF_X)
   2982 	    newsect->flags |= SEC_CODE;
   2983 	}
   2984       if (!(hdr->p_flags & PF_W))
   2985 	newsect->flags |= SEC_READONLY;
   2986     }
   2987 
   2988   return true;
   2989 }
   2990 
   2991 static bool
   2992 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   2993 {
   2994   /* The return value is ignored.  Build-ids are considered optional.  */
   2995   if (templ->xvec->flavour == bfd_target_elf_flavour)
   2996     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   2997       (templ, offset);
   2998   return false;
   2999 }
   3000 
   3001 bool
   3002 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3003 {
   3004   const struct elf_backend_data *bed;
   3005 
   3006   switch (hdr->p_type)
   3007     {
   3008     case PT_NULL:
   3009       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3010 
   3011     case PT_LOAD:
   3012       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3013 	return false;
   3014       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3015 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3016       return true;
   3017 
   3018     case PT_DYNAMIC:
   3019       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3020 
   3021     case PT_INTERP:
   3022       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3023 
   3024     case PT_NOTE:
   3025       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3026 	return false;
   3027       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3028 			    hdr->p_align))
   3029 	return false;
   3030       return true;
   3031 
   3032     case PT_SHLIB:
   3033       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3034 
   3035     case PT_PHDR:
   3036       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3037 
   3038     case PT_GNU_EH_FRAME:
   3039       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3040 					      "eh_frame_hdr");
   3041 
   3042     case PT_GNU_STACK:
   3043       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3044 
   3045     case PT_GNU_RELRO:
   3046       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3047 
   3048     case PT_GNU_SFRAME:
   3049       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3050 					      "sframe");
   3051 
   3052     default:
   3053       /* Check for any processor-specific program segment types.  */
   3054       bed = get_elf_backend_data (abfd);
   3055       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3056     }
   3057 }
   3058 
   3059 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3060    REL or RELA.  */
   3061 
   3062 Elf_Internal_Shdr *
   3063 _bfd_elf_single_rel_hdr (asection *sec)
   3064 {
   3065   if (elf_section_data (sec)->rel.hdr)
   3066     {
   3067       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3068       return elf_section_data (sec)->rel.hdr;
   3069     }
   3070   else
   3071     return elf_section_data (sec)->rela.hdr;
   3072 }
   3073 
   3074 static bool
   3075 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3076 			    Elf_Internal_Shdr *rel_hdr,
   3077 			    const char *sec_name,
   3078 			    bool use_rela_p)
   3079 {
   3080   char *name = (char *) bfd_alloc (abfd,
   3081 				   sizeof ".rela" + strlen (sec_name));
   3082   if (name == NULL)
   3083     return false;
   3084 
   3085   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3086   rel_hdr->sh_name =
   3087     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3088 					false);
   3089   if (rel_hdr->sh_name == (unsigned int) -1)
   3090     return false;
   3091 
   3092   return true;
   3093 }
   3094 
   3095 /* Allocate and initialize a section-header for a new reloc section,
   3096    containing relocations against ASECT.  It is stored in RELDATA.  If
   3097    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3098    relocations.  */
   3099 
   3100 static bool
   3101 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3102 			  struct bfd_elf_section_reloc_data *reldata,
   3103 			  const char *sec_name,
   3104 			  bool use_rela_p,
   3105 			  bool delay_st_name_p)
   3106 {
   3107   Elf_Internal_Shdr *rel_hdr;
   3108   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3109 
   3110   BFD_ASSERT (reldata->hdr == NULL);
   3111   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3112   reldata->hdr = rel_hdr;
   3113 
   3114   if (delay_st_name_p)
   3115     rel_hdr->sh_name = (unsigned int) -1;
   3116   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3117 					use_rela_p))
   3118     return false;
   3119   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3120   rel_hdr->sh_entsize = (use_rela_p
   3121 			 ? bed->s->sizeof_rela
   3122 			 : bed->s->sizeof_rel);
   3123   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3124   rel_hdr->sh_flags = 0;
   3125   rel_hdr->sh_addr = 0;
   3126   rel_hdr->sh_size = 0;
   3127   rel_hdr->sh_offset = 0;
   3128 
   3129   return true;
   3130 }
   3131 
   3132 /* Return the default section type based on the passed in section flags.  */
   3133 
   3134 int
   3135 bfd_elf_get_default_section_type (flagword flags)
   3136 {
   3137   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3138       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3139     return SHT_NOBITS;
   3140   return SHT_PROGBITS;
   3141 }
   3142 
   3143 struct fake_section_arg
   3144 {
   3145   struct bfd_link_info *link_info;
   3146   bool failed;
   3147 };
   3148 
   3149 /* Set up an ELF internal section header for a section.  */
   3150 
   3151 static void
   3152 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3153 {
   3154   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3155   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3156   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3157   Elf_Internal_Shdr *this_hdr;
   3158   unsigned int sh_type;
   3159   const char *name = asect->name;
   3160   bool delay_st_name_p = false;
   3161   bfd_vma mask;
   3162 
   3163   if (arg->failed)
   3164     {
   3165       /* We already failed; just get out of the bfd_map_over_sections
   3166 	 loop.  */
   3167       return;
   3168     }
   3169 
   3170   this_hdr = &esd->this_hdr;
   3171 
   3172   /* ld: compress DWARF debug sections with names: .debug_*.  */
   3173   if (arg->link_info
   3174       && (abfd->flags & BFD_COMPRESS) != 0
   3175       && (asect->flags & SEC_DEBUGGING) != 0
   3176       && name[1] == 'd'
   3177       && name[6] == '_')
   3178     {
   3179       /* If this section will be compressed, delay adding section
   3180 	 name to section name section after it is compressed in
   3181 	 _bfd_elf_assign_file_positions_for_non_load.  */
   3182       delay_st_name_p = true;
   3183     }
   3184 
   3185   if (delay_st_name_p)
   3186     this_hdr->sh_name = (unsigned int) -1;
   3187   else
   3188     {
   3189       this_hdr->sh_name
   3190 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3191 					      name, false);
   3192       if (this_hdr->sh_name == (unsigned int) -1)
   3193 	{
   3194 	  arg->failed = true;
   3195 	  return;
   3196 	}
   3197     }
   3198 
   3199   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3200 
   3201   if ((asect->flags & SEC_ALLOC) != 0
   3202       || asect->user_set_vma)
   3203     this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
   3204   else
   3205     this_hdr->sh_addr = 0;
   3206 
   3207   this_hdr->sh_offset = 0;
   3208   this_hdr->sh_size = asect->size;
   3209   this_hdr->sh_link = 0;
   3210   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3211   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3212     {
   3213       _bfd_error_handler
   3214 	/* xgettext:c-format */
   3215 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3216 	 abfd, asect->alignment_power, asect);
   3217       arg->failed = true;
   3218       return;
   3219     }
   3220   /* Set sh_addralign to the highest power of two given by alignment
   3221      consistent with the section VMA.  Linker scripts can force VMA.  */
   3222   mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
   3223   this_hdr->sh_addralign = mask & -mask;
   3224   /* The sh_entsize and sh_info fields may have been set already by
   3225      copy_private_section_data.  */
   3226 
   3227   this_hdr->bfd_section = asect;
   3228   this_hdr->contents = NULL;
   3229 
   3230   /* If the section type is unspecified, we set it based on
   3231      asect->flags.  */
   3232   if (asect->type != 0)
   3233     sh_type = asect->type;
   3234   else if ((asect->flags & SEC_GROUP) != 0)
   3235     sh_type = SHT_GROUP;
   3236   else
   3237     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3238 
   3239   if (this_hdr->sh_type == SHT_NULL)
   3240     this_hdr->sh_type = sh_type;
   3241   else if (this_hdr->sh_type == SHT_NOBITS
   3242 	   && sh_type == SHT_PROGBITS
   3243 	   && (asect->flags & SEC_ALLOC) != 0)
   3244     {
   3245       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3246 	 allow the link to proceed.  This can happen when users link
   3247 	 non-bss input sections to bss output sections, or emit data
   3248 	 to a bss output section via a linker script.  */
   3249       _bfd_error_handler
   3250 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3251       this_hdr->sh_type = sh_type;
   3252     }
   3253 
   3254   switch (this_hdr->sh_type)
   3255     {
   3256     default:
   3257       break;
   3258 
   3259     case SHT_STRTAB:
   3260     case SHT_NOTE:
   3261     case SHT_NOBITS:
   3262     case SHT_PROGBITS:
   3263       break;
   3264 
   3265     case SHT_INIT_ARRAY:
   3266     case SHT_FINI_ARRAY:
   3267     case SHT_PREINIT_ARRAY:
   3268       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3269       break;
   3270 
   3271     case SHT_HASH:
   3272       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3273       break;
   3274 
   3275     case SHT_DYNSYM:
   3276       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3277       break;
   3278 
   3279     case SHT_DYNAMIC:
   3280       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3281       break;
   3282 
   3283     case SHT_RELA:
   3284       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3285 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3286       break;
   3287 
   3288      case SHT_REL:
   3289       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3290 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3291       break;
   3292 
   3293      case SHT_GNU_versym:
   3294       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3295       break;
   3296 
   3297      case SHT_GNU_verdef:
   3298       this_hdr->sh_entsize = 0;
   3299       /* objcopy or strip will copy over sh_info, but may not set
   3300 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3301 	 zero.  */
   3302       if (this_hdr->sh_info == 0)
   3303 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3304       else
   3305 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3306 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3307       break;
   3308 
   3309     case SHT_GNU_verneed:
   3310       this_hdr->sh_entsize = 0;
   3311       /* objcopy or strip will copy over sh_info, but may not set
   3312 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3313 	 zero.  */
   3314       if (this_hdr->sh_info == 0)
   3315 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3316       else
   3317 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3318 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3319       break;
   3320 
   3321     case SHT_GROUP:
   3322       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3323       break;
   3324 
   3325     case SHT_GNU_HASH:
   3326       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3327       break;
   3328     }
   3329 
   3330   if ((asect->flags & SEC_ALLOC) != 0)
   3331     this_hdr->sh_flags |= SHF_ALLOC;
   3332   if ((asect->flags & SEC_READONLY) == 0)
   3333     this_hdr->sh_flags |= SHF_WRITE;
   3334   if ((asect->flags & SEC_CODE) != 0)
   3335     this_hdr->sh_flags |= SHF_EXECINSTR;
   3336   if ((asect->flags & SEC_MERGE) != 0)
   3337     {
   3338       this_hdr->sh_flags |= SHF_MERGE;
   3339       this_hdr->sh_entsize = asect->entsize;
   3340     }
   3341   if ((asect->flags & SEC_STRINGS) != 0)
   3342     this_hdr->sh_flags |= SHF_STRINGS;
   3343   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3344     this_hdr->sh_flags |= SHF_GROUP;
   3345   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3346     {
   3347       this_hdr->sh_flags |= SHF_TLS;
   3348       if (asect->size == 0
   3349 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3350 	{
   3351 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3352 
   3353 	  this_hdr->sh_size = 0;
   3354 	  if (o != NULL)
   3355 	    {
   3356 	      this_hdr->sh_size = o->offset + o->size;
   3357 	      if (this_hdr->sh_size != 0)
   3358 		this_hdr->sh_type = SHT_NOBITS;
   3359 	    }
   3360 	}
   3361     }
   3362   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3363     this_hdr->sh_flags |= SHF_EXCLUDE;
   3364 
   3365   /* If the section has relocs, set up a section header for the
   3366      SHT_REL[A] section.  If two relocation sections are required for
   3367      this section, it is up to the processor-specific back-end to
   3368      create the other.  */
   3369   if ((asect->flags & SEC_RELOC) != 0)
   3370     {
   3371       /* When doing a relocatable link, create both REL and RELA sections if
   3372 	 needed.  */
   3373       if (arg->link_info
   3374 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3375 	  && esd->rel.count + esd->rela.count > 0
   3376 	  && (bfd_link_relocatable (arg->link_info)
   3377 	      || arg->link_info->emitrelocations))
   3378 	{
   3379 	  if (esd->rel.count && esd->rel.hdr == NULL
   3380 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3381 					    false, delay_st_name_p))
   3382 	    {
   3383 	      arg->failed = true;
   3384 	      return;
   3385 	    }
   3386 	  if (esd->rela.count && esd->rela.hdr == NULL
   3387 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3388 					    true, delay_st_name_p))
   3389 	    {
   3390 	      arg->failed = true;
   3391 	      return;
   3392 	    }
   3393 	}
   3394       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3395 					  (asect->use_rela_p
   3396 					   ? &esd->rela : &esd->rel),
   3397 					  name,
   3398 					  asect->use_rela_p,
   3399 					  delay_st_name_p))
   3400 	{
   3401 	  arg->failed = true;
   3402 	  return;
   3403 	}
   3404     }
   3405 
   3406   /* Check for processor-specific section types.  */
   3407   sh_type = this_hdr->sh_type;
   3408   if (bed->elf_backend_fake_sections
   3409       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3410     {
   3411       arg->failed = true;
   3412       return;
   3413     }
   3414 
   3415   if (sh_type == SHT_NOBITS && asect->size != 0)
   3416     {
   3417       /* Don't change the header type from NOBITS if we are being
   3418 	 called for objcopy --only-keep-debug.  */
   3419       this_hdr->sh_type = sh_type;
   3420     }
   3421 }
   3422 
   3423 /* Fill in the contents of a SHT_GROUP section.  Called from
   3424    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3425    when ELF targets use the generic linker, ld.  Called for ld -r
   3426    from bfd_elf_final_link.  */
   3427 
   3428 void
   3429 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3430 {
   3431   bool *failedptr = (bool *) failedptrarg;
   3432   asection *elt, *first;
   3433   unsigned char *loc;
   3434   bool gas;
   3435 
   3436   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3437      elfxx-ia64.c.  */
   3438   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3439       || sec->size == 0
   3440       || *failedptr)
   3441     return;
   3442 
   3443   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3444     {
   3445       unsigned long symindx = 0;
   3446 
   3447       /* elf_group_id will have been set up by objcopy and the
   3448 	 generic linker.  */
   3449       if (elf_group_id (sec) != NULL)
   3450 	symindx = elf_group_id (sec)->udata.i;
   3451 
   3452       if (symindx == 0)
   3453 	{
   3454 	  /* If called from the assembler, swap_out_syms will have set up
   3455 	     elf_section_syms.
   3456 	     PR 25699: A corrupt input file could contain bogus group info.  */
   3457 	  if (sec->index >= elf_num_section_syms (abfd)
   3458 	      || elf_section_syms (abfd)[sec->index] == NULL)
   3459 	    {
   3460 	      *failedptr = true;
   3461 	      return;
   3462 	    }
   3463 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3464 	}
   3465       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3466     }
   3467   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3468     {
   3469       /* The ELF backend linker sets sh_info to -2 when the group
   3470 	 signature symbol is global, and thus the index can't be
   3471 	 set until all local symbols are output.  */
   3472       asection *igroup;
   3473       struct bfd_elf_section_data *sec_data;
   3474       unsigned long symndx;
   3475       unsigned long extsymoff;
   3476       struct elf_link_hash_entry *h;
   3477 
   3478       /* The point of this little dance to the first SHF_GROUP section
   3479 	 then back to the SHT_GROUP section is that this gets us to
   3480 	 the SHT_GROUP in the input object.  */
   3481       igroup = elf_sec_group (elf_next_in_group (sec));
   3482       sec_data = elf_section_data (igroup);
   3483       symndx = sec_data->this_hdr.sh_info;
   3484       extsymoff = 0;
   3485       if (!elf_bad_symtab (igroup->owner))
   3486 	{
   3487 	  Elf_Internal_Shdr *symtab_hdr;
   3488 
   3489 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3490 	  extsymoff = symtab_hdr->sh_info;
   3491 	}
   3492       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3493       while (h->root.type == bfd_link_hash_indirect
   3494 	     || h->root.type == bfd_link_hash_warning)
   3495 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3496 
   3497       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3498     }
   3499 
   3500   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3501   gas = true;
   3502   if (sec->contents == NULL)
   3503     {
   3504       gas = false;
   3505       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3506 
   3507       /* Arrange for the section to be written out.  */
   3508       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3509       if (sec->contents == NULL)
   3510 	{
   3511 	  *failedptr = true;
   3512 	  return;
   3513 	}
   3514     }
   3515 
   3516   loc = sec->contents + sec->size;
   3517 
   3518   /* Get the pointer to the first section in the group that gas
   3519      squirreled away here.  objcopy arranges for this to be set to the
   3520      start of the input section group.  */
   3521   first = elt = elf_next_in_group (sec);
   3522 
   3523   /* First element is a flag word.  Rest of section is elf section
   3524      indices for all the sections of the group.  Write them backwards
   3525      just to keep the group in the same order as given in .section
   3526      directives, not that it matters.  */
   3527   while (elt != NULL)
   3528     {
   3529       asection *s;
   3530 
   3531       s = elt;
   3532       if (!gas)
   3533 	s = s->output_section;
   3534       if (s != NULL
   3535 	  && !bfd_is_abs_section (s))
   3536 	{
   3537 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   3538 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   3539 
   3540 	  if (elf_sec->rel.hdr != NULL
   3541 	      && (gas
   3542 		  || (input_elf_sec->rel.hdr != NULL
   3543 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   3544 	    {
   3545 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   3546 	      loc -= 4;
   3547 	      if (loc == sec->contents)
   3548 		break;
   3549 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   3550 	    }
   3551 	  if (elf_sec->rela.hdr != NULL
   3552 	      && (gas
   3553 		  || (input_elf_sec->rela.hdr != NULL
   3554 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   3555 	    {
   3556 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   3557 	      loc -= 4;
   3558 	      if (loc == sec->contents)
   3559 		break;
   3560 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   3561 	    }
   3562 	  loc -= 4;
   3563 	  if (loc == sec->contents)
   3564 	    break;
   3565 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   3566 	}
   3567       elt = elf_next_in_group (elt);
   3568       if (elt == first)
   3569 	break;
   3570     }
   3571 
   3572   /* We should always get here with loc == sec->contents + 4, but it is
   3573      possible to craft bogus SHT_GROUP sections that will cause segfaults
   3574      in objcopy without checking loc here and in the loop above.  */
   3575   if (loc == sec->contents)
   3576     BFD_ASSERT (0);
   3577   else
   3578     {
   3579       loc -= 4;
   3580       if (loc != sec->contents)
   3581 	{
   3582 	  BFD_ASSERT (0);
   3583 	  memset (sec->contents + 4, 0, loc - sec->contents);
   3584 	  loc = sec->contents;
   3585 	}
   3586     }
   3587 
   3588   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   3589 }
   3590 
   3591 /* Given NAME, the name of a relocation section stripped of its
   3592    .rel/.rela prefix, return the section in ABFD to which the
   3593    relocations apply.  */
   3594 
   3595 asection *
   3596 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   3597 {
   3598   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   3599      section likely apply to .got.plt or .got section.  */
   3600   if (get_elf_backend_data (abfd)->want_got_plt
   3601       && strcmp (name, ".plt") == 0)
   3602     {
   3603       asection *sec;
   3604 
   3605       name = ".got.plt";
   3606       sec = bfd_get_section_by_name (abfd, name);
   3607       if (sec != NULL)
   3608 	return sec;
   3609       name = ".got";
   3610     }
   3611 
   3612   return bfd_get_section_by_name (abfd, name);
   3613 }
   3614 
   3615 /* Return the section to which RELOC_SEC applies.  */
   3616 
   3617 static asection *
   3618 elf_get_reloc_section (asection *reloc_sec)
   3619 {
   3620   const char *name;
   3621   unsigned int type;
   3622   bfd *abfd;
   3623   const struct elf_backend_data *bed;
   3624 
   3625   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   3626   if (type != SHT_REL && type != SHT_RELA)
   3627     return NULL;
   3628 
   3629   /* We look up the section the relocs apply to by name.  */
   3630   name = reloc_sec->name;
   3631   if (!startswith (name, ".rel"))
   3632     return NULL;
   3633   name += 4;
   3634   if (type == SHT_RELA && *name++ != 'a')
   3635     return NULL;
   3636 
   3637   abfd = reloc_sec->owner;
   3638   bed = get_elf_backend_data (abfd);
   3639   return bed->get_reloc_section (abfd, name);
   3640 }
   3641 
   3642 /* Assign all ELF section numbers.  The dummy first section is handled here
   3643    too.  The link/info pointers for the standard section types are filled
   3644    in here too, while we're at it.  LINK_INFO will be 0 when arriving
   3645    here for gas, objcopy, and when using the generic ELF linker.  */
   3646 
   3647 static bool
   3648 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   3649 {
   3650   struct elf_obj_tdata *t = elf_tdata (abfd);
   3651   asection *sec;
   3652   unsigned int section_number;
   3653   Elf_Internal_Shdr **i_shdrp;
   3654   struct bfd_elf_section_data *d;
   3655   bool need_symtab;
   3656   size_t amt;
   3657 
   3658   section_number = 1;
   3659 
   3660   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   3661 
   3662   /* SHT_GROUP sections are in relocatable files only.  */
   3663   if (link_info == NULL || !link_info->resolve_section_groups)
   3664     {
   3665       size_t reloc_count = 0;
   3666 
   3667       /* Put SHT_GROUP sections first.  */
   3668       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   3669 	{
   3670 	  d = elf_section_data (sec);
   3671 
   3672 	  if (d->this_hdr.sh_type == SHT_GROUP)
   3673 	    {
   3674 	      if (sec->flags & SEC_LINKER_CREATED)
   3675 		{
   3676 		  /* Remove the linker created SHT_GROUP sections.  */
   3677 		  bfd_section_list_remove (abfd, sec);
   3678 		  abfd->section_count--;
   3679 		}
   3680 	      else
   3681 		d->this_idx = section_number++;
   3682 	    }
   3683 
   3684 	  /* Count relocations.  */
   3685 	  reloc_count += sec->reloc_count;
   3686 	}
   3687 
   3688       /* Set/clear HAS_RELOC depending on whether there are relocations.  */
   3689       if (reloc_count == 0)
   3690 	abfd->flags &= ~HAS_RELOC;
   3691       else
   3692 	abfd->flags |= HAS_RELOC;
   3693     }
   3694 
   3695   for (sec = abfd->sections; sec; sec = sec->next)
   3696     {
   3697       d = elf_section_data (sec);
   3698 
   3699       if (d->this_hdr.sh_type != SHT_GROUP)
   3700 	d->this_idx = section_number++;
   3701       if (d->this_hdr.sh_name != (unsigned int) -1)
   3702 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   3703       if (d->rel.hdr)
   3704 	{
   3705 	  d->rel.idx = section_number++;
   3706 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   3707 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   3708 	}
   3709       else
   3710 	d->rel.idx = 0;
   3711 
   3712       if (d->rela.hdr)
   3713 	{
   3714 	  d->rela.idx = section_number++;
   3715 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   3716 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   3717 	}
   3718       else
   3719 	d->rela.idx = 0;
   3720     }
   3721 
   3722   need_symtab = (bfd_get_symcount (abfd) > 0
   3723 		 || (link_info == NULL
   3724 		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   3725 			 == HAS_RELOC)));
   3726   if (need_symtab)
   3727     {
   3728       elf_onesymtab (abfd) = section_number++;
   3729       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   3730       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   3731 	{
   3732 	  elf_section_list *entry;
   3733 
   3734 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   3735 
   3736 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   3737 	  entry->ndx = section_number++;
   3738 	  elf_symtab_shndx_list (abfd) = entry;
   3739 	  entry->hdr.sh_name
   3740 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3741 						  ".symtab_shndx", false);
   3742 	  if (entry->hdr.sh_name == (unsigned int) -1)
   3743 	    return false;
   3744 	}
   3745       elf_strtab_sec (abfd) = section_number++;
   3746       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   3747     }
   3748 
   3749   elf_shstrtab_sec (abfd) = section_number++;
   3750   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   3751   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   3752 
   3753   if (section_number >= SHN_LORESERVE)
   3754     {
   3755       /* xgettext:c-format */
   3756       _bfd_error_handler (_("%pB: too many sections: %u"),
   3757 			  abfd, section_number);
   3758       return false;
   3759     }
   3760 
   3761   elf_numsections (abfd) = section_number;
   3762   elf_elfheader (abfd)->e_shnum = section_number;
   3763 
   3764   /* Set up the list of section header pointers, in agreement with the
   3765      indices.  */
   3766   amt = section_number * sizeof (Elf_Internal_Shdr *);
   3767   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
   3768   if (i_shdrp == NULL)
   3769     return false;
   3770 
   3771   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   3772 						 sizeof (Elf_Internal_Shdr));
   3773   if (i_shdrp[0] == NULL)
   3774     {
   3775       bfd_release (abfd, i_shdrp);
   3776       return false;
   3777     }
   3778 
   3779   elf_elfsections (abfd) = i_shdrp;
   3780 
   3781   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   3782   if (need_symtab)
   3783     {
   3784       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   3785       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   3786 	{
   3787 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   3788 	  BFD_ASSERT (entry != NULL);
   3789 	  i_shdrp[entry->ndx] = & entry->hdr;
   3790 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   3791 	}
   3792       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   3793       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   3794     }
   3795 
   3796   for (sec = abfd->sections; sec; sec = sec->next)
   3797     {
   3798       asection *s;
   3799 
   3800       d = elf_section_data (sec);
   3801 
   3802       i_shdrp[d->this_idx] = &d->this_hdr;
   3803       if (d->rel.idx != 0)
   3804 	i_shdrp[d->rel.idx] = d->rel.hdr;
   3805       if (d->rela.idx != 0)
   3806 	i_shdrp[d->rela.idx] = d->rela.hdr;
   3807 
   3808       /* Fill in the sh_link and sh_info fields while we're at it.  */
   3809 
   3810       /* sh_link of a reloc section is the section index of the symbol
   3811 	 table.  sh_info is the section index of the section to which
   3812 	 the relocation entries apply.  */
   3813       if (d->rel.idx != 0)
   3814 	{
   3815 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   3816 	  d->rel.hdr->sh_info = d->this_idx;
   3817 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   3818 	}
   3819       if (d->rela.idx != 0)
   3820 	{
   3821 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   3822 	  d->rela.hdr->sh_info = d->this_idx;
   3823 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   3824 	}
   3825 
   3826       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   3827       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   3828 	{
   3829 	  s = elf_linked_to_section (sec);
   3830 	  /* We can now have a NULL linked section pointer.
   3831 	     This happens when the sh_link field is 0, which is done
   3832 	     when a linked to section is discarded but the linking
   3833 	     section has been retained for some reason.  */
   3834 	  if (s)
   3835 	    {
   3836 	      /* Check discarded linkonce section.  */
   3837 	      if (discarded_section (s))
   3838 		{
   3839 		  asection *kept;
   3840 		  _bfd_error_handler
   3841 		    /* xgettext:c-format */
   3842 		    (_("%pB: sh_link of section `%pA' points to"
   3843 		       " discarded section `%pA' of `%pB'"),
   3844 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   3845 		  /* Point to the kept section if it has the same
   3846 		     size as the discarded one.  */
   3847 		  kept = _bfd_elf_check_kept_section (s, link_info);
   3848 		  if (kept == NULL)
   3849 		    {
   3850 		      bfd_set_error (bfd_error_bad_value);
   3851 		      return false;
   3852 		    }
   3853 		  s = kept;
   3854 		}
   3855 	      /* Handle objcopy. */
   3856 	      else if (s->output_section == NULL)
   3857 		{
   3858 		  _bfd_error_handler
   3859 		    /* xgettext:c-format */
   3860 		    (_("%pB: sh_link of section `%pA' points to"
   3861 		       " removed section `%pA' of `%pB'"),
   3862 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   3863 		  bfd_set_error (bfd_error_bad_value);
   3864 		  return false;
   3865 		}
   3866 	      s = s->output_section;
   3867 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3868 	    }
   3869 	}
   3870 
   3871       switch (d->this_hdr.sh_type)
   3872 	{
   3873 	case SHT_REL:
   3874 	case SHT_RELA:
   3875 	  /* A reloc section which we are treating as a normal BFD
   3876 	     section.  sh_link is the section index of the symbol
   3877 	     table.  sh_info is the section index of the section to
   3878 	     which the relocation entries apply.  We assume that an
   3879 	     allocated reloc section uses the dynamic symbol table
   3880 	     if there is one.  Otherwise we guess the normal symbol
   3881 	     table.  FIXME: How can we be sure?  */
   3882 	  if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
   3883 	    {
   3884 	      s = bfd_get_section_by_name (abfd, ".dynsym");
   3885 	      if (s != NULL)
   3886 		d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3887 	    }
   3888 	  if (d->this_hdr.sh_link == 0)
   3889 	    d->this_hdr.sh_link = elf_onesymtab (abfd);
   3890 
   3891 	  s = elf_get_reloc_section (sec);
   3892 	  if (s != NULL)
   3893 	    {
   3894 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   3895 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   3896 	    }
   3897 	  break;
   3898 
   3899 	case SHT_STRTAB:
   3900 	  /* We assume that a section named .stab*str is a stabs
   3901 	     string section.  We look for a section with the same name
   3902 	     but without the trailing ``str'', and set its sh_link
   3903 	     field to point to this section.  */
   3904 	  if (startswith (sec->name, ".stab")
   3905 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   3906 	    {
   3907 	      size_t len;
   3908 	      char *alc;
   3909 
   3910 	      len = strlen (sec->name);
   3911 	      alc = (char *) bfd_malloc (len - 2);
   3912 	      if (alc == NULL)
   3913 		return false;
   3914 	      memcpy (alc, sec->name, len - 3);
   3915 	      alc[len - 3] = '\0';
   3916 	      s = bfd_get_section_by_name (abfd, alc);
   3917 	      free (alc);
   3918 	      if (s != NULL)
   3919 		{
   3920 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   3921 
   3922 		  /* This is a .stab section.  */
   3923 		  elf_section_data (s)->this_hdr.sh_entsize = 12;
   3924 		}
   3925 	    }
   3926 	  break;
   3927 
   3928 	case SHT_DYNAMIC:
   3929 	case SHT_DYNSYM:
   3930 	case SHT_GNU_verneed:
   3931 	case SHT_GNU_verdef:
   3932 	  /* sh_link is the section header index of the string table
   3933 	     used for the dynamic entries, or the symbol table, or the
   3934 	     version strings.  */
   3935 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   3936 	  if (s != NULL)
   3937 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3938 	  break;
   3939 
   3940 	case SHT_GNU_LIBLIST:
   3941 	  /* sh_link is the section header index of the prelink library
   3942 	     list used for the dynamic entries, or the symbol table, or
   3943 	     the version strings.  */
   3944 	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
   3945 					      ? ".dynstr" : ".gnu.libstr"));
   3946 	  if (s != NULL)
   3947 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3948 	  break;
   3949 
   3950 	case SHT_HASH:
   3951 	case SHT_GNU_HASH:
   3952 	case SHT_GNU_versym:
   3953 	  /* sh_link is the section header index of the symbol table
   3954 	     this hash table or version table is for.  */
   3955 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3956 	  if (s != NULL)
   3957 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3958 	  break;
   3959 
   3960 	case SHT_GROUP:
   3961 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   3962 	}
   3963     }
   3964 
   3965   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   3966      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   3967      debug section name from .debug_* to .zdebug_* if needed.  */
   3968 
   3969   return true;
   3970 }
   3971 
   3972 static bool
   3973 sym_is_global (bfd *abfd, asymbol *sym)
   3974 {
   3975   /* If the backend has a special mapping, use it.  */
   3976   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3977   if (bed->elf_backend_sym_is_global)
   3978     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   3979 
   3980   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   3981 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   3982 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   3983 }
   3984 
   3985 /* Filter global symbols of ABFD to include in the import library.  All
   3986    SYMCOUNT symbols of ABFD can be examined from their pointers in
   3987    SYMS.  Pointers of symbols to keep should be stored contiguously at
   3988    the beginning of that array.
   3989 
   3990    Returns the number of symbols to keep.  */
   3991 
   3992 unsigned int
   3993 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   3994 				asymbol **syms, long symcount)
   3995 {
   3996   long src_count, dst_count = 0;
   3997 
   3998   for (src_count = 0; src_count < symcount; src_count++)
   3999     {
   4000       asymbol *sym = syms[src_count];
   4001       char *name = (char *) bfd_asymbol_name (sym);
   4002       struct bfd_link_hash_entry *h;
   4003 
   4004       if (!sym_is_global (abfd, sym))
   4005 	continue;
   4006 
   4007       h = bfd_link_hash_lookup (info->hash, name, false, false, false);
   4008       if (h == NULL)
   4009 	continue;
   4010       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4011 	continue;
   4012       if (h->linker_def || h->ldscript_def)
   4013 	continue;
   4014 
   4015       syms[dst_count++] = sym;
   4016     }
   4017 
   4018   syms[dst_count] = NULL;
   4019 
   4020   return dst_count;
   4021 }
   4022 
   4023 /* Don't output section symbols for sections that are not going to be
   4024    output, that are duplicates or there is no BFD section.  */
   4025 
   4026 static bool
   4027 ignore_section_sym (bfd *abfd, asymbol *sym)
   4028 {
   4029   elf_symbol_type *type_ptr;
   4030 
   4031   if (sym == NULL)
   4032     return false;
   4033 
   4034   if ((sym->flags & BSF_SECTION_SYM) == 0)
   4035     return false;
   4036 
   4037   /* Ignore the section symbol if it isn't used.  */
   4038   if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
   4039     return true;
   4040 
   4041   if (sym->section == NULL)
   4042     return true;
   4043 
   4044   type_ptr = elf_symbol_from (sym);
   4045   return ((type_ptr != NULL
   4046 	   && type_ptr->internal_elf_sym.st_shndx != 0
   4047 	   && bfd_is_abs_section (sym->section))
   4048 	  || !(sym->section->owner == abfd
   4049 	       || (sym->section->output_section != NULL
   4050 		   && sym->section->output_section->owner == abfd
   4051 		   && sym->section->output_offset == 0)
   4052 	       || bfd_is_abs_section (sym->section)));
   4053 }
   4054 
   4055 /* Map symbol from it's internal number to the external number, moving
   4056    all local symbols to be at the head of the list.  */
   4057 
   4058 static bool
   4059 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4060 {
   4061   unsigned int symcount = bfd_get_symcount (abfd);
   4062   asymbol **syms = bfd_get_outsymbols (abfd);
   4063   asymbol **sect_syms;
   4064   unsigned int num_locals = 0;
   4065   unsigned int num_globals = 0;
   4066   unsigned int num_locals2 = 0;
   4067   unsigned int num_globals2 = 0;
   4068   unsigned int max_index = 0;
   4069   unsigned int idx;
   4070   asection *asect;
   4071   asymbol **new_syms;
   4072   size_t amt;
   4073 
   4074 #ifdef DEBUG
   4075   fprintf (stderr, "elf_map_symbols\n");
   4076   fflush (stderr);
   4077 #endif
   4078 
   4079   for (asect = abfd->sections; asect; asect = asect->next)
   4080     {
   4081       if (max_index < asect->index)
   4082 	max_index = asect->index;
   4083     }
   4084 
   4085   max_index++;
   4086   amt = max_index * sizeof (asymbol *);
   4087   sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
   4088   if (sect_syms == NULL)
   4089     return false;
   4090   elf_section_syms (abfd) = sect_syms;
   4091   elf_num_section_syms (abfd) = max_index;
   4092 
   4093   /* Init sect_syms entries for any section symbols we have already
   4094      decided to output.  */
   4095   for (idx = 0; idx < symcount; idx++)
   4096     {
   4097       asymbol *sym = syms[idx];
   4098 
   4099       if ((sym->flags & BSF_SECTION_SYM) != 0
   4100 	  && sym->value == 0
   4101 	  && !ignore_section_sym (abfd, sym)
   4102 	  && !bfd_is_abs_section (sym->section))
   4103 	{
   4104 	  asection *sec = sym->section;
   4105 
   4106 	  if (sec->owner != abfd)
   4107 	    sec = sec->output_section;
   4108 
   4109 	  sect_syms[sec->index] = syms[idx];
   4110 	}
   4111     }
   4112 
   4113   /* Classify all of the symbols.  */
   4114   for (idx = 0; idx < symcount; idx++)
   4115     {
   4116       if (sym_is_global (abfd, syms[idx]))
   4117 	num_globals++;
   4118       else if (!ignore_section_sym (abfd, syms[idx]))
   4119 	num_locals++;
   4120     }
   4121 
   4122   /* We will be adding a section symbol for each normal BFD section.  Most
   4123      sections will already have a section symbol in outsymbols, but
   4124      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4125      at least in that case.  */
   4126   for (asect = abfd->sections; asect; asect = asect->next)
   4127     {
   4128       asymbol *sym = asect->symbol;
   4129       /* Don't include ignored section symbols.  */
   4130       if (!ignore_section_sym (abfd, sym)
   4131 	  && sect_syms[asect->index] == NULL)
   4132 	{
   4133 	  if (!sym_is_global (abfd, asect->symbol))
   4134 	    num_locals++;
   4135 	  else
   4136 	    num_globals++;
   4137 	}
   4138     }
   4139 
   4140   /* Now sort the symbols so the local symbols are first.  */
   4141   amt = (num_locals + num_globals) * sizeof (asymbol *);
   4142   new_syms = (asymbol **) bfd_alloc (abfd, amt);
   4143   if (new_syms == NULL)
   4144     return false;
   4145 
   4146   for (idx = 0; idx < symcount; idx++)
   4147     {
   4148       asymbol *sym = syms[idx];
   4149       unsigned int i;
   4150 
   4151       if (sym_is_global (abfd, sym))
   4152 	i = num_locals + num_globals2++;
   4153       /* Don't include ignored section symbols.  */
   4154       else if (!ignore_section_sym (abfd, sym))
   4155 	i = num_locals2++;
   4156       else
   4157 	continue;
   4158       new_syms[i] = sym;
   4159       sym->udata.i = i + 1;
   4160     }
   4161   for (asect = abfd->sections; asect; asect = asect->next)
   4162     {
   4163       asymbol *sym = asect->symbol;
   4164       if (!ignore_section_sym (abfd, sym)
   4165 	  && sect_syms[asect->index] == NULL)
   4166 	{
   4167 	  unsigned int i;
   4168 
   4169 	  sect_syms[asect->index] = sym;
   4170 	  if (!sym_is_global (abfd, sym))
   4171 	    i = num_locals2++;
   4172 	  else
   4173 	    i = num_locals + num_globals2++;
   4174 	  new_syms[i] = sym;
   4175 	  sym->udata.i = i + 1;
   4176 	}
   4177     }
   4178 
   4179   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4180 
   4181   *pnum_locals = num_locals;
   4182   return true;
   4183 }
   4184 
   4185 /* Align to the maximum file alignment that could be required for any
   4186    ELF data structure.  */
   4187 
   4188 static inline file_ptr
   4189 align_file_position (file_ptr off, int align)
   4190 {
   4191   return (off + align - 1) & ~(align - 1);
   4192 }
   4193 
   4194 /* Assign a file position to a section, optionally aligning to the
   4195    required section alignment.  */
   4196 
   4197 file_ptr
   4198 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4199 					   file_ptr offset,
   4200 					   bool align)
   4201 {
   4202   if (align && i_shdrp->sh_addralign > 1)
   4203     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
   4204   i_shdrp->sh_offset = offset;
   4205   if (i_shdrp->bfd_section != NULL)
   4206     i_shdrp->bfd_section->filepos = offset;
   4207   if (i_shdrp->sh_type != SHT_NOBITS)
   4208     offset += i_shdrp->sh_size;
   4209   return offset;
   4210 }
   4211 
   4212 /* Compute the file positions we are going to put the sections at, and
   4213    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4214    is not NULL, this is being called by the ELF backend linker.  */
   4215 
   4216 bool
   4217 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4218 					 struct bfd_link_info *link_info)
   4219 {
   4220   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4221   struct fake_section_arg fsargs;
   4222   bool failed;
   4223   struct elf_strtab_hash *strtab = NULL;
   4224   Elf_Internal_Shdr *shstrtab_hdr;
   4225   bool need_symtab;
   4226 
   4227   if (abfd->output_has_begun)
   4228     return true;
   4229 
   4230   /* Do any elf backend specific processing first.  */
   4231   if (bed->elf_backend_begin_write_processing)
   4232     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4233 
   4234   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4235     return false;
   4236 
   4237   fsargs.failed = false;
   4238   fsargs.link_info = link_info;
   4239   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4240   if (fsargs.failed)
   4241     return false;
   4242 
   4243   if (!assign_section_numbers (abfd, link_info))
   4244     return false;
   4245 
   4246   /* The backend linker builds symbol table information itself.  */
   4247   need_symtab = (link_info == NULL
   4248 		 && (bfd_get_symcount (abfd) > 0
   4249 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4250 			 == HAS_RELOC)));
   4251   if (need_symtab)
   4252     {
   4253       /* Non-zero if doing a relocatable link.  */
   4254       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4255 
   4256       if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
   4257 	return false;
   4258     }
   4259 
   4260   failed = false;
   4261   if (link_info == NULL)
   4262     {
   4263       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4264       if (failed)
   4265 	return false;
   4266     }
   4267 
   4268   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4269   /* sh_name was set in init_file_header.  */
   4270   shstrtab_hdr->sh_type = SHT_STRTAB;
   4271   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4272   shstrtab_hdr->sh_addr = 0;
   4273   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4274   shstrtab_hdr->sh_entsize = 0;
   4275   shstrtab_hdr->sh_link = 0;
   4276   shstrtab_hdr->sh_info = 0;
   4277   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4278   shstrtab_hdr->sh_addralign = 1;
   4279 
   4280   if (!assign_file_positions_except_relocs (abfd, link_info))
   4281     return false;
   4282 
   4283   if (need_symtab)
   4284     {
   4285       file_ptr off;
   4286       Elf_Internal_Shdr *hdr;
   4287 
   4288       off = elf_next_file_pos (abfd);
   4289 
   4290       hdr = & elf_symtab_hdr (abfd);
   4291       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4292 
   4293       if (elf_symtab_shndx_list (abfd) != NULL)
   4294 	{
   4295 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4296 	  if (hdr->sh_size != 0)
   4297 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4298 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4299 	}
   4300 
   4301       hdr = &elf_tdata (abfd)->strtab_hdr;
   4302       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4303 
   4304       elf_next_file_pos (abfd) = off;
   4305 
   4306       /* Now that we know where the .strtab section goes, write it
   4307 	 out.  */
   4308       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4309 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4310 	return false;
   4311       _bfd_elf_strtab_free (strtab);
   4312     }
   4313 
   4314   abfd->output_has_begun = true;
   4315 
   4316   return true;
   4317 }
   4318 
   4319 /* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
   4320    function effectively returns whether --eh-frame-hdr is given on the
   4321    command line.  After size_dynamic_sections the result reflects
   4322    whether .eh_frame_hdr will actually be output (sizing isn't done
   4323    until ldemul_after_allocation).  */
   4324 
   4325 static asection *
   4326 elf_eh_frame_hdr (const struct bfd_link_info *info)
   4327 {
   4328   if (info != NULL && is_elf_hash_table (info->hash))
   4329     return elf_hash_table (info)->eh_info.hdr_sec;
   4330   return NULL;
   4331 }
   4332 
   4333 /* Make an initial estimate of the size of the program header.  If we
   4334    get the number wrong here, we'll redo section placement.  */
   4335 
   4336 static bfd_size_type
   4337 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4338 {
   4339   size_t segs;
   4340   asection *s;
   4341   const struct elf_backend_data *bed;
   4342 
   4343   /* Assume we will need exactly two PT_LOAD segments: one for text
   4344      and one for data.  */
   4345   segs = 2;
   4346 
   4347   s = bfd_get_section_by_name (abfd, ".interp");
   4348   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4349     {
   4350       /* If we have a loadable interpreter section, we need a
   4351 	 PT_INTERP segment.  In this case, assume we also need a
   4352 	 PT_PHDR segment, although that may not be true for all
   4353 	 targets.  */
   4354       segs += 2;
   4355     }
   4356 
   4357   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
   4358     {
   4359       /* We need a PT_DYNAMIC segment.  */
   4360       ++segs;
   4361     }
   4362 
   4363   if (info != NULL && info->relro)
   4364     {
   4365       /* We need a PT_GNU_RELRO segment.  */
   4366       ++segs;
   4367     }
   4368 
   4369   if (elf_eh_frame_hdr (info))
   4370     {
   4371       /* We need a PT_GNU_EH_FRAME segment.  */
   4372       ++segs;
   4373     }
   4374 
   4375   if (elf_stack_flags (abfd))
   4376     {
   4377       /* We need a PT_GNU_STACK segment.  */
   4378       ++segs;
   4379     }
   4380 
   4381   if (elf_sframe (abfd))
   4382     {
   4383       /* We need a PT_GNU_SFRAME segment.  */
   4384       ++segs;
   4385     }
   4386 
   4387   s = bfd_get_section_by_name (abfd,
   4388 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4389   if (s != NULL && s->size != 0)
   4390     {
   4391       /* We need a PT_GNU_PROPERTY segment.  */
   4392       ++segs;
   4393     }
   4394 
   4395   for (s = abfd->sections; s != NULL; s = s->next)
   4396     {
   4397       if ((s->flags & SEC_LOAD) != 0
   4398 	  && elf_section_type (s) == SHT_NOTE)
   4399 	{
   4400 	  unsigned int alignment_power;
   4401 	  /* We need a PT_NOTE segment.  */
   4402 	  ++segs;
   4403 	  /* Try to create just one PT_NOTE segment for all adjacent
   4404 	     loadable SHT_NOTE sections.  gABI requires that within a
   4405 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4406 	     each note should have the same alignment.  So we check
   4407 	     whether the sections are correctly aligned.  */
   4408 	  alignment_power = s->alignment_power;
   4409 	  while (s->next != NULL
   4410 		 && s->next->alignment_power == alignment_power
   4411 		 && (s->next->flags & SEC_LOAD) != 0
   4412 		 && elf_section_type (s->next) == SHT_NOTE)
   4413 	    s = s->next;
   4414 	}
   4415     }
   4416 
   4417   for (s = abfd->sections; s != NULL; s = s->next)
   4418     {
   4419       if (s->flags & SEC_THREAD_LOCAL)
   4420 	{
   4421 	  /* We need a PT_TLS segment.  */
   4422 	  ++segs;
   4423 	  break;
   4424 	}
   4425     }
   4426 
   4427   bed = get_elf_backend_data (abfd);
   4428 
   4429   if ((abfd->flags & D_PAGED) != 0
   4430       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4431     {
   4432       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4433       bfd_vma commonpagesize;
   4434       unsigned int page_align_power;
   4435 
   4436       if (info != NULL)
   4437 	commonpagesize = info->commonpagesize;
   4438       else
   4439 	commonpagesize = bed->commonpagesize;
   4440       page_align_power = bfd_log2 (commonpagesize);
   4441       for (s = abfd->sections; s != NULL; s = s->next)
   4442 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   4443 	  {
   4444 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   4445 	      {
   4446 		_bfd_error_handler
   4447 		  /* xgettext:c-format */
   4448 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   4449 		     "sh_info field: %d"),
   4450 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4451 		continue;
   4452 	      }
   4453 	    /* Align mbind section to page size.  */
   4454 	    if (s->alignment_power < page_align_power)
   4455 	      s->alignment_power = page_align_power;
   4456 	    segs ++;
   4457 	  }
   4458     }
   4459 
   4460   /* Let the backend count up any program headers it might need.  */
   4461   if (bed->elf_backend_additional_program_headers)
   4462     {
   4463       int a;
   4464 
   4465       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4466       if (a == -1)
   4467 	abort ();
   4468       segs += a;
   4469     }
   4470 
   4471   return segs * bed->s->sizeof_phdr;
   4472 }
   4473 
   4474 /* Find the segment that contains the output_section of section.  */
   4475 
   4476 Elf_Internal_Phdr *
   4477 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4478 {
   4479   struct elf_segment_map *m;
   4480   Elf_Internal_Phdr *p;
   4481 
   4482   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4483        m != NULL;
   4484        m = m->next, p++)
   4485     {
   4486       int i;
   4487 
   4488       for (i = m->count - 1; i >= 0; i--)
   4489 	if (m->sections[i] == section)
   4490 	  return p;
   4491     }
   4492 
   4493   return NULL;
   4494 }
   4495 
   4496 /* Create a mapping from a set of sections to a program segment.  */
   4497 
   4498 static struct elf_segment_map *
   4499 make_mapping (bfd *abfd,
   4500 	      asection **sections,
   4501 	      unsigned int from,
   4502 	      unsigned int to,
   4503 	      bool phdr)
   4504 {
   4505   struct elf_segment_map *m;
   4506   unsigned int i;
   4507   asection **hdrpp;
   4508   size_t amt;
   4509 
   4510   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   4511   amt += (to - from) * sizeof (asection *);
   4512   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4513   if (m == NULL)
   4514     return NULL;
   4515   m->next = NULL;
   4516   m->p_type = PT_LOAD;
   4517   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4518     m->sections[i - from] = *hdrpp;
   4519   m->count = to - from;
   4520 
   4521   if (from == 0 && phdr)
   4522     {
   4523       /* Include the headers in the first PT_LOAD segment.  */
   4524       m->includes_filehdr = 1;
   4525       m->includes_phdrs = 1;
   4526     }
   4527 
   4528   return m;
   4529 }
   4530 
   4531 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   4532    on failure.  */
   4533 
   4534 struct elf_segment_map *
   4535 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   4536 {
   4537   struct elf_segment_map *m;
   4538 
   4539   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   4540 					     sizeof (struct elf_segment_map));
   4541   if (m == NULL)
   4542     return NULL;
   4543   m->next = NULL;
   4544   m->p_type = PT_DYNAMIC;
   4545   m->count = 1;
   4546   m->sections[0] = dynsec;
   4547 
   4548   return m;
   4549 }
   4550 
   4551 /* Possibly add or remove segments from the segment map.  */
   4552 
   4553 static bool
   4554 elf_modify_segment_map (bfd *abfd,
   4555 			struct bfd_link_info *info,
   4556 			bool remove_empty_load)
   4557 {
   4558   struct elf_segment_map **m;
   4559   const struct elf_backend_data *bed;
   4560 
   4561   /* The placement algorithm assumes that non allocated sections are
   4562      not in PT_LOAD segments.  We ensure this here by removing such
   4563      sections from the segment map.  We also remove excluded
   4564      sections.  Finally, any PT_LOAD segment without sections is
   4565      removed.  */
   4566   m = &elf_seg_map (abfd);
   4567   while (*m)
   4568     {
   4569       unsigned int i, new_count;
   4570 
   4571       for (new_count = 0, i = 0; i < (*m)->count; i++)
   4572 	{
   4573 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   4574 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   4575 		  || (*m)->p_type != PT_LOAD))
   4576 	    {
   4577 	      (*m)->sections[new_count] = (*m)->sections[i];
   4578 	      new_count++;
   4579 	    }
   4580 	}
   4581       (*m)->count = new_count;
   4582 
   4583       if (remove_empty_load
   4584 	  && (*m)->p_type == PT_LOAD
   4585 	  && (*m)->count == 0
   4586 	  && !(*m)->includes_phdrs)
   4587 	*m = (*m)->next;
   4588       else
   4589 	m = &(*m)->next;
   4590     }
   4591 
   4592   bed = get_elf_backend_data (abfd);
   4593   if (bed->elf_backend_modify_segment_map != NULL)
   4594     {
   4595       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   4596 	return false;
   4597     }
   4598 
   4599   return true;
   4600 }
   4601 
   4602 #define IS_TBSS(s) \
   4603   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   4604 
   4605 /* Set up a mapping from BFD sections to program segments.  Update
   4606    NEED_LAYOUT if the section layout is changed.  */
   4607 
   4608 bool
   4609 _bfd_elf_map_sections_to_segments (bfd *abfd,
   4610 				   struct bfd_link_info *info,
   4611 				   bool *need_layout)
   4612 {
   4613   unsigned int count;
   4614   struct elf_segment_map *m;
   4615   asection **sections = NULL;
   4616   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4617   bool no_user_phdrs;
   4618 
   4619   no_user_phdrs = elf_seg_map (abfd) == NULL;
   4620 
   4621   if (info != NULL)
   4622     {
   4623       info->user_phdrs = !no_user_phdrs;
   4624 
   4625       /* Size the relative relocations if DT_RELR is enabled.  */
   4626       if (info->enable_dt_relr
   4627 	  && need_layout != NULL
   4628 	  && bed->size_relative_relocs
   4629 	  && !bed->size_relative_relocs (info, need_layout))
   4630 	info->callbacks->einfo
   4631 	  (_("%F%P: failed to size relative relocations\n"));
   4632     }
   4633 
   4634   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   4635     {
   4636       asection *s;
   4637       unsigned int i;
   4638       struct elf_segment_map *mfirst;
   4639       struct elf_segment_map **pm;
   4640       asection *last_hdr;
   4641       bfd_vma last_size;
   4642       unsigned int hdr_index;
   4643       bfd_vma maxpagesize;
   4644       asection **hdrpp;
   4645       bool phdr_in_segment;
   4646       bool writable;
   4647       bool executable;
   4648       unsigned int tls_count = 0;
   4649       asection *first_tls = NULL;
   4650       asection *first_mbind = NULL;
   4651       asection *dynsec, *eh_frame_hdr;
   4652       asection *sframe;
   4653       size_t amt;
   4654       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   4655       bfd_size_type phdr_size;  /* Octets/bytes.  */
   4656       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   4657 
   4658       /* Select the allocated sections, and sort them.  */
   4659 
   4660       amt = bfd_count_sections (abfd) * sizeof (asection *);
   4661       sections = (asection **) bfd_malloc (amt);
   4662       if (sections == NULL)
   4663 	goto error_return;
   4664 
   4665       /* Calculate top address, avoiding undefined behaviour of shift
   4666 	 left operator when shift count is equal to size of type
   4667 	 being shifted.  */
   4668       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   4669       addr_mask = (addr_mask << 1) + 1;
   4670 
   4671       i = 0;
   4672       for (s = abfd->sections; s != NULL; s = s->next)
   4673 	{
   4674 	  if ((s->flags & SEC_ALLOC) != 0)
   4675 	    {
   4676 	      /* target_index is unused until bfd_elf_final_link
   4677 		 starts output of section symbols.  Use it to make
   4678 		 qsort stable.  */
   4679 	      s->target_index = i;
   4680 	      sections[i] = s;
   4681 	      ++i;
   4682 	      /* A wrapping section potentially clashes with header.  */
   4683 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   4684 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   4685 	    }
   4686 	}
   4687       BFD_ASSERT (i <= bfd_count_sections (abfd));
   4688       count = i;
   4689 
   4690       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   4691 
   4692       phdr_size = elf_program_header_size (abfd);
   4693       if (phdr_size == (bfd_size_type) -1)
   4694 	phdr_size = get_program_header_size (abfd, info);
   4695       phdr_size += bed->s->sizeof_ehdr;
   4696       /* phdr_size is compared to LMA values which are in bytes.  */
   4697       phdr_size /= opb;
   4698       if (info != NULL)
   4699 	maxpagesize = info->maxpagesize;
   4700       else
   4701 	maxpagesize = bed->maxpagesize;
   4702       if (maxpagesize == 0)
   4703 	maxpagesize = 1;
   4704       phdr_in_segment = info != NULL && info->load_phdrs;
   4705       if (count != 0
   4706 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   4707 	      >= (phdr_size & (maxpagesize - 1))))
   4708 	/* For compatibility with old scripts that may not be using
   4709 	   SIZEOF_HEADERS, add headers when it looks like space has
   4710 	   been left for them.  */
   4711 	phdr_in_segment = true;
   4712 
   4713       /* Build the mapping.  */
   4714       mfirst = NULL;
   4715       pm = &mfirst;
   4716 
   4717       /* If we have a .interp section, then create a PT_PHDR segment for
   4718 	 the program headers and a PT_INTERP segment for the .interp
   4719 	 section.  */
   4720       s = bfd_get_section_by_name (abfd, ".interp");
   4721       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4722 	{
   4723 	  amt = sizeof (struct elf_segment_map);
   4724 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4725 	  if (m == NULL)
   4726 	    goto error_return;
   4727 	  m->next = NULL;
   4728 	  m->p_type = PT_PHDR;
   4729 	  m->p_flags = PF_R;
   4730 	  m->p_flags_valid = 1;
   4731 	  m->includes_phdrs = 1;
   4732 	  phdr_in_segment = true;
   4733 	  *pm = m;
   4734 	  pm = &m->next;
   4735 
   4736 	  amt = sizeof (struct elf_segment_map);
   4737 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4738 	  if (m == NULL)
   4739 	    goto error_return;
   4740 	  m->next = NULL;
   4741 	  m->p_type = PT_INTERP;
   4742 	  m->count = 1;
   4743 	  m->sections[0] = s;
   4744 
   4745 	  *pm = m;
   4746 	  pm = &m->next;
   4747 	}
   4748 
   4749       /* Look through the sections.  We put sections in the same program
   4750 	 segment when the start of the second section can be placed within
   4751 	 a few bytes of the end of the first section.  */
   4752       last_hdr = NULL;
   4753       last_size = 0;
   4754       hdr_index = 0;
   4755       writable = false;
   4756       executable = false;
   4757       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   4758       if (dynsec != NULL
   4759 	  && (dynsec->flags & SEC_LOAD) == 0)
   4760 	dynsec = NULL;
   4761 
   4762       if ((abfd->flags & D_PAGED) == 0)
   4763 	phdr_in_segment = false;
   4764 
   4765       /* Deal with -Ttext or something similar such that the first section
   4766 	 is not adjacent to the program headers.  This is an
   4767 	 approximation, since at this point we don't know exactly how many
   4768 	 program headers we will need.  */
   4769       if (phdr_in_segment && count > 0)
   4770 	{
   4771 	  bfd_vma phdr_lma;  /* Bytes.  */
   4772 	  bool separate_phdr = false;
   4773 
   4774 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   4775 	  if (info != NULL
   4776 	      && info->separate_code
   4777 	      && (sections[0]->flags & SEC_CODE) != 0)
   4778 	    {
   4779 	      /* If data sections should be separate from code and
   4780 		 thus not executable, and the first section is
   4781 		 executable then put the file and program headers in
   4782 		 their own PT_LOAD.  */
   4783 	      separate_phdr = true;
   4784 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   4785 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   4786 		{
   4787 		  /* The file and program headers are currently on the
   4788 		     same page as the first section.  Put them on the
   4789 		     previous page if we can.  */
   4790 		  if (phdr_lma >= maxpagesize)
   4791 		    phdr_lma -= maxpagesize;
   4792 		  else
   4793 		    separate_phdr = false;
   4794 		}
   4795 	    }
   4796 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   4797 	      || (sections[0]->lma & addr_mask) < phdr_size)
   4798 	    /* If file and program headers would be placed at the end
   4799 	       of memory then it's probably better to omit them.  */
   4800 	    phdr_in_segment = false;
   4801 	  else if (phdr_lma < wrap_to)
   4802 	    /* If a section wraps around to where we'll be placing
   4803 	       file and program headers, then the headers will be
   4804 	       overwritten.  */
   4805 	    phdr_in_segment = false;
   4806 	  else if (separate_phdr)
   4807 	    {
   4808 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   4809 	      if (m == NULL)
   4810 		goto error_return;
   4811 	      m->p_paddr = phdr_lma * opb;
   4812 	      m->p_vaddr_offset
   4813 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   4814 	      m->p_paddr_valid = 1;
   4815 	      *pm = m;
   4816 	      pm = &m->next;
   4817 	      phdr_in_segment = false;
   4818 	    }
   4819 	}
   4820 
   4821       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   4822 	{
   4823 	  asection *hdr;
   4824 	  bool new_segment;
   4825 
   4826 	  hdr = *hdrpp;
   4827 
   4828 	  /* See if this section and the last one will fit in the same
   4829 	     segment.  */
   4830 
   4831 	  if (last_hdr == NULL)
   4832 	    {
   4833 	      /* If we don't have a segment yet, then we don't need a new
   4834 		 one (we build the last one after this loop).  */
   4835 	      new_segment = false;
   4836 	    }
   4837 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   4838 	    {
   4839 	      /* If this section has a different relation between the
   4840 		 virtual address and the load address, then we need a new
   4841 		 segment.  */
   4842 	      new_segment = true;
   4843 	    }
   4844 	  else if (hdr->lma < last_hdr->lma + last_size
   4845 		   || last_hdr->lma + last_size < last_hdr->lma)
   4846 	    {
   4847 	      /* If this section has a load address that makes it overlap
   4848 		 the previous section, then we need a new segment.  */
   4849 	      new_segment = true;
   4850 	    }
   4851 	  else if ((abfd->flags & D_PAGED) != 0
   4852 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   4853 		       == (hdr->lma & -maxpagesize)))
   4854 	    {
   4855 	      /* If we are demand paged then we can't map two disk
   4856 		 pages onto the same memory page.  */
   4857 	      new_segment = false;
   4858 	    }
   4859 	  /* In the next test we have to be careful when last_hdr->lma is close
   4860 	     to the end of the address space.  If the aligned address wraps
   4861 	     around to the start of the address space, then there are no more
   4862 	     pages left in memory and it is OK to assume that the current
   4863 	     section can be included in the current segment.  */
   4864 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4865 		    + maxpagesize > last_hdr->lma)
   4866 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4867 		       + maxpagesize <= hdr->lma))
   4868 	    {
   4869 	      /* If putting this section in this segment would force us to
   4870 		 skip a page in the segment, then we need a new segment.  */
   4871 	      new_segment = true;
   4872 	    }
   4873 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   4874 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   4875 	    {
   4876 	      /* We don't want to put a loaded section after a
   4877 		 nonloaded (ie. bss style) section in the same segment
   4878 		 as that will force the non-loaded section to be loaded.
   4879 		 Consider .tbss sections as loaded for this purpose.  */
   4880 	      new_segment = true;
   4881 	    }
   4882 	  else if ((abfd->flags & D_PAGED) == 0)
   4883 	    {
   4884 	      /* If the file is not demand paged, which means that we
   4885 		 don't require the sections to be correctly aligned in the
   4886 		 file, then there is no other reason for a new segment.  */
   4887 	      new_segment = false;
   4888 	    }
   4889 	  else if (info != NULL
   4890 		   && info->separate_code
   4891 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   4892 	    {
   4893 	      new_segment = true;
   4894 	    }
   4895 	  else if (! writable
   4896 		   && (hdr->flags & SEC_READONLY) == 0)
   4897 	    {
   4898 	      /* We don't want to put a writable section in a read only
   4899 		 segment.  */
   4900 	      new_segment = true;
   4901 	    }
   4902 	  else
   4903 	    {
   4904 	      /* Otherwise, we can use the same segment.  */
   4905 	      new_segment = false;
   4906 	    }
   4907 
   4908 	  /* Allow interested parties a chance to override our decision.  */
   4909 	  if (last_hdr != NULL
   4910 	      && info != NULL
   4911 	      && info->callbacks->override_segment_assignment != NULL)
   4912 	    new_segment
   4913 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   4914 							      last_hdr,
   4915 							      new_segment);
   4916 
   4917 	  if (! new_segment)
   4918 	    {
   4919 	      if ((hdr->flags & SEC_READONLY) == 0)
   4920 		writable = true;
   4921 	      if ((hdr->flags & SEC_CODE) != 0)
   4922 		executable = true;
   4923 	      last_hdr = hdr;
   4924 	      /* .tbss sections effectively have zero size.  */
   4925 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   4926 	      continue;
   4927 	    }
   4928 
   4929 	  /* We need a new program segment.  We must create a new program
   4930 	     header holding all the sections from hdr_index until hdr.  */
   4931 
   4932 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   4933 	  if (m == NULL)
   4934 	    goto error_return;
   4935 
   4936 	  *pm = m;
   4937 	  pm = &m->next;
   4938 
   4939 	  if ((hdr->flags & SEC_READONLY) == 0)
   4940 	    writable = true;
   4941 	  else
   4942 	    writable = false;
   4943 
   4944 	  if ((hdr->flags & SEC_CODE) == 0)
   4945 	    executable = false;
   4946 	  else
   4947 	    executable = true;
   4948 
   4949 	  last_hdr = hdr;
   4950 	  /* .tbss sections effectively have zero size.  */
   4951 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   4952 	  hdr_index = i;
   4953 	  phdr_in_segment = false;
   4954 	}
   4955 
   4956       /* Create a final PT_LOAD program segment, but not if it's just
   4957 	 for .tbss.  */
   4958       if (last_hdr != NULL
   4959 	  && (i - hdr_index != 1
   4960 	      || !IS_TBSS (last_hdr)))
   4961 	{
   4962 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   4963 	  if (m == NULL)
   4964 	    goto error_return;
   4965 
   4966 	  *pm = m;
   4967 	  pm = &m->next;
   4968 	}
   4969 
   4970       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   4971       if (dynsec != NULL)
   4972 	{
   4973 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   4974 	  if (m == NULL)
   4975 	    goto error_return;
   4976 	  *pm = m;
   4977 	  pm = &m->next;
   4978 	}
   4979 
   4980       /* For each batch of consecutive loadable SHT_NOTE  sections,
   4981 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   4982 	 because if we link together nonloadable .note sections and
   4983 	 loadable .note sections, we will generate two .note sections
   4984 	 in the output file.  */
   4985       for (s = abfd->sections; s != NULL; s = s->next)
   4986 	{
   4987 	  if ((s->flags & SEC_LOAD) != 0
   4988 	      && elf_section_type (s) == SHT_NOTE)
   4989 	    {
   4990 	      asection *s2;
   4991 	      unsigned int alignment_power = s->alignment_power;
   4992 
   4993 	      count = 1;
   4994 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   4995 		{
   4996 		  if (s2->next->alignment_power == alignment_power
   4997 		      && (s2->next->flags & SEC_LOAD) != 0
   4998 		      && elf_section_type (s2->next) == SHT_NOTE
   4999 		      && align_power (s2->lma + s2->size / opb,
   5000 				      alignment_power)
   5001 		      == s2->next->lma)
   5002 		    count++;
   5003 		  else
   5004 		    break;
   5005 		}
   5006 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5007 	      amt += count * sizeof (asection *);
   5008 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5009 	      if (m == NULL)
   5010 		goto error_return;
   5011 	      m->next = NULL;
   5012 	      m->p_type = PT_NOTE;
   5013 	      m->count = count;
   5014 	      while (count > 1)
   5015 		{
   5016 		  m->sections[m->count - count--] = s;
   5017 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5018 		  s = s->next;
   5019 		}
   5020 	      m->sections[m->count - 1] = s;
   5021 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5022 	      *pm = m;
   5023 	      pm = &m->next;
   5024 	    }
   5025 	  if (s->flags & SEC_THREAD_LOCAL)
   5026 	    {
   5027 	      if (! tls_count)
   5028 		first_tls = s;
   5029 	      tls_count++;
   5030 	    }
   5031 	  if (first_mbind == NULL
   5032 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5033 	    first_mbind = s;
   5034 	}
   5035 
   5036       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5037       if (tls_count > 0)
   5038 	{
   5039 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5040 	  amt += tls_count * sizeof (asection *);
   5041 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5042 	  if (m == NULL)
   5043 	    goto error_return;
   5044 	  m->next = NULL;
   5045 	  m->p_type = PT_TLS;
   5046 	  m->count = tls_count;
   5047 	  /* Mandated PF_R.  */
   5048 	  m->p_flags = PF_R;
   5049 	  m->p_flags_valid = 1;
   5050 	  s = first_tls;
   5051 	  for (i = 0; i < tls_count; ++i)
   5052 	    {
   5053 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5054 		{
   5055 		  _bfd_error_handler
   5056 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5057 		  s = first_tls;
   5058 		  i = 0;
   5059 		  while (i < tls_count)
   5060 		    {
   5061 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5062 			{
   5063 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5064 			  i++;
   5065 			}
   5066 		      else
   5067 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5068 		      s = s->next;
   5069 		    }
   5070 		  bfd_set_error (bfd_error_bad_value);
   5071 		  goto error_return;
   5072 		}
   5073 	      m->sections[i] = s;
   5074 	      s = s->next;
   5075 	    }
   5076 
   5077 	  *pm = m;
   5078 	  pm = &m->next;
   5079 	}
   5080 
   5081       if (first_mbind
   5082 	  && (abfd->flags & D_PAGED) != 0
   5083 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5084 	for (s = first_mbind; s != NULL; s = s->next)
   5085 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5086 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5087 	    {
   5088 	      /* Mandated PF_R.  */
   5089 	      unsigned long p_flags = PF_R;
   5090 	      if ((s->flags & SEC_READONLY) == 0)
   5091 		p_flags |= PF_W;
   5092 	      if ((s->flags & SEC_CODE) != 0)
   5093 		p_flags |= PF_X;
   5094 
   5095 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5096 	      m = bfd_zalloc (abfd, amt);
   5097 	      if (m == NULL)
   5098 		goto error_return;
   5099 	      m->next = NULL;
   5100 	      m->p_type = (PT_GNU_MBIND_LO
   5101 			   + elf_section_data (s)->this_hdr.sh_info);
   5102 	      m->count = 1;
   5103 	      m->p_flags_valid = 1;
   5104 	      m->sections[0] = s;
   5105 	      m->p_flags = p_flags;
   5106 
   5107 	      *pm = m;
   5108 	      pm = &m->next;
   5109 	    }
   5110 
   5111       s = bfd_get_section_by_name (abfd,
   5112 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5113       if (s != NULL && s->size != 0)
   5114 	{
   5115 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5116 	  m = bfd_zalloc (abfd, amt);
   5117 	  if (m == NULL)
   5118 	    goto error_return;
   5119 	  m->next = NULL;
   5120 	  m->p_type = PT_GNU_PROPERTY;
   5121 	  m->count = 1;
   5122 	  m->p_flags_valid = 1;
   5123 	  m->sections[0] = s;
   5124 	  m->p_flags = PF_R;
   5125 	  *pm = m;
   5126 	  pm = &m->next;
   5127 	}
   5128 
   5129       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5130 	 segment.  */
   5131       eh_frame_hdr = elf_eh_frame_hdr (info);
   5132       if (eh_frame_hdr != NULL
   5133 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5134 	{
   5135 	  amt = sizeof (struct elf_segment_map);
   5136 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5137 	  if (m == NULL)
   5138 	    goto error_return;
   5139 	  m->next = NULL;
   5140 	  m->p_type = PT_GNU_EH_FRAME;
   5141 	  m->count = 1;
   5142 	  m->sections[0] = eh_frame_hdr->output_section;
   5143 
   5144 	  *pm = m;
   5145 	  pm = &m->next;
   5146 	}
   5147 
   5148       /* If there is a .sframe section, throw in a PT_GNU_SFRAME
   5149 	 segment.  */
   5150       sframe = elf_sframe (abfd);
   5151       if (sframe != NULL
   5152 	  && (sframe->output_section->flags & SEC_LOAD) != 0
   5153 	  && sframe->size != 0)
   5154 	{
   5155 	  amt = sizeof (struct elf_segment_map);
   5156 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5157 	  if (m == NULL)
   5158 	    goto error_return;
   5159 	  m->next = NULL;
   5160 	  m->p_type = PT_GNU_SFRAME;
   5161 	  m->count = 1;
   5162 	  m->sections[0] = sframe->output_section;
   5163 
   5164 	  *pm = m;
   5165 	  pm = &m->next;
   5166 	}
   5167 
   5168       if (elf_stack_flags (abfd))
   5169 	{
   5170 	  amt = sizeof (struct elf_segment_map);
   5171 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5172 	  if (m == NULL)
   5173 	    goto error_return;
   5174 	  m->next = NULL;
   5175 	  m->p_type = PT_GNU_STACK;
   5176 	  m->p_flags = elf_stack_flags (abfd);
   5177 	  m->p_align = bed->stack_align;
   5178 	  m->p_flags_valid = 1;
   5179 	  m->p_align_valid = m->p_align != 0;
   5180 	  if (info->stacksize > 0)
   5181 	    {
   5182 	      m->p_size = info->stacksize;
   5183 	      m->p_size_valid = 1;
   5184 	    }
   5185 
   5186 	  *pm = m;
   5187 	  pm = &m->next;
   5188 	}
   5189 
   5190       if (info != NULL && info->relro)
   5191 	{
   5192 	  for (m = mfirst; m != NULL; m = m->next)
   5193 	    {
   5194 	      if (m->p_type == PT_LOAD
   5195 		  && m->count != 0
   5196 		  && m->sections[0]->vma >= info->relro_start
   5197 		  && m->sections[0]->vma < info->relro_end)
   5198 		{
   5199 		  i = m->count;
   5200 		  while (--i != (unsigned) -1)
   5201 		    {
   5202 		      if (m->sections[i]->size > 0
   5203 			  && (m->sections[i]->flags & SEC_LOAD) != 0
   5204 			  && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
   5205 			break;
   5206 		    }
   5207 
   5208 		  if (i != (unsigned) -1)
   5209 		    break;
   5210 		}
   5211 	    }
   5212 
   5213 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5214 	  if (m != NULL)
   5215 	    {
   5216 	      amt = sizeof (struct elf_segment_map);
   5217 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5218 	      if (m == NULL)
   5219 		goto error_return;
   5220 	      m->next = NULL;
   5221 	      m->p_type = PT_GNU_RELRO;
   5222 	      *pm = m;
   5223 	      pm = &m->next;
   5224 	    }
   5225 	}
   5226 
   5227       free (sections);
   5228       elf_seg_map (abfd) = mfirst;
   5229     }
   5230 
   5231   if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
   5232     return false;
   5233 
   5234   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5235     ++count;
   5236   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5237 
   5238   return true;
   5239 
   5240  error_return:
   5241   free (sections);
   5242   return false;
   5243 }
   5244 
   5245 /* Sort sections by address.  */
   5246 
   5247 static int
   5248 elf_sort_sections (const void *arg1, const void *arg2)
   5249 {
   5250   const asection *sec1 = *(const asection **) arg1;
   5251   const asection *sec2 = *(const asection **) arg2;
   5252   bfd_size_type size1, size2;
   5253 
   5254   /* Sort by LMA first, since this is the address used to
   5255      place the section into a segment.  */
   5256   if (sec1->lma < sec2->lma)
   5257     return -1;
   5258   else if (sec1->lma > sec2->lma)
   5259     return 1;
   5260 
   5261   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5262      the same, and this will do nothing.  */
   5263   if (sec1->vma < sec2->vma)
   5264     return -1;
   5265   else if (sec1->vma > sec2->vma)
   5266     return 1;
   5267 
   5268   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5269 
   5270 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5271 		  && (x)->size != 0)
   5272 
   5273   if (TOEND (sec1))
   5274     {
   5275       if (!TOEND (sec2))
   5276 	return 1;
   5277     }
   5278   else if (TOEND (sec2))
   5279     return -1;
   5280 
   5281 #undef TOEND
   5282 
   5283   /* Sort by size, to put zero sized sections
   5284      before others at the same address.  */
   5285 
   5286   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5287   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5288 
   5289   if (size1 < size2)
   5290     return -1;
   5291   if (size1 > size2)
   5292     return 1;
   5293 
   5294   return sec1->target_index - sec2->target_index;
   5295 }
   5296 
   5297 /* This qsort comparison functions sorts PT_LOAD segments first and
   5298    by p_paddr, for assign_file_positions_for_load_sections.  */
   5299 
   5300 static int
   5301 elf_sort_segments (const void *arg1, const void *arg2)
   5302 {
   5303   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5304   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5305 
   5306   if (m1->p_type != m2->p_type)
   5307     {
   5308       if (m1->p_type == PT_NULL)
   5309 	return 1;
   5310       if (m2->p_type == PT_NULL)
   5311 	return -1;
   5312       return m1->p_type < m2->p_type ? -1 : 1;
   5313     }
   5314   if (m1->includes_filehdr != m2->includes_filehdr)
   5315     return m1->includes_filehdr ? -1 : 1;
   5316   if (m1->no_sort_lma != m2->no_sort_lma)
   5317     return m1->no_sort_lma ? -1 : 1;
   5318   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5319     {
   5320       bfd_vma lma1, lma2;  /* Octets.  */
   5321       lma1 = 0;
   5322       if (m1->p_paddr_valid)
   5323 	lma1 = m1->p_paddr;
   5324       else if (m1->count != 0)
   5325 	{
   5326 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5327 						  m1->sections[0]);
   5328 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5329 	}
   5330       lma2 = 0;
   5331       if (m2->p_paddr_valid)
   5332 	lma2 = m2->p_paddr;
   5333       else if (m2->count != 0)
   5334 	{
   5335 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5336 						  m2->sections[0]);
   5337 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5338 	}
   5339       if (lma1 != lma2)
   5340 	return lma1 < lma2 ? -1 : 1;
   5341     }
   5342   if (m1->idx != m2->idx)
   5343     return m1->idx < m2->idx ? -1 : 1;
   5344   return 0;
   5345 }
   5346 
   5347 /* Ian Lance Taylor writes:
   5348 
   5349    We shouldn't be using % with a negative signed number.  That's just
   5350    not good.  We have to make sure either that the number is not
   5351    negative, or that the number has an unsigned type.  When the types
   5352    are all the same size they wind up as unsigned.  When file_ptr is a
   5353    larger signed type, the arithmetic winds up as signed long long,
   5354    which is wrong.
   5355 
   5356    What we're trying to say here is something like ``increase OFF by
   5357    the least amount that will cause it to be equal to the VMA modulo
   5358    the page size.''  */
   5359 /* In other words, something like:
   5360 
   5361    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5362    off_offset = off % bed->maxpagesize;
   5363    if (vma_offset < off_offset)
   5364      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5365    else
   5366      adjustment = vma_offset - off_offset;
   5367 
   5368    which can be collapsed into the expression below.  */
   5369 
   5370 static file_ptr
   5371 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5372 {
   5373   /* PR binutils/16199: Handle an alignment of zero.  */
   5374   if (maxpagesize == 0)
   5375     maxpagesize = 1;
   5376   return ((vma - off) % maxpagesize);
   5377 }
   5378 
   5379 static void
   5380 print_segment_map (const struct elf_segment_map *m)
   5381 {
   5382   unsigned int j;
   5383   const char *pt = get_segment_type (m->p_type);
   5384   char buf[32];
   5385 
   5386   if (pt == NULL)
   5387     {
   5388       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5389 	sprintf (buf, "LOPROC+%7.7x",
   5390 		 (unsigned int) (m->p_type - PT_LOPROC));
   5391       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5392 	sprintf (buf, "LOOS+%7.7x",
   5393 		 (unsigned int) (m->p_type - PT_LOOS));
   5394       else
   5395 	snprintf (buf, sizeof (buf), "%8.8x",
   5396 		  (unsigned int) m->p_type);
   5397       pt = buf;
   5398     }
   5399   fflush (stdout);
   5400   fprintf (stderr, "%s:", pt);
   5401   for (j = 0; j < m->count; j++)
   5402     fprintf (stderr, " %s", m->sections [j]->name);
   5403   putc ('\n',stderr);
   5404   fflush (stderr);
   5405 }
   5406 
   5407 static bool
   5408 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
   5409 {
   5410   void *buf;
   5411   bool ret;
   5412 
   5413   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   5414     return false;
   5415   buf = bfd_zmalloc (len);
   5416   if (buf == NULL)
   5417     return false;
   5418   ret = bfd_bwrite (buf, len, abfd) == len;
   5419   free (buf);
   5420   return ret;
   5421 }
   5422 
   5423 /* Assign file positions to the sections based on the mapping from
   5424    sections to segments.  This function also sets up some fields in
   5425    the file header.  */
   5426 
   5427 static bool
   5428 assign_file_positions_for_load_sections (bfd *abfd,
   5429 					 struct bfd_link_info *link_info)
   5430 {
   5431   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5432   struct elf_segment_map *m;
   5433   struct elf_segment_map *phdr_load_seg;
   5434   Elf_Internal_Phdr *phdrs;
   5435   Elf_Internal_Phdr *p;
   5436   file_ptr off;  /* Octets.  */
   5437   bfd_size_type maxpagesize;
   5438   unsigned int alloc, actual;
   5439   unsigned int i, j;
   5440   struct elf_segment_map **sorted_seg_map;
   5441   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5442 
   5443   if (link_info == NULL
   5444       && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5445     return false;
   5446 
   5447   alloc = 0;
   5448   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5449     m->idx = alloc++;
   5450 
   5451   if (alloc)
   5452     {
   5453       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5454       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5455     }
   5456   else
   5457     {
   5458       /* PR binutils/12467.  */
   5459       elf_elfheader (abfd)->e_phoff = 0;
   5460       elf_elfheader (abfd)->e_phentsize = 0;
   5461     }
   5462 
   5463   elf_elfheader (abfd)->e_phnum = alloc;
   5464 
   5465   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5466     {
   5467       actual = alloc;
   5468       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5469     }
   5470   else
   5471     {
   5472       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   5473       BFD_ASSERT (elf_program_header_size (abfd)
   5474 		  == actual * bed->s->sizeof_phdr);
   5475       BFD_ASSERT (actual >= alloc);
   5476     }
   5477 
   5478   if (alloc == 0)
   5479     {
   5480       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5481       return true;
   5482     }
   5483 
   5484   /* We're writing the size in elf_program_header_size (abfd),
   5485      see assign_file_positions_except_relocs, so make sure we have
   5486      that amount allocated, with trailing space cleared.
   5487      The variable alloc contains the computed need, while
   5488      elf_program_header_size (abfd) contains the size used for the
   5489      layout.
   5490      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5491      where the layout is forced to according to a larger size in the
   5492      last iterations for the testcase ld-elf/header.  */
   5493   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   5494 			     + alloc * sizeof (*sorted_seg_map)));
   5495   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   5496   elf_tdata (abfd)->phdr = phdrs;
   5497   if (phdrs == NULL)
   5498     return false;
   5499 
   5500   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   5501     {
   5502       sorted_seg_map[j] = m;
   5503       /* If elf_segment_map is not from map_sections_to_segments, the
   5504 	 sections may not be correctly ordered.  NOTE: sorting should
   5505 	 not be done to the PT_NOTE section of a corefile, which may
   5506 	 contain several pseudo-sections artificially created by bfd.
   5507 	 Sorting these pseudo-sections breaks things badly.  */
   5508       if (m->count > 1
   5509 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5510 	       && m->p_type == PT_NOTE))
   5511 	{
   5512 	  for (i = 0; i < m->count; i++)
   5513 	    m->sections[i]->target_index = i;
   5514 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5515 		 elf_sort_sections);
   5516 	}
   5517     }
   5518   if (alloc > 1)
   5519     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   5520 	   elf_sort_segments);
   5521 
   5522   maxpagesize = 1;
   5523   if ((abfd->flags & D_PAGED) != 0)
   5524     {
   5525       if (link_info != NULL)
   5526 	maxpagesize = link_info->maxpagesize;
   5527       else
   5528 	maxpagesize = bed->maxpagesize;
   5529     }
   5530 
   5531   /* Sections must map to file offsets past the ELF file header.  */
   5532   off = bed->s->sizeof_ehdr;
   5533   /* And if one of the PT_LOAD headers doesn't include the program
   5534      headers then we'll be mapping program headers in the usual
   5535      position after the ELF file header.  */
   5536   phdr_load_seg = NULL;
   5537   for (j = 0; j < alloc; j++)
   5538     {
   5539       m = sorted_seg_map[j];
   5540       if (m->p_type != PT_LOAD)
   5541 	break;
   5542       if (m->includes_phdrs)
   5543 	{
   5544 	  phdr_load_seg = m;
   5545 	  break;
   5546 	}
   5547     }
   5548   if (phdr_load_seg == NULL)
   5549     off += actual * bed->s->sizeof_phdr;
   5550 
   5551   for (j = 0; j < alloc; j++)
   5552     {
   5553       asection **secpp;
   5554       bfd_vma off_adjust;  /* Octets.  */
   5555       bool no_contents;
   5556       bfd_size_type p_align;
   5557       bool p_align_p;
   5558 
   5559       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   5560 	 number of sections with contents contributing to both p_filesz
   5561 	 and p_memsz, followed by a number of sections with no contents
   5562 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   5563 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   5564       m = sorted_seg_map[j];
   5565       p = phdrs + m->idx;
   5566       p->p_type = m->p_type;
   5567       p->p_flags = m->p_flags;
   5568       p_align = bed->p_align;
   5569       p_align_p = false;
   5570 
   5571       if (m->count == 0)
   5572 	p->p_vaddr = m->p_vaddr_offset * opb;
   5573       else
   5574 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   5575 
   5576       if (m->p_paddr_valid)
   5577 	p->p_paddr = m->p_paddr;
   5578       else if (m->count == 0)
   5579 	p->p_paddr = 0;
   5580       else
   5581 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   5582 
   5583       if (p->p_type == PT_LOAD
   5584 	  && (abfd->flags & D_PAGED) != 0)
   5585 	{
   5586 	  /* p_align in demand paged PT_LOAD segments effectively stores
   5587 	     the maximum page size.  When copying an executable with
   5588 	     objcopy, we set m->p_align from the input file.  Use this
   5589 	     value for maxpagesize rather than bed->maxpagesize, which
   5590 	     may be different.  Note that we use maxpagesize for PT_TLS
   5591 	     segment alignment later in this function, so we are relying
   5592 	     on at least one PT_LOAD segment appearing before a PT_TLS
   5593 	     segment.  */
   5594 	  if (m->p_align_valid)
   5595 	    maxpagesize = m->p_align;
   5596 	  else if (p_align != 0
   5597 		   && (link_info == NULL
   5598 		       || !link_info->maxpagesize_is_set))
   5599 	    /* Set p_align to the default p_align value while laying
   5600 	       out segments aligning to the maximum page size or the
   5601 	       largest section alignment.  The run-time loader can
   5602 	       align segments to the default p_align value or the
   5603 	       maximum page size, depending on system page size.  */
   5604 	    p_align_p = true;
   5605 
   5606 	  p->p_align = maxpagesize;
   5607 	}
   5608       else if (m->p_align_valid)
   5609 	p->p_align = m->p_align;
   5610       else if (m->count == 0)
   5611 	p->p_align = 1 << bed->s->log_file_align;
   5612 
   5613       if (m == phdr_load_seg)
   5614 	{
   5615 	  if (!m->includes_filehdr)
   5616 	    p->p_offset = off;
   5617 	  off += actual * bed->s->sizeof_phdr;
   5618 	}
   5619 
   5620       no_contents = false;
   5621       off_adjust = 0;
   5622       if (p->p_type == PT_LOAD
   5623 	  && m->count > 0)
   5624 	{
   5625 	  bfd_size_type align;  /* Bytes.  */
   5626 	  unsigned int align_power = 0;
   5627 
   5628 	  if (m->p_align_valid)
   5629 	    align = p->p_align;
   5630 	  else
   5631 	    {
   5632 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5633 		{
   5634 		  unsigned int secalign;
   5635 
   5636 		  secalign = bfd_section_alignment (*secpp);
   5637 		  if (secalign > align_power)
   5638 		    align_power = secalign;
   5639 		}
   5640 	      align = (bfd_size_type) 1 << align_power;
   5641 	      if (align < maxpagesize)
   5642 		{
   5643 		  /* If a section requires alignment higher than the
   5644 		     default p_align value, don't set p_align to the
   5645 		     default p_align value.  */
   5646 		  if (align > p_align)
   5647 		    p_align_p = false;
   5648 		  align = maxpagesize;
   5649 		}
   5650 	      else
   5651 		{
   5652 		  /* If a section requires alignment higher than the
   5653 		     maximum page size, set p_align to the section
   5654 		     alignment.  */
   5655 		  p_align_p = true;
   5656 		  p_align = align;
   5657 		}
   5658 	    }
   5659 
   5660 	  for (i = 0; i < m->count; i++)
   5661 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   5662 	      /* If we aren't making room for this section, then
   5663 		 it must be SHT_NOBITS regardless of what we've
   5664 		 set via struct bfd_elf_special_section.  */
   5665 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   5666 
   5667 	  /* Find out whether this segment contains any loadable
   5668 	     sections.  */
   5669 	  no_contents = true;
   5670 	  for (i = 0; i < m->count; i++)
   5671 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   5672 	      {
   5673 		no_contents = false;
   5674 		break;
   5675 	      }
   5676 
   5677 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   5678 
   5679 	  /* Broken hardware and/or kernel require that files do not
   5680 	     map the same page with different permissions on some hppa
   5681 	     processors.  */
   5682 	  if (j != 0
   5683 	      && (abfd->flags & D_PAGED) != 0
   5684 	      && bed->no_page_alias
   5685 	      && (off & (maxpagesize - 1)) != 0
   5686 	      && ((off & -maxpagesize)
   5687 		  == ((off + off_adjust) & -maxpagesize)))
   5688 	    off_adjust += maxpagesize;
   5689 	  off += off_adjust;
   5690 	  if (no_contents)
   5691 	    {
   5692 	      /* We shouldn't need to align the segment on disk since
   5693 		 the segment doesn't need file space, but the gABI
   5694 		 arguably requires the alignment and glibc ld.so
   5695 		 checks it.  So to comply with the alignment
   5696 		 requirement but not waste file space, we adjust
   5697 		 p_offset for just this segment.  (OFF_ADJUST is
   5698 		 subtracted from OFF later.)  This may put p_offset
   5699 		 past the end of file, but that shouldn't matter.  */
   5700 	    }
   5701 	  else
   5702 	    off_adjust = 0;
   5703 	}
   5704       /* Make sure the .dynamic section is the first section in the
   5705 	 PT_DYNAMIC segment.  */
   5706       else if (p->p_type == PT_DYNAMIC
   5707 	       && m->count > 1
   5708 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   5709 	{
   5710 	  _bfd_error_handler
   5711 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   5712 	       " is not the .dynamic section"),
   5713 	     abfd);
   5714 	  bfd_set_error (bfd_error_bad_value);
   5715 	  return false;
   5716 	}
   5717       /* Set the note section type to SHT_NOTE.  */
   5718       else if (p->p_type == PT_NOTE)
   5719 	for (i = 0; i < m->count; i++)
   5720 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   5721 
   5722       if (m->includes_filehdr)
   5723 	{
   5724 	  if (!m->p_flags_valid)
   5725 	    p->p_flags |= PF_R;
   5726 	  p->p_filesz = bed->s->sizeof_ehdr;
   5727 	  p->p_memsz = bed->s->sizeof_ehdr;
   5728 	  if (p->p_type == PT_LOAD)
   5729 	    {
   5730 	      if (m->count > 0)
   5731 		{
   5732 		  if (p->p_vaddr < (bfd_vma) off
   5733 		      || (!m->p_paddr_valid
   5734 			  && p->p_paddr < (bfd_vma) off))
   5735 		    {
   5736 		      _bfd_error_handler
   5737 			(_("%pB: not enough room for program headers,"
   5738 			   " try linking with -N"),
   5739 			 abfd);
   5740 		      bfd_set_error (bfd_error_bad_value);
   5741 		      return false;
   5742 		    }
   5743 		  p->p_vaddr -= off;
   5744 		  if (!m->p_paddr_valid)
   5745 		    p->p_paddr -= off;
   5746 		}
   5747 	    }
   5748 	  else if (sorted_seg_map[0]->includes_filehdr)
   5749 	    {
   5750 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   5751 	      p->p_vaddr = filehdr->p_vaddr;
   5752 	      if (!m->p_paddr_valid)
   5753 		p->p_paddr = filehdr->p_paddr;
   5754 	    }
   5755 	}
   5756 
   5757       if (m->includes_phdrs)
   5758 	{
   5759 	  if (!m->p_flags_valid)
   5760 	    p->p_flags |= PF_R;
   5761 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   5762 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   5763 	  if (!m->includes_filehdr)
   5764 	    {
   5765 	      if (p->p_type == PT_LOAD)
   5766 		{
   5767 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   5768 		  if (m->count > 0)
   5769 		    {
   5770 		      p->p_vaddr -= off - p->p_offset;
   5771 		      if (!m->p_paddr_valid)
   5772 			p->p_paddr -= off - p->p_offset;
   5773 		    }
   5774 		}
   5775 	      else if (phdr_load_seg != NULL)
   5776 		{
   5777 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   5778 		  bfd_vma phdr_off = 0;  /* Octets.  */
   5779 		  if (phdr_load_seg->includes_filehdr)
   5780 		    phdr_off = bed->s->sizeof_ehdr;
   5781 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   5782 		  if (!m->p_paddr_valid)
   5783 		    p->p_paddr = phdr->p_paddr + phdr_off;
   5784 		  p->p_offset = phdr->p_offset + phdr_off;
   5785 		}
   5786 	      else
   5787 		p->p_offset = bed->s->sizeof_ehdr;
   5788 	    }
   5789 	}
   5790 
   5791       if (p->p_type == PT_LOAD
   5792 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   5793 	{
   5794 	  if (!m->includes_filehdr && !m->includes_phdrs)
   5795 	    {
   5796 	      p->p_offset = off;
   5797 	      if (no_contents)
   5798 		{
   5799 		  /* Put meaningless p_offset for PT_LOAD segments
   5800 		     without file contents somewhere within the first
   5801 		     page, in an attempt to not point past EOF.  */
   5802 		  bfd_size_type align = maxpagesize;
   5803 		  if (align < p->p_align)
   5804 		    align = p->p_align;
   5805 		  if (align < 1)
   5806 		    align = 1;
   5807 		  p->p_offset = off % align;
   5808 		}
   5809 	    }
   5810 	  else
   5811 	    {
   5812 	      file_ptr adjust;  /* Octets.  */
   5813 
   5814 	      adjust = off - (p->p_offset + p->p_filesz);
   5815 	      if (!no_contents)
   5816 		p->p_filesz += adjust;
   5817 	      p->p_memsz += adjust;
   5818 	    }
   5819 	}
   5820 
   5821       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   5822 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   5823 	 core files, for sections in PT_NOTE segments.
   5824 	 assign_file_positions_for_non_load_sections will set filepos
   5825 	 for other sections and update p_filesz for other segments.  */
   5826       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5827 	{
   5828 	  asection *sec;
   5829 	  bfd_size_type align;
   5830 	  Elf_Internal_Shdr *this_hdr;
   5831 
   5832 	  sec = *secpp;
   5833 	  this_hdr = &elf_section_data (sec)->this_hdr;
   5834 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   5835 
   5836 	  if ((p->p_type == PT_LOAD
   5837 	       || p->p_type == PT_TLS)
   5838 	      && (this_hdr->sh_type != SHT_NOBITS
   5839 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   5840 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   5841 			  || p->p_type == PT_TLS))))
   5842 	    {
   5843 	      bfd_vma p_start = p->p_paddr;		/* Octets.  */
   5844 	      bfd_vma p_end = p_start + p->p_memsz;	/* Octets.  */
   5845 	      bfd_vma s_start = sec->lma * opb;		/* Octets.  */
   5846 	      bfd_vma adjust = s_start - p_end;		/* Octets.  */
   5847 
   5848 	      if (adjust != 0
   5849 		  && (s_start < p_end
   5850 		      || p_end < p_start))
   5851 		{
   5852 		  _bfd_error_handler
   5853 		    /* xgettext:c-format */
   5854 		    (_("%pB: section %pA lma %#" PRIx64
   5855 		       " adjusted to %#" PRIx64),
   5856 		     abfd, sec, (uint64_t) s_start / opb,
   5857 		     (uint64_t) p_end / opb);
   5858 		  adjust = 0;
   5859 		  sec->lma = p_end / opb;
   5860 		}
   5861 	      p->p_memsz += adjust;
   5862 
   5863 	      if (p->p_type == PT_LOAD)
   5864 		{
   5865 		  if (this_hdr->sh_type != SHT_NOBITS)
   5866 		    {
   5867 		      off_adjust = 0;
   5868 		      if (p->p_filesz + adjust < p->p_memsz)
   5869 			{
   5870 			  /* We have a PROGBITS section following NOBITS ones.
   5871 			     Allocate file space for the NOBITS section(s) and
   5872 			     zero it.  */
   5873 			  adjust = p->p_memsz - p->p_filesz;
   5874 			  if (!write_zeros (abfd, off, adjust))
   5875 			    return false;
   5876 			}
   5877 		    }
   5878 		  /* We only adjust sh_offset in SHT_NOBITS sections
   5879 		     as would seem proper for their address when the
   5880 		     section is first in the segment.  sh_offset
   5881 		     doesn't really have any significance for
   5882 		     SHT_NOBITS anyway, apart from a notional position
   5883 		     relative to other sections.  Historically we
   5884 		     didn't bother with adjusting sh_offset and some
   5885 		     programs depend on it not being adjusted.  See
   5886 		     pr12921 and pr25662.  */
   5887 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   5888 		    {
   5889 		      off += adjust;
   5890 		      if (this_hdr->sh_type == SHT_NOBITS)
   5891 			off_adjust += adjust;
   5892 		    }
   5893 		}
   5894 	      if (this_hdr->sh_type != SHT_NOBITS)
   5895 		p->p_filesz += adjust;
   5896 	    }
   5897 
   5898 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   5899 	    {
   5900 	      /* The section at i == 0 is the one that actually contains
   5901 		 everything.  */
   5902 	      if (i == 0)
   5903 		{
   5904 		  this_hdr->sh_offset = sec->filepos = off;
   5905 		  off += this_hdr->sh_size;
   5906 		  p->p_filesz = this_hdr->sh_size;
   5907 		  p->p_memsz = 0;
   5908 		  p->p_align = 1;
   5909 		}
   5910 	      else
   5911 		{
   5912 		  /* The rest are fake sections that shouldn't be written.  */
   5913 		  sec->filepos = 0;
   5914 		  sec->size = 0;
   5915 		  sec->flags = 0;
   5916 		  continue;
   5917 		}
   5918 	    }
   5919 	  else
   5920 	    {
   5921 	      if (p->p_type == PT_LOAD)
   5922 		{
   5923 		  this_hdr->sh_offset = sec->filepos = off;
   5924 		  if (this_hdr->sh_type != SHT_NOBITS)
   5925 		    off += this_hdr->sh_size;
   5926 		}
   5927 	      else if (this_hdr->sh_type == SHT_NOBITS
   5928 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   5929 		       && this_hdr->sh_offset == 0)
   5930 		{
   5931 		  /* This is a .tbss section that didn't get a PT_LOAD.
   5932 		     (See _bfd_elf_map_sections_to_segments "Create a
   5933 		     final PT_LOAD".)  Set sh_offset to the value it
   5934 		     would have if we had created a zero p_filesz and
   5935 		     p_memsz PT_LOAD header for the section.  This
   5936 		     also makes the PT_TLS header have the same
   5937 		     p_offset value.  */
   5938 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   5939 							  off, align);
   5940 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   5941 		}
   5942 
   5943 	      if (this_hdr->sh_type != SHT_NOBITS)
   5944 		{
   5945 		  p->p_filesz += this_hdr->sh_size;
   5946 		  /* A load section without SHF_ALLOC is something like
   5947 		     a note section in a PT_NOTE segment.  These take
   5948 		     file space but are not loaded into memory.  */
   5949 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5950 		    p->p_memsz += this_hdr->sh_size;
   5951 		}
   5952 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5953 		{
   5954 		  if (p->p_type == PT_TLS)
   5955 		    p->p_memsz += this_hdr->sh_size;
   5956 
   5957 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   5958 		     normal segments.  */
   5959 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   5960 		    p->p_memsz += this_hdr->sh_size;
   5961 		}
   5962 
   5963 	      if (align > p->p_align
   5964 		  && !m->p_align_valid
   5965 		  && (p->p_type != PT_LOAD
   5966 		      || (abfd->flags & D_PAGED) == 0))
   5967 		p->p_align = align;
   5968 	    }
   5969 
   5970 	  if (!m->p_flags_valid)
   5971 	    {
   5972 	      p->p_flags |= PF_R;
   5973 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   5974 		p->p_flags |= PF_X;
   5975 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   5976 		p->p_flags |= PF_W;
   5977 	    }
   5978 	}
   5979 
   5980       off -= off_adjust;
   5981 
   5982       /* PR ld/20815 - Check that the program header segment, if
   5983 	 present, will be loaded into memory.  */
   5984       if (p->p_type == PT_PHDR
   5985 	  && phdr_load_seg == NULL
   5986 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   5987 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   5988 	{
   5989 	  /* The fix for this error is usually to edit the linker script being
   5990 	     used and set up the program headers manually.  Either that or
   5991 	     leave room for the headers at the start of the SECTIONS.  */
   5992 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   5993 				" by LOAD segment"),
   5994 			      abfd);
   5995 	  if (link_info == NULL)
   5996 	    return false;
   5997 	  /* Arrange for the linker to exit with an error, deleting
   5998 	     the output file unless --noinhibit-exec is given.  */
   5999 	  link_info->callbacks->info ("%X");
   6000 	}
   6001 
   6002       /* Check that all sections are in a PT_LOAD segment.
   6003 	 Don't check funky gdb generated core files.  */
   6004       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6005 	{
   6006 	  bool check_vma = true;
   6007 
   6008 	  for (i = 1; i < m->count; i++)
   6009 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6010 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6011 				       ->this_hdr), p) != 0
   6012 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6013 				       ->this_hdr), p) != 0)
   6014 	      {
   6015 		/* Looks like we have overlays packed into the segment.  */
   6016 		check_vma = false;
   6017 		break;
   6018 	      }
   6019 
   6020 	  for (i = 0; i < m->count; i++)
   6021 	    {
   6022 	      Elf_Internal_Shdr *this_hdr;
   6023 	      asection *sec;
   6024 
   6025 	      sec = m->sections[i];
   6026 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6027 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6028 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6029 		{
   6030 		  _bfd_error_handler
   6031 		    /* xgettext:c-format */
   6032 		    (_("%pB: section `%pA' can't be allocated in segment %d"),
   6033 		     abfd, sec, j);
   6034 		  print_segment_map (m);
   6035 		}
   6036 	    }
   6037 
   6038 	  if (p_align_p)
   6039 	    p->p_align = p_align;
   6040 	}
   6041     }
   6042 
   6043   elf_next_file_pos (abfd) = off;
   6044 
   6045   if (link_info != NULL
   6046       && phdr_load_seg != NULL
   6047       && phdr_load_seg->includes_filehdr)
   6048     {
   6049       /* There is a segment that contains both the file headers and the
   6050 	 program headers, so provide a symbol __ehdr_start pointing there.
   6051 	 A program can use this to examine itself robustly.  */
   6052 
   6053       struct elf_link_hash_entry *hash
   6054 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   6055 				false, false, true);
   6056       /* If the symbol was referenced and not defined, define it.  */
   6057       if (hash != NULL
   6058 	  && (hash->root.type == bfd_link_hash_new
   6059 	      || hash->root.type == bfd_link_hash_undefined
   6060 	      || hash->root.type == bfd_link_hash_undefweak
   6061 	      || hash->root.type == bfd_link_hash_common))
   6062 	{
   6063 	  asection *s = NULL;
   6064 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6065 
   6066 	  if (phdr_load_seg->count != 0)
   6067 	    /* The segment contains sections, so use the first one.  */
   6068 	    s = phdr_load_seg->sections[0];
   6069 	  else
   6070 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6071 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6072 	      if (m->p_type == PT_LOAD && m->count != 0)
   6073 		{
   6074 		  s = m->sections[0];
   6075 		  break;
   6076 		}
   6077 
   6078 	  if (s != NULL)
   6079 	    {
   6080 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6081 	      hash->root.u.def.section = s;
   6082 	    }
   6083 	  else
   6084 	    {
   6085 	      hash->root.u.def.value = filehdr_vaddr;
   6086 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6087 	    }
   6088 
   6089 	  hash->root.type = bfd_link_hash_defined;
   6090 	  hash->def_regular = 1;
   6091 	  hash->non_elf = 0;
   6092 	}
   6093     }
   6094 
   6095   return true;
   6096 }
   6097 
   6098 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6099    is no defined method for detecting such files, so we have to
   6100    use heuristics instead.  */
   6101 
   6102 bool
   6103 is_debuginfo_file (bfd *abfd)
   6104 {
   6105   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6106     return false;
   6107 
   6108   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6109   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6110   Elf_Internal_Shdr **headerp;
   6111 
   6112   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6113     {
   6114       Elf_Internal_Shdr *header = * headerp;
   6115 
   6116       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6117 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6118       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6119 	  && header->sh_type != SHT_NOBITS
   6120 	  && header->sh_type != SHT_NOTE)
   6121 	return false;
   6122     }
   6123 
   6124   return true;
   6125 }
   6126 
   6127 /* Assign file positions for other sections, except for compressed debug
   6128    and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
   6129 
   6130 static bool
   6131 assign_file_positions_for_non_load_sections (bfd *abfd,
   6132 					     struct bfd_link_info *link_info)
   6133 {
   6134   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6135   Elf_Internal_Shdr **i_shdrpp;
   6136   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6137   Elf_Internal_Phdr *phdrs;
   6138   Elf_Internal_Phdr *p;
   6139   struct elf_segment_map *m;
   6140   file_ptr off;
   6141   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6142   bfd_vma maxpagesize;
   6143 
   6144   if (link_info != NULL)
   6145     maxpagesize = link_info->maxpagesize;
   6146   else
   6147     maxpagesize = bed->maxpagesize;
   6148   i_shdrpp = elf_elfsections (abfd);
   6149   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6150   off = elf_next_file_pos (abfd);
   6151   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6152     {
   6153       Elf_Internal_Shdr *hdr;
   6154       bfd_vma align;
   6155 
   6156       hdr = *hdrpp;
   6157       if (hdr->bfd_section != NULL
   6158 	  && (hdr->bfd_section->filepos != 0
   6159 	      || (hdr->sh_type == SHT_NOBITS
   6160 		  && hdr->contents == NULL)))
   6161 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6162       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6163 	{
   6164 	  if (hdr->sh_size != 0
   6165 	      /* PR 24717 - debuginfo files are known to be not strictly
   6166 		 compliant with the ELF standard.  In particular they often
   6167 		 have .note.gnu.property sections that are outside of any
   6168 		 loadable segment.  This is not a problem for such files,
   6169 		 so do not warn about them.  */
   6170 	      && ! is_debuginfo_file (abfd))
   6171 	    _bfd_error_handler
   6172 	      /* xgettext:c-format */
   6173 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6174 	       abfd,
   6175 	       (hdr->bfd_section == NULL
   6176 		? "*unknown*"
   6177 		: hdr->bfd_section->name));
   6178 	  /* We don't need to page align empty sections.  */
   6179 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6180 	    align = maxpagesize;
   6181 	  else
   6182 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6183 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6184 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   6185 							   false);
   6186 	}
   6187       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6188 		&& hdr->bfd_section == NULL)
   6189 	       /* We don't know the offset of these sections yet:
   6190 		  their size has not been decided.  */
   6191 	       || (abfd->is_linker_output
   6192 		   && hdr->bfd_section != NULL
   6193 		   && (hdr->sh_name == -1u
   6194 		       || bfd_section_is_ctf (hdr->bfd_section)))
   6195 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6196 	       || (elf_symtab_shndx_list (abfd) != NULL
   6197 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6198 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6199 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6200 	hdr->sh_offset = -1;
   6201       else
   6202 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6203     }
   6204   elf_next_file_pos (abfd) = off;
   6205 
   6206   /* Now that we have set the section file positions, we can set up
   6207      the file positions for the non PT_LOAD segments.  */
   6208   phdrs = elf_tdata (abfd)->phdr;
   6209   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6210     {
   6211       if (p->p_type == PT_GNU_RELRO)
   6212 	{
   6213 	  bfd_vma start, end;  /* Bytes.  */
   6214 	  bool ok;
   6215 
   6216 	  if (link_info != NULL)
   6217 	    {
   6218 	      /* During linking the range of the RELRO segment is passed
   6219 		 in link_info.  Note that there may be padding between
   6220 		 relro_start and the first RELRO section.  */
   6221 	      start = link_info->relro_start;
   6222 	      end = link_info->relro_end;
   6223 	    }
   6224 	  else if (m->count != 0)
   6225 	    {
   6226 	      if (!m->p_size_valid)
   6227 		abort ();
   6228 	      start = m->sections[0]->vma;
   6229 	      end = start + m->p_size / opb;
   6230 	    }
   6231 	  else
   6232 	    {
   6233 	      start = 0;
   6234 	      end = 0;
   6235 	    }
   6236 
   6237 	  ok = false;
   6238 	  if (start < end)
   6239 	    {
   6240 	      struct elf_segment_map *lm;
   6241 	      const Elf_Internal_Phdr *lp;
   6242 	      unsigned int i;
   6243 
   6244 	      /* Find a LOAD segment containing a section in the RELRO
   6245 		 segment.  */
   6246 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6247 		   lm != NULL;
   6248 		   lm = lm->next, lp++)
   6249 		{
   6250 		  if (lp->p_type == PT_LOAD
   6251 		      && lm->count != 0
   6252 		      && (lm->sections[lm->count - 1]->vma
   6253 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6254 			     ? lm->sections[lm->count - 1]->size / opb
   6255 			     : 0)) > start
   6256 		      && lm->sections[0]->vma < end)
   6257 		    break;
   6258 		}
   6259 
   6260 	      if (lm != NULL)
   6261 		{
   6262 		  /* Find the section starting the RELRO segment.  */
   6263 		  for (i = 0; i < lm->count; i++)
   6264 		    {
   6265 		      asection *s = lm->sections[i];
   6266 		      if (s->vma >= start
   6267 			  && s->vma < end
   6268 			  && s->size != 0)
   6269 			break;
   6270 		    }
   6271 
   6272 		  if (i < lm->count)
   6273 		    {
   6274 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6275 		      p->p_paddr = lm->sections[i]->lma * opb;
   6276 		      p->p_offset = lm->sections[i]->filepos;
   6277 		      p->p_memsz = end * opb - p->p_vaddr;
   6278 		      p->p_filesz = p->p_memsz;
   6279 
   6280 		      /* The RELRO segment typically ends a few bytes
   6281 			 into .got.plt but other layouts are possible.
   6282 			 In cases where the end does not match any
   6283 			 loaded section (for instance is in file
   6284 			 padding), trim p_filesz back to correspond to
   6285 			 the end of loaded section contents.  */
   6286 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6287 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6288 
   6289 		      /* Preserve the alignment and flags if they are
   6290 			 valid.  The gold linker generates RW/4 for
   6291 			 the PT_GNU_RELRO section.  It is better for
   6292 			 objcopy/strip to honor these attributes
   6293 			 otherwise gdb will choke when using separate
   6294 			 debug files.  */
   6295 		      if (!m->p_align_valid)
   6296 			p->p_align = 1;
   6297 		      if (!m->p_flags_valid)
   6298 			p->p_flags = PF_R;
   6299 		      ok = true;
   6300 		    }
   6301 		}
   6302 	    }
   6303 
   6304 	  if (!ok)
   6305 	    {
   6306 	      if (link_info != NULL)
   6307 		_bfd_error_handler
   6308 		  (_("%pB: warning: unable to allocate any sections"
   6309 		     " to PT_GNU_RELRO segment"),
   6310 		   abfd);
   6311 	      memset (p, 0, sizeof *p);
   6312 	    }
   6313 	}
   6314       else if (p->p_type == PT_GNU_STACK)
   6315 	{
   6316 	  if (m->p_size_valid)
   6317 	    p->p_memsz = m->p_size;
   6318 	}
   6319       else if (m->count != 0)
   6320 	{
   6321 	  unsigned int i;
   6322 
   6323 	  if (p->p_type != PT_LOAD
   6324 	      && (p->p_type != PT_NOTE
   6325 		  || bfd_get_format (abfd) != bfd_core))
   6326 	    {
   6327 	      /* A user specified segment layout may include a PHDR
   6328 		 segment that overlaps with a LOAD segment...  */
   6329 	      if (p->p_type == PT_PHDR)
   6330 		{
   6331 		  m->count = 0;
   6332 		  continue;
   6333 		}
   6334 
   6335 	      if (m->includes_filehdr || m->includes_phdrs)
   6336 		{
   6337 		  /* PR 17512: file: 2195325e.  */
   6338 		  _bfd_error_handler
   6339 		    (_("%pB: error: non-load segment %d includes file header "
   6340 		       "and/or program header"),
   6341 		     abfd, (int) (p - phdrs));
   6342 		  return false;
   6343 		}
   6344 
   6345 	      p->p_filesz = 0;
   6346 	      p->p_offset = m->sections[0]->filepos;
   6347 	      for (i = m->count; i-- != 0;)
   6348 		{
   6349 		  asection *sect = m->sections[i];
   6350 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6351 		  if (hdr->sh_type != SHT_NOBITS)
   6352 		    {
   6353 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6354 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6355 			 should be the same as p_filesz.  */
   6356 		      if (p->p_type == PT_NOTE
   6357 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6358 			p->p_memsz = p->p_filesz;
   6359 		      break;
   6360 		    }
   6361 		}
   6362 	    }
   6363 	}
   6364     }
   6365 
   6366   return true;
   6367 }
   6368 
   6369 static elf_section_list *
   6370 find_section_in_list (unsigned int i, elf_section_list * list)
   6371 {
   6372   for (;list != NULL; list = list->next)
   6373     if (list->ndx == i)
   6374       break;
   6375   return list;
   6376 }
   6377 
   6378 /* Work out the file positions of all the sections.  This is called by
   6379    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6380    VMAs must be known before this is called.
   6381 
   6382    Reloc sections come in two flavours: Those processed specially as
   6383    "side-channel" data attached to a section to which they apply, and
   6384    those that bfd doesn't process as relocations.  The latter sort are
   6385    stored in a normal bfd section by bfd_section_from_shdr.  We don't
   6386    consider the former sort here, unless they form part of the loadable
   6387    image.  Reloc sections not assigned here (and compressed debugging
   6388    sections and CTF sections which nothing else in the file can rely
   6389    upon) will be handled later by assign_file_positions_for_relocs.
   6390 
   6391    We also don't set the positions of the .symtab and .strtab here.  */
   6392 
   6393 static bool
   6394 assign_file_positions_except_relocs (bfd *abfd,
   6395 				     struct bfd_link_info *link_info)
   6396 {
   6397   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6398   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6399   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6400   unsigned int alloc;
   6401 
   6402   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6403       && bfd_get_format (abfd) != bfd_core)
   6404     {
   6405       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6406       unsigned int num_sec = elf_numsections (abfd);
   6407       Elf_Internal_Shdr **hdrpp;
   6408       unsigned int i;
   6409       file_ptr off;
   6410 
   6411       /* Start after the ELF header.  */
   6412       off = i_ehdrp->e_ehsize;
   6413 
   6414       /* We are not creating an executable, which means that we are
   6415 	 not creating a program header, and that the actual order of
   6416 	 the sections in the file is unimportant.  */
   6417       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6418 	{
   6419 	  Elf_Internal_Shdr *hdr;
   6420 
   6421 	  hdr = *hdrpp;
   6422 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6423 	       && hdr->bfd_section == NULL)
   6424 	      /* Do not assign offsets for these sections yet: we don't know
   6425 		 their sizes.  */
   6426 	      || (abfd->is_linker_output
   6427 		  && hdr->bfd_section != NULL
   6428 		  && (hdr->sh_name == -1u
   6429 		      || bfd_section_is_ctf (hdr->bfd_section)))
   6430 	      || i == elf_onesymtab (abfd)
   6431 	      || (elf_symtab_shndx_list (abfd) != NULL
   6432 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6433 	      || i == elf_strtab_sec (abfd)
   6434 	      || i == elf_shstrtab_sec (abfd))
   6435 	    {
   6436 	      hdr->sh_offset = -1;
   6437 	    }
   6438 	  else
   6439 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6440 	}
   6441 
   6442       elf_next_file_pos (abfd) = off;
   6443       elf_program_header_size (abfd) = 0;
   6444     }
   6445   else
   6446     {
   6447       /* Assign file positions for the loaded sections based on the
   6448 	 assignment of sections to segments.  */
   6449       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6450 	return false;
   6451 
   6452       /* And for non-load sections.  */
   6453       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6454 	return false;
   6455     }
   6456 
   6457   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   6458     return false;
   6459 
   6460   /* Write out the program headers.  */
   6461   alloc = i_ehdrp->e_phnum;
   6462   if (alloc != 0)
   6463     {
   6464       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   6465 	{
   6466 	  /* Memory resident segments with non-zero size and RWX
   6467 	     permissions are a security risk, so we generate a warning
   6468 	     here if we are creating any.  */
   6469 	  unsigned int i;
   6470 
   6471 	  for (i = 0; i < alloc; i++)
   6472 	    {
   6473 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   6474 
   6475 	      if (phdr->p_memsz == 0)
   6476 		continue;
   6477 
   6478 	      if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
   6479 		_bfd_error_handler (_("warning: %pB has a TLS segment"
   6480 				      " with execute permission"),
   6481 				    abfd);
   6482 	      else if (phdr->p_type == PT_LOAD
   6483 		       && ((phdr->p_flags & (PF_R | PF_W | PF_X))
   6484 			   == (PF_R | PF_W | PF_X)))
   6485 		_bfd_error_handler (_("warning: %pB has a LOAD segment"
   6486 				      " with RWX permissions"),
   6487 				    abfd);
   6488 	    }
   6489 	}
   6490 
   6491       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   6492 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   6493 	return false;
   6494     }
   6495 
   6496   return true;
   6497 }
   6498 
   6499 bool
   6500 _bfd_elf_init_file_header (bfd *abfd,
   6501 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   6502 {
   6503   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   6504   struct elf_strtab_hash *shstrtab;
   6505   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6506 
   6507   i_ehdrp = elf_elfheader (abfd);
   6508 
   6509   shstrtab = _bfd_elf_strtab_init ();
   6510   if (shstrtab == NULL)
   6511     return false;
   6512 
   6513   elf_shstrtab (abfd) = shstrtab;
   6514 
   6515   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   6516   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   6517   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   6518   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   6519 
   6520   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   6521   i_ehdrp->e_ident[EI_DATA] =
   6522     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   6523   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   6524 
   6525   if ((abfd->flags & DYNAMIC) != 0)
   6526     i_ehdrp->e_type = ET_DYN;
   6527   else if ((abfd->flags & EXEC_P) != 0)
   6528     i_ehdrp->e_type = ET_EXEC;
   6529   else if (bfd_get_format (abfd) == bfd_core)
   6530     i_ehdrp->e_type = ET_CORE;
   6531   else
   6532     i_ehdrp->e_type = ET_REL;
   6533 
   6534   switch (bfd_get_arch (abfd))
   6535     {
   6536     case bfd_arch_unknown:
   6537       i_ehdrp->e_machine = EM_NONE;
   6538       break;
   6539 
   6540       /* There used to be a long list of cases here, each one setting
   6541 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   6542 	 in the corresponding bfd definition.  To avoid duplication,
   6543 	 the switch was removed.  Machines that need special handling
   6544 	 can generally do it in elf_backend_final_write_processing(),
   6545 	 unless they need the information earlier than the final write.
   6546 	 Such need can generally be supplied by replacing the tests for
   6547 	 e_machine with the conditions used to determine it.  */
   6548     default:
   6549       i_ehdrp->e_machine = bed->elf_machine_code;
   6550     }
   6551 
   6552   i_ehdrp->e_version = bed->s->ev_current;
   6553   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   6554 
   6555   /* No program header, for now.  */
   6556   i_ehdrp->e_phoff = 0;
   6557   i_ehdrp->e_phentsize = 0;
   6558   i_ehdrp->e_phnum = 0;
   6559 
   6560   /* Each bfd section is section header entry.  */
   6561   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   6562   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   6563 
   6564   elf_tdata (abfd)->symtab_hdr.sh_name =
   6565     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   6566   elf_tdata (abfd)->strtab_hdr.sh_name =
   6567     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   6568   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   6569     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   6570   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   6571       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   6572       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   6573     return false;
   6574 
   6575   return true;
   6576 }
   6577 
   6578 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   6579 
   6580    FIXME: We used to have code here to sort the PT_LOAD segments into
   6581    ascending order, as per the ELF spec.  But this breaks some programs,
   6582    including the Linux kernel.  But really either the spec should be
   6583    changed or the programs updated.  */
   6584 
   6585 bool
   6586 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   6587 {
   6588   if (link_info != NULL && bfd_link_pie (link_info))
   6589     {
   6590       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   6591       unsigned int num_segments = i_ehdrp->e_phnum;
   6592       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   6593       Elf_Internal_Phdr *segment = tdata->phdr;
   6594       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   6595 
   6596       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   6597       bfd_vma p_vaddr = (bfd_vma) -1;
   6598       for (; segment < end_segment; segment++)
   6599 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   6600 	  p_vaddr = segment->p_vaddr;
   6601 
   6602       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   6603 	 segments is non-zero.  */
   6604       if (p_vaddr)
   6605 	i_ehdrp->e_type = ET_EXEC;
   6606     }
   6607   return true;
   6608 }
   6609 
   6610 /* Assign file positions for all the reloc sections which are not part
   6611    of the loadable file image, and the file position of section headers.  */
   6612 
   6613 static bool
   6614 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   6615 {
   6616   file_ptr off;
   6617   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   6618   Elf_Internal_Shdr *shdrp;
   6619   Elf_Internal_Ehdr *i_ehdrp;
   6620   const struct elf_backend_data *bed;
   6621 
   6622   off = elf_next_file_pos (abfd);
   6623 
   6624   shdrpp = elf_elfsections (abfd);
   6625   end_shdrpp = shdrpp + elf_numsections (abfd);
   6626   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   6627     {
   6628       shdrp = *shdrpp;
   6629       if (shdrp->sh_offset == -1)
   6630 	{
   6631 	  asection *sec = shdrp->bfd_section;
   6632 	  if (sec == NULL
   6633 	      || shdrp->sh_type == SHT_REL
   6634 	      || shdrp->sh_type == SHT_RELA)
   6635 	    ;
   6636 	  else if (bfd_section_is_ctf (sec))
   6637 	    {
   6638 	      /* Update section size and contents.	*/
   6639 	      shdrp->sh_size = sec->size;
   6640 	      shdrp->contents = sec->contents;
   6641 	    }
   6642 	  else if (shdrp->sh_name == -1u)
   6643 	    {
   6644 	      const char *name = sec->name;
   6645 	      struct bfd_elf_section_data *d;
   6646 
   6647 	      /* Compress DWARF debug sections.  */
   6648 	      if (!bfd_compress_section (abfd, sec, shdrp->contents))
   6649 		return false;
   6650 
   6651 	      if (sec->compress_status == COMPRESS_SECTION_DONE
   6652 		  && (abfd->flags & BFD_COMPRESS_GABI) == 0
   6653 		  && name[1] == 'd')
   6654 		{
   6655 		  /* If section is compressed with zlib-gnu, convert
   6656 		     section name from .debug_* to .zdebug_*.  */
   6657 		  char *new_name = bfd_debug_name_to_zdebug (abfd, name);
   6658 		  if (new_name == NULL)
   6659 		    return false;
   6660 		  name = new_name;
   6661 		}
   6662 	      /* Add section name to section name section.  */
   6663 	      shdrp->sh_name
   6664 		= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   6665 						      name, false);
   6666 	      d = elf_section_data (sec);
   6667 
   6668 	      /* Add reloc section name to section name section.  */
   6669 	      if (d->rel.hdr
   6670 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
   6671 						  name, false))
   6672 		return false;
   6673 	      if (d->rela.hdr
   6674 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
   6675 						  name, true))
   6676 		return false;
   6677 
   6678 	      /* Update section size and contents.  */
   6679 	      shdrp->sh_size = sec->size;
   6680 	      shdrp->contents = sec->contents;
   6681 	      sec->contents = NULL;
   6682 	    }
   6683 
   6684 	  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   6685 	}
   6686     }
   6687 
   6688   /* Place section name section after DWARF debug sections have been
   6689      compressed.  */
   6690   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   6691   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   6692   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   6693   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   6694 
   6695   /* Place the section headers.  */
   6696   i_ehdrp = elf_elfheader (abfd);
   6697   bed = get_elf_backend_data (abfd);
   6698   off = align_file_position (off, 1 << bed->s->log_file_align);
   6699   i_ehdrp->e_shoff = off;
   6700   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   6701   elf_next_file_pos (abfd) = off;
   6702 
   6703   return true;
   6704 }
   6705 
   6706 bool
   6707 _bfd_elf_write_object_contents (bfd *abfd)
   6708 {
   6709   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6710   Elf_Internal_Shdr **i_shdrp;
   6711   bool failed;
   6712   unsigned int count, num_sec;
   6713   struct elf_obj_tdata *t;
   6714 
   6715   if (! abfd->output_has_begun
   6716       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   6717     return false;
   6718   /* Do not rewrite ELF data when the BFD has been opened for update.
   6719      abfd->output_has_begun was set to TRUE on opening, so creation of
   6720      new sections, and modification of existing section sizes was
   6721      restricted.  This means the ELF header, program headers and
   6722      section headers can't have changed.  If the contents of any
   6723      sections has been modified, then those changes have already been
   6724      written to the BFD.  */
   6725   else if (abfd->direction == both_direction)
   6726     {
   6727       BFD_ASSERT (abfd->output_has_begun);
   6728       return true;
   6729     }
   6730 
   6731   i_shdrp = elf_elfsections (abfd);
   6732 
   6733   failed = false;
   6734   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   6735   if (failed)
   6736     return false;
   6737 
   6738   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   6739     return false;
   6740 
   6741   /* After writing the headers, we need to write the sections too...  */
   6742   num_sec = elf_numsections (abfd);
   6743   for (count = 1; count < num_sec; count++)
   6744     {
   6745       i_shdrp[count]->sh_name
   6746 	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   6747 				  i_shdrp[count]->sh_name);
   6748       if (bed->elf_backend_section_processing)
   6749 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   6750 	  return false;
   6751       if (i_shdrp[count]->contents)
   6752 	{
   6753 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   6754 
   6755 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   6756 	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
   6757 	    return false;
   6758 	}
   6759     }
   6760 
   6761   /* Write out the section header names.  */
   6762   t = elf_tdata (abfd);
   6763   if (elf_shstrtab (abfd) != NULL
   6764       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   6765 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   6766     return false;
   6767 
   6768   if (!(*bed->elf_backend_final_write_processing) (abfd))
   6769     return false;
   6770 
   6771   if (!bed->s->write_shdrs_and_ehdr (abfd))
   6772     return false;
   6773 
   6774   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   6775   if (t->o->build_id.after_write_object_contents != NULL
   6776       && !(*t->o->build_id.after_write_object_contents) (abfd))
   6777     return false;
   6778   if (t->o->package_metadata.after_write_object_contents != NULL
   6779       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   6780     return false;
   6781 
   6782   return true;
   6783 }
   6784 
   6785 bool
   6786 _bfd_elf_write_corefile_contents (bfd *abfd)
   6787 {
   6788   /* Hopefully this can be done just like an object file.  */
   6789   return _bfd_elf_write_object_contents (abfd);
   6790 }
   6791 
   6792 /* Given a section, search the header to find them.  */
   6793 
   6794 unsigned int
   6795 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   6796 {
   6797   const struct elf_backend_data *bed;
   6798   unsigned int sec_index;
   6799 
   6800   if (elf_section_data (asect) != NULL
   6801       && elf_section_data (asect)->this_idx != 0)
   6802     return elf_section_data (asect)->this_idx;
   6803 
   6804   if (bfd_is_abs_section (asect))
   6805     sec_index = SHN_ABS;
   6806   else if (bfd_is_com_section (asect))
   6807     sec_index = SHN_COMMON;
   6808   else if (bfd_is_und_section (asect))
   6809     sec_index = SHN_UNDEF;
   6810   else
   6811     sec_index = SHN_BAD;
   6812 
   6813   bed = get_elf_backend_data (abfd);
   6814   if (bed->elf_backend_section_from_bfd_section)
   6815     {
   6816       int retval = sec_index;
   6817 
   6818       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   6819 	return retval;
   6820     }
   6821 
   6822   if (sec_index == SHN_BAD)
   6823     bfd_set_error (bfd_error_nonrepresentable_section);
   6824 
   6825   return sec_index;
   6826 }
   6827 
   6828 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   6829    on error.  */
   6830 
   6831 int
   6832 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   6833 {
   6834   asymbol *asym_ptr = *asym_ptr_ptr;
   6835   int idx;
   6836   flagword flags = asym_ptr->flags;
   6837 
   6838   /* When gas creates relocations against local labels, it creates its
   6839      own symbol for the section, but does put the symbol into the
   6840      symbol chain, so udata is 0.  When the linker is generating
   6841      relocatable output, this section symbol may be for one of the
   6842      input sections rather than the output section.  */
   6843   if (asym_ptr->udata.i == 0
   6844       && (flags & BSF_SECTION_SYM)
   6845       && asym_ptr->section)
   6846     {
   6847       asection *sec;
   6848 
   6849       sec = asym_ptr->section;
   6850       if (sec->owner != abfd && sec->output_section != NULL)
   6851 	sec = sec->output_section;
   6852       if (sec->owner == abfd
   6853 	  && sec->index < elf_num_section_syms (abfd)
   6854 	  && elf_section_syms (abfd)[sec->index] != NULL)
   6855 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   6856     }
   6857 
   6858   idx = asym_ptr->udata.i;
   6859 
   6860   if (idx == 0)
   6861     {
   6862       /* This case can occur when using --strip-symbol on a symbol
   6863 	 which is used in a relocation entry.  */
   6864       _bfd_error_handler
   6865 	/* xgettext:c-format */
   6866 	(_("%pB: symbol `%s' required but not present"),
   6867 	 abfd, bfd_asymbol_name (asym_ptr));
   6868       bfd_set_error (bfd_error_no_symbols);
   6869       return -1;
   6870     }
   6871 
   6872 #if DEBUG & 4
   6873   {
   6874     fprintf (stderr,
   6875 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
   6876 	     " flags = 0x%.8x\n",
   6877 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   6878     fflush (stderr);
   6879   }
   6880 #endif
   6881 
   6882   return idx;
   6883 }
   6884 
   6885 static inline bfd_vma
   6886 segment_size (Elf_Internal_Phdr *segment)
   6887 {
   6888   return (segment->p_memsz > segment->p_filesz
   6889 	  ? segment->p_memsz : segment->p_filesz);
   6890 }
   6891 
   6892 
   6893 /* Returns the end address of the segment + 1.  */
   6894 static inline bfd_vma
   6895 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
   6896 {
   6897   return start + segment_size (segment);
   6898 }
   6899 
   6900 static inline bfd_size_type
   6901 section_size (asection *section, Elf_Internal_Phdr *segment)
   6902 {
   6903   if ((section->flags & SEC_HAS_CONTENTS) != 0
   6904       || (section->flags & SEC_THREAD_LOCAL) == 0
   6905       || segment->p_type == PT_TLS)
   6906     return section->size;
   6907   return 0;
   6908 }
   6909 
   6910 /* Returns TRUE if the given section is contained within the given
   6911    segment.  LMA addresses are compared against PADDR when
   6912    bed->want_p_paddr_set_to_zero is false, VMA against VADDR when true.  */
   6913 static bool
   6914 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
   6915 		 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
   6916 		 const struct elf_backend_data *bed)
   6917 {
   6918   bfd_vma seg_addr = !bed->want_p_paddr_set_to_zero ? paddr : vaddr;
   6919   bfd_vma addr = !bed->want_p_paddr_set_to_zero ? section->lma : section->vma;
   6920   bfd_vma octet;
   6921   if (_bfd_mul_overflow (addr, opb, &octet))
   6922     return false;
   6923   /* The third and fourth lines below are testing that the section end
   6924      address is within the segment.  It's written this way to avoid
   6925      overflow.  Add seg_addr + section_size to both sides of the
   6926      inequality to make it obvious.  */
   6927   return (octet >= seg_addr
   6928 	  && segment_size (segment) >= section_size (section, segment)
   6929 	  && (octet - seg_addr
   6930 	      <= segment_size (segment) - section_size (section, segment)));
   6931 }
   6932 
   6933 /* Handle PT_NOTE segment.  */
   6934 static bool
   6935 is_note (asection *s, Elf_Internal_Phdr *p)
   6936 {
   6937   return (p->p_type == PT_NOTE
   6938 	  && elf_section_type (s) == SHT_NOTE
   6939 	  && (ufile_ptr) s->filepos >= p->p_offset
   6940 	  && p->p_filesz >= s->size
   6941 	  && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
   6942 }
   6943 
   6944 /* Rewrite program header information.  */
   6945 
   6946 static bool
   6947 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   6948 {
   6949   Elf_Internal_Ehdr *iehdr;
   6950   struct elf_segment_map *map;
   6951   struct elf_segment_map *map_first;
   6952   struct elf_segment_map **pointer_to_map;
   6953   Elf_Internal_Phdr *segment;
   6954   asection *section;
   6955   unsigned int i;
   6956   unsigned int num_segments;
   6957   bool phdr_included = false;
   6958   bool p_paddr_valid;
   6959   struct elf_segment_map *phdr_adjust_seg = NULL;
   6960   unsigned int phdr_adjust_num = 0;
   6961   const struct elf_backend_data *bed;
   6962   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   6963 
   6964   bed = get_elf_backend_data (ibfd);
   6965   iehdr = elf_elfheader (ibfd);
   6966 
   6967   map_first = NULL;
   6968   pointer_to_map = &map_first;
   6969 
   6970   num_segments = elf_elfheader (ibfd)->e_phnum;
   6971 
   6972   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   6973      linker, which generates a PT_INTERP section with p_vaddr and
   6974      p_memsz set to 0.  */
   6975 #define IS_SOLARIS_PT_INTERP(p, s)					\
   6976   (p->p_vaddr == 0							\
   6977    && p->p_paddr == 0							\
   6978    && p->p_memsz == 0							\
   6979    && p->p_filesz > 0							\
   6980    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   6981    && s->size > 0							\
   6982    && (bfd_vma) s->filepos >= p->p_offset				\
   6983    && ((bfd_vma) s->filepos + s->size					\
   6984        <= p->p_offset + p->p_filesz))
   6985 
   6986   /* Decide if the given section should be included in the given segment.
   6987      A section will be included if:
   6988        1. It is within the address space of the segment -- we use the LMA
   6989 	  if that is set for the segment and the VMA otherwise,
   6990        2. It is an allocated section or a NOTE section in a PT_NOTE
   6991 	  segment.
   6992        3. There is an output section associated with it,
   6993        4. The section has not already been allocated to a previous segment.
   6994        5. PT_GNU_STACK segments do not include any sections.
   6995        6. PT_TLS segment includes only SHF_TLS sections.
   6996        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   6997        8. PT_DYNAMIC should not contain empty sections at the beginning
   6998 	  (with the possible exception of .dynamic).  */
   6999 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb)		\
   7000   (((is_contained_by (section, segment, segment->p_paddr,		\
   7001 		      segment->p_vaddr, opb, bed)			\
   7002      && (section->flags & SEC_ALLOC) != 0)				\
   7003     || is_note (section, segment))					\
   7004    && segment->p_type != PT_GNU_STACK					\
   7005    && (segment->p_type != PT_TLS					\
   7006        || (section->flags & SEC_THREAD_LOCAL))				\
   7007    && (segment->p_type == PT_LOAD					\
   7008        || segment->p_type == PT_TLS					\
   7009        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7010    && (segment->p_type != PT_DYNAMIC					\
   7011        || section_size (section, segment) > 0				\
   7012        || (segment->p_paddr						\
   7013 	   ? segment->p_paddr != section->lma * (opb)			\
   7014 	   : segment->p_vaddr != section->vma * (opb))			\
   7015        || (strcmp (bfd_section_name (section), ".dynamic") == 0))	\
   7016    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7017 
   7018 /* If the output section of a section in the input segment is NULL,
   7019    it is removed from the corresponding output segment.   */
   7020 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb)		\
   7021   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)		\
   7022    && section->output_section != NULL)
   7023 
   7024   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7025 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7026   (seg1->field >= segment_end (seg2, seg2->field))
   7027 
   7028   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7029      their VMA address ranges and their LMA address ranges overlap.
   7030      It is possible to have overlapping VMA ranges without overlapping LMA
   7031      ranges.  RedBoot images for example can have both .data and .bss mapped
   7032      to the same VMA range, but with the .data section mapped to a different
   7033      LMA.  */
   7034 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7035   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7036 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7037    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7038 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7039 
   7040   /* Initialise the segment mark field, and discard stupid alignment.  */
   7041   for (section = ibfd->sections; section != NULL; section = section->next)
   7042     {
   7043       asection *o = section->output_section;
   7044       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7045 	o->alignment_power = 0;
   7046       section->segment_mark = false;
   7047     }
   7048 
   7049   /* The Solaris linker creates program headers in which all the
   7050      p_paddr fields are zero.  When we try to objcopy or strip such a
   7051      file, we get confused.  Check for this case, and if we find it
   7052      don't set the p_paddr_valid fields.  */
   7053   p_paddr_valid = false;
   7054   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7055        i < num_segments;
   7056        i++, segment++)
   7057     if (segment->p_paddr != 0)
   7058       {
   7059 	p_paddr_valid = true;
   7060 	break;
   7061       }
   7062 
   7063   /* Scan through the segments specified in the program header
   7064      of the input BFD.  For this first scan we look for overlaps
   7065      in the loadable segments.  These can be created by weird
   7066      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7067   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7068        i < num_segments;
   7069        i++, segment++)
   7070     {
   7071       unsigned int j;
   7072       Elf_Internal_Phdr *segment2;
   7073 
   7074       if (segment->p_type == PT_INTERP)
   7075 	for (section = ibfd->sections; section; section = section->next)
   7076 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7077 	    {
   7078 	      /* Mininal change so that the normal section to segment
   7079 		 assignment code will work.  */
   7080 	      segment->p_vaddr = section->vma * opb;
   7081 	      break;
   7082 	    }
   7083 
   7084       if (segment->p_type != PT_LOAD)
   7085 	{
   7086 	  /* Remove PT_GNU_RELRO segment.  */
   7087 	  if (segment->p_type == PT_GNU_RELRO)
   7088 	    segment->p_type = PT_NULL;
   7089 	  continue;
   7090 	}
   7091 
   7092       /* Determine if this segment overlaps any previous segments.  */
   7093       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7094 	{
   7095 	  bfd_signed_vma extra_length;
   7096 
   7097 	  if (segment2->p_type != PT_LOAD
   7098 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7099 	    continue;
   7100 
   7101 	  /* Merge the two segments together.  */
   7102 	  if (segment2->p_vaddr < segment->p_vaddr)
   7103 	    {
   7104 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7105 		 SEGMENT.  */
   7106 	      extra_length = (segment_end (segment, segment->p_vaddr)
   7107 			      - segment_end (segment2, segment2->p_vaddr));
   7108 
   7109 	      if (extra_length > 0)
   7110 		{
   7111 		  segment2->p_memsz += extra_length;
   7112 		  segment2->p_filesz += extra_length;
   7113 		}
   7114 
   7115 	      segment->p_type = PT_NULL;
   7116 
   7117 	      /* Since we have deleted P we must restart the outer loop.  */
   7118 	      i = 0;
   7119 	      segment = elf_tdata (ibfd)->phdr;
   7120 	      break;
   7121 	    }
   7122 	  else
   7123 	    {
   7124 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7125 		 SEGMENT2.  */
   7126 	      extra_length = (segment_end (segment2, segment2->p_vaddr)
   7127 			      - segment_end (segment, segment->p_vaddr));
   7128 
   7129 	      if (extra_length > 0)
   7130 		{
   7131 		  segment->p_memsz += extra_length;
   7132 		  segment->p_filesz += extra_length;
   7133 		}
   7134 
   7135 	      segment2->p_type = PT_NULL;
   7136 	    }
   7137 	}
   7138     }
   7139 
   7140   /* The second scan attempts to assign sections to segments.  */
   7141   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7142        i < num_segments;
   7143        i++, segment++)
   7144     {
   7145       unsigned int section_count;
   7146       asection **sections;
   7147       asection *output_section;
   7148       unsigned int isec;
   7149       asection *matching_lma;
   7150       asection *suggested_lma;
   7151       unsigned int j;
   7152       size_t amt;
   7153       asection *first_section;
   7154 
   7155       if (segment->p_type == PT_NULL)
   7156 	continue;
   7157 
   7158       first_section = NULL;
   7159       /* Compute how many sections might be placed into this segment.  */
   7160       for (section = ibfd->sections, section_count = 0;
   7161 	   section != NULL;
   7162 	   section = section->next)
   7163 	{
   7164 	  /* Find the first section in the input segment, which may be
   7165 	     removed from the corresponding output segment.   */
   7166 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
   7167 	    {
   7168 	      if (first_section == NULL)
   7169 		first_section = section;
   7170 	      if (section->output_section != NULL)
   7171 		++section_count;
   7172 	    }
   7173 	}
   7174 
   7175       /* Allocate a segment map big enough to contain
   7176 	 all of the sections we have selected.  */
   7177       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7178       amt += section_count * sizeof (asection *);
   7179       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7180       if (map == NULL)
   7181 	return false;
   7182 
   7183       /* Initialise the fields of the segment map.  Default to
   7184 	 using the physical address of the segment in the input BFD.  */
   7185       map->next = NULL;
   7186       map->p_type = segment->p_type;
   7187       map->p_flags = segment->p_flags;
   7188       map->p_flags_valid = 1;
   7189 
   7190       if (map->p_type == PT_LOAD
   7191 	  && (ibfd->flags & D_PAGED) != 0
   7192 	  && maxpagesize > 1
   7193 	  && segment->p_align > 1)
   7194 	{
   7195 	  map->p_align = segment->p_align;
   7196 	  if (segment->p_align > maxpagesize)
   7197 	    map->p_align = maxpagesize;
   7198 	  map->p_align_valid = 1;
   7199 	}
   7200 
   7201       /* If the first section in the input segment is removed, there is
   7202 	 no need to preserve segment physical address in the corresponding
   7203 	 output segment.  */
   7204       if (!first_section || first_section->output_section != NULL)
   7205 	{
   7206 	  map->p_paddr = segment->p_paddr;
   7207 	  map->p_paddr_valid = p_paddr_valid;
   7208 	}
   7209 
   7210       /* Determine if this segment contains the ELF file header
   7211 	 and if it contains the program headers themselves.  */
   7212       map->includes_filehdr = (segment->p_offset == 0
   7213 			       && segment->p_filesz >= iehdr->e_ehsize);
   7214       map->includes_phdrs = 0;
   7215 
   7216       if (!phdr_included || segment->p_type != PT_LOAD)
   7217 	{
   7218 	  map->includes_phdrs =
   7219 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7220 	     && (segment->p_offset + segment->p_filesz
   7221 		 >= ((bfd_vma) iehdr->e_phoff
   7222 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7223 
   7224 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7225 	    phdr_included = true;
   7226 	}
   7227 
   7228       if (section_count == 0)
   7229 	{
   7230 	  /* Special segments, such as the PT_PHDR segment, may contain
   7231 	     no sections, but ordinary, loadable segments should contain
   7232 	     something.  They are allowed by the ELF spec however, so only
   7233 	     a warning is produced.
   7234 	     There is however the valid use case of embedded systems which
   7235 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7236 	     flash memory with zeros.  No warning is shown for that case.  */
   7237 	  if (segment->p_type == PT_LOAD
   7238 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7239 	    /* xgettext:c-format */
   7240 	    _bfd_error_handler
   7241 	      (_("%pB: warning: empty loadable segment detected"
   7242 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7243 	       ibfd, (uint64_t) segment->p_vaddr);
   7244 
   7245 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7246 	  map->count = 0;
   7247 	  *pointer_to_map = map;
   7248 	  pointer_to_map = &map->next;
   7249 
   7250 	  continue;
   7251 	}
   7252 
   7253       /* Now scan the sections in the input BFD again and attempt
   7254 	 to add their corresponding output sections to the segment map.
   7255 	 The problem here is how to handle an output section which has
   7256 	 been moved (ie had its LMA changed).  There are four possibilities:
   7257 
   7258 	 1. None of the sections have been moved.
   7259 	    In this case we can continue to use the segment LMA from the
   7260 	    input BFD.
   7261 
   7262 	 2. All of the sections have been moved by the same amount.
   7263 	    In this case we can change the segment's LMA to match the LMA
   7264 	    of the first section.
   7265 
   7266 	 3. Some of the sections have been moved, others have not.
   7267 	    In this case those sections which have not been moved can be
   7268 	    placed in the current segment which will have to have its size,
   7269 	    and possibly its LMA changed, and a new segment or segments will
   7270 	    have to be created to contain the other sections.
   7271 
   7272 	 4. The sections have been moved, but not by the same amount.
   7273 	    In this case we can change the segment's LMA to match the LMA
   7274 	    of the first section and we will have to create a new segment
   7275 	    or segments to contain the other sections.
   7276 
   7277 	 In order to save time, we allocate an array to hold the section
   7278 	 pointers that we are interested in.  As these sections get assigned
   7279 	 to a segment, they are removed from this array.  */
   7280 
   7281       amt = section_count * sizeof (asection *);
   7282       sections = (asection **) bfd_malloc (amt);
   7283       if (sections == NULL)
   7284 	return false;
   7285 
   7286       /* Step One: Scan for segment vs section LMA conflicts.
   7287 	 Also add the sections to the section array allocated above.
   7288 	 Also add the sections to the current segment.  In the common
   7289 	 case, where the sections have not been moved, this means that
   7290 	 we have completely filled the segment, and there is nothing
   7291 	 more to do.  */
   7292       isec = 0;
   7293       matching_lma = NULL;
   7294       suggested_lma = NULL;
   7295 
   7296       for (section = first_section, j = 0;
   7297 	   section != NULL;
   7298 	   section = section->next)
   7299 	{
   7300 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
   7301 	    {
   7302 	      output_section = section->output_section;
   7303 
   7304 	      sections[j++] = section;
   7305 
   7306 	      /* The Solaris native linker always sets p_paddr to 0.
   7307 		 We try to catch that case here, and set it to the
   7308 		 correct value.  Note - some backends require that
   7309 		 p_paddr be left as zero.  */
   7310 	      if (!p_paddr_valid
   7311 		  && segment->p_vaddr != 0
   7312 		  && !bed->want_p_paddr_set_to_zero
   7313 		  && isec == 0
   7314 		  && output_section->lma != 0
   7315 		  && (align_power (segment->p_vaddr
   7316 				   + (map->includes_filehdr
   7317 				      ? iehdr->e_ehsize : 0)
   7318 				   + (map->includes_phdrs
   7319 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7320 				      : 0),
   7321 				   output_section->alignment_power * opb)
   7322 		      == (output_section->vma * opb)))
   7323 		map->p_paddr = segment->p_vaddr;
   7324 
   7325 	      /* Match up the physical address of the segment with the
   7326 		 LMA address of the output section.  */
   7327 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7328 				   map->p_paddr + map->p_vaddr_offset, opb, bed)
   7329 		  || is_note (section, segment))
   7330 		{
   7331 		  if (matching_lma == NULL
   7332 		      || output_section->lma < matching_lma->lma)
   7333 		    matching_lma = output_section;
   7334 
   7335 		  /* We assume that if the section fits within the segment
   7336 		     then it does not overlap any other section within that
   7337 		     segment.  */
   7338 		  map->sections[isec++] = output_section;
   7339 		}
   7340 	      else if (suggested_lma == NULL)
   7341 		suggested_lma = output_section;
   7342 
   7343 	      if (j == section_count)
   7344 		break;
   7345 	    }
   7346 	}
   7347 
   7348       BFD_ASSERT (j == section_count);
   7349 
   7350       /* Step Two: Adjust the physical address of the current segment,
   7351 	 if necessary.  */
   7352       if (isec == section_count)
   7353 	{
   7354 	  /* All of the sections fitted within the segment as currently
   7355 	     specified.  This is the default case.  Add the segment to
   7356 	     the list of built segments and carry on to process the next
   7357 	     program header in the input BFD.  */
   7358 	  map->count = section_count;
   7359 	  *pointer_to_map = map;
   7360 	  pointer_to_map = &map->next;
   7361 
   7362 	  if (p_paddr_valid
   7363 	      && !bed->want_p_paddr_set_to_zero)
   7364 	    {
   7365 	      bfd_vma hdr_size = 0;
   7366 	      if (map->includes_filehdr)
   7367 		hdr_size = iehdr->e_ehsize;
   7368 	      if (map->includes_phdrs)
   7369 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7370 
   7371 	      /* Account for padding before the first section in the
   7372 		 segment.  */
   7373 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7374 				     - matching_lma->lma);
   7375 	    }
   7376 
   7377 	  free (sections);
   7378 	  continue;
   7379 	}
   7380       else
   7381 	{
   7382 	  /* Change the current segment's physical address to match
   7383 	     the LMA of the first section that fitted, or if no
   7384 	     section fitted, the first section.  */
   7385 	  if (matching_lma == NULL)
   7386 	    matching_lma = suggested_lma;
   7387 
   7388 	  map->p_paddr = matching_lma->lma * opb;
   7389 
   7390 	  /* Offset the segment physical address from the lma
   7391 	     to allow for space taken up by elf headers.  */
   7392 	  if (map->includes_phdrs)
   7393 	    {
   7394 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7395 
   7396 	      /* iehdr->e_phnum is just an estimate of the number
   7397 		 of program headers that we will need.  Make a note
   7398 		 here of the number we used and the segment we chose
   7399 		 to hold these headers, so that we can adjust the
   7400 		 offset when we know the correct value.  */
   7401 	      phdr_adjust_num = iehdr->e_phnum;
   7402 	      phdr_adjust_seg = map;
   7403 	    }
   7404 
   7405 	  if (map->includes_filehdr)
   7406 	    {
   7407 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7408 	      map->p_paddr -= iehdr->e_ehsize;
   7409 	      /* We've subtracted off the size of headers from the
   7410 		 first section lma, but there may have been some
   7411 		 alignment padding before that section too.  Try to
   7412 		 account for that by adjusting the segment lma down to
   7413 		 the same alignment.  */
   7414 	      if (segment->p_align != 0 && segment->p_align < align)
   7415 		align = segment->p_align;
   7416 	      map->p_paddr &= -(align * opb);
   7417 	    }
   7418 	}
   7419 
   7420       /* Step Three: Loop over the sections again, this time assigning
   7421 	 those that fit to the current segment and removing them from the
   7422 	 sections array; but making sure not to leave large gaps.  Once all
   7423 	 possible sections have been assigned to the current segment it is
   7424 	 added to the list of built segments and if sections still remain
   7425 	 to be assigned, a new segment is constructed before repeating
   7426 	 the loop.  */
   7427       isec = 0;
   7428       do
   7429 	{
   7430 	  map->count = 0;
   7431 	  suggested_lma = NULL;
   7432 
   7433 	  /* Fill the current segment with sections that fit.  */
   7434 	  for (j = 0; j < section_count; j++)
   7435 	    {
   7436 	      section = sections[j];
   7437 
   7438 	      if (section == NULL)
   7439 		continue;
   7440 
   7441 	      output_section = section->output_section;
   7442 
   7443 	      BFD_ASSERT (output_section != NULL);
   7444 
   7445 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7446 				   map->p_paddr + map->p_vaddr_offset, opb, bed)
   7447 		  || is_note (section, segment))
   7448 		{
   7449 		  if (map->count == 0)
   7450 		    {
   7451 		      /* If the first section in a segment does not start at
   7452 			 the beginning of the segment, then something is
   7453 			 wrong.  */
   7454 		      if (align_power (map->p_paddr
   7455 				       + (map->includes_filehdr
   7456 					  ? iehdr->e_ehsize : 0)
   7457 				       + (map->includes_phdrs
   7458 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7459 					  : 0),
   7460 				       output_section->alignment_power * opb)
   7461 			  != output_section->lma * opb)
   7462 			goto sorry;
   7463 		    }
   7464 		  else
   7465 		    {
   7466 		      asection *prev_sec;
   7467 
   7468 		      prev_sec = map->sections[map->count - 1];
   7469 
   7470 		      /* If the gap between the end of the previous section
   7471 			 and the start of this section is more than
   7472 			 maxpagesize then we need to start a new segment.  */
   7473 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7474 				      maxpagesize)
   7475 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7476 			  || (prev_sec->lma + prev_sec->size
   7477 			      > output_section->lma))
   7478 			{
   7479 			  if (suggested_lma == NULL)
   7480 			    suggested_lma = output_section;
   7481 
   7482 			  continue;
   7483 			}
   7484 		    }
   7485 
   7486 		  map->sections[map->count++] = output_section;
   7487 		  ++isec;
   7488 		  sections[j] = NULL;
   7489 		  if (segment->p_type == PT_LOAD)
   7490 		    section->segment_mark = true;
   7491 		}
   7492 	      else if (suggested_lma == NULL)
   7493 		suggested_lma = output_section;
   7494 	    }
   7495 
   7496 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   7497 	     be assigned to any segment - because for example they have a
   7498 	     negative size - or segments that do not contain any sections.
   7499 	     But there are also valid reasons why a segment can be empty.
   7500 	     So allow a count of zero.  */
   7501 
   7502 	  /* Add the current segment to the list of built segments.  */
   7503 	  *pointer_to_map = map;
   7504 	  pointer_to_map = &map->next;
   7505 
   7506 	  if (isec < section_count)
   7507 	    {
   7508 	      /* We still have not allocated all of the sections to
   7509 		 segments.  Create a new segment here, initialise it
   7510 		 and carry on looping.  */
   7511 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7512 	      amt += section_count * sizeof (asection *);
   7513 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7514 	      if (map == NULL)
   7515 		{
   7516 		  free (sections);
   7517 		  return false;
   7518 		}
   7519 
   7520 	      /* Initialise the fields of the segment map.  Set the physical
   7521 		 physical address to the LMA of the first section that has
   7522 		 not yet been assigned.  */
   7523 	      map->next = NULL;
   7524 	      map->p_type = segment->p_type;
   7525 	      map->p_flags = segment->p_flags;
   7526 	      map->p_flags_valid = 1;
   7527 	      map->p_paddr = suggested_lma->lma * opb;
   7528 	      map->p_paddr_valid = p_paddr_valid;
   7529 	      map->includes_filehdr = 0;
   7530 	      map->includes_phdrs = 0;
   7531 	    }
   7532 
   7533 	  continue;
   7534 	sorry:
   7535 	  bfd_set_error (bfd_error_sorry);
   7536 	  free (sections);
   7537 	  return false;
   7538 	}
   7539       while (isec < section_count);
   7540 
   7541       free (sections);
   7542     }
   7543 
   7544   elf_seg_map (obfd) = map_first;
   7545 
   7546   /* If we had to estimate the number of program headers that were
   7547      going to be needed, then check our estimate now and adjust
   7548      the offset if necessary.  */
   7549   if (phdr_adjust_seg != NULL)
   7550     {
   7551       unsigned int count;
   7552 
   7553       for (count = 0, map = map_first; map != NULL; map = map->next)
   7554 	count++;
   7555 
   7556       if (count > phdr_adjust_num)
   7557 	phdr_adjust_seg->p_paddr
   7558 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   7559 
   7560       for (map = map_first; map != NULL; map = map->next)
   7561 	if (map->p_type == PT_PHDR)
   7562 	  {
   7563 	    bfd_vma adjust
   7564 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   7565 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   7566 	    break;
   7567 	  }
   7568     }
   7569 
   7570 #undef IS_SOLARIS_PT_INTERP
   7571 #undef IS_SECTION_IN_INPUT_SEGMENT
   7572 #undef INCLUDE_SECTION_IN_SEGMENT
   7573 #undef SEGMENT_AFTER_SEGMENT
   7574 #undef SEGMENT_OVERLAPS
   7575   return true;
   7576 }
   7577 
   7578 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   7579 
   7580 static bool
   7581 elf_is_p_align_valid (bfd *abfd)
   7582 {
   7583   unsigned int i;
   7584   Elf_Internal_Phdr *segment;
   7585   unsigned int num_segments;
   7586   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7587   bfd_size_type maxpagesize = bed->maxpagesize;
   7588   bfd_size_type p_align = bed->p_align;
   7589 
   7590   /* Return true if the default p_align value isn't set or the maximum
   7591      page size is the same as the minimum page size.  */
   7592   if (p_align == 0 || maxpagesize == bed->minpagesize)
   7593     return true;
   7594 
   7595   /* When the default p_align value is set, p_align may be set to the
   7596      default p_align value while segments are aligned to the maximum
   7597      page size.  In this case, the input p_align will be ignored and
   7598      the maximum page size will be used to align the output segments.  */
   7599   segment = elf_tdata (abfd)->phdr;
   7600   num_segments = elf_elfheader (abfd)->e_phnum;
   7601   for (i = 0; i < num_segments; i++, segment++)
   7602     if (segment->p_type == PT_LOAD
   7603 	&& (segment->p_align != p_align
   7604 	    || vma_page_aligned_bias (segment->p_vaddr,
   7605 				      segment->p_offset,
   7606 				      maxpagesize) != 0))
   7607       return true;
   7608 
   7609   return false;
   7610 }
   7611 
   7612 /* Copy ELF program header information.  */
   7613 
   7614 static bool
   7615 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   7616 {
   7617   Elf_Internal_Ehdr *iehdr;
   7618   struct elf_segment_map *map;
   7619   struct elf_segment_map *map_first;
   7620   struct elf_segment_map **pointer_to_map;
   7621   Elf_Internal_Phdr *segment;
   7622   unsigned int i;
   7623   unsigned int num_segments;
   7624   bool phdr_included = false;
   7625   bool p_paddr_valid;
   7626   bool p_palign_valid;
   7627   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7628 
   7629   iehdr = elf_elfheader (ibfd);
   7630 
   7631   map_first = NULL;
   7632   pointer_to_map = &map_first;
   7633 
   7634   /* If all the segment p_paddr fields are zero, don't set
   7635      map->p_paddr_valid.  */
   7636   p_paddr_valid = false;
   7637   num_segments = elf_elfheader (ibfd)->e_phnum;
   7638   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7639        i < num_segments;
   7640        i++, segment++)
   7641     if (segment->p_paddr != 0)
   7642       {
   7643 	p_paddr_valid = true;
   7644 	break;
   7645       }
   7646 
   7647   p_palign_valid = elf_is_p_align_valid (ibfd);
   7648 
   7649   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7650        i < num_segments;
   7651        i++, segment++)
   7652     {
   7653       asection *section;
   7654       unsigned int section_count;
   7655       size_t amt;
   7656       Elf_Internal_Shdr *this_hdr;
   7657       asection *first_section = NULL;
   7658       asection *lowest_section;
   7659 
   7660       /* Compute how many sections are in this segment.  */
   7661       for (section = ibfd->sections, section_count = 0;
   7662 	   section != NULL;
   7663 	   section = section->next)
   7664 	{
   7665 	  this_hdr = &(elf_section_data(section)->this_hdr);
   7666 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7667 	    {
   7668 	      if (first_section == NULL)
   7669 		first_section = section;
   7670 	      section_count++;
   7671 	    }
   7672 	}
   7673 
   7674       /* Allocate a segment map big enough to contain
   7675 	 all of the sections we have selected.  */
   7676       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7677       amt += section_count * sizeof (asection *);
   7678       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7679       if (map == NULL)
   7680 	return false;
   7681 
   7682       /* Initialize the fields of the output segment map with the
   7683 	 input segment.  */
   7684       map->next = NULL;
   7685       map->p_type = segment->p_type;
   7686       map->p_flags = segment->p_flags;
   7687       map->p_flags_valid = 1;
   7688       map->p_paddr = segment->p_paddr;
   7689       map->p_paddr_valid = p_paddr_valid;
   7690       map->p_align = segment->p_align;
   7691       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   7692       map->p_align_valid = (map->p_type == PT_GNU_STACK
   7693 			    || p_palign_valid);
   7694       map->p_vaddr_offset = 0;
   7695 
   7696       if (map->p_type == PT_GNU_RELRO
   7697 	  || map->p_type == PT_GNU_STACK)
   7698 	{
   7699 	  /* The PT_GNU_RELRO segment may contain the first a few
   7700 	     bytes in the .got.plt section even if the whole .got.plt
   7701 	     section isn't in the PT_GNU_RELRO segment.  We won't
   7702 	     change the size of the PT_GNU_RELRO segment.
   7703 	     Similarly, PT_GNU_STACK size is significant on uclinux
   7704 	     systems.    */
   7705 	  map->p_size = segment->p_memsz;
   7706 	  map->p_size_valid = 1;
   7707 	}
   7708 
   7709       /* Determine if this segment contains the ELF file header
   7710 	 and if it contains the program headers themselves.  */
   7711       map->includes_filehdr = (segment->p_offset == 0
   7712 			       && segment->p_filesz >= iehdr->e_ehsize);
   7713 
   7714       map->includes_phdrs = 0;
   7715       if (! phdr_included || segment->p_type != PT_LOAD)
   7716 	{
   7717 	  map->includes_phdrs =
   7718 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7719 	     && (segment->p_offset + segment->p_filesz
   7720 		 >= ((bfd_vma) iehdr->e_phoff
   7721 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7722 
   7723 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7724 	    phdr_included = true;
   7725 	}
   7726 
   7727       lowest_section = NULL;
   7728       if (section_count != 0)
   7729 	{
   7730 	  unsigned int isec = 0;
   7731 
   7732 	  for (section = first_section;
   7733 	       section != NULL;
   7734 	       section = section->next)
   7735 	    {
   7736 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7737 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7738 		{
   7739 		  map->sections[isec++] = section->output_section;
   7740 		  if ((section->flags & SEC_ALLOC) != 0)
   7741 		    {
   7742 		      bfd_vma seg_off;
   7743 
   7744 		      if (lowest_section == NULL
   7745 			  || section->lma < lowest_section->lma)
   7746 			lowest_section = section;
   7747 
   7748 		      /* Section lmas are set up from PT_LOAD header
   7749 			 p_paddr in _bfd_elf_make_section_from_shdr.
   7750 			 If this header has a p_paddr that disagrees
   7751 			 with the section lma, flag the p_paddr as
   7752 			 invalid.  */
   7753 		      if ((section->flags & SEC_LOAD) != 0)
   7754 			seg_off = this_hdr->sh_offset - segment->p_offset;
   7755 		      else
   7756 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   7757 		      if (section->lma * opb - segment->p_paddr != seg_off)
   7758 			map->p_paddr_valid = false;
   7759 		    }
   7760 		  if (isec == section_count)
   7761 		    break;
   7762 		}
   7763 	    }
   7764 	}
   7765 
   7766       if (section_count == 0)
   7767 	map->p_vaddr_offset = segment->p_vaddr / opb;
   7768       else if (map->p_paddr_valid)
   7769 	{
   7770 	  /* Account for padding before the first section in the segment.  */
   7771 	  bfd_vma hdr_size = 0;
   7772 	  if (map->includes_filehdr)
   7773 	    hdr_size = iehdr->e_ehsize;
   7774 	  if (map->includes_phdrs)
   7775 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7776 
   7777 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7778 				 - (lowest_section ? lowest_section->lma : 0));
   7779 	}
   7780 
   7781       map->count = section_count;
   7782       *pointer_to_map = map;
   7783       pointer_to_map = &map->next;
   7784     }
   7785 
   7786   elf_seg_map (obfd) = map_first;
   7787   return true;
   7788 }
   7789 
   7790 /* Copy private BFD data.  This copies or rewrites ELF program header
   7791    information.  */
   7792 
   7793 static bool
   7794 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   7795 {
   7796   bfd_vma maxpagesize;
   7797 
   7798   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7799       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7800     return true;
   7801 
   7802   if (elf_tdata (ibfd)->phdr == NULL)
   7803     return true;
   7804 
   7805   if (ibfd->xvec == obfd->xvec)
   7806     {
   7807       /* Check to see if any sections in the input BFD
   7808 	 covered by ELF program header have changed.  */
   7809       Elf_Internal_Phdr *segment;
   7810       asection *section, *osec;
   7811       unsigned int i, num_segments;
   7812       Elf_Internal_Shdr *this_hdr;
   7813       const struct elf_backend_data *bed;
   7814 
   7815       bed = get_elf_backend_data (ibfd);
   7816 
   7817       /* Regenerate the segment map if p_paddr is set to 0.  */
   7818       if (bed->want_p_paddr_set_to_zero)
   7819 	goto rewrite;
   7820 
   7821       /* Initialize the segment mark field.  */
   7822       for (section = obfd->sections; section != NULL;
   7823 	   section = section->next)
   7824 	section->segment_mark = false;
   7825 
   7826       num_segments = elf_elfheader (ibfd)->e_phnum;
   7827       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7828 	   i < num_segments;
   7829 	   i++, segment++)
   7830 	{
   7831 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   7832 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   7833 	     which severly confuses things, so always regenerate the segment
   7834 	     map in this case.  */
   7835 	  if (segment->p_paddr == 0
   7836 	      && segment->p_memsz == 0
   7837 	      && (segment->p_type == PT_INTERP
   7838 		  || segment->p_type == PT_DYNAMIC))
   7839 	    goto rewrite;
   7840 
   7841 	  for (section = ibfd->sections;
   7842 	       section != NULL; section = section->next)
   7843 	    {
   7844 	      /* We mark the output section so that we know it comes
   7845 		 from the input BFD.  */
   7846 	      osec = section->output_section;
   7847 	      if (osec)
   7848 		osec->segment_mark = true;
   7849 
   7850 	      /* Check if this section is covered by the segment.  */
   7851 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7852 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7853 		{
   7854 		  /* FIXME: Check if its output section is changed or
   7855 		     removed.  What else do we need to check?  */
   7856 		  if (osec == NULL
   7857 		      || section->flags != osec->flags
   7858 		      || section->lma != osec->lma
   7859 		      || section->vma != osec->vma
   7860 		      || section->size != osec->size
   7861 		      || section->rawsize != osec->rawsize
   7862 		      || section->alignment_power != osec->alignment_power)
   7863 		    goto rewrite;
   7864 		}
   7865 	    }
   7866 	}
   7867 
   7868       /* Check to see if any output section do not come from the
   7869 	 input BFD.  */
   7870       for (section = obfd->sections; section != NULL;
   7871 	   section = section->next)
   7872 	{
   7873 	  if (!section->segment_mark)
   7874 	    goto rewrite;
   7875 	  else
   7876 	    section->segment_mark = false;
   7877 	}
   7878 
   7879       return copy_elf_program_header (ibfd, obfd);
   7880     }
   7881 
   7882  rewrite:
   7883   maxpagesize = 0;
   7884   if (ibfd->xvec == obfd->xvec)
   7885     {
   7886       /* When rewriting program header, set the output maxpagesize to
   7887 	 the maximum alignment of input PT_LOAD segments.  */
   7888       Elf_Internal_Phdr *segment;
   7889       unsigned int i;
   7890       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   7891 
   7892       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7893 	   i < num_segments;
   7894 	   i++, segment++)
   7895 	if (segment->p_type == PT_LOAD
   7896 	    && maxpagesize < segment->p_align)
   7897 	  {
   7898 	    /* PR 17512: file: f17299af.  */
   7899 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   7900 	      /* xgettext:c-format */
   7901 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   7902 				    PRIx64 " is too large"),
   7903 				  ibfd, (uint64_t) segment->p_align);
   7904 	    else
   7905 	      maxpagesize = segment->p_align;
   7906 	  }
   7907     }
   7908   if (maxpagesize == 0)
   7909     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   7910 
   7911   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   7912 }
   7913 
   7914 /* Initialize private output section information from input section.  */
   7915 
   7916 bool
   7917 _bfd_elf_init_private_section_data (bfd *ibfd,
   7918 				    asection *isec,
   7919 				    bfd *obfd,
   7920 				    asection *osec,
   7921 				    struct bfd_link_info *link_info)
   7922 
   7923 {
   7924   Elf_Internal_Shdr *ihdr, *ohdr;
   7925   bool final_link = (link_info != NULL
   7926 		     && !bfd_link_relocatable (link_info));
   7927 
   7928   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7929       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7930     return true;
   7931 
   7932   BFD_ASSERT (elf_section_data (osec) != NULL);
   7933 
   7934   /* If this is a known ABI section, ELF section type and flags may
   7935      have been set up when OSEC was created.  For normal sections we
   7936      allow the user to override the type and flags other than
   7937      SHF_MASKOS and SHF_MASKPROC.  */
   7938   if (elf_section_type (osec) == SHT_PROGBITS
   7939       || elf_section_type (osec) == SHT_NOTE
   7940       || elf_section_type (osec) == SHT_NOBITS)
   7941     elf_section_type (osec) = SHT_NULL;
   7942   /* For objcopy and relocatable link, copy the ELF section type from
   7943      the input file if the BFD section flags are the same.  (If they
   7944      are different the user may be doing something like
   7945      "objcopy --set-section-flags .text=alloc,data".)  For a final
   7946      link allow some flags that the linker clears to differ.  */
   7947   if (elf_section_type (osec) == SHT_NULL
   7948       && (osec->flags == isec->flags
   7949 	  || (final_link
   7950 	      && ((osec->flags ^ isec->flags)
   7951 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   7952     elf_section_type (osec) = elf_section_type (isec);
   7953 
   7954   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   7955   elf_section_flags (osec) = (elf_section_flags (isec)
   7956 			      & (SHF_MASKOS | SHF_MASKPROC));
   7957 
   7958   /* Copy sh_info from input for mbind section.  */
   7959   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   7960       && elf_section_flags (isec) & SHF_GNU_MBIND)
   7961     elf_section_data (osec)->this_hdr.sh_info
   7962       = elf_section_data (isec)->this_hdr.sh_info;
   7963 
   7964   /* Set things up for objcopy and relocatable link.  The output
   7965      SHT_GROUP section will have its elf_next_in_group pointing back
   7966      to the input group members.  Ignore linker created group section.
   7967      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   7968   if ((link_info == NULL
   7969        || !link_info->resolve_section_groups)
   7970       && (elf_sec_group (isec) == NULL
   7971 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   7972     {
   7973       if (elf_section_flags (isec) & SHF_GROUP)
   7974 	elf_section_flags (osec) |= SHF_GROUP;
   7975       elf_next_in_group (osec) = elf_next_in_group (isec);
   7976       elf_section_data (osec)->group = elf_section_data (isec)->group;
   7977     }
   7978 
   7979   /* If not decompress, preserve SHF_COMPRESSED.  */
   7980   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   7981     elf_section_flags (osec) |= (elf_section_flags (isec)
   7982 				 & SHF_COMPRESSED);
   7983 
   7984   ihdr = &elf_section_data (isec)->this_hdr;
   7985 
   7986   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   7987      don't use the output section of the linked-to section since it
   7988      may be NULL at this point.  */
   7989   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   7990     {
   7991       ohdr = &elf_section_data (osec)->this_hdr;
   7992       ohdr->sh_flags |= SHF_LINK_ORDER;
   7993       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   7994     }
   7995 
   7996   osec->use_rela_p = isec->use_rela_p;
   7997 
   7998   return true;
   7999 }
   8000 
   8001 /* Copy private section information.  This copies over the entsize
   8002    field, and sometimes the info field.  */
   8003 
   8004 bool
   8005 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8006 				    asection *isec,
   8007 				    bfd *obfd,
   8008 				    asection *osec)
   8009 {
   8010   Elf_Internal_Shdr *ihdr, *ohdr;
   8011 
   8012   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8013       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8014     return true;
   8015 
   8016   ihdr = &elf_section_data (isec)->this_hdr;
   8017   ohdr = &elf_section_data (osec)->this_hdr;
   8018 
   8019   ohdr->sh_entsize = ihdr->sh_entsize;
   8020 
   8021   if (ihdr->sh_type == SHT_SYMTAB
   8022       || ihdr->sh_type == SHT_DYNSYM
   8023       || ihdr->sh_type == SHT_GNU_verneed
   8024       || ihdr->sh_type == SHT_GNU_verdef)
   8025     ohdr->sh_info = ihdr->sh_info;
   8026 
   8027   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   8028 					     NULL);
   8029 }
   8030 
   8031 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8032    necessary if we are removing either the SHT_GROUP section or any of
   8033    the group member sections.  DISCARDED is the value that a section's
   8034    output_section has if the section will be discarded, NULL when this
   8035    function is called from objcopy, bfd_abs_section_ptr when called
   8036    from the linker.  */
   8037 
   8038 bool
   8039 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8040 {
   8041   asection *isec;
   8042 
   8043   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8044     if (elf_section_type (isec) == SHT_GROUP)
   8045       {
   8046 	asection *first = elf_next_in_group (isec);
   8047 	asection *s = first;
   8048 	bfd_size_type removed = 0;
   8049 
   8050 	while (s != NULL)
   8051 	  {
   8052 	    /* If this member section is being output but the
   8053 	       SHT_GROUP section is not, then clear the group info
   8054 	       set up by _bfd_elf_copy_private_section_data.  */
   8055 	    if (s->output_section != discarded
   8056 		&& isec->output_section == discarded)
   8057 	      {
   8058 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8059 		elf_group_name (s->output_section) = NULL;
   8060 	      }
   8061 	    else
   8062 	      {
   8063 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8064 		if (s->output_section == discarded
   8065 		    && isec->output_section != discarded)
   8066 		  {
   8067 		    /* Conversely, if the member section is not being
   8068 		       output but the SHT_GROUP section is, then adjust
   8069 		       its size.  */
   8070 		    removed += 4;
   8071 		    if (elf_sec->rel.hdr != NULL
   8072 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8073 		      removed += 4;
   8074 		    if (elf_sec->rela.hdr != NULL
   8075 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8076 		      removed += 4;
   8077 		  }
   8078 		else
   8079 		  {
   8080 		    /* Also adjust for zero-sized relocation member
   8081 		       section.  */
   8082 		    if (elf_sec->rel.hdr != NULL
   8083 			&& elf_sec->rel.hdr->sh_size == 0)
   8084 		      removed += 4;
   8085 		    if (elf_sec->rela.hdr != NULL
   8086 			&& elf_sec->rela.hdr->sh_size == 0)
   8087 		      removed += 4;
   8088 		  }
   8089 	      }
   8090 	    s = elf_next_in_group (s);
   8091 	    if (s == first)
   8092 	      break;
   8093 	  }
   8094 	if (removed != 0)
   8095 	  {
   8096 	    if (discarded != NULL)
   8097 	      {
   8098 		/* If we've been called for ld -r, then we need to
   8099 		   adjust the input section size.  */
   8100 		if (isec->rawsize == 0)
   8101 		  isec->rawsize = isec->size;
   8102 		isec->size = isec->rawsize - removed;
   8103 		if (isec->size <= 4)
   8104 		  {
   8105 		    isec->size = 0;
   8106 		    isec->flags |= SEC_EXCLUDE;
   8107 		  }
   8108 	      }
   8109 	    else if (isec->output_section != NULL)
   8110 	      {
   8111 		/* Adjust the output section size when called from
   8112 		   objcopy. */
   8113 		isec->output_section->size -= removed;
   8114 		if (isec->output_section->size <= 4)
   8115 		  {
   8116 		    isec->output_section->size = 0;
   8117 		    isec->output_section->flags |= SEC_EXCLUDE;
   8118 		  }
   8119 	      }
   8120 	  }
   8121       }
   8122 
   8123   return true;
   8124 }
   8125 
   8126 /* Copy private header information.  */
   8127 
   8128 bool
   8129 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8130 {
   8131   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8132       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8133     return true;
   8134 
   8135   /* Copy over private BFD data if it has not already been copied.
   8136      This must be done here, rather than in the copy_private_bfd_data
   8137      entry point, because the latter is called after the section
   8138      contents have been set, which means that the program headers have
   8139      already been worked out.  */
   8140   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8141     {
   8142       if (! copy_private_bfd_data (ibfd, obfd))
   8143 	return false;
   8144     }
   8145 
   8146   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8147 }
   8148 
   8149 /* Copy private symbol information.  If this symbol is in a section
   8150    which we did not map into a BFD section, try to map the section
   8151    index correctly.  We use special macro definitions for the mapped
   8152    section indices; these definitions are interpreted by the
   8153    swap_out_syms function.  */
   8154 
   8155 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8156 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8157 #define MAP_STRTAB    (SHN_HIOS + 3)
   8158 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8159 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8160 
   8161 bool
   8162 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8163 				   asymbol *isymarg,
   8164 				   bfd *obfd,
   8165 				   asymbol *osymarg)
   8166 {
   8167   elf_symbol_type *isym, *osym;
   8168 
   8169   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8170       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8171     return true;
   8172 
   8173   isym = elf_symbol_from (isymarg);
   8174   osym = elf_symbol_from (osymarg);
   8175 
   8176   if (isym != NULL
   8177       && isym->internal_elf_sym.st_shndx != 0
   8178       && osym != NULL
   8179       && bfd_is_abs_section (isym->symbol.section))
   8180     {
   8181       unsigned int shndx;
   8182 
   8183       shndx = isym->internal_elf_sym.st_shndx;
   8184       if (shndx == elf_onesymtab (ibfd))
   8185 	shndx = MAP_ONESYMTAB;
   8186       else if (shndx == elf_dynsymtab (ibfd))
   8187 	shndx = MAP_DYNSYMTAB;
   8188       else if (shndx == elf_strtab_sec (ibfd))
   8189 	shndx = MAP_STRTAB;
   8190       else if (shndx == elf_shstrtab_sec (ibfd))
   8191 	shndx = MAP_SHSTRTAB;
   8192       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8193 	shndx = MAP_SYM_SHNDX;
   8194       osym->internal_elf_sym.st_shndx = shndx;
   8195     }
   8196 
   8197   return true;
   8198 }
   8199 
   8200 /* Swap out the symbols.  */
   8201 
   8202 static bool
   8203 swap_out_syms (bfd *abfd,
   8204 	       struct elf_strtab_hash **sttp,
   8205 	       int relocatable_p,
   8206 	       struct bfd_link_info *info)
   8207 {
   8208   const struct elf_backend_data *bed;
   8209   unsigned int symcount;
   8210   asymbol **syms;
   8211   struct elf_strtab_hash *stt;
   8212   Elf_Internal_Shdr *symtab_hdr;
   8213   Elf_Internal_Shdr *symtab_shndx_hdr;
   8214   Elf_Internal_Shdr *symstrtab_hdr;
   8215   struct elf_sym_strtab *symstrtab;
   8216   bfd_byte *outbound_syms;
   8217   bfd_byte *outbound_shndx;
   8218   unsigned long outbound_syms_index;
   8219   unsigned int idx;
   8220   unsigned int num_locals;
   8221   size_t amt;
   8222   bool name_local_sections;
   8223 
   8224   if (!elf_map_symbols (abfd, &num_locals))
   8225     return false;
   8226 
   8227   /* Dump out the symtabs.  */
   8228   stt = _bfd_elf_strtab_init ();
   8229   if (stt == NULL)
   8230     return false;
   8231 
   8232   bed = get_elf_backend_data (abfd);
   8233   symcount = bfd_get_symcount (abfd);
   8234   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8235   symtab_hdr->sh_type = SHT_SYMTAB;
   8236   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8237   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8238   symtab_hdr->sh_info = num_locals + 1;
   8239   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8240 
   8241   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8242   symstrtab_hdr->sh_type = SHT_STRTAB;
   8243 
   8244   /* Allocate buffer to swap out the .strtab section.  */
   8245   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8246       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8247     {
   8248       bfd_set_error (bfd_error_no_memory);
   8249       _bfd_elf_strtab_free (stt);
   8250       return false;
   8251     }
   8252 
   8253   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8254       || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
   8255     {
   8256     error_no_mem:
   8257       bfd_set_error (bfd_error_no_memory);
   8258     error_return:
   8259       free (symstrtab);
   8260       _bfd_elf_strtab_free (stt);
   8261       return false;
   8262     }
   8263   symtab_hdr->contents = outbound_syms;
   8264   outbound_syms_index = 0;
   8265 
   8266   outbound_shndx = NULL;
   8267 
   8268   if (elf_symtab_shndx_list (abfd))
   8269     {
   8270       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8271       if (symtab_shndx_hdr->sh_name != 0)
   8272 	{
   8273 	  if (_bfd_mul_overflow (symcount + 1,
   8274 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8275 	    goto error_no_mem;
   8276 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8277 	  if (outbound_shndx == NULL)
   8278 	    goto error_return;
   8279 
   8280 	  symtab_shndx_hdr->contents = outbound_shndx;
   8281 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8282 	  symtab_shndx_hdr->sh_size = amt;
   8283 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8284 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8285 	}
   8286       /* FIXME: What about any other headers in the list ?  */
   8287     }
   8288 
   8289   /* Now generate the data (for "contents").  */
   8290   {
   8291     /* Fill in zeroth symbol and swap it out.  */
   8292     Elf_Internal_Sym sym;
   8293     sym.st_name = 0;
   8294     sym.st_value = 0;
   8295     sym.st_size = 0;
   8296     sym.st_info = 0;
   8297     sym.st_other = 0;
   8298     sym.st_shndx = SHN_UNDEF;
   8299     sym.st_target_internal = 0;
   8300     symstrtab[0].sym = sym;
   8301     symstrtab[0].dest_index = outbound_syms_index;
   8302     outbound_syms_index++;
   8303   }
   8304 
   8305   name_local_sections
   8306     = (bed->elf_backend_name_local_section_symbols
   8307        && bed->elf_backend_name_local_section_symbols (abfd));
   8308 
   8309   syms = bfd_get_outsymbols (abfd);
   8310   for (idx = 0; idx < symcount;)
   8311     {
   8312       Elf_Internal_Sym sym;
   8313       bfd_vma value = syms[idx]->value;
   8314       elf_symbol_type *type_ptr;
   8315       flagword flags = syms[idx]->flags;
   8316       int type;
   8317 
   8318       if (!name_local_sections
   8319 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8320 	{
   8321 	  /* Local section symbols have no name.  */
   8322 	  sym.st_name = (unsigned long) -1;
   8323 	}
   8324       else
   8325 	{
   8326 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8327 	     to get the final offset for st_name.  */
   8328 	  sym.st_name
   8329 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   8330 						   false);
   8331 	  if (sym.st_name == (unsigned long) -1)
   8332 	    goto error_return;
   8333 	}
   8334 
   8335       type_ptr = elf_symbol_from (syms[idx]);
   8336 
   8337       if ((flags & BSF_SECTION_SYM) == 0
   8338 	  && bfd_is_com_section (syms[idx]->section))
   8339 	{
   8340 	  /* ELF common symbols put the alignment into the `value' field,
   8341 	     and the size into the `size' field.  This is backwards from
   8342 	     how BFD handles it, so reverse it here.  */
   8343 	  sym.st_size = value;
   8344 	  if (type_ptr == NULL
   8345 	      || type_ptr->internal_elf_sym.st_value == 0)
   8346 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8347 	  else
   8348 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8349 	  sym.st_shndx = _bfd_elf_section_from_bfd_section
   8350 	    (abfd, syms[idx]->section);
   8351 	}
   8352       else
   8353 	{
   8354 	  asection *sec = syms[idx]->section;
   8355 	  unsigned int shndx;
   8356 
   8357 	  if (sec->output_section)
   8358 	    {
   8359 	      value += sec->output_offset;
   8360 	      sec = sec->output_section;
   8361 	    }
   8362 
   8363 	  /* Don't add in the section vma for relocatable output.  */
   8364 	  if (! relocatable_p)
   8365 	    value += sec->vma;
   8366 	  sym.st_value = value;
   8367 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8368 
   8369 	  if (bfd_is_abs_section (sec)
   8370 	      && type_ptr != NULL
   8371 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8372 	    {
   8373 	      /* This symbol is in a real ELF section which we did
   8374 		 not create as a BFD section.  Undo the mapping done
   8375 		 by copy_private_symbol_data.  */
   8376 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8377 	      switch (shndx)
   8378 		{
   8379 		case MAP_ONESYMTAB:
   8380 		  shndx = elf_onesymtab (abfd);
   8381 		  break;
   8382 		case MAP_DYNSYMTAB:
   8383 		  shndx = elf_dynsymtab (abfd);
   8384 		  break;
   8385 		case MAP_STRTAB:
   8386 		  shndx = elf_strtab_sec (abfd);
   8387 		  break;
   8388 		case MAP_SHSTRTAB:
   8389 		  shndx = elf_shstrtab_sec (abfd);
   8390 		  break;
   8391 		case MAP_SYM_SHNDX:
   8392 		  if (elf_symtab_shndx_list (abfd))
   8393 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8394 		  break;
   8395 		case SHN_COMMON:
   8396 		case SHN_ABS:
   8397 		  shndx = SHN_ABS;
   8398 		  break;
   8399 		default:
   8400 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8401 		    {
   8402 		      if (bed->symbol_section_index)
   8403 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8404 		      /* Otherwise just leave the index alone.  */
   8405 		    }
   8406 		  else
   8407 		    {
   8408 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   8409 			_bfd_error_handler (_("%pB: \
   8410 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   8411 					  abfd, shndx);
   8412 		      shndx = SHN_ABS;
   8413 		    }
   8414 		  break;
   8415 		}
   8416 	    }
   8417 	  else
   8418 	    {
   8419 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8420 
   8421 	      if (shndx == SHN_BAD)
   8422 		{
   8423 		  asection *sec2;
   8424 
   8425 		  /* Writing this would be a hell of a lot easier if
   8426 		     we had some decent documentation on bfd, and
   8427 		     knew what to expect of the library, and what to
   8428 		     demand of applications.  For example, it
   8429 		     appears that `objcopy' might not set the
   8430 		     section of a symbol to be a section that is
   8431 		     actually in the output file.  */
   8432 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   8433 		  if (sec2 != NULL)
   8434 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   8435 		  if (shndx == SHN_BAD)
   8436 		    {
   8437 		      /* xgettext:c-format */
   8438 		      _bfd_error_handler
   8439 			(_("unable to find equivalent output section"
   8440 			   " for symbol '%s' from section '%s'"),
   8441 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   8442 			 sec->name);
   8443 		      bfd_set_error (bfd_error_invalid_operation);
   8444 		      goto error_return;
   8445 		    }
   8446 		}
   8447 	    }
   8448 
   8449 	  sym.st_shndx = shndx;
   8450 	}
   8451 
   8452       if ((flags & BSF_THREAD_LOCAL) != 0)
   8453 	type = STT_TLS;
   8454       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8455 	type = STT_GNU_IFUNC;
   8456       else if ((flags & BSF_FUNCTION) != 0)
   8457 	type = STT_FUNC;
   8458       else if ((flags & BSF_OBJECT) != 0)
   8459 	type = STT_OBJECT;
   8460       else if ((flags & BSF_RELC) != 0)
   8461 	type = STT_RELC;
   8462       else if ((flags & BSF_SRELC) != 0)
   8463 	type = STT_SRELC;
   8464       else
   8465 	type = STT_NOTYPE;
   8466 
   8467       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8468 	type = STT_TLS;
   8469 
   8470       /* Processor-specific types.  */
   8471       if (type_ptr != NULL
   8472 	  && bed->elf_backend_get_symbol_type)
   8473 	type = ((*bed->elf_backend_get_symbol_type)
   8474 		(&type_ptr->internal_elf_sym, type));
   8475 
   8476       if (flags & BSF_SECTION_SYM)
   8477 	{
   8478 	  if (flags & BSF_GLOBAL)
   8479 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   8480 	  else
   8481 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   8482 	}
   8483       else if (bfd_is_com_section (syms[idx]->section))
   8484 	{
   8485 	  if (type != STT_TLS)
   8486 	    {
   8487 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   8488 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   8489 			? STT_COMMON : STT_OBJECT);
   8490 	      else
   8491 		type = ((flags & BSF_ELF_COMMON) != 0
   8492 			? STT_COMMON : STT_OBJECT);
   8493 	    }
   8494 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   8495 	}
   8496       else if (bfd_is_und_section (syms[idx]->section))
   8497 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   8498 				    ? STB_WEAK
   8499 				    : STB_GLOBAL),
   8500 				   type);
   8501       else if (flags & BSF_FILE)
   8502 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   8503       else
   8504 	{
   8505 	  int bind = STB_LOCAL;
   8506 
   8507 	  if (flags & BSF_LOCAL)
   8508 	    bind = STB_LOCAL;
   8509 	  else if (flags & BSF_GNU_UNIQUE)
   8510 	    bind = STB_GNU_UNIQUE;
   8511 	  else if (flags & BSF_WEAK)
   8512 	    bind = STB_WEAK;
   8513 	  else if (flags & BSF_GLOBAL)
   8514 	    bind = STB_GLOBAL;
   8515 
   8516 	  sym.st_info = ELF_ST_INFO (bind, type);
   8517 	}
   8518 
   8519       if (type_ptr != NULL)
   8520 	{
   8521 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   8522 	  sym.st_target_internal
   8523 	    = type_ptr->internal_elf_sym.st_target_internal;
   8524 	}
   8525       else
   8526 	{
   8527 	  sym.st_other = 0;
   8528 	  sym.st_target_internal = 0;
   8529 	}
   8530 
   8531       idx++;
   8532       symstrtab[idx].sym = sym;
   8533       symstrtab[idx].dest_index = outbound_syms_index;
   8534 
   8535       outbound_syms_index++;
   8536     }
   8537 
   8538   /* Finalize the .strtab section.  */
   8539   _bfd_elf_strtab_finalize (stt);
   8540 
   8541   /* Swap out the .strtab section.  */
   8542   for (idx = 0; idx <= symcount; idx++)
   8543     {
   8544       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   8545       if (elfsym->sym.st_name == (unsigned long) -1)
   8546 	elfsym->sym.st_name = 0;
   8547       else
   8548 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   8549 						      elfsym->sym.st_name);
   8550       if (info && info->callbacks->ctf_new_symbol)
   8551 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   8552 					 &elfsym->sym);
   8553 
   8554       /* Inform the linker of the addition of this symbol.  */
   8555 
   8556       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   8557 			       (outbound_syms
   8558 				+ (elfsym->dest_index
   8559 				   * bed->s->sizeof_sym)),
   8560 			       NPTR_ADD (outbound_shndx,
   8561 					 (elfsym->dest_index
   8562 					  * sizeof (Elf_External_Sym_Shndx))));
   8563     }
   8564   free (symstrtab);
   8565 
   8566   *sttp = stt;
   8567   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   8568   symstrtab_hdr->sh_type = SHT_STRTAB;
   8569   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   8570   symstrtab_hdr->sh_addr = 0;
   8571   symstrtab_hdr->sh_entsize = 0;
   8572   symstrtab_hdr->sh_link = 0;
   8573   symstrtab_hdr->sh_info = 0;
   8574   symstrtab_hdr->sh_addralign = 1;
   8575 
   8576   return true;
   8577 }
   8578 
   8579 /* Return the number of bytes required to hold the symtab vector.
   8580 
   8581    Note that we base it on the count plus 1, since we will null terminate
   8582    the vector allocated based on this size.  However, the ELF symbol table
   8583    always has a dummy entry as symbol #0, so it ends up even.  */
   8584 
   8585 long
   8586 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   8587 {
   8588   bfd_size_type symcount;
   8589   long symtab_size;
   8590   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   8591 
   8592   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8593   if (symcount > LONG_MAX / sizeof (asymbol *))
   8594     {
   8595       bfd_set_error (bfd_error_file_too_big);
   8596       return -1;
   8597     }
   8598   symtab_size = symcount * (sizeof (asymbol *));
   8599   if (symcount == 0)
   8600     symtab_size = sizeof (asymbol *);
   8601   else if (!bfd_write_p (abfd))
   8602     {
   8603       ufile_ptr filesize = bfd_get_file_size (abfd);
   8604 
   8605       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   8606 	{
   8607 	  bfd_set_error (bfd_error_file_truncated);
   8608 	  return -1;
   8609 	}
   8610     }
   8611 
   8612   return symtab_size;
   8613 }
   8614 
   8615 long
   8616 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   8617 {
   8618   bfd_size_type symcount;
   8619   long symtab_size;
   8620   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   8621 
   8622   if (elf_dynsymtab (abfd) == 0)
   8623     {
   8624       bfd_set_error (bfd_error_invalid_operation);
   8625       return -1;
   8626     }
   8627 
   8628   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8629   if (symcount > LONG_MAX / sizeof (asymbol *))
   8630     {
   8631       bfd_set_error (bfd_error_file_too_big);
   8632       return -1;
   8633     }
   8634   symtab_size = symcount * (sizeof (asymbol *));
   8635   if (symcount == 0)
   8636     symtab_size = sizeof (asymbol *);
   8637   else if (!bfd_write_p (abfd))
   8638     {
   8639       ufile_ptr filesize = bfd_get_file_size (abfd);
   8640 
   8641       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   8642 	{
   8643 	  bfd_set_error (bfd_error_file_truncated);
   8644 	  return -1;
   8645 	}
   8646     }
   8647 
   8648   return symtab_size;
   8649 }
   8650 
   8651 long
   8652 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   8653 {
   8654   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   8655     {
   8656       /* Sanity check reloc section size.  */
   8657       ufile_ptr filesize = bfd_get_file_size (abfd);
   8658 
   8659       if (filesize != 0)
   8660 	{
   8661 	  struct bfd_elf_section_data *d = elf_section_data (asect);
   8662 	  bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
   8663 	  bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
   8664 
   8665 	  if (rel_size + rela_size > filesize
   8666 	      || rel_size + rela_size < rel_size)
   8667 	    {
   8668 	      bfd_set_error (bfd_error_file_truncated);
   8669 	      return -1;
   8670 	    }
   8671 	}
   8672     }
   8673 
   8674 #if SIZEOF_LONG == SIZEOF_INT
   8675   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   8676     {
   8677       bfd_set_error (bfd_error_file_too_big);
   8678       return -1;
   8679     }
   8680 #endif
   8681   return (asect->reloc_count + 1L) * sizeof (arelent *);
   8682 }
   8683 
   8684 /* Canonicalize the relocs.  */
   8685 
   8686 long
   8687 _bfd_elf_canonicalize_reloc (bfd *abfd,
   8688 			     sec_ptr section,
   8689 			     arelent **relptr,
   8690 			     asymbol **symbols)
   8691 {
   8692   arelent *tblptr;
   8693   unsigned int i;
   8694   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8695 
   8696   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   8697     return -1;
   8698 
   8699   tblptr = section->relocation;
   8700   for (i = 0; i < section->reloc_count; i++)
   8701     *relptr++ = tblptr++;
   8702 
   8703   *relptr = NULL;
   8704 
   8705   return section->reloc_count;
   8706 }
   8707 
   8708 long
   8709 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   8710 {
   8711   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8712   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   8713 
   8714   if (symcount >= 0)
   8715     abfd->symcount = symcount;
   8716   return symcount;
   8717 }
   8718 
   8719 long
   8720 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   8721 				      asymbol **allocation)
   8722 {
   8723   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8724   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   8725 
   8726   if (symcount >= 0)
   8727     abfd->dynsymcount = symcount;
   8728   return symcount;
   8729 }
   8730 
   8731 /* Return the size required for the dynamic reloc entries.  Any loadable
   8732    section that was actually installed in the BFD, and has type SHT_REL
   8733    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   8734    dynamic reloc section.  */
   8735 
   8736 long
   8737 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   8738 {
   8739   bfd_size_type count, ext_rel_size;
   8740   asection *s;
   8741 
   8742   if (elf_dynsymtab (abfd) == 0)
   8743     {
   8744       bfd_set_error (bfd_error_invalid_operation);
   8745       return -1;
   8746     }
   8747 
   8748   count = 1;
   8749   ext_rel_size = 0;
   8750   for (s = abfd->sections; s != NULL; s = s->next)
   8751     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8752 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8753 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8754       {
   8755 	ext_rel_size += s->size;
   8756 	if (ext_rel_size < s->size)
   8757 	  {
   8758 	    bfd_set_error (bfd_error_file_truncated);
   8759 	    return -1;
   8760 	  }
   8761 	count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8762 	if (count > LONG_MAX / sizeof (arelent *))
   8763 	  {
   8764 	    bfd_set_error (bfd_error_file_too_big);
   8765 	    return -1;
   8766 	  }
   8767       }
   8768   if (count > 1 && !bfd_write_p (abfd))
   8769     {
   8770       /* Sanity check reloc section sizes.  */
   8771       ufile_ptr filesize = bfd_get_file_size (abfd);
   8772       if (filesize != 0 && ext_rel_size > filesize)
   8773 	{
   8774 	  bfd_set_error (bfd_error_file_truncated);
   8775 	  return -1;
   8776 	}
   8777     }
   8778   return count * sizeof (arelent *);
   8779 }
   8780 
   8781 /* Canonicalize the dynamic relocation entries.  Note that we return the
   8782    dynamic relocations as a single block, although they are actually
   8783    associated with particular sections; the interface, which was
   8784    designed for SunOS style shared libraries, expects that there is only
   8785    one set of dynamic relocs.  Any loadable section that was actually
   8786    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   8787    dynamic symbol table, is considered to be a dynamic reloc section.  */
   8788 
   8789 long
   8790 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   8791 				     arelent **storage,
   8792 				     asymbol **syms)
   8793 {
   8794   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   8795   asection *s;
   8796   long ret;
   8797 
   8798   if (elf_dynsymtab (abfd) == 0)
   8799     {
   8800       bfd_set_error (bfd_error_invalid_operation);
   8801       return -1;
   8802     }
   8803 
   8804   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   8805   ret = 0;
   8806   for (s = abfd->sections; s != NULL; s = s->next)
   8807     {
   8808       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8809 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8810 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8811 	{
   8812 	  arelent *p;
   8813 	  long count, i;
   8814 
   8815 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   8816 	    return -1;
   8817 	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8818 	  p = s->relocation;
   8819 	  for (i = 0; i < count; i++)
   8820 	    *storage++ = p++;
   8821 	  ret += count;
   8822 	}
   8823     }
   8824 
   8825   *storage = NULL;
   8826 
   8827   return ret;
   8828 }
   8829 
   8830 /* Read in the version information.  */
   8832 
   8833 bool
   8834 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   8835 {
   8836   bfd_byte *contents = NULL;
   8837   unsigned int freeidx = 0;
   8838   size_t amt;
   8839 
   8840   if (elf_dynverref (abfd) != 0)
   8841     {
   8842       Elf_Internal_Shdr *hdr;
   8843       Elf_External_Verneed *everneed;
   8844       Elf_Internal_Verneed *iverneed;
   8845       unsigned int i;
   8846       bfd_byte *contents_end;
   8847 
   8848       hdr = &elf_tdata (abfd)->dynverref_hdr;
   8849 
   8850       if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   8851 	{
   8852 	error_return_bad_verref:
   8853 	  _bfd_error_handler
   8854 	    (_("%pB: .gnu.version_r invalid entry"), abfd);
   8855 	  bfd_set_error (bfd_error_bad_value);
   8856 	error_return_verref:
   8857 	  elf_tdata (abfd)->verref = NULL;
   8858 	  elf_tdata (abfd)->cverrefs = 0;
   8859 	  goto error_return;
   8860 	}
   8861 
   8862       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   8863 	goto error_return_verref;
   8864       contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   8865       if (contents == NULL)
   8866 	goto error_return_verref;
   8867 
   8868       if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
   8869 	{
   8870 	  bfd_set_error (bfd_error_file_too_big);
   8871 	  goto error_return_verref;
   8872 	}
   8873       if (amt == 0)
   8874 	goto error_return_verref;
   8875       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
   8876       if (elf_tdata (abfd)->verref == NULL)
   8877 	goto error_return_verref;
   8878 
   8879       BFD_ASSERT (sizeof (Elf_External_Verneed)
   8880 		  == sizeof (Elf_External_Vernaux));
   8881       contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
   8882       everneed = (Elf_External_Verneed *) contents;
   8883       iverneed = elf_tdata (abfd)->verref;
   8884       for (i = 0; i < hdr->sh_info; i++, iverneed++)
   8885 	{
   8886 	  Elf_External_Vernaux *evernaux;
   8887 	  Elf_Internal_Vernaux *ivernaux;
   8888 	  unsigned int j;
   8889 
   8890 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   8891 
   8892 	  iverneed->vn_bfd = abfd;
   8893 
   8894 	  iverneed->vn_filename =
   8895 	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8896 					     iverneed->vn_file);
   8897 	  if (iverneed->vn_filename == NULL)
   8898 	    goto error_return_bad_verref;
   8899 
   8900 	  if (iverneed->vn_cnt == 0)
   8901 	    iverneed->vn_auxptr = NULL;
   8902 	  else
   8903 	    {
   8904 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   8905 				     sizeof (Elf_Internal_Vernaux), &amt))
   8906 		{
   8907 		  bfd_set_error (bfd_error_file_too_big);
   8908 		  goto error_return_verref;
   8909 		}
   8910 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   8911 		bfd_alloc (abfd, amt);
   8912 	      if (iverneed->vn_auxptr == NULL)
   8913 		goto error_return_verref;
   8914 	    }
   8915 
   8916 	  if (iverneed->vn_aux
   8917 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8918 	    goto error_return_bad_verref;
   8919 
   8920 	  evernaux = ((Elf_External_Vernaux *)
   8921 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   8922 	  ivernaux = iverneed->vn_auxptr;
   8923 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   8924 	    {
   8925 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   8926 
   8927 	      ivernaux->vna_nodename =
   8928 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8929 						 ivernaux->vna_name);
   8930 	      if (ivernaux->vna_nodename == NULL)
   8931 		goto error_return_bad_verref;
   8932 
   8933 	      if (ivernaux->vna_other > freeidx)
   8934 		freeidx = ivernaux->vna_other;
   8935 
   8936 	      ivernaux->vna_nextptr = NULL;
   8937 	      if (ivernaux->vna_next == 0)
   8938 		{
   8939 		  iverneed->vn_cnt = j + 1;
   8940 		  break;
   8941 		}
   8942 	      if (j + 1 < iverneed->vn_cnt)
   8943 		ivernaux->vna_nextptr = ivernaux + 1;
   8944 
   8945 	      if (ivernaux->vna_next
   8946 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   8947 		goto error_return_bad_verref;
   8948 
   8949 	      evernaux = ((Elf_External_Vernaux *)
   8950 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   8951 	    }
   8952 
   8953 	  iverneed->vn_nextref = NULL;
   8954 	  if (iverneed->vn_next == 0)
   8955 	    break;
   8956 	  if (i + 1 < hdr->sh_info)
   8957 	    iverneed->vn_nextref = iverneed + 1;
   8958 
   8959 	  if (iverneed->vn_next
   8960 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8961 	    goto error_return_bad_verref;
   8962 
   8963 	  everneed = ((Elf_External_Verneed *)
   8964 		      ((bfd_byte *) everneed + iverneed->vn_next));
   8965 	}
   8966       elf_tdata (abfd)->cverrefs = i;
   8967 
   8968       free (contents);
   8969       contents = NULL;
   8970     }
   8971 
   8972   if (elf_dynverdef (abfd) != 0)
   8973     {
   8974       Elf_Internal_Shdr *hdr;
   8975       Elf_External_Verdef *everdef;
   8976       Elf_Internal_Verdef *iverdef;
   8977       Elf_Internal_Verdef *iverdefarr;
   8978       Elf_Internal_Verdef iverdefmem;
   8979       unsigned int i;
   8980       unsigned int maxidx;
   8981       bfd_byte *contents_end_def, *contents_end_aux;
   8982 
   8983       hdr = &elf_tdata (abfd)->dynverdef_hdr;
   8984 
   8985       if (hdr->sh_size < sizeof (Elf_External_Verdef))
   8986 	{
   8987 	error_return_bad_verdef:
   8988 	  _bfd_error_handler
   8989 	    (_("%pB: .gnu.version_d invalid entry"), abfd);
   8990 	  bfd_set_error (bfd_error_bad_value);
   8991 	error_return_verdef:
   8992 	  elf_tdata (abfd)->verdef = NULL;
   8993 	  elf_tdata (abfd)->cverdefs = 0;
   8994 	  goto error_return;
   8995 	}
   8996 
   8997       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   8998 	goto error_return_verdef;
   8999       contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   9000       if (contents == NULL)
   9001 	goto error_return_verdef;
   9002 
   9003       BFD_ASSERT (sizeof (Elf_External_Verdef)
   9004 		  >= sizeof (Elf_External_Verdaux));
   9005       contents_end_def = contents + hdr->sh_size
   9006 			 - sizeof (Elf_External_Verdef);
   9007       contents_end_aux = contents + hdr->sh_size
   9008 			 - sizeof (Elf_External_Verdaux);
   9009 
   9010       /* We know the number of entries in the section but not the maximum
   9011 	 index.  Therefore we have to run through all entries and find
   9012 	 the maximum.  */
   9013       everdef = (Elf_External_Verdef *) contents;
   9014       maxidx = 0;
   9015       for (i = 0; i < hdr->sh_info; ++i)
   9016 	{
   9017 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9018 
   9019 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9020 	    goto error_return_bad_verdef;
   9021 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9022 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9023 
   9024 	  if (iverdefmem.vd_next == 0)
   9025 	    break;
   9026 
   9027 	  if (iverdefmem.vd_next
   9028 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9029 	    goto error_return_bad_verdef;
   9030 
   9031 	  everdef = ((Elf_External_Verdef *)
   9032 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9033 	}
   9034 
   9035       if (default_imported_symver)
   9036 	{
   9037 	  if (freeidx > maxidx)
   9038 	    maxidx = ++freeidx;
   9039 	  else
   9040 	    freeidx = ++maxidx;
   9041 	}
   9042       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9043 	{
   9044 	  bfd_set_error (bfd_error_file_too_big);
   9045 	  goto error_return_verdef;
   9046 	}
   9047       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9048       if (elf_tdata (abfd)->verdef == NULL)
   9049 	goto error_return_verdef;
   9050 
   9051       elf_tdata (abfd)->cverdefs = maxidx;
   9052 
   9053       everdef = (Elf_External_Verdef *) contents;
   9054       iverdefarr = elf_tdata (abfd)->verdef;
   9055       for (i = 0; i < hdr->sh_info; i++)
   9056 	{
   9057 	  Elf_External_Verdaux *everdaux;
   9058 	  Elf_Internal_Verdaux *iverdaux;
   9059 	  unsigned int j;
   9060 
   9061 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9062 
   9063 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9064 	    goto error_return_bad_verdef;
   9065 
   9066 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9067 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9068 
   9069 	  iverdef->vd_bfd = abfd;
   9070 
   9071 	  if (iverdef->vd_cnt == 0)
   9072 	    iverdef->vd_auxptr = NULL;
   9073 	  else
   9074 	    {
   9075 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9076 				     sizeof (Elf_Internal_Verdaux), &amt))
   9077 		{
   9078 		  bfd_set_error (bfd_error_file_too_big);
   9079 		  goto error_return_verdef;
   9080 		}
   9081 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9082 		bfd_alloc (abfd, amt);
   9083 	      if (iverdef->vd_auxptr == NULL)
   9084 		goto error_return_verdef;
   9085 	    }
   9086 
   9087 	  if (iverdef->vd_aux
   9088 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9089 	    goto error_return_bad_verdef;
   9090 
   9091 	  everdaux = ((Elf_External_Verdaux *)
   9092 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9093 	  iverdaux = iverdef->vd_auxptr;
   9094 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9095 	    {
   9096 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9097 
   9098 	      iverdaux->vda_nodename =
   9099 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9100 						 iverdaux->vda_name);
   9101 	      if (iverdaux->vda_nodename == NULL)
   9102 		goto error_return_bad_verdef;
   9103 
   9104 	      iverdaux->vda_nextptr = NULL;
   9105 	      if (iverdaux->vda_next == 0)
   9106 		{
   9107 		  iverdef->vd_cnt = j + 1;
   9108 		  break;
   9109 		}
   9110 	      if (j + 1 < iverdef->vd_cnt)
   9111 		iverdaux->vda_nextptr = iverdaux + 1;
   9112 
   9113 	      if (iverdaux->vda_next
   9114 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9115 		goto error_return_bad_verdef;
   9116 
   9117 	      everdaux = ((Elf_External_Verdaux *)
   9118 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9119 	    }
   9120 
   9121 	  iverdef->vd_nodename = NULL;
   9122 	  if (iverdef->vd_cnt)
   9123 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9124 
   9125 	  iverdef->vd_nextdef = NULL;
   9126 	  if (iverdef->vd_next == 0)
   9127 	    break;
   9128 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9129 	    iverdef->vd_nextdef = iverdef + 1;
   9130 
   9131 	  everdef = ((Elf_External_Verdef *)
   9132 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9133 	}
   9134 
   9135       free (contents);
   9136       contents = NULL;
   9137     }
   9138   else if (default_imported_symver)
   9139     {
   9140       if (freeidx < 3)
   9141 	freeidx = 3;
   9142       else
   9143 	freeidx++;
   9144 
   9145       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9146 	{
   9147 	  bfd_set_error (bfd_error_file_too_big);
   9148 	  goto error_return;
   9149 	}
   9150       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9151       if (elf_tdata (abfd)->verdef == NULL)
   9152 	goto error_return;
   9153 
   9154       elf_tdata (abfd)->cverdefs = freeidx;
   9155     }
   9156 
   9157   /* Create a default version based on the soname.  */
   9158   if (default_imported_symver)
   9159     {
   9160       Elf_Internal_Verdef *iverdef;
   9161       Elf_Internal_Verdaux *iverdaux;
   9162 
   9163       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9164 
   9165       iverdef->vd_version = VER_DEF_CURRENT;
   9166       iverdef->vd_flags = 0;
   9167       iverdef->vd_ndx = freeidx;
   9168       iverdef->vd_cnt = 1;
   9169 
   9170       iverdef->vd_bfd = abfd;
   9171 
   9172       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9173       if (iverdef->vd_nodename == NULL)
   9174 	goto error_return_verdef;
   9175       iverdef->vd_nextdef = NULL;
   9176       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9177 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9178       if (iverdef->vd_auxptr == NULL)
   9179 	goto error_return_verdef;
   9180 
   9181       iverdaux = iverdef->vd_auxptr;
   9182       iverdaux->vda_nodename = iverdef->vd_nodename;
   9183     }
   9184 
   9185   return true;
   9186 
   9187  error_return:
   9188   free (contents);
   9189   return false;
   9190 }
   9191 
   9192 asymbol *
   9194 _bfd_elf_make_empty_symbol (bfd *abfd)
   9195 {
   9196   elf_symbol_type *newsym;
   9197 
   9198   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9199   if (!newsym)
   9200     return NULL;
   9201   newsym->symbol.the_bfd = abfd;
   9202   return &newsym->symbol;
   9203 }
   9204 
   9205 void
   9206 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9207 			  asymbol *symbol,
   9208 			  symbol_info *ret)
   9209 {
   9210   bfd_symbol_info (symbol, ret);
   9211 }
   9212 
   9213 /* Return whether a symbol name implies a local symbol.  Most targets
   9214    use this function for the is_local_label_name entry point, but some
   9215    override it.  */
   9216 
   9217 bool
   9218 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9219 			      const char *name)
   9220 {
   9221   /* Normal local symbols start with ``.L''.  */
   9222   if (name[0] == '.' && name[1] == 'L')
   9223     return true;
   9224 
   9225   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9226      DWARF debugging symbols starting with ``..''.  */
   9227   if (name[0] == '.' && name[1] == '.')
   9228     return true;
   9229 
   9230   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9231      emitting DWARF debugging output.  I suspect this is actually a
   9232      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9233      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9234      underscore to be emitted on some ELF targets).  For ease of use,
   9235      we treat such symbols as local.  */
   9236   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9237     return true;
   9238 
   9239   /* Treat assembler generated fake symbols, dollar local labels and
   9240      forward-backward labels (aka local labels) as locals.
   9241      These labels have the form:
   9242 
   9243        L0^A.*				       (fake symbols)
   9244 
   9245        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9246 
   9247      Versions which start with .L will have already been matched above,
   9248      so we only need to match the rest.  */
   9249   if (name[0] == 'L' && ISDIGIT (name[1]))
   9250     {
   9251       bool ret = false;
   9252       const char * p;
   9253       char c;
   9254 
   9255       for (p = name + 2; (c = *p); p++)
   9256 	{
   9257 	  if (c == 1 || c == 2)
   9258 	    {
   9259 	      if (c == 1 && p == name + 2)
   9260 		/* A fake symbol.  */
   9261 		return true;
   9262 
   9263 	      /* FIXME: We are being paranoid here and treating symbols like
   9264 		 L0^Bfoo as if there were non-local, on the grounds that the
   9265 		 assembler will never generate them.  But can any symbol
   9266 		 containing an ASCII value in the range 1-31 ever be anything
   9267 		 other than some kind of local ?  */
   9268 	      ret = true;
   9269 	    }
   9270 
   9271 	  if (! ISDIGIT (c))
   9272 	    {
   9273 	      ret = false;
   9274 	      break;
   9275 	    }
   9276 	}
   9277       return ret;
   9278     }
   9279 
   9280   return false;
   9281 }
   9282 
   9283 alent *
   9284 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9285 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9286 {
   9287   abort ();
   9288   return NULL;
   9289 }
   9290 
   9291 bool
   9292 _bfd_elf_set_arch_mach (bfd *abfd,
   9293 			enum bfd_architecture arch,
   9294 			unsigned long machine)
   9295 {
   9296   /* If this isn't the right architecture for this backend, and this
   9297      isn't the generic backend, fail.  */
   9298   if (arch != get_elf_backend_data (abfd)->arch
   9299       && arch != bfd_arch_unknown
   9300       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9301     return false;
   9302 
   9303   return bfd_default_set_arch_mach (abfd, arch, machine);
   9304 }
   9305 
   9306 /* Find the nearest line to a particular section and offset,
   9307    for error reporting.  */
   9308 
   9309 bool
   9310 _bfd_elf_find_nearest_line (bfd *abfd,
   9311 			    asymbol **symbols,
   9312 			    asection *section,
   9313 			    bfd_vma offset,
   9314 			    const char **filename_ptr,
   9315 			    const char **functionname_ptr,
   9316 			    unsigned int *line_ptr,
   9317 			    unsigned int *discriminator_ptr)
   9318 {
   9319   return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
   9320 					      offset, filename_ptr,
   9321 					      functionname_ptr, line_ptr,
   9322 					      discriminator_ptr);
   9323 }
   9324 
   9325 /* Find the nearest line to a particular section and offset,
   9326    for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
   9327    can be optionally specified.  */
   9328 
   9329 bool
   9330 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
   9331 				     const char *alt_filename,
   9332 				     asymbol **symbols,
   9333 				     asection *section,
   9334 				     bfd_vma offset,
   9335 				     const char **filename_ptr,
   9336 				     const char **functionname_ptr,
   9337 				     unsigned int *line_ptr,
   9338 				     unsigned int *discriminator_ptr)
   9339 {
   9340   bool found;
   9341 
   9342   if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
   9343 					      section, offset, filename_ptr,
   9344 					      functionname_ptr, line_ptr,
   9345 					      discriminator_ptr,
   9346 					      dwarf_debug_sections,
   9347 					      &elf_tdata (abfd)->dwarf2_find_line_info))
   9348     return true;
   9349 
   9350   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   9351 				     filename_ptr, functionname_ptr, line_ptr))
   9352     {
   9353       if (!*functionname_ptr)
   9354 	_bfd_elf_find_function (abfd, symbols, section, offset,
   9355 				*filename_ptr ? NULL : filename_ptr,
   9356 				functionname_ptr);
   9357       return true;
   9358     }
   9359 
   9360   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   9361 					     &found, filename_ptr,
   9362 					     functionname_ptr, line_ptr,
   9363 					     &elf_tdata (abfd)->line_info))
   9364     return false;
   9365   if (found && (*functionname_ptr || *line_ptr))
   9366     return true;
   9367 
   9368   if (symbols == NULL)
   9369     return false;
   9370 
   9371   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   9372 				filename_ptr, functionname_ptr))
   9373     return false;
   9374 
   9375   *line_ptr = 0;
   9376   return true;
   9377 }
   9378 
   9379 /* Find the line for a symbol.  */
   9380 
   9381 bool
   9382 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   9383 		    const char **filename_ptr, unsigned int *line_ptr)
   9384 {
   9385   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9386   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   9387 					filename_ptr, NULL, line_ptr, NULL,
   9388 					dwarf_debug_sections,
   9389 					&tdata->dwarf2_find_line_info);
   9390 }
   9391 
   9392 /* After a call to bfd_find_nearest_line, successive calls to
   9393    bfd_find_inliner_info can be used to get source information about
   9394    each level of function inlining that terminated at the address
   9395    passed to bfd_find_nearest_line.  Currently this is only supported
   9396    for DWARF2 with appropriate DWARF3 extensions. */
   9397 
   9398 bool
   9399 _bfd_elf_find_inliner_info (bfd *abfd,
   9400 			    const char **filename_ptr,
   9401 			    const char **functionname_ptr,
   9402 			    unsigned int *line_ptr)
   9403 {
   9404   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9405   return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   9406 					functionname_ptr, line_ptr,
   9407 					&tdata->dwarf2_find_line_info);
   9408 }
   9409 
   9410 int
   9411 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   9412 {
   9413   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9414   int ret = bed->s->sizeof_ehdr;
   9415 
   9416   if (!bfd_link_relocatable (info))
   9417     {
   9418       bfd_size_type phdr_size = elf_program_header_size (abfd);
   9419 
   9420       if (phdr_size == (bfd_size_type) -1)
   9421 	{
   9422 	  struct elf_segment_map *m;
   9423 
   9424 	  phdr_size = 0;
   9425 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   9426 	    phdr_size += bed->s->sizeof_phdr;
   9427 
   9428 	  if (phdr_size == 0)
   9429 	    phdr_size = get_program_header_size (abfd, info);
   9430 	}
   9431 
   9432       elf_program_header_size (abfd) = phdr_size;
   9433       ret += phdr_size;
   9434     }
   9435 
   9436   return ret;
   9437 }
   9438 
   9439 bool
   9440 _bfd_elf_set_section_contents (bfd *abfd,
   9441 			       sec_ptr section,
   9442 			       const void *location,
   9443 			       file_ptr offset,
   9444 			       bfd_size_type count)
   9445 {
   9446   Elf_Internal_Shdr *hdr;
   9447 
   9448   if (! abfd->output_has_begun
   9449       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   9450     return false;
   9451 
   9452   if (!count)
   9453     return true;
   9454 
   9455   hdr = &elf_section_data (section)->this_hdr;
   9456   if (hdr->sh_offset == (file_ptr) -1)
   9457     {
   9458       unsigned char *contents;
   9459 
   9460       if (bfd_section_is_ctf (section))
   9461 	/* Nothing to do with this section: the contents are generated
   9462 	   later.  */
   9463 	return true;
   9464 
   9465       if ((offset + count) > hdr->sh_size)
   9466 	{
   9467 	  _bfd_error_handler
   9468 	    (_("%pB:%pA: error: attempting to write"
   9469 	       " over the end of the section"),
   9470 	     abfd, section);
   9471 
   9472 	  bfd_set_error (bfd_error_invalid_operation);
   9473 	  return false;
   9474 	}
   9475 
   9476       contents = hdr->contents;
   9477       if (contents == NULL)
   9478 	{
   9479 	  _bfd_error_handler
   9480 	    (_("%pB:%pA: error: attempting to write"
   9481 	       " section into an empty buffer"),
   9482 	     abfd, section);
   9483 
   9484 	  bfd_set_error (bfd_error_invalid_operation);
   9485 	  return false;
   9486 	}
   9487 
   9488       memcpy (contents + offset, location, count);
   9489       return true;
   9490     }
   9491 
   9492   return _bfd_generic_set_section_contents (abfd, section,
   9493 					    location, offset, count);
   9494 }
   9495 
   9496 bool
   9497 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   9498 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   9499 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   9500 {
   9501   abort ();
   9502   return false;
   9503 }
   9504 
   9505 /* Try to convert a non-ELF reloc into an ELF one.  */
   9506 
   9507 bool
   9508 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   9509 {
   9510   /* Check whether we really have an ELF howto.  */
   9511 
   9512   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   9513     {
   9514       bfd_reloc_code_real_type code;
   9515       reloc_howto_type *howto;
   9516 
   9517       /* Alien reloc: Try to determine its type to replace it with an
   9518 	 equivalent ELF reloc.  */
   9519 
   9520       if (areloc->howto->pc_relative)
   9521 	{
   9522 	  switch (areloc->howto->bitsize)
   9523 	    {
   9524 	    case 8:
   9525 	      code = BFD_RELOC_8_PCREL;
   9526 	      break;
   9527 	    case 12:
   9528 	      code = BFD_RELOC_12_PCREL;
   9529 	      break;
   9530 	    case 16:
   9531 	      code = BFD_RELOC_16_PCREL;
   9532 	      break;
   9533 	    case 24:
   9534 	      code = BFD_RELOC_24_PCREL;
   9535 	      break;
   9536 	    case 32:
   9537 	      code = BFD_RELOC_32_PCREL;
   9538 	      break;
   9539 	    case 64:
   9540 	      code = BFD_RELOC_64_PCREL;
   9541 	      break;
   9542 	    default:
   9543 	      goto fail;
   9544 	    }
   9545 
   9546 	  howto = bfd_reloc_type_lookup (abfd, code);
   9547 
   9548 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   9549 	    {
   9550 	      if (howto->pcrel_offset)
   9551 		areloc->addend += areloc->address;
   9552 	      else
   9553 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   9554 	    }
   9555 	}
   9556       else
   9557 	{
   9558 	  switch (areloc->howto->bitsize)
   9559 	    {
   9560 	    case 8:
   9561 	      code = BFD_RELOC_8;
   9562 	      break;
   9563 	    case 14:
   9564 	      code = BFD_RELOC_14;
   9565 	      break;
   9566 	    case 16:
   9567 	      code = BFD_RELOC_16;
   9568 	      break;
   9569 	    case 26:
   9570 	      code = BFD_RELOC_26;
   9571 	      break;
   9572 	    case 32:
   9573 	      code = BFD_RELOC_32;
   9574 	      break;
   9575 	    case 64:
   9576 	      code = BFD_RELOC_64;
   9577 	      break;
   9578 	    default:
   9579 	      goto fail;
   9580 	    }
   9581 
   9582 	  howto = bfd_reloc_type_lookup (abfd, code);
   9583 	}
   9584 
   9585       if (howto)
   9586 	areloc->howto = howto;
   9587       else
   9588 	goto fail;
   9589     }
   9590 
   9591   return true;
   9592 
   9593  fail:
   9594   /* xgettext:c-format */
   9595   _bfd_error_handler (_("%pB: %s unsupported"),
   9596 		      abfd, areloc->howto->name);
   9597   bfd_set_error (bfd_error_sorry);
   9598   return false;
   9599 }
   9600 
   9601 bool
   9602 _bfd_elf_close_and_cleanup (bfd *abfd)
   9603 {
   9604   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9605   if (tdata != NULL
   9606       && (bfd_get_format (abfd) == bfd_object
   9607 	  || bfd_get_format (abfd) == bfd_core))
   9608     {
   9609       if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
   9610 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   9611       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   9612       _bfd_stab_cleanup (abfd, &tdata->line_info);
   9613     }
   9614 
   9615   return _bfd_generic_close_and_cleanup (abfd);
   9616 }
   9617 
   9618 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   9619    in the relocation's offset.  Thus we cannot allow any sort of sanity
   9620    range-checking to interfere.  There is nothing else to do in processing
   9621    this reloc.  */
   9622 
   9623 bfd_reloc_status_type
   9624 _bfd_elf_rel_vtable_reloc_fn
   9625   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   9626    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   9627    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   9628    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   9629 {
   9630   return bfd_reloc_ok;
   9631 }
   9632 
   9633 /* Elf core file support.  Much of this only works on native
   9635    toolchains, since we rely on knowing the
   9636    machine-dependent procfs structure in order to pick
   9637    out details about the corefile.  */
   9638 
   9639 #ifdef HAVE_SYS_PROCFS_H
   9640 # include <sys/procfs.h>
   9641 #endif
   9642 
   9643 /* Return a PID that identifies a "thread" for threaded cores, or the
   9644    PID of the main process for non-threaded cores.  */
   9645 
   9646 static int
   9647 elfcore_make_pid (bfd *abfd)
   9648 {
   9649   int pid;
   9650 
   9651   pid = elf_tdata (abfd)->core->lwpid;
   9652   if (pid == 0)
   9653     pid = elf_tdata (abfd)->core->pid;
   9654 
   9655   return pid;
   9656 }
   9657 
   9658 /* If there isn't a section called NAME, make one, using data from
   9659    SECT.  Note, this function will generate a reference to NAME, so
   9660    you shouldn't deallocate or overwrite it.  */
   9661 
   9662 static bool
   9663 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   9664 {
   9665   asection *sect2;
   9666 
   9667   if (bfd_get_section_by_name (abfd, name) != NULL)
   9668     return true;
   9669 
   9670   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   9671   if (sect2 == NULL)
   9672     return false;
   9673 
   9674   sect2->size = sect->size;
   9675   sect2->filepos = sect->filepos;
   9676   sect2->alignment_power = sect->alignment_power;
   9677   return true;
   9678 }
   9679 
   9680 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   9681    actually creates up to two pseudosections:
   9682    - For the single-threaded case, a section named NAME, unless
   9683      such a section already exists.
   9684    - For the multi-threaded case, a section named "NAME/PID", where
   9685      PID is elfcore_make_pid (abfd).
   9686    Both pseudosections have identical contents.  */
   9687 bool
   9688 _bfd_elfcore_make_pseudosection (bfd *abfd,
   9689 				 char *name,
   9690 				 size_t size,
   9691 				 ufile_ptr filepos)
   9692 {
   9693   char buf[100];
   9694   char *threaded_name;
   9695   size_t len;
   9696   asection *sect;
   9697 
   9698   /* Build the section name.  */
   9699 
   9700   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   9701   len = strlen (buf) + 1;
   9702   threaded_name = (char *) bfd_alloc (abfd, len);
   9703   if (threaded_name == NULL)
   9704     return false;
   9705   memcpy (threaded_name, buf, len);
   9706 
   9707   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   9708 					     SEC_HAS_CONTENTS);
   9709   if (sect == NULL)
   9710     return false;
   9711   sect->size = size;
   9712   sect->filepos = filepos;
   9713   sect->alignment_power = 2;
   9714 
   9715   return elfcore_maybe_make_sect (abfd, name, sect);
   9716 }
   9717 
   9718 static bool
   9719 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   9720 				size_t offs)
   9721 {
   9722   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   9723 						       SEC_HAS_CONTENTS);
   9724 
   9725   if (sect == NULL)
   9726     return false;
   9727 
   9728   sect->size = note->descsz - offs;
   9729   sect->filepos = note->descpos + offs;
   9730   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   9731 
   9732   return true;
   9733 }
   9734 
   9735 /* prstatus_t exists on:
   9736      solaris 2.5+
   9737      linux 2.[01] + glibc
   9738      unixware 4.2
   9739 */
   9740 
   9741 #if defined (HAVE_PRSTATUS_T)
   9742 
   9743 static bool
   9744 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   9745 {
   9746   size_t size;
   9747   int offset;
   9748 
   9749   if (note->descsz == sizeof (prstatus_t))
   9750     {
   9751       prstatus_t prstat;
   9752 
   9753       size = sizeof (prstat.pr_reg);
   9754       offset   = offsetof (prstatus_t, pr_reg);
   9755       memcpy (&prstat, note->descdata, sizeof (prstat));
   9756 
   9757       /* Do not overwrite the core signal if it
   9758 	 has already been set by another thread.  */
   9759       if (elf_tdata (abfd)->core->signal == 0)
   9760 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9761       if (elf_tdata (abfd)->core->pid == 0)
   9762 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9763 
   9764       /* pr_who exists on:
   9765 	 solaris 2.5+
   9766 	 unixware 4.2
   9767 	 pr_who doesn't exist on:
   9768 	 linux 2.[01]
   9769 	 */
   9770 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   9771       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9772 #else
   9773       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9774 #endif
   9775     }
   9776 #if defined (HAVE_PRSTATUS32_T)
   9777   else if (note->descsz == sizeof (prstatus32_t))
   9778     {
   9779       /* 64-bit host, 32-bit corefile */
   9780       prstatus32_t prstat;
   9781 
   9782       size = sizeof (prstat.pr_reg);
   9783       offset   = offsetof (prstatus32_t, pr_reg);
   9784       memcpy (&prstat, note->descdata, sizeof (prstat));
   9785 
   9786       /* Do not overwrite the core signal if it
   9787 	 has already been set by another thread.  */
   9788       if (elf_tdata (abfd)->core->signal == 0)
   9789 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9790       if (elf_tdata (abfd)->core->pid == 0)
   9791 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9792 
   9793       /* pr_who exists on:
   9794 	 solaris 2.5+
   9795 	 unixware 4.2
   9796 	 pr_who doesn't exist on:
   9797 	 linux 2.[01]
   9798 	 */
   9799 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   9800       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9801 #else
   9802       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9803 #endif
   9804     }
   9805 #endif /* HAVE_PRSTATUS32_T */
   9806   else
   9807     {
   9808       /* Fail - we don't know how to handle any other
   9809 	 note size (ie. data object type).  */
   9810       return true;
   9811     }
   9812 
   9813   /* Make a ".reg/999" section and a ".reg" section.  */
   9814   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   9815 					  size, note->descpos + offset);
   9816 }
   9817 #endif /* defined (HAVE_PRSTATUS_T) */
   9818 
   9819 /* Create a pseudosection containing the exact contents of NOTE.  */
   9820 static bool
   9821 elfcore_make_note_pseudosection (bfd *abfd,
   9822 				 char *name,
   9823 				 Elf_Internal_Note *note)
   9824 {
   9825   return _bfd_elfcore_make_pseudosection (abfd, name,
   9826 					  note->descsz, note->descpos);
   9827 }
   9828 
   9829 /* There isn't a consistent prfpregset_t across platforms,
   9830    but it doesn't matter, because we don't have to pick this
   9831    data structure apart.  */
   9832 
   9833 static bool
   9834 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   9835 {
   9836   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   9837 }
   9838 
   9839 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   9840    type of NT_PRXFPREG.  Just include the whole note's contents
   9841    literally.  */
   9842 
   9843 static bool
   9844 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   9845 {
   9846   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   9847 }
   9848 
   9849 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   9850    with a note type of NT_X86_XSTATE.  Just include the whole note's
   9851    contents literally.  */
   9852 
   9853 static bool
   9854 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   9855 {
   9856   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   9857 }
   9858 
   9859 static bool
   9860 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   9861 {
   9862   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   9863 }
   9864 
   9865 static bool
   9866 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   9867 {
   9868   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   9869 }
   9870 
   9871 static bool
   9872 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   9873 {
   9874   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
   9875 }
   9876 
   9877 static bool
   9878 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   9879 {
   9880   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
   9881 }
   9882 
   9883 static bool
   9884 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   9885 {
   9886   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
   9887 }
   9888 
   9889 static bool
   9890 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   9891 {
   9892   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
   9893 }
   9894 
   9895 static bool
   9896 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   9897 {
   9898   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
   9899 }
   9900 
   9901 static bool
   9902 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   9903 {
   9904   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
   9905 }
   9906 
   9907 static bool
   9908 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   9909 {
   9910   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
   9911 }
   9912 
   9913 static bool
   9914 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   9915 {
   9916   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
   9917 }
   9918 
   9919 static bool
   9920 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   9921 {
   9922   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
   9923 }
   9924 
   9925 static bool
   9926 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   9927 {
   9928   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
   9929 }
   9930 
   9931 static bool
   9932 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   9933 {
   9934   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
   9935 }
   9936 
   9937 static bool
   9938 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   9939 {
   9940   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
   9941 }
   9942 
   9943 static bool
   9944 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   9945 {
   9946   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
   9947 }
   9948 
   9949 static bool
   9950 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   9951 {
   9952   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   9953 }
   9954 
   9955 static bool
   9956 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   9957 {
   9958   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   9959 }
   9960 
   9961 static bool
   9962 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   9963 {
   9964   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   9965 }
   9966 
   9967 static bool
   9968 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   9969 {
   9970   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   9971 }
   9972 
   9973 static bool
   9974 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   9975 {
   9976   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   9977 }
   9978 
   9979 static bool
   9980 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   9981 {
   9982   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   9983 }
   9984 
   9985 static bool
   9986 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   9987 {
   9988   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   9989 }
   9990 
   9991 static bool
   9992 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   9993 {
   9994   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   9995 }
   9996 
   9997 static bool
   9998 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   9999 {
   10000   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   10001 }
   10002 
   10003 static bool
   10004 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   10005 {
   10006   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   10007 }
   10008 
   10009 static bool
   10010 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   10011 {
   10012   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   10013 }
   10014 
   10015 static bool
   10016 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   10017 {
   10018   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   10019 }
   10020 
   10021 static bool
   10022 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10023 {
   10024   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   10025 }
   10026 
   10027 static bool
   10028 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10029 {
   10030   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   10031 }
   10032 
   10033 static bool
   10034 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10035 {
   10036   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   10037 }
   10038 
   10039 static bool
   10040 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10041 {
   10042   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   10043 }
   10044 
   10045 static bool
   10046 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10047 {
   10048   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   10049 }
   10050 
   10051 static bool
   10052 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10053 {
   10054   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
   10055 }
   10056 
   10057 static bool
   10058 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10059 {
   10060   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
   10061 }
   10062 
   10063 static bool
   10064 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10065 {
   10066   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
   10067 					  note);
   10068 }
   10069 
   10070 static bool
   10071 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10072 {
   10073   return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
   10074 }
   10075 
   10076 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10077    successful otherwise, return FALSE.  */
   10078 
   10079 static bool
   10080 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10081 {
   10082   return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
   10083 }
   10084 
   10085 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10086    successful otherwise, return FALSE.  */
   10087 
   10088 static bool
   10089 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10090 {
   10091   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10092 }
   10093 
   10094 static bool
   10095 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10096 {
   10097   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
   10098 }
   10099 
   10100 static bool
   10101 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10102 {
   10103   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
   10104 }
   10105 
   10106 static bool
   10107 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10108 {
   10109   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
   10110 }
   10111 
   10112 static bool
   10113 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10114 {
   10115   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
   10116 }
   10117 
   10118 #if defined (HAVE_PRPSINFO_T)
   10119 typedef prpsinfo_t   elfcore_psinfo_t;
   10120 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10121 typedef prpsinfo32_t elfcore_psinfo32_t;
   10122 #endif
   10123 #endif
   10124 
   10125 #if defined (HAVE_PSINFO_T)
   10126 typedef psinfo_t   elfcore_psinfo_t;
   10127 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10128 typedef psinfo32_t elfcore_psinfo32_t;
   10129 #endif
   10130 #endif
   10131 
   10132 /* return a malloc'ed copy of a string at START which is at
   10133    most MAX bytes long, possibly without a terminating '\0'.
   10134    the copy will always have a terminating '\0'.  */
   10135 
   10136 char *
   10137 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10138 {
   10139   char *dups;
   10140   char *end = (char *) memchr (start, '\0', max);
   10141   size_t len;
   10142 
   10143   if (end == NULL)
   10144     len = max;
   10145   else
   10146     len = end - start;
   10147 
   10148   dups = (char *) bfd_alloc (abfd, len + 1);
   10149   if (dups == NULL)
   10150     return NULL;
   10151 
   10152   memcpy (dups, start, len);
   10153   dups[len] = '\0';
   10154 
   10155   return dups;
   10156 }
   10157 
   10158 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10159 static bool
   10160 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10161 {
   10162   if (note->descsz == sizeof (elfcore_psinfo_t))
   10163     {
   10164       elfcore_psinfo_t psinfo;
   10165 
   10166       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10167 
   10168 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   10169       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10170 #endif
   10171       elf_tdata (abfd)->core->program
   10172 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10173 				sizeof (psinfo.pr_fname));
   10174 
   10175       elf_tdata (abfd)->core->command
   10176 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10177 				sizeof (psinfo.pr_psargs));
   10178     }
   10179 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10180   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10181     {
   10182       /* 64-bit host, 32-bit corefile */
   10183       elfcore_psinfo32_t psinfo;
   10184 
   10185       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10186 
   10187 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10188       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10189 #endif
   10190       elf_tdata (abfd)->core->program
   10191 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10192 				sizeof (psinfo.pr_fname));
   10193 
   10194       elf_tdata (abfd)->core->command
   10195 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10196 				sizeof (psinfo.pr_psargs));
   10197     }
   10198 #endif
   10199 
   10200   else
   10201     {
   10202       /* Fail - we don't know how to handle any other
   10203 	 note size (ie. data object type).  */
   10204       return true;
   10205     }
   10206 
   10207   /* Note that for some reason, a spurious space is tacked
   10208      onto the end of the args in some (at least one anyway)
   10209      implementations, so strip it off if it exists.  */
   10210 
   10211   {
   10212     char *command = elf_tdata (abfd)->core->command;
   10213     int n = strlen (command);
   10214 
   10215     if (0 < n && command[n - 1] == ' ')
   10216       command[n - 1] = '\0';
   10217   }
   10218 
   10219   return true;
   10220 }
   10221 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10222 
   10223 #if defined (HAVE_PSTATUS_T)
   10224 static bool
   10225 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10226 {
   10227   if (note->descsz == sizeof (pstatus_t)
   10228 #if defined (HAVE_PXSTATUS_T)
   10229       || note->descsz == sizeof (pxstatus_t)
   10230 #endif
   10231       )
   10232     {
   10233       pstatus_t pstat;
   10234 
   10235       memcpy (&pstat, note->descdata, sizeof (pstat));
   10236 
   10237       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10238     }
   10239 #if defined (HAVE_PSTATUS32_T)
   10240   else if (note->descsz == sizeof (pstatus32_t))
   10241     {
   10242       /* 64-bit host, 32-bit corefile */
   10243       pstatus32_t pstat;
   10244 
   10245       memcpy (&pstat, note->descdata, sizeof (pstat));
   10246 
   10247       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10248     }
   10249 #endif
   10250   /* Could grab some more details from the "representative"
   10251      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10252      NT_LWPSTATUS note, presumably.  */
   10253 
   10254   return true;
   10255 }
   10256 #endif /* defined (HAVE_PSTATUS_T) */
   10257 
   10258 #if defined (HAVE_LWPSTATUS_T)
   10259 static bool
   10260 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10261 {
   10262   lwpstatus_t lwpstat;
   10263   char buf[100];
   10264   char *name;
   10265   size_t len;
   10266   asection *sect;
   10267 
   10268   if (note->descsz != sizeof (lwpstat)
   10269 #if defined (HAVE_LWPXSTATUS_T)
   10270       && note->descsz != sizeof (lwpxstatus_t)
   10271 #endif
   10272       )
   10273     return true;
   10274 
   10275   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10276 
   10277   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10278   /* Do not overwrite the core signal if it has already been set by
   10279      another thread.  */
   10280   if (elf_tdata (abfd)->core->signal == 0)
   10281     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10282 
   10283   /* Make a ".reg/999" section.  */
   10284 
   10285   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   10286   len = strlen (buf) + 1;
   10287   name = bfd_alloc (abfd, len);
   10288   if (name == NULL)
   10289     return false;
   10290   memcpy (name, buf, len);
   10291 
   10292   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10293   if (sect == NULL)
   10294     return false;
   10295 
   10296 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10297   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   10298   sect->filepos = note->descpos
   10299     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   10300 #endif
   10301 
   10302 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10303   sect->size = sizeof (lwpstat.pr_reg);
   10304   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   10305 #endif
   10306 
   10307   sect->alignment_power = 2;
   10308 
   10309   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   10310     return false;
   10311 
   10312   /* Make a ".reg2/999" section */
   10313 
   10314   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   10315   len = strlen (buf) + 1;
   10316   name = bfd_alloc (abfd, len);
   10317   if (name == NULL)
   10318     return false;
   10319   memcpy (name, buf, len);
   10320 
   10321   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10322   if (sect == NULL)
   10323     return false;
   10324 
   10325 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10326   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   10327   sect->filepos = note->descpos
   10328     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   10329 #endif
   10330 
   10331 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   10332   sect->size = sizeof (lwpstat.pr_fpreg);
   10333   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   10334 #endif
   10335 
   10336   sect->alignment_power = 2;
   10337 
   10338   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   10339 }
   10340 #endif /* defined (HAVE_LWPSTATUS_T) */
   10341 
   10342 /* These constants, and the structure offsets used below, are defined by
   10343    Cygwin's core_dump.h */
   10344 #define NOTE_INFO_PROCESS  1
   10345 #define NOTE_INFO_THREAD   2
   10346 #define NOTE_INFO_MODULE   3
   10347 #define NOTE_INFO_MODULE64 4
   10348 
   10349 static bool
   10350 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   10351 {
   10352   char buf[30];
   10353   char *name;
   10354   size_t len;
   10355   unsigned int name_size;
   10356   asection *sect;
   10357   unsigned int type;
   10358   int is_active_thread;
   10359   bfd_vma base_addr;
   10360 
   10361   if (note->descsz < 4)
   10362     return true;
   10363 
   10364   if (! startswith (note->namedata, "win32"))
   10365     return true;
   10366 
   10367   type = bfd_get_32 (abfd, note->descdata);
   10368 
   10369   struct
   10370   {
   10371     const char *type_name;
   10372     unsigned long min_size;
   10373   } size_check[] =
   10374       {
   10375        { "NOTE_INFO_PROCESS", 12 },
   10376        { "NOTE_INFO_THREAD", 12 },
   10377        { "NOTE_INFO_MODULE", 12 },
   10378        { "NOTE_INFO_MODULE64", 16 },
   10379       };
   10380 
   10381   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   10382       return true;
   10383 
   10384   if (note->descsz < size_check[type - 1].min_size)
   10385     {
   10386       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
   10387 			    " is too small"),
   10388 			  abfd, size_check[type - 1].type_name, note->descsz);
   10389       return true;
   10390     }
   10391 
   10392   switch (type)
   10393     {
   10394     case NOTE_INFO_PROCESS:
   10395       /* FIXME: need to add ->core->command.  */
   10396       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   10397       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   10398       break;
   10399 
   10400     case NOTE_INFO_THREAD:
   10401       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   10402 	 structure. */
   10403       /* thread_info.tid */
   10404       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   10405 
   10406       len = strlen (buf) + 1;
   10407       name = (char *) bfd_alloc (abfd, len);
   10408       if (name == NULL)
   10409 	return false;
   10410 
   10411       memcpy (name, buf, len);
   10412 
   10413       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10414       if (sect == NULL)
   10415 	return false;
   10416 
   10417       /* sizeof (thread_info.thread_context) */
   10418       sect->size = note->descsz - 12;
   10419       /* offsetof (thread_info.thread_context) */
   10420       sect->filepos = note->descpos + 12;
   10421       sect->alignment_power = 2;
   10422 
   10423       /* thread_info.is_active_thread */
   10424       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   10425 
   10426       if (is_active_thread)
   10427 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   10428 	  return false;
   10429       break;
   10430 
   10431     case NOTE_INFO_MODULE:
   10432     case NOTE_INFO_MODULE64:
   10433       /* Make a ".module/xxxxxxxx" section.  */
   10434       if (type == NOTE_INFO_MODULE)
   10435 	{
   10436 	  /* module_info.base_address */
   10437 	  base_addr = bfd_get_32 (abfd, note->descdata + 4);
   10438 	  sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   10439 	  /* module_info.module_name_size */
   10440 	  name_size = bfd_get_32 (abfd, note->descdata + 8);
   10441 	}
   10442       else /* NOTE_INFO_MODULE64 */
   10443 	{
   10444 	  /* module_info.base_address */
   10445 	  base_addr = bfd_get_64 (abfd, note->descdata + 4);
   10446 	  sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   10447 	  /* module_info.module_name_size */
   10448 	  name_size = bfd_get_32 (abfd, note->descdata + 12);
   10449 	}
   10450 
   10451       len = strlen (buf) + 1;
   10452       name = (char *) bfd_alloc (abfd, len);
   10453       if (name == NULL)
   10454 	return false;
   10455 
   10456       memcpy (name, buf, len);
   10457 
   10458       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10459 
   10460       if (sect == NULL)
   10461 	return false;
   10462 
   10463       if (note->descsz < 12 + name_size)
   10464 	{
   10465 	  _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
   10466 				" is too small to contain a name of size %u"),
   10467 			      abfd, note->descsz, name_size);
   10468 	  return true;
   10469 	}
   10470 
   10471       sect->size = note->descsz;
   10472       sect->filepos = note->descpos;
   10473       sect->alignment_power = 2;
   10474       break;
   10475 
   10476     default:
   10477       return true;
   10478     }
   10479 
   10480   return true;
   10481 }
   10482 
   10483 static bool
   10484 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   10485 {
   10486   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10487 
   10488   switch (note->type)
   10489     {
   10490     default:
   10491       return true;
   10492 
   10493     case NT_PRSTATUS:
   10494       if (bed->elf_backend_grok_prstatus)
   10495 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   10496 	  return true;
   10497 #if defined (HAVE_PRSTATUS_T)
   10498       return elfcore_grok_prstatus (abfd, note);
   10499 #else
   10500       return true;
   10501 #endif
   10502 
   10503 #if defined (HAVE_PSTATUS_T)
   10504     case NT_PSTATUS:
   10505       return elfcore_grok_pstatus (abfd, note);
   10506 #endif
   10507 
   10508 #if defined (HAVE_LWPSTATUS_T)
   10509     case NT_LWPSTATUS:
   10510       return elfcore_grok_lwpstatus (abfd, note);
   10511 #endif
   10512 
   10513     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   10514       return elfcore_grok_prfpreg (abfd, note);
   10515 
   10516     case NT_WIN32PSTATUS:
   10517       return elfcore_grok_win32pstatus (abfd, note);
   10518 
   10519     case NT_PRXFPREG:		/* Linux SSE extension */
   10520       if (note->namesz == 6
   10521 	  && strcmp (note->namedata, "LINUX") == 0)
   10522 	return elfcore_grok_prxfpreg (abfd, note);
   10523       else
   10524 	return true;
   10525 
   10526     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   10527       if (note->namesz == 6
   10528 	  && strcmp (note->namedata, "LINUX") == 0)
   10529 	return elfcore_grok_xstatereg (abfd, note);
   10530       else
   10531 	return true;
   10532 
   10533     case NT_PPC_VMX:
   10534       if (note->namesz == 6
   10535 	  && strcmp (note->namedata, "LINUX") == 0)
   10536 	return elfcore_grok_ppc_vmx (abfd, note);
   10537       else
   10538 	return true;
   10539 
   10540     case NT_PPC_VSX:
   10541       if (note->namesz == 6
   10542 	  && strcmp (note->namedata, "LINUX") == 0)
   10543 	return elfcore_grok_ppc_vsx (abfd, note);
   10544       else
   10545 	return true;
   10546 
   10547     case NT_PPC_TAR:
   10548       if (note->namesz == 6
   10549 	  && strcmp (note->namedata, "LINUX") == 0)
   10550 	return elfcore_grok_ppc_tar (abfd, note);
   10551       else
   10552 	return true;
   10553 
   10554     case NT_PPC_PPR:
   10555       if (note->namesz == 6
   10556 	  && strcmp (note->namedata, "LINUX") == 0)
   10557 	return elfcore_grok_ppc_ppr (abfd, note);
   10558       else
   10559 	return true;
   10560 
   10561     case NT_PPC_DSCR:
   10562       if (note->namesz == 6
   10563 	  && strcmp (note->namedata, "LINUX") == 0)
   10564 	return elfcore_grok_ppc_dscr (abfd, note);
   10565       else
   10566 	return true;
   10567 
   10568     case NT_PPC_EBB:
   10569       if (note->namesz == 6
   10570 	  && strcmp (note->namedata, "LINUX") == 0)
   10571 	return elfcore_grok_ppc_ebb (abfd, note);
   10572       else
   10573 	return true;
   10574 
   10575     case NT_PPC_PMU:
   10576       if (note->namesz == 6
   10577 	  && strcmp (note->namedata, "LINUX") == 0)
   10578 	return elfcore_grok_ppc_pmu (abfd, note);
   10579       else
   10580 	return true;
   10581 
   10582     case NT_PPC_TM_CGPR:
   10583       if (note->namesz == 6
   10584 	  && strcmp (note->namedata, "LINUX") == 0)
   10585 	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   10586       else
   10587 	return true;
   10588 
   10589     case NT_PPC_TM_CFPR:
   10590       if (note->namesz == 6
   10591 	  && strcmp (note->namedata, "LINUX") == 0)
   10592 	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   10593       else
   10594 	return true;
   10595 
   10596     case NT_PPC_TM_CVMX:
   10597       if (note->namesz == 6
   10598 	  && strcmp (note->namedata, "LINUX") == 0)
   10599 	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   10600       else
   10601 	return true;
   10602 
   10603     case NT_PPC_TM_CVSX:
   10604       if (note->namesz == 6
   10605 	  && strcmp (note->namedata, "LINUX") == 0)
   10606 	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   10607       else
   10608 	return true;
   10609 
   10610     case NT_PPC_TM_SPR:
   10611       if (note->namesz == 6
   10612 	  && strcmp (note->namedata, "LINUX") == 0)
   10613 	return elfcore_grok_ppc_tm_spr (abfd, note);
   10614       else
   10615 	return true;
   10616 
   10617     case NT_PPC_TM_CTAR:
   10618       if (note->namesz == 6
   10619 	  && strcmp (note->namedata, "LINUX") == 0)
   10620 	return elfcore_grok_ppc_tm_ctar (abfd, note);
   10621       else
   10622 	return true;
   10623 
   10624     case NT_PPC_TM_CPPR:
   10625       if (note->namesz == 6
   10626 	  && strcmp (note->namedata, "LINUX") == 0)
   10627 	return elfcore_grok_ppc_tm_cppr (abfd, note);
   10628       else
   10629 	return true;
   10630 
   10631     case NT_PPC_TM_CDSCR:
   10632       if (note->namesz == 6
   10633 	  && strcmp (note->namedata, "LINUX") == 0)
   10634 	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   10635       else
   10636 	return true;
   10637 
   10638     case NT_S390_HIGH_GPRS:
   10639       if (note->namesz == 6
   10640 	  && strcmp (note->namedata, "LINUX") == 0)
   10641 	return elfcore_grok_s390_high_gprs (abfd, note);
   10642       else
   10643 	return true;
   10644 
   10645     case NT_S390_TIMER:
   10646       if (note->namesz == 6
   10647 	  && strcmp (note->namedata, "LINUX") == 0)
   10648 	return elfcore_grok_s390_timer (abfd, note);
   10649       else
   10650 	return true;
   10651 
   10652     case NT_S390_TODCMP:
   10653       if (note->namesz == 6
   10654 	  && strcmp (note->namedata, "LINUX") == 0)
   10655 	return elfcore_grok_s390_todcmp (abfd, note);
   10656       else
   10657 	return true;
   10658 
   10659     case NT_S390_TODPREG:
   10660       if (note->namesz == 6
   10661 	  && strcmp (note->namedata, "LINUX") == 0)
   10662 	return elfcore_grok_s390_todpreg (abfd, note);
   10663       else
   10664 	return true;
   10665 
   10666     case NT_S390_CTRS:
   10667       if (note->namesz == 6
   10668 	  && strcmp (note->namedata, "LINUX") == 0)
   10669 	return elfcore_grok_s390_ctrs (abfd, note);
   10670       else
   10671 	return true;
   10672 
   10673     case NT_S390_PREFIX:
   10674       if (note->namesz == 6
   10675 	  && strcmp (note->namedata, "LINUX") == 0)
   10676 	return elfcore_grok_s390_prefix (abfd, note);
   10677       else
   10678 	return true;
   10679 
   10680     case NT_S390_LAST_BREAK:
   10681       if (note->namesz == 6
   10682 	  && strcmp (note->namedata, "LINUX") == 0)
   10683 	return elfcore_grok_s390_last_break (abfd, note);
   10684       else
   10685 	return true;
   10686 
   10687     case NT_S390_SYSTEM_CALL:
   10688       if (note->namesz == 6
   10689 	  && strcmp (note->namedata, "LINUX") == 0)
   10690 	return elfcore_grok_s390_system_call (abfd, note);
   10691       else
   10692 	return true;
   10693 
   10694     case NT_S390_TDB:
   10695       if (note->namesz == 6
   10696 	  && strcmp (note->namedata, "LINUX") == 0)
   10697 	return elfcore_grok_s390_tdb (abfd, note);
   10698       else
   10699 	return true;
   10700 
   10701     case NT_S390_VXRS_LOW:
   10702       if (note->namesz == 6
   10703 	  && strcmp (note->namedata, "LINUX") == 0)
   10704 	return elfcore_grok_s390_vxrs_low (abfd, note);
   10705       else
   10706 	return true;
   10707 
   10708     case NT_S390_VXRS_HIGH:
   10709       if (note->namesz == 6
   10710 	  && strcmp (note->namedata, "LINUX") == 0)
   10711 	return elfcore_grok_s390_vxrs_high (abfd, note);
   10712       else
   10713 	return true;
   10714 
   10715     case NT_S390_GS_CB:
   10716       if (note->namesz == 6
   10717 	  && strcmp (note->namedata, "LINUX") == 0)
   10718 	return elfcore_grok_s390_gs_cb (abfd, note);
   10719       else
   10720 	return true;
   10721 
   10722     case NT_S390_GS_BC:
   10723       if (note->namesz == 6
   10724 	  && strcmp (note->namedata, "LINUX") == 0)
   10725 	return elfcore_grok_s390_gs_bc (abfd, note);
   10726       else
   10727 	return true;
   10728 
   10729     case NT_ARC_V2:
   10730       if (note->namesz == 6
   10731 	  && strcmp (note->namedata, "LINUX") == 0)
   10732 	return elfcore_grok_arc_v2 (abfd, note);
   10733       else
   10734 	return true;
   10735 
   10736     case NT_ARM_VFP:
   10737       if (note->namesz == 6
   10738 	  && strcmp (note->namedata, "LINUX") == 0)
   10739 	return elfcore_grok_arm_vfp (abfd, note);
   10740       else
   10741 	return true;
   10742 
   10743     case NT_ARM_TLS:
   10744       if (note->namesz == 6
   10745 	  && strcmp (note->namedata, "LINUX") == 0)
   10746 	return elfcore_grok_aarch_tls (abfd, note);
   10747       else
   10748 	return true;
   10749 
   10750     case NT_ARM_HW_BREAK:
   10751       if (note->namesz == 6
   10752 	  && strcmp (note->namedata, "LINUX") == 0)
   10753 	return elfcore_grok_aarch_hw_break (abfd, note);
   10754       else
   10755 	return true;
   10756 
   10757     case NT_ARM_HW_WATCH:
   10758       if (note->namesz == 6
   10759 	  && strcmp (note->namedata, "LINUX") == 0)
   10760 	return elfcore_grok_aarch_hw_watch (abfd, note);
   10761       else
   10762 	return true;
   10763 
   10764     case NT_ARM_SVE:
   10765       if (note->namesz == 6
   10766 	  && strcmp (note->namedata, "LINUX") == 0)
   10767 	return elfcore_grok_aarch_sve (abfd, note);
   10768       else
   10769 	return true;
   10770 
   10771     case NT_ARM_PAC_MASK:
   10772       if (note->namesz == 6
   10773 	  && strcmp (note->namedata, "LINUX") == 0)
   10774 	return elfcore_grok_aarch_pauth (abfd, note);
   10775       else
   10776 	return true;
   10777 
   10778     case NT_ARM_TAGGED_ADDR_CTRL:
   10779       if (note->namesz == 6
   10780 	  && strcmp (note->namedata, "LINUX") == 0)
   10781 	return elfcore_grok_aarch_mte (abfd, note);
   10782       else
   10783 	return true;
   10784 
   10785     case NT_GDB_TDESC:
   10786       if (note->namesz == 4
   10787 	  && strcmp (note->namedata, "GDB") == 0)
   10788 	return elfcore_grok_gdb_tdesc (abfd, note);
   10789       else
   10790 	return true;
   10791 
   10792     case NT_RISCV_CSR:
   10793       if (note->namesz == 4
   10794 	  && strcmp (note->namedata, "GDB") == 0)
   10795 	return elfcore_grok_riscv_csr (abfd, note);
   10796       else
   10797 	return true;
   10798 
   10799     case NT_LARCH_CPUCFG:
   10800       if (note->namesz == 6
   10801 	  && strcmp (note->namedata, "LINUX") == 0)
   10802 	return elfcore_grok_loongarch_cpucfg (abfd, note);
   10803       else
   10804 	return true;
   10805 
   10806     case NT_LARCH_LBT:
   10807       if (note->namesz == 6
   10808 	  && strcmp (note->namedata, "LINUX") == 0)
   10809 	return elfcore_grok_loongarch_lbt (abfd, note);
   10810       else
   10811 	return true;
   10812 
   10813     case NT_LARCH_LSX:
   10814       if (note->namesz == 6
   10815 	  && strcmp (note->namedata, "LINUX") == 0)
   10816 	return elfcore_grok_loongarch_lsx (abfd, note);
   10817       else
   10818 	return true;
   10819 
   10820     case NT_LARCH_LASX:
   10821       if (note->namesz == 6
   10822 	  && strcmp (note->namedata, "LINUX") == 0)
   10823 	return elfcore_grok_loongarch_lasx (abfd, note);
   10824       else
   10825 	return true;
   10826 
   10827     case NT_PRPSINFO:
   10828     case NT_PSINFO:
   10829       if (bed->elf_backend_grok_psinfo)
   10830 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   10831 	  return true;
   10832 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10833       return elfcore_grok_psinfo (abfd, note);
   10834 #else
   10835       return true;
   10836 #endif
   10837 
   10838     case NT_AUXV:
   10839       return elfcore_make_auxv_note_section (abfd, note, 0);
   10840 
   10841     case NT_FILE:
   10842       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   10843 					      note);
   10844 
   10845     case NT_SIGINFO:
   10846       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   10847 					      note);
   10848 
   10849     }
   10850 }
   10851 
   10852 static bool
   10853 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   10854 {
   10855   struct bfd_build_id* build_id;
   10856 
   10857   if (note->descsz == 0)
   10858     return false;
   10859 
   10860   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   10861   if (build_id == NULL)
   10862     return false;
   10863 
   10864   build_id->size = note->descsz;
   10865   memcpy (build_id->data, note->descdata, note->descsz);
   10866   abfd->build_id = build_id;
   10867 
   10868   return true;
   10869 }
   10870 
   10871 static bool
   10872 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   10873 {
   10874   switch (note->type)
   10875     {
   10876     default:
   10877       return true;
   10878 
   10879     case NT_GNU_PROPERTY_TYPE_0:
   10880       return _bfd_elf_parse_gnu_properties (abfd, note);
   10881 
   10882     case NT_GNU_BUILD_ID:
   10883       return elfobj_grok_gnu_build_id (abfd, note);
   10884     }
   10885 }
   10886 
   10887 static bool
   10888 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   10889 {
   10890   struct sdt_note *cur =
   10891     (struct sdt_note *) bfd_alloc (abfd,
   10892 				   sizeof (struct sdt_note) + note->descsz);
   10893 
   10894   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   10895   cur->size = (bfd_size_type) note->descsz;
   10896   memcpy (cur->data, note->descdata, note->descsz);
   10897 
   10898   elf_tdata (abfd)->sdt_note_head = cur;
   10899 
   10900   return true;
   10901 }
   10902 
   10903 static bool
   10904 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   10905 {
   10906   switch (note->type)
   10907     {
   10908     case NT_STAPSDT:
   10909       return elfobj_grok_stapsdt_note_1 (abfd, note);
   10910 
   10911     default:
   10912       return true;
   10913     }
   10914 }
   10915 
   10916 static bool
   10917 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10918 {
   10919   size_t offset;
   10920 
   10921   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10922     {
   10923     case ELFCLASS32:
   10924       if (note->descsz < 108)
   10925 	return false;
   10926       break;
   10927 
   10928     case ELFCLASS64:
   10929       if (note->descsz < 120)
   10930 	return false;
   10931       break;
   10932 
   10933     default:
   10934       return false;
   10935     }
   10936 
   10937   /* Check for version 1 in pr_version.  */
   10938   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10939     return false;
   10940 
   10941   offset = 4;
   10942 
   10943   /* Skip over pr_psinfosz. */
   10944   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   10945     offset += 4;
   10946   else
   10947     {
   10948       offset += 4;	/* Padding before pr_psinfosz. */
   10949       offset += 8;
   10950     }
   10951 
   10952   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   10953   elf_tdata (abfd)->core->program
   10954     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   10955   offset += 17;
   10956 
   10957   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   10958   elf_tdata (abfd)->core->command
   10959     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   10960   offset += 81;
   10961 
   10962   /* Padding before pr_pid.  */
   10963   offset += 2;
   10964 
   10965   /* The pr_pid field was added in version "1a".  */
   10966   if (note->descsz < offset + 4)
   10967     return true;
   10968 
   10969   elf_tdata (abfd)->core->pid
   10970     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10971 
   10972   return true;
   10973 }
   10974 
   10975 static bool
   10976 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10977 {
   10978   size_t offset;
   10979   size_t size;
   10980   size_t min_size;
   10981 
   10982   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   10983      Also compute minimum size of this note.  */
   10984   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10985     {
   10986     case ELFCLASS32:
   10987       offset = 4 + 4;
   10988       min_size = offset + (4 * 2) + 4 + 4 + 4;
   10989       break;
   10990 
   10991     case ELFCLASS64:
   10992       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   10993       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   10994       break;
   10995 
   10996     default:
   10997       return false;
   10998     }
   10999 
   11000   if (note->descsz < min_size)
   11001     return false;
   11002 
   11003   /* Check for version 1 in pr_version.  */
   11004   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11005     return false;
   11006 
   11007   /* Extract size of pr_reg from pr_gregsetsz.  */
   11008   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   11009   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11010     {
   11011       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11012       offset += 4 * 2;
   11013     }
   11014   else
   11015     {
   11016       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   11017       offset += 8 * 2;
   11018     }
   11019 
   11020   /* Skip over pr_osreldate.  */
   11021   offset += 4;
   11022 
   11023   /* Read signal from pr_cursig.  */
   11024   if (elf_tdata (abfd)->core->signal == 0)
   11025     elf_tdata (abfd)->core->signal
   11026       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11027   offset += 4;
   11028 
   11029   /* Read TID from pr_pid.  */
   11030   elf_tdata (abfd)->core->lwpid
   11031       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11032   offset += 4;
   11033 
   11034   /* Padding before pr_reg.  */
   11035   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11036     offset += 4;
   11037 
   11038   /* Make sure that there is enough data remaining in the note.  */
   11039   if ((note->descsz - offset) < size)
   11040     return false;
   11041 
   11042   /* Make a ".reg/999" section and a ".reg" section.  */
   11043   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   11044 					  size, note->descpos + offset);
   11045 }
   11046 
   11047 static bool
   11048 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11049 {
   11050   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11051 
   11052   switch (note->type)
   11053     {
   11054     case NT_PRSTATUS:
   11055       if (bed->elf_backend_grok_freebsd_prstatus)
   11056 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11057 	  return true;
   11058       return elfcore_grok_freebsd_prstatus (abfd, note);
   11059 
   11060     case NT_FPREGSET:
   11061       return elfcore_grok_prfpreg (abfd, note);
   11062 
   11063     case NT_PRPSINFO:
   11064       return elfcore_grok_freebsd_psinfo (abfd, note);
   11065 
   11066     case NT_FREEBSD_THRMISC:
   11067       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11068 
   11069     case NT_FREEBSD_PROCSTAT_PROC:
   11070       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11071 					      note);
   11072 
   11073     case NT_FREEBSD_PROCSTAT_FILES:
   11074       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11075 					      note);
   11076 
   11077     case NT_FREEBSD_PROCSTAT_VMMAP:
   11078       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11079 					      note);
   11080 
   11081     case NT_FREEBSD_PROCSTAT_AUXV:
   11082       return elfcore_make_auxv_note_section (abfd, note, 4);
   11083 
   11084     case NT_FREEBSD_X86_SEGBASES:
   11085       return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
   11086 
   11087     case NT_X86_XSTATE:
   11088       return elfcore_grok_xstatereg (abfd, note);
   11089 
   11090     case NT_FREEBSD_PTLWPINFO:
   11091       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11092 					      note);
   11093 
   11094     case NT_ARM_TLS:
   11095       return elfcore_grok_aarch_tls (abfd, note);
   11096 
   11097     case NT_ARM_VFP:
   11098       return elfcore_grok_arm_vfp (abfd, note);
   11099 
   11100     default:
   11101       return true;
   11102     }
   11103 }
   11104 
   11105 static bool
   11106 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11107 {
   11108   char *cp;
   11109 
   11110   cp = strchr (note->namedata, '@');
   11111   if (cp != NULL)
   11112     {
   11113       *lwpidp = atoi(cp + 1);
   11114       return true;
   11115     }
   11116   return false;
   11117 }
   11118 
   11119 static bool
   11120 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11121 {
   11122   if (note->descsz <= 0x7c + 31)
   11123     return false;
   11124 
   11125   /* Signal number at offset 0x08. */
   11126   elf_tdata (abfd)->core->signal
   11127     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11128 
   11129   /* Process ID at offset 0x50. */
   11130   elf_tdata (abfd)->core->pid
   11131     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11132 
   11133   /* Command name at 0x7c (max 32 bytes, including nul). */
   11134   elf_tdata (abfd)->core->command
   11135     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11136 
   11137   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11138 					  note);
   11139 }
   11140 
   11141 static bool
   11142 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11143 {
   11144   int lwp;
   11145 
   11146   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11147     elf_tdata (abfd)->core->lwpid = lwp;
   11148 
   11149   switch (note->type)
   11150     {
   11151     case NT_NETBSDCORE_PROCINFO:
   11152       /* NetBSD-specific core "procinfo".  Note that we expect to
   11153 	 find this note before any of the others, which is fine,
   11154 	 since the kernel writes this note out first when it
   11155 	 creates a core file.  */
   11156       return elfcore_grok_netbsd_procinfo (abfd, note);
   11157     case NT_NETBSDCORE_AUXV:
   11158       /* NetBSD-specific Elf Auxiliary Vector data. */
   11159       return elfcore_make_auxv_note_section (abfd, note, 0);
   11160     case NT_NETBSDCORE_LWPSTATUS:
   11161       return elfcore_make_note_pseudosection (abfd,
   11162 					      ".note.netbsdcore.lwpstatus",
   11163 					      note);
   11164     default:
   11165       break;
   11166     }
   11167 
   11168   if (note->type == NT_NETBSDCORE_AUXV)
   11169     {
   11170       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   11171 							   SEC_HAS_CONTENTS);
   11172 
   11173       if (sect == NULL)
   11174 	return false;
   11175       sect->size = note->descsz;
   11176       sect->filepos = note->descpos;
   11177       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   11178 
   11179       return true;
   11180     }
   11181 
   11182   /* As of March 2020 there are no other machine-independent notes
   11183      defined for NetBSD core files.  If the note type is less
   11184      than the start of the machine-dependent note types, we don't
   11185      understand it.  */
   11186 
   11187   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11188     return true;
   11189 
   11190 
   11191   switch (bfd_get_arch (abfd))
   11192     {
   11193       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11194 	 PT_GETFPREGS == mach+2.  */
   11195 
   11196     case bfd_arch_aarch64:
   11197     case bfd_arch_alpha:
   11198     case bfd_arch_sparc:
   11199       switch (note->type)
   11200 	{
   11201 	case NT_NETBSDCORE_FIRSTMACH+0:
   11202 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11203 
   11204 	case NT_NETBSDCORE_FIRSTMACH+2:
   11205 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11206 
   11207 	default:
   11208 	  return true;
   11209 	}
   11210 
   11211       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11212 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11213 	 structure which lacks GBR.  */
   11214 
   11215     case bfd_arch_sh:
   11216       switch (note->type)
   11217 	{
   11218 	case NT_NETBSDCORE_FIRSTMACH+3:
   11219 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11220 
   11221 	case NT_NETBSDCORE_FIRSTMACH+5:
   11222 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11223 
   11224 	default:
   11225 	  return true;
   11226 	}
   11227 
   11228       /* On all other arch's, PT_GETREGS == mach+1 and
   11229 	 PT_GETFPREGS == mach+3.  */
   11230 
   11231     default:
   11232       switch (note->type)
   11233 	{
   11234 	case NT_NETBSDCORE_FIRSTMACH+1:
   11235 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11236 
   11237 	case NT_NETBSDCORE_FIRSTMACH+3:
   11238 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11239 
   11240 	default:
   11241 	  return true;
   11242 	}
   11243     }
   11244     /* NOTREACHED */
   11245 }
   11246 
   11247 static bool
   11248 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11249 {
   11250   if (note->descsz <= 0x48 + 31)
   11251     return false;
   11252 
   11253   /* Signal number at offset 0x08. */
   11254   elf_tdata (abfd)->core->signal
   11255     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11256 
   11257   /* Process ID at offset 0x20. */
   11258   elf_tdata (abfd)->core->pid
   11259     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11260 
   11261   /* Command name at 0x48 (max 32 bytes, including nul). */
   11262   elf_tdata (abfd)->core->command
   11263     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11264 
   11265   return true;
   11266 }
   11267 
   11268 /* Processes Solaris's process status note.
   11269    sig_off ~ offsetof(prstatus_t, pr_cursig)
   11270    pid_off ~ offsetof(prstatus_t, pr_pid)
   11271    lwpid_off ~ offsetof(prstatus_t, pr_who)
   11272    gregset_size ~ sizeof(gregset_t)
   11273    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   11274 
   11275 static bool
   11276 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   11277 			       int pid_off, int lwpid_off, size_t gregset_size,
   11278 			       size_t gregset_offset)
   11279 {
   11280   asection *sect = NULL;
   11281   elf_tdata (abfd)->core->signal
   11282     = bfd_get_16 (abfd, note->descdata + sig_off);
   11283   elf_tdata (abfd)->core->pid
   11284     = bfd_get_32 (abfd, note->descdata + pid_off);
   11285   elf_tdata (abfd)->core->lwpid
   11286     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   11287 
   11288   sect = bfd_get_section_by_name (abfd, ".reg");
   11289   if (sect != NULL)
   11290     sect->size = gregset_size;
   11291 
   11292   return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11293 					  note->descpos + gregset_offset);
   11294 }
   11295 
   11296 /* Gets program and arguments from a core.
   11297    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   11298    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   11299 
   11300 static bool
   11301 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   11302 			  int prog_off, int comm_off)
   11303 {
   11304   elf_tdata (abfd)->core->program
   11305     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   11306   elf_tdata (abfd)->core->command
   11307     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   11308 
   11309   return true;
   11310 }
   11311 
   11312 /* Processes Solaris's LWP status note.
   11313    gregset_size ~ sizeof(gregset_t)
   11314    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   11315    fpregset_size ~ sizeof(fpregset_t)
   11316    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   11317 
   11318 static bool
   11319 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   11320 				size_t gregset_size, int gregset_off,
   11321 				size_t fpregset_size, int fpregset_off)
   11322 {
   11323   asection *sect = NULL;
   11324   char reg2_section_name[16] = { 0 };
   11325 
   11326   (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
   11327 		   elf_tdata (abfd)->core->lwpid);
   11328 
   11329   /* offsetof(lwpstatus_t, pr_lwpid) */
   11330   elf_tdata (abfd)->core->lwpid
   11331     = bfd_get_32 (abfd, note->descdata + 4);
   11332   /* offsetof(lwpstatus_t, pr_cursig) */
   11333   elf_tdata (abfd)->core->signal
   11334     = bfd_get_16 (abfd, note->descdata + 12);
   11335 
   11336   sect = bfd_get_section_by_name (abfd, ".reg");
   11337   if (sect != NULL)
   11338     sect->size = gregset_size;
   11339   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11340 					     note->descpos + gregset_off))
   11341     return false;
   11342 
   11343   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   11344   if (sect != NULL)
   11345     {
   11346       sect->size = fpregset_size;
   11347       sect->filepos = note->descpos + fpregset_off;
   11348       sect->alignment_power = 2;
   11349     }
   11350   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
   11351 					     note->descpos + fpregset_off))
   11352     return false;
   11353 
   11354   return true;
   11355 }
   11356 
   11357 static bool
   11358 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   11359 {
   11360   if (note == NULL)
   11361     return false;
   11362 
   11363   /* core files are identified as 32- or 64-bit, SPARC or x86,
   11364      by the size of the descsz which matches the sizeof()
   11365      the type appropriate for that note type (e.g., prstatus_t for
   11366      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   11367      on Solaris. The core file bitness may differ from the bitness of
   11368      gdb itself, so fixed values are used instead of sizeof().
   11369      Appropriate fixed offsets are also used to obtain data from
   11370      the note.  */
   11371 
   11372   switch ((int) note->type)
   11373     {
   11374     case SOLARIS_NT_PRSTATUS:
   11375       switch (note->descsz)
   11376 	{
   11377 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   11378 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11379 					       136, 216, 308, 152, 356);
   11380 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   11381 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11382 					       264, 360, 520, 304, 600);
   11383 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   11384 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11385 					       136, 216, 308, 76, 356);
   11386 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   11387 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11388 					       264, 360, 520, 224, 600);
   11389 	default:
   11390 	  return true;
   11391 	}
   11392 
   11393     case SOLARIS_NT_PSINFO:
   11394     case SOLARIS_NT_PRPSINFO:
   11395       switch (note->descsz)
   11396 	{
   11397 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   11398 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   11399 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   11400 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   11401 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   11402 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   11403 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   11404 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   11405 	default:
   11406 	  return true;
   11407 	}
   11408 
   11409     case SOLARIS_NT_LWPSTATUS:
   11410       switch (note->descsz)
   11411 	{
   11412 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   11413 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11414 						152, 344, 400, 496);
   11415 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   11416 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11417 						304, 544, 544, 848);
   11418 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   11419 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11420 						76, 344, 380, 420);
   11421 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   11422 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11423 						224, 544, 528, 768);
   11424 	default:
   11425 	  return true;
   11426 	}
   11427 
   11428     case SOLARIS_NT_LWPSINFO:
   11429       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   11430       if (note->descsz == 128 || note->descsz == 152)
   11431 	elf_tdata (abfd)->core->lwpid =
   11432 	  bfd_get_32 (abfd, note->descdata + 4);
   11433       break;
   11434 
   11435     default:
   11436       break;
   11437     }
   11438 
   11439   return true;
   11440 }
   11441 
   11442 /* For name starting with "CORE" this may be either a Solaris
   11443    core file or a gdb-generated core file.  Do Solaris-specific
   11444    processing on selected note types first with
   11445    elfcore_grok_solaris_note(), then process the note
   11446    in elfcore_grok_note().  */
   11447 
   11448 static bool
   11449 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   11450 {
   11451   if (!elfcore_grok_solaris_note_impl (abfd, note))
   11452     return false;
   11453 
   11454   return elfcore_grok_note (abfd, note);
   11455 }
   11456 
   11457 static bool
   11458 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11459 {
   11460   if (note->type == NT_OPENBSD_PROCINFO)
   11461     return elfcore_grok_openbsd_procinfo (abfd, note);
   11462 
   11463   if (note->type == NT_OPENBSD_REGS)
   11464     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11465 
   11466   if (note->type == NT_OPENBSD_FPREGS)
   11467     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11468 
   11469   if (note->type == NT_OPENBSD_XFPREGS)
   11470     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   11471 
   11472   if (note->type == NT_OPENBSD_AUXV)
   11473     return elfcore_make_auxv_note_section (abfd, note, 0);
   11474 
   11475   if (note->type == NT_OPENBSD_WCOOKIE)
   11476     {
   11477       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   11478 							   SEC_HAS_CONTENTS);
   11479 
   11480       if (sect == NULL)
   11481 	return false;
   11482       sect->size = note->descsz;
   11483       sect->filepos = note->descpos;
   11484       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   11485 
   11486       return true;
   11487     }
   11488 
   11489   return true;
   11490 }
   11491 
   11492 static bool
   11493 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   11494 {
   11495   void *ddata = note->descdata;
   11496   char buf[100];
   11497   char *name;
   11498   asection *sect;
   11499   short sig;
   11500   unsigned flags;
   11501 
   11502   if (note->descsz < 16)
   11503     return false;
   11504 
   11505   /* nto_procfs_status 'pid' field is at offset 0.  */
   11506   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   11507 
   11508   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   11509   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   11510 
   11511   /* nto_procfs_status 'flags' field is at offset 8.  */
   11512   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   11513 
   11514   /* nto_procfs_status 'what' field is at offset 14.  */
   11515   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   11516     {
   11517       elf_tdata (abfd)->core->signal = sig;
   11518       elf_tdata (abfd)->core->lwpid = *tid;
   11519     }
   11520 
   11521   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   11522      do not come from signals so we make sure we set the current
   11523      thread just in case.  */
   11524   if (flags & 0x00000080)
   11525     elf_tdata (abfd)->core->lwpid = *tid;
   11526 
   11527   /* Make a ".qnx_core_status/%d" section.  */
   11528   sprintf (buf, ".qnx_core_status/%ld", *tid);
   11529 
   11530   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11531   if (name == NULL)
   11532     return false;
   11533   strcpy (name, buf);
   11534 
   11535   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11536   if (sect == NULL)
   11537     return false;
   11538 
   11539   sect->size		= note->descsz;
   11540   sect->filepos		= note->descpos;
   11541   sect->alignment_power = 2;
   11542 
   11543   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   11544 }
   11545 
   11546 static bool
   11547 elfcore_grok_nto_regs (bfd *abfd,
   11548 		       Elf_Internal_Note *note,
   11549 		       long tid,
   11550 		       char *base)
   11551 {
   11552   char buf[100];
   11553   char *name;
   11554   asection *sect;
   11555 
   11556   /* Make a "(base)/%d" section.  */
   11557   sprintf (buf, "%s/%ld", base, tid);
   11558 
   11559   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11560   if (name == NULL)
   11561     return false;
   11562   strcpy (name, buf);
   11563 
   11564   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11565   if (sect == NULL)
   11566     return false;
   11567 
   11568   sect->size		= note->descsz;
   11569   sect->filepos		= note->descpos;
   11570   sect->alignment_power = 2;
   11571 
   11572   /* This is the current thread.  */
   11573   if (elf_tdata (abfd)->core->lwpid == tid)
   11574     return elfcore_maybe_make_sect (abfd, base, sect);
   11575 
   11576   return true;
   11577 }
   11578 
   11579 #define BFD_QNT_CORE_INFO	7
   11580 #define BFD_QNT_CORE_STATUS	8
   11581 #define BFD_QNT_CORE_GREG	9
   11582 #define BFD_QNT_CORE_FPREG	10
   11583 
   11584 static bool
   11585 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   11586 {
   11587   /* Every GREG section has a STATUS section before it.  Store the
   11588      tid from the previous call to pass down to the next gregs
   11589      function.  */
   11590   static long tid = 1;
   11591 
   11592   switch (note->type)
   11593     {
   11594     case BFD_QNT_CORE_INFO:
   11595       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   11596     case BFD_QNT_CORE_STATUS:
   11597       return elfcore_grok_nto_status (abfd, note, &tid);
   11598     case BFD_QNT_CORE_GREG:
   11599       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   11600     case BFD_QNT_CORE_FPREG:
   11601       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   11602     default:
   11603       return true;
   11604     }
   11605 }
   11606 
   11607 static bool
   11608 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   11609 {
   11610   char *name;
   11611   asection *sect;
   11612   size_t len;
   11613 
   11614   /* Use note name as section name.  */
   11615   len = note->namesz;
   11616   name = (char *) bfd_alloc (abfd, len);
   11617   if (name == NULL)
   11618     return false;
   11619   memcpy (name, note->namedata, len);
   11620   name[len - 1] = '\0';
   11621 
   11622   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11623   if (sect == NULL)
   11624     return false;
   11625 
   11626   sect->size		= note->descsz;
   11627   sect->filepos		= note->descpos;
   11628   sect->alignment_power = 1;
   11629 
   11630   return true;
   11631 }
   11632 
   11633 /* Function: elfcore_write_note
   11634 
   11635    Inputs:
   11636      buffer to hold note, and current size of buffer
   11637      name of note
   11638      type of note
   11639      data for note
   11640      size of data for note
   11641 
   11642    Writes note to end of buffer.  ELF64 notes are written exactly as
   11643    for ELF32, despite the current (as of 2006) ELF gabi specifying
   11644    that they ought to have 8-byte namesz and descsz field, and have
   11645    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   11646 
   11647    Return:
   11648    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   11649 
   11650 char *
   11651 elfcore_write_note (bfd *abfd,
   11652 		    char *buf,
   11653 		    int *bufsiz,
   11654 		    const char *name,
   11655 		    int type,
   11656 		    const void *input,
   11657 		    int size)
   11658 {
   11659   Elf_External_Note *xnp;
   11660   size_t namesz;
   11661   size_t newspace;
   11662   char *dest;
   11663 
   11664   namesz = 0;
   11665   if (name != NULL)
   11666     namesz = strlen (name) + 1;
   11667 
   11668   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   11669 
   11670   buf = (char *) realloc (buf, *bufsiz + newspace);
   11671   if (buf == NULL)
   11672     return buf;
   11673   dest = buf + *bufsiz;
   11674   *bufsiz += newspace;
   11675   xnp = (Elf_External_Note *) dest;
   11676   H_PUT_32 (abfd, namesz, xnp->namesz);
   11677   H_PUT_32 (abfd, size, xnp->descsz);
   11678   H_PUT_32 (abfd, type, xnp->type);
   11679   dest = xnp->name;
   11680   if (name != NULL)
   11681     {
   11682       memcpy (dest, name, namesz);
   11683       dest += namesz;
   11684       while (namesz & 3)
   11685 	{
   11686 	  *dest++ = '\0';
   11687 	  ++namesz;
   11688 	}
   11689     }
   11690   memcpy (dest, input, size);
   11691   dest += size;
   11692   while (size & 3)
   11693     {
   11694       *dest++ = '\0';
   11695       ++size;
   11696     }
   11697   return buf;
   11698 }
   11699 
   11700 /* gcc-8 warns (*) on all the strncpy calls in this function about
   11701    possible string truncation.  The "truncation" is not a bug.  We
   11702    have an external representation of structs with fields that are not
   11703    necessarily NULL terminated and corresponding internal
   11704    representation fields that are one larger so that they can always
   11705    be NULL terminated.
   11706    gcc versions between 4.2 and 4.6 do not allow pragma control of
   11707    diagnostics inside functions, giving a hard error if you try to use
   11708    the finer control available with later versions.
   11709    gcc prior to 4.2 warns about diagnostic push and pop.
   11710    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   11711    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   11712    (*) Depending on your system header files!  */
   11713 #if GCC_VERSION >= 8000
   11714 # pragma GCC diagnostic push
   11715 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   11716 #endif
   11717 char *
   11718 elfcore_write_prpsinfo (bfd  *abfd,
   11719 			char *buf,
   11720 			int  *bufsiz,
   11721 			const char *fname,
   11722 			const char *psargs)
   11723 {
   11724   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11725 
   11726   if (bed->elf_backend_write_core_note != NULL)
   11727     {
   11728       char *ret;
   11729       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11730 						 NT_PRPSINFO, fname, psargs);
   11731       if (ret != NULL)
   11732 	return ret;
   11733     }
   11734 
   11735 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11736 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   11737   if (bed->s->elfclass == ELFCLASS32)
   11738     {
   11739 #  if defined (HAVE_PSINFO32_T)
   11740       psinfo32_t data;
   11741       int note_type = NT_PSINFO;
   11742 #  else
   11743       prpsinfo32_t data;
   11744       int note_type = NT_PRPSINFO;
   11745 #  endif
   11746 
   11747       memset (&data, 0, sizeof (data));
   11748       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11749       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11750       return elfcore_write_note (abfd, buf, bufsiz,
   11751 				 "CORE", note_type, &data, sizeof (data));
   11752     }
   11753   else
   11754 # endif
   11755     {
   11756 # if defined (HAVE_PSINFO_T)
   11757       psinfo_t data;
   11758       int note_type = NT_PSINFO;
   11759 # else
   11760       prpsinfo_t data;
   11761       int note_type = NT_PRPSINFO;
   11762 # endif
   11763 
   11764       memset (&data, 0, sizeof (data));
   11765       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11766       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11767       return elfcore_write_note (abfd, buf, bufsiz,
   11768 				 "CORE", note_type, &data, sizeof (data));
   11769     }
   11770 #endif	/* PSINFO_T or PRPSINFO_T */
   11771 
   11772   free (buf);
   11773   return NULL;
   11774 }
   11775 #if GCC_VERSION >= 8000
   11776 # pragma GCC diagnostic pop
   11777 #endif
   11778 
   11779 char *
   11780 elfcore_write_linux_prpsinfo32
   11781   (bfd *abfd, char *buf, int *bufsiz,
   11782    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11783 {
   11784   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   11785     {
   11786       struct elf_external_linux_prpsinfo32_ugid16 data;
   11787 
   11788       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   11789       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11790 				 &data, sizeof (data));
   11791     }
   11792   else
   11793     {
   11794       struct elf_external_linux_prpsinfo32_ugid32 data;
   11795 
   11796       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   11797       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11798 				 &data, sizeof (data));
   11799     }
   11800 }
   11801 
   11802 char *
   11803 elfcore_write_linux_prpsinfo64
   11804   (bfd *abfd, char *buf, int *bufsiz,
   11805    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11806 {
   11807   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   11808     {
   11809       struct elf_external_linux_prpsinfo64_ugid16 data;
   11810 
   11811       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   11812       return elfcore_write_note (abfd, buf, bufsiz,
   11813 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11814     }
   11815   else
   11816     {
   11817       struct elf_external_linux_prpsinfo64_ugid32 data;
   11818 
   11819       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   11820       return elfcore_write_note (abfd, buf, bufsiz,
   11821 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11822     }
   11823 }
   11824 
   11825 char *
   11826 elfcore_write_prstatus (bfd *abfd,
   11827 			char *buf,
   11828 			int *bufsiz,
   11829 			long pid,
   11830 			int cursig,
   11831 			const void *gregs)
   11832 {
   11833   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11834 
   11835   if (bed->elf_backend_write_core_note != NULL)
   11836     {
   11837       char *ret;
   11838       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11839 						 NT_PRSTATUS,
   11840 						 pid, cursig, gregs);
   11841       if (ret != NULL)
   11842 	return ret;
   11843     }
   11844 
   11845 #if defined (HAVE_PRSTATUS_T)
   11846 #if defined (HAVE_PRSTATUS32_T)
   11847   if (bed->s->elfclass == ELFCLASS32)
   11848     {
   11849       prstatus32_t prstat;
   11850 
   11851       memset (&prstat, 0, sizeof (prstat));
   11852       prstat.pr_pid = pid;
   11853       prstat.pr_cursig = cursig;
   11854       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11855       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11856 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11857     }
   11858   else
   11859 #endif
   11860     {
   11861       prstatus_t prstat;
   11862 
   11863       memset (&prstat, 0, sizeof (prstat));
   11864       prstat.pr_pid = pid;
   11865       prstat.pr_cursig = cursig;
   11866       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11867       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11868 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11869     }
   11870 #endif /* HAVE_PRSTATUS_T */
   11871 
   11872   free (buf);
   11873   return NULL;
   11874 }
   11875 
   11876 #if defined (HAVE_LWPSTATUS_T)
   11877 char *
   11878 elfcore_write_lwpstatus (bfd *abfd,
   11879 			 char *buf,
   11880 			 int *bufsiz,
   11881 			 long pid,
   11882 			 int cursig,
   11883 			 const void *gregs)
   11884 {
   11885   lwpstatus_t lwpstat;
   11886   const char *note_name = "CORE";
   11887 
   11888   memset (&lwpstat, 0, sizeof (lwpstat));
   11889   lwpstat.pr_lwpid  = pid >> 16;
   11890   lwpstat.pr_cursig = cursig;
   11891 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   11892   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   11893 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11894 #if !defined(gregs)
   11895   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   11896 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   11897 #else
   11898   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   11899 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   11900 #endif
   11901 #endif
   11902   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   11903 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   11904 }
   11905 #endif /* HAVE_LWPSTATUS_T */
   11906 
   11907 #if defined (HAVE_PSTATUS_T)
   11908 char *
   11909 elfcore_write_pstatus (bfd *abfd,
   11910 		       char *buf,
   11911 		       int *bufsiz,
   11912 		       long pid,
   11913 		       int cursig ATTRIBUTE_UNUSED,
   11914 		       const void *gregs ATTRIBUTE_UNUSED)
   11915 {
   11916   const char *note_name = "CORE";
   11917 #if defined (HAVE_PSTATUS32_T)
   11918   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11919 
   11920   if (bed->s->elfclass == ELFCLASS32)
   11921     {
   11922       pstatus32_t pstat;
   11923 
   11924       memset (&pstat, 0, sizeof (pstat));
   11925       pstat.pr_pid = pid & 0xffff;
   11926       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11927 				NT_PSTATUS, &pstat, sizeof (pstat));
   11928       return buf;
   11929     }
   11930   else
   11931 #endif
   11932     {
   11933       pstatus_t pstat;
   11934 
   11935       memset (&pstat, 0, sizeof (pstat));
   11936       pstat.pr_pid = pid & 0xffff;
   11937       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11938 				NT_PSTATUS, &pstat, sizeof (pstat));
   11939       return buf;
   11940     }
   11941 }
   11942 #endif /* HAVE_PSTATUS_T */
   11943 
   11944 char *
   11945 elfcore_write_prfpreg (bfd *abfd,
   11946 		       char *buf,
   11947 		       int *bufsiz,
   11948 		       const void *fpregs,
   11949 		       int size)
   11950 {
   11951   const char *note_name = "CORE";
   11952   return elfcore_write_note (abfd, buf, bufsiz,
   11953 			     note_name, NT_FPREGSET, fpregs, size);
   11954 }
   11955 
   11956 char *
   11957 elfcore_write_prxfpreg (bfd *abfd,
   11958 			char *buf,
   11959 			int *bufsiz,
   11960 			const void *xfpregs,
   11961 			int size)
   11962 {
   11963   char *note_name = "LINUX";
   11964   return elfcore_write_note (abfd, buf, bufsiz,
   11965 			     note_name, NT_PRXFPREG, xfpregs, size);
   11966 }
   11967 
   11968 char *
   11969 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   11970 			 const void *xfpregs, int size)
   11971 {
   11972   char *note_name;
   11973   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   11974     note_name = "FreeBSD";
   11975   else
   11976     note_name = "LINUX";
   11977   return elfcore_write_note (abfd, buf, bufsiz,
   11978 			     note_name, NT_X86_XSTATE, xfpregs, size);
   11979 }
   11980 
   11981 char *
   11982 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   11983 			    const void *regs, int size)
   11984 {
   11985   char *note_name = "FreeBSD";
   11986   return elfcore_write_note (abfd, buf, bufsiz,
   11987 			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
   11988 }
   11989 
   11990 char *
   11991 elfcore_write_ppc_vmx (bfd *abfd,
   11992 		       char *buf,
   11993 		       int *bufsiz,
   11994 		       const void *ppc_vmx,
   11995 		       int size)
   11996 {
   11997   char *note_name = "LINUX";
   11998   return elfcore_write_note (abfd, buf, bufsiz,
   11999 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   12000 }
   12001 
   12002 char *
   12003 elfcore_write_ppc_vsx (bfd *abfd,
   12004 		       char *buf,
   12005 		       int *bufsiz,
   12006 		       const void *ppc_vsx,
   12007 		       int size)
   12008 {
   12009   char *note_name = "LINUX";
   12010   return elfcore_write_note (abfd, buf, bufsiz,
   12011 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   12012 }
   12013 
   12014 char *
   12015 elfcore_write_ppc_tar (bfd *abfd,
   12016 		       char *buf,
   12017 		       int *bufsiz,
   12018 		       const void *ppc_tar,
   12019 		       int size)
   12020 {
   12021   char *note_name = "LINUX";
   12022   return elfcore_write_note (abfd, buf, bufsiz,
   12023 			     note_name, NT_PPC_TAR, ppc_tar, size);
   12024 }
   12025 
   12026 char *
   12027 elfcore_write_ppc_ppr (bfd *abfd,
   12028 		       char *buf,
   12029 		       int *bufsiz,
   12030 		       const void *ppc_ppr,
   12031 		       int size)
   12032 {
   12033   char *note_name = "LINUX";
   12034   return elfcore_write_note (abfd, buf, bufsiz,
   12035 			     note_name, NT_PPC_PPR, ppc_ppr, size);
   12036 }
   12037 
   12038 char *
   12039 elfcore_write_ppc_dscr (bfd *abfd,
   12040 			char *buf,
   12041 			int *bufsiz,
   12042 			const void *ppc_dscr,
   12043 			int size)
   12044 {
   12045   char *note_name = "LINUX";
   12046   return elfcore_write_note (abfd, buf, bufsiz,
   12047 			     note_name, NT_PPC_DSCR, ppc_dscr, size);
   12048 }
   12049 
   12050 char *
   12051 elfcore_write_ppc_ebb (bfd *abfd,
   12052 		       char *buf,
   12053 		       int *bufsiz,
   12054 		       const void *ppc_ebb,
   12055 		       int size)
   12056 {
   12057   char *note_name = "LINUX";
   12058   return elfcore_write_note (abfd, buf, bufsiz,
   12059 			     note_name, NT_PPC_EBB, ppc_ebb, size);
   12060 }
   12061 
   12062 char *
   12063 elfcore_write_ppc_pmu (bfd *abfd,
   12064 		       char *buf,
   12065 		       int *bufsiz,
   12066 		       const void *ppc_pmu,
   12067 		       int size)
   12068 {
   12069   char *note_name = "LINUX";
   12070   return elfcore_write_note (abfd, buf, bufsiz,
   12071 			     note_name, NT_PPC_PMU, ppc_pmu, size);
   12072 }
   12073 
   12074 char *
   12075 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12076 			   char *buf,
   12077 			   int *bufsiz,
   12078 			   const void *ppc_tm_cgpr,
   12079 			   int size)
   12080 {
   12081   char *note_name = "LINUX";
   12082   return elfcore_write_note (abfd, buf, bufsiz,
   12083 			     note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
   12084 }
   12085 
   12086 char *
   12087 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12088 			   char *buf,
   12089 			   int *bufsiz,
   12090 			   const void *ppc_tm_cfpr,
   12091 			   int size)
   12092 {
   12093   char *note_name = "LINUX";
   12094   return elfcore_write_note (abfd, buf, bufsiz,
   12095 			     note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
   12096 }
   12097 
   12098 char *
   12099 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12100 			   char *buf,
   12101 			   int *bufsiz,
   12102 			   const void *ppc_tm_cvmx,
   12103 			   int size)
   12104 {
   12105   char *note_name = "LINUX";
   12106   return elfcore_write_note (abfd, buf, bufsiz,
   12107 			     note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
   12108 }
   12109 
   12110 char *
   12111 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12112 			   char *buf,
   12113 			   int *bufsiz,
   12114 			   const void *ppc_tm_cvsx,
   12115 			   int size)
   12116 {
   12117   char *note_name = "LINUX";
   12118   return elfcore_write_note (abfd, buf, bufsiz,
   12119 			     note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
   12120 }
   12121 
   12122 char *
   12123 elfcore_write_ppc_tm_spr (bfd *abfd,
   12124 			  char *buf,
   12125 			  int *bufsiz,
   12126 			  const void *ppc_tm_spr,
   12127 			  int size)
   12128 {
   12129   char *note_name = "LINUX";
   12130   return elfcore_write_note (abfd, buf, bufsiz,
   12131 			     note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
   12132 }
   12133 
   12134 char *
   12135 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12136 			   char *buf,
   12137 			   int *bufsiz,
   12138 			   const void *ppc_tm_ctar,
   12139 			   int size)
   12140 {
   12141   char *note_name = "LINUX";
   12142   return elfcore_write_note (abfd, buf, bufsiz,
   12143 			     note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
   12144 }
   12145 
   12146 char *
   12147 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12148 			   char *buf,
   12149 			   int *bufsiz,
   12150 			   const void *ppc_tm_cppr,
   12151 			   int size)
   12152 {
   12153   char *note_name = "LINUX";
   12154   return elfcore_write_note (abfd, buf, bufsiz,
   12155 			     note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
   12156 }
   12157 
   12158 char *
   12159 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12160 			    char *buf,
   12161 			    int *bufsiz,
   12162 			    const void *ppc_tm_cdscr,
   12163 			    int size)
   12164 {
   12165   char *note_name = "LINUX";
   12166   return elfcore_write_note (abfd, buf, bufsiz,
   12167 			     note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
   12168 }
   12169 
   12170 static char *
   12171 elfcore_write_s390_high_gprs (bfd *abfd,
   12172 			      char *buf,
   12173 			      int *bufsiz,
   12174 			      const void *s390_high_gprs,
   12175 			      int size)
   12176 {
   12177   char *note_name = "LINUX";
   12178   return elfcore_write_note (abfd, buf, bufsiz,
   12179 			     note_name, NT_S390_HIGH_GPRS,
   12180 			     s390_high_gprs, size);
   12181 }
   12182 
   12183 char *
   12184 elfcore_write_s390_timer (bfd *abfd,
   12185 			  char *buf,
   12186 			  int *bufsiz,
   12187 			  const void *s390_timer,
   12188 			  int size)
   12189 {
   12190   char *note_name = "LINUX";
   12191   return elfcore_write_note (abfd, buf, bufsiz,
   12192 			     note_name, NT_S390_TIMER, s390_timer, size);
   12193 }
   12194 
   12195 char *
   12196 elfcore_write_s390_todcmp (bfd *abfd,
   12197 			   char *buf,
   12198 			   int *bufsiz,
   12199 			   const void *s390_todcmp,
   12200 			   int size)
   12201 {
   12202   char *note_name = "LINUX";
   12203   return elfcore_write_note (abfd, buf, bufsiz,
   12204 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   12205 }
   12206 
   12207 char *
   12208 elfcore_write_s390_todpreg (bfd *abfd,
   12209 			    char *buf,
   12210 			    int *bufsiz,
   12211 			    const void *s390_todpreg,
   12212 			    int size)
   12213 {
   12214   char *note_name = "LINUX";
   12215   return elfcore_write_note (abfd, buf, bufsiz,
   12216 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   12217 }
   12218 
   12219 char *
   12220 elfcore_write_s390_ctrs (bfd *abfd,
   12221 			 char *buf,
   12222 			 int *bufsiz,
   12223 			 const void *s390_ctrs,
   12224 			 int size)
   12225 {
   12226   char *note_name = "LINUX";
   12227   return elfcore_write_note (abfd, buf, bufsiz,
   12228 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   12229 }
   12230 
   12231 char *
   12232 elfcore_write_s390_prefix (bfd *abfd,
   12233 			   char *buf,
   12234 			   int *bufsiz,
   12235 			   const void *s390_prefix,
   12236 			   int size)
   12237 {
   12238   char *note_name = "LINUX";
   12239   return elfcore_write_note (abfd, buf, bufsiz,
   12240 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   12241 }
   12242 
   12243 char *
   12244 elfcore_write_s390_last_break (bfd *abfd,
   12245 			       char *buf,
   12246 			       int *bufsiz,
   12247 			       const void *s390_last_break,
   12248 			       int size)
   12249 {
   12250   char *note_name = "LINUX";
   12251   return elfcore_write_note (abfd, buf, bufsiz,
   12252 			     note_name, NT_S390_LAST_BREAK,
   12253 			     s390_last_break, size);
   12254 }
   12255 
   12256 char *
   12257 elfcore_write_s390_system_call (bfd *abfd,
   12258 				char *buf,
   12259 				int *bufsiz,
   12260 				const void *s390_system_call,
   12261 				int size)
   12262 {
   12263   char *note_name = "LINUX";
   12264   return elfcore_write_note (abfd, buf, bufsiz,
   12265 			     note_name, NT_S390_SYSTEM_CALL,
   12266 			     s390_system_call, size);
   12267 }
   12268 
   12269 char *
   12270 elfcore_write_s390_tdb (bfd *abfd,
   12271 			char *buf,
   12272 			int *bufsiz,
   12273 			const void *s390_tdb,
   12274 			int size)
   12275 {
   12276   char *note_name = "LINUX";
   12277   return elfcore_write_note (abfd, buf, bufsiz,
   12278 			     note_name, NT_S390_TDB, s390_tdb, size);
   12279 }
   12280 
   12281 char *
   12282 elfcore_write_s390_vxrs_low (bfd *abfd,
   12283 			     char *buf,
   12284 			     int *bufsiz,
   12285 			     const void *s390_vxrs_low,
   12286 			     int size)
   12287 {
   12288   char *note_name = "LINUX";
   12289   return elfcore_write_note (abfd, buf, bufsiz,
   12290 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   12291 }
   12292 
   12293 char *
   12294 elfcore_write_s390_vxrs_high (bfd *abfd,
   12295 			     char *buf,
   12296 			     int *bufsiz,
   12297 			     const void *s390_vxrs_high,
   12298 			     int size)
   12299 {
   12300   char *note_name = "LINUX";
   12301   return elfcore_write_note (abfd, buf, bufsiz,
   12302 			     note_name, NT_S390_VXRS_HIGH,
   12303 			     s390_vxrs_high, size);
   12304 }
   12305 
   12306 char *
   12307 elfcore_write_s390_gs_cb (bfd *abfd,
   12308 			  char *buf,
   12309 			  int *bufsiz,
   12310 			  const void *s390_gs_cb,
   12311 			  int size)
   12312 {
   12313   char *note_name = "LINUX";
   12314   return elfcore_write_note (abfd, buf, bufsiz,
   12315 			     note_name, NT_S390_GS_CB,
   12316 			     s390_gs_cb, size);
   12317 }
   12318 
   12319 char *
   12320 elfcore_write_s390_gs_bc (bfd *abfd,
   12321 			  char *buf,
   12322 			  int *bufsiz,
   12323 			  const void *s390_gs_bc,
   12324 			  int size)
   12325 {
   12326   char *note_name = "LINUX";
   12327   return elfcore_write_note (abfd, buf, bufsiz,
   12328 			     note_name, NT_S390_GS_BC,
   12329 			     s390_gs_bc, size);
   12330 }
   12331 
   12332 char *
   12333 elfcore_write_arm_vfp (bfd *abfd,
   12334 		       char *buf,
   12335 		       int *bufsiz,
   12336 		       const void *arm_vfp,
   12337 		       int size)
   12338 {
   12339   char *note_name = "LINUX";
   12340   return elfcore_write_note (abfd, buf, bufsiz,
   12341 			     note_name, NT_ARM_VFP, arm_vfp, size);
   12342 }
   12343 
   12344 char *
   12345 elfcore_write_aarch_tls (bfd *abfd,
   12346 		       char *buf,
   12347 		       int *bufsiz,
   12348 		       const void *aarch_tls,
   12349 		       int size)
   12350 {
   12351   char *note_name = "LINUX";
   12352   return elfcore_write_note (abfd, buf, bufsiz,
   12353 			     note_name, NT_ARM_TLS, aarch_tls, size);
   12354 }
   12355 
   12356 char *
   12357 elfcore_write_aarch_hw_break (bfd *abfd,
   12358 			    char *buf,
   12359 			    int *bufsiz,
   12360 			    const void *aarch_hw_break,
   12361 			    int size)
   12362 {
   12363   char *note_name = "LINUX";
   12364   return elfcore_write_note (abfd, buf, bufsiz,
   12365 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   12366 }
   12367 
   12368 char *
   12369 elfcore_write_aarch_hw_watch (bfd *abfd,
   12370 			    char *buf,
   12371 			    int *bufsiz,
   12372 			    const void *aarch_hw_watch,
   12373 			    int size)
   12374 {
   12375   char *note_name = "LINUX";
   12376   return elfcore_write_note (abfd, buf, bufsiz,
   12377 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   12378 }
   12379 
   12380 char *
   12381 elfcore_write_aarch_sve (bfd *abfd,
   12382 			 char *buf,
   12383 			 int *bufsiz,
   12384 			 const void *aarch_sve,
   12385 			 int size)
   12386 {
   12387   char *note_name = "LINUX";
   12388   return elfcore_write_note (abfd, buf, bufsiz,
   12389 			     note_name, NT_ARM_SVE, aarch_sve, size);
   12390 }
   12391 
   12392 char *
   12393 elfcore_write_aarch_pauth (bfd *abfd,
   12394 			   char *buf,
   12395 			   int *bufsiz,
   12396 			   const void *aarch_pauth,
   12397 			   int size)
   12398 {
   12399   char *note_name = "LINUX";
   12400   return elfcore_write_note (abfd, buf, bufsiz,
   12401 			     note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
   12402 }
   12403 
   12404 char *
   12405 elfcore_write_aarch_mte (bfd *abfd,
   12406 				      char *buf,
   12407 				      int *bufsiz,
   12408 				      const void *aarch_mte,
   12409 				      int size)
   12410 {
   12411   char *note_name = "LINUX";
   12412   return elfcore_write_note (abfd, buf, bufsiz,
   12413 			     note_name, NT_ARM_TAGGED_ADDR_CTRL,
   12414 			     aarch_mte,
   12415 			     size);
   12416 }
   12417 
   12418 char *
   12419 elfcore_write_arc_v2 (bfd *abfd,
   12420 		      char *buf,
   12421 		      int *bufsiz,
   12422 		      const void *arc_v2,
   12423 		      int size)
   12424 {
   12425   char *note_name = "LINUX";
   12426   return elfcore_write_note (abfd, buf, bufsiz,
   12427 			     note_name, NT_ARC_V2, arc_v2, size);
   12428 }
   12429 
   12430 char *
   12431 elfcore_write_loongarch_cpucfg (bfd *abfd,
   12432 				char *buf,
   12433 				int *bufsiz,
   12434 				const void *loongarch_cpucfg,
   12435 				int size)
   12436 {
   12437   char *note_name = "LINUX";
   12438   return elfcore_write_note (abfd, buf, bufsiz,
   12439 			     note_name, NT_LARCH_CPUCFG,
   12440 			     loongarch_cpucfg, size);
   12441 }
   12442 
   12443 char *
   12444 elfcore_write_loongarch_lbt (bfd *abfd,
   12445 			     char *buf,
   12446 			     int *bufsiz,
   12447 			     const void *loongarch_lbt,
   12448 			     int size)
   12449 {
   12450   char *note_name = "LINUX";
   12451   return elfcore_write_note (abfd, buf, bufsiz,
   12452 			     note_name, NT_LARCH_LBT, loongarch_lbt, size);
   12453 }
   12454 
   12455 char *
   12456 elfcore_write_loongarch_lsx (bfd *abfd,
   12457 			     char *buf,
   12458 			     int *bufsiz,
   12459 			     const void *loongarch_lsx,
   12460 			     int size)
   12461 {
   12462   char *note_name = "LINUX";
   12463   return elfcore_write_note (abfd, buf, bufsiz,
   12464 			     note_name, NT_LARCH_LSX, loongarch_lsx, size);
   12465 }
   12466 
   12467 char *
   12468 elfcore_write_loongarch_lasx (bfd *abfd,
   12469 			      char *buf,
   12470 			      int *bufsiz,
   12471 			      const void *loongarch_lasx,
   12472 			      int size)
   12473 {
   12474   char *note_name = "LINUX";
   12475   return elfcore_write_note (abfd, buf, bufsiz,
   12476 			     note_name, NT_LARCH_LASX, loongarch_lasx, size);
   12477 }
   12478 
   12479 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   12480    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12481    written into.  Return a pointer to the new start of the note buffer, to
   12482    replace BUF which may no longer be valid.  */
   12483 
   12484 char *
   12485 elfcore_write_riscv_csr (bfd *abfd,
   12486 			 char *buf,
   12487 			 int *bufsiz,
   12488 			 const void *csrs,
   12489 			 int size)
   12490 {
   12491   const char *note_name = "GDB";
   12492   return elfcore_write_note (abfd, buf, bufsiz,
   12493 			     note_name, NT_RISCV_CSR, csrs, size);
   12494 }
   12495 
   12496 /* Write the target description (a string) pointed to by TDESC, length
   12497    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   12498    note is being written into.  Return a pointer to the new start of the
   12499    note buffer, to replace BUF which may no longer be valid.  */
   12500 
   12501 char *
   12502 elfcore_write_gdb_tdesc (bfd *abfd,
   12503 			 char *buf,
   12504 			 int *bufsiz,
   12505 			 const void *tdesc,
   12506 			 int size)
   12507 {
   12508   const char *note_name = "GDB";
   12509   return elfcore_write_note (abfd, buf, bufsiz,
   12510 			     note_name, NT_GDB_TDESC, tdesc, size);
   12511 }
   12512 
   12513 char *
   12514 elfcore_write_register_note (bfd *abfd,
   12515 			     char *buf,
   12516 			     int *bufsiz,
   12517 			     const char *section,
   12518 			     const void *data,
   12519 			     int size)
   12520 {
   12521   if (strcmp (section, ".reg2") == 0)
   12522     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   12523   if (strcmp (section, ".reg-xfp") == 0)
   12524     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   12525   if (strcmp (section, ".reg-xstate") == 0)
   12526     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   12527   if (strcmp (section, ".reg-x86-segbases") == 0)
   12528     return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
   12529   if (strcmp (section, ".reg-ppc-vmx") == 0)
   12530     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   12531   if (strcmp (section, ".reg-ppc-vsx") == 0)
   12532     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   12533   if (strcmp (section, ".reg-ppc-tar") == 0)
   12534     return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
   12535   if (strcmp (section, ".reg-ppc-ppr") == 0)
   12536     return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
   12537   if (strcmp (section, ".reg-ppc-dscr") == 0)
   12538     return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
   12539   if (strcmp (section, ".reg-ppc-ebb") == 0)
   12540     return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
   12541   if (strcmp (section, ".reg-ppc-pmu") == 0)
   12542     return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
   12543   if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
   12544     return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
   12545   if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
   12546     return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
   12547   if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
   12548     return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
   12549   if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
   12550     return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
   12551   if (strcmp (section, ".reg-ppc-tm-spr") == 0)
   12552     return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
   12553   if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
   12554     return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
   12555   if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
   12556     return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
   12557   if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
   12558     return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
   12559   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   12560     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   12561   if (strcmp (section, ".reg-s390-timer") == 0)
   12562     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   12563   if (strcmp (section, ".reg-s390-todcmp") == 0)
   12564     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   12565   if (strcmp (section, ".reg-s390-todpreg") == 0)
   12566     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   12567   if (strcmp (section, ".reg-s390-ctrs") == 0)
   12568     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   12569   if (strcmp (section, ".reg-s390-prefix") == 0)
   12570     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   12571   if (strcmp (section, ".reg-s390-last-break") == 0)
   12572     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   12573   if (strcmp (section, ".reg-s390-system-call") == 0)
   12574     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   12575   if (strcmp (section, ".reg-s390-tdb") == 0)
   12576     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   12577   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   12578     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   12579   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   12580     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   12581   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   12582     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   12583   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   12584     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   12585   if (strcmp (section, ".reg-arm-vfp") == 0)
   12586     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   12587   if (strcmp (section, ".reg-aarch-tls") == 0)
   12588     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   12589   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   12590     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   12591   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   12592     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   12593   if (strcmp (section, ".reg-aarch-sve") == 0)
   12594     return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
   12595   if (strcmp (section, ".reg-aarch-pauth") == 0)
   12596     return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
   12597   if (strcmp (section, ".reg-aarch-mte") == 0)
   12598     return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
   12599   if (strcmp (section, ".reg-arc-v2") == 0)
   12600     return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
   12601   if (strcmp (section, ".gdb-tdesc") == 0)
   12602     return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
   12603   if (strcmp (section, ".reg-riscv-csr") == 0)
   12604     return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
   12605   if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
   12606     return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
   12607   if (strcmp (section, ".reg-loongarch-lbt") == 0)
   12608     return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
   12609   if (strcmp (section, ".reg-loongarch-lsx") == 0)
   12610     return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
   12611   if (strcmp (section, ".reg-loongarch-lasx") == 0)
   12612     return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
   12613   return NULL;
   12614 }
   12615 
   12616 char *
   12617 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   12618 			 const void *buf, int bufsiz)
   12619 {
   12620   return elfcore_write_note (obfd, note_data, note_size,
   12621 			     "CORE", NT_FILE, buf, bufsiz);
   12622 }
   12623 
   12624 static bool
   12625 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   12626 		 size_t align)
   12627 {
   12628   char *p;
   12629 
   12630   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   12631      gABI specifies that PT_NOTE alignment should be aligned to 4
   12632      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   12633      align is less than 4, we use 4 byte alignment.   */
   12634   if (align < 4)
   12635     align = 4;
   12636   if (align != 4 && align != 8)
   12637     return false;
   12638 
   12639   p = buf;
   12640   while (p < buf + size)
   12641     {
   12642       Elf_External_Note *xnp = (Elf_External_Note *) p;
   12643       Elf_Internal_Note in;
   12644 
   12645       if (offsetof (Elf_External_Note, name) > buf - p + size)
   12646 	return false;
   12647 
   12648       in.type = H_GET_32 (abfd, xnp->type);
   12649 
   12650       in.namesz = H_GET_32 (abfd, xnp->namesz);
   12651       in.namedata = xnp->name;
   12652       if (in.namesz > buf - in.namedata + size)
   12653 	return false;
   12654 
   12655       in.descsz = H_GET_32 (abfd, xnp->descsz);
   12656       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   12657       in.descpos = offset + (in.descdata - buf);
   12658       if (in.descsz != 0
   12659 	  && (in.descdata >= buf + size
   12660 	      || in.descsz > buf - in.descdata + size))
   12661 	return false;
   12662 
   12663       switch (bfd_get_format (abfd))
   12664 	{
   12665 	default:
   12666 	  return true;
   12667 
   12668 	case bfd_core:
   12669 	  {
   12670 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   12671 	    struct
   12672 	    {
   12673 	      const char * string;
   12674 	      size_t len;
   12675 	      bool (*func) (bfd *, Elf_Internal_Note *);
   12676 	    }
   12677 	    grokers[] =
   12678 	    {
   12679 	      GROKER_ELEMENT ("", elfcore_grok_note),
   12680 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   12681 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   12682 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   12683 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   12684 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   12685 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   12686 	      GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
   12687 	    };
   12688 #undef GROKER_ELEMENT
   12689 	    int i;
   12690 
   12691 	    for (i = ARRAY_SIZE (grokers); i--;)
   12692 	      {
   12693 		if (in.namesz >= grokers[i].len
   12694 		    && strncmp (in.namedata, grokers[i].string,
   12695 				grokers[i].len) == 0)
   12696 		  {
   12697 		    if (! grokers[i].func (abfd, & in))
   12698 		      return false;
   12699 		    break;
   12700 		  }
   12701 	      }
   12702 	    break;
   12703 	  }
   12704 
   12705 	case bfd_object:
   12706 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   12707 	    {
   12708 	      if (! elfobj_grok_gnu_note (abfd, &in))
   12709 		return false;
   12710 	    }
   12711 	  else if (in.namesz == sizeof "stapsdt"
   12712 		   && strcmp (in.namedata, "stapsdt") == 0)
   12713 	    {
   12714 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   12715 		return false;
   12716 	    }
   12717 	  break;
   12718 	}
   12719 
   12720       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   12721     }
   12722 
   12723   return true;
   12724 }
   12725 
   12726 bool
   12727 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   12728 		size_t align)
   12729 {
   12730   char *buf;
   12731 
   12732   if (size == 0 || (size + 1) == 0)
   12733     return true;
   12734 
   12735   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   12736     return false;
   12737 
   12738   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   12739   if (buf == NULL)
   12740     return false;
   12741 
   12742   /* PR 17512: file: ec08f814
   12743      0-termintate the buffer so that string searches will not overflow.  */
   12744   buf[size] = 0;
   12745 
   12746   if (!elf_parse_notes (abfd, buf, size, offset, align))
   12747     {
   12748       free (buf);
   12749       return false;
   12750     }
   12751 
   12752   free (buf);
   12753   return true;
   12754 }
   12755 
   12756 /* Providing external access to the ELF program header table.  */
   12758 
   12759 /* Return an upper bound on the number of bytes required to store a
   12760    copy of ABFD's program header table entries.  Return -1 if an error
   12761    occurs; bfd_get_error will return an appropriate code.  */
   12762 
   12763 long
   12764 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   12765 {
   12766   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   12767     {
   12768       bfd_set_error (bfd_error_wrong_format);
   12769       return -1;
   12770     }
   12771 
   12772   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   12773 }
   12774 
   12775 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   12776    will be stored as an array of Elf_Internal_Phdr structures, as
   12777    defined in include/elf/internal.h.  To find out how large the
   12778    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   12779 
   12780    Return the number of program header table entries read, or -1 if an
   12781    error occurs; bfd_get_error will return an appropriate code.  */
   12782 
   12783 int
   12784 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   12785 {
   12786   int num_phdrs;
   12787 
   12788   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   12789     {
   12790       bfd_set_error (bfd_error_wrong_format);
   12791       return -1;
   12792     }
   12793 
   12794   num_phdrs = elf_elfheader (abfd)->e_phnum;
   12795   if (num_phdrs != 0)
   12796     memcpy (phdrs, elf_tdata (abfd)->phdr,
   12797 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   12798 
   12799   return num_phdrs;
   12800 }
   12801 
   12802 enum elf_reloc_type_class
   12803 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12804 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   12805 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   12806 {
   12807   return reloc_class_normal;
   12808 }
   12809 
   12810 /* For RELA architectures, return the relocation value for a
   12811    relocation against a local symbol.  */
   12812 
   12813 bfd_vma
   12814 _bfd_elf_rela_local_sym (bfd *abfd,
   12815 			 Elf_Internal_Sym *sym,
   12816 			 asection **psec,
   12817 			 Elf_Internal_Rela *rel)
   12818 {
   12819   asection *sec = *psec;
   12820   bfd_vma relocation;
   12821 
   12822   relocation = (sec->output_section->vma
   12823 		+ sec->output_offset
   12824 		+ sym->st_value);
   12825   if ((sec->flags & SEC_MERGE)
   12826       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   12827       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   12828     {
   12829       rel->r_addend =
   12830 	_bfd_merged_section_offset (abfd, psec,
   12831 				    elf_section_data (sec)->sec_info,
   12832 				    sym->st_value + rel->r_addend);
   12833       if (sec != *psec)
   12834 	{
   12835 	  /* If we have changed the section, and our original section is
   12836 	     marked with SEC_EXCLUDE, it means that the original
   12837 	     SEC_MERGE section has been completely subsumed in some
   12838 	     other SEC_MERGE section.  In this case, we need to leave
   12839 	     some info around for --emit-relocs.  */
   12840 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   12841 	    sec->kept_section = *psec;
   12842 	  sec = *psec;
   12843 	}
   12844       rel->r_addend -= relocation;
   12845       rel->r_addend += sec->output_section->vma + sec->output_offset;
   12846     }
   12847   return relocation;
   12848 }
   12849 
   12850 bfd_vma
   12851 _bfd_elf_rel_local_sym (bfd *abfd,
   12852 			Elf_Internal_Sym *sym,
   12853 			asection **psec,
   12854 			bfd_vma addend)
   12855 {
   12856   asection *sec = *psec;
   12857 
   12858   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   12859     return sym->st_value + addend;
   12860 
   12861   return _bfd_merged_section_offset (abfd, psec,
   12862 				     elf_section_data (sec)->sec_info,
   12863 				     sym->st_value + addend);
   12864 }
   12865 
   12866 /* Adjust an address within a section.  Given OFFSET within SEC, return
   12867    the new offset within the section, based upon changes made to the
   12868    section.  Returns -1 if the offset is now invalid.
   12869    The offset (in abnd out) is in target sized bytes, however big a
   12870    byte may be.  */
   12871 
   12872 bfd_vma
   12873 _bfd_elf_section_offset (bfd *abfd,
   12874 			 struct bfd_link_info *info,
   12875 			 asection *sec,
   12876 			 bfd_vma offset)
   12877 {
   12878   switch (sec->sec_info_type)
   12879     {
   12880     case SEC_INFO_TYPE_STABS:
   12881       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   12882 				       offset);
   12883     case SEC_INFO_TYPE_EH_FRAME:
   12884       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   12885 
   12886     default:
   12887       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   12888 	{
   12889 	  /* Reverse the offset.  */
   12890 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12891 	  bfd_size_type address_size = bed->s->arch_size / 8;
   12892 
   12893 	  /* address_size and sec->size are in octets.  Convert
   12894 	     to bytes before subtracting the original offset.  */
   12895 	  offset = ((sec->size - address_size)
   12896 		    / bfd_octets_per_byte (abfd, sec) - offset);
   12897 	}
   12898       return offset;
   12899     }
   12900 }
   12901 
   12902 /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   12904    reconstruct an ELF file by reading the segments out of remote memory
   12905    based on the ELF file header at EHDR_VMA and the ELF program headers it
   12906    points to.  If not null, *LOADBASEP is filled in with the difference
   12907    between the VMAs from which the segments were read, and the VMAs the
   12908    file headers (and hence BFD's idea of each section's VMA) put them at.
   12909 
   12910    The function TARGET_READ_MEMORY is called to copy LEN bytes from the
   12911    remote memory at target address VMA into the local buffer at MYADDR; it
   12912    should return zero on success or an `errno' code on failure.  TEMPL must
   12913    be a BFD for an ELF target with the word size and byte order found in
   12914    the remote memory.  */
   12915 
   12916 bfd *
   12917 bfd_elf_bfd_from_remote_memory
   12918   (bfd *templ,
   12919    bfd_vma ehdr_vma,
   12920    bfd_size_type size,
   12921    bfd_vma *loadbasep,
   12922    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   12923 {
   12924   return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
   12925     (templ, ehdr_vma, size, loadbasep, target_read_memory);
   12926 }
   12927 
   12928 long
   12930 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   12931 			       long symcount ATTRIBUTE_UNUSED,
   12932 			       asymbol **syms ATTRIBUTE_UNUSED,
   12933 			       long dynsymcount,
   12934 			       asymbol **dynsyms,
   12935 			       asymbol **ret)
   12936 {
   12937   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12938   asection *relplt;
   12939   asymbol *s;
   12940   const char *relplt_name;
   12941   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   12942   arelent *p;
   12943   long count, i, n;
   12944   size_t size;
   12945   Elf_Internal_Shdr *hdr;
   12946   char *names;
   12947   asection *plt;
   12948 
   12949   *ret = NULL;
   12950 
   12951   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   12952     return 0;
   12953 
   12954   if (dynsymcount <= 0)
   12955     return 0;
   12956 
   12957   if (!bed->plt_sym_val)
   12958     return 0;
   12959 
   12960   relplt_name = bed->relplt_name;
   12961   if (relplt_name == NULL)
   12962     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   12963   relplt = bfd_get_section_by_name (abfd, relplt_name);
   12964   if (relplt == NULL)
   12965     return 0;
   12966 
   12967   hdr = &elf_section_data (relplt)->this_hdr;
   12968   if (hdr->sh_link != elf_dynsymtab (abfd)
   12969       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   12970     return 0;
   12971 
   12972   plt = bfd_get_section_by_name (abfd, ".plt");
   12973   if (plt == NULL)
   12974     return 0;
   12975 
   12976   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   12977   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   12978     return -1;
   12979 
   12980   count = relplt->size / hdr->sh_entsize;
   12981   size = count * sizeof (asymbol);
   12982   p = relplt->relocation;
   12983   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12984     {
   12985       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   12986       if (p->addend != 0)
   12987 	{
   12988 #ifdef BFD64
   12989 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   12990 #else
   12991 	  size += sizeof ("+0x") - 1 + 8;
   12992 #endif
   12993 	}
   12994     }
   12995 
   12996   s = *ret = (asymbol *) bfd_malloc (size);
   12997   if (s == NULL)
   12998     return -1;
   12999 
   13000   names = (char *) (s + count);
   13001   p = relplt->relocation;
   13002   n = 0;
   13003   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13004     {
   13005       size_t len;
   13006       bfd_vma addr;
   13007 
   13008       addr = bed->plt_sym_val (i, plt, p);
   13009       if (addr == (bfd_vma) -1)
   13010 	continue;
   13011 
   13012       *s = **p->sym_ptr_ptr;
   13013       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   13014 	 we are defining a symbol, ensure one of them is set.  */
   13015       if ((s->flags & BSF_LOCAL) == 0)
   13016 	s->flags |= BSF_GLOBAL;
   13017       s->flags |= BSF_SYNTHETIC;
   13018       s->section = plt;
   13019       s->value = addr - plt->vma;
   13020       s->name = names;
   13021       s->udata.p = NULL;
   13022       len = strlen ((*p->sym_ptr_ptr)->name);
   13023       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   13024       names += len;
   13025       if (p->addend != 0)
   13026 	{
   13027 	  char buf[30], *a;
   13028 
   13029 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   13030 	  names += sizeof ("+0x") - 1;
   13031 	  bfd_sprintf_vma (abfd, buf, p->addend);
   13032 	  for (a = buf; *a == '0'; ++a)
   13033 	    ;
   13034 	  len = strlen (a);
   13035 	  memcpy (names, a, len);
   13036 	  names += len;
   13037 	}
   13038       memcpy (names, "@plt", sizeof ("@plt"));
   13039       names += sizeof ("@plt");
   13040       ++s, ++n;
   13041     }
   13042 
   13043   return n;
   13044 }
   13045 
   13046 /* It is only used by x86-64 so far.
   13047    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   13048    but current usage would allow all of _bfd_std_section to be zero.  */
   13049 static const asymbol lcomm_sym
   13050   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   13051 asection _bfd_elf_large_com_section
   13052   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   13053 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   13054 
   13055 bool
   13056 _bfd_elf_final_write_processing (bfd *abfd)
   13057 {
   13058   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   13059 
   13060   i_ehdrp = elf_elfheader (abfd);
   13061 
   13062   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13063     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   13064 
   13065   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13066      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13067      or STB_GNU_UNIQUE binding.  */
   13068   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13069     {
   13070       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13071 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13072       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13073 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13074 	{
   13075 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13076 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13077 				  "and FreeBSD targets"));
   13078 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13079 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13080 				  "only by GNU and FreeBSD targets"));
   13081 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13082 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13083 				  "only by GNU and FreeBSD targets"));
   13084 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13085 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13086 				  "only by GNU and FreeBSD targets"));
   13087 	  bfd_set_error (bfd_error_sorry);
   13088 	  return false;
   13089 	}
   13090     }
   13091   return true;
   13092 }
   13093 
   13094 
   13095 /* Return TRUE for ELF symbol types that represent functions.
   13096    This is the default version of this function, which is sufficient for
   13097    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13098 
   13099 bool
   13100 _bfd_elf_is_function_type (unsigned int type)
   13101 {
   13102   return (type == STT_FUNC
   13103 	  || type == STT_GNU_IFUNC);
   13104 }
   13105 
   13106 /* If the ELF symbol SYM might be a function in SEC, return the
   13107    function size and set *CODE_OFF to the function's entry point,
   13108    otherwise return zero.  */
   13109 
   13110 bfd_size_type
   13111 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13112 			     bfd_vma *code_off)
   13113 {
   13114   bfd_size_type size;
   13115   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13116 
   13117   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13118 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13119       || sym->section != sec)
   13120     return 0;
   13121 
   13122   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13123 
   13124   /* In theory we should check that the symbol's type satisfies
   13125      _bfd_elf_is_function_type(), but there are some function-like
   13126      symbols which would fail this test.  (eg _start).  Instead
   13127      we check for hidden, local, notype symbols with zero size.
   13128      This type of symbol is generated by the annobin plugin for gcc
   13129      and clang, and should not be considered to be a function symbol.  */
   13130   if (size == 0
   13131       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13132       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13133       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13134     return 0;
   13135 
   13136   *code_off = sym->value;
   13137   /* Do not return 0 for the function's size.  */
   13138   return size ? size : 1;
   13139 }
   13140 
   13141 /* Set to non-zero to enable some debug messages.  */
   13142 #define DEBUG_SECONDARY_RELOCS	 0
   13143 
   13144 /* An internal-to-the-bfd-library only section type
   13145    used to indicate a cached secondary reloc section.  */
   13146 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13147 
   13148 /* Create a BFD section to hold a secondary reloc section.  */
   13149 
   13150 bool
   13151 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13152 				       Elf_Internal_Shdr *hdr,
   13153 				       const char * name,
   13154 				       unsigned int shindex)
   13155 {
   13156   /* We only support RELA secondary relocs.  */
   13157   if (hdr->sh_type != SHT_RELA)
   13158     return false;
   13159 
   13160 #if DEBUG_SECONDARY_RELOCS
   13161   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13162 #endif
   13163   hdr->sh_type = SHT_SECONDARY_RELOC;
   13164   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13165 }
   13166 
   13167 /* Read in any secondary relocs associated with SEC.  */
   13168 
   13169 bool
   13170 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13171 					asection *  sec,
   13172 					asymbol **  symbols,
   13173 					bool dynamic)
   13174 {
   13175   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13176   asection * relsec;
   13177   bool result = true;
   13178   bfd_vma (*r_sym) (bfd_vma);
   13179   ufile_ptr filesize;
   13180 
   13181 #if BFD_DEFAULT_TARGET_SIZE > 32
   13182   if (bfd_arch_bits_per_address (abfd) != 32)
   13183     r_sym = elf64_r_sym;
   13184   else
   13185 #endif
   13186     r_sym = elf32_r_sym;
   13187 
   13188   if (!elf_section_data (sec)->has_secondary_relocs)
   13189     return true;
   13190 
   13191   /* Discover if there are any secondary reloc sections
   13192      associated with SEC.  */
   13193   filesize = bfd_get_file_size (abfd);
   13194   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13195     {
   13196       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13197 
   13198       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13199 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13200 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13201 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13202 	{
   13203 	  bfd_byte * native_relocs;
   13204 	  bfd_byte * native_reloc;
   13205 	  arelent * internal_relocs;
   13206 	  arelent * internal_reloc;
   13207 	  size_t i;
   13208 	  unsigned int entsize;
   13209 	  unsigned int symcount;
   13210 	  bfd_size_type reloc_count;
   13211 	  size_t amt;
   13212 
   13213 	  if (ebd->elf_info_to_howto == NULL)
   13214 	    return false;
   13215 
   13216 #if DEBUG_SECONDARY_RELOCS
   13217 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13218 		   sec->name, relsec->name);
   13219 #endif
   13220 	  entsize = hdr->sh_entsize;
   13221 
   13222 	  if (filesize != 0
   13223 	      && ((ufile_ptr) hdr->sh_offset > filesize
   13224 		  || hdr->sh_size > filesize - hdr->sh_offset))
   13225 	    {
   13226 	      bfd_set_error (bfd_error_file_truncated);
   13227 	      result = false;
   13228 	      continue;
   13229 	    }
   13230 
   13231 	  native_relocs = bfd_malloc (hdr->sh_size);
   13232 	  if (native_relocs == NULL)
   13233 	    {
   13234 	      result = false;
   13235 	      continue;
   13236 	    }
   13237 
   13238 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13239 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13240 	    {
   13241 	      free (native_relocs);
   13242 	      bfd_set_error (bfd_error_file_too_big);
   13243 	      result = false;
   13244 	      continue;
   13245 	    }
   13246 
   13247 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   13248 	  if (internal_relocs == NULL)
   13249 	    {
   13250 	      free (native_relocs);
   13251 	      result = false;
   13252 	      continue;
   13253 	    }
   13254 
   13255 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   13256 	      || (bfd_bread (native_relocs, hdr->sh_size, abfd)
   13257 		  != hdr->sh_size))
   13258 	    {
   13259 	      free (native_relocs);
   13260 	      /* The internal_relocs will be freed when
   13261 		 the memory for the bfd is released.  */
   13262 	      result = false;
   13263 	      continue;
   13264 	    }
   13265 
   13266 	  if (dynamic)
   13267 	    symcount = bfd_get_dynamic_symcount (abfd);
   13268 	  else
   13269 	    symcount = bfd_get_symcount (abfd);
   13270 
   13271 	  for (i = 0, internal_reloc = internal_relocs,
   13272 		 native_reloc = native_relocs;
   13273 	       i < reloc_count;
   13274 	       i++, internal_reloc++, native_reloc += entsize)
   13275 	    {
   13276 	      bool res;
   13277 	      Elf_Internal_Rela rela;
   13278 
   13279 	      if (entsize == ebd->s->sizeof_rel)
   13280 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   13281 	      else /* entsize == ebd->s->sizeof_rela */
   13282 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   13283 
   13284 	      /* The address of an ELF reloc is section relative for an object
   13285 		 file, and absolute for an executable file or shared library.
   13286 		 The address of a normal BFD reloc is always section relative,
   13287 		 and the address of a dynamic reloc is absolute..  */
   13288 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   13289 		internal_reloc->address = rela.r_offset;
   13290 	      else
   13291 		internal_reloc->address = rela.r_offset - sec->vma;
   13292 
   13293 	      if (r_sym (rela.r_info) == STN_UNDEF)
   13294 		{
   13295 		  /* FIXME: This and the error case below mean that we
   13296 		     have a symbol on relocs that is not elf_symbol_type.  */
   13297 		  internal_reloc->sym_ptr_ptr =
   13298 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   13299 		}
   13300 	      else if (r_sym (rela.r_info) > symcount)
   13301 		{
   13302 		  _bfd_error_handler
   13303 		    /* xgettext:c-format */
   13304 		    (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
   13305 		     abfd, sec, i, (long) r_sym (rela.r_info));
   13306 		  bfd_set_error (bfd_error_bad_value);
   13307 		  internal_reloc->sym_ptr_ptr =
   13308 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   13309 		  result = false;
   13310 		}
   13311 	      else
   13312 		{
   13313 		  asymbol **ps;
   13314 
   13315 		  ps = symbols + r_sym (rela.r_info) - 1;
   13316 		  internal_reloc->sym_ptr_ptr = ps;
   13317 		  /* Make sure that this symbol is not removed by strip.  */
   13318 		  (*ps)->flags |= BSF_KEEP;
   13319 		}
   13320 
   13321 	      internal_reloc->addend = rela.r_addend;
   13322 
   13323 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   13324 	      if (! res || internal_reloc->howto == NULL)
   13325 		{
   13326 #if DEBUG_SECONDARY_RELOCS
   13327 		  fprintf (stderr,
   13328 			   "there is no howto associated with reloc %lx\n",
   13329 			   rela.r_info);
   13330 #endif
   13331 		  result = false;
   13332 		}
   13333 	    }
   13334 
   13335 	  free (native_relocs);
   13336 	  /* Store the internal relocs.  */
   13337 	  elf_section_data (relsec)->sec_info = internal_relocs;
   13338 	}
   13339     }
   13340 
   13341   return result;
   13342 }
   13343 
   13344 /* Set the ELF section header fields of an output secondary reloc section.  */
   13345 
   13346 bool
   13347 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
   13348 				      bfd *obfd ATTRIBUTE_UNUSED,
   13349 				      const Elf_Internal_Shdr *isection,
   13350 				      Elf_Internal_Shdr *osection)
   13351 {
   13352   asection * isec;
   13353   asection * osec;
   13354   struct bfd_elf_section_data * esd;
   13355 
   13356   if (isection == NULL)
   13357     return false;
   13358 
   13359   if (isection->sh_type != SHT_SECONDARY_RELOC)
   13360     return true;
   13361 
   13362   isec = isection->bfd_section;
   13363   if (isec == NULL)
   13364     return false;
   13365 
   13366   osec = osection->bfd_section;
   13367   if (osec == NULL)
   13368     return false;
   13369 
   13370   esd = elf_section_data (osec);
   13371   BFD_ASSERT (esd->sec_info == NULL);
   13372   esd->sec_info = elf_section_data (isec)->sec_info;
   13373   osection->sh_type = SHT_RELA;
   13374   osection->sh_link = elf_onesymtab (obfd);
   13375   if (osection->sh_link == 0)
   13376     {
   13377       /* There is no symbol table - we are hosed...  */
   13378       _bfd_error_handler
   13379 	/* xgettext:c-format */
   13380 	(_("%pB(%pA): link section cannot be set"
   13381 	   " because the output file does not have a symbol table"),
   13382 	obfd, osec);
   13383       bfd_set_error (bfd_error_bad_value);
   13384       return false;
   13385     }
   13386 
   13387   /* Find the output section that corresponds to the isection's
   13388      sh_info link.  */
   13389   if (isection->sh_info == 0
   13390       || isection->sh_info >= elf_numsections (ibfd))
   13391     {
   13392       _bfd_error_handler
   13393 	/* xgettext:c-format */
   13394 	(_("%pB(%pA): info section index is invalid"),
   13395 	obfd, osec);
   13396       bfd_set_error (bfd_error_bad_value);
   13397       return false;
   13398     }
   13399 
   13400   isection = elf_elfsections (ibfd)[isection->sh_info];
   13401 
   13402   if (isection == NULL
   13403       || isection->bfd_section == NULL
   13404       || isection->bfd_section->output_section == NULL)
   13405     {
   13406       _bfd_error_handler
   13407 	/* xgettext:c-format */
   13408 	(_("%pB(%pA): info section index cannot be set"
   13409 	   " because the section is not in the output"),
   13410 	obfd, osec);
   13411       bfd_set_error (bfd_error_bad_value);
   13412       return false;
   13413     }
   13414 
   13415   esd = elf_section_data (isection->bfd_section->output_section);
   13416   BFD_ASSERT (esd != NULL);
   13417   osection->sh_info = esd->this_idx;
   13418   esd->has_secondary_relocs = true;
   13419 #if DEBUG_SECONDARY_RELOCS
   13420   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   13421 	   osec->name, osection->sh_link, osection->sh_info);
   13422   fprintf (stderr, "mark section %s as having secondary relocs\n",
   13423 	   bfd_section_name (isection->bfd_section->output_section));
   13424 #endif
   13425 
   13426   return true;
   13427 }
   13428 
   13429 /* Write out a secondary reloc section.
   13430 
   13431    FIXME: Currently this function can result in a serious performance penalty
   13432    for files with secondary relocs and lots of sections.  The proper way to
   13433    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   13434    relocs together and then to have this function just walk that chain.  */
   13435 
   13436 bool
   13437 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   13438 {
   13439   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13440   bfd_vma addr_offset;
   13441   asection * relsec;
   13442   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   13443   bool result = true;
   13444 
   13445   if (sec == NULL)
   13446     return false;
   13447 
   13448 #if BFD_DEFAULT_TARGET_SIZE > 32
   13449   if (bfd_arch_bits_per_address (abfd) != 32)
   13450     r_info = elf64_r_info;
   13451   else
   13452 #endif
   13453     r_info = elf32_r_info;
   13454 
   13455   /* The address of an ELF reloc is section relative for an object
   13456      file, and absolute for an executable file or shared library.
   13457      The address of a BFD reloc is always section relative.  */
   13458   addr_offset = 0;
   13459   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   13460     addr_offset = sec->vma;
   13461 
   13462   /* Discover if there are any secondary reloc sections
   13463      associated with SEC.  */
   13464   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13465     {
   13466       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   13467       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   13468 
   13469       if (hdr->sh_type == SHT_RELA
   13470 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   13471 	{
   13472 	  asymbol *    last_sym;
   13473 	  int          last_sym_idx;
   13474 	  size_t       reloc_count;
   13475 	  size_t       idx;
   13476 	  bfd_size_type entsize;
   13477 	  arelent *    src_irel;
   13478 	  bfd_byte *   dst_rela;
   13479 
   13480 	  if (hdr->contents != NULL)
   13481 	    {
   13482 	      _bfd_error_handler
   13483 		/* xgettext:c-format */
   13484 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   13485 		 abfd, relsec);
   13486 	      bfd_set_error (bfd_error_bad_value);
   13487 	      result = false;
   13488 	      continue;
   13489 	    }
   13490 
   13491 	  entsize = hdr->sh_entsize;
   13492 	  if (entsize == 0)
   13493 	    {
   13494 	      _bfd_error_handler
   13495 		/* xgettext:c-format */
   13496 		(_("%pB(%pA): error: secondary reloc section"
   13497 		   " has zero sized entries"),
   13498 		 abfd, relsec);
   13499 	      bfd_set_error (bfd_error_bad_value);
   13500 	      result = false;
   13501 	      continue;
   13502 	    }
   13503 	  else if (entsize != ebd->s->sizeof_rel
   13504 		   && entsize != ebd->s->sizeof_rela)
   13505 	    {
   13506 	      _bfd_error_handler
   13507 		/* xgettext:c-format */
   13508 		(_("%pB(%pA): error: secondary reloc section"
   13509 		   " has non-standard sized entries"),
   13510 		 abfd, relsec);
   13511 	      bfd_set_error (bfd_error_bad_value);
   13512 	      result = false;
   13513 	      continue;
   13514 	    }
   13515 
   13516 	  reloc_count = hdr->sh_size / entsize;
   13517 	  hdr->sh_size = entsize * reloc_count;
   13518 	  if (reloc_count == 0)
   13519 	    {
   13520 	      _bfd_error_handler
   13521 		/* xgettext:c-format */
   13522 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   13523 		 abfd, relsec);
   13524 	      bfd_set_error (bfd_error_bad_value);
   13525 	      result = false;
   13526 	      continue;
   13527 	    }
   13528 
   13529 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   13530 	  if (hdr->contents == NULL)
   13531 	    continue;
   13532 
   13533 #if DEBUG_SECONDARY_RELOCS
   13534 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   13535 		   reloc_count, sec->name, relsec->name);
   13536 #endif
   13537 	  last_sym = NULL;
   13538 	  last_sym_idx = 0;
   13539 	  dst_rela = hdr->contents;
   13540 	  src_irel = (arelent *) esd->sec_info;
   13541 	  if (src_irel == NULL)
   13542 	    {
   13543 	      _bfd_error_handler
   13544 		/* xgettext:c-format */
   13545 		(_("%pB(%pA): error: internal relocs missing"
   13546 		   " for secondary reloc section"),
   13547 		 abfd, relsec);
   13548 	      bfd_set_error (bfd_error_bad_value);
   13549 	      result = false;
   13550 	      continue;
   13551 	    }
   13552 
   13553 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   13554 	    {
   13555 	      Elf_Internal_Rela src_rela;
   13556 	      arelent *ptr;
   13557 	      asymbol *sym;
   13558 	      int n;
   13559 
   13560 	      ptr = src_irel + idx;
   13561 	      if (ptr == NULL)
   13562 		{
   13563 		  _bfd_error_handler
   13564 		    /* xgettext:c-format */
   13565 		    (_("%pB(%pA): error: reloc table entry %zu is empty"),
   13566 		     abfd, relsec, idx);
   13567 		  bfd_set_error (bfd_error_bad_value);
   13568 		  result = false;
   13569 		  break;
   13570 		}
   13571 
   13572 	      if (ptr->sym_ptr_ptr == NULL)
   13573 		{
   13574 		  /* FIXME: Is this an error ? */
   13575 		  n = 0;
   13576 		}
   13577 	      else
   13578 		{
   13579 		  sym = *ptr->sym_ptr_ptr;
   13580 
   13581 		  if (sym == last_sym)
   13582 		    n = last_sym_idx;
   13583 		  else
   13584 		    {
   13585 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   13586 		      if (n < 0)
   13587 			{
   13588 			  _bfd_error_handler
   13589 			    /* xgettext:c-format */
   13590 			    (_("%pB(%pA): error: secondary reloc %zu"
   13591 			       " references a missing symbol"),
   13592 			     abfd, relsec, idx);
   13593 			  bfd_set_error (bfd_error_bad_value);
   13594 			  result = false;
   13595 			  n = 0;
   13596 			}
   13597 
   13598 		      last_sym = sym;
   13599 		      last_sym_idx = n;
   13600 		    }
   13601 
   13602 		  if (sym->the_bfd != NULL
   13603 		      && sym->the_bfd->xvec != abfd->xvec
   13604 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   13605 		    {
   13606 		      _bfd_error_handler
   13607 			/* xgettext:c-format */
   13608 			(_("%pB(%pA): error: secondary reloc %zu"
   13609 			   " references a deleted symbol"),
   13610 			 abfd, relsec, idx);
   13611 		      bfd_set_error (bfd_error_bad_value);
   13612 		      result = false;
   13613 		      n = 0;
   13614 		    }
   13615 		}
   13616 
   13617 	      src_rela.r_offset = ptr->address + addr_offset;
   13618 	      if (ptr->howto == NULL)
   13619 		{
   13620 		  _bfd_error_handler
   13621 		    /* xgettext:c-format */
   13622 		    (_("%pB(%pA): error: secondary reloc %zu"
   13623 		       " is of an unknown type"),
   13624 		     abfd, relsec, idx);
   13625 		  bfd_set_error (bfd_error_bad_value);
   13626 		  result = false;
   13627 		  src_rela.r_info = r_info (0, 0);
   13628 		}
   13629 	      else
   13630 		src_rela.r_info = r_info (n, ptr->howto->type);
   13631 	      src_rela.r_addend = ptr->addend;
   13632 
   13633 	      if (entsize == ebd->s->sizeof_rel)
   13634 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   13635 	      else /* entsize == ebd->s->sizeof_rela */
   13636 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   13637 	    }
   13638 	}
   13639     }
   13640 
   13641   return result;
   13642 }
   13643