Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.17
      1 /* ELF executable support for BFD.
      2 
      3    Copyright (C) 1993-2022 Free Software Foundation, Inc.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 
     23 /*
     24 SECTION
     25 	ELF backends
     26 
     27 	BFD support for ELF formats is being worked on.
     28 	Currently, the best supported back ends are for sparc and i386
     29 	(running svr4 or Solaris 2).
     30 
     31 	Documentation of the internals of the support code still needs
     32 	to be written.  The code is changing quickly enough that we
     33 	haven't bothered yet.  */
     34 
     35 /* For sparc64-cross-sparc32.  */
     36 #define _SYSCALL32
     37 #include "sysdep.h"
     38 #include <limits.h>
     39 #include "bfd.h"
     40 #include "bfdlink.h"
     41 #include "libbfd.h"
     42 #define ARCH_SIZE 0
     43 #include "elf-bfd.h"
     44 #include "libiberty.h"
     45 #include "safe-ctype.h"
     46 #include "elf-linux-core.h"
     47 
     48 #ifdef CORE_HEADER
     49 #include CORE_HEADER
     50 #endif
     51 
     52 static int elf_sort_sections (const void *, const void *);
     53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
     54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
     55 			   struct bfd_link_info *);
     56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
     57 			     file_ptr offset, size_t align);
     58 
     59 /* Swap version information in and out.  The version information is
     60    currently size independent.  If that ever changes, this code will
     61    need to move into elfcode.h.  */
     62 
     63 /* Swap in a Verdef structure.  */
     64 
     65 void
     66 _bfd_elf_swap_verdef_in (bfd *abfd,
     67 			 const Elf_External_Verdef *src,
     68 			 Elf_Internal_Verdef *dst)
     69 {
     70   dst->vd_version = H_GET_16 (abfd, src->vd_version);
     71   dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
     72   dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
     73   dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
     74   dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
     75   dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
     76   dst->vd_next    = H_GET_32 (abfd, src->vd_next);
     77 }
     78 
     79 /* Swap out a Verdef structure.  */
     80 
     81 void
     82 _bfd_elf_swap_verdef_out (bfd *abfd,
     83 			  const Elf_Internal_Verdef *src,
     84 			  Elf_External_Verdef *dst)
     85 {
     86   H_PUT_16 (abfd, src->vd_version, dst->vd_version);
     87   H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
     88   H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
     89   H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
     90   H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
     91   H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
     92   H_PUT_32 (abfd, src->vd_next, dst->vd_next);
     93 }
     94 
     95 /* Swap in a Verdaux structure.  */
     96 
     97 void
     98 _bfd_elf_swap_verdaux_in (bfd *abfd,
     99 			  const Elf_External_Verdaux *src,
    100 			  Elf_Internal_Verdaux *dst)
    101 {
    102   dst->vda_name = H_GET_32 (abfd, src->vda_name);
    103   dst->vda_next = H_GET_32 (abfd, src->vda_next);
    104 }
    105 
    106 /* Swap out a Verdaux structure.  */
    107 
    108 void
    109 _bfd_elf_swap_verdaux_out (bfd *abfd,
    110 			   const Elf_Internal_Verdaux *src,
    111 			   Elf_External_Verdaux *dst)
    112 {
    113   H_PUT_32 (abfd, src->vda_name, dst->vda_name);
    114   H_PUT_32 (abfd, src->vda_next, dst->vda_next);
    115 }
    116 
    117 /* Swap in a Verneed structure.  */
    118 
    119 void
    120 _bfd_elf_swap_verneed_in (bfd *abfd,
    121 			  const Elf_External_Verneed *src,
    122 			  Elf_Internal_Verneed *dst)
    123 {
    124   dst->vn_version = H_GET_16 (abfd, src->vn_version);
    125   dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
    126   dst->vn_file    = H_GET_32 (abfd, src->vn_file);
    127   dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
    128   dst->vn_next    = H_GET_32 (abfd, src->vn_next);
    129 }
    130 
    131 /* Swap out a Verneed structure.  */
    132 
    133 void
    134 _bfd_elf_swap_verneed_out (bfd *abfd,
    135 			   const Elf_Internal_Verneed *src,
    136 			   Elf_External_Verneed *dst)
    137 {
    138   H_PUT_16 (abfd, src->vn_version, dst->vn_version);
    139   H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
    140   H_PUT_32 (abfd, src->vn_file, dst->vn_file);
    141   H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
    142   H_PUT_32 (abfd, src->vn_next, dst->vn_next);
    143 }
    144 
    145 /* Swap in a Vernaux structure.  */
    146 
    147 void
    148 _bfd_elf_swap_vernaux_in (bfd *abfd,
    149 			  const Elf_External_Vernaux *src,
    150 			  Elf_Internal_Vernaux *dst)
    151 {
    152   dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
    153   dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
    154   dst->vna_other = H_GET_16 (abfd, src->vna_other);
    155   dst->vna_name  = H_GET_32 (abfd, src->vna_name);
    156   dst->vna_next  = H_GET_32 (abfd, src->vna_next);
    157 }
    158 
    159 /* Swap out a Vernaux structure.  */
    160 
    161 void
    162 _bfd_elf_swap_vernaux_out (bfd *abfd,
    163 			   const Elf_Internal_Vernaux *src,
    164 			   Elf_External_Vernaux *dst)
    165 {
    166   H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
    167   H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
    168   H_PUT_16 (abfd, src->vna_other, dst->vna_other);
    169   H_PUT_32 (abfd, src->vna_name, dst->vna_name);
    170   H_PUT_32 (abfd, src->vna_next, dst->vna_next);
    171 }
    172 
    173 /* Swap in a Versym structure.  */
    174 
    175 void
    176 _bfd_elf_swap_versym_in (bfd *abfd,
    177 			 const Elf_External_Versym *src,
    178 			 Elf_Internal_Versym *dst)
    179 {
    180   dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
    181 }
    182 
    183 /* Swap out a Versym structure.  */
    184 
    185 void
    186 _bfd_elf_swap_versym_out (bfd *abfd,
    187 			  const Elf_Internal_Versym *src,
    188 			  Elf_External_Versym *dst)
    189 {
    190   H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
    191 }
    192 
    193 /* Standard ELF hash function.  Do not change this function; you will
    194    cause invalid hash tables to be generated.  */
    195 
    196 unsigned long
    197 bfd_elf_hash (const char *namearg)
    198 {
    199   const unsigned char *name = (const unsigned char *) namearg;
    200   unsigned long h = 0;
    201   unsigned long g;
    202   int ch;
    203 
    204   while ((ch = *name++) != '\0')
    205     {
    206       h = (h << 4) + ch;
    207       if ((g = (h & 0xf0000000)) != 0)
    208 	{
    209 	  h ^= g >> 24;
    210 	  /* The ELF ABI says `h &= ~g', but this is equivalent in
    211 	     this case and on some machines one insn instead of two.  */
    212 	  h ^= g;
    213 	}
    214     }
    215   return h & 0xffffffff;
    216 }
    217 
    218 /* DT_GNU_HASH hash function.  Do not change this function; you will
    219    cause invalid hash tables to be generated.  */
    220 
    221 unsigned long
    222 bfd_elf_gnu_hash (const char *namearg)
    223 {
    224   const unsigned char *name = (const unsigned char *) namearg;
    225   unsigned long h = 5381;
    226   unsigned char ch;
    227 
    228   while ((ch = *name++) != '\0')
    229     h = (h << 5) + h + ch;
    230   return h & 0xffffffff;
    231 }
    232 
    233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    234    the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
    235 bool
    236 bfd_elf_allocate_object (bfd *abfd,
    237 			 size_t object_size,
    238 			 enum elf_target_id object_id)
    239 {
    240   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    241   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    242   if (abfd->tdata.any == NULL)
    243     return false;
    244 
    245   elf_object_id (abfd) = object_id;
    246   if (abfd->direction != read_direction)
    247     {
    248       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    249       if (o == NULL)
    250 	return false;
    251       elf_tdata (abfd)->o = o;
    252       elf_program_header_size (abfd) = (bfd_size_type) -1;
    253     }
    254   return true;
    255 }
    256 
    257 
    258 bool
    259 bfd_elf_make_object (bfd *abfd)
    260 {
    261   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    262   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
    263 				  bed->target_id);
    264 }
    265 
    266 bool
    267 bfd_elf_mkcorefile (bfd *abfd)
    268 {
    269   /* I think this can be done just like an object file.  */
    270   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    271     return false;
    272   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    273   return elf_tdata (abfd)->core != NULL;
    274 }
    275 
    276 char *
    277 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    278 {
    279   Elf_Internal_Shdr **i_shdrp;
    280   bfd_byte *shstrtab = NULL;
    281   file_ptr offset;
    282   bfd_size_type shstrtabsize;
    283 
    284   i_shdrp = elf_elfsections (abfd);
    285   if (i_shdrp == 0
    286       || shindex >= elf_numsections (abfd)
    287       || i_shdrp[shindex] == 0)
    288     return NULL;
    289 
    290   shstrtab = i_shdrp[shindex]->contents;
    291   if (shstrtab == NULL)
    292     {
    293       /* No cached one, attempt to read, and cache what we read.  */
    294       offset = i_shdrp[shindex]->sh_offset;
    295       shstrtabsize = i_shdrp[shindex]->sh_size;
    296 
    297       /* Allocate and clear an extra byte at the end, to prevent crashes
    298 	 in case the string table is not terminated.  */
    299       if (shstrtabsize + 1 <= 1
    300 	  || (bfd_get_file_size (abfd) > 0 /* not a character device */
    301 		&& shstrtabsize > bfd_get_file_size (abfd))
    302 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    303 	  || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
    304 					      shstrtabsize)) == NULL)
    305 	{
    306 	  /* Once we've failed to read it, make sure we don't keep
    307 	     trying.  Otherwise, we'll keep allocating space for
    308 	     the string table over and over.  */
    309 	  i_shdrp[shindex]->sh_size = 0;
    310 	}
    311       else
    312 	shstrtab[shstrtabsize] = '\0';
    313       i_shdrp[shindex]->contents = shstrtab;
    314     }
    315   return (char *) shstrtab;
    316 }
    317 
    318 char *
    319 bfd_elf_string_from_elf_section (bfd *abfd,
    320 				 unsigned int shindex,
    321 				 unsigned int strindex)
    322 {
    323   Elf_Internal_Shdr *hdr;
    324 
    325   if (strindex == 0)
    326     return "";
    327 
    328   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    329     return NULL;
    330 
    331   hdr = elf_elfsections (abfd)[shindex];
    332 
    333   if (hdr->contents == NULL)
    334     {
    335       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    336 	{
    337 	  /* PR 17512: file: f057ec89.  */
    338 	  /* xgettext:c-format */
    339 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    340 				" a non-string section (number %d)"),
    341 			      abfd, shindex);
    342 	  return NULL;
    343 	}
    344 
    345       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    346 	return NULL;
    347     }
    348   else
    349     {
    350       /* PR 24273: The string section's contents may have already
    351 	 been loaded elsewhere, eg because a corrupt file has the
    352 	 string section index in the ELF header pointing at a group
    353 	 section.  So be paranoid, and test that the last byte of
    354 	 the section is zero.  */
    355       if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
    356 	return NULL;
    357     }
    358 
    359   if (strindex >= hdr->sh_size)
    360     {
    361       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    362       _bfd_error_handler
    363 	/* xgettext:c-format */
    364 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    365 	 abfd, strindex, (uint64_t) hdr->sh_size,
    366 	 (shindex == shstrndx && strindex == hdr->sh_name
    367 	  ? ".shstrtab"
    368 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    369       return NULL;
    370     }
    371 
    372   return ((char *) hdr->contents) + strindex;
    373 }
    374 
    375 /* Read and convert symbols to internal format.
    376    SYMCOUNT specifies the number of symbols to read, starting from
    377    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    378    are non-NULL, they are used to store the internal symbols, external
    379    symbols, and symbol section index extensions, respectively.
    380    Returns a pointer to the internal symbol buffer (malloced if necessary)
    381    or NULL if there were no symbols or some kind of problem.  */
    382 
    383 Elf_Internal_Sym *
    384 bfd_elf_get_elf_syms (bfd *ibfd,
    385 		      Elf_Internal_Shdr *symtab_hdr,
    386 		      size_t symcount,
    387 		      size_t symoffset,
    388 		      Elf_Internal_Sym *intsym_buf,
    389 		      void *extsym_buf,
    390 		      Elf_External_Sym_Shndx *extshndx_buf)
    391 {
    392   Elf_Internal_Shdr *shndx_hdr;
    393   void *alloc_ext;
    394   const bfd_byte *esym;
    395   Elf_External_Sym_Shndx *alloc_extshndx;
    396   Elf_External_Sym_Shndx *shndx;
    397   Elf_Internal_Sym *alloc_intsym;
    398   Elf_Internal_Sym *isym;
    399   Elf_Internal_Sym *isymend;
    400   const struct elf_backend_data *bed;
    401   size_t extsym_size;
    402   size_t amt;
    403   file_ptr pos;
    404 
    405   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    406     abort ();
    407 
    408   if (symcount == 0)
    409     return intsym_buf;
    410 
    411   /* Normal syms might have section extension entries.  */
    412   shndx_hdr = NULL;
    413   if (elf_symtab_shndx_list (ibfd) != NULL)
    414     {
    415       elf_section_list * entry;
    416       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    417 
    418       /* Find an index section that is linked to this symtab section.  */
    419       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    420 	{
    421 	  /* PR 20063.  */
    422 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    423 	    continue;
    424 
    425 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    426 	    {
    427 	      shndx_hdr = & entry->hdr;
    428 	      break;
    429 	    };
    430 	}
    431 
    432       if (shndx_hdr == NULL)
    433 	{
    434 	  if (symtab_hdr == & elf_symtab_hdr (ibfd))
    435 	    /* Not really accurate, but this was how the old code used to work.  */
    436 	    shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
    437 	  /* Otherwise we do nothing.  The assumption is that
    438 	     the index table will not be needed.  */
    439 	}
    440     }
    441 
    442   /* Read the symbols.  */
    443   alloc_ext = NULL;
    444   alloc_extshndx = NULL;
    445   alloc_intsym = NULL;
    446   bed = get_elf_backend_data (ibfd);
    447   extsym_size = bed->s->sizeof_sym;
    448   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
    449     {
    450       bfd_set_error (bfd_error_file_too_big);
    451       intsym_buf = NULL;
    452       goto out;
    453     }
    454   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    455   if (extsym_buf == NULL)
    456     {
    457       alloc_ext = bfd_malloc (amt);
    458       extsym_buf = alloc_ext;
    459     }
    460   if (extsym_buf == NULL
    461       || bfd_seek (ibfd, pos, SEEK_SET) != 0
    462       || bfd_bread (extsym_buf, amt, ibfd) != amt)
    463     {
    464       intsym_buf = NULL;
    465       goto out;
    466     }
    467 
    468   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    469     extshndx_buf = NULL;
    470   else
    471     {
    472       if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
    473 	{
    474 	  bfd_set_error (bfd_error_file_too_big);
    475 	  intsym_buf = NULL;
    476 	  goto out;
    477 	}
    478       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    479       if (extshndx_buf == NULL)
    480 	{
    481 	  alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
    482 	  extshndx_buf = alloc_extshndx;
    483 	}
    484       if (extshndx_buf == NULL
    485 	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
    486 	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
    487 	{
    488 	  intsym_buf = NULL;
    489 	  goto out;
    490 	}
    491     }
    492 
    493   if (intsym_buf == NULL)
    494     {
    495       if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
    496 	{
    497 	  bfd_set_error (bfd_error_file_too_big);
    498 	  goto out;
    499 	}
    500       alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
    501       intsym_buf = alloc_intsym;
    502       if (intsym_buf == NULL)
    503 	goto out;
    504     }
    505 
    506   /* Convert the symbols to internal form.  */
    507   isymend = intsym_buf + symcount;
    508   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    509 	   shndx = extshndx_buf;
    510        isym < isymend;
    511        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    512     if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    513       {
    514 	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    515 	/* xgettext:c-format */
    516 	_bfd_error_handler (_("%pB symbol number %lu references"
    517 			      " nonexistent SHT_SYMTAB_SHNDX section"),
    518 			    ibfd, (unsigned long) symoffset);
    519 	free (alloc_intsym);
    520 	intsym_buf = NULL;
    521 	goto out;
    522       }
    523 
    524  out:
    525   free (alloc_ext);
    526   free (alloc_extshndx);
    527 
    528   return intsym_buf;
    529 }
    530 
    531 /* Look up a symbol name.  */
    532 const char *
    533 bfd_elf_sym_name (bfd *abfd,
    534 		  Elf_Internal_Shdr *symtab_hdr,
    535 		  Elf_Internal_Sym *isym,
    536 		  asection *sym_sec)
    537 {
    538   const char *name;
    539   unsigned int iname = isym->st_name;
    540   unsigned int shindex = symtab_hdr->sh_link;
    541 
    542   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    543       /* Check for a bogus st_shndx to avoid crashing.  */
    544       && isym->st_shndx < elf_numsections (abfd))
    545     {
    546       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    547       shindex = elf_elfheader (abfd)->e_shstrndx;
    548     }
    549 
    550   name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
    551   if (name == NULL)
    552     name = "(null)";
    553   else if (sym_sec && *name == '\0')
    554     name = bfd_section_name (sym_sec);
    555 
    556   return name;
    557 }
    558 
    559 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
    560    sections.  The first element is the flags, the rest are section
    561    pointers.  */
    562 
    563 typedef union elf_internal_group {
    564   Elf_Internal_Shdr *shdr;
    565   unsigned int flags;
    566 } Elf_Internal_Group;
    567 
    568 /* Return the name of the group signature symbol.  Why isn't the
    569    signature just a string?  */
    570 
    571 static const char *
    572 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    573 {
    574   Elf_Internal_Shdr *hdr;
    575   unsigned char esym[sizeof (Elf64_External_Sym)];
    576   Elf_External_Sym_Shndx eshndx;
    577   Elf_Internal_Sym isym;
    578 
    579   /* First we need to ensure the symbol table is available.  Make sure
    580      that it is a symbol table section.  */
    581   if (ghdr->sh_link >= elf_numsections (abfd))
    582     return NULL;
    583   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    584   if (hdr->sh_type != SHT_SYMTAB
    585       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    586     return NULL;
    587 
    588   /* Go read the symbol.  */
    589   hdr = &elf_tdata (abfd)->symtab_hdr;
    590   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    591 			    &isym, esym, &eshndx) == NULL)
    592     return NULL;
    593 
    594   return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
    595 }
    596 
    597 /* Set next_in_group list pointer, and group name for NEWSECT.  */
    598 
    599 static bool
    600 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
    601 {
    602   unsigned int num_group = elf_tdata (abfd)->num_group;
    603 
    604   /* If num_group is zero, read in all SHT_GROUP sections.  The count
    605      is set to -1 if there are no SHT_GROUP sections.  */
    606   if (num_group == 0)
    607     {
    608       unsigned int i, shnum;
    609 
    610       /* First count the number of groups.  If we have a SHT_GROUP
    611 	 section with just a flag word (ie. sh_size is 4), ignore it.  */
    612       shnum = elf_numsections (abfd);
    613       num_group = 0;
    614 
    615 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
    616 	(   (shdr)->sh_type == SHT_GROUP		\
    617 	 && (shdr)->sh_size >= minsize			\
    618 	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
    619 	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
    620 
    621       for (i = 0; i < shnum; i++)
    622 	{
    623 	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    624 
    625 	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    626 	    num_group += 1;
    627 	}
    628 
    629       if (num_group == 0)
    630 	{
    631 	  num_group = (unsigned) -1;
    632 	  elf_tdata (abfd)->num_group = num_group;
    633 	  elf_tdata (abfd)->group_sect_ptr = NULL;
    634 	}
    635       else
    636 	{
    637 	  /* We keep a list of elf section headers for group sections,
    638 	     so we can find them quickly.  */
    639 	  size_t amt;
    640 
    641 	  elf_tdata (abfd)->num_group = num_group;
    642 	  amt = num_group * sizeof (Elf_Internal_Shdr *);
    643 	  elf_tdata (abfd)->group_sect_ptr
    644 	    = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
    645 	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
    646 	    return false;
    647 	  num_group = 0;
    648 
    649 	  for (i = 0; i < shnum; i++)
    650 	    {
    651 	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    652 
    653 	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
    654 		{
    655 		  unsigned char *src;
    656 		  Elf_Internal_Group *dest;
    657 
    658 		  /* Make sure the group section has a BFD section
    659 		     attached to it.  */
    660 		  if (!bfd_section_from_shdr (abfd, i))
    661 		    return false;
    662 
    663 		  /* Add to list of sections.  */
    664 		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
    665 		  num_group += 1;
    666 
    667 		  /* Read the raw contents.  */
    668 		  BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
    669 		  shdr->contents = NULL;
    670 		  if (_bfd_mul_overflow (shdr->sh_size,
    671 					 sizeof (*dest) / 4, &amt)
    672 		      || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
    673 		      || !(shdr->contents
    674 			   = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
    675 		    {
    676 		      _bfd_error_handler
    677 			/* xgettext:c-format */
    678 			(_("%pB: invalid size field in group section"
    679 			   " header: %#" PRIx64 ""),
    680 			 abfd, (uint64_t) shdr->sh_size);
    681 		      bfd_set_error (bfd_error_bad_value);
    682 		      -- num_group;
    683 		      continue;
    684 		    }
    685 
    686 		  /* Translate raw contents, a flag word followed by an
    687 		     array of elf section indices all in target byte order,
    688 		     to the flag word followed by an array of elf section
    689 		     pointers.  */
    690 		  src = shdr->contents + shdr->sh_size;
    691 		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
    692 
    693 		  while (1)
    694 		    {
    695 		      unsigned int idx;
    696 
    697 		      src -= 4;
    698 		      --dest;
    699 		      idx = H_GET_32 (abfd, src);
    700 		      if (src == shdr->contents)
    701 			{
    702 			  dest->shdr = NULL;
    703 			  dest->flags = idx;
    704 			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
    705 			    shdr->bfd_section->flags
    706 			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    707 			  break;
    708 			}
    709 		      if (idx < shnum)
    710 			{
    711 			  dest->shdr = elf_elfsections (abfd)[idx];
    712 			  /* PR binutils/23199: All sections in a
    713 			     section group should be marked with
    714 			     SHF_GROUP.  But some tools generate
    715 			     broken objects without SHF_GROUP.  Fix
    716 			     them up here.  */
    717 			  dest->shdr->sh_flags |= SHF_GROUP;
    718 			}
    719 		      if (idx >= shnum
    720 			  || dest->shdr->sh_type == SHT_GROUP)
    721 			{
    722 			  _bfd_error_handler
    723 			    (_("%pB: invalid entry in SHT_GROUP section [%u]"),
    724 			       abfd, i);
    725 			  dest->shdr = NULL;
    726 			}
    727 		    }
    728 		}
    729 	    }
    730 
    731 	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
    732 	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
    733 	    {
    734 	      elf_tdata (abfd)->num_group = num_group;
    735 
    736 	      /* If all groups are invalid then fail.  */
    737 	      if (num_group == 0)
    738 		{
    739 		  elf_tdata (abfd)->group_sect_ptr = NULL;
    740 		  elf_tdata (abfd)->num_group = num_group = -1;
    741 		  _bfd_error_handler
    742 		    (_("%pB: no valid group sections found"), abfd);
    743 		  bfd_set_error (bfd_error_bad_value);
    744 		}
    745 	    }
    746 	}
    747     }
    748 
    749   if (num_group != (unsigned) -1)
    750     {
    751       unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
    752       unsigned int j;
    753 
    754       for (j = 0; j < num_group; j++)
    755 	{
    756 	  /* Begin search from previous found group.  */
    757 	  unsigned i = (j + search_offset) % num_group;
    758 
    759 	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    760 	  Elf_Internal_Group *idx;
    761 	  bfd_size_type n_elt;
    762 
    763 	  if (shdr == NULL)
    764 	    continue;
    765 
    766 	  idx = (Elf_Internal_Group *) shdr->contents;
    767 	  if (idx == NULL || shdr->sh_size < 4)
    768 	    {
    769 	      /* See PR 21957 for a reproducer.  */
    770 	      /* xgettext:c-format */
    771 	      _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
    772 				  abfd, shdr->bfd_section);
    773 	      elf_tdata (abfd)->group_sect_ptr[i] = NULL;
    774 	      bfd_set_error (bfd_error_bad_value);
    775 	      return false;
    776 	    }
    777 	  n_elt = shdr->sh_size / 4;
    778 
    779 	  /* Look through this group's sections to see if current
    780 	     section is a member.  */
    781 	  while (--n_elt != 0)
    782 	    if ((++idx)->shdr == hdr)
    783 	      {
    784 		asection *s = NULL;
    785 
    786 		/* We are a member of this group.  Go looking through
    787 		   other members to see if any others are linked via
    788 		   next_in_group.  */
    789 		idx = (Elf_Internal_Group *) shdr->contents;
    790 		n_elt = shdr->sh_size / 4;
    791 		while (--n_elt != 0)
    792 		  if ((++idx)->shdr != NULL
    793 		      && (s = idx->shdr->bfd_section) != NULL
    794 		      && elf_next_in_group (s) != NULL)
    795 		    break;
    796 		if (n_elt != 0)
    797 		  {
    798 		    /* Snarf the group name from other member, and
    799 		       insert current section in circular list.  */
    800 		    elf_group_name (newsect) = elf_group_name (s);
    801 		    elf_next_in_group (newsect) = elf_next_in_group (s);
    802 		    elf_next_in_group (s) = newsect;
    803 		  }
    804 		else
    805 		  {
    806 		    const char *gname;
    807 
    808 		    gname = group_signature (abfd, shdr);
    809 		    if (gname == NULL)
    810 		      return false;
    811 		    elf_group_name (newsect) = gname;
    812 
    813 		    /* Start a circular list with one element.  */
    814 		    elf_next_in_group (newsect) = newsect;
    815 		  }
    816 
    817 		/* If the group section has been created, point to the
    818 		   new member.  */
    819 		if (shdr->bfd_section != NULL)
    820 		  elf_next_in_group (shdr->bfd_section) = newsect;
    821 
    822 		elf_tdata (abfd)->group_search_offset = i;
    823 		j = num_group - 1;
    824 		break;
    825 	      }
    826 	}
    827     }
    828 
    829   if (elf_group_name (newsect) == NULL)
    830     {
    831       /* xgettext:c-format */
    832       _bfd_error_handler (_("%pB: no group info for section '%pA'"),
    833 			  abfd, newsect);
    834       return false;
    835     }
    836   return true;
    837 }
    838 
    839 bool
    840 _bfd_elf_setup_sections (bfd *abfd)
    841 {
    842   unsigned int i;
    843   unsigned int num_group = elf_tdata (abfd)->num_group;
    844   bool result = true;
    845   asection *s;
    846 
    847   /* Process SHF_LINK_ORDER.  */
    848   for (s = abfd->sections; s != NULL; s = s->next)
    849     {
    850       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    851       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    852 	{
    853 	  unsigned int elfsec = this_hdr->sh_link;
    854 	  /* An sh_link value of 0 is now allowed.  It indicates that linked
    855 	     to section has already been discarded, but that the current
    856 	     section has been retained for some other reason.  This linking
    857 	     section is still a candidate for later garbage collection
    858 	     however.  */
    859 	  if (elfsec == 0)
    860 	    {
    861 	      elf_linked_to_section (s) = NULL;
    862 	    }
    863 	  else
    864 	    {
    865 	      asection *linksec = NULL;
    866 
    867 	      if (elfsec < elf_numsections (abfd))
    868 		{
    869 		  this_hdr = elf_elfsections (abfd)[elfsec];
    870 		  linksec = this_hdr->bfd_section;
    871 		}
    872 
    873 	      /* PR 1991, 2008:
    874 		 Some strip/objcopy may leave an incorrect value in
    875 		 sh_link.  We don't want to proceed.  */
    876 	      if (linksec == NULL)
    877 		{
    878 		  _bfd_error_handler
    879 		    /* xgettext:c-format */
    880 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    881 		     s->owner, elfsec, s);
    882 		  result = false;
    883 		}
    884 
    885 	      elf_linked_to_section (s) = linksec;
    886 	    }
    887 	}
    888       else if (this_hdr->sh_type == SHT_GROUP
    889 	       && elf_next_in_group (s) == NULL)
    890 	{
    891 	  _bfd_error_handler
    892 	    /* xgettext:c-format */
    893 	    (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
    894 	     abfd, elf_section_data (s)->this_idx);
    895 	  result = false;
    896 	}
    897     }
    898 
    899   /* Process section groups.  */
    900   if (num_group == (unsigned) -1)
    901     return result;
    902 
    903   for (i = 0; i < num_group; i++)
    904     {
    905       Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
    906       Elf_Internal_Group *idx;
    907       unsigned int n_elt;
    908 
    909       /* PR binutils/18758: Beware of corrupt binaries with invalid group data.  */
    910       if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
    911 	{
    912 	  _bfd_error_handler
    913 	    /* xgettext:c-format */
    914 	    (_("%pB: section group entry number %u is corrupt"),
    915 	     abfd, i);
    916 	  result = false;
    917 	  continue;
    918 	}
    919 
    920       idx = (Elf_Internal_Group *) shdr->contents;
    921       n_elt = shdr->sh_size / 4;
    922 
    923       while (--n_elt != 0)
    924 	{
    925 	  ++ idx;
    926 
    927 	  if (idx->shdr == NULL)
    928 	    continue;
    929 	  else if (idx->shdr->bfd_section)
    930 	    elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
    931 	  else if (idx->shdr->sh_type != SHT_RELA
    932 		   && idx->shdr->sh_type != SHT_REL)
    933 	    {
    934 	      /* There are some unknown sections in the group.  */
    935 	      _bfd_error_handler
    936 		/* xgettext:c-format */
    937 		(_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
    938 		 abfd,
    939 		 idx->shdr->sh_type,
    940 		 bfd_elf_string_from_elf_section (abfd,
    941 						  (elf_elfheader (abfd)
    942 						   ->e_shstrndx),
    943 						  idx->shdr->sh_name),
    944 		 shdr->bfd_section);
    945 	      result = false;
    946 	    }
    947 	}
    948     }
    949 
    950   return result;
    951 }
    952 
    953 bool
    954 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    955 {
    956   return elf_next_in_group (sec) != NULL;
    957 }
    958 
    959 const char *
    960 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    961 {
    962   if (elf_sec_group (sec) != NULL)
    963     return elf_group_name (sec);
    964   return NULL;
    965 }
    966 
    967 static char *
    968 convert_debug_to_zdebug (bfd *abfd, const char *name)
    969 {
    970   unsigned int len = strlen (name);
    971   char *new_name = bfd_alloc (abfd, len + 2);
    972   if (new_name == NULL)
    973     return NULL;
    974   new_name[0] = '.';
    975   new_name[1] = 'z';
    976   memcpy (new_name + 2, name + 1, len);
    977   return new_name;
    978 }
    979 
    980 static char *
    981 convert_zdebug_to_debug (bfd *abfd, const char *name)
    982 {
    983   unsigned int len = strlen (name);
    984   char *new_name = bfd_alloc (abfd, len);
    985   if (new_name == NULL)
    986     return NULL;
    987   new_name[0] = '.';
    988   memcpy (new_name + 1, name + 2, len - 1);
    989   return new_name;
    990 }
    991 
    992 /* This a copy of lto_section defined in GCC (lto-streamer.h).  */
    993 
    994 struct lto_section
    995 {
    996   int16_t major_version;
    997   int16_t minor_version;
    998   unsigned char slim_object;
    999 
   1000   /* Flags is a private field that is not defined publicly.  */
   1001   uint16_t flags;
   1002 };
   1003 
   1004 /* Make a BFD section from an ELF section.  We store a pointer to the
   1005    BFD section in the bfd_section field of the header.  */
   1006 
   1007 bool
   1008 _bfd_elf_make_section_from_shdr (bfd *abfd,
   1009 				 Elf_Internal_Shdr *hdr,
   1010 				 const char *name,
   1011 				 int shindex)
   1012 {
   1013   asection *newsect;
   1014   flagword flags;
   1015   const struct elf_backend_data *bed;
   1016   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   1017 
   1018   if (hdr->bfd_section != NULL)
   1019     return true;
   1020 
   1021   newsect = bfd_make_section_anyway (abfd, name);
   1022   if (newsect == NULL)
   1023     return false;
   1024 
   1025   hdr->bfd_section = newsect;
   1026   elf_section_data (newsect)->this_hdr = *hdr;
   1027   elf_section_data (newsect)->this_idx = shindex;
   1028 
   1029   /* Always use the real type/flags.  */
   1030   elf_section_type (newsect) = hdr->sh_type;
   1031   elf_section_flags (newsect) = hdr->sh_flags;
   1032 
   1033   newsect->filepos = hdr->sh_offset;
   1034 
   1035   flags = SEC_NO_FLAGS;
   1036   if (hdr->sh_type != SHT_NOBITS)
   1037     flags |= SEC_HAS_CONTENTS;
   1038   if (hdr->sh_type == SHT_GROUP)
   1039     flags |= SEC_GROUP;
   1040   if ((hdr->sh_flags & SHF_ALLOC) != 0)
   1041     {
   1042       flags |= SEC_ALLOC;
   1043       if (hdr->sh_type != SHT_NOBITS)
   1044 	flags |= SEC_LOAD;
   1045     }
   1046   if ((hdr->sh_flags & SHF_WRITE) == 0)
   1047     flags |= SEC_READONLY;
   1048   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
   1049     flags |= SEC_CODE;
   1050   else if ((flags & SEC_LOAD) != 0)
   1051     flags |= SEC_DATA;
   1052   if ((hdr->sh_flags & SHF_MERGE) != 0)
   1053     {
   1054       flags |= SEC_MERGE;
   1055       newsect->entsize = hdr->sh_entsize;
   1056     }
   1057   if ((hdr->sh_flags & SHF_STRINGS) != 0)
   1058     flags |= SEC_STRINGS;
   1059   if (hdr->sh_flags & SHF_GROUP)
   1060     if (!setup_group (abfd, hdr, newsect))
   1061       return false;
   1062   if ((hdr->sh_flags & SHF_TLS) != 0)
   1063     flags |= SEC_THREAD_LOCAL;
   1064   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
   1065     flags |= SEC_EXCLUDE;
   1066 
   1067   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
   1068     {
   1069       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
   1070 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
   1071 	 byte.  */
   1072     case ELFOSABI_GNU:
   1073     case ELFOSABI_FREEBSD:
   1074       if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
   1075 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
   1076       /* Fall through */
   1077     case ELFOSABI_NONE:
   1078       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
   1079 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
   1080       break;
   1081     }
   1082 
   1083   if ((flags & SEC_ALLOC) == 0)
   1084     {
   1085       /* The debugging sections appear to be recognized only by name,
   1086 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
   1087       if (name [0] == '.')
   1088 	{
   1089 	  if (startswith (name, ".debug")
   1090 	      || startswith (name, ".gnu.debuglto_.debug_")
   1091 	      || startswith (name, ".gnu.linkonce.wi.")
   1092 	      || startswith (name, ".zdebug"))
   1093 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
   1094 	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
   1095 		   || startswith (name, ".note.gnu"))
   1096 	    {
   1097 	      flags |= SEC_ELF_OCTETS;
   1098 	      opb = 1;
   1099 	    }
   1100 	  else if (startswith (name, ".line")
   1101 		   || startswith (name, ".stab")
   1102 		   || strcmp (name, ".gdb_index") == 0)
   1103 	    flags |= SEC_DEBUGGING;
   1104 	}
   1105     }
   1106 
   1107   if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
   1108       || !bfd_set_section_size (newsect, hdr->sh_size)
   1109       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
   1110 							& -hdr->sh_addralign)))
   1111     return false;
   1112 
   1113   /* As a GNU extension, if the name begins with .gnu.linkonce, we
   1114      only link a single copy of the section.  This is used to support
   1115      g++.  g++ will emit each template expansion in its own section.
   1116      The symbols will be defined as weak, so that multiple definitions
   1117      are permitted.  The GNU linker extension is to actually discard
   1118      all but one of the sections.  */
   1119   if (startswith (name, ".gnu.linkonce")
   1120       && elf_next_in_group (newsect) == NULL)
   1121     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1122 
   1123   if (!bfd_set_section_flags (newsect, flags))
   1124     return false;
   1125 
   1126   bed = get_elf_backend_data (abfd);
   1127   if (bed->elf_backend_section_flags)
   1128     if (!bed->elf_backend_section_flags (hdr))
   1129       return false;
   1130 
   1131   /* We do not parse the PT_NOTE segments as we are interested even in the
   1132      separate debug info files which may have the segments offsets corrupted.
   1133      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1134   if (hdr->sh_type == SHT_NOTE)
   1135     {
   1136       bfd_byte *contents;
   1137 
   1138       if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
   1139 	return false;
   1140 
   1141       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1142 		       hdr->sh_offset, hdr->sh_addralign);
   1143       free (contents);
   1144     }
   1145 
   1146   if ((newsect->flags & SEC_ALLOC) != 0)
   1147     {
   1148       Elf_Internal_Phdr *phdr;
   1149       unsigned int i, nload;
   1150 
   1151       /* Some ELF linkers produce binaries with all the program header
   1152 	 p_paddr fields zero.  If we have such a binary with more than
   1153 	 one PT_LOAD header, then leave the section lma equal to vma
   1154 	 so that we don't create sections with overlapping lma.  */
   1155       phdr = elf_tdata (abfd)->phdr;
   1156       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1157 	if (phdr->p_paddr != 0)
   1158 	  break;
   1159 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1160 	  ++nload;
   1161       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1162 	return true;
   1163 
   1164       phdr = elf_tdata (abfd)->phdr;
   1165       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1166 	{
   1167 	  if (((phdr->p_type == PT_LOAD
   1168 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1169 	       || phdr->p_type == PT_TLS)
   1170 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1171 	    {
   1172 	      if ((newsect->flags & SEC_LOAD) == 0)
   1173 		newsect->lma = (phdr->p_paddr
   1174 				+ hdr->sh_addr - phdr->p_vaddr) / opb;
   1175 	      else
   1176 		/* We used to use the same adjustment for SEC_LOAD
   1177 		   sections, but that doesn't work if the segment
   1178 		   is packed with code from multiple VMAs.
   1179 		   Instead we calculate the section LMA based on
   1180 		   the segment LMA.  It is assumed that the
   1181 		   segment will contain sections with contiguous
   1182 		   LMAs, even if the VMAs are not.  */
   1183 		newsect->lma = (phdr->p_paddr
   1184 				+ hdr->sh_offset - phdr->p_offset) / opb;
   1185 
   1186 	      /* With contiguous segments, we can't tell from file
   1187 		 offsets whether a section with zero size should
   1188 		 be placed at the end of one segment or the
   1189 		 beginning of the next.  Decide based on vaddr.  */
   1190 	      if (hdr->sh_addr >= phdr->p_vaddr
   1191 		  && (hdr->sh_addr + hdr->sh_size
   1192 		      <= phdr->p_vaddr + phdr->p_memsz))
   1193 		break;
   1194 	    }
   1195 	}
   1196     }
   1197 
   1198   /* Compress/decompress DWARF debug sections with names: .debug_* and
   1199      .zdebug_*, after the section flags is set.  */
   1200   if ((newsect->flags & SEC_DEBUGGING)
   1201       && ((name[1] == 'd' && name[6] == '_')
   1202 	  || (name[1] == 'z' && name[7] == '_')))
   1203     {
   1204       enum { nothing, compress, decompress } action = nothing;
   1205       int compression_header_size;
   1206       bfd_size_type uncompressed_size;
   1207       unsigned int uncompressed_align_power;
   1208       bool compressed
   1209 	= bfd_is_section_compressed_with_header (abfd, newsect,
   1210 						 &compression_header_size,
   1211 						 &uncompressed_size,
   1212 						 &uncompressed_align_power);
   1213       if (compressed)
   1214 	{
   1215 	  /* Compressed section.  Check if we should decompress.  */
   1216 	  if ((abfd->flags & BFD_DECOMPRESS))
   1217 	    action = decompress;
   1218 	}
   1219 
   1220       /* Compress the uncompressed section or convert from/to .zdebug*
   1221 	 section.  Check if we should compress.  */
   1222       if (action == nothing)
   1223 	{
   1224 	  if (newsect->size != 0
   1225 	      && (abfd->flags & BFD_COMPRESS)
   1226 	      && compression_header_size >= 0
   1227 	      && uncompressed_size > 0
   1228 	      && (!compressed
   1229 		  || ((compression_header_size > 0)
   1230 		      != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
   1231 	    action = compress;
   1232 	  else
   1233 	    return true;
   1234 	}
   1235 
   1236       if (action == compress)
   1237 	{
   1238 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1239 	    {
   1240 	      _bfd_error_handler
   1241 		/* xgettext:c-format */
   1242 		(_("%pB: unable to initialize compress status for section %s"),
   1243 		 abfd, name);
   1244 	      return false;
   1245 	    }
   1246 	}
   1247       else
   1248 	{
   1249 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1250 	    {
   1251 	      _bfd_error_handler
   1252 		/* xgettext:c-format */
   1253 		(_("%pB: unable to initialize decompress status for section %s"),
   1254 		 abfd, name);
   1255 	      return false;
   1256 	    }
   1257 	}
   1258 
   1259       if (abfd->is_linker_input)
   1260 	{
   1261 	  if (name[1] == 'z'
   1262 	      && (action == decompress
   1263 		  || (action == compress
   1264 		      && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
   1265 	    {
   1266 	      /* Convert section name from .zdebug_* to .debug_* so
   1267 		 that linker will consider this section as a debug
   1268 		 section.  */
   1269 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   1270 	      if (new_name == NULL)
   1271 		return false;
   1272 	      bfd_rename_section (newsect, new_name);
   1273 	    }
   1274 	}
   1275       else
   1276 	/* For objdump, don't rename the section.  For objcopy, delay
   1277 	   section rename to elf_fake_sections.  */
   1278 	newsect->flags |= SEC_ELF_RENAME;
   1279     }
   1280 
   1281   /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
   1282      section.  */
   1283   if (startswith (name, ".gnu.lto_.lto."))
   1284     {
   1285       struct lto_section lsection;
   1286       if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
   1287 				    sizeof (struct lto_section)))
   1288 	abfd->lto_slim_object = lsection.slim_object;
   1289     }
   1290 
   1291   return true;
   1292 }
   1293 
   1294 const char *const bfd_elf_section_type_names[] =
   1295 {
   1296   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1297   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1298   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1299 };
   1300 
   1301 /* ELF relocs are against symbols.  If we are producing relocatable
   1302    output, and the reloc is against an external symbol, and nothing
   1303    has given us any additional addend, the resulting reloc will also
   1304    be against the same symbol.  In such a case, we don't want to
   1305    change anything about the way the reloc is handled, since it will
   1306    all be done at final link time.  Rather than put special case code
   1307    into bfd_perform_relocation, all the reloc types use this howto
   1308    function, or should call this function for relocatable output.  */
   1309 
   1310 bfd_reloc_status_type
   1311 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1312 		       arelent *reloc_entry,
   1313 		       asymbol *symbol,
   1314 		       void *data ATTRIBUTE_UNUSED,
   1315 		       asection *input_section,
   1316 		       bfd *output_bfd,
   1317 		       char **error_message ATTRIBUTE_UNUSED)
   1318 {
   1319   if (output_bfd != NULL
   1320       && (symbol->flags & BSF_SECTION_SYM) == 0
   1321       && (! reloc_entry->howto->partial_inplace
   1322 	  || reloc_entry->addend == 0))
   1323     {
   1324       reloc_entry->address += input_section->output_offset;
   1325       return bfd_reloc_ok;
   1326     }
   1327 
   1328   /* In some cases the relocation should be treated as output section
   1329      relative, as when linking ELF DWARF into PE COFF.  Many ELF
   1330      targets lack section relative relocations and instead use
   1331      ordinary absolute relocations for references between DWARF
   1332      sections.  That is arguably a bug in those targets but it happens
   1333      to work for the usual case of linking to non-loaded ELF debug
   1334      sections with VMAs forced to zero.  PE COFF on the other hand
   1335      doesn't allow a section VMA of zero.  */
   1336   if (output_bfd == NULL
   1337       && !reloc_entry->howto->pc_relative
   1338       && (symbol->section->flags & SEC_DEBUGGING) != 0
   1339       && (input_section->flags & SEC_DEBUGGING) != 0)
   1340     reloc_entry->addend -= symbol->section->output_section->vma;
   1341 
   1342   return bfd_reloc_continue;
   1343 }
   1344 
   1345 /* Returns TRUE if section A matches section B.
   1347    Names, addresses and links may be different, but everything else
   1348    should be the same.  */
   1349 
   1350 static bool
   1351 section_match (const Elf_Internal_Shdr * a,
   1352 	       const Elf_Internal_Shdr * b)
   1353 {
   1354   if (a->sh_type != b->sh_type
   1355       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1356       || a->sh_addralign != b->sh_addralign
   1357       || a->sh_entsize != b->sh_entsize)
   1358     return false;
   1359   if (a->sh_type == SHT_SYMTAB
   1360       || a->sh_type == SHT_STRTAB)
   1361     return true;
   1362   return a->sh_size == b->sh_size;
   1363 }
   1364 
   1365 /* Find a section in OBFD that has the same characteristics
   1366    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1367    none can be found.  Check's section HINT first, as this is likely
   1368    to be the correct section.  */
   1369 
   1370 static unsigned int
   1371 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1372 	   const unsigned int hint)
   1373 {
   1374   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1375   unsigned int i;
   1376 
   1377   BFD_ASSERT (iheader != NULL);
   1378 
   1379   /* See PR 20922 for a reproducer of the NULL test.  */
   1380   if (hint < elf_numsections (obfd)
   1381       && oheaders[hint] != NULL
   1382       && section_match (oheaders[hint], iheader))
   1383     return hint;
   1384 
   1385   for (i = 1; i < elf_numsections (obfd); i++)
   1386     {
   1387       Elf_Internal_Shdr * oheader = oheaders[i];
   1388 
   1389       if (oheader == NULL)
   1390 	continue;
   1391       if (section_match (oheader, iheader))
   1392 	/* FIXME: Do we care if there is a potential for
   1393 	   multiple matches ?  */
   1394 	return i;
   1395     }
   1396 
   1397   return SHN_UNDEF;
   1398 }
   1399 
   1400 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1401    Processor specific section, based upon a matching input section.
   1402    Returns TRUE upon success, FALSE otherwise.  */
   1403 
   1404 static bool
   1405 copy_special_section_fields (const bfd *ibfd,
   1406 			     bfd *obfd,
   1407 			     const Elf_Internal_Shdr *iheader,
   1408 			     Elf_Internal_Shdr *oheader,
   1409 			     const unsigned int secnum)
   1410 {
   1411   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1412   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1413   bool changed = false;
   1414   unsigned int sh_link;
   1415 
   1416   if (oheader->sh_type == SHT_NOBITS)
   1417     {
   1418       /* This is a feature for objcopy --only-keep-debug:
   1419 	 When a section's type is changed to NOBITS, we preserve
   1420 	 the sh_link and sh_info fields so that they can be
   1421 	 matched up with the original.
   1422 
   1423 	 Note: Strictly speaking these assignments are wrong.
   1424 	 The sh_link and sh_info fields should point to the
   1425 	 relevent sections in the output BFD, which may not be in
   1426 	 the same location as they were in the input BFD.  But
   1427 	 the whole point of this action is to preserve the
   1428 	 original values of the sh_link and sh_info fields, so
   1429 	 that they can be matched up with the section headers in
   1430 	 the original file.  So strictly speaking we may be
   1431 	 creating an invalid ELF file, but it is only for a file
   1432 	 that just contains debug info and only for sections
   1433 	 without any contents.  */
   1434       if (oheader->sh_link == 0)
   1435 	oheader->sh_link = iheader->sh_link;
   1436       if (oheader->sh_info == 0)
   1437 	oheader->sh_info = iheader->sh_info;
   1438       return true;
   1439     }
   1440 
   1441   /* Allow the target a chance to decide how these fields should be set.  */
   1442   if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1443 						    iheader, oheader))
   1444     return true;
   1445 
   1446   /* We have an iheader which might match oheader, and which has non-zero
   1447      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1448      the section in the output bfd which corresponds to the linked section
   1449      in the input bfd.  */
   1450   if (iheader->sh_link != SHN_UNDEF)
   1451     {
   1452       /* See PR 20931 for a reproducer.  */
   1453       if (iheader->sh_link >= elf_numsections (ibfd))
   1454 	{
   1455 	  _bfd_error_handler
   1456 	    /* xgettext:c-format */
   1457 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1458 	     ibfd, iheader->sh_link, secnum);
   1459 	  return false;
   1460 	}
   1461 
   1462       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1463       if (sh_link != SHN_UNDEF)
   1464 	{
   1465 	  oheader->sh_link = sh_link;
   1466 	  changed = true;
   1467 	}
   1468       else
   1469 	/* FIXME: Should we install iheader->sh_link
   1470 	   if we could not find a match ?  */
   1471 	_bfd_error_handler
   1472 	  /* xgettext:c-format */
   1473 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1474     }
   1475 
   1476   if (iheader->sh_info)
   1477     {
   1478       /* The sh_info field can hold arbitrary information, but if the
   1479 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1480 	 section index.  */
   1481       if (iheader->sh_flags & SHF_INFO_LINK)
   1482 	{
   1483 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1484 			       iheader->sh_info);
   1485 	  if (sh_link != SHN_UNDEF)
   1486 	    oheader->sh_flags |= SHF_INFO_LINK;
   1487 	}
   1488       else
   1489 	/* No idea what it means - just copy it.  */
   1490 	sh_link = iheader->sh_info;
   1491 
   1492       if (sh_link != SHN_UNDEF)
   1493 	{
   1494 	  oheader->sh_info = sh_link;
   1495 	  changed = true;
   1496 	}
   1497       else
   1498 	_bfd_error_handler
   1499 	  /* xgettext:c-format */
   1500 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1501     }
   1502 
   1503   return changed;
   1504 }
   1505 
   1506 /* Copy the program header and other data from one object module to
   1507    another.  */
   1508 
   1509 bool
   1510 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1511 {
   1512   const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1513   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1514   const struct elf_backend_data *bed;
   1515   unsigned int i;
   1516 
   1517   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1518     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1519     return true;
   1520 
   1521   if (!elf_flags_init (obfd))
   1522     {
   1523       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1524       elf_flags_init (obfd) = true;
   1525     }
   1526 
   1527   elf_gp (obfd) = elf_gp (ibfd);
   1528 
   1529   /* Also copy the EI_OSABI field.  */
   1530   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1531     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1532 
   1533   /* If set, copy the EI_ABIVERSION field.  */
   1534   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1535     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1536       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1537 
   1538   /* Copy object attributes.  */
   1539   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1540 
   1541   if (iheaders == NULL || oheaders == NULL)
   1542     return true;
   1543 
   1544   bed = get_elf_backend_data (obfd);
   1545 
   1546   /* Possibly copy other fields in the section header.  */
   1547   for (i = 1; i < elf_numsections (obfd); i++)
   1548     {
   1549       unsigned int j;
   1550       Elf_Internal_Shdr * oheader = oheaders[i];
   1551 
   1552       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1553 	 because of a special case need for generating separate debug info
   1554 	 files.  See below for more details.  */
   1555       if (oheader == NULL
   1556 	  || (oheader->sh_type != SHT_NOBITS
   1557 	      && oheader->sh_type < SHT_LOOS))
   1558 	continue;
   1559 
   1560       /* Ignore empty sections, and sections whose
   1561 	 fields have already been initialised.  */
   1562       if (oheader->sh_size == 0
   1563 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1564 	continue;
   1565 
   1566       /* Scan for the matching section in the input bfd.
   1567 	 First we try for a direct mapping between the input and output sections.  */
   1568       for (j = 1; j < elf_numsections (ibfd); j++)
   1569 	{
   1570 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1571 
   1572 	  if (iheader == NULL)
   1573 	    continue;
   1574 
   1575 	  if (oheader->bfd_section != NULL
   1576 	      && iheader->bfd_section != NULL
   1577 	      && iheader->bfd_section->output_section != NULL
   1578 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1579 	    {
   1580 	      /* We have found a connection from the input section to the
   1581 		 output section.  Attempt to copy the header fields.  If
   1582 		 this fails then do not try any further sections - there
   1583 		 should only be a one-to-one mapping between input and output. */
   1584 	      if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1585 		j = elf_numsections (ibfd);
   1586 	      break;
   1587 	    }
   1588 	}
   1589 
   1590       if (j < elf_numsections (ibfd))
   1591 	continue;
   1592 
   1593       /* That failed.  So try to deduce the corresponding input section.
   1594 	 Unfortunately we cannot compare names as the output string table
   1595 	 is empty, so instead we check size, address and type.  */
   1596       for (j = 1; j < elf_numsections (ibfd); j++)
   1597 	{
   1598 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1599 
   1600 	  if (iheader == NULL)
   1601 	    continue;
   1602 
   1603 	  /* Try matching fields in the input section's header.
   1604 	     Since --only-keep-debug turns all non-debug sections into
   1605 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1606 	     input type.  */
   1607 	  if ((oheader->sh_type == SHT_NOBITS
   1608 	       || iheader->sh_type == oheader->sh_type)
   1609 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1610 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1611 	      && iheader->sh_addralign == oheader->sh_addralign
   1612 	      && iheader->sh_entsize == oheader->sh_entsize
   1613 	      && iheader->sh_size == oheader->sh_size
   1614 	      && iheader->sh_addr == oheader->sh_addr
   1615 	      && (iheader->sh_info != oheader->sh_info
   1616 		  || iheader->sh_link != oheader->sh_link))
   1617 	    {
   1618 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1619 		break;
   1620 	    }
   1621 	}
   1622 
   1623       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1624 	{
   1625 	  /* Final attempt.  Call the backend copy function
   1626 	     with a NULL input section.  */
   1627 	  (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1628 							       NULL, oheader);
   1629 	}
   1630     }
   1631 
   1632   return true;
   1633 }
   1634 
   1635 static const char *
   1636 get_segment_type (unsigned int p_type)
   1637 {
   1638   const char *pt;
   1639   switch (p_type)
   1640     {
   1641     case PT_NULL: pt = "NULL"; break;
   1642     case PT_LOAD: pt = "LOAD"; break;
   1643     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1644     case PT_INTERP: pt = "INTERP"; break;
   1645     case PT_NOTE: pt = "NOTE"; break;
   1646     case PT_SHLIB: pt = "SHLIB"; break;
   1647     case PT_PHDR: pt = "PHDR"; break;
   1648     case PT_TLS: pt = "TLS"; break;
   1649     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1650     case PT_GNU_STACK: pt = "STACK"; break;
   1651     case PT_GNU_RELRO: pt = "RELRO"; break;
   1652     default: pt = NULL; break;
   1653     }
   1654   return pt;
   1655 }
   1656 
   1657 /* Print out the program headers.  */
   1658 
   1659 bool
   1660 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1661 {
   1662   FILE *f = (FILE *) farg;
   1663   Elf_Internal_Phdr *p;
   1664   asection *s;
   1665   bfd_byte *dynbuf = NULL;
   1666 
   1667   p = elf_tdata (abfd)->phdr;
   1668   if (p != NULL)
   1669     {
   1670       unsigned int i, c;
   1671 
   1672       fprintf (f, _("\nProgram Header:\n"));
   1673       c = elf_elfheader (abfd)->e_phnum;
   1674       for (i = 0; i < c; i++, p++)
   1675 	{
   1676 	  const char *pt = get_segment_type (p->p_type);
   1677 	  char buf[20];
   1678 
   1679 	  if (pt == NULL)
   1680 	    {
   1681 	      sprintf (buf, "0x%lx", p->p_type);
   1682 	      pt = buf;
   1683 	    }
   1684 	  fprintf (f, "%8s off    0x", pt);
   1685 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1686 	  fprintf (f, " vaddr 0x");
   1687 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1688 	  fprintf (f, " paddr 0x");
   1689 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1690 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1691 	  fprintf (f, "         filesz 0x");
   1692 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1693 	  fprintf (f, " memsz 0x");
   1694 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1695 	  fprintf (f, " flags %c%c%c",
   1696 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1697 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1698 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1699 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1700 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1701 	  fprintf (f, "\n");
   1702 	}
   1703     }
   1704 
   1705   s = bfd_get_section_by_name (abfd, ".dynamic");
   1706   if (s != NULL)
   1707     {
   1708       unsigned int elfsec;
   1709       unsigned long shlink;
   1710       bfd_byte *extdyn, *extdynend;
   1711       size_t extdynsize;
   1712       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1713 
   1714       fprintf (f, _("\nDynamic Section:\n"));
   1715 
   1716       if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
   1717 	goto error_return;
   1718 
   1719       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1720       if (elfsec == SHN_BAD)
   1721 	goto error_return;
   1722       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1723 
   1724       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1725       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1726 
   1727       extdyn = dynbuf;
   1728       /* PR 17512: file: 6f427532.  */
   1729       if (s->size < extdynsize)
   1730 	goto error_return;
   1731       extdynend = extdyn + s->size;
   1732       /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
   1733 	 Fix range check.  */
   1734       for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   1735 	{
   1736 	  Elf_Internal_Dyn dyn;
   1737 	  const char *name = "";
   1738 	  char ab[20];
   1739 	  bool stringp;
   1740 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1741 
   1742 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1743 
   1744 	  if (dyn.d_tag == DT_NULL)
   1745 	    break;
   1746 
   1747 	  stringp = false;
   1748 	  switch (dyn.d_tag)
   1749 	    {
   1750 	    default:
   1751 	      if (bed->elf_backend_get_target_dtag)
   1752 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1753 
   1754 	      if (!strcmp (name, ""))
   1755 		{
   1756 		  sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
   1757 		  name = ab;
   1758 		}
   1759 	      break;
   1760 
   1761 	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
   1762 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1763 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1764 	    case DT_HASH: name = "HASH"; break;
   1765 	    case DT_STRTAB: name = "STRTAB"; break;
   1766 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1767 	    case DT_RELA: name = "RELA"; break;
   1768 	    case DT_RELASZ: name = "RELASZ"; break;
   1769 	    case DT_RELAENT: name = "RELAENT"; break;
   1770 	    case DT_STRSZ: name = "STRSZ"; break;
   1771 	    case DT_SYMENT: name = "SYMENT"; break;
   1772 	    case DT_INIT: name = "INIT"; break;
   1773 	    case DT_FINI: name = "FINI"; break;
   1774 	    case DT_SONAME: name = "SONAME"; stringp = true; break;
   1775 	    case DT_RPATH: name = "RPATH"; stringp = true; break;
   1776 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1777 	    case DT_REL: name = "REL"; break;
   1778 	    case DT_RELSZ: name = "RELSZ"; break;
   1779 	    case DT_RELENT: name = "RELENT"; break;
   1780 	    case DT_RELR: name = "RELR"; break;
   1781 	    case DT_RELRSZ: name = "RELRSZ"; break;
   1782 	    case DT_RELRENT: name = "RELRENT"; break;
   1783 	    case DT_PLTREL: name = "PLTREL"; break;
   1784 	    case DT_DEBUG: name = "DEBUG"; break;
   1785 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1786 	    case DT_JMPREL: name = "JMPREL"; break;
   1787 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1788 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1789 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1790 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1791 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1792 	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
   1793 	    case DT_FLAGS: name = "FLAGS"; break;
   1794 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1795 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1796 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1797 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1798 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1799 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1800 	    case DT_FEATURE: name = "FEATURE"; break;
   1801 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1802 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1803 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1804 	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
   1805 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
   1806 	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
   1807 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1808 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1809 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1810 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1811 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1812 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1813 	    case DT_VERSYM: name = "VERSYM"; break;
   1814 	    case DT_VERDEF: name = "VERDEF"; break;
   1815 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1816 	    case DT_VERNEED: name = "VERNEED"; break;
   1817 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1818 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
   1819 	    case DT_USED: name = "USED"; break;
   1820 	    case DT_FILTER: name = "FILTER"; stringp = true; break;
   1821 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1822 	    }
   1823 
   1824 	  fprintf (f, "  %-20s ", name);
   1825 	  if (! stringp)
   1826 	    {
   1827 	      fprintf (f, "0x");
   1828 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1829 	    }
   1830 	  else
   1831 	    {
   1832 	      const char *string;
   1833 	      unsigned int tagv = dyn.d_un.d_val;
   1834 
   1835 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1836 	      if (string == NULL)
   1837 		goto error_return;
   1838 	      fprintf (f, "%s", string);
   1839 	    }
   1840 	  fprintf (f, "\n");
   1841 	}
   1842 
   1843       free (dynbuf);
   1844       dynbuf = NULL;
   1845     }
   1846 
   1847   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1848       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1849     {
   1850       if (! _bfd_elf_slurp_version_tables (abfd, false))
   1851 	return false;
   1852     }
   1853 
   1854   if (elf_dynverdef (abfd) != 0)
   1855     {
   1856       Elf_Internal_Verdef *t;
   1857 
   1858       fprintf (f, _("\nVersion definitions:\n"));
   1859       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1860 	{
   1861 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1862 		   t->vd_flags, t->vd_hash,
   1863 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1864 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1865 	    {
   1866 	      Elf_Internal_Verdaux *a;
   1867 
   1868 	      fprintf (f, "\t");
   1869 	      for (a = t->vd_auxptr->vda_nextptr;
   1870 		   a != NULL;
   1871 		   a = a->vda_nextptr)
   1872 		fprintf (f, "%s ",
   1873 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1874 	      fprintf (f, "\n");
   1875 	    }
   1876 	}
   1877     }
   1878 
   1879   if (elf_dynverref (abfd) != 0)
   1880     {
   1881       Elf_Internal_Verneed *t;
   1882 
   1883       fprintf (f, _("\nVersion References:\n"));
   1884       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1885 	{
   1886 	  Elf_Internal_Vernaux *a;
   1887 
   1888 	  fprintf (f, _("  required from %s:\n"),
   1889 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1890 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1891 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1892 		     a->vna_flags, a->vna_other,
   1893 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1894 	}
   1895     }
   1896 
   1897   return true;
   1898 
   1899  error_return:
   1900   free (dynbuf);
   1901   return false;
   1902 }
   1903 
   1904 /* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
   1905    and return symbol version for symbol version itself.   */
   1906 
   1907 const char *
   1908 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   1909 				    bool base_p,
   1910 				    bool *hidden)
   1911 {
   1912   const char *version_string = NULL;
   1913   if (elf_dynversym (abfd) != 0
   1914       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   1915     {
   1916       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   1917 
   1918       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   1919       vernum &= VERSYM_VERSION;
   1920 
   1921       if (vernum == 0)
   1922 	version_string = "";
   1923       else if (vernum == 1
   1924 	       && (vernum > elf_tdata (abfd)->cverdefs
   1925 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   1926 		       == VER_FLG_BASE)))
   1927 	version_string = base_p ? "Base" : "";
   1928       else if (vernum <= elf_tdata (abfd)->cverdefs)
   1929 	{
   1930 	  const char *nodename
   1931 	    = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   1932 	  version_string = "";
   1933 	  if (base_p
   1934 	      || nodename == NULL
   1935 	      || symbol->name == NULL
   1936 	      || strcmp (symbol->name, nodename) != 0)
   1937 	    version_string = nodename;
   1938 	}
   1939       else
   1940 	{
   1941 	  Elf_Internal_Verneed *t;
   1942 
   1943 	  version_string = _("<corrupt>");
   1944 	  for (t = elf_tdata (abfd)->verref;
   1945 	       t != NULL;
   1946 	       t = t->vn_nextref)
   1947 	    {
   1948 	      Elf_Internal_Vernaux *a;
   1949 
   1950 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1951 		{
   1952 		  if (a->vna_other == vernum)
   1953 		    {
   1954 		      *hidden = true;
   1955 		      version_string = a->vna_nodename;
   1956 		      break;
   1957 		    }
   1958 		}
   1959 	    }
   1960 	}
   1961     }
   1962   return version_string;
   1963 }
   1964 
   1965 /* Display ELF-specific fields of a symbol.  */
   1966 
   1967 void
   1968 bfd_elf_print_symbol (bfd *abfd,
   1969 		      void *filep,
   1970 		      asymbol *symbol,
   1971 		      bfd_print_symbol_type how)
   1972 {
   1973   FILE *file = (FILE *) filep;
   1974   switch (how)
   1975     {
   1976     case bfd_print_symbol_name:
   1977       fprintf (file, "%s", symbol->name);
   1978       break;
   1979     case bfd_print_symbol_more:
   1980       fprintf (file, "elf ");
   1981       bfd_fprintf_vma (abfd, file, symbol->value);
   1982       fprintf (file, " %x", symbol->flags);
   1983       break;
   1984     case bfd_print_symbol_all:
   1985       {
   1986 	const char *section_name;
   1987 	const char *name = NULL;
   1988 	const struct elf_backend_data *bed;
   1989 	unsigned char st_other;
   1990 	bfd_vma val;
   1991 	const char *version_string;
   1992 	bool hidden;
   1993 
   1994 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   1995 
   1996 	bed = get_elf_backend_data (abfd);
   1997 	if (bed->elf_backend_print_symbol_all)
   1998 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   1999 
   2000 	if (name == NULL)
   2001 	  {
   2002 	    name = symbol->name;
   2003 	    bfd_print_symbol_vandf (abfd, file, symbol);
   2004 	  }
   2005 
   2006 	fprintf (file, " %s\t", section_name);
   2007 	/* Print the "other" value for a symbol.  For common symbols,
   2008 	   we've already printed the size; now print the alignment.
   2009 	   For other symbols, we have no specified alignment, and
   2010 	   we've printed the address; now print the size.  */
   2011 	if (symbol->section && bfd_is_com_section (symbol->section))
   2012 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   2013 	else
   2014 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   2015 	bfd_fprintf_vma (abfd, file, val);
   2016 
   2017 	/* If we have version information, print it.  */
   2018 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2019 							     symbol,
   2020 							     true,
   2021 							     &hidden);
   2022 	if (version_string)
   2023 	  {
   2024 	    if (!hidden)
   2025 	      fprintf (file, "  %-11s", version_string);
   2026 	    else
   2027 	      {
   2028 		int i;
   2029 
   2030 		fprintf (file, " (%s)", version_string);
   2031 		for (i = 10 - strlen (version_string); i > 0; --i)
   2032 		  putc (' ', file);
   2033 	      }
   2034 	  }
   2035 
   2036 	/* If the st_other field is not zero, print it.  */
   2037 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2038 
   2039 	switch (st_other)
   2040 	  {
   2041 	  case 0: break;
   2042 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2043 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2044 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2045 	  default:
   2046 	    /* Some other non-defined flags are also present, so print
   2047 	       everything hex.  */
   2048 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2049 	  }
   2050 
   2051 	fprintf (file, " %s", name);
   2052       }
   2053       break;
   2054     }
   2055 }
   2056 
   2057 /* ELF .o/exec file reading */
   2059 
   2060 /* Create a new bfd section from an ELF section header.  */
   2061 
   2062 bool
   2063 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2064 {
   2065   Elf_Internal_Shdr *hdr;
   2066   Elf_Internal_Ehdr *ehdr;
   2067   const struct elf_backend_data *bed;
   2068   const char *name;
   2069   bool ret = true;
   2070 
   2071   if (shindex >= elf_numsections (abfd))
   2072     return false;
   2073 
   2074   /* PR17512: A corrupt ELF binary might contain a loop of sections via
   2075      sh_link or sh_info.  Detect this here, by refusing to load a
   2076      section that we are already in the process of loading.  */
   2077   if (elf_tdata (abfd)->being_created[shindex])
   2078     {
   2079       _bfd_error_handler
   2080 	(_("%pB: warning: loop in section dependencies detected"), abfd);
   2081       return false;
   2082     }
   2083   elf_tdata (abfd)->being_created[shindex] = true;
   2084 
   2085   hdr = elf_elfsections (abfd)[shindex];
   2086   ehdr = elf_elfheader (abfd);
   2087   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2088 					  hdr->sh_name);
   2089   if (name == NULL)
   2090     goto fail;
   2091 
   2092   bed = get_elf_backend_data (abfd);
   2093   switch (hdr->sh_type)
   2094     {
   2095     case SHT_NULL:
   2096       /* Inactive section. Throw it away.  */
   2097       goto success;
   2098 
   2099     case SHT_PROGBITS:		/* Normal section with contents.  */
   2100     case SHT_NOBITS:		/* .bss section.  */
   2101     case SHT_HASH:		/* .hash section.  */
   2102     case SHT_NOTE:		/* .note section.  */
   2103     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2104     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2105     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2106     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2107     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2108       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2109       goto success;
   2110 
   2111     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2112       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2113 	goto fail;
   2114 
   2115       if (hdr->sh_link > elf_numsections (abfd))
   2116 	{
   2117 	  /* PR 10478: Accept Solaris binaries with a sh_link
   2118 	     field set to SHN_BEFORE or SHN_AFTER.  */
   2119 	  switch (bfd_get_arch (abfd))
   2120 	    {
   2121 	    case bfd_arch_i386:
   2122 	    case bfd_arch_sparc:
   2123 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
   2124 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
   2125 		break;
   2126 	      /* Otherwise fall through.  */
   2127 	    default:
   2128 	      goto fail;
   2129 	    }
   2130 	}
   2131       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2132 	goto fail;
   2133       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2134 	{
   2135 	  Elf_Internal_Shdr *dynsymhdr;
   2136 
   2137 	  /* The shared libraries distributed with hpux11 have a bogus
   2138 	     sh_link field for the ".dynamic" section.  Find the
   2139 	     string table for the ".dynsym" section instead.  */
   2140 	  if (elf_dynsymtab (abfd) != 0)
   2141 	    {
   2142 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2143 	      hdr->sh_link = dynsymhdr->sh_link;
   2144 	    }
   2145 	  else
   2146 	    {
   2147 	      unsigned int i, num_sec;
   2148 
   2149 	      num_sec = elf_numsections (abfd);
   2150 	      for (i = 1; i < num_sec; i++)
   2151 		{
   2152 		  dynsymhdr = elf_elfsections (abfd)[i];
   2153 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2154 		    {
   2155 		      hdr->sh_link = dynsymhdr->sh_link;
   2156 		      break;
   2157 		    }
   2158 		}
   2159 	    }
   2160 	}
   2161       goto success;
   2162 
   2163     case SHT_SYMTAB:		/* A symbol table.  */
   2164       if (elf_onesymtab (abfd) == shindex)
   2165 	goto success;
   2166 
   2167       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2168 	goto fail;
   2169 
   2170       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2171 	{
   2172 	  if (hdr->sh_size != 0)
   2173 	    goto fail;
   2174 	  /* Some assemblers erroneously set sh_info to one with a
   2175 	     zero sh_size.  ld sees this as a global symbol count
   2176 	     of (unsigned) -1.  Fix it here.  */
   2177 	  hdr->sh_info = 0;
   2178 	  goto success;
   2179 	}
   2180 
   2181       /* PR 18854: A binary might contain more than one symbol table.
   2182 	 Unusual, but possible.  Warn, but continue.  */
   2183       if (elf_onesymtab (abfd) != 0)
   2184 	{
   2185 	  _bfd_error_handler
   2186 	    /* xgettext:c-format */
   2187 	    (_("%pB: warning: multiple symbol tables detected"
   2188 	       " - ignoring the table in section %u"),
   2189 	     abfd, shindex);
   2190 	  goto success;
   2191 	}
   2192       elf_onesymtab (abfd) = shindex;
   2193       elf_symtab_hdr (abfd) = *hdr;
   2194       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2195       abfd->flags |= HAS_SYMS;
   2196 
   2197       /* Sometimes a shared object will map in the symbol table.  If
   2198 	 SHF_ALLOC is set, and this is a shared object, then we also
   2199 	 treat this section as a BFD section.  We can not base the
   2200 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2201 	 set in a relocatable object file, which would confuse the
   2202 	 linker.  */
   2203       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2204 	  && (abfd->flags & DYNAMIC) != 0
   2205 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2206 						shindex))
   2207 	goto fail;
   2208 
   2209       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2210 	 can't read symbols without that section loaded as well.  It
   2211 	 is most likely specified by the next section header.  */
   2212       {
   2213 	elf_section_list * entry;
   2214 	unsigned int i, num_sec;
   2215 
   2216 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2217 	  if (entry->hdr.sh_link == shindex)
   2218 	    goto success;
   2219 
   2220 	num_sec = elf_numsections (abfd);
   2221 	for (i = shindex + 1; i < num_sec; i++)
   2222 	  {
   2223 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2224 
   2225 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2226 		&& hdr2->sh_link == shindex)
   2227 	      break;
   2228 	  }
   2229 
   2230 	if (i == num_sec)
   2231 	  for (i = 1; i < shindex; i++)
   2232 	    {
   2233 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2234 
   2235 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2236 		  && hdr2->sh_link == shindex)
   2237 		break;
   2238 	    }
   2239 
   2240 	if (i != shindex)
   2241 	  ret = bfd_section_from_shdr (abfd, i);
   2242 	/* else FIXME: we have failed to find the symbol table - should we issue an error ? */
   2243 	goto success;
   2244       }
   2245 
   2246     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2247       if (elf_dynsymtab (abfd) == shindex)
   2248 	goto success;
   2249 
   2250       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2251 	goto fail;
   2252 
   2253       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2254 	{
   2255 	  if (hdr->sh_size != 0)
   2256 	    goto fail;
   2257 
   2258 	  /* Some linkers erroneously set sh_info to one with a
   2259 	     zero sh_size.  ld sees this as a global symbol count
   2260 	     of (unsigned) -1.  Fix it here.  */
   2261 	  hdr->sh_info = 0;
   2262 	  goto success;
   2263 	}
   2264 
   2265       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2266 	 Unusual, but possible.  Warn, but continue.  */
   2267       if (elf_dynsymtab (abfd) != 0)
   2268 	{
   2269 	  _bfd_error_handler
   2270 	    /* xgettext:c-format */
   2271 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2272 	       " - ignoring the table in section %u"),
   2273 	     abfd, shindex);
   2274 	  goto success;
   2275 	}
   2276       elf_dynsymtab (abfd) = shindex;
   2277       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2278       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2279       abfd->flags |= HAS_SYMS;
   2280 
   2281       /* Besides being a symbol table, we also treat this as a regular
   2282 	 section, so that objcopy can handle it.  */
   2283       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2284       goto success;
   2285 
   2286     case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections.  */
   2287       {
   2288 	elf_section_list * entry;
   2289 
   2290 	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
   2291 	  if (entry->ndx == shindex)
   2292 	    goto success;
   2293 
   2294 	entry = bfd_alloc (abfd, sizeof (*entry));
   2295 	if (entry == NULL)
   2296 	  goto fail;
   2297 	entry->ndx = shindex;
   2298 	entry->hdr = * hdr;
   2299 	entry->next = elf_symtab_shndx_list (abfd);
   2300 	elf_symtab_shndx_list (abfd) = entry;
   2301 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2302 	goto success;
   2303       }
   2304 
   2305     case SHT_STRTAB:		/* A string table.  */
   2306       if (hdr->bfd_section != NULL)
   2307 	goto success;
   2308 
   2309       if (ehdr->e_shstrndx == shindex)
   2310 	{
   2311 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2312 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2313 	  goto success;
   2314 	}
   2315 
   2316       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2317 	{
   2318 	symtab_strtab:
   2319 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2320 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2321 	  goto success;
   2322 	}
   2323 
   2324       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2325 	{
   2326 	dynsymtab_strtab:
   2327 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2328 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2329 	  elf_elfsections (abfd)[shindex] = hdr;
   2330 	  /* We also treat this as a regular section, so that objcopy
   2331 	     can handle it.  */
   2332 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2333 						 shindex);
   2334 	  goto success;
   2335 	}
   2336 
   2337       /* If the string table isn't one of the above, then treat it as a
   2338 	 regular section.  We need to scan all the headers to be sure,
   2339 	 just in case this strtab section appeared before the above.  */
   2340       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2341 	{
   2342 	  unsigned int i, num_sec;
   2343 
   2344 	  num_sec = elf_numsections (abfd);
   2345 	  for (i = 1; i < num_sec; i++)
   2346 	    {
   2347 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2348 	      if (hdr2->sh_link == shindex)
   2349 		{
   2350 		  /* Prevent endless recursion on broken objects.  */
   2351 		  if (i == shindex)
   2352 		    goto fail;
   2353 		  if (! bfd_section_from_shdr (abfd, i))
   2354 		    goto fail;
   2355 		  if (elf_onesymtab (abfd) == i)
   2356 		    goto symtab_strtab;
   2357 		  if (elf_dynsymtab (abfd) == i)
   2358 		    goto dynsymtab_strtab;
   2359 		}
   2360 	    }
   2361 	}
   2362       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2363       goto success;
   2364 
   2365     case SHT_REL:
   2366     case SHT_RELA:
   2367     case SHT_RELR:
   2368       /* *These* do a lot of work -- but build no sections!  */
   2369       {
   2370 	asection *target_sect;
   2371 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2372 	unsigned int num_sec = elf_numsections (abfd);
   2373 	struct bfd_elf_section_data *esdt;
   2374 	bfd_size_type size;
   2375 
   2376 	if (hdr->sh_type == SHT_REL)
   2377 	  size = bed->s->sizeof_rel;
   2378 	else if (hdr->sh_type == SHT_RELA)
   2379 	  size = bed->s->sizeof_rela;
   2380 	else
   2381 	  size = bed->s->arch_size / 8;
   2382 	if (hdr->sh_entsize != size)
   2383 	  goto fail;
   2384 
   2385 	/* Check for a bogus link to avoid crashing.  */
   2386 	if (hdr->sh_link >= num_sec)
   2387 	  {
   2388 	    _bfd_error_handler
   2389 	      /* xgettext:c-format */
   2390 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2391 	       abfd, hdr->sh_link, name, shindex);
   2392 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2393 						   shindex);
   2394 	    goto success;
   2395 	  }
   2396 
   2397 	/* Get the symbol table.  */
   2398 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2399 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2400 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2401 	  goto fail;
   2402 
   2403 	/* If this is an alloc section in an executable or shared
   2404 	   library, or the reloc section does not use the main symbol
   2405 	   table we don't treat it as a reloc section.  BFD can't
   2406 	   adequately represent such a section, so at least for now,
   2407 	   we don't try.  We just present it as a normal section.  We
   2408 	   also can't use it as a reloc section if it points to the
   2409 	   null section, an invalid section, another reloc section, or
   2410 	   its sh_link points to the null section.  */
   2411 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2412 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2413 	    || hdr->sh_link == SHN_UNDEF
   2414 	    || hdr->sh_link != elf_onesymtab (abfd)
   2415 	    || hdr->sh_info == SHN_UNDEF
   2416 	    || hdr->sh_info >= num_sec
   2417 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2418 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2419 	  {
   2420 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2421 						   shindex);
   2422 	    goto success;
   2423 	  }
   2424 
   2425 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2426 	  goto fail;
   2427 
   2428 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2429 	if (target_sect == NULL)
   2430 	  goto fail;
   2431 
   2432 	esdt = elf_section_data (target_sect);
   2433 	if (hdr->sh_type == SHT_RELA)
   2434 	  p_hdr = &esdt->rela.hdr;
   2435 	else
   2436 	  p_hdr = &esdt->rel.hdr;
   2437 
   2438 	/* PR 17512: file: 0b4f81b7.
   2439 	   Also see PR 24456, for a file which deliberately has two reloc
   2440 	   sections.  */
   2441 	if (*p_hdr != NULL)
   2442 	  {
   2443 	    if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
   2444 	      {
   2445 		_bfd_error_handler
   2446 		  /* xgettext:c-format */
   2447 		  (_("%pB: warning: secondary relocation section '%s' "
   2448 		     "for section %pA found - ignoring"),
   2449 		   abfd, name, target_sect);
   2450 	      }
   2451 	    else
   2452 	      esdt->has_secondary_relocs = true;
   2453 	    goto success;
   2454 	  }
   2455 
   2456 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2457 	if (hdr2 == NULL)
   2458 	  goto fail;
   2459 	*hdr2 = *hdr;
   2460 	*p_hdr = hdr2;
   2461 	elf_elfsections (abfd)[shindex] = hdr2;
   2462 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2463 				     * bed->s->int_rels_per_ext_rel);
   2464 	target_sect->flags |= SEC_RELOC;
   2465 	target_sect->relocation = NULL;
   2466 	target_sect->rel_filepos = hdr->sh_offset;
   2467 	/* In the section to which the relocations apply, mark whether
   2468 	   its relocations are of the REL or RELA variety.  */
   2469 	if (hdr->sh_size != 0)
   2470 	  {
   2471 	    if (hdr->sh_type == SHT_RELA)
   2472 	      target_sect->use_rela_p = 1;
   2473 	  }
   2474 	abfd->flags |= HAS_RELOC;
   2475 	goto success;
   2476       }
   2477 
   2478     case SHT_GNU_verdef:
   2479       elf_dynverdef (abfd) = shindex;
   2480       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2481       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2482       goto success;
   2483 
   2484     case SHT_GNU_versym:
   2485       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2486 	goto fail;
   2487 
   2488       elf_dynversym (abfd) = shindex;
   2489       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2490       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2491       goto success;
   2492 
   2493     case SHT_GNU_verneed:
   2494       elf_dynverref (abfd) = shindex;
   2495       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2496       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2497       goto success;
   2498 
   2499     case SHT_SHLIB:
   2500       goto success;
   2501 
   2502     case SHT_GROUP:
   2503       if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
   2504 	goto fail;
   2505 
   2506       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2507 	goto fail;
   2508 
   2509       goto success;
   2510 
   2511     default:
   2512       /* Possibly an attributes section.  */
   2513       if (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2514 	  || hdr->sh_type == bed->obj_attrs_section_type)
   2515 	{
   2516 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2517 	    goto fail;
   2518 	  _bfd_elf_parse_attributes (abfd, hdr);
   2519 	  goto success;
   2520 	}
   2521 
   2522       /* Check for any processor-specific section types.  */
   2523       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2524 	goto success;
   2525 
   2526       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2527 	{
   2528 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2529 	    /* FIXME: How to properly handle allocated section reserved
   2530 	       for applications?  */
   2531 	    _bfd_error_handler
   2532 	      /* xgettext:c-format */
   2533 	      (_("%pB: unknown type [%#x] section `%s'"),
   2534 	       abfd, hdr->sh_type, name);
   2535 	  else
   2536 	    {
   2537 	      /* Allow sections reserved for applications.  */
   2538 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2539 						     shindex);
   2540 	      goto success;
   2541 	    }
   2542 	}
   2543       else if (hdr->sh_type >= SHT_LOPROC
   2544 	       && hdr->sh_type <= SHT_HIPROC)
   2545 	/* FIXME: We should handle this section.  */
   2546 	_bfd_error_handler
   2547 	  /* xgettext:c-format */
   2548 	  (_("%pB: unknown type [%#x] section `%s'"),
   2549 	   abfd, hdr->sh_type, name);
   2550       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2551 	{
   2552 	  /* Unrecognised OS-specific sections.  */
   2553 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2554 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2555 	       required to correctly process the section and the file should
   2556 	       be rejected with an error message.  */
   2557 	    _bfd_error_handler
   2558 	      /* xgettext:c-format */
   2559 	      (_("%pB: unknown type [%#x] section `%s'"),
   2560 	       abfd, hdr->sh_type, name);
   2561 	  else
   2562 	    {
   2563 	      /* Otherwise it should be processed.  */
   2564 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2565 	      goto success;
   2566 	    }
   2567 	}
   2568       else
   2569 	/* FIXME: We should handle this section.  */
   2570 	_bfd_error_handler
   2571 	  /* xgettext:c-format */
   2572 	  (_("%pB: unknown type [%#x] section `%s'"),
   2573 	   abfd, hdr->sh_type, name);
   2574 
   2575       goto fail;
   2576     }
   2577 
   2578  fail:
   2579   ret = false;
   2580  success:
   2581   elf_tdata (abfd)->being_created[shindex] = false;
   2582   return ret;
   2583 }
   2584 
   2585 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   2586 
   2587 Elf_Internal_Sym *
   2588 bfd_sym_from_r_symndx (struct sym_cache *cache,
   2589 		       bfd *abfd,
   2590 		       unsigned long r_symndx)
   2591 {
   2592   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   2593 
   2594   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   2595     {
   2596       Elf_Internal_Shdr *symtab_hdr;
   2597       unsigned char esym[sizeof (Elf64_External_Sym)];
   2598       Elf_External_Sym_Shndx eshndx;
   2599 
   2600       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2601       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   2602 				&cache->sym[ent], esym, &eshndx) == NULL)
   2603 	return NULL;
   2604 
   2605       if (cache->abfd != abfd)
   2606 	{
   2607 	  memset (cache->indx, -1, sizeof (cache->indx));
   2608 	  cache->abfd = abfd;
   2609 	}
   2610       cache->indx[ent] = r_symndx;
   2611     }
   2612 
   2613   return &cache->sym[ent];
   2614 }
   2615 
   2616 /* Given an ELF section number, retrieve the corresponding BFD
   2617    section.  */
   2618 
   2619 asection *
   2620 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   2621 {
   2622   if (sec_index >= elf_numsections (abfd))
   2623     return NULL;
   2624   return elf_elfsections (abfd)[sec_index]->bfd_section;
   2625 }
   2626 
   2627 static const struct bfd_elf_special_section special_sections_b[] =
   2628 {
   2629   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2630   { NULL,		    0,	0, 0,		 0 }
   2631 };
   2632 
   2633 static const struct bfd_elf_special_section special_sections_c[] =
   2634 {
   2635   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   2636   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   2637   { NULL,			0, 0, 0,	    0 }
   2638 };
   2639 
   2640 static const struct bfd_elf_special_section special_sections_d[] =
   2641 {
   2642   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2643   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2644   /* There are more DWARF sections than these, but they needn't be added here
   2645      unless you have to cope with broken compilers that don't emit section
   2646      attributes or you want to help the user writing assembler.  */
   2647   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   2648   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   2649   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   2650   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   2651   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   2652   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   2653   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   2654   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   2655   { NULL,		       0,	 0, 0,		  0 }
   2656 };
   2657 
   2658 static const struct bfd_elf_special_section special_sections_f[] =
   2659 {
   2660   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2661   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   2662   { NULL,			   0 , 0, 0,		  0 }
   2663 };
   2664 
   2665 static const struct bfd_elf_special_section special_sections_g[] =
   2666 {
   2667   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2668   { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   2669   { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2670   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   2671   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   2672   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   2673   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   2674   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   2675   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   2676   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   2677   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   2678   { NULL,			 0,	   0, 0,	       0 }
   2679 };
   2680 
   2681 static const struct bfd_elf_special_section special_sections_h[] =
   2682 {
   2683   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   2684   { NULL,		     0, 0, 0,		 0 }
   2685 };
   2686 
   2687 static const struct bfd_elf_special_section special_sections_i[] =
   2688 {
   2689   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   2690   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2691   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   2692   { NULL,		       0,      0, 0,		  0 }
   2693 };
   2694 
   2695 static const struct bfd_elf_special_section special_sections_l[] =
   2696 {
   2697   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   2698   { NULL,		     0, 0, 0,		 0 }
   2699 };
   2700 
   2701 static const struct bfd_elf_special_section special_sections_n[] =
   2702 {
   2703   { STRING_COMMA_LEN (".noinit"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2704   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   2705   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   2706   { NULL,		     0,		  0, 0,		   0 }
   2707 };
   2708 
   2709 static const struct bfd_elf_special_section special_sections_p[] =
   2710 {
   2711   { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,	SHF_ALLOC + SHF_WRITE },
   2712   { STRING_COMMA_LEN (".persistent"),	 -2, SHT_PROGBITS,	SHF_ALLOC + SHF_WRITE },
   2713   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   2714   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   2715   { NULL,		    0,		  0, 0,			0 }
   2716 };
   2717 
   2718 static const struct bfd_elf_special_section special_sections_r[] =
   2719 {
   2720   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   2721   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   2722   { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
   2723   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   2724   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   2725   { NULL,		    0,	   0, 0,	    0 }
   2726 };
   2727 
   2728 static const struct bfd_elf_special_section special_sections_s[] =
   2729 {
   2730   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   2731   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   2732   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   2733   /* See struct bfd_elf_special_section declaration for the semantics of
   2734      this special case where .prefix_length != strlen (.prefix).  */
   2735   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   2736   { NULL,			0,  0, 0,	   0 }
   2737 };
   2738 
   2739 static const struct bfd_elf_special_section special_sections_t[] =
   2740 {
   2741   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   2742   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2743   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   2744   { NULL,		      0,  0, 0,		   0 }
   2745 };
   2746 
   2747 static const struct bfd_elf_special_section special_sections_z[] =
   2748 {
   2749   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   2750   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   2751   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   2752   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   2753   { NULL,		      0,  0, 0,		   0 }
   2754 };
   2755 
   2756 static const struct bfd_elf_special_section * const special_sections[] =
   2757 {
   2758   special_sections_b,		/* 'b' */
   2759   special_sections_c,		/* 'c' */
   2760   special_sections_d,		/* 'd' */
   2761   NULL,				/* 'e' */
   2762   special_sections_f,		/* 'f' */
   2763   special_sections_g,		/* 'g' */
   2764   special_sections_h,		/* 'h' */
   2765   special_sections_i,		/* 'i' */
   2766   NULL,				/* 'j' */
   2767   NULL,				/* 'k' */
   2768   special_sections_l,		/* 'l' */
   2769   NULL,				/* 'm' */
   2770   special_sections_n,		/* 'n' */
   2771   NULL,				/* 'o' */
   2772   special_sections_p,		/* 'p' */
   2773   NULL,				/* 'q' */
   2774   special_sections_r,		/* 'r' */
   2775   special_sections_s,		/* 's' */
   2776   special_sections_t,		/* 't' */
   2777   NULL,				/* 'u' */
   2778   NULL,				/* 'v' */
   2779   NULL,				/* 'w' */
   2780   NULL,				/* 'x' */
   2781   NULL,				/* 'y' */
   2782   special_sections_z		/* 'z' */
   2783 };
   2784 
   2785 const struct bfd_elf_special_section *
   2786 _bfd_elf_get_special_section (const char *name,
   2787 			      const struct bfd_elf_special_section *spec,
   2788 			      unsigned int rela)
   2789 {
   2790   int i;
   2791   int len;
   2792 
   2793   len = strlen (name);
   2794 
   2795   for (i = 0; spec[i].prefix != NULL; i++)
   2796     {
   2797       int suffix_len;
   2798       int prefix_len = spec[i].prefix_length;
   2799 
   2800       if (len < prefix_len)
   2801 	continue;
   2802       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   2803 	continue;
   2804 
   2805       suffix_len = spec[i].suffix_length;
   2806       if (suffix_len <= 0)
   2807 	{
   2808 	  if (name[prefix_len] != 0)
   2809 	    {
   2810 	      if (suffix_len == 0)
   2811 		continue;
   2812 	      if (name[prefix_len] != '.'
   2813 		  && (suffix_len == -2
   2814 		      || (rela && spec[i].type == SHT_REL)))
   2815 		continue;
   2816 	    }
   2817 	}
   2818       else
   2819 	{
   2820 	  if (len < prefix_len + suffix_len)
   2821 	    continue;
   2822 	  if (memcmp (name + len - suffix_len,
   2823 		      spec[i].prefix + prefix_len,
   2824 		      suffix_len) != 0)
   2825 	    continue;
   2826 	}
   2827       return &spec[i];
   2828     }
   2829 
   2830   return NULL;
   2831 }
   2832 
   2833 const struct bfd_elf_special_section *
   2834 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   2835 {
   2836   int i;
   2837   const struct bfd_elf_special_section *spec;
   2838   const struct elf_backend_data *bed;
   2839 
   2840   /* See if this is one of the special sections.  */
   2841   if (sec->name == NULL)
   2842     return NULL;
   2843 
   2844   bed = get_elf_backend_data (abfd);
   2845   spec = bed->special_sections;
   2846   if (spec)
   2847     {
   2848       spec = _bfd_elf_get_special_section (sec->name,
   2849 					   bed->special_sections,
   2850 					   sec->use_rela_p);
   2851       if (spec != NULL)
   2852 	return spec;
   2853     }
   2854 
   2855   if (sec->name[0] != '.')
   2856     return NULL;
   2857 
   2858   i = sec->name[1] - 'b';
   2859   if (i < 0 || i > 'z' - 'b')
   2860     return NULL;
   2861 
   2862   spec = special_sections[i];
   2863 
   2864   if (spec == NULL)
   2865     return NULL;
   2866 
   2867   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   2868 }
   2869 
   2870 bool
   2871 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   2872 {
   2873   struct bfd_elf_section_data *sdata;
   2874   const struct elf_backend_data *bed;
   2875   const struct bfd_elf_special_section *ssect;
   2876 
   2877   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   2878   if (sdata == NULL)
   2879     {
   2880       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   2881 							  sizeof (*sdata));
   2882       if (sdata == NULL)
   2883 	return false;
   2884       sec->used_by_bfd = sdata;
   2885     }
   2886 
   2887   /* Indicate whether or not this section should use RELA relocations.  */
   2888   bed = get_elf_backend_data (abfd);
   2889   sec->use_rela_p = bed->default_use_rela_p;
   2890 
   2891   /* Set up ELF section type and flags for newly created sections, if
   2892      there is an ABI mandated section.  */
   2893   ssect = (*bed->get_sec_type_attr) (abfd, sec);
   2894   if (ssect != NULL)
   2895     {
   2896       elf_section_type (sec) = ssect->type;
   2897       elf_section_flags (sec) = ssect->attr;
   2898     }
   2899 
   2900   return _bfd_generic_new_section_hook (abfd, sec);
   2901 }
   2902 
   2903 /* Create a new bfd section from an ELF program header.
   2904 
   2905    Since program segments have no names, we generate a synthetic name
   2906    of the form segment<NUM>, where NUM is generally the index in the
   2907    program header table.  For segments that are split (see below) we
   2908    generate the names segment<NUM>a and segment<NUM>b.
   2909 
   2910    Note that some program segments may have a file size that is different than
   2911    (less than) the memory size.  All this means is that at execution the
   2912    system must allocate the amount of memory specified by the memory size,
   2913    but only initialize it with the first "file size" bytes read from the
   2914    file.  This would occur for example, with program segments consisting
   2915    of combined data+bss.
   2916 
   2917    To handle the above situation, this routine generates TWO bfd sections
   2918    for the single program segment.  The first has the length specified by
   2919    the file size of the segment, and the second has the length specified
   2920    by the difference between the two sizes.  In effect, the segment is split
   2921    into its initialized and uninitialized parts.
   2922 
   2923  */
   2924 
   2925 bool
   2926 _bfd_elf_make_section_from_phdr (bfd *abfd,
   2927 				 Elf_Internal_Phdr *hdr,
   2928 				 int hdr_index,
   2929 				 const char *type_name)
   2930 {
   2931   asection *newsect;
   2932   char *name;
   2933   char namebuf[64];
   2934   size_t len;
   2935   int split;
   2936   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   2937 
   2938   split = ((hdr->p_memsz > 0)
   2939 	    && (hdr->p_filesz > 0)
   2940 	    && (hdr->p_memsz > hdr->p_filesz));
   2941 
   2942   if (hdr->p_filesz > 0)
   2943     {
   2944       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   2945       len = strlen (namebuf) + 1;
   2946       name = (char *) bfd_alloc (abfd, len);
   2947       if (!name)
   2948 	return false;
   2949       memcpy (name, namebuf, len);
   2950       newsect = bfd_make_section (abfd, name);
   2951       if (newsect == NULL)
   2952 	return false;
   2953       newsect->vma = hdr->p_vaddr / opb;
   2954       newsect->lma = hdr->p_paddr / opb;
   2955       newsect->size = hdr->p_filesz;
   2956       newsect->filepos = hdr->p_offset;
   2957       newsect->flags |= SEC_HAS_CONTENTS;
   2958       newsect->alignment_power = bfd_log2 (hdr->p_align);
   2959       if (hdr->p_type == PT_LOAD)
   2960 	{
   2961 	  newsect->flags |= SEC_ALLOC;
   2962 	  newsect->flags |= SEC_LOAD;
   2963 	  if (hdr->p_flags & PF_X)
   2964 	    {
   2965 	      /* FIXME: all we known is that it has execute PERMISSION,
   2966 		 may be data.  */
   2967 	      newsect->flags |= SEC_CODE;
   2968 	    }
   2969 	}
   2970       if (!(hdr->p_flags & PF_W))
   2971 	{
   2972 	  newsect->flags |= SEC_READONLY;
   2973 	}
   2974     }
   2975 
   2976   if (hdr->p_memsz > hdr->p_filesz)
   2977     {
   2978       bfd_vma align;
   2979 
   2980       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   2981       len = strlen (namebuf) + 1;
   2982       name = (char *) bfd_alloc (abfd, len);
   2983       if (!name)
   2984 	return false;
   2985       memcpy (name, namebuf, len);
   2986       newsect = bfd_make_section (abfd, name);
   2987       if (newsect == NULL)
   2988 	return false;
   2989       newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
   2990       newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
   2991       newsect->size = hdr->p_memsz - hdr->p_filesz;
   2992       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   2993       align = newsect->vma & -newsect->vma;
   2994       if (align == 0 || align > hdr->p_align)
   2995 	align = hdr->p_align;
   2996       newsect->alignment_power = bfd_log2 (align);
   2997       if (hdr->p_type == PT_LOAD)
   2998 	{
   2999 	  newsect->flags |= SEC_ALLOC;
   3000 	  if (hdr->p_flags & PF_X)
   3001 	    newsect->flags |= SEC_CODE;
   3002 	}
   3003       if (!(hdr->p_flags & PF_W))
   3004 	newsect->flags |= SEC_READONLY;
   3005     }
   3006 
   3007   return true;
   3008 }
   3009 
   3010 static bool
   3011 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   3012 {
   3013   /* The return value is ignored.  Build-ids are considered optional.  */
   3014   if (templ->xvec->flavour == bfd_target_elf_flavour)
   3015     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   3016       (templ, offset);
   3017   return false;
   3018 }
   3019 
   3020 bool
   3021 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3022 {
   3023   const struct elf_backend_data *bed;
   3024 
   3025   switch (hdr->p_type)
   3026     {
   3027     case PT_NULL:
   3028       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3029 
   3030     case PT_LOAD:
   3031       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3032 	return false;
   3033       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3034 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3035       return true;
   3036 
   3037     case PT_DYNAMIC:
   3038       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3039 
   3040     case PT_INTERP:
   3041       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3042 
   3043     case PT_NOTE:
   3044       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3045 	return false;
   3046       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3047 			    hdr->p_align))
   3048 	return false;
   3049       return true;
   3050 
   3051     case PT_SHLIB:
   3052       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3053 
   3054     case PT_PHDR:
   3055       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3056 
   3057     case PT_GNU_EH_FRAME:
   3058       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3059 					      "eh_frame_hdr");
   3060 
   3061     case PT_GNU_STACK:
   3062       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3063 
   3064     case PT_GNU_RELRO:
   3065       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3066 
   3067     default:
   3068       /* Check for any processor-specific program segment types.  */
   3069       bed = get_elf_backend_data (abfd);
   3070       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3071     }
   3072 }
   3073 
   3074 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3075    REL or RELA.  */
   3076 
   3077 Elf_Internal_Shdr *
   3078 _bfd_elf_single_rel_hdr (asection *sec)
   3079 {
   3080   if (elf_section_data (sec)->rel.hdr)
   3081     {
   3082       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3083       return elf_section_data (sec)->rel.hdr;
   3084     }
   3085   else
   3086     return elf_section_data (sec)->rela.hdr;
   3087 }
   3088 
   3089 static bool
   3090 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3091 			    Elf_Internal_Shdr *rel_hdr,
   3092 			    const char *sec_name,
   3093 			    bool use_rela_p)
   3094 {
   3095   char *name = (char *) bfd_alloc (abfd,
   3096 				   sizeof ".rela" + strlen (sec_name));
   3097   if (name == NULL)
   3098     return false;
   3099 
   3100   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3101   rel_hdr->sh_name =
   3102     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3103 					false);
   3104   if (rel_hdr->sh_name == (unsigned int) -1)
   3105     return false;
   3106 
   3107   return true;
   3108 }
   3109 
   3110 /* Allocate and initialize a section-header for a new reloc section,
   3111    containing relocations against ASECT.  It is stored in RELDATA.  If
   3112    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3113    relocations.  */
   3114 
   3115 static bool
   3116 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3117 			  struct bfd_elf_section_reloc_data *reldata,
   3118 			  const char *sec_name,
   3119 			  bool use_rela_p,
   3120 			  bool delay_st_name_p)
   3121 {
   3122   Elf_Internal_Shdr *rel_hdr;
   3123   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3124 
   3125   BFD_ASSERT (reldata->hdr == NULL);
   3126   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3127   reldata->hdr = rel_hdr;
   3128 
   3129   if (delay_st_name_p)
   3130     rel_hdr->sh_name = (unsigned int) -1;
   3131   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3132 					use_rela_p))
   3133     return false;
   3134   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3135   rel_hdr->sh_entsize = (use_rela_p
   3136 			 ? bed->s->sizeof_rela
   3137 			 : bed->s->sizeof_rel);
   3138   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3139   rel_hdr->sh_flags = 0;
   3140   rel_hdr->sh_addr = 0;
   3141   rel_hdr->sh_size = 0;
   3142   rel_hdr->sh_offset = 0;
   3143 
   3144   return true;
   3145 }
   3146 
   3147 /* Return the default section type based on the passed in section flags.  */
   3148 
   3149 int
   3150 bfd_elf_get_default_section_type (flagword flags)
   3151 {
   3152   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3153       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3154     return SHT_NOBITS;
   3155   return SHT_PROGBITS;
   3156 }
   3157 
   3158 struct fake_section_arg
   3159 {
   3160   struct bfd_link_info *link_info;
   3161   bool failed;
   3162 };
   3163 
   3164 /* Set up an ELF internal section header for a section.  */
   3165 
   3166 static void
   3167 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3168 {
   3169   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3170   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3171   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3172   Elf_Internal_Shdr *this_hdr;
   3173   unsigned int sh_type;
   3174   const char *name = asect->name;
   3175   bool delay_st_name_p = false;
   3176   bfd_vma mask;
   3177 
   3178   if (arg->failed)
   3179     {
   3180       /* We already failed; just get out of the bfd_map_over_sections
   3181 	 loop.  */
   3182       return;
   3183     }
   3184 
   3185   this_hdr = &esd->this_hdr;
   3186 
   3187   if (arg->link_info)
   3188     {
   3189       /* ld: compress DWARF debug sections with names: .debug_*.  */
   3190       if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
   3191 	  && (asect->flags & SEC_DEBUGGING)
   3192 	  && name[1] == 'd'
   3193 	  && name[6] == '_')
   3194 	{
   3195 	  /* Set SEC_ELF_COMPRESS to indicate this section should be
   3196 	     compressed.  */
   3197 	  asect->flags |= SEC_ELF_COMPRESS;
   3198 	  /* If this section will be compressed, delay adding section
   3199 	     name to section name section after it is compressed in
   3200 	     _bfd_elf_assign_file_positions_for_non_load.  */
   3201 	  delay_st_name_p = true;
   3202 	}
   3203     }
   3204   else if ((asect->flags & SEC_ELF_RENAME))
   3205     {
   3206       /* objcopy: rename output DWARF debug section.  */
   3207       if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
   3208 	{
   3209 	  /* When we decompress or compress with SHF_COMPRESSED,
   3210 	     convert section name from .zdebug_* to .debug_* if
   3211 	     needed.  */
   3212 	  if (name[1] == 'z')
   3213 	    {
   3214 	      char *new_name = convert_zdebug_to_debug (abfd, name);
   3215 	      if (new_name == NULL)
   3216 		{
   3217 		  arg->failed = true;
   3218 		  return;
   3219 		}
   3220 	      name = new_name;
   3221 	    }
   3222 	}
   3223       else if (asect->compress_status == COMPRESS_SECTION_DONE)
   3224 	{
   3225 	  /* PR binutils/18087: Compression does not always make a
   3226 	     section smaller.  So only rename the section when
   3227 	     compression has actually taken place.  If input section
   3228 	     name is .zdebug_*, we should never compress it again.  */
   3229 	  char *new_name = convert_debug_to_zdebug (abfd, name);
   3230 	  if (new_name == NULL)
   3231 	    {
   3232 	      arg->failed = true;
   3233 	      return;
   3234 	    }
   3235 	  BFD_ASSERT (name[1] != 'z');
   3236 	  name = new_name;
   3237 	}
   3238     }
   3239 
   3240   if (delay_st_name_p)
   3241     this_hdr->sh_name = (unsigned int) -1;
   3242   else
   3243     {
   3244       this_hdr->sh_name
   3245 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3246 					      name, false);
   3247       if (this_hdr->sh_name == (unsigned int) -1)
   3248 	{
   3249 	  arg->failed = true;
   3250 	  return;
   3251 	}
   3252     }
   3253 
   3254   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3255 
   3256   if ((asect->flags & SEC_ALLOC) != 0
   3257       || asect->user_set_vma)
   3258     this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
   3259   else
   3260     this_hdr->sh_addr = 0;
   3261 
   3262   this_hdr->sh_offset = 0;
   3263   this_hdr->sh_size = asect->size;
   3264   this_hdr->sh_link = 0;
   3265   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3266   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3267     {
   3268       _bfd_error_handler
   3269 	/* xgettext:c-format */
   3270 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3271 	 abfd, asect->alignment_power, asect);
   3272       arg->failed = true;
   3273       return;
   3274     }
   3275   /* Set sh_addralign to the highest power of two given by alignment
   3276      consistent with the section VMA.  Linker scripts can force VMA.  */
   3277   mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
   3278   this_hdr->sh_addralign = mask & -mask;
   3279   /* The sh_entsize and sh_info fields may have been set already by
   3280      copy_private_section_data.  */
   3281 
   3282   this_hdr->bfd_section = asect;
   3283   this_hdr->contents = NULL;
   3284 
   3285   /* If the section type is unspecified, we set it based on
   3286      asect->flags.  */
   3287   if (asect->type != 0)
   3288     sh_type = asect->type;
   3289   else if ((asect->flags & SEC_GROUP) != 0)
   3290     sh_type = SHT_GROUP;
   3291   else
   3292     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3293 
   3294   if (this_hdr->sh_type == SHT_NULL)
   3295     this_hdr->sh_type = sh_type;
   3296   else if (this_hdr->sh_type == SHT_NOBITS
   3297 	   && sh_type == SHT_PROGBITS
   3298 	   && (asect->flags & SEC_ALLOC) != 0)
   3299     {
   3300       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3301 	 allow the link to proceed.  This can happen when users link
   3302 	 non-bss input sections to bss output sections, or emit data
   3303 	 to a bss output section via a linker script.  */
   3304       _bfd_error_handler
   3305 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3306       this_hdr->sh_type = sh_type;
   3307     }
   3308 
   3309   switch (this_hdr->sh_type)
   3310     {
   3311     default:
   3312       break;
   3313 
   3314     case SHT_STRTAB:
   3315     case SHT_NOTE:
   3316     case SHT_NOBITS:
   3317     case SHT_PROGBITS:
   3318       break;
   3319 
   3320     case SHT_INIT_ARRAY:
   3321     case SHT_FINI_ARRAY:
   3322     case SHT_PREINIT_ARRAY:
   3323       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3324       break;
   3325 
   3326     case SHT_HASH:
   3327       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3328       break;
   3329 
   3330     case SHT_DYNSYM:
   3331       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3332       break;
   3333 
   3334     case SHT_DYNAMIC:
   3335       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3336       break;
   3337 
   3338     case SHT_RELA:
   3339       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3340 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3341       break;
   3342 
   3343      case SHT_REL:
   3344       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3345 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3346       break;
   3347 
   3348      case SHT_GNU_versym:
   3349       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3350       break;
   3351 
   3352      case SHT_GNU_verdef:
   3353       this_hdr->sh_entsize = 0;
   3354       /* objcopy or strip will copy over sh_info, but may not set
   3355 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3356 	 zero.  */
   3357       if (this_hdr->sh_info == 0)
   3358 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3359       else
   3360 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3361 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3362       break;
   3363 
   3364     case SHT_GNU_verneed:
   3365       this_hdr->sh_entsize = 0;
   3366       /* objcopy or strip will copy over sh_info, but may not set
   3367 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3368 	 zero.  */
   3369       if (this_hdr->sh_info == 0)
   3370 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3371       else
   3372 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3373 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3374       break;
   3375 
   3376     case SHT_GROUP:
   3377       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3378       break;
   3379 
   3380     case SHT_GNU_HASH:
   3381       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3382       break;
   3383     }
   3384 
   3385   if ((asect->flags & SEC_ALLOC) != 0)
   3386     this_hdr->sh_flags |= SHF_ALLOC;
   3387   if ((asect->flags & SEC_READONLY) == 0)
   3388     this_hdr->sh_flags |= SHF_WRITE;
   3389   if ((asect->flags & SEC_CODE) != 0)
   3390     this_hdr->sh_flags |= SHF_EXECINSTR;
   3391   if ((asect->flags & SEC_MERGE) != 0)
   3392     {
   3393       this_hdr->sh_flags |= SHF_MERGE;
   3394       this_hdr->sh_entsize = asect->entsize;
   3395     }
   3396   if ((asect->flags & SEC_STRINGS) != 0)
   3397     this_hdr->sh_flags |= SHF_STRINGS;
   3398   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3399     this_hdr->sh_flags |= SHF_GROUP;
   3400   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3401     {
   3402       this_hdr->sh_flags |= SHF_TLS;
   3403       if (asect->size == 0
   3404 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3405 	{
   3406 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3407 
   3408 	  this_hdr->sh_size = 0;
   3409 	  if (o != NULL)
   3410 	    {
   3411 	      this_hdr->sh_size = o->offset + o->size;
   3412 	      if (this_hdr->sh_size != 0)
   3413 		this_hdr->sh_type = SHT_NOBITS;
   3414 	    }
   3415 	}
   3416     }
   3417   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3418     this_hdr->sh_flags |= SHF_EXCLUDE;
   3419 
   3420   /* If the section has relocs, set up a section header for the
   3421      SHT_REL[A] section.  If two relocation sections are required for
   3422      this section, it is up to the processor-specific back-end to
   3423      create the other.  */
   3424   if ((asect->flags & SEC_RELOC) != 0)
   3425     {
   3426       /* When doing a relocatable link, create both REL and RELA sections if
   3427 	 needed.  */
   3428       if (arg->link_info
   3429 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3430 	  && esd->rel.count + esd->rela.count > 0
   3431 	  && (bfd_link_relocatable (arg->link_info)
   3432 	      || arg->link_info->emitrelocations))
   3433 	{
   3434 	  if (esd->rel.count && esd->rel.hdr == NULL
   3435 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3436 					    false, delay_st_name_p))
   3437 	    {
   3438 	      arg->failed = true;
   3439 	      return;
   3440 	    }
   3441 	  if (esd->rela.count && esd->rela.hdr == NULL
   3442 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3443 					    true, delay_st_name_p))
   3444 	    {
   3445 	      arg->failed = true;
   3446 	      return;
   3447 	    }
   3448 	}
   3449       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3450 					  (asect->use_rela_p
   3451 					   ? &esd->rela : &esd->rel),
   3452 					  name,
   3453 					  asect->use_rela_p,
   3454 					  delay_st_name_p))
   3455 	{
   3456 	  arg->failed = true;
   3457 	  return;
   3458 	}
   3459     }
   3460 
   3461   /* Check for processor-specific section types.  */
   3462   sh_type = this_hdr->sh_type;
   3463   if (bed->elf_backend_fake_sections
   3464       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3465     {
   3466       arg->failed = true;
   3467       return;
   3468     }
   3469 
   3470   if (sh_type == SHT_NOBITS && asect->size != 0)
   3471     {
   3472       /* Don't change the header type from NOBITS if we are being
   3473 	 called for objcopy --only-keep-debug.  */
   3474       this_hdr->sh_type = sh_type;
   3475     }
   3476 }
   3477 
   3478 /* Fill in the contents of a SHT_GROUP section.  Called from
   3479    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3480    when ELF targets use the generic linker, ld.  Called for ld -r
   3481    from bfd_elf_final_link.  */
   3482 
   3483 void
   3484 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3485 {
   3486   bool *failedptr = (bool *) failedptrarg;
   3487   asection *elt, *first;
   3488   unsigned char *loc;
   3489   bool gas;
   3490 
   3491   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3492      elfxx-ia64.c.  */
   3493   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3494       || sec->size == 0
   3495       || *failedptr)
   3496     return;
   3497 
   3498   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3499     {
   3500       unsigned long symindx = 0;
   3501 
   3502       /* elf_group_id will have been set up by objcopy and the
   3503 	 generic linker.  */
   3504       if (elf_group_id (sec) != NULL)
   3505 	symindx = elf_group_id (sec)->udata.i;
   3506 
   3507       if (symindx == 0)
   3508 	{
   3509 	  /* If called from the assembler, swap_out_syms will have set up
   3510 	     elf_section_syms.
   3511 	     PR 25699: A corrupt input file could contain bogus group info.  */
   3512 	  if (sec->index >= elf_num_section_syms (abfd)
   3513 	      || elf_section_syms (abfd)[sec->index] == NULL)
   3514 	    {
   3515 	      *failedptr = true;
   3516 	      return;
   3517 	    }
   3518 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3519 	}
   3520       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3521     }
   3522   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3523     {
   3524       /* The ELF backend linker sets sh_info to -2 when the group
   3525 	 signature symbol is global, and thus the index can't be
   3526 	 set until all local symbols are output.  */
   3527       asection *igroup;
   3528       struct bfd_elf_section_data *sec_data;
   3529       unsigned long symndx;
   3530       unsigned long extsymoff;
   3531       struct elf_link_hash_entry *h;
   3532 
   3533       /* The point of this little dance to the first SHF_GROUP section
   3534 	 then back to the SHT_GROUP section is that this gets us to
   3535 	 the SHT_GROUP in the input object.  */
   3536       igroup = elf_sec_group (elf_next_in_group (sec));
   3537       sec_data = elf_section_data (igroup);
   3538       symndx = sec_data->this_hdr.sh_info;
   3539       extsymoff = 0;
   3540       if (!elf_bad_symtab (igroup->owner))
   3541 	{
   3542 	  Elf_Internal_Shdr *symtab_hdr;
   3543 
   3544 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3545 	  extsymoff = symtab_hdr->sh_info;
   3546 	}
   3547       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3548       while (h->root.type == bfd_link_hash_indirect
   3549 	     || h->root.type == bfd_link_hash_warning)
   3550 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3551 
   3552       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3553     }
   3554 
   3555   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3556   gas = true;
   3557   if (sec->contents == NULL)
   3558     {
   3559       gas = false;
   3560       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3561 
   3562       /* Arrange for the section to be written out.  */
   3563       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3564       if (sec->contents == NULL)
   3565 	{
   3566 	  *failedptr = true;
   3567 	  return;
   3568 	}
   3569     }
   3570 
   3571   loc = sec->contents + sec->size;
   3572 
   3573   /* Get the pointer to the first section in the group that gas
   3574      squirreled away here.  objcopy arranges for this to be set to the
   3575      start of the input section group.  */
   3576   first = elt = elf_next_in_group (sec);
   3577 
   3578   /* First element is a flag word.  Rest of section is elf section
   3579      indices for all the sections of the group.  Write them backwards
   3580      just to keep the group in the same order as given in .section
   3581      directives, not that it matters.  */
   3582   while (elt != NULL)
   3583     {
   3584       asection *s;
   3585 
   3586       s = elt;
   3587       if (!gas)
   3588 	s = s->output_section;
   3589       if (s != NULL
   3590 	  && !bfd_is_abs_section (s))
   3591 	{
   3592 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   3593 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   3594 
   3595 	  if (elf_sec->rel.hdr != NULL
   3596 	      && (gas
   3597 		  || (input_elf_sec->rel.hdr != NULL
   3598 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   3599 	    {
   3600 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   3601 	      loc -= 4;
   3602 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   3603 	    }
   3604 	  if (elf_sec->rela.hdr != NULL
   3605 	      && (gas
   3606 		  || (input_elf_sec->rela.hdr != NULL
   3607 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   3608 	    {
   3609 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   3610 	      loc -= 4;
   3611 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   3612 	    }
   3613 	  loc -= 4;
   3614 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   3615 	}
   3616       elt = elf_next_in_group (elt);
   3617       if (elt == first)
   3618 	break;
   3619     }
   3620 
   3621   loc -= 4;
   3622   BFD_ASSERT (loc == sec->contents);
   3623 
   3624   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   3625 }
   3626 
   3627 /* Given NAME, the name of a relocation section stripped of its
   3628    .rel/.rela prefix, return the section in ABFD to which the
   3629    relocations apply.  */
   3630 
   3631 asection *
   3632 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   3633 {
   3634   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   3635      section likely apply to .got.plt or .got section.  */
   3636   if (get_elf_backend_data (abfd)->want_got_plt
   3637       && strcmp (name, ".plt") == 0)
   3638     {
   3639       asection *sec;
   3640 
   3641       name = ".got.plt";
   3642       sec = bfd_get_section_by_name (abfd, name);
   3643       if (sec != NULL)
   3644 	return sec;
   3645       name = ".got";
   3646     }
   3647 
   3648   return bfd_get_section_by_name (abfd, name);
   3649 }
   3650 
   3651 /* Return the section to which RELOC_SEC applies.  */
   3652 
   3653 static asection *
   3654 elf_get_reloc_section (asection *reloc_sec)
   3655 {
   3656   const char *name;
   3657   unsigned int type;
   3658   bfd *abfd;
   3659   const struct elf_backend_data *bed;
   3660 
   3661   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   3662   if (type != SHT_REL && type != SHT_RELA)
   3663     return NULL;
   3664 
   3665   /* We look up the section the relocs apply to by name.  */
   3666   name = reloc_sec->name;
   3667   if (!startswith (name, ".rel"))
   3668     return NULL;
   3669   name += 4;
   3670   if (type == SHT_RELA && *name++ != 'a')
   3671     return NULL;
   3672 
   3673   abfd = reloc_sec->owner;
   3674   bed = get_elf_backend_data (abfd);
   3675   return bed->get_reloc_section (abfd, name);
   3676 }
   3677 
   3678 /* Assign all ELF section numbers.  The dummy first section is handled here
   3679    too.  The link/info pointers for the standard section types are filled
   3680    in here too, while we're at it.  LINK_INFO will be 0 when arriving
   3681    here for objcopy, and when using the generic ELF linker.  */
   3682 
   3683 static bool
   3684 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   3685 {
   3686   struct elf_obj_tdata *t = elf_tdata (abfd);
   3687   asection *sec;
   3688   unsigned int section_number;
   3689   Elf_Internal_Shdr **i_shdrp;
   3690   struct bfd_elf_section_data *d;
   3691   bool need_symtab;
   3692   size_t amt;
   3693 
   3694   section_number = 1;
   3695 
   3696   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   3697 
   3698   /* SHT_GROUP sections are in relocatable files only.  */
   3699   if (link_info == NULL || !link_info->resolve_section_groups)
   3700     {
   3701       size_t reloc_count = 0;
   3702 
   3703       /* Put SHT_GROUP sections first.  */
   3704       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   3705 	{
   3706 	  d = elf_section_data (sec);
   3707 
   3708 	  if (d->this_hdr.sh_type == SHT_GROUP)
   3709 	    {
   3710 	      if (sec->flags & SEC_LINKER_CREATED)
   3711 		{
   3712 		  /* Remove the linker created SHT_GROUP sections.  */
   3713 		  bfd_section_list_remove (abfd, sec);
   3714 		  abfd->section_count--;
   3715 		}
   3716 	      else
   3717 		d->this_idx = section_number++;
   3718 	    }
   3719 
   3720 	  /* Count relocations.  */
   3721 	  reloc_count += sec->reloc_count;
   3722 	}
   3723 
   3724       /* Clear HAS_RELOC if there are no relocations.  */
   3725       if (reloc_count == 0)
   3726 	abfd->flags &= ~HAS_RELOC;
   3727     }
   3728 
   3729   for (sec = abfd->sections; sec; sec = sec->next)
   3730     {
   3731       d = elf_section_data (sec);
   3732 
   3733       if (d->this_hdr.sh_type != SHT_GROUP)
   3734 	d->this_idx = section_number++;
   3735       if (d->this_hdr.sh_name != (unsigned int) -1)
   3736 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   3737       if (d->rel.hdr)
   3738 	{
   3739 	  d->rel.idx = section_number++;
   3740 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   3741 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   3742 	}
   3743       else
   3744 	d->rel.idx = 0;
   3745 
   3746       if (d->rela.hdr)
   3747 	{
   3748 	  d->rela.idx = section_number++;
   3749 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   3750 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   3751 	}
   3752       else
   3753 	d->rela.idx = 0;
   3754     }
   3755 
   3756   need_symtab = (bfd_get_symcount (abfd) > 0
   3757 		 || (link_info == NULL
   3758 		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   3759 			 == HAS_RELOC)));
   3760   if (need_symtab)
   3761     {
   3762       elf_onesymtab (abfd) = section_number++;
   3763       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   3764       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   3765 	{
   3766 	  elf_section_list *entry;
   3767 
   3768 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   3769 
   3770 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   3771 	  entry->ndx = section_number++;
   3772 	  elf_symtab_shndx_list (abfd) = entry;
   3773 	  entry->hdr.sh_name
   3774 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3775 						  ".symtab_shndx", false);
   3776 	  if (entry->hdr.sh_name == (unsigned int) -1)
   3777 	    return false;
   3778 	}
   3779       elf_strtab_sec (abfd) = section_number++;
   3780       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   3781     }
   3782 
   3783   elf_shstrtab_sec (abfd) = section_number++;
   3784   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   3785   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   3786 
   3787   if (section_number >= SHN_LORESERVE)
   3788     {
   3789       /* xgettext:c-format */
   3790       _bfd_error_handler (_("%pB: too many sections: %u"),
   3791 			  abfd, section_number);
   3792       return false;
   3793     }
   3794 
   3795   elf_numsections (abfd) = section_number;
   3796   elf_elfheader (abfd)->e_shnum = section_number;
   3797 
   3798   /* Set up the list of section header pointers, in agreement with the
   3799      indices.  */
   3800   amt = section_number * sizeof (Elf_Internal_Shdr *);
   3801   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
   3802   if (i_shdrp == NULL)
   3803     return false;
   3804 
   3805   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   3806 						 sizeof (Elf_Internal_Shdr));
   3807   if (i_shdrp[0] == NULL)
   3808     {
   3809       bfd_release (abfd, i_shdrp);
   3810       return false;
   3811     }
   3812 
   3813   elf_elfsections (abfd) = i_shdrp;
   3814 
   3815   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   3816   if (need_symtab)
   3817     {
   3818       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   3819       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   3820 	{
   3821 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   3822 	  BFD_ASSERT (entry != NULL);
   3823 	  i_shdrp[entry->ndx] = & entry->hdr;
   3824 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   3825 	}
   3826       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   3827       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   3828     }
   3829 
   3830   for (sec = abfd->sections; sec; sec = sec->next)
   3831     {
   3832       asection *s;
   3833 
   3834       d = elf_section_data (sec);
   3835 
   3836       i_shdrp[d->this_idx] = &d->this_hdr;
   3837       if (d->rel.idx != 0)
   3838 	i_shdrp[d->rel.idx] = d->rel.hdr;
   3839       if (d->rela.idx != 0)
   3840 	i_shdrp[d->rela.idx] = d->rela.hdr;
   3841 
   3842       /* Fill in the sh_link and sh_info fields while we're at it.  */
   3843 
   3844       /* sh_link of a reloc section is the section index of the symbol
   3845 	 table.  sh_info is the section index of the section to which
   3846 	 the relocation entries apply.  */
   3847       if (d->rel.idx != 0)
   3848 	{
   3849 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   3850 	  d->rel.hdr->sh_info = d->this_idx;
   3851 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   3852 	}
   3853       if (d->rela.idx != 0)
   3854 	{
   3855 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   3856 	  d->rela.hdr->sh_info = d->this_idx;
   3857 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   3858 	}
   3859 
   3860       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   3861       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   3862 	{
   3863 	  s = elf_linked_to_section (sec);
   3864 	  /* We can now have a NULL linked section pointer.
   3865 	     This happens when the sh_link field is 0, which is done
   3866 	     when a linked to section is discarded but the linking
   3867 	     section has been retained for some reason.  */
   3868 	  if (s)
   3869 	    {
   3870 	      /* Check discarded linkonce section.  */
   3871 	      if (discarded_section (s))
   3872 		{
   3873 		  asection *kept;
   3874 		  _bfd_error_handler
   3875 		    /* xgettext:c-format */
   3876 		    (_("%pB: sh_link of section `%pA' points to"
   3877 		       " discarded section `%pA' of `%pB'"),
   3878 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   3879 		  /* Point to the kept section if it has the same
   3880 		     size as the discarded one.  */
   3881 		  kept = _bfd_elf_check_kept_section (s, link_info);
   3882 		  if (kept == NULL)
   3883 		    {
   3884 		      bfd_set_error (bfd_error_bad_value);
   3885 		      return false;
   3886 		    }
   3887 		  s = kept;
   3888 		}
   3889 	      /* Handle objcopy. */
   3890 	      else if (s->output_section == NULL)
   3891 		{
   3892 		  _bfd_error_handler
   3893 		    /* xgettext:c-format */
   3894 		    (_("%pB: sh_link of section `%pA' points to"
   3895 		       " removed section `%pA' of `%pB'"),
   3896 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   3897 		  bfd_set_error (bfd_error_bad_value);
   3898 		  return false;
   3899 		}
   3900 	      s = s->output_section;
   3901 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3902 	    }
   3903 	}
   3904 
   3905       switch (d->this_hdr.sh_type)
   3906 	{
   3907 	case SHT_REL:
   3908 	case SHT_RELA:
   3909 	  /* A reloc section which we are treating as a normal BFD
   3910 	     section.  sh_link is the section index of the symbol
   3911 	     table.  sh_info is the section index of the section to
   3912 	     which the relocation entries apply.  We assume that an
   3913 	     allocated reloc section uses the dynamic symbol table
   3914 	     if there is one.  Otherwise we guess the normal symbol
   3915 	     table.  FIXME: How can we be sure?  */
   3916 	  if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
   3917 	    {
   3918 	      s = bfd_get_section_by_name (abfd, ".dynsym");
   3919 	      if (s != NULL)
   3920 		d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3921 	    }
   3922 	  if (d->this_hdr.sh_link == 0)
   3923 	    d->this_hdr.sh_link = elf_onesymtab (abfd);
   3924 
   3925 	  s = elf_get_reloc_section (sec);
   3926 	  if (s != NULL)
   3927 	    {
   3928 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   3929 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   3930 	    }
   3931 	  break;
   3932 
   3933 	case SHT_STRTAB:
   3934 	  /* We assume that a section named .stab*str is a stabs
   3935 	     string section.  We look for a section with the same name
   3936 	     but without the trailing ``str'', and set its sh_link
   3937 	     field to point to this section.  */
   3938 	  if (startswith (sec->name, ".stab")
   3939 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   3940 	    {
   3941 	      size_t len;
   3942 	      char *alc;
   3943 
   3944 	      len = strlen (sec->name);
   3945 	      alc = (char *) bfd_malloc (len - 2);
   3946 	      if (alc == NULL)
   3947 		return false;
   3948 	      memcpy (alc, sec->name, len - 3);
   3949 	      alc[len - 3] = '\0';
   3950 	      s = bfd_get_section_by_name (abfd, alc);
   3951 	      free (alc);
   3952 	      if (s != NULL)
   3953 		{
   3954 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   3955 
   3956 		  /* This is a .stab section.  */
   3957 		  elf_section_data (s)->this_hdr.sh_entsize = 12;
   3958 		}
   3959 	    }
   3960 	  break;
   3961 
   3962 	case SHT_DYNAMIC:
   3963 	case SHT_DYNSYM:
   3964 	case SHT_GNU_verneed:
   3965 	case SHT_GNU_verdef:
   3966 	  /* sh_link is the section header index of the string table
   3967 	     used for the dynamic entries, or the symbol table, or the
   3968 	     version strings.  */
   3969 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   3970 	  if (s != NULL)
   3971 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3972 	  break;
   3973 
   3974 	case SHT_GNU_LIBLIST:
   3975 	  /* sh_link is the section header index of the prelink library
   3976 	     list used for the dynamic entries, or the symbol table, or
   3977 	     the version strings.  */
   3978 	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
   3979 					      ? ".dynstr" : ".gnu.libstr"));
   3980 	  if (s != NULL)
   3981 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3982 	  break;
   3983 
   3984 	case SHT_HASH:
   3985 	case SHT_GNU_HASH:
   3986 	case SHT_GNU_versym:
   3987 	  /* sh_link is the section header index of the symbol table
   3988 	     this hash table or version table is for.  */
   3989 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   3990 	  if (s != NULL)
   3991 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   3992 	  break;
   3993 
   3994 	case SHT_GROUP:
   3995 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   3996 	}
   3997     }
   3998 
   3999   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   4000      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   4001      debug section name from .debug_* to .zdebug_* if needed.  */
   4002 
   4003   return true;
   4004 }
   4005 
   4006 static bool
   4007 sym_is_global (bfd *abfd, asymbol *sym)
   4008 {
   4009   /* If the backend has a special mapping, use it.  */
   4010   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4011   if (bed->elf_backend_sym_is_global)
   4012     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   4013 
   4014   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   4015 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   4016 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   4017 }
   4018 
   4019 /* Filter global symbols of ABFD to include in the import library.  All
   4020    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4021    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4022    the beginning of that array.
   4023 
   4024    Returns the number of symbols to keep.  */
   4025 
   4026 unsigned int
   4027 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4028 				asymbol **syms, long symcount)
   4029 {
   4030   long src_count, dst_count = 0;
   4031 
   4032   for (src_count = 0; src_count < symcount; src_count++)
   4033     {
   4034       asymbol *sym = syms[src_count];
   4035       char *name = (char *) bfd_asymbol_name (sym);
   4036       struct bfd_link_hash_entry *h;
   4037 
   4038       if (!sym_is_global (abfd, sym))
   4039 	continue;
   4040 
   4041       h = bfd_link_hash_lookup (info->hash, name, false, false, false);
   4042       if (h == NULL)
   4043 	continue;
   4044       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4045 	continue;
   4046       if (h->linker_def || h->ldscript_def)
   4047 	continue;
   4048 
   4049       syms[dst_count++] = sym;
   4050     }
   4051 
   4052   syms[dst_count] = NULL;
   4053 
   4054   return dst_count;
   4055 }
   4056 
   4057 /* Don't output section symbols for sections that are not going to be
   4058    output, that are duplicates or there is no BFD section.  */
   4059 
   4060 static bool
   4061 ignore_section_sym (bfd *abfd, asymbol *sym)
   4062 {
   4063   elf_symbol_type *type_ptr;
   4064 
   4065   if (sym == NULL)
   4066     return false;
   4067 
   4068   if ((sym->flags & BSF_SECTION_SYM) == 0)
   4069     return false;
   4070 
   4071   /* Ignore the section symbol if it isn't used.  */
   4072   if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
   4073     return true;
   4074 
   4075   if (sym->section == NULL)
   4076     return true;
   4077 
   4078   type_ptr = elf_symbol_from (sym);
   4079   return ((type_ptr != NULL
   4080 	   && type_ptr->internal_elf_sym.st_shndx != 0
   4081 	   && bfd_is_abs_section (sym->section))
   4082 	  || !(sym->section->owner == abfd
   4083 	       || (sym->section->output_section != NULL
   4084 		   && sym->section->output_section->owner == abfd
   4085 		   && sym->section->output_offset == 0)
   4086 	       || bfd_is_abs_section (sym->section)));
   4087 }
   4088 
   4089 /* Map symbol from it's internal number to the external number, moving
   4090    all local symbols to be at the head of the list.  */
   4091 
   4092 static bool
   4093 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4094 {
   4095   unsigned int symcount = bfd_get_symcount (abfd);
   4096   asymbol **syms = bfd_get_outsymbols (abfd);
   4097   asymbol **sect_syms;
   4098   unsigned int num_locals = 0;
   4099   unsigned int num_globals = 0;
   4100   unsigned int num_locals2 = 0;
   4101   unsigned int num_globals2 = 0;
   4102   unsigned int max_index = 0;
   4103   unsigned int idx;
   4104   asection *asect;
   4105   asymbol **new_syms;
   4106   size_t amt;
   4107 
   4108 #ifdef DEBUG
   4109   fprintf (stderr, "elf_map_symbols\n");
   4110   fflush (stderr);
   4111 #endif
   4112 
   4113   for (asect = abfd->sections; asect; asect = asect->next)
   4114     {
   4115       if (max_index < asect->index)
   4116 	max_index = asect->index;
   4117     }
   4118 
   4119   max_index++;
   4120   amt = max_index * sizeof (asymbol *);
   4121   sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
   4122   if (sect_syms == NULL)
   4123     return false;
   4124   elf_section_syms (abfd) = sect_syms;
   4125   elf_num_section_syms (abfd) = max_index;
   4126 
   4127   /* Init sect_syms entries for any section symbols we have already
   4128      decided to output.  */
   4129   for (idx = 0; idx < symcount; idx++)
   4130     {
   4131       asymbol *sym = syms[idx];
   4132 
   4133       if ((sym->flags & BSF_SECTION_SYM) != 0
   4134 	  && sym->value == 0
   4135 	  && !ignore_section_sym (abfd, sym)
   4136 	  && !bfd_is_abs_section (sym->section))
   4137 	{
   4138 	  asection *sec = sym->section;
   4139 
   4140 	  if (sec->owner != abfd)
   4141 	    sec = sec->output_section;
   4142 
   4143 	  sect_syms[sec->index] = syms[idx];
   4144 	}
   4145     }
   4146 
   4147   /* Classify all of the symbols.  */
   4148   for (idx = 0; idx < symcount; idx++)
   4149     {
   4150       if (sym_is_global (abfd, syms[idx]))
   4151 	num_globals++;
   4152       else if (!ignore_section_sym (abfd, syms[idx]))
   4153 	num_locals++;
   4154     }
   4155 
   4156   /* We will be adding a section symbol for each normal BFD section.  Most
   4157      sections will already have a section symbol in outsymbols, but
   4158      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4159      at least in that case.  */
   4160   for (asect = abfd->sections; asect; asect = asect->next)
   4161     {
   4162       asymbol *sym = asect->symbol;
   4163       /* Don't include ignored section symbols.  */
   4164       if (!ignore_section_sym (abfd, sym)
   4165 	  && sect_syms[asect->index] == NULL)
   4166 	{
   4167 	  if (!sym_is_global (abfd, asect->symbol))
   4168 	    num_locals++;
   4169 	  else
   4170 	    num_globals++;
   4171 	}
   4172     }
   4173 
   4174   /* Now sort the symbols so the local symbols are first.  */
   4175   amt = (num_locals + num_globals) * sizeof (asymbol *);
   4176   new_syms = (asymbol **) bfd_alloc (abfd, amt);
   4177   if (new_syms == NULL)
   4178     return false;
   4179 
   4180   for (idx = 0; idx < symcount; idx++)
   4181     {
   4182       asymbol *sym = syms[idx];
   4183       unsigned int i;
   4184 
   4185       if (sym_is_global (abfd, sym))
   4186 	i = num_locals + num_globals2++;
   4187       /* Don't include ignored section symbols.  */
   4188       else if (!ignore_section_sym (abfd, sym))
   4189 	i = num_locals2++;
   4190       else
   4191 	continue;
   4192       new_syms[i] = sym;
   4193       sym->udata.i = i + 1;
   4194     }
   4195   for (asect = abfd->sections; asect; asect = asect->next)
   4196     {
   4197       asymbol *sym = asect->symbol;
   4198       if (!ignore_section_sym (abfd, sym)
   4199 	  && sect_syms[asect->index] == NULL)
   4200 	{
   4201 	  unsigned int i;
   4202 
   4203 	  sect_syms[asect->index] = sym;
   4204 	  if (!sym_is_global (abfd, sym))
   4205 	    i = num_locals2++;
   4206 	  else
   4207 	    i = num_locals + num_globals2++;
   4208 	  new_syms[i] = sym;
   4209 	  sym->udata.i = i + 1;
   4210 	}
   4211     }
   4212 
   4213   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4214 
   4215   *pnum_locals = num_locals;
   4216   return true;
   4217 }
   4218 
   4219 /* Align to the maximum file alignment that could be required for any
   4220    ELF data structure.  */
   4221 
   4222 static inline file_ptr
   4223 align_file_position (file_ptr off, int align)
   4224 {
   4225   return (off + align - 1) & ~(align - 1);
   4226 }
   4227 
   4228 /* Assign a file position to a section, optionally aligning to the
   4229    required section alignment.  */
   4230 
   4231 file_ptr
   4232 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4233 					   file_ptr offset,
   4234 					   bool align)
   4235 {
   4236   if (align && i_shdrp->sh_addralign > 1)
   4237     offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
   4238   i_shdrp->sh_offset = offset;
   4239   if (i_shdrp->bfd_section != NULL)
   4240     i_shdrp->bfd_section->filepos = offset;
   4241   if (i_shdrp->sh_type != SHT_NOBITS)
   4242     offset += i_shdrp->sh_size;
   4243   return offset;
   4244 }
   4245 
   4246 /* Compute the file positions we are going to put the sections at, and
   4247    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4248    is not NULL, this is being called by the ELF backend linker.  */
   4249 
   4250 bool
   4251 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4252 					 struct bfd_link_info *link_info)
   4253 {
   4254   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4255   struct fake_section_arg fsargs;
   4256   bool failed;
   4257   struct elf_strtab_hash *strtab = NULL;
   4258   Elf_Internal_Shdr *shstrtab_hdr;
   4259   bool need_symtab;
   4260 
   4261   if (abfd->output_has_begun)
   4262     return true;
   4263 
   4264   /* Do any elf backend specific processing first.  */
   4265   if (bed->elf_backend_begin_write_processing)
   4266     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4267 
   4268   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4269     return false;
   4270 
   4271   fsargs.failed = false;
   4272   fsargs.link_info = link_info;
   4273   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4274   if (fsargs.failed)
   4275     return false;
   4276 
   4277   if (!assign_section_numbers (abfd, link_info))
   4278     return false;
   4279 
   4280   /* The backend linker builds symbol table information itself.  */
   4281   need_symtab = (link_info == NULL
   4282 		 && (bfd_get_symcount (abfd) > 0
   4283 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4284 			 == HAS_RELOC)));
   4285   if (need_symtab)
   4286     {
   4287       /* Non-zero if doing a relocatable link.  */
   4288       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4289 
   4290       if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
   4291 	return false;
   4292     }
   4293 
   4294   failed = false;
   4295   if (link_info == NULL)
   4296     {
   4297       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4298       if (failed)
   4299 	return false;
   4300     }
   4301 
   4302   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4303   /* sh_name was set in init_file_header.  */
   4304   shstrtab_hdr->sh_type = SHT_STRTAB;
   4305   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4306   shstrtab_hdr->sh_addr = 0;
   4307   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4308   shstrtab_hdr->sh_entsize = 0;
   4309   shstrtab_hdr->sh_link = 0;
   4310   shstrtab_hdr->sh_info = 0;
   4311   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4312   shstrtab_hdr->sh_addralign = 1;
   4313 
   4314   if (!assign_file_positions_except_relocs (abfd, link_info))
   4315     return false;
   4316 
   4317   if (need_symtab)
   4318     {
   4319       file_ptr off;
   4320       Elf_Internal_Shdr *hdr;
   4321 
   4322       off = elf_next_file_pos (abfd);
   4323 
   4324       hdr = & elf_symtab_hdr (abfd);
   4325       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4326 
   4327       if (elf_symtab_shndx_list (abfd) != NULL)
   4328 	{
   4329 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4330 	  if (hdr->sh_size != 0)
   4331 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4332 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4333 	}
   4334 
   4335       hdr = &elf_tdata (abfd)->strtab_hdr;
   4336       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   4337 
   4338       elf_next_file_pos (abfd) = off;
   4339 
   4340       /* Now that we know where the .strtab section goes, write it
   4341 	 out.  */
   4342       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4343 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4344 	return false;
   4345       _bfd_elf_strtab_free (strtab);
   4346     }
   4347 
   4348   abfd->output_has_begun = true;
   4349 
   4350   return true;
   4351 }
   4352 
   4353 /* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
   4354    function effectively returns whether --eh-frame-hdr is given on the
   4355    command line.  After size_dynamic_sections the result reflects
   4356    whether .eh_frame_hdr will actually be output (sizing isn't done
   4357    until ldemul_after_allocation).  */
   4358 
   4359 static asection *
   4360 elf_eh_frame_hdr (const struct bfd_link_info *info)
   4361 {
   4362   if (info != NULL && is_elf_hash_table (info->hash))
   4363     return elf_hash_table (info)->eh_info.hdr_sec;
   4364   return NULL;
   4365 }
   4366 
   4367 /* Make an initial estimate of the size of the program header.  If we
   4368    get the number wrong here, we'll redo section placement.  */
   4369 
   4370 static bfd_size_type
   4371 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4372 {
   4373   size_t segs;
   4374   asection *s, *s2;
   4375   const struct elf_backend_data *bed;
   4376 
   4377   /* Assume we will need exactly two PT_LOAD segments: one for text
   4378      and one for data.  */
   4379   segs = 2;
   4380 
   4381   s = bfd_get_section_by_name (abfd, ".interp");
   4382   s2 = bfd_get_section_by_name (abfd, ".dynamic");
   4383   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4384     {
   4385       ++segs;
   4386     }
   4387 
   4388   if (s2 != NULL && (s2->flags & SEC_LOAD) != 0)
   4389     {
   4390       /* We need a PT_DYNAMIC segment.  */
   4391       ++segs;
   4392     }
   4393 
   4394   if ((s != NULL && (s->flags & SEC_LOAD) != 0) ||
   4395       (s2 != NULL && (s2->flags & SEC_LOAD) != 0))
   4396     {
   4397       /*
   4398        * If either a PT_INTERP or PT_DYNAMIC segment is created,
   4399        * also create a PT_PHDR segment.
   4400        */
   4401       ++segs;
   4402     }
   4403 
   4404   if (info != NULL && info->relro)
   4405     {
   4406       /* We need a PT_GNU_RELRO segment.  */
   4407       ++segs;
   4408     }
   4409 
   4410   if (elf_eh_frame_hdr (info))
   4411     {
   4412       /* We need a PT_GNU_EH_FRAME segment.  */
   4413       ++segs;
   4414     }
   4415 
   4416   if (elf_stack_flags (abfd))
   4417     {
   4418       /* We need a PT_GNU_STACK segment.  */
   4419       ++segs;
   4420     }
   4421 
   4422   s = bfd_get_section_by_name (abfd,
   4423 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4424   if (s != NULL && s->size != 0)
   4425     {
   4426       /* We need a PT_GNU_PROPERTY segment.  */
   4427       ++segs;
   4428     }
   4429 
   4430   for (s = abfd->sections; s != NULL; s = s->next)
   4431     {
   4432       if ((s->flags & SEC_LOAD) != 0
   4433 	  && elf_section_type (s) == SHT_NOTE)
   4434 	{
   4435 	  unsigned int alignment_power;
   4436 	  /* We need a PT_NOTE segment.  */
   4437 	  ++segs;
   4438 	  /* Try to create just one PT_NOTE segment for all adjacent
   4439 	     loadable SHT_NOTE sections.  gABI requires that within a
   4440 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4441 	     each note should have the same alignment.  So we check
   4442 	     whether the sections are correctly aligned.  */
   4443 	  alignment_power = s->alignment_power;
   4444 	  while (s->next != NULL
   4445 		 && s->next->alignment_power == alignment_power
   4446 		 && (s->next->flags & SEC_LOAD) != 0
   4447 		 && elf_section_type (s->next) == SHT_NOTE)
   4448 	    s = s->next;
   4449 	}
   4450     }
   4451 
   4452   for (s = abfd->sections; s != NULL; s = s->next)
   4453     {
   4454       if (s->flags & SEC_THREAD_LOCAL)
   4455 	{
   4456 	  /* We need a PT_TLS segment.  */
   4457 	  ++segs;
   4458 	  break;
   4459 	}
   4460     }
   4461 
   4462   bed = get_elf_backend_data (abfd);
   4463 
   4464   if ((abfd->flags & D_PAGED) != 0
   4465       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4466     {
   4467       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4468       bfd_vma commonpagesize;
   4469       unsigned int page_align_power;
   4470 
   4471       if (info != NULL)
   4472 	commonpagesize = info->commonpagesize;
   4473       else
   4474 	commonpagesize = bed->commonpagesize;
   4475       page_align_power = bfd_log2 (commonpagesize);
   4476       for (s = abfd->sections; s != NULL; s = s->next)
   4477 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   4478 	  {
   4479 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   4480 	      {
   4481 		_bfd_error_handler
   4482 		  /* xgettext:c-format */
   4483 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   4484 		     "sh_info field: %d"),
   4485 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4486 		continue;
   4487 	      }
   4488 	    /* Align mbind section to page size.  */
   4489 	    if (s->alignment_power < page_align_power)
   4490 	      s->alignment_power = page_align_power;
   4491 	    segs ++;
   4492 	  }
   4493     }
   4494 
   4495   /* Let the backend count up any program headers it might need.  */
   4496   if (bed->elf_backend_additional_program_headers)
   4497     {
   4498       int a;
   4499 
   4500       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4501       if (a == -1)
   4502 	abort ();
   4503       segs += a;
   4504     }
   4505 
   4506   return segs * bed->s->sizeof_phdr;
   4507 }
   4508 
   4509 /* Find the segment that contains the output_section of section.  */
   4510 
   4511 Elf_Internal_Phdr *
   4512 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4513 {
   4514   struct elf_segment_map *m;
   4515   Elf_Internal_Phdr *p;
   4516 
   4517   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4518        m != NULL;
   4519        m = m->next, p++)
   4520     {
   4521       int i;
   4522 
   4523       for (i = m->count - 1; i >= 0; i--)
   4524 	if (m->sections[i] == section)
   4525 	  return p;
   4526     }
   4527 
   4528   return NULL;
   4529 }
   4530 
   4531 /* Create a mapping from a set of sections to a program segment.  */
   4532 
   4533 static struct elf_segment_map *
   4534 make_mapping (bfd *abfd,
   4535 	      asection **sections,
   4536 	      unsigned int from,
   4537 	      unsigned int to,
   4538 	      bool phdr)
   4539 {
   4540   struct elf_segment_map *m;
   4541   unsigned int i;
   4542   asection **hdrpp;
   4543   size_t amt;
   4544 
   4545   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   4546   amt += (to - from) * sizeof (asection *);
   4547   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4548   if (m == NULL)
   4549     return NULL;
   4550   m->next = NULL;
   4551   m->p_type = PT_LOAD;
   4552   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4553     m->sections[i - from] = *hdrpp;
   4554   m->count = to - from;
   4555 
   4556   if (from == 0 && phdr)
   4557     {
   4558       /* Include the headers in the first PT_LOAD segment.  */
   4559       m->includes_filehdr = 1;
   4560       m->includes_phdrs = 1;
   4561     }
   4562 
   4563   return m;
   4564 }
   4565 
   4566 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   4567    on failure.  */
   4568 
   4569 struct elf_segment_map *
   4570 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   4571 {
   4572   struct elf_segment_map *m;
   4573 
   4574   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   4575 					     sizeof (struct elf_segment_map));
   4576   if (m == NULL)
   4577     return NULL;
   4578   m->next = NULL;
   4579   m->p_type = PT_DYNAMIC;
   4580   m->count = 1;
   4581   m->sections[0] = dynsec;
   4582 
   4583   return m;
   4584 }
   4585 
   4586 /* Possibly add or remove segments from the segment map.  */
   4587 
   4588 static bool
   4589 elf_modify_segment_map (bfd *abfd,
   4590 			struct bfd_link_info *info,
   4591 			bool remove_empty_load)
   4592 {
   4593   struct elf_segment_map **m;
   4594   const struct elf_backend_data *bed;
   4595 
   4596   /* The placement algorithm assumes that non allocated sections are
   4597      not in PT_LOAD segments.  We ensure this here by removing such
   4598      sections from the segment map.  We also remove excluded
   4599      sections.  Finally, any PT_LOAD segment without sections is
   4600      removed.  */
   4601   m = &elf_seg_map (abfd);
   4602   while (*m)
   4603     {
   4604       unsigned int i, new_count;
   4605 
   4606       for (new_count = 0, i = 0; i < (*m)->count; i++)
   4607 	{
   4608 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   4609 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   4610 		  || (*m)->p_type != PT_LOAD))
   4611 	    {
   4612 	      (*m)->sections[new_count] = (*m)->sections[i];
   4613 	      new_count++;
   4614 	    }
   4615 	}
   4616       (*m)->count = new_count;
   4617 
   4618       if (remove_empty_load
   4619 	  && (*m)->p_type == PT_LOAD
   4620 	  && (*m)->count == 0
   4621 	  && !(*m)->includes_phdrs)
   4622 	*m = (*m)->next;
   4623       else
   4624 	m = &(*m)->next;
   4625     }
   4626 
   4627   bed = get_elf_backend_data (abfd);
   4628   if (bed->elf_backend_modify_segment_map != NULL)
   4629     {
   4630       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   4631 	return false;
   4632     }
   4633 
   4634   return true;
   4635 }
   4636 
   4637 #define IS_TBSS(s) \
   4638   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   4639 
   4640 /* Set up a mapping from BFD sections to program segments.  Update
   4641    NEED_LAYOUT if the section layout is changed.  */
   4642 
   4643 bool
   4644 _bfd_elf_map_sections_to_segments (bfd *abfd,
   4645 				   struct bfd_link_info *info,
   4646 				   bool *need_layout)
   4647 {
   4648   unsigned int count;
   4649   struct elf_segment_map *m;
   4650   asection **sections = NULL;
   4651   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4652   bool no_user_phdrs;
   4653 
   4654   no_user_phdrs = elf_seg_map (abfd) == NULL;
   4655 
   4656   if (info != NULL)
   4657     {
   4658       info->user_phdrs = !no_user_phdrs;
   4659 
   4660       /* Size the relative relocations if DT_RELR is enabled.  */
   4661       if (info->enable_dt_relr
   4662 	  && need_layout != NULL
   4663 	  && bed->size_relative_relocs
   4664 	  && !bed->size_relative_relocs (info, need_layout))
   4665 	info->callbacks->einfo
   4666 	  (_("%F%P: failed to size relative relocations\n"));
   4667     }
   4668 
   4669   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   4670     {
   4671       asection *s;
   4672       unsigned int i;
   4673       struct elf_segment_map *mfirst;
   4674       struct elf_segment_map **pm;
   4675       asection *last_hdr;
   4676       bfd_vma last_size;
   4677       unsigned int hdr_index;
   4678       bfd_vma maxpagesize;
   4679       asection **hdrpp;
   4680       bool phdr_in_segment;
   4681       bool writable;
   4682       bool executable;
   4683       unsigned int tls_count = 0;
   4684       asection *first_tls = NULL;
   4685       asection *first_mbind = NULL;
   4686       asection *dynsec, *eh_frame_hdr;
   4687       size_t amt;
   4688       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   4689       bfd_size_type phdr_size;  /* Octets/bytes.  */
   4690       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   4691 
   4692       /* Select the allocated sections, and sort them.  */
   4693 
   4694       amt = bfd_count_sections (abfd) * sizeof (asection *);
   4695       sections = (asection **) bfd_malloc (amt);
   4696       if (sections == NULL)
   4697 	goto error_return;
   4698 
   4699       /* Calculate top address, avoiding undefined behaviour of shift
   4700 	 left operator when shift count is equal to size of type
   4701 	 being shifted.  */
   4702       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   4703       addr_mask = (addr_mask << 1) + 1;
   4704 
   4705       i = 0;
   4706       for (s = abfd->sections; s != NULL; s = s->next)
   4707 	{
   4708 	  if ((s->flags & SEC_ALLOC) != 0)
   4709 	    {
   4710 	      /* target_index is unused until bfd_elf_final_link
   4711 		 starts output of section symbols.  Use it to make
   4712 		 qsort stable.  */
   4713 	      s->target_index = i;
   4714 	      sections[i] = s;
   4715 	      ++i;
   4716 	      /* A wrapping section potentially clashes with header.  */
   4717 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   4718 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   4719 	    }
   4720 	}
   4721       BFD_ASSERT (i <= bfd_count_sections (abfd));
   4722       count = i;
   4723 
   4724       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   4725 
   4726       phdr_size = elf_program_header_size (abfd);
   4727       if (phdr_size == (bfd_size_type) -1)
   4728 	phdr_size = get_program_header_size (abfd, info);
   4729       phdr_size += bed->s->sizeof_ehdr;
   4730       /* phdr_size is compared to LMA values which are in bytes.  */
   4731       phdr_size /= opb;
   4732       if (info != NULL)
   4733 	maxpagesize = info->maxpagesize;
   4734       else
   4735 	maxpagesize = bed->maxpagesize;
   4736       if (maxpagesize == 0)
   4737 	maxpagesize = 1;
   4738       phdr_in_segment = info != NULL && info->load_phdrs;
   4739       if (count != 0
   4740 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   4741 	      >= (phdr_size & (maxpagesize - 1))))
   4742 	/* For compatibility with old scripts that may not be using
   4743 	   SIZEOF_HEADERS, add headers when it looks like space has
   4744 	   been left for them.  */
   4745 	phdr_in_segment = true;
   4746 
   4747       /* Build the mapping.  */
   4748       mfirst = NULL;
   4749       pm = &mfirst;
   4750 
   4751       /* If we have a .interp section, then create a PT_PHDR segment for
   4752 	 the program headers and a PT_INTERP segment for the .interp
   4753 	 section.  */
   4754       s = bfd_get_section_by_name (abfd, ".interp");
   4755       if (s != NULL && (s->flags & SEC_LOAD) == 0)
   4756       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4757 	s = NULL;
   4758       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   4759       if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0)
   4760 	dynsec = NULL;
   4761 
   4762       if (s != NULL || dynsec != NULL)
   4763 	{
   4764 	  amt = sizeof (struct elf_segment_map);
   4765 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4766 	  if (m == NULL)
   4767 	    goto error_return;
   4768 	  m->next = NULL;
   4769 	  m->p_type = PT_PHDR;
   4770 	  m->p_flags = PF_R;
   4771 	  m->p_flags_valid = 1;
   4772 	  m->includes_phdrs = 1;
   4773 	  phdr_in_segment = true;
   4774 	  *pm = m;
   4775 	  pm = &m->next;
   4776 	}
   4777 
   4778       if (s != NULL)
   4779 	{
   4780 	  amt = sizeof (struct elf_segment_map);
   4781 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4782 	  if (m == NULL)
   4783 	    goto error_return;
   4784 	  m->next = NULL;
   4785 	  m->p_type = PT_INTERP;
   4786 	  m->count = 1;
   4787 	  m->sections[0] = s;
   4788 
   4789 	  *pm = m;
   4790 	  pm = &m->next;
   4791 	}
   4792 
   4793       /* Look through the sections.  We put sections in the same program
   4794 	 segment when the start of the second section can be placed within
   4795 	 a few bytes of the end of the first section.  */
   4796       last_hdr = NULL;
   4797       last_size = 0;
   4798       hdr_index = 0;
   4799       writable = false;
   4800       executable = false;
   4801       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   4802       if (dynsec != NULL
   4803 	  && (dynsec->flags & SEC_LOAD) == 0)
   4804 	dynsec = NULL;
   4805 
   4806       if ((abfd->flags & D_PAGED) == 0)
   4807 	phdr_in_segment = false;
   4808 
   4809       /* Deal with -Ttext or something similar such that the first section
   4810 	 is not adjacent to the program headers.  This is an
   4811 	 approximation, since at this point we don't know exactly how many
   4812 	 program headers we will need.  */
   4813       if (phdr_in_segment && count > 0)
   4814 	{
   4815 	  bfd_vma phdr_lma;  /* Bytes.  */
   4816 	  bool separate_phdr = false;
   4817 
   4818 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   4819 	  if (info != NULL
   4820 	      && info->separate_code
   4821 	      && (sections[0]->flags & SEC_CODE) != 0)
   4822 	    {
   4823 	      /* If data sections should be separate from code and
   4824 		 thus not executable, and the first section is
   4825 		 executable then put the file and program headers in
   4826 		 their own PT_LOAD.  */
   4827 	      separate_phdr = true;
   4828 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   4829 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   4830 		{
   4831 		  /* The file and program headers are currently on the
   4832 		     same page as the first section.  Put them on the
   4833 		     previous page if we can.  */
   4834 		  if (phdr_lma >= maxpagesize)
   4835 		    phdr_lma -= maxpagesize;
   4836 		  else
   4837 		    separate_phdr = false;
   4838 		}
   4839 	    }
   4840 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   4841 	      || (sections[0]->lma & addr_mask) < phdr_size)
   4842 	    /* If file and program headers would be placed at the end
   4843 	       of memory then it's probably better to omit them.  */
   4844 	    phdr_in_segment = false;
   4845 	  else if (phdr_lma < wrap_to)
   4846 	    /* If a section wraps around to where we'll be placing
   4847 	       file and program headers, then the headers will be
   4848 	       overwritten.  */
   4849 	    phdr_in_segment = false;
   4850 	  else if (separate_phdr)
   4851 	    {
   4852 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   4853 	      if (m == NULL)
   4854 		goto error_return;
   4855 	      m->p_paddr = phdr_lma * opb;
   4856 	      m->p_vaddr_offset
   4857 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   4858 	      m->p_paddr_valid = 1;
   4859 	      *pm = m;
   4860 	      pm = &m->next;
   4861 	      phdr_in_segment = false;
   4862 	    }
   4863 	}
   4864 
   4865       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   4866 	{
   4867 	  asection *hdr;
   4868 	  bool new_segment;
   4869 
   4870 	  hdr = *hdrpp;
   4871 
   4872 	  /* See if this section and the last one will fit in the same
   4873 	     segment.  */
   4874 
   4875 	  if (last_hdr == NULL)
   4876 	    {
   4877 	      /* If we don't have a segment yet, then we don't need a new
   4878 		 one (we build the last one after this loop).  */
   4879 	      new_segment = false;
   4880 	    }
   4881 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   4882 	    {
   4883 	      /* If this section has a different relation between the
   4884 		 virtual address and the load address, then we need a new
   4885 		 segment.  */
   4886 	      new_segment = true;
   4887 	    }
   4888 	  else if (hdr->lma < last_hdr->lma + last_size
   4889 		   || last_hdr->lma + last_size < last_hdr->lma)
   4890 	    {
   4891 	      /* If this section has a load address that makes it overlap
   4892 		 the previous section, then we need a new segment.  */
   4893 	      new_segment = true;
   4894 	    }
   4895 	  else if ((abfd->flags & D_PAGED) != 0
   4896 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   4897 		       == (hdr->lma & -maxpagesize)))
   4898 	    {
   4899 	      /* If we are demand paged then we can't map two disk
   4900 		 pages onto the same memory page.  */
   4901 	      new_segment = false;
   4902 	    }
   4903 	  /* In the next test we have to be careful when last_hdr->lma is close
   4904 	     to the end of the address space.  If the aligned address wraps
   4905 	     around to the start of the address space, then there are no more
   4906 	     pages left in memory and it is OK to assume that the current
   4907 	     section can be included in the current segment.  */
   4908 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4909 		    + maxpagesize > last_hdr->lma)
   4910 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   4911 		       + maxpagesize <= hdr->lma))
   4912 	    {
   4913 	      /* If putting this section in this segment would force us to
   4914 		 skip a page in the segment, then we need a new segment.  */
   4915 	      new_segment = true;
   4916 	    }
   4917 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   4918 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   4919 	    {
   4920 	      /* We don't want to put a loaded section after a
   4921 		 nonloaded (ie. bss style) section in the same segment
   4922 		 as that will force the non-loaded section to be loaded.
   4923 		 Consider .tbss sections as loaded for this purpose.  */
   4924 	      new_segment = true;
   4925 	    }
   4926 	  else if ((abfd->flags & D_PAGED) == 0)
   4927 	    {
   4928 	      /* If the file is not demand paged, which means that we
   4929 		 don't require the sections to be correctly aligned in the
   4930 		 file, then there is no other reason for a new segment.  */
   4931 	      new_segment = false;
   4932 	    }
   4933 	  else if (info != NULL
   4934 		   && info->separate_code
   4935 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   4936 	    {
   4937 	      new_segment = true;
   4938 	    }
   4939 	  else if (! writable
   4940 		   && (hdr->flags & SEC_READONLY) == 0)
   4941 	    {
   4942 	      /* We don't want to put a writable section in a read only
   4943 		 segment.  */
   4944 	      new_segment = true;
   4945 	    }
   4946 	  else
   4947 	    {
   4948 	      /* Otherwise, we can use the same segment.  */
   4949 	      new_segment = false;
   4950 	    }
   4951 
   4952 	  /* Allow interested parties a chance to override our decision.  */
   4953 	  if (last_hdr != NULL
   4954 	      && info != NULL
   4955 	      && info->callbacks->override_segment_assignment != NULL)
   4956 	    new_segment
   4957 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   4958 							      last_hdr,
   4959 							      new_segment);
   4960 
   4961 	  if (! new_segment)
   4962 	    {
   4963 	      if ((hdr->flags & SEC_READONLY) == 0)
   4964 		writable = true;
   4965 	      if ((hdr->flags & SEC_CODE) != 0)
   4966 		executable = true;
   4967 	      last_hdr = hdr;
   4968 	      /* .tbss sections effectively have zero size.  */
   4969 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   4970 	      continue;
   4971 	    }
   4972 
   4973 	  /* We need a new program segment.  We must create a new program
   4974 	     header holding all the sections from hdr_index until hdr.  */
   4975 
   4976 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   4977 	  if (m == NULL)
   4978 	    goto error_return;
   4979 
   4980 	  *pm = m;
   4981 	  pm = &m->next;
   4982 
   4983 	  if ((hdr->flags & SEC_READONLY) == 0)
   4984 	    writable = true;
   4985 	  else
   4986 	    writable = false;
   4987 
   4988 	  if ((hdr->flags & SEC_CODE) == 0)
   4989 	    executable = false;
   4990 	  else
   4991 	    executable = true;
   4992 
   4993 	  last_hdr = hdr;
   4994 	  /* .tbss sections effectively have zero size.  */
   4995 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   4996 	  hdr_index = i;
   4997 	  phdr_in_segment = false;
   4998 	}
   4999 
   5000       /* Create a final PT_LOAD program segment, but not if it's just
   5001 	 for .tbss.  */
   5002       if (last_hdr != NULL
   5003 	  && (i - hdr_index != 1
   5004 	      || !IS_TBSS (last_hdr)))
   5005 	{
   5006 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5007 	  if (m == NULL)
   5008 	    goto error_return;
   5009 
   5010 	  *pm = m;
   5011 	  pm = &m->next;
   5012 	}
   5013 
   5014       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   5015       if (dynsec != NULL)
   5016 	{
   5017 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   5018 	  if (m == NULL)
   5019 	    goto error_return;
   5020 	  *pm = m;
   5021 	  pm = &m->next;
   5022 	}
   5023 
   5024       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5025 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5026 	 because if we link together nonloadable .note sections and
   5027 	 loadable .note sections, we will generate two .note sections
   5028 	 in the output file.  */
   5029       for (s = abfd->sections; s != NULL; s = s->next)
   5030 	{
   5031 	  if ((s->flags & SEC_LOAD) != 0
   5032 	      && elf_section_type (s) == SHT_NOTE)
   5033 	    {
   5034 	      asection *s2;
   5035 	      unsigned int alignment_power = s->alignment_power;
   5036 
   5037 	      count = 1;
   5038 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5039 		{
   5040 		  if (s2->next->alignment_power == alignment_power
   5041 		      && (s2->next->flags & SEC_LOAD) != 0
   5042 		      && elf_section_type (s2->next) == SHT_NOTE
   5043 		      && align_power (s2->lma + s2->size / opb,
   5044 				      alignment_power)
   5045 		      == s2->next->lma)
   5046 		    count++;
   5047 		  else
   5048 		    break;
   5049 		}
   5050 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5051 	      amt += count * sizeof (asection *);
   5052 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5053 	      if (m == NULL)
   5054 		goto error_return;
   5055 	      m->next = NULL;
   5056 	      m->p_type = PT_NOTE;
   5057 	      m->count = count;
   5058 	      while (count > 1)
   5059 		{
   5060 		  m->sections[m->count - count--] = s;
   5061 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5062 		  s = s->next;
   5063 		}
   5064 	      m->sections[m->count - 1] = s;
   5065 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5066 	      *pm = m;
   5067 	      pm = &m->next;
   5068 	    }
   5069 	  if (s->flags & SEC_THREAD_LOCAL)
   5070 	    {
   5071 	      if (! tls_count)
   5072 		first_tls = s;
   5073 	      tls_count++;
   5074 	    }
   5075 	  if (first_mbind == NULL
   5076 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5077 	    first_mbind = s;
   5078 	}
   5079 
   5080       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5081       if (tls_count > 0)
   5082 	{
   5083 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5084 	  amt += tls_count * sizeof (asection *);
   5085 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5086 	  if (m == NULL)
   5087 	    goto error_return;
   5088 	  m->next = NULL;
   5089 	  m->p_type = PT_TLS;
   5090 	  m->count = tls_count;
   5091 	  /* Mandated PF_R.  */
   5092 	  m->p_flags = PF_R;
   5093 	  m->p_flags_valid = 1;
   5094 	  s = first_tls;
   5095 	  for (i = 0; i < tls_count; ++i)
   5096 	    {
   5097 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5098 		{
   5099 		  _bfd_error_handler
   5100 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5101 		  s = first_tls;
   5102 		  i = 0;
   5103 		  while (i < tls_count)
   5104 		    {
   5105 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5106 			{
   5107 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5108 			  i++;
   5109 			}
   5110 		      else
   5111 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5112 		      s = s->next;
   5113 		    }
   5114 		  bfd_set_error (bfd_error_bad_value);
   5115 		  goto error_return;
   5116 		}
   5117 	      m->sections[i] = s;
   5118 	      s = s->next;
   5119 	    }
   5120 
   5121 	  *pm = m;
   5122 	  pm = &m->next;
   5123 	}
   5124 
   5125       if (first_mbind
   5126 	  && (abfd->flags & D_PAGED) != 0
   5127 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5128 	for (s = first_mbind; s != NULL; s = s->next)
   5129 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5130 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5131 	    {
   5132 	      /* Mandated PF_R.  */
   5133 	      unsigned long p_flags = PF_R;
   5134 	      if ((s->flags & SEC_READONLY) == 0)
   5135 		p_flags |= PF_W;
   5136 	      if ((s->flags & SEC_CODE) != 0)
   5137 		p_flags |= PF_X;
   5138 
   5139 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5140 	      m = bfd_zalloc (abfd, amt);
   5141 	      if (m == NULL)
   5142 		goto error_return;
   5143 	      m->next = NULL;
   5144 	      m->p_type = (PT_GNU_MBIND_LO
   5145 			   + elf_section_data (s)->this_hdr.sh_info);
   5146 	      m->count = 1;
   5147 	      m->p_flags_valid = 1;
   5148 	      m->sections[0] = s;
   5149 	      m->p_flags = p_flags;
   5150 
   5151 	      *pm = m;
   5152 	      pm = &m->next;
   5153 	    }
   5154 
   5155       s = bfd_get_section_by_name (abfd,
   5156 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5157       if (s != NULL && s->size != 0)
   5158 	{
   5159 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5160 	  m = bfd_zalloc (abfd, amt);
   5161 	  if (m == NULL)
   5162 	    goto error_return;
   5163 	  m->next = NULL;
   5164 	  m->p_type = PT_GNU_PROPERTY;
   5165 	  m->count = 1;
   5166 	  m->p_flags_valid = 1;
   5167 	  m->sections[0] = s;
   5168 	  m->p_flags = PF_R;
   5169 	  *pm = m;
   5170 	  pm = &m->next;
   5171 	}
   5172 
   5173       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5174 	 segment.  */
   5175       eh_frame_hdr = elf_eh_frame_hdr (info);
   5176       if (eh_frame_hdr != NULL
   5177 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5178 	{
   5179 	  amt = sizeof (struct elf_segment_map);
   5180 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5181 	  if (m == NULL)
   5182 	    goto error_return;
   5183 	  m->next = NULL;
   5184 	  m->p_type = PT_GNU_EH_FRAME;
   5185 	  m->count = 1;
   5186 	  m->sections[0] = eh_frame_hdr->output_section;
   5187 
   5188 	  *pm = m;
   5189 	  pm = &m->next;
   5190 	}
   5191 
   5192       if (elf_stack_flags (abfd))
   5193 	{
   5194 	  amt = sizeof (struct elf_segment_map);
   5195 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5196 	  if (m == NULL)
   5197 	    goto error_return;
   5198 	  m->next = NULL;
   5199 	  m->p_type = PT_GNU_STACK;
   5200 	  m->p_flags = elf_stack_flags (abfd);
   5201 	  m->p_align = bed->stack_align;
   5202 	  m->p_flags_valid = 1;
   5203 	  m->p_align_valid = m->p_align != 0;
   5204 	  if (info->stacksize > 0)
   5205 	    {
   5206 	      m->p_size = info->stacksize;
   5207 	      m->p_size_valid = 1;
   5208 	    }
   5209 
   5210 	  *pm = m;
   5211 	  pm = &m->next;
   5212 	}
   5213 
   5214       if (info != NULL && info->relro)
   5215 	{
   5216 	  for (m = mfirst; m != NULL; m = m->next)
   5217 	    {
   5218 	      if (m->p_type == PT_LOAD
   5219 		  && m->count != 0
   5220 		  && m->sections[0]->vma >= info->relro_start
   5221 		  && m->sections[0]->vma < info->relro_end)
   5222 		{
   5223 		  i = m->count;
   5224 		  while (--i != (unsigned) -1)
   5225 		    {
   5226 		      if (m->sections[i]->size > 0
   5227 			  && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
   5228 			  == (SEC_LOAD | SEC_HAS_CONTENTS))
   5229 			break;
   5230 		    }
   5231 
   5232 		  if (i != (unsigned) -1)
   5233 		    break;
   5234 		}
   5235 	    }
   5236 
   5237 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5238 	  if (m != NULL)
   5239 	    {
   5240 	      amt = sizeof (struct elf_segment_map);
   5241 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5242 	      if (m == NULL)
   5243 		goto error_return;
   5244 	      m->next = NULL;
   5245 	      m->p_type = PT_GNU_RELRO;
   5246 	      *pm = m;
   5247 	      pm = &m->next;
   5248 	    }
   5249 	}
   5250 
   5251       free (sections);
   5252       elf_seg_map (abfd) = mfirst;
   5253     }
   5254 
   5255   if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
   5256     return false;
   5257 
   5258   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5259     ++count;
   5260   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5261 
   5262   return true;
   5263 
   5264  error_return:
   5265   free (sections);
   5266   return false;
   5267 }
   5268 
   5269 /* Sort sections by address.  */
   5270 
   5271 static int
   5272 elf_sort_sections (const void *arg1, const void *arg2)
   5273 {
   5274   const asection *sec1 = *(const asection **) arg1;
   5275   const asection *sec2 = *(const asection **) arg2;
   5276   bfd_size_type size1, size2;
   5277 
   5278   /* Sort by LMA first, since this is the address used to
   5279      place the section into a segment.  */
   5280   if (sec1->lma < sec2->lma)
   5281     return -1;
   5282   else if (sec1->lma > sec2->lma)
   5283     return 1;
   5284 
   5285   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5286      the same, and this will do nothing.  */
   5287   if (sec1->vma < sec2->vma)
   5288     return -1;
   5289   else if (sec1->vma > sec2->vma)
   5290     return 1;
   5291 
   5292   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5293 
   5294 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5295 		  && (x)->size != 0)
   5296 
   5297   if (TOEND (sec1))
   5298     {
   5299       if (!TOEND (sec2))
   5300 	return 1;
   5301     }
   5302   else if (TOEND (sec2))
   5303     return -1;
   5304 
   5305 #undef TOEND
   5306 
   5307   /* Sort by size, to put zero sized sections
   5308      before others at the same address.  */
   5309 
   5310   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5311   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5312 
   5313   if (size1 < size2)
   5314     return -1;
   5315   if (size1 > size2)
   5316     return 1;
   5317 
   5318   return sec1->target_index - sec2->target_index;
   5319 }
   5320 
   5321 /* This qsort comparison functions sorts PT_LOAD segments first and
   5322    by p_paddr, for assign_file_positions_for_load_sections.  */
   5323 
   5324 static int
   5325 elf_sort_segments (const void *arg1, const void *arg2)
   5326 {
   5327   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5328   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5329 
   5330   if (m1->p_type != m2->p_type)
   5331     {
   5332       if (m1->p_type == PT_NULL)
   5333 	return 1;
   5334       if (m2->p_type == PT_NULL)
   5335 	return -1;
   5336       return m1->p_type < m2->p_type ? -1 : 1;
   5337     }
   5338   if (m1->includes_filehdr != m2->includes_filehdr)
   5339     return m1->includes_filehdr ? -1 : 1;
   5340   if (m1->no_sort_lma != m2->no_sort_lma)
   5341     return m1->no_sort_lma ? -1 : 1;
   5342   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5343     {
   5344       bfd_vma lma1, lma2;  /* Octets.  */
   5345       lma1 = 0;
   5346       if (m1->p_paddr_valid)
   5347 	lma1 = m1->p_paddr;
   5348       else if (m1->count != 0)
   5349 	{
   5350 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5351 						  m1->sections[0]);
   5352 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5353 	}
   5354       lma2 = 0;
   5355       if (m2->p_paddr_valid)
   5356 	lma2 = m2->p_paddr;
   5357       else if (m2->count != 0)
   5358 	{
   5359 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5360 						  m2->sections[0]);
   5361 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5362 	}
   5363       if (lma1 != lma2)
   5364 	return lma1 < lma2 ? -1 : 1;
   5365     }
   5366   if (m1->idx != m2->idx)
   5367     return m1->idx < m2->idx ? -1 : 1;
   5368   return 0;
   5369 }
   5370 
   5371 /* Ian Lance Taylor writes:
   5372 
   5373    We shouldn't be using % with a negative signed number.  That's just
   5374    not good.  We have to make sure either that the number is not
   5375    negative, or that the number has an unsigned type.  When the types
   5376    are all the same size they wind up as unsigned.  When file_ptr is a
   5377    larger signed type, the arithmetic winds up as signed long long,
   5378    which is wrong.
   5379 
   5380    What we're trying to say here is something like ``increase OFF by
   5381    the least amount that will cause it to be equal to the VMA modulo
   5382    the page size.''  */
   5383 /* In other words, something like:
   5384 
   5385    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5386    off_offset = off % bed->maxpagesize;
   5387    if (vma_offset < off_offset)
   5388      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5389    else
   5390      adjustment = vma_offset - off_offset;
   5391 
   5392    which can be collapsed into the expression below.  */
   5393 
   5394 static file_ptr
   5395 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5396 {
   5397   /* PR binutils/16199: Handle an alignment of zero.  */
   5398   if (maxpagesize == 0)
   5399     maxpagesize = 1;
   5400   return ((vma - off) % maxpagesize);
   5401 }
   5402 
   5403 static void
   5404 print_segment_map (const struct elf_segment_map *m)
   5405 {
   5406   unsigned int j;
   5407   const char *pt = get_segment_type (m->p_type);
   5408   char buf[32];
   5409 
   5410   if (pt == NULL)
   5411     {
   5412       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5413 	sprintf (buf, "LOPROC+%7.7x",
   5414 		 (unsigned int) (m->p_type - PT_LOPROC));
   5415       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5416 	sprintf (buf, "LOOS+%7.7x",
   5417 		 (unsigned int) (m->p_type - PT_LOOS));
   5418       else
   5419 	snprintf (buf, sizeof (buf), "%8.8x",
   5420 		  (unsigned int) m->p_type);
   5421       pt = buf;
   5422     }
   5423   fflush (stdout);
   5424   fprintf (stderr, "%s:", pt);
   5425   for (j = 0; j < m->count; j++)
   5426     fprintf (stderr, " %s", m->sections [j]->name);
   5427   putc ('\n',stderr);
   5428   fflush (stderr);
   5429 }
   5430 
   5431 static bool
   5432 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
   5433 {
   5434   void *buf;
   5435   bool ret;
   5436 
   5437   if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   5438     return false;
   5439   buf = bfd_zmalloc (len);
   5440   if (buf == NULL)
   5441     return false;
   5442   ret = bfd_bwrite (buf, len, abfd) == len;
   5443   free (buf);
   5444   return ret;
   5445 }
   5446 
   5447 /* Assign file positions to the sections based on the mapping from
   5448    sections to segments.  This function also sets up some fields in
   5449    the file header.  */
   5450 
   5451 static bool
   5452 assign_file_positions_for_load_sections (bfd *abfd,
   5453 					 struct bfd_link_info *link_info)
   5454 {
   5455   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5456   struct elf_segment_map *m;
   5457   struct elf_segment_map *phdr_load_seg;
   5458   Elf_Internal_Phdr *phdrs;
   5459   Elf_Internal_Phdr *p;
   5460   file_ptr off;  /* Octets.  */
   5461   bfd_size_type maxpagesize;
   5462   bfd_size_type p_align;
   5463   bool p_align_p = false;
   5464   unsigned int alloc, actual;
   5465   unsigned int i, j;
   5466   struct elf_segment_map **sorted_seg_map;
   5467   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5468 
   5469   if (link_info == NULL
   5470       && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5471     return false;
   5472 
   5473   alloc = 0;
   5474   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5475     m->idx = alloc++;
   5476 
   5477   if (alloc)
   5478     {
   5479       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5480       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5481     }
   5482   else
   5483     {
   5484       /* PR binutils/12467.  */
   5485       elf_elfheader (abfd)->e_phoff = 0;
   5486       elf_elfheader (abfd)->e_phentsize = 0;
   5487     }
   5488 
   5489   elf_elfheader (abfd)->e_phnum = alloc;
   5490 
   5491   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5492     {
   5493       actual = alloc;
   5494       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5495     }
   5496   else
   5497     {
   5498       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   5499       BFD_ASSERT (elf_program_header_size (abfd)
   5500 		  == actual * bed->s->sizeof_phdr);
   5501       BFD_ASSERT (actual >= alloc);
   5502     }
   5503 
   5504   if (alloc == 0)
   5505     {
   5506       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5507       return true;
   5508     }
   5509 
   5510   /* We're writing the size in elf_program_header_size (abfd),
   5511      see assign_file_positions_except_relocs, so make sure we have
   5512      that amount allocated, with trailing space cleared.
   5513      The variable alloc contains the computed need, while
   5514      elf_program_header_size (abfd) contains the size used for the
   5515      layout.
   5516      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5517      where the layout is forced to according to a larger size in the
   5518      last iterations for the testcase ld-elf/header.  */
   5519   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   5520 			     + alloc * sizeof (*sorted_seg_map)));
   5521   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   5522   elf_tdata (abfd)->phdr = phdrs;
   5523   if (phdrs == NULL)
   5524     return false;
   5525 
   5526   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   5527     {
   5528       sorted_seg_map[j] = m;
   5529       /* If elf_segment_map is not from map_sections_to_segments, the
   5530 	 sections may not be correctly ordered.  NOTE: sorting should
   5531 	 not be done to the PT_NOTE section of a corefile, which may
   5532 	 contain several pseudo-sections artificially created by bfd.
   5533 	 Sorting these pseudo-sections breaks things badly.  */
   5534       if (m->count > 1
   5535 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5536 	       && m->p_type == PT_NOTE))
   5537 	{
   5538 	  for (i = 0; i < m->count; i++)
   5539 	    m->sections[i]->target_index = i;
   5540 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5541 		 elf_sort_sections);
   5542 	}
   5543     }
   5544   if (alloc > 1)
   5545     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   5546 	   elf_sort_segments);
   5547 
   5548   p_align = bed->p_align;
   5549   maxpagesize = 1;
   5550   if ((abfd->flags & D_PAGED) != 0)
   5551     {
   5552       if (link_info != NULL)
   5553 	maxpagesize = link_info->maxpagesize;
   5554       else
   5555 	maxpagesize = bed->maxpagesize;
   5556     }
   5557 
   5558   /* Sections must map to file offsets past the ELF file header.  */
   5559   off = bed->s->sizeof_ehdr;
   5560   /* And if one of the PT_LOAD headers doesn't include the program
   5561      headers then we'll be mapping program headers in the usual
   5562      position after the ELF file header.  */
   5563   phdr_load_seg = NULL;
   5564   for (j = 0; j < alloc; j++)
   5565     {
   5566       m = sorted_seg_map[j];
   5567       if (m->p_type != PT_LOAD)
   5568 	break;
   5569       if (m->includes_phdrs)
   5570 	{
   5571 	  phdr_load_seg = m;
   5572 	  break;
   5573 	}
   5574     }
   5575   if (phdr_load_seg == NULL)
   5576     off += actual * bed->s->sizeof_phdr;
   5577 
   5578   for (j = 0; j < alloc; j++)
   5579     {
   5580       asection **secpp;
   5581       bfd_vma off_adjust;  /* Octets.  */
   5582       bool no_contents;
   5583 
   5584       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   5585 	 number of sections with contents contributing to both p_filesz
   5586 	 and p_memsz, followed by a number of sections with no contents
   5587 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   5588 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   5589       m = sorted_seg_map[j];
   5590       p = phdrs + m->idx;
   5591       p->p_type = m->p_type;
   5592       p->p_flags = m->p_flags;
   5593 
   5594       if (m->count == 0)
   5595 	p->p_vaddr = m->p_vaddr_offset * opb;
   5596       else
   5597 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   5598 
   5599       if (m->p_paddr_valid)
   5600 	p->p_paddr = m->p_paddr;
   5601       else if (m->count == 0)
   5602 	p->p_paddr = 0;
   5603       else
   5604 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   5605 
   5606       if (p->p_type == PT_LOAD
   5607 	  && (abfd->flags & D_PAGED) != 0)
   5608 	{
   5609 	  /* p_align in demand paged PT_LOAD segments effectively stores
   5610 	     the maximum page size.  When copying an executable with
   5611 	     objcopy, we set m->p_align from the input file.  Use this
   5612 	     value for maxpagesize rather than bed->maxpagesize, which
   5613 	     may be different.  Note that we use maxpagesize for PT_TLS
   5614 	     segment alignment later in this function, so we are relying
   5615 	     on at least one PT_LOAD segment appearing before a PT_TLS
   5616 	     segment.  */
   5617 	  if (m->p_align_valid)
   5618 	    maxpagesize = m->p_align;
   5619 	  else if (p_align != 0
   5620 		   && (link_info == NULL
   5621 		       || !link_info->maxpagesize_is_set))
   5622 	    /* Set p_align to the default p_align value while laying
   5623 	       out segments aligning to the maximum page size or the
   5624 	       largest section alignment.  The run-time loader can
   5625 	       align segments to the default p_align value or the
   5626 	       maximum page size, depending on system page size.  */
   5627 	    p_align_p = true;
   5628 
   5629 	  p->p_align = maxpagesize;
   5630 	}
   5631       else if (m->p_align_valid)
   5632 	p->p_align = m->p_align;
   5633       else if (m->count == 0)
   5634 	p->p_align = 1 << bed->s->log_file_align;
   5635 
   5636       if (m == phdr_load_seg)
   5637 	{
   5638 	  if (!m->includes_filehdr)
   5639 	    p->p_offset = off;
   5640 	  off += actual * bed->s->sizeof_phdr;
   5641 	}
   5642 
   5643       no_contents = false;
   5644       off_adjust = 0;
   5645       if (p->p_type == PT_LOAD
   5646 	  && m->count > 0)
   5647 	{
   5648 	  bfd_size_type align;  /* Bytes.  */
   5649 	  unsigned int align_power = 0;
   5650 
   5651 	  if (m->p_align_valid)
   5652 	    align = p->p_align;
   5653 	  else
   5654 	    {
   5655 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5656 		{
   5657 		  unsigned int secalign;
   5658 
   5659 		  secalign = bfd_section_alignment (*secpp);
   5660 		  if (secalign > align_power)
   5661 		    align_power = secalign;
   5662 		}
   5663 	      align = (bfd_size_type) 1 << align_power;
   5664 	      if (align < maxpagesize)
   5665 		{
   5666 		  /* If a section requires alignment higher than the
   5667 		     default p_align value, don't set p_align to the
   5668 		     default p_align value.  */
   5669 		  if (align > p_align)
   5670 		    p_align_p = false;
   5671 		  align = maxpagesize;
   5672 		}
   5673 	      else
   5674 		{
   5675 		  /* If a section requires alignment higher than the
   5676 		     maximum page size, set p_align to the section
   5677 		     alignment.  */
   5678 		  p_align_p = true;
   5679 		  p_align = align;
   5680 		}
   5681 	    }
   5682 
   5683 	  for (i = 0; i < m->count; i++)
   5684 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   5685 	      /* If we aren't making room for this section, then
   5686 		 it must be SHT_NOBITS regardless of what we've
   5687 		 set via struct bfd_elf_special_section.  */
   5688 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   5689 
   5690 	  /* Find out whether this segment contains any loadable
   5691 	     sections.  */
   5692 	  no_contents = true;
   5693 	  for (i = 0; i < m->count; i++)
   5694 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   5695 	      {
   5696 		no_contents = false;
   5697 		break;
   5698 	      }
   5699 
   5700 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   5701 
   5702 	  /* Broken hardware and/or kernel require that files do not
   5703 	     map the same page with different permissions on some hppa
   5704 	     processors.  */
   5705 	  if (j != 0
   5706 	      && (abfd->flags & D_PAGED) != 0
   5707 	      && bed->no_page_alias
   5708 	      && (off & (maxpagesize - 1)) != 0
   5709 	      && ((off & -maxpagesize)
   5710 		  == ((off + off_adjust) & -maxpagesize)))
   5711 	    off_adjust += maxpagesize;
   5712 	  off += off_adjust;
   5713 	  if (no_contents)
   5714 	    {
   5715 	      /* We shouldn't need to align the segment on disk since
   5716 		 the segment doesn't need file space, but the gABI
   5717 		 arguably requires the alignment and glibc ld.so
   5718 		 checks it.  So to comply with the alignment
   5719 		 requirement but not waste file space, we adjust
   5720 		 p_offset for just this segment.  (OFF_ADJUST is
   5721 		 subtracted from OFF later.)  This may put p_offset
   5722 		 past the end of file, but that shouldn't matter.  */
   5723 	    }
   5724 	  else
   5725 	    off_adjust = 0;
   5726 	}
   5727       /* Make sure the .dynamic section is the first section in the
   5728 	 PT_DYNAMIC segment.  */
   5729       else if (p->p_type == PT_DYNAMIC
   5730 	       && m->count > 1
   5731 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   5732 	{
   5733 	  _bfd_error_handler
   5734 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   5735 	       " is not the .dynamic section"),
   5736 	     abfd);
   5737 	  bfd_set_error (bfd_error_bad_value);
   5738 	  return false;
   5739 	}
   5740       /* Set the note section type to SHT_NOTE.  */
   5741       else if (p->p_type == PT_NOTE)
   5742 	for (i = 0; i < m->count; i++)
   5743 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   5744 
   5745       if (m->includes_filehdr)
   5746 	{
   5747 	  if (!m->p_flags_valid)
   5748 	    p->p_flags |= PF_R;
   5749 	  p->p_filesz = bed->s->sizeof_ehdr;
   5750 	  p->p_memsz = bed->s->sizeof_ehdr;
   5751 	  if (p->p_type == PT_LOAD)
   5752 	    {
   5753 	      if (m->count > 0)
   5754 		{
   5755 		  if (p->p_vaddr < (bfd_vma) off
   5756 		      || (!m->p_paddr_valid
   5757 			  && p->p_paddr < (bfd_vma) off))
   5758 		    {
   5759 		      _bfd_error_handler
   5760 			(_("%pB: not enough room for program headers,"
   5761 			   " try linking with -N"),
   5762 			 abfd);
   5763 		      bfd_set_error (bfd_error_bad_value);
   5764 		      return false;
   5765 		    }
   5766 		  p->p_vaddr -= off;
   5767 		  if (!m->p_paddr_valid)
   5768 		    p->p_paddr -= off;
   5769 		}
   5770 	    }
   5771 	  else if (sorted_seg_map[0]->includes_filehdr)
   5772 	    {
   5773 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   5774 	      p->p_vaddr = filehdr->p_vaddr;
   5775 	      if (!m->p_paddr_valid)
   5776 		p->p_paddr = filehdr->p_paddr;
   5777 	    }
   5778 	}
   5779 
   5780       if (m->includes_phdrs)
   5781 	{
   5782 	  if (!m->p_flags_valid)
   5783 	    p->p_flags |= PF_R;
   5784 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   5785 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   5786 	  if (!m->includes_filehdr)
   5787 	    {
   5788 	      if (p->p_type == PT_LOAD)
   5789 		{
   5790 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   5791 		  if (m->count > 0)
   5792 		    {
   5793 		      p->p_vaddr -= off - p->p_offset;
   5794 		      if (!m->p_paddr_valid)
   5795 			p->p_paddr -= off - p->p_offset;
   5796 		    }
   5797 		}
   5798 	      else if (phdr_load_seg != NULL)
   5799 		{
   5800 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   5801 		  bfd_vma phdr_off = 0;  /* Octets.  */
   5802 		  if (phdr_load_seg->includes_filehdr)
   5803 		    phdr_off = bed->s->sizeof_ehdr;
   5804 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   5805 		  if (!m->p_paddr_valid)
   5806 		    p->p_paddr = phdr->p_paddr + phdr_off;
   5807 		  p->p_offset = phdr->p_offset + phdr_off;
   5808 		}
   5809 	      else
   5810 		p->p_offset = bed->s->sizeof_ehdr;
   5811 	    }
   5812 	}
   5813 
   5814       if (p->p_type == PT_LOAD
   5815 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   5816 	{
   5817 	  if (!m->includes_filehdr && !m->includes_phdrs)
   5818 	    {
   5819 	      p->p_offset = off;
   5820 	      if (no_contents)
   5821 		{
   5822 		  /* Put meaningless p_offset for PT_LOAD segments
   5823 		     without file contents somewhere within the first
   5824 		     page, in an attempt to not point past EOF.  */
   5825 		  bfd_size_type align = maxpagesize;
   5826 		  if (align < p->p_align)
   5827 		    align = p->p_align;
   5828 		  if (align < 1)
   5829 		    align = 1;
   5830 		  p->p_offset = off % align;
   5831 		}
   5832 	    }
   5833 	  else
   5834 	    {
   5835 	      file_ptr adjust;  /* Octets.  */
   5836 
   5837 	      adjust = off - (p->p_offset + p->p_filesz);
   5838 	      if (!no_contents)
   5839 		p->p_filesz += adjust;
   5840 	      p->p_memsz += adjust;
   5841 	    }
   5842 	}
   5843 
   5844       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   5845 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   5846 	 core files, for sections in PT_NOTE segments.
   5847 	 assign_file_positions_for_non_load_sections will set filepos
   5848 	 for other sections and update p_filesz for other segments.  */
   5849       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   5850 	{
   5851 	  asection *sec;
   5852 	  bfd_size_type align;
   5853 	  Elf_Internal_Shdr *this_hdr;
   5854 
   5855 	  sec = *secpp;
   5856 	  this_hdr = &elf_section_data (sec)->this_hdr;
   5857 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   5858 
   5859 	  if ((p->p_type == PT_LOAD
   5860 	       || p->p_type == PT_TLS)
   5861 	      && (this_hdr->sh_type != SHT_NOBITS
   5862 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   5863 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   5864 			  || p->p_type == PT_TLS))))
   5865 	    {
   5866 	      bfd_vma p_start = p->p_paddr;                /* Octets.  */
   5867 	      bfd_vma p_end = p_start + p->p_memsz;        /* Octets.  */
   5868 	      bfd_vma s_start = sec->lma * opb;            /* Octets.  */
   5869 	      bfd_vma adjust = s_start - p_end;            /* Octets.  */
   5870 
   5871 	      if (adjust != 0
   5872 		  && (s_start < p_end
   5873 		      || p_end < p_start))
   5874 		{
   5875 		  _bfd_error_handler
   5876 		    /* xgettext:c-format */
   5877 		    (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
   5878 		     abfd, sec, (uint64_t) s_start / opb,
   5879 		     (uint64_t) p_end / opb);
   5880 		  adjust = 0;
   5881 		  sec->lma = p_end / opb;
   5882 		}
   5883 	      p->p_memsz += adjust;
   5884 
   5885 	      if (p->p_type == PT_LOAD)
   5886 		{
   5887 		  if (this_hdr->sh_type != SHT_NOBITS)
   5888 		    {
   5889 		      off_adjust = 0;
   5890 		      if (p->p_filesz + adjust < p->p_memsz)
   5891 			{
   5892 			  /* We have a PROGBITS section following NOBITS ones.
   5893 			     Allocate file space for the NOBITS section(s) and
   5894 			     zero it.  */
   5895 			  adjust = p->p_memsz - p->p_filesz;
   5896 			  if (!write_zeros (abfd, off, adjust))
   5897 			    return false;
   5898 			}
   5899 		    }
   5900 		  /* We only adjust sh_offset in SHT_NOBITS sections
   5901 		     as would seem proper for their address when the
   5902 		     section is first in the segment.  sh_offset
   5903 		     doesn't really have any significance for
   5904 		     SHT_NOBITS anyway, apart from a notional position
   5905 		     relative to other sections.  Historically we
   5906 		     didn't bother with adjusting sh_offset and some
   5907 		     programs depend on it not being adjusted.  See
   5908 		     pr12921 and pr25662.  */
   5909 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   5910 		    {
   5911 		      off += adjust;
   5912 		      if (this_hdr->sh_type == SHT_NOBITS)
   5913 			off_adjust += adjust;
   5914 		    }
   5915 		}
   5916 	      if (this_hdr->sh_type != SHT_NOBITS)
   5917 		p->p_filesz += adjust;
   5918 	    }
   5919 
   5920 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   5921 	    {
   5922 	      /* The section at i == 0 is the one that actually contains
   5923 		 everything.  */
   5924 	      if (i == 0)
   5925 		{
   5926 		  this_hdr->sh_offset = sec->filepos = off;
   5927 		  off += this_hdr->sh_size;
   5928 		  p->p_filesz = this_hdr->sh_size;
   5929 		  p->p_memsz = 0;
   5930 		  p->p_align = 1;
   5931 		}
   5932 	      else
   5933 		{
   5934 		  /* The rest are fake sections that shouldn't be written.  */
   5935 		  sec->filepos = 0;
   5936 		  sec->size = 0;
   5937 		  sec->flags = 0;
   5938 		  continue;
   5939 		}
   5940 	    }
   5941 	  else
   5942 	    {
   5943 	      if (p->p_type == PT_LOAD)
   5944 		{
   5945 		  this_hdr->sh_offset = sec->filepos = off;
   5946 		  if (this_hdr->sh_type != SHT_NOBITS)
   5947 		    off += this_hdr->sh_size;
   5948 		}
   5949 	      else if (this_hdr->sh_type == SHT_NOBITS
   5950 		       && (this_hdr->sh_flags & SHF_TLS) != 0
   5951 		       && this_hdr->sh_offset == 0)
   5952 		{
   5953 		  /* This is a .tbss section that didn't get a PT_LOAD.
   5954 		     (See _bfd_elf_map_sections_to_segments "Create a
   5955 		     final PT_LOAD".)  Set sh_offset to the value it
   5956 		     would have if we had created a zero p_filesz and
   5957 		     p_memsz PT_LOAD header for the section.  This
   5958 		     also makes the PT_TLS header have the same
   5959 		     p_offset value.  */
   5960 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   5961 							  off, align);
   5962 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   5963 		}
   5964 
   5965 	      if (this_hdr->sh_type != SHT_NOBITS)
   5966 		{
   5967 		  p->p_filesz += this_hdr->sh_size;
   5968 		  /* A load section without SHF_ALLOC is something like
   5969 		     a note section in a PT_NOTE segment.  These take
   5970 		     file space but are not loaded into memory.  */
   5971 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5972 		    p->p_memsz += this_hdr->sh_size;
   5973 		}
   5974 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   5975 		{
   5976 		  if (p->p_type == PT_TLS)
   5977 		    p->p_memsz += this_hdr->sh_size;
   5978 
   5979 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   5980 		     normal segments.  */
   5981 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   5982 		    p->p_memsz += this_hdr->sh_size;
   5983 		}
   5984 
   5985 	      if (align > p->p_align
   5986 		  && !m->p_align_valid
   5987 		  && (p->p_type != PT_LOAD
   5988 		      || (abfd->flags & D_PAGED) == 0))
   5989 		p->p_align = align;
   5990 	    }
   5991 
   5992 	  if (!m->p_flags_valid)
   5993 	    {
   5994 	      p->p_flags |= PF_R;
   5995 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   5996 		p->p_flags |= PF_X;
   5997 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   5998 		p->p_flags |= PF_W;
   5999 	    }
   6000 	}
   6001 
   6002       off -= off_adjust;
   6003 
   6004       /* PR ld/20815 - Check that the program header segment, if
   6005 	 present, will be loaded into memory.  */
   6006       if (p->p_type == PT_PHDR
   6007 	  && phdr_load_seg == NULL
   6008 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   6009 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   6010 	{
   6011 	  /* The fix for this error is usually to edit the linker script being
   6012 	     used and set up the program headers manually.  Either that or
   6013 	     leave room for the headers at the start of the SECTIONS.  */
   6014 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   6015 				" by LOAD segment"),
   6016 			      abfd);
   6017 	  if (link_info == NULL)
   6018 	    return false;
   6019 	  /* Arrange for the linker to exit with an error, deleting
   6020 	     the output file unless --noinhibit-exec is given.  */
   6021 	  link_info->callbacks->info ("%X");
   6022 	}
   6023 
   6024       /* Check that all sections are in a PT_LOAD segment.
   6025 	 Don't check funky gdb generated core files.  */
   6026       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6027 	{
   6028 	  bool check_vma = true;
   6029 
   6030 	  for (i = 1; i < m->count; i++)
   6031 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6032 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6033 				       ->this_hdr), p) != 0
   6034 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6035 				       ->this_hdr), p) != 0)
   6036 	      {
   6037 		/* Looks like we have overlays packed into the segment.  */
   6038 		check_vma = false;
   6039 		break;
   6040 	      }
   6041 
   6042 	  for (i = 0; i < m->count; i++)
   6043 	    {
   6044 	      Elf_Internal_Shdr *this_hdr;
   6045 	      asection *sec;
   6046 
   6047 	      sec = m->sections[i];
   6048 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6049 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6050 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6051 		{
   6052 		  _bfd_error_handler
   6053 		    /* xgettext:c-format */
   6054 		    (_("%pB: section `%pA' can't be allocated in segment %d"),
   6055 		     abfd, sec, j);
   6056 		  print_segment_map (m);
   6057 		}
   6058 	    }
   6059 
   6060 	  if (p_align_p)
   6061 	    p->p_align = p_align;
   6062 	}
   6063     }
   6064 
   6065   elf_next_file_pos (abfd) = off;
   6066 
   6067   if (link_info != NULL
   6068       && phdr_load_seg != NULL
   6069       && phdr_load_seg->includes_filehdr)
   6070     {
   6071       /* There is a segment that contains both the file headers and the
   6072 	 program headers, so provide a symbol __ehdr_start pointing there.
   6073 	 A program can use this to examine itself robustly.  */
   6074 
   6075       struct elf_link_hash_entry *hash
   6076 	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
   6077 				false, false, true);
   6078       /* If the symbol was referenced and not defined, define it.  */
   6079       if (hash != NULL
   6080 	  && (hash->root.type == bfd_link_hash_new
   6081 	      || hash->root.type == bfd_link_hash_undefined
   6082 	      || hash->root.type == bfd_link_hash_undefweak
   6083 	      || hash->root.type == bfd_link_hash_common))
   6084 	{
   6085 	  asection *s = NULL;
   6086 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6087 
   6088 	  if (phdr_load_seg->count != 0)
   6089 	    /* The segment contains sections, so use the first one.  */
   6090 	    s = phdr_load_seg->sections[0];
   6091 	  else
   6092 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6093 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6094 	      if (m->p_type == PT_LOAD && m->count != 0)
   6095 		{
   6096 		  s = m->sections[0];
   6097 		  break;
   6098 		}
   6099 
   6100 	  if (s != NULL)
   6101 	    {
   6102 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6103 	      hash->root.u.def.section = s;
   6104 	    }
   6105 	  else
   6106 	    {
   6107 	      hash->root.u.def.value = filehdr_vaddr;
   6108 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6109 	    }
   6110 
   6111 	  hash->root.type = bfd_link_hash_defined;
   6112 	  hash->def_regular = 1;
   6113 	  hash->non_elf = 0;
   6114 	}
   6115     }
   6116 
   6117   return true;
   6118 }
   6119 
   6120 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6121    is no defined method for detecting such files, so we have to
   6122    use heuristics instead.  */
   6123 
   6124 bool
   6125 is_debuginfo_file (bfd *abfd)
   6126 {
   6127   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6128     return false;
   6129 
   6130   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6131   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6132   Elf_Internal_Shdr **headerp;
   6133 
   6134   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6135     {
   6136       Elf_Internal_Shdr *header = * headerp;
   6137 
   6138       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6139 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6140       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6141 	  && header->sh_type != SHT_NOBITS
   6142 	  && header->sh_type != SHT_NOTE)
   6143 	return false;
   6144     }
   6145 
   6146   return true;
   6147 }
   6148 
   6149 /* Assign file positions for the other sections, except for compressed debugging
   6150    and other sections assigned in _bfd_elf_assign_file_positions_for_non_load().  */
   6151 
   6152 static bool
   6153 assign_file_positions_for_non_load_sections (bfd *abfd,
   6154 					     struct bfd_link_info *link_info)
   6155 {
   6156   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6157   Elf_Internal_Shdr **i_shdrpp;
   6158   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6159   Elf_Internal_Phdr *phdrs;
   6160   Elf_Internal_Phdr *p;
   6161   struct elf_segment_map *m;
   6162   file_ptr off;
   6163   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6164   bfd_vma maxpagesize;
   6165 
   6166   if (link_info != NULL)
   6167     maxpagesize = link_info->maxpagesize;
   6168   else
   6169     maxpagesize = bed->maxpagesize;
   6170   i_shdrpp = elf_elfsections (abfd);
   6171   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6172   off = elf_next_file_pos (abfd);
   6173   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6174     {
   6175       Elf_Internal_Shdr *hdr;
   6176       bfd_vma align;
   6177 
   6178       hdr = *hdrpp;
   6179       if (hdr->bfd_section != NULL
   6180 	  && (hdr->bfd_section->filepos != 0
   6181 	      || (hdr->sh_type == SHT_NOBITS
   6182 		  && hdr->contents == NULL)))
   6183 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6184       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6185 	{
   6186 	  if (hdr->sh_size != 0
   6187 	      /* PR 24717 - debuginfo files are known to be not strictly
   6188 		 compliant with the ELF standard.  In particular they often
   6189 		 have .note.gnu.property sections that are outside of any
   6190 		 loadable segment.  This is not a problem for such files,
   6191 		 so do not warn about them.  */
   6192 	      && ! is_debuginfo_file (abfd))
   6193 	    _bfd_error_handler
   6194 	      /* xgettext:c-format */
   6195 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6196 	       abfd,
   6197 	       (hdr->bfd_section == NULL
   6198 		? "*unknown*"
   6199 		: hdr->bfd_section->name));
   6200 	  /* We don't need to page align empty sections.  */
   6201 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6202 	    align = maxpagesize;
   6203 	  else
   6204 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6205 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6206 	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
   6207 							   false);
   6208 	}
   6209       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6210 		&& hdr->bfd_section == NULL)
   6211 	       /* We don't know the offset of these sections yet: their size has
   6212 		  not been decided.  */
   6213 	       || (hdr->bfd_section != NULL
   6214 		   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
   6215 		       || (bfd_section_is_ctf (hdr->bfd_section)
   6216 			   && abfd->is_linker_output)))
   6217 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6218 	       || (elf_symtab_shndx_list (abfd) != NULL
   6219 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6220 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6221 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6222 	hdr->sh_offset = -1;
   6223       else
   6224 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6225     }
   6226   elf_next_file_pos (abfd) = off;
   6227 
   6228   /* Now that we have set the section file positions, we can set up
   6229      the file positions for the non PT_LOAD segments.  */
   6230   phdrs = elf_tdata (abfd)->phdr;
   6231   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6232     {
   6233       if (p->p_type == PT_GNU_RELRO)
   6234 	{
   6235 	  bfd_vma start, end;  /* Bytes.  */
   6236 	  bool ok;
   6237 
   6238 	  if (link_info != NULL)
   6239 	    {
   6240 	      /* During linking the range of the RELRO segment is passed
   6241 		 in link_info.  Note that there may be padding between
   6242 		 relro_start and the first RELRO section.  */
   6243 	      start = link_info->relro_start;
   6244 	      end = link_info->relro_end;
   6245 	    }
   6246 	  else if (m->count != 0)
   6247 	    {
   6248 	      if (!m->p_size_valid)
   6249 		abort ();
   6250 	      start = m->sections[0]->vma;
   6251 	      end = start + m->p_size / opb;
   6252 	    }
   6253 	  else
   6254 	    {
   6255 	      start = 0;
   6256 	      end = 0;
   6257 	    }
   6258 
   6259 	  ok = false;
   6260 	  if (start < end)
   6261 	    {
   6262 	      struct elf_segment_map *lm;
   6263 	      const Elf_Internal_Phdr *lp;
   6264 	      unsigned int i;
   6265 
   6266 	      /* Find a LOAD segment containing a section in the RELRO
   6267 		 segment.  */
   6268 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6269 		   lm != NULL;
   6270 		   lm = lm->next, lp++)
   6271 		{
   6272 		  if (lp->p_type == PT_LOAD
   6273 		      && lm->count != 0
   6274 		      && (lm->sections[lm->count - 1]->vma
   6275 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6276 			     ? lm->sections[lm->count - 1]->size / opb
   6277 			     : 0)) > start
   6278 		      && lm->sections[0]->vma < end)
   6279 		    break;
   6280 		}
   6281 
   6282 	      if (lm != NULL)
   6283 		{
   6284 		  /* Find the section starting the RELRO segment.  */
   6285 		  for (i = 0; i < lm->count; i++)
   6286 		    {
   6287 		      asection *s = lm->sections[i];
   6288 		      if (s->vma >= start
   6289 			  && s->vma < end
   6290 			  && s->size != 0)
   6291 			break;
   6292 		    }
   6293 
   6294 		  if (i < lm->count)
   6295 		    {
   6296 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6297 		      p->p_paddr = lm->sections[i]->lma * opb;
   6298 		      p->p_offset = lm->sections[i]->filepos;
   6299 		      p->p_memsz = end * opb - p->p_vaddr;
   6300 		      p->p_filesz = p->p_memsz;
   6301 
   6302 		      /* The RELRO segment typically ends a few bytes
   6303 			 into .got.plt but other layouts are possible.
   6304 			 In cases where the end does not match any
   6305 			 loaded section (for instance is in file
   6306 			 padding), trim p_filesz back to correspond to
   6307 			 the end of loaded section contents.  */
   6308 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6309 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6310 
   6311 		      /* Preserve the alignment and flags if they are
   6312 			 valid.  The gold linker generates RW/4 for
   6313 			 the PT_GNU_RELRO section.  It is better for
   6314 			 objcopy/strip to honor these attributes
   6315 			 otherwise gdb will choke when using separate
   6316 			 debug files.  */
   6317 		      if (!m->p_align_valid)
   6318 			p->p_align = 1;
   6319 		      if (!m->p_flags_valid)
   6320 			p->p_flags = PF_R;
   6321 		      ok = true;
   6322 		    }
   6323 		}
   6324 	    }
   6325 
   6326 	  if (!ok)
   6327 	    {
   6328 	      if (link_info != NULL)
   6329 		_bfd_error_handler
   6330 		  (_("%pB: warning: unable to allocate any sections to PT_GNU_RELRO segment"),
   6331 		   abfd);
   6332 	      memset (p, 0, sizeof *p);
   6333 	    }
   6334 	}
   6335       else if (p->p_type == PT_GNU_STACK)
   6336 	{
   6337 	  if (m->p_size_valid)
   6338 	    p->p_memsz = m->p_size;
   6339 	}
   6340       else if (m->count != 0)
   6341 	{
   6342 	  unsigned int i;
   6343 
   6344 	  if (p->p_type != PT_LOAD
   6345 	      && (p->p_type != PT_NOTE
   6346 		  || bfd_get_format (abfd) != bfd_core))
   6347 	    {
   6348 	      /* A user specified segment layout may include a PHDR
   6349 		 segment that overlaps with a LOAD segment...  */
   6350 	      if (p->p_type == PT_PHDR)
   6351 		{
   6352 		  m->count = 0;
   6353 		  continue;
   6354 		}
   6355 
   6356 	      if (m->includes_filehdr || m->includes_phdrs)
   6357 		{
   6358 		  /* PR 17512: file: 2195325e.  */
   6359 		  _bfd_error_handler
   6360 		    (_("%pB: error: non-load segment %d includes file header "
   6361 		       "and/or program header"),
   6362 		     abfd, (int) (p - phdrs));
   6363 		  return false;
   6364 		}
   6365 
   6366 	      p->p_filesz = 0;
   6367 	      p->p_offset = m->sections[0]->filepos;
   6368 	      for (i = m->count; i-- != 0;)
   6369 		{
   6370 		  asection *sect = m->sections[i];
   6371 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6372 		  if (hdr->sh_type != SHT_NOBITS)
   6373 		    {
   6374 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6375 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6376 			 should be the same as p_filesz.  */
   6377 		      if (p->p_type == PT_NOTE
   6378 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6379 			p->p_memsz = p->p_filesz;
   6380 		      break;
   6381 		    }
   6382 		}
   6383 	    }
   6384 	}
   6385     }
   6386 
   6387   return true;
   6388 }
   6389 
   6390 static elf_section_list *
   6391 find_section_in_list (unsigned int i, elf_section_list * list)
   6392 {
   6393   for (;list != NULL; list = list->next)
   6394     if (list->ndx == i)
   6395       break;
   6396   return list;
   6397 }
   6398 
   6399 /* Work out the file positions of all the sections.  This is called by
   6400    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6401    VMAs must be known before this is called.
   6402 
   6403    Reloc sections come in two flavours: Those processed specially as
   6404    "side-channel" data attached to a section to which they apply, and those that
   6405    bfd doesn't process as relocations.  The latter sort are stored in a normal
   6406    bfd section by bfd_section_from_shdr.  We don't consider the former sort
   6407    here, unless they form part of the loadable image.  Reloc sections not
   6408    assigned here (and compressed debugging sections and CTF sections which
   6409    nothing else in the file can rely upon) will be handled later by
   6410    assign_file_positions_for_relocs.
   6411 
   6412    We also don't set the positions of the .symtab and .strtab here.  */
   6413 
   6414 static bool
   6415 assign_file_positions_except_relocs (bfd *abfd,
   6416 				     struct bfd_link_info *link_info)
   6417 {
   6418   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6419   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6420   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6421   unsigned int alloc;
   6422 
   6423   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6424       && bfd_get_format (abfd) != bfd_core)
   6425     {
   6426       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6427       unsigned int num_sec = elf_numsections (abfd);
   6428       Elf_Internal_Shdr **hdrpp;
   6429       unsigned int i;
   6430       file_ptr off;
   6431 
   6432       /* Start after the ELF header.  */
   6433       off = i_ehdrp->e_ehsize;
   6434 
   6435       /* We are not creating an executable, which means that we are
   6436 	 not creating a program header, and that the actual order of
   6437 	 the sections in the file is unimportant.  */
   6438       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6439 	{
   6440 	  Elf_Internal_Shdr *hdr;
   6441 
   6442 	  hdr = *hdrpp;
   6443 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6444 	       && hdr->bfd_section == NULL)
   6445 	      /* Do not assign offsets for these sections yet: we don't know
   6446 		 their sizes.  */
   6447 	      || (hdr->bfd_section != NULL
   6448 		  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
   6449 		      || (bfd_section_is_ctf (hdr->bfd_section)
   6450 			  && abfd->is_linker_output)))
   6451 	      || i == elf_onesymtab (abfd)
   6452 	      || (elf_symtab_shndx_list (abfd) != NULL
   6453 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6454 	      || i == elf_strtab_sec (abfd)
   6455 	      || i == elf_shstrtab_sec (abfd))
   6456 	    {
   6457 	      hdr->sh_offset = -1;
   6458 	    }
   6459 	  else
   6460 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
   6461 	}
   6462 
   6463       elf_next_file_pos (abfd) = off;
   6464       elf_program_header_size (abfd) = 0;
   6465     }
   6466   else
   6467     {
   6468       /* Assign file positions for the loaded sections based on the
   6469 	 assignment of sections to segments.  */
   6470       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6471 	return false;
   6472 
   6473       /* And for non-load sections.  */
   6474       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6475 	return false;
   6476     }
   6477 
   6478   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   6479     return false;
   6480 
   6481   /* Write out the program headers.  */
   6482   alloc = i_ehdrp->e_phnum;
   6483   if (alloc != 0)
   6484     {
   6485       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   6486 	{
   6487 	  /* Memory resident segments with non-zero size and RWX permissions are a
   6488 	     security risk, so we generate a warning here if we are creating any.  */
   6489 	  unsigned int i;
   6490 
   6491 	  for (i = 0; i < alloc; i++)
   6492 	    {
   6493 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   6494 
   6495 	      if (phdr->p_memsz == 0)
   6496 		continue;
   6497 
   6498 	      if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
   6499 		_bfd_error_handler (_("warning: %pB has a TLS segment with execute permission"),
   6500 				    abfd);
   6501 	      else if (phdr->p_type == PT_LOAD
   6502 		       && (phdr->p_flags & (PF_R | PF_W | PF_X)) == (PF_R | PF_W | PF_X))
   6503 		_bfd_error_handler (_("warning: %pB has a LOAD segment with RWX permissions"),
   6504 				    abfd);
   6505 	    }
   6506 	}
   6507 
   6508       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   6509 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   6510 	return false;
   6511     }
   6512 
   6513   return true;
   6514 }
   6515 
   6516 bool
   6517 _bfd_elf_init_file_header (bfd *abfd,
   6518 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   6519 {
   6520   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   6521   struct elf_strtab_hash *shstrtab;
   6522   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6523 
   6524   i_ehdrp = elf_elfheader (abfd);
   6525 
   6526   shstrtab = _bfd_elf_strtab_init ();
   6527   if (shstrtab == NULL)
   6528     return false;
   6529 
   6530   elf_shstrtab (abfd) = shstrtab;
   6531 
   6532   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   6533   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   6534   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   6535   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   6536 
   6537   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   6538   i_ehdrp->e_ident[EI_DATA] =
   6539     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   6540   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   6541 
   6542   if ((abfd->flags & DYNAMIC) != 0)
   6543     i_ehdrp->e_type = ET_DYN;
   6544   else if ((abfd->flags & EXEC_P) != 0)
   6545     i_ehdrp->e_type = ET_EXEC;
   6546   else if (bfd_get_format (abfd) == bfd_core)
   6547     i_ehdrp->e_type = ET_CORE;
   6548   else
   6549     i_ehdrp->e_type = ET_REL;
   6550 
   6551   switch (bfd_get_arch (abfd))
   6552     {
   6553     case bfd_arch_unknown:
   6554       i_ehdrp->e_machine = EM_NONE;
   6555       break;
   6556 
   6557       /* There used to be a long list of cases here, each one setting
   6558 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   6559 	 in the corresponding bfd definition.  To avoid duplication,
   6560 	 the switch was removed.  Machines that need special handling
   6561 	 can generally do it in elf_backend_final_write_processing(),
   6562 	 unless they need the information earlier than the final write.
   6563 	 Such need can generally be supplied by replacing the tests for
   6564 	 e_machine with the conditions used to determine it.  */
   6565     default:
   6566       i_ehdrp->e_machine = bed->elf_machine_code;
   6567     }
   6568 
   6569   i_ehdrp->e_version = bed->s->ev_current;
   6570   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   6571 
   6572   /* No program header, for now.  */
   6573   i_ehdrp->e_phoff = 0;
   6574   i_ehdrp->e_phentsize = 0;
   6575   i_ehdrp->e_phnum = 0;
   6576 
   6577   /* Each bfd section is section header entry.  */
   6578   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   6579   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   6580 
   6581   elf_tdata (abfd)->symtab_hdr.sh_name =
   6582     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   6583   elf_tdata (abfd)->strtab_hdr.sh_name =
   6584     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   6585   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   6586     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   6587   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   6588       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   6589       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   6590     return false;
   6591 
   6592   return true;
   6593 }
   6594 
   6595 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   6596 
   6597    FIXME: We used to have code here to sort the PT_LOAD segments into
   6598    ascending order, as per the ELF spec.  But this breaks some programs,
   6599    including the Linux kernel.  But really either the spec should be
   6600    changed or the programs updated.  */
   6601 
   6602 bool
   6603 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   6604 {
   6605   if (link_info != NULL && bfd_link_pie (link_info))
   6606     {
   6607       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   6608       unsigned int num_segments = i_ehdrp->e_phnum;
   6609       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   6610       Elf_Internal_Phdr *segment = tdata->phdr;
   6611       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   6612 
   6613       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   6614       bfd_vma p_vaddr = (bfd_vma) -1;
   6615       for (; segment < end_segment; segment++)
   6616 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   6617 	  p_vaddr = segment->p_vaddr;
   6618 
   6619       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   6620 	 segments is non-zero.  */
   6621       if (p_vaddr)
   6622 	i_ehdrp->e_type = ET_EXEC;
   6623     }
   6624   return true;
   6625 }
   6626 
   6627 /* Assign file positions for all the reloc sections which are not part
   6628    of the loadable file image, and the file position of section headers.  */
   6629 
   6630 static bool
   6631 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   6632 {
   6633   file_ptr off;
   6634   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   6635   Elf_Internal_Shdr *shdrp;
   6636   Elf_Internal_Ehdr *i_ehdrp;
   6637   const struct elf_backend_data *bed;
   6638 
   6639   off = elf_next_file_pos (abfd);
   6640 
   6641   shdrpp = elf_elfsections (abfd);
   6642   end_shdrpp = shdrpp + elf_numsections (abfd);
   6643   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   6644     {
   6645       shdrp = *shdrpp;
   6646       if (shdrp->sh_offset == -1)
   6647 	{
   6648 	  asection *sec = shdrp->bfd_section;
   6649 	  bool is_rel = (shdrp->sh_type == SHT_REL
   6650 			 || shdrp->sh_type == SHT_RELA);
   6651 	  bool is_ctf = sec && bfd_section_is_ctf (sec);
   6652 	  if (is_rel
   6653 	      || is_ctf
   6654 	      || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
   6655 	    {
   6656 	      if (!is_rel && !is_ctf)
   6657 		{
   6658 		  const char *name = sec->name;
   6659 		  struct bfd_elf_section_data *d;
   6660 
   6661 		  /* Compress DWARF debug sections.  */
   6662 		  if (!bfd_compress_section (abfd, sec,
   6663 					     shdrp->contents))
   6664 		    return false;
   6665 
   6666 		  if (sec->compress_status == COMPRESS_SECTION_DONE
   6667 		      && (abfd->flags & BFD_COMPRESS_GABI) == 0)
   6668 		    {
   6669 		      /* If section is compressed with zlib-gnu, convert
   6670 			 section name from .debug_* to .zdebug_*.  */
   6671 		      char *new_name
   6672 			= convert_debug_to_zdebug (abfd, name);
   6673 		      if (new_name == NULL)
   6674 			return false;
   6675 		      name = new_name;
   6676 		    }
   6677 		  /* Add section name to section name section.  */
   6678 		  if (shdrp->sh_name != (unsigned int) -1)
   6679 		    abort ();
   6680 		  shdrp->sh_name
   6681 		    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   6682 							  name, false);
   6683 		  d = elf_section_data (sec);
   6684 
   6685 		  /* Add reloc section name to section name section.  */
   6686 		  if (d->rel.hdr
   6687 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6688 						      d->rel.hdr,
   6689 						      name, false))
   6690 		    return false;
   6691 		  if (d->rela.hdr
   6692 		      && !_bfd_elf_set_reloc_sh_name (abfd,
   6693 						      d->rela.hdr,
   6694 						      name, true))
   6695 		    return false;
   6696 
   6697 		  /* Update section size and contents.  */
   6698 		  shdrp->sh_size = sec->size;
   6699 		  shdrp->contents = sec->contents;
   6700 		  shdrp->bfd_section->contents = NULL;
   6701 		}
   6702 	      else if (is_ctf)
   6703 		{
   6704 		  /* Update section size and contents.	*/
   6705 		  shdrp->sh_size = sec->size;
   6706 		  shdrp->contents = sec->contents;
   6707 		}
   6708 
   6709 	      off = _bfd_elf_assign_file_position_for_section (shdrp,
   6710 							       off,
   6711 							       true);
   6712 	    }
   6713 	}
   6714     }
   6715 
   6716   /* Place section name section after DWARF debug sections have been
   6717      compressed.  */
   6718   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   6719   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   6720   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   6721   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
   6722 
   6723   /* Place the section headers.  */
   6724   i_ehdrp = elf_elfheader (abfd);
   6725   bed = get_elf_backend_data (abfd);
   6726   off = align_file_position (off, 1 << bed->s->log_file_align);
   6727   i_ehdrp->e_shoff = off;
   6728   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   6729   elf_next_file_pos (abfd) = off;
   6730 
   6731   return true;
   6732 }
   6733 
   6734 bool
   6735 _bfd_elf_write_object_contents (bfd *abfd)
   6736 {
   6737   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6738   Elf_Internal_Shdr **i_shdrp;
   6739   bool failed;
   6740   unsigned int count, num_sec;
   6741   struct elf_obj_tdata *t;
   6742 
   6743   if (! abfd->output_has_begun
   6744       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   6745     return false;
   6746   /* Do not rewrite ELF data when the BFD has been opened for update.
   6747      abfd->output_has_begun was set to TRUE on opening, so creation of new
   6748      sections, and modification of existing section sizes was restricted.
   6749      This means the ELF header, program headers and section headers can't have
   6750      changed.
   6751      If the contents of any sections has been modified, then those changes have
   6752      already been written to the BFD.  */
   6753   else if (abfd->direction == both_direction)
   6754     {
   6755       BFD_ASSERT (abfd->output_has_begun);
   6756       return true;
   6757     }
   6758 
   6759   i_shdrp = elf_elfsections (abfd);
   6760 
   6761   failed = false;
   6762   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   6763   if (failed)
   6764     return false;
   6765 
   6766   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   6767     return false;
   6768 
   6769   /* After writing the headers, we need to write the sections too...  */
   6770   num_sec = elf_numsections (abfd);
   6771   for (count = 1; count < num_sec; count++)
   6772     {
   6773       i_shdrp[count]->sh_name
   6774 	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   6775 				  i_shdrp[count]->sh_name);
   6776       if (bed->elf_backend_section_processing)
   6777 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   6778 	  return false;
   6779       if (i_shdrp[count]->contents)
   6780 	{
   6781 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   6782 
   6783 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   6784 	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
   6785 	    return false;
   6786 	}
   6787     }
   6788 
   6789   /* Write out the section header names.  */
   6790   t = elf_tdata (abfd);
   6791   if (elf_shstrtab (abfd) != NULL
   6792       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   6793 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   6794     return false;
   6795 
   6796   if (!(*bed->elf_backend_final_write_processing) (abfd))
   6797     return false;
   6798 
   6799   if (!bed->s->write_shdrs_and_ehdr (abfd))
   6800     return false;
   6801 
   6802   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   6803   if (t->o->build_id.after_write_object_contents != NULL
   6804       && !(*t->o->build_id.after_write_object_contents) (abfd))
   6805     return false;
   6806   if (t->o->package_metadata.after_write_object_contents != NULL
   6807       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   6808     return false;
   6809 
   6810   return true;
   6811 }
   6812 
   6813 bool
   6814 _bfd_elf_write_corefile_contents (bfd *abfd)
   6815 {
   6816   /* Hopefully this can be done just like an object file.  */
   6817   return _bfd_elf_write_object_contents (abfd);
   6818 }
   6819 
   6820 /* Given a section, search the header to find them.  */
   6821 
   6822 unsigned int
   6823 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   6824 {
   6825   const struct elf_backend_data *bed;
   6826   unsigned int sec_index;
   6827 
   6828   if (elf_section_data (asect) != NULL
   6829       && elf_section_data (asect)->this_idx != 0)
   6830     return elf_section_data (asect)->this_idx;
   6831 
   6832   if (bfd_is_abs_section (asect))
   6833     sec_index = SHN_ABS;
   6834   else if (bfd_is_com_section (asect))
   6835     sec_index = SHN_COMMON;
   6836   else if (bfd_is_und_section (asect))
   6837     sec_index = SHN_UNDEF;
   6838   else
   6839     sec_index = SHN_BAD;
   6840 
   6841   bed = get_elf_backend_data (abfd);
   6842   if (bed->elf_backend_section_from_bfd_section)
   6843     {
   6844       int retval = sec_index;
   6845 
   6846       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   6847 	return retval;
   6848     }
   6849 
   6850   if (sec_index == SHN_BAD)
   6851     bfd_set_error (bfd_error_nonrepresentable_section);
   6852 
   6853   return sec_index;
   6854 }
   6855 
   6856 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   6857    on error.  */
   6858 
   6859 int
   6860 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   6861 {
   6862   asymbol *asym_ptr = *asym_ptr_ptr;
   6863   int idx;
   6864   flagword flags = asym_ptr->flags;
   6865 
   6866   /* When gas creates relocations against local labels, it creates its
   6867      own symbol for the section, but does put the symbol into the
   6868      symbol chain, so udata is 0.  When the linker is generating
   6869      relocatable output, this section symbol may be for one of the
   6870      input sections rather than the output section.  */
   6871   if (asym_ptr->udata.i == 0
   6872       && (flags & BSF_SECTION_SYM)
   6873       && asym_ptr->section)
   6874     {
   6875       asection *sec;
   6876 
   6877       sec = asym_ptr->section;
   6878       if (sec->owner != abfd && sec->output_section != NULL)
   6879 	sec = sec->output_section;
   6880       if (sec->owner == abfd
   6881 	  && sec->index < elf_num_section_syms (abfd)
   6882 	  && elf_section_syms (abfd)[sec->index] != NULL)
   6883 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   6884     }
   6885 
   6886   idx = asym_ptr->udata.i;
   6887 
   6888   if (idx == 0)
   6889     {
   6890       /* This case can occur when using --strip-symbol on a symbol
   6891 	 which is used in a relocation entry.  */
   6892       _bfd_error_handler
   6893 	/* xgettext:c-format */
   6894 	(_("%pB: symbol `%s' required but not present"),
   6895 	 abfd, bfd_asymbol_name (asym_ptr));
   6896       bfd_set_error (bfd_error_no_symbols);
   6897       return -1;
   6898     }
   6899 
   6900 #if DEBUG & 4
   6901   {
   6902     fprintf (stderr,
   6903 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
   6904 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   6905     fflush (stderr);
   6906   }
   6907 #endif
   6908 
   6909   return idx;
   6910 }
   6911 
   6912 /* Rewrite program header information.  */
   6913 
   6914 static bool
   6915 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   6916 {
   6917   Elf_Internal_Ehdr *iehdr;
   6918   struct elf_segment_map *map;
   6919   struct elf_segment_map *map_first;
   6920   struct elf_segment_map **pointer_to_map;
   6921   Elf_Internal_Phdr *segment;
   6922   asection *section;
   6923   unsigned int i;
   6924   unsigned int num_segments;
   6925   bool phdr_included = false;
   6926   bool p_paddr_valid;
   6927   struct elf_segment_map *phdr_adjust_seg = NULL;
   6928   unsigned int phdr_adjust_num = 0;
   6929   const struct elf_backend_data *bed;
   6930   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   6931 
   6932   bed = get_elf_backend_data (ibfd);
   6933   iehdr = elf_elfheader (ibfd);
   6934 
   6935   map_first = NULL;
   6936   pointer_to_map = &map_first;
   6937 
   6938   num_segments = elf_elfheader (ibfd)->e_phnum;
   6939 
   6940   /* Returns the end address of the segment + 1.  */
   6941 #define SEGMENT_END(segment, start)					\
   6942   (start + (segment->p_memsz > segment->p_filesz			\
   6943 	    ? segment->p_memsz : segment->p_filesz))
   6944 
   6945 #define SECTION_SIZE(section, segment)					\
   6946   (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))		\
   6947     != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)			\
   6948    ? section->size : 0)
   6949 
   6950   /* Returns TRUE if the given section is contained within
   6951      the given segment.  VMA addresses are compared.  */
   6952 #define IS_CONTAINED_BY_VMA(section, segment, opb)			\
   6953   (section->vma * (opb) >= segment->p_vaddr				\
   6954    && (section->vma * (opb) + SECTION_SIZE (section, segment)		\
   6955        <= (SEGMENT_END (segment, segment->p_vaddr))))
   6956 
   6957   /* Returns TRUE if the given section is contained within
   6958      the given segment.  LMA addresses are compared.  */
   6959 #define IS_CONTAINED_BY_LMA(section, segment, base, opb)		\
   6960   (section->lma * (opb) >= base						\
   6961    && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
   6962    && (section->lma * (opb) + SECTION_SIZE (section, segment)		\
   6963        <= SEGMENT_END (segment, base)))
   6964 
   6965   /* Handle PT_NOTE segment.  */
   6966 #define IS_NOTE(p, s)							\
   6967   (p->p_type == PT_NOTE							\
   6968    && elf_section_type (s) == SHT_NOTE					\
   6969    && (bfd_vma) s->filepos >= p->p_offset				\
   6970    && ((bfd_vma) s->filepos + s->size					\
   6971        <= p->p_offset + p->p_filesz))
   6972 
   6973   /* Special case: corefile "NOTE" section containing regs, prpsinfo
   6974      etc.  */
   6975 #define IS_COREFILE_NOTE(p, s)						\
   6976   (IS_NOTE (p, s)							\
   6977    && bfd_get_format (ibfd) == bfd_core					\
   6978    && s->vma == 0							\
   6979    && s->lma == 0)
   6980 
   6981   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   6982      linker, which generates a PT_INTERP section with p_vaddr and
   6983      p_memsz set to 0.  */
   6984 #define IS_SOLARIS_PT_INTERP(p, s)					\
   6985   (p->p_vaddr == 0							\
   6986    && p->p_paddr == 0							\
   6987    && p->p_memsz == 0							\
   6988    && p->p_filesz > 0							\
   6989    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   6990    && s->size > 0							\
   6991    && (bfd_vma) s->filepos >= p->p_offset				\
   6992    && ((bfd_vma) s->filepos + s->size					\
   6993        <= p->p_offset + p->p_filesz))
   6994 
   6995   /* Decide if the given section should be included in the given segment.
   6996      A section will be included if:
   6997        1. It is within the address space of the segment -- we use the LMA
   6998 	  if that is set for the segment and the VMA otherwise,
   6999        2. It is an allocated section or a NOTE section in a PT_NOTE
   7000 	  segment.
   7001        3. There is an output section associated with it,
   7002        4. The section has not already been allocated to a previous segment.
   7003        5. PT_GNU_STACK segments do not include any sections.
   7004        6. PT_TLS segment includes only SHF_TLS sections.
   7005        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   7006        8. PT_DYNAMIC should not contain empty sections at the beginning
   7007 	  (with the possible exception of .dynamic).  */
   7008 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb)		\
   7009   ((((segment->p_paddr							\
   7010       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb)	\
   7011       : IS_CONTAINED_BY_VMA (section, segment, opb))			\
   7012      && (section->flags & SEC_ALLOC) != 0)				\
   7013     || IS_NOTE (segment, section))					\
   7014    && segment->p_type != PT_GNU_STACK					\
   7015    && (segment->p_type != PT_TLS					\
   7016        || (section->flags & SEC_THREAD_LOCAL))				\
   7017    && (segment->p_type == PT_LOAD					\
   7018        || segment->p_type == PT_TLS					\
   7019        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7020    && (segment->p_type != PT_DYNAMIC					\
   7021        || SECTION_SIZE (section, segment) > 0				\
   7022        || (segment->p_paddr						\
   7023 	   ? segment->p_paddr != section->lma * (opb)			\
   7024 	   : segment->p_vaddr != section->vma * (opb))			\
   7025        || (strcmp (bfd_section_name (section), ".dynamic") == 0))	\
   7026    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7027 
   7028 /* If the output section of a section in the input segment is NULL,
   7029    it is removed from the corresponding output segment.   */
   7030 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb)		\
   7031   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)		\
   7032    && section->output_section != NULL)
   7033 
   7034   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7035 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7036   (seg1->field >= SEGMENT_END (seg2, seg2->field))
   7037 
   7038   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7039      their VMA address ranges and their LMA address ranges overlap.
   7040      It is possible to have overlapping VMA ranges without overlapping LMA
   7041      ranges.  RedBoot images for example can have both .data and .bss mapped
   7042      to the same VMA range, but with the .data section mapped to a different
   7043      LMA.  */
   7044 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7045   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7046 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7047    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7048 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7049 
   7050   /* Initialise the segment mark field, and discard stupid alignment.  */
   7051   for (section = ibfd->sections; section != NULL; section = section->next)
   7052     {
   7053       asection *o = section->output_section;
   7054       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7055 	o->alignment_power = 0;
   7056       section->segment_mark = false;
   7057     }
   7058 
   7059   /* The Solaris linker creates program headers in which all the
   7060      p_paddr fields are zero.  When we try to objcopy or strip such a
   7061      file, we get confused.  Check for this case, and if we find it
   7062      don't set the p_paddr_valid fields.  */
   7063   p_paddr_valid = false;
   7064   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7065        i < num_segments;
   7066        i++, segment++)
   7067     if (segment->p_paddr != 0)
   7068       {
   7069 	p_paddr_valid = true;
   7070 	break;
   7071       }
   7072 
   7073   /* Scan through the segments specified in the program header
   7074      of the input BFD.  For this first scan we look for overlaps
   7075      in the loadable segments.  These can be created by weird
   7076      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7077   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7078        i < num_segments;
   7079        i++, segment++)
   7080     {
   7081       unsigned int j;
   7082       Elf_Internal_Phdr *segment2;
   7083 
   7084       if (segment->p_type == PT_INTERP)
   7085 	for (section = ibfd->sections; section; section = section->next)
   7086 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7087 	    {
   7088 	      /* Mininal change so that the normal section to segment
   7089 		 assignment code will work.  */
   7090 	      segment->p_vaddr = section->vma * opb;
   7091 	      break;
   7092 	    }
   7093 
   7094       if (segment->p_type != PT_LOAD)
   7095 	{
   7096 	  /* Remove PT_GNU_RELRO segment.  */
   7097 	  if (segment->p_type == PT_GNU_RELRO)
   7098 	    segment->p_type = PT_NULL;
   7099 	  continue;
   7100 	}
   7101 
   7102       /* Determine if this segment overlaps any previous segments.  */
   7103       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7104 	{
   7105 	  bfd_signed_vma extra_length;
   7106 
   7107 	  if (segment2->p_type != PT_LOAD
   7108 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7109 	    continue;
   7110 
   7111 	  /* Merge the two segments together.  */
   7112 	  if (segment2->p_vaddr < segment->p_vaddr)
   7113 	    {
   7114 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7115 		 SEGMENT.  */
   7116 	      extra_length = (SEGMENT_END (segment, segment->p_vaddr)
   7117 			      - SEGMENT_END (segment2, segment2->p_vaddr));
   7118 
   7119 	      if (extra_length > 0)
   7120 		{
   7121 		  segment2->p_memsz += extra_length;
   7122 		  segment2->p_filesz += extra_length;
   7123 		}
   7124 
   7125 	      segment->p_type = PT_NULL;
   7126 
   7127 	      /* Since we have deleted P we must restart the outer loop.  */
   7128 	      i = 0;
   7129 	      segment = elf_tdata (ibfd)->phdr;
   7130 	      break;
   7131 	    }
   7132 	  else
   7133 	    {
   7134 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7135 		 SEGMENT2.  */
   7136 	      extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
   7137 			      - SEGMENT_END (segment, segment->p_vaddr));
   7138 
   7139 	      if (extra_length > 0)
   7140 		{
   7141 		  segment->p_memsz += extra_length;
   7142 		  segment->p_filesz += extra_length;
   7143 		}
   7144 
   7145 	      segment2->p_type = PT_NULL;
   7146 	    }
   7147 	}
   7148     }
   7149 
   7150   /* The second scan attempts to assign sections to segments.  */
   7151   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7152        i < num_segments;
   7153        i++, segment++)
   7154     {
   7155       unsigned int section_count;
   7156       asection **sections;
   7157       asection *output_section;
   7158       unsigned int isec;
   7159       asection *matching_lma;
   7160       asection *suggested_lma;
   7161       unsigned int j;
   7162       size_t amt;
   7163       asection *first_section;
   7164 
   7165       if (segment->p_type == PT_NULL)
   7166 	continue;
   7167 
   7168       first_section = NULL;
   7169       /* Compute how many sections might be placed into this segment.  */
   7170       for (section = ibfd->sections, section_count = 0;
   7171 	   section != NULL;
   7172 	   section = section->next)
   7173 	{
   7174 	  /* Find the first section in the input segment, which may be
   7175 	     removed from the corresponding output segment.   */
   7176 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
   7177 	    {
   7178 	      if (first_section == NULL)
   7179 		first_section = section;
   7180 	      if (section->output_section != NULL)
   7181 		++section_count;
   7182 	    }
   7183 	}
   7184 
   7185       /* Allocate a segment map big enough to contain
   7186 	 all of the sections we have selected.  */
   7187       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7188       amt += section_count * sizeof (asection *);
   7189       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7190       if (map == NULL)
   7191 	return false;
   7192 
   7193       /* Initialise the fields of the segment map.  Default to
   7194 	 using the physical address of the segment in the input BFD.  */
   7195       map->next = NULL;
   7196       map->p_type = segment->p_type;
   7197       map->p_flags = segment->p_flags;
   7198       map->p_flags_valid = 1;
   7199 
   7200       if (map->p_type == PT_LOAD
   7201 	  && (ibfd->flags & D_PAGED) != 0
   7202 	  && maxpagesize > 1
   7203 	  && segment->p_align > 1)
   7204 	{
   7205 	  map->p_align = segment->p_align;
   7206 	  if (segment->p_align > maxpagesize)
   7207 	    map->p_align = maxpagesize;
   7208 	  map->p_align_valid = 1;
   7209 	}
   7210 
   7211       /* If the first section in the input segment is removed, there is
   7212 	 no need to preserve segment physical address in the corresponding
   7213 	 output segment.  */
   7214       if (!first_section || first_section->output_section != NULL)
   7215 	{
   7216 	  map->p_paddr = segment->p_paddr;
   7217 	  map->p_paddr_valid = p_paddr_valid;
   7218 	}
   7219 
   7220       /* Determine if this segment contains the ELF file header
   7221 	 and if it contains the program headers themselves.  */
   7222       map->includes_filehdr = (segment->p_offset == 0
   7223 			       && segment->p_filesz >= iehdr->e_ehsize);
   7224       map->includes_phdrs = 0;
   7225 
   7226       if (!phdr_included || segment->p_type != PT_LOAD)
   7227 	{
   7228 	  map->includes_phdrs =
   7229 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7230 	     && (segment->p_offset + segment->p_filesz
   7231 		 >= ((bfd_vma) iehdr->e_phoff
   7232 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7233 
   7234 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7235 	    phdr_included = true;
   7236 	}
   7237 
   7238       if (section_count == 0)
   7239 	{
   7240 	  /* Special segments, such as the PT_PHDR segment, may contain
   7241 	     no sections, but ordinary, loadable segments should contain
   7242 	     something.  They are allowed by the ELF spec however, so only
   7243 	     a warning is produced.
   7244 	     There is however the valid use case of embedded systems which
   7245 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7246 	     flash memory with zeros.  No warning is shown for that case.  */
   7247 	  if (segment->p_type == PT_LOAD
   7248 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7249 	    /* xgettext:c-format */
   7250 	    _bfd_error_handler
   7251 	      (_("%pB: warning: empty loadable segment detected"
   7252 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7253 	       ibfd, (uint64_t) segment->p_vaddr);
   7254 
   7255 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7256 	  map->count = 0;
   7257 	  *pointer_to_map = map;
   7258 	  pointer_to_map = &map->next;
   7259 
   7260 	  continue;
   7261 	}
   7262 
   7263       /* Now scan the sections in the input BFD again and attempt
   7264 	 to add their corresponding output sections to the segment map.
   7265 	 The problem here is how to handle an output section which has
   7266 	 been moved (ie had its LMA changed).  There are four possibilities:
   7267 
   7268 	 1. None of the sections have been moved.
   7269 	    In this case we can continue to use the segment LMA from the
   7270 	    input BFD.
   7271 
   7272 	 2. All of the sections have been moved by the same amount.
   7273 	    In this case we can change the segment's LMA to match the LMA
   7274 	    of the first section.
   7275 
   7276 	 3. Some of the sections have been moved, others have not.
   7277 	    In this case those sections which have not been moved can be
   7278 	    placed in the current segment which will have to have its size,
   7279 	    and possibly its LMA changed, and a new segment or segments will
   7280 	    have to be created to contain the other sections.
   7281 
   7282 	 4. The sections have been moved, but not by the same amount.
   7283 	    In this case we can change the segment's LMA to match the LMA
   7284 	    of the first section and we will have to create a new segment
   7285 	    or segments to contain the other sections.
   7286 
   7287 	 In order to save time, we allocate an array to hold the section
   7288 	 pointers that we are interested in.  As these sections get assigned
   7289 	 to a segment, they are removed from this array.  */
   7290 
   7291       amt = section_count * sizeof (asection *);
   7292       sections = (asection **) bfd_malloc (amt);
   7293       if (sections == NULL)
   7294 	return false;
   7295 
   7296       /* Step One: Scan for segment vs section LMA conflicts.
   7297 	 Also add the sections to the section array allocated above.
   7298 	 Also add the sections to the current segment.  In the common
   7299 	 case, where the sections have not been moved, this means that
   7300 	 we have completely filled the segment, and there is nothing
   7301 	 more to do.  */
   7302       isec = 0;
   7303       matching_lma = NULL;
   7304       suggested_lma = NULL;
   7305 
   7306       for (section = first_section, j = 0;
   7307 	   section != NULL;
   7308 	   section = section->next)
   7309 	{
   7310 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
   7311 	    {
   7312 	      output_section = section->output_section;
   7313 
   7314 	      sections[j++] = section;
   7315 
   7316 	      /* The Solaris native linker always sets p_paddr to 0.
   7317 		 We try to catch that case here, and set it to the
   7318 		 correct value.  Note - some backends require that
   7319 		 p_paddr be left as zero.  */
   7320 	      if (!p_paddr_valid
   7321 		  && segment->p_vaddr != 0
   7322 		  && !bed->want_p_paddr_set_to_zero
   7323 		  && isec == 0
   7324 		  && output_section->lma != 0
   7325 		  && (align_power (segment->p_vaddr
   7326 				   + (map->includes_filehdr
   7327 				      ? iehdr->e_ehsize : 0)
   7328 				   + (map->includes_phdrs
   7329 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7330 				      : 0),
   7331 				   output_section->alignment_power * opb)
   7332 		      == (output_section->vma * opb)))
   7333 		map->p_paddr = segment->p_vaddr;
   7334 
   7335 	      /* Match up the physical address of the segment with the
   7336 		 LMA address of the output section.  */
   7337 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
   7338 				       opb)
   7339 		  || IS_COREFILE_NOTE (segment, section)
   7340 		  || (bed->want_p_paddr_set_to_zero
   7341 		      && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
   7342 		{
   7343 		  if (matching_lma == NULL
   7344 		      || output_section->lma < matching_lma->lma)
   7345 		    matching_lma = output_section;
   7346 
   7347 		  /* We assume that if the section fits within the segment
   7348 		     then it does not overlap any other section within that
   7349 		     segment.  */
   7350 		  map->sections[isec++] = output_section;
   7351 		}
   7352 	      else if (suggested_lma == NULL)
   7353 		suggested_lma = output_section;
   7354 
   7355 	      if (j == section_count)
   7356 		break;
   7357 	    }
   7358 	}
   7359 
   7360       BFD_ASSERT (j == section_count);
   7361 
   7362       /* Step Two: Adjust the physical address of the current segment,
   7363 	 if necessary.  */
   7364       if (isec == section_count)
   7365 	{
   7366 	  /* All of the sections fitted within the segment as currently
   7367 	     specified.  This is the default case.  Add the segment to
   7368 	     the list of built segments and carry on to process the next
   7369 	     program header in the input BFD.  */
   7370 	  map->count = section_count;
   7371 	  *pointer_to_map = map;
   7372 	  pointer_to_map = &map->next;
   7373 
   7374 	  if (p_paddr_valid
   7375 	      && !bed->want_p_paddr_set_to_zero)
   7376 	    {
   7377 	      bfd_vma hdr_size = 0;
   7378 	      if (map->includes_filehdr)
   7379 		hdr_size = iehdr->e_ehsize;
   7380 	      if (map->includes_phdrs)
   7381 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7382 
   7383 	      /* Account for padding before the first section in the
   7384 		 segment.  */
   7385 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7386 				     - matching_lma->lma);
   7387 	    }
   7388 
   7389 	  free (sections);
   7390 	  continue;
   7391 	}
   7392       else
   7393 	{
   7394 	  /* Change the current segment's physical address to match
   7395 	     the LMA of the first section that fitted, or if no
   7396 	     section fitted, the first section.  */
   7397 	  if (matching_lma == NULL)
   7398 	    matching_lma = suggested_lma;
   7399 
   7400 	  map->p_paddr = matching_lma->lma * opb;
   7401 
   7402 	  /* Offset the segment physical address from the lma
   7403 	     to allow for space taken up by elf headers.  */
   7404 	  if (map->includes_phdrs)
   7405 	    {
   7406 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7407 
   7408 	      /* iehdr->e_phnum is just an estimate of the number
   7409 		 of program headers that we will need.  Make a note
   7410 		 here of the number we used and the segment we chose
   7411 		 to hold these headers, so that we can adjust the
   7412 		 offset when we know the correct value.  */
   7413 	      phdr_adjust_num = iehdr->e_phnum;
   7414 	      phdr_adjust_seg = map;
   7415 	    }
   7416 
   7417 	  if (map->includes_filehdr)
   7418 	    {
   7419 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7420 	      map->p_paddr -= iehdr->e_ehsize;
   7421 	      /* We've subtracted off the size of headers from the
   7422 		 first section lma, but there may have been some
   7423 		 alignment padding before that section too.  Try to
   7424 		 account for that by adjusting the segment lma down to
   7425 		 the same alignment.  */
   7426 	      if (segment->p_align != 0 && segment->p_align < align)
   7427 		align = segment->p_align;
   7428 	      map->p_paddr &= -(align * opb);
   7429 	    }
   7430 	}
   7431 
   7432       /* Step Three: Loop over the sections again, this time assigning
   7433 	 those that fit to the current segment and removing them from the
   7434 	 sections array; but making sure not to leave large gaps.  Once all
   7435 	 possible sections have been assigned to the current segment it is
   7436 	 added to the list of built segments and if sections still remain
   7437 	 to be assigned, a new segment is constructed before repeating
   7438 	 the loop.  */
   7439       isec = 0;
   7440       do
   7441 	{
   7442 	  map->count = 0;
   7443 	  suggested_lma = NULL;
   7444 
   7445 	  /* Fill the current segment with sections that fit.  */
   7446 	  for (j = 0; j < section_count; j++)
   7447 	    {
   7448 	      section = sections[j];
   7449 
   7450 	      if (section == NULL)
   7451 		continue;
   7452 
   7453 	      output_section = section->output_section;
   7454 
   7455 	      BFD_ASSERT (output_section != NULL);
   7456 
   7457 	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
   7458 				       opb)
   7459 		  || IS_COREFILE_NOTE (segment, section))
   7460 		{
   7461 		  if (map->count == 0)
   7462 		    {
   7463 		      /* If the first section in a segment does not start at
   7464 			 the beginning of the segment, then something is
   7465 			 wrong.  */
   7466 		      if (align_power (map->p_paddr
   7467 				       + (map->includes_filehdr
   7468 					  ? iehdr->e_ehsize : 0)
   7469 				       + (map->includes_phdrs
   7470 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7471 					  : 0),
   7472 				       output_section->alignment_power * opb)
   7473 			  != output_section->lma * opb)
   7474 			goto sorry;
   7475 		    }
   7476 		  else
   7477 		    {
   7478 		      asection *prev_sec;
   7479 
   7480 		      prev_sec = map->sections[map->count - 1];
   7481 
   7482 		      /* If the gap between the end of the previous section
   7483 			 and the start of this section is more than
   7484 			 maxpagesize then we need to start a new segment.  */
   7485 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7486 				      maxpagesize)
   7487 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7488 			  || (prev_sec->lma + prev_sec->size
   7489 			      > output_section->lma))
   7490 			{
   7491 			  if (suggested_lma == NULL)
   7492 			    suggested_lma = output_section;
   7493 
   7494 			  continue;
   7495 			}
   7496 		    }
   7497 
   7498 		  map->sections[map->count++] = output_section;
   7499 		  ++isec;
   7500 		  sections[j] = NULL;
   7501 		  if (segment->p_type == PT_LOAD)
   7502 		    section->segment_mark = true;
   7503 		}
   7504 	      else if (suggested_lma == NULL)
   7505 		suggested_lma = output_section;
   7506 	    }
   7507 
   7508 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   7509 	     be assigned to any segment - because for example they have a
   7510 	     negative size - or segments that do not contain any sections.
   7511 	     But there are also valid reasons why a segment can be empty.
   7512 	     So allow a count of zero.  */
   7513 
   7514 	  /* Add the current segment to the list of built segments.  */
   7515 	  *pointer_to_map = map;
   7516 	  pointer_to_map = &map->next;
   7517 
   7518 	  if (isec < section_count)
   7519 	    {
   7520 	      /* We still have not allocated all of the sections to
   7521 		 segments.  Create a new segment here, initialise it
   7522 		 and carry on looping.  */
   7523 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7524 	      amt += section_count * sizeof (asection *);
   7525 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7526 	      if (map == NULL)
   7527 		{
   7528 		  free (sections);
   7529 		  return false;
   7530 		}
   7531 
   7532 	      /* Initialise the fields of the segment map.  Set the physical
   7533 		 physical address to the LMA of the first section that has
   7534 		 not yet been assigned.  */
   7535 	      map->next = NULL;
   7536 	      map->p_type = segment->p_type;
   7537 	      map->p_flags = segment->p_flags;
   7538 	      map->p_flags_valid = 1;
   7539 	      map->p_paddr = suggested_lma->lma * opb;
   7540 	      map->p_paddr_valid = p_paddr_valid;
   7541 	      map->includes_filehdr = 0;
   7542 	      map->includes_phdrs = 0;
   7543 	    }
   7544 
   7545 	  continue;
   7546 	sorry:
   7547 	  bfd_set_error (bfd_error_sorry);
   7548 	  free (sections);
   7549 	  return false;
   7550 	}
   7551       while (isec < section_count);
   7552 
   7553       free (sections);
   7554     }
   7555 
   7556   elf_seg_map (obfd) = map_first;
   7557 
   7558   /* If we had to estimate the number of program headers that were
   7559      going to be needed, then check our estimate now and adjust
   7560      the offset if necessary.  */
   7561   if (phdr_adjust_seg != NULL)
   7562     {
   7563       unsigned int count;
   7564 
   7565       for (count = 0, map = map_first; map != NULL; map = map->next)
   7566 	count++;
   7567 
   7568       if (count > phdr_adjust_num)
   7569 	phdr_adjust_seg->p_paddr
   7570 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   7571 
   7572       for (map = map_first; map != NULL; map = map->next)
   7573 	if (map->p_type == PT_PHDR)
   7574 	  {
   7575 	    bfd_vma adjust
   7576 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   7577 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   7578 	    break;
   7579 	  }
   7580     }
   7581 
   7582 #undef SEGMENT_END
   7583 #undef SECTION_SIZE
   7584 #undef IS_CONTAINED_BY_VMA
   7585 #undef IS_CONTAINED_BY_LMA
   7586 #undef IS_NOTE
   7587 #undef IS_COREFILE_NOTE
   7588 #undef IS_SOLARIS_PT_INTERP
   7589 #undef IS_SECTION_IN_INPUT_SEGMENT
   7590 #undef INCLUDE_SECTION_IN_SEGMENT
   7591 #undef SEGMENT_AFTER_SEGMENT
   7592 #undef SEGMENT_OVERLAPS
   7593   return true;
   7594 }
   7595 
   7596 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   7597 
   7598 static bool
   7599 elf_is_p_align_valid (bfd *abfd)
   7600 {
   7601   unsigned int i;
   7602   Elf_Internal_Phdr *segment;
   7603   unsigned int num_segments;
   7604   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7605   bfd_size_type maxpagesize = bed->maxpagesize;
   7606   bfd_size_type p_align = bed->p_align;
   7607 
   7608   /* Return true if the default p_align value isn't set or the maximum
   7609      page size is the same as the minimum page size.  */
   7610   if (p_align == 0 || maxpagesize == bed->minpagesize)
   7611     return true;
   7612 
   7613   /* When the default p_align value is set, p_align may be set to the
   7614      default p_align value while segments are aligned to the maximum
   7615      page size.  In this case, the input p_align will be ignored and
   7616      the maximum page size will be used to align the output segments.  */
   7617   segment = elf_tdata (abfd)->phdr;
   7618   num_segments = elf_elfheader (abfd)->e_phnum;
   7619   for (i = 0; i < num_segments; i++, segment++)
   7620     if (segment->p_type == PT_LOAD
   7621 	&& (segment->p_align != p_align
   7622 	    || vma_page_aligned_bias (segment->p_vaddr,
   7623 				      segment->p_offset,
   7624 				      maxpagesize) != 0))
   7625       return true;
   7626 
   7627   return false;
   7628 }
   7629 
   7630 /* Copy ELF program header information.  */
   7631 
   7632 static bool
   7633 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   7634 {
   7635   Elf_Internal_Ehdr *iehdr;
   7636   struct elf_segment_map *map;
   7637   struct elf_segment_map *map_first;
   7638   struct elf_segment_map **pointer_to_map;
   7639   Elf_Internal_Phdr *segment;
   7640   unsigned int i;
   7641   unsigned int num_segments;
   7642   bool phdr_included = false;
   7643   bool p_paddr_valid;
   7644   bool p_palign_valid;
   7645   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7646 
   7647   iehdr = elf_elfheader (ibfd);
   7648 
   7649   map_first = NULL;
   7650   pointer_to_map = &map_first;
   7651 
   7652   /* If all the segment p_paddr fields are zero, don't set
   7653      map->p_paddr_valid.  */
   7654   p_paddr_valid = false;
   7655   num_segments = elf_elfheader (ibfd)->e_phnum;
   7656   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7657        i < num_segments;
   7658        i++, segment++)
   7659     if (segment->p_paddr != 0)
   7660       {
   7661 	p_paddr_valid = true;
   7662 	break;
   7663       }
   7664 
   7665   p_palign_valid = elf_is_p_align_valid (ibfd);
   7666 
   7667   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7668        i < num_segments;
   7669        i++, segment++)
   7670     {
   7671       asection *section;
   7672       unsigned int section_count;
   7673       size_t amt;
   7674       Elf_Internal_Shdr *this_hdr;
   7675       asection *first_section = NULL;
   7676       asection *lowest_section;
   7677 
   7678       /* Compute how many sections are in this segment.  */
   7679       for (section = ibfd->sections, section_count = 0;
   7680 	   section != NULL;
   7681 	   section = section->next)
   7682 	{
   7683 	  this_hdr = &(elf_section_data(section)->this_hdr);
   7684 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7685 	    {
   7686 	      if (first_section == NULL)
   7687 		first_section = section;
   7688 	      section_count++;
   7689 	    }
   7690 	}
   7691 
   7692       /* Allocate a segment map big enough to contain
   7693 	 all of the sections we have selected.  */
   7694       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7695       amt += section_count * sizeof (asection *);
   7696       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7697       if (map == NULL)
   7698 	return false;
   7699 
   7700       /* Initialize the fields of the output segment map with the
   7701 	 input segment.  */
   7702       map->next = NULL;
   7703       map->p_type = segment->p_type;
   7704       map->p_flags = segment->p_flags;
   7705       map->p_flags_valid = 1;
   7706       map->p_paddr = segment->p_paddr;
   7707       map->p_paddr_valid = p_paddr_valid;
   7708       map->p_align = segment->p_align;
   7709       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   7710       map->p_align_valid = (map->p_type == PT_GNU_STACK
   7711 			    || p_palign_valid);
   7712       map->p_vaddr_offset = 0;
   7713 
   7714       if (map->p_type == PT_GNU_RELRO
   7715 	  || map->p_type == PT_GNU_STACK)
   7716 	{
   7717 	  /* The PT_GNU_RELRO segment may contain the first a few
   7718 	     bytes in the .got.plt section even if the whole .got.plt
   7719 	     section isn't in the PT_GNU_RELRO segment.  We won't
   7720 	     change the size of the PT_GNU_RELRO segment.
   7721 	     Similarly, PT_GNU_STACK size is significant on uclinux
   7722 	     systems.    */
   7723 	  map->p_size = segment->p_memsz;
   7724 	  map->p_size_valid = 1;
   7725 	}
   7726 
   7727       /* Determine if this segment contains the ELF file header
   7728 	 and if it contains the program headers themselves.  */
   7729       map->includes_filehdr = (segment->p_offset == 0
   7730 			       && segment->p_filesz >= iehdr->e_ehsize);
   7731 
   7732       map->includes_phdrs = 0;
   7733       if (! phdr_included || segment->p_type != PT_LOAD)
   7734 	{
   7735 	  map->includes_phdrs =
   7736 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7737 	     && (segment->p_offset + segment->p_filesz
   7738 		 >= ((bfd_vma) iehdr->e_phoff
   7739 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7740 
   7741 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7742 	    phdr_included = true;
   7743 	}
   7744 
   7745       lowest_section = NULL;
   7746       if (section_count != 0)
   7747 	{
   7748 	  unsigned int isec = 0;
   7749 
   7750 	  for (section = first_section;
   7751 	       section != NULL;
   7752 	       section = section->next)
   7753 	    {
   7754 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7755 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7756 		{
   7757 		  map->sections[isec++] = section->output_section;
   7758 		  if ((section->flags & SEC_ALLOC) != 0)
   7759 		    {
   7760 		      bfd_vma seg_off;
   7761 
   7762 		      if (lowest_section == NULL
   7763 			  || section->lma < lowest_section->lma)
   7764 			lowest_section = section;
   7765 
   7766 		      /* Section lmas are set up from PT_LOAD header
   7767 			 p_paddr in _bfd_elf_make_section_from_shdr.
   7768 			 If this header has a p_paddr that disagrees
   7769 			 with the section lma, flag the p_paddr as
   7770 			 invalid.  */
   7771 		      if ((section->flags & SEC_LOAD) != 0)
   7772 			seg_off = this_hdr->sh_offset - segment->p_offset;
   7773 		      else
   7774 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   7775 		      if (section->lma * opb - segment->p_paddr != seg_off)
   7776 			map->p_paddr_valid = false;
   7777 		    }
   7778 		  if (isec == section_count)
   7779 		    break;
   7780 		}
   7781 	    }
   7782 	}
   7783 
   7784       if (section_count == 0)
   7785 	map->p_vaddr_offset = segment->p_vaddr / opb;
   7786       else if (map->p_paddr_valid)
   7787 	{
   7788 	  /* Account for padding before the first section in the segment.  */
   7789 	  bfd_vma hdr_size = 0;
   7790 	  if (map->includes_filehdr)
   7791 	    hdr_size = iehdr->e_ehsize;
   7792 	  if (map->includes_phdrs)
   7793 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7794 
   7795 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7796 				 - (lowest_section ? lowest_section->lma : 0));
   7797 	}
   7798 
   7799       map->count = section_count;
   7800       *pointer_to_map = map;
   7801       pointer_to_map = &map->next;
   7802     }
   7803 
   7804   elf_seg_map (obfd) = map_first;
   7805   return true;
   7806 }
   7807 
   7808 /* Copy private BFD data.  This copies or rewrites ELF program header
   7809    information.  */
   7810 
   7811 static bool
   7812 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   7813 {
   7814   bfd_vma maxpagesize;
   7815 
   7816   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   7817       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   7818     return true;
   7819 
   7820   if (elf_tdata (ibfd)->phdr == NULL)
   7821     return true;
   7822 
   7823   if (ibfd->xvec == obfd->xvec)
   7824     {
   7825       /* Check to see if any sections in the input BFD
   7826 	 covered by ELF program header have changed.  */
   7827       Elf_Internal_Phdr *segment;
   7828       asection *section, *osec;
   7829       unsigned int i, num_segments;
   7830       Elf_Internal_Shdr *this_hdr;
   7831       const struct elf_backend_data *bed;
   7832 
   7833       bed = get_elf_backend_data (ibfd);
   7834 
   7835       /* Regenerate the segment map if p_paddr is set to 0.  */
   7836       if (bed->want_p_paddr_set_to_zero)
   7837 	goto rewrite;
   7838 
   7839       /* Initialize the segment mark field.  */
   7840       for (section = obfd->sections; section != NULL;
   7841 	   section = section->next)
   7842 	section->segment_mark = false;
   7843 
   7844       num_segments = elf_elfheader (ibfd)->e_phnum;
   7845       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7846 	   i < num_segments;
   7847 	   i++, segment++)
   7848 	{
   7849 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   7850 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   7851 	     which severly confuses things, so always regenerate the segment
   7852 	     map in this case.  */
   7853 	  if (segment->p_paddr == 0
   7854 	      && segment->p_memsz == 0
   7855 	      && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
   7856 	    goto rewrite;
   7857 
   7858 	  for (section = ibfd->sections;
   7859 	       section != NULL; section = section->next)
   7860 	    {
   7861 	      /* We mark the output section so that we know it comes
   7862 		 from the input BFD.  */
   7863 	      osec = section->output_section;
   7864 	      if (osec)
   7865 		osec->segment_mark = true;
   7866 
   7867 	      /* Check if this section is covered by the segment.  */
   7868 	      this_hdr = &(elf_section_data(section)->this_hdr);
   7869 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   7870 		{
   7871 		  /* FIXME: Check if its output section is changed or
   7872 		     removed.  What else do we need to check?  */
   7873 		  if (osec == NULL
   7874 		      || section->flags != osec->flags
   7875 		      || section->lma != osec->lma
   7876 		      || section->vma != osec->vma
   7877 		      || section->size != osec->size
   7878 		      || section->rawsize != osec->rawsize
   7879 		      || section->alignment_power != osec->alignment_power)
   7880 		    goto rewrite;
   7881 		}
   7882 	    }
   7883 	}
   7884 
   7885       /* Check to see if any output section do not come from the
   7886 	 input BFD.  */
   7887       for (section = obfd->sections; section != NULL;
   7888 	   section = section->next)
   7889 	{
   7890 	  if (!section->segment_mark)
   7891 	    goto rewrite;
   7892 	  else
   7893 	    section->segment_mark = false;
   7894 	}
   7895 
   7896       return copy_elf_program_header (ibfd, obfd);
   7897     }
   7898 
   7899  rewrite:
   7900   maxpagesize = 0;
   7901   if (ibfd->xvec == obfd->xvec)
   7902     {
   7903       /* When rewriting program header, set the output maxpagesize to
   7904 	 the maximum alignment of input PT_LOAD segments.  */
   7905       Elf_Internal_Phdr *segment;
   7906       unsigned int i;
   7907       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   7908 
   7909       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7910 	   i < num_segments;
   7911 	   i++, segment++)
   7912 	if (segment->p_type == PT_LOAD
   7913 	    && maxpagesize < segment->p_align)
   7914 	  {
   7915 	    /* PR 17512: file: f17299af.  */
   7916 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   7917 	      /* xgettext:c-format */
   7918 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   7919 				    PRIx64 " is too large"),
   7920 				  ibfd, (uint64_t) segment->p_align);
   7921 	    else
   7922 	      maxpagesize = segment->p_align;
   7923 	  }
   7924     }
   7925   if (maxpagesize == 0)
   7926     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   7927 
   7928   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   7929 }
   7930 
   7931 /* Initialize private output section information from input section.  */
   7932 
   7933 bool
   7934 _bfd_elf_init_private_section_data (bfd *ibfd,
   7935 				    asection *isec,
   7936 				    bfd *obfd,
   7937 				    asection *osec,
   7938 				    struct bfd_link_info *link_info)
   7939 
   7940 {
   7941   Elf_Internal_Shdr *ihdr, *ohdr;
   7942   bool final_link = (link_info != NULL
   7943 		     && !bfd_link_relocatable (link_info));
   7944 
   7945   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   7946       || obfd->xvec->flavour != bfd_target_elf_flavour)
   7947     return true;
   7948 
   7949   BFD_ASSERT (elf_section_data (osec) != NULL);
   7950 
   7951   /* If this is a known ABI section, ELF section type and flags may
   7952      have been set up when OSEC was created.  For normal sections we
   7953      allow the user to override the type and flags other than
   7954      SHF_MASKOS and SHF_MASKPROC.  */
   7955   if (elf_section_type (osec) == SHT_PROGBITS
   7956       || elf_section_type (osec) == SHT_NOTE
   7957       || elf_section_type (osec) == SHT_NOBITS)
   7958     elf_section_type (osec) = SHT_NULL;
   7959   /* For objcopy and relocatable link, copy the ELF section type from
   7960      the input file if the BFD section flags are the same.  (If they
   7961      are different the user may be doing something like
   7962      "objcopy --set-section-flags .text=alloc,data".)  For a final
   7963      link allow some flags that the linker clears to differ.  */
   7964   if (elf_section_type (osec) == SHT_NULL
   7965       && (osec->flags == isec->flags
   7966 	  || (final_link
   7967 	      && ((osec->flags ^ isec->flags)
   7968 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   7969     elf_section_type (osec) = elf_section_type (isec);
   7970 
   7971   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   7972   elf_section_flags (osec) = (elf_section_flags (isec)
   7973 			      & (SHF_MASKOS | SHF_MASKPROC));
   7974 
   7975   /* Copy sh_info from input for mbind section.  */
   7976   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   7977       && elf_section_flags (isec) & SHF_GNU_MBIND)
   7978     elf_section_data (osec)->this_hdr.sh_info
   7979       = elf_section_data (isec)->this_hdr.sh_info;
   7980 
   7981   /* Set things up for objcopy and relocatable link.  The output
   7982      SHT_GROUP section will have its elf_next_in_group pointing back
   7983      to the input group members.  Ignore linker created group section.
   7984      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   7985   if ((link_info == NULL
   7986        || !link_info->resolve_section_groups)
   7987       && (elf_sec_group (isec) == NULL
   7988 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   7989     {
   7990       if (elf_section_flags (isec) & SHF_GROUP)
   7991 	elf_section_flags (osec) |= SHF_GROUP;
   7992       elf_next_in_group (osec) = elf_next_in_group (isec);
   7993       elf_section_data (osec)->group = elf_section_data (isec)->group;
   7994     }
   7995 
   7996   /* If not decompress, preserve SHF_COMPRESSED.  */
   7997   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   7998     elf_section_flags (osec) |= (elf_section_flags (isec)
   7999 				 & SHF_COMPRESSED);
   8000 
   8001   ihdr = &elf_section_data (isec)->this_hdr;
   8002 
   8003   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   8004      don't use the output section of the linked-to section since it
   8005      may be NULL at this point.  */
   8006   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   8007     {
   8008       ohdr = &elf_section_data (osec)->this_hdr;
   8009       ohdr->sh_flags |= SHF_LINK_ORDER;
   8010       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   8011     }
   8012 
   8013   osec->use_rela_p = isec->use_rela_p;
   8014 
   8015   return true;
   8016 }
   8017 
   8018 /* Copy private section information.  This copies over the entsize
   8019    field, and sometimes the info field.  */
   8020 
   8021 bool
   8022 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8023 				    asection *isec,
   8024 				    bfd *obfd,
   8025 				    asection *osec)
   8026 {
   8027   Elf_Internal_Shdr *ihdr, *ohdr;
   8028 
   8029   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8030       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8031     return true;
   8032 
   8033   ihdr = &elf_section_data (isec)->this_hdr;
   8034   ohdr = &elf_section_data (osec)->this_hdr;
   8035 
   8036   ohdr->sh_entsize = ihdr->sh_entsize;
   8037 
   8038   if (ihdr->sh_type == SHT_SYMTAB
   8039       || ihdr->sh_type == SHT_DYNSYM
   8040       || ihdr->sh_type == SHT_GNU_verneed
   8041       || ihdr->sh_type == SHT_GNU_verdef)
   8042     ohdr->sh_info = ihdr->sh_info;
   8043 
   8044   return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
   8045 					     NULL);
   8046 }
   8047 
   8048 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8049    necessary if we are removing either the SHT_GROUP section or any of
   8050    the group member sections.  DISCARDED is the value that a section's
   8051    output_section has if the section will be discarded, NULL when this
   8052    function is called from objcopy, bfd_abs_section_ptr when called
   8053    from the linker.  */
   8054 
   8055 bool
   8056 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8057 {
   8058   asection *isec;
   8059 
   8060   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8061     if (elf_section_type (isec) == SHT_GROUP)
   8062       {
   8063 	asection *first = elf_next_in_group (isec);
   8064 	asection *s = first;
   8065 	bfd_size_type removed = 0;
   8066 
   8067 	while (s != NULL)
   8068 	  {
   8069 	    /* If this member section is being output but the
   8070 	       SHT_GROUP section is not, then clear the group info
   8071 	       set up by _bfd_elf_copy_private_section_data.  */
   8072 	    if (s->output_section != discarded
   8073 		&& isec->output_section == discarded)
   8074 	      {
   8075 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8076 		elf_group_name (s->output_section) = NULL;
   8077 	      }
   8078 	    else
   8079 	      {
   8080 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8081 		if (s->output_section == discarded
   8082 		    && isec->output_section != discarded)
   8083 		  {
   8084 		    /* Conversely, if the member section is not being
   8085 		       output but the SHT_GROUP section is, then adjust
   8086 		       its size.  */
   8087 		    removed += 4;
   8088 		    if (elf_sec->rel.hdr != NULL
   8089 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8090 		      removed += 4;
   8091 		    if (elf_sec->rela.hdr != NULL
   8092 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8093 		      removed += 4;
   8094 		  }
   8095 		else
   8096 		  {
   8097 		    /* Also adjust for zero-sized relocation member
   8098 		       section.  */
   8099 		    if (elf_sec->rel.hdr != NULL
   8100 			&& elf_sec->rel.hdr->sh_size == 0)
   8101 		      removed += 4;
   8102 		    if (elf_sec->rela.hdr != NULL
   8103 			&& elf_sec->rela.hdr->sh_size == 0)
   8104 		      removed += 4;
   8105 		  }
   8106 	      }
   8107 	    s = elf_next_in_group (s);
   8108 	    if (s == first)
   8109 	      break;
   8110 	  }
   8111 	if (removed != 0)
   8112 	  {
   8113 	    if (discarded != NULL)
   8114 	      {
   8115 		/* If we've been called for ld -r, then we need to
   8116 		   adjust the input section size.  */
   8117 		if (isec->rawsize == 0)
   8118 		  isec->rawsize = isec->size;
   8119 		isec->size = isec->rawsize - removed;
   8120 		if (isec->size <= 4)
   8121 		  {
   8122 		    isec->size = 0;
   8123 		    isec->flags |= SEC_EXCLUDE;
   8124 		  }
   8125 	      }
   8126 	    else if (isec->output_section != NULL)
   8127 	      {
   8128 		/* Adjust the output section size when called from
   8129 		   objcopy. */
   8130 		isec->output_section->size -= removed;
   8131 		if (isec->output_section->size <= 4)
   8132 		  {
   8133 		    isec->output_section->size = 0;
   8134 		    isec->output_section->flags |= SEC_EXCLUDE;
   8135 		  }
   8136 	      }
   8137 	  }
   8138       }
   8139 
   8140   return true;
   8141 }
   8142 
   8143 /* Copy private header information.  */
   8144 
   8145 bool
   8146 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8147 {
   8148   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8149       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8150     return true;
   8151 
   8152   /* Copy over private BFD data if it has not already been copied.
   8153      This must be done here, rather than in the copy_private_bfd_data
   8154      entry point, because the latter is called after the section
   8155      contents have been set, which means that the program headers have
   8156      already been worked out.  */
   8157   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8158     {
   8159       if (! copy_private_bfd_data (ibfd, obfd))
   8160 	return false;
   8161     }
   8162 
   8163   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8164 }
   8165 
   8166 /* Copy private symbol information.  If this symbol is in a section
   8167    which we did not map into a BFD section, try to map the section
   8168    index correctly.  We use special macro definitions for the mapped
   8169    section indices; these definitions are interpreted by the
   8170    swap_out_syms function.  */
   8171 
   8172 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8173 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8174 #define MAP_STRTAB    (SHN_HIOS + 3)
   8175 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8176 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8177 
   8178 bool
   8179 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8180 				   asymbol *isymarg,
   8181 				   bfd *obfd,
   8182 				   asymbol *osymarg)
   8183 {
   8184   elf_symbol_type *isym, *osym;
   8185 
   8186   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8187       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8188     return true;
   8189 
   8190   isym = elf_symbol_from (isymarg);
   8191   osym = elf_symbol_from (osymarg);
   8192 
   8193   if (isym != NULL
   8194       && isym->internal_elf_sym.st_shndx != 0
   8195       && osym != NULL
   8196       && bfd_is_abs_section (isym->symbol.section))
   8197     {
   8198       unsigned int shndx;
   8199 
   8200       shndx = isym->internal_elf_sym.st_shndx;
   8201       if (shndx == elf_onesymtab (ibfd))
   8202 	shndx = MAP_ONESYMTAB;
   8203       else if (shndx == elf_dynsymtab (ibfd))
   8204 	shndx = MAP_DYNSYMTAB;
   8205       else if (shndx == elf_strtab_sec (ibfd))
   8206 	shndx = MAP_STRTAB;
   8207       else if (shndx == elf_shstrtab_sec (ibfd))
   8208 	shndx = MAP_SHSTRTAB;
   8209       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8210 	shndx = MAP_SYM_SHNDX;
   8211       osym->internal_elf_sym.st_shndx = shndx;
   8212     }
   8213 
   8214   return true;
   8215 }
   8216 
   8217 /* Swap out the symbols.  */
   8218 
   8219 static bool
   8220 swap_out_syms (bfd *abfd,
   8221 	       struct elf_strtab_hash **sttp,
   8222 	       int relocatable_p,
   8223 	       struct bfd_link_info *info)
   8224 {
   8225   const struct elf_backend_data *bed;
   8226   unsigned int symcount;
   8227   asymbol **syms;
   8228   struct elf_strtab_hash *stt;
   8229   Elf_Internal_Shdr *symtab_hdr;
   8230   Elf_Internal_Shdr *symtab_shndx_hdr;
   8231   Elf_Internal_Shdr *symstrtab_hdr;
   8232   struct elf_sym_strtab *symstrtab;
   8233   bfd_byte *outbound_syms;
   8234   bfd_byte *outbound_shndx;
   8235   unsigned long outbound_syms_index;
   8236   unsigned int idx;
   8237   unsigned int num_locals;
   8238   size_t amt;
   8239   bool name_local_sections;
   8240 
   8241   if (!elf_map_symbols (abfd, &num_locals))
   8242     return false;
   8243 
   8244   /* Dump out the symtabs.  */
   8245   stt = _bfd_elf_strtab_init ();
   8246   if (stt == NULL)
   8247     return false;
   8248 
   8249   bed = get_elf_backend_data (abfd);
   8250   symcount = bfd_get_symcount (abfd);
   8251   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8252   symtab_hdr->sh_type = SHT_SYMTAB;
   8253   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8254   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8255   symtab_hdr->sh_info = num_locals + 1;
   8256   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8257 
   8258   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8259   symstrtab_hdr->sh_type = SHT_STRTAB;
   8260 
   8261   /* Allocate buffer to swap out the .strtab section.  */
   8262   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8263       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8264     {
   8265       bfd_set_error (bfd_error_no_memory);
   8266       _bfd_elf_strtab_free (stt);
   8267       return false;
   8268     }
   8269 
   8270   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8271       || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
   8272     {
   8273     error_no_mem:
   8274       bfd_set_error (bfd_error_no_memory);
   8275     error_return:
   8276       free (symstrtab);
   8277       _bfd_elf_strtab_free (stt);
   8278       return false;
   8279     }
   8280   symtab_hdr->contents = outbound_syms;
   8281   outbound_syms_index = 0;
   8282 
   8283   outbound_shndx = NULL;
   8284 
   8285   if (elf_symtab_shndx_list (abfd))
   8286     {
   8287       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8288       if (symtab_shndx_hdr->sh_name != 0)
   8289 	{
   8290 	  if (_bfd_mul_overflow (symcount + 1,
   8291 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8292 	    goto error_no_mem;
   8293 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8294 	  if (outbound_shndx == NULL)
   8295 	    goto error_return;
   8296 
   8297 	  symtab_shndx_hdr->contents = outbound_shndx;
   8298 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8299 	  symtab_shndx_hdr->sh_size = amt;
   8300 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8301 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8302 	}
   8303       /* FIXME: What about any other headers in the list ?  */
   8304     }
   8305 
   8306   /* Now generate the data (for "contents").  */
   8307   {
   8308     /* Fill in zeroth symbol and swap it out.  */
   8309     Elf_Internal_Sym sym;
   8310     sym.st_name = 0;
   8311     sym.st_value = 0;
   8312     sym.st_size = 0;
   8313     sym.st_info = 0;
   8314     sym.st_other = 0;
   8315     sym.st_shndx = SHN_UNDEF;
   8316     sym.st_target_internal = 0;
   8317     symstrtab[0].sym = sym;
   8318     symstrtab[0].dest_index = outbound_syms_index;
   8319     outbound_syms_index++;
   8320   }
   8321 
   8322   name_local_sections
   8323     = (bed->elf_backend_name_local_section_symbols
   8324        && bed->elf_backend_name_local_section_symbols (abfd));
   8325 
   8326   syms = bfd_get_outsymbols (abfd);
   8327   for (idx = 0; idx < symcount;)
   8328     {
   8329       Elf_Internal_Sym sym;
   8330       bfd_vma value = syms[idx]->value;
   8331       elf_symbol_type *type_ptr;
   8332       flagword flags = syms[idx]->flags;
   8333       int type;
   8334 
   8335       if (!name_local_sections
   8336 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8337 	{
   8338 	  /* Local section symbols have no name.  */
   8339 	  sym.st_name = (unsigned long) -1;
   8340 	}
   8341       else
   8342 	{
   8343 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8344 	     to get the final offset for st_name.  */
   8345 	  sym.st_name
   8346 	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
   8347 						   false);
   8348 	  if (sym.st_name == (unsigned long) -1)
   8349 	    goto error_return;
   8350 	}
   8351 
   8352       type_ptr = elf_symbol_from (syms[idx]);
   8353 
   8354       if ((flags & BSF_SECTION_SYM) == 0
   8355 	  && bfd_is_com_section (syms[idx]->section))
   8356 	{
   8357 	  /* ELF common symbols put the alignment into the `value' field,
   8358 	     and the size into the `size' field.  This is backwards from
   8359 	     how BFD handles it, so reverse it here.  */
   8360 	  sym.st_size = value;
   8361 	  if (type_ptr == NULL
   8362 	      || type_ptr->internal_elf_sym.st_value == 0)
   8363 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8364 	  else
   8365 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8366 	  sym.st_shndx = _bfd_elf_section_from_bfd_section
   8367 	    (abfd, syms[idx]->section);
   8368 	}
   8369       else
   8370 	{
   8371 	  asection *sec = syms[idx]->section;
   8372 	  unsigned int shndx;
   8373 
   8374 	  if (sec->output_section)
   8375 	    {
   8376 	      value += sec->output_offset;
   8377 	      sec = sec->output_section;
   8378 	    }
   8379 
   8380 	  /* Don't add in the section vma for relocatable output.  */
   8381 	  if (! relocatable_p)
   8382 	    value += sec->vma;
   8383 	  sym.st_value = value;
   8384 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8385 
   8386 	  if (bfd_is_abs_section (sec)
   8387 	      && type_ptr != NULL
   8388 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8389 	    {
   8390 	      /* This symbol is in a real ELF section which we did
   8391 		 not create as a BFD section.  Undo the mapping done
   8392 		 by copy_private_symbol_data.  */
   8393 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8394 	      switch (shndx)
   8395 		{
   8396 		case MAP_ONESYMTAB:
   8397 		  shndx = elf_onesymtab (abfd);
   8398 		  break;
   8399 		case MAP_DYNSYMTAB:
   8400 		  shndx = elf_dynsymtab (abfd);
   8401 		  break;
   8402 		case MAP_STRTAB:
   8403 		  shndx = elf_strtab_sec (abfd);
   8404 		  break;
   8405 		case MAP_SHSTRTAB:
   8406 		  shndx = elf_shstrtab_sec (abfd);
   8407 		  break;
   8408 		case MAP_SYM_SHNDX:
   8409 		  if (elf_symtab_shndx_list (abfd))
   8410 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8411 		  break;
   8412 		case SHN_COMMON:
   8413 		case SHN_ABS:
   8414 		  shndx = SHN_ABS;
   8415 		  break;
   8416 		default:
   8417 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8418 		    {
   8419 		      if (bed->symbol_section_index)
   8420 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8421 		      /* Otherwise just leave the index alone.  */
   8422 		    }
   8423 		  else
   8424 		    {
   8425 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   8426 			_bfd_error_handler (_("%pB: \
   8427 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   8428 					  abfd, shndx);
   8429 		      shndx = SHN_ABS;
   8430 		    }
   8431 		  break;
   8432 		}
   8433 	    }
   8434 	  else
   8435 	    {
   8436 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8437 
   8438 	      if (shndx == SHN_BAD)
   8439 		{
   8440 		  asection *sec2;
   8441 
   8442 		  /* Writing this would be a hell of a lot easier if
   8443 		     we had some decent documentation on bfd, and
   8444 		     knew what to expect of the library, and what to
   8445 		     demand of applications.  For example, it
   8446 		     appears that `objcopy' might not set the
   8447 		     section of a symbol to be a section that is
   8448 		     actually in the output file.  */
   8449 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   8450 		  if (sec2 != NULL)
   8451 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   8452 		  if (shndx == SHN_BAD)
   8453 		    {
   8454 		      /* xgettext:c-format */
   8455 		      _bfd_error_handler
   8456 			(_("unable to find equivalent output section"
   8457 			   " for symbol '%s' from section '%s'"),
   8458 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   8459 			 sec->name);
   8460 		      bfd_set_error (bfd_error_invalid_operation);
   8461 		      goto error_return;
   8462 		    }
   8463 		}
   8464 	    }
   8465 
   8466 	  sym.st_shndx = shndx;
   8467 	}
   8468 
   8469       if ((flags & BSF_THREAD_LOCAL) != 0)
   8470 	type = STT_TLS;
   8471       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8472 	type = STT_GNU_IFUNC;
   8473       else if ((flags & BSF_FUNCTION) != 0)
   8474 	type = STT_FUNC;
   8475       else if ((flags & BSF_OBJECT) != 0)
   8476 	type = STT_OBJECT;
   8477       else if ((flags & BSF_RELC) != 0)
   8478 	type = STT_RELC;
   8479       else if ((flags & BSF_SRELC) != 0)
   8480 	type = STT_SRELC;
   8481       else
   8482 	type = STT_NOTYPE;
   8483 
   8484       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8485 	type = STT_TLS;
   8486 
   8487       /* Processor-specific types.  */
   8488       if (type_ptr != NULL
   8489 	  && bed->elf_backend_get_symbol_type)
   8490 	type = ((*bed->elf_backend_get_symbol_type)
   8491 		(&type_ptr->internal_elf_sym, type));
   8492 
   8493       if (flags & BSF_SECTION_SYM)
   8494 	{
   8495 	  if (flags & BSF_GLOBAL)
   8496 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   8497 	  else
   8498 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   8499 	}
   8500       else if (bfd_is_com_section (syms[idx]->section))
   8501 	{
   8502 	  if (type != STT_TLS)
   8503 	    {
   8504 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   8505 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   8506 			? STT_COMMON : STT_OBJECT);
   8507 	      else
   8508 		type = ((flags & BSF_ELF_COMMON) != 0
   8509 			? STT_COMMON : STT_OBJECT);
   8510 	    }
   8511 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   8512 	}
   8513       else if (bfd_is_und_section (syms[idx]->section))
   8514 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   8515 				    ? STB_WEAK
   8516 				    : STB_GLOBAL),
   8517 				   type);
   8518       else if (flags & BSF_FILE)
   8519 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   8520       else
   8521 	{
   8522 	  int bind = STB_LOCAL;
   8523 
   8524 	  if (flags & BSF_LOCAL)
   8525 	    bind = STB_LOCAL;
   8526 	  else if (flags & BSF_GNU_UNIQUE)
   8527 	    bind = STB_GNU_UNIQUE;
   8528 	  else if (flags & BSF_WEAK)
   8529 	    bind = STB_WEAK;
   8530 	  else if (flags & BSF_GLOBAL)
   8531 	    bind = STB_GLOBAL;
   8532 
   8533 	  sym.st_info = ELF_ST_INFO (bind, type);
   8534 	}
   8535 
   8536       if (type_ptr != NULL)
   8537 	{
   8538 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   8539 	  sym.st_target_internal
   8540 	    = type_ptr->internal_elf_sym.st_target_internal;
   8541 	}
   8542       else
   8543 	{
   8544 	  sym.st_other = 0;
   8545 	  sym.st_target_internal = 0;
   8546 	}
   8547 
   8548       idx++;
   8549       symstrtab[idx].sym = sym;
   8550       symstrtab[idx].dest_index = outbound_syms_index;
   8551 
   8552       outbound_syms_index++;
   8553     }
   8554 
   8555   /* Finalize the .strtab section.  */
   8556   _bfd_elf_strtab_finalize (stt);
   8557 
   8558   /* Swap out the .strtab section.  */
   8559   for (idx = 0; idx <= symcount; idx++)
   8560     {
   8561       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   8562       if (elfsym->sym.st_name == (unsigned long) -1)
   8563 	elfsym->sym.st_name = 0;
   8564       else
   8565 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   8566 						      elfsym->sym.st_name);
   8567       if (info && info->callbacks->ctf_new_symbol)
   8568 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   8569 					 &elfsym->sym);
   8570 
   8571       /* Inform the linker of the addition of this symbol.  */
   8572 
   8573       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   8574 			       (outbound_syms
   8575 				+ (elfsym->dest_index
   8576 				   * bed->s->sizeof_sym)),
   8577 			       NPTR_ADD (outbound_shndx,
   8578 					 (elfsym->dest_index
   8579 					  * sizeof (Elf_External_Sym_Shndx))));
   8580     }
   8581   free (symstrtab);
   8582 
   8583   *sttp = stt;
   8584   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   8585   symstrtab_hdr->sh_type = SHT_STRTAB;
   8586   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   8587   symstrtab_hdr->sh_addr = 0;
   8588   symstrtab_hdr->sh_entsize = 0;
   8589   symstrtab_hdr->sh_link = 0;
   8590   symstrtab_hdr->sh_info = 0;
   8591   symstrtab_hdr->sh_addralign = 1;
   8592 
   8593   return true;
   8594 }
   8595 
   8596 /* Return the number of bytes required to hold the symtab vector.
   8597 
   8598    Note that we base it on the count plus 1, since we will null terminate
   8599    the vector allocated based on this size.  However, the ELF symbol table
   8600    always has a dummy entry as symbol #0, so it ends up even.  */
   8601 
   8602 long
   8603 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   8604 {
   8605   bfd_size_type symcount;
   8606   long symtab_size;
   8607   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   8608 
   8609   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8610   if (symcount > LONG_MAX / sizeof (asymbol *))
   8611     {
   8612       bfd_set_error (bfd_error_file_too_big);
   8613       return -1;
   8614     }
   8615   symtab_size = symcount * (sizeof (asymbol *));
   8616   if (symcount == 0)
   8617     symtab_size = sizeof (asymbol *);
   8618   else if (!bfd_write_p (abfd))
   8619     {
   8620       ufile_ptr filesize = bfd_get_file_size (abfd);
   8621 
   8622       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   8623 	{
   8624 	  bfd_set_error (bfd_error_file_truncated);
   8625 	  return -1;
   8626 	}
   8627     }
   8628 
   8629   return symtab_size;
   8630 }
   8631 
   8632 long
   8633 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   8634 {
   8635   bfd_size_type symcount;
   8636   long symtab_size;
   8637   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   8638 
   8639   if (elf_dynsymtab (abfd) == 0)
   8640     {
   8641       bfd_set_error (bfd_error_invalid_operation);
   8642       return -1;
   8643     }
   8644 
   8645   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   8646   if (symcount > LONG_MAX / sizeof (asymbol *))
   8647     {
   8648       bfd_set_error (bfd_error_file_too_big);
   8649       return -1;
   8650     }
   8651   symtab_size = symcount * (sizeof (asymbol *));
   8652   if (symcount == 0)
   8653     symtab_size = sizeof (asymbol *);
   8654   else if (!bfd_write_p (abfd))
   8655     {
   8656       ufile_ptr filesize = bfd_get_file_size (abfd);
   8657 
   8658       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   8659 	{
   8660 	  bfd_set_error (bfd_error_file_truncated);
   8661 	  return -1;
   8662 	}
   8663     }
   8664 
   8665   return symtab_size;
   8666 }
   8667 
   8668 long
   8669 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   8670 {
   8671   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   8672     {
   8673       /* Sanity check reloc section size.  */
   8674       struct bfd_elf_section_data *d = elf_section_data (asect);
   8675       Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
   8676       bfd_size_type ext_rel_size = rel_hdr->sh_size;
   8677       ufile_ptr filesize = bfd_get_file_size (abfd);
   8678 
   8679       if (filesize != 0 && ext_rel_size > filesize)
   8680 	{
   8681 	  bfd_set_error (bfd_error_file_truncated);
   8682 	  return -1;
   8683 	}
   8684     }
   8685 
   8686 #if SIZEOF_LONG == SIZEOF_INT
   8687   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   8688     {
   8689       bfd_set_error (bfd_error_file_too_big);
   8690       return -1;
   8691     }
   8692 #endif
   8693   return (asect->reloc_count + 1L) * sizeof (arelent *);
   8694 }
   8695 
   8696 /* Canonicalize the relocs.  */
   8697 
   8698 long
   8699 _bfd_elf_canonicalize_reloc (bfd *abfd,
   8700 			     sec_ptr section,
   8701 			     arelent **relptr,
   8702 			     asymbol **symbols)
   8703 {
   8704   arelent *tblptr;
   8705   unsigned int i;
   8706   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8707 
   8708   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   8709     return -1;
   8710 
   8711   tblptr = section->relocation;
   8712   for (i = 0; i < section->reloc_count; i++)
   8713     *relptr++ = tblptr++;
   8714 
   8715   *relptr = NULL;
   8716 
   8717   return section->reloc_count;
   8718 }
   8719 
   8720 long
   8721 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   8722 {
   8723   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8724   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   8725 
   8726   if (symcount >= 0)
   8727     abfd->symcount = symcount;
   8728   return symcount;
   8729 }
   8730 
   8731 long
   8732 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   8733 				      asymbol **allocation)
   8734 {
   8735   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8736   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   8737 
   8738   if (symcount >= 0)
   8739     abfd->dynsymcount = symcount;
   8740   return symcount;
   8741 }
   8742 
   8743 /* Return the size required for the dynamic reloc entries.  Any loadable
   8744    section that was actually installed in the BFD, and has type SHT_REL
   8745    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   8746    dynamic reloc section.  */
   8747 
   8748 long
   8749 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   8750 {
   8751   bfd_size_type count, ext_rel_size;
   8752   asection *s;
   8753 
   8754   if (elf_dynsymtab (abfd) == 0)
   8755     {
   8756       bfd_set_error (bfd_error_invalid_operation);
   8757       return -1;
   8758     }
   8759 
   8760   count = 1;
   8761   ext_rel_size = 0;
   8762   for (s = abfd->sections; s != NULL; s = s->next)
   8763     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8764 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8765 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8766       {
   8767 	ext_rel_size += s->size;
   8768 	if (ext_rel_size < s->size)
   8769 	  {
   8770 	    bfd_set_error (bfd_error_file_truncated);
   8771 	    return -1;
   8772 	  }
   8773 	count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8774 	if (count > LONG_MAX / sizeof (arelent *))
   8775 	  {
   8776 	    bfd_set_error (bfd_error_file_too_big);
   8777 	    return -1;
   8778 	  }
   8779       }
   8780   if (count > 1 && !bfd_write_p (abfd))
   8781     {
   8782       /* Sanity check reloc section sizes.  */
   8783       ufile_ptr filesize = bfd_get_file_size (abfd);
   8784       if (filesize != 0 && ext_rel_size > filesize)
   8785 	{
   8786 	  bfd_set_error (bfd_error_file_truncated);
   8787 	  return -1;
   8788 	}
   8789     }
   8790   return count * sizeof (arelent *);
   8791 }
   8792 
   8793 /* Canonicalize the dynamic relocation entries.  Note that we return the
   8794    dynamic relocations as a single block, although they are actually
   8795    associated with particular sections; the interface, which was
   8796    designed for SunOS style shared libraries, expects that there is only
   8797    one set of dynamic relocs.  Any loadable section that was actually
   8798    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   8799    dynamic symbol table, is considered to be a dynamic reloc section.  */
   8800 
   8801 long
   8802 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   8803 				     arelent **storage,
   8804 				     asymbol **syms)
   8805 {
   8806   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   8807   asection *s;
   8808   long ret;
   8809 
   8810   if (elf_dynsymtab (abfd) == 0)
   8811     {
   8812       bfd_set_error (bfd_error_invalid_operation);
   8813       return -1;
   8814     }
   8815 
   8816   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   8817   ret = 0;
   8818   for (s = abfd->sections; s != NULL; s = s->next)
   8819     {
   8820       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   8821 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   8822 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
   8823 	{
   8824 	  arelent *p;
   8825 	  long count, i;
   8826 
   8827 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   8828 	    return -1;
   8829 	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
   8830 	  p = s->relocation;
   8831 	  for (i = 0; i < count; i++)
   8832 	    *storage++ = p++;
   8833 	  ret += count;
   8834 	}
   8835     }
   8836 
   8837   *storage = NULL;
   8838 
   8839   return ret;
   8840 }
   8841 
   8842 /* Read in the version information.  */
   8844 
   8845 bool
   8846 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   8847 {
   8848   bfd_byte *contents = NULL;
   8849   unsigned int freeidx = 0;
   8850   size_t amt;
   8851 
   8852   if (elf_dynverref (abfd) != 0)
   8853     {
   8854       Elf_Internal_Shdr *hdr;
   8855       Elf_External_Verneed *everneed;
   8856       Elf_Internal_Verneed *iverneed;
   8857       unsigned int i;
   8858       bfd_byte *contents_end;
   8859 
   8860       hdr = &elf_tdata (abfd)->dynverref_hdr;
   8861 
   8862       if (hdr->sh_info == 0
   8863 	  || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   8864 	{
   8865 	error_return_bad_verref:
   8866 	  _bfd_error_handler
   8867 	    (_("%pB: .gnu.version_r invalid entry"), abfd);
   8868 	  bfd_set_error (bfd_error_bad_value);
   8869 	error_return_verref:
   8870 	  elf_tdata (abfd)->verref = NULL;
   8871 	  elf_tdata (abfd)->cverrefs = 0;
   8872 	  goto error_return;
   8873 	}
   8874 
   8875       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   8876 	goto error_return_verref;
   8877       contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   8878       if (contents == NULL)
   8879 	goto error_return_verref;
   8880 
   8881       if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
   8882 	{
   8883 	  bfd_set_error (bfd_error_file_too_big);
   8884 	  goto error_return_verref;
   8885 	}
   8886       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
   8887       if (elf_tdata (abfd)->verref == NULL)
   8888 	goto error_return_verref;
   8889 
   8890       BFD_ASSERT (sizeof (Elf_External_Verneed)
   8891 		  == sizeof (Elf_External_Vernaux));
   8892       contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
   8893       everneed = (Elf_External_Verneed *) contents;
   8894       iverneed = elf_tdata (abfd)->verref;
   8895       for (i = 0; i < hdr->sh_info; i++, iverneed++)
   8896 	{
   8897 	  Elf_External_Vernaux *evernaux;
   8898 	  Elf_Internal_Vernaux *ivernaux;
   8899 	  unsigned int j;
   8900 
   8901 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   8902 
   8903 	  iverneed->vn_bfd = abfd;
   8904 
   8905 	  iverneed->vn_filename =
   8906 	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8907 					     iverneed->vn_file);
   8908 	  if (iverneed->vn_filename == NULL)
   8909 	    goto error_return_bad_verref;
   8910 
   8911 	  if (iverneed->vn_cnt == 0)
   8912 	    iverneed->vn_auxptr = NULL;
   8913 	  else
   8914 	    {
   8915 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   8916 				     sizeof (Elf_Internal_Vernaux), &amt))
   8917 		{
   8918 		  bfd_set_error (bfd_error_file_too_big);
   8919 		  goto error_return_verref;
   8920 		}
   8921 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   8922 		bfd_alloc (abfd, amt);
   8923 	      if (iverneed->vn_auxptr == NULL)
   8924 		goto error_return_verref;
   8925 	    }
   8926 
   8927 	  if (iverneed->vn_aux
   8928 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8929 	    goto error_return_bad_verref;
   8930 
   8931 	  evernaux = ((Elf_External_Vernaux *)
   8932 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   8933 	  ivernaux = iverneed->vn_auxptr;
   8934 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   8935 	    {
   8936 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   8937 
   8938 	      ivernaux->vna_nodename =
   8939 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   8940 						 ivernaux->vna_name);
   8941 	      if (ivernaux->vna_nodename == NULL)
   8942 		goto error_return_bad_verref;
   8943 
   8944 	      if (ivernaux->vna_other > freeidx)
   8945 		freeidx = ivernaux->vna_other;
   8946 
   8947 	      ivernaux->vna_nextptr = NULL;
   8948 	      if (ivernaux->vna_next == 0)
   8949 		{
   8950 		  iverneed->vn_cnt = j + 1;
   8951 		  break;
   8952 		}
   8953 	      if (j + 1 < iverneed->vn_cnt)
   8954 		ivernaux->vna_nextptr = ivernaux + 1;
   8955 
   8956 	      if (ivernaux->vna_next
   8957 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   8958 		goto error_return_bad_verref;
   8959 
   8960 	      evernaux = ((Elf_External_Vernaux *)
   8961 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   8962 	    }
   8963 
   8964 	  iverneed->vn_nextref = NULL;
   8965 	  if (iverneed->vn_next == 0)
   8966 	    break;
   8967 	  if (i + 1 < hdr->sh_info)
   8968 	    iverneed->vn_nextref = iverneed + 1;
   8969 
   8970 	  if (iverneed->vn_next
   8971 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   8972 	    goto error_return_bad_verref;
   8973 
   8974 	  everneed = ((Elf_External_Verneed *)
   8975 		      ((bfd_byte *) everneed + iverneed->vn_next));
   8976 	}
   8977       elf_tdata (abfd)->cverrefs = i;
   8978 
   8979       free (contents);
   8980       contents = NULL;
   8981     }
   8982 
   8983   if (elf_dynverdef (abfd) != 0)
   8984     {
   8985       Elf_Internal_Shdr *hdr;
   8986       Elf_External_Verdef *everdef;
   8987       Elf_Internal_Verdef *iverdef;
   8988       Elf_Internal_Verdef *iverdefarr;
   8989       Elf_Internal_Verdef iverdefmem;
   8990       unsigned int i;
   8991       unsigned int maxidx;
   8992       bfd_byte *contents_end_def, *contents_end_aux;
   8993 
   8994       hdr = &elf_tdata (abfd)->dynverdef_hdr;
   8995 
   8996       if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
   8997 	{
   8998 	error_return_bad_verdef:
   8999 	  _bfd_error_handler
   9000 	    (_("%pB: .gnu.version_d invalid entry"), abfd);
   9001 	  bfd_set_error (bfd_error_bad_value);
   9002 	error_return_verdef:
   9003 	  elf_tdata (abfd)->verdef = NULL;
   9004 	  elf_tdata (abfd)->cverdefs = 0;
   9005 	  goto error_return;
   9006 	}
   9007 
   9008       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9009 	goto error_return_verdef;
   9010       contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
   9011       if (contents == NULL)
   9012 	goto error_return_verdef;
   9013 
   9014       BFD_ASSERT (sizeof (Elf_External_Verdef)
   9015 		  >= sizeof (Elf_External_Verdaux));
   9016       contents_end_def = contents + hdr->sh_size
   9017 			 - sizeof (Elf_External_Verdef);
   9018       contents_end_aux = contents + hdr->sh_size
   9019 			 - sizeof (Elf_External_Verdaux);
   9020 
   9021       /* We know the number of entries in the section but not the maximum
   9022 	 index.  Therefore we have to run through all entries and find
   9023 	 the maximum.  */
   9024       everdef = (Elf_External_Verdef *) contents;
   9025       maxidx = 0;
   9026       for (i = 0; i < hdr->sh_info; ++i)
   9027 	{
   9028 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9029 
   9030 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9031 	    goto error_return_bad_verdef;
   9032 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9033 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9034 
   9035 	  if (iverdefmem.vd_next == 0)
   9036 	    break;
   9037 
   9038 	  if (iverdefmem.vd_next
   9039 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9040 	    goto error_return_bad_verdef;
   9041 
   9042 	  everdef = ((Elf_External_Verdef *)
   9043 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9044 	}
   9045 
   9046       if (default_imported_symver)
   9047 	{
   9048 	  if (freeidx > maxidx)
   9049 	    maxidx = ++freeidx;
   9050 	  else
   9051 	    freeidx = ++maxidx;
   9052 	}
   9053       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9054 	{
   9055 	  bfd_set_error (bfd_error_file_too_big);
   9056 	  goto error_return_verdef;
   9057 	}
   9058       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9059       if (elf_tdata (abfd)->verdef == NULL)
   9060 	goto error_return_verdef;
   9061 
   9062       elf_tdata (abfd)->cverdefs = maxidx;
   9063 
   9064       everdef = (Elf_External_Verdef *) contents;
   9065       iverdefarr = elf_tdata (abfd)->verdef;
   9066       for (i = 0; i < hdr->sh_info; i++)
   9067 	{
   9068 	  Elf_External_Verdaux *everdaux;
   9069 	  Elf_Internal_Verdaux *iverdaux;
   9070 	  unsigned int j;
   9071 
   9072 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9073 
   9074 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9075 	    goto error_return_bad_verdef;
   9076 
   9077 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9078 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9079 
   9080 	  iverdef->vd_bfd = abfd;
   9081 
   9082 	  if (iverdef->vd_cnt == 0)
   9083 	    iverdef->vd_auxptr = NULL;
   9084 	  else
   9085 	    {
   9086 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9087 				     sizeof (Elf_Internal_Verdaux), &amt))
   9088 		{
   9089 		  bfd_set_error (bfd_error_file_too_big);
   9090 		  goto error_return_verdef;
   9091 		}
   9092 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9093 		bfd_alloc (abfd, amt);
   9094 	      if (iverdef->vd_auxptr == NULL)
   9095 		goto error_return_verdef;
   9096 	    }
   9097 
   9098 	  if (iverdef->vd_aux
   9099 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9100 	    goto error_return_bad_verdef;
   9101 
   9102 	  everdaux = ((Elf_External_Verdaux *)
   9103 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9104 	  iverdaux = iverdef->vd_auxptr;
   9105 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9106 	    {
   9107 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9108 
   9109 	      iverdaux->vda_nodename =
   9110 		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9111 						 iverdaux->vda_name);
   9112 	      if (iverdaux->vda_nodename == NULL)
   9113 		goto error_return_bad_verdef;
   9114 
   9115 	      iverdaux->vda_nextptr = NULL;
   9116 	      if (iverdaux->vda_next == 0)
   9117 		{
   9118 		  iverdef->vd_cnt = j + 1;
   9119 		  break;
   9120 		}
   9121 	      if (j + 1 < iverdef->vd_cnt)
   9122 		iverdaux->vda_nextptr = iverdaux + 1;
   9123 
   9124 	      if (iverdaux->vda_next
   9125 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9126 		goto error_return_bad_verdef;
   9127 
   9128 	      everdaux = ((Elf_External_Verdaux *)
   9129 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9130 	    }
   9131 
   9132 	  iverdef->vd_nodename = NULL;
   9133 	  if (iverdef->vd_cnt)
   9134 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9135 
   9136 	  iverdef->vd_nextdef = NULL;
   9137 	  if (iverdef->vd_next == 0)
   9138 	    break;
   9139 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9140 	    iverdef->vd_nextdef = iverdef + 1;
   9141 
   9142 	  everdef = ((Elf_External_Verdef *)
   9143 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9144 	}
   9145 
   9146       free (contents);
   9147       contents = NULL;
   9148     }
   9149   else if (default_imported_symver)
   9150     {
   9151       if (freeidx < 3)
   9152 	freeidx = 3;
   9153       else
   9154 	freeidx++;
   9155 
   9156       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9157 	{
   9158 	  bfd_set_error (bfd_error_file_too_big);
   9159 	  goto error_return;
   9160 	}
   9161       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9162       if (elf_tdata (abfd)->verdef == NULL)
   9163 	goto error_return;
   9164 
   9165       elf_tdata (abfd)->cverdefs = freeidx;
   9166     }
   9167 
   9168   /* Create a default version based on the soname.  */
   9169   if (default_imported_symver)
   9170     {
   9171       Elf_Internal_Verdef *iverdef;
   9172       Elf_Internal_Verdaux *iverdaux;
   9173 
   9174       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9175 
   9176       iverdef->vd_version = VER_DEF_CURRENT;
   9177       iverdef->vd_flags = 0;
   9178       iverdef->vd_ndx = freeidx;
   9179       iverdef->vd_cnt = 1;
   9180 
   9181       iverdef->vd_bfd = abfd;
   9182 
   9183       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9184       if (iverdef->vd_nodename == NULL)
   9185 	goto error_return_verdef;
   9186       iverdef->vd_nextdef = NULL;
   9187       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9188 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9189       if (iverdef->vd_auxptr == NULL)
   9190 	goto error_return_verdef;
   9191 
   9192       iverdaux = iverdef->vd_auxptr;
   9193       iverdaux->vda_nodename = iverdef->vd_nodename;
   9194     }
   9195 
   9196   return true;
   9197 
   9198  error_return:
   9199   free (contents);
   9200   return false;
   9201 }
   9202 
   9203 asymbol *
   9205 _bfd_elf_make_empty_symbol (bfd *abfd)
   9206 {
   9207   elf_symbol_type *newsym;
   9208 
   9209   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9210   if (!newsym)
   9211     return NULL;
   9212   newsym->symbol.the_bfd = abfd;
   9213   return &newsym->symbol;
   9214 }
   9215 
   9216 void
   9217 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9218 			  asymbol *symbol,
   9219 			  symbol_info *ret)
   9220 {
   9221   bfd_symbol_info (symbol, ret);
   9222 }
   9223 
   9224 /* Return whether a symbol name implies a local symbol.  Most targets
   9225    use this function for the is_local_label_name entry point, but some
   9226    override it.  */
   9227 
   9228 bool
   9229 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9230 			      const char *name)
   9231 {
   9232   /* Normal local symbols start with ``.L''.  */
   9233   if (name[0] == '.' && name[1] == 'L')
   9234     return true;
   9235 
   9236   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9237      DWARF debugging symbols starting with ``..''.  */
   9238   if (name[0] == '.' && name[1] == '.')
   9239     return true;
   9240 
   9241   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9242      emitting DWARF debugging output.  I suspect this is actually a
   9243      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9244      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9245      underscore to be emitted on some ELF targets).  For ease of use,
   9246      we treat such symbols as local.  */
   9247   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9248     return true;
   9249 
   9250   /* Treat assembler generated fake symbols, dollar local labels and
   9251      forward-backward labels (aka local labels) as locals.
   9252      These labels have the form:
   9253 
   9254        L0^A.*				       (fake symbols)
   9255 
   9256        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9257 
   9258      Versions which start with .L will have already been matched above,
   9259      so we only need to match the rest.  */
   9260   if (name[0] == 'L' && ISDIGIT (name[1]))
   9261     {
   9262       bool ret = false;
   9263       const char * p;
   9264       char c;
   9265 
   9266       for (p = name + 2; (c = *p); p++)
   9267 	{
   9268 	  if (c == 1 || c == 2)
   9269 	    {
   9270 	      if (c == 1 && p == name + 2)
   9271 		/* A fake symbol.  */
   9272 		return true;
   9273 
   9274 	      /* FIXME: We are being paranoid here and treating symbols like
   9275 		 L0^Bfoo as if there were non-local, on the grounds that the
   9276 		 assembler will never generate them.  But can any symbol
   9277 		 containing an ASCII value in the range 1-31 ever be anything
   9278 		 other than some kind of local ?  */
   9279 	      ret = true;
   9280 	    }
   9281 
   9282 	  if (! ISDIGIT (c))
   9283 	    {
   9284 	      ret = false;
   9285 	      break;
   9286 	    }
   9287 	}
   9288       return ret;
   9289     }
   9290 
   9291   return false;
   9292 }
   9293 
   9294 alent *
   9295 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9296 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9297 {
   9298   abort ();
   9299   return NULL;
   9300 }
   9301 
   9302 bool
   9303 _bfd_elf_set_arch_mach (bfd *abfd,
   9304 			enum bfd_architecture arch,
   9305 			unsigned long machine)
   9306 {
   9307   /* If this isn't the right architecture for this backend, and this
   9308      isn't the generic backend, fail.  */
   9309   if (arch != get_elf_backend_data (abfd)->arch
   9310       && arch != bfd_arch_unknown
   9311       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9312     return false;
   9313 
   9314   return bfd_default_set_arch_mach (abfd, arch, machine);
   9315 }
   9316 
   9317 /* Find the nearest line to a particular section and offset,
   9318    for error reporting.  */
   9319 
   9320 bool
   9321 _bfd_elf_find_nearest_line (bfd *abfd,
   9322 			    asymbol **symbols,
   9323 			    asection *section,
   9324 			    bfd_vma offset,
   9325 			    const char **filename_ptr,
   9326 			    const char **functionname_ptr,
   9327 			    unsigned int *line_ptr,
   9328 			    unsigned int *discriminator_ptr)
   9329 {
   9330   bool found;
   9331 
   9332   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
   9333 				     filename_ptr, functionname_ptr,
   9334 				     line_ptr, discriminator_ptr,
   9335 				     dwarf_debug_sections,
   9336 				     &elf_tdata (abfd)->dwarf2_find_line_info))
   9337     return true;
   9338 
   9339   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   9340 				     filename_ptr, functionname_ptr, line_ptr))
   9341     {
   9342       if (!*functionname_ptr)
   9343 	_bfd_elf_find_function (abfd, symbols, section, offset,
   9344 				*filename_ptr ? NULL : filename_ptr,
   9345 				functionname_ptr);
   9346       return true;
   9347     }
   9348 
   9349   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   9350 					     &found, filename_ptr,
   9351 					     functionname_ptr, line_ptr,
   9352 					     &elf_tdata (abfd)->line_info))
   9353     return false;
   9354   if (found && (*functionname_ptr || *line_ptr))
   9355     return true;
   9356 
   9357   if (symbols == NULL)
   9358     return false;
   9359 
   9360   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   9361 				filename_ptr, functionname_ptr))
   9362     return false;
   9363 
   9364   *line_ptr = 0;
   9365   return true;
   9366 }
   9367 
   9368 /* Find the line for a symbol.  */
   9369 
   9370 bool
   9371 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   9372 		    const char **filename_ptr, unsigned int *line_ptr)
   9373 {
   9374   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   9375 					filename_ptr, NULL, line_ptr, NULL,
   9376 					dwarf_debug_sections,
   9377 					&elf_tdata (abfd)->dwarf2_find_line_info);
   9378 }
   9379 
   9380 /* After a call to bfd_find_nearest_line, successive calls to
   9381    bfd_find_inliner_info can be used to get source information about
   9382    each level of function inlining that terminated at the address
   9383    passed to bfd_find_nearest_line.  Currently this is only supported
   9384    for DWARF2 with appropriate DWARF3 extensions. */
   9385 
   9386 bool
   9387 _bfd_elf_find_inliner_info (bfd *abfd,
   9388 			    const char **filename_ptr,
   9389 			    const char **functionname_ptr,
   9390 			    unsigned int *line_ptr)
   9391 {
   9392   bool found;
   9393   found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   9394 					 functionname_ptr, line_ptr,
   9395 					 & elf_tdata (abfd)->dwarf2_find_line_info);
   9396   return found;
   9397 }
   9398 
   9399 int
   9400 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   9401 {
   9402   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9403   int ret = bed->s->sizeof_ehdr;
   9404 
   9405   if (!bfd_link_relocatable (info))
   9406     {
   9407       bfd_size_type phdr_size = elf_program_header_size (abfd);
   9408 
   9409       if (phdr_size == (bfd_size_type) -1)
   9410 	{
   9411 	  struct elf_segment_map *m;
   9412 
   9413 	  phdr_size = 0;
   9414 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   9415 	    phdr_size += bed->s->sizeof_phdr;
   9416 
   9417 	  if (phdr_size == 0)
   9418 	    phdr_size = get_program_header_size (abfd, info);
   9419 	}
   9420 
   9421       elf_program_header_size (abfd) = phdr_size;
   9422       ret += phdr_size;
   9423     }
   9424 
   9425   return ret;
   9426 }
   9427 
   9428 bool
   9429 _bfd_elf_set_section_contents (bfd *abfd,
   9430 			       sec_ptr section,
   9431 			       const void *location,
   9432 			       file_ptr offset,
   9433 			       bfd_size_type count)
   9434 {
   9435   Elf_Internal_Shdr *hdr;
   9436 
   9437   if (! abfd->output_has_begun
   9438       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   9439     return false;
   9440 
   9441   if (!count)
   9442     return true;
   9443 
   9444   hdr = &elf_section_data (section)->this_hdr;
   9445   if (hdr->sh_offset == (file_ptr) -1)
   9446     {
   9447       unsigned char *contents;
   9448 
   9449       if (bfd_section_is_ctf (section))
   9450 	/* Nothing to do with this section: the contents are generated
   9451 	   later.  */
   9452 	return true;
   9453 
   9454       if ((section->flags & SEC_ELF_COMPRESS) == 0)
   9455 	{
   9456 	  _bfd_error_handler
   9457 	    (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
   9458 	     abfd, section);
   9459 	  bfd_set_error (bfd_error_invalid_operation);
   9460 	  return false;
   9461 	}
   9462 
   9463       if ((offset + count) > hdr->sh_size)
   9464 	{
   9465 	  _bfd_error_handler
   9466 	    (_("%pB:%pA: error: attempting to write over the end of the section"),
   9467 	     abfd, section);
   9468 
   9469 	  bfd_set_error (bfd_error_invalid_operation);
   9470 	  return false;
   9471 	}
   9472 
   9473       contents = hdr->contents;
   9474       if (contents == NULL)
   9475 	{
   9476 	  _bfd_error_handler
   9477 	    (_("%pB:%pA: error: attempting to write section into an empty buffer"),
   9478 	     abfd, section);
   9479 
   9480 	  bfd_set_error (bfd_error_invalid_operation);
   9481 	  return false;
   9482 	}
   9483 
   9484       memcpy (contents + offset, location, count);
   9485       return true;
   9486     }
   9487 
   9488   return _bfd_generic_set_section_contents (abfd, section,
   9489 					    location, offset, count);
   9490 }
   9491 
   9492 bool
   9493 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   9494 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   9495 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   9496 {
   9497   abort ();
   9498   return false;
   9499 }
   9500 
   9501 /* Try to convert a non-ELF reloc into an ELF one.  */
   9502 
   9503 bool
   9504 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   9505 {
   9506   /* Check whether we really have an ELF howto.  */
   9507 
   9508   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   9509     {
   9510       bfd_reloc_code_real_type code;
   9511       reloc_howto_type *howto;
   9512 
   9513       /* Alien reloc: Try to determine its type to replace it with an
   9514 	 equivalent ELF reloc.  */
   9515 
   9516       if (areloc->howto->pc_relative)
   9517 	{
   9518 	  switch (areloc->howto->bitsize)
   9519 	    {
   9520 	    case 8:
   9521 	      code = BFD_RELOC_8_PCREL;
   9522 	      break;
   9523 	    case 12:
   9524 	      code = BFD_RELOC_12_PCREL;
   9525 	      break;
   9526 	    case 16:
   9527 	      code = BFD_RELOC_16_PCREL;
   9528 	      break;
   9529 	    case 24:
   9530 	      code = BFD_RELOC_24_PCREL;
   9531 	      break;
   9532 	    case 32:
   9533 	      code = BFD_RELOC_32_PCREL;
   9534 	      break;
   9535 	    case 64:
   9536 	      code = BFD_RELOC_64_PCREL;
   9537 	      break;
   9538 	    default:
   9539 	      goto fail;
   9540 	    }
   9541 
   9542 	  howto = bfd_reloc_type_lookup (abfd, code);
   9543 
   9544 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   9545 	    {
   9546 	      if (howto->pcrel_offset)
   9547 		areloc->addend += areloc->address;
   9548 	      else
   9549 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   9550 	    }
   9551 	}
   9552       else
   9553 	{
   9554 	  switch (areloc->howto->bitsize)
   9555 	    {
   9556 	    case 8:
   9557 	      code = BFD_RELOC_8;
   9558 	      break;
   9559 	    case 14:
   9560 	      code = BFD_RELOC_14;
   9561 	      break;
   9562 	    case 16:
   9563 	      code = BFD_RELOC_16;
   9564 	      break;
   9565 	    case 26:
   9566 	      code = BFD_RELOC_26;
   9567 	      break;
   9568 	    case 32:
   9569 	      code = BFD_RELOC_32;
   9570 	      break;
   9571 	    case 64:
   9572 	      code = BFD_RELOC_64;
   9573 	      break;
   9574 	    default:
   9575 	      goto fail;
   9576 	    }
   9577 
   9578 	  howto = bfd_reloc_type_lookup (abfd, code);
   9579 	}
   9580 
   9581       if (howto)
   9582 	areloc->howto = howto;
   9583       else
   9584 	goto fail;
   9585     }
   9586 
   9587   return true;
   9588 
   9589  fail:
   9590   /* xgettext:c-format */
   9591   _bfd_error_handler (_("%pB: %s unsupported"),
   9592 		      abfd, areloc->howto->name);
   9593   bfd_set_error (bfd_error_sorry);
   9594   return false;
   9595 }
   9596 
   9597 bool
   9598 _bfd_elf_close_and_cleanup (bfd *abfd)
   9599 {
   9600   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9601   if (tdata != NULL
   9602       && (bfd_get_format (abfd) == bfd_object
   9603 	  || bfd_get_format (abfd) == bfd_core))
   9604     {
   9605       if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
   9606 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   9607       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   9608     }
   9609 
   9610   return _bfd_generic_close_and_cleanup (abfd);
   9611 }
   9612 
   9613 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   9614    in the relocation's offset.  Thus we cannot allow any sort of sanity
   9615    range-checking to interfere.  There is nothing else to do in processing
   9616    this reloc.  */
   9617 
   9618 bfd_reloc_status_type
   9619 _bfd_elf_rel_vtable_reloc_fn
   9620   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   9621    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   9622    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   9623    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   9624 {
   9625   return bfd_reloc_ok;
   9626 }
   9627 
   9628 /* Elf core file support.  Much of this only works on native
   9630    toolchains, since we rely on knowing the
   9631    machine-dependent procfs structure in order to pick
   9632    out details about the corefile.  */
   9633 
   9634 #ifdef HAVE_SYS_PROCFS_H
   9635 # include <sys/procfs.h>
   9636 #endif
   9637 
   9638 /* Return a PID that identifies a "thread" for threaded cores, or the
   9639    PID of the main process for non-threaded cores.  */
   9640 
   9641 static int
   9642 elfcore_make_pid (bfd *abfd)
   9643 {
   9644   int pid;
   9645 
   9646   pid = elf_tdata (abfd)->core->lwpid;
   9647   if (pid == 0)
   9648     pid = elf_tdata (abfd)->core->pid;
   9649 
   9650   return pid;
   9651 }
   9652 
   9653 /* If there isn't a section called NAME, make one, using
   9654    data from SECT.  Note, this function will generate a
   9655    reference to NAME, so you shouldn't deallocate or
   9656    overwrite it.  */
   9657 
   9658 static bool
   9659 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   9660 {
   9661   asection *sect2;
   9662 
   9663   if (bfd_get_section_by_name (abfd, name) != NULL)
   9664     return true;
   9665 
   9666   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   9667   if (sect2 == NULL)
   9668     return false;
   9669 
   9670   sect2->size = sect->size;
   9671   sect2->filepos = sect->filepos;
   9672   sect2->alignment_power = sect->alignment_power;
   9673   return true;
   9674 }
   9675 
   9676 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   9677    actually creates up to two pseudosections:
   9678    - For the single-threaded case, a section named NAME, unless
   9679      such a section already exists.
   9680    - For the multi-threaded case, a section named "NAME/PID", where
   9681      PID is elfcore_make_pid (abfd).
   9682    Both pseudosections have identical contents.  */
   9683 bool
   9684 _bfd_elfcore_make_pseudosection (bfd *abfd,
   9685 				 char *name,
   9686 				 size_t size,
   9687 				 ufile_ptr filepos)
   9688 {
   9689   char buf[100];
   9690   char *threaded_name;
   9691   size_t len;
   9692   asection *sect;
   9693 
   9694   /* Build the section name.  */
   9695 
   9696   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   9697   len = strlen (buf) + 1;
   9698   threaded_name = (char *) bfd_alloc (abfd, len);
   9699   if (threaded_name == NULL)
   9700     return false;
   9701   memcpy (threaded_name, buf, len);
   9702 
   9703   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   9704 					     SEC_HAS_CONTENTS);
   9705   if (sect == NULL)
   9706     return false;
   9707   sect->size = size;
   9708   sect->filepos = filepos;
   9709   sect->alignment_power = 2;
   9710 
   9711   return elfcore_maybe_make_sect (abfd, name, sect);
   9712 }
   9713 
   9714 static bool
   9715 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   9716 				size_t offs)
   9717 {
   9718   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   9719 						       SEC_HAS_CONTENTS);
   9720 
   9721   if (sect == NULL)
   9722     return false;
   9723 
   9724   sect->size = note->descsz - offs;
   9725   sect->filepos = note->descpos + offs;
   9726   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   9727 
   9728   return true;
   9729 }
   9730 
   9731 /* prstatus_t exists on:
   9732      solaris 2.5+
   9733      linux 2.[01] + glibc
   9734      unixware 4.2
   9735 */
   9736 
   9737 #if defined (HAVE_PRSTATUS_T)
   9738 
   9739 static bool
   9740 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   9741 {
   9742   size_t size;
   9743   int offset;
   9744 
   9745   if (note->descsz == sizeof (prstatus_t))
   9746     {
   9747       prstatus_t prstat;
   9748 
   9749       size = sizeof (prstat.pr_reg);
   9750       offset   = offsetof (prstatus_t, pr_reg);
   9751       memcpy (&prstat, note->descdata, sizeof (prstat));
   9752 
   9753       /* Do not overwrite the core signal if it
   9754 	 has already been set by another thread.  */
   9755       if (elf_tdata (abfd)->core->signal == 0)
   9756 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9757       if (elf_tdata (abfd)->core->pid == 0)
   9758 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9759 
   9760       /* pr_who exists on:
   9761 	 solaris 2.5+
   9762 	 unixware 4.2
   9763 	 pr_who doesn't exist on:
   9764 	 linux 2.[01]
   9765 	 */
   9766 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   9767       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9768 #else
   9769       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9770 #endif
   9771     }
   9772 #if defined (HAVE_PRSTATUS32_T)
   9773   else if (note->descsz == sizeof (prstatus32_t))
   9774     {
   9775       /* 64-bit host, 32-bit corefile */
   9776       prstatus32_t prstat;
   9777 
   9778       size = sizeof (prstat.pr_reg);
   9779       offset   = offsetof (prstatus32_t, pr_reg);
   9780       memcpy (&prstat, note->descdata, sizeof (prstat));
   9781 
   9782       /* Do not overwrite the core signal if it
   9783 	 has already been set by another thread.  */
   9784       if (elf_tdata (abfd)->core->signal == 0)
   9785 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   9786       if (elf_tdata (abfd)->core->pid == 0)
   9787 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   9788 
   9789       /* pr_who exists on:
   9790 	 solaris 2.5+
   9791 	 unixware 4.2
   9792 	 pr_who doesn't exist on:
   9793 	 linux 2.[01]
   9794 	 */
   9795 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   9796       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   9797 #else
   9798       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   9799 #endif
   9800     }
   9801 #endif /* HAVE_PRSTATUS32_T */
   9802   else
   9803     {
   9804       /* Fail - we don't know how to handle any other
   9805 	 note size (ie. data object type).  */
   9806       return true;
   9807     }
   9808 
   9809   /* Make a ".reg/999" section and a ".reg" section.  */
   9810   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   9811 					  size, note->descpos + offset);
   9812 }
   9813 #endif /* defined (HAVE_PRSTATUS_T) */
   9814 
   9815 /* Create a pseudosection containing the exact contents of NOTE.  */
   9816 static bool
   9817 elfcore_make_note_pseudosection (bfd *abfd,
   9818 				 char *name,
   9819 				 Elf_Internal_Note *note)
   9820 {
   9821   return _bfd_elfcore_make_pseudosection (abfd, name,
   9822 					  note->descsz, note->descpos);
   9823 }
   9824 
   9825 /* There isn't a consistent prfpregset_t across platforms,
   9826    but it doesn't matter, because we don't have to pick this
   9827    data structure apart.  */
   9828 
   9829 static bool
   9830 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   9831 {
   9832   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   9833 }
   9834 
   9835 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   9836    type of NT_PRXFPREG.  Just include the whole note's contents
   9837    literally.  */
   9838 
   9839 static bool
   9840 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   9841 {
   9842   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   9843 }
   9844 
   9845 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   9846    with a note type of NT_X86_XSTATE.  Just include the whole note's
   9847    contents literally.  */
   9848 
   9849 static bool
   9850 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   9851 {
   9852   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   9853 }
   9854 
   9855 static bool
   9856 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   9857 {
   9858   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   9859 }
   9860 
   9861 static bool
   9862 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   9863 {
   9864   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   9865 }
   9866 
   9867 static bool
   9868 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   9869 {
   9870   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
   9871 }
   9872 
   9873 static bool
   9874 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   9875 {
   9876   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
   9877 }
   9878 
   9879 static bool
   9880 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   9881 {
   9882   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
   9883 }
   9884 
   9885 static bool
   9886 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   9887 {
   9888   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
   9889 }
   9890 
   9891 static bool
   9892 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   9893 {
   9894   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
   9895 }
   9896 
   9897 static bool
   9898 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   9899 {
   9900   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
   9901 }
   9902 
   9903 static bool
   9904 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   9905 {
   9906   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
   9907 }
   9908 
   9909 static bool
   9910 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   9911 {
   9912   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
   9913 }
   9914 
   9915 static bool
   9916 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   9917 {
   9918   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
   9919 }
   9920 
   9921 static bool
   9922 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   9923 {
   9924   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
   9925 }
   9926 
   9927 static bool
   9928 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   9929 {
   9930   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
   9931 }
   9932 
   9933 static bool
   9934 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   9935 {
   9936   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
   9937 }
   9938 
   9939 static bool
   9940 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   9941 {
   9942   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
   9943 }
   9944 
   9945 static bool
   9946 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   9947 {
   9948   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   9949 }
   9950 
   9951 static bool
   9952 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   9953 {
   9954   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   9955 }
   9956 
   9957 static bool
   9958 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   9959 {
   9960   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   9961 }
   9962 
   9963 static bool
   9964 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   9965 {
   9966   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   9967 }
   9968 
   9969 static bool
   9970 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   9971 {
   9972   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   9973 }
   9974 
   9975 static bool
   9976 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   9977 {
   9978   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   9979 }
   9980 
   9981 static bool
   9982 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   9983 {
   9984   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   9985 }
   9986 
   9987 static bool
   9988 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   9989 {
   9990   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   9991 }
   9992 
   9993 static bool
   9994 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   9995 {
   9996   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   9997 }
   9998 
   9999 static bool
   10000 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   10001 {
   10002   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   10003 }
   10004 
   10005 static bool
   10006 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   10007 {
   10008   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   10009 }
   10010 
   10011 static bool
   10012 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   10013 {
   10014   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   10015 }
   10016 
   10017 static bool
   10018 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10019 {
   10020   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   10021 }
   10022 
   10023 static bool
   10024 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10025 {
   10026   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   10027 }
   10028 
   10029 static bool
   10030 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10031 {
   10032   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   10033 }
   10034 
   10035 static bool
   10036 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10037 {
   10038   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   10039 }
   10040 
   10041 static bool
   10042 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10043 {
   10044   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   10045 }
   10046 
   10047 static bool
   10048 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10049 {
   10050   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
   10051 }
   10052 
   10053 static bool
   10054 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10055 {
   10056   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
   10057 }
   10058 
   10059 static bool
   10060 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10061 {
   10062   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
   10063 					  note);
   10064 }
   10065 
   10066 static bool
   10067 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10068 {
   10069   return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
   10070 }
   10071 
   10072 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10073    successful otherwise, return FALSE.  */
   10074 
   10075 static bool
   10076 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10077 {
   10078   return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
   10079 }
   10080 
   10081 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10082    successful otherwise, return FALSE.  */
   10083 
   10084 static bool
   10085 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10086 {
   10087   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10088 }
   10089 
   10090 static bool
   10091 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10092 {
   10093   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
   10094 }
   10095 
   10096 static bool
   10097 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10098 {
   10099   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
   10100 }
   10101 
   10102 static bool
   10103 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10104 {
   10105   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
   10106 }
   10107 
   10108 static bool
   10109 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10110 {
   10111   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
   10112 }
   10113 
   10114 #if defined (HAVE_PRPSINFO_T)
   10115 typedef prpsinfo_t   elfcore_psinfo_t;
   10116 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10117 typedef prpsinfo32_t elfcore_psinfo32_t;
   10118 #endif
   10119 #endif
   10120 
   10121 #if defined (HAVE_PSINFO_T)
   10122 typedef psinfo_t   elfcore_psinfo_t;
   10123 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10124 typedef psinfo32_t elfcore_psinfo32_t;
   10125 #endif
   10126 #endif
   10127 
   10128 /* return a malloc'ed copy of a string at START which is at
   10129    most MAX bytes long, possibly without a terminating '\0'.
   10130    the copy will always have a terminating '\0'.  */
   10131 
   10132 char *
   10133 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10134 {
   10135   char *dups;
   10136   char *end = (char *) memchr (start, '\0', max);
   10137   size_t len;
   10138 
   10139   if (end == NULL)
   10140     len = max;
   10141   else
   10142     len = end - start;
   10143 
   10144   dups = (char *) bfd_alloc (abfd, len + 1);
   10145   if (dups == NULL)
   10146     return NULL;
   10147 
   10148   memcpy (dups, start, len);
   10149   dups[len] = '\0';
   10150 
   10151   return dups;
   10152 }
   10153 
   10154 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10155 static bool
   10156 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10157 {
   10158   if (note->descsz == sizeof (elfcore_psinfo_t))
   10159     {
   10160       elfcore_psinfo_t psinfo;
   10161 
   10162       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10163 
   10164 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_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 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10176   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10177     {
   10178       /* 64-bit host, 32-bit corefile */
   10179       elfcore_psinfo32_t psinfo;
   10180 
   10181       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10182 
   10183 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10184       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10185 #endif
   10186       elf_tdata (abfd)->core->program
   10187 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10188 				sizeof (psinfo.pr_fname));
   10189 
   10190       elf_tdata (abfd)->core->command
   10191 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10192 				sizeof (psinfo.pr_psargs));
   10193     }
   10194 #endif
   10195 
   10196   else
   10197     {
   10198       /* Fail - we don't know how to handle any other
   10199 	 note size (ie. data object type).  */
   10200       return true;
   10201     }
   10202 
   10203   /* Note that for some reason, a spurious space is tacked
   10204      onto the end of the args in some (at least one anyway)
   10205      implementations, so strip it off if it exists.  */
   10206 
   10207   {
   10208     char *command = elf_tdata (abfd)->core->command;
   10209     int n = strlen (command);
   10210 
   10211     if (0 < n && command[n - 1] == ' ')
   10212       command[n - 1] = '\0';
   10213   }
   10214 
   10215   return true;
   10216 }
   10217 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10218 
   10219 #if defined (HAVE_PSTATUS_T)
   10220 static bool
   10221 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10222 {
   10223   if (note->descsz == sizeof (pstatus_t)
   10224 #if defined (HAVE_PXSTATUS_T)
   10225       || note->descsz == sizeof (pxstatus_t)
   10226 #endif
   10227       )
   10228     {
   10229       pstatus_t pstat;
   10230 
   10231       memcpy (&pstat, note->descdata, sizeof (pstat));
   10232 
   10233       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10234     }
   10235 #if defined (HAVE_PSTATUS32_T)
   10236   else if (note->descsz == sizeof (pstatus32_t))
   10237     {
   10238       /* 64-bit host, 32-bit corefile */
   10239       pstatus32_t pstat;
   10240 
   10241       memcpy (&pstat, note->descdata, sizeof (pstat));
   10242 
   10243       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10244     }
   10245 #endif
   10246   /* Could grab some more details from the "representative"
   10247      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10248      NT_LWPSTATUS note, presumably.  */
   10249 
   10250   return true;
   10251 }
   10252 #endif /* defined (HAVE_PSTATUS_T) */
   10253 
   10254 #if defined (HAVE_LWPSTATUS_T)
   10255 static bool
   10256 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10257 {
   10258   lwpstatus_t lwpstat;
   10259   char buf[100];
   10260   char *name;
   10261   size_t len;
   10262   asection *sect;
   10263 
   10264   if (note->descsz != sizeof (lwpstat)
   10265 #if defined (HAVE_LWPXSTATUS_T)
   10266       && note->descsz != sizeof (lwpxstatus_t)
   10267 #endif
   10268       )
   10269     return true;
   10270 
   10271   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10272 
   10273   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10274   /* Do not overwrite the core signal if it has already been set by
   10275      another thread.  */
   10276   if (elf_tdata (abfd)->core->signal == 0)
   10277     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10278 
   10279   /* Make a ".reg/999" section.  */
   10280 
   10281   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   10282   len = strlen (buf) + 1;
   10283   name = bfd_alloc (abfd, len);
   10284   if (name == NULL)
   10285     return false;
   10286   memcpy (name, buf, len);
   10287 
   10288   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10289   if (sect == NULL)
   10290     return false;
   10291 
   10292 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10293   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   10294   sect->filepos = note->descpos
   10295     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   10296 #endif
   10297 
   10298 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10299   sect->size = sizeof (lwpstat.pr_reg);
   10300   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   10301 #endif
   10302 
   10303   sect->alignment_power = 2;
   10304 
   10305   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   10306     return false;
   10307 
   10308   /* Make a ".reg2/999" section */
   10309 
   10310   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   10311   len = strlen (buf) + 1;
   10312   name = bfd_alloc (abfd, len);
   10313   if (name == NULL)
   10314     return false;
   10315   memcpy (name, buf, len);
   10316 
   10317   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10318   if (sect == NULL)
   10319     return false;
   10320 
   10321 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10322   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   10323   sect->filepos = note->descpos
   10324     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   10325 #endif
   10326 
   10327 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   10328   sect->size = sizeof (lwpstat.pr_fpreg);
   10329   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   10330 #endif
   10331 
   10332   sect->alignment_power = 2;
   10333 
   10334   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   10335 }
   10336 #endif /* defined (HAVE_LWPSTATUS_T) */
   10337 
   10338 /* These constants, and the structure offsets used below, are defined by
   10339    Cygwin's core_dump.h */
   10340 #define NOTE_INFO_PROCESS  1
   10341 #define NOTE_INFO_THREAD   2
   10342 #define NOTE_INFO_MODULE   3
   10343 #define NOTE_INFO_MODULE64 4
   10344 
   10345 static bool
   10346 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   10347 {
   10348   char buf[30];
   10349   char *name;
   10350   size_t len;
   10351   unsigned int name_size;
   10352   asection *sect;
   10353   unsigned int type;
   10354   int is_active_thread;
   10355   bfd_vma base_addr;
   10356 
   10357   if (note->descsz < 4)
   10358     return true;
   10359 
   10360   if (! startswith (note->namedata, "win32"))
   10361     return true;
   10362 
   10363   type = bfd_get_32 (abfd, note->descdata);
   10364 
   10365   struct
   10366   {
   10367     const char *type_name;
   10368     unsigned long min_size;
   10369   } size_check[] =
   10370       {
   10371        { "NOTE_INFO_PROCESS", 12 },
   10372        { "NOTE_INFO_THREAD", 12 },
   10373        { "NOTE_INFO_MODULE", 12 },
   10374        { "NOTE_INFO_MODULE64", 16 },
   10375       };
   10376 
   10377   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   10378       return true;
   10379 
   10380   if (note->descsz < size_check[type - 1].min_size)
   10381     {
   10382       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
   10383                           abfd, size_check[type - 1].type_name, note->descsz);
   10384       return true;
   10385     }
   10386 
   10387   switch (type)
   10388     {
   10389     case NOTE_INFO_PROCESS:
   10390       /* FIXME: need to add ->core->command.  */
   10391       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   10392       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   10393       break;
   10394 
   10395     case NOTE_INFO_THREAD:
   10396       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   10397          structure. */
   10398       /* thread_info.tid */
   10399       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   10400 
   10401       len = strlen (buf) + 1;
   10402       name = (char *) bfd_alloc (abfd, len);
   10403       if (name == NULL)
   10404 	return false;
   10405 
   10406       memcpy (name, buf, len);
   10407 
   10408       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10409       if (sect == NULL)
   10410 	return false;
   10411 
   10412       /* sizeof (thread_info.thread_context) */
   10413       sect->size = note->descsz - 12;
   10414       /* offsetof (thread_info.thread_context) */
   10415       sect->filepos = note->descpos + 12;
   10416       sect->alignment_power = 2;
   10417 
   10418       /* thread_info.is_active_thread */
   10419       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   10420 
   10421       if (is_active_thread)
   10422 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   10423 	  return false;
   10424       break;
   10425 
   10426     case NOTE_INFO_MODULE:
   10427     case NOTE_INFO_MODULE64:
   10428       /* Make a ".module/xxxxxxxx" section.  */
   10429       if (type == NOTE_INFO_MODULE)
   10430         {
   10431           /* module_info.base_address */
   10432           base_addr = bfd_get_32 (abfd, note->descdata + 4);
   10433           sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   10434           /* module_info.module_name_size */
   10435           name_size = bfd_get_32 (abfd, note->descdata + 8);
   10436         }
   10437       else /* NOTE_INFO_MODULE64 */
   10438         {
   10439           /* module_info.base_address */
   10440           base_addr = bfd_get_64 (abfd, note->descdata + 4);
   10441           sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   10442           /* module_info.module_name_size */
   10443           name_size = bfd_get_32 (abfd, note->descdata + 12);
   10444         }
   10445 
   10446       len = strlen (buf) + 1;
   10447       name = (char *) bfd_alloc (abfd, len);
   10448       if (name == NULL)
   10449 	return false;
   10450 
   10451       memcpy (name, buf, len);
   10452 
   10453       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10454 
   10455       if (sect == NULL)
   10456 	return false;
   10457 
   10458       if (note->descsz < 12 + name_size)
   10459         {
   10460           _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
   10461                               abfd, note->descsz, name_size);
   10462           return true;
   10463         }
   10464 
   10465       sect->size = note->descsz;
   10466       sect->filepos = note->descpos;
   10467       sect->alignment_power = 2;
   10468       break;
   10469 
   10470     default:
   10471       return true;
   10472     }
   10473 
   10474   return true;
   10475 }
   10476 
   10477 static bool
   10478 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   10479 {
   10480   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   10481 
   10482   switch (note->type)
   10483     {
   10484     default:
   10485       return true;
   10486 
   10487     case NT_PRSTATUS:
   10488       if (bed->elf_backend_grok_prstatus)
   10489 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   10490 	  return true;
   10491 #if defined (HAVE_PRSTATUS_T)
   10492       return elfcore_grok_prstatus (abfd, note);
   10493 #else
   10494       return true;
   10495 #endif
   10496 
   10497 #if defined (HAVE_PSTATUS_T)
   10498     case NT_PSTATUS:
   10499       return elfcore_grok_pstatus (abfd, note);
   10500 #endif
   10501 
   10502 #if defined (HAVE_LWPSTATUS_T)
   10503     case NT_LWPSTATUS:
   10504       return elfcore_grok_lwpstatus (abfd, note);
   10505 #endif
   10506 
   10507     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   10508       return elfcore_grok_prfpreg (abfd, note);
   10509 
   10510     case NT_WIN32PSTATUS:
   10511       return elfcore_grok_win32pstatus (abfd, note);
   10512 
   10513     case NT_PRXFPREG:		/* Linux SSE extension */
   10514       if (note->namesz == 6
   10515 	  && strcmp (note->namedata, "LINUX") == 0)
   10516 	return elfcore_grok_prxfpreg (abfd, note);
   10517       else
   10518 	return true;
   10519 
   10520     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   10521       if (note->namesz == 6
   10522 	  && strcmp (note->namedata, "LINUX") == 0)
   10523 	return elfcore_grok_xstatereg (abfd, note);
   10524       else
   10525 	return true;
   10526 
   10527     case NT_PPC_VMX:
   10528       if (note->namesz == 6
   10529 	  && strcmp (note->namedata, "LINUX") == 0)
   10530 	return elfcore_grok_ppc_vmx (abfd, note);
   10531       else
   10532 	return true;
   10533 
   10534     case NT_PPC_VSX:
   10535       if (note->namesz == 6
   10536 	  && strcmp (note->namedata, "LINUX") == 0)
   10537 	return elfcore_grok_ppc_vsx (abfd, note);
   10538       else
   10539 	return true;
   10540 
   10541     case NT_PPC_TAR:
   10542       if (note->namesz == 6
   10543 	  && strcmp (note->namedata, "LINUX") == 0)
   10544 	return elfcore_grok_ppc_tar (abfd, note);
   10545       else
   10546 	return true;
   10547 
   10548     case NT_PPC_PPR:
   10549       if (note->namesz == 6
   10550 	  && strcmp (note->namedata, "LINUX") == 0)
   10551 	return elfcore_grok_ppc_ppr (abfd, note);
   10552       else
   10553 	return true;
   10554 
   10555     case NT_PPC_DSCR:
   10556       if (note->namesz == 6
   10557 	  && strcmp (note->namedata, "LINUX") == 0)
   10558 	return elfcore_grok_ppc_dscr (abfd, note);
   10559       else
   10560 	return true;
   10561 
   10562     case NT_PPC_EBB:
   10563       if (note->namesz == 6
   10564 	  && strcmp (note->namedata, "LINUX") == 0)
   10565 	return elfcore_grok_ppc_ebb (abfd, note);
   10566       else
   10567 	return true;
   10568 
   10569     case NT_PPC_PMU:
   10570       if (note->namesz == 6
   10571 	  && strcmp (note->namedata, "LINUX") == 0)
   10572 	return elfcore_grok_ppc_pmu (abfd, note);
   10573       else
   10574 	return true;
   10575 
   10576     case NT_PPC_TM_CGPR:
   10577       if (note->namesz == 6
   10578 	  && strcmp (note->namedata, "LINUX") == 0)
   10579 	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   10580       else
   10581 	return true;
   10582 
   10583     case NT_PPC_TM_CFPR:
   10584       if (note->namesz == 6
   10585 	  && strcmp (note->namedata, "LINUX") == 0)
   10586 	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   10587       else
   10588 	return true;
   10589 
   10590     case NT_PPC_TM_CVMX:
   10591       if (note->namesz == 6
   10592 	  && strcmp (note->namedata, "LINUX") == 0)
   10593 	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   10594       else
   10595 	return true;
   10596 
   10597     case NT_PPC_TM_CVSX:
   10598       if (note->namesz == 6
   10599 	  && strcmp (note->namedata, "LINUX") == 0)
   10600 	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   10601       else
   10602 	return true;
   10603 
   10604     case NT_PPC_TM_SPR:
   10605       if (note->namesz == 6
   10606 	  && strcmp (note->namedata, "LINUX") == 0)
   10607 	return elfcore_grok_ppc_tm_spr (abfd, note);
   10608       else
   10609 	return true;
   10610 
   10611     case NT_PPC_TM_CTAR:
   10612       if (note->namesz == 6
   10613 	  && strcmp (note->namedata, "LINUX") == 0)
   10614 	return elfcore_grok_ppc_tm_ctar (abfd, note);
   10615       else
   10616 	return true;
   10617 
   10618     case NT_PPC_TM_CPPR:
   10619       if (note->namesz == 6
   10620 	  && strcmp (note->namedata, "LINUX") == 0)
   10621 	return elfcore_grok_ppc_tm_cppr (abfd, note);
   10622       else
   10623 	return true;
   10624 
   10625     case NT_PPC_TM_CDSCR:
   10626       if (note->namesz == 6
   10627 	  && strcmp (note->namedata, "LINUX") == 0)
   10628 	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   10629       else
   10630 	return true;
   10631 
   10632     case NT_S390_HIGH_GPRS:
   10633       if (note->namesz == 6
   10634 	  && strcmp (note->namedata, "LINUX") == 0)
   10635 	return elfcore_grok_s390_high_gprs (abfd, note);
   10636       else
   10637 	return true;
   10638 
   10639     case NT_S390_TIMER:
   10640       if (note->namesz == 6
   10641 	  && strcmp (note->namedata, "LINUX") == 0)
   10642 	return elfcore_grok_s390_timer (abfd, note);
   10643       else
   10644 	return true;
   10645 
   10646     case NT_S390_TODCMP:
   10647       if (note->namesz == 6
   10648 	  && strcmp (note->namedata, "LINUX") == 0)
   10649 	return elfcore_grok_s390_todcmp (abfd, note);
   10650       else
   10651 	return true;
   10652 
   10653     case NT_S390_TODPREG:
   10654       if (note->namesz == 6
   10655 	  && strcmp (note->namedata, "LINUX") == 0)
   10656 	return elfcore_grok_s390_todpreg (abfd, note);
   10657       else
   10658 	return true;
   10659 
   10660     case NT_S390_CTRS:
   10661       if (note->namesz == 6
   10662 	  && strcmp (note->namedata, "LINUX") == 0)
   10663 	return elfcore_grok_s390_ctrs (abfd, note);
   10664       else
   10665 	return true;
   10666 
   10667     case NT_S390_PREFIX:
   10668       if (note->namesz == 6
   10669 	  && strcmp (note->namedata, "LINUX") == 0)
   10670 	return elfcore_grok_s390_prefix (abfd, note);
   10671       else
   10672 	return true;
   10673 
   10674     case NT_S390_LAST_BREAK:
   10675       if (note->namesz == 6
   10676 	  && strcmp (note->namedata, "LINUX") == 0)
   10677 	return elfcore_grok_s390_last_break (abfd, note);
   10678       else
   10679 	return true;
   10680 
   10681     case NT_S390_SYSTEM_CALL:
   10682       if (note->namesz == 6
   10683 	  && strcmp (note->namedata, "LINUX") == 0)
   10684 	return elfcore_grok_s390_system_call (abfd, note);
   10685       else
   10686 	return true;
   10687 
   10688     case NT_S390_TDB:
   10689       if (note->namesz == 6
   10690 	  && strcmp (note->namedata, "LINUX") == 0)
   10691 	return elfcore_grok_s390_tdb (abfd, note);
   10692       else
   10693 	return true;
   10694 
   10695     case NT_S390_VXRS_LOW:
   10696       if (note->namesz == 6
   10697 	  && strcmp (note->namedata, "LINUX") == 0)
   10698 	return elfcore_grok_s390_vxrs_low (abfd, note);
   10699       else
   10700 	return true;
   10701 
   10702     case NT_S390_VXRS_HIGH:
   10703       if (note->namesz == 6
   10704 	  && strcmp (note->namedata, "LINUX") == 0)
   10705 	return elfcore_grok_s390_vxrs_high (abfd, note);
   10706       else
   10707 	return true;
   10708 
   10709     case NT_S390_GS_CB:
   10710       if (note->namesz == 6
   10711 	  && strcmp (note->namedata, "LINUX") == 0)
   10712 	return elfcore_grok_s390_gs_cb (abfd, note);
   10713       else
   10714 	return true;
   10715 
   10716     case NT_S390_GS_BC:
   10717       if (note->namesz == 6
   10718 	  && strcmp (note->namedata, "LINUX") == 0)
   10719 	return elfcore_grok_s390_gs_bc (abfd, note);
   10720       else
   10721 	return true;
   10722 
   10723     case NT_ARC_V2:
   10724       if (note->namesz == 6
   10725 	  && strcmp (note->namedata, "LINUX") == 0)
   10726 	return elfcore_grok_arc_v2 (abfd, note);
   10727       else
   10728 	return true;
   10729 
   10730     case NT_ARM_VFP:
   10731       if (note->namesz == 6
   10732 	  && strcmp (note->namedata, "LINUX") == 0)
   10733 	return elfcore_grok_arm_vfp (abfd, note);
   10734       else
   10735 	return true;
   10736 
   10737     case NT_ARM_TLS:
   10738       if (note->namesz == 6
   10739 	  && strcmp (note->namedata, "LINUX") == 0)
   10740 	return elfcore_grok_aarch_tls (abfd, note);
   10741       else
   10742 	return true;
   10743 
   10744     case NT_ARM_HW_BREAK:
   10745       if (note->namesz == 6
   10746 	  && strcmp (note->namedata, "LINUX") == 0)
   10747 	return elfcore_grok_aarch_hw_break (abfd, note);
   10748       else
   10749 	return true;
   10750 
   10751     case NT_ARM_HW_WATCH:
   10752       if (note->namesz == 6
   10753 	  && strcmp (note->namedata, "LINUX") == 0)
   10754 	return elfcore_grok_aarch_hw_watch (abfd, note);
   10755       else
   10756 	return true;
   10757 
   10758     case NT_ARM_SVE:
   10759       if (note->namesz == 6
   10760 	  && strcmp (note->namedata, "LINUX") == 0)
   10761 	return elfcore_grok_aarch_sve (abfd, note);
   10762       else
   10763 	return true;
   10764 
   10765     case NT_ARM_PAC_MASK:
   10766       if (note->namesz == 6
   10767 	  && strcmp (note->namedata, "LINUX") == 0)
   10768 	return elfcore_grok_aarch_pauth (abfd, note);
   10769       else
   10770 	return true;
   10771 
   10772     case NT_ARM_TAGGED_ADDR_CTRL:
   10773       if (note->namesz == 6
   10774 	  && strcmp (note->namedata, "LINUX") == 0)
   10775 	return elfcore_grok_aarch_mte (abfd, note);
   10776       else
   10777 	return true;
   10778 
   10779     case NT_GDB_TDESC:
   10780       if (note->namesz == 4
   10781           && strcmp (note->namedata, "GDB") == 0)
   10782         return elfcore_grok_gdb_tdesc (abfd, note);
   10783       else
   10784         return true;
   10785 
   10786     case NT_RISCV_CSR:
   10787       if (note->namesz == 4
   10788           && strcmp (note->namedata, "GDB") == 0)
   10789         return elfcore_grok_riscv_csr (abfd, note);
   10790       else
   10791 	return true;
   10792 
   10793     case NT_LARCH_CPUCFG:
   10794       if (note->namesz == 6
   10795 	  && strcmp (note->namedata, "LINUX") == 0)
   10796 	return elfcore_grok_loongarch_cpucfg (abfd, note);
   10797       else
   10798 	return true;
   10799 
   10800     case NT_LARCH_LBT:
   10801       if (note->namesz == 6
   10802 	  && strcmp (note->namedata, "LINUX") == 0)
   10803 	return elfcore_grok_loongarch_lbt (abfd, note);
   10804       else
   10805 	return true;
   10806 
   10807     case NT_LARCH_LSX:
   10808       if (note->namesz == 6
   10809 	  && strcmp (note->namedata, "LINUX") == 0)
   10810 	return elfcore_grok_loongarch_lsx (abfd, note);
   10811       else
   10812 	return true;
   10813 
   10814     case NT_LARCH_LASX:
   10815       if (note->namesz == 6
   10816 	  && strcmp (note->namedata, "LINUX") == 0)
   10817 	return elfcore_grok_loongarch_lasx (abfd, note);
   10818       else
   10819 	return true;
   10820 
   10821     case NT_PRPSINFO:
   10822     case NT_PSINFO:
   10823       if (bed->elf_backend_grok_psinfo)
   10824 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   10825 	  return true;
   10826 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10827       return elfcore_grok_psinfo (abfd, note);
   10828 #else
   10829       return true;
   10830 #endif
   10831 
   10832     case NT_AUXV:
   10833       return elfcore_make_auxv_note_section (abfd, note, 0);
   10834 
   10835     case NT_FILE:
   10836       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   10837 					      note);
   10838 
   10839     case NT_SIGINFO:
   10840       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   10841 					      note);
   10842 
   10843     }
   10844 }
   10845 
   10846 static bool
   10847 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   10848 {
   10849   struct bfd_build_id* build_id;
   10850 
   10851   if (note->descsz == 0)
   10852     return false;
   10853 
   10854   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   10855   if (build_id == NULL)
   10856     return false;
   10857 
   10858   build_id->size = note->descsz;
   10859   memcpy (build_id->data, note->descdata, note->descsz);
   10860   abfd->build_id = build_id;
   10861 
   10862   return true;
   10863 }
   10864 
   10865 static bool
   10866 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   10867 {
   10868   switch (note->type)
   10869     {
   10870     default:
   10871       return true;
   10872 
   10873     case NT_GNU_PROPERTY_TYPE_0:
   10874       return _bfd_elf_parse_gnu_properties (abfd, note);
   10875 
   10876     case NT_GNU_BUILD_ID:
   10877       return elfobj_grok_gnu_build_id (abfd, note);
   10878     }
   10879 }
   10880 
   10881 static bool
   10882 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   10883 {
   10884   struct sdt_note *cur =
   10885     (struct sdt_note *) bfd_alloc (abfd,
   10886 				   sizeof (struct sdt_note) + note->descsz);
   10887 
   10888   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   10889   cur->size = (bfd_size_type) note->descsz;
   10890   memcpy (cur->data, note->descdata, note->descsz);
   10891 
   10892   elf_tdata (abfd)->sdt_note_head = cur;
   10893 
   10894   return true;
   10895 }
   10896 
   10897 static bool
   10898 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   10899 {
   10900   switch (note->type)
   10901     {
   10902     case NT_STAPSDT:
   10903       return elfobj_grok_stapsdt_note_1 (abfd, note);
   10904 
   10905     default:
   10906       return true;
   10907     }
   10908 }
   10909 
   10910 static bool
   10911 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10912 {
   10913   size_t offset;
   10914 
   10915   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10916     {
   10917     case ELFCLASS32:
   10918       if (note->descsz < 108)
   10919 	return false;
   10920       break;
   10921 
   10922     case ELFCLASS64:
   10923       if (note->descsz < 120)
   10924 	return false;
   10925       break;
   10926 
   10927     default:
   10928       return false;
   10929     }
   10930 
   10931   /* Check for version 1 in pr_version.  */
   10932   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10933     return false;
   10934 
   10935   offset = 4;
   10936 
   10937   /* Skip over pr_psinfosz. */
   10938   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   10939     offset += 4;
   10940   else
   10941     {
   10942       offset += 4;	/* Padding before pr_psinfosz. */
   10943       offset += 8;
   10944     }
   10945 
   10946   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   10947   elf_tdata (abfd)->core->program
   10948     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   10949   offset += 17;
   10950 
   10951   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   10952   elf_tdata (abfd)->core->command
   10953     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   10954   offset += 81;
   10955 
   10956   /* Padding before pr_pid.  */
   10957   offset += 2;
   10958 
   10959   /* The pr_pid field was added in version "1a".  */
   10960   if (note->descsz < offset + 4)
   10961     return true;
   10962 
   10963   elf_tdata (abfd)->core->pid
   10964     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   10965 
   10966   return true;
   10967 }
   10968 
   10969 static bool
   10970 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10971 {
   10972   size_t offset;
   10973   size_t size;
   10974   size_t min_size;
   10975 
   10976   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   10977      Also compute minimum size of this note.  */
   10978   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   10979     {
   10980     case ELFCLASS32:
   10981       offset = 4 + 4;
   10982       min_size = offset + (4 * 2) + 4 + 4 + 4;
   10983       break;
   10984 
   10985     case ELFCLASS64:
   10986       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   10987       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   10988       break;
   10989 
   10990     default:
   10991       return false;
   10992     }
   10993 
   10994   if (note->descsz < min_size)
   10995     return false;
   10996 
   10997   /* Check for version 1 in pr_version.  */
   10998   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   10999     return false;
   11000 
   11001   /* Extract size of pr_reg from pr_gregsetsz.  */
   11002   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   11003   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11004     {
   11005       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11006       offset += 4 * 2;
   11007     }
   11008   else
   11009     {
   11010       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   11011       offset += 8 * 2;
   11012     }
   11013 
   11014   /* Skip over pr_osreldate.  */
   11015   offset += 4;
   11016 
   11017   /* Read signal from pr_cursig.  */
   11018   if (elf_tdata (abfd)->core->signal == 0)
   11019     elf_tdata (abfd)->core->signal
   11020       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11021   offset += 4;
   11022 
   11023   /* Read TID from pr_pid.  */
   11024   elf_tdata (abfd)->core->lwpid
   11025       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11026   offset += 4;
   11027 
   11028   /* Padding before pr_reg.  */
   11029   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11030     offset += 4;
   11031 
   11032   /* Make sure that there is enough data remaining in the note.  */
   11033   if ((note->descsz - offset) < size)
   11034     return false;
   11035 
   11036   /* Make a ".reg/999" section and a ".reg" section.  */
   11037   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   11038 					  size, note->descpos + offset);
   11039 }
   11040 
   11041 static bool
   11042 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11043 {
   11044   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11045 
   11046   switch (note->type)
   11047     {
   11048     case NT_PRSTATUS:
   11049       if (bed->elf_backend_grok_freebsd_prstatus)
   11050 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11051 	  return true;
   11052       return elfcore_grok_freebsd_prstatus (abfd, note);
   11053 
   11054     case NT_FPREGSET:
   11055       return elfcore_grok_prfpreg (abfd, note);
   11056 
   11057     case NT_PRPSINFO:
   11058       return elfcore_grok_freebsd_psinfo (abfd, note);
   11059 
   11060     case NT_FREEBSD_THRMISC:
   11061       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11062 
   11063     case NT_FREEBSD_PROCSTAT_PROC:
   11064       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11065 					      note);
   11066 
   11067     case NT_FREEBSD_PROCSTAT_FILES:
   11068       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11069 					      note);
   11070 
   11071     case NT_FREEBSD_PROCSTAT_VMMAP:
   11072       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11073 					      note);
   11074 
   11075     case NT_FREEBSD_PROCSTAT_AUXV:
   11076       return elfcore_make_auxv_note_section (abfd, note, 4);
   11077 
   11078     case NT_FREEBSD_X86_SEGBASES:
   11079       return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
   11080 
   11081     case NT_X86_XSTATE:
   11082       return elfcore_grok_xstatereg (abfd, note);
   11083 
   11084     case NT_FREEBSD_PTLWPINFO:
   11085       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11086 					      note);
   11087 
   11088     case NT_ARM_TLS:
   11089       return elfcore_grok_aarch_tls (abfd, note);
   11090 
   11091     case NT_ARM_VFP:
   11092       return elfcore_grok_arm_vfp (abfd, note);
   11093 
   11094     default:
   11095       return true;
   11096     }
   11097 }
   11098 
   11099 static bool
   11100 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11101 {
   11102   char *cp;
   11103 
   11104   cp = strchr (note->namedata, '@');
   11105   if (cp != NULL)
   11106     {
   11107       *lwpidp = atoi(cp + 1);
   11108       return true;
   11109     }
   11110   return false;
   11111 }
   11112 
   11113 static bool
   11114 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11115 {
   11116   if (note->descsz <= 0x7c + 31)
   11117     return false;
   11118 
   11119   /* Signal number at offset 0x08. */
   11120   elf_tdata (abfd)->core->signal
   11121     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11122 
   11123   /* Process ID at offset 0x50. */
   11124   elf_tdata (abfd)->core->pid
   11125     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11126 
   11127   /* Command name at 0x7c (max 32 bytes, including nul). */
   11128   elf_tdata (abfd)->core->command
   11129     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11130 
   11131   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11132 					  note);
   11133 }
   11134 
   11135 static bool
   11136 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11137 {
   11138   int lwp;
   11139 
   11140   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11141     elf_tdata (abfd)->core->lwpid = lwp;
   11142 
   11143   switch (note->type)
   11144     {
   11145     case NT_NETBSDCORE_PROCINFO:
   11146       /* NetBSD-specific core "procinfo".  Note that we expect to
   11147 	 find this note before any of the others, which is fine,
   11148 	 since the kernel writes this note out first when it
   11149 	 creates a core file.  */
   11150       return elfcore_grok_netbsd_procinfo (abfd, note);
   11151     case NT_NETBSDCORE_AUXV:
   11152       /* NetBSD-specific Elf Auxiliary Vector data. */
   11153       return elfcore_make_auxv_note_section (abfd, note, 4);
   11154     case NT_NETBSDCORE_LWPSTATUS:
   11155       return elfcore_make_note_pseudosection (abfd,
   11156 					      ".note.netbsdcore.lwpstatus",
   11157 					      note);
   11158     default:
   11159       break;
   11160     }
   11161 
   11162   /* As of March 2020 there are no other machine-independent notes
   11163      defined for NetBSD core files.  If the note type is less
   11164      than the start of the machine-dependent note types, we don't
   11165      understand it.  */
   11166 
   11167   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11168     return true;
   11169 
   11170 
   11171   switch (bfd_get_arch (abfd))
   11172     {
   11173       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11174 	 PT_GETFPREGS == mach+2.  */
   11175 
   11176     case bfd_arch_aarch64:
   11177     case bfd_arch_alpha:
   11178     case bfd_arch_sparc:
   11179       switch (note->type)
   11180 	{
   11181 	case NT_NETBSDCORE_FIRSTMACH+0:
   11182 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11183 
   11184 	case NT_NETBSDCORE_FIRSTMACH+2:
   11185 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11186 
   11187 	default:
   11188 	  return true;
   11189 	}
   11190 
   11191       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11192 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11193 	 structure which lacks GBR.  */
   11194 
   11195     case bfd_arch_sh:
   11196       switch (note->type)
   11197 	{
   11198 	case NT_NETBSDCORE_FIRSTMACH+3:
   11199 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11200 
   11201 	case NT_NETBSDCORE_FIRSTMACH+5:
   11202 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11203 
   11204 	default:
   11205 	  return true;
   11206 	}
   11207 
   11208       /* On all other arch's, PT_GETREGS == mach+1 and
   11209 	 PT_GETFPREGS == mach+3.  */
   11210 
   11211     default:
   11212       switch (note->type)
   11213 	{
   11214 	case NT_NETBSDCORE_FIRSTMACH+1:
   11215 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11216 
   11217 	case NT_NETBSDCORE_FIRSTMACH+3:
   11218 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11219 
   11220 	default:
   11221 	  return true;
   11222 	}
   11223     }
   11224     /* NOTREACHED */
   11225 }
   11226 
   11227 static bool
   11228 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11229 {
   11230   if (note->descsz <= 0x48 + 31)
   11231     return false;
   11232 
   11233   /* Signal number at offset 0x08. */
   11234   elf_tdata (abfd)->core->signal
   11235     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11236 
   11237   /* Process ID at offset 0x20. */
   11238   elf_tdata (abfd)->core->pid
   11239     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11240 
   11241   /* Command name at 0x48 (max 32 bytes, including nul). */
   11242   elf_tdata (abfd)->core->command
   11243     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11244 
   11245   return true;
   11246 }
   11247 
   11248 /* Processes Solaris's process status note.
   11249    sig_off ~ offsetof(prstatus_t, pr_cursig)
   11250    pid_off ~ offsetof(prstatus_t, pr_pid)
   11251    lwpid_off ~ offsetof(prstatus_t, pr_who)
   11252    gregset_size ~ sizeof(gregset_t)
   11253    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   11254 
   11255 static bool
   11256 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   11257 			       int pid_off, int lwpid_off, size_t gregset_size,
   11258 			       size_t gregset_offset)
   11259 {
   11260   asection *sect = NULL;
   11261   elf_tdata (abfd)->core->signal
   11262     = bfd_get_16 (abfd, note->descdata + sig_off);
   11263   elf_tdata (abfd)->core->pid
   11264     = bfd_get_32 (abfd, note->descdata + pid_off);
   11265   elf_tdata (abfd)->core->lwpid
   11266     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   11267 
   11268   sect = bfd_get_section_by_name (abfd, ".reg");
   11269   if (sect != NULL)
   11270     sect->size = gregset_size;
   11271 
   11272   return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11273 					  note->descpos + gregset_offset);
   11274 }
   11275 
   11276 /* Gets program and arguments from a core.
   11277    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   11278    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   11279 
   11280 static bool
   11281 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   11282 			  int prog_off, int comm_off)
   11283 {
   11284   elf_tdata (abfd)->core->program
   11285     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   11286   elf_tdata (abfd)->core->command
   11287     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   11288 
   11289   return true;
   11290 }
   11291 
   11292 /* Processes Solaris's LWP status note.
   11293    gregset_size ~ sizeof(gregset_t)
   11294    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   11295    fpregset_size ~ sizeof(fpregset_t)
   11296    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   11297 
   11298 static bool
   11299 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   11300 				size_t gregset_size, int gregset_off,
   11301 				size_t fpregset_size, int fpregset_off)
   11302 {
   11303   asection *sect = NULL;
   11304   char reg2_section_name[16] = { 0 };
   11305 
   11306   (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
   11307 		   elf_tdata (abfd)->core->lwpid);
   11308 
   11309   /* offsetof(lwpstatus_t, pr_lwpid) */
   11310   elf_tdata (abfd)->core->lwpid
   11311     = bfd_get_32 (abfd, note->descdata + 4);
   11312   /* offsetof(lwpstatus_t, pr_cursig) */
   11313   elf_tdata (abfd)->core->signal
   11314     = bfd_get_16 (abfd, note->descdata + 12);
   11315 
   11316   sect = bfd_get_section_by_name (abfd, ".reg");
   11317   if (sect != NULL)
   11318     sect->size = gregset_size;
   11319   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11320 					     note->descpos + gregset_off))
   11321     return false;
   11322 
   11323   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   11324   if (sect != NULL)
   11325     {
   11326       sect->size = fpregset_size;
   11327       sect->filepos = note->descpos + fpregset_off;
   11328       sect->alignment_power = 2;
   11329     }
   11330   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
   11331 					     note->descpos + fpregset_off))
   11332     return false;
   11333 
   11334   return true;
   11335 }
   11336 
   11337 static bool
   11338 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   11339 {
   11340   if (note == NULL)
   11341     return false;
   11342 
   11343   /* core files are identified as 32- or 64-bit, SPARC or x86,
   11344      by the size of the descsz which matches the sizeof()
   11345      the type appropriate for that note type (e.g., prstatus_t for
   11346      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   11347      on Solaris. The core file bitness may differ from the bitness of
   11348      gdb itself, so fixed values are used instead of sizeof().
   11349      Appropriate fixed offsets are also used to obtain data from
   11350      the note.  */
   11351 
   11352   switch ((int) note->type)
   11353     {
   11354     case SOLARIS_NT_PRSTATUS:
   11355       switch (note->descsz)
   11356 	{
   11357 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   11358 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11359 					       136, 216, 308, 152, 356);
   11360 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   11361 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11362 					       264, 360, 520, 304, 600);
   11363 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   11364 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11365 					       136, 216, 308, 76, 356);
   11366 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   11367 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11368 					       264, 360, 520, 224, 600);
   11369 	default:
   11370 	  return true;
   11371 	}
   11372 
   11373     case SOLARIS_NT_PSINFO:
   11374     case SOLARIS_NT_PRPSINFO:
   11375       switch (note->descsz)
   11376 	{
   11377 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   11378 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   11379 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   11380 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   11381 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   11382 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   11383 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   11384 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   11385 	default:
   11386 	  return true;
   11387 	}
   11388 
   11389     case SOLARIS_NT_LWPSTATUS:
   11390       switch (note->descsz)
   11391 	{
   11392 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   11393 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11394 						152, 344, 400, 496);
   11395 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   11396 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11397 						304, 544, 544, 848);
   11398 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   11399 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11400 						76, 344, 380, 420);
   11401 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   11402 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11403 						224, 544, 528, 768);
   11404 	default:
   11405 	  return true;
   11406 	}
   11407 
   11408     case SOLARIS_NT_LWPSINFO:
   11409       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   11410       if (note->descsz == 128 || note->descsz == 152)
   11411 	elf_tdata (abfd)->core->lwpid =
   11412 	  bfd_get_32 (abfd, note->descdata + 4);
   11413       break;
   11414 
   11415     default:
   11416       break;
   11417     }
   11418 
   11419   return true;
   11420 }
   11421 
   11422 /* For name starting with "CORE" this may be either a Solaris
   11423    core file or a gdb-generated core file.  Do Solaris-specific
   11424    processing on selected note types first with
   11425    elfcore_grok_solaris_note(), then process the note
   11426    in elfcore_grok_note().  */
   11427 
   11428 static bool
   11429 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   11430 {
   11431   if (!elfcore_grok_solaris_note_impl (abfd, note))
   11432     return false;
   11433 
   11434   return elfcore_grok_note (abfd, note);
   11435 }
   11436 
   11437 static bool
   11438 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11439 {
   11440   if (note->type == NT_OPENBSD_PROCINFO)
   11441     return elfcore_grok_openbsd_procinfo (abfd, note);
   11442 
   11443   if (note->type == NT_OPENBSD_REGS)
   11444     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11445 
   11446   if (note->type == NT_OPENBSD_FPREGS)
   11447     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11448 
   11449   if (note->type == NT_OPENBSD_XFPREGS)
   11450     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   11451 
   11452   if (note->type == NT_OPENBSD_AUXV)
   11453     return elfcore_make_auxv_note_section (abfd, note, 0);
   11454 
   11455   if (note->type == NT_OPENBSD_WCOOKIE)
   11456     {
   11457       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   11458 							   SEC_HAS_CONTENTS);
   11459 
   11460       if (sect == NULL)
   11461 	return false;
   11462       sect->size = note->descsz;
   11463       sect->filepos = note->descpos;
   11464       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   11465 
   11466       return true;
   11467     }
   11468 
   11469   return true;
   11470 }
   11471 
   11472 static bool
   11473 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   11474 {
   11475   void *ddata = note->descdata;
   11476   char buf[100];
   11477   char *name;
   11478   asection *sect;
   11479   short sig;
   11480   unsigned flags;
   11481 
   11482   if (note->descsz < 16)
   11483     return false;
   11484 
   11485   /* nto_procfs_status 'pid' field is at offset 0.  */
   11486   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   11487 
   11488   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   11489   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   11490 
   11491   /* nto_procfs_status 'flags' field is at offset 8.  */
   11492   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   11493 
   11494   /* nto_procfs_status 'what' field is at offset 14.  */
   11495   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   11496     {
   11497       elf_tdata (abfd)->core->signal = sig;
   11498       elf_tdata (abfd)->core->lwpid = *tid;
   11499     }
   11500 
   11501   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   11502      do not come from signals so we make sure we set the current
   11503      thread just in case.  */
   11504   if (flags & 0x00000080)
   11505     elf_tdata (abfd)->core->lwpid = *tid;
   11506 
   11507   /* Make a ".qnx_core_status/%d" section.  */
   11508   sprintf (buf, ".qnx_core_status/%ld", *tid);
   11509 
   11510   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11511   if (name == NULL)
   11512     return false;
   11513   strcpy (name, buf);
   11514 
   11515   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11516   if (sect == NULL)
   11517     return false;
   11518 
   11519   sect->size		= note->descsz;
   11520   sect->filepos		= note->descpos;
   11521   sect->alignment_power = 2;
   11522 
   11523   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   11524 }
   11525 
   11526 static bool
   11527 elfcore_grok_nto_regs (bfd *abfd,
   11528 		       Elf_Internal_Note *note,
   11529 		       long tid,
   11530 		       char *base)
   11531 {
   11532   char buf[100];
   11533   char *name;
   11534   asection *sect;
   11535 
   11536   /* Make a "(base)/%d" section.  */
   11537   sprintf (buf, "%s/%ld", base, tid);
   11538 
   11539   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11540   if (name == NULL)
   11541     return false;
   11542   strcpy (name, buf);
   11543 
   11544   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11545   if (sect == NULL)
   11546     return false;
   11547 
   11548   sect->size		= note->descsz;
   11549   sect->filepos		= note->descpos;
   11550   sect->alignment_power = 2;
   11551 
   11552   /* This is the current thread.  */
   11553   if (elf_tdata (abfd)->core->lwpid == tid)
   11554     return elfcore_maybe_make_sect (abfd, base, sect);
   11555 
   11556   return true;
   11557 }
   11558 
   11559 #define BFD_QNT_CORE_INFO	7
   11560 #define BFD_QNT_CORE_STATUS	8
   11561 #define BFD_QNT_CORE_GREG	9
   11562 #define BFD_QNT_CORE_FPREG	10
   11563 
   11564 static bool
   11565 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   11566 {
   11567   /* Every GREG section has a STATUS section before it.  Store the
   11568      tid from the previous call to pass down to the next gregs
   11569      function.  */
   11570   static long tid = 1;
   11571 
   11572   switch (note->type)
   11573     {
   11574     case BFD_QNT_CORE_INFO:
   11575       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   11576     case BFD_QNT_CORE_STATUS:
   11577       return elfcore_grok_nto_status (abfd, note, &tid);
   11578     case BFD_QNT_CORE_GREG:
   11579       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   11580     case BFD_QNT_CORE_FPREG:
   11581       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   11582     default:
   11583       return true;
   11584     }
   11585 }
   11586 
   11587 static bool
   11588 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   11589 {
   11590   char *name;
   11591   asection *sect;
   11592   size_t len;
   11593 
   11594   /* Use note name as section name.  */
   11595   len = note->namesz;
   11596   name = (char *) bfd_alloc (abfd, len);
   11597   if (name == NULL)
   11598     return false;
   11599   memcpy (name, note->namedata, len);
   11600   name[len - 1] = '\0';
   11601 
   11602   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11603   if (sect == NULL)
   11604     return false;
   11605 
   11606   sect->size		= note->descsz;
   11607   sect->filepos		= note->descpos;
   11608   sect->alignment_power = 1;
   11609 
   11610   return true;
   11611 }
   11612 
   11613 /* Function: elfcore_write_note
   11614 
   11615    Inputs:
   11616      buffer to hold note, and current size of buffer
   11617      name of note
   11618      type of note
   11619      data for note
   11620      size of data for note
   11621 
   11622    Writes note to end of buffer.  ELF64 notes are written exactly as
   11623    for ELF32, despite the current (as of 2006) ELF gabi specifying
   11624    that they ought to have 8-byte namesz and descsz field, and have
   11625    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   11626 
   11627    Return:
   11628    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   11629 
   11630 char *
   11631 elfcore_write_note (bfd *abfd,
   11632 		    char *buf,
   11633 		    int *bufsiz,
   11634 		    const char *name,
   11635 		    int type,
   11636 		    const void *input,
   11637 		    int size)
   11638 {
   11639   Elf_External_Note *xnp;
   11640   size_t namesz;
   11641   size_t newspace;
   11642   char *dest;
   11643 
   11644   namesz = 0;
   11645   if (name != NULL)
   11646     namesz = strlen (name) + 1;
   11647 
   11648   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   11649 
   11650   buf = (char *) realloc (buf, *bufsiz + newspace);
   11651   if (buf == NULL)
   11652     return buf;
   11653   dest = buf + *bufsiz;
   11654   *bufsiz += newspace;
   11655   xnp = (Elf_External_Note *) dest;
   11656   H_PUT_32 (abfd, namesz, xnp->namesz);
   11657   H_PUT_32 (abfd, size, xnp->descsz);
   11658   H_PUT_32 (abfd, type, xnp->type);
   11659   dest = xnp->name;
   11660   if (name != NULL)
   11661     {
   11662       memcpy (dest, name, namesz);
   11663       dest += namesz;
   11664       while (namesz & 3)
   11665 	{
   11666 	  *dest++ = '\0';
   11667 	  ++namesz;
   11668 	}
   11669     }
   11670   memcpy (dest, input, size);
   11671   dest += size;
   11672   while (size & 3)
   11673     {
   11674       *dest++ = '\0';
   11675       ++size;
   11676     }
   11677   return buf;
   11678 }
   11679 
   11680 /* gcc-8 warns (*) on all the strncpy calls in this function about
   11681    possible string truncation.  The "truncation" is not a bug.  We
   11682    have an external representation of structs with fields that are not
   11683    necessarily NULL terminated and corresponding internal
   11684    representation fields that are one larger so that they can always
   11685    be NULL terminated.
   11686    gcc versions between 4.2 and 4.6 do not allow pragma control of
   11687    diagnostics inside functions, giving a hard error if you try to use
   11688    the finer control available with later versions.
   11689    gcc prior to 4.2 warns about diagnostic push and pop.
   11690    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   11691    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   11692    (*) Depending on your system header files!  */
   11693 #if GCC_VERSION >= 8000
   11694 # pragma GCC diagnostic push
   11695 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   11696 #endif
   11697 char *
   11698 elfcore_write_prpsinfo (bfd  *abfd,
   11699 			char *buf,
   11700 			int  *bufsiz,
   11701 			const char *fname,
   11702 			const char *psargs)
   11703 {
   11704   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11705 
   11706   if (bed->elf_backend_write_core_note != NULL)
   11707     {
   11708       char *ret;
   11709       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11710 						 NT_PRPSINFO, fname, psargs);
   11711       if (ret != NULL)
   11712 	return ret;
   11713     }
   11714 
   11715 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11716 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   11717   if (bed->s->elfclass == ELFCLASS32)
   11718     {
   11719 #  if defined (HAVE_PSINFO32_T)
   11720       psinfo32_t data;
   11721       int note_type = NT_PSINFO;
   11722 #  else
   11723       prpsinfo32_t data;
   11724       int note_type = NT_PRPSINFO;
   11725 #  endif
   11726 
   11727       memset (&data, 0, sizeof (data));
   11728       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11729       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11730       return elfcore_write_note (abfd, buf, bufsiz,
   11731 				 "CORE", note_type, &data, sizeof (data));
   11732     }
   11733   else
   11734 # endif
   11735     {
   11736 # if defined (HAVE_PSINFO_T)
   11737       psinfo_t data;
   11738       int note_type = NT_PSINFO;
   11739 # else
   11740       prpsinfo_t data;
   11741       int note_type = NT_PRPSINFO;
   11742 # endif
   11743 
   11744       memset (&data, 0, sizeof (data));
   11745       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   11746       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   11747       return elfcore_write_note (abfd, buf, bufsiz,
   11748 				 "CORE", note_type, &data, sizeof (data));
   11749     }
   11750 #endif	/* PSINFO_T or PRPSINFO_T */
   11751 
   11752   free (buf);
   11753   return NULL;
   11754 }
   11755 #if GCC_VERSION >= 8000
   11756 # pragma GCC diagnostic pop
   11757 #endif
   11758 
   11759 char *
   11760 elfcore_write_linux_prpsinfo32
   11761   (bfd *abfd, char *buf, int *bufsiz,
   11762    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11763 {
   11764   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   11765     {
   11766       struct elf_external_linux_prpsinfo32_ugid16 data;
   11767 
   11768       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   11769       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11770 				 &data, sizeof (data));
   11771     }
   11772   else
   11773     {
   11774       struct elf_external_linux_prpsinfo32_ugid32 data;
   11775 
   11776       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   11777       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   11778 				 &data, sizeof (data));
   11779     }
   11780 }
   11781 
   11782 char *
   11783 elfcore_write_linux_prpsinfo64
   11784   (bfd *abfd, char *buf, int *bufsiz,
   11785    const struct elf_internal_linux_prpsinfo *prpsinfo)
   11786 {
   11787   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   11788     {
   11789       struct elf_external_linux_prpsinfo64_ugid16 data;
   11790 
   11791       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   11792       return elfcore_write_note (abfd, buf, bufsiz,
   11793 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11794     }
   11795   else
   11796     {
   11797       struct elf_external_linux_prpsinfo64_ugid32 data;
   11798 
   11799       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   11800       return elfcore_write_note (abfd, buf, bufsiz,
   11801 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   11802     }
   11803 }
   11804 
   11805 char *
   11806 elfcore_write_prstatus (bfd *abfd,
   11807 			char *buf,
   11808 			int *bufsiz,
   11809 			long pid,
   11810 			int cursig,
   11811 			const void *gregs)
   11812 {
   11813   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11814 
   11815   if (bed->elf_backend_write_core_note != NULL)
   11816     {
   11817       char *ret;
   11818       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   11819 						 NT_PRSTATUS,
   11820 						 pid, cursig, gregs);
   11821       if (ret != NULL)
   11822 	return ret;
   11823     }
   11824 
   11825 #if defined (HAVE_PRSTATUS_T)
   11826 #if defined (HAVE_PRSTATUS32_T)
   11827   if (bed->s->elfclass == ELFCLASS32)
   11828     {
   11829       prstatus32_t prstat;
   11830 
   11831       memset (&prstat, 0, sizeof (prstat));
   11832       prstat.pr_pid = pid;
   11833       prstat.pr_cursig = cursig;
   11834       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11835       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11836 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11837     }
   11838   else
   11839 #endif
   11840     {
   11841       prstatus_t prstat;
   11842 
   11843       memset (&prstat, 0, sizeof (prstat));
   11844       prstat.pr_pid = pid;
   11845       prstat.pr_cursig = cursig;
   11846       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   11847       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   11848 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   11849     }
   11850 #endif /* HAVE_PRSTATUS_T */
   11851 
   11852   free (buf);
   11853   return NULL;
   11854 }
   11855 
   11856 #if defined (HAVE_LWPSTATUS_T)
   11857 char *
   11858 elfcore_write_lwpstatus (bfd *abfd,
   11859 			 char *buf,
   11860 			 int *bufsiz,
   11861 			 long pid,
   11862 			 int cursig,
   11863 			 const void *gregs)
   11864 {
   11865   lwpstatus_t lwpstat;
   11866   const char *note_name = "CORE";
   11867 
   11868   memset (&lwpstat, 0, sizeof (lwpstat));
   11869   lwpstat.pr_lwpid  = pid >> 16;
   11870   lwpstat.pr_cursig = cursig;
   11871 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   11872   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   11873 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11874 #if !defined(gregs)
   11875   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   11876 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   11877 #else
   11878   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   11879 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   11880 #endif
   11881 #endif
   11882   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   11883 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   11884 }
   11885 #endif /* HAVE_LWPSTATUS_T */
   11886 
   11887 #if defined (HAVE_PSTATUS_T)
   11888 char *
   11889 elfcore_write_pstatus (bfd *abfd,
   11890 		       char *buf,
   11891 		       int *bufsiz,
   11892 		       long pid,
   11893 		       int cursig ATTRIBUTE_UNUSED,
   11894 		       const void *gregs ATTRIBUTE_UNUSED)
   11895 {
   11896   const char *note_name = "CORE";
   11897 #if defined (HAVE_PSTATUS32_T)
   11898   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11899 
   11900   if (bed->s->elfclass == ELFCLASS32)
   11901     {
   11902       pstatus32_t pstat;
   11903 
   11904       memset (&pstat, 0, sizeof (pstat));
   11905       pstat.pr_pid = pid & 0xffff;
   11906       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11907 				NT_PSTATUS, &pstat, sizeof (pstat));
   11908       return buf;
   11909     }
   11910   else
   11911 #endif
   11912     {
   11913       pstatus_t pstat;
   11914 
   11915       memset (&pstat, 0, sizeof (pstat));
   11916       pstat.pr_pid = pid & 0xffff;
   11917       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   11918 				NT_PSTATUS, &pstat, sizeof (pstat));
   11919       return buf;
   11920     }
   11921 }
   11922 #endif /* HAVE_PSTATUS_T */
   11923 
   11924 char *
   11925 elfcore_write_prfpreg (bfd *abfd,
   11926 		       char *buf,
   11927 		       int *bufsiz,
   11928 		       const void *fpregs,
   11929 		       int size)
   11930 {
   11931   const char *note_name = "CORE";
   11932   return elfcore_write_note (abfd, buf, bufsiz,
   11933 			     note_name, NT_FPREGSET, fpregs, size);
   11934 }
   11935 
   11936 char *
   11937 elfcore_write_prxfpreg (bfd *abfd,
   11938 			char *buf,
   11939 			int *bufsiz,
   11940 			const void *xfpregs,
   11941 			int size)
   11942 {
   11943   char *note_name = "LINUX";
   11944   return elfcore_write_note (abfd, buf, bufsiz,
   11945 			     note_name, NT_PRXFPREG, xfpregs, size);
   11946 }
   11947 
   11948 char *
   11949 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   11950 			 const void *xfpregs, int size)
   11951 {
   11952   char *note_name;
   11953   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   11954     note_name = "FreeBSD";
   11955   else
   11956     note_name = "LINUX";
   11957   return elfcore_write_note (abfd, buf, bufsiz,
   11958 			     note_name, NT_X86_XSTATE, xfpregs, size);
   11959 }
   11960 
   11961 char *
   11962 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   11963 			    const void *regs, int size)
   11964 {
   11965   char *note_name = "FreeBSD";
   11966   return elfcore_write_note (abfd, buf, bufsiz,
   11967 			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
   11968 }
   11969 
   11970 char *
   11971 elfcore_write_ppc_vmx (bfd *abfd,
   11972 		       char *buf,
   11973 		       int *bufsiz,
   11974 		       const void *ppc_vmx,
   11975 		       int size)
   11976 {
   11977   char *note_name = "LINUX";
   11978   return elfcore_write_note (abfd, buf, bufsiz,
   11979 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   11980 }
   11981 
   11982 char *
   11983 elfcore_write_ppc_vsx (bfd *abfd,
   11984 		       char *buf,
   11985 		       int *bufsiz,
   11986 		       const void *ppc_vsx,
   11987 		       int size)
   11988 {
   11989   char *note_name = "LINUX";
   11990   return elfcore_write_note (abfd, buf, bufsiz,
   11991 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   11992 }
   11993 
   11994 char *
   11995 elfcore_write_ppc_tar (bfd *abfd,
   11996 		       char *buf,
   11997 		       int *bufsiz,
   11998 		       const void *ppc_tar,
   11999 		       int size)
   12000 {
   12001   char *note_name = "LINUX";
   12002   return elfcore_write_note (abfd, buf, bufsiz,
   12003 			     note_name, NT_PPC_TAR, ppc_tar, size);
   12004 }
   12005 
   12006 char *
   12007 elfcore_write_ppc_ppr (bfd *abfd,
   12008 		       char *buf,
   12009 		       int *bufsiz,
   12010 		       const void *ppc_ppr,
   12011 		       int size)
   12012 {
   12013   char *note_name = "LINUX";
   12014   return elfcore_write_note (abfd, buf, bufsiz,
   12015 			     note_name, NT_PPC_PPR, ppc_ppr, size);
   12016 }
   12017 
   12018 char *
   12019 elfcore_write_ppc_dscr (bfd *abfd,
   12020 			char *buf,
   12021 			int *bufsiz,
   12022 			const void *ppc_dscr,
   12023 			int size)
   12024 {
   12025   char *note_name = "LINUX";
   12026   return elfcore_write_note (abfd, buf, bufsiz,
   12027 			     note_name, NT_PPC_DSCR, ppc_dscr, size);
   12028 }
   12029 
   12030 char *
   12031 elfcore_write_ppc_ebb (bfd *abfd,
   12032 		       char *buf,
   12033 		       int *bufsiz,
   12034 		       const void *ppc_ebb,
   12035 		       int size)
   12036 {
   12037   char *note_name = "LINUX";
   12038   return elfcore_write_note (abfd, buf, bufsiz,
   12039 			     note_name, NT_PPC_EBB, ppc_ebb, size);
   12040 }
   12041 
   12042 char *
   12043 elfcore_write_ppc_pmu (bfd *abfd,
   12044 		       char *buf,
   12045 		       int *bufsiz,
   12046 		       const void *ppc_pmu,
   12047 		       int size)
   12048 {
   12049   char *note_name = "LINUX";
   12050   return elfcore_write_note (abfd, buf, bufsiz,
   12051 			     note_name, NT_PPC_PMU, ppc_pmu, size);
   12052 }
   12053 
   12054 char *
   12055 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12056 			   char *buf,
   12057 			   int *bufsiz,
   12058 			   const void *ppc_tm_cgpr,
   12059 			   int size)
   12060 {
   12061   char *note_name = "LINUX";
   12062   return elfcore_write_note (abfd, buf, bufsiz,
   12063 			     note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
   12064 }
   12065 
   12066 char *
   12067 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12068 			   char *buf,
   12069 			   int *bufsiz,
   12070 			   const void *ppc_tm_cfpr,
   12071 			   int size)
   12072 {
   12073   char *note_name = "LINUX";
   12074   return elfcore_write_note (abfd, buf, bufsiz,
   12075 			     note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
   12076 }
   12077 
   12078 char *
   12079 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12080 			   char *buf,
   12081 			   int *bufsiz,
   12082 			   const void *ppc_tm_cvmx,
   12083 			   int size)
   12084 {
   12085   char *note_name = "LINUX";
   12086   return elfcore_write_note (abfd, buf, bufsiz,
   12087 			     note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
   12088 }
   12089 
   12090 char *
   12091 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12092 			   char *buf,
   12093 			   int *bufsiz,
   12094 			   const void *ppc_tm_cvsx,
   12095 			   int size)
   12096 {
   12097   char *note_name = "LINUX";
   12098   return elfcore_write_note (abfd, buf, bufsiz,
   12099 			     note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
   12100 }
   12101 
   12102 char *
   12103 elfcore_write_ppc_tm_spr (bfd *abfd,
   12104 			  char *buf,
   12105 			  int *bufsiz,
   12106 			  const void *ppc_tm_spr,
   12107 			  int size)
   12108 {
   12109   char *note_name = "LINUX";
   12110   return elfcore_write_note (abfd, buf, bufsiz,
   12111 			     note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
   12112 }
   12113 
   12114 char *
   12115 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12116 			   char *buf,
   12117 			   int *bufsiz,
   12118 			   const void *ppc_tm_ctar,
   12119 			   int size)
   12120 {
   12121   char *note_name = "LINUX";
   12122   return elfcore_write_note (abfd, buf, bufsiz,
   12123 			     note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
   12124 }
   12125 
   12126 char *
   12127 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12128 			   char *buf,
   12129 			   int *bufsiz,
   12130 			   const void *ppc_tm_cppr,
   12131 			   int size)
   12132 {
   12133   char *note_name = "LINUX";
   12134   return elfcore_write_note (abfd, buf, bufsiz,
   12135 			     note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
   12136 }
   12137 
   12138 char *
   12139 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12140 			    char *buf,
   12141 			    int *bufsiz,
   12142 			    const void *ppc_tm_cdscr,
   12143 			    int size)
   12144 {
   12145   char *note_name = "LINUX";
   12146   return elfcore_write_note (abfd, buf, bufsiz,
   12147 			     note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
   12148 }
   12149 
   12150 static char *
   12151 elfcore_write_s390_high_gprs (bfd *abfd,
   12152 			      char *buf,
   12153 			      int *bufsiz,
   12154 			      const void *s390_high_gprs,
   12155 			      int size)
   12156 {
   12157   char *note_name = "LINUX";
   12158   return elfcore_write_note (abfd, buf, bufsiz,
   12159 			     note_name, NT_S390_HIGH_GPRS,
   12160 			     s390_high_gprs, size);
   12161 }
   12162 
   12163 char *
   12164 elfcore_write_s390_timer (bfd *abfd,
   12165 			  char *buf,
   12166 			  int *bufsiz,
   12167 			  const void *s390_timer,
   12168 			  int size)
   12169 {
   12170   char *note_name = "LINUX";
   12171   return elfcore_write_note (abfd, buf, bufsiz,
   12172 			     note_name, NT_S390_TIMER, s390_timer, size);
   12173 }
   12174 
   12175 char *
   12176 elfcore_write_s390_todcmp (bfd *abfd,
   12177 			   char *buf,
   12178 			   int *bufsiz,
   12179 			   const void *s390_todcmp,
   12180 			   int size)
   12181 {
   12182   char *note_name = "LINUX";
   12183   return elfcore_write_note (abfd, buf, bufsiz,
   12184 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   12185 }
   12186 
   12187 char *
   12188 elfcore_write_s390_todpreg (bfd *abfd,
   12189 			    char *buf,
   12190 			    int *bufsiz,
   12191 			    const void *s390_todpreg,
   12192 			    int size)
   12193 {
   12194   char *note_name = "LINUX";
   12195   return elfcore_write_note (abfd, buf, bufsiz,
   12196 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   12197 }
   12198 
   12199 char *
   12200 elfcore_write_s390_ctrs (bfd *abfd,
   12201 			 char *buf,
   12202 			 int *bufsiz,
   12203 			 const void *s390_ctrs,
   12204 			 int size)
   12205 {
   12206   char *note_name = "LINUX";
   12207   return elfcore_write_note (abfd, buf, bufsiz,
   12208 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   12209 }
   12210 
   12211 char *
   12212 elfcore_write_s390_prefix (bfd *abfd,
   12213 			   char *buf,
   12214 			   int *bufsiz,
   12215 			   const void *s390_prefix,
   12216 			   int size)
   12217 {
   12218   char *note_name = "LINUX";
   12219   return elfcore_write_note (abfd, buf, bufsiz,
   12220 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   12221 }
   12222 
   12223 char *
   12224 elfcore_write_s390_last_break (bfd *abfd,
   12225 			       char *buf,
   12226 			       int *bufsiz,
   12227 			       const void *s390_last_break,
   12228 			       int size)
   12229 {
   12230   char *note_name = "LINUX";
   12231   return elfcore_write_note (abfd, buf, bufsiz,
   12232 			     note_name, NT_S390_LAST_BREAK,
   12233 			     s390_last_break, size);
   12234 }
   12235 
   12236 char *
   12237 elfcore_write_s390_system_call (bfd *abfd,
   12238 				char *buf,
   12239 				int *bufsiz,
   12240 				const void *s390_system_call,
   12241 				int size)
   12242 {
   12243   char *note_name = "LINUX";
   12244   return elfcore_write_note (abfd, buf, bufsiz,
   12245 			     note_name, NT_S390_SYSTEM_CALL,
   12246 			     s390_system_call, size);
   12247 }
   12248 
   12249 char *
   12250 elfcore_write_s390_tdb (bfd *abfd,
   12251 			char *buf,
   12252 			int *bufsiz,
   12253 			const void *s390_tdb,
   12254 			int size)
   12255 {
   12256   char *note_name = "LINUX";
   12257   return elfcore_write_note (abfd, buf, bufsiz,
   12258 			     note_name, NT_S390_TDB, s390_tdb, size);
   12259 }
   12260 
   12261 char *
   12262 elfcore_write_s390_vxrs_low (bfd *abfd,
   12263 			     char *buf,
   12264 			     int *bufsiz,
   12265 			     const void *s390_vxrs_low,
   12266 			     int size)
   12267 {
   12268   char *note_name = "LINUX";
   12269   return elfcore_write_note (abfd, buf, bufsiz,
   12270 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   12271 }
   12272 
   12273 char *
   12274 elfcore_write_s390_vxrs_high (bfd *abfd,
   12275 			     char *buf,
   12276 			     int *bufsiz,
   12277 			     const void *s390_vxrs_high,
   12278 			     int size)
   12279 {
   12280   char *note_name = "LINUX";
   12281   return elfcore_write_note (abfd, buf, bufsiz,
   12282 			     note_name, NT_S390_VXRS_HIGH,
   12283 			     s390_vxrs_high, size);
   12284 }
   12285 
   12286 char *
   12287 elfcore_write_s390_gs_cb (bfd *abfd,
   12288 			  char *buf,
   12289 			  int *bufsiz,
   12290 			  const void *s390_gs_cb,
   12291 			  int size)
   12292 {
   12293   char *note_name = "LINUX";
   12294   return elfcore_write_note (abfd, buf, bufsiz,
   12295 			     note_name, NT_S390_GS_CB,
   12296 			     s390_gs_cb, size);
   12297 }
   12298 
   12299 char *
   12300 elfcore_write_s390_gs_bc (bfd *abfd,
   12301 			  char *buf,
   12302 			  int *bufsiz,
   12303 			  const void *s390_gs_bc,
   12304 			  int size)
   12305 {
   12306   char *note_name = "LINUX";
   12307   return elfcore_write_note (abfd, buf, bufsiz,
   12308 			     note_name, NT_S390_GS_BC,
   12309 			     s390_gs_bc, size);
   12310 }
   12311 
   12312 char *
   12313 elfcore_write_arm_vfp (bfd *abfd,
   12314 		       char *buf,
   12315 		       int *bufsiz,
   12316 		       const void *arm_vfp,
   12317 		       int size)
   12318 {
   12319   char *note_name = "LINUX";
   12320   return elfcore_write_note (abfd, buf, bufsiz,
   12321 			     note_name, NT_ARM_VFP, arm_vfp, size);
   12322 }
   12323 
   12324 char *
   12325 elfcore_write_aarch_tls (bfd *abfd,
   12326 		       char *buf,
   12327 		       int *bufsiz,
   12328 		       const void *aarch_tls,
   12329 		       int size)
   12330 {
   12331   char *note_name = "LINUX";
   12332   return elfcore_write_note (abfd, buf, bufsiz,
   12333 			     note_name, NT_ARM_TLS, aarch_tls, size);
   12334 }
   12335 
   12336 char *
   12337 elfcore_write_aarch_hw_break (bfd *abfd,
   12338 			    char *buf,
   12339 			    int *bufsiz,
   12340 			    const void *aarch_hw_break,
   12341 			    int size)
   12342 {
   12343   char *note_name = "LINUX";
   12344   return elfcore_write_note (abfd, buf, bufsiz,
   12345 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   12346 }
   12347 
   12348 char *
   12349 elfcore_write_aarch_hw_watch (bfd *abfd,
   12350 			    char *buf,
   12351 			    int *bufsiz,
   12352 			    const void *aarch_hw_watch,
   12353 			    int size)
   12354 {
   12355   char *note_name = "LINUX";
   12356   return elfcore_write_note (abfd, buf, bufsiz,
   12357 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   12358 }
   12359 
   12360 char *
   12361 elfcore_write_aarch_sve (bfd *abfd,
   12362 			 char *buf,
   12363 			 int *bufsiz,
   12364 			 const void *aarch_sve,
   12365 			 int size)
   12366 {
   12367   char *note_name = "LINUX";
   12368   return elfcore_write_note (abfd, buf, bufsiz,
   12369 			     note_name, NT_ARM_SVE, aarch_sve, size);
   12370 }
   12371 
   12372 char *
   12373 elfcore_write_aarch_pauth (bfd *abfd,
   12374 			   char *buf,
   12375 			   int *bufsiz,
   12376 			   const void *aarch_pauth,
   12377 			   int size)
   12378 {
   12379   char *note_name = "LINUX";
   12380   return elfcore_write_note (abfd, buf, bufsiz,
   12381 			     note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
   12382 }
   12383 
   12384 char *
   12385 elfcore_write_aarch_mte (bfd *abfd,
   12386 				      char *buf,
   12387 				      int *bufsiz,
   12388 				      const void *aarch_mte,
   12389 				      int size)
   12390 {
   12391   char *note_name = "LINUX";
   12392   return elfcore_write_note (abfd, buf, bufsiz,
   12393 			     note_name, NT_ARM_TAGGED_ADDR_CTRL,
   12394 			     aarch_mte,
   12395 			     size);
   12396 }
   12397 
   12398 char *
   12399 elfcore_write_arc_v2 (bfd *abfd,
   12400 		      char *buf,
   12401 		      int *bufsiz,
   12402 		      const void *arc_v2,
   12403 		      int size)
   12404 {
   12405   char *note_name = "LINUX";
   12406   return elfcore_write_note (abfd, buf, bufsiz,
   12407 			     note_name, NT_ARC_V2, arc_v2, size);
   12408 }
   12409 
   12410 char *
   12411 elfcore_write_loongarch_cpucfg (bfd *abfd,
   12412 				char *buf,
   12413 				int *bufsiz,
   12414 				const void *loongarch_cpucfg,
   12415 				int size)
   12416 {
   12417   char *note_name = "LINUX";
   12418   return elfcore_write_note (abfd, buf, bufsiz,
   12419 			     note_name, NT_LARCH_CPUCFG,
   12420 			     loongarch_cpucfg, size);
   12421 }
   12422 
   12423 char *
   12424 elfcore_write_loongarch_lbt (bfd *abfd,
   12425 			     char *buf,
   12426 			     int *bufsiz,
   12427 			     const void *loongarch_lbt,
   12428 			     int size)
   12429 {
   12430   char *note_name = "LINUX";
   12431   return elfcore_write_note (abfd, buf, bufsiz,
   12432 			     note_name, NT_LARCH_LBT, loongarch_lbt, size);
   12433 }
   12434 
   12435 char *
   12436 elfcore_write_loongarch_lsx (bfd *abfd,
   12437 			     char *buf,
   12438 			     int *bufsiz,
   12439 			     const void *loongarch_lsx,
   12440 			     int size)
   12441 {
   12442   char *note_name = "LINUX";
   12443   return elfcore_write_note (abfd, buf, bufsiz,
   12444 			     note_name, NT_LARCH_LSX, loongarch_lsx, size);
   12445 }
   12446 
   12447 char *
   12448 elfcore_write_loongarch_lasx (bfd *abfd,
   12449 			      char *buf,
   12450 			      int *bufsiz,
   12451 			      const void *loongarch_lasx,
   12452 			      int size)
   12453 {
   12454   char *note_name = "LINUX";
   12455   return elfcore_write_note (abfd, buf, bufsiz,
   12456 			     note_name, NT_LARCH_LASX, loongarch_lasx, size);
   12457 }
   12458 
   12459 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   12460    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12461    written into.  Return a pointer to the new start of the note buffer, to
   12462    replace BUF which may no longer be valid.  */
   12463 
   12464 char *
   12465 elfcore_write_riscv_csr (bfd *abfd,
   12466                          char *buf,
   12467                          int *bufsiz,
   12468                          const void *csrs,
   12469                          int size)
   12470 {
   12471   const char *note_name = "GDB";
   12472   return elfcore_write_note (abfd, buf, bufsiz,
   12473 			     note_name, NT_RISCV_CSR, csrs, size);
   12474 }
   12475 
   12476 /* Write the target description (a string) pointed to by TDESC, length
   12477    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   12478    note is being written into.  Return a pointer to the new start of the
   12479    note buffer, to replace BUF which may no longer be valid.  */
   12480 
   12481 char *
   12482 elfcore_write_gdb_tdesc (bfd *abfd,
   12483 			 char *buf,
   12484 			 int *bufsiz,
   12485 			 const void *tdesc,
   12486 			 int size)
   12487 {
   12488   const char *note_name = "GDB";
   12489   return elfcore_write_note (abfd, buf, bufsiz,
   12490                              note_name, NT_GDB_TDESC, tdesc, size);
   12491 }
   12492 
   12493 char *
   12494 elfcore_write_register_note (bfd *abfd,
   12495 			     char *buf,
   12496 			     int *bufsiz,
   12497 			     const char *section,
   12498 			     const void *data,
   12499 			     int size)
   12500 {
   12501   if (strcmp (section, ".reg2") == 0)
   12502     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   12503   if (strcmp (section, ".reg-xfp") == 0)
   12504     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   12505   if (strcmp (section, ".reg-xstate") == 0)
   12506     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   12507   if (strcmp (section, ".reg-x86-segbases") == 0)
   12508     return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
   12509   if (strcmp (section, ".reg-ppc-vmx") == 0)
   12510     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   12511   if (strcmp (section, ".reg-ppc-vsx") == 0)
   12512     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   12513   if (strcmp (section, ".reg-ppc-tar") == 0)
   12514     return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
   12515   if (strcmp (section, ".reg-ppc-ppr") == 0)
   12516     return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
   12517   if (strcmp (section, ".reg-ppc-dscr") == 0)
   12518     return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
   12519   if (strcmp (section, ".reg-ppc-ebb") == 0)
   12520     return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
   12521   if (strcmp (section, ".reg-ppc-pmu") == 0)
   12522     return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
   12523   if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
   12524     return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
   12525   if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
   12526     return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
   12527   if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
   12528     return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
   12529   if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
   12530     return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
   12531   if (strcmp (section, ".reg-ppc-tm-spr") == 0)
   12532     return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
   12533   if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
   12534     return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
   12535   if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
   12536     return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
   12537   if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
   12538     return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
   12539   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   12540     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   12541   if (strcmp (section, ".reg-s390-timer") == 0)
   12542     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   12543   if (strcmp (section, ".reg-s390-todcmp") == 0)
   12544     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   12545   if (strcmp (section, ".reg-s390-todpreg") == 0)
   12546     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   12547   if (strcmp (section, ".reg-s390-ctrs") == 0)
   12548     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   12549   if (strcmp (section, ".reg-s390-prefix") == 0)
   12550     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   12551   if (strcmp (section, ".reg-s390-last-break") == 0)
   12552     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   12553   if (strcmp (section, ".reg-s390-system-call") == 0)
   12554     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   12555   if (strcmp (section, ".reg-s390-tdb") == 0)
   12556     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   12557   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   12558     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   12559   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   12560     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   12561   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   12562     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   12563   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   12564     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   12565   if (strcmp (section, ".reg-arm-vfp") == 0)
   12566     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   12567   if (strcmp (section, ".reg-aarch-tls") == 0)
   12568     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   12569   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   12570     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   12571   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   12572     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   12573   if (strcmp (section, ".reg-aarch-sve") == 0)
   12574     return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
   12575   if (strcmp (section, ".reg-aarch-pauth") == 0)
   12576     return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
   12577   if (strcmp (section, ".reg-aarch-mte") == 0)
   12578     return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
   12579   if (strcmp (section, ".reg-arc-v2") == 0)
   12580     return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
   12581   if (strcmp (section, ".gdb-tdesc") == 0)
   12582     return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
   12583   if (strcmp (section, ".reg-riscv-csr") == 0)
   12584     return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
   12585   if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
   12586     return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
   12587   if (strcmp (section, ".reg-loongarch-lbt") == 0)
   12588     return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
   12589   if (strcmp (section, ".reg-loongarch-lsx") == 0)
   12590     return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
   12591   if (strcmp (section, ".reg-loongarch-lasx") == 0)
   12592     return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
   12593   return NULL;
   12594 }
   12595 
   12596 char *
   12597 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   12598 			 const void *buf, int bufsiz)
   12599 {
   12600   return elfcore_write_note (obfd, note_data, note_size,
   12601 			     "CORE", NT_FILE, buf, bufsiz);
   12602 }
   12603 
   12604 static bool
   12605 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   12606 		 size_t align)
   12607 {
   12608   char *p;
   12609 
   12610   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   12611      gABI specifies that PT_NOTE alignment should be aligned to 4
   12612      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   12613      align is less than 4, we use 4 byte alignment.   */
   12614   if (align < 4)
   12615     align = 4;
   12616   if (align != 4 && align != 8)
   12617     return false;
   12618 
   12619   p = buf;
   12620   while (p < buf + size)
   12621     {
   12622       Elf_External_Note *xnp = (Elf_External_Note *) p;
   12623       Elf_Internal_Note in;
   12624 
   12625       if (offsetof (Elf_External_Note, name) > buf - p + size)
   12626 	return false;
   12627 
   12628       in.type = H_GET_32 (abfd, xnp->type);
   12629 
   12630       in.namesz = H_GET_32 (abfd, xnp->namesz);
   12631       in.namedata = xnp->name;
   12632       if (in.namesz > buf - in.namedata + size)
   12633 	return false;
   12634 
   12635       in.descsz = H_GET_32 (abfd, xnp->descsz);
   12636       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   12637       in.descpos = offset + (in.descdata - buf);
   12638       if (in.descsz != 0
   12639 	  && (in.descdata >= buf + size
   12640 	      || in.descsz > buf - in.descdata + size))
   12641 	return false;
   12642 
   12643       switch (bfd_get_format (abfd))
   12644 	{
   12645 	default:
   12646 	  return true;
   12647 
   12648 	case bfd_core:
   12649 	  {
   12650 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   12651 	    struct
   12652 	    {
   12653 	      const char * string;
   12654 	      size_t len;
   12655 	      bool (*func) (bfd *, Elf_Internal_Note *);
   12656 	    }
   12657 	    grokers[] =
   12658 	    {
   12659 	      GROKER_ELEMENT ("", elfcore_grok_note),
   12660 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   12661 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   12662 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   12663 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   12664 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   12665 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   12666 	      GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
   12667 	    };
   12668 #undef GROKER_ELEMENT
   12669 	    int i;
   12670 
   12671 	    for (i = ARRAY_SIZE (grokers); i--;)
   12672 	      {
   12673 		if (in.namesz >= grokers[i].len
   12674 		    && strncmp (in.namedata, grokers[i].string,
   12675 				grokers[i].len) == 0)
   12676 		  {
   12677 		    if (! grokers[i].func (abfd, & in))
   12678 		      return false;
   12679 		    break;
   12680 		  }
   12681 	      }
   12682 	    break;
   12683 	  }
   12684 
   12685 	case bfd_object:
   12686 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   12687 	    {
   12688 	      if (! elfobj_grok_gnu_note (abfd, &in))
   12689 		return false;
   12690 	    }
   12691 	  else if (in.namesz == sizeof "stapsdt"
   12692 		   && strcmp (in.namedata, "stapsdt") == 0)
   12693 	    {
   12694 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   12695 		return false;
   12696 	    }
   12697 	  break;
   12698 	}
   12699 
   12700       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   12701     }
   12702 
   12703   return true;
   12704 }
   12705 
   12706 bool
   12707 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   12708 		size_t align)
   12709 {
   12710   char *buf;
   12711 
   12712   if (size == 0 || (size + 1) == 0)
   12713     return true;
   12714 
   12715   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   12716     return false;
   12717 
   12718   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   12719   if (buf == NULL)
   12720     return false;
   12721 
   12722   /* PR 17512: file: ec08f814
   12723      0-termintate the buffer so that string searches will not overflow.  */
   12724   buf[size] = 0;
   12725 
   12726   if (!elf_parse_notes (abfd, buf, size, offset, align))
   12727     {
   12728       free (buf);
   12729       return false;
   12730     }
   12731 
   12732   free (buf);
   12733   return true;
   12734 }
   12735 
   12736 /* Providing external access to the ELF program header table.  */
   12738 
   12739 /* Return an upper bound on the number of bytes required to store a
   12740    copy of ABFD's program header table entries.  Return -1 if an error
   12741    occurs; bfd_get_error will return an appropriate code.  */
   12742 
   12743 long
   12744 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   12745 {
   12746   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   12747     {
   12748       bfd_set_error (bfd_error_wrong_format);
   12749       return -1;
   12750     }
   12751 
   12752   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   12753 }
   12754 
   12755 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   12756    will be stored as an array of Elf_Internal_Phdr structures, as
   12757    defined in include/elf/internal.h.  To find out how large the
   12758    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   12759 
   12760    Return the number of program header table entries read, or -1 if an
   12761    error occurs; bfd_get_error will return an appropriate code.  */
   12762 
   12763 int
   12764 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   12765 {
   12766   int num_phdrs;
   12767 
   12768   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   12769     {
   12770       bfd_set_error (bfd_error_wrong_format);
   12771       return -1;
   12772     }
   12773 
   12774   num_phdrs = elf_elfheader (abfd)->e_phnum;
   12775   if (num_phdrs != 0)
   12776     memcpy (phdrs, elf_tdata (abfd)->phdr,
   12777 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   12778 
   12779   return num_phdrs;
   12780 }
   12781 
   12782 enum elf_reloc_type_class
   12783 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12784 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   12785 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   12786 {
   12787   return reloc_class_normal;
   12788 }
   12789 
   12790 /* For RELA architectures, return the relocation value for a
   12791    relocation against a local symbol.  */
   12792 
   12793 bfd_vma
   12794 _bfd_elf_rela_local_sym (bfd *abfd,
   12795 			 Elf_Internal_Sym *sym,
   12796 			 asection **psec,
   12797 			 Elf_Internal_Rela *rel)
   12798 {
   12799   asection *sec = *psec;
   12800   bfd_vma relocation;
   12801 
   12802   relocation = (sec->output_section->vma
   12803 		+ sec->output_offset
   12804 		+ sym->st_value);
   12805   if ((sec->flags & SEC_MERGE)
   12806       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   12807       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   12808     {
   12809       rel->r_addend =
   12810 	_bfd_merged_section_offset (abfd, psec,
   12811 				    elf_section_data (sec)->sec_info,
   12812 				    sym->st_value + rel->r_addend);
   12813       if (sec != *psec)
   12814 	{
   12815 	  /* If we have changed the section, and our original section is
   12816 	     marked with SEC_EXCLUDE, it means that the original
   12817 	     SEC_MERGE section has been completely subsumed in some
   12818 	     other SEC_MERGE section.  In this case, we need to leave
   12819 	     some info around for --emit-relocs.  */
   12820 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   12821 	    sec->kept_section = *psec;
   12822 	  sec = *psec;
   12823 	}
   12824       rel->r_addend -= relocation;
   12825       rel->r_addend += sec->output_section->vma + sec->output_offset;
   12826     }
   12827   return relocation;
   12828 }
   12829 
   12830 bfd_vma
   12831 _bfd_elf_rel_local_sym (bfd *abfd,
   12832 			Elf_Internal_Sym *sym,
   12833 			asection **psec,
   12834 			bfd_vma addend)
   12835 {
   12836   asection *sec = *psec;
   12837 
   12838   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   12839     return sym->st_value + addend;
   12840 
   12841   return _bfd_merged_section_offset (abfd, psec,
   12842 				     elf_section_data (sec)->sec_info,
   12843 				     sym->st_value + addend);
   12844 }
   12845 
   12846 /* Adjust an address within a section.  Given OFFSET within SEC, return
   12847    the new offset within the section, based upon changes made to the
   12848    section.  Returns -1 if the offset is now invalid.
   12849    The offset (in abnd out) is in target sized bytes, however big a
   12850    byte may be.  */
   12851 
   12852 bfd_vma
   12853 _bfd_elf_section_offset (bfd *abfd,
   12854 			 struct bfd_link_info *info,
   12855 			 asection *sec,
   12856 			 bfd_vma offset)
   12857 {
   12858   switch (sec->sec_info_type)
   12859     {
   12860     case SEC_INFO_TYPE_STABS:
   12861       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   12862 				       offset);
   12863     case SEC_INFO_TYPE_EH_FRAME:
   12864       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   12865 
   12866     default:
   12867       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   12868 	{
   12869 	  /* Reverse the offset.  */
   12870 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12871 	  bfd_size_type address_size = bed->s->arch_size / 8;
   12872 
   12873 	  /* address_size and sec->size are in octets.  Convert
   12874 	     to bytes before subtracting the original offset.  */
   12875 	  offset = ((sec->size - address_size)
   12876 		    / bfd_octets_per_byte (abfd, sec) - offset);
   12877 	}
   12878       return offset;
   12879     }
   12880 }
   12881 
   12882 /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   12884    reconstruct an ELF file by reading the segments out of remote memory
   12885    based on the ELF file header at EHDR_VMA and the ELF program headers it
   12886    points to.  If not null, *LOADBASEP is filled in with the difference
   12887    between the VMAs from which the segments were read, and the VMAs the
   12888    file headers (and hence BFD's idea of each section's VMA) put them at.
   12889 
   12890    The function TARGET_READ_MEMORY is called to copy LEN bytes from the
   12891    remote memory at target address VMA into the local buffer at MYADDR; it
   12892    should return zero on success or an `errno' code on failure.  TEMPL must
   12893    be a BFD for an ELF target with the word size and byte order found in
   12894    the remote memory.  */
   12895 
   12896 bfd *
   12897 bfd_elf_bfd_from_remote_memory
   12898   (bfd *templ,
   12899    bfd_vma ehdr_vma,
   12900    bfd_size_type size,
   12901    bfd_vma *loadbasep,
   12902    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   12903 {
   12904   return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
   12905     (templ, ehdr_vma, size, loadbasep, target_read_memory);
   12906 }
   12907 
   12908 long
   12910 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   12911 			       long symcount ATTRIBUTE_UNUSED,
   12912 			       asymbol **syms ATTRIBUTE_UNUSED,
   12913 			       long dynsymcount,
   12914 			       asymbol **dynsyms,
   12915 			       asymbol **ret)
   12916 {
   12917   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12918   asection *relplt;
   12919   asymbol *s;
   12920   const char *relplt_name;
   12921   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   12922   arelent *p;
   12923   long count, i, n;
   12924   size_t size;
   12925   Elf_Internal_Shdr *hdr;
   12926   char *names;
   12927   asection *plt;
   12928 
   12929   *ret = NULL;
   12930 
   12931   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   12932     return 0;
   12933 
   12934   if (dynsymcount <= 0)
   12935     return 0;
   12936 
   12937   if (!bed->plt_sym_val)
   12938     return 0;
   12939 
   12940   relplt_name = bed->relplt_name;
   12941   if (relplt_name == NULL)
   12942     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   12943   relplt = bfd_get_section_by_name (abfd, relplt_name);
   12944   if (relplt == NULL)
   12945     return 0;
   12946 
   12947   hdr = &elf_section_data (relplt)->this_hdr;
   12948   if (hdr->sh_link != elf_dynsymtab (abfd)
   12949       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   12950     return 0;
   12951 
   12952   plt = bfd_get_section_by_name (abfd, ".plt");
   12953   if (plt == NULL)
   12954     return 0;
   12955 
   12956   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   12957   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   12958     return -1;
   12959 
   12960   count = relplt->size / hdr->sh_entsize;
   12961   size = count * sizeof (asymbol);
   12962   p = relplt->relocation;
   12963   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12964     {
   12965       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   12966       if (p->addend != 0)
   12967 	{
   12968 #ifdef BFD64
   12969 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   12970 #else
   12971 	  size += sizeof ("+0x") - 1 + 8;
   12972 #endif
   12973 	}
   12974     }
   12975 
   12976   s = *ret = (asymbol *) bfd_malloc (size);
   12977   if (s == NULL)
   12978     return -1;
   12979 
   12980   names = (char *) (s + count);
   12981   p = relplt->relocation;
   12982   n = 0;
   12983   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   12984     {
   12985       size_t len;
   12986       bfd_vma addr;
   12987 
   12988       addr = bed->plt_sym_val (i, plt, p);
   12989       if (addr == (bfd_vma) -1)
   12990 	continue;
   12991 
   12992       *s = **p->sym_ptr_ptr;
   12993       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   12994 	 we are defining a symbol, ensure one of them is set.  */
   12995       if ((s->flags & BSF_LOCAL) == 0)
   12996 	s->flags |= BSF_GLOBAL;
   12997       s->flags |= BSF_SYNTHETIC;
   12998       s->section = plt;
   12999       s->value = addr - plt->vma;
   13000       s->name = names;
   13001       s->udata.p = NULL;
   13002       len = strlen ((*p->sym_ptr_ptr)->name);
   13003       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   13004       names += len;
   13005       if (p->addend != 0)
   13006 	{
   13007 	  char buf[30], *a;
   13008 
   13009 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   13010 	  names += sizeof ("+0x") - 1;
   13011 	  bfd_sprintf_vma (abfd, buf, p->addend);
   13012 	  for (a = buf; *a == '0'; ++a)
   13013 	    ;
   13014 	  len = strlen (a);
   13015 	  memcpy (names, a, len);
   13016 	  names += len;
   13017 	}
   13018       memcpy (names, "@plt", sizeof ("@plt"));
   13019       names += sizeof ("@plt");
   13020       ++s, ++n;
   13021     }
   13022 
   13023   return n;
   13024 }
   13025 
   13026 /* It is only used by x86-64 so far.
   13027    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   13028    but current usage would allow all of _bfd_std_section to be zero.  */
   13029 static const asymbol lcomm_sym
   13030   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   13031 asection _bfd_elf_large_com_section
   13032   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   13033 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   13034 
   13035 bool
   13036 _bfd_elf_final_write_processing (bfd *abfd)
   13037 {
   13038   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   13039 
   13040   i_ehdrp = elf_elfheader (abfd);
   13041 
   13042   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13043     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   13044 
   13045   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13046      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13047      or STB_GNU_UNIQUE binding.  */
   13048   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13049     {
   13050       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13051 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13052       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13053 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13054 	{
   13055 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13056 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13057 				  "and FreeBSD targets"));
   13058 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13059 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13060 				  "only by GNU and FreeBSD targets"));
   13061 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13062 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13063 				  "only by GNU and FreeBSD targets"));
   13064 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13065 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13066 				  "only by GNU and FreeBSD targets"));
   13067 	  bfd_set_error (bfd_error_sorry);
   13068 	  return false;
   13069 	}
   13070     }
   13071   return true;
   13072 }
   13073 
   13074 
   13075 /* Return TRUE for ELF symbol types that represent functions.
   13076    This is the default version of this function, which is sufficient for
   13077    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13078 
   13079 bool
   13080 _bfd_elf_is_function_type (unsigned int type)
   13081 {
   13082   return (type == STT_FUNC
   13083 	  || type == STT_GNU_IFUNC);
   13084 }
   13085 
   13086 /* If the ELF symbol SYM might be a function in SEC, return the
   13087    function size and set *CODE_OFF to the function's entry point,
   13088    otherwise return zero.  */
   13089 
   13090 bfd_size_type
   13091 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13092 			     bfd_vma *code_off)
   13093 {
   13094   bfd_size_type size;
   13095   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13096 
   13097   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13098 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13099       || sym->section != sec)
   13100     return 0;
   13101 
   13102   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13103 
   13104   /* In theory we should check that the symbol's type satisfies
   13105      _bfd_elf_is_function_type(), but there are some function-like
   13106      symbols which would fail this test.  (eg _start).  Instead
   13107      we check for hidden, local, notype symbols with zero size.
   13108      This type of symbol is generated by the annobin plugin for gcc
   13109      and clang, and should not be considered to be a function symbol.  */
   13110   if (size == 0
   13111       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13112       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13113       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13114     return 0;
   13115 
   13116   *code_off = sym->value;
   13117   /* Do not return 0 for the function's size.  */
   13118   return size ? size : 1;
   13119 }
   13120 
   13121 /* Set to non-zero to enable some debug messages.  */
   13122 #define DEBUG_SECONDARY_RELOCS	 0
   13123 
   13124 /* An internal-to-the-bfd-library only section type
   13125    used to indicate a cached secondary reloc section.  */
   13126 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13127 
   13128 /* Create a BFD section to hold a secondary reloc section.  */
   13129 
   13130 bool
   13131 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13132 				       Elf_Internal_Shdr *hdr,
   13133 				       const char * name,
   13134 				       unsigned int shindex)
   13135 {
   13136   /* We only support RELA secondary relocs.  */
   13137   if (hdr->sh_type != SHT_RELA)
   13138     return false;
   13139 
   13140 #if DEBUG_SECONDARY_RELOCS
   13141   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13142 #endif
   13143   hdr->sh_type = SHT_SECONDARY_RELOC;
   13144   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13145 }
   13146 
   13147 /* Read in any secondary relocs associated with SEC.  */
   13148 
   13149 bool
   13150 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13151 					asection *  sec,
   13152 					asymbol **  symbols,
   13153 					bool dynamic)
   13154 {
   13155   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13156   asection * relsec;
   13157   bool result = true;
   13158   bfd_vma (*r_sym) (bfd_vma);
   13159 
   13160 #if BFD_DEFAULT_TARGET_SIZE > 32
   13161   if (bfd_arch_bits_per_address (abfd) != 32)
   13162     r_sym = elf64_r_sym;
   13163   else
   13164 #endif
   13165     r_sym = elf32_r_sym;
   13166 
   13167   if (!elf_section_data (sec)->has_secondary_relocs)
   13168     return true;
   13169 
   13170   /* Discover if there are any secondary reloc sections
   13171      associated with SEC.  */
   13172   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13173     {
   13174       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13175 
   13176       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13177 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13178 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13179 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13180 	{
   13181 	  bfd_byte * native_relocs;
   13182 	  bfd_byte * native_reloc;
   13183 	  arelent * internal_relocs;
   13184 	  arelent * internal_reloc;
   13185 	  unsigned int i;
   13186 	  unsigned int entsize;
   13187 	  unsigned int symcount;
   13188 	  unsigned int reloc_count;
   13189 	  size_t amt;
   13190 
   13191 	  if (ebd->elf_info_to_howto == NULL)
   13192 	    return false;
   13193 
   13194 #if DEBUG_SECONDARY_RELOCS
   13195 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13196 		   sec->name, relsec->name);
   13197 #endif
   13198 	  entsize = hdr->sh_entsize;
   13199 
   13200 	  native_relocs = bfd_malloc (hdr->sh_size);
   13201 	  if (native_relocs == NULL)
   13202 	    {
   13203 	      result = false;
   13204 	      continue;
   13205 	    }
   13206 
   13207 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13208 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13209 	    {
   13210 	      free (native_relocs);
   13211 	      bfd_set_error (bfd_error_file_too_big);
   13212 	      result = false;
   13213 	      continue;
   13214 	    }
   13215 
   13216 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   13217 	  if (internal_relocs == NULL)
   13218 	    {
   13219 	      free (native_relocs);
   13220 	      result = false;
   13221 	      continue;
   13222 	    }
   13223 
   13224 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   13225 	      || (bfd_bread (native_relocs, hdr->sh_size, abfd)
   13226 		  != hdr->sh_size))
   13227 	    {
   13228 	      free (native_relocs);
   13229 	      /* The internal_relocs will be freed when
   13230 		 the memory for the bfd is released.  */
   13231 	      result = false;
   13232 	      continue;
   13233 	    }
   13234 
   13235 	  if (dynamic)
   13236 	    symcount = bfd_get_dynamic_symcount (abfd);
   13237 	  else
   13238 	    symcount = bfd_get_symcount (abfd);
   13239 
   13240 	  for (i = 0, internal_reloc = internal_relocs,
   13241 		 native_reloc = native_relocs;
   13242 	       i < reloc_count;
   13243 	       i++, internal_reloc++, native_reloc += entsize)
   13244 	    {
   13245 	      bool res;
   13246 	      Elf_Internal_Rela rela;
   13247 
   13248 	      if (entsize == ebd->s->sizeof_rel)
   13249 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   13250 	      else /* entsize == ebd->s->sizeof_rela */
   13251 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   13252 
   13253 	      /* The address of an ELF reloc is section relative for an object
   13254 		 file, and absolute for an executable file or shared library.
   13255 		 The address of a normal BFD reloc is always section relative,
   13256 		 and the address of a dynamic reloc is absolute..  */
   13257 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   13258 		internal_reloc->address = rela.r_offset;
   13259 	      else
   13260 		internal_reloc->address = rela.r_offset - sec->vma;
   13261 
   13262 	      if (r_sym (rela.r_info) == STN_UNDEF)
   13263 		{
   13264 		  /* FIXME: This and the error case below mean that we
   13265 		     have a symbol on relocs that is not elf_symbol_type.  */
   13266 		  internal_reloc->sym_ptr_ptr =
   13267 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   13268 		}
   13269 	      else if (r_sym (rela.r_info) > symcount)
   13270 		{
   13271 		  _bfd_error_handler
   13272 		    /* xgettext:c-format */
   13273 		    (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
   13274 		     abfd, sec, i, (long) r_sym (rela.r_info));
   13275 		  bfd_set_error (bfd_error_bad_value);
   13276 		  internal_reloc->sym_ptr_ptr =
   13277 		    bfd_abs_section_ptr->symbol_ptr_ptr;
   13278 		  result = false;
   13279 		}
   13280 	      else
   13281 		{
   13282 		  asymbol **ps;
   13283 
   13284 		  ps = symbols + r_sym (rela.r_info) - 1;
   13285 		  internal_reloc->sym_ptr_ptr = ps;
   13286 		  /* Make sure that this symbol is not removed by strip.  */
   13287 		  (*ps)->flags |= BSF_KEEP;
   13288 		}
   13289 
   13290 	      internal_reloc->addend = rela.r_addend;
   13291 
   13292 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   13293 	      if (! res || internal_reloc->howto == NULL)
   13294 		{
   13295 #if DEBUG_SECONDARY_RELOCS
   13296 		  fprintf (stderr, "there is no howto associated with reloc %lx\n",
   13297 			   rela.r_info);
   13298 #endif
   13299 		  result = false;
   13300 		}
   13301 	    }
   13302 
   13303 	  free (native_relocs);
   13304 	  /* Store the internal relocs.  */
   13305 	  elf_section_data (relsec)->sec_info = internal_relocs;
   13306 	}
   13307     }
   13308 
   13309   return result;
   13310 }
   13311 
   13312 /* Set the ELF section header fields of an output secondary reloc section.  */
   13313 
   13314 bool
   13315 _bfd_elf_copy_special_section_fields (const bfd *   ibfd ATTRIBUTE_UNUSED,
   13316 				      bfd *         obfd ATTRIBUTE_UNUSED,
   13317 				      const Elf_Internal_Shdr * isection,
   13318 				      Elf_Internal_Shdr *       osection)
   13319 {
   13320   asection * isec;
   13321   asection * osec;
   13322   struct bfd_elf_section_data * esd;
   13323 
   13324   if (isection == NULL)
   13325     return false;
   13326 
   13327   if (isection->sh_type != SHT_SECONDARY_RELOC)
   13328     return true;
   13329 
   13330   isec = isection->bfd_section;
   13331   if (isec == NULL)
   13332     return false;
   13333 
   13334   osec = osection->bfd_section;
   13335   if (osec == NULL)
   13336     return false;
   13337 
   13338   esd = elf_section_data (osec);
   13339   BFD_ASSERT (esd->sec_info == NULL);
   13340   esd->sec_info = elf_section_data (isec)->sec_info;
   13341   osection->sh_type = SHT_RELA;
   13342   osection->sh_link = elf_onesymtab (obfd);
   13343   if (osection->sh_link == 0)
   13344     {
   13345       /* There is no symbol table - we are hosed...  */
   13346       _bfd_error_handler
   13347 	/* xgettext:c-format */
   13348 	(_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
   13349 	obfd, osec);
   13350       bfd_set_error (bfd_error_bad_value);
   13351       return false;
   13352     }
   13353 
   13354   /* Find the output section that corresponds to the isection's sh_info link.  */
   13355   if (isection->sh_info == 0
   13356       || isection->sh_info >= elf_numsections (ibfd))
   13357     {
   13358       _bfd_error_handler
   13359 	/* xgettext:c-format */
   13360 	(_("%pB(%pA): info section index is invalid"),
   13361 	obfd, osec);
   13362       bfd_set_error (bfd_error_bad_value);
   13363       return false;
   13364     }
   13365 
   13366   isection = elf_elfsections (ibfd)[isection->sh_info];
   13367 
   13368   if (isection == NULL
   13369       || isection->bfd_section == NULL
   13370       || isection->bfd_section->output_section == NULL)
   13371     {
   13372       _bfd_error_handler
   13373 	/* xgettext:c-format */
   13374 	(_("%pB(%pA): info section index cannot be set because the section is not in the output"),
   13375 	obfd, osec);
   13376       bfd_set_error (bfd_error_bad_value);
   13377       return false;
   13378     }
   13379 
   13380   esd = elf_section_data (isection->bfd_section->output_section);
   13381   BFD_ASSERT (esd != NULL);
   13382   osection->sh_info = esd->this_idx;
   13383   esd->has_secondary_relocs = true;
   13384 #if DEBUG_SECONDARY_RELOCS
   13385   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   13386 	   osec->name, osection->sh_link, osection->sh_info);
   13387   fprintf (stderr, "mark section %s as having secondary relocs\n",
   13388 	   bfd_section_name (isection->bfd_section->output_section));
   13389 #endif
   13390 
   13391   return true;
   13392 }
   13393 
   13394 /* Write out a secondary reloc section.
   13395 
   13396    FIXME: Currently this function can result in a serious performance penalty
   13397    for files with secondary relocs and lots of sections.  The proper way to
   13398    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   13399    relocs together and then to have this function just walk that chain.  */
   13400 
   13401 bool
   13402 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   13403 {
   13404   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13405   bfd_vma addr_offset;
   13406   asection * relsec;
   13407   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   13408   bool result = true;
   13409 
   13410   if (sec == NULL)
   13411     return false;
   13412 
   13413 #if BFD_DEFAULT_TARGET_SIZE > 32
   13414   if (bfd_arch_bits_per_address (abfd) != 32)
   13415     r_info = elf64_r_info;
   13416   else
   13417 #endif
   13418     r_info = elf32_r_info;
   13419 
   13420   /* The address of an ELF reloc is section relative for an object
   13421      file, and absolute for an executable file or shared library.
   13422      The address of a BFD reloc is always section relative.  */
   13423   addr_offset = 0;
   13424   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   13425     addr_offset = sec->vma;
   13426 
   13427   /* Discover if there are any secondary reloc sections
   13428      associated with SEC.  */
   13429   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13430     {
   13431       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   13432       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   13433 
   13434       if (hdr->sh_type == SHT_RELA
   13435 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   13436 	{
   13437 	  asymbol *    last_sym;
   13438 	  int          last_sym_idx;
   13439 	  unsigned int reloc_count;
   13440 	  unsigned int idx;
   13441 	  unsigned int entsize;
   13442 	  arelent *    src_irel;
   13443 	  bfd_byte *   dst_rela;
   13444 
   13445 	  if (hdr->contents != NULL)
   13446 	    {
   13447 	      _bfd_error_handler
   13448 		/* xgettext:c-format */
   13449 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   13450 		 abfd, relsec);
   13451 	      bfd_set_error (bfd_error_bad_value);
   13452 	      result = false;
   13453 	      continue;
   13454 	    }
   13455 
   13456 	  entsize = hdr->sh_entsize;
   13457 	  if (entsize == 0)
   13458 	    {
   13459 	      _bfd_error_handler
   13460 		/* xgettext:c-format */
   13461 		(_("%pB(%pA): error: secondary reloc section has zero sized entries"),
   13462 		 abfd, relsec);
   13463 	      bfd_set_error (bfd_error_bad_value);
   13464 	      result = false;
   13465 	      continue;
   13466 	    }
   13467 	  else if (entsize != ebd->s->sizeof_rel
   13468 		   && entsize != ebd->s->sizeof_rela)
   13469 	    {
   13470 	      _bfd_error_handler
   13471 		/* xgettext:c-format */
   13472 		(_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
   13473 		 abfd, relsec);
   13474 	      bfd_set_error (bfd_error_bad_value);
   13475 	      result = false;
   13476 	      continue;
   13477 	    }
   13478 
   13479 	  reloc_count = hdr->sh_size / entsize;
   13480 	  if (reloc_count <= 0)
   13481 	    {
   13482 	      _bfd_error_handler
   13483 		/* xgettext:c-format */
   13484 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   13485 		 abfd, relsec);
   13486 	      bfd_set_error (bfd_error_bad_value);
   13487 	      result = false;
   13488 	      continue;
   13489 	    }
   13490 
   13491 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   13492 	  if (hdr->contents == NULL)
   13493 	    continue;
   13494 
   13495 #if DEBUG_SECONDARY_RELOCS
   13496 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   13497 		   reloc_count, sec->name, relsec->name);
   13498 #endif
   13499 	  last_sym = NULL;
   13500 	  last_sym_idx = 0;
   13501 	  dst_rela = hdr->contents;
   13502 	  src_irel = (arelent *) esd->sec_info;
   13503 	  if (src_irel == NULL)
   13504 	    {
   13505 	      _bfd_error_handler
   13506 		/* xgettext:c-format */
   13507 		(_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
   13508 		 abfd, relsec);
   13509 	      bfd_set_error (bfd_error_bad_value);
   13510 	      result = false;
   13511 	      continue;
   13512 	    }
   13513 
   13514 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   13515 	    {
   13516 	      Elf_Internal_Rela src_rela;
   13517 	      arelent *ptr;
   13518 	      asymbol *sym;
   13519 	      int n;
   13520 
   13521 	      ptr = src_irel + idx;
   13522 	      if (ptr == NULL)
   13523 		{
   13524 		  _bfd_error_handler
   13525 		    /* xgettext:c-format */
   13526 		    (_("%pB(%pA): error: reloc table entry %u is empty"),
   13527 		     abfd, relsec, idx);
   13528 		  bfd_set_error (bfd_error_bad_value);
   13529 		  result = false;
   13530 		  break;
   13531 		}
   13532 
   13533 	      if (ptr->sym_ptr_ptr == NULL)
   13534 		{
   13535 		  /* FIXME: Is this an error ? */
   13536 		  n = 0;
   13537 		}
   13538 	      else
   13539 		{
   13540 		  sym = *ptr->sym_ptr_ptr;
   13541 
   13542 		  if (sym == last_sym)
   13543 		    n = last_sym_idx;
   13544 		  else
   13545 		    {
   13546 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   13547 		      if (n < 0)
   13548 			{
   13549 			  _bfd_error_handler
   13550 			    /* xgettext:c-format */
   13551 			    (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
   13552 			     abfd, relsec, idx);
   13553 			  bfd_set_error (bfd_error_bad_value);
   13554 			  result = false;
   13555 			  n = 0;
   13556 			}
   13557 
   13558 		      last_sym = sym;
   13559 		      last_sym_idx = n;
   13560 		    }
   13561 
   13562 		  if (sym->the_bfd != NULL
   13563 		      && sym->the_bfd->xvec != abfd->xvec
   13564 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   13565 		    {
   13566 		      _bfd_error_handler
   13567 			/* xgettext:c-format */
   13568 			(_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
   13569 			 abfd, relsec, idx);
   13570 		      bfd_set_error (bfd_error_bad_value);
   13571 		      result = false;
   13572 		      n = 0;
   13573 		    }
   13574 		}
   13575 
   13576 	      src_rela.r_offset = ptr->address + addr_offset;
   13577 	      if (ptr->howto == NULL)
   13578 		{
   13579 		  _bfd_error_handler
   13580 		    /* xgettext:c-format */
   13581 		    (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
   13582 		     abfd, relsec, idx);
   13583 		  bfd_set_error (bfd_error_bad_value);
   13584 		  result = false;
   13585 		  src_rela.r_info = r_info (0, 0);
   13586 		}
   13587 	      else
   13588 		src_rela.r_info = r_info (n, ptr->howto->type);
   13589 	      src_rela.r_addend = ptr->addend;
   13590 
   13591 	      if (entsize == ebd->s->sizeof_rel)
   13592 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   13593 	      else /* entsize == ebd->s->sizeof_rela */
   13594 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   13595 	    }
   13596 	}
   13597     }
   13598 
   13599   return result;
   13600 }
   13601