Home | History | Annotate | Line # | Download | only in bfd
elf.c revision 1.22
      1 /* ELF executable support for BFD.
      2 
      3    Copyright (C) 1993-2026 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 /* Utility macro to make testing for string equality easier to read.  */
     53 #ifndef streq
     54 #define streq(A,B) (strcmp ((A), (B)) == 0)
     55 #endif
     56 
     57 /* Core note names.  */
     58 #define NOTE_NAME_CORE     "CORE"
     59 #define NOTE_NAME_FREEBSD  "FreeBSD"
     60 #define NOTE_NAME_GDB      "GDB"
     61 #define NOTE_NAME_LINUX    "LINUX"
     62 
     63 /* Names of a pseudo-section which represent core notes.  */
     64 #define NOTE_PSEUDO_SECTION_AARCH_FPMR		".reg-aarch-fpmr"
     65 #define NOTE_PSEUDO_SECTION_AARCH_GCS		".reg-aarch-gcs"
     66 #define NOTE_PSEUDO_SECTION_AARCH_HW_BREAK	".reg-aarch-hw-break"
     67 #define NOTE_PSEUDO_SECTION_AARCH_HW_WATCH	".reg-aarch-hw-watch"
     68 #define NOTE_PSEUDO_SECTION_AARCH_MTE		".reg-aarch-mte"
     69 #define NOTE_PSEUDO_SECTION_AARCH_PAUTH		".reg-aarch-pauth"
     70 #define NOTE_PSEUDO_SECTION_AARCH_SSVE		".reg-aarch-ssve"
     71 #define NOTE_PSEUDO_SECTION_AARCH_SVE		".reg-aarch-sve"
     72 #define NOTE_PSEUDO_SECTION_AARCH_TLS		".reg-aarch-tls"
     73 #define NOTE_PSEUDO_SECTION_AARCH_ZA		".reg-aarch-za"
     74 #define NOTE_PSEUDO_SECTION_AARCH_ZT		".reg-aarch-zt"
     75 #define NOTE_PSEUDO_SECTION_ARC_V2		".reg-arc-v2"
     76 #define NOTE_PSEUDO_SECTION_ARM_VFP		".reg-arm-vfp"
     77 #define NOTE_PSEUDO_SECTION_I386_TLS	 	".reg-i386-tls"
     78 #define NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG	".reg-loongarch-cpucfg"
     79 #define NOTE_PSEUDO_SECTION_LOONGARCH_LASX	".reg-loongarch-lasx"
     80 #define NOTE_PSEUDO_SECTION_LOONGARCH_LBT	".reg-loongarch-lbt"
     81 #define NOTE_PSEUDO_SECTION_LOONGARCH_LSX	".reg-loongarch-lsx"
     82 #define NOTE_PSEUDO_SECTION_PPC_DSCR		".reg-ppc-dscr"
     83 #define NOTE_PSEUDO_SECTION_PPC_EBB		".reg-ppc-ebb"
     84 #define NOTE_PSEUDO_SECTION_PPC_PMU		".reg-ppc-pmu"
     85 #define NOTE_PSEUDO_SECTION_PPC_PPR		".reg-ppc-ppr"
     86 #define NOTE_PSEUDO_SECTION_PPC_TAR		".reg-ppc-tar"
     87 #define NOTE_PSEUDO_SECTION_PPC_TM_CDSCR	".reg-ppc-tm-cdscr"
     88 #define NOTE_PSEUDO_SECTION_PPC_TM_CFPR		".reg-ppc-tm-cfpr"
     89 #define NOTE_PSEUDO_SECTION_PPC_TM_CGPR		".reg-ppc-tm-cgpr"
     90 #define NOTE_PSEUDO_SECTION_PPC_TM_CPPR		".reg-ppc-tm-cppr"
     91 #define NOTE_PSEUDO_SECTION_PPC_TM_CTAR		".reg-ppc-tm-ctar"
     92 #define NOTE_PSEUDO_SECTION_PPC_TM_CVMX		".reg-ppc-tm-cvmx"
     93 #define NOTE_PSEUDO_SECTION_PPC_TM_CVSX		".reg-ppc-tm-cvsx"
     94 #define NOTE_PSEUDO_SECTION_PPC_TM_SPR		".reg-ppc-tm-spr"
     95 #define NOTE_PSEUDO_SECTION_PPC_VSX		".reg-ppc-vsx"
     96 #define NOTE_PSEUDO_SECTION_PPC_VMX		".reg-ppc-vmx"
     97 #define NOTE_PSEUDO_SECTION_REG		 	".reg"
     98 #define NOTE_PSEUDO_SECTION_REG2	 	".reg2"
     99 #define NOTE_PSEUDO_SECTION_RISCV_CSR		".reg-riscv-csr"
    100 #define NOTE_PSEUDO_SECTION_S390_CTRS		".reg-s390-ctrs"
    101 #define NOTE_PSEUDO_SECTION_S390_GS_BC		".reg-s390-gs-bc"
    102 #define NOTE_PSEUDO_SECTION_S390_GS_CB		".reg-s390-gs-cb"
    103 #define NOTE_PSEUDO_SECTION_S390_HIGH_GPRS	".reg-s390-high-gprs"
    104 #define NOTE_PSEUDO_SECTION_S390_LAST_BREAK	".reg-s390-last-break"
    105 #define NOTE_PSEUDO_SECTION_S390_PREFIX		".reg-s390-prefix"
    106 #define NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL	".reg-s390-system-call"
    107 #define NOTE_PSEUDO_SECTION_S390_TDB		".reg-s390-tdb"
    108 #define NOTE_PSEUDO_SECTION_S390_TIMER		".reg-s390-timer"
    109 #define NOTE_PSEUDO_SECTION_S390_TODCMP		".reg-s390-todcmp"
    110 #define NOTE_PSEUDO_SECTION_S390_TODPREG	".reg-s390-todpreg"
    111 #define NOTE_PSEUDO_SECTION_S390_VXRS_HIGH	".reg-s390-vxrs-high"
    112 #define NOTE_PSEUDO_SECTION_S390_VXRS_LOW	".reg-s390-vxrs-low"
    113 #define NOTE_PSEUDO_SECTION_SSP		 	".reg-ssp"
    114 #define NOTE_PSEUDO_SECTION_TDESC		".gdb-tdesc"
    115 #define NOTE_PSEUDO_SECTION_X86_SEGBASES 	".reg-x86-segbases"
    116 #define NOTE_PSEUDO_SECTION_XFP          	".reg-xfp"
    117 #define NOTE_PSEUDO_SECTION_XSTATE       	".reg-xstate"
    118 #define NOTE_PSEUDO_SECTION_XSAVE_LAYOUT	".reg-xsave-layout"
    119 
    120 static int elf_sort_sections (const void *, const void *);
    121 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
    122 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
    123 			   struct bfd_link_info *);
    124 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
    125 			     file_ptr offset, size_t align);
    126 
    127 /* Swap version information in and out.  The version information is
    128    currently size independent.  If that ever changes, this code will
    129    need to move into elfcode.h.  */
    130 
    131 /* Swap in a Verdef structure.  */
    132 
    133 void
    134 _bfd_elf_swap_verdef_in (bfd *abfd,
    135 			 const Elf_External_Verdef *src,
    136 			 Elf_Internal_Verdef *dst)
    137 {
    138   dst->vd_version = H_GET_16 (abfd, src->vd_version);
    139   dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
    140   dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
    141   dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
    142   dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
    143   dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
    144   dst->vd_next    = H_GET_32 (abfd, src->vd_next);
    145 }
    146 
    147 /* Swap out a Verdef structure.  */
    148 
    149 void
    150 _bfd_elf_swap_verdef_out (bfd *abfd,
    151 			  const Elf_Internal_Verdef *src,
    152 			  Elf_External_Verdef *dst)
    153 {
    154   H_PUT_16 (abfd, src->vd_version, dst->vd_version);
    155   H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
    156   H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
    157   H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
    158   H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
    159   H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
    160   H_PUT_32 (abfd, src->vd_next, dst->vd_next);
    161 }
    162 
    163 /* Swap in a Verdaux structure.  */
    164 
    165 void
    166 _bfd_elf_swap_verdaux_in (bfd *abfd,
    167 			  const Elf_External_Verdaux *src,
    168 			  Elf_Internal_Verdaux *dst)
    169 {
    170   dst->vda_name = H_GET_32 (abfd, src->vda_name);
    171   dst->vda_next = H_GET_32 (abfd, src->vda_next);
    172 }
    173 
    174 /* Swap out a Verdaux structure.  */
    175 
    176 void
    177 _bfd_elf_swap_verdaux_out (bfd *abfd,
    178 			   const Elf_Internal_Verdaux *src,
    179 			   Elf_External_Verdaux *dst)
    180 {
    181   H_PUT_32 (abfd, src->vda_name, dst->vda_name);
    182   H_PUT_32 (abfd, src->vda_next, dst->vda_next);
    183 }
    184 
    185 /* Swap in a Verneed structure.  */
    186 
    187 void
    188 _bfd_elf_swap_verneed_in (bfd *abfd,
    189 			  const Elf_External_Verneed *src,
    190 			  Elf_Internal_Verneed *dst)
    191 {
    192   dst->vn_version = H_GET_16 (abfd, src->vn_version);
    193   dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
    194   dst->vn_file    = H_GET_32 (abfd, src->vn_file);
    195   dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
    196   dst->vn_next    = H_GET_32 (abfd, src->vn_next);
    197 }
    198 
    199 /* Swap out a Verneed structure.  */
    200 
    201 void
    202 _bfd_elf_swap_verneed_out (bfd *abfd,
    203 			   const Elf_Internal_Verneed *src,
    204 			   Elf_External_Verneed *dst)
    205 {
    206   H_PUT_16 (abfd, src->vn_version, dst->vn_version);
    207   H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
    208   H_PUT_32 (abfd, src->vn_file, dst->vn_file);
    209   H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
    210   H_PUT_32 (abfd, src->vn_next, dst->vn_next);
    211 }
    212 
    213 /* Swap in a Vernaux structure.  */
    214 
    215 void
    216 _bfd_elf_swap_vernaux_in (bfd *abfd,
    217 			  const Elf_External_Vernaux *src,
    218 			  Elf_Internal_Vernaux *dst)
    219 {
    220   dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
    221   dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
    222   dst->vna_other = H_GET_16 (abfd, src->vna_other);
    223   dst->vna_name  = H_GET_32 (abfd, src->vna_name);
    224   dst->vna_next  = H_GET_32 (abfd, src->vna_next);
    225 }
    226 
    227 /* Swap out a Vernaux structure.  */
    228 
    229 void
    230 _bfd_elf_swap_vernaux_out (bfd *abfd,
    231 			   const Elf_Internal_Vernaux *src,
    232 			   Elf_External_Vernaux *dst)
    233 {
    234   H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
    235   H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
    236   H_PUT_16 (abfd, src->vna_other, dst->vna_other);
    237   H_PUT_32 (abfd, src->vna_name, dst->vna_name);
    238   H_PUT_32 (abfd, src->vna_next, dst->vna_next);
    239 }
    240 
    241 /* Swap in a Versym structure.  */
    242 
    243 void
    244 _bfd_elf_swap_versym_in (bfd *abfd,
    245 			 const Elf_External_Versym *src,
    246 			 Elf_Internal_Versym *dst)
    247 {
    248   dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
    249 }
    250 
    251 /* Swap out a Versym structure.  */
    252 
    253 void
    254 _bfd_elf_swap_versym_out (bfd *abfd,
    255 			  const Elf_Internal_Versym *src,
    256 			  Elf_External_Versym *dst)
    257 {
    258   H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
    259 }
    260 
    261 /* Standard ELF hash function.  Do not change this function; you will
    262    cause invalid hash tables to be generated.  */
    263 
    264 unsigned long
    265 bfd_elf_hash (const char *namearg)
    266 {
    267   uint32_t h = 0;
    268 
    269   for (const unsigned char *name = (const unsigned char *) namearg;
    270        *name; name++)
    271     {
    272       h = (h << 4) + *name;
    273       h ^= (h >> 24) & 0xf0;
    274     }
    275   return h & 0x0fffffff;
    276 }
    277 
    278 /* DT_GNU_HASH hash function.  Do not change this function; you will
    279    cause invalid hash tables to be generated.  */
    280 
    281 unsigned long
    282 bfd_elf_gnu_hash (const char *namearg)
    283 {
    284   uint32_t h = 5381;
    285 
    286   for (const unsigned char *name = (const unsigned char *) namearg;
    287        *name; name++)
    288     h = (h << 5) + h + *name;
    289   return h;
    290 }
    291 
    292 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
    293    the object_id field of an elf_obj_tdata field set.  */
    294 bool
    295 bfd_elf_allocate_object (bfd *abfd,
    296 			 size_t object_size)
    297 {
    298   BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
    299   abfd->tdata.any = bfd_zalloc (abfd, object_size);
    300   if (abfd->tdata.any == NULL)
    301     return false;
    302 
    303   elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
    304   if (abfd->direction != read_direction)
    305     {
    306       struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
    307       if (o == NULL)
    308 	return false;
    309       elf_tdata (abfd)->o = o;
    310       elf_program_header_size (abfd) = (bfd_size_type) -1;
    311     }
    312   return true;
    313 }
    314 
    315 
    316 bool
    317 bfd_elf_make_object (bfd *abfd)
    318 {
    319   return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
    320 }
    321 
    322 bool
    323 bfd_elf_mkcorefile (bfd *abfd)
    324 {
    325   /* I think this can be done just like an object file.  */
    326   if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
    327     return false;
    328   elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
    329   return elf_tdata (abfd)->core != NULL;
    330 }
    331 
    332 char *
    333 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
    334 {
    335   Elf_Internal_Shdr **i_shdrp;
    336   bfd_byte *shstrtab = NULL;
    337   file_ptr offset;
    338   bfd_size_type shstrtabsize;
    339 
    340   i_shdrp = elf_elfsections (abfd);
    341   if (i_shdrp == 0
    342       || shindex >= elf_numsections (abfd)
    343       || i_shdrp[shindex] == 0)
    344     return NULL;
    345 
    346   shstrtab = i_shdrp[shindex]->contents;
    347   if (shstrtab == NULL)
    348     {
    349       /* No cached one, attempt to read, and cache what we read.  */
    350       offset = i_shdrp[shindex]->sh_offset;
    351       shstrtabsize = i_shdrp[shindex]->sh_size;
    352 
    353       if (shstrtabsize == 0
    354 	  || bfd_seek (abfd, offset, SEEK_SET) != 0
    355 	  || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
    356 	{
    357 	  /* Once we've failed to read it, make sure we don't keep
    358 	     trying.  Otherwise, we'll keep allocating space for
    359 	     the string table over and over.  */
    360 	  i_shdrp[shindex]->sh_size = 0;
    361 	}
    362       else if (shstrtab[shstrtabsize - 1] != 0)
    363 	{
    364 	  /* It is an error if a string table isn't terminated.  */
    365 	  _bfd_error_handler
    366 	    /* xgettext:c-format */
    367 	    (_("%pB: string table [%u] is corrupt"), abfd, shindex);
    368 	  shstrtab[shstrtabsize - 1] = 0;
    369 	}
    370       i_shdrp[shindex]->contents = shstrtab;
    371     }
    372   return (char *) shstrtab;
    373 }
    374 
    375 char *
    376 bfd_elf_string_from_elf_section (bfd *abfd,
    377 				 unsigned int shindex,
    378 				 unsigned int strindex)
    379 {
    380   Elf_Internal_Shdr *hdr;
    381 
    382   if (strindex == 0)
    383     return "";
    384 
    385   if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
    386     return NULL;
    387 
    388   hdr = elf_elfsections (abfd)[shindex];
    389 
    390   if (hdr->contents == NULL)
    391     {
    392       if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
    393 	{
    394 	  /* PR 17512: file: f057ec89.  */
    395 	  /* xgettext:c-format */
    396 	  _bfd_error_handler (_("%pB: attempt to load strings from"
    397 				" a non-string section (number %d)"),
    398 			      abfd, shindex);
    399 	  return NULL;
    400 	}
    401 
    402       if (bfd_elf_get_str_section (abfd, shindex) == NULL)
    403 	return NULL;
    404     }
    405   else
    406     {
    407       /* PR 24273: The string section's contents may have already
    408 	 been loaded elsewhere, eg because a corrupt file has the
    409 	 string section index in the ELF header pointing at a group
    410 	 section.  So be paranoid, and test that the last byte of
    411 	 the section is zero.  */
    412       if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
    413 	return NULL;
    414     }
    415 
    416   if (strindex >= hdr->sh_size)
    417     {
    418       unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
    419       _bfd_error_handler
    420 	/* xgettext:c-format */
    421 	(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
    422 	 abfd, strindex, (uint64_t) hdr->sh_size,
    423 	 (shindex == shstrndx && strindex == hdr->sh_name
    424 	  ? ".shstrtab"
    425 	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
    426       return NULL;
    427     }
    428 
    429   return ((char *) hdr->contents) + strindex;
    430 }
    431 
    432 /* Read and convert symbols to internal format.
    433    SYMCOUNT specifies the number of symbols to read, starting from
    434    symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
    435    are non-NULL, they are used to store the internal symbols, external
    436    symbols, and symbol section index extensions, respectively.
    437    Returns a pointer to the internal symbol buffer (malloced if necessary)
    438    or NULL if there were no symbols or some kind of problem.  */
    439 
    440 Elf_Internal_Sym *
    441 bfd_elf_get_elf_syms (bfd *ibfd,
    442 		      Elf_Internal_Shdr *symtab_hdr,
    443 		      size_t symcount,
    444 		      size_t symoffset,
    445 		      Elf_Internal_Sym *intsym_buf,
    446 		      void *extsym_buf,
    447 		      Elf_External_Sym_Shndx *extshndx_buf)
    448 {
    449   Elf_Internal_Shdr *shndx_hdr;
    450   void *alloc_ext;
    451   const bfd_byte *esym;
    452   Elf_External_Sym_Shndx *alloc_extshndx;
    453   Elf_External_Sym_Shndx *shndx;
    454   Elf_Internal_Sym *alloc_intsym;
    455   Elf_Internal_Sym *isym;
    456   Elf_Internal_Sym *isymend;
    457   elf_backend_data *bed;
    458   size_t extsym_size;
    459   size_t amt;
    460   file_ptr pos;
    461 
    462   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
    463     abort ();
    464 
    465   if (symcount == 0)
    466     return intsym_buf;
    467 
    468   if (elf_use_dt_symtab_p (ibfd))
    469     {
    470       /* Use dynamic symbol table.  */
    471       if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
    472 	{
    473 	  bfd_set_error (bfd_error_invalid_operation);
    474 	  return NULL;
    475 	}
    476       return elf_tdata (ibfd)->dt_symtab + symoffset;
    477     }
    478 
    479   /* Normal syms might have section extension entries.  */
    480   shndx_hdr = NULL;
    481   if (elf_symtab_shndx_list (ibfd) != NULL)
    482     {
    483       elf_section_list * entry;
    484       Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
    485 
    486       /* Find an index section that is linked to this symtab section.  */
    487       for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
    488 	{
    489 	  /* PR 20063.  */
    490 	  if (entry->hdr.sh_link >= elf_numsections (ibfd))
    491 	    continue;
    492 
    493 	  if (sections[entry->hdr.sh_link] == symtab_hdr)
    494 	    {
    495 	      shndx_hdr = & entry->hdr;
    496 	      break;
    497 	    };
    498 	}
    499 
    500       if (shndx_hdr == NULL)
    501 	{
    502 	  if (symtab_hdr == &elf_symtab_hdr (ibfd))
    503 	    /* Not really accurate, but this was how the old code used
    504 	       to work.  */
    505 	    shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
    506 	  /* Otherwise we do nothing.  The assumption is that
    507 	     the index table will not be needed.  */
    508 	}
    509     }
    510 
    511   /* Read the symbols.  */
    512   alloc_ext = NULL;
    513   alloc_extshndx = NULL;
    514   alloc_intsym = NULL;
    515   bed = get_elf_backend_data (ibfd);
    516   extsym_size = bed->s->sizeof_sym;
    517   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
    518     {
    519       bfd_set_error (bfd_error_file_too_big);
    520       return NULL;
    521     }
    522   pos = symtab_hdr->sh_offset + symoffset * extsym_size;
    523   size_t alloc_ext_size = amt;
    524   if (bfd_seek (ibfd, pos, SEEK_SET) != 0
    525       || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
    526 				    &alloc_ext, ibfd, false))
    527     {
    528       intsym_buf = NULL;
    529       goto out2;
    530     }
    531 
    532   size_t alloc_extshndx_size = 0;
    533   if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
    534     extshndx_buf = NULL;
    535   else
    536     {
    537       if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
    538 	{
    539 	  bfd_set_error (bfd_error_file_too_big);
    540 	  intsym_buf = NULL;
    541 	  goto out1;
    542 	}
    543       alloc_extshndx_size = amt;
    544       pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
    545       if (bfd_seek (ibfd, pos, SEEK_SET) != 0
    546 	  || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
    547 					&alloc_extshndx_size,
    548 					(void **) &alloc_extshndx,
    549 					ibfd, false))
    550 	{
    551 	  intsym_buf = NULL;
    552 	  goto out1;
    553 	}
    554     }
    555 
    556   if (intsym_buf == NULL)
    557     {
    558       if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
    559 	{
    560 	  bfd_set_error (bfd_error_file_too_big);
    561 	  goto out1;
    562 	}
    563       alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
    564       intsym_buf = alloc_intsym;
    565       if (intsym_buf == NULL)
    566 	goto out1;
    567     }
    568 
    569   /* Convert the symbols to internal form.  */
    570   isymend = intsym_buf + symcount;
    571   for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
    572 	   shndx = extshndx_buf;
    573        isym < isymend;
    574        esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
    575     {
    576       if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
    577 	{
    578 	  symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
    579 	  /* xgettext:c-format */
    580 	  _bfd_error_handler (_("%pB symbol number %lu references"
    581 				" nonexistent SHT_SYMTAB_SHNDX section"),
    582 			      ibfd, (unsigned long) symoffset);
    583 	  free (alloc_intsym);
    584 	  intsym_buf = NULL;
    585 	  goto out1;
    586 	}
    587 
    588       /* PR 33019: Do not accept unsupported binding values - they will
    589 	 likely cause problems later on.  */
    590       int bind = ELF_ST_BIND (isym->st_info);
    591       if (bind > STB_WEAK && bind < STB_LOOS)
    592 	{
    593 	  /* xgettext:c-format */
    594 	  _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
    595 			      ibfd, (unsigned long) (isym - intsym_buf), bind);
    596 	  free (alloc_intsym);
    597 	  intsym_buf = NULL;
    598 	  goto out1;
    599 	}
    600 
    601       /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
    602       int t = ELF_ST_TYPE (isym->st_info);
    603       if (t == 7)
    604 	{
    605 	  /* xgettext:c-format */
    606 	  _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
    607 			      ibfd, (unsigned long) (isym - intsym_buf), t);
    608 	  free (alloc_intsym);
    609 	  intsym_buf = NULL;
    610 	  goto out1;
    611 	}
    612     }
    613 
    614  out1:
    615   _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
    616  out2:
    617   _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
    618 
    619   return intsym_buf;
    620 }
    621 
    622 /* Look up a symbol name.  */
    623 static const char *
    624 bfd_elf_sym_name_raw (bfd *abfd,
    625 		      Elf_Internal_Shdr *symtab_hdr,
    626 		      Elf_Internal_Sym *isym)
    627 {
    628   unsigned int iname = isym->st_name;
    629   unsigned int shindex = symtab_hdr->sh_link;
    630 
    631   if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
    632       /* Check for a bogus st_shndx to avoid crashing.  */
    633       && isym->st_shndx < elf_numsections (abfd))
    634     {
    635       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
    636       shindex = elf_elfheader (abfd)->e_shstrndx;
    637     }
    638 
    639   return bfd_elf_string_from_elf_section (abfd, shindex, iname);
    640 }
    641 
    642 const char *
    643 bfd_elf_sym_name (bfd *abfd,
    644 		  Elf_Internal_Shdr *symtab_hdr,
    645 		  Elf_Internal_Sym *isym,
    646 		  asection *sym_sec)
    647 {
    648   const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
    649   if (name == NULL)
    650     name = bfd_symbol_error_name;
    651   else if (sym_sec && *name == '\0')
    652     name = bfd_section_name (sym_sec);
    653 
    654   return name;
    655 }
    656 
    657 /* Return the name of the group signature symbol.  Why isn't the
    658    signature just a string?  */
    659 
    660 static const char *
    661 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
    662 {
    663   Elf_Internal_Shdr *hdr;
    664   unsigned char esym[sizeof (Elf64_External_Sym)];
    665   Elf_External_Sym_Shndx eshndx;
    666   Elf_Internal_Sym isym;
    667 
    668   /* First we need to ensure the symbol table is available.  Make sure
    669      that it is a symbol table section.  */
    670   if (ghdr->sh_link >= elf_numsections (abfd))
    671     return NULL;
    672   hdr = elf_elfsections (abfd) [ghdr->sh_link];
    673   if (hdr->sh_type != SHT_SYMTAB
    674       || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
    675     return NULL;
    676 
    677   /* Go read the symbol.  */
    678   hdr = &elf_tdata (abfd)->symtab_hdr;
    679   if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
    680 			    &isym, esym, &eshndx) == NULL)
    681     return NULL;
    682 
    683   return bfd_elf_sym_name_raw (abfd, hdr, &isym);
    684 }
    685 
    686 static bool
    687 is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
    688 {
    689   return (shdr->sh_size >= minsize
    690 	  && shdr->sh_entsize == GRP_ENTRY_SIZE
    691 	  && shdr->sh_size % GRP_ENTRY_SIZE == 0
    692 	  && shdr->bfd_section != NULL);
    693 }
    694 
    695 
    696 /* Set next_in_group, sec_group list pointers, and group names.  */
    697 
    698 static bool
    699 process_sht_group_entries (bfd *abfd,
    700 			   Elf_Internal_Shdr *ghdr, unsigned int gidx)
    701 {
    702   unsigned char *contents;
    703 
    704   /* Read the raw contents.  */
    705   if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
    706     {
    707       _bfd_error_handler
    708 	/* xgettext:c-format */
    709 	(_("%pB: could not read contents of group [%u]"), abfd, gidx);
    710       return false;
    711     }
    712 
    713   asection *last_elt = NULL;
    714   const char *gname = NULL;
    715   unsigned char *p = contents + ghdr->sh_size;
    716   while (1)
    717     {
    718       unsigned int idx;
    719       Elf_Internal_Shdr *shdr;
    720       asection *elt;
    721 
    722       p -= 4;
    723       idx = H_GET_32 (abfd, p);
    724       if (p == contents)
    725 	{
    726 	  if ((idx & GRP_COMDAT) != 0)
    727 	    ghdr->bfd_section->flags
    728 	      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
    729 	  break;
    730 	}
    731 
    732       if (idx == 0
    733 	  || idx >= elf_numsections (abfd)
    734 	  || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
    735 	  || ((elt = shdr->bfd_section) != NULL
    736 	      && elf_sec_group (elt) != NULL
    737 	      && elf_sec_group (elt) != ghdr->bfd_section))
    738 	{
    739 	  _bfd_error_handler
    740 	    (_("%pB: invalid entry (%#x) in group [%u]"),
    741 	     abfd, idx, gidx);
    742 	  continue;
    743 	}
    744 
    745       /* PR binutils/23199: According to the ELF gABI all sections in
    746 	 a group must be marked with SHF_GROUP, but some tools
    747 	 generate broken objects.  Fix them up here.  */
    748       shdr->sh_flags |= SHF_GROUP;
    749 
    750       if (elt == NULL)
    751 	{
    752 	  if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
    753 	    {
    754 	      const char *name = bfd_elf_string_from_elf_section
    755 		(abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
    756 
    757 	      _bfd_error_handler
    758 		/* xgettext:c-format */
    759 		(_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
    760 		 abfd, shdr->sh_type, name, gidx);
    761 	    }
    762 	  continue;
    763 	}
    764 
    765       /* Don't try to add a section to elf_next_in_group list twice.  */
    766       if (elf_sec_group (elt) != NULL)
    767 	continue;
    768 
    769       if (last_elt == NULL)
    770 	{
    771 	  /* Start a circular list with one element.
    772 	     It will be in reverse order to match what gas does.  */
    773 	  elf_next_in_group (elt) = elt;
    774 	  /* Point the group section to it.  */
    775 	  elf_next_in_group (ghdr->bfd_section) = elt;
    776 	  gname = group_signature (abfd, ghdr);
    777 	  if (gname == NULL)
    778 	    {
    779 	      free (contents);
    780 	      return false;
    781 	    }
    782 	}
    783       else
    784 	{
    785 	  elf_next_in_group (elt) = elf_next_in_group (last_elt);
    786 	  elf_next_in_group (last_elt) = elt;
    787 	}
    788       last_elt = elt;
    789       elf_group_name (elt) = gname;
    790       elf_sec_group (elt) = ghdr->bfd_section;
    791     }
    792 
    793   free (contents);
    794   return last_elt != NULL;
    795 }
    796 
    797 bool
    798 _bfd_elf_setup_sections (bfd *abfd)
    799 {
    800   bool result = true;
    801 
    802   /* Process SHF_LINK_ORDER.  */
    803   for (asection *s = abfd->sections; s != NULL; s = s->next)
    804     {
    805       Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
    806       if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
    807 	{
    808 	  unsigned int elfsec = this_hdr->sh_link;
    809 	  /* An sh_link value of 0 is now allowed.  It indicates that linked
    810 	     to section has already been discarded, but that the current
    811 	     section has been retained for some other reason.  This linking
    812 	     section is still a candidate for later garbage collection
    813 	     however.  */
    814 	  if (elfsec == 0)
    815 	    {
    816 	      elf_linked_to_section (s) = NULL;
    817 	    }
    818 	  else
    819 	    {
    820 	      asection *linksec = NULL;
    821 
    822 	      if (elfsec < elf_numsections (abfd))
    823 		{
    824 		  this_hdr = elf_elfsections (abfd)[elfsec];
    825 		  linksec = this_hdr->bfd_section;
    826 		}
    827 
    828 	      /* PR 1991, 2008:
    829 		 Some strip/objcopy may leave an incorrect value in
    830 		 sh_link.  We don't want to proceed.  */
    831 	      if (linksec == NULL)
    832 		{
    833 		  _bfd_error_handler
    834 		    /* xgettext:c-format */
    835 		    (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
    836 		     s->owner, elfsec, s);
    837 		  result = false;
    838 		}
    839 
    840 	      elf_linked_to_section (s) = linksec;
    841 	    }
    842 	}
    843     }
    844 
    845   /* Process section groups.  */
    846   for (unsigned int i = 1; i < elf_numsections (abfd); i++)
    847     {
    848       Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
    849 
    850       if (shdr && shdr->sh_type == SHT_GROUP)
    851 	{
    852 	  if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
    853 	    {
    854 	      if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
    855 		  && !process_sht_group_entries (abfd, shdr, i))
    856 		result = false;
    857 	    }
    858 	  else
    859 	    {
    860 	      /* PR binutils/18758: Beware of corrupt binaries with
    861 		 invalid group data.  */
    862 	      _bfd_error_handler
    863 		/* xgettext:c-format */
    864 		(_("%pB: section group entry number %u is corrupt"), abfd, i);
    865 	      result = false;
    866 	    }
    867 	}
    868     }
    869 
    870   return result;
    871 }
    872 
    873 bool
    874 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    875 {
    876   return elf_next_in_group (sec) != NULL;
    877 }
    878 
    879 const char *
    880 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
    881 {
    882   if (elf_sec_group (sec) != NULL)
    883     return elf_group_name (sec);
    884   return NULL;
    885 }
    886 
    887 /* Make a BFD section from an ELF section.  We store a pointer to the
    888    BFD section in the bfd_section field of the header.  */
    889 
    890 bool
    891 _bfd_elf_make_section_from_shdr (bfd *abfd,
    892 				 Elf_Internal_Shdr *hdr,
    893 				 const char *name,
    894 				 int shindex)
    895 {
    896   asection *newsect;
    897   flagword flags;
    898   elf_backend_data *bed;
    899   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
    900 
    901   if (hdr->bfd_section != NULL)
    902     return true;
    903 
    904   newsect = bfd_make_section_anyway (abfd, name);
    905   if (newsect == NULL)
    906     return false;
    907 
    908   hdr->bfd_section = newsect;
    909   elf_section_data (newsect)->this_hdr = *hdr;
    910   elf_section_data (newsect)->this_idx = shindex;
    911 
    912   /* Always use the real type/flags.  */
    913   elf_section_type (newsect) = hdr->sh_type;
    914   elf_section_flags (newsect) = hdr->sh_flags;
    915 
    916   newsect->filepos = hdr->sh_offset;
    917 
    918   flags = SEC_NO_FLAGS;
    919   if (hdr->sh_type != SHT_NOBITS)
    920     flags |= SEC_HAS_CONTENTS;
    921   if (hdr->sh_type == SHT_GROUP)
    922     flags |= SEC_GROUP;
    923   if ((hdr->sh_flags & SHF_ALLOC) != 0)
    924     {
    925       flags |= SEC_ALLOC;
    926       if (hdr->sh_type != SHT_NOBITS)
    927 	flags |= SEC_LOAD;
    928     }
    929   if ((hdr->sh_flags & SHF_WRITE) == 0)
    930     flags |= SEC_READONLY;
    931   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
    932     flags |= SEC_CODE;
    933   else if ((flags & SEC_LOAD) != 0)
    934     flags |= SEC_DATA;
    935   if ((hdr->sh_flags & SHF_MERGE) != 0)
    936     flags |= SEC_MERGE;
    937   if ((hdr->sh_flags & SHF_STRINGS) != 0)
    938     flags |= SEC_STRINGS;
    939   if ((hdr->sh_flags & SHF_TLS) != 0)
    940     flags |= SEC_THREAD_LOCAL;
    941   if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
    942     flags |= SEC_EXCLUDE;
    943 
    944   newsect->entsize = hdr->sh_entsize;
    945 
    946   switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
    947     {
    948       /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
    949 	 but binutils as of 2019-07-23 did not set the EI_OSABI header
    950 	 byte.  */
    951     case ELFOSABI_GNU:
    952     case ELFOSABI_FREEBSD:
    953       if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
    954 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
    955       /* Fall through */
    956     case ELFOSABI_NONE:
    957       if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
    958 	elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
    959       break;
    960     }
    961 
    962   if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0)
    963     {
    964       /* The debugging sections appear to be recognized only by name,
    965 	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
    966       if (name [0] == '.')
    967 	{
    968 	  if (startswith (name, ".debug")
    969 	      || startswith (name, ".gnu.debuglto_.debug_")
    970 	      || startswith (name, ".gnu.linkonce.wi.")
    971 	      || startswith (name, ".zdebug"))
    972 	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
    973 	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
    974 		   || startswith (name, ".note.gnu"))
    975 	    {
    976 	      flags |= SEC_ELF_OCTETS;
    977 	      opb = 1;
    978 	    }
    979 	  else if (startswith (name, ".line")
    980 		   || startswith (name, ".stab")
    981 		   || streq (name, ".gdb_index"))
    982 	    flags |= SEC_DEBUGGING;
    983 	}
    984     }
    985 
    986   if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
    987       || !bfd_set_section_size (newsect, hdr->sh_size)
    988       || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
    989 							& -hdr->sh_addralign)))
    990     return false;
    991 
    992   /* As a GNU extension, if the name begins with .gnu.linkonce, we
    993      only link a single copy of the section.  This is used to support
    994      g++.  g++ will emit each template expansion in its own section.
    995      The symbols will be defined as weak, so that multiple definitions
    996      are permitted.  The GNU linker extension is to actually discard
    997      all but one of the sections.  */
    998   if (startswith (name, ".gnu.linkonce")
    999       && elf_next_in_group (newsect) == NULL)
   1000     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
   1001 
   1002   if (!bfd_set_section_flags (newsect, flags))
   1003     return false;
   1004 
   1005   bed = get_elf_backend_data (abfd);
   1006   if (bed->elf_backend_section_flags)
   1007     if (!bed->elf_backend_section_flags (hdr))
   1008       return false;
   1009 
   1010   /* We do not parse the PT_NOTE segments as we are interested even in the
   1011      separate debug info files which may have the segments offsets corrupted.
   1012      PT_NOTEs from the core files are currently not parsed using BFD.  */
   1013   if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
   1014     {
   1015       bfd_byte *contents;
   1016 
   1017       if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
   1018 	return false;
   1019 
   1020       elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
   1021 		       hdr->sh_offset, hdr->sh_addralign);
   1022       _bfd_elf_munmap_section_contents (newsect, contents);
   1023     }
   1024 
   1025   if ((newsect->flags & SEC_ALLOC) != 0)
   1026     {
   1027       Elf_Internal_Phdr *phdr;
   1028       unsigned int i, nload;
   1029 
   1030       /* Some ELF linkers produce binaries with all the program header
   1031 	 p_paddr fields zero.  If we have such a binary with more than
   1032 	 one PT_LOAD header, then leave the section lma equal to vma
   1033 	 so that we don't create sections with overlapping lma.  */
   1034       phdr = elf_tdata (abfd)->phdr;
   1035       for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1036 	if (phdr->p_paddr != 0)
   1037 	  break;
   1038 	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
   1039 	  ++nload;
   1040       if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
   1041 	return true;
   1042 
   1043       phdr = elf_tdata (abfd)->phdr;
   1044       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
   1045 	{
   1046 	  if (((phdr->p_type == PT_LOAD
   1047 		&& (hdr->sh_flags & SHF_TLS) == 0)
   1048 	       || phdr->p_type == PT_TLS)
   1049 	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
   1050 	    {
   1051 	      if ((newsect->flags & SEC_LOAD) == 0)
   1052 		newsect->lma = (phdr->p_paddr
   1053 				+ hdr->sh_addr - phdr->p_vaddr) / opb;
   1054 	      else
   1055 		/* We used to use the same adjustment for SEC_LOAD
   1056 		   sections, but that doesn't work if the segment
   1057 		   is packed with code from multiple VMAs.
   1058 		   Instead we calculate the section LMA based on
   1059 		   the segment LMA.  It is assumed that the
   1060 		   segment will contain sections with contiguous
   1061 		   LMAs, even if the VMAs are not.  */
   1062 		newsect->lma = (phdr->p_paddr
   1063 				+ hdr->sh_offset - phdr->p_offset) / opb;
   1064 
   1065 	      /* With contiguous segments, we can't tell from file
   1066 		 offsets whether a section with zero size should
   1067 		 be placed at the end of one segment or the
   1068 		 beginning of the next.  Decide based on vaddr.  */
   1069 	      if (hdr->sh_addr >= phdr->p_vaddr
   1070 		  && (hdr->sh_addr + hdr->sh_size
   1071 		      <= phdr->p_vaddr + phdr->p_memsz))
   1072 		break;
   1073 	    }
   1074 	}
   1075     }
   1076 
   1077   /* Compress/decompress DWARF debug sections with names: .debug_*,
   1078      .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
   1079   if ((newsect->flags & SEC_DEBUGGING) != 0
   1080       && (newsect->flags & SEC_HAS_CONTENTS) != 0
   1081       && (newsect->flags & SEC_ELF_OCTETS) != 0)
   1082     {
   1083       enum { nothing, compress, decompress } action = nothing;
   1084       int compression_header_size;
   1085       bfd_size_type uncompressed_size;
   1086       unsigned int uncompressed_align_power;
   1087       enum compression_type ch_type = ch_none;
   1088       bool compressed
   1089 	= bfd_is_section_compressed_info (abfd, newsect,
   1090 					  &compression_header_size,
   1091 					  &uncompressed_size,
   1092 					  &uncompressed_align_power,
   1093 					  &ch_type);
   1094 
   1095       /* Should we decompress?  */
   1096       if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
   1097 	action = decompress;
   1098 
   1099       /* Should we compress?  Or convert to a different compression?  */
   1100       else if ((abfd->flags & BFD_COMPRESS) != 0
   1101 	       && newsect->size != 0
   1102 	       && compression_header_size >= 0
   1103 	       && uncompressed_size > 0)
   1104 	{
   1105 	  if (!compressed)
   1106 	    action = compress;
   1107 	  else
   1108 	    {
   1109 	      enum compression_type new_ch_type = ch_none;
   1110 	      if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
   1111 		new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
   1112 			       ? ch_compress_zstd : ch_compress_zlib);
   1113 	      if (new_ch_type != ch_type)
   1114 		action = compress;
   1115 	    }
   1116 	}
   1117 
   1118       if (action == compress)
   1119 	{
   1120 	  if (!bfd_init_section_compress_status (abfd, newsect))
   1121 	    {
   1122 	      _bfd_error_handler
   1123 		/* xgettext:c-format */
   1124 		(_("%pB: unable to compress section %s"), abfd, name);
   1125 	      return false;
   1126 	    }
   1127 	}
   1128       else if (action == decompress)
   1129 	{
   1130 	  if (!bfd_init_section_decompress_status (abfd, newsect))
   1131 	    {
   1132 	      _bfd_error_handler
   1133 		/* xgettext:c-format */
   1134 		(_("%pB: unable to decompress section %s"), abfd, name);
   1135 	      return false;
   1136 	    }
   1137 #ifndef HAVE_ZSTD
   1138 	  if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
   1139 	    {
   1140 	      _bfd_error_handler
   1141 		  /* xgettext:c-format */
   1142 		  (_ ("%pB: section %s is compressed with zstd, but BFD "
   1143 		      "is not built with zstd support"),
   1144 		   abfd, name);
   1145 	      newsect->compress_status = COMPRESS_SECTION_NONE;
   1146 	      return false;
   1147 	    }
   1148 #endif
   1149 	  if (abfd->is_linker_input
   1150 	      && name[1] == 'z')
   1151 	    {
   1152 	      /* Rename section from .zdebug_* to .debug_* so that ld
   1153 		 scripts will see this section as a debug section.  */
   1154 	      char *new_name = bfd_zdebug_name_to_debug (abfd, name);
   1155 	      if (new_name == NULL)
   1156 		return false;
   1157 	      bfd_rename_section (newsect, new_name);
   1158 	    }
   1159 	}
   1160     }
   1161 
   1162   return true;
   1163 }
   1164 
   1165 const char *const bfd_elf_section_type_names[] =
   1166 {
   1167   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
   1168   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
   1169   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
   1170 };
   1171 
   1172 /* ELF relocs are against symbols.  If we are producing relocatable
   1173    output, and the reloc is against an external symbol, and nothing
   1174    has given us any additional addend, the resulting reloc will also
   1175    be against the same symbol.  In such a case, we don't want to
   1176    change anything about the way the reloc is handled, since it will
   1177    all be done at final link time.  Rather than put special case code
   1178    into bfd_perform_relocation, all the reloc types use this howto
   1179    function, or should call this function for relocatable output.  */
   1180 
   1181 bfd_reloc_status_type
   1182 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1183 		       arelent *reloc_entry,
   1184 		       asymbol *symbol,
   1185 		       void *data ATTRIBUTE_UNUSED,
   1186 		       asection *input_section,
   1187 		       bfd *output_bfd,
   1188 		       char **error_message ATTRIBUTE_UNUSED)
   1189 {
   1190   if (output_bfd != NULL
   1191       && (symbol->flags & BSF_SECTION_SYM) == 0
   1192       && (! reloc_entry->howto->partial_inplace
   1193 	  || reloc_entry->addend == 0))
   1194     {
   1195       reloc_entry->address += input_section->output_offset;
   1196       return bfd_reloc_ok;
   1197     }
   1198 
   1199   /* In some cases the relocation should be treated as output section
   1200      relative, as when linking ELF DWARF into PE COFF.  Many ELF
   1201      targets lack section relative relocations and instead use
   1202      ordinary absolute relocations for references between DWARF
   1203      sections.  That is arguably a bug in those targets but it happens
   1204      to work for the usual case of linking to non-loaded ELF debug
   1205      sections with VMAs forced to zero.  PE COFF on the other hand
   1206      doesn't allow a section VMA of zero.  */
   1207   if (output_bfd == NULL
   1208       && !reloc_entry->howto->pc_relative
   1209       && (symbol->section->flags & SEC_DEBUGGING) != 0
   1210       && (input_section->flags & SEC_DEBUGGING) != 0)
   1211     reloc_entry->addend -= symbol->section->output_section->vma;
   1212 
   1213   return bfd_reloc_continue;
   1214 }
   1215 
   1216 /* Returns TRUE if section A matches section B.
   1218    Names, addresses and links may be different, but everything else
   1219    should be the same.  */
   1220 
   1221 static bool
   1222 section_match (const Elf_Internal_Shdr * a,
   1223 	       const Elf_Internal_Shdr * b)
   1224 {
   1225   if (a->sh_type != b->sh_type
   1226       || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
   1227       || a->sh_addralign != b->sh_addralign
   1228       || a->sh_entsize != b->sh_entsize)
   1229     return false;
   1230   if (a->sh_type == SHT_SYMTAB
   1231       || a->sh_type == SHT_STRTAB)
   1232     return true;
   1233   return a->sh_size == b->sh_size;
   1234 }
   1235 
   1236 /* Find a section in OBFD that has the same characteristics
   1237    as IHEADER.  Return the index of this section or SHN_UNDEF if
   1238    none can be found.  Check's section HINT first, as this is likely
   1239    to be the correct section.  */
   1240 
   1241 static unsigned int
   1242 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
   1243 	   const unsigned int hint)
   1244 {
   1245   Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
   1246   unsigned int i;
   1247 
   1248   BFD_ASSERT (iheader != NULL);
   1249 
   1250   /* See PR 20922 for a reproducer of the NULL test.  */
   1251   if (hint < elf_numsections (obfd)
   1252       && oheaders[hint] != NULL
   1253       && section_match (oheaders[hint], iheader))
   1254     return hint;
   1255 
   1256   for (i = 1; i < elf_numsections (obfd); i++)
   1257     {
   1258       Elf_Internal_Shdr * oheader = oheaders[i];
   1259 
   1260       if (oheader == NULL)
   1261 	continue;
   1262       if (section_match (oheader, iheader))
   1263 	/* FIXME: Do we care if there is a potential for
   1264 	   multiple matches ?  */
   1265 	return i;
   1266     }
   1267 
   1268   return SHN_UNDEF;
   1269 }
   1270 
   1271 /* PR 19938: Attempt to set the ELF section header fields of an OS or
   1272    Processor specific section, based upon a matching input section.
   1273    Returns TRUE upon success, FALSE otherwise.  */
   1274 
   1275 static bool
   1276 copy_special_section_fields (const bfd *ibfd,
   1277 			     bfd *obfd,
   1278 			     const Elf_Internal_Shdr *iheader,
   1279 			     Elf_Internal_Shdr *oheader,
   1280 			     const unsigned int secnum)
   1281 {
   1282   elf_backend_data *bed = get_elf_backend_data (obfd);
   1283   const Elf_Internal_Shdr **iheaders
   1284     = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1285   bool changed = false;
   1286   unsigned int sh_link;
   1287 
   1288   if (oheader->sh_type == SHT_NOBITS)
   1289     {
   1290       /* This is a feature for objcopy --only-keep-debug:
   1291 	 When a section's type is changed to NOBITS, we preserve
   1292 	 the sh_link and sh_info fields so that they can be
   1293 	 matched up with the original.
   1294 
   1295 	 Note: Strictly speaking these assignments are wrong.
   1296 	 The sh_link and sh_info fields should point to the
   1297 	 relevent sections in the output BFD, which may not be in
   1298 	 the same location as they were in the input BFD.  But
   1299 	 the whole point of this action is to preserve the
   1300 	 original values of the sh_link and sh_info fields, so
   1301 	 that they can be matched up with the section headers in
   1302 	 the original file.  So strictly speaking we may be
   1303 	 creating an invalid ELF file, but it is only for a file
   1304 	 that just contains debug info and only for sections
   1305 	 without any contents.  */
   1306       if (oheader->sh_link == 0)
   1307 	oheader->sh_link = iheader->sh_link;
   1308       if (oheader->sh_info == 0)
   1309 	oheader->sh_info = iheader->sh_info;
   1310       return true;
   1311     }
   1312 
   1313   /* Allow the target a chance to decide how these fields should be set.  */
   1314   if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1315 						    iheader, oheader))
   1316     return true;
   1317 
   1318   /* We have an iheader which might match oheader, and which has non-zero
   1319      sh_info and/or sh_link fields.  Attempt to follow those links and find
   1320      the section in the output bfd which corresponds to the linked section
   1321      in the input bfd.  */
   1322   if (iheader->sh_link != SHN_UNDEF)
   1323     {
   1324       /* See PR 20931 for a reproducer.  */
   1325       if (iheader->sh_link >= elf_numsections (ibfd))
   1326 	{
   1327 	  _bfd_error_handler
   1328 	    /* xgettext:c-format */
   1329 	    (_("%pB: invalid sh_link field (%d) in section number %d"),
   1330 	     ibfd, iheader->sh_link, secnum);
   1331 	  return false;
   1332 	}
   1333 
   1334       sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
   1335       if (sh_link != SHN_UNDEF)
   1336 	{
   1337 	  oheader->sh_link = sh_link;
   1338 	  changed = true;
   1339 	}
   1340       else
   1341 	/* FIXME: Should we install iheader->sh_link
   1342 	   if we could not find a match ?  */
   1343 	_bfd_error_handler
   1344 	  /* xgettext:c-format */
   1345 	  (_("%pB: failed to find link section for section %d"), obfd, secnum);
   1346     }
   1347 
   1348   if (iheader->sh_info)
   1349     {
   1350       /* The sh_info field can hold arbitrary information, but if the
   1351 	 SHF_LINK_INFO flag is set then it should be interpreted as a
   1352 	 section index.  */
   1353       if (iheader->sh_flags & SHF_INFO_LINK)
   1354 	{
   1355 	  sh_link = find_link (obfd, iheaders[iheader->sh_info],
   1356 			       iheader->sh_info);
   1357 	  if (sh_link != SHN_UNDEF)
   1358 	    oheader->sh_flags |= SHF_INFO_LINK;
   1359 	}
   1360       else
   1361 	/* No idea what it means - just copy it.  */
   1362 	sh_link = iheader->sh_info;
   1363 
   1364       if (sh_link != SHN_UNDEF)
   1365 	{
   1366 	  oheader->sh_info = sh_link;
   1367 	  changed = true;
   1368 	}
   1369       else
   1370 	_bfd_error_handler
   1371 	  /* xgettext:c-format */
   1372 	  (_("%pB: failed to find info section for section %d"), obfd, secnum);
   1373     }
   1374 
   1375   return changed;
   1376 }
   1377 
   1378 /* Copy the program header and other data from one object module to
   1379    another.  */
   1380 
   1381 bool
   1382 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1383 {
   1384   const Elf_Internal_Shdr **iheaders;
   1385   Elf_Internal_Shdr **oheaders;
   1386   elf_backend_data *bed;
   1387   unsigned int i;
   1388 
   1389   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   1390     return true;
   1391 
   1392   if (!elf_flags_init (obfd))
   1393     {
   1394       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   1395       elf_flags_init (obfd) = true;
   1396     }
   1397 
   1398   elf_gp (obfd) = elf_gp (ibfd);
   1399 
   1400   /* Also copy the EI_OSABI field.  */
   1401   elf_elfheader (obfd)->e_ident[EI_OSABI] =
   1402     elf_elfheader (ibfd)->e_ident[EI_OSABI];
   1403 
   1404   /* If set, copy the EI_ABIVERSION field.  */
   1405   if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
   1406     elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
   1407       = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
   1408 
   1409   /* Copy object attributes.  */
   1410   _bfd_elf_copy_obj_attributes (ibfd, obfd);
   1411 
   1412   iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
   1413   oheaders = elf_elfsections (obfd);
   1414   if (iheaders == NULL || oheaders == NULL)
   1415     return true;
   1416 
   1417   bed = get_elf_backend_data (obfd);
   1418 
   1419   /* Possibly copy other fields in the section header.  */
   1420   for (i = 1; i < elf_numsections (obfd); i++)
   1421     {
   1422       unsigned int j;
   1423       Elf_Internal_Shdr * oheader = oheaders[i];
   1424 
   1425       /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
   1426 	 because of a special case need for generating separate debug info
   1427 	 files.  See below for more details.  */
   1428       if (oheader == NULL
   1429 	  || (oheader->sh_type != SHT_NOBITS
   1430 	      && oheader->sh_type < SHT_LOOS))
   1431 	continue;
   1432 
   1433       /* Ignore empty sections, and sections whose
   1434 	 fields have already been initialised.  */
   1435       if (oheader->sh_size == 0
   1436 	  || (oheader->sh_info != 0 && oheader->sh_link != 0))
   1437 	continue;
   1438 
   1439       /* Scan for the matching section in the input bfd.
   1440 	 First we try for a direct mapping between the input and
   1441 	 output sections.  */
   1442       for (j = 1; j < elf_numsections (ibfd); j++)
   1443 	{
   1444 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1445 
   1446 	  if (iheader == NULL)
   1447 	    continue;
   1448 
   1449 	  if (oheader->bfd_section != NULL
   1450 	      && iheader->bfd_section != NULL
   1451 	      && iheader->bfd_section->output_section != NULL
   1452 	      && iheader->bfd_section->output_section == oheader->bfd_section)
   1453 	    {
   1454 	      /* We have found a connection from the input section to
   1455 		 the output section.  Attempt to copy the header fields.
   1456 		 If this fails then do not try any further sections -
   1457 		 there should only be a one-to-one mapping between
   1458 		 input and output.  */
   1459 	      if (!copy_special_section_fields (ibfd, obfd,
   1460 						iheader, oheader, i))
   1461 		j = elf_numsections (ibfd);
   1462 	      break;
   1463 	    }
   1464 	}
   1465 
   1466       if (j < elf_numsections (ibfd))
   1467 	continue;
   1468 
   1469       /* That failed.  So try to deduce the corresponding input section.
   1470 	 Unfortunately we cannot compare names as the output string table
   1471 	 is empty, so instead we check size, address and type.  */
   1472       for (j = 1; j < elf_numsections (ibfd); j++)
   1473 	{
   1474 	  const Elf_Internal_Shdr * iheader = iheaders[j];
   1475 
   1476 	  if (iheader == NULL)
   1477 	    continue;
   1478 
   1479 	  /* Try matching fields in the input section's header.
   1480 	     Since --only-keep-debug turns all non-debug sections into
   1481 	     SHT_NOBITS sections, the output SHT_NOBITS type matches any
   1482 	     input type.  */
   1483 	  if ((oheader->sh_type == SHT_NOBITS
   1484 	       || iheader->sh_type == oheader->sh_type)
   1485 	      && (iheader->sh_flags & ~ SHF_INFO_LINK)
   1486 	      == (oheader->sh_flags & ~ SHF_INFO_LINK)
   1487 	      && iheader->sh_addralign == oheader->sh_addralign
   1488 	      && iheader->sh_entsize == oheader->sh_entsize
   1489 	      && iheader->sh_size == oheader->sh_size
   1490 	      && iheader->sh_addr == oheader->sh_addr
   1491 	      && (iheader->sh_info != oheader->sh_info
   1492 		  || iheader->sh_link != oheader->sh_link))
   1493 	    {
   1494 	      if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
   1495 		break;
   1496 	    }
   1497 	}
   1498 
   1499       if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
   1500 	{
   1501 	  /* Final attempt.  Call the backend copy function
   1502 	     with a NULL input section.  */
   1503 	  (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
   1504 							       NULL, oheader);
   1505 	}
   1506     }
   1507 
   1508   return true;
   1509 }
   1510 
   1511 static const char *
   1512 get_segment_type (unsigned int p_type)
   1513 {
   1514   const char *pt;
   1515   switch (p_type)
   1516     {
   1517     case PT_NULL: pt = "NULL"; break;
   1518     case PT_LOAD: pt = "LOAD"; break;
   1519     case PT_DYNAMIC: pt = "DYNAMIC"; break;
   1520     case PT_INTERP: pt = "INTERP"; break;
   1521     case PT_NOTE: pt = "NOTE"; break;
   1522     case PT_SHLIB: pt = "SHLIB"; break;
   1523     case PT_PHDR: pt = "PHDR"; break;
   1524     case PT_TLS: pt = "TLS"; break;
   1525     case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
   1526     case PT_GNU_STACK: pt = "STACK"; break;
   1527     case PT_GNU_RELRO: pt = "RELRO"; break;
   1528     case PT_GNU_SFRAME: pt = "SFRAME"; break;
   1529     default: pt = NULL; break;
   1530     }
   1531   return pt;
   1532 }
   1533 
   1534 /* Print out the program headers.  */
   1535 
   1536 bool
   1537 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
   1538 {
   1539   FILE *f = (FILE *) farg;
   1540   Elf_Internal_Phdr *p;
   1541   asection *s;
   1542   bfd_byte *dynbuf = NULL;
   1543 
   1544   p = elf_tdata (abfd)->phdr;
   1545   if (p != NULL)
   1546     {
   1547       unsigned int i, c;
   1548 
   1549       fprintf (f, _("\nProgram Header:\n"));
   1550       c = elf_elfheader (abfd)->e_phnum;
   1551       for (i = 0; i < c; i++, p++)
   1552 	{
   1553 	  const char *pt = get_segment_type (p->p_type);
   1554 	  char buf[20];
   1555 
   1556 	  if (pt == NULL)
   1557 	    {
   1558 	      sprintf (buf, "0x%lx", p->p_type);
   1559 	      pt = buf;
   1560 	    }
   1561 	  fprintf (f, "%8s off    0x", pt);
   1562 	  bfd_fprintf_vma (abfd, f, p->p_offset);
   1563 	  fprintf (f, " vaddr 0x");
   1564 	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
   1565 	  fprintf (f, " paddr 0x");
   1566 	  bfd_fprintf_vma (abfd, f, p->p_paddr);
   1567 	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
   1568 	  fprintf (f, "         filesz 0x");
   1569 	  bfd_fprintf_vma (abfd, f, p->p_filesz);
   1570 	  fprintf (f, " memsz 0x");
   1571 	  bfd_fprintf_vma (abfd, f, p->p_memsz);
   1572 	  fprintf (f, " flags %c%c%c",
   1573 		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
   1574 		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
   1575 		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
   1576 	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
   1577 	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
   1578 	  fprintf (f, "\n");
   1579 	}
   1580     }
   1581 
   1582   s = bfd_get_section_by_name (abfd, ".dynamic");
   1583   if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
   1584     {
   1585       unsigned int elfsec;
   1586       unsigned long shlink;
   1587       bfd_byte *extdyn, *extdynend;
   1588       size_t extdynsize;
   1589       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1590 
   1591       fprintf (f, _("\nDynamic Section:\n"));
   1592 
   1593       if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
   1594 	goto error_return;
   1595 
   1596       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
   1597       if (elfsec == SHN_BAD)
   1598 	goto error_return;
   1599       shlink = elf_elfsections (abfd)[elfsec]->sh_link;
   1600 
   1601       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1602       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1603 
   1604       for (extdyn = dynbuf, extdynend = dynbuf + s->size;
   1605 	   (size_t) (extdynend - extdyn) >= extdynsize;
   1606 	   extdyn += extdynsize)
   1607 	{
   1608 	  Elf_Internal_Dyn dyn;
   1609 	  const char *name = "";
   1610 	  char ab[20];
   1611 	  bool stringp;
   1612 	  elf_backend_data *bed = get_elf_backend_data (abfd);
   1613 
   1614 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1615 
   1616 	  if (dyn.d_tag == DT_NULL)
   1617 	    break;
   1618 
   1619 	  stringp = false;
   1620 	  switch (dyn.d_tag)
   1621 	    {
   1622 	    default:
   1623 	      if (bed->elf_backend_get_target_dtag)
   1624 		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
   1625 
   1626 	      if (streq (name, ""))
   1627 		{
   1628 		  sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
   1629 		  name = ab;
   1630 		}
   1631 	      break;
   1632 
   1633 	    case DT_NEEDED: name = "NEEDED"; stringp = true; break;
   1634 	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
   1635 	    case DT_PLTGOT: name = "PLTGOT"; break;
   1636 	    case DT_HASH: name = "HASH"; break;
   1637 	    case DT_STRTAB: name = "STRTAB"; break;
   1638 	    case DT_SYMTAB: name = "SYMTAB"; break;
   1639 	    case DT_RELA: name = "RELA"; break;
   1640 	    case DT_RELASZ: name = "RELASZ"; break;
   1641 	    case DT_RELAENT: name = "RELAENT"; break;
   1642 	    case DT_STRSZ: name = "STRSZ"; break;
   1643 	    case DT_SYMENT: name = "SYMENT"; break;
   1644 	    case DT_INIT: name = "INIT"; break;
   1645 	    case DT_FINI: name = "FINI"; break;
   1646 	    case DT_SONAME: name = "SONAME"; stringp = true; break;
   1647 	    case DT_RPATH: name = "RPATH"; stringp = true; break;
   1648 	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
   1649 	    case DT_REL: name = "REL"; break;
   1650 	    case DT_RELSZ: name = "RELSZ"; break;
   1651 	    case DT_RELENT: name = "RELENT"; break;
   1652 	    case DT_RELR: name = "RELR"; break;
   1653 	    case DT_RELRSZ: name = "RELRSZ"; break;
   1654 	    case DT_RELRENT: name = "RELRENT"; break;
   1655 	    case DT_PLTREL: name = "PLTREL"; break;
   1656 	    case DT_DEBUG: name = "DEBUG"; break;
   1657 	    case DT_TEXTREL: name = "TEXTREL"; break;
   1658 	    case DT_JMPREL: name = "JMPREL"; break;
   1659 	    case DT_BIND_NOW: name = "BIND_NOW"; break;
   1660 	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
   1661 	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
   1662 	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
   1663 	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
   1664 	    case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
   1665 	    case DT_FLAGS: name = "FLAGS"; break;
   1666 	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
   1667 	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
   1668 	    case DT_CHECKSUM: name = "CHECKSUM"; break;
   1669 	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
   1670 	    case DT_MOVEENT: name = "MOVEENT"; break;
   1671 	    case DT_MOVESZ: name = "MOVESZ"; break;
   1672 	    case DT_FEATURE: name = "FEATURE"; break;
   1673 	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
   1674 	    case DT_SYMINSZ: name = "SYMINSZ"; break;
   1675 	    case DT_SYMINENT: name = "SYMINENT"; break;
   1676 	    case DT_CONFIG: name = "CONFIG"; stringp = true; break;
   1677 	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
   1678 	    case DT_AUDIT: name = "AUDIT"; stringp = true; break;
   1679 	    case DT_PLTPAD: name = "PLTPAD"; break;
   1680 	    case DT_MOVETAB: name = "MOVETAB"; break;
   1681 	    case DT_SYMINFO: name = "SYMINFO"; break;
   1682 	    case DT_RELACOUNT: name = "RELACOUNT"; break;
   1683 	    case DT_RELCOUNT: name = "RELCOUNT"; break;
   1684 	    case DT_FLAGS_1: name = "FLAGS_1"; break;
   1685 	    case DT_VERSYM: name = "VERSYM"; break;
   1686 	    case DT_VERDEF: name = "VERDEF"; break;
   1687 	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
   1688 	    case DT_VERNEED: name = "VERNEED"; break;
   1689 	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
   1690 	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
   1691 	    case DT_USED: name = "USED"; break;
   1692 	    case DT_FILTER: name = "FILTER"; stringp = true; break;
   1693 	    case DT_GNU_HASH: name = "GNU_HASH"; break;
   1694 	    }
   1695 
   1696 	  fprintf (f, "  %-20s ", name);
   1697 	  if (! stringp)
   1698 	    {
   1699 	      fprintf (f, "0x");
   1700 	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
   1701 	    }
   1702 	  else
   1703 	    {
   1704 	      const char *string;
   1705 	      unsigned int tagv = dyn.d_un.d_val;
   1706 
   1707 	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
   1708 	      if (string == NULL)
   1709 		goto error_return;
   1710 	      fprintf (f, "%s", string);
   1711 	    }
   1712 	  fprintf (f, "\n");
   1713 	}
   1714 
   1715       _bfd_elf_munmap_section_contents (s, dynbuf);
   1716       dynbuf = NULL;
   1717     }
   1718 
   1719   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
   1720       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
   1721     {
   1722       if (! _bfd_elf_slurp_version_tables (abfd, false))
   1723 	return false;
   1724     }
   1725 
   1726   if (elf_dynverdef (abfd) != 0)
   1727     {
   1728       Elf_Internal_Verdef *t;
   1729 
   1730       fprintf (f, _("\nVersion definitions:\n"));
   1731       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
   1732 	{
   1733 	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
   1734 		   t->vd_flags, t->vd_hash,
   1735 		   t->vd_nodename ? t->vd_nodename : "<corrupt>");
   1736 	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
   1737 	    {
   1738 	      Elf_Internal_Verdaux *a;
   1739 
   1740 	      fprintf (f, "\t");
   1741 	      for (a = t->vd_auxptr->vda_nextptr;
   1742 		   a != NULL;
   1743 		   a = a->vda_nextptr)
   1744 		fprintf (f, "%s ",
   1745 			 a->vda_nodename ? a->vda_nodename : "<corrupt>");
   1746 	      fprintf (f, "\n");
   1747 	    }
   1748 	}
   1749     }
   1750 
   1751   if (elf_dynverref (abfd) != 0)
   1752     {
   1753       Elf_Internal_Verneed *t;
   1754 
   1755       fprintf (f, _("\nVersion References:\n"));
   1756       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
   1757 	{
   1758 	  Elf_Internal_Vernaux *a;
   1759 
   1760 	  fprintf (f, _("  required from %s:\n"),
   1761 		   t->vn_filename ? t->vn_filename : "<corrupt>");
   1762 	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   1763 	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
   1764 		     a->vna_flags, a->vna_other,
   1765 		     a->vna_nodename ? a->vna_nodename : "<corrupt>");
   1766 	}
   1767     }
   1768 
   1769   return true;
   1770 
   1771  error_return:
   1772   _bfd_elf_munmap_section_contents (s, dynbuf);
   1773   return false;
   1774 }
   1775 
   1776 /* Find the file offset corresponding to VMA by using the program
   1777    headers.  */
   1778 
   1779 static file_ptr
   1780 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
   1781 		 size_t size, size_t *max_size_p)
   1782 {
   1783   Elf_Internal_Phdr *seg;
   1784   size_t i;
   1785 
   1786   for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
   1787     if (seg->p_type == PT_LOAD
   1788 	&& vma >= (seg->p_vaddr & -seg->p_align)
   1789 	&& vma + size <= seg->p_vaddr + seg->p_filesz)
   1790       {
   1791 	if (max_size_p)
   1792 	  *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
   1793 	return vma - seg->p_vaddr + seg->p_offset;
   1794       }
   1795 
   1796   if (max_size_p)
   1797     *max_size_p = 0;
   1798   bfd_set_error (bfd_error_invalid_operation);
   1799   return (file_ptr) -1;
   1800 }
   1801 
   1802 /* Convert hash table to internal form.  */
   1803 
   1804 static bfd_vma *
   1805 get_hash_table_data (bfd *abfd, bfd_size_type number,
   1806 		     unsigned int ent_size, bfd_size_type filesize)
   1807 {
   1808   unsigned char *e_data = NULL;
   1809   bfd_vma *i_data = NULL;
   1810   bfd_size_type size;
   1811   void *e_data_addr;
   1812   size_t e_data_size ATTRIBUTE_UNUSED;
   1813 
   1814   if (ent_size != 4 && ent_size != 8)
   1815     return NULL;
   1816 
   1817   if ((size_t) number != number)
   1818     {
   1819       bfd_set_error (bfd_error_file_too_big);
   1820       return NULL;
   1821     }
   1822 
   1823   size = ent_size * number;
   1824   /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
   1825      attempting to allocate memory when the read is bound to fail.  */
   1826   if (size > filesize
   1827       || number >= ~(size_t) 0 / ent_size
   1828       || number >= ~(size_t) 0 / sizeof (*i_data))
   1829     {
   1830       bfd_set_error (bfd_error_file_too_big);
   1831       return NULL;
   1832     }
   1833 
   1834   e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
   1835   if (e_data == NULL)
   1836     return NULL;
   1837 
   1838   i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
   1839   if (i_data == NULL)
   1840     {
   1841       _bfd_munmap_temporary (e_data_addr, e_data_size);
   1842       return NULL;
   1843     }
   1844 
   1845   if (ent_size == 4)
   1846     while (number--)
   1847       i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
   1848   else
   1849     while (number--)
   1850       i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
   1851 
   1852   _bfd_munmap_temporary (e_data_addr, e_data_size);
   1853   return i_data;
   1854 }
   1855 
   1856 /* Address of .MIPS.xhash section.  FIXME: What is the best way to
   1857    support DT_MIPS_XHASH?  */
   1858 #define DT_MIPS_XHASH	       0x70000036
   1859 
   1860 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
   1861 
   1862 bool
   1863 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
   1864 			      Elf_Internal_Phdr *phdrs, size_t phnum,
   1865 			      bfd_size_type filesize)
   1866 {
   1867   bfd_byte *extdyn, *extdynend;
   1868   size_t extdynsize;
   1869   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1870   bool (*swap_symbol_in) (bfd *, const void *, const void *,
   1871 			  Elf_Internal_Sym *);
   1872   Elf_Internal_Dyn dyn;
   1873   bfd_vma dt_hash = 0;
   1874   bfd_vma dt_gnu_hash = 0;
   1875   bfd_vma dt_mips_xhash = 0;
   1876   bfd_vma dt_strtab = 0;
   1877   bfd_vma dt_symtab = 0;
   1878   size_t dt_strsz = 0;
   1879   bfd_vma dt_versym = 0;
   1880   bfd_vma dt_verdef = 0;
   1881   bfd_vma dt_verneed = 0;
   1882   bfd_byte *dynbuf = NULL;
   1883   char *strbuf = NULL;
   1884   bfd_vma *gnubuckets = NULL;
   1885   bfd_vma *gnuchains = NULL;
   1886   bfd_vma *mipsxlat = NULL;
   1887   file_ptr saved_filepos, filepos;
   1888   bool res = false;
   1889   size_t amt;
   1890   bfd_byte *esymbuf = NULL, *esym;
   1891   bfd_size_type symcount;
   1892   Elf_Internal_Sym *isymbuf = NULL;
   1893   Elf_Internal_Sym *isym, *isymend;
   1894   bfd_byte *versym = NULL;
   1895   bfd_byte *verdef = NULL;
   1896   bfd_byte *verneed = NULL;
   1897   size_t verdef_size = 0;
   1898   size_t verneed_size = 0;
   1899   size_t extsym_size;
   1900   elf_backend_data *bed;
   1901   void *dynbuf_addr = NULL;
   1902   void *esymbuf_addr = NULL;
   1903   size_t dynbuf_size = 0;
   1904   size_t esymbuf_size = 0;
   1905 
   1906   /* Return TRUE if symbol table is bad.  */
   1907   if (elf_bad_symtab (abfd))
   1908     return true;
   1909 
   1910   /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
   1911   if (elf_tdata (abfd)->dt_strtab != NULL)
   1912     return true;
   1913 
   1914   bed = get_elf_backend_data (abfd);
   1915 
   1916   /* Save file position for elf_object_p.  */
   1917   saved_filepos = bfd_tell (abfd);
   1918 
   1919   if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
   1920     goto error_return;
   1921 
   1922   dynbuf_size = phdr->p_filesz;
   1923   dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
   1924   if (dynbuf == NULL)
   1925     goto error_return;
   1926 
   1927   extsym_size = bed->s->sizeof_sym;
   1928   extdynsize = bed->s->sizeof_dyn;
   1929   swap_dyn_in = bed->s->swap_dyn_in;
   1930 
   1931   extdyn = dynbuf;
   1932   if (phdr->p_filesz < extdynsize)
   1933     goto error_return;
   1934   extdynend = extdyn + phdr->p_filesz;
   1935   for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
   1936     {
   1937       swap_dyn_in (abfd, extdyn, &dyn);
   1938 
   1939       if (dyn.d_tag == DT_NULL)
   1940 	break;
   1941 
   1942       switch (dyn.d_tag)
   1943 	{
   1944 	case DT_HASH:
   1945 	  dt_hash = dyn.d_un.d_val;
   1946 	  break;
   1947 	case DT_GNU_HASH:
   1948 	  if (bed->elf_machine_code != EM_MIPS
   1949 	      && bed->elf_machine_code != EM_MIPS_RS3_LE)
   1950 	    dt_gnu_hash = dyn.d_un.d_val;
   1951 	  break;
   1952 	case DT_STRTAB:
   1953 	  dt_strtab = dyn.d_un.d_val;
   1954 	  break;
   1955 	case DT_SYMTAB:
   1956 	  dt_symtab = dyn.d_un.d_val;
   1957 	  break;
   1958 	case DT_STRSZ:
   1959 	  dt_strsz = dyn.d_un.d_val;
   1960 	  break;
   1961 	case DT_SYMENT:
   1962 	  if (dyn.d_un.d_val != extsym_size)
   1963 	    goto error_return;
   1964 	  break;
   1965 	case DT_VERSYM:
   1966 	  dt_versym = dyn.d_un.d_val;
   1967 	  break;
   1968 	case DT_VERDEF:
   1969 	  dt_verdef = dyn.d_un.d_val;
   1970 	  break;
   1971 	case DT_VERNEED:
   1972 	  dt_verneed = dyn.d_un.d_val;
   1973 	  break;
   1974 	default:
   1975 	  if (dyn.d_tag == DT_MIPS_XHASH
   1976 	      && (bed->elf_machine_code == EM_MIPS
   1977 		  || bed->elf_machine_code == EM_MIPS_RS3_LE))
   1978 	    {
   1979 	      dt_gnu_hash = dyn.d_un.d_val;
   1980 	      dt_mips_xhash = dyn.d_un.d_val;
   1981 	    }
   1982 	  break;
   1983 	}
   1984     }
   1985 
   1986   /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
   1987      segment.  */
   1988   if ((!dt_hash && !dt_gnu_hash)
   1989       || !dt_strtab
   1990       || !dt_symtab
   1991       || !dt_strsz)
   1992     goto error_return;
   1993 
   1994   /* Get dynamic string table.  */
   1995   filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
   1996   if (filepos == (file_ptr) -1
   1997       || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   1998     goto error_return;
   1999 
   2000   /* Dynamic string table must be valid until ABFD is closed.  */
   2001   strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
   2002   if (strbuf == NULL)
   2003     goto error_return;
   2004   if (strbuf[dt_strsz - 1] != 0)
   2005     {
   2006       /* It is an error if a string table is't terminated.  */
   2007       _bfd_error_handler
   2008 	/* xgettext:c-format */
   2009 	(_("%pB: DT_STRTAB table is corrupt"), abfd);
   2010       strbuf[dt_strsz - 1] = 0;
   2011     }
   2012 
   2013   /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
   2014      DT_HASH since it is simpler than DT_GNU_HASH.  */
   2015   if (dt_hash)
   2016     {
   2017       unsigned char nb[16];
   2018       unsigned int hash_ent_size;
   2019 
   2020       switch (bed->elf_machine_code)
   2021 	{
   2022 	case EM_ALPHA:
   2023 	case EM_S390:
   2024 	case EM_S390_OLD:
   2025 	  if (bed->s->elfclass == ELFCLASS64)
   2026 	    {
   2027 	      hash_ent_size = 8;
   2028 	      break;
   2029 	    }
   2030 	  /* FALLTHROUGH */
   2031 	default:
   2032 	  hash_ent_size = 4;
   2033 	  break;
   2034 	}
   2035 
   2036       filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
   2037 				 NULL);
   2038       if (filepos == (file_ptr) -1
   2039 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0
   2040 	  || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
   2041 	goto error_return;
   2042 
   2043       /* The number of dynamic symbol table entries equals the number
   2044 	 of chains.  */
   2045       if (hash_ent_size == 8)
   2046 	symcount = bfd_get_64 (abfd, nb + hash_ent_size);
   2047       else
   2048 	symcount = bfd_get_32 (abfd, nb + hash_ent_size);
   2049     }
   2050   else
   2051     {
   2052       /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
   2053 	 bindings are in hash table.  Since in dynamic symbol table,
   2054 	 all symbols with STB_LOCAL binding are placed before symbols
   2055 	 with other bindings and all undefined symbols are placed
   2056 	 before defined ones, the highest symbol index in DT_GNU_HASH
   2057 	 is the highest dynamic symbol table index.  */
   2058       unsigned char nb[16];
   2059       bfd_vma ngnubuckets;
   2060       bfd_vma gnusymidx;
   2061       size_t i, ngnuchains;
   2062       bfd_vma maxchain = 0xffffffff, bitmaskwords;
   2063       bfd_vma buckets_vma;
   2064 
   2065       filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
   2066 				 sizeof (nb), NULL);
   2067       if (filepos == (file_ptr) -1
   2068 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0
   2069 	  || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
   2070 	goto error_return;
   2071 
   2072       ngnubuckets = bfd_get_32 (abfd, nb);
   2073       gnusymidx = bfd_get_32 (abfd, nb + 4);
   2074       bitmaskwords = bfd_get_32 (abfd, nb + 8);
   2075       buckets_vma = dt_gnu_hash + 16;
   2076       if (bed->s->elfclass == ELFCLASS32)
   2077 	buckets_vma += bitmaskwords * 4;
   2078       else
   2079 	buckets_vma += bitmaskwords * 8;
   2080       filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
   2081       if (filepos == (file_ptr) -1
   2082 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2083 	goto error_return;
   2084 
   2085       gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
   2086       if (gnubuckets == NULL)
   2087 	goto error_return;
   2088 
   2089       for (i = 0; i < ngnubuckets; i++)
   2090 	if (gnubuckets[i] != 0)
   2091 	  {
   2092 	    if (gnubuckets[i] < gnusymidx)
   2093 	      goto error_return;
   2094 
   2095 	    if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
   2096 	      maxchain = gnubuckets[i];
   2097 	  }
   2098 
   2099       if (maxchain == 0xffffffff)
   2100 	{
   2101 	  symcount = 0;
   2102 	  goto empty_gnu_hash;
   2103 	}
   2104 
   2105       maxchain -= gnusymidx;
   2106       filepos = offset_from_vma (phdrs, phnum,
   2107 				 buckets_vma + 4 * (ngnubuckets + maxchain),
   2108 				 4, NULL);
   2109       if (filepos == (file_ptr) -1
   2110 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2111 	goto error_return;
   2112 
   2113       do
   2114 	{
   2115 	  if (bfd_read (nb, 4, abfd) != 4)
   2116 	    goto error_return;
   2117 	  ++maxchain;
   2118 	  if (maxchain == 0)
   2119 	    goto error_return;
   2120 	}
   2121       while ((bfd_get_32 (abfd, nb) & 1) == 0);
   2122 
   2123       filepos = offset_from_vma (phdrs, phnum,
   2124 				 buckets_vma + 4 * ngnubuckets,
   2125 				 4, NULL);
   2126       if (filepos == (file_ptr) -1
   2127 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2128 	goto error_return;
   2129 
   2130       gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
   2131       if (gnuchains == NULL)
   2132 	goto error_return;
   2133       ngnuchains = maxchain;
   2134 
   2135       if (dt_mips_xhash)
   2136 	{
   2137 	  filepos = offset_from_vma (phdrs, phnum,
   2138 				     buckets_vma + 4 * (ngnubuckets + maxchain),
   2139 				     4, NULL);
   2140 	  if (filepos == (file_ptr) -1
   2141 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2142 	    goto error_return;
   2143 
   2144 	  mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
   2145 	  if (mipsxlat == NULL)
   2146 	    goto error_return;
   2147 	}
   2148 
   2149       symcount = 0;
   2150       for (i = 0; i < ngnubuckets; ++i)
   2151 	if (gnubuckets[i] != 0)
   2152 	  {
   2153 	    bfd_vma si = gnubuckets[i];
   2154 	    bfd_vma off = si - gnusymidx;
   2155 	    do
   2156 	      {
   2157 		if (mipsxlat)
   2158 		  {
   2159 		    if (mipsxlat[off] >= symcount)
   2160 		      symcount = mipsxlat[off] + 1;
   2161 		  }
   2162 		else
   2163 		  {
   2164 		    if (si >= symcount)
   2165 		      symcount = si + 1;
   2166 		  }
   2167 		si++;
   2168 	      }
   2169 	    while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
   2170 	  }
   2171     }
   2172 
   2173   /* Swap in dynamic symbol table.  */
   2174   if (_bfd_mul_overflow (symcount, extsym_size, &amt))
   2175     {
   2176       bfd_set_error (bfd_error_file_too_big);
   2177       goto error_return;
   2178     }
   2179 
   2180   filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
   2181   if (filepos == (file_ptr) -1
   2182       || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2183     goto error_return;
   2184   esymbuf_size = amt;
   2185   esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
   2186 				 &esymbuf_addr, &esymbuf_size);
   2187   if (esymbuf == NULL)
   2188     goto error_return;
   2189 
   2190   if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
   2191     {
   2192       bfd_set_error (bfd_error_file_too_big);
   2193       goto error_return;
   2194     }
   2195 
   2196   /* Dynamic symbol table must be valid until ABFD is closed.  */
   2197   isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
   2198   if (isymbuf == NULL)
   2199     goto error_return;
   2200 
   2201   swap_symbol_in = bed->s->swap_symbol_in;
   2202 
   2203   /* Convert the symbols to internal form.  */
   2204   isymend = isymbuf + symcount;
   2205   for (esym = esymbuf, isym = isymbuf;
   2206        isym < isymend;
   2207        esym += extsym_size, isym++)
   2208     if (!swap_symbol_in (abfd, esym, NULL, isym)
   2209 	|| isym->st_name >= dt_strsz)
   2210       {
   2211 	bfd_set_error (bfd_error_invalid_operation);
   2212 	goto error_return;
   2213       }
   2214 
   2215   if (dt_versym)
   2216     {
   2217       /* Swap in DT_VERSYM.  */
   2218       if (_bfd_mul_overflow (symcount, 2, &amt))
   2219 	{
   2220 	  bfd_set_error (bfd_error_file_too_big);
   2221 	  goto error_return;
   2222 	}
   2223 
   2224       filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
   2225       if (filepos == (file_ptr) -1
   2226 	  || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2227 	goto error_return;
   2228 
   2229       /* DT_VERSYM info must be valid until ABFD is closed.  */
   2230       versym = _bfd_mmap_persistent (abfd, amt);
   2231 
   2232       if (dt_verdef)
   2233 	{
   2234 	  /* Read in DT_VERDEF.  */
   2235 	  filepos = offset_from_vma (phdrs, phnum, dt_verdef,
   2236 				     0, &verdef_size);
   2237 	  if (filepos == (file_ptr) -1
   2238 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2239 	    goto error_return;
   2240 
   2241 	  /* DT_VERDEF info must be valid until ABFD is closed.  */
   2242 	  verdef = _bfd_mmap_persistent (abfd, verdef_size);
   2243 	}
   2244 
   2245       if (dt_verneed)
   2246 	{
   2247 	  /* Read in DT_VERNEED.  */
   2248 	  filepos = offset_from_vma (phdrs, phnum, dt_verneed,
   2249 				     0, &verneed_size);
   2250 	  if (filepos == (file_ptr) -1
   2251 	      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
   2252 	    goto error_return;
   2253 
   2254 	  /* DT_VERNEED info must be valid until ABFD is closed.  */
   2255 	  verneed = _bfd_mmap_persistent (abfd, verneed_size);
   2256 	}
   2257     }
   2258 
   2259  empty_gnu_hash:
   2260   elf_tdata (abfd)->dt_strtab = strbuf;
   2261   elf_tdata (abfd)->dt_strsz = dt_strsz;
   2262   elf_tdata (abfd)->dt_symtab = isymbuf;
   2263   elf_tdata (abfd)->dt_symtab_count = symcount;
   2264   elf_tdata (abfd)->dt_versym = versym;
   2265   elf_tdata (abfd)->dt_verdef = verdef;
   2266   elf_tdata (abfd)->dt_verneed = verneed;
   2267   elf_tdata (abfd)->dt_verdef_count
   2268     = verdef_size / sizeof (Elf_External_Verdef);
   2269   elf_tdata (abfd)->dt_verneed_count
   2270     = verneed_size / sizeof (Elf_External_Verneed);
   2271 
   2272   res = true;
   2273 
   2274  error_return:
   2275   /* Restore file position for elf_object_p.  */
   2276   if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
   2277     res = false;
   2278   _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
   2279   _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
   2280   free (gnubuckets);
   2281   free (gnuchains);
   2282   free (mipsxlat);
   2283   return res;
   2284 }
   2285 
   2286 /* Reconstruct section from dynamic symbol.  */
   2287 
   2288 asection *
   2289 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
   2290 					  Elf_Internal_Sym *isym)
   2291 {
   2292   asection *sec;
   2293   flagword flags;
   2294 
   2295   if (!elf_use_dt_symtab_p (abfd))
   2296     return NULL;
   2297 
   2298   flags = SEC_ALLOC | SEC_LOAD;
   2299   switch (ELF_ST_TYPE (isym->st_info))
   2300     {
   2301     case STT_FUNC:
   2302     case STT_GNU_IFUNC:
   2303       sec = bfd_get_section_by_name (abfd, ".text");
   2304       if (sec == NULL)
   2305 	sec = bfd_make_section_with_flags (abfd,
   2306 					   ".text",
   2307 					   flags | SEC_CODE);
   2308       break;
   2309     case STT_COMMON:
   2310       sec = bfd_com_section_ptr;
   2311       break;
   2312     case STT_OBJECT:
   2313       sec = bfd_get_section_by_name (abfd, ".data");
   2314       if (sec == NULL)
   2315 	sec = bfd_make_section_with_flags (abfd,
   2316 					   ".data",
   2317 					   flags | SEC_DATA);
   2318       break;
   2319     case STT_TLS:
   2320       sec = bfd_get_section_by_name (abfd, ".tdata");
   2321       if (sec == NULL)
   2322 	sec = bfd_make_section_with_flags (abfd,
   2323 					   ".tdata",
   2324 					   (flags
   2325 					    | SEC_DATA
   2326 					    | SEC_THREAD_LOCAL));
   2327       break;
   2328     default:
   2329       sec = bfd_abs_section_ptr;
   2330       break;
   2331     }
   2332 
   2333   return sec;
   2334 }
   2335 
   2336 /* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
   2337    and return symbol version for symbol version itself.   */
   2338 
   2339 const char *
   2340 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
   2341 				    bool base_p,
   2342 				    bool *hidden)
   2343 {
   2344   const char *version_string = NULL;
   2345   if ((elf_dynversym (abfd) != 0
   2346        && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
   2347       || (elf_tdata (abfd)->dt_versym != NULL
   2348 	  && (elf_tdata (abfd)->dt_verdef != NULL
   2349 	      || elf_tdata (abfd)->dt_verneed != NULL)))
   2350     {
   2351       unsigned int vernum = ((elf_symbol_type *) symbol)->version;
   2352 
   2353       *hidden = (vernum & VERSYM_HIDDEN) != 0;
   2354       vernum &= VERSYM_VERSION;
   2355 
   2356       if (vernum == 0)
   2357 	version_string = "";
   2358       else if (vernum == 1
   2359 	       && (vernum > elf_tdata (abfd)->cverdefs
   2360 		   || (elf_tdata (abfd)->verdef[0].vd_flags
   2361 		       == VER_FLG_BASE)))
   2362 	version_string = base_p ? "Base" : "";
   2363       else if (vernum <= elf_tdata (abfd)->cverdefs)
   2364 	{
   2365 	  const char *nodename
   2366 	    = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
   2367 	  version_string = "";
   2368 	  if (base_p
   2369 	      || nodename == NULL
   2370 	      || symbol->name == NULL
   2371 	      || ! streq (symbol->name, nodename))
   2372 	    version_string = nodename;
   2373 	}
   2374       else
   2375 	{
   2376 	  Elf_Internal_Verneed *t;
   2377 
   2378 	  version_string = _("<corrupt>");
   2379 	  for (t = elf_tdata (abfd)->verref;
   2380 	       t != NULL;
   2381 	       t = t->vn_nextref)
   2382 	    {
   2383 	      Elf_Internal_Vernaux *a;
   2384 
   2385 	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
   2386 		{
   2387 		  if (a->vna_other == vernum)
   2388 		    {
   2389 		      *hidden = true;
   2390 		      version_string = a->vna_nodename;
   2391 		      break;
   2392 		    }
   2393 		}
   2394 	    }
   2395 	}
   2396     }
   2397   return version_string;
   2398 }
   2399 
   2400 /* Display ELF-specific fields of a symbol.  */
   2401 
   2402 void
   2403 _bfd_elf_print_symbol (bfd *abfd,
   2404 		       void *filep,
   2405 		       asymbol *symbol,
   2406 		       bfd_print_symbol_type how)
   2407 {
   2408   FILE *file = (FILE *) filep;
   2409   const char *symname = (symbol->name != bfd_symbol_error_name
   2410 			 ? symbol->name : _("<corrupt>"));
   2411 
   2412   switch (how)
   2413     {
   2414     case bfd_print_symbol_name:
   2415       fprintf (file, "%s", symname);
   2416       break;
   2417     case bfd_print_symbol_more:
   2418       fprintf (file, "elf ");
   2419       bfd_fprintf_vma (abfd, file, symbol->value);
   2420       fprintf (file, " %x", symbol->flags);
   2421       break;
   2422     case bfd_print_symbol_all:
   2423       {
   2424 	const char *section_name;
   2425 	const char *name = NULL;
   2426 	elf_backend_data *bed;
   2427 	unsigned char st_other;
   2428 	bfd_vma val;
   2429 	const char *version_string;
   2430 	bool hidden;
   2431 
   2432 	section_name = symbol->section ? symbol->section->name : "(*none*)";
   2433 
   2434 	bed = get_elf_backend_data (abfd);
   2435 	if (bed->elf_backend_print_symbol_all)
   2436 	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
   2437 
   2438 	if (name != NULL)
   2439 	  symname = name;
   2440 	else
   2441 	  bfd_print_symbol_vandf (abfd, file, symbol);
   2442 
   2443 	fprintf (file, " %s\t", section_name);
   2444 	/* Print the "other" value for a symbol.  For common symbols,
   2445 	   we've already printed the size; now print the alignment.
   2446 	   For other symbols, we have no specified alignment, and
   2447 	   we've printed the address; now print the size.  */
   2448 	if (symbol->section && bfd_is_com_section (symbol->section))
   2449 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
   2450 	else
   2451 	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
   2452 	bfd_fprintf_vma (abfd, file, val);
   2453 
   2454 	/* If we have version information, print it.  */
   2455 	version_string = _bfd_elf_get_symbol_version_string (abfd,
   2456 							     symbol,
   2457 							     true,
   2458 							     &hidden);
   2459 	if (version_string)
   2460 	  {
   2461 	    if (!hidden)
   2462 	      fprintf (file, "  %-11s", version_string);
   2463 	    else
   2464 	      {
   2465 		int i;
   2466 
   2467 		fprintf (file, " (%s)", version_string);
   2468 		for (i = 10 - strlen (version_string); i > 0; --i)
   2469 		  putc (' ', file);
   2470 	      }
   2471 	  }
   2472 
   2473 	/* If the st_other field is not zero, print it.  */
   2474 	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
   2475 
   2476 	switch (st_other)
   2477 	  {
   2478 	  case 0: break;
   2479 	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
   2480 	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
   2481 	  case STV_PROTECTED: fprintf (file, " .protected"); break;
   2482 	  default:
   2483 	    /* Some other non-defined flags are also present, so print
   2484 	       everything hex.  */
   2485 	    fprintf (file, " 0x%02x", (unsigned int) st_other);
   2486 	  }
   2487 
   2488 	fprintf (file, " %s", symname);
   2489       }
   2490       break;
   2491     }
   2492 }
   2493 
   2494 /* ELF .o/exec file reading */
   2496 
   2497 /* Create a new bfd section from an ELF section header.  */
   2498 
   2499 bool
   2500 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   2501 {
   2502   Elf_Internal_Shdr *hdr;
   2503   Elf_Internal_Ehdr *ehdr;
   2504   elf_backend_data *bed;
   2505   const char *name;
   2506   bool ret = true;
   2507 
   2508   if (shindex >= elf_numsections (abfd))
   2509     return false;
   2510 
   2511   /* PR17512: A corrupt ELF binary might contain a loop of sections via
   2512      sh_link or sh_info.  Detect this here, by refusing to load a
   2513      section that we are already in the process of loading.  */
   2514   if (elf_tdata (abfd)->being_created[shindex])
   2515     {
   2516       _bfd_error_handler
   2517 	(_("%pB: warning: loop in section dependencies detected"), abfd);
   2518       return false;
   2519     }
   2520   elf_tdata (abfd)->being_created[shindex] = true;
   2521 
   2522   hdr = elf_elfsections (abfd)[shindex];
   2523   ehdr = elf_elfheader (abfd);
   2524   name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
   2525 					  hdr->sh_name);
   2526   if (name == NULL)
   2527     goto fail;
   2528 
   2529   bed = get_elf_backend_data (abfd);
   2530   switch (hdr->sh_type)
   2531     {
   2532     case SHT_NULL:
   2533       /* Inactive section. Throw it away.  */
   2534       goto success;
   2535 
   2536     case SHT_PROGBITS:		/* Normal section with contents.  */
   2537     case SHT_NOBITS:		/* .bss section.  */
   2538     case SHT_HASH:		/* .hash section.  */
   2539     case SHT_NOTE:		/* .note section.  */
   2540     case SHT_INIT_ARRAY:	/* .init_array section.  */
   2541     case SHT_FINI_ARRAY:	/* .fini_array section.  */
   2542     case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
   2543     case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
   2544     case SHT_GNU_HASH:		/* .gnu.hash section.  */
   2545     case SHT_GNU_SFRAME:	/* .sframe section.  */
   2546       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2547       goto success;
   2548 
   2549     case SHT_DYNAMIC:	/* Dynamic linking information.  */
   2550       if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2551 	goto fail;
   2552 
   2553       if (hdr->sh_link > elf_numsections (abfd))
   2554 	{
   2555 	  /* PR 10478: Accept Solaris binaries with a sh_link field
   2556 	     set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
   2557 	  switch (bfd_get_arch (abfd))
   2558 	    {
   2559 	    case bfd_arch_i386:
   2560 	    case bfd_arch_sparc:
   2561 	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
   2562 		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
   2563 		break;
   2564 	      /* Otherwise fall through.  */
   2565 	    default:
   2566 	      goto fail;
   2567 	    }
   2568 	}
   2569       else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
   2570 	goto fail;
   2571       else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
   2572 	{
   2573 	  Elf_Internal_Shdr *dynsymhdr;
   2574 
   2575 	  /* The shared libraries distributed with hpux11 have a bogus
   2576 	     sh_link field for the ".dynamic" section.  Find the
   2577 	     string table for the ".dynsym" section instead.  */
   2578 	  if (elf_dynsymtab (abfd) != 0)
   2579 	    {
   2580 	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
   2581 	      hdr->sh_link = dynsymhdr->sh_link;
   2582 	    }
   2583 	  else
   2584 	    {
   2585 	      unsigned int i, num_sec;
   2586 
   2587 	      num_sec = elf_numsections (abfd);
   2588 	      for (i = 1; i < num_sec; i++)
   2589 		{
   2590 		  dynsymhdr = elf_elfsections (abfd)[i];
   2591 		  if (dynsymhdr->sh_type == SHT_DYNSYM)
   2592 		    {
   2593 		      hdr->sh_link = dynsymhdr->sh_link;
   2594 		      break;
   2595 		    }
   2596 		}
   2597 	    }
   2598 	}
   2599       goto success;
   2600 
   2601     case SHT_SYMTAB:		/* A symbol table.  */
   2602       if (elf_onesymtab (abfd) == shindex)
   2603 	goto success;
   2604 
   2605       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2606 	goto fail;
   2607 
   2608       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2609 	{
   2610 	  if (hdr->sh_size != 0)
   2611 	    goto fail;
   2612 	  /* Some assemblers erroneously set sh_info to one with a
   2613 	     zero sh_size.  ld sees this as a global symbol count
   2614 	     of (unsigned) -1.  Fix it here.  */
   2615 	  hdr->sh_info = 0;
   2616 	  goto success;
   2617 	}
   2618 
   2619       /* PR 18854: A binary might contain more than one symbol table.
   2620 	 Unusual, but possible.  Warn, but continue.  */
   2621       if (elf_onesymtab (abfd) != 0)
   2622 	{
   2623 	  _bfd_error_handler
   2624 	    /* xgettext:c-format */
   2625 	    (_("%pB: warning: multiple symbol tables detected"
   2626 	       " - ignoring the table in section %u"),
   2627 	     abfd, shindex);
   2628 	  goto success;
   2629 	}
   2630       elf_onesymtab (abfd) = shindex;
   2631       elf_symtab_hdr (abfd) = *hdr;
   2632       elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
   2633       abfd->flags |= HAS_SYMS;
   2634 
   2635       /* Sometimes a shared object will map in the symbol table.  If
   2636 	 SHF_ALLOC is set, and this is a shared object, then we also
   2637 	 treat this section as a BFD section.  We can not base the
   2638 	 decision purely on SHF_ALLOC, because that flag is sometimes
   2639 	 set in a relocatable object file, which would confuse the
   2640 	 linker.  */
   2641       if ((hdr->sh_flags & SHF_ALLOC) != 0
   2642 	  && (abfd->flags & DYNAMIC) != 0
   2643 	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2644 						shindex))
   2645 	goto fail;
   2646 
   2647       /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
   2648 	 can't read symbols without that section loaded as well.  It
   2649 	 is most likely specified by the next section header.  */
   2650       {
   2651 	elf_section_list * entry;
   2652 	unsigned int i, num_sec;
   2653 
   2654 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2655 	  if (entry->hdr.sh_link == shindex)
   2656 	    goto success;
   2657 
   2658 	num_sec = elf_numsections (abfd);
   2659 	for (i = shindex + 1; i < num_sec; i++)
   2660 	  {
   2661 	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2662 
   2663 	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2664 		&& hdr2->sh_link == shindex)
   2665 	      break;
   2666 	  }
   2667 
   2668 	if (i == num_sec)
   2669 	  for (i = 1; i < shindex; i++)
   2670 	    {
   2671 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2672 
   2673 	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
   2674 		  && hdr2->sh_link == shindex)
   2675 		break;
   2676 	    }
   2677 
   2678 	if (i != shindex)
   2679 	  ret = bfd_section_from_shdr (abfd, i);
   2680 	/* else FIXME: we have failed to find the symbol table.
   2681 	   Should we issue an error?  */
   2682 	goto success;
   2683       }
   2684 
   2685     case SHT_DYNSYM:		/* A dynamic symbol table.  */
   2686       if (elf_dynsymtab (abfd) == shindex)
   2687 	goto success;
   2688 
   2689       if (hdr->sh_entsize != bed->s->sizeof_sym)
   2690 	goto fail;
   2691 
   2692       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
   2693 	{
   2694 	  if (hdr->sh_size != 0)
   2695 	    goto fail;
   2696 
   2697 	  /* Some linkers erroneously set sh_info to one with a
   2698 	     zero sh_size.  ld sees this as a global symbol count
   2699 	     of (unsigned) -1.  Fix it here.  */
   2700 	  hdr->sh_info = 0;
   2701 	  goto success;
   2702 	}
   2703 
   2704       /* PR 18854: A binary might contain more than one dynamic symbol table.
   2705 	 Unusual, but possible.  Warn, but continue.  */
   2706       if (elf_dynsymtab (abfd) != 0)
   2707 	{
   2708 	  _bfd_error_handler
   2709 	    /* xgettext:c-format */
   2710 	    (_("%pB: warning: multiple dynamic symbol tables detected"
   2711 	       " - ignoring the table in section %u"),
   2712 	     abfd, shindex);
   2713 	  goto success;
   2714 	}
   2715       elf_dynsymtab (abfd) = shindex;
   2716       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
   2717       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   2718       abfd->flags |= HAS_SYMS;
   2719 
   2720       /* Besides being a symbol table, we also treat this as a regular
   2721 	 section, so that objcopy can handle it.  */
   2722       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2723       goto success;
   2724 
   2725     case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
   2726       {
   2727 	elf_section_list * entry;
   2728 
   2729 	for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
   2730 	  if (entry->ndx == shindex)
   2731 	    goto success;
   2732 
   2733 	entry = bfd_alloc (abfd, sizeof (*entry));
   2734 	if (entry == NULL)
   2735 	  goto fail;
   2736 	entry->ndx = shindex;
   2737 	entry->hdr = * hdr;
   2738 	entry->next = elf_symtab_shndx_list (abfd);
   2739 	elf_symtab_shndx_list (abfd) = entry;
   2740 	elf_elfsections (abfd)[shindex] = & entry->hdr;
   2741 	goto success;
   2742       }
   2743 
   2744     case SHT_STRTAB:		/* A string table.  */
   2745       if (hdr->bfd_section != NULL)
   2746 	goto success;
   2747 
   2748       if (ehdr->e_shstrndx == shindex)
   2749 	{
   2750 	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
   2751 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
   2752 	  goto success;
   2753 	}
   2754 
   2755       if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
   2756 	{
   2757 	symtab_strtab:
   2758 	  elf_tdata (abfd)->strtab_hdr = *hdr;
   2759 	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
   2760 	  goto success;
   2761 	}
   2762 
   2763       if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
   2764 	{
   2765 	dynsymtab_strtab:
   2766 	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
   2767 	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
   2768 	  elf_elfsections (abfd)[shindex] = hdr;
   2769 	  /* We also treat this as a regular section, so that objcopy
   2770 	     can handle it.  */
   2771 	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
   2772 						 shindex);
   2773 	  goto success;
   2774 	}
   2775 
   2776       /* If the string table isn't one of the above, then treat it as a
   2777 	 regular section.  We need to scan all the headers to be sure,
   2778 	 just in case this strtab section appeared before the above.  */
   2779       if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
   2780 	{
   2781 	  unsigned int i, num_sec;
   2782 
   2783 	  num_sec = elf_numsections (abfd);
   2784 	  for (i = 1; i < num_sec; i++)
   2785 	    {
   2786 	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
   2787 	      if (hdr2->sh_link == shindex)
   2788 		{
   2789 		  /* Prevent endless recursion on broken objects.  */
   2790 		  if (i == shindex)
   2791 		    goto fail;
   2792 		  if (! bfd_section_from_shdr (abfd, i))
   2793 		    goto fail;
   2794 		  if (elf_onesymtab (abfd) == i)
   2795 		    goto symtab_strtab;
   2796 		  if (elf_dynsymtab (abfd) == i)
   2797 		    goto dynsymtab_strtab;
   2798 		}
   2799 	    }
   2800 	}
   2801       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2802       goto success;
   2803 
   2804     case SHT_REL:
   2805     case SHT_RELA:
   2806     case SHT_RELR:
   2807       /* *These* do a lot of work -- but build no sections!  */
   2808       {
   2809 	asection *target_sect;
   2810 	Elf_Internal_Shdr *hdr2, **p_hdr;
   2811 	unsigned int num_sec = elf_numsections (abfd);
   2812 	struct bfd_elf_section_data *esdt;
   2813 	bfd_size_type size;
   2814 
   2815 	if (hdr->sh_type == SHT_REL)
   2816 	  size = bed->s->sizeof_rel;
   2817 	else if (hdr->sh_type == SHT_RELA)
   2818 	  size = bed->s->sizeof_rela;
   2819 	else
   2820 	  size = bed->s->arch_size / 8;
   2821 	if (hdr->sh_entsize != size)
   2822 	  goto fail;
   2823 
   2824 	/* Check for a bogus link to avoid crashing.  */
   2825 	if (hdr->sh_link >= num_sec)
   2826 	  {
   2827 	    _bfd_error_handler
   2828 	      /* xgettext:c-format */
   2829 	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
   2830 	       abfd, hdr->sh_link, name, shindex);
   2831 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2832 	    goto success;
   2833 	  }
   2834 
   2835 	/* Get the symbol table.  */
   2836 	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
   2837 	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
   2838 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
   2839 	  goto fail;
   2840 
   2841 	/* If this is an alloc section in an executable or shared
   2842 	   library, or the reloc section does not use the main symbol
   2843 	   table we don't treat it as a reloc section.  BFD can't
   2844 	   adequately represent such a section, so at least for now,
   2845 	   we don't try.  We just present it as a normal section.  We
   2846 	   also can't use it as a reloc section if it points to the
   2847 	   null section, an invalid section, another reloc section, or
   2848 	   its sh_link points to the null section.  */
   2849 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
   2850 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
   2851 	    || (hdr->sh_flags & SHF_COMPRESSED) != 0
   2852 	    || hdr->sh_type == SHT_RELR
   2853 	    || hdr->sh_link == SHN_UNDEF
   2854 	    || hdr->sh_link != elf_onesymtab (abfd)
   2855 	    || hdr->sh_info == SHN_UNDEF
   2856 	    || hdr->sh_info >= num_sec
   2857 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
   2858 	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
   2859 	  {
   2860 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2861 	    goto success;
   2862 	  }
   2863 
   2864 	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
   2865 	  goto fail;
   2866 
   2867 	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
   2868 	if (target_sect == NULL)
   2869 	  goto fail;
   2870 
   2871 	esdt = elf_section_data (target_sect);
   2872 	if (hdr->sh_type == SHT_RELA)
   2873 	  p_hdr = &esdt->rela.hdr;
   2874 	else
   2875 	  p_hdr = &esdt->rel.hdr;
   2876 
   2877 	/* PR 17512: file: 0b4f81b7.
   2878 	   Also see PR 24456, for a file which deliberately has two reloc
   2879 	   sections.  */
   2880 	if (*p_hdr != NULL)
   2881 	  {
   2882 	    if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
   2883 	      {
   2884 		_bfd_error_handler
   2885 		  /* xgettext:c-format */
   2886 		  (_("%pB: warning: secondary relocation section '%s' "
   2887 		     "for section %pA found - ignoring"),
   2888 		   abfd, name, target_sect);
   2889 	      }
   2890 	    else
   2891 	      esdt->has_secondary_relocs = true;
   2892 	    goto success;
   2893 	  }
   2894 
   2895 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
   2896 	if (hdr2 == NULL)
   2897 	  goto fail;
   2898 	*hdr2 = *hdr;
   2899 	*p_hdr = hdr2;
   2900 	elf_elfsections (abfd)[shindex] = hdr2;
   2901 	target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
   2902 				     * bed->s->int_rels_per_ext_rel);
   2903 	target_sect->flags |= SEC_RELOC;
   2904 	target_sect->relocation = NULL;
   2905 	target_sect->rel_filepos = hdr->sh_offset;
   2906 	/* In the section to which the relocations apply, mark whether
   2907 	   its relocations are of the REL or RELA variety.  */
   2908 	if (hdr->sh_size != 0)
   2909 	  {
   2910 	    if (hdr->sh_type == SHT_RELA)
   2911 	      target_sect->use_rela_p = 1;
   2912 	  }
   2913 	abfd->flags |= HAS_RELOC;
   2914 	goto success;
   2915       }
   2916 
   2917     case SHT_GNU_verdef:
   2918       if (hdr->sh_info != 0)
   2919 	elf_dynverdef (abfd) = shindex;
   2920       elf_tdata (abfd)->dynverdef_hdr = *hdr;
   2921       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2922       goto success;
   2923 
   2924     case SHT_GNU_versym:
   2925       if (hdr->sh_entsize != sizeof (Elf_External_Versym))
   2926 	goto fail;
   2927 
   2928       elf_dynversym (abfd) = shindex;
   2929       elf_tdata (abfd)->dynversym_hdr = *hdr;
   2930       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2931       goto success;
   2932 
   2933     case SHT_GNU_verneed:
   2934       if (hdr->sh_info != 0)
   2935 	elf_dynverref (abfd) = shindex;
   2936       elf_tdata (abfd)->dynverref_hdr = *hdr;
   2937       ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2938       goto success;
   2939 
   2940     case SHT_SHLIB:
   2941       goto success;
   2942 
   2943     case SHT_GROUP:
   2944       if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2945 	goto fail;
   2946 
   2947       goto success;
   2948 
   2949     default:
   2950       /* Possibly an attributes section.  */
   2951       if (get_elf_backend_data (abfd)->target_os != is_solaris
   2952 	  /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES.  */
   2953 	  && (hdr->sh_type == SHT_GNU_ATTRIBUTES
   2954 	      || hdr->sh_type == bed->obj_attrs_section_type))
   2955 	{
   2956 	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   2957 	    goto fail;
   2958 	  _bfd_elf_parse_attributes (abfd, hdr);
   2959 	  goto success;
   2960 	}
   2961 
   2962       /* Check for any processor-specific section types.  */
   2963       if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
   2964 	goto success;
   2965 
   2966       if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
   2967 	{
   2968 	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
   2969 	    /* FIXME: How to properly handle allocated section reserved
   2970 	       for applications?  */
   2971 	    _bfd_error_handler
   2972 	      /* xgettext:c-format */
   2973 	      (_("%pB: unknown type [%#x] section `%s'"),
   2974 	       abfd, hdr->sh_type, name);
   2975 	  else
   2976 	    {
   2977 	      /* Allow sections reserved for applications.  */
   2978 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   2979 	      goto success;
   2980 	    }
   2981 	}
   2982       else if (hdr->sh_type >= SHT_LOPROC
   2983 	       && hdr->sh_type <= SHT_HIPROC)
   2984 	/* FIXME: We should handle this section.  */
   2985 	_bfd_error_handler
   2986 	  /* xgettext:c-format */
   2987 	  (_("%pB: unknown type [%#x] section `%s'"),
   2988 	   abfd, hdr->sh_type, name);
   2989       else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
   2990 	{
   2991 	  /* Unrecognised OS-specific sections.  */
   2992 	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
   2993 	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
   2994 	       required to correctly process the section and the file should
   2995 	       be rejected with an error message.  */
   2996 	    _bfd_error_handler
   2997 	      /* xgettext:c-format */
   2998 	      (_("%pB: unknown type [%#x] section `%s'"),
   2999 	       abfd, hdr->sh_type, name);
   3000 	  else
   3001 	    {
   3002 	      /* Otherwise it should be processed.  */
   3003 	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   3004 	      goto success;
   3005 	    }
   3006 	}
   3007       else
   3008 	/* FIXME: We should handle this section.  */
   3009 	_bfd_error_handler
   3010 	  /* xgettext:c-format */
   3011 	  (_("%pB: unknown type [%#x] section `%s'"),
   3012 	   abfd, hdr->sh_type, name);
   3013 
   3014       goto fail;
   3015     }
   3016 
   3017  fail:
   3018   ret = false;
   3019  success:
   3020   elf_tdata (abfd)->being_created[shindex] = false;
   3021   return ret;
   3022 }
   3023 
   3024 /* Return the local symbol specified by ABFD, R_SYMNDX.  */
   3025 
   3026 Elf_Internal_Sym *
   3027 bfd_sym_from_r_symndx (struct sym_cache *cache,
   3028 		       bfd *abfd,
   3029 		       unsigned long r_symndx)
   3030 {
   3031   unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
   3032 
   3033   if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
   3034     {
   3035       Elf_Internal_Shdr *symtab_hdr;
   3036       unsigned char esym[sizeof (Elf64_External_Sym)];
   3037       Elf_External_Sym_Shndx eshndx;
   3038 
   3039       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   3040       if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
   3041 				&cache->sym[ent], esym, &eshndx) == NULL)
   3042 	return NULL;
   3043 
   3044       if (cache->abfd != abfd)
   3045 	{
   3046 	  memset (cache->indx, -1, sizeof (cache->indx));
   3047 	  cache->abfd = abfd;
   3048 	}
   3049       cache->indx[ent] = r_symndx;
   3050     }
   3051 
   3052   return &cache->sym[ent];
   3053 }
   3054 
   3055 /* Given an ELF section number, retrieve the corresponding BFD
   3056    section.  */
   3057 
   3058 asection *
   3059 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
   3060 {
   3061   if (sec_index >= elf_numsections (abfd))
   3062     return NULL;
   3063   return elf_elfsections (abfd)[sec_index]->bfd_section;
   3064 }
   3065 
   3066 static const struct bfd_elf_special_section special_sections_b[] =
   3067 {
   3068   { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3069   { NULL,		    0,	0, 0,		 0 }
   3070 };
   3071 
   3072 static const struct bfd_elf_special_section special_sections_c[] =
   3073 {
   3074   { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
   3075   { STRING_COMMA_LEN (".ctf"),	0, SHT_PROGBITS,    0 },
   3076   { NULL,			0, 0, 0,	    0 }
   3077 };
   3078 
   3079 static const struct bfd_elf_special_section special_sections_d[] =
   3080 {
   3081   { STRING_COMMA_LEN (".data"),		-2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3082   { STRING_COMMA_LEN (".data1"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3083   /* There are more DWARF sections than these, but they needn't be added here
   3084      unless you have to cope with broken compilers that don't emit section
   3085      attributes or you want to help the user writing assembler.  */
   3086   { STRING_COMMA_LEN (".debug"),	 0, SHT_PROGBITS, 0 },
   3087   { STRING_COMMA_LEN (".debug_line"),	 0, SHT_PROGBITS, 0 },
   3088   { STRING_COMMA_LEN (".debug_info"),	 0, SHT_PROGBITS, 0 },
   3089   { STRING_COMMA_LEN (".debug_abbrev"),	 0, SHT_PROGBITS, 0 },
   3090   { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
   3091   { STRING_COMMA_LEN (".dynamic"),	 0, SHT_DYNAMIC,  SHF_ALLOC },
   3092   { STRING_COMMA_LEN (".dynstr"),	 0, SHT_STRTAB,	  SHF_ALLOC },
   3093   { STRING_COMMA_LEN (".dynsym"),	 0, SHT_DYNSYM,	  SHF_ALLOC },
   3094   { NULL,		       0,	 0, 0,		  0 }
   3095 };
   3096 
   3097 static const struct bfd_elf_special_section special_sections_f[] =
   3098 {
   3099   { STRING_COMMA_LEN (".fini"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   3100   { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
   3101   { NULL,			   0 , 0, 0,		  0 }
   3102 };
   3103 
   3104 static const struct bfd_elf_special_section special_sections_g[] =
   3105 {
   3106   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   3107   { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
   3108   { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   3109   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
   3110   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
   3111   { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
   3112   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
   3113   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
   3114   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
   3115   { STRING_COMMA_LEN (".gnu.liblist"),	   0, SHT_GNU_LIBLIST, SHF_ALLOC },
   3116   { STRING_COMMA_LEN (".gnu.conflict"),	   0, SHT_RELA,	       SHF_ALLOC },
   3117   { STRING_COMMA_LEN (".gnu.hash"),	   0, SHT_GNU_HASH,    SHF_ALLOC },
   3118   { NULL,			 0,	   0, 0,	       0 }
   3119 };
   3120 
   3121 static const struct bfd_elf_special_section special_sections_h[] =
   3122 {
   3123   { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,	 SHF_ALLOC },
   3124   { NULL,		     0, 0, 0,		 0 }
   3125 };
   3126 
   3127 static const struct bfd_elf_special_section special_sections_i[] =
   3128 {
   3129   { STRING_COMMA_LEN (".init"),	       0, SHT_PROGBITS,	  SHF_ALLOC + SHF_EXECINSTR },
   3130   { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   3131   { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,	  0 },
   3132   { NULL,		       0,      0, 0,		  0 }
   3133 };
   3134 
   3135 static const struct bfd_elf_special_section special_sections_l[] =
   3136 {
   3137   { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
   3138   { NULL,		     0, 0, 0,		 0 }
   3139 };
   3140 
   3141 static const struct bfd_elf_special_section special_sections_n[] =
   3142 {
   3143   { STRING_COMMA_LEN (".noinit"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3144   { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
   3145   { STRING_COMMA_LEN (".note"),		 -1, SHT_NOTE,	   0 },
   3146   { NULL,		     0,		  0, 0,		   0 }
   3147 };
   3148 
   3149 static const struct bfd_elf_special_section special_sections_p[] =
   3150 {
   3151   { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,	SHF_ALLOC + SHF_WRITE },
   3152   { STRING_COMMA_LEN (".persistent"),	 -2, SHT_PROGBITS,	SHF_ALLOC + SHF_WRITE },
   3153   { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   3154   { STRING_COMMA_LEN (".plt"),		  0, SHT_PROGBITS,	SHF_ALLOC + SHF_EXECINSTR },
   3155   { NULL,		    0,		  0, 0,			0 }
   3156 };
   3157 
   3158 static const struct bfd_elf_special_section special_sections_r[] =
   3159 {
   3160   { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
   3161   { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
   3162   { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
   3163   { STRING_COMMA_LEN (".rela"),	  -1, SHT_RELA,	    0 },
   3164   /* .relro_padding is generated by lld.  It should not be confused with a
   3165      reloc containing section, because otherwise elf_fake_sections() will
   3166      set the entsize to 8, which may not be an actual multiple of the
   3167      section's size.
   3168      Note - this entry must appear before the ".rel" entry below.  */
   3169   { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
   3170   { STRING_COMMA_LEN (".rel"),	  -1, SHT_REL,	    0 },
   3171   { NULL,		    0,	   0, 0,	    0 }
   3172 };
   3173 
   3174 static const struct bfd_elf_special_section special_sections_s[] =
   3175 {
   3176   { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
   3177   { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
   3178   { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
   3179   /* See struct bfd_elf_special_section declaration for the semantics of
   3180      this special case where .prefix_length != strlen (.prefix).  */
   3181   { ".stabstr",			5,  3, SHT_STRTAB, 0 },
   3182   { NULL,			0,  0, 0,	   0 }
   3183 };
   3184 
   3185 static const struct bfd_elf_special_section special_sections_t[] =
   3186 {
   3187   { STRING_COMMA_LEN (".text"),	 -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
   3188   { STRING_COMMA_LEN (".tbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
   3189   { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
   3190   { NULL,		      0,  0, 0,		   0 }
   3191 };
   3192 
   3193 static const struct bfd_elf_special_section special_sections_z[] =
   3194 {
   3195   { STRING_COMMA_LEN (".zdebug_line"),	  0, SHT_PROGBITS, 0 },
   3196   { STRING_COMMA_LEN (".zdebug_info"),	  0, SHT_PROGBITS, 0 },
   3197   { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
   3198   { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
   3199   { NULL,		      0,  0, 0,		   0 }
   3200 };
   3201 
   3202 static const struct bfd_elf_special_section * const special_sections[] =
   3203 {
   3204   special_sections_b,		/* 'b' */
   3205   special_sections_c,		/* 'c' */
   3206   special_sections_d,		/* 'd' */
   3207   NULL,				/* 'e' */
   3208   special_sections_f,		/* 'f' */
   3209   special_sections_g,		/* 'g' */
   3210   special_sections_h,		/* 'h' */
   3211   special_sections_i,		/* 'i' */
   3212   NULL,				/* 'j' */
   3213   NULL,				/* 'k' */
   3214   special_sections_l,		/* 'l' */
   3215   NULL,				/* 'm' */
   3216   special_sections_n,		/* 'n' */
   3217   NULL,				/* 'o' */
   3218   special_sections_p,		/* 'p' */
   3219   NULL,				/* 'q' */
   3220   special_sections_r,		/* 'r' */
   3221   special_sections_s,		/* 's' */
   3222   special_sections_t,		/* 't' */
   3223   NULL,				/* 'u' */
   3224   NULL,				/* 'v' */
   3225   NULL,				/* 'w' */
   3226   NULL,				/* 'x' */
   3227   NULL,				/* 'y' */
   3228   special_sections_z		/* 'z' */
   3229 };
   3230 
   3231 const struct bfd_elf_special_section *
   3232 _bfd_elf_get_special_section (const char *name,
   3233 			      const struct bfd_elf_special_section *spec,
   3234 			      unsigned int rela)
   3235 {
   3236   int i;
   3237   int len;
   3238 
   3239   len = strlen (name);
   3240 
   3241   for (i = 0; spec[i].prefix != NULL; i++)
   3242     {
   3243       int suffix_len;
   3244       int prefix_len = spec[i].prefix_length;
   3245 
   3246       if (len < prefix_len)
   3247 	continue;
   3248       if (memcmp (name, spec[i].prefix, prefix_len) != 0)
   3249 	continue;
   3250 
   3251       suffix_len = spec[i].suffix_length;
   3252       if (suffix_len <= 0)
   3253 	{
   3254 	  if (name[prefix_len] != 0)
   3255 	    {
   3256 	      if (suffix_len == 0)
   3257 		continue;
   3258 	      if (name[prefix_len] != '.'
   3259 		  && (suffix_len == -2
   3260 		      || (rela && spec[i].type == SHT_REL)))
   3261 		continue;
   3262 	    }
   3263 	}
   3264       else
   3265 	{
   3266 	  if (len < prefix_len + suffix_len)
   3267 	    continue;
   3268 	  if (memcmp (name + len - suffix_len,
   3269 		      spec[i].prefix + prefix_len,
   3270 		      suffix_len) != 0)
   3271 	    continue;
   3272 	}
   3273       return &spec[i];
   3274     }
   3275 
   3276   return NULL;
   3277 }
   3278 
   3279 const struct bfd_elf_special_section *
   3280 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   3281 {
   3282   int i;
   3283   const struct bfd_elf_special_section *spec;
   3284   elf_backend_data *bed;
   3285 
   3286   /* See if this is one of the special sections.  */
   3287   if (sec->name == NULL)
   3288     return NULL;
   3289 
   3290   bed = get_elf_backend_data (abfd);
   3291   spec = bed->special_sections;
   3292   if (spec)
   3293     {
   3294       spec = _bfd_elf_get_special_section (sec->name,
   3295 					   bed->special_sections,
   3296 					   sec->use_rela_p);
   3297       if (spec != NULL)
   3298 	return spec;
   3299     }
   3300 
   3301   if (sec->name[0] != '.')
   3302     return NULL;
   3303 
   3304   i = sec->name[1] - 'b';
   3305   if (i < 0 || i > 'z' - 'b')
   3306     return NULL;
   3307 
   3308   spec = special_sections[i];
   3309 
   3310   if (spec == NULL)
   3311     return NULL;
   3312 
   3313   return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
   3314 }
   3315 
   3316 bool
   3317 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
   3318 {
   3319   struct bfd_elf_section_data *sdata;
   3320   elf_backend_data *bed;
   3321   const struct bfd_elf_special_section *ssect;
   3322 
   3323   sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
   3324   if (sdata == NULL)
   3325     {
   3326       sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
   3327 							  sizeof (*sdata));
   3328       if (sdata == NULL)
   3329 	return false;
   3330       sec->used_by_bfd = sdata;
   3331     }
   3332 
   3333   /* Indicate whether or not this section should use RELA relocations.  */
   3334   bed = get_elf_backend_data (abfd);
   3335   sec->use_rela_p = bed->default_use_rela_p;
   3336 
   3337   /* Set up ELF section type and flags for newly created sections, if
   3338      there is an ABI mandated section.  */
   3339   ssect = (*bed->get_sec_type_attr) (abfd, sec);
   3340   if (ssect != NULL)
   3341     {
   3342       elf_section_type (sec) = ssect->type;
   3343       elf_section_flags (sec) = ssect->attr;
   3344     }
   3345 
   3346   return _bfd_generic_new_section_hook (abfd, sec);
   3347 }
   3348 
   3349 /* Create a new bfd section from an ELF program header.
   3350 
   3351    Since program segments have no names, we generate a synthetic name
   3352    of the form segment<NUM>, where NUM is generally the index in the
   3353    program header table.  For segments that are split (see below) we
   3354    generate the names segment<NUM>a and segment<NUM>b.
   3355 
   3356    Note that some program segments may have a file size that is different than
   3357    (less than) the memory size.  All this means is that at execution the
   3358    system must allocate the amount of memory specified by the memory size,
   3359    but only initialize it with the first "file size" bytes read from the
   3360    file.  This would occur for example, with program segments consisting
   3361    of combined data+bss.
   3362 
   3363    To handle the above situation, this routine generates TWO bfd sections
   3364    for the single program segment.  The first has the length specified by
   3365    the file size of the segment, and the second has the length specified
   3366    by the difference between the two sizes.  In effect, the segment is split
   3367    into its initialized and uninitialized parts.  */
   3368 
   3369 bool
   3370 _bfd_elf_make_section_from_phdr (bfd *abfd,
   3371 				 Elf_Internal_Phdr *hdr,
   3372 				 int hdr_index,
   3373 				 const char *type_name)
   3374 {
   3375   asection *newsect;
   3376   char *name;
   3377   char namebuf[64];
   3378   size_t len;
   3379   int split;
   3380   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   3381 
   3382   split = ((hdr->p_memsz > 0)
   3383 	    && (hdr->p_filesz > 0)
   3384 	    && (hdr->p_memsz > hdr->p_filesz));
   3385 
   3386   if (hdr->p_filesz > 0)
   3387     {
   3388       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
   3389       len = strlen (namebuf) + 1;
   3390       name = (char *) bfd_alloc (abfd, len);
   3391       if (!name)
   3392 	return false;
   3393       memcpy (name, namebuf, len);
   3394       newsect = bfd_make_section (abfd, name);
   3395       if (newsect == NULL)
   3396 	return false;
   3397       newsect->vma = hdr->p_vaddr / opb;
   3398       newsect->lma = hdr->p_paddr / opb;
   3399       newsect->size = hdr->p_filesz;
   3400       newsect->filepos = hdr->p_offset;
   3401       newsect->flags |= SEC_HAS_CONTENTS;
   3402       newsect->alignment_power = bfd_log2 (hdr->p_align);
   3403       if (hdr->p_type == PT_LOAD)
   3404 	{
   3405 	  newsect->flags |= SEC_ALLOC;
   3406 	  newsect->flags |= SEC_LOAD;
   3407 	  if (hdr->p_flags & PF_X)
   3408 	    {
   3409 	      /* FIXME: all we known is that it has execute PERMISSION,
   3410 		 may be data.  */
   3411 	      newsect->flags |= SEC_CODE;
   3412 	    }
   3413 	}
   3414       if (!(hdr->p_flags & PF_W))
   3415 	{
   3416 	  newsect->flags |= SEC_READONLY;
   3417 	}
   3418     }
   3419 
   3420   if (hdr->p_memsz > hdr->p_filesz)
   3421     {
   3422       bfd_vma align;
   3423 
   3424       sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
   3425       len = strlen (namebuf) + 1;
   3426       name = (char *) bfd_alloc (abfd, len);
   3427       if (!name)
   3428 	return false;
   3429       memcpy (name, namebuf, len);
   3430       newsect = bfd_make_section (abfd, name);
   3431       if (newsect == NULL)
   3432 	return false;
   3433       newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
   3434       newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
   3435       newsect->size = hdr->p_memsz - hdr->p_filesz;
   3436       newsect->filepos = hdr->p_offset + hdr->p_filesz;
   3437       align = newsect->vma & -newsect->vma;
   3438       if (align == 0 || align > hdr->p_align)
   3439 	align = hdr->p_align;
   3440       newsect->alignment_power = bfd_log2 (align);
   3441       if (hdr->p_type == PT_LOAD)
   3442 	{
   3443 	  newsect->flags |= SEC_ALLOC;
   3444 	  if (hdr->p_flags & PF_X)
   3445 	    newsect->flags |= SEC_CODE;
   3446 	}
   3447       if (!(hdr->p_flags & PF_W))
   3448 	newsect->flags |= SEC_READONLY;
   3449     }
   3450 
   3451   return true;
   3452 }
   3453 
   3454 static bool
   3455 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
   3456 {
   3457   /* The return value is ignored.  Build-ids are considered optional.  */
   3458   if (templ->xvec->flavour == bfd_target_elf_flavour)
   3459     return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
   3460       (templ, offset);
   3461   return false;
   3462 }
   3463 
   3464 bool
   3465 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
   3466 {
   3467   elf_backend_data *bed;
   3468 
   3469   switch (hdr->p_type)
   3470     {
   3471     case PT_NULL:
   3472       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
   3473 
   3474     case PT_LOAD:
   3475       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
   3476 	return false;
   3477       if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
   3478 	_bfd_elf_core_find_build_id (abfd, hdr->p_offset);
   3479       return true;
   3480 
   3481     case PT_DYNAMIC:
   3482       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
   3483 
   3484     case PT_INTERP:
   3485       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
   3486 
   3487     case PT_NOTE:
   3488       if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
   3489 	return false;
   3490       if (! _bfd_elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
   3491 				 hdr->p_align))
   3492 	return false;
   3493       return true;
   3494 
   3495     case PT_SHLIB:
   3496       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
   3497 
   3498     case PT_PHDR:
   3499       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
   3500 
   3501     case PT_GNU_EH_FRAME:
   3502       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3503 					      "eh_frame_hdr");
   3504 
   3505     case PT_GNU_STACK:
   3506       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
   3507 
   3508     case PT_GNU_RELRO:
   3509       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
   3510 
   3511     case PT_GNU_SFRAME:
   3512       return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
   3513 					      "sframe");
   3514 
   3515     default:
   3516       /* Check for any processor-specific program segment types.  */
   3517       bed = get_elf_backend_data (abfd);
   3518       return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
   3519     }
   3520 }
   3521 
   3522 /* Return the REL_HDR for SEC, assuming there is only a single one, either
   3523    REL or RELA.  */
   3524 
   3525 Elf_Internal_Shdr *
   3526 _bfd_elf_single_rel_hdr (asection *sec)
   3527 {
   3528   if (elf_section_data (sec)->rel.hdr)
   3529     {
   3530       BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
   3531       return elf_section_data (sec)->rel.hdr;
   3532     }
   3533   else
   3534     return elf_section_data (sec)->rela.hdr;
   3535 }
   3536 
   3537 static bool
   3538 _bfd_elf_set_reloc_sh_name (bfd *abfd,
   3539 			    Elf_Internal_Shdr *rel_hdr,
   3540 			    const char *sec_name,
   3541 			    bool use_rela_p)
   3542 {
   3543   char *name = (char *) bfd_alloc (abfd,
   3544 				   sizeof ".rela" + strlen (sec_name));
   3545   if (name == NULL)
   3546     return false;
   3547 
   3548   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
   3549   rel_hdr->sh_name =
   3550     (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
   3551 					false);
   3552   if (rel_hdr->sh_name == (unsigned int) -1)
   3553     return false;
   3554 
   3555   return true;
   3556 }
   3557 
   3558 /* Allocate and initialize a section-header for a new reloc section,
   3559    containing relocations against ASECT.  It is stored in RELDATA.  If
   3560    USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
   3561    relocations.  */
   3562 
   3563 static bool
   3564 _bfd_elf_init_reloc_shdr (bfd *abfd,
   3565 			  struct bfd_elf_section_reloc_data *reldata,
   3566 			  const char *sec_name,
   3567 			  bool use_rela_p,
   3568 			  bool delay_sh_name_p)
   3569 {
   3570   Elf_Internal_Shdr *rel_hdr;
   3571   elf_backend_data *bed = get_elf_backend_data (abfd);
   3572 
   3573   BFD_ASSERT (reldata->hdr == NULL);
   3574   rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
   3575   if (rel_hdr == NULL)
   3576     return false;
   3577   reldata->hdr = rel_hdr;
   3578 
   3579   if (delay_sh_name_p)
   3580     rel_hdr->sh_name = (unsigned int) -1;
   3581   else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
   3582 					use_rela_p))
   3583     return false;
   3584   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
   3585   rel_hdr->sh_entsize = (use_rela_p
   3586 			 ? bed->s->sizeof_rela
   3587 			 : bed->s->sizeof_rel);
   3588   rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   3589   rel_hdr->sh_flags = 0;
   3590   rel_hdr->sh_addr = 0;
   3591   rel_hdr->sh_size = 0;
   3592   rel_hdr->sh_offset = 0;
   3593 
   3594   return true;
   3595 }
   3596 
   3597 /* Return the default section type based on the passed in section flags.  */
   3598 
   3599 int
   3600 bfd_elf_get_default_section_type (flagword flags)
   3601 {
   3602   if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
   3603       && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   3604     return SHT_NOBITS;
   3605   return SHT_PROGBITS;
   3606 }
   3607 
   3608 struct fake_section_arg
   3609 {
   3610   struct bfd_link_info *link_info;
   3611   bool failed;
   3612 };
   3613 
   3614 /* Set up an ELF internal section header for a section.  */
   3615 
   3616 static void
   3617 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
   3618 {
   3619   struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
   3620   elf_backend_data *bed = get_elf_backend_data (abfd);
   3621   struct bfd_elf_section_data *esd = elf_section_data (asect);
   3622   Elf_Internal_Shdr *this_hdr;
   3623   unsigned int sh_type;
   3624   const char *name = asect->name;
   3625   bool delay_sh_name_p = false;
   3626   bfd_vma mask;
   3627 
   3628   if (arg->failed)
   3629     {
   3630       /* We already failed; just get out of the bfd_map_over_sections
   3631 	 loop.  */
   3632       return;
   3633     }
   3634 
   3635   this_hdr = &esd->this_hdr;
   3636 
   3637   /* ld: compress DWARF debug sections with names: .debug_*.  */
   3638   if (arg->link_info
   3639       && (abfd->flags & BFD_COMPRESS) != 0
   3640       && (asect->flags & SEC_DEBUGGING) != 0
   3641       && (asect->flags & SEC_ALLOC) == 0
   3642       && (asect->flags & SEC_HAS_CONTENTS) != 0
   3643       && name[1] == 'd'
   3644       && name[6] == '_')
   3645     {
   3646       /* If this section will be compressed, delay adding section
   3647 	 name to section name section after it is compressed in
   3648 	 _bfd_elf_assign_file_positions_for_non_load.  */
   3649       delay_sh_name_p = true;
   3650     }
   3651 
   3652   if (delay_sh_name_p)
   3653     this_hdr->sh_name = (unsigned int) -1;
   3654   else
   3655     {
   3656       this_hdr->sh_name
   3657 	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   3658 					      name, false);
   3659       if (this_hdr->sh_name == (unsigned int) -1)
   3660 	{
   3661 	  arg->failed = true;
   3662 	  return;
   3663 	}
   3664     }
   3665 
   3666   /* Don't clear sh_flags. Assembler may set additional bits.  */
   3667 
   3668   if ((asect->flags & SEC_ALLOC) != 0
   3669       || asect->user_set_vma)
   3670     this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
   3671   else
   3672     this_hdr->sh_addr = 0;
   3673 
   3674   this_hdr->sh_offset = 0;
   3675   this_hdr->sh_size = asect->size;
   3676   this_hdr->sh_link = 0;
   3677   /* PR 17512: file: 0eb809fe, 8b0535ee.  */
   3678   if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   3679     {
   3680       _bfd_error_handler
   3681 	/* xgettext:c-format */
   3682 	(_("%pB: error: alignment power %d of section `%pA' is too big"),
   3683 	 abfd, asect->alignment_power, asect);
   3684       arg->failed = true;
   3685       return;
   3686     }
   3687   /* Set sh_addralign to the highest power of two given by alignment
   3688      consistent with the section VMA.  Linker scripts can force VMA.  */
   3689   mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
   3690   this_hdr->sh_addralign = mask & -mask;
   3691   /* The sh_entsize and sh_info fields may have been set already by
   3692      copy_private_section_data.  */
   3693 
   3694   this_hdr->bfd_section = asect;
   3695   this_hdr->contents = NULL;
   3696 
   3697   /* If the section type is unspecified, we set it based on
   3698      asect->flags.  */
   3699   if (asect->type != 0)
   3700     sh_type = asect->type;
   3701   else if ((asect->flags & SEC_GROUP) != 0)
   3702     sh_type = SHT_GROUP;
   3703   else
   3704     sh_type = bfd_elf_get_default_section_type (asect->flags);
   3705 
   3706   if (this_hdr->sh_type == SHT_NULL)
   3707     this_hdr->sh_type = sh_type;
   3708   else if (this_hdr->sh_type == SHT_NOBITS
   3709 	   && sh_type == SHT_PROGBITS
   3710 	   && (asect->flags & SEC_ALLOC) != 0)
   3711     {
   3712       /* Warn if we are changing a NOBITS section to PROGBITS, but
   3713 	 allow the link to proceed.  This can happen when users link
   3714 	 non-bss input sections to bss output sections, or emit data
   3715 	 to a bss output section via a linker script.  */
   3716       _bfd_error_handler
   3717 	(_("warning: section `%pA' type changed to PROGBITS"), asect);
   3718       this_hdr->sh_type = sh_type;
   3719     }
   3720 
   3721   switch (this_hdr->sh_type)
   3722     {
   3723     default:
   3724       break;
   3725 
   3726     case SHT_STRTAB:
   3727     case SHT_NOTE:
   3728     case SHT_NOBITS:
   3729     case SHT_PROGBITS:
   3730       break;
   3731 
   3732     case SHT_INIT_ARRAY:
   3733     case SHT_FINI_ARRAY:
   3734     case SHT_PREINIT_ARRAY:
   3735       this_hdr->sh_entsize = bed->s->arch_size / 8;
   3736       break;
   3737 
   3738     case SHT_HASH:
   3739       this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
   3740       break;
   3741 
   3742     case SHT_DYNSYM:
   3743       this_hdr->sh_entsize = bed->s->sizeof_sym;
   3744       break;
   3745 
   3746     case SHT_DYNAMIC:
   3747       this_hdr->sh_entsize = bed->s->sizeof_dyn;
   3748       break;
   3749 
   3750     case SHT_RELA:
   3751       if (get_elf_backend_data (abfd)->may_use_rela_p)
   3752 	this_hdr->sh_entsize = bed->s->sizeof_rela;
   3753       break;
   3754 
   3755      case SHT_REL:
   3756       if (get_elf_backend_data (abfd)->may_use_rel_p)
   3757 	this_hdr->sh_entsize = bed->s->sizeof_rel;
   3758       break;
   3759 
   3760      case SHT_GNU_versym:
   3761       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
   3762       break;
   3763 
   3764      case SHT_GNU_verdef:
   3765       this_hdr->sh_entsize = 0;
   3766       /* objcopy or strip will copy over sh_info, but may not set
   3767 	 cverdefs.  The linker will set cverdefs, but sh_info will be
   3768 	 zero.  */
   3769       if (this_hdr->sh_info == 0)
   3770 	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
   3771       else
   3772 	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
   3773 		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
   3774       break;
   3775 
   3776     case SHT_GNU_verneed:
   3777       this_hdr->sh_entsize = 0;
   3778       /* objcopy or strip will copy over sh_info, but may not set
   3779 	 cverrefs.  The linker will set cverrefs, but sh_info will be
   3780 	 zero.  */
   3781       if (this_hdr->sh_info == 0)
   3782 	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
   3783       else
   3784 	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
   3785 		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
   3786       break;
   3787 
   3788     case SHT_GROUP:
   3789       this_hdr->sh_entsize = GRP_ENTRY_SIZE;
   3790       break;
   3791 
   3792     case SHT_GNU_HASH:
   3793       this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
   3794       break;
   3795     }
   3796 
   3797   if ((asect->flags & SEC_ALLOC) != 0)
   3798     this_hdr->sh_flags |= SHF_ALLOC;
   3799   if ((asect->flags & SEC_READONLY) == 0)
   3800     this_hdr->sh_flags |= SHF_WRITE;
   3801   if ((asect->flags & SEC_CODE) != 0)
   3802     this_hdr->sh_flags |= SHF_EXECINSTR;
   3803   if ((asect->flags & SEC_MERGE) != 0)
   3804     {
   3805       this_hdr->sh_flags |= SHF_MERGE;
   3806       this_hdr->sh_entsize = asect->entsize;
   3807     }
   3808   if ((asect->flags & SEC_STRINGS) != 0)
   3809     {
   3810       this_hdr->sh_flags |= SHF_STRINGS;
   3811       this_hdr->sh_entsize = asect->entsize;
   3812     }
   3813   if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
   3814     this_hdr->sh_flags |= SHF_GROUP;
   3815   if ((asect->flags & SEC_THREAD_LOCAL) != 0)
   3816     {
   3817       this_hdr->sh_flags |= SHF_TLS;
   3818       if (asect->size == 0
   3819 	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
   3820 	{
   3821 	  struct bfd_link_order *o = asect->map_tail.link_order;
   3822 
   3823 	  this_hdr->sh_size = 0;
   3824 	  if (o != NULL)
   3825 	    {
   3826 	      this_hdr->sh_size = o->offset + o->size;
   3827 	      if (this_hdr->sh_size != 0)
   3828 		this_hdr->sh_type = SHT_NOBITS;
   3829 	    }
   3830 	}
   3831     }
   3832   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
   3833     this_hdr->sh_flags |= SHF_EXCLUDE;
   3834 
   3835   if (this_hdr->sh_entsize == 0)
   3836     this_hdr->sh_entsize = asect->entsize;
   3837 
   3838   /* If the section has relocs, set up a section header for the
   3839      SHT_REL[A] section.  If two relocation sections are required for
   3840      this section, it is up to the processor-specific back-end to
   3841      create the other.  */
   3842   if ((asect->flags & SEC_RELOC) != 0)
   3843     {
   3844       /* When doing a relocatable link, create both REL and RELA sections if
   3845 	 needed.  */
   3846       if (arg->link_info
   3847 	  /* Do the normal setup if we wouldn't create any sections here.  */
   3848 	  && esd->rel.count + esd->rela.count > 0
   3849 	  && (bfd_link_relocatable (arg->link_info)
   3850 	      || arg->link_info->emitrelocations))
   3851 	{
   3852 	  if (esd->rel.count && esd->rel.hdr == NULL
   3853 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
   3854 					    false, delay_sh_name_p))
   3855 	    {
   3856 	      arg->failed = true;
   3857 	      return;
   3858 	    }
   3859 	  if (esd->rela.count && esd->rela.hdr == NULL
   3860 	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
   3861 					    true, delay_sh_name_p))
   3862 	    {
   3863 	      arg->failed = true;
   3864 	      return;
   3865 	    }
   3866 	}
   3867       else if (!_bfd_elf_init_reloc_shdr (abfd,
   3868 					  (asect->use_rela_p
   3869 					   ? &esd->rela : &esd->rel),
   3870 					  name,
   3871 					  asect->use_rela_p,
   3872 					  delay_sh_name_p))
   3873 	{
   3874 	  arg->failed = true;
   3875 	  return;
   3876 	}
   3877     }
   3878 
   3879   /* Check for processor-specific section types.  */
   3880   sh_type = this_hdr->sh_type;
   3881   if (bed->elf_backend_fake_sections
   3882       && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
   3883     {
   3884       arg->failed = true;
   3885       return;
   3886     }
   3887 
   3888   if (sh_type == SHT_NOBITS && asect->size != 0)
   3889     {
   3890       /* Don't change the header type from NOBITS if we are being
   3891 	 called for objcopy --only-keep-debug.  */
   3892       this_hdr->sh_type = sh_type;
   3893     }
   3894 }
   3895 
   3896 /* Fill in the contents of a SHT_GROUP section.  Called from
   3897    _bfd_elf_compute_section_file_positions for gas, objcopy, and
   3898    when ELF targets use the generic linker, ld.  Called for ld -r
   3899    from bfd_elf_final_link.  */
   3900 
   3901 void
   3902 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   3903 {
   3904   bool *failedptr = (bool *) failedptrarg;
   3905   asection *elt, *first;
   3906   unsigned char *loc;
   3907   bool gas;
   3908 
   3909   /* Ignore linker created group section.  See elfNN_ia64_object_p in
   3910      elfxx-ia64.c.  */
   3911   if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
   3912       || sec->size == 0
   3913       || *failedptr)
   3914     return;
   3915 
   3916   if (elf_section_data (sec)->this_hdr.sh_info == 0)
   3917     {
   3918       unsigned long symindx = 0;
   3919 
   3920       /* elf_group_id will have been set up by objcopy and the
   3921 	 generic linker.  */
   3922       if (elf_group_id (sec) != NULL)
   3923 	symindx = elf_group_id (sec)->udata.i;
   3924 
   3925       if (symindx == 0)
   3926 	{
   3927 	  /* If called from the assembler, swap_out_syms will have set up
   3928 	     elf_section_syms.
   3929 	     PR 25699: A corrupt input file could contain bogus group info.  */
   3930 	  if (sec->index >= elf_num_section_syms (abfd)
   3931 	      || elf_section_syms (abfd)[sec->index] == NULL)
   3932 	    {
   3933 	      *failedptr = true;
   3934 	      return;
   3935 	    }
   3936 	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
   3937 	}
   3938       elf_section_data (sec)->this_hdr.sh_info = symindx;
   3939     }
   3940   else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
   3941     {
   3942       /* The ELF backend linker sets sh_info to -2 when the group
   3943 	 signature symbol is global, and thus the index can't be
   3944 	 set until all local symbols are output.  */
   3945       asection *igroup;
   3946       struct bfd_elf_section_data *sec_data;
   3947       unsigned long symndx;
   3948       unsigned long extsymoff;
   3949       struct elf_link_hash_entry *h;
   3950 
   3951       /* The point of this little dance to the first SHF_GROUP section
   3952 	 then back to the SHT_GROUP section is that this gets us to
   3953 	 the SHT_GROUP in the input object.  */
   3954       igroup = elf_sec_group (elf_next_in_group (sec));
   3955       sec_data = elf_section_data (igroup);
   3956       symndx = sec_data->this_hdr.sh_info;
   3957       extsymoff = 0;
   3958       if (!elf_bad_symtab (igroup->owner))
   3959 	{
   3960 	  Elf_Internal_Shdr *symtab_hdr;
   3961 
   3962 	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
   3963 	  extsymoff = symtab_hdr->sh_info;
   3964 	}
   3965       h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
   3966       while (h->root.type == bfd_link_hash_indirect
   3967 	     || h->root.type == bfd_link_hash_warning)
   3968 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3969 
   3970       elf_section_data (sec)->this_hdr.sh_info = h->indx;
   3971     }
   3972 
   3973   /* The contents won't be allocated for "ld -r" or objcopy.  */
   3974   gas = true;
   3975   if (sec->contents == NULL)
   3976     {
   3977       gas = false;
   3978       sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
   3979 
   3980       /* Arrange for the section to be written out.  */
   3981       elf_section_data (sec)->this_hdr.contents = sec->contents;
   3982       if (sec->contents == NULL)
   3983 	{
   3984 	  *failedptr = true;
   3985 	  return;
   3986 	}
   3987       sec->alloced = 1;
   3988     }
   3989 
   3990   loc = sec->contents + sec->size;
   3991 
   3992   /* Get the pointer to the first section in the group that gas
   3993      squirreled away here.  objcopy arranges for this to be set to the
   3994      start of the input section group.  */
   3995   first = elt = elf_next_in_group (sec);
   3996 
   3997   /* First element is a flag word.  Rest of section is elf section
   3998      indices for all the sections of the group.  Write them backwards
   3999      just to keep the group in the same order as given in .section
   4000      directives, not that it matters.  */
   4001   while (elt != NULL)
   4002     {
   4003       asection *s;
   4004 
   4005       s = elt;
   4006       if (!gas)
   4007 	s = s->output_section;
   4008       if (s != NULL
   4009 	  && !bfd_is_abs_section (s))
   4010 	{
   4011 	  struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   4012 	  struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
   4013 
   4014 	  if (elf_sec->rel.hdr != NULL
   4015 	      && (gas
   4016 		  || (input_elf_sec->rel.hdr != NULL
   4017 		      && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
   4018 	    {
   4019 	      elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
   4020 	      loc -= 4;
   4021 	      if (loc == sec->contents)
   4022 		break;
   4023 	      H_PUT_32 (abfd, elf_sec->rel.idx, loc);
   4024 	    }
   4025 	  if (elf_sec->rela.hdr != NULL
   4026 	      && (gas
   4027 		  || (input_elf_sec->rela.hdr != NULL
   4028 		      && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
   4029 	    {
   4030 	      elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
   4031 	      loc -= 4;
   4032 	      if (loc == sec->contents)
   4033 		break;
   4034 	      H_PUT_32 (abfd, elf_sec->rela.idx, loc);
   4035 	    }
   4036 	  loc -= 4;
   4037 	  if (loc == sec->contents)
   4038 	    break;
   4039 	  H_PUT_32 (abfd, elf_sec->this_idx, loc);
   4040 	}
   4041       elt = elf_next_in_group (elt);
   4042       if (elt == first)
   4043 	break;
   4044     }
   4045 
   4046   /* We should always get here with loc == sec->contents + 4.  Return
   4047      an error for bogus SHT_GROUP sections.  */
   4048   loc -= 4;
   4049   if (loc != sec->contents)
   4050     {
   4051       /* xgettext:c-format */
   4052       _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
   4053 			  abfd, sec);
   4054       bfd_set_error (bfd_error_bad_value);
   4055       *failedptr = true;
   4056       return;
   4057     }
   4058 
   4059   H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
   4060 }
   4061 
   4062 /* Given NAME, the name of a relocation section stripped of its
   4063    .rel/.rela prefix, return the section in ABFD to which the
   4064    relocations apply.  */
   4065 
   4066 asection *
   4067 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
   4068 {
   4069   /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
   4070      section likely apply to .got.plt or .got section.  */
   4071   if (get_elf_backend_data (abfd)->want_got_plt
   4072       && streq (name, ".plt"))
   4073     {
   4074       asection *sec;
   4075 
   4076       name = ".got.plt";
   4077       sec = bfd_get_section_by_name (abfd, name);
   4078       if (sec != NULL)
   4079 	return sec;
   4080       name = ".got";
   4081     }
   4082 
   4083   return bfd_get_section_by_name (abfd, name);
   4084 }
   4085 
   4086 /* Return the section to which RELOC_SEC applies.  */
   4087 
   4088 static asection *
   4089 elf_get_reloc_section (asection *reloc_sec)
   4090 {
   4091   const char *name;
   4092   unsigned int type;
   4093   bfd *abfd;
   4094   elf_backend_data *bed;
   4095 
   4096   type = elf_section_data (reloc_sec)->this_hdr.sh_type;
   4097   if (type != SHT_REL && type != SHT_RELA)
   4098     return NULL;
   4099 
   4100   /* We look up the section the relocs apply to by name.  */
   4101   name = reloc_sec->name;
   4102   if (!startswith (name, ".rel"))
   4103     return NULL;
   4104   name += 4;
   4105   if (type == SHT_RELA && *name++ != 'a')
   4106     return NULL;
   4107 
   4108   abfd = reloc_sec->owner;
   4109   bed = get_elf_backend_data (abfd);
   4110   return bed->get_reloc_section (abfd, name);
   4111 }
   4112 
   4113 /* Assign all ELF section numbers.  The dummy first section is handled here
   4114    too.  The link/info pointers for the standard section types are filled
   4115    in here too, while we're at it.  LINK_INFO will be 0 when arriving
   4116    here for gas, objcopy, and when using the generic ELF linker.  */
   4117 
   4118 static bool
   4119 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
   4120 {
   4121   struct elf_obj_tdata *t = elf_tdata (abfd);
   4122   asection *sec;
   4123   unsigned int section_number;
   4124   Elf_Internal_Shdr **i_shdrp;
   4125   struct bfd_elf_section_data *d;
   4126   bool need_symtab;
   4127   size_t amt;
   4128 
   4129   section_number = 1;
   4130 
   4131   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
   4132 
   4133   /* SHT_GROUP sections are in relocatable files only.  */
   4134   if (link_info == NULL || !link_info->resolve_section_groups)
   4135     {
   4136       size_t reloc_count = 0;
   4137 
   4138       /* Put SHT_GROUP sections first.  */
   4139       for (sec = abfd->sections; sec != NULL; sec = sec->next)
   4140 	{
   4141 	  d = elf_section_data (sec);
   4142 
   4143 	  if (d->this_hdr.sh_type == SHT_GROUP)
   4144 	    {
   4145 	      if (sec->flags & SEC_LINKER_CREATED)
   4146 		{
   4147 		  /* Remove the linker created SHT_GROUP sections.  */
   4148 		  bfd_section_list_remove (abfd, sec);
   4149 		  abfd->section_count--;
   4150 		}
   4151 	      else
   4152 		d->this_idx = section_number++;
   4153 	    }
   4154 
   4155 	  /* Count relocations.  */
   4156 	  reloc_count += sec->reloc_count;
   4157 	}
   4158 
   4159       /* Set/clear HAS_RELOC depending on whether there are relocations.  */
   4160       if (reloc_count == 0)
   4161 	abfd->flags &= ~HAS_RELOC;
   4162       else
   4163 	abfd->flags |= HAS_RELOC;
   4164     }
   4165 
   4166   for (sec = abfd->sections; sec; sec = sec->next)
   4167     {
   4168       d = elf_section_data (sec);
   4169 
   4170       if (d->this_hdr.sh_type != SHT_GROUP)
   4171 	d->this_idx = section_number++;
   4172       if (d->this_hdr.sh_name != (unsigned int) -1)
   4173 	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
   4174       if (d->rel.hdr)
   4175 	{
   4176 	  d->rel.idx = section_number++;
   4177 	  if (d->rel.hdr->sh_name != (unsigned int) -1)
   4178 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
   4179 	}
   4180       else
   4181 	d->rel.idx = 0;
   4182 
   4183       if (d->rela.hdr)
   4184 	{
   4185 	  d->rela.idx = section_number++;
   4186 	  if (d->rela.hdr->sh_name != (unsigned int) -1)
   4187 	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
   4188 	}
   4189       else
   4190 	d->rela.idx = 0;
   4191     }
   4192 
   4193   need_symtab = (bfd_get_symcount (abfd) > 0
   4194 		 || (link_info == NULL
   4195 		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4196 			 == HAS_RELOC)));
   4197   if (need_symtab)
   4198     {
   4199       elf_onesymtab (abfd) = section_number++;
   4200       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
   4201       if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
   4202 	{
   4203 	  elf_section_list *entry;
   4204 
   4205 	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
   4206 
   4207 	  entry = bfd_zalloc (abfd, sizeof (*entry));
   4208 	  entry->ndx = section_number++;
   4209 	  elf_symtab_shndx_list (abfd) = entry;
   4210 	  entry->hdr.sh_name
   4211 	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   4212 						  ".symtab_shndx", false);
   4213 	  if (entry->hdr.sh_name == (unsigned int) -1)
   4214 	    return false;
   4215 	}
   4216       elf_strtab_sec (abfd) = section_number++;
   4217       _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
   4218     }
   4219 
   4220   elf_shstrtab_sec (abfd) = section_number++;
   4221   _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   4222   elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
   4223 
   4224   if (section_number >= SHN_LORESERVE)
   4225     {
   4226       /* xgettext:c-format */
   4227       _bfd_error_handler (_("%pB: too many sections: %u"),
   4228 			  abfd, section_number);
   4229       return false;
   4230     }
   4231 
   4232   elf_numsections (abfd) = section_number;
   4233   elf_elfheader (abfd)->e_shnum = section_number;
   4234 
   4235   /* Set up the list of section header pointers, in agreement with the
   4236      indices.  */
   4237   amt = section_number * sizeof (Elf_Internal_Shdr *);
   4238   i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
   4239   if (i_shdrp == NULL)
   4240     return false;
   4241 
   4242   i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
   4243 						 sizeof (Elf_Internal_Shdr));
   4244   if (i_shdrp[0] == NULL)
   4245     {
   4246       bfd_release (abfd, i_shdrp);
   4247       return false;
   4248     }
   4249 
   4250   elf_elfsections (abfd) = i_shdrp;
   4251 
   4252   i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
   4253   if (need_symtab)
   4254     {
   4255       i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
   4256       if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
   4257 	{
   4258 	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
   4259 	  BFD_ASSERT (entry != NULL);
   4260 	  i_shdrp[entry->ndx] = & entry->hdr;
   4261 	  entry->hdr.sh_link = elf_onesymtab (abfd);
   4262 	}
   4263       i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
   4264       t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
   4265     }
   4266 
   4267   for (sec = abfd->sections; sec; sec = sec->next)
   4268     {
   4269       asection *s;
   4270 
   4271       d = elf_section_data (sec);
   4272 
   4273       i_shdrp[d->this_idx] = &d->this_hdr;
   4274       if (d->rel.idx != 0)
   4275 	i_shdrp[d->rel.idx] = d->rel.hdr;
   4276       if (d->rela.idx != 0)
   4277 	i_shdrp[d->rela.idx] = d->rela.hdr;
   4278 
   4279       /* Fill in the sh_link and sh_info fields while we're at it.  */
   4280 
   4281       /* sh_link of a reloc section is the section index of the symbol
   4282 	 table.  sh_info is the section index of the section to which
   4283 	 the relocation entries apply.  */
   4284       if (d->rel.idx != 0)
   4285 	{
   4286 	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
   4287 	  d->rel.hdr->sh_info = d->this_idx;
   4288 	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
   4289 	}
   4290       if (d->rela.idx != 0)
   4291 	{
   4292 	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
   4293 	  d->rela.hdr->sh_info = d->this_idx;
   4294 	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
   4295 	}
   4296 
   4297       /* We need to set up sh_link for SHF_LINK_ORDER.  */
   4298       if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
   4299 	{
   4300 	  s = elf_linked_to_section (sec);
   4301 	  /* We can now have a NULL linked section pointer.
   4302 	     This happens when the sh_link field is 0, which is done
   4303 	     when a linked to section is discarded but the linking
   4304 	     section has been retained for some reason.  */
   4305 	  if (s)
   4306 	    {
   4307 	      /* Check discarded linkonce section.  */
   4308 	      if (discarded_section (s))
   4309 		{
   4310 		  asection *kept;
   4311 		  _bfd_error_handler
   4312 		    /* xgettext:c-format */
   4313 		    (_("%pB: sh_link of section `%pA' points to"
   4314 		       " discarded section `%pA' of `%pB'"),
   4315 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   4316 		  /* Point to the kept section if it has the same
   4317 		     size as the discarded one.  */
   4318 		  kept = _bfd_elf_check_kept_section (s, link_info);
   4319 		  if (kept == NULL)
   4320 		    {
   4321 		      bfd_set_error (bfd_error_bad_value);
   4322 		      return false;
   4323 		    }
   4324 		  s = kept;
   4325 		}
   4326 	      /* Handle objcopy. */
   4327 	      else if (s->output_section == NULL)
   4328 		{
   4329 		  _bfd_error_handler
   4330 		    /* xgettext:c-format */
   4331 		    (_("%pB: sh_link of section `%pA' points to"
   4332 		       " removed section `%pA' of `%pB'"),
   4333 		     abfd, d->this_hdr.bfd_section, s, s->owner);
   4334 		  bfd_set_error (bfd_error_bad_value);
   4335 		  return false;
   4336 		}
   4337 	      s = s->output_section;
   4338 	      d->this_hdr.sh_link
   4339 		= _bfd_elf_section_from_bfd_section (abfd, s);
   4340 	    }
   4341 	}
   4342 
   4343       switch (d->this_hdr.sh_type)
   4344 	{
   4345 	case SHT_REL:
   4346 	case SHT_RELA:
   4347 	  /* sh_link is the section index of the symbol table.
   4348 	     sh_info is the section index of the section to which the
   4349 	     relocation entries apply.  */
   4350 	  if (d->this_hdr.sh_link == 0)
   4351 	    {
   4352 	      /* FIXME maybe: If this is a reloc section which we are
   4353 		 treating as a normal section then we likely should
   4354 		 not be assuming its sh_link is .dynsym or .symtab.  */
   4355 	      if ((sec->flags & SEC_ALLOC) != 0)
   4356 		{
   4357 		  s = bfd_get_section_by_name (abfd, ".dynsym");
   4358 		  if (s != NULL)
   4359 		    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4360 		}
   4361 	      else
   4362 		d->this_hdr.sh_link = elf_onesymtab (abfd);
   4363 	    }
   4364 
   4365 	  s = elf_get_reloc_section (sec);
   4366 	  if (s != NULL)
   4367 	    {
   4368 	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
   4369 	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
   4370 	    }
   4371 	  break;
   4372 
   4373 	case SHT_STRTAB:
   4374 	  /* We assume that a section named .stab*str is a stabs
   4375 	     string section.  We look for a section with the same name
   4376 	     but without the trailing ``str'', and set its sh_link
   4377 	     field to point to this section.  */
   4378 	  if (startswith (sec->name, ".stab")
   4379 	      && streq (sec->name + strlen (sec->name) - 3, "str"))
   4380 	    {
   4381 	      size_t len;
   4382 	      char *alc;
   4383 
   4384 	      len = strlen (sec->name);
   4385 	      alc = (char *) bfd_malloc (len - 2);
   4386 	      if (alc == NULL)
   4387 		return false;
   4388 	      memcpy (alc, sec->name, len - 3);
   4389 	      alc[len - 3] = '\0';
   4390 	      s = bfd_get_section_by_name (abfd, alc);
   4391 	      free (alc);
   4392 	      if (s != NULL)
   4393 		{
   4394 		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
   4395 
   4396 		  /* This is a .stab section.  */
   4397 		  elf_section_data (s)->this_hdr.sh_entsize = 12;
   4398 		}
   4399 	    }
   4400 	  break;
   4401 
   4402 	case SHT_DYNAMIC:
   4403 	case SHT_DYNSYM:
   4404 	case SHT_GNU_verneed:
   4405 	case SHT_GNU_verdef:
   4406 	  /* sh_link is the section header index of the string table
   4407 	     used for the dynamic entries, or the symbol table, or the
   4408 	     version strings.  */
   4409 	  s = bfd_get_section_by_name (abfd, ".dynstr");
   4410 	  if (s != NULL)
   4411 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4412 	  break;
   4413 
   4414 	case SHT_GNU_LIBLIST:
   4415 	  /* sh_link is the section header index of the prelink library
   4416 	     list used for the dynamic entries, or the symbol table, or
   4417 	     the version strings.  */
   4418 	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
   4419 					      ? ".dynstr" : ".gnu.libstr"));
   4420 	  if (s != NULL)
   4421 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4422 	  break;
   4423 
   4424 	case SHT_HASH:
   4425 	case SHT_GNU_HASH:
   4426 	case SHT_GNU_versym:
   4427 	  /* sh_link is the section header index of the symbol table
   4428 	     this hash table or version table is for.  */
   4429 	  s = bfd_get_section_by_name (abfd, ".dynsym");
   4430 	  if (s != NULL)
   4431 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
   4432 	  break;
   4433 
   4434 	case SHT_GROUP:
   4435 	  d->this_hdr.sh_link = elf_onesymtab (abfd);
   4436 	}
   4437     }
   4438 
   4439   /* Delay setting sh_name to _bfd_elf_write_object_contents so that
   4440      _bfd_elf_assign_file_positions_for_non_load can convert DWARF
   4441      debug section name from .debug_* to .zdebug_* if needed.  */
   4442 
   4443   return true;
   4444 }
   4445 
   4446 static bool
   4447 sym_is_global (bfd *abfd, asymbol *sym)
   4448 {
   4449   /* If the backend has a special mapping, use it.  */
   4450   elf_backend_data *bed = get_elf_backend_data (abfd);
   4451   if (bed->elf_backend_sym_is_global)
   4452     return (*bed->elf_backend_sym_is_global) (abfd, sym);
   4453 
   4454   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
   4455 	  || bfd_is_und_section (bfd_asymbol_section (sym))
   4456 	  || bfd_is_com_section (bfd_asymbol_section (sym)));
   4457 }
   4458 
   4459 /* Filter global symbols of ABFD to include in the import library.  All
   4460    SYMCOUNT symbols of ABFD can be examined from their pointers in
   4461    SYMS.  Pointers of symbols to keep should be stored contiguously at
   4462    the beginning of that array.
   4463 
   4464    Returns the number of symbols to keep.  */
   4465 
   4466 unsigned int
   4467 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
   4468 				asymbol **syms, long symcount)
   4469 {
   4470   long src_count, dst_count = 0;
   4471 
   4472   for (src_count = 0; src_count < symcount; src_count++)
   4473     {
   4474       asymbol *sym = syms[src_count];
   4475       char *name = (char *) bfd_asymbol_name (sym);
   4476       struct bfd_link_hash_entry *h;
   4477 
   4478       if (!sym_is_global (abfd, sym))
   4479 	continue;
   4480 
   4481       h = bfd_link_hash_lookup (info->hash, name, false, false, false);
   4482       if (h == NULL)
   4483 	continue;
   4484       if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
   4485 	continue;
   4486       if (h->linker_def || h->ldscript_def)
   4487 	continue;
   4488 
   4489       syms[dst_count++] = sym;
   4490     }
   4491 
   4492   syms[dst_count] = NULL;
   4493 
   4494   return dst_count;
   4495 }
   4496 
   4497 /* Don't output symbols for sections that are not going to be output,
   4498    that are duplicates or there is no BFD section.  */
   4499 
   4500 static bool
   4501 ignore_sym (asymbol *sym)
   4502 {
   4503   if (sym == NULL)
   4504     return false;
   4505 
   4506   if (sym->section == NULL)
   4507     return true;
   4508 
   4509   if ((sym->flags & BSF_SECTION_SYM) != 0)
   4510     {
   4511       if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
   4512 	return true;
   4513       /* With ld -r on generic elf targets it is possible to have
   4514 	 multiple section symbols in the output for a given section.
   4515 	 We'd like to get rid of all but the first one.  This drops
   4516 	 them if the first input section is non-zero size, but fails
   4517 	 to do so if the first input section is zero sized.  */
   4518       if (sym->section->output_offset != 0)
   4519 	return true;
   4520     }
   4521 
   4522   return discarded_section (sym->section);
   4523 }
   4524 
   4525 /* Map symbol from it's internal number to the external number, moving
   4526    all local symbols to be at the head of the list.  */
   4527 
   4528 static bool
   4529 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
   4530 {
   4531   unsigned int symcount = bfd_get_symcount (abfd);
   4532   asymbol **syms = bfd_get_outsymbols (abfd);
   4533   asymbol **sect_syms;
   4534   unsigned int num_locals = 0;
   4535   unsigned int num_globals = 0;
   4536   unsigned int max_index = 0;
   4537   unsigned int idx;
   4538   asection *asect;
   4539   asymbol **new_syms;
   4540   size_t amt;
   4541 
   4542 #ifdef DEBUG
   4543   fprintf (stderr, "elf_map_symbols\n");
   4544   fflush (stderr);
   4545 #endif
   4546 
   4547   for (asect = abfd->sections; asect; asect = asect->next)
   4548     {
   4549       if (max_index < asect->index)
   4550 	max_index = asect->index;
   4551     }
   4552 
   4553   max_index++;
   4554   amt = max_index * sizeof (asymbol *);
   4555   sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
   4556   if (sect_syms == NULL)
   4557     return false;
   4558   elf_section_syms (abfd) = sect_syms;
   4559   elf_num_section_syms (abfd) = max_index;
   4560 
   4561   /* Init sect_syms entries for any section symbols we have already
   4562      decided to output.  */
   4563   for (idx = 0; idx < symcount; idx++)
   4564     {
   4565       asymbol *sym = syms[idx];
   4566 
   4567       if ((sym->flags & BSF_SECTION_SYM) != 0
   4568 	  && sym->value == 0
   4569 	  && !ignore_sym (sym)
   4570 	  && !bfd_is_abs_section (sym->section))
   4571 	{
   4572 	  asection *sec = sym->section;
   4573 
   4574 	  if (sec->owner != abfd)
   4575 	    {
   4576 	      sec = sec->output_section;
   4577 	      if (sec == NULL)
   4578 		return false;
   4579 	    }
   4580 
   4581 	  sect_syms[sec->index] = syms[idx];
   4582 	}
   4583     }
   4584 
   4585   /* Classify all of the symbols.  */
   4586   for (idx = 0; idx < symcount; idx++)
   4587     {
   4588       if (ignore_sym (syms[idx]))
   4589 	continue;
   4590       if (sym_is_global (abfd, syms[idx]))
   4591 	num_globals++;
   4592       else
   4593 	num_locals++;
   4594     }
   4595 
   4596   /* We will be adding a section symbol for each normal BFD section.  Most
   4597      sections will already have a section symbol in outsymbols, but
   4598      eg. SHT_GROUP sections will not, and we need the section symbol mapped
   4599      at least in that case.  */
   4600   for (asect = abfd->sections; asect; asect = asect->next)
   4601     {
   4602       asymbol *sym = asect->symbol;
   4603       /* Don't include ignored section symbols.  */
   4604       if (!ignore_sym (sym)
   4605 	  && sect_syms[asect->index] == NULL)
   4606 	{
   4607 	  if (sym_is_global (abfd, asect->symbol))
   4608 	    num_globals++;
   4609 	  else
   4610 	    num_locals++;
   4611 	}
   4612     }
   4613 
   4614   /* Now sort the symbols so the local symbols are first.  */
   4615   amt = (num_locals + num_globals) * sizeof (asymbol *);
   4616   new_syms = (asymbol **) bfd_alloc (abfd, amt);
   4617   if (new_syms == NULL)
   4618     return false;
   4619 
   4620   unsigned int num_globals2 = 0;
   4621   unsigned int num_locals2 = 0;
   4622   for (idx = 0; idx < symcount; idx++)
   4623     {
   4624       asymbol *sym = syms[idx];
   4625       unsigned int i;
   4626 
   4627       if (ignore_sym (sym))
   4628 	continue;
   4629 
   4630       if (sym_is_global (abfd, sym))
   4631 	i = num_locals + num_globals2++;
   4632       else
   4633 	i = num_locals2++;
   4634       new_syms[i] = sym;
   4635       sym->udata.i = i + 1;
   4636     }
   4637   for (asect = abfd->sections; asect; asect = asect->next)
   4638     {
   4639       asymbol *sym = asect->symbol;
   4640       if (!ignore_sym (sym)
   4641 	  && sect_syms[asect->index] == NULL)
   4642 	{
   4643 	  unsigned int i;
   4644 
   4645 	  sect_syms[asect->index] = sym;
   4646 	  if (sym_is_global (abfd, sym))
   4647 	    i = num_locals + num_globals2++;
   4648 	  else
   4649 	    i = num_locals2++;
   4650 	  new_syms[i] = sym;
   4651 	  sym->udata.i = i + 1;
   4652 	}
   4653     }
   4654 
   4655   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
   4656 
   4657   *pnum_locals = num_locals;
   4658   return true;
   4659 }
   4660 
   4661 /* Assign a file position to a section, optionally aligning to the
   4662    required section alignment.  */
   4663 
   4664 file_ptr
   4665 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
   4666 					   file_ptr offset,
   4667 					   bool align,
   4668 					   unsigned char log_file_align)
   4669 {
   4670   if (i_shdrp->sh_addralign > 1)
   4671     {
   4672       file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
   4673 
   4674       if (align)
   4675 	offset = BFD_ALIGN (offset, salign);
   4676       else if (log_file_align)
   4677 	{
   4678 	  /* Heuristic: Cap alignment at log_file_align.  */
   4679 	  file_ptr falign = 1u << log_file_align;
   4680 
   4681 	  offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
   4682 	}
   4683     }
   4684   i_shdrp->sh_offset = offset;
   4685   if (i_shdrp->bfd_section != NULL)
   4686     i_shdrp->bfd_section->filepos = offset;
   4687   if (i_shdrp->sh_type != SHT_NOBITS)
   4688     offset += i_shdrp->sh_size;
   4689   return offset;
   4690 }
   4691 
   4692 /* Compute the file positions we are going to put the sections at, and
   4693    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
   4694    is not NULL, this is being called by the ELF backend linker.  */
   4695 
   4696 bool
   4697 _bfd_elf_compute_section_file_positions (bfd *abfd,
   4698 					 struct bfd_link_info *link_info)
   4699 {
   4700   elf_backend_data *bed = get_elf_backend_data (abfd);
   4701   struct fake_section_arg fsargs;
   4702   bool failed;
   4703   struct elf_strtab_hash *strtab = NULL;
   4704   Elf_Internal_Shdr *shstrtab_hdr;
   4705   bool need_symtab;
   4706 
   4707   if (abfd->output_has_begun)
   4708     return true;
   4709 
   4710   /* Do any elf backend specific processing first.  */
   4711   if (bed->elf_backend_begin_write_processing)
   4712     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
   4713 
   4714   if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
   4715     return false;
   4716 
   4717   fsargs.failed = false;
   4718   fsargs.link_info = link_info;
   4719   bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
   4720   if (fsargs.failed)
   4721     return false;
   4722 
   4723   if (!assign_section_numbers (abfd, link_info))
   4724     return false;
   4725 
   4726   /* The backend linker builds symbol table information itself.  */
   4727   need_symtab = (link_info == NULL
   4728 		 && (bfd_get_symcount (abfd) > 0
   4729 		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
   4730 			 == HAS_RELOC)));
   4731   if (need_symtab)
   4732     {
   4733       /* Non-zero if doing a relocatable link.  */
   4734       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
   4735 
   4736       if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
   4737 	return false;
   4738     }
   4739 
   4740   failed = false;
   4741   if (link_info == NULL)
   4742     {
   4743       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
   4744       if (failed)
   4745 	goto err_free_strtab;
   4746     }
   4747 
   4748   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   4749   /* sh_name was set in init_file_header.  */
   4750   shstrtab_hdr->sh_type = SHT_STRTAB;
   4751   /* sh_flags, sh_addr, sh_entsize, sh_link, sh_info are all zeroed
   4752      when tdata is allocated.  */
   4753   /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4754   /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
   4755   shstrtab_hdr->sh_addralign = 1;
   4756 
   4757   if (!assign_file_positions_except_relocs (abfd, link_info))
   4758     goto err_free_strtab;
   4759 
   4760   if (strtab != NULL)
   4761     {
   4762       file_ptr off;
   4763       Elf_Internal_Shdr *hdr;
   4764 
   4765       off = elf_next_file_pos (abfd);
   4766 
   4767       hdr = & elf_symtab_hdr (abfd);
   4768       off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   4769 
   4770       if (elf_symtab_shndx_list (abfd) != NULL)
   4771 	{
   4772 	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
   4773 	  if (hdr->sh_size != 0)
   4774 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   4775 	  /* FIXME: What about other symtab_shndx sections in the list ?  */
   4776 	}
   4777 
   4778       hdr = &elf_tdata (abfd)->strtab_hdr;
   4779       off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   4780 
   4781       elf_next_file_pos (abfd) = off;
   4782 
   4783       /* Now that we know where the .strtab section goes, write it
   4784 	 out.  */
   4785       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   4786 	  || ! _bfd_elf_strtab_emit (abfd, strtab))
   4787 	goto err_free_strtab;
   4788       _bfd_elf_strtab_free (strtab);
   4789     }
   4790 
   4791   abfd->output_has_begun = true;
   4792   return true;
   4793 
   4794  err_free_strtab:
   4795   if (strtab != NULL)
   4796     _bfd_elf_strtab_free (strtab);
   4797   return false;
   4798 }
   4799 
   4800 /* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
   4801    function effectively returns whether --eh-frame-hdr is given on the
   4802    command line.  After size_dynamic_sections the result reflects
   4803    whether .eh_frame_hdr will actually be output (sizing isn't done
   4804    until ldemul_after_allocation).  */
   4805 
   4806 static asection *
   4807 elf_eh_frame_hdr (const struct bfd_link_info *info)
   4808 {
   4809   if (info != NULL && is_elf_hash_table (info->hash))
   4810     return elf_hash_table (info)->eh_info.hdr_sec;
   4811   return NULL;
   4812 }
   4813 
   4814 /* Make an initial estimate of the size of the program header.  If we
   4815    get the number wrong here, we'll redo section placement.  */
   4816 
   4817 static bfd_size_type
   4818 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
   4819 {
   4820   size_t segs;
   4821   asection *s, *s2;
   4822   elf_backend_data *bed;
   4823 
   4824   /* Assume we will need exactly two PT_LOAD segments: one for text
   4825      and one for data.  */
   4826   segs = 2;
   4827 
   4828   s = bfd_get_section_by_name (abfd, ".interp");
   4829   s2 = bfd_get_section_by_name (abfd, ".dynamic");
   4830   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   4831     {
   4832       ++segs;
   4833     }
   4834 
   4835   if (s2 != NULL && (s2->flags & SEC_LOAD) != 0)
   4836     {
   4837       /* We need a PT_DYNAMIC segment.  */
   4838       ++segs;
   4839     }
   4840 
   4841   if ((s != NULL && (s->flags & SEC_LOAD) != 0) ||
   4842       (s2 != NULL && (s2->flags & SEC_LOAD) != 0))
   4843     {
   4844       /*
   4845        * If either a PT_INTERP or PT_DYNAMIC segment is created,
   4846        * also create a PT_PHDR segment.
   4847        */
   4848       ++segs;
   4849     }
   4850 
   4851   if (info != NULL && info->relro)
   4852     {
   4853       /* We need a PT_GNU_RELRO segment.  */
   4854       ++segs;
   4855     }
   4856 
   4857   if (elf_eh_frame_hdr (info))
   4858     {
   4859       /* We need a PT_GNU_EH_FRAME segment.  */
   4860       ++segs;
   4861     }
   4862 
   4863   if (elf_stack_flags (abfd))
   4864     {
   4865       /* We need a PT_GNU_STACK segment.  */
   4866       ++segs;
   4867     }
   4868 
   4869   if (elf_sframe (abfd))
   4870     {
   4871       /* We need a PT_GNU_SFRAME segment.  */
   4872       ++segs;
   4873     }
   4874 
   4875   s = bfd_get_section_by_name (abfd,
   4876 			       NOTE_GNU_PROPERTY_SECTION_NAME);
   4877   if (s != NULL && s->size != 0)
   4878     {
   4879       /* We need a PT_GNU_PROPERTY segment.  */
   4880       ++segs;
   4881     }
   4882 
   4883   for (s = abfd->sections; s != NULL; s = s->next)
   4884     {
   4885       if ((s->flags & SEC_LOAD) != 0
   4886 	  && elf_section_type (s) == SHT_NOTE)
   4887 	{
   4888 	  unsigned int alignment_power;
   4889 	  /* We need a PT_NOTE segment.  */
   4890 	  ++segs;
   4891 	  /* Try to create just one PT_NOTE segment for all adjacent
   4892 	     loadable SHT_NOTE sections.  gABI requires that within a
   4893 	     PT_NOTE segment (and also inside of each SHT_NOTE section)
   4894 	     each note should have the same alignment.  So we check
   4895 	     whether the sections are correctly aligned.  */
   4896 	  alignment_power = s->alignment_power;
   4897 	  while (s->next != NULL
   4898 		 && s->next->alignment_power == alignment_power
   4899 		 && (s->next->flags & SEC_LOAD) != 0
   4900 		 && elf_section_type (s->next) == SHT_NOTE)
   4901 	    s = s->next;
   4902 	}
   4903     }
   4904 
   4905   for (s = abfd->sections; s != NULL; s = s->next)
   4906     {
   4907       if (s->flags & SEC_THREAD_LOCAL)
   4908 	{
   4909 	  /* We need a PT_TLS segment.  */
   4910 	  ++segs;
   4911 	  break;
   4912 	}
   4913     }
   4914 
   4915   bed = get_elf_backend_data (abfd);
   4916 
   4917   if ((abfd->flags & D_PAGED) != 0
   4918       && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   4919     {
   4920       /* Add a PT_GNU_MBIND segment for each mbind section.  */
   4921       bfd_vma commonpagesize;
   4922       unsigned int page_align_power;
   4923 
   4924       if (info != NULL)
   4925 	commonpagesize = info->commonpagesize;
   4926       else
   4927 	commonpagesize = bed->commonpagesize;
   4928       page_align_power = bfd_log2 (commonpagesize);
   4929       for (s = abfd->sections; s != NULL; s = s->next)
   4930 	if (elf_section_flags (s) & SHF_GNU_MBIND)
   4931 	  {
   4932 	    if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
   4933 	      {
   4934 		_bfd_error_handler
   4935 		  /* xgettext:c-format */
   4936 		  (_("%pB: GNU_MBIND section `%pA' has invalid "
   4937 		     "sh_info field: %d"),
   4938 		   abfd, s, elf_section_data (s)->this_hdr.sh_info);
   4939 		continue;
   4940 	      }
   4941 	    /* Align mbind section to page size.  */
   4942 	    if (s->alignment_power < page_align_power)
   4943 	      s->alignment_power = page_align_power;
   4944 	    segs ++;
   4945 	  }
   4946     }
   4947 
   4948   /* Let the backend count up any program headers it might need.  */
   4949   if (bed->elf_backend_additional_program_headers)
   4950     {
   4951       int a;
   4952 
   4953       a = (*bed->elf_backend_additional_program_headers) (abfd, info);
   4954       if (a == -1)
   4955 	abort ();
   4956       segs += a;
   4957     }
   4958 
   4959   return segs * bed->s->sizeof_phdr;
   4960 }
   4961 
   4962 /* Find the segment that contains the output_section of section.  */
   4963 
   4964 Elf_Internal_Phdr *
   4965 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
   4966 {
   4967   struct elf_segment_map *m;
   4968   Elf_Internal_Phdr *p;
   4969 
   4970   for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
   4971        m != NULL;
   4972        m = m->next, p++)
   4973     {
   4974       int i;
   4975 
   4976       for (i = m->count - 1; i >= 0; i--)
   4977 	if (m->sections[i] == section)
   4978 	  return p;
   4979     }
   4980 
   4981   return NULL;
   4982 }
   4983 
   4984 /* Create a mapping from a set of sections to a program segment.  */
   4985 
   4986 static struct elf_segment_map *
   4987 make_mapping (bfd *abfd,
   4988 	      asection **sections,
   4989 	      unsigned int from,
   4990 	      unsigned int to,
   4991 	      bool phdr)
   4992 {
   4993   struct elf_segment_map *m;
   4994   unsigned int i;
   4995   asection **hdrpp;
   4996   size_t amt;
   4997 
   4998   amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   4999   amt += (to - from) * sizeof (asection *);
   5000   m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5001   if (m == NULL)
   5002     return NULL;
   5003   m->next = NULL;
   5004   m->p_type = PT_LOAD;
   5005   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
   5006     m->sections[i - from] = *hdrpp;
   5007   m->count = to - from;
   5008 
   5009   if (from == 0 && phdr)
   5010     {
   5011       /* Include the headers in the first PT_LOAD segment.  */
   5012       m->includes_filehdr = 1;
   5013       m->includes_phdrs = 1;
   5014     }
   5015 
   5016   return m;
   5017 }
   5018 
   5019 /* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
   5020    on failure.  */
   5021 
   5022 struct elf_segment_map *
   5023 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
   5024 {
   5025   struct elf_segment_map *m;
   5026 
   5027   m = (struct elf_segment_map *) bfd_zalloc (abfd,
   5028 					     sizeof (struct elf_segment_map));
   5029   if (m == NULL)
   5030     return NULL;
   5031   m->next = NULL;
   5032   m->p_type = PT_DYNAMIC;
   5033   m->count = 1;
   5034   m->sections[0] = dynsec;
   5035 
   5036   return m;
   5037 }
   5038 
   5039 /* Possibly add or remove segments from the segment map.  */
   5040 
   5041 static bool
   5042 elf_modify_segment_map (bfd *abfd,
   5043 			struct bfd_link_info *info,
   5044 			bool remove_empty_load)
   5045 {
   5046   struct elf_segment_map **m;
   5047   elf_backend_data *bed;
   5048 
   5049   /* The placement algorithm assumes that non allocated sections are
   5050      not in PT_LOAD segments.  We ensure this here by removing such
   5051      sections from the segment map.  We also remove excluded
   5052      sections.  Finally, any PT_LOAD segment without sections is
   5053      removed.  */
   5054   m = &elf_seg_map (abfd);
   5055   while (*m)
   5056     {
   5057       unsigned int i, new_count;
   5058 
   5059       for (new_count = 0, i = 0; i < (*m)->count; i++)
   5060 	{
   5061 	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
   5062 	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
   5063 		  || (*m)->p_type != PT_LOAD))
   5064 	    {
   5065 	      (*m)->sections[new_count] = (*m)->sections[i];
   5066 	      new_count++;
   5067 	    }
   5068 	}
   5069       (*m)->count = new_count;
   5070 
   5071       if (remove_empty_load
   5072 	  && (*m)->p_type == PT_LOAD
   5073 	  && (*m)->count == 0
   5074 	  && !(*m)->includes_phdrs)
   5075 	*m = (*m)->next;
   5076       else
   5077 	m = &(*m)->next;
   5078     }
   5079 
   5080   bed = get_elf_backend_data (abfd);
   5081   if (bed->elf_backend_modify_segment_map != NULL)
   5082     {
   5083       if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
   5084 	return false;
   5085     }
   5086 
   5087   return true;
   5088 }
   5089 
   5090 #define IS_TBSS(s) \
   5091   ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
   5092 
   5093 /* Set up a mapping from BFD sections to program segments.  Update
   5094    NEED_LAYOUT if the section layout is changed.  */
   5095 
   5096 bool
   5097 bfd_elf_map_sections_to_segments (bfd *abfd,
   5098 				  struct bfd_link_info *info,
   5099 				  bool *need_layout)
   5100 {
   5101   unsigned int count;
   5102   struct elf_segment_map *m;
   5103   asection **sections = NULL;
   5104   elf_backend_data *bed = get_elf_backend_data (abfd);
   5105   bool no_user_phdrs;
   5106 
   5107   no_user_phdrs = elf_seg_map (abfd) == NULL;
   5108 
   5109   if (info != NULL)
   5110     {
   5111       info->user_phdrs = !no_user_phdrs;
   5112 
   5113       /* Size the relative relocations if DT_RELR is enabled.  */
   5114       if (info->enable_dt_relr
   5115 	  && need_layout != NULL
   5116 	  && bed->size_relative_relocs
   5117 	  && !bed->size_relative_relocs (info, need_layout))
   5118 	info->callbacks->fatal
   5119 	  (_("%P: failed to size relative relocations\n"));
   5120     }
   5121 
   5122   if (no_user_phdrs && bfd_count_sections (abfd) != 0)
   5123     {
   5124       asection *s;
   5125       unsigned int i;
   5126       struct elf_segment_map *mfirst;
   5127       struct elf_segment_map **pm;
   5128       asection *last_hdr;
   5129       bfd_vma last_size;
   5130       unsigned int hdr_index;
   5131       bfd_vma maxpagesize;
   5132       asection **hdrpp;
   5133       bool phdr_in_segment;
   5134       bool writable;
   5135       bool executable;
   5136       unsigned int tls_count = 0;
   5137       asection *first_tls = NULL;
   5138       asection *first_mbind = NULL;
   5139       asection *dynsec, *eh_frame_hdr;
   5140       asection *sframe;
   5141       size_t amt;
   5142       bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
   5143       bfd_size_type phdr_size;  /* Octets/bytes.  */
   5144       unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5145 
   5146       /* Select the allocated sections, and sort them.  */
   5147 
   5148       amt = bfd_count_sections (abfd) * sizeof (asection *);
   5149       sections = (asection **) bfd_malloc (amt);
   5150       if (sections == NULL)
   5151 	goto error_return;
   5152 
   5153       /* Calculate top address, avoiding undefined behaviour of shift
   5154 	 left operator when shift count is equal to size of type
   5155 	 being shifted.  */
   5156       addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
   5157       addr_mask = (addr_mask << 1) + 1;
   5158 
   5159       i = 0;
   5160       for (s = abfd->sections; s != NULL; s = s->next)
   5161 	{
   5162 	  if ((s->flags & SEC_ALLOC) != 0)
   5163 	    {
   5164 	      /* target_index is unused until bfd_elf_final_link
   5165 		 starts output of section symbols.  Use it to make
   5166 		 qsort stable.  */
   5167 	      s->target_index = i;
   5168 	      sections[i] = s;
   5169 	      ++i;
   5170 	      /* A wrapping section potentially clashes with header.  */
   5171 	      if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
   5172 		wrap_to = (s->lma + s->size / opb) & addr_mask;
   5173 	    }
   5174 	}
   5175       BFD_ASSERT (i <= bfd_count_sections (abfd));
   5176       count = i;
   5177 
   5178       qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
   5179 
   5180       phdr_size = elf_program_header_size (abfd);
   5181       if (phdr_size == (bfd_size_type) -1)
   5182 	phdr_size = get_program_header_size (abfd, info);
   5183       phdr_size += bed->s->sizeof_ehdr;
   5184       /* phdr_size is compared to LMA values which are in bytes.  */
   5185       phdr_size /= opb;
   5186       if (info != NULL)
   5187 	maxpagesize = info->maxpagesize;
   5188       else
   5189 	maxpagesize = bed->maxpagesize;
   5190       if (maxpagesize == 0)
   5191 	maxpagesize = 1;
   5192       phdr_in_segment = info != NULL && info->load_phdrs;
   5193       if (count != 0
   5194 	  && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
   5195 	      >= (phdr_size & (maxpagesize - 1))))
   5196 	/* For compatibility with old scripts that may not be using
   5197 	   SIZEOF_HEADERS, add headers when it looks like space has
   5198 	   been left for them.  */
   5199 	phdr_in_segment = true;
   5200 
   5201       /* Build the mapping.  */
   5202       mfirst = NULL;
   5203       pm = &mfirst;
   5204 
   5205       /* If we have a .interp section, then create a PT_PHDR segment for
   5206 	 the program headers and a PT_INTERP segment for the .interp
   5207 	 section.  */
   5208       s = bfd_get_section_by_name (abfd, ".interp");
   5209       if (s != NULL && (s->flags & SEC_LOAD) == 0)
   5210       if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
   5211 	s = NULL;
   5212       dynsec = bfd_get_section_by_name (abfd, ".dynamic");
   5213       if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0)
   5214 	dynsec = NULL;
   5215 
   5216       if (s != NULL || dynsec != NULL)
   5217 	{
   5218 	  amt = sizeof (struct elf_segment_map);
   5219 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5220 	  if (m == NULL)
   5221 	    goto error_return;
   5222 	  m->next = NULL;
   5223 	  m->p_type = PT_PHDR;
   5224 	  m->p_flags = PF_R;
   5225 	  m->p_flags_valid = 1;
   5226 	  m->includes_phdrs = 1;
   5227 	  phdr_in_segment = true;
   5228 	  *pm = m;
   5229 	  pm = &m->next;
   5230 	}
   5231 
   5232       if (s != NULL)
   5233 	{
   5234 	  amt = sizeof (struct elf_segment_map);
   5235 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5236 	  if (m == NULL)
   5237 	    goto error_return;
   5238 	  m->next = NULL;
   5239 	  m->p_type = PT_INTERP;
   5240 	  m->count = 1;
   5241 	  m->sections[0] = s;
   5242 
   5243 	  *pm = m;
   5244 	  pm = &m->next;
   5245 	}
   5246 
   5247       /* Look through the sections.  We put sections in the same program
   5248 	 segment when the start of the second section can be placed within
   5249 	 a few bytes of the end of the first section.  */
   5250       last_hdr = NULL;
   5251       last_size = 0;
   5252       hdr_index = 0;
   5253       writable = false;
   5254       executable = false;
   5255 
   5256       if ((abfd->flags & D_PAGED) == 0)
   5257 	phdr_in_segment = false;
   5258 
   5259       /* Deal with -Ttext or something similar such that the first section
   5260 	 is not adjacent to the program headers.  This is an
   5261 	 approximation, since at this point we don't know exactly how many
   5262 	 program headers we will need.  */
   5263       if (phdr_in_segment && count > 0)
   5264 	{
   5265 	  bfd_vma phdr_lma;  /* Bytes.  */
   5266 	  bool separate_phdr = false;
   5267 
   5268 	  phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
   5269 	  if (info != NULL
   5270 	      && info->separate_code
   5271 	      && (sections[0]->flags & SEC_CODE) != 0)
   5272 	    {
   5273 	      /* If data sections should be separate from code and
   5274 		 thus not executable, and the first section is
   5275 		 executable then put the file and program headers in
   5276 		 their own PT_LOAD.  */
   5277 	      if (!info->one_rosegment)
   5278 		separate_phdr = true;
   5279 
   5280 	      if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
   5281 		   == (sections[0]->lma & addr_mask & -maxpagesize)))
   5282 		{
   5283 		  /* The file and program headers are currently on the
   5284 		     same page as the first section.  Put them on the
   5285 		     previous page if we can.  */
   5286 		  if (phdr_lma >= maxpagesize)
   5287 		    phdr_lma -= maxpagesize;
   5288 		  else
   5289 		    separate_phdr = false;
   5290 		}
   5291 	    }
   5292 	  if ((sections[0]->lma & addr_mask) < phdr_lma
   5293 	      || (sections[0]->lma & addr_mask) < phdr_size)
   5294 	    /* If file and program headers would be placed at the end
   5295 	       of memory then it's probably better to omit them.  */
   5296 	    phdr_in_segment = false;
   5297 	  else if (phdr_lma < wrap_to)
   5298 	    /* If a section wraps around to where we'll be placing
   5299 	       file and program headers, then the headers will be
   5300 	       overwritten.  */
   5301 	    phdr_in_segment = false;
   5302 	  else if (separate_phdr)
   5303 	    {
   5304 	      m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
   5305 	      if (m == NULL)
   5306 		goto error_return;
   5307 	      m->p_paddr = phdr_lma * opb;
   5308 	      m->p_vaddr_offset
   5309 		= (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
   5310 	      m->p_paddr_valid = 1;
   5311 	      *pm = m;
   5312 	      pm = &m->next;
   5313 	      phdr_in_segment = false;
   5314 	    }
   5315 	}
   5316 
   5317       for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
   5318 	{
   5319 	  asection *hdr;
   5320 	  bool new_segment;
   5321 
   5322 	  hdr = *hdrpp;
   5323 
   5324 	  /* See if this section and the last one will fit in the same
   5325 	     segment.  */
   5326 
   5327 	  if (last_hdr == NULL)
   5328 	    {
   5329 	      /* If we don't have a segment yet, then we don't need a new
   5330 		 one (we build the last one after this loop).  */
   5331 	      new_segment = false;
   5332 	    }
   5333 	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
   5334 	    {
   5335 	      /* If this section has a different relation between the
   5336 		 virtual address and the load address, then we need a new
   5337 		 segment.  */
   5338 	      new_segment = true;
   5339 	    }
   5340 	  else if (hdr->lma < last_hdr->lma + last_size
   5341 		   || last_hdr->lma + last_size < last_hdr->lma)
   5342 	    {
   5343 	      /* If this section has a load address that makes it overlap
   5344 		 the previous section, then we need a new segment.  */
   5345 	      new_segment = true;
   5346 	    }
   5347 	  else if ((abfd->flags & D_PAGED) != 0
   5348 		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
   5349 		       == (hdr->lma & -maxpagesize)))
   5350 	    {
   5351 	      /* If we are demand paged then we can't map two disk
   5352 		 pages onto the same memory page.  */
   5353 	      new_segment = false;
   5354 	    }
   5355 	  /* In the next test we have to be careful when last_hdr->lma is close
   5356 	     to the end of the address space.  If the aligned address wraps
   5357 	     around to the start of the address space, then there are no more
   5358 	     pages left in memory and it is OK to assume that the current
   5359 	     section can be included in the current segment.  */
   5360 	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5361 		    + maxpagesize > last_hdr->lma)
   5362 		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
   5363 		       + maxpagesize <= hdr->lma))
   5364 	    {
   5365 	      /* If putting this section in this segment would force us to
   5366 		 skip a page in the segment, then we need a new segment.  */
   5367 	      new_segment = true;
   5368 	    }
   5369 	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
   5370 		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
   5371 	    {
   5372 	      /* We don't want to put a loaded section after a
   5373 		 nonloaded (ie. bss style) section in the same segment
   5374 		 as that will force the non-loaded section to be loaded.
   5375 		 Consider .tbss sections as loaded for this purpose.  */
   5376 	      new_segment = true;
   5377 	    }
   5378 	  else if ((abfd->flags & D_PAGED) == 0)
   5379 	    {
   5380 	      /* If the file is not demand paged, which means that we
   5381 		 don't require the sections to be correctly aligned in the
   5382 		 file, then there is no other reason for a new segment.  */
   5383 	      new_segment = false;
   5384 	    }
   5385 	  else if (info != NULL
   5386 		   && info->separate_code
   5387 		   && executable != ((hdr->flags & SEC_CODE) != 0))
   5388 	    {
   5389 	      new_segment = true;
   5390 	    }
   5391 	  else if (! writable
   5392 		   && (hdr->flags & SEC_READONLY) == 0)
   5393 	    {
   5394 	      /* We don't want to put a writable section in a read only
   5395 		 segment.  */
   5396 	      new_segment = true;
   5397 	    }
   5398 	  else
   5399 	    {
   5400 	      /* Otherwise, we can use the same segment.  */
   5401 	      new_segment = false;
   5402 	    }
   5403 
   5404 	  /* Allow interested parties a chance to override our decision.  */
   5405 	  if (last_hdr != NULL
   5406 	      && info != NULL
   5407 	      && info->callbacks->override_segment_assignment != NULL)
   5408 	    new_segment
   5409 	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
   5410 							      last_hdr,
   5411 							      new_segment);
   5412 
   5413 	  if (! new_segment)
   5414 	    {
   5415 	      if ((hdr->flags & SEC_READONLY) == 0)
   5416 		writable = true;
   5417 	      if ((hdr->flags & SEC_CODE) != 0)
   5418 		executable = true;
   5419 	      last_hdr = hdr;
   5420 	      /* .tbss sections effectively have zero size.  */
   5421 	      last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5422 	      continue;
   5423 	    }
   5424 
   5425 	  /* We need a new program segment.  We must create a new program
   5426 	     header holding all the sections from hdr_index until hdr.  */
   5427 
   5428 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5429 	  if (m == NULL)
   5430 	    goto error_return;
   5431 
   5432 	  *pm = m;
   5433 	  pm = &m->next;
   5434 
   5435 	  if ((hdr->flags & SEC_READONLY) == 0)
   5436 	    writable = true;
   5437 	  else
   5438 	    writable = false;
   5439 
   5440 	  if ((hdr->flags & SEC_CODE) == 0)
   5441 	    executable = false;
   5442 	  else
   5443 	    executable = true;
   5444 
   5445 	  last_hdr = hdr;
   5446 	  /* .tbss sections effectively have zero size.  */
   5447 	  last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
   5448 	  hdr_index = i;
   5449 	  phdr_in_segment = false;
   5450 	}
   5451 
   5452       /* Create a final PT_LOAD program segment, but not if it's just
   5453 	 for .tbss.  */
   5454       if (last_hdr != NULL
   5455 	  && (i - hdr_index != 1
   5456 	      || !IS_TBSS (last_hdr)))
   5457 	{
   5458 	  m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
   5459 	  if (m == NULL)
   5460 	    goto error_return;
   5461 
   5462 	  *pm = m;
   5463 	  pm = &m->next;
   5464 	}
   5465 
   5466       /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
   5467       if (dynsec != NULL)
   5468 	{
   5469 	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
   5470 	  if (m == NULL)
   5471 	    goto error_return;
   5472 	  *pm = m;
   5473 	  pm = &m->next;
   5474 	}
   5475 
   5476       /* For each batch of consecutive loadable SHT_NOTE  sections,
   5477 	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
   5478 	 because if we link together nonloadable .note sections and
   5479 	 loadable .note sections, we will generate two .note sections
   5480 	 in the output file.  */
   5481       for (s = abfd->sections; s != NULL; s = s->next)
   5482 	{
   5483 	  if ((s->flags & SEC_LOAD) != 0
   5484 	      && elf_section_type (s) == SHT_NOTE)
   5485 	    {
   5486 	      asection *s2;
   5487 	      unsigned int alignment_power = s->alignment_power;
   5488 
   5489 	      count = 1;
   5490 	      for (s2 = s; s2->next != NULL; s2 = s2->next)
   5491 		{
   5492 		  if (s2->next->alignment_power == alignment_power
   5493 		      && (s2->next->flags & SEC_LOAD) != 0
   5494 		      && elf_section_type (s2->next) == SHT_NOTE
   5495 		      && align_power (s2->lma + s2->size / opb,
   5496 				      alignment_power)
   5497 		      == s2->next->lma)
   5498 		    count++;
   5499 		  else
   5500 		    break;
   5501 		}
   5502 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5503 	      amt += count * sizeof (asection *);
   5504 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5505 	      if (m == NULL)
   5506 		goto error_return;
   5507 	      m->next = NULL;
   5508 	      m->p_type = PT_NOTE;
   5509 	      m->count = count;
   5510 	      while (count > 1)
   5511 		{
   5512 		  m->sections[m->count - count--] = s;
   5513 		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5514 		  s = s->next;
   5515 		}
   5516 	      m->sections[m->count - 1] = s;
   5517 	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
   5518 	      *pm = m;
   5519 	      pm = &m->next;
   5520 	    }
   5521 	  if (s->flags & SEC_THREAD_LOCAL)
   5522 	    {
   5523 	      if (! tls_count)
   5524 		first_tls = s;
   5525 	      tls_count++;
   5526 	    }
   5527 	  if (first_mbind == NULL
   5528 	      && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
   5529 	    first_mbind = s;
   5530 	}
   5531 
   5532       /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
   5533       if (tls_count > 0)
   5534 	{
   5535 	  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   5536 	  amt += tls_count * sizeof (asection *);
   5537 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5538 	  if (m == NULL)
   5539 	    goto error_return;
   5540 	  m->next = NULL;
   5541 	  m->p_type = PT_TLS;
   5542 	  m->count = tls_count;
   5543 	  /* Mandated PF_R.  */
   5544 	  m->p_flags = PF_R;
   5545 	  m->p_flags_valid = 1;
   5546 	  s = first_tls;
   5547 	  for (i = 0; i < tls_count; ++i)
   5548 	    {
   5549 	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
   5550 		{
   5551 		  _bfd_error_handler
   5552 		    (_("%pB: TLS sections are not adjacent:"), abfd);
   5553 		  s = first_tls;
   5554 		  i = 0;
   5555 		  while (i < tls_count)
   5556 		    {
   5557 		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
   5558 			{
   5559 			  _bfd_error_handler (_("	    TLS: %pA"), s);
   5560 			  i++;
   5561 			}
   5562 		      else
   5563 			_bfd_error_handler (_("	non-TLS: %pA"), s);
   5564 		      s = s->next;
   5565 		    }
   5566 		  bfd_set_error (bfd_error_bad_value);
   5567 		  goto error_return;
   5568 		}
   5569 	      m->sections[i] = s;
   5570 	      s = s->next;
   5571 	    }
   5572 
   5573 	  *pm = m;
   5574 	  pm = &m->next;
   5575 	}
   5576 
   5577       if (first_mbind
   5578 	  && (abfd->flags & D_PAGED) != 0
   5579 	  && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
   5580 	for (s = first_mbind; s != NULL; s = s->next)
   5581 	  if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
   5582 	      && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
   5583 	    {
   5584 	      /* Mandated PF_R.  */
   5585 	      unsigned long p_flags = PF_R;
   5586 	      if ((s->flags & SEC_READONLY) == 0)
   5587 		p_flags |= PF_W;
   5588 	      if ((s->flags & SEC_CODE) != 0)
   5589 		p_flags |= PF_X;
   5590 
   5591 	      amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5592 	      m = bfd_zalloc (abfd, amt);
   5593 	      if (m == NULL)
   5594 		goto error_return;
   5595 	      m->next = NULL;
   5596 	      m->p_type = (PT_GNU_MBIND_LO
   5597 			   + elf_section_data (s)->this_hdr.sh_info);
   5598 	      m->count = 1;
   5599 	      m->p_flags_valid = 1;
   5600 	      m->sections[0] = s;
   5601 	      m->p_flags = p_flags;
   5602 
   5603 	      *pm = m;
   5604 	      pm = &m->next;
   5605 	    }
   5606 
   5607       s = bfd_get_section_by_name (abfd,
   5608 				   NOTE_GNU_PROPERTY_SECTION_NAME);
   5609       if (s != NULL && s->size != 0)
   5610 	{
   5611 	  amt = sizeof (struct elf_segment_map) + sizeof (asection *);
   5612 	  m = bfd_zalloc (abfd, amt);
   5613 	  if (m == NULL)
   5614 	    goto error_return;
   5615 	  m->next = NULL;
   5616 	  m->p_type = PT_GNU_PROPERTY;
   5617 	  m->count = 1;
   5618 	  m->p_flags_valid = 1;
   5619 	  m->sections[0] = s;
   5620 	  m->p_flags = PF_R;
   5621 	  *pm = m;
   5622 	  pm = &m->next;
   5623 	}
   5624 
   5625       /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
   5626 	 segment.  */
   5627       eh_frame_hdr = elf_eh_frame_hdr (info);
   5628       if (eh_frame_hdr != NULL
   5629 	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
   5630 	{
   5631 	  amt = sizeof (struct elf_segment_map);
   5632 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5633 	  if (m == NULL)
   5634 	    goto error_return;
   5635 	  m->next = NULL;
   5636 	  m->p_type = PT_GNU_EH_FRAME;
   5637 	  m->count = 1;
   5638 	  m->sections[0] = eh_frame_hdr->output_section;
   5639 
   5640 	  *pm = m;
   5641 	  pm = &m->next;
   5642 	}
   5643 
   5644       /* If there is a .sframe section, throw in a PT_GNU_SFRAME
   5645 	 segment.  */
   5646       sframe = elf_sframe (abfd);
   5647       if (sframe != NULL
   5648 	  && (sframe->output_section->flags & SEC_LOAD) != 0
   5649 	  && sframe->size != 0)
   5650 	{
   5651 	  amt = sizeof (struct elf_segment_map);
   5652 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5653 	  if (m == NULL)
   5654 	    goto error_return;
   5655 	  m->next = NULL;
   5656 	  m->p_type = PT_GNU_SFRAME;
   5657 	  m->count = 1;
   5658 	  m->sections[0] = sframe->output_section;
   5659 
   5660 	  *pm = m;
   5661 	  pm = &m->next;
   5662 	}
   5663 
   5664       if (elf_stack_flags (abfd))
   5665 	{
   5666 	  amt = sizeof (struct elf_segment_map);
   5667 	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5668 	  if (m == NULL)
   5669 	    goto error_return;
   5670 	  m->next = NULL;
   5671 	  m->p_type = PT_GNU_STACK;
   5672 	  m->p_flags = elf_stack_flags (abfd);
   5673 	  m->p_align = bed->stack_align;
   5674 	  m->p_flags_valid = 1;
   5675 	  m->p_align_valid = m->p_align != 0;
   5676 	  if (info->stacksize > 0)
   5677 	    {
   5678 	      m->p_size = info->stacksize;
   5679 	      m->p_size_valid = 1;
   5680 	    }
   5681 
   5682 	  *pm = m;
   5683 	  pm = &m->next;
   5684 	}
   5685 
   5686       if (info != NULL && info->relro)
   5687 	{
   5688 	  for (m = mfirst; m != NULL; m = m->next)
   5689 	    {
   5690 	      if (m->p_type == PT_LOAD
   5691 		  && m->count != 0
   5692 		  && m->sections[0]->vma >= info->relro_start
   5693 		  && m->sections[0]->vma < info->relro_end)
   5694 		{
   5695 		  i = m->count;
   5696 		  while (--i != (unsigned) -1)
   5697 		    {
   5698 		      if (m->sections[i]->size > 0
   5699 			  && (m->sections[i]->flags & SEC_LOAD) != 0
   5700 			  && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
   5701 			break;
   5702 		    }
   5703 
   5704 		  if (i != (unsigned) -1)
   5705 		    break;
   5706 		}
   5707 	    }
   5708 
   5709 	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
   5710 	  if (m != NULL)
   5711 	    {
   5712 	      amt = sizeof (struct elf_segment_map);
   5713 	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   5714 	      if (m == NULL)
   5715 		goto error_return;
   5716 	      m->next = NULL;
   5717 	      m->p_type = PT_GNU_RELRO;
   5718 	      *pm = m;
   5719 	      pm = &m->next;
   5720 	    }
   5721 	}
   5722 
   5723       free (sections);
   5724       elf_seg_map (abfd) = mfirst;
   5725     }
   5726 
   5727   if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
   5728     return false;
   5729 
   5730   for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
   5731     ++count;
   5732   elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
   5733 
   5734   return true;
   5735 
   5736  error_return:
   5737   free (sections);
   5738   return false;
   5739 }
   5740 
   5741 /* Sort sections by address.  */
   5742 
   5743 static int
   5744 elf_sort_sections (const void *arg1, const void *arg2)
   5745 {
   5746   const asection *sec1 = *(const asection **) arg1;
   5747   const asection *sec2 = *(const asection **) arg2;
   5748   bfd_size_type size1, size2;
   5749 
   5750   /* Sort by LMA first, since this is the address used to
   5751      place the section into a segment.  */
   5752   if (sec1->lma < sec2->lma)
   5753     return -1;
   5754   else if (sec1->lma > sec2->lma)
   5755     return 1;
   5756 
   5757   /* Then sort by VMA.  Normally the LMA and the VMA will be
   5758      the same, and this will do nothing.  */
   5759   if (sec1->vma < sec2->vma)
   5760     return -1;
   5761   else if (sec1->vma > sec2->vma)
   5762     return 1;
   5763 
   5764   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
   5765 
   5766 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
   5767 		  && (x)->size != 0)
   5768 
   5769   if (TOEND (sec1))
   5770     {
   5771       if (!TOEND (sec2))
   5772 	return 1;
   5773     }
   5774   else if (TOEND (sec2))
   5775     return -1;
   5776 
   5777 #undef TOEND
   5778 
   5779   /* Sort by size, to put zero sized sections
   5780      before others at the same address.  */
   5781 
   5782   size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
   5783   size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
   5784 
   5785   if (size1 < size2)
   5786     return -1;
   5787   if (size1 > size2)
   5788     return 1;
   5789 
   5790   return sec1->target_index - sec2->target_index;
   5791 }
   5792 
   5793 /* This qsort comparison functions sorts PT_LOAD segments first and
   5794    by p_paddr, for assign_file_positions_for_load_sections.  */
   5795 
   5796 static int
   5797 elf_sort_segments (const void *arg1, const void *arg2)
   5798 {
   5799   const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
   5800   const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
   5801 
   5802   if (m1->p_type != m2->p_type)
   5803     {
   5804       if (m1->p_type == PT_NULL)
   5805 	return 1;
   5806       if (m2->p_type == PT_NULL)
   5807 	return -1;
   5808       return m1->p_type < m2->p_type ? -1 : 1;
   5809     }
   5810   if (m1->includes_filehdr != m2->includes_filehdr)
   5811     return m1->includes_filehdr ? -1 : 1;
   5812   if (m1->no_sort_lma != m2->no_sort_lma)
   5813     return m1->no_sort_lma ? -1 : 1;
   5814   if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
   5815     {
   5816       bfd_vma lma1, lma2;  /* Octets.  */
   5817       lma1 = 0;
   5818       if (m1->p_paddr_valid)
   5819 	lma1 = m1->p_paddr;
   5820       else if (m1->count != 0)
   5821 	{
   5822 	  unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
   5823 						  m1->sections[0]);
   5824 	  lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
   5825 	}
   5826       lma2 = 0;
   5827       if (m2->p_paddr_valid)
   5828 	lma2 = m2->p_paddr;
   5829       else if (m2->count != 0)
   5830 	{
   5831 	  unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
   5832 						  m2->sections[0]);
   5833 	  lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
   5834 	}
   5835       if (lma1 != lma2)
   5836 	return lma1 < lma2 ? -1 : 1;
   5837     }
   5838   if (m1->idx != m2->idx)
   5839     return m1->idx < m2->idx ? -1 : 1;
   5840   return 0;
   5841 }
   5842 
   5843 /* Ian Lance Taylor writes:
   5844 
   5845    We shouldn't be using % with a negative signed number.  That's just
   5846    not good.  We have to make sure either that the number is not
   5847    negative, or that the number has an unsigned type.  When the types
   5848    are all the same size they wind up as unsigned.  When file_ptr is a
   5849    larger signed type, the arithmetic winds up as signed long long,
   5850    which is wrong.
   5851 
   5852    What we're trying to say here is something like ``increase OFF by
   5853    the least amount that will cause it to be equal to the VMA modulo
   5854    the page size.''  */
   5855 /* In other words, something like:
   5856 
   5857    vma_offset = m->sections[0]->vma % bed->maxpagesize;
   5858    off_offset = off % bed->maxpagesize;
   5859    if (vma_offset < off_offset)
   5860      adjustment = vma_offset + bed->maxpagesize - off_offset;
   5861    else
   5862      adjustment = vma_offset - off_offset;
   5863 
   5864    which can be collapsed into the expression below.  */
   5865 
   5866 static file_ptr
   5867 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
   5868 {
   5869   /* PR binutils/16199: Handle an alignment of zero.  */
   5870   if (maxpagesize == 0)
   5871     maxpagesize = 1;
   5872   return ((vma - off) % maxpagesize);
   5873 }
   5874 
   5875 static void
   5876 print_segment_map (const struct elf_segment_map *m)
   5877 {
   5878   unsigned int j;
   5879   const char *pt = get_segment_type (m->p_type);
   5880   char buf[32];
   5881 
   5882   if (pt == NULL)
   5883     {
   5884       if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
   5885 	sprintf (buf, "LOPROC+%7.7x",
   5886 		 (unsigned int) (m->p_type - PT_LOPROC));
   5887       else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
   5888 	sprintf (buf, "LOOS+%7.7x",
   5889 		 (unsigned int) (m->p_type - PT_LOOS));
   5890       else
   5891 	snprintf (buf, sizeof (buf), "%8.8x",
   5892 		  (unsigned int) m->p_type);
   5893       pt = buf;
   5894     }
   5895   fflush (stdout);
   5896   fprintf (stderr, "%s:", pt);
   5897   for (j = 0; j < m->count; j++)
   5898     fprintf (stderr, " %s", m->sections [j]->name);
   5899   putc ('\n',stderr);
   5900   fflush (stderr);
   5901 }
   5902 
   5903 /* Assign file positions to the sections based on the mapping from
   5904    sections to segments.  This function also sets up some fields in
   5905    the file header.  */
   5906 
   5907 static bool
   5908 assign_file_positions_for_load_sections (bfd *abfd,
   5909 					 struct bfd_link_info *link_info)
   5910 {
   5911   elf_backend_data *bed = get_elf_backend_data (abfd);
   5912   struct elf_segment_map *m;
   5913   struct elf_segment_map *phdr_load_seg;
   5914   Elf_Internal_Phdr *phdrs;
   5915   Elf_Internal_Phdr *p;
   5916   file_ptr off;  /* Octets.  */
   5917   bfd_size_type maxpagesize;
   5918   unsigned int alloc, actual;
   5919   unsigned int i, j;
   5920   struct elf_segment_map **sorted_seg_map;
   5921   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   5922 
   5923   if (link_info == NULL
   5924       && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
   5925     return false;
   5926 
   5927   alloc = 0;
   5928   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   5929     m->idx = alloc++;
   5930 
   5931   if (alloc)
   5932     {
   5933       elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
   5934       elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
   5935     }
   5936   else
   5937     {
   5938       /* PR binutils/12467.  */
   5939       elf_elfheader (abfd)->e_phoff = 0;
   5940       elf_elfheader (abfd)->e_phentsize = 0;
   5941     }
   5942 
   5943   elf_elfheader (abfd)->e_phnum = alloc;
   5944 
   5945   if (elf_program_header_size (abfd) == (bfd_size_type) -1)
   5946     {
   5947       actual = alloc;
   5948       elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
   5949     }
   5950   else
   5951     {
   5952       actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
   5953       BFD_ASSERT (elf_program_header_size (abfd)
   5954 		  == actual * bed->s->sizeof_phdr);
   5955       BFD_ASSERT (actual >= alloc);
   5956     }
   5957 
   5958   if (alloc == 0)
   5959     {
   5960       elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
   5961       return true;
   5962     }
   5963 
   5964   /* We're writing the size in elf_program_header_size (abfd),
   5965      see assign_file_positions_except_relocs, so make sure we have
   5966      that amount allocated, with trailing space cleared.
   5967      The variable alloc contains the computed need, while
   5968      elf_program_header_size (abfd) contains the size used for the
   5969      layout.
   5970      See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
   5971      where the layout is forced to according to a larger size in the
   5972      last iterations for the testcase ld-elf/header.  */
   5973   phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
   5974 			     + alloc * sizeof (*sorted_seg_map)));
   5975   sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
   5976   elf_tdata (abfd)->phdr = phdrs;
   5977   if (phdrs == NULL)
   5978     return false;
   5979 
   5980   for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
   5981     {
   5982       sorted_seg_map[j] = m;
   5983       /* If elf_segment_map is not from map_sections_to_segments, the
   5984 	 sections may not be correctly ordered.  NOTE: sorting should
   5985 	 not be done to the PT_NOTE section of a corefile, which may
   5986 	 contain several pseudo-sections artificially created by bfd.
   5987 	 Sorting these pseudo-sections breaks things badly.  */
   5988       if (m->count > 1
   5989 	  && !(elf_elfheader (abfd)->e_type == ET_CORE
   5990 	       && m->p_type == PT_NOTE))
   5991 	{
   5992 	  for (i = 0; i < m->count; i++)
   5993 	    m->sections[i]->target_index = i;
   5994 	  qsort (m->sections, (size_t) m->count, sizeof (asection *),
   5995 		 elf_sort_sections);
   5996 	}
   5997     }
   5998   if (alloc > 1)
   5999     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
   6000 	   elf_sort_segments);
   6001 
   6002   maxpagesize = 1;
   6003   if ((abfd->flags & D_PAGED) != 0)
   6004     {
   6005       if (link_info != NULL)
   6006 	maxpagesize = link_info->maxpagesize;
   6007       else
   6008 	maxpagesize = bed->maxpagesize;
   6009     }
   6010 
   6011   /* Sections must map to file offsets past the ELF file header.  */
   6012   off = bed->s->sizeof_ehdr;
   6013   /* And if one of the PT_LOAD headers doesn't include the program
   6014      headers then we'll be mapping program headers in the usual
   6015      position after the ELF file header.  */
   6016   phdr_load_seg = NULL;
   6017   for (j = 0; j < alloc; j++)
   6018     {
   6019       m = sorted_seg_map[j];
   6020       if (m->p_type != PT_LOAD)
   6021 	break;
   6022       if (m->includes_phdrs)
   6023 	{
   6024 	  phdr_load_seg = m;
   6025 	  break;
   6026 	}
   6027     }
   6028   if (phdr_load_seg == NULL)
   6029     off += actual * bed->s->sizeof_phdr;
   6030 
   6031   for (j = 0; j < alloc; j++)
   6032     {
   6033       asection **secpp;
   6034       bfd_vma off_adjust;  /* Octets.  */
   6035       bool no_contents;
   6036       bfd_size_type align_pagesize;
   6037 
   6038       /* An ELF segment (described by Elf_Internal_Phdr) may contain a
   6039 	 number of sections with contents contributing to both p_filesz
   6040 	 and p_memsz, followed by a number of sections with no contents
   6041 	 that just contribute to p_memsz.  In this loop, OFF tracks next
   6042 	 available file offset for PT_LOAD and PT_NOTE segments.  */
   6043       m = sorted_seg_map[j];
   6044       p = phdrs + m->idx;
   6045       p->p_type = m->p_type;
   6046       p->p_flags = m->p_flags;
   6047 
   6048       if (m->count == 0)
   6049 	p->p_vaddr = m->p_vaddr_offset * opb;
   6050       else
   6051 	p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
   6052 
   6053       if (m->p_paddr_valid)
   6054 	p->p_paddr = m->p_paddr;
   6055       else if (m->count == 0)
   6056 	p->p_paddr = 0;
   6057       else
   6058 	p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
   6059 
   6060       align_pagesize = 0;
   6061       if (p->p_type == PT_LOAD
   6062 	  && (abfd->flags & D_PAGED) != 0)
   6063 	{
   6064 	  /* p_align in demand paged PT_LOAD segments effectively stores
   6065 	     the maximum page size.  When copying an executable with
   6066 	     objcopy, we set m->p_align from the input file.  Use this
   6067 	     value for maxpagesize rather than bed->maxpagesize, which
   6068 	     may be different.  Note that we use maxpagesize for PT_TLS
   6069 	     segment alignment later in this function, so we are relying
   6070 	     on at least one PT_LOAD segment appearing before a PT_TLS
   6071 	     segment.  */
   6072 	  if (m->p_align_valid)
   6073 	    maxpagesize = m->p_align;
   6074 	  else if (bed->p_align != 0
   6075 		   && (link_info == NULL
   6076 		       || !link_info->maxpagesize_is_set))
   6077 	    /* We will lay out this binary using maxpagesize but set
   6078 	       p->p_align later to the possibly smaller bed->p_align.
   6079 	       The run-time loader will then be able to load this
   6080 	       binary when the system page size is maxpagesize, but if
   6081 	       the system page size is smaller can use p->p_align.
   6082 	       In either case p->p_align will be increased if
   6083 	       necessary to match section alignment.  */
   6084 	    align_pagesize = bed->p_align;
   6085 
   6086 	  p->p_align = maxpagesize;
   6087 	}
   6088       else if (m->p_align_valid)
   6089 	p->p_align = m->p_align;
   6090       else if (m->count == 0)
   6091 	p->p_align = 1 << bed->s->log_file_align;
   6092 
   6093       if (m == phdr_load_seg)
   6094 	off += actual * bed->s->sizeof_phdr;
   6095 
   6096       no_contents = false;
   6097       off_adjust = 0;
   6098       if (p->p_type == PT_LOAD
   6099 	  && m->count > 0)
   6100 	{
   6101 	  bfd_size_type align;  /* Bytes.  */
   6102 	  unsigned int align_power = 0;
   6103 
   6104 	  if (m->p_align_valid)
   6105 	    align = p->p_align;
   6106 	  else
   6107 	    {
   6108 	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6109 		{
   6110 		  unsigned int secalign;
   6111 
   6112 		  secalign = bfd_section_alignment (*secpp);
   6113 		  if (secalign > align_power)
   6114 		    align_power = secalign;
   6115 		}
   6116 	      align = (bfd_size_type) 1 << align_power;
   6117 	      /* If a section requires alignment higher than the
   6118 		 minimum p_align value, don't reduce a maxpagesize
   6119 		 p->p_align set earlier in this function.  */
   6120 	      if (align > bed->p_align)
   6121 		align_pagesize = 0;
   6122 	      if (align < maxpagesize)
   6123 		align = maxpagesize;
   6124 	      else
   6125 		{
   6126 		  /* If a section requires alignment higher than the
   6127 		     maximum page size, set p_align to the section
   6128 		     alignment.  */
   6129 		  if ((abfd->flags & D_PAGED) != 0)
   6130 		    p->p_align = align;
   6131 		}
   6132 	    }
   6133 
   6134 	  for (i = 0; i < m->count; i++)
   6135 	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   6136 	      /* If we aren't making room for this section, then
   6137 		 it must be SHT_NOBITS regardless of what we've
   6138 		 set via struct bfd_elf_special_section.  */
   6139 	      elf_section_type (m->sections[i]) = SHT_NOBITS;
   6140 
   6141 	  /* Find out whether this segment contains any loadable
   6142 	     sections.  */
   6143 	  no_contents = true;
   6144 	  for (i = 0; i < m->count; i++)
   6145 	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
   6146 	      {
   6147 		no_contents = false;
   6148 		break;
   6149 	      }
   6150 
   6151 	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
   6152 
   6153 	  /* Broken hardware and/or kernel require that files do not
   6154 	     map the same page with different permissions on some hppa
   6155 	     processors.  */
   6156 	  if (j != 0
   6157 	      && (abfd->flags & D_PAGED) != 0
   6158 	      && bed->no_page_alias
   6159 	      && (off & (maxpagesize - 1)) != 0
   6160 	      && ((off & -maxpagesize)
   6161 		  == ((off + off_adjust) & -maxpagesize)))
   6162 	    off_adjust += maxpagesize;
   6163 	  off += off_adjust;
   6164 	  if (no_contents)
   6165 	    {
   6166 	      /* We shouldn't need to align the segment on disk since
   6167 		 the segment doesn't need file space, but the gABI
   6168 		 arguably requires the alignment and glibc ld.so
   6169 		 checks it.  So to comply with the alignment
   6170 		 requirement but not waste file space, we adjust
   6171 		 p_offset for just this segment.  (OFF_ADJUST is
   6172 		 subtracted from OFF later.)  This may put p_offset
   6173 		 past the end of file, but that shouldn't matter.  */
   6174 	    }
   6175 	  else
   6176 	    off_adjust = 0;
   6177 	}
   6178       /* Make sure the .dynamic section is the first section in the
   6179 	 PT_DYNAMIC segment.  */
   6180       else if (p->p_type == PT_DYNAMIC
   6181 	       && m->count > 1
   6182 	       && ! streq (m->sections[0]->name, ".dynamic"))
   6183 	{
   6184 	  _bfd_error_handler
   6185 	    (_("%pB: The first section in the PT_DYNAMIC segment"
   6186 	       " is not the .dynamic section"),
   6187 	     abfd);
   6188 	  bfd_set_error (bfd_error_bad_value);
   6189 	  return false;
   6190 	}
   6191       /* Set the note section type to SHT_NOTE.  */
   6192       else if (p->p_type == PT_NOTE)
   6193 	for (i = 0; i < m->count; i++)
   6194 	  elf_section_type (m->sections[i]) = SHT_NOTE;
   6195 
   6196       if (m->includes_filehdr)
   6197 	{
   6198 	  if (!m->p_flags_valid)
   6199 	    p->p_flags |= PF_R;
   6200 	  p->p_filesz = bed->s->sizeof_ehdr;
   6201 	  p->p_memsz = bed->s->sizeof_ehdr;
   6202 	  if (p->p_type == PT_LOAD)
   6203 	    {
   6204 	      if (m->count > 0)
   6205 		{
   6206 		  if (p->p_vaddr < (bfd_vma) off
   6207 		      || (!m->p_paddr_valid
   6208 			  && p->p_paddr < (bfd_vma) off))
   6209 		    {
   6210 		      _bfd_error_handler
   6211 			(_("%pB: not enough room for program headers,"
   6212 			   " try linking with -N"),
   6213 			 abfd);
   6214 		      bfd_set_error (bfd_error_bad_value);
   6215 		      return false;
   6216 		    }
   6217 		  p->p_vaddr -= off;
   6218 		  if (!m->p_paddr_valid)
   6219 		    p->p_paddr -= off;
   6220 		}
   6221 	    }
   6222 	  else if (sorted_seg_map[0]->includes_filehdr)
   6223 	    {
   6224 	      Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
   6225 	      p->p_vaddr = filehdr->p_vaddr;
   6226 	      if (!m->p_paddr_valid)
   6227 		p->p_paddr = filehdr->p_paddr;
   6228 	    }
   6229 	}
   6230 
   6231       if (m->includes_phdrs)
   6232 	{
   6233 	  if (!m->p_flags_valid)
   6234 	    p->p_flags |= PF_R;
   6235 	  p->p_filesz += actual * bed->s->sizeof_phdr;
   6236 	  p->p_memsz += actual * bed->s->sizeof_phdr;
   6237 	  if (!m->includes_filehdr)
   6238 	    {
   6239 	      if (p->p_type == PT_LOAD)
   6240 		{
   6241 		  p->p_offset = off - actual * bed->s->sizeof_phdr;
   6242 		  elf_elfheader (abfd)->e_phoff = p->p_offset;
   6243 		  if (m->count > 0)
   6244 		    {
   6245 		      p->p_vaddr -= off - p->p_offset;
   6246 		      if (!m->p_paddr_valid)
   6247 			p->p_paddr -= off - p->p_offset;
   6248 		    }
   6249 		}
   6250 	      else if (phdr_load_seg != NULL)
   6251 		{
   6252 		  /* Also set PT_PHDR to match phdr_load_seg.  We've
   6253 		     sorted segments so that phdr_load_seg will
   6254 		     already be set by the code immediately above.  */
   6255 		  Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
   6256 		  bfd_vma phdr_off = 0;  /* Octets.  */
   6257 		  if (phdr_load_seg->includes_filehdr)
   6258 		    phdr_off = bed->s->sizeof_ehdr;
   6259 		  p->p_vaddr = phdr->p_vaddr + phdr_off;
   6260 		  if (!m->p_paddr_valid)
   6261 		    p->p_paddr = phdr->p_paddr + phdr_off;
   6262 		  p->p_offset = phdr->p_offset + phdr_off;
   6263 		}
   6264 	      else
   6265 		p->p_offset = bed->s->sizeof_ehdr;
   6266 	    }
   6267 	}
   6268 
   6269       if (p->p_type == PT_LOAD
   6270 	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
   6271 	{
   6272 	  if (!m->includes_filehdr && !m->includes_phdrs)
   6273 	    {
   6274 	      p->p_offset = off;
   6275 	      if (no_contents)
   6276 		{
   6277 		  /* Put meaningless p_offset for PT_LOAD segments
   6278 		     without file contents somewhere within the first
   6279 		     page, in an attempt to not point past EOF.  */
   6280 		  bfd_size_type align = maxpagesize;
   6281 		  if (align < p->p_align)
   6282 		    align = p->p_align;
   6283 		  if (align < 1)
   6284 		    align = 1;
   6285 		  /* Avoid p_offset of zero, which might be wrongly
   6286 		     interpreted as the segment being the first one,
   6287 		     containing the file header.  PR32763.  */
   6288 		  p->p_offset = (off + align - 1) % align + 1;
   6289 		}
   6290 	    }
   6291 	  else
   6292 	    {
   6293 	      file_ptr adjust;  /* Octets.  */
   6294 
   6295 	      adjust = off - (p->p_offset + p->p_filesz);
   6296 	      if (!no_contents)
   6297 		p->p_filesz += adjust;
   6298 	      p->p_memsz += adjust;
   6299 	    }
   6300 	}
   6301 
   6302       if (align_pagesize)
   6303 	p->p_align = align_pagesize;
   6304 
   6305       /* Set up p_filesz, p_memsz, p_align and p_flags from the section
   6306 	 maps.  Set filepos for sections in PT_LOAD segments, and in
   6307 	 core files, for sections in PT_NOTE segments.
   6308 	 assign_file_positions_for_non_load_sections will set filepos
   6309 	 for other sections and update p_filesz for other segments.  */
   6310       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
   6311 	{
   6312 	  asection *sec;
   6313 	  bfd_size_type align;
   6314 	  Elf_Internal_Shdr *this_hdr;
   6315 
   6316 	  sec = *secpp;
   6317 	  this_hdr = &elf_section_data (sec)->this_hdr;
   6318 	  align = (bfd_size_type) 1 << bfd_section_alignment (sec);
   6319 
   6320 	  if ((p->p_type == PT_LOAD
   6321 	       || p->p_type == PT_TLS)
   6322 	      && (this_hdr->sh_type != SHT_NOBITS
   6323 		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
   6324 		      && ((this_hdr->sh_flags & SHF_TLS) == 0
   6325 			  || p->p_type == PT_TLS))))
   6326 	    {
   6327 	      bfd_vma p_start = p->p_paddr;		/* Octets.  */
   6328 	      bfd_vma p_end = p_start + p->p_memsz;	/* Octets.  */
   6329 	      bfd_vma s_start = sec->lma * opb;		/* Octets.  */
   6330 	      bfd_vma adjust = s_start - p_end;		/* Octets.  */
   6331 
   6332 	      if (adjust != 0
   6333 		  && (s_start < p_end
   6334 		      || p_end < p_start))
   6335 		{
   6336 		  _bfd_error_handler
   6337 		    /* xgettext:c-format */
   6338 		    (_("%pB: section %pA lma %#" PRIx64
   6339 		       " adjusted to %#" PRIx64),
   6340 		     abfd, sec, (uint64_t) s_start / opb,
   6341 		     (uint64_t) p_end / opb);
   6342 		  adjust = 0;
   6343 		  sec->lma = p_end / opb;
   6344 		}
   6345 	      p->p_memsz += adjust;
   6346 
   6347 	      if (p->p_type == PT_LOAD)
   6348 		{
   6349 		  if (this_hdr->sh_type != SHT_NOBITS)
   6350 		    {
   6351 		      off_adjust = 0;
   6352 		      if (p->p_filesz + adjust < p->p_memsz)
   6353 			{
   6354 			  /* We have a PROGBITS section following NOBITS ones.
   6355 			     Allocate file space for the NOBITS section(s).
   6356 			     We don't need to write out the zeros, posix
   6357 			     fseek past the end of data already written
   6358 			     followed by a write at that location is
   6359 			     guaranteed to result in zeros being read
   6360 			     from the gap.  */
   6361 			  adjust = p->p_memsz - p->p_filesz;
   6362 			}
   6363 		    }
   6364 		  /* We only adjust sh_offset in SHT_NOBITS sections
   6365 		     as would seem proper for their address when the
   6366 		     section is first in the segment.  sh_offset
   6367 		     doesn't really have any significance for
   6368 		     SHT_NOBITS anyway, apart from a notional position
   6369 		     relative to other sections.  Historically we
   6370 		     didn't bother with adjusting sh_offset and some
   6371 		     programs depend on it not being adjusted.  See
   6372 		     pr12921 and pr25662.  */
   6373 		  if (this_hdr->sh_type != SHT_NOBITS || i == 0)
   6374 		    {
   6375 		      off += adjust;
   6376 		      if (this_hdr->sh_type == SHT_NOBITS)
   6377 			off_adjust += adjust;
   6378 		    }
   6379 		}
   6380 	      if (this_hdr->sh_type != SHT_NOBITS)
   6381 		p->p_filesz += adjust;
   6382 	    }
   6383 
   6384 	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
   6385 	    {
   6386 	      /* The section at i == 0 is the one that actually contains
   6387 		 everything.  */
   6388 	      if (i == 0)
   6389 		{
   6390 		  this_hdr->sh_offset = sec->filepos = off;
   6391 		  off += this_hdr->sh_size;
   6392 		  p->p_filesz = this_hdr->sh_size;
   6393 		  p->p_memsz = 0;
   6394 		  p->p_align = 1;
   6395 		}
   6396 	      else
   6397 		{
   6398 		  /* The rest are fake sections that shouldn't be written.  */
   6399 		  sec->filepos = 0;
   6400 		  sec->size = 0;
   6401 		  sec->flags = 0;
   6402 		  continue;
   6403 		}
   6404 	    }
   6405 	  else
   6406 	    {
   6407 	      if (this_hdr->sh_type == SHT_NOBITS
   6408 		  && (this_hdr->sh_flags & SHF_TLS) != 0
   6409 		  && this_hdr->sh_offset == 0)
   6410 		{
   6411 		  /* Set sh_offset for .tbss sections to their nominal
   6412 		     offset after aligning.  They are not loaded from
   6413 		     disk so the value doesn't really matter, except
   6414 		     when the .tbss section is the first one in a
   6415 		     PT_TLS segment.  In that case it sets the
   6416 		     p_offset for the PT_TLS segment, which according
   6417 		     to the ELF gABI ought to satisfy
   6418 		     p_offset % p_align == p_vaddr % p_align.  */
   6419 		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
   6420 							  off, align);
   6421 		  this_hdr->sh_offset = sec->filepos = off + adjust;
   6422 		}
   6423 	      else if (p->p_type == PT_LOAD)
   6424 		{
   6425 		  this_hdr->sh_offset = sec->filepos = off;
   6426 		  if (this_hdr->sh_type != SHT_NOBITS)
   6427 		    off += this_hdr->sh_size;
   6428 		}
   6429 
   6430 	      if (this_hdr->sh_type != SHT_NOBITS)
   6431 		{
   6432 		  p->p_filesz += this_hdr->sh_size;
   6433 		  /* A load section without SHF_ALLOC is something like
   6434 		     a note section in a PT_NOTE segment.  These take
   6435 		     file space but are not loaded into memory.  */
   6436 		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6437 		    p->p_memsz += this_hdr->sh_size;
   6438 		}
   6439 	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
   6440 		{
   6441 		  if (p->p_type == PT_TLS)
   6442 		    p->p_memsz += this_hdr->sh_size;
   6443 
   6444 		  /* .tbss is special.  It doesn't contribute to p_memsz of
   6445 		     normal segments.  */
   6446 		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
   6447 		    p->p_memsz += this_hdr->sh_size;
   6448 		}
   6449 
   6450 	      if (align > p->p_align
   6451 		  && !m->p_align_valid
   6452 		  && (p->p_type != PT_LOAD
   6453 		      || (abfd->flags & D_PAGED) == 0))
   6454 		p->p_align = align;
   6455 	    }
   6456 
   6457 	  if (!m->p_flags_valid)
   6458 	    {
   6459 	      p->p_flags |= PF_R;
   6460 	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
   6461 		p->p_flags |= PF_X;
   6462 	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
   6463 		p->p_flags |= PF_W;
   6464 	    }
   6465 	}
   6466 
   6467       off -= off_adjust;
   6468 
   6469       /* PR ld/20815 - Check that the program header segment, if
   6470 	 present, will be loaded into memory.  */
   6471       if (p->p_type == PT_PHDR
   6472 	  && phdr_load_seg == NULL
   6473 	  && !(bed->elf_backend_allow_non_load_phdr != NULL
   6474 	       && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
   6475 	{
   6476 	  /* The fix for this error is usually to edit the linker script being
   6477 	     used and set up the program headers manually.  Either that or
   6478 	     leave room for the headers at the start of the SECTIONS.  */
   6479 	  _bfd_error_handler (_("%pB: error: PHDR segment not covered"
   6480 				" by LOAD segment"),
   6481 			      abfd);
   6482 	  if (link_info == NULL)
   6483 	    return false;
   6484 	  /* Arrange for the linker to exit with an error, deleting
   6485 	     the output file unless --noinhibit-exec is given.  */
   6486 	  link_info->callbacks->info ("%X");
   6487 	}
   6488 
   6489       /* Check that all sections are in a PT_LOAD segment.
   6490 	 Don't check funky gdb generated core files.  */
   6491       if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
   6492 	{
   6493 	  bool check_vma = true;
   6494 
   6495 	  for (i = 1; i < m->count; i++)
   6496 	    if (m->sections[i]->vma == m->sections[i - 1]->vma
   6497 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
   6498 				       ->this_hdr), p) != 0
   6499 		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
   6500 				       ->this_hdr), p) != 0)
   6501 	      {
   6502 		/* Looks like we have overlays packed into the segment.  */
   6503 		check_vma = false;
   6504 		break;
   6505 	      }
   6506 
   6507 	  for (i = 0; i < m->count; i++)
   6508 	    {
   6509 	      Elf_Internal_Shdr *this_hdr;
   6510 	      asection *sec;
   6511 
   6512 	      sec = m->sections[i];
   6513 	      this_hdr = &(elf_section_data(sec)->this_hdr);
   6514 	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
   6515 		  && !ELF_TBSS_SPECIAL (this_hdr, p))
   6516 		{
   6517 		  _bfd_error_handler
   6518 		    /* xgettext:c-format */
   6519 		    (_("%pB: section `%pA' can't be allocated in segment %u"),
   6520 		     abfd, sec, m->idx);
   6521 		  print_segment_map (m);
   6522 		}
   6523 	    }
   6524 	}
   6525     }
   6526 
   6527   elf_next_file_pos (abfd) = off;
   6528 
   6529   if (link_info != NULL
   6530       && phdr_load_seg != NULL
   6531       && phdr_load_seg->includes_filehdr)
   6532     {
   6533       /* There is a segment that contains both the file headers and the
   6534 	 program headers, so provide a symbol __ehdr_start pointing there.
   6535 	 A program can use this to examine itself robustly.  */
   6536 
   6537       struct elf_link_hash_table *htab = elf_hash_table (link_info);
   6538       struct elf_link_hash_entry *hash = htab->hehdr_start;
   6539 
   6540       /* If the symbol was referenced and not defined, define it.  */
   6541       if (hash != NULL
   6542 	  && (hash->root.type == bfd_link_hash_new
   6543 	      || hash->root.type == bfd_link_hash_undefined
   6544 	      || hash->root.type == bfd_link_hash_undefweak
   6545 	      || hash->root.type == bfd_link_hash_common))
   6546 	{
   6547 	  asection *s = NULL;
   6548 	  bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
   6549 
   6550 	  if (phdr_load_seg->count != 0)
   6551 	    /* The segment contains sections, so use the first one.  */
   6552 	    s = phdr_load_seg->sections[0];
   6553 	  else
   6554 	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
   6555 	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   6556 	      if (m->p_type == PT_LOAD && m->count != 0)
   6557 		{
   6558 		  s = m->sections[0];
   6559 		  break;
   6560 		}
   6561 
   6562 	  if (s != NULL)
   6563 	    {
   6564 	      hash->root.u.def.value = filehdr_vaddr - s->vma;
   6565 	      hash->root.u.def.section = s;
   6566 	    }
   6567 	  else
   6568 	    {
   6569 	      hash->root.u.def.value = filehdr_vaddr;
   6570 	      hash->root.u.def.section = bfd_abs_section_ptr;
   6571 	    }
   6572 
   6573 	  hash->root.type = bfd_link_hash_defined;
   6574 	  hash->def_regular = 1;
   6575 	  hash->non_elf = 0;
   6576 	}
   6577     }
   6578 
   6579   return true;
   6580 }
   6581 
   6582 /* Determine if a bfd is a debuginfo file.  Unfortunately there
   6583    is no defined method for detecting such files, so we have to
   6584    use heuristics instead.  */
   6585 
   6586 bool
   6587 is_debuginfo_file (bfd *abfd)
   6588 {
   6589   if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   6590     return false;
   6591 
   6592   Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
   6593   Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
   6594   Elf_Internal_Shdr **headerp;
   6595 
   6596   for (headerp = start_headers; headerp < end_headers; headerp ++)
   6597     {
   6598       Elf_Internal_Shdr *header = * headerp;
   6599 
   6600       /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
   6601 	 The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
   6602       if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
   6603 	  && header->sh_type != SHT_NOBITS
   6604 	  && header->sh_type != SHT_NOTE)
   6605 	return false;
   6606     }
   6607 
   6608   return true;
   6609 }
   6610 
   6611 /* Assign file positions for other sections, except for compressed debug
   6612    and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
   6613 
   6614 static bool
   6615 assign_file_positions_for_non_load_sections (bfd *abfd,
   6616 					     struct bfd_link_info *link_info)
   6617 {
   6618   elf_backend_data *bed = get_elf_backend_data (abfd);
   6619   Elf_Internal_Shdr **i_shdrpp;
   6620   Elf_Internal_Shdr **hdrpp, **end_hdrpp;
   6621   Elf_Internal_Phdr *phdrs;
   6622   Elf_Internal_Phdr *p;
   6623   struct elf_segment_map *m;
   6624   file_ptr off;
   6625   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
   6626   bfd_vma maxpagesize;
   6627 
   6628   if (link_info != NULL)
   6629     maxpagesize = link_info->maxpagesize;
   6630   else
   6631     maxpagesize = bed->maxpagesize;
   6632   i_shdrpp = elf_elfsections (abfd);
   6633   end_hdrpp = i_shdrpp + elf_numsections (abfd);
   6634   off = elf_next_file_pos (abfd);
   6635   for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
   6636     {
   6637       Elf_Internal_Shdr *hdr;
   6638       bfd_vma align;
   6639 
   6640       hdr = *hdrpp;
   6641       if (hdr->bfd_section != NULL
   6642 	  && (hdr->bfd_section->filepos != 0
   6643 	      || (hdr->sh_type == SHT_NOBITS
   6644 		  && hdr->contents == NULL)))
   6645 	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
   6646       else if ((hdr->sh_flags & SHF_ALLOC) != 0)
   6647 	{
   6648 	  if (hdr->sh_size != 0
   6649 	      /* PR 24717 - debuginfo files are known to be not strictly
   6650 		 compliant with the ELF standard.  In particular they often
   6651 		 have .note.gnu.property sections that are outside of any
   6652 		 loadable segment.  This is not a problem for such files,
   6653 		 so do not warn about them.  */
   6654 	      && ! is_debuginfo_file (abfd))
   6655 	    _bfd_error_handler
   6656 	      /* xgettext:c-format */
   6657 	      (_("%pB: warning: allocated section `%s' not in segment"),
   6658 	       abfd,
   6659 	       (hdr->bfd_section == NULL
   6660 		? "*unknown*"
   6661 		: hdr->bfd_section->name));
   6662 	  /* We don't need to page align empty sections.  */
   6663 	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
   6664 	    align = maxpagesize;
   6665 	  else
   6666 	    align = hdr->sh_addralign & -hdr->sh_addralign;
   6667 	  off += vma_page_aligned_bias (hdr->sh_addr, off, align);
   6668 	  off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
   6669 							   bed->s->log_file_align);
   6670 	}
   6671       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6672 		&& hdr->bfd_section == NULL)
   6673 	       /* We don't know the offset of these sections yet:
   6674 		  their size has not been decided.  */
   6675 	       || (abfd->is_linker_output
   6676 		   && hdr->bfd_section != NULL
   6677 		   && (hdr->sh_name == -1u
   6678 		       || bfd_section_is_ctf (hdr->bfd_section)))
   6679 	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
   6680 	       || (elf_symtab_shndx_list (abfd) != NULL
   6681 		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6682 	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
   6683 	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
   6684 	hdr->sh_offset = -1;
   6685       else
   6686 	off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
   6687     }
   6688   elf_next_file_pos (abfd) = off;
   6689 
   6690   /* Now that we have set the section file positions, we can set up
   6691      the file positions for the non PT_LOAD segments.  */
   6692   phdrs = elf_tdata (abfd)->phdr;
   6693   for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
   6694     {
   6695       if (p->p_type == PT_GNU_RELRO)
   6696 	{
   6697 	  bfd_vma start, end;  /* Bytes.  */
   6698 	  bool ok;
   6699 
   6700 	  if (link_info != NULL)
   6701 	    {
   6702 	      /* During linking the range of the RELRO segment is passed
   6703 		 in link_info.  Note that there may be padding between
   6704 		 relro_start and the first RELRO section.  */
   6705 	      start = link_info->relro_start;
   6706 	      end = link_info->relro_end;
   6707 	    }
   6708 	  else if (m->count != 0)
   6709 	    {
   6710 	      if (!m->p_size_valid)
   6711 		abort ();
   6712 	      start = m->sections[0]->vma;
   6713 	      end = start + m->p_size / opb;
   6714 	    }
   6715 	  else
   6716 	    {
   6717 	      start = 0;
   6718 	      end = 0;
   6719 	    }
   6720 
   6721 	  ok = false;
   6722 	  if (start < end)
   6723 	    {
   6724 	      struct elf_segment_map *lm;
   6725 	      const Elf_Internal_Phdr *lp;
   6726 	      unsigned int i;
   6727 
   6728 	      /* Find a LOAD segment containing a section in the RELRO
   6729 		 segment.  */
   6730 	      for (lm = elf_seg_map (abfd), lp = phdrs;
   6731 		   lm != NULL;
   6732 		   lm = lm->next, lp++)
   6733 		{
   6734 		  if (lp->p_type == PT_LOAD
   6735 		      && lm->count != 0
   6736 		      && (lm->sections[lm->count - 1]->vma
   6737 			  + (!IS_TBSS (lm->sections[lm->count - 1])
   6738 			     ? lm->sections[lm->count - 1]->size / opb
   6739 			     : 0)) > start
   6740 		      && lm->sections[0]->vma < end)
   6741 		    break;
   6742 		}
   6743 
   6744 	      if (lm != NULL)
   6745 		{
   6746 		  /* Find the section starting the RELRO segment.  */
   6747 		  for (i = 0; i < lm->count; i++)
   6748 		    {
   6749 		      asection *s = lm->sections[i];
   6750 		      if (s->vma >= start
   6751 			  && s->vma < end
   6752 			  && s->size != 0)
   6753 			break;
   6754 		    }
   6755 
   6756 		  if (i < lm->count)
   6757 		    {
   6758 		      p->p_vaddr = lm->sections[i]->vma * opb;
   6759 		      p->p_paddr = lm->sections[i]->lma * opb;
   6760 		      p->p_offset = lm->sections[i]->filepos;
   6761 		      p->p_memsz = end * opb - p->p_vaddr;
   6762 		      p->p_filesz = p->p_memsz;
   6763 
   6764 		      /* The RELRO segment typically ends a few bytes
   6765 			 into .got.plt but other layouts are possible.
   6766 			 In cases where the end does not match any
   6767 			 loaded section (for instance is in file
   6768 			 padding), trim p_filesz back to correspond to
   6769 			 the end of loaded section contents.  */
   6770 		      if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
   6771 			p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
   6772 
   6773 		      /* Preserve the alignment and flags if they are
   6774 			 valid.  The gold linker generates RW/4 for
   6775 			 the PT_GNU_RELRO section.  It is better for
   6776 			 objcopy/strip to honor these attributes
   6777 			 otherwise gdb will choke when using separate
   6778 			 debug files.  */
   6779 		      if (!m->p_align_valid)
   6780 			p->p_align = 1;
   6781 		      if (!m->p_flags_valid)
   6782 			p->p_flags = PF_R;
   6783 		      ok = true;
   6784 		    }
   6785 		}
   6786 	    }
   6787 
   6788 	  if (!ok)
   6789 	    {
   6790 	      if (link_info != NULL)
   6791 		_bfd_error_handler
   6792 		  (_("%pB: warning: unable to allocate any sections"
   6793 		     " to PT_GNU_RELRO segment"),
   6794 		   abfd);
   6795 	      memset (p, 0, sizeof *p);
   6796 	    }
   6797 	}
   6798       else if (p->p_type == PT_GNU_STACK)
   6799 	{
   6800 	  if (m->p_size_valid)
   6801 	    p->p_memsz = m->p_size;
   6802 	}
   6803       else if (m->count != 0)
   6804 	{
   6805 	  unsigned int i;
   6806 
   6807 	  if (p->p_type != PT_LOAD
   6808 	      && (p->p_type != PT_NOTE
   6809 		  || bfd_get_format (abfd) != bfd_core))
   6810 	    {
   6811 	      /* A user specified segment layout may include a PHDR
   6812 		 segment that overlaps with a LOAD segment...  */
   6813 	      if (p->p_type == PT_PHDR)
   6814 		{
   6815 		  m->count = 0;
   6816 		  continue;
   6817 		}
   6818 
   6819 	      if (m->includes_filehdr || m->includes_phdrs)
   6820 		{
   6821 		  /* PR 17512: file: 2195325e.  */
   6822 		  _bfd_error_handler
   6823 		    (_("%pB: error: non-load segment %d includes file header "
   6824 		       "and/or program header"),
   6825 		     abfd, (int) (p - phdrs));
   6826 		  return false;
   6827 		}
   6828 
   6829 	      p->p_filesz = 0;
   6830 	      p->p_offset = m->sections[0]->filepos;
   6831 	      for (i = m->count; i-- != 0;)
   6832 		{
   6833 		  asection *sect = m->sections[i];
   6834 		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
   6835 		  if (hdr->sh_type != SHT_NOBITS)
   6836 		    {
   6837 		      p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
   6838 		      /* NB: p_memsz of the loadable PT_NOTE segment
   6839 			 should be the same as p_filesz.  */
   6840 		      if (p->p_type == PT_NOTE
   6841 			  && (hdr->sh_flags & SHF_ALLOC) != 0)
   6842 			p->p_memsz = p->p_filesz;
   6843 		      break;
   6844 		    }
   6845 		}
   6846 	    }
   6847 	}
   6848     }
   6849 
   6850   return true;
   6851 }
   6852 
   6853 static elf_section_list *
   6854 find_section_in_list (unsigned int i, elf_section_list * list)
   6855 {
   6856   for (;list != NULL; list = list->next)
   6857     if (list->ndx == i)
   6858       break;
   6859   return list;
   6860 }
   6861 
   6862 /* Work out the file positions of all the sections.  This is called by
   6863    _bfd_elf_compute_section_file_positions.  All the section sizes and
   6864    VMAs must be known before this is called.
   6865 
   6866    Reloc sections come in two flavours: Those processed specially as
   6867    "side-channel" data attached to a section to which they apply, and
   6868    those that bfd doesn't process as relocations.  The latter sort are
   6869    stored in a normal bfd section by bfd_section_from_shdr.  We don't
   6870    consider the former sort here, unless they form part of the loadable
   6871    image.  Reloc sections not assigned here (and compressed debugging
   6872    sections and CTF sections which nothing else in the file can rely
   6873    upon) will be handled later by assign_file_positions_for_relocs.
   6874 
   6875    We also don't set the positions of the .symtab and .strtab here.  */
   6876 
   6877 static bool
   6878 assign_file_positions_except_relocs (bfd *abfd,
   6879 				     struct bfd_link_info *link_info)
   6880 {
   6881   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   6882   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   6883   elf_backend_data *bed = get_elf_backend_data (abfd);
   6884   unsigned int alloc;
   6885 
   6886   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   6887       && bfd_get_format (abfd) != bfd_core)
   6888     {
   6889       Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
   6890       unsigned int num_sec = elf_numsections (abfd);
   6891       Elf_Internal_Shdr **hdrpp;
   6892       unsigned int i;
   6893       file_ptr off;
   6894 
   6895       /* Start after the ELF header.  */
   6896       off = i_ehdrp->e_ehsize;
   6897 
   6898       /* We are not creating an executable, which means that we are
   6899 	 not creating a program header, and that the actual order of
   6900 	 the sections in the file is unimportant.  */
   6901       for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
   6902 	{
   6903 	  Elf_Internal_Shdr *hdr;
   6904 
   6905 	  hdr = *hdrpp;
   6906 	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
   6907 	       && hdr->bfd_section == NULL)
   6908 	      /* Do not assign offsets for these sections yet: we don't know
   6909 		 their sizes.  */
   6910 	      || (abfd->is_linker_output
   6911 		  && hdr->bfd_section != NULL
   6912 		  && (hdr->sh_name == -1u
   6913 		      || bfd_section_is_ctf (hdr->bfd_section)))
   6914 	      || i == elf_onesymtab (abfd)
   6915 	      || (elf_symtab_shndx_list (abfd) != NULL
   6916 		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
   6917 	      || i == elf_strtab_sec (abfd)
   6918 	      || i == elf_shstrtab_sec (abfd))
   6919 	    {
   6920 	      hdr->sh_offset = -1;
   6921 	    }
   6922 	  else
   6923 	    /* There shouldn't be a need to effect "capped" file alignment here,
   6924 	       yet at least the Linux kernel's modpost utility was found to be
   6925 	       unhappy without.  While the issue was addressed there, let's be
   6926 	       kind for at least the foreseeable future ...  */
   6927 	    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
   6928 							     bed->s->log_file_align);
   6929 	}
   6930 
   6931       elf_next_file_pos (abfd) = off;
   6932       elf_program_header_size (abfd) = 0;
   6933     }
   6934   else
   6935     {
   6936       /* Assign file positions for the loaded sections based on the
   6937 	 assignment of sections to segments.  */
   6938       if (!assign_file_positions_for_load_sections (abfd, link_info))
   6939 	return false;
   6940 
   6941       /* And for non-load sections.  */
   6942       if (!assign_file_positions_for_non_load_sections (abfd, link_info))
   6943 	return false;
   6944     }
   6945 
   6946   if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
   6947     return false;
   6948 
   6949   /* Write out the program headers.  */
   6950   alloc = i_ehdrp->e_phnum;
   6951   if (alloc != 0)
   6952     {
   6953       if (link_info != NULL && ! link_info->no_warn_rwx_segments)
   6954 	{
   6955 	  bool warned_tls = false;
   6956 	  bool warned_rwx = false;
   6957 
   6958 	  /* Memory resident segments with non-zero size and RWX
   6959 	     permissions are a security risk, so we generate a warning
   6960 	     here if we are creating any.  */
   6961 	  unsigned int i;
   6962 
   6963 	  for (i = 0; i < alloc; i++)
   6964 	    {
   6965 	      const Elf_Internal_Phdr * phdr = tdata->phdr + i;
   6966 
   6967 	      if (phdr->p_memsz == 0)
   6968 		continue;
   6969 
   6970 	      if (! warned_tls
   6971 		  && phdr->p_type == PT_TLS
   6972 		  && (phdr->p_flags & PF_X))
   6973 		{
   6974 		  if (link_info->warn_is_error_for_rwx_segments)
   6975 		    {
   6976 		      _bfd_error_handler (_("\
   6977 error: %pB has a TLS segment with execute permission"),
   6978 					  abfd);
   6979 		      return false;
   6980 		    }
   6981 
   6982 		  _bfd_error_handler (_("\
   6983 warning: %pB has a TLS segment with execute permission"),
   6984 				      abfd);
   6985 		  if (warned_rwx)
   6986 		    break;
   6987 
   6988 		  warned_tls = true;
   6989 		}
   6990 	      else if (! warned_rwx
   6991 		       && phdr->p_type == PT_LOAD
   6992 		       && ((phdr->p_flags & (PF_R | PF_W | PF_X))
   6993 			   == (PF_R | PF_W | PF_X)))
   6994 		{
   6995 		  if (link_info->warn_is_error_for_rwx_segments)
   6996 		    {
   6997 		      _bfd_error_handler (_("\
   6998 error: %pB has a LOAD segment with RWX permissions"),
   6999 					  abfd);
   7000 		      return false;
   7001 		    }
   7002 
   7003 		  _bfd_error_handler (_("\
   7004 warning: %pB has a LOAD segment with RWX permissions"),
   7005 				      abfd);
   7006 		  if (warned_tls)
   7007 		    break;
   7008 
   7009 		  warned_rwx = true;
   7010 		}
   7011 	    }
   7012 	}
   7013 
   7014       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
   7015 	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
   7016 	return false;
   7017     }
   7018 
   7019   return true;
   7020 }
   7021 
   7022 bool
   7023 _bfd_elf_init_file_header (bfd *abfd,
   7024 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   7025 {
   7026   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
   7027   struct elf_strtab_hash *shstrtab;
   7028   elf_backend_data *bed = get_elf_backend_data (abfd);
   7029 
   7030   i_ehdrp = elf_elfheader (abfd);
   7031 
   7032   shstrtab = _bfd_elf_strtab_init ();
   7033   if (shstrtab == NULL)
   7034     return false;
   7035 
   7036   elf_shstrtab (abfd) = shstrtab;
   7037 
   7038   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
   7039   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
   7040   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
   7041   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
   7042 
   7043   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
   7044   i_ehdrp->e_ident[EI_DATA] =
   7045     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   7046   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
   7047 
   7048   if ((abfd->flags & DYNAMIC) != 0)
   7049     i_ehdrp->e_type = ET_DYN;
   7050   else if ((abfd->flags & EXEC_P) != 0)
   7051     i_ehdrp->e_type = ET_EXEC;
   7052   else if (bfd_get_format (abfd) == bfd_core)
   7053     i_ehdrp->e_type = ET_CORE;
   7054   else
   7055     i_ehdrp->e_type = ET_REL;
   7056 
   7057   switch (bfd_get_arch (abfd))
   7058     {
   7059     case bfd_arch_unknown:
   7060       i_ehdrp->e_machine = EM_NONE;
   7061       break;
   7062 
   7063       /* There used to be a long list of cases here, each one setting
   7064 	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
   7065 	 in the corresponding bfd definition.  To avoid duplication,
   7066 	 the switch was removed.  Machines that need special handling
   7067 	 can generally do it in elf_backend_final_write_processing(),
   7068 	 unless they need the information earlier than the final write.
   7069 	 Such need can generally be supplied by replacing the tests for
   7070 	 e_machine with the conditions used to determine it.  */
   7071     default:
   7072       i_ehdrp->e_machine = bed->elf_machine_code;
   7073     }
   7074 
   7075   i_ehdrp->e_version = bed->s->ev_current;
   7076   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
   7077 
   7078   /* No program header, for now.  */
   7079   i_ehdrp->e_phoff = 0;
   7080   i_ehdrp->e_phentsize = 0;
   7081   i_ehdrp->e_phnum = 0;
   7082 
   7083   /* Each bfd section is section header entry.  */
   7084   i_ehdrp->e_entry = bfd_get_start_address (abfd);
   7085   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
   7086 
   7087   elf_tdata (abfd)->symtab_hdr.sh_name =
   7088     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   7089   elf_tdata (abfd)->strtab_hdr.sh_name =
   7090     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   7091   elf_tdata (abfd)->shstrtab_hdr.sh_name =
   7092     (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   7093   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
   7094       || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
   7095       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
   7096     return false;
   7097 
   7098   return true;
   7099 }
   7100 
   7101 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
   7102 
   7103    FIXME: We used to have code here to sort the PT_LOAD segments into
   7104    ascending order, as per the ELF spec.  But this breaks some programs,
   7105    including the Linux kernel.  But really either the spec should be
   7106    changed or the programs updated.  */
   7107 
   7108 bool
   7109 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
   7110 {
   7111   if (link_info != NULL && bfd_link_pie (link_info))
   7112     {
   7113       Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
   7114       unsigned int num_segments = i_ehdrp->e_phnum;
   7115       struct elf_obj_tdata *tdata = elf_tdata (obfd);
   7116       Elf_Internal_Phdr *segment = tdata->phdr;
   7117       Elf_Internal_Phdr *end_segment = &segment[num_segments];
   7118 
   7119       /* Find the lowest p_vaddr in PT_LOAD segments.  */
   7120       bfd_vma p_vaddr = (bfd_vma) -1;
   7121       for (; segment < end_segment; segment++)
   7122 	if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
   7123 	  p_vaddr = segment->p_vaddr;
   7124 
   7125       /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
   7126 	 segments is non-zero.  */
   7127       if (p_vaddr)
   7128 	i_ehdrp->e_type = ET_EXEC;
   7129     }
   7130   return true;
   7131 }
   7132 
   7133 /* Assign file positions for all the reloc sections which are not part
   7134    of the loadable file image, and the file position of section headers.  */
   7135 
   7136 static bool
   7137 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   7138 {
   7139   file_ptr off;
   7140   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   7141   Elf_Internal_Shdr *shdrp;
   7142   Elf_Internal_Ehdr *i_ehdrp;
   7143   elf_backend_data *bed = get_elf_backend_data (abfd);
   7144 
   7145   /* Skip non-load sections without section header.  */
   7146   if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
   7147     return true;
   7148 
   7149   off = elf_next_file_pos (abfd);
   7150 
   7151   shdrpp = elf_elfsections (abfd);
   7152   end_shdrpp = shdrpp + elf_numsections (abfd);
   7153   for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
   7154     {
   7155       shdrp = *shdrpp;
   7156       if (shdrp->sh_offset == -1)
   7157 	{
   7158 	  asection *sec = shdrp->bfd_section;
   7159 	  if (sec == NULL
   7160 	      || shdrp->sh_type == SHT_REL
   7161 	      || shdrp->sh_type == SHT_RELA)
   7162 	    ;
   7163 	  else if (bfd_section_is_ctf (sec))
   7164 	    {
   7165 	      /* Update section size and contents.	*/
   7166 	      shdrp->sh_size = sec->size;
   7167 	      shdrp->contents = sec->contents;
   7168 	    }
   7169 	  else if (shdrp->sh_name == -1u)
   7170 	    {
   7171 	      const char *name = sec->name;
   7172 	      struct bfd_elf_section_data *d;
   7173 
   7174 	      /* Compress DWARF debug sections.  */
   7175 	      if (!bfd_compress_section (abfd, sec, shdrp->contents))
   7176 		return false;
   7177 
   7178 	      if (sec->compress_status == COMPRESS_SECTION_DONE
   7179 		  && (abfd->flags & BFD_COMPRESS_GABI) == 0
   7180 		  && name[1] == 'd')
   7181 		{
   7182 		  /* If section is compressed with zlib-gnu, convert
   7183 		     section name from .debug_* to .zdebug_*.  */
   7184 		  char *new_name = bfd_debug_name_to_zdebug (abfd, name);
   7185 		  if (new_name == NULL)
   7186 		    return false;
   7187 		  name = new_name;
   7188 		}
   7189 	      /* Add section name to section name section.  */
   7190 	      shdrp->sh_name
   7191 		= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
   7192 						      name, false);
   7193 	      d = elf_section_data (sec);
   7194 
   7195 	      /* Add reloc section name to section name section.  */
   7196 	      if (d->rel.hdr
   7197 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
   7198 						  name, false))
   7199 		return false;
   7200 	      if (d->rela.hdr
   7201 		  && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
   7202 						  name, true))
   7203 		return false;
   7204 
   7205 	      /* Update section size and contents.  */
   7206 	      shdrp->sh_size = sec->size;
   7207 	      shdrp->contents = sec->contents;
   7208 	      sec->contents = NULL;
   7209 	    }
   7210 
   7211 	  off = _bfd_elf_assign_file_position_for_section (shdrp, off,
   7212 		  (abfd->flags & (EXEC_P | DYNAMIC))
   7213 		  || bfd_get_format (abfd) == bfd_core,
   7214 		  bed->s->log_file_align);
   7215 	}
   7216     }
   7217 
   7218   /* Place section name section after DWARF debug sections have been
   7219      compressed.  */
   7220   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   7221   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   7222   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   7223   off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
   7224 
   7225   /* Place the section headers.  */
   7226   i_ehdrp = elf_elfheader (abfd);
   7227   off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
   7228   i_ehdrp->e_shoff = off;
   7229   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
   7230   elf_next_file_pos (abfd) = off;
   7231 
   7232   return true;
   7233 }
   7234 
   7235 bool
   7236 _bfd_elf_write_object_contents (bfd *abfd)
   7237 {
   7238   elf_backend_data *bed = get_elf_backend_data (abfd);
   7239   Elf_Internal_Shdr **i_shdrp;
   7240   bool failed;
   7241   unsigned int count, num_sec;
   7242   struct elf_obj_tdata *t;
   7243 
   7244   if (! abfd->output_has_begun
   7245       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   7246     return false;
   7247   /* Do not rewrite ELF data when the BFD has been opened for update.
   7248      abfd->output_has_begun was set to TRUE on opening, so creation of
   7249      new sections, and modification of existing section sizes was
   7250      restricted.  This means the ELF header, program headers and
   7251      section headers can't have changed.  If the contents of any
   7252      sections has been modified, then those changes have already been
   7253      written to the BFD.  */
   7254   else if (abfd->direction == both_direction)
   7255     {
   7256       BFD_ASSERT (abfd->output_has_begun);
   7257       return true;
   7258     }
   7259 
   7260   i_shdrp = elf_elfsections (abfd);
   7261 
   7262   failed = false;
   7263   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
   7264   if (failed)
   7265     return false;
   7266 
   7267   if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
   7268     return false;
   7269 
   7270   /* After writing the headers, we need to write the sections too...  */
   7271   num_sec = elf_numsections (abfd);
   7272   for (count = 1; count < num_sec; count++)
   7273     {
   7274       /* Don't set the sh_name field without section header.  */
   7275       if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
   7276 	i_shdrp[count]->sh_name
   7277 	  = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
   7278 				    i_shdrp[count]->sh_name);
   7279       if (bed->elf_backend_section_processing)
   7280 	if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
   7281 	  return false;
   7282       if (i_shdrp[count]->contents)
   7283 	{
   7284 	  bfd_size_type amt = i_shdrp[count]->sh_size;
   7285 
   7286 	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
   7287 	      || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
   7288 	    return false;
   7289 	}
   7290     }
   7291 
   7292   /* Write out the section header names.  */
   7293   t = elf_tdata (abfd);
   7294   if (elf_shstrtab (abfd) != NULL
   7295       && t->shstrtab_hdr.sh_offset != -1
   7296       && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
   7297 	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
   7298     return false;
   7299 
   7300   if (!(*bed->elf_backend_final_write_processing) (abfd))
   7301     return false;
   7302 
   7303   if (!bed->s->write_shdrs_and_ehdr (abfd))
   7304     return false;
   7305 
   7306   /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
   7307   if (t->o->build_id.after_write_object_contents != NULL
   7308       && !(*t->o->build_id.after_write_object_contents) (abfd))
   7309     return false;
   7310   if (t->o->package_metadata.after_write_object_contents != NULL
   7311       && !(*t->o->package_metadata.after_write_object_contents) (abfd))
   7312     return false;
   7313 
   7314   return true;
   7315 }
   7316 
   7317 bool
   7318 _bfd_elf_write_corefile_contents (bfd *abfd)
   7319 {
   7320   /* Hopefully this can be done just like an object file.  */
   7321   return _bfd_elf_write_object_contents (abfd);
   7322 }
   7323 
   7324 /* Given a section, search the header to find them.  */
   7325 
   7326 unsigned int
   7327 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   7328 {
   7329   elf_backend_data *bed;
   7330   unsigned int sec_index;
   7331 
   7332   if (elf_section_data (asect) != NULL
   7333       && elf_section_data (asect)->this_idx != 0)
   7334     return elf_section_data (asect)->this_idx;
   7335 
   7336   if (bfd_is_abs_section (asect))
   7337     sec_index = SHN_ABS;
   7338   else if (bfd_is_com_section (asect))
   7339     sec_index = SHN_COMMON;
   7340   else if (bfd_is_und_section (asect))
   7341     sec_index = SHN_UNDEF;
   7342   else
   7343     sec_index = SHN_BAD;
   7344 
   7345   bed = get_elf_backend_data (abfd);
   7346   if (bed->elf_backend_section_from_bfd_section)
   7347     {
   7348       int retval = sec_index;
   7349 
   7350       if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
   7351 	return retval;
   7352     }
   7353 
   7354   if (sec_index == SHN_BAD)
   7355     bfd_set_error (bfd_error_nonrepresentable_section);
   7356 
   7357   return sec_index;
   7358 }
   7359 
   7360 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
   7361    on error.  */
   7362 
   7363 int
   7364 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
   7365 {
   7366   asymbol *asym_ptr = *asym_ptr_ptr;
   7367   int idx;
   7368   flagword flags = asym_ptr->flags;
   7369 
   7370   /* When gas creates relocations against local labels, it creates its
   7371      own symbol for the section, but does put the symbol into the
   7372      symbol chain, so udata is 0.  When the linker is generating
   7373      relocatable output, this section symbol may be for one of the
   7374      input sections rather than the output section.  */
   7375   if (asym_ptr->udata.i == 0
   7376       && (flags & BSF_SECTION_SYM)
   7377       && asym_ptr->section)
   7378     {
   7379       asection *sec;
   7380 
   7381       sec = asym_ptr->section;
   7382       if (sec->owner != abfd && sec->output_section != NULL)
   7383 	sec = sec->output_section;
   7384       if (sec->owner == abfd
   7385 	  && sec->index < elf_num_section_syms (abfd)
   7386 	  && elf_section_syms (abfd)[sec->index] != NULL)
   7387 	asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
   7388     }
   7389 
   7390   idx = asym_ptr->udata.i;
   7391 
   7392   if (idx == 0)
   7393     {
   7394       /* This case can occur when using --strip-symbol on a symbol
   7395 	 which is used in a relocation entry.  */
   7396       _bfd_error_handler
   7397 	/* xgettext:c-format */
   7398 	(_("%pB: symbol `%s' required but not present"),
   7399 	 abfd, bfd_asymbol_name (asym_ptr));
   7400       bfd_set_error (bfd_error_no_symbols);
   7401       return -1;
   7402     }
   7403 
   7404 #if DEBUG & 4
   7405   {
   7406     fprintf (stderr,
   7407 	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
   7408 	     " flags = 0x%.8x\n",
   7409 	     (long) asym_ptr, asym_ptr->name, idx, flags);
   7410     fflush (stderr);
   7411   }
   7412 #endif
   7413 
   7414   return idx;
   7415 }
   7416 
   7417 static inline bfd_vma
   7418 segment_size (Elf_Internal_Phdr *segment)
   7419 {
   7420   return (segment->p_memsz > segment->p_filesz
   7421 	  ? segment->p_memsz : segment->p_filesz);
   7422 }
   7423 
   7424 
   7425 /* Returns the end address of the segment + 1.  */
   7426 static inline bfd_vma
   7427 segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
   7428 {
   7429   return start + segment_size (segment);
   7430 }
   7431 
   7432 static inline bfd_size_type
   7433 section_size (asection *section, Elf_Internal_Phdr *segment)
   7434 {
   7435   if ((section->flags & SEC_HAS_CONTENTS) != 0
   7436       || (section->flags & SEC_THREAD_LOCAL) == 0
   7437       || segment->p_type == PT_TLS)
   7438     return section->size;
   7439   return 0;
   7440 }
   7441 
   7442 /* Returns TRUE if the given section is contained within the given
   7443    segment.  LMA addresses are compared against PADDR when
   7444    USE_VADDR is false, VMA against VADDR when true.  */
   7445 static bool
   7446 is_contained_by (asection *section, Elf_Internal_Phdr *segment,
   7447 		 bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
   7448 		 bool use_vaddr)
   7449 {
   7450   bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
   7451   bfd_vma addr = !use_vaddr ? section->lma : section->vma;
   7452   bfd_vma octet;
   7453   if (_bfd_mul_overflow (addr, opb, &octet))
   7454     return false;
   7455   /* The third and fourth lines below are testing that the section end
   7456      address is within the segment.  It's written this way to avoid
   7457      overflow.  Add seg_addr + section_size to both sides of the
   7458      inequality to make it obvious.  */
   7459   return (octet >= seg_addr
   7460 	  && segment_size (segment) >= section_size (section, segment)
   7461 	  && (octet - seg_addr
   7462 	      <= segment_size (segment) - section_size (section, segment)));
   7463 }
   7464 
   7465 /* Handle PT_NOTE segment.  */
   7466 static bool
   7467 is_note (asection *s, Elf_Internal_Phdr *p)
   7468 {
   7469   return (p->p_type == PT_NOTE
   7470 	  && elf_section_type (s) == SHT_NOTE
   7471 	  && (ufile_ptr) s->filepos >= p->p_offset
   7472 	  && p->p_filesz >= s->size
   7473 	  && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
   7474 }
   7475 
   7476 /* Rewrite program header information.  */
   7477 
   7478 static bool
   7479 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   7480 {
   7481   Elf_Internal_Ehdr *iehdr;
   7482   struct elf_segment_map *map;
   7483   struct elf_segment_map *map_first;
   7484   struct elf_segment_map **pointer_to_map;
   7485   Elf_Internal_Phdr *segment;
   7486   asection *section;
   7487   unsigned int i;
   7488   unsigned int num_segments;
   7489   bool phdr_included = false;
   7490   bool p_paddr_valid;
   7491   struct elf_segment_map *phdr_adjust_seg = NULL;
   7492   unsigned int phdr_adjust_num = 0;
   7493   elf_backend_data *bed;
   7494   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   7495 
   7496   bed = get_elf_backend_data (ibfd);
   7497   iehdr = elf_elfheader (ibfd);
   7498 
   7499   map_first = NULL;
   7500   pointer_to_map = &map_first;
   7501 
   7502   num_segments = elf_elfheader (ibfd)->e_phnum;
   7503 
   7504   /* The complicated case when p_vaddr is 0 is to handle the Solaris
   7505      linker, which generates a PT_INTERP section with p_vaddr and
   7506      p_memsz set to 0.  */
   7507 #define IS_SOLARIS_PT_INTERP(p, s)					\
   7508   (p->p_vaddr == 0							\
   7509    && p->p_paddr == 0							\
   7510    && p->p_memsz == 0							\
   7511    && p->p_filesz > 0							\
   7512    && (s->flags & SEC_HAS_CONTENTS) != 0				\
   7513    && s->size > 0							\
   7514    && (bfd_vma) s->filepos >= p->p_offset				\
   7515    && ((bfd_vma) s->filepos + s->size					\
   7516        <= p->p_offset + p->p_filesz))
   7517 
   7518   /* Decide if the given section should be included in the given segment.
   7519      A section will be included if:
   7520        1. It is within the address space of the segment -- we use the LMA
   7521 	  if that is set for the segment and the VMA otherwise,
   7522        2. It is an allocated section or a NOTE section in a PT_NOTE
   7523 	  segment.
   7524        3. There is an output section associated with it,
   7525        4. The section has not already been allocated to a previous segment.
   7526        5. PT_GNU_STACK segments do not include any sections.
   7527        6. PT_TLS segment includes only SHF_TLS sections.
   7528        7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
   7529        8. PT_DYNAMIC should not contain empty sections at the beginning
   7530 	  (with the possible exception of .dynamic).  */
   7531 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid)	\
   7532   (((is_contained_by (section, segment, segment->p_paddr,		\
   7533 		      segment->p_vaddr, opb, !paddr_valid)		\
   7534      && (section->flags & SEC_ALLOC) != 0)				\
   7535     || is_note (section, segment))					\
   7536    && segment->p_type != PT_GNU_STACK					\
   7537    && (segment->p_type != PT_TLS					\
   7538        || (section->flags & SEC_THREAD_LOCAL))				\
   7539    && (segment->p_type == PT_LOAD					\
   7540        || segment->p_type == PT_TLS					\
   7541        || (section->flags & SEC_THREAD_LOCAL) == 0)			\
   7542    && (segment->p_type != PT_DYNAMIC					\
   7543        || section_size (section, segment) > 0				\
   7544        || (segment->p_paddr						\
   7545 	   ? segment->p_paddr != section->lma * (opb)			\
   7546 	   : segment->p_vaddr != section->vma * (opb))			\
   7547        || (streq (bfd_section_name (section), ".dynamic")))		\
   7548    && (segment->p_type != PT_LOAD || !section->segment_mark))
   7549 
   7550 /* If the output section of a section in the input segment is NULL,
   7551    it is removed from the corresponding output segment.   */
   7552 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)	\
   7553   (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid)	\
   7554    && section->output_section != NULL)
   7555 
   7556   /* Returns TRUE iff seg1 starts after the end of seg2.  */
   7557 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
   7558   (seg1->field >= segment_end (seg2, seg2->field))
   7559 
   7560   /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
   7561      their VMA address ranges and their LMA address ranges overlap.
   7562      It is possible to have overlapping VMA ranges without overlapping LMA
   7563      ranges.  RedBoot images for example can have both .data and .bss mapped
   7564      to the same VMA range, but with the .data section mapped to a different
   7565      LMA.  */
   7566 #define SEGMENT_OVERLAPS(seg1, seg2)					\
   7567   (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
   7568 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
   7569    && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
   7570 	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
   7571 
   7572   /* Initialise the segment mark field, and discard stupid alignment.  */
   7573   for (section = ibfd->sections; section != NULL; section = section->next)
   7574     {
   7575       asection *o = section->output_section;
   7576       if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
   7577 	o->alignment_power = 0;
   7578       section->segment_mark = false;
   7579     }
   7580 
   7581   /* The Solaris linker creates program headers in which all the
   7582      p_paddr fields are zero.  When we try to objcopy or strip such a
   7583      file, we get confused.  Check for this case, and if we find it
   7584      don't set the p_paddr_valid fields.  */
   7585   p_paddr_valid = false;
   7586   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7587        i < num_segments;
   7588        i++, segment++)
   7589     if (segment->p_paddr != 0)
   7590       {
   7591 	p_paddr_valid = true;
   7592 	break;
   7593       }
   7594 
   7595   /* Scan through the segments specified in the program header
   7596      of the input BFD.  For this first scan we look for overlaps
   7597      in the loadable segments.  These can be created by weird
   7598      parameters to objcopy.  Also, fix some solaris weirdness.  */
   7599   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7600        i < num_segments;
   7601        i++, segment++)
   7602     {
   7603       unsigned int j;
   7604       Elf_Internal_Phdr *segment2;
   7605 
   7606       if (segment->p_type == PT_INTERP)
   7607 	for (section = ibfd->sections; section; section = section->next)
   7608 	  if (IS_SOLARIS_PT_INTERP (segment, section))
   7609 	    {
   7610 	      /* Mininal change so that the normal section to segment
   7611 		 assignment code will work.  */
   7612 	      segment->p_vaddr = section->vma * opb;
   7613 	      break;
   7614 	    }
   7615 
   7616       if (segment->p_type != PT_LOAD)
   7617 	{
   7618 	  /* Remove PT_GNU_RELRO segment.  */
   7619 	  if (segment->p_type == PT_GNU_RELRO)
   7620 	    segment->p_type = PT_NULL;
   7621 	  continue;
   7622 	}
   7623 
   7624       /* Determine if this segment overlaps any previous segments.  */
   7625       for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
   7626 	{
   7627 	  bfd_signed_vma extra_length;
   7628 
   7629 	  if (segment2->p_type != PT_LOAD
   7630 	      || !SEGMENT_OVERLAPS (segment, segment2))
   7631 	    continue;
   7632 
   7633 	  /* Merge the two segments together.  */
   7634 	  if (segment2->p_vaddr < segment->p_vaddr)
   7635 	    {
   7636 	      /* Extend SEGMENT2 to include SEGMENT and then delete
   7637 		 SEGMENT.  */
   7638 	      extra_length = (segment_end (segment, segment->p_vaddr)
   7639 			      - segment_end (segment2, segment2->p_vaddr));
   7640 
   7641 	      if (extra_length > 0)
   7642 		{
   7643 		  segment2->p_memsz += extra_length;
   7644 		  segment2->p_filesz += extra_length;
   7645 		}
   7646 
   7647 	      segment->p_type = PT_NULL;
   7648 
   7649 	      /* Since we have deleted P we must restart the outer loop.  */
   7650 	      i = 0;
   7651 	      segment = elf_tdata (ibfd)->phdr;
   7652 	      break;
   7653 	    }
   7654 	  else
   7655 	    {
   7656 	      /* Extend SEGMENT to include SEGMENT2 and then delete
   7657 		 SEGMENT2.  */
   7658 	      extra_length = (segment_end (segment2, segment2->p_vaddr)
   7659 			      - segment_end (segment, segment->p_vaddr));
   7660 
   7661 	      if (extra_length > 0)
   7662 		{
   7663 		  segment->p_memsz += extra_length;
   7664 		  segment->p_filesz += extra_length;
   7665 		}
   7666 
   7667 	      segment2->p_type = PT_NULL;
   7668 	    }
   7669 	}
   7670     }
   7671 
   7672   /* The second scan attempts to assign sections to segments.  */
   7673   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   7674        i < num_segments;
   7675        i++, segment++)
   7676     {
   7677       unsigned int section_count;
   7678       asection **sections;
   7679       asection *output_section;
   7680       unsigned int isec;
   7681       asection *matching_lma;
   7682       asection *suggested_lma;
   7683       unsigned int j;
   7684       size_t amt;
   7685       asection *first_section;
   7686 
   7687       if (segment->p_type == PT_NULL)
   7688 	continue;
   7689 
   7690       first_section = NULL;
   7691       /* Compute how many sections might be placed into this segment.  */
   7692       for (section = ibfd->sections, section_count = 0;
   7693 	   section != NULL;
   7694 	   section = section->next)
   7695 	{
   7696 	  /* Find the first section in the input segment, which may be
   7697 	     removed from the corresponding output segment.   */
   7698 	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
   7699 	    {
   7700 	      if (first_section == NULL)
   7701 		first_section = section;
   7702 	      if (section->output_section != NULL)
   7703 		++section_count;
   7704 	    }
   7705 	}
   7706 
   7707       /* Allocate a segment map big enough to contain
   7708 	 all of the sections we have selected.  */
   7709       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   7710       amt += section_count * sizeof (asection *);
   7711       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   7712       if (map == NULL)
   7713 	return false;
   7714 
   7715       /* Initialise the fields of the segment map.  Default to
   7716 	 using the physical address of the segment in the input BFD.  */
   7717       map->next = NULL;
   7718       map->p_type = segment->p_type;
   7719       map->p_flags = segment->p_flags;
   7720       map->p_flags_valid = 1;
   7721 
   7722       if (map->p_type == PT_LOAD
   7723 	  && (ibfd->flags & D_PAGED) != 0
   7724 	  && maxpagesize > 1
   7725 	  && segment->p_align > 1)
   7726 	{
   7727 	  map->p_align = segment->p_align;
   7728 	  if (segment->p_align > maxpagesize)
   7729 	    map->p_align = maxpagesize;
   7730 	  map->p_align_valid = 1;
   7731 	}
   7732 
   7733       /* If the first section in the input segment is removed, there is
   7734 	 no need to preserve segment physical address in the corresponding
   7735 	 output segment.  */
   7736       if (!first_section || first_section->output_section != NULL)
   7737 	{
   7738 	  map->p_paddr = segment->p_paddr;
   7739 	  map->p_paddr_valid = p_paddr_valid;
   7740 	}
   7741 
   7742       /* Determine if this segment contains the ELF file header
   7743 	 and if it contains the program headers themselves.  */
   7744       map->includes_filehdr = (segment->p_offset == 0
   7745 			       && segment->p_filesz >= iehdr->e_ehsize);
   7746       map->includes_phdrs = 0;
   7747 
   7748       if (!phdr_included || segment->p_type != PT_LOAD)
   7749 	{
   7750 	  map->includes_phdrs =
   7751 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   7752 	     && (segment->p_offset + segment->p_filesz
   7753 		 >= ((bfd_vma) iehdr->e_phoff
   7754 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   7755 
   7756 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   7757 	    phdr_included = true;
   7758 	}
   7759 
   7760       if (section_count == 0)
   7761 	{
   7762 	  /* Special segments, such as the PT_PHDR segment, may contain
   7763 	     no sections, but ordinary, loadable segments should contain
   7764 	     something.  They are allowed by the ELF spec however, so only
   7765 	     a warning is produced.
   7766 	     Don't warn if an empty PT_LOAD contains the program headers.
   7767 	     There is however the valid use case of embedded systems which
   7768 	     have segments with p_filesz of 0 and a p_memsz > 0 to initialize
   7769 	     flash memory with zeros.  No warning is shown for that case.  */
   7770 	  if (segment->p_type == PT_LOAD
   7771 	      && !map->includes_phdrs
   7772 	      && (segment->p_filesz > 0 || segment->p_memsz == 0))
   7773 	    /* xgettext:c-format */
   7774 	    _bfd_error_handler
   7775 	      (_("%pB: warning: empty loadable segment detected"
   7776 		 " at vaddr=%#" PRIx64 ", is this intentional?"),
   7777 	       ibfd, (uint64_t) segment->p_vaddr);
   7778 
   7779 	  map->p_vaddr_offset = segment->p_vaddr / opb;
   7780 	  map->count = 0;
   7781 	  *pointer_to_map = map;
   7782 	  pointer_to_map = &map->next;
   7783 
   7784 	  continue;
   7785 	}
   7786 
   7787       /* Now scan the sections in the input BFD again and attempt
   7788 	 to add their corresponding output sections to the segment map.
   7789 	 The problem here is how to handle an output section which has
   7790 	 been moved (ie had its LMA changed).  There are four possibilities:
   7791 
   7792 	 1. None of the sections have been moved.
   7793 	    In this case we can continue to use the segment LMA from the
   7794 	    input BFD.
   7795 
   7796 	 2. All of the sections have been moved by the same amount.
   7797 	    In this case we can change the segment's LMA to match the LMA
   7798 	    of the first section.
   7799 
   7800 	 3. Some of the sections have been moved, others have not.
   7801 	    In this case those sections which have not been moved can be
   7802 	    placed in the current segment which will have to have its size,
   7803 	    and possibly its LMA changed, and a new segment or segments will
   7804 	    have to be created to contain the other sections.
   7805 
   7806 	 4. The sections have been moved, but not by the same amount.
   7807 	    In this case we can change the segment's LMA to match the LMA
   7808 	    of the first section and we will have to create a new segment
   7809 	    or segments to contain the other sections.
   7810 
   7811 	 In order to save time, we allocate an array to hold the section
   7812 	 pointers that we are interested in.  As these sections get assigned
   7813 	 to a segment, they are removed from this array.  */
   7814 
   7815       amt = section_count * sizeof (asection *);
   7816       sections = (asection **) bfd_malloc (amt);
   7817       if (sections == NULL)
   7818 	return false;
   7819 
   7820       /* Step One: Scan for segment vs section LMA conflicts.
   7821 	 Also add the sections to the section array allocated above.
   7822 	 Also add the sections to the current segment.  In the common
   7823 	 case, where the sections have not been moved, this means that
   7824 	 we have completely filled the segment, and there is nothing
   7825 	 more to do.  */
   7826       isec = 0;
   7827       matching_lma = NULL;
   7828       suggested_lma = NULL;
   7829 
   7830       for (section = first_section, j = 0;
   7831 	   section != NULL;
   7832 	   section = section->next)
   7833 	{
   7834 	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
   7835 	    {
   7836 	      output_section = section->output_section;
   7837 
   7838 	      sections[j++] = section;
   7839 
   7840 	      /* The Solaris native linker always sets p_paddr to 0.
   7841 		 We try to catch that case here, and set it to the
   7842 		 correct value.  Note - some backends require that
   7843 		 p_paddr be left as zero.  */
   7844 	      if (!p_paddr_valid
   7845 		  && segment->p_vaddr != 0
   7846 		  && !bed->want_p_paddr_set_to_zero
   7847 		  && isec == 0
   7848 		  && output_section->lma != 0
   7849 		  && (align_power (segment->p_vaddr
   7850 				   + (map->includes_filehdr
   7851 				      ? iehdr->e_ehsize : 0)
   7852 				   + (map->includes_phdrs
   7853 				      ? iehdr->e_phnum * iehdr->e_phentsize
   7854 				      : 0),
   7855 				   output_section->alignment_power * opb)
   7856 		      == (output_section->vma * opb)))
   7857 		map->p_paddr = segment->p_vaddr;
   7858 
   7859 	      /* Match up the physical address of the segment with the
   7860 		 LMA address of the output section.  */
   7861 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7862 				   0, opb, false)
   7863 		  || is_note (section, segment))
   7864 		{
   7865 		  if (matching_lma == NULL
   7866 		      || output_section->lma < matching_lma->lma)
   7867 		    matching_lma = output_section;
   7868 
   7869 		  /* We assume that if the section fits within the segment
   7870 		     then it does not overlap any other section within that
   7871 		     segment.  */
   7872 		  map->sections[isec++] = output_section;
   7873 		}
   7874 	      else if (suggested_lma == NULL)
   7875 		suggested_lma = output_section;
   7876 
   7877 	      if (j == section_count)
   7878 		break;
   7879 	    }
   7880 	}
   7881 
   7882       BFD_ASSERT (j == section_count);
   7883 
   7884       /* Step Two: Adjust the physical address of the current segment,
   7885 	 if necessary.  */
   7886       if (isec == section_count)
   7887 	{
   7888 	  /* All of the sections fitted within the segment as currently
   7889 	     specified.  This is the default case.  Add the segment to
   7890 	     the list of built segments and carry on to process the next
   7891 	     program header in the input BFD.  */
   7892 	  map->count = section_count;
   7893 	  *pointer_to_map = map;
   7894 	  pointer_to_map = &map->next;
   7895 
   7896 	  if (p_paddr_valid
   7897 	      && !bed->want_p_paddr_set_to_zero)
   7898 	    {
   7899 	      bfd_vma hdr_size = 0;
   7900 	      if (map->includes_filehdr)
   7901 		hdr_size = iehdr->e_ehsize;
   7902 	      if (map->includes_phdrs)
   7903 		hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   7904 
   7905 	      /* Account for padding before the first section in the
   7906 		 segment.  */
   7907 	      map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   7908 				     - matching_lma->lma);
   7909 	    }
   7910 
   7911 	  free (sections);
   7912 	  continue;
   7913 	}
   7914       else
   7915 	{
   7916 	  /* Change the current segment's physical address to match
   7917 	     the LMA of the first section that fitted, or if no
   7918 	     section fitted, the first section.  */
   7919 	  if (matching_lma == NULL)
   7920 	    matching_lma = suggested_lma;
   7921 
   7922 	  map->p_paddr = matching_lma->lma * opb;
   7923 
   7924 	  /* Offset the segment physical address from the lma
   7925 	     to allow for space taken up by elf headers.  */
   7926 	  if (map->includes_phdrs)
   7927 	    {
   7928 	      map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
   7929 
   7930 	      /* iehdr->e_phnum is just an estimate of the number
   7931 		 of program headers that we will need.  Make a note
   7932 		 here of the number we used and the segment we chose
   7933 		 to hold these headers, so that we can adjust the
   7934 		 offset when we know the correct value.  */
   7935 	      phdr_adjust_num = iehdr->e_phnum;
   7936 	      phdr_adjust_seg = map;
   7937 	    }
   7938 
   7939 	  if (map->includes_filehdr)
   7940 	    {
   7941 	      bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
   7942 	      map->p_paddr -= iehdr->e_ehsize;
   7943 	      /* We've subtracted off the size of headers from the
   7944 		 first section lma, but there may have been some
   7945 		 alignment padding before that section too.  Try to
   7946 		 account for that by adjusting the segment lma down to
   7947 		 the same alignment.  */
   7948 	      if (segment->p_align != 0 && segment->p_align < align)
   7949 		align = segment->p_align;
   7950 	      map->p_paddr &= -(align * opb);
   7951 	    }
   7952 	}
   7953 
   7954       /* Step Three: Loop over the sections again, this time assigning
   7955 	 those that fit to the current segment and removing them from the
   7956 	 sections array; but making sure not to leave large gaps.  Once all
   7957 	 possible sections have been assigned to the current segment it is
   7958 	 added to the list of built segments and if sections still remain
   7959 	 to be assigned, a new segment is constructed before repeating
   7960 	 the loop.  */
   7961       isec = 0;
   7962       do
   7963 	{
   7964 	  map->count = 0;
   7965 	  suggested_lma = NULL;
   7966 
   7967 	  /* Fill the current segment with sections that fit.  */
   7968 	  for (j = 0; j < section_count; j++)
   7969 	    {
   7970 	      section = sections[j];
   7971 
   7972 	      if (section == NULL)
   7973 		continue;
   7974 
   7975 	      output_section = section->output_section;
   7976 
   7977 	      BFD_ASSERT (output_section != NULL);
   7978 
   7979 	      if (is_contained_by (output_section, segment, map->p_paddr,
   7980 				   0, opb, false)
   7981 		  || is_note (section, segment))
   7982 		{
   7983 		  if (map->count == 0)
   7984 		    {
   7985 		      /* If the first section in a segment does not start at
   7986 			 the beginning of the segment, then something is
   7987 			 wrong.  */
   7988 		      if (align_power (map->p_paddr
   7989 				       + (map->includes_filehdr
   7990 					  ? iehdr->e_ehsize : 0)
   7991 				       + (map->includes_phdrs
   7992 					  ? iehdr->e_phnum * iehdr->e_phentsize
   7993 					  : 0),
   7994 				       output_section->alignment_power * opb)
   7995 			  != output_section->lma * opb)
   7996 			goto sorry;
   7997 		    }
   7998 		  else
   7999 		    {
   8000 		      asection *prev_sec;
   8001 
   8002 		      prev_sec = map->sections[map->count - 1];
   8003 
   8004 		      /* If the gap between the end of the previous section
   8005 			 and the start of this section is more than
   8006 			 maxpagesize then we need to start a new segment.  */
   8007 		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
   8008 				      maxpagesize)
   8009 			   < BFD_ALIGN (output_section->lma, maxpagesize))
   8010 			  || (prev_sec->lma + prev_sec->size
   8011 			      > output_section->lma))
   8012 			{
   8013 			  if (suggested_lma == NULL)
   8014 			    suggested_lma = output_section;
   8015 
   8016 			  continue;
   8017 			}
   8018 		    }
   8019 
   8020 		  map->sections[map->count++] = output_section;
   8021 		  ++isec;
   8022 		  sections[j] = NULL;
   8023 		  if (segment->p_type == PT_LOAD)
   8024 		    section->segment_mark = true;
   8025 		}
   8026 	      else if (suggested_lma == NULL)
   8027 		suggested_lma = output_section;
   8028 	    }
   8029 
   8030 	  /* PR 23932.  A corrupt input file may contain sections that cannot
   8031 	     be assigned to any segment - because for example they have a
   8032 	     negative size - or segments that do not contain any sections.
   8033 	     But there are also valid reasons why a segment can be empty.
   8034 	     So allow a count of zero.  */
   8035 
   8036 	  /* Add the current segment to the list of built segments.  */
   8037 	  *pointer_to_map = map;
   8038 	  pointer_to_map = &map->next;
   8039 
   8040 	  if (isec < section_count)
   8041 	    {
   8042 	      /* We still have not allocated all of the sections to
   8043 		 segments.  Create a new segment here, initialise it
   8044 		 and carry on looping.  */
   8045 	      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8046 	      amt += section_count * sizeof (asection *);
   8047 	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8048 	      if (map == NULL)
   8049 		{
   8050 		  free (sections);
   8051 		  return false;
   8052 		}
   8053 
   8054 	      /* Initialise the fields of the segment map.  Set the physical
   8055 		 physical address to the LMA of the first section that has
   8056 		 not yet been assigned.  */
   8057 	      map->next = NULL;
   8058 	      map->p_type = segment->p_type;
   8059 	      map->p_flags = segment->p_flags;
   8060 	      map->p_flags_valid = 1;
   8061 	      map->p_paddr = suggested_lma->lma * opb;
   8062 	      map->p_paddr_valid = p_paddr_valid;
   8063 	      map->includes_filehdr = 0;
   8064 	      map->includes_phdrs = 0;
   8065 	    }
   8066 
   8067 	  continue;
   8068 	sorry:
   8069 	  bfd_set_error (bfd_error_sorry);
   8070 	  free (sections);
   8071 	  return false;
   8072 	}
   8073       while (isec < section_count);
   8074 
   8075       free (sections);
   8076     }
   8077 
   8078   elf_seg_map (obfd) = map_first;
   8079 
   8080   /* If we had to estimate the number of program headers that were
   8081      going to be needed, then check our estimate now and adjust
   8082      the offset if necessary.  */
   8083   if (phdr_adjust_seg != NULL)
   8084     {
   8085       unsigned int count;
   8086 
   8087       for (count = 0, map = map_first; map != NULL; map = map->next)
   8088 	count++;
   8089 
   8090       if (count > phdr_adjust_num)
   8091 	phdr_adjust_seg->p_paddr
   8092 	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
   8093 
   8094       for (map = map_first; map != NULL; map = map->next)
   8095 	if (map->p_type == PT_PHDR)
   8096 	  {
   8097 	    bfd_vma adjust
   8098 	      = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
   8099 	    map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
   8100 	    break;
   8101 	  }
   8102     }
   8103 
   8104 #undef IS_SOLARIS_PT_INTERP
   8105 #undef IS_SECTION_IN_INPUT_SEGMENT
   8106 #undef INCLUDE_SECTION_IN_SEGMENT
   8107 #undef SEGMENT_AFTER_SEGMENT
   8108 #undef SEGMENT_OVERLAPS
   8109   return true;
   8110 }
   8111 
   8112 /* Return true if p_align in the ELF program header in ABFD is valid.  */
   8113 
   8114 static bool
   8115 elf_is_p_align_valid (bfd *abfd)
   8116 {
   8117   unsigned int i;
   8118   Elf_Internal_Phdr *segment;
   8119   unsigned int num_segments;
   8120   elf_backend_data *bed = get_elf_backend_data (abfd);
   8121   bfd_size_type maxpagesize = bed->maxpagesize;
   8122   bfd_size_type p_align = bed->p_align;
   8123 
   8124   /* Return true if the default p_align value isn't set or the maximum
   8125      page size is the same as the minimum page size.  */
   8126   if (p_align == 0 || maxpagesize == bed->minpagesize)
   8127     return true;
   8128 
   8129   /* When the default p_align value is set, p_align may be set to the
   8130      default p_align value while segments are aligned to the maximum
   8131      page size.  In this case, the input p_align will be ignored and
   8132      the maximum page size will be used to align the output segments.  */
   8133   segment = elf_tdata (abfd)->phdr;
   8134   num_segments = elf_elfheader (abfd)->e_phnum;
   8135   for (i = 0; i < num_segments; i++, segment++)
   8136     if (segment->p_type == PT_LOAD
   8137 	&& (segment->p_align != p_align
   8138 	    || vma_page_aligned_bias (segment->p_vaddr,
   8139 				      segment->p_offset,
   8140 				      maxpagesize) != 0))
   8141       return true;
   8142 
   8143   return false;
   8144 }
   8145 
   8146 /* Copy ELF program header information.  */
   8147 
   8148 static bool
   8149 copy_elf_program_header (bfd *ibfd, bfd *obfd)
   8150 {
   8151   Elf_Internal_Ehdr *iehdr;
   8152   struct elf_segment_map *map;
   8153   struct elf_segment_map *map_first;
   8154   struct elf_segment_map **pointer_to_map;
   8155   Elf_Internal_Phdr *segment;
   8156   unsigned int i;
   8157   unsigned int num_segments;
   8158   bool phdr_included = false;
   8159   bool p_paddr_valid;
   8160   bool p_palign_valid;
   8161   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
   8162 
   8163   iehdr = elf_elfheader (ibfd);
   8164 
   8165   map_first = NULL;
   8166   pointer_to_map = &map_first;
   8167 
   8168   /* If all the segment p_paddr fields are zero, don't set
   8169      map->p_paddr_valid.  */
   8170   p_paddr_valid = false;
   8171   num_segments = elf_elfheader (ibfd)->e_phnum;
   8172   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8173        i < num_segments;
   8174        i++, segment++)
   8175     if (segment->p_paddr != 0)
   8176       {
   8177 	p_paddr_valid = true;
   8178 	break;
   8179       }
   8180 
   8181   p_palign_valid = elf_is_p_align_valid (ibfd);
   8182 
   8183   for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8184        i < num_segments;
   8185        i++, segment++)
   8186     {
   8187       asection *section;
   8188       unsigned int section_count;
   8189       size_t amt;
   8190       Elf_Internal_Shdr *this_hdr;
   8191       asection *first_section = NULL;
   8192       asection *lowest_section;
   8193 
   8194       /* Compute how many sections are in this segment.  */
   8195       for (section = ibfd->sections, section_count = 0;
   8196 	   section != NULL;
   8197 	   section = section->next)
   8198 	{
   8199 	  this_hdr = &(elf_section_data(section)->this_hdr);
   8200 	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8201 	    {
   8202 	      if (first_section == NULL)
   8203 		first_section = section;
   8204 	      section_count++;
   8205 	    }
   8206 	}
   8207 
   8208       /* Allocate a segment map big enough to contain
   8209 	 all of the sections we have selected.  */
   8210       amt = sizeof (struct elf_segment_map) - sizeof (asection *);
   8211       amt += section_count * sizeof (asection *);
   8212       map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
   8213       if (map == NULL)
   8214 	return false;
   8215 
   8216       /* Initialize the fields of the output segment map with the
   8217 	 input segment.  */
   8218       map->next = NULL;
   8219       map->p_type = segment->p_type;
   8220       map->p_flags = segment->p_flags;
   8221       map->p_flags_valid = 1;
   8222       map->p_paddr = segment->p_paddr;
   8223       map->p_paddr_valid = p_paddr_valid;
   8224       map->p_align = segment->p_align;
   8225       /* Keep p_align of PT_GNU_STACK for stack alignment.  */
   8226       map->p_align_valid = (map->p_type == PT_GNU_STACK
   8227 			    || p_palign_valid);
   8228       map->p_vaddr_offset = 0;
   8229 
   8230       if (map->p_type == PT_GNU_RELRO
   8231 	  || map->p_type == PT_GNU_STACK)
   8232 	{
   8233 	  /* The PT_GNU_RELRO segment may contain the first a few
   8234 	     bytes in the .got.plt section even if the whole .got.plt
   8235 	     section isn't in the PT_GNU_RELRO segment.  We won't
   8236 	     change the size of the PT_GNU_RELRO segment.
   8237 	     Similarly, PT_GNU_STACK size is significant on uclinux
   8238 	     systems.    */
   8239 	  map->p_size = segment->p_memsz;
   8240 	  map->p_size_valid = 1;
   8241 	}
   8242 
   8243       /* Determine if this segment contains the ELF file header
   8244 	 and if it contains the program headers themselves.  */
   8245       map->includes_filehdr = (segment->p_offset == 0
   8246 			       && segment->p_filesz >= iehdr->e_ehsize);
   8247 
   8248       map->includes_phdrs = 0;
   8249       if (! phdr_included || segment->p_type != PT_LOAD)
   8250 	{
   8251 	  map->includes_phdrs =
   8252 	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
   8253 	     && (segment->p_offset + segment->p_filesz
   8254 		 >= ((bfd_vma) iehdr->e_phoff
   8255 		     + iehdr->e_phnum * iehdr->e_phentsize)));
   8256 
   8257 	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
   8258 	    phdr_included = true;
   8259 	}
   8260 
   8261       lowest_section = NULL;
   8262       if (section_count != 0)
   8263 	{
   8264 	  unsigned int isec = 0;
   8265 
   8266 	  for (section = first_section;
   8267 	       section != NULL;
   8268 	       section = section->next)
   8269 	    {
   8270 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8271 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8272 		{
   8273 		  map->sections[isec++] = section->output_section;
   8274 		  if ((section->flags & SEC_ALLOC) != 0)
   8275 		    {
   8276 		      bfd_vma seg_off;
   8277 
   8278 		      if (lowest_section == NULL
   8279 			  || section->lma < lowest_section->lma)
   8280 			lowest_section = section;
   8281 
   8282 		      /* Section lmas are set up from PT_LOAD header
   8283 			 p_paddr in _bfd_elf_make_section_from_shdr.
   8284 			 If this header has a p_paddr that disagrees
   8285 			 with the section lma, flag the p_paddr as
   8286 			 invalid.  */
   8287 		      if ((section->flags & SEC_LOAD) != 0)
   8288 			seg_off = this_hdr->sh_offset - segment->p_offset;
   8289 		      else
   8290 			seg_off = this_hdr->sh_addr - segment->p_vaddr;
   8291 		      if (section->lma * opb - segment->p_paddr != seg_off)
   8292 			map->p_paddr_valid = false;
   8293 		    }
   8294 		  if (isec == section_count)
   8295 		    break;
   8296 		}
   8297 	    }
   8298 	}
   8299 
   8300       if (section_count == 0)
   8301 	map->p_vaddr_offset = segment->p_vaddr / opb;
   8302       else if (map->p_paddr_valid)
   8303 	{
   8304 	  /* Account for padding before the first section in the segment.  */
   8305 	  bfd_vma hdr_size = 0;
   8306 	  if (map->includes_filehdr)
   8307 	    hdr_size = iehdr->e_ehsize;
   8308 	  if (map->includes_phdrs)
   8309 	    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
   8310 
   8311 	  map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
   8312 				 - (lowest_section ? lowest_section->lma : 0));
   8313 	}
   8314 
   8315       map->count = section_count;
   8316       *pointer_to_map = map;
   8317       pointer_to_map = &map->next;
   8318     }
   8319 
   8320   elf_seg_map (obfd) = map_first;
   8321   return true;
   8322 }
   8323 
   8324 /* Copy private BFD data.  This copies or rewrites ELF program header
   8325    information.  */
   8326 
   8327 static bool
   8328 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   8329 {
   8330   bfd_vma maxpagesize;
   8331 
   8332   if (elf_tdata (ibfd)->phdr == NULL)
   8333     return true;
   8334 
   8335   if (ibfd->xvec == obfd->xvec)
   8336     {
   8337       /* Check to see if any sections in the input BFD
   8338 	 covered by ELF program header have changed.  */
   8339       Elf_Internal_Phdr *segment;
   8340       asection * section;
   8341       asection * osec;
   8342       asection * prev;
   8343       unsigned int i, num_segments;
   8344       Elf_Internal_Shdr *this_hdr;
   8345       elf_backend_data *bed;
   8346 
   8347       bed = get_elf_backend_data (ibfd);
   8348 
   8349       /* Regenerate the segment map if p_paddr is set to 0.  */
   8350       if (bed->want_p_paddr_set_to_zero)
   8351 	goto rewrite;
   8352 
   8353       /* Initialize the segment mark field.  */
   8354       for (section = obfd->sections; section != NULL;
   8355 	   section = section->next)
   8356 	section->segment_mark = false;
   8357 
   8358       num_segments = elf_elfheader (ibfd)->e_phnum;
   8359       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8360 	   i < num_segments;
   8361 	   i++, segment++)
   8362 	{
   8363 	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
   8364 	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
   8365 	     which severly confuses things, so always regenerate the segment
   8366 	     map in this case.  */
   8367 	  if (segment->p_paddr == 0
   8368 	      && segment->p_memsz == 0
   8369 	      && (segment->p_type == PT_INTERP
   8370 		  || segment->p_type == PT_DYNAMIC))
   8371 	    goto rewrite;
   8372 
   8373 	  for (section = ibfd->sections, prev = NULL;
   8374 	       section != NULL; section = section->next)
   8375 	    {
   8376 	      /* We mark the output section so that we know it comes
   8377 		 from the input BFD.  */
   8378 	      osec = section->output_section;
   8379 	      if (osec)
   8380 		osec->segment_mark = true;
   8381 
   8382 	      /* Check if this section is covered by the segment.  */
   8383 	      this_hdr = &(elf_section_data(section)->this_hdr);
   8384 	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
   8385 		{
   8386 		  /* FIXME: Check if its output section is changed or
   8387 		     removed.  What else do we need to check?  */
   8388 		  if (osec == NULL
   8389 		      || section->flags != osec->flags
   8390 		      || section->lma != osec->lma
   8391 		      || section->vma != osec->vma
   8392 		      || section->size != osec->size
   8393 		      || section->rawsize != osec->rawsize
   8394 		      || section->alignment_power != osec->alignment_power)
   8395 		    goto rewrite;
   8396 
   8397 		  /* PR 31450: If this is an allocated section then make sure
   8398 		     that this section's vma to lma relationship is the same
   8399 		     as previous (allocated) section's.  */
   8400 		  if (prev != NULL
   8401 		      && section->flags & SEC_ALLOC
   8402 		      && section->lma - section->vma != prev->lma - prev->vma)
   8403 		    goto rewrite;
   8404 
   8405 		  if (section->flags & SEC_ALLOC)
   8406 		    prev = section;
   8407 		}
   8408 	    }
   8409 	}
   8410 
   8411       /* Check to see if any output section do not come from the
   8412 	 input BFD.  */
   8413       for (section = obfd->sections; section != NULL;
   8414 	   section = section->next)
   8415 	{
   8416 	  if (!section->segment_mark)
   8417 	    goto rewrite;
   8418 	  else
   8419 	    section->segment_mark = false;
   8420 	}
   8421 
   8422       return copy_elf_program_header (ibfd, obfd);
   8423     }
   8424 
   8425  rewrite:
   8426   maxpagesize = 0;
   8427   if (ibfd->xvec == obfd->xvec)
   8428     {
   8429       /* When rewriting program header, set the output maxpagesize to
   8430 	 the maximum alignment of input PT_LOAD segments.  */
   8431       Elf_Internal_Phdr *segment;
   8432       unsigned int i;
   8433       unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
   8434 
   8435       for (i = 0, segment = elf_tdata (ibfd)->phdr;
   8436 	   i < num_segments;
   8437 	   i++, segment++)
   8438 	if (segment->p_type == PT_LOAD
   8439 	    && maxpagesize < segment->p_align)
   8440 	  {
   8441 	    /* PR 17512: file: f17299af.  */
   8442 	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
   8443 	      /* xgettext:c-format */
   8444 	      _bfd_error_handler (_("%pB: warning: segment alignment of %#"
   8445 				    PRIx64 " is too large"),
   8446 				  ibfd, (uint64_t) segment->p_align);
   8447 	    else
   8448 	      maxpagesize = segment->p_align;
   8449 	  }
   8450     }
   8451   if (maxpagesize == 0)
   8452     maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
   8453 
   8454   return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
   8455 }
   8456 
   8457 /* Copy private section information from input to output.  This function
   8458    is called both by objcopy where LINK_INFO is NULL, and ld where
   8459    LINK_INFO is non-NULL in the usual case but NULL for a special case
   8460    when dealing with LTO IR or cloning output sections.  */
   8461 
   8462 bool
   8463 _bfd_elf_copy_private_section_data (bfd *ibfd,
   8464 				    asection *isec,
   8465 				    bfd *obfd ATTRIBUTE_UNUSED,
   8466 				    asection *osec,
   8467 				    struct bfd_link_info *link_info)
   8468 {
   8469   if (ibfd->xvec->flavour != bfd_target_elf_flavour)
   8470     return true;
   8471 
   8472   Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
   8473   Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
   8474   if (link_info == NULL)
   8475     {
   8476       ohdr->sh_entsize = ihdr->sh_entsize;
   8477 
   8478       if (ihdr->sh_type == SHT_SYMTAB
   8479 	  || ihdr->sh_type == SHT_DYNSYM
   8480 	  || ihdr->sh_type == SHT_GNU_verneed
   8481 	  || ihdr->sh_type == SHT_GNU_verdef)
   8482 	ohdr->sh_info = ihdr->sh_info;
   8483     }
   8484 
   8485   /* If this is a known ABI section, ELF section type and flags may
   8486      have been set up when OSEC was created.  For normal sections we
   8487      allow the user to override the type and flags other than
   8488      SHF_MASKOS and SHF_MASKPROC.  */
   8489   if (elf_section_type (osec) == SHT_PROGBITS
   8490       || elf_section_type (osec) == SHT_NOTE
   8491       || elf_section_type (osec) == SHT_NOBITS)
   8492     elf_section_type (osec) = SHT_NULL;
   8493 
   8494   /* For objcopy and relocatable link, copy the ELF section type from
   8495      the input file if the BFD section flags are the same.  (If they
   8496      are different the user may be doing something like
   8497      "objcopy --set-section-flags .text=alloc,data".)  For a final
   8498      link allow some flags that the linker clears to differ.  */
   8499   bool final_link = (link_info != NULL
   8500 		     && !bfd_link_relocatable (link_info));
   8501   if (elf_section_type (osec) == SHT_NULL
   8502       && (osec->flags == isec->flags
   8503 	  || (final_link
   8504 	      && ((osec->flags ^ isec->flags)
   8505 		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
   8506     elf_section_type (osec) = elf_section_type (isec);
   8507 
   8508   elf_section_flags (osec) = elf_section_flags (isec);
   8509   /* Like for type, retain flags for objcopy (yet unlike for type, don't do so
   8510      for relocatable link).  Same heuristic as there: If the BFD section flags
   8511      are different, assume --set-section-flags is in use for the section.
   8512 
   8513      FIXME: Is this correct for all OS/PROC specific flags?  */
   8514   if (link_info != NULL || osec->flags != isec->flags)
   8515     elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
   8516   else
   8517     {
   8518       /* Clear only flags which are set below or elsewhere.  */
   8519       elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
   8520 				    | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
   8521 				    | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
   8522 				    | SHF_COMPRESSED);
   8523       if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
   8524 	_bfd_error_handler
   8525 	  (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
   8526 	   ibfd, isec,
   8527 	   (uint64_t) (elf_section_flags (osec)
   8528 		       & ~(SHF_MASKOS | SHF_MASKPROC)));
   8529     }
   8530 
   8531   /* Copy sh_info from input for mbind section.  */
   8532   if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
   8533       && elf_section_flags (isec) & SHF_GNU_MBIND)
   8534     elf_section_data (osec)->this_hdr.sh_info
   8535       = elf_section_data (isec)->this_hdr.sh_info;
   8536 
   8537   /* Set things up for objcopy and relocatable link.  The output
   8538      SHT_GROUP section will have its elf_next_in_group pointing back
   8539      to the input group members.  Ignore linker created group section.
   8540      See elfNN_ia64_object_p in elfxx-ia64.c.  */
   8541   if ((link_info == NULL
   8542        || !link_info->resolve_section_groups)
   8543       && (elf_sec_group (isec) == NULL
   8544 	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
   8545     {
   8546       if (elf_section_flags (isec) & SHF_GROUP)
   8547 	elf_section_flags (osec) |= SHF_GROUP;
   8548       elf_next_in_group (osec) = elf_next_in_group (isec);
   8549       elf_section_data (osec)->group = elf_section_data (isec)->group;
   8550     }
   8551 
   8552   /* If not decompress, preserve SHF_COMPRESSED.  */
   8553   if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
   8554     elf_section_flags (osec) |= (elf_section_flags (isec)
   8555 				 & SHF_COMPRESSED);
   8556 
   8557   /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
   8558      don't use the output section of the linked-to section since it
   8559      may be NULL at this point.  */
   8560   if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
   8561     {
   8562       ohdr->sh_flags |= SHF_LINK_ORDER;
   8563       elf_linked_to_section (osec) = elf_linked_to_section (isec);
   8564     }
   8565 
   8566   osec->use_rela_p = isec->use_rela_p;
   8567 
   8568   return true;
   8569 }
   8570 
   8571 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
   8572    necessary if we are removing either the SHT_GROUP section or any of
   8573    the group member sections.  DISCARDED is the value that a section's
   8574    output_section has if the section will be discarded, NULL when this
   8575    function is called from objcopy, bfd_abs_section_ptr when called
   8576    from the linker.  */
   8577 
   8578 bool
   8579 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
   8580 {
   8581   asection *isec;
   8582 
   8583   for (isec = ibfd->sections; isec != NULL; isec = isec->next)
   8584     if (elf_section_type (isec) == SHT_GROUP)
   8585       {
   8586 	asection *first = elf_next_in_group (isec);
   8587 	asection *s = first;
   8588 	bfd_size_type removed = 0;
   8589 
   8590 	while (s != NULL)
   8591 	  {
   8592 	    /* If this member section is being output but the
   8593 	       SHT_GROUP section is not, then clear the group info
   8594 	       set up by _bfd_elf_copy_private_section_data.  */
   8595 	    if (s->output_section != discarded
   8596 		&& isec->output_section == discarded)
   8597 	      {
   8598 		elf_section_flags (s->output_section) &= ~SHF_GROUP;
   8599 		elf_group_name (s->output_section) = NULL;
   8600 	      }
   8601 	    else
   8602 	      {
   8603 		struct bfd_elf_section_data *elf_sec = elf_section_data (s);
   8604 		if (s->output_section == discarded
   8605 		    && isec->output_section != discarded)
   8606 		  {
   8607 		    /* Conversely, if the member section is not being
   8608 		       output but the SHT_GROUP section is, then adjust
   8609 		       its size.  */
   8610 		    removed += 4;
   8611 		    if (elf_sec->rel.hdr != NULL
   8612 			&& (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
   8613 		      removed += 4;
   8614 		    if (elf_sec->rela.hdr != NULL
   8615 			&& (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
   8616 		      removed += 4;
   8617 		  }
   8618 		else
   8619 		  {
   8620 		    /* Also adjust for zero-sized relocation member
   8621 		       section.  */
   8622 		    if (elf_sec->rel.hdr != NULL
   8623 			&& elf_sec->rel.hdr->sh_size == 0)
   8624 		      removed += 4;
   8625 		    if (elf_sec->rela.hdr != NULL
   8626 			&& elf_sec->rela.hdr->sh_size == 0)
   8627 		      removed += 4;
   8628 		  }
   8629 	      }
   8630 	    s = elf_next_in_group (s);
   8631 	    if (s == first)
   8632 	      break;
   8633 	  }
   8634 	if (removed != 0)
   8635 	  {
   8636 	    if (discarded != NULL)
   8637 	      {
   8638 		/* If we've been called for ld -r, then we need to
   8639 		   adjust the input section size.  */
   8640 		if (isec->rawsize == 0)
   8641 		  isec->rawsize = isec->size;
   8642 		isec->size = isec->rawsize - removed;
   8643 		if (isec->size <= 4)
   8644 		  {
   8645 		    isec->size = 0;
   8646 		    isec->flags |= SEC_EXCLUDE;
   8647 		  }
   8648 	      }
   8649 	    else if (isec->output_section != NULL)
   8650 	      {
   8651 		/* Adjust the output section size when called from
   8652 		   objcopy. */
   8653 		isec->output_section->size -= removed;
   8654 		if (isec->output_section->size <= 4)
   8655 		  {
   8656 		    isec->output_section->size = 0;
   8657 		    isec->output_section->flags |= SEC_EXCLUDE;
   8658 		  }
   8659 	      }
   8660 	  }
   8661       }
   8662 
   8663   return true;
   8664 }
   8665 
   8666 /* Copy private header information.  */
   8667 
   8668 bool
   8669 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
   8670 {
   8671   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   8672     return true;
   8673 
   8674   /* Copy over private BFD data if it has not already been copied.
   8675      This must be done here, rather than in the copy_private_bfd_data
   8676      entry point, because the latter is called after the section
   8677      contents have been set, which means that the program headers have
   8678      already been worked out.  */
   8679   if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
   8680     {
   8681       if (! copy_private_bfd_data (ibfd, obfd))
   8682 	return false;
   8683     }
   8684 
   8685   return _bfd_elf_fixup_group_sections (ibfd, NULL);
   8686 }
   8687 
   8688 /* Copy private symbol information.  If this symbol is in a section
   8689    which we did not map into a BFD section, try to map the section
   8690    index correctly.  We use special macro definitions for the mapped
   8691    section indices; these definitions are interpreted by the
   8692    swap_out_syms function.  */
   8693 
   8694 #define MAP_ONESYMTAB (SHN_HIOS + 1)
   8695 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
   8696 #define MAP_STRTAB    (SHN_HIOS + 3)
   8697 #define MAP_SHSTRTAB  (SHN_HIOS + 4)
   8698 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
   8699 
   8700 bool
   8701 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
   8702 				   asymbol **isymarg,
   8703 				   bfd *obfd ATTRIBUTE_UNUSED,
   8704 				   asymbol **osymarg)
   8705 {
   8706   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   8707     return true;
   8708 
   8709   elf_symbol_type *isym = elf_symbol_from (*isymarg);
   8710   elf_symbol_type *osym = elf_symbol_from (*osymarg);
   8711   if (isym != NULL
   8712       && isym->internal_elf_sym.st_shndx != 0
   8713       && osym != NULL
   8714       && bfd_is_abs_section (isym->symbol.section))
   8715     {
   8716       unsigned int shndx;
   8717 
   8718       shndx = isym->internal_elf_sym.st_shndx;
   8719       if (shndx == elf_onesymtab (ibfd))
   8720 	shndx = MAP_ONESYMTAB;
   8721       else if (shndx == elf_dynsymtab (ibfd))
   8722 	shndx = MAP_DYNSYMTAB;
   8723       else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
   8724 	shndx = MAP_STRTAB;
   8725       else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
   8726 	shndx = MAP_SHSTRTAB;
   8727       else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
   8728 	shndx = MAP_SYM_SHNDX;
   8729       osym->internal_elf_sym.st_shndx = shndx;
   8730     }
   8731 
   8732   return true;
   8733 }
   8734 
   8735 /* Swap out the symbols.  */
   8736 
   8737 static bool
   8738 swap_out_syms (bfd *abfd,
   8739 	       struct elf_strtab_hash **sttp,
   8740 	       int relocatable_p,
   8741 	       struct bfd_link_info *info)
   8742 {
   8743   elf_backend_data *bed;
   8744   unsigned int symcount;
   8745   asymbol **syms;
   8746   struct elf_strtab_hash *stt;
   8747   Elf_Internal_Shdr *symtab_hdr;
   8748   Elf_Internal_Shdr *symtab_shndx_hdr;
   8749   Elf_Internal_Shdr *symstrtab_hdr;
   8750   struct elf_sym_strtab *symstrtab;
   8751   bfd_byte *outbound_syms;
   8752   bfd_byte *outbound_shndx;
   8753   unsigned long outbound_syms_index;
   8754   unsigned int idx;
   8755   unsigned int num_locals;
   8756   size_t amt;
   8757   bool name_local_sections;
   8758 
   8759   if (!elf_map_symbols (abfd, &num_locals))
   8760     return false;
   8761 
   8762   /* Dump out the symtabs.  */
   8763   stt = _bfd_elf_strtab_init ();
   8764   if (stt == NULL)
   8765     return false;
   8766 
   8767   bed = get_elf_backend_data (abfd);
   8768   symcount = bfd_get_symcount (abfd);
   8769   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8770   symtab_hdr->sh_type = SHT_SYMTAB;
   8771   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
   8772   symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
   8773   symtab_hdr->sh_info = num_locals + 1;
   8774   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
   8775 
   8776   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   8777   symstrtab_hdr->sh_type = SHT_STRTAB;
   8778 
   8779   /* Allocate buffer to swap out the .strtab section.  */
   8780   if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
   8781       || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
   8782     {
   8783       bfd_set_error (bfd_error_no_memory);
   8784       _bfd_elf_strtab_free (stt);
   8785       return false;
   8786     }
   8787 
   8788   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
   8789       || (outbound_syms = bfd_malloc (amt)) == NULL)
   8790     {
   8791     error_no_mem:
   8792       bfd_set_error (bfd_error_no_memory);
   8793     error_return:
   8794       free (symstrtab);
   8795       _bfd_elf_strtab_free (stt);
   8796       return false;
   8797     }
   8798   symtab_hdr->contents = outbound_syms;
   8799   outbound_syms_index = 0;
   8800 
   8801   outbound_shndx = NULL;
   8802 
   8803   if (elf_symtab_shndx_list (abfd))
   8804     {
   8805       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
   8806       if (symtab_shndx_hdr->sh_name != 0)
   8807 	{
   8808 	  if (_bfd_mul_overflow (symcount + 1,
   8809 				 sizeof (Elf_External_Sym_Shndx), &amt))
   8810 	    goto error_no_mem;
   8811 	  outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
   8812 	  if (outbound_shndx == NULL)
   8813 	    goto error_return;
   8814 
   8815 	  symtab_shndx_hdr->contents = outbound_shndx;
   8816 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
   8817 	  symtab_shndx_hdr->sh_size = amt;
   8818 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
   8819 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
   8820 	}
   8821       /* FIXME: What about any other headers in the list ?  */
   8822     }
   8823 
   8824   /* Now generate the data (for "contents").  */
   8825   {
   8826     /* Fill in zeroth symbol and swap it out.  */
   8827     Elf_Internal_Sym sym;
   8828     sym.st_name = 0;
   8829     sym.st_value = 0;
   8830     sym.st_size = 0;
   8831     sym.st_info = 0;
   8832     sym.st_other = 0;
   8833     sym.st_shndx = SHN_UNDEF;
   8834     sym.st_target_internal = 0;
   8835     symstrtab[outbound_syms_index].sym = sym;
   8836     symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
   8837     outbound_syms_index++;
   8838   }
   8839 
   8840   name_local_sections
   8841     = (bed->elf_backend_name_local_section_symbols
   8842        && bed->elf_backend_name_local_section_symbols (abfd));
   8843 
   8844   syms = bfd_get_outsymbols (abfd);
   8845   for (idx = 0; idx < symcount; idx++)
   8846     {
   8847       Elf_Internal_Sym sym;
   8848 
   8849       flagword flags = syms[idx]->flags;
   8850       if (!name_local_sections
   8851 	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
   8852 	{
   8853 	  /* Local section symbols have no name.  */
   8854 	  sym.st_name = 0;
   8855 	}
   8856       else
   8857 	{
   8858 	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
   8859 	     to get the final offset for st_name.  */
   8860 	  size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
   8861 	  if (stridx == (size_t) -1)
   8862 	    goto error_return;
   8863 	  sym.st_name = stridx;
   8864 	}
   8865 
   8866       bfd_vma value = syms[idx]->value;
   8867       elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
   8868       asection *sec = syms[idx]->section;
   8869 
   8870       if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
   8871 	{
   8872 	  /* ELF common symbols put the alignment into the `value' field,
   8873 	     and the size into the `size' field.  This is backwards from
   8874 	     how BFD handles it, so reverse it here.  */
   8875 	  sym.st_size = value;
   8876 	  if (type_ptr == NULL
   8877 	      || type_ptr->internal_elf_sym.st_value == 0)
   8878 	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
   8879 	  else
   8880 	    sym.st_value = type_ptr->internal_elf_sym.st_value;
   8881 	  sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8882 	}
   8883       else
   8884 	{
   8885 	  unsigned int shndx;
   8886 
   8887 	  if (sec->output_section)
   8888 	    {
   8889 	      value += sec->output_offset;
   8890 	      sec = sec->output_section;
   8891 	    }
   8892 
   8893 	  /* Don't add in the section vma for relocatable output.  */
   8894 	  if (! relocatable_p)
   8895 	    value += sec->vma;
   8896 	  sym.st_value = value;
   8897 	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
   8898 
   8899 	  if (bfd_is_abs_section (sec)
   8900 	      && type_ptr != NULL
   8901 	      && type_ptr->internal_elf_sym.st_shndx != 0)
   8902 	    {
   8903 	      /* This symbol is in a real ELF section which we did
   8904 		 not create as a BFD section.  Undo the mapping done
   8905 		 by copy_private_symbol_data.  */
   8906 	      shndx = type_ptr->internal_elf_sym.st_shndx;
   8907 	      switch (shndx)
   8908 		{
   8909 		case MAP_ONESYMTAB:
   8910 		  shndx = elf_onesymtab (abfd);
   8911 		  break;
   8912 		case MAP_DYNSYMTAB:
   8913 		  shndx = elf_dynsymtab (abfd);
   8914 		  break;
   8915 		case MAP_STRTAB:
   8916 		  shndx = elf_strtab_sec (abfd);
   8917 		  break;
   8918 		case MAP_SHSTRTAB:
   8919 		  shndx = elf_shstrtab_sec (abfd);
   8920 		  break;
   8921 		case MAP_SYM_SHNDX:
   8922 		  if (elf_symtab_shndx_list (abfd))
   8923 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
   8924 		  break;
   8925 		case SHN_COMMON:
   8926 		case SHN_ABS:
   8927 		  shndx = SHN_ABS;
   8928 		  break;
   8929 		default:
   8930 		  if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
   8931 		    {
   8932 		      if (bed->symbol_section_index)
   8933 			shndx = bed->symbol_section_index (abfd, type_ptr);
   8934 		      /* Otherwise just leave the index alone.  */
   8935 		    }
   8936 		  else
   8937 		    {
   8938 		      if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
   8939 			_bfd_error_handler (_("%pB: \
   8940 Unable to handle section index %x in ELF symbol.  Using ABS instead."),
   8941 					  abfd, shndx);
   8942 		      shndx = SHN_ABS;
   8943 		    }
   8944 		  break;
   8945 		}
   8946 	    }
   8947 	  else
   8948 	    {
   8949 	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8950 
   8951 	      if (shndx == SHN_BAD)
   8952 		{
   8953 		  asection *sec2;
   8954 
   8955 		  /* Writing this would be a hell of a lot easier if
   8956 		     we had some decent documentation on bfd, and
   8957 		     knew what to expect of the library, and what to
   8958 		     demand of applications.  For example, it
   8959 		     appears that `objcopy' might not set the
   8960 		     section of a symbol to be a section that is
   8961 		     actually in the output file.  */
   8962 		  sec2 = bfd_get_section_by_name (abfd, sec->name);
   8963 		  if (sec2 != NULL)
   8964 		    shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
   8965 		  if (shndx == SHN_BAD)
   8966 		    {
   8967 		      /* xgettext:c-format */
   8968 		      _bfd_error_handler
   8969 			(_("unable to find equivalent output section"
   8970 			   " for symbol '%s' from section '%s'"),
   8971 			 syms[idx]->name ? syms[idx]->name : "<Local sym>",
   8972 			 sec->name);
   8973 		      bfd_set_error (bfd_error_invalid_operation);
   8974 		      goto error_return;
   8975 		    }
   8976 		}
   8977 	    }
   8978 
   8979 	  sym.st_shndx = shndx;
   8980 	}
   8981 
   8982       int type;
   8983       if ((flags & BSF_THREAD_LOCAL) != 0)
   8984 	type = STT_TLS;
   8985       else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
   8986 	type = STT_GNU_IFUNC;
   8987       else if ((flags & BSF_FUNCTION) != 0)
   8988 	type = STT_FUNC;
   8989       else if ((flags & BSF_OBJECT) != 0)
   8990 	type = STT_OBJECT;
   8991       else if ((flags & BSF_RELC) != 0)
   8992 	type = STT_RELC;
   8993       else if ((flags & BSF_SRELC) != 0)
   8994 	type = STT_SRELC;
   8995       else
   8996 	type = STT_NOTYPE;
   8997 
   8998       if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
   8999 	type = STT_TLS;
   9000 
   9001       /* Processor-specific types.  */
   9002       if (type_ptr != NULL
   9003 	  && bed->elf_backend_get_symbol_type)
   9004 	type = ((*bed->elf_backend_get_symbol_type)
   9005 		(&type_ptr->internal_elf_sym, type));
   9006 
   9007       if (flags & BSF_SECTION_SYM)
   9008 	{
   9009 	  if (flags & BSF_GLOBAL)
   9010 	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
   9011 	  else
   9012 	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
   9013 	}
   9014       else if (bfd_is_com_section (syms[idx]->section))
   9015 	{
   9016 	  if (type != STT_TLS)
   9017 	    {
   9018 	      if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
   9019 		type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
   9020 			? STT_COMMON : STT_OBJECT);
   9021 	      else
   9022 		type = ((flags & BSF_ELF_COMMON) != 0
   9023 			? STT_COMMON : STT_OBJECT);
   9024 	    }
   9025 	  sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
   9026 	}
   9027       else if (bfd_is_und_section (syms[idx]->section))
   9028 	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
   9029 				    ? STB_WEAK
   9030 				    : STB_GLOBAL),
   9031 				   type);
   9032       else if (flags & BSF_FILE)
   9033 	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
   9034       else
   9035 	{
   9036 	  int bind = STB_LOCAL;
   9037 
   9038 	  if (flags & BSF_LOCAL)
   9039 	    bind = STB_LOCAL;
   9040 	  else if (flags & BSF_GNU_UNIQUE)
   9041 	    bind = STB_GNU_UNIQUE;
   9042 	  else if (flags & BSF_WEAK)
   9043 	    bind = STB_WEAK;
   9044 	  else if (flags & BSF_GLOBAL)
   9045 	    bind = STB_GLOBAL;
   9046 
   9047 	  sym.st_info = ELF_ST_INFO (bind, type);
   9048 	}
   9049 
   9050       if (type_ptr != NULL)
   9051 	{
   9052 	  sym.st_other = type_ptr->internal_elf_sym.st_other;
   9053 	  sym.st_target_internal
   9054 	    = type_ptr->internal_elf_sym.st_target_internal;
   9055 	}
   9056       else
   9057 	{
   9058 	  sym.st_other = 0;
   9059 	  sym.st_target_internal = 0;
   9060 	}
   9061 
   9062       symstrtab[outbound_syms_index].sym = sym;
   9063       symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
   9064       outbound_syms_index++;
   9065     }
   9066 
   9067   /* Finalize the .strtab section.  */
   9068   _bfd_elf_strtab_finalize (stt);
   9069 
   9070   /* Swap out the .strtab section.  */
   9071   for (idx = 0; idx < outbound_syms_index; idx++)
   9072     {
   9073       struct elf_sym_strtab *elfsym = &symstrtab[idx];
   9074       if (elfsym->sym.st_name != 0)
   9075 	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
   9076 						      elfsym->sym.st_name);
   9077       if (info && info->callbacks->ctf_new_symbol)
   9078 	info->callbacks->ctf_new_symbol (elfsym->dest_index,
   9079 					 &elfsym->sym);
   9080 
   9081       /* Inform the linker of the addition of this symbol.  */
   9082 
   9083       bed->s->swap_symbol_out (abfd, &elfsym->sym,
   9084 			       (outbound_syms
   9085 				+ (elfsym->dest_index
   9086 				   * bed->s->sizeof_sym)),
   9087 			       NPTR_ADD (outbound_shndx,
   9088 					 (elfsym->dest_index
   9089 					  * sizeof (Elf_External_Sym_Shndx))));
   9090     }
   9091   free (symstrtab);
   9092 
   9093   *sttp = stt;
   9094   symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
   9095   symstrtab_hdr->sh_type = SHT_STRTAB;
   9096   symstrtab_hdr->sh_addralign = 1;
   9097 
   9098   return true;
   9099 }
   9100 
   9101 /* Return the number of bytes required to hold the symtab vector.
   9102 
   9103    Note that we base it on the count plus 1, since we will null terminate
   9104    the vector allocated based on this size.  However, the ELF symbol table
   9105    always has a dummy entry as symbol #0, so it ends up even.  */
   9106 
   9107 long
   9108 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
   9109 {
   9110   bfd_size_type symcount;
   9111   long symtab_size;
   9112   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
   9113 
   9114   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9115   if (symcount > LONG_MAX / sizeof (asymbol *))
   9116     {
   9117       bfd_set_error (bfd_error_file_too_big);
   9118       return -1;
   9119     }
   9120   symtab_size = symcount * (sizeof (asymbol *));
   9121   if (symcount == 0)
   9122     symtab_size = sizeof (asymbol *);
   9123   else if (!bfd_write_p (abfd))
   9124     {
   9125       ufile_ptr filesize = bfd_get_file_size (abfd);
   9126 
   9127       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9128 	{
   9129 	  bfd_set_error (bfd_error_file_truncated);
   9130 	  return -1;
   9131 	}
   9132     }
   9133 
   9134   return symtab_size;
   9135 }
   9136 
   9137 long
   9138 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
   9139 {
   9140   bfd_size_type symcount;
   9141   long symtab_size;
   9142   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   9143 
   9144   if (elf_dynsymtab (abfd) == 0)
   9145     {
   9146       /* Check if there is dynamic symbol table.  */
   9147       symcount = elf_tdata (abfd)->dt_symtab_count;
   9148       if (symcount)
   9149 	goto compute_symtab_size;
   9150 
   9151       bfd_set_error (bfd_error_invalid_operation);
   9152       return -1;
   9153     }
   9154 
   9155   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
   9156   if (symcount > LONG_MAX / sizeof (asymbol *))
   9157     {
   9158       bfd_set_error (bfd_error_file_too_big);
   9159       return -1;
   9160     }
   9161 
   9162  compute_symtab_size:
   9163   symtab_size = symcount * (sizeof (asymbol *));
   9164   if (symcount == 0)
   9165     symtab_size = sizeof (asymbol *);
   9166   else if (!bfd_write_p (abfd))
   9167     {
   9168       ufile_ptr filesize = bfd_get_file_size (abfd);
   9169 
   9170       if (filesize != 0 && (unsigned long) symtab_size > filesize)
   9171 	{
   9172 	  bfd_set_error (bfd_error_file_truncated);
   9173 	  return -1;
   9174 	}
   9175     }
   9176 
   9177   return symtab_size;
   9178 }
   9179 
   9180 long
   9181 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
   9182 {
   9183   if (asect->reloc_count != 0 && !bfd_write_p (abfd))
   9184     {
   9185       /* Sanity check reloc section size.  */
   9186       ufile_ptr filesize = bfd_get_file_size (abfd);
   9187 
   9188       if (filesize != 0)
   9189 	{
   9190 	  struct bfd_elf_section_data *d = elf_section_data (asect);
   9191 	  bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
   9192 	  bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
   9193 
   9194 	  if (rel_size + rela_size > filesize
   9195 	      || rel_size + rela_size < rel_size)
   9196 	    {
   9197 	      bfd_set_error (bfd_error_file_truncated);
   9198 	      return -1;
   9199 	    }
   9200 	}
   9201     }
   9202 
   9203 #if SIZEOF_LONG == SIZEOF_INT
   9204   if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
   9205     {
   9206       bfd_set_error (bfd_error_file_too_big);
   9207       return -1;
   9208     }
   9209 #endif
   9210   return (asect->reloc_count + 1L) * sizeof (arelent *);
   9211 }
   9212 
   9213 /* Canonicalize the relocs.  */
   9214 
   9215 long
   9216 _bfd_elf_canonicalize_reloc (bfd *abfd,
   9217 			     sec_ptr section,
   9218 			     arelent **relptr,
   9219 			     asymbol **symbols)
   9220 {
   9221   arelent *tblptr;
   9222   unsigned int i;
   9223   elf_backend_data *bed = get_elf_backend_data (abfd);
   9224 
   9225   if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
   9226     return -1;
   9227 
   9228   tblptr = section->relocation;
   9229   for (i = 0; i < section->reloc_count; i++)
   9230     *relptr++ = tblptr++;
   9231 
   9232   *relptr = NULL;
   9233 
   9234   return section->reloc_count;
   9235 }
   9236 
   9237 long
   9238 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
   9239 {
   9240   elf_backend_data *bed = get_elf_backend_data (abfd);
   9241   long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
   9242 
   9243   if (symcount >= 0)
   9244     abfd->symcount = symcount;
   9245   return symcount;
   9246 }
   9247 
   9248 long
   9249 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   9250 				      asymbol **allocation)
   9251 {
   9252   elf_backend_data *bed = get_elf_backend_data (abfd);
   9253   long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
   9254 
   9255   if (symcount >= 0)
   9256     abfd->dynsymcount = symcount;
   9257   return symcount;
   9258 }
   9259 
   9260 /* Return the size required for the dynamic reloc entries.  Any loadable
   9261    section that was actually installed in the BFD, and has type SHT_REL
   9262    or SHT_RELA, and uses the dynamic symbol table, is considered to be a
   9263    dynamic reloc section.  */
   9264 
   9265 long
   9266 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   9267 {
   9268   bfd_size_type count, ext_rel_size;
   9269   asection *s;
   9270 
   9271   if (elf_dynsymtab (abfd) == 0)
   9272     {
   9273       bfd_set_error (bfd_error_invalid_operation);
   9274       return -1;
   9275     }
   9276 
   9277   count = 1;
   9278   ext_rel_size = 0;
   9279   for (s = abfd->sections; s != NULL; s = s->next)
   9280     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9281 	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9282 	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9283 	&& (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9284       {
   9285 	ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
   9286 	if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
   9287 	  {
   9288 	    bfd_set_error (bfd_error_file_truncated);
   9289 	    return -1;
   9290 	  }
   9291 	count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9292 	if (count > LONG_MAX / sizeof (arelent *))
   9293 	  {
   9294 	    bfd_set_error (bfd_error_file_too_big);
   9295 	    return -1;
   9296 	  }
   9297       }
   9298   if (count > 1 && !bfd_write_p (abfd))
   9299     {
   9300       /* Sanity check reloc section sizes.  */
   9301       ufile_ptr filesize = bfd_get_file_size (abfd);
   9302       if (filesize != 0 && ext_rel_size > filesize)
   9303 	{
   9304 	  bfd_set_error (bfd_error_file_truncated);
   9305 	  return -1;
   9306 	}
   9307     }
   9308   return count * sizeof (arelent *);
   9309 }
   9310 
   9311 /* Canonicalize the dynamic relocation entries.  Note that we return the
   9312    dynamic relocations as a single block, although they are actually
   9313    associated with particular sections; the interface, which was
   9314    designed for SunOS style shared libraries, expects that there is only
   9315    one set of dynamic relocs.  Any loadable section that was actually
   9316    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
   9317    dynamic symbol table, is considered to be a dynamic reloc section.  */
   9318 
   9319 long
   9320 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   9321 				     arelent **storage,
   9322 				     asymbol **syms)
   9323 {
   9324   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   9325   asection *s;
   9326   long ret;
   9327 
   9328   if (elf_dynsymtab (abfd) == 0)
   9329     {
   9330       bfd_set_error (bfd_error_invalid_operation);
   9331       return -1;
   9332     }
   9333 
   9334   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   9335   ret = 0;
   9336   for (s = abfd->sections; s != NULL; s = s->next)
   9337     {
   9338       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
   9339 	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
   9340 	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
   9341 	  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
   9342 	{
   9343 	  arelent *p;
   9344 	  long count, i;
   9345 
   9346 	  if (! (*slurp_relocs) (abfd, s, syms, true))
   9347 	    return -1;
   9348 	  count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
   9349 	  p = s->relocation;
   9350 	  for (i = 0; i < count; i++)
   9351 	    *storage++ = p++;
   9352 	  ret += count;
   9353 	}
   9354     }
   9355 
   9356   *storage = NULL;
   9357 
   9358   return ret;
   9359 }
   9360 
   9361 /* Read in the version information.  */
   9363 
   9364 bool
   9365 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
   9366 {
   9367   bfd_byte *contents = NULL;
   9368   unsigned int freeidx = 0;
   9369   size_t amt;
   9370   void *contents_addr = NULL;
   9371   size_t contents_size = 0;
   9372 
   9373   if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
   9374     {
   9375       Elf_Internal_Shdr *hdr;
   9376       Elf_External_Verneed *everneed;
   9377       Elf_Internal_Verneed *iverneed;
   9378       unsigned int i;
   9379       bfd_byte *contents_end;
   9380       size_t verneed_count;
   9381       size_t verneed_size;
   9382 
   9383       if (elf_tdata (abfd)->dt_verneed != NULL)
   9384 	{
   9385 	  hdr = NULL;
   9386 	  contents = elf_tdata (abfd)->dt_verneed;
   9387 	  verneed_count = elf_tdata (abfd)->dt_verneed_count;
   9388 	  verneed_size = verneed_count * sizeof (Elf_External_Verneed);
   9389 	}
   9390       else
   9391 	{
   9392 	  hdr = &elf_tdata (abfd)->dynverref_hdr;
   9393 
   9394 	  if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
   9395 	    {
   9396 	    error_return_bad_verref:
   9397 	      _bfd_error_handler
   9398 		(_("%pB: .gnu.version_r invalid entry"), abfd);
   9399 	      bfd_set_error (bfd_error_bad_value);
   9400 	    error_return_verref:
   9401 	      elf_tdata (abfd)->verref = NULL;
   9402 	      elf_tdata (abfd)->cverrefs = 0;
   9403 	      goto error_return;
   9404 	    }
   9405 
   9406 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9407 	    goto error_return_verref;
   9408 	  contents_size = hdr->sh_size;
   9409 	  contents = _bfd_mmap_temporary (abfd, contents_size,
   9410 					  &contents_addr, &contents_size);
   9411 	  if (contents == NULL)
   9412 	    goto error_return_verref;
   9413 
   9414 	  verneed_size = hdr->sh_size;
   9415 	  verneed_count = hdr->sh_info;
   9416 	}
   9417 
   9418       if (_bfd_mul_overflow (verneed_count,
   9419 			     sizeof (Elf_Internal_Verneed), &amt))
   9420 	{
   9421 	  bfd_set_error (bfd_error_file_too_big);
   9422 	  goto error_return_verref;
   9423 	}
   9424       if (amt == 0)
   9425 	goto error_return_verref;
   9426       elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
   9427       if (elf_tdata (abfd)->verref == NULL)
   9428 	goto error_return_verref;
   9429 
   9430       BFD_ASSERT (sizeof (Elf_External_Verneed)
   9431 		  == sizeof (Elf_External_Vernaux));
   9432       contents_end = (contents + verneed_size
   9433 		      - sizeof (Elf_External_Verneed));
   9434       everneed = (Elf_External_Verneed *) contents;
   9435       iverneed = elf_tdata (abfd)->verref;
   9436       for (i = 0; i < verneed_count; i++, iverneed++)
   9437 	{
   9438 	  Elf_External_Vernaux *evernaux;
   9439 	  Elf_Internal_Vernaux *ivernaux;
   9440 	  unsigned int j;
   9441 
   9442 	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
   9443 
   9444 	  iverneed->vn_bfd = abfd;
   9445 
   9446 	  if (elf_use_dt_symtab_p (abfd))
   9447 	    {
   9448 	      if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
   9449 		iverneed->vn_filename
   9450 		  = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
   9451 	      else
   9452 		iverneed->vn_filename = NULL;
   9453 	    }
   9454 	  else if (hdr == NULL)
   9455 	    goto error_return_bad_verref;
   9456 	  else
   9457 	    iverneed->vn_filename
   9458 	      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9459 						 iverneed->vn_file);
   9460 	  if (iverneed->vn_filename == NULL)
   9461 	    goto error_return_bad_verref;
   9462 
   9463 	  if (iverneed->vn_cnt == 0)
   9464 	    iverneed->vn_auxptr = NULL;
   9465 	  else
   9466 	    {
   9467 	      if (_bfd_mul_overflow (iverneed->vn_cnt,
   9468 				     sizeof (Elf_Internal_Vernaux), &amt))
   9469 		{
   9470 		  bfd_set_error (bfd_error_file_too_big);
   9471 		  goto error_return_verref;
   9472 		}
   9473 	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
   9474 		bfd_alloc (abfd, amt);
   9475 	      if (iverneed->vn_auxptr == NULL)
   9476 		goto error_return_verref;
   9477 	    }
   9478 
   9479 	  if (iverneed->vn_aux
   9480 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9481 	    goto error_return_bad_verref;
   9482 
   9483 	  evernaux = ((Elf_External_Vernaux *)
   9484 		      ((bfd_byte *) everneed + iverneed->vn_aux));
   9485 	  ivernaux = iverneed->vn_auxptr;
   9486 	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
   9487 	    {
   9488 	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
   9489 
   9490 	      if (elf_use_dt_symtab_p (abfd))
   9491 		{
   9492 		  if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
   9493 		    ivernaux->vna_nodename
   9494 		      = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
   9495 		  else
   9496 		    ivernaux->vna_nodename = NULL;
   9497 		}
   9498 	      else if (hdr == NULL)
   9499 		goto error_return_bad_verref;
   9500 	      else
   9501 		ivernaux->vna_nodename
   9502 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9503 						     ivernaux->vna_name);
   9504 	      if (ivernaux->vna_nodename == NULL)
   9505 		goto error_return_bad_verref;
   9506 
   9507 	      if (ivernaux->vna_other > freeidx)
   9508 		freeidx = ivernaux->vna_other;
   9509 
   9510 	      ivernaux->vna_nextptr = NULL;
   9511 	      if (ivernaux->vna_next == 0)
   9512 		{
   9513 		  iverneed->vn_cnt = j + 1;
   9514 		  break;
   9515 		}
   9516 	      if (j + 1 < iverneed->vn_cnt)
   9517 		ivernaux->vna_nextptr = ivernaux + 1;
   9518 
   9519 	      if (ivernaux->vna_next
   9520 		  > (size_t) (contents_end - (bfd_byte *) evernaux))
   9521 		goto error_return_bad_verref;
   9522 
   9523 	      evernaux = ((Elf_External_Vernaux *)
   9524 			  ((bfd_byte *) evernaux + ivernaux->vna_next));
   9525 	    }
   9526 
   9527 	  iverneed->vn_nextref = NULL;
   9528 	  if (iverneed->vn_next == 0)
   9529 	    break;
   9530 	  if (hdr != NULL && (i + 1 < hdr->sh_info))
   9531 	    iverneed->vn_nextref = iverneed + 1;
   9532 
   9533 	  if (iverneed->vn_next
   9534 	      > (size_t) (contents_end - (bfd_byte *) everneed))
   9535 	    goto error_return_bad_verref;
   9536 
   9537 	  everneed = ((Elf_External_Verneed *)
   9538 		      ((bfd_byte *) everneed + iverneed->vn_next));
   9539 	}
   9540       elf_tdata (abfd)->cverrefs = i;
   9541 
   9542       if (contents != elf_tdata (abfd)->dt_verneed)
   9543 	_bfd_munmap_temporary (contents_addr, contents_size);
   9544       contents = NULL;
   9545       contents_addr = NULL;
   9546     }
   9547 
   9548   if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
   9549     {
   9550       Elf_Internal_Shdr *hdr;
   9551       Elf_External_Verdef *everdef;
   9552       Elf_Internal_Verdef *iverdef;
   9553       Elf_Internal_Verdef *iverdefarr;
   9554       Elf_Internal_Verdef iverdefmem;
   9555       unsigned int i;
   9556       unsigned int maxidx;
   9557       bfd_byte *contents_end_def, *contents_end_aux;
   9558       size_t verdef_count;
   9559       size_t verdef_size;
   9560 
   9561       if (elf_tdata (abfd)->dt_verdef != NULL)
   9562 	{
   9563 	  hdr = NULL;
   9564 	  contents = elf_tdata (abfd)->dt_verdef;
   9565 	  verdef_count = elf_tdata (abfd)->dt_verdef_count;
   9566 	  verdef_size = verdef_count * sizeof (Elf_External_Verdef);
   9567 	}
   9568       else
   9569 	{
   9570 	  hdr = &elf_tdata (abfd)->dynverdef_hdr;
   9571 
   9572 	  if (hdr->sh_size < sizeof (Elf_External_Verdef))
   9573 	    {
   9574 	    error_return_bad_verdef:
   9575 	      _bfd_error_handler
   9576 		(_("%pB: .gnu.version_d invalid entry"), abfd);
   9577 	      bfd_set_error (bfd_error_bad_value);
   9578 	    error_return_verdef:
   9579 	      elf_tdata (abfd)->verdef = NULL;
   9580 	      elf_tdata (abfd)->cverdefs = 0;
   9581 	      goto error_return;
   9582 	    }
   9583 
   9584 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
   9585 	    goto error_return_verdef;
   9586 	  contents_size = hdr->sh_size;
   9587 	  contents = _bfd_mmap_temporary (abfd, contents_size,
   9588 					  &contents_addr, &contents_size);
   9589 	  if (contents == NULL)
   9590 	    goto error_return_verdef;
   9591 
   9592 	  BFD_ASSERT (sizeof (Elf_External_Verdef)
   9593 		      >= sizeof (Elf_External_Verdaux));
   9594 
   9595 	  verdef_count = hdr->sh_info;
   9596 	  verdef_size = hdr->sh_size;
   9597 	}
   9598 
   9599       contents_end_def = (contents + verdef_size
   9600 			  - sizeof (Elf_External_Verdef));
   9601       contents_end_aux = (contents + verdef_size
   9602 			  - sizeof (Elf_External_Verdaux));
   9603 
   9604       /* We know the number of entries in the section but not the maximum
   9605 	 index.  Therefore we have to run through all entries and find
   9606 	 the maximum.  */
   9607       everdef = (Elf_External_Verdef *) contents;
   9608       maxidx = 0;
   9609       for (i = 0; i < verdef_count; ++i)
   9610 	{
   9611 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9612 
   9613 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
   9614 	    goto error_return_bad_verdef;
   9615 	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
   9616 	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
   9617 
   9618 	  if (iverdefmem.vd_next == 0)
   9619 	    break;
   9620 
   9621 	  if (iverdefmem.vd_next
   9622 	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
   9623 	    goto error_return_bad_verdef;
   9624 
   9625 	  everdef = ((Elf_External_Verdef *)
   9626 		     ((bfd_byte *) everdef + iverdefmem.vd_next));
   9627 	}
   9628 
   9629       if (default_imported_symver)
   9630 	{
   9631 	  if (freeidx > maxidx)
   9632 	    maxidx = ++freeidx;
   9633 	  else
   9634 	    freeidx = ++maxidx;
   9635 	}
   9636       if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
   9637 	{
   9638 	  bfd_set_error (bfd_error_file_too_big);
   9639 	  goto error_return_verdef;
   9640 	}
   9641 
   9642       if (amt == 0)
   9643 	goto error_return_verdef;
   9644       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9645       if (elf_tdata (abfd)->verdef == NULL)
   9646 	goto error_return_verdef;
   9647 
   9648       elf_tdata (abfd)->cverdefs = maxidx;
   9649 
   9650       everdef = (Elf_External_Verdef *) contents;
   9651       iverdefarr = elf_tdata (abfd)->verdef;
   9652       for (i = 0; i < verdef_count; ++i)
   9653 	{
   9654 	  Elf_External_Verdaux *everdaux;
   9655 	  Elf_Internal_Verdaux *iverdaux;
   9656 	  unsigned int j;
   9657 
   9658 	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
   9659 
   9660 	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
   9661 	    goto error_return_bad_verdef;
   9662 
   9663 	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
   9664 	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
   9665 
   9666 	  iverdef->vd_bfd = abfd;
   9667 
   9668 	  if (iverdef->vd_cnt == 0)
   9669 	    iverdef->vd_auxptr = NULL;
   9670 	  else
   9671 	    {
   9672 	      if (_bfd_mul_overflow (iverdef->vd_cnt,
   9673 				     sizeof (Elf_Internal_Verdaux), &amt))
   9674 		{
   9675 		  bfd_set_error (bfd_error_file_too_big);
   9676 		  goto error_return_verdef;
   9677 		}
   9678 	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
   9679 		bfd_alloc (abfd, amt);
   9680 	      if (iverdef->vd_auxptr == NULL)
   9681 		goto error_return_verdef;
   9682 	    }
   9683 
   9684 	  if (iverdef->vd_aux
   9685 	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
   9686 	    goto error_return_bad_verdef;
   9687 
   9688 	  everdaux = ((Elf_External_Verdaux *)
   9689 		      ((bfd_byte *) everdef + iverdef->vd_aux));
   9690 	  iverdaux = iverdef->vd_auxptr;
   9691 	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
   9692 	    {
   9693 	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
   9694 
   9695 	      if (elf_use_dt_symtab_p (abfd))
   9696 		{
   9697 		  if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
   9698 		    iverdaux->vda_nodename
   9699 		      = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
   9700 		  else
   9701 		    iverdaux->vda_nodename = NULL;
   9702 		}
   9703 	      else
   9704 		iverdaux->vda_nodename
   9705 		  = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
   9706 						     iverdaux->vda_name);
   9707 	      if (iverdaux->vda_nodename == NULL)
   9708 		goto error_return_bad_verdef;
   9709 
   9710 	      iverdaux->vda_nextptr = NULL;
   9711 	      if (iverdaux->vda_next == 0)
   9712 		{
   9713 		  iverdef->vd_cnt = j + 1;
   9714 		  break;
   9715 		}
   9716 	      if (j + 1 < iverdef->vd_cnt)
   9717 		iverdaux->vda_nextptr = iverdaux + 1;
   9718 
   9719 	      if (iverdaux->vda_next
   9720 		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
   9721 		goto error_return_bad_verdef;
   9722 
   9723 	      everdaux = ((Elf_External_Verdaux *)
   9724 			  ((bfd_byte *) everdaux + iverdaux->vda_next));
   9725 	    }
   9726 
   9727 	  iverdef->vd_nodename = NULL;
   9728 	  if (iverdef->vd_cnt)
   9729 	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
   9730 
   9731 	  iverdef->vd_nextdef = NULL;
   9732 	  if (iverdef->vd_next == 0)
   9733 	    break;
   9734 	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
   9735 	    iverdef->vd_nextdef = iverdef + 1;
   9736 
   9737 	  everdef = ((Elf_External_Verdef *)
   9738 		     ((bfd_byte *) everdef + iverdef->vd_next));
   9739 	}
   9740 
   9741       if (contents != elf_tdata (abfd)->dt_verdef)
   9742 	_bfd_munmap_temporary (contents_addr, contents_size);
   9743       contents = NULL;
   9744       contents_addr = NULL;
   9745     }
   9746   else if (default_imported_symver)
   9747     {
   9748       if (freeidx < 3)
   9749 	freeidx = 3;
   9750       else
   9751 	freeidx++;
   9752 
   9753       if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
   9754 	{
   9755 	  bfd_set_error (bfd_error_file_too_big);
   9756 	  goto error_return;
   9757 	}
   9758       if (amt == 0)
   9759 	goto error_return;
   9760       elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
   9761       if (elf_tdata (abfd)->verdef == NULL)
   9762 	goto error_return;
   9763 
   9764       elf_tdata (abfd)->cverdefs = freeidx;
   9765     }
   9766 
   9767   /* Create a default version based on the soname.  */
   9768   if (default_imported_symver)
   9769     {
   9770       Elf_Internal_Verdef *iverdef;
   9771       Elf_Internal_Verdaux *iverdaux;
   9772 
   9773       iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
   9774 
   9775       iverdef->vd_version = VER_DEF_CURRENT;
   9776       iverdef->vd_flags = 0;
   9777       iverdef->vd_ndx = freeidx;
   9778       iverdef->vd_cnt = 1;
   9779 
   9780       iverdef->vd_bfd = abfd;
   9781 
   9782       iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
   9783       if (iverdef->vd_nodename == NULL)
   9784 	goto error_return_verdef;
   9785       iverdef->vd_nextdef = NULL;
   9786       iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
   9787 			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
   9788       if (iverdef->vd_auxptr == NULL)
   9789 	goto error_return_verdef;
   9790 
   9791       iverdaux = iverdef->vd_auxptr;
   9792       iverdaux->vda_nodename = iverdef->vd_nodename;
   9793     }
   9794 
   9795   return true;
   9796 
   9797  error_return:
   9798   if (contents != elf_tdata (abfd)->dt_verneed
   9799       && contents != elf_tdata (abfd)->dt_verdef)
   9800     _bfd_munmap_temporary (contents_addr, contents_size);
   9801   return false;
   9802 }
   9803 
   9804 asymbol *
   9806 _bfd_elf_make_empty_symbol (bfd *abfd)
   9807 {
   9808   elf_symbol_type *newsym;
   9809 
   9810   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
   9811   if (!newsym)
   9812     return NULL;
   9813   newsym->symbol.the_bfd = abfd;
   9814   return &newsym->symbol;
   9815 }
   9816 
   9817 void
   9818 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   9819 			  asymbol *symbol,
   9820 			  symbol_info *ret)
   9821 {
   9822   bfd_symbol_info (symbol, ret);
   9823 }
   9824 
   9825 /* Return whether a symbol name implies a local symbol.  Most targets
   9826    use this function for the is_local_label_name entry point, but some
   9827    override it.  */
   9828 
   9829 bool
   9830 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   9831 			      const char *name)
   9832 {
   9833   /* Normal local symbols start with ``.L''.  */
   9834   if (name[0] == '.' && name[1] == 'L')
   9835     return true;
   9836 
   9837   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
   9838      DWARF debugging symbols starting with ``..''.  */
   9839   if (name[0] == '.' && name[1] == '.')
   9840     return true;
   9841 
   9842   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
   9843      emitting DWARF debugging output.  I suspect this is actually a
   9844      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
   9845      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
   9846      underscore to be emitted on some ELF targets).  For ease of use,
   9847      we treat such symbols as local.  */
   9848   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   9849     return true;
   9850 
   9851   /* Treat assembler generated fake symbols, dollar local labels and
   9852      forward-backward labels (aka local labels) as locals.
   9853      These labels have the form:
   9854 
   9855        L0^A.*				       (fake symbols)
   9856 
   9857        [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
   9858 
   9859      Versions which start with .L will have already been matched above,
   9860      so we only need to match the rest.  */
   9861   if (name[0] == 'L' && ISDIGIT (name[1]))
   9862     {
   9863       bool ret = false;
   9864       const char * p;
   9865       char c;
   9866 
   9867       for (p = name + 2; (c = *p); p++)
   9868 	{
   9869 	  if (c == 1 || c == 2)
   9870 	    {
   9871 	      if (c == 1 && p == name + 2)
   9872 		/* A fake symbol.  */
   9873 		return true;
   9874 
   9875 	      /* FIXME: We are being paranoid here and treating symbols like
   9876 		 L0^Bfoo as if there were non-local, on the grounds that the
   9877 		 assembler will never generate them.  But can any symbol
   9878 		 containing an ASCII value in the range 1-31 ever be anything
   9879 		 other than some kind of local ?  */
   9880 	      ret = true;
   9881 	    }
   9882 
   9883 	  if (! ISDIGIT (c))
   9884 	    {
   9885 	      ret = false;
   9886 	      break;
   9887 	    }
   9888 	}
   9889       return ret;
   9890     }
   9891 
   9892   return false;
   9893 }
   9894 
   9895 alent *
   9896 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
   9897 		     asymbol *symbol ATTRIBUTE_UNUSED)
   9898 {
   9899   abort ();
   9900   return NULL;
   9901 }
   9902 
   9903 bool
   9904 _bfd_elf_set_arch_mach (bfd *abfd,
   9905 			enum bfd_architecture arch,
   9906 			unsigned long machine)
   9907 {
   9908   /* If this isn't the right architecture for this backend, and this
   9909      isn't the generic backend, fail.  */
   9910   if (arch != get_elf_backend_data (abfd)->arch
   9911       && arch != bfd_arch_unknown
   9912       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
   9913     return false;
   9914 
   9915   return bfd_default_set_arch_mach (abfd, arch, machine);
   9916 }
   9917 
   9918 /* Find the nearest line to a particular section and offset,
   9919    for error reporting.  */
   9920 
   9921 bool
   9922 _bfd_elf_find_nearest_line (bfd *abfd,
   9923 			    asymbol **symbols,
   9924 			    asection *section,
   9925 			    bfd_vma offset,
   9926 			    const char **filename_ptr,
   9927 			    const char **functionname_ptr,
   9928 			    unsigned int *line_ptr,
   9929 			    unsigned int *discriminator_ptr)
   9930 {
   9931   return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
   9932 					      offset, filename_ptr,
   9933 					      functionname_ptr, line_ptr,
   9934 					      discriminator_ptr);
   9935 }
   9936 
   9937 /* Find the nearest line to a particular section and offset,
   9938    for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
   9939    can be optionally specified.  */
   9940 
   9941 bool
   9942 _bfd_elf_find_nearest_line_with_alt (bfd *abfd,
   9943 				     const char *alt_filename,
   9944 				     asymbol **symbols,
   9945 				     asection *section,
   9946 				     bfd_vma offset,
   9947 				     const char **filename_ptr,
   9948 				     const char **functionname_ptr,
   9949 				     unsigned int *line_ptr,
   9950 				     unsigned int *discriminator_ptr)
   9951 {
   9952   bool found;
   9953 
   9954   if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
   9955 					      section, offset, filename_ptr,
   9956 					      functionname_ptr, line_ptr,
   9957 					      discriminator_ptr,
   9958 					      dwarf_debug_sections,
   9959 					      &elf_tdata (abfd)->dwarf2_find_line_info))
   9960     return true;
   9961 
   9962   if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
   9963 				     filename_ptr, functionname_ptr, line_ptr))
   9964     {
   9965       if (!*functionname_ptr)
   9966 	_bfd_elf_find_function (abfd, symbols, section, offset,
   9967 				*filename_ptr ? NULL : filename_ptr,
   9968 				functionname_ptr);
   9969       return true;
   9970     }
   9971 
   9972   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
   9973 					     &found, filename_ptr,
   9974 					     functionname_ptr, line_ptr,
   9975 					     &elf_tdata (abfd)->line_info))
   9976     return false;
   9977   if (found && (*functionname_ptr || *line_ptr))
   9978     return true;
   9979 
   9980   if (symbols == NULL)
   9981     return false;
   9982 
   9983   if (! _bfd_elf_find_function (abfd, symbols, section, offset,
   9984 				filename_ptr, functionname_ptr))
   9985     return false;
   9986 
   9987   *line_ptr = 0;
   9988   return true;
   9989 }
   9990 
   9991 /* Find the line for a symbol.  */
   9992 
   9993 bool
   9994 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
   9995 		    const char **filename_ptr, unsigned int *line_ptr)
   9996 {
   9997   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   9998   return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
   9999 					filename_ptr, NULL, line_ptr, NULL,
   10000 					dwarf_debug_sections,
   10001 					&tdata->dwarf2_find_line_info);
   10002 }
   10003 
   10004 /* After a call to bfd_find_nearest_line, successive calls to
   10005    bfd_find_inliner_info can be used to get source information about
   10006    each level of function inlining that terminated at the address
   10007    passed to bfd_find_nearest_line.  Currently this is only supported
   10008    for DWARF2 with appropriate DWARF3 extensions. */
   10009 
   10010 bool
   10011 _bfd_elf_find_inliner_info (bfd *abfd,
   10012 			    const char **filename_ptr,
   10013 			    const char **functionname_ptr,
   10014 			    unsigned int *line_ptr)
   10015 {
   10016   struct elf_obj_tdata *tdata = elf_tdata (abfd);
   10017   return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
   10018 					functionname_ptr, line_ptr,
   10019 					&tdata->dwarf2_find_line_info);
   10020 }
   10021 
   10022 int
   10023 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
   10024 {
   10025   elf_backend_data *bed = get_elf_backend_data (abfd);
   10026   int ret = bed->s->sizeof_ehdr;
   10027 
   10028   if (!bfd_link_relocatable (info))
   10029     {
   10030       bfd_size_type phdr_size = elf_program_header_size (abfd);
   10031 
   10032       if (phdr_size == (bfd_size_type) -1)
   10033 	{
   10034 	  struct elf_segment_map *m;
   10035 
   10036 	  phdr_size = 0;
   10037 	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   10038 	    phdr_size += bed->s->sizeof_phdr;
   10039 
   10040 	  if (phdr_size == 0)
   10041 	    phdr_size = get_program_header_size (abfd, info);
   10042 	}
   10043 
   10044       elf_program_header_size (abfd) = phdr_size;
   10045       ret += phdr_size;
   10046     }
   10047 
   10048   return ret;
   10049 }
   10050 
   10051 bool
   10052 _bfd_elf_set_section_contents (bfd *abfd,
   10053 			       sec_ptr section,
   10054 			       const void *location,
   10055 			       file_ptr offset,
   10056 			       bfd_size_type count)
   10057 {
   10058   Elf_Internal_Shdr *hdr;
   10059 
   10060   if (! abfd->output_has_begun
   10061       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
   10062     return false;
   10063 
   10064   if (!count)
   10065     return true;
   10066 
   10067   hdr = &elf_section_data (section)->this_hdr;
   10068   if (hdr->sh_offset == (file_ptr) -1)
   10069     {
   10070       unsigned char *contents;
   10071 
   10072       if (bfd_section_is_ctf (section))
   10073 	/* Nothing to do with this section: the contents are generated
   10074 	   later.  */
   10075 	return true;
   10076 
   10077       if ((offset + count) > hdr->sh_size)
   10078 	{
   10079 	  _bfd_error_handler
   10080 	    (_("%pB:%pA: error: attempting to write"
   10081 	       " over the end of the section"),
   10082 	     abfd, section);
   10083 
   10084 	  bfd_set_error (bfd_error_invalid_operation);
   10085 	  return false;
   10086 	}
   10087 
   10088       contents = hdr->contents;
   10089       if (contents == NULL)
   10090 	{
   10091 	  _bfd_error_handler
   10092 	    (_("%pB:%pA: error: attempting to write"
   10093 	       " section into an empty buffer"),
   10094 	     abfd, section);
   10095 
   10096 	  bfd_set_error (bfd_error_invalid_operation);
   10097 	  return false;
   10098 	}
   10099 
   10100       memcpy (contents + offset, location, count);
   10101       return true;
   10102     }
   10103 
   10104   return _bfd_generic_set_section_contents (abfd, section,
   10105 					    location, offset, count);
   10106 }
   10107 
   10108 bool
   10109 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   10110 			   arelent *cache_ptr ATTRIBUTE_UNUSED,
   10111 			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
   10112 {
   10113   abort ();
   10114   return false;
   10115 }
   10116 
   10117 /* Try to convert a non-ELF reloc into an ELF one.  */
   10118 
   10119 bool
   10120 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
   10121 {
   10122   /* Check whether we really have an ELF howto.  */
   10123 
   10124   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
   10125     {
   10126       bfd_reloc_code_real_type code;
   10127       reloc_howto_type *howto;
   10128 
   10129       /* Alien reloc: Try to determine its type to replace it with an
   10130 	 equivalent ELF reloc.  */
   10131 
   10132       if (areloc->howto->pc_relative)
   10133 	{
   10134 	  switch (areloc->howto->bitsize)
   10135 	    {
   10136 	    case 8:
   10137 	      code = BFD_RELOC_8_PCREL;
   10138 	      break;
   10139 	    case 12:
   10140 	      code = BFD_RELOC_12_PCREL;
   10141 	      break;
   10142 	    case 16:
   10143 	      code = BFD_RELOC_16_PCREL;
   10144 	      break;
   10145 	    case 24:
   10146 	      code = BFD_RELOC_24_PCREL;
   10147 	      break;
   10148 	    case 32:
   10149 	      code = BFD_RELOC_32_PCREL;
   10150 	      break;
   10151 	    case 64:
   10152 	      code = BFD_RELOC_64_PCREL;
   10153 	      break;
   10154 	    default:
   10155 	      goto fail;
   10156 	    }
   10157 
   10158 	  howto = bfd_reloc_type_lookup (abfd, code);
   10159 
   10160 	  if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
   10161 	    {
   10162 	      if (howto->pcrel_offset)
   10163 		areloc->addend += areloc->address;
   10164 	      else
   10165 		areloc->addend -= areloc->address; /* addend is unsigned!! */
   10166 	    }
   10167 	}
   10168       else
   10169 	{
   10170 	  switch (areloc->howto->bitsize)
   10171 	    {
   10172 	    case 8:
   10173 	      code = BFD_RELOC_8;
   10174 	      break;
   10175 	    case 14:
   10176 	      code = BFD_RELOC_14;
   10177 	      break;
   10178 	    case 16:
   10179 	      code = BFD_RELOC_16;
   10180 	      break;
   10181 	    case 26:
   10182 	      code = BFD_RELOC_26;
   10183 	      break;
   10184 	    case 32:
   10185 	      code = BFD_RELOC_32;
   10186 	      break;
   10187 	    case 64:
   10188 	      code = BFD_RELOC_64;
   10189 	      break;
   10190 	    default:
   10191 	      goto fail;
   10192 	    }
   10193 
   10194 	  howto = bfd_reloc_type_lookup (abfd, code);
   10195 	}
   10196 
   10197       if (howto)
   10198 	areloc->howto = howto;
   10199       else
   10200 	goto fail;
   10201     }
   10202 
   10203   return true;
   10204 
   10205  fail:
   10206   /* xgettext:c-format */
   10207   _bfd_error_handler (_("%pB: %s unsupported"),
   10208 		      abfd, areloc->howto->name);
   10209   bfd_set_error (bfd_error_sorry);
   10210   return false;
   10211 }
   10212 
   10213 bool
   10214 _bfd_elf_free_cached_info (bfd *abfd)
   10215 {
   10216   struct elf_obj_tdata *tdata;
   10217 
   10218   if ((bfd_get_format (abfd) == bfd_object
   10219        || bfd_get_format (abfd) == bfd_core)
   10220       && (tdata = elf_tdata (abfd)) != NULL)
   10221     {
   10222       if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
   10223 	_bfd_elf_strtab_free (elf_shstrtab (abfd));
   10224       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
   10225       _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
   10226       _bfd_stab_cleanup (abfd, &tdata->line_info);
   10227       for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
   10228 	{
   10229 	  _bfd_elf_munmap_section_contents (sec, sec->contents);
   10230 	  if (!sec->alloced)
   10231 	    {
   10232 	      free (elf_section_data (sec)->this_hdr.contents);
   10233 	      elf_section_data (sec)->this_hdr.contents = NULL;
   10234 	    }
   10235 	  free (elf_section_data (sec)->relocs);
   10236 	  elf_section_data (sec)->relocs = NULL;
   10237 	  if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
   10238 	    {
   10239 	      struct eh_frame_sec_info *sec_info = sec->sec_info;
   10240 	      free (sec_info->cies);
   10241 	    }
   10242 	}
   10243       free (tdata->symtab_hdr.contents);
   10244       tdata->symtab_hdr.contents = NULL;
   10245     }
   10246 
   10247   return _bfd_generic_bfd_free_cached_info (abfd);
   10248 }
   10249 
   10250 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
   10251    in the relocation's offset.  Thus we cannot allow any sort of sanity
   10252    range-checking to interfere.  There is nothing else to do in processing
   10253    this reloc.  */
   10254 
   10255 bfd_reloc_status_type
   10256 _bfd_elf_rel_vtable_reloc_fn
   10257   (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
   10258    struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
   10259    void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
   10260    bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
   10261 {
   10262   return bfd_reloc_ok;
   10263 }
   10264 
   10265 /* Elf core file support.  Much of this only works on native
   10267    toolchains, since we rely on knowing the
   10268    machine-dependent procfs structure in order to pick
   10269    out details about the corefile.  */
   10270 
   10271 #ifdef HAVE_SYS_PROCFS_H
   10272 # include <sys/procfs.h>
   10273 #endif
   10274 
   10275 /* Return a PID that identifies a "thread" for threaded cores, or the
   10276    PID of the main process for non-threaded cores.  */
   10277 
   10278 static int
   10279 elfcore_make_pid (bfd *abfd)
   10280 {
   10281   int pid;
   10282 
   10283   pid = elf_tdata (abfd)->core->lwpid;
   10284   if (pid == 0)
   10285     pid = elf_tdata (abfd)->core->pid;
   10286 
   10287   return pid;
   10288 }
   10289 
   10290 /* If there isn't a section called NAME, make one, using data from
   10291    SECT.  Note, this function will generate a reference to NAME, so
   10292    you shouldn't deallocate or overwrite it.  */
   10293 
   10294 static bool
   10295 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
   10296 {
   10297   asection *sect2;
   10298 
   10299   if (bfd_get_section_by_name (abfd, name) != NULL)
   10300     return true;
   10301 
   10302   sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
   10303   if (sect2 == NULL)
   10304     return false;
   10305 
   10306   sect2->size = sect->size;
   10307   sect2->filepos = sect->filepos;
   10308   sect2->alignment_power = sect->alignment_power;
   10309   return true;
   10310 }
   10311 
   10312 /* Create a pseudosection containing SIZE bytes at FILEPOS.  This
   10313    actually creates up to two pseudosections:
   10314    - For the single-threaded case, a section named NAME, unless
   10315      such a section already exists.
   10316    - For the multi-threaded case, a section named "NAME/PID", where
   10317      PID is elfcore_make_pid (abfd).
   10318    Both pseudosections have identical contents.  */
   10319 bool
   10320 _bfd_elfcore_make_pseudosection (bfd *abfd,
   10321 				 char *name,
   10322 				 size_t size,
   10323 				 ufile_ptr filepos)
   10324 {
   10325   char buf[100];
   10326   char *threaded_name;
   10327   size_t len;
   10328   asection *sect;
   10329 
   10330   /* Build the section name.  */
   10331 
   10332   sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
   10333   len = strlen (buf) + 1;
   10334   threaded_name = (char *) bfd_alloc (abfd, len);
   10335   if (threaded_name == NULL)
   10336     return false;
   10337   memcpy (threaded_name, buf, len);
   10338 
   10339   sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
   10340 					     SEC_HAS_CONTENTS);
   10341   if (sect == NULL)
   10342     return false;
   10343   sect->size = size;
   10344   sect->filepos = filepos;
   10345   sect->alignment_power = 2;
   10346 
   10347   return elfcore_maybe_make_sect (abfd, name, sect);
   10348 }
   10349 
   10350 static bool
   10351 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
   10352 				size_t offs)
   10353 {
   10354   asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
   10355 						       SEC_HAS_CONTENTS);
   10356 
   10357   if (sect == NULL)
   10358     return false;
   10359 
   10360   sect->size = note->descsz - offs;
   10361   sect->filepos = note->descpos + offs;
   10362   sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   10363 
   10364   return true;
   10365 }
   10366 
   10367 /* prstatus_t exists on:
   10368      solaris 2.5+
   10369      linux 2.[01] + glibc
   10370      unixware 4.2
   10371 */
   10372 
   10373 #if defined (HAVE_PRSTATUS_T)
   10374 
   10375 static bool
   10376 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   10377 {
   10378   size_t size;
   10379   int offset;
   10380 
   10381   if (note->descsz == sizeof (prstatus_t))
   10382     {
   10383       prstatus_t prstat;
   10384 
   10385       size = sizeof (prstat.pr_reg);
   10386       offset   = offsetof (prstatus_t, pr_reg);
   10387       memcpy (&prstat, note->descdata, sizeof (prstat));
   10388 
   10389       /* Do not overwrite the core signal if it
   10390 	 has already been set by another thread.  */
   10391       if (elf_tdata (abfd)->core->signal == 0)
   10392 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10393       if (elf_tdata (abfd)->core->pid == 0)
   10394 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10395 
   10396       /* pr_who exists on:
   10397 	 solaris 2.5+
   10398 	 unixware 4.2
   10399 	 pr_who doesn't exist on:
   10400 	 linux 2.[01]
   10401 	 */
   10402 #if defined (HAVE_PRSTATUS_T_PR_WHO)
   10403       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10404 #else
   10405       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10406 #endif
   10407     }
   10408 #if defined (HAVE_PRSTATUS32_T)
   10409   else if (note->descsz == sizeof (prstatus32_t))
   10410     {
   10411       /* 64-bit host, 32-bit corefile */
   10412       prstatus32_t prstat;
   10413 
   10414       size = sizeof (prstat.pr_reg);
   10415       offset   = offsetof (prstatus32_t, pr_reg);
   10416       memcpy (&prstat, note->descdata, sizeof (prstat));
   10417 
   10418       /* Do not overwrite the core signal if it
   10419 	 has already been set by another thread.  */
   10420       if (elf_tdata (abfd)->core->signal == 0)
   10421 	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
   10422       if (elf_tdata (abfd)->core->pid == 0)
   10423 	elf_tdata (abfd)->core->pid = prstat.pr_pid;
   10424 
   10425       /* pr_who exists on:
   10426 	 solaris 2.5+
   10427 	 unixware 4.2
   10428 	 pr_who doesn't exist on:
   10429 	 linux 2.[01]
   10430 	 */
   10431 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
   10432       elf_tdata (abfd)->core->lwpid = prstat.pr_who;
   10433 #else
   10434       elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
   10435 #endif
   10436     }
   10437 #endif /* HAVE_PRSTATUS32_T */
   10438   else
   10439     {
   10440       /* Fail - we don't know how to handle any other
   10441 	 note size (ie. data object type).  */
   10442       return true;
   10443     }
   10444 
   10445   /* Make a ".reg/999" section and a ".reg" section.  */
   10446   return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
   10447 					  size, note->descpos + offset);
   10448 }
   10449 #endif /* defined (HAVE_PRSTATUS_T) */
   10450 
   10451 /* Create a pseudosection containing the exact contents of NOTE.  */
   10452 static bool
   10453 elfcore_make_note_pseudosection (bfd *abfd,
   10454 				 char *name,
   10455 				 Elf_Internal_Note *note)
   10456 {
   10457   return _bfd_elfcore_make_pseudosection (abfd, name,
   10458 					  note->descsz, note->descpos);
   10459 }
   10460 
   10461 /* There isn't a consistent prfpregset_t across platforms,
   10462    but it doesn't matter, because we don't have to pick this
   10463    data structure apart.  */
   10464 
   10465 static bool
   10466 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
   10467 {
   10468   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   10469 }
   10470 
   10471 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
   10472    type of NT_PRXFPREG.  Just include the whole note's contents
   10473    literally.  */
   10474 
   10475 static bool
   10476 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
   10477 {
   10478   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
   10479 }
   10480 
   10481 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
   10482    with a note type of NT_X86_XSTATE.  Just include the whole note's
   10483    contents literally.  */
   10484 
   10485 static bool
   10486 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
   10487 {
   10488   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XSTATE, note);
   10489 }
   10490 
   10491 static bool
   10492 elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
   10493 {
   10494   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_SSP, note);
   10495 }
   10496 
   10497 /* Linux dumps the XSAVE Layout description in a note named "LINUX"
   10498    with a note type of NT_X86_XSAVE_LAYOUT. */
   10499 static bool
   10500 elfcore_grok_xsave_layout_desc (bfd *abfd, Elf_Internal_Note *note)
   10501 {
   10502   return elfcore_make_note_pseudosection (abfd,
   10503 					  NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,
   10504 					  note);
   10505 }
   10506 
   10507 static bool
   10508 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
   10509 {
   10510   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VMX, note);
   10511 }
   10512 
   10513 static bool
   10514 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
   10515 {
   10516   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VSX, note);
   10517 }
   10518 
   10519 static bool
   10520 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
   10521 {
   10522   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TAR, note);
   10523 }
   10524 
   10525 static bool
   10526 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
   10527 {
   10528   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PPR, note);
   10529 }
   10530 
   10531 static bool
   10532 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
   10533 {
   10534   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DSCR, note);
   10535 }
   10536 
   10537 static bool
   10538 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
   10539 {
   10540   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_EBB, note);
   10541 }
   10542 
   10543 static bool
   10544 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
   10545 {
   10546   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PMU, note);
   10547 }
   10548 
   10549 static bool
   10550 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
   10551 {
   10552   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CGPR, note);
   10553 }
   10554 
   10555 static bool
   10556 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
   10557 {
   10558   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CFPR, note);
   10559 }
   10560 
   10561 static bool
   10562 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
   10563 {
   10564   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVMX, note);
   10565 }
   10566 
   10567 static bool
   10568 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
   10569 {
   10570   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVSX, note);
   10571 }
   10572 
   10573 static bool
   10574 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
   10575 {
   10576   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_SPR, note);
   10577 }
   10578 
   10579 static bool
   10580 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
   10581 {
   10582   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CTAR, note);
   10583 }
   10584 
   10585 static bool
   10586 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
   10587 {
   10588   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CPPR, note);
   10589 }
   10590 
   10591 static bool
   10592 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
   10593 {
   10594   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, note);
   10595 }
   10596 
   10597 static bool
   10598 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
   10599 {
   10600   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, note);
   10601 }
   10602 
   10603 static bool
   10604 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
   10605 {
   10606   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TIMER, note);
   10607 }
   10608 
   10609 static bool
   10610 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
   10611 {
   10612   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODCMP, note);
   10613 }
   10614 
   10615 static bool
   10616 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
   10617 {
   10618   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODPREG, note);
   10619 }
   10620 
   10621 static bool
   10622 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
   10623 {
   10624   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_CTRS, note);
   10625 }
   10626 
   10627 static bool
   10628 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
   10629 {
   10630   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_PREFIX, note);
   10631 }
   10632 
   10633 static bool
   10634 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
   10635 {
   10636   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_LAST_BREAK, note);
   10637 }
   10638 
   10639 static bool
   10640 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
   10641 {
   10642   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, note);
   10643 }
   10644 
   10645 static bool
   10646 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
   10647 {
   10648   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TDB, note);
   10649 }
   10650 
   10651 static bool
   10652 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
   10653 {
   10654   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_LOW, note);
   10655 }
   10656 
   10657 static bool
   10658 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
   10659 {
   10660   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, note);
   10661 }
   10662 
   10663 static bool
   10664 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
   10665 {
   10666   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_CB, note);
   10667 }
   10668 
   10669 static bool
   10670 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
   10671 {
   10672   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_BC, note);
   10673 }
   10674 
   10675 static bool
   10676 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
   10677 {
   10678   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARM_VFP, note);
   10679 }
   10680 
   10681 static bool
   10682 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
   10683 {
   10684   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_TLS, note);
   10685 }
   10686 
   10687 static bool
   10688 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
   10689 {
   10690   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, note);
   10691 }
   10692 
   10693 static bool
   10694 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
   10695 {
   10696   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, note);
   10697 }
   10698 
   10699 static bool
   10700 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
   10701 {
   10702   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SVE, note);
   10703 }
   10704 
   10705 static bool
   10706 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
   10707 {
   10708   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_PAUTH, note);
   10709 }
   10710 
   10711 static bool
   10712 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
   10713 {
   10714   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_MTE, note);
   10715 }
   10716 
   10717 static bool
   10718 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
   10719 {
   10720   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SSVE, note);
   10721 }
   10722 
   10723 static bool
   10724 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
   10725 {
   10726   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZA, note);
   10727 }
   10728 
   10729 /* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
   10730    successful, otherwise return FALSE.  */
   10731 
   10732 static bool
   10733 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
   10734 {
   10735   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZT, note);
   10736 }
   10737 
   10738 /* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
   10739    successful, otherwise return FALSE.  */
   10740 
   10741 static bool
   10742 elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
   10743 {
   10744   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_GCS, note);
   10745 }
   10746 
   10747 /* Convert NOTE into the appropriate note pseudo-section for the AArch64 FPMR.
   10748  * Return TRUE if successful, otherwise return FALSE.  */
   10749 
   10750 static bool
   10751 elfcore_grok_aarch_fpmr (bfd *abfd, Elf_Internal_Note *note)
   10752 {
   10753   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_FPMR, note);
   10754 }
   10755 
   10756 static bool
   10757 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   10758 {
   10759   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARC_V2, note);
   10760 }
   10761 
   10762 /* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
   10763    successful otherwise, return FALSE.  */
   10764 
   10765 static bool
   10766 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
   10767 {
   10768   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_RISCV_CSR, note);
   10769 }
   10770 
   10771 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
   10772    successful otherwise, return FALSE.  */
   10773 
   10774 static bool
   10775 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   10776 {
   10777   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
   10778 }
   10779 
   10780 static bool
   10781 elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
   10782 {
   10783   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_I386_TLS,
   10784 					  note);
   10785 }
   10786 
   10787 static bool
   10788 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
   10789 {
   10790   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, note);
   10791 }
   10792 
   10793 static bool
   10794 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
   10795 {
   10796   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LBT, note);
   10797 }
   10798 
   10799 static bool
   10800 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
   10801 {
   10802   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LSX, note);
   10803 }
   10804 
   10805 static bool
   10806 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
   10807 {
   10808   return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LASX, note);
   10809 }
   10810 
   10811 #if defined (HAVE_PRPSINFO_T)
   10812 typedef prpsinfo_t   elfcore_psinfo_t;
   10813 #if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
   10814 typedef prpsinfo32_t elfcore_psinfo32_t;
   10815 #endif
   10816 #endif
   10817 
   10818 #if defined (HAVE_PSINFO_T)
   10819 typedef psinfo_t   elfcore_psinfo_t;
   10820 #if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
   10821 typedef psinfo32_t elfcore_psinfo32_t;
   10822 #endif
   10823 #endif
   10824 
   10825 /* return a malloc'ed copy of a string at START which is at
   10826    most MAX bytes long, possibly without a terminating '\0'.
   10827    the copy will always have a terminating '\0'.  */
   10828 
   10829 char *
   10830 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
   10831 {
   10832   char *dups;
   10833   char *end = (char *) memchr (start, '\0', max);
   10834   size_t len;
   10835 
   10836   if (end == NULL)
   10837     len = max;
   10838   else
   10839     len = end - start;
   10840 
   10841   dups = (char *) bfd_alloc (abfd, len + 1);
   10842   if (dups == NULL)
   10843     return NULL;
   10844 
   10845   memcpy (dups, start, len);
   10846   dups[len] = '\0';
   10847 
   10848   return dups;
   10849 }
   10850 
   10851 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   10852 static bool
   10853 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   10854 {
   10855   if (note->descsz == sizeof (elfcore_psinfo_t))
   10856     {
   10857       elfcore_psinfo_t psinfo;
   10858 
   10859       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10860 
   10861 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
   10862       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10863 #endif
   10864       elf_tdata (abfd)->core->program
   10865 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10866 				sizeof (psinfo.pr_fname));
   10867 
   10868       elf_tdata (abfd)->core->command
   10869 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10870 				sizeof (psinfo.pr_psargs));
   10871     }
   10872 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   10873   else if (note->descsz == sizeof (elfcore_psinfo32_t))
   10874     {
   10875       /* 64-bit host, 32-bit corefile */
   10876       elfcore_psinfo32_t psinfo;
   10877 
   10878       memcpy (&psinfo, note->descdata, sizeof (psinfo));
   10879 
   10880 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
   10881       elf_tdata (abfd)->core->pid = psinfo.pr_pid;
   10882 #endif
   10883       elf_tdata (abfd)->core->program
   10884 	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
   10885 				sizeof (psinfo.pr_fname));
   10886 
   10887       elf_tdata (abfd)->core->command
   10888 	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
   10889 				sizeof (psinfo.pr_psargs));
   10890     }
   10891 #endif
   10892 
   10893   else
   10894     {
   10895       /* Fail - we don't know how to handle any other
   10896 	 note size (ie. data object type).  */
   10897       return true;
   10898     }
   10899 
   10900   /* Note that for some reason, a spurious space is tacked
   10901      onto the end of the args in some (at least one anyway)
   10902      implementations, so strip it off if it exists.  */
   10903 
   10904   {
   10905     char *command = elf_tdata (abfd)->core->command;
   10906     int n = strlen (command);
   10907 
   10908     if (0 < n && command[n - 1] == ' ')
   10909       command[n - 1] = '\0';
   10910   }
   10911 
   10912   return true;
   10913 }
   10914 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
   10915 
   10916 #if defined (HAVE_PSTATUS_T)
   10917 static bool
   10918 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
   10919 {
   10920   if (note->descsz == sizeof (pstatus_t)
   10921 #if defined (HAVE_PXSTATUS_T)
   10922       || note->descsz == sizeof (pxstatus_t)
   10923 #endif
   10924       )
   10925     {
   10926       pstatus_t pstat;
   10927 
   10928       memcpy (&pstat, note->descdata, sizeof (pstat));
   10929 
   10930       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10931     }
   10932 #if defined (HAVE_PSTATUS32_T)
   10933   else if (note->descsz == sizeof (pstatus32_t))
   10934     {
   10935       /* 64-bit host, 32-bit corefile */
   10936       pstatus32_t pstat;
   10937 
   10938       memcpy (&pstat, note->descdata, sizeof (pstat));
   10939 
   10940       elf_tdata (abfd)->core->pid = pstat.pr_pid;
   10941     }
   10942 #endif
   10943   /* Could grab some more details from the "representative"
   10944      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
   10945      NT_LWPSTATUS note, presumably.  */
   10946 
   10947   return true;
   10948 }
   10949 #endif /* defined (HAVE_PSTATUS_T) */
   10950 
   10951 #if defined (HAVE_LWPSTATUS_T)
   10952 static bool
   10953 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
   10954 {
   10955   lwpstatus_t lwpstat;
   10956   char buf[100];
   10957   char *name;
   10958   size_t len;
   10959   asection *sect;
   10960 
   10961   if (note->descsz != sizeof (lwpstat)
   10962 #if defined (HAVE_LWPXSTATUS_T)
   10963       && note->descsz != sizeof (lwpxstatus_t)
   10964 #endif
   10965       )
   10966     return true;
   10967 
   10968   memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
   10969 
   10970   elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
   10971   /* Do not overwrite the core signal if it has already been set by
   10972      another thread.  */
   10973   if (elf_tdata (abfd)->core->signal == 0)
   10974     elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
   10975 
   10976   /* Make a ".reg/999" section.  */
   10977 
   10978   sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%d", elfcore_make_pid (abfd));
   10979   len = strlen (buf) + 1;
   10980   name = bfd_alloc (abfd, len);
   10981   if (name == NULL)
   10982     return false;
   10983   memcpy (name, buf, len);
   10984 
   10985   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   10986   if (sect == NULL)
   10987     return false;
   10988 
   10989 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   10990   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
   10991   sect->filepos = note->descpos
   10992     + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
   10993 #endif
   10994 
   10995 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   10996   sect->size = sizeof (lwpstat.pr_reg);
   10997   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
   10998 #endif
   10999 
   11000   sect->alignment_power = 2;
   11001 
   11002   if (!elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
   11003     return false;
   11004 
   11005   /* Make a ".reg2/999" section */
   11006 
   11007   sprintf (buf, NOTE_PSEUDO_SECTION_REG2 "/%d", elfcore_make_pid (abfd));
   11008   len = strlen (buf) + 1;
   11009   name = bfd_alloc (abfd, len);
   11010   if (name == NULL)
   11011     return false;
   11012   memcpy (name, buf, len);
   11013 
   11014   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11015   if (sect == NULL)
   11016     return false;
   11017 
   11018 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   11019   sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
   11020   sect->filepos = note->descpos
   11021     + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
   11022 #endif
   11023 
   11024 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
   11025   sect->size = sizeof (lwpstat.pr_fpreg);
   11026   sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
   11027 #endif
   11028 
   11029   sect->alignment_power = 2;
   11030 
   11031   return elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG2, sect);
   11032 }
   11033 #endif /* defined (HAVE_LWPSTATUS_T) */
   11034 
   11035 /* These constants, and the structure offsets used below, are defined by
   11036    Cygwin's core_dump.h */
   11037 #define NOTE_INFO_PROCESS  1
   11038 #define NOTE_INFO_THREAD   2
   11039 #define NOTE_INFO_MODULE   3
   11040 #define NOTE_INFO_MODULE64 4
   11041 
   11042 static bool
   11043 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
   11044 {
   11045   char buf[30];
   11046   char *name;
   11047   size_t len;
   11048   unsigned int name_size;
   11049   asection *sect;
   11050   unsigned int type;
   11051   int is_active_thread;
   11052   bfd_vma base_addr;
   11053 
   11054   if (note->descsz < 4)
   11055     return true;
   11056 
   11057   if (! startswith (note->namedata, "win32"))
   11058     return true;
   11059 
   11060   type = bfd_get_32 (abfd, note->descdata);
   11061 
   11062   static const struct
   11063   {
   11064     const char *type_name;
   11065     unsigned long min_size;
   11066   } size_check[] =
   11067       {
   11068        { "NOTE_INFO_PROCESS", 12 },
   11069        { "NOTE_INFO_THREAD", 12 },
   11070        { "NOTE_INFO_MODULE", 12 },
   11071        { "NOTE_INFO_MODULE64", 16 },
   11072       };
   11073 
   11074   if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
   11075       return true;
   11076 
   11077   if (note->descsz < size_check[type - 1].min_size)
   11078     {
   11079       _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
   11080 			    " is too small"),
   11081 			  abfd, size_check[type - 1].type_name, note->descsz);
   11082       return true;
   11083     }
   11084 
   11085   switch (type)
   11086     {
   11087     case NOTE_INFO_PROCESS:
   11088       /* FIXME: need to add ->core->command.  */
   11089       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
   11090       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
   11091       break;
   11092 
   11093     case NOTE_INFO_THREAD:
   11094       /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
   11095 	 structure. */
   11096       /* thread_info.tid */
   11097       sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
   11098 
   11099       len = strlen (buf) + 1;
   11100       name = (char *) bfd_alloc (abfd, len);
   11101       if (name == NULL)
   11102 	return false;
   11103 
   11104       memcpy (name, buf, len);
   11105 
   11106       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11107       if (sect == NULL)
   11108 	return false;
   11109 
   11110       /* sizeof (thread_info.thread_context) */
   11111       sect->size = note->descsz - 12;
   11112       /* offsetof (thread_info.thread_context) */
   11113       sect->filepos = note->descpos + 12;
   11114       sect->alignment_power = 2;
   11115 
   11116       /* thread_info.is_active_thread */
   11117       is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
   11118 
   11119       if (is_active_thread)
   11120 	if (! elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
   11121 	  return false;
   11122       break;
   11123 
   11124     case NOTE_INFO_MODULE:
   11125     case NOTE_INFO_MODULE64:
   11126       /* Make a ".module/xxxxxxxx" section.  */
   11127       if (type == NOTE_INFO_MODULE)
   11128 	{
   11129 	  /* module_info.base_address */
   11130 	  base_addr = bfd_get_32 (abfd, note->descdata + 4);
   11131 	  sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
   11132 	  /* module_info.module_name_size */
   11133 	  name_size = bfd_get_32 (abfd, note->descdata + 8);
   11134 	}
   11135       else /* NOTE_INFO_MODULE64 */
   11136 	{
   11137 	  /* module_info.base_address */
   11138 	  base_addr = bfd_get_64 (abfd, note->descdata + 4);
   11139 	  sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
   11140 	  /* module_info.module_name_size */
   11141 	  name_size = bfd_get_32 (abfd, note->descdata + 12);
   11142 	}
   11143 
   11144       len = strlen (buf) + 1;
   11145       name = (char *) bfd_alloc (abfd, len);
   11146       if (name == NULL)
   11147 	return false;
   11148 
   11149       memcpy (name, buf, len);
   11150 
   11151       sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11152 
   11153       if (sect == NULL)
   11154 	return false;
   11155 
   11156       if (note->descsz < 12 + name_size)
   11157 	{
   11158 	  _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
   11159 				" is too small to contain a name of size %u"),
   11160 			      abfd, note->descsz, name_size);
   11161 	  return true;
   11162 	}
   11163 
   11164       sect->size = note->descsz;
   11165       sect->filepos = note->descpos;
   11166       sect->alignment_power = 2;
   11167       break;
   11168 
   11169     default:
   11170       return true;
   11171     }
   11172 
   11173   return true;
   11174 }
   11175 
   11176 static bool
   11177 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
   11178 {
   11179   elf_backend_data *bed = get_elf_backend_data (abfd);
   11180 
   11181   /* Short cut for LINUX notes.  */
   11182   if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
   11183       && streq (note->namedata, NOTE_NAME_LINUX))
   11184     {
   11185       switch (note->type)
   11186 	{
   11187 	case NT_386_TLS:	return elfcore_grok_i386_tls (abfd, note);
   11188 	case NT_ARC_V2:		return elfcore_grok_arc_v2 (abfd, note);
   11189 	case NT_ARM_FPMR:	return elfcore_grok_aarch_fpmr (abfd, note);
   11190 	case NT_ARM_GCS:	return elfcore_grok_aarch_gcs (abfd, note);
   11191 	case NT_ARM_HW_BREAK:	return elfcore_grok_aarch_hw_break (abfd, note);
   11192 	case NT_ARM_HW_WATCH:	return elfcore_grok_aarch_hw_watch (abfd, note);
   11193 	case NT_ARM_PAC_MASK:	return elfcore_grok_aarch_pauth (abfd, note);
   11194 	case NT_ARM_SSVE:	return elfcore_grok_aarch_ssve (abfd, note);
   11195 	case NT_ARM_SVE:	return elfcore_grok_aarch_sve (abfd, note);
   11196 	case NT_ARM_TAGGED_ADDR_CTRL: return elfcore_grok_aarch_mte (abfd, note);
   11197 	case NT_ARM_TLS:	return elfcore_grok_aarch_tls (abfd, note);
   11198 	case NT_ARM_VFP:	return elfcore_grok_arm_vfp (abfd, note);
   11199 	case NT_ARM_ZA:		return elfcore_grok_aarch_za (abfd, note);
   11200 	case NT_ARM_ZT:		return elfcore_grok_aarch_zt (abfd, note);
   11201 	case NT_LARCH_CPUCFG:	return elfcore_grok_loongarch_cpucfg (abfd, note);
   11202 	case NT_LARCH_LASX:	return elfcore_grok_loongarch_lasx (abfd, note);
   11203 	case NT_LARCH_LBT:	return elfcore_grok_loongarch_lbt (abfd, note);
   11204 	case NT_LARCH_LSX:	return elfcore_grok_loongarch_lsx (abfd, note);
   11205 	case NT_PPC_DSCR:	return elfcore_grok_ppc_dscr (abfd, note);
   11206 	case NT_PPC_EBB:	return elfcore_grok_ppc_ebb (abfd, note);
   11207 	case NT_PPC_PMU:	return elfcore_grok_ppc_pmu (abfd, note);
   11208 	case NT_PPC_PPR:	return elfcore_grok_ppc_ppr (abfd, note);
   11209 	case NT_PPC_TAR:	return elfcore_grok_ppc_tar (abfd, note);
   11210 	case NT_PPC_TM_CDSCR:	return elfcore_grok_ppc_tm_cdscr (abfd, note);
   11211 	case NT_PPC_TM_CFPR:	return elfcore_grok_ppc_tm_cfpr (abfd, note);
   11212 	case NT_PPC_TM_CGPR:	return elfcore_grok_ppc_tm_cgpr (abfd, note);
   11213 	case NT_PPC_TM_CPPR:	return elfcore_grok_ppc_tm_cppr (abfd, note);
   11214 	case NT_PPC_TM_CTAR:	return elfcore_grok_ppc_tm_ctar (abfd, note);
   11215 	case NT_PPC_TM_CVMX:	return elfcore_grok_ppc_tm_cvmx (abfd, note);
   11216 	case NT_PPC_TM_CVSX:	return elfcore_grok_ppc_tm_cvsx (abfd, note);
   11217 	case NT_PPC_TM_SPR:	return elfcore_grok_ppc_tm_spr (abfd, note);
   11218 	case NT_PPC_VMX:	return elfcore_grok_ppc_vmx (abfd, note);
   11219 	case NT_PPC_VSX:	return elfcore_grok_ppc_vsx (abfd, note);
   11220 	case NT_PRXFPREG:	return elfcore_grok_prxfpreg (abfd, note);
   11221 	case NT_S390_CTRS:	return elfcore_grok_s390_ctrs (abfd, note);
   11222 	case NT_S390_GS_BC:	return elfcore_grok_s390_gs_bc (abfd, note);
   11223 	case NT_S390_GS_CB:	return elfcore_grok_s390_gs_cb (abfd, note);
   11224 	case NT_S390_HIGH_GPRS:	return elfcore_grok_s390_high_gprs (abfd, note);
   11225 	case NT_S390_LAST_BREAK:  return elfcore_grok_s390_last_break (abfd, note);
   11226 	case NT_S390_PREFIX:	return elfcore_grok_s390_prefix (abfd, note);
   11227 	case NT_S390_SYSTEM_CALL: return elfcore_grok_s390_system_call (abfd, note);
   11228 	case NT_S390_TDB:	return elfcore_grok_s390_tdb (abfd, note);
   11229 	case NT_S390_TIMER:	return elfcore_grok_s390_timer (abfd, note);
   11230 	case NT_S390_TODCMP:	return elfcore_grok_s390_todcmp (abfd, note);
   11231 	case NT_S390_TODPREG:	return elfcore_grok_s390_todpreg (abfd, note);
   11232 	case NT_S390_VXRS_HIGH:	return elfcore_grok_s390_vxrs_high (abfd, note);
   11233 	case NT_S390_VXRS_LOW:	return elfcore_grok_s390_vxrs_low (abfd, note);
   11234 	case NT_X86_SHSTK:	return elfcore_grok_sspreg (abfd, note);
   11235 	case NT_X86_XSTATE:	return elfcore_grok_xstatereg (abfd, note);
   11236 	case NT_X86_XSAVE_LAYOUT: return elfcore_grok_xsave_layout_desc (abfd, note);
   11237 	default: break;
   11238 	}
   11239     }
   11240 
   11241   switch (note->type)
   11242     {
   11243     default:
   11244       return true;
   11245 
   11246     case NT_PRSTATUS:
   11247       if (bed->elf_backend_grok_prstatus)
   11248 	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
   11249 	  return true;
   11250 #if defined (HAVE_PRSTATUS_T)
   11251       return elfcore_grok_prstatus (abfd, note);
   11252 #else
   11253       return true;
   11254 #endif
   11255 
   11256 #if defined (HAVE_PSTATUS_T)
   11257     case NT_PSTATUS:
   11258       return elfcore_grok_pstatus (abfd, note);
   11259 #endif
   11260 
   11261 #if defined (HAVE_LWPSTATUS_T)
   11262     case NT_LWPSTATUS:
   11263       return elfcore_grok_lwpstatus (abfd, note);
   11264 #endif
   11265 
   11266     case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG.  */
   11267       return elfcore_grok_prfpreg (abfd, note);
   11268 
   11269     case NT_WIN32PSTATUS:
   11270       return elfcore_grok_win32pstatus (abfd, note);
   11271 
   11272     case NT_GDB_TDESC:
   11273       if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
   11274 	return elfcore_grok_gdb_tdesc (abfd, note);
   11275       else
   11276 	return true;
   11277 
   11278     case NT_RISCV_CSR:
   11279       if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
   11280 	return elfcore_grok_riscv_csr (abfd, note);
   11281       else
   11282 	return true;
   11283 
   11284     case NT_PRPSINFO:
   11285     case NT_PSINFO:
   11286       if (bed->elf_backend_grok_psinfo)
   11287 	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
   11288 	  return true;
   11289 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   11290       return elfcore_grok_psinfo (abfd, note);
   11291 #else
   11292       return true;
   11293 #endif
   11294 
   11295     case NT_AUXV:
   11296       return elfcore_make_auxv_note_section (abfd, note, 0);
   11297 
   11298     case NT_FILE:
   11299       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
   11300 					      note);
   11301 
   11302     case NT_SIGINFO:
   11303       return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
   11304 					      note);
   11305     }
   11306 }
   11307 
   11308 static bool
   11309 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
   11310 {
   11311   struct bfd_build_id* build_id;
   11312 
   11313   if (note->descsz == 0)
   11314     return false;
   11315 
   11316   build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
   11317   if (build_id == NULL)
   11318     return false;
   11319 
   11320   build_id->size = note->descsz;
   11321   memcpy (build_id->data, note->descdata, note->descsz);
   11322   abfd->build_id = build_id;
   11323 
   11324   return true;
   11325 }
   11326 
   11327 static bool
   11328 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
   11329 {
   11330   switch (note->type)
   11331     {
   11332     default:
   11333       return true;
   11334 
   11335     case NT_GNU_PROPERTY_TYPE_0:
   11336       return _bfd_elf_parse_gnu_properties (abfd, note);
   11337 
   11338     case NT_GNU_BUILD_ID:
   11339       return elfobj_grok_gnu_build_id (abfd, note);
   11340     }
   11341 }
   11342 
   11343 static bool
   11344 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
   11345 {
   11346   struct sdt_note *cur =
   11347     (struct sdt_note *) bfd_alloc (abfd,
   11348 				   sizeof (struct sdt_note) + note->descsz);
   11349 
   11350   cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
   11351   cur->size = (bfd_size_type) note->descsz;
   11352   memcpy (cur->data, note->descdata, note->descsz);
   11353 
   11354   elf_tdata (abfd)->sdt_note_head = cur;
   11355 
   11356   return true;
   11357 }
   11358 
   11359 static bool
   11360 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
   11361 {
   11362   switch (note->type)
   11363     {
   11364     case NT_STAPSDT:
   11365       return elfobj_grok_stapsdt_note_1 (abfd, note);
   11366 
   11367     default:
   11368       return true;
   11369     }
   11370 }
   11371 
   11372 static bool
   11373 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
   11374 {
   11375   size_t offset;
   11376 
   11377   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11378     {
   11379     case ELFCLASS32:
   11380       if (note->descsz < 108)
   11381 	return false;
   11382       break;
   11383 
   11384     case ELFCLASS64:
   11385       if (note->descsz < 120)
   11386 	return false;
   11387       break;
   11388 
   11389     default:
   11390       return false;
   11391     }
   11392 
   11393   /* Check for version 1 in pr_version.  */
   11394   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11395     return false;
   11396 
   11397   offset = 4;
   11398 
   11399   /* Skip over pr_psinfosz. */
   11400   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11401     offset += 4;
   11402   else
   11403     {
   11404       offset += 4;	/* Padding before pr_psinfosz. */
   11405       offset += 8;
   11406     }
   11407 
   11408   /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
   11409   elf_tdata (abfd)->core->program
   11410     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
   11411   offset += 17;
   11412 
   11413   /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
   11414   elf_tdata (abfd)->core->command
   11415     = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
   11416   offset += 81;
   11417 
   11418   /* Padding before pr_pid.  */
   11419   offset += 2;
   11420 
   11421   /* The pr_pid field was added in version "1a".  */
   11422   if (note->descsz < offset + 4)
   11423     return true;
   11424 
   11425   elf_tdata (abfd)->core->pid
   11426     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11427 
   11428   return true;
   11429 }
   11430 
   11431 static bool
   11432 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
   11433 {
   11434   size_t offset;
   11435   size_t size;
   11436   size_t min_size;
   11437 
   11438   /* Compute offset of pr_getregsz, skipping over pr_statussz.
   11439      Also compute minimum size of this note.  */
   11440   switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
   11441     {
   11442     case ELFCLASS32:
   11443       offset = 4 + 4;
   11444       min_size = offset + (4 * 2) + 4 + 4 + 4;
   11445       break;
   11446 
   11447     case ELFCLASS64:
   11448       offset = 4 + 4 + 8;	/* Includes padding before pr_statussz.  */
   11449       min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
   11450       break;
   11451 
   11452     default:
   11453       return false;
   11454     }
   11455 
   11456   if (note->descsz < min_size)
   11457     return false;
   11458 
   11459   /* Check for version 1 in pr_version.  */
   11460   if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
   11461     return false;
   11462 
   11463   /* Extract size of pr_reg from pr_gregsetsz.  */
   11464   /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
   11465   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
   11466     {
   11467       size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11468       offset += 4 * 2;
   11469     }
   11470   else
   11471     {
   11472       size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
   11473       offset += 8 * 2;
   11474     }
   11475 
   11476   /* Skip over pr_osreldate.  */
   11477   offset += 4;
   11478 
   11479   /* Read signal from pr_cursig.  */
   11480   if (elf_tdata (abfd)->core->signal == 0)
   11481     elf_tdata (abfd)->core->signal
   11482       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11483   offset += 4;
   11484 
   11485   /* Read TID from pr_pid.  */
   11486   elf_tdata (abfd)->core->lwpid
   11487       = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
   11488   offset += 4;
   11489 
   11490   /* Padding before pr_reg.  */
   11491   if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
   11492     offset += 4;
   11493 
   11494   /* Make sure that there is enough data remaining in the note.  */
   11495   if ((note->descsz - offset) < size)
   11496     return false;
   11497 
   11498   /* Make a ".reg/999" section and a ".reg" section.  */
   11499   return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
   11500 					  size, note->descpos + offset);
   11501 }
   11502 
   11503 static bool
   11504 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
   11505 {
   11506   elf_backend_data *bed = get_elf_backend_data (abfd);
   11507 
   11508   switch (note->type)
   11509     {
   11510     case NT_PRSTATUS:
   11511       if (bed->elf_backend_grok_freebsd_prstatus)
   11512 	if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
   11513 	  return true;
   11514       return elfcore_grok_freebsd_prstatus (abfd, note);
   11515 
   11516     case NT_FPREGSET:
   11517       return elfcore_grok_prfpreg (abfd, note);
   11518 
   11519     case NT_PRPSINFO:
   11520       return elfcore_grok_freebsd_psinfo (abfd, note);
   11521 
   11522     case NT_FREEBSD_THRMISC:
   11523       return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
   11524 
   11525     case NT_FREEBSD_PROCSTAT_PROC:
   11526       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
   11527 					      note);
   11528 
   11529     case NT_FREEBSD_PROCSTAT_FILES:
   11530       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
   11531 					      note);
   11532 
   11533     case NT_FREEBSD_PROCSTAT_VMMAP:
   11534       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
   11535 					      note);
   11536 
   11537     case NT_FREEBSD_PROCSTAT_AUXV:
   11538       return elfcore_make_auxv_note_section (abfd, note, 4);
   11539 
   11540     case NT_FREEBSD_X86_SEGBASES:
   11541       return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_X86_SEGBASES, note);
   11542 
   11543     case NT_X86_XSTATE:
   11544       return elfcore_grok_xstatereg (abfd, note);
   11545 
   11546     case NT_FREEBSD_PTLWPINFO:
   11547       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
   11548 					      note);
   11549 
   11550     case NT_ARM_TLS:
   11551       return elfcore_grok_aarch_tls (abfd, note);
   11552 
   11553     case NT_ARM_VFP:
   11554       return elfcore_grok_arm_vfp (abfd, note);
   11555 
   11556     default:
   11557       return true;
   11558     }
   11559 }
   11560 
   11561 static bool
   11562 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
   11563 {
   11564   char *cp;
   11565 
   11566   cp = strchr (note->namedata, '@');
   11567   if (cp != NULL)
   11568     {
   11569       *lwpidp = atoi(cp + 1);
   11570       return true;
   11571     }
   11572   return false;
   11573 }
   11574 
   11575 static bool
   11576 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11577 {
   11578   if (note->descsz <= 0x7c + 31)
   11579     return false;
   11580 
   11581   /* Signal number at offset 0x08. */
   11582   elf_tdata (abfd)->core->signal
   11583     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11584 
   11585   /* Process ID at offset 0x50. */
   11586   elf_tdata (abfd)->core->pid
   11587     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
   11588 
   11589   /* Command name at 0x7c (max 32 bytes, including nul). */
   11590   elf_tdata (abfd)->core->command
   11591     = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
   11592 
   11593   return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
   11594 					  note);
   11595 }
   11596 
   11597 static bool
   11598 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11599 {
   11600   int lwp;
   11601 
   11602   if (elfcore_netbsd_get_lwpid (note, &lwp))
   11603     elf_tdata (abfd)->core->lwpid = lwp;
   11604 
   11605   switch (note->type)
   11606     {
   11607     case NT_NETBSDCORE_PROCINFO:
   11608       /* NetBSD-specific core "procinfo".  Note that we expect to
   11609 	 find this note before any of the others, which is fine,
   11610 	 since the kernel writes this note out first when it
   11611 	 creates a core file.  */
   11612       return elfcore_grok_netbsd_procinfo (abfd, note);
   11613     case NT_NETBSDCORE_AUXV:
   11614       /* NetBSD-specific Elf Auxiliary Vector data. */
   11615       return elfcore_make_auxv_note_section (abfd, note, 0);
   11616     case NT_NETBSDCORE_LWPSTATUS:
   11617       return elfcore_make_note_pseudosection (abfd,
   11618 					      ".note.netbsdcore.lwpstatus",
   11619 					      note);
   11620     default:
   11621       break;
   11622     }
   11623 
   11624   /* As of March 2020 there are no other machine-independent notes
   11625      defined for NetBSD core files.  If the note type is less
   11626      than the start of the machine-dependent note types, we don't
   11627      understand it.  */
   11628 
   11629   if (note->type < NT_NETBSDCORE_FIRSTMACH)
   11630     return true;
   11631 
   11632 
   11633   switch (bfd_get_arch (abfd))
   11634     {
   11635       /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
   11636 	 PT_GETFPREGS == mach+2.  */
   11637 
   11638     case bfd_arch_aarch64:
   11639     case bfd_arch_alpha:
   11640     case bfd_arch_sparc:
   11641       switch (note->type)
   11642 	{
   11643 	case NT_NETBSDCORE_FIRSTMACH+0:
   11644 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
   11645 
   11646 	case NT_NETBSDCORE_FIRSTMACH+2:
   11647 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   11648 
   11649 	default:
   11650 	  return true;
   11651 	}
   11652 
   11653       /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
   11654 	 There's also old PT___GETREGS40 == mach + 1 for old reg
   11655 	 structure which lacks GBR.  */
   11656 
   11657     case bfd_arch_sh:
   11658       switch (note->type)
   11659 	{
   11660 	case NT_NETBSDCORE_FIRSTMACH+3:
   11661 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
   11662 
   11663 	case NT_NETBSDCORE_FIRSTMACH+5:
   11664 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   11665 
   11666 	default:
   11667 	  return true;
   11668 	}
   11669 
   11670       /* On all other arch's, PT_GETREGS == mach+1 and
   11671 	 PT_GETFPREGS == mach+3.  */
   11672 
   11673     default:
   11674       switch (note->type)
   11675 	{
   11676 	case NT_NETBSDCORE_FIRSTMACH+1:
   11677 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
   11678 
   11679 	case NT_NETBSDCORE_FIRSTMACH+3:
   11680 	  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   11681 
   11682 	default:
   11683 	  return true;
   11684 	}
   11685     }
   11686     /* NOTREACHED */
   11687 }
   11688 
   11689 static bool
   11690 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
   11691 {
   11692   if (note->descsz <= 0x48 + 31)
   11693     return false;
   11694 
   11695   /* Signal number at offset 0x08. */
   11696   elf_tdata (abfd)->core->signal
   11697     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
   11698 
   11699   /* Process ID at offset 0x20. */
   11700   elf_tdata (abfd)->core->pid
   11701     = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
   11702 
   11703   /* Command name at 0x48 (max 32 bytes, including nul). */
   11704   elf_tdata (abfd)->core->command
   11705     = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
   11706 
   11707   return true;
   11708 }
   11709 
   11710 /* Processes Solaris's process status note.
   11711    sig_off ~ offsetof(prstatus_t, pr_cursig)
   11712    pid_off ~ offsetof(prstatus_t, pr_pid)
   11713    lwpid_off ~ offsetof(prstatus_t, pr_who)
   11714    gregset_size ~ sizeof(gregset_t)
   11715    gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
   11716 
   11717 static bool
   11718 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
   11719 			       int pid_off, int lwpid_off, size_t gregset_size,
   11720 			       size_t gregset_offset)
   11721 {
   11722   asection *sect = NULL;
   11723   elf_tdata (abfd)->core->signal
   11724     = bfd_get_16 (abfd, note->descdata + sig_off);
   11725   elf_tdata (abfd)->core->pid
   11726     = bfd_get_32 (abfd, note->descdata + pid_off);
   11727   elf_tdata (abfd)->core->lwpid
   11728     = bfd_get_32 (abfd, note->descdata + lwpid_off);
   11729 
   11730   sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
   11731   if (sect != NULL)
   11732     sect->size = gregset_size;
   11733 
   11734   return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
   11735 					  note->descpos + gregset_offset);
   11736 }
   11737 
   11738 /* Gets program and arguments from a core.
   11739    prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
   11740    comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
   11741 
   11742 static bool
   11743 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
   11744 			  int prog_off, int comm_off)
   11745 {
   11746   elf_tdata (abfd)->core->program
   11747     = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
   11748   elf_tdata (abfd)->core->command
   11749     = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
   11750 
   11751   return true;
   11752 }
   11753 
   11754 /* Processes Solaris's LWP status note.
   11755    gregset_size ~ sizeof(gregset_t)
   11756    gregset_off ~ offsetof(lwpstatus_t, pr_reg)
   11757    fpregset_size ~ sizeof(fpregset_t)
   11758    fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
   11759 
   11760 static bool
   11761 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
   11762 				size_t gregset_size, int gregset_off,
   11763 				size_t fpregset_size, int fpregset_off)
   11764 {
   11765   asection *sect = NULL;
   11766   char reg2_section_name[16] = { 0 };
   11767 
   11768   (void) snprintf (reg2_section_name, 16, "%s/%i", NOTE_PSEUDO_SECTION_REG2,
   11769 		   elf_tdata (abfd)->core->lwpid);
   11770 
   11771   /* offsetof(lwpstatus_t, pr_lwpid) */
   11772   elf_tdata (abfd)->core->lwpid
   11773     = bfd_get_32 (abfd, note->descdata + 4);
   11774   /* offsetof(lwpstatus_t, pr_cursig) */
   11775   elf_tdata (abfd)->core->signal
   11776     = bfd_get_16 (abfd, note->descdata + 12);
   11777 
   11778   sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
   11779   if (sect != NULL)
   11780     sect->size = gregset_size;
   11781   else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
   11782 					     note->descpos + gregset_off))
   11783     return false;
   11784 
   11785   sect = bfd_get_section_by_name (abfd, reg2_section_name);
   11786   if (sect != NULL)
   11787     {
   11788       sect->size = fpregset_size;
   11789       sect->filepos = note->descpos + fpregset_off;
   11790       sect->alignment_power = 2;
   11791     }
   11792   else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, fpregset_size,
   11793 					     note->descpos + fpregset_off))
   11794     return false;
   11795 
   11796   return true;
   11797 }
   11798 
   11799 static bool
   11800 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
   11801 {
   11802   if (note == NULL)
   11803     return false;
   11804 
   11805   /* core files are identified as 32- or 64-bit, SPARC or x86,
   11806      by the size of the descsz which matches the sizeof()
   11807      the type appropriate for that note type (e.g., prstatus_t for
   11808      SOLARIS_NT_PRSTATUS) for the corresponding architecture
   11809      on Solaris. The core file bitness may differ from the bitness of
   11810      gdb itself, so fixed values are used instead of sizeof().
   11811      Appropriate fixed offsets are also used to obtain data from
   11812      the note.  */
   11813 
   11814   switch ((int) note->type)
   11815     {
   11816     case SOLARIS_NT_PRSTATUS:
   11817       switch (note->descsz)
   11818 	{
   11819 	case 508: /* sizeof(prstatus_t) SPARC 32-bit */
   11820 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11821 					       136, 216, 308, 152, 356);
   11822 	case 904: /* sizeof(prstatus_t) SPARC 64-bit */
   11823 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11824 					       264, 360, 520, 304, 600);
   11825 	case 432: /* sizeof(prstatus_t) Intel 32-bit */
   11826 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11827 					       136, 216, 308, 76, 356);
   11828 	case 824: /* sizeof(prstatus_t) Intel 64-bit */
   11829 	  return elfcore_grok_solaris_prstatus(abfd, note,
   11830 					       264, 360, 520, 224, 600);
   11831 	default:
   11832 	  return true;
   11833 	}
   11834 
   11835     case SOLARIS_NT_PSINFO:
   11836     case SOLARIS_NT_PRPSINFO:
   11837       switch (note->descsz)
   11838 	{
   11839 	case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
   11840 	  return elfcore_grok_solaris_info(abfd, note, 84, 100);
   11841 	case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
   11842 	  return elfcore_grok_solaris_info(abfd, note, 120, 136);
   11843 	case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
   11844 	  return elfcore_grok_solaris_info(abfd, note, 88, 104);
   11845 	case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
   11846 	  return elfcore_grok_solaris_info(abfd, note, 136, 152);
   11847 	default:
   11848 	  return true;
   11849 	}
   11850 
   11851     case SOLARIS_NT_LWPSTATUS:
   11852       switch (note->descsz)
   11853 	{
   11854 	case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
   11855 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11856 						152, 344, 400, 496);
   11857 	case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
   11858 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11859 						304, 544, 544, 848);
   11860 	case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
   11861 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11862 						76, 344, 380, 420);
   11863 	case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
   11864 	  return elfcore_grok_solaris_lwpstatus(abfd, note,
   11865 						224, 544, 528, 768);
   11866 	default:
   11867 	  return true;
   11868 	}
   11869 
   11870     case SOLARIS_NT_LWPSINFO:
   11871       /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
   11872       if (note->descsz == 128 || note->descsz == 152)
   11873 	elf_tdata (abfd)->core->lwpid =
   11874 	  bfd_get_32 (abfd, note->descdata + 4);
   11875       break;
   11876 
   11877     default:
   11878       break;
   11879     }
   11880 
   11881   return true;
   11882 }
   11883 
   11884 /* For name starting with "CORE" this may be either a Solaris
   11885    core file or a gdb-generated core file.  Do Solaris-specific
   11886    processing on selected note types first with
   11887    elfcore_grok_solaris_note(), then process the note
   11888    in elfcore_grok_note().  */
   11889 
   11890 static bool
   11891 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
   11892 {
   11893   if (!elfcore_grok_solaris_note_impl (abfd, note))
   11894     return false;
   11895 
   11896   return elfcore_grok_note (abfd, note);
   11897 }
   11898 
   11899 static bool
   11900 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
   11901 {
   11902   if (note->type == NT_OPENBSD_PROCINFO)
   11903     return elfcore_grok_openbsd_procinfo (abfd, note);
   11904 
   11905   if (note->type == NT_OPENBSD_REGS)
   11906     return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
   11907 
   11908   if (note->type == NT_OPENBSD_FPREGS)
   11909     return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
   11910 
   11911   if (note->type == NT_OPENBSD_XFPREGS)
   11912     return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
   11913 
   11914   if (note->type == NT_OPENBSD_AUXV)
   11915     return elfcore_make_auxv_note_section (abfd, note, 0);
   11916 
   11917   if (note->type == NT_OPENBSD_WCOOKIE)
   11918     {
   11919       asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
   11920 							   SEC_HAS_CONTENTS);
   11921 
   11922       if (sect == NULL)
   11923 	return false;
   11924       sect->size = note->descsz;
   11925       sect->filepos = note->descpos;
   11926       sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
   11927 
   11928       return true;
   11929     }
   11930 
   11931   return true;
   11932 }
   11933 
   11934 static bool
   11935 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
   11936 {
   11937   void *ddata = note->descdata;
   11938   char buf[100];
   11939   char *name;
   11940   asection *sect;
   11941   short sig;
   11942   unsigned flags;
   11943 
   11944   if (note->descsz < 16)
   11945     return false;
   11946 
   11947   /* nto_procfs_status 'pid' field is at offset 0.  */
   11948   elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
   11949 
   11950   /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
   11951   *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
   11952 
   11953   /* nto_procfs_status 'flags' field is at offset 8.  */
   11954   flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
   11955 
   11956   /* nto_procfs_status 'what' field is at offset 14.  */
   11957   if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
   11958     {
   11959       elf_tdata (abfd)->core->signal = sig;
   11960       elf_tdata (abfd)->core->lwpid = *tid;
   11961     }
   11962 
   11963   /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
   11964      do not come from signals so we make sure we set the current
   11965      thread just in case.  */
   11966   if (flags & 0x00000080)
   11967     elf_tdata (abfd)->core->lwpid = *tid;
   11968 
   11969   /* Make a ".qnx_core_status/%d" section.  */
   11970   sprintf (buf, ".qnx_core_status/%ld", *tid);
   11971 
   11972   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   11973   if (name == NULL)
   11974     return false;
   11975   strcpy (name, buf);
   11976 
   11977   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   11978   if (sect == NULL)
   11979     return false;
   11980 
   11981   sect->size		= note->descsz;
   11982   sect->filepos		= note->descpos;
   11983   sect->alignment_power = 2;
   11984 
   11985   return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
   11986 }
   11987 
   11988 static bool
   11989 elfcore_grok_nto_regs (bfd *abfd,
   11990 		       Elf_Internal_Note *note,
   11991 		       long tid,
   11992 		       char *base)
   11993 {
   11994   char buf[100];
   11995   char *name;
   11996   asection *sect;
   11997 
   11998   /* Make a "(base)/%d" section.  */
   11999   sprintf (buf, "%s/%ld", base, tid);
   12000 
   12001   name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
   12002   if (name == NULL)
   12003     return false;
   12004   strcpy (name, buf);
   12005 
   12006   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12007   if (sect == NULL)
   12008     return false;
   12009 
   12010   sect->size		= note->descsz;
   12011   sect->filepos		= note->descpos;
   12012   sect->alignment_power = 2;
   12013 
   12014   /* This is the current thread.  */
   12015   if (elf_tdata (abfd)->core->lwpid == tid)
   12016     return elfcore_maybe_make_sect (abfd, base, sect);
   12017 
   12018   return true;
   12019 }
   12020 
   12021 static bool
   12022 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
   12023 {
   12024   /* Every GREG section has a STATUS section before it.  Store the
   12025      tid from the previous call to pass down to the next gregs
   12026      function.  */
   12027   static long tid = 1;
   12028 
   12029   switch (note->type)
   12030     {
   12031     case QNT_CORE_INFO:
   12032       return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
   12033     case QNT_CORE_STATUS:
   12034       return elfcore_grok_nto_status (abfd, note, &tid);
   12035     case QNT_CORE_GREG:
   12036       return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG);
   12037     case QNT_CORE_FPREG:
   12038       return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG2);
   12039     default:
   12040       return true;
   12041     }
   12042 }
   12043 
   12044 static bool
   12045 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
   12046 {
   12047   char *name;
   12048   asection *sect;
   12049   size_t len;
   12050 
   12051   /* Use note name as section name.  */
   12052   len = note->namesz;
   12053   name = (char *) bfd_alloc (abfd, len);
   12054   if (name == NULL)
   12055     return false;
   12056   memcpy (name, note->namedata, len);
   12057   name[len - 1] = '\0';
   12058 
   12059   sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
   12060   if (sect == NULL)
   12061     return false;
   12062 
   12063   sect->size		= note->descsz;
   12064   sect->filepos		= note->descpos;
   12065   sect->alignment_power = 1;
   12066 
   12067   return true;
   12068 }
   12069 
   12070 /* Function: elfcore_write_note
   12071 
   12072    Inputs:
   12073      buffer to hold note, and current size of buffer
   12074      name of note
   12075      type of note
   12076      data for note
   12077      size of data for note
   12078 
   12079    Writes note to end of buffer.  ELF64 notes are written exactly as
   12080    for ELF32, despite the current (as of 2006) ELF gabi specifying
   12081    that they ought to have 8-byte namesz and descsz field, and have
   12082    8-byte alignment.  Other writers, eg. Linux kernel, do the same.
   12083 
   12084    Return:
   12085    Pointer to realloc'd buffer, *BUFSIZ updated.  */
   12086 
   12087 char *
   12088 elfcore_write_note (bfd *abfd,
   12089 		    char *buf,
   12090 		    int *bufsiz,
   12091 		    const char *name,
   12092 		    int type,
   12093 		    const void *input,
   12094 		    int size)
   12095 {
   12096   Elf_External_Note *xnp;
   12097   size_t namesz;
   12098   size_t newspace;
   12099   char *dest;
   12100 
   12101   namesz = 0;
   12102   if (name != NULL)
   12103     namesz = strlen (name) + 1;
   12104 
   12105   newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
   12106 
   12107   buf = (char *) realloc (buf, *bufsiz + newspace);
   12108   if (buf == NULL)
   12109     return buf;
   12110   dest = buf + *bufsiz;
   12111   *bufsiz += newspace;
   12112   xnp = (Elf_External_Note *) dest;
   12113   H_PUT_32 (abfd, namesz, xnp->namesz);
   12114   H_PUT_32 (abfd, size, xnp->descsz);
   12115   H_PUT_32 (abfd, type, xnp->type);
   12116   dest = xnp->name;
   12117   if (name != NULL)
   12118     {
   12119       memcpy (dest, name, namesz);
   12120       dest += namesz;
   12121       while (namesz & 3)
   12122 	{
   12123 	  *dest++ = '\0';
   12124 	  ++namesz;
   12125 	}
   12126     }
   12127   memcpy (dest, input, size);
   12128   dest += size;
   12129   while (size & 3)
   12130     {
   12131       *dest++ = '\0';
   12132       ++size;
   12133     }
   12134   return buf;
   12135 }
   12136 
   12137 /* gcc-8 warns (*) on all the strncpy calls in this function about
   12138    possible string truncation.  The "truncation" is not a bug.  We
   12139    have an external representation of structs with fields that are not
   12140    necessarily NULL terminated and corresponding internal
   12141    representation fields that are one larger so that they can always
   12142    be NULL terminated.
   12143    gcc versions between 4.2 and 4.6 do not allow pragma control of
   12144    diagnostics inside functions, giving a hard error if you try to use
   12145    the finer control available with later versions.
   12146    gcc prior to 4.2 warns about diagnostic push and pop.
   12147    gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
   12148    unless you also add #pragma GCC diagnostic ignored "-Wpragma".
   12149    (*) Depending on your system header files!  */
   12150 #if GCC_VERSION >= 8000
   12151 # pragma GCC diagnostic push
   12152 # pragma GCC diagnostic ignored "-Wstringop-truncation"
   12153 #endif
   12154 char *
   12155 elfcore_write_prpsinfo (bfd  *abfd,
   12156 			char *buf,
   12157 			int  *bufsiz,
   12158 			const char *fname,
   12159 			const char *psargs)
   12160 {
   12161   elf_backend_data *bed = get_elf_backend_data (abfd);
   12162 
   12163   if (bed->elf_backend_write_core_note != NULL)
   12164     {
   12165       char *ret;
   12166       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12167 						 NT_PRPSINFO, fname, psargs);
   12168       if (ret != NULL)
   12169 	return ret;
   12170     }
   12171 
   12172 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
   12173 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
   12174   if (bed->s->elfclass == ELFCLASS32)
   12175     {
   12176 #  if defined (HAVE_PSINFO32_T)
   12177       psinfo32_t data;
   12178       int note_type = NT_PSINFO;
   12179 #  else
   12180       prpsinfo32_t data;
   12181       int note_type = NT_PRPSINFO;
   12182 #  endif
   12183 
   12184       memset (&data, 0, sizeof (data));
   12185       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12186       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12187       return elfcore_write_note (abfd, buf, bufsiz,
   12188 				 NOTE_NAME_CORE, note_type, &data, sizeof (data));
   12189     }
   12190   else
   12191 # endif
   12192     {
   12193 # if defined (HAVE_PSINFO_T)
   12194       psinfo_t data;
   12195       int note_type = NT_PSINFO;
   12196 # else
   12197       prpsinfo_t data;
   12198       int note_type = NT_PRPSINFO;
   12199 # endif
   12200 
   12201       memset (&data, 0, sizeof (data));
   12202       strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
   12203       strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
   12204       return elfcore_write_note (abfd, buf, bufsiz,
   12205 				 NOTE_NAME_CORE, note_type, &data, sizeof (data));
   12206     }
   12207 #endif	/* PSINFO_T or PRPSINFO_T */
   12208 
   12209   free (buf);
   12210   return NULL;
   12211 }
   12212 #if GCC_VERSION >= 8000
   12213 # pragma GCC diagnostic pop
   12214 #endif
   12215 
   12216 char *
   12217 elfcore_write_linux_prpsinfo32
   12218   (bfd *abfd, char *buf, int *bufsiz,
   12219    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12220 {
   12221   if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
   12222     {
   12223       struct elf_external_linux_prpsinfo32_ugid16 data;
   12224 
   12225       swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
   12226       return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
   12227 				 &data, sizeof (data));
   12228     }
   12229   else
   12230     {
   12231       struct elf_external_linux_prpsinfo32_ugid32 data;
   12232 
   12233       swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
   12234       return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
   12235 				 &data, sizeof (data));
   12236     }
   12237 }
   12238 
   12239 char *
   12240 elfcore_write_linux_prpsinfo64
   12241   (bfd *abfd, char *buf, int *bufsiz,
   12242    const struct elf_internal_linux_prpsinfo *prpsinfo)
   12243 {
   12244   if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
   12245     {
   12246       struct elf_external_linux_prpsinfo64_ugid16 data;
   12247 
   12248       swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
   12249       return elfcore_write_note (abfd, buf, bufsiz,
   12250 				 NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
   12251     }
   12252   else
   12253     {
   12254       struct elf_external_linux_prpsinfo64_ugid32 data;
   12255 
   12256       swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
   12257       return elfcore_write_note (abfd, buf, bufsiz,
   12258 				 NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
   12259     }
   12260 }
   12261 
   12262 char *
   12263 elfcore_write_prstatus (bfd *abfd,
   12264 			char *buf,
   12265 			int *bufsiz,
   12266 			long pid,
   12267 			int cursig,
   12268 			const void *gregs)
   12269 {
   12270   elf_backend_data *bed = get_elf_backend_data (abfd);
   12271 
   12272   if (bed->elf_backend_write_core_note != NULL)
   12273     {
   12274       char *ret;
   12275       ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
   12276 						 NT_PRSTATUS,
   12277 						 pid, cursig, gregs);
   12278       if (ret != NULL)
   12279 	return ret;
   12280     }
   12281 
   12282 #if defined (HAVE_PRSTATUS_T)
   12283 #if defined (HAVE_PRSTATUS32_T)
   12284   if (bed->s->elfclass == ELFCLASS32)
   12285     {
   12286       prstatus32_t prstat;
   12287 
   12288       memset (&prstat, 0, sizeof (prstat));
   12289       prstat.pr_pid = pid;
   12290       prstat.pr_cursig = cursig;
   12291       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12292       return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12293 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12294     }
   12295   else
   12296 #endif
   12297     {
   12298       prstatus_t prstat;
   12299 
   12300       memset (&prstat, 0, sizeof (prstat));
   12301       prstat.pr_pid = pid;
   12302       prstat.pr_cursig = cursig;
   12303       memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
   12304       return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12305 				 NT_PRSTATUS, &prstat, sizeof (prstat));
   12306     }
   12307 #endif /* HAVE_PRSTATUS_T */
   12308 
   12309   free (buf);
   12310   return NULL;
   12311 }
   12312 
   12313 #if defined (HAVE_LWPSTATUS_T)
   12314 char *
   12315 elfcore_write_lwpstatus (bfd *abfd,
   12316 			 char *buf,
   12317 			 int *bufsiz,
   12318 			 long pid,
   12319 			 int cursig,
   12320 			 const void *gregs)
   12321 {
   12322   lwpstatus_t lwpstat;
   12323 
   12324   memset (&lwpstat, 0, sizeof (lwpstat));
   12325   lwpstat.pr_lwpid  = pid >> 16;
   12326   lwpstat.pr_cursig = cursig;
   12327 #if defined (HAVE_LWPSTATUS_T_PR_REG)
   12328   memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
   12329 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
   12330 #if !defined(gregs)
   12331   memcpy (lwpstat.pr_context.uc_mcontext.gregs,
   12332 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
   12333 #else
   12334   memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
   12335 	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
   12336 #endif
   12337 #endif
   12338   return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12339 			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
   12340 }
   12341 #endif /* HAVE_LWPSTATUS_T */
   12342 
   12343 #if defined (HAVE_PSTATUS_T)
   12344 char *
   12345 elfcore_write_pstatus (bfd *abfd,
   12346 		       char *buf,
   12347 		       int *bufsiz,
   12348 		       long pid,
   12349 		       int cursig ATTRIBUTE_UNUSED,
   12350 		       const void *gregs ATTRIBUTE_UNUSED)
   12351 {
   12352 #if defined (HAVE_PSTATUS32_T)
   12353   elf_backend_data *bed = get_elf_backend_data (abfd);
   12354 
   12355   if (bed->s->elfclass == ELFCLASS32)
   12356     {
   12357       pstatus32_t pstat;
   12358 
   12359       memset (&pstat, 0, sizeof (pstat));
   12360       pstat.pr_pid = pid & 0xffff;
   12361       buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12362 				NT_PSTATUS, &pstat, sizeof (pstat));
   12363       return buf;
   12364     }
   12365   else
   12366 #endif
   12367     {
   12368       pstatus_t pstat;
   12369 
   12370       memset (&pstat, 0, sizeof (pstat));
   12371       pstat.pr_pid = pid & 0xffff;
   12372       buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
   12373 				NT_PSTATUS, &pstat, sizeof (pstat));
   12374       return buf;
   12375     }
   12376 }
   12377 #endif /* HAVE_PSTATUS_T */
   12378 
   12379 char *
   12380 elfcore_write_prfpreg (bfd *abfd,
   12381 		       char *buf,
   12382 		       int *bufsiz,
   12383 		       const void *fpregs,
   12384 		       int size)
   12385 {
   12386   return elfcore_write_note (abfd, buf, bufsiz,
   12387 			     NOTE_NAME_CORE, NT_FPREGSET, fpregs, size);
   12388 }
   12389 
   12390 char *
   12391 elfcore_write_prxfpreg (bfd *abfd,
   12392 			char *buf,
   12393 			int *bufsiz,
   12394 			const void *xfpregs,
   12395 			int size)
   12396 {
   12397   return elfcore_write_note (abfd, buf, bufsiz,
   12398 			     NOTE_NAME_LINUX, NT_PRXFPREG, xfpregs, size);
   12399 }
   12400 
   12401 char *
   12402 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
   12403 			 const void *xfpregs, int size)
   12404 {
   12405   char *note_name;
   12406 
   12407   if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
   12408     note_name = NOTE_NAME_FREEBSD;
   12409   else
   12410     note_name = NOTE_NAME_LINUX;
   12411 
   12412   return elfcore_write_note (abfd, buf, bufsiz,
   12413 			     note_name, NT_X86_XSTATE, xfpregs, size);
   12414 }
   12415 
   12416 char *
   12417 elfcore_write_xsave_layout (bfd *abfd, char *buf, int *bufsiz,
   12418 			    const void *xsave_layout, int size)
   12419 {
   12420   return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX,
   12421 			     NT_X86_XSAVE_LAYOUT, xsave_layout, size);
   12422 }
   12423 
   12424 static char *
   12425 elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
   12426 		      const void *ssp, int size)
   12427 {
   12428   return elfcore_write_note (abfd, buf, bufsiz,
   12429 			     NOTE_NAME_LINUX, NT_X86_SHSTK, ssp, size);
   12430 }
   12431 
   12432 char *
   12433 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
   12434 			    const void *regs, int size)
   12435 {
   12436   return elfcore_write_note (abfd, buf, bufsiz,
   12437 			     NOTE_NAME_FREEBSD, NT_FREEBSD_X86_SEGBASES,
   12438 			     regs, size);
   12439 }
   12440 
   12441 char *
   12442 elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
   12443 			    const void *regs, int size)
   12444 {
   12445   return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_386_TLS,
   12446 			     regs, size);
   12447 }
   12448 
   12449 char *
   12450 elfcore_write_ppc_vmx (bfd *abfd,
   12451 		       char *buf,
   12452 		       int *bufsiz,
   12453 		       const void *ppc_vmx,
   12454 		       int size)
   12455 {
   12456   return elfcore_write_note (abfd, buf, bufsiz,
   12457 			     NOTE_NAME_LINUX, NT_PPC_VMX, ppc_vmx, size);
   12458 }
   12459 
   12460 char *
   12461 elfcore_write_ppc_vsx (bfd *abfd,
   12462 		       char *buf,
   12463 		       int *bufsiz,
   12464 		       const void *ppc_vsx,
   12465 		       int size)
   12466 {
   12467   return elfcore_write_note (abfd, buf, bufsiz,
   12468 			     NOTE_NAME_LINUX, NT_PPC_VSX, ppc_vsx, size);
   12469 }
   12470 
   12471 char *
   12472 elfcore_write_ppc_tar (bfd *abfd,
   12473 		       char *buf,
   12474 		       int *bufsiz,
   12475 		       const void *ppc_tar,
   12476 		       int size)
   12477 {
   12478   return elfcore_write_note (abfd, buf, bufsiz,
   12479 			     NOTE_NAME_LINUX, NT_PPC_TAR, ppc_tar, size);
   12480 }
   12481 
   12482 char *
   12483 elfcore_write_ppc_ppr (bfd *abfd,
   12484 		       char *buf,
   12485 		       int *bufsiz,
   12486 		       const void *ppc_ppr,
   12487 		       int size)
   12488 {
   12489   return elfcore_write_note (abfd, buf, bufsiz,
   12490 			     NOTE_NAME_LINUX, NT_PPC_PPR, ppc_ppr, size);
   12491 }
   12492 
   12493 char *
   12494 elfcore_write_ppc_dscr (bfd *abfd,
   12495 			char *buf,
   12496 			int *bufsiz,
   12497 			const void *ppc_dscr,
   12498 			int size)
   12499 {
   12500   return elfcore_write_note (abfd, buf, bufsiz,
   12501 			     NOTE_NAME_LINUX, NT_PPC_DSCR, ppc_dscr, size);
   12502 }
   12503 
   12504 char *
   12505 elfcore_write_ppc_ebb (bfd *abfd,
   12506 		       char *buf,
   12507 		       int *bufsiz,
   12508 		       const void *ppc_ebb,
   12509 		       int size)
   12510 {
   12511   return elfcore_write_note (abfd, buf, bufsiz,
   12512 			     NOTE_NAME_LINUX, NT_PPC_EBB, ppc_ebb, size);
   12513 }
   12514 
   12515 char *
   12516 elfcore_write_ppc_pmu (bfd *abfd,
   12517 		       char *buf,
   12518 		       int *bufsiz,
   12519 		       const void *ppc_pmu,
   12520 		       int size)
   12521 {
   12522   return elfcore_write_note (abfd, buf, bufsiz,
   12523 			     NOTE_NAME_LINUX, NT_PPC_PMU, ppc_pmu, size);
   12524 }
   12525 
   12526 char *
   12527 elfcore_write_ppc_tm_cgpr (bfd *abfd,
   12528 			   char *buf,
   12529 			   int *bufsiz,
   12530 			   const void *ppc_tm_cgpr,
   12531 			   int size)
   12532 {
   12533   return elfcore_write_note (abfd, buf, bufsiz,
   12534 			     NOTE_NAME_LINUX, NT_PPC_TM_CGPR,
   12535 			     ppc_tm_cgpr, size);
   12536 }
   12537 
   12538 char *
   12539 elfcore_write_ppc_tm_cfpr (bfd *abfd,
   12540 			   char *buf,
   12541 			   int *bufsiz,
   12542 			   const void *ppc_tm_cfpr,
   12543 			   int size)
   12544 {
   12545   return elfcore_write_note (abfd, buf, bufsiz,
   12546 			     NOTE_NAME_LINUX, NT_PPC_TM_CFPR,
   12547 			     ppc_tm_cfpr, size);
   12548 }
   12549 
   12550 char *
   12551 elfcore_write_ppc_tm_cvmx (bfd *abfd,
   12552 			   char *buf,
   12553 			   int *bufsiz,
   12554 			   const void *ppc_tm_cvmx,
   12555 			   int size)
   12556 {
   12557   return elfcore_write_note (abfd, buf, bufsiz,
   12558 			     NOTE_NAME_LINUX, NT_PPC_TM_CVMX,
   12559 			     ppc_tm_cvmx, size);
   12560 }
   12561 
   12562 char *
   12563 elfcore_write_ppc_tm_cvsx (bfd *abfd,
   12564 			   char *buf,
   12565 			   int *bufsiz,
   12566 			   const void *ppc_tm_cvsx,
   12567 			   int size)
   12568 {
   12569   return elfcore_write_note (abfd, buf, bufsiz,
   12570 			     NOTE_NAME_LINUX, NT_PPC_TM_CVSX,
   12571 			     ppc_tm_cvsx, size);
   12572 }
   12573 
   12574 char *
   12575 elfcore_write_ppc_tm_spr (bfd *abfd,
   12576 			  char *buf,
   12577 			  int *bufsiz,
   12578 			  const void *ppc_tm_spr,
   12579 			  int size)
   12580 {
   12581   return elfcore_write_note (abfd, buf, bufsiz,
   12582 			     NOTE_NAME_LINUX, NT_PPC_TM_SPR,
   12583 			     ppc_tm_spr, size);
   12584 }
   12585 
   12586 char *
   12587 elfcore_write_ppc_tm_ctar (bfd *abfd,
   12588 			   char *buf,
   12589 			   int *bufsiz,
   12590 			   const void *ppc_tm_ctar,
   12591 			   int size)
   12592 {
   12593   return elfcore_write_note (abfd, buf, bufsiz,
   12594 			     NOTE_NAME_LINUX, NT_PPC_TM_CTAR,
   12595 			     ppc_tm_ctar, size);
   12596 }
   12597 
   12598 char *
   12599 elfcore_write_ppc_tm_cppr (bfd *abfd,
   12600 			   char *buf,
   12601 			   int *bufsiz,
   12602 			   const void *ppc_tm_cppr,
   12603 			   int size)
   12604 {
   12605   return elfcore_write_note (abfd, buf, bufsiz,
   12606 			     NOTE_NAME_LINUX, NT_PPC_TM_CPPR,
   12607 			     ppc_tm_cppr, size);
   12608 }
   12609 
   12610 char *
   12611 elfcore_write_ppc_tm_cdscr (bfd *abfd,
   12612 			    char *buf,
   12613 			    int *bufsiz,
   12614 			    const void *ppc_tm_cdscr,
   12615 			    int size)
   12616 {
   12617   return elfcore_write_note (abfd, buf, bufsiz,
   12618 			     NOTE_NAME_LINUX, NT_PPC_TM_CDSCR,
   12619 			     ppc_tm_cdscr, size);
   12620 }
   12621 
   12622 static char *
   12623 elfcore_write_s390_high_gprs (bfd *abfd,
   12624 			      char *buf,
   12625 			      int *bufsiz,
   12626 			      const void *s390_high_gprs,
   12627 			      int size)
   12628 {
   12629   return elfcore_write_note (abfd, buf, bufsiz,
   12630 			     NOTE_NAME_LINUX, NT_S390_HIGH_GPRS,
   12631 			     s390_high_gprs, size);
   12632 }
   12633 
   12634 char *
   12635 elfcore_write_s390_timer (bfd *abfd,
   12636 			  char *buf,
   12637 			  int *bufsiz,
   12638 			  const void *s390_timer,
   12639 			  int size)
   12640 {
   12641   return elfcore_write_note (abfd, buf, bufsiz,
   12642 			     NOTE_NAME_LINUX, NT_S390_TIMER,
   12643 			     s390_timer, size);
   12644 }
   12645 
   12646 char *
   12647 elfcore_write_s390_todcmp (bfd *abfd,
   12648 			   char *buf,
   12649 			   int *bufsiz,
   12650 			   const void *s390_todcmp,
   12651 			   int size)
   12652 {
   12653   return elfcore_write_note (abfd, buf, bufsiz,
   12654 			     NOTE_NAME_LINUX, NT_S390_TODCMP,
   12655 			     s390_todcmp, size);
   12656 }
   12657 
   12658 char *
   12659 elfcore_write_s390_todpreg (bfd *abfd,
   12660 			    char *buf,
   12661 			    int *bufsiz,
   12662 			    const void *s390_todpreg,
   12663 			    int size)
   12664 {
   12665   return elfcore_write_note (abfd, buf, bufsiz,
   12666 			     NOTE_NAME_LINUX, NT_S390_TODPREG,
   12667 			     s390_todpreg, size);
   12668 }
   12669 
   12670 char *
   12671 elfcore_write_s390_ctrs (bfd *abfd,
   12672 			 char *buf,
   12673 			 int *bufsiz,
   12674 			 const void *s390_ctrs,
   12675 			 int size)
   12676 {
   12677   return elfcore_write_note (abfd, buf, bufsiz,
   12678 			     NOTE_NAME_LINUX, NT_S390_CTRS, s390_ctrs, size);
   12679 }
   12680 
   12681 char *
   12682 elfcore_write_s390_prefix (bfd *abfd,
   12683 			   char *buf,
   12684 			   int *bufsiz,
   12685 			   const void *s390_prefix,
   12686 			   int size)
   12687 {
   12688   return elfcore_write_note (abfd, buf, bufsiz,
   12689 			     NOTE_NAME_LINUX, NT_S390_PREFIX,
   12690 			     s390_prefix, size);
   12691 }
   12692 
   12693 char *
   12694 elfcore_write_s390_last_break (bfd *abfd,
   12695 			       char *buf,
   12696 			       int *bufsiz,
   12697 			       const void *s390_last_break,
   12698 			       int size)
   12699 {
   12700   return elfcore_write_note (abfd, buf, bufsiz,
   12701 			     NOTE_NAME_LINUX, NT_S390_LAST_BREAK,
   12702 			     s390_last_break, size);
   12703 }
   12704 
   12705 char *
   12706 elfcore_write_s390_system_call (bfd *abfd,
   12707 				char *buf,
   12708 				int *bufsiz,
   12709 				const void *s390_system_call,
   12710 				int size)
   12711 {
   12712   return elfcore_write_note (abfd, buf, bufsiz,
   12713 			     NOTE_NAME_LINUX, NT_S390_SYSTEM_CALL,
   12714 			     s390_system_call, size);
   12715 }
   12716 
   12717 char *
   12718 elfcore_write_s390_tdb (bfd *abfd,
   12719 			char *buf,
   12720 			int *bufsiz,
   12721 			const void *s390_tdb,
   12722 			int size)
   12723 {
   12724   return elfcore_write_note (abfd, buf, bufsiz,
   12725 			     NOTE_NAME_LINUX, NT_S390_TDB, s390_tdb, size);
   12726 }
   12727 
   12728 char *
   12729 elfcore_write_s390_vxrs_low (bfd *abfd,
   12730 			     char *buf,
   12731 			     int *bufsiz,
   12732 			     const void *s390_vxrs_low,
   12733 			     int size)
   12734 {
   12735   return elfcore_write_note (abfd, buf, bufsiz,
   12736 			     NOTE_NAME_LINUX, NT_S390_VXRS_LOW,
   12737 			     s390_vxrs_low, size);
   12738 }
   12739 
   12740 char *
   12741 elfcore_write_s390_vxrs_high (bfd *abfd,
   12742 			     char *buf,
   12743 			     int *bufsiz,
   12744 			     const void *s390_vxrs_high,
   12745 			     int size)
   12746 {
   12747   return elfcore_write_note (abfd, buf, bufsiz,
   12748 			     NOTE_NAME_LINUX, NT_S390_VXRS_HIGH,
   12749 			     s390_vxrs_high, size);
   12750 }
   12751 
   12752 char *
   12753 elfcore_write_s390_gs_cb (bfd *abfd,
   12754 			  char *buf,
   12755 			  int *bufsiz,
   12756 			  const void *s390_gs_cb,
   12757 			  int size)
   12758 {
   12759   return elfcore_write_note (abfd, buf, bufsiz,
   12760 			     NOTE_NAME_LINUX, NT_S390_GS_CB,
   12761 			     s390_gs_cb, size);
   12762 }
   12763 
   12764 char *
   12765 elfcore_write_s390_gs_bc (bfd *abfd,
   12766 			  char *buf,
   12767 			  int *bufsiz,
   12768 			  const void *s390_gs_bc,
   12769 			  int size)
   12770 {
   12771   return elfcore_write_note (abfd, buf, bufsiz,
   12772 			     NOTE_NAME_LINUX, NT_S390_GS_BC,
   12773 			     s390_gs_bc, size);
   12774 }
   12775 
   12776 char *
   12777 elfcore_write_arm_vfp (bfd *abfd,
   12778 		       char *buf,
   12779 		       int *bufsiz,
   12780 		       const void *arm_vfp,
   12781 		       int size)
   12782 {
   12783   return elfcore_write_note (abfd, buf, bufsiz,
   12784 			     NOTE_NAME_LINUX, NT_ARM_VFP,
   12785 			     arm_vfp, size);
   12786 }
   12787 
   12788 char *
   12789 elfcore_write_aarch_tls (bfd *abfd,
   12790 		       char *buf,
   12791 		       int *bufsiz,
   12792 		       const void *aarch_tls,
   12793 		       int size)
   12794 {
   12795   return elfcore_write_note (abfd, buf, bufsiz,
   12796 			     NOTE_NAME_LINUX, NT_ARM_TLS, aarch_tls, size);
   12797 }
   12798 
   12799 char *
   12800 elfcore_write_aarch_hw_break (bfd *abfd,
   12801 			    char *buf,
   12802 			    int *bufsiz,
   12803 			    const void *aarch_hw_break,
   12804 			    int size)
   12805 {
   12806   return elfcore_write_note (abfd, buf, bufsiz,
   12807 			     NOTE_NAME_LINUX, NT_ARM_HW_BREAK,
   12808 			     aarch_hw_break, size);
   12809 }
   12810 
   12811 char *
   12812 elfcore_write_aarch_hw_watch (bfd *abfd,
   12813 			    char *buf,
   12814 			    int *bufsiz,
   12815 			    const void *aarch_hw_watch,
   12816 			    int size)
   12817 {
   12818   return elfcore_write_note (abfd, buf, bufsiz,
   12819 			     NOTE_NAME_LINUX, NT_ARM_HW_WATCH,
   12820 			     aarch_hw_watch, size);
   12821 }
   12822 
   12823 char *
   12824 elfcore_write_aarch_sve (bfd *abfd,
   12825 			 char *buf,
   12826 			 int *bufsiz,
   12827 			 const void *aarch_sve,
   12828 			 int size)
   12829 {
   12830   return elfcore_write_note (abfd, buf, bufsiz,
   12831 			     NOTE_NAME_LINUX, NT_ARM_SVE, aarch_sve, size);
   12832 }
   12833 
   12834 char *
   12835 elfcore_write_aarch_pauth (bfd *abfd,
   12836 			   char *buf,
   12837 			   int *bufsiz,
   12838 			   const void *aarch_pauth,
   12839 			   int size)
   12840 {
   12841   return elfcore_write_note (abfd, buf, bufsiz,
   12842 			     NOTE_NAME_LINUX, NT_ARM_PAC_MASK,
   12843 			     aarch_pauth, size);
   12844 }
   12845 
   12846 char *
   12847 elfcore_write_aarch_mte (bfd *abfd,
   12848 			 char *buf,
   12849 			 int *bufsiz,
   12850 			 const void *aarch_mte,
   12851 			 int size)
   12852 {
   12853   return elfcore_write_note (abfd, buf, bufsiz,
   12854 			     NOTE_NAME_LINUX, NT_ARM_TAGGED_ADDR_CTRL,
   12855 			     aarch_mte, size);
   12856 }
   12857 
   12858 char *
   12859 elfcore_write_aarch_ssve (bfd *abfd,
   12860 			  char *buf,
   12861 			  int *bufsiz,
   12862 			  const void *aarch_ssve,
   12863 			  int size)
   12864 {
   12865   return elfcore_write_note (abfd, buf, bufsiz,
   12866 			     NOTE_NAME_LINUX, NT_ARM_SSVE,
   12867 			     aarch_ssve, size);
   12868 }
   12869 
   12870 char *
   12871 elfcore_write_aarch_za (bfd *abfd,
   12872 			char *buf,
   12873 			int *bufsiz,
   12874 			const void *aarch_za,
   12875 			int size)
   12876 {
   12877   return elfcore_write_note (abfd, buf, bufsiz,
   12878 			     NOTE_NAME_LINUX, NT_ARM_ZA,
   12879 			     aarch_za, size);
   12880 }
   12881 
   12882 /* Write the buffer of zt register values in aarch_zt (length SIZE) into
   12883    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12884    written into.  Return a pointer to the new start of the note buffer, to
   12885    replace BUF which may no longer be valid.  */
   12886 
   12887 char *
   12888 elfcore_write_aarch_zt (bfd *abfd,
   12889 			char *buf,
   12890 			int *bufsiz,
   12891 			const void *aarch_zt,
   12892 			int size)
   12893 {
   12894   return elfcore_write_note (abfd, buf, bufsiz,
   12895 			     NOTE_NAME_LINUX, NT_ARM_ZT,
   12896 			     aarch_zt, size);
   12897 }
   12898 
   12899 /* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
   12900    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12901    written into.  Return a pointer to the new start of the note buffer, to
   12902    replace BUF which may no longer be valid.  */
   12903 
   12904 static char *
   12905 elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
   12906 			 const void *aarch_gcs, int size)
   12907 {
   12908   return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_GCS,
   12909 			     aarch_gcs, size);
   12910 }
   12911 
   12912 /* Write the buffer of FPMR value in AARCH_FPMR (length SIZE) into
   12913    the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12914    written into.  Return a pointer to the new start of the note buffer, to
   12915    replace BUF which may no longer be valid.  */
   12916 
   12917 char *
   12918 elfcore_write_aarch_fpmr (bfd *abfd,
   12919 			char *buf,
   12920 			int *bufsiz,
   12921 			const void *aarch_fpmr,
   12922 			int size)
   12923 {
   12924   return elfcore_write_note (abfd, buf, bufsiz,
   12925 			     NOTE_NAME_LINUX, NT_ARM_FPMR, aarch_fpmr, size);
   12926 }
   12927 
   12928 char *
   12929 elfcore_write_arc_v2 (bfd *abfd,
   12930 		      char *buf,
   12931 		      int *bufsiz,
   12932 		      const void *arc_v2,
   12933 		      int size)
   12934 {
   12935   return elfcore_write_note (abfd, buf, bufsiz,
   12936 			     NOTE_NAME_LINUX, NT_ARC_V2, arc_v2, size);
   12937 }
   12938 
   12939 char *
   12940 elfcore_write_loongarch_cpucfg (bfd *abfd,
   12941 				char *buf,
   12942 				int *bufsiz,
   12943 				const void *loongarch_cpucfg,
   12944 				int size)
   12945 {
   12946   return elfcore_write_note (abfd, buf, bufsiz,
   12947 			     NOTE_NAME_LINUX, NT_LARCH_CPUCFG,
   12948 			     loongarch_cpucfg, size);
   12949 }
   12950 
   12951 char *
   12952 elfcore_write_loongarch_lbt (bfd *abfd,
   12953 			     char *buf,
   12954 			     int *bufsiz,
   12955 			     const void *loongarch_lbt,
   12956 			     int size)
   12957 {
   12958   return elfcore_write_note (abfd, buf, bufsiz,
   12959 			     NOTE_NAME_LINUX, NT_LARCH_LBT,
   12960 			     loongarch_lbt, size);
   12961 }
   12962 
   12963 char *
   12964 elfcore_write_loongarch_lsx (bfd *abfd,
   12965 			     char *buf,
   12966 			     int *bufsiz,
   12967 			     const void *loongarch_lsx,
   12968 			     int size)
   12969 {
   12970   return elfcore_write_note (abfd, buf, bufsiz,
   12971 			     NOTE_NAME_LINUX, NT_LARCH_LSX,
   12972 			     loongarch_lsx, size);
   12973 }
   12974 
   12975 char *
   12976 elfcore_write_loongarch_lasx (bfd *abfd,
   12977 			      char *buf,
   12978 			      int *bufsiz,
   12979 			      const void *loongarch_lasx,
   12980 			      int size)
   12981 {
   12982   return elfcore_write_note (abfd, buf, bufsiz,
   12983 			     NOTE_NAME_LINUX, NT_LARCH_LASX,
   12984 			     loongarch_lasx, size);
   12985 }
   12986 
   12987 /* Write the buffer of csr values in CSRS (length SIZE) into the note
   12988    buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
   12989    written into.  Return a pointer to the new start of the note buffer, to
   12990    replace BUF which may no longer be valid.  */
   12991 
   12992 char *
   12993 elfcore_write_riscv_csr (bfd *abfd,
   12994 			 char *buf,
   12995 			 int *bufsiz,
   12996 			 const void *csrs,
   12997 			 int size)
   12998 {
   12999   return elfcore_write_note (abfd, buf, bufsiz,
   13000 			     NOTE_NAME_GDB, NT_RISCV_CSR, csrs, size);
   13001 }
   13002 
   13003 /* Write the target description (a string) pointed to by TDESC, length
   13004    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
   13005    note is being written into.  Return a pointer to the new start of the
   13006    note buffer, to replace BUF which may no longer be valid.  */
   13007 
   13008 char *
   13009 elfcore_write_gdb_tdesc (bfd *abfd,
   13010 			 char *buf,
   13011 			 int *bufsiz,
   13012 			 const void *tdesc,
   13013 			 int size)
   13014 {
   13015   return elfcore_write_note (abfd, buf, bufsiz,
   13016 			     NOTE_NAME_GDB, NT_GDB_TDESC, tdesc, size);
   13017 }
   13018 
   13019 char *
   13020 elfcore_write_register_note (bfd *abfd,
   13021 			     char *buf,
   13022 			     int *bufsiz,
   13023 			     const char *section,
   13024 			     const void *data,
   13025 			     int size)
   13026 {
   13027   static const struct
   13028     {
   13029       const char * section_name;
   13030       char *       (*writer) (bfd *, char *, int *, const void *, int);
   13031     }
   13032   note_writers [] =
   13033     {
   13034       { NOTE_PSEUDO_SECTION_AARCH_FPMR,       elfcore_write_aarch_fpmr},
   13035       { NOTE_PSEUDO_SECTION_AARCH_GCS,        elfcore_write_aarch_gcs},
   13036       { NOTE_PSEUDO_SECTION_AARCH_HW_BREAK,   elfcore_write_aarch_hw_break},
   13037       { NOTE_PSEUDO_SECTION_AARCH_HW_WATCH,   elfcore_write_aarch_hw_watch},
   13038       { NOTE_PSEUDO_SECTION_AARCH_MTE,        elfcore_write_aarch_mte},
   13039       { NOTE_PSEUDO_SECTION_AARCH_PAUTH,      elfcore_write_aarch_pauth},
   13040       { NOTE_PSEUDO_SECTION_AARCH_SSVE,       elfcore_write_aarch_ssve},
   13041       { NOTE_PSEUDO_SECTION_AARCH_SVE,        elfcore_write_aarch_sve},
   13042       { NOTE_PSEUDO_SECTION_AARCH_TLS,        elfcore_write_aarch_tls},
   13043       { NOTE_PSEUDO_SECTION_AARCH_ZA,         elfcore_write_aarch_za},
   13044       { NOTE_PSEUDO_SECTION_AARCH_ZT,         elfcore_write_aarch_zt},
   13045       { NOTE_PSEUDO_SECTION_ARC_V2,           elfcore_write_arc_v2},
   13046       { NOTE_PSEUDO_SECTION_ARM_VFP,          elfcore_write_arm_vfp},
   13047       { NOTE_PSEUDO_SECTION_I386_TLS,         elfcore_write_i386_tls},
   13048       { NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, elfcore_write_loongarch_cpucfg},
   13049       { NOTE_PSEUDO_SECTION_LOONGARCH_LASX,   elfcore_write_loongarch_lasx},
   13050       { NOTE_PSEUDO_SECTION_LOONGARCH_LBT,    elfcore_write_loongarch_lbt},
   13051       { NOTE_PSEUDO_SECTION_LOONGARCH_LSX,    elfcore_write_loongarch_lsx},
   13052       { NOTE_PSEUDO_SECTION_PPC_DSCR,         elfcore_write_ppc_dscr},
   13053       { NOTE_PSEUDO_SECTION_PPC_EBB,          elfcore_write_ppc_ebb},
   13054       { NOTE_PSEUDO_SECTION_PPC_PMU,          elfcore_write_ppc_pmu},
   13055       { NOTE_PSEUDO_SECTION_PPC_PPR,          elfcore_write_ppc_ppr},
   13056       { NOTE_PSEUDO_SECTION_PPC_TAR,          elfcore_write_ppc_tar},
   13057       { NOTE_PSEUDO_SECTION_PPC_TM_CDSCR,     elfcore_write_ppc_tm_cdscr},
   13058       { NOTE_PSEUDO_SECTION_PPC_TM_CFPR,      elfcore_write_ppc_tm_cfpr},
   13059       { NOTE_PSEUDO_SECTION_PPC_TM_CGPR,      elfcore_write_ppc_tm_cgpr},
   13060       { NOTE_PSEUDO_SECTION_PPC_TM_CPPR,      elfcore_write_ppc_tm_cppr},
   13061       { NOTE_PSEUDO_SECTION_PPC_TM_CTAR,      elfcore_write_ppc_tm_ctar},
   13062       { NOTE_PSEUDO_SECTION_PPC_TM_CVMX,      elfcore_write_ppc_tm_cvmx},
   13063       { NOTE_PSEUDO_SECTION_PPC_TM_CVSX,      elfcore_write_ppc_tm_cvsx},
   13064       { NOTE_PSEUDO_SECTION_PPC_TM_SPR,       elfcore_write_ppc_tm_spr},
   13065       { NOTE_PSEUDO_SECTION_PPC_VMX,          elfcore_write_ppc_vmx},
   13066       { NOTE_PSEUDO_SECTION_PPC_VSX,          elfcore_write_ppc_vsx},
   13067       { NOTE_PSEUDO_SECTION_REG2,             elfcore_write_prfpreg},
   13068       { NOTE_PSEUDO_SECTION_RISCV_CSR,        elfcore_write_riscv_csr},
   13069       { NOTE_PSEUDO_SECTION_S390_CTRS,        elfcore_write_s390_ctrs},
   13070       { NOTE_PSEUDO_SECTION_S390_GS_BC,       elfcore_write_s390_gs_bc},
   13071       { NOTE_PSEUDO_SECTION_S390_GS_CB,       elfcore_write_s390_gs_cb},
   13072       { NOTE_PSEUDO_SECTION_S390_HIGH_GPRS,   elfcore_write_s390_high_gprs},
   13073       { NOTE_PSEUDO_SECTION_S390_LAST_BREAK,  elfcore_write_s390_last_break},
   13074       { NOTE_PSEUDO_SECTION_S390_PREFIX,      elfcore_write_s390_prefix},
   13075       { NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, elfcore_write_s390_system_call},
   13076       { NOTE_PSEUDO_SECTION_S390_TDB,         elfcore_write_s390_tdb},
   13077       { NOTE_PSEUDO_SECTION_S390_TIMER,       elfcore_write_s390_timer},
   13078       { NOTE_PSEUDO_SECTION_S390_TODCMP,      elfcore_write_s390_todcmp},
   13079       { NOTE_PSEUDO_SECTION_S390_TODPREG,     elfcore_write_s390_todpreg},
   13080       { NOTE_PSEUDO_SECTION_S390_VXRS_HIGH,   elfcore_write_s390_vxrs_high},
   13081       { NOTE_PSEUDO_SECTION_S390_VXRS_LOW,    elfcore_write_s390_vxrs_low},
   13082       { NOTE_PSEUDO_SECTION_SSP,              elfcore_write_sspreg},
   13083       { NOTE_PSEUDO_SECTION_TDESC,            elfcore_write_gdb_tdesc},
   13084       { NOTE_PSEUDO_SECTION_X86_SEGBASES,     elfcore_write_x86_segbases},
   13085       { NOTE_PSEUDO_SECTION_XFP,              elfcore_write_prxfpreg},
   13086       { NOTE_PSEUDO_SECTION_XSTATE,           elfcore_write_xstatereg},
   13087       { NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,     elfcore_write_xsave_layout} /* NB/ No comma.  */
   13088     };
   13089 
   13090   int i;
   13091 
   13092   for (i = ARRAY_SIZE (note_writers); i--;)
   13093     if (streq (section, note_writers[i].section_name))
   13094       return  note_writers[i].writer (abfd, buf, bufsiz, data, size);
   13095 
   13096   return NULL;
   13097 }
   13098 
   13099 char *
   13100 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
   13101 			 const void *buf, int bufsiz)
   13102 {
   13103   return elfcore_write_note (obfd, note_data, note_size,
   13104 			     NOTE_NAME_CORE, NT_FILE, buf, bufsiz);
   13105 }
   13106 
   13107 static bool
   13108 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
   13109 		 size_t align)
   13110 {
   13111   char *p;
   13112 
   13113   /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
   13114      gABI specifies that PT_NOTE alignment should be aligned to 4
   13115      bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
   13116      align is less than 4, we use 4 byte alignment.   */
   13117   if (align < 4)
   13118     align = 4;
   13119   if (align != 4 && align != 8)
   13120     return false;
   13121 
   13122   p = buf;
   13123   while (p < buf + size)
   13124     {
   13125       Elf_External_Note *xnp = (Elf_External_Note *) p;
   13126       Elf_Internal_Note in;
   13127 
   13128       if (offsetof (Elf_External_Note, name) > buf - p + size)
   13129 	return false;
   13130 
   13131       in.type = H_GET_32 (abfd, xnp->type);
   13132 
   13133       in.namesz = H_GET_32 (abfd, xnp->namesz);
   13134       in.namedata = xnp->name;
   13135       if (in.namesz > buf - in.namedata + size)
   13136 	return false;
   13137 
   13138       in.descsz = H_GET_32 (abfd, xnp->descsz);
   13139       in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
   13140       in.descpos = offset + (in.descdata - buf);
   13141       if (in.descsz != 0
   13142 	  && (in.descdata >= buf + size
   13143 	      || in.descsz > buf - in.descdata + size))
   13144 	return false;
   13145 
   13146       switch (bfd_get_format (abfd))
   13147 	{
   13148 	default:
   13149 	  return true;
   13150 
   13151 	case bfd_core:
   13152 	  {
   13153 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
   13154 	    static const struct
   13155 	    {
   13156 	      const char * string;
   13157 	      size_t len;
   13158 	      bool (*func) (bfd *, Elf_Internal_Note *);
   13159 	    }
   13160 	    grokers[] =
   13161 	    {
   13162 	      GROKER_ELEMENT ("", elfcore_grok_note),
   13163 	      GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
   13164 	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
   13165 	      GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
   13166 	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
   13167 	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
   13168 	      GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
   13169 	      GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
   13170 	    };
   13171 #undef GROKER_ELEMENT
   13172 	    int i;
   13173 
   13174 	    for (i = ARRAY_SIZE (grokers); i--;)
   13175 	      {
   13176 		if (in.namesz >= grokers[i].len
   13177 		    && strncmp (in.namedata, grokers[i].string,
   13178 				grokers[i].len) == 0)
   13179 		  {
   13180 		    if (! grokers[i].func (abfd, & in))
   13181 		      return false;
   13182 		    break;
   13183 		  }
   13184 	      }
   13185 	    break;
   13186 	  }
   13187 
   13188 	case bfd_object:
   13189 	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
   13190 	    {
   13191 	      if (! elfobj_grok_gnu_note (abfd, &in))
   13192 		return false;
   13193 	    }
   13194 	  else if (in.namesz == sizeof "stapsdt"
   13195 		   && strcmp (in.namedata, "stapsdt") == 0)
   13196 	    {
   13197 	      if (! elfobj_grok_stapsdt_note (abfd, &in))
   13198 		return false;
   13199 	    }
   13200 	  break;
   13201 	}
   13202 
   13203       p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
   13204     }
   13205 
   13206   return true;
   13207 }
   13208 
   13209 bool
   13210 _bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
   13211 		     size_t align)
   13212 {
   13213   char *buf;
   13214 
   13215   if (size == 0 || (size + 1) == 0)
   13216     return true;
   13217 
   13218   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
   13219     return false;
   13220 
   13221   buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
   13222   if (buf == NULL)
   13223     return false;
   13224 
   13225   /* PR 17512: file: ec08f814
   13226      0-termintate the buffer so that string searches will not overflow.  */
   13227   buf[size] = 0;
   13228 
   13229   if (!elf_parse_notes (abfd, buf, size, offset, align))
   13230     {
   13231       free (buf);
   13232       return false;
   13233     }
   13234 
   13235   free (buf);
   13236   return true;
   13237 }
   13238 
   13239 /* Providing external access to the ELF program header table.  */
   13241 
   13242 /* Return an upper bound on the number of bytes required to store a
   13243    copy of ABFD's program header table entries.  Return -1 if an error
   13244    occurs; bfd_get_error will return an appropriate code.  */
   13245 
   13246 long
   13247 bfd_get_elf_phdr_upper_bound (bfd *abfd)
   13248 {
   13249   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13250     {
   13251       bfd_set_error (bfd_error_wrong_format);
   13252       return -1;
   13253     }
   13254 
   13255   return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
   13256 }
   13257 
   13258 /* Copy ABFD's program header table entries to *PHDRS.  The entries
   13259    will be stored as an array of Elf_Internal_Phdr structures, as
   13260    defined in include/elf/internal.h.  To find out how large the
   13261    buffer needs to be, call bfd_get_elf_phdr_upper_bound.
   13262 
   13263    Return the number of program header table entries read, or -1 if an
   13264    error occurs; bfd_get_error will return an appropriate code.  */
   13265 
   13266 int
   13267 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
   13268 {
   13269   int num_phdrs;
   13270 
   13271   if (abfd->xvec->flavour != bfd_target_elf_flavour)
   13272     {
   13273       bfd_set_error (bfd_error_wrong_format);
   13274       return -1;
   13275     }
   13276 
   13277   num_phdrs = elf_elfheader (abfd)->e_phnum;
   13278   if (num_phdrs != 0)
   13279     memcpy (phdrs, elf_tdata (abfd)->phdr,
   13280 	    num_phdrs * sizeof (Elf_Internal_Phdr));
   13281 
   13282   return num_phdrs;
   13283 }
   13284 
   13285 enum elf_reloc_type_class
   13286 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   13287 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   13288 			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
   13289 {
   13290   return reloc_class_normal;
   13291 }
   13292 
   13293 /* For RELA architectures, return the relocation value for a
   13294    relocation against a local symbol.  */
   13295 
   13296 bfd_vma
   13297 _bfd_elf_rela_local_sym (bfd *abfd,
   13298 			 Elf_Internal_Sym *sym,
   13299 			 asection **psec,
   13300 			 Elf_Internal_Rela *rel)
   13301 {
   13302   asection *sec = *psec;
   13303   bfd_vma relocation;
   13304 
   13305   relocation = (sec->output_section->vma
   13306 		+ sec->output_offset
   13307 		+ sym->st_value);
   13308   if ((sec->flags & SEC_MERGE)
   13309       && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   13310       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   13311     {
   13312       rel->r_addend =
   13313 	_bfd_merged_section_offset (abfd, psec,
   13314 				    sym->st_value + rel->r_addend);
   13315       if (sec != *psec)
   13316 	{
   13317 	  /* If we have changed the section, and our original section is
   13318 	     marked with SEC_EXCLUDE, it means that the original
   13319 	     SEC_MERGE section has been completely subsumed in some
   13320 	     other SEC_MERGE section.  In this case, we need to leave
   13321 	     some info around for --emit-relocs.  */
   13322 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   13323 	    sec->kept_section = *psec;
   13324 	  sec = *psec;
   13325 	}
   13326       rel->r_addend -= relocation;
   13327       rel->r_addend += sec->output_section->vma + sec->output_offset;
   13328     }
   13329   return relocation;
   13330 }
   13331 
   13332 bfd_vma
   13333 _bfd_elf_rel_local_sym (bfd *abfd,
   13334 			Elf_Internal_Sym *sym,
   13335 			asection **psec,
   13336 			bfd_vma addend)
   13337 {
   13338   asection *sec = *psec;
   13339 
   13340   if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
   13341     return sym->st_value + addend;
   13342 
   13343   return _bfd_merged_section_offset (abfd, psec,
   13344 				     sym->st_value + addend);
   13345 }
   13346 
   13347 /* Adjust an address within a section.  Given OFFSET within SEC, return
   13348    the new offset within the section, based upon changes made to the
   13349    section.  Returns -1 if the offset is now invalid.
   13350    The offset (in abnd out) is in target sized bytes, however big a
   13351    byte may be.  */
   13352 
   13353 bfd_vma
   13354 _bfd_elf_section_offset (bfd *abfd,
   13355 			 struct bfd_link_info *info,
   13356 			 asection *sec,
   13357 			 bfd_vma offset)
   13358 {
   13359   switch (sec->sec_info_type)
   13360     {
   13361     case SEC_INFO_TYPE_STABS:
   13362       return _bfd_stab_section_offset (sec, offset);
   13363 
   13364     case SEC_INFO_TYPE_EH_FRAME:
   13365       return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
   13366 
   13367     case SEC_INFO_TYPE_SFRAME:
   13368       return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
   13369 
   13370     default:
   13371       if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
   13372 	{
   13373 	  /* Reverse the offset.  */
   13374 	  elf_backend_data *bed = get_elf_backend_data (abfd);
   13375 	  bfd_size_type address_size = bed->s->arch_size / 8;
   13376 
   13377 	  /* address_size and sec->size are in octets.  Convert
   13378 	     to bytes before subtracting the original offset.  */
   13379 	  offset = ((sec->size - address_size)
   13380 		    / bfd_octets_per_byte (abfd, sec) - offset);
   13381 	}
   13382       return offset;
   13383     }
   13384 }
   13385 
   13386 long
   13388 _bfd_elf_get_synthetic_symtab (bfd *abfd,
   13389 			       long symcount ATTRIBUTE_UNUSED,
   13390 			       asymbol **syms ATTRIBUTE_UNUSED,
   13391 			       long dynsymcount,
   13392 			       asymbol **dynsyms,
   13393 			       asymbol **ret)
   13394 {
   13395   elf_backend_data *bed = get_elf_backend_data (abfd);
   13396   asection *relplt;
   13397   asymbol *s;
   13398   const char *relplt_name;
   13399   bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   13400   arelent *p;
   13401   long count, i, n;
   13402   size_t size;
   13403   Elf_Internal_Shdr *hdr;
   13404   char *names;
   13405   asection *plt;
   13406 
   13407   *ret = NULL;
   13408 
   13409   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   13410     return 0;
   13411 
   13412   if (dynsymcount <= 0)
   13413     return 0;
   13414 
   13415   if (!bed->plt_sym_val)
   13416     return 0;
   13417 
   13418   relplt_name = bed->relplt_name;
   13419   if (relplt_name == NULL)
   13420     relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
   13421   relplt = bfd_get_section_by_name (abfd, relplt_name);
   13422   if (relplt == NULL)
   13423     return 0;
   13424 
   13425   hdr = &elf_section_data (relplt)->this_hdr;
   13426   if (hdr->sh_link != elf_dynsymtab (abfd)
   13427       || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
   13428     return 0;
   13429 
   13430   plt = bfd_get_section_by_name (abfd, ".plt");
   13431   if (plt == NULL)
   13432     return 0;
   13433 
   13434   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   13435   if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
   13436     return -1;
   13437 
   13438   count = NUM_SHDR_ENTRIES (hdr);
   13439   size = count * sizeof (asymbol);
   13440   p = relplt->relocation;
   13441   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13442     {
   13443       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   13444       if (p->addend != 0)
   13445 	{
   13446 #ifdef BFD64
   13447 	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
   13448 #else
   13449 	  size += sizeof ("+0x") - 1 + 8;
   13450 #endif
   13451 	}
   13452     }
   13453 
   13454   s = *ret = (asymbol *) bfd_malloc (size);
   13455   if (s == NULL)
   13456     return -1;
   13457 
   13458   names = (char *) (s + count);
   13459   p = relplt->relocation;
   13460   n = 0;
   13461   for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
   13462     {
   13463       size_t len;
   13464       bfd_vma addr;
   13465 
   13466       addr = bed->plt_sym_val (i, plt, p);
   13467       if (addr == (bfd_vma) -1)
   13468 	continue;
   13469 
   13470       *s = **p->sym_ptr_ptr;
   13471       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   13472 	 we are defining a symbol, ensure one of them is set.  */
   13473       if ((s->flags & BSF_LOCAL) == 0)
   13474 	s->flags |= BSF_GLOBAL;
   13475       s->flags |= BSF_SYNTHETIC;
   13476       s->section = plt;
   13477       s->value = addr - plt->vma;
   13478       s->name = names;
   13479       s->udata.p = NULL;
   13480       len = strlen ((*p->sym_ptr_ptr)->name);
   13481       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   13482       names += len;
   13483       if (p->addend != 0)
   13484 	{
   13485 	  char buf[30], *a;
   13486 
   13487 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   13488 	  names += sizeof ("+0x") - 1;
   13489 	  bfd_sprintf_vma (abfd, buf, p->addend);
   13490 	  for (a = buf; *a == '0'; ++a)
   13491 	    ;
   13492 	  len = strlen (a);
   13493 	  memcpy (names, a, len);
   13494 	  names += len;
   13495 	}
   13496       memcpy (names, "@plt", sizeof ("@plt"));
   13497       names += sizeof ("@plt");
   13498       ++s, ++n;
   13499     }
   13500 
   13501   return n;
   13502 }
   13503 
   13504 bool
   13505 _bfd_elf_final_write_processing (bfd *abfd)
   13506 {
   13507   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   13508   elf_backend_data *bed = get_elf_backend_data (abfd);
   13509 
   13510   if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13511     i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
   13512 
   13513   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
   13514       || bed->target_os == is_solaris)
   13515     {
   13516       elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
   13517       elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
   13518     }
   13519 
   13520   /* Set the osabi field to ELFOSABI_GNU if the binary contains
   13521      SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
   13522      or STB_GNU_UNIQUE binding.  */
   13523   if (elf_tdata (abfd)->has_gnu_osabi != 0)
   13524     {
   13525       if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
   13526 	i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
   13527       else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
   13528 	       && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
   13529 	{
   13530 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
   13531 	    _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
   13532 				  "and FreeBSD targets"));
   13533 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
   13534 	    _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
   13535 				  "only by GNU and FreeBSD targets"));
   13536 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
   13537 	    _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
   13538 				  "only by GNU and FreeBSD targets"));
   13539 	  if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
   13540 	    _bfd_error_handler (_("GNU_RETAIN section is supported "
   13541 				  "only by GNU and FreeBSD targets"));
   13542 	  bfd_set_error (bfd_error_sorry);
   13543 	  return false;
   13544 	}
   13545     }
   13546   return true;
   13547 }
   13548 
   13549 
   13550 /* Return TRUE for ELF symbol types that represent functions.
   13551    This is the default version of this function, which is sufficient for
   13552    most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
   13553 
   13554 bool
   13555 _bfd_elf_is_function_type (unsigned int type)
   13556 {
   13557   return (type == STT_FUNC
   13558 	  || type == STT_GNU_IFUNC);
   13559 }
   13560 
   13561 /* If the ELF symbol SYM might be a function in SEC, return the
   13562    function size and set *CODE_OFF to the function's entry point,
   13563    otherwise return zero.  */
   13564 
   13565 bfd_size_type
   13566 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   13567 			     bfd_vma *code_off)
   13568 {
   13569   bfd_size_type size;
   13570   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   13571 
   13572   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   13573 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
   13574       || sym->section != sec)
   13575     return 0;
   13576 
   13577   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   13578 
   13579   /* In theory we should check that the symbol's type satisfies
   13580      _bfd_elf_is_function_type(), but there are some function-like
   13581      symbols which would fail this test.  (eg _start).  Instead
   13582      we check for hidden, local, notype symbols with zero size.
   13583      This type of symbol is generated by the annobin plugin for gcc
   13584      and clang, and should not be considered to be a function symbol.  */
   13585   if (size == 0
   13586       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   13587       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   13588       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   13589     return 0;
   13590 
   13591   *code_off = sym->value;
   13592   /* Do not return 0 for the function's size.  */
   13593   return size ? size : 1;
   13594 }
   13595 
   13596 /* Set to non-zero to enable some debug messages.  */
   13597 #define DEBUG_SECONDARY_RELOCS	 0
   13598 
   13599 /* An internal-to-the-bfd-library only section type
   13600    used to indicate a cached secondary reloc section.  */
   13601 #define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
   13602 
   13603 /* Create a BFD section to hold a secondary reloc section.  */
   13604 
   13605 bool
   13606 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
   13607 				       Elf_Internal_Shdr *hdr,
   13608 				       const char * name,
   13609 				       unsigned int shindex)
   13610 {
   13611   /* We only support RELA secondary relocs.  */
   13612   if (hdr->sh_type != SHT_RELA)
   13613     return false;
   13614 
   13615 #if DEBUG_SECONDARY_RELOCS
   13616   fprintf (stderr, "secondary reloc section %s encountered\n", name);
   13617 #endif
   13618   hdr->sh_type = SHT_SECONDARY_RELOC;
   13619   return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
   13620 }
   13621 
   13622 /* Read in any secondary relocs associated with SEC.  */
   13623 
   13624 bool
   13625 _bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
   13626 					asection *  sec,
   13627 					asymbol **  symbols,
   13628 					bool dynamic)
   13629 {
   13630   elf_backend_data *ebd = get_elf_backend_data (abfd);
   13631   asection * relsec;
   13632   bool result = true;
   13633   bfd_vma (*r_sym) (bfd_vma);
   13634   ufile_ptr filesize;
   13635 
   13636 #if BFD_DEFAULT_TARGET_SIZE > 32
   13637   if (bfd_arch_bits_per_address (abfd) != 32)
   13638     r_sym = elf64_r_sym;
   13639   else
   13640 #endif
   13641     r_sym = elf32_r_sym;
   13642 
   13643   if (!elf_section_data (sec)->has_secondary_relocs)
   13644     return true;
   13645 
   13646   /* Discover if there are any secondary reloc sections
   13647      associated with SEC.  */
   13648   filesize = bfd_get_file_size (abfd);
   13649   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13650     {
   13651       Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
   13652 
   13653       if (hdr->sh_type == SHT_SECONDARY_RELOC
   13654 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
   13655 	  && (hdr->sh_entsize == ebd->s->sizeof_rel
   13656 	      || hdr->sh_entsize == ebd->s->sizeof_rela))
   13657 	{
   13658 	  bfd_byte * native_relocs;
   13659 	  bfd_byte * native_reloc;
   13660 	  arelent * internal_relocs;
   13661 	  arelent * internal_reloc;
   13662 	  size_t i;
   13663 	  unsigned int entsize;
   13664 	  unsigned int symcount;
   13665 	  bfd_size_type reloc_count;
   13666 	  size_t amt;
   13667 
   13668 	  if (ebd->elf_info_to_howto == NULL)
   13669 	    return false;
   13670 
   13671 #if DEBUG_SECONDARY_RELOCS
   13672 	  fprintf (stderr, "read secondary relocs for %s from %s\n",
   13673 		   sec->name, relsec->name);
   13674 #endif
   13675 	  entsize = hdr->sh_entsize;
   13676 
   13677 	  if (filesize != 0
   13678 	      && ((ufile_ptr) hdr->sh_offset > filesize
   13679 		  || hdr->sh_size > filesize - hdr->sh_offset))
   13680 	    {
   13681 	      bfd_set_error (bfd_error_file_truncated);
   13682 	      result = false;
   13683 	      continue;
   13684 	    }
   13685 
   13686 	  native_relocs = bfd_malloc (hdr->sh_size);
   13687 	  if (native_relocs == NULL)
   13688 	    {
   13689 	      result = false;
   13690 	      continue;
   13691 	    }
   13692 
   13693 	  reloc_count = NUM_SHDR_ENTRIES (hdr);
   13694 	  if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
   13695 	    {
   13696 	      free (native_relocs);
   13697 	      bfd_set_error (bfd_error_file_too_big);
   13698 	      result = false;
   13699 	      continue;
   13700 	    }
   13701 
   13702 	  internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   13703 	  if (internal_relocs == NULL)
   13704 	    {
   13705 	      free (native_relocs);
   13706 	      result = false;
   13707 	      continue;
   13708 	    }
   13709 
   13710 	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
   13711 	      || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
   13712 	    {
   13713 	      free (native_relocs);
   13714 	      /* The internal_relocs will be freed when
   13715 		 the memory for the bfd is released.  */
   13716 	      result = false;
   13717 	      continue;
   13718 	    }
   13719 
   13720 	  if (dynamic)
   13721 	    symcount = bfd_get_dynamic_symcount (abfd);
   13722 	  else
   13723 	    symcount = bfd_get_symcount (abfd);
   13724 
   13725 	  for (i = 0, internal_reloc = internal_relocs,
   13726 		 native_reloc = native_relocs;
   13727 	       i < reloc_count;
   13728 	       i++, internal_reloc++, native_reloc += entsize)
   13729 	    {
   13730 	      bool res;
   13731 	      Elf_Internal_Rela rela;
   13732 
   13733 	      if (entsize == ebd->s->sizeof_rel)
   13734 		ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
   13735 	      else /* entsize == ebd->s->sizeof_rela */
   13736 		ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
   13737 
   13738 	      /* The address of an ELF reloc is section relative for an object
   13739 		 file, and absolute for an executable file or shared library.
   13740 		 The address of a normal BFD reloc is always section relative,
   13741 		 and the address of a dynamic reloc is absolute..  */
   13742 	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
   13743 		internal_reloc->address = rela.r_offset;
   13744 	      else
   13745 		internal_reloc->address = rela.r_offset - sec->vma;
   13746 
   13747 	      if (r_sym (rela.r_info) == STN_UNDEF)
   13748 		{
   13749 		  /* FIXME: This and the error case below mean that we
   13750 		     have a symbol on relocs that is not elf_symbol_type.  */
   13751 		  internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   13752 		}
   13753 	      else if (r_sym (rela.r_info) > symcount)
   13754 		{
   13755 		  _bfd_error_handler
   13756 		    /* xgettext:c-format */
   13757 		    (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
   13758 		     abfd, sec, i, (long) r_sym (rela.r_info));
   13759 		  bfd_set_error (bfd_error_bad_value);
   13760 		  internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   13761 		  result = false;
   13762 		}
   13763 	      else
   13764 		{
   13765 		  asymbol **ps;
   13766 
   13767 		  ps = symbols + r_sym (rela.r_info) - 1;
   13768 		  internal_reloc->sym_ptr_ptr = ps;
   13769 		  /* Make sure that this symbol is not removed by strip.  */
   13770 		  (*ps)->flags |= BSF_KEEP;
   13771 		}
   13772 
   13773 	      internal_reloc->addend = rela.r_addend;
   13774 
   13775 	      res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
   13776 	      if (! res || internal_reloc->howto == NULL)
   13777 		{
   13778 #if DEBUG_SECONDARY_RELOCS
   13779 		  fprintf (stderr,
   13780 			   "there is no howto associated with reloc %lx\n",
   13781 			   rela.r_info);
   13782 #endif
   13783 		  result = false;
   13784 		}
   13785 	    }
   13786 
   13787 	  free (native_relocs);
   13788 	  /* Store the internal relocs.  */
   13789 	  relsec->sec_info = internal_relocs;
   13790 	}
   13791     }
   13792 
   13793   return result;
   13794 }
   13795 
   13796 /* Set the ELF section header fields of an output secondary reloc section.  */
   13797 
   13798 bool
   13799 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
   13800 				      bfd *obfd ATTRIBUTE_UNUSED,
   13801 				      const Elf_Internal_Shdr *isection,
   13802 				      Elf_Internal_Shdr *osection)
   13803 {
   13804   asection * isec;
   13805   asection * osec;
   13806   struct bfd_elf_section_data * esd;
   13807 
   13808   if (isection == NULL)
   13809     return false;
   13810 
   13811   if (isection->sh_type != SHT_SECONDARY_RELOC)
   13812     return true;
   13813 
   13814   isec = isection->bfd_section;
   13815   if (isec == NULL)
   13816     return false;
   13817 
   13818   osec = osection->bfd_section;
   13819   if (osec == NULL)
   13820     return false;
   13821 
   13822   BFD_ASSERT (osec->sec_info == NULL);
   13823   osec->sec_info = isec->sec_info;
   13824   osection->sh_type = SHT_RELA;
   13825   osection->sh_link = elf_onesymtab (obfd);
   13826   if (osection->sh_link == 0)
   13827     {
   13828       /* There is no symbol table - we are hosed...  */
   13829       _bfd_error_handler
   13830 	/* xgettext:c-format */
   13831 	(_("%pB(%pA): link section cannot be set"
   13832 	   " because the output file does not have a symbol table"),
   13833 	obfd, osec);
   13834       bfd_set_error (bfd_error_bad_value);
   13835       return false;
   13836     }
   13837 
   13838   /* Find the output section that corresponds to the isection's
   13839      sh_info link.  */
   13840   if (isection->sh_info == 0
   13841       || isection->sh_info >= elf_numsections (ibfd))
   13842     {
   13843       _bfd_error_handler
   13844 	/* xgettext:c-format */
   13845 	(_("%pB(%pA): info section index is invalid"),
   13846 	obfd, osec);
   13847       bfd_set_error (bfd_error_bad_value);
   13848       return false;
   13849     }
   13850 
   13851   isection = elf_elfsections (ibfd)[isection->sh_info];
   13852 
   13853   if (isection == NULL
   13854       || isection->bfd_section == NULL
   13855       || isection->bfd_section->output_section == NULL)
   13856     {
   13857       _bfd_error_handler
   13858 	/* xgettext:c-format */
   13859 	(_("%pB(%pA): info section index cannot be set"
   13860 	   " because the section is not in the output"),
   13861 	obfd, osec);
   13862       bfd_set_error (bfd_error_bad_value);
   13863       return false;
   13864     }
   13865 
   13866   esd = elf_section_data (isection->bfd_section->output_section);
   13867   BFD_ASSERT (esd != NULL);
   13868   osection->sh_info = esd->this_idx;
   13869   esd->has_secondary_relocs = true;
   13870 #if DEBUG_SECONDARY_RELOCS
   13871   fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
   13872 	   osec->name, osection->sh_link, osection->sh_info);
   13873   fprintf (stderr, "mark section %s as having secondary relocs\n",
   13874 	   bfd_section_name (isection->bfd_section->output_section));
   13875 #endif
   13876 
   13877   return true;
   13878 }
   13879 
   13880 /* Write out a secondary reloc section.
   13881 
   13882    FIXME: Currently this function can result in a serious performance penalty
   13883    for files with secondary relocs and lots of sections.  The proper way to
   13884    fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
   13885    relocs together and then to have this function just walk that chain.  */
   13886 
   13887 bool
   13888 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   13889 {
   13890   elf_backend_data *ebd = get_elf_backend_data (abfd);
   13891   bfd_vma addr_offset;
   13892   asection * relsec;
   13893   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   13894   bool result = true;
   13895 
   13896   if (sec == NULL)
   13897     return false;
   13898 
   13899 #if BFD_DEFAULT_TARGET_SIZE > 32
   13900   if (bfd_arch_bits_per_address (abfd) != 32)
   13901     r_info = elf64_r_info;
   13902   else
   13903 #endif
   13904     r_info = elf32_r_info;
   13905 
   13906   /* The address of an ELF reloc is section relative for an object
   13907      file, and absolute for an executable file or shared library.
   13908      The address of a BFD reloc is always section relative.  */
   13909   addr_offset = 0;
   13910   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   13911     addr_offset = sec->vma;
   13912 
   13913   /* Discover if there are any secondary reloc sections
   13914      associated with SEC.  */
   13915   for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
   13916     {
   13917       const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
   13918       Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
   13919 
   13920       if (hdr->sh_type == SHT_RELA
   13921 	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
   13922 	{
   13923 	  asymbol *    last_sym;
   13924 	  int          last_sym_idx;
   13925 	  size_t       reloc_count;
   13926 	  size_t       idx;
   13927 	  bfd_size_type entsize;
   13928 	  arelent *    src_irel;
   13929 	  bfd_byte *   dst_rela;
   13930 
   13931 	  if (hdr->contents != NULL)
   13932 	    {
   13933 	      _bfd_error_handler
   13934 		/* xgettext:c-format */
   13935 		(_("%pB(%pA): error: secondary reloc section processed twice"),
   13936 		 abfd, relsec);
   13937 	      bfd_set_error (bfd_error_bad_value);
   13938 	      result = false;
   13939 	      continue;
   13940 	    }
   13941 
   13942 	  entsize = hdr->sh_entsize;
   13943 	  if (entsize == 0)
   13944 	    {
   13945 	      _bfd_error_handler
   13946 		/* xgettext:c-format */
   13947 		(_("%pB(%pA): error: secondary reloc section"
   13948 		   " has zero sized entries"),
   13949 		 abfd, relsec);
   13950 	      bfd_set_error (bfd_error_bad_value);
   13951 	      result = false;
   13952 	      continue;
   13953 	    }
   13954 	  else if (entsize != ebd->s->sizeof_rel
   13955 		   && entsize != ebd->s->sizeof_rela)
   13956 	    {
   13957 	      _bfd_error_handler
   13958 		/* xgettext:c-format */
   13959 		(_("%pB(%pA): error: secondary reloc section"
   13960 		   " has non-standard sized entries"),
   13961 		 abfd, relsec);
   13962 	      bfd_set_error (bfd_error_bad_value);
   13963 	      result = false;
   13964 	      continue;
   13965 	    }
   13966 
   13967 	  reloc_count = hdr->sh_size / entsize;
   13968 	  hdr->sh_size = entsize * reloc_count;
   13969 	  if (reloc_count == 0)
   13970 	    {
   13971 	      _bfd_error_handler
   13972 		/* xgettext:c-format */
   13973 		(_("%pB(%pA): error: secondary reloc section is empty!"),
   13974 		 abfd, relsec);
   13975 	      bfd_set_error (bfd_error_bad_value);
   13976 	      result = false;
   13977 	      continue;
   13978 	    }
   13979 
   13980 	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
   13981 	  if (hdr->contents == NULL)
   13982 	    continue;
   13983 	  relsec->alloced = 1;
   13984 
   13985 #if DEBUG_SECONDARY_RELOCS
   13986 	  fprintf (stderr, "write %u secondary relocs for %s from %s\n",
   13987 		   reloc_count, sec->name, relsec->name);
   13988 #endif
   13989 	  last_sym = NULL;
   13990 	  last_sym_idx = 0;
   13991 	  dst_rela = hdr->contents;
   13992 	  src_irel = sec->sec_info;
   13993 	  if (src_irel == NULL)
   13994 	    {
   13995 	      _bfd_error_handler
   13996 		/* xgettext:c-format */
   13997 		(_("%pB(%pA): error: internal relocs missing"
   13998 		   " for secondary reloc section"),
   13999 		 abfd, relsec);
   14000 	      bfd_set_error (bfd_error_bad_value);
   14001 	      result = false;
   14002 	      continue;
   14003 	    }
   14004 
   14005 	  for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
   14006 	    {
   14007 	      Elf_Internal_Rela src_rela;
   14008 	      arelent *ptr;
   14009 	      asymbol *sym;
   14010 	      int n;
   14011 
   14012 	      ptr = src_irel + idx;
   14013 	      if (ptr == NULL)
   14014 		{
   14015 		  _bfd_error_handler
   14016 		    /* xgettext:c-format */
   14017 		    (_("%pB(%pA): error: reloc table entry %zu is empty"),
   14018 		     abfd, relsec, idx);
   14019 		  bfd_set_error (bfd_error_bad_value);
   14020 		  result = false;
   14021 		  break;
   14022 		}
   14023 
   14024 	      if (ptr->sym_ptr_ptr == NULL)
   14025 		{
   14026 		  /* FIXME: Is this an error ? */
   14027 		  n = 0;
   14028 		}
   14029 	      else
   14030 		{
   14031 		  sym = *ptr->sym_ptr_ptr;
   14032 
   14033 		  if (sym == last_sym)
   14034 		    n = last_sym_idx;
   14035 		  else
   14036 		    {
   14037 		      n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
   14038 		      if (n < 0)
   14039 			{
   14040 			  _bfd_error_handler
   14041 			    /* xgettext:c-format */
   14042 			    (_("%pB(%pA): error: secondary reloc %zu"
   14043 			       " references a missing symbol"),
   14044 			     abfd, relsec, idx);
   14045 			  bfd_set_error (bfd_error_bad_value);
   14046 			  result = false;
   14047 			  n = 0;
   14048 			}
   14049 
   14050 		      last_sym = sym;
   14051 		      last_sym_idx = n;
   14052 		    }
   14053 
   14054 		  if (sym->the_bfd != NULL
   14055 		      && sym->the_bfd->xvec != abfd->xvec
   14056 		      && ! _bfd_elf_validate_reloc (abfd, ptr))
   14057 		    {
   14058 		      _bfd_error_handler
   14059 			/* xgettext:c-format */
   14060 			(_("%pB(%pA): error: secondary reloc %zu"
   14061 			   " references a deleted symbol"),
   14062 			 abfd, relsec, idx);
   14063 		      bfd_set_error (bfd_error_bad_value);
   14064 		      result = false;
   14065 		      n = 0;
   14066 		    }
   14067 		}
   14068 
   14069 	      src_rela.r_offset = ptr->address + addr_offset;
   14070 	      if (ptr->howto == NULL)
   14071 		{
   14072 		  _bfd_error_handler
   14073 		    /* xgettext:c-format */
   14074 		    (_("%pB(%pA): error: secondary reloc %zu"
   14075 		       " is of an unknown type"),
   14076 		     abfd, relsec, idx);
   14077 		  bfd_set_error (bfd_error_bad_value);
   14078 		  result = false;
   14079 		  src_rela.r_info = r_info (0, 0);
   14080 		}
   14081 	      else
   14082 		src_rela.r_info = r_info (n, ptr->howto->type);
   14083 	      src_rela.r_addend = ptr->addend;
   14084 
   14085 	      if (entsize == ebd->s->sizeof_rel)
   14086 		ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
   14087 	      else /* entsize == ebd->s->sizeof_rela */
   14088 		ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
   14089 	    }
   14090 	}
   14091     }
   14092 
   14093   return result;
   14094 }
   14095 
   14096 /* Mmap in section contents.  */
   14097 
   14098 static bool
   14099 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
   14100 {
   14101 #ifdef USE_MMAP
   14102   elf_backend_data *bed = get_elf_backend_data (abfd);
   14103   if (bed->use_mmap
   14104       && sec->compress_status == COMPRESS_SECTION_NONE
   14105       && (sec->flags & SEC_LINKER_CREATED) == 0)
   14106     {
   14107       /* Use mmap only if section size >= the minimum mmap section
   14108 	 size.  */
   14109       size_t readsz = bfd_get_section_limit_octets (abfd, sec);
   14110       size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
   14111       if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
   14112 	{
   14113 	  if (sec->contents != NULL)
   14114 	    {
   14115 	      if (!sec->mmapped_p)
   14116 		abort ();
   14117 	      *buf = sec->contents;
   14118 	      return true;
   14119 	    }
   14120 	  if (sec->mmapped_p)
   14121 	    abort ();
   14122 	  sec->mmapped_p = 1;
   14123 
   14124 	  /* We can't use the final link preallocated buffer for mmap.  */
   14125 	  *buf = NULL;
   14126 	}
   14127     }
   14128 #endif
   14129   /* FIXME: We should not get here if sec->alloced is set.  */
   14130   bool ret = bfd_get_full_section_contents (abfd, sec, buf);
   14131   if (ret && sec->mmapped_p)
   14132     *buf = sec->contents;
   14133   return ret;
   14134 }
   14135 
   14136 /* Mmap in section contents.  */
   14137 
   14138 bool
   14139 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
   14140 {
   14141   *buf = NULL;
   14142   return elf_mmap_section_contents (abfd, sec, buf);
   14143 }
   14144 
   14145 /* Mmap in the full section contents for the final link.  */
   14146 
   14147 bool
   14148 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
   14149 				     bfd_byte **buf)
   14150 {
   14151   return elf_mmap_section_contents (abfd, sec, buf);
   14152 }
   14153 
   14154 /* Munmap section contents.  */
   14155 
   14156 void
   14157 _bfd_elf_munmap_section_contents (asection *sec, void *contents)
   14158 {
   14159   /* NB: Since _bfd_elf_munmap_section_contents is called like free,
   14160      CONTENTS may be NULL.  */
   14161   if (contents == NULL)
   14162     return;
   14163 
   14164   if (sec->alloced
   14165       /* What a tangled web we weave with section contents.
   14166 	 FIXME: We shouldn't need to test anything but sec->alloced
   14167 	 here, but there are cases where a buffer is allocated for a
   14168 	 section but then another buffer is malloc'd anyway.  eg.
   14169 	 trace through ld-elf/eh4 testcase on x86_64.  */
   14170       && (sec->contents == contents
   14171 	  || elf_section_data (sec)->this_hdr.contents == contents))
   14172     return;
   14173 
   14174   /* Don't leave pointers to data we are about to munmap or free.  */
   14175   if (sec->contents == contents)
   14176     sec->contents = NULL;
   14177   if (elf_section_data (sec)->this_hdr.contents == contents)
   14178     elf_section_data (sec)->this_hdr.contents = NULL;
   14179 
   14180 #ifdef USE_MMAP
   14181   if (sec->mmapped_p)
   14182     {
   14183       /* When _bfd_elf_mmap_section_contents returns CONTENTS as
   14184 	 malloced, CONTENTS_ADDR is set to NULL.  */
   14185       if (elf_section_data (sec)->contents_addr != NULL)
   14186 	{
   14187 	  /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
   14188 	  if (munmap (elf_section_data (sec)->contents_addr,
   14189 		      elf_section_data (sec)->contents_size) != 0)
   14190 	    abort ();
   14191 	  sec->mmapped_p = 0;
   14192 	  elf_section_data (sec)->contents_addr = NULL;
   14193 	  elf_section_data (sec)->contents_size = 0;
   14194 	  return;
   14195 	}
   14196     }
   14197 #endif
   14198 
   14199   free (contents);
   14200 }
   14201 
   14202 /* Munmap the full section contents for the final link.  */
   14203 
   14204 void
   14205 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
   14206 {
   14207 #ifdef USE_MMAP
   14208   if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
   14209     {
   14210       /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
   14211 	 malloced, CONTENTS_ADDR is set to NULL.  */
   14212       /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
   14213       if (munmap (elf_section_data (sec)->contents_addr,
   14214 		  elf_section_data (sec)->contents_size) != 0)
   14215 	abort ();
   14216       sec->mmapped_p = 0;
   14217       sec->contents = NULL;
   14218       elf_section_data (sec)->this_hdr.contents = NULL;
   14219       elf_section_data (sec)->contents_addr = NULL;
   14220       elf_section_data (sec)->contents_size = 0;
   14221     }
   14222 #endif
   14223 }
   14224