Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.1.1.10
      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 to work.  */
    434 	    shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
    435 	  /* Otherwise we do nothing.  The assumption is that
    436 	     the index table will not be needed.  */
    437 	}
    438     }
    439 
    440   /* Read the symbols.  */
    441   alloc_ext = NULL;
    442   alloc_extshndx = NULL;
    443   alloc_intsym = NULL;
    444   bed = get_elf_backend_data (ibfd);
    445   extsym_size = bed->s->sizeof_sym;
    446   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
    447     {
    448       bfd_set_error (bfd_error_file_too_big);
    449       intsym_buf = NULL;
    450       goto out;
    451     }
    452   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    453   if (extsym_buf == NULL)
    454     {
    455       alloc_ext = bfd_malloc (amt);
    456       extsym_buf = alloc_ext;
    457     }
    458   if (extsym_buf == NULL
    459       || bfd_seek (ibfd, pos, SEEK_SET) != 0
    460       || bfd_bread (extsym_buf, amt, ibfd) != amt)
    461     {
    462       intsym_buf = NULL;
    463       goto out;
    464     }
    465 
    466   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    467     extshndx_buf = NULL;
    468   else
    469     {
    470       if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
    471 	{
    472 	  bfd_set_error (bfd_error_file_too_big);
    473 	  intsym_buf = NULL;
    474 	  goto out;
    475 	}
    476       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    477       if (extshndx_buf == NULL)
    478 	{
    479 	  alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
    480 	  extshndx_buf = alloc_extshndx;
    481 	}
    482       if (extshndx_buf == NULL
    483 	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
    484 	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
    485 	{
    486 	  intsym_buf = NULL;
    487 	  goto out;
    488 	}
    489     }
    490 
    491   if (intsym_buf == NULL)
    492     {
    493       if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
    494 	{
    495 	  bfd_set_error (bfd_error_file_too_big);
    496 	  goto out;
    497 	}
    498       alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
    499       intsym_buf = alloc_intsym;
    500       if (intsym_buf == NULL)
    501 	goto out;
    502     }
    503 
    504   /* Convert the symbols to internal form.  */
    505   isymend = intsym_buf + symcount;
    506   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    507 	   shndx = extshndx_buf;
    508        isym < isymend;
    509        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    510     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    511       {
    512 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    513 	/* xgettext:c-format */
    514 	_bfd_error_handler (_("%pB symbol number %lu references"
    515 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    516 			    ibfd, (unsigned long) symoffset);
    517 	free (alloc_intsym);
    518 	intsym_buf = NULL;
    519 	goto out;
    520       }
    521 
    522  out:
    523   free (alloc_ext);
    524   free (alloc_extshndx);
    525 
    526   return intsym_buf;
    527 }
    528 
    529 /* Look up a symbol name.  */
    530 const char *
    531 bfd_elf_sym_name (bfd *abfd,
    532 		  Elf_Internal_Shdr *symtab_hdr,
    533 		  Elf_Internal_Sym *isym,
    534 		  asection *sym_sec)
    535 {
    536   const char *name;
    537   unsigned int iname = isym->st_name;
    538   unsigned int shindex = symtab_hdr->sh_link;
    539 
    540   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    541       /* Check for a bogus st_shndx to avoid crashing.  */
    542       && isym->st_shndx < elf_numsections (abfd))
    543     {
    544       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    545       shindex = elf_elfheader (abfd)->e_shstrndx;
    546     }
    547 
    548   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    549   if (name == NULL)
    550     name = "(null)";
    551   else if (sym_sec && *name == '\0')
    552     name = bfd_section_name (sym_sec);
    553 
    554   return name;
    555 }
    556 
    557 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    558    sections.  The first element is the flags, the rest are section
    559    pointers.  */
    560 
    561 typedef union elf_internal_group {
    562   Elf_Internal_Shdr *shdr;
    563   unsigned int flags;
    564 } Elf_Internal_Group;
    565 
    566 /* Return the name of the group signature symbol.  Why isn't the
    567    signature just a string?  */
    568 
    569 static const char *
    570 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    571 {
    572   Elf_Internal_Shdr *hdr;
    573   unsigned char esym[sizeof (Elf64_External_Sym)];
    574   Elf_External_Sym_Shndx eshndx;
    575   Elf_Internal_Sym isym;
    576 
    577   /* First we need to ensure the symbol table is available.  Make sure
    578      that it is a symbol table section.  */
    579   if (ghdr->sh_link >= elf_numsections (abfd))
    580     return NULL;
    581   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    582   if (hdr->sh_type != SHT_SYMTAB
    583       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    584     return NULL;
    585 
    586   /* Go read the symbol.  */
    587   hdr = &elf_tdata (abfd)->symtab_hdr;
    588   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    589 			    &isym, esym, &eshndx) == NULL)
    590     return NULL;
    591 
    592   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    593 }
    594 
    595 /* Set next_in_group list pointer, and group name for NEWSECT.  */
    596 
    597 static bool
    598 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    599 {
    600   unsigned int num_group = elf_tdata (abfd)->num_group;
    601 
    602   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    603      is set to -1 if there are no SHT_GROUP sections.  */
    604   if (num_group == 0)
    605     {
    606       unsigned int i, shnum;
    607 
    608       /* First count the number of groups.  If we have a SHT_GROUP
    609 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    610       shnum = elf_numsections (abfd);
    611       num_group = 0;
    612 
    613 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    614 	(   (shdr)->sh_type == SHT_GROUP		\
    615 	 && (shdr)->sh_size >= minsize			\
    616 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    617 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    618 
    619       for (i = 0; i < shnum; i++)
    620 	{
    621 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    622 
    623 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    624 	    num_group += 1;
    625 	}
    626 
    627       if (num_group == 0)
    628 	{
    629 	  num_group = (unsigned) -1;
    630 	  elf_tdata (abfd)->num_group = num_group;
    631 	  elf_tdata (abfd)->group_sect_ptr = NULL;
    632 	}
    633       else
    634 	{
    635 	  /* We keep a list of elf section headers for group sections,
    636 	     so we can find them quickly.  */
    637 	  size_t amt;
    638 
    639 	  elf_tdata (abfd)->num_group = num_group;
    640 	  amt = num_group * sizeof (Elf_Internal_Shdr *);
    641 	  elf_tdata (abfd)->group_sect_ptr
    642 	    = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
    643 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    644 	    return false;
    645 	  num_group = 0;
    646 
    647 	  for (i = 0; i < shnum; i++)
    648 	    {
    649 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    650 
    651 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    652 		{
    653 		  unsigned char *src;
    654 		  Elf_Internal_Group *dest;
    655 
    656 		  /* Make sure the group section has a BFD section
    657 		     attached to it.  */
    658 		  if (!bfd_section_from_shdr (abfd, i))
    659 		    return false;
    660 
    661 		  /* Add to list of sections.  */
    662 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    663 		  num_group += 1;
    664 
    665 		  /* Read the raw contents.  */
    666 		  BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
    667 		  shdr->contents = NULL;
    668 		  if (_bfd_mul_overflow (shdr->sh_size,
    669 					 sizeof (*dest) / 4, &amt)
    670 		      || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    671 		      || !(shdr->contents
    672 			   = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
    673 		    {
    674 		      _bfd_error_handler
    675 			/* xgettext:c-format */
    676 			(_("%pB: invalid size field in group section"
    677 			   " header: %#" PRIx64 ""),
    678 			 abfd, (uint64_t) shdr->sh_size);
    679 		      bfd_set_error (bfd_error_bad_value);
    680 		      -- num_group;
    681 		      continue;
    682 		    }
    683 
    684 		  /* Translate raw contents, a flag word followed by an
    685 		     array of elf section indices all in target byte order,
    686 		     to the flag word followed by an array of elf section
    687 		     pointers.  */
    688 		  src = shdr->contents + shdr->sh_size;
    689 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    690 
    691 		  while (1)
    692 		    {
    693 		      unsigned int idx;
    694 
    695 		      src -= 4;
    696 		      --dest;
    697 		      idx = H_GET_32 (abfd, src);
    698 		      if (src == shdr->contents)
    699 			{
    700 			  dest->shdr = NULL;
    701 			  dest->flags = idx;
    702 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    703 			    shdr->bfd_section->flags
    704 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    705 			  break;
    706 			}
    707 		      if (idx < shnum)
    708 			{
    709 			  dest->shdr = elf_elfsections (abfd)[idx];
    710 			  /* PR binutils/23199: All sections in a
    711 			     section group should be marked with
    712 			     SHF_GROUP.  But some tools generate
    713 			     broken objects without SHF_GROUP.  Fix
    714 			     them up here.  */
    715 			  dest->shdr->sh_flags |= SHF_GROUP;
    716 			}
    717 		      if (idx >= shnum
    718 			  || dest->shdr->sh_type == SHT_GROUP)
    719 			{
    720 			  _bfd_error_handler
    721 			    (_("%pB: invalid entry in SHT_GROUP section [%u]"),
    722 			       abfd, i);
    723 			  dest->shdr = NULL;
    724 			}
    725 		    }
    726 		}
    727 	    }
    728 
    729 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    730 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    731 	    {
    732 	      elf_tdata (abfd)->num_group = num_group;
    733 
    734 	      /* If all groups are invalid then fail.  */
    735 	      if (num_group == 0)
    736 		{
    737 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    738 		  elf_tdata (abfd)->num_group = num_group = -1;
    739 		  _bfd_error_handler
    740 		    (_("%pB: no valid group sections found"), abfd);
    741 		  bfd_set_error (bfd_error_bad_value);
    742 		}
    743 	    }
    744 	}
    745     }
    746 
    747   if (num_group != (unsigned) -1)
    748     {
    749       unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
    750       unsigned int j;
    751 
    752       for (j = 0; j < num_group; j++)
    753 	{
    754 	  /* Begin search from previous found group.  */
    755 	  unsigned i = (j + search_offset) % num_group;
    756 
    757 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    758 	  Elf_Internal_Group *idx;
    759 	  bfd_size_type n_elt;
    760 
    761 	  if (shdr == NULL)
    762 	    continue;
    763 
    764 	  idx = (Elf_Internal_Group *) shdr->contents;
    765 	  if (idx == NULL || shdr->sh_size < 4)
    766 	    {
    767 	      /* See PR 21957 for a reproducer.  */
    768 	      /* xgettext:c-format */
    769 	      _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
    770 				  abfd, shdr->bfd_section);
    771 	      elf_tdata (abfd)->group_sect_ptr[i] = NULL;
    772 	      bfd_set_error (bfd_error_bad_value);
    773 	      return false;
    774 	    }
    775 	  n_elt = shdr->sh_size / 4;
    776 
    777 	  /* Look through this group's sections to see if current
    778 	     section is a member.  */
    779 	  while (--n_elt != 0)
    780 	    if ((++idx)->shdr == hdr)
    781 	      {
    782 		asection *s = NULL;
    783 
    784 		/* We are a member of this group.  Go looking through
    785 		   other members to see if any others are linked via
    786 		   next_in_group.  */
    787 		idx = (Elf_Internal_Group *) shdr->contents;
    788 		n_elt = shdr->sh_size / 4;
    789 		while (--n_elt != 0)
    790 		  if ((++idx)->shdr != NULL
    791 		      && (s = idx->shdr->bfd_section) != NULL
    792 		      && elf_next_in_group (s) != NULL)
    793 		    break;
    794 		if (n_elt != 0)
    795 		  {
    796 		    /* Snarf the group name from other member, and
    797 		       insert current section in circular list.  */
    798 		    elf_group_name (newsect) = elf_group_name (s);
    799 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    800 		    elf_next_in_group (s) = newsect;
    801 		  }
    802 		else
    803 		  {
    804 		    const char *gname;
    805 
    806 		    gname = group_signature (abfd, shdr);
    807 		    if (gname == NULL)
    808 		      return false;
    809 		    elf_group_name (newsect) = gname;
    810 
    811 		    /* Start a circular list with one element.  */
    812 		    elf_next_in_group (newsect) = newsect;
    813 		  }
    814 
    815 		/* If the group section has been created, point to the
    816 		   new member.  */
    817 		if (shdr->bfd_section != NULL)
    818 		  elf_next_in_group (shdr->bfd_section) = newsect;
    819 
    820 		elf_tdata (abfd)->group_search_offset = i;
    821 		j = num_group - 1;
    822 		break;
    823 	      }
    824 	}
    825     }
    826 
    827   if (elf_group_name (newsect) == NULL)
    828     {
    829       /* xgettext:c-format */
    830       _bfd_error_handler (_("%pB: no group info for section '%pA'"),
    831 			  abfd, newsect);
    832       return false;
    833     }
    834   return true;
    835 }
    836 
    837 bool
    838 _bfd_elf_setup_sections (bfd *abfd)
    839 {
    840   unsigned int i;
    841   unsigned int num_group = elf_tdata (abfd)->num_group;
    842   bool result = true;
    843   asection *s;
    844 
    845   /* Process SHF_LINK_ORDER.  */
    846   for (s = abfd->sections; s != NULL; s = s->next)
    847     {
    848       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    849       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    850 	{
    851 	  unsigned int elfsec = this_hdr->sh_link;
    852 	  /* An sh_link value of 0 is now allowed.  It indicates that linked
    853 	     to section has already been discarded, but that the current
    854 	     section has been retained for some other reason.  This linking
    855 	     section is still a candidate for later garbage collection
    856 	     however.  */
    857 	  if (elfsec == 0)
    858 	    {
    859 	      elf_linked_to_section (s) = NULL;
    860 	    }
    861 	  else
    862 	    {
    863 	      asection *linksec = NULL;
    864 
    865 	      if (elfsec < elf_numsections (abfd))
    866 		{
    867 		  this_hdr = elf_elfsections (abfd)[elfsec];
    868 		  linksec = this_hdr->bfd_section;
    869 		}
    870 
    871 	      /* PR 1991, 2008:
    872 		 Some strip/objcopy may leave an incorrect value in
    873 		 sh_link.  We don't want to proceed.  */
    874 	      if (linksec == NULL)
    875 		{
    876 		  _bfd_error_handler
    877 		    /* xgettext:c-format */
    878 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    879 		     s->owner, elfsec, s);
    880 		  result = false;
    881 		}
    882 
    883 	      elf_linked_to_section (s) = linksec;
    884 	    }
    885 	}
    886       else if (this_hdr->sh_type == SHT_GROUP
    887 	       && elf_next_in_group (s) == NULL)
    888 	{
    889 	  _bfd_error_handler
    890 	    /* xgettext:c-format */
    891 	    (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
    892 	     abfd, elf_section_data (s)->this_idx);
    893 	  result = false;
    894 	}
    895     }
    896 
    897   /* Process section groups.  */
    898   if (num_group == (unsigned) -1)
    899     return result;
    900 
    901   for (i = 0; i < num_group; i++)
    902     {
    903       Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    904       Elf_Internal_Group *idx;
    905       unsigned int n_elt;
    906 
    907       /* PR binutils/18758: Beware of corrupt binaries with invalid group data.  */
    908       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    909 	{
    910 	  _bfd_error_handler
    911 	    /* xgettext:c-format */
    912 	    (_("%pB: section group entry number %u is corrupt"),
    913 	     abfd, i);
    914 	  result = false;
    915 	  continue;
    916 	}
    917 
    918       idx = (Elf_Internal_Group *) shdr->contents;
    919       n_elt = shdr->sh_size / 4;
    920 
    921       while (--n_elt != 0)
    922 	{
    923 	  ++ idx;
    924 
    925 	  if (idx->shdr == NULL)
    926 	    continue;
    927 	  else if (idx->shdr->bfd_section)
    928 	    elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    929 	  else if (idx->shdr->sh_type != SHT_RELA
    930 		   && idx->shdr->sh_type != SHT_REL)
    931 	    {
    932 	      /* There are some unknown sections in the group.  */
    933 	      _bfd_error_handler
    934 		/* xgettext:c-format */
    935 		(_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
    936 		 abfd,
    937 		 idx->shdr->sh_type,
    938 		 bfd_elf_string_from_elf_section (abfd,
    939 						  (elf_elfheader (abfd)
    940 						   ->e_shstrndx),
    941 						  idx->shdr->sh_name),
    942 		 shdr->bfd_section);
    943 	      result = false;
    944 	    }
    945 	}
    946     }
    947 
    948   return result;
    949 }
    950 
    951 bool
    952 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    953 {
    954   return elf_next_in_group (sec) != NULL;
    955 }
    956 
    957 const char *
    958 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    959 {
    960   if (elf_sec_group (sec) != NULL)
    961     return elf_group_name (sec);
    962   return NULL;
    963 }
    964 
    965 static char *
    966 convert_debug_to_zdebug (bfd *abfd, const char *name)
    967 {
    968   unsigned int len = strlen (name);
    969   char *new_name = bfd_alloc (abfd, len + 2);
    970   if (new_name == NULL)
    971     return NULL;
    972   new_name[0] = '.';
    973   new_name[1] = 'z';
    974   memcpy (new_name + 2, name + 1, len);
    975   return new_name;
    976 }
    977 
    978 static char *
    979 convert_zdebug_to_debug (bfd *abfd, const char *name)
    980 {
    981   unsigned int len = strlen (name);
    982   char *new_name = bfd_alloc (abfd, len);
    983   if (new_name == NULL)
    984     return NULL;
    985   new_name[0] = '.';
    986   memcpy (new_name + 1, name + 2, len - 1);
    987   return new_name;
    988 }
    989 
    990 /* This a copy of lto_section defined in GCC (lto-streamer.h).  */
    991 
    992 struct lto_section
    993 {
    994   int16_t major_version;
    995   int16_t minor_version;
    996   unsigned char slim_object;
    997 
    998   /* Flags is a private field that is not defined publicly.  */
    999   uint16_t flags;
   1000 };
   1001 
   1002 /* Make a BFD section from an ELF section.  We store a pointer to the
   1003    BFD section in the bfd_section field of the header.  */
   1004 
   1005 bool
   1006 _bfd_elf_make_section_from_shdr (bfd *abfd,
   1007 				 Elf_Internal_Shdr *hdr,
   1008 				 const char *name,
   1009 				 int shindex)
   1010 {
   1011   asection *newsect;
   1012   flagword flags;
   1013   const struct elf_backend_data *bed;
   1014   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   1015 
   1016   if (hdr->bfd_section != NULL)
   1017     return true;
   1018 
   1019   newsect = bfd_make_section_anyway (abfd, name);
   1020   if (newsect == NULL)
   1021     return false;
   1022 
   1023   hdr->bfd_section = newsect;
   1024   elf_section_data (newsect)->this_hdr = *hdr;
   1025   elf_section_data (newsect)->this_idx = shindex;
   1026 
   1027   /* Always use the real type/flags.  */
   1028   elf_section_type (newsect) = hdr->sh_type;
   1029   elf_section_flags (newsect) = hdr->sh_flags;
   1030 
   1031   newsect->filepos = hdr->sh_offset;
   1032 
   1033   flags = SEC_NO_FLAGS;
   1034   if (hdr->sh_type != SHT_NOBITS)
   1035     flags |= SEC_HAS_CONTENTS;
   1036   if (hdr->sh_type == SHT_GROUP)
   1037     flags |= SEC_GROUP;
   1038   if ((hdr->sh_flags & SHF_ALLOC) != 0)
   1039     {
   1040       flags |= SEC_ALLOC;
   1041       if (hdr->sh_type != SHT_NOBITS)
   1042 	flags |= SEC_LOAD;
   1043     }
   1044   if ((hdr->sh_flags & SHF_WRITE) == 0)
   1045     flags |= SEC_READONLY;
   1046   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
   1047     flags |= SEC_CODE;
   1048   else if ((flags & SEC_LOAD) != 0)
   1049     flags |= SEC_DATA;
   1050   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1051     {
   1052       flags |= SEC_MERGE;
   1053       newsect->entsize = hdr->sh_entsize;
   1054     }
   1055   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1056     flags |= SEC_STRINGS;
   1057   if (hdr->sh_flags & SHF_GROUP)
   1058     if (!setup_group (abfd, hdr, newsect))
   1059       return false;
   1060   if ((hdr->sh_flags & SHF_TLS) != 0)
   1061     flags |= SEC_THREAD_LOCAL;
   1062   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1063     flags |= SEC_EXCLUDE;
   1064 
   1065   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
   1066     {
   1067       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
   1068 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
   1069 	 byte.  */
   1070     case ELFOSABI_GNU:
   1071     case ELFOSABI_FREEBSD:
   1072       if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
   1073 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
   1074       /* Fall through */
   1075     case ELFOSABI_NONE:
   1076       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
   1077 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
   1078       break;
   1079     }
   1080 
   1081   if ((flags & SEC_ALLOC) == 0)
   1082     {
   1083       /* The debugging sections appear to be recognized only by name,
   1084 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1085       if (name [0] == '.')
   1086 	{
   1087 	  if (startswith (name, ".debug")
   1088 	      || startswith (name, ".gnu.debuglto_.debug_")
   1089 	      || startswith (name, ".gnu.linkonce.wi.")
   1090 	      || startswith (name, ".zdebug"))
   1091 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
   1092 	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
   1093 		   || startswith (name, ".note.gnu"))
   1094 	    {
   1095 	      flags |= SEC_ELF_OCTETS;
   1096 	      opb = 1;
   1097 	    }
   1098 	  else if (startswith (name, ".line")
   1099 		   || startswith (name, ".stab")
   1100 		   || strcmp (name, ".gdb_index") == 0)
   1101 	    flags |= SEC_DEBUGGING;
   1102 	}
   1103     }
   1104 
   1105   if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
   1106       || !bfd_set_section_size (newsect, hdr->sh_size)
   1107       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
   1108 							& -hdr->sh_addralign)))
   1109     return false;
   1110 
   1111   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1112      only link a single copy of the section.  This is used to support
   1113      g++.  g++ will emit each template expansion in its own section.
   1114      The symbols will be defined as weak, so that multiple definitions
   1115      are permitted.  The GNU linker extension is to actually discard
   1116      all but one of the sections.  */
   1117   if (startswith (name, ".gnu.linkonce")
   1118       && elf_next_in_group (newsect) == NULL)
   1119     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1120 
   1121   if (!bfd_set_section_flags (newsect, flags))
   1122     return false;
   1123 
   1124   bed = get_elf_backend_data (abfd);
   1125   if (bed->elf_backend_section_flags)
   1126     if (!bed->elf_backend_section_flags (hdr))
   1127       return false;
   1128 
   1129   /* We do not parse the PT_NOTE segments as we are interested even in the
   1130      separate debug info files which may have the segments offsets corrupted.
   1131      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1132   if (hdr->sh_type == SHT_NOTE)
   1133     {
   1134       bfd_byte *contents;
   1135 
   1136       if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
   1137 	return false;
   1138 
   1139       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1140 		       hdr->sh_offset, hdr->sh_addralign);
   1141       free (contents);
   1142     }
   1143 
   1144   if ((newsect->flags & SEC_ALLOC) != 0)
   1145     {
   1146       Elf_Internal_Phdr *phdr;
   1147       unsigned int i, nload;
   1148 
   1149       /* Some ELF linkers produce binaries with all the program header
   1150 	 p_paddr fields zero.  If we have such a binary with more than
   1151 	 one PT_LOAD header, then leave the section lma equal to vma
   1152 	 so that we don't create sections with overlapping lma.  */
   1153       phdr = elf_tdata (abfd)->phdr;
   1154       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1155 	if (phdr->p_paddr != 0)
   1156 	  break;
   1157 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1158 	  ++nload;
   1159       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1160 	return true;
   1161 
   1162       phdr = elf_tdata (abfd)->phdr;
   1163       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1164 	{
   1165 	  if (((phdr->p_type == PT_LOAD
   1166 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1167 	       || phdr->p_type == PT_TLS)
   1168 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1169 	    {
   1170 	      if ((newsect->flags & SEC_LOAD) == 0)
   1171 		newsect->lma = (phdr->p_paddr
   1172 				+ hdr->sh_addr - phdr->p_vaddr) / opb;
   1173 	      else
   1174 		/* We used to use the same adjustment for SEC_LOAD
   1175 		   sections, but that doesn't work if the segment
   1176 		   is packed with code from multiple VMAs.
   1177 		   Instead we calculate the section LMA based on
   1178 		   the segment LMA.  It is assumed that the
   1179 		   segment will contain sections with contiguous
   1180 		   LMAs, even if the VMAs are not.  */
   1181 		newsect->lma = (phdr->p_paddr
   1182 				+ hdr->sh_offset - phdr->p_offset) / opb;
   1183 
   1184 	      /* With contiguous segments, we can't tell from file
   1185 		 offsets whether a section with zero size should
   1186 		 be placed at the end of one segment or the
   1187 		 beginning of the next.  Decide based on vaddr.  */
   1188 	      if (hdr->sh_addr >= phdr->p_vaddr
   1189 		  && (hdr->sh_addr + hdr->sh_size
   1190 		      <= phdr->p_vaddr + phdr->p_memsz))
   1191 		break;
   1192 	    }
   1193 	}
   1194     }
   1195 
   1196   /* Compress/decompress DWARF debug sections with names: .debug_* and
   1197      .zdebug_*, after the section flags is set.  */
   1198   if ((newsect->flags & SEC_DEBUGGING)
   1199       && ((name[1] == 'd' && name[6] == '_')
   1200 	  || (name[1] == 'z' && name[7] == '_')))
   1201     {
   1202       enum { nothing, compress, decompress } action = nothing;
   1203       int compression_header_size;
   1204       bfd_size_type uncompressed_size;
   1205       unsigned int uncompressed_align_power;
   1206       bool compressed
   1207 	= bfd_is_section_compressed_with_header (abfd, newsect,
   1208 						 &compression_header_size,
   1209 						 &uncompressed_size,
   1210 						 &uncompressed_align_power);
   1211       if (compressed)
   1212 	{
   1213 	  /* Compressed section.  Check if we should decompress.  */
   1214 	  if ((abfd->flags & BFD_DECOMPRESS))
   1215 	    action = decompress;
   1216 	}
   1217 
   1218       /* Compress the uncompressed section or convert from/to .zdebug*
   1219 	 section.  Check if we should compress.  */
   1220       if (action == nothing)
   1221 	{
   1222 	  if (newsect->size != 0
   1223 	      && (abfd->flags & BFD_COMPRESS)
   1224 	      && compression_header_size >= 0
   1225 	      && uncompressed_size > 0
   1226 	      && (!compressed
   1227 		  || ((compression_header_size > 0)
   1228 		      != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
   1229 	    action = compress;
   1230 	  else
   1231 	    return true;
   1232 	}
   1233 
   1234       if (action == compress)
   1235 	{
   1236 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1237 	    {
   1238 	      _bfd_error_handler
   1239 		/* xgettext:c-format */
   1240 		(_("%pB: unable to initialize compress status for section %s"),
   1241 		 abfd, name);
   1242 	      return false;
   1243 	    }
   1244 	}
   1245       else
   1246 	{
   1247 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1248 	    {
   1249 	      _bfd_error_handler
   1250 		/* xgettext:c-format */
   1251 		(_("%pB: unable to initialize decompress status for section %s"),
   1252 		 abfd, name);
   1253 	      return false;
   1254 	    }
   1255 	}
   1256 
   1257       if (abfd->is_linker_input)
   1258 	{
   1259 	  if (name[1] == 'z'
   1260 	      && (action == decompress
   1261 		  || (action == compress
   1262 		      && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
   1263 	    {
   1264 	      /* Convert section name from .zdebug_* to .debug_* so
   1265 		 that linker will consider this section as a debug
   1266 		 section.  */
   1267 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   1268 	      if (new_name == NULL)
   1269 		return false;
   1270 	      bfd_rename_section (newsect, new_name);
   1271 	    }
   1272 	}
   1273       else
   1274 	/* For objdump, don't rename the section.  For objcopy, delay
   1275 	   section rename to elf_fake_sections.  */
   1276 	newsect->flags |= SEC_ELF_RENAME;
   1277     }
   1278 
   1279   /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
   1280      section.  */
   1281   if (startswith (name, ".gnu.lto_.lto."))
   1282     {
   1283       struct lto_section lsection;
   1284       if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
   1285 				    sizeof (struct lto_section)))
   1286 	abfd->lto_slim_object = lsection.slim_object;
   1287     }
   1288 
   1289   return true;
   1290 }
   1291 
   1292 const char *const bfd_elf_section_type_names[] =
   1293 {
   1294   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1295   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1296   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1297 };
   1298 
   1299 /* ELF relocs are against symbols.  If we are producing relocatable
   1300    output, and the reloc is against an external symbol, and nothing
   1301    has given us any additional addend, the resulting reloc will also
   1302    be against the same symbol.  In such a case, we don't want to
   1303    change anything about the way the reloc is handled, since it will
   1304    all be done at final link time.  Rather than put special case code
   1305    into bfd_perform_relocation, all the reloc types use this howto
   1306    function, or should call this function for relocatable output.  */
   1307 
   1308 bfd_reloc_status_type
   1309 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1310 		       arelent *reloc_entry,
   1311 		       asymbol *symbol,
   1312 		       void *data ATTRIBUTE_UNUSED,
   1313 		       asection *input_section,
   1314 		       bfd *output_bfd,
   1315 		       char **error_message ATTRIBUTE_UNUSED)
   1316 {
   1317   if (output_bfd != NULL
   1318       && (symbol->flags & BSF_SECTION_SYM) == 0
   1319       && (! reloc_entry->howto->partial_inplace
   1320 	  || reloc_entry->addend == 0))
   1321     {
   1322       reloc_entry->address += input_section->output_offset;
   1323       return bfd_reloc_ok;
   1324     }
   1325 
   1326   /* In some cases the relocation should be treated as output section
   1327      relative, as when linking ELF DWARF into PE COFF.  Many ELF
   1328      targets lack section relative relocations and instead use
   1329      ordinary absolute relocations for references between DWARF
   1330      sections.  That is arguably a bug in those targets but it happens
   1331      to work for the usual case of linking to non-loaded ELF debug
   1332      sections with VMAs forced to zero.  PE COFF on the other hand
   1333      doesn't allow a section VMA of zero.  */
   1334   if (output_bfd == NULL
   1335       && !reloc_entry->howto->pc_relative
   1336       && (symbol->section->flags & SEC_DEBUGGING) != 0
   1337       && (input_section->flags & SEC_DEBUGGING) != 0)
   1338     reloc_entry->addend -= symbol->section->output_section->vma;
   1339 
   1340   return bfd_reloc_continue;
   1341 }
   1342 
   1343 /* Returns TRUE if section A matches section B.
   1345    Names, addresses and links may be different, but everything else
   1346    should be the same.  */
   1347 
   1348 static bool
   1349 section_match (const Elf_Internal_Shdr * a,
   1350 	       const Elf_Internal_Shdr * b)
   1351 {
   1352   if (a->sh_type != b->sh_type
   1353       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1354       || a->sh_addralign != b->sh_addralign
   1355       || a->sh_entsize != b->sh_entsize)
   1356     return false;
   1357   if (a->sh_type == SHT_SYMTAB
   1358       || a->sh_type == SHT_STRTAB)
   1359     return true;
   1360   return a->sh_size == b->sh_size;
   1361 }
   1362 
   1363 /* Find a section in OBFD that has the same characteristics
   1364    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1365    none can be found.  Check's section HINT first, as this is likely
   1366    to be the correct section.  */
   1367 
   1368 static unsigned int
   1369 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1370 	   const unsigned int hint)
   1371 {
   1372   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1373   unsigned int i;
   1374 
   1375   BFD_ASSERT (iheader != NULL);
   1376 
   1377   /* See PR 20922 for a reproducer of the NULL test.  */
   1378   if (hint < elf_numsections (obfd)
   1379       && oheaders[hint] != NULL
   1380       && section_match (oheaders[hint], iheader))
   1381     return hint;
   1382 
   1383   for (i = 1; i < elf_numsections (obfd); i++)
   1384     {
   1385       Elf_Internal_Shdr * oheader = oheaders[i];
   1386 
   1387       if (oheader == NULL)
   1388 	continue;
   1389       if (section_match (oheader, iheader))
   1390 	/* FIXME: Do we care if there is a potential for
   1391 	   multiple matches ?  */
   1392 	return i;
   1393     }
   1394 
   1395   return SHN_UNDEF;
   1396 }
   1397 
   1398 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1399    Processor specific section, based upon a matching input section.
   1400    Returns TRUE upon success, FALSE otherwise.  */
   1401 
   1402 static bool
   1403 copy_special_section_fields (const bfd *ibfd,
   1404 			     bfd *obfd,
   1405 			     const Elf_Internal_Shdr *iheader,
   1406 			     Elf_Internal_Shdr *oheader,
   1407 			     const unsigned int secnum)
   1408 {
   1409   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1410   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1411   bool changed = false;
   1412   unsigned int sh_link;
   1413 
   1414   if (oheader->sh_type == SHT_NOBITS)
   1415     {
   1416       /* This is a feature for objcopy --only-keep-debug:
   1417 	 When a section's type is changed to NOBITS, we preserve
   1418 	 the sh_link and sh_info fields so that they can be
   1419 	 matched up with the original.
   1420 
   1421 	 Note: Strictly speaking these assignments are wrong.
   1422 	 The sh_link and sh_info fields should point to the
   1423 	 relevent sections in the output BFD, which may not be in
   1424 	 the same location as they were in the input BFD.  But
   1425 	 the whole point of this action is to preserve the
   1426 	 original values of the sh_link and sh_info fields, so
   1427 	 that they can be matched up with the section headers in
   1428 	 the original file.  So strictly speaking we may be
   1429 	 creating an invalid ELF file, but it is only for a file
   1430 	 that just contains debug info and only for sections
   1431 	 without any contents.  */
   1432       if (oheader->sh_link == 0)
   1433 	oheader->sh_link = iheader->sh_link;
   1434       if (oheader->sh_info == 0)
   1435 	oheader->sh_info = iheader->sh_info;
   1436       return true;
   1437     }
   1438 
   1439   /* Allow the target a chance to decide how these fields should be set.  */
   1440   if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1441 						    iheader, oheader))
   1442     return true;
   1443 
   1444   /* We have an iheader which might match oheader, and which has non-zero
   1445      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1446      the section in the output bfd which corresponds to the linked section
   1447      in the input bfd.  */
   1448   if (iheader->sh_link != SHN_UNDEF)
   1449     {
   1450       /* See PR 20931 for a reproducer.  */
   1451       if (iheader->sh_link >= elf_numsections (ibfd))
   1452 	{
   1453 	  _bfd_error_handler
   1454 	    /* xgettext:c-format */
   1455 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1456 	     ibfd, iheader->sh_link, secnum);
   1457 	  return false;
   1458 	}
   1459 
   1460       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1461       if (sh_link != SHN_UNDEF)
   1462 	{
   1463 	  oheader->sh_link = sh_link;
   1464 	  changed = true;
   1465 	}
   1466       else
   1467 	/* FIXME: Should we install iheader->sh_link
   1468 	   if we could not find a match ?  */
   1469 	_bfd_error_handler
   1470 	  /* xgettext:c-format */
   1471 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1472     }
   1473 
   1474   if (iheader->sh_info)
   1475     {
   1476       /* The sh_info field can hold arbitrary information, but if the
   1477 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1478 	 section index.  */
   1479       if (iheader->sh_flags & SHF_INFO_LINK)
   1480 	{
   1481 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1482 			       iheader->sh_info);
   1483 	  if (sh_link != SHN_UNDEF)
   1484 	    oheader->sh_flags |= SHF_INFO_LINK;
   1485 	}
   1486       else
   1487 	/* No idea what it means - just copy it.  */
   1488 	sh_link = iheader->sh_info;
   1489 
   1490       if (sh_link != SHN_UNDEF)
   1491 	{
   1492 	  oheader->sh_info = sh_link;
   1493 	  changed = true;
   1494 	}
   1495       else
   1496 	_bfd_error_handler
   1497 	  /* xgettext:c-format */
   1498 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1499     }
   1500 
   1501   return changed;
   1502 }
   1503 
   1504 /* Copy the program header and other data from one object module to
   1505    another.  */
   1506 
   1507 bool
   1508 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1509 {
   1510   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1511   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1512   const struct elf_backend_data *bed;
   1513   unsigned int i;
   1514 
   1515   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1516     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1517     return true;
   1518 
   1519   if (!elf_flags_init (obfd))
   1520     {
   1521       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1522       elf_flags_init (obfd) = true;
   1523     }
   1524 
   1525   elf_gp (obfd) = elf_gp (ibfd);
   1526 
   1527   /* Also copy the EI_OSABI field.  */
   1528   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1529     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1530 
   1531   /* If set, copy the EI_ABIVERSION field.  */
   1532   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1533     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1534       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1535 
   1536   /* Copy object attributes.  */
   1537   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1538 
   1539   if (iheaders == NULL || oheaders == NULL)
   1540     return true;
   1541 
   1542   bed = get_elf_backend_data (obfd);
   1543 
   1544   /* Possibly copy other fields in the section header.  */
   1545   for (i = 1; i < elf_numsections (obfd); i++)
   1546     {
   1547       unsigned int j;
   1548       Elf_Internal_Shdr * oheader = oheaders[i];
   1549 
   1550       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1551 	 because of a special case need for generating separate debug info
   1552 	 files.  See below for more details.  */
   1553       if (oheader == NULL
   1554 	  || (oheader->sh_type != SHT_NOBITS
   1555 	      && oheader->sh_type < SHT_LOOS))
   1556 	continue;
   1557 
   1558       /* Ignore empty sections, and sections whose
   1559 	 fields have already been initialised.  */
   1560       if (oheader->sh_size == 0
   1561 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1562 	continue;
   1563 
   1564       /* Scan for the matching section in the input bfd.
   1565 	 First we try for a direct mapping between the input and output sections.  */
   1566       for (j = 1; j < elf_numsections (ibfd); j++)
   1567 	{
   1568 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1569 
   1570 	  if (iheader == NULL)
   1571 	    continue;
   1572 
   1573 	  if (oheader->bfd_section != NULL
   1574 	      && iheader->bfd_section != NULL
   1575 	      && iheader->bfd_section->output_section != NULL
   1576 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1577 	    {
   1578 	      /* We have found a connection from the input section to the
   1579 		 output section.  Attempt to copy the header fields.  If
   1580 		 this fails then do not try any further sections - there
   1581 		 should only be a one-to-one mapping between input and output. */
   1582 	      if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1583 		j = elf_numsections (ibfd);
   1584 	      break;
   1585 	    }
   1586 	}
   1587 
   1588       if (j < elf_numsections (ibfd))
   1589 	continue;
   1590 
   1591       /* That failed.  So try to deduce the corresponding input section.
   1592 	 Unfortunately we cannot compare names as the output string table
   1593 	 is empty, so instead we check size, address and type.  */
   1594       for (j = 1; j < elf_numsections (ibfd); j++)
   1595 	{
   1596 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1597 
   1598 	  if (iheader == NULL)
   1599 	    continue;
   1600 
   1601 	  /* Try matching fields in the input section's header.
   1602 	     Since --only-keep-debug turns all non-debug sections into
   1603 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1604 	     input type.  */
   1605 	  if ((oheader->sh_type == SHT_NOBITS
   1606 	       || iheader->sh_type == oheader->sh_type)
   1607 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1608 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1609 	      && iheader->sh_addralign == oheader->sh_addralign
   1610 	      && iheader->sh_entsize == oheader->sh_entsize
   1611 	      && iheader->sh_size == oheader->sh_size
   1612 	      && iheader->sh_addr == oheader->sh_addr
   1613 	      && (iheader->sh_info != oheader->sh_info
   1614 		  || iheader->sh_link != oheader->sh_link))
   1615 	    {
   1616 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1617 		break;
   1618 	    }
   1619 	}
   1620 
   1621       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1622 	{
   1623 	  /* Final attempt.  Call the backend copy function
   1624 	     with a NULL input section.  */
   1625 	  (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1626 							       NULL, oheader);
   1627 	}
   1628     }
   1629 
   1630   return true;
   1631 }
   1632 
   1633 static const char *
   1634 get_segment_type (unsigned int p_type)
   1635 {
   1636   const char *pt;
   1637   switch (p_type)
   1638     {
   1639     case PT_NULL: pt = "NULL"; break;
   1640     case PT_LOAD: pt = "LOAD"; break;
   1641     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1642     case PT_INTERP: pt = "INTERP"; break;
   1643     case PT_NOTE: pt = "NOTE"; break;
   1644     case PT_SHLIB: pt = "SHLIB"; break;
   1645     case PT_PHDR: pt = "PHDR"; break;
   1646     case PT_TLS: pt = "TLS"; break;
   1647     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1648     case PT_GNU_STACK: pt = "STACK"; break;
   1649     case PT_GNU_RELRO: pt = "RELRO"; break;
   1650     default: pt = NULL; break;
   1651     }
   1652   return pt;
   1653 }
   1654 
   1655 /* Print out the program headers.  */
   1656 
   1657 bool
   1658 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1659 {
   1660   FILE *f = (FILE *) farg;
   1661   Elf_Internal_Phdr *p;
   1662   asection *s;
   1663   bfd_byte *dynbuf = NULL;
   1664 
   1665   p = elf_tdata (abfd)->phdr;
   1666   if (p != NULL)
   1667     {
   1668       unsigned int i, c;
   1669 
   1670       fprintf (f, _("\nProgram Header:\n"));
   1671       c = elf_elfheader (abfd)->e_phnum;
   1672       for (i = 0; i < c; i++, p++)
   1673 	{
   1674 	  const char *pt = get_segment_type (p->p_type);
   1675 	  char buf[20];
   1676 
   1677 	  if (pt == NULL)
   1678 	    {
   1679 	      sprintf (buf, "0x%lx", p->p_type);
   1680 	      pt = buf;
   1681 	    }
   1682 	  fprintf (f, "%8s off    0x", pt);
   1683 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1684 	  fprintf (f, " vaddr 0x");
   1685 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1686 	  fprintf (f, " paddr 0x");
   1687 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1688 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1689 	  fprintf (f, "         filesz 0x");
   1690 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1691 	  fprintf (f, " memsz 0x");
   1692 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1693 	  fprintf (f, " flags %c%c%c",
   1694 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1695 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1696 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1697 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1698 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1699 	  fprintf (f, "\n");
   1700 	}
   1701     }
   1702 
   1703   s = bfd_get_section_by_name (abfd, ".dynamic");
   1704   if (s != NULL)
   1705     {
   1706       unsigned int elfsec;
   1707       unsigned long shlink;
   1708       bfd_byte *extdyn, *extdynend;
   1709       size_t extdynsize;
   1710       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1711 
   1712       fprintf (f, _("\nDynamic Section:\n"));
   1713 
   1714       if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   1715 	goto error_return;
   1716 
   1717       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1718       if (elfsec == SHN_BAD)
   1719 	goto error_return;
   1720       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1721 
   1722       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1723       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1724 
   1725       extdyn = dynbuf;
   1726       /* PR 17512: file: 6f427532.  */
   1727       if (s->size < extdynsize)
   1728 	goto error_return;
   1729       extdynend = extdyn + s->size;
   1730       /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
   1731 	 Fix range check.  */
   1732       for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   1733 	{
   1734 	  Elf_Internal_Dyn dyn;
   1735 	  const char *name = "";
   1736 	  char ab[20];
   1737 	  bool stringp;
   1738 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1739 
   1740 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1741 
   1742 	  if (dyn.d_tag == DT_NULL)
   1743 	    break;
   1744 
   1745 	  stringp = false;
   1746 	  switch (dyn.d_tag)
   1747 	    {
   1748 	    default:
   1749 	      if (bed->elf_backend_get_target_dtag)
   1750 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1751 
   1752 	      if (!strcmp (name, ""))
   1753 		{
   1754 		  sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
   1755 		  name = ab;
   1756 		}
   1757 	      break;
   1758 
   1759 	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
   1760 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1761 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1762 	    case DT_HASH: name = "HASH"; break;
   1763 	    case DT_STRTAB: name = "STRTAB"; break;
   1764 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1765 	    case DT_RELA: name = "RELA"; break;
   1766 	    case DT_RELASZ: name = "RELASZ"; break;
   1767 	    case DT_RELAENT: name = "RELAENT"; break;
   1768 	    case DT_STRSZ: name = "STRSZ"; break;
   1769 	    case DT_SYMENT: name = "SYMENT"; break;
   1770 	    case DT_INIT: name = "INIT"; break;
   1771 	    case DT_FINI: name = "FINI"; break;
   1772 	    case DT_SONAME: name = "SONAME"; stringp = true; break;
   1773 	    case DT_RPATH: name = "RPATH"; stringp = true; break;
   1774 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1775 	    case DT_REL: name = "REL"; break;
   1776 	    case DT_RELSZ: name = "RELSZ"; break;
   1777 	    case DT_RELENT: name = "RELENT"; break;
   1778 	    case DT_RELR: name = "RELR"; break;
   1779 	    case DT_RELRSZ: name = "RELRSZ"; break;
   1780 	    case DT_RELRENT: name = "RELRENT"; break;
   1781 	    case DT_PLTREL: name = "PLTREL"; break;
   1782 	    case DT_DEBUG: name = "DEBUG"; break;
   1783 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1784 	    case DT_JMPREL: name = "JMPREL"; break;
   1785 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1786 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1787 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1788 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1789 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1790 	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
   1791 	    case DT_FLAGS: name = "FLAGS"; break;
   1792 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1793 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1794 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1795 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1796 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1797 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1798 	    case DT_FEATURE: name = "FEATURE"; break;
   1799 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1800 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1801 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1802 	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
   1803 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
   1804 	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
   1805 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1806 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1807 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1808 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1809 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1810 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1811 	    case DT_VERSYM: name = "VERSYM"; break;
   1812 	    case DT_VERDEF: name = "VERDEF"; break;
   1813 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1814 	    case DT_VERNEED: name = "VERNEED"; break;
   1815 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1816 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
   1817 	    case DT_USED: name = "USED"; break;
   1818 	    case DT_FILTER: name = "FILTER"; stringp = true; break;
   1819 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1820 	    }
   1821 
   1822 	  fprintf (f, "  %-20s ", name);
   1823 	  if (! stringp)
   1824 	    {
   1825 	      fprintf (f, "0x");
   1826 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1827 	    }
   1828 	  else
   1829 	    {
   1830 	      const char *string;
   1831 	      unsigned int tagv = dyn.d_un.d_val;
   1832 
   1833 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1834 	      if (string == NULL)
   1835 		goto error_return;
   1836 	      fprintf (f, "%s", string);
   1837 	    }
   1838 	  fprintf (f, "\n");
   1839 	}
   1840 
   1841       free (dynbuf);
   1842       dynbuf = NULL;
   1843     }
   1844 
   1845   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1846       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1847     {
   1848       if (! _bfd_elf_slurp_version_tables (abfd, false))
   1849 	return false;
   1850     }
   1851 
   1852   if (elf_dynverdef (abfd) != 0)
   1853     {
   1854       Elf_Internal_Verdef *t;
   1855 
   1856       fprintf (f, _("\nVersion definitions:\n"));
   1857       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1858 	{
   1859 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1860 		   t->vd_flags, t->vd_hash,
   1861 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1862 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1863 	    {
   1864 	      Elf_Internal_Verdaux *a;
   1865 
   1866 	      fprintf (f, "\t");
   1867 	      for (a = t->vd_auxptr->vda_nextptr;
   1868 		   a != NULL;
   1869 		   a = a->vda_nextptr)
   1870 		fprintf (f, "%s ",
   1871 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1872 	      fprintf (f, "\n");
   1873 	    }
   1874 	}
   1875     }
   1876 
   1877   if (elf_dynverref (abfd) != 0)
   1878     {
   1879       Elf_Internal_Verneed *t;
   1880 
   1881       fprintf (f, _("\nVersion References:\n"));
   1882       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1883 	{
   1884 	  Elf_Internal_Vernaux *a;
   1885 
   1886 	  fprintf (f, _("  required from %s:\n"),
   1887 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1888 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1889 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1890 		     a->vna_flags, a->vna_other,
   1891 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1892 	}
   1893     }
   1894 
   1895   return true;
   1896 
   1897  error_return:
   1898   free (dynbuf);
   1899   return false;
   1900 }
   1901 
   1902 /* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
   1903    and return symbol version for symbol version itself.   */
   1904 
   1905 const char *
   1906 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   1907 				    bool base_p,
   1908 				    bool *hidden)
   1909 {
   1910   const char *version_string = NULL;
   1911   if (elf_dynversym (abfd) != 0
   1912       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   1913     {
   1914       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   1915 
   1916       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   1917       vernum &= VERSYM_VERSION;
   1918 
   1919       if (vernum == 0)
   1920 	version_string = "";
   1921       else if (vernum == 1
   1922 	       && (vernum > elf_tdata (abfd)->cverdefs
   1923 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   1924 		       == VER_FLG_BASE)))
   1925 	version_string = base_p ? "Base" : "";
   1926       else if (vernum <= elf_tdata (abfd)->cverdefs)
   1927 	{
   1928 	  const char *nodename
   1929 	    = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   1930 	  version_string = "";
   1931 	  if (base_p
   1932 	      || nodename == NULL
   1933 	      || symbol->name == NULL
   1934 	      || strcmp (symbol->name, nodename) != 0)
   1935 	    version_string = nodename;
   1936 	}
   1937       else
   1938 	{
   1939 	  Elf_Internal_Verneed *t;
   1940 
   1941 	  version_string = _("<corrupt>");
   1942 	  for (t = elf_tdata (abfd)->verref;
   1943 	       t != NULL;
   1944 	       t = t->vn_nextref)
   1945 	    {
   1946 	      Elf_Internal_Vernaux *a;
   1947 
   1948 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1949 		{
   1950 		  if (a->vna_other == vernum)
   1951 		    {
   1952 		      *hidden = true;
   1953 		      version_string = a->vna_nodename;
   1954 		      break;
   1955 		    }
   1956 		}
   1957 	    }
   1958 	}
   1959     }
   1960   return version_string;
   1961 }
   1962 
   1963 /* Display ELF-specific fields of a symbol.  */
   1964 
   1965 void
   1966 bfd_elf_print_symbol (bfd *abfd,
   1967 		      void *filep,
   1968 		      asymbol *symbol,
   1969 		      bfd_print_symbol_type how)
   1970 {
   1971   FILE *file = (FILE *) filep;
   1972   switch (how)
   1973     {
   1974     case bfd_print_symbol_name:
   1975       fprintf (file, "%s", symbol->name);
   1976       break;
   1977     case bfd_print_symbol_more:
   1978       fprintf (file, "elf ");
   1979       bfd_fprintf_vma (abfd, file, symbol->value);
   1980       fprintf (file, " %x", symbol->flags);
   1981       break;
   1982     case bfd_print_symbol_all:
   1983       {
   1984 	const char *section_name;
   1985 	const char *name = NULL;
   1986 	const struct elf_backend_data *bed;
   1987 	unsigned char st_other;
   1988 	bfd_vma val;
   1989 	const char *version_string;
   1990 	bool hidden;
   1991 
   1992 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   1993 
   1994 	bed = get_elf_backend_data (abfd);
   1995 	if (bed->elf_backend_print_symbol_all)
   1996 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   1997 
   1998 	if (name == NULL)
   1999 	  {
   2000 	    name = symbol->name;
   2001 	    bfd_print_symbol_vandf (abfd, file, symbol);
   2002 	  }
   2003 
   2004 	fprintf (file, " %s\t", section_name);
   2005 	/* Print the "other" value for a symbol.  For common symbols,
   2006 	   we've already printed the size; now print the alignment.
   2007 	   For other symbols, we have no specified alignment, and
   2008 	   we've printed the address; now print the size.  */
   2009 	if (symbol->section && bfd_is_com_section (symbol->section))
   2010 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   2011 	else
   2012 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   2013 	bfd_fprintf_vma (abfd, file, val);
   2014 
   2015 	/* If we have version information, print it.  */
   2016 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2017 							     symbol,
   2018 							     true,
   2019 							     &hidden);
   2020 	if (version_string)
   2021 	  {
   2022 	    if (!hidden)
   2023 	      fprintf (file, "  %-11s", version_string);
   2024 	    else
   2025 	      {
   2026 		int i;
   2027 
   2028 		fprintf (file, " (%s)", version_string);
   2029 		for (i = 10 - strlen (version_string); i > 0; --i)
   2030 		  putc (' ', file);
   2031 	      }
   2032 	  }
   2033 
   2034 	/* If the st_other field is not zero, print it.  */
   2035 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2036 
   2037 	switch (st_other)
   2038 	  {
   2039 	  case 0: break;
   2040 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2041 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2042 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2043 	  default:
   2044 	    /* Some other non-defined flags are also present, so print
   2045 	       everything hex.  */
   2046 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2047 	  }
   2048 
   2049 	fprintf (file, " %s", name);
   2050       }
   2051       break;
   2052     }
   2053 }
   2054 
   2055 /* ELF .o/exec file reading */
   2057 
   2058 /* Create a new bfd section from an ELF section header.  */
   2059 
   2060 bool
   2061 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2062 {
   2063   Elf_Internal_Shdr *hdr;
   2064   Elf_Internal_Ehdr *ehdr;
   2065   const struct elf_backend_data *bed;
   2066   const char *name;
   2067   bool ret = true;
   2068 
   2069   if (shindex >= elf_numsections (abfd))
   2070     return false;
   2071 
   2072   /* PR17512: A corrupt ELF binary might contain a loop of sections via
   2073      sh_link or sh_info.  Detect this here, by refusing to load a
   2074      section that we are already in the process of loading.  */
   2075   if (elf_tdata (abfd)->being_created[shindex])
   2076     {
   2077       _bfd_error_handler
   2078 	(_("%pB: warning: loop in section dependencies detected"), abfd);
   2079       return false;
   2080     }
   2081   elf_tdata (abfd)->being_created[shindex] = true;
   2082 
   2083   hdr = elf_elfsections (abfd)[shindex];
   2084   ehdr = elf_elfheader (abfd);
   2085   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2086 					  hdr->sh_name);
   2087   if (name == NULL)
   2088     goto fail;
   2089 
   2090   bed = get_elf_backend_data (abfd);
   2091   switch (hdr->sh_type)
   2092     {
   2093     case SHT_NULL:
   2094       /* Inactive section. Throw it away.  */
   2095       goto success;
   2096 
   2097     case SHT_PROGBITS:		/* Normal section with contents.  */
   2098     case SHT_NOBITS:		/* .bss section.  */
   2099     case SHT_HASH:		/* .hash section.  */
   2100     case SHT_NOTE:		/* .note section.  */
   2101     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2102     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2103     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2104     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2105     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2106       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2107       goto success;
   2108 
   2109     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2110       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2111 	goto fail;
   2112 
   2113       if (hdr->sh_link > elf_numsections (abfd))
   2114 	{
   2115 	  /* PR 10478: Accept Solaris binaries with a sh_link
   2116 	     field set to SHN_BEFORE or SHN_AFTER.  */
   2117 	  switch (bfd_get_arch (abfd))
   2118 	    {
   2119 	    case bfd_arch_i386:
   2120 	    case bfd_arch_sparc:
   2121 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
   2122 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
   2123 		break;
   2124 	      /* Otherwise fall through.  */
   2125 	    default:
   2126 	      goto fail;
   2127 	    }
   2128 	}
   2129       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2130 	goto fail;
   2131       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2132 	{
   2133 	  Elf_Internal_Shdr *dynsymhdr;
   2134 
   2135 	  /* The shared libraries distributed with hpux11 have a bogus
   2136 	     sh_link field for the ".dynamic" section.  Find the
   2137 	     string table for the ".dynsym" section instead.  */
   2138 	  if (elf_dynsymtab (abfd) != 0)
   2139 	    {
   2140 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2141 	      hdr->sh_link = dynsymhdr->sh_link;
   2142 	    }
   2143 	  else
   2144 	    {
   2145 	      unsigned int i, num_sec;
   2146 
   2147 	      num_sec = elf_numsections (abfd);
   2148 	      for (i = 1; i < num_sec; i++)
   2149 		{
   2150 		  dynsymhdr = elf_elfsections (abfd)[i];
   2151 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2152 		    {
   2153 		      hdr->sh_link = dynsymhdr->sh_link;
   2154 		      break;
   2155 		    }
   2156 		}
   2157 	    }
   2158 	}
   2159       goto success;
   2160 
   2161     case SHT_SYMTAB:		/* A symbol table.  */
   2162       if (elf_onesymtab (abfd) == shindex)
   2163 	goto success;
   2164 
   2165       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2166 	goto fail;
   2167 
   2168       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2169 	{
   2170 	  if (hdr->sh_size != 0)
   2171 	    goto fail;
   2172 	  /* Some assemblers erroneously set sh_info to one with a
   2173 	     zero sh_size.  ld sees this as a global symbol count
   2174 	     of (unsigned) -1.  Fix it here.  */
   2175 	  hdr->sh_info = 0;
   2176 	  goto success;
   2177 	}
   2178 
   2179       /* PR 18854: A binary might contain more than one symbol table.
   2180 	 Unusual, but possible.  Warn, but continue.  */
   2181       if (elf_onesymtab (abfd) != 0)
   2182 	{
   2183 	  _bfd_error_handler
   2184 	    /* xgettext:c-format */
   2185 	    (_("%pB: warning: multiple symbol tables detected"
   2186 	       " - ignoring the table in section %u"),
   2187 	     abfd, shindex);
   2188 	  goto success;
   2189 	}
   2190       elf_onesymtab (abfd) = shindex;
   2191       elf_symtab_hdr (abfd) = *hdr;
   2192       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2193       abfd->flags |= HAS_SYMS;
   2194 
   2195       /* Sometimes a shared object will map in the symbol table.  If
   2196 	 SHF_ALLOC is set, and this is a shared object, then we also
   2197 	 treat this section as a BFD section.  We can not base the
   2198 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2199 	 set in a relocatable object file, which would confuse the
   2200 	 linker.  */
   2201       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2202 	  && (abfd->flags & DYNAMIC) != 0
   2203 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2204 						shindex))
   2205 	goto fail;
   2206 
   2207       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2208 	 can't read symbols without that section loaded as well.  It
   2209 	 is most likely specified by the next section header.  */
   2210       {
   2211 	elf_section_list * entry;
   2212 	unsigned int i, num_sec;
   2213 
   2214 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2215 	  if (entry->hdr.sh_link == shindex)
   2216 	    goto success;
   2217 
   2218 	num_sec = elf_numsections (abfd);
   2219 	for (i = shindex + 1; i < num_sec; i++)
   2220 	  {
   2221 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2222 
   2223 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2224 		&& hdr2->sh_link == shindex)
   2225 	      break;
   2226 	  }
   2227 
   2228 	if (i == num_sec)
   2229 	  for (i = 1; i < shindex; i++)
   2230 	    {
   2231 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2232 
   2233 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2234 		  && hdr2->sh_link == shindex)
   2235 		break;
   2236 	    }
   2237 
   2238 	if (i != shindex)
   2239 	  ret = bfd_section_from_shdr (abfd, i);
   2240 	/* else FIXME: we have failed to find the symbol table - should we issue an error ? */
   2241 	goto success;
   2242       }
   2243 
   2244     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2245       if (elf_dynsymtab (abfd) == shindex)
   2246 	goto success;
   2247 
   2248       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2249 	goto fail;
   2250 
   2251       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2252 	{
   2253 	  if (hdr->sh_size != 0)
   2254 	    goto fail;
   2255 
   2256 	  /* Some linkers erroneously set sh_info to one with a
   2257 	     zero sh_size.  ld sees this as a global symbol count
   2258 	     of (unsigned) -1.  Fix it here.  */
   2259 	  hdr->sh_info = 0;
   2260 	  goto success;
   2261 	}
   2262 
   2263       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2264 	 Unusual, but possible.  Warn, but continue.  */
   2265       if (elf_dynsymtab (abfd) != 0)
   2266 	{
   2267 	  _bfd_error_handler
   2268 	    /* xgettext:c-format */
   2269 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2270 	       " - ignoring the table in section %u"),
   2271 	     abfd, shindex);
   2272 	  goto success;
   2273 	}
   2274       elf_dynsymtab (abfd) = shindex;
   2275       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2276       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2277       abfd->flags |= HAS_SYMS;
   2278 
   2279       /* Besides being a symbol table, we also treat this as a regular
   2280 	 section, so that objcopy can handle it.  */
   2281       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2282       goto success;
   2283 
   2284     case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections.  */
   2285       {
   2286 	elf_section_list * entry;
   2287 
   2288 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2289 	  if (entry->ndx == shindex)
   2290 	    goto success;
   2291 
   2292 	entry = bfd_alloc (abfd, sizeof (*entry));
   2293 	if (entry == NULL)
   2294 	  goto fail;
   2295 	entry->ndx = shindex;
   2296 	entry->hdr = * hdr;
   2297 	entry->next = elf_symtab_shndx_list (abfd);
   2298 	elf_symtab_shndx_list (abfd) = entry;
   2299 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2300 	goto success;
   2301       }
   2302 
   2303     case SHT_STRTAB:		/* A string table.  */
   2304       if (hdr->bfd_section != NULL)
   2305 	goto success;
   2306 
   2307       if (ehdr->e_shstrndx == shindex)
   2308 	{
   2309 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2310 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2311 	  goto success;
   2312 	}
   2313 
   2314       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2315 	{
   2316 	symtab_strtab:
   2317 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2318 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2319 	  goto success;
   2320 	}
   2321 
   2322       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2323 	{
   2324 	dynsymtab_strtab:
   2325 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2326 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2327 	  elf_elfsections (abfd)[shindex] = hdr;
   2328 	  /* We also treat this as a regular section, so that objcopy
   2329 	     can handle it.  */
   2330 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2331 						 shindex);
   2332 	  goto success;
   2333 	}
   2334 
   2335       /* If the string table isn't one of the above, then treat it as a
   2336 	 regular section.  We need to scan all the headers to be sure,
   2337 	 just in case this strtab section appeared before the above.  */
   2338       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2339 	{
   2340 	  unsigned int i, num_sec;
   2341 
   2342 	  num_sec = elf_numsections (abfd);
   2343 	  for (i = 1; i < num_sec; i++)
   2344 	    {
   2345 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2346 	      if (hdr2->sh_link == shindex)
   2347 		{
   2348 		  /* Prevent endless recursion on broken objects.  */
   2349 		  if (i == shindex)
   2350 		    goto fail;
   2351 		  if (! bfd_section_from_shdr (abfd, i))
   2352 		    goto fail;
   2353 		  if (elf_onesymtab (abfd) == i)
   2354 		    goto symtab_strtab;
   2355 		  if (elf_dynsymtab (abfd) == i)
   2356 		    goto dynsymtab_strtab;
   2357 		}
   2358 	    }
   2359 	}
   2360       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2361       goto success;
   2362 
   2363     case SHT_REL:
   2364     case SHT_RELA:
   2365     case SHT_RELR:
   2366       /* *These* do a lot of work -- but build no sections!  */
   2367       {
   2368 	asection *target_sect;
   2369 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2370 	unsigned int num_sec = elf_numsections (abfd);
   2371 	struct bfd_elf_section_data *esdt;
   2372 	bfd_size_type size;
   2373 
   2374 	if (hdr->sh_type == SHT_REL)
   2375 	  size = bed->s->sizeof_rel;
   2376 	else if (hdr->sh_type == SHT_RELA)
   2377 	  size = bed->s->sizeof_rela;
   2378 	else
   2379 	  size = bed->s->arch_size / 8;
   2380 	if (hdr->sh_entsize != size)
   2381 	  goto fail;
   2382 
   2383 	/* Check for a bogus link to avoid crashing.  */
   2384 	if (hdr->sh_link >= num_sec)
   2385 	  {
   2386 	    _bfd_error_handler
   2387 	      /* xgettext:c-format */
   2388 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2389 	       abfd, hdr->sh_link, name, shindex);
   2390 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2391 						   shindex);
   2392 	    goto success;
   2393 	  }
   2394 
   2395 	/* Get the symbol table.  */
   2396 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2397 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2398 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2399 	  goto fail;
   2400 
   2401 	/* If this is an alloc section in an executable or shared
   2402 	   library, or the reloc section does not use the main symbol
   2403 	   table we don't treat it as a reloc section.  BFD can't
   2404 	   adequately represent such a section, so at least for now,
   2405 	   we don't try.  We just present it as a normal section.  We
   2406 	   also can't use it as a reloc section if it points to the
   2407 	   null section, an invalid section, another reloc section, or
   2408 	   its sh_link points to the null section.  */
   2409 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2410 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2411 	    || hdr->sh_link == SHN_UNDEF
   2412 	    || hdr->sh_link != elf_onesymtab (abfd)
   2413 	    || hdr->sh_info == SHN_UNDEF
   2414 	    || hdr->sh_info >= num_sec
   2415 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2416 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2417 	  {
   2418 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2419 						   shindex);
   2420 	    goto success;
   2421 	  }
   2422 
   2423 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2424 	  goto fail;
   2425 
   2426 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2427 	if (target_sect == NULL)
   2428 	  goto fail;
   2429 
   2430 	esdt = elf_section_data (target_sect);
   2431 	if (hdr->sh_type == SHT_RELA)
   2432 	  p_hdr = &esdt->rela.hdr;
   2433 	else
   2434 	  p_hdr = &esdt->rel.hdr;
   2435 
   2436 	/* PR 17512: file: 0b4f81b7.
   2437 	   Also see PR 24456, for a file which deliberately has two reloc
   2438 	   sections.  */
   2439 	if (*p_hdr != NULL)
   2440 	  {
   2441 	    if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
   2442 	      {
   2443 		_bfd_error_handler
   2444 		  /* xgettext:c-format */
   2445 		  (_("%pB: warning: secondary relocation section '%s' "
   2446 		     "for section %pA found - ignoring"),
   2447 		   abfd, name, target_sect);
   2448 	      }
   2449 	    else
   2450 	      esdt->has_secondary_relocs = true;
   2451 	    goto success;
   2452 	  }
   2453 
   2454 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2455 	if (hdr2 == NULL)
   2456 	  goto fail;
   2457 	*hdr2 = *hdr;
   2458 	*p_hdr = hdr2;
   2459 	elf_elfsections (abfd)[shindex] = hdr2;
   2460 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2461 				     * bed->s->int_rels_per_ext_rel);
   2462 	target_sect->flags |= SEC_RELOC;
   2463 	target_sect->relocation = NULL;
   2464 	target_sect->rel_filepos = hdr->sh_offset;
   2465 	/* In the section to which the relocations apply, mark whether
   2466 	   its relocations are of the REL or RELA variety.  */
   2467 	if (hdr->sh_size != 0)
   2468 	  {
   2469 	    if (hdr->sh_type == SHT_RELA)
   2470 	      target_sect->use_rela_p = 1;
   2471 	  }
   2472 	abfd->flags |= HAS_RELOC;
   2473 	goto success;
   2474       }
   2475 
   2476     case SHT_GNU_verdef:
   2477       elf_dynverdef (abfd) = shindex;
   2478       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2479       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2480       goto success;
   2481 
   2482     case SHT_GNU_versym:
   2483       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2484 	goto fail;
   2485 
   2486       elf_dynversym (abfd) = shindex;
   2487       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2488       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2489       goto success;
   2490 
   2491     case SHT_GNU_verneed:
   2492       elf_dynverref (abfd) = shindex;
   2493       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2494       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2495       goto success;
   2496 
   2497     case SHT_SHLIB:
   2498       goto success;
   2499 
   2500     case SHT_GROUP:
   2501       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   2502 	goto fail;
   2503 
   2504       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2505 	goto fail;
   2506 
   2507       goto success;
   2508 
   2509     default:
   2510       /* Possibly an attributes section.  */
   2511       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2512 	  || hdr->sh_type == bed->obj_attrs_section_type)
   2513 	{
   2514 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2515 	    goto fail;
   2516 	  _bfd_elf_parse_attributes (abfd, hdr);
   2517 	  goto success;
   2518 	}
   2519 
   2520       /* Check for any processor-specific section types.  */
   2521       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2522 	goto success;
   2523 
   2524       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2525 	{
   2526 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2527 	    /* FIXME: How to properly handle allocated section reserved
   2528 	       for applications?  */
   2529 	    _bfd_error_handler
   2530 	      /* xgettext:c-format */
   2531 	      (_("%pB: unknown type [%#x] section `%s'"),
   2532 	       abfd, hdr->sh_type, name);
   2533 	  else
   2534 	    {
   2535 	      /* Allow sections reserved for applications.  */
   2536 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2537 						     shindex);
   2538 	      goto success;
   2539 	    }
   2540 	}
   2541       else if (hdr->sh_type >= SHT_LOPROC
   2542 	       && hdr->sh_type <= SHT_HIPROC)
   2543 	/* FIXME: We should handle this section.  */
   2544 	_bfd_error_handler
   2545 	  /* xgettext:c-format */
   2546 	  (_("%pB: unknown type [%#x] section `%s'"),
   2547 	   abfd, hdr->sh_type, name);
   2548       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2549 	{
   2550 	  /* Unrecognised OS-specific sections.  */
   2551 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2552 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2553 	       required to correctly process the section and the file should
   2554 	       be rejected with an error message.  */
   2555 	    _bfd_error_handler
   2556 	      /* xgettext:c-format */
   2557 	      (_("%pB: unknown type [%#x] section `%s'"),
   2558 	       abfd, hdr->sh_type, name);
   2559 	  else
   2560 	    {
   2561 	      /* Otherwise it should be processed.  */
   2562 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2563 	      goto success;
   2564 	    }
   2565 	}
   2566       else
   2567 	/* FIXME: We should handle this section.  */
   2568 	_bfd_error_handler
   2569 	  /* xgettext:c-format */
   2570 	  (_("%pB: unknown type [%#x] section `%s'"),
   2571 	   abfd, hdr->sh_type, name);
   2572 
   2573       goto fail;
   2574     }
   2575 
   2576  fail:
   2577   ret = false;
   2578  success:
   2579   elf_tdata (abfd)->being_created[shindex] = false;
   2580   return ret;
   2581 }
   2582 
   2583 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   2584 
   2585 Elf_Internal_Sym *
   2586 bfd_sym_from_r_symndx (struct sym_cache *cache,
   2587 		       bfd *abfd,
   2588 		       unsigned long r_symndx)
   2589 {
   2590   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   2591 
   2592   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   2593     {
   2594       Elf_Internal_Shdr *symtab_hdr;
   2595       unsigned char esym[sizeof (Elf64_External_Sym)];
   2596       Elf_External_Sym_Shndx eshndx;
   2597 
   2598       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2599       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   2600 				&cache->sym[ent], esym, &eshndx) == NULL)
   2601 	return NULL;
   2602 
   2603       if (cache->abfd != abfd)
   2604 	{
   2605 	  memset (cache->indx, -1, sizeof (cache->indx));
   2606 	  cache->abfd = abfd;
   2607 	}
   2608       cache->indx[ent] = r_symndx;
   2609     }
   2610 
   2611   return &cache->sym[ent];
   2612 }
   2613 
   2614 /* Given an ELF section number, retrieve the corresponding BFD
   2615    section.  */
   2616 
   2617 asection *
   2618 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   2619 {
   2620   if (sec_index >= elf_numsections (abfd))
   2621     return NULL;
   2622   return elf_elfsections (abfd)[sec_index]->bfd_section;
   2623 }
   2624 
   2625 static const struct bfd_elf_special_section special_sections_b[] =
   2626 {
   2627   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2628   { NULL,		    0,	0, 0,		 0 }
   2629 };
   2630 
   2631 static const struct bfd_elf_special_section special_sections_c[] =
   2632 {
   2633   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   2634   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   2635   { NULL,			0, 0, 0,	    0 }
   2636 };
   2637 
   2638 static const struct bfd_elf_special_section special_sections_d[] =
   2639 {
   2640   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2641   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2642   /* There are more DWARF sections than these, but they needn't be added here
   2643      unless you have to cope with broken compilers that don't emit section
   2644      attributes or you want to help the user writing assembler.  */
   2645   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   2646   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   2647   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   2648   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   2649   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   2650   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   2651   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   2652   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   2653   { NULL,		       0,	 0, 0,		  0 }
   2654 };
   2655 
   2656 static const struct bfd_elf_special_section special_sections_f[] =
   2657 {
   2658   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2659   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   2660   { NULL,			   0 , 0, 0,		  0 }
   2661 };
   2662 
   2663 static const struct bfd_elf_special_section special_sections_g[] =
   2664 {
   2665   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2666   { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2667   { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2668   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   2669   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2670   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   2671   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   2672   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   2673   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   2674   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   2675   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   2676   { NULL,			 0,	   0, 0,	       0 }
   2677 };
   2678 
   2679 static const struct bfd_elf_special_section special_sections_h[] =
   2680 {
   2681   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   2682   { NULL,		     0, 0, 0,		 0 }
   2683 };
   2684 
   2685 static const struct bfd_elf_special_section special_sections_i[] =
   2686 {
   2687   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2688   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2689   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   2690   { NULL,		       0,      0, 0,		  0 }
   2691 };
   2692 
   2693 static const struct bfd_elf_special_section special_sections_l[] =
   2694 {
   2695   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   2696   { NULL,		     0, 0, 0,		 0 }
   2697 };
   2698 
   2699 static const struct bfd_elf_special_section special_sections_n[] =
   2700 {
   2701   { STRING_COMMA_LEN (".noinit"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2702   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   2703   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   2704   { NULL,		     0,		  0, 0,		   0 }
   2705 };
   2706 
   2707 static const struct bfd_elf_special_section special_sections_p[] =
   2708 {
   2709   { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,	SHF_ALLOC + SHF_WRITE },
   2710   { STRING_COMMA_LEN (".persistent"),	 -2, SHT_PROGBITS,	SHF_ALLOC + SHF_WRITE },
   2711   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2712   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   2713   { NULL,		    0,		  0, 0,			0 }
   2714 };
   2715 
   2716 static const struct bfd_elf_special_section special_sections_r[] =
   2717 {
   2718   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   2719   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   2720   { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
   2721   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   2722   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   2723   { NULL,		    0,	   0, 0,	    0 }
   2724 };
   2725 
   2726 static const struct bfd_elf_special_section special_sections_s[] =
   2727 {
   2728   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   2729   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   2730   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   2731   /* See struct bfd_elf_special_section declaration for the semantics of
   2732      this special case where .prefix_length != strlen (.prefix).  */
   2733   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   2734   { NULL,			0,  0, 0,	   0 }
   2735 };
   2736 
   2737 static const struct bfd_elf_special_section special_sections_t[] =
   2738 {
   2739   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   2740   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2741   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2742   { NULL,		      0,  0, 0,		   0 }
   2743 };
   2744 
   2745 static const struct bfd_elf_special_section special_sections_z[] =
   2746 {
   2747   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   2748   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   2749   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   2750   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   2751   { NULL,		      0,  0, 0,		   0 }
   2752 };
   2753 
   2754 static const struct bfd_elf_special_section * const special_sections[] =
   2755 {
   2756   special_sections_b,		/* 'b' */
   2757   special_sections_c,		/* 'c' */
   2758   special_sections_d,		/* 'd' */
   2759   NULL,				/* 'e' */
   2760   special_sections_f,		/* 'f' */
   2761   special_sections_g,		/* 'g' */
   2762   special_sections_h,		/* 'h' */
   2763   special_sections_i,		/* 'i' */
   2764   NULL,				/* 'j' */
   2765   NULL,				/* 'k' */
   2766   special_sections_l,		/* 'l' */
   2767   NULL,				/* 'm' */
   2768   special_sections_n,		/* 'n' */
   2769   NULL,				/* 'o' */
   2770   special_sections_p,		/* 'p' */
   2771   NULL,				/* 'q' */
   2772   special_sections_r,		/* 'r' */
   2773   special_sections_s,		/* 's' */
   2774   special_sections_t,		/* 't' */
   2775   NULL,				/* 'u' */
   2776   NULL,				/* 'v' */
   2777   NULL,				/* 'w' */
   2778   NULL,				/* 'x' */
   2779   NULL,				/* 'y' */
   2780   special_sections_z		/* 'z' */
   2781 };
   2782 
   2783 const struct bfd_elf_special_section *
   2784 _bfd_elf_get_special_section (const char *name,
   2785 			      const struct bfd_elf_special_section *spec,
   2786 			      unsigned int rela)
   2787 {
   2788   int i;
   2789   int len;
   2790 
   2791   len = strlen (name);
   2792 
   2793   for (i = 0; spec[i].prefix != NULL; i++)
   2794     {
   2795       int suffix_len;
   2796       int prefix_len = spec[i].prefix_length;
   2797 
   2798       if (len < prefix_len)
   2799 	continue;
   2800       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   2801 	continue;
   2802 
   2803       suffix_len = spec[i].suffix_length;
   2804       if (suffix_len <= 0)
   2805 	{
   2806 	  if (name[prefix_len] != 0)
   2807 	    {
   2808 	      if (suffix_len == 0)
   2809 		continue;
   2810 	      if (name[prefix_len] != '.'
   2811 		  && (suffix_len == -2
   2812 		      || (rela && spec[i].type == SHT_REL)))
   2813 		continue;
   2814 	    }
   2815 	}
   2816       else
   2817 	{
   2818 	  if (len < prefix_len + suffix_len)
   2819 	    continue;
   2820 	  if (memcmp (name + len - suffix_len,
   2821 		      spec[i].prefix + prefix_len,
   2822 		      suffix_len) != 0)
   2823 	    continue;
   2824 	}
   2825       return &spec[i];
   2826     }
   2827 
   2828   return NULL;
   2829 }
   2830 
   2831 const struct bfd_elf_special_section *
   2832 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   2833 {
   2834   int i;
   2835   const struct bfd_elf_special_section *spec;
   2836   const struct elf_backend_data *bed;
   2837 
   2838   /* See if this is one of the special sections.  */
   2839   if (sec->name == NULL)
   2840     return NULL;
   2841 
   2842   bed = get_elf_backend_data (abfd);
   2843   spec = bed->special_sections;
   2844   if (spec)
   2845     {
   2846       spec = _bfd_elf_get_special_section (sec->name,
   2847 					   bed->special_sections,
   2848 					   sec->use_rela_p);
   2849       if (spec != NULL)
   2850 	return spec;
   2851     }
   2852 
   2853   if (sec->name[0] != '.')
   2854     return NULL;
   2855 
   2856   i = sec->name[1] - 'b';
   2857   if (i < 0 || i > 'z' - 'b')
   2858     return NULL;
   2859 
   2860   spec = special_sections[i];
   2861 
   2862   if (spec == NULL)
   2863     return NULL;
   2864 
   2865   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   2866 }
   2867 
   2868 bool
   2869 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   2870 {
   2871   struct bfd_elf_section_data *sdata;
   2872   const struct elf_backend_data *bed;
   2873   const struct bfd_elf_special_section *ssect;
   2874 
   2875   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   2876   if (sdata == NULL)
   2877     {
   2878       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   2879 							  sizeof (*sdata));
   2880       if (sdata == NULL)
   2881 	return false;
   2882       sec->used_by_bfd = sdata;
   2883     }
   2884 
   2885   /* Indicate whether or not this section should use RELA relocations.  */
   2886   bed = get_elf_backend_data (abfd);
   2887   sec->use_rela_p = bed->default_use_rela_p;
   2888 
   2889   /* Set up ELF section type and flags for newly created sections, if
   2890      there is an ABI mandated section.  */
   2891   ssect = (*bed->get_sec_type_attr) (abfd, sec);
   2892   if (ssect != NULL)
   2893     {
   2894       elf_section_type (sec) = ssect->type;
   2895       elf_section_flags (sec) = ssect->attr;
   2896     }
   2897 
   2898   return _bfd_generic_new_section_hook (abfd, sec);
   2899 }
   2900 
   2901 /* Create a new bfd section from an ELF program header.
   2902 
   2903    Since program segments have no names, we generate a synthetic name
   2904    of the form segment<NUM>, where NUM is generally the index in the
   2905    program header table.  For segments that are split (see below) we
   2906    generate the names segment<NUM>a and segment<NUM>b.
   2907 
   2908    Note that some program segments may have a file size that is different than
   2909    (less than) the memory size.  All this means is that at execution the
   2910    system must allocate the amount of memory specified by the memory size,
   2911    but only initialize it with the first "file size" bytes read from the
   2912    file.  This would occur for example, with program segments consisting
   2913    of combined data+bss.
   2914 
   2915    To handle the above situation, this routine generates TWO bfd sections
   2916    for the single program segment.  The first has the length specified by
   2917    the file size of the segment, and the second has the length specified
   2918    by the difference between the two sizes.  In effect, the segment is split
   2919    into its initialized and uninitialized parts.
   2920 
   2921  */
   2922 
   2923 bool
   2924 _bfd_elf_make_section_from_phdr (bfd *abfd,
   2925 				 Elf_Internal_Phdr *hdr,
   2926 				 int hdr_index,
   2927 				 const char *type_name)
   2928 {
   2929   asection *newsect;
   2930   char *name;
   2931   char namebuf[64];
   2932   size_t len;
   2933   int split;
   2934   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   2935 
   2936   split = ((hdr->p_memsz > 0)
   2937 	    && (hdr->p_filesz > 0)
   2938 	    && (hdr->p_memsz > hdr->p_filesz));
   2939 
   2940   if (hdr->p_filesz > 0)
   2941     {
   2942       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   2943       len = strlen (namebuf) + 1;
   2944       name = (char *) bfd_alloc (abfd, len);
   2945       if (!name)
   2946 	return false;
   2947       memcpy (name, namebuf, len);
   2948       newsect = bfd_make_section (abfd, name);
   2949       if (newsect == NULL)
   2950 	return false;
   2951       newsect->vma = hdr->p_vaddr / opb;
   2952       newsect->lma = hdr->p_paddr / opb;
   2953       newsect->size = hdr->p_filesz;
   2954       newsect->filepos = hdr->p_offset;
   2955       newsect->flags |= SEC_HAS_CONTENTS;
   2956       newsect->alignment_power = bfd_log2 (hdr->p_align);
   2957       if (hdr->p_type == PT_LOAD)
   2958 	{
   2959 	  newsect->flags |= SEC_ALLOC;
   2960 	  newsect->flags |= SEC_LOAD;
   2961 	  if (hdr->p_flags & PF_X)
   2962 	    {
   2963 	      /* FIXME: all we known is that it has execute PERMISSION,
   2964 		 may be data.  */
   2965 	      newsect->flags |= SEC_CODE;
   2966 	    }
   2967 	}
   2968       if (!(hdr->p_flags & PF_W))
   2969 	{
   2970 	  newsect->flags |= SEC_READONLY;
   2971 	}
   2972     }
   2973 
   2974   if (hdr->p_memsz > hdr->p_filesz)
   2975     {
   2976       bfd_vma align;
   2977 
   2978       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   2979       len = strlen (namebuf) + 1;
   2980       name = (char *) bfd_alloc (abfd, len);
   2981       if (!name)
   2982 	return false;
   2983       memcpy (name, namebuf, len);
   2984       newsect = bfd_make_section (abfd, name);
   2985       if (newsect == NULL)
   2986 	return false;
   2987       newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
   2988       newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
   2989       newsect->size = hdr->p_memsz - hdr->p_filesz;
   2990       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   2991       align = newsect->vma & -newsect->vma;
   2992       if (align == 0 || align > hdr->p_align)
   2993 	align = hdr->p_align;
   2994       newsect->alignment_power = bfd_log2 (align);
   2995       if (hdr->p_type == PT_LOAD)
   2996 	{
   2997 	  newsect->flags |= SEC_ALLOC;
   2998 	  if (hdr->p_flags & PF_X)
   2999 	    newsect->flags |= SEC_CODE;
   3000 	}
   3001       if (!(hdr->p_flags & PF_W))
   3002 	newsect->flags |= SEC_READONLY;
   3003     }
   3004 
   3005   return true;
   3006 }
   3007 
   3008 static bool
   3009 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   3010 {
   3011   /* The return value is ignored.  Build-ids are considered optional.  */
   3012   if (templ->xvec->flavour == bfd_target_elf_flavour)
   3013     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   3014       (templ, offset);
   3015   return false;
   3016 }
   3017 
   3018 bool
   3019 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3020 {
   3021   const struct elf_backend_data *bed;
   3022 
   3023   switch (hdr->p_type)
   3024     {
   3025     case PT_NULL:
   3026       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3027 
   3028     case PT_LOAD:
   3029       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3030 	return false;
   3031       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3032 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3033       return true;
   3034 
   3035     case PT_DYNAMIC:
   3036       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3037 
   3038     case PT_INTERP:
   3039       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3040 
   3041     case PT_NOTE:
   3042       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3043 	return false;
   3044       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3045 			    hdr->p_align))
   3046 	return false;
   3047       return true;
   3048 
   3049     case PT_SHLIB:
   3050       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3051 
   3052     case PT_PHDR:
   3053       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3054 
   3055     case PT_GNU_EH_FRAME:
   3056       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3057 					      "eh_frame_hdr");
   3058 
   3059     case PT_GNU_STACK:
   3060       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3061 
   3062     case PT_GNU_RELRO:
   3063       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3064 
   3065     default:
   3066       /* Check for any processor-specific program segment types.  */
   3067       bed = get_elf_backend_data (abfd);
   3068       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3069     }
   3070 }
   3071 
   3072 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3073    REL or RELA.  */
   3074 
   3075 Elf_Internal_Shdr *
   3076 _bfd_elf_single_rel_hdr (asection *sec)
   3077 {
   3078   if (elf_section_data (sec)->rel.hdr)
   3079     {
   3080       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3081       return elf_section_data (sec)->rel.hdr;
   3082     }
   3083   else
   3084     return elf_section_data (sec)->rela.hdr;
   3085 }
   3086 
   3087 static bool
   3088 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3089 			    Elf_Internal_Shdr *rel_hdr,
   3090 			    const char *sec_name,
   3091 			    bool use_rela_p)
   3092 {
   3093   char *name = (char *) bfd_alloc (abfd,
   3094 				   sizeof ".rela" + strlen (sec_name));
   3095   if (name == NULL)
   3096     return false;
   3097 
   3098   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3099   rel_hdr->sh_name =
   3100     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3101 					false);
   3102   if (rel_hdr->sh_name == (unsigned int) -1)
   3103     return false;
   3104 
   3105   return true;
   3106 }
   3107 
   3108 /* Allocate and initialize a section-header for a new reloc section,
   3109    containing relocations against ASECT.  It is stored in RELDATA.  If
   3110    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3111    relocations.  */
   3112 
   3113 static bool
   3114 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3115 			  struct bfd_elf_section_reloc_data *reldata,
   3116 			  const char *sec_name,
   3117 			  bool use_rela_p,
   3118 			  bool delay_st_name_p)
   3119 {
   3120   Elf_Internal_Shdr *rel_hdr;
   3121   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3122 
   3123   BFD_ASSERT (reldata->hdr == NULL);
   3124   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3125   reldata->hdr = rel_hdr;
   3126 
   3127   if (delay_st_name_p)
   3128     rel_hdr->sh_name = (unsigned int) -1;
   3129   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3130 					use_rela_p))
   3131     return false;
   3132   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3133   rel_hdr->sh_entsize = (use_rela_p
   3134 			 ? bed->s->sizeof_rela
   3135 			 : bed->s->sizeof_rel);
   3136   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3137   rel_hdr->sh_flags = 0;
   3138   rel_hdr->sh_addr = 0;
   3139   rel_hdr->sh_size = 0;
   3140   rel_hdr->sh_offset = 0;
   3141 
   3142   return true;
   3143 }
   3144 
   3145 /* Return the default section type based on the passed in section flags.  */
   3146 
   3147 int
   3148 bfd_elf_get_default_section_type (flagword flags)
   3149 {
   3150   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3151       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3152     return SHT_NOBITS;
   3153   return SHT_PROGBITS;
   3154 }
   3155 
   3156 struct fake_section_arg
   3157 {
   3158   struct bfd_link_info *link_info;
   3159   bool failed;
   3160 };
   3161 
   3162 /* Set up an ELF internal section header for a section.  */
   3163 
   3164 static void
   3165 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3166 {
   3167   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3168   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3169   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3170   Elf_Internal_Shdr *this_hdr;
   3171   unsigned int sh_type;
   3172   const char *name = asect->name;
   3173   bool delay_st_name_p = false;
   3174   bfd_vma mask;
   3175 
   3176   if (arg->failed)
   3177     {
   3178       /* We already failed; just get out of the bfd_map_over_sections
   3179 	 loop.  */
   3180       return;
   3181     }
   3182 
   3183   this_hdr = &esd->this_hdr;
   3184 
   3185   if (arg->link_info)
   3186     {
   3187       /* ld: compress DWARF debug sections with names: .debug_*.  */
   3188       if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
   3189 	  && (asect->flags & SEC_DEBUGGING)
   3190 	  && name[1] == 'd'
   3191 	  && name[6] == '_')
   3192 	{
   3193 	  /* Set SEC_ELF_COMPRESS to indicate this section should be
   3194 	     compressed.  */
   3195 	  asect->flags |= SEC_ELF_COMPRESS;
   3196 	  /* If this section will be compressed, delay adding section
   3197 	     name to section name section after it is compressed in
   3198 	     _bfd_elf_assign_file_positions_for_non_load.  */
   3199 	  delay_st_name_p = true;
   3200 	}
   3201     }
   3202   else if ((asect->flags & SEC_ELF_RENAME))
   3203     {
   3204       /* objcopy: rename output DWARF debug section.  */
   3205       if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
   3206 	{
   3207 	  /* When we decompress or compress with SHF_COMPRESSED,
   3208 	     convert section name from .zdebug_* to .debug_* if
   3209 	     needed.  */
   3210 	  if (name[1] == 'z')
   3211 	    {
   3212 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   3213 	      if (new_name == NULL)
   3214 		{
   3215 		  arg->failed = true;
   3216 		  return;
   3217 		}
   3218 	      name = new_name;
   3219 	    }
   3220 	}
   3221       else if (asect->compress_status == COMPRESS_SECTION_DONE)
   3222 	{
   3223 	  /* PR binutils/18087: Compression does not always make a
   3224 	     section smaller.  So only rename the section when
   3225 	     compression has actually taken place.  If input section
   3226 	     name is .zdebug_*, we should never compress it again.  */
   3227 	  char *new_name = convert_debug_to_zdebug (abfd, name);
   3228 	  if (new_name == NULL)
   3229 	    {
   3230 	      arg->failed = true;
   3231 	      return;
   3232 	    }
   3233 	  BFD_ASSERT (name[1] != 'z');
   3234 	  name = new_name;
   3235 	}
   3236     }
   3237 
   3238   if (delay_st_name_p)
   3239     this_hdr->sh_name = (unsigned int) -1;
   3240   else
   3241     {
   3242       this_hdr->sh_name
   3243 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3244 					      name, false);
   3245       if (this_hdr->sh_name == (unsigned int) -1)
   3246 	{
   3247 	  arg->failed = true;
   3248 	  return;
   3249 	}
   3250     }
   3251 
   3252   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3253 
   3254   if ((asect->flags & SEC_ALLOC) != 0
   3255       || asect->user_set_vma)
   3256     this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
   3257   else
   3258     this_hdr->sh_addr = 0;
   3259 
   3260   this_hdr->sh_offset = 0;
   3261   this_hdr->sh_size = asect->size;
   3262   this_hdr->sh_link = 0;
   3263   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3264   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3265     {
   3266       _bfd_error_handler
   3267 	/* xgettext:c-format */
   3268 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3269 	 abfd, asect->alignment_power, asect);
   3270       arg->failed = true;
   3271       return;
   3272     }
   3273   /* Set sh_addralign to the highest power of two given by alignment
   3274      consistent with the section VMA.  Linker scripts can force VMA.  */
   3275   mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
   3276   this_hdr->sh_addralign = mask & -mask;
   3277   /* The sh_entsize and sh_info fields may have been set already by
   3278      copy_private_section_data.  */
   3279 
   3280   this_hdr->bfd_section = asect;
   3281   this_hdr->contents = NULL;
   3282 
   3283   /* If the section type is unspecified, we set it based on
   3284      asect->flags.  */
   3285   if (asect->type != 0)
   3286     sh_type = asect->type;
   3287   else if ((asect->flags & SEC_GROUP) != 0)
   3288     sh_type = SHT_GROUP;
   3289   else
   3290     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3291 
   3292   if (this_hdr->sh_type == SHT_NULL)
   3293     this_hdr->sh_type = sh_type;
   3294   else if (this_hdr->sh_type == SHT_NOBITS
   3295 	   && sh_type == SHT_PROGBITS
   3296 	   && (asect->flags & SEC_ALLOC) != 0)
   3297     {
   3298       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3299 	 allow the link to proceed.  This can happen when users link
   3300 	 non-bss input sections to bss output sections, or emit data
   3301 	 to a bss output section via a linker script.  */
   3302       _bfd_error_handler
   3303 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3304       this_hdr->sh_type = sh_type;
   3305     }
   3306 
   3307   switch (this_hdr->sh_type)
   3308     {
   3309     default:
   3310       break;
   3311 
   3312     case SHT_STRTAB:
   3313     case SHT_NOTE:
   3314     case SHT_NOBITS:
   3315     case SHT_PROGBITS:
   3316       break;
   3317 
   3318     case SHT_INIT_ARRAY:
   3319     case SHT_FINI_ARRAY:
   3320     case SHT_PREINIT_ARRAY:
   3321       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3322       break;
   3323 
   3324     case SHT_HASH:
   3325       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3326       break;
   3327 
   3328     case SHT_DYNSYM:
   3329       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3330       break;
   3331 
   3332     case SHT_DYNAMIC:
   3333       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3334       break;
   3335 
   3336     case SHT_RELA:
   3337       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3338 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3339       break;
   3340 
   3341      case SHT_REL:
   3342       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3343 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3344       break;
   3345 
   3346      case SHT_GNU_versym:
   3347       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3348       break;
   3349 
   3350      case SHT_GNU_verdef:
   3351       this_hdr->sh_entsize = 0;
   3352       /* objcopy or strip will copy over sh_info, but may not set
   3353 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3354 	 zero.  */
   3355       if (this_hdr->sh_info == 0)
   3356 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3357       else
   3358 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3359 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3360       break;
   3361 
   3362     case SHT_GNU_verneed:
   3363       this_hdr->sh_entsize = 0;
   3364       /* objcopy or strip will copy over sh_info, but may not set
   3365 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3366 	 zero.  */
   3367       if (this_hdr->sh_info == 0)
   3368 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3369       else
   3370 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3371 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3372       break;
   3373 
   3374     case SHT_GROUP:
   3375       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3376       break;
   3377 
   3378     case SHT_GNU_HASH:
   3379       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3380       break;
   3381     }
   3382 
   3383   if ((asect->flags & SEC_ALLOC) != 0)
   3384     this_hdr->sh_flags |= SHF_ALLOC;
   3385   if ((asect->flags & SEC_READONLY) == 0)
   3386     this_hdr->sh_flags |= SHF_WRITE;
   3387   if ((asect->flags & SEC_CODE) != 0)
   3388     this_hdr->sh_flags |= SHF_EXECINSTR;
   3389   if ((asect->flags & SEC_MERGE) != 0)
   3390     {
   3391       this_hdr->sh_flags |= SHF_MERGE;
   3392       this_hdr->sh_entsize = asect->entsize;
   3393     }
   3394   if ((asect->flags & SEC_STRINGS) != 0)
   3395     this_hdr->sh_flags |= SHF_STRINGS;
   3396   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3397     this_hdr->sh_flags |= SHF_GROUP;
   3398   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3399     {
   3400       this_hdr->sh_flags |= SHF_TLS;
   3401       if (asect->size == 0
   3402 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3403 	{
   3404 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3405 
   3406 	  this_hdr->sh_size = 0;
   3407 	  if (o != NULL)
   3408 	    {
   3409 	      this_hdr->sh_size = o->offset + o->size;
   3410 	      if (this_hdr->sh_size != 0)
   3411 		this_hdr->sh_type = SHT_NOBITS;
   3412 	    }
   3413 	}
   3414     }
   3415   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3416     this_hdr->sh_flags |= SHF_EXCLUDE;
   3417 
   3418   /* If the section has relocs, set up a section header for the
   3419      SHT_REL[A] section.  If two relocation sections are required for
   3420      this section, it is up to the processor-specific back-end to
   3421      create the other.  */
   3422   if ((asect->flags & SEC_RELOC) != 0)
   3423     {
   3424       /* When doing a relocatable link, create both REL and RELA sections if
   3425 	 needed.  */
   3426       if (arg->link_info
   3427 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3428 	  && esd->rel.count + esd->rela.count > 0
   3429 	  && (bfd_link_relocatable (arg->link_info)
   3430 	      || arg->link_info->emitrelocations))
   3431 	{
   3432 	  if (esd->rel.count && esd->rel.hdr == NULL
   3433 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3434 					    false, delay_st_name_p))
   3435 	    {
   3436 	      arg->failed = true;
   3437 	      return;
   3438 	    }
   3439 	  if (esd->rela.count && esd->rela.hdr == NULL
   3440 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3441 					    true, delay_st_name_p))
   3442 	    {
   3443 	      arg->failed = true;
   3444 	      return;
   3445 	    }
   3446 	}
   3447       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3448 					  (asect->use_rela_p
   3449 					   ? &esd->rela : &esd->rel),
   3450 					  name,
   3451 					  asect->use_rela_p,
   3452 					  delay_st_name_p))
   3453 	{
   3454 	  arg->failed = true;
   3455 	  return;
   3456 	}
   3457     }
   3458 
   3459   /* Check for processor-specific section types.  */
   3460   sh_type = this_hdr->sh_type;
   3461   if (bed->elf_backend_fake_sections
   3462       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3463     {
   3464       arg->failed = true;
   3465       return;
   3466     }
   3467 
   3468   if (sh_type == SHT_NOBITS && asect->size != 0)
   3469     {
   3470       /* Don't change the header type from NOBITS if we are being
   3471 	 called for objcopy --only-keep-debug.  */
   3472       this_hdr->sh_type = sh_type;
   3473     }
   3474 }
   3475 
   3476 /* Fill in the contents of a SHT_GROUP section.  Called from
   3477    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3478    when ELF targets use the generic linker, ld.  Called for ld -r
   3479    from bfd_elf_final_link.  */
   3480 
   3481 void
   3482 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3483 {
   3484   bool *failedptr = (bool *) failedptrarg;
   3485   asection *elt, *first;
   3486   unsigned char *loc;
   3487   bool gas;
   3488 
   3489   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3490      elfxx-ia64.c.  */
   3491   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3492       || sec->size == 0
   3493       || *failedptr)
   3494     return;
   3495 
   3496   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3497     {
   3498       unsigned long symindx = 0;
   3499 
   3500       /* elf_group_id will have been set up by objcopy and the
   3501 	 generic linker.  */
   3502       if (elf_group_id (sec) != NULL)
   3503 	symindx = elf_group_id (sec)->udata.i;
   3504 
   3505       if (symindx == 0)
   3506 	{
   3507 	  /* If called from the assembler, swap_out_syms will have set up
   3508 	     elf_section_syms.
   3509 	     PR 25699: A corrupt input file could contain bogus group info.  */
   3510 	  if (sec->index >= elf_num_section_syms (abfd)
   3511 	      || elf_section_syms (abfd)[sec->index] == NULL)
   3512 	    {
   3513 	      *failedptr = true;
   3514 	      return;
   3515 	    }
   3516 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3517 	}
   3518       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3519     }
   3520   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3521     {
   3522       /* The ELF backend linker sets sh_info to -2 when the group
   3523 	 signature symbol is global, and thus the index can't be
   3524 	 set until all local symbols are output.  */
   3525       asection *igroup;
   3526       struct bfd_elf_section_data *sec_data;
   3527       unsigned long symndx;
   3528       unsigned long extsymoff;
   3529       struct elf_link_hash_entry *h;
   3530 
   3531       /* The point of this little dance to the first SHF_GROUP section
   3532 	 then back to the SHT_GROUP section is that this gets us to
   3533 	 the SHT_GROUP in the input object.  */
   3534       igroup = elf_sec_group (elf_next_in_group (sec));
   3535       sec_data = elf_section_data (igroup);
   3536       symndx = sec_data->this_hdr.sh_info;
   3537       extsymoff = 0;
   3538       if (!elf_bad_symtab (igroup->owner))
   3539 	{
   3540 	  Elf_Internal_Shdr *symtab_hdr;
   3541 
   3542 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3543 	  extsymoff = symtab_hdr->sh_info;
   3544 	}
   3545       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3546       while (h->root.type == bfd_link_hash_indirect
   3547 	     || h->root.type == bfd_link_hash_warning)
   3548 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3549 
   3550       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3551     }
   3552 
   3553   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3554   gas = true;
   3555   if (sec->contents == NULL)
   3556     {
   3557       gas = false;
   3558       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3559 
   3560       /* Arrange for the section to be written out.  */
   3561       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3562       if (sec->contents == NULL)
   3563 	{
   3564 	  *failedptr = true;
   3565 	  return;
   3566 	}
   3567     }
   3568 
   3569   loc = sec->contents + sec->size;
   3570 
   3571   /* Get the pointer to the first section in the group that gas
   3572      squirreled away here.  objcopy arranges for this to be set to the
   3573      start of the input section group.  */
   3574   first = elt = elf_next_in_group (sec);
   3575 
   3576   /* First element is a flag word.  Rest of section is elf section
   3577      indices for all the sections of the group.  Write them backwards
   3578      just to keep the group in the same order as given in .section
   3579      directives, not that it matters.  */
   3580   while (elt != NULL)
   3581     {
   3582       asection *s;
   3583 
   3584       s = elt;
   3585       if (!gas)
   3586 	s = s->output_section;
   3587       if (s != NULL
   3588 	  && !bfd_is_abs_section (s))
   3589 	{
   3590 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   3591 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   3592 
   3593 	  if (elf_sec->rel.hdr != NULL
   3594 	      && (gas
   3595 		  || (input_elf_sec->rel.hdr != NULL
   3596 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   3597 	    {
   3598 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   3599 	      loc -= 4;
   3600 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   3601 	    }
   3602 	  if (elf_sec->rela.hdr != NULL
   3603 	      && (gas
   3604 		  || (input_elf_sec->rela.hdr != NULL
   3605 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   3606 	    {
   3607 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   3608 	      loc -= 4;
   3609 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   3610 	    }
   3611 	  loc -= 4;
   3612 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   3613 	}
   3614       elt = elf_next_in_group (elt);
   3615       if (elt == first)
   3616 	break;
   3617     }
   3618 
   3619   loc -= 4;
   3620   BFD_ASSERT (loc == sec->contents);
   3621 
   3622   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   3623 }
   3624 
   3625 /* Given NAME, the name of a relocation section stripped of its
   3626    .rel/.rela prefix, return the section in ABFD to which the
   3627    relocations apply.  */
   3628 
   3629 asection *
   3630 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   3631 {
   3632   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   3633      section likely apply to .got.plt or .got section.  */
   3634   if (get_elf_backend_data (abfd)->want_got_plt
   3635       && strcmp (name, ".plt") == 0)
   3636     {
   3637       asection *sec;
   3638 
   3639       name = ".got.plt";
   3640       sec = bfd_get_section_by_name (abfd, name);
   3641       if (sec != NULL)
   3642 	return sec;
   3643       name = ".got";
   3644     }
   3645 
   3646   return bfd_get_section_by_name (abfd, name);
   3647 }
   3648 
   3649 /* Return the section to which RELOC_SEC applies.  */
   3650 
   3651 static asection *
   3652 elf_get_reloc_section (asection *reloc_sec)
   3653 {
   3654   const char *name;
   3655   unsigned int type;
   3656   bfd *abfd;
   3657   const struct elf_backend_data *bed;
   3658 
   3659   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   3660   if (type != SHT_REL && type != SHT_RELA)
   3661     return NULL;
   3662 
   3663   /* We look up the section the relocs apply to by name.  */
   3664   name = reloc_sec->name;
   3665   if (!startswith (name, ".rel"))
   3666     return NULL;
   3667   name += 4;
   3668   if (type == SHT_RELA && *name++ != 'a')
   3669     return NULL;
   3670 
   3671   abfd = reloc_sec->owner;
   3672   bed = get_elf_backend_data (abfd);
   3673   return bed->get_reloc_section (abfd, name);
   3674 }
   3675 
   3676 /* Assign all ELF section numbers.  The dummy first section is handled here
   3677    too.  The link/info pointers for the standard section types are filled
   3678    in here too, while we're at it.  LINK_INFO will be 0 when arriving
   3679    here for objcopy, and when using the generic ELF linker.  */
   3680 
   3681 static bool
   3682 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   3683 {
   3684   struct elf_obj_tdata *t = elf_tdata (abfd);
   3685   asection *sec;
   3686   unsigned int section_number;
   3687   Elf_Internal_Shdr **i_shdrp;
   3688   struct bfd_elf_section_data *d;
   3689   bool need_symtab;
   3690   size_t amt;
   3691 
   3692   section_number = 1;
   3693 
   3694   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   3695 
   3696   /* SHT_GROUP sections are in relocatable files only.  */
   3697   if (link_info == NULL || !link_info->resolve_section_groups)
   3698     {
   3699       size_t reloc_count = 0;
   3700 
   3701       /* Put SHT_GROUP sections first.  */
   3702       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   3703 	{
   3704 	  d = elf_section_data (sec);
   3705 
   3706 	  if (d->this_hdr.sh_type == SHT_GROUP)
   3707 	    {
   3708 	      if (sec->flags & SEC_LINKER_CREATED)
   3709 		{
   3710 		  /* Remove the linker created SHT_GROUP sections.  */
   3711 		  bfd_section_list_remove (abfd, sec);
   3712 		  abfd->section_count--;
   3713 		}
   3714 	      else
   3715 		d->this_idx = section_number++;
   3716 	    }
   3717 
   3718 	  /* Count relocations.  */
   3719 	  reloc_count += sec->reloc_count;
   3720 	}
   3721 
   3722       /* Clear HAS_RELOC if there are no relocations.  */
   3723       if (reloc_count == 0)
   3724 	abfd->flags &= ~HAS_RELOC;
   3725     }
   3726 
   3727   for (sec = abfd->sections; sec; sec = sec->next)
   3728     {
   3729       d = elf_section_data (sec);
   3730 
   3731       if (d->this_hdr.sh_type != SHT_GROUP)
   3732 	d->this_idx = section_number++;
   3733       if (d->this_hdr.sh_name != (unsigned int) -1)
   3734 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   3735       if (d->rel.hdr)
   3736 	{
   3737 	  d->rel.idx = section_number++;
   3738 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   3739 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   3740 	}
   3741       else
   3742 	d->rel.idx = 0;
   3743 
   3744       if (d->rela.hdr)
   3745 	{
   3746 	  d->rela.idx = section_number++;
   3747 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   3748 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   3749 	}
   3750       else
   3751 	d->rela.idx = 0;
   3752     }
   3753 
   3754   need_symtab = (bfd_get_symcount (abfd) > 0
   3755 		 || (link_info == NULL
   3756 		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   3757 			 == HAS_RELOC)));
   3758   if (need_symtab)
   3759     {
   3760       elf_onesymtab (abfd) = section_number++;
   3761       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   3762       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   3763 	{
   3764 	  elf_section_list *entry;
   3765 
   3766 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   3767 
   3768 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   3769 	  entry->ndx = section_number++;
   3770 	  elf_symtab_shndx_list (abfd) = entry;
   3771 	  entry->hdr.sh_name
   3772 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3773 						  ".symtab_shndx", false);
   3774 	  if (entry->hdr.sh_name == (unsigned int) -1)
   3775 	    return false;
   3776 	}
   3777       elf_strtab_sec (abfd) = section_number++;
   3778       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   3779     }
   3780 
   3781   elf_shstrtab_sec (abfd) = section_number++;
   3782   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   3783   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   3784 
   3785   if (section_number >= SHN_LORESERVE)
   3786     {
   3787       /* xgettext:c-format */
   3788       _bfd_error_handler (_("%pB: too many sections: %u"),
   3789 			  abfd, section_number);
   3790       return false;
   3791     }
   3792 
   3793   elf_numsections (abfd) = section_number;
   3794   elf_elfheader (abfd)->e_shnum = section_number;
   3795 
   3796   /* Set up the list of section header pointers, in agreement with the
   3797      indices.  */
   3798   amt = section_number * sizeof (Elf_Internal_Shdr *);
   3799   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
   3800   if (i_shdrp == NULL)
   3801     return false;
   3802 
   3803   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   3804 						 sizeof (Elf_Internal_Shdr));
   3805   if (i_shdrp[0] == NULL)
   3806     {
   3807       bfd_release (abfd, i_shdrp);
   3808       return false;
   3809     }
   3810 
   3811   elf_elfsections (abfd) = i_shdrp;
   3812 
   3813   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   3814   if (need_symtab)
   3815     {
   3816       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   3817       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   3818 	{
   3819 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   3820 	  BFD_ASSERT (entry != NULL);
   3821 	  i_shdrp[entry->ndx] = & entry->hdr;
   3822 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   3823 	}
   3824       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   3825       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   3826     }
   3827 
   3828   for (sec = abfd->sections; sec; sec = sec->next)
   3829     {
   3830       asection *s;
   3831 
   3832       d = elf_section_data (sec);
   3833 
   3834       i_shdrp[d->this_idx] = &d->this_hdr;
   3835       if (d->rel.idx != 0)
   3836 	i_shdrp[d->rel.idx] = d->rel.hdr;
   3837       if (d->rela.idx != 0)
   3838 	i_shdrp[d->rela.idx] = d->rela.hdr;
   3839 
   3840       /* Fill in the sh_link and sh_info fields while we're at it.  */
   3841 
   3842       /* sh_link of a reloc section is the section index of the symbol
   3843 	 table.  sh_info is the section index of the section to which
   3844 	 the relocation entries apply.  */
   3845       if (d->rel.idx != 0)
   3846 	{
   3847 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   3848 	  d->rel.hdr->sh_info = d->this_idx;
   3849 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   3850 	}
   3851       if (d->rela.idx != 0)
   3852 	{
   3853 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   3854 	  d->rela.hdr->sh_info = d->this_idx;
   3855 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   3856 	}
   3857 
   3858       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   3859       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   3860 	{
   3861 	  s = elf_linked_to_section (sec);
   3862 	  /* We can now have a NULL linked section pointer.
   3863 	     This happens when the sh_link field is 0, which is done
   3864 	     when a linked to section is discarded but the linking
   3865 	     section has been retained for some reason.  */
   3866 	  if (s)
   3867 	    {
   3868 	      /* Check discarded linkonce section.  */
   3869 	      if (discarded_section (s))
   3870 		{
   3871 		  asection *kept;
   3872 		  _bfd_error_handler
   3873 		    /* xgettext:c-format */
   3874 		    (_("%pB: sh_link of section `%pA' points to"
   3875 		       " discarded section `%pA' of `%pB'"),
   3876 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   3877 		  /* Point to the kept section if it has the same
   3878 		     size as the discarded one.  */
   3879 		  kept = _bfd_elf_check_kept_section (s, link_info);
   3880 		  if (kept == NULL)
   3881 		    {
   3882 		      bfd_set_error (bfd_error_bad_value);
   3883 		      return false;
   3884 		    }
   3885 		  s = kept;
   3886 		}
   3887 	      /* Handle objcopy. */
   3888 	      else if (s->output_section == NULL)
   3889 		{
   3890 		  _bfd_error_handler
   3891 		    /* xgettext:c-format */
   3892 		    (_("%pB: sh_link of section `%pA' points to"
   3893 		       " removed section `%pA' of `%pB'"),
   3894 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   3895 		  bfd_set_error (bfd_error_bad_value);
   3896 		  return false;
   3897 		}
   3898 	      s = s->output_section;
   3899 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3900 	    }
   3901 	}
   3902 
   3903       switch (d->this_hdr.sh_type)
   3904 	{
   3905 	case SHT_REL:
   3906 	case SHT_RELA:
   3907 	  /* A reloc section which we are treating as a normal BFD
   3908 	     section.  sh_link is the section index of the symbol
   3909 	     table.  sh_info is the section index of the section to
   3910 	     which the relocation entries apply.  We assume that an
   3911 	     allocated reloc section uses the dynamic symbol table
   3912 	     if there is one.  Otherwise we guess the normal symbol
   3913 	     table.  FIXME: How can we be sure?  */
   3914 	  if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
   3915 	    {
   3916 	      s = bfd_get_section_by_name (abfd, ".dynsym");
   3917 	      if (s != NULL)
   3918 		d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3919 	    }
   3920 	  if (d->this_hdr.sh_link == 0)
   3921 	    d->this_hdr.sh_link = elf_onesymtab (abfd);
   3922 
   3923 	  s = elf_get_reloc_section (sec);
   3924 	  if (s != NULL)
   3925 	    {
   3926 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   3927 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   3928 	    }
   3929 	  break;
   3930 
   3931 	case SHT_STRTAB:
   3932 	  /* We assume that a section named .stab*str is a stabs
   3933 	     string section.  We look for a section with the same name
   3934 	     but without the trailing ``str'', and set its sh_link
   3935 	     field to point to this section.  */
   3936 	  if (startswith (sec->name, ".stab")
   3937 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   3938 	    {
   3939 	      size_t len;
   3940 	      char *alc;
   3941 
   3942 	      len = strlen (sec->name);
   3943 	      alc = (char *) bfd_malloc (len - 2);
   3944 	      if (alc == NULL)
   3945 		return false;
   3946 	      memcpy (alc, sec->name, len - 3);
   3947 	      alc[len - 3] = '\0';
   3948 	      s = bfd_get_section_by_name (abfd, alc);
   3949 	      free (alc);
   3950 	      if (s != NULL)
   3951 		{
   3952 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   3953 
   3954 		  /* This is a .stab section.  */
   3955 		  elf_section_data (s)->this_hdr.sh_entsize = 12;
   3956 		}
   3957 	    }
   3958 	  break;
   3959 
   3960 	case SHT_DYNAMIC:
   3961 	case SHT_DYNSYM:
   3962 	case SHT_GNU_verneed:
   3963 	case SHT_GNU_verdef:
   3964 	  /* sh_link is the section header index of the string table
   3965 	     used for the dynamic entries, or the symbol table, or the
   3966 	     version strings.  */
   3967 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   3968 	  if (s != NULL)
   3969 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3970 	  break;
   3971 
   3972 	case SHT_GNU_LIBLIST:
   3973 	  /* sh_link is the section header index of the prelink library
   3974 	     list used for the dynamic entries, or the symbol table, or
   3975 	     the version strings.  */
   3976 	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
   3977 					      ? ".dynstr" : ".gnu.libstr"));
   3978 	  if (s != NULL)
   3979 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3980 	  break;
   3981 
   3982 	case SHT_HASH:
   3983 	case SHT_GNU_HASH:
   3984 	case SHT_GNU_versym:
   3985 	  /* sh_link is the section header index of the symbol table
   3986 	     this hash table or version table is for.  */
   3987 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3988 	  if (s != NULL)
   3989 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3990 	  break;
   3991 
   3992 	case SHT_GROUP:
   3993 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   3994 	}
   3995     }
   3996 
   3997   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   3998      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   3999      debug section name from .debug_* to .zdebug_* if needed.  */
   4000 
   4001   return true;
   4002 }
   4003 
   4004 static bool
   4005 sym_is_global (bfd *abfd, asymbol *sym)
   4006 {
   4007   /* If the backend has a special mapping, use it.  */
   4008   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4009   if (bed->elf_backend_sym_is_global)
   4010     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   4011 
   4012   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   4013 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   4014 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   4015 }
   4016 
   4017 /* Filter global symbols of ABFD to include in the import library.  All
   4018    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4019    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4020    the beginning of that array.
   4021 
   4022    Returns the number of symbols to keep.  */
   4023 
   4024 unsigned int
   4025 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4026 				asymbol **syms, long symcount)
   4027 {
   4028   long src_count, dst_count = 0;
   4029 
   4030   for (src_count = 0; src_count < symcount; src_count++)
   4031     {
   4032       asymbol *sym = syms[src_count];
   4033       char *name = (char *) bfd_asymbol_name (sym);
   4034       struct bfd_link_hash_entry *h;
   4035 
   4036       if (!sym_is_global (abfd, sym))
   4037 	continue;
   4038 
   4039       h = bfd_link_hash_lookup (info->hash, name, false, false, false);
   4040       if (h == NULL)
   4041 	continue;
   4042       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4043 	continue;
   4044       if (h->linker_def || h->ldscript_def)
   4045 	continue;
   4046 
   4047       syms[dst_count++] = sym;
   4048     }
   4049 
   4050   syms[dst_count] = NULL;
   4051 
   4052   return dst_count;
   4053 }
   4054 
   4055 /* Don't output section symbols for sections that are not going to be
   4056    output, that are duplicates or there is no BFD section.  */
   4057 
   4058 static bool
   4059 ignore_section_sym (bfd *abfd, asymbol *sym)
   4060 {
   4061   elf_symbol_type *type_ptr;
   4062 
   4063   if (sym == NULL)
   4064     return false;
   4065 
   4066   if ((sym->flags & BSF_SECTION_SYM) == 0)
   4067     return false;
   4068 
   4069   /* Ignore the section symbol if it isn't used.  */
   4070   if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
   4071     return true;
   4072 
   4073   if (sym->section == NULL)
   4074     return true;
   4075 
   4076   type_ptr = elf_symbol_from (sym);
   4077   return ((type_ptr != NULL
   4078 	   && type_ptr->internal_elf_sym.st_shndx != 0
   4079 	   && bfd_is_abs_section (sym->section))
   4080 	  || !(sym->section->owner == abfd
   4081 	       || (sym->section->output_section != NULL
   4082 		   && sym->section->output_section->owner == abfd
   4083 		   && sym->section->output_offset == 0)
   4084 	       || bfd_is_abs_section (sym->section)));
   4085 }
   4086 
   4087 /* Map symbol from it's internal number to the external number, moving
   4088    all local symbols to be at the head of the list.  */
   4089 
   4090 static bool
   4091 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4092 {
   4093   unsigned int symcount = bfd_get_symcount (abfd);
   4094   asymbol **syms = bfd_get_outsymbols (abfd);
   4095   asymbol **sect_syms;
   4096   unsigned int num_locals = 0;
   4097   unsigned int num_globals = 0;
   4098   unsigned int num_locals2 = 0;
   4099   unsigned int num_globals2 = 0;
   4100   unsigned int max_index = 0;
   4101   unsigned int idx;
   4102   asection *asect;
   4103   asymbol **new_syms;
   4104   size_t amt;
   4105 
   4106 #ifdef DEBUG
   4107   fprintf (stderr, "elf_map_symbols\n");
   4108   fflush (stderr);
   4109 #endif
   4110 
   4111   for (asect = abfd->sections; asect; asect = asect->next)
   4112     {
   4113       if (max_index < asect->index)
   4114 	max_index = asect->index;
   4115     }
   4116 
   4117   max_index++;
   4118   amt = max_index * sizeof (asymbol *);
   4119   sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
   4120   if (sect_syms == NULL)
   4121     return false;
   4122   elf_section_syms (abfd) = sect_syms;
   4123   elf_num_section_syms (abfd) = max_index;
   4124 
   4125   /* Init sect_syms entries for any section symbols we have already
   4126      decided to output.  */
   4127   for (idx = 0; idx < symcount; idx++)
   4128     {
   4129       asymbol *sym = syms[idx];
   4130 
   4131       if ((sym->flags & BSF_SECTION_SYM) != 0
   4132 	  && sym->value == 0
   4133 	  && !ignore_section_sym (abfd, sym)
   4134 	  && !bfd_is_abs_section (sym->section))
   4135 	{
   4136 	  asection *sec = sym->section;
   4137 
   4138 	  if (sec->owner != abfd)
   4139 	    sec = sec->output_section;
   4140 
   4141 	  sect_syms[sec->index] = syms[idx];
   4142 	}
   4143     }
   4144 
   4145   /* Classify all of the symbols.  */
   4146   for (idx = 0; idx < symcount; idx++)
   4147     {
   4148       if (sym_is_global (abfd, syms[idx]))
   4149 	num_globals++;
   4150       else if (!ignore_section_sym (abfd, syms[idx]))
   4151 	num_locals++;
   4152     }
   4153 
   4154   /* We will be adding a section symbol for each normal BFD section.  Most
   4155      sections will already have a section symbol in outsymbols, but
   4156      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4157      at least in that case.  */
   4158   for (asect = abfd->sections; asect; asect = asect->next)
   4159     {
   4160       asymbol *sym = asect->symbol;
   4161       /* Don't include ignored section symbols.  */
   4162       if (!ignore_section_sym (abfd, sym)
   4163 	  && sect_syms[asect->index] == NULL)
   4164 	{
   4165 	  if (!sym_is_global (abfd, asect->symbol))
   4166 	    num_locals++;
   4167 	  else
   4168 	    num_globals++;
   4169 	}
   4170     }
   4171 
   4172   /* Now sort the symbols so the local symbols are first.  */
   4173   amt = (num_locals + num_globals) * sizeof (asymbol *);
   4174   new_syms = (asymbol **) bfd_alloc (abfd, amt);
   4175   if (new_syms == NULL)
   4176     return false;
   4177 
   4178   for (idx = 0; idx < symcount; idx++)
   4179     {
   4180       asymbol *sym = syms[idx];
   4181       unsigned int i;
   4182 
   4183       if (sym_is_global (abfd, sym))
   4184 	i = num_locals + num_globals2++;
   4185       /* Don't include ignored section symbols.  */
   4186       else if (!ignore_section_sym (abfd, sym))
   4187 	i = num_locals2++;
   4188       else
   4189 	continue;
   4190       new_syms[i] = sym;
   4191       sym->udata.i = i + 1;
   4192     }
   4193   for (asect = abfd->sections; asect; asect = asect->next)
   4194     {
   4195       asymbol *sym = asect->symbol;
   4196       if (!ignore_section_sym (abfd, sym)
   4197 	  && sect_syms[asect->index] == NULL)
   4198 	{
   4199 	  unsigned int i;
   4200 
   4201 	  sect_syms[asect->index] = sym;
   4202 	  if (!sym_is_global (abfd, sym))
   4203 	    i = num_locals2++;
   4204 	  else
   4205 	    i = num_locals + num_globals2++;
   4206 	  new_syms[i] = sym;
   4207 	  sym->udata.i = i + 1;
   4208 	}
   4209     }
   4210 
   4211   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4212 
   4213   *pnum_locals = num_locals;
   4214   return true;
   4215 }
   4216 
   4217 /* Align to the maximum file alignment that could be required for any
   4218    ELF data structure.  */
   4219 
   4220 static inline file_ptr
   4221 align_file_position (file_ptr off, int align)
   4222 {
   4223   return (off + align - 1) & ~(align - 1);
   4224 }
   4225 
   4226 /* Assign a file position to a section, optionally aligning to the
   4227    required section alignment.  */
   4228 
   4229 file_ptr
   4230 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4231 					   file_ptr offset,
   4232 					   bool align)
   4233 {
   4234   if (align && i_shdrp->sh_addralign > 1)
   4235     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
   4236   i_shdrp->sh_offset = offset;
   4237   if (i_shdrp->bfd_section != NULL)
   4238     i_shdrp->bfd_section->filepos = offset;
   4239   if (i_shdrp->sh_type != SHT_NOBITS)
   4240     offset += i_shdrp->sh_size;
   4241   return offset;
   4242 }
   4243 
   4244 /* Compute the file positions we are going to put the sections at, and
   4245    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4246    is not NULL, this is being called by the ELF backend linker.  */
   4247 
   4248 bool
   4249 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4250 					 struct bfd_link_info *link_info)
   4251 {
   4252   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4253   struct fake_section_arg fsargs;
   4254   bool failed;
   4255   struct elf_strtab_hash *strtab = NULL;
   4256   Elf_Internal_Shdr *shstrtab_hdr;
   4257   bool need_symtab;
   4258 
   4259   if (abfd->output_has_begun)
   4260     return true;
   4261 
   4262   /* Do any elf backend specific processing first.  */
   4263   if (bed->elf_backend_begin_write_processing)
   4264     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4265 
   4266   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4267     return false;
   4268 
   4269   fsargs.failed = false;
   4270   fsargs.link_info = link_info;
   4271   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4272   if (fsargs.failed)
   4273     return false;
   4274 
   4275   if (!assign_section_numbers (abfd, link_info))
   4276     return false;
   4277 
   4278   /* The backend linker builds symbol table information itself.  */
   4279   need_symtab = (link_info == NULL
   4280 		 && (bfd_get_symcount (abfd) > 0
   4281 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4282 			 == HAS_RELOC)));
   4283   if (need_symtab)
   4284     {
   4285       /* Non-zero if doing a relocatable link.  */
   4286       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4287 
   4288       if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
   4289 	return false;
   4290     }
   4291 
   4292   failed = false;
   4293   if (link_info == NULL)
   4294     {
   4295       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4296       if (failed)
   4297 	return false;
   4298     }
   4299 
   4300   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4301   /* sh_name was set in init_file_header.  */
   4302   shstrtab_hdr->sh_type = SHT_STRTAB;
   4303   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4304   shstrtab_hdr->sh_addr = 0;
   4305   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4306   shstrtab_hdr->sh_entsize = 0;
   4307   shstrtab_hdr->sh_link = 0;
   4308   shstrtab_hdr->sh_info = 0;
   4309   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4310   shstrtab_hdr->sh_addralign = 1;
   4311 
   4312   if (!assign_file_positions_except_relocs (abfd, link_info))
   4313     return false;
   4314 
   4315   if (need_symtab)
   4316     {
   4317       file_ptr off;
   4318       Elf_Internal_Shdr *hdr;
   4319 
   4320       off = elf_next_file_pos (abfd);
   4321 
   4322       hdr = & elf_symtab_hdr (abfd);
   4323       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4324 
   4325       if (elf_symtab_shndx_list (abfd) != NULL)
   4326 	{
   4327 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4328 	  if (hdr->sh_size != 0)
   4329 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4330 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4331 	}
   4332 
   4333       hdr = &elf_tdata (abfd)->strtab_hdr;
   4334       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4335 
   4336       elf_next_file_pos (abfd) = off;
   4337 
   4338       /* Now that we know where the .strtab section goes, write it
   4339 	 out.  */
   4340       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4341 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4342 	return false;
   4343       _bfd_elf_strtab_free (strtab);
   4344     }
   4345 
   4346   abfd->output_has_begun = true;
   4347 
   4348   return true;
   4349 }
   4350 
   4351 /* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
   4352    function effectively returns whether --eh-frame-hdr is given on the
   4353    command line.  After size_dynamic_sections the result reflects
   4354    whether .eh_frame_hdr will actually be output (sizing isn't done
   4355    until ldemul_after_allocation).  */
   4356 
   4357 static asection *
   4358 elf_eh_frame_hdr (const struct bfd_link_info *info)
   4359 {
   4360   if (info != NULL && is_elf_hash_table (info->hash))
   4361     return elf_hash_table (info)->eh_info.hdr_sec;
   4362   return NULL;
   4363 }
   4364 
   4365 /* Make an initial estimate of the size of the program header.  If we
   4366    get the number wrong here, we'll redo section placement.  */
   4367 
   4368 static bfd_size_type
   4369 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4370 {
   4371   size_t segs;
   4372   asection *s;
   4373   const struct elf_backend_data *bed;
   4374 
   4375   /* Assume we will need exactly two PT_LOAD segments: one for text
   4376      and one for data.  */
   4377   segs = 2;
   4378 
   4379   s = bfd_get_section_by_name (abfd, ".interp");
   4380   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4381     {
   4382       /* If we have a loadable interpreter section, we need a
   4383 	 PT_INTERP segment.  In this case, assume we also need a
   4384 	 PT_PHDR segment, although that may not be true for all
   4385 	 targets.  */
   4386       segs += 2;
   4387     }
   4388 
   4389   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
   4390     {
   4391       /* We need a PT_DYNAMIC segment.  */
   4392       ++segs;
   4393     }
   4394 
   4395   if (info != NULL && info->relro)
   4396     {
   4397       /* We need a PT_GNU_RELRO segment.  */
   4398       ++segs;
   4399     }
   4400 
   4401   if (elf_eh_frame_hdr (info))
   4402     {
   4403       /* We need a PT_GNU_EH_FRAME segment.  */
   4404       ++segs;
   4405     }
   4406 
   4407   if (elf_stack_flags (abfd))
   4408     {
   4409       /* We need a PT_GNU_STACK segment.  */
   4410       ++segs;
   4411     }
   4412 
   4413   s = bfd_get_section_by_name (abfd,
   4414 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4415   if (s != NULL && s->size != 0)
   4416     {
   4417       /* We need a PT_GNU_PROPERTY segment.  */
   4418       ++segs;
   4419     }
   4420 
   4421   for (s = abfd->sections; s != NULL; s = s->next)
   4422     {
   4423       if ((s->flags & SEC_LOAD) != 0
   4424 	  && elf_section_type (s) == SHT_NOTE)
   4425 	{
   4426 	  unsigned int alignment_power;
   4427 	  /* We need a PT_NOTE segment.  */
   4428 	  ++segs;
   4429 	  /* Try to create just one PT_NOTE segment for all adjacent
   4430 	     loadable SHT_NOTE sections.  gABI requires that within a
   4431 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4432 	     each note should have the same alignment.  So we check
   4433 	     whether the sections are correctly aligned.  */
   4434 	  alignment_power = s->alignment_power;
   4435 	  while (s->next != NULL
   4436 		 && s->next->alignment_power == alignment_power
   4437 		 && (s->next->flags & SEC_LOAD) != 0
   4438 		 && elf_section_type (s->next) == SHT_NOTE)
   4439 	    s = s->next;
   4440 	}
   4441     }
   4442 
   4443   for (s = abfd->sections; s != NULL; s = s->next)
   4444     {
   4445       if (s->flags & SEC_THREAD_LOCAL)
   4446 	{
   4447 	  /* We need a PT_TLS segment.  */
   4448 	  ++segs;
   4449 	  break;
   4450 	}
   4451     }
   4452 
   4453   bed = get_elf_backend_data (abfd);
   4454 
   4455   if ((abfd->flags & D_PAGED) != 0
   4456       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4457     {
   4458       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4459       bfd_vma commonpagesize;
   4460       unsigned int page_align_power;
   4461 
   4462       if (info != NULL)
   4463 	commonpagesize = info->commonpagesize;
   4464       else
   4465 	commonpagesize = bed->commonpagesize;
   4466       page_align_power = bfd_log2 (commonpagesize);
   4467       for (s = abfd->sections; s != NULL; s = s->next)
   4468 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   4469 	  {
   4470 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   4471 	      {
   4472 		_bfd_error_handler
   4473 		  /* xgettext:c-format */
   4474 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   4475 		     "sh_info field: %d"),
   4476 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4477 		continue;
   4478 	      }
   4479 	    /* Align mbind section to page size.  */
   4480 	    if (s->alignment_power < page_align_power)
   4481 	      s->alignment_power = page_align_power;
   4482 	    segs ++;
   4483 	  }
   4484     }
   4485 
   4486   /* Let the backend count up any program headers it might need.  */
   4487   if (bed->elf_backend_additional_program_headers)
   4488     {
   4489       int a;
   4490 
   4491       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4492       if (a == -1)
   4493 	abort ();
   4494       segs += a;
   4495     }
   4496 
   4497   return segs * bed->s->sizeof_phdr;
   4498 }
   4499 
   4500 /* Find the segment that contains the output_section of section.  */
   4501 
   4502 Elf_Internal_Phdr *
   4503 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4504 {
   4505   struct elf_segment_map *m;
   4506   Elf_Internal_Phdr *p;
   4507 
   4508   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4509        m != NULL;
   4510        m = m->next, p++)
   4511     {
   4512       int i;
   4513 
   4514       for (i = m->count - 1; i >= 0; i--)
   4515 	if (m->sections[i] == section)
   4516 	  return p;
   4517     }
   4518 
   4519   return NULL;
   4520 }
   4521 
   4522 /* Create a mapping from a set of sections to a program segment.  */
   4523 
   4524 static struct elf_segment_map *
   4525 make_mapping (bfd *abfd,
   4526 	      asection **sections,
   4527 	      unsigned int from,
   4528 	      unsigned int to,
   4529 	      bool phdr)
   4530 {
   4531   struct elf_segment_map *m;
   4532   unsigned int i;
   4533   asection **hdrpp;
   4534   size_t amt;
   4535 
   4536   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   4537   amt += (to - from) * sizeof (asection *);
   4538   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4539   if (m == NULL)
   4540     return NULL;
   4541   m->next = NULL;
   4542   m->p_type = PT_LOAD;
   4543   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4544     m->sections[i - from] = *hdrpp;
   4545   m->count = to - from;
   4546 
   4547   if (from == 0 && phdr)
   4548     {
   4549       /* Include the headers in the first PT_LOAD segment.  */
   4550       m->includes_filehdr = 1;
   4551       m->includes_phdrs = 1;
   4552     }
   4553 
   4554   return m;
   4555 }
   4556 
   4557 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   4558    on failure.  */
   4559 
   4560 struct elf_segment_map *
   4561 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   4562 {
   4563   struct elf_segment_map *m;
   4564 
   4565   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   4566 					     sizeof (struct elf_segment_map));
   4567   if (m == NULL)
   4568     return NULL;
   4569   m->next = NULL;
   4570   m->p_type = PT_DYNAMIC;
   4571   m->count = 1;
   4572   m->sections[0] = dynsec;
   4573 
   4574   return m;
   4575 }
   4576 
   4577 /* Possibly add or remove segments from the segment map.  */
   4578 
   4579 static bool
   4580 elf_modify_segment_map (bfd *abfd,
   4581 			struct bfd_link_info *info,
   4582 			bool remove_empty_load)
   4583 {
   4584   struct elf_segment_map **m;
   4585   const struct elf_backend_data *bed;
   4586 
   4587   /* The placement algorithm assumes that non allocated sections are
   4588      not in PT_LOAD segments.  We ensure this here by removing such
   4589      sections from the segment map.  We also remove excluded
   4590      sections.  Finally, any PT_LOAD segment without sections is
   4591      removed.  */
   4592   m = &elf_seg_map (abfd);
   4593   while (*m)
   4594     {
   4595       unsigned int i, new_count;
   4596 
   4597       for (new_count = 0, i = 0; i < (*m)->count; i++)
   4598 	{
   4599 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   4600 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   4601 		  || (*m)->p_type != PT_LOAD))
   4602 	    {
   4603 	      (*m)->sections[new_count] = (*m)->sections[i];
   4604 	      new_count++;
   4605 	    }
   4606 	}
   4607       (*m)->count = new_count;
   4608 
   4609       if (remove_empty_load
   4610 	  && (*m)->p_type == PT_LOAD
   4611 	  && (*m)->count == 0
   4612 	  && !(*m)->includes_phdrs)
   4613 	*m = (*m)->next;
   4614       else
   4615 	m = &(*m)->next;
   4616     }
   4617 
   4618   bed = get_elf_backend_data (abfd);
   4619   if (bed->elf_backend_modify_segment_map != NULL)
   4620     {
   4621       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   4622 	return false;
   4623     }
   4624 
   4625   return true;
   4626 }
   4627 
   4628 #define IS_TBSS(s) \
   4629   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   4630 
   4631 /* Set up a mapping from BFD sections to program segments.  Update
   4632    NEED_LAYOUT if the section layout is changed.  */
   4633 
   4634 bool
   4635 _bfd_elf_map_sections_to_segments (bfd *abfd,
   4636 				   struct bfd_link_info *info,
   4637 				   bool *need_layout)
   4638 {
   4639   unsigned int count;
   4640   struct elf_segment_map *m;
   4641   asection **sections = NULL;
   4642   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4643   bool no_user_phdrs;
   4644 
   4645   no_user_phdrs = elf_seg_map (abfd) == NULL;
   4646 
   4647   if (info != NULL)
   4648     {
   4649       info->user_phdrs = !no_user_phdrs;
   4650 
   4651       /* Size the relative relocations if DT_RELR is enabled.  */
   4652       if (info->enable_dt_relr
   4653 	  && need_layout != NULL
   4654 	  && bed->size_relative_relocs
   4655 	  && !bed->size_relative_relocs (info, need_layout))
   4656 	info->callbacks->einfo
   4657 	  (_("%F%P: failed to size relative relocations\n"));
   4658     }
   4659 
   4660   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   4661     {
   4662       asection *s;
   4663       unsigned int i;
   4664       struct elf_segment_map *mfirst;
   4665       struct elf_segment_map **pm;
   4666       asection *last_hdr;
   4667       bfd_vma last_size;
   4668       unsigned int hdr_index;
   4669       bfd_vma maxpagesize;
   4670       asection **hdrpp;
   4671       bool phdr_in_segment;
   4672       bool writable;
   4673       bool executable;
   4674       unsigned int tls_count = 0;
   4675       asection *first_tls = NULL;
   4676       asection *first_mbind = NULL;
   4677       asection *dynsec, *eh_frame_hdr;
   4678       size_t amt;
   4679       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   4680       bfd_size_type phdr_size;  /* Octets/bytes.  */
   4681       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   4682 
   4683       /* Select the allocated sections, and sort them.  */
   4684 
   4685       amt = bfd_count_sections (abfd) * sizeof (asection *);
   4686       sections = (asection **) bfd_malloc (amt);
   4687       if (sections == NULL)
   4688 	goto error_return;
   4689 
   4690       /* Calculate top address, avoiding undefined behaviour of shift
   4691 	 left operator when shift count is equal to size of type
   4692 	 being shifted.  */
   4693       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   4694       addr_mask = (addr_mask << 1) + 1;
   4695 
   4696       i = 0;
   4697       for (s = abfd->sections; s != NULL; s = s->next)
   4698 	{
   4699 	  if ((s->flags & SEC_ALLOC) != 0)
   4700 	    {
   4701 	      /* target_index is unused until bfd_elf_final_link
   4702 		 starts output of section symbols.  Use it to make
   4703 		 qsort stable.  */
   4704 	      s->target_index = i;
   4705 	      sections[i] = s;
   4706 	      ++i;
   4707 	      /* A wrapping section potentially clashes with header.  */
   4708 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   4709 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   4710 	    }
   4711 	}
   4712       BFD_ASSERT (i <= bfd_count_sections (abfd));
   4713       count = i;
   4714 
   4715       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   4716 
   4717       phdr_size = elf_program_header_size (abfd);
   4718       if (phdr_size == (bfd_size_type) -1)
   4719 	phdr_size = get_program_header_size (abfd, info);
   4720       phdr_size += bed->s->sizeof_ehdr;
   4721       /* phdr_size is compared to LMA values which are in bytes.  */
   4722       phdr_size /= opb;
   4723       if (info != NULL)
   4724 	maxpagesize = info->maxpagesize;
   4725       else
   4726 	maxpagesize = bed->maxpagesize;
   4727       if (maxpagesize == 0)
   4728 	maxpagesize = 1;
   4729       phdr_in_segment = info != NULL && info->load_phdrs;
   4730       if (count != 0
   4731 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   4732 	      >= (phdr_size & (maxpagesize - 1))))
   4733 	/* For compatibility with old scripts that may not be using
   4734 	   SIZEOF_HEADERS, add headers when it looks like space has
   4735 	   been left for them.  */
   4736 	phdr_in_segment = true;
   4737 
   4738       /* Build the mapping.  */
   4739       mfirst = NULL;
   4740       pm = &mfirst;
   4741 
   4742       /* If we have a .interp section, then create a PT_PHDR segment for
   4743 	 the program headers and a PT_INTERP segment for the .interp
   4744 	 section.  */
   4745       s = bfd_get_section_by_name (abfd, ".interp");
   4746       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4747 	{
   4748 	  amt = sizeof (struct elf_segment_map);
   4749 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4750 	  if (m == NULL)
   4751 	    goto error_return;
   4752 	  m->next = NULL;
   4753 	  m->p_type = PT_PHDR;
   4754 	  m->p_flags = PF_R;
   4755 	  m->p_flags_valid = 1;
   4756 	  m->includes_phdrs = 1;
   4757 	  phdr_in_segment = true;
   4758 	  *pm = m;
   4759 	  pm = &m->next;
   4760 
   4761 	  amt = sizeof (struct elf_segment_map);
   4762 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4763 	  if (m == NULL)
   4764 	    goto error_return;
   4765 	  m->next = NULL;
   4766 	  m->p_type = PT_INTERP;
   4767 	  m->count = 1;
   4768 	  m->sections[0] = s;
   4769 
   4770 	  *pm = m;
   4771 	  pm = &m->next;
   4772 	}
   4773 
   4774       /* Look through the sections.  We put sections in the same program
   4775 	 segment when the start of the second section can be placed within
   4776 	 a few bytes of the end of the first section.  */
   4777       last_hdr = NULL;
   4778       last_size = 0;
   4779       hdr_index = 0;
   4780       writable = false;
   4781       executable = false;
   4782       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   4783       if (dynsec != NULL
   4784 	  && (dynsec->flags & SEC_LOAD) == 0)
   4785 	dynsec = NULL;
   4786 
   4787       if ((abfd->flags & D_PAGED) == 0)
   4788 	phdr_in_segment = false;
   4789 
   4790       /* Deal with -Ttext or something similar such that the first section
   4791 	 is not adjacent to the program headers.  This is an
   4792 	 approximation, since at this point we don't know exactly how many
   4793 	 program headers we will need.  */
   4794       if (phdr_in_segment && count > 0)
   4795 	{
   4796 	  bfd_vma phdr_lma;  /* Bytes.  */
   4797 	  bool separate_phdr = false;
   4798 
   4799 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   4800 	  if (info != NULL
   4801 	      && info->separate_code
   4802 	      && (sections[0]->flags & SEC_CODE) != 0)
   4803 	    {
   4804 	      /* If data sections should be separate from code and
   4805 		 thus not executable, and the first section is
   4806 		 executable then put the file and program headers in
   4807 		 their own PT_LOAD.  */
   4808 	      separate_phdr = true;
   4809 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   4810 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   4811 		{
   4812 		  /* The file and program headers are currently on the
   4813 		     same page as the first section.  Put them on the
   4814 		     previous page if we can.  */
   4815 		  if (phdr_lma >= maxpagesize)
   4816 		    phdr_lma -= maxpagesize;
   4817 		  else
   4818 		    separate_phdr = false;
   4819 		}
   4820 	    }
   4821 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   4822 	      || (sections[0]->lma & addr_mask) < phdr_size)
   4823 	    /* If file and program headers would be placed at the end
   4824 	       of memory then it's probably better to omit them.  */
   4825 	    phdr_in_segment = false;
   4826 	  else if (phdr_lma < wrap_to)
   4827 	    /* If a section wraps around to where we'll be placing
   4828 	       file and program headers, then the headers will be
   4829 	       overwritten.  */
   4830 	    phdr_in_segment = false;
   4831 	  else if (separate_phdr)
   4832 	    {
   4833 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   4834 	      if (m == NULL)
   4835 		goto error_return;
   4836 	      m->p_paddr = phdr_lma * opb;
   4837 	      m->p_vaddr_offset
   4838 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   4839 	      m->p_paddr_valid = 1;
   4840 	      *pm = m;
   4841 	      pm = &m->next;
   4842 	      phdr_in_segment = false;
   4843 	    }
   4844 	}
   4845 
   4846       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   4847 	{
   4848 	  asection *hdr;
   4849 	  bool new_segment;
   4850 
   4851 	  hdr = *hdrpp;
   4852 
   4853 	  /* See if this section and the last one will fit in the same
   4854 	     segment.  */
   4855 
   4856 	  if (last_hdr == NULL)
   4857 	    {
   4858 	      /* If we don't have a segment yet, then we don't need a new
   4859 		 one (we build the last one after this loop).  */
   4860 	      new_segment = false;
   4861 	    }
   4862 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   4863 	    {
   4864 	      /* If this section has a different relation between the
   4865 		 virtual address and the load address, then we need a new
   4866 		 segment.  */
   4867 	      new_segment = true;
   4868 	    }
   4869 	  else if (hdr->lma < last_hdr->lma + last_size
   4870 		   || last_hdr->lma + last_size < last_hdr->lma)
   4871 	    {
   4872 	      /* If this section has a load address that makes it overlap
   4873 		 the previous section, then we need a new segment.  */
   4874 	      new_segment = true;
   4875 	    }
   4876 	  else if ((abfd->flags & D_PAGED) != 0
   4877 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   4878 		       == (hdr->lma & -maxpagesize)))
   4879 	    {
   4880 	      /* If we are demand paged then we can't map two disk
   4881 		 pages onto the same memory page.  */
   4882 	      new_segment = false;
   4883 	    }
   4884 	  /* In the next test we have to be careful when last_hdr->lma is close
   4885 	     to the end of the address space.  If the aligned address wraps
   4886 	     around to the start of the address space, then there are no more
   4887 	     pages left in memory and it is OK to assume that the current
   4888 	     section can be included in the current segment.  */
   4889 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4890 		    + maxpagesize > last_hdr->lma)
   4891 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4892 		       + maxpagesize <= hdr->lma))
   4893 	    {
   4894 	      /* If putting this section in this segment would force us to
   4895 		 skip a page in the segment, then we need a new segment.  */
   4896 	      new_segment = true;
   4897 	    }
   4898 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   4899 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   4900 	    {
   4901 	      /* We don't want to put a loaded section after a
   4902 		 nonloaded (ie. bss style) section in the same segment
   4903 		 as that will force the non-loaded section to be loaded.
   4904 		 Consider .tbss sections as loaded for this purpose.  */
   4905 	      new_segment = true;
   4906 	    }
   4907 	  else if ((abfd->flags & D_PAGED) == 0)
   4908 	    {
   4909 	      /* If the file is not demand paged, which means that we
   4910 		 don't require the sections to be correctly aligned in the
   4911 		 file, then there is no other reason for a new segment.  */
   4912 	      new_segment = false;
   4913 	    }
   4914 	  else if (info != NULL
   4915 		   && info->separate_code
   4916 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   4917 	    {
   4918 	      new_segment = true;
   4919 	    }
   4920 	  else if (! writable
   4921 		   && (hdr->flags & SEC_READONLY) == 0)
   4922 	    {
   4923 	      /* We don't want to put a writable section in a read only
   4924 		 segment.  */
   4925 	      new_segment = true;
   4926 	    }
   4927 	  else
   4928 	    {
   4929 	      /* Otherwise, we can use the same segment.  */
   4930 	      new_segment = false;
   4931 	    }
   4932 
   4933 	  /* Allow interested parties a chance to override our decision.  */
   4934 	  if (last_hdr != NULL
   4935 	      && info != NULL
   4936 	      && info->callbacks->override_segment_assignment != NULL)
   4937 	    new_segment
   4938 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   4939 							      last_hdr,
   4940 							      new_segment);
   4941 
   4942 	  if (! new_segment)
   4943 	    {
   4944 	      if ((hdr->flags & SEC_READONLY) == 0)
   4945 		writable = true;
   4946 	      if ((hdr->flags & SEC_CODE) != 0)
   4947 		executable = true;
   4948 	      last_hdr = hdr;
   4949 	      /* .tbss sections effectively have zero size.  */
   4950 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   4951 	      continue;
   4952 	    }
   4953 
   4954 	  /* We need a new program segment.  We must create a new program
   4955 	     header holding all the sections from hdr_index until hdr.  */
   4956 
   4957 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   4958 	  if (m == NULL)
   4959 	    goto error_return;
   4960 
   4961 	  *pm = m;
   4962 	  pm = &m->next;
   4963 
   4964 	  if ((hdr->flags & SEC_READONLY) == 0)
   4965 	    writable = true;
   4966 	  else
   4967 	    writable = false;
   4968 
   4969 	  if ((hdr->flags & SEC_CODE) == 0)
   4970 	    executable = false;
   4971 	  else
   4972 	    executable = true;
   4973 
   4974 	  last_hdr = hdr;
   4975 	  /* .tbss sections effectively have zero size.  */
   4976 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   4977 	  hdr_index = i;
   4978 	  phdr_in_segment = false;
   4979 	}
   4980 
   4981       /* Create a final PT_LOAD program segment, but not if it's just
   4982 	 for .tbss.  */
   4983       if (last_hdr != NULL
   4984 	  && (i - hdr_index != 1
   4985 	      || !IS_TBSS (last_hdr)))
   4986 	{
   4987 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   4988 	  if (m == NULL)
   4989 	    goto error_return;
   4990 
   4991 	  *pm = m;
   4992 	  pm = &m->next;
   4993 	}
   4994 
   4995       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   4996       if (dynsec != NULL)
   4997 	{
   4998 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   4999 	  if (m == NULL)
   5000 	    goto error_return;
   5001 	  *pm = m;
   5002 	  pm = &m->next;
   5003 	}
   5004 
   5005       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5006 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5007 	 because if we link together nonloadable .note sections and
   5008 	 loadable .note sections, we will generate two .note sections
   5009 	 in the output file.  */
   5010       for (s = abfd->sections; s != NULL; s = s->next)
   5011 	{
   5012 	  if ((s->flags & SEC_LOAD) != 0
   5013 	      && elf_section_type (s) == SHT_NOTE)
   5014 	    {
   5015 	      asection *s2;
   5016 	      unsigned int alignment_power = s->alignment_power;
   5017 
   5018 	      count = 1;
   5019 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5020 		{
   5021 		  if (s2->next->alignment_power == alignment_power
   5022 		      && (s2->next->flags & SEC_LOAD) != 0
   5023 		      && elf_section_type (s2->next) == SHT_NOTE
   5024 		      && align_power (s2->lma + s2->size / opb,
   5025 				      alignment_power)
   5026 		      == s2->next->lma)
   5027 		    count++;
   5028 		  else
   5029 		    break;
   5030 		}
   5031 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5032 	      amt += count * sizeof (asection *);
   5033 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5034 	      if (m == NULL)
   5035 		goto error_return;
   5036 	      m->next = NULL;
   5037 	      m->p_type = PT_NOTE;
   5038 	      m->count = count;
   5039 	      while (count > 1)
   5040 		{
   5041 		  m->sections[m->count - count--] = s;
   5042 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5043 		  s = s->next;
   5044 		}
   5045 	      m->sections[m->count - 1] = s;
   5046 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5047 	      *pm = m;
   5048 	      pm = &m->next;
   5049 	    }
   5050 	  if (s->flags & SEC_THREAD_LOCAL)
   5051 	    {
   5052 	      if (! tls_count)
   5053 		first_tls = s;
   5054 	      tls_count++;
   5055 	    }
   5056 	  if (first_mbind == NULL
   5057 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5058 	    first_mbind = s;
   5059 	}
   5060 
   5061       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5062       if (tls_count > 0)
   5063 	{
   5064 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5065 	  amt += tls_count * sizeof (asection *);
   5066 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5067 	  if (m == NULL)
   5068 	    goto error_return;
   5069 	  m->next = NULL;
   5070 	  m->p_type = PT_TLS;
   5071 	  m->count = tls_count;
   5072 	  /* Mandated PF_R.  */
   5073 	  m->p_flags = PF_R;
   5074 	  m->p_flags_valid = 1;
   5075 	  s = first_tls;
   5076 	  for (i = 0; i < tls_count; ++i)
   5077 	    {
   5078 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5079 		{
   5080 		  _bfd_error_handler
   5081 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5082 		  s = first_tls;
   5083 		  i = 0;
   5084 		  while (i < tls_count)
   5085 		    {
   5086 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5087 			{
   5088 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5089 			  i++;
   5090 			}
   5091 		      else
   5092 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5093 		      s = s->next;
   5094 		    }
   5095 		  bfd_set_error (bfd_error_bad_value);
   5096 		  goto error_return;
   5097 		}
   5098 	      m->sections[i] = s;
   5099 	      s = s->next;
   5100 	    }
   5101 
   5102 	  *pm = m;
   5103 	  pm = &m->next;
   5104 	}
   5105 
   5106       if (first_mbind
   5107 	  && (abfd->flags & D_PAGED) != 0
   5108 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5109 	for (s = first_mbind; s != NULL; s = s->next)
   5110 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5111 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5112 	    {
   5113 	      /* Mandated PF_R.  */
   5114 	      unsigned long p_flags = PF_R;
   5115 	      if ((s->flags & SEC_READONLY) == 0)
   5116 		p_flags |= PF_W;
   5117 	      if ((s->flags & SEC_CODE) != 0)
   5118 		p_flags |= PF_X;
   5119 
   5120 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5121 	      m = bfd_zalloc (abfd, amt);
   5122 	      if (m == NULL)
   5123 		goto error_return;
   5124 	      m->next = NULL;
   5125 	      m->p_type = (PT_GNU_MBIND_LO
   5126 			   + elf_section_data (s)->this_hdr.sh_info);
   5127 	      m->count = 1;
   5128 	      m->p_flags_valid = 1;
   5129 	      m->sections[0] = s;
   5130 	      m->p_flags = p_flags;
   5131 
   5132 	      *pm = m;
   5133 	      pm = &m->next;
   5134 	    }
   5135 
   5136       s = bfd_get_section_by_name (abfd,
   5137 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5138       if (s != NULL && s->size != 0)
   5139 	{
   5140 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5141 	  m = bfd_zalloc (abfd, amt);
   5142 	  if (m == NULL)
   5143 	    goto error_return;
   5144 	  m->next = NULL;
   5145 	  m->p_type = PT_GNU_PROPERTY;
   5146 	  m->count = 1;
   5147 	  m->p_flags_valid = 1;
   5148 	  m->sections[0] = s;
   5149 	  m->p_flags = PF_R;
   5150 	  *pm = m;
   5151 	  pm = &m->next;
   5152 	}
   5153 
   5154       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5155 	 segment.  */
   5156       eh_frame_hdr = elf_eh_frame_hdr (info);
   5157       if (eh_frame_hdr != NULL
   5158 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5159 	{
   5160 	  amt = sizeof (struct elf_segment_map);
   5161 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5162 	  if (m == NULL)
   5163 	    goto error_return;
   5164 	  m->next = NULL;
   5165 	  m->p_type = PT_GNU_EH_FRAME;
   5166 	  m->count = 1;
   5167 	  m->sections[0] = eh_frame_hdr->output_section;
   5168 
   5169 	  *pm = m;
   5170 	  pm = &m->next;
   5171 	}
   5172 
   5173       if (elf_stack_flags (abfd))
   5174 	{
   5175 	  amt = sizeof (struct elf_segment_map);
   5176 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5177 	  if (m == NULL)
   5178 	    goto error_return;
   5179 	  m->next = NULL;
   5180 	  m->p_type = PT_GNU_STACK;
   5181 	  m->p_flags = elf_stack_flags (abfd);
   5182 	  m->p_align = bed->stack_align;
   5183 	  m->p_flags_valid = 1;
   5184 	  m->p_align_valid = m->p_align != 0;
   5185 	  if (info->stacksize > 0)
   5186 	    {
   5187 	      m->p_size = info->stacksize;
   5188 	      m->p_size_valid = 1;
   5189 	    }
   5190 
   5191 	  *pm = m;
   5192 	  pm = &m->next;
   5193 	}
   5194 
   5195       if (info != NULL && info->relro)
   5196 	{
   5197 	  for (m = mfirst; m != NULL; m = m->next)
   5198 	    {
   5199 	      if (m->p_type == PT_LOAD
   5200 		  && m->count != 0
   5201 		  && m->sections[0]->vma >= info->relro_start
   5202 		  && m->sections[0]->vma < info->relro_end)
   5203 		{
   5204 		  i = m->count;
   5205 		  while (--i != (unsigned) -1)
   5206 		    {
   5207 		      if (m->sections[i]->size > 0
   5208 			  && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
   5209 			  == (SEC_LOAD | SEC_HAS_CONTENTS))
   5210 			break;
   5211 		    }
   5212 
   5213 		  if (i != (unsigned) -1)
   5214 		    break;
   5215 		}
   5216 	    }
   5217 
   5218 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5219 	  if (m != NULL)
   5220 	    {
   5221 	      amt = sizeof (struct elf_segment_map);
   5222 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5223 	      if (m == NULL)
   5224 		goto error_return;
   5225 	      m->next = NULL;
   5226 	      m->p_type = PT_GNU_RELRO;
   5227 	      *pm = m;
   5228 	      pm = &m->next;
   5229 	    }
   5230 	}
   5231 
   5232       free (sections);
   5233       elf_seg_map (abfd) = mfirst;
   5234     }
   5235 
   5236   if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
   5237     return false;
   5238 
   5239   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5240     ++count;
   5241   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5242 
   5243   return true;
   5244 
   5245  error_return:
   5246   free (sections);
   5247   return false;
   5248 }
   5249 
   5250 /* Sort sections by address.  */
   5251 
   5252 static int
   5253 elf_sort_sections (const void *arg1, const void *arg2)
   5254 {
   5255   const asection *sec1 = *(const asection **) arg1;
   5256   const asection *sec2 = *(const asection **) arg2;
   5257   bfd_size_type size1, size2;
   5258 
   5259   /* Sort by LMA first, since this is the address used to
   5260      place the section into a segment.  */
   5261   if (sec1->lma < sec2->lma)
   5262     return -1;
   5263   else if (sec1->lma > sec2->lma)
   5264     return 1;
   5265 
   5266   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5267      the same, and this will do nothing.  */
   5268   if (sec1->vma < sec2->vma)
   5269     return -1;
   5270   else if (sec1->vma > sec2->vma)
   5271     return 1;
   5272 
   5273   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5274 
   5275 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5276 		  && (x)->size != 0)
   5277 
   5278   if (TOEND (sec1))
   5279     {
   5280       if (!TOEND (sec2))
   5281 	return 1;
   5282     }
   5283   else if (TOEND (sec2))
   5284     return -1;
   5285 
   5286 #undef TOEND
   5287 
   5288   /* Sort by size, to put zero sized sections
   5289      before others at the same address.  */
   5290 
   5291   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5292   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5293 
   5294   if (size1 < size2)
   5295     return -1;
   5296   if (size1 > size2)
   5297     return 1;
   5298 
   5299   return sec1->target_index - sec2->target_index;
   5300 }
   5301 
   5302 /* This qsort comparison functions sorts PT_LOAD segments first and
   5303    by p_paddr, for assign_file_positions_for_load_sections.  */
   5304 
   5305 static int
   5306 elf_sort_segments (const void *arg1, const void *arg2)
   5307 {
   5308   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5309   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5310 
   5311   if (m1->p_type != m2->p_type)
   5312     {
   5313       if (m1->p_type == PT_NULL)
   5314 	return 1;
   5315       if (m2->p_type == PT_NULL)
   5316 	return -1;
   5317       return m1->p_type < m2->p_type ? -1 : 1;
   5318     }
   5319   if (m1->includes_filehdr != m2->includes_filehdr)
   5320     return m1->includes_filehdr ? -1 : 1;
   5321   if (m1->no_sort_lma != m2->no_sort_lma)
   5322     return m1->no_sort_lma ? -1 : 1;
   5323   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5324     {
   5325       bfd_vma lma1, lma2;  /* Octets.  */
   5326       lma1 = 0;
   5327       if (m1->p_paddr_valid)
   5328 	lma1 = m1->p_paddr;
   5329       else if (m1->count != 0)
   5330 	{
   5331 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5332 						  m1->sections[0]);
   5333 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5334 	}
   5335       lma2 = 0;
   5336       if (m2->p_paddr_valid)
   5337 	lma2 = m2->p_paddr;
   5338       else if (m2->count != 0)
   5339 	{
   5340 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5341 						  m2->sections[0]);
   5342 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5343 	}
   5344       if (lma1 != lma2)
   5345 	return lma1 < lma2 ? -1 : 1;
   5346     }
   5347   if (m1->idx != m2->idx)
   5348     return m1->idx < m2->idx ? -1 : 1;
   5349   return 0;
   5350 }
   5351 
   5352 /* Ian Lance Taylor writes:
   5353 
   5354    We shouldn't be using % with a negative signed number.  That's just
   5355    not good.  We have to make sure either that the number is not
   5356    negative, or that the number has an unsigned type.  When the types
   5357    are all the same size they wind up as unsigned.  When file_ptr is a
   5358    larger signed type, the arithmetic winds up as signed long long,
   5359    which is wrong.
   5360 
   5361    What we're trying to say here is something like ``increase OFF by
   5362    the least amount that will cause it to be equal to the VMA modulo
   5363    the page size.''  */
   5364 /* In other words, something like:
   5365 
   5366    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5367    off_offset = off % bed->maxpagesize;
   5368    if (vma_offset < off_offset)
   5369      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5370    else
   5371      adjustment = vma_offset - off_offset;
   5372 
   5373    which can be collapsed into the expression below.  */
   5374 
   5375 static file_ptr
   5376 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5377 {
   5378   /* PR binutils/16199: Handle an alignment of zero.  */
   5379   if (maxpagesize == 0)
   5380     maxpagesize = 1;
   5381   return ((vma - off) % maxpagesize);
   5382 }
   5383 
   5384 static void
   5385 print_segment_map (const struct elf_segment_map *m)
   5386 {
   5387   unsigned int j;
   5388   const char *pt = get_segment_type (m->p_type);
   5389   char buf[32];
   5390 
   5391   if (pt == NULL)
   5392     {
   5393       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5394 	sprintf (buf, "LOPROC+%7.7x",
   5395 		 (unsigned int) (m->p_type - PT_LOPROC));
   5396       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5397 	sprintf (buf, "LOOS+%7.7x",
   5398 		 (unsigned int) (m->p_type - PT_LOOS));
   5399       else
   5400 	snprintf (buf, sizeof (buf), "%8.8x",
   5401 		  (unsigned int) m->p_type);
   5402       pt = buf;
   5403     }
   5404   fflush (stdout);
   5405   fprintf (stderr, "%s:", pt);
   5406   for (j = 0; j < m->count; j++)
   5407     fprintf (stderr, " %s", m->sections [j]->name);
   5408   putc ('\n',stderr);
   5409   fflush (stderr);
   5410 }
   5411 
   5412 static bool
   5413 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
   5414 {
   5415   void *buf;
   5416   bool ret;
   5417 
   5418   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   5419     return false;
   5420   buf = bfd_zmalloc (len);
   5421   if (buf == NULL)
   5422     return false;
   5423   ret = bfd_bwrite (buf, len, abfd) == len;
   5424   free (buf);
   5425   return ret;
   5426 }
   5427 
   5428 /* Assign file positions to the sections based on the mapping from
   5429    sections to segments.  This function also sets up some fields in
   5430    the file header.  */
   5431 
   5432 static bool
   5433 assign_file_positions_for_load_sections (bfd *abfd,
   5434 					 struct bfd_link_info *link_info)
   5435 {
   5436   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5437   struct elf_segment_map *m;
   5438   struct elf_segment_map *phdr_load_seg;
   5439   Elf_Internal_Phdr *phdrs;
   5440   Elf_Internal_Phdr *p;
   5441   file_ptr off;  /* Octets.  */
   5442   bfd_size_type maxpagesize;
   5443   bfd_size_type p_align;
   5444   bool p_align_p = false;
   5445   unsigned int alloc, actual;
   5446   unsigned int i, j;
   5447   struct elf_segment_map **sorted_seg_map;
   5448   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5449 
   5450   if (link_info == NULL
   5451       && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5452     return false;
   5453 
   5454   alloc = 0;
   5455   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5456     m->idx = alloc++;
   5457 
   5458   if (alloc)
   5459     {
   5460       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5461       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5462     }
   5463   else
   5464     {
   5465       /* PR binutils/12467.  */
   5466       elf_elfheader (abfd)->e_phoff = 0;
   5467       elf_elfheader (abfd)->e_phentsize = 0;
   5468     }
   5469 
   5470   elf_elfheader (abfd)->e_phnum = alloc;
   5471 
   5472   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5473     {
   5474       actual = alloc;
   5475       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5476     }
   5477   else
   5478     {
   5479       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   5480       BFD_ASSERT (elf_program_header_size (abfd)
   5481 		  == actual * bed->s->sizeof_phdr);
   5482       BFD_ASSERT (actual >= alloc);
   5483     }
   5484 
   5485   if (alloc == 0)
   5486     {
   5487       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5488       return true;
   5489     }
   5490 
   5491   /* We're writing the size in elf_program_header_size (abfd),
   5492      see assign_file_positions_except_relocs, so make sure we have
   5493      that amount allocated, with trailing space cleared.
   5494      The variable alloc contains the computed need, while
   5495      elf_program_header_size (abfd) contains the size used for the
   5496      layout.
   5497      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5498      where the layout is forced to according to a larger size in the
   5499      last iterations for the testcase ld-elf/header.  */
   5500   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   5501 			     + alloc * sizeof (*sorted_seg_map)));
   5502   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   5503   elf_tdata (abfd)->phdr = phdrs;
   5504   if (phdrs == NULL)
   5505     return false;
   5506 
   5507   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   5508     {
   5509       sorted_seg_map[j] = m;
   5510       /* If elf_segment_map is not from map_sections_to_segments, the
   5511 	 sections may not be correctly ordered.  NOTE: sorting should
   5512 	 not be done to the PT_NOTE section of a corefile, which may
   5513 	 contain several pseudo-sections artificially created by bfd.
   5514 	 Sorting these pseudo-sections breaks things badly.  */
   5515       if (m->count > 1
   5516 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5517 	       && m->p_type == PT_NOTE))
   5518 	{
   5519 	  for (i = 0; i < m->count; i++)
   5520 	    m->sections[i]->target_index = i;
   5521 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5522 		 elf_sort_sections);
   5523 	}
   5524     }
   5525   if (alloc > 1)
   5526     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   5527 	   elf_sort_segments);
   5528 
   5529   p_align = bed->p_align;
   5530   maxpagesize = 1;
   5531   if ((abfd->flags & D_PAGED) != 0)
   5532     {
   5533       if (link_info != NULL)
   5534 	maxpagesize = link_info->maxpagesize;
   5535       else
   5536 	maxpagesize = bed->maxpagesize;
   5537     }
   5538 
   5539   /* Sections must map to file offsets past the ELF file header.  */
   5540   off = bed->s->sizeof_ehdr;
   5541   /* And if one of the PT_LOAD headers doesn't include the program
   5542      headers then we'll be mapping program headers in the usual
   5543      position after the ELF file header.  */
   5544   phdr_load_seg = NULL;
   5545   for (j = 0; j < alloc; j++)
   5546     {
   5547       m = sorted_seg_map[j];
   5548       if (m->p_type != PT_LOAD)
   5549 	break;
   5550       if (m->includes_phdrs)
   5551 	{
   5552 	  phdr_load_seg = m;
   5553 	  break;
   5554 	}
   5555     }
   5556   if (phdr_load_seg == NULL)
   5557     off += actual * bed->s->sizeof_phdr;
   5558 
   5559   for (j = 0; j < alloc; j++)
   5560     {
   5561       asection **secpp;
   5562       bfd_vma off_adjust;  /* Octets.  */
   5563       bool no_contents;
   5564 
   5565       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   5566 	 number of sections with contents contributing to both p_filesz
   5567 	 and p_memsz, followed by a number of sections with no contents
   5568 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   5569 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   5570       m = sorted_seg_map[j];
   5571       p = phdrs + m->idx;
   5572       p->p_type = m->p_type;
   5573       p->p_flags = m->p_flags;
   5574 
   5575       if (m->count == 0)
   5576 	p->p_vaddr = m->p_vaddr_offset * opb;
   5577       else
   5578 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   5579 
   5580       if (m->p_paddr_valid)
   5581 	p->p_paddr = m->p_paddr;
   5582       else if (m->count == 0)
   5583 	p->p_paddr = 0;
   5584       else
   5585 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   5586 
   5587       if (p->p_type == PT_LOAD
   5588 	  && (abfd->flags & D_PAGED) != 0)
   5589 	{
   5590 	  /* p_align in demand paged PT_LOAD segments effectively stores
   5591 	     the maximum page size.  When copying an executable with
   5592 	     objcopy, we set m->p_align from the input file.  Use this
   5593 	     value for maxpagesize rather than bed->maxpagesize, which
   5594 	     may be different.  Note that we use maxpagesize for PT_TLS
   5595 	     segment alignment later in this function, so we are relying
   5596 	     on at least one PT_LOAD segment appearing before a PT_TLS
   5597 	     segment.  */
   5598 	  if (m->p_align_valid)
   5599 	    maxpagesize = m->p_align;
   5600 	  else if (p_align != 0
   5601 		   && (link_info == NULL
   5602 		       || !link_info->maxpagesize_is_set))
   5603 	    /* Set p_align to the default p_align value while laying
   5604 	       out segments aligning to the maximum page size or the
   5605 	       largest section alignment.  The run-time loader can
   5606 	       align segments to the default p_align value or the
   5607 	       maximum page size, depending on system page size.  */
   5608 	    p_align_p = true;
   5609 
   5610 	  p->p_align = maxpagesize;
   5611 	}
   5612       else if (m->p_align_valid)
   5613 	p->p_align = m->p_align;
   5614       else if (m->count == 0)
   5615 	p->p_align = 1 << bed->s->log_file_align;
   5616 
   5617       if (m == phdr_load_seg)
   5618 	{
   5619 	  if (!m->includes_filehdr)
   5620 	    p->p_offset = off;
   5621 	  off += actual * bed->s->sizeof_phdr;
   5622 	}
   5623 
   5624       no_contents = false;
   5625       off_adjust = 0;
   5626       if (p->p_type == PT_LOAD
   5627 	  && m->count > 0)
   5628 	{
   5629 	  bfd_size_type align;  /* Bytes.  */
   5630 	  unsigned int align_power = 0;
   5631 
   5632 	  if (m->p_align_valid)
   5633 	    align = p->p_align;
   5634 	  else
   5635 	    {
   5636 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5637 		{
   5638 		  unsigned int secalign;
   5639 
   5640 		  secalign = bfd_section_alignment (*secpp);
   5641 		  if (secalign > align_power)
   5642 		    align_power = secalign;
   5643 		}
   5644 	      align = (bfd_size_type) 1 << align_power;
   5645 	      if (align < maxpagesize)
   5646 		{
   5647 		  /* If a section requires alignment higher than the
   5648 		     default p_align value, don't set p_align to the
   5649 		     default p_align value.  */
   5650 		  if (align > p_align)
   5651 		    p_align_p = false;
   5652 		  align = maxpagesize;
   5653 		}
   5654 	      else
   5655 		{
   5656 		  /* If a section requires alignment higher than the
   5657 		     maximum page size, set p_align to the section
   5658 		     alignment.  */
   5659 		  p_align_p = true;
   5660 		  p_align = align;
   5661 		}
   5662 	    }
   5663 
   5664 	  for (i = 0; i < m->count; i++)
   5665 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   5666 	      /* If we aren't making room for this section, then
   5667 		 it must be SHT_NOBITS regardless of what we've
   5668 		 set via struct bfd_elf_special_section.  */
   5669 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   5670 
   5671 	  /* Find out whether this segment contains any loadable
   5672 	     sections.  */
   5673 	  no_contents = true;
   5674 	  for (i = 0; i < m->count; i++)
   5675 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   5676 	      {
   5677 		no_contents = false;
   5678 		break;
   5679 	      }
   5680 
   5681 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   5682 
   5683 	  /* Broken hardware and/or kernel require that files do not
   5684 	     map the same page with different permissions on some hppa
   5685 	     processors.  */
   5686 	  if (j != 0
   5687 	      && (abfd->flags & D_PAGED) != 0
   5688 	      && bed->no_page_alias
   5689 	      && (off & (maxpagesize - 1)) != 0
   5690 	      && ((off & -maxpagesize)
   5691 		  == ((off + off_adjust) & -maxpagesize)))
   5692 	    off_adjust += maxpagesize;
   5693 	  off += off_adjust;
   5694 	  if (no_contents)
   5695 	    {
   5696 	      /* We shouldn't need to align the segment on disk since
   5697 		 the segment doesn't need file space, but the gABI
   5698 		 arguably requires the alignment and glibc ld.so
   5699 		 checks it.  So to comply with the alignment
   5700 		 requirement but not waste file space, we adjust
   5701 		 p_offset for just this segment.  (OFF_ADJUST is
   5702 		 subtracted from OFF later.)  This may put p_offset
   5703 		 past the end of file, but that shouldn't matter.  */
   5704 	    }
   5705 	  else
   5706 	    off_adjust = 0;
   5707 	}
   5708       /* Make sure the .dynamic section is the first section in the
   5709 	 PT_DYNAMIC segment.  */
   5710       else if (p->p_type == PT_DYNAMIC
   5711 	       && m->count > 1
   5712 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   5713 	{
   5714 	  _bfd_error_handler
   5715 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   5716 	       " is not the .dynamic section"),
   5717 	     abfd);
   5718 	  bfd_set_error (bfd_error_bad_value);
   5719 	  return false;
   5720 	}
   5721       /* Set the note section type to SHT_NOTE.  */
   5722       else if (p->p_type == PT_NOTE)
   5723 	for (i = 0; i < m->count; i++)
   5724 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   5725 
   5726       if (m->includes_filehdr)
   5727 	{
   5728 	  if (!m->p_flags_valid)
   5729 	    p->p_flags |= PF_R;
   5730 	  p->p_filesz = bed->s->sizeof_ehdr;
   5731 	  p->p_memsz = bed->s->sizeof_ehdr;
   5732 	  if (p->p_type == PT_LOAD)
   5733 	    {
   5734 	      if (m->count > 0)
   5735 		{
   5736 		  if (p->p_vaddr < (bfd_vma) off
   5737 		      || (!m->p_paddr_valid
   5738 			  && p->p_paddr < (bfd_vma) off))
   5739 		    {
   5740 		      _bfd_error_handler
   5741 			(_("%pB: not enough room for program headers,"
   5742 			   " try linking with -N"),
   5743 			 abfd);
   5744 		      bfd_set_error (bfd_error_bad_value);
   5745 		      return false;
   5746 		    }
   5747 		  p->p_vaddr -= off;
   5748 		  if (!m->p_paddr_valid)
   5749 		    p->p_paddr -= off;
   5750 		}
   5751 	    }
   5752 	  else if (sorted_seg_map[0]->includes_filehdr)
   5753 	    {
   5754 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   5755 	      p->p_vaddr = filehdr->p_vaddr;
   5756 	      if (!m->p_paddr_valid)
   5757 		p->p_paddr = filehdr->p_paddr;
   5758 	    }
   5759 	}
   5760 
   5761       if (m->includes_phdrs)
   5762 	{
   5763 	  if (!m->p_flags_valid)
   5764 	    p->p_flags |= PF_R;
   5765 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   5766 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   5767 	  if (!m->includes_filehdr)
   5768 	    {
   5769 	      if (p->p_type == PT_LOAD)
   5770 		{
   5771 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   5772 		  if (m->count > 0)
   5773 		    {
   5774 		      p->p_vaddr -= off - p->p_offset;
   5775 		      if (!m->p_paddr_valid)
   5776 			p->p_paddr -= off - p->p_offset;
   5777 		    }
   5778 		}
   5779 	      else if (phdr_load_seg != NULL)
   5780 		{
   5781 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   5782 		  bfd_vma phdr_off = 0;  /* Octets.  */
   5783 		  if (phdr_load_seg->includes_filehdr)
   5784 		    phdr_off = bed->s->sizeof_ehdr;
   5785 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   5786 		  if (!m->p_paddr_valid)
   5787 		    p->p_paddr = phdr->p_paddr + phdr_off;
   5788 		  p->p_offset = phdr->p_offset + phdr_off;
   5789 		}
   5790 	      else
   5791 		p->p_offset = bed->s->sizeof_ehdr;
   5792 	    }
   5793 	}
   5794 
   5795       if (p->p_type == PT_LOAD
   5796 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   5797 	{
   5798 	  if (!m->includes_filehdr && !m->includes_phdrs)
   5799 	    {
   5800 	      p->p_offset = off;
   5801 	      if (no_contents)
   5802 		{
   5803 		  /* Put meaningless p_offset for PT_LOAD segments
   5804 		     without file contents somewhere within the first
   5805 		     page, in an attempt to not point past EOF.  */
   5806 		  bfd_size_type align = maxpagesize;
   5807 		  if (align < p->p_align)
   5808 		    align = p->p_align;
   5809 		  if (align < 1)
   5810 		    align = 1;
   5811 		  p->p_offset = off % align;
   5812 		}
   5813 	    }
   5814 	  else
   5815 	    {
   5816 	      file_ptr adjust;  /* Octets.  */
   5817 
   5818 	      adjust = off - (p->p_offset + p->p_filesz);
   5819 	      if (!no_contents)
   5820 		p->p_filesz += adjust;
   5821 	      p->p_memsz += adjust;
   5822 	    }
   5823 	}
   5824 
   5825       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   5826 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   5827 	 core files, for sections in PT_NOTE segments.
   5828 	 assign_file_positions_for_non_load_sections will set filepos
   5829 	 for other sections and update p_filesz for other segments.  */
   5830       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5831 	{
   5832 	  asection *sec;
   5833 	  bfd_size_type align;
   5834 	  Elf_Internal_Shdr *this_hdr;
   5835 
   5836 	  sec = *secpp;
   5837 	  this_hdr = &elf_section_data (sec)->this_hdr;
   5838 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   5839 
   5840 	  if ((p->p_type == PT_LOAD
   5841 	       || p->p_type == PT_TLS)
   5842 	      && (this_hdr->sh_type != SHT_NOBITS
   5843 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   5844 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   5845 			  || p->p_type == PT_TLS))))
   5846 	    {
   5847 	      bfd_vma p_start = p->p_paddr;                /* Octets.  */
   5848 	      bfd_vma p_end = p_start + p->p_memsz;        /* Octets.  */
   5849 	      bfd_vma s_start = sec->lma * opb;            /* Octets.  */
   5850 	      bfd_vma adjust = s_start - p_end;            /* Octets.  */
   5851 
   5852 	      if (adjust != 0
   5853 		  && (s_start < p_end
   5854 		      || p_end < p_start))
   5855 		{
   5856 		  _bfd_error_handler
   5857 		    /* xgettext:c-format */
   5858 		    (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
   5859 		     abfd, sec, (uint64_t) s_start / opb,
   5860 		     (uint64_t) p_end / opb);
   5861 		  adjust = 0;
   5862 		  sec->lma = p_end / opb;
   5863 		}
   5864 	      p->p_memsz += adjust;
   5865 
   5866 	      if (p->p_type == PT_LOAD)
   5867 		{
   5868 		  if (this_hdr->sh_type != SHT_NOBITS)
   5869 		    {
   5870 		      off_adjust = 0;
   5871 		      if (p->p_filesz + adjust < p->p_memsz)
   5872 			{
   5873 			  /* We have a PROGBITS section following NOBITS ones.
   5874 			     Allocate file space for the NOBITS section(s) and
   5875 			     zero it.  */
   5876 			  adjust = p->p_memsz - p->p_filesz;
   5877 			  if (!write_zeros (abfd, off, adjust))
   5878 			    return false;
   5879 			}
   5880 		    }
   5881 		  /* We only adjust sh_offset in SHT_NOBITS sections
   5882 		     as would seem proper for their address when the
   5883 		     section is first in the segment.  sh_offset
   5884 		     doesn't really have any significance for
   5885 		     SHT_NOBITS anyway, apart from a notional position
   5886 		     relative to other sections.  Historically we
   5887 		     didn't bother with adjusting sh_offset and some
   5888 		     programs depend on it not being adjusted.  See
   5889 		     pr12921 and pr25662.  */
   5890 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   5891 		    {
   5892 		      off += adjust;
   5893 		      if (this_hdr->sh_type == SHT_NOBITS)
   5894 			off_adjust += adjust;
   5895 		    }
   5896 		}
   5897 	      if (this_hdr->sh_type != SHT_NOBITS)
   5898 		p->p_filesz += adjust;
   5899 	    }
   5900 
   5901 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   5902 	    {
   5903 	      /* The section at i == 0 is the one that actually contains
   5904 		 everything.  */
   5905 	      if (i == 0)
   5906 		{
   5907 		  this_hdr->sh_offset = sec->filepos = off;
   5908 		  off += this_hdr->sh_size;
   5909 		  p->p_filesz = this_hdr->sh_size;
   5910 		  p->p_memsz = 0;
   5911 		  p->p_align = 1;
   5912 		}
   5913 	      else
   5914 		{
   5915 		  /* The rest are fake sections that shouldn't be written.  */
   5916 		  sec->filepos = 0;
   5917 		  sec->size = 0;
   5918 		  sec->flags = 0;
   5919 		  continue;
   5920 		}
   5921 	    }
   5922 	  else
   5923 	    {
   5924 	      if (p->p_type == PT_LOAD)
   5925 		{
   5926 		  this_hdr->sh_offset = sec->filepos = off;
   5927 		  if (this_hdr->sh_type != SHT_NOBITS)
   5928 		    off += this_hdr->sh_size;
   5929 		}
   5930 	      else if (this_hdr->sh_type == SHT_NOBITS
   5931 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   5932 		       && this_hdr->sh_offset == 0)
   5933 		{
   5934 		  /* This is a .tbss section that didn't get a PT_LOAD.
   5935 		     (See _bfd_elf_map_sections_to_segments "Create a
   5936 		     final PT_LOAD".)  Set sh_offset to the value it
   5937 		     would have if we had created a zero p_filesz and
   5938 		     p_memsz PT_LOAD header for the section.  This
   5939 		     also makes the PT_TLS header have the same
   5940 		     p_offset value.  */
   5941 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   5942 							  off, align);
   5943 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   5944 		}
   5945 
   5946 	      if (this_hdr->sh_type != SHT_NOBITS)
   5947 		{
   5948 		  p->p_filesz += this_hdr->sh_size;
   5949 		  /* A load section without SHF_ALLOC is something like
   5950 		     a note section in a PT_NOTE segment.  These take
   5951 		     file space but are not loaded into memory.  */
   5952 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5953 		    p->p_memsz += this_hdr->sh_size;
   5954 		}
   5955 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5956 		{
   5957 		  if (p->p_type == PT_TLS)
   5958 		    p->p_memsz += this_hdr->sh_size;
   5959 
   5960 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   5961 		     normal segments.  */
   5962 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   5963 		    p->p_memsz += this_hdr->sh_size;
   5964 		}
   5965 
   5966 	      if (align > p->p_align
   5967 		  && !m->p_align_valid
   5968 		  && (p->p_type != PT_LOAD
   5969 		      || (abfd->flags & D_PAGED) == 0))
   5970 		p->p_align = align;
   5971 	    }
   5972 
   5973 	  if (!m->p_flags_valid)
   5974 	    {
   5975 	      p->p_flags |= PF_R;
   5976 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   5977 		p->p_flags |= PF_X;
   5978 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   5979 		p->p_flags |= PF_W;
   5980 	    }
   5981 	}
   5982 
   5983       off -= off_adjust;
   5984 
   5985       /* PR ld/20815 - Check that the program header segment, if
   5986 	 present, will be loaded into memory.  */
   5987       if (p->p_type == PT_PHDR
   5988 	  && phdr_load_seg == NULL
   5989 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   5990 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   5991 	{
   5992 	  /* The fix for this error is usually to edit the linker script being
   5993 	     used and set up the program headers manually.  Either that or
   5994 	     leave room for the headers at the start of the SECTIONS.  */
   5995 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   5996 				" by LOAD segment"),
   5997 			      abfd);
   5998 	  if (link_info == NULL)
   5999 	    return false;
   6000 	  /* Arrange for the linker to exit with an error, deleting
   6001 	     the output file unless --noinhibit-exec is given.  */
   6002 	  link_info->callbacks->info ("%X");
   6003 	}
   6004 
   6005       /* Check that all sections are in a PT_LOAD segment.
   6006 	 Don't check funky gdb generated core files.  */
   6007       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6008 	{
   6009 	  bool check_vma = true;
   6010 
   6011 	  for (i = 1; i < m->count; i++)
   6012 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6013 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6014 				       ->this_hdr), p) != 0
   6015 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6016 				       ->this_hdr), p) != 0)
   6017 	      {
   6018 		/* Looks like we have overlays packed into the segment.  */
   6019 		check_vma = false;
   6020 		break;
   6021 	      }
   6022 
   6023 	  for (i = 0; i < m->count; i++)
   6024 	    {
   6025 	      Elf_Internal_Shdr *this_hdr;
   6026 	      asection *sec;
   6027 
   6028 	      sec = m->sections[i];
   6029 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6030 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6031 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6032 		{
   6033 		  _bfd_error_handler
   6034 		    /* xgettext:c-format */
   6035 		    (_("%pB: section `%pA' can't be allocated in segment %d"),
   6036 		     abfd, sec, j);
   6037 		  print_segment_map (m);
   6038 		}
   6039 	    }
   6040 
   6041 	  if (p_align_p)
   6042 	    p->p_align = p_align;
   6043 	}
   6044     }
   6045 
   6046   elf_next_file_pos (abfd) = off;
   6047 
   6048   if (link_info != NULL
   6049       && phdr_load_seg != NULL
   6050       && phdr_load_seg->includes_filehdr)
   6051     {
   6052       /* There is a segment that contains both the file headers and the
   6053 	 program headers, so provide a symbol __ehdr_start pointing there.
   6054 	 A program can use this to examine itself robustly.  */
   6055 
   6056       struct elf_link_hash_entry *hash
   6057 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   6058 				false, false, true);
   6059       /* If the symbol was referenced and not defined, define it.  */
   6060       if (hash != NULL
   6061 	  && (hash->root.type == bfd_link_hash_new
   6062 	      || hash->root.type == bfd_link_hash_undefined
   6063 	      || hash->root.type == bfd_link_hash_undefweak
   6064 	      || hash->root.type == bfd_link_hash_common))
   6065 	{
   6066 	  asection *s = NULL;
   6067 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6068 
   6069 	  if (phdr_load_seg->count != 0)
   6070 	    /* The segment contains sections, so use the first one.  */
   6071 	    s = phdr_load_seg->sections[0];
   6072 	  else
   6073 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6074 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6075 	      if (m->p_type == PT_LOAD && m->count != 0)
   6076 		{
   6077 		  s = m->sections[0];
   6078 		  break;
   6079 		}
   6080 
   6081 	  if (s != NULL)
   6082 	    {
   6083 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6084 	      hash->root.u.def.section = s;
   6085 	    }
   6086 	  else
   6087 	    {
   6088 	      hash->root.u.def.value = filehdr_vaddr;
   6089 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6090 	    }
   6091 
   6092 	  hash->root.type = bfd_link_hash_defined;
   6093 	  hash->def_regular = 1;
   6094 	  hash->non_elf = 0;
   6095 	}
   6096     }
   6097 
   6098   return true;
   6099 }
   6100 
   6101 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6102    is no defined method for detecting such files, so we have to
   6103    use heuristics instead.  */
   6104 
   6105 bool
   6106 is_debuginfo_file (bfd *abfd)
   6107 {
   6108   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6109     return false;
   6110 
   6111   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6112   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6113   Elf_Internal_Shdr **headerp;
   6114 
   6115   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6116     {
   6117       Elf_Internal_Shdr *header = * headerp;
   6118 
   6119       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6120 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6121       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6122 	  && header->sh_type != SHT_NOBITS
   6123 	  && header->sh_type != SHT_NOTE)
   6124 	return false;
   6125     }
   6126 
   6127   return true;
   6128 }
   6129 
   6130 /* Assign file positions for the other sections, except for compressed debugging
   6131    and other sections assigned in _bfd_elf_assign_file_positions_for_non_load().  */
   6132 
   6133 static bool
   6134 assign_file_positions_for_non_load_sections (bfd *abfd,
   6135 					     struct bfd_link_info *link_info)
   6136 {
   6137   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6138   Elf_Internal_Shdr **i_shdrpp;
   6139   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6140   Elf_Internal_Phdr *phdrs;
   6141   Elf_Internal_Phdr *p;
   6142   struct elf_segment_map *m;
   6143   file_ptr off;
   6144   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6145   bfd_vma maxpagesize;
   6146 
   6147   if (link_info != NULL)
   6148     maxpagesize = link_info->maxpagesize;
   6149   else
   6150     maxpagesize = bed->maxpagesize;
   6151   i_shdrpp = elf_elfsections (abfd);
   6152   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6153   off = elf_next_file_pos (abfd);
   6154   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6155     {
   6156       Elf_Internal_Shdr *hdr;
   6157       bfd_vma align;
   6158 
   6159       hdr = *hdrpp;
   6160       if (hdr->bfd_section != NULL
   6161 	  && (hdr->bfd_section->filepos != 0
   6162 	      || (hdr->sh_type == SHT_NOBITS
   6163 		  && hdr->contents == NULL)))
   6164 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6165       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6166 	{
   6167 	  if (hdr->sh_size != 0
   6168 	      /* PR 24717 - debuginfo files are known to be not strictly
   6169 		 compliant with the ELF standard.  In particular they often
   6170 		 have .note.gnu.property sections that are outside of any
   6171 		 loadable segment.  This is not a problem for such files,
   6172 		 so do not warn about them.  */
   6173 	      && ! is_debuginfo_file (abfd))
   6174 	    _bfd_error_handler
   6175 	      /* xgettext:c-format */
   6176 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6177 	       abfd,
   6178 	       (hdr->bfd_section == NULL
   6179 		? "*unknown*"
   6180 		: hdr->bfd_section->name));
   6181 	  /* We don't need to page align empty sections.  */
   6182 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6183 	    align = maxpagesize;
   6184 	  else
   6185 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6186 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6187 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   6188 							   false);
   6189 	}
   6190       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6191 		&& hdr->bfd_section == NULL)
   6192 	       /* We don't know the offset of these sections yet: their size has
   6193 		  not been decided.  */
   6194 	       || (hdr->bfd_section != NULL
   6195 		   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
   6196 		       || (bfd_section_is_ctf (hdr->bfd_section)
   6197 			   && abfd->is_linker_output)))
   6198 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6199 	       || (elf_symtab_shndx_list (abfd) != NULL
   6200 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6201 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6202 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6203 	hdr->sh_offset = -1;
   6204       else
   6205 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6206     }
   6207   elf_next_file_pos (abfd) = off;
   6208 
   6209   /* Now that we have set the section file positions, we can set up
   6210      the file positions for the non PT_LOAD segments.  */
   6211   phdrs = elf_tdata (abfd)->phdr;
   6212   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6213     {
   6214       if (p->p_type == PT_GNU_RELRO)
   6215 	{
   6216 	  bfd_vma start, end;  /* Bytes.  */
   6217 	  bool ok;
   6218 
   6219 	  if (link_info != NULL)
   6220 	    {
   6221 	      /* During linking the range of the RELRO segment is passed
   6222 		 in link_info.  Note that there may be padding between
   6223 		 relro_start and the first RELRO section.  */
   6224 	      start = link_info->relro_start;
   6225 	      end = link_info->relro_end;
   6226 	    }
   6227 	  else if (m->count != 0)
   6228 	    {
   6229 	      if (!m->p_size_valid)
   6230 		abort ();
   6231 	      start = m->sections[0]->vma;
   6232 	      end = start + m->p_size / opb;
   6233 	    }
   6234 	  else
   6235 	    {
   6236 	      start = 0;
   6237 	      end = 0;
   6238 	    }
   6239 
   6240 	  ok = false;
   6241 	  if (start < end)
   6242 	    {
   6243 	      struct elf_segment_map *lm;
   6244 	      const Elf_Internal_Phdr *lp;
   6245 	      unsigned int i;
   6246 
   6247 	      /* Find a LOAD segment containing a section in the RELRO
   6248 		 segment.  */
   6249 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6250 		   lm != NULL;
   6251 		   lm = lm->next, lp++)
   6252 		{
   6253 		  if (lp->p_type == PT_LOAD
   6254 		      && lm->count != 0
   6255 		      && (lm->sections[lm->count - 1]->vma
   6256 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6257 			     ? lm->sections[lm->count - 1]->size / opb
   6258 			     : 0)) > start
   6259 		      && lm->sections[0]->vma < end)
   6260 		    break;
   6261 		}
   6262 
   6263 	      if (lm != NULL)
   6264 		{
   6265 		  /* Find the section starting the RELRO segment.  */
   6266 		  for (i = 0; i < lm->count; i++)
   6267 		    {
   6268 		      asection *s = lm->sections[i];
   6269 		      if (s->vma >= start
   6270 			  && s->vma < end
   6271 			  && s->size != 0)
   6272 			break;
   6273 		    }
   6274 
   6275 		  if (i < lm->count)
   6276 		    {
   6277 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6278 		      p->p_paddr = lm->sections[i]->lma * opb;
   6279 		      p->p_offset = lm->sections[i]->filepos;
   6280 		      p->p_memsz = end * opb - p->p_vaddr;
   6281 		      p->p_filesz = p->p_memsz;
   6282 
   6283 		      /* The RELRO segment typically ends a few bytes
   6284 			 into .got.plt but other layouts are possible.
   6285 			 In cases where the end does not match any
   6286 			 loaded section (for instance is in file
   6287 			 padding), trim p_filesz back to correspond to
   6288 			 the end of loaded section contents.  */
   6289 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6290 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6291 
   6292 		      /* Preserve the alignment and flags if they are
   6293 			 valid.  The gold linker generates RW/4 for
   6294 			 the PT_GNU_RELRO section.  It is better for
   6295 			 objcopy/strip to honor these attributes
   6296 			 otherwise gdb will choke when using separate
   6297 			 debug files.  */
   6298 		      if (!m->p_align_valid)
   6299 			p->p_align = 1;
   6300 		      if (!m->p_flags_valid)
   6301 			p->p_flags = PF_R;
   6302 		      ok = true;
   6303 		    }
   6304 		}
   6305 	    }
   6306 
   6307 	  if (!ok)
   6308 	    {
   6309 	      if (link_info != NULL)
   6310 		_bfd_error_handler
   6311 		  (_("%pB: warning: unable to allocate any sections to PT_GNU_RELRO segment"),
   6312 		   abfd);
   6313 	      memset (p, 0, sizeof *p);
   6314 	    }
   6315 	}
   6316       else if (p->p_type == PT_GNU_STACK)
   6317 	{
   6318 	  if (m->p_size_valid)
   6319 	    p->p_memsz = m->p_size;
   6320 	}
   6321       else if (m->count != 0)
   6322 	{
   6323 	  unsigned int i;
   6324 
   6325 	  if (p->p_type != PT_LOAD
   6326 	      && (p->p_type != PT_NOTE
   6327 		  || bfd_get_format (abfd) != bfd_core))
   6328 	    {
   6329 	      /* A user specified segment layout may include a PHDR
   6330 		 segment that overlaps with a LOAD segment...  */
   6331 	      if (p->p_type == PT_PHDR)
   6332 		{
   6333 		  m->count = 0;
   6334 		  continue;
   6335 		}
   6336 
   6337 	      if (m->includes_filehdr || m->includes_phdrs)
   6338 		{
   6339 		  /* PR 17512: file: 2195325e.  */
   6340 		  _bfd_error_handler
   6341 		    (_("%pB: error: non-load segment %d includes file header "
   6342 		       "and/or program header"),
   6343 		     abfd, (int) (p - phdrs));
   6344 		  return false;
   6345 		}
   6346 
   6347 	      p->p_filesz = 0;
   6348 	      p->p_offset = m->sections[0]->filepos;
   6349 	      for (i = m->count; i-- != 0;)
   6350 		{
   6351 		  asection *sect = m->sections[i];
   6352 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6353 		  if (hdr->sh_type != SHT_NOBITS)
   6354 		    {
   6355 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6356 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6357 			 should be the same as p_filesz.  */
   6358 		      if (p->p_type == PT_NOTE
   6359 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6360 			p->p_memsz = p->p_filesz;
   6361 		      break;
   6362 		    }
   6363 		}
   6364 	    }
   6365 	}
   6366     }
   6367 
   6368   return true;
   6369 }
   6370 
   6371 static elf_section_list *
   6372 find_section_in_list (unsigned int i, elf_section_list * list)
   6373 {
   6374   for (;list != NULL; list = list->next)
   6375     if (list->ndx == i)
   6376       break;
   6377   return list;
   6378 }
   6379 
   6380 /* Work out the file positions of all the sections.  This is called by
   6381    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6382    VMAs must be known before this is called.
   6383 
   6384    Reloc sections come in two flavours: Those processed specially as
   6385    "side-channel" data attached to a section to which they apply, and those that
   6386    bfd doesn't process as relocations.  The latter sort are stored in a normal
   6387    bfd section by bfd_section_from_shdr.  We don't consider the former sort
   6388    here, unless they form part of the loadable image.  Reloc sections not
   6389    assigned here (and compressed debugging sections and CTF sections which
   6390    nothing else in the file can rely upon) will be handled later by
   6391    assign_file_positions_for_relocs.
   6392 
   6393    We also don't set the positions of the .symtab and .strtab here.  */
   6394 
   6395 static bool
   6396 assign_file_positions_except_relocs (bfd *abfd,
   6397 				     struct bfd_link_info *link_info)
   6398 {
   6399   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6400   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6401   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6402   unsigned int alloc;
   6403 
   6404   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6405       && bfd_get_format (abfd) != bfd_core)
   6406     {
   6407       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6408       unsigned int num_sec = elf_numsections (abfd);
   6409       Elf_Internal_Shdr **hdrpp;
   6410       unsigned int i;
   6411       file_ptr off;
   6412 
   6413       /* Start after the ELF header.  */
   6414       off = i_ehdrp->e_ehsize;
   6415 
   6416       /* We are not creating an executable, which means that we are
   6417 	 not creating a program header, and that the actual order of
   6418 	 the sections in the file is unimportant.  */
   6419       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6420 	{
   6421 	  Elf_Internal_Shdr *hdr;
   6422 
   6423 	  hdr = *hdrpp;
   6424 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6425 	       && hdr->bfd_section == NULL)
   6426 	      /* Do not assign offsets for these sections yet: we don't know
   6427 		 their sizes.  */
   6428 	      || (hdr->bfd_section != NULL
   6429 		  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
   6430 		      || (bfd_section_is_ctf (hdr->bfd_section)
   6431 			  && abfd->is_linker_output)))
   6432 	      || i == elf_onesymtab (abfd)
   6433 	      || (elf_symtab_shndx_list (abfd) != NULL
   6434 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6435 	      || i == elf_strtab_sec (abfd)
   6436 	      || i == elf_shstrtab_sec (abfd))
   6437 	    {
   6438 	      hdr->sh_offset = -1;
   6439 	    }
   6440 	  else
   6441 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6442 	}
   6443 
   6444       elf_next_file_pos (abfd) = off;
   6445       elf_program_header_size (abfd) = 0;
   6446     }
   6447   else
   6448     {
   6449       /* Assign file positions for the loaded sections based on the
   6450 	 assignment of sections to segments.  */
   6451       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6452 	return false;
   6453 
   6454       /* And for non-load sections.  */
   6455       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6456 	return false;
   6457     }
   6458 
   6459   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   6460     return false;
   6461 
   6462   /* Write out the program headers.  */
   6463   alloc = i_ehdrp->e_phnum;
   6464   if (alloc != 0)
   6465     {
   6466       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   6467 	{
   6468 	  /* Memory resident segments with non-zero size and RWX permissions are a
   6469 	     security risk, so we generate a warning here if we are creating any.  */
   6470 	  unsigned int i;
   6471 
   6472 	  for (i = 0; i < alloc; i++)
   6473 	    {
   6474 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   6475 
   6476 	      if (phdr->p_memsz == 0)
   6477 		continue;
   6478 
   6479 	      if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
   6480 		_bfd_error_handler (_("warning: %pB has a TLS segment with execute permission"),
   6481 				    abfd);
   6482 	      else if (phdr->p_type == PT_LOAD
   6483 		       && (phdr->p_flags & (PF_R | PF_W | PF_X)) == (PF_R | PF_W | PF_X))
   6484 		_bfd_error_handler (_("warning: %pB has a LOAD segment with RWX permissions"),
   6485 				    abfd);
   6486 	    }
   6487 	}
   6488 
   6489       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   6490 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   6491 	return false;
   6492     }
   6493 
   6494   return true;
   6495 }
   6496 
   6497 bool
   6498 _bfd_elf_init_file_header (bfd *abfd,
   6499 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   6500 {
   6501   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   6502   struct elf_strtab_hash *shstrtab;
   6503   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6504 
   6505   i_ehdrp = elf_elfheader (abfd);
   6506 
   6507   shstrtab = _bfd_elf_strtab_init ();
   6508   if (shstrtab == NULL)
   6509     return false;
   6510 
   6511   elf_shstrtab (abfd) = shstrtab;
   6512 
   6513   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   6514   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   6515   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   6516   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   6517 
   6518   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   6519   i_ehdrp->e_ident[EI_DATA] =
   6520     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   6521   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   6522 
   6523   if ((abfd->flags & DYNAMIC) != 0)
   6524     i_ehdrp->e_type = ET_DYN;
   6525   else if ((abfd->flags & EXEC_P) != 0)
   6526     i_ehdrp->e_type = ET_EXEC;
   6527   else if (bfd_get_format (abfd) == bfd_core)
   6528     i_ehdrp->e_type = ET_CORE;
   6529   else
   6530     i_ehdrp->e_type = ET_REL;
   6531 
   6532   switch (bfd_get_arch (abfd))
   6533     {
   6534     case bfd_arch_unknown:
   6535       i_ehdrp->e_machine = EM_NONE;
   6536       break;
   6537 
   6538       /* There used to be a long list of cases here, each one setting
   6539 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   6540 	 in the corresponding bfd definition.  To avoid duplication,
   6541 	 the switch was removed.  Machines that need special handling
   6542 	 can generally do it in elf_backend_final_write_processing(),
   6543 	 unless they need the information earlier than the final write.
   6544 	 Such need can generally be supplied by replacing the tests for
   6545 	 e_machine with the conditions used to determine it.  */
   6546     default:
   6547       i_ehdrp->e_machine = bed->elf_machine_code;
   6548     }
   6549 
   6550   i_ehdrp->e_version = bed->s->ev_current;
   6551   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   6552 
   6553   /* No program header, for now.  */
   6554   i_ehdrp->e_phoff = 0;
   6555   i_ehdrp->e_phentsize = 0;
   6556   i_ehdrp->e_phnum = 0;
   6557 
   6558   /* Each bfd section is section header entry.  */
   6559   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   6560   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   6561 
   6562   elf_tdata (abfd)->symtab_hdr.sh_name =
   6563     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   6564   elf_tdata (abfd)->strtab_hdr.sh_name =
   6565     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   6566   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   6567     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   6568   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   6569       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   6570       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   6571     return false;
   6572 
   6573   return true;
   6574 }
   6575 
   6576 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   6577 
   6578    FIXME: We used to have code here to sort the PT_LOAD segments into
   6579    ascending order, as per the ELF spec.  But this breaks some programs,
   6580    including the Linux kernel.  But really either the spec should be
   6581    changed or the programs updated.  */
   6582 
   6583 bool
   6584 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   6585 {
   6586   if (link_info != NULL && bfd_link_pie (link_info))
   6587     {
   6588       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   6589       unsigned int num_segments = i_ehdrp->e_phnum;
   6590       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   6591       Elf_Internal_Phdr *segment = tdata->phdr;
   6592       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   6593 
   6594       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   6595       bfd_vma p_vaddr = (bfd_vma) -1;
   6596       for (; segment < end_segment; segment++)
   6597 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   6598 	  p_vaddr = segment->p_vaddr;
   6599 
   6600       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   6601 	 segments is non-zero.  */
   6602       if (p_vaddr)
   6603 	i_ehdrp->e_type = ET_EXEC;
   6604     }
   6605   return true;
   6606 }
   6607 
   6608 /* Assign file positions for all the reloc sections which are not part
   6609    of the loadable file image, and the file position of section headers.  */
   6610 
   6611 static bool
   6612 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   6613 {
   6614   file_ptr off;
   6615   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   6616   Elf_Internal_Shdr *shdrp;
   6617   Elf_Internal_Ehdr *i_ehdrp;
   6618   const struct elf_backend_data *bed;
   6619 
   6620   off = elf_next_file_pos (abfd);
   6621 
   6622   shdrpp = elf_elfsections (abfd);
   6623   end_shdrpp = shdrpp + elf_numsections (abfd);
   6624   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   6625     {
   6626       shdrp = *shdrpp;
   6627       if (shdrp->sh_offset == -1)
   6628 	{
   6629 	  asection *sec = shdrp->bfd_section;
   6630 	  bool is_rel = (shdrp->sh_type == SHT_REL
   6631 			 || shdrp->sh_type == SHT_RELA);
   6632 	  bool is_ctf = sec && bfd_section_is_ctf (sec);
   6633 	  if (is_rel
   6634 	      || is_ctf
   6635 	      || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
   6636 	    {
   6637 	      if (!is_rel && !is_ctf)
   6638 		{
   6639 		  const char *name = sec->name;
   6640 		  struct bfd_elf_section_data *d;
   6641 
   6642 		  /* Compress DWARF debug sections.  */
   6643 		  if (!bfd_compress_section (abfd, sec,
   6644 					     shdrp->contents))
   6645 		    return false;
   6646 
   6647 		  if (sec->compress_status == COMPRESS_SECTION_DONE
   6648 		      && (abfd->flags & BFD_COMPRESS_GABI) == 0)
   6649 		    {
   6650 		      /* If section is compressed with zlib-gnu, convert
   6651 			 section name from .debug_* to .zdebug_*.  */
   6652 		      char *new_name
   6653 			= convert_debug_to_zdebug (abfd, name);
   6654 		      if (new_name == NULL)
   6655 			return false;
   6656 		      name = new_name;
   6657 		    }
   6658 		  /* Add section name to section name section.  */
   6659 		  if (shdrp->sh_name != (unsigned int) -1)
   6660 		    abort ();
   6661 		  shdrp->sh_name
   6662 		    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   6663 							  name, false);
   6664 		  d = elf_section_data (sec);
   6665 
   6666 		  /* Add reloc section name to section name section.  */
   6667 		  if (d->rel.hdr
   6668 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6669 						      d->rel.hdr,
   6670 						      name, false))
   6671 		    return false;
   6672 		  if (d->rela.hdr
   6673 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6674 						      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 		  shdrp->bfd_section->contents = NULL;
   6682 		}
   6683 	      else if (is_ctf)
   6684 		{
   6685 		  /* Update section size and contents.	*/
   6686 		  shdrp->sh_size = sec->size;
   6687 		  shdrp->contents = sec->contents;
   6688 		}
   6689 
   6690 	      off = _bfd_elf_assign_file_position_for_section (shdrp,
   6691 							       off,
   6692 							       true);
   6693 	    }
   6694 	}
   6695     }
   6696 
   6697   /* Place section name section after DWARF debug sections have been
   6698      compressed.  */
   6699   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   6700   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   6701   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   6702   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   6703 
   6704   /* Place the section headers.  */
   6705   i_ehdrp = elf_elfheader (abfd);
   6706   bed = get_elf_backend_data (abfd);
   6707   off = align_file_position (off, 1 << bed->s->log_file_align);
   6708   i_ehdrp->e_shoff = off;
   6709   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   6710   elf_next_file_pos (abfd) = off;
   6711 
   6712   return true;
   6713 }
   6714 
   6715 bool
   6716 _bfd_elf_write_object_contents (bfd *abfd)
   6717 {
   6718   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6719   Elf_Internal_Shdr **i_shdrp;
   6720   bool failed;
   6721   unsigned int count, num_sec;
   6722   struct elf_obj_tdata *t;
   6723 
   6724   if (! abfd->output_has_begun
   6725       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   6726     return false;
   6727   /* Do not rewrite ELF data when the BFD has been opened for update.
   6728      abfd->output_has_begun was set to TRUE on opening, so creation of new
   6729      sections, and modification of existing section sizes was restricted.
   6730      This means the ELF header, program headers and section headers can't have
   6731      changed.
   6732      If the contents of any sections has been modified, then those changes have
   6733      already been written to the BFD.  */
   6734   else if (abfd->direction == both_direction)
   6735     {
   6736       BFD_ASSERT (abfd->output_has_begun);
   6737       return true;
   6738     }
   6739 
   6740   i_shdrp = elf_elfsections (abfd);
   6741 
   6742   failed = false;
   6743   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   6744   if (failed)
   6745     return false;
   6746 
   6747   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   6748     return false;
   6749 
   6750   /* After writing the headers, we need to write the sections too...  */
   6751   num_sec = elf_numsections (abfd);
   6752   for (count = 1; count < num_sec; count++)
   6753     {
   6754       i_shdrp[count]->sh_name
   6755 	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   6756 				  i_shdrp[count]->sh_name);
   6757       if (bed->elf_backend_section_processing)
   6758 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   6759 	  return false;
   6760       if (i_shdrp[count]->contents)
   6761 	{
   6762 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   6763 
   6764 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   6765 	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
   6766 	    return false;
   6767 	}
   6768     }
   6769 
   6770   /* Write out the section header names.  */
   6771   t = elf_tdata (abfd);
   6772   if (elf_shstrtab (abfd) != NULL
   6773       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   6774 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   6775     return false;
   6776 
   6777   if (!(*bed->elf_backend_final_write_processing) (abfd))
   6778     return false;
   6779 
   6780   if (!bed->s->write_shdrs_and_ehdr (abfd))
   6781     return false;
   6782 
   6783   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   6784   if (t->o->build_id.after_write_object_contents != NULL
   6785       && !(*t->o->build_id.after_write_object_contents) (abfd))
   6786     return false;
   6787   if (t->o->package_metadata.after_write_object_contents != NULL
   6788       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   6789     return false;
   6790 
   6791   return true;
   6792 }
   6793 
   6794 bool
   6795 _bfd_elf_write_corefile_contents (bfd *abfd)
   6796 {
   6797   /* Hopefully this can be done just like an object file.  */
   6798   return _bfd_elf_write_object_contents (abfd);
   6799 }
   6800 
   6801 /* Given a section, search the header to find them.  */
   6802 
   6803 unsigned int
   6804 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   6805 {
   6806   const struct elf_backend_data *bed;
   6807   unsigned int sec_index;
   6808 
   6809   if (elf_section_data (asect) != NULL
   6810       && elf_section_data (asect)->this_idx != 0)
   6811     return elf_section_data (asect)->this_idx;
   6812 
   6813   if (bfd_is_abs_section (asect))
   6814     sec_index = SHN_ABS;
   6815   else if (bfd_is_com_section (asect))
   6816     sec_index = SHN_COMMON;
   6817   else if (bfd_is_und_section (asect))
   6818     sec_index = SHN_UNDEF;
   6819   else
   6820     sec_index = SHN_BAD;
   6821 
   6822   bed = get_elf_backend_data (abfd);
   6823   if (bed->elf_backend_section_from_bfd_section)
   6824     {
   6825       int retval = sec_index;
   6826 
   6827       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   6828 	return retval;
   6829     }
   6830 
   6831   if (sec_index == SHN_BAD)
   6832     bfd_set_error (bfd_error_nonrepresentable_section);
   6833 
   6834   return sec_index;
   6835 }
   6836 
   6837 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   6838    on error.  */
   6839 
   6840 int
   6841 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   6842 {
   6843   asymbol *asym_ptr = *asym_ptr_ptr;
   6844   int idx;
   6845   flagword flags = asym_ptr->flags;
   6846 
   6847   /* When gas creates relocations against local labels, it creates its
   6848      own symbol for the section, but does put the symbol into the
   6849      symbol chain, so udata is 0.  When the linker is generating
   6850      relocatable output, this section symbol may be for one of the
   6851      input sections rather than the output section.  */
   6852   if (asym_ptr->udata.i == 0
   6853       && (flags & BSF_SECTION_SYM)
   6854       && asym_ptr->section)
   6855     {
   6856       asection *sec;
   6857 
   6858       sec = asym_ptr->section;
   6859       if (sec->owner != abfd && sec->output_section != NULL)
   6860 	sec = sec->output_section;
   6861       if (sec->owner == abfd
   6862 	  && sec->index < elf_num_section_syms (abfd)
   6863 	  && elf_section_syms (abfd)[sec->index] != NULL)
   6864 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   6865     }
   6866 
   6867   idx = asym_ptr->udata.i;
   6868 
   6869   if (idx == 0)
   6870     {
   6871       /* This case can occur when using --strip-symbol on a symbol
   6872 	 which is used in a relocation entry.  */
   6873       _bfd_error_handler
   6874 	/* xgettext:c-format */
   6875 	(_("%pB: symbol `%s' required but not present"),
   6876 	 abfd, bfd_asymbol_name (asym_ptr));
   6877       bfd_set_error (bfd_error_no_symbols);
   6878       return -1;
   6879     }
   6880 
   6881 #if DEBUG & 4
   6882   {
   6883     fprintf (stderr,
   6884 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
   6885 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   6886     fflush (stderr);
   6887   }
   6888 #endif
   6889 
   6890   return idx;
   6891 }
   6892 
   6893 /* Rewrite program header information.  */
   6894 
   6895 static bool
   6896 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   6897 {
   6898   Elf_Internal_Ehdr *iehdr;
   6899   struct elf_segment_map *map;
   6900   struct elf_segment_map *map_first;
   6901   struct elf_segment_map **pointer_to_map;
   6902   Elf_Internal_Phdr *segment;
   6903   asection *section;
   6904   unsigned int i;
   6905   unsigned int num_segments;
   6906   bool phdr_included = false;
   6907   bool p_paddr_valid;
   6908   struct elf_segment_map *phdr_adjust_seg = NULL;
   6909   unsigned int phdr_adjust_num = 0;
   6910   const struct elf_backend_data *bed;
   6911   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   6912 
   6913   bed = get_elf_backend_data (ibfd);
   6914   iehdr = elf_elfheader (ibfd);
   6915 
   6916   map_first = NULL;
   6917   pointer_to_map = &map_first;
   6918 
   6919   num_segments = elf_elfheader (ibfd)->e_phnum;
   6920 
   6921   /* Returns the end address of the segment + 1.  */
   6922 #define SEGMENT_END(segment, start)					\
   6923   (start + (segment->p_memsz > segment->p_filesz			\
   6924 	    ? segment->p_memsz : segment->p_filesz))
   6925 
   6926 #define SECTION_SIZE(section, segment)					\
   6927   (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))		\
   6928     != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)			\
   6929    ? section->size : 0)
   6930 
   6931   /* Returns TRUE if the given section is contained within
   6932      the given segment.  VMA addresses are compared.  */
   6933 #define IS_CONTAINED_BY_VMA(section, segment, opb)			\
   6934   (section->vma * (opb) >= segment->p_vaddr				\
   6935    && (section->vma * (opb) + SECTION_SIZE (section, segment)		\
   6936        <= (SEGMENT_END (segment, segment->p_vaddr))))
   6937 
   6938   /* Returns TRUE if the given section is contained within
   6939      the given segment.  LMA addresses are compared.  */
   6940 #define IS_CONTAINED_BY_LMA(section, segment, base, opb)		\
   6941   (section->lma * (opb) >= base						\
   6942    && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
   6943    && (section->lma * (opb) + SECTION_SIZE (section, segment)		\
   6944        <= SEGMENT_END (segment, base)))
   6945 
   6946   /* Handle PT_NOTE segment.  */
   6947 #define IS_NOTE(p, s)							\
   6948   (p->p_type == PT_NOTE							\
   6949    && elf_section_type (s) == SHT_NOTE					\
   6950    && (bfd_vma) s->filepos >= p->p_offset				\
   6951    && ((bfd_vma) s->filepos + s->size					\
   6952        <= p->p_offset + p->p_filesz))
   6953 
   6954   /* Special case: corefile "NOTE" section containing regs, prpsinfo
   6955      etc.  */
   6956 #define IS_COREFILE_NOTE(p, s)						\
   6957   (IS_NOTE (p, s)							\
   6958    && bfd_get_format (ibfd) == bfd_core					\
   6959    && s->vma == 0							\
   6960    && s->lma == 0)
   6961 
   6962   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   6963      linker, which generates a PT_INTERP section with p_vaddr and
   6964      p_memsz set to 0.  */
   6965 #define IS_SOLARIS_PT_INTERP(p, s)					\
   6966   (p->p_vaddr == 0							\
   6967    && p->p_paddr == 0							\
   6968    && p->p_memsz == 0							\
   6969    && p->p_filesz > 0							\
   6970    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   6971    && s->size > 0							\
   6972    && (bfd_vma) s->filepos >= p->p_offset				\
   6973    && ((bfd_vma) s->filepos + s->size					\
   6974        <= p->p_offset + p->p_filesz))
   6975 
   6976   /* Decide if the given section should be included in the given segment.
   6977      A section will be included if:
   6978        1. It is within the address space of the segment -- we use the LMA
   6979 	  if that is set for the segment and the VMA otherwise,
   6980        2. It is an allocated section or a NOTE section in a PT_NOTE
   6981 	  segment.
   6982        3. There is an output section associated with it,
   6983        4. The section has not already been allocated to a previous segment.
   6984        5. PT_GNU_STACK segments do not include any sections.
   6985        6. PT_TLS segment includes only SHF_TLS sections.
   6986        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   6987        8. PT_DYNAMIC should not contain empty sections at the beginning
   6988 	  (with the possible exception of .dynamic).  */
   6989 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb)		\
   6990   ((((segment->p_paddr							\
   6991       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb)	\
   6992       : IS_CONTAINED_BY_VMA (section, segment, opb))			\
   6993      && (section->flags & SEC_ALLOC) != 0)				\
   6994     || IS_NOTE (segment, section))					\
   6995    && segment->p_type != PT_GNU_STACK					\
   6996    && (segment->p_type != PT_TLS					\
   6997        || (section->flags & SEC_THREAD_LOCAL))				\
   6998    && (segment->p_type == PT_LOAD					\
   6999        || segment->p_type == PT_TLS					\
   7000        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7001    && (segment->p_type != PT_DYNAMIC					\
   7002        || SECTION_SIZE (section, segment) > 0				\
   7003        || (segment->p_paddr						\
   7004 	   ? segment->p_paddr != section->lma * (opb)			\
   7005 	   : segment->p_vaddr != section->vma * (opb))			\
   7006        || (strcmp (bfd_section_name (section), ".dynamic") == 0))	\
   7007    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7008 
   7009 /* If the output section of a section in the input segment is NULL,
   7010    it is removed from the corresponding output segment.   */
   7011 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb)		\
   7012   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)		\
   7013    && section->output_section != NULL)
   7014 
   7015   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7016 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7017   (seg1->field >= SEGMENT_END (seg2, seg2->field))
   7018 
   7019   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7020      their VMA address ranges and their LMA address ranges overlap.
   7021      It is possible to have overlapping VMA ranges without overlapping LMA
   7022      ranges.  RedBoot images for example can have both .data and .bss mapped
   7023      to the same VMA range, but with the .data section mapped to a different
   7024      LMA.  */
   7025 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7026   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7027 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7028    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7029 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7030 
   7031   /* Initialise the segment mark field, and discard stupid alignment.  */
   7032   for (section = ibfd->sections; section != NULL; section = section->next)
   7033     {
   7034       asection *o = section->output_section;
   7035       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7036 	o->alignment_power = 0;
   7037       section->segment_mark = false;
   7038     }
   7039 
   7040   /* The Solaris linker creates program headers in which all the
   7041      p_paddr fields are zero.  When we try to objcopy or strip such a
   7042      file, we get confused.  Check for this case, and if we find it
   7043      don't set the p_paddr_valid fields.  */
   7044   p_paddr_valid = false;
   7045   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7046        i < num_segments;
   7047        i++, segment++)
   7048     if (segment->p_paddr != 0)
   7049       {
   7050 	p_paddr_valid = true;
   7051 	break;
   7052       }
   7053 
   7054   /* Scan through the segments specified in the program header
   7055      of the input BFD.  For this first scan we look for overlaps
   7056      in the loadable segments.  These can be created by weird
   7057      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7058   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7059        i < num_segments;
   7060        i++, segment++)
   7061     {
   7062       unsigned int j;
   7063       Elf_Internal_Phdr *segment2;
   7064 
   7065       if (segment->p_type == PT_INTERP)
   7066 	for (section = ibfd->sections; section; section = section->next)
   7067 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7068 	    {
   7069 	      /* Mininal change so that the normal section to segment
   7070 		 assignment code will work.  */
   7071 	      segment->p_vaddr = section->vma * opb;
   7072 	      break;
   7073 	    }
   7074 
   7075       if (segment->p_type != PT_LOAD)
   7076 	{
   7077 	  /* Remove PT_GNU_RELRO segment.  */
   7078 	  if (segment->p_type == PT_GNU_RELRO)
   7079 	    segment->p_type = PT_NULL;
   7080 	  continue;
   7081 	}
   7082 
   7083       /* Determine if this segment overlaps any previous segments.  */
   7084       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7085 	{
   7086 	  bfd_signed_vma extra_length;
   7087 
   7088 	  if (segment2->p_type != PT_LOAD
   7089 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7090 	    continue;
   7091 
   7092 	  /* Merge the two segments together.  */
   7093 	  if (segment2->p_vaddr < segment->p_vaddr)
   7094 	    {
   7095 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7096 		 SEGMENT.  */
   7097 	      extra_length = (SEGMENT_END (segment, segment->p_vaddr)
   7098 			      - SEGMENT_END (segment2, segment2->p_vaddr));
   7099 
   7100 	      if (extra_length > 0)
   7101 		{
   7102 		  segment2->p_memsz += extra_length;
   7103 		  segment2->p_filesz += extra_length;
   7104 		}
   7105 
   7106 	      segment->p_type = PT_NULL;
   7107 
   7108 	      /* Since we have deleted P we must restart the outer loop.  */
   7109 	      i = 0;
   7110 	      segment = elf_tdata (ibfd)->phdr;
   7111 	      break;
   7112 	    }
   7113 	  else
   7114 	    {
   7115 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7116 		 SEGMENT2.  */
   7117 	      extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
   7118 			      - SEGMENT_END (segment, segment->p_vaddr));
   7119 
   7120 	      if (extra_length > 0)
   7121 		{
   7122 		  segment->p_memsz += extra_length;
   7123 		  segment->p_filesz += extra_length;
   7124 		}
   7125 
   7126 	      segment2->p_type = PT_NULL;
   7127 	    }
   7128 	}
   7129     }
   7130 
   7131   /* The second scan attempts to assign sections to segments.  */
   7132   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7133        i < num_segments;
   7134        i++, segment++)
   7135     {
   7136       unsigned int section_count;
   7137       asection **sections;
   7138       asection *output_section;
   7139       unsigned int isec;
   7140       asection *matching_lma;
   7141       asection *suggested_lma;
   7142       unsigned int j;
   7143       size_t amt;
   7144       asection *first_section;
   7145 
   7146       if (segment->p_type == PT_NULL)
   7147 	continue;
   7148 
   7149       first_section = NULL;
   7150       /* Compute how many sections might be placed into this segment.  */
   7151       for (section = ibfd->sections, section_count = 0;
   7152 	   section != NULL;
   7153 	   section = section->next)
   7154 	{
   7155 	  /* Find the first section in the input segment, which may be
   7156 	     removed from the corresponding output segment.   */
   7157 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
   7158 	    {
   7159 	      if (first_section == NULL)
   7160 		first_section = section;
   7161 	      if (section->output_section != NULL)
   7162 		++section_count;
   7163 	    }
   7164 	}
   7165 
   7166       /* Allocate a segment map big enough to contain
   7167 	 all of the sections we have selected.  */
   7168       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7169       amt += section_count * sizeof (asection *);
   7170       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7171       if (map == NULL)
   7172 	return false;
   7173 
   7174       /* Initialise the fields of the segment map.  Default to
   7175 	 using the physical address of the segment in the input BFD.  */
   7176       map->next = NULL;
   7177       map->p_type = segment->p_type;
   7178       map->p_flags = segment->p_flags;
   7179       map->p_flags_valid = 1;
   7180 
   7181       if (map->p_type == PT_LOAD
   7182 	  && (ibfd->flags & D_PAGED) != 0
   7183 	  && maxpagesize > 1
   7184 	  && segment->p_align > 1)
   7185 	{
   7186 	  map->p_align = segment->p_align;
   7187 	  if (segment->p_align > maxpagesize)
   7188 	    map->p_align = maxpagesize;
   7189 	  map->p_align_valid = 1;
   7190 	}
   7191 
   7192       /* If the first section in the input segment is removed, there is
   7193 	 no need to preserve segment physical address in the corresponding
   7194 	 output segment.  */
   7195       if (!first_section || first_section->output_section != NULL)
   7196 	{
   7197 	  map->p_paddr = segment->p_paddr;
   7198 	  map->p_paddr_valid = p_paddr_valid;
   7199 	}
   7200 
   7201       /* Determine if this segment contains the ELF file header
   7202 	 and if it contains the program headers themselves.  */
   7203       map->includes_filehdr = (segment->p_offset == 0
   7204 			       && segment->p_filesz >= iehdr->e_ehsize);
   7205       map->includes_phdrs = 0;
   7206 
   7207       if (!phdr_included || segment->p_type != PT_LOAD)
   7208 	{
   7209 	  map->includes_phdrs =
   7210 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7211 	     && (segment->p_offset + segment->p_filesz
   7212 		 >= ((bfd_vma) iehdr->e_phoff
   7213 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7214 
   7215 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7216 	    phdr_included = true;
   7217 	}
   7218 
   7219       if (section_count == 0)
   7220 	{
   7221 	  /* Special segments, such as the PT_PHDR segment, may contain
   7222 	     no sections, but ordinary, loadable segments should contain
   7223 	     something.  They are allowed by the ELF spec however, so only
   7224 	     a warning is produced.
   7225 	     There is however the valid use case of embedded systems which
   7226 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7227 	     flash memory with zeros.  No warning is shown for that case.  */
   7228 	  if (segment->p_type == PT_LOAD
   7229 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7230 	    /* xgettext:c-format */
   7231 	    _bfd_error_handler
   7232 	      (_("%pB: warning: empty loadable segment detected"
   7233 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7234 	       ibfd, (uint64_t) segment->p_vaddr);
   7235 
   7236 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7237 	  map->count = 0;
   7238 	  *pointer_to_map = map;
   7239 	  pointer_to_map = &map->next;
   7240 
   7241 	  continue;
   7242 	}
   7243 
   7244       /* Now scan the sections in the input BFD again and attempt
   7245 	 to add their corresponding output sections to the segment map.
   7246 	 The problem here is how to handle an output section which has
   7247 	 been moved (ie had its LMA changed).  There are four possibilities:
   7248 
   7249 	 1. None of the sections have been moved.
   7250 	    In this case we can continue to use the segment LMA from the
   7251 	    input BFD.
   7252 
   7253 	 2. All of the sections have been moved by the same amount.
   7254 	    In this case we can change the segment's LMA to match the LMA
   7255 	    of the first section.
   7256 
   7257 	 3. Some of the sections have been moved, others have not.
   7258 	    In this case those sections which have not been moved can be
   7259 	    placed in the current segment which will have to have its size,
   7260 	    and possibly its LMA changed, and a new segment or segments will
   7261 	    have to be created to contain the other sections.
   7262 
   7263 	 4. The sections have been moved, but not by the same amount.
   7264 	    In this case we can change the segment's LMA to match the LMA
   7265 	    of the first section and we will have to create a new segment
   7266 	    or segments to contain the other sections.
   7267 
   7268 	 In order to save time, we allocate an array to hold the section
   7269 	 pointers that we are interested in.  As these sections get assigned
   7270 	 to a segment, they are removed from this array.  */
   7271 
   7272       amt = section_count * sizeof (asection *);
   7273       sections = (asection **) bfd_malloc (amt);
   7274       if (sections == NULL)
   7275 	return false;
   7276 
   7277       /* Step One: Scan for segment vs section LMA conflicts.
   7278 	 Also add the sections to the section array allocated above.
   7279 	 Also add the sections to the current segment.  In the common
   7280 	 case, where the sections have not been moved, this means that
   7281 	 we have completely filled the segment, and there is nothing
   7282 	 more to do.  */
   7283       isec = 0;
   7284       matching_lma = NULL;
   7285       suggested_lma = NULL;
   7286 
   7287       for (section = first_section, j = 0;
   7288 	   section != NULL;
   7289 	   section = section->next)
   7290 	{
   7291 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
   7292 	    {
   7293 	      output_section = section->output_section;
   7294 
   7295 	      sections[j++] = section;
   7296 
   7297 	      /* The Solaris native linker always sets p_paddr to 0.
   7298 		 We try to catch that case here, and set it to the
   7299 		 correct value.  Note - some backends require that
   7300 		 p_paddr be left as zero.  */
   7301 	      if (!p_paddr_valid
   7302 		  && segment->p_vaddr != 0
   7303 		  && !bed->want_p_paddr_set_to_zero
   7304 		  && isec == 0
   7305 		  && output_section->lma != 0
   7306 		  && (align_power (segment->p_vaddr
   7307 				   + (map->includes_filehdr
   7308 				      ? iehdr->e_ehsize : 0)
   7309 				   + (map->includes_phdrs
   7310 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7311 				      : 0),
   7312 				   output_section->alignment_power * opb)
   7313 		      == (output_section->vma * opb)))
   7314 		map->p_paddr = segment->p_vaddr;
   7315 
   7316 	      /* Match up the physical address of the segment with the
   7317 		 LMA address of the output section.  */
   7318 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
   7319 				       opb)
   7320 		  || IS_COREFILE_NOTE (segment, section)
   7321 		  || (bed->want_p_paddr_set_to_zero
   7322 		      && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
   7323 		{
   7324 		  if (matching_lma == NULL
   7325 		      || output_section->lma < matching_lma->lma)
   7326 		    matching_lma = output_section;
   7327 
   7328 		  /* We assume that if the section fits within the segment
   7329 		     then it does not overlap any other section within that
   7330 		     segment.  */
   7331 		  map->sections[isec++] = output_section;
   7332 		}
   7333 	      else if (suggested_lma == NULL)
   7334 		suggested_lma = output_section;
   7335 
   7336 	      if (j == section_count)
   7337 		break;
   7338 	    }
   7339 	}
   7340 
   7341       BFD_ASSERT (j == section_count);
   7342 
   7343       /* Step Two: Adjust the physical address of the current segment,
   7344 	 if necessary.  */
   7345       if (isec == section_count)
   7346 	{
   7347 	  /* All of the sections fitted within the segment as currently
   7348 	     specified.  This is the default case.  Add the segment to
   7349 	     the list of built segments and carry on to process the next
   7350 	     program header in the input BFD.  */
   7351 	  map->count = section_count;
   7352 	  *pointer_to_map = map;
   7353 	  pointer_to_map = &map->next;
   7354 
   7355 	  if (p_paddr_valid
   7356 	      && !bed->want_p_paddr_set_to_zero)
   7357 	    {
   7358 	      bfd_vma hdr_size = 0;
   7359 	      if (map->includes_filehdr)
   7360 		hdr_size = iehdr->e_ehsize;
   7361 	      if (map->includes_phdrs)
   7362 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7363 
   7364 	      /* Account for padding before the first section in the
   7365 		 segment.  */
   7366 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7367 				     - matching_lma->lma);
   7368 	    }
   7369 
   7370 	  free (sections);
   7371 	  continue;
   7372 	}
   7373       else
   7374 	{
   7375 	  /* Change the current segment's physical address to match
   7376 	     the LMA of the first section that fitted, or if no
   7377 	     section fitted, the first section.  */
   7378 	  if (matching_lma == NULL)
   7379 	    matching_lma = suggested_lma;
   7380 
   7381 	  map->p_paddr = matching_lma->lma * opb;
   7382 
   7383 	  /* Offset the segment physical address from the lma
   7384 	     to allow for space taken up by elf headers.  */
   7385 	  if (map->includes_phdrs)
   7386 	    {
   7387 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7388 
   7389 	      /* iehdr->e_phnum is just an estimate of the number
   7390 		 of program headers that we will need.  Make a note
   7391 		 here of the number we used and the segment we chose
   7392 		 to hold these headers, so that we can adjust the
   7393 		 offset when we know the correct value.  */
   7394 	      phdr_adjust_num = iehdr->e_phnum;
   7395 	      phdr_adjust_seg = map;
   7396 	    }
   7397 
   7398 	  if (map->includes_filehdr)
   7399 	    {
   7400 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7401 	      map->p_paddr -= iehdr->e_ehsize;
   7402 	      /* We've subtracted off the size of headers from the
   7403 		 first section lma, but there may have been some
   7404 		 alignment padding before that section too.  Try to
   7405 		 account for that by adjusting the segment lma down to
   7406 		 the same alignment.  */
   7407 	      if (segment->p_align != 0 && segment->p_align < align)
   7408 		align = segment->p_align;
   7409 	      map->p_paddr &= -(align * opb);
   7410 	    }
   7411 	}
   7412 
   7413       /* Step Three: Loop over the sections again, this time assigning
   7414 	 those that fit to the current segment and removing them from the
   7415 	 sections array; but making sure not to leave large gaps.  Once all
   7416 	 possible sections have been assigned to the current segment it is
   7417 	 added to the list of built segments and if sections still remain
   7418 	 to be assigned, a new segment is constructed before repeating
   7419 	 the loop.  */
   7420       isec = 0;
   7421       do
   7422 	{
   7423 	  map->count = 0;
   7424 	  suggested_lma = NULL;
   7425 
   7426 	  /* Fill the current segment with sections that fit.  */
   7427 	  for (j = 0; j < section_count; j++)
   7428 	    {
   7429 	      section = sections[j];
   7430 
   7431 	      if (section == NULL)
   7432 		continue;
   7433 
   7434 	      output_section = section->output_section;
   7435 
   7436 	      BFD_ASSERT (output_section != NULL);
   7437 
   7438 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
   7439 				       opb)
   7440 		  || IS_COREFILE_NOTE (segment, section))
   7441 		{
   7442 		  if (map->count == 0)
   7443 		    {
   7444 		      /* If the first section in a segment does not start at
   7445 			 the beginning of the segment, then something is
   7446 			 wrong.  */
   7447 		      if (align_power (map->p_paddr
   7448 				       + (map->includes_filehdr
   7449 					  ? iehdr->e_ehsize : 0)
   7450 				       + (map->includes_phdrs
   7451 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7452 					  : 0),
   7453 				       output_section->alignment_power * opb)
   7454 			  != output_section->lma * opb)
   7455 			goto sorry;
   7456 		    }
   7457 		  else
   7458 		    {
   7459 		      asection *prev_sec;
   7460 
   7461 		      prev_sec = map->sections[map->count - 1];
   7462 
   7463 		      /* If the gap between the end of the previous section
   7464 			 and the start of this section is more than
   7465 			 maxpagesize then we need to start a new segment.  */
   7466 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7467 				      maxpagesize)
   7468 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7469 			  || (prev_sec->lma + prev_sec->size
   7470 			      > output_section->lma))
   7471 			{
   7472 			  if (suggested_lma == NULL)
   7473 			    suggested_lma = output_section;
   7474 
   7475 			  continue;
   7476 			}
   7477 		    }
   7478 
   7479 		  map->sections[map->count++] = output_section;
   7480 		  ++isec;
   7481 		  sections[j] = NULL;
   7482 		  if (segment->p_type == PT_LOAD)
   7483 		    section->segment_mark = true;
   7484 		}
   7485 	      else if (suggested_lma == NULL)
   7486 		suggested_lma = output_section;
   7487 	    }
   7488 
   7489 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   7490 	     be assigned to any segment - because for example they have a
   7491 	     negative size - or segments that do not contain any sections.
   7492 	     But there are also valid reasons why a segment can be empty.
   7493 	     So allow a count of zero.  */
   7494 
   7495 	  /* Add the current segment to the list of built segments.  */
   7496 	  *pointer_to_map = map;
   7497 	  pointer_to_map = &map->next;
   7498 
   7499 	  if (isec < section_count)
   7500 	    {
   7501 	      /* We still have not allocated all of the sections to
   7502 		 segments.  Create a new segment here, initialise it
   7503 		 and carry on looping.  */
   7504 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7505 	      amt += section_count * sizeof (asection *);
   7506 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7507 	      if (map == NULL)
   7508 		{
   7509 		  free (sections);
   7510 		  return false;
   7511 		}
   7512 
   7513 	      /* Initialise the fields of the segment map.  Set the physical
   7514 		 physical address to the LMA of the first section that has
   7515 		 not yet been assigned.  */
   7516 	      map->next = NULL;
   7517 	      map->p_type = segment->p_type;
   7518 	      map->p_flags = segment->p_flags;
   7519 	      map->p_flags_valid = 1;
   7520 	      map->p_paddr = suggested_lma->lma * opb;
   7521 	      map->p_paddr_valid = p_paddr_valid;
   7522 	      map->includes_filehdr = 0;
   7523 	      map->includes_phdrs = 0;
   7524 	    }
   7525 
   7526 	  continue;
   7527 	sorry:
   7528 	  bfd_set_error (bfd_error_sorry);
   7529 	  free (sections);
   7530 	  return false;
   7531 	}
   7532       while (isec < section_count);
   7533 
   7534       free (sections);
   7535     }
   7536 
   7537   elf_seg_map (obfd) = map_first;
   7538 
   7539   /* If we had to estimate the number of program headers that were
   7540      going to be needed, then check our estimate now and adjust
   7541      the offset if necessary.  */
   7542   if (phdr_adjust_seg != NULL)
   7543     {
   7544       unsigned int count;
   7545 
   7546       for (count = 0, map = map_first; map != NULL; map = map->next)
   7547 	count++;
   7548 
   7549       if (count > phdr_adjust_num)
   7550 	phdr_adjust_seg->p_paddr
   7551 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   7552 
   7553       for (map = map_first; map != NULL; map = map->next)
   7554 	if (map->p_type == PT_PHDR)
   7555 	  {
   7556 	    bfd_vma adjust
   7557 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   7558 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   7559 	    break;
   7560 	  }
   7561     }
   7562 
   7563 #undef SEGMENT_END
   7564 #undef SECTION_SIZE
   7565 #undef IS_CONTAINED_BY_VMA
   7566 #undef IS_CONTAINED_BY_LMA
   7567 #undef IS_NOTE
   7568 #undef IS_COREFILE_NOTE
   7569 #undef IS_SOLARIS_PT_INTERP
   7570 #undef IS_SECTION_IN_INPUT_SEGMENT
   7571 #undef INCLUDE_SECTION_IN_SEGMENT
   7572 #undef SEGMENT_AFTER_SEGMENT
   7573 #undef SEGMENT_OVERLAPS
   7574   return true;
   7575 }
   7576 
   7577 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   7578 
   7579 static bool
   7580 elf_is_p_align_valid (bfd *abfd)
   7581 {
   7582   unsigned int i;
   7583   Elf_Internal_Phdr *segment;
   7584   unsigned int num_segments;
   7585   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7586   bfd_size_type maxpagesize = bed->maxpagesize;
   7587   bfd_size_type p_align = bed->p_align;
   7588 
   7589   /* Return true if the default p_align value isn't set or the maximum
   7590      page size is the same as the minimum page size.  */
   7591   if (p_align == 0 || maxpagesize == bed->minpagesize)
   7592     return true;
   7593 
   7594   /* When the default p_align value is set, p_align may be set to the
   7595      default p_align value while segments are aligned to the maximum
   7596      page size.  In this case, the input p_align will be ignored and
   7597      the maximum page size will be used to align the output segments.  */
   7598   segment = elf_tdata (abfd)->phdr;
   7599   num_segments = elf_elfheader (abfd)->e_phnum;
   7600   for (i = 0; i < num_segments; i++, segment++)
   7601     if (segment->p_type == PT_LOAD
   7602 	&& (segment->p_align != p_align
   7603 	    || vma_page_aligned_bias (segment->p_vaddr,
   7604 				      segment->p_offset,
   7605 				      maxpagesize) != 0))
   7606       return true;
   7607 
   7608   return false;
   7609 }
   7610 
   7611 /* Copy ELF program header information.  */
   7612 
   7613 static bool
   7614 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   7615 {
   7616   Elf_Internal_Ehdr *iehdr;
   7617   struct elf_segment_map *map;
   7618   struct elf_segment_map *map_first;
   7619   struct elf_segment_map **pointer_to_map;
   7620   Elf_Internal_Phdr *segment;
   7621   unsigned int i;
   7622   unsigned int num_segments;
   7623   bool phdr_included = false;
   7624   bool p_paddr_valid;
   7625   bool p_palign_valid;
   7626   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7627 
   7628   iehdr = elf_elfheader (ibfd);
   7629 
   7630   map_first = NULL;
   7631   pointer_to_map = &map_first;
   7632 
   7633   /* If all the segment p_paddr fields are zero, don't set
   7634      map->p_paddr_valid.  */
   7635   p_paddr_valid = false;
   7636   num_segments = elf_elfheader (ibfd)->e_phnum;
   7637   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7638        i < num_segments;
   7639        i++, segment++)
   7640     if (segment->p_paddr != 0)
   7641       {
   7642 	p_paddr_valid = true;
   7643 	break;
   7644       }
   7645 
   7646   p_palign_valid = elf_is_p_align_valid (ibfd);
   7647 
   7648   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7649        i < num_segments;
   7650        i++, segment++)
   7651     {
   7652       asection *section;
   7653       unsigned int section_count;
   7654       size_t amt;
   7655       Elf_Internal_Shdr *this_hdr;
   7656       asection *first_section = NULL;
   7657       asection *lowest_section;
   7658 
   7659       /* Compute how many sections are in this segment.  */
   7660       for (section = ibfd->sections, section_count = 0;
   7661 	   section != NULL;
   7662 	   section = section->next)
   7663 	{
   7664 	  this_hdr = &(elf_section_data(section)->this_hdr);
   7665 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7666 	    {
   7667 	      if (first_section == NULL)
   7668 		first_section = section;
   7669 	      section_count++;
   7670 	    }
   7671 	}
   7672 
   7673       /* Allocate a segment map big enough to contain
   7674 	 all of the sections we have selected.  */
   7675       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7676       amt += section_count * sizeof (asection *);
   7677       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7678       if (map == NULL)
   7679 	return false;
   7680 
   7681       /* Initialize the fields of the output segment map with the
   7682 	 input segment.  */
   7683       map->next = NULL;
   7684       map->p_type = segment->p_type;
   7685       map->p_flags = segment->p_flags;
   7686       map->p_flags_valid = 1;
   7687       map->p_paddr = segment->p_paddr;
   7688       map->p_paddr_valid = p_paddr_valid;
   7689       map->p_align = segment->p_align;
   7690       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   7691       map->p_align_valid = (map->p_type == PT_GNU_STACK
   7692 			    || p_palign_valid);
   7693       map->p_vaddr_offset = 0;
   7694 
   7695       if (map->p_type == PT_GNU_RELRO
   7696 	  || map->p_type == PT_GNU_STACK)
   7697 	{
   7698 	  /* The PT_GNU_RELRO segment may contain the first a few
   7699 	     bytes in the .got.plt section even if the whole .got.plt
   7700 	     section isn't in the PT_GNU_RELRO segment.  We won't
   7701 	     change the size of the PT_GNU_RELRO segment.
   7702 	     Similarly, PT_GNU_STACK size is significant on uclinux
   7703 	     systems.    */
   7704 	  map->p_size = segment->p_memsz;
   7705 	  map->p_size_valid = 1;
   7706 	}
   7707 
   7708       /* Determine if this segment contains the ELF file header
   7709 	 and if it contains the program headers themselves.  */
   7710       map->includes_filehdr = (segment->p_offset == 0
   7711 			       && segment->p_filesz >= iehdr->e_ehsize);
   7712 
   7713       map->includes_phdrs = 0;
   7714       if (! phdr_included || segment->p_type != PT_LOAD)
   7715 	{
   7716 	  map->includes_phdrs =
   7717 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7718 	     && (segment->p_offset + segment->p_filesz
   7719 		 >= ((bfd_vma) iehdr->e_phoff
   7720 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7721 
   7722 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7723 	    phdr_included = true;
   7724 	}
   7725 
   7726       lowest_section = NULL;
   7727       if (section_count != 0)
   7728 	{
   7729 	  unsigned int isec = 0;
   7730 
   7731 	  for (section = first_section;
   7732 	       section != NULL;
   7733 	       section = section->next)
   7734 	    {
   7735 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7736 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7737 		{
   7738 		  map->sections[isec++] = section->output_section;
   7739 		  if ((section->flags & SEC_ALLOC) != 0)
   7740 		    {
   7741 		      bfd_vma seg_off;
   7742 
   7743 		      if (lowest_section == NULL
   7744 			  || section->lma < lowest_section->lma)
   7745 			lowest_section = section;
   7746 
   7747 		      /* Section lmas are set up from PT_LOAD header
   7748 			 p_paddr in _bfd_elf_make_section_from_shdr.
   7749 			 If this header has a p_paddr that disagrees
   7750 			 with the section lma, flag the p_paddr as
   7751 			 invalid.  */
   7752 		      if ((section->flags & SEC_LOAD) != 0)
   7753 			seg_off = this_hdr->sh_offset - segment->p_offset;
   7754 		      else
   7755 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   7756 		      if (section->lma * opb - segment->p_paddr != seg_off)
   7757 			map->p_paddr_valid = false;
   7758 		    }
   7759 		  if (isec == section_count)
   7760 		    break;
   7761 		}
   7762 	    }
   7763 	}
   7764 
   7765       if (section_count == 0)
   7766 	map->p_vaddr_offset = segment->p_vaddr / opb;
   7767       else if (map->p_paddr_valid)
   7768 	{
   7769 	  /* Account for padding before the first section in the segment.  */
   7770 	  bfd_vma hdr_size = 0;
   7771 	  if (map->includes_filehdr)
   7772 	    hdr_size = iehdr->e_ehsize;
   7773 	  if (map->includes_phdrs)
   7774 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7775 
   7776 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7777 				 - (lowest_section ? lowest_section->lma : 0));
   7778 	}
   7779 
   7780       map->count = section_count;
   7781       *pointer_to_map = map;
   7782       pointer_to_map = &map->next;
   7783     }
   7784 
   7785   elf_seg_map (obfd) = map_first;
   7786   return true;
   7787 }
   7788 
   7789 /* Copy private BFD data.  This copies or rewrites ELF program header
   7790    information.  */
   7791 
   7792 static bool
   7793 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   7794 {
   7795   bfd_vma maxpagesize;
   7796 
   7797   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7798       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7799     return true;
   7800 
   7801   if (elf_tdata (ibfd)->phdr == NULL)
   7802     return true;
   7803 
   7804   if (ibfd->xvec == obfd->xvec)
   7805     {
   7806       /* Check to see if any sections in the input BFD
   7807 	 covered by ELF program header have changed.  */
   7808       Elf_Internal_Phdr *segment;
   7809       asection *section, *osec;
   7810       unsigned int i, num_segments;
   7811       Elf_Internal_Shdr *this_hdr;
   7812       const struct elf_backend_data *bed;
   7813 
   7814       bed = get_elf_backend_data (ibfd);
   7815 
   7816       /* Regenerate the segment map if p_paddr is set to 0.  */
   7817       if (bed->want_p_paddr_set_to_zero)
   7818 	goto rewrite;
   7819 
   7820       /* Initialize the segment mark field.  */
   7821       for (section = obfd->sections; section != NULL;
   7822 	   section = section->next)
   7823 	section->segment_mark = false;
   7824 
   7825       num_segments = elf_elfheader (ibfd)->e_phnum;
   7826       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7827 	   i < num_segments;
   7828 	   i++, segment++)
   7829 	{
   7830 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   7831 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   7832 	     which severly confuses things, so always regenerate the segment
   7833 	     map in this case.  */
   7834 	  if (segment->p_paddr == 0
   7835 	      && segment->p_memsz == 0
   7836 	      && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
   7837 	    goto rewrite;
   7838 
   7839 	  for (section = ibfd->sections;
   7840 	       section != NULL; section = section->next)
   7841 	    {
   7842 	      /* We mark the output section so that we know it comes
   7843 		 from the input BFD.  */
   7844 	      osec = section->output_section;
   7845 	      if (osec)
   7846 		osec->segment_mark = true;
   7847 
   7848 	      /* Check if this section is covered by the segment.  */
   7849 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7850 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7851 		{
   7852 		  /* FIXME: Check if its output section is changed or
   7853 		     removed.  What else do we need to check?  */
   7854 		  if (osec == NULL
   7855 		      || section->flags != osec->flags
   7856 		      || section->lma != osec->lma
   7857 		      || section->vma != osec->vma
   7858 		      || section->size != osec->size
   7859 		      || section->rawsize != osec->rawsize
   7860 		      || section->alignment_power != osec->alignment_power)
   7861 		    goto rewrite;
   7862 		}
   7863 	    }
   7864 	}
   7865 
   7866       /* Check to see if any output section do not come from the
   7867 	 input BFD.  */
   7868       for (section = obfd->sections; section != NULL;
   7869 	   section = section->next)
   7870 	{
   7871 	  if (!section->segment_mark)
   7872 	    goto rewrite;
   7873 	  else
   7874 	    section->segment_mark = false;
   7875 	}
   7876 
   7877       return copy_elf_program_header (ibfd, obfd);
   7878     }
   7879 
   7880  rewrite:
   7881   maxpagesize = 0;
   7882   if (ibfd->xvec == obfd->xvec)
   7883     {
   7884       /* When rewriting program header, set the output maxpagesize to
   7885 	 the maximum alignment of input PT_LOAD segments.  */
   7886       Elf_Internal_Phdr *segment;
   7887       unsigned int i;
   7888       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   7889 
   7890       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7891 	   i < num_segments;
   7892 	   i++, segment++)
   7893 	if (segment->p_type == PT_LOAD
   7894 	    && maxpagesize < segment->p_align)
   7895 	  {
   7896 	    /* PR 17512: file: f17299af.  */
   7897 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   7898 	      /* xgettext:c-format */
   7899 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   7900 				    PRIx64 " is too large"),
   7901 				  ibfd, (uint64_t) segment->p_align);
   7902 	    else
   7903 	      maxpagesize = segment->p_align;
   7904 	  }
   7905     }
   7906   if (maxpagesize == 0)
   7907     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   7908 
   7909   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   7910 }
   7911 
   7912 /* Initialize private output section information from input section.  */
   7913 
   7914 bool
   7915 _bfd_elf_init_private_section_data (bfd *ibfd,
   7916 				    asection *isec,
   7917 				    bfd *obfd,
   7918 				    asection *osec,
   7919 				    struct bfd_link_info *link_info)
   7920 
   7921 {
   7922   Elf_Internal_Shdr *ihdr, *ohdr;
   7923   bool final_link = (link_info != NULL
   7924 		     && !bfd_link_relocatable (link_info));
   7925 
   7926   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7927       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7928     return true;
   7929 
   7930   BFD_ASSERT (elf_section_data (osec) != NULL);
   7931 
   7932   /* If this is a known ABI section, ELF section type and flags may
   7933      have been set up when OSEC was created.  For normal sections we
   7934      allow the user to override the type and flags other than
   7935      SHF_MASKOS and SHF_MASKPROC.  */
   7936   if (elf_section_type (osec) == SHT_PROGBITS
   7937       || elf_section_type (osec) == SHT_NOTE
   7938       || elf_section_type (osec) == SHT_NOBITS)
   7939     elf_section_type (osec) = SHT_NULL;
   7940   /* For objcopy and relocatable link, copy the ELF section type from
   7941      the input file if the BFD section flags are the same.  (If they
   7942      are different the user may be doing something like
   7943      "objcopy --set-section-flags .text=alloc,data".)  For a final
   7944      link allow some flags that the linker clears to differ.  */
   7945   if (elf_section_type (osec) == SHT_NULL
   7946       && (osec->flags == isec->flags
   7947 	  || (final_link
   7948 	      && ((osec->flags ^ isec->flags)
   7949 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   7950     elf_section_type (osec) = elf_section_type (isec);
   7951 
   7952   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   7953   elf_section_flags (osec) = (elf_section_flags (isec)
   7954 			      & (SHF_MASKOS | SHF_MASKPROC));
   7955 
   7956   /* Copy sh_info from input for mbind section.  */
   7957   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   7958       && elf_section_flags (isec) & SHF_GNU_MBIND)
   7959     elf_section_data (osec)->this_hdr.sh_info
   7960       = elf_section_data (isec)->this_hdr.sh_info;
   7961 
   7962   /* Set things up for objcopy and relocatable link.  The output
   7963      SHT_GROUP section will have its elf_next_in_group pointing back
   7964      to the input group members.  Ignore linker created group section.
   7965      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   7966   if ((link_info == NULL
   7967        || !link_info->resolve_section_groups)
   7968       && (elf_sec_group (isec) == NULL
   7969 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   7970     {
   7971       if (elf_section_flags (isec) & SHF_GROUP)
   7972 	elf_section_flags (osec) |= SHF_GROUP;
   7973       elf_next_in_group (osec) = elf_next_in_group (isec);
   7974       elf_section_data (osec)->group = elf_section_data (isec)->group;
   7975     }
   7976 
   7977   /* If not decompress, preserve SHF_COMPRESSED.  */
   7978   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   7979     elf_section_flags (osec) |= (elf_section_flags (isec)
   7980 				 & SHF_COMPRESSED);
   7981 
   7982   ihdr = &elf_section_data (isec)->this_hdr;
   7983 
   7984   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   7985      don't use the output section of the linked-to section since it
   7986      may be NULL at this point.  */
   7987   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   7988     {
   7989       ohdr = &elf_section_data (osec)->this_hdr;
   7990       ohdr->sh_flags |= SHF_LINK_ORDER;
   7991       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   7992     }
   7993 
   7994   osec->use_rela_p = isec->use_rela_p;
   7995 
   7996   return true;
   7997 }
   7998 
   7999 /* Copy private section information.  This copies over the entsize
   8000    field, and sometimes the info field.  */
   8001 
   8002 bool
   8003 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8004 				    asection *isec,
   8005 				    bfd *obfd,
   8006 				    asection *osec)
   8007 {
   8008   Elf_Internal_Shdr *ihdr, *ohdr;
   8009 
   8010   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8011       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8012     return true;
   8013 
   8014   ihdr = &elf_section_data (isec)->this_hdr;
   8015   ohdr = &elf_section_data (osec)->this_hdr;
   8016 
   8017   ohdr->sh_entsize = ihdr->sh_entsize;
   8018 
   8019   if (ihdr->sh_type == SHT_SYMTAB
   8020       || ihdr->sh_type == SHT_DYNSYM
   8021       || ihdr->sh_type == SHT_GNU_verneed
   8022       || ihdr->sh_type == SHT_GNU_verdef)
   8023     ohdr->sh_info = ihdr->sh_info;
   8024 
   8025   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   8026 					     NULL);
   8027 }
   8028 
   8029 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8030    necessary if we are removing either the SHT_GROUP section or any of
   8031    the group member sections.  DISCARDED is the value that a section's
   8032    output_section has if the section will be discarded, NULL when this
   8033    function is called from objcopy, bfd_abs_section_ptr when called
   8034    from the linker.  */
   8035 
   8036 bool
   8037 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8038 {
   8039   asection *isec;
   8040 
   8041   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8042     if (elf_section_type (isec) == SHT_GROUP)
   8043       {
   8044 	asection *first = elf_next_in_group (isec);
   8045 	asection *s = first;
   8046 	bfd_size_type removed = 0;
   8047 
   8048 	while (s != NULL)
   8049 	  {
   8050 	    /* If this member section is being output but the
   8051 	       SHT_GROUP section is not, then clear the group info
   8052 	       set up by _bfd_elf_copy_private_section_data.  */
   8053 	    if (s->output_section != discarded
   8054 		&& isec->output_section == discarded)
   8055 	      {
   8056 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8057 		elf_group_name (s->output_section) = NULL;
   8058 	      }
   8059 	    else
   8060 	      {
   8061 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8062 		if (s->output_section == discarded
   8063 		    && isec->output_section != discarded)
   8064 		  {
   8065 		    /* Conversely, if the member section is not being
   8066 		       output but the SHT_GROUP section is, then adjust
   8067 		       its size.  */
   8068 		    removed += 4;
   8069 		    if (elf_sec->rel.hdr != NULL
   8070 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8071 		      removed += 4;
   8072 		    if (elf_sec->rela.hdr != NULL
   8073 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8074 		      removed += 4;
   8075 		  }
   8076 		else
   8077 		  {
   8078 		    /* Also adjust for zero-sized relocation member
   8079 		       section.  */
   8080 		    if (elf_sec->rel.hdr != NULL
   8081 			&& elf_sec->rel.hdr->sh_size == 0)
   8082 		      removed += 4;
   8083 		    if (elf_sec->rela.hdr != NULL
   8084 			&& elf_sec->rela.hdr->sh_size == 0)
   8085 		      removed += 4;
   8086 		  }
   8087 	      }
   8088 	    s = elf_next_in_group (s);
   8089 	    if (s == first)
   8090 	      break;
   8091 	  }
   8092 	if (removed != 0)
   8093 	  {
   8094 	    if (discarded != NULL)
   8095 	      {
   8096 		/* If we've been called for ld -r, then we need to
   8097 		   adjust the input section size.  */
   8098 		if (isec->rawsize == 0)
   8099 		  isec->rawsize = isec->size;
   8100 		isec->size = isec->rawsize - removed;
   8101 		if (isec->size <= 4)
   8102 		  {
   8103 		    isec->size = 0;
   8104 		    isec->flags |= SEC_EXCLUDE;
   8105 		  }
   8106 	      }
   8107 	    else if (isec->output_section != NULL)
   8108 	      {
   8109 		/* Adjust the output section size when called from
   8110 		   objcopy. */
   8111 		isec->output_section->size -= removed;
   8112 		if (isec->output_section->size <= 4)
   8113 		  {
   8114 		    isec->output_section->size = 0;
   8115 		    isec->output_section->flags |= SEC_EXCLUDE;
   8116 		  }
   8117 	      }
   8118 	  }
   8119       }
   8120 
   8121   return true;
   8122 }
   8123 
   8124 /* Copy private header information.  */
   8125 
   8126 bool
   8127 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8128 {
   8129   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8130       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8131     return true;
   8132 
   8133   /* Copy over private BFD data if it has not already been copied.
   8134      This must be done here, rather than in the copy_private_bfd_data
   8135      entry point, because the latter is called after the section
   8136      contents have been set, which means that the program headers have
   8137      already been worked out.  */
   8138   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8139     {
   8140       if (! copy_private_bfd_data (ibfd, obfd))
   8141 	return false;
   8142     }
   8143 
   8144   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8145 }
   8146 
   8147 /* Copy private symbol information.  If this symbol is in a section
   8148    which we did not map into a BFD section, try to map the section
   8149    index correctly.  We use special macro definitions for the mapped
   8150    section indices; these definitions are interpreted by the
   8151    swap_out_syms function.  */
   8152 
   8153 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8154 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8155 #define MAP_STRTAB    (SHN_HIOS + 3)
   8156 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8157 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8158 
   8159 bool
   8160 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8161 				   asymbol *isymarg,
   8162 				   bfd *obfd,
   8163 				   asymbol *osymarg)
   8164 {
   8165   elf_symbol_type *isym, *osym;
   8166 
   8167   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8168       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8169     return true;
   8170 
   8171   isym = elf_symbol_from (isymarg);
   8172   osym = elf_symbol_from (osymarg);
   8173 
   8174   if (isym != NULL
   8175       && isym->internal_elf_sym.st_shndx != 0
   8176       && osym != NULL
   8177       && bfd_is_abs_section (isym->symbol.section))
   8178     {
   8179       unsigned int shndx;
   8180 
   8181       shndx = isym->internal_elf_sym.st_shndx;
   8182       if (shndx == elf_onesymtab (ibfd))
   8183 	shndx = MAP_ONESYMTAB;
   8184       else if (shndx == elf_dynsymtab (ibfd))
   8185 	shndx = MAP_DYNSYMTAB;
   8186       else if (shndx == elf_strtab_sec (ibfd))
   8187 	shndx = MAP_STRTAB;
   8188       else if (shndx == elf_shstrtab_sec (ibfd))
   8189 	shndx = MAP_SHSTRTAB;
   8190       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8191 	shndx = MAP_SYM_SHNDX;
   8192       osym->internal_elf_sym.st_shndx = shndx;
   8193     }
   8194 
   8195   return true;
   8196 }
   8197 
   8198 /* Swap out the symbols.  */
   8199 
   8200 static bool
   8201 swap_out_syms (bfd *abfd,
   8202 	       struct elf_strtab_hash **sttp,
   8203 	       int relocatable_p,
   8204 	       struct bfd_link_info *info)
   8205 {
   8206   const struct elf_backend_data *bed;
   8207   unsigned int symcount;
   8208   asymbol **syms;
   8209   struct elf_strtab_hash *stt;
   8210   Elf_Internal_Shdr *symtab_hdr;
   8211   Elf_Internal_Shdr *symtab_shndx_hdr;
   8212   Elf_Internal_Shdr *symstrtab_hdr;
   8213   struct elf_sym_strtab *symstrtab;
   8214   bfd_byte *outbound_syms;
   8215   bfd_byte *outbound_shndx;
   8216   unsigned long outbound_syms_index;
   8217   unsigned int idx;
   8218   unsigned int num_locals;
   8219   size_t amt;
   8220   bool name_local_sections;
   8221 
   8222   if (!elf_map_symbols (abfd, &num_locals))
   8223     return false;
   8224 
   8225   /* Dump out the symtabs.  */
   8226   stt = _bfd_elf_strtab_init ();
   8227   if (stt == NULL)
   8228     return false;
   8229 
   8230   bed = get_elf_backend_data (abfd);
   8231   symcount = bfd_get_symcount (abfd);
   8232   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8233   symtab_hdr->sh_type = SHT_SYMTAB;
   8234   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8235   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8236   symtab_hdr->sh_info = num_locals + 1;
   8237   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8238 
   8239   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8240   symstrtab_hdr->sh_type = SHT_STRTAB;
   8241 
   8242   /* Allocate buffer to swap out the .strtab section.  */
   8243   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8244       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8245     {
   8246       bfd_set_error (bfd_error_no_memory);
   8247       _bfd_elf_strtab_free (stt);
   8248       return false;
   8249     }
   8250 
   8251   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8252       || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
   8253     {
   8254     error_no_mem:
   8255       bfd_set_error (bfd_error_no_memory);
   8256     error_return:
   8257       free (symstrtab);
   8258       _bfd_elf_strtab_free (stt);
   8259       return false;
   8260     }
   8261   symtab_hdr->contents = outbound_syms;
   8262   outbound_syms_index = 0;
   8263 
   8264   outbound_shndx = NULL;
   8265 
   8266   if (elf_symtab_shndx_list (abfd))
   8267     {
   8268       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8269       if (symtab_shndx_hdr->sh_name != 0)
   8270 	{
   8271 	  if (_bfd_mul_overflow (symcount + 1,
   8272 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8273 	    goto error_no_mem;
   8274 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8275 	  if (outbound_shndx == NULL)
   8276 	    goto error_return;
   8277 
   8278 	  symtab_shndx_hdr->contents = outbound_shndx;
   8279 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8280 	  symtab_shndx_hdr->sh_size = amt;
   8281 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8282 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8283 	}
   8284       /* FIXME: What about any other headers in the list ?  */
   8285     }
   8286 
   8287   /* Now generate the data (for "contents").  */
   8288   {
   8289     /* Fill in zeroth symbol and swap it out.  */
   8290     Elf_Internal_Sym sym;
   8291     sym.st_name = 0;
   8292     sym.st_value = 0;
   8293     sym.st_size = 0;
   8294     sym.st_info = 0;
   8295     sym.st_other = 0;
   8296     sym.st_shndx = SHN_UNDEF;
   8297     sym.st_target_internal = 0;
   8298     symstrtab[0].sym = sym;
   8299     symstrtab[0].dest_index = outbound_syms_index;
   8300     outbound_syms_index++;
   8301   }
   8302 
   8303   name_local_sections
   8304     = (bed->elf_backend_name_local_section_symbols
   8305        && bed->elf_backend_name_local_section_symbols (abfd));
   8306 
   8307   syms = bfd_get_outsymbols (abfd);
   8308   for (idx = 0; idx < symcount;)
   8309     {
   8310       Elf_Internal_Sym sym;
   8311       bfd_vma value = syms[idx]->value;
   8312       elf_symbol_type *type_ptr;
   8313       flagword flags = syms[idx]->flags;
   8314       int type;
   8315 
   8316       if (!name_local_sections
   8317 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8318 	{
   8319 	  /* Local section symbols have no name.  */
   8320 	  sym.st_name = (unsigned long) -1;
   8321 	}
   8322       else
   8323 	{
   8324 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8325 	     to get the final offset for st_name.  */
   8326 	  sym.st_name
   8327 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   8328 						   false);
   8329 	  if (sym.st_name == (unsigned long) -1)
   8330 	    goto error_return;
   8331 	}
   8332 
   8333       type_ptr = elf_symbol_from (syms[idx]);
   8334 
   8335       if ((flags & BSF_SECTION_SYM) == 0
   8336 	  && bfd_is_com_section (syms[idx]->section))
   8337 	{
   8338 	  /* ELF common symbols put the alignment into the `value' field,
   8339 	     and the size into the `size' field.  This is backwards from
   8340 	     how BFD handles it, so reverse it here.  */
   8341 	  sym.st_size = value;
   8342 	  if (type_ptr == NULL
   8343 	      || type_ptr->internal_elf_sym.st_value == 0)
   8344 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8345 	  else
   8346 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8347 	  sym.st_shndx = _bfd_elf_section_from_bfd_section
   8348 	    (abfd, syms[idx]->section);
   8349 	}
   8350       else
   8351 	{
   8352 	  asection *sec = syms[idx]->section;
   8353 	  unsigned int shndx;
   8354 
   8355 	  if (sec->output_section)
   8356 	    {
   8357 	      value += sec->output_offset;
   8358 	      sec = sec->output_section;
   8359 	    }
   8360 
   8361 	  /* Don't add in the section vma for relocatable output.  */
   8362 	  if (! relocatable_p)
   8363 	    value += sec->vma;
   8364 	  sym.st_value = value;
   8365 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8366 
   8367 	  if (bfd_is_abs_section (sec)
   8368 	      && type_ptr != NULL
   8369 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8370 	    {
   8371 	      /* This symbol is in a real ELF section which we did
   8372 		 not create as a BFD section.  Undo the mapping done
   8373 		 by copy_private_symbol_data.  */
   8374 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8375 	      switch (shndx)
   8376 		{
   8377 		case MAP_ONESYMTAB:
   8378 		  shndx = elf_onesymtab (abfd);
   8379 		  break;
   8380 		case MAP_DYNSYMTAB:
   8381 		  shndx = elf_dynsymtab (abfd);
   8382 		  break;
   8383 		case MAP_STRTAB:
   8384 		  shndx = elf_strtab_sec (abfd);
   8385 		  break;
   8386 		case MAP_SHSTRTAB:
   8387 		  shndx = elf_shstrtab_sec (abfd);
   8388 		  break;
   8389 		case MAP_SYM_SHNDX:
   8390 		  if (elf_symtab_shndx_list (abfd))
   8391 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8392 		  break;
   8393 		case SHN_COMMON:
   8394 		case SHN_ABS:
   8395 		  shndx = SHN_ABS;
   8396 		  break;
   8397 		default:
   8398 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8399 		    {
   8400 		      if (bed->symbol_section_index)
   8401 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8402 		      /* Otherwise just leave the index alone.  */
   8403 		    }
   8404 		  else
   8405 		    {
   8406 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   8407 			_bfd_error_handler (_("%pB: \
   8408 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   8409 					  abfd, shndx);
   8410 		      shndx = SHN_ABS;
   8411 		    }
   8412 		  break;
   8413 		}
   8414 	    }
   8415 	  else
   8416 	    {
   8417 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8418 
   8419 	      if (shndx == SHN_BAD)
   8420 		{
   8421 		  asection *sec2;
   8422 
   8423 		  /* Writing this would be a hell of a lot easier if
   8424 		     we had some decent documentation on bfd, and
   8425 		     knew what to expect of the library, and what to
   8426 		     demand of applications.  For example, it
   8427 		     appears that `objcopy' might not set the
   8428 		     section of a symbol to be a section that is
   8429 		     actually in the output file.  */
   8430 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   8431 		  if (sec2 != NULL)
   8432 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   8433 		  if (shndx == SHN_BAD)
   8434 		    {
   8435 		      /* xgettext:c-format */
   8436 		      _bfd_error_handler
   8437 			(_("unable to find equivalent output section"
   8438 			   " for symbol '%s' from section '%s'"),
   8439 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   8440 			 sec->name);
   8441 		      bfd_set_error (bfd_error_invalid_operation);
   8442 		      goto error_return;
   8443 		    }
   8444 		}
   8445 	    }
   8446 
   8447 	  sym.st_shndx = shndx;
   8448 	}
   8449 
   8450       if ((flags & BSF_THREAD_LOCAL) != 0)
   8451 	type = STT_TLS;
   8452       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8453 	type = STT_GNU_IFUNC;
   8454       else if ((flags & BSF_FUNCTION) != 0)
   8455 	type = STT_FUNC;
   8456       else if ((flags & BSF_OBJECT) != 0)
   8457 	type = STT_OBJECT;
   8458       else if ((flags & BSF_RELC) != 0)
   8459 	type = STT_RELC;
   8460       else if ((flags & BSF_SRELC) != 0)
   8461 	type = STT_SRELC;
   8462       else
   8463 	type = STT_NOTYPE;
   8464 
   8465       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8466 	type = STT_TLS;
   8467 
   8468       /* Processor-specific types.  */
   8469       if (type_ptr != NULL
   8470 	  && bed->elf_backend_get_symbol_type)
   8471 	type = ((*bed->elf_backend_get_symbol_type)
   8472 		(&type_ptr->internal_elf_sym, type));
   8473 
   8474       if (flags & BSF_SECTION_SYM)
   8475 	{
   8476 	  if (flags & BSF_GLOBAL)
   8477 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   8478 	  else
   8479 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   8480 	}
   8481       else if (bfd_is_com_section (syms[idx]->section))
   8482 	{
   8483 	  if (type != STT_TLS)
   8484 	    {
   8485 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   8486 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   8487 			? STT_COMMON : STT_OBJECT);
   8488 	      else
   8489 		type = ((flags & BSF_ELF_COMMON) != 0
   8490 			? STT_COMMON : STT_OBJECT);
   8491 	    }
   8492 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   8493 	}
   8494       else if (bfd_is_und_section (syms[idx]->section))
   8495 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   8496 				    ? STB_WEAK
   8497 				    : STB_GLOBAL),
   8498 				   type);
   8499       else if (flags & BSF_FILE)
   8500 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   8501       else
   8502 	{
   8503 	  int bind = STB_LOCAL;
   8504 
   8505 	  if (flags & BSF_LOCAL)
   8506 	    bind = STB_LOCAL;
   8507 	  else if (flags & BSF_GNU_UNIQUE)
   8508 	    bind = STB_GNU_UNIQUE;
   8509 	  else if (flags & BSF_WEAK)
   8510 	    bind = STB_WEAK;
   8511 	  else if (flags & BSF_GLOBAL)
   8512 	    bind = STB_GLOBAL;
   8513 
   8514 	  sym.st_info = ELF_ST_INFO (bind, type);
   8515 	}
   8516 
   8517       if (type_ptr != NULL)
   8518 	{
   8519 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   8520 	  sym.st_target_internal
   8521 	    = type_ptr->internal_elf_sym.st_target_internal;
   8522 	}
   8523       else
   8524 	{
   8525 	  sym.st_other = 0;
   8526 	  sym.st_target_internal = 0;
   8527 	}
   8528 
   8529       idx++;
   8530       symstrtab[idx].sym = sym;
   8531       symstrtab[idx].dest_index = outbound_syms_index;
   8532 
   8533       outbound_syms_index++;
   8534     }
   8535 
   8536   /* Finalize the .strtab section.  */
   8537   _bfd_elf_strtab_finalize (stt);
   8538 
   8539   /* Swap out the .strtab section.  */
   8540   for (idx = 0; idx <= symcount; idx++)
   8541     {
   8542       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   8543       if (elfsym->sym.st_name == (unsigned long) -1)
   8544 	elfsym->sym.st_name = 0;
   8545       else
   8546 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   8547 						      elfsym->sym.st_name);
   8548       if (info && info->callbacks->ctf_new_symbol)
   8549 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   8550 					 &elfsym->sym);
   8551 
   8552       /* Inform the linker of the addition of this symbol.  */
   8553 
   8554       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   8555 			       (outbound_syms
   8556 				+ (elfsym->dest_index
   8557 				   * bed->s->sizeof_sym)),
   8558 			       NPTR_ADD (outbound_shndx,
   8559 					 (elfsym->dest_index
   8560 					  * sizeof (Elf_External_Sym_Shndx))));
   8561     }
   8562   free (symstrtab);
   8563 
   8564   *sttp = stt;
   8565   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   8566   symstrtab_hdr->sh_type = SHT_STRTAB;
   8567   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   8568   symstrtab_hdr->sh_addr = 0;
   8569   symstrtab_hdr->sh_entsize = 0;
   8570   symstrtab_hdr->sh_link = 0;
   8571   symstrtab_hdr->sh_info = 0;
   8572   symstrtab_hdr->sh_addralign = 1;
   8573 
   8574   return true;
   8575 }
   8576 
   8577 /* Return the number of bytes required to hold the symtab vector.
   8578 
   8579    Note that we base it on the count plus 1, since we will null terminate
   8580    the vector allocated based on this size.  However, the ELF symbol table
   8581    always has a dummy entry as symbol #0, so it ends up even.  */
   8582 
   8583 long
   8584 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   8585 {
   8586   bfd_size_type symcount;
   8587   long symtab_size;
   8588   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   8589 
   8590   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8591   if (symcount > LONG_MAX / sizeof (asymbol *))
   8592     {
   8593       bfd_set_error (bfd_error_file_too_big);
   8594       return -1;
   8595     }
   8596   symtab_size = symcount * (sizeof (asymbol *));
   8597   if (symcount == 0)
   8598     symtab_size = sizeof (asymbol *);
   8599   else if (!bfd_write_p (abfd))
   8600     {
   8601       ufile_ptr filesize = bfd_get_file_size (abfd);
   8602 
   8603       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   8604 	{
   8605 	  bfd_set_error (bfd_error_file_truncated);
   8606 	  return -1;
   8607 	}
   8608     }
   8609 
   8610   return symtab_size;
   8611 }
   8612 
   8613 long
   8614 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   8615 {
   8616   bfd_size_type symcount;
   8617   long symtab_size;
   8618   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   8619 
   8620   if (elf_dynsymtab (abfd) == 0)
   8621     {
   8622       bfd_set_error (bfd_error_invalid_operation);
   8623       return -1;
   8624     }
   8625 
   8626   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8627   if (symcount > LONG_MAX / sizeof (asymbol *))
   8628     {
   8629       bfd_set_error (bfd_error_file_too_big);
   8630       return -1;
   8631     }
   8632   symtab_size = symcount * (sizeof (asymbol *));
   8633   if (symcount == 0)
   8634     symtab_size = sizeof (asymbol *);
   8635   else if (!bfd_write_p (abfd))
   8636     {
   8637       ufile_ptr filesize = bfd_get_file_size (abfd);
   8638 
   8639       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   8640 	{
   8641 	  bfd_set_error (bfd_error_file_truncated);
   8642 	  return -1;
   8643 	}
   8644     }
   8645 
   8646   return symtab_size;
   8647 }
   8648 
   8649 long
   8650 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   8651 {
   8652   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   8653     {
   8654       /* Sanity check reloc section size.  */
   8655       struct bfd_elf_section_data *d = elf_section_data (asect);
   8656       Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
   8657       bfd_size_type ext_rel_size = rel_hdr->sh_size;
   8658       ufile_ptr filesize = bfd_get_file_size (abfd);
   8659 
   8660       if (filesize != 0 && ext_rel_size > filesize)
   8661 	{
   8662 	  bfd_set_error (bfd_error_file_truncated);
   8663 	  return -1;
   8664 	}
   8665     }
   8666 
   8667 #if SIZEOF_LONG == SIZEOF_INT
   8668   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   8669     {
   8670       bfd_set_error (bfd_error_file_too_big);
   8671       return -1;
   8672     }
   8673 #endif
   8674   return (asect->reloc_count + 1L) * sizeof (arelent *);
   8675 }
   8676 
   8677 /* Canonicalize the relocs.  */
   8678 
   8679 long
   8680 _bfd_elf_canonicalize_reloc (bfd *abfd,
   8681 			     sec_ptr section,
   8682 			     arelent **relptr,
   8683 			     asymbol **symbols)
   8684 {
   8685   arelent *tblptr;
   8686   unsigned int i;
   8687   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8688 
   8689   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   8690     return -1;
   8691 
   8692   tblptr = section->relocation;
   8693   for (i = 0; i < section->reloc_count; i++)
   8694     *relptr++ = tblptr++;
   8695 
   8696   *relptr = NULL;
   8697 
   8698   return section->reloc_count;
   8699 }
   8700 
   8701 long
   8702 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   8703 {
   8704   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8705   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   8706 
   8707   if (symcount >= 0)
   8708     abfd->symcount = symcount;
   8709   return symcount;
   8710 }
   8711 
   8712 long
   8713 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   8714 				      asymbol **allocation)
   8715 {
   8716   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8717   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   8718 
   8719   if (symcount >= 0)
   8720     abfd->dynsymcount = symcount;
   8721   return symcount;
   8722 }
   8723 
   8724 /* Return the size required for the dynamic reloc entries.  Any loadable
   8725    section that was actually installed in the BFD, and has type SHT_REL
   8726    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   8727    dynamic reloc section.  */
   8728 
   8729 long
   8730 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   8731 {
   8732   bfd_size_type count, ext_rel_size;
   8733   asection *s;
   8734 
   8735   if (elf_dynsymtab (abfd) == 0)
   8736     {
   8737       bfd_set_error (bfd_error_invalid_operation);
   8738       return -1;
   8739     }
   8740 
   8741   count = 1;
   8742   ext_rel_size = 0;
   8743   for (s = abfd->sections; s != NULL; s = s->next)
   8744     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8745 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8746 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8747       {
   8748 	ext_rel_size += s->size;
   8749 	if (ext_rel_size < s->size)
   8750 	  {
   8751 	    bfd_set_error (bfd_error_file_truncated);
   8752 	    return -1;
   8753 	  }
   8754 	count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8755 	if (count > LONG_MAX / sizeof (arelent *))
   8756 	  {
   8757 	    bfd_set_error (bfd_error_file_too_big);
   8758 	    return -1;
   8759 	  }
   8760       }
   8761   if (count > 1 && !bfd_write_p (abfd))
   8762     {
   8763       /* Sanity check reloc section sizes.  */
   8764       ufile_ptr filesize = bfd_get_file_size (abfd);
   8765       if (filesize != 0 && ext_rel_size > filesize)
   8766 	{
   8767 	  bfd_set_error (bfd_error_file_truncated);
   8768 	  return -1;
   8769 	}
   8770     }
   8771   return count * sizeof (arelent *);
   8772 }
   8773 
   8774 /* Canonicalize the dynamic relocation entries.  Note that we return the
   8775    dynamic relocations as a single block, although they are actually
   8776    associated with particular sections; the interface, which was
   8777    designed for SunOS style shared libraries, expects that there is only
   8778    one set of dynamic relocs.  Any loadable section that was actually
   8779    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   8780    dynamic symbol table, is considered to be a dynamic reloc section.  */
   8781 
   8782 long
   8783 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   8784 				     arelent **storage,
   8785 				     asymbol **syms)
   8786 {
   8787   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   8788   asection *s;
   8789   long ret;
   8790 
   8791   if (elf_dynsymtab (abfd) == 0)
   8792     {
   8793       bfd_set_error (bfd_error_invalid_operation);
   8794       return -1;
   8795     }
   8796 
   8797   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   8798   ret = 0;
   8799   for (s = abfd->sections; s != NULL; s = s->next)
   8800     {
   8801       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8802 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8803 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8804 	{
   8805 	  arelent *p;
   8806 	  long count, i;
   8807 
   8808 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   8809 	    return -1;
   8810 	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8811 	  p = s->relocation;
   8812 	  for (i = 0; i < count; i++)
   8813 	    *storage++ = p++;
   8814 	  ret += count;
   8815 	}
   8816     }
   8817 
   8818   *storage = NULL;
   8819 
   8820   return ret;
   8821 }
   8822 
   8823 /* Read in the version information.  */
   8825 
   8826 bool
   8827 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   8828 {
   8829   bfd_byte *contents = NULL;
   8830   unsigned int freeidx = 0;
   8831   size_t amt;
   8832 
   8833   if (elf_dynverref (abfd) != 0)
   8834     {
   8835       Elf_Internal_Shdr *hdr;
   8836       Elf_External_Verneed *everneed;
   8837       Elf_Internal_Verneed *iverneed;
   8838       unsigned int i;
   8839       bfd_byte *contents_end;
   8840 
   8841       hdr = &elf_tdata (abfd)->dynverref_hdr;
   8842 
   8843       if (hdr->sh_info == 0
   8844 	  || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   8845 	{
   8846 	error_return_bad_verref:
   8847 	  _bfd_error_handler
   8848 	    (_("%pB: .gnu.version_r invalid entry"), abfd);
   8849 	  bfd_set_error (bfd_error_bad_value);
   8850 	error_return_verref:
   8851 	  elf_tdata (abfd)->verref = NULL;
   8852 	  elf_tdata (abfd)->cverrefs = 0;
   8853 	  goto error_return;
   8854 	}
   8855 
   8856       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   8857 	goto error_return_verref;
   8858       contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   8859       if (contents == NULL)
   8860 	goto error_return_verref;
   8861 
   8862       if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
   8863 	{
   8864 	  bfd_set_error (bfd_error_file_too_big);
   8865 	  goto error_return_verref;
   8866 	}
   8867       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
   8868       if (elf_tdata (abfd)->verref == NULL)
   8869 	goto error_return_verref;
   8870 
   8871       BFD_ASSERT (sizeof (Elf_External_Verneed)
   8872 		  == sizeof (Elf_External_Vernaux));
   8873       contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
   8874       everneed = (Elf_External_Verneed *) contents;
   8875       iverneed = elf_tdata (abfd)->verref;
   8876       for (i = 0; i < hdr->sh_info; i++, iverneed++)
   8877 	{
   8878 	  Elf_External_Vernaux *evernaux;
   8879 	  Elf_Internal_Vernaux *ivernaux;
   8880 	  unsigned int j;
   8881 
   8882 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   8883 
   8884 	  iverneed->vn_bfd = abfd;
   8885 
   8886 	  iverneed->vn_filename =
   8887 	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8888 					     iverneed->vn_file);
   8889 	  if (iverneed->vn_filename == NULL)
   8890 	    goto error_return_bad_verref;
   8891 
   8892 	  if (iverneed->vn_cnt == 0)
   8893 	    iverneed->vn_auxptr = NULL;
   8894 	  else
   8895 	    {
   8896 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   8897 				     sizeof (Elf_Internal_Vernaux), &amt))
   8898 		{
   8899 		  bfd_set_error (bfd_error_file_too_big);
   8900 		  goto error_return_verref;
   8901 		}
   8902 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   8903 		bfd_alloc (abfd, amt);
   8904 	      if (iverneed->vn_auxptr == NULL)
   8905 		goto error_return_verref;
   8906 	    }
   8907 
   8908 	  if (iverneed->vn_aux
   8909 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8910 	    goto error_return_bad_verref;
   8911 
   8912 	  evernaux = ((Elf_External_Vernaux *)
   8913 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   8914 	  ivernaux = iverneed->vn_auxptr;
   8915 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   8916 	    {
   8917 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   8918 
   8919 	      ivernaux->vna_nodename =
   8920 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8921 						 ivernaux->vna_name);
   8922 	      if (ivernaux->vna_nodename == NULL)
   8923 		goto error_return_bad_verref;
   8924 
   8925 	      if (ivernaux->vna_other > freeidx)
   8926 		freeidx = ivernaux->vna_other;
   8927 
   8928 	      ivernaux->vna_nextptr = NULL;
   8929 	      if (ivernaux->vna_next == 0)
   8930 		{
   8931 		  iverneed->vn_cnt = j + 1;
   8932 		  break;
   8933 		}
   8934 	      if (j + 1 < iverneed->vn_cnt)
   8935 		ivernaux->vna_nextptr = ivernaux + 1;
   8936 
   8937 	      if (ivernaux->vna_next
   8938 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   8939 		goto error_return_bad_verref;
   8940 
   8941 	      evernaux = ((Elf_External_Vernaux *)
   8942 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   8943 	    }
   8944 
   8945 	  iverneed->vn_nextref = NULL;
   8946 	  if (iverneed->vn_next == 0)
   8947 	    break;
   8948 	  if (i + 1 < hdr->sh_info)
   8949 	    iverneed->vn_nextref = iverneed + 1;
   8950 
   8951 	  if (iverneed->vn_next
   8952 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8953 	    goto error_return_bad_verref;
   8954 
   8955 	  everneed = ((Elf_External_Verneed *)
   8956 		      ((bfd_byte *) everneed + iverneed->vn_next));
   8957 	}
   8958       elf_tdata (abfd)->cverrefs = i;
   8959 
   8960       free (contents);
   8961       contents = NULL;
   8962     }
   8963 
   8964   if (elf_dynverdef (abfd) != 0)
   8965     {
   8966       Elf_Internal_Shdr *hdr;
   8967       Elf_External_Verdef *everdef;
   8968       Elf_Internal_Verdef *iverdef;
   8969       Elf_Internal_Verdef *iverdefarr;
   8970       Elf_Internal_Verdef iverdefmem;
   8971       unsigned int i;
   8972       unsigned int maxidx;
   8973       bfd_byte *contents_end_def, *contents_end_aux;
   8974 
   8975       hdr = &elf_tdata (abfd)->dynverdef_hdr;
   8976 
   8977       if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
   8978 	{
   8979 	error_return_bad_verdef:
   8980 	  _bfd_error_handler
   8981 	    (_("%pB: .gnu.version_d invalid entry"), abfd);
   8982 	  bfd_set_error (bfd_error_bad_value);
   8983 	error_return_verdef:
   8984 	  elf_tdata (abfd)->verdef = NULL;
   8985 	  elf_tdata (abfd)->cverdefs = 0;
   8986 	  goto error_return;
   8987 	}
   8988 
   8989       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   8990 	goto error_return_verdef;
   8991       contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   8992       if (contents == NULL)
   8993 	goto error_return_verdef;
   8994 
   8995       BFD_ASSERT (sizeof (Elf_External_Verdef)
   8996 		  >= sizeof (Elf_External_Verdaux));
   8997       contents_end_def = contents + hdr->sh_size
   8998 			 - sizeof (Elf_External_Verdef);
   8999       contents_end_aux = contents + hdr->sh_size
   9000 			 - sizeof (Elf_External_Verdaux);
   9001 
   9002       /* We know the number of entries in the section but not the maximum
   9003 	 index.  Therefore we have to run through all entries and find
   9004 	 the maximum.  */
   9005       everdef = (Elf_External_Verdef *) contents;
   9006       maxidx = 0;
   9007       for (i = 0; i < hdr->sh_info; ++i)
   9008 	{
   9009 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9010 
   9011 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9012 	    goto error_return_bad_verdef;
   9013 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9014 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9015 
   9016 	  if (iverdefmem.vd_next == 0)
   9017 	    break;
   9018 
   9019 	  if (iverdefmem.vd_next
   9020 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9021 	    goto error_return_bad_verdef;
   9022 
   9023 	  everdef = ((Elf_External_Verdef *)
   9024 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9025 	}
   9026 
   9027       if (default_imported_symver)
   9028 	{
   9029 	  if (freeidx > maxidx)
   9030 	    maxidx = ++freeidx;
   9031 	  else
   9032 	    freeidx = ++maxidx;
   9033 	}
   9034       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9035 	{
   9036 	  bfd_set_error (bfd_error_file_too_big);
   9037 	  goto error_return_verdef;
   9038 	}
   9039       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9040       if (elf_tdata (abfd)->verdef == NULL)
   9041 	goto error_return_verdef;
   9042 
   9043       elf_tdata (abfd)->cverdefs = maxidx;
   9044 
   9045       everdef = (Elf_External_Verdef *) contents;
   9046       iverdefarr = elf_tdata (abfd)->verdef;
   9047       for (i = 0; i < hdr->sh_info; i++)
   9048 	{
   9049 	  Elf_External_Verdaux *everdaux;
   9050 	  Elf_Internal_Verdaux *iverdaux;
   9051 	  unsigned int j;
   9052 
   9053 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9054 
   9055 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9056 	    goto error_return_bad_verdef;
   9057 
   9058 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9059 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9060 
   9061 	  iverdef->vd_bfd = abfd;
   9062 
   9063 	  if (iverdef->vd_cnt == 0)
   9064 	    iverdef->vd_auxptr = NULL;
   9065 	  else
   9066 	    {
   9067 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9068 				     sizeof (Elf_Internal_Verdaux), &amt))
   9069 		{
   9070 		  bfd_set_error (bfd_error_file_too_big);
   9071 		  goto error_return_verdef;
   9072 		}
   9073 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9074 		bfd_alloc (abfd, amt);
   9075 	      if (iverdef->vd_auxptr == NULL)
   9076 		goto error_return_verdef;
   9077 	    }
   9078 
   9079 	  if (iverdef->vd_aux
   9080 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9081 	    goto error_return_bad_verdef;
   9082 
   9083 	  everdaux = ((Elf_External_Verdaux *)
   9084 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9085 	  iverdaux = iverdef->vd_auxptr;
   9086 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9087 	    {
   9088 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9089 
   9090 	      iverdaux->vda_nodename =
   9091 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9092 						 iverdaux->vda_name);
   9093 	      if (iverdaux->vda_nodename == NULL)
   9094 		goto error_return_bad_verdef;
   9095 
   9096 	      iverdaux->vda_nextptr = NULL;
   9097 	      if (iverdaux->vda_next == 0)
   9098 		{
   9099 		  iverdef->vd_cnt = j + 1;
   9100 		  break;
   9101 		}
   9102 	      if (j + 1 < iverdef->vd_cnt)
   9103 		iverdaux->vda_nextptr = iverdaux + 1;
   9104 
   9105 	      if (iverdaux->vda_next
   9106 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9107 		goto error_return_bad_verdef;
   9108 
   9109 	      everdaux = ((Elf_External_Verdaux *)
   9110 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9111 	    }
   9112 
   9113 	  iverdef->vd_nodename = NULL;
   9114 	  if (iverdef->vd_cnt)
   9115 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9116 
   9117 	  iverdef->vd_nextdef = NULL;
   9118 	  if (iverdef->vd_next == 0)
   9119 	    break;
   9120 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9121 	    iverdef->vd_nextdef = iverdef + 1;
   9122 
   9123 	  everdef = ((Elf_External_Verdef *)
   9124 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9125 	}
   9126 
   9127       free (contents);
   9128       contents = NULL;
   9129     }
   9130   else if (default_imported_symver)
   9131     {
   9132       if (freeidx < 3)
   9133 	freeidx = 3;
   9134       else
   9135 	freeidx++;
   9136 
   9137       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9138 	{
   9139 	  bfd_set_error (bfd_error_file_too_big);
   9140 	  goto error_return;
   9141 	}
   9142       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9143       if (elf_tdata (abfd)->verdef == NULL)
   9144 	goto error_return;
   9145 
   9146       elf_tdata (abfd)->cverdefs = freeidx;
   9147     }
   9148 
   9149   /* Create a default version based on the soname.  */
   9150   if (default_imported_symver)
   9151     {
   9152       Elf_Internal_Verdef *iverdef;
   9153       Elf_Internal_Verdaux *iverdaux;
   9154 
   9155       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9156 
   9157       iverdef->vd_version = VER_DEF_CURRENT;
   9158       iverdef->vd_flags = 0;
   9159       iverdef->vd_ndx = freeidx;
   9160       iverdef->vd_cnt = 1;
   9161 
   9162       iverdef->vd_bfd = abfd;
   9163 
   9164       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9165       if (iverdef->vd_nodename == NULL)
   9166 	goto error_return_verdef;
   9167       iverdef->vd_nextdef = NULL;
   9168       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9169 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9170       if (iverdef->vd_auxptr == NULL)
   9171 	goto error_return_verdef;
   9172 
   9173       iverdaux = iverdef->vd_auxptr;
   9174       iverdaux->vda_nodename = iverdef->vd_nodename;
   9175     }
   9176 
   9177   return true;
   9178 
   9179  error_return:
   9180   free (contents);
   9181   return false;
   9182 }
   9183 
   9184 asymbol *
   9186 _bfd_elf_make_empty_symbol (bfd *abfd)
   9187 {
   9188   elf_symbol_type *newsym;
   9189 
   9190   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9191   if (!newsym)
   9192     return NULL;
   9193   newsym->symbol.the_bfd = abfd;
   9194   return &newsym->symbol;
   9195 }
   9196 
   9197 void
   9198 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9199 			  asymbol *symbol,
   9200 			  symbol_info *ret)
   9201 {
   9202   bfd_symbol_info (symbol, ret);
   9203 }
   9204 
   9205 /* Return whether a symbol name implies a local symbol.  Most targets
   9206    use this function for the is_local_label_name entry point, but some
   9207    override it.  */
   9208 
   9209 bool
   9210 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9211 			      const char *name)
   9212 {
   9213   /* Normal local symbols start with ``.L''.  */
   9214   if (name[0] == '.' && name[1] == 'L')
   9215     return true;
   9216 
   9217   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9218      DWARF debugging symbols starting with ``..''.  */
   9219   if (name[0] == '.' && name[1] == '.')
   9220     return true;
   9221 
   9222   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9223      emitting DWARF debugging output.  I suspect this is actually a
   9224      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9225      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9226      underscore to be emitted on some ELF targets).  For ease of use,
   9227      we treat such symbols as local.  */
   9228   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9229     return true;
   9230 
   9231   /* Treat assembler generated fake symbols, dollar local labels and
   9232      forward-backward labels (aka local labels) as locals.
   9233      These labels have the form:
   9234 
   9235        L0^A.*				       (fake symbols)
   9236 
   9237        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9238 
   9239      Versions which start with .L will have already been matched above,
   9240      so we only need to match the rest.  */
   9241   if (name[0] == 'L' && ISDIGIT (name[1]))
   9242     {
   9243       bool ret = false;
   9244       const char * p;
   9245       char c;
   9246 
   9247       for (p = name + 2; (c = *p); p++)
   9248 	{
   9249 	  if (c == 1 || c == 2)
   9250 	    {
   9251 	      if (c == 1 && p == name + 2)
   9252 		/* A fake symbol.  */
   9253 		return true;
   9254 
   9255 	      /* FIXME: We are being paranoid here and treating symbols like
   9256 		 L0^Bfoo as if there were non-local, on the grounds that the
   9257 		 assembler will never generate them.  But can any symbol
   9258 		 containing an ASCII value in the range 1-31 ever be anything
   9259 		 other than some kind of local ?  */
   9260 	      ret = true;
   9261 	    }
   9262 
   9263 	  if (! ISDIGIT (c))
   9264 	    {
   9265 	      ret = false;
   9266 	      break;
   9267 	    }
   9268 	}
   9269       return ret;
   9270     }
   9271 
   9272   return false;
   9273 }
   9274 
   9275 alent *
   9276 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9277 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9278 {
   9279   abort ();
   9280   return NULL;
   9281 }
   9282 
   9283 bool
   9284 _bfd_elf_set_arch_mach (bfd *abfd,
   9285 			enum bfd_architecture arch,
   9286 			unsigned long machine)
   9287 {
   9288   /* If this isn't the right architecture for this backend, and this
   9289      isn't the generic backend, fail.  */
   9290   if (arch != get_elf_backend_data (abfd)->arch
   9291       && arch != bfd_arch_unknown
   9292       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9293     return false;
   9294 
   9295   return bfd_default_set_arch_mach (abfd, arch, machine);
   9296 }
   9297 
   9298 /* Find the nearest line to a particular section and offset,
   9299    for error reporting.  */
   9300 
   9301 bool
   9302 _bfd_elf_find_nearest_line (bfd *abfd,
   9303 			    asymbol **symbols,
   9304 			    asection *section,
   9305 			    bfd_vma offset,
   9306 			    const char **filename_ptr,
   9307 			    const char **functionname_ptr,
   9308 			    unsigned int *line_ptr,
   9309 			    unsigned int *discriminator_ptr)
   9310 {
   9311   bool found;
   9312 
   9313   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   9314 				     filename_ptr, functionname_ptr,
   9315 				     line_ptr, discriminator_ptr,
   9316 				     dwarf_debug_sections,
   9317 				     &elf_tdata (abfd)->dwarf2_find_line_info))
   9318     return true;
   9319 
   9320   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   9321 				     filename_ptr, functionname_ptr, line_ptr))
   9322     {
   9323       if (!*functionname_ptr)
   9324 	_bfd_elf_find_function (abfd, symbols, section, offset,
   9325 				*filename_ptr ? NULL : filename_ptr,
   9326 				functionname_ptr);
   9327       return true;
   9328     }
   9329 
   9330   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   9331 					     &found, filename_ptr,
   9332 					     functionname_ptr, line_ptr,
   9333 					     &elf_tdata (abfd)->line_info))
   9334     return false;
   9335   if (found && (*functionname_ptr || *line_ptr))
   9336     return true;
   9337 
   9338   if (symbols == NULL)
   9339     return false;
   9340 
   9341   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   9342 				filename_ptr, functionname_ptr))
   9343     return false;
   9344 
   9345   *line_ptr = 0;
   9346   return true;
   9347 }
   9348 
   9349 /* Find the line for a symbol.  */
   9350 
   9351 bool
   9352 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   9353 		    const char **filename_ptr, unsigned int *line_ptr)
   9354 {
   9355   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   9356 					filename_ptr, NULL, line_ptr, NULL,
   9357 					dwarf_debug_sections,
   9358 					&elf_tdata (abfd)->dwarf2_find_line_info);
   9359 }
   9360 
   9361 /* After a call to bfd_find_nearest_line, successive calls to
   9362    bfd_find_inliner_info can be used to get source information about
   9363    each level of function inlining that terminated at the address
   9364    passed to bfd_find_nearest_line.  Currently this is only supported
   9365    for DWARF2 with appropriate DWARF3 extensions. */
   9366 
   9367 bool
   9368 _bfd_elf_find_inliner_info (bfd *abfd,
   9369 			    const char **filename_ptr,
   9370 			    const char **functionname_ptr,
   9371 			    unsigned int *line_ptr)
   9372 {
   9373   bool found;
   9374   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   9375 					 functionname_ptr, line_ptr,
   9376 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   9377   return found;
   9378 }
   9379 
   9380 int
   9381 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   9382 {
   9383   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9384   int ret = bed->s->sizeof_ehdr;
   9385 
   9386   if (!bfd_link_relocatable (info))
   9387     {
   9388       bfd_size_type phdr_size = elf_program_header_size (abfd);
   9389 
   9390       if (phdr_size == (bfd_size_type) -1)
   9391 	{
   9392 	  struct elf_segment_map *m;
   9393 
   9394 	  phdr_size = 0;
   9395 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   9396 	    phdr_size += bed->s->sizeof_phdr;
   9397 
   9398 	  if (phdr_size == 0)
   9399 	    phdr_size = get_program_header_size (abfd, info);
   9400 	}
   9401 
   9402       elf_program_header_size (abfd) = phdr_size;
   9403       ret += phdr_size;
   9404     }
   9405 
   9406   return ret;
   9407 }
   9408 
   9409 bool
   9410 _bfd_elf_set_section_contents (bfd *abfd,
   9411 			       sec_ptr section,
   9412 			       const void *location,
   9413 			       file_ptr offset,
   9414 			       bfd_size_type count)
   9415 {
   9416   Elf_Internal_Shdr *hdr;
   9417 
   9418   if (! abfd->output_has_begun
   9419       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   9420     return false;
   9421 
   9422   if (!count)
   9423     return true;
   9424 
   9425   hdr = &elf_section_data (section)->this_hdr;
   9426   if (hdr->sh_offset == (file_ptr) -1)
   9427     {
   9428       unsigned char *contents;
   9429 
   9430       if (bfd_section_is_ctf (section))
   9431 	/* Nothing to do with this section: the contents are generated
   9432 	   later.  */
   9433 	return true;
   9434 
   9435       if ((section->flags & SEC_ELF_COMPRESS) == 0)
   9436 	{
   9437 	  _bfd_error_handler
   9438 	    (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
   9439 	     abfd, section);
   9440 	  bfd_set_error (bfd_error_invalid_operation);
   9441 	  return false;
   9442 	}
   9443 
   9444       if ((offset + count) > hdr->sh_size)
   9445 	{
   9446 	  _bfd_error_handler
   9447 	    (_("%pB:%pA: error: attempting to write over the end of the section"),
   9448 	     abfd, section);
   9449 
   9450 	  bfd_set_error (bfd_error_invalid_operation);
   9451 	  return false;
   9452 	}
   9453 
   9454       contents = hdr->contents;
   9455       if (contents == NULL)
   9456 	{
   9457 	  _bfd_error_handler
   9458 	    (_("%pB:%pA: error: attempting to write section into an empty buffer"),
   9459 	     abfd, section);
   9460 
   9461 	  bfd_set_error (bfd_error_invalid_operation);
   9462 	  return false;
   9463 	}
   9464 
   9465       memcpy (contents + offset, location, count);
   9466       return true;
   9467     }
   9468 
   9469   return _bfd_generic_set_section_contents (abfd, section,
   9470 					    location, offset, count);
   9471 }
   9472 
   9473 bool
   9474 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   9475 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   9476 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   9477 {
   9478   abort ();
   9479   return false;
   9480 }
   9481 
   9482 /* Try to convert a non-ELF reloc into an ELF one.  */
   9483 
   9484 bool
   9485 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   9486 {
   9487   /* Check whether we really have an ELF howto.  */
   9488 
   9489   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   9490     {
   9491       bfd_reloc_code_real_type code;
   9492       reloc_howto_type *howto;
   9493 
   9494       /* Alien reloc: Try to determine its type to replace it with an
   9495 	 equivalent ELF reloc.  */
   9496 
   9497       if (areloc->howto->pc_relative)
   9498 	{
   9499 	  switch (areloc->howto->bitsize)
   9500 	    {
   9501 	    case 8:
   9502 	      code = BFD_RELOC_8_PCREL;
   9503 	      break;
   9504 	    case 12:
   9505 	      code = BFD_RELOC_12_PCREL;
   9506 	      break;
   9507 	    case 16:
   9508 	      code = BFD_RELOC_16_PCREL;
   9509 	      break;
   9510 	    case 24:
   9511 	      code = BFD_RELOC_24_PCREL;
   9512 	      break;
   9513 	    case 32:
   9514 	      code = BFD_RELOC_32_PCREL;
   9515 	      break;
   9516 	    case 64:
   9517 	      code = BFD_RELOC_64_PCREL;
   9518 	      break;
   9519 	    default:
   9520 	      goto fail;
   9521 	    }
   9522 
   9523 	  howto = bfd_reloc_type_lookup (abfd, code);
   9524 
   9525 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   9526 	    {
   9527 	      if (howto->pcrel_offset)
   9528 		areloc->addend += areloc->address;
   9529 	      else
   9530 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   9531 	    }
   9532 	}
   9533       else
   9534 	{
   9535 	  switch (areloc->howto->bitsize)
   9536 	    {
   9537 	    case 8:
   9538 	      code = BFD_RELOC_8;
   9539 	      break;
   9540 	    case 14:
   9541 	      code = BFD_RELOC_14;
   9542 	      break;
   9543 	    case 16:
   9544 	      code = BFD_RELOC_16;
   9545 	      break;
   9546 	    case 26:
   9547 	      code = BFD_RELOC_26;
   9548 	      break;
   9549 	    case 32:
   9550 	      code = BFD_RELOC_32;
   9551 	      break;
   9552 	    case 64:
   9553 	      code = BFD_RELOC_64;
   9554 	      break;
   9555 	    default:
   9556 	      goto fail;
   9557 	    }
   9558 
   9559 	  howto = bfd_reloc_type_lookup (abfd, code);
   9560 	}
   9561 
   9562       if (howto)
   9563 	areloc->howto = howto;
   9564       else
   9565 	goto fail;
   9566     }
   9567 
   9568   return true;
   9569 
   9570  fail:
   9571   /* xgettext:c-format */
   9572   _bfd_error_handler (_("%pB: %s unsupported"),
   9573 		      abfd, areloc->howto->name);
   9574   bfd_set_error (bfd_error_sorry);
   9575   return false;
   9576 }
   9577 
   9578 bool
   9579 _bfd_elf_close_and_cleanup (bfd *abfd)
   9580 {
   9581   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9582   if (tdata != NULL
   9583       && (bfd_get_format (abfd) == bfd_object
   9584 	  || bfd_get_format (abfd) == bfd_core))
   9585     {
   9586       if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
   9587 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   9588       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   9589     }
   9590 
   9591   return _bfd_generic_close_and_cleanup (abfd);
   9592 }
   9593 
   9594 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   9595    in the relocation's offset.  Thus we cannot allow any sort of sanity
   9596    range-checking to interfere.  There is nothing else to do in processing
   9597    this reloc.  */
   9598 
   9599 bfd_reloc_status_type
   9600 _bfd_elf_rel_vtable_reloc_fn
   9601   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   9602    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   9603    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   9604    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   9605 {
   9606   return bfd_reloc_ok;
   9607 }
   9608 
   9609 /* Elf core file support.  Much of this only works on native
   9611    toolchains, since we rely on knowing the
   9612    machine-dependent procfs structure in order to pick
   9613    out details about the corefile.  */
   9614 
   9615 #ifdef HAVE_SYS_PROCFS_H
   9616 # include <sys/procfs.h>
   9617 #endif
   9618 
   9619 /* Return a PID that identifies a "thread" for threaded cores, or the
   9620    PID of the main process for non-threaded cores.  */
   9621 
   9622 static int
   9623 elfcore_make_pid (bfd *abfd)
   9624 {
   9625   int pid;
   9626 
   9627   pid = elf_tdata (abfd)->core->lwpid;
   9628   if (pid == 0)
   9629     pid = elf_tdata (abfd)->core->pid;
   9630 
   9631   return pid;
   9632 }
   9633 
   9634 /* If there isn't a section called NAME, make one, using
   9635    data from SECT.  Note, this function will generate a
   9636    reference to NAME, so you shouldn't deallocate or
   9637    overwrite it.  */
   9638 
   9639 static bool
   9640 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   9641 {
   9642   asection *sect2;
   9643 
   9644   if (bfd_get_section_by_name (abfd, name) != NULL)
   9645     return true;
   9646 
   9647   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   9648   if (sect2 == NULL)
   9649     return false;
   9650 
   9651   sect2->size = sect->size;
   9652   sect2->filepos = sect->filepos;
   9653   sect2->alignment_power = sect->alignment_power;
   9654   return true;
   9655 }
   9656 
   9657 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   9658    actually creates up to two pseudosections:
   9659    - For the single-threaded case, a section named NAME, unless
   9660      such a section already exists.
   9661    - For the multi-threaded case, a section named "NAME/PID", where
   9662      PID is elfcore_make_pid (abfd).
   9663    Both pseudosections have identical contents.  */
   9664 bool
   9665 _bfd_elfcore_make_pseudosection (bfd *abfd,
   9666 				 char *name,
   9667 				 size_t size,
   9668 				 ufile_ptr filepos)
   9669 {
   9670   char buf[100];
   9671   char *threaded_name;
   9672   size_t len;
   9673   asection *sect;
   9674 
   9675   /* Build the section name.  */
   9676 
   9677   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   9678   len = strlen (buf) + 1;
   9679   threaded_name = (char *) bfd_alloc (abfd, len);
   9680   if (threaded_name == NULL)
   9681     return false;
   9682   memcpy (threaded_name, buf, len);
   9683 
   9684   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   9685 					     SEC_HAS_CONTENTS);
   9686   if (sect == NULL)
   9687     return false;
   9688   sect->size = size;
   9689   sect->filepos = filepos;
   9690   sect->alignment_power = 2;
   9691 
   9692   return elfcore_maybe_make_sect (abfd, name, sect);
   9693 }
   9694 
   9695 static bool
   9696 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   9697 				size_t offs)
   9698 {
   9699   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   9700 						       SEC_HAS_CONTENTS);
   9701 
   9702   if (sect == NULL)
   9703     return false;
   9704 
   9705   sect->size = note->descsz - offs;
   9706   sect->filepos = note->descpos + offs;
   9707   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   9708 
   9709   return true;
   9710 }
   9711 
   9712 /* prstatus_t exists on:
   9713      solaris 2.5+
   9714      linux 2.[01] + glibc
   9715      unixware 4.2
   9716 */
   9717 
   9718 #if defined (HAVE_PRSTATUS_T)
   9719 
   9720 static bool
   9721 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   9722 {
   9723   size_t size;
   9724   int offset;
   9725 
   9726   if (note->descsz == sizeof (prstatus_t))
   9727     {
   9728       prstatus_t prstat;
   9729 
   9730       size = sizeof (prstat.pr_reg);
   9731       offset   = offsetof (prstatus_t, pr_reg);
   9732       memcpy (&prstat, note->descdata, sizeof (prstat));
   9733 
   9734       /* Do not overwrite the core signal if it
   9735 	 has already been set by another thread.  */
   9736       if (elf_tdata (abfd)->core->signal == 0)
   9737 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9738       if (elf_tdata (abfd)->core->pid == 0)
   9739 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9740 
   9741       /* pr_who exists on:
   9742 	 solaris 2.5+
   9743 	 unixware 4.2
   9744 	 pr_who doesn't exist on:
   9745 	 linux 2.[01]
   9746 	 */
   9747 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   9748       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9749 #else
   9750       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9751 #endif
   9752     }
   9753 #if defined (HAVE_PRSTATUS32_T)
   9754   else if (note->descsz == sizeof (prstatus32_t))
   9755     {
   9756       /* 64-bit host, 32-bit corefile */
   9757       prstatus32_t prstat;
   9758 
   9759       size = sizeof (prstat.pr_reg);
   9760       offset   = offsetof (prstatus32_t, pr_reg);
   9761       memcpy (&prstat, note->descdata, sizeof (prstat));
   9762 
   9763       /* Do not overwrite the core signal if it
   9764 	 has already been set by another thread.  */
   9765       if (elf_tdata (abfd)->core->signal == 0)
   9766 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9767       if (elf_tdata (abfd)->core->pid == 0)
   9768 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9769 
   9770       /* pr_who exists on:
   9771 	 solaris 2.5+
   9772 	 unixware 4.2
   9773 	 pr_who doesn't exist on:
   9774 	 linux 2.[01]
   9775 	 */
   9776 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   9777       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9778 #else
   9779       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9780 #endif
   9781     }
   9782 #endif /* HAVE_PRSTATUS32_T */
   9783   else
   9784     {
   9785       /* Fail - we don't know how to handle any other
   9786 	 note size (ie. data object type).  */
   9787       return true;
   9788     }
   9789 
   9790   /* Make a ".reg/999" section and a ".reg" section.  */
   9791   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   9792 					  size, note->descpos + offset);
   9793 }
   9794 #endif /* defined (HAVE_PRSTATUS_T) */
   9795 
   9796 /* Create a pseudosection containing the exact contents of NOTE.  */
   9797 static bool
   9798 elfcore_make_note_pseudosection (bfd *abfd,
   9799 				 char *name,
   9800 				 Elf_Internal_Note *note)
   9801 {
   9802   return _bfd_elfcore_make_pseudosection (abfd, name,
   9803 					  note->descsz, note->descpos);
   9804 }
   9805 
   9806 /* There isn't a consistent prfpregset_t across platforms,
   9807    but it doesn't matter, because we don't have to pick this
   9808    data structure apart.  */
   9809 
   9810 static bool
   9811 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   9812 {
   9813   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   9814 }
   9815 
   9816 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   9817    type of NT_PRXFPREG.  Just include the whole note's contents
   9818    literally.  */
   9819 
   9820 static bool
   9821 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   9822 {
   9823   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   9824 }
   9825 
   9826 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   9827    with a note type of NT_X86_XSTATE.  Just include the whole note's
   9828    contents literally.  */
   9829 
   9830 static bool
   9831 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   9832 {
   9833   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   9834 }
   9835 
   9836 static bool
   9837 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   9838 {
   9839   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   9840 }
   9841 
   9842 static bool
   9843 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   9844 {
   9845   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   9846 }
   9847 
   9848 static bool
   9849 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   9850 {
   9851   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
   9852 }
   9853 
   9854 static bool
   9855 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   9856 {
   9857   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
   9858 }
   9859 
   9860 static bool
   9861 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   9862 {
   9863   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
   9864 }
   9865 
   9866 static bool
   9867 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   9868 {
   9869   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
   9870 }
   9871 
   9872 static bool
   9873 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   9874 {
   9875   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
   9876 }
   9877 
   9878 static bool
   9879 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   9880 {
   9881   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
   9882 }
   9883 
   9884 static bool
   9885 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   9886 {
   9887   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
   9888 }
   9889 
   9890 static bool
   9891 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   9892 {
   9893   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
   9894 }
   9895 
   9896 static bool
   9897 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   9898 {
   9899   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
   9900 }
   9901 
   9902 static bool
   9903 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   9904 {
   9905   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
   9906 }
   9907 
   9908 static bool
   9909 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   9910 {
   9911   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
   9912 }
   9913 
   9914 static bool
   9915 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   9916 {
   9917   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
   9918 }
   9919 
   9920 static bool
   9921 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   9922 {
   9923   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
   9924 }
   9925 
   9926 static bool
   9927 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   9928 {
   9929   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   9930 }
   9931 
   9932 static bool
   9933 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   9934 {
   9935   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   9936 }
   9937 
   9938 static bool
   9939 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   9940 {
   9941   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   9942 }
   9943 
   9944 static bool
   9945 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   9946 {
   9947   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   9948 }
   9949 
   9950 static bool
   9951 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   9952 {
   9953   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   9954 }
   9955 
   9956 static bool
   9957 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   9958 {
   9959   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   9960 }
   9961 
   9962 static bool
   9963 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   9964 {
   9965   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   9966 }
   9967 
   9968 static bool
   9969 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   9970 {
   9971   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   9972 }
   9973 
   9974 static bool
   9975 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   9976 {
   9977   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   9978 }
   9979 
   9980 static bool
   9981 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   9982 {
   9983   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   9984 }
   9985 
   9986 static bool
   9987 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   9988 {
   9989   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   9990 }
   9991 
   9992 static bool
   9993 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   9994 {
   9995   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   9996 }
   9997 
   9998 static bool
   9999 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10000 {
   10001   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   10002 }
   10003 
   10004 static bool
   10005 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10006 {
   10007   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   10008 }
   10009 
   10010 static bool
   10011 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10012 {
   10013   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   10014 }
   10015 
   10016 static bool
   10017 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10018 {
   10019   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   10020 }
   10021 
   10022 static bool
   10023 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10024 {
   10025   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   10026 }
   10027 
   10028 static bool
   10029 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10030 {
   10031   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
   10032 }
   10033 
   10034 static bool
   10035 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10036 {
   10037   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
   10038 }
   10039 
   10040 static bool
   10041 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10042 {
   10043   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
   10044 					  note);
   10045 }
   10046 
   10047 static bool
   10048 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10049 {
   10050   return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
   10051 }
   10052 
   10053 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10054    successful otherwise, return FALSE.  */
   10055 
   10056 static bool
   10057 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10058 {
   10059   return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
   10060 }
   10061 
   10062 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10063    successful otherwise, return FALSE.  */
   10064 
   10065 static bool
   10066 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10067 {
   10068   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10069 }
   10070 
   10071 static bool
   10072 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10073 {
   10074   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
   10075 }
   10076 
   10077 static bool
   10078 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10079 {
   10080   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
   10081 }
   10082 
   10083 static bool
   10084 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10085 {
   10086   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
   10087 }
   10088 
   10089 static bool
   10090 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10091 {
   10092   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
   10093 }
   10094 
   10095 #if defined (HAVE_PRPSINFO_T)
   10096 typedef prpsinfo_t   elfcore_psinfo_t;
   10097 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10098 typedef prpsinfo32_t elfcore_psinfo32_t;
   10099 #endif
   10100 #endif
   10101 
   10102 #if defined (HAVE_PSINFO_T)
   10103 typedef psinfo_t   elfcore_psinfo_t;
   10104 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10105 typedef psinfo32_t elfcore_psinfo32_t;
   10106 #endif
   10107 #endif
   10108 
   10109 /* return a malloc'ed copy of a string at START which is at
   10110    most MAX bytes long, possibly without a terminating '\0'.
   10111    the copy will always have a terminating '\0'.  */
   10112 
   10113 char *
   10114 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10115 {
   10116   char *dups;
   10117   char *end = (char *) memchr (start, '\0', max);
   10118   size_t len;
   10119 
   10120   if (end == NULL)
   10121     len = max;
   10122   else
   10123     len = end - start;
   10124 
   10125   dups = (char *) bfd_alloc (abfd, len + 1);
   10126   if (dups == NULL)
   10127     return NULL;
   10128 
   10129   memcpy (dups, start, len);
   10130   dups[len] = '\0';
   10131 
   10132   return dups;
   10133 }
   10134 
   10135 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10136 static bool
   10137 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10138 {
   10139   if (note->descsz == sizeof (elfcore_psinfo_t))
   10140     {
   10141       elfcore_psinfo_t psinfo;
   10142 
   10143       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10144 
   10145 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   10146       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10147 #endif
   10148       elf_tdata (abfd)->core->program
   10149 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10150 				sizeof (psinfo.pr_fname));
   10151 
   10152       elf_tdata (abfd)->core->command
   10153 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10154 				sizeof (psinfo.pr_psargs));
   10155     }
   10156 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10157   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10158     {
   10159       /* 64-bit host, 32-bit corefile */
   10160       elfcore_psinfo32_t psinfo;
   10161 
   10162       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10163 
   10164 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10165       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10166 #endif
   10167       elf_tdata (abfd)->core->program
   10168 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10169 				sizeof (psinfo.pr_fname));
   10170 
   10171       elf_tdata (abfd)->core->command
   10172 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10173 				sizeof (psinfo.pr_psargs));
   10174     }
   10175 #endif
   10176 
   10177   else
   10178     {
   10179       /* Fail - we don't know how to handle any other
   10180 	 note size (ie. data object type).  */
   10181       return true;
   10182     }
   10183 
   10184   /* Note that for some reason, a spurious space is tacked
   10185      onto the end of the args in some (at least one anyway)
   10186      implementations, so strip it off if it exists.  */
   10187 
   10188   {
   10189     char *command = elf_tdata (abfd)->core->command;
   10190     int n = strlen (command);
   10191 
   10192     if (0 < n && command[n - 1] == ' ')
   10193       command[n - 1] = '\0';
   10194   }
   10195 
   10196   return true;
   10197 }
   10198 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10199 
   10200 #if defined (HAVE_PSTATUS_T)
   10201 static bool
   10202 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10203 {
   10204   if (note->descsz == sizeof (pstatus_t)
   10205 #if defined (HAVE_PXSTATUS_T)
   10206       || note->descsz == sizeof (pxstatus_t)
   10207 #endif
   10208       )
   10209     {
   10210       pstatus_t pstat;
   10211 
   10212       memcpy (&pstat, note->descdata, sizeof (pstat));
   10213 
   10214       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10215     }
   10216 #if defined (HAVE_PSTATUS32_T)
   10217   else if (note->descsz == sizeof (pstatus32_t))
   10218     {
   10219       /* 64-bit host, 32-bit corefile */
   10220       pstatus32_t pstat;
   10221 
   10222       memcpy (&pstat, note->descdata, sizeof (pstat));
   10223 
   10224       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10225     }
   10226 #endif
   10227   /* Could grab some more details from the "representative"
   10228      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10229      NT_LWPSTATUS note, presumably.  */
   10230 
   10231   return true;
   10232 }
   10233 #endif /* defined (HAVE_PSTATUS_T) */
   10234 
   10235 #if defined (HAVE_LWPSTATUS_T)
   10236 static bool
   10237 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10238 {
   10239   lwpstatus_t lwpstat;
   10240   char buf[100];
   10241   char *name;
   10242   size_t len;
   10243   asection *sect;
   10244 
   10245   if (note->descsz != sizeof (lwpstat)
   10246 #if defined (HAVE_LWPXSTATUS_T)
   10247       && note->descsz != sizeof (lwpxstatus_t)
   10248 #endif
   10249       )
   10250     return true;
   10251 
   10252   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10253 
   10254   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10255   /* Do not overwrite the core signal if it has already been set by
   10256      another thread.  */
   10257   if (elf_tdata (abfd)->core->signal == 0)
   10258     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10259 
   10260   /* Make a ".reg/999" section.  */
   10261 
   10262   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   10263   len = strlen (buf) + 1;
   10264   name = bfd_alloc (abfd, len);
   10265   if (name == NULL)
   10266     return false;
   10267   memcpy (name, buf, len);
   10268 
   10269   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10270   if (sect == NULL)
   10271     return false;
   10272 
   10273 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10274   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   10275   sect->filepos = note->descpos
   10276     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   10277 #endif
   10278 
   10279 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10280   sect->size = sizeof (lwpstat.pr_reg);
   10281   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   10282 #endif
   10283 
   10284   sect->alignment_power = 2;
   10285 
   10286   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   10287     return false;
   10288 
   10289   /* Make a ".reg2/999" section */
   10290 
   10291   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   10292   len = strlen (buf) + 1;
   10293   name = bfd_alloc (abfd, len);
   10294   if (name == NULL)
   10295     return false;
   10296   memcpy (name, buf, len);
   10297 
   10298   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10299   if (sect == NULL)
   10300     return false;
   10301 
   10302 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10303   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   10304   sect->filepos = note->descpos
   10305     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   10306 #endif
   10307 
   10308 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   10309   sect->size = sizeof (lwpstat.pr_fpreg);
   10310   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   10311 #endif
   10312 
   10313   sect->alignment_power = 2;
   10314 
   10315   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   10316 }
   10317 #endif /* defined (HAVE_LWPSTATUS_T) */
   10318 
   10319 /* These constants, and the structure offsets used below, are defined by
   10320    Cygwin's core_dump.h */
   10321 #define NOTE_INFO_PROCESS  1
   10322 #define NOTE_INFO_THREAD   2
   10323 #define NOTE_INFO_MODULE   3
   10324 #define NOTE_INFO_MODULE64 4
   10325 
   10326 static bool
   10327 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   10328 {
   10329   char buf[30];
   10330   char *name;
   10331   size_t len;
   10332   unsigned int name_size;
   10333   asection *sect;
   10334   unsigned int type;
   10335   int is_active_thread;
   10336   bfd_vma base_addr;
   10337 
   10338   if (note->descsz < 4)
   10339     return true;
   10340 
   10341   if (! startswith (note->namedata, "win32"))
   10342     return true;
   10343 
   10344   type = bfd_get_32 (abfd, note->descdata);
   10345 
   10346   struct
   10347   {
   10348     const char *type_name;
   10349     unsigned long min_size;
   10350   } size_check[] =
   10351       {
   10352        { "NOTE_INFO_PROCESS", 12 },
   10353        { "NOTE_INFO_THREAD", 12 },
   10354        { "NOTE_INFO_MODULE", 12 },
   10355        { "NOTE_INFO_MODULE64", 16 },
   10356       };
   10357 
   10358   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   10359       return true;
   10360 
   10361   if (note->descsz < size_check[type - 1].min_size)
   10362     {
   10363       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
   10364                           abfd, size_check[type - 1].type_name, note->descsz);
   10365       return true;
   10366     }
   10367 
   10368   switch (type)
   10369     {
   10370     case NOTE_INFO_PROCESS:
   10371       /* FIXME: need to add ->core->command.  */
   10372       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   10373       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   10374       break;
   10375 
   10376     case NOTE_INFO_THREAD:
   10377       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   10378          structure. */
   10379       /* thread_info.tid */
   10380       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   10381 
   10382       len = strlen (buf) + 1;
   10383       name = (char *) bfd_alloc (abfd, len);
   10384       if (name == NULL)
   10385 	return false;
   10386 
   10387       memcpy (name, buf, len);
   10388 
   10389       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10390       if (sect == NULL)
   10391 	return false;
   10392 
   10393       /* sizeof (thread_info.thread_context) */
   10394       sect->size = note->descsz - 12;
   10395       /* offsetof (thread_info.thread_context) */
   10396       sect->filepos = note->descpos + 12;
   10397       sect->alignment_power = 2;
   10398 
   10399       /* thread_info.is_active_thread */
   10400       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   10401 
   10402       if (is_active_thread)
   10403 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   10404 	  return false;
   10405       break;
   10406 
   10407     case NOTE_INFO_MODULE:
   10408     case NOTE_INFO_MODULE64:
   10409       /* Make a ".module/xxxxxxxx" section.  */
   10410       if (type == NOTE_INFO_MODULE)
   10411         {
   10412           /* module_info.base_address */
   10413           base_addr = bfd_get_32 (abfd, note->descdata + 4);
   10414           sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   10415           /* module_info.module_name_size */
   10416           name_size = bfd_get_32 (abfd, note->descdata + 8);
   10417         }
   10418       else /* NOTE_INFO_MODULE64 */
   10419         {
   10420           /* module_info.base_address */
   10421           base_addr = bfd_get_64 (abfd, note->descdata + 4);
   10422           sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   10423           /* module_info.module_name_size */
   10424           name_size = bfd_get_32 (abfd, note->descdata + 12);
   10425         }
   10426 
   10427       len = strlen (buf) + 1;
   10428       name = (char *) bfd_alloc (abfd, len);
   10429       if (name == NULL)
   10430 	return false;
   10431 
   10432       memcpy (name, buf, len);
   10433 
   10434       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10435 
   10436       if (sect == NULL)
   10437 	return false;
   10438 
   10439       if (note->descsz < 12 + name_size)
   10440         {
   10441           _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
   10442                               abfd, note->descsz, name_size);
   10443           return true;
   10444         }
   10445 
   10446       sect->size = note->descsz;
   10447       sect->filepos = note->descpos;
   10448       sect->alignment_power = 2;
   10449       break;
   10450 
   10451     default:
   10452       return true;
   10453     }
   10454 
   10455   return true;
   10456 }
   10457 
   10458 static bool
   10459 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   10460 {
   10461   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10462 
   10463   switch (note->type)
   10464     {
   10465     default:
   10466       return true;
   10467 
   10468     case NT_PRSTATUS:
   10469       if (bed->elf_backend_grok_prstatus)
   10470 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   10471 	  return true;
   10472 #if defined (HAVE_PRSTATUS_T)
   10473       return elfcore_grok_prstatus (abfd, note);
   10474 #else
   10475       return true;
   10476 #endif
   10477 
   10478 #if defined (HAVE_PSTATUS_T)
   10479     case NT_PSTATUS:
   10480       return elfcore_grok_pstatus (abfd, note);
   10481 #endif
   10482 
   10483 #if defined (HAVE_LWPSTATUS_T)
   10484     case NT_LWPSTATUS:
   10485       return elfcore_grok_lwpstatus (abfd, note);
   10486 #endif
   10487 
   10488     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   10489       return elfcore_grok_prfpreg (abfd, note);
   10490 
   10491     case NT_WIN32PSTATUS:
   10492       return elfcore_grok_win32pstatus (abfd, note);
   10493 
   10494     case NT_PRXFPREG:		/* Linux SSE extension */
   10495       if (note->namesz == 6
   10496 	  && strcmp (note->namedata, "LINUX") == 0)
   10497 	return elfcore_grok_prxfpreg (abfd, note);
   10498       else
   10499 	return true;
   10500 
   10501     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   10502       if (note->namesz == 6
   10503 	  && strcmp (note->namedata, "LINUX") == 0)
   10504 	return elfcore_grok_xstatereg (abfd, note);
   10505       else
   10506 	return true;
   10507 
   10508     case NT_PPC_VMX:
   10509       if (note->namesz == 6
   10510 	  && strcmp (note->namedata, "LINUX") == 0)
   10511 	return elfcore_grok_ppc_vmx (abfd, note);
   10512       else
   10513 	return true;
   10514 
   10515     case NT_PPC_VSX:
   10516       if (note->namesz == 6
   10517 	  && strcmp (note->namedata, "LINUX") == 0)
   10518 	return elfcore_grok_ppc_vsx (abfd, note);
   10519       else
   10520 	return true;
   10521 
   10522     case NT_PPC_TAR:
   10523       if (note->namesz == 6
   10524 	  && strcmp (note->namedata, "LINUX") == 0)
   10525 	return elfcore_grok_ppc_tar (abfd, note);
   10526       else
   10527 	return true;
   10528 
   10529     case NT_PPC_PPR:
   10530       if (note->namesz == 6
   10531 	  && strcmp (note->namedata, "LINUX") == 0)
   10532 	return elfcore_grok_ppc_ppr (abfd, note);
   10533       else
   10534 	return true;
   10535 
   10536     case NT_PPC_DSCR:
   10537       if (note->namesz == 6
   10538 	  && strcmp (note->namedata, "LINUX") == 0)
   10539 	return elfcore_grok_ppc_dscr (abfd, note);
   10540       else
   10541 	return true;
   10542 
   10543     case NT_PPC_EBB:
   10544       if (note->namesz == 6
   10545 	  && strcmp (note->namedata, "LINUX") == 0)
   10546 	return elfcore_grok_ppc_ebb (abfd, note);
   10547       else
   10548 	return true;
   10549 
   10550     case NT_PPC_PMU:
   10551       if (note->namesz == 6
   10552 	  && strcmp (note->namedata, "LINUX") == 0)
   10553 	return elfcore_grok_ppc_pmu (abfd, note);
   10554       else
   10555 	return true;
   10556 
   10557     case NT_PPC_TM_CGPR:
   10558       if (note->namesz == 6
   10559 	  && strcmp (note->namedata, "LINUX") == 0)
   10560 	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   10561       else
   10562 	return true;
   10563 
   10564     case NT_PPC_TM_CFPR:
   10565       if (note->namesz == 6
   10566 	  && strcmp (note->namedata, "LINUX") == 0)
   10567 	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   10568       else
   10569 	return true;
   10570 
   10571     case NT_PPC_TM_CVMX:
   10572       if (note->namesz == 6
   10573 	  && strcmp (note->namedata, "LINUX") == 0)
   10574 	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   10575       else
   10576 	return true;
   10577 
   10578     case NT_PPC_TM_CVSX:
   10579       if (note->namesz == 6
   10580 	  && strcmp (note->namedata, "LINUX") == 0)
   10581 	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   10582       else
   10583 	return true;
   10584 
   10585     case NT_PPC_TM_SPR:
   10586       if (note->namesz == 6
   10587 	  && strcmp (note->namedata, "LINUX") == 0)
   10588 	return elfcore_grok_ppc_tm_spr (abfd, note);
   10589       else
   10590 	return true;
   10591 
   10592     case NT_PPC_TM_CTAR:
   10593       if (note->namesz == 6
   10594 	  && strcmp (note->namedata, "LINUX") == 0)
   10595 	return elfcore_grok_ppc_tm_ctar (abfd, note);
   10596       else
   10597 	return true;
   10598 
   10599     case NT_PPC_TM_CPPR:
   10600       if (note->namesz == 6
   10601 	  && strcmp (note->namedata, "LINUX") == 0)
   10602 	return elfcore_grok_ppc_tm_cppr (abfd, note);
   10603       else
   10604 	return true;
   10605 
   10606     case NT_PPC_TM_CDSCR:
   10607       if (note->namesz == 6
   10608 	  && strcmp (note->namedata, "LINUX") == 0)
   10609 	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   10610       else
   10611 	return true;
   10612 
   10613     case NT_S390_HIGH_GPRS:
   10614       if (note->namesz == 6
   10615 	  && strcmp (note->namedata, "LINUX") == 0)
   10616 	return elfcore_grok_s390_high_gprs (abfd, note);
   10617       else
   10618 	return true;
   10619 
   10620     case NT_S390_TIMER:
   10621       if (note->namesz == 6
   10622 	  && strcmp (note->namedata, "LINUX") == 0)
   10623 	return elfcore_grok_s390_timer (abfd, note);
   10624       else
   10625 	return true;
   10626 
   10627     case NT_S390_TODCMP:
   10628       if (note->namesz == 6
   10629 	  && strcmp (note->namedata, "LINUX") == 0)
   10630 	return elfcore_grok_s390_todcmp (abfd, note);
   10631       else
   10632 	return true;
   10633 
   10634     case NT_S390_TODPREG:
   10635       if (note->namesz == 6
   10636 	  && strcmp (note->namedata, "LINUX") == 0)
   10637 	return elfcore_grok_s390_todpreg (abfd, note);
   10638       else
   10639 	return true;
   10640 
   10641     case NT_S390_CTRS:
   10642       if (note->namesz == 6
   10643 	  && strcmp (note->namedata, "LINUX") == 0)
   10644 	return elfcore_grok_s390_ctrs (abfd, note);
   10645       else
   10646 	return true;
   10647 
   10648     case NT_S390_PREFIX:
   10649       if (note->namesz == 6
   10650 	  && strcmp (note->namedata, "LINUX") == 0)
   10651 	return elfcore_grok_s390_prefix (abfd, note);
   10652       else
   10653 	return true;
   10654 
   10655     case NT_S390_LAST_BREAK:
   10656       if (note->namesz == 6
   10657 	  && strcmp (note->namedata, "LINUX") == 0)
   10658 	return elfcore_grok_s390_last_break (abfd, note);
   10659       else
   10660 	return true;
   10661 
   10662     case NT_S390_SYSTEM_CALL:
   10663       if (note->namesz == 6
   10664 	  && strcmp (note->namedata, "LINUX") == 0)
   10665 	return elfcore_grok_s390_system_call (abfd, note);
   10666       else
   10667 	return true;
   10668 
   10669     case NT_S390_TDB:
   10670       if (note->namesz == 6
   10671 	  && strcmp (note->namedata, "LINUX") == 0)
   10672 	return elfcore_grok_s390_tdb (abfd, note);
   10673       else
   10674 	return true;
   10675 
   10676     case NT_S390_VXRS_LOW:
   10677       if (note->namesz == 6
   10678 	  && strcmp (note->namedata, "LINUX") == 0)
   10679 	return elfcore_grok_s390_vxrs_low (abfd, note);
   10680       else
   10681 	return true;
   10682 
   10683     case NT_S390_VXRS_HIGH:
   10684       if (note->namesz == 6
   10685 	  && strcmp (note->namedata, "LINUX") == 0)
   10686 	return elfcore_grok_s390_vxrs_high (abfd, note);
   10687       else
   10688 	return true;
   10689 
   10690     case NT_S390_GS_CB:
   10691       if (note->namesz == 6
   10692 	  && strcmp (note->namedata, "LINUX") == 0)
   10693 	return elfcore_grok_s390_gs_cb (abfd, note);
   10694       else
   10695 	return true;
   10696 
   10697     case NT_S390_GS_BC:
   10698       if (note->namesz == 6
   10699 	  && strcmp (note->namedata, "LINUX") == 0)
   10700 	return elfcore_grok_s390_gs_bc (abfd, note);
   10701       else
   10702 	return true;
   10703 
   10704     case NT_ARC_V2:
   10705       if (note->namesz == 6
   10706 	  && strcmp (note->namedata, "LINUX") == 0)
   10707 	return elfcore_grok_arc_v2 (abfd, note);
   10708       else
   10709 	return true;
   10710 
   10711     case NT_ARM_VFP:
   10712       if (note->namesz == 6
   10713 	  && strcmp (note->namedata, "LINUX") == 0)
   10714 	return elfcore_grok_arm_vfp (abfd, note);
   10715       else
   10716 	return true;
   10717 
   10718     case NT_ARM_TLS:
   10719       if (note->namesz == 6
   10720 	  && strcmp (note->namedata, "LINUX") == 0)
   10721 	return elfcore_grok_aarch_tls (abfd, note);
   10722       else
   10723 	return true;
   10724 
   10725     case NT_ARM_HW_BREAK:
   10726       if (note->namesz == 6
   10727 	  && strcmp (note->namedata, "LINUX") == 0)
   10728 	return elfcore_grok_aarch_hw_break (abfd, note);
   10729       else
   10730 	return true;
   10731 
   10732     case NT_ARM_HW_WATCH:
   10733       if (note->namesz == 6
   10734 	  && strcmp (note->namedata, "LINUX") == 0)
   10735 	return elfcore_grok_aarch_hw_watch (abfd, note);
   10736       else
   10737 	return true;
   10738 
   10739     case NT_ARM_SVE:
   10740       if (note->namesz == 6
   10741 	  && strcmp (note->namedata, "LINUX") == 0)
   10742 	return elfcore_grok_aarch_sve (abfd, note);
   10743       else
   10744 	return true;
   10745 
   10746     case NT_ARM_PAC_MASK:
   10747       if (note->namesz == 6
   10748 	  && strcmp (note->namedata, "LINUX") == 0)
   10749 	return elfcore_grok_aarch_pauth (abfd, note);
   10750       else
   10751 	return true;
   10752 
   10753     case NT_ARM_TAGGED_ADDR_CTRL:
   10754       if (note->namesz == 6
   10755 	  && strcmp (note->namedata, "LINUX") == 0)
   10756 	return elfcore_grok_aarch_mte (abfd, note);
   10757       else
   10758 	return true;
   10759 
   10760     case NT_GDB_TDESC:
   10761       if (note->namesz == 4
   10762           && strcmp (note->namedata, "GDB") == 0)
   10763         return elfcore_grok_gdb_tdesc (abfd, note);
   10764       else
   10765         return true;
   10766 
   10767     case NT_RISCV_CSR:
   10768       if (note->namesz == 4
   10769           && strcmp (note->namedata, "GDB") == 0)
   10770         return elfcore_grok_riscv_csr (abfd, note);
   10771       else
   10772 	return true;
   10773 
   10774     case NT_LARCH_CPUCFG:
   10775       if (note->namesz == 6
   10776 	  && strcmp (note->namedata, "LINUX") == 0)
   10777 	return elfcore_grok_loongarch_cpucfg (abfd, note);
   10778       else
   10779 	return true;
   10780 
   10781     case NT_LARCH_LBT:
   10782       if (note->namesz == 6
   10783 	  && strcmp (note->namedata, "LINUX") == 0)
   10784 	return elfcore_grok_loongarch_lbt (abfd, note);
   10785       else
   10786 	return true;
   10787 
   10788     case NT_LARCH_LSX:
   10789       if (note->namesz == 6
   10790 	  && strcmp (note->namedata, "LINUX") == 0)
   10791 	return elfcore_grok_loongarch_lsx (abfd, note);
   10792       else
   10793 	return true;
   10794 
   10795     case NT_LARCH_LASX:
   10796       if (note->namesz == 6
   10797 	  && strcmp (note->namedata, "LINUX") == 0)
   10798 	return elfcore_grok_loongarch_lasx (abfd, note);
   10799       else
   10800 	return true;
   10801 
   10802     case NT_PRPSINFO:
   10803     case NT_PSINFO:
   10804       if (bed->elf_backend_grok_psinfo)
   10805 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   10806 	  return true;
   10807 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10808       return elfcore_grok_psinfo (abfd, note);
   10809 #else
   10810       return true;
   10811 #endif
   10812 
   10813     case NT_AUXV:
   10814       return elfcore_make_auxv_note_section (abfd, note, 0);
   10815 
   10816     case NT_FILE:
   10817       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   10818 					      note);
   10819 
   10820     case NT_SIGINFO:
   10821       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   10822 					      note);
   10823 
   10824     }
   10825 }
   10826 
   10827 static bool
   10828 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   10829 {
   10830   struct bfd_build_id* build_id;
   10831 
   10832   if (note->descsz == 0)
   10833     return false;
   10834 
   10835   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   10836   if (build_id == NULL)
   10837     return false;
   10838 
   10839   build_id->size = note->descsz;
   10840   memcpy (build_id->data, note->descdata, note->descsz);
   10841   abfd->build_id = build_id;
   10842 
   10843   return true;
   10844 }
   10845 
   10846 static bool
   10847 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   10848 {
   10849   switch (note->type)
   10850     {
   10851     default:
   10852       return true;
   10853 
   10854     case NT_GNU_PROPERTY_TYPE_0:
   10855       return _bfd_elf_parse_gnu_properties (abfd, note);
   10856 
   10857     case NT_GNU_BUILD_ID:
   10858       return elfobj_grok_gnu_build_id (abfd, note);
   10859     }
   10860 }
   10861 
   10862 static bool
   10863 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   10864 {
   10865   struct sdt_note *cur =
   10866     (struct sdt_note *) bfd_alloc (abfd,
   10867 				   sizeof (struct sdt_note) + note->descsz);
   10868 
   10869   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   10870   cur->size = (bfd_size_type) note->descsz;
   10871   memcpy (cur->data, note->descdata, note->descsz);
   10872 
   10873   elf_tdata (abfd)->sdt_note_head = cur;
   10874 
   10875   return true;
   10876 }
   10877 
   10878 static bool
   10879 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   10880 {
   10881   switch (note->type)
   10882     {
   10883     case NT_STAPSDT:
   10884       return elfobj_grok_stapsdt_note_1 (abfd, note);
   10885 
   10886     default:
   10887       return true;
   10888     }
   10889 }
   10890 
   10891 static bool
   10892 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10893 {
   10894   size_t offset;
   10895 
   10896   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10897     {
   10898     case ELFCLASS32:
   10899       if (note->descsz < 108)
   10900 	return false;
   10901       break;
   10902 
   10903     case ELFCLASS64:
   10904       if (note->descsz < 120)
   10905 	return false;
   10906       break;
   10907 
   10908     default:
   10909       return false;
   10910     }
   10911 
   10912   /* Check for version 1 in pr_version.  */
   10913   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10914     return false;
   10915 
   10916   offset = 4;
   10917 
   10918   /* Skip over pr_psinfosz. */
   10919   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   10920     offset += 4;
   10921   else
   10922     {
   10923       offset += 4;	/* Padding before pr_psinfosz. */
   10924       offset += 8;
   10925     }
   10926 
   10927   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   10928   elf_tdata (abfd)->core->program
   10929     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   10930   offset += 17;
   10931 
   10932   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   10933   elf_tdata (abfd)->core->command
   10934     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   10935   offset += 81;
   10936 
   10937   /* Padding before pr_pid.  */
   10938   offset += 2;
   10939 
   10940   /* The pr_pid field was added in version "1a".  */
   10941   if (note->descsz < offset + 4)
   10942     return true;
   10943 
   10944   elf_tdata (abfd)->core->pid
   10945     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10946 
   10947   return true;
   10948 }
   10949 
   10950 static bool
   10951 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10952 {
   10953   size_t offset;
   10954   size_t size;
   10955   size_t min_size;
   10956 
   10957   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   10958      Also compute minimum size of this note.  */
   10959   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10960     {
   10961     case ELFCLASS32:
   10962       offset = 4 + 4;
   10963       min_size = offset + (4 * 2) + 4 + 4 + 4;
   10964       break;
   10965 
   10966     case ELFCLASS64:
   10967       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   10968       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   10969       break;
   10970 
   10971     default:
   10972       return false;
   10973     }
   10974 
   10975   if (note->descsz < min_size)
   10976     return false;
   10977 
   10978   /* Check for version 1 in pr_version.  */
   10979   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10980     return false;
   10981 
   10982   /* Extract size of pr_reg from pr_gregsetsz.  */
   10983   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   10984   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   10985     {
   10986       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10987       offset += 4 * 2;
   10988     }
   10989   else
   10990     {
   10991       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   10992       offset += 8 * 2;
   10993     }
   10994 
   10995   /* Skip over pr_osreldate.  */
   10996   offset += 4;
   10997 
   10998   /* Read signal from pr_cursig.  */
   10999   if (elf_tdata (abfd)->core->signal == 0)
   11000     elf_tdata (abfd)->core->signal
   11001       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11002   offset += 4;
   11003 
   11004   /* Read TID from pr_pid.  */
   11005   elf_tdata (abfd)->core->lwpid
   11006       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11007   offset += 4;
   11008 
   11009   /* Padding before pr_reg.  */
   11010   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11011     offset += 4;
   11012 
   11013   /* Make sure that there is enough data remaining in the note.  */
   11014   if ((note->descsz - offset) < size)
   11015     return false;
   11016 
   11017   /* Make a ".reg/999" section and a ".reg" section.  */
   11018   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   11019 					  size, note->descpos + offset);
   11020 }
   11021 
   11022 static bool
   11023 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11024 {
   11025   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11026 
   11027   switch (note->type)
   11028     {
   11029     case NT_PRSTATUS:
   11030       if (bed->elf_backend_grok_freebsd_prstatus)
   11031 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11032 	  return true;
   11033       return elfcore_grok_freebsd_prstatus (abfd, note);
   11034 
   11035     case NT_FPREGSET:
   11036       return elfcore_grok_prfpreg (abfd, note);
   11037 
   11038     case NT_PRPSINFO:
   11039       return elfcore_grok_freebsd_psinfo (abfd, note);
   11040 
   11041     case NT_FREEBSD_THRMISC:
   11042       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11043 
   11044     case NT_FREEBSD_PROCSTAT_PROC:
   11045       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11046 					      note);
   11047 
   11048     case NT_FREEBSD_PROCSTAT_FILES:
   11049       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11050 					      note);
   11051 
   11052     case NT_FREEBSD_PROCSTAT_VMMAP:
   11053       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11054 					      note);
   11055 
   11056     case NT_FREEBSD_PROCSTAT_AUXV:
   11057       return elfcore_make_auxv_note_section (abfd, note, 4);
   11058 
   11059     case NT_FREEBSD_X86_SEGBASES:
   11060       return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
   11061 
   11062     case NT_X86_XSTATE:
   11063       return elfcore_grok_xstatereg (abfd, note);
   11064 
   11065     case NT_FREEBSD_PTLWPINFO:
   11066       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11067 					      note);
   11068 
   11069     case NT_ARM_TLS:
   11070       return elfcore_grok_aarch_tls (abfd, note);
   11071 
   11072     case NT_ARM_VFP:
   11073       return elfcore_grok_arm_vfp (abfd, note);
   11074 
   11075     default:
   11076       return true;
   11077     }
   11078 }
   11079 
   11080 static bool
   11081 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11082 {
   11083   char *cp;
   11084 
   11085   cp = strchr (note->namedata, '@');
   11086   if (cp != NULL)
   11087     {
   11088       *lwpidp = atoi(cp + 1);
   11089       return true;
   11090     }
   11091   return false;
   11092 }
   11093 
   11094 static bool
   11095 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11096 {
   11097   if (note->descsz <= 0x7c + 31)
   11098     return false;
   11099 
   11100   /* Signal number at offset 0x08. */
   11101   elf_tdata (abfd)->core->signal
   11102     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11103 
   11104   /* Process ID at offset 0x50. */
   11105   elf_tdata (abfd)->core->pid
   11106     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11107 
   11108   /* Command name at 0x7c (max 32 bytes, including nul). */
   11109   elf_tdata (abfd)->core->command
   11110     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11111 
   11112   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11113 					  note);
   11114 }
   11115 
   11116 static bool
   11117 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11118 {
   11119   int lwp;
   11120 
   11121   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11122     elf_tdata (abfd)->core->lwpid = lwp;
   11123 
   11124   switch (note->type)
   11125     {
   11126     case NT_NETBSDCORE_PROCINFO:
   11127       /* NetBSD-specific core "procinfo".  Note that we expect to
   11128 	 find this note before any of the others, which is fine,
   11129 	 since the kernel writes this note out first when it
   11130 	 creates a core file.  */
   11131       return elfcore_grok_netbsd_procinfo (abfd, note);
   11132     case NT_NETBSDCORE_AUXV:
   11133       /* NetBSD-specific Elf Auxiliary Vector data. */
   11134       return elfcore_make_auxv_note_section (abfd, note, 4);
   11135     case NT_NETBSDCORE_LWPSTATUS:
   11136       return elfcore_make_note_pseudosection (abfd,
   11137 					      ".note.netbsdcore.lwpstatus",
   11138 					      note);
   11139     default:
   11140       break;
   11141     }
   11142 
   11143   /* As of March 2020 there are no other machine-independent notes
   11144      defined for NetBSD core files.  If the note type is less
   11145      than the start of the machine-dependent note types, we don't
   11146      understand it.  */
   11147 
   11148   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11149     return true;
   11150 
   11151 
   11152   switch (bfd_get_arch (abfd))
   11153     {
   11154       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11155 	 PT_GETFPREGS == mach+2.  */
   11156 
   11157     case bfd_arch_aarch64:
   11158     case bfd_arch_alpha:
   11159     case bfd_arch_sparc:
   11160       switch (note->type)
   11161 	{
   11162 	case NT_NETBSDCORE_FIRSTMACH+0:
   11163 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11164 
   11165 	case NT_NETBSDCORE_FIRSTMACH+2:
   11166 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11167 
   11168 	default:
   11169 	  return true;
   11170 	}
   11171 
   11172       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11173 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11174 	 structure which lacks GBR.  */
   11175 
   11176     case bfd_arch_sh:
   11177       switch (note->type)
   11178 	{
   11179 	case NT_NETBSDCORE_FIRSTMACH+3:
   11180 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11181 
   11182 	case NT_NETBSDCORE_FIRSTMACH+5:
   11183 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11184 
   11185 	default:
   11186 	  return true;
   11187 	}
   11188 
   11189       /* On all other arch's, PT_GETREGS == mach+1 and
   11190 	 PT_GETFPREGS == mach+3.  */
   11191 
   11192     default:
   11193       switch (note->type)
   11194 	{
   11195 	case NT_NETBSDCORE_FIRSTMACH+1:
   11196 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11197 
   11198 	case NT_NETBSDCORE_FIRSTMACH+3:
   11199 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11200 
   11201 	default:
   11202 	  return true;
   11203 	}
   11204     }
   11205     /* NOTREACHED */
   11206 }
   11207 
   11208 static bool
   11209 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11210 {
   11211   if (note->descsz <= 0x48 + 31)
   11212     return false;
   11213 
   11214   /* Signal number at offset 0x08. */
   11215   elf_tdata (abfd)->core->signal
   11216     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11217 
   11218   /* Process ID at offset 0x20. */
   11219   elf_tdata (abfd)->core->pid
   11220     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11221 
   11222   /* Command name at 0x48 (max 32 bytes, including nul). */
   11223   elf_tdata (abfd)->core->command
   11224     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11225 
   11226   return true;
   11227 }
   11228 
   11229 /* Processes Solaris's process status note.
   11230    sig_off ~ offsetof(prstatus_t, pr_cursig)
   11231    pid_off ~ offsetof(prstatus_t, pr_pid)
   11232    lwpid_off ~ offsetof(prstatus_t, pr_who)
   11233    gregset_size ~ sizeof(gregset_t)
   11234    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   11235 
   11236 static bool
   11237 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   11238 			       int pid_off, int lwpid_off, size_t gregset_size,
   11239 			       size_t gregset_offset)
   11240 {
   11241   asection *sect = NULL;
   11242   elf_tdata (abfd)->core->signal
   11243     = bfd_get_16 (abfd, note->descdata + sig_off);
   11244   elf_tdata (abfd)->core->pid
   11245     = bfd_get_32 (abfd, note->descdata + pid_off);
   11246   elf_tdata (abfd)->core->lwpid
   11247     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   11248 
   11249   sect = bfd_get_section_by_name (abfd, ".reg");
   11250   if (sect != NULL)
   11251     sect->size = gregset_size;
   11252 
   11253   return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11254 					  note->descpos + gregset_offset);
   11255 }
   11256 
   11257 /* Gets program and arguments from a core.
   11258    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   11259    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   11260 
   11261 static bool
   11262 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   11263 			  int prog_off, int comm_off)
   11264 {
   11265   elf_tdata (abfd)->core->program
   11266     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   11267   elf_tdata (abfd)->core->command
   11268     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   11269 
   11270   return true;
   11271 }
   11272 
   11273 /* Processes Solaris's LWP status note.
   11274    gregset_size ~ sizeof(gregset_t)
   11275    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   11276    fpregset_size ~ sizeof(fpregset_t)
   11277    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   11278 
   11279 static bool
   11280 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   11281 				size_t gregset_size, int gregset_off,
   11282 				size_t fpregset_size, int fpregset_off)
   11283 {
   11284   asection *sect = NULL;
   11285   char reg2_section_name[16] = { 0 };
   11286 
   11287   (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
   11288 		   elf_tdata (abfd)->core->lwpid);
   11289 
   11290   /* offsetof(lwpstatus_t, pr_lwpid) */
   11291   elf_tdata (abfd)->core->lwpid
   11292     = bfd_get_32 (abfd, note->descdata + 4);
   11293   /* offsetof(lwpstatus_t, pr_cursig) */
   11294   elf_tdata (abfd)->core->signal
   11295     = bfd_get_16 (abfd, note->descdata + 12);
   11296 
   11297   sect = bfd_get_section_by_name (abfd, ".reg");
   11298   if (sect != NULL)
   11299     sect->size = gregset_size;
   11300   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11301 					     note->descpos + gregset_off))
   11302     return false;
   11303 
   11304   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   11305   if (sect != NULL)
   11306     {
   11307       sect->size = fpregset_size;
   11308       sect->filepos = note->descpos + fpregset_off;
   11309       sect->alignment_power = 2;
   11310     }
   11311   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
   11312 					     note->descpos + fpregset_off))
   11313     return false;
   11314 
   11315   return true;
   11316 }
   11317 
   11318 static bool
   11319 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   11320 {
   11321   if (note == NULL)
   11322     return false;
   11323 
   11324   /* core files are identified as 32- or 64-bit, SPARC or x86,
   11325      by the size of the descsz which matches the sizeof()
   11326      the type appropriate for that note type (e.g., prstatus_t for
   11327      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   11328      on Solaris. The core file bitness may differ from the bitness of
   11329      gdb itself, so fixed values are used instead of sizeof().
   11330      Appropriate fixed offsets are also used to obtain data from
   11331      the note.  */
   11332 
   11333   switch ((int) note->type)
   11334     {
   11335     case SOLARIS_NT_PRSTATUS:
   11336       switch (note->descsz)
   11337 	{
   11338 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   11339 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11340 					       136, 216, 308, 152, 356);
   11341 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   11342 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11343 					       264, 360, 520, 304, 600);
   11344 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   11345 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11346 					       136, 216, 308, 76, 356);
   11347 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   11348 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11349 					       264, 360, 520, 224, 600);
   11350 	default:
   11351 	  return true;
   11352 	}
   11353 
   11354     case SOLARIS_NT_PSINFO:
   11355     case SOLARIS_NT_PRPSINFO:
   11356       switch (note->descsz)
   11357 	{
   11358 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   11359 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   11360 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   11361 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   11362 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   11363 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   11364 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   11365 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   11366 	default:
   11367 	  return true;
   11368 	}
   11369 
   11370     case SOLARIS_NT_LWPSTATUS:
   11371       switch (note->descsz)
   11372 	{
   11373 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   11374 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11375 						152, 344, 400, 496);
   11376 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   11377 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11378 						304, 544, 544, 848);
   11379 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   11380 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11381 						76, 344, 380, 420);
   11382 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   11383 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11384 						224, 544, 528, 768);
   11385 	default:
   11386 	  return true;
   11387 	}
   11388 
   11389     case SOLARIS_NT_LWPSINFO:
   11390       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   11391       if (note->descsz == 128 || note->descsz == 152)
   11392 	elf_tdata (abfd)->core->lwpid =
   11393 	  bfd_get_32 (abfd, note->descdata + 4);
   11394       break;
   11395 
   11396     default:
   11397       break;
   11398     }
   11399 
   11400   return true;
   11401 }
   11402 
   11403 /* For name starting with "CORE" this may be either a Solaris
   11404    core file or a gdb-generated core file.  Do Solaris-specific
   11405    processing on selected note types first with
   11406    elfcore_grok_solaris_note(), then process the note
   11407    in elfcore_grok_note().  */
   11408 
   11409 static bool
   11410 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   11411 {
   11412   if (!elfcore_grok_solaris_note_impl (abfd, note))
   11413     return false;
   11414 
   11415   return elfcore_grok_note (abfd, note);
   11416 }
   11417 
   11418 static bool
   11419 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11420 {
   11421   if (note->type == NT_OPENBSD_PROCINFO)
   11422     return elfcore_grok_openbsd_procinfo (abfd, note);
   11423 
   11424   if (note->type == NT_OPENBSD_REGS)
   11425     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11426 
   11427   if (note->type == NT_OPENBSD_FPREGS)
   11428     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11429 
   11430   if (note->type == NT_OPENBSD_XFPREGS)
   11431     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   11432 
   11433   if (note->type == NT_OPENBSD_AUXV)
   11434     return elfcore_make_auxv_note_section (abfd, note, 0);
   11435 
   11436   if (note->type == NT_OPENBSD_WCOOKIE)
   11437     {
   11438       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   11439 							   SEC_HAS_CONTENTS);
   11440 
   11441       if (sect == NULL)
   11442 	return false;
   11443       sect->size = note->descsz;
   11444       sect->filepos = note->descpos;
   11445       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   11446 
   11447       return true;
   11448     }
   11449 
   11450   return true;
   11451 }
   11452 
   11453 static bool
   11454 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   11455 {
   11456   void *ddata = note->descdata;
   11457   char buf[100];
   11458   char *name;
   11459   asection *sect;
   11460   short sig;
   11461   unsigned flags;
   11462 
   11463   if (note->descsz < 16)
   11464     return false;
   11465 
   11466   /* nto_procfs_status 'pid' field is at offset 0.  */
   11467   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   11468 
   11469   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   11470   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   11471 
   11472   /* nto_procfs_status 'flags' field is at offset 8.  */
   11473   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   11474 
   11475   /* nto_procfs_status 'what' field is at offset 14.  */
   11476   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   11477     {
   11478       elf_tdata (abfd)->core->signal = sig;
   11479       elf_tdata (abfd)->core->lwpid = *tid;
   11480     }
   11481 
   11482   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   11483      do not come from signals so we make sure we set the current
   11484      thread just in case.  */
   11485   if (flags & 0x00000080)
   11486     elf_tdata (abfd)->core->lwpid = *tid;
   11487 
   11488   /* Make a ".qnx_core_status/%d" section.  */
   11489   sprintf (buf, ".qnx_core_status/%ld", *tid);
   11490 
   11491   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11492   if (name == NULL)
   11493     return false;
   11494   strcpy (name, buf);
   11495 
   11496   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11497   if (sect == NULL)
   11498     return false;
   11499 
   11500   sect->size		= note->descsz;
   11501   sect->filepos		= note->descpos;
   11502   sect->alignment_power = 2;
   11503 
   11504   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   11505 }
   11506 
   11507 static bool
   11508 elfcore_grok_nto_regs (bfd *abfd,
   11509 		       Elf_Internal_Note *note,
   11510 		       long tid,
   11511 		       char *base)
   11512 {
   11513   char buf[100];
   11514   char *name;
   11515   asection *sect;
   11516 
   11517   /* Make a "(base)/%d" section.  */
   11518   sprintf (buf, "%s/%ld", base, tid);
   11519 
   11520   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11521   if (name == NULL)
   11522     return false;
   11523   strcpy (name, buf);
   11524 
   11525   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11526   if (sect == NULL)
   11527     return false;
   11528 
   11529   sect->size		= note->descsz;
   11530   sect->filepos		= note->descpos;
   11531   sect->alignment_power = 2;
   11532 
   11533   /* This is the current thread.  */
   11534   if (elf_tdata (abfd)->core->lwpid == tid)
   11535     return elfcore_maybe_make_sect (abfd, base, sect);
   11536 
   11537   return true;
   11538 }
   11539 
   11540 #define BFD_QNT_CORE_INFO	7
   11541 #define BFD_QNT_CORE_STATUS	8
   11542 #define BFD_QNT_CORE_GREG	9
   11543 #define BFD_QNT_CORE_FPREG	10
   11544 
   11545 static bool
   11546 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   11547 {
   11548   /* Every GREG section has a STATUS section before it.  Store the
   11549      tid from the previous call to pass down to the next gregs
   11550      function.  */
   11551   static long tid = 1;
   11552 
   11553   switch (note->type)
   11554     {
   11555     case BFD_QNT_CORE_INFO:
   11556       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   11557     case BFD_QNT_CORE_STATUS:
   11558       return elfcore_grok_nto_status (abfd, note, &tid);
   11559     case BFD_QNT_CORE_GREG:
   11560       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   11561     case BFD_QNT_CORE_FPREG:
   11562       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   11563     default:
   11564       return true;
   11565     }
   11566 }
   11567 
   11568 static bool
   11569 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   11570 {
   11571   char *name;
   11572   asection *sect;
   11573   size_t len;
   11574 
   11575   /* Use note name as section name.  */
   11576   len = note->namesz;
   11577   name = (char *) bfd_alloc (abfd, len);
   11578   if (name == NULL)
   11579     return false;
   11580   memcpy (name, note->namedata, len);
   11581   name[len - 1] = '\0';
   11582 
   11583   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11584   if (sect == NULL)
   11585     return false;
   11586 
   11587   sect->size		= note->descsz;
   11588   sect->filepos		= note->descpos;
   11589   sect->alignment_power = 1;
   11590 
   11591   return true;
   11592 }
   11593 
   11594 /* Function: elfcore_write_note
   11595 
   11596    Inputs:
   11597      buffer to hold note, and current size of buffer
   11598      name of note
   11599      type of note
   11600      data for note
   11601      size of data for note
   11602 
   11603    Writes note to end of buffer.  ELF64 notes are written exactly as
   11604    for ELF32, despite the current (as of 2006) ELF gabi specifying
   11605    that they ought to have 8-byte namesz and descsz field, and have
   11606    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   11607 
   11608    Return:
   11609    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   11610 
   11611 char *
   11612 elfcore_write_note (bfd *abfd,
   11613 		    char *buf,
   11614 		    int *bufsiz,
   11615 		    const char *name,
   11616 		    int type,
   11617 		    const void *input,
   11618 		    int size)
   11619 {
   11620   Elf_External_Note *xnp;
   11621   size_t namesz;
   11622   size_t newspace;
   11623   char *dest;
   11624 
   11625   namesz = 0;
   11626   if (name != NULL)
   11627     namesz = strlen (name) + 1;
   11628 
   11629   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   11630 
   11631   buf = (char *) realloc (buf, *bufsiz + newspace);
   11632   if (buf == NULL)
   11633     return buf;
   11634   dest = buf + *bufsiz;
   11635   *bufsiz += newspace;
   11636   xnp = (Elf_External_Note *) dest;
   11637   H_PUT_32 (abfd, namesz, xnp->namesz);
   11638   H_PUT_32 (abfd, size, xnp->descsz);
   11639   H_PUT_32 (abfd, type, xnp->type);
   11640   dest = xnp->name;
   11641   if (name != NULL)
   11642     {
   11643       memcpy (dest, name, namesz);
   11644       dest += namesz;
   11645       while (namesz & 3)
   11646 	{
   11647 	  *dest++ = '\0';
   11648 	  ++namesz;
   11649 	}
   11650     }
   11651   memcpy (dest, input, size);
   11652   dest += size;
   11653   while (size & 3)
   11654     {
   11655       *dest++ = '\0';
   11656       ++size;
   11657     }
   11658   return buf;
   11659 }
   11660 
   11661 /* gcc-8 warns (*) on all the strncpy calls in this function about
   11662    possible string truncation.  The "truncation" is not a bug.  We
   11663    have an external representation of structs with fields that are not
   11664    necessarily NULL terminated and corresponding internal
   11665    representation fields that are one larger so that they can always
   11666    be NULL terminated.
   11667    gcc versions between 4.2 and 4.6 do not allow pragma control of
   11668    diagnostics inside functions, giving a hard error if you try to use
   11669    the finer control available with later versions.
   11670    gcc prior to 4.2 warns about diagnostic push and pop.
   11671    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   11672    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   11673    (*) Depending on your system header files!  */
   11674 #if GCC_VERSION >= 8000
   11675 # pragma GCC diagnostic push
   11676 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   11677 #endif
   11678 char *
   11679 elfcore_write_prpsinfo (bfd  *abfd,
   11680 			char *buf,
   11681 			int  *bufsiz,
   11682 			const char *fname,
   11683 			const char *psargs)
   11684 {
   11685   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11686 
   11687   if (bed->elf_backend_write_core_note != NULL)
   11688     {
   11689       char *ret;
   11690       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11691 						 NT_PRPSINFO, fname, psargs);
   11692       if (ret != NULL)
   11693 	return ret;
   11694     }
   11695 
   11696 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11697 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   11698   if (bed->s->elfclass == ELFCLASS32)
   11699     {
   11700 #  if defined (HAVE_PSINFO32_T)
   11701       psinfo32_t data;
   11702       int note_type = NT_PSINFO;
   11703 #  else
   11704       prpsinfo32_t data;
   11705       int note_type = NT_PRPSINFO;
   11706 #  endif
   11707 
   11708       memset (&data, 0, sizeof (data));
   11709       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11710       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11711       return elfcore_write_note (abfd, buf, bufsiz,
   11712 				 "CORE", note_type, &data, sizeof (data));
   11713     }
   11714   else
   11715 # endif
   11716     {
   11717 # if defined (HAVE_PSINFO_T)
   11718       psinfo_t data;
   11719       int note_type = NT_PSINFO;
   11720 # else
   11721       prpsinfo_t data;
   11722       int note_type = NT_PRPSINFO;
   11723 # endif
   11724 
   11725       memset (&data, 0, sizeof (data));
   11726       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11727       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11728       return elfcore_write_note (abfd, buf, bufsiz,
   11729 				 "CORE", note_type, &data, sizeof (data));
   11730     }
   11731 #endif	/* PSINFO_T or PRPSINFO_T */
   11732 
   11733   free (buf);
   11734   return NULL;
   11735 }
   11736 #if GCC_VERSION >= 8000
   11737 # pragma GCC diagnostic pop
   11738 #endif
   11739 
   11740 char *
   11741 elfcore_write_linux_prpsinfo32
   11742   (bfd *abfd, char *buf, int *bufsiz,
   11743    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11744 {
   11745   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   11746     {
   11747       struct elf_external_linux_prpsinfo32_ugid16 data;
   11748 
   11749       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   11750       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11751 				 &data, sizeof (data));
   11752     }
   11753   else
   11754     {
   11755       struct elf_external_linux_prpsinfo32_ugid32 data;
   11756 
   11757       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   11758       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11759 				 &data, sizeof (data));
   11760     }
   11761 }
   11762 
   11763 char *
   11764 elfcore_write_linux_prpsinfo64
   11765   (bfd *abfd, char *buf, int *bufsiz,
   11766    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11767 {
   11768   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   11769     {
   11770       struct elf_external_linux_prpsinfo64_ugid16 data;
   11771 
   11772       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   11773       return elfcore_write_note (abfd, buf, bufsiz,
   11774 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11775     }
   11776   else
   11777     {
   11778       struct elf_external_linux_prpsinfo64_ugid32 data;
   11779 
   11780       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   11781       return elfcore_write_note (abfd, buf, bufsiz,
   11782 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11783     }
   11784 }
   11785 
   11786 char *
   11787 elfcore_write_prstatus (bfd *abfd,
   11788 			char *buf,
   11789 			int *bufsiz,
   11790 			long pid,
   11791 			int cursig,
   11792 			const void *gregs)
   11793 {
   11794   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11795 
   11796   if (bed->elf_backend_write_core_note != NULL)
   11797     {
   11798       char *ret;
   11799       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11800 						 NT_PRSTATUS,
   11801 						 pid, cursig, gregs);
   11802       if (ret != NULL)
   11803 	return ret;
   11804     }
   11805 
   11806 #if defined (HAVE_PRSTATUS_T)
   11807 #if defined (HAVE_PRSTATUS32_T)
   11808   if (bed->s->elfclass == ELFCLASS32)
   11809     {
   11810       prstatus32_t prstat;
   11811 
   11812       memset (&prstat, 0, sizeof (prstat));
   11813       prstat.pr_pid = pid;
   11814       prstat.pr_cursig = cursig;
   11815       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11816       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11817 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11818     }
   11819   else
   11820 #endif
   11821     {
   11822       prstatus_t prstat;
   11823 
   11824       memset (&prstat, 0, sizeof (prstat));
   11825       prstat.pr_pid = pid;
   11826       prstat.pr_cursig = cursig;
   11827       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11828       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11829 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11830     }
   11831 #endif /* HAVE_PRSTATUS_T */
   11832 
   11833   free (buf);
   11834   return NULL;
   11835 }
   11836 
   11837 #if defined (HAVE_LWPSTATUS_T)
   11838 char *
   11839 elfcore_write_lwpstatus (bfd *abfd,
   11840 			 char *buf,
   11841 			 int *bufsiz,
   11842 			 long pid,
   11843 			 int cursig,
   11844 			 const void *gregs)
   11845 {
   11846   lwpstatus_t lwpstat;
   11847   const char *note_name = "CORE";
   11848 
   11849   memset (&lwpstat, 0, sizeof (lwpstat));
   11850   lwpstat.pr_lwpid  = pid >> 16;
   11851   lwpstat.pr_cursig = cursig;
   11852 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   11853   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   11854 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11855 #if !defined(gregs)
   11856   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   11857 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   11858 #else
   11859   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   11860 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   11861 #endif
   11862 #endif
   11863   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   11864 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   11865 }
   11866 #endif /* HAVE_LWPSTATUS_T */
   11867 
   11868 #if defined (HAVE_PSTATUS_T)
   11869 char *
   11870 elfcore_write_pstatus (bfd *abfd,
   11871 		       char *buf,
   11872 		       int *bufsiz,
   11873 		       long pid,
   11874 		       int cursig ATTRIBUTE_UNUSED,
   11875 		       const void *gregs ATTRIBUTE_UNUSED)
   11876 {
   11877   const char *note_name = "CORE";
   11878 #if defined (HAVE_PSTATUS32_T)
   11879   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11880 
   11881   if (bed->s->elfclass == ELFCLASS32)
   11882     {
   11883       pstatus32_t pstat;
   11884 
   11885       memset (&pstat, 0, sizeof (pstat));
   11886       pstat.pr_pid = pid & 0xffff;
   11887       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11888 				NT_PSTATUS, &pstat, sizeof (pstat));
   11889       return buf;
   11890     }
   11891   else
   11892 #endif
   11893     {
   11894       pstatus_t pstat;
   11895 
   11896       memset (&pstat, 0, sizeof (pstat));
   11897       pstat.pr_pid = pid & 0xffff;
   11898       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11899 				NT_PSTATUS, &pstat, sizeof (pstat));
   11900       return buf;
   11901     }
   11902 }
   11903 #endif /* HAVE_PSTATUS_T */
   11904 
   11905 char *
   11906 elfcore_write_prfpreg (bfd *abfd,
   11907 		       char *buf,
   11908 		       int *bufsiz,
   11909 		       const void *fpregs,
   11910 		       int size)
   11911 {
   11912   const char *note_name = "CORE";
   11913   return elfcore_write_note (abfd, buf, bufsiz,
   11914 			     note_name, NT_FPREGSET, fpregs, size);
   11915 }
   11916 
   11917 char *
   11918 elfcore_write_prxfpreg (bfd *abfd,
   11919 			char *buf,
   11920 			int *bufsiz,
   11921 			const void *xfpregs,
   11922 			int size)
   11923 {
   11924   char *note_name = "LINUX";
   11925   return elfcore_write_note (abfd, buf, bufsiz,
   11926 			     note_name, NT_PRXFPREG, xfpregs, size);
   11927 }
   11928 
   11929 char *
   11930 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   11931 			 const void *xfpregs, int size)
   11932 {
   11933   char *note_name;
   11934   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   11935     note_name = "FreeBSD";
   11936   else
   11937     note_name = "LINUX";
   11938   return elfcore_write_note (abfd, buf, bufsiz,
   11939 			     note_name, NT_X86_XSTATE, xfpregs, size);
   11940 }
   11941 
   11942 char *
   11943 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   11944 			    const void *regs, int size)
   11945 {
   11946   char *note_name = "FreeBSD";
   11947   return elfcore_write_note (abfd, buf, bufsiz,
   11948 			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
   11949 }
   11950 
   11951 char *
   11952 elfcore_write_ppc_vmx (bfd *abfd,
   11953 		       char *buf,
   11954 		       int *bufsiz,
   11955 		       const void *ppc_vmx,
   11956 		       int size)
   11957 {
   11958   char *note_name = "LINUX";
   11959   return elfcore_write_note (abfd, buf, bufsiz,
   11960 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   11961 }
   11962 
   11963 char *
   11964 elfcore_write_ppc_vsx (bfd *abfd,
   11965 		       char *buf,
   11966 		       int *bufsiz,
   11967 		       const void *ppc_vsx,
   11968 		       int size)
   11969 {
   11970   char *note_name = "LINUX";
   11971   return elfcore_write_note (abfd, buf, bufsiz,
   11972 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   11973 }
   11974 
   11975 char *
   11976 elfcore_write_ppc_tar (bfd *abfd,
   11977 		       char *buf,
   11978 		       int *bufsiz,
   11979 		       const void *ppc_tar,
   11980 		       int size)
   11981 {
   11982   char *note_name = "LINUX";
   11983   return elfcore_write_note (abfd, buf, bufsiz,
   11984 			     note_name, NT_PPC_TAR, ppc_tar, size);
   11985 }
   11986 
   11987 char *
   11988 elfcore_write_ppc_ppr (bfd *abfd,
   11989 		       char *buf,
   11990 		       int *bufsiz,
   11991 		       const void *ppc_ppr,
   11992 		       int size)
   11993 {
   11994   char *note_name = "LINUX";
   11995   return elfcore_write_note (abfd, buf, bufsiz,
   11996 			     note_name, NT_PPC_PPR, ppc_ppr, size);
   11997 }
   11998 
   11999 char *
   12000 elfcore_write_ppc_dscr (bfd *abfd,
   12001 			char *buf,
   12002 			int *bufsiz,
   12003 			const void *ppc_dscr,
   12004 			int size)
   12005 {
   12006   char *note_name = "LINUX";
   12007   return elfcore_write_note (abfd, buf, bufsiz,
   12008 			     note_name, NT_PPC_DSCR, ppc_dscr, size);
   12009 }
   12010 
   12011 char *
   12012 elfcore_write_ppc_ebb (bfd *abfd,
   12013 		       char *buf,
   12014 		       int *bufsiz,
   12015 		       const void *ppc_ebb,
   12016 		       int size)
   12017 {
   12018   char *note_name = "LINUX";
   12019   return elfcore_write_note (abfd, buf, bufsiz,
   12020 			     note_name, NT_PPC_EBB, ppc_ebb, size);
   12021 }
   12022 
   12023 char *
   12024 elfcore_write_ppc_pmu (bfd *abfd,
   12025 		       char *buf,
   12026 		       int *bufsiz,
   12027 		       const void *ppc_pmu,
   12028 		       int size)
   12029 {
   12030   char *note_name = "LINUX";
   12031   return elfcore_write_note (abfd, buf, bufsiz,
   12032 			     note_name, NT_PPC_PMU, ppc_pmu, size);
   12033 }
   12034 
   12035 char *
   12036 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12037 			   char *buf,
   12038 			   int *bufsiz,
   12039 			   const void *ppc_tm_cgpr,
   12040 			   int size)
   12041 {
   12042   char *note_name = "LINUX";
   12043   return elfcore_write_note (abfd, buf, bufsiz,
   12044 			     note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
   12045 }
   12046 
   12047 char *
   12048 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12049 			   char *buf,
   12050 			   int *bufsiz,
   12051 			   const void *ppc_tm_cfpr,
   12052 			   int size)
   12053 {
   12054   char *note_name = "LINUX";
   12055   return elfcore_write_note (abfd, buf, bufsiz,
   12056 			     note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
   12057 }
   12058 
   12059 char *
   12060 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12061 			   char *buf,
   12062 			   int *bufsiz,
   12063 			   const void *ppc_tm_cvmx,
   12064 			   int size)
   12065 {
   12066   char *note_name = "LINUX";
   12067   return elfcore_write_note (abfd, buf, bufsiz,
   12068 			     note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
   12069 }
   12070 
   12071 char *
   12072 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12073 			   char *buf,
   12074 			   int *bufsiz,
   12075 			   const void *ppc_tm_cvsx,
   12076 			   int size)
   12077 {
   12078   char *note_name = "LINUX";
   12079   return elfcore_write_note (abfd, buf, bufsiz,
   12080 			     note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
   12081 }
   12082 
   12083 char *
   12084 elfcore_write_ppc_tm_spr (bfd *abfd,
   12085 			  char *buf,
   12086 			  int *bufsiz,
   12087 			  const void *ppc_tm_spr,
   12088 			  int size)
   12089 {
   12090   char *note_name = "LINUX";
   12091   return elfcore_write_note (abfd, buf, bufsiz,
   12092 			     note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
   12093 }
   12094 
   12095 char *
   12096 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12097 			   char *buf,
   12098 			   int *bufsiz,
   12099 			   const void *ppc_tm_ctar,
   12100 			   int size)
   12101 {
   12102   char *note_name = "LINUX";
   12103   return elfcore_write_note (abfd, buf, bufsiz,
   12104 			     note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
   12105 }
   12106 
   12107 char *
   12108 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12109 			   char *buf,
   12110 			   int *bufsiz,
   12111 			   const void *ppc_tm_cppr,
   12112 			   int size)
   12113 {
   12114   char *note_name = "LINUX";
   12115   return elfcore_write_note (abfd, buf, bufsiz,
   12116 			     note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
   12117 }
   12118 
   12119 char *
   12120 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12121 			    char *buf,
   12122 			    int *bufsiz,
   12123 			    const void *ppc_tm_cdscr,
   12124 			    int size)
   12125 {
   12126   char *note_name = "LINUX";
   12127   return elfcore_write_note (abfd, buf, bufsiz,
   12128 			     note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
   12129 }
   12130 
   12131 static char *
   12132 elfcore_write_s390_high_gprs (bfd *abfd,
   12133 			      char *buf,
   12134 			      int *bufsiz,
   12135 			      const void *s390_high_gprs,
   12136 			      int size)
   12137 {
   12138   char *note_name = "LINUX";
   12139   return elfcore_write_note (abfd, buf, bufsiz,
   12140 			     note_name, NT_S390_HIGH_GPRS,
   12141 			     s390_high_gprs, size);
   12142 }
   12143 
   12144 char *
   12145 elfcore_write_s390_timer (bfd *abfd,
   12146 			  char *buf,
   12147 			  int *bufsiz,
   12148 			  const void *s390_timer,
   12149 			  int size)
   12150 {
   12151   char *note_name = "LINUX";
   12152   return elfcore_write_note (abfd, buf, bufsiz,
   12153 			     note_name, NT_S390_TIMER, s390_timer, size);
   12154 }
   12155 
   12156 char *
   12157 elfcore_write_s390_todcmp (bfd *abfd,
   12158 			   char *buf,
   12159 			   int *bufsiz,
   12160 			   const void *s390_todcmp,
   12161 			   int size)
   12162 {
   12163   char *note_name = "LINUX";
   12164   return elfcore_write_note (abfd, buf, bufsiz,
   12165 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   12166 }
   12167 
   12168 char *
   12169 elfcore_write_s390_todpreg (bfd *abfd,
   12170 			    char *buf,
   12171 			    int *bufsiz,
   12172 			    const void *s390_todpreg,
   12173 			    int size)
   12174 {
   12175   char *note_name = "LINUX";
   12176   return elfcore_write_note (abfd, buf, bufsiz,
   12177 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   12178 }
   12179 
   12180 char *
   12181 elfcore_write_s390_ctrs (bfd *abfd,
   12182 			 char *buf,
   12183 			 int *bufsiz,
   12184 			 const void *s390_ctrs,
   12185 			 int size)
   12186 {
   12187   char *note_name = "LINUX";
   12188   return elfcore_write_note (abfd, buf, bufsiz,
   12189 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   12190 }
   12191 
   12192 char *
   12193 elfcore_write_s390_prefix (bfd *abfd,
   12194 			   char *buf,
   12195 			   int *bufsiz,
   12196 			   const void *s390_prefix,
   12197 			   int size)
   12198 {
   12199   char *note_name = "LINUX";
   12200   return elfcore_write_note (abfd, buf, bufsiz,
   12201 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   12202 }
   12203 
   12204 char *
   12205 elfcore_write_s390_last_break (bfd *abfd,
   12206 			       char *buf,
   12207 			       int *bufsiz,
   12208 			       const void *s390_last_break,
   12209 			       int size)
   12210 {
   12211   char *note_name = "LINUX";
   12212   return elfcore_write_note (abfd, buf, bufsiz,
   12213 			     note_name, NT_S390_LAST_BREAK,
   12214 			     s390_last_break, size);
   12215 }
   12216 
   12217 char *
   12218 elfcore_write_s390_system_call (bfd *abfd,
   12219 				char *buf,
   12220 				int *bufsiz,
   12221 				const void *s390_system_call,
   12222 				int size)
   12223 {
   12224   char *note_name = "LINUX";
   12225   return elfcore_write_note (abfd, buf, bufsiz,
   12226 			     note_name, NT_S390_SYSTEM_CALL,
   12227 			     s390_system_call, size);
   12228 }
   12229 
   12230 char *
   12231 elfcore_write_s390_tdb (bfd *abfd,
   12232 			char *buf,
   12233 			int *bufsiz,
   12234 			const void *s390_tdb,
   12235 			int size)
   12236 {
   12237   char *note_name = "LINUX";
   12238   return elfcore_write_note (abfd, buf, bufsiz,
   12239 			     note_name, NT_S390_TDB, s390_tdb, size);
   12240 }
   12241 
   12242 char *
   12243 elfcore_write_s390_vxrs_low (bfd *abfd,
   12244 			     char *buf,
   12245 			     int *bufsiz,
   12246 			     const void *s390_vxrs_low,
   12247 			     int size)
   12248 {
   12249   char *note_name = "LINUX";
   12250   return elfcore_write_note (abfd, buf, bufsiz,
   12251 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   12252 }
   12253 
   12254 char *
   12255 elfcore_write_s390_vxrs_high (bfd *abfd,
   12256 			     char *buf,
   12257 			     int *bufsiz,
   12258 			     const void *s390_vxrs_high,
   12259 			     int size)
   12260 {
   12261   char *note_name = "LINUX";
   12262   return elfcore_write_note (abfd, buf, bufsiz,
   12263 			     note_name, NT_S390_VXRS_HIGH,
   12264 			     s390_vxrs_high, size);
   12265 }
   12266 
   12267 char *
   12268 elfcore_write_s390_gs_cb (bfd *abfd,
   12269 			  char *buf,
   12270 			  int *bufsiz,
   12271 			  const void *s390_gs_cb,
   12272 			  int size)
   12273 {
   12274   char *note_name = "LINUX";
   12275   return elfcore_write_note (abfd, buf, bufsiz,
   12276 			     note_name, NT_S390_GS_CB,
   12277 			     s390_gs_cb, size);
   12278 }
   12279 
   12280 char *
   12281 elfcore_write_s390_gs_bc (bfd *abfd,
   12282 			  char *buf,
   12283 			  int *bufsiz,
   12284 			  const void *s390_gs_bc,
   12285 			  int size)
   12286 {
   12287   char *note_name = "LINUX";
   12288   return elfcore_write_note (abfd, buf, bufsiz,
   12289 			     note_name, NT_S390_GS_BC,
   12290 			     s390_gs_bc, size);
   12291 }
   12292 
   12293 char *
   12294 elfcore_write_arm_vfp (bfd *abfd,
   12295 		       char *buf,
   12296 		       int *bufsiz,
   12297 		       const void *arm_vfp,
   12298 		       int size)
   12299 {
   12300   char *note_name = "LINUX";
   12301   return elfcore_write_note (abfd, buf, bufsiz,
   12302 			     note_name, NT_ARM_VFP, arm_vfp, size);
   12303 }
   12304 
   12305 char *
   12306 elfcore_write_aarch_tls (bfd *abfd,
   12307 		       char *buf,
   12308 		       int *bufsiz,
   12309 		       const void *aarch_tls,
   12310 		       int size)
   12311 {
   12312   char *note_name = "LINUX";
   12313   return elfcore_write_note (abfd, buf, bufsiz,
   12314 			     note_name, NT_ARM_TLS, aarch_tls, size);
   12315 }
   12316 
   12317 char *
   12318 elfcore_write_aarch_hw_break (bfd *abfd,
   12319 			    char *buf,
   12320 			    int *bufsiz,
   12321 			    const void *aarch_hw_break,
   12322 			    int size)
   12323 {
   12324   char *note_name = "LINUX";
   12325   return elfcore_write_note (abfd, buf, bufsiz,
   12326 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   12327 }
   12328 
   12329 char *
   12330 elfcore_write_aarch_hw_watch (bfd *abfd,
   12331 			    char *buf,
   12332 			    int *bufsiz,
   12333 			    const void *aarch_hw_watch,
   12334 			    int size)
   12335 {
   12336   char *note_name = "LINUX";
   12337   return elfcore_write_note (abfd, buf, bufsiz,
   12338 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   12339 }
   12340 
   12341 char *
   12342 elfcore_write_aarch_sve (bfd *abfd,
   12343 			 char *buf,
   12344 			 int *bufsiz,
   12345 			 const void *aarch_sve,
   12346 			 int size)
   12347 {
   12348   char *note_name = "LINUX";
   12349   return elfcore_write_note (abfd, buf, bufsiz,
   12350 			     note_name, NT_ARM_SVE, aarch_sve, size);
   12351 }
   12352 
   12353 char *
   12354 elfcore_write_aarch_pauth (bfd *abfd,
   12355 			   char *buf,
   12356 			   int *bufsiz,
   12357 			   const void *aarch_pauth,
   12358 			   int size)
   12359 {
   12360   char *note_name = "LINUX";
   12361   return elfcore_write_note (abfd, buf, bufsiz,
   12362 			     note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
   12363 }
   12364 
   12365 char *
   12366 elfcore_write_aarch_mte (bfd *abfd,
   12367 				      char *buf,
   12368 				      int *bufsiz,
   12369 				      const void *aarch_mte,
   12370 				      int size)
   12371 {
   12372   char *note_name = "LINUX";
   12373   return elfcore_write_note (abfd, buf, bufsiz,
   12374 			     note_name, NT_ARM_TAGGED_ADDR_CTRL,
   12375 			     aarch_mte,
   12376 			     size);
   12377 }
   12378 
   12379 char *
   12380 elfcore_write_arc_v2 (bfd *abfd,
   12381 		      char *buf,
   12382 		      int *bufsiz,
   12383 		      const void *arc_v2,
   12384 		      int size)
   12385 {
   12386   char *note_name = "LINUX";
   12387   return elfcore_write_note (abfd, buf, bufsiz,
   12388 			     note_name, NT_ARC_V2, arc_v2, size);
   12389 }
   12390 
   12391 char *
   12392 elfcore_write_loongarch_cpucfg (bfd *abfd,
   12393 				char *buf,
   12394 				int *bufsiz,
   12395 				const void *loongarch_cpucfg,
   12396 				int size)
   12397 {
   12398   char *note_name = "LINUX";
   12399   return elfcore_write_note (abfd, buf, bufsiz,
   12400 			     note_name, NT_LARCH_CPUCFG,
   12401 			     loongarch_cpucfg, size);
   12402 }
   12403 
   12404 char *
   12405 elfcore_write_loongarch_lbt (bfd *abfd,
   12406 			     char *buf,
   12407 			     int *bufsiz,
   12408 			     const void *loongarch_lbt,
   12409 			     int size)
   12410 {
   12411   char *note_name = "LINUX";
   12412   return elfcore_write_note (abfd, buf, bufsiz,
   12413 			     note_name, NT_LARCH_LBT, loongarch_lbt, size);
   12414 }
   12415 
   12416 char *
   12417 elfcore_write_loongarch_lsx (bfd *abfd,
   12418 			     char *buf,
   12419 			     int *bufsiz,
   12420 			     const void *loongarch_lsx,
   12421 			     int size)
   12422 {
   12423   char *note_name = "LINUX";
   12424   return elfcore_write_note (abfd, buf, bufsiz,
   12425 			     note_name, NT_LARCH_LSX, loongarch_lsx, size);
   12426 }
   12427 
   12428 char *
   12429 elfcore_write_loongarch_lasx (bfd *abfd,
   12430 			      char *buf,
   12431 			      int *bufsiz,
   12432 			      const void *loongarch_lasx,
   12433 			      int size)
   12434 {
   12435   char *note_name = "LINUX";
   12436   return elfcore_write_note (abfd, buf, bufsiz,
   12437 			     note_name, NT_LARCH_LASX, loongarch_lasx, size);
   12438 }
   12439 
   12440 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   12441    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12442    written into.  Return a pointer to the new start of the note buffer, to
   12443    replace BUF which may no longer be valid.  */
   12444 
   12445 char *
   12446 elfcore_write_riscv_csr (bfd *abfd,
   12447                          char *buf,
   12448                          int *bufsiz,
   12449                          const void *csrs,
   12450                          int size)
   12451 {
   12452   const char *note_name = "GDB";
   12453   return elfcore_write_note (abfd, buf, bufsiz,
   12454 			     note_name, NT_RISCV_CSR, csrs, size);
   12455 }
   12456 
   12457 /* Write the target description (a string) pointed to by TDESC, length
   12458    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   12459    note is being written into.  Return a pointer to the new start of the
   12460    note buffer, to replace BUF which may no longer be valid.  */
   12461 
   12462 char *
   12463 elfcore_write_gdb_tdesc (bfd *abfd,
   12464 			 char *buf,
   12465 			 int *bufsiz,
   12466 			 const void *tdesc,
   12467 			 int size)
   12468 {
   12469   const char *note_name = "GDB";
   12470   return elfcore_write_note (abfd, buf, bufsiz,
   12471                              note_name, NT_GDB_TDESC, tdesc, size);
   12472 }
   12473 
   12474 char *
   12475 elfcore_write_register_note (bfd *abfd,
   12476 			     char *buf,
   12477 			     int *bufsiz,
   12478 			     const char *section,
   12479 			     const void *data,
   12480 			     int size)
   12481 {
   12482   if (strcmp (section, ".reg2") == 0)
   12483     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   12484   if (strcmp (section, ".reg-xfp") == 0)
   12485     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   12486   if (strcmp (section, ".reg-xstate") == 0)
   12487     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   12488   if (strcmp (section, ".reg-x86-segbases") == 0)
   12489     return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
   12490   if (strcmp (section, ".reg-ppc-vmx") == 0)
   12491     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   12492   if (strcmp (section, ".reg-ppc-vsx") == 0)
   12493     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   12494   if (strcmp (section, ".reg-ppc-tar") == 0)
   12495     return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
   12496   if (strcmp (section, ".reg-ppc-ppr") == 0)
   12497     return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
   12498   if (strcmp (section, ".reg-ppc-dscr") == 0)
   12499     return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
   12500   if (strcmp (section, ".reg-ppc-ebb") == 0)
   12501     return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
   12502   if (strcmp (section, ".reg-ppc-pmu") == 0)
   12503     return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
   12504   if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
   12505     return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
   12506   if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
   12507     return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
   12508   if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
   12509     return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
   12510   if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
   12511     return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
   12512   if (strcmp (section, ".reg-ppc-tm-spr") == 0)
   12513     return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
   12514   if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
   12515     return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
   12516   if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
   12517     return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
   12518   if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
   12519     return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
   12520   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   12521     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   12522   if (strcmp (section, ".reg-s390-timer") == 0)
   12523     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   12524   if (strcmp (section, ".reg-s390-todcmp") == 0)
   12525     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   12526   if (strcmp (section, ".reg-s390-todpreg") == 0)
   12527     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   12528   if (strcmp (section, ".reg-s390-ctrs") == 0)
   12529     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   12530   if (strcmp (section, ".reg-s390-prefix") == 0)
   12531     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   12532   if (strcmp (section, ".reg-s390-last-break") == 0)
   12533     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   12534   if (strcmp (section, ".reg-s390-system-call") == 0)
   12535     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   12536   if (strcmp (section, ".reg-s390-tdb") == 0)
   12537     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   12538   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   12539     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   12540   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   12541     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   12542   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   12543     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   12544   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   12545     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   12546   if (strcmp (section, ".reg-arm-vfp") == 0)
   12547     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   12548   if (strcmp (section, ".reg-aarch-tls") == 0)
   12549     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   12550   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   12551     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   12552   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   12553     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   12554   if (strcmp (section, ".reg-aarch-sve") == 0)
   12555     return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
   12556   if (strcmp (section, ".reg-aarch-pauth") == 0)
   12557     return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
   12558   if (strcmp (section, ".reg-aarch-mte") == 0)
   12559     return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
   12560   if (strcmp (section, ".reg-arc-v2") == 0)
   12561     return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
   12562   if (strcmp (section, ".gdb-tdesc") == 0)
   12563     return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
   12564   if (strcmp (section, ".reg-riscv-csr") == 0)
   12565     return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
   12566   if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
   12567     return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
   12568   if (strcmp (section, ".reg-loongarch-lbt") == 0)
   12569     return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
   12570   if (strcmp (section, ".reg-loongarch-lsx") == 0)
   12571     return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
   12572   if (strcmp (section, ".reg-loongarch-lasx") == 0)
   12573     return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
   12574   return NULL;
   12575 }
   12576 
   12577 char *
   12578 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   12579 			 const void *buf, int bufsiz)
   12580 {
   12581   return elfcore_write_note (obfd, note_data, note_size,
   12582 			     "CORE", NT_FILE, buf, bufsiz);
   12583 }
   12584 
   12585 static bool
   12586 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   12587 		 size_t align)
   12588 {
   12589   char *p;
   12590 
   12591   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   12592      gABI specifies that PT_NOTE alignment should be aligned to 4
   12593      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   12594      align is less than 4, we use 4 byte alignment.   */
   12595   if (align < 4)
   12596     align = 4;
   12597   if (align != 4 && align != 8)
   12598     return false;
   12599 
   12600   p = buf;
   12601   while (p < buf + size)
   12602     {
   12603       Elf_External_Note *xnp = (Elf_External_Note *) p;
   12604       Elf_Internal_Note in;
   12605 
   12606       if (offsetof (Elf_External_Note, name) > buf - p + size)
   12607 	return false;
   12608 
   12609       in.type = H_GET_32 (abfd, xnp->type);
   12610 
   12611       in.namesz = H_GET_32 (abfd, xnp->namesz);
   12612       in.namedata = xnp->name;
   12613       if (in.namesz > buf - in.namedata + size)
   12614 	return false;
   12615 
   12616       in.descsz = H_GET_32 (abfd, xnp->descsz);
   12617       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   12618       in.descpos = offset + (in.descdata - buf);
   12619       if (in.descsz != 0
   12620 	  && (in.descdata >= buf + size
   12621 	      || in.descsz > buf - in.descdata + size))
   12622 	return false;
   12623 
   12624       switch (bfd_get_format (abfd))
   12625 	{
   12626 	default:
   12627 	  return true;
   12628 
   12629 	case bfd_core:
   12630 	  {
   12631 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   12632 	    struct
   12633 	    {
   12634 	      const char * string;
   12635 	      size_t len;
   12636 	      bool (*func) (bfd *, Elf_Internal_Note *);
   12637 	    }
   12638 	    grokers[] =
   12639 	    {
   12640 	      GROKER_ELEMENT ("", elfcore_grok_note),
   12641 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   12642 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   12643 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   12644 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   12645 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   12646 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   12647 	      GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
   12648 	    };
   12649 #undef GROKER_ELEMENT
   12650 	    int i;
   12651 
   12652 	    for (i = ARRAY_SIZE (grokers); i--;)
   12653 	      {
   12654 		if (in.namesz >= grokers[i].len
   12655 		    && strncmp (in.namedata, grokers[i].string,
   12656 				grokers[i].len) == 0)
   12657 		  {
   12658 		    if (! grokers[i].func (abfd, & in))
   12659 		      return false;
   12660 		    break;
   12661 		  }
   12662 	      }
   12663 	    break;
   12664 	  }
   12665 
   12666 	case bfd_object:
   12667 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   12668 	    {
   12669 	      if (! elfobj_grok_gnu_note (abfd, &in))
   12670 		return false;
   12671 	    }
   12672 	  else if (in.namesz == sizeof "stapsdt"
   12673 		   && strcmp (in.namedata, "stapsdt") == 0)
   12674 	    {
   12675 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   12676 		return false;
   12677 	    }
   12678 	  break;
   12679 	}
   12680 
   12681       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   12682     }
   12683 
   12684   return true;
   12685 }
   12686 
   12687 bool
   12688 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   12689 		size_t align)
   12690 {
   12691   char *buf;
   12692 
   12693   if (size == 0 || (size + 1) == 0)
   12694     return true;
   12695 
   12696   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   12697     return false;
   12698 
   12699   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   12700   if (buf == NULL)
   12701     return false;
   12702 
   12703   /* PR 17512: file: ec08f814
   12704      0-termintate the buffer so that string searches will not overflow.  */
   12705   buf[size] = 0;
   12706 
   12707   if (!elf_parse_notes (abfd, buf, size, offset, align))
   12708     {
   12709       free (buf);
   12710       return false;
   12711     }
   12712 
   12713   free (buf);
   12714   return true;
   12715 }
   12716 
   12717 /* Providing external access to the ELF program header table.  */
   12719 
   12720 /* Return an upper bound on the number of bytes required to store a
   12721    copy of ABFD's program header table entries.  Return -1 if an error
   12722    occurs; bfd_get_error will return an appropriate code.  */
   12723 
   12724 long
   12725 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   12726 {
   12727   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   12728     {
   12729       bfd_set_error (bfd_error_wrong_format);
   12730       return -1;
   12731     }
   12732 
   12733   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   12734 }
   12735 
   12736 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   12737    will be stored as an array of Elf_Internal_Phdr structures, as
   12738    defined in include/elf/internal.h.  To find out how large the
   12739    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   12740 
   12741    Return the number of program header table entries read, or -1 if an
   12742    error occurs; bfd_get_error will return an appropriate code.  */
   12743 
   12744 int
   12745 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   12746 {
   12747   int num_phdrs;
   12748 
   12749   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   12750     {
   12751       bfd_set_error (bfd_error_wrong_format);
   12752       return -1;
   12753     }
   12754 
   12755   num_phdrs = elf_elfheader (abfd)->e_phnum;
   12756   if (num_phdrs != 0)
   12757     memcpy (phdrs, elf_tdata (abfd)->phdr,
   12758 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   12759 
   12760   return num_phdrs;
   12761 }
   12762 
   12763 enum elf_reloc_type_class
   12764 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12765 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   12766 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   12767 {
   12768   return reloc_class_normal;
   12769 }
   12770 
   12771 /* For RELA architectures, return the relocation value for a
   12772    relocation against a local symbol.  */
   12773 
   12774 bfd_vma
   12775 _bfd_elf_rela_local_sym (bfd *abfd,
   12776 			 Elf_Internal_Sym *sym,
   12777 			 asection **psec,
   12778 			 Elf_Internal_Rela *rel)
   12779 {
   12780   asection *sec = *psec;
   12781   bfd_vma relocation;
   12782 
   12783   relocation = (sec->output_section->vma
   12784 		+ sec->output_offset
   12785 		+ sym->st_value);
   12786   if ((sec->flags & SEC_MERGE)
   12787       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   12788       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   12789     {
   12790       rel->r_addend =
   12791 	_bfd_merged_section_offset (abfd, psec,
   12792 				    elf_section_data (sec)->sec_info,
   12793 				    sym->st_value + rel->r_addend);
   12794       if (sec != *psec)
   12795 	{
   12796 	  /* If we have changed the section, and our original section is
   12797 	     marked with SEC_EXCLUDE, it means that the original
   12798 	     SEC_MERGE section has been completely subsumed in some
   12799 	     other SEC_MERGE section.  In this case, we need to leave
   12800 	     some info around for --emit-relocs.  */
   12801 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   12802 	    sec->kept_section = *psec;
   12803 	  sec = *psec;
   12804 	}
   12805       rel->r_addend -= relocation;
   12806       rel->r_addend += sec->output_section->vma + sec->output_offset;
   12807     }
   12808   return relocation;
   12809 }
   12810 
   12811 bfd_vma
   12812 _bfd_elf_rel_local_sym (bfd *abfd,
   12813 			Elf_Internal_Sym *sym,
   12814 			asection **psec,
   12815 			bfd_vma addend)
   12816 {
   12817   asection *sec = *psec;
   12818 
   12819   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   12820     return sym->st_value + addend;
   12821 
   12822   return _bfd_merged_section_offset (abfd, psec,
   12823 				     elf_section_data (sec)->sec_info,
   12824 				     sym->st_value + addend);
   12825 }
   12826 
   12827 /* Adjust an address within a section.  Given OFFSET within SEC, return
   12828    the new offset within the section, based upon changes made to the
   12829    section.  Returns -1 if the offset is now invalid.
   12830    The offset (in abnd out) is in target sized bytes, however big a
   12831    byte may be.  */
   12832 
   12833 bfd_vma
   12834 _bfd_elf_section_offset (bfd *abfd,
   12835 			 struct bfd_link_info *info,
   12836 			 asection *sec,
   12837 			 bfd_vma offset)
   12838 {
   12839   switch (sec->sec_info_type)
   12840     {
   12841     case SEC_INFO_TYPE_STABS:
   12842       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   12843 				       offset);
   12844     case SEC_INFO_TYPE_EH_FRAME:
   12845       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   12846 
   12847     default:
   12848       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   12849 	{
   12850 	  /* Reverse the offset.  */
   12851 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12852 	  bfd_size_type address_size = bed->s->arch_size / 8;
   12853 
   12854 	  /* address_size and sec->size are in octets.  Convert
   12855 	     to bytes before subtracting the original offset.  */
   12856 	  offset = ((sec->size - address_size)
   12857 		    / bfd_octets_per_byte (abfd, sec) - offset);
   12858 	}
   12859       return offset;
   12860     }
   12861 }
   12862 
   12863 /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   12865    reconstruct an ELF file by reading the segments out of remote memory
   12866    based on the ELF file header at EHDR_VMA and the ELF program headers it
   12867    points to.  If not null, *LOADBASEP is filled in with the difference
   12868    between the VMAs from which the segments were read, and the VMAs the
   12869    file headers (and hence BFD's idea of each section's VMA) put them at.
   12870 
   12871    The function TARGET_READ_MEMORY is called to copy LEN bytes from the
   12872    remote memory at target address VMA into the local buffer at MYADDR; it
   12873    should return zero on success or an `errno' code on failure.  TEMPL must
   12874    be a BFD for an ELF target with the word size and byte order found in
   12875    the remote memory.  */
   12876 
   12877 bfd *
   12878 bfd_elf_bfd_from_remote_memory
   12879   (bfd *templ,
   12880    bfd_vma ehdr_vma,
   12881    bfd_size_type size,
   12882    bfd_vma *loadbasep,
   12883    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   12884 {
   12885   return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
   12886     (templ, ehdr_vma, size, loadbasep, target_read_memory);
   12887 }
   12888 
   12889 long
   12891 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   12892 			       long symcount ATTRIBUTE_UNUSED,
   12893 			       asymbol **syms ATTRIBUTE_UNUSED,
   12894 			       long dynsymcount,
   12895 			       asymbol **dynsyms,
   12896 			       asymbol **ret)
   12897 {
   12898   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12899   asection *relplt;
   12900   asymbol *s;
   12901   const char *relplt_name;
   12902   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   12903   arelent *p;
   12904   long count, i, n;
   12905   size_t size;
   12906   Elf_Internal_Shdr *hdr;
   12907   char *names;
   12908   asection *plt;
   12909 
   12910   *ret = NULL;
   12911 
   12912   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   12913     return 0;
   12914 
   12915   if (dynsymcount <= 0)
   12916     return 0;
   12917 
   12918   if (!bed->plt_sym_val)
   12919     return 0;
   12920 
   12921   relplt_name = bed->relplt_name;
   12922   if (relplt_name == NULL)
   12923     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   12924   relplt = bfd_get_section_by_name (abfd, relplt_name);
   12925   if (relplt == NULL)
   12926     return 0;
   12927 
   12928   hdr = &elf_section_data (relplt)->this_hdr;
   12929   if (hdr->sh_link != elf_dynsymtab (abfd)
   12930       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   12931     return 0;
   12932 
   12933   plt = bfd_get_section_by_name (abfd, ".plt");
   12934   if (plt == NULL)
   12935     return 0;
   12936 
   12937   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   12938   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   12939     return -1;
   12940 
   12941   count = relplt->size / hdr->sh_entsize;
   12942   size = count * sizeof (asymbol);
   12943   p = relplt->relocation;
   12944   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12945     {
   12946       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   12947       if (p->addend != 0)
   12948 	{
   12949 #ifdef BFD64
   12950 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   12951 #else
   12952 	  size += sizeof ("+0x") - 1 + 8;
   12953 #endif
   12954 	}
   12955     }
   12956 
   12957   s = *ret = (asymbol *) bfd_malloc (size);
   12958   if (s == NULL)
   12959     return -1;
   12960 
   12961   names = (char *) (s + count);
   12962   p = relplt->relocation;
   12963   n = 0;
   12964   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12965     {
   12966       size_t len;
   12967       bfd_vma addr;
   12968 
   12969       addr = bed->plt_sym_val (i, plt, p);
   12970       if (addr == (bfd_vma) -1)
   12971 	continue;
   12972 
   12973       *s = **p->sym_ptr_ptr;
   12974       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   12975 	 we are defining a symbol, ensure one of them is set.  */
   12976       if ((s->flags & BSF_LOCAL) == 0)
   12977 	s->flags |= BSF_GLOBAL;
   12978       s->flags |= BSF_SYNTHETIC;
   12979       s->section = plt;
   12980       s->value = addr - plt->vma;
   12981       s->name = names;
   12982       s->udata.p = NULL;
   12983       len = strlen ((*p->sym_ptr_ptr)->name);
   12984       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   12985       names += len;
   12986       if (p->addend != 0)
   12987 	{
   12988 	  char buf[30], *a;
   12989 
   12990 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   12991 	  names += sizeof ("+0x") - 1;
   12992 	  bfd_sprintf_vma (abfd, buf, p->addend);
   12993 	  for (a = buf; *a == '0'; ++a)
   12994 	    ;
   12995 	  len = strlen (a);
   12996 	  memcpy (names, a, len);
   12997 	  names += len;
   12998 	}
   12999       memcpy (names, "@plt", sizeof ("@plt"));
   13000       names += sizeof ("@plt");
   13001       ++s, ++n;
   13002     }
   13003 
   13004   return n;
   13005 }
   13006 
   13007 /* It is only used by x86-64 so far.
   13008    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   13009    but current usage would allow all of _bfd_std_section to be zero.  */
   13010 static const asymbol lcomm_sym
   13011   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   13012 asection _bfd_elf_large_com_section
   13013   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   13014 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   13015 
   13016 bool
   13017 _bfd_elf_final_write_processing (bfd *abfd)
   13018 {
   13019   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   13020 
   13021   i_ehdrp = elf_elfheader (abfd);
   13022 
   13023   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13024     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   13025 
   13026   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13027      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13028      or STB_GNU_UNIQUE binding.  */
   13029   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13030     {
   13031       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13032 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13033       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13034 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13035 	{
   13036 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13037 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13038 				  "and FreeBSD targets"));
   13039 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13040 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13041 				  "only by GNU and FreeBSD targets"));
   13042 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13043 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13044 				  "only by GNU and FreeBSD targets"));
   13045 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13046 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13047 				  "only by GNU and FreeBSD targets"));
   13048 	  bfd_set_error (bfd_error_sorry);
   13049 	  return false;
   13050 	}
   13051     }
   13052   return true;
   13053 }
   13054 
   13055 
   13056 /* Return TRUE for ELF symbol types that represent functions.
   13057    This is the default version of this function, which is sufficient for
   13058    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13059 
   13060 bool
   13061 _bfd_elf_is_function_type (unsigned int type)
   13062 {
   13063   return (type == STT_FUNC
   13064 	  || type == STT_GNU_IFUNC);
   13065 }
   13066 
   13067 /* If the ELF symbol SYM might be a function in SEC, return the
   13068    function size and set *CODE_OFF to the function's entry point,
   13069    otherwise return zero.  */
   13070 
   13071 bfd_size_type
   13072 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13073 			     bfd_vma *code_off)
   13074 {
   13075   bfd_size_type size;
   13076   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13077 
   13078   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13079 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13080       || sym->section != sec)
   13081     return 0;
   13082 
   13083   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13084 
   13085   /* In theory we should check that the symbol's type satisfies
   13086      _bfd_elf_is_function_type(), but there are some function-like
   13087      symbols which would fail this test.  (eg _start).  Instead
   13088      we check for hidden, local, notype symbols with zero size.
   13089      This type of symbol is generated by the annobin plugin for gcc
   13090      and clang, and should not be considered to be a function symbol.  */
   13091   if (size == 0
   13092       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13093       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13094       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13095     return 0;
   13096 
   13097   *code_off = sym->value;
   13098   /* Do not return 0 for the function's size.  */
   13099   return size ? size : 1;
   13100 }
   13101 
   13102 /* Set to non-zero to enable some debug messages.  */
   13103 #define DEBUG_SECONDARY_RELOCS	 0
   13104 
   13105 /* An internal-to-the-bfd-library only section type
   13106    used to indicate a cached secondary reloc section.  */
   13107 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13108 
   13109 /* Create a BFD section to hold a secondary reloc section.  */
   13110 
   13111 bool
   13112 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13113 				       Elf_Internal_Shdr *hdr,
   13114 				       const char * name,
   13115 				       unsigned int shindex)
   13116 {
   13117   /* We only support RELA secondary relocs.  */
   13118   if (hdr->sh_type != SHT_RELA)
   13119     return false;
   13120 
   13121 #if DEBUG_SECONDARY_RELOCS
   13122   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13123 #endif
   13124   hdr->sh_type = SHT_SECONDARY_RELOC;
   13125   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13126 }
   13127 
   13128 /* Read in any secondary relocs associated with SEC.  */
   13129 
   13130 bool
   13131 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13132 					asection *  sec,
   13133 					asymbol **  symbols,
   13134 					bool dynamic)
   13135 {
   13136   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13137   asection * relsec;
   13138   bool result = true;
   13139   bfd_vma (*r_sym) (bfd_vma);
   13140 
   13141 #if BFD_DEFAULT_TARGET_SIZE > 32
   13142   if (bfd_arch_bits_per_address (abfd) != 32)
   13143     r_sym = elf64_r_sym;
   13144   else
   13145 #endif
   13146     r_sym = elf32_r_sym;
   13147 
   13148   if (!elf_section_data (sec)->has_secondary_relocs)
   13149     return true;
   13150 
   13151   /* Discover if there are any secondary reloc sections
   13152      associated with SEC.  */
   13153   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13154     {
   13155       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13156 
   13157       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13158 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13159 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13160 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13161 	{
   13162 	  bfd_byte * native_relocs;
   13163 	  bfd_byte * native_reloc;
   13164 	  arelent * internal_relocs;
   13165 	  arelent * internal_reloc;
   13166 	  unsigned int i;
   13167 	  unsigned int entsize;
   13168 	  unsigned int symcount;
   13169 	  unsigned int reloc_count;
   13170 	  size_t amt;
   13171 
   13172 	  if (ebd->elf_info_to_howto == NULL)
   13173 	    return false;
   13174 
   13175 #if DEBUG_SECONDARY_RELOCS
   13176 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13177 		   sec->name, relsec->name);
   13178 #endif
   13179 	  entsize = hdr->sh_entsize;
   13180 
   13181 	  native_relocs = bfd_malloc (hdr->sh_size);
   13182 	  if (native_relocs == NULL)
   13183 	    {
   13184 	      result = false;
   13185 	      continue;
   13186 	    }
   13187 
   13188 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13189 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13190 	    {
   13191 	      free (native_relocs);
   13192 	      bfd_set_error (bfd_error_file_too_big);
   13193 	      result = false;
   13194 	      continue;
   13195 	    }
   13196 
   13197 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   13198 	  if (internal_relocs == NULL)
   13199 	    {
   13200 	      free (native_relocs);
   13201 	      result = false;
   13202 	      continue;
   13203 	    }
   13204 
   13205 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   13206 	      || (bfd_bread (native_relocs, hdr->sh_size, abfd)
   13207 		  != hdr->sh_size))
   13208 	    {
   13209 	      free (native_relocs);
   13210 	      /* The internal_relocs will be freed when
   13211 		 the memory for the bfd is released.  */
   13212 	      result = false;
   13213 	      continue;
   13214 	    }
   13215 
   13216 	  if (dynamic)
   13217 	    symcount = bfd_get_dynamic_symcount (abfd);
   13218 	  else
   13219 	    symcount = bfd_get_symcount (abfd);
   13220 
   13221 	  for (i = 0, internal_reloc = internal_relocs,
   13222 		 native_reloc = native_relocs;
   13223 	       i < reloc_count;
   13224 	       i++, internal_reloc++, native_reloc += entsize)
   13225 	    {
   13226 	      bool res;
   13227 	      Elf_Internal_Rela rela;
   13228 
   13229 	      if (entsize == ebd->s->sizeof_rel)
   13230 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   13231 	      else /* entsize == ebd->s->sizeof_rela */
   13232 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   13233 
   13234 	      /* The address of an ELF reloc is section relative for an object
   13235 		 file, and absolute for an executable file or shared library.
   13236 		 The address of a normal BFD reloc is always section relative,
   13237 		 and the address of a dynamic reloc is absolute..  */
   13238 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   13239 		internal_reloc->address = rela.r_offset;
   13240 	      else
   13241 		internal_reloc->address = rela.r_offset - sec->vma;
   13242 
   13243 	      if (r_sym (rela.r_info) == STN_UNDEF)
   13244 		{
   13245 		  /* FIXME: This and the error case below mean that we
   13246 		     have a symbol on relocs that is not elf_symbol_type.  */
   13247 		  internal_reloc->sym_ptr_ptr =
   13248 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   13249 		}
   13250 	      else if (r_sym (rela.r_info) > symcount)
   13251 		{
   13252 		  _bfd_error_handler
   13253 		    /* xgettext:c-format */
   13254 		    (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
   13255 		     abfd, sec, i, (long) r_sym (rela.r_info));
   13256 		  bfd_set_error (bfd_error_bad_value);
   13257 		  internal_reloc->sym_ptr_ptr =
   13258 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   13259 		  result = false;
   13260 		}
   13261 	      else
   13262 		{
   13263 		  asymbol **ps;
   13264 
   13265 		  ps = symbols + r_sym (rela.r_info) - 1;
   13266 		  internal_reloc->sym_ptr_ptr = ps;
   13267 		  /* Make sure that this symbol is not removed by strip.  */
   13268 		  (*ps)->flags |= BSF_KEEP;
   13269 		}
   13270 
   13271 	      internal_reloc->addend = rela.r_addend;
   13272 
   13273 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   13274 	      if (! res || internal_reloc->howto == NULL)
   13275 		{
   13276 #if DEBUG_SECONDARY_RELOCS
   13277 		  fprintf (stderr, "there is no howto associated with reloc %lx\n",
   13278 			   rela.r_info);
   13279 #endif
   13280 		  result = false;
   13281 		}
   13282 	    }
   13283 
   13284 	  free (native_relocs);
   13285 	  /* Store the internal relocs.  */
   13286 	  elf_section_data (relsec)->sec_info = internal_relocs;
   13287 	}
   13288     }
   13289 
   13290   return result;
   13291 }
   13292 
   13293 /* Set the ELF section header fields of an output secondary reloc section.  */
   13294 
   13295 bool
   13296 _bfd_elf_copy_special_section_fields (const bfd *   ibfd ATTRIBUTE_UNUSED,
   13297 				      bfd *         obfd ATTRIBUTE_UNUSED,
   13298 				      const Elf_Internal_Shdr * isection,
   13299 				      Elf_Internal_Shdr *       osection)
   13300 {
   13301   asection * isec;
   13302   asection * osec;
   13303   struct bfd_elf_section_data * esd;
   13304 
   13305   if (isection == NULL)
   13306     return false;
   13307 
   13308   if (isection->sh_type != SHT_SECONDARY_RELOC)
   13309     return true;
   13310 
   13311   isec = isection->bfd_section;
   13312   if (isec == NULL)
   13313     return false;
   13314 
   13315   osec = osection->bfd_section;
   13316   if (osec == NULL)
   13317     return false;
   13318 
   13319   esd = elf_section_data (osec);
   13320   BFD_ASSERT (esd->sec_info == NULL);
   13321   esd->sec_info = elf_section_data (isec)->sec_info;
   13322   osection->sh_type = SHT_RELA;
   13323   osection->sh_link = elf_onesymtab (obfd);
   13324   if (osection->sh_link == 0)
   13325     {
   13326       /* There is no symbol table - we are hosed...  */
   13327       _bfd_error_handler
   13328 	/* xgettext:c-format */
   13329 	(_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
   13330 	obfd, osec);
   13331       bfd_set_error (bfd_error_bad_value);
   13332       return false;
   13333     }
   13334 
   13335   /* Find the output section that corresponds to the isection's sh_info link.  */
   13336   if (isection->sh_info == 0
   13337       || isection->sh_info >= elf_numsections (ibfd))
   13338     {
   13339       _bfd_error_handler
   13340 	/* xgettext:c-format */
   13341 	(_("%pB(%pA): info section index is invalid"),
   13342 	obfd, osec);
   13343       bfd_set_error (bfd_error_bad_value);
   13344       return false;
   13345     }
   13346 
   13347   isection = elf_elfsections (ibfd)[isection->sh_info];
   13348 
   13349   if (isection == NULL
   13350       || isection->bfd_section == NULL
   13351       || isection->bfd_section->output_section == NULL)
   13352     {
   13353       _bfd_error_handler
   13354 	/* xgettext:c-format */
   13355 	(_("%pB(%pA): info section index cannot be set because the section is not in the output"),
   13356 	obfd, osec);
   13357       bfd_set_error (bfd_error_bad_value);
   13358       return false;
   13359     }
   13360 
   13361   esd = elf_section_data (isection->bfd_section->output_section);
   13362   BFD_ASSERT (esd != NULL);
   13363   osection->sh_info = esd->this_idx;
   13364   esd->has_secondary_relocs = true;
   13365 #if DEBUG_SECONDARY_RELOCS
   13366   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   13367 	   osec->name, osection->sh_link, osection->sh_info);
   13368   fprintf (stderr, "mark section %s as having secondary relocs\n",
   13369 	   bfd_section_name (isection->bfd_section->output_section));
   13370 #endif
   13371 
   13372   return true;
   13373 }
   13374 
   13375 /* Write out a secondary reloc section.
   13376 
   13377    FIXME: Currently this function can result in a serious performance penalty
   13378    for files with secondary relocs and lots of sections.  The proper way to
   13379    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   13380    relocs together and then to have this function just walk that chain.  */
   13381 
   13382 bool
   13383 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   13384 {
   13385   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13386   bfd_vma addr_offset;
   13387   asection * relsec;
   13388   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   13389   bool result = true;
   13390 
   13391   if (sec == NULL)
   13392     return false;
   13393 
   13394 #if BFD_DEFAULT_TARGET_SIZE > 32
   13395   if (bfd_arch_bits_per_address (abfd) != 32)
   13396     r_info = elf64_r_info;
   13397   else
   13398 #endif
   13399     r_info = elf32_r_info;
   13400 
   13401   /* The address of an ELF reloc is section relative for an object
   13402      file, and absolute for an executable file or shared library.
   13403      The address of a BFD reloc is always section relative.  */
   13404   addr_offset = 0;
   13405   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   13406     addr_offset = sec->vma;
   13407 
   13408   /* Discover if there are any secondary reloc sections
   13409      associated with SEC.  */
   13410   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13411     {
   13412       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   13413       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   13414 
   13415       if (hdr->sh_type == SHT_RELA
   13416 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   13417 	{
   13418 	  asymbol *    last_sym;
   13419 	  int          last_sym_idx;
   13420 	  unsigned int reloc_count;
   13421 	  unsigned int idx;
   13422 	  unsigned int entsize;
   13423 	  arelent *    src_irel;
   13424 	  bfd_byte *   dst_rela;
   13425 
   13426 	  if (hdr->contents != NULL)
   13427 	    {
   13428 	      _bfd_error_handler
   13429 		/* xgettext:c-format */
   13430 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   13431 		 abfd, relsec);
   13432 	      bfd_set_error (bfd_error_bad_value);
   13433 	      result = false;
   13434 	      continue;
   13435 	    }
   13436 
   13437 	  entsize = hdr->sh_entsize;
   13438 	  if (entsize == 0)
   13439 	    {
   13440 	      _bfd_error_handler
   13441 		/* xgettext:c-format */
   13442 		(_("%pB(%pA): error: secondary reloc section has zero sized entries"),
   13443 		 abfd, relsec);
   13444 	      bfd_set_error (bfd_error_bad_value);
   13445 	      result = false;
   13446 	      continue;
   13447 	    }
   13448 	  else if (entsize != ebd->s->sizeof_rel
   13449 		   && entsize != ebd->s->sizeof_rela)
   13450 	    {
   13451 	      _bfd_error_handler
   13452 		/* xgettext:c-format */
   13453 		(_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
   13454 		 abfd, relsec);
   13455 	      bfd_set_error (bfd_error_bad_value);
   13456 	      result = false;
   13457 	      continue;
   13458 	    }
   13459 
   13460 	  reloc_count = hdr->sh_size / entsize;
   13461 	  if (reloc_count <= 0)
   13462 	    {
   13463 	      _bfd_error_handler
   13464 		/* xgettext:c-format */
   13465 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   13466 		 abfd, relsec);
   13467 	      bfd_set_error (bfd_error_bad_value);
   13468 	      result = false;
   13469 	      continue;
   13470 	    }
   13471 
   13472 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   13473 	  if (hdr->contents == NULL)
   13474 	    continue;
   13475 
   13476 #if DEBUG_SECONDARY_RELOCS
   13477 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   13478 		   reloc_count, sec->name, relsec->name);
   13479 #endif
   13480 	  last_sym = NULL;
   13481 	  last_sym_idx = 0;
   13482 	  dst_rela = hdr->contents;
   13483 	  src_irel = (arelent *) esd->sec_info;
   13484 	  if (src_irel == NULL)
   13485 	    {
   13486 	      _bfd_error_handler
   13487 		/* xgettext:c-format */
   13488 		(_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
   13489 		 abfd, relsec);
   13490 	      bfd_set_error (bfd_error_bad_value);
   13491 	      result = false;
   13492 	      continue;
   13493 	    }
   13494 
   13495 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   13496 	    {
   13497 	      Elf_Internal_Rela src_rela;
   13498 	      arelent *ptr;
   13499 	      asymbol *sym;
   13500 	      int n;
   13501 
   13502 	      ptr = src_irel + idx;
   13503 	      if (ptr == NULL)
   13504 		{
   13505 		  _bfd_error_handler
   13506 		    /* xgettext:c-format */
   13507 		    (_("%pB(%pA): error: reloc table entry %u is empty"),
   13508 		     abfd, relsec, idx);
   13509 		  bfd_set_error (bfd_error_bad_value);
   13510 		  result = false;
   13511 		  break;
   13512 		}
   13513 
   13514 	      if (ptr->sym_ptr_ptr == NULL)
   13515 		{
   13516 		  /* FIXME: Is this an error ? */
   13517 		  n = 0;
   13518 		}
   13519 	      else
   13520 		{
   13521 		  sym = *ptr->sym_ptr_ptr;
   13522 
   13523 		  if (sym == last_sym)
   13524 		    n = last_sym_idx;
   13525 		  else
   13526 		    {
   13527 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   13528 		      if (n < 0)
   13529 			{
   13530 			  _bfd_error_handler
   13531 			    /* xgettext:c-format */
   13532 			    (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
   13533 			     abfd, relsec, idx);
   13534 			  bfd_set_error (bfd_error_bad_value);
   13535 			  result = false;
   13536 			  n = 0;
   13537 			}
   13538 
   13539 		      last_sym = sym;
   13540 		      last_sym_idx = n;
   13541 		    }
   13542 
   13543 		  if (sym->the_bfd != NULL
   13544 		      && sym->the_bfd->xvec != abfd->xvec
   13545 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   13546 		    {
   13547 		      _bfd_error_handler
   13548 			/* xgettext:c-format */
   13549 			(_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
   13550 			 abfd, relsec, idx);
   13551 		      bfd_set_error (bfd_error_bad_value);
   13552 		      result = false;
   13553 		      n = 0;
   13554 		    }
   13555 		}
   13556 
   13557 	      src_rela.r_offset = ptr->address + addr_offset;
   13558 	      if (ptr->howto == NULL)
   13559 		{
   13560 		  _bfd_error_handler
   13561 		    /* xgettext:c-format */
   13562 		    (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
   13563 		     abfd, relsec, idx);
   13564 		  bfd_set_error (bfd_error_bad_value);
   13565 		  result = false;
   13566 		  src_rela.r_info = r_info (0, 0);
   13567 		}
   13568 	      else
   13569 		src_rela.r_info = r_info (n, ptr->howto->type);
   13570 	      src_rela.r_addend = ptr->addend;
   13571 
   13572 	      if (entsize == ebd->s->sizeof_rel)
   13573 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   13574 	      else /* entsize == ebd->s->sizeof_rela */
   13575 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   13576 	    }
   13577 	}
   13578     }
   13579 
   13580   return result;
   13581 }
   13582