Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.14
      1 /* ELF executable support for BFD.
      2 
      3    Copyright (C) 1993-2025 Free Software Foundation, Inc.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 
     23 /*
     24 SECTION
     25 	ELF backends
     26 
     27 	BFD support for ELF formats is being worked on.
     28 	Currently, the best supported back ends are for sparc and i386
     29 	(running svr4 or Solaris 2).
     30 
     31 	Documentation of the internals of the support code still needs
     32 	to be written.  The code is changing quickly enough that we
     33 	haven't bothered yet.  */
     34 
     35 /* For sparc64-cross-sparc32.  */
     36 #define _SYSCALL32
     37 #include "sysdep.h"
     38 #include <limits.h>
     39 #include "bfd.h"
     40 #include "bfdlink.h"
     41 #include "libbfd.h"
     42 #define ARCH_SIZE 0
     43 #include "elf-bfd.h"
     44 #include "libiberty.h"
     45 #include "safe-ctype.h"
     46 #include "elf-linux-core.h"
     47 
     48 #ifdef CORE_HEADER
     49 #include CORE_HEADER
     50 #endif
     51 
     52 static int elf_sort_sections (const void *, const void *);
     53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
     54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
     55 			   struct bfd_link_info *);
     56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
     57 			     file_ptr offset, size_t align);
     58 
     59 /* Swap version information in and out.  The version information is
     60    currently size independent.  If that ever changes, this code will
     61    need to move into elfcode.h.  */
     62 
     63 /* Swap in a Verdef structure.  */
     64 
     65 void
     66 _bfd_elf_swap_verdef_in (bfd *abfd,
     67 			 const Elf_External_Verdef *src,
     68 			 Elf_Internal_Verdef *dst)
     69 {
     70   dst->vd_version = H_GET_16 (abfd, src->vd_version);
     71   dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
     72   dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
     73   dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
     74   dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
     75   dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
     76   dst->vd_next    = H_GET_32 (abfd, src->vd_next);
     77 }
     78 
     79 /* Swap out a Verdef structure.  */
     80 
     81 void
     82 _bfd_elf_swap_verdef_out (bfd *abfd,
     83 			  const Elf_Internal_Verdef *src,
     84 			  Elf_External_Verdef *dst)
     85 {
     86   H_PUT_16 (abfd, src->vd_version, dst->vd_version);
     87   H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
     88   H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
     89   H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
     90   H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
     91   H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
     92   H_PUT_32 (abfd, src->vd_next, dst->vd_next);
     93 }
     94 
     95 /* Swap in a Verdaux structure.  */
     96 
     97 void
     98 _bfd_elf_swap_verdaux_in (bfd *abfd,
     99 			  const Elf_External_Verdaux *src,
    100 			  Elf_Internal_Verdaux *dst)
    101 {
    102   dst->vda_name = H_GET_32 (abfd, src->vda_name);
    103   dst->vda_next = H_GET_32 (abfd, src->vda_next);
    104 }
    105 
    106 /* Swap out a Verdaux structure.  */
    107 
    108 void
    109 _bfd_elf_swap_verdaux_out (bfd *abfd,
    110 			   const Elf_Internal_Verdaux *src,
    111 			   Elf_External_Verdaux *dst)
    112 {
    113   H_PUT_32 (abfd, src->vda_name, dst->vda_name);
    114   H_PUT_32 (abfd, src->vda_next, dst->vda_next);
    115 }
    116 
    117 /* Swap in a Verneed structure.  */
    118 
    119 void
    120 _bfd_elf_swap_verneed_in (bfd *abfd,
    121 			  const Elf_External_Verneed *src,
    122 			  Elf_Internal_Verneed *dst)
    123 {
    124   dst->vn_version = H_GET_16 (abfd, src->vn_version);
    125   dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
    126   dst->vn_file    = H_GET_32 (abfd, src->vn_file);
    127   dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
    128   dst->vn_next    = H_GET_32 (abfd, src->vn_next);
    129 }
    130 
    131 /* Swap out a Verneed structure.  */
    132 
    133 void
    134 _bfd_elf_swap_verneed_out (bfd *abfd,
    135 			   const Elf_Internal_Verneed *src,
    136 			   Elf_External_Verneed *dst)
    137 {
    138   H_PUT_16 (abfd, src->vn_version, dst->vn_version);
    139   H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
    140   H_PUT_32 (abfd, src->vn_file, dst->vn_file);
    141   H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
    142   H_PUT_32 (abfd, src->vn_next, dst->vn_next);
    143 }
    144 
    145 /* Swap in a Vernaux structure.  */
    146 
    147 void
    148 _bfd_elf_swap_vernaux_in (bfd *abfd,
    149 			  const Elf_External_Vernaux *src,
    150 			  Elf_Internal_Vernaux *dst)
    151 {
    152   dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
    153   dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
    154   dst->vna_other = H_GET_16 (abfd, src->vna_other);
    155   dst->vna_name  = H_GET_32 (abfd, src->vna_name);
    156   dst->vna_next  = H_GET_32 (abfd, src->vna_next);
    157 }
    158 
    159 /* Swap out a Vernaux structure.  */
    160 
    161 void
    162 _bfd_elf_swap_vernaux_out (bfd *abfd,
    163 			   const Elf_Internal_Vernaux *src,
    164 			   Elf_External_Vernaux *dst)
    165 {
    166   H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
    167   H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
    168   H_PUT_16 (abfd, src->vna_other, dst->vna_other);
    169   H_PUT_32 (abfd, src->vna_name, dst->vna_name);
    170   H_PUT_32 (abfd, src->vna_next, dst->vna_next);
    171 }
    172 
    173 /* Swap in a Versym structure.  */
    174 
    175 void
    176 _bfd_elf_swap_versym_in (bfd *abfd,
    177 			 const Elf_External_Versym *src,
    178 			 Elf_Internal_Versym *dst)
    179 {
    180   dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
    181 }
    182 
    183 /* Swap out a Versym structure.  */
    184 
    185 void
    186 _bfd_elf_swap_versym_out (bfd *abfd,
    187 			  const Elf_Internal_Versym *src,
    188 			  Elf_External_Versym *dst)
    189 {
    190   H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
    191 }
    192 
    193 /* Standard ELF hash function.  Do not change this function; you will
    194    cause invalid hash tables to be generated.  */
    195 
    196 unsigned long
    197 bfd_elf_hash (const char *namearg)
    198 {
    199   uint32_t h = 0;
    200 
    201   for (const unsigned char *name = (const unsigned char *) namearg;
    202        *name; name++)
    203     {
    204       h = (h << 4) + *name;
    205       h ^= (h >> 24) & 0xf0;
    206     }
    207   return h & 0x0fffffff;
    208 }
    209 
    210 /* DT_GNU_HASH hash function.  Do not change this function; you will
    211    cause invalid hash tables to be generated.  */
    212 
    213 unsigned long
    214 bfd_elf_gnu_hash (const char *namearg)
    215 {
    216   uint32_t h = 5381;
    217 
    218   for (const unsigned char *name = (const unsigned char *) namearg;
    219        *name; name++)
    220     h = (h << 5) + h + *name;
    221   return h;
    222 }
    223 
    224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    225    the object_id field of an elf_obj_tdata field set.  */
    226 bool
    227 bfd_elf_allocate_object (bfd *abfd,
    228 			 size_t object_size)
    229 {
    230   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    231   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    232   if (abfd->tdata.any == NULL)
    233     return false;
    234 
    235   elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
    236   if (abfd->direction != read_direction)
    237     {
    238       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    239       if (o == NULL)
    240 	return false;
    241       elf_tdata (abfd)->o = o;
    242       elf_program_header_size (abfd) = (bfd_size_type) -1;
    243     }
    244   return true;
    245 }
    246 
    247 
    248 bool
    249 bfd_elf_make_object (bfd *abfd)
    250 {
    251   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
    252 }
    253 
    254 bool
    255 bfd_elf_mkcorefile (bfd *abfd)
    256 {
    257   /* I think this can be done just like an object file.  */
    258   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    259     return false;
    260   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    261   return elf_tdata (abfd)->core != NULL;
    262 }
    263 
    264 char *
    265 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    266 {
    267   Elf_Internal_Shdr **i_shdrp;
    268   bfd_byte *shstrtab = NULL;
    269   file_ptr offset;
    270   bfd_size_type shstrtabsize;
    271 
    272   i_shdrp = elf_elfsections (abfd);
    273   if (i_shdrp == 0
    274       || shindex >= elf_numsections (abfd)
    275       || i_shdrp[shindex] == 0)
    276     return NULL;
    277 
    278   shstrtab = i_shdrp[shindex]->contents;
    279   if (shstrtab == NULL)
    280     {
    281       /* No cached one, attempt to read, and cache what we read.  */
    282       offset = i_shdrp[shindex]->sh_offset;
    283       shstrtabsize = i_shdrp[shindex]->sh_size;
    284 
    285       if (shstrtabsize == 0
    286 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    287 	  || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
    288 	{
    289 	  /* Once we've failed to read it, make sure we don't keep
    290 	     trying.  Otherwise, we'll keep allocating space for
    291 	     the string table over and over.  */
    292 	  i_shdrp[shindex]->sh_size = 0;
    293 	}
    294       else if (shstrtab[shstrtabsize - 1] != 0)
    295 	{
    296 	  /* It is an error if a string table isn't terminated.  */
    297 	  _bfd_error_handler
    298 	    /* xgettext:c-format */
    299 	    (_("%pB: string table [%u] is corrupt"), abfd, shindex);
    300 	  shstrtab[shstrtabsize - 1] = 0;
    301 	}
    302       i_shdrp[shindex]->contents = shstrtab;
    303     }
    304   return (char *) shstrtab;
    305 }
    306 
    307 char *
    308 bfd_elf_string_from_elf_section (bfd *abfd,
    309 				 unsigned int shindex,
    310 				 unsigned int strindex)
    311 {
    312   Elf_Internal_Shdr *hdr;
    313 
    314   if (strindex == 0)
    315     return "";
    316 
    317   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    318     return NULL;
    319 
    320   hdr = elf_elfsections (abfd)[shindex];
    321 
    322   if (hdr->contents == NULL)
    323     {
    324       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    325 	{
    326 	  /* PR 17512: file: f057ec89.  */
    327 	  /* xgettext:c-format */
    328 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    329 				" a non-string section (number %d)"),
    330 			      abfd, shindex);
    331 	  return NULL;
    332 	}
    333 
    334       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    335 	return NULL;
    336     }
    337   else
    338     {
    339       /* PR 24273: The string section's contents may have already
    340 	 been loaded elsewhere, eg because a corrupt file has the
    341 	 string section index in the ELF header pointing at a group
    342 	 section.  So be paranoid, and test that the last byte of
    343 	 the section is zero.  */
    344       if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
    345 	return NULL;
    346     }
    347 
    348   if (strindex >= hdr->sh_size)
    349     {
    350       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    351       _bfd_error_handler
    352 	/* xgettext:c-format */
    353 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    354 	 abfd, strindex, (uint64_t) hdr->sh_size,
    355 	 (shindex == shstrndx && strindex == hdr->sh_name
    356 	  ? ".shstrtab"
    357 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    358       return NULL;
    359     }
    360 
    361   return ((char *) hdr->contents) + strindex;
    362 }
    363 
    364 /* Read and convert symbols to internal format.
    365    SYMCOUNT specifies the number of symbols to read, starting from
    366    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    367    are non-NULL, they are used to store the internal symbols, external
    368    symbols, and symbol section index extensions, respectively.
    369    Returns a pointer to the internal symbol buffer (malloced if necessary)
    370    or NULL if there were no symbols or some kind of problem.  */
    371 
    372 Elf_Internal_Sym *
    373 bfd_elf_get_elf_syms (bfd *ibfd,
    374 		      Elf_Internal_Shdr *symtab_hdr,
    375 		      size_t symcount,
    376 		      size_t symoffset,
    377 		      Elf_Internal_Sym *intsym_buf,
    378 		      void *extsym_buf,
    379 		      Elf_External_Sym_Shndx *extshndx_buf)
    380 {
    381   Elf_Internal_Shdr *shndx_hdr;
    382   void *alloc_ext;
    383   const bfd_byte *esym;
    384   Elf_External_Sym_Shndx *alloc_extshndx;
    385   Elf_External_Sym_Shndx *shndx;
    386   Elf_Internal_Sym *alloc_intsym;
    387   Elf_Internal_Sym *isym;
    388   Elf_Internal_Sym *isymend;
    389   const struct elf_backend_data *bed;
    390   size_t extsym_size;
    391   size_t amt;
    392   file_ptr pos;
    393 
    394   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    395     abort ();
    396 
    397   if (symcount == 0)
    398     return intsym_buf;
    399 
    400   if (elf_use_dt_symtab_p (ibfd))
    401     {
    402       /* Use dynamic symbol table.  */
    403       if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
    404 	{
    405 	  bfd_set_error (bfd_error_invalid_operation);
    406 	  return NULL;
    407 	}
    408       return elf_tdata (ibfd)->dt_symtab + symoffset;
    409     }
    410 
    411   /* Normal syms might have section extension entries.  */
    412   shndx_hdr = NULL;
    413   if (elf_symtab_shndx_list (ibfd) != NULL)
    414     {
    415       elf_section_list * entry;
    416       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    417 
    418       /* Find an index section that is linked to this symtab section.  */
    419       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    420 	{
    421 	  /* PR 20063.  */
    422 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    423 	    continue;
    424 
    425 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    426 	    {
    427 	      shndx_hdr = & entry->hdr;
    428 	      break;
    429 	    };
    430 	}
    431 
    432       if (shndx_hdr == NULL)
    433 	{
    434 	  if (symtab_hdr == &elf_symtab_hdr (ibfd))
    435 	    /* Not really accurate, but this was how the old code used
    436 	       to work.  */
    437 	    shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
    438 	  /* Otherwise we do nothing.  The assumption is that
    439 	     the index table will not be needed.  */
    440 	}
    441     }
    442 
    443   /* Read the symbols.  */
    444   alloc_ext = NULL;
    445   alloc_extshndx = NULL;
    446   alloc_intsym = NULL;
    447   bed = get_elf_backend_data (ibfd);
    448   extsym_size = bed->s->sizeof_sym;
    449   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
    450     {
    451       bfd_set_error (bfd_error_file_too_big);
    452       return NULL;
    453     }
    454   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    455   size_t alloc_ext_size = amt;
    456   if (bfd_seek (ibfd, pos, SEEK_SET) != 0
    457       || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
    458 				    &alloc_ext, ibfd, false))
    459     {
    460       intsym_buf = NULL;
    461       goto out2;
    462     }
    463 
    464   size_t alloc_extshndx_size = 0;
    465   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    466     extshndx_buf = NULL;
    467   else
    468     {
    469       if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
    470 	{
    471 	  bfd_set_error (bfd_error_file_too_big);
    472 	  intsym_buf = NULL;
    473 	  goto out1;
    474 	}
    475       alloc_extshndx_size = amt;
    476       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    477       if (bfd_seek (ibfd, pos, SEEK_SET) != 0
    478 	  || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
    479 					&alloc_extshndx_size,
    480 					(void **) &alloc_extshndx,
    481 					ibfd, false))
    482 	{
    483 	  intsym_buf = NULL;
    484 	  goto out1;
    485 	}
    486     }
    487 
    488   if (intsym_buf == NULL)
    489     {
    490       if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
    491 	{
    492 	  bfd_set_error (bfd_error_file_too_big);
    493 	  goto out1;
    494 	}
    495       alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
    496       intsym_buf = alloc_intsym;
    497       if (intsym_buf == NULL)
    498 	goto out1;
    499     }
    500 
    501   /* Convert the symbols to internal form.  */
    502   isymend = intsym_buf + symcount;
    503   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    504 	   shndx = extshndx_buf;
    505        isym < isymend;
    506        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    507     {
    508       if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    509 	{
    510 	  symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    511 	  /* xgettext:c-format */
    512 	  _bfd_error_handler (_("%pB symbol number %lu references"
    513 				" nonexistent SHT_SYMTAB_SHNDX section"),
    514 			      ibfd, (unsigned long) symoffset);
    515 	  free (alloc_intsym);
    516 	  intsym_buf = NULL;
    517 	  goto out1;
    518 	}
    519 
    520       /* PR 33019: Do not accept unsupported binding values - they will
    521 	 likely cause problems later on.  */
    522       int bind = ELF_ST_BIND (isym->st_info);
    523       if (bind > STB_WEAK && bind < STB_LOOS)
    524 	{
    525 	  /* xgettext:c-format */
    526 	  _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
    527 			      ibfd, (unsigned long) (isym - intsym_buf), bind);
    528 	  free (alloc_intsym);
    529 	  intsym_buf = NULL;
    530 	  goto out1;
    531 	}
    532 
    533       /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
    534       int t = ELF_ST_TYPE (isym->st_info);
    535       if (t == 7)
    536 	{
    537 	  /* xgettext:c-format */
    538 	  _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
    539 			      ibfd, (unsigned long) (isym - intsym_buf), t);
    540 	  free (alloc_intsym);
    541 	  intsym_buf = NULL;
    542 	  goto out1;
    543 	}
    544     }
    545 
    546  out1:
    547   _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
    548  out2:
    549   _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
    550 
    551   return intsym_buf;
    552 }
    553 
    554 /* Look up a symbol name.  */
    555 static const char *
    556 bfd_elf_sym_name_raw (bfd *abfd,
    557 		      Elf_Internal_Shdr *symtab_hdr,
    558 		      Elf_Internal_Sym *isym)
    559 {
    560   unsigned int iname = isym->st_name;
    561   unsigned int shindex = symtab_hdr->sh_link;
    562 
    563   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    564       /* Check for a bogus st_shndx to avoid crashing.  */
    565       && isym->st_shndx < elf_numsections (abfd))
    566     {
    567       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    568       shindex = elf_elfheader (abfd)->e_shstrndx;
    569     }
    570 
    571   return bfd_elf_string_from_elf_section (abfd, shindex, iname);
    572 }
    573 
    574 const char *
    575 bfd_elf_sym_name (bfd *abfd,
    576 		  Elf_Internal_Shdr *symtab_hdr,
    577 		  Elf_Internal_Sym *isym,
    578 		  asection *sym_sec)
    579 {
    580   const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
    581   if (name == NULL)
    582     name = bfd_symbol_error_name;
    583   else if (sym_sec && *name == '\0')
    584     name = bfd_section_name (sym_sec);
    585 
    586   return name;
    587 }
    588 
    589 /* Return the name of the group signature symbol.  Why isn't the
    590    signature just a string?  */
    591 
    592 static const char *
    593 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    594 {
    595   Elf_Internal_Shdr *hdr;
    596   unsigned char esym[sizeof (Elf64_External_Sym)];
    597   Elf_External_Sym_Shndx eshndx;
    598   Elf_Internal_Sym isym;
    599 
    600   /* First we need to ensure the symbol table is available.  Make sure
    601      that it is a symbol table section.  */
    602   if (ghdr->sh_link >= elf_numsections (abfd))
    603     return NULL;
    604   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    605   if (hdr->sh_type != SHT_SYMTAB
    606       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    607     return NULL;
    608 
    609   /* Go read the symbol.  */
    610   hdr = &elf_tdata (abfd)->symtab_hdr;
    611   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    612 			    &isym, esym, &eshndx) == NULL)
    613     return NULL;
    614 
    615   return bfd_elf_sym_name_raw (abfd, hdr, &isym);
    616 }
    617 
    618 static bool
    619 is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
    620 {
    621   return (shdr->sh_size >= minsize
    622 	  && shdr->sh_entsize == GRP_ENTRY_SIZE
    623 	  && shdr->sh_size % GRP_ENTRY_SIZE == 0
    624 	  && shdr->bfd_section != NULL);
    625 }
    626 
    627 
    628 /* Set next_in_group, sec_group list pointers, and group names.  */
    629 
    630 static bool
    631 process_sht_group_entries (bfd *abfd,
    632 			   Elf_Internal_Shdr *ghdr, unsigned int gidx)
    633 {
    634   unsigned char *contents;
    635 
    636   /* Read the raw contents.  */
    637   if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
    638     {
    639       _bfd_error_handler
    640 	/* xgettext:c-format */
    641 	(_("%pB: could not read contents of group [%u]"), abfd, gidx);
    642       return false;
    643     }
    644 
    645   asection *last_elt = NULL;
    646   const char *gname = NULL;
    647   unsigned char *p = contents + ghdr->sh_size;
    648   while (1)
    649     {
    650       unsigned int idx;
    651       Elf_Internal_Shdr *shdr;
    652       asection *elt;
    653 
    654       p -= 4;
    655       idx = H_GET_32 (abfd, p);
    656       if (p == contents)
    657 	{
    658 	  if ((idx & GRP_COMDAT) != 0)
    659 	    ghdr->bfd_section->flags
    660 	      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    661 	  break;
    662 	}
    663 
    664       if (idx == 0
    665 	  || idx >= elf_numsections (abfd)
    666 	  || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
    667 	  || ((elt = shdr->bfd_section) != NULL
    668 	      && elf_sec_group (elt) != NULL
    669 	      && elf_sec_group (elt) != ghdr->bfd_section))
    670 	{
    671 	  _bfd_error_handler
    672 	    (_("%pB: invalid entry (%#x) in group [%u]"),
    673 	     abfd, idx, gidx);
    674 	  continue;
    675 	}
    676 
    677       /* PR binutils/23199: According to the ELF gABI all sections in
    678 	 a group must be marked with SHF_GROUP, but some tools
    679 	 generate broken objects.  Fix them up here.  */
    680       shdr->sh_flags |= SHF_GROUP;
    681 
    682       if (elt == NULL)
    683 	{
    684 	  if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
    685 	    {
    686 	      const char *name = bfd_elf_string_from_elf_section
    687 		(abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
    688 
    689 	      _bfd_error_handler
    690 		/* xgettext:c-format */
    691 		(_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
    692 		 abfd, shdr->sh_type, name, gidx);
    693 	    }
    694 	  continue;
    695 	}
    696 
    697       /* Don't try to add a section to elf_next_in_group list twice.  */
    698       if (elf_sec_group (elt) != NULL)
    699 	continue;
    700 
    701       if (last_elt == NULL)
    702 	{
    703 	  /* Start a circular list with one element.
    704 	     It will be in reverse order to match what gas does.  */
    705 	  elf_next_in_group (elt) = elt;
    706 	  /* Point the group section to it.  */
    707 	  elf_next_in_group (ghdr->bfd_section) = elt;
    708 	  gname = group_signature (abfd, ghdr);
    709 	  if (gname == NULL)
    710 	    {
    711 	      free (contents);
    712 	      return false;
    713 	    }
    714 	}
    715       else
    716 	{
    717 	  elf_next_in_group (elt) = elf_next_in_group (last_elt);
    718 	  elf_next_in_group (last_elt) = elt;
    719 	}
    720       last_elt = elt;
    721       elf_group_name (elt) = gname;
    722       elf_sec_group (elt) = ghdr->bfd_section;
    723     }
    724 
    725   free (contents);
    726   return true;
    727 }
    728 
    729 bool
    730 _bfd_elf_setup_sections (bfd *abfd)
    731 {
    732   bool result = true;
    733 
    734   /* Process SHF_LINK_ORDER.  */
    735   for (asection *s = abfd->sections; s != NULL; s = s->next)
    736     {
    737       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    738       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    739 	{
    740 	  unsigned int elfsec = this_hdr->sh_link;
    741 	  /* An sh_link value of 0 is now allowed.  It indicates that linked
    742 	     to section has already been discarded, but that the current
    743 	     section has been retained for some other reason.  This linking
    744 	     section is still a candidate for later garbage collection
    745 	     however.  */
    746 	  if (elfsec == 0)
    747 	    {
    748 	      elf_linked_to_section (s) = NULL;
    749 	    }
    750 	  else
    751 	    {
    752 	      asection *linksec = NULL;
    753 
    754 	      if (elfsec < elf_numsections (abfd))
    755 		{
    756 		  this_hdr = elf_elfsections (abfd)[elfsec];
    757 		  linksec = this_hdr->bfd_section;
    758 		}
    759 
    760 	      /* PR 1991, 2008:
    761 		 Some strip/objcopy may leave an incorrect value in
    762 		 sh_link.  We don't want to proceed.  */
    763 	      if (linksec == NULL)
    764 		{
    765 		  _bfd_error_handler
    766 		    /* xgettext:c-format */
    767 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    768 		     s->owner, elfsec, s);
    769 		  result = false;
    770 		}
    771 
    772 	      elf_linked_to_section (s) = linksec;
    773 	    }
    774 	}
    775     }
    776 
    777   /* Process section groups.  */
    778   for (unsigned int i = 1; i < elf_numsections (abfd); i++)
    779     {
    780       Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    781 
    782       if (shdr && shdr->sh_type == SHT_GROUP)
    783 	{
    784 	  if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
    785 	    {
    786 	      if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
    787 		  && !process_sht_group_entries (abfd, shdr, i))
    788 		result = false;
    789 	    }
    790 	  else
    791 	    {
    792 	      /* PR binutils/18758: Beware of corrupt binaries with
    793 		 invalid group data.  */
    794 	      _bfd_error_handler
    795 		/* xgettext:c-format */
    796 		(_("%pB: section group entry number %u is corrupt"), abfd, i);
    797 	      result = false;
    798 	    }
    799 	}
    800     }
    801 
    802   return result;
    803 }
    804 
    805 bool
    806 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    807 {
    808   return elf_next_in_group (sec) != NULL;
    809 }
    810 
    811 const char *
    812 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    813 {
    814   if (elf_sec_group (sec) != NULL)
    815     return elf_group_name (sec);
    816   return NULL;
    817 }
    818 
    819 /* Make a BFD section from an ELF section.  We store a pointer to the
    820    BFD section in the bfd_section field of the header.  */
    821 
    822 bool
    823 _bfd_elf_make_section_from_shdr (bfd *abfd,
    824 				 Elf_Internal_Shdr *hdr,
    825 				 const char *name,
    826 				 int shindex)
    827 {
    828   asection *newsect;
    829   flagword flags;
    830   const struct elf_backend_data *bed;
    831   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
    832 
    833   if (hdr->bfd_section != NULL)
    834     return true;
    835 
    836   newsect = bfd_make_section_anyway (abfd, name);
    837   if (newsect == NULL)
    838     return false;
    839 
    840   hdr->bfd_section = newsect;
    841   elf_section_data (newsect)->this_hdr = *hdr;
    842   elf_section_data (newsect)->this_idx = shindex;
    843 
    844   /* Always use the real type/flags.  */
    845   elf_section_type (newsect) = hdr->sh_type;
    846   elf_section_flags (newsect) = hdr->sh_flags;
    847 
    848   newsect->filepos = hdr->sh_offset;
    849 
    850   flags = SEC_NO_FLAGS;
    851   if (hdr->sh_type != SHT_NOBITS)
    852     flags |= SEC_HAS_CONTENTS;
    853   if (hdr->sh_type == SHT_GROUP)
    854     flags |= SEC_GROUP;
    855   if ((hdr->sh_flags & SHF_ALLOC) != 0)
    856     {
    857       flags |= SEC_ALLOC;
    858       if (hdr->sh_type != SHT_NOBITS)
    859 	flags |= SEC_LOAD;
    860     }
    861   if ((hdr->sh_flags & SHF_WRITE) == 0)
    862     flags |= SEC_READONLY;
    863   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
    864     flags |= SEC_CODE;
    865   else if ((flags & SEC_LOAD) != 0)
    866     flags |= SEC_DATA;
    867   if ((hdr->sh_flags & SHF_MERGE) != 0)
    868     {
    869       flags |= SEC_MERGE;
    870       newsect->entsize = hdr->sh_entsize;
    871     }
    872   if ((hdr->sh_flags & SHF_STRINGS) != 0)
    873     {
    874       flags |= SEC_STRINGS;
    875       newsect->entsize = hdr->sh_entsize;
    876     }
    877   if ((hdr->sh_flags & SHF_TLS) != 0)
    878     flags |= SEC_THREAD_LOCAL;
    879   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
    880     flags |= SEC_EXCLUDE;
    881 
    882   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
    883     {
    884       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
    885 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
    886 	 byte.  */
    887     case ELFOSABI_GNU:
    888     case ELFOSABI_FREEBSD:
    889       if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
    890 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
    891       /* Fall through */
    892     case ELFOSABI_NONE:
    893       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
    894 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
    895       break;
    896     }
    897 
    898   if ((flags & SEC_ALLOC) == 0)
    899     {
    900       /* The debugging sections appear to be recognized only by name,
    901 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
    902       if (name [0] == '.')
    903 	{
    904 	  if (startswith (name, ".debug")
    905 	      || startswith (name, ".gnu.debuglto_.debug_")
    906 	      || startswith (name, ".gnu.linkonce.wi.")
    907 	      || startswith (name, ".zdebug"))
    908 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
    909 	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
    910 		   || startswith (name, ".note.gnu"))
    911 	    {
    912 	      flags |= SEC_ELF_OCTETS;
    913 	      opb = 1;
    914 	    }
    915 	  else if (startswith (name, ".line")
    916 		   || startswith (name, ".stab")
    917 		   || strcmp (name, ".gdb_index") == 0)
    918 	    flags |= SEC_DEBUGGING;
    919 	}
    920     }
    921 
    922   if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
    923       || !bfd_set_section_size (newsect, hdr->sh_size)
    924       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
    925 							& -hdr->sh_addralign)))
    926     return false;
    927 
    928   /* As a GNU extension, if the name begins with .gnu.linkonce, we
    929      only link a single copy of the section.  This is used to support
    930      g++.  g++ will emit each template expansion in its own section.
    931      The symbols will be defined as weak, so that multiple definitions
    932      are permitted.  The GNU linker extension is to actually discard
    933      all but one of the sections.  */
    934   if (startswith (name, ".gnu.linkonce")
    935       && elf_next_in_group (newsect) == NULL)
    936     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    937 
    938   if (!bfd_set_section_flags (newsect, flags))
    939     return false;
    940 
    941   bed = get_elf_backend_data (abfd);
    942   if (bed->elf_backend_section_flags)
    943     if (!bed->elf_backend_section_flags (hdr))
    944       return false;
    945 
    946   /* We do not parse the PT_NOTE segments as we are interested even in the
    947      separate debug info files which may have the segments offsets corrupted.
    948      PT_NOTEs from the core files are currently not parsed using BFD.  */
    949   if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
    950     {
    951       bfd_byte *contents;
    952 
    953       if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
    954 	return false;
    955 
    956       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
    957 		       hdr->sh_offset, hdr->sh_addralign);
    958       _bfd_elf_munmap_section_contents (newsect, contents);
    959     }
    960 
    961   if ((newsect->flags & SEC_ALLOC) != 0)
    962     {
    963       Elf_Internal_Phdr *phdr;
    964       unsigned int i, nload;
    965 
    966       /* Some ELF linkers produce binaries with all the program header
    967 	 p_paddr fields zero.  If we have such a binary with more than
    968 	 one PT_LOAD header, then leave the section lma equal to vma
    969 	 so that we don't create sections with overlapping lma.  */
    970       phdr = elf_tdata (abfd)->phdr;
    971       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
    972 	if (phdr->p_paddr != 0)
    973 	  break;
    974 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
    975 	  ++nload;
    976       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
    977 	return true;
    978 
    979       phdr = elf_tdata (abfd)->phdr;
    980       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
    981 	{
    982 	  if (((phdr->p_type == PT_LOAD
    983 		&& (hdr->sh_flags & SHF_TLS) == 0)
    984 	       || phdr->p_type == PT_TLS)
    985 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
    986 	    {
    987 	      if ((newsect->flags & SEC_LOAD) == 0)
    988 		newsect->lma = (phdr->p_paddr
    989 				+ hdr->sh_addr - phdr->p_vaddr) / opb;
    990 	      else
    991 		/* We used to use the same adjustment for SEC_LOAD
    992 		   sections, but that doesn't work if the segment
    993 		   is packed with code from multiple VMAs.
    994 		   Instead we calculate the section LMA based on
    995 		   the segment LMA.  It is assumed that the
    996 		   segment will contain sections with contiguous
    997 		   LMAs, even if the VMAs are not.  */
    998 		newsect->lma = (phdr->p_paddr
    999 				+ hdr->sh_offset - phdr->p_offset) / opb;
   1000 
   1001 	      /* With contiguous segments, we can't tell from file
   1002 		 offsets whether a section with zero size should
   1003 		 be placed at the end of one segment or the
   1004 		 beginning of the next.  Decide based on vaddr.  */
   1005 	      if (hdr->sh_addr >= phdr->p_vaddr
   1006 		  && (hdr->sh_addr + hdr->sh_size
   1007 		      <= phdr->p_vaddr + phdr->p_memsz))
   1008 		break;
   1009 	    }
   1010 	}
   1011     }
   1012 
   1013   /* Compress/decompress DWARF debug sections with names: .debug_*,
   1014      .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
   1015   if ((newsect->flags & SEC_DEBUGGING) != 0
   1016       && (newsect->flags & SEC_HAS_CONTENTS) != 0
   1017       && (newsect->flags & SEC_ELF_OCTETS) != 0)
   1018     {
   1019       enum { nothing, compress, decompress } action = nothing;
   1020       int compression_header_size;
   1021       bfd_size_type uncompressed_size;
   1022       unsigned int uncompressed_align_power;
   1023       enum compression_type ch_type = ch_none;
   1024       bool compressed
   1025 	= bfd_is_section_compressed_info (abfd, newsect,
   1026 					  &compression_header_size,
   1027 					  &uncompressed_size,
   1028 					  &uncompressed_align_power,
   1029 					  &ch_type);
   1030 
   1031       /* Should we decompress?  */
   1032       if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
   1033 	action = decompress;
   1034 
   1035       /* Should we compress?  Or convert to a different compression?  */
   1036       else if ((abfd->flags & BFD_COMPRESS) != 0
   1037 	       && newsect->size != 0
   1038 	       && compression_header_size >= 0
   1039 	       && uncompressed_size > 0)
   1040 	{
   1041 	  if (!compressed)
   1042 	    action = compress;
   1043 	  else
   1044 	    {
   1045 	      enum compression_type new_ch_type = ch_none;
   1046 	      if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
   1047 		new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
   1048 			       ? ch_compress_zstd : ch_compress_zlib);
   1049 	      if (new_ch_type != ch_type)
   1050 		action = compress;
   1051 	    }
   1052 	}
   1053 
   1054       if (action == compress)
   1055 	{
   1056 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1057 	    {
   1058 	      _bfd_error_handler
   1059 		/* xgettext:c-format */
   1060 		(_("%pB: unable to compress section %s"), abfd, name);
   1061 	      return false;
   1062 	    }
   1063 	}
   1064       else if (action == decompress)
   1065 	{
   1066 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1067 	    {
   1068 	      _bfd_error_handler
   1069 		/* xgettext:c-format */
   1070 		(_("%pB: unable to decompress section %s"), abfd, name);
   1071 	      return false;
   1072 	    }
   1073 #ifndef HAVE_ZSTD
   1074 	  if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
   1075 	    {
   1076 	      _bfd_error_handler
   1077 		  /* xgettext:c-format */
   1078 		  (_ ("%pB: section %s is compressed with zstd, but BFD "
   1079 		      "is not built with zstd support"),
   1080 		   abfd, name);
   1081 	      newsect->compress_status = COMPRESS_SECTION_NONE;
   1082 	      return false;
   1083 	    }
   1084 #endif
   1085 	  if (abfd->is_linker_input
   1086 	      && name[1] == 'z')
   1087 	    {
   1088 	      /* Rename section from .zdebug_* to .debug_* so that ld
   1089 		 scripts will see this section as a debug section.  */
   1090 	      char *new_name = bfd_zdebug_name_to_debug (abfd, name);
   1091 	      if (new_name == NULL)
   1092 		return false;
   1093 	      bfd_rename_section (newsect, new_name);
   1094 	    }
   1095 	}
   1096     }
   1097 
   1098   return true;
   1099 }
   1100 
   1101 const char *const bfd_elf_section_type_names[] =
   1102 {
   1103   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1104   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1105   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1106 };
   1107 
   1108 /* ELF relocs are against symbols.  If we are producing relocatable
   1109    output, and the reloc is against an external symbol, and nothing
   1110    has given us any additional addend, the resulting reloc will also
   1111    be against the same symbol.  In such a case, we don't want to
   1112    change anything about the way the reloc is handled, since it will
   1113    all be done at final link time.  Rather than put special case code
   1114    into bfd_perform_relocation, all the reloc types use this howto
   1115    function, or should call this function for relocatable output.  */
   1116 
   1117 bfd_reloc_status_type
   1118 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1119 		       arelent *reloc_entry,
   1120 		       asymbol *symbol,
   1121 		       void *data ATTRIBUTE_UNUSED,
   1122 		       asection *input_section,
   1123 		       bfd *output_bfd,
   1124 		       char **error_message ATTRIBUTE_UNUSED)
   1125 {
   1126   if (output_bfd != NULL
   1127       && (symbol->flags & BSF_SECTION_SYM) == 0
   1128       && (! reloc_entry->howto->partial_inplace
   1129 	  || reloc_entry->addend == 0))
   1130     {
   1131       reloc_entry->address += input_section->output_offset;
   1132       return bfd_reloc_ok;
   1133     }
   1134 
   1135   /* In some cases the relocation should be treated as output section
   1136      relative, as when linking ELF DWARF into PE COFF.  Many ELF
   1137      targets lack section relative relocations and instead use
   1138      ordinary absolute relocations for references between DWARF
   1139      sections.  That is arguably a bug in those targets but it happens
   1140      to work for the usual case of linking to non-loaded ELF debug
   1141      sections with VMAs forced to zero.  PE COFF on the other hand
   1142      doesn't allow a section VMA of zero.  */
   1143   if (output_bfd == NULL
   1144       && !reloc_entry->howto->pc_relative
   1145       && (symbol->section->flags & SEC_DEBUGGING) != 0
   1146       && (input_section->flags & SEC_DEBUGGING) != 0)
   1147     reloc_entry->addend -= symbol->section->output_section->vma;
   1148 
   1149   return bfd_reloc_continue;
   1150 }
   1151 
   1152 /* Returns TRUE if section A matches section B.
   1154    Names, addresses and links may be different, but everything else
   1155    should be the same.  */
   1156 
   1157 static bool
   1158 section_match (const Elf_Internal_Shdr * a,
   1159 	       const Elf_Internal_Shdr * b)
   1160 {
   1161   if (a->sh_type != b->sh_type
   1162       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1163       || a->sh_addralign != b->sh_addralign
   1164       || a->sh_entsize != b->sh_entsize)
   1165     return false;
   1166   if (a->sh_type == SHT_SYMTAB
   1167       || a->sh_type == SHT_STRTAB)
   1168     return true;
   1169   return a->sh_size == b->sh_size;
   1170 }
   1171 
   1172 /* Find a section in OBFD that has the same characteristics
   1173    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1174    none can be found.  Check's section HINT first, as this is likely
   1175    to be the correct section.  */
   1176 
   1177 static unsigned int
   1178 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1179 	   const unsigned int hint)
   1180 {
   1181   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1182   unsigned int i;
   1183 
   1184   BFD_ASSERT (iheader != NULL);
   1185 
   1186   /* See PR 20922 for a reproducer of the NULL test.  */
   1187   if (hint < elf_numsections (obfd)
   1188       && oheaders[hint] != NULL
   1189       && section_match (oheaders[hint], iheader))
   1190     return hint;
   1191 
   1192   for (i = 1; i < elf_numsections (obfd); i++)
   1193     {
   1194       Elf_Internal_Shdr * oheader = oheaders[i];
   1195 
   1196       if (oheader == NULL)
   1197 	continue;
   1198       if (section_match (oheader, iheader))
   1199 	/* FIXME: Do we care if there is a potential for
   1200 	   multiple matches ?  */
   1201 	return i;
   1202     }
   1203 
   1204   return SHN_UNDEF;
   1205 }
   1206 
   1207 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1208    Processor specific section, based upon a matching input section.
   1209    Returns TRUE upon success, FALSE otherwise.  */
   1210 
   1211 static bool
   1212 copy_special_section_fields (const bfd *ibfd,
   1213 			     bfd *obfd,
   1214 			     const Elf_Internal_Shdr *iheader,
   1215 			     Elf_Internal_Shdr *oheader,
   1216 			     const unsigned int secnum)
   1217 {
   1218   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   1219   const Elf_Internal_Shdr **iheaders
   1220     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1221   bool changed = false;
   1222   unsigned int sh_link;
   1223 
   1224   if (oheader->sh_type == SHT_NOBITS)
   1225     {
   1226       /* This is a feature for objcopy --only-keep-debug:
   1227 	 When a section's type is changed to NOBITS, we preserve
   1228 	 the sh_link and sh_info fields so that they can be
   1229 	 matched up with the original.
   1230 
   1231 	 Note: Strictly speaking these assignments are wrong.
   1232 	 The sh_link and sh_info fields should point to the
   1233 	 relevent sections in the output BFD, which may not be in
   1234 	 the same location as they were in the input BFD.  But
   1235 	 the whole point of this action is to preserve the
   1236 	 original values of the sh_link and sh_info fields, so
   1237 	 that they can be matched up with the section headers in
   1238 	 the original file.  So strictly speaking we may be
   1239 	 creating an invalid ELF file, but it is only for a file
   1240 	 that just contains debug info and only for sections
   1241 	 without any contents.  */
   1242       if (oheader->sh_link == 0)
   1243 	oheader->sh_link = iheader->sh_link;
   1244       if (oheader->sh_info == 0)
   1245 	oheader->sh_info = iheader->sh_info;
   1246       return true;
   1247     }
   1248 
   1249   /* Allow the target a chance to decide how these fields should be set.  */
   1250   if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1251 						    iheader, oheader))
   1252     return true;
   1253 
   1254   /* We have an iheader which might match oheader, and which has non-zero
   1255      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1256      the section in the output bfd which corresponds to the linked section
   1257      in the input bfd.  */
   1258   if (iheader->sh_link != SHN_UNDEF)
   1259     {
   1260       /* See PR 20931 for a reproducer.  */
   1261       if (iheader->sh_link >= elf_numsections (ibfd))
   1262 	{
   1263 	  _bfd_error_handler
   1264 	    /* xgettext:c-format */
   1265 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1266 	     ibfd, iheader->sh_link, secnum);
   1267 	  return false;
   1268 	}
   1269 
   1270       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1271       if (sh_link != SHN_UNDEF)
   1272 	{
   1273 	  oheader->sh_link = sh_link;
   1274 	  changed = true;
   1275 	}
   1276       else
   1277 	/* FIXME: Should we install iheader->sh_link
   1278 	   if we could not find a match ?  */
   1279 	_bfd_error_handler
   1280 	  /* xgettext:c-format */
   1281 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1282     }
   1283 
   1284   if (iheader->sh_info)
   1285     {
   1286       /* The sh_info field can hold arbitrary information, but if the
   1287 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1288 	 section index.  */
   1289       if (iheader->sh_flags & SHF_INFO_LINK)
   1290 	{
   1291 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1292 			       iheader->sh_info);
   1293 	  if (sh_link != SHN_UNDEF)
   1294 	    oheader->sh_flags |= SHF_INFO_LINK;
   1295 	}
   1296       else
   1297 	/* No idea what it means - just copy it.  */
   1298 	sh_link = iheader->sh_info;
   1299 
   1300       if (sh_link != SHN_UNDEF)
   1301 	{
   1302 	  oheader->sh_info = sh_link;
   1303 	  changed = true;
   1304 	}
   1305       else
   1306 	_bfd_error_handler
   1307 	  /* xgettext:c-format */
   1308 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1309     }
   1310 
   1311   return changed;
   1312 }
   1313 
   1314 /* Copy the program header and other data from one object module to
   1315    another.  */
   1316 
   1317 bool
   1318 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1319 {
   1320   const Elf_Internal_Shdr **iheaders
   1321     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1322   Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
   1323   const struct elf_backend_data *bed;
   1324   unsigned int i;
   1325 
   1326   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   1327     || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   1328     return true;
   1329 
   1330   if (!elf_flags_init (obfd))
   1331     {
   1332       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1333       elf_flags_init (obfd) = true;
   1334     }
   1335 
   1336   elf_gp (obfd) = elf_gp (ibfd);
   1337 
   1338   /* Also copy the EI_OSABI field.  */
   1339   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1340     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1341 
   1342   /* If set, copy the EI_ABIVERSION field.  */
   1343   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1344     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1345       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1346 
   1347   /* Copy object attributes.  */
   1348   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1349 
   1350   if (iheaders == NULL || oheaders == NULL)
   1351     return true;
   1352 
   1353   bed = get_elf_backend_data (obfd);
   1354 
   1355   /* Possibly copy other fields in the section header.  */
   1356   for (i = 1; i < elf_numsections (obfd); i++)
   1357     {
   1358       unsigned int j;
   1359       Elf_Internal_Shdr * oheader = oheaders[i];
   1360 
   1361       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1362 	 because of a special case need for generating separate debug info
   1363 	 files.  See below for more details.  */
   1364       if (oheader == NULL
   1365 	  || (oheader->sh_type != SHT_NOBITS
   1366 	      && oheader->sh_type < SHT_LOOS))
   1367 	continue;
   1368 
   1369       /* Ignore empty sections, and sections whose
   1370 	 fields have already been initialised.  */
   1371       if (oheader->sh_size == 0
   1372 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1373 	continue;
   1374 
   1375       /* Scan for the matching section in the input bfd.
   1376 	 First we try for a direct mapping between the input and
   1377 	 output sections.  */
   1378       for (j = 1; j < elf_numsections (ibfd); j++)
   1379 	{
   1380 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1381 
   1382 	  if (iheader == NULL)
   1383 	    continue;
   1384 
   1385 	  if (oheader->bfd_section != NULL
   1386 	      && iheader->bfd_section != NULL
   1387 	      && iheader->bfd_section->output_section != NULL
   1388 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1389 	    {
   1390 	      /* We have found a connection from the input section to
   1391 		 the output section.  Attempt to copy the header fields.
   1392 		 If this fails then do not try any further sections -
   1393 		 there should only be a one-to-one mapping between
   1394 		 input and output.  */
   1395 	      if (!copy_special_section_fields (ibfd, obfd,
   1396 						iheader, oheader, i))
   1397 		j = elf_numsections (ibfd);
   1398 	      break;
   1399 	    }
   1400 	}
   1401 
   1402       if (j < elf_numsections (ibfd))
   1403 	continue;
   1404 
   1405       /* That failed.  So try to deduce the corresponding input section.
   1406 	 Unfortunately we cannot compare names as the output string table
   1407 	 is empty, so instead we check size, address and type.  */
   1408       for (j = 1; j < elf_numsections (ibfd); j++)
   1409 	{
   1410 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1411 
   1412 	  if (iheader == NULL)
   1413 	    continue;
   1414 
   1415 	  /* Try matching fields in the input section's header.
   1416 	     Since --only-keep-debug turns all non-debug sections into
   1417 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1418 	     input type.  */
   1419 	  if ((oheader->sh_type == SHT_NOBITS
   1420 	       || iheader->sh_type == oheader->sh_type)
   1421 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1422 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1423 	      && iheader->sh_addralign == oheader->sh_addralign
   1424 	      && iheader->sh_entsize == oheader->sh_entsize
   1425 	      && iheader->sh_size == oheader->sh_size
   1426 	      && iheader->sh_addr == oheader->sh_addr
   1427 	      && (iheader->sh_info != oheader->sh_info
   1428 		  || iheader->sh_link != oheader->sh_link))
   1429 	    {
   1430 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1431 		break;
   1432 	    }
   1433 	}
   1434 
   1435       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1436 	{
   1437 	  /* Final attempt.  Call the backend copy function
   1438 	     with a NULL input section.  */
   1439 	  (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1440 							       NULL, oheader);
   1441 	}
   1442     }
   1443 
   1444   return true;
   1445 }
   1446 
   1447 static const char *
   1448 get_segment_type (unsigned int p_type)
   1449 {
   1450   const char *pt;
   1451   switch (p_type)
   1452     {
   1453     case PT_NULL: pt = "NULL"; break;
   1454     case PT_LOAD: pt = "LOAD"; break;
   1455     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1456     case PT_INTERP: pt = "INTERP"; break;
   1457     case PT_NOTE: pt = "NOTE"; break;
   1458     case PT_SHLIB: pt = "SHLIB"; break;
   1459     case PT_PHDR: pt = "PHDR"; break;
   1460     case PT_TLS: pt = "TLS"; break;
   1461     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1462     case PT_GNU_STACK: pt = "STACK"; break;
   1463     case PT_GNU_RELRO: pt = "RELRO"; break;
   1464     case PT_GNU_SFRAME: pt = "SFRAME"; break;
   1465     default: pt = NULL; break;
   1466     }
   1467   return pt;
   1468 }
   1469 
   1470 /* Print out the program headers.  */
   1471 
   1472 bool
   1473 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1474 {
   1475   FILE *f = (FILE *) farg;
   1476   Elf_Internal_Phdr *p;
   1477   asection *s;
   1478   bfd_byte *dynbuf = NULL;
   1479 
   1480   p = elf_tdata (abfd)->phdr;
   1481   if (p != NULL)
   1482     {
   1483       unsigned int i, c;
   1484 
   1485       fprintf (f, _("\nProgram Header:\n"));
   1486       c = elf_elfheader (abfd)->e_phnum;
   1487       for (i = 0; i < c; i++, p++)
   1488 	{
   1489 	  const char *pt = get_segment_type (p->p_type);
   1490 	  char buf[20];
   1491 
   1492 	  if (pt == NULL)
   1493 	    {
   1494 	      sprintf (buf, "0x%lx", p->p_type);
   1495 	      pt = buf;
   1496 	    }
   1497 	  fprintf (f, "%8s off    0x", pt);
   1498 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1499 	  fprintf (f, " vaddr 0x");
   1500 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1501 	  fprintf (f, " paddr 0x");
   1502 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1503 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1504 	  fprintf (f, "         filesz 0x");
   1505 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1506 	  fprintf (f, " memsz 0x");
   1507 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1508 	  fprintf (f, " flags %c%c%c",
   1509 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1510 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1511 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1512 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1513 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1514 	  fprintf (f, "\n");
   1515 	}
   1516     }
   1517 
   1518   s = bfd_get_section_by_name (abfd, ".dynamic");
   1519   if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
   1520     {
   1521       unsigned int elfsec;
   1522       unsigned long shlink;
   1523       bfd_byte *extdyn, *extdynend;
   1524       size_t extdynsize;
   1525       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1526 
   1527       fprintf (f, _("\nDynamic Section:\n"));
   1528 
   1529       if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
   1530 	goto error_return;
   1531 
   1532       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1533       if (elfsec == SHN_BAD)
   1534 	goto error_return;
   1535       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1536 
   1537       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1538       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1539 
   1540       for (extdyn = dynbuf, extdynend = dynbuf + s->size;
   1541 	   (size_t) (extdynend - extdyn) >= extdynsize;
   1542 	   extdyn += extdynsize)
   1543 	{
   1544 	  Elf_Internal_Dyn dyn;
   1545 	  const char *name = "";
   1546 	  char ab[20];
   1547 	  bool stringp;
   1548 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   1549 
   1550 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1551 
   1552 	  if (dyn.d_tag == DT_NULL)
   1553 	    break;
   1554 
   1555 	  stringp = false;
   1556 	  switch (dyn.d_tag)
   1557 	    {
   1558 	    default:
   1559 	      if (bed->elf_backend_get_target_dtag)
   1560 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1561 
   1562 	      if (!strcmp (name, ""))
   1563 		{
   1564 		  sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
   1565 		  name = ab;
   1566 		}
   1567 	      break;
   1568 
   1569 	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
   1570 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1571 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1572 	    case DT_HASH: name = "HASH"; break;
   1573 	    case DT_STRTAB: name = "STRTAB"; break;
   1574 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1575 	    case DT_RELA: name = "RELA"; break;
   1576 	    case DT_RELASZ: name = "RELASZ"; break;
   1577 	    case DT_RELAENT: name = "RELAENT"; break;
   1578 	    case DT_STRSZ: name = "STRSZ"; break;
   1579 	    case DT_SYMENT: name = "SYMENT"; break;
   1580 	    case DT_INIT: name = "INIT"; break;
   1581 	    case DT_FINI: name = "FINI"; break;
   1582 	    case DT_SONAME: name = "SONAME"; stringp = true; break;
   1583 	    case DT_RPATH: name = "RPATH"; stringp = true; break;
   1584 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1585 	    case DT_REL: name = "REL"; break;
   1586 	    case DT_RELSZ: name = "RELSZ"; break;
   1587 	    case DT_RELENT: name = "RELENT"; break;
   1588 	    case DT_RELR: name = "RELR"; break;
   1589 	    case DT_RELRSZ: name = "RELRSZ"; break;
   1590 	    case DT_RELRENT: name = "RELRENT"; break;
   1591 	    case DT_PLTREL: name = "PLTREL"; break;
   1592 	    case DT_DEBUG: name = "DEBUG"; break;
   1593 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1594 	    case DT_JMPREL: name = "JMPREL"; break;
   1595 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1596 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1597 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1598 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1599 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1600 	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
   1601 	    case DT_FLAGS: name = "FLAGS"; break;
   1602 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1603 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1604 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1605 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1606 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1607 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1608 	    case DT_FEATURE: name = "FEATURE"; break;
   1609 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1610 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1611 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1612 	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
   1613 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
   1614 	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
   1615 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1616 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1617 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1618 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1619 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1620 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1621 	    case DT_VERSYM: name = "VERSYM"; break;
   1622 	    case DT_VERDEF: name = "VERDEF"; break;
   1623 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1624 	    case DT_VERNEED: name = "VERNEED"; break;
   1625 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1626 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
   1627 	    case DT_USED: name = "USED"; break;
   1628 	    case DT_FILTER: name = "FILTER"; stringp = true; break;
   1629 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1630 	    }
   1631 
   1632 	  fprintf (f, "  %-20s ", name);
   1633 	  if (! stringp)
   1634 	    {
   1635 	      fprintf (f, "0x");
   1636 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1637 	    }
   1638 	  else
   1639 	    {
   1640 	      const char *string;
   1641 	      unsigned int tagv = dyn.d_un.d_val;
   1642 
   1643 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1644 	      if (string == NULL)
   1645 		goto error_return;
   1646 	      fprintf (f, "%s", string);
   1647 	    }
   1648 	  fprintf (f, "\n");
   1649 	}
   1650 
   1651       _bfd_elf_munmap_section_contents (s, dynbuf);
   1652       dynbuf = NULL;
   1653     }
   1654 
   1655   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1656       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1657     {
   1658       if (! _bfd_elf_slurp_version_tables (abfd, false))
   1659 	return false;
   1660     }
   1661 
   1662   if (elf_dynverdef (abfd) != 0)
   1663     {
   1664       Elf_Internal_Verdef *t;
   1665 
   1666       fprintf (f, _("\nVersion definitions:\n"));
   1667       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1668 	{
   1669 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1670 		   t->vd_flags, t->vd_hash,
   1671 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1672 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1673 	    {
   1674 	      Elf_Internal_Verdaux *a;
   1675 
   1676 	      fprintf (f, "\t");
   1677 	      for (a = t->vd_auxptr->vda_nextptr;
   1678 		   a != NULL;
   1679 		   a = a->vda_nextptr)
   1680 		fprintf (f, "%s ",
   1681 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1682 	      fprintf (f, "\n");
   1683 	    }
   1684 	}
   1685     }
   1686 
   1687   if (elf_dynverref (abfd) != 0)
   1688     {
   1689       Elf_Internal_Verneed *t;
   1690 
   1691       fprintf (f, _("\nVersion References:\n"));
   1692       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1693 	{
   1694 	  Elf_Internal_Vernaux *a;
   1695 
   1696 	  fprintf (f, _("  required from %s:\n"),
   1697 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1698 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1699 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1700 		     a->vna_flags, a->vna_other,
   1701 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1702 	}
   1703     }
   1704 
   1705   return true;
   1706 
   1707  error_return:
   1708   _bfd_elf_munmap_section_contents (s, dynbuf);
   1709   return false;
   1710 }
   1711 
   1712 /* Find the file offset corresponding to VMA by using the program
   1713    headers.  */
   1714 
   1715 static file_ptr
   1716 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
   1717 		 size_t size, size_t *max_size_p)
   1718 {
   1719   Elf_Internal_Phdr *seg;
   1720   size_t i;
   1721 
   1722   for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
   1723     if (seg->p_type == PT_LOAD
   1724 	&& vma >= (seg->p_vaddr & -seg->p_align)
   1725 	&& vma + size <= seg->p_vaddr + seg->p_filesz)
   1726       {
   1727 	if (max_size_p)
   1728 	  *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
   1729 	return vma - seg->p_vaddr + seg->p_offset;
   1730       }
   1731 
   1732   if (max_size_p)
   1733     *max_size_p = 0;
   1734   bfd_set_error (bfd_error_invalid_operation);
   1735   return (file_ptr) -1;
   1736 }
   1737 
   1738 /* Convert hash table to internal form.  */
   1739 
   1740 static bfd_vma *
   1741 get_hash_table_data (bfd *abfd, bfd_size_type number,
   1742 		     unsigned int ent_size, bfd_size_type filesize)
   1743 {
   1744   unsigned char *e_data = NULL;
   1745   bfd_vma *i_data = NULL;
   1746   bfd_size_type size;
   1747   void *e_data_addr;
   1748   size_t e_data_size ATTRIBUTE_UNUSED;
   1749 
   1750   if (ent_size != 4 && ent_size != 8)
   1751     return NULL;
   1752 
   1753   if ((size_t) number != number)
   1754     {
   1755       bfd_set_error (bfd_error_file_too_big);
   1756       return NULL;
   1757     }
   1758 
   1759   size = ent_size * number;
   1760   /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
   1761      attempting to allocate memory when the read is bound to fail.  */
   1762   if (size > filesize
   1763       || number >= ~(size_t) 0 / ent_size
   1764       || number >= ~(size_t) 0 / sizeof (*i_data))
   1765     {
   1766       bfd_set_error (bfd_error_file_too_big);
   1767       return NULL;
   1768     }
   1769 
   1770   e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
   1771   if (e_data == NULL)
   1772     return NULL;
   1773 
   1774   i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
   1775   if (i_data == NULL)
   1776     {
   1777       _bfd_munmap_temporary (e_data_addr, e_data_size);
   1778       return NULL;
   1779     }
   1780 
   1781   if (ent_size == 4)
   1782     while (number--)
   1783       i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
   1784   else
   1785     while (number--)
   1786       i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
   1787 
   1788   _bfd_munmap_temporary (e_data_addr, e_data_size);
   1789   return i_data;
   1790 }
   1791 
   1792 /* Address of .MIPS.xhash section.  FIXME: What is the best way to
   1793    support DT_MIPS_XHASH?  */
   1794 #define DT_MIPS_XHASH	       0x70000036
   1795 
   1796 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
   1797 
   1798 bool
   1799 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
   1800 			      Elf_Internal_Phdr *phdrs, size_t phnum,
   1801 			      bfd_size_type filesize)
   1802 {
   1803   bfd_byte *extdyn, *extdynend;
   1804   size_t extdynsize;
   1805   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1806   bool (*swap_symbol_in) (bfd *, const void *, const void *,
   1807 			  Elf_Internal_Sym *);
   1808   Elf_Internal_Dyn dyn;
   1809   bfd_vma dt_hash = 0;
   1810   bfd_vma dt_gnu_hash = 0;
   1811   bfd_vma dt_mips_xhash = 0;
   1812   bfd_vma dt_strtab = 0;
   1813   bfd_vma dt_symtab = 0;
   1814   size_t dt_strsz = 0;
   1815   bfd_vma dt_versym = 0;
   1816   bfd_vma dt_verdef = 0;
   1817   bfd_vma dt_verneed = 0;
   1818   bfd_byte *dynbuf = NULL;
   1819   char *strbuf = NULL;
   1820   bfd_vma *gnubuckets = NULL;
   1821   bfd_vma *gnuchains = NULL;
   1822   bfd_vma *mipsxlat = NULL;
   1823   file_ptr saved_filepos, filepos;
   1824   bool res = false;
   1825   size_t amt;
   1826   bfd_byte *esymbuf = NULL, *esym;
   1827   bfd_size_type symcount;
   1828   Elf_Internal_Sym *isymbuf = NULL;
   1829   Elf_Internal_Sym *isym, *isymend;
   1830   bfd_byte *versym = NULL;
   1831   bfd_byte *verdef = NULL;
   1832   bfd_byte *verneed = NULL;
   1833   size_t verdef_size = 0;
   1834   size_t verneed_size = 0;
   1835   size_t extsym_size;
   1836   const struct elf_backend_data *bed;
   1837   void *dynbuf_addr = NULL;
   1838   void *esymbuf_addr = NULL;
   1839   size_t dynbuf_size = 0;
   1840   size_t esymbuf_size = 0;
   1841 
   1842   /* Return TRUE if symbol table is bad.  */
   1843   if (elf_bad_symtab (abfd))
   1844     return true;
   1845 
   1846   /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
   1847   if (elf_tdata (abfd)->dt_strtab != NULL)
   1848     return true;
   1849 
   1850   bed = get_elf_backend_data (abfd);
   1851 
   1852   /* Save file position for elf_object_p.  */
   1853   saved_filepos = bfd_tell (abfd);
   1854 
   1855   if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
   1856     goto error_return;
   1857 
   1858   dynbuf_size = phdr->p_filesz;
   1859   dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
   1860   if (dynbuf == NULL)
   1861     goto error_return;
   1862 
   1863   extsym_size = bed->s->sizeof_sym;
   1864   extdynsize = bed->s->sizeof_dyn;
   1865   swap_dyn_in = bed->s->swap_dyn_in;
   1866 
   1867   extdyn = dynbuf;
   1868   if (phdr->p_filesz < extdynsize)
   1869     goto error_return;
   1870   extdynend = extdyn + phdr->p_filesz;
   1871   for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   1872     {
   1873       swap_dyn_in (abfd, extdyn, &dyn);
   1874 
   1875       if (dyn.d_tag == DT_NULL)
   1876 	break;
   1877 
   1878       switch (dyn.d_tag)
   1879 	{
   1880 	case DT_HASH:
   1881 	  dt_hash = dyn.d_un.d_val;
   1882 	  break;
   1883 	case DT_GNU_HASH:
   1884 	  if (bed->elf_machine_code != EM_MIPS
   1885 	      && bed->elf_machine_code != EM_MIPS_RS3_LE)
   1886 	    dt_gnu_hash = dyn.d_un.d_val;
   1887 	  break;
   1888 	case DT_STRTAB:
   1889 	  dt_strtab = dyn.d_un.d_val;
   1890 	  break;
   1891 	case DT_SYMTAB:
   1892 	  dt_symtab = dyn.d_un.d_val;
   1893 	  break;
   1894 	case DT_STRSZ:
   1895 	  dt_strsz = dyn.d_un.d_val;
   1896 	  break;
   1897 	case DT_SYMENT:
   1898 	  if (dyn.d_un.d_val != extsym_size)
   1899 	    goto error_return;
   1900 	  break;
   1901 	case DT_VERSYM:
   1902 	  dt_versym = dyn.d_un.d_val;
   1903 	  break;
   1904 	case DT_VERDEF:
   1905 	  dt_verdef = dyn.d_un.d_val;
   1906 	  break;
   1907 	case DT_VERNEED:
   1908 	  dt_verneed = dyn.d_un.d_val;
   1909 	  break;
   1910 	default:
   1911 	  if (dyn.d_tag == DT_MIPS_XHASH
   1912 	      && (bed->elf_machine_code == EM_MIPS
   1913 		  || bed->elf_machine_code == EM_MIPS_RS3_LE))
   1914 	    {
   1915 	      dt_gnu_hash = dyn.d_un.d_val;
   1916 	      dt_mips_xhash = dyn.d_un.d_val;
   1917 	    }
   1918 	  break;
   1919 	}
   1920     }
   1921 
   1922   /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
   1923      segment.  */
   1924   if ((!dt_hash && !dt_gnu_hash)
   1925       || !dt_strtab
   1926       || !dt_symtab
   1927       || !dt_strsz)
   1928     goto error_return;
   1929 
   1930   /* Get dynamic string table.  */
   1931   filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
   1932   if (filepos == (file_ptr) -1
   1933       || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   1934     goto error_return;
   1935 
   1936   /* Dynamic string table must be valid until ABFD is closed.  */
   1937   strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
   1938   if (strbuf == NULL)
   1939     goto error_return;
   1940   if (strbuf[dt_strsz - 1] != 0)
   1941     {
   1942       /* It is an error if a string table is't terminated.  */
   1943       _bfd_error_handler
   1944 	/* xgettext:c-format */
   1945 	(_("%pB: DT_STRTAB table is corrupt"), abfd);
   1946       strbuf[dt_strsz - 1] = 0;
   1947     }
   1948 
   1949   /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
   1950      DT_HASH since it is simpler than DT_GNU_HASH.  */
   1951   if (dt_hash)
   1952     {
   1953       unsigned char nb[16];
   1954       unsigned int hash_ent_size;
   1955 
   1956       switch (bed->elf_machine_code)
   1957 	{
   1958 	case EM_ALPHA:
   1959 	case EM_S390:
   1960 	case EM_S390_OLD:
   1961 	  if (bed->s->elfclass == ELFCLASS64)
   1962 	    {
   1963 	      hash_ent_size = 8;
   1964 	      break;
   1965 	    }
   1966 	  /* FALLTHROUGH */
   1967 	default:
   1968 	  hash_ent_size = 4;
   1969 	  break;
   1970 	}
   1971 
   1972       filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
   1973 				 NULL);
   1974       if (filepos == (file_ptr) -1
   1975 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0
   1976 	  || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
   1977 	goto error_return;
   1978 
   1979       /* The number of dynamic symbol table entries equals the number
   1980 	 of chains.  */
   1981       if (hash_ent_size == 8)
   1982 	symcount = bfd_get_64 (abfd, nb + hash_ent_size);
   1983       else
   1984 	symcount = bfd_get_32 (abfd, nb + hash_ent_size);
   1985     }
   1986   else
   1987     {
   1988       /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
   1989 	 bindings are in hash table.  Since in dynamic symbol table,
   1990 	 all symbols with STB_LOCAL binding are placed before symbols
   1991 	 with other bindings and all undefined symbols are placed
   1992 	 before defined ones, the highest symbol index in DT_GNU_HASH
   1993 	 is the highest dynamic symbol table index.  */
   1994       unsigned char nb[16];
   1995       bfd_vma ngnubuckets;
   1996       bfd_vma gnusymidx;
   1997       size_t i, ngnuchains;
   1998       bfd_vma maxchain = 0xffffffff, bitmaskwords;
   1999       bfd_vma buckets_vma;
   2000 
   2001       filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
   2002 				 sizeof (nb), NULL);
   2003       if (filepos == (file_ptr) -1
   2004 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0
   2005 	  || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
   2006 	goto error_return;
   2007 
   2008       ngnubuckets = bfd_get_32 (abfd, nb);
   2009       gnusymidx = bfd_get_32 (abfd, nb + 4);
   2010       bitmaskwords = bfd_get_32 (abfd, nb + 8);
   2011       buckets_vma = dt_gnu_hash + 16;
   2012       if (bed->s->elfclass == ELFCLASS32)
   2013 	buckets_vma += bitmaskwords * 4;
   2014       else
   2015 	buckets_vma += bitmaskwords * 8;
   2016       filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
   2017       if (filepos == (file_ptr) -1
   2018 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2019 	goto error_return;
   2020 
   2021       gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
   2022       if (gnubuckets == NULL)
   2023 	goto error_return;
   2024 
   2025       for (i = 0; i < ngnubuckets; i++)
   2026 	if (gnubuckets[i] != 0)
   2027 	  {
   2028 	    if (gnubuckets[i] < gnusymidx)
   2029 	      goto error_return;
   2030 
   2031 	    if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
   2032 	      maxchain = gnubuckets[i];
   2033 	  }
   2034 
   2035       if (maxchain == 0xffffffff)
   2036 	{
   2037 	  symcount = 0;
   2038 	  goto empty_gnu_hash;
   2039 	}
   2040 
   2041       maxchain -= gnusymidx;
   2042       filepos = offset_from_vma (phdrs, phnum,
   2043 				 buckets_vma + 4 * (ngnubuckets + maxchain),
   2044 				 4, NULL);
   2045       if (filepos == (file_ptr) -1
   2046 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2047 	goto error_return;
   2048 
   2049       do
   2050 	{
   2051 	  if (bfd_read (nb, 4, abfd) != 4)
   2052 	    goto error_return;
   2053 	  ++maxchain;
   2054 	  if (maxchain == 0)
   2055 	    goto error_return;
   2056 	}
   2057       while ((bfd_get_32 (abfd, nb) & 1) == 0);
   2058 
   2059       filepos = offset_from_vma (phdrs, phnum,
   2060 				 buckets_vma + 4 * ngnubuckets,
   2061 				 4, NULL);
   2062       if (filepos == (file_ptr) -1
   2063 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2064 	goto error_return;
   2065 
   2066       gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
   2067       if (gnuchains == NULL)
   2068 	goto error_return;
   2069       ngnuchains = maxchain;
   2070 
   2071       if (dt_mips_xhash)
   2072 	{
   2073 	  filepos = offset_from_vma (phdrs, phnum,
   2074 				     buckets_vma + 4 * (ngnubuckets + maxchain),
   2075 				     4, NULL);
   2076 	  if (filepos == (file_ptr) -1
   2077 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2078 	    goto error_return;
   2079 
   2080 	  mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
   2081 	  if (mipsxlat == NULL)
   2082 	    goto error_return;
   2083 	}
   2084 
   2085       symcount = 0;
   2086       for (i = 0; i < ngnubuckets; ++i)
   2087 	if (gnubuckets[i] != 0)
   2088 	  {
   2089 	    bfd_vma si = gnubuckets[i];
   2090 	    bfd_vma off = si - gnusymidx;
   2091 	    do
   2092 	      {
   2093 		if (mipsxlat)
   2094 		  {
   2095 		    if (mipsxlat[off] >= symcount)
   2096 		      symcount = mipsxlat[off] + 1;
   2097 		  }
   2098 		else
   2099 		  {
   2100 		    if (si >= symcount)
   2101 		      symcount = si + 1;
   2102 		  }
   2103 		si++;
   2104 	      }
   2105 	    while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
   2106 	  }
   2107     }
   2108 
   2109   /* Swap in dynamic symbol table.  */
   2110   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
   2111     {
   2112       bfd_set_error (bfd_error_file_too_big);
   2113       goto error_return;
   2114     }
   2115 
   2116   filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
   2117   if (filepos == (file_ptr) -1
   2118       || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2119     goto error_return;
   2120   esymbuf_size = amt;
   2121   esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
   2122 				 &esymbuf_addr, &esymbuf_size);
   2123   if (esymbuf == NULL)
   2124     goto error_return;
   2125 
   2126   if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
   2127     {
   2128       bfd_set_error (bfd_error_file_too_big);
   2129       goto error_return;
   2130     }
   2131 
   2132   /* Dynamic symbol table must be valid until ABFD is closed.  */
   2133   isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
   2134   if (isymbuf == NULL)
   2135     goto error_return;
   2136 
   2137   swap_symbol_in = bed->s->swap_symbol_in;
   2138 
   2139   /* Convert the symbols to internal form.  */
   2140   isymend = isymbuf + symcount;
   2141   for (esym = esymbuf, isym = isymbuf;
   2142        isym < isymend;
   2143        esym += extsym_size, isym++)
   2144     if (!swap_symbol_in (abfd, esym, NULL, isym)
   2145 	|| isym->st_name >= dt_strsz)
   2146       {
   2147 	bfd_set_error (bfd_error_invalid_operation);
   2148 	goto error_return;
   2149       }
   2150 
   2151   if (dt_versym)
   2152     {
   2153       /* Swap in DT_VERSYM.  */
   2154       if (_bfd_mul_overflow (symcount, 2, &amt))
   2155 	{
   2156 	  bfd_set_error (bfd_error_file_too_big);
   2157 	  goto error_return;
   2158 	}
   2159 
   2160       filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
   2161       if (filepos == (file_ptr) -1
   2162 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2163 	goto error_return;
   2164 
   2165       /* DT_VERSYM info must be valid until ABFD is closed.  */
   2166       versym = _bfd_mmap_persistent (abfd, amt);
   2167 
   2168       if (dt_verdef)
   2169 	{
   2170 	  /* Read in DT_VERDEF.  */
   2171 	  filepos = offset_from_vma (phdrs, phnum, dt_verdef,
   2172 				     0, &verdef_size);
   2173 	  if (filepos == (file_ptr) -1
   2174 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2175 	    goto error_return;
   2176 
   2177 	  /* DT_VERDEF info must be valid until ABFD is closed.  */
   2178 	  verdef = _bfd_mmap_persistent (abfd, verdef_size);
   2179 	}
   2180 
   2181       if (dt_verneed)
   2182 	{
   2183 	  /* Read in DT_VERNEED.  */
   2184 	  filepos = offset_from_vma (phdrs, phnum, dt_verneed,
   2185 				     0, &verneed_size);
   2186 	  if (filepos == (file_ptr) -1
   2187 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2188 	    goto error_return;
   2189 
   2190 	  /* DT_VERNEED info must be valid until ABFD is closed.  */
   2191 	  verneed = _bfd_mmap_persistent (abfd, verneed_size);
   2192 	}
   2193     }
   2194 
   2195  empty_gnu_hash:
   2196   elf_tdata (abfd)->dt_strtab = strbuf;
   2197   elf_tdata (abfd)->dt_strsz = dt_strsz;
   2198   elf_tdata (abfd)->dt_symtab = isymbuf;
   2199   elf_tdata (abfd)->dt_symtab_count = symcount;
   2200   elf_tdata (abfd)->dt_versym = versym;
   2201   elf_tdata (abfd)->dt_verdef = verdef;
   2202   elf_tdata (abfd)->dt_verneed = verneed;
   2203   elf_tdata (abfd)->dt_verdef_count
   2204     = verdef_size / sizeof (Elf_External_Verdef);
   2205   elf_tdata (abfd)->dt_verneed_count
   2206     = verneed_size / sizeof (Elf_External_Verneed);
   2207 
   2208   res = true;
   2209 
   2210  error_return:
   2211   /* Restore file position for elf_object_p.  */
   2212   if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
   2213     res = false;
   2214   _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
   2215   _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
   2216   free (gnubuckets);
   2217   free (gnuchains);
   2218   free (mipsxlat);
   2219   return res;
   2220 }
   2221 
   2222 /* Reconstruct section from dynamic symbol.  */
   2223 
   2224 asection *
   2225 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
   2226 					  Elf_Internal_Sym *isym)
   2227 {
   2228   asection *sec;
   2229   flagword flags;
   2230 
   2231   if (!elf_use_dt_symtab_p (abfd))
   2232     return NULL;
   2233 
   2234   flags = SEC_ALLOC | SEC_LOAD;
   2235   switch (ELF_ST_TYPE (isym->st_info))
   2236     {
   2237     case STT_FUNC:
   2238     case STT_GNU_IFUNC:
   2239       sec = bfd_get_section_by_name (abfd, ".text");
   2240       if (sec == NULL)
   2241 	sec = bfd_make_section_with_flags (abfd,
   2242 					   ".text",
   2243 					   flags | SEC_CODE);
   2244       break;
   2245     case STT_COMMON:
   2246       sec = bfd_com_section_ptr;
   2247       break;
   2248     case STT_OBJECT:
   2249       sec = bfd_get_section_by_name (abfd, ".data");
   2250       if (sec == NULL)
   2251 	sec = bfd_make_section_with_flags (abfd,
   2252 					   ".data",
   2253 					   flags | SEC_DATA);
   2254       break;
   2255     case STT_TLS:
   2256       sec = bfd_get_section_by_name (abfd, ".tdata");
   2257       if (sec == NULL)
   2258 	sec = bfd_make_section_with_flags (abfd,
   2259 					   ".tdata",
   2260 					   (flags
   2261 					    | SEC_DATA
   2262 					    | SEC_THREAD_LOCAL));
   2263       break;
   2264     default:
   2265       sec = bfd_abs_section_ptr;
   2266       break;
   2267     }
   2268 
   2269   return sec;
   2270 }
   2271 
   2272 /* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
   2273    and return symbol version for symbol version itself.   */
   2274 
   2275 const char *
   2276 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   2277 				    bool base_p,
   2278 				    bool *hidden)
   2279 {
   2280   const char *version_string = NULL;
   2281   if ((elf_dynversym (abfd) != 0
   2282        && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   2283       || (elf_tdata (abfd)->dt_versym != NULL
   2284 	  && (elf_tdata (abfd)->dt_verdef != NULL
   2285 	      || elf_tdata (abfd)->dt_verneed != NULL)))
   2286     {
   2287       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   2288 
   2289       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   2290       vernum &= VERSYM_VERSION;
   2291 
   2292       if (vernum == 0)
   2293 	version_string = "";
   2294       else if (vernum == 1
   2295 	       && (vernum > elf_tdata (abfd)->cverdefs
   2296 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   2297 		       == VER_FLG_BASE)))
   2298 	version_string = base_p ? "Base" : "";
   2299       else if (vernum <= elf_tdata (abfd)->cverdefs)
   2300 	{
   2301 	  const char *nodename
   2302 	    = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   2303 	  version_string = "";
   2304 	  if (base_p
   2305 	      || nodename == NULL
   2306 	      || symbol->name == NULL
   2307 	      || strcmp (symbol->name, nodename) != 0)
   2308 	    version_string = nodename;
   2309 	}
   2310       else
   2311 	{
   2312 	  Elf_Internal_Verneed *t;
   2313 
   2314 	  version_string = _("<corrupt>");
   2315 	  for (t = elf_tdata (abfd)->verref;
   2316 	       t != NULL;
   2317 	       t = t->vn_nextref)
   2318 	    {
   2319 	      Elf_Internal_Vernaux *a;
   2320 
   2321 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2322 		{
   2323 		  if (a->vna_other == vernum)
   2324 		    {
   2325 		      *hidden = true;
   2326 		      version_string = a->vna_nodename;
   2327 		      break;
   2328 		    }
   2329 		}
   2330 	    }
   2331 	}
   2332     }
   2333   return version_string;
   2334 }
   2335 
   2336 /* Display ELF-specific fields of a symbol.  */
   2337 
   2338 void
   2339 bfd_elf_print_symbol (bfd *abfd,
   2340 		      void *filep,
   2341 		      asymbol *symbol,
   2342 		      bfd_print_symbol_type how)
   2343 {
   2344   FILE *file = (FILE *) filep;
   2345   const char *symname = (symbol->name != bfd_symbol_error_name
   2346 			 ? symbol->name : _("<corrupt>"));
   2347 
   2348   switch (how)
   2349     {
   2350     case bfd_print_symbol_name:
   2351       fprintf (file, "%s", symname);
   2352       break;
   2353     case bfd_print_symbol_more:
   2354       fprintf (file, "elf ");
   2355       bfd_fprintf_vma (abfd, file, symbol->value);
   2356       fprintf (file, " %x", symbol->flags);
   2357       break;
   2358     case bfd_print_symbol_all:
   2359       {
   2360 	const char *section_name;
   2361 	const char *name = NULL;
   2362 	const struct elf_backend_data *bed;
   2363 	unsigned char st_other;
   2364 	bfd_vma val;
   2365 	const char *version_string;
   2366 	bool hidden;
   2367 
   2368 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   2369 
   2370 	bed = get_elf_backend_data (abfd);
   2371 	if (bed->elf_backend_print_symbol_all)
   2372 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   2373 
   2374 	if (name != NULL)
   2375 	  symname = name;
   2376 	else
   2377 	  bfd_print_symbol_vandf (abfd, file, symbol);
   2378 
   2379 	fprintf (file, " %s\t", section_name);
   2380 	/* Print the "other" value for a symbol.  For common symbols,
   2381 	   we've already printed the size; now print the alignment.
   2382 	   For other symbols, we have no specified alignment, and
   2383 	   we've printed the address; now print the size.  */
   2384 	if (symbol->section && bfd_is_com_section (symbol->section))
   2385 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   2386 	else
   2387 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   2388 	bfd_fprintf_vma (abfd, file, val);
   2389 
   2390 	/* If we have version information, print it.  */
   2391 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2392 							     symbol,
   2393 							     true,
   2394 							     &hidden);
   2395 	if (version_string)
   2396 	  {
   2397 	    if (!hidden)
   2398 	      fprintf (file, "  %-11s", version_string);
   2399 	    else
   2400 	      {
   2401 		int i;
   2402 
   2403 		fprintf (file, " (%s)", version_string);
   2404 		for (i = 10 - strlen (version_string); i > 0; --i)
   2405 		  putc (' ', file);
   2406 	      }
   2407 	  }
   2408 
   2409 	/* If the st_other field is not zero, print it.  */
   2410 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2411 
   2412 	switch (st_other)
   2413 	  {
   2414 	  case 0: break;
   2415 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2416 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2417 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2418 	  default:
   2419 	    /* Some other non-defined flags are also present, so print
   2420 	       everything hex.  */
   2421 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2422 	  }
   2423 
   2424 	fprintf (file, " %s", symname);
   2425       }
   2426       break;
   2427     }
   2428 }
   2429 
   2430 /* ELF .o/exec file reading */
   2432 
   2433 /* Create a new bfd section from an ELF section header.  */
   2434 
   2435 bool
   2436 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2437 {
   2438   Elf_Internal_Shdr *hdr;
   2439   Elf_Internal_Ehdr *ehdr;
   2440   const struct elf_backend_data *bed;
   2441   const char *name;
   2442   bool ret = true;
   2443 
   2444   if (shindex >= elf_numsections (abfd))
   2445     return false;
   2446 
   2447   /* PR17512: A corrupt ELF binary might contain a loop of sections via
   2448      sh_link or sh_info.  Detect this here, by refusing to load a
   2449      section that we are already in the process of loading.  */
   2450   if (elf_tdata (abfd)->being_created[shindex])
   2451     {
   2452       _bfd_error_handler
   2453 	(_("%pB: warning: loop in section dependencies detected"), abfd);
   2454       return false;
   2455     }
   2456   elf_tdata (abfd)->being_created[shindex] = true;
   2457 
   2458   hdr = elf_elfsections (abfd)[shindex];
   2459   ehdr = elf_elfheader (abfd);
   2460   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2461 					  hdr->sh_name);
   2462   if (name == NULL)
   2463     goto fail;
   2464 
   2465   bed = get_elf_backend_data (abfd);
   2466   switch (hdr->sh_type)
   2467     {
   2468     case SHT_NULL:
   2469       /* Inactive section. Throw it away.  */
   2470       goto success;
   2471 
   2472     case SHT_PROGBITS:		/* Normal section with contents.  */
   2473     case SHT_NOBITS:		/* .bss section.  */
   2474     case SHT_HASH:		/* .hash section.  */
   2475     case SHT_NOTE:		/* .note section.  */
   2476     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2477     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2478     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2479     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2480     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2481     case SHT_GNU_SFRAME:	/* .sframe section.  */
   2482       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2483       goto success;
   2484 
   2485     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2486       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2487 	goto fail;
   2488 
   2489       if (hdr->sh_link > elf_numsections (abfd))
   2490 	{
   2491 	  /* PR 10478: Accept Solaris binaries with a sh_link field
   2492 	     set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
   2493 	  switch (bfd_get_arch (abfd))
   2494 	    {
   2495 	    case bfd_arch_i386:
   2496 	    case bfd_arch_sparc:
   2497 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
   2498 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
   2499 		break;
   2500 	      /* Otherwise fall through.  */
   2501 	    default:
   2502 	      goto fail;
   2503 	    }
   2504 	}
   2505       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2506 	goto fail;
   2507       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2508 	{
   2509 	  Elf_Internal_Shdr *dynsymhdr;
   2510 
   2511 	  /* The shared libraries distributed with hpux11 have a bogus
   2512 	     sh_link field for the ".dynamic" section.  Find the
   2513 	     string table for the ".dynsym" section instead.  */
   2514 	  if (elf_dynsymtab (abfd) != 0)
   2515 	    {
   2516 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2517 	      hdr->sh_link = dynsymhdr->sh_link;
   2518 	    }
   2519 	  else
   2520 	    {
   2521 	      unsigned int i, num_sec;
   2522 
   2523 	      num_sec = elf_numsections (abfd);
   2524 	      for (i = 1; i < num_sec; i++)
   2525 		{
   2526 		  dynsymhdr = elf_elfsections (abfd)[i];
   2527 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2528 		    {
   2529 		      hdr->sh_link = dynsymhdr->sh_link;
   2530 		      break;
   2531 		    }
   2532 		}
   2533 	    }
   2534 	}
   2535       goto success;
   2536 
   2537     case SHT_SYMTAB:		/* A symbol table.  */
   2538       if (elf_onesymtab (abfd) == shindex)
   2539 	goto success;
   2540 
   2541       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2542 	goto fail;
   2543 
   2544       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2545 	{
   2546 	  if (hdr->sh_size != 0)
   2547 	    goto fail;
   2548 	  /* Some assemblers erroneously set sh_info to one with a
   2549 	     zero sh_size.  ld sees this as a global symbol count
   2550 	     of (unsigned) -1.  Fix it here.  */
   2551 	  hdr->sh_info = 0;
   2552 	  goto success;
   2553 	}
   2554 
   2555       /* PR 18854: A binary might contain more than one symbol table.
   2556 	 Unusual, but possible.  Warn, but continue.  */
   2557       if (elf_onesymtab (abfd) != 0)
   2558 	{
   2559 	  _bfd_error_handler
   2560 	    /* xgettext:c-format */
   2561 	    (_("%pB: warning: multiple symbol tables detected"
   2562 	       " - ignoring the table in section %u"),
   2563 	     abfd, shindex);
   2564 	  goto success;
   2565 	}
   2566       elf_onesymtab (abfd) = shindex;
   2567       elf_symtab_hdr (abfd) = *hdr;
   2568       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2569       abfd->flags |= HAS_SYMS;
   2570 
   2571       /* Sometimes a shared object will map in the symbol table.  If
   2572 	 SHF_ALLOC is set, and this is a shared object, then we also
   2573 	 treat this section as a BFD section.  We can not base the
   2574 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2575 	 set in a relocatable object file, which would confuse the
   2576 	 linker.  */
   2577       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2578 	  && (abfd->flags & DYNAMIC) != 0
   2579 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2580 						shindex))
   2581 	goto fail;
   2582 
   2583       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2584 	 can't read symbols without that section loaded as well.  It
   2585 	 is most likely specified by the next section header.  */
   2586       {
   2587 	elf_section_list * entry;
   2588 	unsigned int i, num_sec;
   2589 
   2590 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2591 	  if (entry->hdr.sh_link == shindex)
   2592 	    goto success;
   2593 
   2594 	num_sec = elf_numsections (abfd);
   2595 	for (i = shindex + 1; i < num_sec; i++)
   2596 	  {
   2597 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2598 
   2599 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2600 		&& hdr2->sh_link == shindex)
   2601 	      break;
   2602 	  }
   2603 
   2604 	if (i == num_sec)
   2605 	  for (i = 1; i < shindex; i++)
   2606 	    {
   2607 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2608 
   2609 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2610 		  && hdr2->sh_link == shindex)
   2611 		break;
   2612 	    }
   2613 
   2614 	if (i != shindex)
   2615 	  ret = bfd_section_from_shdr (abfd, i);
   2616 	/* else FIXME: we have failed to find the symbol table.
   2617 	   Should we issue an error?  */
   2618 	goto success;
   2619       }
   2620 
   2621     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2622       if (elf_dynsymtab (abfd) == shindex)
   2623 	goto success;
   2624 
   2625       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2626 	goto fail;
   2627 
   2628       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2629 	{
   2630 	  if (hdr->sh_size != 0)
   2631 	    goto fail;
   2632 
   2633 	  /* Some linkers erroneously set sh_info to one with a
   2634 	     zero sh_size.  ld sees this as a global symbol count
   2635 	     of (unsigned) -1.  Fix it here.  */
   2636 	  hdr->sh_info = 0;
   2637 	  goto success;
   2638 	}
   2639 
   2640       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2641 	 Unusual, but possible.  Warn, but continue.  */
   2642       if (elf_dynsymtab (abfd) != 0)
   2643 	{
   2644 	  _bfd_error_handler
   2645 	    /* xgettext:c-format */
   2646 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2647 	       " - ignoring the table in section %u"),
   2648 	     abfd, shindex);
   2649 	  goto success;
   2650 	}
   2651       elf_dynsymtab (abfd) = shindex;
   2652       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2653       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2654       abfd->flags |= HAS_SYMS;
   2655 
   2656       /* Besides being a symbol table, we also treat this as a regular
   2657 	 section, so that objcopy can handle it.  */
   2658       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2659       goto success;
   2660 
   2661     case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
   2662       {
   2663 	elf_section_list * entry;
   2664 
   2665 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2666 	  if (entry->ndx == shindex)
   2667 	    goto success;
   2668 
   2669 	entry = bfd_alloc (abfd, sizeof (*entry));
   2670 	if (entry == NULL)
   2671 	  goto fail;
   2672 	entry->ndx = shindex;
   2673 	entry->hdr = * hdr;
   2674 	entry->next = elf_symtab_shndx_list (abfd);
   2675 	elf_symtab_shndx_list (abfd) = entry;
   2676 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2677 	goto success;
   2678       }
   2679 
   2680     case SHT_STRTAB:		/* A string table.  */
   2681       if (hdr->bfd_section != NULL)
   2682 	goto success;
   2683 
   2684       if (ehdr->e_shstrndx == shindex)
   2685 	{
   2686 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2687 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2688 	  goto success;
   2689 	}
   2690 
   2691       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2692 	{
   2693 	symtab_strtab:
   2694 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2695 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2696 	  goto success;
   2697 	}
   2698 
   2699       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2700 	{
   2701 	dynsymtab_strtab:
   2702 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2703 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2704 	  elf_elfsections (abfd)[shindex] = hdr;
   2705 	  /* We also treat this as a regular section, so that objcopy
   2706 	     can handle it.  */
   2707 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2708 						 shindex);
   2709 	  goto success;
   2710 	}
   2711 
   2712       /* If the string table isn't one of the above, then treat it as a
   2713 	 regular section.  We need to scan all the headers to be sure,
   2714 	 just in case this strtab section appeared before the above.  */
   2715       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2716 	{
   2717 	  unsigned int i, num_sec;
   2718 
   2719 	  num_sec = elf_numsections (abfd);
   2720 	  for (i = 1; i < num_sec; i++)
   2721 	    {
   2722 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2723 	      if (hdr2->sh_link == shindex)
   2724 		{
   2725 		  /* Prevent endless recursion on broken objects.  */
   2726 		  if (i == shindex)
   2727 		    goto fail;
   2728 		  if (! bfd_section_from_shdr (abfd, i))
   2729 		    goto fail;
   2730 		  if (elf_onesymtab (abfd) == i)
   2731 		    goto symtab_strtab;
   2732 		  if (elf_dynsymtab (abfd) == i)
   2733 		    goto dynsymtab_strtab;
   2734 		}
   2735 	    }
   2736 	}
   2737       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2738       goto success;
   2739 
   2740     case SHT_REL:
   2741     case SHT_RELA:
   2742     case SHT_RELR:
   2743       /* *These* do a lot of work -- but build no sections!  */
   2744       {
   2745 	asection *target_sect;
   2746 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2747 	unsigned int num_sec = elf_numsections (abfd);
   2748 	struct bfd_elf_section_data *esdt;
   2749 	bfd_size_type size;
   2750 
   2751 	if (hdr->sh_type == SHT_REL)
   2752 	  size = bed->s->sizeof_rel;
   2753 	else if (hdr->sh_type == SHT_RELA)
   2754 	  size = bed->s->sizeof_rela;
   2755 	else
   2756 	  size = bed->s->arch_size / 8;
   2757 	if (hdr->sh_entsize != size)
   2758 	  goto fail;
   2759 
   2760 	/* Check for a bogus link to avoid crashing.  */
   2761 	if (hdr->sh_link >= num_sec)
   2762 	  {
   2763 	    _bfd_error_handler
   2764 	      /* xgettext:c-format */
   2765 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2766 	       abfd, hdr->sh_link, name, shindex);
   2767 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2768 	    goto success;
   2769 	  }
   2770 
   2771 	/* Get the symbol table.  */
   2772 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2773 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2774 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2775 	  goto fail;
   2776 
   2777 	/* If this is an alloc section in an executable or shared
   2778 	   library, or the reloc section does not use the main symbol
   2779 	   table we don't treat it as a reloc section.  BFD can't
   2780 	   adequately represent such a section, so at least for now,
   2781 	   we don't try.  We just present it as a normal section.  We
   2782 	   also can't use it as a reloc section if it points to the
   2783 	   null section, an invalid section, another reloc section, or
   2784 	   its sh_link points to the null section.  */
   2785 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2786 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2787 	    || (hdr->sh_flags & SHF_COMPRESSED) != 0
   2788 	    || hdr->sh_type == SHT_RELR
   2789 	    || hdr->sh_link == SHN_UNDEF
   2790 	    || hdr->sh_link != elf_onesymtab (abfd)
   2791 	    || hdr->sh_info == SHN_UNDEF
   2792 	    || hdr->sh_info >= num_sec
   2793 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2794 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2795 	  {
   2796 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2797 	    goto success;
   2798 	  }
   2799 
   2800 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2801 	  goto fail;
   2802 
   2803 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2804 	if (target_sect == NULL)
   2805 	  goto fail;
   2806 
   2807 	esdt = elf_section_data (target_sect);
   2808 	if (hdr->sh_type == SHT_RELA)
   2809 	  p_hdr = &esdt->rela.hdr;
   2810 	else
   2811 	  p_hdr = &esdt->rel.hdr;
   2812 
   2813 	/* PR 17512: file: 0b4f81b7.
   2814 	   Also see PR 24456, for a file which deliberately has two reloc
   2815 	   sections.  */
   2816 	if (*p_hdr != NULL)
   2817 	  {
   2818 	    if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
   2819 	      {
   2820 		_bfd_error_handler
   2821 		  /* xgettext:c-format */
   2822 		  (_("%pB: warning: secondary relocation section '%s' "
   2823 		     "for section %pA found - ignoring"),
   2824 		   abfd, name, target_sect);
   2825 	      }
   2826 	    else
   2827 	      esdt->has_secondary_relocs = true;
   2828 	    goto success;
   2829 	  }
   2830 
   2831 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2832 	if (hdr2 == NULL)
   2833 	  goto fail;
   2834 	*hdr2 = *hdr;
   2835 	*p_hdr = hdr2;
   2836 	elf_elfsections (abfd)[shindex] = hdr2;
   2837 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2838 				     * bed->s->int_rels_per_ext_rel);
   2839 	target_sect->flags |= SEC_RELOC;
   2840 	target_sect->relocation = NULL;
   2841 	target_sect->rel_filepos = hdr->sh_offset;
   2842 	/* In the section to which the relocations apply, mark whether
   2843 	   its relocations are of the REL or RELA variety.  */
   2844 	if (hdr->sh_size != 0)
   2845 	  {
   2846 	    if (hdr->sh_type == SHT_RELA)
   2847 	      target_sect->use_rela_p = 1;
   2848 	  }
   2849 	abfd->flags |= HAS_RELOC;
   2850 	goto success;
   2851       }
   2852 
   2853     case SHT_GNU_verdef:
   2854       if (hdr->sh_info != 0)
   2855 	elf_dynverdef (abfd) = shindex;
   2856       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2857       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2858       goto success;
   2859 
   2860     case SHT_GNU_versym:
   2861       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2862 	goto fail;
   2863 
   2864       elf_dynversym (abfd) = shindex;
   2865       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2866       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2867       goto success;
   2868 
   2869     case SHT_GNU_verneed:
   2870       if (hdr->sh_info != 0)
   2871 	elf_dynverref (abfd) = shindex;
   2872       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2873       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2874       goto success;
   2875 
   2876     case SHT_SHLIB:
   2877       goto success;
   2878 
   2879     case SHT_GROUP:
   2880       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2881 	goto fail;
   2882 
   2883       goto success;
   2884 
   2885     default:
   2886       /* Possibly an attributes section.  */
   2887       if (get_elf_backend_data (abfd)->target_os != is_solaris
   2888 	  /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES.  */
   2889 	  && (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2890 	      || hdr->sh_type == bed->obj_attrs_section_type))
   2891 	{
   2892 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2893 	    goto fail;
   2894 	  _bfd_elf_parse_attributes (abfd, hdr);
   2895 	  goto success;
   2896 	}
   2897 
   2898       /* Check for any processor-specific section types.  */
   2899       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2900 	goto success;
   2901 
   2902       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2903 	{
   2904 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2905 	    /* FIXME: How to properly handle allocated section reserved
   2906 	       for applications?  */
   2907 	    _bfd_error_handler
   2908 	      /* xgettext:c-format */
   2909 	      (_("%pB: unknown type [%#x] section `%s'"),
   2910 	       abfd, hdr->sh_type, name);
   2911 	  else
   2912 	    {
   2913 	      /* Allow sections reserved for applications.  */
   2914 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2915 	      goto success;
   2916 	    }
   2917 	}
   2918       else if (hdr->sh_type >= SHT_LOPROC
   2919 	       && hdr->sh_type <= SHT_HIPROC)
   2920 	/* FIXME: We should handle this section.  */
   2921 	_bfd_error_handler
   2922 	  /* xgettext:c-format */
   2923 	  (_("%pB: unknown type [%#x] section `%s'"),
   2924 	   abfd, hdr->sh_type, name);
   2925       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2926 	{
   2927 	  /* Unrecognised OS-specific sections.  */
   2928 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2929 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2930 	       required to correctly process the section and the file should
   2931 	       be rejected with an error message.  */
   2932 	    _bfd_error_handler
   2933 	      /* xgettext:c-format */
   2934 	      (_("%pB: unknown type [%#x] section `%s'"),
   2935 	       abfd, hdr->sh_type, name);
   2936 	  else
   2937 	    {
   2938 	      /* Otherwise it should be processed.  */
   2939 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2940 	      goto success;
   2941 	    }
   2942 	}
   2943       else
   2944 	/* FIXME: We should handle this section.  */
   2945 	_bfd_error_handler
   2946 	  /* xgettext:c-format */
   2947 	  (_("%pB: unknown type [%#x] section `%s'"),
   2948 	   abfd, hdr->sh_type, name);
   2949 
   2950       goto fail;
   2951     }
   2952 
   2953  fail:
   2954   ret = false;
   2955  success:
   2956   elf_tdata (abfd)->being_created[shindex] = false;
   2957   return ret;
   2958 }
   2959 
   2960 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   2961 
   2962 Elf_Internal_Sym *
   2963 bfd_sym_from_r_symndx (struct sym_cache *cache,
   2964 		       bfd *abfd,
   2965 		       unsigned long r_symndx)
   2966 {
   2967   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   2968 
   2969   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   2970     {
   2971       Elf_Internal_Shdr *symtab_hdr;
   2972       unsigned char esym[sizeof (Elf64_External_Sym)];
   2973       Elf_External_Sym_Shndx eshndx;
   2974 
   2975       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2976       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   2977 				&cache->sym[ent], esym, &eshndx) == NULL)
   2978 	return NULL;
   2979 
   2980       if (cache->abfd != abfd)
   2981 	{
   2982 	  memset (cache->indx, -1, sizeof (cache->indx));
   2983 	  cache->abfd = abfd;
   2984 	}
   2985       cache->indx[ent] = r_symndx;
   2986     }
   2987 
   2988   return &cache->sym[ent];
   2989 }
   2990 
   2991 /* Given an ELF section number, retrieve the corresponding BFD
   2992    section.  */
   2993 
   2994 asection *
   2995 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   2996 {
   2997   if (sec_index >= elf_numsections (abfd))
   2998     return NULL;
   2999   return elf_elfsections (abfd)[sec_index]->bfd_section;
   3000 }
   3001 
   3002 static const struct bfd_elf_special_section special_sections_b[] =
   3003 {
   3004   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3005   { NULL,		    0,	0, 0,		 0 }
   3006 };
   3007 
   3008 static const struct bfd_elf_special_section special_sections_c[] =
   3009 {
   3010   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   3011   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   3012   { NULL,			0, 0, 0,	    0 }
   3013 };
   3014 
   3015 static const struct bfd_elf_special_section special_sections_d[] =
   3016 {
   3017   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3018   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3019   /* There are more DWARF sections than these, but they needn't be added here
   3020      unless you have to cope with broken compilers that don't emit section
   3021      attributes or you want to help the user writing assembler.  */
   3022   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   3023   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   3024   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   3025   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   3026   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   3027   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   3028   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   3029   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   3030   { NULL,		       0,	 0, 0,		  0 }
   3031 };
   3032 
   3033 static const struct bfd_elf_special_section special_sections_f[] =
   3034 {
   3035   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   3036   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   3037   { NULL,			   0 , 0, 0,		  0 }
   3038 };
   3039 
   3040 static const struct bfd_elf_special_section special_sections_g[] =
   3041 {
   3042   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   3043   { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   3044   { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   3045   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   3046   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   3047   { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
   3048   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   3049   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   3050   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   3051   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   3052   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   3053   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   3054   { NULL,			 0,	   0, 0,	       0 }
   3055 };
   3056 
   3057 static const struct bfd_elf_special_section special_sections_h[] =
   3058 {
   3059   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   3060   { NULL,		     0, 0, 0,		 0 }
   3061 };
   3062 
   3063 static const struct bfd_elf_special_section special_sections_i[] =
   3064 {
   3065   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   3066   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   3067   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   3068   { NULL,		       0,      0, 0,		  0 }
   3069 };
   3070 
   3071 static const struct bfd_elf_special_section special_sections_l[] =
   3072 {
   3073   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   3074   { NULL,		     0, 0, 0,		 0 }
   3075 };
   3076 
   3077 static const struct bfd_elf_special_section special_sections_n[] =
   3078 {
   3079   { STRING_COMMA_LEN (".noinit"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3080   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   3081   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   3082   { NULL,		     0,		  0, 0,		   0 }
   3083 };
   3084 
   3085 static const struct bfd_elf_special_section special_sections_p[] =
   3086 {
   3087   { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,	SHF_ALLOC + SHF_WRITE },
   3088   { STRING_COMMA_LEN (".persistent"),	 -2, SHT_PROGBITS,	SHF_ALLOC + SHF_WRITE },
   3089   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   3090   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   3091   { NULL,		    0,		  0, 0,			0 }
   3092 };
   3093 
   3094 static const struct bfd_elf_special_section special_sections_r[] =
   3095 {
   3096   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   3097   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   3098   { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
   3099   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   3100   /* .relro_padding is generated by lld.  It should not be confused with a
   3101      reloc containing section, because otherwise elf_fake_sections() will
   3102      set the entsize to 8, which may not be an actual multiple of the
   3103      section's size.
   3104      Note - this entry must appear before the ".rel" entry below.  */
   3105   { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
   3106   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   3107   { NULL,		    0,	   0, 0,	    0 }
   3108 };
   3109 
   3110 static const struct bfd_elf_special_section special_sections_s[] =
   3111 {
   3112   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   3113   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   3114   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   3115   /* See struct bfd_elf_special_section declaration for the semantics of
   3116      this special case where .prefix_length != strlen (.prefix).  */
   3117   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   3118   { NULL,			0,  0, 0,	   0 }
   3119 };
   3120 
   3121 static const struct bfd_elf_special_section special_sections_t[] =
   3122 {
   3123   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   3124   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   3125   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   3126   { NULL,		      0,  0, 0,		   0 }
   3127 };
   3128 
   3129 static const struct bfd_elf_special_section special_sections_z[] =
   3130 {
   3131   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   3132   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   3133   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   3134   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   3135   { NULL,		      0,  0, 0,		   0 }
   3136 };
   3137 
   3138 static const struct bfd_elf_special_section * const special_sections[] =
   3139 {
   3140   special_sections_b,		/* 'b' */
   3141   special_sections_c,		/* 'c' */
   3142   special_sections_d,		/* 'd' */
   3143   NULL,				/* 'e' */
   3144   special_sections_f,		/* 'f' */
   3145   special_sections_g,		/* 'g' */
   3146   special_sections_h,		/* 'h' */
   3147   special_sections_i,		/* 'i' */
   3148   NULL,				/* 'j' */
   3149   NULL,				/* 'k' */
   3150   special_sections_l,		/* 'l' */
   3151   NULL,				/* 'm' */
   3152   special_sections_n,		/* 'n' */
   3153   NULL,				/* 'o' */
   3154   special_sections_p,		/* 'p' */
   3155   NULL,				/* 'q' */
   3156   special_sections_r,		/* 'r' */
   3157   special_sections_s,		/* 's' */
   3158   special_sections_t,		/* 't' */
   3159   NULL,				/* 'u' */
   3160   NULL,				/* 'v' */
   3161   NULL,				/* 'w' */
   3162   NULL,				/* 'x' */
   3163   NULL,				/* 'y' */
   3164   special_sections_z		/* 'z' */
   3165 };
   3166 
   3167 const struct bfd_elf_special_section *
   3168 _bfd_elf_get_special_section (const char *name,
   3169 			      const struct bfd_elf_special_section *spec,
   3170 			      unsigned int rela)
   3171 {
   3172   int i;
   3173   int len;
   3174 
   3175   len = strlen (name);
   3176 
   3177   for (i = 0; spec[i].prefix != NULL; i++)
   3178     {
   3179       int suffix_len;
   3180       int prefix_len = spec[i].prefix_length;
   3181 
   3182       if (len < prefix_len)
   3183 	continue;
   3184       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   3185 	continue;
   3186 
   3187       suffix_len = spec[i].suffix_length;
   3188       if (suffix_len <= 0)
   3189 	{
   3190 	  if (name[prefix_len] != 0)
   3191 	    {
   3192 	      if (suffix_len == 0)
   3193 		continue;
   3194 	      if (name[prefix_len] != '.'
   3195 		  && (suffix_len == -2
   3196 		      || (rela && spec[i].type == SHT_REL)))
   3197 		continue;
   3198 	    }
   3199 	}
   3200       else
   3201 	{
   3202 	  if (len < prefix_len + suffix_len)
   3203 	    continue;
   3204 	  if (memcmp (name + len - suffix_len,
   3205 		      spec[i].prefix + prefix_len,
   3206 		      suffix_len) != 0)
   3207 	    continue;
   3208 	}
   3209       return &spec[i];
   3210     }
   3211 
   3212   return NULL;
   3213 }
   3214 
   3215 const struct bfd_elf_special_section *
   3216 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   3217 {
   3218   int i;
   3219   const struct bfd_elf_special_section *spec;
   3220   const struct elf_backend_data *bed;
   3221 
   3222   /* See if this is one of the special sections.  */
   3223   if (sec->name == NULL)
   3224     return NULL;
   3225 
   3226   bed = get_elf_backend_data (abfd);
   3227   spec = bed->special_sections;
   3228   if (spec)
   3229     {
   3230       spec = _bfd_elf_get_special_section (sec->name,
   3231 					   bed->special_sections,
   3232 					   sec->use_rela_p);
   3233       if (spec != NULL)
   3234 	return spec;
   3235     }
   3236 
   3237   if (sec->name[0] != '.')
   3238     return NULL;
   3239 
   3240   i = sec->name[1] - 'b';
   3241   if (i < 0 || i > 'z' - 'b')
   3242     return NULL;
   3243 
   3244   spec = special_sections[i];
   3245 
   3246   if (spec == NULL)
   3247     return NULL;
   3248 
   3249   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   3250 }
   3251 
   3252 bool
   3253 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   3254 {
   3255   struct bfd_elf_section_data *sdata;
   3256   const struct elf_backend_data *bed;
   3257   const struct bfd_elf_special_section *ssect;
   3258 
   3259   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   3260   if (sdata == NULL)
   3261     {
   3262       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   3263 							  sizeof (*sdata));
   3264       if (sdata == NULL)
   3265 	return false;
   3266       sec->used_by_bfd = sdata;
   3267     }
   3268 
   3269   /* Indicate whether or not this section should use RELA relocations.  */
   3270   bed = get_elf_backend_data (abfd);
   3271   sec->use_rela_p = bed->default_use_rela_p;
   3272 
   3273   /* Set up ELF section type and flags for newly created sections, if
   3274      there is an ABI mandated section.  */
   3275   ssect = (*bed->get_sec_type_attr) (abfd, sec);
   3276   if (ssect != NULL)
   3277     {
   3278       elf_section_type (sec) = ssect->type;
   3279       elf_section_flags (sec) = ssect->attr;
   3280     }
   3281 
   3282   return _bfd_generic_new_section_hook (abfd, sec);
   3283 }
   3284 
   3285 /* Create a new bfd section from an ELF program header.
   3286 
   3287    Since program segments have no names, we generate a synthetic name
   3288    of the form segment<NUM>, where NUM is generally the index in the
   3289    program header table.  For segments that are split (see below) we
   3290    generate the names segment<NUM>a and segment<NUM>b.
   3291 
   3292    Note that some program segments may have a file size that is different than
   3293    (less than) the memory size.  All this means is that at execution the
   3294    system must allocate the amount of memory specified by the memory size,
   3295    but only initialize it with the first "file size" bytes read from the
   3296    file.  This would occur for example, with program segments consisting
   3297    of combined data+bss.
   3298 
   3299    To handle the above situation, this routine generates TWO bfd sections
   3300    for the single program segment.  The first has the length specified by
   3301    the file size of the segment, and the second has the length specified
   3302    by the difference between the two sizes.  In effect, the segment is split
   3303    into its initialized and uninitialized parts.  */
   3304 
   3305 bool
   3306 _bfd_elf_make_section_from_phdr (bfd *abfd,
   3307 				 Elf_Internal_Phdr *hdr,
   3308 				 int hdr_index,
   3309 				 const char *type_name)
   3310 {
   3311   asection *newsect;
   3312   char *name;
   3313   char namebuf[64];
   3314   size_t len;
   3315   int split;
   3316   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   3317 
   3318   split = ((hdr->p_memsz > 0)
   3319 	    && (hdr->p_filesz > 0)
   3320 	    && (hdr->p_memsz > hdr->p_filesz));
   3321 
   3322   if (hdr->p_filesz > 0)
   3323     {
   3324       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   3325       len = strlen (namebuf) + 1;
   3326       name = (char *) bfd_alloc (abfd, len);
   3327       if (!name)
   3328 	return false;
   3329       memcpy (name, namebuf, len);
   3330       newsect = bfd_make_section (abfd, name);
   3331       if (newsect == NULL)
   3332 	return false;
   3333       newsect->vma = hdr->p_vaddr / opb;
   3334       newsect->lma = hdr->p_paddr / opb;
   3335       newsect->size = hdr->p_filesz;
   3336       newsect->filepos = hdr->p_offset;
   3337       newsect->flags |= SEC_HAS_CONTENTS;
   3338       newsect->alignment_power = bfd_log2 (hdr->p_align);
   3339       if (hdr->p_type == PT_LOAD)
   3340 	{
   3341 	  newsect->flags |= SEC_ALLOC;
   3342 	  newsect->flags |= SEC_LOAD;
   3343 	  if (hdr->p_flags & PF_X)
   3344 	    {
   3345 	      /* FIXME: all we known is that it has execute PERMISSION,
   3346 		 may be data.  */
   3347 	      newsect->flags |= SEC_CODE;
   3348 	    }
   3349 	}
   3350       if (!(hdr->p_flags & PF_W))
   3351 	{
   3352 	  newsect->flags |= SEC_READONLY;
   3353 	}
   3354     }
   3355 
   3356   if (hdr->p_memsz > hdr->p_filesz)
   3357     {
   3358       bfd_vma align;
   3359 
   3360       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   3361       len = strlen (namebuf) + 1;
   3362       name = (char *) bfd_alloc (abfd, len);
   3363       if (!name)
   3364 	return false;
   3365       memcpy (name, namebuf, len);
   3366       newsect = bfd_make_section (abfd, name);
   3367       if (newsect == NULL)
   3368 	return false;
   3369       newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
   3370       newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
   3371       newsect->size = hdr->p_memsz - hdr->p_filesz;
   3372       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   3373       align = newsect->vma & -newsect->vma;
   3374       if (align == 0 || align > hdr->p_align)
   3375 	align = hdr->p_align;
   3376       newsect->alignment_power = bfd_log2 (align);
   3377       if (hdr->p_type == PT_LOAD)
   3378 	{
   3379 	  newsect->flags |= SEC_ALLOC;
   3380 	  if (hdr->p_flags & PF_X)
   3381 	    newsect->flags |= SEC_CODE;
   3382 	}
   3383       if (!(hdr->p_flags & PF_W))
   3384 	newsect->flags |= SEC_READONLY;
   3385     }
   3386 
   3387   return true;
   3388 }
   3389 
   3390 static bool
   3391 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   3392 {
   3393   /* The return value is ignored.  Build-ids are considered optional.  */
   3394   if (templ->xvec->flavour == bfd_target_elf_flavour)
   3395     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   3396       (templ, offset);
   3397   return false;
   3398 }
   3399 
   3400 bool
   3401 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3402 {
   3403   const struct elf_backend_data *bed;
   3404 
   3405   switch (hdr->p_type)
   3406     {
   3407     case PT_NULL:
   3408       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3409 
   3410     case PT_LOAD:
   3411       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3412 	return false;
   3413       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3414 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3415       return true;
   3416 
   3417     case PT_DYNAMIC:
   3418       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3419 
   3420     case PT_INTERP:
   3421       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3422 
   3423     case PT_NOTE:
   3424       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3425 	return false;
   3426       if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3427 			    hdr->p_align))
   3428 	return false;
   3429       return true;
   3430 
   3431     case PT_SHLIB:
   3432       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3433 
   3434     case PT_PHDR:
   3435       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3436 
   3437     case PT_GNU_EH_FRAME:
   3438       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3439 					      "eh_frame_hdr");
   3440 
   3441     case PT_GNU_STACK:
   3442       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3443 
   3444     case PT_GNU_RELRO:
   3445       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3446 
   3447     case PT_GNU_SFRAME:
   3448       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3449 					      "sframe");
   3450 
   3451     default:
   3452       /* Check for any processor-specific program segment types.  */
   3453       bed = get_elf_backend_data (abfd);
   3454       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3455     }
   3456 }
   3457 
   3458 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3459    REL or RELA.  */
   3460 
   3461 Elf_Internal_Shdr *
   3462 _bfd_elf_single_rel_hdr (asection *sec)
   3463 {
   3464   if (elf_section_data (sec)->rel.hdr)
   3465     {
   3466       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3467       return elf_section_data (sec)->rel.hdr;
   3468     }
   3469   else
   3470     return elf_section_data (sec)->rela.hdr;
   3471 }
   3472 
   3473 static bool
   3474 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3475 			    Elf_Internal_Shdr *rel_hdr,
   3476 			    const char *sec_name,
   3477 			    bool use_rela_p)
   3478 {
   3479   char *name = (char *) bfd_alloc (abfd,
   3480 				   sizeof ".rela" + strlen (sec_name));
   3481   if (name == NULL)
   3482     return false;
   3483 
   3484   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3485   rel_hdr->sh_name =
   3486     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3487 					false);
   3488   if (rel_hdr->sh_name == (unsigned int) -1)
   3489     return false;
   3490 
   3491   return true;
   3492 }
   3493 
   3494 /* Allocate and initialize a section-header for a new reloc section,
   3495    containing relocations against ASECT.  It is stored in RELDATA.  If
   3496    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3497    relocations.  */
   3498 
   3499 static bool
   3500 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3501 			  struct bfd_elf_section_reloc_data *reldata,
   3502 			  const char *sec_name,
   3503 			  bool use_rela_p,
   3504 			  bool delay_sh_name_p)
   3505 {
   3506   Elf_Internal_Shdr *rel_hdr;
   3507   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3508 
   3509   BFD_ASSERT (reldata->hdr == NULL);
   3510   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3511   if (rel_hdr == NULL)
   3512     return false;
   3513   reldata->hdr = rel_hdr;
   3514 
   3515   if (delay_sh_name_p)
   3516     rel_hdr->sh_name = (unsigned int) -1;
   3517   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3518 					use_rela_p))
   3519     return false;
   3520   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3521   rel_hdr->sh_entsize = (use_rela_p
   3522 			 ? bed->s->sizeof_rela
   3523 			 : bed->s->sizeof_rel);
   3524   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3525   rel_hdr->sh_flags = 0;
   3526   rel_hdr->sh_addr = 0;
   3527   rel_hdr->sh_size = 0;
   3528   rel_hdr->sh_offset = 0;
   3529 
   3530   return true;
   3531 }
   3532 
   3533 /* Return the default section type based on the passed in section flags.  */
   3534 
   3535 int
   3536 bfd_elf_get_default_section_type (flagword flags)
   3537 {
   3538   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3539       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3540     return SHT_NOBITS;
   3541   return SHT_PROGBITS;
   3542 }
   3543 
   3544 struct fake_section_arg
   3545 {
   3546   struct bfd_link_info *link_info;
   3547   bool failed;
   3548 };
   3549 
   3550 /* Set up an ELF internal section header for a section.  */
   3551 
   3552 static void
   3553 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3554 {
   3555   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3556   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   3557   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3558   Elf_Internal_Shdr *this_hdr;
   3559   unsigned int sh_type;
   3560   const char *name = asect->name;
   3561   bool delay_sh_name_p = false;
   3562   bfd_vma mask;
   3563 
   3564   if (arg->failed)
   3565     {
   3566       /* We already failed; just get out of the bfd_map_over_sections
   3567 	 loop.  */
   3568       return;
   3569     }
   3570 
   3571   this_hdr = &esd->this_hdr;
   3572 
   3573   /* ld: compress DWARF debug sections with names: .debug_*.  */
   3574   if (arg->link_info
   3575       && (abfd->flags & BFD_COMPRESS) != 0
   3576       && (asect->flags & SEC_DEBUGGING) != 0
   3577       && (asect->flags & SEC_ALLOC) == 0
   3578       && (asect->flags & SEC_HAS_CONTENTS) != 0
   3579       && name[1] == 'd'
   3580       && name[6] == '_')
   3581     {
   3582       /* If this section will be compressed, delay adding section
   3583 	 name to section name section after it is compressed in
   3584 	 _bfd_elf_assign_file_positions_for_non_load.  */
   3585       delay_sh_name_p = true;
   3586     }
   3587 
   3588   if (delay_sh_name_p)
   3589     this_hdr->sh_name = (unsigned int) -1;
   3590   else
   3591     {
   3592       this_hdr->sh_name
   3593 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3594 					      name, false);
   3595       if (this_hdr->sh_name == (unsigned int) -1)
   3596 	{
   3597 	  arg->failed = true;
   3598 	  return;
   3599 	}
   3600     }
   3601 
   3602   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3603 
   3604   if ((asect->flags & SEC_ALLOC) != 0
   3605       || asect->user_set_vma)
   3606     this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
   3607   else
   3608     this_hdr->sh_addr = 0;
   3609 
   3610   this_hdr->sh_offset = 0;
   3611   this_hdr->sh_size = asect->size;
   3612   this_hdr->sh_link = 0;
   3613   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3614   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3615     {
   3616       _bfd_error_handler
   3617 	/* xgettext:c-format */
   3618 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3619 	 abfd, asect->alignment_power, asect);
   3620       arg->failed = true;
   3621       return;
   3622     }
   3623   /* Set sh_addralign to the highest power of two given by alignment
   3624      consistent with the section VMA.  Linker scripts can force VMA.  */
   3625   mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
   3626   this_hdr->sh_addralign = mask & -mask;
   3627   /* The sh_entsize and sh_info fields may have been set already by
   3628      copy_private_section_data.  */
   3629 
   3630   this_hdr->bfd_section = asect;
   3631   this_hdr->contents = NULL;
   3632 
   3633   /* If the section type is unspecified, we set it based on
   3634      asect->flags.  */
   3635   if (asect->type != 0)
   3636     sh_type = asect->type;
   3637   else if ((asect->flags & SEC_GROUP) != 0)
   3638     sh_type = SHT_GROUP;
   3639   else
   3640     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3641 
   3642   if (this_hdr->sh_type == SHT_NULL)
   3643     this_hdr->sh_type = sh_type;
   3644   else if (this_hdr->sh_type == SHT_NOBITS
   3645 	   && sh_type == SHT_PROGBITS
   3646 	   && (asect->flags & SEC_ALLOC) != 0)
   3647     {
   3648       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3649 	 allow the link to proceed.  This can happen when users link
   3650 	 non-bss input sections to bss output sections, or emit data
   3651 	 to a bss output section via a linker script.  */
   3652       _bfd_error_handler
   3653 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3654       this_hdr->sh_type = sh_type;
   3655     }
   3656 
   3657   switch (this_hdr->sh_type)
   3658     {
   3659     default:
   3660       break;
   3661 
   3662     case SHT_STRTAB:
   3663     case SHT_NOTE:
   3664     case SHT_NOBITS:
   3665     case SHT_PROGBITS:
   3666       break;
   3667 
   3668     case SHT_INIT_ARRAY:
   3669     case SHT_FINI_ARRAY:
   3670     case SHT_PREINIT_ARRAY:
   3671       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3672       break;
   3673 
   3674     case SHT_HASH:
   3675       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3676       break;
   3677 
   3678     case SHT_DYNSYM:
   3679       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3680       break;
   3681 
   3682     case SHT_DYNAMIC:
   3683       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3684       break;
   3685 
   3686     case SHT_RELA:
   3687       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3688 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3689       break;
   3690 
   3691      case SHT_REL:
   3692       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3693 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3694       break;
   3695 
   3696      case SHT_GNU_versym:
   3697       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3698       break;
   3699 
   3700      case SHT_GNU_verdef:
   3701       this_hdr->sh_entsize = 0;
   3702       /* objcopy or strip will copy over sh_info, but may not set
   3703 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3704 	 zero.  */
   3705       if (this_hdr->sh_info == 0)
   3706 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3707       else
   3708 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3709 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3710       break;
   3711 
   3712     case SHT_GNU_verneed:
   3713       this_hdr->sh_entsize = 0;
   3714       /* objcopy or strip will copy over sh_info, but may not set
   3715 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3716 	 zero.  */
   3717       if (this_hdr->sh_info == 0)
   3718 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3719       else
   3720 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3721 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3722       break;
   3723 
   3724     case SHT_GROUP:
   3725       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3726       break;
   3727 
   3728     case SHT_GNU_HASH:
   3729       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3730       break;
   3731     }
   3732 
   3733   if ((asect->flags & SEC_ALLOC) != 0)
   3734     this_hdr->sh_flags |= SHF_ALLOC;
   3735   if ((asect->flags & SEC_READONLY) == 0)
   3736     this_hdr->sh_flags |= SHF_WRITE;
   3737   if ((asect->flags & SEC_CODE) != 0)
   3738     this_hdr->sh_flags |= SHF_EXECINSTR;
   3739   if ((asect->flags & SEC_MERGE) != 0)
   3740     {
   3741       this_hdr->sh_flags |= SHF_MERGE;
   3742       this_hdr->sh_entsize = asect->entsize;
   3743     }
   3744   if ((asect->flags & SEC_STRINGS) != 0)
   3745     {
   3746       this_hdr->sh_flags |= SHF_STRINGS;
   3747       this_hdr->sh_entsize = asect->entsize;
   3748     }
   3749   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3750     this_hdr->sh_flags |= SHF_GROUP;
   3751   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3752     {
   3753       this_hdr->sh_flags |= SHF_TLS;
   3754       if (asect->size == 0
   3755 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3756 	{
   3757 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3758 
   3759 	  this_hdr->sh_size = 0;
   3760 	  if (o != NULL)
   3761 	    {
   3762 	      this_hdr->sh_size = o->offset + o->size;
   3763 	      if (this_hdr->sh_size != 0)
   3764 		this_hdr->sh_type = SHT_NOBITS;
   3765 	    }
   3766 	}
   3767     }
   3768   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3769     this_hdr->sh_flags |= SHF_EXCLUDE;
   3770 
   3771   /* If the section has relocs, set up a section header for the
   3772      SHT_REL[A] section.  If two relocation sections are required for
   3773      this section, it is up to the processor-specific back-end to
   3774      create the other.  */
   3775   if ((asect->flags & SEC_RELOC) != 0)
   3776     {
   3777       /* When doing a relocatable link, create both REL and RELA sections if
   3778 	 needed.  */
   3779       if (arg->link_info
   3780 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3781 	  && esd->rel.count + esd->rela.count > 0
   3782 	  && (bfd_link_relocatable (arg->link_info)
   3783 	      || arg->link_info->emitrelocations))
   3784 	{
   3785 	  if (esd->rel.count && esd->rel.hdr == NULL
   3786 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3787 					    false, delay_sh_name_p))
   3788 	    {
   3789 	      arg->failed = true;
   3790 	      return;
   3791 	    }
   3792 	  if (esd->rela.count && esd->rela.hdr == NULL
   3793 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3794 					    true, delay_sh_name_p))
   3795 	    {
   3796 	      arg->failed = true;
   3797 	      return;
   3798 	    }
   3799 	}
   3800       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3801 					  (asect->use_rela_p
   3802 					   ? &esd->rela : &esd->rel),
   3803 					  name,
   3804 					  asect->use_rela_p,
   3805 					  delay_sh_name_p))
   3806 	{
   3807 	  arg->failed = true;
   3808 	  return;
   3809 	}
   3810     }
   3811 
   3812   /* Check for processor-specific section types.  */
   3813   sh_type = this_hdr->sh_type;
   3814   if (bed->elf_backend_fake_sections
   3815       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3816     {
   3817       arg->failed = true;
   3818       return;
   3819     }
   3820 
   3821   if (sh_type == SHT_NOBITS && asect->size != 0)
   3822     {
   3823       /* Don't change the header type from NOBITS if we are being
   3824 	 called for objcopy --only-keep-debug.  */
   3825       this_hdr->sh_type = sh_type;
   3826     }
   3827 }
   3828 
   3829 /* Fill in the contents of a SHT_GROUP section.  Called from
   3830    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3831    when ELF targets use the generic linker, ld.  Called for ld -r
   3832    from bfd_elf_final_link.  */
   3833 
   3834 void
   3835 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3836 {
   3837   bool *failedptr = (bool *) failedptrarg;
   3838   asection *elt, *first;
   3839   unsigned char *loc;
   3840   bool gas;
   3841 
   3842   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3843      elfxx-ia64.c.  */
   3844   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3845       || sec->size == 0
   3846       || *failedptr)
   3847     return;
   3848 
   3849   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3850     {
   3851       unsigned long symindx = 0;
   3852 
   3853       /* elf_group_id will have been set up by objcopy and the
   3854 	 generic linker.  */
   3855       if (elf_group_id (sec) != NULL)
   3856 	symindx = elf_group_id (sec)->udata.i;
   3857 
   3858       if (symindx == 0)
   3859 	{
   3860 	  /* If called from the assembler, swap_out_syms will have set up
   3861 	     elf_section_syms.
   3862 	     PR 25699: A corrupt input file could contain bogus group info.  */
   3863 	  if (sec->index >= elf_num_section_syms (abfd)
   3864 	      || elf_section_syms (abfd)[sec->index] == NULL)
   3865 	    {
   3866 	      *failedptr = true;
   3867 	      return;
   3868 	    }
   3869 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3870 	}
   3871       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3872     }
   3873   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3874     {
   3875       /* The ELF backend linker sets sh_info to -2 when the group
   3876 	 signature symbol is global, and thus the index can't be
   3877 	 set until all local symbols are output.  */
   3878       asection *igroup;
   3879       struct bfd_elf_section_data *sec_data;
   3880       unsigned long symndx;
   3881       unsigned long extsymoff;
   3882       struct elf_link_hash_entry *h;
   3883 
   3884       /* The point of this little dance to the first SHF_GROUP section
   3885 	 then back to the SHT_GROUP section is that this gets us to
   3886 	 the SHT_GROUP in the input object.  */
   3887       igroup = elf_sec_group (elf_next_in_group (sec));
   3888       sec_data = elf_section_data (igroup);
   3889       symndx = sec_data->this_hdr.sh_info;
   3890       extsymoff = 0;
   3891       if (!elf_bad_symtab (igroup->owner))
   3892 	{
   3893 	  Elf_Internal_Shdr *symtab_hdr;
   3894 
   3895 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3896 	  extsymoff = symtab_hdr->sh_info;
   3897 	}
   3898       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3899       while (h->root.type == bfd_link_hash_indirect
   3900 	     || h->root.type == bfd_link_hash_warning)
   3901 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3902 
   3903       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3904     }
   3905 
   3906   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3907   gas = true;
   3908   if (sec->contents == NULL)
   3909     {
   3910       gas = false;
   3911       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3912 
   3913       /* Arrange for the section to be written out.  */
   3914       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3915       if (sec->contents == NULL)
   3916 	{
   3917 	  *failedptr = true;
   3918 	  return;
   3919 	}
   3920       sec->alloced = 1;
   3921     }
   3922 
   3923   loc = sec->contents + sec->size;
   3924 
   3925   /* Get the pointer to the first section in the group that gas
   3926      squirreled away here.  objcopy arranges for this to be set to the
   3927      start of the input section group.  */
   3928   first = elt = elf_next_in_group (sec);
   3929 
   3930   /* First element is a flag word.  Rest of section is elf section
   3931      indices for all the sections of the group.  Write them backwards
   3932      just to keep the group in the same order as given in .section
   3933      directives, not that it matters.  */
   3934   while (elt != NULL)
   3935     {
   3936       asection *s;
   3937 
   3938       s = elt;
   3939       if (!gas)
   3940 	s = s->output_section;
   3941       if (s != NULL
   3942 	  && !bfd_is_abs_section (s))
   3943 	{
   3944 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   3945 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   3946 
   3947 	  if (elf_sec->rel.hdr != NULL
   3948 	      && (gas
   3949 		  || (input_elf_sec->rel.hdr != NULL
   3950 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   3951 	    {
   3952 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   3953 	      loc -= 4;
   3954 	      if (loc == sec->contents)
   3955 		break;
   3956 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   3957 	    }
   3958 	  if (elf_sec->rela.hdr != NULL
   3959 	      && (gas
   3960 		  || (input_elf_sec->rela.hdr != NULL
   3961 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   3962 	    {
   3963 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   3964 	      loc -= 4;
   3965 	      if (loc == sec->contents)
   3966 		break;
   3967 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   3968 	    }
   3969 	  loc -= 4;
   3970 	  if (loc == sec->contents)
   3971 	    break;
   3972 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   3973 	}
   3974       elt = elf_next_in_group (elt);
   3975       if (elt == first)
   3976 	break;
   3977     }
   3978 
   3979   /* We should always get here with loc == sec->contents + 4.  Return
   3980      an error for bogus SHT_GROUP sections.  */
   3981   loc -= 4;
   3982   if (loc != sec->contents)
   3983     {
   3984       /* xgettext:c-format */
   3985       _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
   3986 			  abfd, sec);
   3987       bfd_set_error (bfd_error_bad_value);
   3988       *failedptr = true;
   3989       return;
   3990     }
   3991 
   3992   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   3993 }
   3994 
   3995 /* Given NAME, the name of a relocation section stripped of its
   3996    .rel/.rela prefix, return the section in ABFD to which the
   3997    relocations apply.  */
   3998 
   3999 asection *
   4000 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   4001 {
   4002   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   4003      section likely apply to .got.plt or .got section.  */
   4004   if (get_elf_backend_data (abfd)->want_got_plt
   4005       && strcmp (name, ".plt") == 0)
   4006     {
   4007       asection *sec;
   4008 
   4009       name = ".got.plt";
   4010       sec = bfd_get_section_by_name (abfd, name);
   4011       if (sec != NULL)
   4012 	return sec;
   4013       name = ".got";
   4014     }
   4015 
   4016   return bfd_get_section_by_name (abfd, name);
   4017 }
   4018 
   4019 /* Return the section to which RELOC_SEC applies.  */
   4020 
   4021 static asection *
   4022 elf_get_reloc_section (asection *reloc_sec)
   4023 {
   4024   const char *name;
   4025   unsigned int type;
   4026   bfd *abfd;
   4027   const struct elf_backend_data *bed;
   4028 
   4029   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   4030   if (type != SHT_REL && type != SHT_RELA)
   4031     return NULL;
   4032 
   4033   /* We look up the section the relocs apply to by name.  */
   4034   name = reloc_sec->name;
   4035   if (!startswith (name, ".rel"))
   4036     return NULL;
   4037   name += 4;
   4038   if (type == SHT_RELA && *name++ != 'a')
   4039     return NULL;
   4040 
   4041   abfd = reloc_sec->owner;
   4042   bed = get_elf_backend_data (abfd);
   4043   return bed->get_reloc_section (abfd, name);
   4044 }
   4045 
   4046 /* Assign all ELF section numbers.  The dummy first section is handled here
   4047    too.  The link/info pointers for the standard section types are filled
   4048    in here too, while we're at it.  LINK_INFO will be 0 when arriving
   4049    here for gas, objcopy, and when using the generic ELF linker.  */
   4050 
   4051 static bool
   4052 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   4053 {
   4054   struct elf_obj_tdata *t = elf_tdata (abfd);
   4055   asection *sec;
   4056   unsigned int section_number;
   4057   Elf_Internal_Shdr **i_shdrp;
   4058   struct bfd_elf_section_data *d;
   4059   bool need_symtab;
   4060   size_t amt;
   4061 
   4062   section_number = 1;
   4063 
   4064   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   4065 
   4066   /* SHT_GROUP sections are in relocatable files only.  */
   4067   if (link_info == NULL || !link_info->resolve_section_groups)
   4068     {
   4069       size_t reloc_count = 0;
   4070 
   4071       /* Put SHT_GROUP sections first.  */
   4072       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   4073 	{
   4074 	  d = elf_section_data (sec);
   4075 
   4076 	  if (d->this_hdr.sh_type == SHT_GROUP)
   4077 	    {
   4078 	      if (sec->flags & SEC_LINKER_CREATED)
   4079 		{
   4080 		  /* Remove the linker created SHT_GROUP sections.  */
   4081 		  bfd_section_list_remove (abfd, sec);
   4082 		  abfd->section_count--;
   4083 		}
   4084 	      else
   4085 		d->this_idx = section_number++;
   4086 	    }
   4087 
   4088 	  /* Count relocations.  */
   4089 	  reloc_count += sec->reloc_count;
   4090 	}
   4091 
   4092       /* Set/clear HAS_RELOC depending on whether there are relocations.  */
   4093       if (reloc_count == 0)
   4094 	abfd->flags &= ~HAS_RELOC;
   4095       else
   4096 	abfd->flags |= HAS_RELOC;
   4097     }
   4098 
   4099   for (sec = abfd->sections; sec; sec = sec->next)
   4100     {
   4101       d = elf_section_data (sec);
   4102 
   4103       if (d->this_hdr.sh_type != SHT_GROUP)
   4104 	d->this_idx = section_number++;
   4105       if (d->this_hdr.sh_name != (unsigned int) -1)
   4106 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   4107       if (d->rel.hdr)
   4108 	{
   4109 	  d->rel.idx = section_number++;
   4110 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   4111 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   4112 	}
   4113       else
   4114 	d->rel.idx = 0;
   4115 
   4116       if (d->rela.hdr)
   4117 	{
   4118 	  d->rela.idx = section_number++;
   4119 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   4120 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   4121 	}
   4122       else
   4123 	d->rela.idx = 0;
   4124     }
   4125 
   4126   need_symtab = (bfd_get_symcount (abfd) > 0
   4127 		 || (link_info == NULL
   4128 		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4129 			 == HAS_RELOC)));
   4130   if (need_symtab)
   4131     {
   4132       elf_onesymtab (abfd) = section_number++;
   4133       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   4134       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   4135 	{
   4136 	  elf_section_list *entry;
   4137 
   4138 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   4139 
   4140 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   4141 	  entry->ndx = section_number++;
   4142 	  elf_symtab_shndx_list (abfd) = entry;
   4143 	  entry->hdr.sh_name
   4144 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   4145 						  ".symtab_shndx", false);
   4146 	  if (entry->hdr.sh_name == (unsigned int) -1)
   4147 	    return false;
   4148 	}
   4149       elf_strtab_sec (abfd) = section_number++;
   4150       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   4151     }
   4152 
   4153   elf_shstrtab_sec (abfd) = section_number++;
   4154   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   4155   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   4156 
   4157   if (section_number >= SHN_LORESERVE)
   4158     {
   4159       /* xgettext:c-format */
   4160       _bfd_error_handler (_("%pB: too many sections: %u"),
   4161 			  abfd, section_number);
   4162       return false;
   4163     }
   4164 
   4165   elf_numsections (abfd) = section_number;
   4166   elf_elfheader (abfd)->e_shnum = section_number;
   4167 
   4168   /* Set up the list of section header pointers, in agreement with the
   4169      indices.  */
   4170   amt = section_number * sizeof (Elf_Internal_Shdr *);
   4171   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
   4172   if (i_shdrp == NULL)
   4173     return false;
   4174 
   4175   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   4176 						 sizeof (Elf_Internal_Shdr));
   4177   if (i_shdrp[0] == NULL)
   4178     {
   4179       bfd_release (abfd, i_shdrp);
   4180       return false;
   4181     }
   4182 
   4183   elf_elfsections (abfd) = i_shdrp;
   4184 
   4185   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   4186   if (need_symtab)
   4187     {
   4188       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   4189       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   4190 	{
   4191 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   4192 	  BFD_ASSERT (entry != NULL);
   4193 	  i_shdrp[entry->ndx] = & entry->hdr;
   4194 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   4195 	}
   4196       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   4197       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   4198     }
   4199 
   4200   for (sec = abfd->sections; sec; sec = sec->next)
   4201     {
   4202       asection *s;
   4203 
   4204       d = elf_section_data (sec);
   4205 
   4206       i_shdrp[d->this_idx] = &d->this_hdr;
   4207       if (d->rel.idx != 0)
   4208 	i_shdrp[d->rel.idx] = d->rel.hdr;
   4209       if (d->rela.idx != 0)
   4210 	i_shdrp[d->rela.idx] = d->rela.hdr;
   4211 
   4212       /* Fill in the sh_link and sh_info fields while we're at it.  */
   4213 
   4214       /* sh_link of a reloc section is the section index of the symbol
   4215 	 table.  sh_info is the section index of the section to which
   4216 	 the relocation entries apply.  */
   4217       if (d->rel.idx != 0)
   4218 	{
   4219 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   4220 	  d->rel.hdr->sh_info = d->this_idx;
   4221 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   4222 	}
   4223       if (d->rela.idx != 0)
   4224 	{
   4225 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   4226 	  d->rela.hdr->sh_info = d->this_idx;
   4227 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   4228 	}
   4229 
   4230       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   4231       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   4232 	{
   4233 	  s = elf_linked_to_section (sec);
   4234 	  /* We can now have a NULL linked section pointer.
   4235 	     This happens when the sh_link field is 0, which is done
   4236 	     when a linked to section is discarded but the linking
   4237 	     section has been retained for some reason.  */
   4238 	  if (s)
   4239 	    {
   4240 	      /* Check discarded linkonce section.  */
   4241 	      if (discarded_section (s))
   4242 		{
   4243 		  asection *kept;
   4244 		  _bfd_error_handler
   4245 		    /* xgettext:c-format */
   4246 		    (_("%pB: sh_link of section `%pA' points to"
   4247 		       " discarded section `%pA' of `%pB'"),
   4248 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   4249 		  /* Point to the kept section if it has the same
   4250 		     size as the discarded one.  */
   4251 		  kept = _bfd_elf_check_kept_section (s, link_info);
   4252 		  if (kept == NULL)
   4253 		    {
   4254 		      bfd_set_error (bfd_error_bad_value);
   4255 		      return false;
   4256 		    }
   4257 		  s = kept;
   4258 		}
   4259 	      /* Handle objcopy. */
   4260 	      else if (s->output_section == NULL)
   4261 		{
   4262 		  _bfd_error_handler
   4263 		    /* xgettext:c-format */
   4264 		    (_("%pB: sh_link of section `%pA' points to"
   4265 		       " removed section `%pA' of `%pB'"),
   4266 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   4267 		  bfd_set_error (bfd_error_bad_value);
   4268 		  return false;
   4269 		}
   4270 	      s = s->output_section;
   4271 	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4272 	    }
   4273 	}
   4274 
   4275       switch (d->this_hdr.sh_type)
   4276 	{
   4277 	case SHT_REL:
   4278 	case SHT_RELA:
   4279 	  /* sh_link is the section index of the symbol table.
   4280 	     sh_info is the section index of the section to which the
   4281 	     relocation entries apply.  */
   4282 	  if (d->this_hdr.sh_link == 0)
   4283 	    {
   4284 	      /* FIXME maybe: If this is a reloc section which we are
   4285 		 treating as a normal section then we likely should
   4286 		 not be assuming its sh_link is .dynsym or .symtab.  */
   4287 	      if ((sec->flags & SEC_ALLOC) != 0)
   4288 		{
   4289 		  s = bfd_get_section_by_name (abfd, ".dynsym");
   4290 		  if (s != NULL)
   4291 		    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4292 		}
   4293 	      else
   4294 		d->this_hdr.sh_link = elf_onesymtab (abfd);
   4295 	    }
   4296 
   4297 	  s = elf_get_reloc_section (sec);
   4298 	  if (s != NULL)
   4299 	    {
   4300 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   4301 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   4302 	    }
   4303 	  break;
   4304 
   4305 	case SHT_STRTAB:
   4306 	  /* We assume that a section named .stab*str is a stabs
   4307 	     string section.  We look for a section with the same name
   4308 	     but without the trailing ``str'', and set its sh_link
   4309 	     field to point to this section.  */
   4310 	  if (startswith (sec->name, ".stab")
   4311 	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
   4312 	    {
   4313 	      size_t len;
   4314 	      char *alc;
   4315 
   4316 	      len = strlen (sec->name);
   4317 	      alc = (char *) bfd_malloc (len - 2);
   4318 	      if (alc == NULL)
   4319 		return false;
   4320 	      memcpy (alc, sec->name, len - 3);
   4321 	      alc[len - 3] = '\0';
   4322 	      s = bfd_get_section_by_name (abfd, alc);
   4323 	      free (alc);
   4324 	      if (s != NULL)
   4325 		{
   4326 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   4327 
   4328 		  /* This is a .stab section.  */
   4329 		  elf_section_data (s)->this_hdr.sh_entsize = 12;
   4330 		}
   4331 	    }
   4332 	  break;
   4333 
   4334 	case SHT_DYNAMIC:
   4335 	case SHT_DYNSYM:
   4336 	case SHT_GNU_verneed:
   4337 	case SHT_GNU_verdef:
   4338 	  /* sh_link is the section header index of the string table
   4339 	     used for the dynamic entries, or the symbol table, or the
   4340 	     version strings.  */
   4341 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   4342 	  if (s != NULL)
   4343 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4344 	  break;
   4345 
   4346 	case SHT_GNU_LIBLIST:
   4347 	  /* sh_link is the section header index of the prelink library
   4348 	     list used for the dynamic entries, or the symbol table, or
   4349 	     the version strings.  */
   4350 	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
   4351 					      ? ".dynstr" : ".gnu.libstr"));
   4352 	  if (s != NULL)
   4353 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4354 	  break;
   4355 
   4356 	case SHT_HASH:
   4357 	case SHT_GNU_HASH:
   4358 	case SHT_GNU_versym:
   4359 	  /* sh_link is the section header index of the symbol table
   4360 	     this hash table or version table is for.  */
   4361 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   4362 	  if (s != NULL)
   4363 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4364 	  break;
   4365 
   4366 	case SHT_GROUP:
   4367 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   4368 	}
   4369     }
   4370 
   4371   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   4372      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   4373      debug section name from .debug_* to .zdebug_* if needed.  */
   4374 
   4375   return true;
   4376 }
   4377 
   4378 static bool
   4379 sym_is_global (bfd *abfd, asymbol *sym)
   4380 {
   4381   /* If the backend has a special mapping, use it.  */
   4382   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4383   if (bed->elf_backend_sym_is_global)
   4384     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   4385 
   4386   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   4387 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   4388 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   4389 }
   4390 
   4391 /* Filter global symbols of ABFD to include in the import library.  All
   4392    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4393    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4394    the beginning of that array.
   4395 
   4396    Returns the number of symbols to keep.  */
   4397 
   4398 unsigned int
   4399 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4400 				asymbol **syms, long symcount)
   4401 {
   4402   long src_count, dst_count = 0;
   4403 
   4404   for (src_count = 0; src_count < symcount; src_count++)
   4405     {
   4406       asymbol *sym = syms[src_count];
   4407       char *name = (char *) bfd_asymbol_name (sym);
   4408       struct bfd_link_hash_entry *h;
   4409 
   4410       if (!sym_is_global (abfd, sym))
   4411 	continue;
   4412 
   4413       h = bfd_link_hash_lookup (info->hash, name, false, false, false);
   4414       if (h == NULL)
   4415 	continue;
   4416       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4417 	continue;
   4418       if (h->linker_def || h->ldscript_def)
   4419 	continue;
   4420 
   4421       syms[dst_count++] = sym;
   4422     }
   4423 
   4424   syms[dst_count] = NULL;
   4425 
   4426   return dst_count;
   4427 }
   4428 
   4429 /* Don't output symbols for sections that are not going to be output,
   4430    that are duplicates or there is no BFD section.  */
   4431 
   4432 static bool
   4433 ignore_sym (asymbol *sym)
   4434 {
   4435   if (sym == NULL)
   4436     return false;
   4437 
   4438   if (sym->section == NULL)
   4439     return true;
   4440 
   4441   if ((sym->flags & BSF_SECTION_SYM) != 0)
   4442     {
   4443       if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
   4444 	return true;
   4445       /* With ld -r on generic elf targets it is possible to have
   4446 	 multiple section symbols in the output for a given section.
   4447 	 We'd like to get rid of all but the first one.  This drops
   4448 	 them if the first input section is non-zero size, but fails
   4449 	 to do so if the first input section is zero sized.  */
   4450       if (sym->section->output_offset != 0)
   4451 	return true;
   4452     }
   4453 
   4454   return discarded_section (sym->section);
   4455 }
   4456 
   4457 /* Map symbol from it's internal number to the external number, moving
   4458    all local symbols to be at the head of the list.  */
   4459 
   4460 static bool
   4461 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4462 {
   4463   unsigned int symcount = bfd_get_symcount (abfd);
   4464   asymbol **syms = bfd_get_outsymbols (abfd);
   4465   asymbol **sect_syms;
   4466   unsigned int num_locals = 0;
   4467   unsigned int num_globals = 0;
   4468   unsigned int max_index = 0;
   4469   unsigned int idx;
   4470   asection *asect;
   4471   asymbol **new_syms;
   4472   size_t amt;
   4473 
   4474 #ifdef DEBUG
   4475   fprintf (stderr, "elf_map_symbols\n");
   4476   fflush (stderr);
   4477 #endif
   4478 
   4479   for (asect = abfd->sections; asect; asect = asect->next)
   4480     {
   4481       if (max_index < asect->index)
   4482 	max_index = asect->index;
   4483     }
   4484 
   4485   max_index++;
   4486   amt = max_index * sizeof (asymbol *);
   4487   sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
   4488   if (sect_syms == NULL)
   4489     return false;
   4490   elf_section_syms (abfd) = sect_syms;
   4491   elf_num_section_syms (abfd) = max_index;
   4492 
   4493   /* Init sect_syms entries for any section symbols we have already
   4494      decided to output.  */
   4495   for (idx = 0; idx < symcount; idx++)
   4496     {
   4497       asymbol *sym = syms[idx];
   4498 
   4499       if ((sym->flags & BSF_SECTION_SYM) != 0
   4500 	  && sym->value == 0
   4501 	  && !ignore_sym (sym)
   4502 	  && !bfd_is_abs_section (sym->section))
   4503 	{
   4504 	  asection *sec = sym->section;
   4505 
   4506 	  if (sec->owner != abfd)
   4507 	    {
   4508 	      sec = sec->output_section;
   4509 	      if (sec == NULL)
   4510 		return false;
   4511 	    }
   4512 
   4513 	  sect_syms[sec->index] = syms[idx];
   4514 	}
   4515     }
   4516 
   4517   /* Classify all of the symbols.  */
   4518   for (idx = 0; idx < symcount; idx++)
   4519     {
   4520       if (ignore_sym (syms[idx]))
   4521 	continue;
   4522       if (sym_is_global (abfd, syms[idx]))
   4523 	num_globals++;
   4524       else
   4525 	num_locals++;
   4526     }
   4527 
   4528   /* We will be adding a section symbol for each normal BFD section.  Most
   4529      sections will already have a section symbol in outsymbols, but
   4530      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4531      at least in that case.  */
   4532   for (asect = abfd->sections; asect; asect = asect->next)
   4533     {
   4534       asymbol *sym = asect->symbol;
   4535       /* Don't include ignored section symbols.  */
   4536       if (!ignore_sym (sym)
   4537 	  && sect_syms[asect->index] == NULL)
   4538 	{
   4539 	  if (sym_is_global (abfd, asect->symbol))
   4540 	    num_globals++;
   4541 	  else
   4542 	    num_locals++;
   4543 	}
   4544     }
   4545 
   4546   /* Now sort the symbols so the local symbols are first.  */
   4547   amt = (num_locals + num_globals) * sizeof (asymbol *);
   4548   new_syms = (asymbol **) bfd_alloc (abfd, amt);
   4549   if (new_syms == NULL)
   4550     return false;
   4551 
   4552   unsigned int num_globals2 = 0;
   4553   unsigned int num_locals2 = 0;
   4554   for (idx = 0; idx < symcount; idx++)
   4555     {
   4556       asymbol *sym = syms[idx];
   4557       unsigned int i;
   4558 
   4559       if (ignore_sym (sym))
   4560 	continue;
   4561 
   4562       if (sym_is_global (abfd, sym))
   4563 	i = num_locals + num_globals2++;
   4564       else
   4565 	i = num_locals2++;
   4566       new_syms[i] = sym;
   4567       sym->udata.i = i + 1;
   4568     }
   4569   for (asect = abfd->sections; asect; asect = asect->next)
   4570     {
   4571       asymbol *sym = asect->symbol;
   4572       if (!ignore_sym (sym)
   4573 	  && sect_syms[asect->index] == NULL)
   4574 	{
   4575 	  unsigned int i;
   4576 
   4577 	  sect_syms[asect->index] = sym;
   4578 	  if (sym_is_global (abfd, sym))
   4579 	    i = num_locals + num_globals2++;
   4580 	  else
   4581 	    i = num_locals2++;
   4582 	  new_syms[i] = sym;
   4583 	  sym->udata.i = i + 1;
   4584 	}
   4585     }
   4586 
   4587   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4588 
   4589   *pnum_locals = num_locals;
   4590   return true;
   4591 }
   4592 
   4593 /* Assign a file position to a section, optionally aligning to the
   4594    required section alignment.  */
   4595 
   4596 file_ptr
   4597 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4598 					   file_ptr offset,
   4599 					   bool align,
   4600 					   unsigned char log_file_align)
   4601 {
   4602   if (i_shdrp->sh_addralign > 1)
   4603     {
   4604       file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
   4605 
   4606       if (align)
   4607 	offset = BFD_ALIGN (offset, salign);
   4608       else if (log_file_align)
   4609 	{
   4610 	  /* Heuristic: Cap alignment at log_file_align.  */
   4611 	  file_ptr falign = 1u << log_file_align;
   4612 
   4613 	  offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
   4614 	}
   4615     }
   4616   i_shdrp->sh_offset = offset;
   4617   if (i_shdrp->bfd_section != NULL)
   4618     i_shdrp->bfd_section->filepos = offset;
   4619   if (i_shdrp->sh_type != SHT_NOBITS)
   4620     offset += i_shdrp->sh_size;
   4621   return offset;
   4622 }
   4623 
   4624 /* Compute the file positions we are going to put the sections at, and
   4625    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4626    is not NULL, this is being called by the ELF backend linker.  */
   4627 
   4628 bool
   4629 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4630 					 struct bfd_link_info *link_info)
   4631 {
   4632   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4633   struct fake_section_arg fsargs;
   4634   bool failed;
   4635   struct elf_strtab_hash *strtab = NULL;
   4636   Elf_Internal_Shdr *shstrtab_hdr;
   4637   bool need_symtab;
   4638 
   4639   if (abfd->output_has_begun)
   4640     return true;
   4641 
   4642   /* Do any elf backend specific processing first.  */
   4643   if (bed->elf_backend_begin_write_processing)
   4644     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4645 
   4646   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4647     return false;
   4648 
   4649   fsargs.failed = false;
   4650   fsargs.link_info = link_info;
   4651   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4652   if (fsargs.failed)
   4653     return false;
   4654 
   4655   if (!assign_section_numbers (abfd, link_info))
   4656     return false;
   4657 
   4658   /* The backend linker builds symbol table information itself.  */
   4659   need_symtab = (link_info == NULL
   4660 		 && (bfd_get_symcount (abfd) > 0
   4661 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4662 			 == HAS_RELOC)));
   4663   if (need_symtab)
   4664     {
   4665       /* Non-zero if doing a relocatable link.  */
   4666       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4667 
   4668       if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
   4669 	return false;
   4670     }
   4671 
   4672   failed = false;
   4673   if (link_info == NULL)
   4674     {
   4675       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4676       if (failed)
   4677 	goto err_free_strtab;
   4678     }
   4679 
   4680   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4681   /* sh_name was set in init_file_header.  */
   4682   shstrtab_hdr->sh_type = SHT_STRTAB;
   4683   shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   4684   shstrtab_hdr->sh_addr = 0;
   4685   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4686   shstrtab_hdr->sh_entsize = 0;
   4687   shstrtab_hdr->sh_link = 0;
   4688   shstrtab_hdr->sh_info = 0;
   4689   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4690   shstrtab_hdr->sh_addralign = 1;
   4691 
   4692   if (!assign_file_positions_except_relocs (abfd, link_info))
   4693     goto err_free_strtab;
   4694 
   4695   if (strtab != NULL)
   4696     {
   4697       file_ptr off;
   4698       Elf_Internal_Shdr *hdr;
   4699 
   4700       off = elf_next_file_pos (abfd);
   4701 
   4702       hdr = & elf_symtab_hdr (abfd);
   4703       off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   4704 
   4705       if (elf_symtab_shndx_list (abfd) != NULL)
   4706 	{
   4707 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4708 	  if (hdr->sh_size != 0)
   4709 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   4710 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4711 	}
   4712 
   4713       hdr = &elf_tdata (abfd)->strtab_hdr;
   4714       off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   4715 
   4716       elf_next_file_pos (abfd) = off;
   4717 
   4718       /* Now that we know where the .strtab section goes, write it
   4719 	 out.  */
   4720       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4721 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4722 	goto err_free_strtab;
   4723       _bfd_elf_strtab_free (strtab);
   4724     }
   4725 
   4726   abfd->output_has_begun = true;
   4727   return true;
   4728 
   4729  err_free_strtab:
   4730   if (strtab != NULL)
   4731     _bfd_elf_strtab_free (strtab);
   4732   return false;
   4733 }
   4734 
   4735 /* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
   4736    function effectively returns whether --eh-frame-hdr is given on the
   4737    command line.  After size_dynamic_sections the result reflects
   4738    whether .eh_frame_hdr will actually be output (sizing isn't done
   4739    until ldemul_after_allocation).  */
   4740 
   4741 static asection *
   4742 elf_eh_frame_hdr (const struct bfd_link_info *info)
   4743 {
   4744   if (info != NULL && is_elf_hash_table (info->hash))
   4745     return elf_hash_table (info)->eh_info.hdr_sec;
   4746   return NULL;
   4747 }
   4748 
   4749 /* Make an initial estimate of the size of the program header.  If we
   4750    get the number wrong here, we'll redo section placement.  */
   4751 
   4752 static bfd_size_type
   4753 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4754 {
   4755   size_t segs;
   4756   asection *s, *s2;
   4757   const struct elf_backend_data *bed;
   4758 
   4759   /* Assume we will need exactly two PT_LOAD segments: one for text
   4760      and one for data.  */
   4761   segs = 2;
   4762 
   4763   s = bfd_get_section_by_name (abfd, ".interp");
   4764   s2 = bfd_get_section_by_name (abfd, ".dynamic");
   4765   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4766     {
   4767       ++segs;
   4768     }
   4769 
   4770   if (s2 != NULL && (s2->flags & SEC_LOAD) != 0)
   4771     {
   4772       /* We need a PT_DYNAMIC segment.  */
   4773       ++segs;
   4774     }
   4775 
   4776   if ((s != NULL && (s->flags & SEC_LOAD) != 0) ||
   4777       (s2 != NULL && (s2->flags & SEC_LOAD) != 0))
   4778     {
   4779       /*
   4780        * If either a PT_INTERP or PT_DYNAMIC segment is created,
   4781        * also create a PT_PHDR segment.
   4782        */
   4783       ++segs;
   4784     }
   4785 
   4786   if (info != NULL && info->relro)
   4787     {
   4788       /* We need a PT_GNU_RELRO segment.  */
   4789       ++segs;
   4790     }
   4791 
   4792   if (elf_eh_frame_hdr (info))
   4793     {
   4794       /* We need a PT_GNU_EH_FRAME segment.  */
   4795       ++segs;
   4796     }
   4797 
   4798   if (elf_stack_flags (abfd))
   4799     {
   4800       /* We need a PT_GNU_STACK segment.  */
   4801       ++segs;
   4802     }
   4803 
   4804   if (elf_sframe (abfd))
   4805     {
   4806       /* We need a PT_GNU_SFRAME segment.  */
   4807       ++segs;
   4808     }
   4809 
   4810   s = bfd_get_section_by_name (abfd,
   4811 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4812   if (s != NULL && s->size != 0)
   4813     {
   4814       /* We need a PT_GNU_PROPERTY segment.  */
   4815       ++segs;
   4816     }
   4817 
   4818   for (s = abfd->sections; s != NULL; s = s->next)
   4819     {
   4820       if ((s->flags & SEC_LOAD) != 0
   4821 	  && elf_section_type (s) == SHT_NOTE)
   4822 	{
   4823 	  unsigned int alignment_power;
   4824 	  /* We need a PT_NOTE segment.  */
   4825 	  ++segs;
   4826 	  /* Try to create just one PT_NOTE segment for all adjacent
   4827 	     loadable SHT_NOTE sections.  gABI requires that within a
   4828 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4829 	     each note should have the same alignment.  So we check
   4830 	     whether the sections are correctly aligned.  */
   4831 	  alignment_power = s->alignment_power;
   4832 	  while (s->next != NULL
   4833 		 && s->next->alignment_power == alignment_power
   4834 		 && (s->next->flags & SEC_LOAD) != 0
   4835 		 && elf_section_type (s->next) == SHT_NOTE)
   4836 	    s = s->next;
   4837 	}
   4838     }
   4839 
   4840   for (s = abfd->sections; s != NULL; s = s->next)
   4841     {
   4842       if (s->flags & SEC_THREAD_LOCAL)
   4843 	{
   4844 	  /* We need a PT_TLS segment.  */
   4845 	  ++segs;
   4846 	  break;
   4847 	}
   4848     }
   4849 
   4850   bed = get_elf_backend_data (abfd);
   4851 
   4852   if ((abfd->flags & D_PAGED) != 0
   4853       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4854     {
   4855       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4856       bfd_vma commonpagesize;
   4857       unsigned int page_align_power;
   4858 
   4859       if (info != NULL)
   4860 	commonpagesize = info->commonpagesize;
   4861       else
   4862 	commonpagesize = bed->commonpagesize;
   4863       page_align_power = bfd_log2 (commonpagesize);
   4864       for (s = abfd->sections; s != NULL; s = s->next)
   4865 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   4866 	  {
   4867 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   4868 	      {
   4869 		_bfd_error_handler
   4870 		  /* xgettext:c-format */
   4871 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   4872 		     "sh_info field: %d"),
   4873 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4874 		continue;
   4875 	      }
   4876 	    /* Align mbind section to page size.  */
   4877 	    if (s->alignment_power < page_align_power)
   4878 	      s->alignment_power = page_align_power;
   4879 	    segs ++;
   4880 	  }
   4881     }
   4882 
   4883   /* Let the backend count up any program headers it might need.  */
   4884   if (bed->elf_backend_additional_program_headers)
   4885     {
   4886       int a;
   4887 
   4888       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4889       if (a == -1)
   4890 	abort ();
   4891       segs += a;
   4892     }
   4893 
   4894   return segs * bed->s->sizeof_phdr;
   4895 }
   4896 
   4897 /* Find the segment that contains the output_section of section.  */
   4898 
   4899 Elf_Internal_Phdr *
   4900 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4901 {
   4902   struct elf_segment_map *m;
   4903   Elf_Internal_Phdr *p;
   4904 
   4905   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4906        m != NULL;
   4907        m = m->next, p++)
   4908     {
   4909       int i;
   4910 
   4911       for (i = m->count - 1; i >= 0; i--)
   4912 	if (m->sections[i] == section)
   4913 	  return p;
   4914     }
   4915 
   4916   return NULL;
   4917 }
   4918 
   4919 /* Create a mapping from a set of sections to a program segment.  */
   4920 
   4921 static struct elf_segment_map *
   4922 make_mapping (bfd *abfd,
   4923 	      asection **sections,
   4924 	      unsigned int from,
   4925 	      unsigned int to,
   4926 	      bool phdr)
   4927 {
   4928   struct elf_segment_map *m;
   4929   unsigned int i;
   4930   asection **hdrpp;
   4931   size_t amt;
   4932 
   4933   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   4934   amt += (to - from) * sizeof (asection *);
   4935   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   4936   if (m == NULL)
   4937     return NULL;
   4938   m->next = NULL;
   4939   m->p_type = PT_LOAD;
   4940   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   4941     m->sections[i - from] = *hdrpp;
   4942   m->count = to - from;
   4943 
   4944   if (from == 0 && phdr)
   4945     {
   4946       /* Include the headers in the first PT_LOAD segment.  */
   4947       m->includes_filehdr = 1;
   4948       m->includes_phdrs = 1;
   4949     }
   4950 
   4951   return m;
   4952 }
   4953 
   4954 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   4955    on failure.  */
   4956 
   4957 struct elf_segment_map *
   4958 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   4959 {
   4960   struct elf_segment_map *m;
   4961 
   4962   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   4963 					     sizeof (struct elf_segment_map));
   4964   if (m == NULL)
   4965     return NULL;
   4966   m->next = NULL;
   4967   m->p_type = PT_DYNAMIC;
   4968   m->count = 1;
   4969   m->sections[0] = dynsec;
   4970 
   4971   return m;
   4972 }
   4973 
   4974 /* Possibly add or remove segments from the segment map.  */
   4975 
   4976 static bool
   4977 elf_modify_segment_map (bfd *abfd,
   4978 			struct bfd_link_info *info,
   4979 			bool remove_empty_load)
   4980 {
   4981   struct elf_segment_map **m;
   4982   const struct elf_backend_data *bed;
   4983 
   4984   /* The placement algorithm assumes that non allocated sections are
   4985      not in PT_LOAD segments.  We ensure this here by removing such
   4986      sections from the segment map.  We also remove excluded
   4987      sections.  Finally, any PT_LOAD segment without sections is
   4988      removed.  */
   4989   m = &elf_seg_map (abfd);
   4990   while (*m)
   4991     {
   4992       unsigned int i, new_count;
   4993 
   4994       for (new_count = 0, i = 0; i < (*m)->count; i++)
   4995 	{
   4996 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   4997 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   4998 		  || (*m)->p_type != PT_LOAD))
   4999 	    {
   5000 	      (*m)->sections[new_count] = (*m)->sections[i];
   5001 	      new_count++;
   5002 	    }
   5003 	}
   5004       (*m)->count = new_count;
   5005 
   5006       if (remove_empty_load
   5007 	  && (*m)->p_type == PT_LOAD
   5008 	  && (*m)->count == 0
   5009 	  && !(*m)->includes_phdrs)
   5010 	*m = (*m)->next;
   5011       else
   5012 	m = &(*m)->next;
   5013     }
   5014 
   5015   bed = get_elf_backend_data (abfd);
   5016   if (bed->elf_backend_modify_segment_map != NULL)
   5017     {
   5018       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   5019 	return false;
   5020     }
   5021 
   5022   return true;
   5023 }
   5024 
   5025 #define IS_TBSS(s) \
   5026   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   5027 
   5028 /* Set up a mapping from BFD sections to program segments.  Update
   5029    NEED_LAYOUT if the section layout is changed.  */
   5030 
   5031 bool
   5032 _bfd_elf_map_sections_to_segments (bfd *abfd,
   5033 				   struct bfd_link_info *info,
   5034 				   bool *need_layout)
   5035 {
   5036   unsigned int count;
   5037   struct elf_segment_map *m;
   5038   asection **sections = NULL;
   5039   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5040   bool no_user_phdrs;
   5041 
   5042   no_user_phdrs = elf_seg_map (abfd) == NULL;
   5043 
   5044   if (info != NULL)
   5045     {
   5046       info->user_phdrs = !no_user_phdrs;
   5047 
   5048       /* Size the relative relocations if DT_RELR is enabled.  */
   5049       if (info->enable_dt_relr
   5050 	  && need_layout != NULL
   5051 	  && bed->size_relative_relocs
   5052 	  && !bed->size_relative_relocs (info, need_layout))
   5053 	info->callbacks->fatal
   5054 	  (_("%P: failed to size relative relocations\n"));
   5055     }
   5056 
   5057   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   5058     {
   5059       asection *s;
   5060       unsigned int i;
   5061       struct elf_segment_map *mfirst;
   5062       struct elf_segment_map **pm;
   5063       asection *last_hdr;
   5064       bfd_vma last_size;
   5065       unsigned int hdr_index;
   5066       bfd_vma maxpagesize;
   5067       asection **hdrpp;
   5068       bool phdr_in_segment;
   5069       bool writable;
   5070       bool executable;
   5071       unsigned int tls_count = 0;
   5072       asection *first_tls = NULL;
   5073       asection *first_mbind = NULL;
   5074       asection *dynsec, *eh_frame_hdr;
   5075       asection *sframe;
   5076       size_t amt;
   5077       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   5078       bfd_size_type phdr_size;  /* Octets/bytes.  */
   5079       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5080 
   5081       /* Select the allocated sections, and sort them.  */
   5082 
   5083       amt = bfd_count_sections (abfd) * sizeof (asection *);
   5084       sections = (asection **) bfd_malloc (amt);
   5085       if (sections == NULL)
   5086 	goto error_return;
   5087 
   5088       /* Calculate top address, avoiding undefined behaviour of shift
   5089 	 left operator when shift count is equal to size of type
   5090 	 being shifted.  */
   5091       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   5092       addr_mask = (addr_mask << 1) + 1;
   5093 
   5094       i = 0;
   5095       for (s = abfd->sections; s != NULL; s = s->next)
   5096 	{
   5097 	  if ((s->flags & SEC_ALLOC) != 0)
   5098 	    {
   5099 	      /* target_index is unused until bfd_elf_final_link
   5100 		 starts output of section symbols.  Use it to make
   5101 		 qsort stable.  */
   5102 	      s->target_index = i;
   5103 	      sections[i] = s;
   5104 	      ++i;
   5105 	      /* A wrapping section potentially clashes with header.  */
   5106 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   5107 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   5108 	    }
   5109 	}
   5110       BFD_ASSERT (i <= bfd_count_sections (abfd));
   5111       count = i;
   5112 
   5113       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   5114 
   5115       phdr_size = elf_program_header_size (abfd);
   5116       if (phdr_size == (bfd_size_type) -1)
   5117 	phdr_size = get_program_header_size (abfd, info);
   5118       phdr_size += bed->s->sizeof_ehdr;
   5119       /* phdr_size is compared to LMA values which are in bytes.  */
   5120       phdr_size /= opb;
   5121       if (info != NULL)
   5122 	maxpagesize = info->maxpagesize;
   5123       else
   5124 	maxpagesize = bed->maxpagesize;
   5125       if (maxpagesize == 0)
   5126 	maxpagesize = 1;
   5127       phdr_in_segment = info != NULL && info->load_phdrs;
   5128       if (count != 0
   5129 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   5130 	      >= (phdr_size & (maxpagesize - 1))))
   5131 	/* For compatibility with old scripts that may not be using
   5132 	   SIZEOF_HEADERS, add headers when it looks like space has
   5133 	   been left for them.  */
   5134 	phdr_in_segment = true;
   5135 
   5136       /* Build the mapping.  */
   5137       mfirst = NULL;
   5138       pm = &mfirst;
   5139 
   5140       /* If we have a .interp section, then create a PT_PHDR segment for
   5141 	 the program headers and a PT_INTERP segment for the .interp
   5142 	 section.  */
   5143       s = bfd_get_section_by_name (abfd, ".interp");
   5144       if (s != NULL && (s->flags & SEC_LOAD) == 0)
   5145       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   5146 	s = NULL;
   5147       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   5148       if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0)
   5149 	dynsec = NULL;
   5150 
   5151       if (s != NULL || dynsec != NULL)
   5152 	{
   5153 	  amt = sizeof (struct elf_segment_map);
   5154 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5155 	  if (m == NULL)
   5156 	    goto error_return;
   5157 	  m->next = NULL;
   5158 	  m->p_type = PT_PHDR;
   5159 	  m->p_flags = PF_R;
   5160 	  m->p_flags_valid = 1;
   5161 	  m->includes_phdrs = 1;
   5162 	  phdr_in_segment = true;
   5163 	  *pm = m;
   5164 	  pm = &m->next;
   5165 	}
   5166 
   5167       if (s != NULL)
   5168 	{
   5169 	  amt = sizeof (struct elf_segment_map);
   5170 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5171 	  if (m == NULL)
   5172 	    goto error_return;
   5173 	  m->next = NULL;
   5174 	  m->p_type = PT_INTERP;
   5175 	  m->count = 1;
   5176 	  m->sections[0] = s;
   5177 
   5178 	  *pm = m;
   5179 	  pm = &m->next;
   5180 	}
   5181 
   5182       /* Look through the sections.  We put sections in the same program
   5183 	 segment when the start of the second section can be placed within
   5184 	 a few bytes of the end of the first section.  */
   5185       last_hdr = NULL;
   5186       last_size = 0;
   5187       hdr_index = 0;
   5188       writable = false;
   5189       executable = false;
   5190 
   5191       if ((abfd->flags & D_PAGED) == 0)
   5192 	phdr_in_segment = false;
   5193 
   5194       /* Deal with -Ttext or something similar such that the first section
   5195 	 is not adjacent to the program headers.  This is an
   5196 	 approximation, since at this point we don't know exactly how many
   5197 	 program headers we will need.  */
   5198       if (phdr_in_segment && count > 0)
   5199 	{
   5200 	  bfd_vma phdr_lma;  /* Bytes.  */
   5201 	  bool separate_phdr = false;
   5202 
   5203 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   5204 	  if (info != NULL
   5205 	      && info->separate_code
   5206 	      && (sections[0]->flags & SEC_CODE) != 0)
   5207 	    {
   5208 	      /* If data sections should be separate from code and
   5209 		 thus not executable, and the first section is
   5210 		 executable then put the file and program headers in
   5211 		 their own PT_LOAD.  */
   5212 	      if (!info->one_rosegment)
   5213 		separate_phdr = true;
   5214 
   5215 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   5216 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   5217 		{
   5218 		  /* The file and program headers are currently on the
   5219 		     same page as the first section.  Put them on the
   5220 		     previous page if we can.  */
   5221 		  if (phdr_lma >= maxpagesize)
   5222 		    phdr_lma -= maxpagesize;
   5223 		  else
   5224 		    separate_phdr = false;
   5225 		}
   5226 	    }
   5227 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   5228 	      || (sections[0]->lma & addr_mask) < phdr_size)
   5229 	    /* If file and program headers would be placed at the end
   5230 	       of memory then it's probably better to omit them.  */
   5231 	    phdr_in_segment = false;
   5232 	  else if (phdr_lma < wrap_to)
   5233 	    /* If a section wraps around to where we'll be placing
   5234 	       file and program headers, then the headers will be
   5235 	       overwritten.  */
   5236 	    phdr_in_segment = false;
   5237 	  else if (separate_phdr)
   5238 	    {
   5239 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   5240 	      if (m == NULL)
   5241 		goto error_return;
   5242 	      m->p_paddr = phdr_lma * opb;
   5243 	      m->p_vaddr_offset
   5244 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   5245 	      m->p_paddr_valid = 1;
   5246 	      *pm = m;
   5247 	      pm = &m->next;
   5248 	      phdr_in_segment = false;
   5249 	    }
   5250 	}
   5251 
   5252       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   5253 	{
   5254 	  asection *hdr;
   5255 	  bool new_segment;
   5256 
   5257 	  hdr = *hdrpp;
   5258 
   5259 	  /* See if this section and the last one will fit in the same
   5260 	     segment.  */
   5261 
   5262 	  if (last_hdr == NULL)
   5263 	    {
   5264 	      /* If we don't have a segment yet, then we don't need a new
   5265 		 one (we build the last one after this loop).  */
   5266 	      new_segment = false;
   5267 	    }
   5268 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   5269 	    {
   5270 	      /* If this section has a different relation between the
   5271 		 virtual address and the load address, then we need a new
   5272 		 segment.  */
   5273 	      new_segment = true;
   5274 	    }
   5275 	  else if (hdr->lma < last_hdr->lma + last_size
   5276 		   || last_hdr->lma + last_size < last_hdr->lma)
   5277 	    {
   5278 	      /* If this section has a load address that makes it overlap
   5279 		 the previous section, then we need a new segment.  */
   5280 	      new_segment = true;
   5281 	    }
   5282 	  else if ((abfd->flags & D_PAGED) != 0
   5283 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   5284 		       == (hdr->lma & -maxpagesize)))
   5285 	    {
   5286 	      /* If we are demand paged then we can't map two disk
   5287 		 pages onto the same memory page.  */
   5288 	      new_segment = false;
   5289 	    }
   5290 	  /* In the next test we have to be careful when last_hdr->lma is close
   5291 	     to the end of the address space.  If the aligned address wraps
   5292 	     around to the start of the address space, then there are no more
   5293 	     pages left in memory and it is OK to assume that the current
   5294 	     section can be included in the current segment.  */
   5295 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5296 		    + maxpagesize > last_hdr->lma)
   5297 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5298 		       + maxpagesize <= hdr->lma))
   5299 	    {
   5300 	      /* If putting this section in this segment would force us to
   5301 		 skip a page in the segment, then we need a new segment.  */
   5302 	      new_segment = true;
   5303 	    }
   5304 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   5305 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   5306 	    {
   5307 	      /* We don't want to put a loaded section after a
   5308 		 nonloaded (ie. bss style) section in the same segment
   5309 		 as that will force the non-loaded section to be loaded.
   5310 		 Consider .tbss sections as loaded for this purpose.  */
   5311 	      new_segment = true;
   5312 	    }
   5313 	  else if ((abfd->flags & D_PAGED) == 0)
   5314 	    {
   5315 	      /* If the file is not demand paged, which means that we
   5316 		 don't require the sections to be correctly aligned in the
   5317 		 file, then there is no other reason for a new segment.  */
   5318 	      new_segment = false;
   5319 	    }
   5320 	  else if (info != NULL
   5321 		   && info->separate_code
   5322 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   5323 	    {
   5324 	      new_segment = true;
   5325 	    }
   5326 	  else if (! writable
   5327 		   && (hdr->flags & SEC_READONLY) == 0)
   5328 	    {
   5329 	      /* We don't want to put a writable section in a read only
   5330 		 segment.  */
   5331 	      new_segment = true;
   5332 	    }
   5333 	  else
   5334 	    {
   5335 	      /* Otherwise, we can use the same segment.  */
   5336 	      new_segment = false;
   5337 	    }
   5338 
   5339 	  /* Allow interested parties a chance to override our decision.  */
   5340 	  if (last_hdr != NULL
   5341 	      && info != NULL
   5342 	      && info->callbacks->override_segment_assignment != NULL)
   5343 	    new_segment
   5344 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   5345 							      last_hdr,
   5346 							      new_segment);
   5347 
   5348 	  if (! new_segment)
   5349 	    {
   5350 	      if ((hdr->flags & SEC_READONLY) == 0)
   5351 		writable = true;
   5352 	      if ((hdr->flags & SEC_CODE) != 0)
   5353 		executable = true;
   5354 	      last_hdr = hdr;
   5355 	      /* .tbss sections effectively have zero size.  */
   5356 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5357 	      continue;
   5358 	    }
   5359 
   5360 	  /* We need a new program segment.  We must create a new program
   5361 	     header holding all the sections from hdr_index until hdr.  */
   5362 
   5363 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5364 	  if (m == NULL)
   5365 	    goto error_return;
   5366 
   5367 	  *pm = m;
   5368 	  pm = &m->next;
   5369 
   5370 	  if ((hdr->flags & SEC_READONLY) == 0)
   5371 	    writable = true;
   5372 	  else
   5373 	    writable = false;
   5374 
   5375 	  if ((hdr->flags & SEC_CODE) == 0)
   5376 	    executable = false;
   5377 	  else
   5378 	    executable = true;
   5379 
   5380 	  last_hdr = hdr;
   5381 	  /* .tbss sections effectively have zero size.  */
   5382 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5383 	  hdr_index = i;
   5384 	  phdr_in_segment = false;
   5385 	}
   5386 
   5387       /* Create a final PT_LOAD program segment, but not if it's just
   5388 	 for .tbss.  */
   5389       if (last_hdr != NULL
   5390 	  && (i - hdr_index != 1
   5391 	      || !IS_TBSS (last_hdr)))
   5392 	{
   5393 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5394 	  if (m == NULL)
   5395 	    goto error_return;
   5396 
   5397 	  *pm = m;
   5398 	  pm = &m->next;
   5399 	}
   5400 
   5401       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   5402       if (dynsec != NULL)
   5403 	{
   5404 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   5405 	  if (m == NULL)
   5406 	    goto error_return;
   5407 	  *pm = m;
   5408 	  pm = &m->next;
   5409 	}
   5410 
   5411       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5412 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5413 	 because if we link together nonloadable .note sections and
   5414 	 loadable .note sections, we will generate two .note sections
   5415 	 in the output file.  */
   5416       for (s = abfd->sections; s != NULL; s = s->next)
   5417 	{
   5418 	  if ((s->flags & SEC_LOAD) != 0
   5419 	      && elf_section_type (s) == SHT_NOTE)
   5420 	    {
   5421 	      asection *s2;
   5422 	      unsigned int alignment_power = s->alignment_power;
   5423 
   5424 	      count = 1;
   5425 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5426 		{
   5427 		  if (s2->next->alignment_power == alignment_power
   5428 		      && (s2->next->flags & SEC_LOAD) != 0
   5429 		      && elf_section_type (s2->next) == SHT_NOTE
   5430 		      && align_power (s2->lma + s2->size / opb,
   5431 				      alignment_power)
   5432 		      == s2->next->lma)
   5433 		    count++;
   5434 		  else
   5435 		    break;
   5436 		}
   5437 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5438 	      amt += count * sizeof (asection *);
   5439 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5440 	      if (m == NULL)
   5441 		goto error_return;
   5442 	      m->next = NULL;
   5443 	      m->p_type = PT_NOTE;
   5444 	      m->count = count;
   5445 	      while (count > 1)
   5446 		{
   5447 		  m->sections[m->count - count--] = s;
   5448 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5449 		  s = s->next;
   5450 		}
   5451 	      m->sections[m->count - 1] = s;
   5452 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5453 	      *pm = m;
   5454 	      pm = &m->next;
   5455 	    }
   5456 	  if (s->flags & SEC_THREAD_LOCAL)
   5457 	    {
   5458 	      if (! tls_count)
   5459 		first_tls = s;
   5460 	      tls_count++;
   5461 	    }
   5462 	  if (first_mbind == NULL
   5463 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5464 	    first_mbind = s;
   5465 	}
   5466 
   5467       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5468       if (tls_count > 0)
   5469 	{
   5470 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5471 	  amt += tls_count * sizeof (asection *);
   5472 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5473 	  if (m == NULL)
   5474 	    goto error_return;
   5475 	  m->next = NULL;
   5476 	  m->p_type = PT_TLS;
   5477 	  m->count = tls_count;
   5478 	  /* Mandated PF_R.  */
   5479 	  m->p_flags = PF_R;
   5480 	  m->p_flags_valid = 1;
   5481 	  s = first_tls;
   5482 	  for (i = 0; i < tls_count; ++i)
   5483 	    {
   5484 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5485 		{
   5486 		  _bfd_error_handler
   5487 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5488 		  s = first_tls;
   5489 		  i = 0;
   5490 		  while (i < tls_count)
   5491 		    {
   5492 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5493 			{
   5494 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5495 			  i++;
   5496 			}
   5497 		      else
   5498 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5499 		      s = s->next;
   5500 		    }
   5501 		  bfd_set_error (bfd_error_bad_value);
   5502 		  goto error_return;
   5503 		}
   5504 	      m->sections[i] = s;
   5505 	      s = s->next;
   5506 	    }
   5507 
   5508 	  *pm = m;
   5509 	  pm = &m->next;
   5510 	}
   5511 
   5512       if (first_mbind
   5513 	  && (abfd->flags & D_PAGED) != 0
   5514 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5515 	for (s = first_mbind; s != NULL; s = s->next)
   5516 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5517 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5518 	    {
   5519 	      /* Mandated PF_R.  */
   5520 	      unsigned long p_flags = PF_R;
   5521 	      if ((s->flags & SEC_READONLY) == 0)
   5522 		p_flags |= PF_W;
   5523 	      if ((s->flags & SEC_CODE) != 0)
   5524 		p_flags |= PF_X;
   5525 
   5526 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5527 	      m = bfd_zalloc (abfd, amt);
   5528 	      if (m == NULL)
   5529 		goto error_return;
   5530 	      m->next = NULL;
   5531 	      m->p_type = (PT_GNU_MBIND_LO
   5532 			   + elf_section_data (s)->this_hdr.sh_info);
   5533 	      m->count = 1;
   5534 	      m->p_flags_valid = 1;
   5535 	      m->sections[0] = s;
   5536 	      m->p_flags = p_flags;
   5537 
   5538 	      *pm = m;
   5539 	      pm = &m->next;
   5540 	    }
   5541 
   5542       s = bfd_get_section_by_name (abfd,
   5543 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5544       if (s != NULL && s->size != 0)
   5545 	{
   5546 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5547 	  m = bfd_zalloc (abfd, amt);
   5548 	  if (m == NULL)
   5549 	    goto error_return;
   5550 	  m->next = NULL;
   5551 	  m->p_type = PT_GNU_PROPERTY;
   5552 	  m->count = 1;
   5553 	  m->p_flags_valid = 1;
   5554 	  m->sections[0] = s;
   5555 	  m->p_flags = PF_R;
   5556 	  *pm = m;
   5557 	  pm = &m->next;
   5558 	}
   5559 
   5560       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5561 	 segment.  */
   5562       eh_frame_hdr = elf_eh_frame_hdr (info);
   5563       if (eh_frame_hdr != NULL
   5564 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5565 	{
   5566 	  amt = sizeof (struct elf_segment_map);
   5567 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5568 	  if (m == NULL)
   5569 	    goto error_return;
   5570 	  m->next = NULL;
   5571 	  m->p_type = PT_GNU_EH_FRAME;
   5572 	  m->count = 1;
   5573 	  m->sections[0] = eh_frame_hdr->output_section;
   5574 
   5575 	  *pm = m;
   5576 	  pm = &m->next;
   5577 	}
   5578 
   5579       /* If there is a .sframe section, throw in a PT_GNU_SFRAME
   5580 	 segment.  */
   5581       sframe = elf_sframe (abfd);
   5582       if (sframe != NULL
   5583 	  && (sframe->output_section->flags & SEC_LOAD) != 0
   5584 	  && sframe->size != 0)
   5585 	{
   5586 	  amt = sizeof (struct elf_segment_map);
   5587 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5588 	  if (m == NULL)
   5589 	    goto error_return;
   5590 	  m->next = NULL;
   5591 	  m->p_type = PT_GNU_SFRAME;
   5592 	  m->count = 1;
   5593 	  m->sections[0] = sframe->output_section;
   5594 
   5595 	  *pm = m;
   5596 	  pm = &m->next;
   5597 	}
   5598 
   5599       if (elf_stack_flags (abfd))
   5600 	{
   5601 	  amt = sizeof (struct elf_segment_map);
   5602 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5603 	  if (m == NULL)
   5604 	    goto error_return;
   5605 	  m->next = NULL;
   5606 	  m->p_type = PT_GNU_STACK;
   5607 	  m->p_flags = elf_stack_flags (abfd);
   5608 	  m->p_align = bed->stack_align;
   5609 	  m->p_flags_valid = 1;
   5610 	  m->p_align_valid = m->p_align != 0;
   5611 	  if (info->stacksize > 0)
   5612 	    {
   5613 	      m->p_size = info->stacksize;
   5614 	      m->p_size_valid = 1;
   5615 	    }
   5616 
   5617 	  *pm = m;
   5618 	  pm = &m->next;
   5619 	}
   5620 
   5621       if (info != NULL && info->relro)
   5622 	{
   5623 	  for (m = mfirst; m != NULL; m = m->next)
   5624 	    {
   5625 	      if (m->p_type == PT_LOAD
   5626 		  && m->count != 0
   5627 		  && m->sections[0]->vma >= info->relro_start
   5628 		  && m->sections[0]->vma < info->relro_end)
   5629 		{
   5630 		  i = m->count;
   5631 		  while (--i != (unsigned) -1)
   5632 		    {
   5633 		      if (m->sections[i]->size > 0
   5634 			  && (m->sections[i]->flags & SEC_LOAD) != 0
   5635 			  && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
   5636 			break;
   5637 		    }
   5638 
   5639 		  if (i != (unsigned) -1)
   5640 		    break;
   5641 		}
   5642 	    }
   5643 
   5644 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5645 	  if (m != NULL)
   5646 	    {
   5647 	      amt = sizeof (struct elf_segment_map);
   5648 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5649 	      if (m == NULL)
   5650 		goto error_return;
   5651 	      m->next = NULL;
   5652 	      m->p_type = PT_GNU_RELRO;
   5653 	      *pm = m;
   5654 	      pm = &m->next;
   5655 	    }
   5656 	}
   5657 
   5658       free (sections);
   5659       elf_seg_map (abfd) = mfirst;
   5660     }
   5661 
   5662   if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
   5663     return false;
   5664 
   5665   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5666     ++count;
   5667   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5668 
   5669   return true;
   5670 
   5671  error_return:
   5672   free (sections);
   5673   return false;
   5674 }
   5675 
   5676 /* Sort sections by address.  */
   5677 
   5678 static int
   5679 elf_sort_sections (const void *arg1, const void *arg2)
   5680 {
   5681   const asection *sec1 = *(const asection **) arg1;
   5682   const asection *sec2 = *(const asection **) arg2;
   5683   bfd_size_type size1, size2;
   5684 
   5685   /* Sort by LMA first, since this is the address used to
   5686      place the section into a segment.  */
   5687   if (sec1->lma < sec2->lma)
   5688     return -1;
   5689   else if (sec1->lma > sec2->lma)
   5690     return 1;
   5691 
   5692   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5693      the same, and this will do nothing.  */
   5694   if (sec1->vma < sec2->vma)
   5695     return -1;
   5696   else if (sec1->vma > sec2->vma)
   5697     return 1;
   5698 
   5699   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5700 
   5701 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5702 		  && (x)->size != 0)
   5703 
   5704   if (TOEND (sec1))
   5705     {
   5706       if (!TOEND (sec2))
   5707 	return 1;
   5708     }
   5709   else if (TOEND (sec2))
   5710     return -1;
   5711 
   5712 #undef TOEND
   5713 
   5714   /* Sort by size, to put zero sized sections
   5715      before others at the same address.  */
   5716 
   5717   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5718   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5719 
   5720   if (size1 < size2)
   5721     return -1;
   5722   if (size1 > size2)
   5723     return 1;
   5724 
   5725   return sec1->target_index - sec2->target_index;
   5726 }
   5727 
   5728 /* This qsort comparison functions sorts PT_LOAD segments first and
   5729    by p_paddr, for assign_file_positions_for_load_sections.  */
   5730 
   5731 static int
   5732 elf_sort_segments (const void *arg1, const void *arg2)
   5733 {
   5734   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5735   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5736 
   5737   if (m1->p_type != m2->p_type)
   5738     {
   5739       if (m1->p_type == PT_NULL)
   5740 	return 1;
   5741       if (m2->p_type == PT_NULL)
   5742 	return -1;
   5743       return m1->p_type < m2->p_type ? -1 : 1;
   5744     }
   5745   if (m1->includes_filehdr != m2->includes_filehdr)
   5746     return m1->includes_filehdr ? -1 : 1;
   5747   if (m1->no_sort_lma != m2->no_sort_lma)
   5748     return m1->no_sort_lma ? -1 : 1;
   5749   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5750     {
   5751       bfd_vma lma1, lma2;  /* Octets.  */
   5752       lma1 = 0;
   5753       if (m1->p_paddr_valid)
   5754 	lma1 = m1->p_paddr;
   5755       else if (m1->count != 0)
   5756 	{
   5757 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5758 						  m1->sections[0]);
   5759 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5760 	}
   5761       lma2 = 0;
   5762       if (m2->p_paddr_valid)
   5763 	lma2 = m2->p_paddr;
   5764       else if (m2->count != 0)
   5765 	{
   5766 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5767 						  m2->sections[0]);
   5768 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5769 	}
   5770       if (lma1 != lma2)
   5771 	return lma1 < lma2 ? -1 : 1;
   5772     }
   5773   if (m1->idx != m2->idx)
   5774     return m1->idx < m2->idx ? -1 : 1;
   5775   return 0;
   5776 }
   5777 
   5778 /* Ian Lance Taylor writes:
   5779 
   5780    We shouldn't be using % with a negative signed number.  That's just
   5781    not good.  We have to make sure either that the number is not
   5782    negative, or that the number has an unsigned type.  When the types
   5783    are all the same size they wind up as unsigned.  When file_ptr is a
   5784    larger signed type, the arithmetic winds up as signed long long,
   5785    which is wrong.
   5786 
   5787    What we're trying to say here is something like ``increase OFF by
   5788    the least amount that will cause it to be equal to the VMA modulo
   5789    the page size.''  */
   5790 /* In other words, something like:
   5791 
   5792    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5793    off_offset = off % bed->maxpagesize;
   5794    if (vma_offset < off_offset)
   5795      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5796    else
   5797      adjustment = vma_offset - off_offset;
   5798 
   5799    which can be collapsed into the expression below.  */
   5800 
   5801 static file_ptr
   5802 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5803 {
   5804   /* PR binutils/16199: Handle an alignment of zero.  */
   5805   if (maxpagesize == 0)
   5806     maxpagesize = 1;
   5807   return ((vma - off) % maxpagesize);
   5808 }
   5809 
   5810 static void
   5811 print_segment_map (const struct elf_segment_map *m)
   5812 {
   5813   unsigned int j;
   5814   const char *pt = get_segment_type (m->p_type);
   5815   char buf[32];
   5816 
   5817   if (pt == NULL)
   5818     {
   5819       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5820 	sprintf (buf, "LOPROC+%7.7x",
   5821 		 (unsigned int) (m->p_type - PT_LOPROC));
   5822       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5823 	sprintf (buf, "LOOS+%7.7x",
   5824 		 (unsigned int) (m->p_type - PT_LOOS));
   5825       else
   5826 	snprintf (buf, sizeof (buf), "%8.8x",
   5827 		  (unsigned int) m->p_type);
   5828       pt = buf;
   5829     }
   5830   fflush (stdout);
   5831   fprintf (stderr, "%s:", pt);
   5832   for (j = 0; j < m->count; j++)
   5833     fprintf (stderr, " %s", m->sections [j]->name);
   5834   putc ('\n',stderr);
   5835   fflush (stderr);
   5836 }
   5837 
   5838 /* Assign file positions to the sections based on the mapping from
   5839    sections to segments.  This function also sets up some fields in
   5840    the file header.  */
   5841 
   5842 static bool
   5843 assign_file_positions_for_load_sections (bfd *abfd,
   5844 					 struct bfd_link_info *link_info)
   5845 {
   5846   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   5847   struct elf_segment_map *m;
   5848   struct elf_segment_map *phdr_load_seg;
   5849   Elf_Internal_Phdr *phdrs;
   5850   Elf_Internal_Phdr *p;
   5851   file_ptr off;  /* Octets.  */
   5852   bfd_size_type maxpagesize;
   5853   unsigned int alloc, actual;
   5854   unsigned int i, j;
   5855   struct elf_segment_map **sorted_seg_map;
   5856   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5857 
   5858   if (link_info == NULL
   5859       && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5860     return false;
   5861 
   5862   alloc = 0;
   5863   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5864     m->idx = alloc++;
   5865 
   5866   if (alloc)
   5867     {
   5868       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5869       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5870     }
   5871   else
   5872     {
   5873       /* PR binutils/12467.  */
   5874       elf_elfheader (abfd)->e_phoff = 0;
   5875       elf_elfheader (abfd)->e_phentsize = 0;
   5876     }
   5877 
   5878   elf_elfheader (abfd)->e_phnum = alloc;
   5879 
   5880   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5881     {
   5882       actual = alloc;
   5883       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5884     }
   5885   else
   5886     {
   5887       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   5888       BFD_ASSERT (elf_program_header_size (abfd)
   5889 		  == actual * bed->s->sizeof_phdr);
   5890       BFD_ASSERT (actual >= alloc);
   5891     }
   5892 
   5893   if (alloc == 0)
   5894     {
   5895       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5896       return true;
   5897     }
   5898 
   5899   /* We're writing the size in elf_program_header_size (abfd),
   5900      see assign_file_positions_except_relocs, so make sure we have
   5901      that amount allocated, with trailing space cleared.
   5902      The variable alloc contains the computed need, while
   5903      elf_program_header_size (abfd) contains the size used for the
   5904      layout.
   5905      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5906      where the layout is forced to according to a larger size in the
   5907      last iterations for the testcase ld-elf/header.  */
   5908   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   5909 			     + alloc * sizeof (*sorted_seg_map)));
   5910   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   5911   elf_tdata (abfd)->phdr = phdrs;
   5912   if (phdrs == NULL)
   5913     return false;
   5914 
   5915   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   5916     {
   5917       sorted_seg_map[j] = m;
   5918       /* If elf_segment_map is not from map_sections_to_segments, the
   5919 	 sections may not be correctly ordered.  NOTE: sorting should
   5920 	 not be done to the PT_NOTE section of a corefile, which may
   5921 	 contain several pseudo-sections artificially created by bfd.
   5922 	 Sorting these pseudo-sections breaks things badly.  */
   5923       if (m->count > 1
   5924 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5925 	       && m->p_type == PT_NOTE))
   5926 	{
   5927 	  for (i = 0; i < m->count; i++)
   5928 	    m->sections[i]->target_index = i;
   5929 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5930 		 elf_sort_sections);
   5931 	}
   5932     }
   5933   if (alloc > 1)
   5934     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   5935 	   elf_sort_segments);
   5936 
   5937   maxpagesize = 1;
   5938   if ((abfd->flags & D_PAGED) != 0)
   5939     {
   5940       if (link_info != NULL)
   5941 	maxpagesize = link_info->maxpagesize;
   5942       else
   5943 	maxpagesize = bed->maxpagesize;
   5944     }
   5945 
   5946   /* Sections must map to file offsets past the ELF file header.  */
   5947   off = bed->s->sizeof_ehdr;
   5948   /* And if one of the PT_LOAD headers doesn't include the program
   5949      headers then we'll be mapping program headers in the usual
   5950      position after the ELF file header.  */
   5951   phdr_load_seg = NULL;
   5952   for (j = 0; j < alloc; j++)
   5953     {
   5954       m = sorted_seg_map[j];
   5955       if (m->p_type != PT_LOAD)
   5956 	break;
   5957       if (m->includes_phdrs)
   5958 	{
   5959 	  phdr_load_seg = m;
   5960 	  break;
   5961 	}
   5962     }
   5963   if (phdr_load_seg == NULL)
   5964     off += actual * bed->s->sizeof_phdr;
   5965 
   5966   for (j = 0; j < alloc; j++)
   5967     {
   5968       asection **secpp;
   5969       bfd_vma off_adjust;  /* Octets.  */
   5970       bool no_contents;
   5971       bfd_size_type align_pagesize;
   5972 
   5973       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   5974 	 number of sections with contents contributing to both p_filesz
   5975 	 and p_memsz, followed by a number of sections with no contents
   5976 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   5977 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   5978       m = sorted_seg_map[j];
   5979       p = phdrs + m->idx;
   5980       p->p_type = m->p_type;
   5981       p->p_flags = m->p_flags;
   5982 
   5983       if (m->count == 0)
   5984 	p->p_vaddr = m->p_vaddr_offset * opb;
   5985       else
   5986 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   5987 
   5988       if (m->p_paddr_valid)
   5989 	p->p_paddr = m->p_paddr;
   5990       else if (m->count == 0)
   5991 	p->p_paddr = 0;
   5992       else
   5993 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   5994 
   5995       align_pagesize = 0;
   5996       if (p->p_type == PT_LOAD
   5997 	  && (abfd->flags & D_PAGED) != 0)
   5998 	{
   5999 	  /* p_align in demand paged PT_LOAD segments effectively stores
   6000 	     the maximum page size.  When copying an executable with
   6001 	     objcopy, we set m->p_align from the input file.  Use this
   6002 	     value for maxpagesize rather than bed->maxpagesize, which
   6003 	     may be different.  Note that we use maxpagesize for PT_TLS
   6004 	     segment alignment later in this function, so we are relying
   6005 	     on at least one PT_LOAD segment appearing before a PT_TLS
   6006 	     segment.  */
   6007 	  if (m->p_align_valid)
   6008 	    maxpagesize = m->p_align;
   6009 	  else if (bed->p_align != 0
   6010 		   && (link_info == NULL
   6011 		       || !link_info->maxpagesize_is_set))
   6012 	    /* We will lay out this binary using maxpagesize but set
   6013 	       p->p_align later to the possibly smaller bed->p_align.
   6014 	       The run-time loader will then be able to load this
   6015 	       binary when the system page size is maxpagesize, but if
   6016 	       the system page size is smaller can use p->p_align.
   6017 	       In either case p->p_align will be increased if
   6018 	       necessary to match section alignment.  */
   6019 	    align_pagesize = bed->p_align;
   6020 
   6021 	  p->p_align = maxpagesize;
   6022 	}
   6023       else if (m->p_align_valid)
   6024 	p->p_align = m->p_align;
   6025       else if (m->count == 0)
   6026 	p->p_align = 1 << bed->s->log_file_align;
   6027 
   6028       if (m == phdr_load_seg)
   6029 	off += actual * bed->s->sizeof_phdr;
   6030 
   6031       no_contents = false;
   6032       off_adjust = 0;
   6033       if (p->p_type == PT_LOAD
   6034 	  && m->count > 0)
   6035 	{
   6036 	  bfd_size_type align;  /* Bytes.  */
   6037 	  unsigned int align_power = 0;
   6038 
   6039 	  if (m->p_align_valid)
   6040 	    align = p->p_align;
   6041 	  else
   6042 	    {
   6043 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6044 		{
   6045 		  unsigned int secalign;
   6046 
   6047 		  secalign = bfd_section_alignment (*secpp);
   6048 		  if (secalign > align_power)
   6049 		    align_power = secalign;
   6050 		}
   6051 	      align = (bfd_size_type) 1 << align_power;
   6052 	      /* If a section requires alignment higher than the
   6053 		 minimum p_align value, don't reduce a maxpagesize
   6054 		 p->p_align set earlier in this function.  */
   6055 	      if (align > bed->p_align)
   6056 		align_pagesize = 0;
   6057 	      if (align < maxpagesize)
   6058 		align = maxpagesize;
   6059 	      else
   6060 		{
   6061 		  /* If a section requires alignment higher than the
   6062 		     maximum page size, set p_align to the section
   6063 		     alignment.  */
   6064 		  if ((abfd->flags & D_PAGED) != 0)
   6065 		    p->p_align = align;
   6066 		}
   6067 	    }
   6068 
   6069 	  for (i = 0; i < m->count; i++)
   6070 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   6071 	      /* If we aren't making room for this section, then
   6072 		 it must be SHT_NOBITS regardless of what we've
   6073 		 set via struct bfd_elf_special_section.  */
   6074 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   6075 
   6076 	  /* Find out whether this segment contains any loadable
   6077 	     sections.  */
   6078 	  no_contents = true;
   6079 	  for (i = 0; i < m->count; i++)
   6080 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   6081 	      {
   6082 		no_contents = false;
   6083 		break;
   6084 	      }
   6085 
   6086 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   6087 
   6088 	  /* Broken hardware and/or kernel require that files do not
   6089 	     map the same page with different permissions on some hppa
   6090 	     processors.  */
   6091 	  if (j != 0
   6092 	      && (abfd->flags & D_PAGED) != 0
   6093 	      && bed->no_page_alias
   6094 	      && (off & (maxpagesize - 1)) != 0
   6095 	      && ((off & -maxpagesize)
   6096 		  == ((off + off_adjust) & -maxpagesize)))
   6097 	    off_adjust += maxpagesize;
   6098 	  off += off_adjust;
   6099 	  if (no_contents)
   6100 	    {
   6101 	      /* We shouldn't need to align the segment on disk since
   6102 		 the segment doesn't need file space, but the gABI
   6103 		 arguably requires the alignment and glibc ld.so
   6104 		 checks it.  So to comply with the alignment
   6105 		 requirement but not waste file space, we adjust
   6106 		 p_offset for just this segment.  (OFF_ADJUST is
   6107 		 subtracted from OFF later.)  This may put p_offset
   6108 		 past the end of file, but that shouldn't matter.  */
   6109 	    }
   6110 	  else
   6111 	    off_adjust = 0;
   6112 	}
   6113       /* Make sure the .dynamic section is the first section in the
   6114 	 PT_DYNAMIC segment.  */
   6115       else if (p->p_type == PT_DYNAMIC
   6116 	       && m->count > 1
   6117 	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
   6118 	{
   6119 	  _bfd_error_handler
   6120 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   6121 	       " is not the .dynamic section"),
   6122 	     abfd);
   6123 	  bfd_set_error (bfd_error_bad_value);
   6124 	  return false;
   6125 	}
   6126       /* Set the note section type to SHT_NOTE.  */
   6127       else if (p->p_type == PT_NOTE)
   6128 	for (i = 0; i < m->count; i++)
   6129 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   6130 
   6131       if (m->includes_filehdr)
   6132 	{
   6133 	  if (!m->p_flags_valid)
   6134 	    p->p_flags |= PF_R;
   6135 	  p->p_filesz = bed->s->sizeof_ehdr;
   6136 	  p->p_memsz = bed->s->sizeof_ehdr;
   6137 	  if (p->p_type == PT_LOAD)
   6138 	    {
   6139 	      if (m->count > 0)
   6140 		{
   6141 		  if (p->p_vaddr < (bfd_vma) off
   6142 		      || (!m->p_paddr_valid
   6143 			  && p->p_paddr < (bfd_vma) off))
   6144 		    {
   6145 		      _bfd_error_handler
   6146 			(_("%pB: not enough room for program headers,"
   6147 			   " try linking with -N"),
   6148 			 abfd);
   6149 		      bfd_set_error (bfd_error_bad_value);
   6150 		      return false;
   6151 		    }
   6152 		  p->p_vaddr -= off;
   6153 		  if (!m->p_paddr_valid)
   6154 		    p->p_paddr -= off;
   6155 		}
   6156 	    }
   6157 	  else if (sorted_seg_map[0]->includes_filehdr)
   6158 	    {
   6159 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   6160 	      p->p_vaddr = filehdr->p_vaddr;
   6161 	      if (!m->p_paddr_valid)
   6162 		p->p_paddr = filehdr->p_paddr;
   6163 	    }
   6164 	}
   6165 
   6166       if (m->includes_phdrs)
   6167 	{
   6168 	  if (!m->p_flags_valid)
   6169 	    p->p_flags |= PF_R;
   6170 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   6171 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   6172 	  if (!m->includes_filehdr)
   6173 	    {
   6174 	      if (p->p_type == PT_LOAD)
   6175 		{
   6176 		  p->p_offset = off - actual * bed->s->sizeof_phdr;
   6177 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   6178 		  if (m->count > 0)
   6179 		    {
   6180 		      p->p_vaddr -= off - p->p_offset;
   6181 		      if (!m->p_paddr_valid)
   6182 			p->p_paddr -= off - p->p_offset;
   6183 		    }
   6184 		}
   6185 	      else if (phdr_load_seg != NULL)
   6186 		{
   6187 		  /* Also set PT_PHDR to match phdr_load_seg.  We've
   6188 		     sorted segments so that phdr_load_seg will
   6189 		     already be set by the code immediately above.  */
   6190 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   6191 		  bfd_vma phdr_off = 0;  /* Octets.  */
   6192 		  if (phdr_load_seg->includes_filehdr)
   6193 		    phdr_off = bed->s->sizeof_ehdr;
   6194 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   6195 		  if (!m->p_paddr_valid)
   6196 		    p->p_paddr = phdr->p_paddr + phdr_off;
   6197 		  p->p_offset = phdr->p_offset + phdr_off;
   6198 		}
   6199 	      else
   6200 		p->p_offset = bed->s->sizeof_ehdr;
   6201 	    }
   6202 	}
   6203 
   6204       if (p->p_type == PT_LOAD
   6205 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   6206 	{
   6207 	  if (!m->includes_filehdr && !m->includes_phdrs)
   6208 	    {
   6209 	      p->p_offset = off;
   6210 	      if (no_contents)
   6211 		{
   6212 		  /* Put meaningless p_offset for PT_LOAD segments
   6213 		     without file contents somewhere within the first
   6214 		     page, in an attempt to not point past EOF.  */
   6215 		  bfd_size_type align = maxpagesize;
   6216 		  if (align < p->p_align)
   6217 		    align = p->p_align;
   6218 		  if (align < 1)
   6219 		    align = 1;
   6220 		  /* Avoid p_offset of zero, which might be wrongly
   6221 		     interpreted as the segment being the first one,
   6222 		     containing the file header.  PR32763.  */
   6223 		  p->p_offset = (off + align - 1) % align + 1;
   6224 		}
   6225 	    }
   6226 	  else
   6227 	    {
   6228 	      file_ptr adjust;  /* Octets.  */
   6229 
   6230 	      adjust = off - (p->p_offset + p->p_filesz);
   6231 	      if (!no_contents)
   6232 		p->p_filesz += adjust;
   6233 	      p->p_memsz += adjust;
   6234 	    }
   6235 	}
   6236 
   6237       if (align_pagesize)
   6238 	p->p_align = align_pagesize;
   6239 
   6240       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   6241 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   6242 	 core files, for sections in PT_NOTE segments.
   6243 	 assign_file_positions_for_non_load_sections will set filepos
   6244 	 for other sections and update p_filesz for other segments.  */
   6245       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6246 	{
   6247 	  asection *sec;
   6248 	  bfd_size_type align;
   6249 	  Elf_Internal_Shdr *this_hdr;
   6250 
   6251 	  sec = *secpp;
   6252 	  this_hdr = &elf_section_data (sec)->this_hdr;
   6253 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   6254 
   6255 	  if ((p->p_type == PT_LOAD
   6256 	       || p->p_type == PT_TLS)
   6257 	      && (this_hdr->sh_type != SHT_NOBITS
   6258 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   6259 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   6260 			  || p->p_type == PT_TLS))))
   6261 	    {
   6262 	      bfd_vma p_start = p->p_paddr;		/* Octets.  */
   6263 	      bfd_vma p_end = p_start + p->p_memsz;	/* Octets.  */
   6264 	      bfd_vma s_start = sec->lma * opb;		/* Octets.  */
   6265 	      bfd_vma adjust = s_start - p_end;		/* Octets.  */
   6266 
   6267 	      if (adjust != 0
   6268 		  && (s_start < p_end
   6269 		      || p_end < p_start))
   6270 		{
   6271 		  _bfd_error_handler
   6272 		    /* xgettext:c-format */
   6273 		    (_("%pB: section %pA lma %#" PRIx64
   6274 		       " adjusted to %#" PRIx64),
   6275 		     abfd, sec, (uint64_t) s_start / opb,
   6276 		     (uint64_t) p_end / opb);
   6277 		  adjust = 0;
   6278 		  sec->lma = p_end / opb;
   6279 		}
   6280 	      p->p_memsz += adjust;
   6281 
   6282 	      if (p->p_type == PT_LOAD)
   6283 		{
   6284 		  if (this_hdr->sh_type != SHT_NOBITS)
   6285 		    {
   6286 		      off_adjust = 0;
   6287 		      if (p->p_filesz + adjust < p->p_memsz)
   6288 			{
   6289 			  /* We have a PROGBITS section following NOBITS ones.
   6290 			     Allocate file space for the NOBITS section(s).
   6291 			     We don't need to write out the zeros, posix
   6292 			     fseek past the end of data already written
   6293 			     followed by a write at that location is
   6294 			     guaranteed to result in zeros being read
   6295 			     from the gap.  */
   6296 			  adjust = p->p_memsz - p->p_filesz;
   6297 			}
   6298 		    }
   6299 		  /* We only adjust sh_offset in SHT_NOBITS sections
   6300 		     as would seem proper for their address when the
   6301 		     section is first in the segment.  sh_offset
   6302 		     doesn't really have any significance for
   6303 		     SHT_NOBITS anyway, apart from a notional position
   6304 		     relative to other sections.  Historically we
   6305 		     didn't bother with adjusting sh_offset and some
   6306 		     programs depend on it not being adjusted.  See
   6307 		     pr12921 and pr25662.  */
   6308 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   6309 		    {
   6310 		      off += adjust;
   6311 		      if (this_hdr->sh_type == SHT_NOBITS)
   6312 			off_adjust += adjust;
   6313 		    }
   6314 		}
   6315 	      if (this_hdr->sh_type != SHT_NOBITS)
   6316 		p->p_filesz += adjust;
   6317 	    }
   6318 
   6319 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   6320 	    {
   6321 	      /* The section at i == 0 is the one that actually contains
   6322 		 everything.  */
   6323 	      if (i == 0)
   6324 		{
   6325 		  this_hdr->sh_offset = sec->filepos = off;
   6326 		  off += this_hdr->sh_size;
   6327 		  p->p_filesz = this_hdr->sh_size;
   6328 		  p->p_memsz = 0;
   6329 		  p->p_align = 1;
   6330 		}
   6331 	      else
   6332 		{
   6333 		  /* The rest are fake sections that shouldn't be written.  */
   6334 		  sec->filepos = 0;
   6335 		  sec->size = 0;
   6336 		  sec->flags = 0;
   6337 		  continue;
   6338 		}
   6339 	    }
   6340 	  else
   6341 	    {
   6342 	      if (this_hdr->sh_type == SHT_NOBITS
   6343 		  && (this_hdr->sh_flags & SHF_TLS) != 0
   6344 		  && this_hdr->sh_offset == 0)
   6345 		{
   6346 		  /* Set sh_offset for .tbss sections to their nominal
   6347 		     offset after aligning.  They are not loaded from
   6348 		     disk so the value doesn't really matter, except
   6349 		     when the .tbss section is the first one in a
   6350 		     PT_TLS segment.  In that case it sets the
   6351 		     p_offset for the PT_TLS segment, which according
   6352 		     to the ELF gABI ought to satisfy
   6353 		     p_offset % p_align == p_vaddr % p_align.  */
   6354 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   6355 							  off, align);
   6356 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   6357 		}
   6358 	      else if (p->p_type == PT_LOAD)
   6359 		{
   6360 		  this_hdr->sh_offset = sec->filepos = off;
   6361 		  if (this_hdr->sh_type != SHT_NOBITS)
   6362 		    off += this_hdr->sh_size;
   6363 		}
   6364 
   6365 	      if (this_hdr->sh_type != SHT_NOBITS)
   6366 		{
   6367 		  p->p_filesz += this_hdr->sh_size;
   6368 		  /* A load section without SHF_ALLOC is something like
   6369 		     a note section in a PT_NOTE segment.  These take
   6370 		     file space but are not loaded into memory.  */
   6371 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6372 		    p->p_memsz += this_hdr->sh_size;
   6373 		}
   6374 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6375 		{
   6376 		  if (p->p_type == PT_TLS)
   6377 		    p->p_memsz += this_hdr->sh_size;
   6378 
   6379 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   6380 		     normal segments.  */
   6381 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   6382 		    p->p_memsz += this_hdr->sh_size;
   6383 		}
   6384 
   6385 	      if (align > p->p_align
   6386 		  && !m->p_align_valid
   6387 		  && (p->p_type != PT_LOAD
   6388 		      || (abfd->flags & D_PAGED) == 0))
   6389 		p->p_align = align;
   6390 	    }
   6391 
   6392 	  if (!m->p_flags_valid)
   6393 	    {
   6394 	      p->p_flags |= PF_R;
   6395 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   6396 		p->p_flags |= PF_X;
   6397 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   6398 		p->p_flags |= PF_W;
   6399 	    }
   6400 	}
   6401 
   6402       off -= off_adjust;
   6403 
   6404       /* PR ld/20815 - Check that the program header segment, if
   6405 	 present, will be loaded into memory.  */
   6406       if (p->p_type == PT_PHDR
   6407 	  && phdr_load_seg == NULL
   6408 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   6409 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   6410 	{
   6411 	  /* The fix for this error is usually to edit the linker script being
   6412 	     used and set up the program headers manually.  Either that or
   6413 	     leave room for the headers at the start of the SECTIONS.  */
   6414 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   6415 				" by LOAD segment"),
   6416 			      abfd);
   6417 	  if (link_info == NULL)
   6418 	    return false;
   6419 	  /* Arrange for the linker to exit with an error, deleting
   6420 	     the output file unless --noinhibit-exec is given.  */
   6421 	  link_info->callbacks->info ("%X");
   6422 	}
   6423 
   6424       /* Check that all sections are in a PT_LOAD segment.
   6425 	 Don't check funky gdb generated core files.  */
   6426       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6427 	{
   6428 	  bool check_vma = true;
   6429 
   6430 	  for (i = 1; i < m->count; i++)
   6431 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6432 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6433 				       ->this_hdr), p) != 0
   6434 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6435 				       ->this_hdr), p) != 0)
   6436 	      {
   6437 		/* Looks like we have overlays packed into the segment.  */
   6438 		check_vma = false;
   6439 		break;
   6440 	      }
   6441 
   6442 	  for (i = 0; i < m->count; i++)
   6443 	    {
   6444 	      Elf_Internal_Shdr *this_hdr;
   6445 	      asection *sec;
   6446 
   6447 	      sec = m->sections[i];
   6448 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6449 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6450 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6451 		{
   6452 		  _bfd_error_handler
   6453 		    /* xgettext:c-format */
   6454 		    (_("%pB: section `%pA' can't be allocated in segment %u"),
   6455 		     abfd, sec, m->idx);
   6456 		  print_segment_map (m);
   6457 		}
   6458 	    }
   6459 	}
   6460     }
   6461 
   6462   elf_next_file_pos (abfd) = off;
   6463 
   6464   if (link_info != NULL
   6465       && phdr_load_seg != NULL
   6466       && phdr_load_seg->includes_filehdr)
   6467     {
   6468       /* There is a segment that contains both the file headers and the
   6469 	 program headers, so provide a symbol __ehdr_start pointing there.
   6470 	 A program can use this to examine itself robustly.  */
   6471 
   6472       struct elf_link_hash_table *htab = elf_hash_table (link_info);
   6473       struct elf_link_hash_entry *hash = htab->hehdr_start;
   6474 
   6475       /* If the symbol was referenced and not defined, define it.  */
   6476       if (hash != NULL
   6477 	  && (hash->root.type == bfd_link_hash_new
   6478 	      || hash->root.type == bfd_link_hash_undefined
   6479 	      || hash->root.type == bfd_link_hash_undefweak
   6480 	      || hash->root.type == bfd_link_hash_common))
   6481 	{
   6482 	  asection *s = NULL;
   6483 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6484 
   6485 	  if (phdr_load_seg->count != 0)
   6486 	    /* The segment contains sections, so use the first one.  */
   6487 	    s = phdr_load_seg->sections[0];
   6488 	  else
   6489 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6490 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6491 	      if (m->p_type == PT_LOAD && m->count != 0)
   6492 		{
   6493 		  s = m->sections[0];
   6494 		  break;
   6495 		}
   6496 
   6497 	  if (s != NULL)
   6498 	    {
   6499 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6500 	      hash->root.u.def.section = s;
   6501 	    }
   6502 	  else
   6503 	    {
   6504 	      hash->root.u.def.value = filehdr_vaddr;
   6505 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6506 	    }
   6507 
   6508 	  hash->root.type = bfd_link_hash_defined;
   6509 	  hash->def_regular = 1;
   6510 	  hash->non_elf = 0;
   6511 	}
   6512     }
   6513 
   6514   return true;
   6515 }
   6516 
   6517 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6518    is no defined method for detecting such files, so we have to
   6519    use heuristics instead.  */
   6520 
   6521 bool
   6522 is_debuginfo_file (bfd *abfd)
   6523 {
   6524   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6525     return false;
   6526 
   6527   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6528   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6529   Elf_Internal_Shdr **headerp;
   6530 
   6531   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6532     {
   6533       Elf_Internal_Shdr *header = * headerp;
   6534 
   6535       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6536 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6537       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6538 	  && header->sh_type != SHT_NOBITS
   6539 	  && header->sh_type != SHT_NOTE)
   6540 	return false;
   6541     }
   6542 
   6543   return true;
   6544 }
   6545 
   6546 /* Assign file positions for other sections, except for compressed debug
   6547    and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
   6548 
   6549 static bool
   6550 assign_file_positions_for_non_load_sections (bfd *abfd,
   6551 					     struct bfd_link_info *link_info)
   6552 {
   6553   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6554   Elf_Internal_Shdr **i_shdrpp;
   6555   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6556   Elf_Internal_Phdr *phdrs;
   6557   Elf_Internal_Phdr *p;
   6558   struct elf_segment_map *m;
   6559   file_ptr off;
   6560   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6561   bfd_vma maxpagesize;
   6562 
   6563   if (link_info != NULL)
   6564     maxpagesize = link_info->maxpagesize;
   6565   else
   6566     maxpagesize = bed->maxpagesize;
   6567   i_shdrpp = elf_elfsections (abfd);
   6568   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6569   off = elf_next_file_pos (abfd);
   6570   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6571     {
   6572       Elf_Internal_Shdr *hdr;
   6573       bfd_vma align;
   6574 
   6575       hdr = *hdrpp;
   6576       if (hdr->bfd_section != NULL
   6577 	  && (hdr->bfd_section->filepos != 0
   6578 	      || (hdr->sh_type == SHT_NOBITS
   6579 		  && hdr->contents == NULL)))
   6580 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6581       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6582 	{
   6583 	  if (hdr->sh_size != 0
   6584 	      /* PR 24717 - debuginfo files are known to be not strictly
   6585 		 compliant with the ELF standard.  In particular they often
   6586 		 have .note.gnu.property sections that are outside of any
   6587 		 loadable segment.  This is not a problem for such files,
   6588 		 so do not warn about them.  */
   6589 	      && ! is_debuginfo_file (abfd))
   6590 	    _bfd_error_handler
   6591 	      /* xgettext:c-format */
   6592 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6593 	       abfd,
   6594 	       (hdr->bfd_section == NULL
   6595 		? "*unknown*"
   6596 		: hdr->bfd_section->name));
   6597 	  /* We don't need to page align empty sections.  */
   6598 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6599 	    align = maxpagesize;
   6600 	  else
   6601 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6602 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6603 	  off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
   6604 							   bed->s->log_file_align);
   6605 	}
   6606       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6607 		&& hdr->bfd_section == NULL)
   6608 	       /* We don't know the offset of these sections yet:
   6609 		  their size has not been decided.  */
   6610 	       || (abfd->is_linker_output
   6611 		   && hdr->bfd_section != NULL
   6612 		   && (hdr->sh_name == -1u
   6613 		       || bfd_section_is_ctf (hdr->bfd_section)))
   6614 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6615 	       || (elf_symtab_shndx_list (abfd) != NULL
   6616 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6617 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6618 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6619 	hdr->sh_offset = -1;
   6620       else
   6621 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   6622     }
   6623   elf_next_file_pos (abfd) = off;
   6624 
   6625   /* Now that we have set the section file positions, we can set up
   6626      the file positions for the non PT_LOAD segments.  */
   6627   phdrs = elf_tdata (abfd)->phdr;
   6628   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6629     {
   6630       if (p->p_type == PT_GNU_RELRO)
   6631 	{
   6632 	  bfd_vma start, end;  /* Bytes.  */
   6633 	  bool ok;
   6634 
   6635 	  if (link_info != NULL)
   6636 	    {
   6637 	      /* During linking the range of the RELRO segment is passed
   6638 		 in link_info.  Note that there may be padding between
   6639 		 relro_start and the first RELRO section.  */
   6640 	      start = link_info->relro_start;
   6641 	      end = link_info->relro_end;
   6642 	    }
   6643 	  else if (m->count != 0)
   6644 	    {
   6645 	      if (!m->p_size_valid)
   6646 		abort ();
   6647 	      start = m->sections[0]->vma;
   6648 	      end = start + m->p_size / opb;
   6649 	    }
   6650 	  else
   6651 	    {
   6652 	      start = 0;
   6653 	      end = 0;
   6654 	    }
   6655 
   6656 	  ok = false;
   6657 	  if (start < end)
   6658 	    {
   6659 	      struct elf_segment_map *lm;
   6660 	      const Elf_Internal_Phdr *lp;
   6661 	      unsigned int i;
   6662 
   6663 	      /* Find a LOAD segment containing a section in the RELRO
   6664 		 segment.  */
   6665 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6666 		   lm != NULL;
   6667 		   lm = lm->next, lp++)
   6668 		{
   6669 		  if (lp->p_type == PT_LOAD
   6670 		      && lm->count != 0
   6671 		      && (lm->sections[lm->count - 1]->vma
   6672 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6673 			     ? lm->sections[lm->count - 1]->size / opb
   6674 			     : 0)) > start
   6675 		      && lm->sections[0]->vma < end)
   6676 		    break;
   6677 		}
   6678 
   6679 	      if (lm != NULL)
   6680 		{
   6681 		  /* Find the section starting the RELRO segment.  */
   6682 		  for (i = 0; i < lm->count; i++)
   6683 		    {
   6684 		      asection *s = lm->sections[i];
   6685 		      if (s->vma >= start
   6686 			  && s->vma < end
   6687 			  && s->size != 0)
   6688 			break;
   6689 		    }
   6690 
   6691 		  if (i < lm->count)
   6692 		    {
   6693 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6694 		      p->p_paddr = lm->sections[i]->lma * opb;
   6695 		      p->p_offset = lm->sections[i]->filepos;
   6696 		      p->p_memsz = end * opb - p->p_vaddr;
   6697 		      p->p_filesz = p->p_memsz;
   6698 
   6699 		      /* The RELRO segment typically ends a few bytes
   6700 			 into .got.plt but other layouts are possible.
   6701 			 In cases where the end does not match any
   6702 			 loaded section (for instance is in file
   6703 			 padding), trim p_filesz back to correspond to
   6704 			 the end of loaded section contents.  */
   6705 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6706 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6707 
   6708 		      /* Preserve the alignment and flags if they are
   6709 			 valid.  The gold linker generates RW/4 for
   6710 			 the PT_GNU_RELRO section.  It is better for
   6711 			 objcopy/strip to honor these attributes
   6712 			 otherwise gdb will choke when using separate
   6713 			 debug files.  */
   6714 		      if (!m->p_align_valid)
   6715 			p->p_align = 1;
   6716 		      if (!m->p_flags_valid)
   6717 			p->p_flags = PF_R;
   6718 		      ok = true;
   6719 		    }
   6720 		}
   6721 	    }
   6722 
   6723 	  if (!ok)
   6724 	    {
   6725 	      if (link_info != NULL)
   6726 		_bfd_error_handler
   6727 		  (_("%pB: warning: unable to allocate any sections"
   6728 		     " to PT_GNU_RELRO segment"),
   6729 		   abfd);
   6730 	      memset (p, 0, sizeof *p);
   6731 	    }
   6732 	}
   6733       else if (p->p_type == PT_GNU_STACK)
   6734 	{
   6735 	  if (m->p_size_valid)
   6736 	    p->p_memsz = m->p_size;
   6737 	}
   6738       else if (m->count != 0)
   6739 	{
   6740 	  unsigned int i;
   6741 
   6742 	  if (p->p_type != PT_LOAD
   6743 	      && (p->p_type != PT_NOTE
   6744 		  || bfd_get_format (abfd) != bfd_core))
   6745 	    {
   6746 	      /* A user specified segment layout may include a PHDR
   6747 		 segment that overlaps with a LOAD segment...  */
   6748 	      if (p->p_type == PT_PHDR)
   6749 		{
   6750 		  m->count = 0;
   6751 		  continue;
   6752 		}
   6753 
   6754 	      if (m->includes_filehdr || m->includes_phdrs)
   6755 		{
   6756 		  /* PR 17512: file: 2195325e.  */
   6757 		  _bfd_error_handler
   6758 		    (_("%pB: error: non-load segment %d includes file header "
   6759 		       "and/or program header"),
   6760 		     abfd, (int) (p - phdrs));
   6761 		  return false;
   6762 		}
   6763 
   6764 	      p->p_filesz = 0;
   6765 	      p->p_offset = m->sections[0]->filepos;
   6766 	      for (i = m->count; i-- != 0;)
   6767 		{
   6768 		  asection *sect = m->sections[i];
   6769 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6770 		  if (hdr->sh_type != SHT_NOBITS)
   6771 		    {
   6772 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6773 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6774 			 should be the same as p_filesz.  */
   6775 		      if (p->p_type == PT_NOTE
   6776 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6777 			p->p_memsz = p->p_filesz;
   6778 		      break;
   6779 		    }
   6780 		}
   6781 	    }
   6782 	}
   6783     }
   6784 
   6785   return true;
   6786 }
   6787 
   6788 static elf_section_list *
   6789 find_section_in_list (unsigned int i, elf_section_list * list)
   6790 {
   6791   for (;list != NULL; list = list->next)
   6792     if (list->ndx == i)
   6793       break;
   6794   return list;
   6795 }
   6796 
   6797 /* Work out the file positions of all the sections.  This is called by
   6798    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6799    VMAs must be known before this is called.
   6800 
   6801    Reloc sections come in two flavours: Those processed specially as
   6802    "side-channel" data attached to a section to which they apply, and
   6803    those that bfd doesn't process as relocations.  The latter sort are
   6804    stored in a normal bfd section by bfd_section_from_shdr.  We don't
   6805    consider the former sort here, unless they form part of the loadable
   6806    image.  Reloc sections not assigned here (and compressed debugging
   6807    sections and CTF sections which nothing else in the file can rely
   6808    upon) will be handled later by assign_file_positions_for_relocs.
   6809 
   6810    We also don't set the positions of the .symtab and .strtab here.  */
   6811 
   6812 static bool
   6813 assign_file_positions_except_relocs (bfd *abfd,
   6814 				     struct bfd_link_info *link_info)
   6815 {
   6816   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6817   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6818   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6819   unsigned int alloc;
   6820 
   6821   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6822       && bfd_get_format (abfd) != bfd_core)
   6823     {
   6824       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6825       unsigned int num_sec = elf_numsections (abfd);
   6826       Elf_Internal_Shdr **hdrpp;
   6827       unsigned int i;
   6828       file_ptr off;
   6829 
   6830       /* Start after the ELF header.  */
   6831       off = i_ehdrp->e_ehsize;
   6832 
   6833       /* We are not creating an executable, which means that we are
   6834 	 not creating a program header, and that the actual order of
   6835 	 the sections in the file is unimportant.  */
   6836       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6837 	{
   6838 	  Elf_Internal_Shdr *hdr;
   6839 
   6840 	  hdr = *hdrpp;
   6841 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6842 	       && hdr->bfd_section == NULL)
   6843 	      /* Do not assign offsets for these sections yet: we don't know
   6844 		 their sizes.  */
   6845 	      || (abfd->is_linker_output
   6846 		  && hdr->bfd_section != NULL
   6847 		  && (hdr->sh_name == -1u
   6848 		      || bfd_section_is_ctf (hdr->bfd_section)))
   6849 	      || i == elf_onesymtab (abfd)
   6850 	      || (elf_symtab_shndx_list (abfd) != NULL
   6851 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6852 	      || i == elf_strtab_sec (abfd)
   6853 	      || i == elf_shstrtab_sec (abfd))
   6854 	    {
   6855 	      hdr->sh_offset = -1;
   6856 	    }
   6857 	  else
   6858 	    /* There shouldn't be a need to effect "capped" file alignment here,
   6859 	       yet at least the Linux kernel's modpost utility was found to be
   6860 	       unhappy without.  While the issue was addressed there, let's be
   6861 	       kind for at least the foreseeable future ...  */
   6862 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
   6863 							     bed->s->log_file_align);
   6864 	}
   6865 
   6866       elf_next_file_pos (abfd) = off;
   6867       elf_program_header_size (abfd) = 0;
   6868     }
   6869   else
   6870     {
   6871       /* Assign file positions for the loaded sections based on the
   6872 	 assignment of sections to segments.  */
   6873       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6874 	return false;
   6875 
   6876       /* And for non-load sections.  */
   6877       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6878 	return false;
   6879     }
   6880 
   6881   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   6882     return false;
   6883 
   6884   /* Write out the program headers.  */
   6885   alloc = i_ehdrp->e_phnum;
   6886   if (alloc != 0)
   6887     {
   6888       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   6889 	{
   6890 	  bool warned_tls = false;
   6891 	  bool warned_rwx = false;
   6892 
   6893 	  /* Memory resident segments with non-zero size and RWX
   6894 	     permissions are a security risk, so we generate a warning
   6895 	     here if we are creating any.  */
   6896 	  unsigned int i;
   6897 
   6898 	  for (i = 0; i < alloc; i++)
   6899 	    {
   6900 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   6901 
   6902 	      if (phdr->p_memsz == 0)
   6903 		continue;
   6904 
   6905 	      if (! warned_tls
   6906 		  && phdr->p_type == PT_TLS
   6907 		  && (phdr->p_flags & PF_X))
   6908 		{
   6909 		  if (link_info->warn_is_error_for_rwx_segments)
   6910 		    {
   6911 		      _bfd_error_handler (_("\
   6912 error: %pB has a TLS segment with execute permission"),
   6913 					  abfd);
   6914 		      return false;
   6915 		    }
   6916 
   6917 		  _bfd_error_handler (_("\
   6918 warning: %pB has a TLS segment with execute permission"),
   6919 				      abfd);
   6920 		  if (warned_rwx)
   6921 		    break;
   6922 
   6923 		  warned_tls = true;
   6924 		}
   6925 	      else if (! warned_rwx
   6926 		       && phdr->p_type == PT_LOAD
   6927 		       && ((phdr->p_flags & (PF_R | PF_W | PF_X))
   6928 			   == (PF_R | PF_W | PF_X)))
   6929 		{
   6930 		  if (link_info->warn_is_error_for_rwx_segments)
   6931 		    {
   6932 		      _bfd_error_handler (_("\
   6933 error: %pB has a LOAD segment with RWX permissions"),
   6934 					  abfd);
   6935 		      return false;
   6936 		    }
   6937 
   6938 		  _bfd_error_handler (_("\
   6939 warning: %pB has a LOAD segment with RWX permissions"),
   6940 				      abfd);
   6941 		  if (warned_tls)
   6942 		    break;
   6943 
   6944 		  warned_rwx = true;
   6945 		}
   6946 	    }
   6947 	}
   6948 
   6949       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   6950 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   6951 	return false;
   6952     }
   6953 
   6954   return true;
   6955 }
   6956 
   6957 bool
   6958 _bfd_elf_init_file_header (bfd *abfd,
   6959 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   6960 {
   6961   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   6962   struct elf_strtab_hash *shstrtab;
   6963   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   6964 
   6965   i_ehdrp = elf_elfheader (abfd);
   6966 
   6967   shstrtab = _bfd_elf_strtab_init ();
   6968   if (shstrtab == NULL)
   6969     return false;
   6970 
   6971   elf_shstrtab (abfd) = shstrtab;
   6972 
   6973   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   6974   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   6975   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   6976   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   6977 
   6978   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   6979   i_ehdrp->e_ident[EI_DATA] =
   6980     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   6981   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   6982 
   6983   if ((abfd->flags & DYNAMIC) != 0)
   6984     i_ehdrp->e_type = ET_DYN;
   6985   else if ((abfd->flags & EXEC_P) != 0)
   6986     i_ehdrp->e_type = ET_EXEC;
   6987   else if (bfd_get_format (abfd) == bfd_core)
   6988     i_ehdrp->e_type = ET_CORE;
   6989   else
   6990     i_ehdrp->e_type = ET_REL;
   6991 
   6992   switch (bfd_get_arch (abfd))
   6993     {
   6994     case bfd_arch_unknown:
   6995       i_ehdrp->e_machine = EM_NONE;
   6996       break;
   6997 
   6998       /* There used to be a long list of cases here, each one setting
   6999 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   7000 	 in the corresponding bfd definition.  To avoid duplication,
   7001 	 the switch was removed.  Machines that need special handling
   7002 	 can generally do it in elf_backend_final_write_processing(),
   7003 	 unless they need the information earlier than the final write.
   7004 	 Such need can generally be supplied by replacing the tests for
   7005 	 e_machine with the conditions used to determine it.  */
   7006     default:
   7007       i_ehdrp->e_machine = bed->elf_machine_code;
   7008     }
   7009 
   7010   i_ehdrp->e_version = bed->s->ev_current;
   7011   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   7012 
   7013   /* No program header, for now.  */
   7014   i_ehdrp->e_phoff = 0;
   7015   i_ehdrp->e_phentsize = 0;
   7016   i_ehdrp->e_phnum = 0;
   7017 
   7018   /* Each bfd section is section header entry.  */
   7019   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   7020   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   7021 
   7022   elf_tdata (abfd)->symtab_hdr.sh_name =
   7023     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   7024   elf_tdata (abfd)->strtab_hdr.sh_name =
   7025     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   7026   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   7027     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   7028   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   7029       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   7030       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   7031     return false;
   7032 
   7033   return true;
   7034 }
   7035 
   7036 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   7037 
   7038    FIXME: We used to have code here to sort the PT_LOAD segments into
   7039    ascending order, as per the ELF spec.  But this breaks some programs,
   7040    including the Linux kernel.  But really either the spec should be
   7041    changed or the programs updated.  */
   7042 
   7043 bool
   7044 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   7045 {
   7046   if (link_info != NULL && bfd_link_pie (link_info))
   7047     {
   7048       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   7049       unsigned int num_segments = i_ehdrp->e_phnum;
   7050       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   7051       Elf_Internal_Phdr *segment = tdata->phdr;
   7052       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   7053 
   7054       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   7055       bfd_vma p_vaddr = (bfd_vma) -1;
   7056       for (; segment < end_segment; segment++)
   7057 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   7058 	  p_vaddr = segment->p_vaddr;
   7059 
   7060       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   7061 	 segments is non-zero.  */
   7062       if (p_vaddr)
   7063 	i_ehdrp->e_type = ET_EXEC;
   7064     }
   7065   return true;
   7066 }
   7067 
   7068 /* Assign file positions for all the reloc sections which are not part
   7069    of the loadable file image, and the file position of section headers.  */
   7070 
   7071 static bool
   7072 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   7073 {
   7074   file_ptr off;
   7075   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   7076   Elf_Internal_Shdr *shdrp;
   7077   Elf_Internal_Ehdr *i_ehdrp;
   7078   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7079 
   7080   /* Skip non-load sections without section header.  */
   7081   if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
   7082     return true;
   7083 
   7084   off = elf_next_file_pos (abfd);
   7085 
   7086   shdrpp = elf_elfsections (abfd);
   7087   end_shdrpp = shdrpp + elf_numsections (abfd);
   7088   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   7089     {
   7090       shdrp = *shdrpp;
   7091       if (shdrp->sh_offset == -1)
   7092 	{
   7093 	  asection *sec = shdrp->bfd_section;
   7094 	  if (sec == NULL
   7095 	      || shdrp->sh_type == SHT_REL
   7096 	      || shdrp->sh_type == SHT_RELA)
   7097 	    ;
   7098 	  else if (bfd_section_is_ctf (sec))
   7099 	    {
   7100 	      /* Update section size and contents.	*/
   7101 	      shdrp->sh_size = sec->size;
   7102 	      shdrp->contents = sec->contents;
   7103 	    }
   7104 	  else if (shdrp->sh_name == -1u)
   7105 	    {
   7106 	      const char *name = sec->name;
   7107 	      struct bfd_elf_section_data *d;
   7108 
   7109 	      /* Compress DWARF debug sections.  */
   7110 	      if (!bfd_compress_section (abfd, sec, shdrp->contents))
   7111 		return false;
   7112 
   7113 	      if (sec->compress_status == COMPRESS_SECTION_DONE
   7114 		  && (abfd->flags & BFD_COMPRESS_GABI) == 0
   7115 		  && name[1] == 'd')
   7116 		{
   7117 		  /* If section is compressed with zlib-gnu, convert
   7118 		     section name from .debug_* to .zdebug_*.  */
   7119 		  char *new_name = bfd_debug_name_to_zdebug (abfd, name);
   7120 		  if (new_name == NULL)
   7121 		    return false;
   7122 		  name = new_name;
   7123 		}
   7124 	      /* Add section name to section name section.  */
   7125 	      shdrp->sh_name
   7126 		= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   7127 						      name, false);
   7128 	      d = elf_section_data (sec);
   7129 
   7130 	      /* Add reloc section name to section name section.  */
   7131 	      if (d->rel.hdr
   7132 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
   7133 						  name, false))
   7134 		return false;
   7135 	      if (d->rela.hdr
   7136 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
   7137 						  name, true))
   7138 		return false;
   7139 
   7140 	      /* Update section size and contents.  */
   7141 	      shdrp->sh_size = sec->size;
   7142 	      shdrp->contents = sec->contents;
   7143 	      sec->contents = NULL;
   7144 	    }
   7145 
   7146 	  off = _bfd_elf_assign_file_position_for_section (shdrp, off,
   7147 		  (abfd->flags & (EXEC_P | DYNAMIC))
   7148 		  || bfd_get_format (abfd) == bfd_core,
   7149 		  bed->s->log_file_align);
   7150 	}
   7151     }
   7152 
   7153   /* Place section name section after DWARF debug sections have been
   7154      compressed.  */
   7155   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   7156   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   7157   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   7158   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
   7159 
   7160   /* Place the section headers.  */
   7161   i_ehdrp = elf_elfheader (abfd);
   7162   off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
   7163   i_ehdrp->e_shoff = off;
   7164   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   7165   elf_next_file_pos (abfd) = off;
   7166 
   7167   return true;
   7168 }
   7169 
   7170 bool
   7171 _bfd_elf_write_object_contents (bfd *abfd)
   7172 {
   7173   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   7174   Elf_Internal_Shdr **i_shdrp;
   7175   bool failed;
   7176   unsigned int count, num_sec;
   7177   struct elf_obj_tdata *t;
   7178 
   7179   if (! abfd->output_has_begun
   7180       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   7181     return false;
   7182   /* Do not rewrite ELF data when the BFD has been opened for update.
   7183      abfd->output_has_begun was set to TRUE on opening, so creation of
   7184      new sections, and modification of existing section sizes was
   7185      restricted.  This means the ELF header, program headers and
   7186      section headers can't have changed.  If the contents of any
   7187      sections has been modified, then those changes have already been
   7188      written to the BFD.  */
   7189   else if (abfd->direction == both_direction)
   7190     {
   7191       BFD_ASSERT (abfd->output_has_begun);
   7192       return true;
   7193     }
   7194 
   7195   i_shdrp = elf_elfsections (abfd);
   7196 
   7197   failed = false;
   7198   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   7199   if (failed)
   7200     return false;
   7201 
   7202   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   7203     return false;
   7204 
   7205   /* After writing the headers, we need to write the sections too...  */
   7206   num_sec = elf_numsections (abfd);
   7207   for (count = 1; count < num_sec; count++)
   7208     {
   7209       /* Don't set the sh_name field without section header.  */
   7210       if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
   7211 	i_shdrp[count]->sh_name
   7212 	  = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   7213 				    i_shdrp[count]->sh_name);
   7214       if (bed->elf_backend_section_processing)
   7215 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   7216 	  return false;
   7217       if (i_shdrp[count]->contents)
   7218 	{
   7219 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   7220 
   7221 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   7222 	      || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
   7223 	    return false;
   7224 	}
   7225     }
   7226 
   7227   /* Write out the section header names.  */
   7228   t = elf_tdata (abfd);
   7229   if (elf_shstrtab (abfd) != NULL
   7230       && t->shstrtab_hdr.sh_offset != -1
   7231       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   7232 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   7233     return false;
   7234 
   7235   if (!(*bed->elf_backend_final_write_processing) (abfd))
   7236     return false;
   7237 
   7238   if (!bed->s->write_shdrs_and_ehdr (abfd))
   7239     return false;
   7240 
   7241   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   7242   if (t->o->build_id.after_write_object_contents != NULL
   7243       && !(*t->o->build_id.after_write_object_contents) (abfd))
   7244     return false;
   7245   if (t->o->package_metadata.after_write_object_contents != NULL
   7246       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   7247     return false;
   7248 
   7249   return true;
   7250 }
   7251 
   7252 bool
   7253 _bfd_elf_write_corefile_contents (bfd *abfd)
   7254 {
   7255   /* Hopefully this can be done just like an object file.  */
   7256   return _bfd_elf_write_object_contents (abfd);
   7257 }
   7258 
   7259 /* Given a section, search the header to find them.  */
   7260 
   7261 unsigned int
   7262 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   7263 {
   7264   const struct elf_backend_data *bed;
   7265   unsigned int sec_index;
   7266 
   7267   if (elf_section_data (asect) != NULL
   7268       && elf_section_data (asect)->this_idx != 0)
   7269     return elf_section_data (asect)->this_idx;
   7270 
   7271   if (bfd_is_abs_section (asect))
   7272     sec_index = SHN_ABS;
   7273   else if (bfd_is_com_section (asect))
   7274     sec_index = SHN_COMMON;
   7275   else if (bfd_is_und_section (asect))
   7276     sec_index = SHN_UNDEF;
   7277   else
   7278     sec_index = SHN_BAD;
   7279 
   7280   bed = get_elf_backend_data (abfd);
   7281   if (bed->elf_backend_section_from_bfd_section)
   7282     {
   7283       int retval = sec_index;
   7284 
   7285       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   7286 	return retval;
   7287     }
   7288 
   7289   if (sec_index == SHN_BAD)
   7290     bfd_set_error (bfd_error_nonrepresentable_section);
   7291 
   7292   return sec_index;
   7293 }
   7294 
   7295 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   7296    on error.  */
   7297 
   7298 int
   7299 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   7300 {
   7301   asymbol *asym_ptr = *asym_ptr_ptr;
   7302   int idx;
   7303   flagword flags = asym_ptr->flags;
   7304 
   7305   /* When gas creates relocations against local labels, it creates its
   7306      own symbol for the section, but does put the symbol into the
   7307      symbol chain, so udata is 0.  When the linker is generating
   7308      relocatable output, this section symbol may be for one of the
   7309      input sections rather than the output section.  */
   7310   if (asym_ptr->udata.i == 0
   7311       && (flags & BSF_SECTION_SYM)
   7312       && asym_ptr->section)
   7313     {
   7314       asection *sec;
   7315 
   7316       sec = asym_ptr->section;
   7317       if (sec->owner != abfd && sec->output_section != NULL)
   7318 	sec = sec->output_section;
   7319       if (sec->owner == abfd
   7320 	  && sec->index < elf_num_section_syms (abfd)
   7321 	  && elf_section_syms (abfd)[sec->index] != NULL)
   7322 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   7323     }
   7324 
   7325   idx = asym_ptr->udata.i;
   7326 
   7327   if (idx == 0)
   7328     {
   7329       /* This case can occur when using --strip-symbol on a symbol
   7330 	 which is used in a relocation entry.  */
   7331       _bfd_error_handler
   7332 	/* xgettext:c-format */
   7333 	(_("%pB: symbol `%s' required but not present"),
   7334 	 abfd, bfd_asymbol_name (asym_ptr));
   7335       bfd_set_error (bfd_error_no_symbols);
   7336       return -1;
   7337     }
   7338 
   7339 #if DEBUG & 4
   7340   {
   7341     fprintf (stderr,
   7342 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
   7343 	     " flags = 0x%.8x\n",
   7344 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   7345     fflush (stderr);
   7346   }
   7347 #endif
   7348 
   7349   return idx;
   7350 }
   7351 
   7352 static inline bfd_vma
   7353 segment_size (Elf_Internal_Phdr *segment)
   7354 {
   7355   return (segment->p_memsz > segment->p_filesz
   7356 	  ? segment->p_memsz : segment->p_filesz);
   7357 }
   7358 
   7359 
   7360 /* Returns the end address of the segment + 1.  */
   7361 static inline bfd_vma
   7362 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
   7363 {
   7364   return start + segment_size (segment);
   7365 }
   7366 
   7367 static inline bfd_size_type
   7368 section_size (asection *section, Elf_Internal_Phdr *segment)
   7369 {
   7370   if ((section->flags & SEC_HAS_CONTENTS) != 0
   7371       || (section->flags & SEC_THREAD_LOCAL) == 0
   7372       || segment->p_type == PT_TLS)
   7373     return section->size;
   7374   return 0;
   7375 }
   7376 
   7377 /* Returns TRUE if the given section is contained within the given
   7378    segment.  LMA addresses are compared against PADDR when
   7379    USE_VADDR is false, VMA against VADDR when true.  */
   7380 static bool
   7381 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
   7382 		 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
   7383 		 bool use_vaddr)
   7384 {
   7385   bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
   7386   bfd_vma addr = !use_vaddr ? section->lma : section->vma;
   7387   bfd_vma octet;
   7388   if (_bfd_mul_overflow (addr, opb, &octet))
   7389     return false;
   7390   /* The third and fourth lines below are testing that the section end
   7391      address is within the segment.  It's written this way to avoid
   7392      overflow.  Add seg_addr + section_size to both sides of the
   7393      inequality to make it obvious.  */
   7394   return (octet >= seg_addr
   7395 	  && segment_size (segment) >= section_size (section, segment)
   7396 	  && (octet - seg_addr
   7397 	      <= segment_size (segment) - section_size (section, segment)));
   7398 }
   7399 
   7400 /* Handle PT_NOTE segment.  */
   7401 static bool
   7402 is_note (asection *s, Elf_Internal_Phdr *p)
   7403 {
   7404   return (p->p_type == PT_NOTE
   7405 	  && elf_section_type (s) == SHT_NOTE
   7406 	  && (ufile_ptr) s->filepos >= p->p_offset
   7407 	  && p->p_filesz >= s->size
   7408 	  && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
   7409 }
   7410 
   7411 /* Rewrite program header information.  */
   7412 
   7413 static bool
   7414 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   7415 {
   7416   Elf_Internal_Ehdr *iehdr;
   7417   struct elf_segment_map *map;
   7418   struct elf_segment_map *map_first;
   7419   struct elf_segment_map **pointer_to_map;
   7420   Elf_Internal_Phdr *segment;
   7421   asection *section;
   7422   unsigned int i;
   7423   unsigned int num_segments;
   7424   bool phdr_included = false;
   7425   bool p_paddr_valid;
   7426   struct elf_segment_map *phdr_adjust_seg = NULL;
   7427   unsigned int phdr_adjust_num = 0;
   7428   const struct elf_backend_data *bed;
   7429   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7430 
   7431   bed = get_elf_backend_data (ibfd);
   7432   iehdr = elf_elfheader (ibfd);
   7433 
   7434   map_first = NULL;
   7435   pointer_to_map = &map_first;
   7436 
   7437   num_segments = elf_elfheader (ibfd)->e_phnum;
   7438 
   7439   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   7440      linker, which generates a PT_INTERP section with p_vaddr and
   7441      p_memsz set to 0.  */
   7442 #define IS_SOLARIS_PT_INTERP(p, s)					\
   7443   (p->p_vaddr == 0							\
   7444    && p->p_paddr == 0							\
   7445    && p->p_memsz == 0							\
   7446    && p->p_filesz > 0							\
   7447    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   7448    && s->size > 0							\
   7449    && (bfd_vma) s->filepos >= p->p_offset				\
   7450    && ((bfd_vma) s->filepos + s->size					\
   7451        <= p->p_offset + p->p_filesz))
   7452 
   7453   /* Decide if the given section should be included in the given segment.
   7454      A section will be included if:
   7455        1. It is within the address space of the segment -- we use the LMA
   7456 	  if that is set for the segment and the VMA otherwise,
   7457        2. It is an allocated section or a NOTE section in a PT_NOTE
   7458 	  segment.
   7459        3. There is an output section associated with it,
   7460        4. The section has not already been allocated to a previous segment.
   7461        5. PT_GNU_STACK segments do not include any sections.
   7462        6. PT_TLS segment includes only SHF_TLS sections.
   7463        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   7464        8. PT_DYNAMIC should not contain empty sections at the beginning
   7465 	  (with the possible exception of .dynamic).  */
   7466 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid)	\
   7467   (((is_contained_by (section, segment, segment->p_paddr,		\
   7468 		      segment->p_vaddr, opb, !paddr_valid)		\
   7469      && (section->flags & SEC_ALLOC) != 0)				\
   7470     || is_note (section, segment))					\
   7471    && segment->p_type != PT_GNU_STACK					\
   7472    && (segment->p_type != PT_TLS					\
   7473        || (section->flags & SEC_THREAD_LOCAL))				\
   7474    && (segment->p_type == PT_LOAD					\
   7475        || segment->p_type == PT_TLS					\
   7476        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7477    && (segment->p_type != PT_DYNAMIC					\
   7478        || section_size (section, segment) > 0				\
   7479        || (segment->p_paddr						\
   7480 	   ? segment->p_paddr != section->lma * (opb)			\
   7481 	   : segment->p_vaddr != section->vma * (opb))			\
   7482        || (strcmp (bfd_section_name (section), ".dynamic") == 0))	\
   7483    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7484 
   7485 /* If the output section of a section in the input segment is NULL,
   7486    it is removed from the corresponding output segment.   */
   7487 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)	\
   7488   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid)	\
   7489    && section->output_section != NULL)
   7490 
   7491   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7492 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7493   (seg1->field >= segment_end (seg2, seg2->field))
   7494 
   7495   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7496      their VMA address ranges and their LMA address ranges overlap.
   7497      It is possible to have overlapping VMA ranges without overlapping LMA
   7498      ranges.  RedBoot images for example can have both .data and .bss mapped
   7499      to the same VMA range, but with the .data section mapped to a different
   7500      LMA.  */
   7501 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7502   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7503 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7504    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7505 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7506 
   7507   /* Initialise the segment mark field, and discard stupid alignment.  */
   7508   for (section = ibfd->sections; section != NULL; section = section->next)
   7509     {
   7510       asection *o = section->output_section;
   7511       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7512 	o->alignment_power = 0;
   7513       section->segment_mark = false;
   7514     }
   7515 
   7516   /* The Solaris linker creates program headers in which all the
   7517      p_paddr fields are zero.  When we try to objcopy or strip such a
   7518      file, we get confused.  Check for this case, and if we find it
   7519      don't set the p_paddr_valid fields.  */
   7520   p_paddr_valid = false;
   7521   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7522        i < num_segments;
   7523        i++, segment++)
   7524     if (segment->p_paddr != 0)
   7525       {
   7526 	p_paddr_valid = true;
   7527 	break;
   7528       }
   7529 
   7530   /* Scan through the segments specified in the program header
   7531      of the input BFD.  For this first scan we look for overlaps
   7532      in the loadable segments.  These can be created by weird
   7533      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7534   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7535        i < num_segments;
   7536        i++, segment++)
   7537     {
   7538       unsigned int j;
   7539       Elf_Internal_Phdr *segment2;
   7540 
   7541       if (segment->p_type == PT_INTERP)
   7542 	for (section = ibfd->sections; section; section = section->next)
   7543 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7544 	    {
   7545 	      /* Mininal change so that the normal section to segment
   7546 		 assignment code will work.  */
   7547 	      segment->p_vaddr = section->vma * opb;
   7548 	      break;
   7549 	    }
   7550 
   7551       if (segment->p_type != PT_LOAD)
   7552 	{
   7553 	  /* Remove PT_GNU_RELRO segment.  */
   7554 	  if (segment->p_type == PT_GNU_RELRO)
   7555 	    segment->p_type = PT_NULL;
   7556 	  continue;
   7557 	}
   7558 
   7559       /* Determine if this segment overlaps any previous segments.  */
   7560       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7561 	{
   7562 	  bfd_signed_vma extra_length;
   7563 
   7564 	  if (segment2->p_type != PT_LOAD
   7565 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7566 	    continue;
   7567 
   7568 	  /* Merge the two segments together.  */
   7569 	  if (segment2->p_vaddr < segment->p_vaddr)
   7570 	    {
   7571 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7572 		 SEGMENT.  */
   7573 	      extra_length = (segment_end (segment, segment->p_vaddr)
   7574 			      - segment_end (segment2, segment2->p_vaddr));
   7575 
   7576 	      if (extra_length > 0)
   7577 		{
   7578 		  segment2->p_memsz += extra_length;
   7579 		  segment2->p_filesz += extra_length;
   7580 		}
   7581 
   7582 	      segment->p_type = PT_NULL;
   7583 
   7584 	      /* Since we have deleted P we must restart the outer loop.  */
   7585 	      i = 0;
   7586 	      segment = elf_tdata (ibfd)->phdr;
   7587 	      break;
   7588 	    }
   7589 	  else
   7590 	    {
   7591 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7592 		 SEGMENT2.  */
   7593 	      extra_length = (segment_end (segment2, segment2->p_vaddr)
   7594 			      - segment_end (segment, segment->p_vaddr));
   7595 
   7596 	      if (extra_length > 0)
   7597 		{
   7598 		  segment->p_memsz += extra_length;
   7599 		  segment->p_filesz += extra_length;
   7600 		}
   7601 
   7602 	      segment2->p_type = PT_NULL;
   7603 	    }
   7604 	}
   7605     }
   7606 
   7607   /* The second scan attempts to assign sections to segments.  */
   7608   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7609        i < num_segments;
   7610        i++, segment++)
   7611     {
   7612       unsigned int section_count;
   7613       asection **sections;
   7614       asection *output_section;
   7615       unsigned int isec;
   7616       asection *matching_lma;
   7617       asection *suggested_lma;
   7618       unsigned int j;
   7619       size_t amt;
   7620       asection *first_section;
   7621 
   7622       if (segment->p_type == PT_NULL)
   7623 	continue;
   7624 
   7625       first_section = NULL;
   7626       /* Compute how many sections might be placed into this segment.  */
   7627       for (section = ibfd->sections, section_count = 0;
   7628 	   section != NULL;
   7629 	   section = section->next)
   7630 	{
   7631 	  /* Find the first section in the input segment, which may be
   7632 	     removed from the corresponding output segment.   */
   7633 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
   7634 	    {
   7635 	      if (first_section == NULL)
   7636 		first_section = section;
   7637 	      if (section->output_section != NULL)
   7638 		++section_count;
   7639 	    }
   7640 	}
   7641 
   7642       /* Allocate a segment map big enough to contain
   7643 	 all of the sections we have selected.  */
   7644       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7645       amt += section_count * sizeof (asection *);
   7646       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7647       if (map == NULL)
   7648 	return false;
   7649 
   7650       /* Initialise the fields of the segment map.  Default to
   7651 	 using the physical address of the segment in the input BFD.  */
   7652       map->next = NULL;
   7653       map->p_type = segment->p_type;
   7654       map->p_flags = segment->p_flags;
   7655       map->p_flags_valid = 1;
   7656 
   7657       if (map->p_type == PT_LOAD
   7658 	  && (ibfd->flags & D_PAGED) != 0
   7659 	  && maxpagesize > 1
   7660 	  && segment->p_align > 1)
   7661 	{
   7662 	  map->p_align = segment->p_align;
   7663 	  if (segment->p_align > maxpagesize)
   7664 	    map->p_align = maxpagesize;
   7665 	  map->p_align_valid = 1;
   7666 	}
   7667 
   7668       /* If the first section in the input segment is removed, there is
   7669 	 no need to preserve segment physical address in the corresponding
   7670 	 output segment.  */
   7671       if (!first_section || first_section->output_section != NULL)
   7672 	{
   7673 	  map->p_paddr = segment->p_paddr;
   7674 	  map->p_paddr_valid = p_paddr_valid;
   7675 	}
   7676 
   7677       /* Determine if this segment contains the ELF file header
   7678 	 and if it contains the program headers themselves.  */
   7679       map->includes_filehdr = (segment->p_offset == 0
   7680 			       && segment->p_filesz >= iehdr->e_ehsize);
   7681       map->includes_phdrs = 0;
   7682 
   7683       if (!phdr_included || segment->p_type != PT_LOAD)
   7684 	{
   7685 	  map->includes_phdrs =
   7686 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7687 	     && (segment->p_offset + segment->p_filesz
   7688 		 >= ((bfd_vma) iehdr->e_phoff
   7689 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7690 
   7691 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7692 	    phdr_included = true;
   7693 	}
   7694 
   7695       if (section_count == 0)
   7696 	{
   7697 	  /* Special segments, such as the PT_PHDR segment, may contain
   7698 	     no sections, but ordinary, loadable segments should contain
   7699 	     something.  They are allowed by the ELF spec however, so only
   7700 	     a warning is produced.
   7701 	     Don't warn if an empty PT_LOAD contains the program headers.
   7702 	     There is however the valid use case of embedded systems which
   7703 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7704 	     flash memory with zeros.  No warning is shown for that case.  */
   7705 	  if (segment->p_type == PT_LOAD
   7706 	      && !map->includes_phdrs
   7707 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7708 	    /* xgettext:c-format */
   7709 	    _bfd_error_handler
   7710 	      (_("%pB: warning: empty loadable segment detected"
   7711 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7712 	       ibfd, (uint64_t) segment->p_vaddr);
   7713 
   7714 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7715 	  map->count = 0;
   7716 	  *pointer_to_map = map;
   7717 	  pointer_to_map = &map->next;
   7718 
   7719 	  continue;
   7720 	}
   7721 
   7722       /* Now scan the sections in the input BFD again and attempt
   7723 	 to add their corresponding output sections to the segment map.
   7724 	 The problem here is how to handle an output section which has
   7725 	 been moved (ie had its LMA changed).  There are four possibilities:
   7726 
   7727 	 1. None of the sections have been moved.
   7728 	    In this case we can continue to use the segment LMA from the
   7729 	    input BFD.
   7730 
   7731 	 2. All of the sections have been moved by the same amount.
   7732 	    In this case we can change the segment's LMA to match the LMA
   7733 	    of the first section.
   7734 
   7735 	 3. Some of the sections have been moved, others have not.
   7736 	    In this case those sections which have not been moved can be
   7737 	    placed in the current segment which will have to have its size,
   7738 	    and possibly its LMA changed, and a new segment or segments will
   7739 	    have to be created to contain the other sections.
   7740 
   7741 	 4. The sections have been moved, but not by the same amount.
   7742 	    In this case we can change the segment's LMA to match the LMA
   7743 	    of the first section and we will have to create a new segment
   7744 	    or segments to contain the other sections.
   7745 
   7746 	 In order to save time, we allocate an array to hold the section
   7747 	 pointers that we are interested in.  As these sections get assigned
   7748 	 to a segment, they are removed from this array.  */
   7749 
   7750       amt = section_count * sizeof (asection *);
   7751       sections = (asection **) bfd_malloc (amt);
   7752       if (sections == NULL)
   7753 	return false;
   7754 
   7755       /* Step One: Scan for segment vs section LMA conflicts.
   7756 	 Also add the sections to the section array allocated above.
   7757 	 Also add the sections to the current segment.  In the common
   7758 	 case, where the sections have not been moved, this means that
   7759 	 we have completely filled the segment, and there is nothing
   7760 	 more to do.  */
   7761       isec = 0;
   7762       matching_lma = NULL;
   7763       suggested_lma = NULL;
   7764 
   7765       for (section = first_section, j = 0;
   7766 	   section != NULL;
   7767 	   section = section->next)
   7768 	{
   7769 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
   7770 	    {
   7771 	      output_section = section->output_section;
   7772 
   7773 	      sections[j++] = section;
   7774 
   7775 	      /* The Solaris native linker always sets p_paddr to 0.
   7776 		 We try to catch that case here, and set it to the
   7777 		 correct value.  Note - some backends require that
   7778 		 p_paddr be left as zero.  */
   7779 	      if (!p_paddr_valid
   7780 		  && segment->p_vaddr != 0
   7781 		  && !bed->want_p_paddr_set_to_zero
   7782 		  && isec == 0
   7783 		  && output_section->lma != 0
   7784 		  && (align_power (segment->p_vaddr
   7785 				   + (map->includes_filehdr
   7786 				      ? iehdr->e_ehsize : 0)
   7787 				   + (map->includes_phdrs
   7788 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7789 				      : 0),
   7790 				   output_section->alignment_power * opb)
   7791 		      == (output_section->vma * opb)))
   7792 		map->p_paddr = segment->p_vaddr;
   7793 
   7794 	      /* Match up the physical address of the segment with the
   7795 		 LMA address of the output section.  */
   7796 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7797 				   0, opb, false)
   7798 		  || is_note (section, segment))
   7799 		{
   7800 		  if (matching_lma == NULL
   7801 		      || output_section->lma < matching_lma->lma)
   7802 		    matching_lma = output_section;
   7803 
   7804 		  /* We assume that if the section fits within the segment
   7805 		     then it does not overlap any other section within that
   7806 		     segment.  */
   7807 		  map->sections[isec++] = output_section;
   7808 		}
   7809 	      else if (suggested_lma == NULL)
   7810 		suggested_lma = output_section;
   7811 
   7812 	      if (j == section_count)
   7813 		break;
   7814 	    }
   7815 	}
   7816 
   7817       BFD_ASSERT (j == section_count);
   7818 
   7819       /* Step Two: Adjust the physical address of the current segment,
   7820 	 if necessary.  */
   7821       if (isec == section_count)
   7822 	{
   7823 	  /* All of the sections fitted within the segment as currently
   7824 	     specified.  This is the default case.  Add the segment to
   7825 	     the list of built segments and carry on to process the next
   7826 	     program header in the input BFD.  */
   7827 	  map->count = section_count;
   7828 	  *pointer_to_map = map;
   7829 	  pointer_to_map = &map->next;
   7830 
   7831 	  if (p_paddr_valid
   7832 	      && !bed->want_p_paddr_set_to_zero)
   7833 	    {
   7834 	      bfd_vma hdr_size = 0;
   7835 	      if (map->includes_filehdr)
   7836 		hdr_size = iehdr->e_ehsize;
   7837 	      if (map->includes_phdrs)
   7838 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7839 
   7840 	      /* Account for padding before the first section in the
   7841 		 segment.  */
   7842 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7843 				     - matching_lma->lma);
   7844 	    }
   7845 
   7846 	  free (sections);
   7847 	  continue;
   7848 	}
   7849       else
   7850 	{
   7851 	  /* Change the current segment's physical address to match
   7852 	     the LMA of the first section that fitted, or if no
   7853 	     section fitted, the first section.  */
   7854 	  if (matching_lma == NULL)
   7855 	    matching_lma = suggested_lma;
   7856 
   7857 	  map->p_paddr = matching_lma->lma * opb;
   7858 
   7859 	  /* Offset the segment physical address from the lma
   7860 	     to allow for space taken up by elf headers.  */
   7861 	  if (map->includes_phdrs)
   7862 	    {
   7863 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7864 
   7865 	      /* iehdr->e_phnum is just an estimate of the number
   7866 		 of program headers that we will need.  Make a note
   7867 		 here of the number we used and the segment we chose
   7868 		 to hold these headers, so that we can adjust the
   7869 		 offset when we know the correct value.  */
   7870 	      phdr_adjust_num = iehdr->e_phnum;
   7871 	      phdr_adjust_seg = map;
   7872 	    }
   7873 
   7874 	  if (map->includes_filehdr)
   7875 	    {
   7876 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7877 	      map->p_paddr -= iehdr->e_ehsize;
   7878 	      /* We've subtracted off the size of headers from the
   7879 		 first section lma, but there may have been some
   7880 		 alignment padding before that section too.  Try to
   7881 		 account for that by adjusting the segment lma down to
   7882 		 the same alignment.  */
   7883 	      if (segment->p_align != 0 && segment->p_align < align)
   7884 		align = segment->p_align;
   7885 	      map->p_paddr &= -(align * opb);
   7886 	    }
   7887 	}
   7888 
   7889       /* Step Three: Loop over the sections again, this time assigning
   7890 	 those that fit to the current segment and removing them from the
   7891 	 sections array; but making sure not to leave large gaps.  Once all
   7892 	 possible sections have been assigned to the current segment it is
   7893 	 added to the list of built segments and if sections still remain
   7894 	 to be assigned, a new segment is constructed before repeating
   7895 	 the loop.  */
   7896       isec = 0;
   7897       do
   7898 	{
   7899 	  map->count = 0;
   7900 	  suggested_lma = NULL;
   7901 
   7902 	  /* Fill the current segment with sections that fit.  */
   7903 	  for (j = 0; j < section_count; j++)
   7904 	    {
   7905 	      section = sections[j];
   7906 
   7907 	      if (section == NULL)
   7908 		continue;
   7909 
   7910 	      output_section = section->output_section;
   7911 
   7912 	      BFD_ASSERT (output_section != NULL);
   7913 
   7914 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7915 				   0, opb, false)
   7916 		  || is_note (section, segment))
   7917 		{
   7918 		  if (map->count == 0)
   7919 		    {
   7920 		      /* If the first section in a segment does not start at
   7921 			 the beginning of the segment, then something is
   7922 			 wrong.  */
   7923 		      if (align_power (map->p_paddr
   7924 				       + (map->includes_filehdr
   7925 					  ? iehdr->e_ehsize : 0)
   7926 				       + (map->includes_phdrs
   7927 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7928 					  : 0),
   7929 				       output_section->alignment_power * opb)
   7930 			  != output_section->lma * opb)
   7931 			goto sorry;
   7932 		    }
   7933 		  else
   7934 		    {
   7935 		      asection *prev_sec;
   7936 
   7937 		      prev_sec = map->sections[map->count - 1];
   7938 
   7939 		      /* If the gap between the end of the previous section
   7940 			 and the start of this section is more than
   7941 			 maxpagesize then we need to start a new segment.  */
   7942 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   7943 				      maxpagesize)
   7944 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   7945 			  || (prev_sec->lma + prev_sec->size
   7946 			      > output_section->lma))
   7947 			{
   7948 			  if (suggested_lma == NULL)
   7949 			    suggested_lma = output_section;
   7950 
   7951 			  continue;
   7952 			}
   7953 		    }
   7954 
   7955 		  map->sections[map->count++] = output_section;
   7956 		  ++isec;
   7957 		  sections[j] = NULL;
   7958 		  if (segment->p_type == PT_LOAD)
   7959 		    section->segment_mark = true;
   7960 		}
   7961 	      else if (suggested_lma == NULL)
   7962 		suggested_lma = output_section;
   7963 	    }
   7964 
   7965 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   7966 	     be assigned to any segment - because for example they have a
   7967 	     negative size - or segments that do not contain any sections.
   7968 	     But there are also valid reasons why a segment can be empty.
   7969 	     So allow a count of zero.  */
   7970 
   7971 	  /* Add the current segment to the list of built segments.  */
   7972 	  *pointer_to_map = map;
   7973 	  pointer_to_map = &map->next;
   7974 
   7975 	  if (isec < section_count)
   7976 	    {
   7977 	      /* We still have not allocated all of the sections to
   7978 		 segments.  Create a new segment here, initialise it
   7979 		 and carry on looping.  */
   7980 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7981 	      amt += section_count * sizeof (asection *);
   7982 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7983 	      if (map == NULL)
   7984 		{
   7985 		  free (sections);
   7986 		  return false;
   7987 		}
   7988 
   7989 	      /* Initialise the fields of the segment map.  Set the physical
   7990 		 physical address to the LMA of the first section that has
   7991 		 not yet been assigned.  */
   7992 	      map->next = NULL;
   7993 	      map->p_type = segment->p_type;
   7994 	      map->p_flags = segment->p_flags;
   7995 	      map->p_flags_valid = 1;
   7996 	      map->p_paddr = suggested_lma->lma * opb;
   7997 	      map->p_paddr_valid = p_paddr_valid;
   7998 	      map->includes_filehdr = 0;
   7999 	      map->includes_phdrs = 0;
   8000 	    }
   8001 
   8002 	  continue;
   8003 	sorry:
   8004 	  bfd_set_error (bfd_error_sorry);
   8005 	  free (sections);
   8006 	  return false;
   8007 	}
   8008       while (isec < section_count);
   8009 
   8010       free (sections);
   8011     }
   8012 
   8013   elf_seg_map (obfd) = map_first;
   8014 
   8015   /* If we had to estimate the number of program headers that were
   8016      going to be needed, then check our estimate now and adjust
   8017      the offset if necessary.  */
   8018   if (phdr_adjust_seg != NULL)
   8019     {
   8020       unsigned int count;
   8021 
   8022       for (count = 0, map = map_first; map != NULL; map = map->next)
   8023 	count++;
   8024 
   8025       if (count > phdr_adjust_num)
   8026 	phdr_adjust_seg->p_paddr
   8027 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   8028 
   8029       for (map = map_first; map != NULL; map = map->next)
   8030 	if (map->p_type == PT_PHDR)
   8031 	  {
   8032 	    bfd_vma adjust
   8033 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   8034 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   8035 	    break;
   8036 	  }
   8037     }
   8038 
   8039 #undef IS_SOLARIS_PT_INTERP
   8040 #undef IS_SECTION_IN_INPUT_SEGMENT
   8041 #undef INCLUDE_SECTION_IN_SEGMENT
   8042 #undef SEGMENT_AFTER_SEGMENT
   8043 #undef SEGMENT_OVERLAPS
   8044   return true;
   8045 }
   8046 
   8047 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   8048 
   8049 static bool
   8050 elf_is_p_align_valid (bfd *abfd)
   8051 {
   8052   unsigned int i;
   8053   Elf_Internal_Phdr *segment;
   8054   unsigned int num_segments;
   8055   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   8056   bfd_size_type maxpagesize = bed->maxpagesize;
   8057   bfd_size_type p_align = bed->p_align;
   8058 
   8059   /* Return true if the default p_align value isn't set or the maximum
   8060      page size is the same as the minimum page size.  */
   8061   if (p_align == 0 || maxpagesize == bed->minpagesize)
   8062     return true;
   8063 
   8064   /* When the default p_align value is set, p_align may be set to the
   8065      default p_align value while segments are aligned to the maximum
   8066      page size.  In this case, the input p_align will be ignored and
   8067      the maximum page size will be used to align the output segments.  */
   8068   segment = elf_tdata (abfd)->phdr;
   8069   num_segments = elf_elfheader (abfd)->e_phnum;
   8070   for (i = 0; i < num_segments; i++, segment++)
   8071     if (segment->p_type == PT_LOAD
   8072 	&& (segment->p_align != p_align
   8073 	    || vma_page_aligned_bias (segment->p_vaddr,
   8074 				      segment->p_offset,
   8075 				      maxpagesize) != 0))
   8076       return true;
   8077 
   8078   return false;
   8079 }
   8080 
   8081 /* Copy ELF program header information.  */
   8082 
   8083 static bool
   8084 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   8085 {
   8086   Elf_Internal_Ehdr *iehdr;
   8087   struct elf_segment_map *map;
   8088   struct elf_segment_map *map_first;
   8089   struct elf_segment_map **pointer_to_map;
   8090   Elf_Internal_Phdr *segment;
   8091   unsigned int i;
   8092   unsigned int num_segments;
   8093   bool phdr_included = false;
   8094   bool p_paddr_valid;
   8095   bool p_palign_valid;
   8096   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   8097 
   8098   iehdr = elf_elfheader (ibfd);
   8099 
   8100   map_first = NULL;
   8101   pointer_to_map = &map_first;
   8102 
   8103   /* If all the segment p_paddr fields are zero, don't set
   8104      map->p_paddr_valid.  */
   8105   p_paddr_valid = false;
   8106   num_segments = elf_elfheader (ibfd)->e_phnum;
   8107   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8108        i < num_segments;
   8109        i++, segment++)
   8110     if (segment->p_paddr != 0)
   8111       {
   8112 	p_paddr_valid = true;
   8113 	break;
   8114       }
   8115 
   8116   p_palign_valid = elf_is_p_align_valid (ibfd);
   8117 
   8118   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8119        i < num_segments;
   8120        i++, segment++)
   8121     {
   8122       asection *section;
   8123       unsigned int section_count;
   8124       size_t amt;
   8125       Elf_Internal_Shdr *this_hdr;
   8126       asection *first_section = NULL;
   8127       asection *lowest_section;
   8128 
   8129       /* Compute how many sections are in this segment.  */
   8130       for (section = ibfd->sections, section_count = 0;
   8131 	   section != NULL;
   8132 	   section = section->next)
   8133 	{
   8134 	  this_hdr = &(elf_section_data(section)->this_hdr);
   8135 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8136 	    {
   8137 	      if (first_section == NULL)
   8138 		first_section = section;
   8139 	      section_count++;
   8140 	    }
   8141 	}
   8142 
   8143       /* Allocate a segment map big enough to contain
   8144 	 all of the sections we have selected.  */
   8145       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8146       amt += section_count * sizeof (asection *);
   8147       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8148       if (map == NULL)
   8149 	return false;
   8150 
   8151       /* Initialize the fields of the output segment map with the
   8152 	 input segment.  */
   8153       map->next = NULL;
   8154       map->p_type = segment->p_type;
   8155       map->p_flags = segment->p_flags;
   8156       map->p_flags_valid = 1;
   8157       map->p_paddr = segment->p_paddr;
   8158       map->p_paddr_valid = p_paddr_valid;
   8159       map->p_align = segment->p_align;
   8160       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   8161       map->p_align_valid = (map->p_type == PT_GNU_STACK
   8162 			    || p_palign_valid);
   8163       map->p_vaddr_offset = 0;
   8164 
   8165       if (map->p_type == PT_GNU_RELRO
   8166 	  || map->p_type == PT_GNU_STACK)
   8167 	{
   8168 	  /* The PT_GNU_RELRO segment may contain the first a few
   8169 	     bytes in the .got.plt section even if the whole .got.plt
   8170 	     section isn't in the PT_GNU_RELRO segment.  We won't
   8171 	     change the size of the PT_GNU_RELRO segment.
   8172 	     Similarly, PT_GNU_STACK size is significant on uclinux
   8173 	     systems.    */
   8174 	  map->p_size = segment->p_memsz;
   8175 	  map->p_size_valid = 1;
   8176 	}
   8177 
   8178       /* Determine if this segment contains the ELF file header
   8179 	 and if it contains the program headers themselves.  */
   8180       map->includes_filehdr = (segment->p_offset == 0
   8181 			       && segment->p_filesz >= iehdr->e_ehsize);
   8182 
   8183       map->includes_phdrs = 0;
   8184       if (! phdr_included || segment->p_type != PT_LOAD)
   8185 	{
   8186 	  map->includes_phdrs =
   8187 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   8188 	     && (segment->p_offset + segment->p_filesz
   8189 		 >= ((bfd_vma) iehdr->e_phoff
   8190 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   8191 
   8192 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   8193 	    phdr_included = true;
   8194 	}
   8195 
   8196       lowest_section = NULL;
   8197       if (section_count != 0)
   8198 	{
   8199 	  unsigned int isec = 0;
   8200 
   8201 	  for (section = first_section;
   8202 	       section != NULL;
   8203 	       section = section->next)
   8204 	    {
   8205 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8206 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8207 		{
   8208 		  map->sections[isec++] = section->output_section;
   8209 		  if ((section->flags & SEC_ALLOC) != 0)
   8210 		    {
   8211 		      bfd_vma seg_off;
   8212 
   8213 		      if (lowest_section == NULL
   8214 			  || section->lma < lowest_section->lma)
   8215 			lowest_section = section;
   8216 
   8217 		      /* Section lmas are set up from PT_LOAD header
   8218 			 p_paddr in _bfd_elf_make_section_from_shdr.
   8219 			 If this header has a p_paddr that disagrees
   8220 			 with the section lma, flag the p_paddr as
   8221 			 invalid.  */
   8222 		      if ((section->flags & SEC_LOAD) != 0)
   8223 			seg_off = this_hdr->sh_offset - segment->p_offset;
   8224 		      else
   8225 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   8226 		      if (section->lma * opb - segment->p_paddr != seg_off)
   8227 			map->p_paddr_valid = false;
   8228 		    }
   8229 		  if (isec == section_count)
   8230 		    break;
   8231 		}
   8232 	    }
   8233 	}
   8234 
   8235       if (section_count == 0)
   8236 	map->p_vaddr_offset = segment->p_vaddr / opb;
   8237       else if (map->p_paddr_valid)
   8238 	{
   8239 	  /* Account for padding before the first section in the segment.  */
   8240 	  bfd_vma hdr_size = 0;
   8241 	  if (map->includes_filehdr)
   8242 	    hdr_size = iehdr->e_ehsize;
   8243 	  if (map->includes_phdrs)
   8244 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   8245 
   8246 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   8247 				 - (lowest_section ? lowest_section->lma : 0));
   8248 	}
   8249 
   8250       map->count = section_count;
   8251       *pointer_to_map = map;
   8252       pointer_to_map = &map->next;
   8253     }
   8254 
   8255   elf_seg_map (obfd) = map_first;
   8256   return true;
   8257 }
   8258 
   8259 /* Copy private BFD data.  This copies or rewrites ELF program header
   8260    information.  */
   8261 
   8262 static bool
   8263 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   8264 {
   8265   bfd_vma maxpagesize;
   8266 
   8267   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8268       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8269     return true;
   8270 
   8271   if (elf_tdata (ibfd)->phdr == NULL)
   8272     return true;
   8273 
   8274   if (ibfd->xvec == obfd->xvec)
   8275     {
   8276       /* Check to see if any sections in the input BFD
   8277 	 covered by ELF program header have changed.  */
   8278       Elf_Internal_Phdr *segment;
   8279       asection * section;
   8280       asection * osec;
   8281       asection * prev;
   8282       unsigned int i, num_segments;
   8283       Elf_Internal_Shdr *this_hdr;
   8284       const struct elf_backend_data *bed;
   8285 
   8286       bed = get_elf_backend_data (ibfd);
   8287 
   8288       /* Regenerate the segment map if p_paddr is set to 0.  */
   8289       if (bed->want_p_paddr_set_to_zero)
   8290 	goto rewrite;
   8291 
   8292       /* Initialize the segment mark field.  */
   8293       for (section = obfd->sections; section != NULL;
   8294 	   section = section->next)
   8295 	section->segment_mark = false;
   8296 
   8297       num_segments = elf_elfheader (ibfd)->e_phnum;
   8298       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8299 	   i < num_segments;
   8300 	   i++, segment++)
   8301 	{
   8302 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   8303 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   8304 	     which severly confuses things, so always regenerate the segment
   8305 	     map in this case.  */
   8306 	  if (segment->p_paddr == 0
   8307 	      && segment->p_memsz == 0
   8308 	      && (segment->p_type == PT_INTERP
   8309 		  || segment->p_type == PT_DYNAMIC))
   8310 	    goto rewrite;
   8311 
   8312 	  for (section = ibfd->sections, prev = NULL;
   8313 	       section != NULL; section = section->next)
   8314 	    {
   8315 	      /* We mark the output section so that we know it comes
   8316 		 from the input BFD.  */
   8317 	      osec = section->output_section;
   8318 	      if (osec)
   8319 		osec->segment_mark = true;
   8320 
   8321 	      /* Check if this section is covered by the segment.  */
   8322 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8323 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8324 		{
   8325 		  /* FIXME: Check if its output section is changed or
   8326 		     removed.  What else do we need to check?  */
   8327 		  if (osec == NULL
   8328 		      || section->flags != osec->flags
   8329 		      || section->lma != osec->lma
   8330 		      || section->vma != osec->vma
   8331 		      || section->size != osec->size
   8332 		      || section->rawsize != osec->rawsize
   8333 		      || section->alignment_power != osec->alignment_power)
   8334 		    goto rewrite;
   8335 
   8336 		  /* PR 31450: If this is an allocated section then make sure
   8337 		     that this section's vma to lma relationship is the same
   8338 		     as previous (allocated) section's.  */
   8339 		  if (prev != NULL
   8340 		      && section->flags & SEC_ALLOC
   8341 		      && section->lma - section->vma != prev->lma - prev->vma)
   8342 		    goto rewrite;
   8343 
   8344 		  if (section->flags & SEC_ALLOC)
   8345 		    prev = section;
   8346 		}
   8347 	    }
   8348 	}
   8349 
   8350       /* Check to see if any output section do not come from the
   8351 	 input BFD.  */
   8352       for (section = obfd->sections; section != NULL;
   8353 	   section = section->next)
   8354 	{
   8355 	  if (!section->segment_mark)
   8356 	    goto rewrite;
   8357 	  else
   8358 	    section->segment_mark = false;
   8359 	}
   8360 
   8361       return copy_elf_program_header (ibfd, obfd);
   8362     }
   8363 
   8364  rewrite:
   8365   maxpagesize = 0;
   8366   if (ibfd->xvec == obfd->xvec)
   8367     {
   8368       /* When rewriting program header, set the output maxpagesize to
   8369 	 the maximum alignment of input PT_LOAD segments.  */
   8370       Elf_Internal_Phdr *segment;
   8371       unsigned int i;
   8372       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   8373 
   8374       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8375 	   i < num_segments;
   8376 	   i++, segment++)
   8377 	if (segment->p_type == PT_LOAD
   8378 	    && maxpagesize < segment->p_align)
   8379 	  {
   8380 	    /* PR 17512: file: f17299af.  */
   8381 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   8382 	      /* xgettext:c-format */
   8383 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   8384 				    PRIx64 " is too large"),
   8385 				  ibfd, (uint64_t) segment->p_align);
   8386 	    else
   8387 	      maxpagesize = segment->p_align;
   8388 	  }
   8389     }
   8390   if (maxpagesize == 0)
   8391     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   8392 
   8393   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   8394 }
   8395 
   8396 /* Copy private section information from input to output.  This function
   8397    is called both by objcopy where LINK_INFO is NULL, and ld where
   8398    LINK_INFO is non-NULL in the usual case but NULL for a special case
   8399    when dealing with LTO IR or cloning output sections.  */
   8400 
   8401 bool
   8402 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8403 				    asection *isec,
   8404 				    bfd *obfd,
   8405 				    asection *osec,
   8406 				    struct bfd_link_info *link_info)
   8407 {
   8408   if (ibfd->xvec->flavour != bfd_target_elf_flavour
   8409       || obfd->xvec->flavour != bfd_target_elf_flavour)
   8410     return true;
   8411 
   8412   Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
   8413   Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
   8414   if (link_info == NULL)
   8415     {
   8416       ohdr->sh_entsize = ihdr->sh_entsize;
   8417 
   8418       if (ihdr->sh_type == SHT_SYMTAB
   8419 	  || ihdr->sh_type == SHT_DYNSYM
   8420 	  || ihdr->sh_type == SHT_GNU_verneed
   8421 	  || ihdr->sh_type == SHT_GNU_verdef)
   8422 	ohdr->sh_info = ihdr->sh_info;
   8423     }
   8424 
   8425   /* If this is a known ABI section, ELF section type and flags may
   8426      have been set up when OSEC was created.  For normal sections we
   8427      allow the user to override the type and flags other than
   8428      SHF_MASKOS and SHF_MASKPROC.  */
   8429   if (elf_section_type (osec) == SHT_PROGBITS
   8430       || elf_section_type (osec) == SHT_NOTE
   8431       || elf_section_type (osec) == SHT_NOBITS)
   8432     elf_section_type (osec) = SHT_NULL;
   8433 
   8434   /* For objcopy and relocatable link, copy the ELF section type from
   8435      the input file if the BFD section flags are the same.  (If they
   8436      are different the user may be doing something like
   8437      "objcopy --set-section-flags .text=alloc,data".)  For a final
   8438      link allow some flags that the linker clears to differ.  */
   8439   bool final_link = (link_info != NULL
   8440 		     && !bfd_link_relocatable (link_info));
   8441   if (elf_section_type (osec) == SHT_NULL
   8442       && (osec->flags == isec->flags
   8443 	  || (final_link
   8444 	      && ((osec->flags ^ isec->flags)
   8445 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   8446     elf_section_type (osec) = elf_section_type (isec);
   8447 
   8448   /* FIXME: Is this correct for all OS/PROC specific flags?  */
   8449   elf_section_flags (osec) = (elf_section_flags (isec)
   8450 			      & (SHF_MASKOS | SHF_MASKPROC));
   8451 
   8452   /* Copy sh_info from input for mbind section.  */
   8453   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   8454       && elf_section_flags (isec) & SHF_GNU_MBIND)
   8455     elf_section_data (osec)->this_hdr.sh_info
   8456       = elf_section_data (isec)->this_hdr.sh_info;
   8457 
   8458   /* Set things up for objcopy and relocatable link.  The output
   8459      SHT_GROUP section will have its elf_next_in_group pointing back
   8460      to the input group members.  Ignore linker created group section.
   8461      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   8462   if ((link_info == NULL
   8463        || !link_info->resolve_section_groups)
   8464       && (elf_sec_group (isec) == NULL
   8465 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   8466     {
   8467       if (elf_section_flags (isec) & SHF_GROUP)
   8468 	elf_section_flags (osec) |= SHF_GROUP;
   8469       elf_next_in_group (osec) = elf_next_in_group (isec);
   8470       elf_section_data (osec)->group = elf_section_data (isec)->group;
   8471     }
   8472 
   8473   /* If not decompress, preserve SHF_COMPRESSED.  */
   8474   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   8475     elf_section_flags (osec) |= (elf_section_flags (isec)
   8476 				 & SHF_COMPRESSED);
   8477 
   8478   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   8479      don't use the output section of the linked-to section since it
   8480      may be NULL at this point.  */
   8481   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   8482     {
   8483       ohdr->sh_flags |= SHF_LINK_ORDER;
   8484       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   8485     }
   8486 
   8487   osec->use_rela_p = isec->use_rela_p;
   8488 
   8489   return true;
   8490 }
   8491 
   8492 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8493    necessary if we are removing either the SHT_GROUP section or any of
   8494    the group member sections.  DISCARDED is the value that a section's
   8495    output_section has if the section will be discarded, NULL when this
   8496    function is called from objcopy, bfd_abs_section_ptr when called
   8497    from the linker.  */
   8498 
   8499 bool
   8500 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8501 {
   8502   asection *isec;
   8503 
   8504   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8505     if (elf_section_type (isec) == SHT_GROUP)
   8506       {
   8507 	asection *first = elf_next_in_group (isec);
   8508 	asection *s = first;
   8509 	bfd_size_type removed = 0;
   8510 
   8511 	while (s != NULL)
   8512 	  {
   8513 	    /* If this member section is being output but the
   8514 	       SHT_GROUP section is not, then clear the group info
   8515 	       set up by _bfd_elf_copy_private_section_data.  */
   8516 	    if (s->output_section != discarded
   8517 		&& isec->output_section == discarded)
   8518 	      {
   8519 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8520 		elf_group_name (s->output_section) = NULL;
   8521 	      }
   8522 	    else
   8523 	      {
   8524 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8525 		if (s->output_section == discarded
   8526 		    && isec->output_section != discarded)
   8527 		  {
   8528 		    /* Conversely, if the member section is not being
   8529 		       output but the SHT_GROUP section is, then adjust
   8530 		       its size.  */
   8531 		    removed += 4;
   8532 		    if (elf_sec->rel.hdr != NULL
   8533 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8534 		      removed += 4;
   8535 		    if (elf_sec->rela.hdr != NULL
   8536 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8537 		      removed += 4;
   8538 		  }
   8539 		else
   8540 		  {
   8541 		    /* Also adjust for zero-sized relocation member
   8542 		       section.  */
   8543 		    if (elf_sec->rel.hdr != NULL
   8544 			&& elf_sec->rel.hdr->sh_size == 0)
   8545 		      removed += 4;
   8546 		    if (elf_sec->rela.hdr != NULL
   8547 			&& elf_sec->rela.hdr->sh_size == 0)
   8548 		      removed += 4;
   8549 		  }
   8550 	      }
   8551 	    s = elf_next_in_group (s);
   8552 	    if (s == first)
   8553 	      break;
   8554 	  }
   8555 	if (removed != 0)
   8556 	  {
   8557 	    if (discarded != NULL)
   8558 	      {
   8559 		/* If we've been called for ld -r, then we need to
   8560 		   adjust the input section size.  */
   8561 		if (isec->rawsize == 0)
   8562 		  isec->rawsize = isec->size;
   8563 		isec->size = isec->rawsize - removed;
   8564 		if (isec->size <= 4)
   8565 		  {
   8566 		    isec->size = 0;
   8567 		    isec->flags |= SEC_EXCLUDE;
   8568 		  }
   8569 	      }
   8570 	    else if (isec->output_section != NULL)
   8571 	      {
   8572 		/* Adjust the output section size when called from
   8573 		   objcopy. */
   8574 		isec->output_section->size -= removed;
   8575 		if (isec->output_section->size <= 4)
   8576 		  {
   8577 		    isec->output_section->size = 0;
   8578 		    isec->output_section->flags |= SEC_EXCLUDE;
   8579 		  }
   8580 	      }
   8581 	  }
   8582       }
   8583 
   8584   return true;
   8585 }
   8586 
   8587 /* Copy private header information.  */
   8588 
   8589 bool
   8590 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8591 {
   8592   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8593       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8594     return true;
   8595 
   8596   /* Copy over private BFD data if it has not already been copied.
   8597      This must be done here, rather than in the copy_private_bfd_data
   8598      entry point, because the latter is called after the section
   8599      contents have been set, which means that the program headers have
   8600      already been worked out.  */
   8601   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8602     {
   8603       if (! copy_private_bfd_data (ibfd, obfd))
   8604 	return false;
   8605     }
   8606 
   8607   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8608 }
   8609 
   8610 /* Copy private symbol information.  If this symbol is in a section
   8611    which we did not map into a BFD section, try to map the section
   8612    index correctly.  We use special macro definitions for the mapped
   8613    section indices; these definitions are interpreted by the
   8614    swap_out_syms function.  */
   8615 
   8616 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8617 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8618 #define MAP_STRTAB    (SHN_HIOS + 3)
   8619 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8620 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8621 
   8622 bool
   8623 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8624 				   asymbol *isymarg,
   8625 				   bfd *obfd,
   8626 				   asymbol *osymarg)
   8627 {
   8628   elf_symbol_type *isym, *osym;
   8629 
   8630   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   8631       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   8632     return true;
   8633 
   8634   isym = elf_symbol_from (isymarg);
   8635   osym = elf_symbol_from (osymarg);
   8636 
   8637   if (isym != NULL
   8638       && isym->internal_elf_sym.st_shndx != 0
   8639       && osym != NULL
   8640       && bfd_is_abs_section (isym->symbol.section))
   8641     {
   8642       unsigned int shndx;
   8643 
   8644       shndx = isym->internal_elf_sym.st_shndx;
   8645       if (shndx == elf_onesymtab (ibfd))
   8646 	shndx = MAP_ONESYMTAB;
   8647       else if (shndx == elf_dynsymtab (ibfd))
   8648 	shndx = MAP_DYNSYMTAB;
   8649       else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
   8650 	shndx = MAP_STRTAB;
   8651       else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
   8652 	shndx = MAP_SHSTRTAB;
   8653       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8654 	shndx = MAP_SYM_SHNDX;
   8655       osym->internal_elf_sym.st_shndx = shndx;
   8656     }
   8657 
   8658   return true;
   8659 }
   8660 
   8661 /* Swap out the symbols.  */
   8662 
   8663 static bool
   8664 swap_out_syms (bfd *abfd,
   8665 	       struct elf_strtab_hash **sttp,
   8666 	       int relocatable_p,
   8667 	       struct bfd_link_info *info)
   8668 {
   8669   const struct elf_backend_data *bed;
   8670   unsigned int symcount;
   8671   asymbol **syms;
   8672   struct elf_strtab_hash *stt;
   8673   Elf_Internal_Shdr *symtab_hdr;
   8674   Elf_Internal_Shdr *symtab_shndx_hdr;
   8675   Elf_Internal_Shdr *symstrtab_hdr;
   8676   struct elf_sym_strtab *symstrtab;
   8677   bfd_byte *outbound_syms;
   8678   bfd_byte *outbound_shndx;
   8679   unsigned long outbound_syms_index;
   8680   unsigned int idx;
   8681   unsigned int num_locals;
   8682   size_t amt;
   8683   bool name_local_sections;
   8684 
   8685   if (!elf_map_symbols (abfd, &num_locals))
   8686     return false;
   8687 
   8688   /* Dump out the symtabs.  */
   8689   stt = _bfd_elf_strtab_init ();
   8690   if (stt == NULL)
   8691     return false;
   8692 
   8693   bed = get_elf_backend_data (abfd);
   8694   symcount = bfd_get_symcount (abfd);
   8695   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8696   symtab_hdr->sh_type = SHT_SYMTAB;
   8697   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8698   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8699   symtab_hdr->sh_info = num_locals + 1;
   8700   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8701 
   8702   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8703   symstrtab_hdr->sh_type = SHT_STRTAB;
   8704 
   8705   /* Allocate buffer to swap out the .strtab section.  */
   8706   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8707       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8708     {
   8709       bfd_set_error (bfd_error_no_memory);
   8710       _bfd_elf_strtab_free (stt);
   8711       return false;
   8712     }
   8713 
   8714   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8715       || (outbound_syms = bfd_malloc (amt)) == NULL)
   8716     {
   8717     error_no_mem:
   8718       bfd_set_error (bfd_error_no_memory);
   8719     error_return:
   8720       free (symstrtab);
   8721       _bfd_elf_strtab_free (stt);
   8722       return false;
   8723     }
   8724   symtab_hdr->contents = outbound_syms;
   8725   outbound_syms_index = 0;
   8726 
   8727   outbound_shndx = NULL;
   8728 
   8729   if (elf_symtab_shndx_list (abfd))
   8730     {
   8731       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8732       if (symtab_shndx_hdr->sh_name != 0)
   8733 	{
   8734 	  if (_bfd_mul_overflow (symcount + 1,
   8735 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8736 	    goto error_no_mem;
   8737 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8738 	  if (outbound_shndx == NULL)
   8739 	    goto error_return;
   8740 
   8741 	  symtab_shndx_hdr->contents = outbound_shndx;
   8742 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8743 	  symtab_shndx_hdr->sh_size = amt;
   8744 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8745 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8746 	}
   8747       /* FIXME: What about any other headers in the list ?  */
   8748     }
   8749 
   8750   /* Now generate the data (for "contents").  */
   8751   {
   8752     /* Fill in zeroth symbol and swap it out.  */
   8753     Elf_Internal_Sym sym;
   8754     sym.st_name = 0;
   8755     sym.st_value = 0;
   8756     sym.st_size = 0;
   8757     sym.st_info = 0;
   8758     sym.st_other = 0;
   8759     sym.st_shndx = SHN_UNDEF;
   8760     sym.st_target_internal = 0;
   8761     symstrtab[outbound_syms_index].sym = sym;
   8762     symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
   8763     outbound_syms_index++;
   8764   }
   8765 
   8766   name_local_sections
   8767     = (bed->elf_backend_name_local_section_symbols
   8768        && bed->elf_backend_name_local_section_symbols (abfd));
   8769 
   8770   syms = bfd_get_outsymbols (abfd);
   8771   for (idx = 0; idx < symcount; idx++)
   8772     {
   8773       Elf_Internal_Sym sym;
   8774 
   8775       flagword flags = syms[idx]->flags;
   8776       if (!name_local_sections
   8777 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8778 	{
   8779 	  /* Local section symbols have no name.  */
   8780 	  sym.st_name = 0;
   8781 	}
   8782       else
   8783 	{
   8784 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8785 	     to get the final offset for st_name.  */
   8786 	  size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
   8787 	  if (stridx == (size_t) -1)
   8788 	    goto error_return;
   8789 	  sym.st_name = stridx;
   8790 	}
   8791 
   8792       bfd_vma value = syms[idx]->value;
   8793       elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
   8794       asection *sec = syms[idx]->section;
   8795 
   8796       if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
   8797 	{
   8798 	  /* ELF common symbols put the alignment into the `value' field,
   8799 	     and the size into the `size' field.  This is backwards from
   8800 	     how BFD handles it, so reverse it here.  */
   8801 	  sym.st_size = value;
   8802 	  if (type_ptr == NULL
   8803 	      || type_ptr->internal_elf_sym.st_value == 0)
   8804 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8805 	  else
   8806 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8807 	  sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8808 	}
   8809       else
   8810 	{
   8811 	  unsigned int shndx;
   8812 
   8813 	  if (sec->output_section)
   8814 	    {
   8815 	      value += sec->output_offset;
   8816 	      sec = sec->output_section;
   8817 	    }
   8818 
   8819 	  /* Don't add in the section vma for relocatable output.  */
   8820 	  if (! relocatable_p)
   8821 	    value += sec->vma;
   8822 	  sym.st_value = value;
   8823 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8824 
   8825 	  if (bfd_is_abs_section (sec)
   8826 	      && type_ptr != NULL
   8827 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8828 	    {
   8829 	      /* This symbol is in a real ELF section which we did
   8830 		 not create as a BFD section.  Undo the mapping done
   8831 		 by copy_private_symbol_data.  */
   8832 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8833 	      switch (shndx)
   8834 		{
   8835 		case MAP_ONESYMTAB:
   8836 		  shndx = elf_onesymtab (abfd);
   8837 		  break;
   8838 		case MAP_DYNSYMTAB:
   8839 		  shndx = elf_dynsymtab (abfd);
   8840 		  break;
   8841 		case MAP_STRTAB:
   8842 		  shndx = elf_strtab_sec (abfd);
   8843 		  break;
   8844 		case MAP_SHSTRTAB:
   8845 		  shndx = elf_shstrtab_sec (abfd);
   8846 		  break;
   8847 		case MAP_SYM_SHNDX:
   8848 		  if (elf_symtab_shndx_list (abfd))
   8849 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8850 		  break;
   8851 		case SHN_COMMON:
   8852 		case SHN_ABS:
   8853 		  shndx = SHN_ABS;
   8854 		  break;
   8855 		default:
   8856 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8857 		    {
   8858 		      if (bed->symbol_section_index)
   8859 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8860 		      /* Otherwise just leave the index alone.  */
   8861 		    }
   8862 		  else
   8863 		    {
   8864 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   8865 			_bfd_error_handler (_("%pB: \
   8866 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   8867 					  abfd, shndx);
   8868 		      shndx = SHN_ABS;
   8869 		    }
   8870 		  break;
   8871 		}
   8872 	    }
   8873 	  else
   8874 	    {
   8875 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8876 
   8877 	      if (shndx == SHN_BAD)
   8878 		{
   8879 		  asection *sec2;
   8880 
   8881 		  /* Writing this would be a hell of a lot easier if
   8882 		     we had some decent documentation on bfd, and
   8883 		     knew what to expect of the library, and what to
   8884 		     demand of applications.  For example, it
   8885 		     appears that `objcopy' might not set the
   8886 		     section of a symbol to be a section that is
   8887 		     actually in the output file.  */
   8888 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   8889 		  if (sec2 != NULL)
   8890 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   8891 		  if (shndx == SHN_BAD)
   8892 		    {
   8893 		      /* xgettext:c-format */
   8894 		      _bfd_error_handler
   8895 			(_("unable to find equivalent output section"
   8896 			   " for symbol '%s' from section '%s'"),
   8897 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   8898 			 sec->name);
   8899 		      bfd_set_error (bfd_error_invalid_operation);
   8900 		      goto error_return;
   8901 		    }
   8902 		}
   8903 	    }
   8904 
   8905 	  sym.st_shndx = shndx;
   8906 	}
   8907 
   8908       int type;
   8909       if ((flags & BSF_THREAD_LOCAL) != 0)
   8910 	type = STT_TLS;
   8911       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8912 	type = STT_GNU_IFUNC;
   8913       else if ((flags & BSF_FUNCTION) != 0)
   8914 	type = STT_FUNC;
   8915       else if ((flags & BSF_OBJECT) != 0)
   8916 	type = STT_OBJECT;
   8917       else if ((flags & BSF_RELC) != 0)
   8918 	type = STT_RELC;
   8919       else if ((flags & BSF_SRELC) != 0)
   8920 	type = STT_SRELC;
   8921       else
   8922 	type = STT_NOTYPE;
   8923 
   8924       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8925 	type = STT_TLS;
   8926 
   8927       /* Processor-specific types.  */
   8928       if (type_ptr != NULL
   8929 	  && bed->elf_backend_get_symbol_type)
   8930 	type = ((*bed->elf_backend_get_symbol_type)
   8931 		(&type_ptr->internal_elf_sym, type));
   8932 
   8933       if (flags & BSF_SECTION_SYM)
   8934 	{
   8935 	  if (flags & BSF_GLOBAL)
   8936 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   8937 	  else
   8938 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   8939 	}
   8940       else if (bfd_is_com_section (syms[idx]->section))
   8941 	{
   8942 	  if (type != STT_TLS)
   8943 	    {
   8944 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   8945 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   8946 			? STT_COMMON : STT_OBJECT);
   8947 	      else
   8948 		type = ((flags & BSF_ELF_COMMON) != 0
   8949 			? STT_COMMON : STT_OBJECT);
   8950 	    }
   8951 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   8952 	}
   8953       else if (bfd_is_und_section (syms[idx]->section))
   8954 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   8955 				    ? STB_WEAK
   8956 				    : STB_GLOBAL),
   8957 				   type);
   8958       else if (flags & BSF_FILE)
   8959 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   8960       else
   8961 	{
   8962 	  int bind = STB_LOCAL;
   8963 
   8964 	  if (flags & BSF_LOCAL)
   8965 	    bind = STB_LOCAL;
   8966 	  else if (flags & BSF_GNU_UNIQUE)
   8967 	    bind = STB_GNU_UNIQUE;
   8968 	  else if (flags & BSF_WEAK)
   8969 	    bind = STB_WEAK;
   8970 	  else if (flags & BSF_GLOBAL)
   8971 	    bind = STB_GLOBAL;
   8972 
   8973 	  sym.st_info = ELF_ST_INFO (bind, type);
   8974 	}
   8975 
   8976       if (type_ptr != NULL)
   8977 	{
   8978 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   8979 	  sym.st_target_internal
   8980 	    = type_ptr->internal_elf_sym.st_target_internal;
   8981 	}
   8982       else
   8983 	{
   8984 	  sym.st_other = 0;
   8985 	  sym.st_target_internal = 0;
   8986 	}
   8987 
   8988       symstrtab[outbound_syms_index].sym = sym;
   8989       symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
   8990       outbound_syms_index++;
   8991     }
   8992 
   8993   /* Finalize the .strtab section.  */
   8994   _bfd_elf_strtab_finalize (stt);
   8995 
   8996   /* Swap out the .strtab section.  */
   8997   for (idx = 0; idx < outbound_syms_index; idx++)
   8998     {
   8999       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   9000       if (elfsym->sym.st_name != 0)
   9001 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   9002 						      elfsym->sym.st_name);
   9003       if (info && info->callbacks->ctf_new_symbol)
   9004 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   9005 					 &elfsym->sym);
   9006 
   9007       /* Inform the linker of the addition of this symbol.  */
   9008 
   9009       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   9010 			       (outbound_syms
   9011 				+ (elfsym->dest_index
   9012 				   * bed->s->sizeof_sym)),
   9013 			       NPTR_ADD (outbound_shndx,
   9014 					 (elfsym->dest_index
   9015 					  * sizeof (Elf_External_Sym_Shndx))));
   9016     }
   9017   free (symstrtab);
   9018 
   9019   *sttp = stt;
   9020   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   9021   symstrtab_hdr->sh_type = SHT_STRTAB;
   9022   symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
   9023   symstrtab_hdr->sh_addr = 0;
   9024   symstrtab_hdr->sh_entsize = 0;
   9025   symstrtab_hdr->sh_link = 0;
   9026   symstrtab_hdr->sh_info = 0;
   9027   symstrtab_hdr->sh_addralign = 1;
   9028 
   9029   return true;
   9030 }
   9031 
   9032 /* Return the number of bytes required to hold the symtab vector.
   9033 
   9034    Note that we base it on the count plus 1, since we will null terminate
   9035    the vector allocated based on this size.  However, the ELF symbol table
   9036    always has a dummy entry as symbol #0, so it ends up even.  */
   9037 
   9038 long
   9039 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   9040 {
   9041   bfd_size_type symcount;
   9042   long symtab_size;
   9043   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   9044 
   9045   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9046   if (symcount > LONG_MAX / sizeof (asymbol *))
   9047     {
   9048       bfd_set_error (bfd_error_file_too_big);
   9049       return -1;
   9050     }
   9051   symtab_size = symcount * (sizeof (asymbol *));
   9052   if (symcount == 0)
   9053     symtab_size = sizeof (asymbol *);
   9054   else if (!bfd_write_p (abfd))
   9055     {
   9056       ufile_ptr filesize = bfd_get_file_size (abfd);
   9057 
   9058       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9059 	{
   9060 	  bfd_set_error (bfd_error_file_truncated);
   9061 	  return -1;
   9062 	}
   9063     }
   9064 
   9065   return symtab_size;
   9066 }
   9067 
   9068 long
   9069 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   9070 {
   9071   bfd_size_type symcount;
   9072   long symtab_size;
   9073   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   9074 
   9075   if (elf_dynsymtab (abfd) == 0)
   9076     {
   9077       /* Check if there is dynamic symbol table.  */
   9078       symcount = elf_tdata (abfd)->dt_symtab_count;
   9079       if (symcount)
   9080 	goto compute_symtab_size;
   9081 
   9082       bfd_set_error (bfd_error_invalid_operation);
   9083       return -1;
   9084     }
   9085 
   9086   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9087   if (symcount > LONG_MAX / sizeof (asymbol *))
   9088     {
   9089       bfd_set_error (bfd_error_file_too_big);
   9090       return -1;
   9091     }
   9092 
   9093  compute_symtab_size:
   9094   symtab_size = symcount * (sizeof (asymbol *));
   9095   if (symcount == 0)
   9096     symtab_size = sizeof (asymbol *);
   9097   else if (!bfd_write_p (abfd))
   9098     {
   9099       ufile_ptr filesize = bfd_get_file_size (abfd);
   9100 
   9101       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9102 	{
   9103 	  bfd_set_error (bfd_error_file_truncated);
   9104 	  return -1;
   9105 	}
   9106     }
   9107 
   9108   return symtab_size;
   9109 }
   9110 
   9111 long
   9112 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   9113 {
   9114   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   9115     {
   9116       /* Sanity check reloc section size.  */
   9117       ufile_ptr filesize = bfd_get_file_size (abfd);
   9118 
   9119       if (filesize != 0)
   9120 	{
   9121 	  struct bfd_elf_section_data *d = elf_section_data (asect);
   9122 	  bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
   9123 	  bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
   9124 
   9125 	  if (rel_size + rela_size > filesize
   9126 	      || rel_size + rela_size < rel_size)
   9127 	    {
   9128 	      bfd_set_error (bfd_error_file_truncated);
   9129 	      return -1;
   9130 	    }
   9131 	}
   9132     }
   9133 
   9134 #if SIZEOF_LONG == SIZEOF_INT
   9135   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   9136     {
   9137       bfd_set_error (bfd_error_file_too_big);
   9138       return -1;
   9139     }
   9140 #endif
   9141   return (asect->reloc_count + 1L) * sizeof (arelent *);
   9142 }
   9143 
   9144 /* Canonicalize the relocs.  */
   9145 
   9146 long
   9147 _bfd_elf_canonicalize_reloc (bfd *abfd,
   9148 			     sec_ptr section,
   9149 			     arelent **relptr,
   9150 			     asymbol **symbols)
   9151 {
   9152   arelent *tblptr;
   9153   unsigned int i;
   9154   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9155 
   9156   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   9157     return -1;
   9158 
   9159   tblptr = section->relocation;
   9160   for (i = 0; i < section->reloc_count; i++)
   9161     *relptr++ = tblptr++;
   9162 
   9163   *relptr = NULL;
   9164 
   9165   return section->reloc_count;
   9166 }
   9167 
   9168 long
   9169 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   9170 {
   9171   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9172   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   9173 
   9174   if (symcount >= 0)
   9175     abfd->symcount = symcount;
   9176   return symcount;
   9177 }
   9178 
   9179 long
   9180 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   9181 				      asymbol **allocation)
   9182 {
   9183   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9184   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   9185 
   9186   if (symcount >= 0)
   9187     abfd->dynsymcount = symcount;
   9188   return symcount;
   9189 }
   9190 
   9191 /* Return the size required for the dynamic reloc entries.  Any loadable
   9192    section that was actually installed in the BFD, and has type SHT_REL
   9193    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   9194    dynamic reloc section.  */
   9195 
   9196 long
   9197 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   9198 {
   9199   bfd_size_type count, ext_rel_size;
   9200   asection *s;
   9201 
   9202   if (elf_dynsymtab (abfd) == 0)
   9203     {
   9204       bfd_set_error (bfd_error_invalid_operation);
   9205       return -1;
   9206     }
   9207 
   9208   count = 1;
   9209   ext_rel_size = 0;
   9210   for (s = abfd->sections; s != NULL; s = s->next)
   9211     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9212 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9213 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9214 	&& (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9215       {
   9216 	ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
   9217 	if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
   9218 	  {
   9219 	    bfd_set_error (bfd_error_file_truncated);
   9220 	    return -1;
   9221 	  }
   9222 	count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9223 	if (count > LONG_MAX / sizeof (arelent *))
   9224 	  {
   9225 	    bfd_set_error (bfd_error_file_too_big);
   9226 	    return -1;
   9227 	  }
   9228       }
   9229   if (count > 1 && !bfd_write_p (abfd))
   9230     {
   9231       /* Sanity check reloc section sizes.  */
   9232       ufile_ptr filesize = bfd_get_file_size (abfd);
   9233       if (filesize != 0 && ext_rel_size > filesize)
   9234 	{
   9235 	  bfd_set_error (bfd_error_file_truncated);
   9236 	  return -1;
   9237 	}
   9238     }
   9239   return count * sizeof (arelent *);
   9240 }
   9241 
   9242 /* Canonicalize the dynamic relocation entries.  Note that we return the
   9243    dynamic relocations as a single block, although they are actually
   9244    associated with particular sections; the interface, which was
   9245    designed for SunOS style shared libraries, expects that there is only
   9246    one set of dynamic relocs.  Any loadable section that was actually
   9247    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   9248    dynamic symbol table, is considered to be a dynamic reloc section.  */
   9249 
   9250 long
   9251 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   9252 				     arelent **storage,
   9253 				     asymbol **syms)
   9254 {
   9255   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   9256   asection *s;
   9257   long ret;
   9258 
   9259   if (elf_dynsymtab (abfd) == 0)
   9260     {
   9261       bfd_set_error (bfd_error_invalid_operation);
   9262       return -1;
   9263     }
   9264 
   9265   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   9266   ret = 0;
   9267   for (s = abfd->sections; s != NULL; s = s->next)
   9268     {
   9269       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9270 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9271 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9272 	  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9273 	{
   9274 	  arelent *p;
   9275 	  long count, i;
   9276 
   9277 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   9278 	    return -1;
   9279 	  count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9280 	  p = s->relocation;
   9281 	  for (i = 0; i < count; i++)
   9282 	    *storage++ = p++;
   9283 	  ret += count;
   9284 	}
   9285     }
   9286 
   9287   *storage = NULL;
   9288 
   9289   return ret;
   9290 }
   9291 
   9292 /* Read in the version information.  */
   9294 
   9295 bool
   9296 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   9297 {
   9298   bfd_byte *contents = NULL;
   9299   unsigned int freeidx = 0;
   9300   size_t amt;
   9301   void *contents_addr = NULL;
   9302   size_t contents_size = 0;
   9303 
   9304   if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
   9305     {
   9306       Elf_Internal_Shdr *hdr;
   9307       Elf_External_Verneed *everneed;
   9308       Elf_Internal_Verneed *iverneed;
   9309       unsigned int i;
   9310       bfd_byte *contents_end;
   9311       size_t verneed_count;
   9312       size_t verneed_size;
   9313 
   9314       if (elf_tdata (abfd)->dt_verneed != NULL)
   9315 	{
   9316 	  hdr = NULL;
   9317 	  contents = elf_tdata (abfd)->dt_verneed;
   9318 	  verneed_count = elf_tdata (abfd)->dt_verneed_count;
   9319 	  verneed_size = verneed_count * sizeof (Elf_External_Verneed);
   9320 	}
   9321       else
   9322 	{
   9323 	  hdr = &elf_tdata (abfd)->dynverref_hdr;
   9324 
   9325 	  if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   9326 	    {
   9327 	    error_return_bad_verref:
   9328 	      _bfd_error_handler
   9329 		(_("%pB: .gnu.version_r invalid entry"), abfd);
   9330 	      bfd_set_error (bfd_error_bad_value);
   9331 	    error_return_verref:
   9332 	      elf_tdata (abfd)->verref = NULL;
   9333 	      elf_tdata (abfd)->cverrefs = 0;
   9334 	      goto error_return;
   9335 	    }
   9336 
   9337 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9338 	    goto error_return_verref;
   9339 	  contents_size = hdr->sh_size;
   9340 	  contents = _bfd_mmap_temporary (abfd, contents_size,
   9341 					  &contents_addr, &contents_size);
   9342 	  if (contents == NULL)
   9343 	    goto error_return_verref;
   9344 
   9345 	  verneed_size = hdr->sh_size;
   9346 	  verneed_count = hdr->sh_info;
   9347 	}
   9348 
   9349       if (_bfd_mul_overflow (verneed_count,
   9350 			     sizeof (Elf_Internal_Verneed), &amt))
   9351 	{
   9352 	  bfd_set_error (bfd_error_file_too_big);
   9353 	  goto error_return_verref;
   9354 	}
   9355       if (amt == 0)
   9356 	goto error_return_verref;
   9357       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
   9358       if (elf_tdata (abfd)->verref == NULL)
   9359 	goto error_return_verref;
   9360 
   9361       BFD_ASSERT (sizeof (Elf_External_Verneed)
   9362 		  == sizeof (Elf_External_Vernaux));
   9363       contents_end = (contents + verneed_size
   9364 		      - sizeof (Elf_External_Verneed));
   9365       everneed = (Elf_External_Verneed *) contents;
   9366       iverneed = elf_tdata (abfd)->verref;
   9367       for (i = 0; i < verneed_count; i++, iverneed++)
   9368 	{
   9369 	  Elf_External_Vernaux *evernaux;
   9370 	  Elf_Internal_Vernaux *ivernaux;
   9371 	  unsigned int j;
   9372 
   9373 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   9374 
   9375 	  iverneed->vn_bfd = abfd;
   9376 
   9377 	  if (elf_use_dt_symtab_p (abfd))
   9378 	    {
   9379 	      if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
   9380 		iverneed->vn_filename
   9381 		  = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
   9382 	      else
   9383 		iverneed->vn_filename = NULL;
   9384 	    }
   9385 	  else if (hdr == NULL)
   9386 	    goto error_return_bad_verref;
   9387 	  else
   9388 	    iverneed->vn_filename
   9389 	      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9390 						 iverneed->vn_file);
   9391 	  if (iverneed->vn_filename == NULL)
   9392 	    goto error_return_bad_verref;
   9393 
   9394 	  if (iverneed->vn_cnt == 0)
   9395 	    iverneed->vn_auxptr = NULL;
   9396 	  else
   9397 	    {
   9398 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   9399 				     sizeof (Elf_Internal_Vernaux), &amt))
   9400 		{
   9401 		  bfd_set_error (bfd_error_file_too_big);
   9402 		  goto error_return_verref;
   9403 		}
   9404 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   9405 		bfd_alloc (abfd, amt);
   9406 	      if (iverneed->vn_auxptr == NULL)
   9407 		goto error_return_verref;
   9408 	    }
   9409 
   9410 	  if (iverneed->vn_aux
   9411 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9412 	    goto error_return_bad_verref;
   9413 
   9414 	  evernaux = ((Elf_External_Vernaux *)
   9415 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   9416 	  ivernaux = iverneed->vn_auxptr;
   9417 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   9418 	    {
   9419 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   9420 
   9421 	      if (elf_use_dt_symtab_p (abfd))
   9422 		{
   9423 		  if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
   9424 		    ivernaux->vna_nodename
   9425 		      = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
   9426 		  else
   9427 		    ivernaux->vna_nodename = NULL;
   9428 		}
   9429 	      else if (hdr == NULL)
   9430 		goto error_return_bad_verref;
   9431 	      else
   9432 		ivernaux->vna_nodename
   9433 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9434 						     ivernaux->vna_name);
   9435 	      if (ivernaux->vna_nodename == NULL)
   9436 		goto error_return_bad_verref;
   9437 
   9438 	      if (ivernaux->vna_other > freeidx)
   9439 		freeidx = ivernaux->vna_other;
   9440 
   9441 	      ivernaux->vna_nextptr = NULL;
   9442 	      if (ivernaux->vna_next == 0)
   9443 		{
   9444 		  iverneed->vn_cnt = j + 1;
   9445 		  break;
   9446 		}
   9447 	      if (j + 1 < iverneed->vn_cnt)
   9448 		ivernaux->vna_nextptr = ivernaux + 1;
   9449 
   9450 	      if (ivernaux->vna_next
   9451 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   9452 		goto error_return_bad_verref;
   9453 
   9454 	      evernaux = ((Elf_External_Vernaux *)
   9455 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   9456 	    }
   9457 
   9458 	  iverneed->vn_nextref = NULL;
   9459 	  if (iverneed->vn_next == 0)
   9460 	    break;
   9461 	  if (hdr != NULL && (i + 1 < hdr->sh_info))
   9462 	    iverneed->vn_nextref = iverneed + 1;
   9463 
   9464 	  if (iverneed->vn_next
   9465 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9466 	    goto error_return_bad_verref;
   9467 
   9468 	  everneed = ((Elf_External_Verneed *)
   9469 		      ((bfd_byte *) everneed + iverneed->vn_next));
   9470 	}
   9471       elf_tdata (abfd)->cverrefs = i;
   9472 
   9473       if (contents != elf_tdata (abfd)->dt_verneed)
   9474 	_bfd_munmap_temporary (contents_addr, contents_size);
   9475       contents = NULL;
   9476       contents_addr = NULL;
   9477     }
   9478 
   9479   if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
   9480     {
   9481       Elf_Internal_Shdr *hdr;
   9482       Elf_External_Verdef *everdef;
   9483       Elf_Internal_Verdef *iverdef;
   9484       Elf_Internal_Verdef *iverdefarr;
   9485       Elf_Internal_Verdef iverdefmem;
   9486       unsigned int i;
   9487       unsigned int maxidx;
   9488       bfd_byte *contents_end_def, *contents_end_aux;
   9489       size_t verdef_count;
   9490       size_t verdef_size;
   9491 
   9492       if (elf_tdata (abfd)->dt_verdef != NULL)
   9493 	{
   9494 	  hdr = NULL;
   9495 	  contents = elf_tdata (abfd)->dt_verdef;
   9496 	  verdef_count = elf_tdata (abfd)->dt_verdef_count;
   9497 	  verdef_size = verdef_count * sizeof (Elf_External_Verdef);
   9498 	}
   9499       else
   9500 	{
   9501 	  hdr = &elf_tdata (abfd)->dynverdef_hdr;
   9502 
   9503 	  if (hdr->sh_size < sizeof (Elf_External_Verdef))
   9504 	    {
   9505 	    error_return_bad_verdef:
   9506 	      _bfd_error_handler
   9507 		(_("%pB: .gnu.version_d invalid entry"), abfd);
   9508 	      bfd_set_error (bfd_error_bad_value);
   9509 	    error_return_verdef:
   9510 	      elf_tdata (abfd)->verdef = NULL;
   9511 	      elf_tdata (abfd)->cverdefs = 0;
   9512 	      goto error_return;
   9513 	    }
   9514 
   9515 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9516 	    goto error_return_verdef;
   9517 	  contents_size = hdr->sh_size;
   9518 	  contents = _bfd_mmap_temporary (abfd, contents_size,
   9519 					  &contents_addr, &contents_size);
   9520 	  if (contents == NULL)
   9521 	    goto error_return_verdef;
   9522 
   9523 	  BFD_ASSERT (sizeof (Elf_External_Verdef)
   9524 		      >= sizeof (Elf_External_Verdaux));
   9525 
   9526 	  verdef_count = hdr->sh_info;
   9527 	  verdef_size = hdr->sh_size;
   9528 	}
   9529 
   9530       contents_end_def = (contents + verdef_size
   9531 			  - sizeof (Elf_External_Verdef));
   9532       contents_end_aux = (contents + verdef_size
   9533 			  - sizeof (Elf_External_Verdaux));
   9534 
   9535       /* We know the number of entries in the section but not the maximum
   9536 	 index.  Therefore we have to run through all entries and find
   9537 	 the maximum.  */
   9538       everdef = (Elf_External_Verdef *) contents;
   9539       maxidx = 0;
   9540       for (i = 0; i < verdef_count; ++i)
   9541 	{
   9542 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9543 
   9544 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9545 	    goto error_return_bad_verdef;
   9546 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9547 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9548 
   9549 	  if (iverdefmem.vd_next == 0)
   9550 	    break;
   9551 
   9552 	  if (iverdefmem.vd_next
   9553 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9554 	    goto error_return_bad_verdef;
   9555 
   9556 	  everdef = ((Elf_External_Verdef *)
   9557 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9558 	}
   9559 
   9560       if (default_imported_symver)
   9561 	{
   9562 	  if (freeidx > maxidx)
   9563 	    maxidx = ++freeidx;
   9564 	  else
   9565 	    freeidx = ++maxidx;
   9566 	}
   9567       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9568 	{
   9569 	  bfd_set_error (bfd_error_file_too_big);
   9570 	  goto error_return_verdef;
   9571 	}
   9572 
   9573       if (amt == 0)
   9574 	goto error_return_verdef;
   9575       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9576       if (elf_tdata (abfd)->verdef == NULL)
   9577 	goto error_return_verdef;
   9578 
   9579       elf_tdata (abfd)->cverdefs = maxidx;
   9580 
   9581       everdef = (Elf_External_Verdef *) contents;
   9582       iverdefarr = elf_tdata (abfd)->verdef;
   9583       for (i = 0; i < verdef_count; ++i)
   9584 	{
   9585 	  Elf_External_Verdaux *everdaux;
   9586 	  Elf_Internal_Verdaux *iverdaux;
   9587 	  unsigned int j;
   9588 
   9589 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9590 
   9591 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9592 	    goto error_return_bad_verdef;
   9593 
   9594 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9595 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9596 
   9597 	  iverdef->vd_bfd = abfd;
   9598 
   9599 	  if (iverdef->vd_cnt == 0)
   9600 	    iverdef->vd_auxptr = NULL;
   9601 	  else
   9602 	    {
   9603 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9604 				     sizeof (Elf_Internal_Verdaux), &amt))
   9605 		{
   9606 		  bfd_set_error (bfd_error_file_too_big);
   9607 		  goto error_return_verdef;
   9608 		}
   9609 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9610 		bfd_alloc (abfd, amt);
   9611 	      if (iverdef->vd_auxptr == NULL)
   9612 		goto error_return_verdef;
   9613 	    }
   9614 
   9615 	  if (iverdef->vd_aux
   9616 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9617 	    goto error_return_bad_verdef;
   9618 
   9619 	  everdaux = ((Elf_External_Verdaux *)
   9620 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9621 	  iverdaux = iverdef->vd_auxptr;
   9622 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9623 	    {
   9624 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9625 
   9626 	      if (elf_use_dt_symtab_p (abfd))
   9627 		{
   9628 		  if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
   9629 		    iverdaux->vda_nodename
   9630 		      = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
   9631 		  else
   9632 		    iverdaux->vda_nodename = NULL;
   9633 		}
   9634 	      else
   9635 		iverdaux->vda_nodename
   9636 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9637 						     iverdaux->vda_name);
   9638 	      if (iverdaux->vda_nodename == NULL)
   9639 		goto error_return_bad_verdef;
   9640 
   9641 	      iverdaux->vda_nextptr = NULL;
   9642 	      if (iverdaux->vda_next == 0)
   9643 		{
   9644 		  iverdef->vd_cnt = j + 1;
   9645 		  break;
   9646 		}
   9647 	      if (j + 1 < iverdef->vd_cnt)
   9648 		iverdaux->vda_nextptr = iverdaux + 1;
   9649 
   9650 	      if (iverdaux->vda_next
   9651 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9652 		goto error_return_bad_verdef;
   9653 
   9654 	      everdaux = ((Elf_External_Verdaux *)
   9655 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9656 	    }
   9657 
   9658 	  iverdef->vd_nodename = NULL;
   9659 	  if (iverdef->vd_cnt)
   9660 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9661 
   9662 	  iverdef->vd_nextdef = NULL;
   9663 	  if (iverdef->vd_next == 0)
   9664 	    break;
   9665 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9666 	    iverdef->vd_nextdef = iverdef + 1;
   9667 
   9668 	  everdef = ((Elf_External_Verdef *)
   9669 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9670 	}
   9671 
   9672       if (contents != elf_tdata (abfd)->dt_verdef)
   9673 	_bfd_munmap_temporary (contents_addr, contents_size);
   9674       contents = NULL;
   9675       contents_addr = NULL;
   9676     }
   9677   else if (default_imported_symver)
   9678     {
   9679       if (freeidx < 3)
   9680 	freeidx = 3;
   9681       else
   9682 	freeidx++;
   9683 
   9684       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9685 	{
   9686 	  bfd_set_error (bfd_error_file_too_big);
   9687 	  goto error_return;
   9688 	}
   9689       if (amt == 0)
   9690 	goto error_return;
   9691       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9692       if (elf_tdata (abfd)->verdef == NULL)
   9693 	goto error_return;
   9694 
   9695       elf_tdata (abfd)->cverdefs = freeidx;
   9696     }
   9697 
   9698   /* Create a default version based on the soname.  */
   9699   if (default_imported_symver)
   9700     {
   9701       Elf_Internal_Verdef *iverdef;
   9702       Elf_Internal_Verdaux *iverdaux;
   9703 
   9704       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9705 
   9706       iverdef->vd_version = VER_DEF_CURRENT;
   9707       iverdef->vd_flags = 0;
   9708       iverdef->vd_ndx = freeidx;
   9709       iverdef->vd_cnt = 1;
   9710 
   9711       iverdef->vd_bfd = abfd;
   9712 
   9713       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9714       if (iverdef->vd_nodename == NULL)
   9715 	goto error_return_verdef;
   9716       iverdef->vd_nextdef = NULL;
   9717       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9718 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9719       if (iverdef->vd_auxptr == NULL)
   9720 	goto error_return_verdef;
   9721 
   9722       iverdaux = iverdef->vd_auxptr;
   9723       iverdaux->vda_nodename = iverdef->vd_nodename;
   9724     }
   9725 
   9726   return true;
   9727 
   9728  error_return:
   9729   if (contents != elf_tdata (abfd)->dt_verneed
   9730       && contents != elf_tdata (abfd)->dt_verdef)
   9731     _bfd_munmap_temporary (contents_addr, contents_size);
   9732   return false;
   9733 }
   9734 
   9735 asymbol *
   9737 _bfd_elf_make_empty_symbol (bfd *abfd)
   9738 {
   9739   elf_symbol_type *newsym;
   9740 
   9741   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9742   if (!newsym)
   9743     return NULL;
   9744   newsym->symbol.the_bfd = abfd;
   9745   return &newsym->symbol;
   9746 }
   9747 
   9748 void
   9749 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9750 			  asymbol *symbol,
   9751 			  symbol_info *ret)
   9752 {
   9753   bfd_symbol_info (symbol, ret);
   9754 }
   9755 
   9756 /* Return whether a symbol name implies a local symbol.  Most targets
   9757    use this function for the is_local_label_name entry point, but some
   9758    override it.  */
   9759 
   9760 bool
   9761 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9762 			      const char *name)
   9763 {
   9764   /* Normal local symbols start with ``.L''.  */
   9765   if (name[0] == '.' && name[1] == 'L')
   9766     return true;
   9767 
   9768   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9769      DWARF debugging symbols starting with ``..''.  */
   9770   if (name[0] == '.' && name[1] == '.')
   9771     return true;
   9772 
   9773   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9774      emitting DWARF debugging output.  I suspect this is actually a
   9775      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9776      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9777      underscore to be emitted on some ELF targets).  For ease of use,
   9778      we treat such symbols as local.  */
   9779   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9780     return true;
   9781 
   9782   /* Treat assembler generated fake symbols, dollar local labels and
   9783      forward-backward labels (aka local labels) as locals.
   9784      These labels have the form:
   9785 
   9786        L0^A.*				       (fake symbols)
   9787 
   9788        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9789 
   9790      Versions which start with .L will have already been matched above,
   9791      so we only need to match the rest.  */
   9792   if (name[0] == 'L' && ISDIGIT (name[1]))
   9793     {
   9794       bool ret = false;
   9795       const char * p;
   9796       char c;
   9797 
   9798       for (p = name + 2; (c = *p); p++)
   9799 	{
   9800 	  if (c == 1 || c == 2)
   9801 	    {
   9802 	      if (c == 1 && p == name + 2)
   9803 		/* A fake symbol.  */
   9804 		return true;
   9805 
   9806 	      /* FIXME: We are being paranoid here and treating symbols like
   9807 		 L0^Bfoo as if there were non-local, on the grounds that the
   9808 		 assembler will never generate them.  But can any symbol
   9809 		 containing an ASCII value in the range 1-31 ever be anything
   9810 		 other than some kind of local ?  */
   9811 	      ret = true;
   9812 	    }
   9813 
   9814 	  if (! ISDIGIT (c))
   9815 	    {
   9816 	      ret = false;
   9817 	      break;
   9818 	    }
   9819 	}
   9820       return ret;
   9821     }
   9822 
   9823   return false;
   9824 }
   9825 
   9826 alent *
   9827 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9828 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9829 {
   9830   abort ();
   9831   return NULL;
   9832 }
   9833 
   9834 bool
   9835 _bfd_elf_set_arch_mach (bfd *abfd,
   9836 			enum bfd_architecture arch,
   9837 			unsigned long machine)
   9838 {
   9839   /* If this isn't the right architecture for this backend, and this
   9840      isn't the generic backend, fail.  */
   9841   if (arch != get_elf_backend_data (abfd)->arch
   9842       && arch != bfd_arch_unknown
   9843       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9844     return false;
   9845 
   9846   return bfd_default_set_arch_mach (abfd, arch, machine);
   9847 }
   9848 
   9849 /* Find the nearest line to a particular section and offset,
   9850    for error reporting.  */
   9851 
   9852 bool
   9853 _bfd_elf_find_nearest_line (bfd *abfd,
   9854 			    asymbol **symbols,
   9855 			    asection *section,
   9856 			    bfd_vma offset,
   9857 			    const char **filename_ptr,
   9858 			    const char **functionname_ptr,
   9859 			    unsigned int *line_ptr,
   9860 			    unsigned int *discriminator_ptr)
   9861 {
   9862   return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
   9863 					      offset, filename_ptr,
   9864 					      functionname_ptr, line_ptr,
   9865 					      discriminator_ptr);
   9866 }
   9867 
   9868 /* Find the nearest line to a particular section and offset,
   9869    for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
   9870    can be optionally specified.  */
   9871 
   9872 bool
   9873 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
   9874 				     const char *alt_filename,
   9875 				     asymbol **symbols,
   9876 				     asection *section,
   9877 				     bfd_vma offset,
   9878 				     const char **filename_ptr,
   9879 				     const char **functionname_ptr,
   9880 				     unsigned int *line_ptr,
   9881 				     unsigned int *discriminator_ptr)
   9882 {
   9883   bool found;
   9884 
   9885   if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
   9886 					      section, offset, filename_ptr,
   9887 					      functionname_ptr, line_ptr,
   9888 					      discriminator_ptr,
   9889 					      dwarf_debug_sections,
   9890 					      &elf_tdata (abfd)->dwarf2_find_line_info))
   9891     return true;
   9892 
   9893   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   9894 				     filename_ptr, functionname_ptr, line_ptr))
   9895     {
   9896       if (!*functionname_ptr)
   9897 	_bfd_elf_find_function (abfd, symbols, section, offset,
   9898 				*filename_ptr ? NULL : filename_ptr,
   9899 				functionname_ptr);
   9900       return true;
   9901     }
   9902 
   9903   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   9904 					     &found, filename_ptr,
   9905 					     functionname_ptr, line_ptr,
   9906 					     &elf_tdata (abfd)->line_info))
   9907     return false;
   9908   if (found && (*functionname_ptr || *line_ptr))
   9909     return true;
   9910 
   9911   if (symbols == NULL)
   9912     return false;
   9913 
   9914   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   9915 				filename_ptr, functionname_ptr))
   9916     return false;
   9917 
   9918   *line_ptr = 0;
   9919   return true;
   9920 }
   9921 
   9922 /* Find the line for a symbol.  */
   9923 
   9924 bool
   9925 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   9926 		    const char **filename_ptr, unsigned int *line_ptr)
   9927 {
   9928   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9929   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   9930 					filename_ptr, NULL, line_ptr, NULL,
   9931 					dwarf_debug_sections,
   9932 					&tdata->dwarf2_find_line_info);
   9933 }
   9934 
   9935 /* After a call to bfd_find_nearest_line, successive calls to
   9936    bfd_find_inliner_info can be used to get source information about
   9937    each level of function inlining that terminated at the address
   9938    passed to bfd_find_nearest_line.  Currently this is only supported
   9939    for DWARF2 with appropriate DWARF3 extensions. */
   9940 
   9941 bool
   9942 _bfd_elf_find_inliner_info (bfd *abfd,
   9943 			    const char **filename_ptr,
   9944 			    const char **functionname_ptr,
   9945 			    unsigned int *line_ptr)
   9946 {
   9947   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9948   return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   9949 					functionname_ptr, line_ptr,
   9950 					&tdata->dwarf2_find_line_info);
   9951 }
   9952 
   9953 int
   9954 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   9955 {
   9956   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   9957   int ret = bed->s->sizeof_ehdr;
   9958 
   9959   if (!bfd_link_relocatable (info))
   9960     {
   9961       bfd_size_type phdr_size = elf_program_header_size (abfd);
   9962 
   9963       if (phdr_size == (bfd_size_type) -1)
   9964 	{
   9965 	  struct elf_segment_map *m;
   9966 
   9967 	  phdr_size = 0;
   9968 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   9969 	    phdr_size += bed->s->sizeof_phdr;
   9970 
   9971 	  if (phdr_size == 0)
   9972 	    phdr_size = get_program_header_size (abfd, info);
   9973 	}
   9974 
   9975       elf_program_header_size (abfd) = phdr_size;
   9976       ret += phdr_size;
   9977     }
   9978 
   9979   return ret;
   9980 }
   9981 
   9982 bool
   9983 _bfd_elf_set_section_contents (bfd *abfd,
   9984 			       sec_ptr section,
   9985 			       const void *location,
   9986 			       file_ptr offset,
   9987 			       bfd_size_type count)
   9988 {
   9989   Elf_Internal_Shdr *hdr;
   9990 
   9991   if (! abfd->output_has_begun
   9992       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   9993     return false;
   9994 
   9995   if (!count)
   9996     return true;
   9997 
   9998   hdr = &elf_section_data (section)->this_hdr;
   9999   if (hdr->sh_offset == (file_ptr) -1)
   10000     {
   10001       unsigned char *contents;
   10002 
   10003       if (bfd_section_is_ctf (section))
   10004 	/* Nothing to do with this section: the contents are generated
   10005 	   later.  */
   10006 	return true;
   10007 
   10008       if ((offset + count) > hdr->sh_size)
   10009 	{
   10010 	  _bfd_error_handler
   10011 	    (_("%pB:%pA: error: attempting to write"
   10012 	       " over the end of the section"),
   10013 	     abfd, section);
   10014 
   10015 	  bfd_set_error (bfd_error_invalid_operation);
   10016 	  return false;
   10017 	}
   10018 
   10019       contents = hdr->contents;
   10020       if (contents == NULL)
   10021 	{
   10022 	  _bfd_error_handler
   10023 	    (_("%pB:%pA: error: attempting to write"
   10024 	       " section into an empty buffer"),
   10025 	     abfd, section);
   10026 
   10027 	  bfd_set_error (bfd_error_invalid_operation);
   10028 	  return false;
   10029 	}
   10030 
   10031       memcpy (contents + offset, location, count);
   10032       return true;
   10033     }
   10034 
   10035   return _bfd_generic_set_section_contents (abfd, section,
   10036 					    location, offset, count);
   10037 }
   10038 
   10039 bool
   10040 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   10041 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   10042 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   10043 {
   10044   abort ();
   10045   return false;
   10046 }
   10047 
   10048 /* Try to convert a non-ELF reloc into an ELF one.  */
   10049 
   10050 bool
   10051 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   10052 {
   10053   /* Check whether we really have an ELF howto.  */
   10054 
   10055   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   10056     {
   10057       bfd_reloc_code_real_type code;
   10058       reloc_howto_type *howto;
   10059 
   10060       /* Alien reloc: Try to determine its type to replace it with an
   10061 	 equivalent ELF reloc.  */
   10062 
   10063       if (areloc->howto->pc_relative)
   10064 	{
   10065 	  switch (areloc->howto->bitsize)
   10066 	    {
   10067 	    case 8:
   10068 	      code = BFD_RELOC_8_PCREL;
   10069 	      break;
   10070 	    case 12:
   10071 	      code = BFD_RELOC_12_PCREL;
   10072 	      break;
   10073 	    case 16:
   10074 	      code = BFD_RELOC_16_PCREL;
   10075 	      break;
   10076 	    case 24:
   10077 	      code = BFD_RELOC_24_PCREL;
   10078 	      break;
   10079 	    case 32:
   10080 	      code = BFD_RELOC_32_PCREL;
   10081 	      break;
   10082 	    case 64:
   10083 	      code = BFD_RELOC_64_PCREL;
   10084 	      break;
   10085 	    default:
   10086 	      goto fail;
   10087 	    }
   10088 
   10089 	  howto = bfd_reloc_type_lookup (abfd, code);
   10090 
   10091 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   10092 	    {
   10093 	      if (howto->pcrel_offset)
   10094 		areloc->addend += areloc->address;
   10095 	      else
   10096 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   10097 	    }
   10098 	}
   10099       else
   10100 	{
   10101 	  switch (areloc->howto->bitsize)
   10102 	    {
   10103 	    case 8:
   10104 	      code = BFD_RELOC_8;
   10105 	      break;
   10106 	    case 14:
   10107 	      code = BFD_RELOC_14;
   10108 	      break;
   10109 	    case 16:
   10110 	      code = BFD_RELOC_16;
   10111 	      break;
   10112 	    case 26:
   10113 	      code = BFD_RELOC_26;
   10114 	      break;
   10115 	    case 32:
   10116 	      code = BFD_RELOC_32;
   10117 	      break;
   10118 	    case 64:
   10119 	      code = BFD_RELOC_64;
   10120 	      break;
   10121 	    default:
   10122 	      goto fail;
   10123 	    }
   10124 
   10125 	  howto = bfd_reloc_type_lookup (abfd, code);
   10126 	}
   10127 
   10128       if (howto)
   10129 	areloc->howto = howto;
   10130       else
   10131 	goto fail;
   10132     }
   10133 
   10134   return true;
   10135 
   10136  fail:
   10137   /* xgettext:c-format */
   10138   _bfd_error_handler (_("%pB: %s unsupported"),
   10139 		      abfd, areloc->howto->name);
   10140   bfd_set_error (bfd_error_sorry);
   10141   return false;
   10142 }
   10143 
   10144 bool
   10145 _bfd_elf_free_cached_info (bfd *abfd)
   10146 {
   10147   struct elf_obj_tdata *tdata;
   10148 
   10149   if ((bfd_get_format (abfd) == bfd_object
   10150        || bfd_get_format (abfd) == bfd_core)
   10151       && (tdata = elf_tdata (abfd)) != NULL)
   10152     {
   10153       if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
   10154 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   10155       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   10156       _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
   10157       _bfd_stab_cleanup (abfd, &tdata->line_info);
   10158       for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
   10159 	{
   10160 	  _bfd_elf_munmap_section_contents (sec, sec->contents);
   10161 	  if (!sec->alloced)
   10162 	    {
   10163 	      free (elf_section_data (sec)->this_hdr.contents);
   10164 	      elf_section_data (sec)->this_hdr.contents = NULL;
   10165 	    }
   10166 	  free (elf_section_data (sec)->relocs);
   10167 	  elf_section_data (sec)->relocs = NULL;
   10168 	  if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
   10169 	    {
   10170 	      struct eh_frame_sec_info *sec_info
   10171 		= elf_section_data (sec)->sec_info;
   10172 	      free (sec_info->cies);
   10173 	    }
   10174 	}
   10175       free (tdata->symtab_hdr.contents);
   10176       tdata->symtab_hdr.contents = NULL;
   10177     }
   10178 
   10179   return _bfd_generic_bfd_free_cached_info (abfd);
   10180 }
   10181 
   10182 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   10183    in the relocation's offset.  Thus we cannot allow any sort of sanity
   10184    range-checking to interfere.  There is nothing else to do in processing
   10185    this reloc.  */
   10186 
   10187 bfd_reloc_status_type
   10188 _bfd_elf_rel_vtable_reloc_fn
   10189   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   10190    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   10191    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   10192    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   10193 {
   10194   return bfd_reloc_ok;
   10195 }
   10196 
   10197 /* Elf core file support.  Much of this only works on native
   10199    toolchains, since we rely on knowing the
   10200    machine-dependent procfs structure in order to pick
   10201    out details about the corefile.  */
   10202 
   10203 #ifdef HAVE_SYS_PROCFS_H
   10204 # include <sys/procfs.h>
   10205 #endif
   10206 
   10207 /* Return a PID that identifies a "thread" for threaded cores, or the
   10208    PID of the main process for non-threaded cores.  */
   10209 
   10210 static int
   10211 elfcore_make_pid (bfd *abfd)
   10212 {
   10213   int pid;
   10214 
   10215   pid = elf_tdata (abfd)->core->lwpid;
   10216   if (pid == 0)
   10217     pid = elf_tdata (abfd)->core->pid;
   10218 
   10219   return pid;
   10220 }
   10221 
   10222 /* If there isn't a section called NAME, make one, using data from
   10223    SECT.  Note, this function will generate a reference to NAME, so
   10224    you shouldn't deallocate or overwrite it.  */
   10225 
   10226 static bool
   10227 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   10228 {
   10229   asection *sect2;
   10230 
   10231   if (bfd_get_section_by_name (abfd, name) != NULL)
   10232     return true;
   10233 
   10234   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   10235   if (sect2 == NULL)
   10236     return false;
   10237 
   10238   sect2->size = sect->size;
   10239   sect2->filepos = sect->filepos;
   10240   sect2->alignment_power = sect->alignment_power;
   10241   return true;
   10242 }
   10243 
   10244 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   10245    actually creates up to two pseudosections:
   10246    - For the single-threaded case, a section named NAME, unless
   10247      such a section already exists.
   10248    - For the multi-threaded case, a section named "NAME/PID", where
   10249      PID is elfcore_make_pid (abfd).
   10250    Both pseudosections have identical contents.  */
   10251 bool
   10252 _bfd_elfcore_make_pseudosection (bfd *abfd,
   10253 				 char *name,
   10254 				 size_t size,
   10255 				 ufile_ptr filepos)
   10256 {
   10257   char buf[100];
   10258   char *threaded_name;
   10259   size_t len;
   10260   asection *sect;
   10261 
   10262   /* Build the section name.  */
   10263 
   10264   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   10265   len = strlen (buf) + 1;
   10266   threaded_name = (char *) bfd_alloc (abfd, len);
   10267   if (threaded_name == NULL)
   10268     return false;
   10269   memcpy (threaded_name, buf, len);
   10270 
   10271   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   10272 					     SEC_HAS_CONTENTS);
   10273   if (sect == NULL)
   10274     return false;
   10275   sect->size = size;
   10276   sect->filepos = filepos;
   10277   sect->alignment_power = 2;
   10278 
   10279   return elfcore_maybe_make_sect (abfd, name, sect);
   10280 }
   10281 
   10282 static bool
   10283 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   10284 				size_t offs)
   10285 {
   10286   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   10287 						       SEC_HAS_CONTENTS);
   10288 
   10289   if (sect == NULL)
   10290     return false;
   10291 
   10292   sect->size = note->descsz - offs;
   10293   sect->filepos = note->descpos + offs;
   10294   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10295 
   10296   return true;
   10297 }
   10298 
   10299 /* prstatus_t exists on:
   10300      solaris 2.5+
   10301      linux 2.[01] + glibc
   10302      unixware 4.2
   10303 */
   10304 
   10305 #if defined (HAVE_PRSTATUS_T)
   10306 
   10307 static bool
   10308 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10309 {
   10310   size_t size;
   10311   int offset;
   10312 
   10313   if (note->descsz == sizeof (prstatus_t))
   10314     {
   10315       prstatus_t prstat;
   10316 
   10317       size = sizeof (prstat.pr_reg);
   10318       offset   = offsetof (prstatus_t, pr_reg);
   10319       memcpy (&prstat, note->descdata, sizeof (prstat));
   10320 
   10321       /* Do not overwrite the core signal if it
   10322 	 has already been set by another thread.  */
   10323       if (elf_tdata (abfd)->core->signal == 0)
   10324 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10325       if (elf_tdata (abfd)->core->pid == 0)
   10326 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10327 
   10328       /* pr_who exists on:
   10329 	 solaris 2.5+
   10330 	 unixware 4.2
   10331 	 pr_who doesn't exist on:
   10332 	 linux 2.[01]
   10333 	 */
   10334 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   10335       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10336 #else
   10337       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10338 #endif
   10339     }
   10340 #if defined (HAVE_PRSTATUS32_T)
   10341   else if (note->descsz == sizeof (prstatus32_t))
   10342     {
   10343       /* 64-bit host, 32-bit corefile */
   10344       prstatus32_t prstat;
   10345 
   10346       size = sizeof (prstat.pr_reg);
   10347       offset   = offsetof (prstatus32_t, pr_reg);
   10348       memcpy (&prstat, note->descdata, sizeof (prstat));
   10349 
   10350       /* Do not overwrite the core signal if it
   10351 	 has already been set by another thread.  */
   10352       if (elf_tdata (abfd)->core->signal == 0)
   10353 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10354       if (elf_tdata (abfd)->core->pid == 0)
   10355 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10356 
   10357       /* pr_who exists on:
   10358 	 solaris 2.5+
   10359 	 unixware 4.2
   10360 	 pr_who doesn't exist on:
   10361 	 linux 2.[01]
   10362 	 */
   10363 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   10364       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10365 #else
   10366       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10367 #endif
   10368     }
   10369 #endif /* HAVE_PRSTATUS32_T */
   10370   else
   10371     {
   10372       /* Fail - we don't know how to handle any other
   10373 	 note size (ie. data object type).  */
   10374       return true;
   10375     }
   10376 
   10377   /* Make a ".reg/999" section and a ".reg" section.  */
   10378   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   10379 					  size, note->descpos + offset);
   10380 }
   10381 #endif /* defined (HAVE_PRSTATUS_T) */
   10382 
   10383 /* Create a pseudosection containing the exact contents of NOTE.  */
   10384 static bool
   10385 elfcore_make_note_pseudosection (bfd *abfd,
   10386 				 char *name,
   10387 				 Elf_Internal_Note *note)
   10388 {
   10389   return _bfd_elfcore_make_pseudosection (abfd, name,
   10390 					  note->descsz, note->descpos);
   10391 }
   10392 
   10393 /* There isn't a consistent prfpregset_t across platforms,
   10394    but it doesn't matter, because we don't have to pick this
   10395    data structure apart.  */
   10396 
   10397 static bool
   10398 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   10399 {
   10400   return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   10401 }
   10402 
   10403 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   10404    type of NT_PRXFPREG.  Just include the whole note's contents
   10405    literally.  */
   10406 
   10407 static bool
   10408 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   10409 {
   10410   return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   10411 }
   10412 
   10413 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   10414    with a note type of NT_X86_XSTATE.  Just include the whole note's
   10415    contents literally.  */
   10416 
   10417 static bool
   10418 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   10419 {
   10420   return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
   10421 }
   10422 
   10423 static bool
   10424 elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
   10425 {
   10426   return elfcore_make_note_pseudosection (abfd, ".reg-ssp", note);
   10427 }
   10428 
   10429 static bool
   10430 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   10431 {
   10432   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
   10433 }
   10434 
   10435 static bool
   10436 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   10437 {
   10438   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
   10439 }
   10440 
   10441 static bool
   10442 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   10443 {
   10444   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
   10445 }
   10446 
   10447 static bool
   10448 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   10449 {
   10450   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
   10451 }
   10452 
   10453 static bool
   10454 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   10455 {
   10456   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
   10457 }
   10458 
   10459 static bool
   10460 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   10461 {
   10462   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
   10463 }
   10464 
   10465 static bool
   10466 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   10467 {
   10468   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
   10469 }
   10470 
   10471 static bool
   10472 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   10473 {
   10474   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
   10475 }
   10476 
   10477 static bool
   10478 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   10479 {
   10480   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
   10481 }
   10482 
   10483 static bool
   10484 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   10485 {
   10486   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
   10487 }
   10488 
   10489 static bool
   10490 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   10491 {
   10492   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
   10493 }
   10494 
   10495 static bool
   10496 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   10497 {
   10498   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
   10499 }
   10500 
   10501 static bool
   10502 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   10503 {
   10504   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
   10505 }
   10506 
   10507 static bool
   10508 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   10509 {
   10510   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
   10511 }
   10512 
   10513 static bool
   10514 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   10515 {
   10516   return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
   10517 }
   10518 
   10519 static bool
   10520 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   10521 {
   10522   return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
   10523 }
   10524 
   10525 static bool
   10526 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   10527 {
   10528   return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
   10529 }
   10530 
   10531 static bool
   10532 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   10533 {
   10534   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
   10535 }
   10536 
   10537 static bool
   10538 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   10539 {
   10540   return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
   10541 }
   10542 
   10543 static bool
   10544 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   10545 {
   10546   return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
   10547 }
   10548 
   10549 static bool
   10550 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   10551 {
   10552   return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
   10553 }
   10554 
   10555 static bool
   10556 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   10557 {
   10558   return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
   10559 }
   10560 
   10561 static bool
   10562 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   10563 {
   10564   return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
   10565 }
   10566 
   10567 static bool
   10568 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   10569 {
   10570   return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
   10571 }
   10572 
   10573 static bool
   10574 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   10575 {
   10576   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
   10577 }
   10578 
   10579 static bool
   10580 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   10581 {
   10582   return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
   10583 }
   10584 
   10585 static bool
   10586 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   10587 {
   10588   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
   10589 }
   10590 
   10591 static bool
   10592 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10593 {
   10594   return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
   10595 }
   10596 
   10597 static bool
   10598 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10599 {
   10600   return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
   10601 }
   10602 
   10603 static bool
   10604 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10605 {
   10606   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
   10607 }
   10608 
   10609 static bool
   10610 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10611 {
   10612   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
   10613 }
   10614 
   10615 static bool
   10616 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10617 {
   10618   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
   10619 }
   10620 
   10621 static bool
   10622 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10623 {
   10624   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
   10625 }
   10626 
   10627 static bool
   10628 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10629 {
   10630   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
   10631 }
   10632 
   10633 static bool
   10634 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10635 {
   10636   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
   10637 					  note);
   10638 }
   10639 
   10640 static bool
   10641 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
   10642 {
   10643   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note);
   10644 }
   10645 
   10646 static bool
   10647 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
   10648 {
   10649   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note);
   10650 }
   10651 
   10652 /* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
   10653    successful, otherwise return FALSE.  */
   10654 
   10655 static bool
   10656 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
   10657 {
   10658   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note);
   10659 }
   10660 
   10661 /* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
   10662    successful, otherwise return FALSE.  */
   10663 
   10664 static bool
   10665 elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
   10666 {
   10667   return elfcore_make_note_pseudosection (abfd, ".reg-aarch-gcs", note);
   10668 }
   10669 
   10670 static bool
   10671 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10672 {
   10673   return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
   10674 }
   10675 
   10676 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10677    successful otherwise, return FALSE.  */
   10678 
   10679 static bool
   10680 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10681 {
   10682   return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
   10683 }
   10684 
   10685 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10686    successful otherwise, return FALSE.  */
   10687 
   10688 static bool
   10689 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10690 {
   10691   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10692 }
   10693 
   10694 static bool
   10695 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10696 {
   10697   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
   10698 }
   10699 
   10700 static bool
   10701 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10702 {
   10703   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
   10704 }
   10705 
   10706 static bool
   10707 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10708 {
   10709   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
   10710 }
   10711 
   10712 static bool
   10713 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10714 {
   10715   return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
   10716 }
   10717 
   10718 #if defined (HAVE_PRPSINFO_T)
   10719 typedef prpsinfo_t   elfcore_psinfo_t;
   10720 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10721 typedef prpsinfo32_t elfcore_psinfo32_t;
   10722 #endif
   10723 #endif
   10724 
   10725 #if defined (HAVE_PSINFO_T)
   10726 typedef psinfo_t   elfcore_psinfo_t;
   10727 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10728 typedef psinfo32_t elfcore_psinfo32_t;
   10729 #endif
   10730 #endif
   10731 
   10732 /* return a malloc'ed copy of a string at START which is at
   10733    most MAX bytes long, possibly without a terminating '\0'.
   10734    the copy will always have a terminating '\0'.  */
   10735 
   10736 char *
   10737 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10738 {
   10739   char *dups;
   10740   char *end = (char *) memchr (start, '\0', max);
   10741   size_t len;
   10742 
   10743   if (end == NULL)
   10744     len = max;
   10745   else
   10746     len = end - start;
   10747 
   10748   dups = (char *) bfd_alloc (abfd, len + 1);
   10749   if (dups == NULL)
   10750     return NULL;
   10751 
   10752   memcpy (dups, start, len);
   10753   dups[len] = '\0';
   10754 
   10755   return dups;
   10756 }
   10757 
   10758 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10759 static bool
   10760 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10761 {
   10762   if (note->descsz == sizeof (elfcore_psinfo_t))
   10763     {
   10764       elfcore_psinfo_t psinfo;
   10765 
   10766       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10767 
   10768 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   10769       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10770 #endif
   10771       elf_tdata (abfd)->core->program
   10772 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10773 				sizeof (psinfo.pr_fname));
   10774 
   10775       elf_tdata (abfd)->core->command
   10776 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10777 				sizeof (psinfo.pr_psargs));
   10778     }
   10779 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10780   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10781     {
   10782       /* 64-bit host, 32-bit corefile */
   10783       elfcore_psinfo32_t psinfo;
   10784 
   10785       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10786 
   10787 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10788       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10789 #endif
   10790       elf_tdata (abfd)->core->program
   10791 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10792 				sizeof (psinfo.pr_fname));
   10793 
   10794       elf_tdata (abfd)->core->command
   10795 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10796 				sizeof (psinfo.pr_psargs));
   10797     }
   10798 #endif
   10799 
   10800   else
   10801     {
   10802       /* Fail - we don't know how to handle any other
   10803 	 note size (ie. data object type).  */
   10804       return true;
   10805     }
   10806 
   10807   /* Note that for some reason, a spurious space is tacked
   10808      onto the end of the args in some (at least one anyway)
   10809      implementations, so strip it off if it exists.  */
   10810 
   10811   {
   10812     char *command = elf_tdata (abfd)->core->command;
   10813     int n = strlen (command);
   10814 
   10815     if (0 < n && command[n - 1] == ' ')
   10816       command[n - 1] = '\0';
   10817   }
   10818 
   10819   return true;
   10820 }
   10821 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10822 
   10823 #if defined (HAVE_PSTATUS_T)
   10824 static bool
   10825 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10826 {
   10827   if (note->descsz == sizeof (pstatus_t)
   10828 #if defined (HAVE_PXSTATUS_T)
   10829       || note->descsz == sizeof (pxstatus_t)
   10830 #endif
   10831       )
   10832     {
   10833       pstatus_t pstat;
   10834 
   10835       memcpy (&pstat, note->descdata, sizeof (pstat));
   10836 
   10837       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10838     }
   10839 #if defined (HAVE_PSTATUS32_T)
   10840   else if (note->descsz == sizeof (pstatus32_t))
   10841     {
   10842       /* 64-bit host, 32-bit corefile */
   10843       pstatus32_t pstat;
   10844 
   10845       memcpy (&pstat, note->descdata, sizeof (pstat));
   10846 
   10847       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10848     }
   10849 #endif
   10850   /* Could grab some more details from the "representative"
   10851      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10852      NT_LWPSTATUS note, presumably.  */
   10853 
   10854   return true;
   10855 }
   10856 #endif /* defined (HAVE_PSTATUS_T) */
   10857 
   10858 #if defined (HAVE_LWPSTATUS_T)
   10859 static bool
   10860 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10861 {
   10862   lwpstatus_t lwpstat;
   10863   char buf[100];
   10864   char *name;
   10865   size_t len;
   10866   asection *sect;
   10867 
   10868   if (note->descsz != sizeof (lwpstat)
   10869 #if defined (HAVE_LWPXSTATUS_T)
   10870       && note->descsz != sizeof (lwpxstatus_t)
   10871 #endif
   10872       )
   10873     return true;
   10874 
   10875   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10876 
   10877   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10878   /* Do not overwrite the core signal if it has already been set by
   10879      another thread.  */
   10880   if (elf_tdata (abfd)->core->signal == 0)
   10881     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10882 
   10883   /* Make a ".reg/999" section.  */
   10884 
   10885   sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
   10886   len = strlen (buf) + 1;
   10887   name = bfd_alloc (abfd, len);
   10888   if (name == NULL)
   10889     return false;
   10890   memcpy (name, buf, len);
   10891 
   10892   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10893   if (sect == NULL)
   10894     return false;
   10895 
   10896 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10897   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   10898   sect->filepos = note->descpos
   10899     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   10900 #endif
   10901 
   10902 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10903   sect->size = sizeof (lwpstat.pr_reg);
   10904   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   10905 #endif
   10906 
   10907   sect->alignment_power = 2;
   10908 
   10909   if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
   10910     return false;
   10911 
   10912   /* Make a ".reg2/999" section */
   10913 
   10914   sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
   10915   len = strlen (buf) + 1;
   10916   name = bfd_alloc (abfd, len);
   10917   if (name == NULL)
   10918     return false;
   10919   memcpy (name, buf, len);
   10920 
   10921   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10922   if (sect == NULL)
   10923     return false;
   10924 
   10925 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10926   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   10927   sect->filepos = note->descpos
   10928     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   10929 #endif
   10930 
   10931 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   10932   sect->size = sizeof (lwpstat.pr_fpreg);
   10933   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   10934 #endif
   10935 
   10936   sect->alignment_power = 2;
   10937 
   10938   return elfcore_maybe_make_sect (abfd, ".reg2", sect);
   10939 }
   10940 #endif /* defined (HAVE_LWPSTATUS_T) */
   10941 
   10942 /* These constants, and the structure offsets used below, are defined by
   10943    Cygwin's core_dump.h */
   10944 #define NOTE_INFO_PROCESS  1
   10945 #define NOTE_INFO_THREAD   2
   10946 #define NOTE_INFO_MODULE   3
   10947 #define NOTE_INFO_MODULE64 4
   10948 
   10949 static bool
   10950 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   10951 {
   10952   char buf[30];
   10953   char *name;
   10954   size_t len;
   10955   unsigned int name_size;
   10956   asection *sect;
   10957   unsigned int type;
   10958   int is_active_thread;
   10959   bfd_vma base_addr;
   10960 
   10961   if (note->descsz < 4)
   10962     return true;
   10963 
   10964   if (! startswith (note->namedata, "win32"))
   10965     return true;
   10966 
   10967   type = bfd_get_32 (abfd, note->descdata);
   10968 
   10969   struct
   10970   {
   10971     const char *type_name;
   10972     unsigned long min_size;
   10973   } size_check[] =
   10974       {
   10975        { "NOTE_INFO_PROCESS", 12 },
   10976        { "NOTE_INFO_THREAD", 12 },
   10977        { "NOTE_INFO_MODULE", 12 },
   10978        { "NOTE_INFO_MODULE64", 16 },
   10979       };
   10980 
   10981   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   10982       return true;
   10983 
   10984   if (note->descsz < size_check[type - 1].min_size)
   10985     {
   10986       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
   10987 			    " is too small"),
   10988 			  abfd, size_check[type - 1].type_name, note->descsz);
   10989       return true;
   10990     }
   10991 
   10992   switch (type)
   10993     {
   10994     case NOTE_INFO_PROCESS:
   10995       /* FIXME: need to add ->core->command.  */
   10996       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   10997       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   10998       break;
   10999 
   11000     case NOTE_INFO_THREAD:
   11001       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   11002 	 structure. */
   11003       /* thread_info.tid */
   11004       sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   11005 
   11006       len = strlen (buf) + 1;
   11007       name = (char *) bfd_alloc (abfd, len);
   11008       if (name == NULL)
   11009 	return false;
   11010 
   11011       memcpy (name, buf, len);
   11012 
   11013       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11014       if (sect == NULL)
   11015 	return false;
   11016 
   11017       /* sizeof (thread_info.thread_context) */
   11018       sect->size = note->descsz - 12;
   11019       /* offsetof (thread_info.thread_context) */
   11020       sect->filepos = note->descpos + 12;
   11021       sect->alignment_power = 2;
   11022 
   11023       /* thread_info.is_active_thread */
   11024       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   11025 
   11026       if (is_active_thread)
   11027 	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
   11028 	  return false;
   11029       break;
   11030 
   11031     case NOTE_INFO_MODULE:
   11032     case NOTE_INFO_MODULE64:
   11033       /* Make a ".module/xxxxxxxx" section.  */
   11034       if (type == NOTE_INFO_MODULE)
   11035 	{
   11036 	  /* module_info.base_address */
   11037 	  base_addr = bfd_get_32 (abfd, note->descdata + 4);
   11038 	  sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   11039 	  /* module_info.module_name_size */
   11040 	  name_size = bfd_get_32 (abfd, note->descdata + 8);
   11041 	}
   11042       else /* NOTE_INFO_MODULE64 */
   11043 	{
   11044 	  /* module_info.base_address */
   11045 	  base_addr = bfd_get_64 (abfd, note->descdata + 4);
   11046 	  sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   11047 	  /* module_info.module_name_size */
   11048 	  name_size = bfd_get_32 (abfd, note->descdata + 12);
   11049 	}
   11050 
   11051       len = strlen (buf) + 1;
   11052       name = (char *) bfd_alloc (abfd, len);
   11053       if (name == NULL)
   11054 	return false;
   11055 
   11056       memcpy (name, buf, len);
   11057 
   11058       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11059 
   11060       if (sect == NULL)
   11061 	return false;
   11062 
   11063       if (note->descsz < 12 + name_size)
   11064 	{
   11065 	  _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
   11066 				" is too small to contain a name of size %u"),
   11067 			      abfd, note->descsz, name_size);
   11068 	  return true;
   11069 	}
   11070 
   11071       sect->size = note->descsz;
   11072       sect->filepos = note->descpos;
   11073       sect->alignment_power = 2;
   11074       break;
   11075 
   11076     default:
   11077       return true;
   11078     }
   11079 
   11080   return true;
   11081 }
   11082 
   11083 static bool
   11084 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   11085 {
   11086   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11087 
   11088   switch (note->type)
   11089     {
   11090     default:
   11091       return true;
   11092 
   11093     case NT_PRSTATUS:
   11094       if (bed->elf_backend_grok_prstatus)
   11095 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   11096 	  return true;
   11097 #if defined (HAVE_PRSTATUS_T)
   11098       return elfcore_grok_prstatus (abfd, note);
   11099 #else
   11100       return true;
   11101 #endif
   11102 
   11103 #if defined (HAVE_PSTATUS_T)
   11104     case NT_PSTATUS:
   11105       return elfcore_grok_pstatus (abfd, note);
   11106 #endif
   11107 
   11108 #if defined (HAVE_LWPSTATUS_T)
   11109     case NT_LWPSTATUS:
   11110       return elfcore_grok_lwpstatus (abfd, note);
   11111 #endif
   11112 
   11113     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
   11114       return elfcore_grok_prfpreg (abfd, note);
   11115 
   11116     case NT_WIN32PSTATUS:
   11117       return elfcore_grok_win32pstatus (abfd, note);
   11118 
   11119     case NT_PRXFPREG:		/* Linux SSE extension */
   11120       if (note->namesz == 6
   11121 	  && strcmp (note->namedata, "LINUX") == 0)
   11122 	return elfcore_grok_prxfpreg (abfd, note);
   11123       else
   11124 	return true;
   11125 
   11126     case NT_X86_XSTATE:		/* Linux XSAVE extension */
   11127       if (note->namesz == 6
   11128 	  && strcmp (note->namedata, "LINUX") == 0)
   11129 	return elfcore_grok_xstatereg (abfd, note);
   11130       else
   11131 	return true;
   11132 
   11133     case NT_X86_SHSTK:		/* Linux CET extension.  */
   11134       if (note->namesz == 6
   11135 	  && strcmp (note->namedata, "LINUX") == 0)
   11136 	return elfcore_grok_sspreg (abfd, note);
   11137       else
   11138 	return true;
   11139 
   11140     case NT_PPC_VMX:
   11141       if (note->namesz == 6
   11142 	  && strcmp (note->namedata, "LINUX") == 0)
   11143 	return elfcore_grok_ppc_vmx (abfd, note);
   11144       else
   11145 	return true;
   11146 
   11147     case NT_PPC_VSX:
   11148       if (note->namesz == 6
   11149 	  && strcmp (note->namedata, "LINUX") == 0)
   11150 	return elfcore_grok_ppc_vsx (abfd, note);
   11151       else
   11152 	return true;
   11153 
   11154     case NT_PPC_TAR:
   11155       if (note->namesz == 6
   11156 	  && strcmp (note->namedata, "LINUX") == 0)
   11157 	return elfcore_grok_ppc_tar (abfd, note);
   11158       else
   11159 	return true;
   11160 
   11161     case NT_PPC_PPR:
   11162       if (note->namesz == 6
   11163 	  && strcmp (note->namedata, "LINUX") == 0)
   11164 	return elfcore_grok_ppc_ppr (abfd, note);
   11165       else
   11166 	return true;
   11167 
   11168     case NT_PPC_DSCR:
   11169       if (note->namesz == 6
   11170 	  && strcmp (note->namedata, "LINUX") == 0)
   11171 	return elfcore_grok_ppc_dscr (abfd, note);
   11172       else
   11173 	return true;
   11174 
   11175     case NT_PPC_EBB:
   11176       if (note->namesz == 6
   11177 	  && strcmp (note->namedata, "LINUX") == 0)
   11178 	return elfcore_grok_ppc_ebb (abfd, note);
   11179       else
   11180 	return true;
   11181 
   11182     case NT_PPC_PMU:
   11183       if (note->namesz == 6
   11184 	  && strcmp (note->namedata, "LINUX") == 0)
   11185 	return elfcore_grok_ppc_pmu (abfd, note);
   11186       else
   11187 	return true;
   11188 
   11189     case NT_PPC_TM_CGPR:
   11190       if (note->namesz == 6
   11191 	  && strcmp (note->namedata, "LINUX") == 0)
   11192 	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   11193       else
   11194 	return true;
   11195 
   11196     case NT_PPC_TM_CFPR:
   11197       if (note->namesz == 6
   11198 	  && strcmp (note->namedata, "LINUX") == 0)
   11199 	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   11200       else
   11201 	return true;
   11202 
   11203     case NT_PPC_TM_CVMX:
   11204       if (note->namesz == 6
   11205 	  && strcmp (note->namedata, "LINUX") == 0)
   11206 	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   11207       else
   11208 	return true;
   11209 
   11210     case NT_PPC_TM_CVSX:
   11211       if (note->namesz == 6
   11212 	  && strcmp (note->namedata, "LINUX") == 0)
   11213 	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   11214       else
   11215 	return true;
   11216 
   11217     case NT_PPC_TM_SPR:
   11218       if (note->namesz == 6
   11219 	  && strcmp (note->namedata, "LINUX") == 0)
   11220 	return elfcore_grok_ppc_tm_spr (abfd, note);
   11221       else
   11222 	return true;
   11223 
   11224     case NT_PPC_TM_CTAR:
   11225       if (note->namesz == 6
   11226 	  && strcmp (note->namedata, "LINUX") == 0)
   11227 	return elfcore_grok_ppc_tm_ctar (abfd, note);
   11228       else
   11229 	return true;
   11230 
   11231     case NT_PPC_TM_CPPR:
   11232       if (note->namesz == 6
   11233 	  && strcmp (note->namedata, "LINUX") == 0)
   11234 	return elfcore_grok_ppc_tm_cppr (abfd, note);
   11235       else
   11236 	return true;
   11237 
   11238     case NT_PPC_TM_CDSCR:
   11239       if (note->namesz == 6
   11240 	  && strcmp (note->namedata, "LINUX") == 0)
   11241 	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   11242       else
   11243 	return true;
   11244 
   11245     case NT_S390_HIGH_GPRS:
   11246       if (note->namesz == 6
   11247 	  && strcmp (note->namedata, "LINUX") == 0)
   11248 	return elfcore_grok_s390_high_gprs (abfd, note);
   11249       else
   11250 	return true;
   11251 
   11252     case NT_S390_TIMER:
   11253       if (note->namesz == 6
   11254 	  && strcmp (note->namedata, "LINUX") == 0)
   11255 	return elfcore_grok_s390_timer (abfd, note);
   11256       else
   11257 	return true;
   11258 
   11259     case NT_S390_TODCMP:
   11260       if (note->namesz == 6
   11261 	  && strcmp (note->namedata, "LINUX") == 0)
   11262 	return elfcore_grok_s390_todcmp (abfd, note);
   11263       else
   11264 	return true;
   11265 
   11266     case NT_S390_TODPREG:
   11267       if (note->namesz == 6
   11268 	  && strcmp (note->namedata, "LINUX") == 0)
   11269 	return elfcore_grok_s390_todpreg (abfd, note);
   11270       else
   11271 	return true;
   11272 
   11273     case NT_S390_CTRS:
   11274       if (note->namesz == 6
   11275 	  && strcmp (note->namedata, "LINUX") == 0)
   11276 	return elfcore_grok_s390_ctrs (abfd, note);
   11277       else
   11278 	return true;
   11279 
   11280     case NT_S390_PREFIX:
   11281       if (note->namesz == 6
   11282 	  && strcmp (note->namedata, "LINUX") == 0)
   11283 	return elfcore_grok_s390_prefix (abfd, note);
   11284       else
   11285 	return true;
   11286 
   11287     case NT_S390_LAST_BREAK:
   11288       if (note->namesz == 6
   11289 	  && strcmp (note->namedata, "LINUX") == 0)
   11290 	return elfcore_grok_s390_last_break (abfd, note);
   11291       else
   11292 	return true;
   11293 
   11294     case NT_S390_SYSTEM_CALL:
   11295       if (note->namesz == 6
   11296 	  && strcmp (note->namedata, "LINUX") == 0)
   11297 	return elfcore_grok_s390_system_call (abfd, note);
   11298       else
   11299 	return true;
   11300 
   11301     case NT_S390_TDB:
   11302       if (note->namesz == 6
   11303 	  && strcmp (note->namedata, "LINUX") == 0)
   11304 	return elfcore_grok_s390_tdb (abfd, note);
   11305       else
   11306 	return true;
   11307 
   11308     case NT_S390_VXRS_LOW:
   11309       if (note->namesz == 6
   11310 	  && strcmp (note->namedata, "LINUX") == 0)
   11311 	return elfcore_grok_s390_vxrs_low (abfd, note);
   11312       else
   11313 	return true;
   11314 
   11315     case NT_S390_VXRS_HIGH:
   11316       if (note->namesz == 6
   11317 	  && strcmp (note->namedata, "LINUX") == 0)
   11318 	return elfcore_grok_s390_vxrs_high (abfd, note);
   11319       else
   11320 	return true;
   11321 
   11322     case NT_S390_GS_CB:
   11323       if (note->namesz == 6
   11324 	  && strcmp (note->namedata, "LINUX") == 0)
   11325 	return elfcore_grok_s390_gs_cb (abfd, note);
   11326       else
   11327 	return true;
   11328 
   11329     case NT_S390_GS_BC:
   11330       if (note->namesz == 6
   11331 	  && strcmp (note->namedata, "LINUX") == 0)
   11332 	return elfcore_grok_s390_gs_bc (abfd, note);
   11333       else
   11334 	return true;
   11335 
   11336     case NT_ARC_V2:
   11337       if (note->namesz == 6
   11338 	  && strcmp (note->namedata, "LINUX") == 0)
   11339 	return elfcore_grok_arc_v2 (abfd, note);
   11340       else
   11341 	return true;
   11342 
   11343     case NT_ARM_VFP:
   11344       if (note->namesz == 6
   11345 	  && strcmp (note->namedata, "LINUX") == 0)
   11346 	return elfcore_grok_arm_vfp (abfd, note);
   11347       else
   11348 	return true;
   11349 
   11350     case NT_ARM_TLS:
   11351       if (note->namesz == 6
   11352 	  && strcmp (note->namedata, "LINUX") == 0)
   11353 	return elfcore_grok_aarch_tls (abfd, note);
   11354       else
   11355 	return true;
   11356 
   11357     case NT_ARM_HW_BREAK:
   11358       if (note->namesz == 6
   11359 	  && strcmp (note->namedata, "LINUX") == 0)
   11360 	return elfcore_grok_aarch_hw_break (abfd, note);
   11361       else
   11362 	return true;
   11363 
   11364     case NT_ARM_HW_WATCH:
   11365       if (note->namesz == 6
   11366 	  && strcmp (note->namedata, "LINUX") == 0)
   11367 	return elfcore_grok_aarch_hw_watch (abfd, note);
   11368       else
   11369 	return true;
   11370 
   11371     case NT_ARM_SVE:
   11372       if (note->namesz == 6
   11373 	  && strcmp (note->namedata, "LINUX") == 0)
   11374 	return elfcore_grok_aarch_sve (abfd, note);
   11375       else
   11376 	return true;
   11377 
   11378     case NT_ARM_PAC_MASK:
   11379       if (note->namesz == 6
   11380 	  && strcmp (note->namedata, "LINUX") == 0)
   11381 	return elfcore_grok_aarch_pauth (abfd, note);
   11382       else
   11383 	return true;
   11384 
   11385     case NT_ARM_TAGGED_ADDR_CTRL:
   11386       if (note->namesz == 6
   11387 	  && strcmp (note->namedata, "LINUX") == 0)
   11388 	return elfcore_grok_aarch_mte (abfd, note);
   11389       else
   11390 	return true;
   11391 
   11392     case NT_ARM_SSVE:
   11393       if (note->namesz == 6
   11394 	  && strcmp (note->namedata, "LINUX") == 0)
   11395 	return elfcore_grok_aarch_ssve (abfd, note);
   11396       else
   11397 	return true;
   11398 
   11399     case NT_ARM_ZA:
   11400       if (note->namesz == 6
   11401 	  && strcmp (note->namedata, "LINUX") == 0)
   11402 	return elfcore_grok_aarch_za (abfd, note);
   11403       else
   11404 	return true;
   11405 
   11406     case NT_ARM_ZT:
   11407       if (note->namesz == 6
   11408 	  && strcmp (note->namedata, "LINUX") == 0)
   11409 	return elfcore_grok_aarch_zt (abfd, note);
   11410       else
   11411 	return true;
   11412 
   11413     case NT_ARM_GCS:
   11414       if (note->namesz == 6 && strcmp (note->namedata, "LINUX") == 0)
   11415 	return elfcore_grok_aarch_gcs (abfd, note);
   11416       else
   11417 	return true;
   11418 
   11419     case NT_GDB_TDESC:
   11420       if (note->namesz == 4
   11421 	  && strcmp (note->namedata, "GDB") == 0)
   11422 	return elfcore_grok_gdb_tdesc (abfd, note);
   11423       else
   11424 	return true;
   11425 
   11426     case NT_RISCV_CSR:
   11427       if (note->namesz == 4
   11428 	  && strcmp (note->namedata, "GDB") == 0)
   11429 	return elfcore_grok_riscv_csr (abfd, note);
   11430       else
   11431 	return true;
   11432 
   11433     case NT_LARCH_CPUCFG:
   11434       if (note->namesz == 6
   11435 	  && strcmp (note->namedata, "LINUX") == 0)
   11436 	return elfcore_grok_loongarch_cpucfg (abfd, note);
   11437       else
   11438 	return true;
   11439 
   11440     case NT_LARCH_LBT:
   11441       if (note->namesz == 6
   11442 	  && strcmp (note->namedata, "LINUX") == 0)
   11443 	return elfcore_grok_loongarch_lbt (abfd, note);
   11444       else
   11445 	return true;
   11446 
   11447     case NT_LARCH_LSX:
   11448       if (note->namesz == 6
   11449 	  && strcmp (note->namedata, "LINUX") == 0)
   11450 	return elfcore_grok_loongarch_lsx (abfd, note);
   11451       else
   11452 	return true;
   11453 
   11454     case NT_LARCH_LASX:
   11455       if (note->namesz == 6
   11456 	  && strcmp (note->namedata, "LINUX") == 0)
   11457 	return elfcore_grok_loongarch_lasx (abfd, note);
   11458       else
   11459 	return true;
   11460 
   11461     case NT_PRPSINFO:
   11462     case NT_PSINFO:
   11463       if (bed->elf_backend_grok_psinfo)
   11464 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   11465 	  return true;
   11466 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11467       return elfcore_grok_psinfo (abfd, note);
   11468 #else
   11469       return true;
   11470 #endif
   11471 
   11472     case NT_AUXV:
   11473       return elfcore_make_auxv_note_section (abfd, note, 0);
   11474 
   11475     case NT_FILE:
   11476       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   11477 					      note);
   11478 
   11479     case NT_SIGINFO:
   11480       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   11481 					      note);
   11482 
   11483     }
   11484 }
   11485 
   11486 static bool
   11487 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   11488 {
   11489   struct bfd_build_id* build_id;
   11490 
   11491   if (note->descsz == 0)
   11492     return false;
   11493 
   11494   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   11495   if (build_id == NULL)
   11496     return false;
   11497 
   11498   build_id->size = note->descsz;
   11499   memcpy (build_id->data, note->descdata, note->descsz);
   11500   abfd->build_id = build_id;
   11501 
   11502   return true;
   11503 }
   11504 
   11505 static bool
   11506 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   11507 {
   11508   switch (note->type)
   11509     {
   11510     default:
   11511       return true;
   11512 
   11513     case NT_GNU_PROPERTY_TYPE_0:
   11514       return _bfd_elf_parse_gnu_properties (abfd, note);
   11515 
   11516     case NT_GNU_BUILD_ID:
   11517       return elfobj_grok_gnu_build_id (abfd, note);
   11518     }
   11519 }
   11520 
   11521 static bool
   11522 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   11523 {
   11524   struct sdt_note *cur =
   11525     (struct sdt_note *) bfd_alloc (abfd,
   11526 				   sizeof (struct sdt_note) + note->descsz);
   11527 
   11528   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   11529   cur->size = (bfd_size_type) note->descsz;
   11530   memcpy (cur->data, note->descdata, note->descsz);
   11531 
   11532   elf_tdata (abfd)->sdt_note_head = cur;
   11533 
   11534   return true;
   11535 }
   11536 
   11537 static bool
   11538 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   11539 {
   11540   switch (note->type)
   11541     {
   11542     case NT_STAPSDT:
   11543       return elfobj_grok_stapsdt_note_1 (abfd, note);
   11544 
   11545     default:
   11546       return true;
   11547     }
   11548 }
   11549 
   11550 static bool
   11551 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   11552 {
   11553   size_t offset;
   11554 
   11555   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11556     {
   11557     case ELFCLASS32:
   11558       if (note->descsz < 108)
   11559 	return false;
   11560       break;
   11561 
   11562     case ELFCLASS64:
   11563       if (note->descsz < 120)
   11564 	return false;
   11565       break;
   11566 
   11567     default:
   11568       return false;
   11569     }
   11570 
   11571   /* Check for version 1 in pr_version.  */
   11572   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11573     return false;
   11574 
   11575   offset = 4;
   11576 
   11577   /* Skip over pr_psinfosz. */
   11578   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11579     offset += 4;
   11580   else
   11581     {
   11582       offset += 4;	/* Padding before pr_psinfosz. */
   11583       offset += 8;
   11584     }
   11585 
   11586   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   11587   elf_tdata (abfd)->core->program
   11588     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   11589   offset += 17;
   11590 
   11591   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   11592   elf_tdata (abfd)->core->command
   11593     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   11594   offset += 81;
   11595 
   11596   /* Padding before pr_pid.  */
   11597   offset += 2;
   11598 
   11599   /* The pr_pid field was added in version "1a".  */
   11600   if (note->descsz < offset + 4)
   11601     return true;
   11602 
   11603   elf_tdata (abfd)->core->pid
   11604     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11605 
   11606   return true;
   11607 }
   11608 
   11609 static bool
   11610 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   11611 {
   11612   size_t offset;
   11613   size_t size;
   11614   size_t min_size;
   11615 
   11616   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   11617      Also compute minimum size of this note.  */
   11618   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11619     {
   11620     case ELFCLASS32:
   11621       offset = 4 + 4;
   11622       min_size = offset + (4 * 2) + 4 + 4 + 4;
   11623       break;
   11624 
   11625     case ELFCLASS64:
   11626       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   11627       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   11628       break;
   11629 
   11630     default:
   11631       return false;
   11632     }
   11633 
   11634   if (note->descsz < min_size)
   11635     return false;
   11636 
   11637   /* Check for version 1 in pr_version.  */
   11638   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11639     return false;
   11640 
   11641   /* Extract size of pr_reg from pr_gregsetsz.  */
   11642   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   11643   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11644     {
   11645       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11646       offset += 4 * 2;
   11647     }
   11648   else
   11649     {
   11650       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   11651       offset += 8 * 2;
   11652     }
   11653 
   11654   /* Skip over pr_osreldate.  */
   11655   offset += 4;
   11656 
   11657   /* Read signal from pr_cursig.  */
   11658   if (elf_tdata (abfd)->core->signal == 0)
   11659     elf_tdata (abfd)->core->signal
   11660       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11661   offset += 4;
   11662 
   11663   /* Read TID from pr_pid.  */
   11664   elf_tdata (abfd)->core->lwpid
   11665       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11666   offset += 4;
   11667 
   11668   /* Padding before pr_reg.  */
   11669   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11670     offset += 4;
   11671 
   11672   /* Make sure that there is enough data remaining in the note.  */
   11673   if ((note->descsz - offset) < size)
   11674     return false;
   11675 
   11676   /* Make a ".reg/999" section and a ".reg" section.  */
   11677   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   11678 					  size, note->descpos + offset);
   11679 }
   11680 
   11681 static bool
   11682 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11683 {
   11684   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   11685 
   11686   switch (note->type)
   11687     {
   11688     case NT_PRSTATUS:
   11689       if (bed->elf_backend_grok_freebsd_prstatus)
   11690 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11691 	  return true;
   11692       return elfcore_grok_freebsd_prstatus (abfd, note);
   11693 
   11694     case NT_FPREGSET:
   11695       return elfcore_grok_prfpreg (abfd, note);
   11696 
   11697     case NT_PRPSINFO:
   11698       return elfcore_grok_freebsd_psinfo (abfd, note);
   11699 
   11700     case NT_FREEBSD_THRMISC:
   11701       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11702 
   11703     case NT_FREEBSD_PROCSTAT_PROC:
   11704       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11705 					      note);
   11706 
   11707     case NT_FREEBSD_PROCSTAT_FILES:
   11708       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11709 					      note);
   11710 
   11711     case NT_FREEBSD_PROCSTAT_VMMAP:
   11712       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11713 					      note);
   11714 
   11715     case NT_FREEBSD_PROCSTAT_AUXV:
   11716       return elfcore_make_auxv_note_section (abfd, note, 4);
   11717 
   11718     case NT_FREEBSD_X86_SEGBASES:
   11719       return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
   11720 
   11721     case NT_X86_XSTATE:
   11722       return elfcore_grok_xstatereg (abfd, note);
   11723 
   11724     case NT_FREEBSD_PTLWPINFO:
   11725       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11726 					      note);
   11727 
   11728     case NT_ARM_TLS:
   11729       return elfcore_grok_aarch_tls (abfd, note);
   11730 
   11731     case NT_ARM_VFP:
   11732       return elfcore_grok_arm_vfp (abfd, note);
   11733 
   11734     default:
   11735       return true;
   11736     }
   11737 }
   11738 
   11739 static bool
   11740 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11741 {
   11742   char *cp;
   11743 
   11744   cp = strchr (note->namedata, '@');
   11745   if (cp != NULL)
   11746     {
   11747       *lwpidp = atoi(cp + 1);
   11748       return true;
   11749     }
   11750   return false;
   11751 }
   11752 
   11753 static bool
   11754 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11755 {
   11756   if (note->descsz <= 0x7c + 31)
   11757     return false;
   11758 
   11759   /* Signal number at offset 0x08. */
   11760   elf_tdata (abfd)->core->signal
   11761     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11762 
   11763   /* Process ID at offset 0x50. */
   11764   elf_tdata (abfd)->core->pid
   11765     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11766 
   11767   /* Command name at 0x7c (max 32 bytes, including nul). */
   11768   elf_tdata (abfd)->core->command
   11769     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11770 
   11771   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11772 					  note);
   11773 }
   11774 
   11775 static bool
   11776 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11777 {
   11778   int lwp;
   11779 
   11780   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11781     elf_tdata (abfd)->core->lwpid = lwp;
   11782 
   11783   switch (note->type)
   11784     {
   11785     case NT_NETBSDCORE_PROCINFO:
   11786       /* NetBSD-specific core "procinfo".  Note that we expect to
   11787 	 find this note before any of the others, which is fine,
   11788 	 since the kernel writes this note out first when it
   11789 	 creates a core file.  */
   11790       return elfcore_grok_netbsd_procinfo (abfd, note);
   11791     case NT_NETBSDCORE_AUXV:
   11792       /* NetBSD-specific Elf Auxiliary Vector data. */
   11793       return elfcore_make_auxv_note_section (abfd, note, 0);
   11794     case NT_NETBSDCORE_LWPSTATUS:
   11795       return elfcore_make_note_pseudosection (abfd,
   11796 					      ".note.netbsdcore.lwpstatus",
   11797 					      note);
   11798     default:
   11799       break;
   11800     }
   11801 
   11802   /* As of March 2020 there are no other machine-independent notes
   11803      defined for NetBSD core files.  If the note type is less
   11804      than the start of the machine-dependent note types, we don't
   11805      understand it.  */
   11806 
   11807   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11808     return true;
   11809 
   11810 
   11811   switch (bfd_get_arch (abfd))
   11812     {
   11813       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11814 	 PT_GETFPREGS == mach+2.  */
   11815 
   11816     case bfd_arch_aarch64:
   11817     case bfd_arch_alpha:
   11818     case bfd_arch_sparc:
   11819       switch (note->type)
   11820 	{
   11821 	case NT_NETBSDCORE_FIRSTMACH+0:
   11822 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11823 
   11824 	case NT_NETBSDCORE_FIRSTMACH+2:
   11825 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11826 
   11827 	default:
   11828 	  return true;
   11829 	}
   11830 
   11831       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11832 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11833 	 structure which lacks GBR.  */
   11834 
   11835     case bfd_arch_sh:
   11836       switch (note->type)
   11837 	{
   11838 	case NT_NETBSDCORE_FIRSTMACH+3:
   11839 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11840 
   11841 	case NT_NETBSDCORE_FIRSTMACH+5:
   11842 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11843 
   11844 	default:
   11845 	  return true;
   11846 	}
   11847 
   11848       /* On all other arch's, PT_GETREGS == mach+1 and
   11849 	 PT_GETFPREGS == mach+3.  */
   11850 
   11851     default:
   11852       switch (note->type)
   11853 	{
   11854 	case NT_NETBSDCORE_FIRSTMACH+1:
   11855 	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
   11856 
   11857 	case NT_NETBSDCORE_FIRSTMACH+3:
   11858 	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   11859 
   11860 	default:
   11861 	  return true;
   11862 	}
   11863     }
   11864     /* NOTREACHED */
   11865 }
   11866 
   11867 static bool
   11868 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11869 {
   11870   if (note->descsz <= 0x48 + 31)
   11871     return false;
   11872 
   11873   /* Signal number at offset 0x08. */
   11874   elf_tdata (abfd)->core->signal
   11875     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11876 
   11877   /* Process ID at offset 0x20. */
   11878   elf_tdata (abfd)->core->pid
   11879     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11880 
   11881   /* Command name at 0x48 (max 32 bytes, including nul). */
   11882   elf_tdata (abfd)->core->command
   11883     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11884 
   11885   return true;
   11886 }
   11887 
   11888 /* Processes Solaris's process status note.
   11889    sig_off ~ offsetof(prstatus_t, pr_cursig)
   11890    pid_off ~ offsetof(prstatus_t, pr_pid)
   11891    lwpid_off ~ offsetof(prstatus_t, pr_who)
   11892    gregset_size ~ sizeof(gregset_t)
   11893    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   11894 
   11895 static bool
   11896 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   11897 			       int pid_off, int lwpid_off, size_t gregset_size,
   11898 			       size_t gregset_offset)
   11899 {
   11900   asection *sect = NULL;
   11901   elf_tdata (abfd)->core->signal
   11902     = bfd_get_16 (abfd, note->descdata + sig_off);
   11903   elf_tdata (abfd)->core->pid
   11904     = bfd_get_32 (abfd, note->descdata + pid_off);
   11905   elf_tdata (abfd)->core->lwpid
   11906     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   11907 
   11908   sect = bfd_get_section_by_name (abfd, ".reg");
   11909   if (sect != NULL)
   11910     sect->size = gregset_size;
   11911 
   11912   return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11913 					  note->descpos + gregset_offset);
   11914 }
   11915 
   11916 /* Gets program and arguments from a core.
   11917    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   11918    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   11919 
   11920 static bool
   11921 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   11922 			  int prog_off, int comm_off)
   11923 {
   11924   elf_tdata (abfd)->core->program
   11925     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   11926   elf_tdata (abfd)->core->command
   11927     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   11928 
   11929   return true;
   11930 }
   11931 
   11932 /* Processes Solaris's LWP status note.
   11933    gregset_size ~ sizeof(gregset_t)
   11934    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   11935    fpregset_size ~ sizeof(fpregset_t)
   11936    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   11937 
   11938 static bool
   11939 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   11940 				size_t gregset_size, int gregset_off,
   11941 				size_t fpregset_size, int fpregset_off)
   11942 {
   11943   asection *sect = NULL;
   11944   char reg2_section_name[16] = { 0 };
   11945 
   11946   (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
   11947 		   elf_tdata (abfd)->core->lwpid);
   11948 
   11949   /* offsetof(lwpstatus_t, pr_lwpid) */
   11950   elf_tdata (abfd)->core->lwpid
   11951     = bfd_get_32 (abfd, note->descdata + 4);
   11952   /* offsetof(lwpstatus_t, pr_cursig) */
   11953   elf_tdata (abfd)->core->signal
   11954     = bfd_get_16 (abfd, note->descdata + 12);
   11955 
   11956   sect = bfd_get_section_by_name (abfd, ".reg");
   11957   if (sect != NULL)
   11958     sect->size = gregset_size;
   11959   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
   11960 					     note->descpos + gregset_off))
   11961     return false;
   11962 
   11963   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   11964   if (sect != NULL)
   11965     {
   11966       sect->size = fpregset_size;
   11967       sect->filepos = note->descpos + fpregset_off;
   11968       sect->alignment_power = 2;
   11969     }
   11970   else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
   11971 					     note->descpos + fpregset_off))
   11972     return false;
   11973 
   11974   return true;
   11975 }
   11976 
   11977 static bool
   11978 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   11979 {
   11980   if (note == NULL)
   11981     return false;
   11982 
   11983   /* core files are identified as 32- or 64-bit, SPARC or x86,
   11984      by the size of the descsz which matches the sizeof()
   11985      the type appropriate for that note type (e.g., prstatus_t for
   11986      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   11987      on Solaris. The core file bitness may differ from the bitness of
   11988      gdb itself, so fixed values are used instead of sizeof().
   11989      Appropriate fixed offsets are also used to obtain data from
   11990      the note.  */
   11991 
   11992   switch ((int) note->type)
   11993     {
   11994     case SOLARIS_NT_PRSTATUS:
   11995       switch (note->descsz)
   11996 	{
   11997 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   11998 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11999 					       136, 216, 308, 152, 356);
   12000 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   12001 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12002 					       264, 360, 520, 304, 600);
   12003 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   12004 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12005 					       136, 216, 308, 76, 356);
   12006 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   12007 	  return elfcore_grok_solaris_prstatus(abfd, note,
   12008 					       264, 360, 520, 224, 600);
   12009 	default:
   12010 	  return true;
   12011 	}
   12012 
   12013     case SOLARIS_NT_PSINFO:
   12014     case SOLARIS_NT_PRPSINFO:
   12015       switch (note->descsz)
   12016 	{
   12017 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   12018 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   12019 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   12020 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   12021 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   12022 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   12023 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   12024 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   12025 	default:
   12026 	  return true;
   12027 	}
   12028 
   12029     case SOLARIS_NT_LWPSTATUS:
   12030       switch (note->descsz)
   12031 	{
   12032 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   12033 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12034 						152, 344, 400, 496);
   12035 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   12036 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12037 						304, 544, 544, 848);
   12038 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   12039 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12040 						76, 344, 380, 420);
   12041 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   12042 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   12043 						224, 544, 528, 768);
   12044 	default:
   12045 	  return true;
   12046 	}
   12047 
   12048     case SOLARIS_NT_LWPSINFO:
   12049       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   12050       if (note->descsz == 128 || note->descsz == 152)
   12051 	elf_tdata (abfd)->core->lwpid =
   12052 	  bfd_get_32 (abfd, note->descdata + 4);
   12053       break;
   12054 
   12055     default:
   12056       break;
   12057     }
   12058 
   12059   return true;
   12060 }
   12061 
   12062 /* For name starting with "CORE" this may be either a Solaris
   12063    core file or a gdb-generated core file.  Do Solaris-specific
   12064    processing on selected note types first with
   12065    elfcore_grok_solaris_note(), then process the note
   12066    in elfcore_grok_note().  */
   12067 
   12068 static bool
   12069 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   12070 {
   12071   if (!elfcore_grok_solaris_note_impl (abfd, note))
   12072     return false;
   12073 
   12074   return elfcore_grok_note (abfd, note);
   12075 }
   12076 
   12077 static bool
   12078 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   12079 {
   12080   if (note->type == NT_OPENBSD_PROCINFO)
   12081     return elfcore_grok_openbsd_procinfo (abfd, note);
   12082 
   12083   if (note->type == NT_OPENBSD_REGS)
   12084     return elfcore_make_note_pseudosection (abfd, ".reg", note);
   12085 
   12086   if (note->type == NT_OPENBSD_FPREGS)
   12087     return elfcore_make_note_pseudosection (abfd, ".reg2", note);
   12088 
   12089   if (note->type == NT_OPENBSD_XFPREGS)
   12090     return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
   12091 
   12092   if (note->type == NT_OPENBSD_AUXV)
   12093     return elfcore_make_auxv_note_section (abfd, note, 0);
   12094 
   12095   if (note->type == NT_OPENBSD_WCOOKIE)
   12096     {
   12097       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   12098 							   SEC_HAS_CONTENTS);
   12099 
   12100       if (sect == NULL)
   12101 	return false;
   12102       sect->size = note->descsz;
   12103       sect->filepos = note->descpos;
   12104       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   12105 
   12106       return true;
   12107     }
   12108 
   12109   return true;
   12110 }
   12111 
   12112 static bool
   12113 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   12114 {
   12115   void *ddata = note->descdata;
   12116   char buf[100];
   12117   char *name;
   12118   asection *sect;
   12119   short sig;
   12120   unsigned flags;
   12121 
   12122   if (note->descsz < 16)
   12123     return false;
   12124 
   12125   /* nto_procfs_status 'pid' field is at offset 0.  */
   12126   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   12127 
   12128   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   12129   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   12130 
   12131   /* nto_procfs_status 'flags' field is at offset 8.  */
   12132   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   12133 
   12134   /* nto_procfs_status 'what' field is at offset 14.  */
   12135   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   12136     {
   12137       elf_tdata (abfd)->core->signal = sig;
   12138       elf_tdata (abfd)->core->lwpid = *tid;
   12139     }
   12140 
   12141   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   12142      do not come from signals so we make sure we set the current
   12143      thread just in case.  */
   12144   if (flags & 0x00000080)
   12145     elf_tdata (abfd)->core->lwpid = *tid;
   12146 
   12147   /* Make a ".qnx_core_status/%d" section.  */
   12148   sprintf (buf, ".qnx_core_status/%ld", *tid);
   12149 
   12150   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   12151   if (name == NULL)
   12152     return false;
   12153   strcpy (name, buf);
   12154 
   12155   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12156   if (sect == NULL)
   12157     return false;
   12158 
   12159   sect->size		= note->descsz;
   12160   sect->filepos		= note->descpos;
   12161   sect->alignment_power = 2;
   12162 
   12163   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   12164 }
   12165 
   12166 static bool
   12167 elfcore_grok_nto_regs (bfd *abfd,
   12168 		       Elf_Internal_Note *note,
   12169 		       long tid,
   12170 		       char *base)
   12171 {
   12172   char buf[100];
   12173   char *name;
   12174   asection *sect;
   12175 
   12176   /* Make a "(base)/%d" section.  */
   12177   sprintf (buf, "%s/%ld", base, tid);
   12178 
   12179   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   12180   if (name == NULL)
   12181     return false;
   12182   strcpy (name, buf);
   12183 
   12184   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12185   if (sect == NULL)
   12186     return false;
   12187 
   12188   sect->size		= note->descsz;
   12189   sect->filepos		= note->descpos;
   12190   sect->alignment_power = 2;
   12191 
   12192   /* This is the current thread.  */
   12193   if (elf_tdata (abfd)->core->lwpid == tid)
   12194     return elfcore_maybe_make_sect (abfd, base, sect);
   12195 
   12196   return true;
   12197 }
   12198 
   12199 static bool
   12200 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   12201 {
   12202   /* Every GREG section has a STATUS section before it.  Store the
   12203      tid from the previous call to pass down to the next gregs
   12204      function.  */
   12205   static long tid = 1;
   12206 
   12207   switch (note->type)
   12208     {
   12209     case QNT_CORE_INFO:
   12210       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   12211     case QNT_CORE_STATUS:
   12212       return elfcore_grok_nto_status (abfd, note, &tid);
   12213     case QNT_CORE_GREG:
   12214       return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
   12215     case QNT_CORE_FPREG:
   12216       return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
   12217     default:
   12218       return true;
   12219     }
   12220 }
   12221 
   12222 static bool
   12223 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   12224 {
   12225   char *name;
   12226   asection *sect;
   12227   size_t len;
   12228 
   12229   /* Use note name as section name.  */
   12230   len = note->namesz;
   12231   name = (char *) bfd_alloc (abfd, len);
   12232   if (name == NULL)
   12233     return false;
   12234   memcpy (name, note->namedata, len);
   12235   name[len - 1] = '\0';
   12236 
   12237   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12238   if (sect == NULL)
   12239     return false;
   12240 
   12241   sect->size		= note->descsz;
   12242   sect->filepos		= note->descpos;
   12243   sect->alignment_power = 1;
   12244 
   12245   return true;
   12246 }
   12247 
   12248 /* Function: elfcore_write_note
   12249 
   12250    Inputs:
   12251      buffer to hold note, and current size of buffer
   12252      name of note
   12253      type of note
   12254      data for note
   12255      size of data for note
   12256 
   12257    Writes note to end of buffer.  ELF64 notes are written exactly as
   12258    for ELF32, despite the current (as of 2006) ELF gabi specifying
   12259    that they ought to have 8-byte namesz and descsz field, and have
   12260    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   12261 
   12262    Return:
   12263    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   12264 
   12265 char *
   12266 elfcore_write_note (bfd *abfd,
   12267 		    char *buf,
   12268 		    int *bufsiz,
   12269 		    const char *name,
   12270 		    int type,
   12271 		    const void *input,
   12272 		    int size)
   12273 {
   12274   Elf_External_Note *xnp;
   12275   size_t namesz;
   12276   size_t newspace;
   12277   char *dest;
   12278 
   12279   namesz = 0;
   12280   if (name != NULL)
   12281     namesz = strlen (name) + 1;
   12282 
   12283   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   12284 
   12285   buf = (char *) realloc (buf, *bufsiz + newspace);
   12286   if (buf == NULL)
   12287     return buf;
   12288   dest = buf + *bufsiz;
   12289   *bufsiz += newspace;
   12290   xnp = (Elf_External_Note *) dest;
   12291   H_PUT_32 (abfd, namesz, xnp->namesz);
   12292   H_PUT_32 (abfd, size, xnp->descsz);
   12293   H_PUT_32 (abfd, type, xnp->type);
   12294   dest = xnp->name;
   12295   if (name != NULL)
   12296     {
   12297       memcpy (dest, name, namesz);
   12298       dest += namesz;
   12299       while (namesz & 3)
   12300 	{
   12301 	  *dest++ = '\0';
   12302 	  ++namesz;
   12303 	}
   12304     }
   12305   memcpy (dest, input, size);
   12306   dest += size;
   12307   while (size & 3)
   12308     {
   12309       *dest++ = '\0';
   12310       ++size;
   12311     }
   12312   return buf;
   12313 }
   12314 
   12315 /* gcc-8 warns (*) on all the strncpy calls in this function about
   12316    possible string truncation.  The "truncation" is not a bug.  We
   12317    have an external representation of structs with fields that are not
   12318    necessarily NULL terminated and corresponding internal
   12319    representation fields that are one larger so that they can always
   12320    be NULL terminated.
   12321    gcc versions between 4.2 and 4.6 do not allow pragma control of
   12322    diagnostics inside functions, giving a hard error if you try to use
   12323    the finer control available with later versions.
   12324    gcc prior to 4.2 warns about diagnostic push and pop.
   12325    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   12326    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   12327    (*) Depending on your system header files!  */
   12328 #if GCC_VERSION >= 8000
   12329 # pragma GCC diagnostic push
   12330 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   12331 #endif
   12332 char *
   12333 elfcore_write_prpsinfo (bfd  *abfd,
   12334 			char *buf,
   12335 			int  *bufsiz,
   12336 			const char *fname,
   12337 			const char *psargs)
   12338 {
   12339   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12340 
   12341   if (bed->elf_backend_write_core_note != NULL)
   12342     {
   12343       char *ret;
   12344       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12345 						 NT_PRPSINFO, fname, psargs);
   12346       if (ret != NULL)
   12347 	return ret;
   12348     }
   12349 
   12350 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   12351 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   12352   if (bed->s->elfclass == ELFCLASS32)
   12353     {
   12354 #  if defined (HAVE_PSINFO32_T)
   12355       psinfo32_t data;
   12356       int note_type = NT_PSINFO;
   12357 #  else
   12358       prpsinfo32_t data;
   12359       int note_type = NT_PRPSINFO;
   12360 #  endif
   12361 
   12362       memset (&data, 0, sizeof (data));
   12363       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12364       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12365       return elfcore_write_note (abfd, buf, bufsiz,
   12366 				 "CORE", note_type, &data, sizeof (data));
   12367     }
   12368   else
   12369 # endif
   12370     {
   12371 # if defined (HAVE_PSINFO_T)
   12372       psinfo_t data;
   12373       int note_type = NT_PSINFO;
   12374 # else
   12375       prpsinfo_t data;
   12376       int note_type = NT_PRPSINFO;
   12377 # endif
   12378 
   12379       memset (&data, 0, sizeof (data));
   12380       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12381       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12382       return elfcore_write_note (abfd, buf, bufsiz,
   12383 				 "CORE", note_type, &data, sizeof (data));
   12384     }
   12385 #endif	/* PSINFO_T or PRPSINFO_T */
   12386 
   12387   free (buf);
   12388   return NULL;
   12389 }
   12390 #if GCC_VERSION >= 8000
   12391 # pragma GCC diagnostic pop
   12392 #endif
   12393 
   12394 char *
   12395 elfcore_write_linux_prpsinfo32
   12396   (bfd *abfd, char *buf, int *bufsiz,
   12397    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12398 {
   12399   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   12400     {
   12401       struct elf_external_linux_prpsinfo32_ugid16 data;
   12402 
   12403       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   12404       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   12405 				 &data, sizeof (data));
   12406     }
   12407   else
   12408     {
   12409       struct elf_external_linux_prpsinfo32_ugid32 data;
   12410 
   12411       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   12412       return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
   12413 				 &data, sizeof (data));
   12414     }
   12415 }
   12416 
   12417 char *
   12418 elfcore_write_linux_prpsinfo64
   12419   (bfd *abfd, char *buf, int *bufsiz,
   12420    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12421 {
   12422   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   12423     {
   12424       struct elf_external_linux_prpsinfo64_ugid16 data;
   12425 
   12426       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   12427       return elfcore_write_note (abfd, buf, bufsiz,
   12428 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   12429     }
   12430   else
   12431     {
   12432       struct elf_external_linux_prpsinfo64_ugid32 data;
   12433 
   12434       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   12435       return elfcore_write_note (abfd, buf, bufsiz,
   12436 				 "CORE", NT_PRPSINFO, &data, sizeof (data));
   12437     }
   12438 }
   12439 
   12440 char *
   12441 elfcore_write_prstatus (bfd *abfd,
   12442 			char *buf,
   12443 			int *bufsiz,
   12444 			long pid,
   12445 			int cursig,
   12446 			const void *gregs)
   12447 {
   12448   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12449 
   12450   if (bed->elf_backend_write_core_note != NULL)
   12451     {
   12452       char *ret;
   12453       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12454 						 NT_PRSTATUS,
   12455 						 pid, cursig, gregs);
   12456       if (ret != NULL)
   12457 	return ret;
   12458     }
   12459 
   12460 #if defined (HAVE_PRSTATUS_T)
   12461 #if defined (HAVE_PRSTATUS32_T)
   12462   if (bed->s->elfclass == ELFCLASS32)
   12463     {
   12464       prstatus32_t prstat;
   12465 
   12466       memset (&prstat, 0, sizeof (prstat));
   12467       prstat.pr_pid = pid;
   12468       prstat.pr_cursig = cursig;
   12469       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12470       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   12471 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12472     }
   12473   else
   12474 #endif
   12475     {
   12476       prstatus_t prstat;
   12477 
   12478       memset (&prstat, 0, sizeof (prstat));
   12479       prstat.pr_pid = pid;
   12480       prstat.pr_cursig = cursig;
   12481       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12482       return elfcore_write_note (abfd, buf, bufsiz, "CORE",
   12483 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12484     }
   12485 #endif /* HAVE_PRSTATUS_T */
   12486 
   12487   free (buf);
   12488   return NULL;
   12489 }
   12490 
   12491 #if defined (HAVE_LWPSTATUS_T)
   12492 char *
   12493 elfcore_write_lwpstatus (bfd *abfd,
   12494 			 char *buf,
   12495 			 int *bufsiz,
   12496 			 long pid,
   12497 			 int cursig,
   12498 			 const void *gregs)
   12499 {
   12500   lwpstatus_t lwpstat;
   12501   const char *note_name = "CORE";
   12502 
   12503   memset (&lwpstat, 0, sizeof (lwpstat));
   12504   lwpstat.pr_lwpid  = pid >> 16;
   12505   lwpstat.pr_cursig = cursig;
   12506 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   12507   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   12508 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   12509 #if !defined(gregs)
   12510   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   12511 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   12512 #else
   12513   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   12514 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   12515 #endif
   12516 #endif
   12517   return elfcore_write_note (abfd, buf, bufsiz, note_name,
   12518 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   12519 }
   12520 #endif /* HAVE_LWPSTATUS_T */
   12521 
   12522 #if defined (HAVE_PSTATUS_T)
   12523 char *
   12524 elfcore_write_pstatus (bfd *abfd,
   12525 		       char *buf,
   12526 		       int *bufsiz,
   12527 		       long pid,
   12528 		       int cursig ATTRIBUTE_UNUSED,
   12529 		       const void *gregs ATTRIBUTE_UNUSED)
   12530 {
   12531   const char *note_name = "CORE";
   12532 #if defined (HAVE_PSTATUS32_T)
   12533   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   12534 
   12535   if (bed->s->elfclass == ELFCLASS32)
   12536     {
   12537       pstatus32_t pstat;
   12538 
   12539       memset (&pstat, 0, sizeof (pstat));
   12540       pstat.pr_pid = pid & 0xffff;
   12541       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   12542 				NT_PSTATUS, &pstat, sizeof (pstat));
   12543       return buf;
   12544     }
   12545   else
   12546 #endif
   12547     {
   12548       pstatus_t pstat;
   12549 
   12550       memset (&pstat, 0, sizeof (pstat));
   12551       pstat.pr_pid = pid & 0xffff;
   12552       buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
   12553 				NT_PSTATUS, &pstat, sizeof (pstat));
   12554       return buf;
   12555     }
   12556 }
   12557 #endif /* HAVE_PSTATUS_T */
   12558 
   12559 char *
   12560 elfcore_write_prfpreg (bfd *abfd,
   12561 		       char *buf,
   12562 		       int *bufsiz,
   12563 		       const void *fpregs,
   12564 		       int size)
   12565 {
   12566   const char *note_name = "CORE";
   12567   return elfcore_write_note (abfd, buf, bufsiz,
   12568 			     note_name, NT_FPREGSET, fpregs, size);
   12569 }
   12570 
   12571 char *
   12572 elfcore_write_prxfpreg (bfd *abfd,
   12573 			char *buf,
   12574 			int *bufsiz,
   12575 			const void *xfpregs,
   12576 			int size)
   12577 {
   12578   char *note_name = "LINUX";
   12579   return elfcore_write_note (abfd, buf, bufsiz,
   12580 			     note_name, NT_PRXFPREG, xfpregs, size);
   12581 }
   12582 
   12583 char *
   12584 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   12585 			 const void *xfpregs, int size)
   12586 {
   12587   char *note_name;
   12588   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   12589     note_name = "FreeBSD";
   12590   else
   12591     note_name = "LINUX";
   12592   return elfcore_write_note (abfd, buf, bufsiz,
   12593 			     note_name, NT_X86_XSTATE, xfpregs, size);
   12594 }
   12595 
   12596 static char *
   12597 elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
   12598 		      const void *ssp, int size)
   12599 {
   12600   const char *note_name = "LINUX";
   12601   return elfcore_write_note (abfd, buf, bufsiz,
   12602 			     note_name, NT_X86_SHSTK, ssp, size);
   12603 }
   12604 
   12605 char *
   12606 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   12607 			    const void *regs, int size)
   12608 {
   12609   char *note_name = "FreeBSD";
   12610   return elfcore_write_note (abfd, buf, bufsiz,
   12611 			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
   12612 }
   12613 
   12614 char *
   12615 elfcore_write_ppc_vmx (bfd *abfd,
   12616 		       char *buf,
   12617 		       int *bufsiz,
   12618 		       const void *ppc_vmx,
   12619 		       int size)
   12620 {
   12621   char *note_name = "LINUX";
   12622   return elfcore_write_note (abfd, buf, bufsiz,
   12623 			     note_name, NT_PPC_VMX, ppc_vmx, size);
   12624 }
   12625 
   12626 char *
   12627 elfcore_write_ppc_vsx (bfd *abfd,
   12628 		       char *buf,
   12629 		       int *bufsiz,
   12630 		       const void *ppc_vsx,
   12631 		       int size)
   12632 {
   12633   char *note_name = "LINUX";
   12634   return elfcore_write_note (abfd, buf, bufsiz,
   12635 			     note_name, NT_PPC_VSX, ppc_vsx, size);
   12636 }
   12637 
   12638 char *
   12639 elfcore_write_ppc_tar (bfd *abfd,
   12640 		       char *buf,
   12641 		       int *bufsiz,
   12642 		       const void *ppc_tar,
   12643 		       int size)
   12644 {
   12645   char *note_name = "LINUX";
   12646   return elfcore_write_note (abfd, buf, bufsiz,
   12647 			     note_name, NT_PPC_TAR, ppc_tar, size);
   12648 }
   12649 
   12650 char *
   12651 elfcore_write_ppc_ppr (bfd *abfd,
   12652 		       char *buf,
   12653 		       int *bufsiz,
   12654 		       const void *ppc_ppr,
   12655 		       int size)
   12656 {
   12657   char *note_name = "LINUX";
   12658   return elfcore_write_note (abfd, buf, bufsiz,
   12659 			     note_name, NT_PPC_PPR, ppc_ppr, size);
   12660 }
   12661 
   12662 char *
   12663 elfcore_write_ppc_dscr (bfd *abfd,
   12664 			char *buf,
   12665 			int *bufsiz,
   12666 			const void *ppc_dscr,
   12667 			int size)
   12668 {
   12669   char *note_name = "LINUX";
   12670   return elfcore_write_note (abfd, buf, bufsiz,
   12671 			     note_name, NT_PPC_DSCR, ppc_dscr, size);
   12672 }
   12673 
   12674 char *
   12675 elfcore_write_ppc_ebb (bfd *abfd,
   12676 		       char *buf,
   12677 		       int *bufsiz,
   12678 		       const void *ppc_ebb,
   12679 		       int size)
   12680 {
   12681   char *note_name = "LINUX";
   12682   return elfcore_write_note (abfd, buf, bufsiz,
   12683 			     note_name, NT_PPC_EBB, ppc_ebb, size);
   12684 }
   12685 
   12686 char *
   12687 elfcore_write_ppc_pmu (bfd *abfd,
   12688 		       char *buf,
   12689 		       int *bufsiz,
   12690 		       const void *ppc_pmu,
   12691 		       int size)
   12692 {
   12693   char *note_name = "LINUX";
   12694   return elfcore_write_note (abfd, buf, bufsiz,
   12695 			     note_name, NT_PPC_PMU, ppc_pmu, size);
   12696 }
   12697 
   12698 char *
   12699 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12700 			   char *buf,
   12701 			   int *bufsiz,
   12702 			   const void *ppc_tm_cgpr,
   12703 			   int size)
   12704 {
   12705   char *note_name = "LINUX";
   12706   return elfcore_write_note (abfd, buf, bufsiz,
   12707 			     note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
   12708 }
   12709 
   12710 char *
   12711 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12712 			   char *buf,
   12713 			   int *bufsiz,
   12714 			   const void *ppc_tm_cfpr,
   12715 			   int size)
   12716 {
   12717   char *note_name = "LINUX";
   12718   return elfcore_write_note (abfd, buf, bufsiz,
   12719 			     note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
   12720 }
   12721 
   12722 char *
   12723 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12724 			   char *buf,
   12725 			   int *bufsiz,
   12726 			   const void *ppc_tm_cvmx,
   12727 			   int size)
   12728 {
   12729   char *note_name = "LINUX";
   12730   return elfcore_write_note (abfd, buf, bufsiz,
   12731 			     note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
   12732 }
   12733 
   12734 char *
   12735 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12736 			   char *buf,
   12737 			   int *bufsiz,
   12738 			   const void *ppc_tm_cvsx,
   12739 			   int size)
   12740 {
   12741   char *note_name = "LINUX";
   12742   return elfcore_write_note (abfd, buf, bufsiz,
   12743 			     note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
   12744 }
   12745 
   12746 char *
   12747 elfcore_write_ppc_tm_spr (bfd *abfd,
   12748 			  char *buf,
   12749 			  int *bufsiz,
   12750 			  const void *ppc_tm_spr,
   12751 			  int size)
   12752 {
   12753   char *note_name = "LINUX";
   12754   return elfcore_write_note (abfd, buf, bufsiz,
   12755 			     note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
   12756 }
   12757 
   12758 char *
   12759 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12760 			   char *buf,
   12761 			   int *bufsiz,
   12762 			   const void *ppc_tm_ctar,
   12763 			   int size)
   12764 {
   12765   char *note_name = "LINUX";
   12766   return elfcore_write_note (abfd, buf, bufsiz,
   12767 			     note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
   12768 }
   12769 
   12770 char *
   12771 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12772 			   char *buf,
   12773 			   int *bufsiz,
   12774 			   const void *ppc_tm_cppr,
   12775 			   int size)
   12776 {
   12777   char *note_name = "LINUX";
   12778   return elfcore_write_note (abfd, buf, bufsiz,
   12779 			     note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
   12780 }
   12781 
   12782 char *
   12783 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12784 			    char *buf,
   12785 			    int *bufsiz,
   12786 			    const void *ppc_tm_cdscr,
   12787 			    int size)
   12788 {
   12789   char *note_name = "LINUX";
   12790   return elfcore_write_note (abfd, buf, bufsiz,
   12791 			     note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
   12792 }
   12793 
   12794 static char *
   12795 elfcore_write_s390_high_gprs (bfd *abfd,
   12796 			      char *buf,
   12797 			      int *bufsiz,
   12798 			      const void *s390_high_gprs,
   12799 			      int size)
   12800 {
   12801   char *note_name = "LINUX";
   12802   return elfcore_write_note (abfd, buf, bufsiz,
   12803 			     note_name, NT_S390_HIGH_GPRS,
   12804 			     s390_high_gprs, size);
   12805 }
   12806 
   12807 char *
   12808 elfcore_write_s390_timer (bfd *abfd,
   12809 			  char *buf,
   12810 			  int *bufsiz,
   12811 			  const void *s390_timer,
   12812 			  int size)
   12813 {
   12814   char *note_name = "LINUX";
   12815   return elfcore_write_note (abfd, buf, bufsiz,
   12816 			     note_name, NT_S390_TIMER, s390_timer, size);
   12817 }
   12818 
   12819 char *
   12820 elfcore_write_s390_todcmp (bfd *abfd,
   12821 			   char *buf,
   12822 			   int *bufsiz,
   12823 			   const void *s390_todcmp,
   12824 			   int size)
   12825 {
   12826   char *note_name = "LINUX";
   12827   return elfcore_write_note (abfd, buf, bufsiz,
   12828 			     note_name, NT_S390_TODCMP, s390_todcmp, size);
   12829 }
   12830 
   12831 char *
   12832 elfcore_write_s390_todpreg (bfd *abfd,
   12833 			    char *buf,
   12834 			    int *bufsiz,
   12835 			    const void *s390_todpreg,
   12836 			    int size)
   12837 {
   12838   char *note_name = "LINUX";
   12839   return elfcore_write_note (abfd, buf, bufsiz,
   12840 			     note_name, NT_S390_TODPREG, s390_todpreg, size);
   12841 }
   12842 
   12843 char *
   12844 elfcore_write_s390_ctrs (bfd *abfd,
   12845 			 char *buf,
   12846 			 int *bufsiz,
   12847 			 const void *s390_ctrs,
   12848 			 int size)
   12849 {
   12850   char *note_name = "LINUX";
   12851   return elfcore_write_note (abfd, buf, bufsiz,
   12852 			     note_name, NT_S390_CTRS, s390_ctrs, size);
   12853 }
   12854 
   12855 char *
   12856 elfcore_write_s390_prefix (bfd *abfd,
   12857 			   char *buf,
   12858 			   int *bufsiz,
   12859 			   const void *s390_prefix,
   12860 			   int size)
   12861 {
   12862   char *note_name = "LINUX";
   12863   return elfcore_write_note (abfd, buf, bufsiz,
   12864 			     note_name, NT_S390_PREFIX, s390_prefix, size);
   12865 }
   12866 
   12867 char *
   12868 elfcore_write_s390_last_break (bfd *abfd,
   12869 			       char *buf,
   12870 			       int *bufsiz,
   12871 			       const void *s390_last_break,
   12872 			       int size)
   12873 {
   12874   char *note_name = "LINUX";
   12875   return elfcore_write_note (abfd, buf, bufsiz,
   12876 			     note_name, NT_S390_LAST_BREAK,
   12877 			     s390_last_break, size);
   12878 }
   12879 
   12880 char *
   12881 elfcore_write_s390_system_call (bfd *abfd,
   12882 				char *buf,
   12883 				int *bufsiz,
   12884 				const void *s390_system_call,
   12885 				int size)
   12886 {
   12887   char *note_name = "LINUX";
   12888   return elfcore_write_note (abfd, buf, bufsiz,
   12889 			     note_name, NT_S390_SYSTEM_CALL,
   12890 			     s390_system_call, size);
   12891 }
   12892 
   12893 char *
   12894 elfcore_write_s390_tdb (bfd *abfd,
   12895 			char *buf,
   12896 			int *bufsiz,
   12897 			const void *s390_tdb,
   12898 			int size)
   12899 {
   12900   char *note_name = "LINUX";
   12901   return elfcore_write_note (abfd, buf, bufsiz,
   12902 			     note_name, NT_S390_TDB, s390_tdb, size);
   12903 }
   12904 
   12905 char *
   12906 elfcore_write_s390_vxrs_low (bfd *abfd,
   12907 			     char *buf,
   12908 			     int *bufsiz,
   12909 			     const void *s390_vxrs_low,
   12910 			     int size)
   12911 {
   12912   char *note_name = "LINUX";
   12913   return elfcore_write_note (abfd, buf, bufsiz,
   12914 			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
   12915 }
   12916 
   12917 char *
   12918 elfcore_write_s390_vxrs_high (bfd *abfd,
   12919 			     char *buf,
   12920 			     int *bufsiz,
   12921 			     const void *s390_vxrs_high,
   12922 			     int size)
   12923 {
   12924   char *note_name = "LINUX";
   12925   return elfcore_write_note (abfd, buf, bufsiz,
   12926 			     note_name, NT_S390_VXRS_HIGH,
   12927 			     s390_vxrs_high, size);
   12928 }
   12929 
   12930 char *
   12931 elfcore_write_s390_gs_cb (bfd *abfd,
   12932 			  char *buf,
   12933 			  int *bufsiz,
   12934 			  const void *s390_gs_cb,
   12935 			  int size)
   12936 {
   12937   char *note_name = "LINUX";
   12938   return elfcore_write_note (abfd, buf, bufsiz,
   12939 			     note_name, NT_S390_GS_CB,
   12940 			     s390_gs_cb, size);
   12941 }
   12942 
   12943 char *
   12944 elfcore_write_s390_gs_bc (bfd *abfd,
   12945 			  char *buf,
   12946 			  int *bufsiz,
   12947 			  const void *s390_gs_bc,
   12948 			  int size)
   12949 {
   12950   char *note_name = "LINUX";
   12951   return elfcore_write_note (abfd, buf, bufsiz,
   12952 			     note_name, NT_S390_GS_BC,
   12953 			     s390_gs_bc, size);
   12954 }
   12955 
   12956 char *
   12957 elfcore_write_arm_vfp (bfd *abfd,
   12958 		       char *buf,
   12959 		       int *bufsiz,
   12960 		       const void *arm_vfp,
   12961 		       int size)
   12962 {
   12963   char *note_name = "LINUX";
   12964   return elfcore_write_note (abfd, buf, bufsiz,
   12965 			     note_name, NT_ARM_VFP, arm_vfp, size);
   12966 }
   12967 
   12968 char *
   12969 elfcore_write_aarch_tls (bfd *abfd,
   12970 		       char *buf,
   12971 		       int *bufsiz,
   12972 		       const void *aarch_tls,
   12973 		       int size)
   12974 {
   12975   char *note_name = "LINUX";
   12976   return elfcore_write_note (abfd, buf, bufsiz,
   12977 			     note_name, NT_ARM_TLS, aarch_tls, size);
   12978 }
   12979 
   12980 char *
   12981 elfcore_write_aarch_hw_break (bfd *abfd,
   12982 			    char *buf,
   12983 			    int *bufsiz,
   12984 			    const void *aarch_hw_break,
   12985 			    int size)
   12986 {
   12987   char *note_name = "LINUX";
   12988   return elfcore_write_note (abfd, buf, bufsiz,
   12989 			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
   12990 }
   12991 
   12992 char *
   12993 elfcore_write_aarch_hw_watch (bfd *abfd,
   12994 			    char *buf,
   12995 			    int *bufsiz,
   12996 			    const void *aarch_hw_watch,
   12997 			    int size)
   12998 {
   12999   char *note_name = "LINUX";
   13000   return elfcore_write_note (abfd, buf, bufsiz,
   13001 			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
   13002 }
   13003 
   13004 char *
   13005 elfcore_write_aarch_sve (bfd *abfd,
   13006 			 char *buf,
   13007 			 int *bufsiz,
   13008 			 const void *aarch_sve,
   13009 			 int size)
   13010 {
   13011   char *note_name = "LINUX";
   13012   return elfcore_write_note (abfd, buf, bufsiz,
   13013 			     note_name, NT_ARM_SVE, aarch_sve, size);
   13014 }
   13015 
   13016 char *
   13017 elfcore_write_aarch_pauth (bfd *abfd,
   13018 			   char *buf,
   13019 			   int *bufsiz,
   13020 			   const void *aarch_pauth,
   13021 			   int size)
   13022 {
   13023   char *note_name = "LINUX";
   13024   return elfcore_write_note (abfd, buf, bufsiz,
   13025 			     note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
   13026 }
   13027 
   13028 char *
   13029 elfcore_write_aarch_mte (bfd *abfd,
   13030 				      char *buf,
   13031 				      int *bufsiz,
   13032 				      const void *aarch_mte,
   13033 				      int size)
   13034 {
   13035   char *note_name = "LINUX";
   13036   return elfcore_write_note (abfd, buf, bufsiz,
   13037 			     note_name, NT_ARM_TAGGED_ADDR_CTRL,
   13038 			     aarch_mte,
   13039 			     size);
   13040 }
   13041 
   13042 char *
   13043 elfcore_write_aarch_ssve (bfd *abfd,
   13044 			  char *buf,
   13045 			  int *bufsiz,
   13046 			  const void *aarch_ssve,
   13047 			  int size)
   13048 {
   13049   char *note_name = "LINUX";
   13050   return elfcore_write_note (abfd, buf, bufsiz,
   13051 			     note_name, NT_ARM_SSVE,
   13052 			     aarch_ssve,
   13053 			     size);
   13054 }
   13055 
   13056 char *
   13057 elfcore_write_aarch_za (bfd *abfd,
   13058 			char *buf,
   13059 			int *bufsiz,
   13060 			const void *aarch_za,
   13061 			int size)
   13062 {
   13063   char *note_name = "LINUX";
   13064   return elfcore_write_note (abfd, buf, bufsiz,
   13065 			     note_name, NT_ARM_ZA,
   13066 			     aarch_za,
   13067 			     size);
   13068 }
   13069 
   13070 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
   13071    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   13072    written into.  Return a pointer to the new start of the note buffer, to
   13073    replace BUF which may no longer be valid.  */
   13074 
   13075 char *
   13076 elfcore_write_aarch_zt (bfd *abfd,
   13077 			char *buf,
   13078 			int *bufsiz,
   13079 			const void *aarch_zt,
   13080 			int size)
   13081 {
   13082   char *note_name = "LINUX";
   13083   return elfcore_write_note (abfd, buf, bufsiz,
   13084 			     note_name, NT_ARM_ZT,
   13085 			     aarch_zt,
   13086 			     size);
   13087 }
   13088 
   13089 /* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
   13090    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   13091    written into.  Return a pointer to the new start of the note buffer, to
   13092    replace BUF which may no longer be valid.  */
   13093 
   13094 static char *
   13095 elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
   13096 			 const void *aarch_gcs, int size)
   13097 {
   13098   const char *note_name = "LINUX";
   13099   return elfcore_write_note (abfd, buf, bufsiz, note_name, NT_ARM_GCS,
   13100 			     aarch_gcs, size);
   13101 }
   13102 
   13103 char *
   13104 elfcore_write_arc_v2 (bfd *abfd,
   13105 		      char *buf,
   13106 		      int *bufsiz,
   13107 		      const void *arc_v2,
   13108 		      int size)
   13109 {
   13110   char *note_name = "LINUX";
   13111   return elfcore_write_note (abfd, buf, bufsiz,
   13112 			     note_name, NT_ARC_V2, arc_v2, size);
   13113 }
   13114 
   13115 char *
   13116 elfcore_write_loongarch_cpucfg (bfd *abfd,
   13117 				char *buf,
   13118 				int *bufsiz,
   13119 				const void *loongarch_cpucfg,
   13120 				int size)
   13121 {
   13122   char *note_name = "LINUX";
   13123   return elfcore_write_note (abfd, buf, bufsiz,
   13124 			     note_name, NT_LARCH_CPUCFG,
   13125 			     loongarch_cpucfg, size);
   13126 }
   13127 
   13128 char *
   13129 elfcore_write_loongarch_lbt (bfd *abfd,
   13130 			     char *buf,
   13131 			     int *bufsiz,
   13132 			     const void *loongarch_lbt,
   13133 			     int size)
   13134 {
   13135   char *note_name = "LINUX";
   13136   return elfcore_write_note (abfd, buf, bufsiz,
   13137 			     note_name, NT_LARCH_LBT, loongarch_lbt, size);
   13138 }
   13139 
   13140 char *
   13141 elfcore_write_loongarch_lsx (bfd *abfd,
   13142 			     char *buf,
   13143 			     int *bufsiz,
   13144 			     const void *loongarch_lsx,
   13145 			     int size)
   13146 {
   13147   char *note_name = "LINUX";
   13148   return elfcore_write_note (abfd, buf, bufsiz,
   13149 			     note_name, NT_LARCH_LSX, loongarch_lsx, size);
   13150 }
   13151 
   13152 char *
   13153 elfcore_write_loongarch_lasx (bfd *abfd,
   13154 			      char *buf,
   13155 			      int *bufsiz,
   13156 			      const void *loongarch_lasx,
   13157 			      int size)
   13158 {
   13159   char *note_name = "LINUX";
   13160   return elfcore_write_note (abfd, buf, bufsiz,
   13161 			     note_name, NT_LARCH_LASX, loongarch_lasx, size);
   13162 }
   13163 
   13164 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   13165    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   13166    written into.  Return a pointer to the new start of the note buffer, to
   13167    replace BUF which may no longer be valid.  */
   13168 
   13169 char *
   13170 elfcore_write_riscv_csr (bfd *abfd,
   13171 			 char *buf,
   13172 			 int *bufsiz,
   13173 			 const void *csrs,
   13174 			 int size)
   13175 {
   13176   const char *note_name = "GDB";
   13177   return elfcore_write_note (abfd, buf, bufsiz,
   13178 			     note_name, NT_RISCV_CSR, csrs, size);
   13179 }
   13180 
   13181 /* Write the target description (a string) pointed to by TDESC, length
   13182    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   13183    note is being written into.  Return a pointer to the new start of the
   13184    note buffer, to replace BUF which may no longer be valid.  */
   13185 
   13186 char *
   13187 elfcore_write_gdb_tdesc (bfd *abfd,
   13188 			 char *buf,
   13189 			 int *bufsiz,
   13190 			 const void *tdesc,
   13191 			 int size)
   13192 {
   13193   const char *note_name = "GDB";
   13194   return elfcore_write_note (abfd, buf, bufsiz,
   13195 			     note_name, NT_GDB_TDESC, tdesc, size);
   13196 }
   13197 
   13198 char *
   13199 elfcore_write_register_note (bfd *abfd,
   13200 			     char *buf,
   13201 			     int *bufsiz,
   13202 			     const char *section,
   13203 			     const void *data,
   13204 			     int size)
   13205 {
   13206   if (strcmp (section, ".reg2") == 0)
   13207     return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
   13208   if (strcmp (section, ".reg-xfp") == 0)
   13209     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   13210   if (strcmp (section, ".reg-xstate") == 0)
   13211     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
   13212   if (strcmp (section, ".reg-x86-segbases") == 0)
   13213     return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
   13214   if (strcmp (section, ".reg-ssp") == 0)
   13215     return elfcore_write_sspreg (abfd, buf, bufsiz, data, size);
   13216   if (strcmp (section, ".reg-ppc-vmx") == 0)
   13217     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   13218   if (strcmp (section, ".reg-ppc-vsx") == 0)
   13219     return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
   13220   if (strcmp (section, ".reg-ppc-tar") == 0)
   13221     return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
   13222   if (strcmp (section, ".reg-ppc-ppr") == 0)
   13223     return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
   13224   if (strcmp (section, ".reg-ppc-dscr") == 0)
   13225     return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
   13226   if (strcmp (section, ".reg-ppc-ebb") == 0)
   13227     return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
   13228   if (strcmp (section, ".reg-ppc-pmu") == 0)
   13229     return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
   13230   if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
   13231     return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
   13232   if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
   13233     return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
   13234   if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
   13235     return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
   13236   if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
   13237     return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
   13238   if (strcmp (section, ".reg-ppc-tm-spr") == 0)
   13239     return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
   13240   if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
   13241     return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
   13242   if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
   13243     return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
   13244   if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
   13245     return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
   13246   if (strcmp (section, ".reg-s390-high-gprs") == 0)
   13247     return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
   13248   if (strcmp (section, ".reg-s390-timer") == 0)
   13249     return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
   13250   if (strcmp (section, ".reg-s390-todcmp") == 0)
   13251     return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
   13252   if (strcmp (section, ".reg-s390-todpreg") == 0)
   13253     return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
   13254   if (strcmp (section, ".reg-s390-ctrs") == 0)
   13255     return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
   13256   if (strcmp (section, ".reg-s390-prefix") == 0)
   13257     return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
   13258   if (strcmp (section, ".reg-s390-last-break") == 0)
   13259     return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
   13260   if (strcmp (section, ".reg-s390-system-call") == 0)
   13261     return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
   13262   if (strcmp (section, ".reg-s390-tdb") == 0)
   13263     return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
   13264   if (strcmp (section, ".reg-s390-vxrs-low") == 0)
   13265     return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
   13266   if (strcmp (section, ".reg-s390-vxrs-high") == 0)
   13267     return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
   13268   if (strcmp (section, ".reg-s390-gs-cb") == 0)
   13269     return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
   13270   if (strcmp (section, ".reg-s390-gs-bc") == 0)
   13271     return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
   13272   if (strcmp (section, ".reg-arm-vfp") == 0)
   13273     return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
   13274   if (strcmp (section, ".reg-aarch-tls") == 0)
   13275     return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
   13276   if (strcmp (section, ".reg-aarch-hw-break") == 0)
   13277     return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
   13278   if (strcmp (section, ".reg-aarch-hw-watch") == 0)
   13279     return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
   13280   if (strcmp (section, ".reg-aarch-sve") == 0)
   13281     return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
   13282   if (strcmp (section, ".reg-aarch-pauth") == 0)
   13283     return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
   13284   if (strcmp (section, ".reg-aarch-mte") == 0)
   13285     return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
   13286   if (strcmp (section, ".reg-aarch-ssve") == 0)
   13287     return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size);
   13288   if (strcmp (section, ".reg-aarch-za") == 0)
   13289     return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size);
   13290   if (strcmp (section, ".reg-aarch-zt") == 0)
   13291     return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size);
   13292   if (strcmp (section, ".reg-aarch-gcs") == 0)
   13293     return elfcore_write_aarch_gcs (abfd, buf, bufsiz, data, size);
   13294   if (strcmp (section, ".reg-arc-v2") == 0)
   13295     return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
   13296   if (strcmp (section, ".gdb-tdesc") == 0)
   13297     return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
   13298   if (strcmp (section, ".reg-riscv-csr") == 0)
   13299     return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
   13300   if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
   13301     return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
   13302   if (strcmp (section, ".reg-loongarch-lbt") == 0)
   13303     return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
   13304   if (strcmp (section, ".reg-loongarch-lsx") == 0)
   13305     return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
   13306   if (strcmp (section, ".reg-loongarch-lasx") == 0)
   13307     return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
   13308   return NULL;
   13309 }
   13310 
   13311 char *
   13312 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   13313 			 const void *buf, int bufsiz)
   13314 {
   13315   return elfcore_write_note (obfd, note_data, note_size,
   13316 			     "CORE", NT_FILE, buf, bufsiz);
   13317 }
   13318 
   13319 static bool
   13320 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   13321 		 size_t align)
   13322 {
   13323   char *p;
   13324 
   13325   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   13326      gABI specifies that PT_NOTE alignment should be aligned to 4
   13327      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   13328      align is less than 4, we use 4 byte alignment.   */
   13329   if (align < 4)
   13330     align = 4;
   13331   if (align != 4 && align != 8)
   13332     return false;
   13333 
   13334   p = buf;
   13335   while (p < buf + size)
   13336     {
   13337       Elf_External_Note *xnp = (Elf_External_Note *) p;
   13338       Elf_Internal_Note in;
   13339 
   13340       if (offsetof (Elf_External_Note, name) > buf - p + size)
   13341 	return false;
   13342 
   13343       in.type = H_GET_32 (abfd, xnp->type);
   13344 
   13345       in.namesz = H_GET_32 (abfd, xnp->namesz);
   13346       in.namedata = xnp->name;
   13347       if (in.namesz > buf - in.namedata + size)
   13348 	return false;
   13349 
   13350       in.descsz = H_GET_32 (abfd, xnp->descsz);
   13351       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   13352       in.descpos = offset + (in.descdata - buf);
   13353       if (in.descsz != 0
   13354 	  && (in.descdata >= buf + size
   13355 	      || in.descsz > buf - in.descdata + size))
   13356 	return false;
   13357 
   13358       switch (bfd_get_format (abfd))
   13359 	{
   13360 	default:
   13361 	  return true;
   13362 
   13363 	case bfd_core:
   13364 	  {
   13365 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   13366 	    struct
   13367 	    {
   13368 	      const char * string;
   13369 	      size_t len;
   13370 	      bool (*func) (bfd *, Elf_Internal_Note *);
   13371 	    }
   13372 	    grokers[] =
   13373 	    {
   13374 	      GROKER_ELEMENT ("", elfcore_grok_note),
   13375 	      GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
   13376 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   13377 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   13378 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   13379 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   13380 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   13381 	      GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
   13382 	    };
   13383 #undef GROKER_ELEMENT
   13384 	    int i;
   13385 
   13386 	    for (i = ARRAY_SIZE (grokers); i--;)
   13387 	      {
   13388 		if (in.namesz >= grokers[i].len
   13389 		    && strncmp (in.namedata, grokers[i].string,
   13390 				grokers[i].len) == 0)
   13391 		  {
   13392 		    if (! grokers[i].func (abfd, & in))
   13393 		      return false;
   13394 		    break;
   13395 		  }
   13396 	      }
   13397 	    break;
   13398 	  }
   13399 
   13400 	case bfd_object:
   13401 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   13402 	    {
   13403 	      if (! elfobj_grok_gnu_note (abfd, &in))
   13404 		return false;
   13405 	    }
   13406 	  else if (in.namesz == sizeof "stapsdt"
   13407 		   && strcmp (in.namedata, "stapsdt") == 0)
   13408 	    {
   13409 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   13410 		return false;
   13411 	    }
   13412 	  break;
   13413 	}
   13414 
   13415       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   13416     }
   13417 
   13418   return true;
   13419 }
   13420 
   13421 bool
   13422 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   13423 		size_t align)
   13424 {
   13425   char *buf;
   13426 
   13427   if (size == 0 || (size + 1) == 0)
   13428     return true;
   13429 
   13430   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   13431     return false;
   13432 
   13433   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   13434   if (buf == NULL)
   13435     return false;
   13436 
   13437   /* PR 17512: file: ec08f814
   13438      0-termintate the buffer so that string searches will not overflow.  */
   13439   buf[size] = 0;
   13440 
   13441   if (!elf_parse_notes (abfd, buf, size, offset, align))
   13442     {
   13443       free (buf);
   13444       return false;
   13445     }
   13446 
   13447   free (buf);
   13448   return true;
   13449 }
   13450 
   13451 /* Providing external access to the ELF program header table.  */
   13453 
   13454 /* Return an upper bound on the number of bytes required to store a
   13455    copy of ABFD's program header table entries.  Return -1 if an error
   13456    occurs; bfd_get_error will return an appropriate code.  */
   13457 
   13458 long
   13459 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   13460 {
   13461   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13462     {
   13463       bfd_set_error (bfd_error_wrong_format);
   13464       return -1;
   13465     }
   13466 
   13467   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   13468 }
   13469 
   13470 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   13471    will be stored as an array of Elf_Internal_Phdr structures, as
   13472    defined in include/elf/internal.h.  To find out how large the
   13473    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   13474 
   13475    Return the number of program header table entries read, or -1 if an
   13476    error occurs; bfd_get_error will return an appropriate code.  */
   13477 
   13478 int
   13479 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   13480 {
   13481   int num_phdrs;
   13482 
   13483   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13484     {
   13485       bfd_set_error (bfd_error_wrong_format);
   13486       return -1;
   13487     }
   13488 
   13489   num_phdrs = elf_elfheader (abfd)->e_phnum;
   13490   if (num_phdrs != 0)
   13491     memcpy (phdrs, elf_tdata (abfd)->phdr,
   13492 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   13493 
   13494   return num_phdrs;
   13495 }
   13496 
   13497 enum elf_reloc_type_class
   13498 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13499 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   13500 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   13501 {
   13502   return reloc_class_normal;
   13503 }
   13504 
   13505 /* For RELA architectures, return the relocation value for a
   13506    relocation against a local symbol.  */
   13507 
   13508 bfd_vma
   13509 _bfd_elf_rela_local_sym (bfd *abfd,
   13510 			 Elf_Internal_Sym *sym,
   13511 			 asection **psec,
   13512 			 Elf_Internal_Rela *rel)
   13513 {
   13514   asection *sec = *psec;
   13515   bfd_vma relocation;
   13516 
   13517   relocation = (sec->output_section->vma
   13518 		+ sec->output_offset
   13519 		+ sym->st_value);
   13520   if ((sec->flags & SEC_MERGE)
   13521       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   13522       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   13523     {
   13524       rel->r_addend =
   13525 	_bfd_merged_section_offset (abfd, psec,
   13526 				    elf_section_data (sec)->sec_info,
   13527 				    sym->st_value + rel->r_addend);
   13528       if (sec != *psec)
   13529 	{
   13530 	  /* If we have changed the section, and our original section is
   13531 	     marked with SEC_EXCLUDE, it means that the original
   13532 	     SEC_MERGE section has been completely subsumed in some
   13533 	     other SEC_MERGE section.  In this case, we need to leave
   13534 	     some info around for --emit-relocs.  */
   13535 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   13536 	    sec->kept_section = *psec;
   13537 	  sec = *psec;
   13538 	}
   13539       rel->r_addend -= relocation;
   13540       rel->r_addend += sec->output_section->vma + sec->output_offset;
   13541     }
   13542   return relocation;
   13543 }
   13544 
   13545 bfd_vma
   13546 _bfd_elf_rel_local_sym (bfd *abfd,
   13547 			Elf_Internal_Sym *sym,
   13548 			asection **psec,
   13549 			bfd_vma addend)
   13550 {
   13551   asection *sec = *psec;
   13552 
   13553   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   13554     return sym->st_value + addend;
   13555 
   13556   return _bfd_merged_section_offset (abfd, psec,
   13557 				     elf_section_data (sec)->sec_info,
   13558 				     sym->st_value + addend);
   13559 }
   13560 
   13561 /* Adjust an address within a section.  Given OFFSET within SEC, return
   13562    the new offset within the section, based upon changes made to the
   13563    section.  Returns -1 if the offset is now invalid.
   13564    The offset (in abnd out) is in target sized bytes, however big a
   13565    byte may be.  */
   13566 
   13567 bfd_vma
   13568 _bfd_elf_section_offset (bfd *abfd,
   13569 			 struct bfd_link_info *info,
   13570 			 asection *sec,
   13571 			 bfd_vma offset)
   13572 {
   13573   switch (sec->sec_info_type)
   13574     {
   13575     case SEC_INFO_TYPE_STABS:
   13576       return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
   13577 				       offset);
   13578     case SEC_INFO_TYPE_EH_FRAME:
   13579       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   13580 
   13581     case SEC_INFO_TYPE_SFRAME:
   13582       return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
   13583 
   13584     default:
   13585       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   13586 	{
   13587 	  /* Reverse the offset.  */
   13588 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13589 	  bfd_size_type address_size = bed->s->arch_size / 8;
   13590 
   13591 	  /* address_size and sec->size are in octets.  Convert
   13592 	     to bytes before subtracting the original offset.  */
   13593 	  offset = ((sec->size - address_size)
   13594 		    / bfd_octets_per_byte (abfd, sec) - offset);
   13595 	}
   13596       return offset;
   13597     }
   13598 }
   13599 
   13600 long
   13602 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   13603 			       long symcount ATTRIBUTE_UNUSED,
   13604 			       asymbol **syms ATTRIBUTE_UNUSED,
   13605 			       long dynsymcount,
   13606 			       asymbol **dynsyms,
   13607 			       asymbol **ret)
   13608 {
   13609   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   13610   asection *relplt;
   13611   asymbol *s;
   13612   const char *relplt_name;
   13613   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   13614   arelent *p;
   13615   long count, i, n;
   13616   size_t size;
   13617   Elf_Internal_Shdr *hdr;
   13618   char *names;
   13619   asection *plt;
   13620 
   13621   *ret = NULL;
   13622 
   13623   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   13624     return 0;
   13625 
   13626   if (dynsymcount <= 0)
   13627     return 0;
   13628 
   13629   if (!bed->plt_sym_val)
   13630     return 0;
   13631 
   13632   relplt_name = bed->relplt_name;
   13633   if (relplt_name == NULL)
   13634     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   13635   relplt = bfd_get_section_by_name (abfd, relplt_name);
   13636   if (relplt == NULL)
   13637     return 0;
   13638 
   13639   hdr = &elf_section_data (relplt)->this_hdr;
   13640   if (hdr->sh_link != elf_dynsymtab (abfd)
   13641       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   13642     return 0;
   13643 
   13644   plt = bfd_get_section_by_name (abfd, ".plt");
   13645   if (plt == NULL)
   13646     return 0;
   13647 
   13648   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   13649   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   13650     return -1;
   13651 
   13652   count = NUM_SHDR_ENTRIES (hdr);
   13653   size = count * sizeof (asymbol);
   13654   p = relplt->relocation;
   13655   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13656     {
   13657       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   13658       if (p->addend != 0)
   13659 	{
   13660 #ifdef BFD64
   13661 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   13662 #else
   13663 	  size += sizeof ("+0x") - 1 + 8;
   13664 #endif
   13665 	}
   13666     }
   13667 
   13668   s = *ret = (asymbol *) bfd_malloc (size);
   13669   if (s == NULL)
   13670     return -1;
   13671 
   13672   names = (char *) (s + count);
   13673   p = relplt->relocation;
   13674   n = 0;
   13675   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13676     {
   13677       size_t len;
   13678       bfd_vma addr;
   13679 
   13680       addr = bed->plt_sym_val (i, plt, p);
   13681       if (addr == (bfd_vma) -1)
   13682 	continue;
   13683 
   13684       *s = **p->sym_ptr_ptr;
   13685       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   13686 	 we are defining a symbol, ensure one of them is set.  */
   13687       if ((s->flags & BSF_LOCAL) == 0)
   13688 	s->flags |= BSF_GLOBAL;
   13689       s->flags |= BSF_SYNTHETIC;
   13690       s->section = plt;
   13691       s->value = addr - plt->vma;
   13692       s->name = names;
   13693       s->udata.p = NULL;
   13694       len = strlen ((*p->sym_ptr_ptr)->name);
   13695       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   13696       names += len;
   13697       if (p->addend != 0)
   13698 	{
   13699 	  char buf[30], *a;
   13700 
   13701 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   13702 	  names += sizeof ("+0x") - 1;
   13703 	  bfd_sprintf_vma (abfd, buf, p->addend);
   13704 	  for (a = buf; *a == '0'; ++a)
   13705 	    ;
   13706 	  len = strlen (a);
   13707 	  memcpy (names, a, len);
   13708 	  names += len;
   13709 	}
   13710       memcpy (names, "@plt", sizeof ("@plt"));
   13711       names += sizeof ("@plt");
   13712       ++s, ++n;
   13713     }
   13714 
   13715   return n;
   13716 }
   13717 
   13718 /* It is only used by x86-64 so far.
   13719    ??? This repeats *COM* id of zero.  sec->id is supposed to be unique,
   13720    but current usage would allow all of _bfd_std_section to be zero.  */
   13721 static const asymbol lcomm_sym
   13722   = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
   13723 asection _bfd_elf_large_com_section
   13724   = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
   13725 		      "LARGE_COMMON", 0, SEC_IS_COMMON);
   13726 
   13727 bool
   13728 _bfd_elf_final_write_processing (bfd *abfd)
   13729 {
   13730   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   13731 
   13732   i_ehdrp = elf_elfheader (abfd);
   13733 
   13734   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13735     i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   13736 
   13737   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13738      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13739      or STB_GNU_UNIQUE binding.  */
   13740   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13741     {
   13742       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13743 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13744       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13745 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13746 	{
   13747 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13748 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13749 				  "and FreeBSD targets"));
   13750 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13751 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13752 				  "only by GNU and FreeBSD targets"));
   13753 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13754 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13755 				  "only by GNU and FreeBSD targets"));
   13756 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13757 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13758 				  "only by GNU and FreeBSD targets"));
   13759 	  bfd_set_error (bfd_error_sorry);
   13760 	  return false;
   13761 	}
   13762     }
   13763   return true;
   13764 }
   13765 
   13766 
   13767 /* Return TRUE for ELF symbol types that represent functions.
   13768    This is the default version of this function, which is sufficient for
   13769    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13770 
   13771 bool
   13772 _bfd_elf_is_function_type (unsigned int type)
   13773 {
   13774   return (type == STT_FUNC
   13775 	  || type == STT_GNU_IFUNC);
   13776 }
   13777 
   13778 /* If the ELF symbol SYM might be a function in SEC, return the
   13779    function size and set *CODE_OFF to the function's entry point,
   13780    otherwise return zero.  */
   13781 
   13782 bfd_size_type
   13783 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13784 			     bfd_vma *code_off)
   13785 {
   13786   bfd_size_type size;
   13787   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13788 
   13789   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13790 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13791       || sym->section != sec)
   13792     return 0;
   13793 
   13794   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13795 
   13796   /* In theory we should check that the symbol's type satisfies
   13797      _bfd_elf_is_function_type(), but there are some function-like
   13798      symbols which would fail this test.  (eg _start).  Instead
   13799      we check for hidden, local, notype symbols with zero size.
   13800      This type of symbol is generated by the annobin plugin for gcc
   13801      and clang, and should not be considered to be a function symbol.  */
   13802   if (size == 0
   13803       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13804       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13805       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13806     return 0;
   13807 
   13808   *code_off = sym->value;
   13809   /* Do not return 0 for the function's size.  */
   13810   return size ? size : 1;
   13811 }
   13812 
   13813 /* Set to non-zero to enable some debug messages.  */
   13814 #define DEBUG_SECONDARY_RELOCS	 0
   13815 
   13816 /* An internal-to-the-bfd-library only section type
   13817    used to indicate a cached secondary reloc section.  */
   13818 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13819 
   13820 /* Create a BFD section to hold a secondary reloc section.  */
   13821 
   13822 bool
   13823 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13824 				       Elf_Internal_Shdr *hdr,
   13825 				       const char * name,
   13826 				       unsigned int shindex)
   13827 {
   13828   /* We only support RELA secondary relocs.  */
   13829   if (hdr->sh_type != SHT_RELA)
   13830     return false;
   13831 
   13832 #if DEBUG_SECONDARY_RELOCS
   13833   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13834 #endif
   13835   hdr->sh_type = SHT_SECONDARY_RELOC;
   13836   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13837 }
   13838 
   13839 /* Read in any secondary relocs associated with SEC.  */
   13840 
   13841 bool
   13842 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13843 					asection *  sec,
   13844 					asymbol **  symbols,
   13845 					bool dynamic)
   13846 {
   13847   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   13848   asection * relsec;
   13849   bool result = true;
   13850   bfd_vma (*r_sym) (bfd_vma);
   13851   ufile_ptr filesize;
   13852 
   13853 #if BFD_DEFAULT_TARGET_SIZE > 32
   13854   if (bfd_arch_bits_per_address (abfd) != 32)
   13855     r_sym = elf64_r_sym;
   13856   else
   13857 #endif
   13858     r_sym = elf32_r_sym;
   13859 
   13860   if (!elf_section_data (sec)->has_secondary_relocs)
   13861     return true;
   13862 
   13863   /* Discover if there are any secondary reloc sections
   13864      associated with SEC.  */
   13865   filesize = bfd_get_file_size (abfd);
   13866   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13867     {
   13868       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13869 
   13870       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13871 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13872 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13873 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13874 	{
   13875 	  bfd_byte * native_relocs;
   13876 	  bfd_byte * native_reloc;
   13877 	  arelent * internal_relocs;
   13878 	  arelent * internal_reloc;
   13879 	  size_t i;
   13880 	  unsigned int entsize;
   13881 	  unsigned int symcount;
   13882 	  bfd_size_type reloc_count;
   13883 	  size_t amt;
   13884 
   13885 	  if (ebd->elf_info_to_howto == NULL)
   13886 	    return false;
   13887 
   13888 #if DEBUG_SECONDARY_RELOCS
   13889 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13890 		   sec->name, relsec->name);
   13891 #endif
   13892 	  entsize = hdr->sh_entsize;
   13893 
   13894 	  if (filesize != 0
   13895 	      && ((ufile_ptr) hdr->sh_offset > filesize
   13896 		  || hdr->sh_size > filesize - hdr->sh_offset))
   13897 	    {
   13898 	      bfd_set_error (bfd_error_file_truncated);
   13899 	      result = false;
   13900 	      continue;
   13901 	    }
   13902 
   13903 	  native_relocs = bfd_malloc (hdr->sh_size);
   13904 	  if (native_relocs == NULL)
   13905 	    {
   13906 	      result = false;
   13907 	      continue;
   13908 	    }
   13909 
   13910 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13911 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13912 	    {
   13913 	      free (native_relocs);
   13914 	      bfd_set_error (bfd_error_file_too_big);
   13915 	      result = false;
   13916 	      continue;
   13917 	    }
   13918 
   13919 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   13920 	  if (internal_relocs == NULL)
   13921 	    {
   13922 	      free (native_relocs);
   13923 	      result = false;
   13924 	      continue;
   13925 	    }
   13926 
   13927 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   13928 	      || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
   13929 	    {
   13930 	      free (native_relocs);
   13931 	      /* The internal_relocs will be freed when
   13932 		 the memory for the bfd is released.  */
   13933 	      result = false;
   13934 	      continue;
   13935 	    }
   13936 
   13937 	  if (dynamic)
   13938 	    symcount = bfd_get_dynamic_symcount (abfd);
   13939 	  else
   13940 	    symcount = bfd_get_symcount (abfd);
   13941 
   13942 	  for (i = 0, internal_reloc = internal_relocs,
   13943 		 native_reloc = native_relocs;
   13944 	       i < reloc_count;
   13945 	       i++, internal_reloc++, native_reloc += entsize)
   13946 	    {
   13947 	      bool res;
   13948 	      Elf_Internal_Rela rela;
   13949 
   13950 	      if (entsize == ebd->s->sizeof_rel)
   13951 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   13952 	      else /* entsize == ebd->s->sizeof_rela */
   13953 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   13954 
   13955 	      /* The address of an ELF reloc is section relative for an object
   13956 		 file, and absolute for an executable file or shared library.
   13957 		 The address of a normal BFD reloc is always section relative,
   13958 		 and the address of a dynamic reloc is absolute..  */
   13959 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   13960 		internal_reloc->address = rela.r_offset;
   13961 	      else
   13962 		internal_reloc->address = rela.r_offset - sec->vma;
   13963 
   13964 	      if (r_sym (rela.r_info) == STN_UNDEF)
   13965 		{
   13966 		  /* FIXME: This and the error case below mean that we
   13967 		     have a symbol on relocs that is not elf_symbol_type.  */
   13968 		  internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   13969 		}
   13970 	      else if (r_sym (rela.r_info) > symcount)
   13971 		{
   13972 		  _bfd_error_handler
   13973 		    /* xgettext:c-format */
   13974 		    (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
   13975 		     abfd, sec, i, (long) r_sym (rela.r_info));
   13976 		  bfd_set_error (bfd_error_bad_value);
   13977 		  internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   13978 		  result = false;
   13979 		}
   13980 	      else
   13981 		{
   13982 		  asymbol **ps;
   13983 
   13984 		  ps = symbols + r_sym (rela.r_info) - 1;
   13985 		  internal_reloc->sym_ptr_ptr = ps;
   13986 		  /* Make sure that this symbol is not removed by strip.  */
   13987 		  (*ps)->flags |= BSF_KEEP;
   13988 		}
   13989 
   13990 	      internal_reloc->addend = rela.r_addend;
   13991 
   13992 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   13993 	      if (! res || internal_reloc->howto == NULL)
   13994 		{
   13995 #if DEBUG_SECONDARY_RELOCS
   13996 		  fprintf (stderr,
   13997 			   "there is no howto associated with reloc %lx\n",
   13998 			   rela.r_info);
   13999 #endif
   14000 		  result = false;
   14001 		}
   14002 	    }
   14003 
   14004 	  free (native_relocs);
   14005 	  /* Store the internal relocs.  */
   14006 	  elf_section_data (relsec)->sec_info = internal_relocs;
   14007 	}
   14008     }
   14009 
   14010   return result;
   14011 }
   14012 
   14013 /* Set the ELF section header fields of an output secondary reloc section.  */
   14014 
   14015 bool
   14016 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
   14017 				      bfd *obfd ATTRIBUTE_UNUSED,
   14018 				      const Elf_Internal_Shdr *isection,
   14019 				      Elf_Internal_Shdr *osection)
   14020 {
   14021   asection * isec;
   14022   asection * osec;
   14023   struct bfd_elf_section_data * esd;
   14024 
   14025   if (isection == NULL)
   14026     return false;
   14027 
   14028   if (isection->sh_type != SHT_SECONDARY_RELOC)
   14029     return true;
   14030 
   14031   isec = isection->bfd_section;
   14032   if (isec == NULL)
   14033     return false;
   14034 
   14035   osec = osection->bfd_section;
   14036   if (osec == NULL)
   14037     return false;
   14038 
   14039   esd = elf_section_data (osec);
   14040   BFD_ASSERT (esd->sec_info == NULL);
   14041   esd->sec_info = elf_section_data (isec)->sec_info;
   14042   osection->sh_type = SHT_RELA;
   14043   osection->sh_link = elf_onesymtab (obfd);
   14044   if (osection->sh_link == 0)
   14045     {
   14046       /* There is no symbol table - we are hosed...  */
   14047       _bfd_error_handler
   14048 	/* xgettext:c-format */
   14049 	(_("%pB(%pA): link section cannot be set"
   14050 	   " because the output file does not have a symbol table"),
   14051 	obfd, osec);
   14052       bfd_set_error (bfd_error_bad_value);
   14053       return false;
   14054     }
   14055 
   14056   /* Find the output section that corresponds to the isection's
   14057      sh_info link.  */
   14058   if (isection->sh_info == 0
   14059       || isection->sh_info >= elf_numsections (ibfd))
   14060     {
   14061       _bfd_error_handler
   14062 	/* xgettext:c-format */
   14063 	(_("%pB(%pA): info section index is invalid"),
   14064 	obfd, osec);
   14065       bfd_set_error (bfd_error_bad_value);
   14066       return false;
   14067     }
   14068 
   14069   isection = elf_elfsections (ibfd)[isection->sh_info];
   14070 
   14071   if (isection == NULL
   14072       || isection->bfd_section == NULL
   14073       || isection->bfd_section->output_section == NULL)
   14074     {
   14075       _bfd_error_handler
   14076 	/* xgettext:c-format */
   14077 	(_("%pB(%pA): info section index cannot be set"
   14078 	   " because the section is not in the output"),
   14079 	obfd, osec);
   14080       bfd_set_error (bfd_error_bad_value);
   14081       return false;
   14082     }
   14083 
   14084   esd = elf_section_data (isection->bfd_section->output_section);
   14085   BFD_ASSERT (esd != NULL);
   14086   osection->sh_info = esd->this_idx;
   14087   esd->has_secondary_relocs = true;
   14088 #if DEBUG_SECONDARY_RELOCS
   14089   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   14090 	   osec->name, osection->sh_link, osection->sh_info);
   14091   fprintf (stderr, "mark section %s as having secondary relocs\n",
   14092 	   bfd_section_name (isection->bfd_section->output_section));
   14093 #endif
   14094 
   14095   return true;
   14096 }
   14097 
   14098 /* Write out a secondary reloc section.
   14099 
   14100    FIXME: Currently this function can result in a serious performance penalty
   14101    for files with secondary relocs and lots of sections.  The proper way to
   14102    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   14103    relocs together and then to have this function just walk that chain.  */
   14104 
   14105 bool
   14106 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   14107 {
   14108   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   14109   bfd_vma addr_offset;
   14110   asection * relsec;
   14111   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   14112   bool result = true;
   14113 
   14114   if (sec == NULL)
   14115     return false;
   14116 
   14117 #if BFD_DEFAULT_TARGET_SIZE > 32
   14118   if (bfd_arch_bits_per_address (abfd) != 32)
   14119     r_info = elf64_r_info;
   14120   else
   14121 #endif
   14122     r_info = elf32_r_info;
   14123 
   14124   /* The address of an ELF reloc is section relative for an object
   14125      file, and absolute for an executable file or shared library.
   14126      The address of a BFD reloc is always section relative.  */
   14127   addr_offset = 0;
   14128   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   14129     addr_offset = sec->vma;
   14130 
   14131   /* Discover if there are any secondary reloc sections
   14132      associated with SEC.  */
   14133   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   14134     {
   14135       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   14136       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   14137 
   14138       if (hdr->sh_type == SHT_RELA
   14139 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   14140 	{
   14141 	  asymbol *    last_sym;
   14142 	  int          last_sym_idx;
   14143 	  size_t       reloc_count;
   14144 	  size_t       idx;
   14145 	  bfd_size_type entsize;
   14146 	  arelent *    src_irel;
   14147 	  bfd_byte *   dst_rela;
   14148 
   14149 	  if (hdr->contents != NULL)
   14150 	    {
   14151 	      _bfd_error_handler
   14152 		/* xgettext:c-format */
   14153 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   14154 		 abfd, relsec);
   14155 	      bfd_set_error (bfd_error_bad_value);
   14156 	      result = false;
   14157 	      continue;
   14158 	    }
   14159 
   14160 	  entsize = hdr->sh_entsize;
   14161 	  if (entsize == 0)
   14162 	    {
   14163 	      _bfd_error_handler
   14164 		/* xgettext:c-format */
   14165 		(_("%pB(%pA): error: secondary reloc section"
   14166 		   " has zero sized entries"),
   14167 		 abfd, relsec);
   14168 	      bfd_set_error (bfd_error_bad_value);
   14169 	      result = false;
   14170 	      continue;
   14171 	    }
   14172 	  else if (entsize != ebd->s->sizeof_rel
   14173 		   && entsize != ebd->s->sizeof_rela)
   14174 	    {
   14175 	      _bfd_error_handler
   14176 		/* xgettext:c-format */
   14177 		(_("%pB(%pA): error: secondary reloc section"
   14178 		   " has non-standard sized entries"),
   14179 		 abfd, relsec);
   14180 	      bfd_set_error (bfd_error_bad_value);
   14181 	      result = false;
   14182 	      continue;
   14183 	    }
   14184 
   14185 	  reloc_count = hdr->sh_size / entsize;
   14186 	  hdr->sh_size = entsize * reloc_count;
   14187 	  if (reloc_count == 0)
   14188 	    {
   14189 	      _bfd_error_handler
   14190 		/* xgettext:c-format */
   14191 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   14192 		 abfd, relsec);
   14193 	      bfd_set_error (bfd_error_bad_value);
   14194 	      result = false;
   14195 	      continue;
   14196 	    }
   14197 
   14198 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   14199 	  if (hdr->contents == NULL)
   14200 	    continue;
   14201 	  relsec->alloced = 1;
   14202 
   14203 #if DEBUG_SECONDARY_RELOCS
   14204 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   14205 		   reloc_count, sec->name, relsec->name);
   14206 #endif
   14207 	  last_sym = NULL;
   14208 	  last_sym_idx = 0;
   14209 	  dst_rela = hdr->contents;
   14210 	  src_irel = (arelent *) esd->sec_info;
   14211 	  if (src_irel == NULL)
   14212 	    {
   14213 	      _bfd_error_handler
   14214 		/* xgettext:c-format */
   14215 		(_("%pB(%pA): error: internal relocs missing"
   14216 		   " for secondary reloc section"),
   14217 		 abfd, relsec);
   14218 	      bfd_set_error (bfd_error_bad_value);
   14219 	      result = false;
   14220 	      continue;
   14221 	    }
   14222 
   14223 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   14224 	    {
   14225 	      Elf_Internal_Rela src_rela;
   14226 	      arelent *ptr;
   14227 	      asymbol *sym;
   14228 	      int n;
   14229 
   14230 	      ptr = src_irel + idx;
   14231 	      if (ptr == NULL)
   14232 		{
   14233 		  _bfd_error_handler
   14234 		    /* xgettext:c-format */
   14235 		    (_("%pB(%pA): error: reloc table entry %zu is empty"),
   14236 		     abfd, relsec, idx);
   14237 		  bfd_set_error (bfd_error_bad_value);
   14238 		  result = false;
   14239 		  break;
   14240 		}
   14241 
   14242 	      if (ptr->sym_ptr_ptr == NULL)
   14243 		{
   14244 		  /* FIXME: Is this an error ? */
   14245 		  n = 0;
   14246 		}
   14247 	      else
   14248 		{
   14249 		  sym = *ptr->sym_ptr_ptr;
   14250 
   14251 		  if (sym == last_sym)
   14252 		    n = last_sym_idx;
   14253 		  else
   14254 		    {
   14255 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   14256 		      if (n < 0)
   14257 			{
   14258 			  _bfd_error_handler
   14259 			    /* xgettext:c-format */
   14260 			    (_("%pB(%pA): error: secondary reloc %zu"
   14261 			       " references a missing symbol"),
   14262 			     abfd, relsec, idx);
   14263 			  bfd_set_error (bfd_error_bad_value);
   14264 			  result = false;
   14265 			  n = 0;
   14266 			}
   14267 
   14268 		      last_sym = sym;
   14269 		      last_sym_idx = n;
   14270 		    }
   14271 
   14272 		  if (sym->the_bfd != NULL
   14273 		      && sym->the_bfd->xvec != abfd->xvec
   14274 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   14275 		    {
   14276 		      _bfd_error_handler
   14277 			/* xgettext:c-format */
   14278 			(_("%pB(%pA): error: secondary reloc %zu"
   14279 			   " references a deleted symbol"),
   14280 			 abfd, relsec, idx);
   14281 		      bfd_set_error (bfd_error_bad_value);
   14282 		      result = false;
   14283 		      n = 0;
   14284 		    }
   14285 		}
   14286 
   14287 	      src_rela.r_offset = ptr->address + addr_offset;
   14288 	      if (ptr->howto == NULL)
   14289 		{
   14290 		  _bfd_error_handler
   14291 		    /* xgettext:c-format */
   14292 		    (_("%pB(%pA): error: secondary reloc %zu"
   14293 		       " is of an unknown type"),
   14294 		     abfd, relsec, idx);
   14295 		  bfd_set_error (bfd_error_bad_value);
   14296 		  result = false;
   14297 		  src_rela.r_info = r_info (0, 0);
   14298 		}
   14299 	      else
   14300 		src_rela.r_info = r_info (n, ptr->howto->type);
   14301 	      src_rela.r_addend = ptr->addend;
   14302 
   14303 	      if (entsize == ebd->s->sizeof_rel)
   14304 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   14305 	      else /* entsize == ebd->s->sizeof_rela */
   14306 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   14307 	    }
   14308 	}
   14309     }
   14310 
   14311   return result;
   14312 }
   14313 
   14314 /* Mmap in section contents.  */
   14315 
   14316 static bool
   14317 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
   14318 {
   14319 #ifdef USE_MMAP
   14320   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   14321   if (bed->use_mmap
   14322       && sec->compress_status == COMPRESS_SECTION_NONE
   14323       && (sec->flags & SEC_LINKER_CREATED) == 0)
   14324     {
   14325       /* Use mmap only if section size >= the minimum mmap section
   14326 	 size.  */
   14327       size_t readsz = bfd_get_section_limit_octets (abfd, sec);
   14328       size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
   14329       if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
   14330 	{
   14331 	  if (sec->contents != NULL)
   14332 	    {
   14333 	      if (!sec->mmapped_p)
   14334 		abort ();
   14335 	      *buf = sec->contents;
   14336 	      return true;
   14337 	    }
   14338 	  if (sec->mmapped_p)
   14339 	    abort ();
   14340 	  sec->mmapped_p = 1;
   14341 
   14342 	  /* We can't use the final link preallocated buffer for mmap.  */
   14343 	  *buf = NULL;
   14344 	}
   14345     }
   14346 #endif
   14347   /* FIXME: We should not get here if sec->alloced is set.  */
   14348   bool ret = bfd_get_full_section_contents (abfd, sec, buf);
   14349   if (ret && sec->mmapped_p)
   14350     *buf = sec->contents;
   14351   return ret;
   14352 }
   14353 
   14354 /* Mmap in section contents.  */
   14355 
   14356 bool
   14357 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
   14358 {
   14359   *buf = NULL;
   14360   return elf_mmap_section_contents (abfd, sec, buf);
   14361 }
   14362 
   14363 /* Mmap in the full section contents for the final link.  */
   14364 
   14365 bool
   14366 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
   14367 				     bfd_byte **buf)
   14368 {
   14369   return elf_mmap_section_contents (abfd, sec, buf);
   14370 }
   14371 
   14372 /* Munmap section contents.  */
   14373 
   14374 void
   14375 _bfd_elf_munmap_section_contents (asection *sec, void *contents)
   14376 {
   14377   /* NB: Since _bfd_elf_munmap_section_contents is called like free,
   14378      CONTENTS may be NULL.  */
   14379   if (contents == NULL)
   14380     return;
   14381 
   14382   if (sec->alloced
   14383       /* What a tangled web we weave with section contents.
   14384 	 FIXME: We shouldn't need to test anything but sec->alloced
   14385 	 here, but there are cases where a buffer is allocated for a
   14386 	 section but then another buffer is malloc'd anyway.  eg.
   14387 	 trace through ld-elf/eh4 testcase on x86_64.  */
   14388       && (sec->contents == contents
   14389 	  || elf_section_data (sec)->this_hdr.contents == contents))
   14390     return;
   14391 
   14392   /* Don't leave pointers to data we are about to munmap or free.  */
   14393   if (sec->contents == contents)
   14394     sec->contents = NULL;
   14395   if (elf_section_data (sec)->this_hdr.contents == contents)
   14396     elf_section_data (sec)->this_hdr.contents = NULL;
   14397 
   14398 #ifdef USE_MMAP
   14399   if (sec->mmapped_p)
   14400     {
   14401       /* When _bfd_elf_mmap_section_contents returns CONTENTS as
   14402 	 malloced, CONTENTS_ADDR is set to NULL.  */
   14403       if (elf_section_data (sec)->contents_addr != NULL)
   14404 	{
   14405 	  /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
   14406 	  if (munmap (elf_section_data (sec)->contents_addr,
   14407 		      elf_section_data (sec)->contents_size) != 0)
   14408 	    abort ();
   14409 	  sec->mmapped_p = 0;
   14410 	  elf_section_data (sec)->contents_addr = NULL;
   14411 	  elf_section_data (sec)->contents_size = 0;
   14412 	  return;
   14413 	}
   14414     }
   14415 #endif
   14416 
   14417   free (contents);
   14418 }
   14419 
   14420 /* Munmap the full section contents for the final link.  */
   14421 
   14422 void
   14423 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
   14424 {
   14425 #ifdef USE_MMAP
   14426   if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
   14427     {
   14428       /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
   14429 	 malloced, CONTENTS_ADDR is set to NULL.  */
   14430       /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
   14431       if (munmap (elf_section_data (sec)->contents_addr,
   14432 		  elf_section_data (sec)->contents_size) != 0)
   14433 	abort ();
   14434       sec->mmapped_p = 0;
   14435       sec->contents = NULL;
   14436       elf_section_data (sec)->this_hdr.contents = NULL;
   14437       elf_section_data (sec)->contents_addr = NULL;
   14438       elf_section_data (sec)->contents_size = 0;
   14439     }
   14440 #endif
   14441 }
   14442